diff --git "a/queries.jsonl" "b/queries.jsonl" new file mode 100644--- /dev/null +++ "b/queries.jsonl" @@ -0,0 +1,3763 @@ +{"id": "00000", "text": "An accordion is a string (yes, in the real world accordions are musical instruments, but let's forget about it for a while) which can be represented as a concatenation of: an opening bracket (ASCII code $091$), a colon (ASCII code $058$), some (possibly zero) vertical line characters (ASCII code $124$), another colon, and a closing bracket (ASCII code $093$). The length of the accordion is the number of characters in it.\n\nFor example, [::], [:||:] and [:|||:] are accordions having length $4$, $6$ and $7$. (:|:), {:||:}, [:], ]:||:[ are not accordions. \n\nYou are given a string $s$. You want to transform it into an accordion by removing some (possibly zero) characters from it. Note that you may not insert new characters or reorder existing ones. Is it possible to obtain an accordion by removing characters from $s$, and if so, what is the maximum possible length of the result?\n\n\n-----Input-----\n\nThe only line contains one string $s$ ($1 \\le |s| \\le 500000$). It consists of lowercase Latin letters and characters [, ], : and |.\n\n\n-----Output-----\n\nIf it is not possible to obtain an accordion by removing some characters from $s$, print $-1$. Otherwise print maximum possible length of the resulting accordion.\n\n\n-----Examples-----\nInput\n|[a:b:|]\n\nOutput\n4\n\nInput\n|]:[|:]\n\nOutput\n-1"} +{"id": "00001", "text": "Anton has the integer x. He is interested what positive integer, which doesn't exceed x, has the maximum sum of digits.\n\nYour task is to help Anton and to find the integer that interests him. If there are several such integers, determine the biggest of them. \n\n\n-----Input-----\n\nThe first line contains the positive integer x (1 \u2264 x \u2264 10^18) \u2014 the integer which Anton has. \n\n\n-----Output-----\n\nPrint the positive integer which doesn't exceed x and has the maximum sum of digits. If there are several such integers, print the biggest of them. Printed integer must not contain leading zeros.\n\n\n-----Examples-----\nInput\n100\n\nOutput\n99\n\nInput\n48\n\nOutput\n48\n\nInput\n521\n\nOutput\n499"} +{"id": "00002", "text": "Apart from having lots of holidays throughout the year, residents of Berland also have whole lucky years. Year is considered lucky if it has no more than 1 non-zero digit in its number. So years 100, 40000, 5 are lucky and 12, 3001 and 12345 are not.\n\nYou are given current year in Berland. Your task is to find how long will residents of Berland wait till the next lucky year.\n\n\n-----Input-----\n\nThe first line contains integer number n (1 \u2264 n \u2264 10^9) \u2014 current year in Berland.\n\n\n-----Output-----\n\nOutput amount of years from the current year to the next lucky one.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n1\n\nInput\n201\n\nOutput\n99\n\nInput\n4000\n\nOutput\n1000\n\n\n\n-----Note-----\n\nIn the first example next lucky year is 5. In the second one \u2014 300. In the third \u2014 5000."} +{"id": "00003", "text": "You have a long fence which consists of $n$ sections. Unfortunately, it is not painted, so you decided to hire $q$ painters to paint it. $i$-th painter will paint all sections $x$ such that $l_i \\le x \\le r_i$.\n\nUnfortunately, you are on a tight budget, so you may hire only $q - 2$ painters. Obviously, only painters you hire will do their work.\n\nYou want to maximize the number of painted sections if you choose $q - 2$ painters optimally. A section is considered painted if at least one painter paints it.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $q$ ($3 \\le n, q \\le 5000$) \u2014 the number of sections and the number of painters availible for hire, respectively.\n\nThen $q$ lines follow, each describing one of the painters: $i$-th line contains two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le n$).\n\n\n-----Output-----\n\nPrint one integer \u2014 maximum number of painted sections if you hire $q - 2$ painters.\n\n\n-----Examples-----\nInput\n7 5\n1 4\n4 5\n5 6\n6 7\n3 5\n\nOutput\n7\n\nInput\n4 3\n1 1\n2 2\n3 4\n\nOutput\n2\n\nInput\n4 4\n1 1\n2 2\n2 3\n3 4\n\nOutput\n3"} +{"id": "00004", "text": "Jamie loves sleeping. One day, he decides that he needs to wake up at exactly hh: mm. However, he hates waking up, so he wants to make waking up less painful by setting the alarm at a lucky time. He will then press the snooze button every x minutes until hh: mm is reached, and only then he will wake up. He wants to know what is the smallest number of times he needs to press the snooze button.\n\nA time is considered lucky if it contains a digit '7'. For example, 13: 07 and 17: 27 are lucky, while 00: 48 and 21: 34 are not lucky.\n\nNote that it is not necessary that the time set for the alarm and the wake-up time are on the same day. It is guaranteed that there is a lucky time Jamie can set so that he can wake at hh: mm.\n\nFormally, find the smallest possible non-negative integer y such that the time representation of the time x\u00b7y minutes before hh: mm contains the digit '7'.\n\nJamie uses 24-hours clock, so after 23: 59 comes 00: 00.\n\n\n-----Input-----\n\nThe first line contains a single integer x (1 \u2264 x \u2264 60).\n\nThe second line contains two two-digit integers, hh and mm (00 \u2264 hh \u2264 23, 00 \u2264 mm \u2264 59).\n\n\n-----Output-----\n\nPrint the minimum number of times he needs to press the button.\n\n\n-----Examples-----\nInput\n3\n11 23\n\nOutput\n2\n\nInput\n5\n01 07\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, Jamie needs to wake up at 11:23. So, he can set his alarm at 11:17. He would press the snooze button when the alarm rings at 11:17 and at 11:20.\n\nIn the second sample, Jamie can set his alarm at exactly at 01:07 which is lucky."} +{"id": "00005", "text": "Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive) for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible.\n\nEach second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab i, then she can move it to the tab max(i - 1, a) or to the tab min(i + 1, b)) or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab i, she can close all the tabs with indices from segment [a, i - 1] or from segment [i + 1, b]). In the aforementioned expressions a and b denote the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7 are closed, then a = 3, b = 6.\n\nWhat is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from l to r inclusive opened?\n\n\n-----Input-----\n\nThe only line of input contains four integer numbers n, pos, l, r (1 \u2264 n \u2264 100, 1 \u2264 pos \u2264 n, 1 \u2264 l \u2264 r \u2264 n) \u2014 the number of the tabs, the cursor position and the segment which Luba needs to leave opened.\n\n\n-----Output-----\n\nPrint one integer equal to the minimum number of seconds required to close all the tabs outside the segment [l, r].\n\n\n-----Examples-----\nInput\n6 3 2 4\n\nOutput\n5\n\nInput\n6 3 1 3\n\nOutput\n1\n\nInput\n5 2 1 5\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the mouse cursor to the tab 3, then to the tab 4, and then close all the tabs to the right of it.\n\nIn the second test she only needs to close all the tabs to the right of the current position of the cursor.\n\nIn the third test Luba doesn't need to do anything."} +{"id": "00006", "text": "You are fighting with Zmei Gorynich \u2014 a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads! \n\n $m$ \n\nInitially Zmei Gorynich has $x$ heads. You can deal $n$ types of blows. If you deal a blow of the $i$-th type, you decrease the number of Gorynich's heads by $min(d_i, curX)$, there $curX$ is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows $h_i$ new heads. If $curX = 0$ then Gorynich is defeated. \n\nYou can deal each blow any number of times, in any order.\n\nFor example, if $curX = 10$, $d = 7$, $h = 10$ then the number of heads changes to $13$ (you cut $7$ heads off, but then Zmei grows $10$ new ones), but if $curX = 10$, $d = 11$, $h = 100$ then number of heads changes to $0$ and Zmei Gorynich is considered defeated.\n\nCalculate the minimum number of blows to defeat Zmei Gorynich!\n\nYou have to answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2013 the number of queries.\n\nThe first line of each query contains two integers $n$ and $x$ ($1 \\le n \\le 100$, $1 \\le x \\le 10^9$) \u2014 the number of possible types of blows and the number of heads Zmei initially has, respectively.\n\nThe following $n$ lines of each query contain the descriptions of types of blows you can deal. The $i$-th line contains two integers $d_i$ and $h_i$ ($1 \\le d_i, h_i \\le 10^9$) \u2014 the description of the $i$-th blow.\n\n\n-----Output-----\n\nFor each query print the minimum number of blows you have to deal to defeat Zmei Gorynich. \n\nIf Zmei Gorynuch cannot be defeated print $-1$.\n\n\n-----Example-----\nInput\n3\n3 10\n6 3\n8 2\n1 4\n4 10\n4 1\n3 2\n2 6\n1 100\n2 15\n10 11\n14 100\n\nOutput\n2\n3\n-1\n\n\n\n-----Note-----\n\nIn the first query you can deal the first blow (after that the number of heads changes to $10 - 6 + 3 = 7$), and then deal the second blow.\n\nIn the second query you just deal the first blow three times, and Zmei is defeated. \n\nIn third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?"} +{"id": "00007", "text": "Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:\n\n\"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away...\"\n\nMore formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens: m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account). Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing. \n\nAnton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!\n\n\n-----Input-----\n\nThe only line of the input contains two integers n and m (1 \u2264 n, m \u2264 10^18)\u00a0\u2014 the capacity of the barn and the number of grains that are brought every day.\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 the number of the day when the barn will become empty for the first time. Days are numbered starting with one.\n\n\n-----Examples-----\nInput\n5 2\n\nOutput\n4\n\nInput\n8 1\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens: At the beginning of the first day grain is brought to the barn. It's full, so nothing happens. At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain. At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it. At the end of the second day two sparrows come. 5 - 2 = 3 grains remain. At the beginning of the third day two grains are brought. The barn becomes full again. At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain. At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain. At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty. \n\nSo the answer is 4, because by the end of the fourth day the barn becomes empty."} +{"id": "00008", "text": "Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from $1$ to $9$). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, $\\ldots$, 9m, 1p, 2p, $\\ldots$, 9p, 1s, 2s, $\\ldots$, 9s.\n\nIn order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.\n\nDo you know the minimum number of extra suited tiles she needs to draw so that she can win?\n\nHere are some useful definitions in this game: A mentsu, also known as meld, is formed by a koutsu or a shuntsu; A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. \n\nSome examples: [2m, 3p, 2s, 4m, 1s, 2s, 4s] \u2014 it contains no koutsu or shuntsu, so it includes no mentsu; [4s, 3m, 3p, 4s, 5p, 4s, 5p] \u2014 it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] \u2014 it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. \n\nNote that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.\n\n\n-----Input-----\n\nThe only line contains three strings\u00a0\u2014 the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from $1$ to $9$ and the second character is m, p or s.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of extra suited tiles she needs to draw.\n\n\n-----Examples-----\nInput\n1s 2s 3s\n\nOutput\n0\n\nInput\n9m 9m 9m\n\nOutput\n0\n\nInput\n3p 9m 2p\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, Tokitsukaze already has a shuntsu.\n\nIn the second example, Tokitsukaze already has a koutsu.\n\nIn the third example, Tokitsukaze can get a shuntsu by drawing one suited tile\u00a0\u2014 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p]."} +{"id": "00009", "text": "Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has stolen his precious sofa! And how can one perform well with such a major loss?\n\nFortunately, the thief had left a note for Grandpa Maks. This note got Maks to the sofa storehouse. Still he had no idea which sofa belongs to him as they all looked the same!\n\nThe storehouse is represented as matrix n \u00d7 m. Every sofa takes two neighbouring by some side cells. No cell is covered by more than one sofa. There can be empty cells.\n\nSofa A is standing to the left of sofa B if there exist two such cells a and b that x_{a} < x_{b}, a is covered by A and b is covered by B. Sofa A is standing to the top of sofa B if there exist two such cells a and b that y_{a} < y_{b}, a is covered by A and b is covered by B. Right and bottom conditions are declared the same way. \n\nNote that in all conditions A \u2260 B. Also some sofa A can be both to the top of another sofa B and to the bottom of it. The same is for left and right conditions.\n\nThe note also stated that there are cnt_{l} sofas to the left of Grandpa Maks's sofa, cnt_{r} \u2014 to the right, cnt_{t} \u2014 to the top and cnt_{b} \u2014 to the bottom.\n\nGrandpa Maks asks you to help him to identify his sofa. It is guaranteed that there is no more than one sofa of given conditions.\n\nOutput the number of Grandpa Maks's sofa. If there is no such sofa that all the conditions are met for it then output -1.\n\n\n-----Input-----\n\nThe first line contains one integer number d (1 \u2264 d \u2264 10^5) \u2014 the number of sofas in the storehouse.\n\nThe second line contains two integer numbers n, m (1 \u2264 n, m \u2264 10^5) \u2014 the size of the storehouse.\n\nNext d lines contains four integer numbers x_1, y_1, x_2, y_2 (1 \u2264 x_1, x_2 \u2264 n, 1 \u2264 y_1, y_2 \u2264 m) \u2014 coordinates of the i-th sofa. It is guaranteed that cells (x_1, y_1) and (x_2, y_2) have common side, (x_1, y_1) \u2260 (x_2, y_2) and no cell is covered by more than one sofa.\n\nThe last line contains four integer numbers cnt_{l}, cnt_{r}, cnt_{t}, cnt_{b} (0 \u2264 cnt_{l}, cnt_{r}, cnt_{t}, cnt_{b} \u2264 d - 1).\n\n\n-----Output-----\n\nPrint the number of the sofa for which all the conditions are met. Sofas are numbered 1 through d as given in input. If there is no such sofa then print -1.\n\n\n-----Examples-----\nInput\n2\n3 2\n3 1 3 2\n1 2 2 2\n1 0 0 1\n\nOutput\n1\n\nInput\n3\n10 10\n1 2 1 1\n5 5 6 5\n6 4 5 4\n2 1 2 0\n\nOutput\n2\n\nInput\n2\n2 2\n2 1 1 1\n1 2 2 2\n1 0 0 0\n\nOutput\n-1\n\n\n\n-----Note-----\n\nLet's consider the second example. The first sofa has 0 to its left, 2 sofas to its right ((1, 1) is to the left of both (5, 5) and (5, 4)), 0 to its top and 2 to its bottom (both 2nd and 3rd sofas are below). The second sofa has cnt_{l} = 2, cnt_{r} = 1, cnt_{t} = 2 and cnt_{b} = 0. The third sofa has cnt_{l} = 2, cnt_{r} = 1, cnt_{t} = 1 and cnt_{b} = 1. \n\nSo the second one corresponds to the given conditions.\n\nIn the third example The first sofa has cnt_{l} = 1, cnt_{r} = 1, cnt_{t} = 0 and cnt_{b} = 1. The second sofa has cnt_{l} = 1, cnt_{r} = 1, cnt_{t} = 1 and cnt_{b} = 0. \n\nAnd there is no sofa with the set (1, 0, 0, 0) so the answer is -1."} +{"id": "00010", "text": "On the planet Mars a year lasts exactly n days (there are no leap years on Mars). But Martians have the same weeks as earthlings\u00a0\u2014 5 work days and then 2 days off. Your task is to determine the minimum possible and the maximum possible number of days off per year on Mars.\n\n\n-----Input-----\n\nThe first line of the input contains a positive integer n (1 \u2264 n \u2264 1 000 000)\u00a0\u2014 the number of days in a year on Mars.\n\n\n-----Output-----\n\nPrint two integers\u00a0\u2014 the minimum possible and the maximum possible number of days off per year on Mars.\n\n\n-----Examples-----\nInput\n14\n\nOutput\n4 4\n\nInput\n2\n\nOutput\n0 2\n\n\n\n-----Note-----\n\nIn the first sample there are 14 days in a year on Mars, and therefore independently of the day a year starts with there will be exactly 4 days off .\n\nIn the second sample there are only 2 days in a year on Mars, and they can both be either work days or days off."} +{"id": "00011", "text": "Little Joty has got a task to do. She has a line of n tiles indexed from 1 to n. She has to paint them in a strange pattern.\n\nAn unpainted tile should be painted Red if it's index is divisible by a and an unpainted tile should be painted Blue if it's index is divisible by b. So the tile with the number divisible by a and b can be either painted Red or Blue.\n\nAfter her painting is done, she will get p chocolates for each tile that is painted Red and q chocolates for each tile that is painted Blue.\n\nNote that she can paint tiles in any order she wants.\n\nGiven the required information, find the maximum\u00a0number of chocolates Joty can get.\n\n\n-----Input-----\n\nThe only line contains five integers n, a, b, p and q (1 \u2264 n, a, b, p, q \u2264 10^9).\n\n\n-----Output-----\n\nPrint the only integer s \u2014 the maximum number of chocolates Joty can get.\n\nNote that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.\n\n\n-----Examples-----\nInput\n5 2 3 12 15\n\nOutput\n39\n\nInput\n20 2 3 3 5\n\nOutput\n51"} +{"id": "00012", "text": "Vova has won $n$ trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.\n\nThe beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible \u2014 that means, to maximize the length of the longest such subsegment.\n\nHelp Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 10^5$) \u2014 the number of trophies.\n\nThe second line contains $n$ characters, each of them is either G or S. If the $i$-th character is G, then the $i$-th trophy is a golden one, otherwise it's a silver trophy. \n\n\n-----Output-----\n\nPrint the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.\n\n\n-----Examples-----\nInput\n10\nGGGSGGGSGG\n\nOutput\n7\n\nInput\n4\nGGGG\n\nOutput\n4\n\nInput\n3\nSSS\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example Vova has to swap trophies with indices $4$ and $10$. Thus he will obtain the sequence \"GGGGGGGSGS\", the length of the longest subsegment of golden trophies is $7$. \n\nIn the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is $4$. \n\nIn the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than $0$."} +{"id": "00013", "text": "Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.\n\nThe situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).\n\nHelp Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses. \n\nPolycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once. \n\n\n-----Input-----\n\nThe first line contains n and k (1 \u2264 k \u2264 n \u2264 10^5) \u2014 the number of online-courses and the number of main courses of Polycarp's specialty. \n\nThe second line contains k distinct integers from 1 to n \u2014 numbers of main online-courses of Polycarp's specialty. \n\nThen n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer t_{i} (0 \u2264 t_{i} \u2264 n - 1) \u2014 the number of courses on which the i-th depends. Then there follows the sequence of t_{i} distinct integers from 1 to n \u2014 numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself. \n\nIt is guaranteed that the sum of all values t_{i} doesn't exceed 10^5. \n\n\n-----Output-----\n\nPrint -1, if there is no the way to get a specialty. \n\nOtherwise, in the first line print the integer m \u2014 the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers \u2014 numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.\n\n\n-----Examples-----\nInput\n6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5\n\nOutput\n5\n1 2 3 4 5 \n\nInput\n9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2\n\nOutput\n6\n1 2 9 4 5 3 \n\nInput\n3 3\n1 2 3\n1 2\n1 3\n1 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course."} +{"id": "00014", "text": "Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).\n\nYou may perform the following operations until both a and s are empty:\n\n Take the first element of a, push it into s and remove it from a (if a is not empty); Take the top element from s, append it to the end of array b and remove it from s (if s is not empty). \n\nYou can perform these operations in arbitrary order.\n\nIf there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.\n\nFor example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:\n\n Remove 3 from a and push it into s; Remove 1 from a and push it into s; Remove 1 from s and append it to the end of b; Remove 2 from a and push it into s; Remove 2 from s and append it to the end of b; Remove 3 from s and append it to the end of b. \n\nAfter all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.\n\nYou are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k q_{i} = p_{i}, and q_{k} > p_{k}). You may not swap or change any of first k elements of the permutation.\n\nPrint the lexicographically maximal permutation p you can obtain.\n\nIf there exists no answer then output -1.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (2 \u2264 n \u2264 200000, 1 \u2264 k < n) \u2014 the size of a desired permutation, and the number of elements you are given, respectively.\n\nThe second line contains k integers p_1, p_2, ..., p_{k} (1 \u2264 p_{i} \u2264 n) \u2014 the first k elements of p. These integers are pairwise distinct.\n\n\n-----Output-----\n\nIf it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.\n\nOtherwise print -1.\n\n\n-----Examples-----\nInput\n5 3\n3 2 1\n\nOutput\n3 2 1 5 4 \nInput\n5 3\n2 3 1\n\nOutput\n-1\n\nInput\n5 1\n3\n\nOutput\n3 2 1 5 4 \nInput\n5 2\n3 4\n\nOutput\n-1"} +{"id": "00015", "text": "Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s_1 = a), and the difference between any two neighbouring elements is equal to c (s_{i} - s_{i} - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, there exists a positive integer i, such that s_{i} = b. Of course, you are the person he asks for a help.\n\n\n-----Input-----\n\nThe first line of the input contain three integers a, b and c ( - 10^9 \u2264 a, b, c \u2264 10^9)\u00a0\u2014 the first element of the sequence, Vasya's favorite number and the difference between any two neighbouring elements of the sequence, respectively.\n\n\n-----Output-----\n\nIf b appears in the sequence s print \"YES\" (without quotes), otherwise print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n1 7 3\n\nOutput\nYES\n\nInput\n10 10 0\n\nOutput\nYES\n\nInput\n1 -4 5\n\nOutput\nNO\n\nInput\n0 60 50\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample, the sequence starts from integers 1, 4, 7, so 7 is its element.\n\nIn the second sample, the favorite integer of Vasya is equal to the first element of the sequence.\n\nIn the third sample all elements of the sequence are greater than Vasya's favorite integer.\n\nIn the fourth sample, the sequence starts from 0, 50, 100, and all the following elements are greater than Vasya's favorite integer."} +{"id": "00016", "text": "A string is called bracket sequence if it does not contain any characters other than \"(\" and \")\". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters \"+\" and \"1\" into this sequence. For example, \"\", \"(())\" and \"()()\" are regular bracket sequences; \"))\" and \")((\" are bracket sequences (but not regular ones), and \"(a)\" and \"(1)+(1)\" are not bracket sequences at all.\n\nYou have a number of strings; each string is a bracket sequence of length $2$. So, overall you have $cnt_1$ strings \"((\", $cnt_2$ strings \"()\", $cnt_3$ strings \")(\" and $cnt_4$ strings \"))\". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length $2(cnt_1 + cnt_2 + cnt_3 + cnt_4)$. You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.\n\n\n-----Input-----\n\nThe input consists of four lines, $i$-th of them contains one integer $cnt_i$ ($0 \\le cnt_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint one integer: $1$ if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, $0$ otherwise.\n\n\n-----Examples-----\nInput\n3\n1\n4\n3\n\nOutput\n1\n\nInput\n0\n0\n0\n0\n\nOutput\n1\n\nInput\n1\n2\n3\n4\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example it is possible to construct a string \"(())()(()((()()()())))\", which is a regular bracket sequence.\n\nIn the second example it is possible to construct a string \"\", which is a regular bracket sequence."} +{"id": "00017", "text": "Arpa is researching the Mexican wave.\n\nThere are n spectators in the stadium, labeled from 1 to n. They start the Mexican wave at time 0. \n\n At time 1, the first spectator stands. At time 2, the second spectator stands. ... At time k, the k-th spectator stands. At time k + 1, the (k + 1)-th spectator stands and the first spectator sits. At time k + 2, the (k + 2)-th spectator stands and the second spectator sits. ... At time n, the n-th spectator stands and the (n - k)-th spectator sits. At time n + 1, the (n + 1 - k)-th spectator sits. ... At time n + k, the n-th spectator sits. \n\nArpa wants to know how many spectators are standing at time t.\n\n\n-----Input-----\n\nThe first line contains three integers n, k, t (1 \u2264 n \u2264 10^9, 1 \u2264 k \u2264 n, 1 \u2264 t < n + k).\n\n\n-----Output-----\n\nPrint single integer: how many spectators are standing at time t.\n\n\n-----Examples-----\nInput\n10 5 3\n\nOutput\n3\n\nInput\n10 5 7\n\nOutput\n5\n\nInput\n10 5 12\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the following a sitting spectator is represented as -, a standing spectator is represented as ^.\n\n At t = 0\u2002 ---------- $\\Rightarrow$ number of standing spectators = 0. At t = 1\u2002 ^--------- $\\Rightarrow$ number of standing spectators = 1. At t = 2\u2002 ^^-------- $\\Rightarrow$ number of standing spectators = 2. At t = 3\u2002 ^^^------- $\\Rightarrow$ number of standing spectators = 3. At t = 4\u2002 ^^^^------ $\\Rightarrow$ number of standing spectators = 4. At t = 5\u2002 ^^^^^----- $\\Rightarrow$ number of standing spectators = 5. At t = 6\u2002 -^^^^^---- $\\Rightarrow$ number of standing spectators = 5. At t = 7\u2002 --^^^^^--- $\\Rightarrow$ number of standing spectators = 5. At t = 8\u2002 ---^^^^^-- $\\Rightarrow$ number of standing spectators = 5. At t = 9\u2002 ----^^^^^- $\\Rightarrow$ number of standing spectators = 5. At t = 10 -----^^^^^ $\\Rightarrow$ number of standing spectators = 5. At t = 11 ------^^^^ $\\Rightarrow$ number of standing spectators = 4. At t = 12 -------^^^ $\\Rightarrow$ number of standing spectators = 3. At t = 13 --------^^ $\\Rightarrow$ number of standing spectators = 2. At t = 14 ---------^ $\\Rightarrow$ number of standing spectators = 1. At t = 15 ---------- $\\Rightarrow$ number of standing spectators = 0."} +{"id": "00018", "text": "Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. \n\nPetya wants to get strings s and t empty and string u lexicographically minimal.\n\nYou should write a program that will help Petya win the game.\n\n\n-----Input-----\n\nFirst line contains non-empty string s (1 \u2264 |s| \u2264 10^5), consisting of lowercase English letters.\n\n\n-----Output-----\n\nPrint resulting string u.\n\n\n-----Examples-----\nInput\ncab\n\nOutput\nabc\n\nInput\nacdb\n\nOutput\nabdc"} +{"id": "00019", "text": "Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level.\n\nAll levels in this game have two stats to them: the number of plays and the number of clears. So when a player attempts the level, the number of plays increases by $1$. If he manages to finish the level successfully then the number of clears increases by $1$ as well. Note that both of the statistics update at the same time (so if the player finishes the level successfully then the number of plays will increase at the same time as the number of clears).\n\nPolycarp is very excited about his level, so he keeps peeking at the stats to know how hard his level turns out to be.\n\nSo he peeked at the stats $n$ times and wrote down $n$ pairs of integers \u2014 $(p_1, c_1), (p_2, c_2), \\dots, (p_n, c_n)$, where $p_i$ is the number of plays at the $i$-th moment of time and $c_i$ is the number of clears at the same moment of time. The stats are given in chronological order (i.e. the order of given pairs is exactly the same as Polycarp has written down).\n\nBetween two consecutive moments of time Polycarp peeked at the stats many players (but possibly zero) could attempt the level.\n\nFinally, Polycarp wonders if he hasn't messed up any records and all the pairs are correct. If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then he considers his records correct.\n\nHelp him to check the correctness of his records.\n\nFor your convenience you have to answer multiple independent test cases.\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ $(1 \\le T \\le 500)$ \u2014 the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 100$) \u2014 the number of moments of time Polycarp peeked at the stats.\n\nEach of the next $n$ lines contains two integers $p_i$ and $c_i$ ($0 \\le p_i, c_i \\le 1000$) \u2014 the number of plays and the number of clears of the level at the $i$-th moment of time.\n\nNote that the stats are given in chronological order.\n\n\n-----Output-----\n\nFor each test case print a single line.\n\nIf there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then print \"YES\".\n\nOtherwise, print \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n6\n3\n0 0\n1 1\n1 2\n2\n1 0\n1000 3\n4\n10 1\n15 2\n10 2\n15 2\n1\n765 432\n2\n4 4\n4 3\n5\n0 0\n1 0\n1 0\n1 0\n1 0\n\nOutput\nNO\nYES\nNO\nYES\nNO\nYES\n\n\n\n-----Note-----\n\nIn the first test case at the third moment of time the number of clears increased but the number of plays did not, that couldn't have happened.\n\nThe second test case is a nice example of a Super Expert level.\n\nIn the third test case the number of plays decreased, which is impossible.\n\nThe fourth test case is probably an auto level with a single jump over the spike.\n\nIn the fifth test case the number of clears decreased, which is also impossible.\n\nNobody wanted to play the sixth test case; Polycarp's mom attempted it to make him feel better, however, she couldn't clear it."} +{"id": "00020", "text": "Karen is getting ready for a new school day!\n\n [Image] \n\nIt is currently hh:mm, given in a 24-hour format. As you know, Karen loves palindromes, and she believes that it is good luck to wake up when the time is a palindrome.\n\nWhat is the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome?\n\nRemember that a palindrome is a string that reads the same forwards and backwards. For instance, 05:39 is not a palindrome, because 05:39 backwards is 93:50. On the other hand, 05:50 is a palindrome, because 05:50 backwards is 05:50.\n\n\n-----Input-----\n\nThe first and only line of input contains a single string in the format hh:mm (00 \u2264 hh \u2264 23, 00 \u2264 mm \u2264 59).\n\n\n-----Output-----\n\nOutput a single integer on a line by itself, the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome.\n\n\n-----Examples-----\nInput\n05:39\n\nOutput\n11\n\nInput\n13:31\n\nOutput\n0\n\nInput\n23:59\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first test case, the minimum number of minutes Karen should sleep for is 11. She can wake up at 05:50, when the time is a palindrome.\n\nIn the second test case, Karen can wake up immediately, as the current time, 13:31, is already a palindrome.\n\nIn the third test case, the minimum number of minutes Karen should sleep for is 1 minute. She can wake up at 00:00, when the time is a palindrome."} +{"id": "00021", "text": "Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n.\n\nNicholas want the minimum element (integer 1) and the maximum element (integer n) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (2 \u2264 n \u2264 100)\u00a0\u2014 the size of the permutation.\n\nThe second line of the input contains n distinct integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n), where a_{i} is equal to the element at the i-th position.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap.\n\n\n-----Examples-----\nInput\n5\n4 5 1 3 2\n\nOutput\n3\n\nInput\n7\n1 6 5 3 4 7 2\n\nOutput\n6\n\nInput\n6\n6 5 4 3 2 1\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first sample, one may obtain the optimal answer by swapping elements 1 and 2.\n\nIn the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2.\n\nIn the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap 5 and 2."} +{"id": "00022", "text": "Let's call a string \"s-palindrome\" if it is symmetric about the middle of the string. For example, the string \"oHo\" is \"s-palindrome\", but the string \"aa\" is not. The string \"aa\" is not \"s-palindrome\", because the second half of it is not a mirror reflection of the first half.\n\n [Image] English alphabet \n\nYou are given a string s. Check if the string is \"s-palindrome\".\n\n\n-----Input-----\n\nThe only line contains the string s (1 \u2264 |s| \u2264 1000) which consists of only English letters.\n\n\n-----Output-----\n\nPrint \"TAK\" if the string s is \"s-palindrome\" and \"NIE\" otherwise.\n\n\n-----Examples-----\nInput\noXoxoXo\n\nOutput\nTAK\n\nInput\nbod\n\nOutput\nTAK\n\nInput\nER\n\nOutput\nNIE"} +{"id": "00023", "text": "You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.\n\nIt is allowed to leave a as it is.\n\n\n-----Input-----\n\nThe first line contains integer a (1 \u2264 a \u2264 10^18). The second line contains integer b (1 \u2264 b \u2264 10^18). Numbers don't have leading zeroes. It is guaranteed that answer exists.\n\n\n-----Output-----\n\nPrint the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.\n\nThe number in the output should have exactly the same length as number a. It should be a permutation of digits of a.\n\n\n-----Examples-----\nInput\n123\n222\n\nOutput\n213\n\nInput\n3921\n10000\n\nOutput\n9321\n\nInput\n4940\n5000\n\nOutput\n4940"} +{"id": "00024", "text": "Alice and Bob play 5-in-a-row game. They have a playing field of size 10 \u00d7 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.\n\nIn current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately.\n\nAlice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.\n\n\n-----Input-----\n\nYou are given matrix 10 \u00d7 10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.\n\nIt is guaranteed that in the current arrangement nobody has still won.\n\n\n-----Output-----\n\nPrint 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.\n\n\n-----Examples-----\nInput\nXX.XX.....\n.....OOOO.\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n\nOutput\nYES\n\nInput\nXXOXX.....\nOO.O......\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n\nOutput\nNO"} +{"id": "00025", "text": "You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the bottom right corner) and is lexicographically maximal.\n\nOne matrix is lexicographically greater than the other if the first different number in the first different row from the top in the first matrix is greater than the corresponding number in the second one.\n\nIf there exists no such matrix then output -1.\n\n\n-----Input-----\n\nThe first line consists of two numbers n and k (1 \u2264 n \u2264 100, 0 \u2264 k \u2264 10^6).\n\n\n-----Output-----\n\nIf the answer exists then output resulting matrix. Otherwise output -1.\n\n\n-----Examples-----\nInput\n2 1\n\nOutput\n1 0 \n0 0 \n\nInput\n3 2\n\nOutput\n1 0 0 \n0 1 0 \n0 0 0 \n\nInput\n2 5\n\nOutput\n-1"} +{"id": "00026", "text": "Wet Shark asked Rat Kwesh to generate three positive real numbers x, y and z, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point.\n\nWet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportunity to earn a lot of cheese. He will hand the three numbers x, y and z to Rat Kwesh, and Rat Kwesh will pick one of the these twelve options: a_1 = x^{y}^{z}; a_2 = x^{z}^{y}; a_3 = (x^{y})^{z}; a_4 = (x^{z})^{y}; a_5 = y^{x}^{z}; a_6 = y^{z}^{x}; a_7 = (y^{x})^{z}; a_8 = (y^{z})^{x}; a_9 = z^{x}^{y}; a_10 = z^{y}^{x}; a_11 = (z^{x})^{y}; a_12 = (z^{y})^{x}. \n\nLet m be the maximum of all the a_{i}, and c be the smallest index (from 1 to 12) such that a_{c} = m. Rat's goal is to find that c, and he asks you to help him. Rat Kwesh wants to see how much cheese he gets, so he you will have to print the expression corresponding to that a_{c}.\n\n \n\n\n-----Input-----\n\nThe only line of the input contains three space-separated real numbers x, y and z (0.1 \u2264 x, y, z \u2264 200.0). Each of x, y and z is given with exactly one digit after the decimal point.\n\n\n-----Output-----\n\nFind the maximum value of expression among x^{y}^{z}, x^{z}^{y}, (x^{y})^{z}, (x^{z})^{y}, y^{x}^{z}, y^{z}^{x}, (y^{x})^{z}, (y^{z})^{x}, z^{x}^{y}, z^{y}^{x}, (z^{x})^{y}, (z^{y})^{x} and print the corresponding expression. If there are many maximums, print the one that comes first in the list. \n\nx^{y}^{z} should be outputted as x^y^z (without brackets), and (x^{y})^{z} should be outputted as (x^y)^z (quotes for clarity). \n\n\n-----Examples-----\nInput\n1.1 3.4 2.5\n\nOutput\nz^y^x\n\nInput\n2.0 2.0 2.0\n\nOutput\nx^y^z\n\nInput\n1.9 1.8 1.7\n\nOutput\n(x^y)^z"} +{"id": "00027", "text": "You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard.\n\nInitially, you have an empty string. Until you type the whole string, you may perform the following operation: add a character to the end of the string. \n\nBesides, at most once you may perform one additional operation: copy the string and append it to itself.\n\nFor example, if you have to type string abcabca, you can type it in 7 operations if you type all the characters one by one. However, you can type it in 5 operations if you type the string abc first and then copy it and type the last character.\n\nIf you have to type string aaaaaaaaa, the best option is to type 4 characters one by one, then copy the string, and then type the remaining character.\n\nPrint the minimum number of operations you need to type the given string.\n\n\n-----Input-----\n\nThe first line of the input containing only one integer number n (1 \u2264 n \u2264 100)\u00a0\u2014 the length of the string you have to type. The second line containing the string s consisting of n lowercase Latin letters.\n\n\n-----Output-----\n\nPrint one integer number\u00a0\u2014 the minimum number of operations you need to type the given string.\n\n\n-----Examples-----\nInput\n7\nabcabca\n\nOutput\n5\n\nInput\n8\nabcdefgh\n\nOutput\n8\n\n\n\n-----Note-----\n\nThe first test described in the problem statement.\n\nIn the second test you can only type all the characters one by one."} +{"id": "00028", "text": "The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website.\n\nUnfortunately, the archive with Olympiad's data is a mess. For example, the files with tests are named arbitrary without any logic.\n\nVladimir wants to rename the files with tests so that their names are distinct integers starting from 1 without any gaps, namely, \"1\", \"2\", ..., \"n', where n is the total number of tests.\n\nSome of the files contain tests from statements (examples), while others contain regular tests. It is possible that there are no examples, and it is possible that all tests are examples. Vladimir wants to rename the files so that the examples are the first several tests, all all the next files contain regular tests only.\n\nThe only operation Vladimir can perform is the \"move\" command. Vladimir wants to write a script file, each of the lines in which is \"move file_1 file_2\", that means that the file \"file_1\" is to be renamed to \"file_2\". If there is a file \"file_2\" at the moment of this line being run, then this file is to be rewritten. After the line \"move file_1 file_2\" the file \"file_1\" doesn't exist, but there is a file \"file_2\" with content equal to the content of \"file_1\" before the \"move\" command.\n\nHelp Vladimir to write the script file with the minimum possible number of lines so that after this script is run: all examples are the first several tests having filenames \"1\", \"2\", ..., \"e\", where e is the total number of examples; all other files contain regular tests with filenames \"e + 1\", \"e + 2\", ..., \"n\", where n is the total number of all tests. \n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 10^5) \u2014 the number of files with tests.\n\nn lines follow, each describing a file with test. Each line has a form of \"name_i type_i\", where \"name_i\" is the filename, and \"type_i\" equals \"1\", if the i-th file contains an example test, and \"0\" if it contains a regular test. Filenames of each file are strings of digits and small English letters with length from 1 to 6 characters. The filenames are guaranteed to be distinct.\n\n\n-----Output-----\n\nIn the first line print the minimum number of lines in Vladimir's script file.\n\nAfter that print the script file, each line should be \"move file_1 file_2\", where \"file_1\" is an existing at the moment of this line being run filename, and \"file_2\" \u2014 is a string of digits and small English letters with length from 1 to 6.\n\n\n-----Examples-----\nInput\n5\n01 0\n2 1\n2extra 0\n3 1\n99 0\n\nOutput\n4\nmove 3 1\nmove 01 5\nmove 2extra 4\nmove 99 3\n\nInput\n2\n1 0\n2 1\n\nOutput\n3\nmove 1 3\nmove 2 1\nmove 3 2\nInput\n5\n1 0\n11 1\n111 0\n1111 1\n11111 0\n\nOutput\n5\nmove 1 5\nmove 11 1\nmove 1111 2\nmove 111 4\nmove 11111 3"} +{"id": "00029", "text": "Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.\n\nThe ticket is considered lucky if the sum of first three digits equals to the sum of last three digits.\n\n\n-----Input-----\n\nYou are given a string consisting of 6 characters (all characters are digits from 0 to 9) \u2014 this string denotes Luba's ticket. The ticket can start with the digit 0.\n\n\n-----Output-----\n\nPrint one number \u2014 the minimum possible number of digits Luba needs to replace to make the ticket lucky.\n\n\n-----Examples-----\nInput\n000000\n\nOutput\n0\n\nInput\n123456\n\nOutput\n2\n\nInput\n111000\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example the ticket is already lucky, so the answer is 0.\n\nIn the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.\n\nIn the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required."} +{"id": "00030", "text": "The campus has $m$ rooms numbered from $0$ to $m - 1$. Also the $x$-mouse lives in the campus. The $x$-mouse is not just a mouse: each second $x$-mouse moves from room $i$ to the room $i \\cdot x \\mod{m}$ (in fact, it teleports from one room to another since it doesn't visit any intermediate room). Starting position of the $x$-mouse is unknown.\n\nYou are responsible to catch the $x$-mouse in the campus, so you are guessing about minimum possible number of traps (one trap in one room) you need to place. You are sure that if the $x$-mouse enters a trapped room, it immediately gets caught.\n\nAnd the only observation you made is $\\text{GCD} (x, m) = 1$.\n\n\n-----Input-----\n\nThe only line contains two integers $m$ and $x$ ($2 \\le m \\le 10^{14}$, $1 \\le x < m$, $\\text{GCD} (x, m) = 1$) \u2014 the number of rooms and the parameter of $x$-mouse. \n\n\n-----Output-----\n\nPrint the only integer \u2014 minimum number of traps you need to install to catch the $x$-mouse.\n\n\n-----Examples-----\nInput\n4 3\n\nOutput\n3\n\nInput\n5 2\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example you can, for example, put traps in rooms $0$, $2$, $3$. If the $x$-mouse starts in one of this rooms it will be caught immediately. If $x$-mouse starts in the $1$-st rooms then it will move to the room $3$, where it will be caught.\n\nIn the second example you can put one trap in room $0$ and one trap in any other room since $x$-mouse will visit all rooms $1..m-1$ if it will start in any of these rooms."} +{"id": "00031", "text": "ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that given a random set of 23 people, there is around 50% chance that some two of them share the same birthday. ZS the Coder finds this very interesting, and decides to test this with the inhabitants of Udayland.\n\nIn Udayland, there are 2^{n} days in a year. ZS the Coder wants to interview k people from Udayland, each of them has birthday in one of 2^{n} days (each day with equal probability). He is interested in the probability of at least two of them have the birthday at the same day. \n\nZS the Coder knows that the answer can be written as an irreducible fraction $\\frac{A}{B}$. He wants to find the values of A and B (he does not like to deal with floating point numbers). Can you help him?\n\n\n-----Input-----\n\nThe first and only line of the input contains two integers n and k (1 \u2264 n \u2264 10^18, 2 \u2264 k \u2264 10^18), meaning that there are 2^{n} days in a year and that ZS the Coder wants to interview exactly k people.\n\n\n-----Output-----\n\nIf the probability of at least two k people having the same birthday in 2^{n} days long year equals $\\frac{A}{B}$ (A \u2265 0, B \u2265 1, $\\operatorname{gcd}(A, B) = 1$), print the A and B in a single line.\n\nSince these numbers may be too large, print them modulo 10^6 + 3. Note that A and B must be coprime before their remainders modulo 10^6 + 3 are taken.\n\n\n-----Examples-----\nInput\n3 2\n\nOutput\n1 8\nInput\n1 3\n\nOutput\n1 1\nInput\n4 3\n\nOutput\n23 128\n\n\n-----Note-----\n\nIn the first sample case, there are 2^3 = 8 days in Udayland. The probability that 2 people have the same birthday among 2 people is clearly $\\frac{1}{8}$, so A = 1, B = 8.\n\nIn the second sample case, there are only 2^1 = 2 days in Udayland, but there are 3 people, so it is guaranteed that two of them have the same birthday. Thus, the probability is 1 and A = B = 1."} +{"id": "00032", "text": "In this problem we assume the Earth to be a completely round ball and its surface a perfect sphere. The length of the equator and any meridian is considered to be exactly 40 000 kilometers. Thus, travelling from North Pole to South Pole or vice versa takes exactly 20 000 kilometers.\n\nLimak, a polar bear, lives on the North Pole. Close to the New Year, he helps somebody with delivering packages all around the world. Instead of coordinates of places to visit, Limak got a description how he should move, assuming that he starts from the North Pole. The description consists of n parts. In the i-th part of his journey, Limak should move t_{i} kilometers in the direction represented by a string dir_{i} that is one of: \"North\", \"South\", \"West\", \"East\".\n\nLimak isn\u2019t sure whether the description is valid. You must help him to check the following conditions: If at any moment of time (before any of the instructions or while performing one of them) Limak is on the North Pole, he can move only to the South. If at any moment of time (before any of the instructions or while performing one of them) Limak is on the South Pole, he can move only to the North. The journey must end on the North Pole. \n\nCheck if the above conditions are satisfied and print \"YES\" or \"NO\" on a single line.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 50).\n\nThe i-th of next n lines contains an integer t_{i} and a string dir_{i} (1 \u2264 t_{i} \u2264 10^6, $\\operatorname{dir}_{i} \\in \\{\\text{North, South, West, East} \\}$)\u00a0\u2014 the length and the direction of the i-th part of the journey, according to the description Limak got.\n\n\n-----Output-----\n\nPrint \"YES\" if the description satisfies the three conditions, otherwise print \"NO\", both without the quotes.\n\n\n-----Examples-----\nInput\n5\n7500 South\n10000 East\n3500 North\n4444 West\n4000 North\n\nOutput\nYES\n\nInput\n2\n15000 South\n4000 East\n\nOutput\nNO\n\nInput\n5\n20000 South\n1000 North\n1000000 West\n9000 North\n10000 North\n\nOutput\nYES\n\nInput\n3\n20000 South\n10 East\n20000 North\n\nOutput\nNO\n\nInput\n2\n1000 North\n1000 South\n\nOutput\nNO\n\nInput\n4\n50 South\n50 North\n15000 South\n15000 North\n\nOutput\nYES\n\n\n\n-----Note-----\n\nDrawings below show how Limak's journey would look like in first two samples. In the second sample the answer is \"NO\" because he doesn't end on the North Pole. [Image]"} +{"id": "00033", "text": "You are given two arithmetic progressions: a_1k + b_1 and a_2l + b_2. Find the number of integers x such that L \u2264 x \u2264 R and x = a_1k' + b_1 = a_2l' + b_2, for some integers k', l' \u2265 0.\n\n\n-----Input-----\n\nThe only line contains six integers a_1, b_1, a_2, b_2, L, R (0 < a_1, a_2 \u2264 2\u00b710^9, - 2\u00b710^9 \u2264 b_1, b_2, L, R \u2264 2\u00b710^9, L \u2264 R).\n\n\n-----Output-----\n\nPrint the desired number of integers x.\n\n\n-----Examples-----\nInput\n2 0 3 3 5 21\n\nOutput\n3\n\nInput\n2 4 3 0 6 17\n\nOutput\n2"} +{"id": "00034", "text": "It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one \u2014 into b pieces.\n\nIvan knows that there will be n people at the celebration (including himself), so Ivan has set n plates for the cakes. Now he is thinking about how to distribute the cakes between the plates. Ivan wants to do it in such a way that all following conditions are met: Each piece of each cake is put on some plate; Each plate contains at least one piece of cake; No plate contains pieces of both cakes. \n\nTo make his guests happy, Ivan wants to distribute the cakes in such a way that the minimum number of pieces on the plate is maximized. Formally, Ivan wants to know the maximum possible number x such that he can distribute the cakes according to the aforementioned conditions, and each plate will contain at least x pieces of cake.\n\nHelp Ivan to calculate this number x!\n\n\n-----Input-----\n\nThe first line contains three integers n, a and b (1 \u2264 a, b \u2264 100, 2 \u2264 n \u2264 a + b) \u2014 the number of plates, the number of pieces of the first cake, and the number of pieces of the second cake, respectively.\n\n\n-----Output-----\n\nPrint the maximum possible number x such that Ivan can distribute the cake in such a way that each plate will contain at least x pieces of cake.\n\n\n-----Examples-----\nInput\n5 2 3\n\nOutput\n1\n\nInput\n4 7 10\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example there is only one way to distribute cakes to plates, all of them will have 1 cake on it.\n\nIn the second example you can have two plates with 3 and 4 pieces of the first cake and two plates both with 5 pieces of the second cake. Minimal number of pieces is 3."} +{"id": "00035", "text": "The flag of Berland is such rectangular field n \u00d7 m that satisfies following conditions:\n\n Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. \n\nYou are given a field n \u00d7 m, consisting of characters 'R', 'G' and 'B'. Output \"YES\" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print \"NO\" (without quotes).\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and m (1 \u2264 n, m \u2264 100) \u2014 the sizes of the field.\n\nEach of the following n lines consisting of m characters 'R', 'G' and 'B' \u2014 the description of the field.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n6 5\nRRRRR\nRRRRR\nBBBBB\nBBBBB\nGGGGG\nGGGGG\n\nOutput\nYES\n\nInput\n4 3\nBRG\nBRG\nBRG\nBRG\n\nOutput\nYES\n\nInput\n6 7\nRRRGGGG\nRRRGGGG\nRRRGGGG\nRRRBBBB\nRRRBBBB\nRRRBBBB\n\nOutput\nNO\n\nInput\n4 4\nRRRR\nRRRR\nBBBB\nGGGG\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe field in the third example doesn't have three parralel stripes.\n\nRows of the field in the fourth example are parralel to each other and to borders. But they have different heights \u2014 2, 1 and 1."} +{"id": "00036", "text": "Ayrat is looking for the perfect code. He decided to start his search from an infinite field tiled by hexagons. For convenience the coordinate system is introduced, take a look at the picture to see how the coordinates of hexagon are defined: \n\n[Image] [Image] Ayrat is searching through the field. He started at point (0, 0) and is moving along the spiral (see second picture). Sometimes he forgets where he is now. Help Ayrat determine his location after n moves.\n\n\n-----Input-----\n\nThe only line of the input contains integer n (0 \u2264 n \u2264 10^18)\u00a0\u2014 the number of Ayrat's moves.\n\n\n-----Output-----\n\nPrint two integers x and y\u00a0\u2014 current coordinates of Ayrat coordinates.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n-2 0\n\nInput\n7\n\nOutput\n3 2"} +{"id": "00037", "text": "Dante is engaged in a fight with \"The Savior\". Before he can fight it with his sword, he needs to break its shields. He has two guns, Ebony and Ivory, each of them is able to perform any non-negative number of shots.\n\nFor every bullet that hits the shield, Ebony deals a units of damage while Ivory deals b units of damage. In order to break the shield Dante has to deal exactly c units of damage. Find out if this is possible.\n\n\n-----Input-----\n\nThe first line of the input contains three integers a, b, c (1 \u2264 a, b \u2264 100, 1 \u2264 c \u2264 10 000)\u00a0\u2014 the number of units of damage dealt by Ebony gun and Ivory gun, and the total number of damage required to break the shield, respectively.\n\n\n-----Output-----\n\nPrint \"Yes\" (without quotes) if Dante can deal exactly c damage to the shield and \"No\" (without quotes) otherwise.\n\n\n-----Examples-----\nInput\n4 6 15\n\nOutput\nNo\n\nInput\n3 2 7\n\nOutput\nYes\n\nInput\n6 11 6\n\nOutput\nYes\n\n\n\n-----Note-----\n\nIn the second sample, Dante can fire 1 bullet from Ebony and 2 from Ivory to deal exactly 1\u00b73 + 2\u00b72 = 7 damage. In the third sample, Dante can fire 1 bullet from ebony and no bullets from ivory to do 1\u00b76 + 0\u00b711 = 6 damage."} +{"id": "00038", "text": "Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the following situation:\n\nThe track is the circle with length L, in distinct points of which there are n barriers. Athlete always run the track in counterclockwise direction if you look on him from above. All barriers are located at integer distance from each other along the track. \n\nHer friends the parrot Kefa and the leopard Sasha participated in competitions and each of them ran one lap. Each of the friends started from some integral point on the track. Both friends wrote the distance from their start along the track to each of the n barriers. Thus, each of them wrote n integers in the ascending order, each of them was between 0 and L - 1, inclusively. [Image] Consider an example. Let L = 8, blue points are barriers, and green points are Kefa's start (A) and Sasha's start (B). Then Kefa writes down the sequence [2, 4, 6], and Sasha writes down [1, 5, 7]. \n\nThere are several tracks in the country, all of them have same length and same number of barriers, but the positions of the barriers can differ among different tracks. Now Dasha is interested if it is possible that Kefa and Sasha ran the same track or they participated on different tracks. \n\nWrite the program which will check that Kefa's and Sasha's tracks coincide (it means that one can be obtained from the other by changing the start position). Note that they always run the track in one direction \u2014 counterclockwise, if you look on a track from above. \n\n\n-----Input-----\n\nThe first line contains two integers n and L (1 \u2264 n \u2264 50, n \u2264 L \u2264 100) \u2014 the number of barriers on a track and its length. \n\nThe second line contains n distinct integers in the ascending order \u2014 the distance from Kefa's start to each barrier in the order of its appearance. All integers are in the range from 0 to L - 1 inclusively.\n\nThe second line contains n distinct integers in the ascending order \u2014 the distance from Sasha's start to each barrier in the order of its overcoming. All integers are in the range from 0 to L - 1 inclusively.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes), if Kefa and Sasha ran the coinciding tracks (it means that the position of all barriers coincides, if they start running from the same points on the track). Otherwise print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n3 8\n2 4 6\n1 5 7\n\nOutput\nYES\n\nInput\n4 9\n2 3 5 8\n0 1 3 6\n\nOutput\nYES\n\nInput\n2 4\n1 3\n1 2\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe first test is analyzed in the statement."} +{"id": "00039", "text": "A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings \"kek\", \"abacaba\", \"r\" and \"papicipap\" are palindromes, while the strings \"abb\" and \"iq\" are not.\n\nA substring $s[l \\ldots r]$ ($1 \\leq l \\leq r \\leq |s|$) of a string $s = s_{1}s_{2} \\ldots s_{|s|}$ is the string $s_{l}s_{l + 1} \\ldots s_{r}$.\n\nAnna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word $s$ is changed into its longest substring that is not a palindrome. If all the substrings of $s$ are palindromes, she skips the word at all.\n\nSome time ago Ann read the word $s$. What is the word she changed it into?\n\n\n-----Input-----\n\nThe first line contains a non-empty string $s$ with length at most $50$ characters, containing lowercase English letters only.\n\n\n-----Output-----\n\nIf there is such a substring in $s$ that is not a palindrome, print the maximum length of such a substring. Otherwise print $0$.\n\nNote that there can be multiple longest substrings that are not palindromes, but their length is unique.\n\n\n-----Examples-----\nInput\nmew\n\nOutput\n3\n\nInput\nwuffuw\n\nOutput\n5\n\nInput\nqqqqqqqq\n\nOutput\n0\n\n\n\n-----Note-----\n\n\"mew\" is not a palindrome, so the longest substring of it that is not a palindrome, is the string \"mew\" itself. Thus, the answer for the first example is $3$.\n\nThe string \"uffuw\" is one of the longest non-palindrome substrings (of length $5$) of the string \"wuffuw\", so the answer for the second example is $5$.\n\nAll substrings of the string \"qqqqqqqq\" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is $0$."} +{"id": "00040", "text": "Is it rated?\n\nHere it is. The Ultimate Question of Competitive Programming, Codeforces, and Everything. And you are here to answer it.\n\nAnother Codeforces round has been conducted. No two participants have the same number of points. For each participant, from the top to the bottom of the standings, their rating before and after the round is known.\n\nIt's known that if at least one participant's rating has changed, then the round was rated for sure.\n\nIt's also known that if the round was rated and a participant with lower rating took a better place in the standings than a participant with higher rating, then at least one round participant's rating has changed.\n\nIn this problem, you should not make any other assumptions about the rating system.\n\nDetermine if the current round is rated, unrated, or it's impossible to determine whether it is rated of not.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 1000)\u00a0\u2014 the number of round participants.\n\nEach of the next n lines contains two integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 4126)\u00a0\u2014 the rating of the i-th participant before and after the round, respectively. The participants are listed in order from the top to the bottom of the standings.\n\n\n-----Output-----\n\nIf the round is rated for sure, print \"rated\". If the round is unrated for sure, print \"unrated\". If it's impossible to determine whether the round is rated or not, print \"maybe\".\n\n\n-----Examples-----\nInput\n6\n3060 3060\n2194 2194\n2876 2903\n2624 2624\n3007 2991\n2884 2884\n\nOutput\nrated\n\nInput\n4\n1500 1500\n1300 1300\n1200 1200\n1400 1400\n\nOutput\nunrated\n\nInput\n5\n3123 3123\n2777 2777\n2246 2246\n2246 2246\n1699 1699\n\nOutput\nmaybe\n\n\n\n-----Note-----\n\nIn the first example, the ratings of the participants in the third and fifth places have changed, therefore, the round was rated.\n\nIn the second example, no one's rating has changed, but the participant in the second place has lower rating than the participant in the fourth place. Therefore, if the round was rated, someone's rating would've changed for sure.\n\nIn the third example, no one's rating has changed, and the participants took places in non-increasing order of their rating. Therefore, it's impossible to determine whether the round is rated or not."} +{"id": "00041", "text": "You are given the array of integer numbers a_0, a_1, ..., a_{n} - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 2\u00b710^5) \u2014 length of the array a. The second line contains integer elements of the array separated by single spaces ( - 10^9 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint the sequence d_0, d_1, ..., d_{n} - 1, where d_{i} is the difference of indices between i and nearest j such that a_{j} = 0. It is possible that i = j.\n\n\n-----Examples-----\nInput\n9\n2 1 0 3 0 0 3 2 4\n\nOutput\n2 1 0 1 0 0 1 2 3 \nInput\n5\n0 1 2 3 4\n\nOutput\n0 1 2 3 4 \nInput\n7\n5 6 0 1 -2 3 4\n\nOutput\n2 1 0 1 2 3 4"} +{"id": "00042", "text": "You are given a binary string $s$.\n\nFind the number of distinct cyclical binary strings of length $n$ which contain $s$ as a substring.\n\nThe cyclical string $t$ contains $s$ as a substring if there is some cyclical shift of string $t$, such that $s$ is a substring of this cyclical shift of $t$.\n\nFor example, the cyclical string \"000111\" contains substrings \"001\", \"01110\" and \"10\", but doesn't contain \"0110\" and \"10110\".\n\nTwo cyclical strings are called different if they differ from each other as strings. For example, two different strings, which differ from each other by a cyclical shift, are still considered different cyclical strings.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 40$)\u00a0\u2014 the length of the target string $t$.\n\nThe next line contains the string $s$ ($1 \\le |s| \\le n$)\u00a0\u2014 the string which must be a substring of cyclical string $t$. String $s$ contains only characters '0' and '1'.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the number of distinct cyclical binary strings $t$, which contain $s$ as a substring.\n\n\n-----Examples-----\nInput\n2\n0\n\nOutput\n3\nInput\n4\n1010\n\nOutput\n2\nInput\n20\n10101010101010\n\nOutput\n962\n\n\n-----Note-----\n\nIn the first example, there are three cyclical strings, which contain \"0\"\u00a0\u2014 \"00\", \"01\" and \"10\".\n\nIn the second example, there are only two such strings\u00a0\u2014 \"1010\", \"0101\"."} +{"id": "00043", "text": "You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.\n\nNon-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and \u03c0. For example, opposite directions vectors have angle equals to \u03c0.\n\n\n-----Input-----\n\nFirst line of the input contains a single integer n (2 \u2264 n \u2264 100 000)\u00a0\u2014 the number of vectors.\n\nThe i-th of the following n lines contains two integers x_{i} and y_{i} (|x|, |y| \u2264 10 000, x^2 + y^2 > 0)\u00a0\u2014 the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).\n\n\n-----Output-----\n\nPrint two integer numbers a and b (a \u2260 b)\u00a0\u2014 a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.\n\n\n-----Examples-----\nInput\n4\n-1 0\n0 -1\n1 0\n1 1\n\nOutput\n3 4\n\nInput\n6\n-1 0\n0 -1\n1 0\n1 1\n-4 -5\n-4 -6\n\nOutput\n6 5"} +{"id": "00044", "text": "Vasiliy has a car and he wants to get from home to the post office. The distance which he needs to pass equals to d kilometers.\n\nVasiliy's car is not new \u2014 it breaks after driven every k kilometers and Vasiliy needs t seconds to repair it. After repairing his car Vasiliy can drive again (but after k kilometers it will break again, and so on). In the beginning of the trip the car is just from repair station.\n\nTo drive one kilometer on car Vasiliy spends a seconds, to walk one kilometer on foot he needs b seconds (a < b).\n\nYour task is to find minimal time after which Vasiliy will be able to reach the post office. Consider that in every moment of time Vasiliy can left his car and start to go on foot.\n\n\n-----Input-----\n\nThe first line contains 5 positive integers d, k, a, b, t (1 \u2264 d \u2264 10^12; 1 \u2264 k, a, b, t \u2264 10^6; a < b), where: d \u2014 the distance from home to the post office; k \u2014 the distance, which car is able to drive before breaking; a \u2014 the time, which Vasiliy spends to drive 1 kilometer on his car; b \u2014 the time, which Vasiliy spends to walk 1 kilometer on foot; t \u2014 the time, which Vasiliy spends to repair his car. \n\n\n-----Output-----\n\nPrint the minimal time after which Vasiliy will be able to reach the post office.\n\n\n-----Examples-----\nInput\n5 2 1 4 10\n\nOutput\n14\n\nInput\n5 2 1 4 5\n\nOutput\n13\n\n\n\n-----Note-----\n\nIn the first example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds) and then to walk on foot 3 kilometers (in 12 seconds). So the answer equals to 14 seconds.\n\nIn the second example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds), then repair his car (in 5 seconds) and drive 2 kilometers more on the car (in 2 seconds). After that he needs to walk on foot 1 kilometer (in 4 seconds). So the answer equals to 13 seconds."} +{"id": "00045", "text": "You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a_1, a_2, ..., a_{k}, that their sum is equal to n and greatest common divisor is maximal.\n\nGreatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.\n\nIf there is no possible sequence then output -1.\n\n\n-----Input-----\n\nThe first line consists of two numbers n and k (1 \u2264 n, k \u2264 10^10).\n\n\n-----Output-----\n\nIf the answer exists then output k numbers \u2014 resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n6 3\n\nOutput\n1 2 3\n\nInput\n8 2\n\nOutput\n2 6\n\nInput\n5 3\n\nOutput\n-1"} +{"id": "00046", "text": "After finishing eating her bun, Alyona came up with two integers n and m. She decided to write down two columns of integers\u00a0\u2014 the first column containing integers from 1 to n and the second containing integers from 1 to m. Now the girl wants to count how many pairs of integers she can choose, one from the first column and the other from the second column, such that their sum is divisible by 5.\n\nFormally, Alyona wants to count the number of pairs of integers (x, y) such that 1 \u2264 x \u2264 n, 1 \u2264 y \u2264 m and $(x + y) \\operatorname{mod} 5$ equals 0.\n\nAs usual, Alyona has some troubles and asks you to help.\n\n\n-----Input-----\n\nThe only line of the input contains two integers n and m (1 \u2264 n, m \u2264 1 000 000).\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the number of pairs of integers (x, y) such that 1 \u2264 x \u2264 n, 1 \u2264 y \u2264 m and (x + y) is divisible by 5.\n\n\n-----Examples-----\nInput\n6 12\n\nOutput\n14\n\nInput\n11 14\n\nOutput\n31\n\nInput\n1 5\n\nOutput\n1\n\nInput\n3 8\n\nOutput\n5\n\nInput\n5 7\n\nOutput\n7\n\nInput\n21 21\n\nOutput\n88\n\n\n\n-----Note-----\n\nFollowing pairs are suitable in the first sample case: for x = 1 fits y equal to 4 or 9; for x = 2 fits y equal to 3 or 8; for x = 3 fits y equal to 2, 7 or 12; for x = 4 fits y equal to 1, 6 or 11; for x = 5 fits y equal to 5 or 10; for x = 6 fits y equal to 4 or 9. \n\nOnly the pair (1, 4) is suitable in the third sample case."} +{"id": "00047", "text": "You are given an array $a$ consisting of $n$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0.\n\nYou may choose at most one consecutive subarray of $a$ and multiply all values contained in this subarray by $x$. You want to maximize the beauty of array after applying at most one such operation.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $x$ ($1 \\le n \\le 3 \\cdot 10^5, -100 \\le x \\le 100$) \u2014 the length of array $a$ and the integer $x$ respectively.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^9 \\le a_i \\le 10^9$) \u2014 the array $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible beauty of array $a$ after multiplying all values belonging to some consecutive subarray $x$.\n\n\n-----Examples-----\nInput\n5 -2\n-3 8 -2 1 -6\n\nOutput\n22\n\nInput\n12 -3\n1 3 3 7 1 3 3 7 1 3 3 7\n\nOutput\n42\n\nInput\n5 10\n-1 -2 -3 -4 -5\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]).\n\nIn the second test case we don't need to multiply any subarray at all.\n\nIn the third test case no matter which subarray we multiply, the beauty of array will be equal to 0."} +{"id": "00048", "text": "Bizon the Champion isn't just charming, he also is very smart.\n\nWhile some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n \u00d7 m multiplication table, where the element on the intersection of the i-th row and j-th column equals i\u00b7j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?\n\nConsider the given multiplication table. If you write out all n\u00b7m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.\n\n\n-----Input-----\n\nThe single line contains integers n, m and k (1 \u2264 n, m \u2264 5\u00b710^5;\u00a01 \u2264 k \u2264 n\u00b7m).\n\n\n-----Output-----\n\nPrint the k-th largest number in a n \u00d7 m multiplication table.\n\n\n-----Examples-----\nInput\n2 2 2\n\nOutput\n2\n\nInput\n2 3 4\n\nOutput\n3\n\nInput\n1 10 5\n\nOutput\n5\n\n\n\n-----Note-----\n\nA 2 \u00d7 3 multiplication table looks like this:\n\n1 2 3\n\n2 4 6"} +{"id": "00049", "text": "Let's write all the positive integer numbers one after another from $1$ without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...\n\nYour task is to print the $k$-th digit of this sequence.\n\n\n-----Input-----\n\nThe first and only line contains integer $k$ ($1 \\le k \\le 10^{12}$) \u2014 the position to process ($1$-based index).\n\n\n-----Output-----\n\nPrint the $k$-th digit of the resulting infinite sequence.\n\n\n-----Examples-----\nInput\n7\n\nOutput\n7\n\nInput\n21\n\nOutput\n5"} +{"id": "00050", "text": "Welcome to Codeforces Stock Exchange! We're pretty limited now as we currently allow trading on one stock, Codeforces Ltd. We hope you'll still be able to make profit from the market!\n\nIn the morning, there are $n$ opportunities to buy shares. The $i$-th of them allows to buy as many shares as you want, each at the price of $s_i$ bourles.\n\nIn the evening, there are $m$ opportunities to sell shares. The $i$-th of them allows to sell as many shares as you want, each at the price of $b_i$ bourles. You can't sell more shares than you have.\n\nIt's morning now and you possess $r$ bourles and no shares.\n\nWhat is the maximum number of bourles you can hold after the evening?\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n, m, r$ ($1 \\leq n \\leq 30$, $1 \\leq m \\leq 30$, $1 \\leq r \\leq 1000$) \u2014 the number of ways to buy the shares on the market, the number of ways to sell the shares on the market, and the number of bourles you hold now.\n\nThe next line contains $n$ integers $s_1, s_2, \\dots, s_n$ ($1 \\leq s_i \\leq 1000$); $s_i$ indicates the opportunity to buy shares at the price of $s_i$ bourles.\n\nThe following line contains $m$ integers $b_1, b_2, \\dots, b_m$ ($1 \\leq b_i \\leq 1000$); $b_i$ indicates the opportunity to sell shares at the price of $b_i$ bourles.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the maximum number of bourles you can hold after the evening.\n\n\n-----Examples-----\nInput\n3 4 11\n4 2 5\n4 4 5 4\n\nOutput\n26\n\nInput\n2 2 50\n5 7\n4 2\n\nOutput\n50\n\n\n\n-----Note-----\n\nIn the first example test, you have $11$ bourles in the morning. It's optimal to buy $5$ shares of a stock at the price of $2$ bourles in the morning, and then to sell all of them at the price of $5$ bourles in the evening. It's easy to verify that you'll have $26$ bourles after the evening.\n\nIn the second example test, it's optimal not to take any action."} +{"id": "00051", "text": "\u0412 \u0411\u0435\u0440\u043b\u044f\u043d\u0434\u0441\u043a\u043e\u043c \u0433\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0438\u0442\u0435\u0442\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0435\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c\u0438 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u0435\u0437 \u043e\u0448\u0438\u0431\u043e\u043a. \u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0434\u0432\u0443\u0445 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0440\u044f\u0434 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u043e\u0448\u0438\u0431\u043a\u0430, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044d\u0442\u0438 \u0434\u0432\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0441\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e. \u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0438\u044f\u043d\u0438\u0438 \u043a\u043e\u043d\u0435\u0446 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0441\u043e\u0432\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0441 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u0432\u0442\u043e\u0440\u043e\u0433\u043e. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0441\u043e\u0432\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c. \u0414\u043b\u0438\u043d\u0430 \u0441\u043e\u0432\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c, \u043c\u0435\u043d\u044c\u0448\u0438\u043c \u0434\u043b\u0438\u043d\u044b \u0442\u0435\u043a\u0441\u0442\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f. \n\n\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0434\u0432\u0443\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u00ababrakadabra\u00bb \u043f\u043e\u0434\u0440\u044f\u0434 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0447\u0442\u043e \u043e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043e \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u0432\u0438\u0434\u0430, \u0438 \u0442\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432\u0438\u0434\u0430 \u00ababrakadabrabrakadabra\u00bb \u0438\u043b\u0438 \u00ababrakadabrakadabra\u00bb (\u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043e\u0432\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u0443, \u0430 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u2014 \u043f\u043e \u0447\u0435\u0442\u044b\u0440\u0435\u043c).\n\n\u041f\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u043c\u0443 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044e t \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043b\u0438, \u0447\u0442\u043e \u044d\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u0432\u0438\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0435\u0442\u0438, \u0438 \u0435\u0441\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 s. \n\n\u041d\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043d\u0430\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u0440\u0443\u0433\u0430 \u043d\u0430 \u0434\u0440\u0443\u0433\u0430 \u0434\u0432\u0443\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u00ababcd\u00bb, \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0432 \u043d\u0451\u043c \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0435\u0442. \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e, \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0434\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432\u0441\u043b\u0435\u0434 \u0437\u0430 \u0434\u0440\u0443\u0433\u0438\u043c \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u043c \u043e\u0448\u0438\u0431\u043a\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u00ababcabc\u00bb, \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0432 \u043d\u0451\u043c \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0435\u0442.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043d\u0435\u043f\u0443\u0441\u0442\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 t, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0430\u044f \u0438\u0437 \u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0445 \u0431\u0443\u043a\u0432 \u043b\u0430\u0442\u0438\u043d\u0441\u043a\u043e\u0433\u043e \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u0430. \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 t \u043d\u0435 \u043f\u0440\u0435\u0432\u043e\u0441\u0445\u043e\u0434\u0438\u0442 100 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0415\u0441\u043b\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 t \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u00abNO\u00bb (\u0431\u0435\u0437 \u043a\u0430\u0432\u044b\u0447\u0435\u043a) \u0432 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.\n\n\u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u00abYES\u00bb (\u0431\u0435\u0437 \u043a\u0430\u0432\u044b\u0447\u0435\u043a), \u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u043a\u0443 s\u00a0\u2014 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0433\u043b\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043e\u0448\u0438\u0431\u043a\u0435. \u0415\u0441\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nabrakadabrabrakadabra\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nYES\nabrakadabra\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nacacacaca\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nYES\nacaca\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nabcabc\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nNO\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nabababab\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nYES\nababab\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\ntatbt\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nNO\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u043c \u043e\u0442\u0432\u0435\u0442\u043e\u043c \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430 acacaca."} +{"id": "00052", "text": "Daniel is organizing a football tournament. He has come up with the following tournament format: In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are held while the number of teams is even. Eventually there will be an odd number of teams remaining. If there is one team remaining, it will be declared the winner, and the tournament ends. Otherwise each of the remaining teams will play with each other remaining team once in round robin tournament (if there are x teams, there will be $\\frac{x \\cdot(x - 1)}{2}$ games), and the tournament ends. \n\nFor example, if there were 20 teams initially, they would begin by playing 10 games. So, 10 teams would be eliminated, and the remaining 10 would play 5 games. Then the remaining 5 teams would play 10 games in a round robin tournament. In total there would be 10+5+10=25 games.\n\nDaniel has already booked the stadium for n games. Help him to determine how many teams he should invite so that the tournament needs exactly n games. You should print all possible numbers of teams that will yield exactly n games in ascending order, or -1 if there are no such numbers.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^18), the number of games that should be played.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nPrint all possible numbers of invited teams in ascending order, one per line. If exactly n games cannot be played, output one number: -1.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n3\n4\n\nInput\n25\n\nOutput\n20\n\nInput\n2\n\nOutput\n-1"} +{"id": "00053", "text": "A string a of length m is called antipalindromic iff m is even, and for each i (1 \u2264 i \u2264 m) a_{i} \u2260 a_{m} - i + 1.\n\nIvan has a string s consisting of n lowercase Latin letters; n is even. He wants to form some string t that will be an antipalindromic permutation of s. Also Ivan has denoted the beauty of index i as b_{i}, and the beauty of t as the sum of b_{i} among all indices i such that s_{i} = t_{i}.\n\nHelp Ivan to determine maximum possible beauty of t he can get.\n\n\n-----Input-----\n\nThe first line contains one integer n (2 \u2264 n \u2264 100, n is even) \u2014 the number of characters in s.\n\nThe second line contains the string s itself. It consists of only lowercase Latin letters, and it is guaranteed that its letters can be reordered to form an antipalindromic string.\n\nThe third line contains n integer numbers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 100), where b_{i} is the beauty of index i.\n\n\n-----Output-----\n\nPrint one number \u2014 the maximum possible beauty of t.\n\n\n-----Examples-----\nInput\n8\nabacabac\n1 1 1 1 1 1 1 1\n\nOutput\n8\n\nInput\n8\nabaccaba\n1 2 3 4 5 6 7 8\n\nOutput\n26\n\nInput\n8\nabacabca\n1 2 3 4 4 3 2 1\n\nOutput\n17"} +{"id": "00054", "text": "Vanya has a scales for weighing loads and weights of masses w^0, w^1, w^2, ..., w^100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.\n\n\n-----Input-----\n\nThe first line contains two integers w, m (2 \u2264 w \u2264 10^9, 1 \u2264 m \u2264 10^9) \u2014 the number defining the masses of the weights and the mass of the item.\n\n\n-----Output-----\n\nPrint word 'YES' if the item can be weighted and 'NO' if it cannot.\n\n\n-----Examples-----\nInput\n3 7\n\nOutput\nYES\n\nInput\n100 99\n\nOutput\nYES\n\nInput\n100 50\n\nOutput\nNO\n\n\n\n-----Note-----\n\nNote to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.\n\nNote to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.\n\nNote to the third sample test. It is impossible to measure the weight of the item in the manner described in the input."} +{"id": "00055", "text": "Jamie is preparing a Codeforces round. He has got an idea for a problem, but does not know how to solve it. Help him write a solution to the following problem:\n\nFind k integers such that the sum of two to the power of each number equals to the number n and the largest integer in the answer is as small as possible. As there may be multiple answers, you are asked to output the lexicographically largest one. \n\nTo be more clear, consider all integer sequence with length k (a_1, a_2, ..., a_{k}) with $\\sum_{i = 1}^{k} 2^{a_{i}} = n$. Give a value $y = \\operatorname{max}_{1 \\leq i \\leq k} a_{i}$ to each sequence. Among all sequence(s) that have the minimum y value, output the one that is the lexicographically largest.\n\nFor definitions of powers and lexicographical order see notes.\n\n\n-----Input-----\n\nThe first line consists of two integers n and k (1 \u2264 n \u2264 10^18, 1 \u2264 k \u2264 10^5)\u00a0\u2014 the required sum and the length of the sequence.\n\n\n-----Output-----\n\nOutput \"No\" (without quotes) in a single line if there does not exist such sequence. Otherwise, output \"Yes\" (without quotes) in the first line, and k numbers separated by space in the second line\u00a0\u2014 the required sequence.\n\nIt is guaranteed that the integers in the answer sequence fit the range [ - 10^18, 10^18].\n\n\n-----Examples-----\nInput\n23 5\n\nOutput\nYes\n3 3 2 1 0 \n\nInput\n13 2\n\nOutput\nNo\n\nInput\n1 2\n\nOutput\nYes\n-1 -1 \n\n\n\n-----Note-----\n\nSample 1:\n\n2^3 + 2^3 + 2^2 + 2^1 + 2^0 = 8 + 8 + 4 + 2 + 1 = 23\n\nAnswers like (3, 3, 2, 0, 1) or (0, 1, 2, 3, 3) are not lexicographically largest.\n\nAnswers like (4, 1, 1, 1, 0) do not have the minimum y value.\n\nSample 2:\n\nIt can be shown there does not exist a sequence with length 2.\n\nSample 3:\n\n$2^{-1} + 2^{-1} = \\frac{1}{2} + \\frac{1}{2} = 1$\n\nPowers of 2:\n\nIf x > 0, then 2^{x} = 2\u00b72\u00b72\u00b7...\u00b72 (x times).\n\nIf x = 0, then 2^{x} = 1.\n\nIf x < 0, then $2^{x} = \\frac{1}{2^{-x}}$.\n\nLexicographical order:\n\nGiven two different sequences of the same length, (a_1, a_2, ... , a_{k}) and (b_1, b_2, ... , b_{k}), the first one is smaller than the second one for the lexicographical order, if and only if a_{i} < b_{i}, for the first i where a_{i} and b_{i} differ."} +{"id": "00056", "text": "Mary has just graduated from one well-known University and is now attending celebration party. Students like to dream of a beautiful life, so they used champagne glasses to construct a small pyramid. The height of the pyramid is n. The top level consists of only 1 glass, that stands on 2 glasses on the second level (counting from the top), then 3 glasses on the third level and so on.The bottom level consists of n glasses.\n\nVlad has seen in the movies many times how the champagne beautifully flows from top levels to bottom ones, filling all the glasses simultaneously. So he took a bottle and started to pour it in the glass located at the top of the pyramid.\n\nEach second, Vlad pours to the top glass the amount of champagne equal to the size of exactly one glass. If the glass is already full, but there is some champagne flowing in it, then it pours over the edge of the glass and is equally distributed over two glasses standing under. If the overflowed glass is at the bottom level, then the champagne pours on the table. For the purpose of this problem we consider that champagne is distributed among pyramid glasses immediately. Vlad is interested in the number of completely full glasses if he stops pouring champagne in t seconds.\n\nPictures below illustrate the pyramid consisting of three levels. [Image] [Image] \n\n\n-----Input-----\n\nThe only line of the input contains two integers n and t (1 \u2264 n \u2264 10, 0 \u2264 t \u2264 10 000)\u00a0\u2014 the height of the pyramid and the number of seconds Vlad will be pouring champagne from the bottle.\n\n\n-----Output-----\n\nPrint the single integer\u00a0\u2014 the number of completely full glasses after t seconds.\n\n\n-----Examples-----\nInput\n3 5\n\nOutput\n4\n\nInput\n4 8\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first sample, the glasses full after 5 seconds are: the top glass, both glasses on the second level and the middle glass at the bottom level. Left and right glasses of the bottom level will be half-empty."} +{"id": "00057", "text": "After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the area of the rectangle must be positive. Wilbur had all four vertices of the planned pool written on a paper, until his friend came along and erased some of the vertices.\n\nNow Wilbur is wondering, if the remaining n vertices of the initial rectangle give enough information to restore the area of the planned swimming pool.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 4)\u00a0\u2014 the number of vertices that were not erased by Wilbur's friend.\n\nEach of the following n lines contains two integers x_{i} and y_{i} ( - 1000 \u2264 x_{i}, y_{i} \u2264 1000)\u00a0\u2014the coordinates of the i-th vertex that remains. Vertices are given in an arbitrary order.\n\nIt's guaranteed that these points are distinct vertices of some rectangle, that has positive area and which sides are parallel to the coordinate axes.\n\n\n-----Output-----\n\nPrint the area of the initial rectangle if it could be uniquely determined by the points remaining. Otherwise, print - 1. \n\n\n-----Examples-----\nInput\n2\n0 0\n1 1\n\nOutput\n1\n\nInput\n1\n1 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample, two opposite corners of the initial rectangle are given, and that gives enough information to say that the rectangle is actually a unit square.\n\nIn the second sample there is only one vertex left and this is definitely not enough to uniquely define the area."} +{"id": "00058", "text": "Petya has equal wooden bars of length n. He wants to make a frame for two equal doors. Each frame has two vertical (left and right) sides of length a and one top side of length b. A solid (i.e. continuous without breaks) piece of bar is needed for each side.\n\nDetermine a minimal number of wooden bars which are needed to make the frames for two doors. Petya can cut the wooden bars into any parts, but each side of each door should be a solid piece of a wooden bar (or a whole wooden bar).\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 1 000) \u2014 the length of each wooden bar.\n\nThe second line contains a single integer a (1 \u2264 a \u2264 n) \u2014 the length of the vertical (left and right) sides of a door frame.\n\nThe third line contains a single integer b (1 \u2264 b \u2264 n) \u2014 the length of the upper side of a door frame.\n\n\n-----Output-----\n\nPrint the minimal number of wooden bars with length n which are needed to make the frames for two doors.\n\n\n-----Examples-----\nInput\n8\n1\n2\n\nOutput\n1\n\nInput\n5\n3\n4\n\nOutput\n6\n\nInput\n6\n4\n2\n\nOutput\n4\n\nInput\n20\n5\n6\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example one wooden bar is enough, since the total length of all six sides of the frames for two doors is 8.\n\nIn the second example 6 wooden bars is enough, because for each side of the frames the new wooden bar is needed."} +{"id": "00059", "text": "You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this array.\n\nFor some indices i (1 \u2264 i \u2264 n - 1) it is possible to swap i-th element with (i + 1)-th, for other indices it is not possible. You may perform any number of swapping operations any order. There is no limit on the number of times you swap i-th element with (i + 1)-th (if the position is not forbidden).\n\nCan you make this array sorted in ascending order performing some sequence of swapping operations?\n\n\n-----Input-----\n\nThe first line contains one integer n (2 \u2264 n \u2264 200000) \u2014 the number of elements in the array.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 200000) \u2014 the elements of the array. Each integer from 1 to n appears exactly once.\n\nThe third line contains a string of n - 1 characters, each character is either 0 or 1. If i-th character is 1, then you can swap i-th element with (i + 1)-th any number of times, otherwise it is forbidden to swap i-th element with (i + 1)-th.\n\n\n-----Output-----\n\nIf it is possible to sort the array in ascending order using any sequence of swaps you are allowed to make, print YES. Otherwise, print NO.\n\n\n-----Examples-----\nInput\n6\n1 2 5 3 4 6\n01110\n\nOutput\nYES\n\nInput\n6\n1 2 5 3 4 6\n01010\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example you may swap a_3 and a_4, and then swap a_4 and a_5."} +{"id": "00060", "text": "A new airplane SuperPuperJet has an infinite number of rows, numbered with positive integers starting with 1 from cockpit to tail. There are six seats in each row, denoted with letters from 'a' to 'f'. Seats 'a', 'b' and 'c' are located to the left of an aisle (if one looks in the direction of the cockpit), while seats 'd', 'e' and 'f' are located to the right. Seats 'a' and 'f' are located near the windows, while seats 'c' and 'd' are located near the aisle. [Image] \n\n\u00a0\n\nIt's lunch time and two flight attendants have just started to serve food. They move from the first rows to the tail, always maintaining a distance of two rows from each other because of the food trolley. Thus, at the beginning the first attendant serves row 1 while the second attendant serves row 3. When both rows are done they move one row forward: the first attendant serves row 2 while the second attendant serves row 4. Then they move three rows forward and the first attendant serves row 5 while the second attendant serves row 7. Then they move one row forward again and so on.\n\nFlight attendants work with the same speed: it takes exactly 1 second to serve one passenger and 1 second to move one row forward. Each attendant first serves the passengers on the seats to the right of the aisle and then serves passengers on the seats to the left of the aisle (if one looks in the direction of the cockpit). Moreover, they always serve passengers in order from the window to the aisle. Thus, the first passenger to receive food in each row is located in seat 'f', and the last one\u00a0\u2014 in seat 'c'. Assume that all seats are occupied.\n\nVasya has seat s in row n and wants to know how many seconds will pass before he gets his lunch.\n\n\n-----Input-----\n\nThe only line of input contains a description of Vasya's seat in the format ns, where n (1 \u2264 n \u2264 10^18) is the index of the row and s is the seat in this row, denoted as letter from 'a' to 'f'. The index of the row and the seat are not separated by a space.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of seconds Vasya has to wait until he gets his lunch.\n\n\n-----Examples-----\nInput\n1f\n\nOutput\n1\n\nInput\n2d\n\nOutput\n10\n\nInput\n4a\n\nOutput\n11\n\nInput\n5e\n\nOutput\n18\n\n\n\n-----Note-----\n\nIn the first sample, the first flight attendant serves Vasya first, so Vasya gets his lunch after 1 second.\n\nIn the second sample, the flight attendants will spend 6 seconds to serve everyone in the rows 1 and 3, then they will move one row forward in 1 second. As they first serve seats located to the right of the aisle in order from window to aisle, Vasya has to wait 3 more seconds. The total is 6 + 1 + 3 = 10."} +{"id": "00061", "text": "After seeing the \"ALL YOUR BASE ARE BELONG TO US\" meme for the first time, numbers X and Y realised that they have different bases, which complicated their relations.\n\nYou're given a number X represented in base b_{x} and a number Y represented in base b_{y}. Compare those two numbers.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and b_{x} (1 \u2264 n \u2264 10, 2 \u2264 b_{x} \u2264 40), where n is the number of digits in the b_{x}-based representation of X. \n\nThe second line contains n space-separated integers x_1, x_2, ..., x_{n} (0 \u2264 x_{i} < b_{x}) \u2014 the digits of X. They are given in the order from the most significant digit to the least significant one.\n\nThe following two lines describe Y in the same way: the third line contains two space-separated integers m and b_{y} (1 \u2264 m \u2264 10, 2 \u2264 b_{y} \u2264 40, b_{x} \u2260 b_{y}), where m is the number of digits in the b_{y}-based representation of Y, and the fourth line contains m space-separated integers y_1, y_2, ..., y_{m} (0 \u2264 y_{i} < b_{y}) \u2014 the digits of Y.\n\nThere will be no leading zeroes. Both X and Y will be positive. All digits of both numbers are given in the standard decimal numeral system.\n\n\n-----Output-----\n\nOutput a single character (quotes for clarity): '<' if X < Y '>' if X > Y '=' if X = Y \n\n\n-----Examples-----\nInput\n6 2\n1 0 1 1 1 1\n2 10\n4 7\n\nOutput\n=\n\nInput\n3 3\n1 0 2\n2 5\n2 4\n\nOutput\n<\n\nInput\n7 16\n15 15 4 0 0 7 10\n7 9\n4 8 0 3 1 5 0\n\nOutput\n>\n\n\n\n-----Note-----\n\nIn the first sample, X = 101111_2 = 47_10 = Y.\n\nIn the second sample, X = 102_3 = 21_5 and Y = 24_5 = 112_3, thus X < Y.\n\nIn the third sample, $X = FF 4007 A_{16}$ and Y = 4803150_9. We may notice that X starts with much larger digits and b_{x} is much larger than b_{y}, so X is clearly larger than Y."} +{"id": "00062", "text": "Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games!\n\nSince you are so curious about Bitland, I'll give you the chance of peeking at one of these games.\n\nBitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a_1, a_2, ..., a_{n}. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn:\n\n Take one of the integers (we'll denote it as a_{i}). Choose integer x (1 \u2264 x \u2264 a_{i}). And then decrease a_{i} by x, that is, apply assignment: a_{i} = a_{i} - x. Choose integer x $(1 \\leq x \\leq \\operatorname{min}_{i = 1} a_{i})$. And then decrease all a_{i} by x, that is, apply assignment: a_{i} = a_{i} - x, for all i. \n\nThe player who cannot make a move loses.\n\nYou're given the initial sequence a_1, a_2, ..., a_{n}. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 3).\n\nThe next line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} < 300).\n\n\n-----Output-----\n\nWrite the name of the winner (provided that both players play optimally well). Either \"BitLGM\" or \"BitAryo\" (without the quotes).\n\n\n-----Examples-----\nInput\n2\n1 1\n\nOutput\nBitLGM\n\nInput\n2\n1 2\n\nOutput\nBitAryo\n\nInput\n3\n1 2 1\n\nOutput\nBitLGM"} +{"id": "00063", "text": "Vova again tries to play some computer card game.\n\nThe rules of deck creation in this game are simple. Vova is given an existing deck of n cards and a magic number k. The order of the cards in the deck is fixed. Each card has a number written on it; number a_{i} is written on the i-th card in the deck.\n\nAfter receiving the deck and the magic number, Vova removes x (possibly x = 0) cards from the top of the deck, y (possibly y = 0) cards from the bottom of the deck, and the rest of the deck is his new deck (Vova has to leave at least one card in the deck after removing cards). So Vova's new deck actually contains cards x + 1, x + 2, ... n - y - 1, n - y from the original deck.\n\nVova's new deck is considered valid iff the product of all numbers written on the cards in his new deck is divisible by k. So Vova received a deck (possibly not a valid one) and a number k, and now he wonders, how many ways are there to choose x and y so the deck he will get after removing x cards from the top and y cards from the bottom is valid?\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 100 000, 1 \u2264 k \u2264 10^9).\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 the numbers written on the cards.\n\n\n-----Output-----\n\nPrint the number of ways to choose x and y so the resulting deck is valid.\n\n\n-----Examples-----\nInput\n3 4\n6 2 8\n\nOutput\n4\n\nInput\n3 6\n9 1 14\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example the possible values of x and y are:\n\n x = 0, y = 0; x = 1, y = 0; x = 2, y = 0; x = 0, y = 1."} +{"id": "00064", "text": "One day Kefa found n baloons. For convenience, we denote color of i-th baloon as s_{i} \u2014 lowercase letter of the Latin alphabet. Also Kefa has k friends. Friend will be upset, If he get two baloons of the same color. Kefa want to give out all baloons to his friends. Help Kefa to find out, can he give out all his baloons, such that no one of his friens will be upset \u2014 print \u00abYES\u00bb, if he can, and \u00abNO\u00bb, otherwise. Note, that Kefa's friend will not upset, if he doesn't get baloons at all.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n, k \u2264 100) \u2014 the number of baloons and friends.\n\nNext line contains string s \u2014 colors of baloons.\n\n\n-----Output-----\n\nAnswer to the task \u2014 \u00abYES\u00bb or \u00abNO\u00bb in a single line.\n\nYou can choose the case (lower or upper) for each letter arbitrary.\n\n\n-----Examples-----\nInput\n4 2\naabb\n\nOutput\nYES\n\nInput\n6 3\naacaab\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample Kefa can give 1-st and 3-rd baloon to the first friend, and 2-nd and 4-th to the second.\n\nIn the second sample Kefa needs to give to all his friends baloons of color a, but one baloon will stay, thats why answer is \u00abNO\u00bb."} +{"id": "00065", "text": "You are given an array of n integer numbers a_0, a_1, ..., a_{n} - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.\n\n\n-----Input-----\n\nThe first line contains positive integer n (2 \u2264 n \u2264 10^5) \u2014 size of the given array. The second line contains n integers a_0, a_1, ..., a_{n} - 1 (1 \u2264 a_{i} \u2264 10^9) \u2014 elements of the array. It is guaranteed that in the array a minimum occurs at least two times.\n\n\n-----Output-----\n\nPrint the only number \u2014 distance between two nearest minimums in the array.\n\n\n-----Examples-----\nInput\n2\n3 3\n\nOutput\n1\n\nInput\n3\n5 6 5\n\nOutput\n2\n\nInput\n9\n2 1 3 5 4 1 2 3 1\n\nOutput\n3"} +{"id": "00066", "text": "Vector Willman and Array Bolt are the two most famous athletes of Byteforces. They are going to compete in a race with a distance of L meters today.\n\n [Image] \n\nWillman and Bolt have exactly the same speed, so when they compete the result is always a tie. That is a problem for the organizers because they want a winner. \n\nWhile watching previous races the organizers have noticed that Willman can perform only steps of length equal to w meters, and Bolt can perform only steps of length equal to b meters. Organizers decided to slightly change the rules of the race. Now, at the end of the racetrack there will be an abyss, and the winner will be declared the athlete, who manages to run farther from the starting point of the the racetrack (which is not the subject to change by any of the athletes). \n\nNote that none of the athletes can run infinitely far, as they both will at some moment of time face the point, such that only one step further will cause them to fall in the abyss. In other words, the athlete will not fall into the abyss if the total length of all his steps will be less or equal to the chosen distance L.\n\nSince the organizers are very fair, the are going to set the length of the racetrack as an integer chosen randomly and uniformly in range from 1 to t (both are included). What is the probability that Willman and Bolt tie again today?\n\n\n-----Input-----\n\nThe first line of the input contains three integers t, w and b (1 \u2264 t, w, b \u2264 5\u00b710^18) \u2014 the maximum possible length of the racetrack, the length of Willman's steps and the length of Bolt's steps respectively.\n\n\n-----Output-----\n\nPrint the answer to the problem as an irreducible fraction [Image]. Follow the format of the samples output.\n\nThe fraction [Image] (p and q are integers, and both p \u2265 0 and q > 0 holds) is called irreducible, if there is no such integer d > 1, that both p and q are divisible by d.\n\n\n-----Examples-----\nInput\n10 3 2\n\nOutput\n3/10\n\nInput\n7 1 2\n\nOutput\n3/7\n\n\n\n-----Note-----\n\nIn the first sample Willman and Bolt will tie in case 1, 6 or 7 are chosen as the length of the racetrack."} +{"id": "00067", "text": "Nauuo is a girl who loves writing comments.\n\nOne day, she posted a comment on Codeforces, wondering whether she would get upvotes or downvotes.\n\nIt's known that there were $x$ persons who would upvote, $y$ persons who would downvote, and there were also another $z$ persons who would vote, but you don't know whether they would upvote or downvote. Note that each of the $x+y+z$ people would vote exactly one time.\n\nThere are three different results: if there are more people upvote than downvote, the result will be \"+\"; if there are more people downvote than upvote, the result will be \"-\"; otherwise the result will be \"0\".\n\nBecause of the $z$ unknown persons, the result may be uncertain (i.e. there are more than one possible results). More formally, the result is uncertain if and only if there exist two different situations of how the $z$ persons vote, that the results are different in the two situations.\n\nTell Nauuo the result or report that the result is uncertain.\n\n\n-----Input-----\n\nThe only line contains three integers $x$, $y$, $z$ ($0\\le x,y,z\\le100$), corresponding to the number of persons who would upvote, downvote or unknown.\n\n\n-----Output-----\n\nIf there is only one possible result, print the result : \"+\", \"-\" or \"0\".\n\nOtherwise, print \"?\" to report that the result is uncertain.\n\n\n-----Examples-----\nInput\n3 7 0\n\nOutput\n-\nInput\n2 0 1\n\nOutput\n+\nInput\n1 1 0\n\nOutput\n0\nInput\n0 0 1\n\nOutput\n?\n\n\n-----Note-----\n\nIn the first example, Nauuo would definitely get three upvotes and seven downvotes, so the only possible result is \"-\".\n\nIn the second example, no matter the person unknown downvotes or upvotes, Nauuo would get more upvotes than downvotes. So the only possible result is \"+\".\n\nIn the third example, Nauuo would definitely get one upvote and one downvote, so the only possible result is \"0\".\n\nIn the fourth example, if the only one person upvoted, the result would be \"+\", otherwise, the result would be \"-\". There are two possible results, so the result is uncertain."} +{"id": "00068", "text": "Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U \u2014 move from $(x, y)$ to $(x, y + 1)$; D \u2014 move from $(x, y)$ to $(x, y - 1)$; L \u2014 move from $(x, y)$ to $(x - 1, y)$; R \u2014 move from $(x, y)$ to $(x + 1, y)$. \n\nVasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.\n\nVasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$. \n\nIf there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.\n\nHelp Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.\n\n\n-----Input-----\n\nThe first line contains one integer number $n~(1 \\le n \\le 2 \\cdot 10^5)$ \u2014 the number of operations.\n\nThe second line contains the sequence of operations \u2014 a string of $n$ characters. Each character is either U, D, L or R.\n\nThe third line contains two integers $x, y~(-10^9 \\le x, y \\le 10^9)$ \u2014 the coordinates of the cell where the robot should end its path.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.\n\n\n-----Examples-----\nInput\n5\nRURUU\n-2 3\n\nOutput\n3\n\nInput\n4\nRULR\n1 1\n\nOutput\n0\n\nInput\n3\nUUU\n100 100\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.\n\nIn the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.\n\nIn the third example the robot can't end his path in the cell $(x, y)$."} +{"id": "00069", "text": "You are given string $s$ of length $n$ consisting of 0-s and 1-s. You build an infinite string $t$ as a concatenation of an infinite number of strings $s$, or $t = ssss \\dots$ For example, if $s =$ 10010, then $t =$ 100101001010010...\n\nCalculate the number of prefixes of $t$ with balance equal to $x$. The balance of some string $q$ is equal to $cnt_{0, q} - cnt_{1, q}$, where $cnt_{0, q}$ is the number of occurrences of 0 in $q$, and $cnt_{1, q}$ is the number of occurrences of 1 in $q$. The number of such prefixes can be infinite; if it is so, you must say that.\n\nA prefix is a string consisting of several first letters of a given string, without any reorders. An empty prefix is also a valid prefix. For example, the string \"abcd\" has 5 prefixes: empty string, \"a\", \"ab\", \"abc\" and \"abcd\".\n\n\n-----Input-----\n\nThe first line contains the single integer $T$ ($1 \\le T \\le 100$) \u2014 the number of test cases.\n\nNext $2T$ lines contain descriptions of test cases \u2014 two lines per test case. The first line contains two integers $n$ and $x$ ($1 \\le n \\le 10^5$, $-10^9 \\le x \\le 10^9$) \u2014 the length of string $s$ and the desired balance, respectively.\n\nThe second line contains the binary string $s$ ($|s| = n$, $s_i \\in \\{\\text{0}, \\text{1}\\}$).\n\nIt's guaranteed that the total sum of $n$ doesn't exceed $10^5$.\n\n\n-----Output-----\n\nPrint $T$ integers \u2014 one per test case. For each test case print the number of prefixes or $-1$ if there is an infinite number of such prefixes.\n\n\n-----Example-----\nInput\n4\n6 10\n010010\n5 3\n10101\n1 0\n0\n2 0\n01\n\nOutput\n3\n0\n1\n-1\n\n\n\n-----Note-----\n\nIn the first test case, there are 3 good prefixes of $t$: with length $28$, $30$ and $32$."} +{"id": "00070", "text": "Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10^{k}.\n\nIn the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10^{k}. For example, if k = 3, in the number 30020 it is enough to delete a single digit (2). In this case, the result is 3000 that is divisible by 10^3 = 1000.\n\nWrite a program that prints the minimum number of digits to be deleted from the given integer number n, so that the result is divisible by 10^{k}. The result should not start with the unnecessary leading zero (i.e., zero can start only the number 0, which is required to be written as exactly one digit).\n\nIt is guaranteed that the answer exists.\n\n\n-----Input-----\n\nThe only line of the input contains two integer numbers n and k (0 \u2264 n \u2264 2 000 000 000, 1 \u2264 k \u2264 9).\n\nIt is guaranteed that the answer exists. All numbers in the input are written in traditional notation of integers, that is, without any extra leading zeros.\n\n\n-----Output-----\n\nPrint w \u2014 the required minimal number of digits to erase. After removing the appropriate w digits from the number n, the result should have a value that is divisible by 10^{k}. The result can start with digit 0 in the single case (the result is zero and written by exactly the only digit 0).\n\n\n-----Examples-----\nInput\n30020 3\n\nOutput\n1\n\nInput\n100 9\n\nOutput\n2\n\nInput\n10203049 2\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the example 2 you can remove two digits: 1 and any 0. The result is number 0 which is divisible by any number."} +{"id": "00071", "text": "On the Literature lesson Sergei noticed an awful injustice, it seems that some students are asked more often than others.\n\nSeating in the class looks like a rectangle, where n rows with m pupils in each. \n\nThe teacher asks pupils in the following order: at first, she asks all pupils from the first row in the order of their seating, then she continues to ask pupils from the next row. If the teacher asked the last row, then the direction of the poll changes, it means that she asks the previous row. The order of asking the rows looks as follows: the 1-st row, the 2-nd row, ..., the n - 1-st row, the n-th row, the n - 1-st row, ..., the 2-nd row, the 1-st row, the 2-nd row, ...\n\nThe order of asking of pupils on the same row is always the same: the 1-st pupil, the 2-nd pupil, ..., the m-th pupil.\n\nDuring the lesson the teacher managed to ask exactly k questions from pupils in order described above. Sergei seats on the x-th row, on the y-th place in the row. Sergei decided to prove to the teacher that pupils are asked irregularly, help him count three values: the maximum number of questions a particular pupil is asked, the minimum number of questions a particular pupil is asked, how many times the teacher asked Sergei. \n\nIf there is only one row in the class, then the teacher always asks children from this row.\n\n\n-----Input-----\n\nThe first and the only line contains five integers n, m, k, x and y (1 \u2264 n, m \u2264 100, 1 \u2264 k \u2264 10^18, 1 \u2264 x \u2264 n, 1 \u2264 y \u2264 m).\n\n\n-----Output-----\n\nPrint three integers: the maximum number of questions a particular pupil is asked, the minimum number of questions a particular pupil is asked, how many times the teacher asked Sergei. \n\n\n-----Examples-----\nInput\n1 3 8 1 1\n\nOutput\n3 2 3\nInput\n4 2 9 4 2\n\nOutput\n2 1 1\nInput\n5 5 25 4 3\n\nOutput\n1 1 1\nInput\n100 100 1000000000000000000 100 100\n\nOutput\n101010101010101 50505050505051 50505050505051\n\n\n-----Note-----\n\nThe order of asking pupils in the first test: the pupil from the first row who seats at the first table, it means it is Sergei; the pupil from the first row who seats at the second table; the pupil from the first row who seats at the third table; the pupil from the first row who seats at the first table, it means it is Sergei; the pupil from the first row who seats at the second table; the pupil from the first row who seats at the third table; the pupil from the first row who seats at the first table, it means it is Sergei; the pupil from the first row who seats at the second table; \n\nThe order of asking pupils in the second test: the pupil from the first row who seats at the first table; the pupil from the first row who seats at the second table; the pupil from the second row who seats at the first table; the pupil from the second row who seats at the second table; the pupil from the third row who seats at the first table; the pupil from the third row who seats at the second table; the pupil from the fourth row who seats at the first table; the pupil from the fourth row who seats at the second table, it means it is Sergei; the pupil from the third row who seats at the first table;"} +{"id": "00072", "text": "After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.\n\nThe three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.\n\nA random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of $7$ because its subribbon a appears $7$ times, and the ribbon abcdabc has the beauty of $2$ because its subribbon abc appears twice.\n\nThe rules are simple. The game will have $n$ turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after $n$ turns wins the treasure.\n\nCould you find out who is going to be the winner if they all play optimally?\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($0 \\leq n \\leq 10^{9}$)\u00a0\u2014 the number of turns.\n\nNext 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than $10^{5}$ uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.\n\n\n-----Output-----\n\nPrint the name of the winner (\"Kuro\", \"Shiro\" or \"Katie\"). If there are at least two cats that share the maximum beauty, print \"Draw\".\n\n\n-----Examples-----\nInput\n3\nKuroo\nShiro\nKatie\n\nOutput\nKuro\n\nInput\n7\ntreasurehunt\nthreefriends\nhiCodeforces\n\nOutput\nShiro\n\nInput\n1\nabcabc\ncbabac\nababca\n\nOutput\nKatie\n\nInput\n15\nfoPaErcvJ\nmZaxowpbt\nmkuOlaHRE\n\nOutput\nDraw\n\n\n\n-----Note-----\n\nIn the first example, after $3$ turns, Kuro can change his ribbon into ooooo, which has the beauty of $5$, while reaching such beauty for Shiro and Katie is impossible (both Shiro and Katie can reach the beauty of at most $4$, for example by changing Shiro's ribbon into SSiSS and changing Katie's ribbon into Kaaaa). Therefore, the winner is Kuro.\n\nIn the fourth example, since the length of each of the string is $9$ and the number of turn is $15$, everyone can change their ribbons in some way to reach the maximal beauty of $9$ by changing their strings into zzzzzzzzz after 9 turns, and repeatedly change their strings into azzzzzzzz and then into zzzzzzzzz thrice. Therefore, the game ends in a draw."} +{"id": "00073", "text": "Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages.\n\nAt first day Mister B read v_0 pages, but after that he started to speed up. Every day, starting from the second, he read a pages more than on the previous day (at first day he read v_0 pages, at second\u00a0\u2014 v_0 + a pages, at third\u00a0\u2014 v_0 + 2a pages, and so on). But Mister B is just a human, so he physically wasn't able to read more than v_1 pages per day.\n\nAlso, to refresh his memory, every day, starting from the second, Mister B had to reread last l pages he read on the previous day. Mister B finished the book when he read the last page for the first time.\n\nHelp Mister B to calculate how many days he needed to finish the book.\n\n\n-----Input-----\n\nFirst and only line contains five space-separated integers: c, v_0, v_1, a and l (1 \u2264 c \u2264 1000, 0 \u2264 l < v_0 \u2264 v_1 \u2264 1000, 0 \u2264 a \u2264 1000) \u2014 the length of the book in pages, the initial reading speed, the maximum reading speed, the acceleration in reading speed and the number of pages for rereading.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of days Mister B needed to finish the book.\n\n\n-----Examples-----\nInput\n5 5 10 5 4\n\nOutput\n1\n\nInput\n12 4 12 4 1\n\nOutput\n3\n\nInput\n15 1 100 0 0\n\nOutput\n15\n\n\n\n-----Note-----\n\nIn the first sample test the book contains 5 pages, so Mister B read it right at the first day.\n\nIn the second sample test at first day Mister B read pages number 1 - 4, at second day\u00a0\u2014 4 - 11, at third day\u00a0\u2014 11 - 12 and finished the book.\n\nIn third sample test every day Mister B read 1 page of the book, so he finished in 15 days."} +{"id": "00074", "text": "Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes.\n\nMore formally, you are given an odd numer n. Find a set of numbers p_{i} (1 \u2264 i \u2264 k), such that\n\n\n\n 1 \u2264 k \u2264 3\n\n p_{i} is a prime\n\n $\\sum_{i = 1}^{k} p_{i} = n$\n\nThe numbers p_{i} do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists.\n\n\n-----Input-----\n\nThe single line contains an odd number n (3 \u2264 n < 10^9).\n\n\n-----Output-----\n\nIn the first line print k (1 \u2264 k \u2264 3), showing how many numbers are in the representation you found.\n\nIn the second line print numbers p_{i} in any order. If there are multiple possible solutions, you can print any of them.\n\n\n-----Examples-----\nInput\n27\n\nOutput\n3\n5 11 11\n\n\n\n-----Note-----\n\nA prime is an integer strictly larger than one that is divisible only by one and by itself."} +{"id": "00075", "text": "You are given a description of a depot. It is a rectangular checkered field of n \u00d7 m size. Each cell in a field can be empty (\".\") or it can be occupied by a wall (\"*\"). \n\nYou have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the column y.\n\nYou are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall.\n\n\n-----Input-----\n\nThe first line contains two positive integers n and m (1 \u2264 n, m \u2264 1000)\u00a0\u2014 the number of rows and columns in the depot field. \n\nThe next n lines contain m symbols \".\" and \"*\" each\u00a0\u2014 the description of the field. j-th symbol in i-th of them stands for cell (i, j). If the symbol is equal to \".\", then the corresponding cell is empty, otherwise it equals \"*\" and the corresponding cell is occupied by a wall.\n\n\n-----Output-----\n\nIf it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print \"NO\" in the first line (without quotes).\n\nOtherwise print \"YES\" (without quotes) in the first line and two integers in the second line\u00a0\u2014 the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n3 4\n.*..\n....\n.*..\n\nOutput\nYES\n1 2\n\nInput\n3 3\n..*\n.*.\n*..\n\nOutput\nNO\n\nInput\n6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*..\n\nOutput\nYES\n3 3"} +{"id": "00076", "text": "Berland Football Cup starts really soon! Commentators from all over the world come to the event.\n\nOrganizers have already built $n$ commentary boxes. $m$ regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.\n\nIf $n$ is not divisible by $m$, it is impossible to distribute the boxes to the delegations at the moment.\n\nOrganizers can build a new commentary box paying $a$ burles and demolish a commentary box paying $b$ burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.\n\nWhat is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by $m$)?\n\n\n-----Input-----\n\nThe only line contains four integer numbers $n$, $m$, $a$ and $b$ ($1 \\le n, m \\le 10^{12}$, $1 \\le a, b \\le 100$), where $n$ is the initial number of the commentary boxes, $m$ is the number of delegations to come, $a$ is the fee to build a box and $b$ is the fee to demolish a box.\n\n\n-----Output-----\n\nOutput the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by $m$). It is allowed that the final number of the boxes is equal to $0$.\n\n\n-----Examples-----\nInput\n9 7 3 8\n\nOutput\n15\n\nInput\n2 7 3 7\n\nOutput\n14\n\nInput\n30 6 17 19\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example organizers can build $5$ boxes to make the total of $14$ paying $3$ burles for the each of them.\n\nIn the second example organizers can demolish $2$ boxes to make the total of $0$ paying $7$ burles for the each of them.\n\nIn the third example organizers are already able to distribute all the boxes equally among the delegations, each one get $5$ boxes."} +{"id": "00077", "text": "You are given sequence a_1, a_2, ..., a_{n} of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.\n\nSubsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.\n\nYou should write a program which finds sum of the best subsequence.\n\n\n-----Input-----\n\nThe first line contains integer number n (1 \u2264 n \u2264 10^5).\n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} ( - 10^4 \u2264 a_{i} \u2264 10^4). The sequence contains at least one subsequence with odd sum.\n\n\n-----Output-----\n\nPrint sum of resulting subseqeuence.\n\n\n-----Examples-----\nInput\n4\n-2 2 -3 1\n\nOutput\n3\n\nInput\n3\n2 -5 -3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example sum of the second and the fourth elements is 3."} +{"id": "00078", "text": "The only difference between easy and hard versions is constraints.\n\nPolycarp loves to listen to music, so he never leaves the player, even on the way home from the university. Polycarp overcomes the distance from the university to the house in exactly $T$ minutes.\n\nIn the player, Polycarp stores $n$ songs, each of which is characterized by two parameters: $t_i$ and $g_i$, where $t_i$ is the length of the song in minutes ($1 \\le t_i \\le 15$), $g_i$ is its genre ($1 \\le g_i \\le 3$).\n\nPolycarp wants to create such a playlist so that he can listen to music all the time on the way from the university to his home, and at the time of his arrival home, the playlist is over. Polycarp never interrupts songs and always listens to them from beginning to end. Thus, if he started listening to the $i$-th song, he would spend exactly $t_i$ minutes on its listening. Polycarp also does not like when two songs of the same genre play in a row (i.e. successively/adjacently) or when the songs in his playlist are repeated.\n\nHelp Polycarpus count the number of different sequences of songs (their order matters), the total duration is exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $T$ ($1 \\le n \\le 15, 1 \\le T \\le 225$) \u2014 the number of songs in the player and the required total duration, respectively.\n\nNext, the $n$ lines contain descriptions of songs: the $i$-th line contains two integers $t_i$ and $g_i$ ($1 \\le t_i \\le 15, 1 \\le g_i \\le 3$) \u2014 the duration of the $i$-th song and its genre, respectively.\n\n\n-----Output-----\n\nOutput one integer \u2014 the number of different sequences of songs, the total length of exactly $T$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. Since the answer may be huge, output it modulo $10^9 + 7$ (that is, the remainder when dividing the quantity by $10^9 + 7$).\n\n\n-----Examples-----\nInput\n3 3\n1 1\n1 2\n1 3\n\nOutput\n6\n\nInput\n3 3\n1 1\n1 1\n1 3\n\nOutput\n2\n\nInput\n4 10\n5 3\n2 1\n3 2\n5 1\n\nOutput\n10\n\n\n\n-----Note-----\n\nIn the first example, Polycarp can make any of the $6$ possible playlist by rearranging the available songs: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$ and $[3, 2, 1]$ (indices of the songs are given).\n\nIn the second example, the first and second songs cannot go in succession (since they have the same genre). Thus, Polycarp can create a playlist in one of $2$ possible ways: $[1, 3, 2]$ and $[2, 3, 1]$ (indices of the songs are given).\n\nIn the third example, Polycarp can make the following playlists: $[1, 2, 3]$, $[1, 3, 2]$, $[2, 1, 3]$, $[2, 3, 1]$, $[3, 1, 2]$, $[3, 2, 1]$, $[1, 4]$, $[4, 1]$, $[2, 3, 4]$ and $[4, 3, 2]$ (indices of the songs are given)."} +{"id": "00079", "text": "Vivek initially has an empty array $a$ and some integer constant $m$.\n\nHe performs the following algorithm: Select a random integer $x$ uniformly in range from $1$ to $m$ and append it to the end of $a$. Compute the greatest common divisor of integers in $a$. In case it equals to $1$, break Otherwise, return to step $1$. \n\nFind the expected length of $a$. It can be shown that it can be represented as $\\frac{P}{Q}$ where $P$ and $Q$ are coprime integers and $Q\\neq 0 \\pmod{10^9+7}$. Print the value of $P \\cdot Q^{-1} \\pmod{10^9+7}$.\n\n\n-----Input-----\n\nThe first and only line contains a single integer $m$ ($1 \\leq m \\leq 100000$).\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the expected length of the array $a$ written as $P \\cdot Q^{-1} \\pmod{10^9+7}$.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n2\n\nOutput\n2\n\nInput\n4\n\nOutput\n333333338\n\n\n\n-----Note-----\n\nIn the first example, since Vivek can choose only integers from $1$ to $1$, he will have $a=[1]$ after the first append operation, and after that quit the algorithm. Hence the length of $a$ is always $1$, so its expected value is $1$ as well.\n\nIn the second example, Vivek each time will append either $1$ or $2$, so after finishing the algorithm he will end up having some number of $2$'s (possibly zero), and a single $1$ in the end. The expected length of the list is $1\\cdot \\frac{1}{2} + 2\\cdot \\frac{1}{2^2} + 3\\cdot \\frac{1}{2^3} + \\ldots = 2$."} +{"id": "00080", "text": "Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.\n\nWe define a pair of integers (a, b) good, if GCD(a, b) = x and LCM(a, b) = y, where GCD(a, b) denotes the greatest common divisor of a and b, and LCM(a, b) denotes the least common multiple of a and b.\n\nYou are given two integers x and y. You are to find the number of good pairs of integers (a, b) such that l \u2264 a, b \u2264 r. Note that pairs (a, b) and (b, a) are considered different if a \u2260 b.\n\n\n-----Input-----\n\nThe only line contains four integers l, r, x, y (1 \u2264 l \u2264 r \u2264 10^9, 1 \u2264 x \u2264 y \u2264 10^9).\n\n\n-----Output-----\n\nIn the only line print the only integer\u00a0\u2014 the answer for the problem.\n\n\n-----Examples-----\nInput\n1 2 1 2\n\nOutput\n2\n\nInput\n1 12 1 12\n\nOutput\n4\n\nInput\n50 100 3 30\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example there are two suitable good pairs of integers (a, b): (1, 2) and (2, 1).\n\nIn the second example there are four suitable good pairs of integers (a, b): (1, 12), (12, 1), (3, 4) and (4, 3).\n\nIn the third example there are good pairs of integers, for example, (3, 30), but none of them fits the condition l \u2264 a, b \u2264 r."} +{"id": "00081", "text": "Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.\n\nNeko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one.\n\nGiven his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it?\n\n\n-----Input-----\n\nThe only line contains two integers $a$ and $b$ ($1 \\le a, b \\le 10^9$).\n\n\n-----Output-----\n\nPrint the smallest non-negative integer $k$ ($k \\ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible.\n\nIf there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one.\n\n\n-----Examples-----\nInput\n6 10\n\nOutput\n2\nInput\n21 31\n\nOutput\n9\nInput\n5 10\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible."} +{"id": "00082", "text": "Noora is a student of one famous high school. It's her final year in school\u00a0\u2014 she is going to study in university next year. However, she has to get an \u00abA\u00bb graduation certificate in order to apply to a prestigious one.\n\nIn school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784\u00a0\u2014 to 8. \n\nFor instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.\n\nTo graduate with \u00abA\u00bb certificate, Noora has to have mark k.\n\nNoora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 100, 1 \u2264 k \u2264 100) denoting the number of marks, received by Noora and the value of highest possible mark.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 k) denoting marks received by Noora before Leha's hack.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.\n\n\n-----Examples-----\nInput\n2 10\n8 9\n\nOutput\n4\nInput\n3 5\n4 4 4\n\nOutput\n3\n\n\n-----Note-----\n\nConsider the first example testcase.\n\nMaximal mark is 10, Noora received two marks\u00a0\u2014 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to $\\frac{8 + 9 + 10 + 10 + 10 + 10}{6} = \\frac{57}{6} = 9.5$. Consequently, new final mark is 10. Less number of marks won't fix the situation.\n\nIn the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate."} +{"id": "00083", "text": "You are given an array of $n$ integers: $a_1, a_2, \\ldots, a_n$. Your task is to find some non-zero integer $d$ ($-10^3 \\leq d \\leq 10^3$) such that, after each number in the array is divided by $d$, the number of positive numbers that are presented in the array is greater than or equal to half of the array size (i.e., at least $\\lceil\\frac{n}{2}\\rceil$). Note that those positive numbers do not need to be an integer (e.g., a $2.5$ counts as a positive number). If there are multiple values of $d$ that satisfy the condition, you may print any of them. In case that there is no such $d$, print a single integer $0$.\n\nRecall that $\\lceil x \\rceil$ represents the smallest integer that is not less than $x$ and that zero ($0$) is neither positive nor negative.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 100$)\u00a0\u2014 the number of elements in the array.\n\nThe second line contains $n$ space-separated integers $a_1, a_2, \\ldots, a_n$ ($-10^3 \\le a_i \\le 10^3$).\n\n\n-----Output-----\n\nPrint one integer $d$ ($-10^3 \\leq d \\leq 10^3$ and $d \\neq 0$) that satisfies the given condition. If there are multiple values of $d$ that satisfy the condition, you may print any of them. In case that there is no such $d$, print a single integer $0$.\n\n\n-----Examples-----\nInput\n5\n10 0 -7 2 6\nOutput\n4\nInput\n7\n0 0 1 -1 0 0 2\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first sample, $n = 5$, so we need at least $\\lceil\\frac{5}{2}\\rceil = 3$ positive numbers after division. If $d = 4$, the array after division is $[2.5, 0, -1.75, 0.5, 1.5]$, in which there are $3$ positive numbers (namely: $2.5$, $0.5$, and $1.5$).\n\nIn the second sample, there is no valid $d$, so $0$ should be printed."} +{"id": "00084", "text": "There are n shovels in Polycarp's shop. The i-th shovel costs i burles, that is, the first shovel costs 1 burle, the second shovel costs 2 burles, the third shovel costs 3 burles, and so on. Polycarps wants to sell shovels in pairs.\n\nVisitors are more likely to buy a pair of shovels if their total cost ends with several 9s. Because of this, Polycarp wants to choose a pair of shovels to sell in such a way that the sum of their costs ends with maximum possible number of nines. For example, if he chooses shovels with costs 12345 and 37454, their total cost is 49799, it ends with two nines.\n\nYou are to compute the number of pairs of shovels such that their total cost ends with maximum possible number of nines. Two pairs are considered different if there is a shovel presented in one pair, but not in the other.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 10^9) \u2014 the number of shovels in Polycarp's shop.\n\n\n-----Output-----\n\nPrint the number of pairs of shovels such that their total cost ends with maximum possible number of nines. \n\nNote that it is possible that the largest number of 9s at the end is 0, then you should count all such ways.\n\nIt is guaranteed that for every n \u2264 10^9 the answer doesn't exceed 2\u00b710^9.\n\n\n-----Examples-----\nInput\n7\n\nOutput\n3\n\nInput\n14\n\nOutput\n9\n\nInput\n50\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example the maximum possible number of nines at the end is one. Polycarp cah choose the following pairs of shovels for that purpose: 2 and 7; 3 and 6; 4 and 5. \n\nIn the second example the maximum number of nines at the end of total cost of two shovels is one. The following pairs of shovels suit Polycarp: 1 and 8; 2 and 7; 3 and 6; 4 and 5; 5 and 14; 6 and 13; 7 and 12; 8 and 11; 9 and 10. \n\nIn the third example it is necessary to choose shovels 49 and 50, because the sum of their cost is 99, that means that the total number of nines is equal to two, which is maximum possible for n = 50."} +{"id": "00085", "text": "Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is a_1 \u00d7 b_1 segments large and the second one is a_2 \u00d7 b_2 segments large.\n\nPolycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus's mind and Paraskevi's beauty are equally matched, so the two bars must have the same number of squares.\n\nTo make the bars have the same number of squares, Polycarpus eats a little piece of chocolate each minute. Each minute he does the following: he either breaks one bar exactly in half (vertically or horizontally) and eats exactly a half of the bar, or he chips of exactly one third of a bar (vertically or horizontally) and eats exactly a third of the bar. \n\nIn the first case he is left with a half, of the bar and in the second case he is left with two thirds of the bar.\n\nBoth variants aren't always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16 \u00d7 23, then Polycarpus can chip off a half, but not a third. If the bar is 20 \u00d7 18, then Polycarpus can chip off both a half and a third. If the bar is 5 \u00d7 7, then Polycarpus cannot chip off a half nor a third.\n\nWhat is the minimum number of minutes Polycarpus needs to make two bars consist of the same number of squares? Find not only the required minimum number of minutes, but also the possible sizes of the bars after the process.\n\n\n-----Input-----\n\nThe first line of the input contains integers a_1, b_1 (1 \u2264 a_1, b_1 \u2264 10^9) \u2014 the initial sizes of the first chocolate bar. The second line of the input contains integers a_2, b_2 (1 \u2264 a_2, b_2 \u2264 10^9) \u2014 the initial sizes of the second bar.\n\nYou can use the data of type int64 (in Pascal), long long (in \u0421++), long (in Java) to process large integers (exceeding 2^31 - 1).\n\n\n-----Output-----\n\nIn the first line print m \u2014 the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in m minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line must correspond to the first bar and the third line must correspond to the second bar. If there are multiple solutions, print any of them.\n\nIf there is no solution, print a single line with integer -1.\n\n\n-----Examples-----\nInput\n2 6\n2 3\n\nOutput\n1\n1 6\n2 3\n\nInput\n36 5\n10 16\n\nOutput\n3\n16 5\n5 16\n\nInput\n3 5\n2 1\n\nOutput\n-1"} +{"id": "00086", "text": "Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. \n\nThere are some additional restrictions \u2014 a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). \n\nYou are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well.\n\n\n-----Input-----\n\nThe first line contains four integers: x_{p}, y_{p}, x_{v}, y_{v} (0 \u2264 x_{p}, y_{p}, x_{v}, y_{v} \u2264 10^5) \u2014 Polycarp's and Vasiliy's starting coordinates.\n\nIt is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0).\n\n\n-----Output-----\n\nOutput the name of the winner: \"Polycarp\" or \"Vasiliy\".\n\n\n-----Examples-----\nInput\n2 1 2 2\n\nOutput\nPolycarp\n\nInput\n4 7 7 4\n\nOutput\nVasiliy\n\n\n\n-----Note-----\n\nIn the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn."} +{"id": "00087", "text": "Petr wants to make a calendar for current month. For this purpose he draws a table in which columns correspond to weeks (a week is seven consequent days from Monday to Sunday), rows correspond to weekdays, and cells contain dates. For example, a calendar for January 2017 should look like on the picture: $\\left. \\begin{array}{|r|r|r|r|r|r|} \\hline & {2} & {9} & {16} & {23} & {30} \\\\ \\hline & {3} & {10} & {17} & {24} & {31} \\\\ \\hline & {4} & {11} & {18} & {25} & {} \\\\ \\hline & {5} & {12} & {19} & {26} & {} \\\\ \\hline & {6} & {13} & {20} & {27} & {} \\\\ \\hline & {7} & {14} & {21} & {28} & {} \\\\ \\hline 1 & {8} & {15} & {22} & {29} & {} \\\\ \\hline \\end{array} \\right.$ \n\nPetr wants to know how many columns his table should have given the month and the weekday of the first date of that month? Assume that the year is non-leap.\n\n\n-----Input-----\n\nThe only line contain two integers m and d (1 \u2264 m \u2264 12, 1 \u2264 d \u2264 7)\u00a0\u2014 the number of month (January is the first month, December is the twelfth) and the weekday of the first date of this month (1 is Monday, 7 is Sunday).\n\n\n-----Output-----\n\nPrint single integer: the number of columns the table should have.\n\n\n-----Examples-----\nInput\n1 7\n\nOutput\n6\n\nInput\n1 1\n\nOutput\n5\n\nInput\n11 6\n\nOutput\n5\n\n\n\n-----Note-----\n\nThe first example corresponds to the January 2017 shown on the picture in the statements.\n\nIn the second example 1-st January is Monday, so the whole month fits into 5 columns.\n\nIn the third example 1-st November is Saturday and 5 columns is enough."} +{"id": "00088", "text": "The year 2015 is almost over.\n\nLimak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system\u00a0\u2014 2015_10 = 11111011111_2. Note that he doesn't care about the number of zeros in the decimal representation.\n\nLimak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?\n\nAssume that all positive integers are always written without leading zeros.\n\n\n-----Input-----\n\nThe only line of the input contains two integers a and b (1 \u2264 a \u2264 b \u2264 10^18)\u00a0\u2014 the first year and the last year in Limak's interval respectively.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2013 the number of years Limak will count in his chosen interval.\n\n\n-----Examples-----\nInput\n5 10\n\nOutput\n2\n\nInput\n2015 2015\n\nOutput\n1\n\nInput\n100 105\n\nOutput\n0\n\nInput\n72057594000000000 72057595000000000\n\nOutput\n26\n\n\n\n-----Note-----\n\nIn the first sample Limak's interval contains numbers 5_10 = 101_2, 6_10 = 110_2, 7_10 = 111_2, 8_10 = 1000_2, 9_10 = 1001_2 and 10_10 = 1010_2. Two of them (101_2 and 110_2) have the described property."} +{"id": "00089", "text": "You are given an integer N. Consider all possible segments on the coordinate axis with endpoints at integer points with coordinates between 0 and N, inclusive; there will be $\\frac{n(n + 1)}{2}$ of them.\n\nYou want to draw these segments in several layers so that in each layer the segments don't overlap (they might touch at the endpoints though). You can not move the segments to a different location on the coordinate axis. \n\nFind the minimal number of layers you have to use for the given N.\n\n\n-----Input-----\n\nThe only input line contains a single integer N (1 \u2264 N \u2264 100).\n\n\n-----Output-----\n\nOutput a single integer - the minimal number of layers required to draw the segments for the given N.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n2\n\nInput\n3\n\nOutput\n4\n\nInput\n4\n\nOutput\n6\n\n\n\n-----Note-----\n\nAs an example, here are the segments and their optimal arrangement into layers for N = 4. [Image]"} +{"id": "00090", "text": "Anya loves to fold and stick. Today she decided to do just that.\n\nAnya has n cubes lying in a line and numbered from 1 to n from left to right, with natural numbers written on them. She also has k stickers with exclamation marks. We know that the number of stickers does not exceed the number of cubes.\n\nAnya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120.\n\nYou need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most k exclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to S. Anya can stick at most one exclamation mark on each cube. Can you do it?\n\nTwo ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.\n\n\n-----Input-----\n\nThe first line of the input contains three space-separated integers n, k and S (1 \u2264 n \u2264 25, 0 \u2264 k \u2264 n, 1 \u2264 S \u2264 10^16)\u00a0\u2014\u00a0the number of cubes and the number of stickers that Anya has, and the sum that she needs to get. \n\nThe second line contains n positive integers a_{i} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014\u00a0the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one. \n\nMultiple cubes can contain the same numbers.\n\n\n-----Output-----\n\nOutput the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number S.\n\n\n-----Examples-----\nInput\n2 2 30\n4 3\n\nOutput\n1\n\nInput\n2 2 7\n4 3\n\nOutput\n1\n\nInput\n3 1 1\n1 1 1\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first sample the only way is to choose both cubes and stick an exclamation mark on each of them.\n\nIn the second sample the only way is to choose both cubes but don't stick an exclamation mark on any of them.\n\nIn the third sample it is possible to choose any of the cubes in three ways, and also we may choose to stick or not to stick the exclamation mark on it. So, the total number of ways is six."} +{"id": "00091", "text": "Suppose you are performing the following algorithm. There is an array $v_1, v_2, \\dots, v_n$ filled with zeroes at start. The following operation is applied to the array several times \u2014 at $i$-th step ($0$-indexed) you can: either choose position $pos$ ($1 \\le pos \\le n$) and increase $v_{pos}$ by $k^i$; or not choose any position and skip this step. \n\nYou can choose how the algorithm would behave on each step and when to stop it. The question is: can you make array $v$ equal to the given array $a$ ($v_j = a_j$ for each $j$) after some step?\n\n\n-----Input-----\n\nThe first line contains one integer $T$ ($1 \\le T \\le 1000$) \u2014 the number of test cases. Next $2T$ lines contain test cases \u2014 two lines per test case.\n\nThe first line of each test case contains two integers $n$ and $k$ ($1 \\le n \\le 30$, $2 \\le k \\le 100$) \u2014 the size of arrays $v$ and $a$ and value $k$ used in the algorithm.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 10^{16}$) \u2014 the array you'd like to achieve.\n\n\n-----Output-----\n\nFor each test case print YES (case insensitive) if you can achieve the array $a$ after some step or NO (case insensitive) otherwise.\n\n\n-----Example-----\nInput\n5\n4 100\n0 0 0 0\n1 2\n1\n3 4\n1 4 1\n3 2\n0 1 3\n3 9\n0 59049 810\n\nOutput\nYES\nYES\nNO\nNO\nYES\n\n\n\n-----Note-----\n\nIn the first test case, you can stop the algorithm before the $0$-th step, or don't choose any position several times and stop the algorithm.\n\nIn the second test case, you can add $k^0$ to $v_1$ and stop the algorithm.\n\nIn the third test case, you can't make two $1$ in the array $v$.\n\nIn the fifth test case, you can skip $9^0$ and $9^1$, then add $9^2$ and $9^3$ to $v_3$, skip $9^4$ and finally, add $9^5$ to $v_2$."} +{"id": "00092", "text": "Let's denote d(n) as the number of divisors of a positive integer n. You are given three integers a, b and c. Your task is to calculate the following sum:\n\n$\\sum_{i = 1}^{a} \\sum_{j = 1}^{b} \\sum_{k = 1}^{c} d(i \\cdot j \\cdot k)$\n\nFind the sum modulo 1073741824 (2^30).\n\n\n-----Input-----\n\nThe first line contains three space-separated integers a, b and c (1 \u2264 a, b, c \u2264 100).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the required sum modulo 1073741824 (2^30).\n\n\n-----Examples-----\nInput\n2 2 2\n\nOutput\n20\n\nInput\n5 6 7\n\nOutput\n1520\n\n\n\n-----Note-----\n\nFor the first example.\n\n d(1\u00b71\u00b71) = d(1) = 1; d(1\u00b71\u00b72) = d(2) = 2; d(1\u00b72\u00b71) = d(2) = 2; d(1\u00b72\u00b72) = d(4) = 3; d(2\u00b71\u00b71) = d(2) = 2; d(2\u00b71\u00b72) = d(4) = 3; d(2\u00b72\u00b71) = d(4) = 3; d(2\u00b72\u00b72) = d(8) = 4. \n\nSo the result is 1 + 2 + 2 + 3 + 2 + 3 + 3 + 4 = 20."} +{"id": "00093", "text": "Bessie the cow and her best friend Elsie each received a sliding puzzle on Pi Day. Their puzzles consist of a 2 \u00d7 2 grid and three tiles labeled 'A', 'B', and 'C'. The three tiles sit on top of the grid, leaving one grid cell empty. To make a move, Bessie or Elsie can slide a tile adjacent to the empty cell into the empty cell as shown below: $\\rightarrow$ \n\nIn order to determine if they are truly Best Friends For Life (BFFLs), Bessie and Elsie would like to know if there exists a sequence of moves that takes their puzzles to the same configuration (moves can be performed in both puzzles). Two puzzles are considered to be in the same configuration if each tile is on top of the same grid cell in both puzzles. Since the tiles are labeled with letters, rotations and reflections are not allowed.\n\n\n-----Input-----\n\nThe first two lines of the input consist of a 2 \u00d7 2 grid describing the initial configuration of Bessie's puzzle. The next two lines contain a 2 \u00d7 2 grid describing the initial configuration of Elsie's puzzle. The positions of the tiles are labeled 'A', 'B', and 'C', while the empty cell is labeled 'X'. It's guaranteed that both puzzles contain exactly one tile with each letter and exactly one empty position.\n\n\n-----Output-----\n\nOutput \"YES\"(without quotes) if the puzzles can reach the same configuration (and Bessie and Elsie are truly BFFLs). Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\nAB\nXC\nXB\nAC\n\nOutput\nYES\n\nInput\nAB\nXC\nAC\nBX\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe solution to the first sample is described by the image. All Bessie needs to do is slide her 'A' tile down.\n\nIn the second sample, the two puzzles can never be in the same configuration. Perhaps Bessie and Elsie are not meant to be friends after all..."} +{"id": "00094", "text": "Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1\u00b716^2 + 13\u00b716^1 + 11\u00b716^0). Alexander lived calmly until he tried to convert the number back to the decimal number system.\n\nAlexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k.\n\n\n-----Input-----\n\nThe first line contains the integer n (2 \u2264 n \u2264 10^9). The second line contains the integer k (0 \u2264 k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n.\n\nAlexander guarantees that the answer exists and does not exceed 10^18.\n\nThe number k doesn't contain leading zeros.\n\n\n-----Output-----\n\nPrint the number x (0 \u2264 x \u2264 10^18)\u00a0\u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n13\n12\n\nOutput\n12\nInput\n16\n11311\n\nOutput\n475\nInput\n20\n999\n\nOutput\n3789\nInput\n17\n2016\n\nOutput\n594\n\n\n-----Note-----\n\nIn the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12\u00b713^0 or 15 = 1\u00b713^1 + 2\u00b713^0."} +{"id": "00095", "text": "Array of integers is unimodal, if:\n\n it is strictly increasing in the beginning; after that it is constant; after that it is strictly decreasing. \n\nThe first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent.\n\nFor example, the following three arrays are unimodal: [5, 7, 11, 11, 2, 1], [4, 4, 2], [7], but the following three are not unimodal: [5, 5, 6, 6, 1], [1, 2, 1, 2], [4, 5, 5, 6].\n\nWrite a program that checks if an array is unimodal.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100) \u2014 the number of elements in the array.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1 000) \u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint \"YES\" if the given array is unimodal. Otherwise, print \"NO\".\n\nYou can output each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n6\n1 5 5 5 4 2\n\nOutput\nYES\n\nInput\n5\n10 20 30 20 10\n\nOutput\nYES\n\nInput\n4\n1 2 1 2\n\nOutput\nNO\n\nInput\n7\n3 3 3 3 3 3 3\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example the array is unimodal, because it is strictly increasing in the beginning (from position 1 to position 2, inclusively), that it is constant (from position 2 to position 4, inclusively) and then it is strictly decreasing (from position 4 to position 6, inclusively)."} +{"id": "00096", "text": "At first, let's define function $f(x)$ as follows: $$ \\begin{matrix} f(x) & = & \\left\\{ \\begin{matrix} \\frac{x}{2} & \\mbox{if } x \\text{ is even} \\\\ x - 1 & \\mbox{otherwise } \\end{matrix} \\right. \\end{matrix} $$\n\nWe can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$.\n\nLet's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$?\n\nFormally speaking, you need to find maximum $y$ such that $\\left| \\{ x ~|~ 1 \\le x \\le n, y \\in path(x) \\} \\right| \\ge k$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 10^{18}$).\n\n\n-----Output-----\n\nPrint the only integer \u2014 the maximum value that is contained in at least $k$ paths.\n\n\n-----Examples-----\nInput\n11 3\n\nOutput\n5\n\nInput\n11 6\n\nOutput\n4\n\nInput\n20 20\n\nOutput\n1\n\nInput\n14 5\n\nOutput\n6\n\nInput\n1000000 100\n\nOutput\n31248\n\n\n\n-----Note-----\n\nIn the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$.\n\nIn the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$.\n\nIn the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$."} +{"id": "00097", "text": "Consider a billiard table of rectangular size $n \\times m$ with four pockets. Let's introduce a coordinate system with the origin at the lower left corner (see the picture). [Image] \n\nThere is one ball at the point $(x, y)$ currently. Max comes to the table and strikes the ball. The ball starts moving along a line that is parallel to one of the axes or that makes a $45^{\\circ}$ angle with them. We will assume that: the angles between the directions of the ball before and after a collision with a side are equal, the ball moves indefinitely long, it only stops when it falls into a pocket, the ball can be considered as a point, it falls into a pocket if and only if its coordinates coincide with one of the pockets, initially the ball is not in a pocket. \n\nNote that the ball can move along some side, in this case the ball will just fall into the pocket at the end of the side.\n\nYour task is to determine whether the ball will fall into a pocket eventually, and if yes, which of the four pockets it will be.\n\n\n-----Input-----\n\nThe only line contains $6$ integers $n$, $m$, $x$, $y$, $v_x$, $v_y$ ($1 \\leq n, m \\leq 10^9$, $0 \\leq x \\leq n$; $0 \\leq y \\leq m$; $-1 \\leq v_x, v_y \\leq 1$; $(v_x, v_y) \\neq (0, 0)$)\u00a0\u2014 the width of the table, the length of the table, the $x$-coordinate of the initial position of the ball, the $y$-coordinate of the initial position of the ball, the $x$-component of its initial speed and the $y$-component of its initial speed, respectively. It is guaranteed that the ball is not initially in a pocket.\n\n\n-----Output-----\n\nPrint the coordinates of the pocket the ball will fall into, or $-1$ if the ball will move indefinitely.\n\n\n-----Examples-----\nInput\n4 3 2 2 -1 1\n\nOutput\n0 0\nInput\n4 4 2 0 1 1\n\nOutput\n-1\nInput\n10 10 10 1 -1 0\n\nOutput\n-1\n\n\n-----Note-----\n\nThe first sample: [Image] \n\nThe second sample: [Image] \n\nIn the third sample the ball will never change its $y$ coordinate, so the ball will never fall into a pocket."} +{"id": "00098", "text": "Gerald bought two very rare paintings at the Sotheby's auction and he now wants to hang them on the wall. For that he bought a special board to attach it to the wall and place the paintings on the board. The board has shape of an a_1 \u00d7 b_1 rectangle, the paintings have shape of a a_2 \u00d7 b_2 and a_3 \u00d7 b_3 rectangles.\n\nSince the paintings are painted in the style of abstract art, it does not matter exactly how they will be rotated, but still, one side of both the board, and each of the paintings must be parallel to the floor. The paintings can touch each other and the edges of the board, but can not overlap or go beyond the edge of the board. Gerald asks whether it is possible to place the paintings on the board, or is the board he bought not large enough?\n\n\n-----Input-----\n\nThe first line contains two space-separated numbers a_1 and b_1 \u2014 the sides of the board. Next two lines contain numbers a_2, b_2, a_3 and b_3 \u2014 the sides of the paintings. All numbers a_{i}, b_{i} in the input are integers and fit into the range from 1 to 1000.\n\n\n-----Output-----\n\nIf the paintings can be placed on the wall, print \"YES\" (without the quotes), and if they cannot, print \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n3 2\n1 3\n2 1\n\nOutput\nYES\n\nInput\n5 5\n3 3\n3 3\n\nOutput\nNO\n\nInput\n4 2\n2 3\n1 2\n\nOutput\nYES\n\n\n\n-----Note-----\n\nThat's how we can place the pictures in the first test:\n\n[Image]\n\nAnd that's how we can do it in the third one.\n\n[Image]"} +{"id": "00099", "text": "Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.\n\nYou are given geometric progression b defined by two integers b_1 and q. Remind that a geometric progression is a sequence of integers b_1, b_2, b_3, ..., where for each i > 1 the respective term satisfies the condition b_{i} = b_{i} - 1\u00b7q, where q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b_1 and q can equal 0. Also, Dvastan gave Masha m \"bad\" integers a_1, a_2, ..., a_{m}, and an integer l.\n\nMasha writes all progression terms one by one onto the board (including repetitive) while condition |b_{i}| \u2264 l is satisfied (|x| means absolute value of x). There is an exception: if a term equals one of the \"bad\" integers, Masha skips it (doesn't write onto the board) and moves forward to the next term.\n\nBut the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print \"inf\" in case she needs to write infinitely many integers.\n\n\n-----Input-----\n\nThe first line of input contains four integers b_1, q, l, m (-10^9 \u2264 b_1, q \u2264 10^9, 1 \u2264 l \u2264 10^9, 1 \u2264 m \u2264 10^5)\u00a0\u2014 the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of \"bad\" integers, respectively.\n\nThe second line contains m distinct integers a_1, a_2, ..., a_{m} (-10^9 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 numbers that will never be written on the board.\n\n\n-----Output-----\n\nPrint the only integer, meaning the number of progression terms that will be written on the board if it is finite, or \"inf\" (without quotes) otherwise.\n\n\n-----Examples-----\nInput\n3 2 30 4\n6 14 25 48\n\nOutput\n3\nInput\n123 1 2143435 4\n123 11 -5453 141245\n\nOutput\n0\nInput\n123 1 2143435 4\n54343 -13 6 124\n\nOutput\ninf\n\n\n-----Note-----\n\nIn the first sample case, Masha will write integers 3, 12, 24. Progression term 6 will be skipped because it is a \"bad\" integer. Terms bigger than 24 won't be written because they exceed l by absolute value.\n\nIn the second case, Masha won't write any number because all terms are equal 123 and this is a \"bad\" integer.\n\nIn the third case, Masha will write infinitely integers 123."} +{"id": "00100", "text": "Innocentius has a problem \u2014 his computer monitor has broken. Now some of the pixels are \"dead\", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus.\n\nInnocentius is touch-typing a program that paints a white square one-pixel wide frame on the black screen. As the monitor is broken, some pixels that should be white remain black. Polycarpus should look at what the program displayed on the screen and guess the position and size of the frame Innocentius has painted. Polycarpus doesn't like the game but Innocentius persuaded brother to play as \"the game is good for the imagination and attention\".\n\nHelp Polycarpus, automatize his part in the gaming process. Write the code that finds such possible square frame that: the frame's width is 1 pixel, the frame doesn't go beyond the borders of the screen, all white pixels of the monitor are located on the frame, of all frames that satisfy the previous three conditions, the required frame must have the smallest size. \n\nFormally, a square frame is represented by such pixels of the solid square, that are on the square's border, that is, are not fully surrounded by the other pixels of the square. For example, if the frame's size is d = 3, then it consists of 8 pixels, if its size is d = 2, then it contains 4 pixels and if d = 1, then the frame is reduced to a single pixel.\n\n\n-----Input-----\n\nThe first line contains the resolution of the monitor as a pair of integers n, m (1 \u2264 n, m \u2264 2000). The next n lines contain exactly m characters each \u2014 the state of the monitor pixels at the moment of the game. Character \".\" (period, ASCII code 46) corresponds to the black pixel, and character \"w\" (lowercase English letter w) corresponds to the white pixel. It is guaranteed that at least one pixel of the monitor is white.\n\n\n-----Output-----\n\nPrint the monitor screen. Represent the sought frame by characters \"+\" (the \"plus\" character). The pixels that has become white during the game mustn't be changed. Print them as \"w\". If there are multiple possible ways to position the frame of the minimum size, print any of them.\n\nIf the required frame doesn't exist, then print a single line containing number -1.\n\n\n-----Examples-----\nInput\n4 8\n..w..w..\n........\n........\n..w..w..\n\nOutput\n..w++w..\n..+..+..\n..+..+..\n..w++w..\n\nInput\n5 6\n......\n.w....\n......\n..w...\n......\n\nOutput\n......\n+w+...\n+.+...\n++w...\n......\n\nInput\n2 4\n....\n.w..\n\nOutput\n....\n.w..\n\nInput\n2 6\nw..w.w\n...w..\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample the required size of the optimal frame equals 4. In the second sample the size of the optimal frame equals 3. In the third sample, the size of the optimal frame is 1. In the fourth sample, the required frame doesn't exist."} +{"id": "00101", "text": "Vasya has n burles. One bottle of Ber-Cola costs a burles and one Bars bar costs b burles. He can buy any non-negative integer number of bottles of Ber-Cola and any non-negative integer number of Bars bars.\n\nFind out if it's possible to buy some amount of bottles of Ber-Cola and Bars bars and spend exactly n burles.\n\nIn other words, you should find two non-negative integers x and y such that Vasya can buy x bottles of Ber-Cola and y Bars bars and x\u00b7a + y\u00b7b = n or tell that it's impossible.\n\n\n-----Input-----\n\nFirst line contains single integer n (1 \u2264 n \u2264 10 000 000)\u00a0\u2014 amount of money, that Vasya has.\n\nSecond line contains single integer a (1 \u2264 a \u2264 10 000 000)\u00a0\u2014 cost of one bottle of Ber-Cola.\n\nThird line contains single integer b (1 \u2264 b \u2264 10 000 000)\u00a0\u2014 cost of one Bars bar.\n\n\n-----Output-----\n\nIf Vasya can't buy Bars and Ber-Cola in such a way to spend exactly n burles print \u00abNO\u00bb (without quotes).\n\nOtherwise in first line print \u00abYES\u00bb (without quotes). In second line print two non-negative integers x and y\u00a0\u2014 number of bottles of Ber-Cola and number of Bars bars Vasya should buy in order to spend exactly n burles, i.e. x\u00b7a + y\u00b7b = n. If there are multiple answers print any of them.\n\nAny of numbers x and y can be equal 0.\n\n\n-----Examples-----\nInput\n7\n2\n3\n\nOutput\nYES\n2 1\n\nInput\n100\n25\n10\n\nOutput\nYES\n0 10\n\nInput\n15\n4\n8\n\nOutput\nNO\n\nInput\n9960594\n2551\n2557\n\nOutput\nYES\n1951 1949\n\n\n\n-----Note-----\n\nIn first example Vasya can buy two bottles of Ber-Cola and one Bars bar. He will spend exactly 2\u00b72 + 1\u00b73 = 7 burles.\n\nIn second example Vasya can spend exactly n burles multiple ways: buy two bottles of Ber-Cola and five Bars bars; buy four bottles of Ber-Cola and don't buy Bars bars; don't buy Ber-Cola and buy 10 Bars bars. \n\nIn third example it's impossible to but Ber-Cola and Bars bars in order to spend exactly n burles."} +{"id": "00102", "text": "Today Tavas got his test result as an integer score and he wants to share it with his girlfriend, Nafas.\n\nHis phone operating system is Tavdroid, and its keyboard doesn't have any digits! He wants to share his score with Nafas via text, so he has no choice but to send this number using words. [Image] \n\nHe ate coffee mix without water again, so right now he's really messed up and can't think.\n\nYour task is to help him by telling him what to type.\n\n\n-----Input-----\n\nThe first and only line of input contains an integer s (0 \u2264 s \u2264 99), Tavas's score. \n\n\n-----Output-----\n\nIn the first and only line of output, print a single string consisting only from English lowercase letters and hyphens ('-'). Do not use spaces.\n\n\n-----Examples-----\nInput\n6\n\nOutput\nsix\n\nInput\n99\n\nOutput\nninety-nine\n\nInput\n20\n\nOutput\ntwenty\n\n\n\n-----Note-----\n\nYou can find all you need to know about English numerals in http://en.wikipedia.org/wiki/English_numerals ."} +{"id": "00103", "text": "JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array $a_1$, $a_2$, ..., $a_n$ of integers, such that $1 \\le a_1 < a_2 < \\ldots < a_n \\le 10^3$, and then went to the bathroom.\n\nJATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range $[1, 10^3]$.\n\nJATC wonders what is the greatest number of elements he can erase?\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($1 \\le n \\le 100$)\u00a0\u2014 the number of elements in the array.\n\nThe second line of the input contains $n$ integers $a_i$ ($1 \\le a_1 p_{j}.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 n, k \u2264 100 000)\u00a0\u2014 the number of cows and the length of Farmer John's nap, respectively.\n\n\n-----Output-----\n\nOutput a single integer, the maximum messiness that the Mischievous Mess Makers can achieve by performing no more than k swaps. \n\n\n-----Examples-----\nInput\n5 2\n\nOutput\n10\n\nInput\n1 10\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, the Mischievous Mess Makers can swap the cows in the stalls 1 and 5 during the first minute, then the cows in stalls 2 and 4 during the second minute. This reverses the arrangement of cows, giving us a total messiness of 10.\n\nIn the second sample, there is only one cow, so the maximum possible messiness is 0."} +{"id": "00129", "text": "Ivan is collecting coins. There are only $N$ different collectible coins, Ivan has $K$ of them. He will be celebrating his birthday soon, so all his $M$ freinds decided to gift him coins. They all agreed to three terms: Everyone must gift as many coins as others. All coins given to Ivan must be different. Not less than $L$ coins from gifts altogether, must be new in Ivan's collection.\n\nBut his friends don't know which coins have Ivan already got in his collection. They don't want to spend money so they want to buy minimum quantity of coins, that satisfy all terms, irrespective of the Ivan's collection. Help them to find this minimum number of coins or define it's not possible to meet all the terms.\n\n\n-----Input-----\n\nThe only line of input contains 4 integers $N$, $M$, $K$, $L$ ($1 \\le K \\le N \\le 10^{18}$; $1 \\le M, \\,\\, L \\le 10^{18}$)\u00a0\u2014 quantity of different coins, number of Ivan's friends, size of Ivan's collection and quantity of coins, that must be new in Ivan's collection.\n\n\n-----Output-----\n\nPrint one number\u00a0\u2014 minimal number of coins one friend can gift to satisfy all the conditions. If it is impossible to satisfy all three conditions print \"-1\" (without quotes).\n\n\n-----Examples-----\nInput\n20 15 2 3\n\nOutput\n1\nInput\n10 11 2 4\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first test, one coin from each friend is enough, as he will be presented with 15 different coins and 13 of them will definitely be new.\n\nIn the second test, Ivan has 11 friends, but there are only 10 different coins. So all friends can't present him different coins."} +{"id": "00130", "text": "Polycarp has a checkered sheet of paper of size n \u00d7 m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's \"Black Square\", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.\n\nYou are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 100) \u2014 the sizes of the sheet.\n\nThe next n lines contain m letters 'B' or 'W' each \u2014 the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.\n\n\n-----Output-----\n\nPrint the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.\n\n\n-----Examples-----\nInput\n5 4\nWWWW\nWWWB\nWWWB\nWWBB\nWWWW\n\nOutput\n5\n\nInput\n1 2\nBB\n\nOutput\n-1\n\nInput\n3 3\nWWW\nWWW\nWWW\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example it is needed to paint 5 cells \u2014 (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).\n\nIn the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.\n\nIn the third example all cells are colored white, so it's sufficient to color any cell black."} +{"id": "00131", "text": "There is a beautiful garden of stones in Innopolis.\n\nIts most beautiful place is the $n$ piles with stones numbered from $1$ to $n$.\n\nEJOI participants have visited this place twice. \n\nWhen they first visited it, the number of stones in piles was $x_1, x_2, \\ldots, x_n$, correspondingly. One of the participants wrote down this sequence in a notebook. \n\nThey visited it again the following day, and the number of stones in piles was equal to $y_1, y_2, \\ldots, y_n$. One of the participants also wrote it down in a notebook.\n\nIt is well known that every member of the EJOI jury during the night either sits in the room $108$ or comes to the place with stones. Each jury member who comes there either takes one stone for himself or moves one stone from one pile to another. We can assume that there is an unlimited number of jury members. No one except the jury goes to the place with stones at night.\n\nParticipants want to know whether their notes can be correct or they are sure to have made a mistake.\n\n\n-----Input-----\n\nThe first line of the input file contains a single integer $n$, the number of piles with stones in the garden ($1 \\leq n \\leq 50$).\n\nThe second line contains $n$ integers separated by spaces $x_1, x_2, \\ldots, x_n$, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the first time ($0 \\leq x_i \\leq 1000$).\n\nThe third line contains $n$ integers separated by spaces $y_1, y_2, \\ldots, y_n$, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the second time ($0 \\leq y_i \\leq 1000$).\n\n\n-----Output-----\n\nIf the records can be consistent output \"Yes\", otherwise output \"No\" (quotes for clarity).\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n2 1 4 3 5\n\nOutput\nYes\n\nInput\n5\n1 1 1 1 1\n1 0 1 0 1\n\nOutput\nYes\n\nInput\n3\n2 3 9\n1 7 9\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first example, the following could have happened during the night: one of the jury members moved one stone from the second pile to the first pile, and the other jury member moved one stone from the fourth pile to the third pile.\n\nIn the second example, the jury took stones from the second and fourth piles.\n\nIt can be proved that it is impossible for the jury members to move and took stones to convert the first array into the second array."} +{"id": "00132", "text": "Students Vasya and Petya are studying at the BSU (Byteland State University). At one of the breaks they decided to order a pizza. In this problem pizza is a circle of some radius. The pizza was delivered already cut into n pieces. The i-th piece is a sector of angle equal to a_{i}. Vasya and Petya want to divide all pieces of pizza into two continuous sectors in such way that the difference between angles of these sectors is minimal. Sector angle is sum of angles of all pieces in it. Pay attention, that one of sectors can be empty.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 360) \u00a0\u2014 the number of pieces into which the delivered pizza was cut.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 360) \u00a0\u2014 the angles of the sectors into which the pizza was cut. The sum of all a_{i} is 360.\n\n\n-----Output-----\n\nPrint one integer \u00a0\u2014 the minimal difference between angles of sectors that will go to Vasya and Petya.\n\n\n-----Examples-----\nInput\n4\n90 90 90 90\n\nOutput\n0\n\nInput\n3\n100 100 160\n\nOutput\n40\n\nInput\n1\n360\n\nOutput\n360\n\nInput\n4\n170 30 150 10\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn first sample Vasya can take 1 and 2 pieces, Petya can take 3 and 4 pieces. Then the answer is |(90 + 90) - (90 + 90)| = 0.\n\nIn third sample there is only one piece of pizza that can be taken by only one from Vasya and Petya. So the answer is |360 - 0| = 360.\n\nIn fourth sample Vasya can take 1 and 4 pieces, then Petya will take 2 and 3 pieces. So the answer is |(170 + 10) - (30 + 150)| = 0.\n\nPicture explaning fourth sample:\n\n[Image]\n\nBoth red and green sectors consist of two adjacent pieces of pizza. So Vasya can take green sector, then Petya will take red sector."} +{"id": "00133", "text": "Alice got many presents these days. So she decided to pack them into boxes and send them to her friends.\n\nThere are $n$ kinds of presents. Presents of one kind are identical (i.e. there is no way to distinguish two gifts of the same kind). Presents of different kinds are different (i.e. that is, two gifts of different kinds are distinguishable). The number of presents of each kind, that Alice has is very big, so we can consider Alice has an infinite number of gifts of each kind.\n\nAlso, there are $m$ boxes. All of them are for different people, so they are pairwise distinct (consider that the names of $m$ friends are written on the boxes). For example, putting the first kind of present into the first box but not into the second box, is different from putting the first kind of present into the second box but not into the first box.\n\nAlice wants to pack presents with the following rules: She won't pack more than one present of each kind into the same box, so each box should contain presents of different kinds (i.e. each box contains a subset of $n$ kinds, empty boxes are allowed); For each kind at least one present should be packed into some box. \n\nNow Alice wants to know how many different ways to pack the presents exists. Please, help her and calculate this number. Since the answer can be huge, output it by modulo $10^9+7$.\n\nSee examples and their notes for clarification.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$, separated by spaces ($1 \\leq n,m \\leq 10^9$)\u00a0\u2014 the number of kinds of presents and the number of boxes that Alice has.\n\n\n-----Output-----\n\nPrint one integer \u00a0\u2014 the number of ways to pack the presents with Alice's rules, calculated by modulo $10^9+7$\n\n\n-----Examples-----\nInput\n1 3\n\nOutput\n7\nInput\n2 2\n\nOutput\n9\n\n\n-----Note-----\n\nIn the first example, there are seven ways to pack presents:\n\n$\\{1\\}\\{\\}\\{\\}$\n\n$\\{\\}\\{1\\}\\{\\}$\n\n$\\{\\}\\{\\}\\{1\\}$\n\n$\\{1\\}\\{1\\}\\{\\}$\n\n$\\{\\}\\{1\\}\\{1\\}$\n\n$\\{1\\}\\{\\}\\{1\\}$\n\n$\\{1\\}\\{1\\}\\{1\\}$\n\nIn the second example there are nine ways to pack presents:\n\n$\\{\\}\\{1,2\\}$\n\n$\\{1\\}\\{2\\}$\n\n$\\{1\\}\\{1,2\\}$\n\n$\\{2\\}\\{1\\}$\n\n$\\{2\\}\\{1,2\\}$\n\n$\\{1,2\\}\\{\\}$\n\n$\\{1,2\\}\\{1\\}$\n\n$\\{1,2\\}\\{2\\}$\n\n$\\{1,2\\}\\{1,2\\}$\n\nFor example, the way $\\{2\\}\\{2\\}$ is wrong, because presents of the first kind should be used in the least one box."} +{"id": "00134", "text": "Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.\n\nFor example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.\n\nHere Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.\n\nKatya had no problems with completing this task. Will you do the same?\n\n\n-----Input-----\n\nThe only line of the input contains single integer n (1 \u2264 n \u2264 10^9)\u00a0\u2014 the length of some side of a right triangle.\n\n\n-----Output-----\n\nPrint two integers m and k (1 \u2264 m, k \u2264 10^18), such that n, m and k form a Pythagorean triple, in the only line.\n\nIn case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n4 5\nInput\n6\n\nOutput\n8 10\nInput\n1\n\nOutput\n-1\nInput\n17\n\nOutput\n144 145\nInput\n67\n\nOutput\n2244 2245\n\n\n-----Note-----[Image]\n\nIllustration for the first sample."} +{"id": "00135", "text": "Imp is watching a documentary about cave painting. [Image] \n\nSome numbers, carved in chaotic order, immediately attracted his attention. Imp rapidly proposed a guess that they are the remainders of division of a number n by all integers i from 1 to k. Unfortunately, there are too many integers to analyze for Imp.\n\nImp wants you to check whether all these remainders are distinct. Formally, he wants to check, if all $n \\text{mod} i$, 1 \u2264 i \u2264 k, are distinct, i.\u00a0e. there is no such pair (i, j) that: 1 \u2264 i < j \u2264 k, $n \\operatorname{mod} i = n \\operatorname{mod} j$, where $x \\operatorname{mod} y$ is the remainder of division x by y. \n\n\n-----Input-----\n\nThe only line contains two integers n, k (1 \u2264 n, k \u2264 10^18).\n\n\n-----Output-----\n\nPrint \"Yes\", if all the remainders are distinct, and \"No\" otherwise.\n\nYou can print each letter in arbitrary case (lower or upper).\n\n\n-----Examples-----\nInput\n4 4\n\nOutput\nNo\n\nInput\n5 3\n\nOutput\nYes\n\n\n\n-----Note-----\n\nIn the first sample remainders modulo 1 and 4 coincide."} +{"id": "00136", "text": "You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.\n\nThe input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.\n\nAs input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the function input() in Python2 instead of it use the function raw_input().\n\n\n-----Input-----\n\nThe first line contains a non-negative integer a.\n\nThe second line contains a non-negative integer b.\n\nThe numbers a, b may contain leading zeroes. Each of them contains no more than 10^6 digits.\n\n\n-----Output-----\n\nPrint the symbol \"<\" if a < b and the symbol \">\" if a > b. If the numbers are equal print the symbol \"=\".\n\n\n-----Examples-----\nInput\n9\n10\n\nOutput\n<\n\nInput\n11\n10\n\nOutput\n>\n\nInput\n00012345\n12345\n\nOutput\n=\n\nInput\n0123\n9\n\nOutput\n>\n\nInput\n0123\n111\n\nOutput\n>"} +{"id": "00137", "text": "Kuro has recently won the \"Most intelligent cat ever\" contest. The three friends then decided to go to Katie's home to celebrate Kuro's winning. After a big meal, they took a small break then started playing games.\n\nKuro challenged Katie to create a game with only a white paper, a pencil, a pair of scissors and a lot of arrows (you can assume that the number of arrows is infinite). Immediately, Katie came up with the game called Topological Parity.\n\nThe paper is divided into $n$ pieces enumerated from $1$ to $n$. Shiro has painted some pieces with some color. Specifically, the $i$-th piece has color $c_{i}$ where $c_{i} = 0$ defines black color, $c_{i} = 1$ defines white color and $c_{i} = -1$ means that the piece hasn't been colored yet.\n\nThe rules of the game is simple. Players must put some arrows between some pairs of different pieces in such a way that for each arrow, the number in the piece it starts from is less than the number of the piece it ends at. Also, two different pieces can only be connected by at most one arrow. After that the players must choose the color ($0$ or $1$) for each of the unpainted pieces. The score of a valid way of putting the arrows and coloring pieces is defined as the number of paths of pieces of alternating colors. For example, $[1 \\to 0 \\to 1 \\to 0]$, $[0 \\to 1 \\to 0 \\to 1]$, $[1]$, $[0]$ are valid paths and will be counted. You can only travel from piece $x$ to piece $y$ if and only if there is an arrow from $x$ to $y$.\n\nBut Kuro is not fun yet. He loves parity. Let's call his favorite parity $p$ where $p = 0$ stands for \"even\" and $p = 1$ stands for \"odd\". He wants to put the arrows and choose colors in such a way that the score has the parity of $p$.\n\nIt seems like there will be so many ways which satisfy Kuro. He wants to count the number of them but this could be a very large number. Let's help him with his problem, but print it modulo $10^{9} + 7$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $p$ ($1 \\leq n \\leq 50$, $0 \\leq p \\leq 1$) \u2014 the number of pieces and Kuro's wanted parity.\n\nThe second line contains $n$ integers $c_{1}, c_{2}, ..., c_{n}$ ($-1 \\leq c_{i} \\leq 1$) \u2014 the colors of the pieces.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of ways to put the arrows and choose colors so the number of valid paths of alternating colors has the parity of $p$.\n\n\n-----Examples-----\nInput\n3 1\n-1 0 1\n\nOutput\n6\nInput\n2 1\n1 0\n\nOutput\n1\nInput\n1 1\n-1\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first example, there are $6$ ways to color the pieces and add the arrows, as are shown in the figure below. The scores are $3, 3, 5$ for the first row and $5, 3, 3$ for the second row, both from left to right.\n\n [Image]"} +{"id": "00138", "text": "Little girl Alyona is in a shop to buy some copybooks for school. She study four subjects so she wants to have equal number of copybooks for each of the subjects. There are three types of copybook's packs in the shop: it is possible to buy one copybook for a rubles, a pack of two copybooks for b rubles, and a pack of three copybooks for c rubles. Alyona already has n copybooks.\n\nWhat is the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4? There are infinitely many packs of any type in the shop. Alyona can buy packs of different type in the same purchase.\n\n\n-----Input-----\n\nThe only line contains 4 integers n, a, b, c (1 \u2264 n, a, b, c \u2264 10^9).\n\n\n-----Output-----\n\nPrint the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4.\n\n\n-----Examples-----\nInput\n1 1 3 4\n\nOutput\n3\n\nInput\n6 2 1 1\n\nOutput\n1\n\nInput\n4 4 4 4\n\nOutput\n0\n\nInput\n999999999 1000000000 1000000000 1000000000\n\nOutput\n1000000000\n\n\n\n-----Note-----\n\nIn the first example Alyona can buy 3 packs of 1 copybook for 3a = 3 rubles in total. After that she will have 4 copybooks which she can split between the subjects equally. \n\nIn the second example Alyuna can buy a pack of 2 copybooks for b = 1 ruble. She will have 8 copybooks in total.\n\nIn the third example Alyona can split the copybooks she already has between the 4 subject equally, so she doesn't need to buy anything.\n\nIn the fourth example Alyona should buy one pack of one copybook."} +{"id": "00139", "text": "You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it.\n\nCan you make this graph acyclic by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex).\n\n\n-----Input-----\n\nThe first line contains two integers n and m (2 \u2264 n \u2264 500, 1 \u2264 m \u2264 min(n(n - 1), 100000)) \u2014 the number of vertices and the number of edges, respectively.\n\nThen m lines follow. Each line contains two integers u and v denoting a directed edge going from vertex u to vertex v (1 \u2264 u, v \u2264 n, u \u2260 v). Each ordered pair (u, v) is listed at most once (there is at most one directed edge from u to v).\n\n\n-----Output-----\n\nIf it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.\n\n\n-----Examples-----\nInput\n3 4\n1 2\n2 3\n3 2\n3 1\n\nOutput\nYES\n\nInput\n5 6\n1 2\n2 3\n3 2\n3 1\n2 1\n4 5\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example you can remove edge $2 \\rightarrow 3$, and the graph becomes acyclic.\n\nIn the second example you have to remove at least two edges (for example, $2 \\rightarrow 1$ and $2 \\rightarrow 3$) in order to make the graph acyclic."} +{"id": "00140", "text": "The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $(Ox)$ axis.\n\nOn this street, there are $n$ antennas, numbered from $1$ to $n$. The $i$-th antenna lies on the position $x_i$ and has an initial scope of $s_i$: it covers all integer positions inside the interval $[x_i - s_i; x_i + s_i]$.\n\nIt is possible to increment the scope of any antenna by $1$, this operation costs $1$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want).\n\nTo modernize the street, we need to make all integer positions from $1$ to $m$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $[1; m]$, even if it's not required.\n\nWhat is the minimum amount of coins needed to achieve this modernization?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 80$ and $n \\le m \\le 100\\ 000$).\n\nThe $i$-th of the next $n$ lines contains two integers $x_i$ and $s_i$ ($1 \\le x_i \\le m$ and $0 \\le s_i \\le m$).\n\nOn each position, there is at most one antenna (values $x_i$ are pairwise distinct).\n\n\n-----Output-----\n\nYou have to output a single integer: the minimum amount of coins required to make all integer positions from $1$ to $m$ inclusive covered by at least one antenna.\n\n\n-----Examples-----\nInput\n3 595\n43 2\n300 4\n554 10\n\nOutput\n281\n\nInput\n1 1\n1 1\n\nOutput\n0\n\nInput\n2 50\n20 0\n3 1\n\nOutput\n30\n\nInput\n5 240\n13 0\n50 25\n60 5\n155 70\n165 70\n\nOutput\n26\n\n\n\n-----Note-----\n\nIn the first example, here is a possible strategy:\n\n Increase the scope of the first antenna by $40$, so that it becomes $2 + 40 = 42$. This antenna will cover interval $[43 - 42; 43 + 42]$ which is $[1; 85]$ Increase the scope of the second antenna by $210$, so that it becomes $4 + 210 = 214$. This antenna will cover interval $[300 - 214; 300 + 214]$, which is $[86; 514]$ Increase the scope of the third antenna by $31$, so that it becomes $10 + 31 = 41$. This antenna will cover interval $[554 - 41; 554 + 41]$, which is $[513; 595]$ \n\nTotal cost is $40 + 210 + 31 = 281$. We can prove that it's the minimum cost required to make all positions from $1$ to $595$ covered by at least one antenna.\n\nNote that positions $513$ and $514$ are in this solution covered by two different antennas, but it's not important.\n\n\u2014\n\nIn the second example, the first antenna already covers an interval $[0; 2]$ so we have nothing to do.\n\nNote that the only position that we needed to cover was position $1$; positions $0$ and $2$ are covered, but it's not important."} +{"id": "00141", "text": "You have a set of items, each having some integer weight not greater than $8$. You denote that a subset of items is good if total weight of items in the subset does not exceed $W$.\n\nYou want to calculate the maximum possible weight of a good subset of items. Note that you have to consider the empty set and the original set when calculating the answer.\n\n\n-----Input-----\n\nThe first line contains one integer $W$ ($0 \\le W \\le 10^{18}$) \u2014 the maximum total weight of a good subset.\n\nThe second line denotes the set of items you have. It contains $8$ integers $cnt_1$, $cnt_2$, ..., $cnt_8$ ($0 \\le cnt_i \\le 10^{16}$), where $cnt_i$ is the number of items having weight $i$ in the set.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible weight of a good subset of items.\n\n\n-----Examples-----\nInput\n10\n1 2 3 4 5 6 7 8\n\nOutput\n10\n\nInput\n0\n0 0 0 0 0 0 0 0\n\nOutput\n0\n\nInput\n3\n0 4 1 0 0 9 8 3\n\nOutput\n3"} +{"id": "00142", "text": "A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.\n\nYour favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2^{i} - 1 liters and costs c_{i} roubles. The number of bottles of each type in the store can be considered infinite.\n\nYou want to buy at least L liters of lemonade. How many roubles do you have to spend?\n\n\n-----Input-----\n\nThe first line contains two integers n and L (1 \u2264 n \u2264 30; 1 \u2264 L \u2264 10^9)\u00a0\u2014 the number of types of bottles in the store and the required amount of lemonade in liters, respectively.\n\nThe second line contains n integers c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 10^9)\u00a0\u2014 the costs of bottles of different types.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.\n\n\n-----Examples-----\nInput\n4 12\n20 30 70 90\n\nOutput\n150\n\nInput\n4 3\n10000 1000 100 10\n\nOutput\n10\n\nInput\n4 3\n10 100 1000 10000\n\nOutput\n30\n\nInput\n5 787787787\n123456789 234567890 345678901 456789012 987654321\n\nOutput\n44981600785557577\n\n\n\n-----Note-----\n\nIn the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.\n\nIn the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.\n\nIn the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles."} +{"id": "00143", "text": "Someone gave Alyona an array containing n positive integers a_1, a_2, ..., a_{n}. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.\n\nFormally, after applying some operations Alyona will get an array of n positive integers b_1, b_2, ..., b_{n} such that 1 \u2264 b_{i} \u2264 a_{i} for every 1 \u2264 i \u2264 n. Your task is to determine the maximum possible value of mex of this array.\n\nMex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of elements in the Alyona's array.\n\nThe second line of the input contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint one positive integer\u00a0\u2014 the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.\n\n\n-----Examples-----\nInput\n5\n1 3 3 3 6\n\nOutput\n5\n\nInput\n2\n2 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.\n\nTo reach the answer to the second sample case one must not decrease any of the array elements."} +{"id": "00144", "text": "Recently Vasya found a golden ticket \u2014 a sequence which consists of $n$ digits $a_1a_2\\dots a_n$. Vasya considers a ticket to be lucky if it can be divided into two or more non-intersecting segments with equal sums. For example, ticket $350178$ is lucky since it can be divided into three segments $350$, $17$ and $8$: $3+5+0=1+7=8$. Note that each digit of sequence should belong to exactly one segment.\n\nHelp Vasya! Tell him if the golden ticket he found is lucky or not.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 100$) \u2014 the number of digits in the ticket.\n\nThe second line contains $n$ digits $a_1 a_2 \\dots a_n$ ($0 \\le a_i \\le 9$) \u2014 the golden ticket. Digits are printed without spaces.\n\n\n-----Output-----\n\nIf the golden ticket is lucky then print \"YES\", otherwise print \"NO\" (both case insensitive).\n\n\n-----Examples-----\nInput\n5\n73452\n\nOutput\nYES\n\nInput\n4\n1248\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example the ticket can be divided into $7$, $34$ and $52$: $7=3+4=5+2$.\n\nIn the second example it is impossible to divide ticket into segments with equal sum."} +{"id": "00145", "text": "Those days, many boys use beautiful girls' photos as avatars in forums. So it is pretty hard to tell the gender of a user at the first glance. Last year, our hero went to a forum and had a nice chat with a beauty (he thought so). After that they talked very often and eventually they became a couple in the network. \n\nBut yesterday, he came to see \"her\" in the real world and found out \"she\" is actually a very strong man! Our hero is very sad and he is too tired to love again now. So he came up with a way to recognize users' genders by their user names.\n\nThis is his method: if the number of distinct characters in one's user name is odd, then he is a male, otherwise she is a female. You are given the string that denotes the user name, please help our hero to determine the gender of this user by his method.\n\n\n-----Input-----\n\nThe first line contains a non-empty string, that contains only lowercase English letters \u2014 the user name. This string contains at most 100 letters.\n\n\n-----Output-----\n\nIf it is a female by our hero's method, print \"CHAT WITH HER!\" (without the quotes), otherwise, print \"IGNORE HIM!\" (without the quotes).\n\n\n-----Examples-----\nInput\nwjmzbmr\n\nOutput\nCHAT WITH HER!\n\nInput\nxiaodao\n\nOutput\nIGNORE HIM!\n\nInput\nsevenkplus\n\nOutput\nCHAT WITH HER!\n\n\n\n-----Note-----\n\nFor the first example. There are 6 distinct characters in \"wjmzbmr\". These characters are: \"w\", \"j\", \"m\", \"z\", \"b\", \"r\". So wjmzbmr is a female and you should print \"CHAT WITH HER!\"."} +{"id": "00146", "text": "This morning, Roman woke up and opened the browser with $n$ opened tabs numbered from $1$ to $n$. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.\n\nHe decided to accomplish this by closing every $k$-th ($2 \\leq k \\leq n - 1$) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be $b$) and then close all tabs with numbers $c = b + i \\cdot k$ that satisfy the following condition: $1 \\leq c \\leq n$ and $i$ is an integer (it may be positive, negative or zero).\n\nFor example, if $k = 3$, $n = 14$ and Roman chooses $b = 8$, then he will close tabs with numbers $2$, $5$, $8$, $11$ and $14$.\n\nAfter closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let's denote it $e$) and the amount of remaining social network tabs ($s$). Help Roman to calculate the maximal absolute value of the difference of those values $|e - s|$ so that it would be easy to decide what to do next.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($2 \\leq k < n \\leq 100$) \u2014 the amount of tabs opened currently and the distance between the tabs closed.\n\nThe second line consists of $n$ integers, each of them equal either to $1$ or to $-1$. The $i$-th integer denotes the type of the $i$-th tab: if it is equal to $1$, this tab contains information for the test, and if it is equal to $-1$, it's a social network tab.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the maximum absolute difference between the amounts of remaining tabs of different types $|e - s|$.\n\n\n-----Examples-----\nInput\n4 2\n1 1 -1 1\n\nOutput\n2\n\nInput\n14 3\n-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1\n\nOutput\n9\n\n\n\n-----Note-----\n\nIn the first example we can choose $b = 1$ or $b = 3$. We will delete then one tab of each type and the remaining tabs are then all contain test information. Thus, $e = 2$ and $s = 0$ and $|e - s| = 2$.\n\nIn the second example, on the contrary, we can leave opened only tabs that have social networks opened in them."} +{"id": "00147", "text": "R3D3 spent some time on an internship in MDCS. After earning enough money, he decided to go on a holiday somewhere far, far away. He enjoyed suntanning, drinking alcohol-free cocktails and going to concerts of popular local bands. While listening to \"The White Buttons\" and their hit song \"Dacan the Baker\", he met another robot for whom he was sure is the love of his life. Well, his summer, at least. Anyway, R3D3 was too shy to approach his potential soulmate, so he decided to write her a love letter. However, he stumbled upon a problem. Due to a terrorist threat, the Intergalactic Space Police was monitoring all letters sent in the area. Thus, R3D3 decided to invent his own alphabet, for which he was sure his love would be able to decipher.\n\nThere are n letters in R3D3\u2019s alphabet, and he wants to represent each letter as a sequence of '0' and '1', so that no letter\u2019s sequence is a prefix of another letter's sequence. Since the Intergalactic Space Communications Service has lately introduced a tax for invented alphabets, R3D3 must pay a certain amount of money for each bit in his alphabet\u2019s code (check the sample test for clarifications). He is too lovestruck to think clearly, so he asked you for help.\n\nGiven the costs c_0 and c_1 for each '0' and '1' in R3D3\u2019s alphabet, respectively, you should come up with a coding for the alphabet (with properties as above) with minimum total cost.\n\n\n-----Input-----\n\nThe first line of input contains three integers n (2 \u2264 n \u2264 10^8), c_0 and c_1 (0 \u2264 c_0, c_1 \u2264 10^8)\u00a0\u2014 the number of letters in the alphabet, and costs of '0' and '1', respectively. \n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 minimum possible total a cost of the whole alphabet.\n\n\n-----Example-----\nInput\n4 1 2\n\nOutput\n12\n\n\n\n-----Note-----\n\nThere are 4 letters in the alphabet. The optimal encoding is \"00\", \"01\", \"10\", \"11\". There are 4 zeroes and 4 ones used, so the total cost is 4\u00b71 + 4\u00b72 = 12."} +{"id": "00148", "text": "The circle line of the Roflanpolis subway has $n$ stations.\n\nThere are two parallel routes in the subway. The first one visits stations in order $1 \\to 2 \\to \\ldots \\to n \\to 1 \\to 2 \\to \\ldots$ (so the next stop after station $x$ is equal to $(x+1)$ if $x < n$ and $1$ otherwise). The second route visits stations in order $n \\to (n-1) \\to \\ldots \\to 1 \\to n \\to (n-1) \\to \\ldots$ (so the next stop after station $x$ is equal to $(x-1)$ if $x>1$ and $n$ otherwise). All trains depart their stations simultaneously, and it takes exactly $1$ minute to arrive at the next station.\n\nTwo toads live in this city, their names are Daniel and Vlad.\n\nDaniel is currently in a train of the first route at station $a$ and will exit the subway when his train reaches station $x$.\n\nCoincidentally, Vlad is currently in a train of the second route at station $b$ and he will exit the subway when his train reaches station $y$.\n\nSurprisingly, all numbers $a,x,b,y$ are distinct.\n\nToad Ilya asks you to check if Daniel and Vlad will ever be at the same station at the same time during their journey. In other words, check if there is a moment when their trains stop at the same station. Note that this includes the moments when Daniel or Vlad enter or leave the subway.\n\n\n-----Input-----\n\nThe first line contains five space-separated integers $n$, $a$, $x$, $b$, $y$ ($4 \\leq n \\leq 100$, $1 \\leq a, x, b, y \\leq n$, all numbers among $a$, $x$, $b$, $y$ are distinct)\u00a0\u2014 the number of stations in Roflanpolis, Daniel's start station, Daniel's finish station, Vlad's start station and Vlad's finish station, respectively.\n\n\n-----Output-----\n\nOutput \"YES\" if there is a time moment when Vlad and Daniel are at the same station, and \"NO\" otherwise. You can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n5 1 4 3 2\n\nOutput\nYES\n\nInput\n10 2 1 9 10\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example, Daniel and Vlad start at the stations $(1, 3)$. One minute later they are at stations $(2, 2)$. They are at the same station at this moment. Note that Vlad leaves the subway right after that.\n\nConsider the second example, let's look at the stations Vlad and Daniel are at. They are: initially $(2, 9)$, after $1$ minute $(3, 8)$, after $2$ minutes $(4, 7)$, after $3$ minutes $(5, 6)$, after $4$ minutes $(6, 5)$, after $5$ minutes $(7, 4)$, after $6$ minutes $(8, 3)$, after $7$ minutes $(9, 2)$, after $8$ minutes $(10, 1)$, after $9$ minutes $(1, 10)$. \n\nAfter that, they both leave the subway because they are at their finish stations, so there is no moment when they both are at the same station."} +{"id": "00149", "text": "Unlucky year in Berland is such a year that its number n can be represented as n = x^{a} + y^{b}, where a and b are non-negative integer numbers. \n\nFor example, if x = 2 and y = 3 then the years 4 and 17 are unlucky (4 = 2^0 + 3^1, 17 = 2^3 + 3^2 = 2^4 + 3^0) and year 18 isn't unlucky as there is no such representation for it.\n\nSuch interval of years that there are no unlucky years in it is called The Golden Age.\n\nYou should write a program which will find maximum length of The Golden Age which starts no earlier than the year l and ends no later than the year r. If all years in the interval [l, r] are unlucky then the answer is 0.\n\n\n-----Input-----\n\nThe first line contains four integer numbers x, y, l and r (2 \u2264 x, y \u2264 10^18, 1 \u2264 l \u2264 r \u2264 10^18).\n\n\n-----Output-----\n\nPrint the maximum length of The Golden Age within the interval [l, r].\n\nIf all years in the interval [l, r] are unlucky then print 0.\n\n\n-----Examples-----\nInput\n2 3 1 10\n\nOutput\n1\n\nInput\n3 5 10 22\n\nOutput\n8\n\nInput\n2 3 3 5\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example the unlucky years are 2, 3, 4, 5, 7, 9 and 10. So maximum length of The Golden Age is achived in the intervals [1, 1], [6, 6] and [8, 8].\n\nIn the second example the longest Golden Age is the interval [15, 22]."} +{"id": "00150", "text": "Mr. Funt now lives in a country with a very specific tax laws. The total income of mr. Funt during this year is equal to n (n \u2265 2) burles and the amount of tax he has to pay is calculated as the maximum divisor of n (not equal to n, of course). For example, if n = 6 then Funt has to pay 3 burles, while for n = 25 he needs to pay 5 and if n = 2 he pays only 1 burle.\n\nAs mr. Funt is a very opportunistic person he wants to cheat a bit. In particular, he wants to split the initial n in several parts n_1 + n_2 + ... + n_{k} = n (here k is arbitrary, even k = 1 is allowed) and pay the taxes for each part separately. He can't make some part equal to 1 because it will reveal him. So, the condition n_{i} \u2265 2 should hold for all i from 1 to k.\n\nOstap Bender wonders, how many money Funt has to pay (i.e. minimal) if he chooses and optimal way to split n in parts.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (2 \u2264 n \u2264 2\u00b710^9)\u00a0\u2014 the total year income of mr. Funt.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 minimum possible number of burles that mr. Funt has to pay as a tax.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n2\n\nInput\n27\n\nOutput\n3"} +{"id": "00151", "text": "Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them.\n\nBeroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block.\n\nFor example:\n\n the following words have typos: \"hellno\", \"hackcerrs\" and \"backtothefutttture\"; the following words don't have typos: \"helllllooooo\", \"tobeornottobe\" and \"oooooo\". \n\nWhen Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos.\n\nImplement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem.\n\n\n-----Input-----\n\nThe only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters.\n\n\n-----Output-----\n\nPrint the given word without any changes if there are no typos.\n\nIf there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\nhellno\n\nOutput\nhell no \n\nInput\nabacaba\n\nOutput\nabacaba \n\nInput\nasdfasdf\n\nOutput\nasd fasd f"} +{"id": "00152", "text": "Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare n potions.\n\nAnton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two types that can faster the process of preparing potions. Spells of this type speed up the preparation time of one potion. There are m spells of this type, the i-th of them costs b_{i} manapoints and changes the preparation time of each potion to a_{i} instead of x. Spells of this type immediately prepare some number of potions. There are k such spells, the i-th of them costs d_{i} manapoints and instantly create c_{i} potions. \n\nAnton can use no more than one spell of the first type and no more than one spell of the second type, and the total number of manapoints spent should not exceed s. Consider that all spells are used instantly and right before Anton starts to prepare potions.\n\nAnton wants to get to the next level as fast as possible, so he is interested in the minimum number of time he needs to spent in order to prepare at least n potions.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, m, k (1 \u2264 n \u2264 2\u00b710^9, 1 \u2264 m, k \u2264 2\u00b710^5)\u00a0\u2014 the number of potions, Anton has to make, the number of spells of the first type and the number of spells of the second type.\n\nThe second line of the input contains two integers x and s (2 \u2264 x \u2264 2\u00b710^9, 1 \u2264 s \u2264 2\u00b710^9)\u00a0\u2014 the initial number of seconds required to prepare one potion and the number of manapoints Anton can use.\n\nThe third line contains m integers a_{i} (1 \u2264 a_{i} < x)\u00a0\u2014 the number of seconds it will take to prepare one potion if the i-th spell of the first type is used.\n\nThe fourth line contains m integers b_{i} (1 \u2264 b_{i} \u2264 2\u00b710^9)\u00a0\u2014 the number of manapoints to use the i-th spell of the first type.\n\nThere are k integers c_{i} (1 \u2264 c_{i} \u2264 n) in the fifth line\u00a0\u2014 the number of potions that will be immediately created if the i-th spell of the second type is used. It's guaranteed that c_{i} are not decreasing, i.e. c_{i} \u2264 c_{j} if i < j.\n\nThe sixth line contains k integers d_{i} (1 \u2264 d_{i} \u2264 2\u00b710^9)\u00a0\u2014 the number of manapoints required to use the i-th spell of the second type. It's guaranteed that d_{i} are not decreasing, i.e. d_{i} \u2264 d_{j} if i < j.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimum time one has to spent in order to prepare n potions.\n\n\n-----Examples-----\nInput\n20 3 2\n10 99\n2 4 3\n20 10 40\n4 15\n10 80\n\nOutput\n20\n\nInput\n20 3 2\n10 99\n2 4 3\n200 100 400\n4 15\n100 800\n\nOutput\n200\n\n\n\n-----Note-----\n\nIn the first sample, the optimum answer is to use the second spell of the first type that costs 10 manapoints. Thus, the preparation time of each potion changes to 4 seconds. Also, Anton should use the second spell of the second type to instantly prepare 15 potions spending 80 manapoints. The total number of manapoints used is 10 + 80 = 90, and the preparation time is 4\u00b75 = 20 seconds (15 potions were prepared instantly, and the remaining 5 will take 4 seconds each).\n\nIn the second sample, Anton can't use any of the spells, so he just prepares 20 potions, spending 10 seconds on each of them and the answer is 20\u00b710 = 200."} +{"id": "00153", "text": "Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him t_{j} minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order.\n\nBy solving subtask of arbitrary problem he earns one point. Thus, the number of points for task is equal to the number of solved subtasks in it. Moreover, if Polycarp completely solves the task (solves all k of its subtasks), he recieves one extra point. Thus, total number of points he recieves for the complete solution of the task is k + 1.\n\nPolycarp has M minutes of time. What is the maximum number of points he can earn?\n\n\n-----Input-----\n\nThe first line contains three integer numbers n, k and M (1 \u2264 n \u2264 45, 1 \u2264 k \u2264 45, 0 \u2264 M \u2264 2\u00b710^9).\n\nThe second line contains k integer numbers, values t_{j} (1 \u2264 t_{j} \u2264 1000000), where t_{j} is the time in minutes required to solve j-th subtask of any task.\n\n\n-----Output-----\n\nPrint the maximum amount of points Polycarp can earn in M minutes.\n\n\n-----Examples-----\nInput\n3 4 11\n1 2 3 4\n\nOutput\n6\n\nInput\n5 5 10\n1 2 4 8 16\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first example Polycarp can complete the first task and spend 1 + 2 + 3 + 4 = 10 minutes. He also has the time to solve one subtask of the second task in one minute.\n\nIn the second example Polycarp can solve the first subtask of all five tasks and spend 5\u00b71 = 5 minutes. Also he can solve the second subtasks of two tasks and spend 2\u00b72 = 4 minutes. Thus, he earns 5 + 2 = 7 points in total."} +{"id": "00154", "text": "Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree.\n\nThe depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is $0$.\n\nLet's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices.\n\nLet's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex $v$: If $v$ has a left subtree whose root is $u$, then the parity of the key of $v$ is different from the parity of the key of $u$. If $v$ has a right subtree whose root is $w$, then the parity of the key of $v$ is the same as the parity of the key of $w$. \n\nYou are given a single integer $n$. Find the number of perfectly balanced striped binary search trees with $n$ vertices that have distinct integer keys between $1$ and $n$, inclusive. Output this number modulo $998\\,244\\,353$.\n\n\n-----Input-----\n\nThe only line contains a single integer $n$ ($1 \\le n \\le 10^6$), denoting the required number of vertices.\n\n\n-----Output-----\n\nOutput the number of perfectly balanced striped binary search trees with $n$ vertices and distinct integer keys between $1$ and $n$, inclusive, modulo $998\\,244\\,353$.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n1\n\nInput\n3\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, this is the only tree that satisfies the conditions: $\\left. \\begin{array}{l}{\\text{perfectly balanced}} \\\\{\\text{striped}} \\\\{\\text{binary search tree}} \\end{array} \\right.$\n\nIn the second example, here are various trees that don't satisfy some condition: [Image]"} +{"id": "00155", "text": "You might have heard about the next game in Lara Croft series coming out this year. You also might have watched its trailer. Though you definitely missed the main idea about its plot, so let me lift the veil of secrecy.\n\nLara is going to explore yet another dangerous dungeon. Game designers decided to use good old 2D environment. The dungeon can be represented as a rectangle matrix of n rows and m columns. Cell (x, y) is the cell in the x-th row in the y-th column. Lara can move between the neighbouring by side cells in all four directions.\n\nMoreover, she has even chosen the path for herself to avoid all the traps. She enters the dungeon in cell (1, 1), that is top left corner of the matrix. Then she goes down all the way to cell (n, 1) \u2014 the bottom left corner. Then she starts moving in the snake fashion \u2014 all the way to the right, one cell up, then to the left to the cell in 2-nd column, one cell up. She moves until she runs out of non-visited cells. n and m given are such that she always end up in cell (1, 2).\n\nLara has already moved to a neighbouring cell k times. Can you determine her current position?\n\n\n-----Input-----\n\nThe only line contains three integers n, m and k (2 \u2264 n, m \u2264 10^9, n is always even, 0 \u2264 k < n\u00b7m). Note that k doesn't fit into 32-bit integer type!\n\n\n-----Output-----\n\nPrint the cell (the row and the column where the cell is situated) where Lara ends up after she moves k times.\n\n\n-----Examples-----\nInput\n4 3 0\n\nOutput\n1 1\n\nInput\n4 3 11\n\nOutput\n1 2\n\nInput\n4 3 7\n\nOutput\n3 2\n\n\n\n-----Note-----\n\nHere is her path on matrix 4 by 3: [Image]"} +{"id": "00156", "text": "Today, Osama gave Fadi an integer $X$, and Fadi was wondering about the minimum possible value of $max(a, b)$ such that $LCM(a, b)$ equals $X$. Both $a$ and $b$ should be positive integers.\n\n$LCM(a, b)$ is the smallest positive integer that is divisible by both $a$ and $b$. For example, $LCM(6, 8) = 24$, $LCM(4, 12) = 12$, $LCM(2, 3) = 6$.\n\nOf course, Fadi immediately knew the answer. Can you be just like Fadi and find any such pair?\n\n\n-----Input-----\n\nThe first and only line contains an integer $X$ ($1 \\le X \\le 10^{12}$).\n\n\n-----Output-----\n\nPrint two positive integers, $a$ and $b$, such that the value of $max(a, b)$ is minimum possible and $LCM(a, b)$ equals $X$. If there are several possible such pairs, you can print any.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1 2\n\nInput\n6\n\nOutput\n2 3\n\nInput\n4\n\nOutput\n1 4\n\nInput\n1\n\nOutput\n1 1"} +{"id": "00157", "text": "Nikolay has a lemons, b apples and c pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio 1: 2: 4. It means that for each lemon in the compote should be exactly 2 apples and exactly 4 pears. You can't crumble up, break up or cut these fruits into pieces. These fruits\u00a0\u2014 lemons, apples and pears\u00a0\u2014 should be put in the compote as whole fruits.\n\nYour task is to determine the maximum total number of lemons, apples and pears from which Nikolay can cook the compote. It is possible that Nikolay can't use any fruits, in this case print 0. \n\n\n-----Input-----\n\nThe first line contains the positive integer a (1 \u2264 a \u2264 1000)\u00a0\u2014 the number of lemons Nikolay has. \n\nThe second line contains the positive integer b (1 \u2264 b \u2264 1000)\u00a0\u2014 the number of apples Nikolay has. \n\nThe third line contains the positive integer c (1 \u2264 c \u2264 1000)\u00a0\u2014 the number of pears Nikolay has.\n\n\n-----Output-----\n\nPrint the maximum total number of lemons, apples and pears from which Nikolay can cook the compote.\n\n\n-----Examples-----\nInput\n2\n5\n7\n\nOutput\n7\n\nInput\n4\n7\n13\n\nOutput\n21\n\nInput\n2\n3\n2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example Nikolay can use 1 lemon, 2 apples and 4 pears, so the answer is 1 + 2 + 4 = 7.\n\nIn the second example Nikolay can use 3 lemons, 6 apples and 12 pears, so the answer is 3 + 6 + 12 = 21.\n\nIn the third example Nikolay don't have enough pears to cook any compote, so the answer is 0."} +{"id": "00158", "text": "Berland annual chess tournament is coming!\n\nOrganizers have gathered 2\u00b7n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.\n\nThus, organizers should divide all 2\u00b7n players into two teams with n people each in such a way that the first team always wins.\n\nEvery chess player has its rating r_{i}. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.\n\nAfter teams assignment there will come a drawing to form n pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.\n\nIs it possible to divide all 2\u00b7n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing?\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 100).\n\nThe second line contains 2\u00b7n integers a_1, a_2, ... a_2n (1 \u2264 a_{i} \u2264 1000).\n\n\n-----Output-----\n\nIf it's possible to divide all 2\u00b7n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing, then print \"YES\". Otherwise print \"NO\".\n\n\n-----Examples-----\nInput\n2\n1 3 2 4\n\nOutput\nYES\n\nInput\n1\n3 3\n\nOutput\nNO"} +{"id": "00159", "text": "You are given an array of n elements, you must make it a co-prime array in as few moves as possible.\n\nIn each move you can insert any positive integral number you want not greater than 10^9 in any place in the array.\n\nAn array is co-prime if any two adjacent numbers of it are co-prime.\n\nIn the number theory, two integers a and b are said to be co-prime if the only positive integer that divides both of them is 1.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 1000) \u2014 the number of elements in the given array.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^9) \u2014 the elements of the array a.\n\n\n-----Output-----\n\nPrint integer k on the first line \u2014 the least number of elements needed to add to the array a to make it co-prime.\n\nThe second line should contain n + k integers a_{j} \u2014 the elements of the array a after adding k elements to it. Note that the new array should be co-prime, so any two adjacent values should be co-prime. Also the new array should be got from the original array a by adding k elements to it.\n\nIf there are multiple answers you can print any one of them.\n\n\n-----Example-----\nInput\n3\n2 7 28\n\nOutput\n1\n2 7 9 28"} +{"id": "00160", "text": "We have a sequence of N integers: A_1, A_2, \\cdots, A_N.\nYou can perform the following operation between 0 and K times (inclusive):\n - Choose two integers i and j such that i \\neq j, each between 1 and N (inclusive). Add 1 to A_i and -1 to A_j, possibly producing a negative element.\nCompute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that y = xz.\n\n-----Constraints-----\n - 2 \\leq N \\leq 500\n - 1 \\leq A_i \\leq 10^6\n - 0 \\leq K \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nA_1 A_2 \\cdots A_{N-1} A_{N}\n\n-----Output-----\nPrint the maximum possible positive integer that divides every element of A after the operations.\n\n-----Sample Input-----\n2 3\n8 20\n\n-----Sample Output-----\n7\n\n7 will divide every element of A if, for example, we perform the following operation:\n - Choose i = 2, j = 1. A becomes (7, 21).\nWe cannot reach the situation where 8 or greater integer divides every element of A."} +{"id": "00161", "text": "Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.\n\nAssume that we have a cat with a number $x$. A perfect longcat is a cat with a number equal $2^m - 1$ for some non-negative integer $m$. For example, the numbers $0$, $1$, $3$, $7$, $15$ and so on are suitable for the perfect longcats.\n\nIn the Cat Furrier Transform, the following operations can be performed on $x$: (Operation A): you select any non-negative integer $n$ and replace $x$ with $x \\oplus (2^n - 1)$, with $\\oplus$ being a bitwise XOR operator. (Operation B): replace $x$ with $x + 1$. \n\nThe first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.\n\nNeko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most $40$ operations. Can you help Neko writing a transformation plan?\n\nNote that it is not required to minimize the number of operations. You just need to use no more than $40$ operations.\n\n\n-----Input-----\n\nThe only line contains a single integer $x$ ($1 \\le x \\le 10^6$).\n\n\n-----Output-----\n\nThe first line should contain a single integer $t$ ($0 \\le t \\le 40$)\u00a0\u2014 the number of operations to apply.\n\nThen for each odd-numbered operation print the corresponding number $n_i$ in it. That is, print $\\lceil \\frac{t}{2} \\rceil$ integers $n_i$ ($0 \\le n_i \\le 30$), denoting the replacement $x$ with $x \\oplus (2^{n_i} - 1)$ in the corresponding step.\n\nIf there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.\n\n\n-----Examples-----\nInput\n39\n\nOutput\n4\n5 3 \nInput\n1\n\nOutput\n0\n\nInput\n7\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test, one of the transforms might be as follows: $39 \\to 56 \\to 57 \\to 62 \\to 63$. Or more precisely: Pick $n = 5$. $x$ is transformed into $39 \\oplus 31$, or $56$. Increase $x$ by $1$, changing its value to $57$. Pick $n = 3$. $x$ is transformed into $57 \\oplus 7$, or $62$. Increase $x$ by $1$, changing its value to $63 = 2^6 - 1$. \n\nIn the second and third test, the number already satisfies the goal requirement."} +{"id": "00162", "text": "Luba thinks about watering her garden. The garden can be represented as a segment of length k. Luba has got n buckets, the i-th bucket allows her to water some continuous subsegment of garden of length exactly a_{i} each hour. Luba can't water any parts of the garden that were already watered, also she can't water the ground outside the garden.\n\nLuba has to choose one of the buckets in order to water the garden as fast as possible (as mentioned above, each hour she will water some continuous subsegment of length a_{i} if she chooses the i-th bucket). Help her to determine the minimum number of hours she has to spend watering the garden. It is guaranteed that Luba can always choose a bucket so it is possible water the garden.\n\nSee the examples for better understanding.\n\n\n-----Input-----\n\nThe first line of input contains two integer numbers n and k (1 \u2264 n, k \u2264 100) \u2014 the number of buckets and the length of the garden, respectively.\n\nThe second line of input contains n integer numbers a_{i} (1 \u2264 a_{i} \u2264 100) \u2014 the length of the segment that can be watered by the i-th bucket in one hour.\n\nIt is guaranteed that there is at least one bucket such that it is possible to water the garden in integer number of hours using only this bucket.\n\n\n-----Output-----\n\nPrint one integer number \u2014 the minimum number of hours required to water the garden.\n\n\n-----Examples-----\nInput\n3 6\n2 3 5\n\nOutput\n2\n\nInput\n6 7\n1 2 3 4 5 6\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first test the best option is to choose the bucket that allows to water the segment of length 3. We can't choose the bucket that allows to water the segment of length 5 because then we can't water the whole garden.\n\nIn the second test we can choose only the bucket that allows us to water the segment of length 1."} +{"id": "00163", "text": "On the way to Rio de Janeiro Ostap kills time playing with a grasshopper he took with him in a special box. Ostap builds a line of length n such that some cells of this line are empty and some contain obstacles. Then, he places his grasshopper to one of the empty cells and a small insect in another empty cell. The grasshopper wants to eat the insect.\n\nOstap knows that grasshopper is able to jump to any empty cell that is exactly k cells away from the current (to the left or to the right). Note that it doesn't matter whether intermediate cells are empty or not as the grasshopper makes a jump over them. For example, if k = 1 the grasshopper can jump to a neighboring cell only, and if k = 2 the grasshopper can jump over a single cell.\n\nYour goal is to determine whether there is a sequence of jumps such that grasshopper will get from his initial position to the cell with an insect.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (2 \u2264 n \u2264 100, 1 \u2264 k \u2264 n - 1)\u00a0\u2014 the number of cells in the line and the length of one grasshopper's jump.\n\nThe second line contains a string of length n consisting of characters '.', '#', 'G' and 'T'. Character '.' means that the corresponding cell is empty, character '#' means that the corresponding cell contains an obstacle and grasshopper can't jump there. Character 'G' means that the grasshopper starts at this position and, finally, 'T' means that the target insect is located at this cell. It's guaranteed that characters 'G' and 'T' appear in this line exactly once.\n\n\n-----Output-----\n\nIf there exists a sequence of jumps (each jump of length k), such that the grasshopper can get from his initial position to the cell with the insect, print \"YES\" (without quotes) in the only line of the input. Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n5 2\n#G#T#\n\nOutput\nYES\n\nInput\n6 1\nT....G\n\nOutput\nYES\n\nInput\n7 3\nT..#..G\n\nOutput\nNO\n\nInput\n6 2\n..GT..\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample, the grasshopper can make one jump to the right in order to get from cell 2 to cell 4.\n\nIn the second sample, the grasshopper is only able to jump to neighboring cells but the way to the insect is free\u00a0\u2014 he can get there by jumping left 5 times.\n\nIn the third sample, the grasshopper can't make a single jump.\n\nIn the fourth sample, the grasshopper can only jump to the cells with odd indices, thus he won't be able to reach the insect."} +{"id": "00164", "text": "It's a beautiful April day and Wallace is playing football with his friends. But his friends do not know that Wallace actually stayed home with Gromit and sent them his robotic self instead. Robo-Wallace has several advantages over the other guys. For example, he can hit the ball directly to the specified point. And yet, the notion of a giveaway is foreign to him. The combination of these features makes the Robo-Wallace the perfect footballer \u2014 as soon as the ball gets to him, he can just aim and hit the goal. He followed this tactics in the first half of the match, but he hit the goal rarely. The opposing team has a very good goalkeeper who catches most of the balls that fly directly into the goal. But Robo-Wallace is a quick thinker, he realized that he can cheat the goalkeeper. After all, they are playing in a football box with solid walls. Robo-Wallace can kick the ball to the other side, then the goalkeeper will not try to catch the ball. Then, if the ball bounces off the wall and flies into the goal, the goal will at last be scored.\n\nYour task is to help Robo-Wallace to detect a spot on the wall of the football box, to which the robot should kick the ball, so that the ball bounces once and only once off this wall and goes straight to the goal. In the first half of the match Robo-Wallace got a ball in the head and was severely hit. As a result, some of the schemes have been damaged. Because of the damage, Robo-Wallace can only aim to his right wall (Robo-Wallace is standing with his face to the opposing team's goal).\n\nThe football box is rectangular. Let's introduce a two-dimensional coordinate system so that point (0, 0) lies in the lower left corner of the field, if you look at the box above. Robo-Wallace is playing for the team, whose goal is to the right. It is an improvised football field, so the gate of Robo-Wallace's rivals may be not in the middle of the left wall. [Image] \n\nIn the given coordinate system you are given: y_1, y_2 \u2014 the y-coordinates of the side pillars of the goalposts of robo-Wallace's opponents; y_{w} \u2014 the y-coordinate of the wall to which Robo-Wallace is aiming; x_{b}, y_{b} \u2014 the coordinates of the ball's position when it is hit; r \u2014 the radius of the ball. \n\nA goal is scored when the center of the ball crosses the OY axis in the given coordinate system between (0, y_1) and (0, y_2). The ball moves along a straight line. The ball's hit on the wall is perfectly elastic (the ball does not shrink from the hit), the angle of incidence equals the angle of reflection. If the ball bounces off the wall not to the goal, that is, if it hits the other wall or the goal post, then the opposing team catches the ball and Robo-Wallace starts looking for miscalculation and gets dysfunctional. Such an outcome, if possible, should be avoided. We assume that the ball touches an object, if the distance from the center of the ball to the object is no greater than the ball radius r.\n\n\n-----Input-----\n\nThe first and the single line contains integers y_1, y_2, y_{w}, x_{b}, y_{b}, r (1 \u2264 y_1, y_2, y_{w}, x_{b}, y_{b} \u2264 10^6; y_1 < y_2 < y_{w}; y_{b} + r < y_{w}; 2\u00b7r < y_2 - y_1).\n\nIt is guaranteed that the ball is positioned correctly in the field, doesn't cross any wall, doesn't touch the wall that Robo-Wallace is aiming at. The goal posts can't be located in the field corners.\n\n\n-----Output-----\n\nIf Robo-Wallace can't score a goal in the described manner, print \"-1\" (without the quotes). Otherwise, print a single number x_{w} \u2014 the abscissa of his point of aiming. \n\nIf there are multiple points of aiming, print the abscissa of any of them. When checking the correctness of the answer, all comparisons are made with the permissible absolute error, equal to 10^{ - 8}. \n\nIt is recommended to print as many characters after the decimal point as possible.\n\n\n-----Examples-----\nInput\n4 10 13 10 3 1\n\nOutput\n4.3750000000\n\nInput\n1 4 6 2 2 1\n\nOutput\n-1\n\nInput\n3 10 15 17 9 2\n\nOutput\n11.3333333333\n\n\n\n-----Note-----\n\nNote that in the first and third samples other correct values of abscissa x_{w} are also possible."} +{"id": "00165", "text": "Vasiliy spent his vacation in a sanatorium, came back and found that he completely forgot details of his vacation! \n\nEvery day there was a breakfast, a dinner and a supper in a dining room of the sanatorium (of course, in this order). The only thing that Vasiliy has now is a card from the dining room contaning notes how many times he had a breakfast, a dinner and a supper (thus, the card contains three integers). Vasiliy could sometimes have missed some meal, for example, he could have had a breakfast and a supper, but a dinner, or, probably, at some days he haven't been at the dining room at all.\n\nVasiliy doesn't remember what was the time of the day when he arrived to sanatorium (before breakfast, before dinner, before supper or after supper), and the time when he left it (before breakfast, before dinner, before supper or after supper). So he considers any of these options. After Vasiliy arrived to the sanatorium, he was there all the time until he left. Please note, that it's possible that Vasiliy left the sanatorium on the same day he arrived.\n\nAccording to the notes in the card, help Vasiliy determine the minimum number of meals in the dining room that he could have missed. We shouldn't count as missed meals on the arrival day before Vasiliy's arrival and meals on the departure day after he left.\n\n\n-----Input-----\n\nThe only line contains three integers b, d and s (0 \u2264 b, d, s \u2264 10^18, b + d + s \u2265 1)\u00a0\u2014 the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium. \n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the minimum possible number of meals which Vasiliy could have missed during his vacation. \n\n\n-----Examples-----\nInput\n3 2 1\n\nOutput\n1\n\n\nInput\n1 0 0\n\nOutput\n0\n\n\nInput\n1 1 1\n\nOutput\n0\n\n\nInput\n1000000000000000000 0 1000000000000000000\n\nOutput\n999999999999999999\n\n\n\n\n\n-----Note-----\n\nIn the first sample, Vasiliy could have missed one supper, for example, in case he have arrived before breakfast, have been in the sanatorium for two days (including the day of arrival) and then have left after breakfast on the third day. \n\nIn the second sample, Vasiliy could have arrived before breakfast, have had it, and immediately have left the sanatorium, not missing any meal.\n\nIn the third sample, Vasiliy could have been in the sanatorium for one day, not missing any meal."} +{"id": "00166", "text": "There is a matrix A of size x \u00d7 y filled with integers. For every $i \\in [ 1 . . x ]$, $j \\in [ 1 . . y ]$ A_{i}, j = y(i - 1) + j. Obviously, every integer from [1..xy] occurs exactly once in this matrix. \n\nYou have traversed some path in this matrix. Your path can be described as a sequence of visited cells a_1, a_2, ..., a_{n} denoting that you started in the cell containing the number a_1, then moved to the cell with the number a_2, and so on.\n\nFrom the cell located in i-th line and j-th column (we denote this cell as (i, j)) you can move into one of the following cells: (i + 1, j) \u2014 only if i < x; (i, j + 1) \u2014 only if j < y; (i - 1, j) \u2014 only if i > 1; (i, j - 1) \u2014 only if j > 1.\n\nNotice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. You don't know x and y exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer a_1, then move to the cell containing a_2 (in one step), then move to the cell containing a_3 (also in one step) and so on. Can you choose x and y so that they don't contradict with your sequence of moves?\n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 200000) \u2014 the number of cells you visited on your path (if some cell is visited twice, then it's listed twice).\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 the integers in the cells on your path.\n\n\n-----Output-----\n\nIf all possible values of x and y such that 1 \u2264 x, y \u2264 10^9 contradict with the information about your path, print NO.\n\nOtherwise, print YES in the first line, and in the second line print the values x and y such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 10^9.\n\n\n-----Examples-----\nInput\n8\n1 2 3 6 9 8 5 2\n\nOutput\nYES\n3 3\n\nInput\n6\n1 2 1 2 5 3\n\nOutput\nNO\n\nInput\n2\n1 10\n\nOutput\nYES\n4 9\n\n\n\n-----Note-----\n\nThe matrix and the path on it in the first test looks like this: [Image] \n\nAlso there exist multiple correct answers for both the first and the third examples."} +{"id": "00167", "text": "You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.\n\nSubsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.\n\n\n-----Input-----\n\nThe first line contains string a, and the second line\u00a0\u2014 string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 10^5 characters.\n\n\n-----Output-----\n\nOn the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters.\n\nIf the answer consists of zero characters, output \u00ab-\u00bb (a minus sign).\n\n\n-----Examples-----\nInput\nhi\nbob\n\nOutput\n-\n\nInput\nabca\naccepted\n\nOutput\nac\n\nInput\nabacaba\nabcdcba\n\nOutput\nabcba\n\n\n\n-----Note-----\n\nIn the first example strings a and b don't share any symbols, so the longest string that you can get is empty.\n\nIn the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b."} +{"id": "00168", "text": "Vasya has a pile, that consists of some number of stones. $n$ times he either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation of taking one stone from the pile.\n\nYou are given $n$ operations which Vasya has made. Find the minimal possible number of stones that can be in the pile after making these operations.\n\n\n-----Input-----\n\nThe first line contains one positive integer $n$\u00a0\u2014 the number of operations, that have been made by Vasya ($1 \\leq n \\leq 100$).\n\nThe next line contains the string $s$, consisting of $n$ symbols, equal to \"-\" (without quotes) or \"+\" (without quotes). If Vasya took the stone on $i$-th operation, $s_i$ is equal to \"-\" (without quotes), if added, $s_i$ is equal to \"+\" (without quotes).\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimal possible number of stones that can be in the pile after these $n$ operations.\n\n\n-----Examples-----\nInput\n3\n---\n\nOutput\n0\nInput\n4\n++++\n\nOutput\n4\nInput\n2\n-+\n\nOutput\n1\nInput\n5\n++-++\n\nOutput\n3\n\n\n-----Note-----\n\nIn the first test, if Vasya had $3$ stones in the pile at the beginning, after making operations the number of stones will be equal to $0$. It is impossible to have less number of piles, so the answer is $0$. Please notice, that the number of stones at the beginning can't be less, than $3$, because in this case, Vasya won't be able to take a stone on some operation (the pile will be empty).\n\nIn the second test, if Vasya had $0$ stones in the pile at the beginning, after making operations the number of stones will be equal to $4$. It is impossible to have less number of piles because after making $4$ operations the number of stones in the pile increases on $4$ stones. So, the answer is $4$.\n\nIn the third test, if Vasya had $1$ stone in the pile at the beginning, after making operations the number of stones will be equal to $1$. It can be proved, that it is impossible to have less number of stones after making the operations.\n\nIn the fourth test, if Vasya had $0$ stones in the pile at the beginning, after making operations the number of stones will be equal to $3$."} +{"id": "00169", "text": "Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated.\n\nKolya is hungry, so he went to the nearest milk shop. In 2084 you may buy kefir in a plastic liter bottle, that costs a rubles, or in glass liter bottle, that costs b rubles. Also, you may return empty glass bottle and get c (c < b) rubles back, but you cannot return plastic bottles.\n\nKolya has n rubles and he is really hungry, so he wants to drink as much kefir as possible. There were no plastic bottles in his 1984, so Kolya doesn't know how to act optimally and asks for your help.\n\n\n-----Input-----\n\nFirst line of the input contains a single integer n (1 \u2264 n \u2264 10^18)\u00a0\u2014 the number of rubles Kolya has at the beginning.\n\nThen follow three lines containing integers a, b and c (1 \u2264 a \u2264 10^18, 1 \u2264 c < b \u2264 10^18)\u00a0\u2014 the cost of one plastic liter bottle, the cost of one glass liter bottle and the money one can get back by returning an empty glass bottle, respectively.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 maximum number of liters of kefir, that Kolya can drink.\n\n\n-----Examples-----\nInput\n10\n11\n9\n8\n\nOutput\n2\n\nInput\n10\n5\n6\n1\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample, Kolya can buy one glass bottle, then return it and buy one more glass bottle. Thus he will drink 2 liters of kefir.\n\nIn the second sample, Kolya can buy two plastic bottle and get two liters of kefir, or he can buy one liter glass bottle, then return it and buy one plastic bottle. In both cases he will drink two liters of kefir."} +{"id": "00170", "text": "Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a \"war\"-like card game. \n\nThe rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins. \n\nYou have to calculate how many fights will happen and who will win the game, or state that game won't end.\n\n\n-----Input-----\n\nFirst line contains a single integer n (2 \u2264 n \u2264 10), the number of cards.\n\nSecond line contains integer k_1 (1 \u2264 k_1 \u2264 n - 1), the number of the first soldier's cards. Then follow k_1 integers that are the values on the first soldier's cards, from top to bottom of his stack.\n\nThird line contains integer k_2 (k_1 + k_2 = n), the number of the second soldier's cards. Then follow k_2 integers that are the values on the second soldier's cards, from top to bottom of his stack.\n\nAll card values are different.\n\n\n-----Output-----\n\nIf somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.\n\nIf the game won't end and will continue forever output - 1.\n\n\n-----Examples-----\nInput\n4\n2 1 3\n2 4 2\n\nOutput\n6 2\nInput\n3\n1 2\n2 1 3\n\nOutput\n-1\n\n\n-----Note-----\n\nFirst sample: [Image] \n\nSecond sample: [Image]"} +{"id": "00171", "text": "You have probably registered on Internet sites many times. And each time you should enter your invented password. Usually the registration form automatically checks the password's crypt resistance. If the user's password isn't complex enough, a message is displayed. Today your task is to implement such an automatic check.\n\nWeb-developers of the company Q assume that a password is complex enough, if it meets all of the following conditions: the password length is at least 5 characters; the password contains at least one large English letter; the password contains at least one small English letter; the password contains at least one digit. \n\nYou are given a password. Please implement the automatic check of its complexity for company Q.\n\n\n-----Input-----\n\nThe first line contains a non-empty sequence of characters (at most 100 characters). Each character is either a large English letter, or a small English letter, or a digit, or one of characters: \"!\", \"?\", \".\", \",\", \"_\".\n\n\n-----Output-----\n\nIf the password is complex enough, print message \"Correct\" (without the quotes), otherwise print message \"Too weak\" (without the quotes).\n\n\n-----Examples-----\nInput\nabacaba\n\nOutput\nToo weak\n\nInput\nX12345\n\nOutput\nToo weak\n\nInput\nCONTEST_is_STARTED!!11\n\nOutput\nCorrect"} +{"id": "00172", "text": "In Berland each high school student is characterized by academic performance \u2014 integer value between 1 and 5.\n\nIn high school 0xFF there are two groups of pupils: the group A and the group B. Each group consists of exactly n students. An academic performance of each student is known \u2014 integer value between 1 and 5.\n\nThe school director wants to redistribute students between groups so that each of the two groups has the same number of students whose academic performance is equal to 1, the same number of students whose academic performance is 2 and so on. In other words, the purpose of the school director is to change the composition of groups, so that for each value of academic performance the numbers of students in both groups are equal.\n\nTo achieve this, there is a plan to produce a series of exchanges of students between groups. During the single exchange the director selects one student from the class A and one student of class B. After that, they both change their groups.\n\nPrint the least number of exchanges, in order to achieve the desired equal numbers of students for each academic performance.\n\n\n-----Input-----\n\nThe first line of the input contains integer number n (1 \u2264 n \u2264 100) \u2014 number of students in both groups.\n\nThe second line contains sequence of integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 5), where a_{i} is academic performance of the i-th student of the group A.\n\nThe third line contains sequence of integer numbers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 5), where b_{i} is academic performance of the i-th student of the group B.\n\n\n-----Output-----\n\nPrint the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained.\n\n\n-----Examples-----\nInput\n4\n5 4 4 4\n5 5 4 5\n\nOutput\n1\n\nInput\n6\n1 1 1 1 1 1\n5 5 5 5 5 5\n\nOutput\n3\n\nInput\n1\n5\n3\n\nOutput\n-1\n\nInput\n9\n3 2 5 5 2 3 3 3 2\n4 1 4 1 1 2 4 4 1\n\nOutput\n4"} +{"id": "00173", "text": "Imagine a city with n horizontal streets crossing m vertical streets, forming an (n - 1) \u00d7 (m - 1) grid. In order to increase the traffic flow, mayor of the city has decided to make each street one way. This means in each horizontal street, the traffic moves only from west to east or only from east to west. Also, traffic moves only from north to south or only from south to north in each vertical street. It is possible to enter a horizontal street from a vertical street, or vice versa, at their intersection.\n\n [Image] \n\nThe mayor has received some street direction patterns. Your task is to check whether it is possible to reach any junction from any other junction in the proposed street direction pattern.\n\n\n-----Input-----\n\nThe first line of input contains two integers n and m, (2 \u2264 n, m \u2264 20), denoting the number of horizontal streets and the number of vertical streets.\n\nThe second line contains a string of length n, made of characters '<' and '>', denoting direction of each horizontal street. If the i-th character is equal to '<', the street is directed from east to west otherwise, the street is directed from west to east. Streets are listed in order from north to south.\n\nThe third line contains a string of length m, made of characters '^' and 'v', denoting direction of each vertical street. If the i-th character is equal to '^', the street is directed from south to north, otherwise the street is directed from north to south. Streets are listed in order from west to east.\n\n\n-----Output-----\n\nIf the given pattern meets the mayor's criteria, print a single line containing \"YES\", otherwise print a single line containing \"NO\".\n\n\n-----Examples-----\nInput\n3 3\n><>\nv^v\n\nOutput\nNO\n\nInput\n4 6\n<><>\nv^v^v^\n\nOutput\nYES\n\n\n\n-----Note-----\n\nThe figure above shows street directions in the second sample test case."} +{"id": "00174", "text": "Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false. \n\nImplication is written by using character '$\\rightarrow$', and the arguments and the result of the implication are written as '0' (false) and '1' (true). According to the definition of the implication: \n\n$0 \\rightarrow 0 = 1$ \n\n$0 \\rightarrow 1 = 1$\n\n$1 \\rightarrow 0 = 0$ \n\n$1 \\rightarrow 1 = 1$\n\nWhen a logical expression contains multiple implications, then when there are no brackets, it will be calculated from left to fight. For example,\n\n$0 \\rightarrow 0 \\rightarrow 0 =(0 \\rightarrow 0) \\rightarrow 0 = 1 \\rightarrow 0 = 0$. \n\nWhen there are brackets, we first calculate the expression in brackets. For example,\n\n$0 \\rightarrow(0 \\rightarrow 0) = 0 \\rightarrow 1 = 1$.\n\nFor the given logical expression $a_{1} \\rightarrow a_{2} \\rightarrow a_{3} \\rightarrow \\cdots \\cdots a_{n}$ determine if it is possible to place there brackets so that the value of a logical expression is false. If it is possible, your task is to find such an arrangement of brackets.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100 000) \u2014 the number of arguments in a logical expression.\n\nThe second line contains n numbers a_1, a_2, ..., a_{n} ($a_{i} \\in \\{0,1 \\}$), which means the values of arguments in the expression in the order they occur.\n\n\n-----Output-----\n\nPrint \"NO\" (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0.\n\nOtherwise, print \"YES\" in the first line and the logical expression with the required arrangement of brackets in the second line.\n\nThe expression should only contain characters '0', '1', '-' (character with ASCII code 45), '>' (character with ASCII code 62), '(' and ')'. Characters '-' and '>' can occur in an expression only paired like that: (\"->\") and represent implication. The total number of logical arguments (i.e. digits '0' and '1') in the expression must be equal to n. The order in which the digits follow in the expression from left to right must coincide with a_1, a_2, ..., a_{n}.\n\nThe expression should be correct. More formally, a correct expression is determined as follows: Expressions \"0\", \"1\" (without the quotes) are correct. If v_1, v_2 are correct, then v_1->v_2 is a correct expression. If v is a correct expression, then (v) is a correct expression. \n\nThe total number of characters in the resulting expression mustn't exceed 10^6.\n\nIf there are multiple possible answers, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n4\n0 1 1 0\n\nOutput\nYES\n(((0)->1)->(1->0))\n\nInput\n2\n1 1\n\nOutput\nNO\n\nInput\n1\n0\n\nOutput\nYES\n0"} +{"id": "00175", "text": "You have two variables a and b. Consider the following sequence of actions performed with these variables: If a = 0 or b = 0, end the process. Otherwise, go to step 2; If a \u2265 2\u00b7b, then set the value of a to a - 2\u00b7b, and repeat step 1. Otherwise, go to step 3; If b \u2265 2\u00b7a, then set the value of b to b - 2\u00b7a, and repeat step 1. Otherwise, end the process.\n\nInitially the values of a and b are positive integers, and so the process will be finite.\n\nYou have to determine the values of a and b after the process ends.\n\n\n-----Input-----\n\nThe only line of the input contains two integers n and m (1 \u2264 n, m \u2264 10^18). n is the initial value of variable a, and m is the initial value of variable b.\n\n\n-----Output-----\n\nPrint two integers \u2014 the values of a and b after the end of the process.\n\n\n-----Examples-----\nInput\n12 5\n\nOutput\n0 1\n\nInput\n31 12\n\nOutput\n7 12\n\n\n\n-----Note-----\n\nExplanations to the samples: a = 12, b = 5 $\\rightarrow$ a = 2, b = 5 $\\rightarrow$ a = 2, b = 1 $\\rightarrow$ a = 0, b = 1; a = 31, b = 12 $\\rightarrow$ a = 7, b = 12."} +{"id": "00176", "text": "Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a \u2264 x \u2264 b and x is divisible by k.\n\n\n-----Input-----\n\nThe only line contains three space-separated integers k, a and b (1 \u2264 k \u2264 10^18; - 10^18 \u2264 a \u2264 b \u2264 10^18).\n\n\n-----Output-----\n\nPrint the required number.\n\n\n-----Examples-----\nInput\n1 1 10\n\nOutput\n10\n\nInput\n2 -4 4\n\nOutput\n5"} +{"id": "00177", "text": "Let's write all the positive integer numbers one after another from $1$ without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...\n\nYour task is to print the $k$-th digit of this sequence.\n\n\n-----Input-----\n\nThe first and only line contains integer $k$ ($1 \\le k \\le 10000$) \u2014 the position to process ($1$-based index).\n\n\n-----Output-----\n\nPrint the $k$-th digit of the resulting infinite sequence.\n\n\n-----Examples-----\nInput\n7\n\nOutput\n7\n\nInput\n21\n\nOutput\n5"} +{"id": "00178", "text": "A telephone number is a sequence of exactly $11$ digits such that its first digit is 8.\n\nVasya and Petya are playing a game. Initially they have a string $s$ of length $n$ ($n$ is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a character and erase it from the current string. For example, if the current string 1121, after the player's move it may be 112, 111 or 121. The game ends when the length of string $s$ becomes 11. If the resulting string is a telephone number, Vasya wins, otherwise Petya wins.\n\nYou have to determine if Vasya has a winning strategy (that is, if Vasya can win the game no matter which characters Petya chooses during his moves).\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($13 \\le n < 10^5$, $n$ is odd) \u2014 the length of string $s$.\n\nThe second line contains the string $s$ ($|s| = n$) consisting only of decimal digits.\n\n\n-----Output-----\n\nIf Vasya has a strategy that guarantees him victory, print YES.\n\nOtherwise print NO.\n\n\n-----Examples-----\nInput\n13\n8380011223344\n\nOutput\nYES\n\nInput\n15\n807345619350641\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example Vasya needs to erase the second character. Then Petya cannot erase a character from the remaining string 880011223344 so that it does not become a telephone number.\n\nIn the second example after Vasya's turn Petya can erase one character character 8. The resulting string can't be a telephone number, because there is no digit 8 at all."} +{"id": "00179", "text": "Andrey thinks he is truly a successful developer, but in reality he didn't know about the binary search algorithm until recently. After reading some literature Andrey understood that this algorithm allows to quickly find a certain number $x$ in an array. For an array $a$ indexed from zero, and an integer $x$ the pseudocode of the algorithm is as follows:\n\nBinarySearch(a, x)\n left = 0\n right = a.size()\n while left < right\n middle = (left + right) / 2\n if a[middle] <= x then\n left = middle + 1\n else\n right = middle\n \n if left > 0 and a[left - 1] == x then\n return true\n else\n return false\n\nNote that the elements of the array are indexed from zero, and the division is done in integers (rounding down).\n\nAndrey read that the algorithm only works if the array is sorted. However, he found this statement untrue, because there certainly exist unsorted arrays for which the algorithm find $x$!\n\nAndrey wants to write a letter to the book authors, but before doing that he must consider the permutations of size $n$ such that the algorithm finds $x$ in them. A permutation of size $n$ is an array consisting of $n$ distinct integers between $1$ and $n$ in arbitrary order.\n\nHelp Andrey and find the number of permutations of size $n$ which contain $x$ at position $pos$ and for which the given implementation of the binary search algorithm finds $x$ (returns true). As the result may be extremely large, print the remainder of its division by $10^9+7$.\n\n\n-----Input-----\n\nThe only line of input contains integers $n$, $x$ and $pos$ ($1 \\le x \\le n \\le 1000$, $0 \\le pos \\le n - 1$) \u2014 the required length of the permutation, the number to search, and the required position of that number, respectively.\n\n\n-----Output-----\n\nPrint a single number\u00a0\u2014 the remainder of the division of the number of valid permutations by $10^9+7$.\n\n\n-----Examples-----\nInput\n4 1 2\n\nOutput\n6\n\nInput\n123 42 24\n\nOutput\n824071958\n\n\n\n-----Note-----\n\nAll possible permutations in the first test case: $(2, 3, 1, 4)$, $(2, 4, 1, 3)$, $(3, 2, 1, 4)$, $(3, 4, 1, 2)$, $(4, 2, 1, 3)$, $(4, 3, 1, 2)$."} +{"id": "00180", "text": "Andrey received a postcard from Irina. It contained only the words \"Hello, Andrey!\", and a strange string consisting of lowercase Latin letters, snowflakes and candy canes. Andrey thought that this string is an encrypted message, and decided to decrypt it.\n\nAndrey noticed that snowflakes and candy canes always stand after the letters, so he supposed that the message was encrypted as follows. Candy cane means that the letter before it can be removed, or can be left. A snowflake means that the letter before it can be removed, left, or repeated several times.\n\nFor example, consider the following string: [Image] \n\nThis string can encode the message \u00abhappynewyear\u00bb. For this, candy canes and snowflakes should be used as follows: candy cane 1: remove the letter w, snowflake 1: repeat the letter p twice, candy cane 2: leave the letter n, snowflake 2: remove the letter w, snowflake 3: leave the letter e. \n\n [Image] \n\nPlease note that the same string can encode different messages. For example, the string above can encode \u00abhayewyar\u00bb, \u00abhapppppynewwwwwyear\u00bb, and other messages.\n\nAndrey knows that messages from Irina usually have a length of $k$ letters. Help him to find out if a given string can encode a message of $k$ letters, and if so, give an example of such a message.\n\n\n-----Input-----\n\nThe first line contains the string received in the postcard. The string consists only of lowercase Latin letters, as well as the characters \u00ab*\u00bb and \u00ab?\u00bb, meaning snowflake and candy cone, respectively. These characters can only appear immediately after the letter. The length of the string does not exceed $200$.\n\nThe second line contains an integer number $k$ ($1 \\leq k \\leq 200$), the required message length.\n\n\n-----Output-----\n\nPrint any message of length $k$ that the given string can encode, or \u00abImpossible\u00bb if such a message does not exist.\n\n\n-----Examples-----\nInput\nhw?ap*yn?eww*ye*ar\n12\n\nOutput\nhappynewyear\n\nInput\nab?a\n2\n\nOutput\naa\nInput\nab?a\n3\n\nOutput\naba\nInput\nababb\n5\n\nOutput\nababb\nInput\nab?a\n1\n\nOutput\nImpossible"} +{"id": "00181", "text": "Vasya started working in a machine vision company of IT City. Vasya's team creates software and hardware for identification of people by their face.\n\nOne of the project's know-how is a camera rotating around its optical axis on shooting. People see an eye-catching gadget \u2014 a rotating camera \u2014 come up to it to see it better, look into it. And the camera takes their photo at that time. What could be better for high quality identification?\n\nBut not everything is so simple. The pictures from camera appear rotated too (on clockwise camera rotation frame the content becomes rotated counter-clockwise). But the identification algorithm can work only with faces that are just slightly deviated from vertical.\n\nVasya was entrusted to correct the situation \u2014 to rotate a captured image so that image would be minimally deviated from vertical. Requirements were severe. Firstly, the picture should be rotated only on angle divisible by 90 degrees to not lose a bit of information about the image. Secondly, the frames from the camera are so huge and FPS is so big that adequate rotation speed is provided by hardware FPGA solution only. And this solution can rotate only by 90 degrees clockwise. Of course, one can apply 90 degrees turn several times but for the sake of performance the number of turns should be minimized.\n\nHelp Vasya implement the program that by the given rotation angle of the camera can determine the minimum number of 90 degrees clockwise turns necessary to get a picture in which up direction deviation from vertical is minimum.\n\nThe next figure contains frames taken from an unrotated camera, then from rotated 90 degrees clockwise, then from rotated 90 degrees counter-clockwise. Arrows show direction to \"true up\". [Image] \n\nThe next figure shows 90 degrees clockwise turn by FPGA hardware. [Image] \n\n\n-----Input-----\n\nThe only line of the input contains one integer x ( - 10^18 \u2264 x \u2264 10^18) \u2014 camera angle in degrees. Positive value denotes clockwise camera rotation, negative \u2014 counter-clockwise.\n\n\n-----Output-----\n\nOutput one integer \u2014 the minimum required number of 90 degrees clockwise turns.\n\n\n-----Examples-----\nInput\n60\n\nOutput\n1\n\nInput\n-60\n\nOutput\n3\n\n\n\n-----Note-----\n\nWhen the camera is rotated 60 degrees counter-clockwise (the second example), an image from it is rotated 60 degrees clockwise. One 90 degrees clockwise turn of the image result in 150 degrees clockwise total rotation and deviation from \"true up\" for one turn is 150 degrees. Two 90 degrees clockwise turns of the image result in 240 degrees clockwise total rotation and deviation from \"true up\" for two turns is 120 degrees because 240 degrees clockwise equal to 120 degrees counter-clockwise. Three 90 degrees clockwise turns of the image result in 330 degrees clockwise total rotation and deviation from \"true up\" for three turns is 30 degrees because 330 degrees clockwise equal to 30 degrees counter-clockwise.\n\nFrom 60, 150, 120 and 30 degrees deviations the smallest is 30, and it it achieved in three 90 degrees clockwise turns."} +{"id": "00182", "text": "Carl is a beginner magician. He has a blue, b violet and c orange magic spheres. In one move he can transform two spheres of the same color into one sphere of any other color. To make a spell that has never been seen before, he needs at least x blue, y violet and z orange spheres. Can he get them (possible, in multiple actions)?\n\n\n-----Input-----\n\nThe first line of the input contains three integers a, b and c (0 \u2264 a, b, c \u2264 1 000 000)\u00a0\u2014 the number of blue, violet and orange spheres that are in the magician's disposal.\n\nThe second line of the input contains three integers, x, y and z (0 \u2264 x, y, z \u2264 1 000 000)\u00a0\u2014 the number of blue, violet and orange spheres that he needs to get.\n\n\n-----Output-----\n\nIf the wizard is able to obtain the required numbers of spheres, print \"Yes\". Otherwise, print \"No\".\n\n\n-----Examples-----\nInput\n4 4 0\n2 1 2\n\nOutput\nYes\n\nInput\n5 6 1\n2 7 2\n\nOutput\nNo\n\nInput\n3 3 3\n2 2 2\n\nOutput\nYes\n\n\n\n-----Note-----\n\nIn the first sample the wizard has 4 blue and 4 violet spheres. In his first action he can turn two blue spheres into one violet one. After that he will have 2 blue and 5 violet spheres. Then he turns 4 violet spheres into 2 orange spheres and he ends up with 2 blue, 1 violet and 2 orange spheres, which is exactly what he needs."} +{"id": "00183", "text": "Amr doesn't like Maths as he finds it really boring, so he usually sleeps in Maths lectures. But one day the teacher suspected that Amr is sleeping and asked him a question to make sure he wasn't.\n\nFirst he gave Amr two positive integers n and k. Then he asked Amr, how many integer numbers x > 0 exist such that: Decimal representation of x (without leading zeroes) consists of exactly n digits; There exists some integer y > 0 such that: $y \\operatorname{mod} k = 0$; decimal representation of y is a suffix of decimal representation of x. \n\nAs the answer to this question may be pretty huge the teacher asked Amr to output only its remainder modulo a number m.\n\nCan you help Amr escape this embarrassing situation?\n\n\n-----Input-----\n\nInput consists of three integers n, k, m (1 \u2264 n \u2264 1000, 1 \u2264 k \u2264 100, 1 \u2264 m \u2264 10^9).\n\n\n-----Output-----\n\nPrint the required number modulo m.\n\n\n-----Examples-----\nInput\n1 2 1000\n\nOutput\n4\nInput\n2 2 1000\n\nOutput\n45\nInput\n5 3 1103\n\nOutput\n590\n\n\n-----Note-----\n\nA suffix of a string S is a non-empty string that can be obtained by removing some number (possibly, zero) of first characters from S."} +{"id": "00184", "text": "You are at a water bowling training. There are l people who play with their left hand, r people, who play with their right hand, and a ambidexters, who can play with left or right hand.\n\nThe coach decided to form a team of even number of players, exactly half of the players should play with their right hand, and exactly half of the players should play with their left hand. One player should use only on of his hands.\n\nAmbidexters play as well with their right hand as with their left hand. In the team, an ambidexter can play with their left hand, or with their right hand.\n\nPlease find the maximum possible size of the team, where equal number of players use their left and right hands, respectively.\n\n\n-----Input-----\n\nThe only line contains three integers l, r and a (0 \u2264 l, r, a \u2264 100) \u2014 the number of left-handers, the number of right-handers and the number of ambidexters at the training. \n\n\n-----Output-----\n\nPrint a single even integer\u00a0\u2014 the maximum number of players in the team. It is possible that the team can only have zero number of players.\n\n\n-----Examples-----\nInput\n1 4 2\n\nOutput\n6\n\nInput\n5 5 5\n\nOutput\n14\n\nInput\n0 2 0\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example you can form a team of 6 players. You should take the only left-hander and two ambidexters to play with left hand, and three right-handers to play with right hand. The only person left can't be taken into the team.\n\nIn the second example you can form a team of 14 people. You have to take all five left-handers, all five right-handers, two ambidexters to play with left hand and two ambidexters to play with right hand."} +{"id": "00185", "text": "Finished her homework, Nastya decided to play computer games. Passing levels one by one, Nastya eventually faced a problem. Her mission is to leave a room, where a lot of monsters live, as quickly as possible.\n\nThere are $n$ manholes in the room which are situated on one line, but, unfortunately, all the manholes are closed, and there is one stone on every manhole. There is exactly one coin under every manhole, and to win the game Nastya should pick all the coins. Initially Nastya stands near the $k$-th manhole from the left. She is thinking what to do.\n\nIn one turn, Nastya can do one of the following: if there is at least one stone on the manhole Nastya stands near, throw exactly one stone from it onto any other manhole (yes, Nastya is strong). go to a neighboring manhole; if there are no stones on the manhole Nastya stays near, she can open it and pick the coin from it. After it she must close the manhole immediately (it doesn't require additional moves). \n\n [Image] The figure shows the intermediate state of the game. At the current position Nastya can throw the stone to any other manhole or move left or right to the neighboring manholes. If she were near the leftmost manhole, she could open it (since there are no stones on it). \n\nNastya can leave the room when she picks all the coins. Monsters are everywhere, so you need to compute the minimum number of moves Nastya has to make to pick all the coins.\n\nNote one time more that Nastya can open a manhole only when there are no stones onto it.\n\n\n-----Input-----\n\nThe first and only line contains two integers $n$ and $k$, separated by space ($2 \\leq n \\leq 5000$, $1 \\leq k \\leq n$)\u00a0\u2014 the number of manholes and the index of manhole from the left, near which Nastya stays initially. Initially there is exactly one stone near each of the $n$ manholes. \n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 minimum number of moves which lead Nastya to pick all the coins.\n\n\n-----Examples-----\nInput\n2 2\n\nOutput\n6\n\nInput\n4 2\n\nOutput\n13\n\nInput\n5 1\n\nOutput\n15\n\n\n\n-----Note-----\n\nLet's consider the example where $n = 2$, $k = 2$. Nastya should play as follows:\n\n At first she throws the stone from the second manhole to the first. Now there are two stones on the first manhole. Then she opens the second manhole and pick the coin from it. Then she goes to the first manhole, throws two stones by two moves to the second manhole and then opens the manhole and picks the coin from it. \n\nSo, $6$ moves are required to win."} +{"id": "00186", "text": "Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.\n\nThe students don\u2019t want to use too many blocks, but they also want to be unique, so no two students\u2019 towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and m (0 \u2264 n, m \u2264 1 000 000, n + m > 0)\u00a0\u2014 the number of students using two-block pieces and the number of students using three-block pieces, respectively.\n\n\n-----Output-----\n\nPrint a single integer, denoting the minimum possible height of the tallest tower.\n\n\n-----Examples-----\nInput\n1 3\n\nOutput\n9\n\nInput\n3 2\n\nOutput\n8\n\nInput\n5 0\n\nOutput\n10\n\n\n\n-----Note-----\n\nIn the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.\n\nIn the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks."} +{"id": "00187", "text": "Petya and Vasya decided to play a game. They have n cards (n is an even number). A single integer is written on each card.\n\nBefore the game Petya will choose an integer and after that Vasya will choose another integer (different from the number that Petya chose). During the game each player takes all the cards with number he chose. For example, if Petya chose number 5 before the game he will take all cards on which 5 is written and if Vasya chose number 10 before the game he will take all cards on which 10 is written.\n\nThe game is considered fair if Petya and Vasya can take all n cards, and the number of cards each player gets is the same.\n\nDetermine whether Petya and Vasya can choose integer numbers before the game so that the game is fair. \n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 100) \u2014 number of cards. It is guaranteed that n is an even number.\n\nThe following n lines contain a sequence of integers a_1, a_2, ..., a_{n} (one integer per line, 1 \u2264 a_{i} \u2264 100) \u2014 numbers written on the n cards.\n\n\n-----Output-----\n\nIf it is impossible for Petya and Vasya to choose numbers in such a way that the game will be fair, print \"NO\" (without quotes) in the first line. In this case you should not print anything more.\n\nIn the other case print \"YES\" (without quotes) in the first line. In the second line print two distinct integers \u2014 number that Petya should choose and the number that Vasya should choose to make the game fair. If there are several solutions, print any of them.\n\n\n-----Examples-----\nInput\n4\n11\n27\n27\n11\n\nOutput\nYES\n11 27\n\nInput\n2\n6\n6\n\nOutput\nNO\n\nInput\n6\n10\n20\n30\n20\n10\n20\n\nOutput\nNO\n\nInput\n6\n1\n1\n2\n2\n3\n3\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example the game will be fair if, for example, Petya chooses number 11, and Vasya chooses number 27. Then the will take all cards \u2014 Petya will take cards 1 and 4, and Vasya will take cards 2 and 3. Thus, each of them will take exactly two cards.\n\nIn the second example fair game is impossible because the numbers written on the cards are equal, but the numbers that Petya and Vasya should choose should be distinct.\n\nIn the third example it is impossible to take all cards. Petya and Vasya can take at most five cards \u2014 for example, Petya can choose number 10 and Vasya can choose number 20. But for the game to be fair it is necessary to take 6 cards."} +{"id": "00188", "text": "Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains a_{i} soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}.\n\n [Image] A row in the airplane \n\nDaenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats.\n\nYour task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 10000, 1 \u2264 k \u2264 100)\u00a0\u2014 the number of rows and the number of groups of soldiers, respectively.\n\nThe second line contains k integers a_1, a_2, a_3, ..., a_{k} (1 \u2264 a_{i} \u2264 10000), where a_{i} denotes the number of soldiers in the i-th group.\n\nIt is guaranteed that a_1 + a_2 + ... + a_{k} \u2264 8\u00b7n.\n\n\n-----Output-----\n\nIf we can place the soldiers in the airplane print \"YES\" (without quotes). Otherwise print \"NO\" (without quotes).\n\nYou can choose the case (lower or upper) for each letter arbitrary.\n\n\n-----Examples-----\nInput\n2 2\n5 8\n\nOutput\nYES\n\nInput\n1 2\n7 1\n\nOutput\nNO\n\nInput\n1 2\n4 4\n\nOutput\nYES\n\nInput\n1 4\n2 2 1 2\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample, Daenerys can place the soldiers like in the figure below:\n\n [Image] \n\nIn the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group.\n\nIn the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats.\n\nIn the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6)."} +{"id": "00189", "text": "Salem gave you $n$ sticks with integer positive lengths $a_1, a_2, \\ldots, a_n$.\n\nFor every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from $a$ to $b$ is $|a - b|$, where $|x|$ means the absolute value of $x$.\n\nA stick length $a_i$ is called almost good for some integer $t$ if $|a_i - t| \\le 1$.\n\nSalem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer $t$ and the total cost of changing is minimum possible. The value of $t$ is not fixed in advance and you can choose it as any positive integer. \n\nAs an answer, print the value of $t$ and the minimum cost. If there are multiple optimal choices for $t$, print any of them.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 1000$)\u00a0\u2014 the number of sticks.\n\nThe second line contains $n$ integers $a_i$ ($1 \\le a_i \\le 100$)\u00a0\u2014 the lengths of the sticks.\n\n\n-----Output-----\n\nPrint the value of $t$ and the minimum possible cost. If there are multiple optimal choices for $t$, print any of them.\n\n\n-----Examples-----\nInput\n3\n10 1 4\n\nOutput\n3 7\n\nInput\n5\n1 1 2 2 3\n\nOutput\n2 0\n\n\n\n-----Note-----\n\nIn the first example, we can change $1$ into $2$ and $10$ into $4$ with cost $|1 - 2| + |10 - 4| = 1 + 6 = 7$ and the resulting lengths $[2, 4, 4]$ are almost good for $t = 3$.\n\nIn the second example, the sticks lengths are already almost good for $t = 2$, so we don't have to do anything."} +{"id": "00190", "text": "\u041a\u0430\u0440\u0442\u0430 \u0437\u0432\u0451\u0437\u0434\u043d\u043e\u0433\u043e \u043d\u0435\u0431\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0438\u0437 n \u0441\u0442\u0440\u043e\u043a \u043f\u043e m \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435. \u041a\u0430\u0436\u0434\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b\u00a0\u2014 \u044d\u0442\u043e \u043b\u0438\u0431\u043e \u00ab.\u00bb (\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043d\u0435\u0431\u0430), \u043b\u0438\u0431\u043e \u00ab*\u00bb (\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0442\u043e, \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043d\u0430 \u043d\u0435\u0431\u0435 \u0435\u0441\u0442\u044c \u0437\u0432\u0435\u0437\u0434\u0430). \n\n\u041d\u043e\u0432\u043e\u0435 \u0438\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u0430\u0440\u0442\u044b \u0437\u0432\u0451\u0437\u0434\u043d\u043e\u0433\u043e \u043d\u0435\u0431\u0430 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u043d\u043e \u043d\u0430 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u0445 \u043b\u0438\u0441\u0442\u0430\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c\u0441\u044f \u0432\u0441\u0435 \u0437\u0432\u0435\u0437\u0434\u044b. \u0413\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u0441\u043a\u043e\u043c\u043e\u0433\u043e \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b \u0441\u0442\u043e\u0440\u043e\u043d\u0430\u043c \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044f.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430 n \u0438 m (1 \u2264 n, m \u2264 1000)\u00a0\u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u043d\u0430 \u043a\u0430\u0440\u0442\u0435 \u0437\u0432\u0435\u0437\u0434\u043d\u043e\u0433\u043e \u043d\u0435\u0431\u0430.\n\n\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 n \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0437\u0430\u0434\u0430\u043d\u043e \u043f\u043e m \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. \u041a\u0430\u0436\u0434\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b\u00a0\u2014 \u044d\u0442\u043e \u043b\u0438\u0431\u043e \u00ab.\u00bb (\u043f\u0443\u0441\u0442\u043e\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043d\u0435\u0431\u0430), \u043b\u0438\u0431\u043e \u00ab*\u00bb (\u0437\u0432\u0435\u0437\u0434\u0430).\n\n\u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430 \u043d\u0435\u0431\u0435 \u0435\u0441\u0442\u044c \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u0430 \u0437\u0432\u0435\u0437\u0434\u0430.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043e\u0434\u043d\u043e \u0447\u0438\u0441\u043b\u043e \u2014 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043a\u0440\u044b\u0442\u044c \u0432\u0441\u0435 \u0437\u0432\u0435\u0437\u0434\u044b.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n4 4\n....\n..*.\n...*\n..**\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1 3\n*.*\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2 1\n.\n*\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u041e\u0434\u0438\u043d \u0438\u0437 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:\n\n [Image] \n\n\u041e\u0434\u0438\u043d \u0438\u0437 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 (\u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043a\u0430\u0440\u0442\u044b \u0437\u0432\u0435\u0437\u0434\u043d\u043e\u0433\u043e \u043d\u0435\u0431\u0430):\n\n [Image] \n\n\u041e\u0442\u0432\u0435\u0442 \u043d\u0430 \u0442\u0440\u0435\u0442\u0438\u0439 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:\n\n [Image]"} +{"id": "00191", "text": "A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.\n\nGiven an array $a$ of length $n$, consisting only of the numbers $0$ and $1$, and the number $k$. Exactly $k$ times the following happens: Two numbers $i$ and $j$ are chosen equiprobable such that ($1 \\leq i < j \\leq n$). The numbers in the $i$ and $j$ positions are swapped. \n\nSonya's task is to find the probability that after all the operations are completed, the $a$ array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.\n\nIt can be shown that the desired probability is either $0$ or it can be represented as $\\dfrac{P}{Q}$, where $P$ and $Q$ are coprime integers and $Q \\not\\equiv 0~\\pmod {10^9+7}$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($2 \\leq n \\leq 100, 1 \\leq k \\leq 10^9$)\u00a0\u2014 the length of the array $a$ and the number of operations.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 1$)\u00a0\u2014 the description of the array $a$.\n\n\n-----Output-----\n\nIf the desired probability is $0$, print $0$, otherwise print the value $P \\cdot Q^{-1}$ $\\pmod {10^9+7}$, where $P$ and $Q$ are defined above.\n\n\n-----Examples-----\nInput\n3 2\n0 1 0\n\nOutput\n333333336\nInput\n5 1\n1 1 1 0 0\n\nOutput\n0\nInput\n6 4\n1 0 0 1 1 0\n\nOutput\n968493834\n\n\n-----Note-----\n\nIn the first example, all possible variants of the final array $a$, after applying exactly two operations: $(0, 1, 0)$, $(0, 0, 1)$, $(1, 0, 0)$, $(1, 0, 0)$, $(0, 1, 0)$, $(0, 0, 1)$, $(0, 0, 1)$, $(1, 0, 0)$, $(0, 1, 0)$. Therefore, the answer is $\\dfrac{3}{9}=\\dfrac{1}{3}$.\n\nIn the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is $0$."} +{"id": "00192", "text": "Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length x, and he wishes to perform operations to obtain an equilateral triangle of side length y.\n\nIn a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.\n\nWhat is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y?\n\n\n-----Input-----\n\nThe first and only line contains two integers x and y (3 \u2264 y < x \u2264 100 000)\u00a0\u2014 the starting and ending equilateral triangle side lengths respectively.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of seconds required for Memory to obtain the equilateral triangle of side length y if he starts with the equilateral triangle of side length x.\n\n\n-----Examples-----\nInput\n6 3\n\nOutput\n4\n\nInput\n8 5\n\nOutput\n3\n\nInput\n22 4\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides a, b, and c as (a, b, c). Then, Memory can do $(6,6,6) \\rightarrow(6,6,3) \\rightarrow(6,4,3) \\rightarrow(3,4,3) \\rightarrow(3,3,3)$.\n\nIn the second sample test, Memory can do $(8,8,8) \\rightarrow(8,8,5) \\rightarrow(8,5,5) \\rightarrow(5,5,5)$.\n\nIn the third sample test, Memory can do: $(22,22,22) \\rightarrow(7,22,22) \\rightarrow(7,22,16) \\rightarrow(7,10,16) \\rightarrow(7,10,4) \\rightarrow$\n\n$(7,4,4) \\rightarrow(4,4,4)$."} +{"id": "00193", "text": "The determinant of a matrix 2 \u00d7 2 is defined as follows:$\\operatorname{det} \\left(\\begin{array}{ll}{a} & {b} \\\\{c} & {d} \\end{array} \\right) = a d - b c$\n\nA matrix is called degenerate if its determinant is equal to zero. \n\nThe norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.\n\nYou are given a matrix $A = \\left(\\begin{array}{ll}{a} & {b} \\\\{c} & {d} \\end{array} \\right)$. Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine ||A - B||.\n\n\n-----Input-----\n\nThe first line contains two integers a and b (|a|, |b| \u2264 10^9), the elements of the first row of matrix A. \n\nThe second line contains two integers c and d (|c|, |d| \u2264 10^9) the elements of the second row of matrix A.\n\n\n-----Output-----\n\nOutput a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10^{ - 9}.\n\n\n-----Examples-----\nInput\n1 2\n3 4\n\nOutput\n0.2000000000\n\nInput\n1 0\n0 1\n\nOutput\n0.5000000000\n\n\n\n-----Note-----\n\nIn the first sample matrix B is $\\left(\\begin{array}{ll}{1.2} & {1.8} \\\\{2.8} & {4.2} \\end{array} \\right)$\n\nIn the second sample matrix B is $\\left(\\begin{array}{ll}{0.5} & {0.5} \\\\{0.5} & {0.5} \\end{array} \\right)$"} +{"id": "00194", "text": "In a small restaurant there are a tables for one person and b tables for two persons. \n\nIt it known that n groups of people come today, each consisting of one or two people. \n\nIf a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.\n\nIf a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.\n\nYou are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.\n\n\n-----Input-----\n\nThe first line contains three integers n, a and b (1 \u2264 n \u2264 2\u00b710^5, 1 \u2264 a, b \u2264 2\u00b710^5) \u2014 the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.\n\nThe second line contains a sequence of integers t_1, t_2, ..., t_{n} (1 \u2264 t_{i} \u2264 2) \u2014 the description of clients in chronological order. If t_{i} is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.\n\n\n-----Output-----\n\nPrint the total number of people the restaurant denies service to.\n\n\n-----Examples-----\nInput\n4 1 2\n1 2 1 1\n\nOutput\n0\n\nInput\n4 1 1\n1 1 2 1\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.\n\nIn the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients."} +{"id": "00195", "text": "Each student eagerly awaits the day he would pass the exams successfully. Thus, Vasya was ready to celebrate, but, alas, he didn't pass it. However, many of Vasya's fellow students from the same group were more successful and celebrated after the exam.\n\nSome of them celebrated in the BugDonalds restaurant, some of them\u00a0\u2014 in the BeaverKing restaurant, the most successful ones were fast enough to celebrate in both of restaurants. Students which didn't pass the exam didn't celebrate in any of those restaurants and elected to stay home to prepare for their reexamination. However, this quickly bored Vasya and he started checking celebration photos on the Kilogramm. He found out that, in total, BugDonalds was visited by $A$ students, BeaverKing\u00a0\u2014 by $B$ students and $C$ students visited both restaurants. Vasya also knows that there are $N$ students in his group.\n\nBased on this info, Vasya wants to determine either if his data contradicts itself or, if it doesn't, how many students in his group didn't pass the exam. Can you help him so he won't waste his valuable preparation time?\n\n\n-----Input-----\n\nThe first line contains four integers\u00a0\u2014 $A$, $B$, $C$ and $N$ ($0 \\leq A, B, C, N \\leq 100$).\n\n\n-----Output-----\n\nIf a distribution of $N$ students exists in which $A$ students visited BugDonalds, $B$ \u2014 BeaverKing, $C$ \u2014 both of the restaurants and at least one student is left home (it is known that Vasya didn't pass the exam and stayed at home), output one integer\u00a0\u2014 amount of students (including Vasya) who did not pass the exam. \n\nIf such a distribution does not exist and Vasya made a mistake while determining the numbers $A$, $B$, $C$ or $N$ (as in samples 2 and 3), output $-1$.\n\n\n-----Examples-----\nInput\n10 10 5 20\n\nOutput\n5\nInput\n2 2 0 4\n\nOutput\n-1\nInput\n2 2 2 1\n\nOutput\n-1\n\n\n-----Note-----\n\nThe first sample describes following situation: $5$ only visited BugDonalds, $5$ students only visited BeaverKing, $5$ visited both of them and $5$ students (including Vasya) didn't pass the exam.\n\nIn the second sample $2$ students only visited BugDonalds and $2$ only visited BeaverKing, but that means all $4$ students in group passed the exam which contradicts the fact that Vasya didn't pass meaning that this situation is impossible.\n\nThe third sample describes a situation where $2$ students visited BugDonalds but the group has only $1$ which makes it clearly impossible."} +{"id": "00196", "text": "Nastya received a gift on New Year\u00a0\u2014 a magic wardrobe. It is magic because in the end of each month the number of dresses in it doubles (i.e. the number of dresses becomes twice as large as it is in the beginning of the month).\n\nUnfortunately, right after the doubling the wardrobe eats one of the dresses (if any) with the 50% probability. It happens every month except the last one in the year. \n\nNastya owns x dresses now, so she became interested in the expected number of dresses she will have in one year. Nastya lives in Byteland, so the year lasts for k + 1 months.\n\nNastya is really busy, so she wants you to solve this problem. You are the programmer, after all. Also, you should find the answer modulo 10^9 + 7, because it is easy to see that it is always integer.\n\n\n-----Input-----\n\nThe only line contains two integers x and k (0 \u2264 x, k \u2264 10^18), where x is the initial number of dresses and k + 1 is the number of months in a year in Byteland.\n\n\n-----Output-----\n\nIn the only line print a single integer\u00a0\u2014 the expected number of dresses Nastya will own one year later modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n2 0\n\nOutput\n4\n\nInput\n2 1\n\nOutput\n7\n\nInput\n3 2\n\nOutput\n21\n\n\n\n-----Note-----\n\nIn the first example a year consists on only one month, so the wardrobe does not eat dresses at all.\n\nIn the second example after the first month there are 3 dresses with 50% probability and 4 dresses with 50% probability. Thus, in the end of the year there are 6 dresses with 50% probability and 8 dresses with 50% probability. This way the answer for this test is (6 + 8) / 2 = 7."} +{"id": "00197", "text": "An online contest will soon be held on ForceCoders, a large competitive programming platform. The authors have prepared $n$ problems; and since the platform is very popular, $998244351$ coder from all over the world is going to solve them.\n\nFor each problem, the authors estimated the number of people who would solve it: for the $i$-th problem, the number of accepted solutions will be between $l_i$ and $r_i$, inclusive.\n\nThe creator of ForceCoders uses different criteria to determine if the contest is good or bad. One of these criteria is the number of inversions in the problem order. An inversion is a pair of problems $(x, y)$ such that $x$ is located earlier in the contest ($x < y$), but the number of accepted solutions for $y$ is strictly greater.\n\nObviously, both the creator of ForceCoders and the authors of the contest want the contest to be good. Now they want to calculate the probability that there will be no inversions in the problem order, assuming that for each problem $i$, any integral number of accepted solutions for it (between $l_i$ and $r_i$) is equally probable, and all these numbers are independent.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 50$) \u2014 the number of problems in the contest.\n\nThen $n$ lines follow, the $i$-th line contains two integers $l_i$ and $r_i$ ($0 \\le l_i \\le r_i \\le 998244351$) \u2014 the minimum and maximum number of accepted solutions for the $i$-th problem, respectively.\n\n\n-----Output-----\n\nThe probability that there will be no inversions in the contest can be expressed as an irreducible fraction $\\frac{x}{y}$, where $y$ is coprime with $998244353$. Print one integer \u2014 the value of $xy^{-1}$, taken modulo $998244353$, where $y^{-1}$ is an integer such that $yy^{-1} \\equiv 1$ $(mod$ $998244353)$.\n\n\n-----Examples-----\nInput\n3\n1 2\n1 2\n1 2\n\nOutput\n499122177\n\nInput\n2\n42 1337\n13 420\n\nOutput\n578894053\n\nInput\n2\n1 1\n0 0\n\nOutput\n1\n\nInput\n2\n1 1\n1 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nThe real answer in the first test is $\\frac{1}{2}$."} +{"id": "00198", "text": "Pasha has a wooden stick of some positive integer length n. He wants to perform exactly three cuts to get four parts of the stick. Each part must have some positive integer length and the sum of these lengths will obviously be n. \n\nPasha likes rectangles but hates squares, so he wonders, how many ways are there to split a stick into four parts so that it's possible to form a rectangle using these parts, but is impossible to form a square.\n\nYour task is to help Pasha and count the number of such ways. Two ways to cut the stick are considered distinct if there exists some integer x, such that the number of parts of length x in the first way differ from the number of parts of length x in the second way.\n\n\n-----Input-----\n\nThe first line of the input contains a positive integer n (1 \u2264 n \u2264 2\u00b710^9) \u2014 the length of Pasha's stick.\n\n\n-----Output-----\n\nThe output should contain a single integer\u00a0\u2014 the number of ways to split Pasha's stick into four parts of positive integer length so that it's possible to make a rectangle by connecting the ends of these parts, but is impossible to form a square. \n\n\n-----Examples-----\nInput\n6\n\nOutput\n1\n\nInput\n20\n\nOutput\n4\n\n\n\n-----Note-----\n\nThere is only one way to divide the stick in the first sample {1, 1, 2, 2}.\n\nFour ways to divide the stick in the second sample are {1, 1, 9, 9}, {2, 2, 8, 8}, {3, 3, 7, 7} and {4, 4, 6, 6}. Note that {5, 5, 5, 5} doesn't work."} +{"id": "00199", "text": "The Fair Nut likes kvass very much. On his birthday parents presented him $n$ kegs of kvass. There are $v_i$ liters of kvass in the $i$-th keg. Each keg has a lever. You can pour your glass by exactly $1$ liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by $s$ liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.\n\nHelp him find out how much kvass can be in the least keg or define it's not possible to pour his glass by $s$ liters of kvass.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $s$ ($1 \\le n \\le 10^3$, $1 \\le s \\le 10^{12}$)\u00a0\u2014 the number of kegs and glass volume.\n\nThe second line contains $n$ integers $v_1, v_2, \\ldots, v_n$ ($1 \\le v_i \\le 10^9$)\u00a0\u2014 the volume of $i$-th keg.\n\n\n-----Output-----\n\nIf the Fair Nut cannot pour his glass by $s$ liters of kvass, print $-1$. Otherwise, print a single integer\u00a0\u2014 how much kvass in the least keg can be.\n\n\n-----Examples-----\nInput\n3 3\n4 3 5\n\nOutput\n3\n\nInput\n3 4\n5 3 4\n\nOutput\n2\n\nInput\n3 7\n1 2 3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example, the answer is $3$, the Fair Nut can take $1$ liter from the first keg and $2$ liters from the third keg. There are $3$ liters of kvass in each keg.\n\nIn the second example, the answer is $2$, the Fair Nut can take $3$ liters from the first keg and $1$ liter from the second keg.\n\nIn the third example, the Fair Nut can't pour his cup by $7$ liters, so the answer is $-1$."} +{"id": "00200", "text": "The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h_1 cm from the ground. On the height h_2 cm (h_2 > h_1) on the same tree hung an apple and the caterpillar was crawling to the apple.\n\nGabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.\n\nIn how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.\n\nNote that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.\n\n\n-----Input-----\n\nThe first line contains two integers h_1, h_2 (1 \u2264 h_1 < h_2 \u2264 10^5) \u2014 the heights of the position of the caterpillar and the apple in centimeters.\n\nThe second line contains two integers a, b (1 \u2264 a, b \u2264 10^5) \u2014 the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.\n\n\n-----Output-----\n\nPrint the only integer k \u2014 the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.\n\nIf the caterpillar can't get the apple print the only integer - 1.\n\n\n-----Examples-----\nInput\n10 30\n2 1\n\nOutput\n1\n\nInput\n10 13\n1 1\n\nOutput\n0\n\nInput\n10 19\n1 2\n\nOutput\n-1\n\nInput\n1 50\n5 4\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.\n\nNote that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day."} +{"id": "00201", "text": "A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] \n\nOne day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units.\n\nCandies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.\n\n\n-----Input-----\n\nThe single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 \u2264 C, H_{r}, H_{b}, W_{r}, W_{b} \u2264 10^9).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of joy units that Om Nom can get.\n\n\n-----Examples-----\nInput\n10 3 5 2 3\n\nOutput\n16\n\n\n\n-----Note-----\n\nIn the sample test Om Nom can eat two candies of each type and thus get 16 joy units."} +{"id": "00202", "text": "Professor GukiZ makes a new robot. The robot are in the point with coordinates (x_1, y_1) and should go to the point (x_2, y_2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.\n\n\n-----Input-----\n\nThe first line contains two integers x_1, y_1 ( - 10^9 \u2264 x_1, y_1 \u2264 10^9) \u2014 the start position of the robot.\n\nThe second line contains two integers x_2, y_2 ( - 10^9 \u2264 x_2, y_2 \u2264 10^9) \u2014 the finish position of the robot.\n\n\n-----Output-----\n\nPrint the only integer d \u2014 the minimal number of steps to get the finish position.\n\n\n-----Examples-----\nInput\n0 0\n4 5\n\nOutput\n5\n\nInput\n3 4\n6 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.\n\nIn the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times."} +{"id": "00203", "text": "There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.\n\nEach of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with n who are still eligible to vote make their statements. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction. \n\nYou know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the number of employees. \n\nThe next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.\n\n\n-----Output-----\n\nPrint 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.\n\n\n-----Examples-----\nInput\n5\nDDRRR\n\nOutput\nD\n\nInput\n6\nDDRRRR\n\nOutput\nR\n\n\n\n-----Note-----\n\nConsider one of the voting scenarios for the first sample: Employee 1 denies employee 5 to vote. Employee 2 denies employee 3 to vote. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). Employee 4 denies employee 2 to vote. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). Employee 1 denies employee 4. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans."} +{"id": "00204", "text": "Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than $a$ and screen height not greater than $b$. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is $w$, and the height of the screen is $h$, then the following condition should be met: $\\frac{w}{h} = \\frac{x}{y}$.\n\nThere are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers $w$ and $h$ there is a TV set with screen width $w$ and height $h$ in the shop.\n\nMonocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers $w$ and $h$, beforehand, such that $(w \\le a)$, $(h \\le b)$ and $(\\frac{w}{h} = \\frac{x}{y})$.\n\nIn other words, Monocarp wants to determine the number of TV sets having aspect ratio $\\frac{x}{y}$, screen width not exceeding $a$, and screen height not exceeding $b$. Two TV sets are considered different if they have different screen width or different screen height.\n\n\n-----Input-----\n\nThe first line contains four integers $a$, $b$, $x$, $y$ ($1 \\le a, b, x, y \\le 10^{18}$)\u00a0\u2014 the constraints on the screen width and height, and on the aspect ratio.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints.\n\n\n-----Examples-----\nInput\n17 15 5 3\n\nOutput\n3\n\nInput\n14 16 7 22\n\nOutput\n0\n\nInput\n4 2 6 4\n\nOutput\n1\n\nInput\n1000000000000000000 1000000000000000000 999999866000004473 999999822000007597\n\nOutput\n1000000063\n\n\n\n-----Note-----\n\nIn the first example, there are $3$ possible variants: $(5, 3)$, $(10, 6)$, $(15, 9)$.\n\nIn the second example, there is no TV set meeting the constraints.\n\nIn the third example, there is only one variant: $(3, 2)$."} +{"id": "00205", "text": "The number \"zero\" is called \"love\" (or \"l'oeuf\" to be precise, literally means \"egg\" in French), for example when denoting the zero score in a game of tennis. \n\nAki is fond of numbers, especially those with trailing zeros. For example, the number $9200$ has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is.\n\nHowever, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers.\n\nGiven two integers $n$ and $b$ (in decimal notation), your task is to calculate the number of trailing zero digits in the $b$-ary (in the base/radix of $b$) representation of $n\\,!$ (factorial of $n$). \n\n\n-----Input-----\n\nThe only line of the input contains two integers $n$ and $b$ ($1 \\le n \\le 10^{18}$, $2 \\le b \\le 10^{12}$).\n\n\n-----Output-----\n\nPrint an only integer\u00a0\u2014 the number of trailing zero digits in the $b$-ary representation of $n!$\n\n\n-----Examples-----\nInput\n6 9\n\nOutput\n1\n\nInput\n38 11\n\nOutput\n3\n\nInput\n5 2\n\nOutput\n3\n\nInput\n5 10\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, $6!_{(10)} = 720_{(10)} = 880_{(9)}$.\n\nIn the third and fourth example, $5!_{(10)} = 120_{(10)} = 1111000_{(2)}$.\n\nThe representation of the number $x$ in the $b$-ary base is $d_1, d_2, \\ldots, d_k$ if $x = d_1 b^{k - 1} + d_2 b^{k - 2} + \\ldots + d_k b^0$, where $d_i$ are integers and $0 \\le d_i \\le b - 1$. For example, the number $720$ from the first example is represented as $880_{(9)}$ since $720 = 8 \\cdot 9^2 + 8 \\cdot 9 + 0 \\cdot 1$.\n\nYou can read more about bases here."} +{"id": "00206", "text": "A frog is initially at position $0$ on the number line. The frog has two positive integers $a$ and $b$. From a position $k$, it can either jump to position $k+a$ or $k-b$.\n\nLet $f(x)$ be the number of distinct integers the frog can reach if it never jumps on an integer outside the interval $[0, x]$. The frog doesn't need to visit all these integers in one trip, that is, an integer is counted if the frog can somehow reach it if it starts from $0$.\n\nGiven an integer $m$, find $\\sum_{i=0}^{m} f(i)$. That is, find the sum of all $f(i)$ for $i$ from $0$ to $m$.\n\n\n-----Input-----\n\nThe first line contains three integers $m, a, b$ ($1 \\leq m \\leq 10^9, 1 \\leq a,b \\leq 10^5$).\n\n\n-----Output-----\n\nPrint a single integer, the desired sum.\n\n\n-----Examples-----\nInput\n7 5 3\n\nOutput\n19\n\nInput\n1000000000 1 2019\n\nOutput\n500000001500000001\n\nInput\n100 100000 1\n\nOutput\n101\n\nInput\n6 4 5\n\nOutput\n10\n\n\n\n-----Note-----\n\nIn the first example, we must find $f(0)+f(1)+\\ldots+f(7)$. We have $f(0) = 1, f(1) = 1, f(2) = 1, f(3) = 1, f(4) = 1, f(5) = 3, f(6) = 3, f(7) = 8$. The sum of these values is $19$.\n\nIn the second example, we have $f(i) = i+1$, so we want to find $\\sum_{i=0}^{10^9} i+1$.\n\nIn the third example, the frog can't make any jumps in any case."} +{"id": "00207", "text": "Where do odds begin, and where do they end? Where does hope emerge, and will they ever break?\n\nGiven an integer sequence a_1, a_2, ..., a_{n} of length n. Decide whether it is possible to divide it into an odd number of non-empty subsegments, the each of which has an odd length and begins and ends with odd numbers.\n\nA subsegment is a contiguous slice of the whole sequence. For example, {3, 4, 5} and {1} are subsegments of sequence {1, 2, 3, 4, 5, 6}, while {1, 2, 4} and {7} are not.\n\n\n-----Input-----\n\nThe first line of input contains a non-negative integer n (1 \u2264 n \u2264 100) \u2014 the length of the sequence.\n\nThe second line contains n space-separated non-negative integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 100) \u2014 the elements of the sequence.\n\n\n-----Output-----\n\nOutput \"Yes\" if it's possible to fulfill the requirements, and \"No\" otherwise.\n\nYou can output each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n3\n1 3 5\n\nOutput\nYes\n\nInput\n5\n1 0 1 5 1\n\nOutput\nYes\n\nInput\n3\n4 3 1\n\nOutput\nNo\n\nInput\n4\n3 9 9 3\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first example, divide the sequence into 1 subsegment: {1, 3, 5} and the requirements will be met.\n\nIn the second example, divide the sequence into 3 subsegments: {1, 0, 1}, {5}, {1}.\n\nIn the third example, one of the subsegments must start with 4 which is an even number, thus the requirements cannot be met.\n\nIn the fourth example, the sequence can be divided into 2 subsegments: {3, 9, 9}, {3}, but this is not a valid solution because 2 is an even number."} +{"id": "00208", "text": "Pashmak has fallen in love with an attractive girl called Parmida since one year ago...\n\nToday, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.\n\n\n-----Input-----\n\nThe first line contains four space-separated x_1, y_1, x_2, y_2 ( - 100 \u2264 x_1, y_1, x_2, y_2 \u2264 100) integers, where x_1 and y_1 are coordinates of the first tree and x_2 and y_2 are coordinates of the second tree. It's guaranteed that the given points are distinct.\n\n\n-----Output-----\n\nIf there is no solution to the problem, print -1. Otherwise print four space-separated integers x_3, y_3, x_4, y_4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them. \n\nNote that x_3, y_3, x_4, y_4 must be in the range ( - 1000 \u2264 x_3, y_3, x_4, y_4 \u2264 1000).\n\n\n-----Examples-----\nInput\n0 0 0 1\n\nOutput\n1 0 1 1\n\nInput\n0 0 1 1\n\nOutput\n0 1 1 0\n\nInput\n0 0 1 2\n\nOutput\n-1"} +{"id": "00209", "text": "Jzzhu has invented a kind of sequences, they meet the following property:$f_{1} = x ; f_{2} = y ; \\forall i(i \\geq 2), f_{i} = f_{i - 1} + f_{i + 1}$\n\nYou are given x and y, please calculate f_{n} modulo 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe first line contains two integers x and y (|x|, |y| \u2264 10^9). The second line contains a single integer n (1 \u2264 n \u2264 2\u00b710^9).\n\n\n-----Output-----\n\nOutput a single integer representing f_{n} modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n2 3\n3\n\nOutput\n1\n\nInput\n0 -1\n2\n\nOutput\n1000000006\n\n\n\n-----Note-----\n\nIn the first sample, f_2 = f_1 + f_3, 3 = 2 + f_3, f_3 = 1.\n\nIn the second sample, f_2 = - 1; - 1 modulo (10^9 + 7) equals (10^9 + 6)."} +{"id": "00210", "text": "One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into several parts. This time Lesha decided to split the array A into several, possibly one, new arrays so that the sum of elements in each of the new arrays is not zero. One more condition is that if we place the new arrays one after another they will form the old array A.\n\nLesha is tired now so he asked you to split the array. Help Lesha!\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of elements in the array A.\n\nThe next line contains n integers a_1, a_2, ..., a_{n} ( - 10^3 \u2264 a_{i} \u2264 10^3)\u00a0\u2014 the elements of the array A.\n\n\n-----Output-----\n\nIf it is not possible to split the array A and satisfy all the constraints, print single line containing \"NO\" (without quotes).\n\nOtherwise in the first line print \"YES\" (without quotes). In the next line print single integer k\u00a0\u2014 the number of new arrays. In each of the next k lines print two integers l_{i} and r_{i} which denote the subarray A[l_{i}... r_{i}] of the initial array A being the i-th new array. Integers l_{i}, r_{i} should satisfy the following conditions: l_1 = 1 r_{k} = n r_{i} + 1 = l_{i} + 1 for each 1 \u2264 i < k. \n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n3\n1 2 -3\n\nOutput\nYES\n2\n1 2\n3 3\n\nInput\n8\n9 -12 3 4 -4 -10 7 3\n\nOutput\nYES\n2\n1 2\n3 8\n\nInput\n1\n0\n\nOutput\nNO\n\nInput\n4\n1 2 3 -5\n\nOutput\nYES\n4\n1 1\n2 2\n3 3\n4 4"} +{"id": "00211", "text": "Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.\n\nManao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (10^9 + 9).\n\n\n-----Input-----\n\nThe single line contains three space-separated integers n, m and k (2 \u2264 k \u2264 n \u2264 10^9;\u00a00 \u2264 m \u2264 n).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (10^9 + 9).\n\n\n-----Examples-----\nInput\n5 3 2\n\nOutput\n3\n\nInput\n5 4 2\n\nOutput\n6\n\n\n\n-----Note-----\n\nSample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.\n\nSample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.\n\nAlso note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000\u00a0mod\u00a01000000009, even though 2000000020\u00a0mod\u00a01000000009 is a smaller number."} +{"id": "00212", "text": "You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.\n\nYour task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.\n\nIf a solution exists, you should print it.\n\n\n-----Input-----\n\nThe single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits. \n\n\n-----Output-----\n\nPrint \"NO\" (without quotes), if there is no such way to remove some digits from number n. \n\nOtherwise, print \"YES\" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.\n\nIf there are multiple possible answers, you may print any of them.\n\n\n-----Examples-----\nInput\n3454\n\nOutput\nYES\n344\n\nInput\n10\n\nOutput\nYES\n0\n\nInput\n111111\n\nOutput\nNO"} +{"id": "00213", "text": "In a building where Polycarp lives there are equal number of flats on each floor. Unfortunately, Polycarp don't remember how many flats are on each floor, but he remembers that the flats are numbered from 1 from lower to upper floors. That is, the first several flats are on the first floor, the next several flats are on the second and so on. Polycarp don't remember the total number of flats in the building, so you can consider the building to be infinitely high (i.e. there are infinitely many floors). Note that the floors are numbered from 1.\n\nPolycarp remembers on which floors several flats are located. It is guaranteed that this information is not self-contradictory. It means that there exists a building with equal number of flats on each floor so that the flats from Polycarp's memory have the floors Polycarp remembers.\n\nGiven this information, is it possible to restore the exact floor for flat n? \n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 100, 0 \u2264 m \u2264 100), where n is the number of the flat you need to restore floor for, and m is the number of flats in Polycarp's memory.\n\nm lines follow, describing the Polycarp's memory: each of these lines contains a pair of integers k_{i}, f_{i} (1 \u2264 k_{i} \u2264 100, 1 \u2264 f_{i} \u2264 100), which means that the flat k_{i} is on the f_{i}-th floor. All values k_{i} are distinct.\n\nIt is guaranteed that the given information is not self-contradictory.\n\n\n-----Output-----\n\nPrint the number of the floor in which the n-th flat is located, if it is possible to determine it in a unique way. Print -1 if it is not possible to uniquely restore this floor.\n\n\n-----Examples-----\nInput\n10 3\n6 2\n2 1\n7 3\n\nOutput\n4\n\nInput\n8 4\n3 1\n6 2\n5 2\n2 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example the 6-th flat is on the 2-nd floor, while the 7-th flat is on the 3-rd, so, the 6-th flat is the last on its floor and there are 3 flats on each floor. Thus, the 10-th flat is on the 4-th floor.\n\nIn the second example there can be 3 or 4 flats on each floor, so we can't restore the floor for the 8-th flat."} +{"id": "00214", "text": "Bishwock is a chess figure that consists of three squares resembling an \"L-bar\". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:\n\n \n\nXX XX .X X.\n\nX. .X XX XX\n\n \n\nBishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square. \n\nVasya has a board with $2\\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.\n\nKnowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.\n\n\n-----Input-----\n\nThe input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols \"0\" (zero) that denote the empty squares and symbols \"X\" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the maximum amount of bishwocks that can be placed onto the given board.\n\n\n-----Examples-----\nInput\n00\n00\n\nOutput\n1\nInput\n00X00X0XXX0\n0XXX0X00X00\n\nOutput\n4\nInput\n0X0X0\n0X0X0\n\nOutput\n0\nInput\n0XXX0\n00000\n\nOutput\n2"} +{"id": "00215", "text": "Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters.\n\nLet A be a set of positions in the string. Let's call it pretty if following conditions are met: letters on positions from A in the string are all distinct and lowercase; there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a_1 < j < a_2 for some a_1 and a_2 from A). \n\nWrite a program that will determine the maximum number of elements in a pretty set of positions.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 200) \u2014 length of string s.\n\nThe second line contains a string s consisting of lowercase and uppercase Latin letters.\n\n\n-----Output-----\n\nPrint maximum number of elements in pretty set of positions for string s.\n\n\n-----Examples-----\nInput\n11\naaaaBaabAbA\n\nOutput\n2\n\nInput\n12\nzACaAbbaazzC\n\nOutput\n3\n\nInput\n3\nABC\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position.\n\nIn the second example desired positions can be 7, 8 and 11. There are other ways to choose pretty set consisting of three elements.\n\nIn the third example the given string s does not contain any lowercase letters, so the answer is 0."} +{"id": "00216", "text": "You are given a sequence a consisting of n integers. You may partition this sequence into two sequences b and c in such a way that every element belongs exactly to one of these sequences. \n\nLet B be the sum of elements belonging to b, and C be the sum of elements belonging to c (if some of these sequences is empty, then its sum is 0). What is the maximum possible value of B - C?\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 100) \u2014 the number of elements in a.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} ( - 100 \u2264 a_{i} \u2264 100) \u2014 the elements of sequence a.\n\n\n-----Output-----\n\nPrint the maximum possible value of B - C, where B is the sum of elements of sequence b, and C is the sum of elements of sequence c.\n\n\n-----Examples-----\nInput\n3\n1 -2 0\n\nOutput\n3\n\nInput\n6\n16 23 16 15 42 8\n\nOutput\n120\n\n\n\n-----Note-----\n\nIn the first example we may choose b = {1, 0}, c = { - 2}. Then B = 1, C = - 2, B - C = 3.\n\nIn the second example we choose b = {16, 23, 16, 15, 42, 8}, c = {} (an empty sequence). Then B = 120, C = 0, B - C = 120."} +{"id": "00217", "text": "A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the point x = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a to x = 0 is called a bus journey. In total, the bus must make k journeys.\n\nThe petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.\n\nThere is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.\n\nWhat is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the point x = 0.\n\n\n-----Input-----\n\nThe first line contains four integers a, b, f, k (0 < f < a \u2264 10^6, 1 \u2264 b \u2264 10^9, 1 \u2264 k \u2264 10^4) \u2014 the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.\n\n\n-----Output-----\n\nPrint the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print -1.\n\n\n-----Examples-----\nInput\n6 9 2 4\n\nOutput\n4\n\nInput\n6 10 2 4\n\nOutput\n2\n\nInput\n6 5 4 3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example the bus needs to refuel during each journey.\n\nIn the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty. \n\nIn the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling."} +{"id": "00218", "text": "You are given the string s of length n and the numbers p, q. Split the string s to pieces of length p and q.\n\nFor example, the string \"Hello\" for p = 2, q = 3 can be split to the two strings \"Hel\" and \"lo\" or to the two strings \"He\" and \"llo\".\n\nNote it is allowed to split the string s to the strings only of length p or to the strings only of length q (see the second sample test).\n\n\n-----Input-----\n\nThe first line contains three positive integers n, p, q (1 \u2264 p, q \u2264 n \u2264 100).\n\nThe second line contains the string s consists of lowercase and uppercase latin letters and digits.\n\n\n-----Output-----\n\nIf it's impossible to split the string s to the strings of length p and q print the only number \"-1\".\n\nOtherwise in the first line print integer k \u2014 the number of strings in partition of s.\n\nEach of the next k lines should contain the strings in partition. Each string should be of the length p or q. The string should be in order of their appearing in string s \u2014 from left to right.\n\nIf there are several solutions print any of them.\n\n\n-----Examples-----\nInput\n5 2 3\nHello\n\nOutput\n2\nHe\nllo\n\nInput\n10 9 5\nCodeforces\n\nOutput\n2\nCodef\norces\n\nInput\n6 4 5\nPrivet\n\nOutput\n-1\n\nInput\n8 1 1\nabacabac\n\nOutput\n8\na\nb\na\nc\na\nb\na\nc"} +{"id": "00219", "text": "A sportsman starts from point x_{start} = 0 and runs to point with coordinate x_{finish} = m (on a straight line). Also, the sportsman can jump \u2014 to jump, he should first take a run of length of not less than s meters (in this case for these s meters his path should have no obstacles), and after that he can jump over a length of not more than d meters. Running and jumping is permitted only in the direction from left to right. He can start andfinish a jump only at the points with integer coordinates in which there are no obstacles. To overcome some obstacle, it is necessary to land at a point which is strictly to the right of this obstacle.\n\nOn the way of an athlete are n obstacles at coordinates x_1, x_2, ..., x_{n}. He cannot go over the obstacles, he can only jump over them. Your task is to determine whether the athlete will be able to get to the finish point.\n\n\n-----Input-----\n\nThe first line of the input containsd four integers n, m, s and d (1 \u2264 n \u2264 200 000, 2 \u2264 m \u2264 10^9, 1 \u2264 s, d \u2264 10^9)\u00a0\u2014 the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly.\n\nThe second line contains a sequence of n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 m - 1)\u00a0\u2014 the coordinates of the obstacles. It is guaranteed that the starting and finishing point have no obstacles, also no point can have more than one obstacle, The coordinates of the obstacles are given in an arbitrary order.\n\n\n-----Output-----\n\nIf the runner cannot reach the finishing point, print in the first line of the output \"IMPOSSIBLE\" (without the quotes).\n\nIf the athlete can get from start to finish, print any way to do this in the following format: print a line of form \"RUN X>\" (where \"X\" should be a positive integer), if the athlete should run for \"X\" more meters; print a line of form \"JUMP Y\" (where \"Y\" should be a positive integer), if the sportsman starts a jump and should remain in air for \"Y\" more meters. \n\nAll commands \"RUN\" and \"JUMP\" should strictly alternate, starting with \"RUN\", besides, they should be printed chronologically. It is not allowed to jump over the finishing point but it is allowed to land there after a jump. The athlete should stop as soon as he reaches finish.\n\n\n-----Examples-----\nInput\n3 10 1 3\n3 4 7\n\nOutput\nRUN 2\nJUMP 3\nRUN 1\nJUMP 2\nRUN 2\n\nInput\n2 9 2 3\n6 4\n\nOutput\nIMPOSSIBLE"} +{"id": "00220", "text": "Two positive integers a and b have a sum of s and a bitwise XOR of x. How many possible values are there for the ordered pair (a, b)?\n\n\n-----Input-----\n\nThe first line of the input contains two integers s and x (2 \u2264 s \u2264 10^12, 0 \u2264 x \u2264 10^12), the sum and bitwise xor of the pair of positive integers, respectively.\n\n\n-----Output-----\n\nPrint a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.\n\n\n-----Examples-----\nInput\n9 5\n\nOutput\n4\n\nInput\n3 3\n\nOutput\n2\n\nInput\n5 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, we have the following solutions: (2, 7), (3, 6), (6, 3), (7, 2).\n\nIn the second sample, the only solutions are (1, 2) and (2, 1)."} +{"id": "00221", "text": "Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.\n\nThis time Miroslav laid out $n$ skewers parallel to each other, and enumerated them with consecutive integers from $1$ to $n$ in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number $i$, it leads to turning $k$ closest skewers from each side of the skewer $i$, that is, skewers number $i - k$, $i - k + 1$, ..., $i - 1$, $i + 1$, ..., $i + k - 1$, $i + k$ (if they exist). \n\nFor example, let $n = 6$ and $k = 1$. When Miroslav turns skewer number $3$, then skewers with numbers $2$, $3$, and $4$ will come up turned over. If after that he turns skewer number $1$, then skewers number $1$, $3$, and $4$ will be turned over, while skewer number $2$ will be in the initial position (because it is turned again).\n\nAs we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all $n$ skewers with the minimal possible number of actions. For example, for the above example $n = 6$ and $k = 1$, two turnings are sufficient: he can turn over skewers number $2$ and $5$.\n\nHelp Miroslav turn over all $n$ skewers.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\leq n \\leq 1000$, $0 \\leq k \\leq 1000$)\u00a0\u2014 the number of skewers and the number of skewers from each side that are turned in one step.\n\n\n-----Output-----\n\nThe first line should contain integer $l$\u00a0\u2014 the minimum number of actions needed by Miroslav to turn over all $n$ skewers. After than print $l$ integers from $1$ to $n$ denoting the number of the skewer that is to be turned over at the corresponding step.\n\n\n-----Examples-----\nInput\n7 2\n\nOutput\n2\n1 6 \n\nInput\n5 1\n\nOutput\n2\n1 4 \n\n\n\n-----Note-----\n\nIn the first example the first operation turns over skewers $1$, $2$ and $3$, the second operation turns over skewers $4$, $5$, $6$ and $7$.\n\nIn the second example it is also correct to turn over skewers $2$ and $5$, but turning skewers $2$ and $4$, or $1$ and $5$ are incorrect solutions because the skewer $3$ is in the initial state after these operations."} +{"id": "00222", "text": "You are given a positive integer $n$, written without leading zeroes (for example, the number 04 is incorrect). \n\nIn one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros.\n\nDetermine the minimum number of operations that you need to consistently apply to the given integer $n$ to make from it the square of some positive integer or report that it is impossible.\n\nAn integer $x$ is the square of some positive integer if and only if $x=y^2$ for some positive integer $y$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^{9}$). The number is given without leading zeroes.\n\n\n-----Output-----\n\nIf it is impossible to make the square of some positive integer from $n$, print -1. In the other case, print the minimal number of operations required to do it.\n\n\n-----Examples-----\nInput\n8314\n\nOutput\n2\n\nInput\n625\n\nOutput\n0\n\nInput\n333\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example we should delete from $8314$ the digits $3$ and $4$. After that $8314$ become equals to $81$, which is the square of the integer $9$.\n\nIn the second example the given $625$ is the square of the integer $25$, so you should not delete anything. \n\nIn the third example it is impossible to make the square from $333$, so the answer is -1."} +{"id": "00223", "text": "Let's define a function $f(p)$ on a permutation $p$ as follows. Let $g_i$ be the greatest common divisor (GCD) of elements $p_1$, $p_2$, ..., $p_i$ (in other words, it is the GCD of the prefix of length $i$). Then $f(p)$ is the number of distinct elements among $g_1$, $g_2$, ..., $g_n$.\n\nLet $f_{max}(n)$ be the maximum value of $f(p)$ among all permutations $p$ of integers $1$, $2$, ..., $n$.\n\nGiven an integers $n$, count the number of permutations $p$ of integers $1$, $2$, ..., $n$, such that $f(p)$ is equal to $f_{max}(n)$. Since the answer may be large, print the remainder of its division by $1000\\,000\\,007 = 10^9 + 7$.\n\n\n-----Input-----\n\nThe only line contains the integer $n$ ($2 \\le n \\le 10^6$)\u00a0\u2014 the length of the permutations.\n\n\n-----Output-----\n\nThe only line should contain your answer modulo $10^9+7$.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1\nInput\n3\n\nOutput\n4\nInput\n6\n\nOutput\n120\n\n\n-----Note-----\n\nConsider the second example: these are the permutations of length $3$: $[1,2,3]$, $f(p)=1$. $[1,3,2]$, $f(p)=1$. $[2,1,3]$, $f(p)=2$. $[2,3,1]$, $f(p)=2$. $[3,1,2]$, $f(p)=2$. $[3,2,1]$, $f(p)=2$. \n\nThe maximum value $f_{max}(3) = 2$, and there are $4$ permutations $p$ such that $f(p)=2$."} +{"id": "00224", "text": "One day, the Grasshopper was jumping on the lawn and found a piece of paper with a string. Grasshopper became interested what is the minimum jump ability he should have in order to be able to reach the far end of the string, jumping only on vowels of the English alphabet. Jump ability is the maximum possible length of his jump. \n\nFormally, consider that at the begginning the Grasshopper is located directly in front of the leftmost character of the string. His goal is to reach the position right after the rightmost character of the string. In one jump the Grasshopper could jump to the right any distance from 1 to the value of his jump ability. [Image] The picture corresponds to the first example. \n\nThe following letters are vowels: 'A', 'E', 'I', 'O', 'U' and 'Y'.\n\n\n-----Input-----\n\nThe first line contains non-empty string consisting of capital English letters. It is guaranteed that the length of the string does not exceed 100. \n\n\n-----Output-----\n\nPrint single integer a\u00a0\u2014 the minimum jump ability of the Grasshopper (in the number of symbols) that is needed to overcome the given string, jumping only on vowels.\n\n\n-----Examples-----\nInput\nABABBBACFEYUKOTT\n\nOutput\n4\nInput\nAAA\n\nOutput\n1"} +{"id": "00225", "text": "Dawid has four bags of candies. The $i$-th of them contains $a_i$ candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total?\n\nNote, that you can't keep bags for yourself or throw them away, each bag should be given to one of the friends.\n\n\n-----Input-----\n\nThe only line contains four integers $a_1$, $a_2$, $a_3$ and $a_4$ ($1 \\leq a_i \\leq 100$) \u2014 the numbers of candies in each bag.\n\n\n-----Output-----\n\nOutput YES if it's possible to give the bags to Dawid's friends so that both friends receive the same amount of candies, or NO otherwise. Each character can be printed in any case (either uppercase or lowercase).\n\n\n-----Examples-----\nInput\n1 7 11 5\n\nOutput\nYES\n\nInput\n7 3 2 5\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample test, Dawid can give the first and the third bag to the first friend, and the second and the fourth bag to the second friend. This way, each friend will receive $12$ candies.\n\nIn the second sample test, it's impossible to distribute the bags."} +{"id": "00226", "text": "You may have heard of the pie rule before. It states that if two people wish to fairly share a slice of pie, one person should cut the slice in half, and the other person should choose who gets which slice. Alice and Bob have many slices of pie, and rather than cutting the slices in half, each individual slice will be eaten by just one person.\n\nThe way Alice and Bob decide who eats each slice is as follows. First, the order in which the pies are to be handed out is decided. There is a special token called the \"decider\" token, initially held by Bob. Until all the pie is handed out, whoever has the decider token will give the next slice of pie to one of the participants, and the decider token to the other participant. They continue until no slices of pie are left.\n\nAll of the slices are of excellent quality, so each participant obviously wants to maximize the total amount of pie they get to eat. Assuming both players make their decisions optimally, how much pie will each participant receive?\n\n\n-----Input-----\n\nInput will begin with an integer N (1 \u2264 N \u2264 50), the number of slices of pie. \n\nFollowing this is a line with N integers indicating the sizes of the slices (each between 1 and 100000, inclusive), in the order in which they must be handed out.\n\n\n-----Output-----\n\nPrint two integers. First, the sum of the sizes of slices eaten by Alice, then the sum of the sizes of the slices eaten by Bob, assuming both players make their decisions optimally.\n\n\n-----Examples-----\nInput\n3\n141 592 653\n\nOutput\n653 733\n\nInput\n5\n10 21 10 21 10\n\nOutput\n31 41\n\n\n\n-----Note-----\n\nIn the first example, Bob takes the size 141 slice for himself and gives the decider token to Alice. Then Alice gives the size 592 slice to Bob and keeps the decider token for herself, so that she can then give the size 653 slice to herself."} +{"id": "00227", "text": "You've got a positive integer sequence a_1, a_2, ..., a_{n}. All numbers in the sequence are distinct. Let's fix the set of variables b_1, b_2, ..., b_{m}. Initially each variable b_{i} (1 \u2264 i \u2264 m) contains the value of zero. Consider the following sequence, consisting of n operations.\n\nThe first operation is assigning the value of a_1 to some variable b_{x} (1 \u2264 x \u2264 m). Each of the following n - 1 operations is assigning to some variable b_{y} the value that is equal to the sum of values that are stored in the variables b_{i} and b_{j} (1 \u2264 i, j, y \u2264 m). At that, the value that is assigned on the t-th operation, must equal a_{t}. For each operation numbers y, i, j are chosen anew.\n\nYour task is to find the minimum number of variables m, such that those variables can help you perform the described sequence of operations.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 23). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{k} \u2264 10^9).\n\nIt is guaranteed that all numbers in the sequence are distinct.\n\n\n-----Output-----\n\nIn a single line print a single number \u2014 the minimum number of variables m, such that those variables can help you perform the described sequence of operations.\n\nIf you cannot perform the sequence of operations at any m, print -1.\n\n\n-----Examples-----\nInput\n5\n1 2 3 6 8\n\nOutput\n2\n\nInput\n3\n3 6 5\n\nOutput\n-1\n\nInput\n6\n2 4 8 6 10 18\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample, you can use two variables b_1 and b_2 to perform the following sequence of operations. b_1 := 1; b_2 := b_1 + b_1; b_1 := b_1 + b_2; b_1 := b_1 + b_1; b_1 := b_1 + b_2."} +{"id": "00228", "text": "Alice and Bob are playing a game with $n$ piles of stones. It is guaranteed that $n$ is an even number. The $i$-th pile has $a_i$ stones.\n\nAlice and Bob will play a game alternating turns with Alice going first.\n\nOn a player's turn, they must choose exactly $\\frac{n}{2}$ nonempty piles and independently remove a positive number of stones from each of the chosen piles. They can remove a different number of stones from the piles in a single turn. The first player unable to make a move loses (when there are less than $\\frac{n}{2}$ nonempty piles).\n\nGiven the starting configuration, determine who will win the game.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\leq n \\leq 50$)\u00a0\u2014 the number of piles. It is guaranteed that $n$ is an even number.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 50$)\u00a0\u2014 the number of stones in the piles.\n\n\n-----Output-----\n\nPrint a single string \"Alice\" if Alice wins; otherwise, print \"Bob\" (without double quotes).\n\n\n-----Examples-----\nInput\n2\n8 8\n\nOutput\nBob\n\nInput\n4\n3 1 4 1\n\nOutput\nAlice\n\n\n\n-----Note-----\n\nIn the first example, each player can only remove stones from one pile ($\\frac{2}{2}=1$). Alice loses, since Bob can copy whatever Alice does on the other pile, so Alice will run out of moves first.\n\nIn the second example, Alice can remove $2$ stones from the first pile and $3$ stones from the third pile on her first move to guarantee a win."} +{"id": "00229", "text": "Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.\n\nFilya is given an array of non-negative integers a_1, a_2, ..., a_{n}. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.\n\nNow he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of integers in the Filya's array. The second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 elements of the array.\n\n\n-----Output-----\n\nIf it's impossible to make all elements of the array equal using the process given in the problem statement, then print \"NO\" (without quotes) in the only line of the output. Otherwise print \"YES\" (without quotes).\n\n\n-----Examples-----\nInput\n5\n1 3 3 2 1\n\nOutput\nYES\n\nInput\n5\n1 2 3 4 5\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements."} +{"id": "00230", "text": "Given is a string S of length N.\nFind the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.\nMore formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \\leq l_1, l_2 \\leq N - len + 1 ) that satisfy the following:\n - l_1 + len \\leq l_2\n - S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)\nIf there is no such integer len, print 0.\n\n-----Constraints-----\n - 2 \\leq N \\leq 5 \\times 10^3\n - |S| = N\n - S consists of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS\n\n-----Output-----\nPrint the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.\n\n-----Sample Input-----\n5\nababa\n\n-----Sample Output-----\n2\n\nThe strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.\nNote that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \\leq l_2."} +{"id": "00231", "text": "The main street of Berland is a straight line with n houses built along it (n is an even number). The houses are located at both sides of the street. The houses with odd numbers are at one side of the street and are numbered from 1 to n - 1 in the order from the beginning of the street to the end (in the picture: from left to right). The houses with even numbers are at the other side of the street and are numbered from 2 to n in the order from the end of the street to its beginning (in the picture: from right to left). The corresponding houses with even and odd numbers are strictly opposite each other, that is, house 1 is opposite house n, house 3 is opposite house n - 2, house 5 is opposite house n - 4 and so on. [Image] \n\nVasya needs to get to house number a as quickly as possible. He starts driving from the beginning of the street and drives his car to house a. To get from the beginning of the street to houses number 1 and n, he spends exactly 1 second. He also spends exactly one second to drive the distance between two neighbouring houses. Vasya can park at any side of the road, so the distance between the beginning of the street at the houses that stand opposite one another should be considered the same.\n\nYour task is: find the minimum time Vasya needs to reach house a.\n\n\n-----Input-----\n\nThe first line of the input contains two integers, n and a (1 \u2264 a \u2264 n \u2264 100 000)\u00a0\u2014 the number of houses on the street and the number of the house that Vasya needs to reach, correspondingly. It is guaranteed that number n is even.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum time Vasya needs to get from the beginning of the street to house a.\n\n\n-----Examples-----\nInput\n4 2\n\nOutput\n2\n\nInput\n8 5\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample there are only four houses on the street, two houses at each side. House 2 will be the last at Vasya's right.\n\nThe second sample corresponds to picture with n = 8. House 5 is the one before last at Vasya's left."} +{"id": "00232", "text": "There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. \n\nHeidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k_1 knights with lightsabers of the first color, k_2 knights with lightsabers of the second color, ..., k_{m} knights with lightsabers of the m-th color. Help her find out if this is possible.\n\n\n-----Input-----\n\nThe first line of the input contains n (1 \u2264 n \u2264 100) and m (1 \u2264 m \u2264 n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k_1, k_2, ..., k_{m} (with $1 \\leq \\sum_{i = 1}^{m} k_{i} \\leq n$) \u2013 the desired counts of lightsabers of each color from 1 to m.\n\n\n-----Output-----\n\nOutput YES if an interval with prescribed color counts exists, or output NO if there is none.\n\n\n-----Example-----\nInput\n5 2\n1 1 2 2 1\n1 2\n\nOutput\nYES"} +{"id": "00233", "text": "Mishka is a little polar bear. As known, little bears loves spending their free time playing dice for chocolates. Once in a wonderful sunny morning, walking around blocks of ice, Mishka met her friend Chris, and they started playing the game.\n\nRules of the game are very simple: at first number of rounds n is defined. In every round each of the players throws a cubical dice with distinct numbers from 1 to 6 written on its faces. Player, whose value after throwing the dice is greater, wins the round. In case if player dice values are equal, no one of them is a winner.\n\nIn average, player, who won most of the rounds, is the winner of the game. In case if two players won the same number of rounds, the result of the game is draw.\n\nMishka is still very little and can't count wins and losses, so she asked you to watch their game and determine its result. Please help her!\n\n\n-----Input-----\n\nThe first line of the input contains single integer n n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of game rounds.\n\nThe next n lines contains rounds description. i-th of them contains pair of integers m_{i} and c_{i} (1 \u2264 m_{i}, c_{i} \u2264 6)\u00a0\u2014 values on dice upper face after Mishka's and Chris' throws in i-th round respectively.\n\n\n-----Output-----\n\nIf Mishka is the winner of the game, print \"Mishka\" (without quotes) in the only line.\n\nIf Chris is the winner of the game, print \"Chris\" (without quotes) in the only line.\n\nIf the result of the game is draw, print \"Friendship is magic!^^\" (without quotes) in the only line.\n\n\n-----Examples-----\nInput\n3\n3 5\n2 1\n4 2\n\nOutput\nMishka\nInput\n2\n6 1\n1 6\n\nOutput\nFriendship is magic!^^\nInput\n3\n1 5\n3 3\n2 2\n\nOutput\nChris\n\n\n-----Note-----\n\nIn the first sample case Mishka loses the first round, but wins second and third rounds and thus she is the winner of the game.\n\nIn the second sample case Mishka wins the first round, Chris wins the second round, and the game ends with draw with score 1:1.\n\nIn the third sample case Chris wins the first round, but there is no winner of the next two rounds. The winner of the game is Chris."} +{"id": "00234", "text": "One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won.\n\nAlex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it?\n\nHe needs your help to check it.\n\nA Minesweeper field is a rectangle $n \\times m$, where each cell is either empty, or contains a digit from $1$ to $8$, or a bomb. The field is valid if for each cell: if there is a digit $k$ in the cell, then exactly $k$ neighboring cells have bombs. if the cell is empty, then all neighboring cells have no bombs. \n\nTwo cells are neighbors if they have a common side or a corner (i.\u00a0e. a cell has at most $8$ neighboring cells).\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 100$) \u2014 the sizes of the field.\n\nThe next $n$ lines contain the description of the field. Each line contains $m$ characters, each of them is \".\" (if this cell is empty), \"*\" (if there is bomb in this cell), or a digit from $1$ to $8$, inclusive.\n\n\n-----Output-----\n\nPrint \"YES\", if the field is valid and \"NO\" otherwise.\n\nYou can choose the case (lower or upper) for each letter arbitrarily.\n\n\n-----Examples-----\nInput\n3 3\n111\n1*1\n111\n\nOutput\nYES\nInput\n2 4\n*.*.\n1211\n\nOutput\nNO\n\n\n-----Note-----\n\nIn the second example the answer is \"NO\" because, if the positions of the bombs are preserved, the first line of the field should be *2*1.\n\nYou can read more about Minesweeper in Wikipedia's article."} +{"id": "00235", "text": "After passing a test, Vasya got himself a box of $n$ candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.\n\nThis means the process of eating candies is the following: in the beginning Vasya chooses a single integer $k$, same for all days. After that, in the morning he eats $k$ candies from the box (if there are less than $k$ candies in the box, he eats them all), then in the evening Petya eats $10\\%$ of the candies remaining in the box. If there are still candies left in the box, the process repeats\u00a0\u2014 next day Vasya eats $k$ candies again, and Petya\u00a0\u2014 $10\\%$ of the candies left in a box, and so on.\n\nIf the amount of candies in the box is not divisible by $10$, Petya rounds the amount he takes from the box down. For example, if there were $97$ candies in the box, Petya would eat only $9$ of them. In particular, if there are less than $10$ candies in a box, Petya won't eat any at all.\n\nYour task is to find out the minimal amount of $k$ that can be chosen by Vasya so that he would eat at least half of the $n$ candies he initially got. Note that the number $k$ must be integer.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^{18}$)\u00a0\u2014 the initial amount of candies in the box.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the minimal amount of $k$ that would allow Vasya to eat at least half of candies he got.\n\n\n-----Example-----\nInput\n68\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the sample, the amount of candies, with $k=3$, would change in the following way (Vasya eats first):\n\n$68 \\to 65 \\to 59 \\to 56 \\to 51 \\to 48 \\to 44 \\to 41 \\\\ \\to 37 \\to 34 \\to 31 \\to 28 \\to 26 \\to 23 \\to 21 \\to 18 \\to 17 \\to 14 \\\\ \\to 13 \\to 10 \\to 9 \\to 6 \\to 6 \\to 3 \\to 3 \\to 0$.\n\nIn total, Vasya would eat $39$ candies, while Petya\u00a0\u2014 $29$."} +{"id": "00236", "text": "A necklace can be described as a string of links ('-') and pearls ('o'), with the last link or pearl connected to the first one. $0$ \n\nYou can remove a link or a pearl and insert it between two other existing links or pearls (or between a link and a pearl) on the necklace. This process can be repeated as many times as you like, but you can't throw away any parts.\n\nCan you make the number of links between every two adjacent pearls equal? Two pearls are considered to be adjacent if there is no other pearl between them.\n\nNote that the final necklace should remain as one circular part of the same length as the initial necklace.\n\n\n-----Input-----\n\nThe only line of input contains a string $s$ ($3 \\leq |s| \\leq 100$), representing the necklace, where a dash '-' represents a link and the lowercase English letter 'o' represents a pearl.\n\n\n-----Output-----\n\nPrint \"YES\" if the links and pearls can be rejoined such that the number of links between adjacent pearls is equal. Otherwise print \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n-o-o--\nOutput\nYES\nInput\n-o---\n\nOutput\nYES\nInput\n-o---o-\n\nOutput\nNO\nInput\nooo\n\nOutput\nYES"} +{"id": "00237", "text": "n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n \u2264 m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have. \n\nFrodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?\n\n\n-----Input-----\n\nThe only line contain three integers n, m and k (1 \u2264 n \u2264 m \u2264 10^9, 1 \u2264 k \u2264 n)\u00a0\u2014 the number of hobbits, the number of pillows and the number of Frodo's bed.\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the maximum number of pillows Frodo can have so that no one is hurt.\n\n\n-----Examples-----\nInput\n4 6 2\n\nOutput\n2\n\nInput\n3 10 3\n\nOutput\n4\n\nInput\n3 6 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.\n\nIn the second example Frodo can take at most four pillows, giving three pillows to each of the others.\n\nIn the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed."} +{"id": "00238", "text": "You are given an array $a_1, a_2, \\dots , a_n$ and two integers $m$ and $k$.\n\nYou can choose some subarray $a_l, a_{l+1}, \\dots, a_{r-1}, a_r$. \n\nThe cost of subarray $a_l, a_{l+1}, \\dots, a_{r-1}, a_r$ is equal to $\\sum\\limits_{i=l}^{r} a_i - k \\lceil \\frac{r - l + 1}{m} \\rceil$, where $\\lceil x \\rceil$ is the least integer greater than or equal to $x$. \n\nThe cost of empty subarray is equal to zero.\n\nFor example, if $m = 3$, $k = 10$ and $a = [2, -4, 15, -3, 4, 8, 3]$, then the cost of some subarrays are: $a_3 \\dots a_3: 15 - k \\lceil \\frac{1}{3} \\rceil = 15 - 10 = 5$; $a_3 \\dots a_4: (15 - 3) - k \\lceil \\frac{2}{3} \\rceil = 12 - 10 = 2$; $a_3 \\dots a_5: (15 - 3 + 4) - k \\lceil \\frac{3}{3} \\rceil = 16 - 10 = 6$; $a_3 \\dots a_6: (15 - 3 + 4 + 8) - k \\lceil \\frac{4}{3} \\rceil = 24 - 20 = 4$; $a_3 \\dots a_7: (15 - 3 + 4 + 8 + 3) - k \\lceil \\frac{5}{3} \\rceil = 27 - 20 = 7$. \n\nYour task is to find the maximum cost of some subarray (possibly empty) of array $a$.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$, and $k$ ($1 \\le n \\le 3 \\cdot 10^5, 1 \\le m \\le 10, 1 \\le k \\le 10^9$).\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^9 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint the maximum cost of some subarray of array $a$.\n\n\n-----Examples-----\nInput\n7 3 10\n2 -4 15 -3 4 8 3\n\nOutput\n7\n\nInput\n5 2 1000\n-13 -4 -9 -20 -11\n\nOutput\n0"} +{"id": "00239", "text": "You are given a rectangular grid of lattice points from (0, 0) to (n, m) inclusive. You have to choose exactly 4 different points to build a polyline possibly with self-intersections and self-touching. This polyline should be as long as possible.\n\nA polyline defined by points p_1, p_2, p_3, p_4 consists of the line segments p_1 p_2, p_2 p_3, p_3 p_4, and its length is the sum of the lengths of the individual line segments.\n\n\n-----Input-----\n\nThe only line of the input contains two integers n and m (0 \u2264 n, m \u2264 1000). It is guaranteed that grid contains at least 4 different points.\n\n\n-----Output-----\n\nPrint 4 lines with two integers per line separated by space \u2014 coordinates of points p_1, p_2, p_3, p_4 in order which represent the longest possible polyline.\n\nJudge program compares your answer and jury's answer with 10^{ - 6} precision.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n1 1\n0 0\n1 0\n0 1\n\nInput\n0 10\n\nOutput\n0 1\n0 10\n0 0\n0 9"} +{"id": "00240", "text": "Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k\u00a0\u2014 the number of digits in n. \n\nMagically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of n (a substring of n is a sequence of consecutive digits of the number n).\n\nVasya knows that there may be more than one way to restore the number n. Your task is to find the smallest possible initial integer n. Note that decimal representation of number n contained no leading zeroes, except the case the integer n was equal to zero itself (in this case a single digit 0 was used).\n\n\n-----Input-----\n\nThe first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1 000 000.\n\nThe second line contains the substring of n which Vasya remembers. This string can contain leading zeroes. \n\nIt is guaranteed that the input data is correct, and the answer always exists.\n\n\n-----Output-----\n\nPrint the smalles integer n which Vasya could pass to Kate.\n\n\n-----Examples-----\nInput\n003512\n021\n\nOutput\n30021\n\nInput\n199966633300\n63\n\nOutput\n3036366999"} +{"id": "00241", "text": "Not so long ago company R2 bought company R1 and consequently, all its developments in the field of multicore processors. Now the R2 laboratory is testing one of the R1 processors.\n\nThe testing goes in n steps, at each step the processor gets some instructions, and then its temperature is measured. The head engineer in R2 is keeping a report record on the work of the processor: he writes down the minimum and the maximum measured temperature in his notebook. His assistant had to write down all temperatures into his notebook, but (for unknown reasons) he recorded only m.\n\nThe next day, the engineer's assistant filed in a report with all the m temperatures. However, the chief engineer doubts that the assistant wrote down everything correctly (naturally, the chief engineer doesn't doubt his notes). So he asked you to help him. Given numbers n, m, min, max and the list of m temperatures determine whether you can upgrade the set of m temperatures to the set of n temperatures (that is add n - m temperatures), so that the minimum temperature was min and the maximum one was max.\n\n\n-----Input-----\n\nThe first line contains four integers n, m, min, max (1 \u2264 m < n \u2264 100;\u00a01 \u2264 min < max \u2264 100). The second line contains m space-separated integers t_{i} (1 \u2264 t_{i} \u2264 100) \u2014 the temperatures reported by the assistant.\n\nNote, that the reported temperatures, and the temperatures you want to add can contain equal temperatures.\n\n\n-----Output-----\n\nIf the data is consistent, print 'Correct' (without the quotes). Otherwise, print 'Incorrect' (without the quotes).\n\n\n-----Examples-----\nInput\n2 1 1 2\n1\n\nOutput\nCorrect\n\nInput\n3 1 1 3\n2\n\nOutput\nCorrect\n\nInput\n2 1 1 3\n2\n\nOutput\nIncorrect\n\n\n\n-----Note-----\n\nIn the first test sample one of the possible initial configurations of temperatures is [1, 2].\n\nIn the second test sample one of the possible initial configurations of temperatures is [2, 1, 3].\n\nIn the third test sample it is impossible to add one temperature to obtain the minimum equal to 1 and the maximum equal to 3."} +{"id": "00242", "text": "Mr. Santa asks all the great programmers of the world to solve a trivial problem. He gives them an integer m and asks for the number of positive integers n, such that the factorial of n ends with exactly m zeroes. Are you among those great programmers who can solve this problem?\n\n\n-----Input-----\n\nThe only line of input contains an integer m (1 \u2264 m \u2264 100 000)\u00a0\u2014 the required number of trailing zeroes in factorial.\n\n\n-----Output-----\n\nFirst print k\u00a0\u2014 the number of values of n such that the factorial of n ends with m zeroes. Then print these k integers in increasing order.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n5\n5 6 7 8 9 \nInput\n5\n\nOutput\n0\n\n\n-----Note-----\n\nThe factorial of n is equal to the product of all integers from 1 to n inclusive, that is n! = 1\u00b72\u00b73\u00b7...\u00b7n.\n\nIn the first sample, 5! = 120, 6! = 720, 7! = 5040, 8! = 40320 and 9! = 362880."} +{"id": "00243", "text": "Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.\n\nYou are given a connected undirected graph with $n$ vertices and $m$ weighted edges. There are $k$ special vertices: $x_1, x_2, \\ldots, x_k$.\n\nLet's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.\n\nFor each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.\n\nThe original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $k$ ($2 \\leq k \\leq n \\leq 10^5$, $n-1 \\leq m \\leq 10^5$)\u00a0\u2014 the number of vertices, the number of edges and the number of special vertices.\n\nThe second line contains $k$ distinct integers $x_1, x_2, \\ldots, x_k$ ($1 \\leq x_i \\leq n$).\n\nEach of the following $m$ lines contains three integers $u$, $v$ and $w$ ($1 \\leq u,v \\leq n, 1 \\leq w \\leq 10^9$), denoting there is an edge between $u$ and $v$ of weight $w$. The given graph is undirected, so an edge $(u, v)$ can be used in the both directions.\n\nThe graph may have multiple edges and self-loops.\n\nIt is guaranteed, that the graph is connected.\n\n\n-----Output-----\n\nThe first and only line should contain $k$ integers. The $i$-th integer is the distance between $x_i$ and the farthest special vertex from it.\n\n\n-----Examples-----\nInput\n2 3 2\n2 1\n1 2 3\n1 2 2\n2 2 1\n\nOutput\n2 2 \n\nInput\n4 5 3\n1 2 3\n1 2 5\n4 2 1\n2 3 2\n1 4 4\n1 3 3\n\nOutput\n3 3 3 \n\n\n\n-----Note-----\n\nIn the first example, the distance between vertex $1$ and $2$ equals to $2$ because one can walk through the edge of weight $2$ connecting them. So the distance to the farthest node for both $1$ and $2$ equals to $2$.\n\nIn the second example, one can find that distance between $1$ and $2$, distance between $1$ and $3$ are both $3$ and the distance between $2$ and $3$ is $2$.\n\nThe graph may have multiple edges between and self-loops, as in the first example."} +{"id": "00244", "text": "Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess the current position of the ball.\n\nBomboslav noticed that guys are not very inventive, so the operator always swaps the left shell with the middle one during odd moves (first, third, fifth, etc.) and always swaps the middle shell with the right one during even moves (second, fourth, etc.).\n\nLet's number shells from 0 to 2 from left to right. Thus the left shell is assigned number 0, the middle shell is 1 and the right shell is 2. Bomboslav has missed the moment when the ball was placed beneath the shell, but he knows that exactly n movements were made by the operator and the ball was under shell x at the end. Now he wonders, what was the initial position of the ball?\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 2\u00b710^9)\u00a0\u2014 the number of movements made by the operator.\n\nThe second line contains a single integer x (0 \u2264 x \u2264 2)\u00a0\u2014 the index of the shell where the ball was found after n movements.\n\n\n-----Output-----\n\nPrint one integer from 0 to 2\u00a0\u2014 the index of the shell where the ball was initially placed.\n\n\n-----Examples-----\nInput\n4\n2\n\nOutput\n1\n\nInput\n1\n1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, the ball was initially placed beneath the middle shell and the operator completed four movements. During the first move operator swapped the left shell and the middle shell. The ball is now under the left shell. During the second move operator swapped the middle shell and the right one. The ball is still under the left shell. During the third move operator swapped the left shell and the middle shell again. The ball is again in the middle. Finally, the operators swapped the middle shell and the right shell. The ball is now beneath the right shell."} +{"id": "00245", "text": "You are given n rectangles. The corners of rectangles have integer coordinates and their edges are parallel to the Ox and Oy axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle). \n\nYour task is to determine if the rectangles form a square. In other words, determine if the set of points inside or on the border of at least one rectangle is precisely equal to the set of points inside or on the border of some square.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 5). Next n lines contain four integers each, describing a single rectangle: x_1, y_1, x_2, y_2 (0 \u2264 x_1 < x_2 \u2264 31400, 0 \u2264 y_1 < y_2 \u2264 31400) \u2014 x_1 and x_2 are x-coordinates of the left and right edges of the rectangle, and y_1 and y_2 are y-coordinates of the bottom and top edges of the rectangle. \n\nNo two rectangles overlap (that is, there are no points that belong to the interior of more than one rectangle).\n\n\n-----Output-----\n\nIn a single line print \"YES\", if the given rectangles form a square, or \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n5\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5\n2 2 3 3\n\nOutput\nYES\n\nInput\n4\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5\n\nOutput\nNO"} +{"id": "00246", "text": "Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks that a positive integer number x is really big if the difference between x and the sum of its digits (in decimal representation) is not less than s. To prove that these numbers may have different special properties, he wants to know how rare (or not rare) they are \u2014 in fact, he needs to calculate the quantity of really big numbers that are not greater than n.\n\nIvan tried to do the calculations himself, but soon realized that it's too difficult for him. So he asked you to help him in calculations.\n\n\n-----Input-----\n\nThe first (and the only) line contains two integers n and s (1 \u2264 n, s \u2264 10^18).\n\n\n-----Output-----\n\nPrint one integer \u2014 the quantity of really big numbers that are not greater than n.\n\n\n-----Examples-----\nInput\n12 1\n\nOutput\n3\n\nInput\n25 20\n\nOutput\n0\n\nInput\n10 9\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example numbers 10, 11 and 12 are really big.\n\nIn the second example there are no really big numbers that are not greater than 25 (in fact, the first really big number is 30: 30 - 3 \u2265 20).\n\nIn the third example 10 is the only really big number (10 - 1 \u2265 9)."} +{"id": "00247", "text": "You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordinates are integers), and all points are distinct.\n\nYou may draw two straight lines (not necessarily distinct). Is it possible to do this in such a way that every point lies on at least one of these lines?\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 10^5) \u2014 the number of points you are given.\n\nThen n lines follow, each line containing two integers x_{i} and y_{i} (|x_{i}|, |y_{i}| \u2264 10^9)\u2014 coordinates of i-th point. All n points are distinct.\n\n\n-----Output-----\n\nIf it is possible to draw two straight lines in such a way that each of given points belongs to at least one of these lines, print YES. Otherwise, print NO.\n\n\n-----Examples-----\nInput\n5\n0 0\n0 1\n1 1\n1 -1\n2 2\n\nOutput\nYES\n\nInput\n5\n0 0\n1 0\n2 1\n1 1\n2 3\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example it is possible to draw two lines, the one containing the points 1, 3 and 5, and another one containing two remaining points. [Image]"} +{"id": "00248", "text": "Memory and his friend Lexa are competing to get higher score in one popular computer game. Memory starts with score a and Lexa starts with score b. In a single turn, both Memory and Lexa get some integer in the range [ - k;k] (i.e. one integer among - k, - k + 1, - k + 2, ..., - 2, - 1, 0, 1, 2, ..., k - 1, k) and add them to their current scores. The game has exactly t turns. Memory and Lexa, however, are not good at this game, so they both always get a random integer at their turn.\n\nMemory wonders how many possible games exist such that he ends with a strictly higher score than Lexa. Two games are considered to be different if in at least one turn at least one player gets different score. There are (2k + 1)^2t games in total. Since the answer can be very large, you should print it modulo 10^9 + 7. Please solve this problem for Memory.\n\n\n-----Input-----\n\nThe first and only line of input contains the four integers a, b, k, and t (1 \u2264 a, b \u2264 100, 1 \u2264 k \u2264 1000, 1 \u2264 t \u2264 100)\u00a0\u2014 the amount Memory and Lexa start with, the number k, and the number of turns respectively.\n\n\n-----Output-----\n\nPrint the number of possible games satisfying the conditions modulo 1 000 000 007 (10^9 + 7) in one line.\n\n\n-----Examples-----\nInput\n1 2 2 1\n\nOutput\n6\n\nInput\n1 1 1 2\n\nOutput\n31\n\nInput\n2 12 3 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample test, Memory starts with 1 and Lexa starts with 2. If Lexa picks - 2, Memory can pick 0, 1, or 2 to win. If Lexa picks - 1, Memory can pick 1 or 2 to win. If Lexa picks 0, Memory can pick 2 to win. If Lexa picks 1 or 2, Memory cannot win. Thus, there are 3 + 2 + 1 = 6 possible games in which Memory wins."} +{"id": "00249", "text": "Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! \n\nHowever, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a_1, a_2, ..., a_{n}, where a_{i} denotes the distance of the i-th mark from the origin (a_1 = 0, a_{n} = l).\n\nValery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 \u2264 i \u2264 j \u2264 n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, a_{j} - a_{i} = d). \n\nUnder the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. \n\nYour task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.\n\n\n-----Input-----\n\nThe first line contains four positive space-separated integers n, l, x, y (2 \u2264 n \u2264 10^5, 2 \u2264 l \u2264 10^9, 1 \u2264 x < y \u2264 l) \u2014 the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly.\n\nThe second line contains a sequence of n integers a_1, a_2, ..., a_{n} (0 = a_1 < a_2 < ... < a_{n} = l), where a_{i} shows the distance from the i-th mark to the origin.\n\n\n-----Output-----\n\nIn the first line print a single non-negative integer v \u2014 the minimum number of marks that you need to add on the ruler.\n\nIn the second line print v space-separated integers p_1, p_2, ..., p_{v} (0 \u2264 p_{i} \u2264 l). Number p_{i} means that the i-th mark should be at the distance of p_{i} centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n3 250 185 230\n0 185 250\n\nOutput\n1\n230\n\nInput\n4 250 185 230\n0 20 185 250\n\nOutput\n0\n\nInput\n2 300 185 230\n0 300\n\nOutput\n2\n185 230\n\n\n\n-----Note-----\n\nIn the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark.\n\nIn the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks.\n\nIn the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills."} +{"id": "00250", "text": "As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.\n\nSimple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.\n\nHowever, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.\n\nBabaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of simple cakes Babaei has.\n\nEach of the following n lines contains two integers r_{i} and h_{i} (1 \u2264 r_{i}, h_{i} \u2264 10 000), giving the radius and height of the i-th cake.\n\n\n-----Output-----\n\nPrint the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}.\n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n2\n100 30\n40 10\n\nOutput\n942477.796077000\n\nInput\n4\n1 1\n9 7\n1 4\n10 7\n\nOutput\n3983.539484752\n\n\n\n-----Note-----\n\nIn first sample, the optimal way is to choose the cake number 1.\n\nIn second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4."} +{"id": "00251", "text": "There is a toy building consisting of $n$ towers. Each tower consists of several cubes standing on each other. The $i$-th tower consists of $h_i$ cubes, so it has height $h_i$.\n\nLet's define operation slice on some height $H$ as following: for each tower $i$, if its height is greater than $H$, then remove some top cubes to make tower's height equal to $H$. Cost of one \"slice\" equals to the total number of removed cubes from all towers.\n\nLet's name slice as good one if its cost is lower or equal to $k$ ($k \\ge n$).\n\n [Image] \n\nCalculate the minimum number of good slices you have to do to make all towers have the same height. Of course, it is always possible to make it so.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n \\le 2 \\cdot 10^5$, $n \\le k \\le 10^9$) \u2014 the number of towers and the restriction on slices, respectively.\n\nThe second line contains $n$ space separated integers $h_1, h_2, \\dots, h_n$ ($1 \\le h_i \\le 2 \\cdot 10^5$) \u2014 the initial heights of towers.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of good slices you have to do to make all towers have the same heigth.\n\n\n-----Examples-----\nInput\n5 5\n3 1 2 2 4\n\nOutput\n2\n\nInput\n4 5\n2 3 4 5\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example it's optimal to make $2$ slices. The first slice is on height $2$ (its cost is $3$), and the second one is on height $1$ (its cost is $4$)."} +{"id": "00252", "text": "Alice and Bob are playing yet another card game. This time the rules are the following. There are $n$ cards lying in a row in front of them. The $i$-th card has value $a_i$. \n\nFirst, Alice chooses a non-empty consecutive segment of cards $[l; r]$ ($l \\le r$). After that Bob removes a single card $j$ from that segment $(l \\le j \\le r)$. The score of the game is the total value of the remaining cards on the segment $(a_l + a_{l + 1} + \\dots + a_{j - 1} + a_{j + 1} + \\dots + a_{r - 1} + a_r)$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $0$.\n\nAlice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.\n\nWhat segment should Alice choose so that the score is maximum possible? Output the maximum score.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$) \u2014 the number of cards.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-30 \\le a_i \\le 30$) \u2014 the values on the cards.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the final score of the game.\n\n\n-----Examples-----\nInput\n5\n5 -2 10 -1 4\n\nOutput\n6\n\nInput\n8\n5 2 5 3 -30 -30 6 9\n\nOutput\n10\n\nInput\n3\n-10 6 -15\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example Alice chooses a segment $[1;5]$ \u2014 the entire row of cards. Bob removes card $3$ with the value $10$ from the segment. Thus, the final score is $5 + (-2) + (-1) + 4 = 6$.\n\nIn the second example Alice chooses a segment $[1;4]$, so that Bob removes either card $1$ or $3$ with the value $5$, making the answer $5 + 2 + 3 = 10$.\n\nIn the third example Alice can choose any of the segments of length $1$: $[1;1]$, $[2;2]$ or $[3;3]$. Bob removes the only card, so the score is $0$. If Alice chooses some other segment then the answer will be less than $0$."} +{"id": "00253", "text": "Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.\n\nWhen a garland is switched on, it periodically changes its state \u2014 sometimes it is lit, sometimes not. Formally, if i-th garland is switched on during x-th second, then it is lit only during seconds x, x + k_{i}, x + 2k_{i}, x + 3k_{i} and so on.\n\nMishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers x_1, x_2 and x_3 (not necessarily distinct) so that he will switch on the first garland during x_1-th second, the second one \u2014 during x_2-th second, and the third one \u2014 during x_3-th second, respectively, and during each second starting from max(x_1, x_2, x_3) at least one garland will be lit.\n\nHelp Mishka by telling him if it is possible to do this!\n\n\n-----Input-----\n\nThe first line contains three integers k_1, k_2 and k_3 (1 \u2264 k_{i} \u2264 1500) \u2014 time intervals of the garlands.\n\n\n-----Output-----\n\nIf Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES.\n\nOtherwise, print NO.\n\n\n-----Examples-----\nInput\n2 2 3\n\nOutput\nYES\n\nInput\n4 2 3\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example Mishka can choose x_1 = 1, x_2 = 2, x_3 = 1. The first garland will be lit during seconds 1, 3, 5, 7, ..., the second \u2014 2, 4, 6, 8, ..., which already cover all the seconds after the 2-nd one. It doesn't even matter what x_3 is chosen. Our choice will lead third to be lit during seconds 1, 4, 7, 10, ..., though.\n\nIn the second example there is no way to choose such moments of time, there always be some seconds when no garland is lit."} +{"id": "00254", "text": "You are given a string $s$ of length $n$ consisting of lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.\n\nCalculate the minimum number of operations to delete the whole string $s$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 500$) \u2014 the length of string $s$.\n\nThe second line contains the string $s$ ($|s| = n$) consisting of lowercase Latin letters.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the minimal number of operation to delete string $s$.\n\n\n-----Examples-----\nInput\n5\nabaca\n\nOutput\n3\nInput\n8\nabcddcba\n\nOutput\n4"} +{"id": "00255", "text": "The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.\n\nWe know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.\n\nFor each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 100) \u2014 the number of boys. The second line contains sequence a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100), where a_{i} is the i-th boy's dancing skill.\n\nSimilarly, the third line contains an integer m (1 \u2264 m \u2264 100) \u2014 the number of girls. The fourth line contains sequence b_1, b_2, ..., b_{m} (1 \u2264 b_{j} \u2264 100), where b_{j} is the j-th girl's dancing skill.\n\n\n-----Output-----\n\nPrint a single number \u2014 the required maximum possible number of pairs.\n\n\n-----Examples-----\nInput\n4\n1 4 6 2\n5\n5 1 5 7 9\n\nOutput\n3\n\nInput\n4\n1 2 3 4\n4\n10 11 12 13\n\nOutput\n0\n\nInput\n5\n1 1 1 1 1\n3\n1 2 3\n\nOutput\n2"} +{"id": "00256", "text": "Kicker (table football) is a board game based on football, in which players control the footballers' figures mounted on rods by using bars to get the ball into the opponent's goal. When playing two on two, one player of each team controls the goalkeeper and the full-backs (plays defence), the other player controls the half-backs and forwards (plays attack).\n\nTwo teams of company Q decided to battle each other. Let's enumerate players from both teams by integers from 1 to 4. The first and second player play in the first team, the third and the fourth one play in the second team. For each of the four players we know their game skills in defence and attack. The defence skill of the i-th player is a_{i}, the attack skill is b_{i}.\n\nBefore the game, the teams determine how they will play. First the players of the first team decide who will play in the attack, and who will play in the defence. Then the second team players do the same, based on the choice of their opponents.\n\nWe will define a team's defence as the defence skill of player of the team who plays defence. Similarly, a team's attack is the attack skill of the player of the team who plays attack. We assume that one team is guaranteed to beat the other one, if its defence is strictly greater than the opponent's attack and its attack is strictly greater than the opponent's defence.\n\nThe teams of company Q know each other's strengths and therefore arrange their teams optimally. Identify the team that is guaranteed to win (if both teams act optimally) or tell that there is no such team.\n\n\n-----Input-----\n\nThe input contain the players' description in four lines. The i-th line contains two space-separated integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 100) \u2014 the defence and the attack skill of the i-th player, correspondingly.\n\n\n-----Output-----\n\nIf the first team can win, print phrase \"Team 1\" (without the quotes), if the second team can win, print phrase \"Team 2\" (without the quotes). If no of the teams can definitely win, print \"Draw\" (without the quotes).\n\n\n-----Examples-----\nInput\n1 100\n100 1\n99 99\n99 99\n\nOutput\nTeam 1\n\nInput\n1 1\n2 2\n3 3\n2 2\n\nOutput\nTeam 2\n\nInput\n3 3\n2 2\n1 1\n2 2\n\nOutput\nDraw\n\n\n\n-----Note-----\n\nLet consider the first test sample. The first team can definitely win if it will choose the following arrangement: the first player plays attack, the second player plays defence.\n\nConsider the second sample. The order of the choosing roles for players makes sense in this sample. As the members of the first team choose first, the members of the second team can beat them (because they know the exact defence value and attack value of the first team)."} +{"id": "00257", "text": "Takahashi wants to grill N pieces of meat on a grilling net, which can be seen as a two-dimensional plane. The coordinates of the i-th piece of meat are \\left(x_i, y_i\\right), and its hardness is c_i.\nTakahashi can use one heat source to grill the meat. If he puts the heat source at coordinates \\left(X, Y\\right), where X and Y are real numbers, the i-th piece of meat will be ready to eat in c_i \\times \\sqrt{\\left(X - x_i\\right)^2 + \\left(Y-y_i\\right)^2} seconds.\nTakahashi wants to eat K pieces of meat. Find the time required to have K or more pieces of meat ready if he put the heat source to minimize this time.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 60\n - 1 \\leq K \\leq N\n - -1000 \\leq x_i , y_i \\leq 1000\n - \\left(x_i, y_i\\right) \\neq \\left(x_j, y_j\\right) \\left(i \\neq j \\right)\n - 1 \\leq c_i \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nx_1 y_1 c_1\n\\vdots\nx_N y_N c_N\n\n-----Output-----\nPrint the answer.\nIt will be considered correct if its absolute or relative error from our answer is at most 10^{-6}.\n\n-----Sample Input-----\n4 3\n-1 0 3\n0 0 3\n1 0 2\n1 1 40\n\n-----Sample Output-----\n2.4\n\nIf we put the heat source at \\left(-0.2, 0\\right), the 1-st, 2-nd, and 3-rd pieces of meat will be ready to eat within 2.4 seconds. This is the optimal place to put the heat source."} +{"id": "00258", "text": "Monocarp and Bicarp live in Berland, where every bus ticket consists of $n$ digits ($n$ is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.\n\nMonocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first $\\frac{n}{2}$ digits of this ticket is equal to the sum of the last $\\frac{n}{2}$ digits.\n\nMonocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from $0$ to $9$. The game ends when there are no erased digits in the ticket.\n\nIf the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.\n\n\n-----Input-----\n\nThe first line contains one even integer $n$ $(2 \\le n \\le 2 \\cdot 10^{5})$ \u2014 the number of digits in the ticket.\n\nThe second line contains a string of $n$ digits and \"?\" characters \u2014 the ticket which Monocarp and Bicarp have found. If the $i$-th character is \"?\", then the $i$-th digit is erased. Note that there may be leading zeroes. The number of \"?\" characters is even.\n\n\n-----Output-----\n\nIf Monocarp wins, print \"Monocarp\" (without quotes). Otherwise print \"Bicarp\" (without quotes).\n\n\n-----Examples-----\nInput\n4\n0523\n\nOutput\nBicarp\n\nInput\n2\n??\n\nOutput\nBicarp\n\nInput\n8\n?054??0?\n\nOutput\nBicarp\n\nInput\n6\n???00?\n\nOutput\nMonocarp\n\n\n\n-----Note-----\n\nSince there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.\n\nIn the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy."} +{"id": "00259", "text": "It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.\n\nServal will go to the bus station at time $t$, and there are $n$ bus routes which stop at this station. For the $i$-th bus route, the first bus arrives at time $s_i$ minutes, and each bus of this route comes $d_i$ minutes later than the previous one.\n\nAs Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers $n$ and $t$ ($1\\leq n\\leq 100$, $1\\leq t\\leq 10^5$)\u00a0\u2014 the number of bus routes and the time Serval goes to the station. \n\nEach of the next $n$ lines contains two space-separated integers $s_i$ and $d_i$ ($1\\leq s_i,d_i\\leq 10^5$)\u00a0\u2014 the time when the first bus of this route arrives and the interval between two buses of this route.\n\n\n-----Output-----\n\nPrint one number\u00a0\u2014 what bus route Serval will use. If there are several possible answers, you can print any of them.\n\n\n-----Examples-----\nInput\n2 2\n6 4\n9 5\n\nOutput\n1\n\nInput\n5 5\n3 3\n2 5\n5 6\n4 9\n6 1\n\nOutput\n3\n\nInput\n3 7\n2 2\n2 3\n2 4\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, the first bus of the first route arrives at time $6$, and the first bus of the second route arrives at time $9$, so the first route is the answer.\n\nIn the second example, a bus of the third route arrives at time $5$, so it is the answer.\n\nIn the third example, buses of the first route come at times $2$, $4$, $6$, $8$, and so fourth, buses of the second route come at times $2$, $5$, $8$, and so fourth and buses of the third route come at times $2$, $6$, $10$, and so on, so $1$ and $2$ are both acceptable answers while $3$ is not."} +{"id": "00260", "text": "One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: \"Find such positive integer n, that among numbers n + 1, n + 2, ..., 2\u00b7n there are exactly m numbers which binary representation contains exactly k digits one\".\n\nThe girl got interested in the task and she asked you to help her solve it. Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 10^18.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers, m and k (0 \u2264 m \u2264 10^18; 1 \u2264 k \u2264 64).\n\n\n-----Output-----\n\nPrint the required number n (1 \u2264 n \u2264 10^18). If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n1\n\nInput\n3 2\n\nOutput\n5"} +{"id": "00261", "text": "In this problem you will meet the simplified model of game King of Thieves.\n\nIn a new ZeptoLab game called \"King of Thieves\" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way. [Image] \n\nAn interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.\n\nA dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'. \n\nOne of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i_1, he can make a sequence of jumps through the platforms i_1 < i_2 < ... < i_{k}, if i_2 - i_1 = i_3 - i_2 = ... = i_{k} - i_{k} - 1. Of course, all segments i_1, i_2, ... i_{k} should be exactly the platforms, not pits. \n\nLet's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i_1, i_2, ..., i_5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100) \u2014 the number of segments on the level.\n\nNext line contains the scheme of the level represented as a string of n characters '*' and '.'.\n\n\n-----Output-----\n\nIf the level is good, print the word \"yes\" (without the quotes), otherwise print the word \"no\" (without the quotes).\n\n\n-----Examples-----\nInput\n16\n.**.*..*.***.**.\n\nOutput\nyes\nInput\n11\n.*.*...*.*.\n\nOutput\nno\n\n\n-----Note-----\n\nIn the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14."} +{"id": "00262", "text": "ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n \u00d7 n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.\n\nChris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid ($\\sum a_{r, i}$), each column of the grid ($\\sum a_{i, c}$), and the two long diagonals of the grid (the main diagonal\u00a0\u2014 $\\sum a_{i, i}$ and the secondary diagonal\u00a0\u2014 $\\sum a_{i, n - i + 1}$) are equal. \n\nChris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 500)\u00a0\u2014 the number of rows and columns of the magic grid.\n\nn lines follow, each of them contains n integers. The j-th number in the i-th of them denotes a_{i}, j (1 \u2264 a_{i}, j \u2264 10^9 or a_{i}, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, a_{i}, j will be equal to 0. Otherwise, a_{i}, j is positive.\n\nIt is guaranteed that there is exactly one pair of integers i, j (1 \u2264 i, j \u2264 n) such that a_{i}, j = 0.\n\n\n-----Output-----\n\nOutput a single integer, the positive integer x (1 \u2264 x \u2264 10^18) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output - 1 instead.\n\nIf there are multiple solutions, you may print any of them.\n\n\n-----Examples-----\nInput\n3\n4 0 2\n3 5 7\n8 1 6\n\nOutput\n9\n\nInput\n4\n1 1 1 1\n1 1 0 1\n1 1 1 1\n1 1 1 1\n\nOutput\n1\n\nInput\n4\n1 1 1 1\n1 1 0 1\n1 1 2 1\n1 1 1 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed, \n\nThe sum of numbers in each row is:\n\n4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.\n\nThe sum of numbers in each column is:\n\n4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.\n\nThe sum of numbers in the two diagonals is:\n\n4 + 5 + 6 = 2 + 5 + 8 = 15.\n\nIn the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square."} +{"id": "00263", "text": "There are $n$ benches in the Berland Central park. It is known that $a_i$ people are currently sitting on the $i$-th bench. Another $m$ people are coming to the park and each of them is going to have a seat on some bench out of $n$ available.\n\nLet $k$ be the maximum number of people sitting on one bench after additional $m$ people came to the park. Calculate the minimum possible $k$ and the maximum possible $k$.\n\nNobody leaves the taken seat during the whole process.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ $(1 \\le n \\le 100)$ \u2014 the number of benches in the park.\n\nThe second line contains a single integer $m$ $(1 \\le m \\le 10\\,000)$ \u2014 the number of people additionally coming to the park.\n\nEach of the next $n$ lines contains a single integer $a_i$ $(1 \\le a_i \\le 100)$ \u2014 the initial number of people on the $i$-th bench.\n\n\n-----Output-----\n\nPrint the minimum possible $k$ and the maximum possible $k$, where $k$ is the maximum number of people sitting on one bench after additional $m$ people came to the park.\n\n\n-----Examples-----\nInput\n4\n6\n1\n1\n1\n1\n\nOutput\n3 7\n\nInput\n1\n10\n5\n\nOutput\n15 15\n\nInput\n3\n6\n1\n6\n5\n\nOutput\n6 12\n\nInput\n3\n7\n1\n6\n5\n\nOutput\n7 13\n\n\n\n-----Note-----\n\nIn the first example, each of four benches is occupied by a single person. The minimum $k$ is $3$. For example, it is possible to achieve if two newcomers occupy the first bench, one occupies the second bench, one occupies the third bench, and two remaining \u2014 the fourth bench. The maximum $k$ is $7$. That requires all six new people to occupy the same bench.\n\nThe second example has its minimum $k$ equal to $15$ and maximum $k$ equal to $15$, as there is just a single bench in the park and all $10$ people will occupy it."} +{"id": "00264", "text": "There is an airplane which has n rows from front to back. There will be m people boarding this airplane.\n\nThis airplane has an entrance at the very front and very back of the plane.\n\nEach person has some assigned seat. It is possible for multiple people to have the same assigned seat. The people will then board the plane one by one starting with person 1. Each person can independently choose either the front entrance or back entrance to enter the plane.\n\nWhen a person walks into the plane, they walk directly to their assigned seat and will try to sit in it. If it is occupied, they will continue walking in the direction they walked in until they are at empty seat - they will take the earliest empty seat that they can find. If they get to the end of the row without finding a seat, they will be angry.\n\nFind the number of ways to assign tickets to the passengers and board the plane without anyone getting angry. Two ways are different if there exists a passenger who chose a different entrance in both ways, or the assigned seat is different. Print this count modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line of input will contain two integers n, m (1 \u2264 m \u2264 n \u2264 1 000 000), the number of seats, and the number of passengers, respectively.\n\n\n-----Output-----\n\nPrint a single number, the number of ways, modulo 10^9 + 7.\n\n\n-----Example-----\nInput\n3 3\n\nOutput\n128\n\n\n\n-----Note-----\n\nHere, we will denote a passenger by which seat they were assigned, and which side they came from (either \"F\" or \"B\" for front or back, respectively).\n\nFor example, one valid way is 3B, 3B, 3B (i.e. all passengers were assigned seat 3 and came from the back entrance). Another valid way would be 2F, 1B, 3F.\n\nOne invalid way would be 2B, 2B, 2B, since the third passenger would get to the front without finding a seat."} +{"id": "00265", "text": "A company of $n$ friends wants to order exactly two pizzas. It is known that in total there are $9$ pizza ingredients in nature, which are denoted by integers from $1$ to $9$.\n\nEach of the $n$ friends has one or more favorite ingredients: the $i$-th of friends has the number of favorite ingredients equal to $f_i$ ($1 \\le f_i \\le 9$) and your favorite ingredients form the sequence $b_{i1}, b_{i2}, \\dots, b_{if_i}$ ($1 \\le b_{it} \\le 9$).\n\nThe website of CodePizza restaurant has exactly $m$ ($m \\ge 2$) pizzas. Each pizza is characterized by a set of $r_j$ ingredients $a_{j1}, a_{j2}, \\dots, a_{jr_j}$ ($1 \\le r_j \\le 9$, $1 \\le a_{jt} \\le 9$) , which are included in it, and its price is $c_j$.\n\nHelp your friends choose exactly two pizzas in such a way as to please the maximum number of people in the company. It is known that a person is pleased with the choice if each of his/her favorite ingredients is in at least one ordered pizza. If there are several ways to choose two pizzas so as to please the maximum number of friends, then choose the one that minimizes the total price of two pizzas.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n \\le 10^5, 2 \\le m \\le 10^5$) \u2014 the number of friends in the company and the number of pizzas, respectively.\n\nNext, the $n$ lines contain descriptions of favorite ingredients of the friends: the $i$-th of them contains the number of favorite ingredients $f_i$ ($1 \\le f_i \\le 9$) and a sequence of distinct integers $b_{i1}, b_{i2}, \\dots, b_{if_i}$ ($1 \\le b_{it} \\le 9$).\n\nNext, the $m$ lines contain pizza descriptions: the $j$-th of them contains the integer price of the pizza $c_j$ ($1 \\le c_j \\le 10^9$), the number of ingredients $r_j$ ($1 \\le r_j \\le 9$) and the ingredients themselves as a sequence of distinct integers $a_{j1}, a_{j2}, \\dots, a_{jr_j}$ ($1 \\le a_{jt} \\le 9$).\n\n\n-----Output-----\n\nOutput two integers $j_1$ and $j_2$ ($1 \\le j_1,j_2 \\le m$, $j_1 \\ne j_2$) denoting the indices of two pizzas in the required set. If there are several solutions, output any of them. Pizza indices can be printed in any order.\n\n\n-----Examples-----\nInput\n3 4\n2 6 7\n4 2 3 9 5\n3 2 3 9\n100 1 7\n400 3 3 2 5\n100 2 9 2\n500 3 2 9 5\n\nOutput\n2 3\n\nInput\n4 3\n1 1\n1 2\n1 3\n1 4\n10 4 1 2 3 4\n20 4 1 2 3 4\n30 4 1 2 3 4\n\nOutput\n1 2\n\nInput\n1 5\n9 9 8 7 6 5 4 3 2 1\n3 4 1 2 3 4\n1 4 5 6 7 8\n4 4 1 3 5 7\n1 4 2 4 6 8\n5 4 1 9 2 8\n\nOutput\n2 4"} +{"id": "00266", "text": "You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.\n\n\n-----Input-----\n\nThe single line of the input contains a pair of integers m, s (1 \u2264 m \u2264 100, 0 \u2264 s \u2264 900) \u2014 the length and the sum of the digits of the required numbers.\n\n\n-----Output-----\n\nIn the output print the pair of the required non-negative integer numbers \u2014 first the minimum possible number, then \u2014 the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers \"-1 -1\" (without the quotes).\n\n\n-----Examples-----\nInput\n2 15\n\nOutput\n69 96\n\nInput\n3 0\n\nOutput\n-1 -1"} +{"id": "00267", "text": "You are given two integers $l$ and $r$ ($l \\le r$). Your task is to calculate the sum of numbers from $l$ to $r$ (including $l$ and $r$) such that each number contains at most $k$ different digits, and print this sum modulo $998244353$.\n\nFor example, if $k = 1$ then you have to calculate all numbers from $l$ to $r$ such that each number is formed using only one digit. For $l = 10, r = 50$ the answer is $11 + 22 + 33 + 44 = 110$.\n\n\n-----Input-----\n\nThe only line of the input contains three integers $l$, $r$ and $k$ ($1 \\le l \\le r < 10^{18}, 1 \\le k \\le 10$) \u2014 the borders of the segment and the maximum number of different digits.\n\n\n-----Output-----\n\nPrint one integer \u2014 the sum of numbers from $l$ to $r$ such that each number contains at most $k$ different digits, modulo $998244353$.\n\n\n-----Examples-----\nInput\n10 50 2\n\nOutput\n1230\n\nInput\n1 2345 10\n\nOutput\n2750685\n\nInput\n101 154 2\n\nOutput\n2189\n\n\n\n-----Note-----\n\nFor the first example the answer is just the sum of numbers from $l$ to $r$ which equals to $\\frac{50 \\cdot 51}{2} - \\frac{9 \\cdot 10}{2} = 1230$. This example also explained in the problem statement but for $k = 1$.\n\nFor the second example the answer is just the sum of numbers from $l$ to $r$ which equals to $\\frac{2345 \\cdot 2346}{2} = 2750685$.\n\nFor the third example the answer is $101 + 110 + 111 + 112 + 113 + 114 + 115 + 116 + 117 + 118 + 119 + 121 + 122 + 131 + 133 + 141 + 144 + 151 = 2189$."} +{"id": "00268", "text": "Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a_1, a_2, ..., a_{n} of n integer numbers \u2014 saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:\n\n Each pencil belongs to exactly one box; Each non-empty box has at least k pencils in it; If pencils i and j belong to the same box, then |a_{i} - a_{j}| \u2264 d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |a_{i} - a_{j}| \u2264 d and they belong to different boxes. \n\nHelp Mishka to determine if it's possible to distribute all the pencils into boxes. Print \"YES\" if there exists such a distribution. Otherwise print \"NO\".\n\n\n-----Input-----\n\nThe first line contains three integer numbers n, k and d (1 \u2264 k \u2264 n \u2264 5\u00b710^5, 0 \u2264 d \u2264 10^9) \u2014 the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.\n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 saturation of color of each pencil.\n\n\n-----Output-----\n\nPrint \"YES\" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print \"NO\".\n\n\n-----Examples-----\nInput\n6 3 10\n7 2 7 7 4 2\n\nOutput\nYES\n\nInput\n6 2 3\n4 5 3 13 4 10\n\nOutput\nYES\n\nInput\n3 2 5\n10 16 22\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.\n\nIn the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box."} +{"id": "00269", "text": "Nothing is eternal in the world, Kostya understood it on the 7-th of January when he saw partially dead four-color garland.\n\nNow he has a goal to replace dead light bulbs, however he doesn't know how many light bulbs for each color are required. It is guaranteed that for each of four colors at least one light is working.\n\nIt is known that the garland contains light bulbs of four colors: red, blue, yellow and green. The garland is made as follows: if you take any four consecutive light bulbs then there will not be light bulbs with the same color among them. For example, the garland can look like \"RYBGRYBGRY\", \"YBGRYBGRYBG\", \"BGRYB\", but can not look like \"BGRYG\", \"YBGRYBYGR\" or \"BGYBGY\". Letters denote colors: 'R'\u00a0\u2014 red, 'B'\u00a0\u2014 blue, 'Y'\u00a0\u2014 yellow, 'G'\u00a0\u2014 green.\n\nUsing the information that for each color at least one light bulb still works count the number of dead light bulbs of each four colors.\n\n\n-----Input-----\n\nThe first and the only line contains the string s (4 \u2264 |s| \u2264 100), which describes the garland, the i-th symbol of which describes the color of the i-th light bulb in the order from the beginning of garland: 'R'\u00a0\u2014 the light bulb is red, 'B'\u00a0\u2014 the light bulb is blue, 'Y'\u00a0\u2014 the light bulb is yellow, 'G'\u00a0\u2014 the light bulb is green, '!'\u00a0\u2014 the light bulb is dead. \n\nThe string s can not contain other symbols except those five which were described. \n\nIt is guaranteed that in the given string at least once there is each of four letters 'R', 'B', 'Y' and 'G'. \n\nIt is guaranteed that the string s is correct garland with some blown light bulbs, it means that for example the line \"GRBY!!!B\" can not be in the input data. \n\n\n-----Output-----\n\nIn the only line print four integers k_{r}, k_{b}, k_{y}, k_{g}\u00a0\u2014 the number of dead light bulbs of red, blue, yellow and green colors accordingly.\n\n\n-----Examples-----\nInput\nRYBGRYBGR\n\nOutput\n0 0 0 0\nInput\n!RGYB\n\nOutput\n0 1 0 0\nInput\n!!!!YGRB\n\nOutput\n1 1 1 1\nInput\n!GB!RG!Y!\n\nOutput\n2 1 1 0\n\n\n-----Note-----\n\nIn the first example there are no dead light bulbs.\n\nIn the second example it is obvious that one blue bulb is blown, because it could not be light bulbs of other colors on its place according to the statements."} +{"id": "00270", "text": "There is a cave consisting of N rooms and M one-directional passages. The rooms are numbered 1 through N.\nTakahashi is now in Room 1, and Room N has the exit. The i-th passage connects Room s_i and Room t_i (s_i < t_i) and can only be traversed in the direction from Room s_i to Room t_i. It is known that, for each room except Room N, there is at least one passage going from that room.\nTakahashi will escape from the cave. Each time he reaches a room (assume that he has reached Room 1 at the beginning), he will choose a passage uniformly at random from the ones going from that room and take that passage.\nAoki, a friend of Takahashi's, can block one of the passages (or do nothing) before Takahashi leaves Room 1. However, it is not allowed to block a passage so that Takahashi is potentially unable to reach Room N.\nLet E be the expected number of passages Takahashi takes before he reaches Room N. Find the value of E when Aoki makes a choice that minimizes E.\n\n-----Constraints-----\n - 2 \\leq N \\leq 600\n - N-1 \\leq M \\leq \\frac{N(N-1)}{2}\n - s_i < t_i\n - If i != j, (s_i, t_i) \\neq (s_j, t_j). (Added 21:23 JST)\n - For every v = 1, 2, ..., N-1, there exists i such that v = s_i.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\ns_1 t_1\n:\ns_M t_M\n\n-----Output-----\nPrint the value of E when Aoki makes a choice that minimizes E.\nYour output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6}.\n\n-----Sample Input-----\n4 6\n1 4\n2 3\n1 3\n1 2\n3 4\n2 4\n\n-----Sample Output-----\n1.5000000000\n\nIf Aoki blocks the passage from Room 1 to Room 2, Takahashi will go along the path 1 \u2192 3 \u2192 4 with probability \\frac{1}{2} and 1 \u2192 4 with probability \\frac{1}{2}. E = 1.5 here, and this is the minimum possible value of E."} +{"id": "00271", "text": "Vasya has a non-negative integer n. He wants to round it to nearest integer, which ends up with 0. If n already ends up with 0, Vasya considers it already rounded.\n\nFor example, if n = 4722 answer is 4720. If n = 5 Vasya can round it to 0 or to 10. Both ways are correct.\n\nFor given n find out to which integer will Vasya round it.\n\n\n-----Input-----\n\nThe first line contains single integer n (0 \u2264 n \u2264 10^9)\u00a0\u2014 number that Vasya has.\n\n\n-----Output-----\n\nPrint result of rounding n. Pay attention that in some cases answer isn't unique. In that case print any correct answer.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n0\n\nInput\n113\n\nOutput\n110\n\nInput\n1000000000\n\nOutput\n1000000000\n\nInput\n5432359\n\nOutput\n5432360\n\n\n\n-----Note-----\n\nIn the first example n = 5. Nearest integers, that ends up with zero are 0 and 10. Any of these answers is correct, so you can print 0 or 10."} +{"id": "00272", "text": "Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. \n\nIn order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard.\n\nYou are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once.\n\n\n-----Input-----\n\nThe input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters.\n\n\n-----Output-----\n\nIf Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print \u00ab-1\u00bb (without quotes).\n\nOtherwise, the first line of output should contain the only integer k (k \u2265 0)\u00a0\u2014 the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct.\n\nIf there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair.\n\nEach letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes.\n\n\n-----Examples-----\nInput\nhelloworld\nehoolwlroz\n\nOutput\n3\nh e\nl o\nd z\n\nInput\nhastalavistababy\nhastalavistababy\n\nOutput\n0\n\nInput\nmerrychristmas\nchristmasmerry\n\nOutput\n-1"} +{"id": "00273", "text": "The preferred way to generate user login in Polygon is to concatenate a prefix of the user's first name and a prefix of their last name, in that order. Each prefix must be non-empty, and any of the prefixes can be the full name. Typically there are multiple possible logins for each person.\n\nYou are given the first and the last name of a user. Return the alphabetically earliest login they can get (regardless of other potential Polygon users).\n\nAs a reminder, a prefix of a string s is its substring which occurs at the beginning of s: \"a\", \"ab\", \"abc\" etc. are prefixes of string \"{abcdef}\" but \"b\" and 'bc\" are not. A string a is alphabetically earlier than a string b, if a is a prefix of b, or a and b coincide up to some position, and then a has a letter that is alphabetically earlier than the corresponding letter in b: \"a\" and \"ab\" are alphabetically earlier than \"ac\" but \"b\" and \"ba\" are alphabetically later than \"ac\".\n\n\n-----Input-----\n\nThe input consists of a single line containing two space-separated strings: the first and the last names. Each character of each string is a lowercase English letter. The length of each string is between 1 and 10, inclusive. \n\n\n-----Output-----\n\nOutput a single string\u00a0\u2014 alphabetically earliest possible login formed from these names. The output should be given in lowercase as well.\n\n\n-----Examples-----\nInput\nharry potter\n\nOutput\nhap\n\nInput\ntom riddle\n\nOutput\ntomr"} +{"id": "00274", "text": "A sequence of square brackets is regular if by inserting symbols \"+\" and \"1\" into it, you can get a regular mathematical expression from it. For example, sequences \"[[]][]\", \"[]\" and \"[[][[]]]\" \u2014 are regular, at the same time \"][\", \"[[]\" and \"[[]]][\" \u2014 are irregular. \n\nDraw the given sequence using a minimalistic pseudographics in the strip of the lowest possible height \u2014 use symbols '+', '-' and '|'. For example, the sequence \"[[][]][]\" should be represented as: \n\n+- -++- -+ \n\n|+- -++- -+|| |\n\n|| || ||| |\n\n|+- -++- -+|| |\n\n+- -++- -+\n\n\n\nEach bracket should be represented with the hepl of one or more symbols '|' (the vertical part) and symbols '+' and '-' as on the example which is given above.\n\nBrackets should be drawn without spaces one by one, only dividing pairs of consecutive pairwise brackets with a single-space bar (so that the two brackets do not visually merge into one symbol). The image should have the minimum possible height. \n\nThe enclosed bracket is always smaller than the surrounding bracket, but each bracket separately strives to maximize the height of the image. So the pair of final brackets in the example above occupies the entire height of the image.\n\nStudy carefully the examples below, they adequately explain the condition of the problem. Pay attention that in this problem the answer (the image) is unique. \n\n\n-----Input-----\n\nThe first line contains an even integer n (2 \u2264 n \u2264 100) \u2014 the length of the sequence of brackets.\n\nThe second line contains the sequence of brackets \u2014 these are n symbols \"[\" and \"]\". It is guaranteed that the given sequence of brackets is regular. \n\n\n-----Output-----\n\nPrint the drawn bracket sequence in the format which is given in the condition. Don't print extra (unnecessary) spaces. \n\n\n-----Examples-----\nInput\n8\n[[][]][]\n\nOutput\n+- -++- -+\n|+- -++- -+|| |\n|| || ||| |\n|+- -++- -+|| |\n+- -++- -+\n\nInput\n6\n[[[]]]\n\nOutput\n+- -+\n|+- -+|\n||+- -+||\n||| |||\n||+- -+||\n|+- -+|\n+- -+\n\nInput\n6\n[[][]]\n\nOutput\n+- -+\n|+- -++- -+|\n|| || ||\n|+- -++- -+|\n+- -+\n\nInput\n2\n[]\n\nOutput\n+- -+\n| |\n+- -+\n\nInput\n4\n[][]\n\nOutput\n+- -++- -+\n| || |\n+- -++- -+"} +{"id": "00275", "text": "Piegirl got bored with binary, decimal and other integer based counting systems. Recently she discovered some interesting properties about number $q = \\frac{\\sqrt{5} + 1}{2}$, in particular that q^2 = q + 1, and she thinks it would make a good base for her new unique system. She called it \"golden system\". In golden system the number is a non-empty string containing 0's and 1's as digits. The decimal value of expression a_0a_1...a_{n} equals to $\\sum_{i = 0}^{n} a_{i} \\cdot q^{n - i}$.\n\nSoon Piegirl found out that this system doesn't have same properties that integer base systems do and some operations can not be performed on it. She wasn't able to come up with a fast way of comparing two numbers. She is asking for your help.\n\nGiven two numbers written in golden system notation, determine which of them has larger decimal value.\n\n\n-----Input-----\n\nInput consists of two lines \u2014 one for each number. Each line contains non-empty string consisting of '0' and '1' characters. The length of each string does not exceed 100000.\n\n\n-----Output-----\n\nPrint \">\" if the first number is larger, \"<\" if it is smaller and \"=\" if they are equal.\n\n\n-----Examples-----\nInput\n1000\n111\n\nOutput\n<\n\nInput\n00100\n11\n\nOutput\n=\n\nInput\n110\n101\n\nOutput\n>\n\n\n\n-----Note-----\n\nIn the first example first number equals to $((\\sqrt{5} + 1) / 2)^{3} \\approx 1.618033988^{3} \\approx 4.236$, while second number is approximately 1.618033988^2 + 1.618033988 + 1 \u2248 5.236, which is clearly a bigger number.\n\nIn the second example numbers are equal. Each of them is \u2248 2.618."} +{"id": "00276", "text": "You took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems: the Power Gem of purple color, the Time Gem of green color, the Space Gem of blue color, the Soul Gem of orange color, the Reality Gem of red color, the Mind Gem of yellow color. \n\nUsing colors of Gems you saw in the Gauntlet determine the names of absent Gems.\n\n\n-----Input-----\n\nIn the first line of input there is one integer $n$ ($0 \\le n \\le 6$)\u00a0\u2014 the number of Gems in Infinity Gauntlet.\n\nIn next $n$ lines there are colors of Gems you saw. Words used for colors are: purple, green, blue, orange, red, yellow. It is guaranteed that all the colors are distinct. All colors are given in lowercase English letters.\n\n\n-----Output-----\n\nIn the first line output one integer $m$ ($0 \\le m \\le 6$)\u00a0\u2014 the number of absent Gems.\n\nThen in $m$ lines print the names of absent Gems, each on its own line. Words used for names are: Power, Time, Space, Soul, Reality, Mind. Names can be printed in any order. Keep the first letter uppercase, others lowercase.\n\n\n-----Examples-----\nInput\n4\nred\npurple\nyellow\norange\n\nOutput\n2\nSpace\nTime\n\nInput\n0\n\nOutput\n6\nTime\nMind\nSoul\nPower\nReality\nSpace\n\n\n\n-----Note-----\n\nIn the first sample Thanos already has Reality, Power, Mind and Soul Gems, so he needs two more: Time and Space.\n\nIn the second sample Thanos doesn't have any Gems, so he needs all six."} +{"id": "00277", "text": "The last stage of Football World Cup is played using the play-off system.\n\nThere are n teams left in this stage, they are enumerated from 1 to n. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the third\u00a0\u2014 with the fourth, the fifth\u00a0\u2014 with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over.\n\nArkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids a and b can meet.\n\n\n-----Input-----\n\nThe only line contains three integers n, a and b (2 \u2264 n \u2264 256, 1 \u2264 a, b \u2264 n)\u00a0\u2014 the total number of teams, and the ids of the teams that Arkady is interested in. \n\nIt is guaranteed that n is such that in each round an even number of team advance, and that a and b are not equal.\n\n\n-----Output-----\n\nIn the only line print \"Final!\" (without quotes), if teams a and b can meet in the Final.\n\nOtherwise, print a single integer\u00a0\u2014 the number of the round in which teams a and b can meet. The round are enumerated from 1.\n\n\n-----Examples-----\nInput\n4 1 2\n\nOutput\n1\n\nInput\n8 2 6\n\nOutput\nFinal!\n\nInput\n8 7 5\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example teams 1 and 2 meet in the first round.\n\nIn the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds.\n\nIn the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round."} +{"id": "00278", "text": "Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction.\n\nPavel has a plan: a permutation p and a sequence b_1, b_2, ..., b_{n}, consisting of zeros and ones. Each second Pavel move skewer on position i to position p_{i}, and if b_{i} equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions.\n\nUnfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well.\n\nThere is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k \u2265 2n), so that after k seconds each skewer visits each of the 2n placements.\n\nIt can be shown that some suitable pair of permutation p and sequence b exists for any n.\n\n\n-----Input-----\n\nThe first line contain the integer n (1 \u2264 n \u2264 2\u00b710^5)\u00a0\u2014 the number of skewers.\n\nThe second line contains a sequence of integers p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 n)\u00a0\u2014 the permutation, according to which Pavel wants to move the skewers.\n\nThe third line contains a sequence b_1, b_2, ..., b_{n} consisting of zeros and ones, according to which Pavel wants to reverse the skewers.\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements.\n\n\n-----Examples-----\nInput\n4\n4 3 2 1\n0 1 1 1\n\nOutput\n2\n\nInput\n3\n2 3 1\n0 0 0\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example Pavel can change the permutation to 4, 3, 1, 2.\n\nIn the second example Pavel can change any element of b to 1."} +{"id": "00279", "text": "The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v_1 meters per second, and in the end it is v_2 meters per second. We know that this section of the route took exactly t seconds to pass.\n\nAssuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.\n\n\n-----Input-----\n\nThe first line contains two integers v_1 and v_2 (1 \u2264 v_1, v_2 \u2264 100) \u2014 the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.\n\nThe second line contains two integers t (2 \u2264 t \u2264 100) \u2014 the time when the car moves along the segment in seconds, d (0 \u2264 d \u2264 10) \u2014 the maximum value of the speed change between adjacent seconds.\n\nIt is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v_1, the speed in the last second equals v_2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d. \n\n\n-----Output-----\n\nPrint the maximum possible length of the path segment in meters. \n\n\n-----Examples-----\nInput\n5 6\n4 2\n\nOutput\n26\nInput\n10 10\n10 0\n\nOutput\n100\n\n\n-----Note-----\n\nIn the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.\n\nIn the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters."} +{"id": "00280", "text": "There are N camels numbered 1 through N.\nThe weight of Camel i is w_i.\nYou will arrange the camels in a line and make them cross a bridge consisting of M parts.\nBefore they cross the bridge, you can choose their order in the line - it does not have to be Camel 1, 2, \\ldots, N from front to back - and specify the distance between each adjacent pair of camels to be any non-negative real number.\nThe camels will keep the specified distances between them while crossing the bridge.\nThe i-th part of the bridge has length l_i and weight capacity v_i.\nIf the sum of the weights of camels inside a part (excluding the endpoints) exceeds v_i, the bridge will collapse.\nDetermine whether it is possible to make the camels cross the bridge without it collapsing. If it is possible, find the minimum possible distance between the first and last camels in the line in such a case.\nIt can be proved that the answer is always an integer, so print an integer.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 8\n - 1 \\leq M \\leq 10^5\n - 1 \\leq w_i,l_i,v_i \\leq 10^8\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nw_1 w_2 \\cdots w_N\nl_1 v_1\n\\vdots\nl_M v_M\n\n-----Output-----\nIf the bridge will unavoidably collapse when the camels cross the bridge, print -1.\nOtherwise, print the minimum possible distance between the first and last camels in the line when the camels cross the bridge without it collapsing.\n\n-----Sample Input-----\n3 2\n1 4 2\n10 4\n2 6\n\n-----Sample Output-----\n10\n\n - It is possible to make the camels cross the bridge without it collapsing by, for example, arranging them in the order 1, 3, 2 from front to back, and setting the distances between them to be 0, 10.\n - For Part 1 of the bridge, there are moments when only Camel 1 and 3 are inside the part and moments when only Camel 2 is inside the part. In both cases, the sum of the weights of camels does not exceed 4 - the weight capacity of Part 1 - so there is no collapse.\n - For Part 2 of the bridge, there are moments when only Camel 1 and 3 are inside the part and moments when only Camel 2 is inside the part. In both cases, the sum of the weights of camels does not exceed 6 - the weight capacity of Part 2 - so there is no collapse.\n - Note that the distance between two camels may be 0 and that camels on endpoints of a part are not considered to be inside the part."} +{"id": "00281", "text": "Even if the world is full of counterfeits, I still regard it as wonderful.\n\nPile up herbs and incense, and arise again from the flames and ashes of its predecessor\u00a0\u2014 as is known to many, the phoenix does it like this.\n\nThe phoenix has a rather long lifespan, and reincarnates itself once every a! years. Here a! denotes the factorial of integer a, that is, a! = 1 \u00d7 2 \u00d7 ... \u00d7 a. Specifically, 0! = 1.\n\nKoyomi doesn't care much about this, but before he gets into another mess with oddities, he is interested in the number of times the phoenix will reincarnate in a timespan of b! years, that is, [Image]. Note that when b \u2265 a this value is always integer.\n\nAs the answer can be quite large, it would be enough for Koyomi just to know the last digit of the answer in decimal representation. And you're here to provide Koyomi with this knowledge.\n\n\n-----Input-----\n\nThe first and only line of input contains two space-separated integers a and b (0 \u2264 a \u2264 b \u2264 10^18).\n\n\n-----Output-----\n\nOutput one line containing a single decimal digit\u00a0\u2014 the last digit of the value that interests Koyomi.\n\n\n-----Examples-----\nInput\n2 4\n\nOutput\n2\n\nInput\n0 10\n\nOutput\n0\n\nInput\n107 109\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example, the last digit of $\\frac{4 !}{2 !} = 12$ is 2;\n\nIn the second example, the last digit of $\\frac{10 !}{0 !} = 3628800$ is 0;\n\nIn the third example, the last digit of $\\frac{109 !}{107 !} = 11772$ is 2."} +{"id": "00282", "text": "A frog lives on the axis Ox and needs to reach home which is in the point n. She starts from the point 1. The frog can jump to the right at a distance not more than d. So, after she jumped from the point x she can reach the point x + a, where a is an integer from 1 to d.\n\nFor each point from 1 to n is known if there is a lily flower in it. The frog can jump only in points with a lilies. Guaranteed that there are lilies in the points 1 and n.\n\nDetermine the minimal number of jumps that the frog needs to reach home which is in the point n from the point 1. Consider that initially the frog is in the point 1. If the frog can not reach home, print -1.\n\n\n-----Input-----\n\nThe first line contains two integers n and d (2 \u2264 n \u2264 100, 1 \u2264 d \u2264 n - 1) \u2014 the point, which the frog wants to reach, and the maximal length of the frog jump.\n\nThe second line contains a string s of length n, consisting of zeros and ones. If a character of the string s equals to zero, then in the corresponding point there is no lily flower. In the other case, in the corresponding point there is a lily flower. Guaranteed that the first and the last characters of the string s equal to one.\n\n\n-----Output-----\n\nIf the frog can not reach the home, print -1.\n\nIn the other case, print the minimal number of jumps that the frog needs to reach the home which is in the point n from the point 1.\n\n\n-----Examples-----\nInput\n8 4\n10010101\n\nOutput\n2\n\nInput\n4 2\n1001\n\nOutput\n-1\n\nInput\n8 4\n11100101\n\nOutput\n3\n\nInput\n12 3\n101111100101\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example the from can reach home in two jumps: the first jump from the point 1 to the point 4 (the length of the jump is three), and the second jump from the point 4 to the point 8 (the length of the jump is four).\n\nIn the second example the frog can not reach home, because to make it she need to jump on a distance three, but the maximum length of her jump equals to two."} +{"id": "00283", "text": "PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: \"There exists such a positive integer n that for each positive integer m number n\u00b7m + 1 is a prime number\".\n\nUnfortunately, PolandBall is not experienced yet and doesn't know that his hypothesis is incorrect. Could you prove it wrong? Write a program that finds a counterexample for any n.\n\n\n-----Input-----\n\nThe only number in the input is n (1 \u2264 n \u2264 1000)\u00a0\u2014 number from the PolandBall's hypothesis. \n\n\n-----Output-----\n\nOutput such m that n\u00b7m + 1 is not a prime number. Your answer will be considered correct if you output any suitable m such that 1 \u2264 m \u2264 10^3. It is guaranteed the the answer exists.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n1\nInput\n4\n\nOutput\n2\n\n\n-----Note-----\n\nA prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.\n\nFor the first sample testcase, 3\u00b71 + 1 = 4. We can output 1.\n\nIn the second sample testcase, 4\u00b71 + 1 = 5. We cannot output 1 because 5 is prime. However, m = 2 is okay since 4\u00b72 + 1 = 9, which is not a prime number."} +{"id": "00284", "text": "Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.\n\nKolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).\n\nKolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a \u00d7 1 234 567 + b \u00d7 123 456 + c \u00d7 1 234 = n?\n\nPlease help Kolya answer this question.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 10^9)\u00a0\u2014 Kolya's initial game-coin score.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n1359257\n\nOutput\nYES\nInput\n17851817\n\nOutput\nNO\n\n\n-----Note-----\n\nIn the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total."} +{"id": "00285", "text": "The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = k_{i}\u00b7x + b_{i}. It was necessary to determine whether there is at least one point of intersection of two of these lines, that lays strictly inside the strip between x_1 < x_2. In other words, is it true that there are 1 \u2264 i < j \u2264 n and x', y', such that: y' = k_{i} * x' + b_{i}, that is, point (x', y') belongs to the line number i; y' = k_{j} * x' + b_{j}, that is, point (x', y') belongs to the line number j; x_1 < x' < x_2, that is, point (x', y') lies inside the strip bounded by x_1 < x_2. \n\nYou can't leave Anton in trouble, can you? Write a program that solves the given task.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (2 \u2264 n \u2264 100 000)\u00a0\u2014 the number of lines in the task given to Anton. The second line contains integers x_1 and x_2 ( - 1 000 000 \u2264 x_1 < x_2 \u2264 1 000 000) defining the strip inside which you need to find a point of intersection of at least two lines.\n\nThe following n lines contain integers k_{i}, b_{i} ( - 1 000 000 \u2264 k_{i}, b_{i} \u2264 1 000 000)\u00a0\u2014 the descriptions of the lines. It is guaranteed that all lines are pairwise distinct, that is, for any two i \u2260 j it is true that either k_{i} \u2260 k_{j}, or b_{i} \u2260 b_{j}.\n\n\n-----Output-----\n\nPrint \"Yes\" (without quotes), if there is at least one intersection of two distinct lines, located strictly inside the strip. Otherwise print \"No\" (without quotes).\n\n\n-----Examples-----\nInput\n4\n1 2\n1 2\n1 0\n0 1\n0 2\n\nOutput\nNO\nInput\n2\n1 3\n1 0\n-1 3\n\nOutput\nYES\nInput\n2\n1 3\n1 0\n0 2\n\nOutput\nYES\nInput\n2\n1 3\n1 0\n0 3\n\nOutput\nNO\n\n\n-----Note-----\n\nIn the first sample there are intersections located on the border of the strip, but there are no intersections located strictly inside it. [Image]"} +{"id": "00286", "text": "Let's define a split of $n$ as a nonincreasing sequence of positive integers, the sum of which is $n$. \n\nFor example, the following sequences are splits of $8$: $[4, 4]$, $[3, 3, 2]$, $[2, 2, 1, 1, 1, 1]$, $[5, 2, 1]$.\n\nThe following sequences aren't splits of $8$: $[1, 7]$, $[5, 4]$, $[11, -3]$, $[1, 1, 4, 1, 1]$.\n\nThe weight of a split is the number of elements in the split that are equal to the first element. For example, the weight of the split $[1, 1, 1, 1, 1]$ is $5$, the weight of the split $[5, 5, 3, 3, 3]$ is $2$ and the weight of the split $[9]$ equals $1$.\n\nFor a given $n$, find out the number of different weights of its splits.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\leq n \\leq 10^9$).\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n7\n\nOutput\n4\n\nInput\n8\n\nOutput\n5\n\nInput\n9\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first sample, there are following possible weights of splits of $7$:\n\nWeight 1: [$\\textbf 7$] \n\nWeight 2: [$\\textbf 3$, $\\textbf 3$, 1] \n\nWeight 3: [$\\textbf 2$, $\\textbf 2$, $\\textbf 2$, 1] \n\nWeight 7: [$\\textbf 1$, $\\textbf 1$, $\\textbf 1$, $\\textbf 1$, $\\textbf 1$, $\\textbf 1$, $\\textbf 1$]"} +{"id": "00287", "text": "Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartments that are numbered from 1 to n and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale.\n\nMaxim often visits his neighbors, so apartment is good for him if it is available for sale and there is at least one already inhabited apartment adjacent to it. Maxim knows that there are exactly k already inhabited apartments, but he doesn't know their indices yet.\n\nFind out what could be the minimum possible and the maximum possible number of apartments that are good for Maxim.\n\n\n-----Input-----\n\nThe only line of the input contains two integers: n and k (1 \u2264 n \u2264 10^9, 0 \u2264 k \u2264 n).\n\n\n-----Output-----\n\nPrint the minimum possible and the maximum possible number of apartments good for Maxim.\n\n\n-----Example-----\nInput\n6 3\n\nOutput\n1 3\n\n\n\n-----Note-----\n\nIn the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 1, 2 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 1, 3 and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good."} +{"id": "00288", "text": "Famous Brazil city Rio de Janeiro holds a tennis tournament and Ostap Bender doesn't want to miss this event. There will be n players participating, and the tournament will follow knockout rules from the very first game. That means, that if someone loses a game he leaves the tournament immediately.\n\nOrganizers are still arranging tournament grid (i.e. the order games will happen and who is going to play with whom) but they have already fixed one rule: two players can play against each other only if the number of games one of them has already played differs by no more than one from the number of games the other one has already played. Of course, both players had to win all their games in order to continue participating in the tournament.\n\nTournament hasn't started yet so the audience is a bit bored. Ostap decided to find out what is the maximum number of games the winner of the tournament can take part in (assuming the rule above is used). However, it is unlikely he can deal with this problem without your help.\n\n\n-----Input-----\n\nThe only line of the input contains a single integer n (2 \u2264 n \u2264 10^18)\u00a0\u2014 the number of players to participate in the tournament.\n\n\n-----Output-----\n\nPrint the maximum number of games in which the winner of the tournament can take part.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1\n\nInput\n3\n\nOutput\n2\n\nInput\n4\n\nOutput\n2\n\nInput\n10\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn all samples we consider that player number 1 is the winner.\n\nIn the first sample, there would be only one game so the answer is 1.\n\nIn the second sample, player 1 can consequently beat players 2 and 3. \n\nIn the third sample, player 1 can't play with each other player as after he plays with players 2 and 3 he can't play against player 4, as he has 0 games played, while player 1 already played 2. Thus, the answer is 2 and to achieve we make pairs (1, 2) and (3, 4) and then clash the winners."} +{"id": "00289", "text": "Tonio has a keyboard with only two letters, \"V\" and \"K\".\n\nOne day, he has typed out a string s with only these two letters. He really likes it when the string \"VK\" appears, so he wishes to change at most one letter in the string (or do no changes) to maximize the number of occurrences of that string. Compute the maximum number of times \"VK\" can appear as a substring (i.\u00a0e. a letter \"K\" right after a letter \"V\") in the resulting string.\n\n\n-----Input-----\n\nThe first line will contain a string s consisting only of uppercase English letters \"V\" and \"K\" with length not less than 1 and not greater than 100.\n\n\n-----Output-----\n\nOutput a single integer, the maximum number of times \"VK\" can appear as a substring of the given string after changing at most one character.\n\n\n-----Examples-----\nInput\nVK\n\nOutput\n1\n\nInput\nVV\n\nOutput\n1\n\nInput\nV\n\nOutput\n0\n\nInput\nVKKKKKKKKKVVVVVVVVVK\n\nOutput\n3\n\nInput\nKVKV\n\nOutput\n1\n\n\n\n-----Note-----\n\nFor the first case, we do not change any letters. \"VK\" appears once, which is the maximum number of times it could appear.\n\nFor the second case, we can change the second character from a \"V\" to a \"K\". This will give us the string \"VK\". This has one occurrence of the string \"VK\" as a substring.\n\nFor the fourth case, we can change the fourth character from a \"K\" to a \"V\". This will give us the string \"VKKVKKKKKKVVVVVVVVVK\". This has three occurrences of the string \"VK\" as a substring. We can check no other moves can give us strictly more occurrences."} +{"id": "00290", "text": "Little Sofia is in fourth grade. Today in the geometry lesson she learned about segments and squares. On the way home, she decided to draw $n$ squares in the snow with a side length of $1$. For simplicity, we assume that Sofia lives on a plane and can draw only segments of length $1$, parallel to the coordinate axes, with vertices at integer points.\n\nIn order to draw a segment, Sofia proceeds as follows. If she wants to draw a vertical segment with the coordinates of the ends $(x, y)$ and $(x, y+1)$. Then Sofia looks if there is already a drawn segment with the coordinates of the ends $(x', y)$ and $(x', y+1)$ for some $x'$. If such a segment exists, then Sofia quickly draws a new segment, using the old one as a guideline. If there is no such segment, then Sofia has to take a ruler and measure a new segment for a long time. Same thing happens when Sofia wants to draw a horizontal segment, but only now she checks for the existence of a segment with the same coordinates $x$, $x+1$ and the differing coordinate $y$.\n\nFor example, if Sofia needs to draw one square, she will have to draw two segments using a ruler: [Image] \n\nAfter that, she can draw the remaining two segments, using the first two as a guide: [Image] \n\nIf Sofia needs to draw two squares, she will have to draw three segments using a ruler: [Image] \n\nAfter that, she can draw the remaining four segments, using the first three as a guide: [Image] \n\nSofia is in a hurry, so she wants to minimize the number of segments that she will have to draw with a ruler without a guide. Help her find this minimum number.\n\n\n-----Input-----\n\nThe only line of input contains a single integer $n$ ($1 \\le n \\le 10^{9}$), the number of squares that Sofia wants to draw.\n\n\n-----Output-----\n\nPrint single integer, the minimum number of segments that Sofia will have to draw with a ruler without a guide in order to draw $n$ squares in the manner described above.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n2\n\nInput\n2\n\nOutput\n3\n\nInput\n4\n\nOutput\n4"} +{"id": "00291", "text": "Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob.\n\nRight now, Limak and Bob weigh a and b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight.\n\nLimak eats a lot and his weight is tripled after every year, while Bob's weight is doubled after every year.\n\nAfter how many full years will Limak become strictly larger (strictly heavier) than Bob?\n\n\n-----Input-----\n\nThe only line of the input contains two integers a and b (1 \u2264 a \u2264 b \u2264 10)\u00a0\u2014 the weight of Limak and the weight of Bob respectively.\n\n\n-----Output-----\n\nPrint one integer, denoting the integer number of years after which Limak will become strictly larger than Bob.\n\n\n-----Examples-----\nInput\n4 7\n\nOutput\n2\n\nInput\n4 9\n\nOutput\n3\n\nInput\n1 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample, Limak weighs 4 and Bob weighs 7 initially. After one year their weights are 4\u00b73 = 12 and 7\u00b72 = 14 respectively (one weight is tripled while the other one is doubled). Limak isn't larger than Bob yet. After the second year weights are 36 and 28, so the first weight is greater than the second one. Limak became larger than Bob after two years so you should print 2.\n\nIn the second sample, Limak's and Bob's weights in next years are: 12 and 18, then 36 and 36, and finally 108 and 72 (after three years). The answer is 3. Remember that Limak wants to be larger than Bob and he won't be satisfied with equal weights.\n\nIn the third sample, Limak becomes larger than Bob after the first year. Their weights will be 3 and 2 then."} +{"id": "00292", "text": "Amr bought a new video game \"Guess Your Way Out!\". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node. \n\nLet's index all the leaf nodes from the left to the right from 1 to 2^{h}. The exit is located at some node n where 1 \u2264 n \u2264 2^{h}, the player doesn't know where the exit is so he has to guess his way out!\n\nAmr follows simple algorithm to choose the path. Let's consider infinite command string \"LRLRLRLRL...\" (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules: Character 'L' means \"go to the left child of the current node\"; Character 'R' means \"go to the right child of the current node\"; If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node; If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command; If he reached a leaf node that is not the exit, he returns to the parent of the current node; If he reaches an exit, the game is finished. \n\nNow Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit?\n\n\n-----Input-----\n\nInput consists of two integers h, n (1 \u2264 h \u2264 50, 1 \u2264 n \u2264 2^{h}).\n\n\n-----Output-----\n\nOutput a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm.\n\n\n-----Examples-----\nInput\n1 2\n\nOutput\n2\nInput\n2 3\n\nOutput\n5\nInput\n3 6\n\nOutput\n10\nInput\n10 1024\n\nOutput\n2046\n\n\n-----Note-----\n\nA perfect binary tree of height h is a binary tree consisting of h + 1 levels. Level 0 consists of a single node called root, level h consists of 2^{h} nodes called leaves. Each node that is not a leaf has exactly two children, left and right one. \n\nFollowing picture illustrates the sample test number 3. Nodes are labeled according to the order of visit.\n\n[Image]"} +{"id": "00293", "text": "Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m columns. For example, in a 3 \u00d7 5 table there are 15 squares with side one, 8 squares with side two and 3 squares with side three. The total number of distinct squares in a 3 \u00d7 5 table is 15 + 8 + 3 = 26.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer x (1 \u2264 x \u2264 10^18)\u00a0\u2014 the number of squares inside the tables Spongebob is interested in.\n\n\n-----Output-----\n\nFirst print a single integer k\u00a0\u2014 the number of tables with exactly x distinct squares inside.\n\nThen print k pairs of integers describing the tables. Print the pairs in the order of increasing n, and in case of equality\u00a0\u2014 in the order of increasing m.\n\n\n-----Examples-----\nInput\n26\n\nOutput\n6\n1 26\n2 9\n3 5\n5 3\n9 2\n26 1\n\nInput\n2\n\nOutput\n2\n1 2\n2 1\n\nInput\n8\n\nOutput\n4\n1 8\n2 3\n3 2\n8 1\n\n\n\n-----Note-----\n\nIn a 1 \u00d7 2 table there are 2 1 \u00d7 1 squares. So, 2 distinct squares in total. [Image] \n\nIn a 2 \u00d7 3 table there are 6 1 \u00d7 1 squares and 2 2 \u00d7 2 squares. That is equal to 8 squares in total. [Image]"} +{"id": "00294", "text": "Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.\n\nA year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.\n\nIn this problem you are given n (1 \u2264 n \u2264 24) integers a_1, a_2, ..., a_{n}, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a_1 days, duration of the next month is a_2 days, and so on.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 24) \u2014 the number of integers.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (28 \u2264 a_{i} \u2264 31) \u2014 the numbers you are to check.\n\n\n-----Output-----\n\nIf there are several consecutive months that fit the sequence, print \"YES\" (without quotes). Otherwise, print \"NO\" (without quotes).\n\nYou can print each letter in arbitrary case (small or large).\n\n\n-----Examples-----\nInput\n4\n31 31 30 31\n\nOutput\nYes\n\n\nInput\n2\n30 30\n\nOutput\nNo\n\n\nInput\n5\n29 31 30 31 30\n\nOutput\nYes\n\n\nInput\n3\n31 28 30\n\nOutput\nNo\n\n\nInput\n3\n31 31 28\n\nOutput\nYes\n\n\n\n\n-----Note-----\n\nIn the first example the integers can denote months July, August, September and October.\n\nIn the second example the answer is no, because there are no two consecutive months each having 30 days.\n\nIn the third example the months are: February (leap year) \u2014 March \u2014 April \u2013 May \u2014 June.\n\nIn the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.\n\nIn the fifth example the months are: December \u2014 January \u2014 February (non-leap year)."} +{"id": "00295", "text": "You are given a positive integer $n$.\n\nFind a sequence of fractions $\\frac{a_i}{b_i}$, $i = 1 \\ldots k$ (where $a_i$ and $b_i$ are positive integers) for some $k$ such that:\n\n$$ \\begin{cases} \\text{$b_i$ divides $n$, $1 < b_i < n$ for $i = 1 \\ldots k$} \\\\ \\text{$1 \\le a_i < b_i$ for $i = 1 \\ldots k$} \\\\ \\text{$\\sum\\limits_{i=1}^k \\frac{a_i}{b_i} = 1 - \\frac{1}{n}$} \\end{cases} $$\n\n\n-----Input-----\n\nThe input consists of a single integer $n$ ($2 \\le n \\le 10^9$).\n\n\n-----Output-----\n\nIn the first line print \"YES\" if there exists such a sequence of fractions or \"NO\" otherwise.\n\nIf there exists such a sequence, next lines should contain a description of the sequence in the following format.\n\nThe second line should contain integer $k$ ($1 \\le k \\le 100\\,000$)\u00a0\u2014 the number of elements in the sequence. It is guaranteed that if such a sequence exists, then there exists a sequence of length at most $100\\,000$.\n\nNext $k$ lines should contain fractions of the sequence with two integers $a_i$ and $b_i$ on each line.\n\n\n-----Examples-----\nInput\n2\n\nOutput\nNO\n\nInput\n6\n\nOutput\nYES\n2\n1 2\n1 3\n\n\n\n-----Note-----\n\nIn the second example there is a sequence $\\frac{1}{2}, \\frac{1}{3}$ such that $\\frac{1}{2} + \\frac{1}{3} = 1 - \\frac{1}{6}$."} +{"id": "00296", "text": "Vasya should paint a fence in front of his own cottage. The fence is a sequence of n wooden boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number the board fence using numbers 1, 2, ..., n from left to right. The height of the i-th board is h_{i} centimeters.\n\nVasya has a 1 centimeter wide brush and the paint of two colors, red and green. Of course, the amount of the paint is limited. Vasya counted the area he can paint each of the colors. It turned out that he can not paint over a square centimeters of the fence red, and he can not paint over b square centimeters green. Each board of the fence should be painted exactly one of the two colors. Perhaps Vasya won't need one of the colors.\n\nIn addition, Vasya wants his fence to look smart. To do this, he should paint the fence so as to minimize the value that Vasya called the fence unattractiveness value. Vasya believes that two consecutive fence boards, painted different colors, look unattractive. The unattractiveness value of a fence is the total length of contact between the neighboring boards of various colors. To make the fence look nice, you need to minimize the value as low as possible. Your task is to find what is the minimum unattractiveness Vasya can get, if he paints his fence completely. $1$ \n\nThe picture shows the fence, where the heights of boards (from left to right) are 2,3,2,4,3,1. The first and the fifth boards are painted red, the others are painted green. The first and the second boards have contact length 2, the fourth and fifth boards have contact length 3, the fifth and the sixth have contact length 1. Therefore, the unattractiveness of the given painted fence is 2+3+1=6.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 200) \u2014 the number of boards in Vasya's fence.\n\nThe second line contains two integers a and b (0 \u2264 a, b \u2264 4\u00b710^4) \u2014 the area that can be painted red and the area that can be painted green, correspondingly.\n\nThe third line contains a sequence of n integers h_1, h_2, ..., h_{n} (1 \u2264 h_{i} \u2264 200) \u2014 the heights of the fence boards.\n\nAll numbers in the lines are separated by single spaces.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum unattractiveness value Vasya can get if he paints his fence completely. If it is impossible to do, print - 1.\n\n\n-----Examples-----\nInput\n4\n5 7\n3 3 4 1\n\nOutput\n3\n\nInput\n3\n2 3\n1 3 1\n\nOutput\n2\n\nInput\n3\n3 3\n2 2 2\n\nOutput\n-1"} +{"id": "00297", "text": "Vasya has got three integers $n$, $m$ and $k$. He'd like to find three integer points $(x_1, y_1)$, $(x_2, y_2)$, $(x_3, y_3)$, such that $0 \\le x_1, x_2, x_3 \\le n$, $0 \\le y_1, y_2, y_3 \\le m$ and the area of the triangle formed by these points is equal to $\\frac{nm}{k}$.\n\nHelp Vasya! Find such points (if it's possible). If there are multiple solutions, print any of them.\n\n\n-----Input-----\n\nThe single line contains three integers $n$, $m$, $k$ ($1\\le n, m \\le 10^9$, $2 \\le k \\le 10^9$).\n\n\n-----Output-----\n\nIf there are no such points, print \"NO\".\n\nOtherwise print \"YES\" in the first line. The next three lines should contain integers $x_i, y_i$ \u2014 coordinates of the points, one point per line. If there are multiple solutions, print any of them.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n4 3 3\n\nOutput\nYES\n1 0\n2 3\n4 1\n\nInput\n4 4 7\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example area of the triangle should be equal to $\\frac{nm}{k} = 4$. The triangle mentioned in the output is pictured below: [Image] \n\nIn the second example there is no triangle with area $\\frac{nm}{k} = \\frac{16}{7}$."} +{"id": "00298", "text": "It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends.\n\nToday he invented one simple game to play with Lena, with whom he shares a desk. The rules are simple. Sasha draws n sticks in a row. After that the players take turns crossing out exactly k sticks from left or right in each turn. Sasha moves first, because he is the inventor of the game. If there are less than k sticks on the paper before some turn, the game ends. Sasha wins if he makes strictly more moves than Lena. Sasha wants to know the result of the game before playing, you are to help him.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n, k \u2264 10^18, k \u2264 n)\u00a0\u2014 the number of sticks drawn by Sasha and the number k\u00a0\u2014 the number of sticks to be crossed out on each turn.\n\n\n-----Output-----\n\nIf Sasha wins, print \"YES\" (without quotes), otherwise print \"NO\" (without quotes).\n\nYou can print each letter in arbitrary case (upper of lower).\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\nYES\n\nInput\n10 4\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example Sasha crosses out 1 stick, and then there are no sticks. So Lena can't make a move, and Sasha wins.\n\nIn the second example Sasha crosses out 4 sticks, then Lena crosses out 4 sticks, and after that there are only 2 sticks left. Sasha can't make a move. The players make equal number of moves, so Sasha doesn't win."} +{"id": "00299", "text": "Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a_1, a_2, ..., a_{n}. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise a_{i} times.\n\nGreg now only does three types of exercises: \"chest\" exercises, \"biceps\" exercises and \"back\" exercises. Besides, his training is cyclic, that is, the first exercise he does is a \"chest\" one, the second one is \"biceps\", the third one is \"back\", the fourth one is \"chest\", the fifth one is \"biceps\", and so on to the n-th exercise.\n\nNow Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 20). The second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 25) \u2014 the number of times Greg repeats the exercises.\n\n\n-----Output-----\n\nPrint word \"chest\" (without the quotes), if the chest gets the most exercise, \"biceps\" (without the quotes), if the biceps gets the most exercise and print \"back\" (without the quotes) if the back gets the most exercise.\n\nIt is guaranteed that the input is such that the answer to the problem is unambiguous.\n\n\n-----Examples-----\nInput\n2\n2 8\n\nOutput\nbiceps\n\nInput\n3\n5 1 10\n\nOutput\nback\n\nInput\n7\n3 3 2 7 9 6 8\n\nOutput\nchest\n\n\n\n-----Note-----\n\nIn the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.\n\nIn the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.\n\nIn the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise."} +{"id": "00300", "text": "Translator's note: in Russia's most widespread grading system, there are four grades: 5, 4, 3, 2, the higher the better, roughly corresponding to A, B, C and F respectively in American grading system.\n\nThe term is coming to an end and students start thinking about their grades. Today, a professor told his students that the grades for his course would be given out automatically \u00a0\u2014 he would calculate the simple average (arithmetic mean) of all grades given out for lab works this term and round to the nearest integer. The rounding would be done in favour of the student\u00a0\u2014 $4.5$ would be rounded up to $5$ (as in example 3), but $4.4$ would be rounded down to $4$.\n\nThis does not bode well for Vasya who didn't think those lab works would influence anything, so he may receive a grade worse than $5$ (maybe even the dreaded $2$). However, the professor allowed him to redo some of his works of Vasya's choosing to increase his average grade. Vasya wants to redo as as few lab works as possible in order to get $5$ for the course. Of course, Vasya will get $5$ for the lab works he chooses to redo.\n\nHelp Vasya\u00a0\u2014 calculate the minimum amount of lab works Vasya has to redo.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$\u00a0\u2014 the number of Vasya's grades ($1 \\leq n \\leq 100$).\n\nThe second line contains $n$ integers from $2$ to $5$\u00a0\u2014 Vasya's grades for his lab works.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the minimum amount of lab works that Vasya has to redo. It can be shown that Vasya can always redo enough lab works to get a $5$.\n\n\n-----Examples-----\nInput\n3\n4 4 4\n\nOutput\n2\n\nInput\n4\n5 4 5 5\n\nOutput\n0\n\nInput\n4\n5 3 3 5\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample, it is enough to redo two lab works to make two $4$s into $5$s.\n\nIn the second sample, Vasya's average is already $4.75$ so he doesn't have to redo anything to get a $5$.\n\nIn the second sample Vasya has to redo one lab work to get rid of one of the $3$s, that will make the average exactly $4.5$ so the final grade would be $5$."} +{"id": "00301", "text": "Given 2 integers $u$ and $v$, find the shortest array such that bitwise-xor of its elements is $u$, and the sum of its elements is $v$.\n\n\n-----Input-----\n\nThe only line contains 2 integers $u$ and $v$ $(0 \\le u,v \\le 10^{18})$.\n\n\n-----Output-----\n\nIf there's no array that satisfies the condition, print \"-1\". Otherwise:\n\nThe first line should contain one integer, $n$, representing the length of the desired array. The next line should contain $n$ positive integers, the array itself. If there are multiple possible answers, print any.\n\n\n-----Examples-----\nInput\n2 4\n\nOutput\n2\n3 1\nInput\n1 3\n\nOutput\n3\n1 1 1\nInput\n8 5\n\nOutput\n-1\nInput\n0 0\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first sample, $3\\oplus 1 = 2$ and $3 + 1 = 4$. There is no valid array of smaller length.\n\nNotice that in the fourth sample the array is empty."} +{"id": "00302", "text": "Prof. Vasechkin wants to represent positive integer n as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+\u20131. Help him to find the least number of digits 1 in such sum.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n < 10^15).\n\n\n-----Output-----\n\nPrint expected minimal number of digits 1.\n\n\n-----Examples-----\nInput\n121\n\nOutput\n6"} +{"id": "00303", "text": "Captain Bill the Hummingbird and his crew recieved an interesting challenge offer. Some stranger gave them a map, potion of teleportation and said that only this potion might help them to reach the treasure. \n\nBottle with potion has two values x and y written on it. These values define four moves which can be performed using the potion:\n\n $(a, b) \\rightarrow(a + x, b + y)$ $(a, b) \\rightarrow(a + x, b - y)$ $(a, b) \\rightarrow(a - x, b + y)$ $(a, b) \\rightarrow(a - x, b - y)$ \n\nMap shows that the position of Captain Bill the Hummingbird is (x_1, y_1) and the position of the treasure is (x_2, y_2).\n\nYou task is to tell Captain Bill the Hummingbird whether he should accept this challenge or decline. If it is possible for Captain to reach the treasure using the potion then output \"YES\", otherwise \"NO\" (without quotes).\n\nThe potion can be used infinite amount of times.\n\n\n-----Input-----\n\nThe first line contains four integer numbers x_1, y_1, x_2, y_2 ( - 10^5 \u2264 x_1, y_1, x_2, y_2 \u2264 10^5) \u2014 positions of Captain Bill the Hummingbird and treasure respectively.\n\nThe second line contains two integer numbers x, y (1 \u2264 x, y \u2264 10^5) \u2014 values on the potion bottle.\n\n\n-----Output-----\n\nPrint \"YES\" if it is possible for Captain to reach the treasure using the potion, otherwise print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n0 0 0 6\n2 3\n\nOutput\nYES\n\nInput\n1 1 3 6\n1 5\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example there exists such sequence of moves:\n\n $(0,0) \\rightarrow(2,3)$ \u2014 the first type of move $(2,3) \\rightarrow(0,6)$ \u2014 the third type of move"} +{"id": "00304", "text": "This night wasn't easy on Vasya. His favorite team lost, and he didn't find himself victorious either\u00a0\u2014 although he played perfectly, his teammates let him down every time. He had to win at least one more time, but the losestreak only grew longer and longer... It's no wonder he didn't get any sleep this night at all.\n\nIn the morning, Vasya was waiting the bus to the university on the bus stop. Vasya's thoughts were hazy and so he couldn't remember the right bus' number quite right and got onto the bus with the number $n$.\n\nIn the bus, Vasya thought that he could get the order of the digits in the number of the bus wrong. Futhermore, he could \"see\" some digits several times, but the digits he saw were definitely in the real number of the bus. For example, if Vasya saw the number 2028, it could mean that the real bus number could be 2028, 8022, 2820 or just 820. However, numbers 80, 22208, 52 definitely couldn't be the number of the bus. Also, real bus number couldn't start with the digit 0, this meaning that, for example, number 082 couldn't be the real bus number too.\n\nGiven $n$, determine the total number of possible bus number variants.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\leq n \\leq 10^{18}$)\u00a0\u2014 the number of the bus that was seen by Vasya. It is guaranteed that this number does not start with $0$.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the amount of possible variants of the real bus number.\n\n\n-----Examples-----\nInput\n97\n\nOutput\n2\n\nInput\n2028\n\nOutput\n13\n\n\n\n-----Note-----\n\nIn the first sample, only variants $97$ and $79$ are possible.\n\nIn the second sample, the variants (in the increasing order) are the following: $208$, $280$, $802$, $820$, $2028$, $2082$, $2208$, $2280$, $2802$, $2820$, $8022$, $8202$, $8220$."} +{"id": "00305", "text": "A new delivery of clothing has arrived today to the clothing store. This delivery consists of $a$ ties, $b$ scarves, $c$ vests and $d$ jackets.\n\nThe store does not sell single clothing items \u2014 instead, it sells suits of two types: a suit of the first type consists of one tie and one jacket; a suit of the second type consists of one scarf, one vest and one jacket. \n\nEach suit of the first type costs $e$ coins, and each suit of the second type costs $f$ coins.\n\nCalculate the maximum possible cost of a set of suits that can be composed from the delivered clothing items. Note that one item cannot be used in more than one suit (though some items may be left unused).\n\n\n-----Input-----\n\nThe first line contains one integer $a$ $(1 \\le a \\le 100\\,000)$ \u2014 the number of ties.\n\nThe second line contains one integer $b$ $(1 \\le b \\le 100\\,000)$ \u2014 the number of scarves.\n\nThe third line contains one integer $c$ $(1 \\le c \\le 100\\,000)$ \u2014 the number of vests.\n\nThe fourth line contains one integer $d$ $(1 \\le d \\le 100\\,000)$ \u2014 the number of jackets.\n\nThe fifth line contains one integer $e$ $(1 \\le e \\le 1\\,000)$ \u2014 the cost of one suit of the first type.\n\nThe sixth line contains one integer $f$ $(1 \\le f \\le 1\\,000)$ \u2014 the cost of one suit of the second type.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum total cost of some set of suits that can be composed from the delivered items. \n\n\n-----Examples-----\nInput\n4\n5\n6\n3\n1\n2\n\nOutput\n6\n\nInput\n12\n11\n13\n20\n4\n6\n\nOutput\n102\n\nInput\n17\n14\n5\n21\n15\n17\n\nOutput\n325\n\n\n\n-----Note-----\n\nIt is possible to compose three suits of the second type in the first example, and their total cost will be $6$. Since all jackets will be used, it's impossible to add anything to this set.\n\nThe best course of action in the second example is to compose nine suits of the first type and eleven suits of the second type. The total cost is $9 \\cdot 4 + 11 \\cdot 6 = 102$."} +{"id": "00306", "text": "Given an integer $x$. Your task is to find out how many positive integers $n$ ($1 \\leq n \\leq x$) satisfy $$n \\cdot a^n \\equiv b \\quad (\\textrm{mod}\\;p),$$ where $a, b, p$ are all known constants.\n\n\n-----Input-----\n\nThe only line contains four integers $a,b,p,x$ ($2 \\leq p \\leq 10^6+3$, $1 \\leq a,b < p$, $1 \\leq x \\leq 10^{12}$). It is guaranteed that $p$ is a prime.\n\n\n-----Output-----\n\nPrint a single integer: the number of possible answers $n$.\n\n\n-----Examples-----\nInput\n2 3 5 8\n\nOutput\n2\n\nInput\n4 6 7 13\n\nOutput\n1\n\nInput\n233 233 10007 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample, we can see that $n=2$ and $n=8$ are possible answers."} +{"id": "00307", "text": "Recently Anton found a box with digits in his room. There are k_2 digits 2, k_3 digits 3, k_5 digits 5 and k_6 digits 6.\n\nAnton's favorite integers are 32 and 256. He decided to compose this integers from digits he has. He wants to make the sum of these integers as large as possible. Help him solve this task!\n\nEach digit can be used no more than once, i.e. the composed integers should contain no more than k_2 digits 2, k_3 digits 3 and so on. Of course, unused digits are not counted in the sum.\n\n\n-----Input-----\n\nThe only line of the input contains four integers k_2, k_3, k_5 and k_6\u00a0\u2014 the number of digits 2, 3, 5 and 6 respectively (0 \u2264 k_2, k_3, k_5, k_6 \u2264 5\u00b710^6).\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 maximum possible sum of Anton's favorite integers that can be composed using digits from the box.\n\n\n-----Examples-----\nInput\n5 1 3 4\n\nOutput\n800\n\nInput\n1 1 1 1\n\nOutput\n256\n\n\n\n-----Note-----\n\nIn the first sample, there are five digits 2, one digit 3, three digits 5 and four digits 6. Anton can compose three integers 256 and one integer 32 to achieve the value 256 + 256 + 256 + 32 = 800. Note, that there is one unused integer 2 and one unused integer 6. They are not counted in the answer.\n\nIn the second sample, the optimal answer is to create on integer 256, thus the answer is 256."} +{"id": "00308", "text": "Mr. Bender has a digital table of size n \u00d7 n, each cell can be switched on or off. He wants the field to have at least c switched on squares. When this condition is fulfilled, Mr Bender will be happy.\n\nWe'll consider the table rows numbered from top to bottom from 1 to n, and the columns \u2014 numbered from left to right from 1 to n. Initially there is exactly one switched on cell with coordinates (x, y) (x is the row number, y is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on.\n\nFor a cell with coordinates (x, y) the side-adjacent cells are cells with coordinates (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1).\n\nIn how many seconds will Mr. Bender get happy?\n\n\n-----Input-----\n\nThe first line contains four space-separated integers n, x, y, c (1 \u2264 n, c \u2264 10^9;\u00a01 \u2264 x, y \u2264 n;\u00a0c \u2264 n^2).\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n6 4 3 1\n\nOutput\n0\n\nInput\n9 3 8 10\n\nOutput\n2\n\n\n\n-----Note-----\n\nInitially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. [Image]."} +{"id": "00309", "text": "A little girl loves problems on bitwise operations very much. Here's one of them.\n\nYou are given two integers l and r. Let's consider the values of $a \\oplus b$ for all pairs of integers a and b (l \u2264 a \u2264 b \u2264 r). Your task is to find the maximum value among all considered ones.\n\nExpression $x \\oplus y$ means applying bitwise excluding or operation to integers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is represented as \"^\", in Pascal \u2014 as \u00abxor\u00bb.\n\n\n-----Input-----\n\nThe single line contains space-separated integers l and r (1 \u2264 l \u2264 r \u2264 10^18).\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the maximum value of $a \\oplus b$ for all pairs of integers a, b (l \u2264 a \u2264 b \u2264 r).\n\n\n-----Examples-----\nInput\n1 2\n\nOutput\n3\n\nInput\n8 16\n\nOutput\n31\n\nInput\n1 1\n\nOutput\n0"} +{"id": "00310", "text": "You are given a set of $2n+1$ integer points on a Cartesian plane. Points are numbered from $0$ to $2n$ inclusive. Let $P_i$ be the $i$-th point. The $x$-coordinate of the point $P_i$ equals $i$. The $y$-coordinate of the point $P_i$ equals zero (initially). Thus, initially $P_i=(i,0)$.\n\nThe given points are vertices of a plot of a piecewise function. The $j$-th piece of the function is the segment $P_{j}P_{j + 1}$.\n\nIn one move you can increase the $y$-coordinate of any point with odd $x$-coordinate (i.e. such points are $P_1, P_3, \\dots, P_{2n-1}$) by $1$. Note that the corresponding segments also change.\n\nFor example, the following plot shows a function for $n=3$ (i.e. number of points is $2\\cdot3+1=7$) in which we increased the $y$-coordinate of the point $P_1$ three times and $y$-coordinate of the point $P_5$ one time: [Image] \n\nLet the area of the plot be the area below this plot and above the coordinate axis OX. For example, the area of the plot on the picture above is 4 (the light blue area on the picture above is the area of the plot drawn on it).\n\nLet the height of the plot be the maximum $y$-coordinate among all initial points in the plot (i.e. points $P_0, P_1, \\dots, P_{2n}$). The height of the plot on the picture above is 3.\n\nYour problem is to say which minimum possible height can have the plot consisting of $2n+1$ vertices and having an area equal to $k$. Note that it is unnecessary to minimize the number of moves.\n\nIt is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding $10^{18}$.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n, k \\le 10^{18}$) \u2014 the number of vertices in a plot of a piecewise function and the area we need to obtain.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible height of a plot consisting of $2n+1$ vertices and with an area equals $k$. It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding $10^{18}$.\n\n\n-----Examples-----\nInput\n4 3\n\nOutput\n1\n\nInput\n4 12\n\nOutput\n3\n\nInput\n999999999999999999 999999999999999986\n\nOutput\n1\n\n\n\n-----Note-----\n\nOne of the possible answers to the first example: [Image] \n\nThe area of this plot is 3, the height of this plot is 1.\n\nThere is only one possible answer to the second example: $M M$ \n\nThe area of this plot is 12, the height of this plot is 3."} +{"id": "00311", "text": "Masha lives in a multi-storey building, where floors are numbered with positive integers. Two floors are called adjacent if their numbers differ by one. Masha decided to visit Egor. Masha lives on the floor $x$, Egor on the floor $y$ (not on the same floor with Masha).\n\nThe house has a staircase and an elevator. If Masha uses the stairs, it takes $t_1$ seconds for her to walk between adjacent floors (in each direction). The elevator passes between adjacent floors (in each way) in $t_2$ seconds. The elevator moves with doors closed. The elevator spends $t_3$ seconds to open or close the doors. We can assume that time is not spent on any action except moving between adjacent floors and waiting for the doors to open or close. If Masha uses the elevator, it immediately goes directly to the desired floor.\n\nComing out of the apartment on her floor, Masha noticed that the elevator is now on the floor $z$ and has closed doors. Now she has to choose whether to use the stairs or use the elevator. \n\nIf the time that Masha needs to get to the Egor's floor by the stairs is strictly less than the time it will take her using the elevator, then she will use the stairs, otherwise she will choose the elevator.\n\nHelp Mary to understand whether to use the elevator or the stairs.\n\n\n-----Input-----\n\nThe only line contains six integers $x$, $y$, $z$, $t_1$, $t_2$, $t_3$ ($1 \\leq x, y, z, t_1, t_2, t_3 \\leq 1000$)\u00a0\u2014 the floor Masha is at, the floor Masha wants to get to, the floor the elevator is located on, the time it takes Masha to pass between two floors by stairs, the time it takes the elevator to pass between two floors and the time it takes for the elevator to close or open the doors.\n\nIt is guaranteed that $x \\ne y$.\n\n\n-----Output-----\n\nIf the time it will take to use the elevator is not greater than the time it will take to use the stairs, print \u00abYES\u00bb (without quotes), otherwise print \u00abNO> (without quotes).\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n5 1 4 4 2 1\n\nOutput\nYES\nInput\n1 6 6 2 1 1\n\nOutput\nNO\nInput\n4 1 7 4 1 2\n\nOutput\nYES\n\n\n-----Note-----\n\nIn the first example:\n\nIf Masha goes by the stairs, the time she spends is $4 \\cdot 4 = 16$, because she has to go $4$ times between adjacent floors and each time she spends $4$ seconds. \n\nIf she chooses the elevator, she will have to wait $2$ seconds while the elevator leaves the $4$-th floor and goes to the $5$-th. After that the doors will be opening for another $1$ second. Then Masha will enter the elevator, and she will have to wait for $1$ second for the doors closing. Next, the elevator will spend $4 \\cdot 2 = 8$ seconds going from the $5$-th floor to the $1$-st, because the elevator has to pass $4$ times between adjacent floors and spends $2$ seconds each time. And finally, it will take another $1$ second before the doors are open and Masha can come out. \n\nThus, all the way by elevator will take $2 + 1 + 1 + 8 + 1 = 13$ seconds, which is less than $16$ seconds, so Masha has to choose the elevator.\n\nIn the second example, it is more profitable for Masha to use the stairs, because it will take $13$ seconds to use the elevator, that is more than the $10$ seconds it will takes to go by foot.\n\nIn the third example, the time it takes to use the elevator is equal to the time it takes to walk up by the stairs, and is equal to $12$ seconds. That means Masha will take the elevator."} +{"id": "00312", "text": "One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.\n\nThen, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.\n\nAndrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.\n\nMore formally, you need to find such integer a (1 \u2264 a \u2264 n), that the probability that $|c - a|<|c - m|$ is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 m \u2264 n \u2264 10^9) \u2014 the range of numbers in the game, and the number selected by Misha respectively.\n\n\n-----Output-----\n\nPrint a single number \u2014 such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.\n\n\n-----Examples-----\nInput\n3 1\n\nOutput\n2\nInput\n4 3\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.\n\nIn the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less."} +{"id": "00313", "text": "Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.\n\nOne two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).\n\nThe University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).\n\nThe official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.\n\nAlena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.\n\nOf course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.\n\nAlena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.\n\n\n-----Input-----\n\nThe first line of the input contains a positive integer n (1 \u2264 n \u2264 100) \u2014 the number of lessons at the university. \n\nThe second line contains n numbers a_{i} (0 \u2264 a_{i} \u2264 1). Number a_{i} equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a_1, a_2, ..., a_{n} are separated by spaces.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of pairs during which Alena stays at the university.\n\n\n-----Examples-----\nInput\n5\n0 1 0 1 1\n\nOutput\n4\n\nInput\n7\n1 0 1 0 0 1 0\n\nOutput\n4\n\nInput\n1\n0\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair. \n\nIn the last sample Alena doesn't have a single pair, so she spends all the time at home."} +{"id": "00314", "text": "Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies.\n\nAt first, Arya and Bran have 0 Candies. There are n days, at the i-th day, Arya finds a_{i} candies in a box, that is given by the Many-Faced God. Every day she can give Bran at most 8 of her candies. If she don't give him the candies at the same day, they are saved for her and she can give them to him later.\n\nYour task is to find the minimum number of days Arya needs to give Bran k candies before the end of the n-th day. Formally, you need to output the minimum day index to the end of which k candies will be given out (the days are indexed from 1 to n).\n\nPrint -1 if she can't give him k candies during n given days.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 100, 1 \u2264 k \u2264 10000).\n\nThe second line contains n integers a_1, a_2, a_3, ..., a_{n} (1 \u2264 a_{i} \u2264 100).\n\n\n-----Output-----\n\nIf it is impossible for Arya to give Bran k candies within n days, print -1.\n\nOtherwise print a single integer\u00a0\u2014 the minimum number of days Arya needs to give Bran k candies before the end of the n-th day.\n\n\n-----Examples-----\nInput\n2 3\n1 2\n\nOutput\n2\nInput\n3 17\n10 10 10\n\nOutput\n3\nInput\n1 9\n10\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first sample, Arya can give Bran 3 candies in 2 days.\n\nIn the second sample, Arya can give Bran 17 candies in 3 days, because she can give him at most 8 candies per day.\n\nIn the third sample, Arya can't give Bran 9 candies, because she can give him at most 8 candies per day and she must give him the candies within 1 day."} +{"id": "00315", "text": "Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. \n\nEmpirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. \n\nPolycarp analysed all his affairs over the next n days and made a sequence of n integers a_1, a_2, ..., a_{n}, where a_{i} is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).\n\nHelp Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times. \n\nWrite a program that will find the minumum number of additional walks and the appropriate schedule\u00a0\u2014 the sequence of integers b_1, b_2, ..., b_{n} (b_{i} \u2265 a_{i}), where b_{i} means the total number of walks with the dog on the i-th day.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n, k \u2264 500)\u00a0\u2014 the number of days and the minimum number of walks with Cormen for any two consecutive days. \n\nThe second line contains integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 500)\u00a0\u2014 the number of walks with Cormen on the i-th day which Polycarp has already planned. \n\n\n-----Output-----\n\nIn the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days. \n\nIn the second line print n integers b_1, b_2, ..., b_{n}, where b_{i}\u00a0\u2014 the total number of walks on the i-th day according to the found solutions (a_{i} \u2264 b_{i} for all i from 1 to n). If there are multiple solutions, print any of them. \n\n\n-----Examples-----\nInput\n3 5\n2 0 1\n\nOutput\n4\n2 3 2\n\nInput\n3 1\n0 0 0\n\nOutput\n1\n0 1 0\n\nInput\n4 6\n2 4 3 5\n\nOutput\n0\n2 4 3 5"} +{"id": "00316", "text": "While playing with geometric figures Alex has accidentally invented a concept of a $n$-th order rhombus in a cell grid.\n\nA $1$-st order rhombus is just a square $1 \\times 1$ (i.e just a cell).\n\nA $n$-th order rhombus for all $n \\geq 2$ one obtains from a $n-1$-th order rhombus adding all cells which have a common side with it to it (look at the picture to understand it better).\n\n [Image] \n\nAlex asks you to compute the number of cells in a $n$-th order rhombus.\n\n\n-----Input-----\n\nThe first and only input line contains integer $n$ ($1 \\leq n \\leq 100$)\u00a0\u2014 order of a rhombus whose numbers of cells should be computed.\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the number of cells in a $n$-th order rhombus.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\nInput\n2\n\nOutput\n5\nInput\n3\n\nOutput\n13\n\n\n-----Note-----\n\nImages of rhombus corresponding to the examples are given in the statement."} +{"id": "00317", "text": "A word or a sentence in some language is called a pangram if all the characters of the alphabet of this language appear in it at least once. Pangrams are often used to demonstrate fonts in printing or test the output devices.\n\nYou are given a string consisting of lowercase and uppercase Latin letters. Check whether this string is a pangram. We say that the string contains a letter of the Latin alphabet if this letter occurs in the string in uppercase or lowercase.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of characters in the string.\n\nThe second line contains the string. The string consists only of uppercase and lowercase Latin letters.\n\n\n-----Output-----\n\nOutput \"YES\", if the string is a pangram and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n12\ntoosmallword\n\nOutput\nNO\n\nInput\n35\nTheQuickBrownFoxJumpsOverTheLazyDog\n\nOutput\nYES"} +{"id": "00318", "text": "You are given the current time in 24-hour format hh:mm. Find and print the time after a minutes.\n\nNote that you should find only the time after a minutes, see the examples to clarify the problem statement.\n\nYou can read more about 24-hour format here https://en.wikipedia.org/wiki/24-hour_clock.\n\n\n-----Input-----\n\nThe first line contains the current time in the format hh:mm (0 \u2264 hh < 24, 0 \u2264 mm < 60). The hours and the minutes are given with two digits (the hours or the minutes less than 10 are given with the leading zeroes).\n\nThe second line contains integer a (0 \u2264 a \u2264 10^4) \u2014 the number of the minutes passed.\n\n\n-----Output-----\n\nThe only line should contain the time after a minutes in the format described in the input. Note that you should print exactly two digits for the hours and the minutes (add leading zeroes to the numbers if needed).\n\nSee the examples to check the input/output format.\n\n\n-----Examples-----\nInput\n23:59\n10\n\nOutput\n00:09\n\nInput\n20:20\n121\n\nOutput\n22:21\n\nInput\n10:10\n0\n\nOutput\n10:10"} +{"id": "00319", "text": "You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where a_{i}, j = 1 if the i-th switch turns on the j-th lamp and a_{i}, j = 0 if the i-th switch is not connected to the j-th lamp.\n\nInitially all m lamps are turned off.\n\nSwitches change state only from \"off\" to \"on\". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards.\n\nIt is guaranteed that if you push all n switches then all m lamps will be turned on.\n\nYour think that you have too many switches and you would like to ignore one of them. \n\nYour task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 2000) \u2014 the number of the switches and the number of the lamps.\n\nThe following n lines contain m characters each. The character a_{i}, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise.\n\nIt is guaranteed that if you press all n switches all m lamps will be turned on.\n\n\n-----Output-----\n\nPrint \"YES\" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print \"NO\" if there is no such switch.\n\n\n-----Examples-----\nInput\n4 5\n10101\n01000\n00111\n10000\n\nOutput\nYES\n\nInput\n4 5\n10100\n01000\n00110\n00101\n\nOutput\nNO"} +{"id": "00320", "text": "Valera has got n domino pieces in a row. Each piece consists of two halves \u2014 the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even.\n\nTo do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers x_{i}, y_{i} (1 \u2264 x_{i}, y_{i} \u2264 6). Number x_{i} is initially written on the upper half of the i-th domino, y_{i} is initially written on the lower half.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum required number of seconds. If Valera can't do the task in any time, print - 1.\n\n\n-----Examples-----\nInput\n2\n4 2\n6 4\n\nOutput\n0\n\nInput\n1\n2 3\n\nOutput\n-1\n\nInput\n3\n1 4\n2 3\n4 4\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything.\n\nIn the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd.\n\nIn the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8."} +{"id": "00321", "text": "Alice has a lovely piece of cloth. It has the shape of a square with a side of length $a$ centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length $b$ centimeters (where $b < a$). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).\n\nAlice would like to know whether the area of her cloth expressed in square centimeters is prime. Could you help her to determine it?\n\n\n-----Input-----\n\nThe first line contains a number $t$\u00a0($1 \\leq t \\leq 5$)\u00a0\u2014 the number of test cases.\n\nEach of the next $t$ lines describes the $i$-th test case. It contains two integers $a$ and $b~(1 \\leq b < a \\leq 10^{11})$\u00a0\u2014 the side length of Alice's square and the side length of the square that Bob wants.\n\n\n-----Output-----\n\nPrint $t$ lines, where the $i$-th line is the answer to the $i$-th test case. Print \"YES\" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print \"NO\".\n\nYou can print each letter in an arbitrary case (upper or lower).\n\n\n-----Example-----\nInput\n4\n6 5\n16 13\n61690850361 24777622630\n34 33\n\nOutput\nYES\nNO\nNO\nYES\n\n\n\n-----Note-----\n\nThe figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is $6^2 - 5^2 = 36 - 25 = 11$, which is prime, so the answer is \"YES\". [Image] \n\nIn the second case, the area is $16^2 - 13^2 = 87$, which is divisible by $3$. [Image] \n\nIn the third case, the area of the remaining piece is $61690850361^2 - 24777622630^2 = 3191830435068605713421$. This number is not prime because $3191830435068605713421 = 36913227731 \\cdot 86468472991 $.\n\nIn the last case, the area is $34^2 - 33^2 = 67$."} +{"id": "00322", "text": "You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.\n\n\n-----Input-----\n\nThe first line contains a single positive integer n (2 \u2264 n \u2264 10^5).\n\nThe following n lines contain coordinates of the points. The i-th of these lines contains two single integers x_{i} and y_{i} (|x_{i}|, |y_{i}| \u2264 10^9, x_{i} \u2260 0). No two points coincide.\n\n\n-----Output-----\n\nPrint \"Yes\" if there is such a point, \"No\" \u2014 otherwise.\n\nYou can print every letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n3\n1 1\n-1 -1\n2 -1\n\nOutput\nYes\nInput\n4\n1 1\n2 2\n-1 1\n-2 2\n\nOutput\nNo\nInput\n3\n1 2\n2 1\n4 60\n\nOutput\nYes\n\n\n-----Note-----\n\nIn the first example the second point can be removed.\n\nIn the second example there is no suitable for the condition point.\n\nIn the third example any point can be removed."} +{"id": "00323", "text": "Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It's well known that universities provide students with dormitory for the period of university studies. Consequently Noora had to leave Vi\u010dkopolis and move to Pavlopolis. Thus Leha was left completely alone in a quiet town Vi\u010dkopolis. He almost even fell into a depression from boredom!\n\nLeha came up with a task for himself to relax a little. He chooses two integers A and B and then calculates the greatest common divisor of integers \"A factorial\" and \"B factorial\". Formally the hacker wants to find out GCD(A!, B!). It's well known that the factorial of an integer x is a product of all positive integers less than or equal to x. Thus x! = 1\u00b72\u00b73\u00b7...\u00b7(x - 1)\u00b7x. For example 4! = 1\u00b72\u00b73\u00b74 = 24. Recall that GCD(x, y) is the largest positive integer q that divides (without a remainder) both x and y.\n\nLeha has learned how to solve this task very effective. You are able to cope with it not worse, aren't you?\n\n\n-----Input-----\n\nThe first and single line contains two integers A and B (1 \u2264 A, B \u2264 10^9, min(A, B) \u2264 12).\n\n\n-----Output-----\n\nPrint a single integer denoting the greatest common divisor of integers A! and B!.\n\n\n-----Example-----\nInput\n4 3\n\nOutput\n6\n\n\n\n-----Note-----\n\nConsider the sample.\n\n4! = 1\u00b72\u00b73\u00b74 = 24. 3! = 1\u00b72\u00b73 = 6. The greatest common divisor of integers 24 and 6 is exactly 6."} +{"id": "00324", "text": "Let's call a positive integer composite if it has at least one divisor other than $1$ and itself. For example:\n\n the following numbers are composite: $1024$, $4$, $6$, $9$; the following numbers are not composite: $13$, $1$, $2$, $3$, $37$. \n\nYou are given a positive integer $n$. Find two composite integers $a,b$ such that $a-b=n$.\n\nIt can be proven that solution always exists.\n\n\n-----Input-----\n\nThe input contains one integer $n$ ($1 \\leq n \\leq 10^7$): the given integer.\n\n\n-----Output-----\n\nPrint two composite integers $a,b$ ($2 \\leq a, b \\leq 10^9, a-b=n$).\n\nIt can be proven, that solution always exists.\n\nIf there are several possible solutions, you can print any. \n\n\n-----Examples-----\nInput\n1\n\nOutput\n9 8\n\nInput\n512\n\nOutput\n4608 4096"} +{"id": "00325", "text": "There is a directed graph with N vertices numbered 1 to N and M edges.\nThe i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge.\nAdditionally, there is a button on Vertex N.\nWe will play a game on this graph.\nYou start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins.\nIt takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it.\nAs usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.\nWhen you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.)\nHowever, when you end the game, you will be asked to pay T \\times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \\times P coins, you will have to pay all of your coins instead.\nYour score will be the number of coins you have after this payment.\nDetermine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2500\n - 1 \\leq M \\leq 5000\n - 1 \\leq A_i, B_i \\leq N\n - 1 \\leq C_i \\leq 10^5\n - 0 \\leq P \\leq 10^5\n - All values in input are integers.\n - Vertex N can be reached from Vertex 1.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M P\nA_1 B_1 C_1\n:\nA_M B_M C_M\n\n-----Output-----\nIf there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print -1.\n\n-----Sample Input-----\n3 3 10\n1 2 20\n2 3 30\n1 3 45\n\n-----Sample Output-----\n35\n\n\nThere are two ways to travel from Vertex 1 to Vertex 3:\n - Vertex 1 \\rightarrow 2 \\rightarrow 3: You collect 20 + 30 = 50 coins on the way. After two minutes from the start of the game, you press the button, pay 2 \\times 10 = 20 coins, and you have 50 - 20 = 30 coins left.\n - Vertex 1 \\rightarrow 2: You collect 45 coins on the way. After one minute from the start of the game, you press the button, pay 1 \\times 10 = 10 coins, and you have 45 - 10 = 35 coins left.\nThus, the maximum score that can be obtained is 35."} +{"id": "00326", "text": "We have N strings of lowercase English letters: S_1, S_2, \\cdots, S_N.\nTakahashi wants to make a string that is a palindrome by choosing one or more of these strings - the same string can be chosen more than once - and concatenating them in some order of his choice.\nThe cost of using the string S_i once is C_i, and the cost of using it multiple times is C_i multiplied by that number of times.\nFind the minimum total cost needed to choose strings so that Takahashi can make a palindrome.\nIf there is no choice of strings in which he can make a palindrome, print -1.\n\n-----Constraints-----\n - 1 \\leq N \\leq 50\n - 1 \\leq |S_i| \\leq 20\n - S_i consists of lowercase English letters.\n - 1 \\leq C_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS_1 C_1\nS_2 C_2\n:\nS_N C_N\n\n-----Output-----\nPrint the minimum total cost needed to choose strings so that Takahashi can make a palindrome, or -1 if there is no such choice.\n\n-----Sample Input-----\n3\nba 3\nabc 4\ncbaa 5\n\n-----Sample Output-----\n7\n\nWe have ba, abc, and cbaa.\nFor example, we can use ba once and abc once for a cost of 7, then concatenate them in the order abc, ba to make a palindrome.\nAlso, we can use abc once and cbaa once for a cost of 9, then concatenate them in the order cbaa, abc to make a palindrome.\nWe cannot make a palindrome for a cost less than 7, so we should print 7."} +{"id": "00327", "text": "Since Grisha behaved well last year, at New Year's Eve he was visited by Ded Moroz who brought an enormous bag of gifts with him! The bag contains n sweet candies from the good ol' bakery, each labeled from 1 to n corresponding to its tastiness. No two candies have the same tastiness.\n\nThe choice of candies has a direct effect on Grisha's happiness. One can assume that he should take the tastiest ones\u00a0\u2014 but no, the holiday magic turns things upside down. It is the xor-sum of tastinesses that matters, not the ordinary sum!\n\nA xor-sum of a sequence of integers a_1, a_2, ..., a_{m} is defined as the bitwise XOR of all its elements: $a_{1} \\oplus a_{2} \\oplus \\ldots \\oplus a_{m}$, here $\\oplus$ denotes the bitwise XOR operation; more about bitwise XOR can be found here.\n\nDed Moroz warned Grisha he has more houses to visit, so Grisha can take no more than k candies from the bag. Help Grisha determine the largest xor-sum (largest xor-sum means maximum happiness!) he can obtain.\n\n\n-----Input-----\n\nThe sole string contains two integers n and k (1 \u2264 k \u2264 n \u2264 10^18).\n\n\n-----Output-----\n\nOutput one number\u00a0\u2014 the largest possible xor-sum.\n\n\n-----Examples-----\nInput\n4 3\n\nOutput\n7\n\nInput\n6 6\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first sample case, one optimal answer is 1, 2 and 4, giving the xor-sum of 7.\n\nIn the second sample case, one can, for example, take all six candies and obtain the xor-sum of 7."} +{"id": "00328", "text": "There are $n$ points on the plane, $(x_1,y_1), (x_2,y_2), \\ldots, (x_n,y_n)$.\n\nYou need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle.\n\n\n-----Input-----\n\nFirst line contains one integer $n$ ($1 \\leq n \\leq 10^5$).\n\nEach of the next $n$ lines contains two integers $x_i$ and $y_i$ ($1 \\leq x_i,y_i \\leq 10^9$).\n\n\n-----Output-----\n\nPrint the minimum length of the shorter side of the triangle. It can be proved that it's always an integer.\n\n\n-----Examples-----\nInput\n3\n1 1\n1 2\n2 1\n\nOutput\n3\nInput\n4\n1 1\n1 2\n2 1\n2 2\n\nOutput\n4\n\n\n-----Note-----\n\nIllustration for the first example: [Image]\n\nIllustration for the second example: [Image]"} +{"id": "00329", "text": "Alice likes word \"nineteen\" very much. She has a string s and wants the string to contain as many such words as possible. For that reason she can rearrange the letters of the string.\n\nFor example, if she has string \"xiineteenppnnnewtnee\", she can get string \"xnineteenppnineteenw\", containing (the occurrences marked) two such words. More formally, word \"nineteen\" occurs in the string the number of times you can read it starting from some letter of the string. Of course, you shouldn't skip letters.\n\nHelp her to find the maximum number of \"nineteen\"s that she can get in her string.\n\n\n-----Input-----\n\nThe first line contains a non-empty string s, consisting only of lowercase English letters. The length of string s doesn't exceed 100.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of \"nineteen\"s that she can get in her string.\n\n\n-----Examples-----\nInput\nnniinneetteeeenn\n\nOutput\n2\nInput\nnneteenabcnneteenabcnneteenabcnneteenabcnneteenabcii\n\nOutput\n2\nInput\nnineteenineteen\n\nOutput\n2"} +{"id": "00330", "text": "The weather is fine today and hence it's high time to climb the nearby pine and enjoy the landscape.\n\nThe pine's trunk includes several branches, located one above another and numbered from 2 to y. Some of them (more precise, from 2 to p) are occupied by tiny vile grasshoppers which you're at war with. These grasshoppers are known for their awesome jumping skills: the grasshopper at branch x can jump to branches $2 \\cdot x, 3 \\cdot x, \\ldots, \\lfloor \\frac{y}{x} \\rfloor \\cdot x$.\n\nKeeping this in mind, you wisely decided to choose such a branch that none of the grasshoppers could interrupt you. At the same time you wanna settle as high as possible since the view from up there is simply breathtaking.\n\nIn other words, your goal is to find the highest branch that cannot be reached by any of the grasshoppers or report that it's impossible.\n\n\n-----Input-----\n\nThe only line contains two integers p and y (2 \u2264 p \u2264 y \u2264 10^9).\n\n\n-----Output-----\n\nOutput the number of the highest suitable branch. If there are none, print -1 instead.\n\n\n-----Examples-----\nInput\n3 6\n\nOutput\n5\n\nInput\n3 4\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample case grasshopper from branch 2 reaches branches 2, 4 and 6 while branch 3 is initially settled by another grasshopper. Therefore the answer is 5.\n\nIt immediately follows that there are no valid branches in second sample case."} +{"id": "00331", "text": "Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us.\n\n [Image] \n\nThe girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 \u2264 i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased.\n\nYou will be given n integers a_1, a_2, ..., a_{n} that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then a_{i} equals 0. Otherwise, house i can be bought, and a_{i} represents the money required to buy it, in dollars.\n\nAs Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, and k (2 \u2264 n \u2264 100, 1 \u2264 m \u2264 n, 1 \u2264 k \u2264 100)\u00a0\u2014 the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 100)\u00a0\u2014 denoting the availability and the prices of the houses.\n\nIt is guaranteed that a_{m} = 0 and that it is possible to purchase some house with no more than k dollars.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy.\n\n\n-----Examples-----\nInput\n5 1 20\n0 27 32 21 19\n\nOutput\n40\nInput\n7 3 50\n62 0 0 0 99 33 22\n\nOutput\n30\nInput\n10 5 100\n1 0 1 0 0 0 0 0 1 1\n\nOutput\n20\n\n\n-----Note-----\n\nIn the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters.\n\nIn the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away."} +{"id": "00332", "text": "Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task.\n\nTwo matrices $A$ and $B$ are given, each of them has size $n \\times m$. Nastya can perform the following operation to matrix $A$ unlimited number of times: take any square square submatrix of $A$ and transpose it (i.e. the element of the submatrix which was in the $i$-th row and $j$-th column of the submatrix will be in the $j$-th row and $i$-th column after transposing, and the transposed submatrix itself will keep its place in the matrix $A$). \n\nNastya's task is to check whether it is possible to transform the matrix $A$ to the matrix $B$.\n\n $\\left. \\begin{array}{|c|c|c|c|c|c|c|c|} \\hline 6 & {3} & {2} & {11} \\\\ \\hline 5 & {9} & {4} & {2} \\\\ \\hline 3 & {3} & {3} & {3} \\\\ \\hline 4 & {8} & {2} & {2} \\\\ \\hline 7 & {8} & {6} & {4} \\\\ \\hline \\end{array} \\right.$ Example of the operation \n\nAs it may require a lot of operations, you are asked to answer this question for Nastya.\n\nA square submatrix of matrix $M$ is a matrix which consist of all elements which comes from one of the rows with indeces $x, x+1, \\dots, x+k-1$ of matrix $M$ and comes from one of the columns with indeces $y, y+1, \\dots, y+k-1$ of matrix $M$. $k$ is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes).\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ separated by space ($1 \\leq n, m \\leq 500$)\u00a0\u2014 the numbers of rows and columns in $A$ and $B$ respectively.\n\nEach of the next $n$ lines contains $m$ integers, the $j$-th number in the $i$-th of these lines denotes the $j$-th element of the $i$-th row of the matrix $A$ ($1 \\leq A_{ij} \\leq 10^{9}$).\n\nEach of the next $n$ lines contains $m$ integers, the $j$-th number in the $i$-th of these lines denotes the $j$-th element of the $i$-th row of the matrix $B$ ($1 \\leq B_{ij} \\leq 10^{9}$).\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if it is possible to transform $A$ to $B$ and \"NO\" (without quotes) otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n2 2\n1 1\n6 1\n1 6\n1 1\n\nOutput\nYES\nInput\n2 2\n4 4\n4 5\n5 4\n4 4\n\nOutput\nNO\nInput\n3 3\n1 2 3\n4 5 6\n7 8 9\n1 4 7\n2 5 6\n3 8 9\n\nOutput\nYES\n\n\n-----Note-----\n\nConsider the third example. The matrix $A$ initially looks as follows.\n\n$$ \\begin{bmatrix} 1 & 2 & 3\\\\ 4 & 5 & 6\\\\ 7 & 8 & 9 \\end{bmatrix} $$\n\nThen we choose the whole matrix as transposed submatrix and it becomes\n\n$$ \\begin{bmatrix} 1 & 4 & 7\\\\ 2 & 5 & 8\\\\ 3 & 6 & 9 \\end{bmatrix} $$\n\nThen we transpose the submatrix with corners in cells $(2, 2)$ and $(3, 3)$. \n\n$$ \\begin{bmatrix} 1 & 4 & 7\\\\ 2 & \\textbf{5} & \\textbf{8}\\\\ 3 & \\textbf{6} & \\textbf{9} \\end{bmatrix} $$\n\nSo matrix becomes\n\n$$ \\begin{bmatrix} 1 & 4 & 7\\\\ 2 & 5 & 6\\\\ 3 & 8 & 9 \\end{bmatrix} $$\n\nand it is $B$."} +{"id": "00333", "text": "While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.\n\nGiven two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one of them and not a subsequence of the other.\n\nA subsequence of some string is a sequence of characters that appears in the same order in the string, The appearances don't have to be consecutive, for example, strings \"ac\", \"bc\", \"abc\" and \"a\" are subsequences of string \"abc\" while strings \"abbc\" and \"acb\" are not. The empty string is a subsequence of any string. Any string is a subsequence of itself.\n\n\n-----Input-----\n\nThe first line contains string a, and the second line\u00a0\u2014 string b. Both of these strings are non-empty and consist of lowercase letters of English alphabet. The length of each string is not bigger than 10^5 characters.\n\n\n-----Output-----\n\nIf there's no uncommon subsequence, print \"-1\". Otherwise print the length of the longest uncommon subsequence of a and b.\n\n\n-----Examples-----\nInput\nabcd\ndefgh\n\nOutput\n5\n\nInput\na\na\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example: you can choose \"defgh\" from string b as it is the longest subsequence of string b that doesn't appear as a subsequence of string a."} +{"id": "00334", "text": "A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b, b + a, b + 2a, b + 3a, ... and Morty screams at times d, d + c, d + 2c, d + 3c, .... [Image] \n\nThe Monster will catch them if at any point they scream at the same time, so it wants to know when it will catch them (the first time they scream at the same time) or that they will never scream at the same time.\n\n\n-----Input-----\n\nThe first line of input contains two integers a and b (1 \u2264 a, b \u2264 100). \n\nThe second line contains two integers c and d (1 \u2264 c, d \u2264 100).\n\n\n-----Output-----\n\nPrint the first time Rick and Morty will scream at the same time, or - 1 if they will never scream at the same time.\n\n\n-----Examples-----\nInput\n20 2\n9 19\n\nOutput\n82\n\nInput\n2 1\n16 12\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82. \n\nIn the second sample testcase, all Rick's screams will be at odd times and Morty's will be at even times, so they will never scream at the same time."} +{"id": "00335", "text": "Little C loves number \u00ab3\u00bb very much. He loves all things about it.\n\nNow he has a positive integer $n$. He wants to split $n$ into $3$ positive integers $a,b,c$, such that $a+b+c=n$ and none of the $3$ integers is a multiple of $3$. Help him to find a solution.\n\n\n-----Input-----\n\nA single line containing one integer $n$ ($3 \\leq n \\leq 10^9$) \u2014 the integer Little C has.\n\n\n-----Output-----\n\nPrint $3$ positive integers $a,b,c$ in a single line, such that $a+b+c=n$ and none of them is a multiple of $3$.\n\nIt can be proved that there is at least one solution. If there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n1 1 1\nInput\n233\nOutput\n77 77 79"} +{"id": "00336", "text": "Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on duty. He doesn't want to be fired, so he has to quickly restore the painting. He remembers some facts about it. The painting is a square 3 \u00d7 3, each cell contains a single integer from 1 to n, and different cells may contain either different or equal integers. The sum of integers in each of four squares 2 \u00d7 2 is equal to the sum of integers in the top left square 2 \u00d7 2. Four elements a, b, c and d are known and are located as shown on the picture below. $\\left. \\begin{array}{|c|c|c|} \\hline ? & {a} & {?} \\\\ \\hline b & {?} & {c} \\\\ \\hline ? & {d} & {?} \\\\ \\hline \\end{array} \\right.$\n\nHelp Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number may be equal to 0, meaning Vasya remembers something wrong.\n\nTwo squares are considered to be different, if there exists a cell that contains two different integers in different squares.\n\n\n-----Input-----\n\nThe first line of the input contains five integers n, a, b, c and d (1 \u2264 n \u2264 100 000, 1 \u2264 a, b, c, d \u2264 n)\u00a0\u2014 maximum possible value of an integer in the cell and four integers that Vasya remembers.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of distinct valid squares.\n\n\n-----Examples-----\nInput\n2 1 1 1 2\n\nOutput\n2\n\nInput\n3 3 1 2 3\n\nOutput\n6\n\n\n\n-----Note-----\n\nBelow are all the possible paintings for the first sample. $\\left. \\begin{array}{|l|l|l|} \\hline 2 & {1} & {2} \\\\ \\hline 1 & {1} & {1} \\\\ \\hline 1 & {2} & {1} \\\\ \\hline \\end{array} \\right.$ $\\left. \\begin{array}{|l|l|l|} \\hline 2 & {1} & {2} \\\\ \\hline 1 & {2} & {1} \\\\ \\hline 1 & {2} & {1} \\\\ \\hline \\end{array} \\right.$\n\nIn the second sample, only paintings displayed below satisfy all the rules. $\\left. \\begin{array}{|c|c|c|} \\hline 2 & {3} & {1} \\\\ \\hline 1 & {1} & {2} \\\\ \\hline 2 & {3} & {1} \\\\ \\hline \\end{array} \\right.$ $\\left. \\begin{array}{|c|c|c|} \\hline 2 & {3} & {1} \\\\ \\hline 1 & {2} & {2} \\\\ \\hline 2 & {3} & {1} \\\\ \\hline \\end{array} \\right.$ $\\left. \\begin{array}{|l|l|l|} \\hline 2 & {3} & {1} \\\\ \\hline 1 & {3} & {2} \\\\ \\hline 2 & {3} & {1} \\\\ \\hline \\end{array} \\right.$ $\\left. \\begin{array}{|c|c|c|} \\hline 3 & {3} & {2} \\\\ \\hline 1 & {1} & {2} \\\\ \\hline 3 & {3} & {2} \\\\ \\hline \\end{array} \\right.$ $\\left. \\begin{array}{|c|c|c|} \\hline 3 & {3} & {2} \\\\ \\hline 1 & {2} & {2} \\\\ \\hline 3 & {3} & {2} \\\\ \\hline \\end{array} \\right.$ $\\left. \\begin{array}{|c|c|c|} \\hline 3 & {3} & {2} \\\\ \\hline 1 & {3} & {2} \\\\ \\hline 3 & {3} & {2} \\\\ \\hline \\end{array} \\right.$"} +{"id": "00337", "text": "Today's morning was exceptionally snowy. Meshanya decided to go outside and noticed a huge snowball rolling down the mountain! Luckily, there are two stones on that mountain.\n\nInitially, snowball is at height $h$ and it has weight $w$. Each second the following sequence of events happens: snowball's weights increases by $i$, where $i$\u00a0\u2014 is the current height of snowball, then snowball hits the stone (if it's present at the current height), then snowball moves one meter down. If the snowball reaches height zero, it stops.\n\nThere are exactly two stones on the mountain. First stone has weight $u_1$ and is located at height $d_1$, the second one\u00a0\u2014 $u_2$ and $d_2$ respectively. When the snowball hits either of two stones, it loses weight equal to the weight of that stone. If after this snowball has negative weight, then its weight becomes zero, but the snowball continues moving as before. [Image] \n\nFind the weight of the snowball when it stops moving, that is, it reaches height\u00a00.\n\n\n-----Input-----\n\nFirst line contains two integers $w$ and $h$\u00a0\u2014 initial weight and height of the snowball ($0 \\le w \\le 100$; $1 \\le h \\le 100$).\n\nSecond line contains two integers $u_1$ and $d_1$\u00a0\u2014 weight and height of the first stone ($0 \\le u_1 \\le 100$; $1 \\le d_1 \\le h$).\n\nThird line contains two integers $u_2$ and $d_2$\u00a0\u2014 weight and heigth of the second stone ($0 \\le u_2 \\le 100$; $1 \\le d_2 \\le h$; $d_1 \\ne d_2$). Notice that stones always have different heights.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 final weight of the snowball after it reaches height\u00a00.\n\n\n-----Examples-----\nInput\n4 3\n1 1\n1 2\n\nOutput\n8\nInput\n4 3\n9 2\n0 1\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first example, initially a snowball of weight 4 is located at a height of 3, there are two stones of weight 1, at a height of 1 and 2, respectively. The following events occur sequentially: The weight of the snowball increases by 3 (current height), becomes equal to 7. The snowball moves one meter down, the current height becomes equal to 2. The weight of the snowball increases by 2 (current height), becomes equal to 9. The snowball hits the stone, its weight decreases by 1 (the weight of the stone), becomes equal to 8. The snowball moves one meter down, the current height becomes equal to 1. The weight of the snowball increases by 1 (current height), becomes equal to 9. The snowball hits the stone, its weight decreases by 1 (the weight of the stone), becomes equal to 8. The snowball moves one meter down, the current height becomes equal to 0. \n\nThus, at the end the weight of the snowball is equal to 8."} +{"id": "00338", "text": "At the beginning of the school year Berland State University starts two city school programming groups, for beginners and for intermediate coders. The children were tested in order to sort them into groups. According to the results, each student got some score from 1 to m points. We know that c_1 schoolchildren got 1 point, c_2 children got 2 points, ..., c_{m} children got m points. Now you need to set the passing rate k (integer from 1 to m): all schoolchildren who got less than k points go to the beginner group and those who get at strictly least k points go to the intermediate group. We know that if the size of a group is more than y, then the university won't find a room for them. We also know that if a group has less than x schoolchildren, then it is too small and there's no point in having classes with it. So, you need to split all schoolchildren into two groups so that the size of each group was from x to y, inclusive. \n\nHelp the university pick the passing rate in a way that meets these requirements.\n\n\n-----Input-----\n\nThe first line contains integer m (2 \u2264 m \u2264 100). The second line contains m integers c_1, c_2, ..., c_{m}, separated by single spaces (0 \u2264 c_{i} \u2264 100). The third line contains two space-separated integers x and y (1 \u2264 x \u2264 y \u2264 10000). At least one c_{i} is greater than 0.\n\n\n-----Output-----\n\nIf it is impossible to pick a passing rate in a way that makes the size of each resulting groups at least x and at most y, print 0. Otherwise, print an integer from 1 to m \u2014 the passing rate you'd like to suggest. If there are multiple possible answers, print any of them.\n\n\n-----Examples-----\nInput\n5\n3 4 3 2 1\n6 8\n\nOutput\n3\n\nInput\n5\n0 3 3 4 2\n3 10\n\nOutput\n4\n\nInput\n2\n2 5\n3 6\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample the beginner group has 7 students, the intermediate group has 6 of them. \n\nIn the second sample another correct answer is 3."} +{"id": "00339", "text": "Right now she actually isn't. But she will be, if you don't solve this problem.\n\nYou are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: Subtract 1 from x. This operation costs you A coins. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. What is the minimum amount of coins you have to pay to make x equal to 1?\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 2\u00b710^9).\n\nThe second line contains a single integer k (1 \u2264 k \u2264 2\u00b710^9).\n\nThe third line contains a single integer A (1 \u2264 A \u2264 2\u00b710^9).\n\nThe fourth line contains a single integer B (1 \u2264 B \u2264 2\u00b710^9).\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the minimum amount of coins you have to pay to make x equal to 1.\n\n\n-----Examples-----\nInput\n9\n2\n3\n1\n\nOutput\n6\n\nInput\n5\n5\n2\n20\n\nOutput\n8\n\nInput\n19\n3\n4\n2\n\nOutput\n12\n\n\n\n-----Note-----\n\nIn the first testcase, the optimal strategy is as follows: Subtract 1 from x (9 \u2192 8) paying 3 coins. Divide x by 2 (8 \u2192 4) paying 1 coin. Divide x by 2 (4 \u2192 2) paying 1 coin. Divide x by 2 (2 \u2192 1) paying 1 coin. \n\nThe total cost is 6 coins.\n\nIn the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total."} +{"id": "00340", "text": "JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today the problem is as follows. Given an integer $n$, you can perform the following operations zero or more times: mul $x$: multiplies $n$ by $x$ (where $x$ is an arbitrary positive integer). sqrt: replaces $n$ with $\\sqrt{n}$ (to apply this operation, $\\sqrt{n}$ must be an integer). \n\nYou can perform these operations as many times as you like. What is the minimum value of $n$, that can be achieved and what is the minimum number of operations, to achieve that minimum value?\n\nApparently, no one in the class knows the answer to this problem, maybe you can help them?\n\n\n-----Input-----\n\nThe only line of the input contains a single integer $n$ ($1 \\le n \\le 10^6$)\u00a0\u2014 the initial number.\n\n\n-----Output-----\n\nPrint two integers: the minimum integer $n$ that can be achieved using the described operations and the minimum number of operations required.\n\n\n-----Examples-----\nInput\n20\n\nOutput\n10 2\nInput\n5184\n\nOutput\n6 4\n\n\n-----Note-----\n\nIn the first example, you can apply the operation mul $5$ to get $100$ and then sqrt to get $10$.\n\nIn the second example, you can first apply sqrt to get $72$, then mul $18$ to get $1296$ and finally two more sqrt and you get $6$.\n\nNote, that even if the initial value of $n$ is less or equal $10^6$, it can still become greater than $10^6$ after applying one or more operations."} +{"id": "00341", "text": "At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:\n - The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)\n - Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):\n - R points for winning with Rock;\n - S points for winning with Scissors;\n - P points for winning with Paper.\n - However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.)\nBefore the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.\nThe information Takahashi obtained is given as a string T. If the i-th character of T (1 \\leq i \\leq N) is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.\nWhat is the maximum total score earned in the game by adequately choosing the hand to play in each round?\n\n-----Notes-----\nIn this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.\n - If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;\n - if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;\n - if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;\n - if both players play the same hand, it is a draw.\n\n-----Constraints-----\n - 2 \\leq N \\leq 10^5\n - 1 \\leq K \\leq N-1\n - 1 \\leq R,S,P \\leq 10^4\n - N,K,R,S, and P are all integers.\n - |T| = N\n - T consists of r, p, and s.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nR S P\nT\n\n-----Output-----\nPrint the maximum total score earned in the game.\n\n-----Sample Input-----\n5 2\n8 7 6\nrsrpr\n\n-----Sample Output-----\n27\n\nThe machine will play {Rock, Scissors, Rock, Paper, Rock}.\nWe can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.\nWe cannot earn more points, so the answer is 27."} +{"id": "00342", "text": "Let's call a string good if and only if it consists of only two types of letters\u00a0\u2014 'a' and 'b' and every two consecutive letters are distinct. For example \"baba\" and \"aba\" are good strings and \"abb\" is a bad string.\n\nYou have $a$ strings \"a\", $b$ strings \"b\" and $c$ strings \"ab\". You want to choose some subset of these strings and concatenate them in any arbitrarily order.\n\nWhat is the length of the longest good string you can obtain this way?\n\n\n-----Input-----\n\nThe first line contains three positive integers $a$, $b$, $c$ ($1 \\leq a, b, c \\leq 10^9$)\u00a0\u2014 the number of strings \"a\", \"b\" and \"ab\" respectively.\n\n\n-----Output-----\n\nPrint a single number\u00a0\u2014 the maximum possible length of the good string you can obtain.\n\n\n-----Examples-----\nInput\n1 1 1\n\nOutput\n4\n\nInput\n2 1 2\n\nOutput\n7\n\nInput\n3 5 2\n\nOutput\n11\n\nInput\n2 2 1\n\nOutput\n6\n\nInput\n1000000000 1000000000 1000000000\n\nOutput\n4000000000\n\n\n\n-----Note-----\n\nIn the first example the optimal string is \"baba\".\n\nIn the second example the optimal string is \"abababa\".\n\nIn the third example the optimal string is \"bababababab\".\n\nIn the fourth example the optimal string is \"ababab\"."} +{"id": "00343", "text": "Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each test they will get a mark from 1 to p. Vova is very smart and he can write every test for any mark, but he doesn't want to stand out from the crowd too much. If the sum of his marks for all tests exceeds value x, then his classmates notice how smart he is and start distracting him asking to let them copy his homework. And if the median of his marks will be lower than y points (the definition of a median is given in the notes), then his mom will decide that he gets too many bad marks and forbid him to play computer games.\n\nVova has already wrote k tests and got marks a_1, ..., a_{k}. He doesn't want to get into the first or the second situation described above and now he needs to determine which marks he needs to get for the remaining tests. Help him do that.\n\n\n-----Input-----\n\nThe first line contains 5 space-separated integers: n, k, p, x and y (1 \u2264 n \u2264 999, n is odd, 0 \u2264 k < n, 1 \u2264 p \u2264 1000, n \u2264 x \u2264 n\u00b7p, 1 \u2264 y \u2264 p). Here n is the number of tests that Vova is planned to write, k is the number of tests he has already written, p is the maximum possible mark for a test, x is the maximum total number of points so that the classmates don't yet disturb Vova, y is the minimum median point so that mom still lets him play computer games.\n\nThe second line contains k space-separated integers: a_1, ..., a_{k} (1 \u2264 a_{i} \u2264 p)\u00a0\u2014 the marks that Vova got for the tests he has already written.\n\n\n-----Output-----\n\nIf Vova cannot achieve the desired result, print \"-1\".\n\nOtherwise, print n - k space-separated integers\u00a0\u2014 the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them.\n\n\n-----Examples-----\nInput\n5 3 5 18 4\n3 5 4\n\nOutput\n4 1\n\nInput\n5 3 5 16 4\n5 5 5\n\nOutput\n-1\n\n\n\n-----Note-----\n\nThe median of sequence a_1,\u00a0...,\u00a0a_{n} where n is odd (in this problem n is always odd) is the element staying on (n + 1) / 2 position in the sorted list of a_{i}.\n\nIn the first sample the sum of marks equals 3 + 5 + 4 + 4 + 1 = 17, what doesn't exceed 18, that means that Vova won't be disturbed by his classmates. And the median point of the sequence {1, 3, 4, 4, 5} equals to 4, that isn't less than 4, so his mom lets him play computer games.\n\nPlease note that you do not have to maximize the sum of marks or the median mark. Any of the answers: \"4\u00a02\", \"2\u00a04\", \"5\u00a01\", \"1\u00a05\", \"4\u00a01\", \"1\u00a04\" for the first test is correct.\n\nIn the second sample Vova got three '5' marks, so even if he gets two '1' marks, the sum of marks will be 17, that is more than the required value of 16. So, the answer to this test is \"-1\"."} +{"id": "00344", "text": "Vitya has just started learning Berlanese language. It is known that Berlanese uses the Latin alphabet. Vowel letters are \"a\", \"o\", \"u\", \"i\", and \"e\". Other letters are consonant.\n\nIn Berlanese, there has to be a vowel after every consonant, but there can be any letter after any vowel. The only exception is a consonant \"n\"; after this letter, there can be any letter (not only a vowel) or there can be no letter at all. For example, the words \"harakiri\", \"yupie\", \"man\", and \"nbo\" are Berlanese while the words \"horse\", \"king\", \"my\", and \"nz\" are not.\n\nHelp Vitya find out if a word $s$ is Berlanese.\n\n\n-----Input-----\n\nThe first line of the input contains the string $s$ consisting of $|s|$ ($1\\leq |s|\\leq 100$) lowercase Latin letters.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if there is a vowel after every consonant except \"n\", otherwise print \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\nsumimasen\n\nOutput\nYES\n\nInput\nninja\n\nOutput\nYES\n\nInput\ncodeforces\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first and second samples, a vowel goes after each consonant except \"n\", so the word is Berlanese.\n\nIn the third sample, the consonant \"c\" goes after the consonant \"r\", and the consonant \"s\" stands on the end, so the word is not Berlanese."} +{"id": "00345", "text": "Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every $a$ and $b$ such that $1 \\leq a \\leq b \\leq 6$, there is exactly one domino with $a$ dots on one half and $b$ dots on the other half. The set contains exactly $21$ dominoes. Here is an exact illustration of his set:\n\n [Image] \n\nAlso, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.\n\nWhen placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.\n\nHow many dominoes at most can Anadi place on the edges of his graph?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\leq n \\leq 7$, $0 \\leq m \\leq \\frac{n\\cdot(n-1)}{2}$) \u2014 the number of vertices and the number of edges in the graph.\n\nThe next $m$ lines contain two integers each. Integers in the $i$-th line are $a_i$ and $b_i$ ($1 \\leq a, b \\leq n$, $a \\neq b$) and denote that there is an edge which connects vertices $a_i$ and $b_i$.\n\nThe graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.\n\n\n-----Output-----\n\nOutput one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.\n\n\n-----Examples-----\nInput\n4 4\n1 2\n2 3\n3 4\n4 1\n\nOutput\n4\n\nInput\n7 0\n\nOutput\n0\n\nInput\n3 1\n1 3\n\nOutput\n1\n\nInput\n7 21\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n2 3\n2 4\n2 5\n2 6\n2 7\n3 4\n3 5\n3 6\n3 7\n4 5\n4 6\n4 7\n5 6\n5 7\n6 7\n\nOutput\n16\n\n\n\n-----Note-----\n\nHere is an illustration of Anadi's graph from the first sample test:\n\n [Image] \n\nAnd here is one of the ways to place a domino on each of its edges:\n\n [Image] \n\nNote that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex $1$ have three dots."} +{"id": "00346", "text": "'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2. \n\nThe finals will have n questions, m of them are auction questions and n - m of them are regular questions. Each question has a price. The price of the i-th question is a_{i} points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player the points equal to the price of the question. The wrong answer to the question reduces the number of the player's points by the value of the question price.\n\nThe game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again.\n\nAll R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going to be surprised, but this problem was again entrusted for you to solve.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m (1 \u2264 n, m \u2264 100;\u00a0m \u2264 min(n, 30)) \u2014 the total number of questions and the number of auction questions, correspondingly. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^7) \u2014 the prices of the questions. The third line contains m distinct integers b_{i} (1 \u2264 b_{i} \u2264 n) \u2014 the numbers of auction questions. Assume that the questions are numbered from 1 to n.\n\n\n-----Output-----\n\nIn the single line, print the answer to the problem \u2014 the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type.\n\n\n-----Examples-----\nInput\n4 1\n1 3 7 5\n3\n\nOutput\n18\n\nInput\n3 2\n10 3 8\n2 3\n\nOutput\n40\n\nInput\n2 2\n100 200\n1 2\n\nOutput\n400"} +{"id": "00347", "text": "Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himself from the herd as the best cowmputer scientist in all of Bovinia. Kevin knows his submission time for each problem, the number of wrong submissions that he made on each problem, and his total numbers of successful and unsuccessful hacks. Because Codeforces scoring is complicated, Kevin wants you to write a program to compute his final score.\n\nCodeforces scores are computed as follows: If the maximum point value of a problem is x, and Kevin submitted correctly at minute m but made w wrong submissions, then his score on that problem is $\\operatorname{max}(0.3 x,(1 - \\frac{m}{250}) x - 50 w)$. His total score is equal to the sum of his scores for each problem. In addition, Kevin's total score gets increased by 100 points for each successful hack, but gets decreased by 50 points for each unsuccessful hack.\n\nAll arithmetic operations are performed with absolute precision and no rounding. It is guaranteed that Kevin's final score is an integer.\n\n\n-----Input-----\n\nThe first line of the input contains five space-separated integers m_1, m_2, m_3, m_4, m_5, where m_{i} (0 \u2264 m_{i} \u2264 119) is the time of Kevin's last submission for problem i. His last submission is always correct and gets accepted.\n\nThe second line contains five space-separated integers w_1, w_2, w_3, w_4, w_5, where w_{i} (0 \u2264 w_{i} \u2264 10) is Kevin's number of wrong submissions on problem i.\n\nThe last line contains two space-separated integers h_{s} and h_{u} (0 \u2264 h_{s}, h_{u} \u2264 20), denoting the Kevin's numbers of successful and unsuccessful hacks, respectively.\n\n\n-----Output-----\n\nPrint a single integer, the value of Kevin's final score.\n\n\n-----Examples-----\nInput\n20 40 60 80 100\n0 1 2 3 4\n1 0\n\nOutput\n4900\n\nInput\n119 119 119 119 119\n0 0 0 0 0\n10 0\n\nOutput\n4930\n\n\n\n-----Note-----\n\nIn the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets $(1 - \\frac{119}{250}) = \\frac{131}{250}$ of the points on each problem. So his score from solving problems is $\\frac{131}{250}(500 + 1000 + 1500 + 2000 + 2500) = 3930$. Adding in 10\u00b7100 = 1000 points from hacks, his total score becomes 3930 + 1000 = 4930."} +{"id": "00348", "text": "Alice has got addicted to a game called Sirtet recently.\n\nIn Sirtet, player is given an $n \\times m$ grid. Initially $a_{i,j}$ cubes are stacked up in the cell $(i,j)$. Two cells are called adjacent if they share a side. Player can perform the following operations: stack up one cube in two adjacent cells; stack up two cubes in one cell. \n\nCubes mentioned above are identical in height.\n\nHere is an illustration of the game. States on the right are obtained by performing one of the above operations on the state on the left, and grey cubes are added due to the operation. [Image] \n\nPlayer's goal is to make the height of all cells the same (i.e. so that each cell has the same number of cubes in it) using above operations. \n\nAlice, however, has found out that on some starting grids she may never reach the goal no matter what strategy she uses. Thus, she is wondering the number of initial grids such that $L \\le a_{i,j} \\le R$ for all $1 \\le i \\le n$, $1 \\le j \\le m$; player can reach the goal using above operations. \n\nPlease help Alice with it. Notice that the answer might be large, please output the desired value modulo $998,244,353$.\n\n\n-----Input-----\n\nThe only line contains four integers $n$, $m$, $L$ and $R$ ($1\\le n,m,L,R \\le 10^9$, $L \\le R$, $n \\cdot m \\ge 2$).\n\n\n-----Output-----\n\nOutput one integer, representing the desired answer modulo $998,244,353$.\n\n\n-----Examples-----\nInput\n2 2 1 1\n\nOutput\n1\n\nInput\n1 2 1 2\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample, the only initial grid that satisfies the requirements is $a_{1,1}=a_{2,1}=a_{1,2}=a_{2,2}=1$. Thus the answer should be $1$.\n\nIn the second sample, initial grids that satisfy the requirements are $a_{1,1}=a_{1,2}=1$ and $a_{1,1}=a_{1,2}=2$. Thus the answer should be $2$."} +{"id": "00349", "text": "You are given two $n \\times m$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. \n\nFor example, the matrix $\\begin{bmatrix} 9&10&11\\\\ 11&12&14\\\\ \\end{bmatrix}$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $\\begin{bmatrix} 1&1\\\\ 2&3\\\\ \\end{bmatrix}$ is not increasing because the first row is not strictly increasing.\n\nLet a position in the $i$-th row (from top) and $j$-th column (from left) in a matrix be denoted as $(i, j)$. \n\nIn one operation, you can choose any two numbers $i$ and $j$ and swap the number located in $(i, j)$ in the first matrix with the number in $(i, j)$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.\n\nYou would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print \"Possible\", otherwise, print \"Impossible\".\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\leq n,m \\leq 50$)\u00a0\u2014 the dimensions of each matrix.\n\nEach of the next $n$ lines contains $m$ integers $a_{i1}, a_{i2}, \\ldots, a_{im}$ ($1 \\leq a_{ij} \\leq 10^9$)\u00a0\u2014 the number located in position $(i, j)$ in the first matrix.\n\nEach of the next $n$ lines contains $m$ integers $b_{i1}, b_{i2}, \\ldots, b_{im}$ ($1 \\leq b_{ij} \\leq 10^9$)\u00a0\u2014 the number located in position $(i, j)$ in the second matrix.\n\n\n-----Output-----\n\nPrint a string \"Impossible\" or \"Possible\".\n\n\n-----Examples-----\nInput\n2 2\n2 10\n11 5\n9 4\n3 12\n\nOutput\nPossible\n\nInput\n2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11\n\nOutput\nPossible\n\nInput\n3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8\n\nOutput\nImpossible\n\n\n\n-----Note-----\n\nThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $\\begin{bmatrix} 9&10\\\\ 11&12\\\\ \\end{bmatrix}$ and $\\begin{bmatrix} 2&4\\\\ 3&5\\\\ \\end{bmatrix}$.\n\nIn the second example, we don't need to do any operations.\n\nIn the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices."} +{"id": "00350", "text": "You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied: the i-th letter occurs in the string no more than a_{i} times; the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once. \n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (2 \u2264 n \u2264 26)\u00a0\u2014 the number of letters in the alphabet.\n\nThe next line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum length of the string that meets all the requirements.\n\n\n-----Examples-----\nInput\n3\n2 5 5\n\nOutput\n11\n\nInput\n3\n1 1 2\n\nOutput\n3\n\n\n\n-----Note-----\n\nFor convenience let's consider an alphabet consisting of three letters: \"a\", \"b\", \"c\". In the first sample, some of the optimal strings are: \"cccaabbccbb\", \"aabcbcbcbcb\". In the second sample some of the optimal strings are: \"acc\", \"cbc\"."} +{"id": "00351", "text": "Makes solves problems on Decoforces and lots of other different online judges. Each problem is denoted by its difficulty \u2014 a positive integer number. Difficulties are measured the same across all the judges (the problem with difficulty d on Decoforces is as hard as the problem with difficulty d on any other judge). \n\nMakes has chosen n problems to solve on Decoforces with difficulties a_1, a_2, ..., a_{n}. He can solve these problems in arbitrary order. Though he can solve problem i with difficulty a_{i} only if he had already solved some problem with difficulty $d \\geq \\frac{a_{i}}{2}$ (no matter on what online judge was it).\n\nBefore starting this chosen list of problems, Makes has already solved problems with maximum difficulty k.\n\nWith given conditions it's easy to see that Makes sometimes can't solve all the chosen problems, no matter what order he chooses. So he wants to solve some problems on other judges to finish solving problems from his list. \n\nFor every positive integer y there exist some problem with difficulty y on at least one judge besides Decoforces.\n\nMakes can solve problems on any judge at any time, it isn't necessary to do problems from the chosen list one right after another.\n\nMakes doesn't have too much free time, so he asked you to calculate the minimum number of problems he should solve on other judges in order to solve all the chosen problems from Decoforces.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n, k (1 \u2264 n \u2264 10^3, 1 \u2264 k \u2264 10^9).\n\nThe second line contains n space-separated integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint minimum number of problems Makes should solve on other judges in order to solve all chosen problems on Decoforces.\n\n\n-----Examples-----\nInput\n3 3\n2 1 9\n\nOutput\n1\n\nInput\n4 20\n10 3 6 3\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example Makes at first solves problems 1 and 2. Then in order to solve the problem with difficulty 9, he should solve problem with difficulty no less than 5. The only available are difficulties 5 and 6 on some other judge. Solving any of these will give Makes opportunity to solve problem 3.\n\nIn the second example he can solve every problem right from the start."} +{"id": "00352", "text": "Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there.\n\nAt a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one diploma.\n\nThey also decided that there must be given at least min_1 and at most max_1 diplomas of the first degree, at least min_2 and at most max_2 diplomas of the second degree, and at least min_3 and at most max_3 diplomas of the third degree.\n\nAfter some discussion it was decided to choose from all the options of distributing diplomas satisfying these limitations the one that maximizes the number of participants who receive diplomas of the first degree. Of all these options they select the one which maximizes the number of the participants who receive diplomas of the second degree. If there are multiple of these options, they select the option that maximizes the number of diplomas of the third degree.\n\nChoosing the best option of distributing certificates was entrusted to Ilya, one of the best programmers of Berland. However, he found more important things to do, so it is your task now to choose the best option of distributing of diplomas, based on the described limitations.\n\nIt is guaranteed that the described limitations are such that there is a way to choose such an option of distributing diplomas that all n participants of the Olympiad will receive a diploma of some degree.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (3 \u2264 n \u2264 3\u00b710^6)\u00a0\u2014\u00a0the number of schoolchildren who will participate in the Olympiad.\n\nThe next line of the input contains two integers min_1 and max_1 (1 \u2264 min_1 \u2264 max_1 \u2264 10^6)\u00a0\u2014\u00a0the minimum and maximum limits on the number of diplomas of the first degree that can be distributed.\n\nThe third line of the input contains two integers min_2 and max_2 (1 \u2264 min_2 \u2264 max_2 \u2264 10^6)\u00a0\u2014\u00a0the minimum and maximum limits on the number of diplomas of the second degree that can be distributed. \n\nThe next line of the input contains two integers min_3 and max_3 (1 \u2264 min_3 \u2264 max_3 \u2264 10^6)\u00a0\u2014\u00a0the minimum and maximum limits on the number of diplomas of the third degree that can be distributed. \n\nIt is guaranteed that min_1 + min_2 + min_3 \u2264 n \u2264 max_1 + max_2 + max_3.\n\n\n-----Output-----\n\nIn the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas.\n\nThe optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first degree. Of all the suitable options, the best one is the one which maximizes the number of participants who receive diplomas of the second degree. If there are several of these options, the best one is the one that maximizes the number of diplomas of the third degree.\n\n\n-----Examples-----\nInput\n6\n1 5\n2 6\n3 7\n\nOutput\n1 2 3 \n\nInput\n10\n1 2\n1 3\n1 5\n\nOutput\n2 3 5 \n\nInput\n6\n1 3\n2 2\n2 2\n\nOutput\n2 2 2"} +{"id": "00353", "text": "Every summer Vitya comes to visit his grandmother in the countryside. This summer, he got a huge wart. Every grandma knows that one should treat warts when the moon goes down. Thus, Vitya has to catch the moment when the moon is down.\n\nMoon cycle lasts 30 days. The size of the visible part of the moon (in Vitya's units) for each day is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, and then cycle repeats, thus after the second 1 again goes 0.\n\nAs there is no internet in the countryside, Vitya has been watching the moon for n consecutive days and for each of these days he wrote down the size of the visible part of the moon. Help him find out whether the moon will be up or down next day, or this cannot be determined by the data he has.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 92)\u00a0\u2014 the number of consecutive days Vitya was watching the size of the visible part of the moon. \n\nThe second line contains n integers a_{i} (0 \u2264 a_{i} \u2264 15)\u00a0\u2014 Vitya's records.\n\nIt's guaranteed that the input data is consistent.\n\n\n-----Output-----\n\nIf Vitya can be sure that the size of visible part of the moon on day n + 1 will be less than the size of the visible part on day n, then print \"DOWN\" at the only line of the output. If he might be sure that the size of the visible part will increase, then print \"UP\". If it's impossible to determine what exactly will happen with the moon, print -1.\n\n\n-----Examples-----\nInput\n5\n3 4 5 6 7\n\nOutput\nUP\n\nInput\n7\n12 13 14 15 14 13 12\n\nOutput\nDOWN\n\nInput\n1\n8\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample, the size of the moon on the next day will be equal to 8, thus the answer is \"UP\".\n\nIn the second sample, the size of the moon on the next day will be 11, thus the answer is \"DOWN\".\n\nIn the third sample, there is no way to determine whether the size of the moon on the next day will be 7 or 9, thus the answer is -1."} +{"id": "00354", "text": "We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $s$ can transform to another superhero with name $t$ if $s$ can be made equal to $t$ by changing any vowel in $s$ to any other vowel and any consonant in $s$ to any other consonant. Multiple changes can be made.\n\nIn this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.\n\nGiven the names of two superheroes, determine if the superhero with name $s$ can be transformed to the Superhero with name $t$.\n\n\n-----Input-----\n\nThe first line contains the string $s$ having length between $1$ and $1000$, inclusive.\n\nThe second line contains the string $t$ having length between $1$ and $1000$, inclusive.\n\nBoth strings $s$ and $t$ are guaranteed to be different and consist of lowercase English letters only.\n\n\n-----Output-----\n\nOutput \"Yes\" (without quotes) if the superhero with name $s$ can be transformed to the superhero with name $t$ and \"No\" (without quotes) otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\na\nu\n\nOutput\nYes\n\nInput\nabc\nukm\n\nOutput\nYes\n\nInput\nakm\nua\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $s$ to $t$.\n\nIn the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $s$ to $t$."} +{"id": "00355", "text": "Galois is one of the strongest chess players of Byteforces. He has even invented a new variant of chess, which he named \u00abPawnChess\u00bb.\n\nThis new game is played on a board consisting of 8 rows and 8 columns. At the beginning of every game some black and white pawns are placed on the board. The number of black pawns placed is not necessarily equal to the number of white pawns placed. $8$ \n\nLets enumerate rows and columns with integers from 1 to 8. Rows are numbered from top to bottom, while columns are numbered from left to right. Now we denote as (r, c) the cell located at the row r and at the column c.\n\nThere are always two players A and B playing the game. Player A plays with white pawns, while player B plays with black ones. The goal of player A is to put any of his pawns to the row 1, while player B tries to put any of his pawns to the row 8. As soon as any of the players completes his goal the game finishes immediately and the succeeded player is declared a winner.\n\nPlayer A moves first and then they alternate turns. On his move player A must choose exactly one white pawn and move it one step upward and player B (at his turn) must choose exactly one black pawn and move it one step down. Any move is possible only if the targeted cell is empty. It's guaranteed that for any scenario of the game there will always be at least one move available for any of the players.\n\nMoving upward means that the pawn located in (r, c) will go to the cell (r - 1, c), while moving down means the pawn located in (r, c) will go to the cell (r + 1, c). Again, the corresponding cell must be empty, i.e. not occupied by any other pawn of any color.\n\nGiven the initial disposition of the board, determine who wins the game if both players play optimally. Note that there will always be a winner due to the restriction that for any game scenario both players will have some moves available.\n\n\n-----Input-----\n\nThe input consists of the board description given in eight lines, each line contains eight characters. Character 'B' is used to denote a black pawn, and character 'W' represents a white pawn. Empty cell is marked with '.'. \n\nIt's guaranteed that there will not be white pawns on the first row neither black pawns on the last row.\n\n\n-----Output-----\n\nPrint 'A' if player A wins the game on the given board, and 'B' if player B will claim the victory. Again, it's guaranteed that there will always be a winner on the given board.\n\n\n-----Examples-----\nInput\n........\n........\n.B....B.\n....W...\n........\n..W.....\n........\n........\n\nOutput\nA\n\nInput\n..B.....\n..W.....\n......B.\n........\n.....W..\n......B.\n........\n........\n\nOutput\nB\n\n\n\n-----Note-----\n\nIn the first sample player A is able to complete his goal in 3 steps by always moving a pawn initially located at (4, 5). Player B needs at least 5 steps for any of his pawns to reach the row 8. Hence, player A will be the winner."} +{"id": "00356", "text": "Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.\n\nHe can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.\n\nTwo arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.\n\nVasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.\n\nHelp Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.\n\n\n-----Input-----\n\nThe first line contains a single integer $n~(1 \\le n \\le 3 \\cdot 10^5)$ \u2014 the length of the first array.\n\nThe second line contains $n$ integers $a_1, a_2, \\cdots, a_n~(1 \\le a_i \\le 10^9)$ \u2014 elements of the array $A$.\n\nThe third line contains a single integer $m~(1 \\le m \\le 3 \\cdot 10^5)$ \u2014 the length of the second array.\n\nThe fourth line contains $m$ integers $b_1, b_2, \\cdots, b_m~(1 \\le b_i \\le 10^9)$ - elements of the array $B$.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.\n\nIf there is no way to make array equal, print \"-1\".\n\n\n-----Examples-----\nInput\n5\n11 2 3 5 7\n4\n11 7 3 7\n\nOutput\n3\n\nInput\n2\n1 2\n1\n100\n\nOutput\n-1\n\nInput\n3\n1 2 3\n3\n1 2 3\n\nOutput\n3"} +{"id": "00357", "text": "One day Alex was creating a contest about his friends, but accidentally deleted it. Fortunately, all the problems were saved, but now he needs to find them among other problems.\n\nBut there are too many problems, to do it manually. Alex asks you to write a program, which will determine if a problem is from this contest by its name.\n\nIt is known, that problem is from this contest if and only if its name contains one of Alex's friends' name exactly once. His friends' names are \"Danil\", \"Olya\", \"Slava\", \"Ann\" and \"Nikita\".\n\nNames are case sensitive.\n\n\n-----Input-----\n\nThe only line contains string from lowercase and uppercase letters and \"_\" symbols of length, not more than 100 \u2014 the name of the problem.\n\n\n-----Output-----\n\nPrint \"YES\", if problem is from this contest, and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\nAlex_and_broken_contest\n\nOutput\nNO\nInput\nNikitaAndString\n\nOutput\nYES\nInput\nDanil_and_Olya\n\nOutput\nNO"} +{"id": "00358", "text": "You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors.\n\nConsider positive integers a, a + 1, ..., b (a \u2264 b). You want to find the minimum integer l (1 \u2264 l \u2264 b - a + 1) such that for any integer x (a \u2264 x \u2264 b - l + 1) among l integers x, x + 1, ..., x + l - 1 there are at least k prime numbers. \n\nFind and print the required minimum l. If no value l meets the described limitations, print -1.\n\n\n-----Input-----\n\nA single line contains three space-separated integers a, b, k (1 \u2264 a, b, k \u2264 10^6;\u00a0a \u2264 b).\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the required minimum l. If there's no solution, print -1.\n\n\n-----Examples-----\nInput\n2 4 2\n\nOutput\n3\n\nInput\n6 13 1\n\nOutput\n4\n\nInput\n1 4 3\n\nOutput\n-1"} +{"id": "00359", "text": "Arkadiy has lots square photos with size a \u00d7 a. He wants to put some of them on a rectangular wall with size h \u00d7 w. \n\nThe photos which Arkadiy will put on the wall must form a rectangular grid and the distances between neighboring vertically and horizontally photos and also the distances between outside rows and columns of photos to the nearest bound of the wall must be equal to x, where x is some non-negative real number. Look on the picture below for better understanding of the statement.\n\n [Image] \n\nArkadiy haven't chosen yet how many photos he would put on the wall, however, he want to put at least one photo. Your task is to determine the minimum value of x which can be obtained after putting photos, or report that there is no way to put positive number of photos and satisfy all the constraints. Suppose that Arkadiy has enough photos to make any valid arrangement according to the constraints.\n\nNote that Arkadiy wants to put at least one photo on the wall. The photos should not overlap, should completely lie inside the wall bounds and should have sides parallel to the wall sides.\n\n\n-----Input-----\n\nThe first line contains three integers a, h and w (1 \u2264 a, h, w \u2264 10^9) \u2014 the size of photos and the height and the width of the wall.\n\n\n-----Output-----\n\nPrint one non-negative real number \u2014 the minimum value of x which can be obtained after putting the photos on the wall. The absolute or the relative error of the answer must not exceed 10^{ - 6}.\n\nPrint -1 if there is no way to put positive number of photos and satisfy the constraints.\n\n\n-----Examples-----\nInput\n2 18 13\n\nOutput\n0.5\n\nInput\n4 4 4\n\nOutput\n0\n\nInput\n3 4 3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example Arkadiy can put 7 rows of photos with 5 photos in each row, so the minimum value of x equals to 0.5.\n\nIn the second example Arkadiy can put only 1 photo which will take the whole wall, so the minimum value of x equals to 0.\n\nIn the third example there is no way to put positive number of photos and satisfy the constraints described in the statement, so the answer is -1."} +{"id": "00360", "text": "After lessons Nastya decided to read a book. The book contains $n$ chapters, going one after another, so that one page of the book belongs to exactly one chapter and each chapter contains at least one page.\n\nYesterday evening Nastya did not manage to finish reading the book, so she marked the page with number $k$ as the first page which was not read (i.e. she read all pages from the $1$-st to the $(k-1)$-th).\n\nThe next day Nastya's friend Igor came and asked her, how many chapters remain to be read by Nastya? Nastya is too busy now, so she asks you to compute the number of chapters she has not completely read yet (i.e. the number of chapters she has not started to read or has finished reading somewhere in the middle).\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 100$)\u00a0\u2014 the number of chapters in the book.\n\nThere are $n$ lines then. The $i$-th of these lines contains two integers $l_i$, $r_i$ separated by space ($l_1 = 1$, $l_i \\leq r_i$)\u00a0\u2014 numbers of the first and the last pages of the $i$-th chapter. It's guaranteed that $l_{i+1} = r_i + 1$ for all $1 \\leq i \\leq n-1$, and also that every chapter contains at most $100$ pages.\n\nThe $(n+2)$-th line contains a single integer $k$ ($1 \\leq k \\leq r_n$)\u00a0\u2014 the index of the marked page. \n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of chapters which has not been completely read so far.\n\n\n-----Examples-----\nInput\n3\n1 3\n4 7\n8 11\n2\n\nOutput\n3\n\nInput\n3\n1 4\n5 9\n10 12\n9\n\nOutput\n2\n\nInput\n1\n1 7\n4\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example the book contains $11$ pages and $3$ chapters\u00a0\u2014 $[1;3]$, $[4;7]$ and $[8;11]$. Nastya marked the $2$-nd page, so she finished in the middle of the $1$-st chapter. So, all chapters has not been read so far, so the answer is $3$.\n\nThe book in the second example contains $12$ pages and $3$ chapters too, but Nastya finished reading in the middle of the $2$-nd chapter, so that the answer is $2$."} +{"id": "00361", "text": "A large banner with word CODEFORCES was ordered for the 1000-th onsite round of Codeforces^{\u03c9} that takes place on the Miami beach. Unfortunately, the company that made the banner mixed up two orders and delivered somebody else's banner that contains someone else's word. The word on the banner consists only of upper-case English letters.\n\nThere is very little time to correct the mistake. All that we can manage to do is to cut out some substring from the banner, i.e. several consecutive letters. After that all the resulting parts of the banner will be glued into a single piece (if the beginning or the end of the original banner was cut out, only one part remains); it is not allowed change the relative order of parts of the banner (i.e. after a substring is cut, several first and last letters are left, it is allowed only to glue the last letters to the right of the first letters). Thus, for example, for example, you can cut a substring out from string 'TEMPLATE' and get string 'TEMPLE' (if you cut out string AT), 'PLATE' (if you cut out TEM), 'T' (if you cut out EMPLATE), etc.\n\nHelp the organizers of the round determine whether it is possible to cut out of the banner some substring in such a way that the remaining parts formed word CODEFORCES.\n\n\n-----Input-----\n\nThe single line of the input contains the word written on the banner. The word only consists of upper-case English letters. The word is non-empty and its length doesn't exceed 100 characters. It is guaranteed that the word isn't word CODEFORCES.\n\n\n-----Output-----\n\nPrint 'YES', if there exists a way to cut out the substring, and 'NO' otherwise (without the quotes).\n\n\n-----Examples-----\nInput\nCODEWAITFORITFORCES\n\nOutput\nYES\n\nInput\nBOTTOMCODER\n\nOutput\nNO\n\nInput\nDECODEFORCES\n\nOutput\nYES\n\nInput\nDOGEFORCES\n\nOutput\nNO"} +{"id": "00362", "text": "You are given a regular polygon with $n$ vertices labeled from $1$ to $n$ in counter-clockwise order. The triangulation of a given polygon is a set of triangles such that each vertex of each triangle is a vertex of the initial polygon, there is no pair of triangles such that their intersection has non-zero area, and the total area of all triangles is equal to the area of the given polygon. The weight of a triangulation is the sum of weigths of triangles it consists of, where the weight of a triagle is denoted as the product of labels of its vertices.\n\nCalculate the minimum weight among all triangulations of the polygon.\n\n\n-----Input-----\n\nThe first line contains single integer $n$ ($3 \\le n \\le 500$) \u2014 the number of vertices in the regular polygon.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum weight among all triangulations of the given polygon.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n6\n\nInput\n4\n\nOutput\n18\n\n\n\n-----Note-----\n\nAccording to Wiki: polygon triangulation is the decomposition of a polygonal area (simple polygon) $P$ into a set of triangles, i. e., finding a set of triangles with pairwise non-intersecting interiors whose union is $P$.\n\nIn the first example the polygon is a triangle, so we don't need to cut it further, so the answer is $1 \\cdot 2 \\cdot 3 = 6$.\n\nIn the second example the polygon is a rectangle, so it should be divided into two triangles. It's optimal to cut it using diagonal $1-3$ so answer is $1 \\cdot 2 \\cdot 3 + 1 \\cdot 3 \\cdot 4 = 6 + 12 = 18$."} +{"id": "00363", "text": "Vanya got an important task \u2014 he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers.\n\nVanya wants to know how many digits he will have to write down as he labels the books.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^9) \u2014 the number of books in the library.\n\n\n-----Output-----\n\nPrint the number of digits needed to number all the books.\n\n\n-----Examples-----\nInput\n13\n\nOutput\n17\n\nInput\n4\n\nOutput\n4\n\n\n\n-----Note-----\n\nNote to the first test. The books get numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, which totals to 17 digits.\n\nNote to the second sample. The books get numbers 1, 2, 3, 4, which totals to 4 digits."} +{"id": "00364", "text": "Alice and Bob got very bored during a long car trip so they decided to play a game. From the window they can see cars of different colors running past them. Cars are going one after another.\n\nThe game rules are like this. Firstly Alice chooses some color A, then Bob chooses some color B (A \u2260 B). After each car they update the number of cars of their chosen color that have run past them. Let's define this numbers after i-th car cnt_{A}(i) and cnt_{B}(i).\n\n If cnt_{A}(i) > cnt_{B}(i) for every i then the winner is Alice. If cnt_{B}(i) \u2265 cnt_{A}(i) for every i then the winner is Bob. Otherwise it's a draw. \n\nBob knows all the colors of cars that they will encounter and order of their appearance. Alice have already chosen her color A and Bob now wants to choose such color B that he will win the game (draw is not a win). Help him find this color.\n\nIf there are multiple solutions, print any of them. If there is no such color then print -1.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and A (1 \u2264 n \u2264 10^5, 1 \u2264 A \u2264 10^6) \u2013 number of cars and the color chosen by Alice.\n\nThe second line contains n integer numbers c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 10^6) \u2014 colors of the cars that Alice and Bob will encounter in the order of their appearance.\n\n\n-----Output-----\n\nOutput such color B (1 \u2264 B \u2264 10^6) that if Bob chooses it then he will win the game. If there are multiple solutions, print any of them. If there is no such color then print -1.\n\nIt is guaranteed that if there exists any solution then there exists solution with (1 \u2264 B \u2264 10^6).\n\n\n-----Examples-----\nInput\n4 1\n2 1 4 2\n\nOutput\n2\n\nInput\n5 2\n2 2 4 5 3\n\nOutput\n-1\n\nInput\n3 10\n1 2 3\n\nOutput\n4\n\n\n\n-----Note-----\n\nLet's consider availability of colors in the first example: cnt_2(i) \u2265 cnt_1(i) for every i, and color 2 can be the answer. cnt_4(2) < cnt_1(2), so color 4 isn't the winning one for Bob. All the other colors also have cnt_{j}(2) < cnt_1(2), thus they are not available. \n\nIn the third example every color is acceptable except for 10."} +{"id": "00365", "text": "A one-dimensional Japanese crossword can be represented as a binary string of length x. An encoding of this crossword is an array a of size n, where n is the number of segments formed completely of 1's, and a_{i} is the length of i-th segment. No two segments touch or intersect.\n\nFor example: If x = 6 and the crossword is 111011, then its encoding is an array {3, 2}; If x = 8 and the crossword is 01101010, then its encoding is an array {2, 1, 1}; If x = 5 and the crossword is 11111, then its encoding is an array {5}; If x = 5 and the crossword is 00000, then its encoding is an empty array. \n\nMishka wants to create a new one-dimensional Japanese crossword. He has already picked the length and the encoding for this crossword. And now he needs to check if there is exactly one crossword such that its length and encoding are equal to the length and encoding he picked. Help him to check it!\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and x (1 \u2264 n \u2264 100000, 1 \u2264 x \u2264 10^9) \u2014 the number of elements in the encoding and the length of the crossword Mishka picked.\n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10000) \u2014 the encoding.\n\n\n-----Output-----\n\nPrint YES if there exists exaclty one crossword with chosen length and encoding. Otherwise, print NO.\n\n\n-----Examples-----\nInput\n2 4\n1 3\n\nOutput\nNO\n\nInput\n3 10\n3 3 2\n\nOutput\nYES\n\nInput\n2 10\n1 3\n\nOutput\nNO"} +{"id": "00366", "text": "You have unlimited number of coins with values $1, 2, \\ldots, n$. You want to select some set of coins having the total value of $S$. \n\nIt is allowed to have multiple coins with the same value in the set. What is the minimum number of coins required to get sum $S$?\n\n\n-----Input-----\n\nThe only line of the input contains two integers $n$ and $S$ ($1 \\le n \\le 100\\,000$, $1 \\le S \\le 10^9$)\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the minimum number of coins required to obtain sum $S$.\n\n\n-----Examples-----\nInput\n5 11\n\nOutput\n3\nInput\n6 16\n\nOutput\n3\n\n\n-----Note-----\n\nIn the first example, some of the possible ways to get sum $11$ with $3$ coins are: \n\n $(3, 4, 4)$\n\n $(2, 4, 5)$\n\n $(1, 5, 5)$\n\n $(3, 3, 5)$ \n\nIt is impossible to get sum $11$ with less than $3$ coins.\n\nIn the second example, some of the possible ways to get sum $16$ with $3$ coins are: \n\n $(5, 5, 6)$\n\n $(4, 6, 6)$ \n\nIt is impossible to get sum $16$ with less than $3$ coins."} +{"id": "00367", "text": "A string is called palindrome if it reads the same from left to right and from right to left. For example \"kazak\", \"oo\", \"r\" and \"mikhailrubinchikkihcniburliahkim\" are palindroms, but strings \"abb\" and \"ij\" are not.\n\nYou are given string s consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in s. Then you can permute the order of letters as you want. Permutation doesn't count as changes. \n\nYou should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically.\n\n\n-----Input-----\n\nThe only line contains string s (1 \u2264 |s| \u2264 2\u00b710^5) consisting of only lowercase Latin letters.\n\n\n-----Output-----\n\nPrint the lexicographically smallest palindrome that can be obtained with the minimal number of changes.\n\n\n-----Examples-----\nInput\naabc\n\nOutput\nabba\n\nInput\naabcd\n\nOutput\nabcba"} +{"id": "00368", "text": "A and B are preparing themselves for programming contests.\n\nTo train their logical thinking and solve problems better, A and B decided to play chess. During the game A wondered whose position is now stronger.\n\nFor each chess piece we know its weight: the queen's weight is 9, the rook's weight is 5, the bishop's weight is 3, the knight's weight is 3, the pawn's weight is 1, the king's weight isn't considered in evaluating position. \n\nThe player's weight equals to the sum of weights of all his pieces on the board.\n\nAs A doesn't like counting, he asked you to help him determine which player has the larger position weight.\n\n\n-----Input-----\n\nThe input contains eight lines, eight characters each \u2014 the board's description.\n\nThe white pieces on the board are marked with uppercase letters, the black pieces are marked with lowercase letters.\n\nThe white pieces are denoted as follows: the queen is represented is 'Q', the rook \u2014 as 'R', the bishop \u2014 as'B', the knight \u2014 as 'N', the pawn \u2014 as 'P', the king \u2014 as 'K'.\n\nThe black pieces are denoted as 'q', 'r', 'b', 'n', 'p', 'k', respectively.\n\nAn empty square of the board is marked as '.' (a dot). \n\nIt is not guaranteed that the given chess position can be achieved in a real game. Specifically, there can be an arbitrary (possibly zero) number pieces of each type, the king may be under attack and so on.\n\n\n-----Output-----\n\nPrint \"White\" (without quotes) if the weight of the position of the white pieces is more than the weight of the position of the black pieces, print \"Black\" if the weight of the black pieces is more than the weight of the white pieces and print \"Draw\" if the weights of the white and black pieces are equal.\n\n\n-----Examples-----\nInput\n...QK...\n........\n........\n........\n........\n........\n........\n...rk...\n\nOutput\nWhite\n\nInput\nrnbqkbnr\npppppppp\n........\n........\n........\n........\nPPPPPPPP\nRNBQKBNR\n\nOutput\nDraw\n\nInput\nrppppppr\n...k....\n........\n........\n........\n........\nK...Q...\n........\n\nOutput\nBlack\n\n\n\n-----Note-----\n\nIn the first test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals 5.\n\nIn the second test sample the weights of the positions of the black and the white pieces are equal to 39.\n\nIn the third test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals to 16."} +{"id": "00369", "text": "Takahashi is playing a board game called Sugoroku.\nOn the board, there are N + 1 squares numbered 0 to N. Takahashi starts at Square 0, and he has to stop exactly at Square N to win the game.\nThe game uses a roulette with the M numbers from 1 to M. In each turn, Takahashi spins the roulette. If the number x comes up when he is at Square s, he moves to Square s+x. If this makes him go beyond Square N, he loses the game.\nAdditionally, some of the squares are Game Over Squares. He also loses the game if he stops at one of those squares. You are given a string S of length N + 1, representing which squares are Game Over Squares. For each i (0 \\leq i \\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.\nFind the sequence of numbers coming up in the roulette in which Takahashi can win the game in the fewest number of turns possible. If there are multiple such sequences, find the lexicographically smallest such sequence. If Takahashi cannot win the game, print -1.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^5\n - |S| = N + 1\n - S consists of 0 and 1.\n - S[0] = 0\n - S[N] = 0\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nS\n\n-----Output-----\nIf Takahashi can win the game, print the lexicographically smallest sequence among the shortest sequences of numbers coming up in the roulette in which Takahashi can win the game, with spaces in between.\nIf Takahashi cannot win the game, print -1.\n\n-----Sample Input-----\n9 3\n0001000100\n\n-----Sample Output-----\n1 3 2 3\n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9 via Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and this is the lexicographically smallest sequence in which he reaches Square 9 in four turns."} +{"id": "00370", "text": "Jumbo Takahashi will play golf on an infinite two-dimensional grid.\nThe ball is initially at the origin (0, 0), and the goal is a grid point (a point with integer coordinates) (X, Y). In one stroke, Jumbo Takahashi can perform the following operation:\n - Choose a grid point whose Manhattan distance from the current position of the ball is K, and send the ball to that point.\nThe game is finished when the ball reaches the goal, and the score will be the number of strokes so far. Jumbo Takahashi wants to finish the game with the lowest score possible.\nDetermine if the game can be finished. If the answer is yes, find one way to bring the ball to the goal with the lowest score possible.\nWhat is Manhattan distance?\nThe Manhattan distance between two points (x_1, y_1) and (x_2, y_2) is defined as |x_1-x_2|+|y_1-y_2|.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq K \\leq 10^9\n - -10^5 \\leq X, Y \\leq 10^5\n - (X, Y) \\neq (0, 0)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK\nX Y\n\n-----Output-----\nIf the game cannot be finished, print -1.\nIf the game can be finished, print one way to bring the ball to the destination with the lowest score possible, in the following format:\ns\nx_1 y_1\nx_2 y_2\n.\n.\n.\nx_s y_s\n\nHere, s is the lowest score possible, and (x_i, y_i) is the position of the ball just after the i-th stroke.\n\n-----Sample Input-----\n11\n-1 2\n\n-----Sample Output-----\n3\n7 4\n2 10\n-1 2\n\n - The Manhattan distance between (0, 0) and (7, 4) is |0-7|+|0-4|=11.\n - The Manhattan distance between (7, 4) and (2, 10) is |7-2|+|4-10|=11.\n - The Manhattan distance between (2, 10) and (-1, 2) is |2-(-1)|+|10-2|=11.\nThus, this play is valid.\nAlso, there is no way to finish the game with less than three strokes."} +{"id": "00371", "text": "Bad news came to Mike's village, some thieves stole a bunch of chocolates from the local factory! Horrible! \n\nAside from loving sweet things, thieves from this area are known to be very greedy. So after a thief takes his number of chocolates for himself, the next thief will take exactly k times more than the previous one. The value of k (k > 1) is a secret integer known only to them. It is also known that each thief's bag can carry at most n chocolates (if they intend to take more, the deal is cancelled) and that there were exactly four thieves involved. \n\nSadly, only the thieves know the value of n, but rumours say that the numbers of ways they could have taken the chocolates (for a fixed n, but not fixed k) is m. Two ways are considered different if one of the thieves (they should be numbered in the order they take chocolates) took different number of chocolates in them.\n\nMike want to track the thieves down, so he wants to know what their bags are and value of n will help him in that. Please find the smallest possible value of n or tell him that the rumors are false and there is no such n.\n\n\n-----Input-----\n\nThe single line of input contains the integer m (1 \u2264 m \u2264 10^15)\u00a0\u2014 the number of ways the thieves might steal the chocolates, as rumours say.\n\n\n-----Output-----\n\nPrint the only integer n\u00a0\u2014 the maximum amount of chocolates that thieves' bags can carry. If there are more than one n satisfying the rumors, print the smallest one.\n\nIf there is no such n for a false-rumoured m, print - 1.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n8\n\nInput\n8\n\nOutput\n54\n\nInput\n10\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample case the smallest n that leads to exactly one way of stealing chocolates is n = 8, whereas the amounts of stealed chocolates are (1, 2, 4, 8) (the number of chocolates stolen by each of the thieves).\n\nIn the second sample case the smallest n that leads to exactly 8 ways is n = 54 with the possibilities: (1, 2, 4, 8), \u2002(1, 3, 9, 27), \u2002(2, 4, 8, 16), \u2002(2, 6, 18, 54), \u2002(3, 6, 12, 24), \u2002(4, 8, 16, 32), \u2002(5, 10, 20, 40), \u2002(6, 12, 24, 48).\n\nThere is no n leading to exactly 10 ways of stealing chocolates in the third sample case."} +{"id": "00372", "text": "You are given two circles. Find the area of their intersection.\n\n\n-----Input-----\n\nThe first line contains three integers x_1, y_1, r_1 ( - 10^9 \u2264 x_1, y_1 \u2264 10^9, 1 \u2264 r_1 \u2264 10^9) \u2014 the position of the center and the radius of the first circle.\n\nThe second line contains three integers x_2, y_2, r_2 ( - 10^9 \u2264 x_2, y_2 \u2264 10^9, 1 \u2264 r_2 \u2264 10^9) \u2014 the position of the center and the radius of the second circle.\n\n\n-----Output-----\n\nPrint the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n0 0 4\n6 0 4\n\nOutput\n7.25298806364175601379\n\nInput\n0 0 5\n11 0 5\n\nOutput\n0.00000000000000000000"} +{"id": "00373", "text": "Chouti was doing a competitive programming competition. However, after having all the problems accepted, he got bored and decided to invent some small games.\n\nHe came up with the following game. The player has a positive integer $n$. Initially the value of $n$ equals to $v$ and the player is able to do the following operation as many times as the player want (possibly zero): choose a positive integer $x$ that $x\" (for the first type queries), \"<\" (for the second type queries), \">=\" (for the third type queries), \"<=\" (for the fourth type queries). \n\nAll values of x are integer and meet the inequation - 10^9 \u2264 x \u2264 10^9. The answer is an English letter \"Y\" (for \"yes\") or \"N\" (for \"no\").\n\nConsequtive elements in lines are separated by a single space.\n\n\n-----Output-----\n\nPrint any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2\u00b710^9 \u2264 y \u2264 2\u00b710^9. If there are many answers, print any of them. If such value doesn't exist, print word \"Impossible\" (without the quotes).\n\n\n-----Examples-----\nInput\n4\n>= 1 Y\n< 3 N\n<= -3 N\n> 55 N\n\nOutput\n17\n\nInput\n2\n> 100 Y\n< -100 Y\n\nOutput\nImpossible"} +{"id": "00387", "text": "You are given $a$ uppercase Latin letters 'A' and $b$ letters 'B'.\n\nThe period of the string is the smallest such positive integer $k$ that $s_i = s_{i~mod~k}$ ($0$-indexed) for each $i$. Note that this implies that $k$ won't always divide $a+b = |s|$.\n\nFor example, the period of string \"ABAABAA\" is $3$, the period of \"AAAA\" is $1$, and the period of \"AABBB\" is $5$.\n\nFind the number of different periods over all possible strings with $a$ letters 'A' and $b$ letters 'B'.\n\n\n-----Input-----\n\nThe first line contains two integers $a$ and $b$ ($1 \\le a, b \\le 10^9$) \u2014 the number of letters 'A' and 'B', respectively.\n\n\n-----Output-----\n\nPrint the number of different periods over all possible strings with $a$ letters 'A' and $b$ letters 'B'.\n\n\n-----Examples-----\nInput\n2 4\n\nOutput\n4\n\nInput\n5 3\n\nOutput\n5\n\n\n\n-----Note-----\n\nAll the possible periods for the first example: $3$ \"BBABBA\" $4$ \"BBAABB\" $5$ \"BBBAAB\" $6$ \"AABBBB\" \n\nAll the possible periods for the second example: $3$ \"BAABAABA\" $5$ \"BAABABAA\" $6$ \"BABAAABA\" $7$ \"BAABAAAB\" $8$ \"AAAAABBB\" \n\nNote that these are not the only possible strings for the given periods."} +{"id": "00388", "text": "In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if there are two Bobs?).\n\nA group of soldiers is effective if and only if their names are different. For example, a group (John, Bob, Limak) would be effective, while groups (Gary, Bob, Gary) and (Alice, Alice) wouldn't.\n\nYou are a spy in the enemy's camp. You noticed n soldiers standing in a row, numbered 1 through n. The general wants to choose a group of k consecutive soldiers. For every k consecutive soldiers, the general wrote down whether they would be an effective group or not.\n\nYou managed to steal the general's notes, with n - k + 1 strings s_1, s_2, ..., s_{n} - k + 1, each either \"YES\" or \"NO\". The string s_1 describes a group of soldiers 1 through k (\"YES\" if the group is effective, and \"NO\" otherwise). The string s_2 describes a group of soldiers 2 through k + 1. And so on, till the string s_{n} - k + 1 that describes a group of soldiers n - k + 1 through n. \n\nYour task is to find possible names of n soldiers. Names should match the stolen notes. Each name should be a string that consists of between 1 and 10 English letters, inclusive. The first letter should be uppercase, and all other letters should be lowercase. Names don't have to be existing names\u00a0\u2014 it's allowed to print \"Xyzzzdj\" or \"T\" for example.\n\nFind and print any solution. It can be proved that there always exists at least one solution.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (2 \u2264 k \u2264 n \u2264 50)\u00a0\u2014 the number of soldiers and the size of a group respectively.\n\nThe second line contains n - k + 1 strings s_1, s_2, ..., s_{n} - k + 1. The string s_{i} is \"YES\" if the group of soldiers i through i + k - 1 is effective, and \"NO\" otherwise.\n\n\n-----Output-----\n\nFind any solution satisfying all given conditions. In one line print n space-separated strings, denoting possible names of soldiers in the order. The first letter of each name should be uppercase, while the other letters should be lowercase. Each name should contain English letters only and has length from 1 to 10.\n\nIf there are multiple valid solutions, print any of them.\n\n\n-----Examples-----\nInput\n8 3\nNO NO YES YES YES NO\n\nOutput\nAdam Bob Bob Cpqepqwer Limak Adam Bob Adam\nInput\n9 8\nYES NO\n\nOutput\nR Q Ccccccccc Ccocc Ccc So Strong Samples Ccc\nInput\n3 2\nNO NO\n\nOutput\nNa Na Na\n\n\n-----Note-----\n\nIn the first sample, there are 8 soldiers. For every 3 consecutive ones we know whether they would be an effective group. Let's analyze the provided sample output: First three soldiers (i.e. Adam, Bob, Bob) wouldn't be an effective group because there are two Bobs. Indeed, the string s_1 is \"NO\". Soldiers 2 through 4 (Bob, Bob, Cpqepqwer) wouldn't be effective either, and the string s_2 is \"NO\". Soldiers 3 through 5 (Bob, Cpqepqwer, Limak) would be effective, and the string s_3 is \"YES\". ..., Soldiers 6 through 8 (Adam, Bob, Adam) wouldn't be effective, and the string s_6 is \"NO\"."} +{"id": "00389", "text": "Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That's where the fox comes in and starts the dialog: \"Little bears, wait a little, I want to make your pieces equal\" \"Come off it fox, how are you going to do that?\", the curious bears asked. \"It's easy\", said the fox. \"If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I'll eat a little here and there and make the pieces equal\". \n\nThe little bears realize that the fox's proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers a and b (1 \u2264 a, b \u2264 10^9). \n\n\n-----Output-----\n\nIf the fox is lying to the little bears and it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cheese are initially equal, the required number is 0.\n\n\n-----Examples-----\nInput\n15 20\n\nOutput\n3\n\nInput\n14 8\n\nOutput\n-1\n\nInput\n6 6\n\nOutput\n0"} +{"id": "00390", "text": "A group of $n$ dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them \u2014 a black one, and for the rest the suit will be bought in the future.\n\nOn the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.\n\nThe director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $a$, and $b$ ($1 \\leq n \\leq 20$, $1 \\leq a, b \\leq 100$)\u00a0\u2014 the number of dancers, the cost of a white suit, and the cost of a black suit.\n\nThe next line contains $n$ numbers $c_i$, $i$-th of which denotes the color of the suit of the $i$-th dancer. Number $0$ denotes the white color, $1$\u00a0\u2014 the black color, and $2$ denotes that a suit for this dancer is still to be bought.\n\n\n-----Output-----\n\nIf it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.\n\n\n-----Examples-----\nInput\n5 100 1\n0 1 2 1 2\n\nOutput\n101\n\nInput\n3 10 12\n1 2 0\n\nOutput\n-1\n\nInput\n3 12 1\n0 1 0\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.\n\nIn the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.\n\nIn the third sample, all suits are already bought and their colors form a palindrome."} +{"id": "00391", "text": "You are given a cube of size k \u00d7 k \u00d7 k, which consists of unit cubes. Two unit cubes are considered neighbouring, if they have common face.\n\nYour task is to paint each of k^3 unit cubes one of two colours (black or white), so that the following conditions must be satisfied: each white cube has exactly 2 neighbouring cubes of white color; each black cube has exactly 2 neighbouring cubes of black color. \n\n\n-----Input-----\n\nThe first line contains integer k (1 \u2264 k \u2264 100), which is size of the cube.\n\n\n-----Output-----\n\nPrint -1 if there is no solution. Otherwise, print the required painting of the cube consequently by layers. Print a k \u00d7 k matrix in the first k lines, showing how the first layer of the cube should be painted. In the following k lines print a k \u00d7 k matrix \u2014 the way the second layer should be painted. And so on to the last k-th layer. Note that orientation of the cube in the space does not matter.\n\nMark a white unit cube with symbol \"w\" and a black one with \"b\". Use the format of output data, given in the test samples. You may print extra empty lines, they will be ignored.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n-1\n\nInput\n2\n\nOutput\nbb\nww\n\nbb\nww"} +{"id": "00392", "text": "Duff is in love with lovely numbers! A positive integer x is called lovely if and only if there is no such positive integer a > 1 such that a^2 is a divisor of x. [Image] \n\nMalek has a number store! In his store, he has only divisors of positive integer n (and he has all of them). As a birthday present, Malek wants to give her a lovely number from his store. He wants this number to be as big as possible.\n\nMalek always had issues in math, so he asked for your help. Please tell him what is the biggest lovely number in his store.\n\n\n-----Input-----\n\nThe first and only line of input contains one integer, n (1 \u2264 n \u2264 10^12).\n\n\n-----Output-----\n\nPrint the answer in one line.\n\n\n-----Examples-----\nInput\n10\n\nOutput\n10\n\nInput\n12\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn first sample case, there are numbers 1, 2, 5 and 10 in the shop. 10 isn't divisible by any perfect square, so 10 is lovely.\n\nIn second sample case, there are numbers 1, 2, 3, 4, 6 and 12 in the shop. 12 is divisible by 4 = 2^2, so 12 is not lovely, while 6 is indeed lovely."} +{"id": "00393", "text": "You're given a row with $n$ chairs. We call a seating of people \"maximal\" if the two following conditions hold: There are no neighbors adjacent to anyone seated. It's impossible to seat one more person without violating the first rule. \n\nThe seating is given as a string consisting of zeros and ones ($0$ means that the corresponding seat is empty, $1$ \u2014 occupied). The goal is to determine whether this seating is \"maximal\".\n\nNote that the first and last seats are not adjacent (if $n \\ne 2$).\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 1000$)\u00a0\u2014 the number of chairs.\n\nThe next line contains a string of $n$ characters, each of them is either zero or one, describing the seating.\n\n\n-----Output-----\n\nOutput \"Yes\" (without quotation marks) if the seating is \"maximal\". Otherwise print \"No\".\n\nYou are allowed to print letters in whatever case you'd like (uppercase or lowercase).\n\n\n-----Examples-----\nInput\n3\n101\n\nOutput\nYes\n\nInput\n4\n1011\n\nOutput\nNo\n\nInput\n5\n10001\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn sample case one the given seating is maximal.\n\nIn sample case two the person at chair three has a neighbour to the right.\n\nIn sample case three it is possible to seat yet another person into chair three."} +{"id": "00394", "text": "Bajtek, known for his unusual gifts, recently got an integer array $x_0, x_1, \\ldots, x_{k-1}$.\n\nUnfortunately, after a huge array-party with his extraordinary friends, he realized that he'd lost it. After hours spent on searching for a new toy, Bajtek found on the arrays producer's website another array $a$ of length $n + 1$. As a formal description of $a$ says, $a_0 = 0$ and for all other $i$\u00a0($1 \\le i \\le n$) $a_i = x_{(i-1)\\bmod k} + a_{i-1}$, where $p \\bmod q$ denotes the remainder of division $p$ by $q$.\n\nFor example, if the $x = [1, 2, 3]$ and $n = 5$, then: $a_0 = 0$, $a_1 = x_{0\\bmod 3}+a_0=x_0+0=1$, $a_2 = x_{1\\bmod 3}+a_1=x_1+1=3$, $a_3 = x_{2\\bmod 3}+a_2=x_2+3=6$, $a_4 = x_{3\\bmod 3}+a_3=x_0+6=7$, $a_5 = x_{4\\bmod 3}+a_4=x_1+7=9$. \n\nSo, if the $x = [1, 2, 3]$ and $n = 5$, then $a = [0, 1, 3, 6, 7, 9]$.\n\nNow the boy hopes that he will be able to restore $x$ from $a$! Knowing that $1 \\le k \\le n$, help him and find all possible values of $k$\u00a0\u2014 possible lengths of the lost array.\n\n\n-----Input-----\n\nThe first line contains exactly one integer $n$ ($1 \\le n \\le 1000$)\u00a0\u2014 the length of the array $a$, excluding the element $a_0$.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^6$).\n\nNote that $a_0$ is always $0$ and is not given in the input.\n\n\n-----Output-----\n\nThe first line of the output should contain one integer $l$ denoting the number of correct lengths of the lost array.\n\nThe second line of the output should contain $l$ integers\u00a0\u2014 possible lengths of the lost array in increasing order.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n\nOutput\n5\n1 2 3 4 5 \nInput\n5\n1 3 5 6 8\n\nOutput\n2\n3 5 \nInput\n3\n1 5 3\n\nOutput\n1\n3 \n\n\n-----Note-----\n\nIn the first example, any $k$ is suitable, since $a$ is an arithmetic progression.\n\nPossible arrays $x$: $[1]$ $[1, 1]$ $[1, 1, 1]$ $[1, 1, 1, 1]$ $[1, 1, 1, 1, 1]$\n\nIn the second example, Bajtek's array can have three or five elements.\n\nPossible arrays $x$: $[1, 2, 2]$ $[1, 2, 2, 1, 2]$\n\nFor example, $k = 4$ is bad, since it leads to $6 + x_0 = 8$ and $0 + x_0 = 1$, which is an obvious contradiction.\n\nIn the third example, only $k = n$ is good.\n\nArray $[1, 4, -2]$ satisfies the requirements.\n\nNote that $x_i$ may be negative."} +{"id": "00395", "text": "In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams.\n\nAfter practice competition, participant number i got a score of a_{i}. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question.\n\n\n-----Input-----\n\nThe single line contains six integers a_1, ..., a_6 (0 \u2264 a_{i} \u2264 1000) \u2014 scores of the participants\n\n\n-----Output-----\n\nPrint \"YES\" (quotes for clarity), if it is possible to build teams with equal score, and \"NO\" otherwise.\n\nYou can print each character either upper- or lowercase (\"YeS\" and \"yes\" are valid when the answer is \"YES\").\n\n\n-----Examples-----\nInput\n1 3 2 1 2 1\n\nOutput\nYES\n\nInput\n1 1 1 1 1 99\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample, first team can be composed of 1st, 2nd and 6th participant, second \u2014 of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5.\n\nIn the second sample, score of participant number 6 is too high: his team score will be definitely greater."} +{"id": "00396", "text": "A positive integer is called a 2-3-integer, if it is equal to 2^{x}\u00b73^{y} for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 \u2014 are 2-3 integers, while 5, 10, 21 and 120 are not.\n\nPrint the number of 2-3-integers on the given segment [l, r], i.\u00a0e. the number of sich 2-3-integers t that l \u2264 t \u2264 r.\n\n\n-----Input-----\n\nThe only line contains two integers l and r (1 \u2264 l \u2264 r \u2264 2\u00b710^9).\n\n\n-----Output-----\n\nPrint a single integer the number of 2-3-integers on the segment [l, r].\n\n\n-----Examples-----\nInput\n1 10\n\nOutput\n7\n\nInput\n100 200\n\nOutput\n5\n\nInput\n1 2000000000\n\nOutput\n326\n\n\n\n-----Note-----\n\nIn the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.\n\nIn the second example the 2-3-integers are 108, 128, 144, 162 and 192."} +{"id": "00397", "text": "Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia. \n\nFor the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs $n$ actions. The first action performed is to put a single candy into the box. For each of the remaining moves she can choose from two options:\n\n the first option, in case the box contains at least one candy, is to take exactly one candy out and eat it. This way the number of candies in the box decreased by $1$; the second option is to put candies in the box. In this case, Alya will put $1$ more candy, than she put in the previous time. \n\nThus, if the box is empty, then it can only use the second option.\n\nFor example, one possible sequence of Alya's actions look as follows:\n\n put one candy into the box; put two candies into the box; eat one candy from the box; eat one candy from the box; put three candies into the box; eat one candy from the box; put four candies into the box; eat one candy from the box; put five candies into the box; \n\nThis way she will perform $9$ actions, the number of candies at the end will be $11$, while Alya will eat $4$ candies in total.\n\nYou know the total number of actions $n$ and the number of candies at the end $k$. You need to find the total number of sweets Alya ate. That is the number of moves of the first option. It's guaranteed, that for the given $n$ and $k$ the answer always exists.\n\nPlease note, that during an action of the first option, Alya takes out and eats exactly one candy.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n \\le 10^9$; $0 \\le k \\le 10^9$)\u00a0\u2014 the total number of moves and the number of candies in the box at the end. \n\nIt's guaranteed, that for the given $n$ and $k$ the answer exists.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of candies, which Alya ate. Please note, that in this problem there aren't multiple possible answers\u00a0\u2014 the answer is unique for any input data. \n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n0\nInput\n9 11\n\nOutput\n4\nInput\n5 0\n\nOutput\n3\nInput\n3 2\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first example, Alya has made one move only. According to the statement, the first move is always putting one candy in the box. Hence Alya ate $0$ candies.\n\nIn the second example the possible sequence of Alya's actions looks as follows: put $1$ candy, put $2$ candies, eat a candy, eat a candy, put $3$ candies, eat a candy, put $4$ candies, eat a candy, put $5$ candies. \n\nThis way, she will make exactly $n=9$ actions and in the end the box will contain $1+2-1-1+3-1+4-1+5=11$ candies. The answer is $4$, since she ate $4$ candies in total."} +{"id": "00398", "text": "Mahmoud has n line segments, the i-th of them has length a_{i}. Ehab challenged him to use exactly 3 line segments to form a non-degenerate triangle. Mahmoud doesn't accept challenges unless he is sure he can win, so he asked you to tell him if he should accept the challenge. Given the lengths of the line segments, check if he can choose exactly 3 of them to form a non-degenerate triangle.\n\nMahmoud should use exactly 3 line segments, he can't concatenate two line segments or change any length. A non-degenerate triangle is a triangle with positive area.\n\n\n-----Input-----\n\nThe first line contains single integer n (3 \u2264 n \u2264 10^5)\u00a0\u2014 the number of line segments Mahmoud has.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the lengths of line segments Mahmoud has.\n\n\n-----Output-----\n\nIn the only line print \"YES\" if he can choose exactly three line segments and form a non-degenerate triangle with them, and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n5\n1 5 3 2 4\n\nOutput\nYES\n\nInput\n3\n4 1 2\n\nOutput\nNO\n\n\n\n-----Note-----\n\nFor the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle."} +{"id": "00399", "text": "Imp likes his plush toy a lot.\n\n [Image] \n\nRecently, he found a machine that can clone plush toys. Imp knows that if he applies the machine to an original toy, he additionally gets one more original toy and one copy, and if he applies the machine to a copied toy, he gets two additional copies.\n\nInitially, Imp has only one original toy. He wants to know if it is possible to use machine to get exactly x copied toys and y original toys? He can't throw toys away, and he can't apply the machine to a copy if he doesn't currently have any copies.\n\n\n-----Input-----\n\nThe only line contains two integers x and y (0 \u2264 x, y \u2264 10^9)\u00a0\u2014 the number of copies and the number of original toys Imp wants to get (including the initial one).\n\n\n-----Output-----\n\nPrint \"Yes\", if the desired configuration is possible, and \"No\" otherwise.\n\nYou can print each letter in arbitrary case (upper or lower).\n\n\n-----Examples-----\nInput\n6 3\n\nOutput\nYes\n\nInput\n4 2\n\nOutput\nNo\n\nInput\n1000 1001\n\nOutput\nYes\n\n\n\n-----Note-----\n\nIn the first example, Imp has to apply the machine twice to original toys and then twice to copies."} +{"id": "00400", "text": "Petya loves computer games. Finally a game that he's been waiting for so long came out!\n\nThe main character of this game has n different skills, each of which is characterized by an integer a_{i} from 0 to 100. The higher the number a_{i} is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values \u200b\u200bof $\\lfloor \\frac{a_{i}}{10} \\rfloor$ for all i from 1 to n. The expression \u230a x\u230b denotes the result of rounding the number x down to the nearest integer.\n\nAt the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a_4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.\n\nYour task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.\n\n\n-----Input-----\n\nThe first line of the input contains two positive integers n and k (1 \u2264 n \u2264 10^5, 0 \u2264 k \u2264 10^7) \u2014 the number of skills of the character and the number of units of improvements at Petya's disposal.\n\nThe second line of the input contains a sequence of n integers a_{i} (0 \u2264 a_{i} \u2264 100), where a_{i} characterizes the level of the i-th skill of the character.\n\n\n-----Output-----\n\nThe first line of the output should contain a single non-negative integer \u2014 the maximum total rating of the character that Petya can get using k or less improvement units.\n\n\n-----Examples-----\nInput\n2 4\n7 9\n\nOutput\n2\n\nInput\n3 8\n17 15 19\n\nOutput\n5\n\nInput\n2 2\n99 100\n\nOutput\n20\n\n\n\n-----Note-----\n\nIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor + lfloor frac{100}{10} rfloor = 10 + 10 = 20.\n\nIn the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is $\\lfloor \\frac{20}{10} \\rfloor + \\lfloor \\frac{19}{10} \\rfloor + \\lfloor \\frac{20}{10} \\rfloor = 2 + 1 + 2 = 5$.\n\nIn the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to $\\lfloor \\frac{100}{10} \\rfloor + \\lfloor \\frac{100}{10} \\rfloor = 10 + 10 = 20$."} +{"id": "00401", "text": "You are given two lists of non-zero digits.\n\nLet's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer?\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 9) \u2014 the lengths of the first and the second lists, respectively.\n\nThe second line contains n distinct digits a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 9) \u2014 the elements of the first list.\n\nThe third line contains m distinct digits b_1, b_2, ..., b_{m} (1 \u2264 b_{i} \u2264 9) \u2014 the elements of the second list.\n\n\n-----Output-----\n\nPrint the smallest pretty integer.\n\n\n-----Examples-----\nInput\n2 3\n4 2\n5 7 6\n\nOutput\n25\n\nInput\n8 8\n1 2 3 4 5 6 7 8\n8 7 6 5 4 3 2 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list.\n\nIn the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer."} +{"id": "00402", "text": "Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5\u00b7i minutes to solve the i-th problem.\n\nLimak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first.\n\nHow many problems can Limak solve if he wants to make it to the party?\n\n\n-----Input-----\n\nThe only line of the input contains two integers n and k (1 \u2264 n \u2264 10, 1 \u2264 k \u2264 240)\u00a0\u2014 the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house.\n\n\n-----Output-----\n\nPrint one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier.\n\n\n-----Examples-----\nInput\n3 222\n\nOutput\n2\n\nInput\n4 190\n\nOutput\n4\n\nInput\n7 1\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2.\n\nIn the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight.\n\nIn the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems."} +{"id": "00403", "text": "\u041d\u0430 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0435 \u043a \u0441\u043e\u0440\u0435\u0432\u043d\u043e\u0432\u0430\u043d\u0438\u044f\u043c \u043f\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u043f\u0440\u0438\u0448\u043b\u0438 n \u043a\u043e\u043c\u0430\u043d\u0434. \u0422\u0440\u0435\u043d\u0435\u0440 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043f\u043e\u0434\u043e\u0431\u0440\u0430\u043b \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0443, \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442 \u0437\u0430\u0434\u0430\u0447 \u0434\u043b\u044f i-\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 a_{i} \u0441\u0442\u0440\u0430\u043d\u0438\u0446. \u0412 \u0440\u0430\u0441\u043f\u043e\u0440\u044f\u0436\u0435\u043d\u0438\u0438 \u0442\u0440\u0435\u043d\u0435\u0440\u0430 \u0435\u0441\u0442\u044c x \u043b\u0438\u0441\u0442\u043e\u0432 \u0431\u0443\u043c\u0430\u0433\u0438, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0431\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0447\u0438\u0441\u0442\u044b\u0435, \u0438 y \u043b\u0438\u0441\u0442\u043e\u0432, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0430 \u0447\u0438\u0441\u0442\u0430\u044f. \u041f\u0440\u0438 \u043f\u0435\u0447\u0430\u0442\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043d\u0430 \u043b\u0438\u0441\u0442\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0434\u0432\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u0437 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0437\u0430\u0434\u0430\u0447, \u0430 \u043f\u0440\u0438 \u043f\u0435\u0447\u0430\u0442\u0438 \u043d\u0430 \u043b\u0438\u0441\u0442\u0435 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0442\u0438\u043f\u0430\u00a0\u2014 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0443. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u0430 \u043b\u0438\u0441\u0442\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442\u043e\u0432 \u0437\u0430\u0434\u0430\u0447. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043b\u0438\u0441\u0442\u043e\u0432, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0431\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0447\u0438\u0441\u0442\u044b\u0435, \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043d\u0430 \u043e\u0431\u0435\u0438\u0445 \u0441\u0442\u043e\u0440\u043e\u043d\u0430\u0445, \u043e\u0434\u043d\u0430 \u0438\u0437 \u043d\u0438\u0445 \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f \u0447\u0438\u0441\u0442\u043e\u0439.\n\n\u0412\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043c\u0430\u043d\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0442\u0440\u0435\u043d\u0435\u0440 \u0441\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442\u044b \u0437\u0430\u0434\u0430\u0447 \u0446\u0435\u043b\u0438\u043a\u043e\u043c.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0442\u0440\u0438 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 n, x \u0438 y (1 \u2264 n \u2264 200 000, 0 \u2264 x, y \u2264 10^9)\u00a0\u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043c\u0430\u043d\u0434, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043b\u0438\u0441\u0442\u043e\u0432 \u0431\u0443\u043c\u0430\u0433\u0438 \u0441 \u0434\u0432\u0443\u043c\u044f \u0447\u0438\u0441\u0442\u044b\u043c\u0438 \u0441\u0442\u043e\u0440\u043e\u043d\u0430\u043c\u0438 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043b\u0438\u0441\u0442\u043e\u0432 \u0431\u0443\u043c\u0430\u0433\u0438 \u0441 \u043e\u0434\u043d\u043e\u0439 \u0447\u0438\u0441\u0442\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u043e\u0439.\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438\u0437 n \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10 000), \u0433\u0434\u0435 i-\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0432\u043d\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0432 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442\u0435 \u0437\u0430\u0434\u0430\u0447 \u0434\u043b\u044f i-\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e\u00a0\u2014 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043c\u0430\u043d\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0442\u0440\u0435\u043d\u0435\u0440 \u0441\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442\u044b \u0437\u0430\u0434\u0430\u0447 \u0446\u0435\u043b\u0438\u043a\u043e\u043c.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2 3 5\n4 6\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2 3 5\n4 7\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n6 3 5\n12 11 12 11 12 11\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043e\u0431\u0430 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442\u0430 \u0437\u0430\u0434\u0430\u0447. \u041e\u0434\u0438\u043d \u0438\u0437 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u2014 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0432\u0435\u0441\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442 \u0437\u0430\u0434\u0430\u0447 \u043d\u0430 \u043b\u0438\u0441\u0442\u0430\u0445 \u0441 \u043e\u0434\u043d\u043e\u0439 \u0447\u0438\u0441\u0442\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u043e\u0439 (\u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f 3 \u043b\u0438\u0441\u0442\u0430 \u0441 \u0434\u0432\u0443\u043c\u044f \u0447\u0438\u0441\u0442\u044b\u043c\u0438 \u0441\u0442\u043e\u0440\u043e\u043d\u0430\u043c\u0438 \u0438 1 \u043b\u0438\u0441\u0442 \u0441 \u043e\u0434\u043d\u043e\u0439 \u0447\u0438\u0441\u0442\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u043e\u0439), \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043d\u0430 \u0442\u0440\u0435\u0445 \u043b\u0438\u0441\u0442\u0430\u0445 \u0441 \u0434\u0432\u0443\u043c\u044f \u0447\u0438\u0441\u0442\u044b\u043c\u0438 \u0441\u0442\u043e\u0440\u043e\u043d\u0430\u043c\u0438.\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043e\u0431\u0430 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442\u0430 \u0437\u0430\u0434\u0430\u0447. \u041e\u0434\u0438\u043d \u0438\u0437 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u2014 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442 \u0437\u0430\u0434\u0430\u0447 \u043d\u0430 \u0434\u0432\u0443\u0445 \u043b\u0438\u0441\u0442\u0430\u0445 \u0441 \u0434\u0432\u0443\u043c\u044f \u0447\u0438\u0441\u0442\u044b\u043c\u0438 \u0441\u0442\u043e\u0440\u043e\u043d\u0430\u043c\u0438 (\u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f 1 \u043b\u0438\u0441\u0442 \u0441 \u0434\u0432\u0443\u043c\u044f \u0447\u0438\u0441\u0442\u044b\u043c\u0438 \u0441\u0442\u043e\u0440\u043e\u043d\u0430\u043c\u0438 \u0438 5 \u043b\u0438\u0441\u0442\u043e\u0432 \u0441 \u043e\u0434\u043d\u043e\u0439 \u0447\u0438\u0441\u0442\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u043e\u0439), \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442 \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u043b\u0438\u0441\u0442\u0435 \u0441 \u0434\u0432\u0443\u043c\u044f \u0447\u0438\u0441\u0442\u044b\u043c\u0438 \u0441\u0442\u043e\u0440\u043e\u043d\u0430\u043c\u0438 \u0438 \u043d\u0430 \u043f\u044f\u0442\u0438 \u043b\u0438\u0441\u0442\u0430\u0445 \u0441 \u043e\u0434\u043d\u043e\u0439 \u0447\u0438\u0441\u0442\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u043e\u0439. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0442\u0440\u0435\u043d\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0432\u0441\u0435 \u043b\u0438\u0441\u0442\u044b \u0434\u043b\u044f \u043f\u0435\u0447\u0430\u0442\u0438.\n\n\u0412 \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442 \u0437\u0430\u0434\u0430\u0447 (\u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u0442\u0440\u0451\u0445 11-\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0445). \u0414\u043b\u044f \u043f\u0435\u0447\u0430\u0442\u0438 11-\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0442\u0430 \u0437\u0430\u0434\u0430\u0447 \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u0440\u0430\u0441\u0445\u043e\u0434\u043e\u0432\u0430\u043d\u0430 \u0432\u0441\u044f \u0431\u0443\u043c\u0430\u0433\u0430."} +{"id": "00404", "text": "Ivan has number $b$. He is sorting through the numbers $a$ from $1$ to $10^{18}$, and for every $a$ writes $\\frac{[a, \\,\\, b]}{a}$ on blackboard. Here $[a, \\,\\, b]$ stands for least common multiple of $a$ and $b$. Ivan is very lazy, that's why this task bored him soon. But he is interested in how many different numbers he would write on the board if he would finish the task. Help him to find the quantity of different numbers he would write on the board.\n\n\n-----Input-----\n\nThe only line contains one integer\u00a0\u2014 $b$ $(1 \\le b \\le 10^{10})$.\n\n\n-----Output-----\n\nPrint one number\u00a0\u2014 answer for the problem.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\nInput\n2\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first example $[a, \\,\\, 1] = a$, therefore $\\frac{[a, \\,\\, b]}{a}$ is always equal to $1$.\n\nIn the second example $[a, \\,\\, 2]$ can be equal to $a$ or $2 \\cdot a$ depending on parity of $a$. $\\frac{[a, \\,\\, b]}{a}$ can be equal to $1$ and $2$."} +{"id": "00405", "text": "In a new version of the famous Pinball game, one of the most important parts of the game field is a sequence of n bumpers. The bumpers are numbered with integers from 1 to n from left to right. There are two types of bumpers. They are denoted by the characters '<' and '>'. When the ball hits the bumper at position i it goes one position to the right (to the position i + 1) if the type of this bumper is '>', or one position to the left (to i - 1) if the type of the bumper at position i is '<'. If there is no such position, in other words if i - 1 < 1 or i + 1 > n, the ball falls from the game field.\n\nDepending on the ball's starting position, the ball may eventually fall from the game field or it may stay there forever. You are given a string representing the bumpers' types. Calculate the number of positions such that the ball will eventually fall from the game field if it starts at that position.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the length of the sequence of bumpers. The second line contains the string, which consists of the characters '<' and '>'. The character at the i-th position of this string corresponds to the type of the i-th bumper.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of positions in the sequence such that the ball will eventually fall from the game field if it starts at that position.\n\n\n-----Examples-----\nInput\n4\n<<><\n\nOutput\n2\nInput\n5\n>>>>>\n\nOutput\n5\nInput\n4\n>><<\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first sample, the ball will fall from the field if starts at position 1 or position 2.\n\nIn the second sample, any starting position will result in the ball falling from the field."} +{"id": "00406", "text": "In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length l_{i}.\n\nIlya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.\n\nSticks with lengths a_1, a_2, a_3 and a_4 can make a rectangle if the following properties are observed: a_1 \u2264 a_2 \u2264 a_3 \u2264 a_4 a_1 = a_2 a_3 = a_4 \n\nA rectangle can be made of sticks with lengths of, for example, 3\u00a03\u00a03\u00a03 or 2\u00a02\u00a04\u00a04. A rectangle cannot be made of, for example, sticks 5\u00a05\u00a05\u00a07.\n\nIlya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.\n\nYou have to answer the question \u2014 what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?\n\n\n-----Input-----\n\nThe first line of the input contains a positive integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014\u00a0the number of the available sticks.\n\nThe second line of the input contains n positive integers l_{i} (2 \u2264 l_{i} \u2264 10^6)\u00a0\u2014\u00a0the lengths of the sticks.\n\n\n-----Output-----\n\nThe first line of the output must contain a single non-negative integer\u00a0\u2014\u00a0the maximum total area of the rectangles that Ilya can make from the available sticks.\n\n\n-----Examples-----\nInput\n4\n2 4 4 2\n\nOutput\n8\n\nInput\n4\n2 2 3 5\n\nOutput\n0\n\nInput\n4\n100003 100004 100005 100006\n\nOutput\n10000800015"} +{"id": "00407", "text": "Petya has n positive integers a_1, a_2, ..., a_{n}. \n\nHis friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to 'j' and replaced all digits 0 with one letter, all digits 1 with another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'.\n\nYour task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 1 000) \u2014 the number of Petya's numbers.\n\nEach of the following lines contains non-empty string s_{i} consisting of lowercase Latin letters from 'a' to 'j' \u2014 the Petya's numbers after Vasya's joke. The length of each string does not exceed six characters.\n\n\n-----Output-----\n\nDetermine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests.\n\n\n-----Examples-----\nInput\n3\nab\nde\naj\n\nOutput\n47\n\nInput\n5\nabcdef\nghij\nbdef\naccbd\ng\n\nOutput\n136542\n\nInput\n3\naa\njj\naa\n\nOutput\n44\n\n\n\n-----Note-----\n\nIn the first example, you need to replace the letter 'a' with the digit 1, the letter 'b' with the digit 0, the letter 'd' with the digit 2, the letter 'e' with the digit 3, and the letter 'j' with the digit 4. So after the restoration numbers will look like [10, 23, 14]. The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration.\n\nIn the second example the numbers after the restoration can look like: [120468, 3579, 2468, 10024, 3]. \n\nIn the second example the numbers after the restoration can look like: [11, 22, 11]."} +{"id": "00408", "text": "A and B are preparing themselves for programming contests.\n\nAn important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants.\n\nA believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.\n\nHowever, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.\n\nAs a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible.\n\nThere are n experienced members and m newbies on the training session. Can you calculate what maximum number of teams can be formed?\n\n\n-----Input-----\n\nThe first line contains two integers n and m (0 \u2264 n, m \u2264 5\u00b710^5) \u2014 the number of experienced participants and newbies that are present at the training session. \n\n\n-----Output-----\n\nPrint the maximum number of teams that can be formed.\n\n\n-----Examples-----\nInput\n2 6\n\nOutput\n2\n\nInput\n4 5\n\nOutput\n3\n\n\n\n-----Note-----\n\nLet's represent the experienced players as XP and newbies as NB.\n\nIn the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).\n\nIn the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB)."} +{"id": "00409", "text": "You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings \"AB\" and \"BA\" (the substrings can go in any order).\n\n\n-----Input-----\n\nThe only line of input contains a string s of length between 1 and 10^5 consisting of uppercase Latin letters.\n\n\n-----Output-----\n\nPrint \"YES\" (without the quotes), if string s contains two non-overlapping substrings \"AB\" and \"BA\", and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\nABA\n\nOutput\nNO\n\nInput\nBACFAB\n\nOutput\nYES\n\nInput\nAXBYBXA\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample test, despite the fact that there are substrings \"AB\" and \"BA\", their occurrences overlap, so the answer is \"NO\".\n\nIn the second sample test there are the following occurrences of the substrings: BACFAB.\n\nIn the third sample test there is no substring \"AB\" nor substring \"BA\"."} +{"id": "00410", "text": "There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.\n\nSoon, monsters became hungry and began to eat each other. \n\nOne monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster A eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight a_{i}.\n\nFor example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are: the first monster can't eat the second monster because a_1 = 1 is not greater than a_2 = 2; the second monster can't eat the third monster because a_2 = 2 is not greater than a_3 = 2; the second monster can't eat the fifth monster because they are not neighbors; the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2]. \n\nAfter some time, someone said a good joke and all monsters recovered. At that moment there were k (k \u2264 n) monsters in the queue, the j-th of which had weight b_{j}. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last.\n\nYou are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 500)\u00a0\u2014 the number of monsters in the initial queue.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^6)\u00a0\u2014 the initial weights of the monsters.\n\nThe third line contains single integer k (1 \u2264 k \u2264 n)\u00a0\u2014 the number of monsters in the queue after the joke. \n\nThe fourth line contains k integers b_1, b_2, ..., b_{k} (1 \u2264 b_{j} \u2264 5\u00b710^8)\u00a0\u2014 the weights of the monsters after the joke. \n\nMonsters are listed in the order from the beginning of the queue to the end.\n\n\n-----Output-----\n\nIn case if no actions could lead to the final queue, print \"NO\" (without quotes) in the only line. \n\nOtherwise print \"YES\" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x\u00a0\u2014 the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again. \n\nWhen one monster eats another the queue decreases. If there are several answers, print any of them.\n\n\n-----Examples-----\nInput\n6\n1 2 2 2 1 2\n2\n5 5\n\nOutput\nYES\n2 L\n1 R\n4 L\n3 L\n\nInput\n5\n1 2 3 4 5\n1\n15\n\nOutput\nYES\n5 L\n4 L\n3 L\n2 L\n\nInput\n5\n1 1 1 3 3\n3\n2 1 6\n\nOutput\nNO\n\n\n-----Note-----\n\nIn the first example, initially there were n = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue: the second monster eats the monster to the left (i.e. the first monster), queue becomes [3, 2, 2, 1, 2]; the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5, 2, 1, 2]; the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5, 2, 3]; the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5, 5]. \n\nNote that for each step the output contains numbers of the monsters in their current order in the queue."} +{"id": "00411", "text": "The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign R Rebel spaceships to guard B bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Since she knows how reckless her pilots are, she wants to be sure that any two (straight) paths \u2013 from a base to its assigned spaceship \u2013 do not intersect in the galaxy plane (that is, in 2D), and so there is no risk of collision.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers R, B(1 \u2264 R, B \u2264 10). For 1 \u2264 i \u2264 R, the i + 1-th line contains two space-separated integers x_{i} and y_{i} (|x_{i}|, |y_{i}| \u2264 10000) denoting the coordinates of the i-th Rebel spaceship. The following B lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and that no three points are on the same line.\n\n\n-----Output-----\n\nIf it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote).\n\n\n-----Examples-----\nInput\n3 3\n0 0\n2 0\n3 1\n-2 1\n0 3\n2 2\n\nOutput\nYes\n\nInput\n2 1\n1 0\n2 2\n3 1\n\nOutput\nNo\n\n\n\n-----Note-----\n\nFor the first example, one possible way is to connect the Rebels and bases in order.\n\nFor the second example, there is no perfect matching between Rebels and bases."} +{"id": "00412", "text": "\u041f\u043e\u043b\u0438\u043a\u0430\u0440\u043f \u043c\u0435\u0447\u0442\u0430\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u043e\u043c \u0438 \u0444\u0430\u043d\u0430\u0442\u0435\u0435\u0442 \u043e\u0442 \u0441\u0442\u0435\u043f\u0435\u043d\u0435\u0439 \u0434\u0432\u043e\u0439\u043a\u0438. \u0421\u0440\u0435\u0434\u0438 \u0434\u0432\u0443\u0445 \u0447\u0438\u0441\u0435\u043b \u0435\u043c\u0443 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0442\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0447\u0438\u0441\u043b\u0430 2. \n\n\u041f\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0446\u0435\u043b\u044b\u0445 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b a_1, a_2, ..., a_{n} \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 r\u00a0\u2014 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0447\u0438\u0441\u043b\u0430 2, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u043e \u0438\u0437 \u0447\u0438\u0441\u0435\u043b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0447\u0438\u0441\u0435\u043b a_{i}, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 r.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e n (1 \u2264 n \u2264 100)\u00a0\u2014 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 a.\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430:\n\n r\u00a0\u2014 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0434\u0432\u043e\u0439\u043a\u0438, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u043e \u0438\u0437 \u0447\u0438\u0441\u0435\u043b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 r. \n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n5\n80 7 16 4 48\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n16 3\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n4\n21 5 3 33\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1 4\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0434\u0432\u043e\u0439\u043a\u0438, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u043e \u0447\u0438\u0441\u043b\u043e, \u0440\u0430\u0432\u043d\u0430 16 = 2^4, \u043d\u0430 \u043d\u0435\u0451 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u0430 80, 16 \u0438 48.\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u0441\u0435 \u0447\u0435\u0442\u044b\u0440\u0435 \u0447\u0438\u0441\u043b\u0430 \u043d\u0435\u0447\u0451\u0442\u043d\u044b\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 1 = 2^0. \u042d\u0442\u043e \u0438 \u0431\u0443\u0434\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e \u0434\u0432\u043e\u0439\u043a\u0438 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430."} +{"id": "00413", "text": "Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.\n\nBob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?\n\n\n-----Input-----\n\nThe first and the only line of the input contains two distinct integers n and m (1 \u2264 n, m \u2264 10^4), separated by a space .\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum number of times one needs to push the button required to get the number m out of number n.\n\n\n-----Examples-----\nInput\n4 6\n\nOutput\n2\n\nInput\n10 1\n\nOutput\n9\n\n\n\n-----Note-----\n\nIn the first example you need to push the blue button once, and then push the red button once.\n\nIn the second example, doubling the number is unnecessary, so we need to push the blue button nine times."} +{"id": "00414", "text": "As the name of the task implies, you are asked to do some work with segments and trees.\n\nRecall that a tree is a connected undirected graph such that there is exactly one simple path between every pair of its vertices.\n\nYou are given $n$ segments $[l_1, r_1], [l_2, r_2], \\dots, [l_n, r_n]$, $l_i < r_i$ for every $i$. It is guaranteed that all segments' endpoints are integers, and all endpoints are unique \u2014 there is no pair of segments such that they start in the same point, end in the same point or one starts in the same point the other one ends.\n\nLet's generate a graph with $n$ vertices from these segments. Vertices $v$ and $u$ are connected by an edge if and only if segments $[l_v, r_v]$ and $[l_u, r_u]$ intersect and neither of it lies fully inside the other one.\n\nFor example, pairs $([1, 3], [2, 4])$ and $([5, 10], [3, 7])$ will induce the edges but pairs $([1, 2], [3, 4])$ and $([5, 7], [3, 10])$ will not.\n\nDetermine if the resulting graph is a tree or not.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 5 \\cdot 10^5$) \u2014 the number of segments.\n\nThe $i$-th of the next $n$ lines contain the description of the $i$-th segment \u2014 two integers $l_i$ and $r_i$ ($1 \\le l_i < r_i \\le 2n$).\n\nIt is guaranteed that all segments borders are pairwise distinct. \n\n\n-----Output-----\n\nPrint \"YES\" if the resulting graph is a tree and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n6\n9 12\n2 11\n1 3\n6 10\n5 7\n4 8\n\nOutput\nYES\n\nInput\n5\n1 3\n2 4\n5 9\n6 8\n7 10\n\nOutput\nNO\n\nInput\n5\n5 8\n3 6\n2 9\n7 10\n1 4\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe graph corresponding to the first example:\n\n[Image]\n\nThe graph corresponding to the second example:\n\n[Image]\n\nThe graph corresponding to the third example:\n\n[Image]"} +{"id": "00415", "text": "We get more and more news about DDoS-attacks of popular websites.\n\nArseny is an admin and he thinks that a website is under a DDoS-attack if the total number of requests for a some period of time exceeds $100 \\cdot t$, where $t$ \u2014 the number of seconds in this time segment. \n\nArseny knows statistics on the number of requests per second since the server is booted. He knows the sequence $r_1, r_2, \\dots, r_n$, where $r_i$ \u2014 the number of requests in the $i$-th second after boot. \n\nDetermine the length of the longest continuous period of time, which Arseny considers to be a DDoS-attack. A seeking time period should not go beyond the boundaries of the segment $[1, n]$.\n\n\n-----Input-----\n\nThe first line contains $n$ ($1 \\le n \\le 5000$) \u2014 number of seconds since server has been booted. The second line contains sequence of integers $r_1, r_2, \\dots, r_n$ ($0 \\le r_i \\le 5000$), $r_i$ \u2014 number of requests in the $i$-th second.\n\n\n-----Output-----\n\nPrint the only integer number \u2014 the length of the longest time period which is considered to be a DDoS-attack by Arseny. If it doesn't exist print 0.\n\n\n-----Examples-----\nInput\n5\n100 200 1 1 1\n\nOutput\n3\n\nInput\n5\n1 2 3 4 5\n\nOutput\n0\n\nInput\n2\n101 99\n\nOutput\n1"} +{"id": "00416", "text": "Well, the series which Stepan watched for a very long time, ended. In total, the series had n episodes. For each of them, Stepan remembers either that he definitely has watched it, or that he definitely hasn't watched it, or he is unsure, has he watched this episode or not. \n\nStepan's dissatisfaction is the maximum number of consecutive series that Stepan did not watch.\n\nYour task is to determine according to Stepan's memories if his dissatisfaction could be exactly equal to k.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 100, 0 \u2264 k \u2264 n) \u2014 the number of episodes in the series and the dissatisfaction which should be checked. \n\nThe second line contains the sequence which consists of n symbols \"Y\", \"N\" and \"?\". If the i-th symbol equals \"Y\", Stepan remembers that he has watched the episode number i. If the i-th symbol equals \"N\", Stepan remembers that he hasn't watched the epizode number i. If the i-th symbol equals \"?\", Stepan doesn't exactly remember if he has watched the episode number i or not.\n\n\n-----Output-----\n\nIf Stepan's dissatisfaction can be exactly equal to k, then print \"YES\" (without qoutes). Otherwise print \"NO\" (without qoutes).\n\n\n-----Examples-----\nInput\n5 2\nNYNNY\n\nOutput\nYES\n\nInput\n6 1\n????NN\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first test Stepan remembers about all the episodes whether he has watched them or not. His dissatisfaction is 2, because he hasn't watch two episodes in a row \u2014 the episode number 3 and the episode number 4. The answer is \"YES\", because k = 2.\n\nIn the second test k = 1, Stepan's dissatisfaction is greater than or equal to 2 (because he remembers that he hasn't watch at least two episodes in a row \u2014 number 5 and number 6), even if he has watched the episodes from the first to the fourth, inclusive."} +{"id": "00417", "text": "We have an integer sequence A of length N, where A_1 = X, A_{i+1} = A_i + D (1 \\leq i < N ) holds.\nTakahashi will take some (possibly all or none) of the elements in this sequence, and Aoki will take all of the others.\nLet S and T be the sum of the numbers taken by Takahashi and Aoki, respectively. How many possible values of S - T are there?\n\n-----Constraints-----\n - -10^8 \\leq X, D \\leq 10^8\n - 1 \\leq N \\leq 2 \\times 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN X D\n\n-----Output-----\nPrint the number of possible values of S - T.\n\n-----Sample Input-----\n3 4 2\n\n-----Sample Output-----\n8\n\nA is (4, 6, 8).\nThere are eight ways for (Takahashi, Aoki) to take the elements: ((), (4, 6, 8)), ((4), (6, 8)), ((6), (4, 8)), ((8), (4, 6))), ((4, 6), (8))), ((4, 8), (6))), ((6, 8), (4))), and ((4, 6, 8), ()).\nThe values of S - T in these ways are -18, -10, -6, -2, 2, 6, 10, and 18, respectively, so there are eight possible values of S - T."} +{"id": "00418", "text": "Codeforces user' handle color depends on his rating\u00a0\u2014 it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance.\n\nAnton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it.\n\nAnton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same?\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of participants Anton has outscored in this contest .\n\nThe next n lines describe participants results: the i-th of them consists of a participant handle name_{i} and two integers before_{i} and after_{i} ( - 4000 \u2264 before_{i}, after_{i} \u2264 4000)\u00a0\u2014 participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters \u00ab_\u00bb and \u00ab-\u00bb characters.\n\nIt is guaranteed that all handles are distinct.\n\n\n-----Output-----\n\nPrint \u00abYES\u00bb (quotes for clarity), if Anton has performed good in the contest and \u00abNO\u00bb (quotes for clarity) otherwise.\n\n\n-----Examples-----\nInput\n3\nBurunduk1 2526 2537\nBudAlNik 2084 2214\nsubscriber 2833 2749\n\nOutput\nYES\nInput\n3\nApplejack 2400 2400\nFluttershy 2390 2431\nPinkie_Pie -2500 -2450\n\nOutput\nNO\n\n\n-----Note-----\n\nIn the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest.\n\nIn the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest."} +{"id": "00419", "text": "In the city of Saint Petersburg, a day lasts for $2^{100}$ minutes. From the main station of Saint Petersburg, a train departs after $1$ minute, $4$ minutes, $16$ minutes, and so on; in other words, the train departs at time $4^k$ for each integer $k \\geq 0$. Team BowWow has arrived at the station at the time $s$ and it is trying to count how many trains have they missed; in other words, the number of trains that have departed strictly before time $s$. For example if $s = 20$, then they missed trains which have departed at $1$, $4$ and $16$. As you are the only one who knows the time, help them!\n\nNote that the number $s$ will be given you in a binary representation without leading zeroes.\n\n\n-----Input-----\n\nThe first line contains a single binary number $s$ ($0 \\leq s < 2^{100}$) without leading zeroes.\n\n\n-----Output-----\n\nOutput a single number\u00a0\u2014 the number of trains which have departed strictly before the time $s$.\n\n\n-----Examples-----\nInput\n100000000\n\nOutput\n4\n\nInput\n101\n\nOutput\n2\n\nInput\n10100\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example $100000000_2 = 256_{10}$, missed trains have departed at $1$, $4$, $16$ and $64$.\n\nIn the second example $101_2 = 5_{10}$, trains have departed at $1$ and $4$.\n\nThe third example is explained in the statements."} +{"id": "00420", "text": "Let's assume that we are given a matrix b of size x \u00d7 y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a 2x \u00d7 y matrix c which has the following properties:\n\n the upper half of matrix c (rows with numbers from 1 to x) exactly matches b; the lower half of matrix c (rows with numbers from x + 1 to 2x) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows x and x + 1). \n\nSereja has an n \u00d7 m matrix a. He wants to find such matrix b, that it can be transformed into matrix a, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain?\n\n\n-----Input-----\n\nThe first line contains two integers, n and m (1 \u2264 n, m \u2264 100). Each of the next n lines contains m integers \u2014 the elements of matrix a. The i-th line contains integers a_{i}1, a_{i}2, ..., a_{im} (0 \u2264 a_{ij} \u2264 1) \u2014 the i-th row of the matrix a.\n\n\n-----Output-----\n\nIn the single line, print the answer to the problem \u2014 the minimum number of rows of matrix b.\n\n\n-----Examples-----\nInput\n4 3\n0 0 1\n1 1 0\n1 1 0\n0 0 1\n\nOutput\n2\n\nInput\n3 3\n0 0 0\n0 0 0\n0 0 0\n\nOutput\n3\n\nInput\n8 1\n0\n1\n1\n0\n0\n1\n1\n0\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first test sample the answer is a 2 \u00d7 3 matrix b:\n\n\n\n001\n\n110\n\n\n\nIf we perform a mirroring operation with this matrix, we get the matrix a that is given in the input:\n\n\n\n001\n\n110\n\n110\n\n001"} +{"id": "00421", "text": "A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values \u2014 the start time l_{i} and the finish time r_{i} (l_{i} \u2264 r_{i}).\n\nRestaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?\n\nNo two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.\n\n\n-----Input-----\n\nThe first line contains integer number n (1 \u2264 n \u2264 5\u00b710^5) \u2014 number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 \u2264 l_{i} \u2264 r_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint the maximal number of orders that can be accepted.\n\n\n-----Examples-----\nInput\n2\n7 11\n4 7\n\nOutput\n1\n\nInput\n5\n1 2\n2 3\n3 4\n4 5\n5 6\n\nOutput\n3\n\nInput\n6\n4 8\n1 5\n4 7\n2 5\n1 3\n6 8\n\nOutput\n2"} +{"id": "00422", "text": "Vova has taken his summer practice this year and now he should write a report on how it went.\n\nVova has already drawn all the tables and wrote down all the formulas. Moreover, he has already decided that the report will consist of exactly $n$ pages and the $i$-th page will include $x_i$ tables and $y_i$ formulas. The pages are numbered from $1$ to $n$.\n\nVova fills the pages one after another, he can't go filling page $i + 1$ before finishing page $i$ and he can't skip pages. \n\nHowever, if he draws strictly more than $k$ tables in a row or writes strictly more than $k$ formulas in a row then he will get bored. Vova wants to rearrange tables and formulas in each page in such a way that he doesn't get bored in the process. Vova can't move some table or some formula to another page.\n\nNote that the count doesn't reset on the start of the new page. For example, if the page ends with $3$ tables and the next page starts with $5$ tables, then it's counted as $8$ tables in a row.\n\nHelp Vova to determine if he can rearrange tables and formulas on each page in such a way that there is no more than $k$ tables in a row and no more than $k$ formulas in a row.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n \\le 3 \\cdot 10^5$, $1 \\le k \\le 10^6$).\n\nThe second line contains $n$ integers $x_1, x_2, \\dots, x_n$ ($1 \\le x_i \\le 10^6$) \u2014 the number of tables on the $i$-th page.\n\nThe third line contains $n$ integers $y_1, y_2, \\dots, y_n$ ($1 \\le y_i \\le 10^6$) \u2014 the number of formulas on the $i$-th page.\n\n\n-----Output-----\n\nPrint \"YES\" if Vova can rearrange tables and formulas on each page in such a way that there is no more than $k$ tables in a row and no more than $k$ formulas in a row.\n\nOtherwise print \"NO\".\n\n\n-----Examples-----\nInput\n2 2\n5 5\n2 2\n\nOutput\nYES\n\nInput\n2 2\n5 6\n2 2\n\nOutput\nNO\n\nInput\n4 1\n4 1 10 1\n3 2 10 1\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example the only option to rearrange everything is the following (let table be 'T' and formula be 'F'): page $1$: \"TTFTTFT\" page $2$: \"TFTTFTT\" \n\nThat way all blocks of tables have length $2$.\n\nIn the second example there is no way to fit everything in such a way that there are no more than $2$ tables in a row and $2$ formulas in a row."} +{"id": "00423", "text": "100 years have passed since the last victory of the man versus computer in Go. Technologies made a huge step forward and robots conquered the Earth! It's time for the final fight between human and robot that will decide the faith of the planet.\n\nThe following game was chosen for the fights: initially there is a polynomial P(x) = a_{n}x^{n} + a_{n} - 1x^{n} - 1 + ... + a_1x + a_0, with yet undefined coefficients and the integer k. Players alternate their turns. At each turn, a player pick some index j, such that coefficient a_{j} that stay near x^{j} is not determined yet and sets it to any value (integer or real, positive or negative, 0 is also allowed). Computer moves first. The human will be declared the winner if and only if the resulting polynomial will be divisible by Q(x) = x - k.\n\nPolynomial P(x) is said to be divisible by polynomial Q(x) if there exists a representation P(x) = B(x)Q(x), where B(x) is also some polynomial.\n\nSome moves have been made already and now you wonder, is it true that human can guarantee the victory if he plays optimally?\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 n \u2264 100 000, |k| \u2264 10 000)\u00a0\u2014 the size of the polynomial and the integer k.\n\nThe i-th of the following n + 1 lines contain character '?' if the coefficient near x^{i} - 1 is yet undefined or the integer value a_{i}, if the coefficient is already known ( - 10 000 \u2264 a_{i} \u2264 10 000). Each of integers a_{i} (and even a_{n}) may be equal to 0.\n\nPlease note, that it's not guaranteed that you are given the position of the game where it's computer's turn to move.\n\n\n-----Output-----\n\nPrint \"Yes\" (without quotes) if the human has winning strategy, or \"No\" (without quotes) otherwise.\n\n\n-----Examples-----\nInput\n1 2\n-1\n?\n\nOutput\nYes\n\nInput\n2 100\n-10000\n0\n1\n\nOutput\nYes\nInput\n4 5\n?\n1\n?\n1\n?\n\nOutput\nNo\n\n\n-----Note-----\n\nIn the first sample, computer set a_0 to - 1 on the first move, so if human can set coefficient a_1 to 0.5 and win.\n\nIn the second sample, all coefficients are already set and the resulting polynomial is divisible by x - 100, so the human has won."} +{"id": "00424", "text": "Alice and Bob begin their day with a quick game. They first choose a starting number X_0 \u2265 3 and try to reach one million by the process described below. \n\nAlice goes first and then they take alternating turns. In the i-th turn, the player whose turn it is selects a prime number smaller than the current number, and announces the smallest multiple of this prime number that is not smaller than the current number.\n\nFormally, he or she selects a prime p < X_{i} - 1 and then finds the minimum X_{i} \u2265 X_{i} - 1 such that p divides X_{i}. Note that if the selected prime p already divides X_{i} - 1, then the number does not change.\n\nEve has witnessed the state of the game after two turns. Given X_2, help her determine what is the smallest possible starting number X_0. Note that the players don't necessarily play optimally. You should consider all possible game evolutions.\n\n\n-----Input-----\n\nThe input contains a single integer X_2 (4 \u2264 X_2 \u2264 10^6). It is guaranteed that the integer X_2 is composite, that is, is not prime.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the minimum possible X_0.\n\n\n-----Examples-----\nInput\n14\n\nOutput\n6\n\nInput\n20\n\nOutput\n15\n\nInput\n8192\n\nOutput\n8191\n\n\n\n-----Note-----\n\nIn the first test, the smallest possible starting number is X_0 = 6. One possible course of the game is as follows: Alice picks prime 5 and announces X_1 = 10 Bob picks prime 7 and announces X_2 = 14. \n\nIn the second case, let X_0 = 15. Alice picks prime 2 and announces X_1 = 16 Bob picks prime 5 and announces X_2 = 20."} +{"id": "00425", "text": "Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $p$ (which may be positive, negative, or zero). To combine their tastes, they invented $p$-binary numbers of the form $2^x + p$, where $x$ is a non-negative integer.\n\nFor example, some $-9$-binary (\"minus nine\" binary) numbers are: $-8$ (minus eight), $7$ and $1015$ ($-8=2^0-9$, $7=2^4-9$, $1015=2^{10}-9$).\n\nThe boys now use $p$-binary numbers to represent everything. They now face a problem: given a positive integer $n$, what's the smallest number of $p$-binary numbers (not necessarily distinct) they need to represent $n$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem.\n\nFor example, if $p=0$ we can represent $7$ as $2^0 + 2^1 + 2^2$.\n\nAnd if $p=-9$ we can represent $7$ as one number $(2^4-9)$.\n\nNote that negative $p$-binary numbers are allowed to be in the sum (see the Notes section for an example).\n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $p$ ($1 \\leq n \\leq 10^9$, $-1000 \\leq p \\leq 1000$).\n\n\n-----Output-----\n\nIf it is impossible to represent $n$ as the sum of any number of $p$-binary numbers, print a single integer $-1$. Otherwise, print the smallest possible number of summands.\n\n\n-----Examples-----\nInput\n24 0\n\nOutput\n2\n\nInput\n24 1\n\nOutput\n3\n\nInput\n24 -1\n\nOutput\n4\n\nInput\n4 -7\n\nOutput\n2\n\nInput\n1 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\n$0$-binary numbers are just regular binary powers, thus in the first sample case we can represent $24 = (2^4 + 0) + (2^3 + 0)$.\n\nIn the second sample case, we can represent $24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$.\n\nIn the third sample case, we can represent $24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$. Note that repeated summands are allowed.\n\nIn the fourth sample case, we can represent $4 = (2^4 - 7) + (2^1 - 7)$. Note that the second summand is negative, which is allowed.\n\nIn the fifth sample case, no representation is possible."} +{"id": "00426", "text": "Ania has a large integer $S$. Its decimal representation has length $n$ and doesn't contain any leading zeroes. Ania is allowed to change at most $k$ digits of $S$. She wants to do it in such a way that $S$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\leq n \\leq 200\\,000$, $0 \\leq k \\leq n$) \u2014 the number of digits in the decimal representation of $S$ and the maximum allowed number of changed digits.\n\nThe second line contains the integer $S$. It's guaranteed that $S$ has exactly $n$ digits and doesn't contain any leading zeroes.\n\n\n-----Output-----\n\nOutput the minimal possible value of $S$ which Ania can end with. Note that the resulting integer should also have $n$ digits.\n\n\n-----Examples-----\nInput\n5 3\n51528\n\nOutput\n10028\n\nInput\n3 2\n102\n\nOutput\n100\n\nInput\n1 1\n1\n\nOutput\n0\n\n\n\n-----Note-----\n\nA number has leading zeroes if it consists of at least two digits and its first digit is $0$. For example, numbers $00$, $00069$ and $0101$ have leading zeroes, while $0$, $3000$ and $1010$ don't have leading zeroes."} +{"id": "00427", "text": "You have two friends. You want to present each of them several positive integers. You want to present cnt_1 numbers to the first friend and cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.\n\nIn addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.\n\nYour task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.\n\nA positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.\n\n\n-----Input-----\n\nThe only line contains four positive integers cnt_1, cnt_2, x, y (1 \u2264 cnt_1, cnt_2 < 10^9; cnt_1 + cnt_2 \u2264 10^9; 2 \u2264 x < y \u2264 3\u00b710^4)\u00a0\u2014 the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n3 1 2 3\n\nOutput\n5\n\nInput\n1 3 2 3\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend. \n\nIn the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4."} +{"id": "00428", "text": "Our bear's forest has a checkered field. The checkered field is an n \u00d7 n table, the rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to n from left to right. Let's denote a cell of the field on the intersection of row x and column y by record (x, y). Each cell of the field contains growing raspberry, at that, the cell (x, y) of the field contains x + y raspberry bushes.\n\nThe bear came out to walk across the field. At the beginning of the walk his speed is (dx, dy). Then the bear spends exactly t seconds on the field. Each second the following takes place: Let's suppose that at the current moment the bear is in cell (x, y). First the bear eats the raspberry from all the bushes he has in the current cell. After the bear eats the raspberry from k bushes, he increases each component of his speed by k. In other words, if before eating the k bushes of raspberry his speed was (dx, dy), then after eating the berry his speed equals (dx + k, dy + k). Let's denote the current speed of the bear (dx, dy) (it was increased after the previous step). Then the bear moves from cell (x, y) to cell (((x + dx - 1)\u00a0mod\u00a0n) + 1, ((y + dy - 1)\u00a0mod\u00a0n) + 1). Then one additional raspberry bush grows in each cell of the field. \n\nYou task is to predict the bear's actions. Find the cell he ends up in if he starts from cell (sx, sy). Assume that each bush has infinitely much raspberry and the bear will never eat all of it.\n\n\n-----Input-----\n\nThe first line of the input contains six space-separated integers: n, sx, sy, dx, dy, t (1 \u2264 n \u2264 10^9;\u00a01 \u2264 sx, sy \u2264 n;\u00a0 - 100 \u2264 dx, dy \u2264 100;\u00a00 \u2264 t \u2264 10^18).\n\n\n-----Output-----\n\nPrint two integers \u2014 the coordinates of the cell the bear will end up in after t seconds.\n\n\n-----Examples-----\nInput\n5 1 2 0 1 2\n\nOutput\n3 1\nInput\n1 1 1 -1 -1 2\n\nOutput\n1 1\n\n\n-----Note-----\n\nOperation a\u00a0mod\u00a0b means taking the remainder after dividing a by b. Note that the result of the operation is always non-negative. For example, ( - 1)\u00a0mod\u00a03 = 2.\n\nIn the first sample before the first move the speed vector will equal (3,4) and the bear will get to cell (4,1). Before the second move the speed vector will equal (9,10) and he bear will get to cell (3,1). Don't forget that at the second move, the number of berry bushes increased by 1.\n\nIn the second sample before the first move the speed vector will equal (1,1) and the bear will get to cell (1,1). Before the second move, the speed vector will equal (4,4) and the bear will get to cell (1,1). Don't forget that at the second move, the number of berry bushes increased by 1."} +{"id": "00429", "text": "ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring (contiguous segment of letters) of it of length 26 where each letter of English alphabet appears exactly once. In particular, if the string has length strictly less than 26, no such substring exists and thus it is not nice.\n\nNow, ZS the Coder tells you a word, where some of its letters are missing as he forgot them. He wants to determine if it is possible to fill in the missing letters so that the resulting word is nice. If it is possible, he needs you to find an example of such a word as well. Can you help him?\n\n\n-----Input-----\n\nThe first and only line of the input contains a single string s (1 \u2264 |s| \u2264 50 000), the word that ZS the Coder remembers. Each character of the string is the uppercase letter of English alphabet ('A'-'Z') or is a question mark ('?'), where the question marks denotes the letters that ZS the Coder can't remember.\n\n\n-----Output-----\n\nIf there is no way to replace all the question marks with uppercase letters such that the resulting word is nice, then print - 1 in the only line.\n\nOtherwise, print a string which denotes a possible nice word that ZS the Coder learned. This string should match the string from the input, except for the question marks replaced with uppercase English letters.\n\nIf there are multiple solutions, you may print any of them.\n\n\n-----Examples-----\nInput\nABC??FGHIJK???OPQR?TUVWXY?\n\nOutput\nABCDEFGHIJKLMNOPQRZTUVWXYS\nInput\nWELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO\n\nOutput\n-1\nInput\n??????????????????????????\n\nOutput\nMNBVCXZLKJHGFDSAQPWOEIRUYT\nInput\nAABCDEFGHIJKLMNOPQRSTUVW??M\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first sample case, ABCDEFGHIJKLMNOPQRZTUVWXYS is a valid answer beacuse it contains a substring of length 26 (the whole string in this case) which contains all the letters of the English alphabet exactly once. Note that there are many possible solutions, such as ABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS.\n\nIn the second sample case, there are no missing letters. In addition, the given string does not have a substring of length 26 that contains all the letters of the alphabet, so the answer is - 1.\n\nIn the third sample case, any string of length 26 that contains all letters of the English alphabet fits as an answer."} +{"id": "00430", "text": "Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends.\n\nEach apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to Touma Kazusa must be equal to the total weight of the apples given to Ogiso Setsuna.\n\nBut unfortunately Kitahara Haruki doesn't have a knife right now, so he cannot split any apple into some parts. Please, tell him: is it possible to divide all the apples in a fair way between his friends?\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 100) \u2014 the number of apples. The second line contains n integers w_1, w_2, ..., w_{n} (w_{i} = 100 or w_{i} = 200), where w_{i} is the weight of the i-th apple.\n\n\n-----Output-----\n\nIn a single line print \"YES\" (without the quotes) if it is possible to divide all the apples between his friends. Otherwise print \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n3\n100 200 100\n\nOutput\nYES\n\nInput\n4\n100 100 100 200\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first test sample Kitahara Haruki can give the first and the last apple to Ogiso Setsuna and the middle apple to Touma Kazusa."} +{"id": "00431", "text": "Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.\n\nThe building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.\n\nSagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.\n\nNote that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 15 and 1 \u2264 m \u2264 100) \u2014 the number of floors and the number of rooms in each floor, respectively.\n\nThe next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.\n\nThe first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum total time needed to turn off all the lights.\n\n\n-----Examples-----\nInput\n2 2\n0010\n0100\n\nOutput\n5\n\nInput\n3 4\n001000\n000010\n000010\n\nOutput\n12\n\nInput\n4 3\n01110\n01110\n01110\n01110\n\nOutput\n18\n\n\n\n-----Note-----\n\nIn the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.\n\nIn the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.\n\nIn the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor."} +{"id": "00432", "text": "Medicine faculty of Berland State University has just finished their admission campaign. As usual, about $80\\%$ of applicants are girls and majority of them are going to live in the university dormitory for the next $4$ (hopefully) years.\n\nThe dormitory consists of $n$ rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number $i$ costs $c_i$ burles. Rooms are numbered from $1$ to $n$.\n\nMouse doesn't sit in place all the time, it constantly runs. If it is in room $i$ in second $t$ then it will run to room $a_i$ in second $t + 1$ without visiting any other rooms inbetween ($i = a_i$ means that mouse won't leave room $i$). It's second $0$ in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.\n\nThat would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from $1$ to $n$ at second $0$.\n\nWhat it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?\n\n\n-----Input-----\n\nThe first line contains as single integers $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of rooms in the dormitory.\n\nThe second line contains $n$ integers $c_1, c_2, \\dots, c_n$ ($1 \\le c_i \\le 10^4$) \u2014 $c_i$ is the cost of setting the trap in room number $i$.\n\nThe third line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$) \u2014 $a_i$ is the room the mouse will run to the next second after being in room $i$.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.\n\n\n-----Examples-----\nInput\n5\n1 2 3 2 10\n1 3 4 3 3\n\nOutput\n3\n\nInput\n4\n1 10 2 10\n2 4 2 2\n\nOutput\n10\n\nInput\n7\n1 1 1 1 1 1 1\n2 2 2 3 6 7 6\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example it is enough to set mouse trap in rooms $1$ and $4$. If mouse starts in room $1$ then it gets caught immideately. If mouse starts in any other room then it eventually comes to room $4$.\n\nIn the second example it is enough to set mouse trap in room $2$. If mouse starts in room $2$ then it gets caught immideately. If mouse starts in any other room then it runs to room $2$ in second $1$.\n\nHere are the paths of the mouse from different starts from the third example:\n\n $1 \\rightarrow 2 \\rightarrow 2 \\rightarrow \\dots$; $2 \\rightarrow 2 \\rightarrow \\dots$; $3 \\rightarrow 2 \\rightarrow 2 \\rightarrow \\dots$; $4 \\rightarrow 3 \\rightarrow 2 \\rightarrow 2 \\rightarrow \\dots$; $5 \\rightarrow 6 \\rightarrow 7 \\rightarrow 6 \\rightarrow \\dots$; $6 \\rightarrow 7 \\rightarrow 6 \\rightarrow \\dots$; $7 \\rightarrow 6 \\rightarrow 7 \\rightarrow \\dots$; \n\nSo it's enough to set traps in rooms $2$ and $6$."} +{"id": "00433", "text": "Vasya lives in a round building, whose entrances are numbered sequentially by integers from 1 to n. Entrance n and entrance 1 are adjacent.\n\nToday Vasya got bored and decided to take a walk in the yard. Vasya lives in entrance a and he decided that during his walk he will move around the house b entrances in the direction of increasing numbers (in this order entrance n should be followed by entrance 1). The negative value of b corresponds to moving |b| entrances in the order of decreasing numbers (in this order entrance 1 is followed by entrance n). If b = 0, then Vasya prefers to walk beside his entrance. [Image] Illustration for n = 6, a = 2, b = - 5. \n\nHelp Vasya to determine the number of the entrance, near which he will be at the end of his walk.\n\n\n-----Input-----\n\nThe single line of the input contains three space-separated integers n, a and b (1 \u2264 n \u2264 100, 1 \u2264 a \u2264 n, - 100 \u2264 b \u2264 100)\u00a0\u2014 the number of entrances at Vasya's place, the number of his entrance and the length of his walk, respectively.\n\n\n-----Output-----\n\nPrint a single integer k (1 \u2264 k \u2264 n)\u00a0\u2014 the number of the entrance where Vasya will be at the end of his walk.\n\n\n-----Examples-----\nInput\n6 2 -5\n\nOutput\n3\n\nInput\n5 1 3\n\nOutput\n4\n\nInput\n3 2 7\n\nOutput\n3\n\n\n\n-----Note-----\n\nThe first example is illustrated by the picture in the statements."} +{"id": "00434", "text": "Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions.\n\nPolycarpus believes that if he writes out the population of the capital for several consecutive years in the sequence a_1, a_2, ..., a_{n}, then it is convenient to consider the array as several arithmetic progressions, written one after the other. For example, sequence (8, 6, 4, 2, 1, 4, 7, 10, 2) can be considered as a sequence of three arithmetic progressions (8, 6, 4, 2), (1, 4, 7, 10) and (2), which are written one after another.\n\nUnfortunately, Polycarpus may not have all the data for the n consecutive years (a census of the population doesn't occur every year, after all). For this reason, some values of a_{i} \u200b\u200bmay be unknown. Such values are represented by number -1.\n\nFor a given sequence a = (a_1, a_2, ..., a_{n}), which consists of positive integers and values \u200b\u200b-1, find the minimum number of arithmetic progressions Polycarpus needs to get a. To get a, the progressions need to be written down one after the other. Values \u200b\u200b-1 may correspond to an arbitrary positive integer and the values a_{i} > 0 must be equal to the corresponding elements of sought consecutive record of the progressions.\n\nLet us remind you that a finite sequence c is called an arithmetic progression if the difference c_{i} + 1 - c_{i} of any two consecutive elements in it is constant. By definition, any sequence of length 1 is an arithmetic progression.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 2\u00b710^5) \u2014 the number of elements in the sequence. The second line contains integer values a_1, a_2, ..., a_{n} separated by a space (1 \u2264 a_{i} \u2264 10^9 or a_{i} = - 1).\n\n\n-----Output-----\n\nPrint the minimum number of arithmetic progressions that you need to write one after another to get sequence a. The positions marked as -1 in a can be represented by any positive integers.\n\n\n-----Examples-----\nInput\n9\n8 6 4 2 1 4 7 10 2\n\nOutput\n3\n\nInput\n9\n-1 6 -1 2 -1 4 7 -1 2\n\nOutput\n3\n\nInput\n5\n-1 -1 -1 -1 -1\n\nOutput\n1\n\nInput\n7\n-1 -1 4 5 1 2 3\n\nOutput\n2"} +{"id": "00435", "text": "High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.\n\nVasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 n \u2264 100 000, 0 \u2264 k \u2264 n)\u00a0\u2014 the length of the string and the maximum number of characters to change.\n\nThe second line contains the string, consisting of letters 'a' and 'b' only.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the maximum beauty of the string Vasya can achieve by changing no more than k characters.\n\n\n-----Examples-----\nInput\n4 2\nabba\n\nOutput\n4\n\nInput\n8 1\naabaabaa\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first sample, Vasya can obtain both strings \"aaaa\" and \"bbbb\".\n\nIn the second sample, the optimal answer is obtained with the string \"aaaaabaa\" or with the string \"aabaaaaa\"."} +{"id": "00436", "text": "Alice is the leader of the State Refactoring Party, and she is about to become the prime minister. \n\nThe elections have just taken place. There are $n$ parties, numbered from $1$ to $n$. The $i$-th party has received $a_i$ seats in the parliament.\n\nAlice's party has number $1$. In order to become the prime minister, she needs to build a coalition, consisting of her party and possibly some other parties. There are two conditions she needs to fulfil: The total number of seats of all parties in the coalition must be a strict majority of all the seats, i.e. it must have strictly more than half of the seats. For example, if the parliament has $200$ (or $201$) seats, then the majority is $101$ or more seats. Alice's party must have at least $2$ times more seats than any other party in the coalition. For example, to invite a party with $50$ seats, Alice's party must have at least $100$ seats. \n\nFor example, if $n=4$ and $a=[51, 25, 99, 25]$ (note that Alice'a party has $51$ seats), then the following set $[a_1=51, a_2=25, a_4=25]$ can create a coalition since both conditions will be satisfied. However, the following sets will not create a coalition:\n\n $[a_2=25, a_3=99, a_4=25]$ since Alice's party is not there; $[a_1=51, a_2=25]$ since coalition should have a strict majority; $[a_1=51, a_2=25, a_3=99]$ since Alice's party should have at least $2$ times more seats than any other party in the coalition. \n\nAlice does not have to minimise the number of parties in a coalition. If she wants, she can invite as many parties as she wants (as long as the conditions are satisfied). If Alice's party has enough people to create a coalition on her own, she can invite no parties.\n\nNote that Alice can either invite a party as a whole or not at all. It is not possible to invite only some of the deputies (seats) from another party. In other words, if Alice invites a party, she invites all its deputies.\n\nFind and print any suitable coalition.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\leq n \\leq 100$)\u00a0\u2014 the number of parties.\n\nThe second line contains $n$ space separated integers $a_1, a_2, \\dots, a_n$ ($1 \\leq a_i \\leq 100$)\u00a0\u2014 the number of seats the $i$-th party has.\n\n\n-----Output-----\n\nIf no coalition satisfying both conditions is possible, output a single line with an integer $0$.\n\nOtherwise, suppose there are $k$ ($1 \\leq k \\leq n$) parties in the coalition (Alice does not have to minimise the number of parties in a coalition), and their indices are $c_1, c_2, \\dots, c_k$ ($1 \\leq c_i \\leq n$). Output two lines, first containing the integer $k$, and the second the space-separated indices $c_1, c_2, \\dots, c_k$. \n\nYou may print the parties in any order. Alice's party (number $1$) must be on that list. If there are multiple solutions, you may print any of them.\n\n\n-----Examples-----\nInput\n3\n100 50 50\n\nOutput\n2\n1 2\n\nInput\n3\n80 60 60\n\nOutput\n0\n\nInput\n2\n6 5\n\nOutput\n1\n1\n\nInput\n4\n51 25 99 25\n\nOutput\n3\n1 2 4\n\n\n\n-----Note-----\n\nIn the first example, Alice picks the second party. Note that she can also pick the third party or both of them. However, she cannot become prime minister without any of them, because $100$ is not a strict majority out of $200$.\n\nIn the second example, there is no way of building a majority, as both other parties are too large to become a coalition partner.\n\nIn the third example, Alice already has the majority. \n\nThe fourth example is described in the problem statement."} +{"id": "00437", "text": "3R2 - Standby for Action\n\nOur dear Cafe's owner, JOE Miller, will soon take part in a new game TV-show \"1 vs. $n$\"!\n\nThe game goes in rounds, where in each round the host asks JOE and his opponents a common question. All participants failing to answer are eliminated. The show ends when only JOE remains (we assume that JOE never answers a question wrong!).\n\nFor each question JOE answers, if there are $s$ ($s > 0$) opponents remaining and $t$ ($0 \\le t \\le s$) of them make a mistake on it, JOE receives $\\displaystyle\\frac{t}{s}$ dollars, and consequently there will be $s - t$ opponents left for the next question.\n\nJOE wonders what is the maximum possible reward he can receive in the best possible scenario. Yet he has little time before show starts, so can you help him answering it instead?\n\n\n-----Input-----\n\nThe first and single line contains a single integer $n$ ($1 \\le n \\le 10^5$), denoting the number of JOE's opponents in the show.\n\n\n-----Output-----\n\nPrint a number denoting the maximum prize (in dollars) JOE could have.\n\nYour answer will be considered correct if it's absolute or relative error won't exceed $10^{-4}$. In other words, if your answer is $a$ and the jury answer is $b$, then it must hold that $\\frac{|a - b|}{max(1, b)} \\le 10^{-4}$.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1.000000000000\n\nInput\n2\n\nOutput\n1.500000000000\n\n\n\n-----Note-----\n\nIn the second example, the best scenario would be: one contestant fails at the first question, the other fails at the next one. The total reward will be $\\displaystyle \\frac{1}{2} + \\frac{1}{1} = 1.5$ dollars."} +{"id": "00438", "text": "Santa Claus has n candies, he dreams to give them as gifts to children.\n\nWhat is the maximal number of children for whose he can give candies if Santa Claus want each kid should get distinct positive integer number of candies. Santa Class wants to give all n candies he has.\n\n\n-----Input-----\n\nThe only line contains positive integer number n (1 \u2264 n \u2264 1000) \u2014 number of candies Santa Claus has.\n\n\n-----Output-----\n\nPrint to the first line integer number k \u2014 maximal number of kids which can get candies.\n\nPrint to the second line k distinct integer numbers: number of candies for each of k kid. The sum of k printed numbers should be exactly n.\n\nIf there are many solutions, print any of them.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n2\n2 3\n\nInput\n9\n\nOutput\n3\n3 5 1\n\nInput\n2\n\nOutput\n1\n2"} +{"id": "00439", "text": "The following problem is well-known: given integers n and m, calculate $2^{n} \\operatorname{mod} m$, \n\nwhere 2^{n} = 2\u00b72\u00b7...\u00b72 (n factors), and $x \\operatorname{mod} y$ denotes the remainder of division of x by y.\n\nYou are asked to solve the \"reverse\" problem. Given integers n and m, calculate $m \\operatorname{mod} 2^{n}$. \n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^8).\n\nThe second line contains a single integer m (1 \u2264 m \u2264 10^8).\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the value of $m \\operatorname{mod} 2^{n}$.\n\n\n-----Examples-----\nInput\n4\n42\n\nOutput\n10\n\nInput\n1\n58\n\nOutput\n0\n\nInput\n98765432\n23456789\n\nOutput\n23456789\n\n\n\n-----Note-----\n\nIn the first example, the remainder of division of 42 by 2^4 = 16 is equal to 10.\n\nIn the second example, 58 is divisible by 2^1 = 2 without remainder, and the answer is 0."} +{"id": "00440", "text": "Victor tries to write his own text editor, with word correction included. However, the rules of word correction are really strange.\n\nVictor thinks that if a word contains two consecutive vowels, then it's kinda weird and it needs to be replaced. So the word corrector works in such a way: as long as there are two consecutive vowels in the word, it deletes the first vowel in a word such that there is another vowel right before it. If there are no two consecutive vowels in the word, it is considered to be correct.\n\nYou are given a word s. Can you predict what will it become after correction?\n\nIn this problem letters a, e, i, o, u and y are considered to be vowels.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 100) \u2014 the number of letters in word s before the correction.\n\nThe second line contains a string s consisting of exactly n lowercase Latin letters \u2014 the word before the correction.\n\n\n-----Output-----\n\nOutput the word s after the correction.\n\n\n-----Examples-----\nInput\n5\nweird\n\nOutput\nwerd\n\nInput\n4\nword\n\nOutput\nword\n\nInput\n5\naaeaa\n\nOutput\na\n\n\n\n-----Note-----\n\nExplanations of the examples: There is only one replace: weird $\\rightarrow$ werd; No replace needed since there are no two consecutive vowels; aaeaa $\\rightarrow$ aeaa $\\rightarrow$ aaa $\\rightarrow$ aa $\\rightarrow$ a."} +{"id": "00441", "text": "There are $n$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.\n\nThe university team for the Olympiad consists of $a$ student-programmers and $b$ student-athletes. Determine the largest number of students from all $a+b$ students, which you can put in the railway carriage so that: no student-programmer is sitting next to the student-programmer; and no student-athlete is sitting next to the student-athlete. \n\nIn the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.\n\nConsider that initially occupied seat places are occupied by jury members (who obviously are not students at all).\n\n\n-----Input-----\n\nThe first line contain three integers $n$, $a$ and $b$ ($1 \\le n \\le 2\\cdot10^{5}$, $0 \\le a, b \\le 2\\cdot10^{5}$, $a + b > 0$) \u2014 total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.\n\nThe second line contains a string with length $n$, consisting of characters \".\" and \"*\". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.\n\n\n-----Output-----\n\nPrint the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.\n\n\n-----Examples-----\nInput\n5 1 1\n*...*\n\nOutput\n2\n\nInput\n6 2 3\n*...*.\n\nOutput\n4\n\nInput\n11 3 10\n.*....**.*.\n\nOutput\n7\n\nInput\n3 2 3\n***\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example you can put all student, for example, in the following way: *.AB*\n\nIn the second example you can put four students, for example, in the following way: *BAB*B\n\nIn the third example you can put seven students, for example, in the following way: B*ABAB**A*B\n\nThe letter A means a student-programmer, and the letter B \u2014 student-athlete."} +{"id": "00442", "text": "Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.\n\nHeidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.\n\nThe first hashing function she designed is as follows.\n\nGiven two positive integers $(x, y)$ she defines $H(x,y):=x^2+2xy+x+1$.\n\nNow, Heidi wonders if the function is reversible. That is, given a positive integer $r$, can you find a pair $(x, y)$ (of positive integers) such that $H(x, y) = r$?\n\nIf multiple such pairs exist, output the one with smallest possible $x$. If there is no such pair, output \"NO\".\n\n\n-----Input-----\n\nThe first and only line contains an integer $r$ ($1 \\le r \\le 10^{12}$).\n\n\n-----Output-----\n\nOutput integers $x, y$ such that $H(x,y) = r$ and $x$ is smallest possible, or \"NO\" if no such pair exists.\n\n\n-----Examples-----\nInput\n19\n\nOutput\n1 8\n\nInput\n16\n\nOutput\nNO"} +{"id": "00443", "text": "There are quite a lot of ways to have fun with inflatable balloons. For example, you can fill them with water and see what happens.\n\nGrigory and Andrew have the same opinion. So, once upon a time, they went to the shop and bought $n$ packets with inflatable balloons, where $i$-th of them has exactly $a_i$ balloons inside.\n\nThey want to divide the balloons among themselves. In addition, there are several conditions to hold:\n\n Do not rip the packets (both Grigory and Andrew should get unbroken packets); Distribute all packets (every packet should be given to someone); Give both Grigory and Andrew at least one packet; To provide more fun, the total number of balloons in Grigory's packets should not be equal to the total number of balloons in Andrew's packets. \n\nHelp them to divide the balloons or determine that it's impossible under these conditions.\n\n\n-----Input-----\n\nThe first line of input contains a single integer $n$ ($1 \\le n \\le 10$)\u00a0\u2014 the number of packets with balloons.\n\nThe second line contains $n$ integers: $a_1$, $a_2$, $\\ldots$, $a_n$ ($1 \\le a_i \\le 1000$)\u00a0\u2014 the number of balloons inside the corresponding packet.\n\n\n-----Output-----\n\nIf it's impossible to divide the balloons satisfying the conditions above, print $-1$.\n\nOtherwise, print an integer $k$\u00a0\u2014 the number of packets to give to Grigory followed by $k$ distinct integers from $1$ to $n$\u00a0\u2014 the indices of those. The order of packets doesn't matter.\n\nIf there are multiple ways to divide balloons, output any of them.\n\n\n-----Examples-----\nInput\n3\n1 2 1\n\nOutput\n2\n1 2\n\nInput\n2\n5 5\n\nOutput\n-1\n\nInput\n1\n10\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first test Grigory gets $3$ balloons in total while Andrey gets $1$.\n\nIn the second test there's only one way to divide the packets which leads to equal numbers of balloons.\n\nIn the third test one of the boys won't get a packet at all."} +{"id": "00444", "text": "Vasya has his favourite number $n$. He wants to split it to some non-zero digits. It means, that he wants to choose some digits $d_1, d_2, \\ldots, d_k$, such that $1 \\leq d_i \\leq 9$ for all $i$ and $d_1 + d_2 + \\ldots + d_k = n$.\n\nVasya likes beauty in everything, so he wants to find any solution with the minimal possible number of different digits among $d_1, d_2, \\ldots, d_k$. Help him!\n\n\n-----Input-----\n\nThe first line contains a single integer $n$\u00a0\u2014 the number that Vasya wants to split ($1 \\leq n \\leq 1000$).\n\n\n-----Output-----\n\nIn the first line print one integer $k$\u00a0\u2014 the number of digits in the partition. Note that $k$ must satisfy the inequality $1 \\leq k \\leq n$. In the next line print $k$ digits $d_1, d_2, \\ldots, d_k$ separated by spaces. All digits must satisfy the inequalities $1 \\leq d_i \\leq 9$.\n\nYou should find a partition of $n$ in which the number of different digits among $d_1, d_2, \\ldots, d_k$ will be minimal possible among all partitions of $n$ into non-zero digits. Among such partitions, it is allowed to find any. It is guaranteed that there exists at least one partition of the number $n$ into digits.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n1 \nInput\n4\n\nOutput\n2\n2 2\n\nInput\n27\n\nOutput\n3\n9 9 9\n\n\n\n-----Note-----\n\nIn the first test, the number $1$ can be divided into $1$ digit equal to $1$.\n\nIn the second test, there are $3$ partitions of the number $4$ into digits in which the number of different digits is $1$. This partitions are $[1, 1, 1, 1]$, $[2, 2]$ and $[4]$. Any of these partitions can be found. And, for example, dividing the number $4$ to the digits $[1, 1, 2]$ isn't an answer, because it has $2$ different digits, that isn't the minimum possible number."} +{"id": "00445", "text": "A tuple of positive integers {x_1, x_2, ..., x_{k}} is called simple if for all pairs of positive integers (i, j) (1 \u2264 i < j \u2264 k), x_{i} + x_{j} is a prime.\n\nYou are given an array a with n positive integers a_1, a_2, ..., a_{n} (not necessary distinct). You want to find a simple subset of the array a with the maximum size.\n\nA prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.\n\nLet's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 1000) \u2014 the number of integers in the array a.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^6) \u2014 the elements of the array a.\n\n\n-----Output-----\n\nOn the first line print integer m \u2014 the maximum possible size of simple subset of a.\n\nOn the second line print m integers b_{l} \u2014 the elements of the simple subset of the array a with the maximum size.\n\nIf there is more than one solution you can print any of them. You can print the elements of the subset in any order.\n\n\n-----Examples-----\nInput\n2\n2 3\n\nOutput\n2\n3 2\n\nInput\n2\n2 2\n\nOutput\n1\n2\n\nInput\n3\n2 1 1\n\nOutput\n3\n1 1 2\n\nInput\n2\n83 14\n\nOutput\n2\n14 83"} +{"id": "00446", "text": "Recently Luba learned about a special kind of numbers that she calls beautiful numbers. The number is called beautiful iff its binary representation consists of k + 1 consecutive ones, and then k consecutive zeroes.\n\nSome examples of beautiful numbers: 1_2 (1_10); 110_2 (6_10); 1111000_2 (120_10); 111110000_2 (496_10). \n\nMore formally, the number is beautiful iff there exists some positive integer k such that the number is equal to (2^{k} - 1) * (2^{k} - 1).\n\nLuba has got an integer number n, and she wants to find its greatest beautiful divisor. Help her to find it!\n\n\n-----Input-----\n\nThe only line of input contains one number n (1 \u2264 n \u2264 10^5) \u2014 the number Luba has got.\n\n\n-----Output-----\n\nOutput one number \u2014 the greatest beautiful divisor of Luba's number. It is obvious that the answer always exists.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n1\n\nInput\n992\n\nOutput\n496"} +{"id": "00447", "text": "Consider the decimal presentation of an integer. Let's call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else.\n\nFor example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is 0-magic, 123 is 2-magic, 34 is 4-magic and 71 is 1-magic.\n\nFind the number of d-magic numbers in the segment [a, b] that are multiple of m. Because the answer can be very huge you should only find its value modulo 10^9 + 7 (so you should find the remainder after dividing by 10^9 + 7).\n\n\n-----Input-----\n\nThe first line contains two integers m, d (1 \u2264 m \u2264 2000, 0 \u2264 d \u2264 9) \u2014 the parameters from the problem statement.\n\nThe second line contains positive integer a in decimal presentation (without leading zeroes).\n\nThe third line contains positive integer b in decimal presentation (without leading zeroes).\n\nIt is guaranteed that a \u2264 b, the number of digits in a and b are the same and don't exceed 2000.\n\n\n-----Output-----\n\nPrint the only integer a \u2014 the remainder after dividing by 10^9 + 7 of the number of d-magic numbers in segment [a, b] that are multiple of m.\n\n\n-----Examples-----\nInput\n2 6\n10\n99\n\nOutput\n8\n\nInput\n2 0\n1\n9\n\nOutput\n4\n\nInput\n19 7\n1000\n9999\n\nOutput\n6\n\n\n\n-----Note-----\n\nThe numbers from the answer of the first example are 16, 26, 36, 46, 56, 76, 86 and 96.\n\nThe numbers from the answer of the second example are 2, 4, 6 and 8.\n\nThe numbers from the answer of the third example are 1767, 2717, 5757, 6707, 8797 and 9747."} +{"id": "00448", "text": "There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to n. The i-th child wants to get at least a_{i} candies.\n\nJzzhu asks children to line up. Initially, the i-th child stands at the i-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm:\n\n Give m candies to the first child of the line. If this child still haven't got enough candies, then the child goes to the end of the line, else the child go home. Repeat the first two steps while the line is not empty. \n\nConsider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order?\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n \u2264 100;\u00a01 \u2264 m \u2264 100). The second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100).\n\n\n-----Output-----\n\nOutput a single integer, representing the number of the last child.\n\n\n-----Examples-----\nInput\n5 2\n1 3 1 4 2\n\nOutput\n4\n\nInput\n6 4\n1 1 2 2 3 3\n\nOutput\n6\n\n\n\n-----Note-----\n\nLet's consider the first sample. \n\nFirstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line). Then child 3 gets 2 candies and go home, and then child 4 gets 2 candies and goes to the end of the line. Currently the line looks like [5, 2, 4]. Then child 5 gets 2 candies and goes home. Then child 2 gets two candies and goes home, and finally child 4 gets 2 candies and goes home.\n\nChild 4 is the last one who goes home."} +{"id": "00449", "text": "Allen has a LOT of money. He has $n$ dollars in the bank. For security reasons, he wants to withdraw it in cash (we will not disclose the reasons here). The denominations for dollar bills are $1$, $5$, $10$, $20$, $100$. What is the minimum number of bills Allen could receive after withdrawing his entire balance?\n\n\n-----Input-----\n\nThe first and only line of input contains a single integer $n$ ($1 \\le n \\le 10^9$).\n\n\n-----Output-----\n\nOutput the minimum number of bills that Allen could receive.\n\n\n-----Examples-----\nInput\n125\n\nOutput\n3\n\nInput\n43\n\nOutput\n5\n\nInput\n1000000000\n\nOutput\n10000000\n\n\n\n-----Note-----\n\nIn the first sample case, Allen can withdraw this with a $100$ dollar bill, a $20$ dollar bill, and a $5$ dollar bill. There is no way for Allen to receive $125$ dollars in one or two bills.\n\nIn the second sample case, Allen can withdraw two $20$ dollar bills and three $1$ dollar bills.\n\nIn the third sample case, Allen can withdraw $100000000$ (ten million!) $100$ dollar bills."} +{"id": "00450", "text": "Permutation p is an ordered set of integers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as p_{i}. We'll call number n the size or the length of permutation p_1, p_2, ..., p_{n}.\n\nWe'll call position i (1 \u2264 i \u2264 n) in permutation p_1, p_2, ..., p_{n} good, if |p[i] - i| = 1. Count the number of permutations of size n with exactly k good positions. Print the answer modulo 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe single line contains two space-separated integers n and k (1 \u2264 n \u2264 1000, 0 \u2264 k \u2264 n).\n\n\n-----Output-----\n\nPrint the number of permutations of length n with exactly k good positions modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n1 0\n\nOutput\n1\n\nInput\n2 1\n\nOutput\n0\n\nInput\n3 2\n\nOutput\n4\n\nInput\n4 1\n\nOutput\n6\n\nInput\n7 4\n\nOutput\n328\n\n\n\n-----Note-----\n\nThe only permutation of size 1 has 0 good positions.\n\nPermutation (1, 2) has 0 good positions, and permutation (2, 1) has 2 positions.\n\nPermutations of size 3:\n\n\n\n (1, 2, 3) \u2014 0 positions\n\n $(1,3,2)$ \u2014 2 positions\n\n $(2,1,3)$ \u2014 2 positions\n\n $(2,3,1)$ \u2014 2 positions\n\n $(3,1,2)$ \u2014 2 positions\n\n (3, 2, 1) \u2014 0 positions"} +{"id": "00451", "text": "Arkady decided to buy roses for his girlfriend.\n\nA flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. \n\nArkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is b_{i}, and its color is c_{i} ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). \n\nCompute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 k \u2264 n \u2264 200 000) \u2014 the number of roses in the show and the number of roses Arkady wants to buy.\n\nThe second line contains a sequence of integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 10 000), where b_{i} equals the beauty of the i-th rose.\n\nThe third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where c_{i} denotes the color of the i-th rose: 'W' denotes white, 'O' \u00a0\u2014 orange, 'R' \u2014 red.\n\n\n-----Output-----\n\nPrint the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1.\n\n\n-----Examples-----\nInput\n5 3\n4 3 4 1 6\nRROWW\n\nOutput\n11\n\nInput\n5 2\n10 20 14 20 11\nRRRRR\n\nOutput\n-1\n\nInput\n11 5\n5 6 3 2 3 4 7 5 4 5 6\nRWOORWORROW\n\nOutput\n28\n\n\n\n-----Note-----\n\nIn the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. \n\nIn the second example Arkady can not buy a bouquet because all roses have the same color."} +{"id": "00452", "text": "A continued fraction of height n is a fraction of form $a_{1} + \\frac{1}{a_{2} + \\frac{1}{\\ldots + \\frac{1}{a_{n}}}}$. You are given two rational numbers, one is represented as [Image] and the other one is represented as a finite fraction of height n. Check if they are equal.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers p, q (1 \u2264 q \u2264 p \u2264 10^18) \u2014 the numerator and the denominator of the first fraction.\n\nThe second line contains integer n (1 \u2264 n \u2264 90) \u2014 the height of the second fraction. The third line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^18) \u2014 the continued fraction.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nPrint \"YES\" if these fractions are equal and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n9 4\n2\n2 4\n\nOutput\nYES\n\nInput\n9 4\n3\n2 3 1\n\nOutput\nYES\n\nInput\n9 4\n3\n1 2 4\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample $2 + \\frac{1}{4} = \\frac{9}{4}$.\n\nIn the second sample $2 + \\frac{1}{3 + \\frac{1}{1}} = 2 + \\frac{1}{4} = \\frac{9}{4}$.\n\nIn the third sample $1 + \\frac{1}{2 + \\frac{1}{4}} = \\frac{13}{9}$."} +{"id": "00453", "text": "When new students come to the Specialized Educational and Scientific Centre (SESC) they need to start many things from the beginning. Sometimes the teachers say (not always unfairly) that we cannot even count. So our teachers decided to teach us arithmetics from the start. And what is the best way to teach students add and subtract? \u2014 That's right, using counting sticks! An here's our new task: \n\nAn expression of counting sticks is an expression of type:[ A sticks][sign +][B sticks][sign =][C sticks] (1 \u2264 A, B, C). \n\nSign + consists of two crossed sticks: one vertical and one horizontal. Sign = consists of two horizontal sticks. The expression is arithmetically correct if A + B = C.\n\nWe've got an expression that looks like A + B = C given by counting sticks. Our task is to shift at most one stick (or we can shift nothing) so that the expression became arithmetically correct. Note that we cannot remove the sticks from the expression, also we cannot shift the sticks from the signs + and =.\n\nWe really aren't fabulous at arithmetics. Can you help us?\n\n\n-----Input-----\n\nThe single line contains the initial expression. It is guaranteed that the expression looks like A + B = C, where 1 \u2264 A, B, C \u2264 100.\n\n\n-----Output-----\n\nIf there isn't a way to shift the stick so the expression becomes correct, print on a single line \"Impossible\" (without the quotes). If there is a way, print the resulting expression. Follow the format of the output from the test samples. Don't print extra space characters.\n\nIf there are multiple correct answers, print any of them. For clarifications, you are recommended to see the test samples.\n\n\n-----Examples-----\nInput\n||+|=|||||\n\nOutput\n|||+|=||||\n\nInput\n|||||+||=||\n\nOutput\nImpossible\n\nInput\n|+|=||||||\n\nOutput\nImpossible\n\nInput\n||||+||=||||||\n\nOutput\n||||+||=||||||\n\n\n\n-----Note-----\n\nIn the first sample we can shift stick from the third group of sticks to the first one.\n\nIn the second sample we cannot shift vertical stick from + sign to the second group of sticks. So we cannot make a - sign.\n\nThere is no answer in the third sample because we cannot remove sticks from the expression.\n\nIn the forth sample the initial expression is already arithmetically correct and that is why we don't have to shift sticks."} +{"id": "00454", "text": "Let us define the oddness of a permutation p = {p_1,\\ p_2,\\ ...,\\ p_n} of {1,\\ 2,\\ ...,\\ n} as \\sum_{i = 1}^n |i - p_i|.\nFind the number of permutations of {1,\\ 2,\\ ...,\\ n} of oddness k, modulo 10^9+7.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq n \\leq 50\n - 0 \\leq k \\leq n^2\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn k\n\n-----Output-----\nPrint the number of permutations of {1,\\ 2,\\ ...,\\ n} of oddness k, modulo 10^9+7.\n\n-----Sample Input-----\n3 2\n\n-----Sample Output-----\n2\n\nThere are six permutations of {1,\\ 2,\\ 3}. Among them, two have oddness of 2: {2,\\ 1,\\ 3} and {1,\\ 3,\\ 2}."} +{"id": "00455", "text": "Snuke is introducing a robot arm with the following properties to his factory:\n - The robot arm consists of m sections and m+1 joints. The sections are numbered 1, 2, ..., m, and the joints are numbered 0, 1, ..., m. Section i connects Joint i-1 and Joint i. The length of Section i is d_i.\n - For each section, its mode can be specified individually. There are four modes: L, R, D and U. The mode of a section decides the direction of that section. If we consider the factory as a coordinate plane, the position of Joint i will be determined as follows (we denote its coordinates as (x_i, y_i)):\n - (x_0, y_0) = (0, 0).\n - If the mode of Section i is L, (x_{i}, y_{i}) = (x_{i-1} - d_{i}, y_{i-1}).\n - If the mode of Section i is R, (x_{i}, y_{i}) = (x_{i-1} + d_{i}, y_{i-1}).\n - If the mode of Section i is D, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} - d_{i}).\n - If the mode of Section i is U, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} + d_{i}).\nSnuke would like to introduce a robot arm so that the position of Joint m can be matched with all of the N points (X_1, Y_1), (X_2, Y_2), ..., (X_N, Y_N) by properly specifying the modes of the sections.\nIs this possible?\nIf so, find such a robot arm and how to bring Joint m to each point (X_j, Y_j).\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 1000\n - -10^9 \\leq X_i \\leq 10^9\n - -10^9 \\leq Y_i \\leq 10^9\n\n-----Partial Score-----\n - In the test cases worth 300 points, -10 \\leq X_i \\leq 10 and -10 \\leq Y_i \\leq 10 hold.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nX_1 Y_1\nX_2 Y_2\n:\nX_N Y_N\n\n-----Output-----\nIf the condition can be satisfied, follow the following format. If the condition cannot be satisfied, print -1.\nm\nd_1 d_2 ... d_m\nw_1\nw_2\n:\nw_N\n\nm and d_i are the configurations of the robot arm. Refer to the problem statement for what each of them means.\nHere, 1 \\leq m \\leq 40 and 1 \\leq d_i \\leq 10^{12} must hold. Also, m and d_i must all be integers.\nw_j is a string of length m that represents the way to bring Joint m of the robot arm to point (X_j, Y_j).\nThe i-th character of w_j should be one of the letters L, R, D and U, representing the mode of Section i.\n\n-----Sample Input-----\n3\n-1 0\n0 3\n2 -1\n\n-----Sample Output-----\n2\n1 2\nRL\nUU\nDR\n\nIn the given way to bring Joint m of the robot arm to each (X_j, Y_j), the positions of the joints will be as follows:\n - To (X_1, Y_1) = (-1, 0): First, the position of Joint 0 is (x_0, y_0) = (0, 0). As the mode of Section 1 is R, the position of Joint 1 is (x_1, y_1) = (1, 0). Then, as the mode of Section 2 is L, the position of Joint 2 is (x_2, y_2) = (-1, 0).\n - To (X_2, Y_2) = (0, 3): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, 1), (x_2, y_2) = (0, 3).\n - To (X_3, Y_3) = (2, -1): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, -1), (x_2, y_2) = (2, -1)."} +{"id": "00456", "text": "Polycarp has interviewed Oleg and has written the interview down without punctuation marks and spaces to save time. Thus, the interview is now a string s consisting of n lowercase English letters.\n\nThere is a filler word ogo in Oleg's speech. All words that can be obtained from ogo by adding go several times to the end of it are also considered to be fillers. For example, the words ogo, ogogo, ogogogo are fillers, but the words go, og, ogog, ogogog and oggo are not fillers.\n\nThe fillers have maximal size, for example, for ogogoo speech we can't consider ogo a filler and goo as a normal phrase. We should consider ogogo as a filler here.\n\nTo print the interview, Polycarp has to replace each of the fillers with three asterisks. Note that a filler word is replaced with exactly three asterisks regardless of its length.\n\nPolycarp has dealt with this problem in no time. Can you do the same? The clock is ticking!\n\n\n-----Input-----\n\nThe first line contains a positive integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the length of the interview.\n\nThe second line contains the string s of length n, consisting of lowercase English letters.\n\n\n-----Output-----\n\nPrint the interview text after the replacement of each of the fillers with \"***\". It is allowed for the substring \"***\" to have several consecutive occurences.\n\n\n-----Examples-----\nInput\n7\naogogob\n\nOutput\na***b\n\nInput\n13\nogogmgogogogo\n\nOutput\n***gmg***\n\nInput\n9\nogoogoogo\n\nOutput\n*********\n\n\n\n-----Note-----\n\nThe first sample contains one filler word ogogo, so the interview for printing is \"a***b\".\n\nThe second sample contains two fillers ogo and ogogogo. Thus, the interview is transformed to \"***gmg***\"."} +{"id": "00457", "text": "Let's introduce some definitions that will be needed later.\n\nLet $prime(x)$ be the set of prime divisors of $x$. For example, $prime(140) = \\{ 2, 5, 7 \\}$, $prime(169) = \\{ 13 \\}$.\n\nLet $g(x, p)$ be the maximum possible integer $p^k$ where $k$ is an integer such that $x$ is divisible by $p^k$. For example: $g(45, 3) = 9$ ($45$ is divisible by $3^2=9$ but not divisible by $3^3=27$), $g(63, 7) = 7$ ($63$ is divisible by $7^1=7$ but not divisible by $7^2=49$). \n\nLet $f(x, y)$ be the product of $g(y, p)$ for all $p$ in $prime(x)$. For example: $f(30, 70) = g(70, 2) \\cdot g(70, 3) \\cdot g(70, 5) = 2^1 \\cdot 3^0 \\cdot 5^1 = 10$, $f(525, 63) = g(63, 3) \\cdot g(63, 5) \\cdot g(63, 7) = 3^2 \\cdot 5^0 \\cdot 7^1 = 63$. \n\nYou have integers $x$ and $n$. Calculate $f(x, 1) \\cdot f(x, 2) \\cdot \\ldots \\cdot f(x, n) \\bmod{(10^{9} + 7)}$.\n\n\n-----Input-----\n\nThe only line contains integers $x$ and $n$ ($2 \\le x \\le 10^{9}$, $1 \\le n \\le 10^{18}$)\u00a0\u2014 the numbers used in formula.\n\n\n-----Output-----\n\nPrint the answer.\n\n\n-----Examples-----\nInput\n10 2\n\nOutput\n2\n\nInput\n20190929 1605\n\nOutput\n363165664\n\nInput\n947 987654321987654321\n\nOutput\n593574252\n\n\n\n-----Note-----\n\nIn the first example, $f(10, 1) = g(1, 2) \\cdot g(1, 5) = 1$, $f(10, 2) = g(2, 2) \\cdot g(2, 5) = 2$.\n\nIn the second example, actual value of formula is approximately $1.597 \\cdot 10^{171}$. Make sure you print the answer modulo $(10^{9} + 7)$.\n\nIn the third example, be careful about overflow issue."} +{"id": "00458", "text": "Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. \n\nFind all integer solutions x (0 < x < 10^9) of the equation:x = b\u00b7s(x)^{a} + c, \n\nwhere a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x.\n\nThe teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers: a, b, c (1 \u2264 a \u2264 5;\u00a01 \u2264 b \u2264 10000;\u00a0 - 10000 \u2264 c \u2264 10000).\n\n\n-----Output-----\n\nPrint integer n \u2014 the number of the solutions that you've found. Next print n integers in the increasing order \u2014 the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9.\n\n\n-----Examples-----\nInput\n3 2 8\n\nOutput\n3\n10 2008 13726 \nInput\n1 2 -18\n\nOutput\n0\n\nInput\n2 2 -1\n\nOutput\n4\n1 31 337 967"} +{"id": "00459", "text": "During the breaks between competitions, top-model Izabella tries to develop herself and not to be bored. For example, now she tries to solve Rubik's cube 2x2x2.\n\nIt's too hard to learn to solve Rubik's cube instantly, so she learns to understand if it's possible to solve the cube in some state using 90-degrees rotation of one face of the cube in any direction.\n\nTo check her answers she wants to use a program which will for some state of cube tell if it's possible to solve it using one rotation, described above.\n\nCube is called solved if for each face of cube all squares on it has the same color.\n\nhttps://en.wikipedia.org/wiki/Rubik's_Cube\n\n\n-----Input-----\n\nIn first line given a sequence of 24 integers a_{i} (1 \u2264 a_{i} \u2264 6), where a_{i} denotes color of i-th square. There are exactly 4 occurrences of all colors in this sequence.\n\n\n-----Output-----\n\nPrint \u00abYES\u00bb (without quotes) if it's possible to solve cube using one rotation and \u00abNO\u00bb (without quotes) otherwise.\n\n\n-----Examples-----\nInput\n2 5 4 6 1 3 6 2 5 5 1 2 3 5 3 1 1 2 4 6 6 4 3 4\n\nOutput\nNO\nInput\n5 3 5 3 2 5 2 5 6 2 6 2 4 4 4 4 1 1 1 1 6 3 6 3\n\nOutput\nYES\n\n\n-----Note-----\n\nIn first test case cube looks like this: [Image] \n\nIn second test case cube looks like this: [Image] \n\nIt's possible to solve cube by rotating face with squares with numbers 13, 14, 15, 16."} +{"id": "00460", "text": "Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participants out of those who got into top 500, will receive a Codeforces T-shirt.\n\nUnfortunately, you didn't manage to get into top 25, but you got into top 500, taking place p.\n\nNow the elimination round of 8VC Venture Cup 2017 is being held. It has been announced that the Codecraft-17 T-shirt winners will be chosen as follows. Let s be the number of points of the winner of the elimination round of 8VC Venture Cup 2017. Then the following pseudocode will be executed: \n\ni := (s div 50) mod 475\n\nrepeat 25 times:\n\n i := (i * 96 + 42) mod 475\n\n print (26 + i)\n\n\n\nHere \"div\" is the integer division operator, \"mod\" is the modulo (the remainder of division) operator.\n\nAs the result of pseudocode execution, 25 integers between 26 and 500, inclusive, will be printed. These will be the numbers of places of the participants who get the Codecraft-17 T-shirts. It is guaranteed that the 25 printed integers will be pairwise distinct for any value of s.\n\nYou're in the lead of the elimination round of 8VC Venture Cup 2017, having x points. You believe that having at least y points in the current round will be enough for victory.\n\nTo change your final score, you can make any number of successful and unsuccessful hacks. A successful hack brings you 100 points, an unsuccessful one takes 50 points from you. It's difficult to do successful hacks, though.\n\nYou want to win the current round and, at the same time, ensure getting a Codecraft-17 T-shirt. What is the smallest number of successful hacks you have to do to achieve that?\n\n\n-----Input-----\n\nThe only line contains three integers p, x and y (26 \u2264 p \u2264 500; 1 \u2264 y \u2264 x \u2264 20000)\u00a0\u2014 your place in Codecraft-17, your current score in the elimination round of 8VC Venture Cup 2017, and the smallest number of points you consider sufficient for winning the current round.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the smallest number of successful hacks you have to do in order to both win the elimination round of 8VC Venture Cup 2017 and ensure getting a Codecraft-17 T-shirt.\n\nIt's guaranteed that your goal is achievable for any valid input data.\n\n\n-----Examples-----\nInput\n239 10880 9889\n\nOutput\n0\n\nInput\n26 7258 6123\n\nOutput\n2\n\nInput\n493 8000 8000\n\nOutput\n24\n\nInput\n101 6800 6500\n\nOutput\n0\n\nInput\n329 19913 19900\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first example, there is no need to do any hacks since 10880 points already bring the T-shirt to the 239-th place of Codecraft-17 (that is, you). In this case, according to the pseudocode, the T-shirts will be given to the participants at the following places: \n\n475 422 84 411 453 210 157 294 146 188 420 367 29 356 398 155 102 239 91 133 365 312 449 301 343\n\n\n\nIn the second example, you have to do two successful and one unsuccessful hack to make your score equal to 7408.\n\nIn the third example, you need to do as many as 24 successful hacks to make your score equal to 10400.\n\nIn the fourth example, it's sufficient to do 6 unsuccessful hacks (and no successful ones) to make your score equal to 6500, which is just enough for winning the current round and also getting the T-shirt."} +{"id": "00461", "text": "Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit's and Owl's houses is a meters, between Rabbit's and Eeyore's house is b meters, between Owl's and Eeyore's house is c meters.\n\nFor enjoying his life and singing merry songs Winnie-the-Pooh should have a meal n times a day. Now he is in the Rabbit's house and has a meal for the first time. Each time when in the friend's house where Winnie is now the supply of honey is about to end, Winnie leaves that house. If Winnie has not had a meal the required amount of times, he comes out from the house and goes to someone else of his two friends. For this he chooses one of two adjacent paths, arrives to the house on the other end and visits his friend. You may assume that when Winnie is eating in one of his friend's house, the supply of honey in other friend's houses recover (most probably, they go to the supply store).\n\nWinnie-the-Pooh does not like physical activity. He wants to have a meal n times, traveling minimum possible distance. Help him to find this distance.\n\n\n-----Input-----\n\nFirst line contains an integer n (1 \u2264 n \u2264 100)\u00a0\u2014 number of visits.\n\nSecond line contains an integer a (1 \u2264 a \u2264 100)\u00a0\u2014 distance between Rabbit's and Owl's houses.\n\nThird line contains an integer b (1 \u2264 b \u2264 100)\u00a0\u2014 distance between Rabbit's and Eeyore's houses.\n\nFourth line contains an integer c (1 \u2264 c \u2264 100)\u00a0\u2014 distance between Owl's and Eeyore's houses.\n\n\n-----Output-----\n\nOutput one number\u00a0\u2014 minimum distance in meters Winnie must go through to have a meal n times.\n\n\n-----Examples-----\nInput\n3\n2\n3\n1\n\nOutput\n3\n\nInput\n1\n2\n3\n5\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test case the optimal path for Winnie is the following: first have a meal in Rabbit's house, then in Owl's house, then in Eeyore's house. Thus he will pass the distance 2 + 1 = 3.\n\nIn the second test case Winnie has a meal in Rabbit's house and that is for him. So he doesn't have to walk anywhere at all."} +{"id": "00462", "text": "There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x_1, the second friend lives at the point x_2, and the third friend lives at the point x_3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?\n\nIt's guaranteed that the optimal answer is always integer.\n\n\n-----Input-----\n\nThe first line of the input contains three distinct integers x_1, x_2 and x_3 (1 \u2264 x_1, x_2, x_3 \u2264 100)\u00a0\u2014 the coordinates of the houses of the first, the second and the third friends respectively. \n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimum total distance the friends need to travel in order to meet together.\n\n\n-----Examples-----\nInput\n7 1 4\n\nOutput\n6\n\nInput\n30 20 10\n\nOutput\n20\n\n\n\n-----Note-----\n\nIn the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4."} +{"id": "00463", "text": "There is an array with n elements a_1, a_2, ..., a_{n} and the number x.\n\nIn one operation you can select some i (1 \u2264 i \u2264 n) and replace element a_{i} with a_{i} & x, where & denotes the bitwise and operation.\n\nYou want the array to have at least two equal elements after applying some operations (possibly, none). In other words, there should be at least two distinct indices i \u2260 j such that a_{i} = a_{j}. Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply.\n\n\n-----Input-----\n\nThe first line contains integers n and x (2 \u2264 n \u2264 100 000, 1 \u2264 x \u2264 100 000), number of elements in the array and the number to and with.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 100 000), the elements of the array.\n\n\n-----Output-----\n\nPrint a single integer denoting the minimal number of operations to do, or -1, if it is impossible.\n\n\n-----Examples-----\nInput\n4 3\n1 2 3 7\n\nOutput\n1\n\nInput\n2 228\n1 1\n\nOutput\n0\n\nInput\n3 7\n1 2 3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example one can apply the operation to the last element of the array. That replaces 7 with 3, so we achieve the goal in one move.\n\nIn the second example the array already has two equal elements.\n\nIn the third example applying the operation won't change the array at all, so it is impossible to make some pair of elements equal."} +{"id": "00464", "text": "You have a given picture with size $w \\times h$. Determine if the given picture has a single \"+\" shape or not. A \"+\" shape is described below:\n\n A \"+\" shape has one center nonempty cell. There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In other words, there should be a ray in each direction. All other cells are empty. \n\nFind out if the given picture has single \"+\" shape.\n\n\n-----Input-----\n\nThe first line contains two integers $h$ and $w$ ($1 \\le h$, $w \\le 500$)\u00a0\u2014 the height and width of the picture.\n\nThe $i$-th of the next $h$ lines contains string $s_{i}$ of length $w$ consisting \".\" and \"*\" where \".\" denotes the empty space and \"*\" denotes the non-empty space.\n\n\n-----Output-----\n\nIf the given picture satisfies all conditions, print \"YES\". Otherwise, print \"NO\".\n\nYou can output each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n5 6\n......\n..*...\n.****.\n..*...\n..*...\n\nOutput\nYES\n\nInput\n3 5\n..*..\n****.\n.*...\n\nOutput\nNO\n\nInput\n7 7\n.......\n...*...\n..****.\n...*...\n...*...\n.......\n.*.....\n\nOutput\nNO\n\nInput\n5 6\n..**..\n..**..\n******\n..**..\n..**..\n\nOutput\nNO\n\nInput\n3 7\n.*...*.\n***.***\n.*...*.\n\nOutput\nNO\n\nInput\n5 10\n..........\n..*.......\n.*.******.\n..*.......\n..........\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example, the given picture contains one \"+\".\n\nIn the second example, two vertical branches are located in a different column.\n\nIn the third example, there is a dot outside of the shape.\n\nIn the fourth example, the width of the two vertical branches is $2$.\n\nIn the fifth example, there are two shapes.\n\nIn the sixth example, there is an empty space inside of the shape."} +{"id": "00465", "text": "Given three numbers $n, a, b$. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to $a$, and the number of components in its complement is $b$. The matrix must be symmetric, and all digits on the main diagonal must be zeroes.\n\nIn an undirected graph loops (edges from a vertex to itself) are not allowed. It can be at most one edge between a pair of vertices.\n\nThe adjacency matrix of an undirected graph is a square matrix of size $n$ consisting only of \"0\" and \"1\", where $n$ is the number of vertices of the graph and the $i$-th row and the $i$-th column correspond to the $i$-th vertex of the graph. The cell $(i,j)$ of the adjacency matrix contains $1$ if and only if the $i$-th and $j$-th vertices in the graph are connected by an edge.\n\nA connected component is a set of vertices $X$ such that for every two vertices from this set there exists at least one path in the graph connecting this pair of vertices, but adding any other vertex to $X$ violates this rule.\n\nThe complement or inverse of a graph $G$ is a graph $H$ on the same vertices such that two distinct vertices of $H$ are adjacent if and only if they are not adjacent in $G$.\n\n\n-----Input-----\n\nIn a single line, three numbers are given $n, a, b \\,(1 \\le n \\le 1000, 1 \\le a, b \\le n)$: is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement. \n\n\n-----Output-----\n\nIf there is no graph that satisfies these constraints on a single line, print \"NO\" (without quotes).\n\nOtherwise, on the first line, print \"YES\"(without quotes). In each of the next $n$ lines, output $n$ digits such that $j$-th digit of $i$-th line must be $1$ if and only if there is an edge between vertices $i$ and $j$ in $G$ (and $0$ otherwise). Note that the matrix must be symmetric, and all digits on the main diagonal must be zeroes. \n\nIf there are several matrices that satisfy the conditions \u2014 output any of them.\n\n\n-----Examples-----\nInput\n3 1 2\n\nOutput\nYES\n001\n001\n110\n\nInput\n3 3 3\n\nOutput\nNO"} +{"id": "00466", "text": "The finalists of the \"Russian Code Cup\" competition in 2214 will be the participants who win in one of the elimination rounds.\n\nThe elimination rounds are divided into main and additional. Each of the main elimination rounds consists of c problems, the winners of the round are the first n people in the rating list. Each of the additional elimination rounds consists of d problems. The winner of the additional round is one person. Besides, k winners of the past finals are invited to the finals without elimination.\n\nAs a result of all elimination rounds at least n\u00b7m people should go to the finals. You need to organize elimination rounds in such a way, that at least n\u00b7m people go to the finals, and the total amount of used problems in all rounds is as small as possible.\n\n\n-----Input-----\n\nThe first line contains two integers c and d (1 \u2264 c, d \u2264 100)\u00a0\u2014 the number of problems in the main and additional rounds, correspondingly. The second line contains two integers n and m (1 \u2264 n, m \u2264 100). Finally, the third line contains an integer k (1 \u2264 k \u2264 100)\u00a0\u2014 the number of the pre-chosen winners. \n\n\n-----Output-----\n\nIn the first line, print a single integer \u2014 the minimum number of problems the jury needs to prepare.\n\n\n-----Examples-----\nInput\n1 10\n7 2\n1\n\nOutput\n2\n\nInput\n2 2\n2 1\n2\n\nOutput\n0"} +{"id": "00467", "text": "Let\u2019s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid: ABCDEFGHIJKLM\n\nNOPQRSTUVWXYZ \n\nWe say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.\n\nA sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, \"ABC\" is a path, and so is \"KXWIHIJK\". \"MAB\" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).\n\nYou\u2019re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there\u2019s no solution, print \"Impossible\" (without the quotes).\n\n\n-----Input-----\n\nThe only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.\n\n\n-----Output-----\n\nOutput two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print \"Impossible\".\n\n\n-----Examples-----\nInput\nABCDEFGHIJKLMNOPQRSGTUVWXYZ\n\nOutput\nYXWVUTGHIJKLM\nZABCDEFSRQPON\n\nInput\nBUVTYZFQSNRIWOXXGJLKACPEMDH\n\nOutput\nImpossible"} +{"id": "00468", "text": "Year 2118. Androids are in mass production for decades now, and they do all the work for humans. But androids have to go to school to be able to solve creative tasks. Just like humans before.\n\nIt turns out that high school struggles are not gone. If someone is not like others, he is bullied. Vasya-8800 is an economy-class android which is produced by a little-known company. His design is not perfect, his characteristics also could be better. So he is bullied by other androids.\n\nOne of the popular pranks on Vasya is to force him to compare $x^y$ with $y^x$. Other androids can do it in milliseconds while Vasya's memory is too small to store such big numbers.\n\nPlease help Vasya! Write a fast program to compare $x^y$ with $y^x$ for Vasya, maybe then other androids will respect him.\n\n\n-----Input-----\n\nOn the only line of input there are two integers $x$ and $y$ ($1 \\le x, y \\le 10^{9}$).\n\n\n-----Output-----\n\nIf $x^y < y^x$, then print '<' (without quotes). If $x^y > y^x$, then print '>' (without quotes). If $x^y = y^x$, then print '=' (without quotes).\n\n\n-----Examples-----\nInput\n5 8\n\nOutput\n>\n\nInput\n10 3\n\nOutput\n<\n\nInput\n6 6\n\nOutput\n=\n\n\n\n-----Note-----\n\nIn the first example $5^8 = 5 \\cdot 5 \\cdot 5 \\cdot 5 \\cdot 5 \\cdot 5 \\cdot 5 \\cdot 5 = 390625$, and $8^5 = 8 \\cdot 8 \\cdot 8 \\cdot 8 \\cdot 8 = 32768$. So you should print '>'.\n\nIn the second example $10^3 = 1000 < 3^{10} = 59049$.\n\nIn the third example $6^6 = 46656 = 6^6$."} +{"id": "00469", "text": "A girl named Xenia has a cupboard that looks like an arc from ahead. The arc is made of a semicircle with radius r (the cupboard's top) and two walls of height h (the cupboard's sides). The cupboard's depth is r, that is, it looks like a rectangle with base r and height h + r from the sides. The figure below shows what the cupboard looks like (the front view is on the left, the side view is on the right). [Image] \n\nXenia got lots of balloons for her birthday. The girl hates the mess, so she wants to store the balloons in the cupboard. Luckily, each balloon is a sphere with radius $\\frac{r}{2}$. Help Xenia calculate the maximum number of balloons she can put in her cupboard. \n\nYou can say that a balloon is in the cupboard if you can't see any part of the balloon on the left or right view. The balloons in the cupboard can touch each other. It is not allowed to squeeze the balloons or deform them in any way. You can assume that the cupboard's walls are negligibly thin.\n\n\n-----Input-----\n\nThe single line contains two integers r, h (1 \u2264 r, h \u2264 10^7).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of balloons Xenia can put in the cupboard.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n3\n\nInput\n1 2\n\nOutput\n5\n\nInput\n2 1\n\nOutput\n2"} +{"id": "00470", "text": "A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer.\n\nLimak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards.\n\nHe is allowed to at most once discard two or three cards with the same number. Of course, he won't discard cards if it's impossible to choose two or three cards with the same number.\n\nGiven five numbers written on cards, cay you find the minimum sum of numbers on remaining cards?\n\n\n-----Input-----\n\nThe only line of the input contains five integers t_1, t_2, t_3, t_4 and t_5 (1 \u2264 t_{i} \u2264 100)\u00a0\u2014 numbers written on cards.\n\n\n-----Output-----\n\nPrint the minimum possible sum of numbers written on remaining cards.\n\n\n-----Examples-----\nInput\n7 3 7 3 20\n\nOutput\n26\n\nInput\n7 9 3 1 8\n\nOutput\n28\n\nInput\n10 10 10 10 10\n\nOutput\n20\n\n\n\n-----Note-----\n\nIn the first sample, Limak has cards with numbers 7, 3, 7, 3 and 20. Limak can do one of the following.\n\n Do nothing and the sum would be 7 + 3 + 7 + 3 + 20 = 40. Remove two cards with a number 7. The remaining sum would be 3 + 3 + 20 = 26. Remove two cards with a number 3. The remaining sum would be 7 + 7 + 20 = 34. \n\nYou are asked to minimize the sum so the answer is 26.\n\nIn the second sample, it's impossible to find two or three cards with the same number. Hence, Limak does nothing and the sum is 7 + 9 + 1 + 3 + 8 = 28.\n\nIn the third sample, all cards have the same number. It's optimal to discard any three cards. The sum of two remaining numbers is 10 + 10 = 20."} +{"id": "00471", "text": "Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x_1, x_2, ..., x_{n}. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.\n\nVasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and a (1 \u2264 n \u2264 100 000, - 1 000 000 \u2264 a \u2264 1 000 000)\u00a0\u2014 the number of checkpoints and Vasya's starting position respectively.\n\nThe second line contains n integers x_1, x_2, ..., x_{n} ( - 1 000 000 \u2264 x_{i} \u2264 1 000 000)\u00a0\u2014 coordinates of the checkpoints.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.\n\n\n-----Examples-----\nInput\n3 10\n1 7 12\n\nOutput\n7\n\nInput\n2 0\n11 -10\n\nOutput\n10\n\nInput\n5 0\n0 0 1000 0 0\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7.\n\nIn the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point - 10."} +{"id": "00472", "text": "Let's consider equation:x^2 + s(x)\u00b7x - n = 0, \n\nwhere x, n are positive integers, s(x) is the function, equal to the sum of digits of number x in the decimal number system.\n\nYou are given an integer n, find the smallest positive integer root of equation x, or else determine that there are no such roots.\n\n\n-----Input-----\n\nA single line contains integer n (1 \u2264 n \u2264 10^18) \u2014 the equation parameter.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use cin, cout streams or the %I64d specifier. \n\n\n-----Output-----\n\nPrint -1, if the equation doesn't have integer positive roots. Otherwise print such smallest integer x (x > 0), that the equation given in the statement holds.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1\n\nInput\n110\n\nOutput\n10\n\nInput\n4\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first test case x = 1 is the minimum root. As s(1) = 1 and 1^2 + 1\u00b71 - 2 = 0.\n\nIn the second test case x = 10 is the minimum root. As s(10) = 1 + 0 = 1 and 10^2 + 1\u00b710 - 110 = 0.\n\nIn the third test case the equation has no roots."} +{"id": "00473", "text": "George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. \n\nHelp George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). \n\n\n-----Input-----\n\nThe first line contains current time s as a string in the format \"hh:mm\". The second line contains time t in the format \"hh:mm\" \u2014 the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 \u2264 hh \u2264 23, 00 \u2264 mm \u2264 59.\n\n\n-----Output-----\n\nIn the single line print time p \u2014 the time George went to bed in the format similar to the format of the time in the input.\n\n\n-----Examples-----\nInput\n05:50\n05:44\n\nOutput\n00:06\n\nInput\n00:00\n01:00\n\nOutput\n23:00\n\nInput\n00:01\n00:00\n\nOutput\n00:01\n\n\n\n-----Note-----\n\nIn the first sample George went to bed at \"00:06\". Note that you should print the time only in the format \"00:06\". That's why answers \"0:06\", \"00:6\" and others will be considered incorrect. \n\nIn the second sample, George went to bed yesterday.\n\nIn the third sample, George didn't do to bed at all."} +{"id": "00474", "text": "You are given array $a_1, a_2, \\dots, a_n$. Find the subsegment $a_l, a_{l+1}, \\dots, a_r$ ($1 \\le l \\le r \\le n$) with maximum arithmetic mean $\\frac{1}{r - l + 1}\\sum\\limits_{i=l}^{r}{a_i}$ (in floating-point numbers, i.e. without any rounding).\n\nIf there are many such subsegments find the longest one.\n\n\n-----Input-----\n\nThe first line contains single integer $n$ ($1 \\le n \\le 10^5$) \u2014 length of the array $a$.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 10^9$) \u2014 the array $a$.\n\n\n-----Output-----\n\nPrint the single integer \u2014 the length of the longest subsegment with maximum possible arithmetic mean.\n\n\n-----Example-----\nInput\n5\n6 1 6 6 0\n\nOutput\n2\n\n\n\n-----Note-----\n\nThe subsegment $[3, 4]$ is the longest among all subsegments with maximum arithmetic mean."} +{"id": "00475", "text": "On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.\n\nThere are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.\n\nHaving finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).\n\nSo as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\\,244\\,353$.\n\n\n-----Input-----\n\nThe first and only line contains three integers $n$, $m$ and $k$ ($1 \\leq n,m \\leq 2000, 0 \\leq k \\leq n-1$)\u00a0\u2014 the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of ways to color bricks modulo $998\\,244\\,353$.\n\n\n-----Examples-----\nInput\n3 3 0\n\nOutput\n3\n\nInput\n3 2 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.\n\nIn the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image]"} +{"id": "00476", "text": "A magic number is a number formed by concatenation of numbers 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but 1444, 514 and 414 are not.\n\nYou're given a number. Determine if it is a magic number or not.\n\n\n-----Input-----\n\nThe first line of input contains an integer n, (1 \u2264 n \u2264 10^9). This number doesn't contain leading zeros.\n\n\n-----Output-----\n\nPrint \"YES\" if n is a magic number or print \"NO\" if it's not.\n\n\n-----Examples-----\nInput\n114114\n\nOutput\nYES\n\nInput\n1111\n\nOutput\nYES\n\nInput\n441231\n\nOutput\nNO"} +{"id": "00477", "text": "Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n \u00d7 m chessboard, a very tasty candy and two numbers a and b.\n\nDima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:\n\n move the candy from position (x, y) on the board to position (x - a, y - b); move the candy from position (x, y) on the board to position (x + a, y - b); move the candy from position (x, y) on the board to position (x - a, y + b); move the candy from position (x, y) on the board to position (x + a, y + b). \n\nNaturally, Dima doesn't allow to move the candy beyond the chessboard borders.\n\nInna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task! \n\n\n-----Input-----\n\nThe first line of the input contains six integers n, m, i, j, a, b (1 \u2264 n, m \u2264 10^6;\u00a01 \u2264 i \u2264 n;\u00a01 \u2264 j \u2264 m;\u00a01 \u2264 a, b \u2264 10^6).\n\nYou can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the minimum number of moves needed to get the candy.\n\nIf Inna and the pony cannot get the candy playing by Dima's rules, print on a single line \"Poor Inna and pony!\" without the quotes.\n\n\n-----Examples-----\nInput\n5 7 1 3 2 2\n\nOutput\n2\n\nInput\n5 5 2 3 1 1\n\nOutput\nPoor Inna and pony!\n\n\n\n-----Note-----\n\nNote to sample 1:\n\nInna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two."} +{"id": "00478", "text": "You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.\n\nIn one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \\le i \\le |s|$ during each operation.\n\nFor the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).\n\nConsider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it. \n\nYour task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $|s|$ ($1 \\le |s| \\le 100$) \u2014 the length of $s$.\n\nThe second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible number of characters you can remove if you choose the sequence of moves optimally.\n\n\n-----Examples-----\nInput\n8\nbacabcab\n\nOutput\n4\n\nInput\n4\nbcda\n\nOutput\n3\n\nInput\n6\nabbbbb\n\nOutput\n5\n\n\n\n-----Note-----\n\nThe first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.\n\nIn the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a."} +{"id": "00479", "text": "ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.\n\nFor example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.\n\nLet's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.\n\n\n-----Input-----\n\nThe first line contains two integers n, k (1 \u2264 n \u2264 5000, 1 \u2264 k \u2264 20).\n\nThe next line contains n space-separated integers a_{i} (1 \u2264 a_{i} \u2264 10^7) \u2014 the denominations of the bills that are used in the country. Numbers a_{i} follow in the strictly increasing order.\n\nThe next line contains integer q (1 \u2264 q \u2264 20) \u2014 the number of requests for cash withdrawal that you will make.\n\nThe next q lines contain numbers x_{i} (1 \u2264 x_{i} \u2264 2\u00b710^8) \u2014 the sums of money in burles that you are going to withdraw from the ATM.\n\n\n-----Output-----\n\nFor each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print - 1, if it is impossible to get the corresponding sum.\n\n\n-----Examples-----\nInput\n6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950\n\nOutput\n6\n20\n19\n20\n-1\n3\n-1\n-1\n\nInput\n5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15\n\nOutput\n1\n1\n1\n2\n2\n2\n2\n-1"} +{"id": "00480", "text": "The tram in Berland goes along a straight line from the point 0 to the point s and back, passing 1 meter per t_1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points x = 0 and x = s.\n\nIgor is at the point x_1. He should reach the point x_2. Igor passes 1 meter per t_2 seconds. \n\nYour task is to determine the minimum time Igor needs to get from the point x_1 to the point x_2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the point x_1.\n\nIgor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is not obligatory that points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than 1 meter per t_2 seconds). He can also stand at some point for some time.\n\n\n-----Input-----\n\nThe first line contains three integers s, x_1 and x_2 (2 \u2264 s \u2264 1000, 0 \u2264 x_1, x_2 \u2264 s, x_1 \u2260 x_2)\u00a0\u2014 the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to.\n\nThe second line contains two integers t_1 and t_2 (1 \u2264 t_1, t_2 \u2264 1000)\u00a0\u2014 the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter.\n\nThe third line contains two integers p and d (1 \u2264 p \u2264 s - 1, d is either 1 or $- 1$)\u00a0\u2014 the position of the tram in the moment Igor came to the point x_1 and the direction of the tram at this moment. If $d = - 1$, the tram goes in the direction from the point s to the point 0. If d = 1, the tram goes in the direction from the point 0 to the point s.\n\n\n-----Output-----\n\nPrint the minimum time in seconds which Igor needs to get from the point x_1 to the point x_2.\n\n\n-----Examples-----\nInput\n4 2 4\n3 4\n1 1\n\nOutput\n8\n\nInput\n5 4 0\n1 2\n3 1\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds in total, because he passes 1 meter per 4 seconds. \n\nIn the second example Igor can, for example, go towards the point x_2 and get to the point 1 in 6 seconds (because he has to pass 3 meters, but he passes 1 meters per 2 seconds). At that moment the tram will be at the point 1, so Igor can enter the tram and pass 1 meter in 1 second. Thus, Igor will reach the point x_2 in 7 seconds in total."} +{"id": "00481", "text": "Let's consider a table consisting of n rows and n columns. The cell located at the intersection of i-th row and j-th column contains number i \u00d7 j. The rows and columns are numbered starting from 1.\n\nYou are given a positive integer x. Your task is to count the number of cells in a table that contain number x.\n\n\n-----Input-----\n\nThe single line contains numbers n and x (1 \u2264 n \u2264 10^5, 1 \u2264 x \u2264 10^9) \u2014 the size of the table and the number that we are looking for in the table.\n\n\n-----Output-----\n\nPrint a single number: the number of times x occurs in the table.\n\n\n-----Examples-----\nInput\n10 5\n\nOutput\n2\n\nInput\n6 12\n\nOutput\n4\n\nInput\n5 13\n\nOutput\n0\n\n\n\n-----Note-----\n\nA table for the second sample test is given below. The occurrences of number 12 are marked bold. [Image]"} +{"id": "00482", "text": "Innokentiy decides to change the password in the social net \"Contact!\", but he is too lazy to invent a new password by himself. That is why he needs your help. \n\nInnokentiy decides that new password should satisfy the following conditions: the length of the password must be equal to n, the password should consist only of lowercase Latin letters, the number of distinct symbols in the password must be equal to k, any two consecutive symbols in the password must be distinct. \n\nYour task is to help Innokentiy and to invent a new password which will satisfy all given conditions. \n\n\n-----Input-----\n\nThe first line contains two positive integers n and k (2 \u2264 n \u2264 100, 2 \u2264 k \u2264 min(n, 26)) \u2014 the length of the password and the number of distinct symbols in it. \n\nPay attention that a desired new password always exists.\n\n\n-----Output-----\n\nPrint any password which satisfies all conditions given by Innokentiy.\n\n\n-----Examples-----\nInput\n4 3\n\nOutput\njava\n\nInput\n6 6\n\nOutput\npython\n\nInput\n5 2\n\nOutput\nphphp\n\n\n\n-----Note-----\n\nIn the first test there is one of the appropriate new passwords \u2014 java, because its length is equal to 4 and 3 distinct lowercase letters a, j and v are used in it.\n\nIn the second test there is one of the appropriate new passwords \u2014 python, because its length is equal to 6 and it consists of 6 distinct lowercase letters.\n\nIn the third test there is one of the appropriate new passwords \u2014 phphp, because its length is equal to 5 and 2 distinct lowercase letters p and h are used in it.\n\nPay attention the condition that no two identical symbols are consecutive is correct for all appropriate passwords in tests."} +{"id": "00483", "text": "There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, x_{i} is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.\n\nYou know the direction of each particle movement\u00a0\u2014 it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.\n\nWrite the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.\n\n\n-----Input-----\n\nThe first line contains the positive integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the number of particles. \n\nThe second line contains n symbols \"L\" and \"R\". If the i-th symbol equals \"L\", then the i-th particle will move to the left, otherwise the i-th symbol equals \"R\" and the i-th particle will move to the right.\n\nThe third line contains the sequence of pairwise distinct even integers x_1, x_2, ..., x_{n} (0 \u2264 x_{i} \u2264 10^9)\u00a0\u2014 the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. \n\n\n-----Output-----\n\nIn the first line print the only integer\u00a0\u2014 the first moment (in microseconds) when two particles are at the same point and there will be an explosion. \n\nPrint the only integer -1, if the collision of particles doesn't happen. \n\n\n-----Examples-----\nInput\n4\nRLRL\n2 4 6 10\n\nOutput\n1\n\nInput\n3\nLLR\n40 50 60\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. \n\nIn the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point."} +{"id": "00484", "text": "One very important person has a piece of paper in the form of a rectangle a \u00d7 b.\n\nAlso, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size x_{i} \u00d7 y_{i}. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees).\n\nA very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals?\n\n\n-----Input-----\n\nThe first line contains three integer numbers n, a and b (1 \u2264 n, a, b \u2264 100).\n\nEach of the next n lines contain two numbers x_{i}, y_{i} (1 \u2264 x_{i}, y_{i} \u2264 100).\n\n\n-----Output-----\n\nPrint the largest total area that can be occupied by two seals. If you can not select two seals, print 0.\n\n\n-----Examples-----\nInput\n2 2 2\n1 2\n2 1\n\nOutput\n4\n\nInput\n4 10 9\n2 3\n1 1\n5 10\n9 11\n\nOutput\n56\n\nInput\n3 10 10\n6 6\n7 7\n20 5\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper.\n\nIn the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area.\n\nIn the third example there is no such pair of seals that they both can fit on a piece of paper."} +{"id": "00485", "text": "The Cybermen and the Daleks have long been the Doctor's main enemies. Everyone knows that both these species enjoy destroying everything they encounter. However, a little-known fact about them is that they both also love taking Turing tests!\n\nHeidi designed a series of increasingly difficult tasks for them to spend their time on, which would allow the Doctor enough time to save innocent lives!\n\nThe funny part is that these tasks would be very easy for a human to solve.\n\nThe first task is as follows. There are some points on the plane. All but one of them are on the boundary of an axis-aligned square (its sides are parallel to the axes). Identify that point.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($2 \\le n \\le 10$).\n\nEach of the following $4n + 1$ lines contains two integers $x_i, y_i$ ($0 \\leq x_i, y_i \\leq 50$), describing the coordinates of the next point.\n\nIt is guaranteed that there are at least $n$ points on each side of the square and all $4n + 1$ points are distinct.\n\n\n-----Output-----\n\nPrint two integers\u00a0\u2014 the coordinates of the point that is not on the boundary of the square.\n\n\n-----Examples-----\nInput\n2\n0 0\n0 1\n0 2\n1 0\n1 1\n1 2\n2 0\n2 1\n2 2\n\nOutput\n1 1\n\nInput\n2\n0 0\n0 1\n0 2\n0 3\n1 0\n1 2\n2 0\n2 1\n2 2\n\nOutput\n0 3\n\n\n\n-----Note-----\n\nIn both examples, the square has four sides $x=0$, $x=2$, $y=0$, $y=2$."} +{"id": "00486", "text": "Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater value of the product makes the nirvana deeper.\n\nHelp Kurt find the maximum possible product of digits among all integers from $1$ to $n$.\n\n\n-----Input-----\n\nThe only input line contains the integer $n$ ($1 \\le n \\le 2\\cdot10^9$).\n\n\n-----Output-----\n\nPrint the maximum product of digits among all integers from $1$ to $n$.\n\n\n-----Examples-----\nInput\n390\n\nOutput\n216\n\nInput\n7\n\nOutput\n7\n\nInput\n1000000000\n\nOutput\n387420489\n\n\n\n-----Note-----\n\nIn the first example the maximum product is achieved for $389$ (the product of digits is $3\\cdot8\\cdot9=216$).\n\nIn the second example the maximum product is achieved for $7$ (the product of digits is $7$).\n\nIn the third example the maximum product is achieved for $999999999$ (the product of digits is $9^9=387420489$)."} +{"id": "00487", "text": "Awruk is taking part in elections in his school. It is the final round. He has only one opponent\u00a0\u2014 Elodreip. The are $n$ students in the school. Each student has exactly $k$ votes and is obligated to use all of them. So Awruk knows that if a person gives $a_i$ votes for Elodreip, than he will get exactly $k - a_i$ votes from this person. Of course $0 \\le k - a_i$ holds.\n\nAwruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows $a_1, a_2, \\dots, a_n$ \u2014 how many votes for Elodreip each student wants to give. Now he wants to change the number $k$ to win the elections. Of course he knows that bigger $k$ means bigger chance that somebody may notice that he has changed something and then he will be disqualified.\n\nSo, Awruk knows $a_1, a_2, \\dots, a_n$ \u2014 how many votes each student will give to his opponent. Help him select the smallest winning number $k$. In order to win, Awruk needs to get strictly more votes than Elodreip.\n\n\n-----Input-----\n\nThe first line contains integer $n$ ($1 \\le n \\le 100$)\u00a0\u2014 the number of students in the school.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 100$)\u00a0\u2014 the number of votes each student gives to Elodreip.\n\n\n-----Output-----\n\nOutput the smallest integer $k$ ($k \\ge \\max a_i$) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.\n\n\n-----Examples-----\nInput\n5\n1 1 1 5 1\n\nOutput\n5\nInput\n5\n2 2 3 2 2\n\nOutput\n5\n\n\n-----Note-----\n\nIn the first example, Elodreip gets $1 + 1 + 1 + 5 + 1 = 9$ votes. The smallest possible $k$ is $5$ (it surely can't be less due to the fourth person), and it leads to $4 + 4 + 4 + 0 + 4 = 16$ votes for Awruk, which is enough to win.\n\nIn the second example, Elodreip gets $11$ votes. If $k = 4$, Awruk gets $9$ votes and loses to Elodreip."} +{"id": "00488", "text": "You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right.\n\nYou perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can't perform it.\n\nHow many operations will you need to perform until the next operation does not have any points to delete?\n\n\n-----Input-----\n\nInput contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc.\n\nThe number of the points is between 1 and 10^6.\n\n\n-----Output-----\n\nOutput one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete.\n\n\n-----Examples-----\nInput\naabb\n\nOutput\n2\n\nInput\naabcaa\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first test case, the first operation will delete two middle points and leave points \"ab\", which will be deleted with the second operation. There will be no points left to apply the third operation to.\n\nIn the second test case, the first operation will delete the four points in the middle, leaving points \"aa\". None of them have neighbors of other colors, so the second operation can't be applied."} +{"id": "00489", "text": "After returning from the army Makes received a gift \u2014 an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i, \u00a0j, \u00a0k) (i < j < k), such that a_{i}\u00b7a_{j}\u00b7a_{k} is minimum possible, are there in the array? Help him with it!\n\n\n-----Input-----\n\nThe first line of input contains a positive integer number n\u00a0(3 \u2264 n \u2264 10^5) \u2014 the number of elements in array a. The second line contains n positive integer numbers a_{i}\u00a0(1 \u2264 a_{i} \u2264 10^9) \u2014 the elements of a given array.\n\n\n-----Output-----\n\nPrint one number \u2014 the quantity of triples (i, \u00a0j, \u00a0k) such that i, \u00a0j and k are pairwise distinct and a_{i}\u00b7a_{j}\u00b7a_{k} is minimum possible.\n\n\n-----Examples-----\nInput\n4\n1 1 1 1\n\nOutput\n4\n\nInput\n5\n1 3 2 3 4\n\nOutput\n2\n\nInput\n6\n1 3 3 1 3 2\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4.\n\nIn the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2.\n\nIn the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices."} +{"id": "00490", "text": "Katie, Kuro and Shiro are best friends. They have known each other since kindergarten. That's why they often share everything with each other and work together on some very hard problems.\n\nToday is Shiro's birthday. She really loves pizza so she wants to invite her friends to the pizza restaurant near her house to celebrate her birthday, including her best friends Katie and Kuro.\n\nShe has ordered a very big round pizza, in order to serve her many friends. Exactly $n$ of Shiro's friends are here. That's why she has to divide the pizza into $n + 1$ slices (Shiro also needs to eat). She wants the slices to be exactly the same size and shape. If not, some of her friends will get mad and go home early, and the party will be over.\n\nShiro is now hungry. She wants to cut the pizza with minimum of straight cuts. A cut is a straight segment, it might have ends inside or outside the pizza. But she is too lazy to pick up the calculator.\n\nAs usual, she will ask Katie and Kuro for help. But they haven't come yet. Could you help Shiro with this problem?\n\n\n-----Input-----\n\nA single line contains one non-negative integer $n$ ($0 \\le n \\leq 10^{18}$)\u00a0\u2014 the number of Shiro's friends. The circular pizza has to be sliced into $n + 1$ pieces.\n\n\n-----Output-----\n\nA single integer\u00a0\u2014 the number of straight cuts Shiro needs.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n2\nInput\n4\n\nOutput\n5\n\n\n-----Note-----\n\nTo cut the round pizza into quarters one has to make two cuts through the center with angle $90^{\\circ}$ between them.\n\nTo cut the round pizza into five equal parts one has to make five cuts."} +{"id": "00491", "text": "Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obligations. Moreover, they even have their own bank accounts. The state of a bank account is an integer. The state of a bank account can be a negative number. This means that the owner of the account owes the bank money.\n\nIlya the Lion has recently had a birthday, so he got a lot of gifts. One of them (the gift of the main ZooVille bank) is the opportunity to delete the last digit or the digit before last from the state of his bank account no more than once. For example, if the state of Ilya's bank account is -123, then Ilya can delete the last digit and get his account balance equal to -12, also he can remove its digit before last and get the account balance equal to -13. Of course, Ilya is permitted not to use the opportunity to delete a digit from the balance.\n\nIlya is not very good at math, and that's why he asks you to help him maximize his bank account. Find the maximum state of the bank account that can be obtained using the bank's gift.\n\n\n-----Input-----\n\nThe single line contains integer n (10 \u2264 |n| \u2264 10^9) \u2014 the state of Ilya's bank account.\n\n\n-----Output-----\n\nIn a single line print an integer \u2014 the maximum state of the bank account that Ilya can get. \n\n\n-----Examples-----\nInput\n2230\n\nOutput\n2230\n\nInput\n-10\n\nOutput\n0\n\nInput\n-100003\n\nOutput\n-10000\n\n\n\n-----Note-----\n\nIn the first test sample Ilya doesn't profit from using the present.\n\nIn the second test sample you can delete digit 1 and get the state of the account equal to 0."} +{"id": "00492", "text": "[Image] \n\nWalking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays\u00a0\u2013 caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption.\n\nSpinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one):\n\n [Image] \n\nAfter the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in.\n\nSlastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this.\n\n\n-----Input-----\n\nThere are two characters in the first string\u00a0\u2013 the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space.\n\nIn the second strings, a single number n is given (0 \u2264 n \u2264 10^9)\u00a0\u2013 the duration of the rotation.\n\nIt is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position.\n\n\n-----Output-----\n\nOutput cw, if the direction is clockwise, ccw\u00a0\u2013 if counter-clockwise, and undefined otherwise.\n\n\n-----Examples-----\nInput\n^ >\n1\n\nOutput\ncw\n\nInput\n< ^\n3\n\nOutput\nccw\n\nInput\n^ v\n6\n\nOutput\nundefined"} +{"id": "00493", "text": "Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a \"domino show\".\n\nChris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.\n\nAfter each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process. [Image] \n\nGiven the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process!\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 3000), the number of the dominoes in the line. The next line contains a character string s of length n. The i-th character of the string s_{i} is equal to \"L\", if the i-th domino has been pushed to the left; \"R\", if the i-th domino has been pushed to the right; \".\", if the i-th domino has not been pushed. \n\nIt is guaranteed that if s_{i} = s_{j} = \"L\" and i < j, then there exists such k that i < k < j and s_{k} = \"R\"; if s_{i} = s_{j} = \"R\" and i < j, then there exists such k that i < k < j and s_{k} = \"L\".\n\n\n-----Output-----\n\nOutput a single integer, the number of the dominoes that remain vertical at the end of the process.\n\n\n-----Examples-----\nInput\n14\n.L.R...LR..L..\n\nOutput\n4\n\nInput\n5\nR....\n\nOutput\n0\n\nInput\n1\n.\n\nOutput\n1\n\n\n\n-----Note-----\n\nThe first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.\n\nIn the second example case, all pieces fall down since the first piece topples all the other pieces.\n\nIn the last example case, a single piece has not been pushed in either direction."} +{"id": "00494", "text": "n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation a_1, a_2, ..., a_{n} of length n. It is an integer sequence such that each integer from 1 to n appears exactly once in it.\n\nThe game consists of m steps. On each step the current leader with index i counts out a_{i} people in clockwise order, starting from the next person. The last one to be pointed at by the leader becomes the new leader.\n\nYou are given numbers l_1, l_2, ..., l_{m} \u2014 indices of leaders in the beginning of each step. Child with number l_1 is the first leader in the game. \n\nWrite a program which will restore a possible permutation a_1, a_2, ..., a_{n}. If there are multiple solutions then print any of them. If there is no solution then print -1.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n, m (1 \u2264 n, m \u2264 100).\n\nThe second line contains m integer numbers l_1, l_2, ..., l_{m} (1 \u2264 l_{i} \u2264 n) \u2014 indices of leaders in the beginning of each step.\n\n\n-----Output-----\n\nPrint such permutation of n numbers a_1, a_2, ..., a_{n} that leaders in the game will be exactly l_1, l_2, ..., l_{m} if all the rules are followed. If there are multiple solutions print any of them. \n\nIf there is no permutation which satisfies all described conditions print -1.\n\n\n-----Examples-----\nInput\n4 5\n2 3 1 4 4\n\nOutput\n3 1 2 4 \n\nInput\n3 3\n3 1 2\n\nOutput\n-1\n\n\n\n-----Note-----\n\nLet's follow leadership in the first example: Child 2 starts. Leadership goes from 2 to 2 + a_2 = 3. Leadership goes from 3 to 3 + a_3 = 5. As it's greater than 4, it's going in a circle to 1. Leadership goes from 1 to 1 + a_1 = 4. Leadership goes from 4 to 4 + a_4 = 8. Thus in circle it still remains at 4."} +{"id": "00495", "text": "Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.\n\nHelp Pasha count the maximum number he can get if he has the time to make at most k swaps.\n\n\n-----Input-----\n\nThe single line contains two integers a and k (1 \u2264 a \u2264 10^18;\u00a00 \u2264 k \u2264 100).\n\n\n-----Output-----\n\nPrint the maximum number that Pasha can get if he makes at most k swaps.\n\n\n-----Examples-----\nInput\n1990 1\n\nOutput\n9190\n\nInput\n300 0\n\nOutput\n300\n\nInput\n1034 2\n\nOutput\n3104\n\nInput\n9090000078001234 6\n\nOutput\n9907000008001234"} +{"id": "00496", "text": "Petya is preparing for IQ test and he has noticed that there many problems like: you are given a sequence, find the next number. Now Petya can solve only problems with arithmetic or geometric progressions.\n\nArithmetic progression is a sequence a_1, a_1 + d, a_1 + 2d, ..., a_1 + (n - 1)d, where a_1 and d are any numbers.\n\nGeometric progression is a sequence b_1, b_2 = b_1q, ..., b_{n} = b_{n} - 1q, where b_1 \u2260 0, q \u2260 0, q \u2260 1. \n\nHelp Petya and write a program to determine if the given sequence is arithmetic or geometric. Also it should found the next number. If the sequence is neither arithmetic nor geometric, print 42 (he thinks it is impossible to find better answer). You should also print 42 if the next element of progression is not integer. So answer is always integer.\n\n\n-----Input-----\n\nThe first line contains exactly four integer numbers between 1 and 1000, inclusively.\n\n\n-----Output-----\n\nPrint the required number. If the given sequence is arithmetic progression, print the next progression element. Similarly, if the given sequence is geometric progression, print the next progression element.\n\nPrint 42 if the given sequence is not an arithmetic or geometric progression.\n\n\n-----Examples-----\nInput\n836 624 412 200\n\nOutput\n-12\n\nInput\n1 334 667 1000\n\nOutput\n1333\n\n\n\n-----Note-----\n\nThis problem contains very weak pretests!"} +{"id": "00497", "text": "Ilya lives in a beautiful city of Chordalsk.\n\nThere are $n$ houses on the street Ilya lives, they are numerated from $1$ to $n$ from left to right; the distance between every two neighboring houses is equal to $1$ unit. The neighboring houses are $1$ and $2$, $2$ and $3$, ..., $n-1$ and $n$. The houses $n$ and $1$ are not neighboring.\n\nThe houses are colored in colors $c_1, c_2, \\ldots, c_n$ so that the $i$-th house is colored in the color $c_i$. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.\n\nIlya wants to select two houses $i$ and $j$ so that $1 \\leq i < j \\leq n$, and they have different colors: $c_i \\neq c_j$. He will then walk from the house $i$ to the house $j$ the distance of $(j-i)$ units.\n\nIlya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.\n\nHelp Ilya, find this maximum possible distance.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($3 \\leq n \\leq 300\\,000$)\u00a0\u2014 the number of cities on the street.\n\nThe second line contains $n$ integers $c_1, c_2, \\ldots, c_n$ ($1 \\leq c_i \\leq n$)\u00a0\u2014 the colors of the houses.\n\nIt is guaranteed that there is at least one pair of indices $i$ and $j$ so that $1 \\leq i < j \\leq n$ and $c_i \\neq c_j$.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum possible distance Ilya can walk.\n\n\n-----Examples-----\nInput\n5\n1 2 3 2 3\n\nOutput\n4\n\nInput\n3\n1 2 1\n\nOutput\n1\n\nInput\n7\n1 1 3 1 1 1 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of $5-1 = 4$ units.\n\nIn the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of $1$ unit.\n\nIn the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of $7-3 = 4$ units."} +{"id": "00498", "text": "Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture).\n\nThe organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i.\u00a0e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i.\u00a0e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. [Image] The picture illustrates the first and the second samples. \n\nSanta Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right!\n\n\n-----Input-----\n\nThe only line contains three integers n, m and k (1 \u2264 n, m \u2264 10 000, 1 \u2264 k \u2264 2nm)\u00a0\u2014 the number of lanes, the number of desks in each lane and the number of Santa Claus' place.\n\n\n-----Output-----\n\nPrint two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be \"L\", if Santa Clause should sit on the left, and \"R\" if his place is on the right.\n\n\n-----Examples-----\nInput\n4 3 9\n\nOutput\n2 2 L\n\nInput\n4 3 24\n\nOutput\n4 3 R\n\nInput\n2 4 4\n\nOutput\n1 2 R\n\n\n\n-----Note-----\n\nThe first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example.\n\nIn the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right."} +{"id": "00499", "text": "Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions: take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color; take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color. \n\nShe repeats this process until there is only one card left. What are the possible colors for the final card?\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 200)\u00a0\u2014 the total number of cards.\n\nThe next line contains a string s of length n \u2014 the colors of the cards. s contains only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively.\n\n\n-----Output-----\n\nPrint a single string of up to three characters\u00a0\u2014 the possible colors of the final card (using the same symbols as the input) in alphabetical order.\n\n\n-----Examples-----\nInput\n2\nRB\n\nOutput\nG\n\nInput\n3\nGRG\n\nOutput\nBR\n\nInput\n5\nBBBBB\n\nOutput\nB\n\n\n\n-----Note-----\n\nIn the first sample, Catherine has one red card and one blue card, which she must exchange for a green card.\n\nIn the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue card. Alternatively, she can exchange a green and a red card for a blue card, then exchange the blue card and remaining green card for a red card.\n\nIn the third sample, Catherine only has blue cards, so she can only exchange them for more blue cards."} +{"id": "00500", "text": "Dasha decided to have a rest after solving the problem. She had been ready to start her favourite activity \u2014 origami, but remembered the puzzle that she could not solve. [Image] \n\nThe tree is a non-oriented connected graph without cycles. In particular, there always are n - 1 edges in a tree with n vertices.\n\nThe puzzle is to position the vertices at the points of the Cartesian plane with integral coordinates, so that the segments between the vertices connected by edges are parallel to the coordinate axes. Also, the intersection of segments is allowed only at their ends. Distinct vertices should be placed at different points. \n\nHelp Dasha to find any suitable way to position the tree vertices on the plane.\n\nIt is guaranteed that if it is possible to position the tree vertices on the plane without violating the condition which is given above, then you can do it by using points with integral coordinates which don't exceed 10^18 in absolute value.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 30) \u2014 the number of vertices in the tree. \n\nEach of next n - 1 lines contains two integers u_{i}, v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n) that mean that the i-th edge of the tree connects vertices u_{i} and v_{i}.\n\nIt is guaranteed that the described graph is a tree.\n\n\n-----Output-----\n\nIf the puzzle doesn't have a solution then in the only line print \"NO\".\n\nOtherwise, the first line should contain \"YES\". The next n lines should contain the pair of integers x_{i}, y_{i} (|x_{i}|, |y_{i}| \u2264 10^18) \u2014 the coordinates of the point which corresponds to the i-th vertex of the tree.\n\nIf there are several solutions, print any of them. \n\n\n-----Examples-----\nInput\n7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\nOutput\nYES\n0 0\n1 0\n0 1\n2 0\n1 -1\n-1 1\n0 2\nInput\n6\n1 2\n2 3\n2 4\n2 5\n2 6\n\nOutput\nNO\n\nInput\n4\n1 2\n2 3\n3 4\nOutput\nYES\n3 3\n4 3\n5 3\n6 3\n\n\n-----Note-----\n\nIn the first sample one of the possible positions of tree is: [Image]"} +{"id": "00501", "text": "Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.\n\nConsider two infinite sets of numbers. The first set consists of odd positive numbers ($1, 3, 5, 7, \\ldots$), and the second set consists of even positive numbers ($2, 4, 6, 8, \\ldots$). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage\u00a0\u2014 the first two numbers from the second set, on the third stage\u00a0\u2014 the next four numbers from the first set, on the fourth\u00a0\u2014 the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another. \n\nThe ten first written numbers: $1, 2, 4, 3, 5, 7, 9, 6, 8, 10$. Let's number the numbers written, starting with one.\n\nThe task is to find the sum of numbers with numbers from $l$ to $r$ for given integers $l$ and $r$. The answer may be big, so you need to find the remainder of the division by $1000000007$ ($10^9+7$).\n\nNazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.\n\n\n-----Input-----\n\nThe first line contains two integers $l$ and $r$ ($1 \\leq l \\leq r \\leq 10^{18}$)\u00a0\u2014 the range in which you need to find the sum.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the answer modulo $1000000007$ ($10^9+7$).\n\n\n-----Examples-----\nInput\n1 3\n\nOutput\n7\nInput\n5 14\n\nOutput\n105\nInput\n88005553535 99999999999\n\nOutput\n761141116\n\n\n-----Note-----\n\nIn the first example, the answer is the sum of the first three numbers written out ($1 + 2 + 4 = 7$).\n\nIn the second example, the numbers with numbers from $5$ to $14$: $5, 7, 9, 6, 8, 10, 12, 14, 16, 18$. Their sum is $105$."} +{"id": "00502", "text": "Arpa is taking a geometry exam. Here is the last problem of the exam.\n\nYou are given three points a, b, c.\n\nFind a point and an angle such that if we rotate the page around the point by the angle, the new position of a is the same as the old position of b, and the new position of b is the same as the old position of c.\n\nArpa is doubting if the problem has a solution or not (i.e. if there exists a point and an angle satisfying the condition). Help Arpa determine if the question has a solution or not.\n\n\n-----Input-----\n\nThe only line contains six integers a_{x}, a_{y}, b_{x}, b_{y}, c_{x}, c_{y} (|a_{x}|, |a_{y}|, |b_{x}|, |b_{y}|, |c_{x}|, |c_{y}| \u2264 10^9). It's guaranteed that the points are distinct.\n\n\n-----Output-----\n\nPrint \"Yes\" if the problem has a solution, \"No\" otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n0 1 1 1 1 0\n\nOutput\nYes\n\nInput\n1 1 0 0 1000 1000\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first sample test, rotate the page around (0.5, 0.5) by $90^{\\circ}$.\n\nIn the second sample test, you can't find any solution."} +{"id": "00503", "text": "Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.\n\nHe wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.\n\nA subsequence of length three is a combination of three such indexes i_1, i_2, i_3, that 1 \u2264 i_1 < i_2 < i_3 \u2264 n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.\n\nA geometric progression with common ratio k is a sequence of numbers of the form b\u00b7k^0, b\u00b7k^1, ..., b\u00b7k^{r} - 1.\n\nPolycarp is only three years old, so he can not calculate this number himself. Help him to do it.\n\n\n-----Input-----\n\nThe first line of the input contains two integers, n and k (1 \u2264 n, k \u2264 2\u00b710^5), showing how many numbers Polycarp's sequence has and his favorite number.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9) \u2014 elements of the sequence.\n\n\n-----Output-----\n\nOutput a single number \u2014 the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.\n\n\n-----Examples-----\nInput\n5 2\n1 1 2 2 4\n\nOutput\n4\nInput\n3 1\n1 1 1\n\nOutput\n1\nInput\n10 3\n1 2 6 2 3 6 9 18 3 9\n\nOutput\n6\n\n\n-----Note-----\n\nIn the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4."} +{"id": "00504", "text": "Recently Max has got himself into popular CCG \"BrainStone\". As \"BrainStone\" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:\n\nMax owns n creatures, i-th of them can be described with two numbers \u2014 its health hp_{i} and its damage dmg_{i}. Max also has two types of spells in stock: Doubles health of the creature (hp_{i} := hp_{i}\u00b72); Assigns value of health of the creature to its damage (dmg_{i} := hp_{i}). \n\nSpell of first type can be used no more than a times in total, of the second type \u2014 no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.\n\nMax is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.\n\n\n-----Input-----\n\nThe first line contains three integers n, a, b (1 \u2264 n \u2264 2\u00b710^5, 0 \u2264 a \u2264 20, 0 \u2264 b \u2264 2\u00b710^5) \u2014 the number of creatures, spells of the first type and spells of the second type, respectively.\n\nThe i-th of the next n lines contain two number hp_{i} and dmg_{i} (1 \u2264 hp_{i}, dmg_{i} \u2264 10^9) \u2014 description of the i-th creature.\n\n\n-----Output-----\n\nPrint single integer \u2014 maximum total damage creatures can deal.\n\n\n-----Examples-----\nInput\n2 1 1\n10 15\n6 1\n\nOutput\n27\n\nInput\n3 0 3\n10 8\n7 11\n5 2\n\nOutput\n26\n\n\n\n-----Note-----\n\nIn the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6\u00b72 = 27.\n\nIn the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26."} +{"id": "00505", "text": "The Robot is in a rectangular maze of size n \u00d7 m. Each cell of the maze is either empty or occupied by an obstacle. The Robot can move between neighboring cells on the side left (the symbol \"L\"), right (the symbol \"R\"), up (the symbol \"U\") or down (the symbol \"D\"). The Robot can move to the cell only if it is empty. Initially, the Robot is in the empty cell.\n\nYour task is to find lexicographically minimal Robot's cycle with length exactly k, which begins and ends in the cell where the Robot was initially. It is allowed to the Robot to visit any cell many times (including starting).\n\nConsider that Robot's way is given as a line which consists of symbols \"L\", \"R\", \"U\" and \"D\". For example, if firstly the Robot goes down, then left, then right and up, it means that his way is written as \"DLRU\".\n\nIn this task you don't need to minimize the length of the way. Find the minimum lexicographical (in alphabet order as in the dictionary) line which satisfies requirements above.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and k (1 \u2264 n, m \u2264 1000, 1 \u2264 k \u2264 10^6) \u2014 the size of the maze and the length of the cycle. \n\nEach of the following n lines contains m symbols \u2014 the description of the maze. If the symbol equals to \".\" the current cell is empty. If the symbol equals to \"*\" the current cell is occupied by an obstacle. If the symbol equals to \"X\" then initially the Robot is in this cell and it is empty. It is guaranteed that the symbol \"X\" is found in the maze exactly once. \n\n\n-----Output-----\n\nPrint the lexicographically minimum Robot's way with the length exactly k, which starts and ends in the cell where initially Robot is. If there is no such way, print \"IMPOSSIBLE\"(without quotes).\n\n\n-----Examples-----\nInput\n2 3 2\n.**\nX..\n\nOutput\nRL\n\nInput\n5 6 14\n..***.\n*...X.\n..*...\n..*.**\n....*.\n\nOutput\nDLDDLLLRRRUURU\n\nInput\n3 3 4\n***\n*X*\n***\n\nOutput\nIMPOSSIBLE\n\n\n\n-----Note-----\n\nIn the first sample two cyclic ways for the Robot with the length 2 exist \u2014 \"UD\" and \"RL\". The second cycle is lexicographically less. \n\nIn the second sample the Robot should move in the following way: down, left, down, down, left, left, left, right, right, right, up, up, right, up. \n\nIn the third sample the Robot can't move to the neighboring cells, because they are occupied by obstacles."} +{"id": "00506", "text": "One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm \u00d7 b mm sheet of paper (a > b). Usually the first step in making an origami is making a square piece of paper from the rectangular sheet by folding the sheet along the bisector of the right angle, and cutting the excess part.\n\n [Image] \n\nAfter making a paper ship from the square piece, Vasya looked on the remaining (a - b) mm \u00d7 b mm strip of paper. He got the idea to use this strip of paper in the same way to make an origami, and then use the remainder (if it exists) and so on. At the moment when he is left with a square piece of paper, he will make the last ship from it and stop.\n\nCan you determine how many ships Vasya will make during the lesson?\n\n\n-----Input-----\n\nThe first line of the input contains two integers a, b (1 \u2264 b < a \u2264 10^12) \u2014 the sizes of the original sheet of paper.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of ships that Vasya will make.\n\n\n-----Examples-----\nInput\n2 1\n\nOutput\n2\n\nInput\n10 7\n\nOutput\n6\n\nInput\n1000000000000 1\n\nOutput\n1000000000000\n\n\n\n-----Note-----\n\nPictures to the first and second sample test.\n\n [Image]"} +{"id": "00507", "text": "Sengoku still remembers the mysterious \"colourful meteoroids\" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.\n\nOn that night, Sengoku constructed a permutation p_1, p_2, ..., p_{n} of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a_1, a_2, ..., a_{n} and b_1, b_2, ..., b_{n} respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 \u2264 i \u2264 n) exists, such that a_{i} \u2260 b_{i} holds.\n\nWell, she almost had it all \u2014 each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 \u2264 i \u2264 n) such that a_{i} \u2260 p_{i}, and exactly one j (1 \u2264 j \u2264 n) such that b_{j} \u2260 p_{j}.\n\nFor now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.\n\n\n-----Input-----\n\nThe first line of input contains a positive integer n (2 \u2264 n \u2264 1 000) \u2014 the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n) \u2014 the sequence of colours in the first meteor outburst.\n\nThe third line contains n space-separated integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 n) \u2014 the sequence of colours in the second meteor outburst. At least one i (1 \u2264 i \u2264 n) exists, such that a_{i} \u2260 b_{i} holds.\n\n\n-----Output-----\n\nOutput n space-separated integers p_1, p_2, ..., p_{n}, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.\n\nInput guarantees that such permutation exists.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 3\n1 2 5 4 5\n\nOutput\n1 2 5 4 3\n\nInput\n5\n4 4 2 3 1\n5 4 5 3 1\n\nOutput\n5 4 2 3 1\n\nInput\n4\n1 1 3 4\n1 4 3 4\n\nOutput\n1 2 3 4\n\n\n\n-----Note-----\n\nIn the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.\n\nIn the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints."} +{"id": "00508", "text": "On one quiet day all of sudden Mister B decided to draw angle a on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex n-gon (regular convex polygon with n sides).\n\nThat's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices v_1, v_2, v_3 such that the angle $\\angle v_{1} v_{2} v_{3}$ (where v_2 is the vertex of the angle, and v_1 and v_3 lie on its sides) is as close as possible to a. In other words, the value $|\\angle v_{1} v_{2} v_{3} - a|$ should be minimum possible.\n\nIf there are many optimal solutions, Mister B should be satisfied with any of them.\n\n\n-----Input-----\n\nFirst and only line contains two space-separated integers n and a (3 \u2264 n \u2264 10^5, 1 \u2264 a \u2264 180)\u00a0\u2014 the number of vertices in the polygon and the needed angle, in degrees.\n\n\n-----Output-----\n\nPrint three space-separated integers: the vertices v_1, v_2, v_3, which form $\\angle v_{1} v_{2} v_{3}$. If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to n in clockwise order.\n\n\n-----Examples-----\nInput\n3 15\n\nOutput\n1 2 3\n\nInput\n4 67\n\nOutput\n2 1 3\n\nInput\n4 68\n\nOutput\n4 1 2\n\n\n\n-----Note-----\n\nIn first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct.\n\nVertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since |45 - 67| < |90 - 67|. Other correct answers are: \"3 1 2\", \"3 2 4\", \"4 2 3\", \"4 3 1\", \"1 3 4\", \"1 4 2\", \"2 4 1\", \"4 1 3\", \"3 1 4\", \"3 4 2\", \"2 4 3\", \"2 3 1\", \"1 3 2\", \"1 2 4\", \"4 2 1\".\n\nIn third sample test, on the contrary, the angle of 90 degrees was chosen, since |90 - 68| < |45 - 68|. Other correct answers are: \"2 1 4\", \"3 2 1\", \"1 2 3\", \"4 3 2\", \"2 3 4\", \"1 4 3\", \"3 4 1\"."} +{"id": "00509", "text": "Petr has just bought a new car. He's just arrived at the most known Petersburg's petrol station to refuel it when he suddenly discovered that the petrol tank is secured with a combination lock! The lock has a scale of $360$ degrees and a pointer which initially points at zero:\n\n [Image] \n\nPetr called his car dealer, who instructed him to rotate the lock's wheel exactly $n$ times. The $i$-th rotation should be $a_i$ degrees, either clockwise or counterclockwise, and after all $n$ rotations the pointer should again point at zero.\n\nThis confused Petr a little bit as he isn't sure which rotations should be done clockwise and which should be done counterclockwise. As there are many possible ways of rotating the lock, help him and find out whether there exists at least one, such that after all $n$ rotations the pointer will point at zero again.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\leq n \\leq 15$) \u2014 the number of rotations.\n\nEach of the following $n$ lines contains one integer $a_i$ ($1 \\leq a_i \\leq 180$) \u2014 the angle of the $i$-th rotation in degrees.\n\n\n-----Output-----\n\nIf it is possible to do all the rotations so that the pointer will point at zero after all of them are performed, print a single word \"YES\". Otherwise, print \"NO\". Petr will probably buy a new car in this case.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n3\n10\n20\n30\n\nOutput\nYES\n\nInput\n3\n10\n10\n10\n\nOutput\nNO\n\nInput\n3\n120\n120\n120\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example, we can achieve our goal by applying the first and the second rotation clockwise, and performing the third rotation counterclockwise.\n\nIn the second example, it's impossible to perform the rotations in order to make the pointer point at zero in the end.\n\nIn the third example, Petr can do all three rotations clockwise. In this case, the whole wheel will be rotated by $360$ degrees clockwise and the pointer will point at zero again."} +{"id": "00510", "text": "Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad.\n\nThe rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions $a$, $b$ and $c$ respectively. At the end of the performance, the distance between each pair of ropewalkers was at least $d$.\n\nRopewalkers can walk on the rope. In one second, only one ropewalker can change his position. Every ropewalker can change his position exactly by $1$ (i. e. shift by $1$ to the left or right direction on the rope). Agafon, Boniface and Konrad can not move at the same time (Only one of them can move at each moment). Ropewalkers can be at the same positions at the same time and can \"walk past each other\".\n\nYou should find the minimum duration (in seconds) of the performance. In other words, find the minimum number of seconds needed so that the distance between each pair of ropewalkers can be greater or equal to $d$.\n\nRopewalkers can walk to negative coordinates, due to the rope is infinite to both sides.\n\n\n-----Input-----\n\nThe only line of the input contains four integers $a$, $b$, $c$, $d$ ($1 \\le a, b, c, d \\le 10^9$). It is possible that any two (or all three) ropewalkers are in the same position at the beginning of the performance.\n\n\n-----Output-----\n\nOutput one integer \u2014 the minimum duration (in seconds) of the performance.\n\n\n-----Examples-----\nInput\n5 2 6 3\n\nOutput\n2\n\nInput\n3 1 5 6\n\nOutput\n8\n\nInput\n8 3 3 2\n\nOutput\n2\n\nInput\n2 3 10 4\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example: in the first two seconds Konrad moves for 2 positions to the right (to the position $8$), while Agafon and Boniface stay at their positions. Thus, the distance between Agafon and Boniface will be $|5 - 2| = 3$, the distance between Boniface and Konrad will be $|2 - 8| = 6$ and the distance between Agafon and Konrad will be $|5 - 8| = 3$. Therefore, all three pairwise distances will be at least $d=3$, so the performance could be finished within 2 seconds."} +{"id": "00511", "text": "Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b. \n\nVasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly.\n\n\n-----Input-----\n\nThe first line contains two integer numbers x and y (1 \u2264 x, y \u2264 10^12).\n\n\n-----Output-----\n\nPrint f(x, y).\n\n\n-----Examples-----\nInput\n3 5\n\nOutput\n3\n\nInput\n6 3\n\nOutput\n1"} +{"id": "00512", "text": "There is a building with 2N floors, numbered 1, 2, \\ldots, 2N from bottom to top.\nThe elevator in this building moved from Floor 1 to Floor 2N just once.\nOn the way, N persons got on and off the elevator. Each person i (1 \\leq i \\leq N) got on at Floor A_i and off at Floor B_i. Here, 1 \\leq A_i < B_i \\leq 2N, and just one person got on or off at each floor.\nAdditionally, because of their difficult personalities, the following condition was satisfied:\n - Let C_i (= B_i - A_i - 1) be the number of times, while Person i were on the elevator, other persons got on or off. Then, the following holds:\n - If there was a moment when both Person i and Person j were on the elevator, C_i = C_j.\nWe recorded the sequences A and B, but unfortunately, we have lost some of the records. If the record of A_i or B_i is lost, it will be given to you as -1.\nAdditionally, the remaining records may be incorrect.\nDetermine whether there is a pair of A and B that is consistent with the remaining records.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - A_i = -1 or 1 \\leq A_i \\leq 2N.\n - B_i = -1 or 1 \\leq B_i \\leq 2N.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 B_1\nA_2 B_2\n:\nA_N B_N\n\n-----Output-----\nIf there is a pair of A and B that is consistent with the remaining records, print Yes; otherwise, print No.\n\n-----Sample Input-----\n3\n1 -1\n-1 4\n-1 6\n\n-----Sample Output-----\nYes\n\nFor example, if B_1 = 3, A_2 = 2, and A_3 = 5, all the requirements are met.\nIn this case, there is a moment when both Person 1 and Person 2 were on the elevator, which is fine since C_1 = C_2 = 1."} +{"id": "00513", "text": "Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, except for the average of these nine points. In other words, there must be three integers x_1, x_2, x_3 and three more integers y_1, y_2, y_3, such that x_1 < x_2 < x_3, y_1 < y_2 < y_3 and the eight point set consists of all points (x_{i}, y_{j}) (1 \u2264 i, j \u2264 3), except for point (x_2, y_2).\n\nYou have a set of eight points. Find out if Gerald can use this set?\n\n\n-----Input-----\n\nThe input consists of eight lines, the i-th line contains two space-separated integers x_{i} and y_{i} (0 \u2264 x_{i}, y_{i} \u2264 10^6). You do not have any other conditions for these points.\n\n\n-----Output-----\n\nIn a single line print word \"respectable\", if the given set of points corresponds to Gerald's decency rules, and \"ugly\" otherwise.\n\n\n-----Examples-----\nInput\n0 0\n0 1\n0 2\n1 0\n1 2\n2 0\n2 1\n2 2\n\nOutput\nrespectable\n\nInput\n0 0\n1 0\n2 0\n3 0\n4 0\n5 0\n6 0\n7 0\n\nOutput\nugly\n\nInput\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n\nOutput\nugly"} +{"id": "00514", "text": "Adilbek was assigned to a special project. For Adilbek it means that he has $n$ days to run a special program and provide its results. But there is a problem: the program needs to run for $d$ days to calculate the results.\n\nFortunately, Adilbek can optimize the program. If he spends $x$ ($x$ is a non-negative integer) days optimizing the program, he will make the program run in $\\left\\lceil \\frac{d}{x + 1} \\right\\rceil$ days ($\\left\\lceil a \\right\\rceil$ is the ceiling function: $\\left\\lceil 2.4 \\right\\rceil = 3$, $\\left\\lceil 2 \\right\\rceil = 2$). The program cannot be run and optimized simultaneously, so the total number of days he will spend is equal to $x + \\left\\lceil \\frac{d}{x + 1} \\right\\rceil$.\n\nWill Adilbek be able to provide the generated results in no more than $n$ days?\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\le T \\le 50$) \u2014 the number of test cases.\n\nThe next $T$ lines contain test cases \u2013 one per line. Each line contains two integers $n$ and $d$ ($1 \\le n \\le 10^9$, $1 \\le d \\le 10^9$) \u2014 the number of days before the deadline and the number of days the program runs.\n\n\n-----Output-----\n\nPrint $T$ answers \u2014 one per test case. For each test case print YES (case insensitive) if Adilbek can fit in $n$ days or NO (case insensitive) otherwise.\n\n\n-----Example-----\nInput\n3\n1 1\n4 5\n5 11\n\nOutput\nYES\nYES\nNO\n\n\n\n-----Note-----\n\nIn the first test case, Adilbek decides not to optimize the program at all, since $d \\le n$.\n\nIn the second test case, Adilbek can spend $1$ day optimizing the program and it will run $\\left\\lceil \\frac{5}{2} \\right\\rceil = 3$ days. In total, he will spend $4$ days and will fit in the limit.\n\nIn the third test case, it's impossible to fit in the limit. For example, if Adilbek will optimize the program $2$ days, it'll still work $\\left\\lceil \\frac{11}{2+1} \\right\\rceil = 4$ days."} +{"id": "00515", "text": "Apart from Nian, there is a daemon named Sui, which terrifies children and causes them to become sick. Parents give their children money wrapped in red packets and put them under the pillow, so that when Sui tries to approach them, it will be driven away by the fairies inside.\n\nBig Banban is hesitating over the amount of money to give out. He considers loops to be lucky since it symbolizes unity and harmony.\n\nHe would like to find a positive integer n not greater than 10^18, such that there are exactly k loops in the decimal representation of n, or determine that such n does not exist.\n\nA loop is a planar area enclosed by lines in the digits' decimal representation written in Arabic numerals. For example, there is one loop in digit 4, two loops in 8 and no loops in 5. Refer to the figure below for all exact forms.\n\n $0123456789$ \n\n\n-----Input-----\n\nThe first and only line contains an integer k (1 \u2264 k \u2264 10^6)\u00a0\u2014 the desired number of loops.\n\n\n-----Output-----\n\nOutput an integer\u00a0\u2014 if no such n exists, output -1; otherwise output any such n. In the latter case, your output should be a positive decimal integer not exceeding 10^18.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n462\nInput\n6\n\nOutput\n8080"} +{"id": "00516", "text": "Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you?\n\nLeha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark \"?\". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s=\"ab?b\" as a result, it will appear in t=\"aabrbb\" as a substring.\n\nGuaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol \"?\" should be considered equal to any other symbol.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 m \u2264 1000) \u2014 the length of the string s and the length of the string t correspondingly.\n\nThe second line contains n lowercase English letters \u2014 string s.\n\nThe third line contains m lowercase English letters \u2014 string t.\n\n\n-----Output-----\n\nIn the first line print single integer k \u2014 the minimal number of symbols that need to be replaced.\n\nIn the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.\n\n\n-----Examples-----\nInput\n3 5\nabc\nxaybz\n\nOutput\n2\n2 3 \n\nInput\n4 10\nabcd\nebceabazcd\n\nOutput\n1\n2"} +{"id": "00517", "text": "A tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n.\n\nLimak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the tree\u00a0\u2014 he can tell you only three values n, d and h:\n\n The tree had exactly n vertices. The tree had diameter d. In other words, d was the biggest distance between two vertices. Limak also remembers that he once rooted the tree in vertex 1 and after that its height was h. In other words, h was the biggest distance between vertex 1 and some other vertex. \n\nThe distance between two vertices of the tree is the number of edges on the simple path between them.\n\nHelp Limak to restore his tree. Check whether there exists a tree satisfying the given conditions. Find any such tree and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree\u00a0\u2013 in this case print \"-1\".\n\n\n-----Input-----\n\nThe first line contains three integers n, d and h (2 \u2264 n \u2264 100 000, 1 \u2264 h \u2264 d \u2264 n - 1)\u00a0\u2014 the number of vertices, diameter, and height after rooting in vertex 1, respectively.\n\n\n-----Output-----\n\nIf there is no tree matching what Limak remembers, print the only line with \"-1\" (without the quotes).\n\nOtherwise, describe any tree matching Limak's description. Print n - 1 lines, each with two space-separated integers\u00a0\u2013 indices of vertices connected by an edge. If there are many valid trees, print any of them. You can print edges in any order.\n\n\n-----Examples-----\nInput\n5 3 2\n\nOutput\n1 2\n1 3\n3 4\n3 5\nInput\n8 5 2\n\nOutput\n-1\n\nInput\n8 4 2\n\nOutput\n4 8\n5 7\n2 3\n8 1\n2 1\n5 6\n1 5\n\n\n\n-----Note-----\n\nBelow you can see trees printed to the output in the first sample and the third sample.\n\n [Image]"} +{"id": "00518", "text": "NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: [Image] \n\nIt turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself.\n\nHe managed to calculate the number of outer circles $n$ and the radius of the inner circle $r$. NN thinks that, using this information, you can exactly determine the radius of the outer circles $R$ so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. \n\nHelp NN find the required radius for building the required picture.\n\n\n-----Input-----\n\nThe first and the only line of the input file contains two numbers $n$ and $r$ ($3 \\leq n \\leq 100$, $1 \\leq r \\leq 100$)\u00a0\u2014 the number of the outer circles and the radius of the inner circle respectively.\n\n\n-----Output-----\n\nOutput a single number $R$\u00a0\u2014 the radius of the outer circle required for building the required picture. \n\nYour answer will be accepted if its relative or absolute error does not exceed $10^{-6}$.\n\nFormally, if your answer is $a$ and the jury's answer is $b$. Your answer is accepted if and only when $\\frac{|a-b|}{max(1, |b|)} \\le 10^{-6}$.\n\n\n-----Examples-----\nInput\n3 1\n\nOutput\n6.4641016\n\nInput\n6 1\n\nOutput\n1.0000000\n\nInput\n100 100\n\nOutput\n3.2429391"} +{"id": "00519", "text": "Harry Potter and He-Who-Must-Not-Be-Named engaged in a fight to the death once again. This time they are located at opposite ends of the corridor of length l. Two opponents simultaneously charge a deadly spell in the enemy. We know that the impulse of Harry's magic spell flies at a speed of p meters per second, and the impulse of You-Know-Who's magic spell flies at a speed of q meters per second.\n\nThe impulses are moving through the corridor toward each other, and at the time of the collision they turn round and fly back to those who cast them without changing their original speeds. Then, as soon as the impulse gets back to it's caster, the wizard reflects it and sends again towards the enemy, without changing the original speed of the impulse.\n\nSince Harry has perfectly mastered the basics of magic, he knows that after the second collision both impulses will disappear, and a powerful explosion will occur exactly in the place of their collision. However, the young wizard isn't good at math, so he asks you to calculate the distance from his position to the place of the second meeting of the spell impulses, provided that the opponents do not change positions during the whole fight.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer l (1 \u2264 l \u2264 1 000)\u00a0\u2014 the length of the corridor where the fight takes place.\n\nThe second line contains integer p, the third line contains integer q (1 \u2264 p, q \u2264 500)\u00a0\u2014 the speeds of magical impulses for Harry Potter and He-Who-Must-Not-Be-Named, respectively.\n\n\n-----Output-----\n\nPrint a single real number\u00a0\u2014 the distance from the end of the corridor, where Harry is located, to the place of the second meeting of the spell impulses. Your answer will be considered correct if its absolute or relative error will not exceed 10^{ - 4}. \n\nNamely: let's assume that your answer equals a, and the answer of the jury is b. The checker program will consider your answer correct if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-4}$.\n\n\n-----Examples-----\nInput\n100\n50\n50\n\nOutput\n50\n\nInput\n199\n60\n40\n\nOutput\n119.4\n\n\n\n-----Note-----\n\nIn the first sample the speeds of the impulses are equal, so both of their meetings occur exactly in the middle of the corridor."} +{"id": "00520", "text": "There is the faculty of Computer Science in Berland. In the social net \"TheContact!\" for each course of this faculty there is the special group whose name equals the year of university entrance of corresponding course of students at the university. \n\nEach of students joins the group of his course and joins all groups for which the year of student's university entrance differs by no more than x from the year of university entrance of this student, where x \u2014 some non-negative integer. A value x is not given, but it can be uniquely determined from the available data. Note that students don't join other groups. \n\nYou are given the list of groups which the student Igor joined. According to this information you need to determine the year of Igor's university entrance.\n\n\n-----Input-----\n\nThe first line contains the positive odd integer n (1 \u2264 n \u2264 5) \u2014 the number of groups which Igor joined. \n\nThe next line contains n distinct integers a_1, a_2, ..., a_{n} (2010 \u2264 a_{i} \u2264 2100) \u2014 years of student's university entrance for each group in which Igor is the member.\n\nIt is guaranteed that the input data is correct and the answer always exists. Groups are given randomly.\n\n\n-----Output-----\n\nPrint the year of Igor's university entrance. \n\n\n-----Examples-----\nInput\n3\n2014 2016 2015\n\nOutput\n2015\n\nInput\n1\n2050\n\nOutput\n2050\n\n\n\n-----Note-----\n\nIn the first test the value x = 1. Igor entered the university in 2015. So he joined groups members of which are students who entered the university in 2014, 2015 and 2016.\n\nIn the second test the value x = 0. Igor entered only the group which corresponds to the year of his university entrance."} +{"id": "00521", "text": "Overlooking the captivating blend of myriads of vernal hues, Arkady the painter lays out a long, long canvas.\n\nArkady has a sufficiently large amount of paint of three colours: cyan, magenta, and yellow. On the one-dimensional canvas split into n consecutive segments, each segment needs to be painted in one of the colours.\n\nArkady has already painted some (possibly none or all) segments and passes the paintbrush to you. You are to determine whether there are at least two ways of colouring all the unpainted segments so that no two adjacent segments are of the same colour. Two ways are considered different if and only if a segment is painted in different colours in them.\n\n\n-----Input-----\n\nThe first line contains a single positive integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the length of the canvas.\n\nThe second line contains a string s of n characters, the i-th of which is either 'C' (denoting a segment painted in cyan), 'M' (denoting one painted in magenta), 'Y' (one painted in yellow), or '?' (an unpainted one).\n\n\n-----Output-----\n\nIf there are at least two different ways of painting, output \"Yes\"; otherwise output \"No\" (both without quotes).\n\nYou can print each character in any case (upper or lower).\n\n\n-----Examples-----\nInput\n5\nCY??Y\n\nOutput\nYes\n\nInput\n5\nC?C?Y\n\nOutput\nYes\n\nInput\n5\n?CYC?\n\nOutput\nYes\n\nInput\n5\nC??MM\n\nOutput\nNo\n\nInput\n3\nMMY\n\nOutput\nNo\n\n\n\n-----Note-----\n\nFor the first example, there are exactly two different ways of colouring: CYCMY and CYMCY.\n\nFor the second example, there are also exactly two different ways of colouring: CMCMY and CYCMY.\n\nFor the third example, there are four ways of colouring: MCYCM, MCYCY, YCYCM, and YCYCY.\n\nFor the fourth example, no matter how the unpainted segments are coloured, the existing magenta segments will prevent the painting from satisfying the requirements. The similar is true for the fifth example."} +{"id": "00522", "text": "Let $f_{x} = c^{2x-6} \\cdot f_{x-1} \\cdot f_{x-2} \\cdot f_{x-3}$ for $x \\ge 4$.\n\nYou have given integers $n$, $f_{1}$, $f_{2}$, $f_{3}$, and $c$. Find $f_{n} \\bmod (10^{9}+7)$.\n\n\n-----Input-----\n\nThe only line contains five integers $n$, $f_{1}$, $f_{2}$, $f_{3}$, and $c$ ($4 \\le n \\le 10^{18}$, $1 \\le f_{1}$, $f_{2}$, $f_{3}$, $c \\le 10^{9}$).\n\n\n-----Output-----\n\nPrint $f_{n} \\bmod (10^{9} + 7)$.\n\n\n-----Examples-----\nInput\n5 1 2 5 3\n\nOutput\n72900\n\nInput\n17 97 41 37 11\n\nOutput\n317451037\n\n\n\n-----Note-----\n\nIn the first example, $f_{4} = 90$, $f_{5} = 72900$.\n\nIn the second example, $f_{17} \\approx 2.28 \\times 10^{29587}$."} +{"id": "00523", "text": "Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings \"pop\", \"noon\", \"x\", and \"kkkkkk\" are palindromes, while strings \"moon\", \"tv\", and \"abab\" are not. An empty string is also a palindrome.\n\nGildong loves this concept so much, so he wants to play with it. He has $n$ distinct strings of equal length $m$. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 100$, $1 \\le m \\le 50$) \u2014 the number of strings and the length of each string.\n\nNext $n$ lines contain a string of length $m$ each, consisting of lowercase Latin letters only. All strings are distinct.\n\n\n-----Output-----\n\nIn the first line, print the length of the longest palindrome string you made.\n\nIn the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.\n\n\n-----Examples-----\nInput\n3 3\ntab\none\nbat\n\nOutput\n6\ntabbat\n\nInput\n4 2\noo\nox\nxo\nxx\n\nOutput\n6\noxxxxo\n\nInput\n3 5\nhello\ncodef\norces\n\nOutput\n0\n\n\nInput\n9 4\nabab\nbaba\nabcd\nbcde\ncdef\ndefg\nwxyz\nzyxw\nijji\n\nOutput\n20\nababwxyzijjizyxwbaba\n\n\n\n-----Note-----\n\nIn the first example, \"battab\" is also a valid answer.\n\nIn the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.\n\nIn the third example, the empty string is the only valid palindrome string."} +{"id": "00524", "text": "Let's call a list of positive integers $a_0, a_1, ..., a_{n-1}$ a power sequence if there is a positive integer $c$, so that for every $0 \\le i \\le n-1$ then $a_i = c^i$.\n\nGiven a list of $n$ positive integers $a_0, a_1, ..., a_{n-1}$, you are allowed to: Reorder the list (i.e. pick a permutation $p$ of $\\{0,1,...,n - 1\\}$ and change $a_i$ to $a_{p_i}$), then Do the following operation any number of times: pick an index $i$ and change $a_i$ to $a_i - 1$ or $a_i + 1$ (i.e. increment or decrement $a_i$ by $1$) with a cost of $1$. \n\nFind the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($3 \\le n \\le 10^5$).\n\nThe second line contains $n$ integers $a_0, a_1, ..., a_{n-1}$ ($1 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint the minimum cost to transform $a_0, a_1, ..., a_{n-1}$ into a power sequence.\n\n\n-----Examples-----\nInput\n3\n1 3 2\n\nOutput\n1\n\nInput\n3\n1000000000 1000000000 1000000000\n\nOutput\n1999982505\n\n\n\n-----Note-----\n\nIn the first example, we first reorder $\\{1, 3, 2\\}$ into $\\{1, 2, 3\\}$, then increment $a_2$ to $4$ with cost $1$ to get a power sequence $\\{1, 2, 4\\}$."} +{"id": "00525", "text": "Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!\n\nA password is an array $a$ of $n$ positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index $i$ such that $1 \\leq i < n$ and $a_{i} \\neq a_{i+1}$, delete both $a_i$ and $a_{i+1}$ from the array and put $a_{i}+a_{i+1}$ in their place. \n\nFor example, for array $[7, 4, 3, 7]$ you can choose $i = 2$ and the array will become $[7, 4+3, 7] = [7, 7, 7]$. Note that in this array you can't apply this operation anymore.\n\nNotice that one operation will decrease the size of the password by $1$. What is the shortest possible length of the password after some number (possibly $0$) of operations?\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 100$). Description of the test cases follows.\n\nThe first line of each test case contains an integer $n$ ($1 \\leq n \\leq 2 \\cdot 10^5$)\u00a0\u2014 the length of the password.\n\nThe second line of each test case contains $n$ integers $a_{1},a_{2},\\dots,a_{n}$ ($1 \\leq a_{i} \\leq 10^9$)\u00a0\u2014 the initial contents of your password.\n\nThe sum of $n$ over all test cases will not exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each password, print one integer: the shortest possible length of the password after some number of operations.\n\n\n-----Example-----\nInput\n2\n4\n2 1 3 1\n2\n420 420\n\nOutput\n1\n2\n\n\n\n-----Note-----\n\nIn the first test case, you can do the following to achieve a length of $1$:\n\nPick $i=2$ to get $[2, 4, 1]$\n\nPick $i=1$ to get $[6, 1]$\n\nPick $i=1$ to get $[7]$\n\nIn the second test case, you can't perform any operations because there is no valid $i$ that satisfies the requirements mentioned above."} +{"id": "00526", "text": "Student Dima from Kremland has a matrix $a$ of size $n \\times m$ filled with non-negative integers.\n\nHe wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him!\n\nFormally, he wants to choose an integers sequence $c_1, c_2, \\ldots, c_n$ ($1 \\leq c_j \\leq m$) so that the inequality $a_{1, c_1} \\oplus a_{2, c_2} \\oplus \\ldots \\oplus a_{n, c_n} > 0$ holds, where $a_{i, j}$ is the matrix element from the $i$-th row and the $j$-th column.\n\nHere $x \\oplus y$ denotes the bitwise XOR operation of integers $x$ and $y$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\leq n, m \\leq 500$)\u00a0\u2014 the number of rows and the number of columns in the matrix $a$.\n\nEach of the next $n$ lines contains $m$ integers: the $j$-th integer in the $i$-th line is the $j$-th element of the $i$-th row of the matrix $a$, i.e. $a_{i, j}$ ($0 \\leq a_{i, j} \\leq 1023$). \n\n\n-----Output-----\n\nIf there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print \"NIE\".\n\nOtherwise print \"TAK\" in the first line, in the next line print $n$ integers $c_1, c_2, \\ldots c_n$ ($1 \\leq c_j \\leq m$), so that the inequality $a_{1, c_1} \\oplus a_{2, c_2} \\oplus \\ldots \\oplus a_{n, c_n} > 0$ holds. \n\nIf there is more than one possible answer, you may output any.\n\n\n-----Examples-----\nInput\n3 2\n0 0\n0 0\n0 0\n\nOutput\nNIE\n\nInput\n2 3\n7 7 7\n7 7 10\n\nOutput\nTAK\n1 3 \n\n\n\n-----Note-----\n\nIn the first example, all the numbers in the matrix are $0$, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero.\n\nIn the second example, the selected numbers are $7$ (the first number in the first line) and $10$ (the third number in the second line), $7 \\oplus 10 = 13$, $13$ is more than $0$, so the answer is found."} +{"id": "00527", "text": "Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.\n - Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\\ldots{s'}_i (the first i characters in s').\n\n-----Notes-----\n - A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.\n\n-----Constraints-----\n - 1 \\leq |s| \\leq 10^5\n - 1 \\leq |t| \\leq 10^5\n - s and t consists of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\ns\nt\n\n-----Output-----\nIf there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.\n\n-----Sample Input-----\ncontest\nson\n\n-----Sample Output-----\n10\n\nt = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.\nOn the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.\nSimilarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10."} +{"id": "00528", "text": "Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).\n\nThere are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves.\n\nLet A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z.\n\nFor example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well.\n\nCan you help Limak and check if the network is reasonable? Print \"YES\" or \"NO\" accordingly, without the quotes.\n\n\n-----Input-----\n\nThe first line of the input contain two integers n and m (3 \u2264 n \u2264 150 000, $0 \\leq m \\leq \\operatorname{min}(150000, \\frac{n \\cdot(n - 1)}{2})$)\u00a0\u2014 the number of members and the number of pairs of members that are friends.\n\nThe i-th of the next m lines contains two distinct integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n, a_{i} \u2260 b_{i}). Members a_{i} and b_{i} are friends with each other. No pair of members will appear more than once in the input.\n\n\n-----Output-----\n\nIf the given network is reasonable, print \"YES\" in a single line (without the quotes). Otherwise, print \"NO\" in a single line (without the quotes).\n\n\n-----Examples-----\nInput\n4 3\n1 3\n3 4\n1 4\n\nOutput\nYES\n\nInput\n4 4\n3 1\n2 3\n3 4\n1 2\n\nOutput\nNO\n\nInput\n10 4\n4 3\n5 10\n8 9\n1 2\n\nOutput\nYES\n\nInput\n3 2\n1 2\n2 3\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is \"NO\" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not.\n\n [Image]"} +{"id": "00529", "text": "[Image] \n\n\n-----Input-----\n\nThe first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase.\n\nThe second line of the input is an integer between 0 and 26, inclusive.\n\n\n-----Output-----\n\nOutput the required string.\n\n\n-----Examples-----\nInput\nAprilFool\n14\n\nOutput\nAprILFooL"} +{"id": "00530", "text": "Yaroslav, Andrey and Roman can play cubes for hours and hours. But the game is for three, so when Roman doesn't show up, Yaroslav and Andrey play another game. \n\nRoman leaves a word for each of them. Each word consists of 2\u00b7n binary characters \"0\" or \"1\". After that the players start moving in turns. Yaroslav moves first. During a move, a player must choose an integer from 1 to 2\u00b7n, which hasn't been chosen by anybody up to that moment. Then the player takes a piece of paper and writes out the corresponding character from his string. \n\nLet's represent Yaroslav's word as s = s_1s_2... s_2n. Similarly, let's represent Andrey's word as t = t_1t_2... t_2n. Then, if Yaroslav choose number k during his move, then he is going to write out character s_{k} on the piece of paper. Similarly, if Andrey choose number r during his move, then he is going to write out character t_{r} on the piece of paper.\n\nThe game finishes when no player can make a move. After the game is over, Yaroslav makes some integer from the characters written on his piece of paper (Yaroslav can arrange these characters as he wants). Andrey does the same. The resulting numbers can contain leading zeroes. The person with the largest number wins. If the numbers are equal, the game ends with a draw.\n\nYou are given two strings s and t. Determine the outcome of the game provided that Yaroslav and Andrey play optimally well.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^6). The second line contains string s \u2014 Yaroslav's word. The third line contains string t \u2014 Andrey's word.\n\nIt is guaranteed that both words consist of 2\u00b7n characters \"0\" and \"1\".\n\n\n-----Output-----\n\nPrint \"First\", if both players play optimally well and Yaroslav wins. If Andrey wins, print \"Second\" and if the game ends with a draw, print \"Draw\". Print the words without the quotes.\n\n\n-----Examples-----\nInput\n2\n0111\n0001\n\nOutput\nFirst\n\nInput\n3\n110110\n001001\n\nOutput\nFirst\n\nInput\n3\n111000\n000111\n\nOutput\nDraw\n\nInput\n4\n01010110\n00101101\n\nOutput\nFirst\n\nInput\n4\n01100000\n10010011\n\nOutput\nSecond"} +{"id": "00531", "text": "Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some value n times, and then compute the average value to lower the error.\n\nKirill has already made his measurements, and has got the following integer values: x_1, x_2, ..., x_{n}. It is important that the values are close to each other, namely, the difference between the maximum value and the minimum value is at most 2.\n\nAnya does not want to make the measurements, however, she can't just copy the values from Kirill's work, because the error of each measurement is a random value, and this coincidence will be noted by the teacher. Anya wants to write such integer values y_1, y_2, ..., y_{n} in her work, that the following conditions are met: the average value of x_1, x_2, ..., x_{n} is equal to the average value of y_1, y_2, ..., y_{n}; all Anya's measurements are in the same bounds as all Kirill's measurements, that is, the maximum value among Anya's values is not greater than the maximum value among Kirill's values, and the minimum value among Anya's values is not less than the minimum value among Kirill's values; the number of equal measurements in Anya's work and Kirill's work is as small as possible among options with the previous conditions met. Formally, the teacher goes through all Anya's values one by one, if there is equal value in Kirill's work and it is not strike off yet, he strikes off this Anya's value and one of equal values in Kirill's work. The number of equal measurements is then the total number of strike off values in Anya's work. \n\nHelp Anya to write such a set of measurements that the conditions above are met.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100 000) \u2014 the numeber of measurements made by Kirill.\n\nThe second line contains a sequence of integers x_1, x_2, ..., x_{n} ( - 100 000 \u2264 x_{i} \u2264 100 000) \u2014 the measurements made by Kirill. It is guaranteed that the difference between the maximum and minimum values among values x_1, x_2, ..., x_{n} does not exceed 2.\n\n\n-----Output-----\n\nIn the first line print the minimum possible number of equal measurements.\n\nIn the second line print n integers y_1, y_2, ..., y_{n} \u2014 the values Anya should write. You can print the integers in arbitrary order. Keep in mind that the minimum value among Anya's values should be not less that the minimum among Kirill's values, and the maximum among Anya's values should be not greater than the maximum among Kirill's values.\n\nIf there are multiple answers, print any of them. \n\n\n-----Examples-----\nInput\n6\n-1 1 1 0 0 -1\n\nOutput\n2\n0 0 0 0 0 0 \n\nInput\n3\n100 100 101\n\nOutput\n3\n101 100 100 \n\nInput\n7\n-10 -9 -10 -8 -10 -9 -9\n\nOutput\n5\n-10 -10 -9 -9 -9 -9 -9 \n\n\n\n-----Note-----\n\nIn the first example Anya can write zeros as here measurements results. The average value is then equal to the average value of Kirill's values, and there are only two equal measurements.\n\nIn the second example Anya should write two values 100 and one value 101 (in any order), because it is the only possibility to make the average be the equal to the average of Kirill's values. Thus, all three measurements are equal.\n\nIn the third example the number of equal measurements is 5."} +{"id": "00532", "text": "Grigoriy, like the hero of one famous comedy film, found a job as a night security guard at the museum. At first night he received embosser and was to take stock of the whole exposition.\n\nEmbosser is a special devise that allows to \"print\" the text of a plastic tape. Text is printed sequentially, character by character. The device consists of a wheel with a lowercase English letters written in a circle, static pointer to the current letter and a button that print the chosen letter. At one move it's allowed to rotate the alphabetic wheel one step clockwise or counterclockwise. Initially, static pointer points to letter 'a'. Other letters are located as shown on the picture: [Image] \n\nAfter Grigoriy add new item to the base he has to print its name on the plastic tape and attach it to the corresponding exhibit. It's not required to return the wheel to its initial position with pointer on the letter 'a'.\n\nOur hero is afraid that some exhibits may become alive and start to attack him, so he wants to print the names as fast as possible. Help him, for the given string find the minimum number of rotations of the wheel required to print it.\n\n\n-----Input-----\n\nThe only line of input contains the name of some exhibit\u00a0\u2014 the non-empty string consisting of no more than 100 characters. It's guaranteed that the string consists of only lowercase English letters.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimum number of rotations of the wheel, required to print the name given in the input.\n\n\n-----Examples-----\nInput\nzeus\n\nOutput\n18\n\nInput\nmap\n\nOutput\n35\n\nInput\nares\n\nOutput\n34\n\n\n\n-----Note-----\n\n\u00a0 [Image] \n\nTo print the string from the first sample it would be optimal to perform the following sequence of rotations: from 'a' to 'z' (1 rotation counterclockwise), from 'z' to 'e' (5 clockwise rotations), from 'e' to 'u' (10 rotations counterclockwise), from 'u' to 's' (2 counterclockwise rotations). In total, 1 + 5 + 10 + 2 = 18 rotations are required."} +{"id": "00533", "text": "The final match of the Berland Football Cup has been held recently. The referee has shown $n$ yellow cards throughout the match. At the beginning of the match there were $a_1$ players in the first team and $a_2$ players in the second team.\n\nThe rules of sending players off the game are a bit different in Berland football. If a player from the first team receives $k_1$ yellow cards throughout the match, he can no longer participate in the match \u2014 he's sent off. And if a player from the second team receives $k_2$ yellow cards, he's sent off. After a player leaves the match, he can no longer receive any yellow cards. Each of $n$ yellow cards was shown to exactly one player. Even if all players from one team (or even from both teams) leave the match, the game still continues.\n\nThe referee has lost his records on who has received each yellow card. Help him to determine the minimum and the maximum number of players that could have been thrown out of the game.\n\n\n-----Input-----\n\nThe first line contains one integer $a_1$ $(1 \\le a_1 \\le 1\\,000)$ \u2014 the number of players in the first team.\n\nThe second line contains one integer $a_2$ $(1 \\le a_2 \\le 1\\,000)$ \u2014 the number of players in the second team.\n\nThe third line contains one integer $k_1$ $(1 \\le k_1 \\le 1\\,000)$ \u2014 the maximum number of yellow cards a player from the first team can receive (after receiving that many yellow cards, he leaves the game).\n\nThe fourth line contains one integer $k_2$ $(1 \\le k_2 \\le 1\\,000)$ \u2014 the maximum number of yellow cards a player from the second team can receive (after receiving that many yellow cards, he leaves the game).\n\nThe fifth line contains one integer $n$ $(1 \\le n \\le a_1 \\cdot k_1 + a_2 \\cdot k_2)$ \u2014 the number of yellow cards that have been shown during the match.\n\n\n-----Output-----\n\nPrint two integers \u2014 the minimum and the maximum number of players that could have been thrown out of the game.\n\n\n-----Examples-----\nInput\n2\n3\n5\n1\n8\n\nOutput\n0 4\n\nInput\n3\n1\n6\n7\n25\n\nOutput\n4 4\n\nInput\n6\n4\n9\n10\n89\n\nOutput\n5 9\n\n\n\n-----Note-----\n\nIn the first example it could be possible that no player left the game, so the first number in the output is $0$. The maximum possible number of players that could have been forced to leave the game is $4$ \u2014 one player from the first team, and three players from the second.\n\nIn the second example the maximum possible number of yellow cards has been shown $(3 \\cdot 6 + 1 \\cdot 7 = 25)$, so in any case all players were sent off."} +{"id": "00534", "text": "During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen. Initially the children stood in the order they entered the canteen. However, after a while the boys started feeling awkward for standing in front of the girls in the queue and they started letting the girls move forward each second. \n\nLet's describe the process more precisely. Let's say that the positions in the queue are sequentially numbered by integers from 1 to n, at that the person in the position number 1 is served first. Then, if at time x a boy stands on the i-th position and a girl stands on the (i + 1)-th position, then at time x + 1 the i-th position will have a girl and the (i + 1)-th position will have a boy. The time is given in seconds.\n\nYou've got the initial position of the children, at the initial moment of time. Determine the way the queue is going to look after t seconds.\n\n\n-----Input-----\n\nThe first line contains two integers n and t (1 \u2264 n, t \u2264 50), which represent the number of children in the queue and the time after which the queue will transform into the arrangement you need to find. \n\nThe next line contains string s, which represents the schoolchildren's initial arrangement. If the i-th position in the queue contains a boy, then the i-th character of string s equals \"B\", otherwise the i-th character equals \"G\".\n\n\n-----Output-----\n\nPrint string a, which describes the arrangement after t seconds. If the i-th position has a boy after the needed time, then the i-th character a must equal \"B\", otherwise it must equal \"G\".\n\n\n-----Examples-----\nInput\n5 1\nBGGBG\n\nOutput\nGBGGB\n\nInput\n5 2\nBGGBG\n\nOutput\nGGBGB\n\nInput\n4 1\nGGGB\n\nOutput\nGGGB"} +{"id": "00535", "text": "Makoto has a big blackboard with a positive integer $n$ written on it. He will perform the following action exactly $k$ times:\n\nSuppose the number currently written on the blackboard is $v$. He will randomly pick one of the divisors of $v$ (possibly $1$ and $v$) and replace $v$ with this divisor. As Makoto uses his famous random number generator (RNG) and as he always uses $58$ as his generator seed, each divisor is guaranteed to be chosen with equal probability.\n\nHe now wonders what is the expected value of the number written on the blackboard after $k$ steps.\n\nIt can be shown that this value can be represented as $\\frac{P}{Q}$ where $P$ and $Q$ are coprime integers and $Q \\not\\equiv 0 \\pmod{10^9+7}$. Print the value of $P \\cdot Q^{-1}$ modulo $10^9+7$.\n\n\n-----Input-----\n\nThe only line of the input contains two integers $n$ and $k$ ($1 \\leq n \\leq 10^{15}$, $1 \\leq k \\leq 10^4$).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the expected value of the number on the blackboard after $k$ steps as $P \\cdot Q^{-1} \\pmod{10^9+7}$ for $P$, $Q$ defined above.\n\n\n-----Examples-----\nInput\n6 1\n\nOutput\n3\n\nInput\n6 2\n\nOutput\n875000008\n\nInput\n60 5\n\nOutput\n237178099\n\n\n\n-----Note-----\n\nIn the first example, after one step, the number written on the blackboard is $1$, $2$, $3$ or $6$ \u2014 each occurring with equal probability. Hence, the answer is $\\frac{1+2+3+6}{4}=3$.\n\nIn the second example, the answer is equal to $1 \\cdot \\frac{9}{16}+2 \\cdot \\frac{3}{16}+3 \\cdot \\frac{3}{16}+6 \\cdot \\frac{1}{16}=\\frac{15}{8}$."} +{"id": "00536", "text": "Now it's time of Olympiads. Vanya and Egor decided to make his own team to take part in a programming Olympiad. They've been best friends ever since primary school and hopefully, that can somehow help them in teamwork.\n\nFor each team Olympiad, Vanya takes his play cards with numbers. He takes only the cards containing numbers 1 and 0. The boys are very superstitious. They think that they can do well at the Olympiad if they begin with laying all the cards in a row so that: there wouldn't be a pair of any side-adjacent cards with zeroes in a row; there wouldn't be a group of three consecutive cards containing numbers one. \n\nToday Vanya brought n cards with zeroes and m cards with numbers one. The number of cards was so much that the friends do not know how to put all those cards in the described way. Help them find the required arrangement of the cards or else tell the guys that it is impossible to arrange cards in such a way.\n\n\n-----Input-----\n\nThe first line contains two integers: n (1 \u2264 n \u2264 10^6) \u2014 the number of cards containing number 0; m (1 \u2264 m \u2264 10^6) \u2014 the number of cards containing number 1.\n\n\n-----Output-----\n\nIn a single line print the required sequence of zeroes and ones without any spaces. If such sequence is impossible to obtain, print -1.\n\n\n-----Examples-----\nInput\n1 2\n\nOutput\n101\n\nInput\n4 8\n\nOutput\n110110110101\n\nInput\n4 10\n\nOutput\n11011011011011\n\nInput\n1 5\n\nOutput\n-1"} +{"id": "00537", "text": "There are n students who have taken part in an olympiad. Now it's time to award the students.\n\nSome of them will receive diplomas, some wiil get certificates, and others won't receive anything. Students with diplomas and certificates are called winners. But there are some rules of counting the number of diplomas and certificates. The number of certificates must be exactly k times greater than the number of diplomas. The number of winners must not be greater than half of the number of all students (i.e. not be greater than half of n). It's possible that there are no winners.\n\nYou have to identify the maximum possible number of winners, according to these rules. Also for this case you have to calculate the number of students with diplomas, the number of students with certificates and the number of students who are not winners.\n\n\n-----Input-----\n\nThe first (and the only) line of input contains two integers n and k (1 \u2264 n, k \u2264 10^12), where n is the number of students and k is the ratio between the number of certificates and the number of diplomas.\n\n\n-----Output-----\n\nOutput three numbers: the number of students with diplomas, the number of students with certificates and the number of students who are not winners in case when the number of winners is maximum possible.\n\nIt's possible that there are no winners.\n\n\n-----Examples-----\nInput\n18 2\n\nOutput\n3 6 9\n\nInput\n9 10\n\nOutput\n0 0 9\n\nInput\n1000000000000 5\n\nOutput\n83333333333 416666666665 500000000002\n\nInput\n1000000000000 499999999999\n\nOutput\n1 499999999999 500000000000"} +{"id": "00538", "text": "Let quasi-palindromic number be such number that adding some leading zeros (possible none) to it produces a palindromic string. \n\nString t is called a palindrome, if it reads the same from left to right and from right to left.\n\nFor example, numbers 131 and 2010200 are quasi-palindromic, they can be transformed to strings \"131\" and \"002010200\", respectively, which are palindromes.\n\nYou are given some integer number x. Check if it's a quasi-palindromic number.\n\n\n-----Input-----\n\nThe first line contains one integer number x (1 \u2264 x \u2264 10^9). This number is given without any leading zeroes.\n\n\n-----Output-----\n\nPrint \"YES\" if number x is quasi-palindromic. Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n131\n\nOutput\nYES\n\nInput\n320\n\nOutput\nNO\n\nInput\n2010200\n\nOutput\nYES"} +{"id": "00539", "text": "Imp is in a magic forest, where xorangles grow (wut?)\n\n [Image] \n\nA xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of order n to get out of the forest. \n\nFormally, for a given integer n you have to find the number of such triples (a, b, c), that:\n\n 1 \u2264 a \u2264 b \u2264 c \u2264 n; $a \\oplus b \\oplus c = 0$, where $x \\oplus y$ denotes the bitwise xor of integers x and y. (a, b, c) form a non-degenerate (with strictly positive area) triangle. \n\n\n-----Input-----\n\nThe only line contains a single integer n (1 \u2264 n \u2264 2500).\n\n\n-----Output-----\n\nPrint the number of xorangles of order n.\n\n\n-----Examples-----\nInput\n6\n\nOutput\n1\n\nInput\n10\n\nOutput\n2\n\n\n\n-----Note-----\n\nThe only xorangle in the first sample is (3, 5, 6)."} +{"id": "00540", "text": "You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.\n\nThe level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.\n\nLet's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). \n\nYou are staying in the cell (r_1, c_1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r_2, c_2) since the exit to the next level is there. Can you do this?\n\n\n-----Input-----\n\nThe first line contains two integers, n and m (1 \u2264 n, m \u2264 500)\u00a0\u2014 the number of rows and columns in the cave description.\n\nEach of the next n lines describes the initial state of the level of the cave, each line consists of m characters \".\" (that is, intact ice) and \"X\" (cracked ice).\n\nThe next line contains two integers, r_1 and c_1 (1 \u2264 r_1 \u2264 n, 1 \u2264 c_1 \u2264 m)\u00a0\u2014 your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r_1, c_1), that is, the ice on the starting cell is initially cracked.\n\nThe next line contains two integers r_2 and c_2 (1 \u2264 r_2 \u2264 n, 1 \u2264 c_2 \u2264 m)\u00a0\u2014 the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.\n\n\n-----Output-----\n\nIf you can reach the destination, print 'YES', otherwise print 'NO'.\n\n\n-----Examples-----\nInput\n4 6\nX...XX\n...XX.\n.X..X.\n......\n1 6\n2 2\n\nOutput\nYES\n\nInput\n5 4\n.X..\n...X\nX.X.\n....\n.XX.\n5 3\n1 1\n\nOutput\nNO\n\nInput\n4 7\n..X.XX.\n.XX..X.\nX...X..\nX......\n2 2\n1 6\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample test one possible path is:\n\n[Image]\n\nAfter the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended."} +{"id": "00541", "text": "There are N islands lining up from west to east, connected by N-1 bridges.\nThe i-th bridge connects the i-th island from the west and the (i+1)-th island from the west.\nOne day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:\nRequest i: A dispute took place between the a_i-th island from the west and the b_i-th island from the west. Please make traveling between these islands with bridges impossible.\nYou decided to remove some bridges to meet all these M requests.\nFind the minimum number of bridges that must be removed.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^5\n - 1 \\leq a_i < b_i \\leq N\n - All pairs (a_i, b_i) are distinct.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\na_1 b_1\na_2 b_2\n:\na_M b_M\n\n-----Output-----\nPrint the minimum number of bridges that must be removed.\n\n-----Sample Input-----\n5 2\n1 4\n2 5\n\n-----Sample Output-----\n1\n\nThe requests can be met by removing the bridge connecting the second and third islands from the west."} +{"id": "00542", "text": "Vasya has become interested in wrestling. In wrestling wrestlers use techniques for which they are awarded points by judges. The wrestler who gets the most points wins.\n\nWhen the numbers of points of both wrestlers are equal, the wrestler whose sequence of points is lexicographically greater, wins.\n\nIf the sequences of the awarded points coincide, the wrestler who performed the last technique wins. Your task is to determine which wrestler won.\n\n\n-----Input-----\n\nThe first line contains number n \u2014 the number of techniques that the wrestlers have used (1 \u2264 n \u2264 2\u00b710^5). \n\nThe following n lines contain integer numbers a_{i} (|a_{i}| \u2264 10^9, a_{i} \u2260 0). If a_{i} is positive, that means that the first wrestler performed the technique that was awarded with a_{i} points. And if a_{i} is negative, that means that the second wrestler performed the technique that was awarded with ( - a_{i}) points.\n\nThe techniques are given in chronological order.\n\n\n-----Output-----\n\nIf the first wrestler wins, print string \"first\", otherwise print \"second\"\n\n\n-----Examples-----\nInput\n5\n1\n2\n-3\n-4\n3\n\nOutput\nsecond\n\nInput\n3\n-1\n-2\n3\n\nOutput\nfirst\n\nInput\n2\n4\n-4\n\nOutput\nsecond\n\n\n\n-----Note-----\n\nSequence x = x_1x_2... x_{|}x| is lexicographically larger than sequence y = y_1y_2... y_{|}y|, if either |x| > |y| and x_1 = y_1, x_2 = y_2, ... , x_{|}y| = y_{|}y|, or there is such number r (r < |x|, r < |y|), that x_1 = y_1, x_2 = y_2, ... , x_{r} = y_{r} and x_{r} + 1 > y_{r} + 1.\n\nWe use notation |a| to denote length of sequence a."} +{"id": "00543", "text": "The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.\n\nTeams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be a_{i} teams on the i-th day.\n\nThere are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).\n\nAs Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.\n\nSereja wants to order exactly a_{i} pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.\n\n\n-----Input-----\n\nThe first line of input contains a single integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the number of training sessions.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10 000)\u00a0\u2014 the number of teams that will be present on each of the days.\n\n\n-----Output-----\n\nIf there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print \"YES\" (without quotes) in the only line of output. Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n4\n1 2 1 2\n\nOutput\nYES\n\nInput\n3\n1 0 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.\n\nIn the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days."} +{"id": "00544", "text": "You are given a string $s$ consisting of $n$ lowercase Latin letters. $n$ is even.\n\nFor each position $i$ ($1 \\le i \\le n$) in string $s$ you are required to change the letter on this position either to the previous letter in alphabetic order or to the next one (letters 'a' and 'z' have only one of these options). Letter in every position must be changed exactly once.\n\nFor example, letter 'p' should be changed either to 'o' or to 'q', letter 'a' should be changed to 'b' and letter 'z' should be changed to 'y'.\n\nThat way string \"codeforces\", for example, can be changed to \"dpedepqbft\" ('c' $\\rightarrow$ 'd', 'o' $\\rightarrow$ 'p', 'd' $\\rightarrow$ 'e', 'e' $\\rightarrow$ 'd', 'f' $\\rightarrow$ 'e', 'o' $\\rightarrow$ 'p', 'r' $\\rightarrow$ 'q', 'c' $\\rightarrow$ 'b', 'e' $\\rightarrow$ 'f', 's' $\\rightarrow$ 't').\n\nString $s$ is called a palindrome if it reads the same from left to right and from right to left. For example, strings \"abba\" and \"zz\" are palindromes and strings \"abca\" and \"zy\" are not.\n\nYour goal is to check if it's possible to make string $s$ a palindrome by applying the aforementioned changes to every position. Print \"YES\" if string $s$ can be transformed to a palindrome and \"NO\" otherwise.\n\nEach testcase contains several strings, for each of them you are required to solve the problem separately.\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\le T \\le 50$) \u2014 the number of strings in a testcase.\n\nThen $2T$ lines follow \u2014 lines $(2i - 1)$ and $2i$ of them describe the $i$-th string. The first line of the pair contains a single integer $n$ ($2 \\le n \\le 100$, $n$ is even) \u2014 the length of the corresponding string. The second line of the pair contains a string $s$, consisting of $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nPrint $T$ lines. The $i$-th line should contain the answer to the $i$-th string of the input. Print \"YES\" if it's possible to make the $i$-th string a palindrome by applying the aforementioned changes to every position. Print \"NO\" otherwise.\n\n\n-----Example-----\nInput\n5\n6\nabccba\n2\ncf\n4\nadfa\n8\nabaazaba\n2\nml\n\nOutput\nYES\nNO\nYES\nNO\nNO\n\n\n\n-----Note-----\n\nThe first string of the example can be changed to \"bcbbcb\", two leftmost letters and two rightmost letters got changed to the next letters, two middle letters got changed to the previous letters.\n\nThe second string can be changed to \"be\", \"bg\", \"de\", \"dg\", but none of these resulting strings are palindromes.\n\nThe third string can be changed to \"beeb\" which is a palindrome.\n\nThe fifth string can be changed to \"lk\", \"lm\", \"nk\", \"nm\", but none of these resulting strings are palindromes. Also note that no letter can remain the same, so you can't obtain strings \"ll\" or \"mm\"."} +{"id": "00545", "text": "Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.\n\nMore formally, you are given two strings s_1, s_2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s_3 of length n, such that f(s_1, s_3) = f(s_2, s_3) = t. If there is no such string, print - 1.\n\n\n-----Input-----\n\nThe first line contains two integers n and t (1 \u2264 n \u2264 10^5, 0 \u2264 t \u2264 n).\n\nThe second line contains string s_1 of length n, consisting of lowercase English letters.\n\nThe third line contain string s_2 of length n, consisting of lowercase English letters.\n\n\n-----Output-----\n\nPrint a string of length n, differing from string s_1 and from s_2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.\n\n\n-----Examples-----\nInput\n3 2\nabc\nxyc\n\nOutput\nayd\nInput\n1 0\nc\nb\n\nOutput\n-1"} +{"id": "00546", "text": "It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..\n\nThere is a glob pattern in the statements (a string consisting of lowercase English letters, characters \"?\" and \"*\"). It is known that character \"*\" occurs no more than once in the pattern.\n\nAlso, n query strings are given, it is required to determine for each of them if the pattern matches it or not.\n\nEverything seemed easy to Petya, but then he discovered that the special pattern characters differ from their usual meaning.\n\nA pattern matches a string if it is possible to replace each character \"?\" with one good lowercase English letter, and the character \"*\" (if there is one) with any, including empty, string of bad lowercase English letters, so that the resulting string is the same as the given string.\n\nThe good letters are given to Petya. All the others are bad.\n\n\n-----Input-----\n\nThe first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad.\n\nThe second line contains the pattern\u00a0\u2014 a string s of lowercase English letters, characters \"?\" and \"*\" (1 \u2264 |s| \u2264 10^5). It is guaranteed that character \"*\" occurs in s no more than once.\n\nThe third line contains integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of query strings.\n\nn lines follow, each of them contains single non-empty string consisting of lowercase English letters\u00a0\u2014 a query string.\n\nIt is guaranteed that the total length of all query strings is not greater than 10^5.\n\n\n-----Output-----\n\nPrint n lines: in the i-th of them print \"YES\" if the pattern matches the i-th query string, and \"NO\" otherwise.\n\nYou can choose the case (lower or upper) for each letter arbitrary.\n\n\n-----Examples-----\nInput\nab\na?a\n2\naaa\naab\n\nOutput\nYES\nNO\n\nInput\nabc\na?a?a*\n4\nabacaba\nabaca\napapa\naaaaax\n\nOutput\nNO\nYES\nNO\nYES\n\n\n\n-----Note-----\n\nIn the first example we can replace \"?\" with good letters \"a\" and \"b\", so we can see that the answer for the first query is \"YES\", and the answer for the second query is \"NO\", because we can't match the third letter.\n\nExplanation of the second example. The first query: \"NO\", because character \"*\" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string \"ba\", in which both letters are good. The second query: \"YES\", because characters \"?\" can be replaced with corresponding good letters, and character \"*\" can be replaced with empty string, and the strings will coincide. The third query: \"NO\", because characters \"?\" can't be replaced with bad letters. The fourth query: \"YES\", because characters \"?\" can be replaced with good letters \"a\", and character \"*\" can be replaced with a string of bad letters \"x\"."} +{"id": "00547", "text": "Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.\n\nVanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.\n\nEntering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.\n\nDetermine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 n, k \u2264 100)\u00a0\u2014 the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.\n\nThe next n lines contains passwords, one per line\u00a0\u2014 pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.\n\nThe last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.\n\n\n-----Output-----\n\nPrint two integers\u00a0\u2014 time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.\n\n\n-----Examples-----\nInput\n5 2\ncba\nabc\nbb1\nabC\nABC\nabc\n\nOutput\n1 15\n\nInput\n4 100\n11\n22\n1\n2\n22\n\nOutput\n3 4\n\n\n\n-----Note-----\n\nConsider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.\n\nConsider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all."} +{"id": "00548", "text": "Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally?\n\n\n-----Input-----\n\nFirst line of input data contains single integer n (1 \u2264 n \u2264 10^6) \u2014 length of the array.\n\nNext line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nOutput answer in single line. \"First\", if first player wins, and \"Second\" otherwise (without quotes).\n\n\n-----Examples-----\nInput\n4\n1 3 2 3\n\nOutput\nFirst\n\nInput\n2\n2 2\n\nOutput\nSecond\n\n\n\n-----Note-----\n\nIn first sample first player remove whole array in one move and win.\n\nIn second sample first player can't make a move and lose."} +{"id": "00549", "text": "A big company decided to launch a new series of rectangular displays, and decided that the display must have exactly n pixels. \n\nYour task is to determine the size of the rectangular display \u2014 the number of lines (rows) of pixels a and the number of columns of pixels b, so that:\n\n there are exactly n pixels on the display; the number of rows does not exceed the number of columns, it means a \u2264 b; the difference b - a is as small as possible. \n\n\n-----Input-----\n\nThe first line contains the positive integer n (1 \u2264 n \u2264 10^6)\u00a0\u2014 the number of pixels display should have.\n\n\n-----Output-----\n\nPrint two integers\u00a0\u2014 the number of rows and columns on the display. \n\n\n-----Examples-----\nInput\n8\n\nOutput\n2 4\n\nInput\n64\n\nOutput\n8 8\n\nInput\n5\n\nOutput\n1 5\n\nInput\n999999\n\nOutput\n999 1001\n\n\n\n-----Note-----\n\nIn the first example the minimum possible difference equals 2, so on the display should be 2 rows of 4 pixels.\n\nIn the second example the minimum possible difference equals 0, so on the display should be 8 rows of 8 pixels.\n\nIn the third example the minimum possible difference equals 4, so on the display should be 1 row of 5 pixels."} +{"id": "00550", "text": "When registering in a social network, users are allowed to create their own convenient login to make it easier to share contacts, print it on business cards, etc.\n\nLogin is an arbitrary sequence of lower and uppercase latin letters, digits and underline symbols (\u00ab_\u00bb). However, in order to decrease the number of frauds and user-inattention related issues, it is prohibited to register a login if it is similar with an already existing login. More precisely, two logins s and t are considered similar if we can transform s to t via a sequence of operations of the following types: transform lowercase letters to uppercase and vice versa; change letter \u00abO\u00bb (uppercase latin letter) to digit \u00ab0\u00bb and vice versa; change digit \u00ab1\u00bb (one) to any letter among \u00abl\u00bb (lowercase latin \u00abL\u00bb), \u00abI\u00bb (uppercase latin \u00abi\u00bb) and vice versa, or change one of these letters to other. \n\nFor example, logins \u00abCodeforces\u00bb and \u00abcodef0rces\u00bb as well as \u00abOO0OOO00O0OOO0O00OOO0OO_lol\u00bb and \u00abOO0OOO0O00OOO0O00OO0OOO_1oI\u00bb are considered similar whereas \u00abCodeforces\u00bb and \u00abCode_forces\u00bb are not.\n\nYou're given a list of existing logins with no two similar amonst and a newly created user login. Check whether this new login is similar with any of the existing ones.\n\n\n-----Input-----\n\nThe first line contains a non-empty string s consisting of lower and uppercase latin letters, digits and underline symbols (\u00ab_\u00bb) with length not exceeding 50 \u00a0\u2014 the login itself.\n\nThe second line contains a single integer n (1 \u2264 n \u2264 1 000)\u00a0\u2014 the number of existing logins.\n\nThe next n lines describe the existing logins, following the same constraints as the user login (refer to the first line of the input). It's guaranteed that no two existing logins are similar.\n\n\n-----Output-----\n\nPrint \u00abYes\u00bb (without quotes), if user can register via this login, i.e. none of the existing logins is similar with it.\n\nOtherwise print \u00abNo\u00bb (without quotes).\n\n\n-----Examples-----\nInput\n1_wat\n2\n2_wat\nwat_1\n\nOutput\nYes\n\nInput\n000\n3\n00\nooA\noOo\n\nOutput\nNo\n\nInput\n_i_\n3\n__i_\n_1_\nI\n\nOutput\nNo\n\nInput\nLa0\n3\n2a0\nLa1\n1a0\n\nOutput\nNo\n\nInput\nabc\n1\naBc\n\nOutput\nNo\n\nInput\n0Lil\n2\nLIL0\n0Ril\n\nOutput\nYes\n\n\n\n-----Note-----\n\nIn the second sample case the user wants to create a login consisting of three zeros. It's impossible due to collision with the third among the existing.\n\nIn the third sample case the new login is similar with the second one."} +{"id": "00551", "text": "Connect the countless points with lines, till we reach the faraway yonder.\n\nThere are n points on a coordinate plane, the i-th of which being (i, y_{i}).\n\nDetermine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.\n\n\n-----Input-----\n\nThe first line of input contains a positive integer n (3 \u2264 n \u2264 1 000) \u2014 the number of points.\n\nThe second line contains n space-separated integers y_1, y_2, ..., y_{n} ( - 10^9 \u2264 y_{i} \u2264 10^9) \u2014 the vertical coordinates of each point.\n\n\n-----Output-----\n\nOutput \"Yes\" (without quotes) if it's possible to fulfill the requirements, and \"No\" otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n5\n7 5 8 6 9\n\nOutput\nYes\n\nInput\n5\n-1 -2 0 0 -5\n\nOutput\nNo\n\nInput\n5\n5 4 3 2 1\n\nOutput\nNo\n\nInput\n5\n1000000000 0 0 0 0\n\nOutput\nYes\n\n\n\n-----Note-----\n\nIn the first example, there are five points: (1, 7), (2, 5), (3, 8), (4, 6) and (5, 9). It's possible to draw a line that passes through points 1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one.\n\nIn the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel.\n\nIn the third example, it's impossible to satisfy both requirements at the same time."} +{"id": "00552", "text": "Vasya had three strings $a$, $b$ and $s$, which consist of lowercase English letters. The lengths of strings $a$ and $b$ are equal to $n$, the length of the string $s$ is equal to $m$. \n\nVasya decided to choose a substring of the string $a$, then choose a substring of the string $b$ and concatenate them. Formally, he chooses a segment $[l_1, r_1]$ ($1 \\leq l_1 \\leq r_1 \\leq n$) and a segment $[l_2, r_2]$ ($1 \\leq l_2 \\leq r_2 \\leq n$), and after concatenation he obtains a string $a[l_1, r_1] + b[l_2, r_2] = a_{l_1} a_{l_1 + 1} \\ldots a_{r_1} b_{l_2} b_{l_2 + 1} \\ldots b_{r_2}$.\n\nNow, Vasya is interested in counting number of ways to choose those segments adhering to the following conditions:\n\n segments $[l_1, r_1]$ and $[l_2, r_2]$ have non-empty intersection, i.e. there exists at least one integer $x$, such that $l_1 \\leq x \\leq r_1$ and $l_2 \\leq x \\leq r_2$; the string $a[l_1, r_1] + b[l_2, r_2]$ is equal to the string $s$. \n\n\n-----Input-----\n\nThe first line contains integers $n$ and $m$ ($1 \\leq n \\leq 500\\,000, 2 \\leq m \\leq 2 \\cdot n$)\u00a0\u2014 the length of strings $a$ and $b$ and the length of the string $s$.\n\nThe next three lines contain strings $a$, $b$ and $s$, respectively. The length of the strings $a$ and $b$ is $n$, while the length of the string $s$ is $m$.\n\nAll strings consist of lowercase English letters.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of ways to choose a pair of segments, which satisfy Vasya's conditions.\n\n\n-----Examples-----\nInput\n6 5\naabbaa\nbaaaab\naaaaa\n\nOutput\n4\n\nInput\n5 4\nazaza\nzazaz\nazaz\n\nOutput\n11\n\nInput\n9 12\nabcabcabc\nxyzxyzxyz\nabcabcayzxyz\n\nOutput\n2\n\n\n\n-----Note-----\n\nLet's list all the pairs of segments that Vasya could choose in the first example:\n\n $[2, 2]$ and $[2, 5]$; $[1, 2]$ and $[2, 4]$; $[5, 5]$ and $[2, 5]$; $[5, 6]$ and $[3, 5]$;"} +{"id": "00553", "text": "During a New Year special offer the \"Sudislavl Bars\" offered n promo codes. Each promo code consists of exactly six digits and gives right to one free cocktail at the bar \"Mosquito Shelter\". Of course, all the promocodes differ.\n\nAs the \"Mosquito Shelter\" opens only at 9, and partying in Sudislavl usually begins at as early as 6, many problems may arise as to how to type a promotional code without errors. It is necessary to calculate such maximum k, that the promotional code could be uniquely identified if it was typed with no more than k errors. At that, k = 0 means that the promotional codes must be entered exactly.\n\nA mistake in this problem should be considered as entering the wrong numbers. For example, value \"123465\" contains two errors relative to promocode \"123456\". Regardless of the number of errors the entered value consists of exactly six digits.\n\n\n-----Input-----\n\nThe first line of the output contains number n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of promocodes.\n\nEach of the next n lines contains a single promocode, consisting of exactly 6 digits. It is guaranteed that all the promocodes are distinct. Promocodes can start from digit \"0\".\n\n\n-----Output-----\n\nPrint the maximum k (naturally, not exceeding the length of the promocode), such that any promocode can be uniquely identified if it is typed with at most k mistakes.\n\n\n-----Examples-----\nInput\n2\n000000\n999999\n\nOutput\n2\n\nInput\n6\n211111\n212111\n222111\n111111\n112111\n121111\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample k < 3, so if a bar customer types in value \"090909\", then it will be impossible to define which promocode exactly corresponds to it."} +{"id": "00554", "text": "Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is a_{i}. The mood can be positive, zero or negative.\n\nLet's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in.\n\nFor example, consider the case when the mother has 5 flowers, and their moods are equal to 1, - 2, 1, 3, - 4. Suppose the mother suggested subarrays (1, - 2), (3, - 4), (1, 3), (1, - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then: the first flower adds 1\u00b71 = 1 to the girl's happiness, because he is in one of chosen subarrays, the second flower adds ( - 2)\u00b71 = - 2, because he is in one of chosen subarrays, the third flower adds 1\u00b72 = 2, because he is in two of chosen subarrays, the fourth flower adds 3\u00b72 = 6, because he is in two of chosen subarrays, the fifth flower adds ( - 4)\u00b70 = 0, because he is in no chosen subarrays. \n\nThus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!\n\nAlyona can choose any number of the subarrays, even 0 or all suggested by her mother.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 100)\u00a0\u2014 the number of flowers and the number of subarrays suggested by the mother.\n\nThe second line contains the flowers moods\u00a0\u2014 n integers a_1, a_2, ..., a_{n} ( - 100 \u2264 a_{i} \u2264 100).\n\nThe next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers l_{i} and r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n) denoting the subarray a[l_{i}], a[l_{i} + 1], ..., a[r_{i}].\n\nEach subarray can encounter more than once.\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the maximum possible value added to the Alyona's happiness.\n\n\n-----Examples-----\nInput\n5 4\n1 -2 1 3 -4\n1 2\n4 5\n3 4\n1 4\n\nOutput\n7\n\nInput\n4 3\n1 2 3 4\n1 3\n2 4\n1 1\n\nOutput\n16\n\nInput\n2 2\n-1 -2\n1 1\n1 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nThe first example is the situation described in the statements.\n\nIn the second example Alyona should choose all subarrays.\n\nThe third example has answer 0 because Alyona can choose none of the subarrays."} +{"id": "00555", "text": "Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t. \n\nHelp Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero.\n\n\n-----Input-----\n\nThe first line contains a single integer x (1 \u2264 x \u2264 10^18) \u2014 the number that Luke Skywalker gave to Chewbacca.\n\n\n-----Output-----\n\nPrint the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes.\n\n\n-----Examples-----\nInput\n27\n\nOutput\n22\n\nInput\n4545\n\nOutput\n4444"} +{"id": "00556", "text": "Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure.\n\nUnfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed to help if Rostislav solves a simple task (and if he doesn't, then why would he need Splay trees anyway?)\n\nGiven integers l, r and k, you need to print all powers of number k within range from l to r inclusive. However, Rostislav doesn't want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!\n\n\n-----Input-----\n\nThe first line of the input contains three space-separated integers l, r and k (1 \u2264 l \u2264 r \u2264 10^18, 2 \u2264 k \u2264 10^9).\n\n\n-----Output-----\n\nPrint all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print \"-1\" (without the quotes).\n\n\n-----Examples-----\nInput\n1 10 2\n\nOutput\n1 2 4 8 \nInput\n2 4 5\n\nOutput\n-1\n\n\n-----Note-----\n\nNote to the first sample: numbers 2^0 = 1, 2^1 = 2, 2^2 = 4, 2^3 = 8 lie within the specified range. The number 2^4 = 16 is greater then 10, thus it shouldn't be printed."} +{"id": "00557", "text": "Pig is visiting a friend.\n\nPig's house is located at point 0, and his friend's house is located at point m on an axis.\n\nPig can use teleports to move along the axis.\n\nTo use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport there is the rightmost point it can move Pig to, this point is known as the limit of the teleport.\n\nFormally, a teleport located at point x with limit y can move Pig from point x to any point within the segment [x; y], including the bounds. [Image] \n\nDetermine if Pig can visit the friend using teleports only, or he should use his car.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 100, 1 \u2264 m \u2264 100)\u00a0\u2014 the number of teleports and the location of the friend's house.\n\nThe next n lines contain information about teleports.\n\nThe i-th of these lines contains two integers a_{i} and b_{i} (0 \u2264 a_{i} \u2264 b_{i} \u2264 m), where a_{i} is the location of the i-th teleport, and b_{i} is its limit.\n\nIt is guaranteed that a_{i} \u2265 a_{i} - 1 for every i (2 \u2264 i \u2264 n).\n\n\n-----Output-----\n\nPrint \"YES\" if there is a path from Pig's house to his friend's house that uses only teleports, and \"NO\" otherwise.\n\nYou can print each letter in arbitrary case (upper or lower).\n\n\n-----Examples-----\nInput\n3 5\n0 2\n2 4\n3 5\n\nOutput\nYES\n\nInput\n3 7\n0 4\n2 5\n6 7\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe first example is shown on the picture below: [Image] \n\nPig can use the first teleport from his house (point 0) to reach point 2, then using the second teleport go from point 2 to point 3, then using the third teleport go from point 3 to point 5, where his friend lives.\n\nThe second example is shown on the picture below: [Image] \n\nYou can see that there is no path from Pig's house to his friend's house that uses only teleports."} +{"id": "00558", "text": "There are N blocks arranged in a row. Let us paint these blocks.\nWe will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways.\nFind the number of ways to paint the blocks under the following conditions:\n - For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors.\n - There may be at most K pairs of adjacent blocks that are painted in the same color.\nSince the count may be enormous, print it modulo 998244353.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N, M \\leq 2 \\times 10^5\n - 0 \\leq K \\leq N - 1\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M K\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n3 2 1\n\n-----Sample Output-----\n6\n\nThe following ways to paint the blocks satisfy the conditions: 112, 121, 122, 211, 212, and 221. Here, digits represent the colors of the blocks."} +{"id": "00559", "text": "Given are a prime number p and a sequence of p integers a_0, \\ldots, a_{p-1} consisting of zeros and ones.\nFind a polynomial of degree at most p-1, f(x) = b_{p-1} x^{p-1} + b_{p-2} x^{p-2} + \\ldots + b_0, satisfying the following conditions:\n - For each i (0 \\leq i \\leq p-1), b_i is an integer such that 0 \\leq b_i \\leq p-1.\n - For each i (0 \\leq i \\leq p-1), f(i) \\equiv a_i \\pmod p.\n\n-----Constraints-----\n - 2 \\leq p \\leq 2999\n - p is a prime number.\n - 0 \\leq a_i \\leq 1\n\n-----Input-----\nInput is given from Standard Input in the following format:\np\na_0 a_1 \\ldots a_{p-1}\n\n-----Output-----\nPrint b_0, b_1, \\ldots, b_{p-1} of a polynomial f(x) satisfying the conditions, in this order, with spaces in between.\nIt can be proved that a solution always exists. If multiple solutions exist, any of them will be accepted.\n\n-----Sample Input-----\n2\n1 0\n\n-----Sample Output-----\n1 1\n\nf(x) = x + 1 satisfies the conditions, as follows:\n - f(0) = 0 + 1 = 1 \\equiv 1 \\pmod 2\n - f(1) = 1 + 1 = 2 \\equiv 0 \\pmod 2"} +{"id": "00560", "text": "You are given a rectangular cake, represented as an r \u00d7 c grid. Each cell either has an evil strawberry, or is empty. For example, a 3 \u00d7 4 cake may look as follows: [Image] \n\nThe cakeminator is going to eat the cake! Each time he eats, he chooses a row or a column that does not contain any evil strawberries and contains at least one cake cell that has not been eaten before, and eats all the cake cells there. He may decide to eat any number of times.\n\nPlease output the maximum number of cake cells that the cakeminator can eat.\n\n\n-----Input-----\n\nThe first line contains two integers r and c (2 \u2264 r, c \u2264 10), denoting the number of rows and the number of columns of the cake. The next r lines each contains c characters \u2014 the j-th character of the i-th line denotes the content of the cell at row i and column j, and is either one of these: '.' character denotes a cake cell with no evil strawberry; 'S' character denotes a cake cell with an evil strawberry. \n\n\n-----Output-----\n\nOutput the maximum number of cake cells that the cakeminator can eat.\n\n\n-----Examples-----\nInput\n3 4\nS...\n....\n..S.\n\nOutput\n8\n\n\n\n-----Note-----\n\nFor the first example, one possible way to eat the maximum number of cake cells is as follows (perform 3 eats). [Image] [Image] [Image]"} +{"id": "00561", "text": "Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a_1, a_2, ..., a_{n} of length n, that the following condition fulfills: a_2 - a_1 = a_3 - a_2 = a_4 - a_3 = ... = a_{i} + 1 - a_{i} = ... = a_{n} - a_{n} - 1.\n\nFor example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not.\n\nAlexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards).\n\nArthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of cards. The next line contains the sequence of integers \u2014 the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 10^8.\n\n\n-----Output-----\n\nIf Arthur can write infinitely many distinct integers on the card, print on a single line -1.\n\nOtherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 10^8 or even be negative (see test samples).\n\n\n-----Examples-----\nInput\n3\n4 1 7\n\nOutput\n2\n-2 10\n\nInput\n1\n10\n\nOutput\n-1\n\nInput\n4\n1 3 5 9\n\nOutput\n1\n7\n\nInput\n4\n4 3 4 5\n\nOutput\n0\n\nInput\n2\n2 4\n\nOutput\n3\n0 3 6"} +{"id": "00562", "text": "Polycarp is a great fan of television.\n\nHe wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.\n\nPolycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.\n\nPolycarp wants to check out all n shows. Are two TVs enough to do so?\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 2\u00b710^5) \u2014 the number of shows.\n\nEach of the next n lines contains two integers l_{i} and r_{i} (0 \u2264 l_{i} < r_{i} \u2264 10^9) \u2014 starting and ending time of i-th show.\n\n\n-----Output-----\n\nIf Polycarp is able to check out all the shows using only two TVs then print \"YES\" (without quotes). Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n3\n1 2\n2 3\n4 5\n\nOutput\nYES\n\nInput\n4\n1 2\n2 3\n2 3\n1 2\n\nOutput\nNO"} +{"id": "00563", "text": "Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one. \n\nYour friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the pair (a, c) is coprime. \n\nYou want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers (a, b, c), for which the statement is false, and the numbers meet the condition l \u2264 a < b < c \u2264 r. \n\nMore specifically, you need to find three numbers (a, b, c), such that l \u2264 a < b < c \u2264 r, pairs (a, b) and (b, c) are coprime, and pair (a, c) is not coprime.\n\n\n-----Input-----\n\nThe single line contains two positive space-separated integers l, r (1 \u2264 l \u2264 r \u2264 10^18; r - l \u2264 50).\n\n\n-----Output-----\n\nPrint three positive space-separated integers a, b, c\u00a0\u2014 three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order. \n\nIf the counterexample does not exist, print the single number -1.\n\n\n-----Examples-----\nInput\n2 4\n\nOutput\n2 3 4\n\nInput\n10 11\n\nOutput\n-1\n\nInput\n900000000000000009 900000000000000029\n\nOutput\n900000000000000009 900000000000000010 900000000000000021\n\n\n\n-----Note-----\n\nIn the first sample pair (2, 4) is not coprime and pairs (2, 3) and (3, 4) are. \n\nIn the second sample you cannot form a group of three distinct integers, so the answer is -1. \n\nIn the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three."} +{"id": "00564", "text": "Sereja showed an interesting game to his friends. The game goes like that. Initially, there is a table with an empty cup and n water mugs on it. Then all players take turns to move. During a move, a player takes a non-empty mug of water and pours all water from it into the cup. If the cup overfills, then we assume that this player lost.\n\nAs soon as Sereja's friends heard of the game, they wanted to play it. Sereja, on the other hand, wanted to find out whether his friends can play the game in such a way that there are no losers. You are given the volumes of all mugs and the cup. Also, you know that Sereja has (n - 1) friends. Determine if Sereja's friends can play the game so that nobody loses.\n\n\n-----Input-----\n\nThe first line contains integers n and s (2 \u2264 n \u2264 100;\u00a01 \u2264 s \u2264 1000) \u2014 the number of mugs and the volume of the cup. The next line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10). Number a_{i} means the volume of the i-th mug.\n\n\n-----Output-----\n\nIn a single line, print \"YES\" (without the quotes) if his friends can play in the described manner, and \"NO\" (without the quotes) otherwise.\n\n\n-----Examples-----\nInput\n3 4\n1 1 1\n\nOutput\nYES\n\nInput\n3 4\n3 1 3\n\nOutput\nYES\n\nInput\n3 4\n4 4 4\n\nOutput\nNO"} +{"id": "00565", "text": "Alice and Bob are decorating a Christmas Tree. \n\nAlice wants only $3$ types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have $y$ yellow ornaments, $b$ blue ornaments and $r$ red ornaments.\n\nIn Bob's opinion, a Christmas Tree will be beautiful if: the number of blue ornaments used is greater by exactly $1$ than the number of yellow ornaments, and the number of red ornaments used is greater by exactly $1$ than the number of blue ornaments. \n\nThat is, if they have $8$ yellow ornaments, $13$ blue ornaments and $9$ red ornaments, we can choose $4$ yellow, $5$ blue and $6$ red ornaments ($5=4+1$ and $6=5+1$).\n\nAlice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion.\n\nIn the example two paragraphs above, we would choose $7$ yellow, $8$ blue and $9$ red ornaments. If we do it, we will use $7+8+9=24$ ornaments. That is the maximum number.\n\nSince Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! \n\nIt is guaranteed that it is possible to choose at least $6$ ($1+2+3=6$) ornaments.\n\n\n-----Input-----\n\nThe only line contains three integers $y$, $b$, $r$ ($1 \\leq y \\leq 100$, $2 \\leq b \\leq 100$, $3 \\leq r \\leq 100$)\u00a0\u2014 the number of yellow, blue and red ornaments. \n\nIt is guaranteed that it is possible to choose at least $6$ ($1+2+3=6$) ornaments.\n\n\n-----Output-----\n\nPrint one number\u00a0\u2014 the maximum number of ornaments that can be used. \n\n\n-----Examples-----\nInput\n8 13 9\n\nOutput\n24\nInput\n13 3 6\n\nOutput\n9\n\n\n-----Note-----\n\nIn the first example, the answer is $7+8+9=24$.\n\nIn the second example, the answer is $2+3+4=9$."} +{"id": "00566", "text": "You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?\n\nYour task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.\n\n\n-----Input-----\n\nThe single line contains three integers r, g and b (0 \u2264 r, g, b \u2264 2\u00b710^9) \u2014 the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.\n\n\n-----Output-----\n\nPrint a single integer t \u2014 the maximum number of tables that can be decorated in the required manner.\n\n\n-----Examples-----\nInput\n5 4 3\n\nOutput\n4\n\nInput\n1 1 1\n\nOutput\n1\n\nInput\n2 3 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample you can decorate the tables with the following balloon sets: \"rgg\", \"gbb\", \"brr\", \"rrg\", where \"r\", \"g\" and \"b\" represent the red, green and blue balls, respectively."} +{"id": "00567", "text": "You and your friend are participating in a TV show \"Run For Your Prize\".\n\nAt the start of the show n prizes are located on a straight line. i-th prize is located at position a_{i}. Positions of all prizes are distinct. You start at position 1, your friend \u2014 at position 10^6 (and there is no prize in any of these two positions). You have to work as a team and collect all prizes in minimum possible time, in any order.\n\nYou know that it takes exactly 1 second to move from position x to position x + 1 or x - 1, both for you and your friend. You also have trained enough to instantly pick up any prize, if its position is equal to your current position (and the same is true for your friend). Carrying prizes does not affect your speed (or your friend's speed) at all.\n\nNow you may discuss your strategy with your friend and decide who will pick up each prize. Remember that every prize must be picked up, either by you or by your friend.\n\nWhat is the minimum number of seconds it will take to pick up all the prizes?\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 10^5) \u2014 the number of prizes.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (2 \u2264 a_{i} \u2264 10^6 - 1) \u2014 the positions of the prizes. No two prizes are located at the same position. Positions are given in ascending order.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of seconds it will take to collect all prizes.\n\n\n-----Examples-----\nInput\n3\n2 3 9\n\nOutput\n8\n\nInput\n2\n2 999995\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example you take all the prizes: take the first at 1, the second at 2 and the third at 8.\n\nIn the second example you take the first prize in 1 second and your friend takes the other in 5 seconds, you do this simultaneously, so the total time is 5."} +{"id": "00568", "text": "Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle.\n\nMore formally, there are 3n gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the circle by numbers from 0 to 3n - 1, let the gnome sitting on the i-th place have a_{i} coins. If there is an integer i (0 \u2264 i < n) such that a_{i} + a_{i} + n + a_{i} + 2n \u2260 6, then Tanya is satisfied. \n\nCount the number of ways to choose a_{i} so that Tanya is satisfied. As there can be many ways of distributing coins, print the remainder of this number modulo 10^9 + 7. Two ways, a and b, are considered distinct if there is index i (0 \u2264 i < 3n), such that a_{i} \u2260 b_{i} (that is, some gnome got different number of coins in these two ways).\n\n\n-----Input-----\n\nA single line contains number n (1 \u2264 n \u2264 10^5) \u2014 the number of the gnomes divided by three.\n\n\n-----Output-----\n\nPrint a single number \u2014 the remainder of the number of variants of distributing coins that satisfy Tanya modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n20\nInput\n2\n\nOutput\n680\n\n\n-----Note-----\n\n20 ways for n = 1 (gnome with index 0 sits on the top of the triangle, gnome 1 on the right vertex, gnome 2 on the left vertex): [Image]"} +{"id": "00569", "text": "A wise man told Kerem \"Different is good\" once, so Kerem wants all things in his life to be different. \n\nKerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string \"aba\" has substrings \"\" (empty substring), \"a\", \"b\", \"a\", \"ab\", \"ba\", \"aba\".\n\nIf string s has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.\n\nYour task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the length of the string s.\n\nThe second line contains the string s of length n consisting of only lowercase English letters.\n\n\n-----Output-----\n\nIf it's impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.\n\n\n-----Examples-----\nInput\n2\naa\n\nOutput\n1\n\nInput\n4\nkoko\n\nOutput\n2\n\nInput\n5\nmurat\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample one of the possible solutions is to change the first character to 'b'.\n\nIn the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes \"abko\"."} +{"id": "00570", "text": "At regular competition Vladik and Valera won a and b candies respectively. Vladik offered 1 his candy to Valera. After that Valera gave Vladik 2 his candies, so that no one thought that he was less generous. Vladik for same reason gave 3 candies to Valera in next turn.\n\nMore formally, the guys take turns giving each other one candy more than they received in the previous turn.\n\nThis continued until the moment when one of them couldn\u2019t give the right amount of candy. Candies, which guys got from each other, they don\u2019t consider as their own. You need to know, who is the first who can\u2019t give the right amount of candy.\n\n\n-----Input-----\n\nSingle line of input data contains two space-separated integers a, b (1 \u2264 a, b \u2264 10^9) \u2014 number of Vladik and Valera candies respectively.\n\n\n-----Output-----\n\nPring a single line \"Vladik\u2019\u2019 in case, if Vladik first who can\u2019t give right amount of candy, or \"Valera\u2019\u2019 otherwise.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\nValera\n\nInput\n7 6\n\nOutput\nVladik\n\n\n\n-----Note-----\n\nIllustration for first test case:\n\n[Image]\n\nIllustration for second test case:\n\n[Image]"} +{"id": "00571", "text": "Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.\n\nIn his favorite math class, the teacher taught him the following interesting definitions.\n\nA parenthesis sequence is a string, containing only characters \"(\" and \")\".\n\nA correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters \"1\" and \"+\" between the original characters of the sequence. For example, parenthesis sequences \"()()\", \"(())\" are correct (the resulting expressions are: \"(1+1)+(1+1)\", \"((1+1)+1)\"), while \")(\" and \")\" are not. Note that the empty string is a correct parenthesis sequence by definition.\n\nWe define that $|s|$ as the length of string $s$. A strict prefix $s[1\\dots l]$ $(1\\leq l< |s|)$ of a string $s = s_1s_2\\dots s_{|s|}$ is string $s_1s_2\\dots s_l$. Note that the empty string and the whole string are not strict prefixes of any string by the definition.\n\nHaving learned these definitions, he comes up with a new problem. He writes down a string $s$ containing only characters \"(\", \")\" and \"?\". And what he is going to do, is to replace each of the \"?\" in $s$ independently by one of \"(\" and \")\" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.\n\nAfter all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable.\n\n\n-----Input-----\n\nThe first line contains a single integer $|s|$ ($1\\leq |s|\\leq 3 \\cdot 10^5$), the length of the string.\n\nThe second line contains a string $s$, containing only \"(\", \")\" and \"?\".\n\n\n-----Output-----\n\nA single line contains a string representing the answer.\n\nIf there are many solutions, any of them is acceptable.\n\nIf there is no answer, print a single line containing \":(\" (without the quotes).\n\n\n-----Examples-----\nInput\n6\n(?????\n\nOutput\n(()())\nInput\n10\n(???(???(?\n\nOutput\n:(\n\n\n\n-----Note-----\n\nIt can be proved that there is no solution for the second sample, so print \":(\"."} +{"id": "00572", "text": "Limak is a little polar bear. He doesn't have many toys and thus he often plays with polynomials.\n\nHe considers a polynomial valid if its degree is n and its coefficients are integers not exceeding k by the absolute value. More formally:\n\nLet a_0, a_1, ..., a_{n} denote the coefficients, so $P(x) = \\sum_{i = 0}^{n} a_{i} \\cdot x^{i}$. Then, a polynomial P(x) is valid if all the following conditions are satisfied: a_{i} is integer for every i; |a_{i}| \u2264 k for every i; a_{n} \u2260 0. \n\nLimak has recently got a valid polynomial P with coefficients a_0, a_1, a_2, ..., a_{n}. He noticed that P(2) \u2260 0 and he wants to change it. He is going to change one coefficient to get a valid polynomial Q of degree n that Q(2) = 0. Count the number of ways to do so. You should count two ways as a distinct if coefficients of target polynoms differ.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 200 000, 1 \u2264 k \u2264 10^9)\u00a0\u2014 the degree of the polynomial and the limit for absolute values of coefficients.\n\nThe second line contains n + 1 integers a_0, a_1, ..., a_{n} (|a_{i}| \u2264 k, a_{n} \u2260 0)\u00a0\u2014 describing a valid polynomial $P(x) = \\sum_{i = 0}^{n} a_{i} \\cdot x^{i}$. It's guaranteed that P(2) \u2260 0.\n\n\n-----Output-----\n\nPrint the number of ways to change one coefficient to get a valid polynomial Q that Q(2) = 0.\n\n\n-----Examples-----\nInput\n3 1000000000\n10 -9 -3 5\n\nOutput\n3\n\nInput\n3 12\n10 -9 -3 5\n\nOutput\n2\n\nInput\n2 20\n14 -7 19\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, we are given a polynomial P(x) = 10 - 9x - 3x^2 + 5x^3.\n\nLimak can change one coefficient in three ways: He can set a_0 = - 10. Then he would get Q(x) = - 10 - 9x - 3x^2 + 5x^3 and indeed Q(2) = - 10 - 18 - 12 + 40 = 0. Or he can set a_2 = - 8. Then Q(x) = 10 - 9x - 8x^2 + 5x^3 and indeed Q(2) = 10 - 18 - 32 + 40 = 0. Or he can set a_1 = - 19. Then Q(x) = 10 - 19x - 3x^2 + 5x^3 and indeed Q(2) = 10 - 38 - 12 + 40 = 0. \n\nIn the second sample, we are given the same polynomial. This time though, k is equal to 12 instead of 10^9. Two first of ways listed above are still valid but in the third way we would get |a_1| > k what is not allowed. Thus, the answer is 2 this time."} +{"id": "00573", "text": "There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.\n\nThe coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can't use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team.\n\n\n-----Input-----\n\nThe first line contains single integer n (2 \u2264 n \u2264 2\u00b710^5) \u2014 the number of groups.\n\nThe second line contains a sequence of integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 2), where a_{i} is the number of people in group i.\n\n\n-----Output-----\n\nPrint the maximum number of teams of three people the coach can form.\n\n\n-----Examples-----\nInput\n4\n1 1 2 1\n\nOutput\n1\n\nInput\n2\n2 2\n\nOutput\n0\n\nInput\n7\n2 2 2 1 1 1 1\n\nOutput\n3\n\nInput\n3\n1 1 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example the coach can form one team. For example, he can take students from the first, second and fourth groups.\n\nIn the second example he can't make a single team.\n\nIn the third example the coach can form three teams. For example, he can do this in the following way: The first group (of two people) and the seventh group (of one person), The second group (of two people) and the sixth group (of one person), The third group (of two people) and the fourth group (of one person)."} +{"id": "00574", "text": "Developing tools for creation of locations maps for turn-based fights in a new game, Petya faced the following problem.\n\nA field map consists of hexagonal cells. Since locations sizes are going to be big, a game designer wants to have a tool for quick filling of a field part with identical enemy units. This action will look like following: a game designer will select a rectangular area on the map, and each cell whose center belongs to the selected rectangle will be filled with the enemy unit.\n\nMore formally, if a game designer selected cells having coordinates (x_1, y_1) and (x_2, y_2), where x_1 \u2264 x_2 and y_1 \u2264 y_2, then all cells having center coordinates (x, y) such that x_1 \u2264 x \u2264 x_2 and y_1 \u2264 y \u2264 y_2 will be filled. Orthogonal coordinates system is set up so that one of cell sides is parallel to OX axis, all hexagon centers have integer coordinates and for each integer x there are cells having center with such x coordinate and for each integer y there are cells having center with such y coordinate. It is guaranteed that difference x_2 - x_1 is divisible by 2.\n\nWorking on the problem Petya decided that before painting selected units he wants to output number of units that will be painted on the map.\n\nHelp him implement counting of these units before painting.\n\n [Image] \n\n\n-----Input-----\n\nThe only line of input contains four integers x_1, y_1, x_2, y_2 ( - 10^9 \u2264 x_1 \u2264 x_2 \u2264 10^9, - 10^9 \u2264 y_1 \u2264 y_2 \u2264 10^9) \u2014 the coordinates of the centers of two cells.\n\n\n-----Output-----\n\nOutput one integer \u2014 the number of cells to be filled.\n\n\n-----Examples-----\nInput\n1 1 5 5\n\nOutput\n13"} +{"id": "00575", "text": "Alice and Bob are playing chess on a huge chessboard with dimensions $n \\times n$. Alice has a single piece left\u00a0\u2014 a queen, located at $(a_x, a_y)$, while Bob has only the king standing at $(b_x, b_y)$. Alice thinks that as her queen is dominating the chessboard, victory is hers. \n\nBut Bob has made a devious plan to seize the victory for himself\u00a0\u2014 he needs to march his king to $(c_x, c_y)$ in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.\n\nBob will win if he can move his king from $(b_x, b_y)$ to $(c_x, c_y)$ without ever getting in check. Remember that a king can move to any of the $8$ adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen. \n\nFind whether Bob can win or not.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($3 \\leq n \\leq 1000$)\u00a0\u2014 the dimensions of the chessboard.\n\nThe second line contains two integers $a_x$ and $a_y$ ($1 \\leq a_x, a_y \\leq n$)\u00a0\u2014 the coordinates of Alice's queen.\n\nThe third line contains two integers $b_x$ and $b_y$ ($1 \\leq b_x, b_y \\leq n$)\u00a0\u2014 the coordinates of Bob's king.\n\nThe fourth line contains two integers $c_x$ and $c_y$ ($1 \\leq c_x, c_y \\leq n$)\u00a0\u2014 the coordinates of the location that Bob wants to get to.\n\nIt is guaranteed that Bob's king is currently not in check and the target location is not in check either.\n\nFurthermore, the king is not located on the same square as the queen (i.e. $a_x \\neq b_x$ or $a_y \\neq b_y$), and the target does coincide neither with the queen's position (i.e. $c_x \\neq a_x$ or $c_y \\neq a_y$) nor with the king's position (i.e. $c_x \\neq b_x$ or $c_y \\neq b_y$).\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if Bob can get from $(b_x, b_y)$ to $(c_x, c_y)$ without ever getting in check, otherwise print \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n8\n4 4\n1 3\n3 1\n\nOutput\nYES\n\nInput\n8\n4 4\n2 3\n1 6\n\nOutput\nNO\n\nInput\n8\n3 5\n1 2\n6 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.\n\nIn the first case, the king can move, for instance, via the squares $(2, 3)$ and $(3, 2)$. Note that the direct route through $(2, 2)$ goes through check.\n\n [Image] \n\nIn the second case, the queen watches the fourth rank, and the king has no means of crossing it.\n\n [Image] \n\nIn the third case, the queen watches the third file.\n\n [Image]"} +{"id": "00576", "text": "Given an array $a$, consisting of $n$ integers, find:\n\n$$\\max\\limits_{1 \\le i < j \\le n} LCM(a_i,a_j),$$\n\nwhere $LCM(x, y)$ is the smallest positive integer that is divisible by both $x$ and $y$. For example, $LCM(6, 8) = 24$, $LCM(4, 12) = 12$, $LCM(2, 3) = 6$.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($2 \\le n \\le 10^5$)\u00a0\u2014 the number of elements in the array $a$.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^5$)\u00a0\u2014 the elements of the array $a$.\n\n\n-----Output-----\n\nPrint one integer, the maximum value of the least common multiple of two elements in the array $a$.\n\n\n-----Examples-----\nInput\n3\n13 35 77\n\nOutput\n1001\nInput\n6\n1 2 4 8 16 32\n\nOutput\n32"} +{"id": "00577", "text": "Phoenix is picking berries in his backyard. There are $n$ shrubs, and each shrub has $a_i$ red berries and $b_i$ blue berries.\n\nEach basket can contain $k$ berries. But, Phoenix has decided that each basket may only contain berries from the same shrub or berries of the same color (red or blue). In other words, all berries in a basket must be from the same shrub or/and have the same color.\n\nFor example, if there are two shrubs with $5$ red and $2$ blue berries in the first shrub and $2$ red and $1$ blue berries in the second shrub then Phoenix can fill $2$ baskets of capacity $4$ completely: the first basket will contain $3$ red and $1$ blue berries from the first shrub; the second basket will contain the $2$ remaining red berries from the first shrub and $2$ red berries from the second shrub. \n\nHelp Phoenix determine the maximum number of baskets he can fill completely!\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($ 1\\le n, k \\le 500$)\u00a0\u2014 the number of shrubs and the basket capacity, respectively.\n\nThe $i$-th of the next $n$ lines contain two integers $a_i$ and $b_i$ ($0 \\le a_i, b_i \\le 10^9$)\u00a0\u2014 the number of red and blue berries in the $i$-th shrub, respectively.\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 the maximum number of baskets that Phoenix can fill completely.\n\n\n-----Examples-----\nInput\n2 4\n5 2\n2 1\n\nOutput\n2\n\nInput\n1 5\n2 3\n\nOutput\n1\n\nInput\n2 5\n2 1\n1 3\n\nOutput\n0\n\nInput\n1 2\n1000000000 1\n\nOutput\n500000000\n\n\n\n-----Note-----\n\nThe first example is described above.\n\nIn the second example, Phoenix can fill one basket fully using all the berries from the first (and only) shrub.\n\nIn the third example, Phoenix cannot fill any basket completely because there are less than $5$ berries in each shrub, less than $5$ total red berries, and less than $5$ total blue berries.\n\nIn the fourth example, Phoenix can put all the red berries into baskets, leaving an extra blue berry behind."} +{"id": "00578", "text": "Barney is standing in a bar and starring at a pretty girl. He wants to shoot her with his heart arrow but he needs to know the distance between him and the girl to make his shot accurate. [Image] \n\nBarney asked the bar tender Carl about this distance value, but Carl was so busy talking to the customers so he wrote the distance value (it's a real number) on a napkin. The problem is that he wrote it in scientific notation. The scientific notation of some real number x is the notation of form AeB, where A is a real number and B is an integer and x = A \u00d7 10^{B} is true. In our case A is between 0 and 9 and B is non-negative.\n\nBarney doesn't know anything about scientific notation (as well as anything scientific at all). So he asked you to tell him the distance value in usual decimal representation with minimal number of digits after the decimal point (and no decimal point if it is an integer). See the output format for better understanding.\n\n\n-----Input-----\n\nThe first and only line of input contains a single string of form a.deb where a, d and b are integers and e is usual character 'e' (0 \u2264 a \u2264 9, 0 \u2264 d < 10^100, 0 \u2264 b \u2264 100)\u00a0\u2014 the scientific notation of the desired distance value.\n\na and b contain no leading zeros and d contains no trailing zeros (but may be equal to 0). Also, b can not be non-zero if a is zero.\n\n\n-----Output-----\n\nPrint the only real number x (the desired distance value) in the only line in its decimal notation. \n\nThus if x is an integer, print it's integer value without decimal part and decimal point and without leading zeroes. \n\nOtherwise print x in a form of p.q such that p is an integer that have no leading zeroes (but may be equal to zero), and q is an integer that have no trailing zeroes (and may not be equal to zero).\n\n\n-----Examples-----\nInput\n8.549e2\n\nOutput\n854.9\n\nInput\n8.549e3\n\nOutput\n8549\n\nInput\n0.33e0\n\nOutput\n0.33"} +{"id": "00579", "text": "Takahashi will play a game using a piece on an array of squares numbered 1, 2, \\cdots, N. Square i has an integer C_i written on it. Also, he is given a permutation of 1, 2, \\cdots, N: P_1, P_2, \\cdots, P_N.\nNow, he will choose one square and place the piece on that square. Then, he will make the following move some number of times between 1 and K (inclusive):\n - In one move, if the piece is now on Square i (1 \\leq i \\leq N), move it to Square P_i. Here, his score increases by C_{P_i}.\nHelp him by finding the maximum possible score at the end of the game. (The score is 0 at the beginning of the game.)\n\n-----Constraints-----\n - 2 \\leq N \\leq 5000\n - 1 \\leq K \\leq 10^9\n - 1 \\leq P_i \\leq N\n - P_i \\neq i\n - P_1, P_2, \\cdots, P_N are all different.\n - -10^9 \\leq C_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nP_1 P_2 \\cdots P_N\nC_1 C_2 \\cdots C_N\n\n-----Output-----\nPrint the maximum possible score at the end of the game.\n\n-----Sample Input-----\n5 2\n2 4 5 1 3\n3 4 -10 -8 8\n\n-----Sample Output-----\n8\n\nWhen we start at some square of our choice and make at most two moves, we have the following options:\n - If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n - If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n - If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n - If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n - If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\nThe maximum score achieved is 8."} +{"id": "00580", "text": "Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s \u2260 t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer.\n\nOnce a year the President visited his historic home town t, for which his motorcade passes along some path from s to t (he always returns on a personal plane). Since the president is a very busy man, he always chooses the path from s to t, along which he will travel the fastest.\n\nThe ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer.\n\n\n-----Input-----\n\nThe first lines contain four integers n, m, s and t (2 \u2264 n \u2264 10^5;\u00a01 \u2264 m \u2264 10^5;\u00a01 \u2264 s, t \u2264 n) \u2014 the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town (s \u2260 t).\n\nNext m lines contain the roads. Each road is given as a group of three integers a_{i}, b_{i}, l_{i} (1 \u2264 a_{i}, b_{i} \u2264 n;\u00a0a_{i} \u2260 b_{i};\u00a01 \u2264 l_{i} \u2264 10^6) \u2014 the cities that are connected by the i-th road and the time needed to ride along it. The road is directed from city a_{i} to city b_{i}.\n\nThe cities are numbered from 1 to n. Each pair of cities can have multiple roads between them. It is guaranteed that there is a path from s to t along the roads.\n\n\n-----Output-----\n\nPrint m lines. The i-th line should contain information about the i-th road (the roads are numbered in the order of appearance in the input).\n\nIf the president will definitely ride along it during his travels, the line must contain a single word \"YES\" (without the quotes).\n\nOtherwise, if the i-th road can be repaired so that the travel time on it remains positive and then president will definitely ride along it, print space-separated word \"CAN\" (without the quotes), and the minimum cost of repairing.\n\nIf we can't make the road be such that president will definitely ride along it, print \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n6 7 1 6\n1 2 2\n1 3 10\n2 3 7\n2 4 8\n3 5 3\n4 5 2\n5 6 1\n\nOutput\nYES\nCAN 2\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nYES\n\nInput\n3 3 1 3\n1 2 10\n2 3 10\n1 3 100\n\nOutput\nYES\nYES\nCAN 81\n\nInput\n2 2 1 2\n1 2 1\n1 2 2\n\nOutput\nYES\nNO\n\n\n\n-----Note-----\n\nThe cost of repairing the road is the difference between the time needed to ride along it before and after the repairing.\n\nIn the first sample president initially may choose one of the two following ways for a ride: 1 \u2192 2 \u2192 4 \u2192 5 \u2192 6 or 1 \u2192 2 \u2192 3 \u2192 5 \u2192 6."} +{"id": "00581", "text": "You are given an unweighted tree with n vertices. Then n - 1 following operations are applied to the tree. A single operation consists of the following steps: choose two leaves; add the length of the simple path between them to the answer; remove one of the chosen leaves from the tree. \n\nInitial answer (before applying operations) is 0. Obviously after n - 1 such operations the tree will consist of a single vertex. \n\nCalculate the maximal possible answer you can achieve, and construct a sequence of operations that allows you to achieve this answer!\n\n\n-----Input-----\n\nThe first line contains one integer number n (2 \u2264 n \u2264 2\u00b710^5) \u2014 the number of vertices in the tree. \n\nNext n - 1 lines describe the edges of the tree in form a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n, a_{i} \u2260 b_{i}). It is guaranteed that given graph is a tree.\n\n\n-----Output-----\n\nIn the first line print one integer number \u2014 maximal possible answer. \n\nIn the next n - 1 lines print the operations in order of their applying in format a_{i}, b_{i}, c_{i}, where a_{i}, b_{i} \u2014 pair of the leaves that are chosen in the current operation (1 \u2264 a_{i}, b_{i} \u2264 n), c_{i} (1 \u2264 c_{i} \u2264 n, c_{i} = a_{i} or c_{i} = b_{i}) \u2014 choosen leaf that is removed from the tree in the current operation. \n\nSee the examples for better understanding.\n\n\n-----Examples-----\nInput\n3\n1 2\n1 3\n\nOutput\n3\n2 3 3\n2 1 1\n\nInput\n5\n1 2\n1 3\n2 4\n2 5\n\nOutput\n9\n3 5 5\n4 3 3\n4 1 1\n4 2 2"} +{"id": "00582", "text": "VK news recommendation system daily selects interesting publications of one of $n$ disjoint categories for each user. Each publication belongs to exactly one category. For each category $i$ batch algorithm selects $a_i$ publications.\n\nThe latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of $i$-th category within $t_i$ seconds. \n\nWhat is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can't remove publications recommended by the batch algorithm.\n\n\n-----Input-----\n\nThe first line of input consists of single integer $n$\u00a0\u2014 the number of news categories ($1 \\le n \\le 200\\,000$).\n\nThe second line of input consists of $n$ integers $a_i$\u00a0\u2014 the number of publications of $i$-th category selected by the batch algorithm ($1 \\le a_i \\le 10^9$).\n\nThe third line of input consists of $n$ integers $t_i$\u00a0\u2014 time it takes for targeted algorithm to find one new publication of category $i$ ($1 \\le t_i \\le 10^5)$.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimal required time for the targeted algorithm to get rid of categories with the same size.\n\n\n-----Examples-----\nInput\n5\n3 7 9 7 8\n5 2 5 7 5\n\nOutput\n6\n\nInput\n5\n1 2 3 4 5\n1 1 1 1 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, it is possible to find three publications of the second type, which will take 6 seconds.\n\nIn the second example, all news categories contain a different number of publications."} +{"id": "00583", "text": "This is an easier version of the problem. In this version, $n \\le 500$.\n\nVasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence.\n\nTo digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him.\n\nWe remind that bracket sequence $s$ is called correct if: $s$ is empty; $s$ is equal to \"($t$)\", where $t$ is correct bracket sequence; $s$ is equal to $t_1 t_2$, i.e. concatenation of $t_1$ and $t_2$, where $t_1$ and $t_2$ are correct bracket sequences. \n\nFor example, \"(()())\", \"()\" are correct, while \")(\" and \"())\" are not.\n\nThe cyclical shift of the string $s$ of length $n$ by $k$ ($0 \\leq k < n$) is a string formed by a concatenation of the last $k$ symbols of the string $s$ with the first $n - k$ symbols of string $s$. For example, the cyclical shift of string \"(())()\" by $2$ equals \"()(())\".\n\nCyclical shifts $i$ and $j$ are considered different, if $i \\ne j$.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 500$), the length of the string.\n\nThe second line contains a string, consisting of exactly $n$ characters, where each of the characters is either \"(\" or \")\".\n\n\n-----Output-----\n\nThe first line should contain a single integer\u00a0\u2014 the largest beauty of the string, which can be achieved by swapping some two characters.\n\nThe second line should contain integers $l$ and $r$ ($1 \\leq l, r \\leq n$)\u00a0\u2014 the indices of two characters, which should be swapped in order to maximize the string's beauty.\n\nIn case there are several possible swaps, print any of them.\n\n\n-----Examples-----\nInput\n10\n()()())(()\n\nOutput\n5\n8 7\n\nInput\n12\n)(()(()())()\n\nOutput\n4\n5 10\n\nInput\n6\n)))(()\n\nOutput\n0\n1 1\n\n\n\n-----Note-----\n\nIn the first example, we can swap $7$-th and $8$-th character, obtaining a string \"()()()()()\". The cyclical shifts by $0, 2, 4, 6, 8$ of this string form a correct bracket sequence.\n\nIn the second example, after swapping $5$-th and $10$-th character, we obtain a string \")(())()()(()\". The cyclical shifts by $11, 7, 5, 3$ of this string form a correct bracket sequence.\n\nIn the third example, swap of any two brackets results in $0$ cyclical shifts being correct bracket sequences."} +{"id": "00584", "text": "Modern text editors usually show some information regarding the document being edited. For example, the number of words, the number of pages, or the number of characters.\n\nIn this problem you should implement the similar functionality.\n\nYou are given a string which only consists of: uppercase and lowercase English letters, underscore symbols (they are used as separators), parentheses (both opening and closing). \n\nIt is guaranteed that each opening parenthesis has a succeeding closing parenthesis. Similarly, each closing parentheses has a preceding opening parentheses matching it. For each pair of matching parentheses there are no other parenthesis between them. In other words, each parenthesis in the string belongs to a matching \"opening-closing\" pair, and such pairs can't be nested.\n\nFor example, the following string is valid: \"_Hello_Vasya(and_Petya)__bye_(and_OK)\".\n\nWord is a maximal sequence of consecutive letters, i.e. such sequence that the first character to the left and the first character to the right of it is an underscore, a parenthesis, or it just does not exist. For example, the string above consists of seven words: \"Hello\", \"Vasya\", \"and\", \"Petya\", \"bye\", \"and\" and \"OK\". Write a program that finds: the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses), the number of words inside the parentheses (print 0, if there is no word inside the parentheses). \n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 255)\u00a0\u2014 the length of the given string. The second line contains the string consisting of only lowercase and uppercase English letters, parentheses and underscore symbols. \n\n\n-----Output-----\n\nPrint two space-separated integers: the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses), the number of words inside the parentheses (print 0, if there is no word inside the parentheses). \n\n\n-----Examples-----\nInput\n37\n_Hello_Vasya(and_Petya)__bye_(and_OK)\n\nOutput\n5 4\n\n\nInput\n37\n_a_(_b___c)__de_f(g_)__h__i(j_k_l)m__\n\nOutput\n2 6\n\n\nInput\n27\n(LoooonG)__shOrt__(LoooonG)\n\nOutput\n5 2\n\n\nInput\n5\n(___)\n\nOutput\n0 0\n\n\n\n\n\n-----Note-----\n\nIn the first sample, the words \"Hello\", \"Vasya\" and \"bye\" are outside any of the parentheses, and the words \"and\", \"Petya\", \"and\" and \"OK\" are inside. Note, that the word \"and\" is given twice and you should count it twice in the answer."} +{"id": "00585", "text": "You are given two arrays $a_1, a_2, \\dots , a_n$ and $b_1, b_2, \\dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$).\n\nYou have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on.\n\nFor example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. \n\nYou have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$)\u00a0\u2014 the length of arrays $a$ and $b$ respectively.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots , a_n$ ($1 \\le a_i \\le 10^9$)\u00a0\u2014 the array $a$.\n\nThe third line contains $m$ integers $b_1, b_2, \\dots , b_m$ ($1 \\le b_i \\le 10^9; b_i < b_{i+1}$)\u00a0\u2014 the array $b$.\n\n\n-----Output-----\n\nIn only line print one integer \u2014 the number of ways to divide the array $a$ modulo 998244353.\n\n\n-----Examples-----\nInput\n6 3\n12 10 20 20 25 30\n10 20 30\n\nOutput\n2\n\nInput\n4 2\n1 3 3 7\n3 7\n\nOutput\n0\n\nInput\n8 2\n1 2 2 2 2 2 2 2\n1 2\n\nOutput\n7"} +{"id": "00586", "text": "You are given a square board, consisting of $n$ rows and $n$ columns. Each tile in it should be colored either white or black.\n\nLet's call some coloring beautiful if each pair of adjacent rows are either the same or different in every position. The same condition should be held for the columns as well.\n\nLet's call some coloring suitable if it is beautiful and there is no rectangle of the single color, consisting of at least $k$ tiles.\n\nYour task is to count the number of suitable colorings of the board of the given size.\n\nSince the answer can be very large, print it modulo $998244353$.\n\n\n-----Input-----\n\nA single line contains two integers $n$ and $k$ ($1 \\le n \\le 500$, $1 \\le k \\le n^2$) \u2014 the number of rows and columns of the board and the maximum number of tiles inside the rectangle of the single color, respectively.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of suitable colorings of the board of the given size modulo $998244353$.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n0\n\nInput\n2 3\n\nOutput\n6\n\nInput\n49 1808\n\nOutput\n359087121\n\n\n\n-----Note-----\n\nBoard of size $1 \\times 1$ is either a single black tile or a single white tile. Both of them include a rectangle of a single color, consisting of $1$ tile.\n\nHere are the beautiful colorings of a board of size $2 \\times 2$ that don't include rectangles of a single color, consisting of at least $3$ tiles: [Image] \n\nThe rest of beautiful colorings of a board of size $2 \\times 2$ are the following: [Image]"} +{"id": "00587", "text": "There are N pieces of sushi. Each piece has two parameters: \"kind of topping\" t_i and \"deliciousness\" d_i.\nYou are choosing K among these N pieces to eat.\nYour \"satisfaction\" here will be calculated as follows:\n - The satisfaction is the sum of the \"base total deliciousness\" and the \"variety bonus\".\n - The base total deliciousness is the sum of the deliciousness of the pieces you eat.\n - The variety bonus is x*x, where x is the number of different kinds of toppings of the pieces you eat.\nYou want to have as much satisfaction as possible.\nFind this maximum satisfaction.\n\n-----Constraints-----\n - 1 \\leq K \\leq N \\leq 10^5\n - 1 \\leq t_i \\leq N\n - 1 \\leq d_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nt_1 d_1\nt_2 d_2\n.\n.\n.\nt_N d_N\n\n-----Output-----\nPrint the maximum satisfaction that you can obtain.\n\n-----Sample Input-----\n5 3\n1 9\n1 7\n2 6\n2 5\n3 1\n\n-----Sample Output-----\n26\n\nIf you eat Sushi 1,2 and 3:\n - The base total deliciousness is 9+7+6=22.\n - The variety bonus is 2*2=4.\nThus, your satisfaction will be 26, which is optimal."} +{"id": "00588", "text": "E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.\nHe has N engines, which can be used as follows:\n - When E869120 uses the i-th engine, his X- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).\n - E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.\nHe wants to go as far as possible from the origin.\nLet (X, Y) be his final coordinates. Find the maximum possible value of \\sqrt{X^2 + Y^2}, the distance from the origin.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - -1 \\ 000 \\ 000 \\leq x_i \\leq 1 \\ 000 \\ 000\n - -1 \\ 000 \\ 000 \\leq y_i \\leq 1 \\ 000 \\ 000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nx_1 y_1\nx_2 y_2\n : :\nx_N y_N\n\n-----Output-----\nPrint the maximum possible final distance from the origin, as a real value.\nYour output is considered correct when the relative or absolute error from the true answer is at most 10^{-10}.\n\n-----Sample Input-----\n3\n0 10\n5 -5\n-5 -5\n\n-----Sample Output-----\n10.000000000000000000000000000000000000000000000000\n\nThe final distance from the origin can be 10 if we use the engines in one of the following three ways:\n - Use Engine 1 to move to (0, 10).\n - Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n - Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\nThe distance cannot be greater than 10, so the maximum possible distance is 10."} +{"id": "00589", "text": "Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there. \n\nThe content special agent has got an important task: to get the latest research by British scientists on the English Language. These developments are encoded and stored in a large safe. The Beaver's teeth are strong enough, so the authorities assured that upon arriving at the place the beaver won't have any problems with opening the safe.\n\nAnd he finishes his aspen sprig and leaves for this important task. Of course, the Beaver arrived at the location without any problems, but alas. He can't open the safe with his strong and big teeth. At this point, the Smart Beaver get a call from the headquarters and learns that opening the safe with the teeth is not necessary, as a reliable source has sent the following information: the safe code consists of digits and has no leading zeroes. There also is a special hint, which can be used to open the safe. The hint is string s with the following structure:\n\n if s_{i} = \"?\", then the digit that goes i-th in the safe code can be anything (between 0 to 9, inclusively); if s_{i} is a digit (between 0 to 9, inclusively), then it means that there is digit s_{i} on position i in code; if the string contains letters from \"A\" to \"J\", then all positions with the same letters must contain the same digits and the positions with distinct letters must contain distinct digits. The length of the safe code coincides with the length of the hint. \n\nFor example, hint \"?JGJ9\" has such matching safe code variants: \"51919\", \"55959\", \"12329\", \"93539\" and so on, and has wrong variants such as: \"56669\", \"00111\", \"03539\" and \"13666\".\n\nAfter receiving such information, the authorities change the plan and ask the special agents to work quietly and gently and not to try to open the safe by mechanical means, and try to find the password using the given hint.\n\nAt a special agent school the Smart Beaver was the fastest in his platoon finding codes for such safes, but now he is not in that shape: the years take their toll ... Help him to determine the number of possible variants of the code to the safe, matching the given hint. After receiving this information, and knowing his own speed of entering codes, the Smart Beaver will be able to determine whether he will have time for tonight's show \"Beavers are on the trail\" on his favorite TV channel, or he should work for a sleepless night...\n\n\n-----Input-----\n\nThe first line contains string s \u2014 the hint to the safe code. String s consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string s doesn't equal to character 0.\n\nThe input limits for scoring 30 points are (subproblem A1): 1 \u2264 |s| \u2264 5. \n\nThe input limits for scoring 100 points are (subproblems A1+A2): 1 \u2264 |s| \u2264 10^5. \n\nHere |s| means the length of string s.\n\n\n-----Output-----\n\nPrint the number of codes that match the given hint.\n\n\n-----Examples-----\nInput\nAJ\n\nOutput\n81\n\nInput\n1?AA\n\nOutput\n100"} +{"id": "00590", "text": "Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.\n\nRecently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.\n\nThus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.\n\nIn order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal \u2014 compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if x_{i} < y_{i}, where i is the first index in which the permutations x and y differ.\n\nDetermine the array Ivan will obtain after performing all the changes.\n\n\n-----Input-----\n\nThe first line contains an single integer n (2 \u2264 n \u2264 200 000) \u2014 the number of elements in Ivan's array.\n\nThe second line contains a sequence of integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n) \u2014 the description of Ivan's array.\n\n\n-----Output-----\n\nIn the first line print q \u2014 the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.\n\n\n-----Examples-----\nInput\n4\n3 2 2 3\n\nOutput\n2\n1 2 4 3 \n\nInput\n6\n4 5 6 3 2 1\n\nOutput\n0\n4 5 6 3 2 1 \n\nInput\n10\n6 8 4 6 7 1 6 3 4 5\n\nOutput\n3\n2 8 4 6 7 1 9 3 10 5 \n\n\n\n-----Note-----\n\nIn the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers \u2014 this permutation is lexicographically minimal among all suitable. \n\nIn the second example Ivan does not need to change anything because his array already is a permutation."} +{"id": "00591", "text": "Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.\n\nVasya has a train lighting schedule for all n hours of the trip \u2014 n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.\n\n\n-----Input-----\n\nThe first input line contains two integers n and k (1 \u2264 n \u2264 1000, 1 \u2264 k \u2264 n) \u2014 the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers a_{i} (0 \u2264 a_{i} \u2264 100), a_{i} is the light level at the i-th hour.\n\n\n-----Output-----\n\nIn the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b_1, b_2, ..., b_{k}, \u2014 the indexes of hours Vasya will read at (1 \u2264 b_{i} \u2264 n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers b_{i} in an arbitrary order.\n\n\n-----Examples-----\nInput\n5 3\n20 10 30 40 10\n\nOutput\n20\n1 3 4 \n\nInput\n6 5\n90 20 35 40 60 100\n\nOutput\n35\n1 3 4 5 6 \n\n\n\n-----Note-----\n\nIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20."} +{"id": "00592", "text": "You are given a positive integer $n$ greater or equal to $2$. For every pair of integers $a$ and $b$ ($2 \\le |a|, |b| \\le n$), you can transform $a$ into $b$ if and only if there exists an integer $x$ such that $1 < |x|$ and ($a \\cdot x = b$ or $b \\cdot x = a$), where $|x|$ denotes the absolute value of $x$.\n\nAfter such a transformation, your score increases by $|x|$ points and you are not allowed to transform $a$ into $b$ nor $b$ into $a$ anymore.\n\nInitially, you have a score of $0$. You can start at any integer and transform it as many times as you like. What is the maximum score you can achieve?\n\n\n-----Input-----\n\nA single line contains a single integer $n$ ($2 \\le n \\le 100\\,000$)\u00a0\u2014 the given integer described above.\n\n\n-----Output-----\n\nPrint an only integer\u00a0\u2014 the maximum score that can be achieved with the transformations. If it is not possible to perform even a single transformation for all possible starting integers, print $0$.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n8\nInput\n6\n\nOutput\n28\nInput\n2\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first example, the transformations are $2 \\rightarrow 4 \\rightarrow (-2) \\rightarrow (-4) \\rightarrow 2$.\n\nIn the third example, it is impossible to perform even a single transformation."} +{"id": "00593", "text": "The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.\n\nThe electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.\n\nAt the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.\n\nDetermine who will win the elections.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n, m (1 \u2264 n, m \u2264 100) \u2014 the number of candidates and of cities, respectively.\n\nEach of the next m lines contains n non-negative integers, the j-th number in the i-th line a_{ij} (1 \u2264 j \u2264 n, 1 \u2264 i \u2264 m, 0 \u2264 a_{ij} \u2264 10^9) denotes the number of votes for candidate j in city i.\n\nIt is guaranteed that the total number of people in all the cities does not exceed 10^9.\n\n\n-----Output-----\n\nPrint a single number \u2014 the index of the candidate who won the elections. The candidates are indexed starting from one.\n\n\n-----Examples-----\nInput\n3 3\n1 2 3\n2 3 1\n1 2 1\n\nOutput\n2\nInput\n3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7\n\nOutput\n1\n\n\n-----Note-----\n\nNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.\n\nNote to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index."} +{"id": "00594", "text": "Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it.\n\nValera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds).\n\nLet's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some \"extra\" time if for its running time, a seconds, an inequality 2a \u2264 v holds.\n\nAs a result, Valera decided to set v seconds TL, that the following conditions are met: v is a positive integer; all correct solutions pass the system testing; at least one correct solution passes the system testing with some \"extra\" time; all wrong solutions do not pass the system testing; value v is minimum among all TLs, for which points 1, 2, 3, 4 hold. \n\nHelp Valera and find the most suitable TL or else state that such TL doesn't exist.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 100). The second line contains n space-separated positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100) \u2014 the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b_1, b_2, ..., b_{m} (1 \u2264 b_{i} \u2264 100) \u2014 the running time of each of m wrong solutions in seconds. \n\n\n-----Output-----\n\nIf there is a valid TL value, print it. Otherwise, print -1.\n\n\n-----Examples-----\nInput\n3 6\n4 5 2\n8 9 6 10 7 11\n\nOutput\n5\nInput\n3 1\n3 4 5\n6\n\nOutput\n-1"} +{"id": "00595", "text": "The girl Taylor has a beautiful calendar for the year y. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.\n\nThe calendar is so beautiful that she wants to know what is the next year after y when the calendar will be exactly the same. Help Taylor to find that year.\n\nNote that leap years has 366 days. The year is leap if it is divisible by 400 or it is divisible by 4, but not by 100 (https://en.wikipedia.org/wiki/Leap_year).\n\n\n-----Input-----\n\nThe only line contains integer y (1000 \u2264 y < 100'000) \u2014 the year of the calendar.\n\n\n-----Output-----\n\nPrint the only integer y' \u2014 the next year after y when the calendar will be the same. Note that you should find the first year after y with the same calendar.\n\n\n-----Examples-----\nInput\n2016\n\nOutput\n2044\n\nInput\n2000\n\nOutput\n2028\n\nInput\n50501\n\nOutput\n50507\n\n\n\n-----Note-----\n\nToday is Monday, the 13th of June, 2016."} +{"id": "00596", "text": "Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendar's scheme of leap years as follows:\n\n Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year. [Image] \n\nIn this problem, you have been given two dates and your task is to calculate how many days are between them. Note, that leap years have unusual number of days in February.\n\nLook at the sample to understand what borders are included in the aswer.\n\n\n-----Input-----\n\nThe first two lines contain two dates, each date is in the format yyyy:mm:dd (1900 \u2264 yyyy \u2264 2038 and yyyy:mm:dd is a legal date).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n1900:01:01\n2038:12:31\n\nOutput\n50768\n\nInput\n1996:03:09\n1991:11:12\n\nOutput\n1579"} +{"id": "00597", "text": "Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces\u2019 superhero. Byteforces is a country that consists of n cities, connected by n - 1 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go from any city to any other city using only the given roads. There are m cities being attacked by humans. So Ari... we meant Super M have to immediately go to each of the cities being attacked to scare those bad humans. Super M can pass from one city to another only using the given roads. Moreover, passing through one road takes her exactly one kron - the time unit used in Byteforces. [Image] \n\nHowever, Super M is not on Byteforces now - she is attending a training camp located in a nearby country Codeforces. Fortunately, there is a special device in Codeforces that allows her to instantly teleport from Codeforces to any city of Byteforces. The way back is too long, so for the purpose of this problem teleportation is used exactly once.\n\nYou are to help Super M, by calculating the city in which she should teleport at the beginning in order to end her job in the minimum time (measured in krons). Also, provide her with this time so she can plan her way back to Codeforces.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 m \u2264 n \u2264 123456) - the number of cities in Byteforces, and the number of cities being attacked respectively.\n\nThen follow n - 1 lines, describing the road system. Each line contains two city numbers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n) - the ends of the road i.\n\nThe last line contains m distinct integers - numbers of cities being attacked. These numbers are given in no particular order.\n\n\n-----Output-----\n\nFirst print the number of the city Super M should teleport to. If there are many possible optimal answers, print the one with the lowest city number.\n\nThen print the minimum possible time needed to scare all humans in cities being attacked, measured in Krons.\n\nNote that the correct answer is always unique.\n\n\n-----Examples-----\nInput\n7 2\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n2 7\n\nOutput\n2\n3\n\nInput\n6 4\n1 2\n2 3\n2 4\n4 5\n4 6\n2 4 5 6\n\nOutput\n2\n4\n\n\n\n-----Note-----\n\nIn the first sample, there are two possibilities to finish the Super M's job in 3 krons. They are:\n\n$2 \\rightarrow 1 \\rightarrow 3 \\rightarrow 7$ and $7 \\rightarrow 3 \\rightarrow 1 \\rightarrow 2$.\n\nHowever, you should choose the first one as it starts in the city with the lower number."} +{"id": "00598", "text": "It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.\n\nSo the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers l_{i}, r_{i}, cost_{i} \u2014 day of departure from Vi\u010dkopolis, day of arriving back in Vi\u010dkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value r_{i} - l_{i} + 1.\n\nAt the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i \u2260 j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: r_{i} < l_{j} or r_{j} < l_{i}.\n\nHelp Leha to choose the necessary vouchers!\n\n\n-----Input-----\n\nThe first line contains two integers n and x (2 \u2264 n, x \u2264 2\u00b710^5) \u2014 the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.\n\nEach of the next n lines contains three integers l_{i}, r_{i} and cost_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 2\u00b710^5, 1 \u2264 cost_{i} \u2264 10^9) \u2014 description of the voucher.\n\n\n-----Output-----\n\nPrint a single integer \u2014 a minimal amount of money that Leha will spend, or print - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.\n\n\n-----Examples-----\nInput\n4 5\n1 3 4\n1 2 5\n5 6 1\n1 2 4\n\nOutput\n5\n\nInput\n3 2\n4 6 3\n2 4 1\n3 5 4\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.\n\nIn the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2."} +{"id": "00599", "text": "Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked to insert exactly one lowercase English letter into s to make it a palindrome. A palindrome is a string that reads the same forward and backward. For example, \"noon\", \"testset\" and \"a\" are all palindromes, while \"test\" and \"kitayuta\" are not.\n\nYou can choose any lowercase English letter, and insert it to any position of s, possibly to the beginning or the end of s. You have to insert a letter even if the given string is already a palindrome.\n\nIf it is possible to insert one lowercase English letter into s so that the resulting string will be a palindrome, print the string after the insertion. Otherwise, print \"NA\" (without quotes, case-sensitive). In case there is more than one palindrome that can be obtained, you are allowed to print any of them.\n\n\n-----Input-----\n\nThe only line of the input contains a string s (1 \u2264 |s| \u2264 10). Each character in s is a lowercase English letter.\n\n\n-----Output-----\n\nIf it is possible to turn s into a palindrome by inserting one lowercase English letter, print the resulting string in a single line. Otherwise, print \"NA\" (without quotes, case-sensitive). In case there is more than one solution, any of them will be accepted. \n\n\n-----Examples-----\nInput\nrevive\n\nOutput\nreviver\n\nInput\nee\n\nOutput\neye\nInput\nkitayuta\n\nOutput\nNA\n\n\n\n-----Note-----\n\nFor the first sample, insert 'r' to the end of \"revive\" to obtain a palindrome \"reviver\".\n\nFor the second sample, there is more than one solution. For example, \"eve\" will also be accepted.\n\nFor the third sample, it is not possible to turn \"kitayuta\" into a palindrome by just inserting one letter."} +{"id": "00600", "text": "Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x_1 = a, another one is in the point x_2 = b. \n\nEach of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third\u00a0\u2014 by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6.\n\nThe friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point.\n\n\n-----Input-----\n\nThe first line contains a single integer a (1 \u2264 a \u2264 1000) \u2014 the initial position of the first friend. \n\nThe second line contains a single integer b (1 \u2264 b \u2264 1000) \u2014 the initial position of the second friend.\n\nIt is guaranteed that a \u2260 b.\n\n\n-----Output-----\n\nPrint the minimum possible total tiredness if the friends meet in the same point.\n\n\n-----Examples-----\nInput\n3\n4\n\nOutput\n1\n\nInput\n101\n99\n\nOutput\n2\n\nInput\n5\n10\n\nOutput\n9\n\n\n\n-----Note-----\n\nIn the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1.\n\nIn the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2.\n\nIn the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend \u2014 two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9."} +{"id": "00601", "text": "You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.\n\nYou decided to rob a town's blacksmith and you take a follower with you. You can carry at most $p$ units and your follower\u00a0\u2014 at most $f$ units.\n\nIn the blacksmith shop, you found $cnt_s$ swords and $cnt_w$ war axes. Each sword weights $s$ units and each war axe\u00a0\u2014 $w$ units. You don't care what to take, since each of them will melt into one steel ingot.\n\nWhat is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains two integers $p$ and $f$ ($1 \\le p, f \\le 10^9$)\u00a0\u2014 yours and your follower's capacities.\n\nThe second line of each test case contains two integers $cnt_s$ and $cnt_w$ ($1 \\le cnt_s, cnt_w \\le 2 \\cdot 10^5$)\u00a0\u2014 the number of swords and war axes in the shop.\n\nThe third line of each test case contains two integers $s$ and $w$ ($1 \\le s, w \\le 10^9$)\u00a0\u2014 the weights of each sword and each war axe.\n\nIt's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.\n\n\n-----Example-----\nInput\n3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5\n\nOutput\n11\n20\n3\n\n\n\n-----Note-----\n\nIn the first test case: you should take $3$ swords and $3$ war axes: $3 \\cdot 5 + 3 \\cdot 6 = 33 \\le 33$ and your follower\u00a0\u2014 $3$ swords and $2$ war axes: $3 \\cdot 5 + 2 \\cdot 6 = 27 \\le 27$. $3 + 3 + 3 + 2 = 11$ weapons in total.\n\nIn the second test case, you can take all available weapons even without your follower's help, since $5 \\cdot 10 + 5 \\cdot 10 \\le 100$.\n\nIn the third test case, you can't take anything, but your follower can take $3$ war axes: $3 \\cdot 5 \\le 19$."} +{"id": "00602", "text": "-----Input-----\n\nThe input contains a single integer a (1 \u2264 a \u2264 40).\n\n\n-----Output-----\n\nOutput a single string.\n\n\n-----Examples-----\nInput\n2\n\nOutput\nAdams\n\nInput\n8\n\nOutput\nVan Buren\n\nInput\n29\n\nOutput\nHarding"} +{"id": "00603", "text": "Fox Ciel has some flowers: r red flowers, g green flowers and b blue flowers. She wants to use these flowers to make several bouquets. There are 4 types of bouquets:\n\n To make a \"red bouquet\", it needs 3 red flowers. To make a \"green bouquet\", it needs 3 green flowers. To make a \"blue bouquet\", it needs 3 blue flowers. To make a \"mixing bouquet\", it needs 1 red, 1 green and 1 blue flower. \n\nHelp Fox Ciel to find the maximal number of bouquets she can make.\n\n\n-----Input-----\n\nThe first line contains three integers r, g and b (0 \u2264 r, g, b \u2264 10^9) \u2014 the number of red, green and blue flowers.\n\n\n-----Output-----\n\nPrint the maximal number of bouquets Fox Ciel can make.\n\n\n-----Examples-----\nInput\n3 6 9\n\nOutput\n6\n\nInput\n4 4 4\n\nOutput\n4\n\nInput\n0 0 0\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn test case 1, we can make 1 red bouquet, 2 green bouquets and 3 blue bouquets.\n\nIn test case 2, we can make 1 red, 1 green, 1 blue and 1 mixing bouquet."} +{"id": "00604", "text": "Nastya owns too many arrays now, so she wants to delete the least important of them. However, she discovered that this array is magic! Nastya now knows that the array has the following properties:\n\n In one second we can add an arbitrary (possibly negative) integer to all elements of the array that are not equal to zero. When all elements of the array become equal to zero, the array explodes. \n\nNastya is always busy, so she wants to explode the array as fast as possible. Compute the minimum time in which the array can be exploded.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5) \u2014 the size of the array.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} ( - 10^5 \u2264 a_{i} \u2264 10^5) \u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of seconds needed to make all elements of the array equal to zero.\n\n\n-----Examples-----\nInput\n5\n1 1 1 1 1\n\nOutput\n1\n\nInput\n3\n2 0 -1\n\nOutput\n2\n\nInput\n4\n5 -6 -5 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example you can add - 1 to all non-zero elements in one second and make them equal to zero.\n\nIn the second example you can add - 2 on the first second, then the array becomes equal to [0, 0, - 3]. On the second second you can add 3 to the third (the only non-zero) element."} +{"id": "00605", "text": "Misha and Vasya participated in a Codeforces contest. Unfortunately, each of them solved only one problem, though successfully submitted it at the first attempt. Misha solved the problem that costs a points and Vasya solved the problem that costs b points. Besides, Misha submitted the problem c minutes after the contest started and Vasya submitted the problem d minutes after the contest started. As you know, on Codeforces the cost of a problem reduces as a round continues. That is, if you submit a problem that costs p points t minutes after the contest started, you get $\\operatorname{max}(\\frac{3p}{10}, p - \\frac{p}{250} \\times t)$ points. \n\nMisha and Vasya are having an argument trying to find out who got more points. Help them to find out the truth.\n\n\n-----Input-----\n\nThe first line contains four integers a, b, c, d (250 \u2264 a, b \u2264 3500, 0 \u2264 c, d \u2264 180). \n\nIt is guaranteed that numbers a and b are divisible by 250 (just like on any real Codeforces round).\n\n\n-----Output-----\n\nOutput on a single line: \n\n\"Misha\" (without the quotes), if Misha got more points than Vasya.\n\n\"Vasya\" (without the quotes), if Vasya got more points than Misha.\n\n\"Tie\" (without the quotes), if both of them got the same number of points.\n\n\n-----Examples-----\nInput\n500 1000 20 30\n\nOutput\nVasya\n\nInput\n1000 1000 1 1\n\nOutput\nTie\n\nInput\n1500 1000 176 177\n\nOutput\nMisha"} +{"id": "00606", "text": "Fifa and Fafa are sharing a flat. Fifa loves video games and wants to download a new soccer game. Unfortunately, Fafa heavily uses the internet which consumes the quota. Fifa can access the internet through his Wi-Fi access point. This access point can be accessed within a range of r meters (this range can be chosen by Fifa) from its position. Fifa must put the access point inside the flat which has a circular shape of radius R. Fifa wants to minimize the area that is not covered by the access point inside the flat without letting Fafa or anyone outside the flat to get access to the internet.\n\nThe world is represented as an infinite 2D plane. The flat is centered at (x_1, y_1) and has radius R and Fafa's laptop is located at (x_2, y_2), not necessarily inside the flat. Find the position and the radius chosen by Fifa for his access point which minimizes the uncovered area.\n\n\n-----Input-----\n\nThe single line of the input contains 5 space-separated integers R, x_1, y_1, x_2, y_2 (1 \u2264 R \u2264 10^5, |x_1|, |y_1|, |x_2|, |y_2| \u2264 10^5).\n\n\n-----Output-----\n\nPrint three space-separated numbers x_{ap}, y_{ap}, r where (x_{ap}, y_{ap}) is the position which Fifa chose for the access point and r is the radius of its range. \n\nYour answer will be considered correct if the radius does not differ from optimal more than 10^{ - 6} absolutely or relatively, and also the radius you printed can be changed by no more than 10^{ - 6} (absolutely or relatively) in such a way that all points outside the flat and Fafa's laptop position are outside circle of the access point range.\n\n\n-----Examples-----\nInput\n5 3 3 1 1\n\nOutput\n3.7677669529663684 3.7677669529663684 3.914213562373095\n\nInput\n10 5 5 5 15\n\nOutput\n5.0 5.0 10.0"} +{"id": "00607", "text": "Recall that the permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).\n\nA sequence $a$ is a subsegment of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as $[l, r]$, where $l, r$ are two integers with $1 \\le l \\le r \\le n$. This indicates the subsegment where $l-1$ elements from the beginning and $n-r$ elements from the end are deleted from the sequence.\n\nFor a permutation $p_1, p_2, \\ldots, p_n$, we define a framed segment as a subsegment $[l,r]$ where $\\max\\{p_l, p_{l+1}, \\dots, p_r\\} - \\min\\{p_l, p_{l+1}, \\dots, p_r\\} = r - l$. For example, for the permutation $(6, 7, 1, 8, 5, 3, 2, 4)$ some of its framed segments are: $[1, 2], [5, 8], [6, 7], [3, 3], [8, 8]$. In particular, a subsegment $[i,i]$ is always a framed segments for any $i$ between $1$ and $n$, inclusive.\n\nWe define the happiness of a permutation $p$ as the number of pairs $(l, r)$ such that $1 \\le l \\le r \\le n$, and $[l, r]$ is a framed segment. For example, the permutation $[3, 1, 2]$ has happiness $5$: all segments except $[1, 2]$ are framed segments.\n\nGiven integers $n$ and $m$, Jongwon wants to compute the sum of happiness for all permutations of length $n$, modulo the prime number $m$. Note that there exist $n!$ (factorial of $n$) different permutations of length $n$.\n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $m$ ($1 \\le n \\le 250\\,000$, $10^8 \\le m \\le 10^9$, $m$ is prime).\n\n\n-----Output-----\n\nPrint $r$ ($0 \\le r < m$), the sum of happiness for all permutations of length $n$, modulo a prime number $m$.\n\n\n-----Examples-----\nInput\n1 993244853\n\nOutput\n1\n\nInput\n2 993244853\n\nOutput\n6\n\nInput\n3 993244853\n\nOutput\n32\n\nInput\n2019 993244853\n\nOutput\n923958830\n\nInput\n2020 437122297\n\nOutput\n265955509\n\n\n\n-----Note-----\n\nFor sample input $n=3$, let's consider all permutations of length $3$: $[1, 2, 3]$, all subsegments are framed segment. Happiness is $6$. $[1, 3, 2]$, all subsegments except $[1, 2]$ are framed segment. Happiness is $5$. $[2, 1, 3]$, all subsegments except $[2, 3]$ are framed segment. Happiness is $5$. $[2, 3, 1]$, all subsegments except $[2, 3]$ are framed segment. Happiness is $5$. $[3, 1, 2]$, all subsegments except $[1, 2]$ are framed segment. Happiness is $5$. $[3, 2, 1]$, all subsegments are framed segment. Happiness is $6$. \n\nThus, the sum of happiness is $6+5+5+5+5+6 = 32$."} +{"id": "00608", "text": "\u0420\u043e\u0434\u0438\u0442\u0435\u043b\u0438 \u0412\u0430\u0441\u0438 \u0445\u043e\u0442\u044f\u0442, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u0443\u0447\u0438\u043b\u0441\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u043e\u0434\u0440\u044f\u0434 \u0442\u0440\u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0446\u0435\u043d\u043a\u0438 (\u00ab\u0447\u0435\u0442\u0432\u0451\u0440\u043a\u0438\u00bb \u0438\u043b\u0438 \u00ab\u043f\u044f\u0442\u0451\u0440\u043a\u0438\u00bb), \u043e\u043d\u0438 \u0434\u0430\u0440\u044f\u0442 \u0435\u043c\u0443 \u043f\u043e\u0434\u0430\u0440\u043e\u043a. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043e\u0446\u0435\u043d\u043a\u0438 \u00ab\u0435\u0434\u0438\u043d\u0438\u0446\u0430\u00bb, \u00ab\u0434\u0432\u043e\u0439\u043a\u0430\u00bb \u0438 \u00ab\u0442\u0440\u043e\u0439\u043a\u0430\u00bb \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0438 \u0412\u0430\u0441\u0438 \u0441\u0447\u0438\u0442\u0430\u044e\u0442 \u043f\u043b\u043e\u0445\u0438\u043c\u0438. \u041a\u043e\u0433\u0434\u0430 \u0412\u0430\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u043e\u0434\u0440\u044f\u0434 \u0442\u0440\u0438 \u0445\u043e\u0440\u043e\u0448\u0438\u0435 \u043e\u0446\u0435\u043d\u043a\u0438, \u0435\u043c\u0443 \u0441\u0440\u0430\u0437\u0443 \u0432\u0440\u0443\u0447\u0430\u044e\u0442 \u043f\u043e\u0434\u0430\u0440\u043e\u043a, \u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u043f\u043e\u0434\u0430\u0440\u043e\u043a, \u0435\u043c\u0443 \u0432\u043d\u043e\u0432\u044c \u043d\u0430\u0434\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0434\u0440\u044f\u0434 \u0435\u0449\u0451 \u0442\u0440\u0438 \u0445\u043e\u0440\u043e\u0448\u0438\u0435 \u043e\u0446\u0435\u043d\u043a\u0438.\n\n\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0412\u0430\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u043f\u043e\u0434\u0440\u044f\u0434 \u043f\u044f\u0442\u044c \u00ab\u0447\u0435\u0442\u0432\u0451\u0440\u043e\u043a\u00bb \u043e\u0446\u0435\u043d\u043e\u043a, \u0430 \u043f\u043e\u0442\u043e\u043c \u00ab\u0434\u0432\u043e\u0439\u043a\u0443\u00bb, \u0442\u043e \u0435\u043c\u0443 \u0434\u0430\u0434\u0443\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u043e\u0434\u0430\u0440\u043e\u043a, \u0430 \u0432\u043e\u0442 \u0435\u0441\u043b\u0438 \u0431\u044b \u00ab\u0447\u0435\u0442\u0432\u0451\u0440\u043e\u043a\u00bb \u0431\u044b\u043b\u043e \u0443\u0436\u0435 \u0448\u0435\u0441\u0442\u044c, \u0442\u043e \u043f\u043e\u0434\u0430\u0440\u043a\u043e\u0432 \u0431\u044b\u043b\u043e \u0431\u044b \u0434\u0432\u0430. \n\n\u0417\u0430 \u043c\u0435\u0441\u044f\u0446 \u0412\u0430\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b n \u043e\u0446\u0435\u043d\u043e\u043a. \u0412\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u0430\u0440\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u0412\u0430\u0441\u044f. \u041e\u0446\u0435\u043d\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u0434\u0430\u043d\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0412\u0430\u0441\u044f \u0438\u0445 \u043f\u043e\u043b\u0443\u0447\u0430\u043b. \n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0446\u0435\u043b\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e n (3 \u2264 n \u2264 1000)\u00a0\u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0446\u0435\u043d\u043e\u043a, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0412\u0430\u0441\u0435\u0439.\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438\u0437 n \u0447\u0438\u0441\u0435\u043b a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 5)\u00a0\u2014 \u043e\u0446\u0435\u043d\u043a\u0438, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0412\u0430\u0441\u0435\u0439. \u041e\u0446\u0435\u043d\u043a\u0438 \u0437\u0430\u0434\u0430\u043d\u044b \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0412\u0430\u0441\u044f \u0438\u0445 \u043f\u043e\u043b\u0443\u0447\u0438\u043b. \n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043e\u0434\u043d\u043e \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e\u00a0\u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u0430\u0440\u043a\u043e\u0432, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0412\u0430\u0441\u0435\u0439.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n6\n4 5 4 5 4 4\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n14\n1 5 4 5 2 4 4 5 5 4 3 4 5 5\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0412\u0430\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0434\u0432\u0430 \u043f\u043e\u0434\u0430\u0440\u043a\u0430\u00a0\u2014 \u0437\u0430 \u043f\u0435\u0440\u0432\u044b\u0435 \u0442\u0440\u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u0446\u0435\u043d\u043a\u0438 \u0438 \u0437\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0442\u0440\u043e\u0439\u043a\u0443 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043e\u0446\u0435\u043d\u043e\u043a \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e."} +{"id": "00609", "text": "Valera is a little boy. Yesterday he got a huge Math hometask at school, so Valera didn't have enough time to properly learn the English alphabet for his English lesson. Unfortunately, the English teacher decided to have a test on alphabet today. At the test Valera got a square piece of squared paper. The length of the side equals n squares (n is an odd number) and each unit square contains some small letter of the English alphabet.\n\nValera needs to know if the letters written on the square piece of paper form letter \"X\". Valera's teacher thinks that the letters on the piece of paper form an \"X\", if: on both diagonals of the square paper all letters are the same; all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals. \n\nHelp Valera, write the program that completes the described task for him.\n\n\n-----Input-----\n\nThe first line contains integer n (3 \u2264 n < 300; n is odd). Each of the next n lines contains n small English letters \u2014 the description of Valera's paper.\n\n\n-----Output-----\n\nPrint string \"YES\", if the letters on the paper form letter \"X\". Otherwise, print string \"NO\". Print the strings without quotes.\n\n\n-----Examples-----\nInput\n5\nxooox\noxoxo\nsoxoo\noxoxo\nxooox\n\nOutput\nNO\n\nInput\n3\nwsw\nsws\nwsw\n\nOutput\nYES\n\nInput\n3\nxpx\npxp\nxpe\n\nOutput\nNO"} +{"id": "00610", "text": "Petya and Vasya decided to play a little. They found n red cubes and m blue cubes. The game goes like that: the players take turns to choose a cube of some color (red or blue) and put it in a line from left to right (overall the line will have n + m cubes). Petya moves first. Petya's task is to get as many pairs of neighbouring cubes of the same color as possible. Vasya's task is to get as many pairs of neighbouring cubes of different colors as possible. \n\nThe number of Petya's points in the game is the number of pairs of neighboring cubes of the same color in the line, the number of Vasya's points in the game is the number of neighbouring cubes of the different color in the line. Your task is to calculate the score at the end of the game (Petya's and Vasya's points, correspondingly), if both boys are playing optimally well. To \"play optimally well\" first of all means to maximize the number of one's points, and second \u2014 to minimize the number of the opponent's points.\n\n\n-----Input-----\n\nThe only line contains two space-separated integers n and m (1 \u2264 n, m \u2264 10^5) \u2014 the number of red and blue cubes, correspondingly.\n\n\n-----Output-----\n\nOn a single line print two space-separated integers \u2014 the number of Petya's and Vasya's points correspondingly provided that both players play optimally well.\n\n\n-----Examples-----\nInput\n3 1\n\nOutput\n2 1\n\nInput\n2 4\n\nOutput\n3 2\n\n\n\n-----Note-----\n\nIn the first test sample the optimal strategy for Petya is to put the blue cube in the line. After that there will be only red cubes left, so by the end of the game the line of cubes from left to right will look as [blue, red, red, red]. So, Petya gets 2 points and Vasya gets 1 point. \n\nIf Petya would choose the red cube during his first move, then, provided that both boys play optimally well, Petya would get 1 point and Vasya would get 2 points."} +{"id": "00611", "text": "Alice got an array of length $n$ as a birthday present once again! This is the third year in a row! \n\nAnd what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.\n\nBob has chosen $m$ changes of the following form. For some integer numbers $x$ and $d$, he chooses an arbitrary position $i$ ($1 \\le i \\le n$) and for every $j \\in [1, n]$ adds $x + d \\cdot dist(i, j)$ to the value of the $j$-th cell. $dist(i, j)$ is the distance between positions $i$ and $j$ (i.e. $dist(i, j) = |i - j|$, where $|x|$ is an absolute value of $x$).\n\nFor example, if Alice currently has an array $[2, 1, 2, 2]$ and Bob chooses position $3$ for $x = -1$ and $d = 2$ then the array will become $[2 - 1 + 2 \\cdot 2,~1 - 1 + 2 \\cdot 1,~2 - 1 + 2 \\cdot 0,~2 - 1 + 2 \\cdot 1]$ = $[5, 2, 1, 3]$. Note that Bob can't choose position $i$ outside of the array (that is, smaller than $1$ or greater than $n$).\n\nAlice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.\n\nWhat is the maximum arithmetic mean value Bob can achieve?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 10^5$) \u2014 the number of elements of the array and the number of changes.\n\nEach of the next $m$ lines contains two integers $x_i$ and $d_i$ ($-10^3 \\le x_i, d_i \\le 10^3$) \u2014 the parameters for the $i$-th change.\n\n\n-----Output-----\n\nPrint the maximal average arithmetic mean of the elements Bob can achieve.\n\nYour answer is considered correct if its absolute or relative error doesn't exceed $10^{-6}$.\n\n\n-----Examples-----\nInput\n2 3\n-1 3\n0 0\n-1 -4\n\nOutput\n-2.500000000000000\n\nInput\n3 2\n0 2\n5 0\n\nOutput\n7.000000000000000"} +{"id": "00612", "text": "Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him?\n\nGiven an array consisting of distinct integers. Is it possible to partition the whole array into k disjoint non-empty parts such that p of the parts have even sum (each of them must have even sum) and remaining k - p have odd sum? (note that parts need not to be continuous).\n\nIf it is possible to partition the array, also give any possible way of valid partitioning.\n\n\n-----Input-----\n\nThe first line will contain three space separated integers n, k, p (1 \u2264 k \u2264 n \u2264 10^5;\u00a00 \u2264 p \u2264 k). The next line will contain n space-separated distinct integers representing the content of array a: a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nIn the first line print \"YES\" (without the quotes) if it is possible to partition the array in the required way. Otherwise print \"NO\" (without the quotes).\n\nIf the required partition exists, print k lines after the first line. The i^{th} of them should contain the content of the i^{th} part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly p parts with even sum, each of the remaining k - p parts must have odd sum.\n\nAs there can be multiple partitions, you are allowed to print any valid partition.\n\n\n-----Examples-----\nInput\n5 5 3\n2 6 10 5 9\n\nOutput\nYES\n1 9\n1 5\n1 10\n1 6\n1 2\n\nInput\n5 5 3\n7 14 2 9 5\n\nOutput\nNO\n\nInput\n5 3 1\n1 2 3 7 5\n\nOutput\nYES\n3 5 1 3\n1 7\n1 2"} +{"id": "00613", "text": "Vasya is studying in the last class of school and soon he will take exams. He decided to study polynomials. Polynomial is a function P(x) = a_0 + a_1x^1 + ... + a_{n}x^{n}. Numbers a_{i} are called coefficients of a polynomial, non-negative integer n is called a degree of a polynomial.\n\nVasya has made a bet with his friends that he can solve any problem with polynomials. They suggested him the problem: \"Determine how many polynomials P(x) exist with integer non-negative coefficients so that $P(t) = a$, and $P(P(t)) = b$, where $t, a$ and b are given positive integers\"? \n\nVasya does not like losing bets, but he has no idea how to solve this task, so please help him to solve the problem.\n\n\n-----Input-----\n\nThe input contains three integer positive numbers $t, a, b$ no greater than 10^18.\n\n\n-----Output-----\n\nIf there is an infinite number of such polynomials, then print \"inf\" without quotes, otherwise print the reminder of an answer modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n2 2 2\n\nOutput\n2\n\nInput\n2 3 3\n\nOutput\n1"} +{"id": "00614", "text": "After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market.\n\nThis morning, as usual, Petya will come to the market. Petya has n different souvenirs to sell; ith souvenir is characterised by its weight w_{i} and cost c_{i}. Petya knows that he might not be able to carry all the souvenirs to the market. So Petya wants to choose a subset of souvenirs such that its total weight is not greater than m, and total cost is maximum possible.\n\nHelp Petya to determine maximum possible total cost.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 100000, 1 \u2264 m \u2264 300000) \u2014 the number of Petya's souvenirs and total weight that he can carry to the market.\n\nThen n lines follow. ith line contains two integers w_{i} and c_{i} (1 \u2264 w_{i} \u2264 3, 1 \u2264 c_{i} \u2264 10^9) \u2014 the weight and the cost of ith souvenir.\n\n\n-----Output-----\n\nPrint one number \u2014 maximum possible total cost of souvenirs that Petya can carry to the market.\n\n\n-----Examples-----\nInput\n1 1\n2 1\n\nOutput\n0\n\nInput\n2 2\n1 3\n2 2\n\nOutput\n3\n\nInput\n4 3\n3 10\n2 7\n2 8\n1 1\n\nOutput\n10"} +{"id": "00615", "text": "Snuke has an integer sequence A of length N.\nHe will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E.\nThe positions of the cuts can be freely chosen.\nLet P,Q,R,S be the sums of the elements in B,C,D,E, respectively.\nSnuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller.\nFind the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.\n\n-----Constraints-----\n - 4 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq A_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nFind the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.\n\n-----Sample Input-----\n5\n3 2 4 1 2\n\n-----Sample Output-----\n2\n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3.\nHere, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2.\nWe cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2."} +{"id": "00616", "text": "We have N locked treasure boxes, numbered 1 to N.\nA shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.\nFind the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 12\n - 1 \\leq M \\leq 10^3\n - 1 \\leq a_i \\leq 10^5\n - 1 \\leq b_i \\leq N\n - 1 \\leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \\leq N\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\na_1 b_1\nc_{11} c_{12} ... c_{1{b_1}}\n:\na_M b_M\nc_{M1} c_{M2} ... c_{M{b_M}}\n\n-----Output-----\nPrint the minimum cost required to unlock all the treasure boxes.\nIf it is impossible to unlock all of them, print -1.\n\n-----Sample Input-----\n2 3\n10 1\n1\n15 1\n2\n30 2\n1 2\n\n-----Sample Output-----\n25\n\nWe can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required."} +{"id": "00617", "text": "Vanya is doing his maths homework. He has an expression of form $x_{1} \\diamond x_{2} \\diamond x_{3} \\diamond \\ldots \\diamond x_{n}$, where x_1, x_2, ..., x_{n} are digits from 1 to 9, and sign [Image] represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.\n\n\n-----Input-----\n\nThe first line contains expression s (1 \u2264 |s| \u2264 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * . \n\nThe number of signs * doesn't exceed 15.\n\n\n-----Output-----\n\nIn the first line print the maximum possible value of an expression.\n\n\n-----Examples-----\nInput\n3+5*7+8*4\n\nOutput\n303\n\nInput\n2+3*5\n\nOutput\n25\n\nInput\n3*4*5\n\nOutput\n60\n\n\n\n-----Note-----\n\nNote to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.\n\nNote to the second sample test. (2 + 3) * 5 = 25.\n\nNote to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60)."} +{"id": "00618", "text": "Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium. \n\nThe scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan.\n\n\n-----Input-----\n\nThe first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol \"|\" indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale. \n\nThe second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet. \n\nIt is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters.\n\n\n-----Output-----\n\nIf you cannot put all the weights on the scales so that the scales were in equilibrium, print string \"Impossible\". Otherwise, print the description of the resulting scales, copy the format of the input.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\nAC|T\nL\n\nOutput\nAC|TL\n\nInput\n|ABC\nXYZ\n\nOutput\nXYZ|ABC\n\nInput\nW|T\nF\n\nOutput\nImpossible\n\nInput\nABC|\nD\n\nOutput\nImpossible"} +{"id": "00619", "text": "Soon after the Chunga-Changa island was discovered, it started to acquire some forms of civilization and even market economy. A new currency arose, colloquially called \"chizhik\". One has to pay in chizhiks to buy a coconut now.\n\nSasha and Masha are about to buy some coconuts which are sold at price $z$ chizhiks per coconut. Sasha has $x$ chizhiks, Masha has $y$ chizhiks. Each girl will buy as many coconuts as she can using only her money. This way each girl will buy an integer non-negative number of coconuts.\n\nThe girls discussed their plans and found that the total number of coconuts they buy can increase (or decrease) if one of them gives several chizhiks to the other girl. The chizhiks can't be split in parts, so the girls can only exchange with integer number of chizhiks.\n\nConsider the following example. Suppose Sasha has $5$ chizhiks, Masha has $4$ chizhiks, and the price for one coconut be $3$ chizhiks. If the girls don't exchange with chizhiks, they will buy $1 + 1 = 2$ coconuts. However, if, for example, Masha gives Sasha one chizhik, then Sasha will have $6$ chizhiks, Masha will have $3$ chizhiks, and the girls will buy $2 + 1 = 3$ coconuts. \n\nIt is not that easy to live on the island now, so Sasha and Mash want to exchange with chizhiks in such a way that they will buy the maximum possible number of coconuts. Nobody wants to have a debt, so among all possible ways to buy the maximum possible number of coconuts find such a way that minimizes the number of chizhiks one girl gives to the other (it is not important who will be the person giving the chizhiks).\n\n\n-----Input-----\n\nThe first line contains three integers $x$, $y$ and $z$ ($0 \\le x, y \\le 10^{18}$, $1 \\le z \\le 10^{18}$)\u00a0\u2014 the number of chizhics Sasha has, the number of chizhics Masha has and the price of a coconut. \n\n\n-----Output-----\n\nPrint two integers: the maximum possible number of coconuts the girls can buy and the minimum number of chizhiks one girl has to give to the other.\n\n\n-----Examples-----\nInput\n5 4 3\n\nOutput\n3 1\n\nInput\n6 8 2\n\nOutput\n7 0\n\n\n\n-----Note-----\n\nThe first example is described in the statement. In the second example the optimal solution is to dot exchange any chizhiks. The girls will buy $3 + 4 = 7$ coconuts."} +{"id": "00620", "text": "Long time ago Alex created an interesting problem about parallelogram. The input data for this problem contained four integer points on the Cartesian plane, that defined the set of vertices of some non-degenerate (positive area) parallelogram. Points not necessary were given in the order of clockwise or counterclockwise traversal.\n\nAlex had very nice test for this problem, but is somehow happened that the last line of the input was lost and now he has only three out of four points of the original parallelogram. He remembers that test was so good that he asks you to restore it given only these three points.\n\n\n-----Input-----\n\nThe input consists of three lines, each containing a pair of integer coordinates x_{i} and y_{i} ( - 1000 \u2264 x_{i}, y_{i} \u2264 1000). It's guaranteed that these three points do not lie on the same line and no two of them coincide.\n\n\n-----Output-----\n\nFirst print integer k\u00a0\u2014 the number of ways to add one new integer point such that the obtained set defines some parallelogram of positive area. There is no requirement for the points to be arranged in any special order (like traversal), they just define the set of vertices.\n\nThen print k lines, each containing a pair of integer\u00a0\u2014 possible coordinates of the fourth point.\n\n\n-----Example-----\nInput\n0 0\n1 0\n0 1\n\nOutput\n3\n1 -1\n-1 1\n1 1\n\n\n\n-----Note-----\n\nIf you need clarification of what parallelogram is, please check Wikipedia page:\n\nhttps://en.wikipedia.org/wiki/Parallelogram"} +{"id": "00621", "text": "Polycarpus has been working in the analytic department of the \"F.R.A.U.D.\" company for as much as n days. Right now his task is to make a series of reports about the company's performance for the last n days. We know that the main information in a day report is value a_{i}, the company's profit on the i-th day. If a_{i} is negative, then the company suffered losses on the i-th day.\n\nPolycarpus should sort the daily reports into folders. Each folder should include data on the company's performance for several consecutive days. Of course, the information on each of the n days should be exactly in one folder. Thus, Polycarpus puts information on the first few days in the first folder. The information on the several following days goes to the second folder, and so on.\n\nIt is known that the boss reads one daily report folder per day. If one folder has three or more reports for the days in which the company suffered losses (a_{i} < 0), he loses his temper and his wrath is terrible.\n\nTherefore, Polycarpus wants to prepare the folders so that none of them contains information on three or more days with the loss, and the number of folders is minimal.\n\nWrite a program that, given sequence a_{i}, will print the minimum number of folders.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100), n is the number of days. The second line contains a sequence of integers a_1, a_2, ..., a_{n} (|a_{i}| \u2264 100), where a_{i} means the company profit on the i-th day. It is possible that the company has no days with the negative a_{i}.\n\n\n-----Output-----\n\nPrint an integer k \u2014 the required minimum number of folders. In the second line print a sequence of integers b_1, b_2, ..., b_{k}, where b_{j} is the number of day reports in the j-th folder.\n\nIf there are multiple ways to sort the reports into k days, print any of them.\n\n\n-----Examples-----\nInput\n11\n1 2 3 -4 -5 -6 5 -5 -6 -7 6\n\nOutput\n3\n5 3 3 \nInput\n5\n0 -1 100 -1 0\n\nOutput\n1\n5 \n\n\n-----Note-----\n\nHere goes a way to sort the reports from the first sample into three folders: 1 2 3 -4 -5 | -6 5 -5 | -6 -7 6 \n\nIn the second sample you can put all five reports in one folder."} +{"id": "00622", "text": "Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems to get to the olympiad like Vladik, but she was confused by the task proposed on the olympiad.\n\nLet's consider the following algorithm of generating a sequence of integers. Initially we have a sequence consisting of a single element equal to 1. Then we perform (n - 1) steps. On each step we take the sequence we've got on the previous step, append it to the end of itself and insert in the middle the minimum positive integer we haven't used before. For example, we get the sequence [1, 2, 1] after the first step, the sequence [1, 2, 1, 3, 1, 2, 1] after the second step.\n\nThe task is to find the value of the element with index k (the elements are numbered from 1) in the obtained sequence, i.\u00a0e. after (n - 1) steps.\n\nPlease help Chloe to solve the problem!\n\n\n-----Input-----\n\nThe only line contains two integers n and k (1 \u2264 n \u2264 50, 1 \u2264 k \u2264 2^{n} - 1).\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the integer at the k-th position in the obtained sequence.\n\n\n-----Examples-----\nInput\n3 2\n\nOutput\n2\nInput\n4 8\n\nOutput\n4\n\n\n-----Note-----\n\nIn the first sample the obtained sequence is [1, 2, 1, 3, 1, 2, 1]. The number on the second position is 2.\n\nIn the second sample the obtained sequence is [1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1]. The number on the eighth position is 4."} +{"id": "00623", "text": "Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at a_1 percent and second one is charged at a_2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not connected to a charger) or charges by 1 percent (if connected to a charger).\n\nGame continues while both joysticks have a positive charge. Hence, if at the beginning of minute some joystick is charged by 1 percent, it has to be connected to a charger, otherwise the game stops. If some joystick completely discharges (its charge turns to 0), the game also stops.\n\nDetermine the maximum number of minutes that game can last. It is prohibited to pause the game, i. e. at each moment both joysticks should be enabled. It is allowed for joystick to be charged by more than 100 percent.\n\n\n-----Input-----\n\nThe first line of the input contains two positive integers a_1 and a_2 (1 \u2264 a_1, a_2 \u2264 100), the initial charge level of first and second joystick respectively.\n\n\n-----Output-----\n\nOutput the only integer, the maximum number of minutes that the game can last. Game continues until some joystick is discharged.\n\n\n-----Examples-----\nInput\n3 5\n\nOutput\n6\n\nInput\n4 4\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first sample game lasts for 6 minute by using the following algorithm: at the beginning of the first minute connect first joystick to the charger, by the end of this minute first joystick is at 4%, second is at 3%; continue the game without changing charger, by the end of the second minute the first joystick is at 5%, second is at 1%; at the beginning of the third minute connect second joystick to the charger, after this minute the first joystick is at 3%, the second one is at 2%; continue the game without changing charger, by the end of the fourth minute first joystick is at 1%, second one is at 3%; at the beginning of the fifth minute connect first joystick to the charger, after this minute the first joystick is at 2%, the second one is at 1%; at the beginning of the sixth minute connect second joystick to the charger, after this minute the first joystick is at 0%, the second one is at 2%. \n\nAfter that the first joystick is completely discharged and the game is stopped."} +{"id": "00624", "text": "Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.\n\nInitially, there are $n$ superheroes in avengers team having powers $a_1, a_2, \\ldots, a_n$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $1$. They can do at most $m$ operations. Also, on a particular superhero at most $k$ operations can be done.\n\nCan you help the avengers team to maximize the average power of their crew?\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $k$ and $m$ ($1 \\le n \\le 10^{5}$, $1 \\le k \\le 10^{5}$, $1 \\le m \\le 10^{7}$)\u00a0\u2014 the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^{6}$)\u00a0\u2014 the initial powers of the superheroes in the cast of avengers.\n\n\n-----Output-----\n\nOutput a single number\u00a0\u2014 the maximum final average power.\n\nYour answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.\n\nFormally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\\frac{|a - b|}{\\max{(1, |b|)}} \\le 10^{-6}$.\n\n\n-----Examples-----\nInput\n2 4 6\n4 7\n\nOutput\n11.00000000000000000000\n\nInput\n4 2 6\n1 3 2 3\n\nOutput\n5.00000000000000000000\n\n\n\n-----Note-----\n\nIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.\n\nIn the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $2$ each."} +{"id": "00625", "text": "For a positive integer n let's define a function f:\n\nf(n) = - 1 + 2 - 3 + .. + ( - 1)^{n}n \n\nYour task is to calculate f(n) for a given integer n.\n\n\n-----Input-----\n\nThe single line contains the positive integer n (1 \u2264 n \u2264 10^15).\n\n\n-----Output-----\n\nPrint f(n) in a single line.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n2\n\nInput\n5\n\nOutput\n-3\n\n\n\n-----Note-----\n\nf(4) = - 1 + 2 - 3 + 4 = 2\n\nf(5) = - 1 + 2 - 3 + 4 - 5 = - 3"} +{"id": "00626", "text": "Robot Doc is located in the hall, with n computers stand in a line, numbered from left to right from 1 to n. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the i-th of them, the robot needs to collect at least a_{i} any pieces of information from the other computers. Doc can hack the computer only if he is right next to it.\n\nThe robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all n parts of information if initially it is next to computer with number 1.\n\nIt is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information.\n\n\n-----Input-----\n\nThe first line contains number n (1 \u2264 n \u2264 1000). The second line contains n non-negative integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} < n), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum number of changes in direction that the robot will have to make in order to collect all n parts of information.\n\n\n-----Examples-----\nInput\n3\n0 2 0\n\nOutput\n1\n\nInput\n5\n4 2 3 0 1\n\nOutput\n3\n\nInput\n7\n0 3 1 0 5 2 6\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.\n\nIn the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer.\n\nIn the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7."} +{"id": "00627", "text": "You are given a string $s$ consisting of $n$ lowercase Latin letters.\n\nYou have to remove at most one (i.e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.\n\nString $s = s_1 s_2 \\dots s_n$ is lexicographically smaller than string $t = t_1 t_2 \\dots t_m$ if $n < m$ and $s_1 = t_1, s_2 = t_2, \\dots, s_n = t_n$ or there exists a number $p$ such that $p \\le min(n, m)$ and $s_1 = t_1, s_2 = t_2, \\dots, s_{p-1} = t_{p-1}$ and $s_p < t_p$.\n\nFor example, \"aaa\" is smaller than \"aaaa\", \"abb\" is smaller than \"abc\", \"pqr\" is smaller than \"z\".\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of $s$.\n\nThe second line of the input contains exactly $n$ lowercase Latin letters \u2014 the string $s$.\n\n\n-----Output-----\n\nPrint one string \u2014 the smallest possible lexicographically string that can be obtained by removing at most one character from the string $s$.\n\n\n-----Examples-----\nInput\n3\naaa\n\nOutput\naa\n\nInput\n5\nabcda\n\nOutput\nabca\n\n\n\n-----Note-----\n\nIn the first example you can remove any character of $s$ to obtain the string \"aa\".\n\nIn the second example \"abca\" < \"abcd\" < \"abcda\" < \"abda\" < \"acda\" < \"bcda\"."} +{"id": "00628", "text": "Mr Keks is a typical white-collar in Byteland.\n\nHe has a bookshelf in his office with some books on it, each book has an integer positive price.\n\nMr Keks defines the value of a shelf as the sum of books prices on it. \n\nMiraculously, Mr Keks was promoted and now he is moving into a new office.\n\nHe learned that in the new office he will have not a single bookshelf, but exactly $k$ bookshelves. He decided that the beauty of the $k$ shelves is the bitwise AND of the values of all the shelves.\n\nHe also decided that he won't spend time on reordering the books, so he will place several first books on the first shelf, several next books on the next shelf and so on. Of course, he will place at least one book on each shelf. This way he will put all his books on $k$ shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\leq k \\leq n \\leq 50$)\u00a0\u2014 the number of books and the number of shelves in the new office.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots a_n$, ($0 < a_i < 2^{50}$)\u00a0\u2014 the prices of the books in the order they stand on the old shelf.\n\n\n-----Output-----\n\nPrint the maximum possible beauty of $k$ shelves in the new office.\n\n\n-----Examples-----\nInput\n10 4\n9 14 28 1 7 13 15 29 2 31\n\nOutput\n24\n\nInput\n7 3\n3 14 15 92 65 35 89\n\nOutput\n64\n\n\n\n-----Note-----\n\nIn the first example you can split the books as follows:\n\n$$(9 + 14 + 28 + 1 + 7) \\& (13 + 15) \\& (29 + 2) \\& (31) = 24.$$\n\nIn the second example you can split the books as follows:\n\n$$(3 + 14 + 15 + 92) \\& (65) \\& (35 + 89) = 64.$$"} +{"id": "00629", "text": "A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.\n\nThe town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.\n\nThe first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.\n\nEach crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.\n\nThe traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to a_{ij} (1 \u2264 i \u2264 2, 1 \u2264 j \u2264 n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals b_{j} (1 \u2264 j \u2264 n). The city doesn't have any other crossings.\n\nThe boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing. [Image] Figure to the first sample. \n\nHelp Laurenty determine the minimum total time he needs to wait at the crossroads.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (2 \u2264 n \u2264 50) \u2014 the number of houses in each row. \n\nEach of the next two lines contains n - 1 space-separated integer \u2014 values a_{ij} (1 \u2264 a_{ij} \u2264 100). \n\nThe last line contains n space-separated integers b_{j} (1 \u2264 b_{j} \u2264 100).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.\n\n\n-----Examples-----\nInput\n4\n1 2 3\n3 2 1\n3 2 2 3\n\nOutput\n12\n\nInput\n3\n1 2\n3 3\n2 1 3\n\nOutput\n11\n\nInput\n2\n1\n1\n1 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nThe first sample is shown on the figure above. \n\nIn the second sample, Laurenty's path can look as follows: Laurenty crosses the avenue, the waiting time is 3; Laurenty uses the second crossing in the first row, the waiting time is 2; Laurenty uses the first crossing in the first row, the waiting time is 1; Laurenty uses the first crossing in the first row, the waiting time is 1; Laurenty crosses the avenue, the waiting time is 1; Laurenty uses the second crossing in the second row, the waiting time is 3. In total we get that the answer equals 11.\n\nIn the last sample Laurenty visits all the crossings, so the answer is 4."} +{"id": "00630", "text": "There are times you recall a good old friend and everything you've come through together. Luckily there are social networks\u00a0\u2014 they store all your message history making it easy to know what you argued over 10 years ago.\n\nMore formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.\n\nEach message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.\n\nDigging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.\n\nDetermine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 10^5, 0 \u2264 k \u2264 n) \u2014 the total amount of messages and the number of previous and next messages visible.\n\nThe second line features a sequence of integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} < i), where a_{i} denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.\n\n\n-----Output-----\n\nPrint n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.\n\n\n-----Examples-----\nInput\n6 0\n0 1 1 2 3 2\n\nOutput\n1 2 2 3 3 3 \n\nInput\n10 1\n0 1 0 3 4 5 2 3 7 0\n\nOutput\n2 3 3 4 5 6 6 6 8 2 \n\nInput\n2 2\n0 1\n\nOutput\n2 2 \n\n\n\n-----Note-----\n\nConsider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.\n\nIn the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6."} +{"id": "00631", "text": "For a given array $a$ consisting of $n$ integers and a given integer $m$ find if it is possible to reorder elements of the array $a$ in such a way that $\\sum_{i=1}^{n}{\\sum_{j=i}^{n}{\\frac{a_j}{j}}}$ equals $m$? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, $\\frac{5}{2}=2.5$.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$\u00a0\u2014 the number of test cases ($1 \\le t \\le 100$). The test cases follow, each in two lines.\n\nThe first line of a test case contains two integers $n$ and $m$ ($1 \\le n \\le 100$, $0 \\le m \\le 10^6$). The second line contains integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 10^6$)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nFor each test case print \"YES\", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and \"NO\" otherwise.\n\n\n-----Example-----\nInput\n2\n3 8\n2 5 1\n4 4\n0 1 2 3\n\nOutput\nYES\nNO\n\n\n\n-----Note-----\n\nIn the first test case one of the reorders could be $[1, 2, 5]$. The sum is equal to $(\\frac{1}{1} + \\frac{2}{2} + \\frac{5}{3}) + (\\frac{2}{2} + \\frac{5}{3}) + (\\frac{5}{3}) = 8$. The brackets denote the inner sum $\\sum_{j=i}^{n}{\\frac{a_j}{j}}$, while the summation of brackets corresponds to the sum over $i$."} +{"id": "00632", "text": "Orac is studying number theory, and he is interested in the properties of divisors.\n\nFor two positive integers $a$ and $b$, $a$ is a divisor of $b$ if and only if there exists an integer $c$, such that $a\\cdot c=b$.\n\nFor $n \\ge 2$, we will denote as $f(n)$ the smallest positive divisor of $n$, except $1$.\n\nFor example, $f(7)=7,f(10)=2,f(35)=5$.\n\nFor the fixed integer $n$, Orac decided to add $f(n)$ to $n$. \n\nFor example, if he had an integer $n=5$, the new value of $n$ will be equal to $10$. And if he had an integer $n=6$, $n$ will be changed to $8$.\n\nOrac loved it so much, so he decided to repeat this operation several times.\n\nNow, for two positive integers $n$ and $k$, Orac asked you to add $f(n)$ to $n$ exactly $k$ times (note that $n$ will change after each operation, so $f(n)$ may change too) and tell him the final value of $n$.\n\nFor example, if Orac gives you $n=5$ and $k=2$, at first you should add $f(5)=5$ to $n=5$, so your new value of $n$ will be equal to $n=10$, after that, you should add $f(10)=2$ to $10$, so your new (and the final!) value of $n$ will be equal to $12$.\n\nOrac may ask you these queries many times.\n\n\n-----Input-----\n\nThe first line of the input is a single integer $t\\ (1\\le t\\le 100)$: the number of times that Orac will ask you.\n\nEach of the next $t$ lines contains two positive integers $n,k\\ (2\\le n\\le 10^6, 1\\le k\\le 10^9)$, corresponding to a query by Orac.\n\nIt is guaranteed that the total sum of $n$ is at most $10^6$. \n\n\n-----Output-----\n\nPrint $t$ lines, the $i$-th of them should contain the final value of $n$ in the $i$-th query by Orac.\n\n\n-----Example-----\nInput\n3\n5 1\n8 2\n3 4\n\nOutput\n10\n12\n12\n\n\n\n-----Note-----\n\nIn the first query, $n=5$ and $k=1$. The divisors of $5$ are $1$ and $5$, the smallest one except $1$ is $5$. Therefore, the only operation adds $f(5)=5$ to $5$, and the result is $10$.\n\nIn the second query, $n=8$ and $k=2$. The divisors of $8$ are $1,2,4,8$, where the smallest one except $1$ is $2$, then after one operation $8$ turns into $8+(f(8)=2)=10$. The divisors of $10$ are $1,2,5,10$, where the smallest one except $1$ is $2$, therefore the answer is $10+(f(10)=2)=12$.\n\nIn the third query, $n$ is changed as follows: $3 \\to 6 \\to 8 \\to 10 \\to 12$."} +{"id": "00633", "text": "Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \\in E$ \u00a0$GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.\n\nConstruct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges.\n\nIf there exists no valid graph with the given number of vertices and edges then output \"Impossible\".\n\nIf there are multiple answers then print any of them.\n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $m$ ($1 \\le n, m \\le 10^5$) \u2014 the number of vertices and the number of edges.\n\n\n-----Output-----\n\nIf there exists no valid graph with the given number of vertices and edges then output \"Impossible\".\n\nOtherwise print the answer in the following format:\n\nThe first line should contain the word \"Possible\".\n\nThe $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \\le v_i, u_i \\le n, v_i \\neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$.\n\nIf there are multiple answers then print any of them.\n\n\n-----Examples-----\nInput\n5 6\n\nOutput\nPossible\n2 5\n3 2\n5 1\n3 4\n4 1\n5 4\n\nInput\n6 12\n\nOutput\nImpossible\n\n\n\n-----Note-----\n\nHere is the representation of the graph from the first example: [Image]"} +{"id": "00634", "text": "In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land.\n\nThe river area can be represented by a grid with r rows and exactly two columns \u2014 each cell represents a rectangular area. The rows are numbered 1 through r from top to bottom, while the columns are numbered 1 and 2.\n\nInitially, all of the cells are occupied by the river. The plan is to turn some of those cells into land one by one, with the cities alternately choosing a cell to reclaim, and continuing until no more cells can be reclaimed.\n\nHowever, the river is also used as a major trade route. The cities need to make sure that ships will still be able to sail from one end of the river to the other. More formally, if a cell (r, c) has been reclaimed, it is not allowed to reclaim any of the cells (r - 1, 3 - c), (r, 3 - c), or (r + 1, 3 - c).\n\nThe cities are not on friendly terms, and each city wants to be the last city to reclaim a cell (they don't care about how many cells they reclaim, just who reclaims a cell last). The cities have already reclaimed n cells. Your job is to determine which city will be the last to reclaim a cell, assuming both choose cells optimally from current moment.\n\n\n-----Input-----\n\nThe first line consists of two integers r and n (1 \u2264 r \u2264 100, 0 \u2264 n \u2264 r). Then n lines follow, describing the cells that were already reclaimed. Each line consists of two integers: r_{i} and c_{i} (1 \u2264 r_{i} \u2264 r, 1 \u2264 c_{i} \u2264 2), which represent the cell located at row r_{i} and column c_{i}. All of the lines describing the cells will be distinct, and the reclaimed cells will not violate the constraints above.\n\n\n-----Output-----\n\nOutput \"WIN\" if the city whose turn it is to choose a cell can guarantee that they will be the last to choose a cell. Otherwise print \"LOSE\".\n\n\n-----Examples-----\nInput\n3 1\n1 1\n\nOutput\nWIN\n\nInput\n12 2\n4 1\n8 1\n\nOutput\nWIN\n\nInput\n1 1\n1 2\n\nOutput\nLOSE\n\n\n\n-----Note-----\n\nIn the first example, there are 3 possible cells for the first city to reclaim: (2, 1), (3, 1), or (3, 2). The first two possibilities both lose, as they leave exactly one cell for the other city. [Image] \n\nHowever, reclaiming the cell at (3, 2) leaves no more cells that can be reclaimed, and therefore the first city wins. $\\text{-}$ \n\nIn the third example, there are no cells that can be reclaimed."} +{"id": "00635", "text": "Alice has a birthday today, so she invited home her best friend Bob. Now Bob needs to find a way to commute to the Alice's home.\n\nIn the city in which Alice and Bob live, the first metro line is being built. This metro line contains $n$ stations numbered from $1$ to $n$. Bob lives near the station with number $1$, while Alice lives near the station with number $s$. The metro line has two tracks. Trains on the first track go from the station $1$ to the station $n$ and trains on the second track go in reverse direction. Just after the train arrives to the end of its track, it goes to the depot immediately, so it is impossible to travel on it after that.\n\nSome stations are not yet open at all and some are only partially open\u00a0\u2014 for each station and for each track it is known whether the station is closed for that track or not. If a station is closed for some track, all trains going in this track's direction pass the station without stopping on it.\n\nWhen the Bob got the information on opened and closed stations, he found that traveling by metro may be unexpectedly complicated. Help Bob determine whether he can travel to the Alice's home by metro or he should search for some other transport.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $s$ ($2 \\le s \\le n \\le 1000$)\u00a0\u2014 the number of stations in the metro and the number of the station where Alice's home is located. Bob lives at station $1$.\n\nNext lines describe information about closed and open stations.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($a_i = 0$ or $a_i = 1$). If $a_i = 1$, then the $i$-th station is open on the first track (that is, in the direction of increasing station numbers). Otherwise the station is closed on the first track.\n\nThe third line contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($b_i = 0$ or $b_i = 1$). If $b_i = 1$, then the $i$-th station is open on the second track (that is, in the direction of decreasing station numbers). Otherwise the station is closed on the second track.\n\n\n-----Output-----\n\nPrint \"YES\" (quotes for clarity) if Bob will be able to commute to the Alice's home by metro and \"NO\" (quotes for clarity) otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n5 3\n1 1 1 1 1\n1 1 1 1 1\n\nOutput\nYES\n\nInput\n5 4\n1 0 0 0 1\n0 1 1 1 1\n\nOutput\nYES\n\nInput\n5 2\n0 1 1 1 1\n1 1 1 1 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example, all stations are opened, so Bob can simply travel to the station with number $3$.\n\nIn the second example, Bob should travel to the station $5$ first, switch to the second track and travel to the station $4$ then.\n\nIn the third example, Bob simply can't enter the train going in the direction of Alice's home."} +{"id": "00636", "text": "Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea.\n\nAmr has n instruments, it takes a_{i} days to learn i-th instrument. Being busy, Amr dedicated k days to learn how to play the maximum possible number of instruments.\n\nAmr asked for your help to distribute his free days between instruments so that he can achieve his goal.\n\n\n-----Input-----\n\nThe first line contains two numbers n, k (1 \u2264 n \u2264 100, 0 \u2264 k \u2264 10 000), the number of instruments and number of days respectively.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 100), representing number of days required to learn the i-th instrument.\n\n\n-----Output-----\n\nIn the first line output one integer m representing the maximum number of instruments Amr can learn.\n\nIn the second line output m space-separated integers: the indices of instruments to be learnt. You may output indices in any order.\n\nif there are multiple optimal solutions output any. It is not necessary to use all days for studying.\n\n\n-----Examples-----\nInput\n4 10\n4 3 1 2\n\nOutput\n4\n1 2 3 4\nInput\n5 6\n4 3 1 1 2\n\nOutput\n3\n1 3 4\nInput\n1 3\n4\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test Amr can learn all 4 instruments.\n\nIn the second test other possible solutions are: {2, 3, 5} or {3, 4, 5}.\n\nIn the third test Amr doesn't have enough time to learn the only presented instrument."} +{"id": "00637", "text": "A camera you have accidentally left in a desert has taken an interesting photo. The photo has a resolution of n pixels width, and each column of this photo is all white or all black. Thus, we can represent the photo as a sequence of n zeros and ones, where 0 means that the corresponding column is all white, and 1 means that the corresponding column is black.\n\nYou think that this photo can contain a zebra. In this case the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width. For example, the photo [0, 0, 0, 1, 1, 1, 0, 0, 0] can be a photo of zebra, while the photo [0, 0, 0, 1, 1, 1, 1] can not, because the width of the black stripe is 3, while the width of the white stripe is 4. Can the given photo be a photo of zebra or not?\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100 000) \u2014 the width of the photo.\n\nThe second line contains a sequence of integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 1) \u2014 the description of the photo. If a_{i} is zero, the i-th column is all black. If a_{i} is one, then the i-th column is all white.\n\n\n-----Output-----\n\nIf the photo can be a photo of zebra, print \"YES\" (without quotes). Otherwise, print \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n9\n0 0 0 1 1 1 0 0 0\n\nOutput\nYES\n\nInput\n7\n0 0 0 1 1 1 1\n\nOutput\nNO\n\nInput\n5\n1 1 1 1 1\n\nOutput\nYES\n\nInput\n8\n1 1 1 0 0 0 1 1\n\nOutput\nNO\n\nInput\n9\n1 1 0 1 1 0 1 1 0\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe first two examples are described in the statements.\n\nIn the third example all pixels are white, so the photo can be a photo of zebra.\n\nIn the fourth example the width of the first stripe is equal to three (white color), the width of the second stripe is equal to three (black), and the width of the third stripe is equal to two (white). Thus, not all stripes have equal length, so this photo is not a photo of zebra."} +{"id": "00638", "text": "The only difference between easy and hard versions is constraints.\n\nA session has begun at Beland State University. Many students are taking exams.\n\nPolygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. \n\nStudents take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student.\n\nThe duration of the whole exam for all students is $M$ minutes ($\\max t_i \\le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam.\n\nFor each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam.\n\nFor each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $M$ ($1 \\le n \\le 100$, $1 \\le M \\le 100$)\u00a0\u2014 the number of students and the total duration of the exam in minutes, respectively.\n\nThe second line of the input contains $n$ integers $t_i$ ($1 \\le t_i \\le 100$)\u00a0\u2014 time in minutes that $i$-th student spends to answer to a ticket.\n\nIt's guaranteed that all values of $t_i$ are not greater than $M$.\n\n\n-----Output-----\n\nPrint $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam.\n\n\n-----Examples-----\nInput\n7 15\n1 2 3 4 5 6 7\n\nOutput\n0 0 0 0 0 2 3 \nInput\n5 100\n80 40 40 40 60\n\nOutput\n0 1 1 2 3 \n\n\n-----Note-----\n\nThe explanation for the example 1.\n\nPlease note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$.\n\nIn order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$).\n\nIn order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$)."} +{"id": "00639", "text": "Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.\n\nDr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .\n\nDr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?\n\n\n-----Input-----\n\nThe first line contains two integers n and x (1 \u2264 n \u2264 100, 0 \u2264 x \u2264 100)\u00a0\u2014 the size of the set Dr. Evil owns, and the desired MEX.\n\nThe second line contains n distinct non-negative integers not exceeding 100 that represent the set.\n\n\n-----Output-----\n\nThe only line should contain one integer\u00a0\u2014 the minimal number of operations Dr. Evil should perform.\n\n\n-----Examples-----\nInput\n5 3\n0 4 5 6 7\n\nOutput\n2\n\nInput\n1 0\n0\n\nOutput\n1\n\nInput\n5 0\n1 2 3 4 5\n\nOutput\n0\n\n\n\n-----Note-----\n\nFor the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.\n\nFor the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.\n\nIn the third test case the set is already evil."} +{"id": "00640", "text": "Two players are playing a game. First each of them writes an integer from 1 to 6, and then a dice is thrown. The player whose written number got closer to the number on the dice wins. If both payers have the same difference, it's a draw.\n\nThe first player wrote number a, the second player wrote number b. How many ways to throw a dice are there, at which the first player wins, or there is a draw, or the second player wins?\n\n\n-----Input-----\n\nThe single line contains two integers a and b (1 \u2264 a, b \u2264 6)\u00a0\u2014 the numbers written on the paper by the first and second player, correspondingly.\n\n\n-----Output-----\n\nPrint three integers: the number of ways to throw the dice at which the first player wins, the game ends with a draw or the second player wins, correspondingly.\n\n\n-----Examples-----\nInput\n2 5\n\nOutput\n3 0 3\n\nInput\n2 4\n\nOutput\n2 1 3\n\n\n\n-----Note-----\n\nThe dice is a standard cube-shaped six-sided object with each side containing a number from 1 to 6, and where all numbers on all sides are distinct.\n\nYou can assume that number a is closer to number x than number b, if |a - x| < |b - x|."} +{"id": "00641", "text": "Today is Wednesday, the third day of the week. What's more interesting is that tomorrow is the last day of the year 2015.\n\nLimak is a little polar bear. He enjoyed this year a lot. Now, he is so eager to the coming year 2016.\n\nLimak wants to prove how responsible a bear he is. He is going to regularly save candies for the entire year 2016! He considers various saving plans. He can save one candy either on some fixed day of the week or on some fixed day of the month.\n\nLimak chose one particular plan. He isn't sure how many candies he will save in the 2016 with his plan. Please, calculate it and tell him.\n\n\n-----Input-----\n\nThe only line of the input is in one of the following two formats: \"x of week\" where x (1 \u2264 x \u2264 7) denotes the day of the week. The 1-st day is Monday and the 7-th one is Sunday. \"x of month\" where x (1 \u2264 x \u2264 31) denotes the day of the month. \n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of candies Limak will save in the year 2016.\n\n\n-----Examples-----\nInput\n4 of week\n\nOutput\n52\n\nInput\n30 of month\n\nOutput\n11\n\n\n\n-----Note-----\n\nPolar bears use the Gregorian calendar. It is the most common calendar and you likely use it too. You can read about it on Wikipedia if you want to \u2013 https://en.wikipedia.org/wiki/Gregorian_calendar. The week starts with Monday.\n\nIn the first sample Limak wants to save one candy on each Thursday (the 4-th day of the week). There are 52 Thursdays in the 2016. Thus, he will save 52 candies in total.\n\nIn the second sample Limak wants to save one candy on the 30-th day of each month. There is the 30-th day in exactly 11 months in the 2016\u00a0\u2014 all months but February. It means that Limak will save 11 candies in total."} +{"id": "00642", "text": "Little boy Petya loves stairs very much. But he is bored from simple going up and down them \u2014 he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.\n\nNow Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.\n\nOne has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 10^9, 0 \u2264 m \u2264 3000) \u2014 the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d_1, d_2, ..., d_{m} (1 \u2264 d_{i} \u2264 n) \u2014 the numbers of the dirty stairs (in an arbitrary order).\n\n\n-----Output-----\n\nPrint \"YES\" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print \"NO\".\n\n\n-----Examples-----\nInput\n10 5\n2 4 8 3 6\n\nOutput\nNO\nInput\n10 5\n2 4 5 7 9\n\nOutput\nYES"} +{"id": "00643", "text": "You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x have been successful. Thus, your current success rate on Codeforces is equal to x / y.\n\nYour favorite rational number in the [0;1] range is p / q. Now you wonder: what is the smallest number of submissions you have to make if you want your success rate to be p / q?\n\n\n-----Input-----\n\nThe first line contains a single integer t (1 \u2264 t \u2264 1000)\u00a0\u2014 the number of test cases.\n\nEach of the next t lines contains four integers x, y, p and q (0 \u2264 x \u2264 y \u2264 10^9; 0 \u2264 p \u2264 q \u2264 10^9; y > 0; q > 0).\n\nIt is guaranteed that p / q is an irreducible fraction.\n\nHacks. For hacks, an additional constraint of t \u2264 5 must be met.\n\n\n-----Output-----\n\nFor each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve.\n\n\n-----Example-----\nInput\n4\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1\n\nOutput\n4\n10\n0\n-1\n\n\n\n-----Note-----\n\nIn the first example, you have to make 4 successful submissions. Your success rate will be equal to 7 / 14, or 1 / 2.\n\nIn the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9 / 24, or 3 / 8.\n\nIn the third example, there is no need to make any new submissions. Your success rate is already equal to 20 / 70, or 2 / 7.\n\nIn the fourth example, the only unsuccessful submission breaks your hopes of having the success rate equal to 1."} +{"id": "00644", "text": "You are given a function $f$ written in some basic language. The function accepts an integer value, which is immediately written into some variable $x$. $x$ is an integer variable and can be assigned values from $0$ to $2^{32}-1$. The function contains three types of commands:\n\n for $n$ \u2014 for loop; end \u2014 every command between \"for $n$\" and corresponding \"end\" is executed $n$ times; add \u2014 adds 1 to $x$. \n\nAfter the execution of these commands, value of $x$ is returned.\n\nEvery \"for $n$\" is matched with \"end\", thus the function is guaranteed to be valid. \"for $n$\" can be immediately followed by \"end\".\"add\" command can be outside of any for loops.\n\nNotice that \"add\" commands might overflow the value of $x$! It means that the value of $x$ becomes greater than $2^{32}-1$ after some \"add\" command. \n\nNow you run $f(0)$ and wonder if the resulting value of $x$ is correct or some overflow made it incorrect.\n\nIf overflow happened then output \"OVERFLOW!!!\", otherwise print the resulting value of $x$.\n\n\n-----Input-----\n\nThe first line contains a single integer $l$ ($1 \\le l \\le 10^5$) \u2014 the number of lines in the function.\n\nEach of the next $l$ lines contains a single command of one of three types:\n\n for $n$ ($1 \\le n \\le 100$) \u2014 for loop; end \u2014 every command between \"for $n$\" and corresponding \"end\" is executed $n$ times; add \u2014 adds 1 to $x$. \n\n\n-----Output-----\n\nIf overflow happened during execution of $f(0)$, then output \"OVERFLOW!!!\", otherwise print the resulting value of $x$.\n\n\n-----Examples-----\nInput\n9\nadd\nfor 43\nend\nfor 10\nfor 15\nadd\nend\nadd\nend\n\nOutput\n161\n\nInput\n2\nfor 62\nend\n\nOutput\n0\n\nInput\n11\nfor 100\nfor 100\nfor 100\nfor 100\nfor 100\nadd\nend\nend\nend\nend\nend\n\nOutput\nOVERFLOW!!!\n\n\n\n-----Note-----\n\nIn the first example the first \"add\" is executed 1 time, the second \"add\" is executed 150 times and the last \"add\" is executed 10 times. Note that \"for $n$\" can be immediately followed by \"end\" and that \"add\" can be outside of any for loops.\n\nIn the second example there are no commands \"add\", thus the returning value is 0.\n\nIn the third example \"add\" command is executed too many times, which causes $x$ to go over $2^{32}-1$."} +{"id": "00645", "text": "Your friend has n cards.\n\nYou know that each card has a lowercase English letter on one side and a digit on the other.\n\nCurrently, your friend has laid out the cards on a table so only one side of each card is visible.\n\nYou would like to know if the following statement is true for cards that your friend owns: \"If a card has a vowel on one side, then it has an even digit on the other side.\" More specifically, a vowel is one of 'a', 'e', 'i', 'o' or 'u', and even digit is one of '0', '2', '4', '6' or '8'.\n\nFor example, if a card has 'a' on one side, and '6' on the other side, then this statement is true for it. Also, the statement is true, for example, for a card with 'b' and '4', and for a card with 'b' and '3' (since the letter is not a vowel). The statement is false, for example, for card with 'e' and '5'. You are interested if the statement is true for all cards. In particular, if no card has a vowel, the statement is true.\n\nTo determine this, you can flip over some cards to reveal the other side. You would like to know what is the minimum number of cards you need to flip in the worst case in order to verify that the statement is true.\n\n\n-----Input-----\n\nThe first and only line of input will contain a string s (1 \u2264 |s| \u2264 50), denoting the sides of the cards that you can see on the table currently. Each character of s is either a lowercase English letter or a digit.\n\n\n-----Output-----\n\nPrint a single integer, the minimum number of cards you must turn over to verify your claim.\n\n\n-----Examples-----\nInput\nee\n\nOutput\n2\n\nInput\nz\n\nOutput\n0\n\nInput\n0ay1\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample, we must turn over both cards. Note that even though both cards have the same letter, they could possibly have different numbers on the other side.\n\nIn the second sample, we don't need to turn over any cards. The statement is vacuously true, since you know your friend has no cards with a vowel on them.\n\nIn the third sample, we need to flip the second and fourth cards."} +{"id": "00646", "text": "There are $n$ detachments on the surface, numbered from $1$ to $n$, the $i$-th detachment is placed in a point with coordinates $(x_i, y_i)$. All detachments are placed in different points.\n\nBrimstone should visit each detachment at least once. You can choose the detachment where Brimstone starts.\n\nTo move from one detachment to another he should first choose one of four directions of movement (up, right, left or down) and then start moving with the constant speed of one unit interval in a second until he comes to a detachment. After he reaches an arbitrary detachment, he can repeat the same process.\n\nEach $t$ seconds an orbital strike covers the whole surface, so at that moment Brimstone should be in a point where some detachment is located. He can stay with any detachment as long as needed.\n\nBrimstone is a good commander, that's why he can create at most one detachment and place it in any empty point with integer coordinates he wants before his trip. Keep in mind that Brimstone will need to visit this detachment, too.\n\nHelp Brimstone and find such minimal $t$ that it is possible to check each detachment. If there is no such $t$ report about it.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ $(2 \\le n \\le 1000)$\u00a0\u2014 the number of detachments.\n\nIn each of the next $n$ lines there is a pair of integers $x_i$, $y_i$ $(|x_i|, |y_i| \\le 10^9)$\u00a0\u2014 the coordinates of $i$-th detachment.\n\nIt is guaranteed that all points are different.\n\n\n-----Output-----\n\nOutput such minimal integer $t$ that it is possible to check all the detachments adding at most one new detachment.\n\nIf there is no such $t$, print $-1$.\n\n\n-----Examples-----\nInput\n4\n100 0\n0 100\n-100 0\n0 -100\n\nOutput\n100\nInput\n7\n0 2\n1 0\n-3 0\n0 -2\n-1 -1\n-1 -3\n-2 -3\n\nOutput\n-1\nInput\n5\n0 0\n0 -1\n3 0\n-2 0\n-2 1\n\nOutput\n2\nInput\n5\n0 0\n2 0\n0 -1\n-2 0\n-2 1\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first test it is possible to place a detachment in $(0, 0)$, so that it is possible to check all the detachments for $t = 100$. It can be proven that it is impossible to check all detachments for $t < 100$; thus the answer is $100$.\n\nIn the second test, there is no such $t$ that it is possible to check all detachments, even with adding at most one new detachment, so the answer is $-1$.\n\nIn the third test, it is possible to place a detachment in $(1, 0)$, so that Brimstone can check all the detachments for $t = 2$. It can be proven that it is the minimal such $t$.\n\nIn the fourth test, there is no need to add any detachments, because the answer will not get better ($t = 2$). It can be proven that it is the minimal such $t$."} +{"id": "00647", "text": "One fine October day a mathematics teacher Vasily Petrov went to a class and saw there n pupils who sat at the $\\frac{n}{2}$ desks, two people at each desk. Vasily quickly realized that number n is even. Like all true mathematicians, Vasily has all students numbered from 1 to n.\n\nBut Vasily Petrov did not like the way the children were seated at the desks. According to him, the students whose numbers differ by 1, can not sit together, as they talk to each other all the time, distract others and misbehave.\n\nOn the other hand, if a righthanded student sits at the left end of the desk and a lefthanded student sits at the right end of the desk, they hit elbows all the time and distract each other. In other cases, the students who sit at the same desk, do not interfere with each other.\n\nVasily knows very well which students are lefthanders and which ones are righthanders, and he asks you to come up with any order that meets these two uncomplicated conditions (students do not talk to each other and do not bump their elbows). It is guaranteed that the input is such that at least one way to seat the students always exists.\n\n\n-----Input-----\n\nThe first input line contains a single even integer n (4 \u2264 n \u2264 100) \u2014 the number of students in the class. The second line contains exactly n capital English letters \"L\" and \"R\". If the i-th letter at the second line equals \"L\", then the student number i is a lefthander, otherwise he is a righthander.\n\n\n-----Output-----\n\nPrint $\\frac{n}{2}$ integer pairs, one pair per line. In the i-th line print the numbers of students that will sit at the i-th desk. The first number in the pair stands for the student who is sitting to the left, and the second number stands for the student who is sitting to the right. Separate the numbers in the pairs by spaces. If there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n6\nLLRLLL\n\nOutput\n1 4\n2 5\n6 3\n\nInput\n4\nRRLL\n\nOutput\n3 1\n4 2"} +{"id": "00648", "text": "Okabe needs bananas for one of his experiments for some strange reason. So he decides to go to the forest and cut banana trees.\n\nConsider the point (x, y) in the 2D plane such that x and y are integers and 0 \u2264 x, y. There is a tree in such a point, and it has x + y bananas. There are no trees nor bananas in other points. Now, Okabe draws a line with equation $y = - \\frac{x}{m} + b$. Okabe can select a single rectangle with axis aligned sides with all points on or under the line and cut all the trees in all points that are inside or on the border of this rectangle and take their bananas. Okabe's rectangle can be degenerate; that is, it can be a line segment or even a point.\n\nHelp Okabe and find the maximum number of bananas he can get if he chooses the rectangle wisely.\n\nOkabe is sure that the answer does not exceed 10^18. You can trust him.\n\n\n-----Input-----\n\nThe first line of input contains two space-separated integers m and b (1 \u2264 m \u2264 1000, 1 \u2264 b \u2264 10000).\n\n\n-----Output-----\n\nPrint the maximum number of bananas Okabe can get from the trees he cuts.\n\n\n-----Examples-----\nInput\n1 5\n\nOutput\n30\n\nInput\n2 3\n\nOutput\n25\n\n\n\n-----Note----- [Image] \n\nThe graph above corresponds to sample test 1. The optimal rectangle is shown in red and has 30 bananas."} +{"id": "00649", "text": "One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on.\n\nLimak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t_1, t_2, ..., t_{n}. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t_1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t_2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2^{n} - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time\u00a0\u2014 it is allowed and such parts do not crash.\n\nBefore launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells?\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 30)\u00a0\u2014 the total depth of the recursion.\n\nThe second line contains n integers t_1, t_2, ..., t_{n} (1 \u2264 t_{i} \u2264 5). On the i-th level each of 2^{i} - 1 parts will cover t_{i} cells before exploding.\n\n\n-----Output-----\n\nPrint one integer, denoting the number of cells which will be visited at least once by any part of the firework.\n\n\n-----Examples-----\nInput\n4\n4 2 2 3\n\nOutput\n39\n\nInput\n6\n1 1 1 1 1 3\n\nOutput\n85\n\nInput\n1\n3\n\nOutput\n3\n\n\n\n-----Note-----\n\nFor the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t_1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39.\n\n [Image] \n\nFor the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level.\n\n [Image]"} +{"id": "00650", "text": "-----Input-----\n\nThe input consists of a single string of uppercase letters A-Z. The length of the string is between 1 and 10 characters, inclusive.\n\n\n-----Output-----\n\nOutput \"YES\" or \"NO\".\n\n\n-----Examples-----\nInput\nNEAT\n\nOutput\nYES\n\nInput\nWORD\n\nOutput\nNO\n\nInput\nCODER\n\nOutput\nNO\n\nInput\nAPRILFOOL\n\nOutput\nNO\n\nInput\nAI\n\nOutput\nYES\n\nInput\nJUROR\n\nOutput\nYES\n\nInput\nYES\n\nOutput\nNO"} +{"id": "00651", "text": "Bob programmed a robot to navigate through a 2d maze.\n\nThe maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'.\n\nThere is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it.\n\nThe robot can only move up, left, right, or down.\n\nWhen Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits.\n\nThe robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions.\n\nBob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.\n\n\n-----Input-----\n\nThe first line of input will contain two integers n and m (2 \u2264 n, m \u2264 50), denoting the dimensions of the maze.\n\nThe next n lines will contain exactly m characters each, denoting the maze.\n\nEach character of the maze will be '.', '#', 'S', or 'E'.\n\nThere will be exactly one 'S' and exactly one 'E' in the maze.\n\nThe last line will contain a single string s (1 \u2264 |s| \u2264 100)\u00a0\u2014 the instructions given to the robot. Each character of s is a digit from 0 to 3.\n\n\n-----Output-----\n\nPrint a single integer, the number of mappings of digits to directions that will lead the robot to the exit.\n\n\n-----Examples-----\nInput\n5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012\n\nOutput\n1\n\nInput\n6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021\n\nOutput\n14\n\nInput\n5 3\n...\n.S.\n###\n.E.\n...\n3\n\nOutput\n0\n\n\n\n-----Note-----\n\nFor the first sample, the only valid mapping is $0 \\rightarrow D, 1 \\rightarrow L, 2 \\rightarrow U, 3 \\rightarrow R$, where D is down, L is left, U is up, R is right."} +{"id": "00652", "text": "You are given n points on a plane. All the points are distinct and no three of them lie on the same line. Find the number of parallelograms with the vertices at the given points.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 2000) \u2014 the number of points.\n\nEach of the next n lines contains two integers (x_{i}, y_{i}) (0 \u2264 x_{i}, y_{i} \u2264 10^9) \u2014 the coordinates of the i-th point.\n\n\n-----Output-----\n\nPrint the only integer c \u2014 the number of parallelograms with the vertices at the given points.\n\n\n-----Example-----\nInput\n4\n0 1\n1 0\n1 1\n2 0\n\nOutput\n1"} +{"id": "00653", "text": "Amugae has a hotel consisting of $10$ rooms. The rooms are numbered from $0$ to $9$ from left to right.\n\nThe hotel has two entrances \u2014 one from the left end, and another from the right end. When a customer arrives to the hotel through the left entrance, they are assigned to an empty room closest to the left entrance. Similarly, when a customer arrives at the hotel through the right entrance, they are assigned to an empty room closest to the right entrance.\n\nOne day, Amugae lost the room assignment list. Thankfully Amugae's memory is perfect, and he remembers all of the customers: when a customer arrived, from which entrance, and when they left the hotel. Initially the hotel was empty. Write a program that recovers the room assignment list from Amugae's memory.\n\n\n-----Input-----\n\nThe first line consists of an integer $n$ ($1 \\le n \\le 10^5$), the number of events in Amugae's memory.\n\nThe second line consists of a string of length $n$ describing the events in chronological order. Each character represents: 'L': A customer arrives from the left entrance. 'R': A customer arrives from the right entrance. '0', '1', ..., '9': The customer in room $x$ ($0$, $1$, ..., $9$ respectively) leaves. \n\nIt is guaranteed that there is at least one empty room when a customer arrives, and there is a customer in the room $x$ when $x$ ($0$, $1$, ..., $9$) is given. Also, all the rooms are initially empty.\n\n\n-----Output-----\n\nIn the only line, output the hotel room's assignment status, from room $0$ to room $9$. Represent an empty room as '0', and an occupied room as '1', without spaces.\n\n\n-----Examples-----\nInput\n8\nLLRL1RL1\n\nOutput\n1010000011\nInput\n9\nL0L0LLRR9\n\nOutput\n1100000010\n\n\n-----Note-----\n\nIn the first example, hotel room's assignment status after each action is as follows. First of all, all rooms are empty. Assignment status is 0000000000. L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000. L: one more customer from the left entrance. Assignment status is 1100000000. R: one more customer from the right entrance. Assignment status is 1100000001. L: one more customer from the left entrance. Assignment status is 1110000001. 1: the customer in room $1$ leaves. Assignment status is 1010000001. R: one more customer from the right entrance. Assignment status is 1010000011. L: one more customer from the left entrance. Assignment status is 1110000011. 1: the customer in room $1$ leaves. Assignment status is 1010000011. \n\nSo after all, hotel room's final assignment status is 1010000011.\n\nIn the second example, hotel room's assignment status after each action is as follows. L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000. 0: the customer in room $0$ leaves. Assignment status is 0000000000. L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000 again. 0: the customer in room $0$ leaves. Assignment status is 0000000000. L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000. L: one more customer from the left entrance. Assignment status is 1100000000. R: one more customer from the right entrance. Assignment status is 1100000001. R: one more customer from the right entrance. Assignment status is 1100000011. 9: the customer in room $9$ leaves. Assignment status is 1100000010. \n\nSo after all, hotel room's final assignment status is 1100000010."} +{"id": "00654", "text": "Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...\n\nIt took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a trie of all correct bracket sequences of length $2n$.\n\nThe definition of correct bracket sequence is as follows: The empty sequence is a correct bracket sequence, If $s$ is a correct bracket sequence, then $(\\,s\\,)$ is a correct bracket sequence, If $s$ and $t$ are a correct bracket sequence, then $st$ is also a correct bracket sequence. \n\nFor example, the strings \"(())\", \"()()\" form a correct bracket sequence, while \")(\" and \"((\" not.\n\nAki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo $10^9 + 7$.\n\n\n-----Input-----\n\nThe only line contains a single integer $n$ ($1 \\le n \\le 1000$).\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo $10^9 + 7$.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n2\n\nOutput\n3\n\nInput\n3\n\nOutput\n9\n\n\n\n-----Note-----\n\nThe pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue. [Image]\u00a0[Image]"} +{"id": "00655", "text": "On a chessboard with a width of $n$ and a height of $n$, rows are numbered from bottom to top from $1$ to $n$, columns are numbered from left to right from $1$ to $n$. Therefore, for each cell of the chessboard, you can assign the coordinates $(r,c)$, where $r$ is the number of the row, and $c$ is the number of the column.\n\nThe white king has been sitting in a cell with $(1,1)$ coordinates for a thousand years, while the black king has been sitting in a cell with $(n,n)$ coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates $(x,y)$...\n\nEach of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules:\n\nAs in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time.\n\nThe player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates $(x,y)$ first will win.\n\nLet's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the $(a,b)$ cell, then in one move he can move from $(a,b)$ to the cells $(a + 1,b)$, $(a - 1,b)$, $(a,b + 1)$, $(a,b - 1)$, $(a + 1,b - 1)$, $(a + 1,b + 1)$, $(a - 1,b - 1)$, or $(a - 1,b + 1)$. Going outside of the field is prohibited.\n\nDetermine the color of the king, who will reach the cell with the coordinates $(x,y)$ first, if the white king moves first.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 10^{18}$)\u00a0\u2014 the length of the side of the chess field.\n\nThe second line contains two integers $x$ and $y$ ($1 \\le x,y \\le n$)\u00a0\u2014 coordinates of the cell, where the coin fell.\n\n\n-----Output-----\n\nIn a single line print the answer \"White\" (without quotes), if the white king will win, or \"Black\" (without quotes), if the black king will win.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n4\n2 3\n\nOutput\nWhite\nInput\n5\n3 5\n\nOutput\nBlack\nInput\n2\n2 2\n\nOutput\nBlack\n\n\n-----Note-----\n\nAn example of the race from the first sample where both the white king and the black king move optimally: The white king moves from the cell $(1,1)$ into the cell $(2,2)$. The black king moves form the cell $(4,4)$ into the cell $(3,3)$. The white king moves from the cell $(2,2)$ into the cell $(2,3)$. This is cell containing the coin, so the white king wins. [Image] \n\nAn example of the race from the second sample where both the white king and the black king move optimally: The white king moves from the cell $(1,1)$ into the cell $(2,2)$. The black king moves form the cell $(5,5)$ into the cell $(4,4)$. The white king moves from the cell $(2,2)$ into the cell $(3,3)$. The black king moves from the cell $(4,4)$ into the cell $(3,5)$. This is the cell, where the coin fell, so the black king wins. [Image] \n\nIn the third example, the coin fell in the starting cell of the black king, so the black king immediately wins. [Image]"} +{"id": "00656", "text": "The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. \n\nVasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days.\n\nBefore the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. \n\nVasya can change summer tires to winter tires and vice versa at the beginning of any day.\n\nFind the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.\n\n\n-----Input-----\n\nThe first line contains two positive integers n and k (1 \u2264 n \u2264 2\u00b710^5, 0 \u2264 k \u2264 n)\u00a0\u2014 the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total.\n\nThe second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 \u2264 t_{i} \u2264 20)\u00a0\u2014 the average air temperature in the i-th winter day. \n\n\n-----Output-----\n\nPrint the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.\n\n\n-----Examples-----\nInput\n4 3\n-5 20 -3 0\n\nOutput\n2\n\nInput\n4 2\n-5 20 -3 0\n\nOutput\n4\n\nInput\n10 6\n2 -5 1 3 0 0 -4 -3 1 0\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. \n\nIn the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four."} +{"id": "00657", "text": "During the winter holidays, the demand for Christmas balls is exceptionally high. Since it's already 2018, the advances in alchemy allow easy and efficient ball creation by utilizing magic crystals.\n\nGrisha needs to obtain some yellow, green and blue balls. It's known that to produce a yellow ball one needs two yellow crystals, green\u00a0\u2014 one yellow and one blue, and for a blue ball, three blue crystals are enough.\n\nRight now there are A yellow and B blue crystals in Grisha's disposal. Find out how many additional crystals he should acquire in order to produce the required number of balls.\n\n\n-----Input-----\n\nThe first line features two integers A and B (0 \u2264 A, B \u2264 10^9), denoting the number of yellow and blue crystals respectively at Grisha's disposal.\n\nThe next line contains three integers x, y and z (0 \u2264 x, y, z \u2264 10^9)\u00a0\u2014 the respective amounts of yellow, green and blue balls to be obtained.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of crystals that Grisha should acquire in addition.\n\n\n-----Examples-----\nInput\n4 3\n2 1 1\n\nOutput\n2\n\nInput\n3 9\n1 1 3\n\nOutput\n1\n\nInput\n12345678 87654321\n43043751 1000000000 53798715\n\nOutput\n2147483648\n\n\n\n-----Note-----\n\nIn the first sample case, Grisha needs five yellow and four blue crystals to create two yellow balls, one green ball, and one blue ball. To do that, Grisha needs to obtain two additional crystals: one yellow and one blue."} +{"id": "00658", "text": "And while Mishka is enjoying her trip...\n\nChris is a little brown bear. No one knows, where and when he met Mishka, but for a long time they are together (excluding her current trip). However, best friends are important too. John is Chris' best friend.\n\nOnce walking with his friend, John gave Chris the following problem:\n\nAt the infinite horizontal road of width w, bounded by lines y = 0 and y = w, there is a bus moving, presented as a convex polygon of n vertices. The bus moves continuously with a constant speed of v in a straight Ox line in direction of decreasing x coordinates, thus in time only x coordinates of its points are changing. Formally, after time t each of x coordinates of its points will be decreased by vt.\n\nThere is a pedestrian in the point (0, 0), who can move only by a vertical pedestrian crossing, presented as a segment connecting points (0, 0) and (0, w) with any speed not exceeding u. Thus the pedestrian can move only in a straight line Oy in any direction with any speed not exceeding u and not leaving the road borders. The pedestrian can instantly change his speed, thus, for example, he can stop instantly.\n\nPlease look at the sample note picture for better understanding.\n\nWe consider the pedestrian is hit by the bus, if at any moment the point he is located in lies strictly inside the bus polygon (this means that if the point lies on the polygon vertex or on its edge, the pedestrian is not hit by the bus).\n\nYou are given the bus position at the moment 0. Please help Chris determine minimum amount of time the pedestrian needs to cross the road and reach the point (0, w) and not to be hit by the bus.\n\n\n-----Input-----\n\nThe first line of the input contains four integers n, w, v, u (3 \u2264 n \u2264 10 000, 1 \u2264 w \u2264 10^9, 1 \u2264 v, u \u2264 1000)\u00a0\u2014 the number of the bus polygon vertices, road width, bus speed and pedestrian speed respectively.\n\nThe next n lines describes polygon vertices in counter-clockwise order. i-th of them contains pair of integers x_{i} and y_{i} ( - 10^9 \u2264 x_{i} \u2264 10^9, 0 \u2264 y_{i} \u2264 w)\u00a0\u2014 coordinates of i-th polygon point. It is guaranteed that the polygon is non-degenerate.\n\n\n-----Output-----\n\nPrint the single real t\u00a0\u2014 the time the pedestrian needs to croos the road and not to be hit by the bus. The answer is considered correct if its relative or absolute error doesn't exceed 10^{ - 6}.\n\n\n-----Example-----\nInput\n5 5 1 2\n1 2\n3 1\n4 3\n3 4\n1 4\n\nOutput\n5.0000000000\n\n\n-----Note-----\n\nFollowing image describes initial position in the first sample case:\n\n[Image]"} +{"id": "00659", "text": "Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.\n\nArray a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions: a_1 \u2264 a_2 \u2264 ... \u2264 a_{n}; a_1 \u2265 a_2 \u2265 ... \u2265 a_{n}. \n\nHelp Petya find the two required positions to swap or else say that they do not exist.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5). The second line contains n non-negative space-separated integers a_1, a_2, ..., a_{n} \u2014 the elements of the array that Petya's mother presented him. All integers in the input do not exceed 10^9.\n\n\n-----Output-----\n\nIf there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.\n\n\n-----Examples-----\nInput\n1\n1\n\nOutput\n-1\n\nInput\n2\n1 2\n\nOutput\n-1\n\nInput\n4\n1 2 3 4\n\nOutput\n1 2\n\nInput\n3\n1 1 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first two samples the required pairs obviously don't exist.\n\nIn the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted."} +{"id": "00660", "text": "A tennis tournament with n participants is running. The participants are playing by an olympic system, so the winners move on and the losers drop out.\n\nThe tournament takes place in the following way (below, m is the number of the participants of the current round): let k be the maximal power of the number 2 such that k \u2264 m, k participants compete in the current round and a half of them passes to the next round, the other m - k participants pass to the next round directly, when only one participant remains, the tournament finishes. \n\nEach match requires b bottles of water for each participant and one bottle for the judge. Besides p towels are given to each participant for the whole tournament.\n\nFind the number of bottles and towels needed for the tournament.\n\nNote that it's a tennis tournament so in each match two participants compete (one of them will win and the other will lose).\n\n\n-----Input-----\n\nThe only line contains three integers n, b, p (1 \u2264 n, b, p \u2264 500) \u2014 the number of participants and the parameters described in the problem statement.\n\n\n-----Output-----\n\nPrint two integers x and y \u2014 the number of bottles and towels need for the tournament.\n\n\n-----Examples-----\nInput\n5 2 3\n\nOutput\n20 15\n\nInput\n8 2 4\n\nOutput\n35 32\n\n\n\n-----Note-----\n\nIn the first example will be three rounds: in the first round will be two matches and for each match 5 bottles of water are needed (two for each of the participants and one for the judge), in the second round will be only one match, so we need another 5 bottles of water, in the third round will also be only one match, so we need another 5 bottles of water. \n\nSo in total we need 20 bottles of water.\n\nIn the second example no participant will move on to some round directly."} +{"id": "00661", "text": "Construct a sequence a = {a_1,\\ a_2,\\ ...,\\ a_{2^{M + 1}}} of length 2^{M + 1} that satisfies the following conditions, if such a sequence exists.\n - Each integer between 0 and 2^M - 1 (inclusive) occurs twice in a.\n - For any i and j (i < j) such that a_i = a_j, the formula a_i \\ xor \\ a_{i + 1} \\ xor \\ ... \\ xor \\ a_j = K holds.\nWhat is xor (bitwise exclusive or)?\nThe xor of integers c_1, c_2, ..., c_n is defined as follows:\n - When c_1 \\ xor \\ c_2 \\ xor \\ ... \\ xor \\ c_n is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if the number of integers among c_1, c_2, ...c_m whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.\nFor example, 3 \\ xor \\ 5 = 6. (If we write it in base two: 011 xor 101 = 110.)\n\n-----Constraints-----\n - All values in input are integers.\n - 0 \\leq M \\leq 17\n - 0 \\leq K \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nM K\n\n-----Output-----\nIf there is no sequence a that satisfies the condition, print -1.\nIf there exists such a sequence a, print the elements of one such sequence a with spaces in between.\nIf there are multiple sequences that satisfies the condition, any of them will be accepted.\n\n-----Sample Input-----\n1 0\n\n-----Sample Output-----\n0 0 1 1\n\nFor this case, there are multiple sequences that satisfy the condition.\nFor example, when a = {0, 0, 1, 1}, there are two pairs (i,\\ j)\\ (i < j) such that a_i = a_j: (1, 2) and (3, 4). Since a_1 \\ xor \\ a_2 = 0 and a_3 \\ xor \\ a_4 = 0, this sequence a satisfies the condition."} +{"id": "00662", "text": "Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a bit difficult for them because chess is a game for two players, not three.\n\nSo they play with each other according to following rules: Alex and Bob play the first game, and Carl is spectating; When the game ends, the one who lost the game becomes the spectator in the next game, and the one who was spectating plays against the winner. \n\nAlex, Bob and Carl play in such a way that there are no draws.\n\nToday they have played n games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it!\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 100) \u2014 the number of games Alex, Bob and Carl played.\n\nThen n lines follow, describing the game log. i-th line contains one integer a_{i} (1 \u2264 a_{i} \u2264 3) which is equal to 1 if Alex won i-th game, to 2 if Bob won i-th game and 3 if Carl won i-th game.\n\n\n-----Output-----\n\nPrint YES if the situation described in the log was possible. Otherwise print NO.\n\n\n-----Examples-----\nInput\n3\n1\n1\n2\n\nOutput\nYES\n\nInput\n2\n1\n2\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example the possible situation is: Alex wins, Carl starts playing instead of Bob; Alex wins, Bob replaces Carl; Bob wins. \n\nThe situation in the second example is impossible because Bob loses the first game, so he cannot win the second one."} +{"id": "00663", "text": "Amr loves Geometry. One day he came up with a very interesting problem.\n\nAmr has a circle of radius r and center in point (x, y). He wants the circle center to be in new position (x', y').\n\nIn one step Amr can put a pin to the border of the circle in a certain point, then rotate the circle around that pin by any angle and finally remove the pin.\n\nHelp Amr to achieve his goal in minimum number of steps.\n\n\n-----Input-----\n\nInput consists of 5 space-separated integers r, x, y, x' y' (1 \u2264 r \u2264 10^5, - 10^5 \u2264 x, y, x', y' \u2264 10^5), circle radius, coordinates of original center of the circle and coordinates of destination center of the circle respectively.\n\n\n-----Output-----\n\nOutput a single integer \u2014 minimum number of steps required to move the center of the circle to the destination point.\n\n\n-----Examples-----\nInput\n2 0 0 0 4\n\nOutput\n1\n\nInput\n1 1 1 4 4\n\nOutput\n3\n\nInput\n4 5 6 5 6\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample test the optimal way is to put a pin at point (0, 2) and rotate the circle by 180 degrees counter-clockwise (or clockwise, no matter).\n\n[Image]"} +{"id": "00664", "text": "One day, Twilight Sparkle is interested in how to sort a sequence of integers a_1, a_2, ..., a_{n} in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:a_1, a_2, ..., a_{n} \u2192 a_{n}, a_1, a_2, ..., a_{n} - 1. \n\nHelp Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?\n\n\n-----Input-----\n\nThe first line contains an integer n (2 \u2264 n \u2264 10^5). The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5).\n\n\n-----Output-----\n\nIf it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.\n\n\n-----Examples-----\nInput\n2\n2 1\n\nOutput\n1\n\nInput\n3\n1 3 2\n\nOutput\n-1\n\nInput\n2\n1 2\n\nOutput\n0"} +{"id": "00665", "text": "New Year is coming! Vasya has prepared a New Year's verse and wants to recite it in front of Santa Claus.\n\nVasya's verse contains $n$ parts. It takes $a_i$ seconds to recite the $i$-th part. Vasya can't change the order of parts in the verse: firstly he recites the part which takes $a_1$ seconds, secondly \u2014 the part which takes $a_2$ seconds, and so on. After reciting the verse, Vasya will get the number of presents equal to the number of parts he fully recited.\n\nVasya can skip at most one part of the verse while reciting it (if he skips more than one part, then Santa will definitely notice it).\n\nSanta will listen to Vasya's verse for no more than $s$ seconds. For example, if $s = 10$, $a = [100, 9, 1, 1]$, and Vasya skips the first part of verse, then he gets two presents.\n\nNote that it is possible to recite the whole verse (if there is enough time). \n\nDetermine which part Vasya needs to skip to obtain the maximum possible number of gifts. If Vasya shouldn't skip anything, print 0. If there are multiple answers, print any of them.\n\nYou have to process $t$ test cases.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThe first line of each test case contains two integers $n$ and $s$ ($1 \\le n \\le 10^5, 1 \\le s \\le 10^9$) \u2014 the number of parts in the verse and the maximum number of seconds Santa will listen to Vasya, respectively.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) \u2014 the time it takes to recite each part of the verse.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case print one integer \u2014 the number of the part that Vasya needs to skip to obtain the maximum number of gifts. If Vasya shouldn't skip any parts, print 0.\n\n\n-----Example-----\nInput\n3\n7 11\n2 9 1 3 18 1 4\n4 35\n11 9 10 7\n1 8\n5\n\nOutput\n2\n1\n0\n\n\n\n-----Note-----\n\nIn the first test case if Vasya skips the second part then he gets three gifts.\n\nIn the second test case no matter what part of the verse Vasya skips.\n\nIn the third test case Vasya can recite the whole verse."} +{"id": "00666", "text": "Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5.... The sequence is built in the following way: at first the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on. Note that the sequence contains numbers, not digits. For example number 10 first appears in the sequence in position 55 (the elements are numerated from one).\n\nFind the number on the n-th position of the sequence.\n\n\n-----Input-----\n\nThe only line contains integer n (1 \u2264 n \u2264 10^14) \u2014 the position of the number to find.\n\nNote that the given number is too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.\n\n\n-----Output-----\n\nPrint the element in the n-th position of the sequence (the elements are numerated from one).\n\n\n-----Examples-----\nInput\n3\n\nOutput\n2\n\nInput\n5\n\nOutput\n2\n\nInput\n10\n\nOutput\n4\n\nInput\n55\n\nOutput\n10\n\nInput\n56\n\nOutput\n1"} +{"id": "00667", "text": "You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer N (3 \u2264 N \u2264 10).\n\nThe following N lines each contain N space-separated integers. jth integer in ith line a_{ij} is the length of the edge that connects vertices i and j. a_{ij} = a_{ji}, a_{ii} = 0, 1 \u2264 a_{ij} \u2264 100 for i \u2260 j.\n\n\n-----Output-----\n\nOutput the maximum length of the shortest path between any pair of vertices in the graph.\n\n\n-----Examples-----\nInput\n3\n0 1 1\n1 0 4\n1 4 0\n\nOutput\n2\n\nInput\n4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0\n\nOutput\n5\n\n\n\n-----Note-----\n\nYou're running short of keywords, so you can't use some of them:define\n\ndo\n\nfor\n\nforeach\n\nwhile\n\nrepeat\n\nuntil\n\nif\n\nthen\n\nelse\n\nelif\n\nelsif\n\nelseif\n\ncase\n\nswitch"} +{"id": "00668", "text": "Polycarp studies at the university in the group which consists of n students (including himself). All they are registrated in the social net \"TheContacnt!\".\n\nNot all students are equally sociable. About each student you know the value a_{i} \u2014 the maximum number of messages which the i-th student is agree to send per day. The student can't send messages to himself. \n\nIn early morning Polycarp knew important news that the programming credit will be tomorrow. For this reason it is necessary to urgently inform all groupmates about this news using private messages. \n\nYour task is to make a plan of using private messages, so that:\n\n the student i sends no more than a_{i} messages (for all i from 1 to n); all students knew the news about the credit (initially only Polycarp knew it); the student can inform the other student only if he knows it himself. \n\nLet's consider that all students are numerated by distinct numbers from 1 to n, and Polycarp always has the number 1.\n\nIn that task you shouldn't minimize the number of messages, the moment of time, when all knew about credit or some other parameters. Find any way how to use private messages which satisfies requirements above. \n\n\n-----Input-----\n\nThe first line contains the positive integer n (2 \u2264 n \u2264 100) \u2014 the number of students. \n\nThe second line contains the sequence a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 100), where a_{i} equals to the maximum number of messages which can the i-th student agree to send. Consider that Polycarp always has the number 1.\n\n\n-----Output-----\n\nPrint -1 to the first line if it is impossible to inform all students about credit. \n\nOtherwise, in the first line print the integer k \u2014 the number of messages which will be sent. In each of the next k lines print two distinct integers f and t, meaning that the student number f sent the message with news to the student number t. All messages should be printed in chronological order. It means that the student, who is sending the message, must already know this news. It is assumed that students can receive repeated messages with news of the credit. \n\nIf there are several answers, it is acceptable to print any of them. \n\n\n-----Examples-----\nInput\n4\n1 2 1 0\n\nOutput\n3\n1 2\n2 4\n2 3\n\nInput\n6\n2 0 1 3 2 0\n\nOutput\n6\n1 3\n3 4\n1 2\n4 5\n5 6\n4 6\n\nInput\n3\n0 2 2\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first test Polycarp (the student number 1) can send the message to the student number 2, who after that can send the message to students number 3 and 4. Thus, all students knew about the credit."} +{"id": "00669", "text": "You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices b_1, b_2, ..., b_{k} (1 \u2264 b_1 < b_2 < ... < b_{k} \u2264 n) in such a way that the value of $\\sum_{i = 1}^{k} a_{b_{i}} \\operatorname{mod} m$ is maximized. Chosen sequence can be empty.\n\nPrint the maximum possible value of $\\sum_{i = 1}^{k} a_{b_{i}} \\operatorname{mod} m$.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 35, 1 \u2264 m \u2264 10^9).\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint the maximum possible value of $\\sum_{i = 1}^{k} a_{b_{i}} \\operatorname{mod} m$.\n\n\n-----Examples-----\nInput\n4 4\n5 2 4 1\n\nOutput\n3\n\nInput\n3 20\n199 41 299\n\nOutput\n19\n\n\n\n-----Note-----\n\nIn the first example you can choose a sequence b = {1, 2}, so the sum $\\sum_{i = 1}^{k} a_{b_{i}}$ is equal to 7 (and that's 3 after taking it modulo 4).\n\nIn the second example you can choose a sequence b = {3}."} +{"id": "00670", "text": "In this problem we consider a very simplified model of Barcelona city.\n\nBarcelona can be represented as a plane with streets of kind $x = c$ and $y = c$ for every integer $c$ (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points $(x, y)$ for which $ax + by + c = 0$.\n\nOne can walk along streets, including the avenue. You are given two integer points $A$ and $B$ somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to $B$ from $A$.\n\n\n-----Input-----\n\nThe first line contains three integers $a$, $b$ and $c$ ($-10^9\\leq a, b, c\\leq 10^9$, at least one of $a$ and $b$ is not zero) representing the Diagonal Avenue.\n\nThe next line contains four integers $x_1$, $y_1$, $x_2$ and $y_2$ ($-10^9\\leq x_1, y_1, x_2, y_2\\leq 10^9$) denoting the points $A = (x_1, y_1)$ and $B = (x_2, y_2)$.\n\n\n-----Output-----\n\nFind the minimum possible travel distance between $A$ and $B$. Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.\n\nFormally, let your answer be $a$, and the jury's answer be $b$. Your answer is accepted if and only if $\\frac{|a - b|}{\\max{(1, |b|)}} \\le 10^{-6}$.\n\n\n-----Examples-----\nInput\n1 1 -3\n0 3 3 0\n\nOutput\n4.2426406871\n\nInput\n3 1 -9\n0 3 3 -1\n\nOutput\n6.1622776602\n\n\n\n-----Note-----\n\nThe first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot. [Image]"} +{"id": "00671", "text": "Every year, hundreds of people come to summer camps, they learn new algorithms and solve hard problems.\n\nThis is your first year at summer camp, and you are asked to solve the following problem. All integers starting with 1 are written in one line. The prefix of these line is \"123456789101112131415...\". Your task is to print the n-th digit of this string (digits are numbered starting with 1.\n\n\n-----Input-----\n\nThe only line of the input contains a single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the position of the digit you need to print.\n\n\n-----Output-----\n\nPrint the n-th digit of the line.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n3\n\nInput\n11\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample the digit at position 3 is '3', as both integers 1 and 2 consist on one digit.\n\nIn the second sample, the digit at position 11 is '0', it belongs to the integer 10."} +{"id": "00672", "text": "Last week, Hamed learned about a new type of equations in his math class called Modular Equations. Lets define i modulo j as the remainder of division of i by j and denote it by $i \\operatorname{mod} j$. A Modular Equation, as Hamed's teacher described, is an equation of the form $a \\operatorname{mod} x = b$ in which a and b are two non-negative integers and x is a variable. We call a positive integer x for which $a \\operatorname{mod} x = b$ a solution of our equation.\n\nHamed didn't pay much attention to the class since he was watching a movie. He only managed to understand the definitions of these equations.\n\nNow he wants to write his math exercises but since he has no idea how to do that, he asked you for help. He has told you all he knows about Modular Equations and asked you to write a program which given two numbers a and b determines how many answers the Modular Equation $a \\operatorname{mod} x = b$ has.\n\n\n-----Input-----\n\nIn the only line of the input two space-separated integers a and b (0 \u2264 a, b \u2264 10^9) are given.\n\n\n-----Output-----\n\nIf there is an infinite number of answers to our equation, print \"infinity\" (without the quotes). Otherwise print the number of solutions of the Modular Equation $a \\operatorname{mod} x = b$.\n\n\n-----Examples-----\nInput\n21 5\n\nOutput\n2\n\nInput\n9435152 272\n\nOutput\n282\n\nInput\n10 10\n\nOutput\ninfinity\n\n\n\n-----Note-----\n\nIn the first sample the answers of the Modular Equation are 8 and 16 since $21 \\operatorname{mod} 8 = 21 \\operatorname{mod} 16 = 5$"} +{"id": "00673", "text": "Johny likes numbers n and k very much. Now Johny wants to find the smallest integer x greater than n, so it is divisible by the number k.\n\n\n-----Input-----\n\nThe only line contains two integers n and k (1 \u2264 n, k \u2264 10^9).\n\n\n-----Output-----\n\nPrint the smallest integer x > n, so it is divisible by the number k.\n\n\n-----Examples-----\nInput\n5 3\n\nOutput\n6\n\nInput\n25 13\n\nOutput\n26\n\nInput\n26 13\n\nOutput\n39"} +{"id": "00674", "text": "A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring.\n\nYou are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print \"NO\" (without quotes).\n\nA substring of a string is a contiguous subsequence of letters in the string. For example, \"ab\", \"c\", \"abc\" are substrings of string \"abc\", while \"ac\" is not a substring of that string.\n\nThe number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap.\n\nString a is lexicographically smaller than string b, if a is a prefix of b, or a has a smaller letter at the first position where a and b differ.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of strings in the set.\n\nEach of the next n lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct.\n\nThe total length of the strings doesn't exceed 10^5.\n\n\n-----Output-----\n\nPrint the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print \"NO\" (without quotes) if there are no good strings.\n\n\n-----Examples-----\nInput\n4\nmail\nai\nlru\ncf\n\nOutput\ncfmailru\n\nInput\n3\nkek\npreceq\ncheburek\n\nOutput\nNO\n\n\n\n-----Note-----\n\nOne can show that in the first sample only two good strings with minimum length exist: \"cfmailru\" and \"mailrucf\". The first string is lexicographically minimum."} +{"id": "00675", "text": "Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.\n\nFor each of the m ghosts Anya knows the time at which it comes: the i-th visit will happen w_{i} seconds after midnight, all w_{i}'s are distinct. Each visit lasts exactly one second.\n\nWhat is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time.\n\n\n-----Input-----\n\nThe first line contains three integers m, t, r (1 \u2264 m, t, r \u2264 300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit. \n\nThe next line contains m space-separated numbers w_{i} (1 \u2264 i \u2264 m, 1 \u2264 w_{i} \u2264 300), the i-th of them repesents at what second after the midnight the i-th ghost will come. All w_{i}'s are distinct, they follow in the strictly increasing order.\n\n\n-----Output-----\n\nIf it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that.\n\nIf that is impossible, print - 1.\n\n\n-----Examples-----\nInput\n1 8 3\n10\n\nOutput\n3\n\nInput\n2 10 1\n5 8\n\nOutput\n1\n\nInput\n1 1 3\n10\n\nOutput\n-1\n\n\n\n-----Note-----\n\nAnya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.\n\nIt takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.\n\nIn the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight.\n\nIn the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.\n\nIn the third sample test the answer is - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes."} +{"id": "00676", "text": "There is an old tradition of keeping 4 boxes of candies in the house in Cyberland. The numbers of candies are special if their arithmetic mean, their median and their range are all equal. By definition, for a set {x_1, x_2, x_3, x_4} (x_1 \u2264 x_2 \u2264 x_3 \u2264 x_4) arithmetic mean is $\\frac{x_{1} + x_{2} + x_{3} + x_{4}}{4}$, median is $\\frac{x_{2} + x_{3}}{2}$ and range is x_4 - x_1. The arithmetic mean and median are not necessary integer. It is well-known that if those three numbers are same, boxes will create a \"debugging field\" and codes in the field will have no bugs.\n\nFor example, 1, 1, 3, 3 is the example of 4 numbers meeting the condition because their mean, median and range are all equal to 2.\n\nJeff has 4 special boxes of candies. However, something bad has happened! Some of the boxes could have been lost and now there are only n (0 \u2264 n \u2264 4) boxes remaining. The i-th remaining box contains a_{i} candies.\n\nNow Jeff wants to know: is there a possible way to find the number of candies of the 4 - n missing boxes, meeting the condition above (the mean, median and range are equal)?\n\n\n-----Input-----\n\nThe first line of input contains an only integer n (0 \u2264 n \u2264 4).\n\nThe next n lines contain integers a_{i}, denoting the number of candies in the i-th box (1 \u2264 a_{i} \u2264 500).\n\n\n-----Output-----\n\nIn the first output line, print \"YES\" if a solution exists, or print \"NO\" if there is no solution.\n\nIf a solution exists, you should output 4 - n more lines, each line containing an integer b, denoting the number of candies in a missing box.\n\nAll your numbers b must satisfy inequality 1 \u2264 b \u2264 10^6. It is guaranteed that if there exists a positive integer solution, you can always find such b's meeting the condition. If there are multiple answers, you are allowed to print any of them.\n\nGiven numbers a_{i} may follow in any order in the input, not necessary in non-decreasing.\n\na_{i} may have stood at any positions in the original set, not necessary on lowest n first positions.\n\n\n-----Examples-----\nInput\n2\n1\n1\n\nOutput\nYES\n3\n3\n\nInput\n3\n1\n1\n1\n\nOutput\nNO\n\nInput\n4\n1\n2\n2\n3\n\nOutput\nYES\n\n\n\n-----Note-----\n\nFor the first sample, the numbers of candies in 4 boxes can be 1, 1, 3, 3. The arithmetic mean, the median and the range of them are all 2.\n\nFor the second sample, it's impossible to find the missing number of candies.\n\nIn the third example no box has been lost and numbers satisfy the condition.\n\nYou may output b in any order."} +{"id": "00677", "text": "You are given $q$ queries in the following form:\n\nGiven three integers $l_i$, $r_i$ and $d_i$, find minimum positive integer $x_i$ such that it is divisible by $d_i$ and it does not belong to the segment $[l_i, r_i]$.\n\nCan you answer all the queries?\n\nRecall that a number $x$ belongs to segment $[l, r]$ if $l \\le x \\le r$.\n\n\n-----Input-----\n\nThe first line contains one integer $q$ ($1 \\le q \\le 500$) \u2014 the number of queries.\n\nThen $q$ lines follow, each containing a query given in the format $l_i$ $r_i$ $d_i$ ($1 \\le l_i \\le r_i \\le 10^9$, $1 \\le d_i \\le 10^9$). $l_i$, $r_i$ and $d_i$ are integers.\n\n\n-----Output-----\n\nFor each query print one integer: the answer to this query.\n\n\n-----Example-----\nInput\n5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n\nOutput\n6\n4\n1\n3\n10"} +{"id": "00678", "text": "HAI\n\nI HAS A TUX\n\nGIMMEH TUX\n\nI HAS A FOO ITS 0\n\nI HAS A BAR ITS 0\n\nI HAS A BAZ ITS 0\n\nI HAS A QUZ ITS 1\n\nTUX IS NOW A NUMBR\n\nIM IN YR LOOP NERFIN YR TUX TIL BOTH SAEM TUX AN 0\n\nI HAS A PUR\n\nGIMMEH PUR\n\nPUR IS NOW A NUMBR\n\nFOO R SUM OF FOO AN PUR\n\nBAR R SUM OF BAR AN 1\n\nBOTH SAEM BIGGR OF PRODUKT OF FOO AN QUZ AN PRODUKT OF BAR BAZ AN PRODUKT OF FOO AN QUZ\n\nO RLY?\n\nYA RLY\n\nBAZ R FOO\n\nQUZ R BAR\n\nOIC\n\nIM OUTTA YR LOOP\n\nBAZ IS NOW A NUMBAR\n\nVISIBLE SMOOSH QUOSHUNT OF BAZ QUZ\n\nKTHXBYE\n\n\n\n\n-----Input-----\n\nThe input contains between 1 and 10 lines, i-th line contains an integer number x_{i} (0 \u2264 x_{i} \u2264 9).\n\n\n-----Output-----\n\nOutput a single real number. The answer is considered to be correct if its absolute or relative error does not exceed 10^{ - 4}.\n\n\n-----Examples-----\nInput\n3\n0\n1\n1\n\nOutput\n0.666667"} +{"id": "00679", "text": "When the curtains are opened, a canvas unfolds outside. Kanno marvels at all the blonde colours along the riverside\u00a0\u2014 not tangerines, but blossoms instead.\n\n\"What a pity it's already late spring,\" sighs Mino with regret, \"one more drizzling night and they'd be gone.\"\n\n\"But these blends are at their best, aren't they?\" Absorbed in the landscape, Kanno remains optimistic. \n\nThe landscape can be expressed as a row of consecutive cells, each of which either contains a flower of colour amber or buff or canary yellow, or is empty.\n\nWhen a flower withers, it disappears from the cell that it originally belonged to, and it spreads petals of its colour in its two neighbouring cells (or outside the field if the cell is on the side of the landscape). In case petals fall outside the given cells, they simply become invisible.\n\nYou are to help Kanno determine whether it's possible that after some (possibly none or all) flowers shed their petals, at least one of the cells contains all three colours, considering both petals and flowers. Note that flowers can wither in arbitrary order.\n\n\n-----Input-----\n\nThe first and only line of input contains a non-empty string $s$ consisting of uppercase English letters 'A', 'B', 'C' and characters '.' (dots) only ($\\lvert s \\rvert \\leq 100$)\u00a0\u2014 denoting cells containing an amber flower, a buff one, a canary yellow one, and no flowers, respectively.\n\n\n-----Output-----\n\nOutput \"Yes\" if it's possible that all three colours appear in some cell, and \"No\" otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n.BAC.\n\nOutput\nYes\n\nInput\nAA..CB\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first example, the buff and canary yellow flowers can leave their petals in the central cell, blending all three colours in it.\n\nIn the second example, it's impossible to satisfy the requirement because there is no way that amber and buff meet in any cell."} +{"id": "00680", "text": "The Squareland national forest is divided into equal $1 \\times 1$ square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates $(x, y)$ of its south-west corner.\n\nThree friends, Alice, Bob, and Charlie are going to buy three distinct plots of land $A, B, C$ in the forest. Initially, all plots in the forest (including the plots $A, B, C$) are covered by trees. The friends want to visit each other, so they want to clean some of the plots from trees. After cleaning, one should be able to reach any of the plots $A, B, C$ from any other one of those by moving through adjacent cleared plots. Two plots are adjacent if they share a side. [Image] For example, $A=(0,0)$, $B=(1,1)$, $C=(2,2)$. The minimal number of plots to be cleared is $5$. One of the ways to do it is shown with the gray color. \n\nOf course, the friends don't want to strain too much. Help them find out the smallest number of plots they need to clean from trees.\n\n\n-----Input-----\n\nThe first line contains two integers $x_A$ and $y_A$\u00a0\u2014 coordinates of the plot $A$ ($0 \\leq x_A, y_A \\leq 1000$). The following two lines describe coordinates $(x_B, y_B)$ and $(x_C, y_C)$ of plots $B$ and $C$ respectively in the same format ($0 \\leq x_B, y_B, x_C, y_C \\leq 1000$). It is guaranteed that all three plots are distinct.\n\n\n-----Output-----\n\nOn the first line print a single integer $k$\u00a0\u2014 the smallest number of plots needed to be cleaned from trees. The following $k$ lines should contain coordinates of all plots needed to be cleaned. All $k$ plots should be distinct. You can output the plots in any order.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n0 0\n1 1\n2 2\n\nOutput\n5\n0 0\n1 0\n1 1\n1 2\n2 2\n\nInput\n0 0\n2 0\n1 1\n\nOutput\n4\n0 0\n1 0\n1 1\n2 0\n\n\n\n-----Note-----\n\nThe first example is shown on the picture in the legend.\n\nThe second example is illustrated with the following image: [Image]"} +{"id": "00681", "text": "Reziba has many magic gems. Each magic gem can be split into $M$ normal gems. The amount of space each magic (and normal) gem takes is $1$ unit. A normal gem cannot be split.\n\nReziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $N$ units. If a magic gem is chosen and split, it takes $M$ units of space (since it is split into $M$ gems); if a magic gem is not split, it takes $1$ unit.\n\nHow many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $N$ units? Print the answer modulo $1000000007$ ($10^9+7$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ.\n\n\n-----Input-----\n\nThe input contains a single line consisting of $2$ integers $N$ and $M$ ($1 \\le N \\le 10^{18}$, $2 \\le M \\le 100$).\n\n\n-----Output-----\n\nPrint one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $N$ units. Print the answer modulo $1000000007$ ($10^9+7$).\n\n\n-----Examples-----\nInput\n4 2\n\nOutput\n5\n\nInput\n3 2\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example each magic gem can split into $2$ normal gems, and we know that the total amount of gems are $4$.\n\nLet $1$ denote a magic gem, and $0$ denote a normal gem.\n\nThe total configurations you can have is: $1 1 1 1$ (None of the gems split); $0 0 1 1$ (First magic gem splits into $2$ normal gems); $1 0 0 1$ (Second magic gem splits into $2$ normal gems); $1 1 0 0$ (Third magic gem splits into $2$ normal gems); $0 0 0 0$ (First and second magic gems split into total $4$ normal gems). \n\nHence, answer is $5$."} +{"id": "00682", "text": "Little Petya is learning to play chess. He has already learned how to move a king, a rook and a bishop. Let us remind you the rules of moving chess pieces. A chessboard is 64 square fields organized into an 8 \u00d7 8 table. A field is represented by a pair of integers (r, c) \u2014 the number of the row and the number of the column (in a classical game the columns are traditionally indexed by letters). Each chess piece takes up exactly one field. To make a move is to move a chess piece, the pieces move by the following rules: A rook moves any number of fields horizontally or vertically. A bishop moves any number of fields diagonally. A king moves one field in any direction \u2014 horizontally, vertically or diagonally. [Image] The pieces move like that \n\nPetya is thinking about the following problem: what minimum number of moves is needed for each of these pieces to move from field (r_1, c_1) to field (r_2, c_2)? At that, we assume that there are no more pieces besides this one on the board. Help him solve this problem.\n\n\n-----Input-----\n\nThe input contains four integers r_1, c_1, r_2, c_2 (1 \u2264 r_1, c_1, r_2, c_2 \u2264 8) \u2014 the coordinates of the starting and the final field. The starting field doesn't coincide with the final one.\n\nYou can assume that the chessboard rows are numbered from top to bottom 1 through 8, and the columns are numbered from left to right 1 through 8.\n\n\n-----Output-----\n\nPrint three space-separated integers: the minimum number of moves the rook, the bishop and the king (in this order) is needed to move from field (r_1, c_1) to field (r_2, c_2). If a piece cannot make such a move, print a 0 instead of the corresponding number.\n\n\n-----Examples-----\nInput\n4 3 1 6\n\nOutput\n2 1 3\n\nInput\n5 5 5 6\n\nOutput\n1 0 1"} +{"id": "00683", "text": "-----Input-----\n\nThe input contains a single integer a (0 \u2264 a \u2264 35).\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n8\n\nInput\n10\n\nOutput\n1024"} +{"id": "00684", "text": "Ichihime is the current priestess of the Mahjong Soul Temple. She claims to be human, despite her cat ears.\n\nThese days the temple is holding a math contest. Usually, Ichihime lacks interest in these things, but this time the prize for the winner is her favorite \u2014 cookies. Ichihime decides to attend the contest. Now she is solving the following problem.[Image]\u00a0\n\nYou are given four positive integers $a$, $b$, $c$, $d$, such that $a \\leq b \\leq c \\leq d$. \n\nYour task is to find three integers $x$, $y$, $z$, satisfying the following conditions: $a \\leq x \\leq b$. $b \\leq y \\leq c$. $c \\leq z \\leq d$. There exists a triangle with a positive non-zero area and the lengths of its three sides are $x$, $y$, and $z$.\n\nIchihime desires to get the cookie, but the problem seems too hard for her. Can you help her?\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 1000$) \u00a0\u2014 the number of test cases.\n\nThe next $t$ lines describe test cases. Each test case is given as four space-separated integers $a$, $b$, $c$, $d$ ($1 \\leq a \\leq b \\leq c \\leq d \\leq 10^9$).\n\n\n-----Output-----\n\nFor each test case, print three integers $x$, $y$, $z$ \u00a0\u2014 the integers you found satisfying the conditions given in the statement.\n\nIt is guaranteed that the answer always exists. If there are multiple answers, print any.\n\n\n-----Example-----\nInput\n4\n1 3 5 7\n1 5 5 7\n100000 200000 300000 400000\n1 1 977539810 977539810\n\nOutput\n3 4 5\n5 5 5\n182690 214748 300999\n1 977539810 977539810\n\n\n\n-----Note-----\n\nOne of the possible solutions to the first test case:\n\n[Image]\n\nOne of the possible solutions to the second test case:\n\n[Image]"} +{"id": "00685", "text": "A plane is flying at a constant height of $h$ meters above the ground surface. Let's consider that it is flying from the point $(-10^9, h)$ to the point $(10^9, h)$ parallel with $Ox$ axis.\n\nA glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let's consider that he may start only when the plane's coordinates are integers). After jumping from the plane, he will fly in the same direction as the plane, parallel to $Ox$ axis, covering a unit of distance every second. Naturally, he will also descend; thus his second coordinate will decrease by one unit every second.\n\nThere are ascending air flows on certain segments, each such segment is characterized by two numbers $x_1$ and $x_2$ ($x_1 < x_2$) representing its endpoints. No two segments share any common points. When the glider is inside one of such segments, he doesn't descend, so his second coordinate stays the same each second. The glider still flies along $Ox$ axis, covering one unit of distance every second. [Image] If the glider jumps out at $1$, he will stop at $10$. Otherwise, if he jumps out at $2$, he will stop at $12$. \n\nDetermine the maximum distance along $Ox$ axis from the point where the glider's flight starts to the point where his flight ends if the glider can choose any integer coordinate to jump from the plane and start his flight. After touching the ground the glider stops altogether, so he cannot glide through an ascending airflow segment if his second coordinate is $0$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $h$ $(1 \\le n \\le 2\\cdot10^{5}, 1 \\le h \\le 10^{9})$\u00a0\u2014 the number of ascending air flow segments and the altitude at which the plane is flying, respectively.\n\nEach of the next $n$ lines contains two integers $x_{i1}$ and $x_{i2}$ $(1 \\le x_{i1} < x_{i2} \\le 10^{9})$\u00a0\u2014 the endpoints of the $i$-th ascending air flow segment. No two segments intersect, and they are given in ascending order.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the maximum distance along $Ox$ axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate.\n\n\n-----Examples-----\nInput\n3 4\n2 5\n7 9\n10 11\n\nOutput\n10\n\nInput\n5 10\n5 7\n11 12\n16 20\n25 26\n30 33\n\nOutput\n18\n\nInput\n1 1000000000\n1 1000000000\n\nOutput\n1999999999\n\n\n\n-----Note-----\n\nIn the first example if the glider can jump out at $(2, 4)$, then the landing point is $(12, 0)$, so the distance is $12-2 = 10$.\n\nIn the second example the glider can fly from $(16,10)$ to $(34,0)$, and the distance is $34-16=18$.\n\nIn the third example the glider can fly from $(-100,1000000000)$ to $(1999999899,0)$, so the distance is $1999999899-(-100)=1999999999$."} +{"id": "00686", "text": "You are given two integers $x$ and $y$ (it is guaranteed that $x > y$). You may choose any prime integer $p$ and subtract it any number of times from $x$. Is it possible to make $x$ equal to $y$?\n\nRecall that a prime number is a positive integer that has exactly two positive divisors: $1$ and this integer itself. The sequence of prime numbers starts with $2$, $3$, $5$, $7$, $11$.\n\nYour program should solve $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases.\n\nThen $t$ lines follow, each describing a test case. Each line contains two integers $x$ and $y$ ($1 \\le y < x \\le 10^{18}$).\n\n\n-----Output-----\n\nFor each test case, print YES if it is possible to choose a prime number $p$ and subtract it any number of times from $x$ so that $x$ becomes equal to $y$. Otherwise, print NO.\n\nYou may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).\n\n\n-----Example-----\nInput\n4\n100 98\n42 32\n1000000000000000000 1\n41 40\n\nOutput\nYES\nYES\nYES\nNO\n\n\n\n-----Note-----\n\nIn the first test of the example you may choose $p = 2$ and subtract it once.\n\nIn the second test of the example you may choose $p = 5$ and subtract it twice. Note that you cannot choose $p = 7$, subtract it, then choose $p = 3$ and subtract it again.\n\nIn the third test of the example you may choose $p = 3$ and subtract it $333333333333333333$ times."} +{"id": "00687", "text": "Kolya is very absent-minded. Today his math teacher asked him to solve a simple problem with the equation $a + 1 = b$ with positive integers $a$ and $b$, but Kolya forgot the numbers $a$ and $b$. He does, however, remember that the first (leftmost) digit of $a$ was $d_a$, and the first (leftmost) digit of $b$ was $d_b$.\n\nCan you reconstruct any equation $a + 1 = b$ that satisfies this property? It may be possible that Kolya misremembers the digits, and there is no suitable equation, in which case report so.\n\n\n-----Input-----\n\nThe only line contains two space-separated digits $d_a$ and $d_b$ ($1 \\leq d_a, d_b \\leq 9$).\n\n\n-----Output-----\n\nIf there is no equation $a + 1 = b$ with positive integers $a$ and $b$ such that the first digit of $a$ is $d_a$, and the first digit of $b$ is $d_b$, print a single number $-1$.\n\nOtherwise, print any suitable $a$ and $b$ that both are positive and do not exceed $10^9$. It is guaranteed that if a solution exists, there also exists a solution with both numbers not exceeding $10^9$.\n\n\n-----Examples-----\nInput\n1 2\n\nOutput\n199 200\n\nInput\n4 4\n\nOutput\n412 413\n\nInput\n5 7\n\nOutput\n-1\n\nInput\n6 2\n\nOutput\n-1"} +{"id": "00688", "text": "Do you remember how Kai constructed the word \"eternity\" using pieces of ice as components?\n\nLittle Sheldon plays with pieces of ice, each piece has exactly one digit between 0 and 9. He wants to construct his favourite number t. He realized that digits 6 and 9 are very similar, so he can rotate piece of ice with 6 to use as 9 (and vice versa). Similary, 2 and 5 work the same. There is no other pair of digits with similar effect. He called this effect \"Digital Mimicry\".\n\nSheldon favourite number is t. He wants to have as many instances of t as possible. How many instances he can construct using the given sequence of ice pieces. He can use any piece at most once. \n\n\n-----Input-----\n\nThe first line contains integer t (1 \u2264 t \u2264 10000). The second line contains the sequence of digits on the pieces. The length of line is equal to the number of pieces and between 1 and 200, inclusive. It contains digits between 0 and 9.\n\n\n-----Output-----\n\nPrint the required number of instances.\n\n\n-----Examples-----\nInput\n42\n23454\n\nOutput\n2\n\nInput\n169\n12118999\n\nOutput\n1\n\n\n\n-----Note-----\n\nThis problem contains very weak pretests."} +{"id": "00689", "text": "You are given $n$ strings $s_1, s_2, \\ldots, s_n$ consisting of lowercase Latin letters.\n\nIn one operation you can remove a character from a string $s_i$ and insert it to an arbitrary position in a string $s_j$ ($j$ may be equal to $i$). You may perform this operation any number of times. Is it possible to make all $n$ strings equal?\n\n\n-----Input-----\n\nThe first line contains $t$ ($1 \\le t \\le 10$): the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 1000$): the number of strings.\n\n$n$ lines follow, the $i$-th line contains $s_i$ ($1 \\le \\lvert s_i \\rvert \\le 1000$).\n\nThe sum of lengths of all strings in all test cases does not exceed $1000$.\n\n\n-----Output-----\n\nIf it is possible to make the strings equal, print \"YES\" (without quotes).\n\nOtherwise, print \"NO\" (without quotes).\n\nYou can output each character in either lowercase or uppercase.\n\n\n-----Example-----\nInput\n4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc\n\nOutput\nYES\nNO\nYES\nNO\n\n\n\n-----Note-----\n\nIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings \"ca\" and \"cbab\" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to \"cab\". \n\nIn the second test case, it is impossible to make all $n$ strings equal."} +{"id": "00690", "text": "You know that Japan is the country with almost the largest 'electronic devices per person' ratio. So you might be quite surprised to find out that the primary school in Japan teaches to count using a Soroban \u2014 an abacus developed in Japan. This phenomenon has its reasons, of course, but we are not going to speak about them. Let's have a look at the Soroban's construction. [Image] \n\nSoroban consists of some number of rods, each rod contains five beads. We will assume that the rods are horizontal lines. One bead on each rod (the leftmost one) is divided from the others by a bar (the reckoning bar). This single bead is called go-dama and four others are ichi-damas. Each rod is responsible for representing a single digit from 0 to 9. We can obtain the value of a digit by following simple algorithm: Set the value of a digit equal to 0. If the go-dama is shifted to the right, add 5. Add the number of ichi-damas shifted to the left. \n\nThus, the upper rod on the picture shows digit 0, the middle one shows digit 2 and the lower one shows 7. We will consider the top rod to represent the last decimal digit of a number, so the picture shows number 720.\n\nWrite the program that prints the way Soroban shows the given number n.\n\n\n-----Input-----\n\nThe first line contains a single integer n (0 \u2264 n < 10^9).\n\n\n-----Output-----\n\nPrint the description of the decimal digits of number n from the last one to the first one (as mentioned on the picture in the statement), one per line. Print the beads as large English letters 'O', rod pieces as character '-' and the reckoning bar as '|'. Print as many rods, as many digits are in the decimal representation of number n without leading zeroes. We can assume that number 0 has no leading zeroes.\n\n\n-----Examples-----\nInput\n2\n\nOutput\nO-|OO-OO\n\nInput\n13\n\nOutput\nO-|OOO-O\nO-|O-OOO\n\nInput\n720\n\nOutput\nO-|-OOOO\nO-|OO-OO\n-O|OO-OO"} +{"id": "00691", "text": "Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets.\n\nDr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to find a set of n distinct non-negative integers such the bitwise-xor sum of the integers in it is exactly x. Dr. Evil doesn't like big numbers, so any number in the set shouldn't be greater than 10^6.\n\n\n-----Input-----\n\nThe only line contains two integers n and x (1 \u2264 n \u2264 10^5, 0 \u2264 x \u2264 10^5)\u00a0\u2014 the number of elements in the set and the desired bitwise-xor, respectively.\n\n\n-----Output-----\n\nIf there is no such set, print \"NO\" (without quotes).\n\nOtherwise, on the first line print \"YES\" (without quotes) and on the second line print n distinct integers, denoting the elements in the set is any order. If there are multiple solutions you can print any of them.\n\n\n-----Examples-----\nInput\n5 5\n\nOutput\nYES\n1 2 4 5 7\nInput\n3 6\n\nOutput\nYES\n1 2 5\n\n\n-----Note-----\n\nYou can read more about the bitwise-xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR\n\nFor the first sample $1 \\oplus 2 \\oplus 4 \\oplus 5 \\oplus 7 = 5$.\n\nFor the second sample $1 \\oplus 2 \\oplus 5 = 6$."} +{"id": "00692", "text": "Btoh yuo adn yuor roomatme lhoate wianshg disehs, btu stlil sdmoeboy msut peorrfm tihs cohre dialy. Oen dya yuo decdie to idourtcne smoe syestm. Yuor rmmotaoe sstgegus teh fooniwllg dael. Yuo argee on tow arayrs of ientgres M adn R, nmebur upmicnog dyas (induiclng teh cunrret oen) wtih sicsescuve irnegets (teh ceurrnt dya is zreo), adn yuo wsah teh diehss on dya D if adn olny if terhe etsixs an iednx i scuh taht D\u00a0mod\u00a0M[i] = R[i], otwsehrie yuor rmootmae deos it. Yuo lkie teh cncepot, btu yuor rmotaome's cuinnng simle meaks yuo ssecupt sthnoemig, so yuo itennd to vefriy teh fnerisas of teh aemnrgeet.\n\nYuo aer geivn ayarrs M adn R. Cuaclatle teh pceanregte of dyas on wchih yuo edn up dnoig teh wisahng. Amsuse taht yuo hvae iiiftlneny mnay dyas aehad of yuo. \n\n\n-----Input-----\n\nThe first line of input contains a single integer N (1 \u2264 N \u2264 16).\n\nThe second and third lines of input contain N integers each, all between 0 and 16, inclusive, and represent arrays M and R, respectively. All M[i] are positive, for each i R[i] < M[i].\n\n\n-----Output-----\n\nOutput a single real number. The answer is considered to be correct if its absolute or relative error does not exceed 10^{ - 4}.\n\n\n-----Examples-----\nInput\n1\n2\n0\n\nOutput\n0.500000\n\nInput\n2\n2 3\n1 0\n\nOutput\n0.666667"} +{"id": "00693", "text": "There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. \n\nHeidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k_1 knights with lightsabers of the first color, k_2 knights with lightsabers of the second color, ..., k_{m} knights with lightsabers of the m-th color.\n\nHowever, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights.\n\n\n-----Input-----\n\nThe first line of the input contains n (1 \u2264 n \u2264 2\u00b710^5) and m (1 \u2264 m \u2264 n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k_1, k_2, ..., k_{m} (with $1 \\leq \\sum_{i = 1}^{m} k_{i} \\leq n$) \u2013 the desired counts of Jedi Knights with lightsabers of each color from 1 to m.\n\n\n-----Output-----\n\nOutput one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output - 1.\n\n\n-----Example-----\nInput\n8 3\n3 3 1 2 2 1 1 3\n3 1 1\n\nOutput\n1"} +{"id": "00694", "text": "Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won.\n\nHaving carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long integer which may consist of even a million digits!\n\nPolycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisible by a as a separate number, and the second (right) part is divisible by b as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values a and b.\n\nHelp Polycarpus and find any suitable method to cut the public key.\n\n\n-----Input-----\n\nThe first line of the input contains the public key of the messenger \u2014 an integer without leading zeroes, its length is in range from 1 to 10^6 digits. The second line contains a pair of space-separated positive integers a, b (1 \u2264 a, b \u2264 10^8).\n\n\n-----Output-----\n\nIn the first line print \"YES\" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines \u2014 the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by a, and the right part must be divisible by b. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them.\n\nIf there is no answer, print in a single line \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n116401024\n97 1024\n\nOutput\nYES\n11640\n1024\n\nInput\n284254589153928171911281811000\n1009 1000\n\nOutput\nYES\n2842545891539\n28171911281811000\n\nInput\n120\n12 1\n\nOutput\nNO"} +{"id": "00695", "text": "[Image] \n\n\n-----Input-----\n\nThe input contains two integers a_1, a_2 (0 \u2264 a_{i} \u2264 32), separated by a single space.\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n0\n\nInput\n3 7\n\nOutput\n0\n\nInput\n13 10\n\nOutput\n1"} +{"id": "00696", "text": "The cows have just learned what a primitive root is! Given a prime p, a primitive root $\\operatorname{mod} p$ is an integer x (1 \u2264 x < p) such that none of integers x - 1, x^2 - 1, ..., x^{p} - 2 - 1 are divisible by p, but x^{p} - 1 - 1 is. \n\nUnfortunately, computing primitive roots can be time consuming, so the cows need your help. Given a prime p, help the cows find the number of primitive roots $\\operatorname{mod} p$.\n\n\n-----Input-----\n\nThe input contains a single line containing an integer p (2 \u2264 p < 2000). It is guaranteed that p is a prime.\n\n\n-----Output-----\n\nOutput on a single line the number of primitive roots $\\operatorname{mod} p$.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n1\n\nInput\n5\n\nOutput\n2\n\n\n\n-----Note-----\n\nThe only primitive root $\\operatorname{mod} 3$ is 2.\n\nThe primitive roots $\\operatorname{mod} 5$ are 2 and 3."} +{"id": "00697", "text": "Natasha's favourite numbers are $n$ and $1$, and Sasha's favourite numbers are $m$ and $-1$. One day Natasha and Sasha met and wrote down every possible array of length $n+m$ such that some $n$ of its elements are equal to $1$ and another $m$ elements are equal to $-1$. For each such array they counted its maximal prefix sum, probably an empty one which is equal to $0$ (in another words, if every nonempty prefix sum is less to zero, then it is considered equal to zero). Formally, denote as $f(a)$ the maximal prefix sum of an array $a_{1, \\ldots ,l}$ of length $l \\geq 0$. Then: \n\n$$f(a) = \\max (0, \\smash{\\displaystyle\\max_{1 \\leq i \\leq l}} \\sum_{j=1}^{i} a_j )$$\n\nNow they want to count the sum of maximal prefix sums for each such an array and they are asking you to help. As this sum can be very large, output it modulo $998\\: 244\\: 853$.\n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $m$ ($0 \\le n,m \\le 2\\,000$).\n\n\n-----Output-----\n\nOutput the answer to the problem modulo $998\\: 244\\: 853$.\n\n\n-----Examples-----\nInput\n0 2\n\nOutput\n0\n\nInput\n2 0\n\nOutput\n2\n\nInput\n2 2\n\nOutput\n5\n\nInput\n2000 2000\n\nOutput\n674532367\n\n\n\n-----Note-----\n\nIn the first example the only possible array is [-1,-1], its maximal prefix sum is equal to $0$. \n\nIn the second example the only possible array is [1,1], its maximal prefix sum is equal to $2$. \n\nThere are $6$ possible arrays in the third example:\n\n[1,1,-1,-1], f([1,1,-1,-1]) = 2\n\n[1,-1,1,-1], f([1,-1,1,-1]) = 1\n\n[1,-1,-1,1], f([1,-1,-1,1]) = 1\n\n[-1,1,1,-1], f([-1,1,1,-1]) = 1\n\n[-1,1,-1,1], f([-1,1,-1,1]) = 0\n\n[-1,-1,1,1], f([-1,-1,1,1]) = 0\n\nSo the answer for the third example is $2+1+1+1+0+0 = 5$."} +{"id": "00698", "text": "Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds.\n\nCodesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don't overlap in time. Each round has a unique identifier \u2014 a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater.\n\nSereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed.\n\nSereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers.\n\n\n-----Input-----\n\nThe first line contains two integers: x (1 \u2264 x \u2264 4000) \u2014 the round Sereja is taking part in today, and k (0 \u2264 k < 4000) \u2014 the number of rounds he took part in.\n\nNext k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: \"1 num_2 num_1\" (where num_2 is the identifier of this Div2 round, num_1 is the identifier of the Div1 round). It is guaranteed that num_1 - num_2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: \"2 num\" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x.\n\n\n-----Output-----\n\nPrint in a single line two integers \u2014 the minimum and the maximum number of rounds that Sereja could have missed.\n\n\n-----Examples-----\nInput\n3 2\n2 1\n2 2\n\nOutput\n0 0\nInput\n9 3\n1 2 3\n2 8\n1 4 5\n\nOutput\n2 3\nInput\n10 0\n\nOutput\n5 9\n\n\n-----Note-----\n\nIn the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1 round. \n\nThe maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 rounds."} +{"id": "00699", "text": "Valera had two bags of potatoes, the first of these bags contains x (x \u2265 1) potatoes, and the second \u2014 y (y \u2265 1) potatoes. Valera \u2014 very scattered boy, so the first bag of potatoes (it contains x potatoes) Valera lost. Valera remembers that the total amount of potatoes (x + y) in the two bags, firstly, was not gerater than n, and, secondly, was divisible by k.\n\nHelp Valera to determine how many potatoes could be in the first bag. Print all such possible numbers in ascending order.\n\n\n-----Input-----\n\nThe first line of input contains three integers y, k, n (1 \u2264 y, k, n \u2264 10^9; $\\frac{n}{k}$ \u2264 10^5).\n\n\n-----Output-----\n\nPrint the list of whitespace-separated integers \u2014 all possible values of x in ascending order. You should print each possible value of x exactly once.\n\nIf there are no such values of x print a single integer -1.\n\n\n-----Examples-----\nInput\n10 1 10\n\nOutput\n-1\n\nInput\n10 6 40\n\nOutput\n2 8 14 20 26"} +{"id": "00700", "text": "The stardate is 1977 and the science and art of detecting Death Stars is in its infancy. Princess Heidi has received information about the stars in the nearby solar system from the Rebel spies and now, to help her identify the exact location of the Death Star, she needs to know whether this information is correct. \n\nTwo rebel spies have provided her with the maps of the solar system. Each map is an N \u00d7 N grid, where each cell is either occupied by a star or empty. To see whether the information is correct, Heidi needs to know whether the two maps are of the same solar system, or if possibly one of the spies is actually an Empire double agent, feeding her false information.\n\nUnfortunately, spies may have accidentally rotated a map by 90, 180, or 270 degrees, or flipped it along the vertical or the horizontal axis, before delivering it to Heidi. If Heidi can rotate or flip the maps so that two of them become identical, then those maps are of the same solar system. Otherwise, there are traitors in the Rebel ranks! Help Heidi find out.\n\n\n-----Input-----\n\nThe first line of the input contains one number N (1 \u2264 N \u2264 10) \u2013 the dimension of each map. Next N lines each contain N characters, depicting the first map: 'X' indicates a star, while 'O' indicates an empty quadrant of space. Next N lines each contain N characters, depicting the second map in the same format.\n\n\n-----Output-----\n\nThe only line of output should contain the word Yes if the maps are identical, or No if it is impossible to match them by performing rotations and translations.\n\n\n-----Examples-----\nInput\n4\nXOOO\nXXOO\nOOOO\nXXXX\nXOOO\nXOOO\nXOXO\nXOXX\n\nOutput\nYes\n\nInput\n2\nXX\nOO\nXO\nOX\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first test, you can match the first map to the second map by first flipping the first map along the vertical axis, and then by rotating it 90 degrees clockwise."} +{"id": "00701", "text": "Bizon the Champion isn't just a bison. He also is a favorite of the \"Bizons\" team.\n\nAt a competition the \"Bizons\" got the following problem: \"You are given two distinct words (strings of English letters), s and t. You need to transform word s into word t\". The task looked simple to the guys because they know the suffix data structures well. Bizon Senior loves suffix automaton. By applying it once to a string, he can remove from this string any single character. Bizon Middle knows suffix array well. By applying it once to a string, he can swap any two characters of this string. The guys do not know anything about the suffix tree, but it can help them do much more. \n\nBizon the Champion wonders whether the \"Bizons\" can solve the problem. Perhaps, the solution do not require both data structures. Find out whether the guys can solve the problem and if they can, how do they do it? Can they solve it either only with use of suffix automaton or only with use of suffix array or they need both structures? Note that any structure may be used an unlimited number of times, the structures may be used in any order.\n\n\n-----Input-----\n\nThe first line contains a non-empty word s. The second line contains a non-empty word t. Words s and t are different. Each word consists only of lowercase English letters. Each word contains at most 100 letters.\n\n\n-----Output-----\n\nIn the single line print the answer to the problem. Print \"need tree\" (without the quotes) if word s cannot be transformed into word t even with use of both suffix array and suffix automaton. Print \"automaton\" (without the quotes) if you need only the suffix automaton to solve the problem. Print \"array\" (without the quotes) if you need only the suffix array to solve the problem. Print \"both\" (without the quotes), if you need both data structures to solve the problem.\n\nIt's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton.\n\n\n-----Examples-----\nInput\nautomaton\ntomat\n\nOutput\nautomaton\n\nInput\narray\narary\n\nOutput\narray\n\nInput\nboth\nhot\n\nOutput\nboth\n\nInput\nneed\ntree\n\nOutput\nneed tree\n\n\n\n-----Note-----\n\nIn the third sample you can act like that: first transform \"both\" into \"oth\" by removing the first character using the suffix automaton and then make two swaps of the string using the suffix array and get \"hot\"."} +{"id": "00702", "text": "One day Alice was cleaning up her basement when she noticed something very curious: an infinite set of wooden pieces! Each piece was made of five square tiles, with four tiles adjacent to the fifth center tile: [Image] By the pieces lay a large square wooden board. The board is divided into $n^2$ cells arranged into $n$ rows and $n$ columns. Some of the cells are already occupied by single tiles stuck to it. The remaining cells are free.\n\nAlice started wondering whether she could fill the board completely using the pieces she had found. Of course, each piece has to cover exactly five distinct cells of the board, no two pieces can overlap and every piece should fit in the board entirely, without some parts laying outside the board borders. The board however was too large for Alice to do the tiling by hand. Can you help determine if it's possible to fully tile the board?\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($3 \\leq n \\leq 50$) \u2014 the size of the board.\n\nThe following $n$ lines describe the board. The $i$-th line ($1 \\leq i \\leq n$) contains a single string of length $n$. Its $j$-th character ($1 \\leq j \\leq n$) is equal to \".\" if the cell in the $i$-th row and the $j$-th column is free; it is equal to \"#\" if it's occupied.\n\nYou can assume that the board contains at least one free cell.\n\n\n-----Output-----\n\nOutput YES if the board can be tiled by Alice's pieces, or NO otherwise. You can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n3\n#.#\n...\n#.#\n\nOutput\nYES\n\nInput\n4\n##.#\n#...\n####\n##.#\n\nOutput\nNO\n\nInput\n5\n#.###\n....#\n#....\n###.#\n#####\n\nOutput\nYES\n\nInput\n5\n#.###\n....#\n#....\n....#\n#..##\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe following sketches show the example boards and their tilings if such tilings exist: [Image]"} +{"id": "00703", "text": "You have a nuts and lots of boxes. The boxes have a wonderful feature: if you put x (x \u2265 0) divisors (the spacial bars that can divide a box) to it, you get a box, divided into x + 1 sections.\n\nYou are minimalist. Therefore, on the one hand, you are against dividing some box into more than k sections. On the other hand, you are against putting more than v nuts into some section of the box. What is the minimum number of boxes you have to use if you want to put all the nuts in boxes, and you have b divisors?\n\nPlease note that you need to minimize the number of used boxes, not sections. You do not have to minimize the number of used divisors.\n\n\n-----Input-----\n\nThe first line contains four space-separated integers k, a, b, v (2 \u2264 k \u2264 1000; 1 \u2264 a, b, v \u2264 1000) \u2014 the maximum number of sections in the box, the number of nuts, the number of divisors and the capacity of each section of the box.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n3 10 3 3\n\nOutput\n2\n\nInput\n3 10 1 3\n\nOutput\n3\n\nInput\n100 100 1 1000\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample you can act like this: Put two divisors to the first box. Now the first box has three sections and we can put three nuts into each section. Overall, the first box will have nine nuts. Do not put any divisors into the second box. Thus, the second box has one section for the last nut. \n\nIn the end we've put all the ten nuts into boxes.\n\nThe second sample is different as we have exactly one divisor and we put it to the first box. The next two boxes will have one section each."} +{"id": "00704", "text": "Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the classical epos depicted in Homer's Odysseus... How can he explain to his literature teacher that he isn't going to become a writer? In fact, he is going to become a programmer. So, he would take great pleasure in writing a program, but none \u2014 in writing a composition.\n\nAs Vasya was fishing for a sentence in the dark pond of his imagination, he suddenly wondered: what is the least number of times he should push a key to shift the cursor from one position to another one?\n\nLet's describe his question more formally: to type a text, Vasya is using the text editor. He has already written n lines, the i-th line contains a_{i} characters (including spaces). If some line contains k characters, then this line overall contains (k + 1) positions where the cursor can stand: before some character or after all characters (at the end of the line). Thus, the cursor's position is determined by a pair of integers (r, c), where r is the number of the line and c is the cursor's position in the line (the positions are indexed starting from one from the beginning of the line).\n\nVasya doesn't use the mouse to move the cursor. He uses keys \"Up\", \"Down\", \"Right\" and \"Left\". When he pushes each of these keys, the cursor shifts in the needed direction. Let's assume that before the corresponding key is pressed, the cursor was located in the position (r, c), then Vasya pushed key: \"Up\": if the cursor was located in the first line (r = 1), then it does not move. Otherwise, it moves to the previous line (with number r - 1), to the same position. At that, if the previous line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r - 1; \"Down\": if the cursor was located in the last line (r = n), then it does not move. Otherwise, it moves to the next line (with number r + 1), to the same position. At that, if the next line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r + 1; \"Right\": if the cursor can move to the right in this line (c < a_{r} + 1), then it moves to the right (to position c + 1). Otherwise, it is located at the end of the line and doesn't move anywhere when Vasya presses the \"Right\" key; \"Left\": if the cursor can move to the left in this line (c > 1), then it moves to the left (to position c - 1). Otherwise, it is located at the beginning of the line and doesn't move anywhere when Vasya presses the \"Left\" key.\n\nYou've got the number of lines in the text file and the number of characters, written in each line of this file. Find the least number of times Vasya should push the keys, described above, to shift the cursor from position (r_1, c_1) to position (r_2, c_2).\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 100) \u2014 the number of lines in the file. The second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^5), separated by single spaces. The third line contains four integers r_1, c_1, r_2, c_2 (1 \u2264 r_1, r_2 \u2264 n, 1 \u2264 c_1 \u2264 a_{r}_1 + 1, 1 \u2264 c_2 \u2264 a_{r}_2 + 1).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of times Vasya should push a key to move the cursor from position (r_1, c_1) to position (r_2, c_2).\n\n\n-----Examples-----\nInput\n4\n2 1 6 4\n3 4 4 2\n\nOutput\n3\n\nInput\n4\n10 5 6 4\n1 11 4 2\n\nOutput\n6\n\nInput\n3\n10 1 10\n1 10 1 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample the editor contains four lines. Let's represent the cursor's possible positions in the line as numbers. Letter s represents the cursor's initial position, letter t represents the last one. Then all possible positions of the cursor in the text editor are described by the following table.\n\n123\n\n12\n\n123s567\n\n1t345\n\nOne of the possible answers in the given sample is: \"Left\", \"Down\", \"Left\"."} +{"id": "00705", "text": "Rock... Paper!\n\nAfter Karen have found the deterministic winning (losing?) strategy for rock-paper-scissors, her brother, Koyomi, comes up with a new game as a substitute. The game works as follows.\n\nA positive integer n is decided first. Both Koyomi and Karen independently choose n distinct positive integers, denoted by x_1, x_2, ..., x_{n} and y_1, y_2, ..., y_{n} respectively. They reveal their sequences, and repeat until all of 2n integers become distinct, which is the only final state to be kept and considered.\n\nThen they count the number of ordered pairs (i, j) (1 \u2264 i, j \u2264 n) such that the value x_{i} xor y_{j} equals to one of the 2n integers. Here xor means the bitwise exclusive or operation on two integers, and is denoted by operators ^ and/or xor in most programming languages.\n\nKaren claims a win if the number of such pairs is even, and Koyomi does otherwise. And you're here to help determine the winner of their latest game.\n\n\n-----Input-----\n\nThe first line of input contains a positive integer n (1 \u2264 n \u2264 2 000) \u2014 the length of both sequences.\n\nThe second line contains n space-separated integers x_1, x_2, ..., x_{n} (1 \u2264 x_{i} \u2264 2\u00b710^6) \u2014 the integers finally chosen by Koyomi.\n\nThe third line contains n space-separated integers y_1, y_2, ..., y_{n} (1 \u2264 y_{i} \u2264 2\u00b710^6) \u2014 the integers finally chosen by Karen.\n\nInput guarantees that the given 2n integers are pairwise distinct, that is, no pair (i, j) (1 \u2264 i, j \u2264 n) exists such that one of the following holds: x_{i} = y_{j}; i \u2260 j and x_{i} = x_{j}; i \u2260 j and y_{i} = y_{j}.\n\n\n-----Output-----\n\nOutput one line \u2014 the name of the winner, that is, \"Koyomi\" or \"Karen\" (without quotes). Please be aware of the capitalization.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n4 5 6\n\nOutput\nKaren\n\nInput\n5\n2 4 6 8 10\n9 7 5 3 1\n\nOutput\nKaren\n\n\n\n-----Note-----\n\nIn the first example, there are 6 pairs satisfying the constraint: (1, 1), (1, 2), (2, 1), (2, 3), (3, 2) and (3, 3). Thus, Karen wins since 6 is an even number.\n\nIn the second example, there are 16 such pairs, and Karen wins again."} +{"id": "00706", "text": "Consider a linear function f(x) = Ax + B. Let's define g^{(0)}(x) = x and g^{(}n)(x) = f(g^{(}n - 1)(x)) for n > 0. For the given integer values A, B, n and x find the value of g^{(}n)(x) modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe only line contains four integers A, B, n and x (1 \u2264 A, B, x \u2264 10^9, 1 \u2264 n \u2264 10^18) \u2014 the parameters from the problem statement.\n\nNote that the given value n can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.\n\n\n-----Output-----\n\nPrint the only integer s \u2014 the value g^{(}n)(x) modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n3 4 1 1\n\nOutput\n7\n\nInput\n3 4 2 1\n\nOutput\n25\n\nInput\n3 4 3 1\n\nOutput\n79"} +{"id": "00707", "text": "Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the $i$-th of them will start during the $x_i$-th minute. Ivan doesn't want to skip any of the events, so he has to set his alarm clock in such a way that it rings during minutes $x_1, x_2, \\dots, x_n$, so he will be awake during each of these minutes (note that it does not matter if his alarm clock will ring during any other minute).\n\nIvan can choose two properties for the alarm clock \u2014 the first minute it will ring (let's denote it as $y$) and the interval between two consecutive signals (let's denote it by $p$). After the clock is set, it will ring during minutes $y, y + p, y + 2p, y + 3p$ and so on.\n\nIvan can choose any minute as the first one, but he cannot choose any arbitrary value of $p$. He has to pick it among the given values $p_1, p_2, \\dots, p_m$ (his phone does not support any other options for this setting).\n\nSo Ivan has to choose the first minute $y$ when the alarm clock should start ringing and the interval between two consecutive signals $p_j$ in such a way that it will ring during all given minutes $x_1, x_2, \\dots, x_n$ (and it does not matter if his alarm clock will ring in any other minutes).\n\nYour task is to tell the first minute $y$ and the index $j$ such that if Ivan sets his alarm clock with properties $y$ and $p_j$ it will ring during all given minutes $x_1, x_2, \\dots, x_n$ or say that it is impossible to choose such values of the given properties. If there are multiple answers, you can print any.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($2 \\le n \\le 3 \\cdot 10^5, 1 \\le m \\le 3 \\cdot 10^5$) \u2014 the number of events and the number of possible settings for the interval between signals.\n\nThe second line of the input contains $n$ integers $x_1, x_2, \\dots, x_n$ ($1 \\le x_i \\le 10^{18}$), where $x_i$ is the minute when $i$-th event starts. It is guaranteed that all $x_i$ are given in increasing order (i. e. the condition $x_1 < x_2 < \\dots < x_n$ holds).\n\nThe third line of the input contains $m$ integers $p_1, p_2, \\dots, p_m$ ($1 \\le p_j \\le 10^{18}$), where $p_j$ is the $j$-th option for the interval between two consecutive signals.\n\n\n-----Output-----\n\nIf it's impossible to choose such values $y$ and $j$ so all constraints are satisfied, print \"NO\" in the first line.\n\nOtherwise print \"YES\" in the first line. Then print two integers $y$ ($1 \\le y \\le 10^{18}$) and $j$ ($1 \\le j \\le m$) in the second line, where $y$ is the first minute Ivan's alarm clock should start ringing and $j$ is the index of the option for the interval between two consecutive signals (options are numbered from $1$ to $m$ in the order they are given input). These values should be chosen in such a way that the alarm clock will ring during all given minutes $x_1, x_2, \\dots, x_n$. If there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n3 5\n3 12 18\n2 6 5 3 3\n\nOutput\nYES\n3 4\n\nInput\n4 2\n1 5 17 19\n4 5\n\nOutput\nNO\n\nInput\n4 2\n1 5 17 19\n2 1\n\nOutput\nYES\n1 1"} +{"id": "00708", "text": "Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities.\n\nBear Limak was once in a city a and he wanted to go to a city b. There was no direct connection so he decided to take a long walk, visiting each city exactly once. Formally: There is no road between a and b. There exists a sequence (path) of n distinct cities v_1, v_2, ..., v_{n} that v_1 = a, v_{n} = b and there is a road between v_{i} and v_{i} + 1 for $i \\in \\{1,2, \\ldots, n - 1 \\}$. \n\nOn the other day, the similar thing happened. Limak wanted to travel between a city c and a city d. There is no road between them but there exists a sequence of n distinct cities u_1, u_2, ..., u_{n} that u_1 = c, u_{n} = d and there is a road between u_{i} and u_{i} + 1 for $i \\in \\{1,2, \\ldots, n - 1 \\}$.\n\nAlso, Limak thinks that there are at most k roads in Bearland. He wonders whether he remembers everything correctly.\n\nGiven n, k and four distinct cities a, b, c, d, can you find possible paths (v_1, ..., v_{n}) and (u_1, ..., u_{n}) to satisfy all the given conditions? Find any solution or print -1 if it's impossible.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (4 \u2264 n \u2264 1000, n - 1 \u2264 k \u2264 2n - 2)\u00a0\u2014 the number of cities and the maximum allowed number of roads, respectively.\n\nThe second line contains four distinct integers a, b, c and d (1 \u2264 a, b, c, d \u2264 n).\n\n\n-----Output-----\n\nPrint -1 if it's impossible to satisfy all the given conditions. Otherwise, print two lines with paths descriptions. The first of these two lines should contain n distinct integers v_1, v_2, ..., v_{n} where v_1 = a and v_{n} = b. The second line should contain n distinct integers u_1, u_2, ..., u_{n} where u_1 = c and u_{n} = d.\n\nTwo paths generate at most 2n - 2 roads: (v_1, v_2), (v_2, v_3), ..., (v_{n} - 1, v_{n}), (u_1, u_2), (u_2, u_3), ..., (u_{n} - 1, u_{n}). Your answer will be considered wrong if contains more than k distinct roads or any other condition breaks. Note that (x, y) and (y, x) are the same road.\n\n\n-----Examples-----\nInput\n7 11\n2 4 7 3\n\nOutput\n2 7 1 3 6 5 4\n7 1 5 4 6 2 3\n\nInput\n1000 999\n10 20 30 40\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample test, there should be 7 cities and at most 11 roads. The provided sample solution generates 10 roads, as in the drawing. You can also see a simple path of length n between 2 and 4, and a path between 7 and 3.\n\n [Image]"} +{"id": "00709", "text": "You are a lover of bacteria. You want to raise some bacteria in a box. \n\nInitially, the box is empty. Each morning, you can put any number of bacteria into the box. And each night, every bacterium in the box will split into two bacteria. You hope to see exactly x bacteria in the box at some moment. \n\nWhat is the minimum number of bacteria you need to put into the box across those days?\n\n\n-----Input-----\n\nThe only line containing one integer x (1 \u2264 x \u2264 10^9).\n\n\n-----Output-----\n\nThe only line containing one integer: the answer.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n2\n\nInput\n8\n\nOutput\n1\n\n\n\n-----Note-----\n\nFor the first sample, we can add one bacterium in the box in the first day morning and at the third morning there will be 4 bacteria in the box. Now we put one more resulting 5 in the box. We added 2 bacteria in the process so the answer is 2.\n\nFor the second sample, we can put one in the first morning and in the 4-th morning there will be 8 in the box. So the answer is 1."} +{"id": "00710", "text": "Today in the scientific lyceum of the Kingdom of Kremland, there was a biology lesson. The topic of the lesson was the genomes. Let's call the genome the string \"ACTG\".\n\nMaxim was very boring to sit in class, so the teacher came up with a task for him: on a given string $s$ consisting of uppercase letters and length of at least $4$, you need to find the minimum number of operations that you need to apply, so that the genome appears in it as a substring. For one operation, you can replace any letter in the string $s$ with the next or previous in the alphabet. For example, for the letter \"D\" the previous one will be \"C\", and the next\u00a0\u2014 \"E\". In this problem, we assume that for the letter \"A\", the previous one will be the letter \"Z\", and the next one will be \"B\", and for the letter \"Z\", the previous one is the letter \"Y\", and the next one is the letter \"A\".\n\nHelp Maxim solve the problem that the teacher gave him.\n\nA string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($4 \\leq n \\leq 50$)\u00a0\u2014 the length of the string $s$.\n\nThe second line contains the string $s$, consisting of exactly $n$ uppercase letters of the Latin alphabet.\n\n\n-----Output-----\n\nOutput the minimum number of operations that need to be applied to the string $s$ so that the genome appears as a substring in it.\n\n\n-----Examples-----\nInput\n4\nZCTH\n\nOutput\n2\nInput\n5\nZDATG\n\nOutput\n5\nInput\n6\nAFBAKC\n\nOutput\n16\n\n\n-----Note-----\n\nIn the first example, you should replace the letter \"Z\" with \"A\" for one operation, the letter \"H\"\u00a0\u2014 with the letter \"G\" for one operation. You will get the string \"ACTG\", in which the genome is present as a substring.\n\nIn the second example, we replace the letter \"A\" with \"C\" for two operations, the letter \"D\"\u00a0\u2014 with the letter \"A\" for three operations. You will get the string \"ZACTG\", in which there is a genome."} +{"id": "00711", "text": "You are given positive integers N and M.\nHow many sequences a of length N consisting of positive integers satisfy a_1 \\times a_2 \\times ... \\times a_N = M? Find the count modulo 10^9+7.\nHere, two sequences a' and a'' are considered different when there exists some i such that a_i' \\neq a_i''.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\n\n-----Output-----\nPrint the number of the sequences consisting of positive integers that satisfy the condition, modulo 10^9 + 7.\n\n-----Sample Input-----\n2 6\n\n-----Sample Output-----\n4\n\nFour sequences satisfy the condition: \\{a_1, a_2\\} = \\{1, 6\\}, \\{2, 3\\}, \\{3, 2\\} and \\{6, 1\\}."} +{"id": "00712", "text": "Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. \n\nLet's assume that n people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue enters the escalator with probability p, or the first person in the queue doesn't move with probability (1 - p), paralyzed by his fear of escalators and making the whole queue wait behind him.\n\nFormally speaking, the i-th person in the queue cannot enter the escalator until people with indices from 1 to i - 1 inclusive enter it. In one second only one person can enter the escalator. The escalator is infinite, so if a person enters it, he never leaves it, that is he will be standing on the escalator at any following second. Ilya needs to count the expected value of the number of people standing on the escalator after t seconds. \n\nYour task is to help him solve this complicated task.\n\n\n-----Input-----\n\nThe first line of the input contains three numbers n, p, t (1 \u2264 n, t \u2264 2000, 0 \u2264 p \u2264 1). Numbers n and t are integers, number p is real, given with exactly two digits after the decimal point.\n\n\n-----Output-----\n\nPrint a single real number \u2014 the expected number of people who will be standing on the escalator after t seconds. The absolute or relative error mustn't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n1 0.50 1\n\nOutput\n0.5\n\nInput\n1 0.50 4\n\nOutput\n0.9375\n\nInput\n4 0.20 2\n\nOutput\n0.4"} +{"id": "00713", "text": "Manao has invented a new mathematical term \u2014 a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. \n\nConsider all points (x, y) which satisfy the inequations: 0 \u2264 x \u2264 n; 0 \u2264 y \u2264 m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points.\n\n\n-----Input-----\n\nThe single line contains two space-separated integers n and m (1 \u2264 n, m \u2264 100).\n\n\n-----Output-----\n\nIn the first line print a single integer \u2014 the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers \u2014 the x- and y- coordinates, respectively, of a point from the set.\n\nIf there are several optimal solutions, you may print any of them.\n\n\n-----Examples-----\nInput\n2 2\n\nOutput\n3\n0 1\n1 2\n2 0\n\nInput\n4 3\n\nOutput\n4\n0 3\n2 1\n3 0\n4 2\n\n\n\n-----Note-----\n\nConsider the first sample. The distance between points (0, 1) and (1, 2) equals $\\sqrt{2}$, between (0, 1) and (2, 0) \u2014 $\\sqrt{5}$, between (1, 2) and (2, 0) \u2014 $\\sqrt{5}$. Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution."} +{"id": "00714", "text": "There are n cards (n is even) in the deck. Each card has a positive integer written on it. n / 2 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player. \n\nFind the way to distribute cards such that the sum of values written of the cards will be equal for each player. It is guaranteed that it is always possible.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (2 \u2264 n \u2264 100)\u00a0\u2014 the number of cards in the deck. It is guaranteed that n is even.\n\nThe second line contains the sequence of n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100), where a_{i} is equal to the number written on the i-th card.\n\n\n-----Output-----\n\nPrint n / 2 pairs of integers, the i-th pair denote the cards that should be given to the i-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input.\n\nIt is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n6\n1 5 7 4 4 3\n\nOutput\n1 3\n6 2\n4 5\n\nInput\n4\n10 10 10 10\n\nOutput\n1 2\n3 4\n\n\n\n-----Note-----\n\nIn the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8. \n\nIn the second sample, all values a_{i} are equal. Thus, any distribution is acceptable."} +{"id": "00715", "text": "Once upon a time a child got a test consisting of multiple-choice questions as homework. A multiple-choice question consists of four choices: A, B, C and D. Each choice has a description, and the child should find out the only one that is correct.\n\nFortunately the child knows how to solve such complicated test. The child will follow the algorithm:\n\n If there is some choice whose description at least twice shorter than all other descriptions, or at least twice longer than all other descriptions, then the child thinks the choice is great. If there is exactly one great choice then the child chooses it. Otherwise the child chooses C (the child think it is the luckiest choice). \n\nYou are given a multiple-choice questions, can you predict child's choose?\n\n\n-----Input-----\n\nThe first line starts with \"A.\" (without quotes), then followed the description of choice A. The next three lines contains the descriptions of the other choices in the same format. They are given in order: B, C, D. Please note, that the description goes after prefix \"X.\", so the prefix mustn't be counted in description's length.\n\nEach description is non-empty and consists of at most 100 characters. Each character can be either uppercase English letter or lowercase English letter, or \"_\". \n\n\n-----Output-----\n\nPrint a single line with the child's choice: \"A\", \"B\", \"C\" or \"D\" (without quotes).\n\n\n-----Examples-----\nInput\nA.VFleaKing_is_the_author_of_this_problem\nB.Picks_is_the_author_of_this_problem\nC.Picking_is_the_author_of_this_problem\nD.Ftiasch_is_cute\n\nOutput\nD\n\nInput\nA.ab\nB.abcde\nC.ab\nD.abc\n\nOutput\nC\n\nInput\nA.c\nB.cc\nC.c\nD.c\n\nOutput\nB\n\n\n\n-----Note-----\n\nIn the first sample, the first choice has length 39, the second one has length 35, the third one has length 37, and the last one has length 15. The choice D (length 15) is twice shorter than all other choices', so it is great choice. There is no other great choices so the child will choose D.\n\nIn the second sample, no choice is great, so the child will choose the luckiest choice C.\n\nIn the third sample, the choice B (length 2) is twice longer than all other choices', so it is great choice. There is no other great choices so the child will choose B."} +{"id": "00716", "text": "Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way to get to the olympiad.\n\nVladik knows n airports. All the airports are located on a straight line. Each airport has unique id from 1 to n, Vladik's house is situated next to the airport with id a, and the place of the olympiad is situated next to the airport with id b. It is possible that Vladik's house and the place of the olympiad are located near the same airport. \n\nTo get to the olympiad, Vladik can fly between any pair of airports any number of times, but he has to start his route at the airport a and finish it at the airport b.\n\nEach airport belongs to one of two companies. The cost of flight from the airport i to the airport j is zero if both airports belong to the same company, and |i - j| if they belong to different companies.\n\nPrint the minimum cost Vladik has to pay to get to the olympiad.\n\n\n-----Input-----\n\nThe first line contains three integers n, a, and b (1 \u2264 n \u2264 10^5, 1 \u2264 a, b \u2264 n)\u00a0\u2014 the number of airports, the id of the airport from which Vladik starts his route and the id of the airport which he has to reach. \n\nThe second line contains a string with length n, which consists only of characters 0 and 1. If the i-th character in this string is 0, then i-th airport belongs to first company, otherwise it belongs to the second.\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the minimum cost Vladik has to pay to get to the olympiad.\n\n\n-----Examples-----\nInput\n4 1 4\n1010\n\nOutput\n1\nInput\n5 5 2\n10110\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first example Vladik can fly to the airport 2 at first and pay |1 - 2| = 1 (because the airports belong to different companies), and then fly from the airport 2 to the airport 4 for free (because the airports belong to the same company). So the cost of the whole flight is equal to 1. It's impossible to get to the olympiad for free, so the answer is equal to 1. \n\nIn the second example Vladik can fly directly from the airport 5 to the airport 2, because they belong to the same company."} +{"id": "00717", "text": "It seems that Borya is seriously sick. He is going visit n doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3 and so on). Borya will get the information about his health from the last doctor.\n\nDoctors have a strange working schedule. The doctor i goes to work on the s_{i}-th day and works every d_{i} day. So, he works on days s_{i}, s_{i} + d_{i}, s_{i} + 2d_{i}, ....\n\nThe doctor's appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors?\n\n\n-----Input-----\n\nFirst line contains an integer n \u2014 number of doctors (1 \u2264 n \u2264 1000). \n\nNext n lines contain two numbers s_{i} and d_{i} (1 \u2264 s_{i}, d_{i} \u2264 1000).\n\n\n-----Output-----\n\nOutput a single integer \u2014 the minimum day at which Borya can visit the last doctor.\n\n\n-----Examples-----\nInput\n3\n2 2\n1 2\n2 2\n\nOutput\n4\n\nInput\n2\n10 1\n6 5\n\nOutput\n11\n\n\n\n-----Note-----\n\nIn the first sample case, Borya can visit all doctors on days 2, 3 and 4.\n\nIn the second sample case, Borya can visit all doctors on days 10 and 11."} +{"id": "00718", "text": "Giga Tower is the tallest and deepest building in Cyberland. There are 17 777 777 777 floors, numbered from - 8 888 888 888 to 8 888 888 888. In particular, there is floor 0 between floor - 1 and floor 1. Every day, thousands of tourists come to this place to enjoy the wonderful view. \n\nIn Cyberland, it is believed that the number \"8\" is a lucky number (that's why Giga Tower has 8 888 888 888 floors above the ground), and, an integer is lucky, if and only if its decimal notation contains at least one digit \"8\". For example, 8, - 180, 808 are all lucky while 42, - 10 are not. In the Giga Tower, if you write code at a floor with lucky floor number, good luck will always be with you (Well, this round is #278, also lucky, huh?).\n\nTourist Henry goes to the tower to seek good luck. Now he is at the floor numbered a. He wants to find the minimum positive integer b, such that, if he walks b floors higher, he will arrive at a floor with a lucky number. \n\n\n-----Input-----\n\nThe only line of input contains an integer a ( - 10^9 \u2264 a \u2264 10^9).\n\n\n-----Output-----\n\nPrint the minimum b in a line.\n\n\n-----Examples-----\nInput\n179\n\nOutput\n1\n\nInput\n-1\n\nOutput\n9\n\nInput\n18\n\nOutput\n10\n\n\n\n-----Note-----\n\nFor the first sample, he has to arrive at the floor numbered 180.\n\nFor the second sample, he will arrive at 8.\n\nNote that b should be positive, so the answer for the third sample is 10, not 0."} +{"id": "00719", "text": "We consider a positive integer perfect, if and only if the sum of its digits is exactly $10$. Given a positive integer $k$, your task is to find the $k$-th smallest perfect positive integer.\n\n\n-----Input-----\n\nA single line with a positive integer $k$ ($1 \\leq k \\leq 10\\,000$).\n\n\n-----Output-----\n\nA single number, denoting the $k$-th smallest perfect integer.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n19\n\nInput\n2\n\nOutput\n28\n\n\n\n-----Note-----\n\nThe first perfect integer is $19$ and the second one is $28$."} +{"id": "00720", "text": "You still have partial information about the score during the historic football match. You are given a set of pairs $(a_i, b_i)$, indicating that at some point during the match the score was \"$a_i$: $b_i$\". It is known that if the current score is \u00ab$x$:$y$\u00bb, then after the goal it will change to \"$x+1$:$y$\" or \"$x$:$y+1$\". What is the largest number of times a draw could appear on the scoreboard?\n\nThe pairs \"$a_i$:$b_i$\" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10000$) \u2014 the number of known moments in the match.\n\nEach of the next $n$ lines contains integers $a_i$ and $b_i$ ($0 \\le a_i, b_i \\le 10^9$), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team).\n\nAll moments are given in chronological order, that is, sequences $x_i$ and $y_j$ are non-decreasing. The last score denotes the final result of the match.\n\n\n-----Output-----\n\nPrint the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.\n\n\n-----Examples-----\nInput\n3\n2 0\n3 1\n3 4\n\nOutput\n2\n\nInput\n3\n0 0\n0 0\n0 0\n\nOutput\n1\n\nInput\n1\n5 4\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4."} +{"id": "00721", "text": "Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs a_{i} rubles. Only one person can hang clothes on one hook.\n\nTonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest. \n\nHelp Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.\n\n\n-----Input-----\n\nThe first line contains two integers n and d (1 \u2264 n, d \u2264 100). The next line contains integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100). The third line contains integer m (1 \u2264 m \u2264 100).\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n2 1\n2 1\n2\n\nOutput\n3\n\nInput\n2 1\n2 1\n10\n\nOutput\n-5\n\n\n\n-----Note-----\n\nIn the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.\n\nIn the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 = - 5."} +{"id": "00722", "text": "International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition.\n\nFor example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 and IAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995.\n\nYou are given a list of abbreviations. For each of them determine the year it stands for.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of abbreviations to process. \n\nThen n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits.\n\n\n-----Output-----\n\nFor each abbreviation given in the input, find the year of the corresponding Olympiad.\n\n\n-----Examples-----\nInput\n5\nIAO'15\nIAO'2015\nIAO'1\nIAO'9\nIAO'0\n\nOutput\n2015\n12015\n1991\n1989\n1990\n\nInput\n4\nIAO'9\nIAO'99\nIAO'999\nIAO'9999\n\nOutput\n1989\n1999\n2999\n9999"} +{"id": "00723", "text": "\u041f\u0440\u043e\u0448\u043b\u043e \u043c\u043d\u043e\u0433\u043e \u043b\u0435\u0442, \u0438 \u043d\u0430 \u0432\u0435\u0447\u0435\u0440\u0438\u043d\u043a\u0435 \u0441\u043d\u043e\u0432\u0430 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0438\u0441\u044c n \u0434\u0440\u0443\u0437\u0435\u0439. \u0421 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0432\u0441\u0442\u0440\u0435\u0447\u0438 \u0442\u0435\u0445\u043d\u0438\u043a\u0430 \u0448\u0430\u0433\u043d\u0443\u043b\u0430 \u0434\u0430\u043b\u0435\u043a\u043e \u0432\u043f\u0435\u0440\u0451\u0434, \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0444\u043e\u0442\u043e\u0430\u043f\u043f\u0430\u0440\u0430\u0442\u044b \u0441 \u0430\u0432\u0442\u043e\u0441\u043f\u0443\u0441\u043a\u043e\u043c, \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043e\u0434\u0438\u043d \u0438\u0437 \u0434\u0440\u0443\u0437\u0435\u0439 \u0441\u0442\u043e\u044f\u043b \u0441 \u0444\u043e\u0442\u043e\u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043e\u043c, \u0438, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c, \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0441\u044f \u043d\u0435 \u0437\u0430\u043f\u0435\u0447\u0430\u0442\u043b\u0451\u043d\u043d\u044b\u043c \u043d\u0430 \u0441\u043d\u0438\u043c\u043a\u0435.\n\n\u0423\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u041d\u0430 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u0434\u0440\u0443\u0437\u0435\u0439 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u0438\u0437 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439: \u0432 \u0441\u0442\u043e\u044f\u0447\u0435\u043c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438 i-\u0439 \u0438\u0437 \u043d\u0438\u0445 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u0448\u0438\u0440\u0438\u043d\u044b w_{i} \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439 \u0438 \u0432\u044b\u0441\u043e\u0442\u044b h_{i} \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439. \u041d\u043e \u0442\u0430\u043a\u0436\u0435, \u043f\u0440\u0438 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u043c\u043e\u0436\u0435\u0442 \u043b\u0435\u0447\u044c, \u0438 \u0442\u043e\u0433\u0434\u0430 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u0448\u0438\u0440\u0438\u043d\u044b h_{i} \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439 \u0438 \u0432\u044b\u0441\u043e\u0442\u044b w_{i} \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439.\n\n\u041e\u0431\u0449\u0430\u044f \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440\u044b W \u00d7 H, \u0433\u0434\u0435 W \u2014 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u0430\u044f \u0448\u0438\u0440\u0438\u043d\u0430 \u0432\u0441\u0435\u0445 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u043e\u0432-\u043b\u044e\u0434\u0435\u0439, \u0430 H \u2014 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0438\u0437 \u0432\u044b\u0441\u043e\u0442. \u0414\u0440\u0443\u0437\u044c\u044f \u0445\u043e\u0442\u044f\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0443\u044e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043b\u043e\u0449\u0430\u0434\u044c \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043e\u0431\u0449\u0430\u044f \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u044f. \u041f\u043e\u043c\u043e\u0433\u0438\u0442\u0435 \u0438\u043c \u0432 \u044d\u0442\u043e\u043c.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e n (1 \u2264 n \u2264 1000) \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0440\u0443\u0437\u0435\u0439.\n\n\u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 n \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u043f\u043e \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 w_{i}, h_{i} (1 \u2264 w_{i}, h_{i} \u2264 1000), \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e i-\u043c\u0443 \u0438\u0437 \u0434\u0440\u0443\u0437\u0435\u0439.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0440\u0430\u0432\u043d\u043e\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0439 \u043f\u043b\u043e\u0449\u0430\u0434\u0438 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438, \u0432\u043c\u0435\u0449\u0430\u044e\u0449\u0435\u0439 \u0432\u0441\u0435\u0445 \u0434\u0440\u0443\u0437\u0435\u0439.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3\n10 1\n20 2\n30 3\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n180\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3\n3 1\n2 2\n4 3\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n21\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1\n5 10\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n50"} +{"id": "00724", "text": "We've got no test cases. A big olympiad is coming up. But the problemsetters' number one priority should be adding another problem to the round. \n\nThe diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1, 3, 2, 1} is 2.\n\nDiameter of multiset consisting of one point is 0.\n\nYou are given n points on the line. What is the minimum number of points you have to remove, so that the diameter of the multiset of the remaining points will not exceed d?\n\n\n-----Input-----\n\nThe first line contains two integers n and d (1 \u2264 n \u2264 100, 0 \u2264 d \u2264 100)\u00a0\u2014 the amount of points and the maximum allowed diameter respectively.\n\nThe second line contains n space separated integers (1 \u2264 x_{i} \u2264 100)\u00a0\u2014 the coordinates of the points.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the minimum number of points you have to remove.\n\n\n-----Examples-----\nInput\n3 1\n2 1 4\n\nOutput\n1\n\nInput\n3 0\n7 7 7\n\nOutput\n0\n\nInput\n6 3\n1 3 4 6 9 10\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first test case the optimal strategy is to remove the point with coordinate 4. The remaining points will have coordinates 1 and 2, so the diameter will be equal to 2 - 1 = 1.\n\nIn the second test case the diameter is equal to 0, so its is unnecessary to remove any points. \n\nIn the third test case the optimal strategy is to remove points with coordinates 1, 9 and 10. The remaining points will have coordinates 3, 4 and 6, so the diameter will be equal to 6 - 3 = 3."} +{"id": "00725", "text": "Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead.\n\nAs you may know, the coolest photos are on the film (because you can specify the hashtag #film for such).\n\nBrain took a lot of colourful pictures on colored and black-and-white film. Then he developed and translated it into a digital form. But now, color and black-and-white photos are in one folder, and to sort them, one needs to spend more than one hour!\n\nAs soon as Brain is a photographer not programmer now, he asks you to help him determine for a single photo whether it is colored or black-and-white.\n\nPhoto can be represented as a matrix sized n \u00d7 m, and each element of the matrix stores a symbol indicating corresponding pixel color. There are only 6 colors: 'C' (cyan) 'M' (magenta) 'Y' (yellow) 'W' (white) 'G' (grey) 'B' (black) \n\nThe photo is considered black-and-white if it has only white, black and grey pixels in it. If there are any of cyan, magenta or yellow pixels in the photo then it is considered colored.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 100)\u00a0\u2014 the number of photo pixel matrix rows and columns respectively.\n\nThen n lines describing matrix rows follow. Each of them contains m space-separated characters describing colors of pixels in a row. Each character in the line is one of the 'C', 'M', 'Y', 'W', 'G' or 'B'.\n\n\n-----Output-----\n\nPrint the \"#Black&White\" (without quotes), if the photo is black-and-white and \"#Color\" (without quotes), if it is colored, in the only line.\n\n\n-----Examples-----\nInput\n2 2\nC M\nY Y\n\nOutput\n#Color\nInput\n3 2\nW W\nW W\nB B\n\nOutput\n#Black&White\nInput\n1 1\nW\n\nOutput\n#Black&White"} +{"id": "00726", "text": "Sonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants.\n\nThe country where Sonya lives is an endless line. There is a city in each integer coordinate on this line. She has $n$ hotels, where the $i$-th hotel is located in the city with coordinate $x_i$. Sonya is a smart girl, so she does not open two or more hotels in the same city.\n\nSonya understands that her business needs to be expanded by opening new hotels, so she decides to build one more. She wants to make the minimum distance from this hotel to all others to be equal to $d$. The girl understands that there are many possible locations to construct such a hotel. Thus she wants to know the number of possible coordinates of the cities where she can build a new hotel. \n\nBecause Sonya is lounging in a jacuzzi in one of her hotels, she is asking you to find the number of cities where she can build a new hotel so that the minimum distance from the original $n$ hotels to the new one is equal to $d$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $d$ ($1\\leq n\\leq 100$, $1\\leq d\\leq 10^9$)\u00a0\u2014 the number of Sonya's hotels and the needed minimum distance from a new hotel to all others.\n\nThe second line contains $n$ different integers in strictly increasing order $x_1, x_2, \\ldots, x_n$ ($-10^9\\leq x_i\\leq 10^9$)\u00a0\u2014 coordinates of Sonya's hotels.\n\n\n-----Output-----\n\nPrint the number of cities where Sonya can build a new hotel so that the minimum distance from this hotel to all others is equal to $d$.\n\n\n-----Examples-----\nInput\n4 3\n-3 2 9 16\n\nOutput\n6\n\nInput\n5 2\n4 8 11 18 19\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example, there are $6$ possible cities where Sonya can build a hotel. These cities have coordinates $-6$, $5$, $6$, $12$, $13$, and $19$.\n\nIn the second example, there are $5$ possible cities where Sonya can build a hotel. These cities have coordinates $2$, $6$, $13$, $16$, and $21$."} +{"id": "00727", "text": "Vasya has an array of integers of length n.\n\nVasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2].\n\nCompute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 200 000) \u2014 the length of the array.\n\nThe second line contains a sequence a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 Vasya's array.\n\n\n-----Output-----\n\nPrint the number of operations Vasya should make to remove all elements from the array.\n\n\n-----Examples-----\nInput\n4\n2 5 5 2\n\nOutput\n2\n\nInput\n5\n6 3 4 1 5\n\nOutput\n5\n\nInput\n8\n4 4 4 2 2 100 100 100\n\nOutput\n3\n\nInput\n6\n10 10 50 10 50 50\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty.\n\nIn the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array.\n\nIn the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second \u2014 all integers 100, in the third \u2014 all integers 2.\n\nIn the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50, 10]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that."} +{"id": "00728", "text": "Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland.\n\nThere are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would get a_{i} votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate.\n\nVictory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn't have many candies and wonders - how many citizens does he have to bribe?\n\n\n-----Input-----\n\nThe first line contains single integer n (2 \u2264 n \u2264 100) - number of candidates.\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1000) - number of votes for each candidate. Limak is candidate number 1.\n\nNote that after bribing number of votes for some candidate might be zero or might be greater than 1000.\n\n\n-----Output-----\n\nPrint the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.\n\n\n-----Examples-----\nInput\n5\n5 1 11 2 8\n\nOutput\n4\n\nInput\n4\n1 8 8 8\n\nOutput\n6\n\nInput\n2\n7 6\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9, 0, 8, 2, 8.\n\nIn the second sample Limak will steal 2 votes from each candidate. Situation will be 7, 6, 6, 6.\n\nIn the third sample Limak is a winner without bribing any citizen."} +{"id": "00729", "text": "You are given a string $s$, consisting of $n$ lowercase Latin letters.\n\nA substring of string $s$ is a continuous segment of letters from $s$. For example, \"defor\" is a substring of \"codeforces\" and \"fors\" is not. \n\nThe length of the substring is the number of letters in it.\n\nLet's call some string of length $n$ diverse if and only if there is no letter to appear strictly more than $\\frac n 2$ times. For example, strings \"abc\" and \"iltlml\" are diverse and strings \"aab\" and \"zz\" are not.\n\nYour task is to find any diverse substring of string $s$ or report that there is none. Note that it is not required to maximize or minimize the length of the resulting substring.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 1000$) \u2014 the length of string $s$.\n\nThe second line is the string $s$, consisting of exactly $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nPrint \"NO\" if there is no diverse substring in the string $s$.\n\nOtherwise the first line should contain \"YES\". The second line should contain any diverse substring of string $s$.\n\n\n-----Examples-----\nInput\n10\ncodeforces\n\nOutput\nYES\ncode\n\nInput\n5\naaaaa\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe first example has lots of correct answers. \n\nPlease, restrain yourself from asking if some specific answer is correct for some specific test or not, these questions always lead to \"No comments\" answer."} +{"id": "00730", "text": "The final round of Bayan Programming Contest will be held in Tehran, and the participants will be carried around with a yellow bus. The bus has 34 passenger seats: 4 seats in the last row and 3 seats in remaining rows. [Image] \n\nThe event coordinator has a list of k participants who should be picked up at the airport. When a participant gets on the bus, he will sit in the last row with an empty seat. If there is more than one empty seat in that row, he will take the leftmost one. \n\nIn order to keep track of the people who are on the bus, the event coordinator needs a figure showing which seats are going to be taken by k participants. Your task is to draw the figure representing occupied seats.\n\n\n-----Input-----\n\nThe only line of input contains integer k, (0 \u2264 k \u2264 34), denoting the number of participants.\n\n\n-----Output-----\n\nPrint the figure of a bus with k passengers as described in sample tests. Character '#' denotes an empty seat, while 'O' denotes a taken seat. 'D' is the bus driver and other characters in the output are for the purpose of beautifying the figure. Strictly follow the sample test cases output format. Print exactly six lines. Do not output extra space or other characters.\n\n\n-----Examples-----\nInput\n9\n\nOutput\n+------------------------+\n|O.O.O.#.#.#.#.#.#.#.#.|D|)\n|O.O.O.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+\n\nInput\n20\n\nOutput\n+------------------------+\n|O.O.O.O.O.O.O.#.#.#.#.|D|)\n|O.O.O.O.O.O.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.#.#.#.#.#.|.|)\n+------------------------+"} +{"id": "00731", "text": "We'll define S(n) for positive integer n as follows: the number of the n's digits in the decimal base. For example, S(893) = 3, S(114514) = 6.\n\nYou want to make a consecutive integer sequence starting from number m (m, m + 1, ...). But you need to pay S(n)\u00b7k to add the number n to the sequence.\n\nYou can spend a cost up to w, and you want to make the sequence as long as possible. Write a program that tells sequence's maximum length.\n\n\n-----Input-----\n\nThe first line contains three integers w (1 \u2264 w \u2264 10^16), m (1 \u2264 m \u2264 10^16), k (1 \u2264 k \u2264 10^9).\n\nPlease, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nThe first line should contain a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n9 1 1\n\nOutput\n9\n\nInput\n77 7 7\n\nOutput\n7\n\nInput\n114 5 14\n\nOutput\n6\n\nInput\n1 1 2\n\nOutput\n0"} +{"id": "00732", "text": "Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky.\n\nLet's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 \u2264 x, y \u2264 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y.\n\nPolycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^9) \u2014 Polycarpus's number.\n\n\n-----Output-----\n\nPrint a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky.\n\n\n-----Examples-----\nInput\n10\n\nOutput\n10\n\nInput\n123\n\nOutput\n113\n\n\n\n-----Note-----\n\nIn the first test sample all numbers that do not exceed 10 are undoubtedly lucky.\n\nIn the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky."} +{"id": "00733", "text": "Iahub and his friend Floyd have started painting a wall. Iahub is painting the wall red and Floyd is painting it pink. You can consider the wall being made of a very large number of bricks, numbered 1, 2, 3 and so on. \n\nIahub has the following scheme of painting: he skips x - 1 consecutive bricks, then he paints the x-th one. That is, he'll paint bricks x, 2\u00b7x, 3\u00b7x and so on red. Similarly, Floyd skips y - 1 consecutive bricks, then he paints the y-th one. Hence he'll paint bricks y, 2\u00b7y, 3\u00b7y and so on pink.\n\nAfter painting the wall all day, the boys observed that some bricks are painted both red and pink. Iahub has a lucky number a and Floyd has a lucky number b. Boys wonder how many bricks numbered no less than a and no greater than b are painted both red and pink. This is exactly your task: compute and print the answer to the question. \n\n\n-----Input-----\n\nThe input will have a single line containing four integers in this order: x, y, a, b. (1 \u2264 x, y \u2264 1000, 1 \u2264 a, b \u2264 2\u00b710^9, a \u2264 b).\n\n\n-----Output-----\n\nOutput a single integer \u2014 the number of bricks numbered no less than a and no greater than b that are painted both red and pink.\n\n\n-----Examples-----\nInput\n2 3 6 18\n\nOutput\n3\n\n\n-----Note-----\n\nLet's look at the bricks from a to b (a = 6, b = 18). The bricks colored in red are numbered 6, 8, 10, 12, 14, 16, 18. The bricks colored in pink are numbered 6, 9, 12, 15, 18. The bricks colored in both red and pink are numbered with 6, 12 and 18."} +{"id": "00734", "text": "You came to the exhibition and one exhibit has drawn your attention. It consists of $n$ stacks of blocks, where the $i$-th stack consists of $a_i$ blocks resting on the surface.\n\nThe height of the exhibit is equal to $m$. Consequently, the number of blocks in each stack is less than or equal to $m$.\n\nThere is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.$\\text{Top View}$ \n\nFind the maximum number of blocks you can remove such that the views for both the cameras would not change.\n\nNote, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 100\\,000$, $1 \\le m \\le 10^9$)\u00a0\u2014 the number of stacks and the height of the exhibit.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le m$)\u00a0\u2014 the number of blocks in each stack from left to right.\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the maximum number of blocks that can be removed.\n\n\n-----Examples-----\nInput\n5 6\n3 3 3 3 3\n\nOutput\n10\nInput\n3 5\n1 2 4\n\nOutput\n3\nInput\n5 5\n2 3 1 4 4\n\nOutput\n9\nInput\n1 1000\n548\n\nOutput\n0\nInput\n3 3\n3 1 1\n\nOutput\n1\n\n\n-----Note-----\n\nThe following pictures illustrate the first example and its possible solution.\n\nBlue cells indicate removed blocks. There are $10$ blue cells, so the answer is $10$.[Image]"} +{"id": "00735", "text": "Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.\n\nUnfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 10^5) \u2014 the size of array a.\n\nThe second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 \u2264 a[i] \u2264 10^9).\n\n\n-----Output-----\n\nPrint \"yes\" or \"no\" (without quotes), depending on the answer.\n\nIf your answer is \"yes\", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.\n\n\n-----Examples-----\nInput\n3\n3 2 1\n\nOutput\nyes\n1 3\n\nInput\n4\n2 1 3 4\n\nOutput\nyes\n1 2\n\nInput\n4\n3 1 2 4\n\nOutput\nno\n\nInput\n2\n1 2\n\nOutput\nyes\n1 1\n\n\n\n-----Note-----\n\nSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.\n\nSample 3. No segment can be reversed such that the array will be sorted.\n\nDefinitions\n\nA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].\n\nIf you have an array a of size n and you reverse its segment [l, r], the array will become:\n\na[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n]."} +{"id": "00736", "text": "Dreamoon wants to climb up a stair of n steps. He can climb 1 or 2 steps at each move. Dreamoon wants the number of moves to be a multiple of an integer m. \n\nWhat is the minimal number of moves making him climb to the top of the stairs that satisfies his condition?\n\n\n-----Input-----\n\nThe single line contains two space separated integers n, m (0 < n \u2264 10000, 1 < m \u2264 10).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimal number of moves being a multiple of m. If there is no way he can climb satisfying condition print - 1 instead.\n\n\n-----Examples-----\nInput\n10 2\n\nOutput\n6\n\nInput\n3 5\n\nOutput\n-1\n\n\n\n-----Note-----\n\nFor the first sample, Dreamoon could climb in 6 moves with following sequence of steps: {2, 2, 2, 2, 1, 1}.\n\nFor the second sample, there are only three valid sequence of steps {2, 1}, {1, 2}, {1, 1, 1} with 2, 2, and 3 steps respectively. All these numbers are not multiples of 5."} +{"id": "00737", "text": "Your security guard friend recently got a new job at a new security company. The company requires him to patrol an area of the city encompassing exactly N city blocks, but they let him choose which blocks. That is, your friend must walk the perimeter of a region whose area is exactly N blocks. Your friend is quite lazy and would like your help to find the shortest possible route that meets the requirements. The city is laid out in a square grid pattern, and is large enough that for the sake of the problem it can be considered infinite.\n\n\n-----Input-----\n\nInput will consist of a single integer N (1 \u2264 N \u2264 10^6), the number of city blocks that must be enclosed by the route.\n\n\n-----Output-----\n\nPrint the minimum perimeter that can be achieved.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n8\n\nInput\n11\n\nOutput\n14\n\nInput\n22\n\nOutput\n20\n\n\n\n-----Note-----\n\nHere are some possible shapes for the examples:\n\n[Image]"} +{"id": "00738", "text": "Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \\leq B \\leq C \\leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \\leq x \\leq B \\leq y \\leq C \\leq z \\leq D$ holds?\n\nYuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property.\n\nThe triangle is called non-degenerate if and only if its vertices are not collinear.\n\n\n-----Input-----\n\nThe first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \\leq A \\leq B \\leq C \\leq D \\leq 5 \\cdot 10^5$)\u00a0\u2014 Yuri's favourite numbers.\n\n\n-----Output-----\n\nPrint the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \\leq x \\leq B \\leq y \\leq C \\leq z \\leq D$ holds.\n\n\n-----Examples-----\nInput\n1 2 3 4\n\nOutput\n4\n\nInput\n1 2 2 5\n\nOutput\n3\n\nInput\n500000 500000 500000 500000\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$.\n\nIn the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$.\n\nIn the third example Yuri can make up only one equilateral triangle with sides equal to $5 \\cdot 10^5$."} +{"id": "00739", "text": " You are given Q tuples of integers (L_i, A_i, B_i, M_i). For each tuple, answer the following question. \nThere is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.\nThe initial term is A, and the common difference is B. That is, s_i = A + B \\times i holds.\nConsider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq L, A, B < 10^{18}\n - 2 \\leq M \\leq 10^9\n - All terms in the arithmetic progression are less than 10^{18}.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nL A B M\n\n-----Output-----\nPrint the remainder when the integer obtained by concatenating the terms is divided by M.\n\n-----Sample Input-----\n5 3 4 10007\n\n-----Sample Output-----\n5563\n\nOur arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563."} +{"id": "00740", "text": "You have k pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has n_1 washing machines, n_2 drying machines and n_3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it before it is dried. Moreover, after a piece of laundry is washed, it needs to be immediately moved into a drying machine, and after it is dried, it needs to be immediately moved into a folding machine.\n\nIt takes t_1 minutes to wash one piece of laundry in a washing machine, t_2 minutes to dry it in a drying machine, and t_3 minutes to fold it in a folding machine. Find the smallest number of minutes that is enough to wash, dry and fold all the laundry you have.\n\n\n-----Input-----\n\nThe only line of the input contains seven integers: k, n_1, n_2, n_3, t_1, t_2, t_3 (1 \u2264 k \u2264 10^4;\u00a01 \u2264 n_1, n_2, n_3, t_1, t_2, t_3 \u2264 1000).\n\n\n-----Output-----\n\nPrint one integer \u2014 smallest number of minutes to do all your laundry.\n\n\n-----Examples-----\nInput\n1 1 1 1 5 5 5\n\nOutput\n15\n\nInput\n8 4 3 2 10 5 2\n\nOutput\n32\n\n\n\n-----Note-----\n\nIn the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it.\n\nIn the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried, there will be no folding machine available, so you have to wait, and start washing third piece at moment 2. Similarly, you can't start washing next piece until moment 5, since otherwise there will be no dryer available, when it is washed. Start time for each of the eight pieces of laundry is 0, 0, 2, 5, 10, 10, 12 and 15 minutes respectively. The last piece of laundry will be ready after 15 + 10 + 5 + 2 = 32 minutes."} +{"id": "00741", "text": "Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment $0$ and turn power off at moment $M$. Moreover, the lamp allows you to set a program of switching its state (states are \"lights on\" and \"lights off\"). Unfortunately, some program is already installed into the lamp.\n\nThe lamp allows only good programs. Good program can be represented as a non-empty array $a$, where $0 < a_1 < a_2 < \\dots < a_{|a|} < M$. All $a_i$ must be integers. Of course, preinstalled program is a good program.\n\nThe lamp follows program $a$ in next manner: at moment $0$ turns power and light on. Then at moment $a_i$ the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment $1$ and then do nothing, the total time when the lamp is lit will be $1$. Finally, at moment $M$ the lamp is turning its power off regardless of its state.\n\nSince you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program $a$, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of $a$, or even at the begining or at the end of $a$.\n\nFind such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from $x$ till moment $y$, then its lit for $y - x$ units of time. Segments of time when the lamp is lit are summed up.\n\n\n-----Input-----\n\nFirst line contains two space separated integers $n$ and $M$ ($1 \\le n \\le 10^5$, $2 \\le M \\le 10^9$) \u2014 the length of program $a$ and the moment when power turns off.\n\nSecond line contains $n$ space separated integers $a_1, a_2, \\dots, a_n$ ($0 < a_1 < a_2 < \\dots < a_n < M$) \u2014 initially installed program $a$.\n\n\n-----Output-----\n\nPrint the only integer \u2014 maximum possible total time when the lamp is lit.\n\n\n-----Examples-----\nInput\n3 10\n4 6 7\n\nOutput\n8\n\nInput\n2 12\n1 10\n\nOutput\n9\n\nInput\n2 7\n3 4\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first example, one of possible optimal solutions is to insert value $x = 3$ before $a_1$, so program will be $[3, 4, 6, 7]$ and time of lamp being lit equals $(3 - 0) + (6 - 4) + (10 - 7) = 8$. Other possible solution is to insert $x = 5$ in appropriate place.\n\nIn the second example, there is only one optimal solution: to insert $x = 2$ between $a_1$ and $a_2$. Program will become $[1, 2, 10]$, and answer will be $(1 - 0) + (10 - 2) = 9$.\n\nIn the third example, optimal answer is to leave program untouched, so answer will be $(3 - 0) + (7 - 4) = 6$."} +{"id": "00742", "text": "You are given a sequence $b_1, b_2, \\ldots, b_n$. Find the lexicographically minimal permutation $a_1, a_2, \\ldots, a_{2n}$ such that $b_i = \\min(a_{2i-1}, a_{2i})$, or determine that it is impossible.\n\n\n-----Input-----\n\nEach test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 100$).\n\nThe first line of each test case consists of one integer $n$\u00a0\u2014 the number of elements in the sequence $b$ ($1 \\le n \\le 100$).\n\nThe second line of each test case consists of $n$ different integers $b_1, \\ldots, b_n$\u00a0\u2014 elements of the sequence $b$ ($1 \\le b_i \\le 2n$).\n\nIt is guaranteed that the sum of $n$ by all test cases doesn't exceed $100$.\n\n\n-----Output-----\n\nFor each test case, if there is no appropriate permutation, print one number $-1$.\n\nOtherwise, print $2n$ integers $a_1, \\ldots, a_{2n}$\u00a0\u2014 required lexicographically minimal permutation of numbers from $1$ to $2n$.\n\n\n-----Example-----\nInput\n5\n1\n1\n2\n4 1\n3\n4 1 3\n4\n2 3 4 5\n5\n1 5 7 2 8\n\nOutput\n1 2 \n-1\n4 5 1 2 3 6 \n-1\n1 3 5 6 7 9 2 4 8 10"} +{"id": "00743", "text": "Fox Ciel is playing a game with numbers now. \n\nCiel has n positive integers: x_1, x_2, ..., x_{n}. She can do the following operation as many times as needed: select two different indexes i and j such that x_{i} > x_{j} hold, and then apply assignment x_{i} = x_{i} - x_{j}. The goal is to make the sum of all numbers as small as possible.\n\nPlease help Ciel to find this minimal sum.\n\n\n-----Input-----\n\nThe first line contains an integer n (2 \u2264 n \u2264 100). Then the second line contains n integers: x_1, x_2, ..., x_{n} (1 \u2264 x_{i} \u2264 100).\n\n\n-----Output-----\n\nOutput a single integer \u2014 the required minimal sum.\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n2\n\nInput\n3\n2 4 6\n\nOutput\n6\n\nInput\n2\n12 18\n\nOutput\n12\n\nInput\n5\n45 12 27 30 18\n\nOutput\n15\n\n\n\n-----Note-----\n\nIn the first example the optimal way is to do the assignment: x_2 = x_2 - x_1.\n\nIn the second example the optimal sequence of operations is: x_3 = x_3 - x_2, x_2 = x_2 - x_1."} +{"id": "00744", "text": "As you may know, MemSQL has American offices in both San Francisco and Seattle. Being a manager in the company, you travel a lot between the two cities, always by plane.\n\nYou prefer flying from Seattle to San Francisco than in the other direction, because it's warmer in San Francisco. You are so busy that you don't remember the number of flights you have made in either direction. However, for each of the last n days you know whether you were in San Francisco office or in Seattle office. You always fly at nights, so you never were at both offices on the same day. Given this information, determine if you flew more times from Seattle to San Francisco during the last n days, or not.\n\n\n-----Input-----\n\nThe first line of input contains single integer n (2 \u2264 n \u2264 100)\u00a0\u2014 the number of days.\n\nThe second line contains a string of length n consisting of only capital 'S' and 'F' letters. If the i-th letter is 'S', then you were in Seattle office on that day. Otherwise you were in San Francisco. The days are given in chronological order, i.e. today is the last day in this sequence.\n\n\n-----Output-----\n\nPrint \"YES\" if you flew more times from Seattle to San Francisco, and \"NO\" otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n4\nFSSF\n\nOutput\nNO\n\nInput\n2\nSF\n\nOutput\nYES\n\nInput\n10\nFFFFFFFFFF\n\nOutput\nNO\n\nInput\n10\nSSFFSFFSFF\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example you were initially at San Francisco, then flew to Seattle, were there for two days and returned to San Francisco. You made one flight in each direction, so the answer is \"NO\".\n\nIn the second example you just flew from Seattle to San Francisco, so the answer is \"YES\".\n\nIn the third example you stayed the whole period in San Francisco, so the answer is \"NO\".\n\nIn the fourth example if you replace 'S' with ones, and 'F' with zeros, you'll get the first few digits of \u03c0 in binary representation. Not very useful information though."} +{"id": "00745", "text": "Calculate the number of ways to place $n$ rooks on $n \\times n$ chessboard so that both following conditions are met:\n\n each empty cell is under attack; exactly $k$ pairs of rooks attack each other. \n\nAn empty cell is under attack if there is at least one rook in the same row or at least one rook in the same column. Two rooks attack each other if they share the same row or column, and there are no other rooks between them. For example, there are only two pairs of rooks that attack each other in the following picture:\n\n [Image] One of the ways to place the rooks for $n = 3$ and $k = 2$ \n\nTwo ways to place the rooks are considered different if there exists at least one cell which is empty in one of the ways but contains a rook in another way.\n\nThe answer might be large, so print it modulo $998244353$.\n\n\n-----Input-----\n\nThe only line of the input contains two integers $n$ and $k$ ($1 \\le n \\le 200000$; $0 \\le k \\le \\frac{n(n - 1)}{2}$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of ways to place the rooks, taken modulo $998244353$.\n\n\n-----Examples-----\nInput\n3 2\n\nOutput\n6\n\nInput\n3 3\n\nOutput\n0\n\nInput\n4 0\n\nOutput\n24\n\nInput\n1337 42\n\nOutput\n807905441"} +{"id": "00746", "text": "Vasiliy lives at point (a, b) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested n available Beru-taxi nearby. The i-th taxi is located at point (x_{i}, y_{i}) and moves with a speed v_{i}. \n\nConsider that each of n drivers will move directly to Vasiliy and with a maximum possible speed. Compute the minimum time when Vasiliy will get in any of Beru-taxi cars.\n\n\n-----Input-----\n\nThe first line of the input contains two integers a and b ( - 100 \u2264 a, b \u2264 100)\u00a0\u2014 coordinates of Vasiliy's home.\n\nThe second line contains a single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of available Beru-taxi cars nearby. \n\nThe i-th of the following n lines contains three integers x_{i}, y_{i} and v_{i} ( - 100 \u2264 x_{i}, y_{i} \u2264 100, 1 \u2264 v_{i} \u2264 100)\u00a0\u2014 the coordinates of the i-th car and its speed.\n\nIt's allowed that several cars are located at the same point. Also, cars may be located at exactly the same point where Vasiliy lives.\n\n\n-----Output-----\n\nPrint a single real value\u00a0\u2014 the minimum time Vasiliy needs to get in any of the Beru-taxi cars. You answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. \n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n0 0\n2\n2 0 1\n0 2 2\n\nOutput\n1.00000000000000000000\nInput\n1 3\n3\n3 3 2\n-2 3 6\n-2 7 10\n\nOutput\n0.50000000000000000000\n\n\n-----Note-----\n\nIn the first sample, first taxi will get to Vasiliy in time 2, and second will do this in time 1, therefore 1 is the answer.\n\nIn the second sample, cars 2 and 3 will arrive simultaneously."} +{"id": "00747", "text": "The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is the hero of today's problem. [Image] \n\nOne day, Om Nom visited his friend Evan. Evan has n candies of two types (fruit drops and caramel drops), the i-th candy hangs at the height of h_{i} centimeters above the floor of the house, its mass is m_{i}. Om Nom wants to eat as many candies as possible. At the beginning Om Nom can make at most x centimeter high jumps. When Om Nom eats a candy of mass y, he gets stronger and the height of his jump increases by y centimeters.\n\nWhat maximum number of candies can Om Nom eat if he never eats two candies of the same type in a row (Om Nom finds it too boring)?\n\n\n-----Input-----\n\nThe first line contains two integers, n and x (1 \u2264 n, x \u2264 2000) \u2014 the number of sweets Evan has and the initial height of Om Nom's jump. \n\nEach of the following n lines contains three integers t_{i}, h_{i}, m_{i} (0 \u2264 t_{i} \u2264 1;\u00a01 \u2264 h_{i}, m_{i} \u2264 2000) \u2014 the type, height and the mass of the i-th candy. If number t_{i} equals 0, then the current candy is a caramel drop, otherwise it is a fruit drop.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of candies Om Nom can eat.\n\n\n-----Examples-----\nInput\n5 3\n0 2 4\n1 3 1\n0 8 3\n0 20 10\n1 5 5\n\nOutput\n4\n\n\n\n-----Note-----\n\nOne of the possible ways to eat 4 candies is to eat them in the order: 1, 5, 3, 2. Let's assume the following scenario: Initially, the height of Om Nom's jump equals 3. He can reach candies 1 and 2. Let's assume that he eats candy 1. As the mass of this candy equals 4, the height of his jump will rise to 3 + 4 = 7. Now Om Nom can reach candies 2 and 5. Let's assume that he eats candy 5. Then the height of his jump will be 7 + 5 = 12. At this moment, Om Nom can reach two candies, 2 and 3. He won't eat candy 2 as its type matches the type of the previously eaten candy. Om Nom eats candy 3, the height of his jump is 12 + 3 = 15. Om Nom eats candy 2, the height of his jump is 15 + 1 = 16. He cannot reach candy 4."} +{"id": "00748", "text": "Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held: a < b < c; a divides b, b divides c. \n\nNaturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has $\\frac{n}{3}$ groups of three.\n\nHelp Xenia, find the required partition or else say that it doesn't exist.\n\n\n-----Input-----\n\nThe first line contains integer n (3 \u2264 n \u2264 99999) \u2014 the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7.\n\nIt is guaranteed that n is divisible by 3.\n\n\n-----Output-----\n\nIf the required partition exists, print $\\frac{n}{3}$ groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them.\n\nIf there is no solution, print -1.\n\n\n-----Examples-----\nInput\n6\n1 1 1 2 2 2\n\nOutput\n-1\n\nInput\n6\n2 2 1 1 4 6\n\nOutput\n1 2 4\n1 2 6"} +{"id": "00749", "text": "You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least k contains this character c.\n\nYou have to find minimum k such that there exists at least one k-dominant character.\n\n\n-----Input-----\n\nThe first line contains string s consisting of lowercase Latin letters (1 \u2264 |s| \u2264 100000).\n\n\n-----Output-----\n\nPrint one number \u2014 the minimum value of k such that there exists at least one k-dominant character.\n\n\n-----Examples-----\nInput\nabacaba\n\nOutput\n2\n\nInput\nzzzzz\n\nOutput\n1\n\nInput\nabcde\n\nOutput\n3"} +{"id": "00750", "text": "Petya is having a party soon, and he has decided to invite his $n$ friends.\n\nHe wants to make invitations in the form of origami. For each invitation, he needs two red sheets, five green sheets, and eight blue sheets. The store sells an infinite number of notebooks of each color, but each notebook consists of only one color with $k$ sheets. That is, each notebook contains $k$ sheets of either red, green, or blue.\n\nFind the minimum number of notebooks that Petya needs to buy to invite all $n$ of his friends.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1\\leq n, k\\leq 10^8$)\u00a0\u2014 the number of Petya's friends and the number of sheets in each notebook respectively.\n\n\n-----Output-----\n\nPrint one number\u00a0\u2014 the minimum number of notebooks that Petya needs to buy.\n\n\n-----Examples-----\nInput\n3 5\n\nOutput\n10\n\nInput\n15 6\n\nOutput\n38\n\n\n\n-----Note-----\n\nIn the first example, we need $2$ red notebooks, $3$ green notebooks, and $5$ blue notebooks.\n\nIn the second example, we need $5$ red notebooks, $13$ green notebooks, and $20$ blue notebooks."} +{"id": "00751", "text": "It's that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of people. People rarely go to the dacha on their own, it's usually a group, so the people stand in queue by groups.\n\nThe bus stop queue has n groups of people. The i-th group from the beginning has a_{i} people. Every 30 minutes an empty bus arrives at the bus stop, it can carry at most m people. Naturally, the people from the first group enter the bus first. Then go the people from the second group and so on. Note that the order of groups in the queue never changes. Moreover, if some group cannot fit all of its members into the current bus, it waits for the next bus together with other groups standing after it in the queue.\n\nYour task is to determine how many buses is needed to transport all n groups to the dacha countryside.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 100). The next line contains n integers: a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 m).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of buses that is needed to transport all n groups to the dacha countryside.\n\n\n-----Examples-----\nInput\n4 3\n2 3 2 1\n\nOutput\n3\n\nInput\n3 4\n1 2 1\n\nOutput\n1"} +{"id": "00752", "text": "Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.\n\nThe valid sizes of T-shirts are either \"M\" or from $0$ to $3$ \"X\" followed by \"S\" or \"L\". For example, sizes \"M\", \"XXS\", \"L\", \"XXXL\" are valid and \"XM\", \"Z\", \"XXXXL\" are not.\n\nThere are $n$ winners to the cup for both the previous year and the current year. Ksenia has a list with the T-shirt sizes printed for the last year cup and is yet to send the new list to the printing office. \n\nOrganizers want to distribute the prizes as soon as possible, so now Ksenia is required not to write the whole list from the scratch but just make some changes to the list of the previous year. In one second she can choose arbitrary position in any word and replace its character with some uppercase Latin letter. Ksenia can't remove or add letters in any of the words.\n\nWhat is the minimal number of seconds Ksenia is required to spend to change the last year list to the current one?\n\nThe lists are unordered. That means, two lists are considered equal if and only if the number of occurrences of any string is the same in both lists.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the number of T-shirts.\n\nThe $i$-th of the next $n$ lines contains $a_i$ \u2014 the size of the $i$-th T-shirt of the list for the previous year.\n\nThe $i$-th of the next $n$ lines contains $b_i$ \u2014 the size of the $i$-th T-shirt of the list for the current year.\n\nIt is guaranteed that all the sizes in the input are valid. It is also guaranteed that Ksenia can produce list $b$ from the list $a$.\n\n\n-----Output-----\n\nPrint the minimal number of seconds Ksenia is required to spend to change the last year list to the current one. If the lists are already equal, print 0.\n\n\n-----Examples-----\nInput\n3\nXS\nXS\nM\nXL\nS\nXS\n\nOutput\n2\n\nInput\n2\nXXXL\nXXL\nXXL\nXXXS\n\nOutput\n1\n\nInput\n2\nM\nXS\nXS\nM\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example Ksenia can replace \"M\" with \"S\" and \"S\" in one of the occurrences of \"XS\" with \"L\".\n\nIn the second example Ksenia should replace \"L\" in \"XXXL\" with \"S\".\n\nIn the third example lists are equal."} +{"id": "00753", "text": "Manao has a monitor. The screen of the monitor has horizontal to vertical length ratio a:b. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratio c:d. Manao adjusts the view in such a way that the movie preserves the original frame ratio, but also occupies as much space on the screen as possible and fits within it completely. Thus, he may have to zoom the movie in or out, but Manao will always change the frame proportionally in both dimensions.\n\nCalculate the ratio of empty screen (the part of the screen not occupied by the movie) to the total screen size. Print the answer as an irreducible fraction p / q.\n\n\n-----Input-----\n\nA single line contains four space-separated integers a, b, c, d (1 \u2264 a, b, c, d \u2264 1000).\n\n\n-----Output-----\n\nPrint the answer to the problem as \"p/q\", where p is a non-negative integer, q is a positive integer and numbers p and q don't have a common divisor larger than 1.\n\n\n-----Examples-----\nInput\n1 1 3 2\n\nOutput\n1/3\n\nInput\n4 3 2 2\n\nOutput\n1/4\n\n\n\n-----Note-----\n\nSample 1. Manao's monitor has a square screen. The movie has 3:2 horizontal to vertical length ratio. Obviously, the movie occupies most of the screen if the width of the picture coincides with the width of the screen. In this case, only 2/3 of the monitor will project the movie in the horizontal dimension: [Image]\n\nSample 2. This time the monitor's width is 4/3 times larger than its height and the movie's frame is square. In this case, the picture must take up the whole monitor in the vertical dimension and only 3/4 in the horizontal dimension: [Image]"} +{"id": "00754", "text": "There are n stones on the table in a row, each of them can be red, green or blue. Count the minimum number of stones to take from the table so that any two neighboring stones had different colors. Stones in a row are considered neighboring if there are no other stones between them.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 50) \u2014 the number of stones on the table. \n\nThe next line contains string s, which represents the colors of the stones. We'll consider the stones in the row numbered from 1 to n from left to right. Then the i-th character s equals \"R\", if the i-th stone is red, \"G\", if it's green and \"B\", if it's blue.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n3\nRRG\n\nOutput\n1\n\nInput\n5\nRRRRR\n\nOutput\n4\n\nInput\n4\nBRBG\n\nOutput\n0"} +{"id": "00755", "text": "An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.\n\n\n-----Input-----\n\nThe first line of the input contains an integer x (1 \u2264 x \u2264 1 000 000)\u00a0\u2014 The coordinate of the friend's house.\n\n\n-----Output-----\n\nPrint the minimum number of steps that elephant needs to make to get from point 0 to point x.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n1\n\nInput\n12\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample the elephant needs to make one step of length 5 to reach the point x.\n\nIn the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves."} +{"id": "00756", "text": "Bear Limak likes watching sports on TV. He is going to watch a game today. The game lasts 90 minutes and there are no breaks.\n\nEach minute can be either interesting or boring. If 15 consecutive minutes are boring then Limak immediately turns TV off.\n\nYou know that there will be n interesting minutes t_1, t_2, ..., t_{n}. Your task is to calculate for how many minutes Limak will watch the game.\n\n\n-----Input-----\n\nThe first line of the input contains one integer n (1 \u2264 n \u2264 90)\u00a0\u2014 the number of interesting minutes.\n\nThe second line contains n integers t_1, t_2, ..., t_{n} (1 \u2264 t_1 < t_2 < ... t_{n} \u2264 90), given in the increasing order.\n\n\n-----Output-----\n\nPrint the number of minutes Limak will watch the game.\n\n\n-----Examples-----\nInput\n3\n7 20 88\n\nOutput\n35\n\nInput\n9\n16 20 30 40 50 60 70 80 90\n\nOutput\n15\n\nInput\n9\n15 20 30 40 50 60 70 80 90\n\nOutput\n90\n\n\n\n-----Note-----\n\nIn the first sample, minutes 21, 22, ..., 35 are all boring and thus Limak will turn TV off immediately after the 35-th minute. So, he would watch the game for 35 minutes.\n\nIn the second sample, the first 15 minutes are boring.\n\nIn the third sample, there are no consecutive 15 boring minutes. So, Limak will watch the whole game."} +{"id": "00757", "text": "Vasya has got many devices that work on electricity. He's got n supply-line filters to plug the devices, the i-th supply-line filter has a_{i} sockets.\n\nOverall Vasya has got m devices and k electrical sockets in his flat, he can plug the devices or supply-line filters directly. Of course, he can plug the supply-line filter to any other supply-line filter. The device (or the supply-line filter) is considered plugged to electricity if it is either plugged to one of k electrical sockets, or if it is plugged to some supply-line filter that is in turn plugged to electricity. \n\nWhat minimum number of supply-line filters from the given set will Vasya need to plug all the devices he has to electricity? Note that all devices and supply-line filters take one socket for plugging and that he can use one socket to plug either one device or one supply-line filter.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, k (1 \u2264 n, m, k \u2264 50) \u2014 the number of supply-line filters, the number of devices and the number of sockets that he can plug to directly, correspondingly. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 50) \u2014 number a_{i} stands for the number of sockets on the i-th supply-line filter.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum number of supply-line filters that is needed to plug all the devices to electricity. If it is impossible to plug all the devices even using all the supply-line filters, print -1.\n\n\n-----Examples-----\nInput\n3 5 3\n3 1 2\n\nOutput\n1\n\nInput\n4 7 2\n3 3 2 4\n\nOutput\n2\n\nInput\n5 5 1\n1 3 1 2 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first test case he can plug the first supply-line filter directly to electricity. After he plug it, he get 5 (3 on the supply-line filter and 2 remaining sockets for direct plugging) available sockets to plug. Thus, one filter is enough to plug 5 devices.\n\nOne of the optimal ways in the second test sample is to plug the second supply-line filter directly and plug the fourth supply-line filter to one of the sockets in the second supply-line filter. Thus, he gets exactly 7 sockets, available to plug: one to plug to the electricity directly, 2 on the second supply-line filter, 4 on the fourth supply-line filter. There's no way he can plug 7 devices if he use one supply-line filter."} +{"id": "00758", "text": "User ainta has a stack of n red and blue balls. He can apply a certain operation which changes the colors of the balls inside the stack.\n\n While the top ball inside the stack is red, pop the ball from the top of the stack. Then replace the blue ball on the top with a red ball. And finally push some blue balls to the stack until the stack has total of n balls inside. \u00a0\n\nIf there are no blue balls inside the stack, ainta can't apply this operation. Given the initial state of the stack, ainta wants to know the maximum number of operations he can repeatedly apply.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 50) \u2014 the number of balls inside the stack.\n\nThe second line contains a string s (|s| = n) describing the initial state of the stack. The i-th character of the string s denotes the color of the i-th ball (we'll number the balls from top to bottom of the stack). If the character is \"R\", the color is red. If the character is \"B\", the color is blue.\n\n\n-----Output-----\n\nPrint the maximum number of operations ainta can repeatedly apply.\n\nPlease, do not write the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n3\nRBR\n\nOutput\n2\n\nInput\n4\nRBBR\n\nOutput\n6\n\nInput\n5\nRBBRR\n\nOutput\n6\n\n\n\n-----Note-----\n\nThe first example is depicted below.\n\nThe explanation how user ainta applies the first operation. He pops out one red ball, changes the color of the ball in the middle from blue to red, and pushes one blue ball.\n\n [Image] \n\nThe explanation how user ainta applies the second operation. He will not pop out red balls, he simply changes the color of the ball on the top from blue to red.\n\n [Image] \n\nFrom now on, ainta can't apply any operation because there are no blue balls inside the stack. ainta applied two operations, so the answer is 2.\n\nThe second example is depicted below. The blue arrow denotes a single operation.\n\n [Image]"} +{"id": "00759", "text": "After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.\n\nAt any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.\n\nDetermine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.\n\n\n-----Input-----\n\nThe first line contains two integers hh and mm (00 \u2264 hh \u2264 23, 00 \u2264 mm \u2264 59) \u2014 the time of Andrew's awakening.\n\nThe second line contains four integers H, D, C and N (1 \u2264 H \u2264 10^5, 1 \u2264 D, C, N \u2264 10^2).\n\n\n-----Output-----\n\nOutput the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10^{ - 4}.\n\nFormally, 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^{-4}$.\n\n\n-----Examples-----\nInput\n19 00\n255 1 100 1\n\nOutput\n25200.0000\n\nInput\n17 41\n1000 6 15 11\n\nOutput\n1365.0000\n\n\n\n-----Note-----\n\nIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.\n\nIn the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles."} +{"id": "00760", "text": "Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.\n\nThen Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?\n\nSee notes for definition of a tandem repeat.\n\n\n-----Input-----\n\nThe first line contains s (1 \u2264 |s| \u2264 200). This string contains only small English letters. The second line contains number k (1 \u2264 k \u2264 200) \u2014 the number of the added characters.\n\n\n-----Output-----\n\nPrint a single number \u2014 the maximum length of the tandem repeat that could have occurred in the new string.\n\n\n-----Examples-----\nInput\naaba\n2\n\nOutput\n6\n\nInput\naaabbbb\n2\n\nOutput\n6\n\nInput\nabracadabra\n10\n\nOutput\n20\n\n\n\n-----Note-----\n\nA tandem repeat of length 2n is string s, where for any position i (1 \u2264 i \u2264 n) the following condition fulfills: s_{i} = s_{i} + n.\n\nIn the first sample Kolya could obtain a string aabaab, in the second \u2014 aaabbbbbb, in the third \u2014 abracadabrabracadabra."} +{"id": "00761", "text": "There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.\n\nAny slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). \n\nWhen a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$.\n\nThe slimes will eat each other until there is only one slime left. \n\nFind the maximum possible value of the last slime.\n\n\n-----Input-----\n\nThe first line of the input contains an integer $n$ ($1 \\le n \\le 500\\,000$) denoting the number of slimes.\n\nThe next line contains $n$ integers $a_i$ ($-10^9 \\le a_i \\le 10^9$), where $a_i$ is the value of $i$-th slime.\n\n\n-----Output-----\n\nPrint an only integer\u00a0\u2014 the maximum possible value of the last slime.\n\n\n-----Examples-----\nInput\n4\n2 1 2 1\n\nOutput\n4\nInput\n5\n0 -1 -1 -1 -1\n\nOutput\n4\n\n\n-----Note-----\n\nIn the first example, a possible way of getting the last slime with value $4$ is:\n\n Second slime eats the third slime, the row now contains slimes $2, -1, 1$\n\n Second slime eats the third slime, the row now contains slimes $2, -2$\n\n First slime eats the second slime, the row now contains $4$ \n\nIn the second example, the first slime can keep eating slimes to its right to end up with a value of $4$."} +{"id": "00762", "text": "There are a lot of things which could be cut\u00a0\u2014 trees, paper, \"the rope\". In this problem you are going to cut a sequence of integers.\n\nThere is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.\n\nCuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, $[4, 1, 2, 3, 4, 5, 4, 4, 5, 5]$ $\\to$ two cuts $\\to$ $[4, 1 | 2, 3, 4, 5 | 4, 4, 5, 5]$. On each segment the number of even elements should be equal to the number of odd elements.\n\nThe cost of the cut between $x$ and $y$ numbers is $|x - y|$ bitcoins. Find the maximum possible number of cuts that can be made while spending no more than $B$ bitcoins.\n\n\n-----Input-----\n\nFirst line of the input contains an integer $n$ ($2 \\le n \\le 100$) and an integer $B$ ($1 \\le B \\le 100$)\u00a0\u2014 the number of elements in the sequence and the number of bitcoins you have.\n\nSecond line contains $n$ integers: $a_1$, $a_2$, ..., $a_n$ ($1 \\le a_i \\le 100$)\u00a0\u2014 elements of the sequence, which contains the equal number of even and odd numbers\n\n\n-----Output-----\n\nPrint the maximum possible number of cuts which can be made while spending no more than $B$ bitcoins.\n\n\n-----Examples-----\nInput\n6 4\n1 2 5 10 15 20\n\nOutput\n1\n\nInput\n4 10\n1 3 2 4\n\nOutput\n0\n\nInput\n6 100\n1 2 3 4 5 6\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample the optimal answer is to split sequence between $2$ and $5$. Price of this cut is equal to $3$ bitcoins.\n\nIn the second sample it is not possible to make even one cut even with unlimited number of bitcoins.\n\nIn the third sample the sequence should be cut between $2$ and $3$, and between $4$ and $5$. The total price of the cuts is $1 + 1 = 2$ bitcoins."} +{"id": "00763", "text": "The Fair Nut lives in $n$ story house. $a_i$ people live on the $i$-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening. \n\nIt was decided that elevator, when it is not used, will stay on the $x$-th floor, but $x$ hasn't been chosen yet. When a person needs to get from floor $a$ to floor $b$, elevator follows the simple algorithm: Moves from the $x$-th floor (initially it stays on the $x$-th floor) to the $a$-th and takes the passenger. Moves from the $a$-th floor to the $b$-th floor and lets out the passenger (if $a$ equals $b$, elevator just opens and closes the doors, but still comes to the floor from the $x$-th floor). Moves from the $b$-th floor back to the $x$-th. The elevator never transposes more than one person and always goes back to the floor $x$ before transposing a next passenger. The elevator spends one unit of electricity to move between neighboring floors. So moving from the $a$-th floor to the $b$-th floor requires $|a - b|$ units of electricity.\n\nYour task is to help Nut to find the minimum number of electricity units, that it would be enough for one day, by choosing an optimal the $x$-th floor. Don't forget than elevator initially stays on the $x$-th floor. \n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\leq n \\leq 100$)\u00a0\u2014 the number of floors.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\leq a_i \\leq 100$)\u00a0\u2014 the number of people on each floor.\n\n\n-----Output-----\n\nIn a single line, print the answer to the problem\u00a0\u2014 the minimum number of electricity units.\n\n\n-----Examples-----\nInput\n3\n0 2 1\n\nOutput\n16\nInput\n2\n1 1\n\nOutput\n4\n\n\n-----Note-----\n\nIn the first example, the answer can be achieved by choosing the second floor as the $x$-th floor. Each person from the second floor (there are two of them) would spend $4$ units of electricity per day ($2$ to get down and $2$ to get up), and one person from the third would spend $8$ units of electricity per day ($4$ to get down and $4$ to get up). $4 \\cdot 2 + 8 \\cdot 1 = 16$.\n\nIn the second example, the answer can be achieved by choosing the first floor as the $x$-th floor."} +{"id": "00764", "text": "After learning about polynomial hashing, Heidi decided to learn about shift-xor hashing. In particular, she came across this interesting problem.\n\nGiven a bitstring $y \\in \\{0,1\\}^n$ find out the number of different $k$ ($0 \\leq k < n$) such that there exists $x \\in \\{0,1\\}^n$ for which $y = x \\oplus \\mbox{shift}^k(x).$\n\nIn the above, $\\oplus$ is the xor operation and $\\mbox{shift}^k$ is the operation of shifting a bitstring cyclically to the right $k$ times. For example, $001 \\oplus 111 = 110$ and $\\mbox{shift}^3(00010010111000) = 00000010010111$.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\leq n \\leq 2 \\cdot 10^5$), the length of the bitstring $y$.\n\nThe second line contains the bitstring $y$.\n\n\n-----Output-----\n\nOutput a single integer: the number of suitable values of $k$.\n\n\n-----Example-----\nInput\n4\n1010\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example: $1100\\oplus \\mbox{shift}^1(1100) = 1010$ $1000\\oplus \\mbox{shift}^2(1000) = 1010$ $0110\\oplus \\mbox{shift}^3(0110) = 1010$ \n\nThere is no $x$ such that $x \\oplus x = 1010$, hence the answer is $3$."} +{"id": "00765", "text": "Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk.\n\nUnfortunately, internet is not that fast in the city of Ekaterinozavodsk and the song takes a lot of time to download. But Lesha is quite impatient. The song's duration is T seconds. Lesha downloads the first S seconds of the song and plays it. When the playback reaches the point that has not yet been downloaded, Lesha immediately plays the song from the start (the loaded part of the song stays in his phone, and the download is continued from the same place), and it happens until the song is downloaded completely and Lesha listens to it to the end. For q seconds of real time the Internet allows you to download q - 1 seconds of the track.\n\nTell Lesha, for how many times he will start the song, including the very first start.\n\n\n-----Input-----\n\nThe single line contains three integers T, S, q (2 \u2264 q \u2264 10^4, 1 \u2264 S < T \u2264 10^5).\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of times the song will be restarted.\n\n\n-----Examples-----\nInput\n5 2 2\n\nOutput\n2\n\nInput\n5 4 7\n\nOutput\n1\n\nInput\n6 2 3\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first test, the song is played twice faster than it is downloaded, which means that during four first seconds Lesha reaches the moment that has not been downloaded, and starts the song again. After another two seconds, the song is downloaded completely, and thus, Lesha starts the song twice.\n\nIn the second test, the song is almost downloaded, and Lesha will start it only once.\n\nIn the third sample test the download finishes and Lesha finishes listening at the same moment. Note that song isn't restarted in this case."} +{"id": "00766", "text": "Let's call a string adorable if its letters can be realigned in such a way that they form two consequent groups of equal symbols (note that different groups must contain different symbols). For example, ababa is adorable (you can transform it to aaabb, where the first three letters form a group of a-s and others \u2014 a group of b-s), but cccc is not since in each possible consequent partition letters in these two groups coincide.\n\nYou're given a string s. Check whether it can be split into two non-empty subsequences such that the strings formed by these subsequences are adorable. Here a subsequence is an arbitrary set of indexes of the string.\n\n\n-----Input-----\n\nThe only line contains s (1 \u2264 |s| \u2264 10^5) consisting of lowercase latin letters.\n\n\n-----Output-----\n\nPrint \u00abYes\u00bb if the string can be split according to the criteria above or \u00abNo\u00bb otherwise.\n\nEach letter can be printed in arbitrary case.\n\n\n-----Examples-----\nInput\nababa\n\nOutput\nYes\n\nInput\nzzcxx\n\nOutput\nYes\n\nInput\nyeee\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn sample case two zzcxx can be split into subsequences zc and zxx each of which is adorable.\n\nThere's no suitable partition in sample case three."} +{"id": "00767", "text": "You are given a set of points $x_1$, $x_2$, ..., $x_n$ on the number line.\n\nTwo points $i$ and $j$ can be matched with each other if the following conditions hold: neither $i$ nor $j$ is matched with any other point; $|x_i - x_j| \\ge z$. \n\nWhat is the maximum number of pairs of points you can match with each other?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $z$ ($2 \\le n \\le 2 \\cdot 10^5$, $1 \\le z \\le 10^9$) \u2014 the number of points and the constraint on the distance between matched points, respectively.\n\nThe second line contains $n$ integers $x_1$, $x_2$, ..., $x_n$ ($1 \\le x_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of pairs of points you can match with each other.\n\n\n-----Examples-----\nInput\n4 2\n1 3 3 7\n\nOutput\n2\n\nInput\n5 5\n10 9 5 8 7\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, you may match point $1$ with point $2$ ($|3 - 1| \\ge 2$), and point $3$ with point $4$ ($|7 - 3| \\ge 2$).\n\nIn the second example, you may match point $1$ with point $3$ ($|5 - 10| \\ge 5$)."} +{"id": "00768", "text": "A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system.\n\nYou are given user's friends' opinions about a list of items. You are also given a threshold T \u2014 the minimal number of \"likes\" necessary for an item to be recommended to the user.\n\nOutput the number of items in the list liked by at least T of user's friends.\n\n\n-----Input-----\n\nThe first line of the input will contain three space-separated integers: the number of friends F (1 \u2264 F \u2264 10), the number of items I (1 \u2264 I \u2264 10) and the threshold T (1 \u2264 T \u2264 F).\n\nThe following F lines of input contain user's friends' opinions. j-th character of i-th line is 'Y' if i-th friend likes j-th item, and 'N' otherwise.\n\n\n-----Output-----\n\nOutput an integer \u2014 the number of items liked by at least T of user's friends.\n\n\n-----Examples-----\nInput\n3 3 2\nYYY\nNNN\nYNY\n\nOutput\n2\n\nInput\n4 4 1\nNNNY\nNNYN\nNYNN\nYNNN\n\nOutput\n4"} +{"id": "00769", "text": "You have a fraction $\\frac{a}{b}$. You need to find the first occurrence of digit c into decimal notation of the fraction after decimal point.\n\n\n-----Input-----\n\nThe first contains three single positive integers a, b, c (1 \u2264 a < b \u2264 10^5, 0 \u2264 c \u2264 9).\n\n\n-----Output-----\n\nPrint position of the first occurrence of digit c into the fraction. Positions are numbered from 1 after decimal point. It there is no such position, print -1.\n\n\n-----Examples-----\nInput\n1 2 0\n\nOutput\n2\nInput\n2 3 7\n\nOutput\n-1\n\n\n-----Note-----\n\nThe fraction in the first example has the following decimal notation: $\\frac{1}{2} = 0.500(0)$. The first zero stands on second position.\n\nThe fraction in the second example has the following decimal notation: $\\frac{2}{3} = 0.666(6)$. There is no digit 7 in decimal notation of the fraction."} +{"id": "00770", "text": "Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.\n\nAlexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.\n\nThe program cannot delete the letters from the list or rearrange them.\n\nAlexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 1000) \u2014 the number of letters in the mailbox.\n\nThe second line contains n space-separated integers (zeros and ones) \u2014 the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum number of operations needed to make all the letters read.\n\n\n-----Examples-----\nInput\n5\n0 1 0 1 0\n\nOutput\n3\n\nInput\n5\n1 1 0 0 1\n\nOutput\n4\n\nInput\n2\n0 0\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.\n\nIn the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.\n\nIn the third sample all letters are already read."} +{"id": "00771", "text": "You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.\n\nNumbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset. \n\n\n-----Input-----\n\nFirst line contains three integers n, k and m (2 \u2264 k \u2264 n \u2264 100 000, 1 \u2264 m \u2264 100 000)\u00a0\u2014 number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.\n\nSecond line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the numbers in the multiset.\n\n\n-----Output-----\n\nIf it is not possible to select k numbers in the desired way, output \u00abNo\u00bb (without the quotes).\n\nOtherwise, in the first line of output print \u00abYes\u00bb (without the quotes). In the second line print k integers b_1, b_2, ..., b_{k}\u00a0\u2014 the selected numbers. If there are multiple possible solutions, print any of them. \n\n\n-----Examples-----\nInput\n3 2 3\n1 8 4\n\nOutput\nYes\n1 4 \nInput\n3 3 3\n1 8 4\n\nOutput\nNo\nInput\n4 3 5\n2 7 7 7\n\nOutput\nYes\n2 7 7"} +{"id": "00772", "text": "Lenny is playing a game on a 3 \u00d7 3 grid of lights. In the beginning of the game all lights are switched on. Pressing any of the lights will toggle it and all side-adjacent lights. The goal of the game is to switch all the lights off. We consider the toggling as follows: if the light was switched on then it will be switched off, if it was switched off then it will be switched on.\n\nLenny has spent some time playing with the grid and by now he has pressed each light a certain number of times. Given the number of times each light is pressed, you have to print the current state of each light.\n\n\n-----Input-----\n\nThe input consists of three rows. Each row contains three integers each between 0 to 100 inclusive. The j-th number in the i-th row is the number of times the j-th light of the i-th row of the grid is pressed.\n\n\n-----Output-----\n\nPrint three lines, each containing three characters. The j-th character of the i-th line is \"1\" if and only if the corresponding light is switched on, otherwise it's \"0\".\n\n\n-----Examples-----\nInput\n1 0 0\n0 0 0\n0 0 1\n\nOutput\n001\n010\n100\n\nInput\n1 0 1\n8 8 8\n2 0 3\n\nOutput\n010\n011\n100"} +{"id": "00773", "text": "Recently Ivan noticed an array a while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug.\n\nIvan clearly remembers that there were n elements in the array, and each element was not less than 1 and not greater than n. Also he remembers q facts about the array. There are two types of facts that Ivan remembers: 1 l_{i} r_{i} v_{i} \u2014 for each x such that l_{i} \u2264 x \u2264 r_{i} a_{x} \u2265 v_{i}; 2 l_{i} r_{i} v_{i} \u2014 for each x such that l_{i} \u2264 x \u2264 r_{i} a_{x} \u2264 v_{i}. \n\nAlso Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the q facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the cost of array as follows:\n\n$\\operatorname{cos} t = \\sum_{i = 1}^{n}(\\operatorname{cnt}(i))^{2}$, where cnt(i) is the number of occurences of i in the array.\n\nHelp Ivan to determine minimum possible cost of the array that corresponds to the facts!\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and q (1 \u2264 n \u2264 50, 0 \u2264 q \u2264 100).\n\nThen q lines follow, each representing a fact about the array. i-th line contains the numbers t_{i}, l_{i}, r_{i} and v_{i} for i-th fact (1 \u2264 t_{i} \u2264 2, 1 \u2264 l_{i} \u2264 r_{i} \u2264 n, 1 \u2264 v_{i} \u2264 n, t_{i} denotes the type of the fact).\n\n\n-----Output-----\n\nIf the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible cost of the array.\n\n\n-----Examples-----\nInput\n3 0\n\nOutput\n3\n\nInput\n3 1\n1 1 3 2\n\nOutput\n5\n\nInput\n3 2\n1 1 3 2\n2 1 3 2\n\nOutput\n9\n\nInput\n3 2\n1 1 3 2\n2 1 3 1\n\nOutput\n-1"} +{"id": "00774", "text": "You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction [Image] whose denominator is no more than n. \n\nFormally, you should find such pair of integers a, b (1 \u2264 b \u2264 n;\u00a00 \u2264 a) that the value $|\\frac{x}{y} - \\frac{a}{b}|$ is as minimal as possible.\n\nIf there are multiple \"nearest\" fractions, choose the one with the minimum denominator. If there are multiple \"nearest\" fractions with the minimum denominator, choose the one with the minimum numerator.\n\n\n-----Input-----\n\nA single line contains three integers x, y, n (1 \u2264 x, y, n \u2264 10^5).\n\n\n-----Output-----\n\nPrint the required fraction in the format \"a/b\" (without quotes).\n\n\n-----Examples-----\nInput\n3 7 6\n\nOutput\n2/5\n\nInput\n7 2 4\n\nOutput\n7/2"} +{"id": "00775", "text": "Zane the wizard is going to perform a magic show shuffling the cups.\n\nThere are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x = i.\n\nThe problematic bone is initially at the position x = 1. Zane will confuse the audience by swapping the cups k times, the i-th time of which involves the cups at the positions x = u_{i} and x = v_{i}. If the bone happens to be at the position where there is a hole at any time, it will fall into the hole onto the ground and will not be affected by future swapping operations.\n\nDo not forget that Zane is a wizard. When he swaps the cups, he does not move them ordinarily. Instead, he teleports the cups (along with the bone, if it is inside) to the intended positions. Therefore, for example, when he swaps the cup at x = 4 and the one at x = 6, they will not be at the position x = 5 at any moment during the operation. [Image] \n\nZane\u2019s puppy, Inzane, is in trouble. Zane is away on his vacation, and Inzane cannot find his beloved bone, as it would be too exhausting to try opening all the cups. Inzane knows that the Codeforces community has successfully helped Zane, so he wants to see if it could help him solve his problem too. Help Inzane determine the final position of the bone.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, and k (2 \u2264 n \u2264 10^6, 1 \u2264 m \u2264 n, 1 \u2264 k \u2264 3\u00b710^5)\u00a0\u2014 the number of cups, the number of holes on the table, and the number of swapping operations, respectively.\n\nThe second line contains m distinct integers h_1, h_2, ..., h_{m} (1 \u2264 h_{i} \u2264 n)\u00a0\u2014 the positions along the x-axis where there is a hole on the table.\n\nEach of the next k lines contains two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i})\u00a0\u2014 the positions of the cups to be swapped.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the final position along the x-axis of the bone.\n\n\n-----Examples-----\nInput\n7 3 4\n3 4 6\n1 2\n2 5\n5 7\n7 1\n\nOutput\n1\nInput\n5 1 2\n2\n1 2\n2 4\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first sample, after the operations, the bone becomes at x = 2, x = 5, x = 7, and x = 1, respectively.\n\nIn the second sample, after the first operation, the bone becomes at x = 2, and falls into the hole onto the ground."} +{"id": "00776", "text": "Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis!\n\nThe computers bought for the room were different. Some of them had only USB ports, some\u00a0\u2014 only PS/2 ports, and some had both options.\n\nYou have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once.\n\nYou want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy.\n\n\n-----Input-----\n\nThe first line contains three integers a, b and c (0 \u2264 a, b, c \u2264 10^5) \u00a0\u2014 the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively.\n\nThe next line contains one integer m (0 \u2264 m \u2264 3\u00b710^5) \u00a0\u2014 the number of mouses in the price list.\n\nThe next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 \u2264 val_{i} \u2264 10^9) \u00a0\u2014 the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in.\n\n\n-----Output-----\n\nOutput two integers separated by space\u00a0\u2014 the number of equipped computers and the total cost of the mouses you will buy.\n\n\n-----Example-----\nInput\n2 1 1\n4\n5 USB\n6 PS/2\n3 PS/2\n7 PS/2\n\nOutput\n3 14\n\n\n\n-----Note-----\n\nIn the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports."} +{"id": "00777", "text": "Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He has 26 photos, labeled \"a\" to \"z\", and he has compiled them into a photo booklet with some photos in some order (possibly with some photos being duplicated). A photo booklet can be described as a string of lowercase letters, consisting of the photos in the booklet in order. He now wants to sell some \"special edition\" photobooks, each with one extra photo inserted anywhere in the book. He wants to make as many distinct photobooks as possible, so he can make more money. He asks Haruhi, how many distinct photobooks can he make by inserting one extra photo into the photobook he already has?\n\nPlease help Haruhi solve this problem.\n\n\n-----Input-----\n\nThe first line of input will be a single string s (1 \u2264 |s| \u2264 20). String s consists only of lowercase English letters. \n\n\n-----Output-----\n\nOutput a single integer equal to the number of distinct photobooks Kyoya Ootori can make.\n\n\n-----Examples-----\nInput\na\n\nOutput\n51\n\nInput\nhi\n\nOutput\n76\n\n\n\n-----Note-----\n\nIn the first case, we can make 'ab','ac',...,'az','ba','ca',...,'za', and 'aa', producing a total of 51 distinct photo booklets."} +{"id": "00778", "text": "King of Berland Berl IV has recently died. Hail Berl V! As a sign of the highest achievements of the deceased king the new king decided to build a mausoleum with Berl IV's body on the main square of the capital.\n\nThe mausoleum will be constructed from 2n blocks, each of them has the shape of a cuboid. Each block has the bottom base of a 1 \u00d7 1 meter square. Among the blocks, exactly two of them have the height of one meter, exactly two have the height of two meters, ..., exactly two have the height of n meters.\n\nThe blocks are arranged in a row without spacing one after the other. Of course, not every arrangement of blocks has the form of a mausoleum. In order to make the given arrangement in the form of the mausoleum, it is necessary that when you pass along the mausoleum, from one end to the other, the heights of the blocks first were non-decreasing (i.e., increasing or remained the same), and then \u2014 non-increasing (decrease or remained unchanged). It is possible that any of these two areas will be omitted. For example, the following sequences of block height meet this requirement:\n\n [1, 2, 2, 3, 4, 4, 3, 1]; [1, 1]; [2, 2, 1, 1]; [1, 2, 3, 3, 2, 1]. \n\nSuddenly, k more requirements appeared. Each of the requirements has the form: \"h[x_{i}] sign_{i} h[y_{i}]\", where h[t] is the height of the t-th block, and a sign_{i} is one of the five possible signs: '=' (equals), '<' (less than), '>' (more than), '<=' (less than or equals), '>=' (more than or equals). Thus, each of the k additional requirements is given by a pair of indexes x_{i}, y_{i} (1 \u2264 x_{i}, y_{i} \u2264 2n) and sign sign_{i}.\n\nFind the number of possible ways to rearrange the blocks so that both the requirement about the shape of the mausoleum (see paragraph 3) and the k additional requirements were met.\n\n\n-----Input-----\n\nThe first line of the input contains integers n and k (1 \u2264 n \u2264 35, 0 \u2264 k \u2264 100) \u2014 the number of pairs of blocks and the number of additional requirements.\n\nNext k lines contain listed additional requirements, one per line in the format \"x_{i} sign_{i} y_{i}\" (1 \u2264 x_{i}, y_{i} \u2264 2n), and the sign is on of the list of the five possible signs.\n\n\n-----Output-----\n\nPrint the sought number of ways.\n\n\n-----Examples-----\nInput\n3 0\n\nOutput\n9\n\nInput\n3 1\n2 > 3\n\nOutput\n1\n\nInput\n4 1\n3 = 6\n\nOutput\n3"} +{"id": "00779", "text": "Fafa owns a company that works on huge projects. There are n employees in Fafa's company. Whenever the company has a new project to start working on, Fafa has to divide the tasks of this project among all the employees.\n\nFafa finds doing this every time is very tiring for him. So, he decided to choose the best l employees in his company as team leaders. Whenever there is a new project, Fafa will divide the tasks among only the team leaders and each team leader will be responsible of some positive number of employees to give them the tasks. To make this process fair for the team leaders, each one of them should be responsible for the same number of employees. Moreover, every employee, who is not a team leader, has to be under the responsibility of exactly one team leader, and no team leader is responsible for another team leader.\n\nGiven the number of employees n, find in how many ways Fafa could choose the number of team leaders l in such a way that it is possible to divide employees between them evenly.\n\n\n-----Input-----\n\nThe input consists of a single line containing a positive integer n (2 \u2264 n \u2264 10^5) \u2014 the number of employees in Fafa's company.\n\n\n-----Output-----\n\nPrint a single integer representing the answer to the problem.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1\n\nInput\n10\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the second sample Fafa has 3 ways: choose only 1 employee as a team leader with 9 employees under his responsibility. choose 2 employees as team leaders with 4 employees under the responsibility of each of them. choose 5 employees as team leaders with 1 employee under the responsibility of each of them."} +{"id": "00780", "text": "Suppose you have a special $x$-$y$-counter. This counter can store some value as a decimal number; at first, the counter has value $0$. \n\nThe counter performs the following algorithm: it prints its lowest digit and, after that, adds either $x$ or $y$ to its value. So all sequences this counter generates are starting from $0$. For example, a $4$-$2$-counter can act as follows: it prints $0$, and adds $4$ to its value, so the current value is $4$, and the output is $0$; it prints $4$, and adds $4$ to its value, so the current value is $8$, and the output is $04$; it prints $8$, and adds $4$ to its value, so the current value is $12$, and the output is $048$; it prints $2$, and adds $2$ to its value, so the current value is $14$, and the output is $0482$; it prints $4$, and adds $4$ to its value, so the current value is $18$, and the output is $04824$. \n\nThis is only one of the possible outputs; for example, the same counter could generate $0246802468024$ as the output, if we chose to add $2$ during each step.\n\nYou wrote down a printed sequence from one of such $x$-$y$-counters. But the sequence was corrupted and several elements from the sequence could be erased.\n\nNow you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $s$ \u2014 the remaining data of the sequence. \n\nFor all $0 \\le x, y < 10$, calculate the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $x$-$y$-counter. Note that you can't change the order of digits in string $s$ or erase any of them; only insertions are allowed.\n\n\n-----Input-----\n\nThe first line contains a single string $s$ ($1 \\le |s| \\le 2 \\cdot 10^6$, $s_i \\in \\{\\text{0} - \\text{9}\\}$) \u2014 the remaining data you have. It's guaranteed that $s_1 = 0$.\n\n\n-----Output-----\n\nPrint a $10 \\times 10$ matrix, where the $j$-th integer ($0$-indexed) on the $i$-th line ($0$-indexed too) is equal to the minimum number of digits you have to insert in the string $s$ to make it a possible output of the $i$-$j$-counter, or $-1$ if there is no way to do so.\n\n\n-----Example-----\nInput\n0840\n\nOutput\n-1 17 7 7 7 -1 2 17 2 7 \n17 17 7 5 5 5 2 7 2 7 \n7 7 7 4 3 7 1 7 2 5 \n7 5 4 7 3 3 2 5 2 3 \n7 5 3 3 7 7 1 7 2 7 \n-1 5 7 3 7 -1 2 9 2 7 \n2 2 1 2 1 2 2 2 0 1 \n17 7 7 5 7 9 2 17 2 3 \n2 2 2 2 2 2 0 2 2 2 \n7 7 5 3 7 7 1 3 2 7 \n\n\n\n-----Note-----\n\nLet's take, for example, $4$-$3$-counter. One of the possible outcomes the counter could print is $0(4)8(1)4(7)0$ (lost elements are in the brackets).\n\nOne of the possible outcomes a $2$-$3$-counter could print is $0(35)8(1)4(7)0$.\n\nThe $6$-$8$-counter could print exactly the string $0840$."} +{"id": "00781", "text": "The Little Elephant loves chess very much. \n\nOne day the Little Elephant and his friend decided to play chess. They've got the chess pieces but the board is a problem. They've got an 8 \u00d7 8 checkered board, each square is painted either black or white. The Little Elephant and his friend know that a proper chessboard doesn't have any side-adjacent cells with the same color and the upper left cell is white. To play chess, they want to make the board they have a proper chessboard. For that the friends can choose any row of the board and cyclically shift the cells of the chosen row, that is, put the last (rightmost) square on the first place in the row and shift the others one position to the right. You can run the described operation multiple times (or not run it at all).\n\nFor example, if the first line of the board looks like that \"BBBBBBWW\" (the white cells of the line are marked with character \"W\", the black cells are marked with character \"B\"), then after one cyclic shift it will look like that \"WBBBBBBW\".\n\nHelp the Little Elephant and his friend to find out whether they can use any number of the described operations to turn the board they have into a proper chessboard.\n\n\n-----Input-----\n\nThe input consists of exactly eight lines. Each line contains exactly eight characters \"W\" or \"B\" without any spaces: the j-th character in the i-th line stands for the color of the j-th cell of the i-th row of the elephants' board. Character \"W\" stands for the white color, character \"B\" stands for the black color.\n\nConsider the rows of the board numbered from 1 to 8 from top to bottom, and the columns \u2014 from 1 to 8 from left to right. The given board can initially be a proper chessboard.\n\n\n-----Output-----\n\nIn a single line print \"YES\" (without the quotes), if we can make the board a proper chessboard and \"NO\" (without the quotes) otherwise.\n\n\n-----Examples-----\nInput\nWBWBWBWB\nBWBWBWBW\nBWBWBWBW\nBWBWBWBW\nWBWBWBWB\nWBWBWBWB\nBWBWBWBW\nWBWBWBWB\n\nOutput\nYES\n\nInput\nWBWBWBWB\nWBWBWBWB\nBBWBWWWB\nBWBWBWBW\nBWBWBWBW\nBWBWBWWW\nBWBWBWBW\nBWBWBWBW\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample you should shift the following lines one position to the right: the 3-rd, the 6-th, the 7-th and the 8-th.\n\nIn the second sample there is no way you can achieve the goal."} +{"id": "00782", "text": "In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.\n\nWhen he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a_1, a_2, ..., a_{n}. He remembered that he calculated gcd(a_{i}, a_{i} + 1, ..., a_{j}) for every 1 \u2264 i \u2264 j \u2264 n and put it into a set S. gcd here means the greatest common divisor.\n\nNote that even if a number is put into the set S twice or more, it only appears once in the set.\n\nNow Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.\n\n\n-----Input-----\n\nThe first line contains a single integer m (1 \u2264 m \u2264 1000)\u00a0\u2014 the size of the set S.\n\nThe second line contains m integers s_1, s_2, ..., s_{m} (1 \u2264 s_{i} \u2264 10^6)\u00a0\u2014 the elements of the set S. It's guaranteed that the elements of the set are given in strictly increasing order, that means s_1 < s_2 < ... < s_{m}.\n\n\n-----Output-----\n\nIf there is no solution, print a single line containing -1.\n\nOtherwise, in the first line print a single integer n denoting the length of the sequence, n should not exceed 4000.\n\nIn the second line print n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^6)\u00a0\u2014 the sequence.\n\nWe can show that if a solution exists, then there is a solution with n not exceeding 4000 and a_{i} not exceeding 10^6.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n4\n2 4 6 12\n\nOutput\n3\n4 6 12\nInput\n2\n2 3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example 2 = gcd(4, 6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among gcd(a_{i}, a_{i} + 1, ..., a_{j}) for every 1 \u2264 i \u2264 j \u2264 n."} +{"id": "00783", "text": "The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.\n\nLet's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.\n\nThe new architect is interested in n questions, i-th of them is about the following: \"how many floors should be added to the i-th house to make it luxurious?\" (for all i from 1 to n, inclusive). You need to help him cope with this task.\n\nNote that all these questions are independent from each other \u2014 the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).\n\n\n-----Input-----\n\nThe first line of the input contains a single number n (1 \u2264 n \u2264 10^5) \u2014 the number of houses in the capital of Berland.\n\nThe second line contains n space-separated positive integers h_{i} (1 \u2264 h_{i} \u2264 10^9), where h_{i} equals the number of floors in the i-th house. \n\n\n-----Output-----\n\nPrint n integers a_1, a_2, ..., a_{n}, where number a_{i} is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then a_{i} should be equal to zero.\n\nAll houses are numbered from left to right, starting from one.\n\n\n-----Examples-----\nInput\n5\n1 2 3 1 2\n\nOutput\n3 2 0 2 0 \nInput\n4\n3 2 1 4\n\nOutput\n2 3 4 0"} +{"id": "00784", "text": "Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: multiply the current number by 2 (that is, replace the number x by 2\u00b7x); append the digit 1 to the right of current number (that is, replace the number x by 10\u00b7x + 1). \n\nYou need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible.\n\nNote that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b.\n\n\n-----Input-----\n\nThe first line contains two positive integers a and b (1 \u2264 a < b \u2264 10^9)\u00a0\u2014 the number which Vasily has and the number he wants to have.\n\n\n-----Output-----\n\nIf there is no way to get b from a, print \"NO\" (without quotes).\n\nOtherwise print three lines. On the first line print \"YES\" (without quotes). The second line should contain single integer k\u00a0\u2014 the length of the transformation sequence. On the third line print the sequence of transformations x_1, x_2, ..., x_{k}, where: x_1 should be equal to a, x_{k} should be equal to b, x_{i} should be obtained from x_{i} - 1 using any of two described operations (1 < i \u2264 k). \n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n2 162\n\nOutput\nYES\n5\n2 4 8 81 162 \n\nInput\n4 42\n\nOutput\nNO\n\nInput\n100 40021\n\nOutput\nYES\n5\n100 200 2001 4002 40021"} +{"id": "00785", "text": "The start of the new academic year brought about the problem of accommodation students into dormitories. One of such dormitories has a a \u00d7 b square meter wonder room. The caretaker wants to accommodate exactly n students there. But the law says that there must be at least 6 square meters per student in a room (that is, the room for n students must have the area of at least 6n square meters). The caretaker can enlarge any (possibly both) side of the room by an arbitrary positive integer of meters. Help him change the room so as all n students could live in it and the total area of the room was as small as possible.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers n, a and b (1 \u2264 n, a, b \u2264 10^9) \u2014 the number of students and the sizes of the room.\n\n\n-----Output-----\n\nPrint three integers s, a_1 and b_1 (a \u2264 a_1;\u00a0b \u2264 b_1) \u2014 the final area of the room and its sizes. If there are multiple optimal solutions, print any of them.\n\n\n-----Examples-----\nInput\n3 3 5\n\nOutput\n18\n3 6\n\nInput\n2 4 4\n\nOutput\n16\n4 4"} +{"id": "00786", "text": "Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating changes by some value, possibly negative or zero.\n\nLimak competed in n contests in the year 2016. He remembers that in the i-th contest he competed in the division d_{i} (i.e. he belonged to this division just before the start of this contest) and his rating changed by c_{i} just after the contest. Note that negative c_{i} denotes the loss of rating.\n\nWhat is the maximum possible rating Limak can have right now, after all n contests? If his rating may be arbitrarily big, print \"Infinity\". If there is no scenario matching the given information, print \"Impossible\".\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 200 000).\n\nThe i-th of next n lines contains two integers c_{i} and d_{i} ( - 100 \u2264 c_{i} \u2264 100, 1 \u2264 d_{i} \u2264 2), describing Limak's rating change after the i-th contest and his division during the i-th contest contest.\n\n\n-----Output-----\n\nIf Limak's current rating can be arbitrarily big, print \"Infinity\" (without quotes). If the situation is impossible, print \"Impossible\" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the n contests.\n\n\n-----Examples-----\nInput\n3\n-7 1\n5 2\n8 2\n\nOutput\n1907\n\nInput\n2\n57 1\n22 2\n\nOutput\nImpossible\n\nInput\n1\n-5 1\n\nOutput\nInfinity\n\nInput\n4\n27 2\n13 1\n-50 1\n8 2\n\nOutput\n1897\n\n\n\n-----Note-----\n\nIn the first sample, the following scenario matches all information Limak remembers and has maximum possible final rating: Limak has rating 1901 and belongs to the division 1 in the first contest. His rating decreases by 7. With rating 1894 Limak is in the division 2. His rating increases by 5. Limak has rating 1899 and is still in the division 2. In the last contest of the year he gets + 8 and ends the year with rating 1907. \n\nIn the second sample, it's impossible that Limak is in the division 1, his rating increases by 57 and after that Limak is in the division 2 in the second contest."} +{"id": "00787", "text": "You are given a string q. A sequence of k strings s_1, s_2, ..., s_{k} is called beautiful, if the concatenation of these strings is string q (formally, s_1 + s_2 + ... + s_{k} = q) and the first characters of these strings are distinct.\n\nFind any beautiful sequence of strings or determine that the beautiful sequence doesn't exist.\n\n\n-----Input-----\n\nThe first line contains a positive integer k (1 \u2264 k \u2264 26) \u2014 the number of strings that should be in a beautiful sequence. \n\nThe second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.\n\n\n-----Output-----\n\nIf such sequence doesn't exist, then print in a single line \"NO\" (without the quotes). Otherwise, print in the first line \"YES\" (without the quotes) and in the next k lines print the beautiful sequence of strings s_1, s_2, ..., s_{k}.\n\nIf there are multiple possible answers, print any of them.\n\n\n-----Examples-----\nInput\n1\nabca\n\nOutput\nYES\nabca\n\nInput\n2\naaacas\n\nOutput\nYES\naaa\ncas\n\nInput\n4\nabc\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the second sample there are two possible answers: {\"aaaca\", \"s\"} and {\"aaa\", \"cas\"}."} +{"id": "00788", "text": "-----Input-----\n\nThe only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of \"valid\").\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Examples-----\nInput\nA221033\n\nOutput\n21\n\nInput\nA223635\n\nOutput\n22\n\nInput\nA232726\n\nOutput\n23"} +{"id": "00789", "text": "Once again Tavas started eating coffee mix without water! Keione told him that it smells awful, but he didn't stop doing that. That's why Keione told his smart friend, SaDDas to punish him! SaDDas took Tavas' headphones and told him: \"If you solve the following problem, I'll return it to you.\" [Image] \n\nThe problem is: \n\nYou are given a lucky number n. Lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.\n\nIf we sort all lucky numbers in increasing order, what's the 1-based index of n? \n\nTavas is not as smart as SaDDas, so he asked you to do him a favor and solve this problem so he can have his headphones back.\n\n\n-----Input-----\n\nThe first and only line of input contains a lucky number n (1 \u2264 n \u2264 10^9).\n\n\n-----Output-----\n\nPrint the index of n among all lucky numbers.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n1\n\nInput\n7\n\nOutput\n2\n\nInput\n77\n\nOutput\n6"} +{"id": "00790", "text": "Mr. Chanek is currently participating in a science fair that is popular in town. He finds an exciting puzzle in the fair and wants to solve it.\n\nThere are $N$ atoms numbered from $1$ to $N$. These atoms are especially quirky. Initially, each atom is in normal state. Each atom can be in an excited. Exciting atom $i$ requires $D_i$ energy. When atom $i$ is excited, it will give $A_i$ energy. You can excite any number of atoms (including zero).\n\nThese atoms also form a peculiar one-way bond. For each $i$, $(1 \\le i < N)$, if atom $i$ is excited, atom $E_i$ will also be excited at no cost. Initially, $E_i$ = $i+1$. Note that atom $N$ cannot form a bond to any atom.\n\nMr. Chanek must change exactly $K$ bonds. Exactly $K$ times, Mr. Chanek chooses an atom $i$, $(1 \\le i < N)$ and changes $E_i$ to a different value other than $i$ and the current $E_i$. Note that an atom's bond can remain unchanged or changed more than once. Help Mr. Chanek determine the maximum energy that he can achieve!\n\nnote: You must first change exactly $K$ bonds before you can start exciting atoms.\n\n\n-----Input-----\n\nThe first line contains two integers $N$ $K$ $(4 \\le N \\le 10^5, 0 \\le K < N)$, the number of atoms, and the number of bonds that must be changed.\n\nThe second line contains $N$ integers $A_i$ $(1 \\le A_i \\le 10^6)$, which denotes the energy given by atom $i$ when on excited state.\n\nThe third line contains $N$ integers $D_i$ $(1 \\le D_i \\le 10^6)$, which denotes the energy needed to excite atom $i$.\n\n\n-----Output-----\n\nA line with an integer that denotes the maximum number of energy that Mr. Chanek can get.\n\n\n-----Example-----\nInput\n6 1\n5 6 7 8 10 2\n3 5 6 7 1 10\n\nOutput\n35\n\n\n\n-----Note-----\n\nAn optimal solution to change $E_5$ to 1 and then excite atom 5 with energy 1. It will cause atoms 1, 2, 3, 4, 5 be excited. The total energy gained by Mr. Chanek is (5 + 6 + 7 + 8 + 10) - 1 = 35.\n\nAnother possible way is to change $E_3$ to 1 and then exciting atom 3 (which will excite atom 1, 2, 3) and exciting atom 4 (which will excite atom 4, 5, 6). The total energy gained by Mr. Chanek is (5 + 6 + 7 + 8 + 10 + 2) - (6 + 7) = 25 which is not optimal."} +{"id": "00791", "text": "Sergey is testing a next-generation processor. Instead of bytes the processor works with memory cells consisting of n bits. These bits are numbered from 1 to n. An integer is stored in the cell in the following way: the least significant bit is stored in the first bit of the cell, the next significant bit is stored in the second bit, and so on; the most significant bit is stored in the n-th bit.\n\nNow Sergey wants to test the following instruction: \"add 1 to the value of the cell\". As a result of the instruction, the integer that is written in the cell must be increased by one; if some of the most significant bits of the resulting number do not fit into the cell, they must be discarded.\n\nSergey wrote certain values \u200b\u200bof the bits in the cell and is going to add one to its value. How many bits of the cell will change after the operation?\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100) \u2014 the number of bits in the cell.\n\nThe second line contains a string consisting of n characters \u2014 the initial state of the cell. The first character denotes the state of the first bit of the cell. The second character denotes the second least significant bit and so on. The last character denotes the state of the most significant bit.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of bits in the cell which change their state after we add 1 to the cell.\n\n\n-----Examples-----\nInput\n4\n1100\n\nOutput\n3\n\nInput\n4\n1111\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample the cell ends up with value 0010, in the second sample \u2014 with 0000."} +{"id": "00792", "text": "Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card.\n\nShe starts with 0 money on her account.\n\nIn the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked.\n\nIn the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d.\n\nIt can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be \u00ab-1\u00bb.\n\nLuba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her!\n\n\n-----Input-----\n\nThe first line contains two integers n, d (1 \u2264 n \u2264 10^5, 1 \u2264 d \u2264 10^9) \u2014the number of days and the money limitation.\n\nThe second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 \u2264 a_{i} \u2264 10^4), where a_{i} represents the transaction in i-th day.\n\n\n-----Output-----\n\nPrint -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money.\n\n\n-----Examples-----\nInput\n5 10\n-1 5 0 -5 3\n\nOutput\n0\n\nInput\n3 4\n-10 0 20\n\nOutput\n-1\n\nInput\n5 10\n-5 0 10 -11 0\n\nOutput\n2"} +{"id": "00793", "text": "You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).\nIn how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?\nHere the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.\nFor both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.\nSince the answer can be tremendous, print the number modulo 10^9+7.\n\n-----Constraints-----\n - 1 \\leq N, M \\leq 2 \\times 10^3\n - The length of S is N.\n - The length of T is M. \n - 1 \\leq S_i, T_i \\leq 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nS_1 S_2 ... S_{N-1} S_{N}\nT_1 T_2 ... T_{M-1} T_{M}\n\n-----Output-----\nPrint the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.\n\n-----Sample Input-----\n2 2\n1 3\n3 1\n\n-----Sample Output-----\n3\n\nS has four subsequences: (), (1), (3), (1, 3).\nT has four subsequences: (), (3), (1), (3, 1).\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both (), 1 \\times 1 pair of subsequences in which the subsequences are both (1), and 1 \\times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs."} +{"id": "00794", "text": "You're given an array $a$ of length $2n$. Is it possible to reorder it in such way so that the sum of the first $n$ elements isn't equal to the sum of the last $n$ elements?\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 1000$), where $2n$ is the number of elements in the array $a$.\n\nThe second line contains $2n$ space-separated integers $a_1$, $a_2$, $\\ldots$, $a_{2n}$ ($1 \\le a_i \\le 10^6$)\u00a0\u2014 the elements of the array $a$.\n\n\n-----Output-----\n\nIf there's no solution, print \"-1\" (without quotes). Otherwise, print a single line containing $2n$ space-separated integers. They must form a reordering of $a$. You are allowed to not change the order.\n\n\n-----Examples-----\nInput\n3\n1 2 2 1 3 1\n\nOutput\n2 1 3 1 1 2\nInput\n1\n1 1\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first example, the first $n$ elements have sum $2+1+3=6$ while the last $n$ elements have sum $1+1+2=4$. The sums aren't equal.\n\nIn the second example, there's no solution."} +{"id": "00795", "text": "In mathematics, the Pythagorean theorem \u2014 is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states:\n\n \u007fIn any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle). \n\nThe theorem can be written as an equation relating the lengths of the sides a, b and c, often called the Pythagorean equation:\n\na^2 + b^2 = c^2\n\nwhere c represents the length of the hypotenuse, and a and b represent the lengths of the other two sides.\n\n [Image] \n\nGiven n, your task is to count how many right-angled triangles with side-lengths a, b and c that satisfied an inequality 1 \u2264 a \u2264 b \u2264 c \u2264 n.\n\n\n-----Input-----\n\nThe only line contains one integer n\u00a0(1 \u2264 n \u2264 10^4) as we mentioned above.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n1\n\nInput\n74\n\nOutput\n35"} +{"id": "00796", "text": "You have $n \\times n$ square grid and an integer $k$. Put an integer in each cell while satisfying the conditions below. All numbers in the grid should be between $1$ and $k$ inclusive. Minimum number of the $i$-th row is $1$ ($1 \\le i \\le n$). Minimum number of the $j$-th column is $1$ ($1 \\le j \\le n$). \n\nFind the number of ways to put integers in the grid. Since the answer can be very large, find the answer modulo $(10^{9} + 7)$. [Image] These are the examples of valid and invalid grid when $n=k=2$. \n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $k$ ($1 \\le n \\le 250$, $1 \\le k \\le 10^{9}$).\n\n\n-----Output-----\n\nPrint the answer modulo $(10^{9} + 7)$.\n\n\n-----Examples-----\nInput\n2 2\n\nOutput\n7\n\nInput\n123 456789\n\nOutput\n689974806\n\n\n\n-----Note-----\n\nIn the first example, following $7$ cases are possible. [Image] \n\nIn the second example, make sure you print the answer modulo $(10^{9} + 7)$."} +{"id": "00797", "text": "Seryozha conducts a course dedicated to building a map of heights of Stepanovo recreation center. He laid a rectangle grid of size $n \\times m$ cells on a map (rows of grid are numbered from $1$ to $n$ from north to south, and columns are numbered from $1$ to $m$ from west to east). After that he measured the average height of each cell above Rybinsk sea level and obtained a matrix of heights of size $n \\times m$. The cell $(i, j)$ lies on the intersection of the $i$-th row and the $j$-th column and has height $h_{i, j}$. \n\nSeryozha is going to look at the result of his work in the browser. The screen of Seryozha's laptop can fit a subrectangle of size $a \\times b$ of matrix of heights ($1 \\le a \\le n$, $1 \\le b \\le m$). Seryozha tries to decide how the weather can affect the recreation center \u2014 for example, if it rains, where all the rainwater will gather. To do so, he is going to find the cell having minimum height among all cells that are shown on the screen of his laptop.\n\nHelp Seryozha to calculate the sum of heights of such cells for all possible subrectangles he can see on his screen. In other words, you have to calculate the sum of minimum heights in submatrices of size $a \\times b$ with top left corners in $(i, j)$ over all $1 \\le i \\le n - a + 1$ and $1 \\le j \\le m - b + 1$.\n\nConsider the sequence $g_i = (g_{i - 1} \\cdot x + y) \\bmod z$. You are given integers $g_0$, $x$, $y$ and $z$. By miraculous coincidence, $h_{i, j} = g_{(i - 1) \\cdot m + j - 1}$ ($(i - 1) \\cdot m + j - 1$ is the index).\n\n\n-----Input-----\n\nThe first line of the input contains four integers $n$, $m$, $a$ and $b$ ($1 \\le n, m \\le 3\\,000$, $1 \\le a \\le n$, $1 \\le b \\le m$) \u2014 the number of rows and columns in the matrix Seryozha has, and the number of rows and columns that can be shown on the screen of the laptop, respectively.\n\nThe second line of the input contains four integers $g_0$, $x$, $y$ and $z$ ($0 \\le g_0, x, y < z \\le 10^9$).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Example-----\nInput\n3 4 2 1\n1 2 3 59\n\nOutput\n111\n\n\n\n-----Note-----\n\nThe matrix from the first example: $\\left. \\begin{array}{|c|c|c|c|} \\hline 1 & {5} & {13} & {29} \\\\ \\hline 2 & {7} & {17} & {37} \\\\ \\hline 18 & {39} & {22} & {47} \\\\ \\hline \\end{array} \\right.$"} +{"id": "00798", "text": "Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into one molecule.\n\nA molecule consists of atoms, with some pairs of atoms connected by atomic bonds. Each atom has a valence number \u2014 the number of bonds the atom must form with other atoms. An atom can form one or multiple bonds with any other atom, but it cannot form a bond with itself. The number of bonds of an atom in the molecule must be equal to its valence number. [Image] \n\nMike knows valence numbers of the three atoms. Find a molecule that can be built from these atoms according to the stated rules, or determine that it is impossible.\n\n\n-----Input-----\n\nThe single line of the input contains three space-separated integers a, b and c (1 \u2264 a, b, c \u2264 10^6) \u2014 the valence numbers of the given atoms.\n\n\n-----Output-----\n\nIf such a molecule can be built, print three space-separated integers \u2014 the number of bonds between the 1-st and the 2-nd, the 2-nd and the 3-rd, the 3-rd and the 1-st atoms, correspondingly. If there are multiple solutions, output any of them. If there is no solution, print \"Impossible\" (without the quotes).\n\n\n-----Examples-----\nInput\n1 1 2\n\nOutput\n0 1 1\n\nInput\n3 4 5\n\nOutput\n1 3 2\n\nInput\n4 1 1\n\nOutput\nImpossible\n\n\n\n-----Note-----\n\nThe first sample corresponds to the first figure. There are no bonds between atoms 1 and 2 in this case.\n\nThe second sample corresponds to the second figure. There is one or more bonds between each pair of atoms.\n\nThe third sample corresponds to the third figure. There is no solution, because an atom cannot form bonds with itself.\n\nThe configuration in the fourth figure is impossible as each atom must have at least one atomic bond."} +{"id": "00799", "text": "In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. \n\nTotally in Berland there are n citizens, the welfare of each of them is estimated as the integer in a_{i} burles (burle is the currency in Berland).\n\nYou are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. \n\n\n-----Input-----\n\nThe first line contains the integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of citizens in the kingdom.\n\nThe second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} (0 \u2264 a_{i} \u2264 10^6)\u00a0\u2014 the welfare of the i-th citizen.\n\n\n-----Output-----\n\nIn the only line print the integer S\u00a0\u2014 the minimum number of burles which are had to spend.\n\n\n-----Examples-----\nInput\n5\n0 1 2 3 4\n\nOutput\n10\nInput\n5\n1 1 0 1 1\n\nOutput\n1\nInput\n3\n1 3 1\n\nOutput\n4\nInput\n1\n12\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.\n\nIn the second example it is enough to give one burle to the third citizen. \n\nIn the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.\n\nIn the fourth example it is possible to give nothing to everyone because all citizens have 12 burles."} +{"id": "00800", "text": "Flatland has recently introduced a new type of an eye check for the driver's licence. The check goes like that: there is a plane with mannequins standing on it. You should tell the value of the minimum angle with the vertex at the origin of coordinates and with all mannequins standing inside or on the boarder of this angle. \n\nAs you spend lots of time \"glued to the screen\", your vision is impaired. So you have to write a program that will pass the check for you.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5) \u2014 the number of mannequins.\n\nNext n lines contain two space-separated integers each: x_{i}, y_{i} (|x_{i}|, |y_{i}| \u2264 1000) \u2014 the coordinates of the i-th mannequin. It is guaranteed that the origin of the coordinates has no mannequin. It is guaranteed that no two mannequins are located in the same point on the plane.\n\n\n-----Output-----\n\nPrint a single real number \u2014 the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10^{ - 6}. \n\n\n-----Examples-----\nInput\n2\n2 0\n0 2\n\nOutput\n90.0000000000\n\nInput\n3\n2 0\n0 2\n-2 2\n\nOutput\n135.0000000000\n\nInput\n4\n2 0\n0 2\n-2 0\n0 -2\n\nOutput\n270.0000000000\n\nInput\n2\n2 1\n1 2\n\nOutput\n36.8698976458\n\n\n\n-----Note-----\n\nSolution for the first sample test is shown below: [Image] \n\nSolution for the second sample test is shown below: [Image] \n\nSolution for the third sample test is shown below: [Image] \n\nSolution for the fourth sample test is shown below: $\\#$"} +{"id": "00801", "text": "In this problem MEX of a certain array is the smallest positive integer not contained in this array.\n\nEveryone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.\n\nYou are given an array $a$ of length $n$. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.\n\nAn array $b$ is a subarray of an array $a$, if $b$ can be obtained from $a$ by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.\n\nLesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the length of the array. \n\nThe next line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le n$)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the MEX of MEXes of all subarrays.\n\n\n-----Examples-----\nInput\n3\n1 3 2\n\nOutput\n3\n\nInput\n5\n1 4 3 1 2\n\nOutput\n6"} +{"id": "00802", "text": "Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.\n\nThere is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once. \n\nSergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit. \n\n\n-----Input-----\n\nThe first line contains the integer n (1 \u2264 n \u2264 100 000) \u2014 the number of flats in the house.\n\nThe second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i. \n\n\n-----Output-----\n\nPrint the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house. \n\n\n-----Examples-----\nInput\n3\nAaA\n\nOutput\n2\n\nInput\n7\nbcAAcbc\n\nOutput\n3\n\nInput\n6\naaBCCe\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.\n\nIn the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6. \n\nIn the third test Sergei B. must begin from the flat number 2 and end in the flat number 6."} +{"id": "00803", "text": "Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.\n\nFor another exercise, Pasha needs exactly $\\frac{n}{2}$ hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?\n\n\n-----Input-----\n\nThe first line contains integer n (2 \u2264 n \u2264 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.\n\n\n-----Output-----\n\nIn the first line, print a single integer \u2014 the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.\n\n\n-----Examples-----\nInput\n4\nxxXx\n\nOutput\n1\nXxXx\n\nInput\n2\nXX\n\nOutput\n1\nxX\n\nInput\n6\nxXXxXx\n\nOutput\n0\nxXXxXx"} +{"id": "00804", "text": "Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters, or print that it is impossible.\n\nString s consists only of lowercase Latin letters, and it is allowed to change characters only to lowercase Latin letters too.\n\n\n-----Input-----\n\nFirst line of input contains string s, consisting only of lowercase Latin letters (1 \u2264 |s| \u2264 1000, |s| denotes the length of s).\n\nSecond line of input contains integer k (1 \u2264 k \u2264 26).\n\n\n-----Output-----\n\nPrint single line with a minimum number of necessary changes, or the word \u00abimpossible\u00bb (without quotes) if it is impossible.\n\n\n-----Examples-----\nInput\nyandex\n6\n\nOutput\n0\n\nInput\nyahoo\n5\n\nOutput\n1\n\nInput\ngoogle\n7\n\nOutput\nimpossible\n\n\n\n-----Note-----\n\nIn the first test case string contains 6 different letters, so we don't need to change anything.\n\nIn the second test case string contains 4 different letters: {'a', 'h', 'o', 'y'}. To get 5 different letters it is necessary to change one occurrence of 'o' to some letter, which doesn't occur in the string, for example, {'b'}.\n\nIn the third test case, it is impossible to make 7 different letters because the length of the string is 6."} +{"id": "00805", "text": "Our old friend Alexey has finally entered the University of City N \u2014 the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm. \n\nThe dorm has exactly one straight dryer \u2014 a 100 centimeter long rope to hang clothes on. The dryer has got a coordinate system installed: the leftmost end of the dryer has coordinate 0, and the opposite end has coordinate 100. Overall, the university has n students. Dean's office allows i-th student to use the segment (l_{i}, r_{i}) of the dryer. However, the dean's office actions are contradictory and now one part of the dryer can belong to multiple students!\n\nAlexey don't like when someone touch his clothes. That's why he want make it impossible to someone clothes touch his ones. So Alexey wonders: what is the total length of the parts of the dryer that he may use in a such way that clothes of the others (n - 1) students aren't drying there. Help him! Note that Alexey, as the most respected student, has number 1.\n\n\n-----Input-----\n\nThe first line contains a positive integer n (1 \u2264 n \u2264 100). The (i + 1)-th line contains integers l_{i} and r_{i} (0 \u2264 l_{i} < r_{i} \u2264 100) \u2014\u00a0the endpoints of the corresponding segment for the i-th student.\n\n\n-----Output-----\n\nOn a single line print a single number k, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments.\n\n\n-----Examples-----\nInput\n3\n0 5\n2 8\n1 6\n\nOutput\n1\n\nInput\n3\n0 10\n1 5\n7 15\n\nOutput\n3\n\n\n\n-----Note-----\n\nNote that it's not important are clothes drying on the touching segments (e.g. (0, 1) and (1, 2)) considered to be touching or not because you need to find the length of segments.\n\nIn the first test sample Alexey may use the only segment (0, 1). In such case his clothes will not touch clothes on the segments (1, 6) and (2, 8). The length of segment (0, 1) is 1.\n\nIn the second test sample Alexey may dry his clothes on segments (0, 1) and (5, 7). Overall length of these segments is 3."} +{"id": "00806", "text": "Ayoub had an array $a$ of integers of size $n$ and this array had two interesting properties: All the integers in the array were between $l$ and $r$ (inclusive). The sum of all the elements was divisible by $3$. \n\nUnfortunately, Ayoub has lost his array, but he remembers the size of the array $n$ and the numbers $l$ and $r$, so he asked you to find the number of ways to restore the array. \n\nSince the answer could be very large, print it modulo $10^9 + 7$ (i.e. the remainder when dividing by $10^9 + 7$). In case there are no satisfying arrays (Ayoub has a wrong memory), print $0$.\n\n\n-----Input-----\n\nThe first and only line contains three integers $n$, $l$ and $r$ ($1 \\le n \\le 2 \\cdot 10^5 , 1 \\le l \\le r \\le 10^9$)\u00a0\u2014 the size of the lost array and the range of numbers in the array.\n\n\n-----Output-----\n\nPrint the remainder when dividing by $10^9 + 7$ the number of ways to restore the array.\n\n\n-----Examples-----\nInput\n2 1 3\n\nOutput\n3\n\nInput\n3 2 2\n\nOutput\n1\n\nInput\n9 9 99\n\nOutput\n711426616\n\n\n\n-----Note-----\n\nIn the first example, the possible arrays are : $[1,2], [2,1], [3, 3]$.\n\nIn the second example, the only possible array is $[2, 2, 2]$."} +{"id": "00807", "text": "The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the following n days. According to the bear's data, on the i-th (1 \u2264 i \u2264 n) day, the price for one barrel of honey is going to is x_{i} kilos of raspberry.\n\nUnfortunately, the bear has neither a honey barrel, nor the raspberry. At the same time, the bear's got a friend who is ready to lend him a barrel of honey for exactly one day for c kilograms of raspberry. That's why the bear came up with a smart plan. He wants to choose some day d (1 \u2264 d < n), lent a barrel of honey and immediately (on day d) sell it according to a daily exchange rate. The next day (d + 1) the bear wants to buy a new barrel of honey according to a daily exchange rate (as he's got some raspberry left from selling the previous barrel) and immediately (on day d + 1) give his friend the borrowed barrel of honey as well as c kilograms of raspberry for renting the barrel.\n\nThe bear wants to execute his plan at most once and then hibernate. What maximum number of kilograms of raspberry can he earn? Note that if at some point of the plan the bear runs out of the raspberry, then he won't execute such a plan.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers, n and c (2 \u2264 n \u2264 100, 0 \u2264 c \u2264 100), \u2014 the number of days and the number of kilos of raspberry that the bear should give for borrowing the barrel.\n\nThe second line contains n space-separated integers x_1, x_2, ..., x_{n} (0 \u2264 x_{i} \u2264 100), the price of a honey barrel on day i.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n5 1\n5 10 7 3 20\n\nOutput\n3\n\nInput\n6 2\n100 1 10 40 10 40\n\nOutput\n97\n\nInput\n3 0\n1 2 3\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample the bear will lend a honey barrel at day 3 and then sell it for 7. Then the bear will buy a barrel for 3 and return it to the friend. So, the profit is (7 - 3 - 1) = 3.\n\nIn the second sample bear will lend a honey barrel at day 1 and then sell it for 100. Then the bear buy the barrel for 1 at the day 2. So, the profit is (100 - 1 - 2) = 97."} +{"id": "00808", "text": "You are given a positive decimal number x.\n\nYour task is to convert it to the \"simple exponential notation\".\n\nLet x = a\u00b710^{b}, where 1 \u2264 a < 10, then in general case the \"simple exponential notation\" looks like \"aEb\". If b equals to zero, the part \"Eb\" should be skipped. If a is an integer, it should be written without decimal point. Also there should not be extra zeroes in a and b.\n\n\n-----Input-----\n\nThe only line contains the positive decimal number x. The length of the line will not exceed 10^6. Note that you are given too large number, so you can't use standard built-in data types \"float\", \"double\" and other.\n\n\n-----Output-----\n\nPrint the only line \u2014 the \"simple exponential notation\" of the given number x.\n\n\n-----Examples-----\nInput\n16\n\nOutput\n1.6E1\n\nInput\n01.23400\n\nOutput\n1.234\n\nInput\n.100\n\nOutput\n1E-1\n\nInput\n100.\n\nOutput\n1E2"} +{"id": "00809", "text": "Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.\n\nInnokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.\n\n\n-----Input-----\n\nThe first line contains four integers n, k, a and b (1 \u2264 k \u2264 n \u2264 10^5, 0 \u2264 a, b \u2264 n)\u00a0\u2014 the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n.\n\n\n-----Output-----\n\nIf it is impossible to drink n cups of tea, print \"NO\" (without quotes).\n\nOtherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n5 1 3 2\n\nOutput\nGBGBG\n\nInput\n7 2 2 5\n\nOutput\nBBGBGBB\nInput\n4 3 4 0\n\nOutput\nNO"} +{"id": "00810", "text": "Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits a and b. Vitaly calls a good number excellent, if the sum of its digits is a good number.\n\nFor example, let's say that Vitaly's favourite digits are 1 and 3, then number 12 isn't good and numbers 13 or 311 are. Also, number 111 is excellent and number 11 isn't. \n\nNow Vitaly is wondering, how many excellent numbers of length exactly n are there. As this number can be rather large, he asks you to count the remainder after dividing it by 1000000007 (10^9 + 7).\n\nA number's length is the number of digits in its decimal representation without leading zeroes.\n\n\n-----Input-----\n\nThe first line contains three integers: a, b, n (1 \u2264 a < b \u2264 9, 1 \u2264 n \u2264 10^6).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n1 3 3\n\nOutput\n1\n\nInput\n2 3 10\n\nOutput\n165"} +{"id": "00811", "text": "Vasily the Programmer loves romance, so this year he decided to illuminate his room with candles.\n\nVasily has a candles.When Vasily lights up a new candle, it first burns for an hour and then it goes out. Vasily is smart, so he can make b went out candles into a new candle. As a result, this new candle can be used like any other new candle.\n\nNow Vasily wonders: for how many hours can his candles light up the room if he acts optimally well? Help him find this number.\n\n\n-----Input-----\n\nThe single line contains two integers, a and b (1 \u2264 a \u2264 1000;\u00a02 \u2264 b \u2264 1000).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of hours Vasily can light up the room for.\n\n\n-----Examples-----\nInput\n4 2\n\nOutput\n7\n\nInput\n6 3\n\nOutput\n8\n\n\n\n-----Note-----\n\nConsider the first sample. For the first four hours Vasily lights up new candles, then he uses four burned out candles to make two new ones and lights them up. When these candles go out (stop burning), Vasily can make another candle. Overall, Vasily can light up the room for 7 hours."} +{"id": "00812", "text": "A sequence $a_1, a_2, \\dots, a_k$ is called an arithmetic progression if for each $i$ from $1$ to $k$ elements satisfy the condition $a_i = a_1 + c \\cdot (i - 1)$ for some fixed $c$.\n\nFor example, these five sequences are arithmetic progressions: $[5, 7, 9, 11]$, $[101]$, $[101, 100, 99]$, $[13, 97]$ and $[5, 5, 5, 5, 5]$. And these four sequences aren't arithmetic progressions: $[3, 1, 2]$, $[1, 2, 4, 8]$, $[1, -1, 1, -1]$ and $[1, 2, 3, 3, 3]$.\n\nYou are given a sequence of integers $b_1, b_2, \\dots, b_n$. Find any index $j$ ($1 \\le j \\le n$), such that if you delete $b_j$ from the sequence, you can reorder the remaining $n-1$ elements, so that you will get an arithmetic progression. If there is no such index, output the number -1.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 2\\cdot10^5$) \u2014 length of the sequence $b$. The second line contains $n$ integers $b_1, b_2, \\dots, b_n$ ($-10^9 \\le b_i \\le 10^9$) \u2014 elements of the sequence $b$.\n\n\n-----Output-----\n\nPrint such index $j$ ($1 \\le j \\le n$), so that if you delete the $j$-th element from the sequence, you can reorder the remaining elements, so that you will get an arithmetic progression. If there are multiple solutions, you are allowed to print any of them. If there is no such index, print -1.\n\n\n-----Examples-----\nInput\n5\n2 6 8 7 4\n\nOutput\n4\nInput\n8\n1 2 3 4 5 6 7 8\n\nOutput\n1\nInput\n4\n1 2 4 8\n\nOutput\n-1\n\n\n-----Note-----\n\nNote to the first example. If you delete the $4$-th element, you can get the arithmetic progression $[2, 4, 6, 8]$.\n\nNote to the second example. The original sequence is already arithmetic progression, so you can delete $1$-st or last element and you will get an arithmetical progression again."} +{"id": "00813", "text": "Pasha has two hamsters: Arthur and Alexander. Pasha put n apples in front of them. Pasha knows which apples Arthur likes. Similarly, Pasha knows which apples Alexander likes. Pasha doesn't want any conflict between the hamsters (as they may like the same apple), so he decided to distribute the apples between the hamsters on his own. He is going to give some apples to Arthur and some apples to Alexander. It doesn't matter how many apples each hamster gets but it is important that each hamster gets only the apples he likes. It is possible that somebody doesn't get any apples.\n\nHelp Pasha distribute all the apples between the hamsters. Note that Pasha wants to distribute all the apples, not just some of them.\n\n\n-----Input-----\n\nThe first line contains integers n, a, b (1 \u2264 n \u2264 100;\u00a01 \u2264 a, b \u2264 n) \u2014 the number of apples Pasha has, the number of apples Arthur likes and the number of apples Alexander likes, correspondingly.\n\nThe next line contains a distinct integers \u2014 the numbers of the apples Arthur likes. The next line contains b distinct integers \u2014 the numbers of the apples Alexander likes.\n\nAssume that the apples are numbered from 1 to n. The input is such that the answer exists.\n\n\n-----Output-----\n\nPrint n characters, each of them equals either 1 or 2. If the i-h character equals 1, then the i-th apple should be given to Arthur, otherwise it should be given to Alexander. If there are multiple correct answers, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n4 2 3\n1 2\n2 3 4\n\nOutput\n1 1 2 2\n\nInput\n5 5 2\n3 4 1 2 5\n2 3\n\nOutput\n1 1 1 1 1"} +{"id": "00814", "text": "Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the ability to change gravity.\n\nThere are n columns of toy cubes in the box arranged in a line. The i-th column contains a_{i} cubes. At first, the gravity in the box is pulling the cubes downwards. When Chris switches the gravity, it begins to pull all the cubes to the right side of the box. The figure shows the initial and final configurations of the cubes in the box: the cubes that have changed their position are highlighted with orange. [Image] \n\nGiven the initial configuration of the toy cubes in the box, find the amounts of cubes in each of the n columns after the gravity switch!\n\n\n-----Input-----\n\nThe first line of input contains an integer n (1 \u2264 n \u2264 100), the number of the columns in the box. The next line contains n space-separated integer numbers. The i-th number a_{i} (1 \u2264 a_{i} \u2264 100) denotes the number of cubes in the i-th column.\n\n\n-----Output-----\n\nOutput n integer numbers separated by spaces, where the i-th number is the amount of cubes in the i-th column after the gravity switch.\n\n\n-----Examples-----\nInput\n4\n3 2 1 2\n\nOutput\n1 2 2 3 \n\nInput\n3\n2 3 8\n\nOutput\n2 3 8 \n\n\n\n-----Note-----\n\nThe first example case is shown on the figure. The top cube of the first column falls to the top of the last column; the top cube of the second column falls to the top of the third column; the middle cube of the first column falls to the top of the second column.\n\nIn the second example case the gravity switch does not change the heights of the columns."} +{"id": "00815", "text": "A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. \n\nMasha came to test these cars. She could climb into all cars, but she liked only the smallest car. \n\nIt's known that a character with size a can climb into some car with size b if and only if a \u2264 b, he or she likes it if and only if he can climb into this car and 2a \u2265 b.\n\nYou are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars.\n\n\n-----Input-----\n\nYou are given four integers V_1, V_2, V_3, V_{m}(1 \u2264 V_{i} \u2264 100)\u00a0\u2014 sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V_1 > V_2 > V_3.\n\n\n-----Output-----\n\nOutput three integers\u00a0\u2014 sizes of father bear's car, mother bear's car and son bear's car, respectively.\n\nIf there are multiple possible solutions, print any.\n\nIf there is no solution, print \"-1\" (without quotes).\n\n\n-----Examples-----\nInput\n50 30 10 10\n\nOutput\n50\n30\n10\n\nInput\n100 50 10 21\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn first test case all conditions for cars' sizes are satisfied.\n\nIn second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20."} +{"id": "00816", "text": "There are some beautiful girls in Arpa\u2019s land as mentioned before.\n\nOnce Arpa came up with an obvious problem:\n\nGiven an array and a number x, count the number of pairs of indices i, j (1 \u2264 i < j \u2264 n) such that $a_{i} \\oplus a_{j} = x$, where $\\oplus$ is bitwise xor operation (see notes for explanation).\n\n [Image] \n\nImmediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.\n\n\n-----Input-----\n\nFirst line contains two integers n and x (1 \u2264 n \u2264 10^5, 0 \u2264 x \u2264 10^5)\u00a0\u2014 the number of elements in the array and the integer x.\n\nSecond line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint a single integer: the answer to the problem.\n\n\n-----Examples-----\nInput\n2 3\n1 2\n\nOutput\n1\nInput\n6 1\n5 1 2 3 4 1\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first sample there is only one pair of i = 1 and j = 2. $a_{1} \\oplus a_{2} = 3 = x$ so the answer is 1.\n\nIn the second sample the only two pairs are i = 3, j = 4 (since $2 \\oplus 3 = 1$) and i = 1, j = 5 (since $5 \\oplus 4 = 1$).\n\nA bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR."} +{"id": "00817", "text": "Some time ago Lesha found an entertaining string $s$ consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows.\n\nLesha chooses an arbitrary (possibly zero) number of pairs on positions $(i, i + 1)$ in such a way that the following conditions are satisfied: for each pair $(i, i + 1)$ the inequality $0 \\le i < |s| - 1$ holds; for each pair $(i, i + 1)$ the equality $s_i = s_{i + 1}$ holds; there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. \n\nLesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string.\n\n\n-----Input-----\n\nThe only line contains the string $s$ ($1 \\le |s| \\le 10^5$) \u2014 the initial string consisting of lowercase English letters only.\n\n\n-----Output-----\n\nIn $|s|$ lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than $10$ characters, instead print the first $5$ characters, then \"...\", then the last $2$ characters of the answer.\n\n\n-----Examples-----\nInput\nabcdd\n\nOutput\n3 abc\n2 bc\n1 c\n0 \n1 d\n\nInput\nabbcdddeaaffdfouurtytwoo\n\nOutput\n18 abbcd...tw\n17 bbcdd...tw\n16 bcddd...tw\n15 cddde...tw\n14 dddea...tw\n13 ddeaa...tw\n12 deaad...tw\n11 eaadf...tw\n10 aadfortytw\n9 adfortytw\n8 dfortytw\n9 fdfortytw\n8 dfortytw\n7 fortytw\n6 ortytw\n5 rtytw\n6 urtytw\n5 rtytw\n4 tytw\n3 ytw\n2 tw\n1 w\n0 \n1 o\n\n\n\n-----Note-----\n\nConsider the first example.\n\n The longest suffix is the whole string \"abcdd\". Choosing one pair $(4, 5)$, Lesha obtains \"abc\". The next longest suffix is \"bcdd\". Choosing one pair $(3, 4)$, we obtain \"bc\". The next longest suffix is \"cdd\". Choosing one pair $(2, 3)$, we obtain \"c\". The next longest suffix is \"dd\". Choosing one pair $(1, 2)$, we obtain \"\" (an empty string). The last suffix is the string \"d\". No pair can be chosen, so the answer is \"d\". \n\nIn the second example, for the longest suffix \"abbcdddeaaffdfouurtytwoo\" choose three pairs $(11, 12)$, $(16, 17)$, $(23, 24)$ and we obtain \"abbcdddeaadfortytw\""} +{"id": "00818", "text": "Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up with a few games that were connected with them.\n\nChilly Willy wants to find the minimum number of length n, such that it is simultaneously divisible by all numbers Willy already knows (2, 3, 5 and 7). Help him with that.\n\nA number's length is the number of digits in its decimal representation without leading zeros.\n\n\n-----Input-----\n\nA single input line contains a single integer n (1 \u2264 n \u2264 10^5).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem without leading zeroes, or \"-1\" (without the quotes), if the number that meet the problem condition does not exist.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n-1\n\nInput\n5\n\nOutput\n10080"} +{"id": "00819", "text": "You are given an array a_1, a_2, ..., a_{n} consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?\n\nDefinitions of subsegment and array splitting are given in notes.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 k \u2264 n \u2264 10^5) \u2014 the size of the array a and the number of subsegments you have to split the array to.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint single integer \u2014 the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments.\n\n\n-----Examples-----\nInput\n5 2\n1 2 3 4 5\n\nOutput\n5\n\nInput\n5 1\n-4 -5 -3 -2 -1\n\nOutput\n-5\n\n\n\n-----Note-----\n\nA subsegment [l, r] (l \u2264 r) of array a is the sequence a_{l}, a_{l} + 1, ..., a_{r}.\n\nSplitting of array a of n elements into k subsegments [l_1, r_1], [l_2, r_2], ..., [l_{k}, r_{k}] (l_1 = 1, r_{k} = n, l_{i} = r_{i} - 1 + 1 for all i > 1) is k sequences (a_{l}_1, ..., a_{r}_1), ..., (a_{l}_{k}, ..., a_{r}_{k}).\n\nIn the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.\n\nIn the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5."} +{"id": "00820", "text": "Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a_1, a_2, ..., a_{n} megabytes. The file size is equal to m megabytes. \n\nFind the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.\n\n\n-----Input-----\n\nThe first line contains positive integer n (1 \u2264 n \u2264 100) \u2014 the number of USB flash drives.\n\nThe second line contains positive integer m (1 \u2264 m \u2264 10^5) \u2014 the size of Sean's file.\n\nEach of the next n lines contains positive integer a_{i} (1 \u2264 a_{i} \u2264 1000) \u2014 the sizes of USB flash drives in megabytes.\n\nIt is guaranteed that the answer exists, i. e. the sum of all a_{i} is not less than m.\n\n\n-----Output-----\n\nPrint the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.\n\n\n-----Examples-----\nInput\n3\n5\n2\n1\n3\n\nOutput\n2\n\nInput\n3\n6\n2\n3\n2\n\nOutput\n3\n\nInput\n2\n5\n5\n10\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example Sean needs only two USB flash drives \u2014 the first and the third.\n\nIn the second example Sean needs all three USB flash drives.\n\nIn the third example Sean needs only one USB flash drive and he can use any available USB flash drive \u2014 the first or the second."} +{"id": "00821", "text": "Two boys decided to compete in text typing on the site \"Key races\". During the competition, they have to type a text consisting of s characters. The first participant types one character in v_1 milliseconds and has ping t_1 milliseconds. The second participant types one character in v_2 milliseconds and has ping t_2 milliseconds.\n\nIf connection ping (delay) is t milliseconds, the competition passes for a participant as follows: Exactly after t milliseconds after the start of the competition the participant receives the text to be entered. Right after that he starts to type it. Exactly t milliseconds after he ends typing all the text, the site receives information about it. \n\nThe winner is the participant whose information on the success comes earlier. If the information comes from both participants at the same time, it is considered that there is a draw.\n\nGiven the length of the text and the information about participants, determine the result of the game.\n\n\n-----Input-----\n\nThe first line contains five integers s, v_1, v_2, t_1, t_2 (1 \u2264 s, v_1, v_2, t_1, t_2 \u2264 1000)\u00a0\u2014 the number of characters in the text, the time of typing one character for the first participant, the time of typing one character for the the second participant, the ping of the first participant and the ping of the second participant.\n\n\n-----Output-----\n\nIf the first participant wins, print \"First\". If the second participant wins, print \"Second\". In case of a draw print \"Friendship\".\n\n\n-----Examples-----\nInput\n5 1 2 1 2\n\nOutput\nFirst\n\nInput\n3 3 1 1 1\n\nOutput\nSecond\n\nInput\n4 5 3 1 5\n\nOutput\nFriendship\n\n\n\n-----Note-----\n\nIn the first example, information on the success of the first participant comes in 7 milliseconds, of the second participant\u00a0\u2014 in 14 milliseconds. So, the first wins.\n\nIn the second example, information on the success of the first participant comes in 11 milliseconds, of the second participant\u00a0\u2014 in 5 milliseconds. So, the second wins.\n\nIn the third example, information on the success of the first participant comes in 22 milliseconds, of the second participant\u00a0\u2014 in 22 milliseconds. So, it is be a draw."} +{"id": "00822", "text": "Comrade Dujikov is busy choosing artists for Timofey's birthday and is recieving calls from Taymyr from Ilia-alpinist.\n\nIlia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, i.e. in minutes m, 2m, 3m and so on. The day is z minutes long, i.e. the day consists of minutes 1, 2, ..., z. How many artists should be killed so that there are no artists in the room when Ilia calls? Consider that a call and a talk with an artist take exactly one minute.\n\n\n-----Input-----\n\nThe only string contains three integers\u00a0\u2014 n, m and z (1 \u2264 n, m, z \u2264 10^4).\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the minimum number of artists that should be killed so that there are no artists in the room when Ilia calls.\n\n\n-----Examples-----\nInput\n1 1 10\n\nOutput\n10\n\nInput\n1 2 5\n\nOutput\n2\n\nInput\n2 3 9\n\nOutput\n1\n\n\n\n-----Note-----\n\nTaymyr is a place in the north of Russia.\n\nIn the first test the artists come each minute, as well as the calls, so we need to kill all of them.\n\nIn the second test we need to kill artists which come on the second and the fourth minutes.\n\nIn the third test\u00a0\u2014 only the artist which comes on the sixth minute."} +{"id": "00823", "text": "Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.\n\nValera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).\n\n\n-----Input-----\n\nThe first line contains two space-separated integers x and y (|x|, |y| \u2264 100).\n\n\n-----Output-----\n\nPrint a single integer, showing how many times Valera has to turn.\n\n\n-----Examples-----\nInput\n0 0\n\nOutput\n0\n\nInput\n1 0\n\nOutput\n0\n\nInput\n0 1\n\nOutput\n2\n\nInput\n-1 -1\n\nOutput\n3"} +{"id": "00824", "text": "As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters \"(\" and \")\" (without quotes)).\n\nOn the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence s of length n is an RSBS if the following conditions are met:\n\n It is not empty (that is n \u2260 0). The length of the sequence is even. First $\\frac{n}{2}$ charactes of the sequence are equal to \"(\". Last $\\frac{n}{2}$ charactes of the sequence are equal to \")\". \n\nFor example, the sequence \"((()))\" is an RSBS but the sequences \"((())\" and \"(()())\" are not RSBS.\n\nElena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence s. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of s is a string that can be obtained from s by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted.\n\nBecause the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 10^9 + 7.\n\nAnton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it!\n\n\n-----Input-----\n\nThe only line of the input contains a string s\u00a0\u2014 the bracket sequence given in Anton's homework. The string consists only of characters \"(\" and \")\" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200 000.\n\n\n-----Output-----\n\nOutput one number\u00a0\u2014 the answer for the task modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n)(()()\n\nOutput\n6\n\nInput\n()()()\n\nOutput\n7\n\nInput\n)))\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample the following subsequences are possible:\n\n If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence \"(())\". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence \"()\". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence \"()\". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence \"()\". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence \"()\". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence \"()\". \n\nThe rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6."} +{"id": "00825", "text": "Given is a positive integer N. Consider repeatedly applying the operation below on N:\n - First, choose a positive integer z satisfying all of the conditions below:\n - z can be represented as z=p^e, where p is a prime number and e is a positive integer;\n - z divides N;\n - z is different from all integers chosen in previous operations.\n - Then, replace N with N/z.\nFind the maximum number of times the operation can be applied.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^{12}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the maximum number of times the operation can be applied.\n\n-----Sample Input-----\n24\n\n-----Sample Output-----\n3\n\nWe can apply the operation three times by, for example, making the following choices:\n - Choose z=2 (=2^1). (Now we have N=12.)\n - Choose z=3 (=3^1). (Now we have N=4.)\n - Choose z=4 (=2^2). (Now we have N=1.)"} +{"id": "00826", "text": "Snuke is visiting a shop in Tokyo called 109 to buy some logs.\nHe wants n logs: one of length 1, one of length 2, ..., and one of length n.\nThe shop has n+1 logs in stock: one of length 1, one of length 2, \\dots, and one of length n+1. Each of these logs is sold for 1 yen (the currency of Japan).\nHe can cut these logs as many times as he wants after buying them. That is, he can get k logs of length L_1, \\dots, L_k from a log of length L, where L = L_1 + \\dots + L_k. He can also throw away unwanted logs.\nSnuke wants to spend as little money as possible to get the logs he wants.\nFind the minimum amount of money needed to get n logs of length 1 to n.\n\n-----Constraints-----\n - 1 \\leq n \\leq 10^{18}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn\n\n-----Output-----\nPrint the minimum amount of money needed to get n logs of length 1 to n.\n\n-----Sample Input-----\n4\n\n-----Sample Output-----\n3\n\nOne way to get the logs he wants with 3 yen is:\n - Buy logs of length 2, 4, and 5.\n - Cut the log of length 5 into two logs of length 1 each and a log of length 3.\n - Throw away one of the logs of length 1."} +{"id": "00827", "text": "Let S be the concatenation of 10^{10} copies of the string 110. (For reference, the concatenation of 3 copies of 110 is 110110110.)\nWe have a string T of length N.\nFind the number of times T occurs in S as a contiguous substring.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - T is a string of length N consisting of 0 and 1.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nT\n\n-----Output-----\nPrint the number of times T occurs in S as a contiguous substring.\n\n-----Sample Input-----\n4\n1011\n\n-----Sample Output-----\n9999999999\n\nS is so long, so let us instead count the number of times 1011 occurs in the concatenation of 3 copies of 110, that is, 110110110. We can see it occurs twice:\n - 1 1011 0110\n - 1101 1011 0"} +{"id": "00828", "text": "There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.\n\nThere was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.\n\nSome of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.\n\n\n-----Input-----\n\nThe first line contains two positive integers n and s (1 \u2264 n \u2264 2\u00b710^5, 1 \u2264 s \u2264 n)\u00a0\u2014 the number of workers and the id of the chief.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 n - 1), where a_{i} is the number of superiors (not only immediate) the worker with id i reported about.\n\n\n-----Output-----\n\nPrint the minimum number of workers that could make a mistake.\n\n\n-----Examples-----\nInput\n3 2\n2 0 2\n\nOutput\n1\n\nInput\n5 3\n1 0 0 4 1\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example it is possible that only the first worker made a mistake. Then: the immediate superior of the first worker is the second worker, the immediate superior of the third worker is the first worker, the second worker is the chief."} +{"id": "00829", "text": "After playing Neo in the legendary \"Matrix\" trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the following problem.\n\nLet's call a string consisting of only zeroes and ones good if it contains different numbers of zeroes and ones. For example, 1, 101, 0000 are good, while 01, 1001, and 111000 are not good.\n\nWe are given a string $s$ of length $n$ consisting of only zeroes and ones. We need to cut $s$ into minimal possible number of substrings $s_1, s_2, \\ldots, s_k$ such that all of them are good. More formally, we have to find minimal by number of strings sequence of good strings $s_1, s_2, \\ldots, s_k$ such that their concatenation (joining) equals $s$, i.e. $s_1 + s_2 + \\dots + s_k = s$.\n\nFor example, cuttings 110010 into 110 and 010 or into 11 and 0010 are valid, as 110, 010, 11, 0010 are all good, and we can't cut 110010 to the smaller number of substrings as 110010 isn't good itself. At the same time, cutting of 110010 into 1100 and 10 isn't valid as both strings aren't good. Also, cutting of 110010 into 1, 1, 0010 isn't valid, as it isn't minimal, even though all $3$ strings are good.\n\nCan you help Keanu? We can show that the solution always exists. If there are multiple optimal answers, print any.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($1\\le n \\le 100$)\u00a0\u2014 the length of the string $s$.\n\nThe second line contains the string $s$ of length $n$ consisting only from zeros and ones.\n\n\n-----Output-----\n\nIn the first line, output a single integer $k$ ($1\\le k$)\u00a0\u2014 a minimal number of strings you have cut $s$ into.\n\nIn the second line, output $k$ strings $s_1, s_2, \\ldots, s_k$ separated with spaces. The length of each string has to be positive. Their concatenation has to be equal to $s$ and all of them have to be good.\n\nIf there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n1\n1\n\nOutput\n1\n1\nInput\n2\n10\n\nOutput\n2\n1 0\nInput\n6\n100011\n\nOutput\n2\n100 011\n\n\n\n-----Note-----\n\nIn the first example, the string 1 wasn't cut at all. As it is good, the condition is satisfied.\n\nIn the second example, 1 and 0 both are good. As 10 isn't good, the answer is indeed minimal.\n\nIn the third example, 100 and 011 both are good. As 100011 isn't good, the answer is indeed minimal."} +{"id": "00830", "text": "As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of a_{i} cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). \n\nThe boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. \n\nBefore going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task.\n\n\n-----Input-----\n\nThe first line contains two space-separated positive integers n and k (1 \u2264 n \u2264 100, 1 \u2264 k \u2264 1000) \u2014 the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers a_{i} (1 \u2264 a_{i} \u2264 10^4) \u2014 the towers' initial heights.\n\n\n-----Output-----\n\nIn the first line print two space-separated non-negative integers s and m (m \u2264 k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that.\n\nIn the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i \u2260 j). Note that in the process of performing operations the heights of some towers can become equal to zero.\n\nIf there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n3 2\n5 8 5\n\nOutput\n0 2\n2 1\n2 3\n\nInput\n3 4\n2 2 4\n\nOutput\n1 1\n3 2\n\nInput\n5 3\n8 3 2 6 3\n\nOutput\n3 3\n1 3\n1 2\n1 3\n\n\n\n-----Note-----\n\nIn the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6."} +{"id": "00831", "text": "You desperately need to build some string t. For that you've got n more strings s_1, s_2, ..., s_{n}. To build string t, you are allowed to perform exactly |t| (|t| is the length of string t) operations on these strings. Each operation looks like that: choose any non-empty string from strings s_1, s_2, ..., s_{n}; choose an arbitrary character from the chosen string and write it on a piece of paper; remove the chosen character from the chosen string. \n\nNote that after you perform the described operation, the total number of characters in strings s_1, s_2, ..., s_{n} decreases by 1. We are assumed to build string t, if the characters, written on the piece of paper, in the order of performed operations form string t.\n\nThere are other limitations, though. For each string s_{i} you know number a_{i} \u2014 the maximum number of characters you are allowed to delete from string s_{i}. You also know that each operation that results in deleting a character from string s_{i}, costs i rubles. That is, an operation on string s_1 is the cheapest (it costs 1 ruble), and the operation on string s_{n} is the most expensive one (it costs n rubles).\n\nYour task is to count the minimum amount of money (in rubles) you will need to build string t by the given rules. Consider the cost of building string t to be the sum of prices of the operations you use.\n\n\n-----Input-----\n\nThe first line of the input contains string t \u2014 the string that you need to build.\n\nThe second line contains a single integer n (1 \u2264 n \u2264 100) \u2014 the number of strings to which you are allowed to apply the described operation. Each of the next n lines contains a string and an integer. The i-th line contains space-separated string s_{i} and integer a_{i} (0 \u2264 a_{i} \u2264 100). Number a_{i} represents the maximum number of characters that can be deleted from string s_{i}.\n\nAll strings in the input only consist of lowercase English letters. All strings are non-empty. The lengths of all strings do not exceed 100 characters.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum money (in rubles) you need in order to build string t. If there is no solution, print -1.\n\n\n-----Examples-----\nInput\nbbaze\n3\nbzb 2\naeb 3\nba 10\n\nOutput\n8\n\nInput\nabacaba\n4\naba 2\nbcc 1\ncaa 2\nbbb 5\n\nOutput\n18\n\nInput\nxyz\n4\naxx 8\nza 1\nefg 4\nt 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nNotes to the samples:\n\nIn the first sample from the first string you should take characters \"b\" and \"z\" with price 1 ruble, from the second string characters \"a\", \"e\" \u0438 \"b\" with price 2 rubles. The price of the string t in this case is 2\u00b71 + 3\u00b72 = 8.\n\nIn the second sample from the first string you should take two characters \"a\" with price 1 ruble, from the second string character \"c\" with price 2 rubles, from the third string two characters \"a\" with price 3 rubles, from the fourth string two characters \"b\" with price 4 rubles. The price of the string t in this case is 2\u00b71 + 1\u00b72 + 2\u00b73 + 2\u00b74 = 18.\n\nIn the third sample the solution doesn't exist because there is no character \"y\" in given strings."} +{"id": "00832", "text": "Manao works on a sports TV. He's spent much time watching the football games of some country. After a while he began to notice different patterns. For example, each team has two sets of uniforms: home uniform and guest uniform. When a team plays a game at home, the players put on the home uniform. When a team plays as a guest on somebody else's stadium, the players put on the guest uniform. The only exception to that rule is: when the home uniform color of the host team matches the guests' uniform, the host team puts on its guest uniform as well. For each team the color of the home and guest uniform is different.\n\nThere are n teams taking part in the national championship. The championship consists of n\u00b7(n - 1) games: each team invites each other team to its stadium. At this point Manao wondered: how many times during the championship is a host team going to put on the guest uniform? Note that the order of the games does not affect this number.\n\nYou know the colors of the home and guest uniform for each team. For simplicity, the colors are numbered by integers in such a way that no two distinct colors have the same number. Help Manao find the answer to his question.\n\n\n-----Input-----\n\nThe first line contains an integer n (2 \u2264 n \u2264 30). Each of the following n lines contains a pair of distinct space-separated integers h_{i}, a_{i} (1 \u2264 h_{i}, a_{i} \u2264 100) \u2014 the colors of the i-th team's home and guest uniforms, respectively.\n\n\n-----Output-----\n\nIn a single line print the number of games where the host team is going to play in the guest uniform.\n\n\n-----Examples-----\nInput\n3\n1 2\n2 4\n3 4\n\nOutput\n1\n\nInput\n4\n100 42\n42 100\n5 42\n100 5\n\nOutput\n5\n\nInput\n2\n1 2\n1 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test case the championship consists of 6 games. The only game with the event in question is the game between teams 2 and 1 on the stadium of team 2.\n\nIn the second test sample the host team will have to wear guest uniform in the games between teams: 1 and 2, 2 and 1, 2 and 3, 3 and 4, 4 and 2 (the host team is written first)."} +{"id": "00833", "text": "Valera loves his garden, where n fruit trees grow.\n\nThis year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat).\n\nValera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well?\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and v (1 \u2264 n, v \u2264 3000) \u2014 the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. \n\nNext n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 3000) \u2014 the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of fruit that Valera can collect. \n\n\n-----Examples-----\nInput\n2 3\n1 5\n2 3\n\nOutput\n8\n\nInput\n5 10\n3 20\n2 20\n1 20\n4 20\n5 20\n\nOutput\n60\n\n\n\n-----Note-----\n\nIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. \n\nIn the second sample, you can only collect 60 fruits, the remaining fruit will simply wither."} +{"id": "00834", "text": "Some time ago Leonid have known about idempotent functions. Idempotent function defined on a set {1, 2, ..., n} is such function $g : \\{1,2, \\ldots, n \\} \\rightarrow \\{1,2, \\ldots, n \\}$, that for any $x \\in \\{1,2, \\ldots, n \\}$ the formula g(g(x)) = g(x) holds.\n\nLet's denote as f^{(}k)(x) the function f applied k times to the value x. More formally, f^{(1)}(x) = f(x), f^{(}k)(x) = f(f^{(}k - 1)(x)) for each k > 1.\n\nYou are given some function $f : \\{1,2, \\ldots, n \\} \\rightarrow \\{1,2, \\ldots, n \\}$. Your task is to find minimum positive integer k such that function f^{(}k)(x) is idempotent.\n\n\n-----Input-----\n\nIn the first line of the input there is a single integer n (1 \u2264 n \u2264 200) \u2014 the size of function f domain.\n\nIn the second line follow f(1), f(2), ..., f(n) (1 \u2264 f(i) \u2264 n for each 1 \u2264 i \u2264 n), the values of a function.\n\n\n-----Output-----\n\nOutput minimum k such that function f^{(}k)(x) is idempotent.\n\n\n-----Examples-----\nInput\n4\n1 2 2 4\n\nOutput\n1\n\nInput\n3\n2 3 3\n\nOutput\n2\n\nInput\n3\n2 3 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample test function f(x) = f^{(1)}(x) is already idempotent since f(f(1)) = f(1) = 1, f(f(2)) = f(2) = 2, f(f(3)) = f(3) = 2, f(f(4)) = f(4) = 4.\n\nIn the second sample test: function f(x) = f^{(1)}(x) isn't idempotent because f(f(1)) = 3 but f(1) = 2; function f(x) = f^{(2)}(x) is idempotent since for any x it is true that f^{(2)}(x) = 3, so it is also true that f^{(2)}(f^{(2)}(x)) = 3. \n\nIn the third sample test: function f(x) = f^{(1)}(x) isn't idempotent because f(f(1)) = 3 but f(1) = 2; function f(f(x)) = f^{(2)}(x) isn't idempotent because f^{(2)}(f^{(2)}(1)) = 2 but f^{(2)}(1) = 3; function f(f(f(x))) = f^{(3)}(x) is idempotent since it is identity function: f^{(3)}(x) = x for any $x \\in \\{1,2,3 \\}$ meaning that the formula f^{(3)}(f^{(3)}(x)) = f^{(3)}(x) also holds."} +{"id": "00835", "text": "Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite \"Le Hamburger de Polycarpus\" as a string of letters 'B' (bread), 'S' (sausage) \u0438 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe \"\u0412SCBS\" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.\n\nPolycarpus has n_{b} pieces of bread, n_{s} pieces of sausage and n_{c} pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are p_{b} rubles for a piece of bread, p_{s} for a piece of sausage and p_{c} for a piece of cheese.\n\nPolycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.\n\n\n-----Input-----\n\nThe first line of the input contains a non-empty string that describes the recipe of \"Le Hamburger de Polycarpus\". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C).\n\nThe second line contains three integers n_{b}, n_{s}, n_{c} (1 \u2264 n_{b}, n_{s}, n_{c} \u2264 100) \u2014 the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers p_{b}, p_{s}, p_{c} (1 \u2264 p_{b}, p_{s}, p_{c} \u2264 100) \u2014 the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 \u2264 r \u2264 10^12) \u2014 the number of rubles Polycarpus has.\n\nPlease, do not write the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nPrint the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.\n\n\n-----Examples-----\nInput\nBBBSSC\n6 4 1\n1 2 3\n4\n\nOutput\n2\n\nInput\nBBC\n1 10 1\n1 10 1\n21\n\nOutput\n7\n\nInput\nBSC\n1 1 1\n1 1 3\n1000000000000\n\nOutput\n200000000001"} +{"id": "00836", "text": "For he knew every Who down in Whoville beneath, Was busy now, hanging a mistletoe wreath. \"And they're hanging their stockings!\" he snarled with a sneer, \"Tomorrow is Christmas! It's practically here!\"Dr. Suess, How The Grinch Stole Christmas\n\nChristmas celebrations are coming to Whoville. Cindy Lou Who and her parents Lou Lou Who and Betty Lou Who decided to give sweets to all people in their street. They decided to give the residents of each house on the street, one kilogram of sweets. So they need as many kilos of sweets as there are homes on their street.\n\nThe street, where the Lou Who family lives can be represented as n consecutive sections of equal length. You can go from any section to a neighbouring one in one unit of time. Each of the sections is one of three types: an empty piece of land, a house or a shop. Cindy Lou and her family can buy sweets in a shop, but no more than one kilogram of sweets in one shop (the vendors care about the residents of Whoville not to overeat on sweets).\n\nAfter the Lou Who family leave their home, they will be on the first section of the road. To get to this section of the road, they also require one unit of time. We can assume that Cindy and her mom and dad can carry an unlimited number of kilograms of sweets. Every time they are on a house section, they can give a kilogram of sweets to the inhabitants of the house, or they can simply move to another section. If the family have already given sweets to the residents of a house, they can't do it again. Similarly, if they are on the shop section, they can either buy a kilo of sweets in it or skip this shop. If they've bought a kilo of sweets in a shop, the seller of the shop remembered them and the won't sell them a single candy if they come again. The time to buy and give sweets can be neglected. The Lou Whos do not want the people of any house to remain without food.\n\nThe Lou Whos want to spend no more than t time units of time to give out sweets, as they really want to have enough time to prepare for the Christmas celebration. In order to have time to give all the sweets, they may have to initially bring additional k kilos of sweets.\n\nCindy Lou wants to know the minimum number of k kilos of sweets they need to take with them, to have time to give sweets to the residents of each house in their street.\n\nYour task is to write a program that will determine the minimum possible value of k.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and t (2 \u2264 n \u2264 5\u00b710^5, 1 \u2264 t \u2264 10^9). The second line of the input contains n characters, the i-th of them equals \"H\" (if the i-th segment contains a house), \"S\" (if the i-th segment contains a shop) or \".\" (if the i-th segment doesn't contain a house or a shop). \n\nIt is guaranteed that there is at least one segment with a house.\n\n\n-----Output-----\n\nIf there isn't a single value of k that makes it possible to give sweets to everybody in at most t units of time, print in a single line \"-1\" (without the quotes). Otherwise, print on a single line the minimum possible value of k.\n\n\n-----Examples-----\nInput\n6 6\nHSHSHS\n\nOutput\n1\n\nInput\n14 100\n...HHHSSS...SH\n\nOutput\n0\n\nInput\n23 50\nHHSS.......SSHHHHHHHHHH\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first example, there are as many stores, as houses. If the family do not take a single kilo of sweets from home, in order to treat the inhabitants of the first house, they will need to make at least one step back, and they have absolutely no time for it. If they take one kilogram of sweets, they won't need to go back.\n\nIn the second example, the number of shops is equal to the number of houses and plenty of time. Available at all stores passing out candy in one direction and give them when passing in the opposite direction.\n\nIn the third example, the shops on the street are fewer than houses. The Lou Whos have to take the missing number of kilograms of sweets with them from home."} +{"id": "00837", "text": "zscoder wants to generate an input file for some programming competition problem.\n\nHis input is a string consisting of n letters 'a'. He is too lazy to write a generator so he will manually generate the input in a text editor.\n\nInitially, the text editor is empty. It takes him x seconds to insert or delete a letter 'a' from the text file and y seconds to copy the contents of the entire text file, and duplicate it.\n\nzscoder wants to find the minimum amount of time needed for him to create the input file of exactly n letters 'a'. Help him to determine the amount of time needed to generate the input.\n\n\n-----Input-----\n\nThe only line contains three integers n, x and y (1 \u2264 n \u2264 10^7, 1 \u2264 x, y \u2264 10^9) \u2014 the number of letters 'a' in the input file and the parameters from the problem statement.\n\n\n-----Output-----\n\nPrint the only integer t \u2014 the minimum amount of time needed to generate the input file.\n\n\n-----Examples-----\nInput\n8 1 1\n\nOutput\n4\n\nInput\n8 1 10\n\nOutput\n8"} +{"id": "00838", "text": "You are given n \u00d7 m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that:\n\n All cells in a set have the same color. Every two cells in a set share row or column. \n\n\n-----Input-----\n\nThe first line of input contains integers n and m (1 \u2264 n, m \u2264 50)\u00a0\u2014 the number of rows and the number of columns correspondingly.\n\nThe next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black.\n\n\n-----Output-----\n\nOutput single integer \u00a0\u2014 the number of non-empty sets from the problem description.\n\n\n-----Examples-----\nInput\n1 1\n0\n\nOutput\n1\n\nInput\n2 3\n1 0 1\n0 1 0\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets."} +{"id": "00839", "text": "Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. \n\nThere is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.\n\nHaving a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. \n\nLet's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.\n\nWe know that if students i and j talk, then the i-th student's happiness increases by g_{ij} and the j-th student's happiness increases by g_{ji}. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower.\n\n\n-----Input-----\n\nThe input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows g_{ij} (0 \u2264 g_{ij} \u2264 10^5). It is guaranteed that g_{ii} = 0 for all i.\n\nAssume that the students are numbered from 1 to 5.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum possible total happiness of the students.\n\n\n-----Examples-----\nInput\n0 0 0 0 9\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n7 0 0 0 0\n\nOutput\n32\n\nInput\n0 43 21 18 2\n3 0 21 11 65\n5 2 0 1 4\n54 62 12 0 99\n87 64 81 33 0\n\nOutput\n620\n\n\n\n-----Note-----\n\nIn the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals:\n\n(g_23 + g_32 + g_15 + g_51) + (g_13 + g_31 + g_54 + g_45) + (g_15 + g_51) + (g_54 + g_45) = 32."} +{"id": "00840", "text": "The term of this problem is the same as the previous one, the only exception \u2014 increased restrictions.\n\n\n-----Input-----\n\nThe first line contains two positive integers n and k (1 \u2264 n \u2264 100 000, 1 \u2264 k \u2264 10^9) \u2014 the number of ingredients and the number of grams of the magic powder.\n\nThe second line contains the sequence a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.\n\nThe third line contains the sequence b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 10^9), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.\n\n\n-----Output-----\n\nPrint the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.\n\n\n-----Examples-----\nInput\n1 1000000000\n1\n1000000000\n\nOutput\n2000000000\n\nInput\n10 1\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n1 1 1 1 1 1 1 1 1 1\n\nOutput\n0\n\nInput\n3 1\n2 1 4\n11 3 16\n\nOutput\n4\n\nInput\n4 3\n4 3 5 6\n11 12 14 20\n\nOutput\n3"} +{"id": "00841", "text": "The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.\n\nYou are given an integer $n$. \n\nYou have to find a sequence $s$ consisting of digits $\\{1, 3, 7\\}$ such that it has exactly $n$ subsequences equal to $1337$.\n\nFor example, sequence $337133377$ has $6$ subsequences equal to $1337$: $337\\underline{1}3\\underline{3}\\underline{3}7\\underline{7}$ (you can remove the second and fifth characters); $337\\underline{1}\\underline{3}3\\underline{3}7\\underline{7}$ (you can remove the third and fifth characters); $337\\underline{1}\\underline{3}\\underline{3}37\\underline{7}$ (you can remove the fourth and fifth characters); $337\\underline{1}3\\underline{3}\\underline{3}\\underline{7}7$ (you can remove the second and sixth characters); $337\\underline{1}\\underline{3}3\\underline{3}\\underline{7}7$ (you can remove the third and sixth characters); $337\\underline{1}\\underline{3}\\underline{3}3\\underline{7}7$ (you can remove the fourth and sixth characters). \n\nNote that the length of the sequence $s$ must not exceed $10^5$.\n\nYou have to answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10$) \u2014 the number of queries. \n\nNext $t$ lines contains a description of queries: the $i$-th line contains one integer $n_i$ ($1 \\le n_i \\le 10^9$).\n\n\n-----Output-----\n\nFor the $i$-th query print one string $s_i$ ($1 \\le |s_i| \\le 10^5$) consisting of digits $\\{1, 3, 7\\}$. String $s_i$ must have exactly $n_i$ subsequences $1337$. If there are multiple such strings, print any of them.\n\n\n-----Example-----\nInput\n2\n6\n1\n\nOutput\n113337\n1337"} +{"id": "00842", "text": "Pari has a friend who loves palindrome numbers. A palindrome number is a number that reads the same forward or backward. For example 12321, 100001 and 1 are palindrome numbers, while 112 and 1021 are not.\n\nPari is trying to love them too, but only very special and gifted people can understand the beauty behind palindrome numbers. Pari loves integers with even length (i.e. the numbers with even number of digits), so she tries to see a lot of big palindrome numbers with even length (like a 2-digit 11 or 6-digit 122221), so maybe she could see something in them.\n\nNow Pari asks you to write a program that gets a huge integer n from the input and tells what is the n-th even-length positive palindrome number?\n\n\n-----Input-----\n\nThe only line of the input contains a single integer n (1 \u2264 n \u2264 10^{100 000}).\n\n\n-----Output-----\n\nPrint the n-th even-length palindrome number.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n11\n\nInput\n10\n\nOutput\n1001\n\n\n\n-----Note-----\n\nThe first 10 even-length palindrome numbers are 11, 22, 33, ... , 88, 99 and 1001."} +{"id": "00843", "text": "Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him.\n\nThe area looks like a strip of cells 1 \u00d7 n. Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grasshopper stops immediately if it jumps out of the strip. Now Artem wants to find out if this will ever happen.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 length of the strip. \n\nNext line contains a string of length n which consists of characters \"<\" and \">\" only, that provide the direction of the jump from the corresponding cell. Next line contains n integers d_{i} (1 \u2264 d_{i} \u2264 10^9)\u00a0\u2014 the length of the jump from the i-th cell.\n\n\n-----Output-----\n\nPrint \"INFINITE\" (without quotes) if grasshopper will continue his jumps forever. Otherwise print \"FINITE\" (without quotes).\n\n\n-----Examples-----\nInput\n2\n><\n1 2\n\nOutput\nFINITE\n\nInput\n3\n>><\n2 1 1\n\nOutput\nINFINITE\n\n\n-----Note-----\n\nIn the first sample grasshopper starts from the first cell and jumps to the right on the next cell. When he is in the second cell he needs to jump two cells left so he will jump out of the strip.\n\nSecond sample grasshopper path is 1 - 3 - 2 - 3 - 2 - 3 and so on. The path is infinite."} +{"id": "00844", "text": "You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string s_{l}s_{l} + 1s_{l} + 2... s_{r}, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.\n\nYou have to determine the length of the longest balanced substring of s.\n\n\n-----Input-----\n\nThe first line contains n (1 \u2264 n \u2264 100000) \u2014 the number of characters in s.\n\nThe second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.\n\n\n-----Output-----\n\nIf there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.\n\n\n-----Examples-----\nInput\n8\n11010111\n\nOutput\n4\n\nInput\n3\n111\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.\n\nIn the second example it's impossible to find a non-empty balanced substring."} +{"id": "00845", "text": "Our good friend Mole is trying to code a big message. He is typing on an unusual keyboard with characters arranged in following way:\n\nqwertyuiop\n\nasdfghjkl;\n\nzxcvbnm,./\n\n\n\nUnfortunately Mole is blind, so sometimes it is problem for him to put his hands accurately. He accidentally moved both his hands with one position to the left or to the right. That means that now he presses not a button he wants, but one neighboring button (left or right, as specified in input).\n\nWe have a sequence of characters he has typed and we want to find the original message.\n\n\n-----Input-----\n\nFirst line of the input contains one letter describing direction of shifting ('L' or 'R' respectively for left or right).\n\nSecond line contains a sequence of characters written by Mole. The size of this sequence will be no more than 100. Sequence contains only symbols that appear on Mole's keyboard. It doesn't contain spaces as there is no space on Mole's keyboard.\n\nIt is guaranteed that even though Mole hands are moved, he is still pressing buttons on keyboard and not hitting outside it.\n\n\n-----Output-----\n\nPrint a line that contains the original message.\n\n\n-----Examples-----\nInput\nR\ns;;upimrrfod;pbr\n\nOutput\nallyouneedislove"} +{"id": "00846", "text": "Mashmokh works in a factory. At the end of each day he must turn off all of the lights. \n\nThe lights on the factory are indexed from 1 to n. There are n buttons in Mashmokh's room indexed from 1 to n as well. If Mashmokh pushes button with index i, then each light with index not less than i that is still turned on turns off.\n\nMashmokh is not very clever. So instead of pushing the first button he pushes some of the buttons randomly each night. He pushed m distinct buttons b_1, b_2, ..., b_{m} (the buttons were pushed consecutively in the given order) this night. Now he wants to know for each light the index of the button that turned this light off. Please note that the index of button b_{i} is actually b_{i}, not i.\n\nPlease, help Mashmokh, print these indices.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and m (1 \u2264 n, m \u2264 100), the number of the factory lights and the pushed buttons respectively. The next line contains m distinct space-separated integers b_1, b_2, ..., b_{m}\u00a0(1 \u2264 b_{i} \u2264 n).\n\nIt is guaranteed that all lights will be turned off after pushing all buttons.\n\n\n-----Output-----\n\nOutput n space-separated integers where the i-th number is index of the button that turns the i-th light off.\n\n\n-----Examples-----\nInput\n5 4\n4 3 1 2\n\nOutput\n1 1 3 4 4 \n\nInput\n5 5\n5 4 3 2 1\n\nOutput\n1 2 3 4 5 \n\n\n\n-----Note-----\n\nIn the first sample, after pressing button number 4, lights 4 and 5 are turned off and lights 1, 2 and 3 are still on. Then after pressing button number 3, light number 3 is turned off as well. Pressing button number 1 turns off lights number 1 and 2 as well so pressing button number 2 in the end has no effect. Thus button number 4 turned lights 4 and 5 off, button number 3 turned light 3 off and button number 1 turned light 1 and 2 off."} +{"id": "00847", "text": "Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.\n\nNatasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?\n\nYou can assume that initially Vanya had infinitely many cards with each integer number from - x to x.\n\n \n\n\n-----Input-----\n\nThe first line contains two integers: n (1 \u2264 n \u2264 1000) \u2014 the number of found cards and x (1 \u2264 x \u2264 1000) \u2014 the maximum absolute value of the number on a card. The second line contains n space-separated integers \u2014 the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.\n\n\n-----Output-----\n\nPrint a single number \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n3 2\n-1 1 2\n\nOutput\n1\n\nInput\n2 3\n-2 -2\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample, Vanya needs to find a single card with number -2.\n\nIn the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value."} +{"id": "00848", "text": "One day, at the \"Russian Code Cup\" event it was decided to play football as an out of competition event. All participants was divided into n teams and played several matches, two teams could not play against each other more than once.\n\nThe appointed Judge was the most experienced member \u2014 Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches.\n\nPavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly k times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table.\n\n\n-----Input-----\n\nThe first line contains two integers \u2014 n and k (1 \u2264 n, k \u2264 1000).\n\n\n-----Output-----\n\nIn the first line print an integer m \u2014 number of the played games. The following m lines should contain the information about all the matches, one match per line. The i-th line should contain two integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n; a_{i} \u2260 b_{i}). The numbers a_{i} and b_{i} mean, that in the i-th match the team with number a_{i} won against the team with number b_{i}. You can assume, that the teams are numbered from 1 to n.\n\nIf a tournir that meets the conditions of the problem does not exist, then print -1.\n\n\n-----Examples-----\nInput\n3 1\n\nOutput\n3\n1 2\n2 3\n3 1"} +{"id": "00849", "text": "SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to shoot in the target in turns, and SmallR shoots first. The probability of shooting the target each time is $\\frac{a}{b}$ for SmallR while $\\frac{c}{d}$ for Zanoes. The one who shoots in the target first should be the winner.\n\nOutput the probability that SmallR will win the match.\n\n\n-----Input-----\n\nA single line contains four integers $a, b, c, d(1 \\leq a, b, c, d \\leq 1000,0 < \\frac{a}{b} < 1,0 < \\frac{c}{d} < 1)$.\n\n\n-----Output-----\n\nPrint a single real number, the probability that SmallR will win the match.\n\nThe answer will be considered correct if the absolute or relative error doesn't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n1 2 1 2\n\nOutput\n0.666666666667"} +{"id": "00850", "text": "Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 50, but he cannot sum 1 and 4.\n\nVasya has a set of k distinct non-negative integers d_1, d_2, ..., d_{k}.\n\nVasya wants to choose some integers from this set so that he could sum any two chosen numbers. What maximal number of integers can he choose in the required manner?\n\n\n-----Input-----\n\nThe first input line contains integer k (1 \u2264 k \u2264 100) \u2014 the number of integers.\n\nThe second line contains k distinct space-separated integers d_1, d_2, ..., d_{k} (0 \u2264 d_{i} \u2264 100).\n\n\n-----Output-----\n\nIn the first line print a single integer n the maximum number of the chosen integers. In the second line print n distinct non-negative integers \u2014 the required integers.\n\nIf there are multiple solutions, print any of them. You can print the numbers in any order.\n\n\n-----Examples-----\nInput\n4\n100 10 1 0\n\nOutput\n4\n0 1 10 100 \nInput\n3\n2 70 3\n\nOutput\n2\n2 70"} +{"id": "00851", "text": "Polycarp's workday lasts exactly $n$ minutes. He loves chocolate bars and can eat one bar in one minute. Today Polycarp has $k$ bars at the beginning of the workday.\n\nIn some minutes of the workday Polycarp has important things to do and in such minutes he is not able to eat a chocolate bar. In other minutes he can either eat or not eat one chocolate bar. It is guaranteed, that in the first and in the last minutes of the workday Polycarp has no important things to do and he will always eat bars in this minutes to gladden himself at the begining and at the end of the workday. Also it is guaranteed, that $k$ is strictly greater than $1$.\n\nYour task is to determine such an order of eating chocolate bars that the maximum break time between eating bars is as minimum as possible.\n\nConsider that Polycarp eats a bar in the minute $x$ and the next bar in the minute $y$ ($x < y$). Then the break time is equal to $y - x - 1$ minutes. It is not necessary for Polycarp to eat all bars he has.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($2 \\le n \\le 200\\,000$, $2 \\le k \\le n$) \u2014 the length of the workday in minutes and the number of chocolate bars, which Polycarp has in the beginning of the workday.\n\nThe second line contains the string with length $n$ consisting of zeros and ones. If the $i$-th symbol in the string equals to zero, Polycarp has no important things to do in the minute $i$ and he can eat a chocolate bar. In the other case, Polycarp is busy in the minute $i$ and can not eat a chocolate bar. It is guaranteed, that the first and the last characters of the string are equal to zero, and Polycarp always eats chocolate bars in these minutes.\n\n\n-----Output-----\n\nPrint the minimum possible break in minutes between eating chocolate bars.\n\n\n-----Examples-----\nInput\n3 3\n010\n\nOutput\n1\n\nInput\n8 3\n01010110\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example Polycarp can not eat the chocolate bar in the second minute, so the time of the break equals to one minute.\n\nIn the second example Polycarp will eat bars in the minutes $1$ and $8$ anyway, also he needs to eat the chocolate bar in the minute $5$, so that the time of the maximum break will be equal to $3$ minutes."} +{"id": "00852", "text": "The only difference between easy and hard versions is on constraints. In this version constraints are lower. You can make hacks only if all versions of the problem are solved.\n\nKoa the Koala is at the beach!\n\nThe beach consists (from left to right) of a shore, $n+1$ meters of sea and an island at $n+1$ meters from the shore.\n\nShe measured the depth of the sea at $1, 2, \\dots, n$ meters from the shore and saved them in array $d$. $d_i$ denotes the depth of the sea at $i$ meters from the shore for $1 \\le i \\le n$.\n\nLike any beach this one has tide, the intensity of the tide is measured by parameter $k$ and affects all depths from the beginning at time $t=0$ in the following way:\n\n For a total of $k$ seconds, each second, tide increases all depths by $1$.\n\n Then, for a total of $k$ seconds, each second, tide decreases all depths by $1$.\n\n This process repeats again and again (ie. depths increase for $k$ seconds then decrease for $k$ seconds and so on ...).\n\nFormally, let's define $0$-indexed array $p = [0, 1, 2, \\ldots, k - 2, k - 1, k, k - 1, k - 2, \\ldots, 2, 1]$ of length $2k$. At time $t$ ($0 \\le t$) depth at $i$ meters from the shore equals $d_i + p[t \\bmod 2k]$ ($t \\bmod 2k$ denotes the remainder of the division of $t$ by $2k$). Note that the changes occur instantaneously after each second, see the notes for better understanding. \n\nAt time $t=0$ Koa is standing at the shore and wants to get to the island. Suppose that at some time $t$ ($0 \\le t$) she is at $x$ ($0 \\le x \\le n$) meters from the shore:\n\n In one second Koa can swim $1$ meter further from the shore ($x$ changes to $x+1$) or not swim at all ($x$ stays the same), in both cases $t$ changes to $t+1$.\n\n As Koa is a bad swimmer, the depth of the sea at the point where she is can't exceed $l$ at integer points of time (or she will drown). More formally, if Koa is at $x$ ($1 \\le x \\le n$) meters from the shore at the moment $t$ (for some integer $t\\ge 0$), the depth of the sea at this point \u00a0\u2014 $d_x + p[t \\bmod 2k]$ \u00a0\u2014 can't exceed $l$. In other words, $d_x + p[t \\bmod 2k] \\le l$ must hold always.\n\n Once Koa reaches the island at $n+1$ meters from the shore, she stops and can rest.\n\nNote that while Koa swims tide doesn't have effect on her (ie. she can't drown while swimming). Note that Koa can choose to stay on the shore for as long as she needs and neither the shore or the island are affected by the tide (they are solid ground and she won't drown there). \n\nKoa wants to know whether she can go from the shore to the island. Help her!\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u00a0\u2014 the number of test cases. Description of the test cases follows.\n\nThe first line of each test case contains three integers $n$, $k$ and $l$ ($1 \\le n \\le 100; 1 \\le k \\le 100; 1 \\le l \\le 100$)\u00a0\u2014 the number of meters of sea Koa measured and parameters $k$ and $l$.\n\nThe second line of each test case contains $n$ integers $d_1, d_2, \\ldots, d_n$ ($0 \\le d_i \\le 100$) \u00a0\u2014 the depths of each meter of sea Koa measured.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $100$.\n\n\n-----Output-----\n\nFor each test case:\n\nPrint Yes if Koa can get from the shore to the island, and No otherwise.\n\nYou may print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n7\n2 1 1\n1 0\n5 2 3\n1 2 3 2 2\n4 3 4\n0 2 4 3\n2 3 5\n3 0\n7 2 3\n3 0 2 1 3 0 1\n7 1 4\n4 4 3 0 2 4 2\n5 2 3\n1 2 3 2 2\n\nOutput\nYes\nNo\nYes\nYes\nYes\nNo\nNo\n\n\n\n-----Note-----\n\nIn the following $s$ denotes the shore, $i$ denotes the island, $x$ denotes distance from Koa to the shore, the underline denotes the position of Koa, and values in the array below denote current depths, affected by tide, at $1, 2, \\dots, n$ meters from the shore.\n\nIn test case $1$ we have $n = 2, k = 1, l = 1, p = [ 0, 1 ]$.\n\nKoa wants to go from shore (at $x = 0$) to the island (at $x = 3$). Let's describe a possible solution:\n\n Initially at $t = 0$ the beach looks like this: $[\\underline{s}, 1, 0, i]$. At $t = 0$ if Koa would decide to swim to $x = 1$, beach would look like: $[s, \\underline{2}, 1, i]$ at $t = 1$, since $2 > 1$ she would drown. So Koa waits $1$ second instead and beach looks like $[\\underline{s}, 2, 1, i]$ at $t = 1$. At $t = 1$ Koa swims to $x = 1$, beach looks like $[s, \\underline{1}, 0, i]$ at $t = 2$. Koa doesn't drown because $1 \\le 1$. At $t = 2$ Koa swims to $x = 2$, beach looks like $[s, 2, \\underline{1}, i]$ at $t = 3$. Koa doesn't drown because $1 \\le 1$. At $t = 3$ Koa swims to $x = 3$, beach looks like $[s, 1, 0, \\underline{i}]$ at $t = 4$. At $t = 4$ Koa is at $x = 3$ and she made it! \n\nWe can show that in test case $2$ Koa can't get to the island."} +{"id": "00853", "text": "Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and put them in a line so that he gets some number. What is the largest possible number divisible by 90 Jeff can make from the cards he's got?\n\nJeff must make the number without leading zero. At that, we assume that number 0 doesn't contain any leading zeroes. Jeff doesn't have to use all the cards.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^3). The next line contains n integers a_1, a_2, ..., a_{n} (a_{i} = 0 or a_{i} = 5). Number a_{i} represents the digit that is written on the i-th card.\n\n\n-----Output-----\n\nIn a single line print the answer to the problem \u2014 the maximum number, divisible by 90. If you can't make any divisible by 90 number from the cards, print -1.\n\n\n-----Examples-----\nInput\n4\n5 0 5 0\n\nOutput\n0\n\nInput\n11\n5 5 5 5 5 5 5 5 0 5 5\n\nOutput\n5555555550\n\n\n\n-----Note-----\n\nIn the first test you can make only one number that is a multiple of 90 \u2014 0.\n\nIn the second test you can make number 5555555550, it is a multiple of 90."} +{"id": "00854", "text": "XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $n$ booths, arranged in a circle. The booths are numbered $1$ through $n$ clockwise with $n$ being adjacent to $1$. The $i$-th booths sells some candies for the price of $a_i$ burles per item. Each booth has an unlimited supply of candies.\n\nPolycarp has decided to spend at most $T$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $1$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not). \n\nPolycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.\n\nCalculate the number of candies Polycarp will buy.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $T$ ($1 \\le n \\le 2 \\cdot 10^5$, $1 \\le T \\le 10^{18}$) \u2014 the number of booths at the fair and the initial amount of burles Polycarp has.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) \u2014 the price of the single candy at booth number $i$.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the total number of candies Polycarp will buy.\n\n\n-----Examples-----\nInput\n3 38\n5 2 5\n\nOutput\n10\n\nInput\n5 21\n2 4 100 2 6\n\nOutput\n6\n\n\n\n-----Note-----\n\nLet's consider the first example. Here are Polycarp's moves until he runs out of money: Booth $1$, buys candy for $5$, $T = 33$; Booth $2$, buys candy for $2$, $T = 31$; Booth $3$, buys candy for $5$, $T = 26$; Booth $1$, buys candy for $5$, $T = 21$; Booth $2$, buys candy for $2$, $T = 19$; Booth $3$, buys candy for $5$, $T = 14$; Booth $1$, buys candy for $5$, $T = 9$; Booth $2$, buys candy for $2$, $T = 7$; Booth $3$, buys candy for $5$, $T = 2$; Booth $1$, buys no candy, not enough money; Booth $2$, buys candy for $2$, $T = 0$. \n\nNo candy can be bought later. The total number of candies bought is $10$.\n\nIn the second example he has $1$ burle left at the end of his path, no candy can be bought with this amount."} +{"id": "00855", "text": "Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is \"Bob\", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, \"Alice\". \n\nSo now Ilya wants to compare his robots' performance in a simple game called \"1-2-3\". This game is similar to the \"Rock-Paper-Scissors\" game: both robots secretly choose a number from the set {1, 2, 3} and say it at the same moment. If both robots choose the same number, then it's a draw and noone gets any points. But if chosen numbers are different, then one of the robots gets a point: 3 beats 2, 2 beats 1 and 1 beats 3. \n\nBoth robots' programs make them choose their numbers in such a way that their choice in (i + 1)-th game depends only on the numbers chosen by them in i-th game. \n\nIlya knows that the robots will play k games, Alice will choose number a in the first game, and Bob will choose b in the first game. He also knows both robots' programs and can tell what each robot will choose depending on their choices in previous game. Ilya doesn't want to wait until robots play all k games, so he asks you to predict the number of points they will have after the final game. \n\n\n-----Input-----\n\nThe first line contains three numbers k, a, b (1 \u2264 k \u2264 10^18, 1 \u2264 a, b \u2264 3). \n\nThen 3 lines follow, i-th of them containing 3 numbers A_{i}, 1, A_{i}, 2, A_{i}, 3, where A_{i}, j represents Alice's choice in the game if Alice chose i in previous game and Bob chose j (1 \u2264 A_{i}, j \u2264 3). \n\nThen 3 lines follow, i-th of them containing 3 numbers B_{i}, 1, B_{i}, 2, B_{i}, 3, where B_{i}, j represents Bob's choice in the game if Alice chose i in previous game and Bob chose j (1 \u2264 B_{i}, j \u2264 3). \n\n\n-----Output-----\n\nPrint two numbers. First of them has to be equal to the number of points Alice will have, and second of them must be Bob's score after k games.\n\n\n-----Examples-----\nInput\n10 2 1\n1 1 1\n1 1 1\n1 1 1\n2 2 2\n2 2 2\n2 2 2\n\nOutput\n1 9\n\nInput\n8 1 1\n2 2 1\n3 3 1\n3 1 3\n1 1 1\n2 1 1\n1 2 3\n\nOutput\n5 2\n\nInput\n5 1 1\n1 2 2\n2 2 2\n2 2 2\n1 2 2\n2 2 2\n2 2 2\n\nOutput\n0 0\n\n\n\n-----Note-----\n\nIn the second example game goes like this:\n\n$(1,1) \\rightarrow(2,1) \\rightarrow(3,2) \\rightarrow(1,2) \\rightarrow(2,1) \\rightarrow(3,2) \\rightarrow(1,2) \\rightarrow(2,1)$\n\nThe fourth and the seventh game are won by Bob, the first game is draw and the rest are won by Alice."} +{"id": "00856", "text": "Being stuck at home, Ray became extremely bored. To pass time, he asks Lord Omkar to use his time bending power: Infinity Clock! However, Lord Omkar will only listen to mortals who can solve the following problem:\n\nYou are given an array $a$ of $n$ integers. You are also given an integer $k$. Lord Omkar wants you to do $k$ operations with this array.\n\nDefine one operation as the following: Set $d$ to be the maximum value of your array. For every $i$ from $1$ to $n$, replace $a_{i}$ with $d-a_{i}$. \n\nThe goal is to predict the contents in the array after $k$ operations. Please help Ray determine what the final sequence will look like!\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of cases $t$ ($1 \\le t \\le 100$). Description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $k$ ($1 \\leq n \\leq 2 \\cdot 10^5, 1 \\leq k \\leq 10^{18}$) \u2013 the length of your array and the number of operations to perform.\n\nThe second line of each test case contains $n$ integers $a_{1},a_{2},...,a_{n}$ $(-10^9 \\leq a_{i} \\leq 10^9)$ \u2013 the initial contents of your array.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each case, print the final version of array $a$ after $k$ operations described above.\n\n\n-----Example-----\nInput\n3\n2 1\n-199 192\n5 19\n5 -1 4 2 0\n1 2\n69\n\nOutput\n391 0\n0 6 1 3 5\n0\n\n\n\n-----Note-----\n\nIn the first test case the array changes as follows:\n\nInitially, the array is $[-199, 192]$. $d = 192$.\n\nAfter the operation, the array becomes $[d-(-199), d-192] = [391, 0]$."} +{"id": "00857", "text": "You are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits.\n\nSome keys on the keypad have fingerprints. You believe the correct code is the longest not necessarily contiguous subsequence of the sequence you have that only contains digits with fingerprints on the corresponding keys. Find such code.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 10$) representing the number of digits in the sequence you have and the number of keys on the keypad that have fingerprints.\n\nThe next line contains $n$ distinct space-separated integers $x_1, x_2, \\ldots, x_n$ ($0 \\le x_i \\le 9$) representing the sequence.\n\nThe next line contains $m$ distinct space-separated integers $y_1, y_2, \\ldots, y_m$ ($0 \\le y_i \\le 9$) \u2014 the keys with fingerprints.\n\n\n-----Output-----\n\nIn a single line print a space-separated sequence of integers representing the code. If the resulting sequence is empty, both printing nothing and printing a single line break is acceptable.\n\n\n-----Examples-----\nInput\n7 3\n3 5 7 1 6 2 8\n1 2 7\n\nOutput\n7 1 2\n\nInput\n4 4\n3 4 1 0\n0 1 7 9\n\nOutput\n1 0\n\n\n\n-----Note-----\n\nIn the first example, the only digits with fingerprints are $1$, $2$ and $7$. All three of them appear in the sequence you know, $7$ first, then $1$ and then $2$. Therefore the output is 7 1 2. Note that the order is important, and shall be the same as the order in the original sequence.\n\nIn the second example digits $0$, $1$, $7$ and $9$ have fingerprints, however only $0$ and $1$ appear in the original sequence. $1$ appears earlier, so the output is 1 0. Again, the order is important."} +{"id": "00858", "text": "Way to go! Heidi now knows how many brains there must be for her to get one. But throwing herself in the midst of a clutch of hungry zombies is quite a risky endeavor. Hence Heidi wonders: what is the smallest number of brains that must be in the chest for her to get out at all (possibly empty-handed, but alive)?\n\nThe brain dinner night will evolve just as in the previous subtask: the same crowd is present, the N - 1 zombies have the exact same mindset as before and Heidi is to make the first proposal, which must be accepted by at least half of the attendees for her to survive.\n\n\n-----Input-----\n\nThe only line of input contains one integer: N, the number of attendees (1 \u2264 N \u2264 10^9).\n\n\n-----Output-----\n\nOutput one integer: the smallest number of brains in the chest which allows Heidi to merely survive.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n0\n\nInput\n3\n\nOutput\n1\n\nInput\n99\n\nOutput\n49"} +{"id": "00859", "text": "Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.\n\nEach command is one of the following two types: Go 1 unit towards the positive direction, denoted as '+' Go 1 unit towards the negative direction, denoted as '-' \n\nBut the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5). \n\nYou are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?\n\n\n-----Input-----\n\nThe first line contains a string s_1 \u2014 the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}. \n\nThe second line contains a string s_2 \u2014 the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.\n\nLengths of two strings are equal and do not exceed 10.\n\n\n-----Output-----\n\nOutput a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10^{ - 9}.\n\n\n-----Examples-----\nInput\n++-+-\n+-+-+\n\nOutput\n1.000000000000\n\nInput\n+-+-\n+-??\n\nOutput\n0.500000000000\n\nInput\n+++\n??-\n\nOutput\n0.000000000000\n\n\n\n-----Note-----\n\nFor the first sample, both s_1 and s_2 will lead Dreamoon to finish at the same position + 1. \n\nFor the second sample, s_1 will lead Dreamoon to finish at position 0, while there are four possibilites for s_2: {\"+-++\", \"+-+-\", \"+--+\", \"+---\"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5. \n\nFor the third sample, s_2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0."} +{"id": "00860", "text": "On February, 30th n students came in the Center for Training Olympiad Programmers (CTOP) of the Berland State University. They came one by one, one after another. Each of them went in, and before sitting down at his desk, greeted with those who were present in the room by shaking hands. Each of the students who came in stayed in CTOP until the end of the day and never left.\n\nAt any time any three students could join together and start participating in a team contest, which lasted until the end of the day. The team did not distract from the contest for a minute, so when another student came in and greeted those who were present, he did not shake hands with the members of the contest writing team. Each team consisted of exactly three students, and each student could not become a member of more than one team. Different teams could start writing contest at different times.\n\nGiven how many present people shook the hands of each student, get a possible order in which the students could have come to CTOP. If such an order does not exist, then print that this is impossible.\n\nPlease note that some students could work independently until the end of the day, without participating in a team contest.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 2\u00b710^5) \u2014 the number of students who came to CTOP. The next line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} < n), where a_{i} is the number of students with who the i-th student shook hands.\n\n\n-----Output-----\n\nIf the sought order of students exists, print in the first line \"Possible\" and in the second line print the permutation of the students' numbers defining the order in which the students entered the center. Number i that stands to the left of number j in this permutation means that the i-th student came earlier than the j-th student. If there are multiple answers, print any of them.\n\nIf the sought order of students doesn't exist, in a single line print \"Impossible\".\n\n\n-----Examples-----\nInput\n5\n2 1 3 0 1\n\nOutput\nPossible\n4 5 1 3 2 \nInput\n9\n0 2 3 4 1 1 0 2 2\n\nOutput\nPossible\n7 5 2 1 6 8 3 4 9\nInput\n4\n0 2 1 1\n\nOutput\nImpossible\n\n\n\n-----Note-----\n\nIn the first sample from the statement the order of events could be as follows: student 4 comes in (a_4 = 0), he has no one to greet; student 5 comes in (a_5 = 1), he shakes hands with student 4; student 1 comes in (a_1 = 2), he shakes hands with two students (students 4, 5); student 3 comes in (a_3 = 3), he shakes hands with three students (students 4, 5, 1); students 4, 5, 3 form a team and start writing a contest; student 2 comes in (a_2 = 1), he shakes hands with one student (number 1). \n\nIn the second sample from the statement the order of events could be as follows: student 7 comes in (a_7 = 0), he has nobody to greet; student 5 comes in (a_5 = 1), he shakes hands with student 7; student 2 comes in (a_2 = 2), he shakes hands with two students (students 7, 5); students 7, 5, 2 form a team and start writing a contest; student 1 comes in(a_1 = 0), he has no one to greet (everyone is busy with the contest); student 6 comes in (a_6 = 1), he shakes hands with student 1; student 8 comes in (a_8 = 2), he shakes hands with two students (students 1, 6); student 3 comes in (a_3 = 3), he shakes hands with three students (students 1, 6, 8); student 4 comes in (a_4 = 4), he shakes hands with four students (students 1, 6, 8, 3); students 8, 3, 4 form a team and start writing a contest; student 9 comes in (a_9 = 2), he shakes hands with two students (students 1, 6). \n\nIn the third sample from the statement the order of events is restored unambiguously: student 1 comes in (a_1 = 0), he has no one to greet; student 3 comes in (or student 4) (a_3 = a_4 = 1), he shakes hands with student 1; student 2 comes in (a_2 = 2), he shakes hands with two students (students 1, 3 (or 4)); the remaining student 4 (or student 3), must shake one student's hand (a_3 = a_4 = 1) but it is impossible as there are only two scenarios: either a team formed and he doesn't greet anyone, or he greets all the three present people who work individually."} +{"id": "00861", "text": "One beautiful day Vasily the bear painted 2m circles of the same radius R on a coordinate plane. Circles with numbers from 1 to m had centers at points (2R - R, 0), (4R - R, 0), ..., (2Rm - R, 0), respectively. Circles with numbers from m + 1 to 2m had centers at points (2R - R, 2R), (4R - R, 2R), ..., (2Rm - R, 2R), respectively. \n\nNaturally, the bear painted the circles for a simple experiment with a fly. The experiment continued for m^2 days. Each day of the experiment got its own unique number from 0 to m^2 - 1, inclusive. \n\nOn the day number i the following things happened: The fly arrived at the coordinate plane at the center of the circle with number $v = \\lfloor \\frac{i}{m} \\rfloor + 1$ ($\\lfloor \\frac{x}{y} \\rfloor$ is the result of dividing number x by number y, rounded down to an integer). The fly went along the coordinate plane to the center of the circle number $u = m + 1 +(i \\operatorname{mod} m)$ ($x \\text{mod} y$ is the remainder after dividing number x by number y). The bear noticed that the fly went from the center of circle v to the center of circle u along the shortest path with all points lying on the border or inside at least one of the 2m circles. After the fly reached the center of circle u, it flew away in an unknown direction. \n\nHelp Vasily, count the average distance the fly went along the coordinate plane during each of these m^2 days.\n\n\n-----Input-----\n\nThe first line contains two integers m, R (1 \u2264 m \u2264 10^5, 1 \u2264 R \u2264 10).\n\n\n-----Output-----\n\nIn a single line print a single real number \u2014 the answer to the problem. The answer will be considered correct if its absolute or relative error doesn't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n2.0000000000\n\nInput\n2 2\n\nOutput\n5.4142135624\n\n\n\n-----Note-----\n\n[Image]\n\nFigure to the second sample"} +{"id": "00862", "text": "Allen wants to enter a fan zone that occupies a round square and has $n$ entrances.\n\nThere already is a queue of $a_i$ people in front of the $i$-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.\n\nAllen uses the following strategy to enter the fan zone: Initially he stands in the end of the queue in front of the first entrance. Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance). \n\nDetermine the entrance through which Allen will finally enter the fan zone.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 10^5$)\u00a0\u2014 the number of entrances.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 10^9$)\u00a0\u2014 the number of people in queues. These numbers do not include Allen.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of entrance that Allen will use.\n\n\n-----Examples-----\nInput\n4\n2 3 2 0\n\nOutput\n3\n\nInput\n2\n10 10\n\nOutput\n1\n\nInput\n6\n5 2 6 5 7 4\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first example the number of people (not including Allen) changes as follows: $[\\textbf{2}, 3, 2, 0] \\to [1, \\textbf{2}, 1, 0] \\to [0, 1, \\textbf{0}, 0]$. The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance.\n\nIn the second example the number of people (not including Allen) changes as follows: $[\\textbf{10}, 10] \\to [9, \\textbf{9}] \\to [\\textbf{8}, 8] \\to [7, \\textbf{7}] \\to [\\textbf{6}, 6] \\to \\\\ [5, \\textbf{5}] \\to [\\textbf{4}, 4] \\to [3, \\textbf{3}] \\to [\\textbf{2}, 2] \\to [1, \\textbf{1}] \\to [\\textbf{0}, 0]$.\n\nIn the third example the number of people (not including Allen) changes as follows: $[\\textbf{5}, 2, 6, 5, 7, 4] \\to [4, \\textbf{1}, 5, 4, 6, 3] \\to [3, 0, \\textbf{4}, 3, 5, 2] \\to \\\\ [2, 0, 3, \\textbf{2}, 4, 1] \\to [1, 0, 2, 1, \\textbf{3}, 0] \\to [0, 0, 1, 0, 2, \\textbf{0}]$."} +{"id": "00863", "text": "Buses run between the cities A and B, the first one is at 05:00 AM and the last one departs not later than at 11:59 PM. A bus from the city A departs every a minutes and arrives to the city B in a t_{a} minutes, and a bus from the city B departs every b minutes and arrives to the city A in a t_{b} minutes.\n\nThe driver Simion wants to make his job diverse, so he counts the buses going towards him. Simion doesn't count the buses he meet at the start and finish.\n\nYou know the time when Simion departed from the city A to the city B. Calculate the number of buses Simion will meet to be sure in his counting.\n\n\n-----Input-----\n\nThe first line contains two integers a, t_{a} (1 \u2264 a, t_{a} \u2264 120) \u2014 the frequency of the buses from the city A to the city B and the travel time. Both values are given in minutes.\n\nThe second line contains two integers b, t_{b} (1 \u2264 b, t_{b} \u2264 120) \u2014 the frequency of the buses from the city B to the city A and the travel time. Both values are given in minutes.\n\nThe last line contains the departure time of Simion from the city A in the format hh:mm. It is guaranteed that there are a bus from the city A at that time. Note that the hours and the minutes are given with exactly two digits.\n\n\n-----Output-----\n\nPrint the only integer z \u2014 the number of buses Simion will meet on the way. Note that you should not count the encounters in cities A and B.\n\n\n-----Examples-----\nInput\n10 30\n10 35\n05:20\n\nOutput\n5\n\nInput\n60 120\n24 100\n13:00\n\nOutput\n9\n\n\n\n-----Note-----\n\nIn the first example Simion departs form the city A at 05:20 AM and arrives to the city B at 05:50 AM. He will meet the first 5 buses from the city B that departed in the period [05:00 AM - 05:40 AM]. Also Simion will meet a bus in the city B at 05:50 AM, but he will not count it.\n\nAlso note that the first encounter will be between 05:26 AM and 05:27 AM (if we suggest that the buses are go with the sustained speed)."} +{"id": "00864", "text": "Natasha is planning an expedition to Mars for $n$ people. One of the important tasks is to provide food for each participant.\n\nThe warehouse has $m$ daily food packages. Each package has some food type $a_i$.\n\nEach participant must eat exactly one food package each day. Due to extreme loads, each participant must eat the same food type throughout the expedition. Different participants may eat different (or the same) types of food.\n\nFormally, for each participant $j$ Natasha should select his food type $b_j$ and each day $j$-th participant will eat one food package of type $b_j$. The values $b_j$ for different participants may be different.\n\nWhat is the maximum possible number of days the expedition can last, following the requirements above?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 100$, $1 \\le m \\le 100$)\u00a0\u2014 the number of the expedition participants and the number of the daily food packages available.\n\nThe second line contains sequence of integers $a_1, a_2, \\dots, a_m$ ($1 \\le a_i \\le 100$), where $a_i$ is the type of $i$-th food package.\n\n\n-----Output-----\n\nPrint the single integer\u00a0\u2014 the number of days the expedition can last. If it is not possible to plan the expedition for even one day, print 0.\n\n\n-----Examples-----\nInput\n4 10\n1 5 2 1 1 1 2 5 7 2\n\nOutput\n2\n\nInput\n100 1\n1\n\nOutput\n0\n\nInput\n2 5\n5 4 3 2 1\n\nOutput\n1\n\nInput\n3 9\n42 42 42 42 42 42 42 42 42\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example, Natasha can assign type $1$ food to the first participant, the same type $1$ to the second, type $5$ to the third and type $2$ to the fourth. In this case, the expedition can last for $2$ days, since each participant can get two food packages of his food type (there will be used $4$ packages of type $1$, two packages of type $2$ and two packages of type $5$).\n\nIn the second example, there are $100$ participants and only $1$ food package. In this case, the expedition can't last even $1$ day."} +{"id": "00865", "text": "Takahashi is at an all-you-can-eat restaurant.\nThe restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.\nThe restaurant has the following rules:\n - You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.\n - You cannot order the same kind of dish more than once.\n - Until you finish eating the dish already served, you cannot order a new dish.\n - After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.\nLet Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.\nWhat is the maximum possible happiness achieved by making optimal choices?\n\n-----Constraints-----\n - 2 \\leq N \\leq 3000\n - 1 \\leq T \\leq 3000\n - 1 \\leq A_i \\leq 3000\n - 1 \\leq B_i \\leq 3000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN T\nA_1 B_1\n:\nA_N B_N\n\n-----Output-----\nPrint the maximum possible happiness Takahashi can achieve.\n\n-----Sample Input-----\n2 60\n10 10\n100 100\n\n-----Sample Output-----\n110\n\nBy ordering the first and second dishes in this order, Takahashi's happiness will be 110.\nNote that, if we manage to order a dish in time, we can spend any amount of time to eat it."} +{"id": "00866", "text": "There is a knight - the chess piece - at the origin (0, 0) of a two-dimensional grid.\nWhen the knight is at the square (i, j), it can be moved to either (i+1,j+2) or (i+2, j+1).\nIn how many ways can the knight reach the square (X, Y)?\nFind the number of ways modulo 10^9 + 7.\n\n-----Constraints-----\n - 1 \\leq X \\leq 10^6\n - 1 \\leq Y \\leq 10^6\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y\n\n-----Output-----\nPrint the number of ways for the knight to reach (X, Y) from (0, 0), modulo 10^9 + 7.\n\n-----Sample Input-----\n3 3\n\n-----Sample Output-----\n2\n\nThere are two ways: (0,0) \\to (1,2) \\to (3,3) and (0,0) \\to (2,1) \\to (3,3)."} +{"id": "00867", "text": "Tonight is brain dinner night and all zombies will gather together to scarf down some delicious brains. The artful Heidi plans to crash the party, incognito, disguised as one of them. Her objective is to get away with at least one brain, so she can analyze the zombies' mindset back home and gain a strategic advantage.\n\nThey will be N guests tonight: N - 1 real zombies and a fake one, our Heidi. The living-dead love hierarchies as much as they love brains: each one has a unique rank in the range 1 to N - 1, and Heidi, who still appears slightly different from the others, is attributed the highest rank, N. Tonight there will be a chest with brains on display and every attendee sees how many there are. These will then be split among the attendees according to the following procedure:\n\nThe zombie of the highest rank makes a suggestion on who gets how many brains (every brain is an indivisible entity). A vote follows. If at least half of the attendees accept the offer, the brains are shared in the suggested way and the feast begins. But if majority is not reached, then the highest-ranked zombie is killed, and the next zombie in hierarchy has to make a suggestion. If he is killed too, then the third highest-ranked makes one, etc. (It's enough to have exactly half of the votes \u2013 in case of a tie, the vote of the highest-ranked alive zombie counts twice, and he will of course vote in favor of his own suggestion in order to stay alive.)\n\nYou should know that zombies are very greedy and sly, and they know this too \u2013 basically all zombie brains are alike. Consequently, a zombie will never accept an offer which is suboptimal for him. That is, if an offer is not strictly better than a potential later offer, he will vote against it. And make no mistake: while zombies may normally seem rather dull, tonight their intellects are perfect. Each zombie's priorities for tonight are, in descending order: survive the event (they experienced death already once and know it is no fun), get as many brains as possible. \n\nHeidi goes first and must make an offer which at least half of the attendees will accept, and which allocates at least one brain for Heidi herself.\n\nWhat is the smallest number of brains that have to be in the chest for this to be possible?\n\n\n-----Input-----\n\nThe only line of input contains one integer: N, the number of attendees (1 \u2264 N \u2264 10^9).\n\n\n-----Output-----\n\nOutput one integer: the smallest number of brains in the chest which allows Heidi to take one brain home.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n4\n\nOutput\n2\n\n\n\n-----Note-----"} +{"id": "00868", "text": "There exists an island called Arpa\u2019s land, some beautiful girls live there, as ugly ones do.\n\nMehrdad wants to become minister of Arpa\u2019s land. Arpa has prepared an exam. Exam has only one question, given n, print the last digit of 1378^{n}. \n\n [Image] \n\nMehrdad has become quite confused and wants you to help him. Please help, although it's a naive cheat.\n\n\n-----Input-----\n\nThe single line of input contains one integer n (0 \u2264 n \u2264 10^9).\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the last digit of 1378^{n}.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n8\nInput\n2\n\nOutput\n4\n\n\n-----Note-----\n\nIn the first example, last digit of 1378^1 = 1378 is 8.\n\nIn the second example, last digit of 1378^2 = 1378\u00b71378 = 1898884 is 4."} +{"id": "00869", "text": "One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red socks and b blue socks.\n\nAccording to the latest fashion, hipsters should wear the socks of different colors: a red one on the left foot, a blue one on the right foot.\n\nEvery day Vasya puts on new socks in the morning and throws them away before going to bed as he doesn't want to wash them.\n\nVasya wonders, what is the maximum number of days when he can dress fashionable and wear different socks, and after that, for how many days he can then wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got.\n\nCan you help him?\n\n\n-----Input-----\n\nThe single line of the input contains two positive integers a and b (1 \u2264 a, b \u2264 100) \u2014 the number of red and blue socks that Vasya's got.\n\n\n-----Output-----\n\nPrint two space-separated integers \u2014 the maximum number of days when Vasya can wear different socks and the number of days when he can wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got.\n\nKeep in mind that at the end of the day Vasya throws away the socks that he's been wearing on that day.\n\n\n-----Examples-----\nInput\n3 1\n\nOutput\n1 1\n\nInput\n2 3\n\nOutput\n2 0\n\nInput\n7 3\n\nOutput\n3 2\n\n\n\n-----Note-----\n\nIn the first sample Vasya can first put on one pair of different socks, after that he has two red socks left to wear on the second day."} +{"id": "00870", "text": "Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and L, and they move towards each other with speed v_1 and v_2, respectively. Luke has width d and is able to choose any position between the presses. Luke dies as soon as the distance between the presses is less than his width. Your task is to determine for how long Luke can stay alive.\n\n\n-----Input-----\n\nThe first line of the input contains four integers d, L, v_1, v_2 (1 \u2264 d, L, v_1, v_2 \u2264 10 000, d < L)\u00a0\u2014 Luke's width, the initial position of the second press and the speed of the first and second presses, respectively.\n\n\n-----Output-----\n\nPrint a single real value\u00a0\u2014 the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. \n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n2 6 2 2\n\nOutput\n1.00000000000000000000\n\nInput\n1 9 1 2\n\nOutput\n2.66666666666666650000\n\n\n\n-----Note-----\n\nIn the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed.\n\nIn the second sample he needs to occupy the position $[ 2 \\frac{2}{3} ; 3 \\frac{2}{3} ]$. In this case both presses move to his edges at the same time."} +{"id": "00871", "text": "These days Arkady works as an air traffic controller at a large airport. He controls a runway which is usually used for landings only. Thus, he has a schedule of planes that are landing in the nearest future, each landing lasts $1$ minute.\n\nHe was asked to insert one takeoff in the schedule. The takeoff takes $1$ minute itself, but for safety reasons there should be a time space between the takeoff and any landing of at least $s$ minutes from both sides.\n\nFind the earliest time when Arkady can insert the takeoff.\n\n\n-----Input-----\n\nThe first line of input contains two integers $n$ and $s$ ($1 \\le n \\le 100$, $1 \\le s \\le 60$)\u00a0\u2014 the number of landings on the schedule and the minimum allowed time (in minutes) between a landing and a takeoff.\n\nEach of next $n$ lines contains two integers $h$ and $m$ ($0 \\le h \\le 23$, $0 \\le m \\le 59$)\u00a0\u2014 the time, in hours and minutes, when a plane will land, starting from current moment (i.\u00a0e. the current time is $0$ $0$). These times are given in increasing order.\n\n\n-----Output-----\n\nPrint two integers $h$ and $m$\u00a0\u2014 the hour and the minute from the current moment of the earliest time Arkady can insert the takeoff.\n\n\n-----Examples-----\nInput\n6 60\n0 0\n1 20\n3 21\n5 0\n19 30\n23 40\n\nOutput\n6 1\n\nInput\n16 50\n0 30\n1 20\n3 0\n4 30\n6 10\n7 50\n9 30\n11 10\n12 50\n14 30\n16 10\n17 50\n19 30\n21 10\n22 50\n23 59\n\nOutput\n24 50\n\nInput\n3 17\n0 30\n1 0\n12 0\n\nOutput\n0 0\n\n\n\n-----Note-----\n\nIn the first example note that there is not enough time between 1:20 and 3:21, because each landing and the takeoff take one minute.\n\nIn the second example there is no gaps in the schedule, so Arkady can only add takeoff after all landings. Note that it is possible that one should wait more than $24$ hours to insert the takeoff.\n\nIn the third example Arkady can insert the takeoff even between the first landing."} +{"id": "00872", "text": "You're given an array $a$ of length $n$. You can perform the following operation on it as many times as you want: Pick two integers $i$ and $j$ $(1 \\le i,j \\le n)$ such that $a_i+a_j$ is odd, then swap $a_i$ and $a_j$. \n\nWhat is lexicographically the smallest array you can obtain?\n\nAn array $x$ is lexicographically smaller than an array $y$ if there exists an index $i$ such that $x_i 0) followed by k integers v_{i}, 1, v_{i}, 2, ..., v_{i}, k. If v_{i}, j is negative, it means that Rick from universe number - v_{i}, j has joined this group and otherwise it means that Morty from universe number v_{i}, j has joined it.\n\nSum of k for all groups does not exceed 10^4.\n\n\n-----Output-----\n\nIn a single line print the answer to Summer's question. Print \"YES\" if she should cancel the event and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n4 2\n1 -3\n4 -2 3 2 -3\n\nOutput\nYES\n\nInput\n5 2\n5 3 -2 1 -1 5\n3 -5 2 5\n\nOutput\nNO\n\nInput\n7 2\n3 -1 6 7\n7 -5 4 2 4 7 -3 4\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample testcase, 1st group only contains the Rick from universe number 3, so in case he's a traitor, then all members of this group are traitors and so Summer should cancel the event."} +{"id": "00902", "text": "n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.\n\nFor each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.\n\n\n-----Input-----\n\nThe first line contains two integers: n and k (2 \u2264 n \u2264 500, 2 \u2264 k \u2264 10^12)\u00a0\u2014 the number of people and the number of wins.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n) \u2014 powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all a_{i} are distinct.\n\n\n-----Output-----\n\nOutput a single integer \u2014 power of the winner.\n\n\n-----Examples-----\nInput\n2 2\n1 2\n\nOutput\n2 \nInput\n4 2\n3 1 2 4\n\nOutput\n3 \nInput\n6 2\n6 5 3 1 2 4\n\nOutput\n6 \nInput\n2 10000000000\n2 1\n\nOutput\n2\n\n\n\n-----Note-----\n\nGames in the second sample:\n\n3 plays with 1. 3 wins. 1 goes to the end of the line.\n\n3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner."} +{"id": "00903", "text": "You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). \n\nYou want to make the median of the array the largest possible using at most $k$ operations.\n\nThe median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n \\le 2 \\cdot 10^5$, $n$ is odd, $1 \\le k \\le 10^9$)\u00a0\u2014 the number of elements in the array and the largest number of operations you can make.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum possible median after the operations.\n\n\n-----Examples-----\nInput\n3 2\n1 3 5\n\nOutput\n5\nInput\n5 5\n1 2 1 1 1\n\nOutput\n3\nInput\n7 7\n4 1 2 4 3 4 4\n\nOutput\n5\n\n\n-----Note-----\n\nIn the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$.\n\nIn the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$.\n\nIn the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$."} +{"id": "00904", "text": "You are given a text of single-space separated words, consisting of small and capital Latin letters.\n\nVolume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text.\n\nCalculate the volume of the given text.\n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 200) \u2014 length of the text.\n\nThe second line contains text of single-space separated words s_1, s_2, ..., s_{i}, consisting only of small and capital Latin letters.\n\n\n-----Output-----\n\nPrint one integer number \u2014 volume of text.\n\n\n-----Examples-----\nInput\n7\nNonZERO\n\nOutput\n5\n\nInput\n24\nthis is zero answer text\n\nOutput\n0\n\nInput\n24\nHarbour Space University\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example there is only one word, there are 5 capital letters in it.\n\nIn the second example all of the words contain 0 capital letters."} +{"id": "00905", "text": "Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town.\n\nUnfortunately, he has just s dollars for sugar. But that's not a reason to be sad, because there are n types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar).\n\nCaisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change. \n\n\n-----Input-----\n\nThe first line contains two space-separated integers n, s (1 \u2264 n, s \u2264 100).\n\nThe i-th of the next n lines contains two integers x_{i}, y_{i} (1 \u2264 x_{i} \u2264 100;\u00a00 \u2264 y_{i} < 100), where x_{i} represents the number of dollars and y_{i} the number of cents needed in order to buy the i-th type of sugar.\n\n\n-----Output-----\n\nPrint a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar.\n\n\n-----Examples-----\nInput\n5 10\n3 90\n12 0\n9 70\n5 50\n7 0\n\nOutput\n50\n\nInput\n5 5\n10 10\n20 20\n30 30\n40 40\n50 50\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change."} +{"id": "00906", "text": "Ralph has a magic field which is divided into n \u00d7 m blocks. That is to say, there are n rows and m columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works only if the product of integers in each row and each column equals to k, where k is either 1 or -1.\n\nNow Ralph wants you to figure out the number of ways to put numbers in each block in such a way that the magic field works properly. Two ways are considered different if and only if there exists at least one block where the numbers in the first way and in the second way are different. You are asked to output the answer modulo 1000000007 = 10^9 + 7.\n\nNote that there is no range of the numbers to put in the blocks, but we can prove that the answer is not infinity.\n\n\n-----Input-----\n\nThe only line contains three integers n, m and k (1 \u2264 n, m \u2264 10^18, k is either 1 or -1).\n\n\n-----Output-----\n\nPrint a single number denoting the answer modulo 1000000007.\n\n\n-----Examples-----\nInput\n1 1 -1\n\nOutput\n1\n\nInput\n1 3 1\n\nOutput\n1\n\nInput\n3 3 -1\n\nOutput\n16\n\n\n\n-----Note-----\n\nIn the first example the only way is to put -1 into the only block.\n\nIn the second example the only way is to put 1 into every block."} +{"id": "00907", "text": "Toad Ivan has $m$ pairs of integers, each integer is between $1$ and $n$, inclusive. The pairs are $(a_1, b_1), (a_2, b_2), \\ldots, (a_m, b_m)$. \n\nHe asks you to check if there exist two integers $x$ and $y$ ($1 \\leq x < y \\leq n$) such that in each given pair at least one integer is equal to $x$ or $y$.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers $n$ and $m$ ($2 \\leq n \\leq 300\\,000$, $1 \\leq m \\leq 300\\,000$)\u00a0\u2014 the upper bound on the values of integers in the pairs, and the number of given pairs.\n\nThe next $m$ lines contain two integers each, the $i$-th of them contains two space-separated integers $a_i$ and $b_i$ ($1 \\leq a_i, b_i \\leq n, a_i \\neq b_i$)\u00a0\u2014 the integers in the $i$-th pair.\n\n\n-----Output-----\n\nOutput \"YES\" if there exist two integers $x$ and $y$ ($1 \\leq x < y \\leq n$) such that in each given pair at least one integer is equal to $x$ or $y$. Otherwise, print \"NO\". You can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n\nOutput\nNO\n\nInput\n5 4\n1 2\n2 3\n3 4\n4 5\n\nOutput\nYES\n\nInput\n300000 5\n1 2\n1 2\n1 2\n1 2\n1 2\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example, you can't choose any $x$, $y$ because for each such pair you can find a given pair where both numbers are different from chosen integers.\n\nIn the second example, you can choose $x=2$ and $y=4$.\n\nIn the third example, you can choose $x=1$ and $y=2$."} +{"id": "00908", "text": "Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.\n\nVasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).\n\nTo reverse the i-th string Vasiliy has to spent c_{i} units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.\n\nString A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.\n\nFor the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (2 \u2264 n \u2264 100 000)\u00a0\u2014 the number of strings.\n\nThe second line contains n integers c_{i} (0 \u2264 c_{i} \u2264 10^9), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string. \n\nThen follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100 000.\n\n\n-----Output-----\n\nIf it is impossible to reverse some of the strings such that they will be located in lexicographical order, print - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.\n\n\n-----Examples-----\nInput\n2\n1 2\nba\nac\n\nOutput\n1\n\nInput\n3\n1 3 1\naa\nba\nac\n\nOutput\n1\n\nInput\n2\n5 5\nbbb\naaa\n\nOutput\n-1\n\nInput\n2\n3 3\naaa\naa\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.\n\nIn the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is - 1.\n\nIn the fourth sample, both strings consists of characters 'a' only, but in the sorted order string \"aa\" should go before string \"aaa\", thus the answer is - 1."} +{"id": "00909", "text": "Petya studies in a school and he adores Maths. His class has been studying arithmetic expressions. On the last class the teacher wrote three positive integers a, b, c on the blackboard. The task was to insert signs of operations '+' and '*', and probably brackets between the numbers so that the value of the resulting expression is as large as possible. Let's consider an example: assume that the teacher wrote numbers 1, 2 and 3 on the blackboard. Here are some ways of placing signs and brackets: 1+2*3=7 1*(2+3)=5 1*2*3=6 (1+2)*3=9 \n\nNote that you can insert operation signs only between a and b, and between b and c, that is, you cannot swap integers. For instance, in the given sample you cannot get expression (1+3)*2.\n\nIt's easy to see that the maximum value that you can obtain is 9.\n\nYour task is: given a, b and c print the maximum value that you can get.\n\n\n-----Input-----\n\nThe input contains three integers a, b and c, each on a single line (1 \u2264 a, b, c \u2264 10).\n\n\n-----Output-----\n\nPrint the maximum value of the expression that you can obtain.\n\n\n-----Examples-----\nInput\n1\n2\n3\n\nOutput\n9\n\nInput\n2\n10\n3\n\nOutput\n60"} +{"id": "00910", "text": "There are n parliamentarians in Berland. They are numbered with integers from 1 to n. It happened that all parliamentarians with odd indices are Democrats and all parliamentarians with even indices are Republicans.\n\nNew parliament assembly hall is a rectangle consisting of a \u00d7 b chairs\u00a0\u2014 a rows of b chairs each. Two chairs are considered neighbouring if they share as side. For example, chair number 5 in row number 2 is neighbouring to chairs number 4 and 6 in this row and chairs with number 5 in rows 1 and 3. Thus, chairs have four neighbours in general, except for the chairs on the border of the hall\n\nWe know that if two parliamentarians from one political party (that is two Democrats or two Republicans) seat nearby they spent all time discussing internal party issues.\n\nWrite the program that given the number of parliamentarians and the sizes of the hall determine if there is a way to find a seat for any parliamentarian, such that no two members of the same party share neighbouring seats.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, a and b (1 \u2264 n \u2264 10 000, 1 \u2264 a, b \u2264 100)\u00a0\u2014 the number of parliamentarians, the number of rows in the assembly hall and the number of seats in each row, respectively.\n\n\n-----Output-----\n\nIf there is no way to assigns seats to parliamentarians in a proper way print -1.\n\nOtherwise print the solution in a lines, each containing b integers. The j-th integer of the i-th line should be equal to the index of parliamentarian occupying this seat, or 0 if this seat should remain empty. If there are multiple possible solution, you may print any of them.\n\n\n-----Examples-----\nInput\n3 2 2\n\nOutput\n0 3\n1 2\n\nInput\n8 4 3\n\nOutput\n7 8 3\n0 1 4\n6 0 5\n0 2 0\n\nInput\n10 2 2\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample there are many other possible solutions. For example, 3 2\n\n0 1\n\n\n\nand 2 1\n\n3 0\n\n\n\nThe following assignment 3 2\n\n1 0\n\n\n\nis incorrect, because parliamentarians 1 and 3 are both from Democrats party but will occupy neighbouring seats."} +{"id": "00911", "text": "Limak and Radewoosh are going to compete against each other in the upcoming algorithmic contest. They are equally skilled but they won't solve problems in the same order.\n\nThere will be n problems. The i-th problem has initial score p_{i} and it takes exactly t_{i} minutes to solve it. Problems are sorted by difficulty\u00a0\u2014 it's guaranteed that p_{i} < p_{i} + 1 and t_{i} < t_{i} + 1.\n\nA constant c is given too, representing the speed of loosing points. Then, submitting the i-th problem at time x (x minutes after the start of the contest) gives max(0, p_{i} - c\u00b7x) points.\n\nLimak is going to solve problems in order 1, 2, ..., n (sorted increasingly by p_{i}). Radewoosh is going to solve them in order n, n - 1, ..., 1 (sorted decreasingly by p_{i}). Your task is to predict the outcome\u00a0\u2014 print the name of the winner (person who gets more points at the end) or a word \"Tie\" in case of a tie.\n\nYou may assume that the duration of the competition is greater or equal than the sum of all t_{i}. That means both Limak and Radewoosh will accept all n problems.\n\n\n-----Input-----\n\nThe first line contains two integers n and c (1 \u2264 n \u2264 50, 1 \u2264 c \u2264 1000)\u00a0\u2014 the number of problems and the constant representing the speed of loosing points.\n\nThe second line contains n integers p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 1000, p_{i} < p_{i} + 1)\u00a0\u2014 initial scores.\n\nThe third line contains n integers t_1, t_2, ..., t_{n} (1 \u2264 t_{i} \u2264 1000, t_{i} < t_{i} + 1) where t_{i} denotes the number of minutes one needs to solve the i-th problem.\n\n\n-----Output-----\n\nPrint \"Limak\" (without quotes) if Limak will get more points in total. Print \"Radewoosh\" (without quotes) if Radewoosh will get more points in total. Print \"Tie\" (without quotes) if Limak and Radewoosh will get the same total number of points.\n\n\n-----Examples-----\nInput\n3 2\n50 85 250\n10 15 25\n\nOutput\nLimak\n\nInput\n3 6\n50 85 250\n10 15 25\n\nOutput\nRadewoosh\n\nInput\n8 1\n10 20 30 40 50 60 70 80\n8 10 58 63 71 72 75 76\n\nOutput\nTie\n\n\n\n-----Note-----\n\nIn the first sample, there are 3 problems. Limak solves them as follows:\n\n Limak spends 10 minutes on the 1-st problem and he gets 50 - c\u00b710 = 50 - 2\u00b710 = 30 points. Limak spends 15 minutes on the 2-nd problem so he submits it 10 + 15 = 25 minutes after the start of the contest. For the 2-nd problem he gets 85 - 2\u00b725 = 35 points. He spends 25 minutes on the 3-rd problem so he submits it 10 + 15 + 25 = 50 minutes after the start. For this problem he gets 250 - 2\u00b750 = 150 points. \n\nSo, Limak got 30 + 35 + 150 = 215 points.\n\nRadewoosh solves problem in the reversed order:\n\n Radewoosh solves 3-rd problem after 25 minutes so he gets 250 - 2\u00b725 = 200 points. He spends 15 minutes on the 2-nd problem so he submits it 25 + 15 = 40 minutes after the start. He gets 85 - 2\u00b740 = 5 points for this problem. He spends 10 minutes on the 1-st problem so he submits it 25 + 15 + 10 = 50 minutes after the start. He gets max(0, 50 - 2\u00b750) = max(0, - 50) = 0 points. \n\nRadewoosh got 200 + 5 + 0 = 205 points in total. Limak has 215 points so Limak wins.\n\nIn the second sample, Limak will get 0 points for each problem and Radewoosh will first solve the hardest problem and he will get 250 - 6\u00b725 = 100 points for that. Radewoosh will get 0 points for other two problems but he is the winner anyway.\n\nIn the third sample, Limak will get 2 points for the 1-st problem and 2 points for the 2-nd problem. Radewoosh will get 4 points for the 8-th problem. They won't get points for other problems and thus there is a tie because 2 + 2 = 4."} +{"id": "00912", "text": "Sakuzyo - Imprinting\n\nA.R.C. Markland-N is a tall building with $n$ floors numbered from $1$ to $n$. Between each two adjacent floors in the building, there is a staircase connecting them.\n\nIt's lunchtime for our sensei Colin \"ConneR\" Neumann Jr, and he's planning for a location to enjoy his meal.\n\nConneR's office is at floor $s$ of the building. On each floor (including floor $s$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $k$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.\n\nCooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.\n\nPlease answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases in the test. Then the descriptions of $t$ test cases follow.\n\nThe first line of a test case contains three integers $n$, $s$ and $k$ ($2 \\le n \\le 10^9$, $1 \\le s \\le n$, $1 \\le k \\le \\min(n-1, 1000)$)\u00a0\u2014 respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants.\n\nThe second line of a test case contains $k$ distinct integers $a_1, a_2, \\ldots, a_k$ ($1 \\le a_i \\le n$)\u00a0\u2014 the floor numbers of the currently closed restaurants.\n\nIt is guaranteed that the sum of $k$ over all test cases does not exceed $1000$.\n\n\n-----Output-----\n\nFor each test case print a single integer\u00a0\u2014 the minimum number of staircases required for ConneR to walk from the floor $s$ to a floor with an open restaurant.\n\n\n-----Example-----\nInput\n5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77\n\nOutput\n2\n0\n4\n0\n2\n\n\n\n-----Note-----\n\nIn the first example test case, the nearest floor with an open restaurant would be the floor $4$.\n\nIn the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.\n\nIn the third example test case, the closest open restaurant is on the $6$-th floor."} +{"id": "00913", "text": "Polycarp is preparing the first programming contest for robots. There are $n$ problems in it, and a lot of robots are going to participate in it. Each robot solving the problem $i$ gets $p_i$ points, and the score of each robot in the competition is calculated as the sum of $p_i$ over all problems $i$ solved by it. For each problem, $p_i$ is an integer not less than $1$.\n\nTwo corporations specializing in problem-solving robot manufacturing, \"Robo-Coder Inc.\" and \"BionicSolver Industries\", are going to register two robots (one for each corporation) for participation as well. Polycarp knows the advantages and flaws of robots produced by these companies, so, for each problem, he knows precisely whether each robot will solve it during the competition. Knowing this, he can try predicting the results \u2014 or manipulating them. \n\nFor some reason (which absolutely cannot involve bribing), Polycarp wants the \"Robo-Coder Inc.\" robot to outperform the \"BionicSolver Industries\" robot in the competition. Polycarp wants to set the values of $p_i$ in such a way that the \"Robo-Coder Inc.\" robot gets strictly more points than the \"BionicSolver Industries\" robot. However, if the values of $p_i$ will be large, it may look very suspicious \u2014 so Polycarp wants to minimize the maximum value of $p_i$ over all problems. Can you help Polycarp to determine the minimum possible upper bound on the number of points given for solving the problems?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the number of problems.\n\nThe second line contains $n$ integers $r_1$, $r_2$, ..., $r_n$ ($0 \\le r_i \\le 1$). $r_i = 1$ means that the \"Robo-Coder Inc.\" robot will solve the $i$-th problem, $r_i = 0$ means that it won't solve the $i$-th problem.\n\nThe third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($0 \\le b_i \\le 1$). $b_i = 1$ means that the \"BionicSolver Industries\" robot will solve the $i$-th problem, $b_i = 0$ means that it won't solve the $i$-th problem.\n\n\n-----Output-----\n\nIf \"Robo-Coder Inc.\" robot cannot outperform the \"BionicSolver Industries\" robot by any means, print one integer $-1$.\n\nOtherwise, print the minimum possible value of $\\max \\limits_{i = 1}^{n} p_i$, if all values of $p_i$ are set in such a way that the \"Robo-Coder Inc.\" robot gets strictly more points than the \"BionicSolver Industries\" robot.\n\n\n-----Examples-----\nInput\n5\n1 1 1 0 0\n0 1 1 1 1\n\nOutput\n3\n\nInput\n3\n0 0 0\n0 0 0\n\nOutput\n-1\n\nInput\n4\n1 1 1 1\n1 1 1 1\n\nOutput\n-1\n\nInput\n9\n1 0 0 0 0 0 0 0 1\n0 1 1 0 1 1 1 1 0\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, one of the valid score assignments is $p = [3, 1, 3, 1, 1]$. Then the \"Robo-Coder\" gets $7$ points, the \"BionicSolver\" \u2014 $6$ points.\n\nIn the second example, both robots get $0$ points, and the score distribution does not matter.\n\nIn the third example, both robots solve all problems, so their points are equal."} +{"id": "00914", "text": "Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.\n\n\n-----Input-----\n\nThe first line contains string s (1 \u2264 |s| \u2264 1000), consisting of lowercase English characters only. The second line contains an integer n (1 \u2264 n \u2264 1000).\n\n\n-----Output-----\n\nOn the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.\n\n\n-----Examples-----\nInput\nbanana\n4\n\nOutput\n2\nbaan\n\nInput\nbanana\n3\n\nOutput\n3\nnab\n\nInput\nbanana\n2\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the second example, Piegirl can order 3 sheets of stickers with the characters \"nab\". She can take characters \"nab\" from the first sheet, \"na\" from the second, and \"a\" from the third, and arrange them to from \"banana\"."} +{"id": "00915", "text": "Karl likes Codeforces and subsequences. He wants to find a string of lowercase English letters that contains at least $k$ subsequences codeforces. Out of all possible strings, Karl wants to find a shortest one.\n\nFormally, a codeforces subsequence of a string $s$ is a subset of ten characters of $s$ that read codeforces from left to right. For example, codeforces contains codeforces a single time, while codeforcesisawesome contains codeforces four times: codeforcesisawesome, codeforcesisawesome, codeforcesisawesome, codeforcesisawesome.\n\nHelp Karl find any shortest string that contains at least $k$ codeforces subsequences.\n\n\n-----Input-----\n\nThe only line contains a single integer $k$ ($1 \\leq k \\leq 10^{16})$.\n\n\n-----Output-----\n\nPrint a shortest string of lowercase English letters that contains at least $k$ codeforces subsequences. If there are several such strings, print any of them.\n\n\n-----Examples-----\nInput\n1\n\nOutput\ncodeforces\n\nInput\n3\n\nOutput\ncodeforcesss"} +{"id": "00916", "text": "Sagheer is playing a game with his best friend Soliman. He brought a tree with n nodes numbered from 1 to n and rooted at node 1. The i-th node has a_{i} apples. This tree has a special property: the lengths of all paths from the root to any leaf have the same parity (i.e. all paths have even length or all paths have odd length).\n\nSagheer and Soliman will take turns to play. Soliman will make the first move. The player who can't make a move loses.\n\nIn each move, the current player will pick a single node, take a non-empty subset of apples from it and do one of the following two things: eat the apples, if the node is a leaf. move the apples to one of the children, if the node is non-leaf. \n\nBefore Soliman comes to start playing, Sagheer will make exactly one change to the tree. He will pick two different nodes u and v and swap the apples of u with the apples of v.\n\nCan you help Sagheer count the number of ways to make the swap (i.e. to choose u and v) after which he will win the game if both players play optimally? (u, v) and (v, u) are considered to be the same pair.\n\n\n-----Input-----\n\nThe first line will contain one integer n (2 \u2264 n \u2264 10^5) \u2014 the number of nodes in the apple tree.\n\nThe second line will contain n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^7) \u2014 the number of apples on each node of the tree.\n\nThe third line will contain n - 1 integers p_2, p_3, ..., p_{n} (1 \u2264 p_{i} \u2264 n) \u2014 the parent of each node of the tree. Node i has parent p_{i} (for 2 \u2264 i \u2264 n). Node 1 is the root of the tree.\n\nIt is guaranteed that the input describes a valid tree, and the lengths of all paths from the root to any leaf will have the same parity.\n\n\n-----Output-----\n\nOn a single line, print the number of different pairs of nodes (u, v), u \u2260 v such that if they start playing after swapping the apples of both nodes, Sagheer will win the game. (u, v) and (v, u) are considered to be the same pair.\n\n\n-----Examples-----\nInput\n3\n2 2 3\n1 1\n\nOutput\n1\n\nInput\n3\n1 2 3\n1 1\n\nOutput\n0\n\nInput\n8\n7 2 2 5 4 3 1 1\n1 1 1 4 4 5 6\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample, Sagheer can only win if he swapped node 1 with node 3. In this case, both leaves will have 2 apples. If Soliman makes a move in a leaf node, Sagheer can make the same move in the other leaf. If Soliman moved some apples from a root to a leaf, Sagheer will eat those moved apples. Eventually, Soliman will not find a move.\n\nIn the second sample, There is no swap that will make Sagheer win the game.\n\nNote that Sagheer must make the swap even if he can win with the initial tree."} +{"id": "00917", "text": "You are planning to build housing on a street. There are $n$ spots available on the street on which you can build a house. The spots are labeled from $1$ to $n$ from left to right. In each spot, you can build a house with an integer height between $0$ and $h$.\n\nIn each spot, if a house has height $a$, you will gain $a^2$ dollars from it.\n\nThe city has $m$ zoning restrictions. The $i$-th restriction says that the tallest house from spots $l_i$ to $r_i$ (inclusive) must be at most $x_i$.\n\nYou would like to build houses to maximize your profit. Determine the maximum profit possible.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $h$, and $m$ ($1 \\leq n,h,m \\leq 50$)\u00a0\u2014 the number of spots, the maximum height, and the number of restrictions.\n\nEach of the next $m$ lines contains three integers $l_i$, $r_i$, and $x_i$ ($1 \\leq l_i \\leq r_i \\leq n$, $0 \\leq x_i \\leq h$)\u00a0\u2014 left and right limits (inclusive) of the $i$-th restriction and the maximum possible height in that range.\n\n\n-----Output-----\n\nPrint a single integer, the maximum profit you can make.\n\n\n-----Examples-----\nInput\n3 3 3\n1 1 1\n2 2 3\n3 3 2\n\nOutput\n14\n\nInput\n4 10 2\n2 3 8\n3 4 7\n\nOutput\n262\n\n\n\n-----Note-----\n\nIn the first example, there are $3$ houses, the maximum height of a house is $3$, and there are $3$ restrictions. The first restriction says the tallest house between $1$ and $1$ must be at most $1$. The second restriction says the tallest house between $2$ and $2$ must be at most $3$. The third restriction says the tallest house between $3$ and $3$ must be at most $2$.\n\nIn this case, it is optimal to build houses with heights $[1, 3, 2]$. This fits within all the restrictions. The total profit in this case is $1^2 + 3^2 + 2^2 = 14$.\n\nIn the second example, there are $4$ houses, the maximum height of a house is $10$, and there are $2$ restrictions. The first restriction says the tallest house from $2$ to $3$ must be at most $8$. The second restriction says the tallest house from $3$ to $4$ must be at most $7$.\n\nIn this case, it's optimal to build houses with heights $[10, 8, 7, 7]$. We get a profit of $10^2+8^2+7^2+7^2 = 262$. Note that there are two restrictions on house $3$ and both of them must be satisfied. Also, note that even though there isn't any explicit restrictions on house $1$, we must still limit its height to be at most $10$ ($h=10$)."} +{"id": "00918", "text": "Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive.\n\nThe team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest.\n\nYour task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (2 \u2264 n \u2264 100 000, 1 \u2264 m \u2264 10 000, n \u2265 2m)\u00a0\u2014 the number of participants of the qualifying contest and the number of regions in Berland.\n\nNext n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive).\n\nIt is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct.\n\n\n-----Output-----\n\nPrint m lines. On the i-th line print the team of the i-th region\u00a0\u2014 the surnames of the two team members in an arbitrary order, or a single character \"?\" (without the quotes) if you need to spend further qualifying contests in the region.\n\n\n-----Examples-----\nInput\n5 2\nIvanov 1 763\nAndreev 2 800\nPetrov 1 595\nSidorov 1 790\nSemenov 2 503\n\nOutput\nSidorov Ivanov\nAndreev Semenov\n\nInput\n5 2\nIvanov 1 800\nAndreev 2 763\nPetrov 1 800\nSidorov 1 800\nSemenov 2 503\n\nOutput\n?\nAndreev Semenov\n\n\n\n-----Note-----\n\nIn the first sample region teams are uniquely determined.\n\nIn the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: \"Petrov\"-\"Sidorov\", \"Ivanov\"-\"Sidorov\", \"Ivanov\" -\"Petrov\", so it is impossible to determine a team uniquely."} +{"id": "00919", "text": "Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string\u00a0\u2014 concatenation of letters, which correspond to the stages.\n\nThere are $n$ stages available. The rocket must contain exactly $k$ of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter 'c' can't go letters 'a', 'b', 'c' and 'd', but can go letters 'e', 'f', ..., 'z'.\n\nFor the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage 'a 'weighs one ton,' b 'weighs two tons, and' z'\u00a0\u2014 $26$ tons.\n\nBuild the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once.\n\n\n-----Input-----\n\nThe first line of input contains two integers\u00a0\u2014 $n$ and $k$ ($1 \\le k \\le n \\le 50$)\u00a0\u2013 the number of available stages and the number of stages to use in the rocket.\n\nThe second line contains string $s$, which consists of exactly $n$ lowercase Latin letters. Each letter defines a new stage, which can be used to build the rocket. Each stage can be used at most once.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.\n\n\n-----Examples-----\nInput\n5 3\nxyabd\n\nOutput\n29\nInput\n7 4\nproblem\n\nOutput\n34\nInput\n2 2\nab\n\nOutput\n-1\nInput\n12 1\nabaabbaaabbb\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first example, the following rockets satisfy the condition:\n\n\n\n \"adx\" (weight is $1+4+24=29$);\n\n \"ady\" (weight is $1+4+25=30$);\n\n \"bdx\" (weight is $2+4+24=30$);\n\n \"bdy\" (weight is $2+4+25=31$).\n\nRocket \"adx\" has the minimal weight, so the answer is $29$.\n\nIn the second example, target rocket is \"belo\". Its weight is $2+5+12+15=34$.\n\nIn the third example, $n=k=2$, so the rocket must have both stages: 'a' and 'b'. This rocket doesn't satisfy the condition, because these letters are adjacent in the alphabet. Answer is -1."} +{"id": "00920", "text": "Nothing has changed since the last round. Dima and Inna still love each other and want to be together. They've made a deal with Seryozha and now they need to make a deal with the dorm guards...\n\nThere are four guardposts in Dima's dorm. Each post contains two guards (in Russia they are usually elderly women). You can bribe a guard by a chocolate bar or a box of juice. For each guard you know the minimum price of the chocolate bar she can accept as a gift and the minimum price of the box of juice she can accept as a gift. If a chocolate bar for some guard costs less than the minimum chocolate bar price for this guard is, or if a box of juice for some guard costs less than the minimum box of juice price for this guard is, then the guard doesn't accept such a gift.\n\nIn order to pass through a guardpost, one needs to bribe both guards.\n\nThe shop has an unlimited amount of juice and chocolate of any price starting with 1. Dima wants to choose some guardpost, buy one gift for each guard from the guardpost and spend exactly n rubles on it.\n\nHelp him choose a post through which he can safely sneak Inna or otherwise say that this is impossible. Mind you, Inna would be very sorry to hear that!\n\n\n-----Input-----\n\nThe first line of the input contains integer n\u00a0(1 \u2264 n \u2264 10^5) \u2014 the money Dima wants to spend. Then follow four lines describing the guardposts. Each line contains four integers a, b, c, d\u00a0(1 \u2264 a, b, c, d \u2264 10^5) \u2014 the minimum price of the chocolate and the minimum price of the juice for the first guard and the minimum price of the chocolate and the minimum price of the juice for the second guard, correspondingly.\n\n\n-----Output-----\n\nIn a single line of the output print three space-separated integers: the number of the guardpost, the cost of the first present and the cost of the second present. If there is no guardpost Dima can sneak Inna through at such conditions, print -1 in a single line. \n\nThe guardposts are numbered from 1 to 4 according to the order given in the input.\n\nIf there are multiple solutions, you can print any of them.\n\n\n-----Examples-----\nInput\n10\n5 6 5 6\n6 6 7 7\n5 8 6 6\n9 9 9 9\n\nOutput\n1 5 5\n\nInput\n10\n6 6 6 6\n7 7 7 7\n4 4 4 4\n8 8 8 8\n\nOutput\n3 4 6\n\nInput\n5\n3 3 3 3\n3 3 3 3\n3 3 3 3\n3 3 3 3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nExplanation of the first example.\n\nThe only way to spend 10 rubles to buy the gifts that won't be less than the minimum prices is to buy two 5 ruble chocolates to both guards from the first guardpost.\n\nExplanation of the second example.\n\nDima needs 12 rubles for the first guardpost, 14 for the second one, 16 for the fourth one. So the only guardpost we can sneak through is the third one. So, Dima can buy 4 ruble chocolate for the first guard and 6 ruble juice of the second guard."} +{"id": "00921", "text": "Polycarp invited all his friends to the tea party to celebrate the holiday. He has n cups, one for each of his n friends, with volumes a_1, a_2, ..., a_{n}. His teapot stores w milliliters of tea (w \u2264 a_1 + a_2 + ... + a_{n}). Polycarp wants to pour tea in cups in such a way that: Every cup will contain tea for at least half of its volume Every cup will contain integer number of milliliters of tea All the tea from the teapot will be poured into cups All friends will be satisfied. \n\nFriend with cup i won't be satisfied, if there exists such cup j that cup i contains less tea than cup j but a_{i} > a_{j}.\n\nFor each cup output how many milliliters of tea should be poured in it. If it's impossible to pour all the tea and satisfy all conditions then output -1.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and w (1 \u2264 n \u2264 100, $1 \\leq w \\leq \\sum_{i = 1}^{n} a_{i}$).\n\nThe second line contains n numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100).\n\n\n-----Output-----\n\nOutput how many milliliters of tea every cup should contain. If there are multiple answers, print any of them.\n\nIf it's impossible to pour all the tea and satisfy all conditions then output -1.\n\n\n-----Examples-----\nInput\n2 10\n8 7\n\nOutput\n6 4 \n\nInput\n4 4\n1 1 1 1\n\nOutput\n1 1 1 1 \n\nInput\n3 10\n9 8 10\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the third example you should pour to the first cup at least 5 milliliters, to the second one at least 4, to the third one at least 5. It sums up to 14, which is greater than 10 milliliters available."} +{"id": "00922", "text": "Polycarp has n dice d_1, d_2, ..., d_{n}. The i-th dice shows numbers from 1 to d_{i}. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A and the values d_1, d_2, ..., d_{n}. However, she finds it enough to make a series of statements of the following type: dice i couldn't show number r. For example, if Polycarp had two six-faced dice and the total sum is A = 11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).\n\nFor each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is A.\n\n\n-----Input-----\n\nThe first line contains two integers n, A (1 \u2264 n \u2264 2\u00b710^5, n \u2264 A \u2264 s) \u2014 the number of dice and the sum of shown values where s = d_1 + d_2 + ... + d_{n}.\n\nThe second line contains n integers d_1, d_2, ..., d_{n} (1 \u2264 d_{i} \u2264 10^6), where d_{i} is the maximum value that the i-th dice can show.\n\n\n-----Output-----\n\nPrint n integers b_1, b_2, ..., b_{n}, where b_{i} is the number of values for which it is guaranteed that the i-th dice couldn't show them.\n\n\n-----Examples-----\nInput\n2 8\n4 4\n\nOutput\n3 3 \nInput\n1 3\n5\n\nOutput\n4 \nInput\n2 3\n2 3\n\nOutput\n0 1 \n\n\n-----Note-----\n\nIn the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.\n\nIn the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5.\n\nIn the third sample from the statement A equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3."} +{"id": "00923", "text": "Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.\n\nIts most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers from 0 to n - 1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on.\n\nBesides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if n = 5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active.\n\nAndrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0, 1, 2, ..., n - 1. Write a program that determines whether the given puzzle is real or fake.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 1000) \u2014 the number of gears.\n\nThe second line contains n digits a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 n - 1) \u2014 the sequence of active teeth: the active tooth of the i-th gear contains number a_{i}.\n\n\n-----Output-----\n\nIn a single line print \"Yes\" (without the quotes), if the given Stolp's gears puzzle is real, and \"No\" (without the quotes) otherwise.\n\n\n-----Examples-----\nInput\n3\n1 0 0\n\nOutput\nYes\n\nInput\n5\n4 2 1 4 3\n\nOutput\nYes\n\nInput\n4\n0 2 3 1\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2."} +{"id": "00924", "text": "Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days $[l_a; r_a]$ are lucky, then there are some unlucky days: $[r_a + 1; l_a + t_a - 1]$, and then there are lucky days again: $[l_a + t_a; r_a + t_a]$ and so on. In other words, the day is lucky for Alice if it lies in the segment $[l_a + k t_a; r_a + k t_a]$ for some non-negative integer $k$.\n\nThe Bob's lucky day have similar structure, however the parameters of his sequence are different: $l_b$, $r_b$, $t_b$. So a day is a lucky for Bob if it lies in a segment $[l_b + k t_b; r_b + k t_b]$, for some non-negative integer $k$.\n\nAlice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.\n\n\n-----Input-----\n\nThe first line contains three integers $l_a$, $r_a$, $t_a$ ($0 \\le l_a \\le r_a \\le t_a - 1, 2 \\le t_a \\le 10^9$) and describes Alice's lucky days.\n\nThe second line contains three integers $l_b$, $r_b$, $t_b$ ($0 \\le l_b \\le r_b \\le t_b - 1, 2 \\le t_b \\le 10^9$) and describes Bob's lucky days.\n\nIt is guaranteed that both Alice and Bob have some unlucky days.\n\n\n-----Output-----\n\nPrint one integer: the maximum number of days in the row that are lucky for both Alice and Bob.\n\n\n-----Examples-----\nInput\n0 2 5\n1 3 5\n\nOutput\n2\n\nInput\n0 1 3\n2 3 6\n\nOutput\n1\n\n\n\n-----Note-----\n\nThe graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.\n\n[Image]\n\n[Image]"} +{"id": "00925", "text": "Malek lives in an apartment block with 100 floors numbered from 0 to 99. The apartment has an elevator with a digital counter showing the floor that the elevator is currently on. The elevator shows each digit of a number with 7 light sticks by turning them on or off. The picture below shows how the elevator shows each digit.[Image]\n\nOne day when Malek wanted to go from floor 88 to floor 0 using the elevator he noticed that the counter shows number 89 instead of 88. Then when the elevator started moving the number on the counter changed to 87. After a little thinking Malek came to the conclusion that there is only one explanation for this: One of the sticks of the counter was broken. Later that day Malek was thinking about the broken stick and suddenly he came up with the following problem.\n\nSuppose the digital counter is showing number n. Malek calls an integer x (0 \u2264 x \u2264 99) good if it's possible that the digital counter was supposed to show x but because of some(possibly none) broken sticks it's showing n instead. Malek wants to know number of good integers for a specific n. So you must write a program that calculates this number. Please note that the counter always shows two digits.\n\n\n-----Input-----\n\nThe only line of input contains exactly two digits representing number n (0 \u2264 n \u2264 99). Note that n may have a leading zero.\n\n\n-----Output-----\n\nIn the only line of the output print the number of good integers.\n\n\n-----Examples-----\nInput\n89\n\nOutput\n2\n\nInput\n00\n\nOutput\n4\n\nInput\n73\n\nOutput\n15\n\n\n\n-----Note-----\n\nIn the first sample the counter may be supposed to show 88 or 89.\n\nIn the second sample the good integers are 00, 08, 80 and 88.\n\nIn the third sample the good integers are 03, 08, 09, 33, 38, 39, 73, 78, 79, 83, 88, 89, 93, 98, 99."} +{"id": "00926", "text": "Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya's idea and wants to present him a rectangular parallelepiped of marble from which he can carve the sphere. \n\nZahar has n stones which are rectangular parallelepipeds. The edges sizes of the i-th of them are a_{i}, b_{i} and c_{i}. He can take no more than two stones and present them to Kostya. \n\nIf Zahar takes two stones, he should glue them together on one of the faces in order to get a new piece of rectangular parallelepiped of marble. Thus, it is possible to glue a pair of stones together if and only if two faces on which they are glued together match as rectangles. In such gluing it is allowed to rotate and flip the stones in any way. \n\nHelp Zahar choose such a present so that Kostya can carve a sphere of the maximum possible volume and present it to Zahar.\n\n\n-----Input-----\n\nThe first line contains the integer n (1 \u2264 n \u2264 10^5).\n\nn lines follow, in the i-th of which there are three integers a_{i}, b_{i} and c_{i} (1 \u2264 a_{i}, b_{i}, c_{i} \u2264 10^9)\u00a0\u2014 the lengths of edges of the i-th stone. Note, that two stones may have exactly the same sizes, but they still will be considered two different stones.\n\n\n-----Output-----\n\nIn the first line print k (1 \u2264 k \u2264 2) the number of stones which Zahar has chosen. In the second line print k distinct integers from 1 to n\u00a0\u2014 the numbers of stones which Zahar needs to choose. Consider that stones are numbered from 1 to n in the order as they are given in the input data.\n\nYou can print the stones in arbitrary order. If there are several answers print any of them. \n\n\n-----Examples-----\nInput\n6\n5 5 5\n3 2 4\n1 4 1\n2 1 3\n3 2 4\n3 3 4\n\nOutput\n1\n1\n\nInput\n7\n10 7 8\n5 10 3\n4 2 6\n5 5 5\n10 2 8\n4 2 1\n7 7 7\n\nOutput\n2\n1 5\n\n\n\n-----Note-----\n\nIn the first example we can connect the pairs of stones: 2 and 4, the size of the parallelepiped: 3 \u00d7 2 \u00d7 5, the radius of the inscribed sphere 1 2 and 5, the size of the parallelepiped: 3 \u00d7 2 \u00d7 8 or 6 \u00d7 2 \u00d7 4 or 3 \u00d7 4 \u00d7 4, the radius of the inscribed sphere 1, or 1, or 1.5 respectively. 2 and 6, the size of the parallelepiped: 3 \u00d7 5 \u00d7 4, the radius of the inscribed sphere 1.5 4 and 5, the size of the parallelepiped: 3 \u00d7 2 \u00d7 5, the radius of the inscribed sphere 1 5 and 6, the size of the parallelepiped: 3 \u00d7 4 \u00d7 5, the radius of the inscribed sphere 1.5 \n\nOr take only one stone: 1 the size of the parallelepiped: 5 \u00d7 5 \u00d7 5, the radius of the inscribed sphere 2.5 2 the size of the parallelepiped: 3 \u00d7 2 \u00d7 4, the radius of the inscribed sphere 1 3 the size of the parallelepiped: 1 \u00d7 4 \u00d7 1, the radius of the inscribed sphere 0.5 4 the size of the parallelepiped: 2 \u00d7 1 \u00d7 3, the radius of the inscribed sphere 0.5 5 the size of the parallelepiped: 3 \u00d7 2 \u00d7 4, the radius of the inscribed sphere 1 6 the size of the parallelepiped: 3 \u00d7 3 \u00d7 4, the radius of the inscribed sphere 1.5 \n\nIt is most profitable to take only the first stone."} +{"id": "00927", "text": "Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:\n - Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \\leq A_i \\leq 9).\n - The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 10^4\n - 1 \\leq M \\leq 9\n - 1 \\leq A_i \\leq 9\n - A_i are all different.\n - There exists an integer that can be formed by exactly N matchsticks under the conditions.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 A_2 ... A_M\n\n-----Output-----\nPrint the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.\n\n-----Sample Input-----\n20 4\n3 7 8 4\n\n-----Sample Output-----\n777773\n\nThe integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions."} +{"id": "00928", "text": "You are given a sequence of positive integers of length N, A=a_1,a_2,\u2026,a_{N}, and an integer K.\nHow many contiguous subsequences of A satisfy the following condition?\n - (Condition) The sum of the elements in the contiguous subsequence is at least K.\nWe consider two contiguous subsequences different if they derive from different positions in A, even if they are the same in content.\nNote that the answer may not fit into a 32-bit integer type.\n\n-----Constraints-----\n - 1 \\leq a_i \\leq 10^5\n - 1 \\leq N \\leq 10^5\n - 1 \\leq K \\leq 10^{10}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the number of contiguous subsequences of A that satisfy the condition.\n\n-----Sample Input-----\n4 10\n6 1 2 7\n\n-----Sample Output-----\n2\n\nThe following two contiguous subsequences satisfy the condition:\n - A[1..4]=a_1,a_2,a_3,a_4, with the sum of 16\n - A[2..4]=a_2,a_3,a_4, with the sum of 10"} +{"id": "00929", "text": "There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i-th row and the j-th column will be denoted as Cell (i, j).\nIn Cell (i, j), a_{ij} coins are placed.\nYou can perform the following operation any number of times:\nOperation: Choose a cell that was not chosen before and contains one or more coins, then move one of those coins to a vertically or horizontally adjacent cell.\nMaximize the number of cells containing an even number of coins.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq H, W \\leq 500\n - 0 \\leq a_{ij} \\leq 9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\na_{11} a_{12} ... a_{1W}\na_{21} a_{22} ... a_{2W}\n:\na_{H1} a_{H2} ... a_{HW}\n\n-----Output-----\nPrint a sequence of operations that maximizes the number of cells containing an even number of coins, in the following format:\nN\ny_1 x_1 y_1' x_1'\ny_2 x_2 y_2' x_2'\n:\ny_N x_N y_N' x_N'\n\nThat is, in the first line, print an integer N between 0 and H \\times W (inclusive), representing the number of operations.\nIn the (i+1)-th line (1 \\leq i \\leq N), print four integers y_i, x_i, y_i' and x_i' (1 \\leq y_i, y_i' \\leq H and 1 \\leq x_i, x_i' \\leq W), representing the i-th operation. These four integers represents the operation of moving one of the coins placed in Cell (y_i, x_i) to a vertically or horizontally adjacent cell, (y_i', x_i').\nNote that if the specified operation violates the specification in the problem statement or the output format is invalid, it will result in Wrong Answer.\n\n-----Sample Input-----\n2 3\n1 2 3\n0 1 1\n\n-----Sample Output-----\n3\n2 2 2 3\n1 1 1 2\n1 3 1 2\n\nEvery cell contains an even number of coins after the following sequence of operations:\n - Move the coin in Cell (2, 2) to Cell (2, 3).\n - Move the coin in Cell (1, 1) to Cell (1, 2).\n - Move one of the coins in Cell (1, 3) to Cell (1, 2)."} +{"id": "00930", "text": "There is a building with n rooms, numbered 1 to n.\nWe can move from any room to any other room in the building.\nLet us call the following event a move: a person in some room i goes to another room j~ (i \\neq j).\nInitially, there was one person in each room in the building.\nAfter that, we know that there were exactly k moves happened up to now.\nWe are interested in the number of people in each of the n rooms now. How many combinations of numbers of people in the n rooms are possible?\nFind the count modulo (10^9 + 7).\n\n-----Constraints-----\n - All values in input are integers.\n - 3 \\leq n \\leq 2 \\times 10^5\n - 2 \\leq k \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn k\n\n-----Output-----\nPrint the number of possible combinations of numbers of people in the n rooms now, modulo (10^9 + 7).\n\n-----Sample Input-----\n3 2\n\n-----Sample Output-----\n10\n\nLet c_1, c_2, and c_3 be the number of people in Room 1, 2, and 3 now, respectively. There are 10 possible combination of (c_1, c_2, c_3):\n - (0, 0, 3)\n - (0, 1, 2)\n - (0, 2, 1)\n - (0, 3, 0)\n - (1, 0, 2)\n - (1, 1, 1)\n - (1, 2, 0)\n - (2, 0, 1)\n - (2, 1, 0)\n - (3, 0, 0)\nFor example, (c_1, c_2, c_3) will be (0, 1, 2) if the person in Room 1 goes to Room 2 and then one of the persons in Room 2 goes to Room 3."} +{"id": "00931", "text": "Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns \u2014 from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (x_{k}, y_{k}).\n\nThe time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. [Image] \n\nInna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!\n\n\n-----Input-----\n\nThe first line of the input contains fix integers n, m, x, y, z, p (1 \u2264 n, m \u2264 10^9;\u00a00 \u2264 x, y, z \u2264 10^9;\u00a01 \u2264 p \u2264 10^5).\n\nEach of the following p lines contains two integers x_{k}, y_{k} (1 \u2264 x_{k} \u2264 n;\u00a01 \u2264 y_{k} \u2264 m) \u2014 the initial coordinates of the k-th candy. Two candies can lie on the same cell.\n\n\n-----Output-----\n\nFor each of the p candies, print on a single line its space-separated new coordinates.\n\n\n-----Examples-----\nInput\n3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3\n\nOutput\n1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1\n\n\n\n-----Note-----\n\nJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:\n\nQWER REWQ \n\nASDF -> FDSA\n\nZXCV VCXZ"} +{"id": "00932", "text": "Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:\n\n$a_{1} OR a_{2} OR \\ldots OR a_{k}$ where $a_{i} \\in \\{0,1 \\}$ is equal to 1 if some a_{i} = 1, otherwise it is equal to 0.\n\nNam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 \u2264 i \u2264 m) and column j (1 \u2264 j \u2264 n) is denoted as A_{ij}. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:\n\n[Image].\n\n(B_{ij} is OR of all elements in row i and column j of matrix A)\n\nNam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.\n\n\n-----Input-----\n\nThe first line contains two integer m and n (1 \u2264 m, n \u2264 100), number of rows and number of columns of matrices respectively.\n\nThe next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).\n\n\n-----Output-----\n\nIn the first line, print \"NO\" if Nam has made a mistake when calculating B, otherwise print \"YES\". If the first line is \"YES\", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.\n\n\n-----Examples-----\nInput\n2 2\n1 0\n0 0\n\nOutput\nNO\n\nInput\n2 3\n1 1 1\n1 1 1\n\nOutput\nYES\n1 1 1\n1 1 1\n\nInput\n2 3\n0 1 0\n1 1 1\n\nOutput\nYES\n0 0 0\n0 1 0"} +{"id": "00933", "text": "Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos.\n\nIn this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word \"helllo\" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words \"helloo\" and \"wwaatt\" contain typos).\n\nWrite a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word.\n\n\n-----Input-----\n\nThe single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters.\n\n\n-----Output-----\n\nPrint such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\nhelloo\n\nOutput\nhello\n\nInput\nwoooooow\n\nOutput\nwoow\n\n\n\n-----Note-----\n\nThe second valid answer to the test from the statement is \"heloo\"."} +{"id": "00934", "text": "Gennady owns a small hotel in the countryside where he lives a peaceful life. He loves to take long walks, watch sunsets and play cards with tourists staying in his hotel. His favorite game is called \"Mau-Mau\".\n\nTo play Mau-Mau, you need a pack of $52$ cards. Each card has a suit (Diamonds \u2014 D, Clubs \u2014 C, Spades \u2014 S, or Hearts \u2014 H), and a rank (2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, or A).\n\nAt the start of the game, there is one card on the table and you have five cards in your hand. You can play a card from your hand if and only if it has the same rank or the same suit as the card on the table.\n\nIn order to check if you'd be a good playing partner, Gennady has prepared a task for you. Given the card on the table and five cards in your hand, check if you can play at least one card.\n\n\n-----Input-----\n\nThe first line of the input contains one string which describes the card on the table. The second line contains five strings which describe the cards in your hand.\n\nEach string is two characters long. The first character denotes the rank and belongs to the set $\\{{\\tt 2}, {\\tt 3}, {\\tt 4}, {\\tt 5}, {\\tt 6}, {\\tt 7}, {\\tt 8}, {\\tt 9}, {\\tt T}, {\\tt J}, {\\tt Q}, {\\tt K}, {\\tt A}\\}$. The second character denotes the suit and belongs to the set $\\{{\\tt D}, {\\tt C}, {\\tt S}, {\\tt H}\\}$.\n\nAll the cards in the input are different.\n\n\n-----Output-----\n\nIf it is possible to play a card from your hand, print one word \"YES\". Otherwise, print \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\nAS\n2H 4C TH JH AD\n\nOutput\nYES\n\nInput\n2H\n3D 4C AC KD AS\n\nOutput\nNO\n\nInput\n4D\nAS AC AD AH 5H\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example, there is an Ace of Spades (AS) on the table. You can play an Ace of Diamonds (AD) because both of them are Aces.\n\nIn the second example, you cannot play any card.\n\nIn the third example, you can play an Ace of Diamonds (AD) because it has the same suit as a Four of Diamonds (4D), which lies on the table."} +{"id": "00935", "text": "After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of n horizontal and m vertical sticks.\n\nAn intersection point is any point on the grid which is formed by the intersection of one horizontal stick and one vertical stick.\n\nIn the grid shown below, n = 3 and m = 3. There are n + m = 6 sticks in total (horizontal sticks are shown in red and vertical sticks are shown in green). There are n\u00b7m = 9 intersection points, numbered from 1 to 9.\n\n [Image] \n\nThe rules of the game are very simple. The players move in turns. Akshat won gold, so he makes the first move. During his/her move, a player must choose any remaining intersection point and remove from the grid all sticks which pass through this point. A player will lose the game if he/she cannot make a move (i.e. there are no intersection points remaining on the grid at his/her move).\n\nAssume that both players play optimally. Who will win the game?\n\n\n-----Input-----\n\nThe first line of input contains two space-separated integers, n and m (1 \u2264 n, m \u2264 100).\n\n\n-----Output-----\n\nPrint a single line containing \"Akshat\" or \"Malvika\" (without the quotes), depending on the winner of the game.\n\n\n-----Examples-----\nInput\n2 2\n\nOutput\nMalvika\n\nInput\n2 3\n\nOutput\nMalvika\n\nInput\n3 3\n\nOutput\nAkshat\n\n\n\n-----Note-----\n\nExplanation of the first sample:\n\nThe grid has four intersection points, numbered from 1 to 4.\n\n [Image] \n\nIf Akshat chooses intersection point 1, then he will remove two sticks (1 - 2 and 1 - 3). The resulting grid will look like this.\n\n [Image] \n\nNow there is only one remaining intersection point (i.e. 4). Malvika must choose it and remove both remaining sticks. After her move the grid will be empty.\n\nIn the empty grid, Akshat cannot make any move, hence he will lose.\n\nSince all 4 intersection points of the grid are equivalent, Akshat will lose no matter which one he picks."} +{"id": "00936", "text": "After celebrating the midcourse the students of one of the faculties of the Berland State University decided to conduct a vote for the best photo. They published the photos in the social network and agreed on the rules to choose a winner: the photo which gets most likes wins. If multiple photoes get most likes, the winner is the photo that gets this number first.\n\nHelp guys determine the winner photo by the records of likes.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 1000) \u2014 the total likes to the published photoes. \n\nThe second line contains n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1 000 000), where a_{i} is the identifier of the photo which got the i-th like.\n\n\n-----Output-----\n\nPrint the identifier of the photo which won the elections.\n\n\n-----Examples-----\nInput\n5\n1 3 2 2 1\n\nOutput\n2\n\nInput\n9\n100 200 300 200 100 300 300 100 200\n\nOutput\n300\n\n\n\n-----Note-----\n\nIn the first test sample the photo with id 1 got two likes (first and fifth), photo with id 2 got two likes (third and fourth), and photo with id 3 got one like (second). \n\nThus, the winner is the photo with identifier 2, as it got: more likes than the photo with id 3; as many likes as the photo with id 1, but the photo with the identifier 2 got its second like earlier."} +{"id": "00937", "text": "Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells a_{i} theorems during the i-th minute.\n\nMishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then t_{i} will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told \u2014 a_{i} during the i-th minute. Otherwise he writes nothing.\n\nYou know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that $j \\in [ i, i + k - 1 ]$ and will write down all the theorems lecturer tells.\n\nYou task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.\n\n\n-----Input-----\n\nThe first line of the input contains two integer numbers n and k (1 \u2264 k \u2264 n \u2264 10^5) \u2014 the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.\n\nThe second line of the input contains n integer numbers a_1, a_2, ... a_{n} (1 \u2264 a_{i} \u2264 10^4) \u2014 the number of theorems lecturer tells during the i-th minute.\n\nThe third line of the input contains n integer numbers t_1, t_2, ... t_{n} (0 \u2264 t_{i} \u2264 1) \u2014 type of Mishka's behavior at the i-th minute of the lecture.\n\n\n-----Output-----\n\nPrint only one integer \u2014 the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.\n\n\n-----Example-----\nInput\n6 3\n1 3 5 2 5 4\n1 1 0 1 0 0\n\nOutput\n16\n\n\n\n-----Note-----\n\nIn the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16."} +{"id": "00938", "text": "In a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decided to spend the remaining time wisely.\n\nLesha knows that today he can study for at most $a$ hours, and he will have $b$ hours to study tomorrow. Note that it is possible that on his planet there are more hours in a day than on Earth. Lesha knows that the quality of his knowledge will only depend on the number of lecture notes he will read. He has access to an infinite number of notes that are enumerated with positive integers, but he knows that he can read the first note in one hour, the second note in two hours and so on. In other words, Lesha can read the note with number $k$ in $k$ hours. Lesha can read the notes in arbitrary order, however, he can't start reading a note in the first day and finish its reading in the second day.\n\nThus, the student has to fully read several lecture notes today, spending at most $a$ hours in total, and fully read several lecture notes tomorrow, spending at most $b$ hours in total. What is the maximum number of notes Lesha can read in the remaining time? Which notes should he read in the first day, and which\u00a0\u2014 in the second?\n\n\n-----Input-----\n\nThe only line of input contains two integers $a$ and $b$ ($0 \\leq a, b \\leq 10^{9}$)\u00a0\u2014 the number of hours Lesha has today and the number of hours Lesha has tomorrow.\n\n\n-----Output-----\n\nIn the first line print a single integer $n$ ($0 \\leq n \\leq a$)\u00a0\u2014 the number of lecture notes Lesha has to read in the first day. In the second line print $n$ distinct integers $p_1, p_2, \\ldots, p_n$ ($1 \\leq p_i \\leq a$), the sum of all $p_i$ should not exceed $a$.\n\nIn the third line print a single integer $m$ ($0 \\leq m \\leq b$)\u00a0\u2014 the number of lecture notes Lesha has to read in the second day. In the fourth line print $m$ distinct integers $q_1, q_2, \\ldots, q_m$ ($1 \\leq q_i \\leq b$), the sum of all $q_i$ should not exceed $b$.\n\nAll integers $p_i$ and $q_i$ should be distinct. The sum $n + m$ should be largest possible.\n\n\n-----Examples-----\nInput\n3 3\n\nOutput\n1\n3 \n2\n2 1 \nInput\n9 12\n\nOutput\n2\n3 6\n4\n1 2 4 5\n\n\n\n-----Note-----\n\nIn the first example Lesha can read the third note in $3$ hours in the first day, and the first and the second notes in one and two hours correspondingly in the second day, spending $3$ hours as well. Note that Lesha can make it the other way round, reading the first and the second notes in the first day and the third note in the second day.\n\nIn the second example Lesha should read the third and the sixth notes in the first day, spending $9$ hours in total. In the second day Lesha should read the first, second fourth and fifth notes, spending $12$ hours in total."} +{"id": "00939", "text": "In Berland, there is the national holiday coming \u2014 the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions: overall, there must be m dances; exactly three people must take part in each dance; each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland). \n\nThe agency has n dancers, and their number can be less than 3m. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance. \n\nYou considered all the criteria and made the plan for the m dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the n dancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n (3 \u2264 n \u2264 10^5) and m (1 \u2264 m \u2264 10^5) \u2014 the number of dancers and the number of dances, correspondingly. Then m lines follow, describing the dances in the order of dancing them. The i-th line contains three distinct integers \u2014 the numbers of the dancers that take part in the i-th dance. The dancers are numbered from 1 to n. Each dancer takes part in at least one dance.\n\n\n-----Output-----\n\nPrint n space-separated integers: the i-th number must represent the color of the i-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists.\n\n\n-----Examples-----\nInput\n7 3\n1 2 3\n1 4 5\n4 6 7\n\nOutput\n1 2 3 3 2 2 1 \n\nInput\n9 3\n3 6 9\n2 5 8\n1 4 7\n\nOutput\n1 1 1 2 2 2 3 3 3 \n\nInput\n5 2\n4 1 5\n3 1 2\n\nOutput\n2 3 1 1 3"} +{"id": "00940", "text": "Masha has three sticks of length $a$, $b$ and $c$ centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks.\n\nWhat is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices.\n\n\n-----Input-----\n\nThe only line contains tree integers $a$, $b$ and $c$ ($1 \\leq a, b, c \\leq 100$)\u00a0\u2014 the lengths of sticks Masha possesses.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks.\n\n\n-----Examples-----\nInput\n3 4 5\n\nOutput\n0\n\nInput\n2 5 3\n\nOutput\n1\n\nInput\n100 10 10\n\nOutput\n81\n\n\n\n-----Note-----\n\nIn the first example, Masha can make a triangle from the sticks without increasing the length of any of them.\n\nIn the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length $2$ centimeter stick by one and after that form a triangle with sides $3$, $3$ and $5$ centimeters.\n\nIn the third example, Masha can take $33$ minutes to increase one of the $10$ centimeters sticks by $33$ centimeters, and after that take $48$ minutes to increase another $10$ centimeters stick by $48$ centimeters. This way she can form a triangle with lengths $43$, $58$ and $100$ centimeters in $81$ minutes. One can show that it is impossible to get a valid triangle faster."} +{"id": "00941", "text": "You are given an integer $n$ ($n \\ge 0$) represented with $k$ digits in base (radix) $b$. So,\n\n$$n = a_1 \\cdot b^{k-1} + a_2 \\cdot b^{k-2} + \\ldots a_{k-1} \\cdot b + a_k.$$\n\nFor example, if $b=17, k=3$ and $a=[11, 15, 7]$ then $n=11\\cdot17^2+15\\cdot17+7=3179+255+7=3441$.\n\nDetermine whether $n$ is even or odd.\n\n\n-----Input-----\n\nThe first line contains two integers $b$ and $k$ ($2\\le b\\le 100$, $1\\le k\\le 10^5$)\u00a0\u2014 the base of the number and the number of digits.\n\nThe second line contains $k$ integers $a_1, a_2, \\ldots, a_k$ ($0\\le a_i < b$)\u00a0\u2014 the digits of $n$.\n\nThe representation of $n$ contains no unnecessary leading zero. That is, $a_1$ can be equal to $0$ only if $k = 1$.\n\n\n-----Output-----\n\nPrint \"even\" if $n$ is even, otherwise print \"odd\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n13 3\n3 2 7\n\nOutput\neven\n\nInput\n10 9\n1 2 3 4 5 6 7 8 9\n\nOutput\nodd\n\nInput\n99 5\n32 92 85 74 4\n\nOutput\nodd\n\nInput\n2 2\n1 0\n\nOutput\neven\n\n\n\n-----Note-----\n\nIn the first example, $n = 3 \\cdot 13^2 + 2 \\cdot 13 + 7 = 540$, which is even.\n\nIn the second example, $n = 123456789$ is odd.\n\nIn the third example, $n = 32 \\cdot 99^4 + 92 \\cdot 99^3 + 85 \\cdot 99^2 + 74 \\cdot 99 + 4 = 3164015155$ is odd.\n\nIn the fourth example $n = 2$."} +{"id": "00942", "text": "Chouti and his classmates are going to the university soon. To say goodbye to each other, the class has planned a big farewell party in which classmates, teachers and parents sang and danced.\n\nChouti remembered that $n$ persons took part in that party. To make the party funnier, each person wore one hat among $n$ kinds of weird hats numbered $1, 2, \\ldots n$. It is possible that several persons wore hats of the same kind. Some kinds of hats can remain unclaimed by anyone.\n\nAfter the party, the $i$-th person said that there were $a_i$ persons wearing a hat differing from his own.\n\nIt has been some days, so Chouti forgot all about others' hats, but he is curious about that. Let $b_i$ be the number of hat type the $i$-th person was wearing, Chouti wants you to find any possible $b_1, b_2, \\ldots, b_n$ that doesn't contradict with any person's statement. Because some persons might have a poor memory, there could be no solution at all.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$), the number of persons in the party.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le n-1$), the statements of people.\n\n\n-----Output-----\n\nIf there is no solution, print a single line \"Impossible\".\n\nOtherwise, print \"Possible\" and then $n$ integers $b_1, b_2, \\ldots, b_n$ ($1 \\le b_i \\le n$).\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n3\n0 0 0\n\nOutput\nPossible\n1 1 1 \nInput\n5\n3 3 2 2 2\n\nOutput\nPossible\n1 1 2 2 2 \nInput\n4\n0 1 2 3\n\nOutput\nImpossible\n\n\n\n-----Note-----\n\nIn the answer to the first example, all hats are the same, so every person will say that there were no persons wearing a hat different from kind $1$.\n\nIn the answer to the second example, the first and the second person wore the hat with type $1$ and all other wore a hat of type $2$.\n\nSo the first two persons will say there were three persons with hats differing from their own. Similarly, three last persons will say there were two persons wearing a hat different from their own.\n\nIn the third example, it can be shown that no solution exists.\n\nIn the first and the second example, other possible configurations are possible."} +{"id": "00943", "text": "Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark. \n\nNote, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0.\n\n\n-----Input-----\n\nThe first line of the input contains one integer, n (1 \u2264 n \u2264 100 000). The next line contains n space separated integers given to Wet Shark. Each of these integers is in range from 1 to 10^9, inclusive. \n\n\n-----Output-----\n\nPrint the maximum possible even sum that can be obtained if we use some of the given integers. \n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n6\nInput\n5\n999999999 999999999 999999999 999999999 999999999\n\nOutput\n3999999996\n\n\n-----Note-----\n\nIn the first sample, we can simply take all three integers for a total sum of 6.\n\nIn the second sample Wet Shark should take any four out of five integers 999 999 999."} +{"id": "00944", "text": "In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.\n\nIn each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.\n\nDetermine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.\n\n\n-----Input-----\n\nThe first line of the input contains a positive integer n (2 \u2264 n \u2264 200 000)\u00a0\u2014 the number of cities in Berland.\n\nEach of the next n - 1 lines contains two numbers u_{i}, v_{i}, meaning that the i-th road connects city u_{i} and city v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i}).\n\n\n-----Output-----\n\nFirst print number k\u00a0\u2014 the minimum number of days needed to repair all the roads in Berland.\n\nIn next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number d_{i} \u2014 the number of roads that should be repaired on the i-th day, and then d_{i} space-separated integers \u2014 the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.\n\nIf there are multiple variants, you can print any of them.\n\n\n-----Examples-----\nInput\n4\n1 2\n3 4\n3 2\n\nOutput\n2\n2 2 1\n1 3\n\nInput\n6\n3 4\n5 4\n3 2\n1 3\n4 6\n\nOutput\n3\n1 1 \n2 2 3 \n2 4 5 \n\n\n\n-----Note-----\n\nIn the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 \u2014 on the second day."} +{"id": "00945", "text": "Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.\n\nThe teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x_1, 0) and (x_2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). [Image] \n\nSeryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^3). The second line contains n distinct integers x_1, x_2, ..., x_{n} ( - 10^6 \u2264 x_{i} \u2264 10^6) \u2014 the i-th point has coordinates (x_{i}, 0). The points are not necessarily sorted by their x coordinate.\n\n\n-----Output-----\n\nIn the single line print \"yes\" (without the quotes), if the line has self-intersections. Otherwise, print \"no\" (without the quotes).\n\n\n-----Examples-----\nInput\n4\n0 10 5 15\n\nOutput\nyes\n\nInput\n4\n0 15 5 10\n\nOutput\nno\n\n\n\n-----Note-----\n\nThe first test from the statement is on the picture to the left, the second test is on the picture to the right."} +{"id": "00946", "text": "When preparing a tournament, Codeforces coordinators try treir best to make the first problem as easy as possible. This time the coordinator had chosen some problem and asked $n$ people about their opinions. Each person answered whether this problem is easy or hard.\n\nIf at least one of these $n$ people has answered that the problem is hard, the coordinator decides to change the problem. For the given responses, check if the problem is easy enough.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 100$) \u2014 the number of people who were asked to give their opinions.\n\nThe second line contains $n$ integers, each integer is either $0$ or $1$. If $i$-th integer is $0$, then $i$-th person thinks that the problem is easy; if it is $1$, then $i$-th person thinks that the problem is hard.\n\n\n-----Output-----\n\nPrint one word: \"EASY\" if the problem is easy according to all responses, or \"HARD\" if there is at least one person who thinks the problem is hard. \n\nYou may print every letter in any register: \"EASY\", \"easy\", \"EaSY\" and \"eAsY\" all will be processed correctly.\n\n\n-----Examples-----\nInput\n3\n0 0 1\n\nOutput\nHARD\n\nInput\n1\n0\n\nOutput\nEASY\n\n\n\n-----Note-----\n\nIn the first example the third person says it's a hard problem, so it should be replaced.\n\nIn the second example the problem easy for the only person, so it doesn't have to be replaced."} +{"id": "00947", "text": "In Omkar's last class of math, he learned about the least common multiple, or $LCM$. $LCM(a, b)$ is the smallest positive integer $x$ which is divisible by both $a$ and $b$.\n\nOmkar, having a laudably curious mind, immediately thought of a problem involving the $LCM$ operation: given an integer $n$, find positive integers $a$ and $b$ such that $a + b = n$ and $LCM(a, b)$ is the minimum value possible.\n\nCan you help Omkar solve his ludicrously challenging math problem?\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\leq t \\leq 10$). Description of the test cases follows.\n\nEach test case consists of a single integer $n$ ($2 \\leq n \\leq 10^{9}$).\n\n\n-----Output-----\n\nFor each test case, output two positive integers $a$ and $b$, such that $a + b = n$ and $LCM(a, b)$ is the minimum possible.\n\n\n-----Example-----\nInput\n3\n4\n6\n9\n\nOutput\n2 2\n3 3\n3 6\n\n\n\n-----Note-----\n\nFor the first test case, the numbers we can choose are $1, 3$ or $2, 2$. $LCM(1, 3) = 3$ and $LCM(2, 2) = 2$, so we output $2 \\ 2$.\n\nFor the second test case, the numbers we can choose are $1, 5$, $2, 4$, or $3, 3$. $LCM(1, 5) = 5$, $LCM(2, 4) = 4$, and $LCM(3, 3) = 3$, so we output $3 \\ 3$.\n\nFor the third test case, $LCM(3, 6) = 6$. It can be shown that there are no other pairs of numbers which sum to $9$ that have a lower $LCM$."} +{"id": "00948", "text": "The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them. \n\nIn this problem an image is a rectangular table that consists of lowercase Latin letters. A face on the image is a 2 \u00d7 2 square, such that from the four letters of this square you can make word \"face\". \n\nYou need to write a program that determines the number of faces on the image. The squares that correspond to the faces can overlap.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers, n and m (1 \u2264 n, m \u2264 50) \u2014 the height and the width of the image, respectively.\n\nNext n lines define the image. Each line contains m lowercase Latin letters.\n\n\n-----Output-----\n\nIn the single line print the number of faces on the image.\n\n\n-----Examples-----\nInput\n4 4\nxxxx\nxfax\nxcex\nxxxx\n\nOutput\n1\n\nInput\n4 2\nxx\ncf\nae\nxx\n\nOutput\n1\n\nInput\n2 3\nfac\ncef\n\nOutput\n2\n\nInput\n1 4\nface\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample the image contains a single face, located in a square with the upper left corner at the second line and the second column: [Image] \n\nIn the second sample the image also contains exactly one face, its upper left corner is at the second row and the first column.\n\nIn the third sample two faces are shown: $\\text{fac}$ \n\nIn the fourth sample the image has no faces on it."} +{"id": "00949", "text": "Greatest common divisor GCD(a, b) of two positive integers a and b is equal to the biggest integer d such that both integers a and b are divisible by d. There are many efficient algorithms to find greatest common divisor GCD(a, b), for example, Euclid algorithm. \n\nFormally, find the biggest integer d, such that all integers a, a + 1, a + 2, ..., b are divisible by d. To make the problem even more complicated we allow a and b to be up to googol, 10^100\u00a0\u2014 such number do not fit even in 64-bit integer type!\n\n\n-----Input-----\n\nThe only line of the input contains two integers a and b (1 \u2264 a \u2264 b \u2264 10^100).\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 greatest common divisor of all integers from a to b inclusive.\n\n\n-----Examples-----\nInput\n1 2\n\nOutput\n1\n\nInput\n61803398874989484820458683436563811772030917980576 61803398874989484820458683436563811772030917980576\n\nOutput\n61803398874989484820458683436563811772030917980576"} +{"id": "00950", "text": "After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements: There is at least one digit in the string, There is at least one lowercase (small) letter of the Latin alphabet in the string, There is at least one of three listed symbols in the string: '#', '*', '&'. [Image] \n\nConsidering that these are programming classes it is not easy to write the password.\n\nFor each character of the password we have a fixed string of length m, on each of these n strings there is a pointer on some character. The i-th character displayed on the screen is the pointed character in the i-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one).\n\nDuring one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index m, and when we move it to the right from the position m it moves to the position 1.\n\nYou need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password. \n\n\n-----Input-----\n\nThe first line contains two integers n, m (3 \u2264 n \u2264 50, 1 \u2264 m \u2264 50) \u2014 the length of the password and the length of strings which are assigned to password symbols. \n\nEach of the next n lines contains the string which is assigned to the i-th symbol of the password string. Its length is m, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.\n\nYou have such input data that you can always get a valid password.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password. \n\n\n-----Examples-----\nInput\n3 4\n1**2\na3*0\nc4**\n\nOutput\n1\n\nInput\n5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first test it is necessary to move the pointer of the third string to one left to get the optimal answer. [Image] \n\nIn the second test one of possible algorithms will be: to move the pointer of the second symbol once to the right. to move the pointer of the third symbol twice to the right. [Image]"} +{"id": "00951", "text": "Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.\n\nYou have to find the minimum number of digits in which these two numbers can differ.\n\n\n-----Input-----\n\nThe first line contains integer k (1 \u2264 k \u2264 10^9).\n\nThe second line contains integer n (1 \u2264 n < 10^100000).\n\nThere are no leading zeros in n. It's guaranteed that this situation is possible.\n\n\n-----Output-----\n\nPrint the minimum number of digits in which the initial number and n can differ.\n\n\n-----Examples-----\nInput\n3\n11\n\nOutput\n1\n\nInput\n3\n99\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, the initial number could be 12.\n\nIn the second example the sum of the digits of n is not less than k. The initial number could be equal to n."} +{"id": "00952", "text": "Andrew prefers taxi to other means of transport, but recently most taxi drivers have been acting inappropriately. In order to earn more money, taxi drivers started to drive in circles. Roads in Andrew's city are one-way, and people are not necessary able to travel from one part to another, but it pales in comparison to insidious taxi drivers.\n\nThe mayor of the city decided to change the direction of certain roads so that the taxi drivers wouldn't be able to increase the cost of the trip endlessly. More formally, if the taxi driver is on a certain crossroads, they wouldn't be able to reach it again if he performs a nonzero trip. \n\nTraffic controllers are needed in order to change the direction the road goes. For every road it is known how many traffic controllers are needed to change the direction of the road to the opposite one. It is allowed to change the directions of roads one by one, meaning that each traffic controller can participate in reversing two or more roads.\n\nYou need to calculate the minimum number of traffic controllers that you need to hire to perform the task and the list of the roads that need to be reversed.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($2 \\leq n \\leq 100\\,000$, $1 \\leq m \\leq 100\\,000$)\u00a0\u2014 the number of crossroads and the number of roads in the city, respectively.\n\nEach of the following $m$ lines contain three integers $u_{i}$, $v_{i}$ and $c_{i}$ ($1 \\leq u_{i}, v_{i} \\leq n$, $1 \\leq c_{i} \\leq 10^9$, $u_{i} \\ne v_{i}$)\u00a0\u2014 the crossroads the road starts at, the crossroads the road ends at and the number of traffic controllers required to reverse this road.\n\n\n-----Output-----\n\nIn the first line output two integers the minimal amount of traffic controllers required to complete the task and amount of roads $k$ which should be reversed. $k$ should not be minimized.\n\nIn the next line output $k$ integers separated by spaces \u2014 numbers of roads, the directions of which should be reversed. The roads are numerated from $1$ in the order they are written in the input. If there are many solutions, print any of them.\n\n\n-----Examples-----\nInput\n5 6\n2 1 1\n5 2 6\n2 3 2\n3 4 3\n4 5 5\n1 5 4\n\nOutput\n2 2\n1 3 \nInput\n5 7\n2 1 5\n3 2 3\n1 3 3\n2 4 1\n4 3 5\n5 4 1\n1 5 3\n\nOutput\n3 3\n3 4 7 \n\n\n-----Note-----\n\nThere are two simple cycles in the first example: $1 \\rightarrow 5 \\rightarrow 2 \\rightarrow 1$ and $2 \\rightarrow 3 \\rightarrow 4 \\rightarrow 5 \\rightarrow 2$. One traffic controller can only reverse the road $2 \\rightarrow 1$ and he can't destroy the second cycle by himself. Two traffic controllers can reverse roads $2 \\rightarrow 1$ and $2 \\rightarrow 3$ which would satisfy the condition.\n\nIn the second example one traffic controller can't destroy the cycle $ 1 \\rightarrow 3 \\rightarrow 2 \\rightarrow 1 $. With the help of three controllers we can, for example, reverse roads $1 \\rightarrow 3$ ,$ 2 \\rightarrow 4$, $1 \\rightarrow 5$."} +{"id": "00953", "text": "User ainta has a permutation p_1, p_2, ..., p_{n}. As the New Year is coming, he wants to make his permutation as pretty as possible.\n\nPermutation a_1, a_2, ..., a_{n} is prettier than permutation b_1, b_2, ..., b_{n}, if and only if there exists an integer k (1 \u2264 k \u2264 n) where a_1 = b_1, a_2 = b_2, ..., a_{k} - 1 = b_{k} - 1 and a_{k} < b_{k} all holds.\n\nAs known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n \u00d7 n binary matrix A, user ainta can swap the values of p_{i} and p_{j} (1 \u2264 i, j \u2264 n, i \u2260 j) if and only if A_{i}, j = 1.\n\nGiven the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 300) \u2014 the size of the permutation p.\n\nThe second line contains n space-separated integers p_1, p_2, ..., p_{n} \u2014 the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation.\n\nNext n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line A_{i}, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 \u2264 i < j \u2264 n, A_{i}, j = A_{j}, i holds. Also, for all integers i where 1 \u2264 i \u2264 n, A_{i}, i = 0 holds.\n\n\n-----Output-----\n\nIn the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.\n\n\n-----Examples-----\nInput\n7\n5 2 4 3 6 7 1\n0001001\n0000000\n0000010\n1000001\n0000000\n0010000\n1001000\n\nOutput\n1 2 4 3 6 7 5\n\nInput\n5\n4 2 1 5 3\n00100\n00011\n10010\n01101\n01010\n\nOutput\n1 2 3 4 5\n\n\n\n-----Note-----\n\nIn the first sample, the swap needed to obtain the prettiest permutation is: (p_1, p_7).\n\nIn the second sample, the swaps needed to obtain the prettiest permutation is (p_1, p_3), (p_4, p_5), (p_3, p_4). [Image] \n\nA permutation p is a sequence of integers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as p_{i}. The size of the permutation p is denoted as n."} +{"id": "00954", "text": "Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Being a dutiful student, he immediately learns how to spell the word.\n\nHongcow has decided to try to make new words from this one. He starts by taking the word he just learned how to spell, and moves the last character of the word to the beginning of the word. He calls this a cyclic shift. He can apply cyclic shift many times. For example, consecutively applying cyclic shift operation to the word \"abracadabra\" Hongcow will get words \"aabracadabr\", \"raabracadab\" and so on.\n\nHongcow is now wondering how many distinct words he can generate by doing the cyclic shift arbitrarily many times. The initial string is also counted.\n\n\n-----Input-----\n\nThe first line of input will be a single string s (1 \u2264 |s| \u2264 50), the word Hongcow initially learns how to spell. The string s consists only of lowercase English letters ('a'\u2013'z').\n\n\n-----Output-----\n\nOutput a single integer equal to the number of distinct strings that Hongcow can obtain by applying the cyclic shift arbitrarily many times to the given string.\n\n\n-----Examples-----\nInput\nabcd\n\nOutput\n4\n\nInput\nbbb\n\nOutput\n1\n\nInput\nyzyz\n\nOutput\n2\n\n\n\n-----Note-----\n\nFor the first sample, the strings Hongcow can generate are \"abcd\", \"dabc\", \"cdab\", and \"bcda\".\n\nFor the second sample, no matter how many times Hongcow does the cyclic shift, Hongcow can only generate \"bbb\".\n\nFor the third sample, the two strings Hongcow can generate are \"yzyz\" and \"zyzy\"."} +{"id": "00955", "text": "Berland shop sells $n$ kinds of juices. Each juice has its price $c_i$. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin \"A\", vitamin \"B\" and vitamin \"C\". Each juice can contain one, two or all three types of vitamins in it.\n\nPetya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ $(1 \\le n \\le 1\\,000)$ \u2014 the number of juices.\n\nEach of the next $n$ lines contains an integer $c_i$ $(1 \\le c_i \\le 100\\,000)$ and a string $s_i$ \u2014 the price of the $i$-th juice and the vitamins it contains. String $s_i$ contains from $1$ to $3$ characters, and the only possible characters are \"A\", \"B\" and \"C\". It is guaranteed that each letter appears no more than once in each string $s_i$. The order of letters in strings $s_i$ is arbitrary.\n\n\n-----Output-----\n\nPrint -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins.\n\n\n-----Examples-----\nInput\n4\n5 C\n6 B\n16 BAC\n4 A\n\nOutput\n15\n\nInput\n2\n10 AB\n15 BA\n\nOutput\n-1\n\nInput\n5\n10 A\n9 BC\n11 CA\n4 A\n5 B\n\nOutput\n13\n\nInput\n6\n100 A\n355 BCA\n150 BC\n160 AC\n180 B\n190 CA\n\nOutput\n250\n\nInput\n2\n5 BA\n11 CB\n\nOutput\n16\n\n\n\n-----Note-----\n\nIn the first example Petya buys the first, the second and the fourth juice. He spends $5 + 6 + 4 = 15$ and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is $16$, which isn't optimal.\n\nIn the second example Petya can't obtain all three vitamins, as no juice contains vitamin \"C\"."} +{"id": "00956", "text": "\u041e\u0441\u043d\u043e\u0432\u043e\u0439 \u043b\u044e\u0431\u043e\u0439 \u0441\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u0434\u0440\u0443\u0436\u0431\u044b \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u0432 \u0442\u043e\u043c \u0438\u043b\u0438 \u0438\u043d\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435. \u0412 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0439 \u0441\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u0434\u0440\u0443\u0436\u0431\u0430 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0435\u0441\u043b\u0438 a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u043c b, \u0442\u043e b \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u043c a. \n\n\u0412 \u044d\u0442\u043e\u0439 \u0436\u0435 \u0441\u0435\u0442\u0438 \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043b\u044e\u0434\u0435\u0439, \u0438\u043c\u0435\u044e\u0449\u0438\u0445 \u0432\u044b\u0441\u043e\u043a\u0443\u044e \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0431\u044b\u0442\u044c \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u043c\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0417\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f x. \u041f\u0443\u0441\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a y, \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u043c x \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0447\u0435\u043c \u0434\u043b\u044f k% \u0434\u0440\u0443\u0437\u0435\u0439 x. \u0422\u043e\u0433\u0434\u0430 \u043e\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u043c \u0434\u0440\u0443\u0433\u043e\u043c \u0434\u043b\u044f x.\n\n\u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0432 \u0441\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0439 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u2014 \u044d\u0442\u043e \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u0442 1 \u0434\u043e 10^9. \u0412\u0430\u043c \u0434\u0430\u043d \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u0440 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u0434\u0440\u0443\u0437\u044c\u044f\u043c\u0438. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0434\u0440\u0443\u0437\u0435\u0439.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 m \u0438 k (1 \u2264 m \u2264 100, 0 \u2264 k \u2264 100) \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u0440 \u0434\u0440\u0443\u0437\u0435\u0439 \u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u043d\u0442 \u043e\u0431\u0449\u0438\u0445 \u0434\u0440\u0443\u0437\u0435\u0439 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0447\u0438\u0442\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u043c \u0434\u0440\u0443\u0433\u043e\u043c.\n\n\u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 m \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043f\u043e \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430 a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 10^9, a_{i} \u2260 b_{i}), \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0445 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u0434\u0440\u0443\u0437\u044c\u044f\u043c\u0438. \n\n\u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u0430\u044f \u043f\u0430\u0440\u0430 \u043b\u044e\u0434\u0435\u0439 \u0444\u0438\u0433\u0443\u0440\u0438\u0440\u0443\u0435\u0442 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0430.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0445 \u043b\u044e\u0434\u0435\u0439 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044f id \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0434\u0440\u0443\u0437\u044c\u044f\u0445. \u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u043c\u0435\u0442\u044c \u0432\u0438\u0434 \"id: \u00a0k\u00a0id_1\u00a0id_2\u00a0...\u00a0id_{k}\", \u0433\u0434\u0435 id \u2014 \u044d\u0442\u043e id \u0441\u0430\u043c\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, k \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0434\u0440\u0443\u0437\u0435\u0439, \u0430 id_1, id_2, ..., id_{k} \u2014 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0434\u0440\u0443\u0437\u0435\u0439 \u0432 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u044e\u0449\u0435\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435. \n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n5 51\n10 23\n23 42\n39 42\n10 39\n39 58\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n10: 1 42\n23: 1 39\n39: 1 23\n42: 1 10\n58: 2 10 42\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n5 100\n1 2\n1 3\n1 4\n2 3\n2 4\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1: 0\n2: 0\n3: 1 4\n4: 1 3"} +{"id": "00957", "text": "As it's the first of April, Heidi is suspecting that the news she reads today are fake, and she does not want to look silly in front of all the contestants. She knows that a newspiece is fake if it contains heidi as a subsequence. Help Heidi assess whether the given piece is true, but please be discreet about it...\n\n\n-----Input-----\n\nThe first and only line of input contains a single nonempty string s of length at most 1000 composed of lowercase letters (a-z).\n\n\n-----Output-----\n\nOutput YES if the string s contains heidi as a subsequence and NO otherwise.\n\n\n-----Examples-----\nInput\nabcheaibcdi\n\nOutput\nYES\nInput\nhiedi\n\nOutput\nNO\n\n\n-----Note-----\n\nA string s contains another string p as a subsequence if it is possible to delete some characters from s and obtain p."} +{"id": "00958", "text": "Limak is a little polar bear. He likes nice strings \u2014 strings of length n, consisting of lowercase English letters only.\n\nThe distance between two letters is defined as the difference between their positions in the alphabet. For example, $\\operatorname{dist}(c, e) = \\operatorname{dist}(e, c) = 2$, and $\\operatorname{dist}(a, z) = \\operatorname{dist}(z, a) = 25$.\n\nAlso, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, $\\operatorname{dist}(a f, d b) = \\operatorname{dist}(a, d) + \\operatorname{dist}(f, b) = 3 + 4 = 7$, and $\\text{dist(bear, roar)} = 16 + 10 + 0 + 0 = 26$.\n\nLimak gives you a nice string s and an integer k. He challenges you to find any nice string s' that $\\operatorname{dist}(s, s^{\\prime}) = k$. Find any s' satisfying the given conditions, or print \"-1\" if it's impossible to do so.\n\nAs input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 10^5, 0 \u2264 k \u2264 10^6).\n\nThe second line contains a string s of length n, consisting of lowercase English letters.\n\n\n-----Output-----\n\nIf there is no string satisfying the given conditions then print \"-1\" (without the quotes).\n\nOtherwise, print any nice string s' that $\\operatorname{dist}(s, s^{\\prime}) = k$.\n\n\n-----Examples-----\nInput\n4 26\nbear\n\nOutput\nroar\nInput\n2 7\naf\n\nOutput\ndb\n\nInput\n3 1000\nhey\n\nOutput\n-1"} +{"id": "00959", "text": "An n \u00d7 n square matrix is special, if: it is binary, that is, each cell contains either a 0, or a 1; the number of ones in each row and column equals 2. \n\nYou are given n and the first m rows of the matrix. Print the number of special n \u00d7 n matrices, such that the first m rows coincide with the given ones.\n\nAs the required value can be rather large, print the remainder after dividing the value by the given number mod.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, m, mod (2 \u2264 n \u2264 500, 0 \u2264 m \u2264 n, 2 \u2264 mod \u2264 10^9). Then m lines follow, each of them contains n characters \u2014 the first rows of the required special matrices. Each of these lines contains exactly two characters '1', the rest characters are '0'. Each column of the given m \u00d7 n table contains at most two numbers one.\n\n\n-----Output-----\n\nPrint the remainder after dividing the required value by number mod.\n\n\n-----Examples-----\nInput\n3 1 1000\n011\n\nOutput\n2\n\nInput\n4 4 100500\n0110\n1010\n0101\n1001\n\nOutput\n1\n\n\n\n-----Note-----\n\nFor the first test the required matrices are: \n\n011\n\n101\n\n110\n\n\n\n011\n\n110\n\n101\n\n\n\nIn the second test the required matrix is already fully given, so the answer is 1."} +{"id": "00960", "text": "Vasya likes to solve equations. Today he wants to solve $(x~\\mathrm{div}~k) \\cdot (x \\bmod k) = n$, where $\\mathrm{div}$ and $\\mathrm{mod}$ stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, $k$ and $n$ are positive integer parameters, and $x$ is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible $x$. Can you help him?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\leq n \\leq 10^6$, $2 \\leq k \\leq 1000$).\n\n\n-----Output-----\n\nPrint a single integer $x$\u00a0\u2014 the smallest positive integer solution to $(x~\\mathrm{div}~k) \\cdot (x \\bmod k) = n$. It is guaranteed that this equation has at least one positive integer solution.\n\n\n-----Examples-----\nInput\n6 3\n\nOutput\n11\n\nInput\n1 2\n\nOutput\n3\n\nInput\n4 6\n\nOutput\n10\n\n\n\n-----Note-----\n\nThe result of integer division $a~\\mathrm{div}~b$ is equal to the largest integer $c$ such that $b \\cdot c \\leq a$. $a$ modulo $b$ (shortened $a \\bmod b$) is the only integer $c$ such that $0 \\leq c < b$, and $a - c$ is divisible by $b$.\n\nIn the first sample, $11~\\mathrm{div}~3 = 3$ and $11 \\bmod 3 = 2$. Since $3 \\cdot 2 = 6$, then $x = 11$ is a solution to $(x~\\mathrm{div}~3) \\cdot (x \\bmod 3) = 6$. One can see that $19$ is the only other positive integer solution, hence $11$ is the smallest one."} +{"id": "00961", "text": "Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:\n\nVladik is at initial train station, and now n people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code a_{i} is known (the code of the city in which they are going to).\n\nTrain chief selects some number of disjoint segments of the original sequence of people (covering entire sequence by segments is not necessary). People who are in the same segment will be in the same train carriage. The segments are selected in such way that if at least one person travels to the city x, then all people who are going to city x should be in the same railway carriage. This means that they can\u2019t belong to different segments. Note, that all people who travel to the city x, either go to it and in the same railway carriage, or do not go anywhere at all.\n\nComfort of a train trip with people on segment from position l to position r is equal to XOR of all distinct codes of cities for people on the segment from position l to position r. XOR operation also known as exclusive OR.\n\nTotal comfort of a train trip is equal to sum of comfort for each segment.\n\nHelp Vladik to know maximal possible total comfort.\n\n\n-----Input-----\n\nFirst line contains single integer n (1 \u2264 n \u2264 5000)\u00a0\u2014 number of people.\n\nSecond line contains n space-separated integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 5000), where a_{i} denotes code of the city to which i-th person is going.\n\n\n-----Output-----\n\nThe output should contain a single integer\u00a0\u2014 maximal possible total comfort.\n\n\n-----Examples-----\nInput\n6\n4 4 2 5 2 3\n\nOutput\n14\n\nInput\n9\n5 1 3 1 5 2 4 2 5\n\nOutput\n9\n\n\n\n-----Note-----\n\nIn the first test case best partition into segments is: [4, 4] [2, 5, 2] [3], answer is calculated as follows: 4 + (2 xor 5) + 3 = 4 + 7 + 3 = 14\n\nIn the second test case best partition into segments is: 5 1 [3] 1 5 [2, 4, 2] 5, answer calculated as follows: 3 + (2 xor 4) = 3 + 6 = 9."} +{"id": "00962", "text": "Given is a directed graph G with N vertices and M edges.\n\nThe vertices are numbered 1 to N, and the i-th edge is directed from Vertex A_i to Vertex B_i.\n\nIt is guaranteed that the graph contains no self-loops or multiple edges.\nDetermine whether there exists an induced subgraph (see Notes) of G such that the in-degree and out-degree of every vertex are both 1. If the answer is yes, show one such subgraph.\n\nHere the null graph is not considered as a subgraph.\n\n-----Notes-----\nFor a directed graph G = (V, E), we call a directed graph G' = (V', E') satisfying the following conditions an induced subgraph of G:\n - V' is a (non-empty) subset of V.\n - E' is the set of all the edges in E that have both endpoints in V'.\n\n-----Constraints-----\n - 1 \\leq N \\leq 1000\n - 0 \\leq M \\leq 2000\n - 1 \\leq A_i,B_i \\leq N\n - A_i \\neq B_i\n - All pairs (A_i, B_i) are distinct.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 B_1\nA_2 B_2\n:\nA_M B_M\n\n-----Output-----\nIf there is no induced subgraph of G that satisfies the condition, print -1.\nOtherwise, print an induced subgraph of G that satisfies the condition, in the following format:\nK\nv_1\nv_2\n:\nv_K\n\nThis represents the induced subgraph of G with K vertices whose vertex set is \\{v_1, v_2, \\ldots, v_K\\}. (The order of v_1, v_2, \\ldots, v_K does not matter.)\nIf there are multiple subgraphs of G that satisfy the condition, printing any of them is accepted.\n\n-----Sample Input-----\n4 5\n1 2\n2 3\n2 4\n4 1\n4 3\n\n-----Sample Output-----\n3\n1\n2\n4\n\nThe induced subgraph of G whose vertex set is \\{1, 2, 4\\} has the edge set \\{(1, 2), (2, 4), (4, 1)\\}. The in-degree and out-degree of every vertex in this graph are both 1."} +{"id": "00963", "text": "There are N cells arranged in a row, numbered 1, 2, \\ldots, N from left to right.\nTak lives in these cells and is currently on Cell 1. He is trying to reach Cell N by using the procedure described below.\nYou are given an integer K that is less than or equal to 10, and K non-intersecting segments [L_1, R_1], [L_2, R_2], \\ldots, [L_K, R_K].\nLet S be the union of these K segments.\nHere, the segment [l, r] denotes the set consisting of all integers i that satisfy l \\leq i \\leq r.\n - When you are on Cell i, pick an integer d from S and move to Cell i + d. You cannot move out of the cells.\nTo help Tak, find the number of ways to go to Cell N, modulo 998244353.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1\u00a0\\leq K \\leq \\min(N, 10)\n - 1 \\leq L_i \\leq R_i \\leq N\n - [L_i, R_i] and [L_j, R_j] do not intersect (i \\neq j) \n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nL_1 R_1\nL_2 R_2\n:\nL_K R_K\n\n-----Output-----\nPrint the number of ways for Tak to go from Cell 1 to Cell N, modulo 998244353.\n\n-----Sample Input-----\n5 2\n1 1\n3 4\n\n-----Sample Output-----\n4\n\nThe set S is the union of the segment [1, 1] and the segment [3, 4], therefore S = \\{ 1, 3, 4 \\} holds.\nThere are 4 possible ways to get to Cell 5:\n - 1 \\to 2 \\to 3 \\to 4 \\to 5,\n - 1 \\to 2 \\to 5,\n - 1 \\to 4 \\to 5 and\n - 1 \\to 5."} +{"id": "00964", "text": "Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. \n\nAdvertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.\n\nYour task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.\n\n\n-----Input-----\n\nThe first line of the input contains six positive integers x_1, y_1, x_2, y_2, x_3, y_3 (1 \u2264 x_1, y_1, x_2, y_2, x_3, y_3 \u2264 100), where x_{i} and y_{i} determine the length and width of the logo of the i-th company respectively.\n\n\n-----Output-----\n\nIf it is impossible to place all the three logos on a square shield, print a single integer \"-1\" (without the quotes).\n\nIf it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters \"A\", \"B\" or \"C\". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters \"A\" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters \"B\" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters \"C\" should be equal to the sizes of the logo of the third company, \n\nNote that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.\n\nSee the samples to better understand the statement.\n\n\n-----Examples-----\nInput\n5 1 2 5 5 2\n\nOutput\n5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC\n\nInput\n4 4 2 6 4 2\n\nOutput\n6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"} +{"id": "00965", "text": "There are n cows playing poker at a table. For the current betting phase, each player's status is either \"ALLIN\", \"IN\", or \"FOLDED\", and does not change throughout the phase. To increase the suspense, a player whose current status is not \"FOLDED\" may show his/her hand to the table. However, so as not to affect any betting decisions, he/she may only do so if all other players have a status of either \"ALLIN\" or \"FOLDED\". The player's own status may be either \"ALLIN\" or \"IN\".\n\nFind the number of cows that can currently show their hands without affecting any betting decisions.\n\n\n-----Input-----\n\nThe first line contains a single integer, n (2 \u2264 n \u2264 2\u00b710^5). The second line contains n characters, each either \"A\", \"I\", or \"F\". The i-th character is \"A\" if the i-th player's status is \"ALLIN\", \"I\" if the i-th player's status is \"IN\", or \"F\" if the i-th player's status is \"FOLDED\".\n\n\n-----Output-----\n\nThe first line should contain a single integer denoting the number of players that can currently show their hands.\n\n\n-----Examples-----\nInput\n6\nAFFAAA\n\nOutput\n4\n\nInput\n3\nAFI\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample, cows 1, 4, 5, and 6 can show their hands. In the second sample, only cow 3 can show her hand."} +{"id": "00966", "text": "It seems like the year of 2013 came only yesterday. Do you know a curious fact? The year of 2013 is the first year after the old 1987 with only distinct digits.\n\nNow you are suggested to solve the following problem: given a year number, find the minimum year number which is strictly larger than the given one and has only distinct digits.\n\n\n-----Input-----\n\nThe single line contains integer y (1000 \u2264 y \u2264 9000) \u2014 the year number.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum year number that is strictly larger than y and all it's digits are distinct. It is guaranteed that the answer exists.\n\n\n-----Examples-----\nInput\n1987\n\nOutput\n2013\n\nInput\n2013\n\nOutput\n2014"} +{"id": "00967", "text": "Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the \"recent actions\" list. He likes to read thread conversations where each thread consists of multiple messages.\n\nRecent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.\n\nEmuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.\n\nHelp Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. \n\n\n-----Input-----\n\nThe first line of input contains an integer n, the number of threads (1 \u2264 n \u2264 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 \u2264 a_{i} \u2264 n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the number of threads that surely contain a new message.\n\n\n-----Examples-----\nInput\n5\n5 2 1 3 4\n\nOutput\n2\n\nInput\n3\n1 2 3\n\nOutput\n0\n\nInput\n4\n4 3 2 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.\n\nIn the second test case, there may be no new messages at all, since the thread order hasn't changed.\n\nIn the third test case, only thread 1 can contain no new messages."} +{"id": "00968", "text": "A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.\n\nLet's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?\n\nMore formally, if we denote the handle of the i-th person as h_{i}, then the following condition must hold: $\\forall i, j(i < j) : h_{p_{i}} < h_{p_{j}}$.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^5) \u2014 the number of people.\n\nThe next n lines each contains two strings. The i-th line contains strings f_{i} and s_{i} (1 \u2264 |f_{i}|, |s_{i}| \u2264 50) \u2014 the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.\n\nThe next line contains n distinct integers: p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 n).\n\n\n-----Output-----\n\nIf it is possible, output \"YES\", otherwise output \"NO\".\n\n\n-----Examples-----\nInput\n3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n1 2 3\n\nOutput\nNO\n\nInput\n3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n3 1 2\n\nOutput\nYES\n\nInput\n2\ngalileo galilei\nnicolaus copernicus\n2 1\n\nOutput\nYES\n\nInput\n10\nrean schwarzer\nfei claussell\nalisa reinford\neliot craig\nlaura arseid\njusis albarea\nmachias regnitz\nsara valestin\nemma millstein\ngaius worzel\n1 2 3 4 5 6 7 8 9 10\n\nOutput\nNO\n\nInput\n10\nrean schwarzer\nfei claussell\nalisa reinford\neliot craig\nlaura arseid\njusis albarea\nmachias regnitz\nsara valestin\nemma millstein\ngaius worzel\n2 4 9 6 5 7 1 3 8 10\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.\n\nIn example 3, if Copernicus uses \"copernicus\" as his handle, everything will be alright."} +{"id": "00969", "text": "A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.\n\nFirst, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.\n\nUnfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.\n\n\n-----Input-----\n\nFirst line of the input contains the string s\u00a0\u2014 the coating that is present in the shop. Second line contains the string t\u00a0\u2014 the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.\n\n\n-----Output-----\n\nThe first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.\n\nIf the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i}\u00a0\u2014 numbers of ending blocks in the corresponding piece. If x_{i} \u2264 y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.\n\n\n-----Examples-----\nInput\nabc\ncbaabc\n\nOutput\n2\n3 1\n1 3\n\nInput\naaabrytaaa\nayrat\n\nOutput\n3\n1 1\n6 5\n8 7\n\nInput\nami\nno\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample string \"cbaabc\" = \"cba\" + \"abc\".\n\nIn the second sample: \"ayrat\" = \"a\" + \"yr\" + \"at\"."} +{"id": "00970", "text": "You are given a chessboard of size 1 \u00d7 n. It is guaranteed that n is even. The chessboard is painted like this: \"BWBW...BW\".\n\nSome cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to $\\frac{n}{2}$.\n\nIn one step you can move one of the pieces one cell to the left or to the right. You cannot move pieces beyond the borders of the board. You also cannot move pieces to the cells that are already occupied.\n\nYour task is to place all the pieces in the cells of the same color using the minimum number of moves (all the pieces must occupy only the black cells or only the white cells after all the moves are made).\n\n\n-----Input-----\n\nThe first line of the input contains one integer n (2 \u2264 n \u2264 100, n is even) \u2014 the size of the chessboard. \n\nThe second line of the input contains $\\frac{n}{2}$ integer numbers $p_{1}, p_{2}, \\ldots, p_{\\frac{n}{2}}$ (1 \u2264 p_{i} \u2264 n) \u2014 initial positions of the pieces. It is guaranteed that all the positions are distinct.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of moves you have to make to place all the pieces in the cells of the same color.\n\n\n-----Examples-----\nInput\n6\n1 2 6\n\nOutput\n2\n\nInput\n10\n1 2 3 4 5\n\nOutput\n10\n\n\n\n-----Note-----\n\nIn the first example the only possible strategy is to move the piece at the position 6 to the position 5 and move the piece at the position 2 to the position 3. Notice that if you decide to place the pieces in the white cells the minimum number of moves will be 3.\n\nIn the second example the possible strategy is to move $5 \\rightarrow 9$ in 4 moves, then $4 \\rightarrow 7$ in 3 moves, $3 \\rightarrow 5$ in 2 moves and $2 \\rightarrow 3$ in 1 move."} +{"id": "00971", "text": "Kolya is going to make fresh orange juice. He has n oranges of sizes a_1, a_2, ..., a_{n}. Kolya will put them in the juicer in the fixed order, starting with orange of size a_1, then orange of size a_2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.\n\nThe juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, b and d (1 \u2264 n \u2264 100 000, 1 \u2264 b \u2264 d \u2264 1 000 000)\u00a0\u2014 the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1 000 000)\u00a0\u2014 sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of times Kolya will have to empty the waste section.\n\n\n-----Examples-----\nInput\n2 7 10\n5 6\n\nOutput\n1\n\nInput\n1 5 10\n7\n\nOutput\n0\n\nInput\n3 10 10\n5 7 7\n\nOutput\n1\n\nInput\n1 1 1\n1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.\n\nIn the second sample, the orange won't fit in the juicer so Kolya will have no juice at all."} +{"id": "00972", "text": "Consider an n \u00d7 m grid. Initially all the cells of the grid are colored white. Lenny has painted some of the cells (at least one) black. We call a painted grid convex if one can walk from any black cell to any another black cell using a path of side-adjacent black cells changing his direction at most once during the path. In the figure below, the left grid is convex while the right one is not convex, because there exist two cells which need more than one time to change direction in their path. [Image] \n\nYou're given a painted grid in the input. Tell Lenny if the grid is convex or not.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 50) \u2014 the size of the grid. Each of the next n lines contains m characters \"B\" or \"W\". Character \"B\" denotes a black cell of the grid and \"W\" denotes a white cell of the grid.\n\nIt's guaranteed that the grid has at least one black cell.\n\n\n-----Output-----\n\nOn the only line of the output print \"YES\" if the grid is convex, otherwise print \"NO\". Do not print quotes.\n\n\n-----Examples-----\nInput\n3 4\nWWBW\nBWWW\nWWWB\n\nOutput\nNO\n\nInput\n3 1\nB\nB\nW\n\nOutput\nYES"} +{"id": "00973", "text": "Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.\n\nThe pasture is a rectangle consisting of R \u00d7 C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.\n\nInitially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number. \n\n\n-----Input-----\n\nFirst line contains two integers R (1 \u2264 R \u2264 500) and C (1 \u2264 C \u2264 500), denoting the number of rows and the numbers of columns respectively.\n\nEach of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.\n\n\n-----Output-----\n\nIf it is impossible to protect all sheep, output a single line with the word \"No\".\n\nOtherwise, output a line with the word \"Yes\". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.\n\nIf there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.\n\n\n-----Examples-----\nInput\n6 6\n..S...\n..S.W.\n.S....\n..W...\n...W..\n......\n\nOutput\nYes\n..SD..\n..SDW.\n.SD...\n.DW...\nDD.W..\n......\n\nInput\n1 2\nSW\n\nOutput\nNo\n\nInput\n5 5\n.S...\n...S.\nS....\n...S.\n.S...\n\nOutput\nYes\n.S...\n...S.\nS.D..\n...S.\n.S...\n\n\n\n-----Note-----\n\nIn the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.\n\nIn the second example, there are no empty spots to put dogs that would guard the lone sheep.\n\nIn the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him."} +{"id": "00974", "text": "Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.\n\nOkabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.\n\nThat's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.\n\nTell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed.\n\n\n-----Input-----\n\nThe first line of input contains the integer n (1 \u2264 n \u2264 3\u00b710^5)\u00a0\u2014 the number of boxes.\n\nEach of the next 2n lines of input starts with a string \"add\" or \"remove\". If the line starts with the \"add\", an integer x (1 \u2264 x \u2264 n) follows, indicating that Daru should add the box with number x to the top of the stack. \n\nIt is guaranteed that exactly n lines contain \"add\" operations, all the boxes added are distinct, and n lines contain \"remove\" operations. It is also guaranteed that a box is always added before it is required to be removed.\n\n\n-----Output-----\n\nPrint the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands.\n\n\n-----Examples-----\nInput\n3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove\n\nOutput\n1\n\nInput\n7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.\n\nIn the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack."} +{"id": "00975", "text": "After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.\n\nRules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.\n\nOf course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.\n\nYour goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of digits in the cards Sherlock and Moriarty are going to use.\n\nThe second line contains n digits\u00a0\u2014 Sherlock's credit card number.\n\nThe third line contains n digits\u00a0\u2014 Moriarty's credit card number.\n\n\n-----Output-----\n\nFirst print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.\n\n\n-----Examples-----\nInput\n3\n123\n321\n\nOutput\n0\n2\n\nInput\n2\n88\n00\n\nOutput\n2\n0\n\n\n\n-----Note-----\n\nFirst sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks."} +{"id": "00976", "text": "You have decided to watch the best moments of some movie. There are two buttons on your player: Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie. Skip exactly x minutes of the movie (x is some fixed positive integer). If the player is now at the t-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (t + x). \n\nInitially the movie is turned on in the player on the first minute, and you want to watch exactly n best moments of the movie, the i-th best moment starts at the l_{i}-th minute and ends at the r_{i}-th minute (more formally, the i-th best moment consists of minutes: l_{i}, l_{i} + 1, ..., r_{i}). \n\nDetermine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments?\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n, x (1 \u2264 n \u2264 50, 1 \u2264 x \u2264 10^5) \u2014 the number of the best moments of the movie and the value of x for the second button.\n\nThe following n lines contain the descriptions of the best moments of the movie, the i-th line of the description contains two integers separated by a space l_{i}, r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 10^5).\n\nIt is guaranteed that for all integers i from 2 to n the following condition holds: r_{i} - 1 < l_{i}.\n\n\n-----Output-----\n\nOutput a single number \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n2 3\n5 6\n10 12\n\nOutput\n6\n\nInput\n1 1\n1 100000\n\nOutput\n100000\n\n\n\n-----Note-----\n\nIn the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie.\n\nIn the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie."} +{"id": "00977", "text": "This is the easy version of the problem. The difference between versions is the constraints on $n$ and $a_i$. You can make hacks only if all versions of the problem are solved.\n\nFirst, Aoi came up with the following idea for the competitive programming problem:\n\nYuzu is a girl who collecting candies. Originally, she has $x$ candies. There are also $n$ enemies numbered with integers from $1$ to $n$. Enemy $i$ has $a_i$ candies.\n\nYuzu is going to determine a permutation $P$. A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $\\{2,3,1,5,4\\}$ is a permutation, but $\\{1,2,2\\}$ is not a permutation ($2$ appears twice in the array) and $\\{1,3,4\\}$ is also not a permutation (because $n=3$ but there is the number $4$ in the array).\n\nAfter that, she will do $n$ duels with the enemies with the following rules: If Yuzu has equal or more number of candies than enemy $P_i$, she wins the duel and gets $1$ candy. Otherwise, she loses the duel and gets nothing. The candy which Yuzu gets will be used in the next duels. \n\nYuzu wants to win all duels. How many valid permutations $P$ exist?\n\nThis problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea:\n\nLet's define $f(x)$ as the number of valid permutations for the integer $x$.\n\nYou are given $n$, $a$ and a prime number $p \\le n$. Let's call a positive integer $x$ good, if the value $f(x)$ is not divisible by $p$. Find all good integers $x$.\n\nYour task is to solve this problem made by Akari.\n\n\n-----Input-----\n\nThe first line contains two integers $n$, $p$ $(2 \\le p \\le n \\le 2000)$. It is guaranteed, that the number $p$ is prime (it has exactly two divisors $1$ and $p$).\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ $(1 \\le a_i \\le 2000)$.\n\n\n-----Output-----\n\nIn the first line, print the number of good integers $x$.\n\nIn the second line, output all good integers $x$ in the ascending order.\n\nIt is guaranteed that the number of good integers $x$ does not exceed $10^5$.\n\n\n-----Examples-----\nInput\n3 2\n3 4 5\n\nOutput\n1\n3\n\nInput\n4 3\n2 3 5 6\n\nOutput\n2\n3 4\n\nInput\n4 3\n9 1 1 1\n\nOutput\n0\n\n\n\n\n-----Note-----\n\nIn the first test, $p=2$. If $x \\le 2$, there are no valid permutations for Yuzu. So $f(x)=0$ for all $x \\le 2$. The number $0$ is divisible by $2$, so all integers $x \\leq 2$ are not good. If $x = 3$, $\\{1,2,3\\}$ is the only valid permutation for Yuzu. So $f(3)=1$, so the number $3$ is good. If $x = 4$, $\\{1,2,3\\} , \\{1,3,2\\} , \\{2,1,3\\} , \\{2,3,1\\}$ are all valid permutations for Yuzu. So $f(4)=4$, so the number $4$ is not good. If $x \\ge 5$, all $6$ permutations are valid for Yuzu. So $f(x)=6$ for all $x \\ge 5$, so all integers $x \\ge 5$ are not good. \n\nSo, the only good number is $3$.\n\nIn the third test, for all positive integers $x$ the value $f(x)$ is divisible by $p = 3$."} +{"id": "00978", "text": "Cucumber boy is fan of Kyubeat, a famous music game.\n\nKyubeat has 16 panels for playing arranged in 4 \u00d7 4 table. When a panel lights up, he has to press that panel.\n\nEach panel has a timing to press (the preffered time when a player should press it), and Cucumber boy is able to press at most k panels in a time with his one hand. Cucumber boy is trying to press all panels in perfect timing, that is he wants to press each panel exactly in its preffered time. If he cannot press the panels with his two hands in perfect timing, his challenge to press all the panels in perfect timing will fail.\n\nYou are given one scene of Kyubeat's panel from the music Cucumber boy is trying. Tell him is he able to press all the panels in perfect timing.\n\n\n-----Input-----\n\nThe first line contains a single integer k (1 \u2264 k \u2264 5) \u2014 the number of panels Cucumber boy can press with his one hand.\n\nNext 4 lines contain 4 characters each (digits from 1 to 9, or period) \u2014 table of panels. If a digit i was written on the panel, it means the boy has to press that panel in time i. If period was written on the panel, he doesn't have to press that panel.\n\n\n-----Output-----\n\nOutput \"YES\" (without quotes), if he is able to press all the panels in perfect timing. If not, output \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n1\n.135\n1247\n3468\n5789\n\nOutput\nYES\n\nInput\n5\n..1.\n1111\n..1.\n..1.\n\nOutput\nYES\n\nInput\n1\n....\n12.1\n.2..\n.2..\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the third sample boy cannot press all panels in perfect timing. He can press all the panels in timing in time 1, but he cannot press the panels in time 2 in timing with his two hands."} +{"id": "00979", "text": "To become the king of Codeforces, Kuroni has to solve the following problem.\n\nHe is given $n$ numbers $a_1, a_2, \\dots, a_n$. Help Kuroni to calculate $\\prod_{1\\le i 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and k (1 \u2264 n \u2264 2\u00b7k \u2264 100 000), denoting the number of cowbells and the number of boxes, respectively.\n\nThe next line contains n space-separated integers s_1, s_2, ..., s_{n} (1 \u2264 s_1 \u2264 s_2 \u2264 ... \u2264 s_{n} \u2264 1 000 000), the sizes of Kevin's cowbells. It is guaranteed that the sizes s_{i} are given in non-decreasing order.\n\n\n-----Output-----\n\nPrint a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.\n\n\n-----Examples-----\nInput\n2 1\n2 5\n\nOutput\n7\n\nInput\n4 3\n2 3 5 9\n\nOutput\n9\n\nInput\n3 2\n3 5 7\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first sample, Kevin must pack his two cowbells into the same box. \n\nIn the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.\n\nIn the third sample, the optimal solution is {3, 5} and {7}."} +{"id": "01010", "text": "Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would contain exactly one nut and any break line goes between two adjacent pieces.\n\nYou are asked to calculate the number of ways he can do it. Two ways to break chocolate are considered distinct if one of them contains a break between some two adjacent pieces and the other one doesn't. \n\nPlease note, that if Bob doesn't make any breaks, all the bar will form one piece and it still has to have exactly one nut.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of pieces in the chocolate bar.\n\nThe second line contains n integers a_{i} (0 \u2264 a_{i} \u2264 1), where 0 represents a piece without the nut and 1 stands for a piece with the nut.\n\n\n-----Output-----\n\nPrint the number of ways to break the chocolate into multiple parts so that each part would contain exactly one nut.\n\n\n-----Examples-----\nInput\n3\n0 1 0\n\nOutput\n1\n\nInput\n5\n1 0 1 0 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample there is exactly one nut, so the number of ways equals 1\u00a0\u2014 Bob shouldn't make any breaks.\n\nIn the second sample you can break the bar in four ways:\n\n10|10|1\n\n1|010|1\n\n10|1|01\n\n1|01|01"} +{"id": "01011", "text": "Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.\n\nVasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 2\u00b710^5) \u2014 the number of throws of the first team. Then follow n integer numbers \u2014 the distances of throws a_{i} (1 \u2264 a_{i} \u2264 2\u00b710^9). \n\nThen follows number m (1 \u2264 m \u2264 2\u00b710^5) \u2014 the number of the throws of the second team. Then follow m integer numbers \u2014 the distances of throws of b_{i} (1 \u2264 b_{i} \u2264 2\u00b710^9).\n\n\n-----Output-----\n\nPrint two numbers in the format a:b \u2014 the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n2\n5 6\n\nOutput\n9:6\n\nInput\n5\n6 7 8 9 10\n5\n1 2 3 4 5\n\nOutput\n15:10"} +{"id": "01012", "text": "You are given a string $s$ consisting only of lowercase Latin letters.\n\nYou can rearrange all letters of this string as you wish. Your task is to obtain a good string by rearranging the letters of the given string or report that it is impossible to do it.\n\nLet's call a string good if it is not a palindrome. Palindrome is a string which is read from left to right the same as from right to left. For example, strings \"abacaba\", \"aa\" and \"z\" are palindromes and strings \"bba\", \"xd\" are not.\n\nYou have to answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 number of queries.\n\nEach of the next $t$ lines contains one string. The $i$-th line contains a string $s_i$ consisting only of lowercase Latin letter. It is guaranteed that the length of $s_i$ is from $1$ to $1000$ (inclusive).\n\n\n-----Output-----\n\nPrint $t$ lines. In the $i$-th line print the answer to the $i$-th query: -1 if it is impossible to obtain a good string by rearranging the letters of $s_i$ and any good string which can be obtained from the given one (by rearranging the letters) otherwise.\n\n\n-----Example-----\nInput\n3\naa\nabacaba\nxdd\n\nOutput\n-1\nabaacba\nxdd\n\n\n-----Note-----\n\nIn the first query we cannot rearrange letters to obtain a good string.\n\nOther examples (not all) of correct answers to the second query: \"ababaca\", \"abcabaa\", \"baacaba\".\n\nIn the third query we can do nothing to obtain a good string."} +{"id": "01013", "text": "Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns \u2014 from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).\n\nSimon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table. \n\nInitially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x_1, y_1), an arbitrary corner of the table (x_2, y_2) and color all cells of the table (p, q), which meet both inequations: min(x_1, x_2) \u2264 p \u2264 max(x_1, x_2), min(y_1, y_2) \u2264 q \u2264 max(y_1, y_2).\n\nHelp Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.\n\n\n-----Input-----\n\nThe first line contains exactly two integers n, m (3 \u2264 n, m \u2264 50).\n\nNext n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers a_{i}1, a_{i}2, ..., a_{im}. If a_{ij} equals zero, then cell (i, j) isn't good. Otherwise a_{ij} equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum number of operations Simon needs to carry out his idea.\n\n\n-----Examples-----\nInput\n3 3\n0 0 0\n0 1 0\n0 0 0\n\nOutput\n4\n\nInput\n4 3\n0 0 0\n0 0 1\n1 0 0\n0 0 0\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample, the sequence of operations can be like this: [Image] For the first time you need to choose cell (2, 2) and corner (1, 1). For the second time you need to choose cell (2, 2) and corner (3, 3). For the third time you need to choose cell (2, 2) and corner (3, 1). For the fourth time you need to choose cell (2, 2) and corner (1, 3). \n\nIn the second sample the sequence of operations can be like this: [Image] For the first time you need to choose cell (3, 1) and corner (4, 3). For the second time you need to choose cell (2, 3) and corner (1, 1)."} +{"id": "01014", "text": "Vasya decided to learn to play chess. Classic chess doesn't seem interesting to him, so he plays his own sort of chess.\n\nThe queen is the piece that captures all squares on its vertical, horizontal and diagonal lines. If the cell is located on the same vertical, horizontal or diagonal line with queen, and the cell contains a piece of the enemy color, the queen is able to move to this square. After that the enemy's piece is removed from the board. The queen cannot move to a cell containing an enemy piece if there is some other piece between it and the queen. \n\nThere is an n \u00d7 n chessboard. We'll denote a cell on the intersection of the r-th row and c-th column as (r, c). The square (1, 1) contains the white queen and the square (1, n) contains the black queen. All other squares contain green pawns that don't belong to anyone.\n\nThe players move in turns. The player that moves first plays for the white queen, his opponent plays for the black queen.\n\nOn each move the player has to capture some piece with his queen (that is, move to a square that contains either a green pawn or the enemy queen). The player loses if either he cannot capture any piece during his move or the opponent took his queen during the previous move. \n\nHelp Vasya determine who wins if both players play with an optimal strategy on the board n \u00d7 n.\n\n\n-----Input-----\n\nThe input contains a single number n (2 \u2264 n \u2264 10^9) \u2014 the size of the board.\n\n\n-----Output-----\n\nOn the first line print the answer to problem \u2014 string \"white\" or string \"black\", depending on who wins if the both players play optimally. \n\nIf the answer is \"white\", then you should also print two integers r and c representing the cell (r, c), where the first player should make his first move to win. If there are multiple such cells, print the one with the minimum r. If there are still multiple squares, print the one with the minimum c.\n\n\n-----Examples-----\nInput\n2\n\nOutput\nwhite\n1 2\n\nInput\n3\n\nOutput\nblack\n\n\n\n-----Note-----\n\nIn the first sample test the white queen can capture the black queen at the first move, so the white player wins.\n\nIn the second test from the statement if the white queen captures the green pawn located on the central vertical line, then it will be captured by the black queen during the next move. So the only move for the white player is to capture the green pawn located at (2, 1). \n\nSimilarly, the black queen doesn't have any other options but to capture the green pawn located at (2, 3), otherwise if it goes to the middle vertical line, it will be captured by the white queen.\n\nDuring the next move the same thing happens \u2014 neither the white, nor the black queen has other options rather than to capture green pawns situated above them. Thus, the white queen ends up on square (3, 1), and the black queen ends up on square (3, 3). \n\nIn this situation the white queen has to capture any of the green pawns located on the middle vertical line, after that it will be captured by the black queen. Thus, the player who plays for the black queen wins."} +{"id": "01015", "text": "A lighthouse keeper Peter commands an army of $n$ battle lemmings. He ordered his army to stand in a line and numbered the lemmings from $1$ to $n$ from left to right. Some of the lemmings hold shields. Each lemming cannot hold more than one shield.\n\nThe more protected Peter's army is, the better. To calculate the protection of the army, he finds the number of protected pairs of lemmings, that is such pairs that both lemmings in the pair don't hold a shield, but there is a lemming with a shield between them.\n\nNow it's time to prepare for defence and increase the protection of the army. To do this, Peter can give orders. He chooses a lemming with a shield and gives him one of the two orders: give the shield to the left neighbor if it exists and doesn't have a shield; give the shield to the right neighbor if it exists and doesn't have a shield. \n\nIn one second Peter can give exactly one order.\n\nIt's not clear how much time Peter has before the defence. So he decided to determine the maximal value of army protection for each $k$ from $0$ to $\\frac{n(n-1)}2$, if he gives no more that $k$ orders. Help Peter to calculate it!\n\n\n-----Input-----\n\nFirst line contains a single integer $n$ ($1 \\le n \\le 80$), the number of lemmings in Peter's army.\n\nSecond line contains $n$ integers $a_i$ ($0 \\le a_i \\le 1$). If $a_i = 1$, then the $i$-th lemming has a shield, otherwise $a_i = 0$.\n\n\n-----Output-----\n\nPrint $\\frac{n(n-1)}2 + 1$ numbers, the greatest possible protection after no more than $0, 1, \\dots, \\frac{n(n-1)}2$ orders.\n\n\n-----Examples-----\nInput\n5\n1 0 0 0 1\n\nOutput\n0 2 3 3 3 3 3 3 3 3 3 \n\nInput\n12\n0 0 0 0 1 1 1 1 0 1 1 0\n\nOutput\n9 12 13 14 14 14 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 \n\n\n\n-----Note-----\n\nConsider the first example.\n\nThe protection is initially equal to zero, because for each pair of lemmings without shields there is no lemmings with shield.\n\nIn one second Peter can order the first lemming give his shield to the right neighbor. In this case, the protection is two, as there are two protected pairs of lemmings, $(1, 3)$ and $(1, 4)$.\n\nIn two seconds Peter can act in the following way. First, he orders the fifth lemming to give a shield to the left neighbor. Then, he orders the first lemming to give a shield to the right neighbor. In this case Peter has three protected pairs of lemmings\u00a0\u2014 $(1, 3)$, $(1, 5)$ and $(3, 5)$.\n\nYou can make sure that it's impossible to give orders in such a way that the protection becomes greater than three."} +{"id": "01016", "text": "DZY loves chemistry, and he enjoys mixing chemicals.\n\nDZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order. \n\nLet's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.\n\nFind the maximum possible danger after pouring all the chemicals one by one in optimal order.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m $(1 \\leq n \\leq 50 ; 0 \\leq m \\leq \\frac{n(n - 1)}{2})$.\n\nEach of the next m lines contains two space-separated integers x_{i} and y_{i} (1 \u2264 x_{i} < y_{i} \u2264 n). These integers mean that the chemical x_{i} will react with the chemical y_{i}. Each pair of chemicals will appear at most once in the input.\n\nConsider all the chemicals numbered from 1 to n in some order.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum possible danger.\n\n\n-----Examples-----\nInput\n1 0\n\nOutput\n1\n\nInput\n2 1\n1 2\n\nOutput\n2\n\nInput\n3 2\n1 2\n2 3\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample, there's only one way to pour, and the danger won't increase.\n\nIn the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.\n\nIn the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring)."} +{"id": "01017", "text": "Little Artem got n stones on his birthday and now wants to give some of them to Masha. He knows that Masha cares more about the fact of receiving the present, rather than the value of that present, so he wants to give her stones as many times as possible. However, Masha remembers the last present she received, so Artem can't give her the same number of stones twice in a row. For example, he can give her 3 stones, then 1 stone, then again 3 stones, but he can't give her 3 stones and then again 3 stones right after that.\n\nHow many times can Artem give presents to Masha?\n\n\n-----Input-----\n\nThe only line of the input contains a single integer n (1 \u2264 n \u2264 10^9)\u00a0\u2014 number of stones Artem received on his birthday.\n\n\n-----Output-----\n\nPrint the maximum possible number of times Artem can give presents to Masha.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n2\n\nOutput\n1\n\nInput\n3\n\nOutput\n2\n\nInput\n4\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample, Artem can only give 1 stone to Masha.\n\nIn the second sample, Atrem can give Masha 1 or 2 stones, though he can't give her 1 stone two times.\n\nIn the third sample, Atrem can first give Masha 2 stones, a then 1 more stone.\n\nIn the fourth sample, Atrem can first give Masha 1 stone, then 2 stones, and finally 1 stone again."} +{"id": "01018", "text": "Stepan has n pens. Every day he uses them, and on the i-th day he uses the pen number i. On the (n + 1)-th day again he uses the pen number 1, on the (n + 2)-th \u2014 he uses the pen number 2 and so on.\n\nOn every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the pen he uses that day. On Sunday Stepan has a day of rest, he does not stend the ink of the pen he uses that day. \n\nStepan knows the current volume of ink in each of his pens. Now it's the Monday morning and Stepan is going to use the pen number 1 today. Your task is to determine which pen will run out of ink before all the rest (that is, there will be no ink left in it), if Stepan will use the pens according to the conditions described above.\n\n\n-----Input-----\n\nThe first line contains the integer n (1 \u2264 n \u2264 50 000) \u2014 the number of pens Stepan has.\n\nThe second line contains the sequence of integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9), where a_{i} is equal to the number of milliliters of ink which the pen number i currently has.\n\n\n-----Output-----\n\nPrint the index of the pen which will run out of ink before all (it means that there will be no ink left in it), if Stepan will use pens according to the conditions described above. \n\nPens are numbered in the order they are given in input data. The numeration begins from one. \n\nNote that the answer is always unambiguous, since several pens can not end at the same time.\n\n\n-----Examples-----\nInput\n3\n3 3 3\n\nOutput\n2\n\nInput\n5\n5 4 5 4 4\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first test Stepan uses ink of pens as follows: on the day number 1 (Monday) Stepan will use the pen number 1, after that there will be 2 milliliters of ink in it; on the day number 2 (Tuesday) Stepan will use the pen number 2, after that there will be 2 milliliters of ink in it; on the day number 3 (Wednesday) Stepan will use the pen number 3, after that there will be 2 milliliters of ink in it; on the day number 4 (Thursday) Stepan will use the pen number 1, after that there will be 1 milliliters of ink in it; on the day number 5 (Friday) Stepan will use the pen number 2, after that there will be 1 milliliters of ink in it; on the day number 6 (Saturday) Stepan will use the pen number 3, after that there will be 1 milliliters of ink in it; on the day number 7 (Sunday) Stepan will use the pen number 1, but it is a day of rest so he will not waste ink of this pen in it; on the day number 8 (Monday) Stepan will use the pen number 2, after that this pen will run out of ink. \n\nSo, the first pen which will not have ink is the pen number 2."} +{"id": "01019", "text": "Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned that a fraction $\\frac{a}{b}$ is called proper iff its numerator is smaller than its denominator (a < b) and that the fraction is called irreducible if its numerator and its denominator are coprime (they do not have positive common divisors except 1).\n\nDuring his free time, Petya thinks about proper irreducible fractions and converts them to decimals using the calculator. One day he mistakenly pressed addition button ( + ) instead of division button (\u00f7) and got sum of numerator and denominator that was equal to n instead of the expected decimal notation. \n\nPetya wanted to restore the original fraction, but soon he realized that it might not be done uniquely. That's why he decided to determine maximum possible proper irreducible fraction $\\frac{a}{b}$ such that sum of its numerator and denominator equals n. Help Petya deal with this problem.\n\n \n\n\n-----Input-----\n\nIn the only line of input there is an integer n (3 \u2264 n \u2264 1000), the sum of numerator and denominator of the fraction.\n\n\n-----Output-----\n\nOutput two space-separated positive integers a and b, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n1 2\n\nInput\n4\n\nOutput\n1 3\n\nInput\n12\n\nOutput\n5 7"} +{"id": "01020", "text": "You have a plate and you want to add some gilding to it. The plate is a rectangle that we split into $w\\times h$ cells. There should be $k$ gilded rings, the first one should go along the edge of the plate, the second one\u00a0\u2014 $2$ cells away from the edge and so on. Each ring has a width of $1$ cell. Formally, the $i$-th of these rings should consist of all bordering cells on the inner rectangle of size $(w - 4(i - 1))\\times(h - 4(i - 1))$.\n\n [Image] The picture corresponds to the third example. \n\nYour task is to compute the number of cells to be gilded.\n\n\n-----Input-----\n\nThe only line contains three integers $w$, $h$ and $k$ ($3 \\le w, h \\le 100$, $1 \\le k \\le \\left\\lfloor \\frac{min(n, m) + 1}{4}\\right\\rfloor$, where $\\lfloor x \\rfloor$ denotes the number $x$ rounded down) \u2014 the number of rows, columns and the number of rings, respectively.\n\n\n-----Output-----\n\nPrint a single positive integer\u00a0\u2014 the number of cells to be gilded.\n\n\n-----Examples-----\nInput\n3 3 1\n\nOutput\n8\n\nInput\n7 9 1\n\nOutput\n28\n\nInput\n7 9 2\n\nOutput\n40\n\n\n\n-----Note-----\n\nThe first example is shown on the picture below.\n\n [Image] \n\nThe second example is shown on the picture below.\n\n [Image] \n\nThe third example is shown in the problem description."} +{"id": "01021", "text": "Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$.\n\nSometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \\le i \\le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$.\n\nAndrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 10^5$)\u00a0\u2014 the number of magic stones.\n\nThe second line contains integers $c_1, c_2, \\ldots, c_n$ ($0 \\le c_i \\le 2 \\cdot 10^9$)\u00a0\u2014 the charges of Grigory's stones.\n\nThe second line contains integers $t_1, t_2, \\ldots, t_n$ ($0 \\le t_i \\le 2 \\cdot 10^9$)\u00a0\u2014 the charges of Andrew's stones.\n\n\n-----Output-----\n\nIf there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print \"Yes\".\n\nOtherwise, print \"No\".\n\n\n-----Examples-----\nInput\n4\n7 2 4 12\n7 15 10 12\n\nOutput\nYes\n\nInput\n3\n4 4 4\n1 2 3\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \\mathbf{4}, 12] \\rightarrow [7, 2, \\mathbf{10}, 12]$. Then synchronize the second stone: $[7, \\mathbf{2}, 10, 12] \\rightarrow [7, \\mathbf{15}, 10, 12]$. \n\nIn the second example, any operation with the second stone will not change its charge."} +{"id": "01022", "text": "There are $n$ children numbered from $1$ to $n$ in a kindergarten. Kindergarten teacher gave $a_i$ ($1 \\leq a_i \\leq n$) candies to the $i$-th child. Children were seated in a row in order from $1$ to $n$ from left to right and started eating candies. \n\nWhile the $i$-th child was eating candies, he calculated two numbers $l_i$ and $r_i$\u00a0\u2014 the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.\n\nFormally, $l_i$ is the number of indices $j$ ($1 \\leq j < i$), such that $a_i < a_j$ and $r_i$ is the number of indices $j$ ($i < j \\leq n$), such that $a_i < a_j$.\n\nEach child told to the kindergarten teacher the numbers $l_i$ and $r_i$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $l$ and $r$ determine whether she could have given the candies to the children such that all children correctly calculated their values $l_i$ and $r_i$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it.\n\n\n-----Input-----\n\nOn the first line there is a single integer $n$ ($1 \\leq n \\leq 1000$)\u00a0\u2014 the number of children in the kindergarten.\n\nOn the next line there are $n$ integers $l_1, l_2, \\ldots, l_n$ ($0 \\leq l_i \\leq n$), separated by spaces.\n\nOn the next line, there are $n$ integer numbers $r_1, r_2, \\ldots, r_n$ ($0 \\leq r_i \\leq n$), separated by spaces.\n\n\n-----Output-----\n\nIf there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print \u00abNO\u00bb (without quotes).\n\nOtherwise, print \u00abYES\u00bb (without quotes) on the first line. On the next line, print $n$ integers $a_1, a_2, \\ldots, a_n$, separated by spaces\u00a0\u2014 the numbers of candies the children $1, 2, \\ldots, n$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $1 \\leq a_i \\leq n$. The number of children seating to the left of the $i$-th child that got more candies than he should be equal to $l_i$ and the number of children seating to the right of the $i$-th child that got more candies than he should be equal to $r_i$. If there is more than one solution, find any of them.\n\n\n-----Examples-----\nInput\n5\n0 0 1 1 2\n2 0 1 0 0\n\nOutput\nYES\n1 3 1 2 1\n\nInput\n4\n0 0 2 0\n1 1 1 1\n\nOutput\nNO\n\nInput\n3\n0 0 0\n0 0 0\n\nOutput\nYES\n1 1 1\n\n\n\n-----Note-----\n\nIn the first example, if the teacher distributed $1$, $3$, $1$, $2$, $1$ candies to $1$-st, $2$-nd, $3$-rd, $4$-th, $5$-th child, respectively, then all the values calculated by the children are correct. For example, the $5$-th child was given $1$ candy, to the left of him $2$ children were given $1$ candy, $1$ child was given $2$ candies and $1$ child\u00a0\u2014 $3$ candies, so there are $2$ children to the left of him that were given more candies than him.\n\nIn the second example it is impossible to distribute the candies, because the $4$-th child made a mistake in calculating the value of $r_4$, because there are no children to the right of him, so $r_4$ should be equal to $0$.\n\nIn the last example all children may have got the same number of candies, that's why all the numbers are $0$. Note that each child should receive at least one candy."} +{"id": "01023", "text": "Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C.\n\nThere are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later.\n\nThere are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later.\n\nThe connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \\ge a_i + t_a$.\n\nYou can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it.\n\nArkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$.\n\n\n-----Input-----\n\nThe first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \\le n, m \\le 2 \\cdot 10^5$, $1 \\le k \\le n + m$, $1 \\le t_a, t_b \\le 10^9$)\u00a0\u2014 the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively.\n\nThe second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \\le a_1 < a_2 < \\ldots < a_n \\le 10^9$)\u00a0\u2014 the times the flights from A to B depart.\n\nThe third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \\le b_1 < b_2 < \\ldots < b_m \\le 10^9$)\u00a0\u2014 the times the flights from B to C depart.\n\n\n-----Output-----\n\nIf you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$.\n\nOtherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time.\n\n\n-----Examples-----\nInput\n4 5 1 1 2\n1 3 5 7\n1 2 3 9 10\n\nOutput\n11\n\nInput\n2 2 4 4 2\n1 10\n10 20\n\nOutput\n-1\n\nInput\n4 3 2 3 1\n1 999999998 999999999 1000000000\n3 4 1000000000\n\nOutput\n1000000003\n\n\n\n-----Note-----\n\nConsider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$.\n\nIn the second example you can simply cancel all flights from A to B and you're done.\n\nIn the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C."} +{"id": "01024", "text": "You are given a permutation of integers from 1 to n. Exactly once you apply the following operation to this permutation: pick a random segment and shuffle its elements. Formally: Pick a random segment (continuous subsequence) from l to r. All $\\frac{n(n + 1)}{2}$ segments are equiprobable. Let k = r - l + 1, i.e. the length of the chosen segment. Pick a random permutation of integers from 1 to k, p_1, p_2, ..., p_{k}. All k! permutation are equiprobable. This permutation is applied to elements of the chosen segment, i.e. permutation a_1, a_2, ..., a_{l} - 1, a_{l}, a_{l} + 1, ..., a_{r} - 1, a_{r}, a_{r} + 1, ..., a_{n} is transformed to a_1, a_2, ..., a_{l} - 1, a_{l} - 1 + p_1, a_{l} - 1 + p_2, ..., a_{l} - 1 + p_{k} - 1, a_{l} - 1 + p_{k}, a_{r} + 1, ..., a_{n}. \n\nInversion if a pair of elements (not necessary neighbouring) with the wrong relative order. In other words, the number of inversion is equal to the number of pairs (i, j) such that i < j and a_{i} > a_{j}. Find the expected number of inversions after we apply exactly one operation mentioned above.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the length of the permutation.\n\nThe second line contains n distinct integers from 1 to n\u00a0\u2014 elements of the permutation.\n\n\n-----Output-----\n\nPrint one real value\u00a0\u2014 the expected number of inversions. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 9}. \n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-9}$.\n\n\n-----Example-----\nInput\n3\n2 3 1\n\nOutput\n1.916666666666666666666666666667"} +{"id": "01025", "text": "Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 2000) \u2014 the number of the points painted on the plane. \n\nNext n lines contain two integers each x_{i}, y_{i} ( - 100 \u2264 x_{i}, y_{i} \u2264 100) \u2014 the coordinates of the i-th point. It is guaranteed that no two given points coincide.\n\n\n-----Output-----\n\nIn the first line print an integer \u2014 the number of triangles with the non-zero area among the painted points.\n\n\n-----Examples-----\nInput\n4\n0 0\n1 1\n2 0\n2 2\n\nOutput\n3\n\nInput\n3\n0 0\n1 1\n2 0\n\nOutput\n1\n\nInput\n1\n1 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nNote to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0).\n\nNote to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).\n\nNote to the third sample test. A single point doesn't form a single triangle."} +{"id": "01026", "text": "Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. \n\nTanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \\dots, c_k]$ should be strictly increasing.\n\nThere are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold.\n\nFor example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). \n\nThere are some additional ways to plan a journey that are not listed above.\n\nTanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of cities in Berland.\n\nThe second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \\le b_i \\le 4 \\cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum beauty of a journey Tanya can choose.\n\n\n-----Examples-----\nInput\n6\n10 7 1 9 10 15\n\nOutput\n26\n\nInput\n1\n400000\n\nOutput\n400000\n\nInput\n7\n8 9 26 11 12 29 14\n\nOutput\n55\n\n\n\n-----Note-----\n\nThe optimal journey plan in the first example is $c = [2, 4, 5]$.\n\nThe optimal journey plan in the second example is $c = [1]$.\n\nThe optimal journey plan in the third example is $c = [3, 6]$."} +{"id": "01027", "text": "Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes. [Image] \n\nInitially, each hole has $a_i$ stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.\n\nNote that the counter-clockwise order means if the player takes the stones from hole $i$, he will put one stone in the $(i+1)$-th hole, then in the $(i+2)$-th, etc. If he puts a stone in the $14$-th hole, the next one will be put in the first hole.\n\nAfter the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.\n\nResli is a famous Mancala player. He wants to know the maximum score he can obtain after one move.\n\n\n-----Input-----\n\nThe only line contains 14 integers $a_1, a_2, \\ldots, a_{14}$ ($0 \\leq a_i \\leq 10^9$)\u00a0\u2014 the number of stones in each hole.\n\nIt is guaranteed that for any $i$ ($1\\leq i \\leq 14$) $a_i$ is either zero or odd, and there is at least one stone in the board.\n\n\n-----Output-----\n\nOutput one integer, the maximum possible score after one move.\n\n\n-----Examples-----\nInput\n0 1 1 0 0 0 0 0 0 7 0 0 0 0\n\nOutput\n4\n\nInput\n5 1 1 1 1 0 0 0 0 0 0 0 0 0\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first test case the board after the move from the hole with $7$ stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to $4$."} +{"id": "01028", "text": "n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends.\n\nYour task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition.\n\n\n-----Input-----\n\nThe only line of input contains two integers n and m, separated by a single space (1 \u2264 m \u2264 n \u2264 10^9) \u2014 the number of participants and the number of teams respectively. \n\n\n-----Output-----\n\nThe only line of the output should contain two integers k_{min} and k_{max} \u2014 the minimum possible number of pairs of friends and the maximum possible number of pairs of friends respectively.\n\n\n-----Examples-----\nInput\n5 1\n\nOutput\n10 10\n\nInput\n3 2\n\nOutput\n1 1\n\nInput\n6 3\n\nOutput\n3 6\n\n\n\n-----Note-----\n\nIn the first sample all the participants get into one team, so there will be exactly ten pairs of friends.\n\nIn the second sample at any possible arrangement one team will always have two participants and the other team will always have one participant. Thus, the number of pairs of friends will always be equal to one.\n\nIn the third sample minimum number of newly formed friendships can be achieved if participants were split on teams consisting of 2 people, maximum number can be achieved if participants were split on teams of 1, 1 and 4 people."} +{"id": "01029", "text": "George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b_1, b_2, ..., b_{|}b| (record |b| denotes the current length of the array). Then one change is a sequence of actions: Choose two distinct indexes i and j (1 \u2264 i, j \u2264 |b|;\u00a0i \u2260 j), such that b_{i} \u2265 b_{j}. Get number v = concat(b_{i}, b_{j}), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22. Add number v to the end of the array. The length of the array will increase by one. Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array. \n\nGeorge played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer p (1 \u2264 p < 10^100000). It is guaranteed that number p doesn't contain any leading zeroes.\n\n\n-----Output-----\n\nPrint an integer \u2014 the maximum number of elements array b could contain originally.\n\n\n-----Examples-----\nInput\n9555\n\nOutput\n4\nInput\n10000000005\n\nOutput\n2\nInput\n800101\n\nOutput\n3\nInput\n45\n\nOutput\n1\nInput\n1000000000000001223300003342220044555\n\nOutput\n17\nInput\n19992000\n\nOutput\n1\nInput\n310200\n\nOutput\n2\n\n\n-----Note-----\n\nLet's consider the test examples: Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} \u2192 {5, 5, 95} \u2192 {95, 55} \u2192 {9555}. Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros. Originally array b could be equal to {800, 10, 1}. Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation. \n\nNote that the numbers can be very large."} +{"id": "01030", "text": "User ainta is making a web site. This time he is going to make a navigation of the pages. In his site, there are n pages numbered by integers from 1 to n. Assume that somebody is on the p-th page now. The navigation will look like this: << p - k p - k + 1 ... p - 1 (p) p + 1 ... p + k - 1 p + k >> \n\nWhen someone clicks the button \"<<\" he is redirected to page 1, and when someone clicks the button \">>\" he is redirected to page n. Of course if someone clicks on a number, he is redirected to the corresponding page.\n\nThere are some conditions in the navigation: If page 1 is in the navigation, the button \"<<\" must not be printed. If page n is in the navigation, the button \">>\" must not be printed. If the page number is smaller than 1 or greater than n, it must not be printed. \u00a0\n\nYou can see some examples of the navigations. Make a program that prints the navigation.\n\n\n-----Input-----\n\nThe first and the only line contains three integers n, p, k (3 \u2264 n \u2264 100; 1 \u2264 p \u2264 n; 1 \u2264 k \u2264 n)\n\n\n-----Output-----\n\nPrint the proper navigation. Follow the format of the output from the test samples.\n\n\n-----Examples-----\nInput\n17 5 2\n\nOutput\n<< 3 4 (5) 6 7 >> \nInput\n6 5 2\n\nOutput\n<< 3 4 (5) 6 \nInput\n6 1 2\n\nOutput\n(1) 2 3 >> \nInput\n6 2 2\n\nOutput\n1 (2) 3 4 >>\nInput\n9 6 3\n\nOutput\n<< 3 4 5 (6) 7 8 9\nInput\n10 6 3\n\nOutput\n<< 3 4 5 (6) 7 8 9 >>\nInput\n8 5 4\n\nOutput\n1 2 3 4 (5) 6 7 8"} +{"id": "01031", "text": "In this problem, your task is to use ASCII graphics to paint a cardiogram. \n\nA cardiogram is a polyline with the following corners:$(0 ; 0),(a_{1} ; a_{1}),(a_{1} + a_{2} ; a_{1} - a_{2}),(a_{1} + a_{2} + a_{3} ; a_{1} - a_{2} + a_{3}), \\ldots,(\\sum_{i = 1}^{n} a_{i} ; \\sum_{i = 1}^{n}(- 1)^{i + 1} a_{i})$\n\nThat is, a cardiogram is fully defined by a sequence of positive integers a_1, a_2, ..., a_{n}.\n\nYour task is to paint a cardiogram by given sequence a_{i}.\n\n\n-----Input-----\n\nThe first line contains integer n (2 \u2264 n \u2264 1000). The next line contains the sequence of integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1000). It is guaranteed that the sum of all a_{i} doesn't exceed 1000.\n\n\n-----Output-----\n\nPrint max\u00a0|y_{i} - y_{j}| lines (where y_{k} is the y coordinate of the k-th point of the polyline), in each line print $\\sum_{i = 1}^{n} a_{i}$ characters. Each character must equal either \u00ab / \u00bb (slash), \u00ab \\ \u00bb (backslash), \u00ab \u00bb (space). The printed image must be the image of the given polyline. Please study the test samples for better understanding of how to print a cardiogram.\n\nNote that in this problem the checker checks your answer taking spaces into consideration. Do not print any extra characters. Remember that the wrong answer to the first pretest doesn't give you a penalty.\n\n\n-----Examples-----\nInput\n5\n3 1 2 5 1\n\nOutput\n / \\ \n / \\ / \\ \n / \\ \n / \\ \n \\ / \n\nInput\n3\n1 5 1\n\nOutput\n / \\ \n \\ \n \\ \n \\ \n \\ /"} +{"id": "01032", "text": "This is the hard version of the problem. The difference between versions is the constraints on $n$ and $a_i$. You can make hacks only if all versions of the problem are solved.\n\nFirst, Aoi came up with the following idea for the competitive programming problem:\n\nYuzu is a girl who collecting candies. Originally, she has $x$ candies. There are also $n$ enemies numbered with integers from $1$ to $n$. Enemy $i$ has $a_i$ candies.\n\nYuzu is going to determine a permutation $P$. A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $\\{2,3,1,5,4\\}$ is a permutation, but $\\{1,2,2\\}$ is not a permutation ($2$ appears twice in the array) and $\\{1,3,4\\}$ is also not a permutation (because $n=3$ but there is the number $4$ in the array).\n\nAfter that, she will do $n$ duels with the enemies with the following rules:\n\n If Yuzu has equal or more number of candies than enemy $P_i$, she wins the duel and gets $1$ candy. Otherwise, she loses the duel and gets nothing. The candy which Yuzu gets will be used in the next duels. \n\nYuzu wants to win all duels. How many valid permutations $P$ exist?\n\nThis problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea:\n\nLet's define $f(x)$ as the number of valid permutations for the integer $x$.\n\nYou are given $n$, $a$ and a prime number $p \\le n$. Let's call a positive integer $x$ good, if the value $f(x)$ is not divisible by $p$. Find all good integers $x$.\n\nYour task is to solve this problem made by Akari.\n\n\n-----Input-----\n\nThe first line contains two integers $n$, $p$ $(2 \\le p \\le n \\le 10^5)$. It is guaranteed, that the number $p$ is prime (it has exactly two divisors $1$ and $p$).\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ $(1 \\le a_i \\le 10^9)$.\n\n\n-----Output-----\n\nIn the first line, print the number of good integers $x$.\n\nIn the second line, output all good integers $x$ in the ascending order.\n\nIt is guaranteed that the number of good integers $x$ does not exceed $10^5$.\n\n\n-----Examples-----\nInput\n3 2\n3 4 5\n\nOutput\n1\n3\n\nInput\n4 3\n2 3 5 6\n\nOutput\n2\n3 4\n\nInput\n4 3\n9 1 1 1\n\nOutput\n0\n\n\nInput\n3 2\n1000000000 1 999999999\n\nOutput\n1\n999999998\n\n\n\n-----Note-----\n\nIn the first test, $p=2$.\n\n If $x \\le 2$, there are no valid permutations for Yuzu. So $f(x)=0$ for all $x \\le 2$. The number $0$ is divisible by $2$, so all integers $x \\leq 2$ are not good. If $x = 3$, $\\{1,2,3\\}$ is the only valid permutation for Yuzu. So $f(3)=1$, so the number $3$ is good. If $x = 4$, $\\{1,2,3\\} , \\{1,3,2\\} , \\{2,1,3\\} , \\{2,3,1\\}$ are all valid permutations for Yuzu. So $f(4)=4$, so the number $4$ is not good. If $x \\ge 5$, all $6$ permutations are valid for Yuzu. So $f(x)=6$ for all $x \\ge 5$, so all integers $x \\ge 5$ are not good. \n\nSo, the only good number is $3$.\n\nIn the third test, for all positive integers $x$ the value $f(x)$ is divisible by $p = 3$."} +{"id": "01033", "text": "You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right. \n\nObviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it. \n\nFinally you ended up with the following conditions to building the castle: h_1 \u2264 H: no sand from the leftmost spot should go over the fence; For any $i \\in [ 1 ; \\infty)$ |h_{i} - h_{i} + 1| \u2264 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\\sum_{i = 1}^{\\infty} h_{i} = n$: you want to spend all the sand you brought with you. \n\nAs you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible. \n\nYour task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.\n\n\n-----Input-----\n\nThe only line contains two integer numbers n and H (1 \u2264 n, H \u2264 10^18) \u2014 the number of sand packs you have and the height of the fence, respectively.\n\n\n-----Output-----\n\nPrint the minimum number of spots you can occupy so the all the castle building conditions hold.\n\n\n-----Examples-----\nInput\n5 2\n\nOutput\n3\n\nInput\n6 8\n\nOutput\n3\n\n\n\n-----Note-----\n\nHere are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied) \n\nThe first list for both cases is the optimal answer, 3 spots are occupied in them.\n\nAnd here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]"} +{"id": "01034", "text": "The Patisserie AtCoder sells cakes with number-shaped candles.\nThere are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.\nEach cake has an integer value called deliciousness, as follows:\n - The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.\n - The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.\n - The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.\nTakahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.\n\nThere are X \\times Y \\times Z such ways to choose three cakes.\n\nWe will arrange these X \\times Y \\times Z ways in descending order of the sum of the deliciousness of the cakes.\n\nPrint the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.\n\n-----Constraints-----\n - 1 \\leq X \\leq 1 \\ 000\n - 1 \\leq Y \\leq 1 \\ 000\n - 1 \\leq Z \\leq 1 \\ 000\n - 1 \\leq K \\leq \\min(3 \\ 000, X \\times Y \\times Z)\n - 1 \\leq A_i \\leq 10 \\ 000 \\ 000 \\ 000\n - 1 \\leq B_i \\leq 10 \\ 000 \\ 000 \\ 000\n - 1 \\leq C_i \\leq 10 \\ 000 \\ 000 \\ 000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y Z K\nA_1 \\ A_2 \\ A_3 \\ ... \\ A_X\nB_1 \\ B_2 \\ B_3 \\ ... \\ B_Y\nC_1 \\ C_2 \\ C_3 \\ ... \\ C_Z\n\n-----Output-----\nPrint K lines. The i-th line should contain the i-th value stated in the problem statement.\n\n-----Sample Input-----\n2 2 2 8\n4 6\n1 5\n3 8\n\n-----Sample Output-----\n19\n17\n15\n14\n13\n12\n10\n8\n\nThere are 2 \\times 2 \\times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:\n - (A_2, B_2, C_2): 6 + 5 + 8 = 19\n - (A_1, B_2, C_2): 4 + 5 + 8 = 17\n - (A_2, B_1, C_2): 6 + 1 + 8 = 15\n - (A_2, B_2, C_1): 6 + 5 + 3 = 14\n - (A_1, B_1, C_2): 4 + 1 + 8 = 13\n - (A_1, B_2, C_1): 4 + 5 + 3 = 12\n - (A_2, B_1, C_1): 6 + 1 + 3 = 10\n - (A_1, B_1, C_1): 4 + 1 + 3 = 8"} +{"id": "01035", "text": "Given are positive integers A and B.\nLet us choose some number of positive common divisors of A and B.\nHere, any two of the chosen divisors must be coprime.\nAt most, how many divisors can we choose?Definition of common divisor\nAn integer d is said to be a common divisor of integers x and y when d divides both x and y.Definition of being coprime\nIntegers x and y are said to be coprime when x and y have no positive common divisors other than 1.Definition of dividing\nAn integer x is said to divide another integer y when there exists an integer \\alpha such that y = \\alpha x.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq A, B \\leq 10^{12}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the maximum number of divisors that can be chosen to satisfy the condition.\n\n-----Sample Input-----\n12 18\n\n-----Sample Output-----\n3\n\n12 and 18 have the following positive common divisors: 1, 2, 3, and 6.\n1 and 2 are coprime, 2 and 3 are coprime, and 3 and 1 are coprime, so we can choose 1, 2, and 3, which achieve the maximum result."} +{"id": "01036", "text": "To decide which is the strongest among Rock, Paper, and Scissors, we will hold an RPS tournament.\nThere are 2^k players in this tournament, numbered 0 through 2^k-1. Each player has his/her favorite hand, which he/she will use in every match.\nA string s of length n consisting of R, P, and S represents the players' favorite hands.\nSpecifically, the favorite hand of Player i is represented by the ((i\\text{ mod } n) + 1)-th character of s; R, P, and S stand for Rock, Paper, and Scissors, respectively.\nFor l and r such that r-l is a power of 2, the winner of the tournament held among Players l through r-1 will be determined as follows:\n - If r-l=1 (that is, there is just one player), the winner is Player l.\n - If r-l\\geq 2, let m=(l+r)/2, and we hold two tournaments, one among Players l through m-1 and the other among Players m through r-1. Let a and b be the respective winners of these tournaments. a and b then play a match of rock paper scissors, and the winner of this match - or a if the match is drawn - is the winner of the tournament held among Players l through r-1.\nFind the favorite hand of the winner of the tournament held among Players 0 through 2^k-1.\n\n-----Notes-----\n - a\\text{ mod } b denotes the remainder when a is divided by b.\n - The outcome of a match of rock paper scissors is determined as follows:\n - If both players choose the same hand, the match is drawn;\n - R beats S;\n - P beats R;\n - S beats P.\n\n-----Constraints-----\n - 1 \\leq n,k \\leq 100\n - s is a string of length n consisting of R, P, and S.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn k\ns\n\n-----Output-----\nPrint the favorite hand of the winner of the tournament held among Players 0 through 2^k-1, as R, P, or S.\n\n-----Sample Input-----\n3 2\nRPS\n\n-----Sample Output-----\nP\n\n - The favorite hand of the winner of the tournament held among Players 0 through 1 is P.\n - The favorite hand of the winner of the tournament held among Players 2 through 3 is R.\n - The favorite hand of the winner of the tournament held among Players 0 through 3 is P.\nThus, the answer is P.\n P\n \u250c\u2500\u2534\u2500\u2510\n P R\n\u250c\u2534\u2510 \u250c\u2534\u2510\nR P S R\n ```\n S\n \u250c\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2510\n P S\n \u250c\u2500\u2534\u2500\u2510 \u250c\u2500\u2534\u2500\u2510\n P R S P\n\u250c\u2534\u2510 \u250c\u2534\u2510 \u250c\u2534\u2510 \u250c\u2534\u2510\nR P S R P S R P\n```"} +{"id": "01037", "text": "There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.\nYou can rearrange these children just one time in any order you like.\nWhen a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \\times |x-y| happiness points.\nFind the maximum total happiness points the children can earn.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2000\n - 1 \\leq A_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the maximum total happiness points the children can earn.\n\n-----Sample Input-----\n4\n1 3 4 2\n\n-----Sample Output-----\n20\n\nIf we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \\times |1-3|+3 \\times |2-4|+4 \\times |3-1|+2 \\times |4-2|=20 happiness points in total."} +{"id": "01038", "text": "Let f(A, B) be the exclusive OR of A, A+1, ..., B. Find f(A, B).\nWhat is exclusive OR?\nThe bitwise exclusive OR of integers c_1, c_2, ..., c_n (let us call it y) is defined as follows:\n - When y is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if, the number of integers among c_1, c_2, ...c_m whose binary representations have 1 in the 2^k's place, is odd, and 0 if that count is even.\nFor example, the exclusive OR of 3 and 5 is 6. (When written in base two: the exclusive OR of 011 and 101 is 110.)\n\n-----Constraints-----\n - All values in input are integers.\n - 0 \\leq A \\leq B \\leq 10^{12}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nCompute f(A, B) and print it.\n\n-----Sample Input-----\n2 4\n\n-----Sample Output-----\n5\n\n2, 3, 4 are 010, 011, 100 in base two, respectively.\nThe exclusive OR of these is 101, which is 5 in base ten."} +{"id": "01039", "text": "You are given a tree with N vertices.\n\nHere, a tree is a kind of graph, and more specifically, a connected undirected graph with N-1 edges, where N is the number of its vertices.\n\nThe i-th edge (1\u2264i\u2264N-1) connects Vertices a_i and b_i, and has a length of c_i.\nYou are also given Q queries and an integer K. In the j-th query (1\u2264j\u2264Q):\n - find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K.\n\n-----Constraints-----\n - 3\u2264N\u226410^5 \n - 1\u2264a_i,b_i\u2264N (1\u2264i\u2264N-1) \n - 1\u2264c_i\u226410^9 (1\u2264i\u2264N-1) \n - The given graph is a tree.\n - 1\u2264Q\u226410^5 \n - 1\u2264K\u2264N \n - 1\u2264x_j,y_j\u2264N (1\u2264j\u2264Q) \n - x_j\u2260y_j (1\u2264j\u2264Q) \n - x_j\u2260K,y_j\u2260K (1\u2264j\u2264Q) \n\n-----Input-----\nInput is given from Standard Input in the following format: \nN \na_1 b_1 c_1 \n: \na_{N-1} b_{N-1} c_{N-1}\nQ K\nx_1 y_1\n: \nx_{Q} y_{Q}\n\n-----Output-----\nPrint the responses to the queries in Q lines.\n\nIn the j-th line j(1\u2264j\u2264Q), print the response to the j-th query.\n\n-----Sample Input-----\n5\n1 2 1\n1 3 1\n2 4 1\n3 5 1\n3 1\n2 4\n2 3\n4 5\n\n-----Sample Output-----\n3\n2\n4\n\nThe shortest paths for the three queries are as follows:\n - Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3 \n - Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2 \n - Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4"} +{"id": "01040", "text": "Given is a string S of length N consisting of lowercase English letters.\nSnuke can do this operation any number of times: remove fox occurring as a substring from s and concatenate the remaining parts of s.\nWhat is the minimum possible length of s after some number of operations by Snuke?\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^{5}\n - s is a string of length N consisting of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\ns\n\n-----Print-----\nPrint the minimum possible length of s after some number of operations by Snuke.\n\n-----Sample Input-----\n6\nicefox\n\n-----Sample Output-----\n3\n\n - By removing the fox at the end of icefox, we can turn s into ice."} +{"id": "01041", "text": "n evenly spaced points have been marked around the edge of a circle. There is a number written at each point. You choose a positive real number k. Then you may repeatedly select a set of 2 or more points which are evenly spaced, and either increase all numbers at points in the set by k or decrease all numbers at points in the set by k. You would like to eventually end up with all numbers equal to 0. Is it possible?\n\nA set of 2 points is considered evenly spaced if they are diametrically opposed, and a set of 3 or more points is considered evenly spaced if they form a regular polygon.\n\n\n-----Input-----\n\nThe first line of input contains an integer n (3 \u2264 n \u2264 100000), the number of points along the circle.\n\nThe following line contains a string s with exactly n digits, indicating the numbers initially present at each of the points, in clockwise order.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if there is some sequence of operations that results in all numbers being 0, otherwise \"NO\" (without quotes).\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n30\n000100000100000110000000001100\n\nOutput\nYES\n\nInput\n6\n314159\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIf we label the points from 1 to n, then for the first test case we can set k = 1. Then we increase the numbers at points 7 and 22 by 1, then decrease the numbers at points 7, 17, and 27 by 1, then decrease the numbers at points 4, 10, 16, 22, and 28 by 1."} +{"id": "01042", "text": "Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 \u2264 a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7.\n\ngcd here means the greatest common divisor.\n\n\n-----Input-----\n\nThe only line contains two positive integers x and y (1 \u2264 x, y \u2264 10^9).\n\n\n-----Output-----\n\nPrint the number of such sequences modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n3 9\n\nOutput\n3\n\nInput\n5 8\n\nOutput\n0\n\n\n\n-----Note-----\n\nThere are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3).\n\nThere are no suitable sequences in the second test."} +{"id": "01043", "text": "You are organizing a boxing tournament, where $n$ boxers will participate ($n$ is a power of $2$), and your friend is one of them. All boxers have different strength from $1$ to $n$, and boxer $i$ wins in the match against boxer $j$ if and only if $i$ is stronger than $j$.\n\nThe tournament will be organized as follows: $n$ boxers will be divided into pairs; the loser in each pair leaves the tournament, and $\\frac{n}{2}$ winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner).\n\nYour friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower.\n\nFurthermore, during each stage you distribute the boxers into pairs as you wish.\n\nThe boxer with strength $i$ can be bribed if you pay him $a_i$ dollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 2^{18}$) \u2014 the number of boxers. $n$ is a power of $2$.\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$, where $a_i$ is the number of dollars you have to pay if you want to bribe the boxer with strength $i$. Exactly one of $a_i$ is equal to $-1$ \u2014 it means that the boxer with strength $i$ is your friend. All other values are in the range $[1, 10^9]$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of dollars you have to pay so your friend wins.\n\n\n-----Examples-----\nInput\n4\n3 9 1 -1\n\nOutput\n0\nInput\n8\n11 -1 13 19 24 7 17 5\n\nOutput\n12\n\n\n-----Note-----\n\nIn the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.\n\nIn the second test case you can distribute boxers as follows (your friend is number $2$):\n\n$1 : 2, 8 : 5, 7 : 3, 6 : 4$ (boxers $2, 8, 7$ and $6$ advance to the next stage);\n\n$2 : 6, 8 : 7$ (boxers $2$ and $8$ advance to the next stage, you have to bribe the boxer with strength $6$);\n\n$2 : 8$ (you have to bribe the boxer with strength $8$);"} +{"id": "01044", "text": "Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the second, second is connected with third and so on, while the last one is connected with the first one again. Cycle may consist of a single isolated vertex.\n\nInitially there are k cycles, i-th of them consisting of exactly v_{i} vertices. Players play alternatively. Peter goes first. On each turn a player must choose a cycle with at least 2 vertices (for example, x vertices) among all available cycles and replace it by two cycles with p and x - p vertices where 1 \u2264 p < x is chosen by the player. The player who cannot make a move loses the game (and his life!).\n\nPeter wants to test some configurations of initial cycle sets before he actually plays with Dr. Octopus. Initially he has an empty set. In the i-th test he adds a cycle with a_{i} vertices to the set (this is actually a multiset because it can contain two or more identical cycles). After each test, Peter wants to know that if the players begin the game with the current set of cycles, who wins? \n\nPeter is pretty good at math, but now he asks you to help.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of tests Peter is about to make.\n\nThe second line contains n space separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9), i-th of them stands for the number of vertices in the cycle added before the i-th test.\n\n\n-----Output-----\n\nPrint the result of all tests in order they are performed. Print 1 if the player who moves first wins or 2 otherwise.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n2\n1\n1\n\nInput\n5\n1 1 5 1 1\n\nOutput\n2\n2\n2\n2\n2\n\n\n\n-----Note-----\n\nIn the first sample test:\n\nIn Peter's first test, there's only one cycle with 1 vertex. First player cannot make a move and loses.\n\nIn his second test, there's one cycle with 1 vertex and one with 2. No one can make a move on the cycle with 1 vertex. First player can replace the second cycle with two cycles of 1 vertex and second player can't make any move and loses.\n\nIn his third test, cycles have 1, 2 and 3 vertices. Like last test, no one can make a move on the first cycle. First player can replace the third cycle with one cycle with size 1 and one with size 2. Now cycles have 1, 1, 2, 2 vertices. Second player's only move is to replace a cycle of size 2 with 2 cycles of size 1. And cycles are 1, 1, 1, 1, 2. First player replaces the last cycle with 2 cycles with size 1 and wins.\n\nIn the second sample test:\n\nHaving cycles of size 1 is like not having them (because no one can make a move on them). \n\nIn Peter's third test: There a cycle of size 5 (others don't matter). First player has two options: replace it with cycles of sizes 1 and 4 or 2 and 3.\n\n If he replaces it with cycles of sizes 1 and 4: Only second cycle matters. Second player will replace it with 2 cycles of sizes 2. First player's only option to replace one of them with two cycles of size 1. Second player does the same thing with the other cycle. First player can't make any move and loses. If he replaces it with cycles of sizes 2 and 3: Second player will replace the cycle of size 3 with two of sizes 1 and 2. Now only cycles with more than one vertex are two cycles of size 2. As shown in previous case, with 2 cycles of size 2 second player wins. \n\nSo, either way first player loses."} +{"id": "01045", "text": "Vanya got n cubes. He decided to build a pyramid from them. Vanya wants to build the pyramid as follows: the top level of the pyramid must consist of 1 cube, the second level must consist of 1 + 2 = 3 cubes, the third level must have 1 + 2 + 3 = 6 cubes, and so on. Thus, the i-th level of the pyramid must have 1 + 2 + ... + (i - 1) + i cubes.\n\nVanya wants to know what is the maximum height of the pyramid that he can make using the given cubes.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^4) \u2014 the number of cubes given to Vanya.\n\n\n-----Output-----\n\nPrint the maximum possible height of the pyramid in the single line.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n25\n\nOutput\n4\n\n\n\n-----Note-----\n\nIllustration to the second sample: [Image]"} +{"id": "01046", "text": "Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.\n\nOne day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.\n\nHelp Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.\n\nNote that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted \u2014 that is, one call connects exactly two people.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^3) \u2014 the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id_1, id_2, ..., id_{n} (0 \u2264 id_{i} \u2264 10^9). Number id_{i} equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise.\n\nConsider the secretaries indexed from 1 to n in some way.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.\n\n\n-----Examples-----\nInput\n6\n0 1 7 1 7 10\n\nOutput\n2\n\nInput\n3\n1 1 1\n\nOutput\n-1\n\nInput\n1\n0\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.\n\nIn the second test sample the described situation is impossible as conferences aren't allowed."} +{"id": "01047", "text": "A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011\u00a0\u2014 are quasibinary and numbers 2, 12, 900 are not.\n\nYou are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^6).\n\n\n-----Output-----\n\nIn the first line print a single integer k\u00a0\u2014 the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.\n\nIn the second line print k numbers \u2014 the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n9\n\nOutput\n9\n1 1 1 1 1 1 1 1 1 \n\nInput\n32\n\nOutput\n3\n10 11 11"} +{"id": "01048", "text": "Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform: U \u2014 move from the cell (x, y) to (x, y + 1); D \u2014 move from (x, y) to (x, y - 1); L \u2014 move from (x, y) to (x - 1, y); R \u2014 move from (x, y) to (x + 1, y). \n\nIvan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!\n\n\n-----Input-----\n\nThe first line contains one number n \u2014 the length of sequence of commands entered by Ivan (1 \u2264 n \u2264 100).\n\nThe second line contains the sequence itself \u2014 a string consisting of n characters. Each character can be U, D, L or R.\n\n\n-----Output-----\n\nPrint the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.\n\n\n-----Examples-----\nInput\n4\nLDUR\n\nOutput\n4\n\nInput\n5\nRRRUU\n\nOutput\n0\n\nInput\n6\nLLRRRR\n\nOutput\n4"} +{"id": "01049", "text": "Arya has n opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya will beat all present opponents. Otherwise, if all opponents are present, then they will beat Arya.\n\nFor each opponent Arya knows his schedule\u00a0\u2014 whether or not he is going to present on each particular day. Tell him the maximum number of consecutive days that he will beat all present opponents.\n\nNote, that if some day there are no opponents present, Arya still considers he beats all the present opponents.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and d (1 \u2264 n, d \u2264 100)\u00a0\u2014 the number of opponents and the number of days, respectively.\n\nThe i-th of the following d lines contains a string of length n consisting of characters '0' and '1'. The j-th character of this string is '0' if the j-th opponent is going to be absent on the i-th day.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the maximum number of consecutive days that Arya will beat all present opponents.\n\n\n-----Examples-----\nInput\n2 2\n10\n00\n\nOutput\n2\n\nInput\n4 1\n0100\n\nOutput\n1\n\nInput\n4 5\n1101\n1111\n0110\n1011\n1111\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first and the second samples, Arya will beat all present opponents each of the d days.\n\nIn the third sample, Arya will beat his opponents on days 1, 3 and 4 and his opponents will beat him on days 2 and 5. Thus, the maximum number of consecutive winning days is 2, which happens on days 3 and 4."} +{"id": "01050", "text": "Vus the Cossack holds a programming competition, in which $n$ people participate. He decided to award them all with pens and notebooks. It is known that Vus has exactly $m$ pens and $k$ notebooks.\n\nDetermine whether the Cossack can reward all participants, giving each of them at least one pen and at least one notebook.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$, and $k$ ($1 \\leq n, m, k \\leq 100$)\u00a0\u2014 the number of participants, the number of pens, and the number of notebooks respectively.\n\n\n-----Output-----\n\nPrint \"Yes\" if it possible to reward all the participants. Otherwise, print \"No\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n5 8 6\n\nOutput\nYes\n\nInput\n3 9 3\n\nOutput\nYes\n\nInput\n8 5 20\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first example, there are $5$ participants. The Cossack has $8$ pens and $6$ notebooks. Therefore, he has enough pens and notebooks.\n\nIn the second example, there are $3$ participants. The Cossack has $9$ pens and $3$ notebooks. He has more than enough pens but only the minimum needed number of notebooks.\n\nIn the third example, there are $8$ participants but only $5$ pens. Since the Cossack does not have enough pens, the answer is \"No\"."} +{"id": "01051", "text": "This year, as in previous years, MemSQL is inviting the top 25 competitors from the Start[c]up qualification round to compete onsite for the final round. Not everyone who is eligible to compete onsite can afford to travel to the office, though. Initially the top 25 contestants are invited to come onsite. Each eligible contestant must either accept or decline the invitation. Whenever a contestant declines, the highest ranked contestant not yet invited is invited to take the place of the one that declined. This continues until 25 contestants have accepted invitations.\n\nAfter the qualifying round completes, you know K of the onsite finalists, as well as their qualifying ranks (which start at 1, there are no ties). Determine the minimum possible number of contestants that declined the invitation to compete onsite in the final round.\n\n\n-----Input-----\n\nThe first line of input contains K (1 \u2264 K \u2264 25), the number of onsite finalists you know. The second line of input contains r_1, r_2, ..., r_{K} (1 \u2264 r_{i} \u2264 10^6), the qualifying ranks of the finalists you know. All these ranks are distinct.\n\n\n-----Output-----\n\nPrint the minimum possible number of contestants that declined the invitation to compete onsite.\n\n\n-----Examples-----\nInput\n25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28\n\nOutput\n3\n\nInput\n5\n16 23 8 15 4\n\nOutput\n0\n\nInput\n3\n14 15 92\n\nOutput\n67\n\n\n\n-----Note-----\n\nIn the first example, you know all 25 onsite finalists. The contestants who ranked 1-st, 13-th, and 27-th must have declined, so the answer is 3."} +{"id": "01052", "text": "A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.\n\nLet's call a permutation an almost identity permutation iff there exist at least n - k indices i (1 \u2264 i \u2264 n) such that p_{i} = i.\n\nYour task is to count the number of almost identity permutations for given numbers n and k.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (4 \u2264 n \u2264 1000, 1 \u2264 k \u2264 4).\n\n\n-----Output-----\n\nPrint the number of almost identity permutations for given n and k.\n\n\n-----Examples-----\nInput\n4 1\n\nOutput\n1\n\nInput\n4 2\n\nOutput\n7\n\nInput\n5 3\n\nOutput\n31\n\nInput\n5 4\n\nOutput\n76"} +{"id": "01053", "text": "Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 \u2264 u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \\oplus v$ (where $\\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph?\n\nYou can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph\n\nYou can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree\n\nThe weight of the minimum spanning tree is the sum of the weights on the edges included in it.\n\n\n-----Input-----\n\nThe only line contains an integer n (2 \u2264 n \u2264 10^12), the number of vertices in the graph.\n\n\n-----Output-----\n\nThe only line contains an integer x, the weight of the graph's minimum spanning tree.\n\n\n-----Example-----\nInput\n4\n\nOutput\n4\n\n\n-----Note-----\n\nIn the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4."} +{"id": "01054", "text": "Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. \n\nLet's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. \n\nBuilding a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city.\n\n\n-----Input-----\n\nThe first line of the input contains number n\u00a0\u2014 the number of mines on the map (2 \u2264 n \u2264 1000). Each of the next n lines contains a pair of integers x_{i} and y_{i}\u00a0\u2014 the coordinates of the corresponding mine ( - 10^9 \u2264 x_{i}, y_{i} \u2264 10^9). All points are pairwise distinct.\n\n\n-----Output-----\n\nPrint the minimum area of the city that can cover all the mines with valuable resources.\n\n\n-----Examples-----\nInput\n2\n0 0\n2 2\n\nOutput\n4\n\nInput\n2\n0 0\n0 3\n\nOutput\n9"} +{"id": "01055", "text": "Thanos sort is a supervillain sorting algorithm, which works as follows: if the array is not sorted, snap your fingers* to remove the first or the second half of the items, and repeat the process.\n\nGiven an input array, what is the size of the longest sorted array you can obtain from it using Thanos sort?\n\n*Infinity Gauntlet required.\n\n\n-----Input-----\n\nThe first line of input contains a single number $n$ ($1 \\le n \\le 16$) \u2014 the size of the array. $n$ is guaranteed to be a power of 2.\n\nThe second line of input contains $n$ space-separated integers $a_i$ ($1 \\le a_i \\le 100$) \u2014 the elements of the array.\n\n\n-----Output-----\n\nReturn the maximal length of a sorted array you can obtain using Thanos sort. The elements of the array have to be sorted in non-decreasing order.\n\n\n-----Examples-----\nInput\n4\n1 2 2 4\n\nOutput\n4\n\nInput\n8\n11 12 1 2 13 14 3 4\n\nOutput\n2\n\nInput\n4\n7 6 5 4\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example the array is already sorted, so no finger snaps are required.\n\nIn the second example the array actually has a subarray of 4 sorted elements, but you can not remove elements from different sides of the array in one finger snap. Each time you have to remove either the whole first half or the whole second half, so you'll have to snap your fingers twice to get to a 2-element sorted array.\n\nIn the third example the array is sorted in decreasing order, so you can only save one element from the ultimate destruction."} +{"id": "01056", "text": "Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple just playing \"Cat's Cradle\" so now she wants to try a different game \u2014 \"Snakes and Ladders\". Unfortunately, she already killed all the snakes, so there are only ladders left now. \n\nThe game is played on a $10 \\times 10$ board as follows: At the beginning of the game, the player is at the bottom left square. The objective of the game is for the player to reach the Goal (the top left square) by following the path and climbing vertical ladders. Once the player reaches the Goal, the game ends. The path is as follows: if a square is not the end of its row, it leads to the square next to it along the direction of its row; if a square is the end of its row, it leads to the square above it. The direction of a row is determined as follows: the direction of the bottom row is to the right; the direction of any other row is opposite the direction of the row below it. See Notes section for visualization of path. During each turn, the player rolls a standard six-sided dice. Suppose that the number shown on the dice is $r$. If the Goal is less than $r$ squares away on the path, the player doesn't move (but the turn is performed). Otherwise, the player advances exactly $r$ squares along the path and then stops. If the player stops on a square with the bottom of a ladder, the player chooses whether or not to climb up that ladder. If she chooses not to climb, then she stays in that square for the beginning of the next turn. Some squares have a ladder in them. Ladders are only placed vertically \u2014 each one leads to the same square of some of the upper rows. In order for the player to climb up a ladder, after rolling the dice, she must stop at the square containing the bottom of the ladder. After using the ladder, the player will end up in the square containing the top of the ladder. She cannot leave the ladder in the middle of climbing. And if the square containing the top of the ladder also contains the bottom of another ladder, she is not allowed to use that second ladder. The numbers on the faces of the dice are 1, 2, 3, 4, 5, and 6, with each number having the same probability of being shown. \n\nPlease note that: it is possible for ladders to overlap, but the player cannot switch to the other ladder while in the middle of climbing the first one; it is possible for ladders to go straight to the top row, but not any higher; it is possible for two ladders to lead to the same tile; it is possible for a ladder to lead to a tile that also has a ladder, but the player will not be able to use that second ladder if she uses the first one; the player can only climb up ladders, not climb down. \n\nHyakugoku wants to finish the game as soon as possible. Thus, on each turn she chooses whether to climb the ladder or not optimally. Help her to determine the minimum expected number of turns the game will take.\n\n\n-----Input-----\n\nInput will consist of ten lines. The $i$-th line will contain 10 non-negative integers $h_{i1}, h_{i2}, \\dots, h_{i10}$. If $h_{ij}$ is $0$, then the tile at the $i$-th row and $j$-th column has no ladder. Otherwise, the ladder at that tile will have a height of $h_{ij}$, i.e. climbing it will lead to the tile $h_{ij}$ rows directly above. It is guaranteed that $0 \\leq h_{ij} < i$. Also, the first number of the first line and the first number of the last line always contain $0$, i.e. the Goal and the starting tile never have ladders.\n\n\n-----Output-----\n\nPrint only one line containing a single floating-point number \u2014 the minimum expected number of turns Hyakugoku can take to finish the game. Your answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$.\n\n\n-----Examples-----\nInput\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n\nOutput\n33.0476190476\n\nInput\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 3 0 0 0 4 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 4 0 0 0\n0 0 3 0 0 0 0 0 0 0\n0 0 4 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 9\n\nOutput\n20.2591405923\n\nInput\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 6 6 6 6 6 6 0 0 0\n1 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n\nOutput\n15.9047592939\n\n\n\n-----Note-----\n\nA visualization of the path and the board from example 2 is as follows: [Image]\n\nThe tile with an 'S' is the starting tile and the tile with an 'E' is the Goal.\n\nFor the first example, there are no ladders.\n\nFor the second example, the board looks like the one in the right part of the image (the ladders have been colored for clarity).\n\nIt is possible for ladders to overlap, as is the case with the red and yellow ladders and green and blue ladders. It is also possible for ladders to go straight to the top, as is the case with the black and blue ladders. However, it is not possible for ladders to go any higher (outside of the board). It is also possible that two ladders lead to the same tile, as is the case with the red and yellow ladders. Also, notice that the red and yellow ladders lead to the tile with the orange ladder. So if the player chooses to climb either of the red and yellow ladders, they will not be able to climb the orange ladder. Finally, notice that the green ladder passes through the starting tile of the blue ladder. The player cannot transfer from the green ladder to the blue ladder while in the middle of climbing the green ladder."} +{"id": "01057", "text": "You are given a string $s$ of length $n$ consisting only of lowercase Latin letters.\n\nA substring of a string is a contiguous subsequence of that string. So, string \"forces\" is substring of string \"codeforces\", but string \"coder\" is not.\n\nYour task is to calculate the number of ways to remove exactly one substring from this string in such a way that all remaining characters are equal (the number of distinct characters either zero or one).\n\nIt is guaranteed that there is at least two different characters in $s$.\n\nNote that you can remove the whole string and it is correct. Also note that you should remove at least one character.\n\nSince the answer can be rather large (not very large though) print it modulo $998244353$.\n\nIf you are Python programmer, consider using PyPy instead of Python when you submit your code.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of the string $s$.\n\nThe second line of the input contains the string $s$ of length $n$ consisting only of lowercase Latin letters.\n\nIt is guaranteed that there is at least two different characters in $s$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of ways modulo $998244353$ to remove exactly one substring from $s$ in such way that all remaining characters are equal.\n\n\n-----Examples-----\nInput\n4\nabaa\n\nOutput\n6\n\nInput\n7\naacdeee\n\nOutput\n6\nInput\n2\naz\n\nOutput\n3\n\n\n-----Note-----\n\nLet $s[l; r]$ be the substring of $s$ from the position $l$ to the position $r$ inclusive.\n\nThen in the first example you can remove the following substrings: $s[1; 2]$; $s[1; 3]$; $s[1; 4]$; $s[2; 2]$; $s[2; 3]$; $s[2; 4]$. \n\nIn the second example you can remove the following substrings: $s[1; 4]$; $s[1; 5]$; $s[1; 6]$; $s[1; 7]$; $s[2; 7]$; $s[3; 7]$. \n\nIn the third example you can remove the following substrings: $s[1; 1]$; $s[1; 2]$; $s[2; 2]$."} +{"id": "01058", "text": "You are given $n$ blocks, each of them is of the form [color$_1$|value|color$_2$], where the block can also be flipped to get [color$_2$|value|color$_1$]. \n\nA sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C.\n\nThe value of the sequence is defined as the sum of the values of the blocks in this sequence.\n\nFind the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence.\n\n\n-----Input-----\n\nThe first line of input contains a single integer $n$ ($1 \\le n \\le 100$)\u00a0\u2014 the number of given blocks.\n\nEach of the following $n$ lines describes corresponding block and consists of $\\mathrm{color}_{1,i}$, $\\mathrm{value}_i$ and $\\mathrm{color}_{2,i}$ ($1 \\le \\mathrm{color}_{1,i}, \\mathrm{color}_{2,i} \\le 4$, $1 \\le \\mathrm{value}_i \\le 100\\,000$).\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the maximum total value of the subset of blocks, which makes a valid sequence.\n\n\n-----Examples-----\nInput\n6\n2 1 4\n1 2 4\n3 4 4\n2 8 3\n3 16 3\n1 32 2\n\nOutput\n63\nInput\n7\n1 100000 1\n1 100000 2\n1 100000 2\n4 50000 3\n3 50000 4\n4 50000 4\n3 50000 3\n\nOutput\n300000\nInput\n4\n1 1000 1\n2 500 2\n3 250 3\n4 125 4\n\nOutput\n1000\n\n\n-----Note-----\n\nIn the first example, it is possible to form a valid sequence from all blocks.\n\nOne of the valid sequences is the following:\n\n[4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2]\n\nThe first block from the input ([2|1|4] $\\to$ [4|1|2]) and second ([1|2|4] $\\to$ [4|2|1]) are flipped.\n\nIn the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped):\n\n[2|100000|1] [1|100000|1] [1|100000|2]\n\nIn the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value."} +{"id": "01059", "text": "Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $k$ is vowelly if there are positive integers $n$ and $m$ such that $n\\cdot m = k$ and when the word is written by using $n$ rows and $m$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.\n\nYou are given an integer $k$ and you must either print a vowelly word of length $k$ or print $-1$ if no such word exists.\n\nIn this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.\n\n\n-----Input-----\n\nInput consists of a single line containing the integer $k$ ($1\\leq k \\leq 10^4$)\u00a0\u2014 the required length.\n\n\n-----Output-----\n\nThe output must consist of a single line, consisting of a vowelly word of length $k$ consisting of lowercase English letters if it exists or $-1$ if it does not.\n\nIf there are multiple possible words, you may output any of them.\n\n\n-----Examples-----\nInput\n7\n\nOutput\n-1\n\nInput\n36\n\nOutput\nagoeuioaeiruuimaeoieauoweouoiaouimae\n\n\n-----Note-----\n\nIn the second example, the word \"agoeuioaeiruuimaeoieauoweouoiaouimae\" can be arranged into the following $6 \\times 6$ grid: $\\left. \\begin{array}{|c|c|c|c|c|c|} \\hline a & {g} & {o} & {e} & {u} & {i} \\\\ \\hline o & {a} & {e} & {i} & {r} & {u} \\\\ \\hline u & {i} & {m} & {a} & {e} & {o} \\\\ \\hline i & {e} & {a} & {u} & {o} & {w} \\\\ \\hline e & {o} & {u} & {o} & {i} & {a} \\\\ \\hline o & {u} & {i} & {m} & {a} & {e} \\\\ \\hline \\end{array} \\right.$ \n\nIt is easy to verify that every row and every column contain all the vowels."} +{"id": "01060", "text": "As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.\n\nJust in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.\n\nLet's define a divisibility graph for a set of positive integers A = {a_1, a_2, ..., a_{n}} as follows. The vertices of the given graph are numbers from set A, and two numbers a_{i} and a_{j} (i \u2260 j) are connected by an edge if and only if either a_{i} is divisible by a_{j}, or a_{j} is divisible by a_{i}.\n\nYou are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^6), that sets the size of set A.\n\nThe second line contains n distinct positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^6) \u2014 elements of subset A. The numbers in the line follow in the ascending order.\n\n\n-----Output-----\n\nPrint a single number \u2014 the maximum size of a clique in a divisibility graph for set A.\n\n\n-----Examples-----\nInput\n8\n3 4 6 8 10 18 21 24\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph."} +{"id": "01061", "text": "You've got a 5 \u00d7 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix: Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 \u2264 i < 5). Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 \u2264 j < 5). \n\nYou think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.\n\n\n-----Input-----\n\nThe input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of moves needed to make the matrix beautiful.\n\n\n-----Examples-----\nInput\n0 0 0 0 0\n0 0 0 0 1\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n\nOutput\n3\n\nInput\n0 0 0 0 0\n0 0 0 0 0\n0 1 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n\nOutput\n1"} +{"id": "01062", "text": "Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings S and T of equal length to be \"similar\". After a brief search on the Internet, he learned about the Hamming distance between two strings S and T of the same length, which is defined as the number of positions in which S and T have different characters. For example, the Hamming distance between words \"permanent\" and \"pergament\" is two, as these words differ in the fourth and sixth letters.\n\nMoreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string S, so that the Hamming distance between a new string S and string T would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.\n\nHelp him do this!\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 200 000) \u2014 the length of strings S and T.\n\nThe second line contains string S.\n\nThe third line contains string T.\n\nEach of the lines only contains lowercase Latin letters.\n\n\n-----Output-----\n\nIn the first line, print number x \u2014 the minimum possible Hamming distance between strings S and T if you swap at most one pair of letters in S.\n\nIn the second line, either print the indexes i and j (1 \u2264 i, j \u2264 n, i \u2260 j), if reaching the minimum possible distance is possible by swapping letters on positions i and j, or print \"-1 -1\", if it is not necessary to swap characters.\n\nIf there are multiple possible answers, print any of them.\n\n\n-----Examples-----\nInput\n9\npergament\npermanent\n\nOutput\n1\n4 6\n\nInput\n6\nwookie\ncookie\n\nOutput\n1\n-1 -1\n\nInput\n4\npetr\negor\n\nOutput\n2\n1 2\n\nInput\n6\ndouble\nbundle\n\nOutput\n2\n4 1\n\n\n\n-----Note-----\n\nIn the second test it is acceptable to print i = 2, j = 3."} +{"id": "01063", "text": "Peter wrote on the board a strictly increasing sequence of positive integers a_1, a_2, ..., a_{n}. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.\n\nRestore the the original sequence knowing digits remaining on the board.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 10^5) \u2014 the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.\n\n\n-----Output-----\n\nIf the answer exists, print in the first line \"YES\" (without the quotes). Next n lines must contain the sequence of positive integers \u2014 a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them.\n\nIf there is no answer, print a single line \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n3\n?\n18\n1?\n\nOutput\nYES\n1\n18\n19\n\nInput\n2\n??\n?\n\nOutput\nNO\n\nInput\n5\n12224\n12??5\n12226\n?0000\n?00000\n\nOutput\nYES\n12224\n12225\n12226\n20000\n100000"} +{"id": "01064", "text": "Adilbek's house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has $n$ positions to install lamps, they correspond to the integer numbers from $0$ to $n - 1$ on the OX axis. However, some positions are blocked and no post lamp can be placed there.\n\nThere are post lamps of different types which differ only by their power. When placed in position $x$, post lamp of power $l$ illuminates the segment $[x; x + l]$. The power of each post lamp is always a positive integer number.\n\nThe post lamp shop provides an infinite amount of lamps of each type from power $1$ to power $k$. Though each customer is only allowed to order post lamps of exactly one type. Post lamps of power $l$ cost $a_l$ each.\n\nWhat is the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street? If some lamps illuminate any other segment of the street, Adilbek does not care, so, for example, he may place a lamp of power $3$ in position $n - 1$ (even though its illumination zone doesn't completely belong to segment $[0; n]$).\n\n\n-----Input-----\n\nThe first line contains three integer numbers $n$, $m$ and $k$ ($1 \\le k \\le n \\le 10^6$, $0 \\le m \\le n$) \u2014 the length of the segment of the street Adilbek wants to illuminate, the number of the blocked positions and the maximum power of the post lamp available.\n\nThe second line contains $m$ integer numbers $s_1, s_2, \\dots, s_m$ ($0 \\le s_1 < s_2 < \\dots s_m < n$) \u2014 the blocked positions.\n\nThe third line contains $k$ integer numbers $a_1, a_2, \\dots, a_k$ ($1 \\le a_i \\le 10^6$) \u2014 the costs of the post lamps.\n\n\n-----Output-----\n\nPrint the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street.\n\nIf illumintaing the entire segment $[0; n]$ is impossible, print -1.\n\n\n-----Examples-----\nInput\n6 2 3\n1 3\n1 2 3\n\nOutput\n6\n\nInput\n4 3 4\n1 2 3\n1 10 100 1000\n\nOutput\n1000\n\nInput\n5 1 5\n0\n3 3 3 3 3\n\nOutput\n-1\n\nInput\n7 4 3\n2 4 5 6\n3 14 15\n\nOutput\n-1"} +{"id": "01065", "text": "$k$ people want to split $n$ candies between them. Each candy should be given to exactly one of them or be thrown away.\n\nThe people are numbered from $1$ to $k$, and Arkady is the first of them. To split the candies, Arkady will choose an integer $x$ and then give the first $x$ candies to himself, the next $x$ candies to the second person, the next $x$ candies to the third person and so on in a cycle. The leftover (the remainder that is not divisible by $x$) will be thrown away.\n\nArkady can't choose $x$ greater than $M$ as it is considered greedy. Also, he can't choose such a small $x$ that some person will receive candies more than $D$ times, as it is considered a slow splitting.\n\nPlease find what is the maximum number of candies Arkady can receive by choosing some valid $x$.\n\n\n-----Input-----\n\nThe only line contains four integers $n$, $k$, $M$ and $D$ ($2 \\le n \\le 10^{18}$, $2 \\le k \\le n$, $1 \\le M \\le n$, $1 \\le D \\le \\min{(n, 1000)}$, $M \\cdot D \\cdot k \\ge n$)\u00a0\u2014 the number of candies, the number of people, the maximum number of candies given to a person at once, the maximum number of times a person can receive candies.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum possible number of candies Arkady can give to himself.\n\nNote that it is always possible to choose some valid $x$.\n\n\n-----Examples-----\nInput\n20 4 5 2\n\nOutput\n8\n\nInput\n30 9 4 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example Arkady should choose $x = 4$. He will give $4$ candies to himself, $4$ candies to the second person, $4$ candies to the third person, then $4$ candies to the fourth person and then again $4$ candies to himself. No person is given candies more than $2$ times, and Arkady receives $8$ candies in total.\n\nNote that if Arkady chooses $x = 5$, he will receive only $5$ candies, and if he chooses $x = 3$, he will receive only $3 + 3 = 6$ candies as well as the second person, the third and the fourth persons will receive $3$ candies, and $2$ candies will be thrown away. He can't choose $x = 1$ nor $x = 2$ because in these cases he will receive candies more than $2$ times.\n\nIn the second example Arkady has to choose $x = 4$, because any smaller value leads to him receiving candies more than $1$ time."} +{"id": "01066", "text": "Being a nonconformist, Volodya is displeased with the current state of things, particularly with the order of natural numbers (natural number is positive integer number). He is determined to rearrange them. But there are too many natural numbers, so Volodya decided to start with the first n. He writes down the following sequence of numbers: firstly all odd integers from 1 to n (in ascending order), then all even integers from 1 to n (also in ascending order). Help our hero to find out which number will stand at the position number k.\n\n\n-----Input-----\n\nThe only line of input contains integers n and k (1 \u2264 k \u2264 n \u2264 10^12).\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nPrint the number that will stand at the position number k after Volodya's manipulations.\n\n\n-----Examples-----\nInput\n10 3\n\nOutput\n5\nInput\n7 7\n\nOutput\n6\n\n\n-----Note-----\n\nIn the first sample Volodya's sequence will look like this: {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}. The third place in the sequence is therefore occupied by the number 5."} +{"id": "01067", "text": "You are given $n$ numbers $a_1, a_2, \\dots, a_n$. With a cost of one coin you can perform the following operation:\n\nChoose one of these numbers and add or subtract $1$ from it.\n\nIn particular, we can apply this operation to the same number several times.\n\nWe want to make the product of all these numbers equal to $1$, in other words, we want $a_1 \\cdot a_2$ $\\dots$ $\\cdot a_n = 1$. \n\nFor example, for $n = 3$ and numbers $[1, -3, 0]$ we can make product equal to $1$ in $3$ coins: add $1$ to second element, add $1$ to second element again, subtract $1$ from third element, so that array becomes $[1, -1, -1]$. And $1\\cdot (-1) \\cdot (-1) = 1$.\n\nWhat is the minimum cost we will have to pay to do that?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the number of numbers.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^9 \\le a_i \\le 10^9$)\u00a0\u2014 the numbers.\n\n\n-----Output-----\n\nOutput a single number\u00a0\u2014 the minimal number of coins you need to pay to make the product equal to $1$.\n\n\n-----Examples-----\nInput\n2\n-1 1\n\nOutput\n2\nInput\n4\n0 0 0 0\n\nOutput\n4\nInput\n5\n-5 -3 5 3 0\n\nOutput\n13\n\n\n-----Note-----\n\nIn the first example, you can change $1$ to $-1$ or $-1$ to $1$ in $2$ coins.\n\nIn the second example, you have to apply at least $4$ operations for the product not to be $0$.\n\nIn the third example, you can change $-5$ to $-1$ in $4$ coins, $-3$ to $-1$ in $2$ coins, $5$ to $1$ in $4$ coins, $3$ to $1$ in $2$ coins, $0$ to $1$ in $1$ coin."} +{"id": "01068", "text": "A correct expression of the form a+b=c was written; a, b and c are non-negative integers without leading zeros. In this expression, the plus and equally signs were lost. The task is to restore the expression. In other words, one character '+' and one character '=' should be inserted into given sequence of digits so that: character'+' is placed on the left of character '=', characters '+' and '=' split the sequence into three non-empty subsequences consisting of digits (let's call the left part a, the middle part\u00a0\u2014 b and the right part\u00a0\u2014 c), all the three parts a, b and c do not contain leading zeros, it is true that a+b=c. \n\nIt is guaranteed that in given tests answer always exists.\n\n\n-----Input-----\n\nThe first line contains a non-empty string consisting of digits. The length of the string does not exceed 10^6.\n\n\n-----Output-----\n\nOutput the restored expression. If there are several solutions, you can print any of them.\n\nNote that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be. \n\nDo not separate numbers and operation signs with spaces. Strictly follow the output format given in the examples.\n\nIf you remove symbol '+' and symbol '=' from answer string you should get a string, same as string from the input data.\n\n\n-----Examples-----\nInput\n12345168\n\nOutput\n123+45=168\n\nInput\n099\n\nOutput\n0+9=9\n\nInput\n199100\n\nOutput\n1+99=100\n\nInput\n123123123456456456579579579\n\nOutput\n123123123+456456456=579579579"} +{"id": "01069", "text": "Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:(1^{n} + 2^{n} + 3^{n} + 4^{n})\u00a0mod\u00a05\n\nfor given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language).\n\n\n-----Input-----\n\nThe single line contains a single integer n (0 \u2264 n \u2264 10^10^5). The number doesn't contain any leading zeroes.\n\n\n-----Output-----\n\nPrint the value of the expression without leading zeros.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n4\n\nInput\n124356983594583453458888889\n\nOutput\n0\n\n\n\n-----Note-----\n\nOperation x\u00a0mod\u00a0y means taking remainder after division x by y.\n\nNote to the first sample:\n\n[Image]"} +{"id": "01070", "text": "There are $n$ houses along the road where Anya lives, each one is painted in one of $k$ possible colors.\n\nAnya likes walking along this road, but she doesn't like when two adjacent houses at the road have the same color. She wants to select a long segment of the road such that no two adjacent houses have the same color.\n\nHelp Anya find the longest segment with this property.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$\u00a0\u2014 the number of houses and the number of colors ($1 \\le n \\le 100\\,000$, $1 \\le k \\le 100\\,000$).\n\nThe next line contains $n$ integers $a_1, a_2, \\ldots, a_n$\u00a0\u2014 the colors of the houses along the road ($1 \\le a_i \\le k$).\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the maximum number of houses on the road segment having no two adjacent houses of the same color.\n\n\n-----Example-----\nInput\n8 3\n1 2 3 3 2 1 2 2\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the example, the longest segment without neighboring houses of the same color is from the house 4 to the house 7. The colors of the houses are $[3, 2, 1, 2]$ and its length is 4 houses."} +{"id": "01071", "text": "Bizon the Champion is called the Champion for a reason. \n\nBizon the Champion has recently got a present \u2014 a new glass cupboard with n shelves and he decided to put all his presents there. All the presents can be divided into two types: medals and cups. Bizon the Champion has a_1 first prize cups, a_2 second prize cups and a_3 third prize cups. Besides, he has b_1 first prize medals, b_2 second prize medals and b_3 third prize medals. \n\nNaturally, the rewards in the cupboard must look good, that's why Bizon the Champion decided to follow the rules: any shelf cannot contain both cups and medals at the same time; no shelf can contain more than five cups; no shelf can have more than ten medals. \n\nHelp Bizon the Champion find out if we can put all the rewards so that all the conditions are fulfilled.\n\n\n-----Input-----\n\nThe first line contains integers a_1, a_2 and a_3 (0 \u2264 a_1, a_2, a_3 \u2264 100). The second line contains integers b_1, b_2 and b_3 (0 \u2264 b_1, b_2, b_3 \u2264 100). The third line contains integer n (1 \u2264 n \u2264 100).\n\nThe numbers in the lines are separated by single spaces.\n\n\n-----Output-----\n\nPrint \"YES\" (without the quotes) if all the rewards can be put on the shelves in the described manner. Otherwise, print \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n1 1 1\n1 1 1\n4\n\nOutput\nYES\n\nInput\n1 1 3\n2 3 4\n2\n\nOutput\nYES\n\nInput\n1 0 0\n1 0 0\n1\n\nOutput\nNO"} +{"id": "01072", "text": "You are given an n \u00d7 m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table\n\nabcd\n\nedfg\n\nhijk\n\n\n\n\u00a0\n\nwe obtain the table:\n\nacd\n\nefg\n\nhjk\n\n\n\n\u00a0\n\nA table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.\n\n\n-----Input-----\n\nThe first line contains two integers \u00a0\u2014 n and m (1 \u2264 n, m \u2264 100).\n\nNext n lines contain m small English letters each\u00a0\u2014 the characters of the table.\n\n\n-----Output-----\n\nPrint a single number\u00a0\u2014 the minimum number of columns that you need to remove in order to make the table good.\n\n\n-----Examples-----\nInput\n1 10\ncodeforces\n\nOutput\n0\n\nInput\n4 4\ncase\ncare\ntest\ncode\n\nOutput\n2\n\nInput\n5 4\ncode\nforc\nesco\ndefo\nrces\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample the table is already good.\n\nIn the second sample you may remove the first and third column.\n\nIn the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).\n\nLet strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t."} +{"id": "01073", "text": "Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of n commands, each either 'U', 'R', 'D', or 'L'\u00a0\u2014 instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices.\n\n\n-----Input-----\n\nThe first line of the input contains a single positive integer, n (1 \u2264 n \u2264 200)\u00a0\u2014 the number of commands.\n\nThe next line contains n characters, each either 'U', 'R', 'D', or 'L'\u00a0\u2014 Calvin's source code.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of contiguous substrings that Calvin can execute and return to his starting square.\n\n\n-----Examples-----\nInput\n6\nURLLDR\n\nOutput\n2\n\nInput\n4\nDLUU\n\nOutput\n0\n\nInput\n7\nRLRLRLR\n\nOutput\n12\n\n\n\n-----Note-----\n\nIn the first case, the entire source code works, as well as the \"RL\" substring in the second and third characters.\n\nNote that, in the third case, the substring \"LR\" appears three times, and is therefore counted three times to the total result."} +{"id": "01074", "text": "++++++++[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++>+\n++++++++++>++++++++++++>+++++++++++++>++++++++++++++>+++++++++++++++>+++++++\n+++++++++<<<<<<<<<<<<<<<<-]>>>>>>>>>>.<<<<<<<<<<>>>>>>>>>>>>>>++.--<<<<<<<<<\n<<<<<>>>>>>>>>>>>>+.-<<<<<<<<<<<<<>>>>>>>>>>>>>>--.++<<<<<<<<<<<<<<>>>>>>>>>\n>>>>>>----.++++<<<<<<<<<<<<<<<>>>>.<<<<>>>>>>>>>>>>>>--.++<<<<<<<<<<<<<<>>>>\n>>>>>>>>>>>---.+++<<<<<<<<<<<<<<<>>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<<>>>>>>>>\n>>>>++.--<<<<<<<<<<<<>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<>>>>>>>>>>>>>>++.--<<<\n<<<<<<<<<<<.\n\nDCBA:^!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\\[ZYXWVUTSRQPONMLKJIHdcbD`Y^]\\UZYRv\n9876543210/.-,+*)('&%$#\"!~}|{zyxwvutsrqponm+*)('&%$#cya`=^]\\[ZYXWVUTSRQPONML\nKJfe^cba`_X]VzTYRv98TSRQ3ONMLEi,+*)('&%$#\"!~}|{zyxwvutsrqponmlkjihgfedcba`_^\n]\\[ZYXWVUTSonPlkjchg`ed]#DCBA@?>=<;:9876543OHGLKDIHGFE>b%$#\"!~}|{zyxwvutsrqp\nonmlkjihgfedcba`_^]\\[ZYXWVUTSRQPONMibafedcba`_X|?>Z62*9*2+,34*9*3+,66+9*8+,52*9*7+,75+9*8+,92+9*6+,48+9*3+,43*9*2+,84*,26*9*3^\n\n\n\n\n-----Input-----\n\nThe input contains a single integer a (0 \u2264 a \u2264 1 000 000).\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Example-----\nInput\n129\n\nOutput\n1"} +{"id": "01075", "text": "Piegirl found the red button. You have one last chance to change the inevitable end.\n\nThe circuit under the button consists of n nodes, numbered from 0 to n - 1. In order to deactivate the button, the n nodes must be disarmed in a particular order. Node 0 must be disarmed first. After disarming node i, the next node to be disarmed must be either node (2\u00b7i) modulo n or node (2\u00b7i) + 1 modulo n. The last node to be disarmed must be node 0. Node 0 must be disarmed twice, but all other nodes must be disarmed exactly once. \n\nYour task is to find any such order and print it. If there is no such order, print -1.\n\n\n-----Input-----\n\nInput consists of a single integer n (2 \u2264 n \u2264 10^5).\n\n\n-----Output-----\n\nPrint an order in which you can to disarm all nodes. If it is impossible, print -1 instead. If there are multiple orders, print any one of them.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n0 1 0\n\nInput\n3\n\nOutput\n-1\nInput\n4\n\nOutput\n0 1 3 2 0\n\nInput\n16\n\nOutput\n0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0"} +{"id": "01076", "text": "zscoder has a deck of $n+m$ custom-made cards, which consists of $n$ cards labelled from $1$ to $n$ and $m$ jokers. Since zscoder is lonely, he wants to play a game with himself using those cards. \n\nInitially, the deck is shuffled uniformly randomly and placed on the table. zscoder has a set $S$ which is initially empty. \n\nEvery second, zscoder draws the top card from the deck. If the card has a number $x$ written on it, zscoder removes the card and adds $x$ to the set $S$. If the card drawn is a joker, zscoder places all the cards back into the deck and reshuffles (uniformly randomly) the $n+m$ cards to form a new deck (hence the new deck now contains all cards from $1$ to $n$ and the $m$ jokers). Then, if $S$ currently contains all the elements from $1$ to $n$, the game ends. Shuffling the deck doesn't take time at all. \n\nWhat is the expected number of seconds before the game ends? We can show that the answer can be written in the form $\\frac{P}{Q}$ where $P, Q$ are relatively prime integers and $Q \\neq 0 \\bmod 998244353$. Output the value of $(P \\cdot Q^{-1})$ modulo $998244353$.\n\n\n-----Input-----\n\nThe only line of the input contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^{6}$).\n\n\n-----Output-----\n\nOutput a single integer, the value of $(P \\cdot Q^{-1})$ modulo $998244353$.\n\n\n-----Examples-----\nInput\n2 1\n\nOutput\n5\n\nInput\n3 2\n\nOutput\n332748127\n\nInput\n14 9\n\nOutput\n969862773\n\n\n\n-----Note-----\n\nFor the first sample, it can be proven that the expected time before the game ends is $5$ seconds.\n\nFor the second sample, it can be proven that the expected time before the game ends is $\\frac{28}{3}$ seconds."} +{"id": "01077", "text": "Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a_1, a_2, ..., a_{n}, where a_{i} is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. \n\nWe define as b_{j} the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b_1, b_2, ..., b_{m} will be as large as possible.\n\nFind this maximum possible value of the minimum among the b_{j} (1 \u2264 j \u2264 m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 m \u2264 n \u2264 2000).\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9), where a_{i} is the performer of the i-th song.\n\n\n-----Output-----\n\nIn the first line print two integers: the maximum possible value of the minimum among the b_{j} (1 \u2264 j \u2264 m), where b_{j} is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.\n\nIn the second line print the changed playlist.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n4 2\n1 2 3 2\n\nOutput\n2 1\n1 2 1 2 \n\n\n\n\nInput\n7 3\n1 3 2 2 2 2 1\n\nOutput\n2 1\n1 3 3 2 2 2 1 \n\n\n\n\nInput\n4 4\n1000000000 100 7 1000000000\n\nOutput\n1 4\n1 2 3 4 \n\n\n\n\n\n\n-----Note-----\n\nIn the first sample, after Polycarp's changes the first band performs two songs (b_1 = 2), and the second band also performs two songs (b_2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. \n\nIn the second sample, after Polycarp's changes the first band performs two songs (b_1 = 2), the second band performs three songs (b_2 = 3), and the third band also performs two songs (b_3 = 2). Thus, the best minimum value is 2."} +{"id": "01078", "text": "Another Codeforces Round has just finished! It has gathered $n$ participants, and according to the results, the expected rating change of participant $i$ is $a_i$. These rating changes are perfectly balanced\u00a0\u2014 their sum is equal to $0$.\n\nUnfortunately, due to minor technical glitches, the round is declared semi-rated. It means that all rating changes must be divided by two.\n\nThere are two conditions though: For each participant $i$, their modified rating change $b_i$ must be integer, and as close to $\\frac{a_i}{2}$ as possible. It means that either $b_i = \\lfloor \\frac{a_i}{2} \\rfloor$ or $b_i = \\lceil \\frac{a_i}{2} \\rceil$. In particular, if $a_i$ is even, $b_i = \\frac{a_i}{2}$. Here $\\lfloor x \\rfloor$ denotes rounding down to the largest integer not greater than $x$, and $\\lceil x \\rceil$ denotes rounding up to the smallest integer not smaller than $x$. The modified rating changes must be perfectly balanced\u00a0\u2014 their sum must be equal to $0$. \n\nCan you help with that?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 13\\,845$), denoting the number of participants.\n\nEach of the next $n$ lines contains a single integer $a_i$ ($-336 \\le a_i \\le 1164$), denoting the rating change of the $i$-th participant.\n\nThe sum of all $a_i$ is equal to $0$.\n\n\n-----Output-----\n\nOutput $n$ integers $b_i$, each denoting the modified rating change of the $i$-th participant in order of input.\n\nFor any $i$, it must be true that either $b_i = \\lfloor \\frac{a_i}{2} \\rfloor$ or $b_i = \\lceil \\frac{a_i}{2} \\rceil$. The sum of all $b_i$ must be equal to $0$.\n\nIf there are multiple solutions, print any. We can show that a solution exists for any valid input.\n\n\n-----Examples-----\nInput\n3\n10\n-5\n-5\n\nOutput\n5\n-2\n-3\n\nInput\n7\n-7\n-29\n0\n3\n24\n-29\n38\n\nOutput\n-3\n-15\n0\n2\n12\n-15\n19\n\n\n\n-----Note-----\n\nIn the first example, $b_1 = 5$, $b_2 = -3$ and $b_3 = -2$ is another correct solution.\n\nIn the second example there are $6$ possible solutions, one of them is shown in the example output."} +{"id": "01079", "text": "Valera considers a number beautiful, if it equals 2^{k} or -2^{k} for some integer k (k \u2265 0). Recently, the math teacher asked Valera to represent number n as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible. \n\nHelp Valera and find, how many numbers he is going to need. In other words, if you look at all decompositions of the number n into beautiful summands, you need to find the size of the decomposition which has the fewest summands.\n\n\n-----Input-----\n\nThe first line contains string s (1 \u2264 |s| \u2264 10^6), that is the binary representation of number n without leading zeroes (n > 0).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum amount of beautiful numbers that give a total of n.\n\n\n-----Examples-----\nInput\n10\n\nOutput\n1\n\nInput\n111\n\nOutput\n2\n\nInput\n1101101\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample n = 2 is a beautiful number.\n\nIn the second sample n = 7 and Valera can decompose it into sum 2^3 + ( - 2^0).\n\nIn the third sample n = 109 can be decomposed into the sum of four summands as follows: 2^7 + ( - 2^4) + ( - 2^2) + 2^0."} +{"id": "01080", "text": "You are given an array $a_1, a_2, \\ldots, a_n$.\n\nIn one operation you can choose two elements $a_i$ and $a_j$ ($i \\ne j$) and decrease each of them by one.\n\nYou need to check whether it is possible to make all the elements equal to zero or not.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 10^5$)\u00a0\u2014 the size of the array.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint \"YES\" if it is possible to make all elements zero, otherwise print \"NO\".\n\n\n-----Examples-----\nInput\n4\n1 1 2 2\n\nOutput\nYES\nInput\n6\n1 2 3 4 5 6\n\nOutput\nNO\n\n\n-----Note-----\n\nIn the first example, you can make all elements equal to zero in $3$ operations: Decrease $a_1$ and $a_2$, Decrease $a_3$ and $a_4$, Decrease $a_3$ and $a_4$ \n\nIn the second example, one can show that it is impossible to make all elements equal to zero."} +{"id": "01081", "text": "-----Input-----\n\nThe input contains a single integer $a$ ($1 \\le a \\le 99$).\n\n\n-----Output-----\n\nOutput \"YES\" or \"NO\".\n\n\n-----Examples-----\nInput\n5\n\nOutput\nYES\n\nInput\n13\n\nOutput\nNO\n\nInput\n24\n\nOutput\nNO\n\nInput\n46\n\nOutput\nYES"} +{"id": "01082", "text": "Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.\n\nTwo ways are considered different if sets of indexes of elements chosen by these ways are different.\n\nSince the answer can be very large, you should find the answer modulo 10^9 + 7.\n\n\n-----Input-----\n\nFirst line contains one integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of elements in the array.\n\nSecond line contains n integers a_{i} (1 \u2264 a_{i} \u2264 70)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of different ways to choose some elements so that their product is a square of a certain integer modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n4\n1 1 1 1\n\nOutput\n15\n\nInput\n4\n2 2 2 2\n\nOutput\n7\n\nInput\n5\n1 2 4 5 8\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn first sample product of elements chosen by any way is 1 and 1 = 1^2. So the answer is 2^4 - 1 = 15.\n\nIn second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7."} +{"id": "01083", "text": "Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible. \n\nHelp Petya to split the integers. Each of n integers should be exactly in one group.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 60 000) \u2014 the number of integers Petya has.\n\n\n-----Output-----\n\nPrint the smallest possible absolute difference in the first line.\n\nIn the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n0\n2 1 4 \n\nInput\n2\n\nOutput\n1\n1 1 \n\n\n\n-----Note-----\n\nIn the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0.\n\nIn the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1."} +{"id": "01084", "text": "There is a rectangular grid of n rows of m initially-white cells each.\n\nArkady performed a certain number (possibly zero) of operations on it. In the i-th operation, a non-empty subset of rows R_{i} and a non-empty subset of columns C_{i} are chosen. For each row r in R_{i} and each column c in C_{i}, the intersection of row r and column c is coloured black.\n\nThere's another constraint: a row or a column can only be chosen at most once among all operations. In other words, it means that no pair of (i, j) (i < j) exists such that $R_{i} \\cap R_{j} \\neq \\varnothing$ or $C_{i} \\cap C_{j} \\neq \\varnothing$, where [Image] denotes intersection of sets, and $\\varnothing$ denotes the empty set.\n\nYou are to determine whether a valid sequence of operations exists that produces a given final grid.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m (1 \u2264 n, m \u2264 50)\u00a0\u2014 the number of rows and columns of the grid, respectively.\n\nEach of the following n lines contains a string of m characters, each being either '.' (denoting a white cell) or '#' (denoting a black cell), representing the desired setup.\n\n\n-----Output-----\n\nIf the given grid can be achieved by any valid sequence of operations, output \"Yes\"; otherwise output \"No\" (both without quotes).\n\nYou can print each character in any case (upper or lower).\n\n\n-----Examples-----\nInput\n5 8\n.#.#..#.\n.....#..\n.#.#..#.\n#.#....#\n.....#..\n\nOutput\nYes\n\nInput\n5 5\n..#..\n..#..\n#####\n..#..\n..#..\n\nOutput\nNo\n\nInput\n5 9\n........#\n#........\n..##.#...\n.......#.\n....#.#.#\n\nOutput\nNo\n\n\n\n-----Note-----\n\nFor the first example, the desired setup can be produced by 3 operations, as is shown below.\n\n [Image] \n\nFor the second example, the desired setup cannot be produced, since in order to colour the center row, the third row and all columns must be selected in one operation, but after that no column can be selected again, hence it won't be possible to colour the other cells in the center column."} +{"id": "01085", "text": "Given is a positive integer N.\nWe will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.\n - Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K.\nIn how many choices of K will N become 1 in the end?\n\n-----Constraints-----\n - 2 \\leq N \\leq 10^{12}\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the number of choices of K in which N becomes 1 in the end.\n\n-----Sample Input-----\n6\n\n-----Sample Output-----\n3\n\nThere are three choices of K in which N becomes 1 in the end: 2, 5, and 6.\nIn each of these choices, N will change as follows:\n - When K=2: 6 \\to 3 \\to 1\n - When K=5: 6 \\to 1\n - When K=6: 6 \\to 1"} +{"id": "01086", "text": "We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.\nThe square (i, j) has two numbers A_{ij} and B_{ij} written on it.\nFirst, for each square, Takahashi paints one of the written numbers red and the other blue.\nThen, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.\nLet the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).\nTakahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.\nFind the minimum unbalancedness possible.\n\n-----Constraints-----\n - 2 \\leq H \\leq 80\n - 2 \\leq W \\leq 80\n - 0 \\leq A_{ij} \\leq 80\n - 0 \\leq B_{ij} \\leq 80\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\nA_{11} A_{12} \\ldots A_{1W}\n:\nA_{H1} A_{H2} \\ldots A_{HW}\nB_{11} B_{12} \\ldots B_{1W}\n:\nB_{H1} B_{H2} \\ldots B_{HW}\n\n-----Output-----\nPrint the minimum unbalancedness possible.\n\n-----Sample Input-----\n2 2\n1 2\n3 4\n3 4\n2 1\n\n-----Sample Output-----\n0\n\nBy painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0."} +{"id": "01087", "text": "You are given N non-negative integers A_1, A_2, ..., A_N and another non-negative integer K.\nFor a integer X between 0 and K (inclusive), let f(X) = (X XOR A_1) + (X XOR A_2) + ... + (X XOR A_N).\nHere, for non-negative integers a and b, a XOR b denotes the bitwise exclusive OR of a and b.\nFind the maximum value of f.\nWhat is XOR?\nThe bitwise exclusive OR of a and b, X, is defined as follows:\n - When X is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if, when written in base two, exactly one of A and B has 1 in the 2^k's place, and 0 otherwise.\nFor example, 3 XOR 5 = 6. (When written in base two: 011 XOR 101 = 110.)\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 0 \\leq K \\leq 10^{12}\n - 0 \\leq A_i \\leq 10^{12}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the maximum value of f.\n\n-----Sample Input-----\n3 7\n1 6 3\n\n-----Sample Output-----\n14\n\nThe maximum value is: f(4) = (4 XOR 1) + (4 XOR 6) + (4 XOR 3) = 5 + 2 + 7 = 14."} +{"id": "01088", "text": "Given are an N \\times N matrix and an integer K. The entry in the i-th row and j-th column of this matrix is denoted as a_{i, j}. This matrix contains each of 1, 2, \\dots, N^2 exactly once.\nSigma can repeat the following two kinds of operation arbitrarily many times in any order.\n - Pick two integers x, y (1 \\leq x < y \\leq N) that satisfy a_{i, x} + a_{i, y} \\leq K for all i (1 \\leq i \\leq N) and swap the x-th and the y-th columns.\n - Pick two integers x, y (1 \\leq x < y \\leq N) that satisfy a_{x, i} + a_{y, i} \\leq K for all i (1 \\leq i \\leq N) and swap the x-th and the y-th rows.\nHow many matrices can he obtain by these operations? Find it modulo 998244353.\n\n-----Constraints-----\n - 1 \\leq N \\leq 50\n - 1 \\leq K \\leq 2 \\times N^2\n - a_{i, j}'s are a rearrangement of 1, 2, \\dots, N^2.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\na_{1, 1} a_{1, 2} ... a_{1, N}\na_{2, 1} a_{2, 2} ... a_{2, N}\n:\na_{N, 1} a_{N, 2} ... a_{N, N}\n\n-----Output-----\nPrint the number of matrices Sigma can obtain modulo 998244353.\n\n-----Sample Input-----\n3 13\n3 2 7\n4 8 9\n1 6 5\n\n-----Sample Output-----\n12\n\nFor example, Sigma can swap two columns, by setting x = 1, y = 2. After that, the resulting matrix will be:\n2 3 7\n8 4 9\n6 1 5\n\nAfter that, he can swap two row vectors by setting x = 1, y = 3, resulting in the following matrix:\n6 1 5\n8 4 9\n2 3 7\n"} +{"id": "01089", "text": "We have a grid of squares with N rows and M columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left. We will choose K of the squares and put a piece on each of them.\nIf we place the K pieces on squares (x_1, y_1), (x_2, y_2), ..., and (x_K, y_K), the cost of this arrangement is computed as:\n\\sum_{i=1}^{K-1} \\sum_{j=i+1}^K (|x_i - x_j| + |y_i - y_j|)\nFind the sum of the costs of all possible arrangements of the pieces. Since this value can be tremendous, print it modulo 10^9+7.\nWe consider two arrangements of the pieces different if and only if there is a square that contains a piece in one of the arrangements but not in the other.\n\n-----Constraints-----\n - 2 \\leq N \\times M \\leq 2 \\times 10^5\n - 2 \\leq K \\leq N \\times M\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M K\n\n-----Output-----\nPrint the sum of the costs of all possible arrangements of the pieces, modulo 10^9+7.\n\n-----Sample Input-----\n2 2 2\n\n-----Sample Output-----\n8\n\nThere are six possible arrangements of the pieces, as follows:\n - ((1,1),(1,2)), with the cost |1-1|+|1-2| = 1\n - ((1,1),(2,1)), with the cost |1-2|+|1-1| = 1\n - ((1,1),(2,2)), with the cost |1-2|+|1-2| = 2\n - ((1,2),(2,1)), with the cost |1-2|+|2-1| = 2\n - ((1,2),(2,2)), with the cost |1-2|+|2-2| = 1\n - ((2,1),(2,2)), with the cost |2-2|+|1-2| = 1\nThe sum of these costs is 8."} +{"id": "01090", "text": "There are N people standing in a queue from west to east.\nGiven is a string S of length N representing the directions of the people.\nThe i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.\nA person is happy if the person in front of him/her is facing the same direction.\nIf no person is standing in front of a person, however, he/she is not happy.\nYou can perform the following operation any number of times between 0 and K (inclusive):\nOperation: Choose integers l and r such that 1 \\leq l \\leq r \\leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.\nWhat is the maximum possible number of happy people you can have?\n\n-----Constraints-----\n - N is an integer satisfying 1 \\leq N \\leq 10^5.\n - K is an integer satisfying 1 \\leq K \\leq 10^5.\n - |S| = N\n - Each character of S is L or R.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nS\n\n-----Output-----\nPrint the maximum possible number of happy people after at most K operations.\n\n-----Sample Input-----\n6 1\nLRLRRL\n\n-----Sample Output-----\n3\n\nIf we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy."} +{"id": "01091", "text": "In this problem we consider a special type of an auction, which is called the second-price auction. As in regular auction n bidders place a bid which is price a bidder ready to pay. The auction is closed, that is, each bidder secretly informs the organizer of the auction price he is willing to pay. After that, the auction winner is the participant who offered the highest price. However, he pay not the price he offers, but the highest price among the offers of other participants (hence the name: the second-price auction).\n\nWrite a program that reads prices offered by bidders and finds the winner and the price he will pay. Consider that all of the offered prices are different.\n\n\n-----Input-----\n\nThe first line of the input contains n (2 \u2264 n \u2264 1000) \u2014 number of bidders. The second line contains n distinct integer numbers p_1, p_2, ... p_{n}, separated by single spaces (1 \u2264 p_{i} \u2264 10000), where p_{i} stands for the price offered by the i-th bidder.\n\n\n-----Output-----\n\nThe single output line should contain two integers: index of the winner and the price he will pay. Indices are 1-based.\n\n\n-----Examples-----\nInput\n2\n5 7\n\nOutput\n2 5\n\nInput\n3\n10 2 8\n\nOutput\n1 8\n\nInput\n6\n3 8 2 9 4 14\n\nOutput\n6 9"} +{"id": "01092", "text": "There are n lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. \n\nHe knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007\u00a0(10^9 + 7).\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 \u2264 n \u2264 1000, 1 \u2264 m \u2264 n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initially switched on.\n\n\n-----Output-----\n\nIn the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007\u00a0(10^9 + 7).\n\n\n-----Examples-----\nInput\n3 1\n1\n\nOutput\n1\n\nInput\n4 2\n1 4\n\nOutput\n2\n\nInput\n11 2\n4 8\n\nOutput\n6720"} +{"id": "01093", "text": "\u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0433\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0440\u0435\u0431\u0442\u0430 \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0434\u0430\u043d \u0432 \u0432\u0438\u0434\u0435 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438\u0437 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u00ab.\u00bb (\u043f\u0443\u0441\u0442\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e) \u0438 \u00ab*\u00bb (\u0447\u0430\u0441\u0442\u044c \u0433\u043e\u0440\u044b). \u041a\u0430\u0436\u0434\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u0443 \u00ab\u0437\u0432\u0451\u0437\u0434\u043e\u0447\u043a\u0443\u00bb. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u00ab*\u00bb \u043b\u0438\u0431\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0438\u0436\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b, \u043b\u0438\u0431\u043e \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0434 \u043d\u0438\u043c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u00ab*\u00bb. ...........\n\n.........*.\n\n.*.......*.\n\n**.......*.\n\n**..*...**.\n\n*********** \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0433\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0440\u0435\u0431\u0442\u0430. \n\n\u041c\u0430\u0440\u0448\u0440\u0443\u0442 \u0442\u0443\u0440\u0438\u0441\u0442\u0430 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u0441\u044c \u0433\u043e\u0440\u043d\u044b\u0439 \u0445\u0440\u0435\u0431\u0435\u0442 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e. \u041a\u0430\u0436\u0434\u044b\u0439 \u0434\u0435\u043d\u044c \u0442\u0443\u0440\u0438\u0441\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0432\u043f\u0440\u0430\u0432\u043e\u00a0\u2014 \u0432 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043e\u043d \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f (\u0438\u043b\u0438 \u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f) \u0432 \u0441\u0430\u043c\u0443\u044e \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0442\u043e\u0447\u043a\u0443 \u0433\u043e\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435.\n\n\u0421\u0447\u0438\u0442\u0430\u044f, \u0447\u0442\u043e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0442\u0443\u0440\u0438\u0441\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u0430\u043c\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u0442\u043e\u0447\u043a\u0435 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0430 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442 \u0441\u0432\u043e\u0439 \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \u0432 \u0441\u0430\u043c\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u0442\u043e\u0447\u043a\u0435 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u043d\u0430\u0439\u0434\u0438\u0442\u0435 \u0434\u0432\u0435 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b: \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043f\u043e\u0434\u044a\u0451\u043c \u0437\u0430 \u0434\u0435\u043d\u044c (\u0440\u0430\u0432\u0435\u043d 0, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0440\u043e\u0444\u0438\u043b\u0435 \u0433\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0440\u0435\u0431\u0442\u0430 \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u044a\u0451\u043c\u0430), \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u0441\u043f\u0443\u0441\u043a \u0437\u0430 \u0434\u0435\u043d\u044c (\u0440\u0430\u0432\u0435\u043d 0, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0440\u043e\u0444\u0438\u043b\u0435 \u0433\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0440\u0435\u0431\u0442\u0430 \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430). \n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 n \u0438 m (1 \u2264 n, m \u2264 100)\u00a0\u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e.\n\n\u0414\u0430\u043b\u0435\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 n \u0441\u0442\u0440\u043e\u043a \u043f\u043e m \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439\u00a0\u2014 \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0433\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0440\u0435\u0431\u0442\u0430. \u041a\u0430\u0436\u0434\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u00a0\u2014 \u044d\u0442\u043e \u043b\u0438\u0431\u043e \u00ab.\u00bb, \u043b\u0438\u0431\u043e \u00ab*\u00bb. \u041a\u0430\u0436\u0434\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u0438\u043d \u0441\u0438\u043c\u0432\u043e\u043b \u00ab*\u00bb. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u00ab*\u00bb \u043b\u0438\u0431\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0438\u0436\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b, \u043b\u0438\u0431\u043e \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0434 \u043d\u0438\u043c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u00ab*\u00bb.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u043e\u0431\u0435\u043b \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430: \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u043f\u043e\u0434\u044a\u0451\u043c\u0430 \u0437\u0430 \u0434\u0435\u043d\u044c (\u0438\u043b\u0438 0, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0440\u043e\u0444\u0438\u043b\u0435 \u0433\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0440\u0435\u0431\u0442\u0430 \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u044a\u0451\u043c\u0430), \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430 \u0437\u0430 \u0434\u0435\u043d\u044c (\u0438\u043b\u0438 0, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0440\u043e\u0444\u0438\u043b\u0435 \u0433\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0440\u0435\u0431\u0442\u0430 \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430). \n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n6 11\n...........\n.........*.\n.*.......*.\n**.......*.\n**..*...**.\n***********\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3 4\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n5 5\n....*\n...**\n..***\n.****\n*****\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1 0\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n8 7\n.......\n.*.....\n.*.....\n.**....\n.**.*..\n.****.*\n.******\n*******\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n6 2\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0441\u043e\u0442\u044b \u0433\u043e\u0440 \u0440\u0430\u0432\u043d\u044b: 3, 4, 1, 1, 2, 1, 1, 1, 2, 5, 1. \u041d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043f\u043e\u0434\u044a\u0435\u043c \u0440\u0430\u0432\u0435\u043d 3 \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 9 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 2) \u0438 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 10 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 5). \u041d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u0441\u043f\u0443\u0441\u043a \u0440\u0430\u0432\u0435\u043d 4 \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 10 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 5) \u0438 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 11 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 1).\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0441\u043e\u0442\u044b \u0433\u043e\u0440 \u0440\u0430\u0432\u043d\u044b: 1, 2, 3, 4, 5. \u041d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043f\u043e\u0434\u044a\u0451\u043c \u0440\u0430\u0432\u0435\u043d 1 \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0435\u0436\u0434\u0443 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 2 (\u0435\u0435 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 2) \u0438 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 3 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 3). \u0422\u0430\u043a \u043a\u0430\u043a \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0433\u043e\u0440\u043d\u043e\u043c \u0445\u0440\u0435\u0431\u0442\u0435 \u043d\u0435\u0442 \u0441\u043f\u0443\u0441\u043a\u043e\u0432, \u0442\u043e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0432\u043d\u0430 0.\n\n\u0412 \u0442\u0440\u0435\u0442\u044c\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0441\u043e\u0442\u044b \u0433\u043e\u0440 \u0440\u0430\u0432\u043d\u044b: 1, 7, 5, 3, 4, 2, 3. \u041d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043f\u043e\u0434\u044a\u0451\u043c \u0440\u0430\u0432\u0435\u043d 6 \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 1 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 1) \u0438 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 2 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 7). \u041d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u0441\u043f\u0443\u0441\u043a \u0440\u0430\u0432\u0435\u043d 2 \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 2 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 7) \u0438 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 3 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 5). \u0422\u0430\u043a\u043e\u0439 \u0436\u0435 \u0441\u043f\u0443\u0441\u043a \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 5 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 4) \u0438 \u0433\u043e\u0440\u043e\u0439 \u043d\u043e\u043c\u0435\u0440 6 (\u0435\u0451 \u0432\u044b\u0441\u043e\u0442\u0430 \u0440\u0430\u0432\u043d\u0430 2)."} +{"id": "01094", "text": "Polycarp is a big lover of killing time in social networks. A page with a chatlist in his favourite network is made so that when a message is sent to some friend, his friend's chat rises to the very top of the page. The relative order of the other chats doesn't change. If there was no chat with this friend before, then a new chat is simply inserted to the top of the list.\n\nAssuming that the chat list is initially empty, given the sequence of Polycaprus' messages make a list of chats after all of his messages are processed. Assume that no friend wrote any message to Polycarpus.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the number of Polycarpus' messages. Next n lines enlist the message recipients in the order in which the messages were sent. The name of each participant is a non-empty sequence of lowercase English letters of length at most 10.\n\n\n-----Output-----\n\nPrint all the recipients to who Polycarp talked to in the order of chats with them, from top to bottom.\n\n\n-----Examples-----\nInput\n4\nalex\nivan\nroman\nivan\n\nOutput\nivan\nroman\nalex\n\nInput\n8\nalina\nmaria\nekaterina\ndarya\ndarya\nekaterina\nmaria\nalina\n\nOutput\nalina\nmaria\nekaterina\ndarya\n\n\n\n-----Note-----\n\nIn the first test case Polycarpus first writes to friend by name \"alex\", and the list looks as follows: alex \n\nThen Polycarpus writes to friend by name \"ivan\" and the list looks as follows: ivan alex \n\nPolycarpus writes the third message to friend by name \"roman\" and the list looks as follows: roman ivan alex \n\nPolycarpus writes the fourth message to friend by name \"ivan\", to who he has already sent a message, so the list of chats changes as follows: ivan roman alex"} +{"id": "01095", "text": "You are given a tube which is reflective inside represented as two non-coinciding, but parallel to $Ox$ lines. Each line has some special integer points\u00a0\u2014 positions of sensors on sides of the tube.\n\nYou are going to emit a laser ray in the tube. To do so, you have to choose two integer points $A$ and $B$ on the first and the second line respectively (coordinates can be negative): the point $A$ is responsible for the position of the laser, and the point $B$\u00a0\u2014 for the direction of the laser ray. The laser ray is a ray starting at $A$ and directed at $B$ which will reflect from the sides of the tube (it doesn't matter if there are any sensors at a reflection point or not). A sensor will only register the ray if the ray hits exactly at the position of the sensor. [Image] Examples of laser rays. Note that image contains two examples. The $3$ sensors (denoted by black bold points on the tube sides) will register the blue ray but only $2$ will register the red. \n\nCalculate the maximum number of sensors which can register your ray if you choose points $A$ and $B$ on the first and the second lines respectively.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $y_1$ ($1 \\le n \\le 10^5$, $0 \\le y_1 \\le 10^9$)\u00a0\u2014 number of sensors on the first line and its $y$ coordinate.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 10^9$)\u00a0\u2014 $x$ coordinates of the sensors on the first line in the ascending order.\n\nThe third line contains two integers $m$ and $y_2$ ($1 \\le m \\le 10^5$, $y_1 < y_2 \\le 10^9$)\u00a0\u2014 number of sensors on the second line and its $y$ coordinate. \n\nThe fourth line contains $m$ integers $b_1, b_2, \\ldots, b_m$ ($0 \\le b_i \\le 10^9$)\u00a0\u2014 $x$ coordinates of the sensors on the second line in the ascending order.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the maximum number of sensors which can register the ray.\n\n\n-----Example-----\nInput\n3 1\n1 5 6\n1 3\n3\n\nOutput\n3\n\n\n\n-----Note-----\n\nOne of the solutions illustrated on the image by pair $A_2$ and $B_2$."} +{"id": "01096", "text": "The only king stands on the standard chess board. You are given his position in format \"cd\", where c is the column from 'a' to 'h' and d is the row from '1' to '8'. Find the number of moves permitted for the king.\n\nCheck the king's moves here https://en.wikipedia.org/wiki/King_(chess). [Image] King moves from the position e4 \n\n\n-----Input-----\n\nThe only line contains the king's position in the format \"cd\", where 'c' is the column from 'a' to 'h' and 'd' is the row from '1' to '8'.\n\n\n-----Output-----\n\nPrint the only integer x \u2014 the number of moves permitted for the king.\n\n\n-----Example-----\nInput\ne4\n\nOutput\n8"} +{"id": "01097", "text": "There are n cities in Berland, each of them has a unique id\u00a0\u2014 an integer from 1 to n, the capital is the one with id 1. Now there is a serious problem in Berland with roads\u00a0\u2014 there are no roads.\n\nThat is why there was a decision to build n - 1 roads so that there will be exactly one simple path between each pair of cities.\n\nIn the construction plan t integers a_1, a_2, ..., a_{t} were stated, where t equals to the distance from the capital to the most distant city, concerning new roads. a_{i} equals the number of cities which should be at the distance i from the capital. The distance between two cities is the number of roads one has to pass on the way from one city to another. \n\nAlso, it was decided that among all the cities except the capital there should be exactly k cities with exactly one road going from each of them. Such cities are dead-ends and can't be economically attractive. In calculation of these cities the capital is not taken into consideration regardless of the number of roads from it. \n\nYour task is to offer a plan of road's construction which satisfies all the described conditions or to inform that it is impossible.\n\n\n-----Input-----\n\nThe first line contains three positive numbers n, t and k (2 \u2264 n \u2264 2\u00b710^5, 1 \u2264 t, k < n)\u00a0\u2014 the distance to the most distant city from the capital and the number of cities which should be dead-ends (the capital in this number is not taken into consideration). \n\nThe second line contains a sequence of t integers a_1, a_2, ..., a_{t} (1 \u2264 a_{i} < n), the i-th number is the number of cities which should be at the distance i from the capital. It is guaranteed that the sum of all the values a_{i} equals n - 1.\n\n\n-----Output-----\n\nIf it is impossible to built roads which satisfy all conditions, print -1.\n\nOtherwise, in the first line print one integer n\u00a0\u2014 the number of cities in Berland. In the each of the next n - 1 line print two integers\u00a0\u2014 the ids of cities that are connected by a road. Each road should be printed exactly once. You can print the roads and the cities connected by a road in any order.\n\nIf there are multiple answers, print any of them. Remember that the capital has id 1.\n\n\n-----Examples-----\nInput\n7 3 3\n2 3 1\n\nOutput\n7\n1 3\n2 1\n2 6\n2 4\n7 4\n3 5\n\nInput\n14 5 6\n4 4 2 2 1\n\nOutput\n14\n3 1\n1 4\n11 6\n1 2\n10 13\n6 10\n10 12\n14 12\n8 4\n5 1\n3 7\n2 6\n5 9\n\nInput\n3 1 1\n2\n\nOutput\n-1"} +{"id": "01098", "text": "Polycarp has a strict daily schedule. He has n alarms set for each day, and the i-th alarm rings each day at the same time during exactly one minute.\n\nDetermine the longest time segment when Polycarp can sleep, i.\u00a0e. no alarm rings in that period. It is possible that Polycarp begins to sleep in one day, and wakes up in another.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100) \u2014 the number of alarms.\n\nEach of the next n lines contains a description of one alarm. Each description has a format \"hh:mm\", where hh is the hour when the alarm rings, and mm is the minute of that hour when the alarm rings. The number of hours is between 0 and 23, and the number of minutes is between 0 and 59. All alarm times are distinct. The order of the alarms is arbitrary.\n\nEach alarm starts ringing in the beginning of the corresponding minute and rings for exactly one minute (i.\u00a0e. stops ringing in the beginning of the next minute). Polycarp can start sleeping instantly when no alarm is ringing, and he wakes up at the moment when some alarm starts ringing.\n\n\n-----Output-----\n\nPrint a line in format \"hh:mm\", denoting the maximum time Polycarp can sleep continuously. hh denotes the number of hours, and mm denotes the number of minutes. The number of minutes should be between 0 and 59. Look through examples to understand the format better.\n\n\n-----Examples-----\nInput\n1\n05:43\n\nOutput\n23:59\n\nInput\n4\n22:00\n03:21\n16:03\n09:59\n\nOutput\n06:37\n\n\n\n-----Note-----\n\nIn the first example there is only one alarm which rings during one minute of a day, and then rings again on the next day, 23 hours and 59 minutes later. Polycarp can sleep all this time."} +{"id": "01099", "text": "You are given a tree with $n$ vertices. You are allowed to modify the structure of the tree through the following multi-step operation: Choose three vertices $a$, $b$, and $c$ such that $b$ is adjacent to both $a$ and $c$. For every vertex $d$ other than $b$ that is adjacent to $a$, remove the edge connecting $d$ and $a$ and add the edge connecting $d$ and $c$. Delete the edge connecting $a$ and $b$ and add the edge connecting $a$ and $c$. \n\nAs an example, consider the following tree: [Image] \n\nThe following diagram illustrates the sequence of steps that happen when we apply an operation to vertices $2$, $4$, and $5$: [Image] \n\nIt can be proven that after each operation, the resulting graph is still a tree.\n\nFind the minimum number of operations that must be performed to transform the tree into a star. A star is a tree with one vertex of degree $n - 1$, called its center, and $n - 1$ vertices of degree $1$.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($3 \\le n \\le 2 \\cdot 10^5$) \u00a0\u2014 the number of vertices in the tree.\n\nThe $i$-th of the following $n - 1$ lines contains two integers $u_i$ and $v_i$ ($1 \\le u_i, v_i \\le n$, $u_i \\neq v_i$) denoting that there exists an edge connecting vertices $u_i$ and $v_i$. It is guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nPrint a single integer \u00a0\u2014 the minimum number of operations needed to transform the tree into a star.\n\nIt can be proven that under the given constraints, it is always possible to transform the tree into a star using at most $10^{18}$ operations.\n\n\n-----Examples-----\nInput\n6\n4 5\n2 6\n3 2\n1 2\n2 4\n\nOutput\n1\n\nInput\n4\n2 4\n4 1\n3 4\n\nOutput\n0\n\n\n\n-----Note-----\n\nThe first test case corresponds to the tree shown in the statement. As we have seen before, we can transform the tree into a star with center at vertex $5$ by applying a single operation to vertices $2$, $4$, and $5$.\n\nIn the second test case, the given tree is already a star with the center at vertex $4$, so no operations have to be performed."} +{"id": "01100", "text": "Ari the monster always wakes up very early with the first ray of the sun and the first thing she does is feeding her squirrel.\n\nAri draws a regular convex polygon on the floor and numbers it's vertices 1, 2, ..., n in clockwise order. Then starting from the vertex 1 she draws a ray in the direction of each other vertex. The ray stops when it reaches a vertex or intersects with another ray drawn before. Ari repeats this process for vertex 2, 3, ..., n (in this particular order). And then she puts a walnut in each region inside the polygon.\n\n [Image] \n\nAda the squirrel wants to collect all the walnuts, but she is not allowed to step on the lines drawn by Ari. That means Ada have to perform a small jump if she wants to go from one region to another. Ada can jump from one region P to another region Q if and only if P and Q share a side or a corner.\n\nAssuming that Ada starts from outside of the picture, what is the minimum number of jumps she has to perform in order to collect all the walnuts?\n\n\n-----Input-----\n\nThe first and only line of the input contains a single integer n (3 \u2264 n \u2264 54321) - the number of vertices of the regular polygon drawn by Ari.\n\n\n-----Output-----\n\nPrint the minimum number of jumps Ada should make to collect all the walnuts. Note, that she doesn't need to leave the polygon after.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n9\n\nInput\n3\n\nOutput\n1\n\n\n\n-----Note-----\n\nOne of the possible solutions for the first sample is shown on the picture above."} +{"id": "01101", "text": "In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his k cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of n rooms located in a row, some of which are occupied.\n\nFarmer John wants to book a set of k + 1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms i and j is defined as |j - i|. Help Farmer John protect his cows by calculating this minimum possible distance.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 k < n \u2264 100 000)\u00a0\u2014 the number of rooms in the hotel and the number of cows travelling with Farmer John.\n\nThe second line contains a string of length n describing the rooms. The i-th character of the string will be '0' if the i-th room is free, and '1' if the i-th room is occupied. It is guaranteed that at least k + 1 characters of this string are '0', so there exists at least one possible choice of k + 1 rooms for Farmer John and his cows to stay in.\n\n\n-----Output-----\n\nPrint the minimum possible distance between Farmer John's room and his farthest cow.\n\n\n-----Examples-----\nInput\n7 2\n0100100\n\nOutput\n2\n\nInput\n5 1\n01010\n\nOutput\n2\n\nInput\n3 2\n000\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms.\n\nIn the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2.\n\nIn the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1."} +{"id": "01102", "text": "There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.\n\nLimak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.\n\nLimak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.\n\nYou know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and a (1 \u2264 a \u2264 n \u2264 100)\u00a0\u2014 the number of cities and the index of city where Limak lives.\n\nThe second line contains n integers t_1, t_2, ..., t_{n} (0 \u2264 t_{i} \u2264 1). There are t_{i} criminals in the i-th city.\n\n\n-----Output-----\n\nPrint the number of criminals Limak will catch.\n\n\n-----Examples-----\nInput\n6 3\n1 1 1 0 1 0\n\nOutput\n3\n\nInput\n5 2\n0 0 0 1 0\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.\n\n [Image] \n\nUsing the BCD gives Limak the following information:\n\n There is one criminal at distance 0 from the third city\u00a0\u2014 Limak is sure that this criminal is exactly in the third city. There is one criminal at distance 1 from the third city\u00a0\u2014 Limak doesn't know if a criminal is in the second or fourth city. There are two criminals at distance 2 from the third city\u00a0\u2014 Limak is sure that there is one criminal in the first city and one in the fifth city. There are zero criminals for every greater distance. \n\nSo, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.\n\nIn the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.\n\n [Image]"} +{"id": "01103", "text": "Vladik was bored on his way home and decided to play the following game. He took n cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions:\n\n the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are c_{k} cards with number k on them in the subsequence, than for all pairs of integers $i \\in [ 1,8 ], j \\in [ 1,8 ]$ the condition |c_{i} - c_{j}| \u2264 1 must hold. if there is at least one card with number x on it in the subsequence, then all cards with number x in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence [1, 1, 2, 2] satisfies this condition while the subsequence [1, 2, 2, 1] doesn't. Note that [1, 1, 2, 2] doesn't satisfy the first condition. \n\nPlease help Vladik to find the length of the longest subsequence that satisfies both conditions.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of cards in Vladik's sequence.\n\nThe second line contains the sequence of n positive integers not exceeding 8\u00a0\u2014 the description of Vladik's sequence.\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the length of the longest subsequence of Vladik's sequence that satisfies both conditions.\n\n\n-----Examples-----\nInput\n3\n1 1 1\n\nOutput\n1\nInput\n8\n8 7 6 5 4 3 2 1\n\nOutput\n8\nInput\n24\n1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8\n\nOutput\n17\n\n\n-----Note-----\n\nIn the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition."} +{"id": "01104", "text": "When Masha came to math classes today, she saw two integer sequences of length $n - 1$ on the blackboard. Let's denote the elements of the first sequence as $a_i$ ($0 \\le a_i \\le 3$), and the elements of the second sequence as $b_i$ ($0 \\le b_i \\le 3$).\n\nMasha became interested if or not there is an integer sequence of length $n$, which elements we will denote as $t_i$ ($0 \\le t_i \\le 3$), so that for every $i$ ($1 \\le i \\le n - 1$) the following is true: $a_i = t_i | t_{i + 1}$ (where $|$ denotes the bitwise OR operation) and $b_i = t_i \\& t_{i + 1}$ (where $\\&$ denotes the bitwise AND operation). \n\nThe question appeared to be too difficult for Masha, so now she asked you to check whether such a sequence $t_i$ of length $n$ exists. If it exists, find such a sequence. If there are multiple such sequences, find any of them.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 10^5$)\u00a0\u2014 the length of the sequence $t_i$. \n\nThe second line contains $n - 1$ integers $a_1, a_2, \\ldots, a_{n-1}$ ($0 \\le a_i \\le 3$)\u00a0\u2014 the first sequence on the blackboard.\n\nThe third line contains $n - 1$ integers $b_1, b_2, \\ldots, b_{n-1}$ ($0 \\le b_i \\le 3$)\u00a0\u2014 the second sequence on the blackboard.\n\n\n-----Output-----\n\nIn the first line print \"YES\" (without quotes), if there is a sequence $t_i$ that satisfies the conditions from the statements, and \"NO\" (without quotes), if there is no such sequence.\n\nIf there is such a sequence, on the second line print $n$ integers $t_1, t_2, \\ldots, t_n$ ($0 \\le t_i \\le 3$)\u00a0\u2014 the sequence that satisfies the statements conditions.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n4\n3 3 2\n1 2 0\n\nOutput\nYES\n1 3 2 0 \nInput\n3\n1 3\n3 2\n\nOutput\nNO\n\n\n-----Note-----\n\nIn the first example it's easy to see that the sequence from output satisfies the given conditions: $t_1 | t_2 = (01_2) | (11_2) = (11_2) = 3 = a_1$ and $t_1 \\& t_2 = (01_2) \\& (11_2) = (01_2) = 1 = b_1$; $t_2 | t_3 = (11_2) | (10_2) = (11_2) = 3 = a_2$ and $t_2 \\& t_3 = (11_2) \\& (10_2) = (10_2) = 2 = b_2$; $t_3 | t_4 = (10_2) | (00_2) = (10_2) = 2 = a_3$ and $t_3 \\& t_4 = (10_2) \\& (00_2) = (00_2) = 0 = b_3$. \n\nIn the second example there is no such sequence."} +{"id": "01105", "text": "During the \"Russian Code Cup\" programming competition, the testing system stores all sent solutions for each participant. We know that many participants use random numbers in their programs and are often sent several solutions with the same source code to check.\n\nEach participant is identified by some unique positive integer k, and each sent solution A is characterized by two numbers: x\u00a0\u2014 the number of different solutions that are sent before the first solution identical to A, and k \u2014 the number of the participant, who is the author of the solution. Consequently, all identical solutions have the same x.\n\nIt is known that the data in the testing system are stored in the chronological order, that is, if the testing system has a solution with number x (x > 0) of the participant with number k, then the testing system has a solution with number x - 1 of the same participant stored somewhere before.\n\nDuring the competition the checking system crashed, but then the data of the submissions of all participants have been restored. Now the jury wants to verify that the recovered data is in chronological order. Help the jury to do so.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of solutions. Each of the following n lines contains two integers separated by space x and k (0 \u2264 x \u2264 10^5; 1 \u2264 k \u2264 10^5)\u00a0\u2014 the number of previous unique solutions and the identifier of the participant.\n\n\n-----Output-----\n\nA single line of the output should contain \u00abYES\u00bb if the data is in chronological order, and \u00abNO\u00bb otherwise.\n\n\n-----Examples-----\nInput\n2\n0 1\n1 1\n\nOutput\nYES\n\nInput\n4\n0 1\n1 2\n1 1\n0 2\n\nOutput\nNO\n\nInput\n4\n0 1\n1 1\n0 1\n0 2\n\nOutput\nYES"} +{"id": "01106", "text": "Om Nom is the main character of a game \"Cut the Rope\". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him. [Image] \n\nThe park consists of 2^{n} + 1 - 1 squares connected by roads so that the scheme of the park is a full binary tree of depth n. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2^{n}, 2^{n} + 1, ..., 2^{n} + 1 - 1 and these exits lead straight to the Om Nom friends' houses. From each square i (2 \u2264 i < 2^{n} + 1) there is a road to the square $\\lfloor \\frac{i}{2} \\rfloor$. Thus, it is possible to go from the park entrance to each of the exits by walking along exactly n roads. [Image] To light the path roads in the evening, the park keeper installed street lights along each road. The road that leads from square i to square $\\lfloor \\frac{i}{2} \\rfloor$ has a_{i} lights.\n\nOm Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe. \n\nHe asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10) \u2014 the number of roads on the path from the entrance to any exit.\n\nThe next line contains 2^{n} + 1 - 2 numbers a_2, a_3, ... a_2^{n} + 1 - 1 \u2014 the initial numbers of street lights on each road of the park. Here a_{i} is the number of street lights on the road between squares i and $\\lfloor \\frac{i}{2} \\rfloor$. All numbers a_{i} are positive integers, not exceeding 100.\n\n\n-----Output-----\n\nPrint the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.\n\n\n-----Examples-----\nInput\n2\n1 2 3 4 5 6\n\nOutput\n5\n\n\n\n-----Note-----\n\nPicture for the sample test. Green color denotes the additional street lights. [Image]"} +{"id": "01107", "text": "Everybody knows that the Berland citizens are keen on health, especially students. Berland students are so tough that all they drink is orange juice!\n\nYesterday one student, Vasya and his mates made some barbecue and they drank this healthy drink only. After they ran out of the first barrel of juice, they decided to play a simple game. All n people who came to the barbecue sat in a circle (thus each person received a unique index b_{i} from 0 to n - 1). The person number 0 started the game (this time it was Vasya). All turns in the game were numbered by integers starting from 1. If the j-th turn was made by the person with index b_{i}, then this person acted like that: he pointed at the person with index (b_{i} + 1)\u00a0mod\u00a0n either with an elbow or with a nod (x\u00a0mod\u00a0y is the remainder after dividing x by y); if j \u2265 4 and the players who had turns number j - 1, j - 2, j - 3, made during their turns the same moves as player b_{i} on the current turn, then he had drunk a glass of juice; the turn went to person number (b_{i} + 1)\u00a0mod\u00a0n. \n\nThe person who was pointed on the last turn did not make any actions.\n\nThe problem was, Vasya's drunk too much juice and can't remember the goal of the game. However, Vasya's got the recorded sequence of all the participants' actions (including himself). Now Vasya wants to find out the maximum amount of juice he could drink if he played optimally well (the other players' actions do not change). Help him.\n\nYou can assume that in any scenario, there is enough juice for everybody.\n\n\n-----Input-----\n\nThe first line contains a single integer n (4 \u2264 n \u2264 2000) \u2014 the number of participants in the game. The second line describes the actual game: the i-th character of this line equals 'a', if the participant who moved i-th pointed at the next person with his elbow, and 'b', if the participant pointed with a nod. The game continued for at least 1 and at most 2000 turns. \n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of glasses of juice Vasya could have drunk if he had played optimally well.\n\n\n-----Examples-----\nInput\n4\nabbba\n\nOutput\n1\n\nInput\n4\nabbab\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn both samples Vasya has got two turns \u2014 1 and 5. In the first sample, Vasya could have drunk a glass of juice during the fifth turn if he had pointed at the next person with a nod. In this case, the sequence of moves would look like \"abbbb\". In the second sample Vasya wouldn't drink a single glass of juice as the moves performed during turns 3 and 4 are different."} +{"id": "01108", "text": "George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. \n\nGeorge and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has p_{i} people living in it and the room can accommodate q_{i} people in total (p_{i} \u2264 q_{i}). Your task is to count how many rooms has free place for both George and Alex.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100) \u2014 the number of rooms.\n\nThe i-th of the next n lines contains two integers p_{i} and q_{i} (0 \u2264 p_{i} \u2264 q_{i} \u2264 100) \u2014 the number of people who already live in the i-th room and the room's capacity.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of rooms where George and Alex can move in.\n\n\n-----Examples-----\nInput\n3\n1 1\n2 2\n3 3\n\nOutput\n0\n\nInput\n3\n1 10\n0 10\n10 10\n\nOutput\n2"} +{"id": "01109", "text": "This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.\n\nArray a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly $\\frac{n}{k}$ times consecutively. In other words, array a is k-periodic, if it has period of length k.\n\nFor example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.\n\nFor the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.\n\n\n-----Input-----\n\nThe first line of the input contains a pair of integers n, k (1 \u2264 k \u2264 n \u2264 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 2), a_{i} is the i-th element of the array.\n\n\n-----Output-----\n\nPrint the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.\n\n\n-----Examples-----\nInput\n6 2\n2 1 2 2 2 1\n\nOutput\n1\n\nInput\n8 4\n1 1 2 1 1 1 2 1\n\nOutput\n0\n\nInput\n9 3\n2 1 1 1 2 1 1 1 2\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].\n\nIn the second sample, the given array already is 4-periodic.\n\nIn the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] \u2014 this array is simultaneously 1-, 3- and 9-periodic."} +{"id": "01110", "text": "Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you should press the buttons in a certain order to open the lock. When you push some button, it either stays pressed into the lock (that means that you've guessed correctly and pushed the button that goes next in the sequence), or all pressed buttons return to the initial position. When all buttons are pressed into the lock at once, the lock opens.\n\nConsider an example with three buttons. Let's say that the opening sequence is: {2, 3, 1}. If you first press buttons 1 or 3, the buttons unpress immediately. If you first press button 2, it stays pressed. If you press 1 after 2, all buttons unpress. If you press 3 after 2, buttons 3 and 2 stay pressed. As soon as you've got two pressed buttons, you only need to press button 1 to open the lock.\n\nManao doesn't know the opening sequence. But he is really smart and he is going to act in the optimal way. Calculate the number of times he's got to push a button in order to open the lock in the worst-case scenario.\n\n\n-----Input-----\n\nA single line contains integer n (1 \u2264 n \u2264 2000) \u2014 the number of buttons the lock has.\n\n\n-----Output-----\n\nIn a single line print the number of times Manao has to push a button in the worst-case scenario.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n3\n\nInput\n3\n\nOutput\n7\n\n\n\n-----Note-----\n\nConsider the first test sample. Manao can fail his first push and push the wrong button. In this case he will already be able to guess the right one with his second push. And his third push will push the second right button. Thus, in the worst-case scenario he will only need 3 pushes."} +{"id": "01111", "text": "You are given a set of n elements indexed from 1 to n. The weight of i-th element is w_{i}. The weight of some subset of a given set is denoted as $W(S) =|S|\\cdot \\sum_{i \\in S} w_{i}$. The weight of some partition R of a given set into k subsets is $W(R) = \\sum_{S \\in R} W(S)$ (recall that a partition of a given set is a set of its subsets such that every element of the given set belongs to exactly one subset in partition).\n\nCalculate the sum of weights of all partitions of a given set into exactly k non-empty subsets, and print it modulo 10^9 + 7. Two partitions are considered different iff there exist two elements x and y such that they belong to the same set in one of the partitions, and to different sets in another partition.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 k \u2264 n \u2264 2\u00b710^5) \u2014 the number of elements and the number of subsets in each partition, respectively.\n\nThe second line contains n integers w_{i} (1 \u2264 w_{i} \u2264 10^9)\u2014 weights of elements of the set.\n\n\n-----Output-----\n\nPrint one integer \u2014 the sum of weights of all partitions of a given set into k non-empty subsets, taken modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n4 2\n2 3 2 3\n\nOutput\n160\n\nInput\n5 2\n1 2 3 4 5\n\nOutput\n645\n\n\n\n-----Note-----\n\nPossible partitions in the first sample: {{1, 2, 3}, {4}}, W(R) = 3\u00b7(w_1 + w_2 + w_3) + 1\u00b7w_4 = 24; {{1, 2, 4}, {3}}, W(R) = 26; {{1, 3, 4}, {2}}, W(R) = 24; {{1, 2}, {3, 4}}, W(R) = 2\u00b7(w_1 + w_2) + 2\u00b7(w_3 + w_4) = 20; {{1, 3}, {2, 4}}, W(R) = 20; {{1, 4}, {2, 3}}, W(R) = 20; {{1}, {2, 3, 4}}, W(R) = 26; \n\nPossible partitions in the second sample: {{1, 2, 3, 4}, {5}}, W(R) = 45; {{1, 2, 3, 5}, {4}}, W(R) = 48; {{1, 2, 4, 5}, {3}}, W(R) = 51; {{1, 3, 4, 5}, {2}}, W(R) = 54; {{2, 3, 4, 5}, {1}}, W(R) = 57; {{1, 2, 3}, {4, 5}}, W(R) = 36; {{1, 2, 4}, {3, 5}}, W(R) = 37; {{1, 2, 5}, {3, 4}}, W(R) = 38; {{1, 3, 4}, {2, 5}}, W(R) = 38; {{1, 3, 5}, {2, 4}}, W(R) = 39; {{1, 4, 5}, {2, 3}}, W(R) = 40; {{2, 3, 4}, {1, 5}}, W(R) = 39; {{2, 3, 5}, {1, 4}}, W(R) = 40; {{2, 4, 5}, {1, 3}}, W(R) = 41; {{3, 4, 5}, {1, 2}}, W(R) = 42."} +{"id": "01112", "text": "Little Elephant loves magic squares very much.\n\nA magic square is a 3 \u00d7 3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square, the sum of integers in all its rows, columns and diagonals equals 15. $\\left. \\begin{array}{|c|c|c|} \\hline 4 & {9} & {2} \\\\ \\hline 3 & {5} & {7} \\\\ \\hline 8 & {1} & {6} \\\\ \\hline \\end{array} \\right.$ \n\nThe Little Elephant remembered one magic square. He started writing this square on a piece of paper, but as he wrote, he forgot all three elements of the main diagonal of the magic square. Fortunately, the Little Elephant clearly remembered that all elements of the magic square did not exceed 10^5. \n\nHelp the Little Elephant, restore the original magic square, given the Elephant's notes.\n\n\n-----Input-----\n\nThe first three lines of the input contain the Little Elephant's notes. The first line contains elements of the first row of the magic square. The second line contains the elements of the second row, the third line is for the third row. The main diagonal elements that have been forgotten by the Elephant are represented by zeroes.\n\nIt is guaranteed that the notes contain exactly three zeroes and they are all located on the main diagonal. It is guaranteed that all positive numbers in the table do not exceed 10^5.\n\n\n-----Output-----\n\nPrint three lines, in each line print three integers \u2014 the Little Elephant's magic square. If there are multiple magic squares, you are allowed to print any of them. Note that all numbers you print must be positive and not exceed 10^5.\n\nIt is guaranteed that there exists at least one magic square that meets the conditions.\n\n\n-----Examples-----\nInput\n0 1 1\n1 0 1\n1 1 0\n\nOutput\n1 1 1\n1 1 1\n1 1 1\n\nInput\n0 3 6\n5 0 5\n4 7 0\n\nOutput\n6 3 6\n5 5 5\n4 7 4"} +{"id": "01113", "text": "Initially Ildar has an empty array. He performs $n$ steps. On each step he takes a subset of integers already added to the array and appends the mex of this subset to the array. \n\nThe mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset $[0, 2, 3]$ is $1$, while the mex of the multiset $[1, 2, 1]$ is $0$.\n\nMore formally, on the step $m$, when Ildar already has an array $a_1, a_2, \\ldots, a_{m-1}$, he chooses some subset of indices $1 \\leq i_1 < i_2 < \\ldots < i_k < m$ (possibly, empty), where $0 \\leq k < m$, and appends the $mex(a_{i_1}, a_{i_2}, \\ldots a_{i_k})$ to the end of the array.\n\nAfter performing all the steps Ildar thinks that he might have made a mistake somewhere. He asks you to determine for a given array $a_1, a_2, \\ldots, a_n$ the minimum step $t$ such that he has definitely made a mistake on at least one of the steps $1, 2, \\ldots, t$, or determine that he could have obtained this array without mistakes.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 100\\,000$)\u00a0\u2014 the number of steps Ildar made.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\leq a_i \\leq 10^9$)\u00a0\u2014 the array Ildar obtained.\n\n\n-----Output-----\n\nIf Ildar could have chosen the subsets on each step in such a way that the resulting array is $a_1, a_2, \\ldots, a_n$, print $-1$.\n\nOtherwise print a single integer $t$\u00a0\u2014 the smallest index of a step such that a mistake was made on at least one step among steps $1, 2, \\ldots, t$.\n\n\n-----Examples-----\nInput\n4\n0 1 2 1\n\nOutput\n-1\nInput\n3\n1 0 1\n\nOutput\n1\nInput\n4\n0 1 2 239\n\nOutput\n4\n\n\n-----Note-----\n\nIn the first example it is possible that Ildar made no mistakes. Here is the process he could have followed. $1$-st step. The initial array is empty. He can choose an empty subset and obtain $0$, because the mex of an empty set is $0$. Appending this value to the end he gets the array $[0]$. $2$-nd step. The current array is $[0]$. He can choose a subset $[0]$ and obtain an integer $1$, because $mex(0) = 1$. Appending this value to the end he gets the array $[0,1]$. $3$-rd step. The current array is $[0,1]$. He can choose a subset $[0,1]$ and obtain an integer $2$, because $mex(0,1) = 2$. Appending this value to the end he gets the array $[0,1,2]$. $4$-th step. The current array is $[0,1,2]$. He can choose a subset $[0]$ and obtain an integer $1$, because $mex(0) = 1$. Appending this value to the end he gets the array $[0,1,2,1]$. \n\nThus, he can get the array without mistakes, so the answer is $-1$.\n\nIn the second example he has definitely made a mistake on the very first step, because he could not have obtained anything different from $0$.\n\nIn the third example he could have obtained $[0, 1, 2]$ without mistakes, but $239$ is definitely wrong."} +{"id": "01114", "text": "While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a_1, a_2, ..., a_{m} of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f_1, f_2, ..., f_{n} of length n and for each number a_{i} got number b_{i} = f_{a}_{i}. To finish the prank he erased the initial sequence a_{i}.\n\nIt's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 100 000)\u00a0\u2014 the lengths of sequences f_{i} and b_{i} respectively.\n\nThe second line contains n integers, determining sequence f_1, f_2, ..., f_{n} (1 \u2264 f_{i} \u2264 n).\n\nThe last line contains m integers, determining sequence b_1, b_2, ..., b_{m} (1 \u2264 b_{i} \u2264 n).\n\n\n-----Output-----\n\nPrint \"Possible\" if there is exactly one sequence a_{i}, such that b_{i} = f_{a}_{i} for all i from 1 to m. Then print m integers a_1, a_2, ..., a_{m}.\n\nIf there are multiple suitable sequences a_{i}, print \"Ambiguity\".\n\nIf Spongebob has made a mistake in his calculations and no suitable sequence a_{i} exists, print \"Impossible\".\n\n\n-----Examples-----\nInput\n3 3\n3 2 1\n1 2 3\n\nOutput\nPossible\n3 2 1 \n\nInput\n3 3\n1 1 1\n1 1 1\n\nOutput\nAmbiguity\n\nInput\n3 3\n1 2 1\n3 3 3\n\nOutput\nImpossible\n\n\n\n-----Note-----\n\nIn the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique.\n\nIn the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.\n\nIn the third sample f_{i} \u2260 3 for all i, so no sequence a_{i} transforms into such b_{i} and we can say for sure that Spongebob has made a mistake."} +{"id": "01115", "text": "Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!\n\nThis contest consists of n problems, and Pasha solves ith problem in a_{i} time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.\n\nUnfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment l_{j} and ending moment r_{j}. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that l_{x} \u2264 T \u2264 r_{x}.\n\nPasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.\n\n\n-----Input-----\n\nThe first line contains one integer n\u00a0(1 \u2264 n \u2264 1000) \u2014 the number of problems. The second line contains n integers a_{i}\u00a0(1 \u2264 a_{i} \u2264 10^5) \u2014 the time Pasha needs to solve ith problem.\n\nThe third line contains one integer m\u00a0(0 \u2264 m \u2264 1000) \u2014 the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers l_{j} and r_{j}\u00a0(1 \u2264 l_{j} < r_{j} \u2264 10^5) \u2014 the starting and the ending moment of jth period.\n\nIt is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition l_{j} > r_{j} - 1 is met.\n\n\n-----Output-----\n\nIf Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.\n\nOtherwise print \"-1\" (without brackets).\n\n\n-----Examples-----\nInput\n2\n3 4\n2\n1 4\n7 9\n\nOutput\n7\n\nInput\n1\n5\n1\n1 4\n\nOutput\n-1\n\nInput\n1\n5\n1\n1 5\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.\n\nIn the second example Pasha invents the solution only after the website stops working for the last time.\n\nIn the third example Pasha sends the solution exactly at the end of the first period."} +{"id": "01116", "text": "You are a rebel leader and you are planning to start a revolution in your country. But the evil Government found out about your plans and set your punishment in the form of correctional labor.\n\nYou must paint a fence which consists of $10^{100}$ planks in two colors in the following way (suppose planks are numbered from left to right from $0$): if the index of the plank is divisible by $r$ (such planks have indices $0$, $r$, $2r$ and so on) then you must paint it red; if the index of the plank is divisible by $b$ (such planks have indices $0$, $b$, $2b$ and so on) then you must paint it blue; if the index is divisible both by $r$ and $b$ you can choose the color to paint the plank; otherwise, you don't need to paint the plank at all (and it is forbidden to spent paint on it). \n\nFurthermore, the Government added one additional restriction to make your punishment worse. Let's list all painted planks of the fence in ascending order: if there are $k$ consecutive planks with the same color in this list, then the Government will state that you failed the labor and execute you immediately. If you don't paint the fence according to the four aforementioned conditions, you will also be executed.\n\nThe question is: will you be able to accomplish the labor (the time is not important) or the execution is unavoidable and you need to escape at all costs.\n\n\n-----Input-----\n\nThe first line contains single integer $T$ ($1 \\le T \\le 1000$) \u2014 the number of test cases.\n\nThe next $T$ lines contain descriptions of test cases \u2014 one per line. Each test case contains three integers $r$, $b$, $k$ ($1 \\le r, b \\le 10^9$, $2 \\le k \\le 10^9$) \u2014 the corresponding coefficients.\n\n\n-----Output-----\n\nPrint $T$ words \u2014 one per line. For each test case print REBEL (case insensitive) if the execution is unavoidable or OBEY (case insensitive) otherwise.\n\n\n-----Example-----\nInput\n4\n1 1 2\n2 10 4\n5 2 3\n3 2 2\n\nOutput\nOBEY\nREBEL\nOBEY\nOBEY"} +{"id": "01117", "text": "There are $n$ rectangles in a row. You can either turn each rectangle by $90$ degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.\n\nFind out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such). \n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^5$)\u00a0\u2014 the number of rectangles.\n\nEach of the next $n$ lines contains two integers $w_i$ and $h_i$ ($1 \\leq w_i, h_i \\leq 10^9$)\u00a0\u2014 the width and the height of the $i$-th rectangle.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n3\n3 4\n4 6\n3 5\n\nOutput\nYES\n\nInput\n2\n3 4\n5 5\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].\n\nIn the second test, there is no way the second rectangle will be not higher than the first one."} +{"id": "01118", "text": "You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.\n\nLet's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.\n\nFor example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.\n\nThe game \"flood fill\" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color. \n\nFind the minimum number of turns needed for the entire line to be changed into a single color.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 5000$)\u00a0\u2014 the number of squares.\n\nThe second line contains integers $c_1, c_2, \\ldots, c_n$ ($1 \\le c_i \\le 5000$)\u00a0\u2014 the initial colors of the squares.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of the turns needed.\n\n\n-----Examples-----\nInput\n4\n5 2 2 1\n\nOutput\n2\n\nInput\n8\n4 5 2 2 1 3 5 5\n\nOutput\n4\n\nInput\n1\n4\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$ \n\nIn the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.\n\nIn the third example, the line already consists of one color only."} +{"id": "01119", "text": "You are given three integers k, p_{a} and p_{b}.\n\nYou will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability p_{a} / (p_{a} + p_{b}), add 'a' to the end of the sequence. Otherwise (with probability p_{b} / (p_{a} + p_{b})), add 'b' to the end of the sequence.\n\nYou stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and $Q \\neq 0 \\operatorname{mod}(10^{9} + 7)$. Print the value of $P \\cdot Q^{-1} \\operatorname{mod}(10^{9} + 7)$.\n\n\n-----Input-----\n\nThe first line will contain three integers integer k, p_{a}, p_{b} (1 \u2264 k \u2264 1 000, 1 \u2264 p_{a}, p_{b} \u2264 1 000 000).\n\n\n-----Output-----\n\nPrint a single integer, the answer to the problem.\n\n\n-----Examples-----\nInput\n1 1 1\n\nOutput\n2\n\nInput\n3 1 4\n\nOutput\n370000006\n\n\n\n-----Note-----\n\nThe first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'. \n\nThe expected amount of times that 'ab' will occur across all valid sequences is 2. \n\nFor the second sample, the answer is equal to $\\frac{341}{100}$."} +{"id": "01120", "text": "Yet another Armageddon is coming! This time the culprit is the Julya tribe calendar. \n\nThe beavers in this tribe knew math very well. Smart Beaver, an archaeologist, got a sacred plate with a magic integer on it. The translation from Old Beaverish is as follows: \n\n\"May the Great Beaver bless you! May your chacres open and may your third eye never turn blind from beholding the Truth! Take the magic number, subtract a digit from it (the digit must occur in the number) and get a new magic number. Repeat this operation until a magic number equals zero. The Earth will stand on Three Beavers for the time, equal to the number of subtractions you perform!\"\n\nDistinct subtraction sequences can obviously get you different number of operations. But the Smart Beaver is ready to face the worst and is asking you to count the minimum number of operations he needs to reduce the magic number to zero.\n\n\n-----Input-----\n\nThe single line contains the magic integer n, 0 \u2264 n.\n\n to get 20 points, you need to solve the problem with constraints: n \u2264 10^6 (subproblem C1); to get 40 points, you need to solve the problem with constraints: n \u2264 10^12 (subproblems C1+C2); to get 100 points, you need to solve the problem with constraints: n \u2264 10^18 (subproblems C1+C2+C3). \n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of subtractions that turns the magic number to a zero.\n\n\n-----Examples-----\nInput\n24\n\nOutput\n5\n\n\n-----Note-----\n\nIn the first test sample the minimum number of operations can be reached by the following sequence of subtractions: \n\n 24 \u2192 20 \u2192 18 \u2192 10 \u2192 9 \u2192 0"} +{"id": "01121", "text": "You have an n \u00d7 m rectangle table, its cells are not initially painted. Your task is to paint all cells of the table. The resulting picture should be a tiling of the table with squares. More formally: each cell must be painted some color (the colors are marked by uppercase Latin letters); we will assume that two cells of the table are connected if they are of the same color and share a side; each connected region of the table must form a square. \n\nGiven n and m, find lexicographically minimum coloring of the table that meets the described properties.\n\n\n-----Input-----\n\nThe first line contains two integers, n and m (1 \u2264 n, m \u2264 100).\n\n\n-----Output-----\n\nPrint lexicographically minimum coloring of the table that meets the described conditions. \n\nOne coloring (let's call it X) is considered lexicographically less than the other one (let's call it Y), if: consider all the table cells from left to right and from top to bottom (first, the first cell in the first row, then the second cell in the first row and so on); let's find in this order the first cell that has distinct colors in two colorings; the letter that marks the color of the cell in X, goes alphabetically before the letter that marks the color of the cell in Y. \n\n\n-----Examples-----\nInput\n1 3\n\nOutput\nABA\n\nInput\n2 2\n\nOutput\nAA\nAA\n\nInput\n3 4\n\nOutput\nAAAB\nAAAC\nAAAB"} +{"id": "01122", "text": "You are going to hold a competition of one-to-one game called AtCoder Janken. (Janken is the Japanese name for Rock-paper-scissors.)N players will participate in this competition, and they are given distinct integers from 1 through N.\nThe arena has M playing fields for two players. You need to assign each playing field two distinct integers between 1 and N (inclusive).\nYou cannot assign the same integer to multiple playing fields.\nThe competition consists of N rounds, each of which proceeds as follows:\n - For each player, if there is a playing field that is assigned the player's integer, the player goes to that field and fight the other player who comes there.\n - Then, each player adds 1 to its integer. If it becomes N+1, change it to 1.\nYou want to ensure that no player fights the same opponent more than once during the N rounds.\nPrint an assignment of integers to the playing fields satisfying this condition.\nIt can be proved that such an assignment always exists under the constraints given.\n\n-----Constraints-----\n - 1 \\leq M\n - M \\times 2 +1 \\leq N \\leq 200000\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\n\n-----Output-----\nPrint M lines in the format below.\nThe i-th line should contain the two integers a_i and b_i assigned to the i-th playing field.\na_1 b_1\na_2 b_2\n:\na_M b_M\n\n-----Sample Input-----\n4 1\n\n-----Sample Output-----\n2 3\n\nLet us call the four players A, B, C, and D, and assume that they are initially given the integers 1, 2, 3, and 4, respectively.\n - The 1-st round is fought by B and C, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 2, 3, 4, and 1, respectively.\n - The 2-nd round is fought by A and B, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 3, 4, 1, and 2, respectively.\n - The 3-rd round is fought by D and A, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 4, 1, 2, and 3, respectively.\n - The 4-th round is fought by C and D, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 1, 2, 3, and 4, respectively.\nNo player fights the same opponent more than once during the four rounds, so this solution will be accepted."} +{"id": "01123", "text": "Consider sequences \\{A_1,...,A_N\\} of length N consisting of integers between 1 and K (inclusive).\nThere are K^N such sequences. Find the sum of \\gcd(A_1, ..., A_N) over all of them.\nSince this sum can be enormous, print the value modulo (10^9+7).\nHere \\gcd(A_1, ..., A_N) denotes the greatest common divisor of A_1, ..., A_N.\n\n-----Constraints-----\n - 2 \\leq N \\leq 10^5\n - 1 \\leq K \\leq 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\n\n-----Output-----\nPrint the sum of \\gcd(A_1, ..., A_N) over all K^N sequences, modulo (10^9+7).\n\n-----Sample Input-----\n3 2\n\n-----Sample Output-----\n9\n\n\\gcd(1,1,1)+\\gcd(1,1,2)+\\gcd(1,2,1)+\\gcd(1,2,2)+\\gcd(2,1,1)+\\gcd(2,1,2)+\\gcd(2,2,1)+\\gcd(2,2,2)=1+1+1+1+1+1+1+2=9\nThus, the answer is 9."} +{"id": "01124", "text": "Snuke had N cards numbered 1 through N.\nEach card has an integer written on it; written on Card i is a_i.\nSnuke did the following procedure:\n - Let X and x be the maximum and minimum values written on Snuke's cards, respectively.\n - If X = x, terminate the procedure. Otherwise, replace each card on which X is written with a card on which X-x is written, then go back to step 1.\nUnder the constraints in this problem, it can be proved that the procedure will eventually terminate. Find the number written on all of Snuke's cards after the procedure.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^{5}\n - 1 \\leq a_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 \\cdots a_N\n\n-----Output-----\nPrint the number written on all of Snuke's cards after the procedure.\n\n-----Sample Input-----\n3\n2 6 6\n\n-----Sample Output-----\n2\n\n - At the beginning of the procedure, the numbers written on Snuke's cards are (2,6,6).\n - Since x=2 and X=6, he replaces each card on which 6 is written with a card on which 4 is written.\n - Now, the numbers written on Snuke's cards are (2,4,4).\n - Since x=2 and X=4, he replaces each card on which 4 is written with a card on which 2 is written.\n - Now, the numbers written on Snuke's cards are (2,2,2).\n - Since x=2 and X=2, he terminates the procedure."} +{"id": "01125", "text": "There are N piles of stones. The i-th pile has A_i stones.\nAoki and Takahashi are about to use them to play the following game:\n - Starting with Aoki, the two players alternately do the following operation:\n - Operation: Choose one pile of stones, and remove one or more stones from it.\n - When a player is unable to do the operation, he loses, and the other player wins.\nWhen the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.\nIn such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.\nIf this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.\n\n-----Constraints-----\n - 2 \\leq N \\leq 300\n - 1 \\leq A_i \\leq 10^{12}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 \\ldots A_N\n\n-----Output-----\nPrint the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.\n\n-----Sample Input-----\n2\n5 3\n\n-----Sample Output-----\n1\n\nWithout moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.\nIf Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions."} +{"id": "01126", "text": "Let us denote by f(x, m) the remainder of the Euclidean division of x by m.\nLet A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M).\nFind \\displaystyle{\\sum_{i=1}^N A_i}.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^{10}\n - 0 \\leq X < M \\leq 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN X M\n\n-----Output-----\nPrint \\displaystyle{\\sum_{i=1}^N A_i}.\n\n-----Sample Input-----\n6 2 1001\n\n-----Sample Output-----\n1369\n\nThe sequence A begins 2,4,16,256,471,620,\\ldots Therefore, the answer is 2+4+16+256+471+620=1369."} +{"id": "01127", "text": "Everyone knows that agents in Valorant decide, who will play as attackers, and who will play as defenders. To do that Raze and Breach decided to play $t$ matches of a digit game...\n\nIn each of $t$ matches of the digit game, a positive integer is generated. It consists of $n$ digits. The digits of this integer are numerated from $1$ to $n$ from the highest-order digit to the lowest-order digit. After this integer is announced, the match starts.\n\nAgents play in turns. Raze starts. In one turn an agent can choose any unmarked digit and mark it. Raze can choose digits on odd positions, but can not choose digits on even positions. Breach can choose digits on even positions, but can not choose digits on odd positions. The match ends, when there is only one unmarked digit left. If the single last digit is odd, then Raze wins, else Breach wins.\n\nIt can be proved, that before the end of the match (for every initial integer with $n$ digits) each agent has an ability to make a turn, i.e. there is at least one unmarked digit, that stands on a position of required parity.\n\nFor each of $t$ matches find out, which agent wins, if both of them want to win and play optimally.\n\n\n-----Input-----\n\nFirst line of input contains an integer $t$ $(1 \\le t \\le 100)$ \u00a0\u2014 the number of matches.\n\nThe first line of each match description contains an integer $n$ $(1 \\le n \\le 10^3)$ \u00a0\u2014 the number of digits of the generated number.\n\nThe second line of each match description contains an $n$-digit positive integer without leading zeros.\n\n\n-----Output-----\n\nFor each match print $1$, if Raze wins, and $2$, if Breach wins.\n\n\n-----Example-----\nInput\n4\n1\n2\n1\n3\n3\n102\n4\n2069\n\nOutput\n2\n1\n1\n2\n\n\n\n-----Note-----\n\nIn the first match no one can make a turn, the only digit left is $2$, it's even, so Breach wins.\n\nIn the second match the only digit left is $3$, it's odd, so Raze wins.\n\nIn the third match Raze can mark the last digit, after that Breach can only mark $0$. $1$ will be the last digit left, it's odd, so Raze wins.\n\nIn the fourth match no matter how Raze plays, Breach can mark $9$, and in the end there will be digit $0$. It's even, so Breach wins."} +{"id": "01128", "text": "One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce $x \\operatorname{mod} m$ (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. \n\nThe board of directors are worried that the production by the given plan may eventually stop (that means that there will be \u0430 moment when the current number of details on the factory is divisible by m). \n\nGiven the number of details a on the first day and number m check if the production stops at some moment.\n\n\n-----Input-----\n\nThe first line contains two integers a and m (1 \u2264 a, m \u2264 10^5).\n\n\n-----Output-----\n\nPrint \"Yes\" (without quotes) if the production will eventually stop, otherwise print \"No\".\n\n\n-----Examples-----\nInput\n1 5\n\nOutput\nNo\n\nInput\n3 6\n\nOutput\nYes"} +{"id": "01129", "text": "You are given n points on a line with their coordinates x_{i}. Find the point x so the sum of distances to the given points is minimal.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 3\u00b710^5) \u2014 the number of points on the line.\n\nThe second line contains n integers x_{i} ( - 10^9 \u2264 x_{i} \u2264 10^9) \u2014 the coordinates of the given n points.\n\n\n-----Output-----\n\nPrint the only integer x \u2014 the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.\n\n\n-----Example-----\nInput\n4\n1 2 3 4\n\nOutput\n2"} +{"id": "01130", "text": "Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.\n\nThere are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.\n\nIvan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.\n\nGiven n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?\n\n\n-----Input-----\n\nThe first line contains three integers n, m and k (1 \u2264 n, m \u2264 500, 0 \u2264 k \u2264 500) \u2014 the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.\n\nThen n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).\n\n\n-----Output-----\n\nPrint the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.\n\n\n-----Examples-----\nInput\n2 5 1\n01001\n10110\n\nOutput\n5\n\nInput\n2 5 0\n01001\n10110\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.\n\nIn the second example Ivan can't skip any lessons, so he spends 4 hours every day."} +{"id": "01131", "text": "Arthur and Alexander are number busters. Today they've got a competition. \n\nArthur took a group of four integers a, b, w, x (0 \u2264 b < w, 0 < x < w) and Alexander took integer \u0441. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: c = c - 1. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if b \u2265 x, perform the assignment b = b - x, if b < x, then perform two consecutive assignments a = a - 1;\u00a0b = w - (x - b).\n\nYou've got numbers a, b, w, x, c. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if c \u2264 a.\n\n\n-----Input-----\n\nThe first line contains integers a, b, w, x, c (1 \u2264 a \u2264 2\u00b710^9, 1 \u2264 w \u2264 1000, 0 \u2264 b < w, 0 < x < w, 1 \u2264 c \u2264 2\u00b710^9).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem's limits.\n\n\n-----Examples-----\nInput\n4 2 3 1 6\n\nOutput\n2\n\nInput\n4 2 3 1 7\n\nOutput\n4\n\nInput\n1 2 3 2 6\n\nOutput\n13\n\nInput\n1 1 2 1 1\n\nOutput\n0"} +{"id": "01132", "text": "This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.\n\nPolycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers\n\nPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.\n\nPolycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.\n\nLet's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. [Image] (1) \u2014 bus, (2) \u2014 ring, (3) \u2014 star \n\nYou've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. \n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m (4 \u2264 n \u2264 10^5;\u00a03 \u2264 m \u2264 10^5) \u2014 the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers x_{i}, y_{i} (1 \u2264 x_{i}, y_{i} \u2264 n) \u2014 the numbers of nodes that are connected by the i-the edge.\n\nIt is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.\n\n\n-----Output-----\n\nIn a single line print the network topology name of the given graph. If the answer is the bus, print \"bus topology\" (without the quotes), if the answer is the ring, print \"ring topology\" (without the quotes), if the answer is the star, print \"star topology\" (without the quotes). If no answer fits, print \"unknown topology\" (without the quotes).\n\n\n-----Examples-----\nInput\n4 3\n1 2\n2 3\n3 4\n\nOutput\nbus topology\n\nInput\n4 4\n1 2\n2 3\n3 4\n4 1\n\nOutput\nring topology\n\nInput\n4 3\n1 2\n1 3\n1 4\n\nOutput\nstar topology\n\nInput\n4 4\n1 2\n2 3\n3 1\n1 4\n\nOutput\nunknown topology"} +{"id": "01133", "text": "Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is that each of them uses at most two distinct letters. Andrew decided to send an article to the magazine, but as he hasn't written any article, he just decided to take a random one from magazine 26Char. However, before sending it to the magazine 2Char, he needs to adapt the text to the format of the journal. To do so, he removes some words from the chosen article, in such a way that the remaining text can be written using no more than two distinct letters.\n\nSince the payment depends from the number of non-space characters in the article, Andrew wants to keep the words with the maximum total length.\n\n\n-----Input-----\n\nThe first line of the input contains number n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of words in the article chosen by Andrew. Following are n lines, each of them contains one word. All the words consist only of small English letters and their total length doesn't exceed 1000. The words are not guaranteed to be distinct, in this case you are allowed to use a word in the article as many times as it appears in the input.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum possible total length of words in Andrew's article.\n\n\n-----Examples-----\nInput\n4\nabb\ncacc\naaa\nbbb\n\nOutput\n9\nInput\n5\na\na\nbcbcb\ncdecdecdecdecdecde\naaaa\n\nOutput\n6\n\n\n-----Note-----\n\nIn the first sample the optimal way to choose words is {'abb', 'aaa', 'bbb'}.\n\nIn the second sample the word 'cdecdecdecdecdecde' consists of three distinct letters, and thus cannot be used in the article. The optimal answer is {'a', 'a', 'aaaa'}."} +{"id": "01134", "text": "Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.\n\nArkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}.\n\nDefine d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day.\n\n\n-----Input-----\n\nThe first line contains a single positive integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of days.\n\nThe second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 \u2264 m_{i} < i)\u00a0\u2014 the number of marks strictly above the water on each day.\n\n\n-----Output-----\n\nOutput one single integer\u00a0\u2014 the minimum possible sum of the number of marks strictly below the water level among all days.\n\n\n-----Examples-----\nInput\n6\n0 1 0 3 0 2\n\nOutput\n6\n\nInput\n5\n0 1 2 1 2\n\nOutput\n1\n\nInput\n5\n0 1 1 2 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, the following figure shows an optimal case. [Image] \n\nNote that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.\n\nIn the second example, the following figure shows an optimal case. [Image]"} +{"id": "01135", "text": "Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. \n\nPolycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.\n\nYou are given an encoding s of some word, your task is to decode it. \n\n\n-----Input-----\n\nThe first line contains a positive integer n (1 \u2264 n \u2264 2000)\u00a0\u2014 the length of the encoded word.\n\nThe second line contains the string s of length n consisting of lowercase English letters\u00a0\u2014 the encoding.\n\n\n-----Output-----\n\nPrint the word that Polycarp encoded.\n\n\n-----Examples-----\nInput\n5\nlogva\n\nOutput\nvolga\n\nInput\n2\nno\n\nOutput\nno\n\nInput\n4\nabba\n\nOutput\nbaba\n\n\n\n-----Note-----\n\nIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.\n\nIn the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.\n\nIn the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba."} +{"id": "01136", "text": "Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + ... + n mod m. As the result can be very large, you should print the value modulo 10^9 + 7 (the remainder when divided by 10^9 + 7).\n\nThe modulo operator a mod b stands for the remainder after dividing a by b. For example 10 mod 3 = 1.\n\n\n-----Input-----\n\nThe only line contains two integers n, m (1 \u2264 n, m \u2264 10^13) \u2014 the parameters of the sum.\n\n\n-----Output-----\n\nPrint integer s \u2014 the value of the required sum modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n3 4\n\nOutput\n4\n\nInput\n4 4\n\nOutput\n1\n\nInput\n1 1\n\nOutput\n0"} +{"id": "01137", "text": "After returned from forest, Alyona started reading a book. She noticed strings s and t, lengths of which are n and m respectively. As usual, reading bored Alyona and she decided to pay her attention to strings s and t, which she considered very similar.\n\nAlyona has her favourite positive integer k and because she is too small, k does not exceed 10. The girl wants now to choose k disjoint non-empty substrings of string s such that these strings appear as disjoint substrings of string t and in the same order as they do in string s. She is also interested in that their length is maximum possible among all variants.\n\nFormally, Alyona wants to find a sequence of k non-empty strings p_1, p_2, p_3, ..., p_{k} satisfying following conditions: s can be represented as concatenation a_1p_1a_2p_2... a_{k}p_{k}a_{k} + 1, where a_1, a_2, ..., a_{k} + 1 is a sequence of arbitrary strings (some of them may be possibly empty); t can be represented as concatenation b_1p_1b_2p_2... b_{k}p_{k}b_{k} + 1, where b_1, b_2, ..., b_{k} + 1 is a sequence of arbitrary strings (some of them may be possibly empty); sum of the lengths of strings in sequence is maximum possible. \n\nPlease help Alyona solve this complicated problem and find at least the sum of the lengths of the strings in a desired sequence.\n\nA substring of a string is a subsequence of consecutive characters of the string.\n\n\n-----Input-----\n\nIn the first line of the input three integers n, m, k (1 \u2264 n, m \u2264 1000, 1 \u2264 k \u2264 10) are given\u00a0\u2014 the length of the string s, the length of the string t and Alyona's favourite number respectively.\n\nThe second line of the input contains string s, consisting of lowercase English letters.\n\nThe third line of the input contains string t, consisting of lowercase English letters.\n\n\n-----Output-----\n\nIn the only line print the only non-negative integer\u00a0\u2014 the sum of the lengths of the strings in a desired sequence.\n\nIt is guaranteed, that at least one desired sequence exists.\n\n\n-----Examples-----\nInput\n3 2 2\nabc\nab\n\nOutput\n2\n\nInput\n9 12 4\nbbaaababb\nabbbabbaaaba\n\nOutput\n7\n\n\n\n-----Note-----\n\nThe following image describes the answer for the second sample case: [Image]"} +{"id": "01138", "text": "Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion: An 'L' indicates he should move one unit left. An 'R' indicates he should move one unit right. A 'U' indicates he should move one unit up. A 'D' indicates he should move one unit down.\n\nBut now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.\n\n\n-----Input-----\n\nThe first and only line contains the string s (1 \u2264 |s| \u2264 100 000)\u00a0\u2014 the instructions Memory is given.\n\n\n-----Output-----\n\nIf there is a string satisfying the conditions, output a single integer\u00a0\u2014 the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.\n\n\n-----Examples-----\nInput\nRRU\n\nOutput\n-1\n\nInput\nUDUR\n\nOutput\n1\n\nInput\nRUUR\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.\n\nIn the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to \"LDUR\". This string uses 1 edit, which is the minimum possible. It also ends at the origin."} +{"id": "01139", "text": "Omkar is building a house. He wants to decide how to make the floor plan for the last floor.\n\nOmkar's floor starts out as $n$ rows of $m$ zeros ($1 \\le n,m \\le 100$). Every row is divided into intervals such that every $0$ in the row is in exactly $1$ interval. For every interval for every row, Omkar can change exactly one of the $0$s contained in that interval to a $1$. Omkar defines the quality of a floor as the sum of the squares of the sums of the values in each column, i. e. if the sum of the values in the $i$-th column is $q_i$, then the quality of the floor is $\\sum_{i = 1}^m q_i^2$.\n\nHelp Omkar find the maximum quality that the floor can have.\n\n\n-----Input-----\n\nThe first line contains two integers, $n$ and $m$ ($1 \\le n,m \\le 100$), which are the number of rows and number of columns, respectively.\n\nYou will then receive a description of the intervals in each row. For every row $i$ from $1$ to $n$: The first row contains a single integer $k_i$ ($1 \\le k_i \\le m$), which is the number of intervals on row $i$. The $j$-th of the next $k_i$ lines contains two integers $l_{i,j}$ and $r_{i,j}$, which are the left and right bound (both inclusive), respectively, of the $j$-th interval of the $i$-th row. It is guaranteed that all intervals other than the first interval will be directly after the interval before it. Formally, $l_{i,1} = 1$, $l_{i,j} \\leq r_{i,j}$ for all $1 \\le j \\le k_i$, $r_{i,j-1} + 1 = l_{i,j}$ for all $2 \\le j \\le k_i$, and $r_{i,k_i} = m$.\n\n\n-----Output-----\n\nOutput one integer, which is the maximum possible quality of an eligible floor plan.\n\n\n-----Example-----\nInput\n4 5\n2\n1 2\n3 5\n2\n1 3\n4 5\n3\n1 1\n2 4\n5 5\n3\n1 1\n2 2\n3 5\n\nOutput\n36\n\n\n\n-----Note-----\n\nThe given test case corresponds to the following diagram. Cells in the same row and have the same number are a part of the same interval.\n\n [Image] \n\nThe most optimal assignment is:\n\n [Image] \n\nThe sum of the $1$st column is $4$, the sum of the $2$nd column is $2$, the sum of the $3$rd and $4$th columns are $0$, and the sum of the $5$th column is $4$.\n\nThe quality of this floor plan is $4^2 + 2^2 + 0^2 + 0^2 + 4^2 = 36$. You can show that there is no floor plan with a higher quality."} +{"id": "01140", "text": "Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number b_{i}. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!\n\nYour task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. \n\n\n-----Input-----\n\nThe first line of the input contains n (2 \u2264 n \u2264 2\u00b710^5). In the next line there are n space-separated integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 10^9).\n\n\n-----Output-----\n\nThe only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n1 1\nInput\n3\n1 4 5\n\nOutput\n4 1\nInput\n5\n3 1 2 3 1\n\nOutput\n2 4\n\n\n-----Note-----\n\nIn the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers."} +{"id": "01141", "text": "Are you going to Scarborough Fair?\n\nParsley, sage, rosemary and thyme.\n\nRemember me to one who lives there.\n\nHe once was the true love of mine.\n\nWillem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.\n\nWillem asks his friend, Grick for directions, Grick helped them, and gave them a task.\n\nAlthough the girl wants to help, Willem insists on doing it by himself.\n\nGrick gave Willem a string of length n.\n\nWillem needs to do m operations, each operation has four parameters l, r, c_1, c_2, which means that all symbols c_1 in range [l, r] (from l-th to r-th, including l and r) are changed into c_2. String is 1-indexed.\n\nGrick wants to know the final string after all the m operations.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 100).\n\nThe second line contains a string s of length n, consisting of lowercase English letters.\n\nEach of the next m lines contains four parameters l, r, c_1, c_2 (1 \u2264 l \u2264 r \u2264 n, c_1, c_2 are lowercase English letters), separated by space.\n\n\n-----Output-----\n\nOutput string s after performing m operations described above.\n\n\n-----Examples-----\nInput\n3 1\nioi\n1 1 i n\n\nOutput\nnoi\nInput\n5 3\nwxhak\n3 3 h x\n1 5 x a\n1 3 w g\n\nOutput\ngaaak\n\n\n-----Note-----\n\nFor the second example:\n\nAfter the first operation, the string is wxxak.\n\nAfter the second operation, the string is waaak.\n\nAfter the third operation, the string is gaaak."} +{"id": "01142", "text": "Recently you've discovered a new shooter. They say it has realistic game mechanics.\n\nYour character has a gun with magazine size equal to $k$ and should exterminate $n$ waves of monsters. The $i$-th wave consists of $a_i$ monsters and happens from the $l_i$-th moment of time up to the $r_i$-th moments of time. All $a_i$ monsters spawn at moment $l_i$ and you have to exterminate all of them before the moment $r_i$ ends (you can kill monsters right at moment $r_i$). For every two consecutive waves, the second wave starts not earlier than the first wave ends (though the second wave can start at the same moment when the first wave ends) \u2014 formally, the condition $r_i \\le l_{i + 1}$ holds. Take a look at the notes for the examples to understand the process better.\n\nYou are confident in yours and your character's skills so you can assume that aiming and shooting are instant and you need exactly one bullet to kill one monster. But reloading takes exactly $1$ unit of time.\n\nOne of the realistic mechanics is a mechanic of reloading: when you reload you throw away the old magazine with all remaining bullets in it. That's why constant reloads may cost you excessive amounts of spent bullets.\n\nYou've taken a liking to this mechanic so now you are wondering: what is the minimum possible number of bullets you need to spend (both used and thrown) to exterminate all waves.\n\nNote that you don't throw the remaining bullets away after eradicating all monsters, and you start with a full magazine.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n \\le 2000$; $1 \\le k \\le 10^9$)\u00a0\u2014 the number of waves and magazine size.\n\nThe next $n$ lines contain descriptions of waves. The $i$-th line contains three integers $l_i$, $r_i$ and $a_i$ ($1 \\le l_i \\le r_i \\le 10^9$; $1 \\le a_i \\le 10^9$)\u00a0\u2014 the period of time when the $i$-th wave happens and the number of monsters in it.\n\nIt's guaranteed that waves don't overlap (but may touch) and are given in the order they occur, i. e. $r_i \\le l_{i + 1}$.\n\n\n-----Output-----\n\nIf there is no way to clear all waves, print $-1$. Otherwise, print the minimum possible number of bullets you need to spend (both used and thrown) to clear all waves.\n\n\n-----Examples-----\nInput\n2 3\n2 3 6\n3 4 3\n\nOutput\n9\n\nInput\n2 5\n3 7 11\n10 12 15\n\nOutput\n30\n\nInput\n5 42\n42 42 42\n42 43 42\n43 44 42\n44 45 42\n45 45 1\n\nOutput\n-1\n\nInput\n1 10\n100 111 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example: At the moment $2$, the first wave occurs and $6$ monsters spawn. You kill $3$ monsters and start reloading. At the moment $3$, the second wave occurs and $3$ more monsters spawn. You kill remaining $3$ monsters from the first wave and start reloading. At the moment $4$, you kill remaining $3$ monsters from the second wave. In total, you'll spend $9$ bullets.\n\nIn the second example: At moment $3$, the first wave occurs and $11$ monsters spawn. You kill $5$ monsters and start reloading. At moment $4$, you kill $5$ more monsters and start reloading. At moment $5$, you kill the last monster and start reloading throwing away old magazine with $4$ bullets. At moment $10$, the second wave occurs and $15$ monsters spawn. You kill $5$ monsters and start reloading. At moment $11$, you kill $5$ more monsters and start reloading. At moment $12$, you kill last $5$ monsters. In total, you'll spend $30$ bullets."} +{"id": "01143", "text": "In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.\n\nFor example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.\n\nIn order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time.\n\n\n-----Input-----\n\nThe first line contains integer n \u2014 the number of Olympiads in 2013 (1 \u2264 n \u2264 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} \u2014 the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 \u2264 m_{i} \u2264 12, d_{i} \u2265 1, 1 \u2264 p_{i}, t_{i} \u2264 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day.\n\nUse the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum jury size.\n\n\n-----Examples-----\nInput\n2\n5 23 1 2\n3 13 2 3\n\nOutput\n2\n\nInput\n3\n12 9 2 1\n12 8 1 3\n12 8 2 2\n\nOutput\n3\n\nInput\n1\n1 10 1 13\n\nOutput\n1"} +{"id": "01144", "text": "Vasya wrote down two strings s of length n and t of length m consisting of small English letters 'a' and 'b'. What is more, he knows that string t has a form \"abab...\", namely there are letters 'a' on odd positions and letters 'b' on even positions.\n\nSuddenly in the morning, Vasya found that somebody spoiled his string. Some letters of the string s were replaced by character '?'.\n\nLet's call a sequence of positions i, i + 1, ..., i + m - 1 as occurrence of string t in s, if 1 \u2264 i \u2264 n - m + 1 and t_1 = s_{i}, t_2 = s_{i} + 1, ..., t_{m} = s_{i} + m - 1.\n\nThe boy defines the beauty of the string s as maximum number of disjoint occurrences of string t in s. Vasya can replace some letters '?' with 'a' or 'b' (letters on different positions can be replaced with different letter). Vasya wants to make some replacements in such a way that beauty of string s is maximum possible. From all such options, he wants to choose one with the minimum number of replacements. Find the number of replacements he should make.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the length of s.\n\nThe second line contains the string s of length n. It contains small English letters 'a', 'b' and characters '?' only.\n\nThe third line contains a single integer m (1 \u2264 m \u2264 10^5)\u00a0\u2014 the length of t. The string t contains letters 'a' on odd positions and 'b' on even positions.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the minimum number of replacements Vasya has to perform to make the beauty of string s the maximum possible.\n\n\n-----Examples-----\nInput\n5\nbb?a?\n1\n\nOutput\n2\n\nInput\n9\nab??ab???\n3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample string t has a form 'a'. The only optimal option is to replace all characters '?' by 'a'.\n\nIn the second sample using two replacements we can make string equal to \"aba?aba??\". It is impossible to get more than two occurrences."} +{"id": "01145", "text": "Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. \n\nFor every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors. \n\nColonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.\n\n\n-----Input-----\n\nFirst line of input consists of one integer n (1 \u2264 n \u2264 3000).\n\nNext line consists of n integers a_{i} (1 \u2264 a_{i} \u2264 n), which stand for coolness factor of each badge.\n\n\n-----Output-----\n\nOutput single integer \u2014 minimum amount of coins the colonel has to pay.\n\n\n-----Examples-----\nInput\n4\n1 3 1 4\n\nOutput\n1\nInput\n5\n1 2 3 2 5\n\nOutput\n2\n\n\n-----Note-----\n\nIn first sample test we can increase factor of first badge by 1.\n\nIn second sample test we can increase factors of the second and the third badge by 1."} +{"id": "01146", "text": "Vasya wants to turn on Christmas lights consisting of m bulbs. Initially, all bulbs are turned off. There are n buttons, each of them is connected to some set of bulbs. Vasya can press any of these buttons. When the button is pressed, it turns on all the bulbs it's connected to. Can Vasya light up all the bulbs?\n\nIf Vasya presses the button such that some bulbs connected to it are already turned on, they do not change their state, i.e. remain turned on.\n\n\n-----Input-----\n\nThe first line of the input contains integers n and m (1 \u2264 n, m \u2264 100)\u00a0\u2014 the number of buttons and the number of bulbs respectively. \n\nEach of the next n lines contains x_{i} (0 \u2264 x_{i} \u2264 m)\u00a0\u2014 the number of bulbs that are turned on by the i-th button, and then x_{i} numbers y_{ij} (1 \u2264 y_{ij} \u2264 m)\u00a0\u2014 the numbers of these bulbs.\n\n\n-----Output-----\n\nIf it's possible to turn on all m bulbs print \"YES\", otherwise print \"NO\".\n\n\n-----Examples-----\nInput\n3 4\n2 1 4\n3 1 3 1\n1 2\n\nOutput\nYES\n\nInput\n3 3\n1 1\n1 2\n1 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample you can press each button once and turn on all the bulbs. In the 2 sample it is impossible to turn on the 3-rd lamp."} +{"id": "01147", "text": "While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a and integer x. He should find the number of different ordered pairs of indexes (i, j) such that a_{i} \u2264 a_{j} and there are exactly k integers y such that a_{i} \u2264 y \u2264 a_{j} and y is divisible by x.\n\nIn this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1).\n\n\n-----Input-----\n\nThe first line contains 3 integers n, x, k (1 \u2264 n \u2264 10^5, 1 \u2264 x \u2264 10^9, 0 \u2264 k \u2264 10^9), where n is the size of the array a and x and k are numbers from the statement.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the elements of the array a.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n4 2 1\n1 3 5 7\n\nOutput\n3\n\nInput\n4 2 0\n5 3 1 7\n\nOutput\n4\n\nInput\n5 3 1\n3 3 3 3 3\n\nOutput\n25\n\n\n\n-----Note-----\n\nIn first sample there are only three suitable pairs of indexes\u00a0\u2014 (1, 2), (2, 3), (3, 4).\n\nIn second sample there are four suitable pairs of indexes(1, 1), (2, 2), (3, 3), (4, 4).\n\nIn third sample every pair (i, j) is suitable, so the answer is 5 * 5 = 25."} +{"id": "01148", "text": "Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains a_{i} liters of paint of color i.\n\nVika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1 \u00d7 1. Squares are numbered 1, 2, 3 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color x, then the next square will be painted in color x + 1. In case of x = n, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops.\n\nSquare is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the number of jars with colors Vika has.\n\nThe second line of the input contains a sequence of integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9), where a_{i} is equal to the number of liters of paint in the i-th jar, i.e. the number of liters of color i that Vika has.\n\n\n-----Output-----\n\nThe only line of the output should contain a single integer\u00a0\u2014 the maximum number of squares that Vika can paint if she follows the rules described above.\n\n\n-----Examples-----\nInput\n5\n2 4 2 3 3\n\nOutput\n12\n\nInput\n3\n5 5 5\n\nOutput\n15\n\nInput\n6\n10 10 10 1 10 10\n\nOutput\n11\n\n\n\n-----Note-----\n\nIn the first sample the best strategy is to start painting using color 4. Then the squares will be painted in the following colors (from left to right): 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.\n\nIn the second sample Vika can start to paint using any color.\n\nIn the third sample Vika should start painting using color number 5."} +{"id": "01149", "text": "There is a game called \"I Wanna Be the Guy\", consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.\n\nLittle X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other?\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100). \n\nThe next line contains an integer p (0 \u2264 p \u2264 n) at first, then follows p distinct integers a_1, a_2, ..., a_{p} (1 \u2264 a_{i} \u2264 n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It's assumed that levels are numbered from 1 to n.\n\n\n-----Output-----\n\nIf they can pass all the levels, print \"I become the guy.\". If it's impossible, print \"Oh, my keyboard!\" (without the quotes).\n\n\n-----Examples-----\nInput\n4\n3 1 2 3\n2 2 4\n\nOutput\nI become the guy.\n\nInput\n4\n3 1 2 3\n2 2 3\n\nOutput\nOh, my keyboard!\n\n\n\n-----Note-----\n\nIn the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both.\n\nIn the second sample, no one can pass level 4."} +{"id": "01150", "text": "Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.\n\nInitially, each mole i (1 \u2264 i \u2264 4n) is placed at some position (x_{i}, y_{i}) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible.\n\nEach mole i has a home placed at the position (a_{i}, b_{i}). Moving this mole one time means rotating his position point (x_{i}, y_{i}) 90 degrees counter-clockwise around it's home point (a_{i}, b_{i}).\n\nA regiment is compact only if the position points of the 4 moles form a square with non-zero area.\n\nHelp Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 100), the number of regiments.\n\nThe next 4n lines contain 4 integers x_{i}, y_{i}, a_{i}, b_{i} ( - 10^4 \u2264 x_{i}, y_{i}, a_{i}, b_{i} \u2264 10^4).\n\n\n-----Output-----\n\nPrint n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print \"-1\" (without quotes).\n\n\n-----Examples-----\nInput\n4\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-2 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n-1 1 0 0\n2 2 0 1\n-1 0 0 -2\n3 0 0 -2\n-1 1 -2 0\n\nOutput\n1\n-1\n3\n3\n\n\n\n-----Note-----\n\nIn the first regiment we can move once the second or the third mole.\n\nWe can't make the second regiment compact.\n\nIn the third regiment, from the last 3 moles we can move once one and twice another one.\n\nIn the fourth regiment, we can move twice the first mole and once the third mole."} +{"id": "01151", "text": "An atom of element X can exist in n distinct states with energies E_1 < E_2 < ... < E_{n}. Arkady wants to build a laser on this element, using a three-level scheme. Here is a simplified description of the scheme. \n\nThree distinct states i, j and k are selected, where i < j < k. After that the following process happens: initially the atom is in the state i, we spend E_{k} - E_{i} energy to put the atom in the state k, the atom emits a photon with useful energy E_{k} - E_{j} and changes its state to the state j, the atom spontaneously changes its state to the state i, losing energy E_{j} - E_{i}, the process repeats from step 1. \n\nLet's define the energy conversion efficiency as $\\eta = \\frac{E_{k} - E_{j}}{E_{k} - E_{i}}$, i.\u00a0e. the ration between the useful energy of the photon and spent energy.\n\nDue to some limitations, Arkady can only choose such three states that E_{k} - E_{i} \u2264 U.\n\nHelp Arkady to find such the maximum possible energy conversion efficiency within the above constraints.\n\n\n-----Input-----\n\nThe first line contains two integers n and U (3 \u2264 n \u2264 10^5, 1 \u2264 U \u2264 10^9) \u2014 the number of states and the maximum possible difference between E_{k} and E_{i}.\n\nThe second line contains a sequence of integers E_1, E_2, ..., E_{n} (1 \u2264 E_1 < E_2... < E_{n} \u2264 10^9). It is guaranteed that all E_{i} are given in increasing order.\n\n\n-----Output-----\n\nIf it is not possible to choose three states that satisfy all constraints, print -1.\n\nOtherwise, print one real number \u03b7\u00a0\u2014 the maximum possible energy conversion efficiency. Your answer is considered correct its absolute or relative error does not exceed 10^{ - 9}.\n\nFormally, 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}$.\n\n\n-----Examples-----\nInput\n4 4\n1 3 5 7\n\nOutput\n0.5\n\nInput\n10 8\n10 13 15 16 17 19 20 22 24 25\n\nOutput\n0.875\n\nInput\n3 1\n2 5 10\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example choose states 1, 2 and 3, so that the energy conversion efficiency becomes equal to $\\eta = \\frac{5 - 3}{5 - 1} = 0.5$.\n\nIn the second example choose states 4, 5 and 9, so that the energy conversion efficiency becomes equal to $\\eta = \\frac{24 - 17}{24 - 16} = 0.875$."} +{"id": "01152", "text": "Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task.\n\nYou are given two matrices $A$ and $B$ of size $n \\times m$, each of which consists of $0$ and $1$ only. You can apply the following operation to the matrix $A$ arbitrary number of times: take any submatrix of the matrix $A$ that has at least two rows and two columns, and invert the values in its corners (i.e. all corners of the submatrix that contain $0$, will be replaced by $1$, and all corners of the submatrix that contain $1$, will be replaced by $0$). You have to answer whether you can obtain the matrix $B$ from the matrix $A$. [Image] An example of the operation. The chosen submatrix is shown in blue and yellow, its corners are shown in yellow. \n\nRamesses don't want to perform these operations by himself, so he asks you to answer this question.\n\nA submatrix of matrix $M$ is a matrix which consist of all elements which come from one of the rows with indices $x_1, x_1+1, \\ldots, x_2$ of matrix $M$ and one of the columns with indices $y_1, y_1+1, \\ldots, y_2$ of matrix $M$, where $x_1, x_2, y_1, y_2$ are the edge rows and columns of the submatrix. In other words, a submatrix is a set of elements of source matrix which form a solid rectangle (i.e. without holes) with sides parallel to the sides of the original matrix. The corners of the submatrix are cells $(x_1, y_1)$, $(x_1, y_2)$, $(x_2, y_1)$, $(x_2, y_2)$, where the cell $(i,j)$ denotes the cell on the intersection of the $i$-th row and the $j$-th column.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\leq n, m \\leq 500$)\u00a0\u2014 the number of rows and the number of columns in matrices $A$ and $B$.\n\nEach of the next $n$ lines contain $m$ integers: the $j$-th integer in the $i$-th line is the $j$-th element of the $i$-th row of the matrix $A$ ($0 \\leq A_{ij} \\leq 1$). \n\nEach of the next $n$ lines contain $m$ integers: the $j$-th integer in the $i$-th line is the $j$-th element of the $i$-th row of the matrix $B$ ($0 \\leq B_{ij} \\leq 1$). \n\n\n-----Output-----\n\nPrint \"Yes\" (without quotes) if it is possible to transform the matrix $A$ to the matrix $B$ using the operations described above, and \"No\" (without quotes), if it is not possible. You can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n3 3\n0 1 0\n0 1 0\n1 0 0\n1 0 0\n1 0 0\n1 0 0\n\nOutput\nYes\n\nInput\n6 7\n0 0 1 1 0 0 1\n0 1 0 0 1 0 1\n0 0 0 1 0 0 1\n1 0 1 0 1 0 0\n0 1 0 0 1 0 1\n0 1 0 1 0 0 1\n1 1 0 1 0 1 1\n0 1 1 0 1 0 0\n1 1 0 1 0 0 1\n1 0 1 0 0 1 0\n0 1 1 0 1 0 0\n0 1 1 1 1 0 1\n\nOutput\nYes\n\nInput\n3 4\n0 1 0 1\n1 0 1 0\n0 1 0 1\n1 1 1 1\n1 1 1 1\n1 1 1 1\n\nOutput\nNo\n\n\n\n-----Note-----\n\nThe examples are explained below. [Image] Example 1. [Image] Example 2. [Image] Example 3."} +{"id": "01153", "text": "Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.\n\nZhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l_1, l_2, ..., l_{k} bytes, then the i-th file is split to one or more blocks b_{i}, 1, b_{i}, 2, ..., b_{i}, m_{i} (here the total length of the blocks b_{i}, 1 + b_{i}, 2 + ... + b_{i}, m_{i} is equal to the length of the file l_{i}), and after that all blocks are transferred through the network, maintaining the order of files in the archive.\n\nZhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.\n\nYou are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 10^5) \u2014 the number of blocks in the first and in the second messages.\n\nThe second line contains n integers x_1, x_2, ..., x_{n} (1 \u2264 x_{i} \u2264 10^6) \u2014 the length of the blocks that form the first message.\n\nThe third line contains m integers y_1, y_2, ..., y_{m} (1 \u2264 y_{i} \u2264 10^6) \u2014 the length of the blocks that form the second message.\n\nIt is guaranteed that x_1 + ... + x_{n} = y_1 + ... + y_{m}. Also, it is guaranteed that x_1 + ... + x_{n} \u2264 10^6.\n\n\n-----Output-----\n\nPrint the maximum number of files the intercepted array could consist of.\n\n\n-----Examples-----\nInput\n7 6\n2 5 3 1 11 4 4\n7 8 2 4 1 8\n\nOutput\n3\n\nInput\n3 3\n1 10 100\n1 100 10\n\nOutput\n2\n\nInput\n1 4\n4\n1 1 1 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.\n\nIn the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.\n\nIn the third example the only possibility is that the archive contains a single file of size 4."} +{"id": "01154", "text": "Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.\n\nVanya has n pieces of potato, the height of the i-th piece is equal to a_{i}. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens:\n\n If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). \n\nProvided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed.\n\n\n-----Input-----\n\nThe first line of the input contains integers n, h and k (1 \u2264 n \u2264 100 000, 1 \u2264 k \u2264 h \u2264 10^9)\u00a0\u2014 the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 h)\u00a0\u2014 the heights of the pieces.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of seconds required to smash all the potatoes following the process described in the problem statement.\n\n\n-----Examples-----\nInput\n5 6 3\n5 4 3 2 1\n\nOutput\n5\n\nInput\n5 6 3\n5 5 5 5 5\n\nOutput\n10\n\nInput\n5 6 3\n1 2 1 1 1\n\nOutput\n2\n\n\n\n-----Note-----\n\nConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. \n\nIn the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2\u00b75 = 10 seconds.\n\nIn the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds."} +{"id": "01155", "text": "We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that $a$ yuan for $b$ kilos (You don't need to care about what \"yuan\" is), the same as $a/b$ yuan for a kilo.\n\nNow imagine you'd like to buy $m$ kilos of apples. You've asked $n$ supermarkets and got the prices. Find the minimum cost for those apples.\n\nYou can assume that there are enough apples in all supermarkets.\n\n\n-----Input-----\n\nThe first line contains two positive integers $n$ and $m$ ($1 \\leq n \\leq 5\\,000$, $1 \\leq m \\leq 100$), denoting that there are $n$ supermarkets and you want to buy $m$ kilos of apples.\n\nThe following $n$ lines describe the information of the supermarkets. Each line contains two positive integers $a, b$ ($1 \\leq a, b \\leq 100$), denoting that in this supermarket, you are supposed to pay $a$ yuan for $b$ kilos of apples.\n\n\n-----Output-----\n\nThe only line, denoting the minimum cost for $m$ kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed $10^{-6}$.\n\nFormally, let your answer be $x$, and the jury's answer be $y$. Your answer is considered correct if $\\frac{|x - y|}{\\max{(1, |y|)}} \\le 10^{-6}$.\n\n\n-----Examples-----\nInput\n3 5\n1 2\n3 4\n1 3\n\nOutput\n1.66666667\n\nInput\n2 1\n99 100\n98 99\n\nOutput\n0.98989899\n\n\n\n-----Note-----\n\nIn the first sample, you are supposed to buy $5$ kilos of apples in supermarket $3$. The cost is $5/3$ yuan.\n\nIn the second sample, you are supposed to buy $1$ kilo of apples in supermarket $2$. The cost is $98/99$ yuan."} +{"id": "01156", "text": "\"We've tried solitary confinement, waterboarding and listening to Just In Beaver, to no avail. We need something extreme.\"\n\n\"Little Alena got an array as a birthday present...\"\n\nThe array b of length n is obtained from the array a of length n and two integers l and r\u00a0(l \u2264 r) using the following procedure:\n\nb_1 = b_2 = b_3 = b_4 = 0.\n\nFor all 5 \u2264 i \u2264 n: b_{i} = 0 if a_{i}, a_{i} - 1, a_{i} - 2, a_{i} - 3, a_{i} - 4 > r and b_{i} - 1 = b_{i} - 2 = b_{i} - 3 = b_{i} - 4 = 1 b_{i} = 1 if a_{i}, a_{i} - 1, a_{i} - 2, a_{i} - 3, a_{i} - 4 < l and b_{i} - 1 = b_{i} - 2 = b_{i} - 3 = b_{i} - 4 = 0 b_{i} = b_{i} - 1 otherwise \n\nYou are given arrays a and b' of the same length. Find two integers l and r\u00a0(l \u2264 r), such that applying the algorithm described above will yield an array b equal to b'.\n\nIt's guaranteed that the answer exists.\n\n\n-----Input-----\n\nThe first line of input contains a single integer n (5 \u2264 n \u2264 10^5)\u00a0\u2014 the length of a and b'.\n\nThe second line of input contains n space separated integers a_1, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the elements of a.\n\nThe third line of input contains a string of n characters, consisting of 0 and 1\u00a0\u2014 the elements of b'. Note that they are not separated by spaces.\n\n\n-----Output-----\n\nOutput two integers l and r\u00a0( - 10^9 \u2264 l \u2264 r \u2264 10^9), conforming to the requirements described above.\n\nIf there are multiple solutions, output any of them.\n\nIt's guaranteed that the answer exists.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n00001\n\nOutput\n6 15\n\nInput\n10\n-10 -9 -8 -7 -6 6 7 8 9 10\n0000111110\n\nOutput\n-5 5\n\n\n\n-----Note-----\n\nIn the first test case any pair of l and r pair is valid, if 6 \u2264 l \u2264 r \u2264 10^9, in that case b_5 = 1, because a_1, ..., a_5 < l."} +{"id": "01157", "text": "You are given a sequence $a_1, a_2, \\dots, a_n$ consisting of $n$ non-zero integers (i.e. $a_i \\ne 0$). \n\nYou have to calculate two following values: the number of pairs of indices $(l, r)$ $(l \\le r)$ such that $a_l \\cdot a_{l + 1} \\dots a_{r - 1} \\cdot a_r$ is negative; the number of pairs of indices $(l, r)$ $(l \\le r)$ such that $a_l \\cdot a_{l + 1} \\dots a_{r - 1} \\cdot a_r$ is positive; \n\n\n-----Input-----\n\nThe first line contains one integer $n$ $(1 \\le n \\le 2 \\cdot 10^{5})$ \u2014 the number of elements in the sequence.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ $(-10^{9} \\le a_i \\le 10^{9}; a_i \\neq 0)$ \u2014 the elements of the sequence.\n\n\n-----Output-----\n\nPrint two integers \u2014 the number of subsegments with negative product and the number of subsegments with positive product, respectively.\n\n\n-----Examples-----\nInput\n5\n5 -3 3 -1 1\n\nOutput\n8 7\n\nInput\n10\n4 2 -4 3 1 2 -4 3 2 3\n\nOutput\n28 27\n\nInput\n5\n-1 -2 -3 -4 -5\n\nOutput\n9 6"} +{"id": "01158", "text": "The king's birthday dinner was attended by $k$ guests. The dinner was quite a success: every person has eaten several dishes (though the number of dishes was the same for every person) and every dish was served alongside with a new set of kitchen utensils.\n\nAll types of utensils in the kingdom are numbered from $1$ to $100$. It is known that every set of utensils is the same and consist of different types of utensils, although every particular type may appear in the set at most once. For example, a valid set of utensils can be composed of one fork, one spoon and one knife.\n\nAfter the dinner was over and the guests were dismissed, the king wondered what minimum possible number of utensils could be stolen. Unfortunately, the king has forgotten how many dishes have been served for every guest but he knows the list of all the utensils left after the dinner. Your task is to find the minimum possible number of stolen utensils.\n\n\n-----Input-----\n\nThe first line contains two integer numbers $n$ and $k$ ($1 \\le n \\le 100, 1 \\le k \\le 100$) \u00a0\u2014 the number of kitchen utensils remaining after the dinner and the number of guests correspondingly.\n\nThe next line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 100$) \u00a0\u2014 the types of the utensils remaining. Equal values stand for identical utensils while different values stand for different utensils.\n\n\n-----Output-----\n\nOutput a single value \u2014 the minimum number of utensils that could be stolen by the guests.\n\n\n-----Examples-----\nInput\n5 2\n1 2 2 1 3\n\nOutput\n1\n\nInput\n10 3\n1 3 3 1 3 5 5 5 5 100\n\nOutput\n14\n\n\n\n-----Note-----\n\nIn the first example it is clear that at least one utensil of type $3$ has been stolen, since there are two guests and only one such utensil. But it is also possible that every person received only one dish and there were only six utensils in total, when every person got a set $(1, 2, 3)$ of utensils. Therefore, the answer is $1$.\n\nOne can show that in the second example at least $2$ dishes should have been served for every guest, so the number of utensils should be at least $24$: every set contains $4$ utensils and every one of the $3$ guests gets two such sets. Therefore, at least $14$ objects have been stolen. Please note that utensils of some types (for example, of types $2$ and $4$ in this example) may be not present in the set served for dishes."} +{"id": "01159", "text": "Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but couldn't think of anything inventive. Hence, he will be giving her a graph. How original, Bob! Alice will surely be thrilled!\n\nWhen building the graph, he needs four conditions to be satisfied: It must be a simple undirected graph, i.e. without multiple (parallel) edges and self-loops. The number of vertices must be exactly $n$\u00a0\u2014 a number he selected. This number is not necessarily prime. The total number of edges must be prime. The degree (i.e. the number of edges connected to the vertex) of each vertex must be prime. \n\nBelow is an example for $n = 4$. The first graph (left one) is invalid as the degree of vertex $2$ (and $4$) equals to $1$, which is not prime. The second graph (middle one) is invalid as the total number of edges is $4$, which is not a prime number. The third graph (right one) is a valid answer for $n = 4$. [Image] \n\nNote that the graph can be disconnected.\n\nPlease help Bob to find any such graph!\n\n\n-----Input-----\n\nThe input consists of a single integer $n$ ($3 \\leq n \\leq 1\\,000$)\u00a0\u2014 the number of vertices.\n\n\n-----Output-----\n\nIf there is no graph satisfying the conditions, print a single line containing the integer $-1$.\n\nOtherwise, first print a line containing a prime number $m$ ($2 \\leq m \\leq \\frac{n(n-1)}{2}$)\u00a0\u2014 the number of edges in the graph. Then, print $m$ lines, the $i$-th of which containing two integers $u_i$, $v_i$ ($1 \\leq u_i, v_i \\leq n$)\u00a0\u2014 meaning that there is an edge between vertices $u_i$ and $v_i$. The degree of each vertex must be prime. There must be no multiple (parallel) edges or self-loops.\n\nIf there are multiple solutions, you may print any of them.\n\nNote that the graph can be disconnected.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n5\n1 2\n1 3\n2 3\n2 4\n3 4\nInput\n8\n\nOutput\n13\n1 2\n1 3\n2 3\n1 4\n2 4\n1 5\n2 5\n1 6\n2 6\n1 7\n1 8\n5 8\n7 8\n\n\n\n-----Note-----\n\nThe first example was described in the statement.\n\nIn the second example, the degrees of vertices are $[7, 5, 2, 2, 3, 2, 2, 3]$. Each of these numbers is prime. Additionally, the number of edges, $13$, is also a prime number, hence both conditions are satisfied. [Image]"} +{"id": "01160", "text": "The organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXL you are given the number of t-shirts of this size.\n\nDuring the registration, the organizers asked each of the n participants about the t-shirt size he wants. If a participant hesitated between two sizes, he could specify two neighboring sizes\u00a0\u2014 this means that any of these two sizes suits him.\n\nWrite a program that will determine whether it is possible to present a t-shirt to each participant of the competition, or not. Of course, each participant should get a t-shirt of proper size: the size he wanted, if he specified one size; any of the two neibouring sizes, if he specified two sizes. \n\nIf it is possible, the program should find any valid distribution of the t-shirts.\n\n\n-----Input-----\n\nThe first line of the input contains six non-negative integers\u00a0\u2014 the number of t-shirts of each size. The numbers are given for the sizes S, M, L, XL, XXL, XXXL, respectively. The total number of t-shirts doesn't exceed 100 000.\n\nThe second line contains positive integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of participants.\n\nThe following n lines contain the sizes specified by the participants, one line per participant. The i-th line contains information provided by the i-th participant: single size or two sizes separated by comma (without any spaces). If there are two sizes, the sizes are written in increasing order. It is guaranteed that two sizes separated by comma are neighboring.\n\n\n-----Output-----\n\nIf it is not possible to present a t-shirt to each participant, print \u00abNO\u00bb (without quotes).\n\nOtherwise, print n + 1 lines. In the first line print \u00abYES\u00bb (without quotes). In the following n lines print the t-shirt sizes the orginizers should give to participants, one per line. The order of the participants should be the same as in the input.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n0 1 0 1 1 0\n3\nXL\nS,M\nXL,XXL\n\nOutput\nYES\nXL\nM\nXXL\n\nInput\n1 1 2 0 1 1\n5\nS\nM\nS,M\nXXL,XXXL\nXL,XXL\n\nOutput\nNO"} +{"id": "01161", "text": "You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.\n\nThe following definition of a regular bracket sequence is well-known, so you can be familiar with it.\n\nLet's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.\n\nFor example the string \"[[(){}]<>]\" is RBS, but the strings \"[)()\" and \"][()()\" are not.\n\nDetermine the least number of replaces to make the string s RBS.\n\n\n-----Input-----\n\nThe only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.\n\n\n-----Output-----\n\nIf it's impossible to get RBS from s print Impossible.\n\nOtherwise print the least number of replaces needed to get RBS from s.\n\n\n-----Examples-----\nInput\n[<}){}\n\nOutput\n2\nInput\n{()}[]\n\nOutput\n0\nInput\n]]\n\nOutput\nImpossible"} +{"id": "01162", "text": "Hasan loves playing games and has recently discovered a game called TopScore. In this soccer-like game there are $p$ players doing penalty shoot-outs. Winner is the one who scores the most. In case of ties, one of the top-scorers will be declared as the winner randomly with equal probability.\n\nThey have just finished the game and now are waiting for the result. But there's a tiny problem! The judges have lost the paper of scores! Fortunately they have calculated sum of the scores before they get lost and also for some of the players they have remembered a lower bound on how much they scored. However, the information about the bounds is private, so Hasan only got to know his bound.\n\nAccording to the available data, he knows that his score is at least $r$ and sum of the scores is $s$.\n\nThus the final state of the game can be represented in form of sequence of $p$ integers $a_1, a_2, \\dots, a_p$ ($0 \\le a_i$) \u2014 player's scores. Hasan is player number $1$, so $a_1 \\ge r$. Also $a_1 + a_2 + \\dots + a_p = s$. Two states are considered different if there exists some position $i$ such that the value of $a_i$ differs in these states. \n\nOnce again, Hasan doesn't know the exact scores (he doesn't know his exact score as well). So he considers each of the final states to be equally probable to achieve.\n\nHelp Hasan find the probability of him winning.\n\nIt can be shown that it is in the form of $\\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \\ne 0$, $P \\le Q$. Report the value of $P \\cdot Q^{-1} \\pmod {998244353}$.\n\n\n-----Input-----\n\nThe only line contains three integers $p$, $s$ and $r$ ($1 \\le p \\le 100$, $0 \\le r \\le s \\le 5000$) \u2014 the number of players, the sum of scores of all players and Hasan's score, respectively.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the probability of Hasan winning.\n\nIt can be shown that it is in the form of $\\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \\ne 0$, $P \\le Q$. Report the value of $P \\cdot Q^{-1} \\pmod {998244353}$.\n\n\n-----Examples-----\nInput\n2 6 3\n\nOutput\n124780545\n\nInput\n5 20 11\n\nOutput\n1\n\nInput\n10 30 10\n\nOutput\n85932500\n\n\n\n-----Note-----\n\nIn the first example Hasan can score $3$, $4$, $5$ or $6$ goals. If he scores $4$ goals or more than he scores strictly more than his only opponent. If he scores $3$ then his opponent also scores $3$ and Hasan has a probability of $\\frac 1 2$ to win the game. Thus, overall he has the probability of $\\frac 7 8$ to win.\n\nIn the second example even Hasan's lower bound on goal implies him scoring more than any of his opponents. Thus, the resulting probability is $1$."} +{"id": "01163", "text": "There are n boys and m girls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers from 1 to n + m. Then the number of integers i (1 \u2264 i < n + m) such that positions with indexes i and i + 1 contain children of different genders (position i has a girl and position i + 1 has a boy or vice versa) must be as large as possible. \n\nHelp the children and tell them how to form the line.\n\n\n-----Input-----\n\nThe single line of the input contains two integers n and m (1 \u2264 n, m \u2264 100), separated by a space.\n\n\n-----Output-----\n\nPrint a line of n + m characters. Print on the i-th position of the line character \"B\", if the i-th position of your arrangement should have a boy and \"G\", if it should have a girl. \n\nOf course, the number of characters \"B\" should equal n and the number of characters \"G\" should equal m. If there are multiple optimal solutions, print any of them.\n\n\n-----Examples-----\nInput\n3 3\n\nOutput\nGBGBGB\n\nInput\n4 2\n\nOutput\nBGBGBB\n\n\n\n-----Note-----\n\nIn the first sample another possible answer is BGBGBG. \n\nIn the second sample answer BBGBGB is also optimal."} +{"id": "01164", "text": "Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format \"name_1price_1name_2price_2...name_{n}price_{n}\", where name_{i} (name of the i-th purchase) is a non-empty string of length not more than 10, consisting of lowercase English letters, and price_{i} (the price of the i-th purchase) is a non-empty string, consisting of digits and dots (decimal points). It is possible that purchases with equal names have different prices.\n\nThe price of each purchase is written in the following format. If the price is an integer number of dollars then cents are not written.\n\nOtherwise, after the number of dollars a dot (decimal point) is written followed by cents in a two-digit format (if number of cents is between 1 and 9 inclusively, there is a leading zero).\n\nAlso, every three digits (from less significant to the most) in dollars are separated by dot (decimal point). No extra leading zeroes are allowed. The price always starts with a digit and ends with a digit.\n\nFor example: \"234\", \"1.544\", \"149.431.10\", \"0.99\" and \"123.05\" are valid prices, \".333\", \"3.33.11\", \"12.00\", \".33\", \"0.1234\" and \"1.2\" are not valid. \n\nWrite a program that will find the total price of all purchases in the given bill.\n\n\n-----Input-----\n\nThe only line of the input contains a non-empty string s with length not greater than 1000\u00a0\u2014 the content of the bill.\n\nIt is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 10^6 dollars.\n\n\n-----Output-----\n\nPrint the total price exactly in the same format as prices given in the input.\n\n\n-----Examples-----\nInput\nchipsy48.32televizor12.390\n\nOutput\n12.438.32\n\nInput\na1b2c3.38\n\nOutput\n6.38\n\nInput\naa0.01t0.03\n\nOutput\n0.04"} +{"id": "01165", "text": "You are given array a with n integers and m queries. The i-th query is given with three integers l_{i}, r_{i}, x_{i}.\n\nFor the i-th query find any position p_{i} (l_{i} \u2264 p_{i} \u2264 r_{i}) so that a_{p}_{i} \u2260 x_{i}.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 2\u00b710^5) \u2014 the number of elements in a and the number of queries.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^6) \u2014 the elements of the array a.\n\nEach of the next m lines contains three integers l_{i}, r_{i}, x_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n, 1 \u2264 x_{i} \u2264 10^6) \u2014 the parameters of the i-th query.\n\n\n-----Output-----\n\nPrint m lines. On the i-th line print integer p_{i} \u2014 the position of any number not equal to x_{i} in segment [l_{i}, r_{i}] or the value - 1 if there is no such number.\n\n\n-----Examples-----\nInput\n6 4\n1 2 1 1 3 5\n1 4 1\n2 6 2\n3 4 1\n3 4 2\n\nOutput\n2\n6\n-1\n4"} +{"id": "01166", "text": "After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. \n\nA token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\\bmod a_i = 0$). \n\nWhoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the number of numbers.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le n$). Furthermore, there are no pair of indices $i \\neq j$ such that $a_i = a_j$.\n\n\n-----Output-----\n\nPrint $s$\u00a0\u2014 a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to \"A\"; otherwise, $s_i$ has to be equal to \"B\". \n\n\n-----Examples-----\nInput\n8\n3 6 5 4 2 7 1 8\n\nOutput\nBAAAABAB\n\nInput\n15\n3 11 2 5 10 9 7 13 15 8 4 12 6 1 14\n\nOutput\nABAAAABBBAABAAB\n\n\n\n-----Note-----\n\nIn the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately."} +{"id": "01167", "text": "Tomorrow is a difficult day for Polycarp: he has to attend $a$ lectures and $b$ practical classes at the university! Since Polycarp is a diligent student, he is going to attend all of them.\n\nWhile preparing for the university, Polycarp wonders whether he can take enough writing implements to write all of the lectures and draw everything he has to during all of the practical classes. Polycarp writes lectures using a pen (he can't use a pencil to write lectures!); he can write down $c$ lectures using one pen, and after that it runs out of ink. During practical classes Polycarp draws blueprints with a pencil (he can't use a pen to draw blueprints!); one pencil is enough to draw all blueprints during $d$ practical classes, after which it is unusable.\n\nPolycarp's pencilcase can hold no more than $k$ writing implements, so if Polycarp wants to take $x$ pens and $y$ pencils, they will fit in the pencilcase if and only if $x + y \\le k$.\n\nNow Polycarp wants to know how many pens and pencils should he take. Help him to determine it, or tell that his pencilcase doesn't have enough room for all the implements he needs tomorrow!\n\nNote that you don't have to minimize the number of writing implements (though their total number must not exceed $k$).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases in the input. Then the test cases follow.\n\nEach test case is described by one line containing five integers $a$, $b$, $c$, $d$ and $k$, separated by spaces ($1 \\le a, b, c, d, k \\le 100$) \u2014 the number of lectures Polycarp has to attend, the number of practical classes Polycarp has to attend, the number of lectures which can be written down using one pen, the number of practical classes for which one pencil is enough, and the number of writing implements that can fit into Polycarp's pencilcase, respectively.\n\nIn hacks it is allowed to use only one test case in the input, so $t = 1$ should be satisfied.\n\n\n-----Output-----\n\nFor each test case, print the answer as follows:\n\nIf the pencilcase can't hold enough writing implements to use them during all lectures and practical classes, print one integer $-1$. Otherwise, print two non-negative integers $x$ and $y$ \u2014 the number of pens and pencils Polycarp should put in his pencilcase. If there are multiple answers, print any of them. Note that you don't have to minimize the number of writing implements (though their total number must not exceed $k$).\n\n\n-----Example-----\nInput\n3\n7 5 4 5 8\n7 5 4 5 2\n20 53 45 26 4\n\nOutput\n7 1\n-1\n1 3\n\n\n\n-----Note-----\n\nThere are many different answers for the first test case; $x = 7$, $y = 1$ is only one of them. For example, $x = 3$, $y = 1$ is also correct.\n\n$x = 1$, $y = 3$ is the only correct answer for the third test case."} +{"id": "01168", "text": "Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :)\n\nRick and Morty like to go to the ridge High Cry for crying loudly\u00a0\u2014 there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this ridge: if Rick and Morty begin crying simultaneously from different mountains, their cry would be heard between these mountains up to the height equal the bitwise OR of mountains they've climbed and all the mountains between them. \n\nBitwise OR is a binary operation which is determined the following way. Consider representation of numbers x and y in binary numeric system (probably with leading zeroes) x = x_{k}... x_1x_0 and y = y_{k}... y_1y_0. Then z = x\u00a0|\u00a0y is defined following way: z = z_{k}... z_1z_0, where z_{i} = 1, if x_{i} = 1 or y_{i} = 1, and z_{i} = 0 otherwise. In the other words, digit of bitwise OR of two numbers equals zero if and only if digits at corresponding positions is both numbers equals zero. For example bitwise OR of numbers 10 = 1010_2 and 9 = 1001_2 equals 11 = 1011_2. In programming languages C/C++/Java/Python this operation is defined as \u00ab|\u00bb, and in Pascal as \u00abor\u00bb.\n\nHelp Rick and Morty calculate the number of ways they can select two mountains in such a way that if they start crying from these mountains their cry will be heard above these mountains and all mountains between them. More formally you should find number of pairs l and r (1 \u2264 l < r \u2264 n) such that bitwise OR of heights of all mountains between l and r (inclusive) is larger than the height of any mountain at this interval.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 200 000), the number of mountains in the ridge.\n\nSecond line contains n integers a_{i} (0 \u2264 a_{i} \u2264 10^9), the heights of mountains in order they are located in the ridge.\n\n\n-----Output-----\n\nPrint the only integer, the number of ways to choose two different mountains.\n\n\n-----Examples-----\nInput\n5\n3 2 1 6 5\n\nOutput\n8\n\nInput\n4\n3 3 3 3\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test case all the ways are pairs of mountains with the numbers (numbering from one):(1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)\n\nIn the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain."} +{"id": "01169", "text": "Vasya has got an undirected graph consisting of $n$ vertices and $m$ edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges $(1, 2)$ and $(2, 1)$ is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.\n\nVasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of $n$ vertices and $m$ edges. \n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $m~(1 \\le n \\le 10^5, 0 \\le m \\le \\frac{n (n - 1)}{2})$.\n\nIt is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.\n\n\n-----Output-----\n\nIn the only line print two numbers $min$ and $max$ \u2014 the minimum and maximum number of isolated vertices, respectively.\n\n\n-----Examples-----\nInput\n4 2\n\nOutput\n0 1\n\nInput\n3 1\n\nOutput\n1 1\n\n\n\n-----Note-----\n\nIn the first example it is possible to construct a graph with $0$ isolated vertices: for example, it should contain edges $(1, 2)$ and $(3, 4)$. To get one isolated vertex, we may construct a graph with edges $(1, 2)$ and $(1, 3)$. \n\nIn the second example the graph will always contain exactly one isolated vertex."} +{"id": "01170", "text": "Let's denote a m-free matrix as a binary (that is, consisting of only 1's and 0's) matrix such that every square submatrix of size m \u00d7 m of this matrix contains at least one zero. \n\nConsider the following problem:\n\nYou are given two integers n and m. You have to construct an m-free square matrix of size n \u00d7 n such that the number of 1's in this matrix is maximum possible. Print the maximum possible number of 1's in such matrix.\n\nYou don't have to solve this problem. Instead, you have to construct a few tests for it.\n\nYou will be given t numbers x_1, x_2, ..., x_{t}. For every $i \\in [ 1, t ]$, find two integers n_{i} and m_{i} (n_{i} \u2265 m_{i}) such that the answer for the aforementioned problem is exactly x_{i} if we set n = n_{i} and m = m_{i}.\n\n\n-----Input-----\n\nThe first line contains one integer t (1 \u2264 t \u2264 100) \u2014 the number of tests you have to construct.\n\nThen t lines follow, i-th line containing one integer x_{i} (0 \u2264 x_{i} \u2264 10^9).\n\nNote that in hacks you have to set t = 1.\n\n\n-----Output-----\n\nFor each test you have to construct, output two positive numbers n_{i} and m_{i} (1 \u2264 m_{i} \u2264 n_{i} \u2264 10^9) such that the maximum number of 1's in a m_{i}-free n_{i} \u00d7 n_{i} matrix is exactly x_{i}. If there are multiple solutions, you may output any of them; and if this is impossible to construct a test, output a single integer - 1. \n\n\n-----Example-----\nInput\n3\n21\n0\n1\n\nOutput\n5 2\n1 1\n-1"} +{"id": "01171", "text": "Your friend gave you a dequeue D as a birthday present.\nD is a horizontal cylinder that contains a row of N jewels.\nThe values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.\nIn the beginning, you have no jewel in your hands.\nYou can perform at most K operations on D, chosen from the following, at most K times (possibly zero):\n - Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.\n - Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.\n - Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.\n - Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.\nFind the maximum possible sum of the values of jewels in your hands after the operations.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 50\n - 1 \\leq K \\leq 100\n - -10^7 \\leq V_i \\leq 10^7\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nV_1 V_2 ... V_N\n\n-----Output-----\nPrint the maximum possible sum of the values of jewels in your hands after the operations.\n\n-----Sample Input-----\n6 4\n-10 8 2 1 2 6\n\n-----Sample Output-----\n14\n\nAfter the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.\n - Do operation A. You take out the jewel of value -10 from the left end of D.\n - Do operation B. You take out the jewel of value 6 from the right end of D.\n - Do operation A. You take out the jewel of value 8 from the left end of D.\n - Do operation D. You insert the jewel of value -10 to the right end of D."} +{"id": "01172", "text": "The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:\n - 1 \u2264 i < j < k \u2264 |T| (|T| is the length of T.)\n - T_i = A (T_i is the i-th character of T from the beginning.)\n - T_j = B\n - T_k = C\nFor example, when T = ABCBC, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.\nYou are given a string S. Each character of S is A, B, C or ?.\nLet Q be the number of occurrences of ? in S. We can make 3^Q strings by replacing each occurrence of ? in S with A, B or C. Find the sum of the ABC numbers of all these strings.\nThis sum can be extremely large, so print the sum modulo 10^9 + 7.\n\n-----Constraints-----\n - 3 \u2264 |S| \u2264 10^5\n - Each character of S is A, B, C or ?.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.\n\n-----Sample Input-----\nA??C\n\n-----Sample Output-----\n8\n\nIn this case, Q = 2, and we can make 3^Q = 9 strings by by replacing each occurrence of ? with A, B or C. The ABC number of each of these strings is as follows:\n - AAAC: 0\n - AABC: 2\n - AACC: 0\n - ABAC: 1\n - ABBC: 2\n - ABCC: 2\n - ACAC: 0\n - ACBC: 1\n - ACCC: 0\nThe sum of these is 0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8, so we print 8 modulo 10^9 + 7, that is, 8."} +{"id": "01173", "text": "N players will participate in a tennis tournament. We will call them Player 1, Player 2, \\ldots, Player N.\nThe tournament is round-robin format, and there will be N(N-1)/2 matches in total.\nIs it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.\n - Each player plays at most one matches in a day.\n - Each player i (1 \\leq i \\leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \\ldots, A_{i, N-1} in this order.\n\n-----Constraints-----\n - 3 \\leq N \\leq 1000\n - 1 \\leq A_{i, j} \\leq N\n - A_{i, j} \\neq i\n - A_{i, 1}, A_{i, 2}, \\ldots, A_{i, N-1} are all different.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_{1, 1} A_{1, 2} \\ldots A_{1, N-1}\nA_{2, 1} A_{2, 2} \\ldots A_{2, N-1}\n:\nA_{N, 1} A_{N, 2} \\ldots A_{N, N-1}\n\n-----Output-----\nIf it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.\n\n-----Sample Input-----\n3\n2 3\n1 3\n1 2\n\n-----Sample Output-----\n3\n\nAll the conditions can be satisfied if the matches are scheduled for three days as follows:\n - Day 1: Player 1 vs Player 2\n - Day 2: Player 1 vs Player 3\n - Day 3: Player 2 vs Player 3\nThis is the minimum number of days required."} +{"id": "01174", "text": "Takahashi is going to buy N items one by one.\nThe price of the i-th item he buys is A_i yen (the currency of Japan).\nHe has M discount tickets, and he can use any number of them when buying an item.\nIf Y tickets are used when buying an item priced X yen, he can get the item for \\frac{X}{2^Y} (rounded down to the nearest integer) yen.\nWhat is the minimum amount of money required to buy all the items?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N, M \\leq 10^5\n - 1 \\leq A_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the minimum amount of money required to buy all the items.\n\n-----Sample Input-----\n3 3\n2 13 8\n\n-----Sample Output-----\n9\n\nWe can buy all the items for 9 yen, as follows:\n - Buy the 1-st item for 2 yen without tickets.\n - Buy the 2-nd item for 3 yen with 2 tickets.\n - Buy the 3-rd item for 4 yen with 1 ticket."} +{"id": "01175", "text": "Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \\leq x \\leq y \\leq R) such that the remainder when y is divided by x is equal to y \\mbox{ XOR } x.What is \\mbox{ XOR }?\n\nThe XOR of integers A and B, A \\mbox{ XOR } B, is defined as follows:\n\n - When A \\mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.\nFor example, 3 \\mbox{ XOR } 5 = 6. (In base two: 011 \\mbox{ XOR } 101 = 110.)\n\n\n-----Constraints-----\n - 1 \\leq L \\leq R \\leq 10^{18}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nL R\n\n-----Output-----\nPrint the number of pairs of integers (x, y) (L \\leq x \\leq y \\leq R) satisfying the condition, modulo 10^9 + 7.\n\n-----Sample Input-----\n2 3\n\n-----Sample Output-----\n3\n\nThree pairs satisfy the condition: (2, 2), (2, 3), and (3, 3)."} +{"id": "01176", "text": "There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.\nYou can perform the following operation on this integer sequence any number of times:\nOperation: Choose an integer i satisfying 1 \\leq i \\leq N-1. Multiply both A_i and A_{i+1} by -1.\nLet B_1, B_2, ..., B_N be the integer sequence after your operations.\nFind the maximum possible value of B_1 + B_2 + ... + B_N.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 10^5\n - -10^9 \\leq A_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the maximum possible value of B_1 + B_2 + ... + B_N.\n\n-----Sample Input-----\n3\n-10 5 -4\n\n-----Sample Output-----\n19\n\nIf we perform the operation as follows:\n - Choose 1 as i, which changes the sequence to 10, -5, -4.\n - Choose 2 as i, which changes the sequence to 10, 5, 4.\nwe have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result."} +{"id": "01177", "text": "Given are a sequence of N integers A_1, A_2, \\ldots, A_N and a positive integer S.\n\nFor a pair of integers (L, R) such that 1\\leq L \\leq R \\leq N, let us define f(L, R) as follows:\n\n - f(L, R) is the number of sequences of integers (x_1, x_2, \\ldots , x_k) such that L \\leq x_1 < x_2 < \\cdots < x_k \\leq R and A_{x_1}+A_{x_2}+\\cdots +A_{x_k} = S.\nFind the sum of f(L, R) over all pairs of integers (L, R) such that 1\\leq L \\leq R\\leq N. Since this sum can be enormous, print it modulo 998244353.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 3000\n - 1 \\leq S \\leq 3000\n - 1 \\leq A_i \\leq 3000\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN S\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the sum of f(L, R), modulo 998244353.\n\n-----Sample Input-----\n3 4\n2 2 4\n\n-----Sample Output-----\n5\n\nThe value of f(L, R) for each pair is as follows, for a total of 5.\n - f(1,1) = 0\n - f(1,2) = 1 (for the sequence (1, 2))\n - f(1,3) = 2 (for (1, 2) and (3))\n - f(2,2) = 0\n - f(2,3) = 1 (for (3))\n - f(3,3) = 1 (for (3))"} +{"id": "01178", "text": "We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.\n\nThe current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.\n\nBefore starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).\n\nDifferent values can be chosen for different columns.\n\nThen, you will create the modified artwork by repeating the following operation:\n\n - Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)\nFind the minimum number of times you need to perform this operation.\n\n-----Constraints-----\n - 1 \\leq N \\leq 300\n - 0 \\leq K \\leq N\n - 0 \\leq H_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nH_1 H_2 ... H_N\n\n-----Output-----\nPrint the minimum number of operations required.\n\n-----Sample Input-----\n4 1\n2 3 4 1\n\n-----Sample Output-----\n3\n\nFor example, by changing the value of H_3 to 2, you can create the modified artwork by the following three operations:\n - Paint black the 1-st through 4-th squares from the left in the 1-st row from the bottom.\n - Paint black the 1-st through 3-rd squares from the left in the 2-nd row from the bottom.\n - Paint black the 2-nd square from the left in the 3-rd row from the bottom."} +{"id": "01179", "text": "In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier\u00a0\u2014 an integer from 1 to 10^9.\n\nAt some moment, robots decided to play the game \"Snowball\". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier.\n\nYour task is to determine the k-th identifier to be pronounced.\n\n\n-----Input-----\n\nThe first line contains two positive integers n and k (1 \u2264 n \u2264 100 000, 1 \u2264 k \u2264 min(2\u00b710^9, n\u00b7(n + 1) / 2).\n\nThe second line contains the sequence id_1, id_2, ..., id_{n} (1 \u2264 id_{i} \u2264 10^9)\u00a0\u2014 identifiers of roborts. It is guaranteed that all identifiers are different.\n\n\n-----Output-----\n\nPrint the k-th pronounced identifier (assume that the numeration starts from 1).\n\n\n-----Examples-----\nInput\n2 2\n1 2\n\nOutput\n1\n\nInput\n4 5\n10 4 18 3\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As k = 2, the answer equals to 1.\n\nIn the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As k = 5, the answer equals to 4."} +{"id": "01180", "text": "Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses (\"+\") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect.\n\nThe lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 10^9 + 7. Help him!\n\n\n-----Input-----\n\nThe first line contains two integers, n and k (0 \u2264 k < n \u2264 10^5).\n\nThe second line contains a string consisting of n digits.\n\n\n-----Output-----\n\nPrint the answer to the problem modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n3 1\n108\n\nOutput\n27\nInput\n3 2\n108\n\nOutput\n9\n\n\n-----Note-----\n\nIn the first sample the result equals (1 + 08) + (10 + 8) = 27.\n\nIn the second sample the result equals 1 + 0 + 8 = 9."} +{"id": "01181", "text": "Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory.\n\nThough Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work.\n\nRyouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page a_{i}. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is $\\sum_{i = 1}^{m - 1}|a_{i + 1} - a_{i}|$.\n\nRyouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y\u00a0(1 \u2264 x, y \u2264 n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place.\n\nPlease tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.\n\n\n-----Input-----\n\nThe first line of input contains two integers n and m\u00a0(1 \u2264 n, m \u2264 10^5).\n\nThe next line contains m integers separated by spaces: a_1, a_2, ..., a_{m} (1 \u2264 a_{i} \u2264 n).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of pages Ryouko needs to turn.\n\n\n-----Examples-----\nInput\n4 6\n1 2 3 4 3 2\n\nOutput\n3\n\nInput\n10 5\n9 4 3 8 8\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3.\n\nIn the second sample, optimal solution is achieved by merging page 9 to 4."} +{"id": "01182", "text": "Paul is at the orchestra. The string section is arranged in an r \u00d7 c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.\n\nTwo pictures are considered to be different if the coordinates of corresponding rectangles are different.\n\n\n-----Input-----\n\nThe first line of input contains four space-separated integers r, c, n, k (1 \u2264 r, c, n \u2264 10, 1 \u2264 k \u2264 n)\u00a0\u2014 the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.\n\nThe next n lines each contain two integers x_{i} and y_{i} (1 \u2264 x_{i} \u2264 r, 1 \u2264 y_{i} \u2264 c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of photographs Paul can take which include at least k violas. \n\n\n-----Examples-----\nInput\n2 2 1 1\n1 2\n\nOutput\n4\n\nInput\n3 2 3 3\n1 1\n3 1\n2 2\n\nOutput\n1\n\nInput\n3 2 3 2\n1 1\n3 1\n2 2\n\nOutput\n4\n\n\n\n-----Note-----\n\nWe will use '*' to denote violinists and '#' to denote violists.\n\nIn the first sample, the orchestra looks as follows \n\n*#\n\n**\n\n Paul can take a photograph of just the viola, the 1 \u00d7 2 column containing the viola, the 2 \u00d7 1 row containing the viola, or the entire string section, for 4 pictures total.\n\nIn the second sample, the orchestra looks as follows \n\n#*\n\n*#\n\n#*\n\n Paul must take a photograph of the entire section.\n\nIn the third sample, the orchestra looks the same as in the second sample."} +{"id": "01183", "text": "Dreamoon is a big fan of the Codeforces contests.\n\nOne day, he claimed that he will collect all the places from $1$ to $54$ after two more rated contests. It's amazing!\n\nBased on this, you come up with the following problem:\n\nThere is a person who participated in $n$ Codeforces rounds. His place in the first round is $a_1$, his place in the second round is $a_2$, ..., his place in the $n$-th round is $a_n$.\n\nYou are given a positive non-zero integer $x$.\n\nPlease, find the largest $v$ such that this person can collect all the places from $1$ to $v$ after $x$ more rated contests.\n\nIn other words, you need to find the largest $v$, such that it is possible, that after $x$ more rated contests, for each $1 \\leq i \\leq v$, there will exist a contest where this person took the $i$-th place.\n\nFor example, if $n=6$, $x=2$ and $a=[3,1,1,5,7,10]$ then answer is $v=5$, because if on the next two contest he will take places $2$ and $4$, then he will collect all places from $1$ to $5$, so it is possible to get $v=5$.\n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\leq t \\leq 5$) denoting the number of test cases in the input.\n\nEach test case contains two lines. The first line contains two integers $n, x$ ($1 \\leq n, x \\leq 100$). The second line contains $n$ positive non-zero integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 100$).\n\n\n-----Output-----\n\nFor each test case print one line containing the largest $v$, such that it is possible that after $x$ other contests, for each $1 \\leq i \\leq v$, there will exist a contest where this person took the $i$-th place.\n\n\n-----Example-----\nInput\n5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20\n\nOutput\n5\n101\n2\n2\n60\n\n\n\n-----Note-----\n\nThe first test case is described in the statement.\n\nIn the second test case, the person has one hundred future contests, so he can take place $1,2,\\ldots,99$ and place $101$ on them in some order, to collect places $1,2,\\ldots,101$."} +{"id": "01184", "text": "Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters from the set in one line, separated by a comma. He also added an opening curved bracket at the beginning of the line and a closing curved bracket at the end of the line. \n\nUnfortunately, from time to time Anton would forget writing some letter and write it again. He asks you to count the total number of distinct letters in his set.\n\n\n-----Input-----\n\nThe first and the single line contains the set of letters. The length of the line doesn't exceed 1000. It is guaranteed that the line starts from an opening curved bracket and ends with a closing curved bracket. Between them, small English letters are listed, separated by a comma. Each comma is followed by a space.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of distinct letters in Anton's set.\n\n\n-----Examples-----\nInput\n{a, b, c}\n\nOutput\n3\n\nInput\n{b, a, b, a}\n\nOutput\n2\n\nInput\n{}\n\nOutput\n0"} +{"id": "01185", "text": "The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.\n\nGiven a sequence of n integers p_1, p_2, ..., p_{n}. You are to choose k pairs of integers:\n\n [l_1, r_1], [l_2, r_2], ..., [l_{k}, r_{k}]\u00a0(1 \u2264 l_1 \u2264 r_1 < l_2 \u2264 r_2 < ... < l_{k} \u2264 r_{k} \u2264 n;\u00a0r_{i} - l_{i} + 1 = m), \n\nin such a way that the value of sum $\\sum_{i = 1}^{k} \\sum_{j = l_{i}}^{r_{i}} p_{j}$ is maximal possible. Help George to cope with the task.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and k (1 \u2264 (m \u00d7 k) \u2264 n \u2264 5000). The second line contains n integers p_1, p_2, ..., p_{n} (0 \u2264 p_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint an integer in a single line \u2014 the maximum possible value of sum.\n\n\n-----Examples-----\nInput\n5 2 1\n1 2 3 4 5\n\nOutput\n9\n\nInput\n7 1 3\n2 10 7 18 5 33 0\n\nOutput\n61"} +{"id": "01186", "text": "Given an integer N, find two permutations: Permutation p of numbers from 1 to N such that p_{i} \u2260 i and p_{i} & i = 0 for all i = 1, 2, ..., N. Permutation q of numbers from 1 to N such that q_{i} \u2260 i and q_{i} & i \u2260 0 for all i = 1, 2, ..., N. \n\n& is the bitwise AND operation.\n\n\n-----Input-----\n\nThe input consists of one line containing a single integer N (1 \u2264 N \u2264 10^5).\n\n\n-----Output-----\n\nFor each subtask, if the required permutation doesn't exist, output a single line containing the word \"NO\"; otherwise output the word \"YES\" in the first line and N elements of the permutation, separated by spaces, in the second line. If there are several possible permutations in a subtask, output any of them.\n\n\n-----Examples-----\nInput\n3\n\nOutput\nNO\nNO\n\nInput\n6\n\nOutput\nYES\n6 5 4 3 2 1 \nYES\n3 6 2 5 1 4"} +{"id": "01187", "text": "You are given a directed graph with $n$ vertices and $m$ directed edges without self-loops or multiple edges.\n\nLet's denote the $k$-coloring of a digraph as following: you color each edge in one of $k$ colors. The $k$-coloring is good if and only if there no cycle formed by edges of same color.\n\nFind a good $k$-coloring of given digraph with minimum possible $k$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($2 \\le n \\le 5000$, $1 \\le m \\le 5000$) \u2014 the number of vertices and edges in the digraph, respectively.\n\nNext $m$ lines contain description of edges \u2014 one per line. Each edge is a pair of integers $u$ and $v$ ($1 \\le u, v \\le n$, $u \\ne v$) \u2014 there is directed edge from $u$ to $v$ in the graph.\n\nIt is guaranteed that each ordered pair $(u, v)$ appears in the list of edges at most once.\n\n\n-----Output-----\n\nIn the first line print single integer $k$ \u2014 the number of used colors in a good $k$-coloring of given graph.\n\nIn the second line print $m$ integers $c_1, c_2, \\dots, c_m$ ($1 \\le c_i \\le k$), where $c_i$ is a color of the $i$-th edge (in order as they are given in the input).\n\nIf there are multiple answers print any of them (you still have to minimize $k$).\n\n\n-----Examples-----\nInput\n4 5\n1 2\n1 3\n3 4\n2 4\n1 4\n\nOutput\n1\n1 1 1 1 1 \n\nInput\n3 3\n1 2\n2 3\n3 1\n\nOutput\n2\n1 1 2"} +{"id": "01188", "text": "It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2^{k} - 1 + r, where k and r are integers, k \u2265 0, 0 < r \u2264 2^{k}. Let's call that representation prairie partition of x.\n\nFor example, the prairie partitions of 12, 17, 7 and 1 are: 12 = 1 + 2 + 4 + 5,\n\n17 = 1 + 2 + 4 + 8 + 2,\n\n7 = 1 + 2 + 4,\n\n1 = 1. \n\nAlice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options!\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of numbers given from Alice to Borys.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^12; a_1 \u2264 a_2 \u2264 ... \u2264 a_{n})\u00a0\u2014 the numbers given from Alice to Borys.\n\n\n-----Output-----\n\nOutput, in increasing order, all possible values of m such that there exists a sequence of positive integers of length m such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.\n\nIf there are no such values of m, output a single integer -1.\n\n\n-----Examples-----\nInput\n8\n1 1 2 2 3 4 5 8\n\nOutput\n2 \n\nInput\n6\n1 1 1 2 2 2\n\nOutput\n2 3 \n\nInput\n5\n1 2 4 4 4\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example, Alice could get the input sequence from [6, 20] as the original sequence.\n\nIn the second example, Alice's original sequence could be either [4, 5] or [3, 3, 3]."} +{"id": "01189", "text": "Heidi the Cow is aghast: cracks in the northern Wall? Zombies gathering outside, forming groups, preparing their assault? This must not happen! Quickly, she fetches her HC^2 (Handbook of Crazy Constructions) and looks for the right chapter:\n\nHow to build a wall: Take a set of bricks. Select one of the possible wall designs. Computing the number of possible designs is left as an exercise to the reader. Place bricks on top of each other, according to the chosen design. \n\nThis seems easy enough. But Heidi is a Coding Cow, not a Constructing Cow. Her mind keeps coming back to point 2b. Despite the imminent danger of a zombie onslaught, she wonders just how many possible walls she could build with up to n bricks.\n\nA wall is a set of wall segments as defined in the easy version. How many different walls can be constructed such that the wall consists of at least 1 and at most n bricks? Two walls are different if there exist a column c and a row r such that one wall has a brick in this spot, and the other does not.\n\nAlong with n, you will be given C, the width of the wall (as defined in the easy version). Return the number of different walls modulo 10^6 + 3.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and C, 1 \u2264 n \u2264 500000, 1 \u2264 C \u2264 200000.\n\n\n-----Output-----\n\nPrint the number of different walls that Heidi could build, modulo 10^6 + 3.\n\n\n-----Examples-----\nInput\n5 1\n\nOutput\n5\n\nInput\n2 2\n\nOutput\n5\n\nInput\n3 2\n\nOutput\n9\n\nInput\n11 5\n\nOutput\n4367\n\nInput\n37 63\n\nOutput\n230574\n\n\n\n-----Note-----\n\nThe number 10^6 + 3 is prime.\n\nIn the second sample case, the five walls are: \n\n B B\n\nB., .B, BB, B., and .B\n\n\n\nIn the third sample case, the nine walls are the five as in the second sample case and in addition the following four: \n\nB B\n\nB B B B\n\nB., .B, BB, and BB"} +{"id": "01190", "text": "In order to make the \"Sea Battle\" game more interesting, Boris decided to add a new ship type to it. The ship consists of two rectangles. The first rectangle has a width of $w_1$ and a height of $h_1$, while the second rectangle has a width of $w_2$ and a height of $h_2$, where $w_1 \\ge w_2$. In this game, exactly one ship is used, made up of two rectangles. There are no other ships on the field.\n\nThe rectangles are placed on field in the following way: the second rectangle is on top the first rectangle; they are aligned to the left, i.e. their left sides are on the same line; the rectangles are adjacent to each other without a gap. \n\nSee the pictures in the notes: the first rectangle is colored red, the second rectangle is colored blue.\n\nFormally, let's introduce a coordinate system. Then, the leftmost bottom cell of the first rectangle has coordinates $(1, 1)$, the rightmost top cell of the first rectangle has coordinates $(w_1, h_1)$, the leftmost bottom cell of the second rectangle has coordinates $(1, h_1 + 1)$ and the rightmost top cell of the second rectangle has coordinates $(w_2, h_1 + h_2)$.\n\nAfter the ship is completely destroyed, all cells neighboring by side or a corner with the ship are marked. Of course, only cells, which don't belong to the ship are marked. On the pictures in the notes such cells are colored green.\n\nFind out how many cells should be marked after the ship is destroyed. The field of the game is infinite in any direction.\n\n\n-----Input-----\n\nFour lines contain integers $w_1, h_1, w_2$ and $h_2$ ($1 \\leq w_1, h_1, w_2, h_2 \\leq 10^8$, $w_1 \\ge w_2$)\u00a0\u2014 the width of the first rectangle, the height of the first rectangle, the width of the second rectangle and the height of the second rectangle. You can't rotate the rectangles.\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the number of cells, which should be marked after the ship is destroyed.\n\n\n-----Examples-----\nInput\n2 1 2 1\n\nOutput\n12\n\nInput\n2 2 1 2\n\nOutput\n16\n\n\n\n-----Note-----\n\nIn the first example the field looks as follows (the first rectangle is red, the second rectangle is blue, green shows the marked squares): [Image] \n\nIn the second example the field looks as: [Image]"} +{"id": "01191", "text": "Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than $k$ other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins.\n\nNow each knight ponders: how many coins he can have if only he kills other knights?\n\nYou should answer this question for each knight.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ $(1 \\le n \\le 10^5, 0 \\le k \\le \\min(n-1,10))$ \u2014 the number of knights and the number $k$ from the statement.\n\nThe second line contains $n$ integers $p_1, p_2 ,\\ldots,p_n$ $(1 \\le p_i \\le 10^9)$ \u2014 powers of the knights. All $p_i$ are distinct.\n\nThe third line contains $n$ integers $c_1, c_2 ,\\ldots,c_n$ $(0 \\le c_i \\le 10^9)$ \u2014 the number of coins each knight has.\n\n\n-----Output-----\n\nPrint $n$ integers \u2014 the maximum number of coins each knight can have it only he kills other knights.\n\n\n-----Examples-----\nInput\n4 2\n4 5 9 7\n1 2 11 33\n\nOutput\n1 3 46 36 \nInput\n5 1\n1 2 3 4 5\n1 2 3 4 5\n\nOutput\n1 3 5 7 9 \nInput\n1 0\n2\n3\n\nOutput\n3 \n\n\n-----Note-----\n\nConsider the first example. The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has. The second knight can kill the first knight and add his coin to his own two. The third knight is the strongest, but he can't kill more than $k = 2$ other knights. It is optimal to kill the second and the fourth knights: $2+11+33 = 46$. The fourth knight should kill the first and the second knights: $33+1+2 = 36$. \n\nIn the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own.\n\nIn the third example there is only one knight, so he can't kill anyone."} +{"id": "01192", "text": "You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l \u2264 r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation.\n\n\n-----Input-----\n\nThe first line of input contains two integers n and k (1 \u2264 n \u2264 100, 1 \u2264 k \u2264 10^9). The next line contains n integers p_1, p_2, ..., p_{n} \u2014 the given permutation. All p_{i} are different and in range from 1 to n.\n\nThe problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.\n\n In subproblem G1 (3 points), the constraints 1 \u2264 n \u2264 6, 1 \u2264 k \u2264 4 will hold. In subproblem G2 (5 points), the constraints 1 \u2264 n \u2264 30, 1 \u2264 k \u2264 200 will hold. In subproblem G3 (16 points), the constraints 1 \u2264 n \u2264 100, 1 \u2264 k \u2264 10^9 will hold. \n\n\n-----Output-----\n\nOutput the answer with absolute or relative error no more than 1e - 9.\n\n\n-----Examples-----\nInput\n3 1\n1 2 3\n\nOutput\n0.833333333333333\n\nInput\n3 4\n1 3 2\n\nOutput\n1.458333333333334\n\n\n\n-----Note-----\n\nConsider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\\frac{1}{2} \\cdot 0 + \\frac{1}{6} \\cdot 1 + \\frac{1}{6} \\cdot 1 + \\frac{1}{6} \\cdot 3 = \\frac{5}{6}$."} +{"id": "01193", "text": "The R1 company wants to hold a web search championship. There were n computers given for the competition, each of them is connected to the Internet. The organizers believe that the data transfer speed directly affects the result. The higher the speed of the Internet is, the faster the participant will find the necessary information. Therefore, before the competition started, each computer had its maximum possible data transfer speed measured. On the i-th computer it was a_{i} kilobits per second.\n\nThere will be k participants competing in the championship, each should get a separate computer. The organizing company does not want any of the participants to have an advantage over the others, so they want to provide the same data transfer speed to each participant's computer. Also, the organizers want to create the most comfortable conditions for the participants, so the data transfer speed on the participants' computers should be as large as possible.\n\nThe network settings of the R1 company has a special option that lets you to cut the initial maximum data transfer speed of any computer to any lower speed. How should the R1 company configure the network using the described option so that at least k of n computers had the same data transfer speed and the data transfer speed on these computers was as large as possible?\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and k (1 \u2264 k \u2264 n \u2264 100) \u2014 the number of computers and the number of participants, respectively. In the second line you have a space-separated sequence consisting of n integers: a_1, a_2, ..., a_{n} (16 \u2264 a_{i} \u2264 32768); number a_{i} denotes the maximum data transfer speed on the i-th computer.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum Internet speed value. It is guaranteed that the answer to the problem is always an integer.\n\n\n-----Examples-----\nInput\n3 2\n40 20 30\n\nOutput\n30\n\nInput\n6 4\n100 20 40 20 50 50\n\nOutput\n40\n\n\n\n-----Note-----\n\nIn the first test case the organizers can cut the first computer's speed to 30 kilobits. Then two computers (the first and the third one) will have the same speed of 30 kilobits. They should be used as the participants' computers. This answer is optimal."} +{"id": "01194", "text": "Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation [Image], where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.\n\nFor example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)\n\nMisha has two permutations, p and q. Your task is to find their sum.\n\nPermutation a = (a_0, a_1, ..., a_{n} - 1) is called to be lexicographically smaller than permutation b = (b_0, b_1, ..., b_{n} - 1), if for some k following conditions hold: a_0 = b_0, a_1 = b_1, ..., a_{k} - 1 = b_{k} - 1, a_{k} < b_{k}.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 200 000).\n\nThe second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.\n\nThe third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.\n\n\n-----Output-----\n\nPrint n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.\n\n\n-----Examples-----\nInput\n2\n0 1\n0 1\n\nOutput\n0 1\n\nInput\n2\n0 1\n1 0\n\nOutput\n1 0\n\nInput\n3\n1 2 0\n2 1 0\n\nOutput\n1 0 2\n\n\n\n-----Note-----\n\nPermutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).\n\nIn the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is $\\operatorname{Perm}((0 + 0) \\operatorname{mod} 2) = \\operatorname{Perm}(0) =(0,1)$.\n\nIn the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is $\\operatorname{Perm}((0 + 1) \\operatorname{mod} 2) = \\operatorname{Perm}(1) =(1,0)$.\n\nPermutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).\n\nIn the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is $\\operatorname{Perm}((3 + 5) \\operatorname{mod} 6) = \\operatorname{Perm}(2) =(1,0,2)$."} +{"id": "01195", "text": "From \"ftying rats\" to urban saniwation workers - can synthetic biology tronsform how we think of pigeons? \n\nThe upiquitous pigeon has long been viewed as vermin - spleading disease, scavenging through trush, and defecating in populous urban spases. Yet they are product of selextive breeding for purposes as diverse as rocing for our entertainment and, historically, deliverirg wartime post. Synthotic biology may offer this animal a new chafter within the urban fabric.\n\nPiteon d'Or recognihes how these birds ripresent a potentially userul interface for urdan biotechnologies. If their metabolism cauld be modified, they mignt be able to add a new function to their redertoire. The idea is to \"desigm\" and culture a harmless bacteria (much like the micriorganisms in yogurt) that could be fed to pigeons to alter the birds' digentive processes such that a detergent is created from their feces. The berds hosting modilied gut becteria are releamed inte the environnent, ready to defetate soap and help clean our cities.\n\n\n-----Input-----\n\nThe first line of input data contains a single integer $n$ ($5 \\le n \\le 10$).\n\nThe second line of input data contains $n$ space-separated integers $a_i$ ($1 \\le a_i \\le 32$).\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Example-----\nInput\n5\n1 2 3 4 5\n\nOutput\n4\n\n\n\n-----Note-----\n\nWe did not proofread this statement at all."} +{"id": "01196", "text": "Each employee of the \"Blake Techologies\" company uses a special messaging app \"Blake Messenger\". All the stuff likes this app and uses it constantly. However, some important futures are missing. For example, many users want to be able to search through the message history. It was already announced that the new feature will appear in the nearest update, when developers faced some troubles that only you may help them to solve.\n\nAll the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of n blocks, each block containing only equal characters. One block may be described as a pair (l_{i}, c_{i}), where l_{i} is the length of the i-th block and c_{i} is the corresponding letter. Thus, the string s may be written as the sequence of pairs $\\langle(l_{1}, c_{1}),(l_{2}, c_{2}), \\ldots,(l_{n}, c_{n}) \\rangle$.\n\nYour task is to write the program, that given two compressed string t and s finds all occurrences of s in t. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that p is the starting position of some occurrence of s in t if and only if t_{p}t_{p} + 1...t_{p} + |s| - 1 = s, where t_{i} is the i-th character of string t.\n\nNote that the way to represent the string in compressed form may not be unique. For example string \"aaaa\" may be given as $\\langle(4, a) \\rangle$, $\\langle(3, a),(1, a) \\rangle$, $\\langle(2, a),(2, a) \\rangle$...\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 200 000)\u00a0\u2014 the number of blocks in the strings t and s, respectively.\n\nThe second line contains the descriptions of n parts of string t in the format \"l_{i}-c_{i}\" (1 \u2264 l_{i} \u2264 1 000 000)\u00a0\u2014 the length of the i-th part and the corresponding lowercase English letter.\n\nThe second line contains the descriptions of m parts of string s in the format \"l_{i}-c_{i}\" (1 \u2264 l_{i} \u2264 1 000 000)\u00a0\u2014 the length of the i-th part and the corresponding lowercase English letter.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of occurrences of s in t.\n\n\n-----Examples-----\nInput\n5 3\n3-a 2-b 4-c 3-a 2-c\n2-a 2-b 1-c\n\nOutput\n1\nInput\n6 1\n3-a 6-b 7-a 4-c 8-e 2-a\n3-a\n\nOutput\n6\nInput\n5 5\n1-h 1-e 1-l 1-l 1-o\n1-w 1-o 1-r 1-l 1-d\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first sample, t = \"aaabbccccaaacc\", and string s = \"aabbc\". The only occurrence of string s in string t starts at position p = 2.\n\nIn the second sample, t = \"aaabbbbbbaaaaaaacccceeeeeeeeaa\", and s = \"aaa\". The occurrences of s in t start at positions p = 1, p = 10, p = 11, p = 12, p = 13 and p = 14."} +{"id": "01197", "text": "Game \"Minesweeper 1D\" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 \u2014 the total number of bombs in adjacent squares.\n\nFor example, the correct field to play looks like that: 001*2***101*. The cells that are marked with \"*\" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs.\n\nValera wants to make a correct field to play \"Minesweeper 1D\". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.\n\n\n-----Input-----\n\nThe first line contains sequence of characters without spaces s_1s_2... s_{n} (1 \u2264 n \u2264 10^6), containing only characters \"*\", \"?\" and digits \"0\", \"1\" or \"2\". If character s_{i} equals \"*\", then the i-th cell of the field contains a bomb. If character s_{i} equals \"?\", then Valera hasn't yet decided what to put in the i-th cell. Character s_{i}, that is equal to a digit, represents the digit written in the i-th square.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of ways Valera can fill the empty cells and get a correct field.\n\nAs the answer can be rather large, print it modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n?01???\n\nOutput\n4\n\nInput\n?\n\nOutput\n2\n\nInput\n**12\n\nOutput\n0\n\nInput\n1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10."} +{"id": "01198", "text": "Since you are the best Wraith King, Nizhniy Magazin \u00abMir\u00bb at the centre of Vinnytsia is offering you a discount.\n\nYou are given an array a of length n and an integer c. \n\nThe value of some array b of length k is the sum of its elements except for the $\\lfloor \\frac{k}{c} \\rfloor$ smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14.\n\nAmong all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays.\n\n\n-----Input-----\n\nThe first line contains integers n and c (1 \u2264 n, c \u2264 100 000).\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 elements of a.\n\n\n-----Output-----\n\nOutput a single integer \u00a0\u2014 the smallest possible sum of values of these subarrays of some partition of a.\n\n\n-----Examples-----\nInput\n3 5\n1 2 3\n\nOutput\n6\n\nInput\n12 10\n1 1 10 10 10 10 10 10 9 10 10 10\n\nOutput\n92\n\nInput\n7 2\n2 3 6 4 5 7 1\n\nOutput\n17\n\nInput\n8 4\n1 3 4 5 5 3 4 1\n\nOutput\n23\n\n\n\n-----Note-----\n\nIn the first example any partition yields 6 as the sum.\n\nIn the second example one of the optimal partitions is [1, 1], [10, 10, 10, 10, 10, 10, 9, 10, 10, 10] with the values 2 and 90 respectively.\n\nIn the third example one of the optimal partitions is [2, 3], [6, 4, 5, 7], [1] with the values 3, 13 and 1 respectively.\n\nIn the fourth example one of the optimal partitions is [1], [3, 4, 5, 5, 3, 4], [1] with the values 1, 21 and 1 respectively."} +{"id": "01199", "text": "A Christmas party in city S. had n children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to m, and the children are numbered from 1 to n. Then the i-th child has both mittens of color c_{i}.\n\nThe Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children.\n\nThe children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten.\n\n\n-----Input-----\n\nThe first line contains two integers n and m \u2014 the number of the children and the number of possible mitten colors (1 \u2264 n \u2264 5000, 1 \u2264 m \u2264 100). The second line contains n integers c_1, c_2, ... c_{n}, where c_{i} is the color of the mittens of the i-th child (1 \u2264 c_{i} \u2264 m).\n\n\n-----Output-----\n\nIn the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next n lines print the way the mittens can be distributed in this case. On the i-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the i-th child will get. If there are multiple solutions, you can print any of them.\n\n\n-----Examples-----\nInput\n6 3\n1 3 2 2 1 1\n\nOutput\n6\n2 1\n1 2\n2 1\n1 3\n1 2\n3 1\n\nInput\n4 2\n1 2 1 1\n\nOutput\n2\n1 2\n1 1\n2 1\n1 1"} +{"id": "01200", "text": "There are n points on a straight line, and the i-th point among them is located at x_{i}. All these coordinates are distinct.\n\nDetermine the number m \u2014 the smallest number of points you should add on the line to make the distances between all neighboring points equal. \n\n\n-----Input-----\n\nThe first line contains a single integer n (3 \u2264 n \u2264 100 000) \u2014 the number of points.\n\nThe second line contains a sequence of integers x_1, x_2, ..., x_{n} ( - 10^9 \u2264 x_{i} \u2264 10^9) \u2014 the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.\n\n\n-----Output-----\n\nPrint a single integer m \u2014 the smallest number of points you should add on the line to make the distances between all neighboring points equal. \n\n\n-----Examples-----\nInput\n3\n-5 10 5\n\nOutput\n1\n\nInput\n6\n100 200 400 300 600 500\n\nOutput\n0\n\nInput\n4\n10 9 0 -1\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first example you can add one point with coordinate 0.\n\nIn the second example the distances between all neighboring points are already equal, so you shouldn't add anything."} +{"id": "01201", "text": "Polycarp is in really serious trouble \u2014 his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take t_{i} seconds to save i-th item. In addition, for each item, he estimated the value of d_{i} \u2014 the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if t_{i} \u2265 d_{i}, then i-th item cannot be saved.\n\nGiven the values p_{i} for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in t_{a} seconds, and the item b \u2014 in t_{a} + t_{b} seconds after fire started.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100) \u2014 the number of items in Polycarp's house.\n\nEach of the following n lines contains three integers t_{i}, d_{i}, p_{i} (1 \u2264 t_{i} \u2264 20, 1 \u2264 d_{i} \u2264 2 000, 1 \u2264 p_{i} \u2264 20) \u2014 the time needed to save the item i, the time after which the item i will burn completely and the value of item i.\n\n\n-----Output-----\n\nIn the first line print the maximum possible total value of the set of saved items. In the second line print one integer m \u2014 the number of items in the desired set. In the third line print m distinct integers \u2014 numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.\n\n\n-----Examples-----\nInput\n3\n3 7 4\n2 6 5\n3 7 6\n\nOutput\n11\n2\n2 3 \n\nInput\n2\n5 6 1\n3 3 5\n\nOutput\n1\n1\n1 \n\n\n\n-----Note-----\n\nIn the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6 + 5 = 11.\n\nIn the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time."} +{"id": "01202", "text": "Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 \u2264 2k \u2264 n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.\n\nThe tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5) \u2014 the number of participants in each semifinal.\n\nEach of the next n lines contains two integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 10^9)\u00a0\u2014 the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a_1, a_2, ..., a_{n} and b_1, b_2, ..., b_{n} are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.\n\n\n-----Output-----\n\nPrint two strings consisting of n characters, each equals either \"0\" or \"1\". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal \"1\" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a \"0\".\n\n\n-----Examples-----\nInput\n4\n9840 9920\n9860 9980\n9930 10020\n10040 10090\n\nOutput\n1110\n1100\n\nInput\n4\n9900 9850\n9940 9930\n10000 10020\n10060 10110\n\nOutput\n1100\n1100\n\n\n\n-----Note-----\n\nConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds."} +{"id": "01203", "text": "While sailing on a boat, Inessa noticed a beautiful water lily flower above the lake's surface. She came closer and it turned out that the lily was exactly $H$ centimeters above the water surface. Inessa grabbed the flower and sailed the distance of $L$ centimeters. Exactly at this point the flower touched the water surface. [Image] \n\nSuppose that the lily grows at some point $A$ on the lake bottom, and its stem is always a straight segment with one endpoint at point $A$. Also suppose that initially the flower was exactly above the point $A$, i.e. its stem was vertical. Can you determine the depth of the lake at point $A$?\n\n\n-----Input-----\n\nThe only line contains two integers $H$ and $L$ ($1 \\le H < L \\le 10^{6}$).\n\n\n-----Output-----\n\nPrint a single number\u00a0\u2014 the depth of the lake at point $A$. The absolute or relative error should not exceed $10^{-6}$.\n\nFormally, let your answer be $A$, and the jury's answer be $B$. Your answer is accepted if and only if $\\frac{|A - B|}{\\max{(1, |B|)}} \\le 10^{-6}$.\n\n\n-----Examples-----\nInput\n1 2\n\nOutput\n1.5000000000000\n\nInput\n3 5\n\nOutput\n2.6666666666667"} +{"id": "01204", "text": "This problem consists of three subproblems: for solving subproblem C1 you will receive 4 points, for solving subproblem C2 you will receive 4 points, and for solving subproblem C3 you will receive 8 points.\n\nManao decided to pursue a fighter's career. He decided to begin with an ongoing tournament. Before Manao joined, there were n contestants in the tournament, numbered from 1 to n. Each of them had already obtained some amount of tournament points, namely the i-th fighter had p_{i} points.\n\nManao is going to engage in a single fight against each contestant. Each of Manao's fights ends in either a win or a loss. A win grants Manao one point, and a loss grants Manao's opponent one point. For each i, Manao estimated the amount of effort e_{i} he needs to invest to win against the i-th contestant. Losing a fight costs no effort.\n\nAfter Manao finishes all of his fights, the ranklist will be determined, with 1 being the best rank and n + 1 being the worst. The contestants will be ranked in descending order of their tournament points. The contestants with the same number of points as Manao will be ranked better than him if they won the match against him and worse otherwise. The exact mechanism of breaking ties for other fighters is not relevant here.\n\nManao's objective is to have rank k or better. Determine the minimum total amount of effort he needs to invest in order to fulfill this goal, if it is possible.\n\n\n-----Input-----\n\nThe first line contains a pair of integers n and k (1 \u2264 k \u2264 n + 1). The i-th of the following n lines contains two integers separated by a single space \u2014 p_{i} and e_{i} (0 \u2264 p_{i}, e_{i} \u2264 200000).\n\nThe problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.\n\n In subproblem C1 (4 points), the constraint 1 \u2264 n \u2264 15 will hold. In subproblem C2 (4 points), the constraint 1 \u2264 n \u2264 100 will hold. In subproblem C3 (8 points), the constraint 1 \u2264 n \u2264 200000 will hold. \n\n\n-----Output-----\n\nPrint a single number in a single line \u2014 the minimum amount of effort Manao needs to use to rank in the top k. If no amount of effort can earn Manao such a rank, output number -1.\n\n\n-----Examples-----\nInput\n3 2\n1 1\n1 4\n2 2\n\nOutput\n3\n\nInput\n2 1\n3 2\n4 0\n\nOutput\n-1\n\nInput\n5 2\n2 10\n2 10\n1 1\n3 1\n3 1\n\nOutput\n12\n\n\n\n-----Note-----\n\nConsider the first test case. At the time when Manao joins the tournament, there are three fighters. The first of them has 1 tournament point and the victory against him requires 1 unit of effort. The second contestant also has 1 tournament point, but Manao needs 4 units of effort to defeat him. The third contestant has 2 points and victory against him costs Manao 2 units of effort. Manao's goal is top be in top 2. The optimal decision is to win against fighters 1 and 3, after which Manao, fighter 2, and fighter 3 will all have 2 points. Manao will rank better than fighter 3 and worse than fighter 2, thus finishing in second place.\n\nConsider the second test case. Even if Manao wins against both opponents, he will still rank third."} +{"id": "01205", "text": "You are given a set of n points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines.\n\nMultiset is a set where equal elements are allowed.\n\nMultiset is called symmetric, if there is a point P on the plane such that the multiset is centrally symmetric in respect of point P.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 2000) \u2014 the number of points in the set.\n\nEach of the next n lines contains two integers x_{i} and y_{i} ( - 10^6 \u2264 x_{i}, y_{i} \u2264 10^6) \u2014 the coordinates of the points. It is guaranteed that no two points coincide.\n\n\n-----Output-----\n\nIf there are infinitely many good lines, print -1.\n\nOtherwise, print single integer\u00a0\u2014 the number of good lines.\n\n\n-----Examples-----\nInput\n3\n1 2\n2 1\n3 3\n\nOutput\n3\n\nInput\n2\n4 3\n1 2\n\nOutput\n-1\n\n\n\n-----Note-----\n\nPicture to the first sample test:\n\n[Image] \n\nIn the second sample, any line containing the origin is good."} +{"id": "01206", "text": "Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random.\n\nHowever, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot.\n\nLet's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between L_{i} and R_{i}, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [L_{i}, R_{i}] with the same probability. \n\nDetermine the expected value that the winner will have to pay in a second-price auction.\n\n\n-----Input-----\n\nThe first line of input contains an integer number n (2 \u2264 n \u2264 5). n lines follow, the i-th of them containing two numbers L_{i} and R_{i} (1 \u2264 L_{i} \u2264 R_{i} \u2264 10000) describing the i-th company's bid preferences.\n\nThis problem doesn't have subproblems. You will get 8 points for the correct submission.\n\n\n-----Output-----\n\nOutput the answer with absolute or relative error no more than 1e - 9.\n\n\n-----Examples-----\nInput\n3\n4 7\n8 10\n5 5\n\nOutput\n5.7500000000\n\nInput\n3\n2 5\n3 4\n1 6\n\nOutput\n3.5000000000\n\n\n\n-----Note-----\n\nConsider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5\u00b75 + 0.25\u00b76 + 0.25\u00b77 = 5.75."} +{"id": "01207", "text": "While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level.\n\nGiven the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level.\n\n\n-----Input-----\n\nThe first line of the input consists of two integers, the number of robots n (2 \u2264 n \u2264 100 000) and the number of rap battles m ($1 \\leq m \\leq \\operatorname{min}(100000, \\frac{n(n - 1)}{2})$).\n\nThe next m lines describe the results of the rap battles in the order they took place. Each consists of two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i}), indicating that robot u_{i} beat robot v_{i} in the i-th rap battle. No two rap battles involve the same pair of robots.\n\nIt is guaranteed that at least one ordering of the robots satisfies all m relations.\n\n\n-----Output-----\n\nPrint the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1.\n\n\n-----Examples-----\nInput\n4 5\n2 1\n1 3\n2 3\n4 2\n4 3\n\nOutput\n4\n\nInput\n3 2\n1 2\n3 2\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles.\n\nIn the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles."} +{"id": "01208", "text": "Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.\n\nToday was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form \"reader entered room\", \"reader left room\". Every reader is assigned a registration number during the registration procedure at the library \u2014 it's a unique integer from 1 to 10^6. Thus, the system logs events of two forms: \"+ r_{i}\" \u2014 the reader with registration number r_{i} entered the room; \"- r_{i}\" \u2014 the reader with registration number r_{i} left the room. \n\nThe first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors.\n\nSignificant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence.\n\nHelp the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you.\n\n\n-----Input-----\n\nThe first line contains a positive integer n (1 \u2264 n \u2264 100) \u2014 the number of records in the system log. Next follow n events from the system journal in the order in which the were made. Each event was written on a single line and looks as \"+ r_{i}\" or \"- r_{i}\", where r_{i} is an integer from 1 to 10^6, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers).\n\nIt is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum possible capacity of the reading room.\n\n\n-----Examples-----\nInput\n6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7\n\nOutput\n3\nInput\n2\n- 1\n- 2\n\nOutput\n2\nInput\n2\n+ 1\n- 1\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3."} +{"id": "01209", "text": "Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\\lfloor a_i \\rfloor$ or $\\lceil a_i \\rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer.\n\nFor example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. \n\nNote that if $a_i$ is an integer, then there is no difference between $\\lfloor a_i \\rfloor$ and $\\lceil a_i \\rceil$, $b_i$ will always be equal to $a_i$.\n\nHelp Vus the Cossack find such sequence!\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\leq n \\leq 10^5$)\u00a0\u2014 the number of numbers.\n\nEach of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$.\n\n\n-----Output-----\n\nIn each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met.\n\nIf there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n4\n4.58413\n1.22491\n-2.10517\n-3.70387\n\nOutput\n4\n2\n-2\n-4\n\nInput\n5\n-6.32509\n3.30066\n-0.93878\n2.00000\n1.96321\n\nOutput\n-6\n3\n-1\n2\n2\n\n\n\n-----Note-----\n\nThe first example is explained in the legend.\n\nIn the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down."} +{"id": "01210", "text": "There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.\n\nEach shark will grow some number of flowers s_{i}. For i-th shark value s_{i} is random integer equiprobably chosen in range from l_{i} to r_{i}. Wet Shark has it's favourite prime number p, and he really likes it! If for any pair of neighbouring sharks i and j the product s_{i}\u00b7s_{j} is divisible by p, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks.\n\nAt the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and p (3 \u2264 n \u2264 100 000, 2 \u2264 p \u2264 10^9)\u00a0\u2014 the number of sharks and Wet Shark's favourite prime number. It is guaranteed that p is prime.\n\nThe i-th of the following n lines contains information about i-th shark\u00a0\u2014 two space-separated integers l_{i} and r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 10^9), the range of flowers shark i can produce. Remember that s_{i} is chosen equiprobably among all integers from l_{i} to r_{i}, inclusive.\n\n\n-----Output-----\n\nPrint a single real number \u2014 the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. \n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n3 2\n1 2\n420 421\n420420 420421\n\nOutput\n4500.0\n\nInput\n3 5\n1 4\n2 3\n11 14\n\nOutput\n0.0\n\n\n\n-----Note-----\n\nA prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.\n\nConsider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (s_0, s_1, s_2) each shark grows: (1, 420, 420420): note that s_0\u00b7s_1 = 420, s_1\u00b7s_2 = 176576400, and s_2\u00b7s_0 = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars. (1, 420, 420421): now, the product s_2\u00b7s_0 is not divisible by 2. Therefore, sharks s_0 and s_2 will receive 1000 dollars, while shark s_1 will receive 2000. The total is 4000. (1, 421, 420420): total is 4000 (1, 421, 420421): total is 0. (2, 420, 420420): total is 6000. (2, 420, 420421): total is 6000. (2, 421, 420420): total is 6000. (2, 421, 420421): total is 4000.\n\nThe expected value is $\\frac{6000 + 4000 + 4000 + 0 + 6000 + 6000 + 6000 + 4000}{8} = 4500$.\n\nIn the second sample, no combination of quantities will garner the sharks any money."} +{"id": "01211", "text": "Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.\n\nHamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.\n\nDima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves a_{i} hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.\n\nOf course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.\n\nFind out how many boxes and of which type should Dima buy to transport maximum number of hamsters.\n\n\n-----Input-----\n\nThe first line contains two integers N and K (0 \u2264 N \u2264 10^18, 1 \u2264 K \u2264 10^5)\u00a0\u2014 the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces.\n\nThe second line contains K integers a_1, a_2, ..., a_{K} (1 \u2264 a_{i} \u2264 10^18 for all i)\u00a0\u2014 the capacities of boxes.\n\n\n-----Output-----\n\nOutput two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input.\n\nIf there are many correct answers, output any of them.\n\n\n-----Examples-----\nInput\n19 3\n5 4 10\n\nOutput\n2 4\n\nInput\n28 3\n5 6 30\n\nOutput\n1 5"} +{"id": "01212", "text": "There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is h_{i} meters, distinct planks can have distinct heights. [Image] Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] \n\nPolycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.\n\nWrite the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).\n\n\n-----Input-----\n\nThe first line of the input contains integers n and k (1 \u2264 n \u2264 1.5\u00b710^5, 1 \u2264 k \u2264 n) \u2014 the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h_1, h_2, ..., h_{n} (1 \u2264 h_{i} \u2264 100), where h_{i} is the height of the i-th plank of the fence.\n\n\n-----Output-----\n\nPrint such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.\n\n\n-----Examples-----\nInput\n7 3\n1 2 6 1 1 7 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8."} +{"id": "01213", "text": "The R1 company has recently bought a high rise building in the centre of Moscow for its main office. It's time to decorate the new office, and the first thing to do is to write the company's slogan above the main entrance to the building.\n\nThe slogan of the company consists of n characters, so the decorators hung a large banner, n meters wide and 1 meter high, divided into n equal squares. The first character of the slogan must be in the first square (the leftmost) of the poster, the second character must be in the second square, and so on.\n\nOf course, the R1 programmers want to write the slogan on the poster themselves. To do this, they have a large (and a very heavy) ladder which was put exactly opposite the k-th square of the poster. To draw the i-th character of the slogan on the poster, you need to climb the ladder, standing in front of the i-th square of the poster. This action (along with climbing up and down the ladder) takes one hour for a painter. The painter is not allowed to draw characters in the adjacent squares when the ladder is in front of the i-th square because the uncomfortable position of the ladder may make the characters untidy. Besides, the programmers can move the ladder. In one hour, they can move the ladder either a meter to the right or a meter to the left.\n\nDrawing characters and moving the ladder is very tiring, so the programmers want to finish the job in as little time as possible. Develop for them an optimal poster painting plan!\n\n\n-----Input-----\n\nThe first line contains two integers, n and k (1 \u2264 k \u2264 n \u2264 100) \u2014 the number of characters in the slogan and the initial position of the ladder, correspondingly. The next line contains the slogan as n characters written without spaces. Each character of the slogan is either a large English letter, or digit, or one of the characters: '.', '!', ',', '?'.\n\n\n-----Output-----\n\nIn t lines, print the actions the programmers need to make. In the i-th line print: \"LEFT\" (without the quotes), if the i-th action was \"move the ladder to the left\"; \"RIGHT\" (without the quotes), if the i-th action was \"move the ladder to the right\"; \"PRINT x\" (without the quotes), if the i-th action was to \"go up the ladder, paint character x, go down the ladder\". \n\nThe painting time (variable t) must be minimum possible. If there are multiple optimal painting plans, you can print any of them.\n\n\n-----Examples-----\nInput\n2 2\nR1\n\nOutput\nPRINT 1\nLEFT\nPRINT R\n\nInput\n2 1\nR1\n\nOutput\nPRINT R\nRIGHT\nPRINT 1\n\nInput\n6 4\nGO?GO!\n\nOutput\nRIGHT\nRIGHT\nPRINT !\nLEFT\nPRINT O\nLEFT\nPRINT G\nLEFT\nPRINT ?\nLEFT\nPRINT O\nLEFT\nPRINT G\n\n\n\n-----Note-----\n\nNote that the ladder cannot be shifted by less than one meter. The ladder can only stand in front of some square of the poster. For example, you cannot shift a ladder by half a meter and position it between two squares. Then go up and paint the first character and the second character."} +{"id": "01214", "text": "Chouti is working on a strange math problem.\n\nThere was a sequence of $n$ positive integers $x_1, x_2, \\ldots, x_n$, where $n$ is even. The sequence was very special, namely for every integer $t$ from $1$ to $n$, $x_1+x_2+...+x_t$ is a square of some integer number (that is, a perfect square).\n\nSomehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $x_2, x_4, x_6, \\ldots, x_n$. The task for him is to restore the original sequence. Again, it's your turn to help him.\n\nThe problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.\n\n\n-----Input-----\n\nThe first line contains an even number $n$ ($2 \\le n \\le 10^5$).\n\nThe second line contains $\\frac{n}{2}$ positive integers $x_2, x_4, \\ldots, x_n$ ($1 \\le x_i \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nIf there are no possible sequence, print \"No\".\n\nOtherwise, print \"Yes\" and then $n$ positive integers $x_1, x_2, \\ldots, x_n$ ($1 \\le x_i \\le 10^{13}$), where $x_2, x_4, \\ldots, x_n$ should be same as in input data. If there are multiple answers, print any.\n\nNote, that the limit for $x_i$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $1 \\le x_i \\le 10^{13}$.\n\n\n-----Examples-----\nInput\n6\n5 11 44\n\nOutput\nYes\n4 5 16 11 64 44\n\nInput\n2\n9900\n\nOutput\nYes\n100 9900\n\nInput\n6\n314 1592 6535\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first example $x_1=4$ $x_1+x_2=9$ $x_1+x_2+x_3=25$ $x_1+x_2+x_3+x_4=36$ $x_1+x_2+x_3+x_4+x_5=100$ $x_1+x_2+x_3+x_4+x_5+x_6=144$ All these numbers are perfect squares.\n\nIn the second example, $x_1=100$, $x_1+x_2=10000$. They are all perfect squares. There're other answers possible. For example, $x_1=22500$ is another answer.\n\nIn the third example, it is possible to show, that no such sequence exists."} +{"id": "01215", "text": "You have a given integer $n$. Find the number of ways to fill all $3 \\times n$ tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap. $\\square$ This picture describes when $n = 4$. The left one is the shape and the right one is $3 \\times n$ tiles. \n\n\n-----Input-----\n\nThe only line contains one integer $n$ ($1 \\le n \\le 60$)\u00a0\u2014 the length.\n\n\n-----Output-----\n\nPrint the number of ways to fill.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n4\nInput\n1\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first example, there are $4$ possible cases of filling.\n\nIn the second example, you cannot fill the shapes in $3 \\times 1$ tiles."} +{"id": "01216", "text": "Stepan likes to repeat vowel letters when he writes words. For example, instead of the word \"pobeda\" he can write \"pobeeeedaaaaa\".\n\nSergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a single vowel. The vowel letters are \"a\", \"e\", \"i\", \"o\", \"u\" and \"y\".\n\nThere are exceptions: if letters \"e\" or \"o\" repeat in a row exactly 2 times, like in words \"feet\" and \"foot\", the program must skip them and do not transform in one vowel. For example, the word \"iiiimpleeemeentatiioon\" must be converted to the word \"implemeentatioon\".\n\nSergey is very busy and asks you to help him and write the required program.\n\n\n-----Input-----\n\nThe first line contains the integer n (1 \u2264 n \u2264 100 000) \u2014 the number of letters in the word written by Stepan.\n\nThe second line contains the string s which has length that equals to n and contains only lowercase English letters \u2014 the word written by Stepan.\n\n\n-----Output-----\n\nPrint the single string \u2014 the word written by Stepan converted according to the rules described in the statement.\n\n\n-----Examples-----\nInput\n13\npobeeeedaaaaa\n\nOutput\npobeda\n\nInput\n22\niiiimpleeemeentatiioon\n\nOutput\nimplemeentatioon\n\nInput\n18\naeiouyaaeeiioouuyy\n\nOutput\naeiouyaeeioouy\n\nInput\n24\naaaoooiiiuuuyyyeeeggghhh\n\nOutput\naoiuyeggghhh"} +{"id": "01217", "text": "You are given two arrays of integers a and b. For each element of the second array b_{j} you should find the number of elements in array a that are less than or equal to the value b_{j}.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 2\u00b710^5) \u2014 the sizes of arrays a and b.\n\nThe second line contains n integers \u2014 the elements of array a ( - 10^9 \u2264 a_{i} \u2264 10^9).\n\nThe third line contains m integers \u2014 the elements of array b ( - 10^9 \u2264 b_{j} \u2264 10^9).\n\n\n-----Output-----\n\nPrint m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value b_{j}.\n\n\n-----Examples-----\nInput\n5 4\n1 3 5 7 9\n6 4 2 8\n\nOutput\n3 2 1 4\n\nInput\n5 5\n1 2 1 2 5\n3 1 4 1 5\n\nOutput\n4 2 4 2 5"} +{"id": "01218", "text": "Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters.\n\nA splitter is a construction that consists of one input (it can be connected to a water pipe) and x output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe. [Image] The figure shows a 4-output splitter \n\nVova has one splitter of each kind: with 2, 3, 4, ..., k outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.\n\nVova needs the pipeline to have exactly n pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and k (1 \u2264 n \u2264 10^18, 2 \u2264 k \u2264 10^9).\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1.\n\n\n-----Examples-----\nInput\n4 3\n\nOutput\n2\n\nInput\n5 5\n\nOutput\n1\n\nInput\n8 4\n\nOutput\n-1"} +{"id": "01219", "text": "While playing yet another strategy game, Mans has recruited $n$ Swedish heroes, whose powers which can be represented as an array $a$.\n\nUnfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $a_i$ and $a_{i+1}$, remove them and insert a hero with power $-(a_i+a_{i+1})$ back in the same position. \n\nFor example if the array contains the elements $[5, 6, 7, 8]$, he can pick $6$ and $7$ and get $[5, -(6+7), 8] = [5, -13, 8]$.\n\nAfter he will perform this operation $n-1$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 200000$).\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($-10^9 \\le a_i \\le 10^9$)\u00a0\u2014 powers of the heroes.\n\n\n-----Output-----\n\nPrint the largest possible power he can achieve after $n-1$ operations.\n\n\n-----Examples-----\nInput\n4\n5 6 7 8\n\nOutput\n26\n\nInput\n5\n4 -5 9 -2 1\n\nOutput\n15\n\n\n\n-----Note-----\n\nSuitable list of operations for the first sample:\n\n$[5, 6, 7, 8] \\rightarrow [-11, 7, 8] \\rightarrow [-11, -15] \\rightarrow [26]$"} +{"id": "01220", "text": "You are given an undirected graph consisting of n vertices and $\\frac{n(n - 1)}{2} - m$ edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.\n\nYou have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 200000, $0 \\leq m \\leq \\operatorname{min}(\\frac{n(n - 1)}{2}, 200000)$).\n\nThen m lines follow, each containing a pair of integers x and y (1 \u2264 x, y \u2264 n, x \u2260 y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices. \n\n\n-----Output-----\n\nFirstly print k \u2014 the number of connected components in this graph.\n\nThen print k integers \u2014 the sizes of components. You should output these integers in non-descending order.\n\n\n-----Example-----\nInput\n5 5\n1 2\n3 4\n3 2\n4 2\n2 5\n\nOutput\n2\n1 4"} +{"id": "01221", "text": "Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming.\n\nLittle Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a_1, a_2, ..., a_{n}, and Banban's have brightness b_1, b_2, ..., b_{m} respectively.\n\nTommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns.\n\nTommy wants to make the product as small as possible, while Banban tries to make it as large as possible.\n\nYou are asked to find the brightness of the chosen pair if both of them choose optimally.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m (2 \u2264 n, m \u2264 50).\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n}.\n\nThe third line contains m space-separated integers b_1, b_2, ..., b_{m}.\n\nAll the integers range from - 10^9 to 10^9.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the brightness of the chosen pair.\n\n\n-----Examples-----\nInput\n2 2\n20 18\n2 14\n\nOutput\n252\n\nInput\n5 3\n-1 0 1 2 3\n-1 0 1\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself.\n\nIn the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself."} +{"id": "01222", "text": "A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:\n - In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.\nFor example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.\nYou are given a positive integer K. Find the K-th smallest lunlun number.\n\n-----Constraints-----\n - 1 \\leq K \\leq 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n15\n\n-----Sample Output-----\n23\n\nWe will list the 15 smallest lunlun numbers in ascending order:\n1,\n2,\n3,\n4,\n5,\n6,\n7,\n8,\n9,\n10,\n11,\n12,\n21,\n22,\n23.\n\nThus, the answer is 23."} +{"id": "01223", "text": "Given is a permutation P of \\{1, 2, \\ldots, N\\}.\nFor a pair (L, R) (1 \\le L \\lt R \\le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \\ldots, P_R.\nFind \\displaystyle \\sum_{L=1}^{N-1} \\sum_{R=L+1}^{N} X_{L,R}.\n\n-----Constraints-----\n - 2 \\le N \\le 10^5 \n - 1 \\le P_i \\le N \n - P_i \\neq P_j (i \\neq j)\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nP_1 P_2 \\ldots P_N\n\n-----Output-----\nPrint \\displaystyle \\sum_{L=1}^{N-1} \\sum_{R=L+1}^{N} X_{L,R}.\n\n-----Sample Input-----\n3\n2 3 1\n\n-----Sample Output-----\n5\n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5."} +{"id": "01224", "text": "Given is an integer N.\nDetermine whether there is a pair of positive integers (A, B) such that 3^A + 5^B = N, and find one such pair if it exists.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^{18}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nIf there is no pair (A, B) that satisfies the condition, print -1.\nIf there is such a pair, print A and B of one such pair with space in between. If there are multiple such pairs, any of them will be accepted.\n\n-----Sample Input-----\n106\n\n-----Sample Output-----\n4 2\n\nWe have 3^4 + 5^2 = 81 + 25 = 106, so (A, B) = (4, 2) satisfies the condition."} +{"id": "01225", "text": "Caracal is fighting with a monster.\nThe health of the monster is H.\nCaracal can attack by choosing one monster. When a monster is attacked, depending on that monster's health, the following happens:\n - If the monster's health is 1, it drops to 0.\n - If the monster's health, X, is greater than 1, that monster disappears. Then, two new monsters appear, each with the health of \\lfloor X/2 \\rfloor.\n(\\lfloor r \\rfloor denotes the greatest integer not exceeding r.)\nCaracal wins when the healths of all existing monsters become 0 or below.\nFind the minimum number of attacks Caracal needs to make before winning.\n\n-----Constraints-----\n - 1 \\leq H \\leq 10^{12}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH\n\n-----Output-----\nFind the minimum number of attacks Caracal needs to make before winning.\n\n-----Sample Input-----\n2\n\n-----Sample Output-----\n3\n\nWhen Caracal attacks the initial monster, it disappears, and two monsters appear, each with the health of 1.\nThen, Caracal can attack each of these new monsters once and win with a total of three attacks."} +{"id": "01226", "text": "Akari has n kinds of flowers, one of each kind.\nShe is going to choose one or more of these flowers to make a bouquet.\nHowever, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.\nHow many different bouquets are there that Akari can make?\nFind the count modulo (10^9 + 7).\nHere, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq n \\leq 10^9\n - 1 \\leq a < b \\leq \\textrm{min}(n, 2 \\times 10^5)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn a b\n\n-----Output-----\nPrint the number of bouquets that Akari can make, modulo (10^9 + 7). (If there are no such bouquets, print 0.)\n\n-----Sample Input-----\n4 1 3\n\n-----Sample Output-----\n7\n\nIn this case, Akari can choose 2 or 4 flowers to make the bouquet.\nThere are 6 ways to choose 2 out of the 4 flowers, and 1 way to choose 4, so there are a total of 7 different bouquets that Akari can make."} +{"id": "01227", "text": "Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.\n\n-----Constraints-----\n - 1 \\leq N < 10^{100}\n - 1 \\leq K \\leq 3\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nK\n\n-----Output-----\nPrint the count.\n\n-----Sample Input-----\n100\n1\n\n-----Sample Output-----\n19\n\nThe following 19 integers satisfy the condition:\n - 1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100"} +{"id": "01228", "text": "Tokitsukaze is one of the characters in the game \"Kantai Collection\". In this game, every character has a common attribute\u00a0\u2014 health points, shortened to HP.\n\nIn general, different values of HP are grouped into $4$ categories: Category $A$ if HP is in the form of $(4 n + 1)$, that is, when divided by $4$, the remainder is $1$; Category $B$ if HP is in the form of $(4 n + 3)$, that is, when divided by $4$, the remainder is $3$; Category $C$ if HP is in the form of $(4 n + 2)$, that is, when divided by $4$, the remainder is $2$; Category $D$ if HP is in the form of $4 n$, that is, when divided by $4$, the remainder is $0$. \n\nThe above-mentioned $n$ can be any integer.\n\nThese $4$ categories ordered from highest to lowest as $A > B > C > D$, which means category $A$ is the highest and category $D$ is the lowest.\n\nWhile playing the game, players can increase the HP of the character. Now, Tokitsukaze wants you to increase her HP by at most $2$ (that is, either by $0$, $1$ or $2$). How much should she increase her HP so that it has the highest possible category?\n\n\n-----Input-----\n\nThe only line contains a single integer $x$ ($30 \\leq x \\leq 100$)\u00a0\u2014 the value Tokitsukaze's HP currently.\n\n\n-----Output-----\n\nPrint an integer $a$ ($0 \\leq a \\leq 2$) and an uppercase letter $b$ ($b \\in \\lbrace A, B, C, D \\rbrace$), representing that the best way is to increase her HP by $a$, and then the category becomes $b$.\n\nNote that the output characters are case-sensitive.\n\n\n-----Examples-----\nInput\n33\n\nOutput\n0 A\n\nInput\n98\n\nOutput\n1 B\n\n\n\n-----Note-----\n\nFor the first example, the category of Tokitsukaze's HP is already $A$, so you don't need to enhance her ability.\n\nFor the second example: If you don't increase her HP, its value is still $98$, which equals to $(4 \\times 24 + 2)$, and its category is $C$. If you increase her HP by $1$, its value becomes $99$, which equals to $(4 \\times 24 + 3)$, and its category becomes $B$. If you increase her HP by $2$, its value becomes $100$, which equals to $(4 \\times 25)$, and its category becomes $D$. \n\nTherefore, the best way is to increase her HP by $1$ so that the category of her HP becomes $B$."} +{"id": "01229", "text": "You have multiset of n strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position i and some letter c of the English alphabet, such that this string is the only string in the multiset that has letter c in position i.\n\nFor example, a multiset of strings {\"abc\", \"aba\", \"adc\", \"ada\"} are not easy to remember. And multiset {\"abc\", \"ada\", \"ssa\"} is easy to remember because: the first string is the only string that has character c in position 3; the second string is the only string that has character d in position 2; the third string is the only string that has character s in position 2. \n\nYou want to change your multiset a little so that it is easy to remember. For a_{ij} coins, you can change character in the j-th position of the i-th string into any other lowercase letter of the English alphabet. Find what is the minimum sum you should pay in order to make the multiset of strings easy to remember.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 20)\u00a0\u2014 the number of strings in the multiset and the length of the strings respectively. Next n lines contain the strings of the multiset, consisting only of lowercase English letters, each string's length is m.\n\nNext n lines contain m integers each, the i-th of them contains integers a_{i}1, a_{i}2, ..., a_{im} (0 \u2264 a_{ij} \u2264 10^6).\n\n\n-----Output-----\n\nPrint a single number \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n4 5\nabcde\nabcde\nabcde\nabcde\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n\nOutput\n3\n\nInput\n4 3\nabc\naba\nadc\nada\n10 10 10\n10 1 10\n10 10 10\n10 1 10\n\nOutput\n2\n\nInput\n3 3\nabc\nada\nssa\n1 1 1\n1 1 1\n1 1 1\n\nOutput\n0"} +{"id": "01230", "text": "Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if $a$ is a friend of $b$, then $b$ is also a friend of $a$. Each user thus has a non-negative amount of friends.\n\nThis morning, somebody anonymously sent Bob the following link: graph realization problem and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them.\n\n\n-----Input-----\n\nThe first line contains one integer $n$\u00a0($1 \\leq n \\leq 5 \\cdot 10^5$), the number of people on the network excluding Bob. \n\nThe second line contains $n$ numbers $a_1,a_2, \\dots, a_n$\u00a0($0 \\leq a_i \\leq n$), with $a_i$ being the number of people that person $i$ is a friend of.\n\n\n-----Output-----\n\nPrint all possible values of $a_{n+1}$\u00a0\u2014 the amount of people that Bob can be friend of, in increasing order.\n\nIf no solution exists, output $-1$.\n\n\n-----Examples-----\nInput\n3\n3 3 3\n\nOutput\n3 \n\nInput\n4\n1 1 1 1\n\nOutput\n0 2 4 \n\nInput\n2\n0 2\n\nOutput\n-1\n\nInput\n35\n21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22\n\nOutput\n13 15 17 19 21 \n\n\n\n-----Note-----\n\nIn the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have $3$ friends.\n\nIn the second test case, there are three possible solutions (apart from symmetries): $a$ is friend of $b$, $c$ is friend of $d$, and Bob has no friends, or $a$ is a friend of $b$ and both $c$ and $d$ are friends with Bob, or Bob is friends of everyone. \n\nThe third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger."} +{"id": "01231", "text": "On her way to programming school tiger Dasha faced her first test \u2014 a huge staircase! [Image] \n\nThe steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values \u2014 the number of steps with even and odd numbers. \n\nYou need to check whether there is an interval of steps from the l-th to the r-th (1 \u2264 l \u2264 r), for which values that Dasha has found are correct.\n\n\n-----Input-----\n\nIn the only line you are given two integers a, b (0 \u2264 a, b \u2264 100) \u2014 the number of even and odd steps, accordingly.\n\n\n-----Output-----\n\nIn the only line print \"YES\", if the interval of steps described above exists, and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n2 3\n\nOutput\nYES\n\nInput\n3 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example one of suitable intervals is from 1 to 5. The interval contains two even steps\u00a0\u2014 2 and 4, and three odd: 1, 3 and 5."} +{"id": "01232", "text": "You are given two arrays A and B consisting of integers, sorted in non-decreasing order. Check whether it is possible to choose k numbers in array A and choose m numbers in array B so that any number chosen in the first array is strictly less than any number chosen in the second array.\n\n\n-----Input-----\n\nThe first line contains two integers n_{A}, n_{B} (1 \u2264 n_{A}, n_{B} \u2264 10^5), separated by a space \u2014 the sizes of arrays A and B, correspondingly.\n\nThe second line contains two integers k and m (1 \u2264 k \u2264 n_{A}, 1 \u2264 m \u2264 n_{B}), separated by a space.\n\nThe third line contains n_{A} numbers a_1, a_2, ... a_{n}_{A} ( - 10^9 \u2264 a_1 \u2264 a_2 \u2264 ... \u2264 a_{n}_{A} \u2264 10^9), separated by spaces \u2014 elements of array A.\n\nThe fourth line contains n_{B} integers b_1, b_2, ... b_{n}_{B} ( - 10^9 \u2264 b_1 \u2264 b_2 \u2264 ... \u2264 b_{n}_{B} \u2264 10^9), separated by spaces \u2014 elements of array B.\n\n\n-----Output-----\n\nPrint \"YES\" (without the quotes), if you can choose k numbers in array A and m numbers in array B so that any number chosen in array A was strictly less than any number chosen in array B. Otherwise, print \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n3 3\n2 1\n1 2 3\n3 4 5\n\nOutput\nYES\n\nInput\n3 3\n3 3\n1 2 3\n3 4 5\n\nOutput\nNO\n\nInput\n5 2\n3 1\n1 1 1 1 1\n2 2\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample test you can, for example, choose numbers 1 and 2 from array A and number 3 from array B (1 < 3 and 2 < 3).\n\nIn the second sample test the only way to choose k elements in the first array and m elements in the second one is to choose all numbers in both arrays, but then not all the numbers chosen in A will be less than all the numbers chosen in B: $3 < 3$."} +{"id": "01233", "text": "Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array a of size n in the non-decreasing order. for (int i = 1; i < n; i = i + 1)\n\n{\n\n int j = i; \n\n while (j > 0 && a[j] < a[j - 1])\n\n {\n\n swap(a[j], a[j - 1]); // swap elements a[j] and a[j - 1]\n\n j = j - 1;\n\n }\n\n}\n\n\n\nPetya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to n - 1. He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.\n\nIt is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 5000) \u2014 the length of the permutation. The second line contains n different integers from 0 to n - 1, inclusive \u2014 the actual permutation.\n\n\n-----Output-----\n\nPrint two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that swapping the elements of the input permutation with indexes i and j leads to the minimum number of the executions.\n\n\n-----Examples-----\nInput\n5\n4 0 3 1 2\n\nOutput\n3 2\n\nInput\n5\n1 2 3 4 0\n\nOutput\n3 4\n\n\n\n-----Note-----\n\nIn the first sample the appropriate pairs are (0, 3) and (0, 4). \n\nIn the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4)."} +{"id": "01234", "text": "An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\\ldots$, $a_r$ for some $l, r$.\n\nSuppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$.\n\n For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$.\n\nYou are given an array $a_1, a_2, \\ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that:\n\n Each element from $a$ belongs to exactly one subarray.\n\n Each subarray has at least $m$ elements.\n\n The sum of all beauties of $k$ subarrays is maximum possible.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $k$ ($2 \\le n \\le 2 \\cdot 10^5$, $1 \\le m$, $2 \\le k$, $m \\cdot k \\le n$)\u00a0\u2014 the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($-10^9 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nIn the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.\n\nIn the second line, print $k-1$ integers $p_1, p_2, \\ldots, p_{k-1}$ ($1 \\le p_1 < p_2 < \\ldots < p_{k-1} < n$) representing the partition of the array, in which:\n\n\n\n All elements with indices from $1$ to $p_1$ belong to the first subarray.\n\n All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray.\n\n $\\ldots$.\n\n All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray.\n\nIf there are several optimal partitions, print any of them.\n\n\n-----Examples-----\nInput\n9 2 3\n5 2 5 2 4 1 1 3 2\n\nOutput\n21\n3 5 \nInput\n6 1 4\n4 1 3 2 2 3\n\nOutput\n12\n1 3 5 \nInput\n2 1 2\n-1000000000 1000000000\n\nOutput\n0\n1 \n\n\n-----Note-----\n\nIn the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$.\n\n The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$. \n\nThe sum of their beauties is $10 + 6 + 5 = 21$.\n\nIn the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$."} +{"id": "01235", "text": "You are given an array $A$, consisting of $n$ positive integers $a_1, a_2, \\dots, a_n$, and an array $B$, consisting of $m$ positive integers $b_1, b_2, \\dots, b_m$. \n\nChoose some element $a$ of $A$ and some element $b$ of $B$ such that $a+b$ doesn't belong to $A$ and doesn't belong to $B$. \n\nFor example, if $A = [2, 1, 7]$ and $B = [1, 3, 4]$, we can choose $1$ from $A$ and $4$ from $B$, as number $5 = 1 + 4$ doesn't belong to $A$ and doesn't belong to $B$. However, we can't choose $2$ from $A$ and $1$ from $B$, as $3 = 2 + 1$ belongs to $B$.\n\nIt can be shown that such a pair exists. If there are multiple answers, print any.\n\nChoose and print any such two numbers.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1\\le n \\le 100$)\u00a0\u2014 the number of elements of $A$.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 200$)\u00a0\u2014 the elements of $A$.\n\nThe third line contains one integer $m$ ($1\\le m \\le 100$)\u00a0\u2014 the number of elements of $B$.\n\nThe fourth line contains $m$ different integers $b_1, b_2, \\dots, b_m$ ($1 \\le b_i \\le 200$)\u00a0\u2014 the elements of $B$.\n\nIt can be shown that the answer always exists.\n\n\n-----Output-----\n\nOutput two numbers $a$ and $b$ such that $a$ belongs to $A$, $b$ belongs to $B$, but $a+b$ doesn't belong to nor $A$ neither $B$.\n\nIf there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n1\n20\n2\n10 20\n\nOutput\n20 20\nInput\n3\n3 2 2\n5\n1 5 7 7 9\n\nOutput\n3 1\n\nInput\n4\n1 3 5 7\n4\n7 5 3 1\n\nOutput\n1 1\n\n\n\n-----Note-----\n\nIn the first example, we can choose $20$ from array $[20]$ and $20$ from array $[10, 20]$. Number $40 = 20 + 20$ doesn't belong to any of those arrays. However, it is possible to choose $10$ from the second array too.\n\nIn the second example, we can choose $3$ from array $[3, 2, 2]$ and $1$ from array $[1, 5, 7, 7, 9]$. Number $4 = 3 + 1$ doesn't belong to any of those arrays.\n\nIn the third example, we can choose $1$ from array $[1, 3, 5, 7]$ and $1$ from array $[7, 5, 3, 1]$. Number $2 = 1 + 1$ doesn't belong to any of those arrays."} +{"id": "01236", "text": "There are n cities in Westeros. The i-th city is inhabited by a_{i} people. Daenerys and Stannis play the following game: in one single move, a player chooses a certain town and burns it to the ground. Thus all its residents, sadly, die. Stannis starts the game. The game ends when Westeros has exactly k cities left.\n\nThe prophecy says that if the total number of surviving residents is even, then Daenerys wins: Stannis gets beheaded, and Daenerys rises on the Iron Throne. If the total number of surviving residents is odd, Stannis wins and everything goes in the completely opposite way.\n\nLord Petyr Baelish wants to know which candidates to the throne he should support, and therefore he wonders, which one of them has a winning strategy. Answer to this question of Lord Baelish and maybe you will become the next Lord of Harrenholl.\n\n\n-----Input-----\n\nThe first line contains two positive space-separated integers, n and k (1 \u2264 k \u2264 n \u2264 2\u00b710^5) \u2014 the initial number of cities in Westeros and the number of cities at which the game ends. \n\nThe second line contains n space-separated positive integers a_{i} (1 \u2264 a_{i} \u2264 10^6), which represent the population of each city in Westeros.\n\n\n-----Output-----\n\nPrint string \"Daenerys\" (without the quotes), if Daenerys wins and \"Stannis\" (without the quotes), if Stannis wins.\n\n\n-----Examples-----\nInput\n3 1\n1 2 1\n\nOutput\nStannis\n\nInput\n3 1\n2 2 1\n\nOutput\nDaenerys\n\nInput\n6 3\n5 20 12 7 14 101\n\nOutput\nStannis\n\n\n\n-----Note-----\n\nIn the first sample Stannis will use his move to burn a city with two people and Daenerys will be forced to burn a city with one resident. The only survivor city will have one resident left, that is, the total sum is odd, and thus Stannis wins.\n\nIn the second sample, if Stannis burns a city with two people, Daenerys burns the city with one resident, or vice versa. In any case, the last remaining city will be inhabited by two people, that is, the total sum is even, and hence Daenerys wins."} +{"id": "01237", "text": "Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special \u2014 it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.\n\nThe elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.\n\n\n-----Input-----\n\nThe first line of input contains two integers n and s (1 \u2264 n \u2264 100, 1 \u2264 s \u2264 1000)\u00a0\u2014 the number of passengers and the number of the top floor respectively.\n\nThe next n lines each contain two space-separated integers f_{i} and t_{i} (1 \u2264 f_{i} \u2264 s, 1 \u2264 t_{i} \u2264 1000)\u00a0\u2014 the floor and the time of arrival in seconds for the passenger number i.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum amount of time in seconds needed to bring all the passengers to floor 0.\n\n\n-----Examples-----\nInput\n3 7\n2 1\n3 8\n5 2\n\nOutput\n11\n\nInput\n5 10\n2 77\n3 33\n8 21\n9 12\n10 64\n\nOutput\n79\n\n\n\n-----Note-----\n\nIn the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:\n\n1. Move to floor 5: takes 2 seconds.\n\n2. Pick up passenger 3.\n\n3. Move to floor 3: takes 2 seconds.\n\n4. Wait for passenger 2 to arrive: takes 4 seconds.\n\n5. Pick up passenger 2.\n\n6. Go to floor 2: takes 1 second.\n\n7. Pick up passenger 1.\n\n8. Go to floor 0: takes 2 seconds.\n\nThis gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds."} +{"id": "01238", "text": "There was an electronic store heist last night.\n\nAll keyboards which were in the store yesterday were numbered in ascending order from some integer number $x$. For example, if $x = 4$ and there were $3$ keyboards in the store, then the devices had indices $4$, $5$ and $6$, and if $x = 10$ and there were $7$ of them then the keyboards had indices $10$, $11$, $12$, $13$, $14$, $15$ and $16$.\n\nAfter the heist, only $n$ keyboards remain, and they have indices $a_1, a_2, \\dots, a_n$. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither $x$ nor the number of keyboards in the store before the heist.\n\n\n-----Input-----\n\nThe first line contains single integer $n$ $(1 \\le n \\le 1\\,000)$\u00a0\u2014 the number of keyboards in the store that remained after the heist.\n\nThe second line contains $n$ distinct integers $a_1, a_2, \\dots, a_n$ $(1 \\le a_i \\le 10^{9})$\u00a0\u2014 the indices of the remaining keyboards. The integers $a_i$ are given in arbitrary order and are pairwise distinct.\n\n\n-----Output-----\n\nPrint the minimum possible number of keyboards that have been stolen if the staff remember neither $x$ nor the number of keyboards in the store before the heist.\n\n\n-----Examples-----\nInput\n4\n10 13 12 8\n\nOutput\n2\n\nInput\n5\n7 5 6 4 8\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, if $x=8$ then minimum number of stolen keyboards is equal to $2$. The keyboards with indices $9$ and $11$ were stolen during the heist.\n\nIn the second example, if $x=4$ then nothing was stolen during the heist."} +{"id": "01239", "text": "There are n cities situated along the main road of Berland. Cities are represented by their coordinates \u2014 integer numbers a_1, a_2, ..., a_{n}. All coordinates are pairwise distinct.\n\nIt is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money \u2014 he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.\n\nIt is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. \n\nYour task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.\n\n\n-----Input-----\n\nThe first line contains one integer number n (2 \u2264 n \u2264 2\u00b710^5).\n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9). All numbers a_{i} are pairwise distinct.\n\n\n-----Output-----\n\nPrint two integer numbers \u2014 the minimal distance and the quantity of pairs with this distance.\n\n\n-----Examples-----\nInput\n4\n6 -3 0 4\n\nOutput\n2 1\n\nInput\n3\n-2 0 2\n\nOutput\n2 2\n\n\n\n-----Note-----\n\nIn the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance."} +{"id": "01240", "text": "Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.\n\nThere will be n columns participating in the parade, the i-th column consists of l_{i} soldiers, who start to march from left leg, and r_{i} soldiers, who start to march from right leg.\n\nThe beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.\n\nNo more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values l_{i} and r_{i}. \n\nFind the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of columns. \n\nThe next n lines contain the pairs of integers l_{i} and r_{i} (1 \u2264 l_{i}, r_{i} \u2264 500)\u00a0\u2014 the number of soldiers in the i-th column which start to march from the left or the right leg respectively.\n\n\n-----Output-----\n\nPrint single integer k\u00a0\u2014 the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.\n\nConsider that columns are numbered from 1 to n in the order they are given in the input data.\n\nIf there are several answers, print any of them.\n\n\n-----Examples-----\nInput\n3\n5 6\n8 9\n10 3\n\nOutput\n3\n\nInput\n2\n6 5\n5 6\n\nOutput\n1\n\nInput\n6\n5 9\n1 3\n4 8\n4 5\n23 54\n12 32\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg\u00a0\u2014 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.\n\nIf you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg\u00a0\u2014 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.\n\nIt is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9."} +{"id": "01241", "text": "You are given an array a with n elements. Each element of a is either 0 or 1.\n\nLet's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 3\u00b710^5, 0 \u2264 k \u2264 n) \u2014 the number of elements in a and the parameter k.\n\nThe second line contains n integers a_{i} (0 \u2264 a_{i} \u2264 1) \u2014 the elements of a.\n\n\n-----Output-----\n\nOn the first line print a non-negative integer z \u2014 the maximal value of f(a) after no more than k changes of zeroes to ones.\n\nOn the second line print n integers a_{j} \u2014 the elements of the array a after the changes.\n\nIf there are multiple answers, you can print any one of them.\n\n\n-----Examples-----\nInput\n7 1\n1 0 0 1 1 0 1\n\nOutput\n4\n1 0 0 1 1 1 1\n\nInput\n10 2\n1 0 0 1 0 1 0 1 0 1\n\nOutput\n5\n1 0 0 1 1 1 1 1 0 1"} +{"id": "01242", "text": "IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.\n\nToday, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. \"It would look much better when I'll swap some of them!\"\u00a0\u2014 thought the girl\u00a0\u2014 \"but how to do it?\". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?\n\nA string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \\ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$.\n\n\n-----Input-----\n\nThe first and the only line contains a string $s$ ($1 \\le |s| \\le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'.\n\n\n-----Output-----\n\nOutput exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise.\n\nIf there are multiple possible sequences leading to the optimal answer, print any of them.\n\n\n-----Examples-----\nInput\nbbab\n\nOutput\n0 1 1 0\n\nInput\naaaaa\n\nOutput\n1 0 0 0 1\n\n\n\n-----Note-----\n\nIn the first example, IA can reverse the second and the third prefix and get a string \"abbb\". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.\n\nIn the second example, she can reverse any subset of prefixes\u00a0\u2014 all letters are 'a'."} +{"id": "01243", "text": "Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.\n\n\n-----Output-----\n\nPrint the total minimum number of moves.\n\n\n-----Examples-----\nInput\n6\n1 6 2 5 3 7\n\nOutput\n12"} +{"id": "01244", "text": "Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time.\n\nHelp Yaroslav.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100) \u2014 the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1000) \u2014 the array elements.\n\n\n-----Output-----\n\nIn the single line print \"YES\" (without the quotes) if Yaroslav can obtain the array he needs, and \"NO\" (without the quotes) otherwise.\n\n\n-----Examples-----\nInput\n1\n1\n\nOutput\nYES\n\nInput\n3\n1 1 2\n\nOutput\nYES\n\nInput\n4\n7 7 7 7\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample the initial array fits well.\n\nIn the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it.\n\nIn the third sample Yarosav can't get the array he needs."} +{"id": "01245", "text": "Notice that the memory limit is non-standard.\n\nRecently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length 2n. Unlike Arthur, Sasha understood the topic very badly, and broke Arthur's favorite correct bracket sequence just to spite him.\n\nAll Arthur remembers about his favorite sequence is for each opening parenthesis ('(') the approximate distance to the corresponding closing one (')'). For the i-th opening bracket he remembers the segment [l_{i}, r_{i}], containing the distance to the corresponding closing bracket.\n\nFormally speaking, for the i-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment [l_{i}, r_{i}].\n\nHelp Arthur restore his favorite correct bracket sequence!\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 600), the number of opening brackets in Arthur's favorite correct bracket sequence. \n\nNext n lines contain numbers l_{i} and r_{i} (1 \u2264 l_{i} \u2264 r_{i} < 2n), representing the segment where lies the distance from the i-th opening bracket and the corresponding closing one. \n\nThe descriptions of the segments are given in the order in which the opening brackets occur in Arthur's favorite sequence if we list them from left to right.\n\n\n-----Output-----\n\nIf it is possible to restore the correct bracket sequence by the given data, print any possible choice.\n\nIf Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line \"IMPOSSIBLE\" (without the quotes).\n\n\n-----Examples-----\nInput\n4\n1 1\n1 1\n1 1\n1 1\n\nOutput\n()()()()\n\nInput\n3\n5 5\n3 3\n1 1\n\nOutput\n((()))\n\nInput\n3\n5 5\n3 3\n2 2\n\nOutput\nIMPOSSIBLE\n\nInput\n3\n2 3\n1 4\n1 4\n\nOutput\n(())()"} +{"id": "01246", "text": "Petya has recently learned data structure named \"Binary heap\".\n\nThe heap he is now operating with allows the following operations: put the given number into the heap; get the value of the minimum element in the heap; extract the minimum element from the heap; \n\nThus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.\n\nIn order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format: insert x\u00a0\u2014 put the element with value x in the heap; getMin x\u00a0\u2014 the value of the minimum element contained in the heap was equal to x; removeMin\u00a0\u2014 the minimum element was extracted from the heap (only one instance, if there were many). \n\nAll the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.\n\nWhile Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.\n\nNow Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.\n\nNow Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.\n\n\n-----Input-----\n\nThe first line of the input contains the only integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of the records left in Petya's journal.\n\nEach of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 10^9 by their absolute value.\n\n\n-----Output-----\n\nThe first line of the output should contain a single integer m\u00a0\u2014 the minimum possible number of records in the modified sequence of operations.\n\nNext m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 10^9 by their absolute value.\n\nNote that the input sequence of operations must be the subsequence of the output sequence.\n\nIt's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.\n\n\n-----Examples-----\nInput\n2\ninsert 3\ngetMin 4\n\nOutput\n4\ninsert 3\nremoveMin\ninsert 4\ngetMin 4\n\nInput\n4\ninsert 1\ninsert 1\nremoveMin\ngetMin 2\n\nOutput\n6\ninsert 1\ninsert 1\nremoveMin\nremoveMin\ninsert 2\ngetMin 2\n\n\n\n-----Note-----\n\nIn the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.\n\nIn the second sample case number 1 is inserted two times, so should be similarly removed twice."} +{"id": "01247", "text": "The Little Girl loves problems on games very much. Here's one of them.\n\nTwo players have got a string s, consisting of lowercase English letters. They play a game that is described by the following rules: The players move in turns; In one move the player can remove an arbitrary letter from string s. If the player before his turn can reorder the letters in string s so as to get a palindrome, this player wins. A palindrome is a string that reads the same both ways (from left to right, and vice versa). For example, string \"abba\" is a palindrome and string \"abc\" isn't. \n\nDetermine which player will win, provided that both sides play optimally well \u2014 the one who moves first or the one who moves second.\n\n\n-----Input-----\n\nThe input contains a single line, containing string s (1 \u2264 |s| \u2264 10^3). String s consists of lowercase English letters.\n\n\n-----Output-----\n\nIn a single line print word \"First\" if the first player wins (provided that both players play optimally well). Otherwise, print word \"Second\". Print the words without the quotes.\n\n\n-----Examples-----\nInput\naba\n\nOutput\nFirst\n\nInput\nabca\n\nOutput\nSecond"} +{"id": "01248", "text": "Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d_1 meter long road between his house and the first shop and a d_2 meter long road between his house and the second shop. Also, there is a road of length d_3 directly connecting these two shops to each other. Help Patrick calculate the minimum distance that he needs to walk in order to go to both shops and return to his house. [Image] \n\nPatrick always starts at his house. He should visit both shops moving only along the three existing roads and return back to his house. He doesn't mind visiting the same shop or passing the same road multiple times. The only goal is to minimize the total distance traveled.\n\n\n-----Input-----\n\nThe first line of the input contains three integers d_1, d_2, d_3 (1 \u2264 d_1, d_2, d_3 \u2264 10^8)\u00a0\u2014 the lengths of the paths. d_1 is the length of the path connecting Patrick's house and the first shop; d_2 is the length of the path connecting Patrick's house and the second shop; d_3 is the length of the path connecting both shops. \n\n\n-----Output-----\n\nPrint the minimum distance that Patrick will have to walk in order to visit both shops and return to his house.\n\n\n-----Examples-----\nInput\n10 20 30\n\nOutput\n60\n\nInput\n1 1 5\n\nOutput\n4\n\n\n\n-----Note-----\n\nThe first sample is shown on the picture in the problem statement. One of the optimal routes is: house $\\rightarrow$ first shop $\\rightarrow$ second shop $\\rightarrow$ house.\n\nIn the second sample one of the optimal routes is: house $\\rightarrow$ first shop $\\rightarrow$ house $\\rightarrow$ second shop $\\rightarrow$ house."} +{"id": "01249", "text": "At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly $k$ flowers.\n\nThe work material for the wreaths for all $n$ citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence $a_1$, $a_2$, ..., $a_m$, where $a_i$ is an integer that denotes the type of flower at the position $i$. This year the liana is very long ($m \\ge n \\cdot k$), and that means every citizen will get a wreath.\n\nVery soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts $k$ flowers from the beginning of the liana, then another $k$ flowers and so on. Each such piece of $k$ flowers is called a workpiece. The machine works until there are less than $k$ flowers on the liana.\n\nDiana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, $k$ flowers must contain flowers of types $b_1$, $b_2$, ..., $b_s$, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.\n\nDiana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least $n$ workpieces?\n\n\n-----Input-----\n\nThe first line contains four integers $m$, $k$, $n$ and $s$ ($1 \\le n, k, m \\le 5 \\cdot 10^5$, $k \\cdot n \\le m$, $1 \\le s \\le k$): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.\n\nThe second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \\le a_i \\le 5 \\cdot 10^5$) \u00a0\u2014 types of flowers on the liana.\n\nThe third line contains $s$ integers $b_1$, $b_2$, ..., $b_s$ ($1 \\le b_i \\le 5 \\cdot 10^5$) \u00a0\u2014 the sequence in Diana's schematic.\n\n\n-----Output-----\n\nIf it's impossible to remove some of the flowers so that there would be at least $n$ workpieces and at least one of them fullfills Diana's schematic requirements, output $-1$.\n\nOtherwise in the first line output one integer $d$ \u00a0\u2014 the number of flowers to be removed by Diana.\n\nIn the next line output $d$ different integers \u00a0\u2014 the positions of the flowers to be removed.\n\nIf there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n7 3 2 2\n1 2 3 3 2 1 2\n2 2\n\nOutput\n1\n4 \n\nInput\n13 4 3 3\n3 2 6 4 1 4 4 7 1 3 3 2 4\n4 3 4\n\nOutput\n-1\n\nInput\n13 4 1 3\n3 2 6 4 1 4 4 7 1 3 3 2 4\n4 3 4\n\nOutput\n9\n1 2 3 4 5 9 11 12 13\n\n\n\n-----Note-----\n\nIn the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types $[1, 2, 3]$ and $[3, 2, 1]$. Those workpieces don't fit Diana's schematic. But if you remove flower on $4$-th place, the machine would output workpieces $[1, 2, 3]$ and $[2, 1, 2]$. The second workpiece fits Diana's schematic.\n\nIn the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.\n\nIn the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs."} +{"id": "01250", "text": "Little boy Valera studies an algorithm of sorting an integer array. After studying the theory, he went on to the practical tasks. As a result, he wrote a program that sorts an array of n integers a_1, a_2, ..., a_{n} in the non-decreasing order. The pseudocode of the program, written by Valera, is given below. The input of the program gets number n and array a.\n\nloop integer variable i from 1 to n - 1\n\n\u00a0\u00a0\u00a0\u00a0loop integer variable j from i to n - 1\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (a_{j} > a_{j} + 1), then swap the values of elements a_{j} and a_{j} + 1\n\n\n\nBut Valera could have made a mistake, because he hasn't yet fully learned the sorting algorithm. If Valera made a mistake in his program, you need to give a counter-example that makes his program work improperly (that is, the example that makes the program sort the array not in the non-decreasing order). If such example for the given value of n doesn't exist, print -1.\n\n\n-----Input-----\n\nYou've got a single integer n (1 \u2264 n \u2264 50) \u2014 the size of the sorted array.\n\n\n-----Output-----\n\nPrint n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100) \u2014 the counter-example, for which Valera's algorithm won't work correctly. If the counter-example that meets the described conditions is impossible to give, print -1.\n\nIf there are several counter-examples, consisting of n numbers, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n-1"} +{"id": "01251", "text": "Bizon the Champion isn't just attentive, he also is very hardworking.\n\nBizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters.\n\nBizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 5000) \u2014 the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of strokes needed to paint the whole fence.\n\n\n-----Examples-----\nInput\n5\n2 2 1 2 1\n\nOutput\n3\n\nInput\n2\n2 2\n\nOutput\n2\n\nInput\n1\n5\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.\n\nIn the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.\n\nIn the third sample there is only one plank that can be painted using a single vertical stroke."} +{"id": "01252", "text": "Recently Irina arrived to one of the most famous cities of Berland\u00a0\u2014 the Berlatov city. There are n showplaces in the city, numbered from 1 to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.\n\nInitially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units.\n\nHelp Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than T time units passing it.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, m and T (2 \u2264 n \u2264 5000, 1 \u2264 m \u2264 5000, 1 \u2264 T \u2264 10^9)\u00a0\u2014 the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.\n\nThe next m lines describes roads in Berlatov. i-th of them contains 3 integers u_{i}, v_{i}, t_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i}, 1 \u2264 t_{i} \u2264 10^9), meaning that there is a road starting from showplace u_{i} and leading to showplace v_{i}, and Irina spends t_{i} time units to pass it. It is guaranteed that the roads do not form cyclic routes.\n\nIt is guaranteed, that there is at most one road between each pair of showplaces.\n\n\n-----Output-----\n\nPrint the single integer k (2 \u2264 k \u2264 n)\u00a0\u2014 the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line.\n\nPrint k distinct integers in the second line\u00a0\u2014 indices of showplaces that Irina will visit on her route, in the order of encountering them.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n4 3 13\n1 2 5\n2 3 7\n2 4 8\n\nOutput\n3\n1 2 4 \n\nInput\n6 6 7\n1 2 2\n1 3 3\n3 6 3\n2 4 2\n4 6 2\n6 5 1\n\nOutput\n4\n1 2 4 6 \n\nInput\n5 5 6\n1 3 3\n3 5 3\n1 2 2\n2 4 3\n4 5 2\n\nOutput\n3\n1 3 5"} +{"id": "01253", "text": "Roma works in a company that sells TVs. Now he has to prepare a report for the last year.\n\nRoma has got a list of the company's incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times.\n\nThe operation of changing a number's sign is the operation of multiplying this number by -1.\n\nHelp Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactly k changes.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n, k \u2264 10^5), showing, how many numbers are in the sequence and how many swaps are to be made.\n\nThe second line contains a non-decreasing sequence, consisting of n integers a_{i} (|a_{i}| \u2264 10^4).\n\nThe numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order.\n\n\n-----Output-----\n\nIn the single line print the answer to the problem \u2014 the maximum total income that we can obtain after exactly k changes.\n\n\n-----Examples-----\nInput\n3 2\n-1 -1 1\n\nOutput\n3\n\nInput\n3 1\n-1 -1 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample we can get sequence [1, 1, 1], thus the total income equals 3.\n\nIn the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1."} +{"id": "01254", "text": "A multi-subject competition is coming! The competition has $m$ different subjects participants can choose from. That's why Alex (the coach) should form a competition delegation among his students. \n\nHe has $n$ candidates. For the $i$-th person he knows subject $s_i$ the candidate specializes in and $r_i$ \u2014 a skill level in his specialization (this level can be negative!). \n\nThe rules of the competition require each delegation to choose some subset of subjects they will participate in. The only restriction is that the number of students from the team participating in each of the chosen subjects should be the same.\n\nAlex decided that each candidate would participate only in the subject he specializes in. Now Alex wonders whom he has to choose to maximize the total sum of skill levels of all delegates, or just skip the competition this year if every valid non-empty delegation has negative sum.\n\n(Of course, Alex doesn't have any spare money so each delegate he chooses must participate in the competition).\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 10^5$, $1 \\le m \\le 10^5$) \u2014 the number of candidates and the number of subjects.\n\nThe next $n$ lines contains two integers per line: $s_i$ and $r_i$ ($1 \\le s_i \\le m$, $-10^4 \\le r_i \\le 10^4$) \u2014 the subject of specialization and the skill level of the $i$-th candidate.\n\n\n-----Output-----\n\nPrint the single integer \u2014 the maximum total sum of skills of delegates who form a valid delegation (according to rules above) or $0$ if every valid non-empty delegation has negative sum.\n\n\n-----Examples-----\nInput\n6 3\n2 6\n3 6\n2 5\n3 5\n1 9\n3 1\n\nOutput\n22\n\nInput\n5 3\n2 6\n3 6\n2 5\n3 5\n1 11\n\nOutput\n23\n\nInput\n5 2\n1 -1\n1 -5\n2 -1\n2 -1\n1 -10\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example it's optimal to choose candidates $1$, $2$, $3$, $4$, so two of them specialize in the $2$-nd subject and other two in the $3$-rd. The total sum is $6 + 6 + 5 + 5 = 22$.\n\nIn the second example it's optimal to choose candidates $1$, $2$ and $5$. One person in each subject and the total sum is $6 + 6 + 11 = 23$.\n\nIn the third example it's impossible to obtain a non-negative sum."} +{"id": "01255", "text": "Valera runs a 24/7 fast food cafe. He magically learned that next day n people will visit his cafe. For each person we know the arrival time: the i-th person comes exactly at h_{i} hours m_{i} minutes. The cafe spends less than a minute to serve each client, but if a client comes in and sees that there is no free cash, than he doesn't want to wait and leaves the cafe immediately. \n\nValera is very greedy, so he wants to serve all n customers next day (and get more profit). However, for that he needs to ensure that at each moment of time the number of working cashes is no less than the number of clients in the cafe. \n\nHelp Valera count the minimum number of cashes to work at his cafe next day, so that they can serve all visitors.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5), that is the number of cafe visitors.\n\nEach of the following n lines has two space-separated integers h_{i} and m_{i} (0 \u2264 h_{i} \u2264 23;\u00a00 \u2264 m_{i} \u2264 59), representing the time when the i-th person comes into the cafe. \n\nNote that the time is given in the chronological order. All time is given within one 24-hour period.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of cashes, needed to serve all clients next day.\n\n\n-----Examples-----\nInput\n4\n8 0\n8 10\n8 10\n8 45\n\nOutput\n2\n\nInput\n3\n0 12\n10 11\n22 22\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample it is not enough one cash to serve all clients, because two visitors will come into cafe in 8:10. Therefore, if there will be one cash in cafe, then one customer will be served by it, and another one will not wait and will go away.\n\nIn the second sample all visitors will come in different times, so it will be enough one cash."} +{"id": "01256", "text": "Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.\n\nThe teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn't enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can't calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3.\n\nYou've got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum.\n\n\n-----Input-----\n\nThe first line contains a non-empty string s \u2014 the sum Xenia needs to count. String s contains no spaces. It only contains digits and characters \"+\". Besides, string s is a correct sum of numbers 1, 2 and 3. String s is at most 100 characters long.\n\n\n-----Output-----\n\nPrint the new sum that Xenia can count.\n\n\n-----Examples-----\nInput\n3+2+1\n\nOutput\n1+2+3\n\nInput\n1+1+3+1+3\n\nOutput\n1+1+1+3+3\n\nInput\n2\n\nOutput\n2"} +{"id": "01257", "text": "People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects.\n\nAlis is among these collectors. Right now she wants to get one of k-special tables. In case you forget, the table n \u00d7 n is called k-special if the following three conditions are satisfied: every integer from 1 to n^2 appears in the table exactly once; in each row numbers are situated in increasing order; the sum of numbers in the k-th column is maximum possible. \n\nYour goal is to help Alice and find at least one k-special table of size n \u00d7 n. Both rows and columns are numbered from 1 to n, with rows numbered from top to bottom and columns numbered from left to right.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 n \u2264 500, 1 \u2264 k \u2264 n)\u00a0\u2014 the size of the table Alice is looking for and the column that should have maximum possible sum.\n\n\n-----Output-----\n\nFirst print the sum of the integers in the k-th column of the required table.\n\nNext n lines should contain the description of the table itself: first line should contains n elements of the first row, second line should contain n elements of the second row and so on.\n\nIf there are multiple suitable table, you are allowed to print any.\n\n\n-----Examples-----\nInput\n4 1\n\nOutput\n28\n1 2 3 4\n5 6 7 8\n9 10 11 12\n13 14 15 16\n\nInput\n5 3\n\nOutput\n85\n5 6 17 18 19\n9 10 23 24 25\n7 8 20 21 22\n3 4 14 15 16\n1 2 11 12 13"} +{"id": "01258", "text": "Bob is an avid fan of the video game \"League of Leesins\", and today he celebrates as the League of Leesins World Championship comes to an end! \n\nThe tournament consisted of $n$ ($n \\ge 5$) teams around the world. Before the tournament starts, Bob has made a prediction of the rankings of each team, from $1$-st to $n$-th. After the final, he compared the prediction with the actual result and found out that the $i$-th team according to his prediction ended up at the $p_i$-th position ($1 \\le p_i \\le n$, all $p_i$ are unique). In other words, $p$ is a permutation of $1, 2, \\dots, n$.\n\nAs Bob's favorite League player is the famous \"3ga\", he decided to write down every $3$ consecutive elements of the permutation $p$. Formally, Bob created an array $q$ of $n-2$ triples, where $q_i = (p_i, p_{i+1}, p_{i+2})$ for each $1 \\le i \\le n-2$. Bob was very proud of his array, so he showed it to his friend Alice.\n\nAfter learning of Bob's array, Alice declared that she could retrieve the permutation $p$ even if Bob rearranges the elements of $q$ and the elements within each triple. Of course, Bob did not believe in such magic, so he did just the same as above to see Alice's respond.\n\nFor example, if $n = 5$ and $p = [1, 4, 2, 3, 5]$, then the original array $q$ will be $[(1, 4, 2), (4, 2, 3), (2, 3, 5)]$. Bob can then rearrange the numbers within each triple and the positions of the triples to get $[(4, 3, 2), (2, 3, 5), (4, 1, 2)]$. Note that $[(1, 4, 2), (4, 2, 2), (3, 3, 5)]$ is not a valid rearrangement of $q$, as Bob is not allowed to swap numbers belong to different triples.\n\nAs Alice's friend, you know for sure that Alice was just trying to show off, so you decided to save her some face by giving her any permutation $p$ that is consistent with the array $q$ she was given. \n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($5 \\le n \\le 10^5$)\u00a0\u2014 the size of permutation $p$.\n\nThe $i$-th of the next $n-2$ lines contains $3$ integers $q_{i, 1}$, $q_{i, 2}$, $q_{i, 3}$ ($1 \\le q_{i, j} \\le n$)\u00a0\u2014 the elements of the $i$-th triple of the rearranged (shuffled) array $q_i$, in random order. Remember, that the numbers within each triple can be rearranged and also the positions of the triples can be rearranged.\n\nIt is guaranteed that there is at least one permutation $p$ that is consistent with the input. \n\n\n-----Output-----\n\nPrint $n$ distinct integers $p_1, p_2, \\ldots, p_n$ ($1 \\le p_i \\le n$) such that $p$ is consistent with array $q$. \n\nIf there are multiple answers, print any. \n\n\n-----Example-----\nInput\n5\n4 3 2\n2 3 5\n4 1 2\n\nOutput\n1 4 2 3 5"} +{"id": "01259", "text": "It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.\n\nGiven a connected graph with $n$ vertices, you can choose to either:\n\n find an independent set that has exactly $\\lceil\\sqrt{n}\\rceil$ vertices.\n\n find a simple cycle of length at least $\\lceil\\sqrt{n}\\rceil$. \n\nAn independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($5 \\le n \\le 10^5$, $n-1 \\le m \\le 2 \\cdot 10^5$)\u00a0\u2014 the number of vertices and edges in the graph.\n\nEach of the next $m$ lines contains two space-separated integers $u$ and $v$ ($1 \\le u,v \\le n$) that mean there's an edge between vertices $u$ and $v$. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.\n\n\n-----Output-----\n\nIf you choose to solve the first problem, then on the first line print \"1\", followed by a line containing $\\lceil\\sqrt{n}\\rceil$ distinct integers not exceeding $n$, the vertices in the desired independent set.\n\nIf you, however, choose to solve the second problem, then on the first line print \"2\", followed by a line containing one integer, $c$, representing the length of the found cycle, followed by a line containing $c$ distinct integers integers not exceeding $n$, the vertices in the desired cycle, in the order they appear in the cycle.\n\n\n-----Examples-----\nInput\n6 6\n1 3\n3 4\n4 2\n2 6\n5 6\n5 1\n\nOutput\n1\n1 6 4\nInput\n6 8\n1 3\n3 4\n4 2\n2 6\n5 6\n5 1\n1 4\n2 5\n\nOutput\n2\n4\n1 5 2 4\nInput\n5 4\n1 2\n1 3\n2 4\n2 5\n\nOutput\n1\n3 4 5 \n\n\n-----Note-----\n\nIn the first sample:\n\n[Image]\n\nNotice that you can solve either problem, so printing the cycle $2-4-3-1-5-6$ is also acceptable.\n\nIn the second sample:\n\n[Image]\n\nNotice that if there are multiple answers you can print any, so printing the cycle $2-5-6$, for example, is acceptable.\n\nIn the third sample:\n\n$A$"} +{"id": "01260", "text": "You are given an array $a$ consisting of $n$ integers. You can perform the following operations with it: Choose some positions $i$ and $j$ ($1 \\le i, j \\le n, i \\ne j$), write the value of $a_i \\cdot a_j$ into the $j$-th cell and remove the number from the $i$-th cell; Choose some position $i$ and remove the number from the $i$-th cell (this operation can be performed no more than once and at any point of time, not necessarily in the beginning). \n\nThe number of elements decreases by one after each operation. However, the indexing of positions stays the same. Deleted numbers can't be used in the later operations.\n\nYour task is to perform exactly $n - 1$ operations with the array in such a way that the only number that remains in the array is maximum possible. This number can be rather large, so instead of printing it you need to print any sequence of operations which leads to this maximum number. Read the output format to understand what exactly you need to print.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in the array.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^9 \\le a_i \\le 10^9$) \u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint $n - 1$ lines. The $k$-th line should contain one of the two possible operations.\n\nThe operation of the first type should look like this: $1~ i_k~ j_k$, where $1$ is the type of operation, $i_k$ and $j_k$ are the positions of the chosen elements.\n\nThe operation of the second type should look like this: $2~ i_k$, where $2$ is the type of operation, $i_k$ is the position of the chosen element. Note that there should be no more than one such operation.\n\nIf there are multiple possible sequences of operations leading to the maximum number \u2014 print any of them.\n\n\n-----Examples-----\nInput\n5\n5 -2 0 1 -3\n\nOutput\n2 3\n1 1 2\n1 2 4\n1 4 5\n\nInput\n5\n5 2 0 4 0\n\nOutput\n1 3 5\n2 5\n1 1 2\n1 2 4\n\nInput\n2\n2 -1\n\nOutput\n2 2\n\nInput\n4\n0 -10 0 0\n\nOutput\n1 1 2\n1 2 3\n1 3 4\n\nInput\n4\n0 0 0 0\n\nOutput\n1 1 2\n1 2 3\n1 3 4\n\n\n\n-----Note-----\n\nLet X be the removed number in the array. Let's take a look at all the examples:\n\nThe first example has, for example, the following sequence of transformations of the array: $[5, -2, 0, 1, -3] \\to [5, -2, X, 1, -3] \\to [X, -10, X, 1, -3] \\to$ $[X, X, X, -10, -3] \\to [X, X, X, X, 30]$. Thus, the maximum answer is $30$. Note, that other sequences that lead to the answer $30$ are also correct.\n\nThe second example has, for example, the following sequence of transformations of the array: $[5, 2, 0, 4, 0] \\to [5, 2, X, 4, 0] \\to [5, 2, X, 4, X] \\to [X, 10, X, 4, X] \\to$ $[X, X, X, 40, X]$. The following answer is also allowed: \n\n1 5 3\n\n1 4 2\n\n1 2 1\n\n2 3\n\n\n\nThen the sequence of transformations of the array will look like this: $[5, 2, 0, 4, 0] \\to [5, 2, 0, 4, X] \\to [5, 8, 0, X, X] \\to [40, X, 0, X, X] \\to$ $[40, X, X, X, X]$.\n\nThe third example can have the following sequence of transformations of the array: $[2, -1] \\to [2, X]$.\n\nThe fourth example can have the following sequence of transformations of the array: $[0, -10, 0, 0] \\to [X, 0, 0, 0] \\to [X, X, 0, 0] \\to [X, X, X, 0]$.\n\nThe fifth example can have the following sequence of transformations of the array: $[0, 0, 0, 0] \\to [X, 0, 0, 0] \\to [X, X, 0, 0] \\to [X, X, X, 0]$."} +{"id": "01261", "text": "Let's call the following process a transformation of a sequence of length $n$.\n\nIf the sequence is empty, the process ends. Otherwise, append the greatest common divisor (GCD) of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a sequence of $n$ integers: the greatest common divisors of all the elements in the sequence before each deletion.\n\nYou are given an integer sequence $1, 2, \\dots, n$. Find the lexicographically maximum result of its transformation.\n\nA sequence $a_1, a_2, \\ldots, a_n$ is lexicographically larger than a sequence $b_1, b_2, \\ldots, b_n$, if there is an index $i$ such that $a_j = b_j$ for all $j < i$, and $a_i > b_i$.\n\n\n-----Input-----\n\nThe first and only line of input contains one integer $n$ ($1\\le n\\le 10^6$).\n\n\n-----Output-----\n\nOutput $n$ integers \u00a0\u2014 the lexicographically maximum result of the transformation.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n1 1 3 \nInput\n2\n\nOutput\n1 2 \nInput\n1\n\nOutput\n1 \n\n\n-----Note-----\n\nIn the first sample the answer may be achieved this way: Append GCD$(1, 2, 3) = 1$, remove $2$. Append GCD$(1, 3) = 1$, remove $1$. Append GCD$(3) = 3$, remove $3$. \n\nWe get the sequence $[1, 1, 3]$ as the result."} +{"id": "01262", "text": "Shichikuji is the new resident deity of the South Black Snail Temple. Her first job is as follows:\n\nThere are $n$ new cities located in Prefecture X. Cities are numbered from $1$ to $n$. City $i$ is located $x_i$ km North of the shrine and $y_i$ km East of the shrine. It is possible that $(x_i, y_i) = (x_j, y_j)$ even when $i \\ne j$.\n\nShichikuji must provide electricity to each city either by building a power station in that city, or by making a connection between that city and another one that already has electricity. So the City has electricity if it has a power station in it or it is connected to a City which has electricity by a direct connection or via a chain of connections.\n\n Building a power station in City $i$ will cost $c_i$ yen; Making a connection between City $i$ and City $j$ will cost $k_i + k_j$ yen per km of wire used for the connection. However, wires can only go the cardinal directions (North, South, East, West). Wires can cross each other. Each wire must have both of its endpoints in some cities. If City $i$ and City $j$ are connected by a wire, the wire will go through any shortest path from City $i$ to City $j$. Thus, the length of the wire if City $i$ and City $j$ are connected is $|x_i - x_j| + |y_i - y_j|$ km. \n\nShichikuji wants to do this job spending as little money as possible, since according to her, there isn't really anything else in the world other than money. However, she died when she was only in fifth grade so she is not smart enough for this. And thus, the new resident deity asks for your help.\n\nAnd so, you have to provide Shichikuji with the following information: minimum amount of yen needed to provide electricity to all cities, the cities in which power stations will be built, and the connections to be made.\n\nIf there are multiple ways to choose the cities and the connections to obtain the construction of minimum price, then print any of them.\n\n\n-----Input-----\n\nFirst line of input contains a single integer $n$ ($1 \\leq n \\leq 2000$) \u2014 the number of cities.\n\nThen, $n$ lines follow. The $i$-th line contains two space-separated integers $x_i$ ($1 \\leq x_i \\leq 10^6$) and $y_i$ ($1 \\leq y_i \\leq 10^6$) \u2014 the coordinates of the $i$-th city.\n\nThe next line contains $n$ space-separated integers $c_1, c_2, \\dots, c_n$ ($1 \\leq c_i \\leq 10^9$) \u2014 the cost of building a power station in the $i$-th city.\n\nThe last line contains $n$ space-separated integers $k_1, k_2, \\dots, k_n$ ($1 \\leq k_i \\leq 10^9$).\n\n\n-----Output-----\n\nIn the first line print a single integer, denoting the minimum amount of yen needed.\n\nThen, print an integer $v$ \u2014 the number of power stations to be built.\n\nNext, print $v$ space-separated integers, denoting the indices of cities in which a power station will be built. Each number should be from $1$ to $n$ and all numbers should be pairwise distinct. You can print the numbers in arbitrary order.\n\nAfter that, print an integer $e$ \u2014 the number of connections to be made.\n\nFinally, print $e$ pairs of integers $a$ and $b$ ($1 \\le a, b \\le n$, $a \\ne b$), denoting that a connection between City $a$ and City $b$ will be made. Each unordered pair of cities should be included at most once (for each $(a, b)$ there should be no more $(a, b)$ or $(b, a)$ pairs). You can print the pairs in arbitrary order.\n\nIf there are multiple ways to choose the cities and the connections to obtain the construction of minimum price, then print any of them.\n\n\n-----Examples-----\nInput\n3\n2 3\n1 1\n3 2\n3 2 3\n3 2 3\n\nOutput\n8\n3\n1 2 3 \n0\n\nInput\n3\n2 1\n1 2\n3 3\n23 2 23\n3 2 3\n\nOutput\n27\n1\n2 \n2\n1 2\n2 3\n\n\n\n-----Note-----\n\nFor the answers given in the samples, refer to the following diagrams (cities with power stations are colored green, other cities are colored blue, and wires are colored red):\n\n[Image]\n\nFor the first example, the cost of building power stations in all cities is $3 + 2 + 3 = 8$. It can be shown that no configuration costs less than 8 yen.\n\nFor the second example, the cost of building a power station in City 2 is 2. The cost of connecting City 1 and City 2 is $2 \\cdot (3 + 2) = 10$. The cost of connecting City 2 and City 3 is $3 \\cdot (2 + 3) = 15$. Thus the total cost is $2 + 10 + 15 = 27$. It can be shown that no configuration costs less than 27 yen."} +{"id": "01263", "text": "Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. \n\nDima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words, $\\frac{\\sum_{j = 1}^{m} a_{j}}{\\sum_{j = 1}^{m} b_{j}} = k$ , where a_{j} is the taste of the j-th chosen fruit and b_{j} is its calories.\n\nInna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem \u2014 now the happiness of a young couple is in your hands!\n\nInna loves Dima very much so she wants to make the salad from at least one fruit.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n, k (1 \u2264 n \u2264 100, 1 \u2264 k \u2264 10). The second line of the input contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100) \u2014 the fruits' tastes. The third line of the input contains n integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 100) \u2014 the fruits' calories. Fruit number i has taste a_{i} and calories b_{i}.\n\n\n-----Output-----\n\nIf there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer \u2014 the maximum possible sum of the taste values of the chosen fruits.\n\n\n-----Examples-----\nInput\n3 2\n10 8 1\n2 7 1\n\nOutput\n18\n\nInput\n5 3\n4 4 4 4 4\n2 2 2 2 2\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition $\\frac{18}{9} = 2 = k$ fulfills, that's exactly what Inna wants.\n\nIn the second test sample we cannot choose the fruits so as to follow Inna's principle."} +{"id": "01264", "text": "Iahub got bored, so he invented a game to be played on paper. \n\nHe writes n integers a_1, a_2, ..., a_{n}. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices i and j (1 \u2264 i \u2264 j \u2264 n) and flips all values a_{k} for which their positions are in range [i, j] (that is i \u2264 k \u2264 j). Flip the value of x means to apply operation x = 1 - x.\n\nThe goal of the game is that after exactly one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 100). In the second line of the input there are n integers: a_1, a_2, ..., a_{n}. It is guaranteed that each of those n values is either 0 or 1.\n\n\n-----Output-----\n\nPrint an integer \u2014 the maximal number of 1s that can be obtained after exactly one move. \n\n\n-----Examples-----\nInput\n5\n1 0 0 1 0\n\nOutput\n4\n\nInput\n4\n1 0 0 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first case, flip the segment from 2 to 5 (i = 2, j = 5). That flip changes the sequence, it becomes: [1 1 1 0 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1].\n\nIn the second case, flipping only the second and the third element (i = 2, j = 3) will turn all numbers into 1."} +{"id": "01265", "text": "The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string.\n\nA Bitlandish string is a string made only of characters \"0\" and \"1\".\n\nBitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish string a, and applies several (possibly zero) operations to it. In one operation the mayor may take any two adjacent characters of a string, define one of them as x and the other one as y. Then he calculates two values p and q: p = x\u00a0xor\u00a0y, q = x\u00a0or\u00a0y. Then he replaces one of the two taken characters by p and the other one by q.\n\nThe xor operation means the bitwise excluding OR operation. The or operation is the bitwise OR operation.\n\nSo for example one operation can transform string 11 to string 10 or to string 01. String 1 cannot be transformed into any other string.\n\nYou've got two Bitlandish strings a and b. Your task is to check if it is possible for BitHaval to transform string a to string b in several (possibly zero) described operations.\n\n\n-----Input-----\n\nThe first line contains Bitlandish string a, the second line contains Bitlandish string b. The strings can have different lengths.\n\nIt is guaranteed that the given strings only consist of characters \"0\" and \"1\". The strings are not empty, their length doesn't exceed 10^6.\n\n\n-----Output-----\n\nPrint \"YES\" if a can be transformed into b, otherwise print \"NO\". Please do not print the quotes.\n\n\n-----Examples-----\nInput\n11\n10\n\nOutput\nYES\n\nInput\n1\n01\n\nOutput\nNO\n\nInput\n000\n101\n\nOutput\nNO"} +{"id": "01266", "text": "Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead.\n\nThe first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help.\n\nConsider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. \n\nHelp Anton and write the program that for the given position determines whether the white king is in check.\n\nRemainder, on how do chess pieces move: Bishop moves any number of cells diagonally, but it can't \"leap\" over the occupied cells. Rook moves any number of cells horizontally or vertically, but it also can't \"leap\" over the occupied cells. Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't \"leap\". \n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 500 000)\u00a0\u2014 the number of black pieces.\n\nThe second line contains two integers x_0 and y_0 ( - 10^9 \u2264 x_0, y_0 \u2264 10^9)\u00a0\u2014 coordinates of the white king.\n\nThen follow n lines, each of them contains a character and two integers x_{i} and y_{i} ( - 10^9 \u2264 x_{i}, y_{i} \u2264 10^9)\u00a0\u2014 type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position.\n\n\n-----Output-----\n\nThe only line of the output should contains \"YES\" (without quotes) if the white king is in check and \"NO\" (without quotes) otherwise.\n\n\n-----Examples-----\nInput\n2\n4 2\nR 1 1\nB 1 5\n\nOutput\nYES\n\nInput\n2\n4 2\nR 3 3\nB 1 5\n\nOutput\nNO\n\n\n\n-----Note-----\n\nPicture for the first sample: [Image] White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is \"YES\".\n\nPicture for the second sample: [Image] Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant \"leap\" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is \"NO\"."} +{"id": "01267", "text": "The recent All-Berland Olympiad in Informatics featured n participants with each scoring a certain amount of points.\n\nAs the head of the programming committee, you are to determine the set of participants to be awarded with diplomas with respect to the following criteria: At least one participant should get a diploma. None of those with score equal to zero should get awarded. When someone is awarded, all participants with score not less than his score should also be awarded. \n\nDetermine the number of ways to choose a subset of participants that will receive the diplomas.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of participants.\n\nThe next line contains a sequence of n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 600)\u00a0\u2014 participants' scores.\n\nIt's guaranteed that at least one participant has non-zero score.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the desired number of ways.\n\n\n-----Examples-----\nInput\n4\n1 3 3 2\n\nOutput\n3\n\nInput\n3\n1 1 1\n\nOutput\n1\n\nInput\n4\n42 0 0 42\n\nOutput\n1\n\n\n\n-----Note-----\n\nThere are three ways to choose a subset in sample case one. Only participants with 3 points will get diplomas. Participants with 2 or 3 points will get diplomas. Everyone will get a diploma! \n\nThe only option in sample case two is to award everyone.\n\nNote that in sample case three participants with zero scores cannot get anything."} +{"id": "01268", "text": "Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola a_{i} and can's capacity b_{i} (a_{i} \u2264 b_{i}).\n\nJafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!\n\n\n-----Input-----\n\nThe first line of the input contains one integer n (2 \u2264 n \u2264 100 000)\u00a0\u2014 number of cola cans.\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9) \u2014 volume of remaining cola in cans.\n\nThe third line contains n space-separated integers that b_1, b_2, ..., b_{n} (a_{i} \u2264 b_{i} \u2264 10^9) \u2014 capacities of the cans.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print \"NO\" (without quotes).\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n2\n3 5\n3 6\n\nOutput\nYES\n\nInput\n3\n6 8 9\n6 10 12\n\nOutput\nNO\n\nInput\n5\n0 0 5 0 0\n1 1 8 10 5\n\nOutput\nYES\n\nInput\n4\n4 1 0 3\n5 2 2 3\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample, there are already 2 cans, so the answer is \"YES\"."} +{"id": "01269", "text": "This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of $m$ and the time limit. You need to solve both subtasks in order to hack this one.\n\nThere are $n+1$ distinct colours in the universe, numbered $0$ through $n$. There is a strip of paper $m$ centimetres long initially painted with colour $0$. \n\nAlice took a brush and painted the strip using the following process. For each $i$ from $1$ to $n$, in this order, she picks two integers $0 \\leq a_i < b_i \\leq m$, such that the segment $[a_i, b_i]$ is currently painted with a single colour, and repaints it with colour $i$. \n\nAlice chose the segments in such a way that each centimetre is now painted in some colour other than $0$. Formally, the segment $[i-1, i]$ is painted with colour $c_i$ ($c_i \\neq 0$). Every colour other than $0$ is visible on the strip.\n\nCount the number of different pairs of sequences $\\{a_i\\}_{i=1}^n$, $\\{b_i\\}_{i=1}^n$ that result in this configuration. \n\nSince this number may be large, output it modulo $998244353$.\n\n\n-----Input-----\n\nThe first line contains a two integers $n$, $m$ ($1 \\leq n \\leq 500$, $n = m$)\u00a0\u2014 the number of colours excluding the colour $0$ and the length of the paper, respectively.\n\nThe second line contains $m$ space separated integers $c_1, c_2, \\ldots, c_m$ ($1 \\leq c_i \\leq n$)\u00a0\u2014 the colour visible on the segment $[i-1, i]$ after the process ends. It is guaranteed that for all $j$ between $1$ and $n$ there is an index $k$ such that $c_k = j$.\n\nNote that since in this subtask $n = m$, this means that $c$ is a permutation of integers $1$ through $n$.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the number of ways Alice can perform the painting, modulo $998244353$.\n\n\n-----Examples-----\nInput\n3 3\n1 2 3\n\nOutput\n5\n\nInput\n7 7\n4 5 1 6 2 3 7\n\nOutput\n165\n\n\n\n-----Note-----\n\nIn the first example, there are $5$ ways, all depicted in the figure below. Here, $0$ is white, $1$ is red, $2$ is green and $3$ is blue.\n\n[Image]\n\nBelow is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour $2$.\n\n[Image]"} +{"id": "01270", "text": "Bachgold problem is very easy to formulate. Given a positive integer n represent it as a sum of maximum possible number of prime numbers. One can prove that such representation exists for any integer greater than 1.\n\nRecall that integer k is called prime if it is greater than 1 and has exactly two positive integer divisors\u00a0\u2014 1 and k. \n\n\n-----Input-----\n\nThe only line of the input contains a single integer n (2 \u2264 n \u2264 100 000).\n\n\n-----Output-----\n\nThe first line of the output contains a single integer k\u00a0\u2014 maximum possible number of primes in representation.\n\nThe second line should contain k primes with their sum equal to n. You can print them in any order. If there are several optimal solution, print any of them.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n2\n2 3\n\nInput\n6\n\nOutput\n3\n2 2 2"} +{"id": "01271", "text": "There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values \u200b\u200b\u2014 red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$).\n\nInitially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second.\n\nIf Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled.\n\nIt is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one.\n\nNote that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies.\n\nTanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $s$ and $k$ ($1 \\le n \\le 50$, $1 \\le s \\le n$, $1 \\le k \\le 2000$) \u2014 number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \\le r_i \\le 50$) \u2014 numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces.\n\n\n-----Output-----\n\nPrint minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print \"-1\".\n\n\n-----Examples-----\nInput\n5 3 10\n1 2 3 4 5\nRGBRR\n\nOutput\n4\n\nInput\n2 1 15\n5 6\nRG\n\nOutput\n-1\n\n\n\n-----Note-----\n\nThe sequence of actions of Tanya for the first example:\n\n move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. \n\nSince Tanya eats candy instantly, the required time is four seconds."} +{"id": "01272", "text": "There are N islands and M bridges.\nThe i-th bridge connects the A_i-th and B_i-th islands bidirectionally.\nInitially, we can travel between any two islands using some of these bridges.\nHowever, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge.\nLet the inconvenience be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining.\nFor each i (1 \\leq i \\leq M), find the inconvenience just after the i-th bridge collapses.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^5\n - 1 \\leq A_i < B_i \\leq N\n - All pairs (A_i, B_i) are distinct.\n - The inconvenience is initially 0.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 B_1\nA_2 B_2\n\\vdots\nA_M B_M\n\n-----Output-----\nIn the order i = 1, 2, ..., M, print the inconvenience just after the i-th bridge collapses.\nNote that the answer may not fit into a 32-bit integer type.\n\n-----Sample Input-----\n4 5\n1 2\n3 4\n1 3\n2 3\n1 4\n\n-----Sample Output-----\n0\n0\n4\n5\n6\n\nFor example, when the first to third bridges have collapsed, the inconvenience is 4 since we can no longer travel between the pairs (1, 2), (1, 3), (2, 4) and (3, 4)."} +{"id": "01273", "text": "Given is a tree G with N vertices.\nThe vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.\nConsider painting the edges in G with some number of colors.\nWe want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.\nAmong the colorings satisfying the condition above, construct one that uses the minimum number of colors.\n\n-----Constraints-----\n - 2 \\le N \\le 10^5\n - 1 \\le a_i \\lt b_i \\le N\n - All values in input are integers.\n - The given graph is a tree.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 b_1\na_2 b_2\n\\vdots\na_{N-1} b_{N-1}\n\n-----Output-----\nPrint N lines.\nThe first line should contain K, the number of colors used.\nThe (i+1)-th line (1 \\le i \\le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \\le c_i \\le K must hold.\nIf there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.\n\n-----Sample Input-----\n3\n1 2\n2 3\n\n-----Sample Output-----\n2\n1\n2\n"} +{"id": "01274", "text": "There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.\nYou can take and complete at most one of these jobs in a day.\nHowever, you cannot retake a job that you have already done.\nFind the maximum total reward that you can earn no later than M days from today.\nYou can already start working today.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^5\n - 1 \\leq A_i \\leq 10^5\n - 1 \\leq B_i \\leq 10^4\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 B_1\nA_2 B_2\n\\vdots\nA_N B_N\n\n-----Output-----\nPrint the maximum total reward that you can earn no later than M days from today.\n\n-----Sample Input-----\n3 4\n4 3\n4 1\n2 2\n\n-----Sample Output-----\n5\n\nYou can earn the total reward of 5 by taking the jobs as follows:\n - Take and complete the first job today. You will earn the reward of 3 after four days from today.\n - Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today."} +{"id": "01275", "text": "Given are integers N and K.\nHow many quadruples of integers (a,b,c,d) satisfy both of the following conditions?\n - 1 \\leq a,b,c,d \\leq N\n - a+b-c-d=K\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - -2(N-1) \\leq K \\leq 2(N-1)\n - All numbers in input are integers.\n\n-----Input-----\nInput is given from standard input in the following format:\nN K\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n2 1\n\n-----Sample Output-----\n4\n\nFour quadruples below satisfy the conditions:\n - (a,b,c,d)=(2,1,1,1)\n - (a,b,c,d)=(1,2,1,1)\n - (a,b,c,d)=(2,2,2,1)\n - (a,b,c,d)=(2,2,1,2)"} +{"id": "01276", "text": "We have a string S of length N consisting of R, G, and B.\nFind the number of triples (i,~j,~k)~(1 \\leq i < j < k \\leq N) that satisfy both of the following conditions:\n - S_i \\neq S_j, S_i \\neq S_k, and S_j \\neq S_k.\n - j - i \\neq k - j.\n\n-----Constraints-----\n - 1 \\leq N \\leq 4000\n - S is a string of length N consisting of R, G, and B.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS\n\n-----Output-----\nPrint the number of triplets in question.\n\n-----Sample Input-----\n4\nRRGB\n\n-----Sample Output-----\n1\n\nOnly the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count."} +{"id": "01277", "text": "We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally.\nTakahashi is standing at Vertex u, and Aoki is standing at Vertex v.\nNow, they will play a game of tag as follows:\n - 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex.\n - 2. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex.\n - 3. Go back to step 1.\nTakahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible.\nFind the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy.\nIt can be proved that the game is bound to end.\n\n-----Constraints-----\n - 2 \\leq N \\leq 10^5\n - 1 \\leq u,v \\leq N\n - u \\neq v\n - 1 \\leq A_i,B_i \\leq N\n - The given graph is a tree.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN u v\nA_1 B_1\n:\nA_{N-1} B_{N-1}\n\n-----Output-----\nPrint the number of moves Aoki will perform before the end of the game.\n\n-----Sample Input-----\n5 4 1\n1 2\n2 3\n3 4\n3 5\n\n-----Sample Output-----\n2\n\nIf both players play optimally, the game will progress as follows:\n - Takahashi moves to Vertex 3.\n - Aoki moves to Vertex 2.\n - Takahashi moves to Vertex 5.\n - Aoki moves to Vertex 3.\n - Takahashi moves to Vertex 3.\nHere, Aoki performs two moves.\nNote that, in each move, it is prohibited to stay at the current vertex."} +{"id": "01278", "text": "For years, the Day of city N was held in the most rainy day of summer. New mayor decided to break this tradition and select a not-so-rainy day for the celebration. The mayor knows the weather forecast for the $n$ days of summer. On the $i$-th day, $a_i$ millimeters of rain will fall. All values $a_i$ are distinct.\n\nThe mayor knows that citizens will watch the weather $x$ days before the celebration and $y$ days after. Because of that, he says that a day $d$ is not-so-rainy if $a_d$ is smaller than rain amounts at each of $x$ days before day $d$ and and each of $y$ days after day $d$. In other words, $a_d < a_j$ should hold for all $d - x \\le j < d$ and $d < j \\le d + y$. Citizens only watch the weather during summer, so we only consider such $j$ that $1 \\le j \\le n$.\n\nHelp mayor find the earliest not-so-rainy day of summer.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $x$ and $y$ ($1 \\le n \\le 100\\,000$, $0 \\le x, y \\le 7$)\u00a0\u2014 the number of days in summer, the number of days citizens watch the weather before the celebration and the number of days they do that after.\n\nThe second line contains $n$ distinct integers $a_1$, $a_2$, ..., $a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ denotes the rain amount on the $i$-th day.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the index of the earliest not-so-rainy day of summer. We can show that the answer always exists.\n\n\n-----Examples-----\nInput\n10 2 2\n10 9 6 7 8 3 2 1 4 5\n\nOutput\n3\n\nInput\n10 2 3\n10 9 6 7 8 3 2 1 4 5\n\nOutput\n8\n\nInput\n5 5 5\n100000 10000 1000 100 10\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example days $3$ and $8$ are not-so-rainy. The $3$-rd day is earlier.\n\nIn the second example day $3$ is not not-so-rainy, because $3 + y = 6$ and $a_3 > a_6$. Thus, day $8$ is the answer. Note that $8 + y = 11$, but we don't consider day $11$, because it is not summer."} +{"id": "01279", "text": "On a random day, Neko found $n$ treasure chests and $m$ keys. The $i$-th chest has an integer $a_i$ written on it and the $j$-th key has an integer $b_j$ on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.\n\nThe $j$-th key can be used to unlock the $i$-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, $a_i + b_j \\equiv 1 \\pmod{2}$. One key can be used to open at most one chest, and one chest can be opened at most once.\n\nFind the maximum number of chests Neko can open.\n\n\n-----Input-----\n\nThe first line contains integers $n$ and $m$ ($1 \\leq n, m \\leq 10^5$)\u00a0\u2014 the number of chests and the number of keys.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^9$)\u00a0\u2014 the numbers written on the treasure chests.\n\nThe third line contains $m$ integers $b_1, b_2, \\ldots, b_m$ ($1 \\leq b_i \\leq 10^9$)\u00a0\u2014 the numbers written on the keys.\n\n\n-----Output-----\n\nPrint the maximum number of chests you can open.\n\n\n-----Examples-----\nInput\n5 4\n9 14 6 2 11\n8 4 7 20\n\nOutput\n3\nInput\n5 1\n2 4 6 8 10\n5\n\nOutput\n1\nInput\n1 4\n10\n20 30 40 50\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first example, one possible way to unlock $3$ chests is as follows:\n\n Use first key to unlock the fifth chest, Use third key to unlock the second chest, Use fourth key to unlock the first chest. \n\nIn the second example, you can use the only key to unlock any single chest (note that one key can't be used twice).\n\nIn the third example, no key can unlock the given chest."} +{"id": "01280", "text": "You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.\n\nA substring s[l...r] (1 \u2264 l \u2264 r \u2264 |s|) of string s = s_1s_2...s_{|}s| (where |s| is the length of string s) is string s_{l}s_{l} + 1...s_{r}.\n\nThe substring s[l...r] is good, if among the letters s_{l}, s_{l} + 1, ..., s_{r} there are at most k bad ones (look at the sample's explanation to understand it more clear).\n\nYour task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] \u2260 s[p...q].\n\n\n-----Input-----\n\nThe first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.\n\nThe second line of the input is the string of characters \"0\" and \"1\", the length is exactly 26 characters. If the i-th character of this string equals \"1\", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter \"a\", the second one corresponds to letter \"b\" and so on.\n\nThe third line of the input consists a single integer k (0 \u2264 k \u2264 |s|) \u2014 the maximum acceptable number of bad characters in a good substring.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of distinct good substrings of string s.\n\n\n-----Examples-----\nInput\nababab\n01000000000000000000000000\n1\n\nOutput\n5\n\nInput\nacbacbacaa\n00000000000000000000000000\n2\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first example there are following good substrings: \"a\", \"ab\", \"b\", \"ba\", \"bab\".\n\nIn the second example there are following good substrings: \"a\", \"aa\", \"ac\", \"b\", \"ba\", \"c\", \"ca\", \"cb\"."} +{"id": "01281", "text": "At a break Vanya came to the class and saw an array of $n$ $k$-bit integers $a_1, a_2, \\ldots, a_n$ on the board. An integer $x$ is called a $k$-bit integer if $0 \\leq x \\leq 2^k - 1$. \n\nOf course, Vanya was not able to resist and started changing the numbers written on the board. To ensure that no one will note anything, Vanya allowed himself to make only one type of changes: choose an index of the array $i$ ($1 \\leq i \\leq n$) and replace the number $a_i$ with the number $\\overline{a_i}$. We define $\\overline{x}$ for a $k$-bit integer $x$ as the $k$-bit integer such that all its $k$ bits differ from the corresponding bits of $x$. \n\nVanya does not like the number $0$. Therefore, he likes such segments $[l, r]$ ($1 \\leq l \\leq r \\leq n$) such that $a_l \\oplus a_{l+1} \\oplus \\ldots \\oplus a_r \\neq 0$, where $\\oplus$ denotes the bitwise XOR operation. Determine the maximum number of segments he likes Vanya can get applying zero or more operations described above.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\leq n \\leq 200\\,000$, $1 \\leq k \\leq 30$).\n\nThe next line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\leq a_i \\leq 2^k - 1$), separated by spaces\u00a0\u2014 the array of $k$-bit integers.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the maximum possible number of segments with XOR not equal to $0$ that can be obtained by making several (possibly $0$) operations described in the statement.\n\n\n-----Examples-----\nInput\n3 2\n1 3 0\n\nOutput\n5\nInput\n6 3\n1 4 4 7 3 4\n\nOutput\n19\n\n\n-----Note-----\n\nIn the first example if Vasya does not perform any operations, he gets an array that has $5$ segments that Vanya likes. If he performs the operation with $i = 2$, he gets an array $[1, 0, 0]$, because $\\overline{3} = 0$ when $k = 2$. This array has $3$ segments that Vanya likes. Also, to get an array with $5$ segments that Vanya likes, he can perform two operations with $i = 3$ and with $i = 2$. He then gets an array $[1, 0, 3]$. It can be proven that he can't obtain $6$ or more segments that he likes.\n\nIn the second example, to get $19$ segments that Vanya likes, he can perform $4$ operations with $i = 3$, $i = 4$, $i = 5$, $i = 6$ and get an array $[1, 4, 3, 0, 4, 3]$."} +{"id": "01282", "text": "There are n schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes.\n\nEach second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginning of the line). In other words, if at some time the i-th position has a boy and the (i + 1)-th position has a girl, then in a second, the i-th position will have a girl and the (i + 1)-th one will have a boy.\n\nLet's take an example of a line of four people: a boy, a boy, a girl, a girl (from the beginning to the end of the line). Next second the line will look like that: a boy, a girl, a boy, a girl. Next second it will be a girl, a boy, a girl, a boy. Next second it will be a girl, a girl, a boy, a boy. The line won't change any more.\n\nYour task is: given the arrangement of the children in the line to determine the time needed to move all girls in front of boys (in the example above it takes 3 seconds). Baking buns takes a lot of time, so no one leaves the line until the line stops changing.\n\n\n-----Input-----\n\nThe first line contains a sequence of letters without spaces s_1s_2... s_{n} (1 \u2264 n \u2264 10^6), consisting of capital English letters M and F. If letter s_{i} equals M, that means that initially, the line had a boy on the i-th position. If letter s_{i} equals F, then initially the line had a girl on the i-th position.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of seconds needed to move all the girls in the line in front of the boys. If the line has only boys or only girls, print 0.\n\n\n-----Examples-----\nInput\nMFM\n\nOutput\n1\n\nInput\nMMFF\n\nOutput\n3\n\nInput\nFFMMM\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test case the sequence of changes looks as follows: MFM \u2192 FMM.\n\nThe second test sample corresponds to the sample from the statement. The sequence of changes is: MMFF \u2192 MFMF \u2192 FMFM \u2192 FFMM."} +{"id": "01283", "text": "Arkady is playing Battleship. The rules of this game aren't really important.\n\nThere is a field of $n \\times n$ cells. There should be exactly one $k$-decker on the field, i.\u00a0e. a ship that is $k$ cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship.\n\nConsider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 100$)\u00a0\u2014 the size of the field and the size of the ship.\n\nThe next $n$ lines contain the field. Each line contains $n$ characters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship).\n\n\n-----Output-----\n\nOutput two integers\u00a0\u2014 the row and the column of a cell that belongs to the maximum possible number of different locations of the ship.\n\nIf there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell.\n\n\n-----Examples-----\nInput\n4 3\n#..#\n#.#.\n....\n.###\n\nOutput\n3 2\n\nInput\n10 4\n#....##...\n.#...#....\n..#..#..#.\n...#.#....\n.#..##.#..\n.....#...#\n...#.##...\n.#...#.#..\n.....#..#.\n...#.#...#\n\nOutput\n6 1\n\nInput\n19 6\n##..............###\n#......#####.....##\n.....#########.....\n....###########....\n...#############...\n..###############..\n.#################.\n.#################.\n.#################.\n.#################.\n#####....##....####\n####............###\n####............###\n#####...####...####\n.#####..####..#####\n...###........###..\n....###########....\n.........##........\n#.................#\n\nOutput\n1 8\n\n\n\n-----Note-----\n\nThe picture below shows the three possible locations of the ship that contain the cell $(3, 2)$ in the first sample. [Image]"} +{"id": "01284", "text": "Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!\n\nYou are given $n$ nonnegative integers $a_1, a_2, \\dots, a_n$ arranged in a circle, where $n$ must be odd (ie. $n-1$ is divisible by $2$). Formally, for all $i$ such that $2 \\leq i \\leq n$, the elements $a_{i - 1}$ and $a_i$ are considered to be adjacent, and $a_n$ and $a_1$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.\n\nHelp Danny find the maximum possible circular value after some sequences of operations. \n\n\n-----Input-----\n\nThe first line contains one odd integer $n$ ($1 \\leq n < 2 \\cdot 10^5$, $n$ is odd) \u00a0\u2014 the initial size of the circle.\n\nThe second line contains $n$ integers $a_{1},a_{2},\\dots,a_{n}$ ($0 \\leq a_{i} \\leq 10^9$) \u00a0\u2014 the initial numbers in the circle.\n\n\n-----Output-----\n\nOutput the maximum possible circular value after applying some sequence of operations to the given circle.\n\n\n-----Examples-----\nInput\n3\n7 10 2\n\nOutput\n17\n\nInput\n1\n4\n\nOutput\n4\n\n\n\n-----Note-----\n\nFor the first test case, here's how a circular value of $17$ is obtained:\n\nPick the number at index $3$. The sum of adjacent elements equals $17$. Delete $7$ and $10$ from the circle and replace $2$ with $17$.\n\nNote that the answer may not fit in a $32$-bit integer."} +{"id": "01285", "text": "You are given a binary matrix $A$ of size $n \\times n$. Let's denote an $x$-compression of the given matrix as a matrix $B$ of size $\\frac{n}{x} \\times \\frac{n}{x}$ such that for every $i \\in [1, n], j \\in [1, n]$ the condition $A[i][j] = B[\\lceil \\frac{i}{x} \\rceil][\\lceil \\frac{j}{x} \\rceil]$ is met.\n\nObviously, $x$-compression is possible only if $x$ divides $n$, but this condition is not enough. For example, the following matrix of size $2 \\times 2$ does not have any $2$-compression:\n\n $01$ $10$ \n\nFor the given matrix $A$, find maximum $x$ such that an $x$-compression of this matrix is possible.\n\nNote that the input is given in compressed form. But even though it is compressed, you'd better use fast input.\n\n\n-----Input-----\n\nThe first line contains one number $n$ ($4 \\le n \\le 5200$) \u2014 the number of rows and columns in the matrix $A$. It is guaranteed that $n$ is divisible by $4$.\n\nThen the representation of matrix follows. Each of $n$ next lines contains $\\frac{n}{4}$ one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from $0$ to $9$ or as uppercase Latin letters from $A$ to $F$). Binary representation of each of these numbers denotes next $4$ elements of the matrix in the corresponding row. For example, if the number $B$ is given, then the corresponding elements are 1011, and if the number is $5$, then the corresponding elements are 0101.\n\nElements are not separated by whitespaces.\n\n\n-----Output-----\n\nPrint one number: maximum $x$ such that an $x$-compression of the given matrix is possible.\n\n\n-----Examples-----\nInput\n8\nE7\nE7\nE7\n00\n00\nE7\nE7\nE7\n\nOutput\n1\n\nInput\n4\n7\nF\nF\nF\n\nOutput\n1\n\n\n\n-----Note-----\n\nThe first example corresponds to the matrix: $11100111$ $11100111$ $11100111$ $00000000$ $00000000$ $11100111$ $11100111$ $11100111$ \n\nIt is easy to see that the answer on this example is $1$."} +{"id": "01286", "text": "As we all know Barney's job is \"PLEASE\" and he has not much to do at work. That's why he started playing \"cups and key\". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's heart is under the middle cup. $\\left. \\begin{array}{l}{\\text{Rey}} \\\\{\\text{to my}} \\\\{\\text{heart}} \\end{array} \\right.$ \n\nThen at one turn Barney swaps the cup in the middle with any of other two cups randomly (he choses each with equal probability), so the chosen cup becomes the middle one. Game lasts n turns and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it was at the start.\n\nAfter n-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney was distracted while making turns and doesn't know if the key is under the middle cup. That's why he asked you to tell him the probability that girl guessed right.\n\nNumber n of game turns can be extremely large, that's why Barney did not give it to you. Instead he gave you an array a_1, a_2, ..., a_{k} such that $n = \\prod_{i = 1}^{k} a_{i}$ \n\nin other words, n is multiplication of all elements of the given array.\n\nBecause of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In other words you need to find it as a fraction p / q such that $\\operatorname{gcd}(p, q) = 1$, where $gcd$ is the greatest common divisor. Since p and q can be extremely large, you only need to find the remainders of dividing each of them by 10^9 + 7.\n\nPlease note that we want $gcd$ of p and q to be 1, not $gcd$ of their remainders after dividing by 10^9 + 7.\n\n\n-----Input-----\n\nThe first line of input contains a single integer k (1 \u2264 k \u2264 10^5)\u00a0\u2014 the number of elements in array Barney gave you.\n\nThe second line contains k integers a_1, a_2, ..., a_{k} (1 \u2264 a_{i} \u2264 10^18)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nIn the only line of output print a single string x / y where x is the remainder of dividing p by 10^9 + 7 and y is the remainder of dividing q by 10^9 + 7.\n\n\n-----Examples-----\nInput\n1\n2\n\nOutput\n1/2\n\nInput\n3\n1 1 1\n\nOutput\n0/1"} +{"id": "01287", "text": "The rules of Sith Tournament are well known to everyone. n Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.\n\nJedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 18)\u00a0\u2014 the number of participants of the Sith Tournament.\n\nEach of the next n lines contains n real numbers, which form a matrix p_{ij} (0 \u2264 p_{ij} \u2264 1). Each its element p_{ij} is the probability that the i-th participant defeats the j-th in a duel.\n\nThe elements on the main diagonal p_{ii} are equal to zero. For all different i, j the equality p_{ij} + p_{ji} = 1 holds. All probabilities are given with no more than six decimal places.\n\nJedi Ivan is the number 1 in the list of the participants.\n\n\n-----Output-----\n\nOutput a real number\u00a0\u2014 the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n3\n0.0 0.5 0.8\n0.5 0.0 0.4\n0.2 0.6 0.0\n\nOutput\n0.680000000000000"} +{"id": "01288", "text": "Ashish has an array $a$ of size $n$.\n\nA subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.\n\nConsider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. \n\nNote that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \\ldots), max(s_2, s_4, s_6, \\ldots))$.\n\nFor example, the cost of $\\{7, 5, 6\\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.\n\nHelp him find the minimum cost of a subsequence of size $k$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($2 \\leq k \\leq n \\leq 2 \\cdot 10^5$) \u00a0\u2014 the size of the array $a$ and the size of the subsequence.\n\nThe next line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^9$) \u00a0\u2014 the elements of the array $a$.\n\n\n-----Output-----\n\nOutput a single integer \u00a0\u2014 the minimum cost of a subsequence of size $k$.\n\n\n-----Examples-----\nInput\n4 2\n1 2 3 4\n\nOutput\n1\nInput\n4 3\n1 2 3 4\n\nOutput\n2\nInput\n5 3\n5 3 4 2 6\n\nOutput\n2\nInput\n6 4\n5 3 50 2 4 5\n\nOutput\n3\n\n\n-----Note-----\n\nIn the first test, consider the subsequence $s$ = $\\{1, 3\\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.\n\nIn the second test, consider the subsequence $s$ = $\\{1, 2, 4\\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.\n\nIn the fourth test, consider the subsequence $s$ = $\\{3, 50, 2, 4\\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$."} +{"id": "01289", "text": "In Berland a bus travels along the main street of the capital. The street begins from the main square and looks like a very long segment. There are n bus stops located along the street, the i-th of them is located at the distance a_{i} from the central square, all distances are distinct, the stops are numbered in the order of increasing distance from the square, that is, a_{i} < a_{i} + 1 for all i from 1 to n - 1. The bus starts its journey from the first stop, it passes stops 2, 3 and so on. It reaches the stop number n, turns around and goes in the opposite direction to stop 1, passing all the intermediate stops in the reverse order. After that, it again starts to move towards stop n. During the day, the bus runs non-stop on this route.\n\nThe bus is equipped with the Berland local positioning system. When the bus passes a stop, the system notes down its number.\n\nOne of the key features of the system is that it can respond to the queries about the distance covered by the bus for the parts of its path between some pair of stops. A special module of the system takes the input with the information about a set of stops on a segment of the path, a stop number occurs in the set as many times as the bus drove past it. This module returns the length of the traveled segment of the path (or -1 if it is impossible to determine the length uniquely). The operation of the module is complicated by the fact that stop numbers occur in the request not in the order they were visited but in the non-decreasing order.\n\nFor example, if the number of stops is 6, and the part of the bus path starts at the bus stop number 5, ends at the stop number 3 and passes the stops as follows: $5 \\rightarrow 6 \\rightarrow 5 \\rightarrow 4 \\rightarrow 3$, then the request about this segment of the path will have form: 3, 4, 5, 5, 6. If the bus on the segment of the path from stop 5 to stop 3 has time to drive past the 1-th stop (i.e., if we consider a segment that ends with the second visit to stop 3 on the way from 5), then the request will have form: 1, 2, 2, 3, 3, 4, 5, 5, 6.\n\nYou will have to repeat the Berland programmers achievement and implement this function.\n\n\n-----Input-----\n\nThe first line contains integer n (2 \u2264 n \u2264 2\u00b710^5) \u2014 the number of stops.\n\nThe second line contains n integers (1 \u2264 a_{i} \u2264 10^9) \u2014 the distance from the i-th stop to the central square. The numbers in the second line go in the increasing order.\n\nThe third line contains integer m (1 \u2264 m \u2264 4\u00b710^5) \u2014 the number of stops the bus visited on some segment of the path.\n\nThe fourth line contains m integers (1 \u2264 b_{i} \u2264 n) \u2014 the sorted list of numbers of the stops visited by the bus on the segment of the path. The number of a stop occurs as many times as it was visited by a bus.\n\nIt is guaranteed that the query corresponds to some segment of the path.\n\n\n-----Output-----\n\nIn the single line please print the distance covered by a bus. If it is impossible to determine it unambiguously, print - 1.\n\n\n-----Examples-----\nInput\n6\n2 3 5 7 11 13\n5\n3 4 5 5 6\n\nOutput\n10\n\nInput\n6\n2 3 5 7 11 13\n9\n1 2 2 3 3 4 5 5 6\n\nOutput\n16\n\nInput\n3\n10 200 300\n4\n1 2 2 3\n\nOutput\n-1\n\nInput\n3\n1 2 3\n4\n1 2 2 3\n\nOutput\n3\n\n\n\n-----Note-----\n\nThe first test from the statement demonstrates the first example shown in the statement of the problem.\n\nThe second test from the statement demonstrates the second example shown in the statement of the problem.\n\nIn the third sample there are two possible paths that have distinct lengths, consequently, the sought length of the segment isn't defined uniquely.\n\nIn the fourth sample, even though two distinct paths correspond to the query, they have the same lengths, so the sought length of the segment is defined uniquely."} +{"id": "01290", "text": "You are given a following process. \n\nThere is a platform with $n$ columns. $1 \\times 1$ squares are appearing one after another in some columns on this platform. If there are no squares in the column, a square will occupy the bottom row. Otherwise a square will appear at the top of the highest square of this column. \n\nWhen all of the $n$ columns have at least one square in them, the bottom row is being removed. You will receive $1$ point for this, and all the squares left will fall down one row. \n\nYou task is to calculate the amount of points you will receive.\n\n\n-----Input-----\n\nThe first line of input contain 2 integer numbers $n$ and $m$ ($1 \\le n, m \\le 1000$) \u2014 the length of the platform and the number of the squares.\n\nThe next line contain $m$ integer numbers $c_1, c_2, \\dots, c_m$ ($1 \\le c_i \\le n$) \u2014 column in which $i$-th square will appear.\n\n\n-----Output-----\n\nPrint one integer \u2014 the amount of points you will receive.\n\n\n-----Example-----\nInput\n3 9\n1 1 2 2 2 3 1 2 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the sample case the answer will be equal to $2$ because after the appearing of $6$-th square will be removed one row (counts of the squares on the platform will look like $[2~ 3~ 1]$, and after removing one row will be $[1~ 2~ 0]$).\n\nAfter the appearing of $9$-th square counts will be $[2~ 3~ 1]$, and after removing one row it will look like $[1~ 2~ 0]$.\n\nSo the answer will be equal to $2$."} +{"id": "01291", "text": "Berlanders like to eat cones after a hard day. Misha Square and Sasha Circle are local authorities of Berland. Each of them controls its points of cone trade. Misha has n points, Sasha \u2014 m. Since their subordinates constantly had conflicts with each other, they decided to build a fence in the form of a circle, so that the points of trade of one businessman are strictly inside a circle, and points of the other one are strictly outside. It doesn't matter which of the two gentlemen will have his trade points inside the circle.\n\nDetermine whether they can build a fence or not.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 10000), numbers of Misha's and Sasha's trade points respectively.\n\nThe next n lines contains pairs of space-separated integers M_{x}, M_{y} ( - 10^4 \u2264 M_{x}, M_{y} \u2264 10^4), coordinates of Misha's trade points.\n\nThe next m lines contains pairs of space-separated integers S_{x}, S_{y} ( - 10^4 \u2264 S_{x}, S_{y} \u2264 10^4), coordinates of Sasha's trade points.\n\nIt is guaranteed that all n + m points are distinct.\n\n\n-----Output-----\n\nThe only output line should contain either word \"YES\" without quotes in case it is possible to build a such fence or word \"NO\" in the other case.\n\n\n-----Examples-----\nInput\n2 2\n-1 0\n1 0\n0 -1\n0 1\n\nOutput\nNO\n\nInput\n4 4\n1 0\n0 1\n-1 0\n0 -1\n1 1\n-1 1\n-1 -1\n1 -1\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample there is no possibility to separate points, because any circle that contains both points ( - 1, 0), (1, 0) also contains at least one point from the set (0, - 1), (0, 1), and vice-versa: any circle that contains both points (0, - 1), (0, 1) also contains at least one point from the set ( - 1, 0), (1, 0)\n\nIn the second sample one of the possible solution is shown below. Misha's points are marked with red colour and Sasha's are marked with blue. [Image]"} +{"id": "01292", "text": "Kilani is playing a game with his friends. This game can be represented as a grid of size $n \\times m$, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).\n\nThe game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player $i$ can expand from a cell with his castle to the empty cell if it's possible to reach it in at most $s_i$ (where $s_i$ is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. \n\nThe game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $p$ ($1 \\le n, m \\le 1000$, $1 \\le p \\le 9$)\u00a0\u2014 the size of the grid and the number of players.\n\nThe second line contains $p$ integers $s_i$ ($1 \\le s \\le 10^9$)\u00a0\u2014 the speed of the expansion for every player.\n\nThe following $n$ lines describe the game grid. Each of them consists of $m$ symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit $x$ ($1 \\le x \\le p$) denotes the castle owned by player $x$.\n\nIt is guaranteed, that each player has at least one castle on the grid.\n\n\n-----Output-----\n\nPrint $p$ integers\u00a0\u2014 the number of cells controlled by each player after the game ends.\n\n\n-----Examples-----\nInput\n3 3 2\n1 1\n1..\n...\n..2\n\nOutput\n6 3 \n\nInput\n3 4 4\n1 1 1 1\n....\n#...\n1234\n\nOutput\n1 4 3 3 \n\n\n\n-----Note-----\n\nThe picture below show the game before it started, the game after the first round and game after the second round in the first example:\n\n [Image] \n\nIn the second example, the first player is \"blocked\" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left."} +{"id": "01293", "text": "Wilbur the pig is tinkering with arrays again. He has the array a_1, a_2, ..., a_{n} initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elements a_{i}, a_{i} + 1, ... , a_{n} or subtract 1 from all elements a_{i}, a_{i} + 1, ..., a_{n}. His goal is to end up with the array b_1, b_2, ..., b_{n}. \n\nOf course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the length of the array a_{i}. Initially a_{i} = 0 for every position i, so this array is not given in the input.\n\nThe second line of the input contains n integers b_1, b_2, ..., b_{n} ( - 10^9 \u2264 b_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint the minimum number of steps that Wilbur needs to make in order to achieve a_{i} = b_{i} for all i.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n\nOutput\n5\nInput\n4\n1 2 2 1\n\nOutput\n3\n\n\n-----Note-----\n\nIn the first sample, Wilbur may successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes.\n\nIn the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract 1."} +{"id": "01294", "text": "Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains $26$ buttons (one for each letter of the Latin alphabet). Each button is either working fine or malfunctioning. \n\nTo check which buttons need replacement, Polycarp pressed some buttons in sequence, and a string $s$ appeared on the screen. When Polycarp presses a button with character $c$, one of the following events happened:\n\n if the button was working correctly, a character $c$ appeared at the end of the string Polycarp was typing; if the button was malfunctioning, two characters $c$ appeared at the end of the string. \n\nFor example, suppose the buttons corresponding to characters a and c are working correctly, and the button corresponding to b is malfunctioning. If Polycarp presses the buttons in the order a, b, a, c, a, b, a, then the string he is typing changes as follows: a $\\rightarrow$ abb $\\rightarrow$ abba $\\rightarrow$ abbac $\\rightarrow$ abbaca $\\rightarrow$ abbacabb $\\rightarrow$ abbacabba.\n\nYou are given a string $s$ which appeared on the screen after Polycarp pressed some buttons. Help Polycarp to determine which buttons are working correctly for sure (that is, this string could not appear on the screen if any of these buttons was malfunctioning).\n\nYou may assume that the buttons don't start malfunctioning when Polycarp types the string: each button either works correctly throughout the whole process, or malfunctions throughout the whole process.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases in the input.\n\nThen the test cases follow. Each test case is represented by one line containing a string $s$ consisting of no less than $1$ and no more than $500$ lowercase Latin letters.\n\n\n-----Output-----\n\nFor each test case, print one line containing a string $res$. The string $res$ should contain all characters which correspond to buttons that work correctly in alphabetical order, without any separators or repetitions. If all buttons may malfunction, $res$ should be empty.\n\n\n-----Example-----\nInput\n4\na\nzzaaz\nccff\ncbddbb\n\nOutput\na\nz\n\nbc"} +{"id": "01295", "text": "You are given n points on the straight line \u2014 the positions (x-coordinates) of the cities and m points on the same line \u2014 the positions (x-coordinates) of the cellular towers. All towers work in the same way \u2014 they provide cellular network for all cities, which are located at the distance which is no more than r from this tower.\n\nYour task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.\n\nIf r = 0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower.\n\n\n-----Input-----\n\nThe first line contains two positive integers n and m (1 \u2264 n, m \u2264 10^5) \u2014 the number of cities and the number of cellular towers.\n\nThe second line contains a sequence of n integers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9) \u2014 the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates a_{i} are given in non-decreasing order.\n\nThe third line contains a sequence of m integers b_1, b_2, ..., b_{m} ( - 10^9 \u2264 b_{j} \u2264 10^9) \u2014 the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates b_{j} are given in non-decreasing order.\n\n\n-----Output-----\n\nPrint minimal r so that each city will be covered by cellular network.\n\n\n-----Examples-----\nInput\n3 2\n-2 2 4\n-3 0\n\nOutput\n4\n\nInput\n5 3\n1 5 10 14 17\n4 11 15\n\nOutput\n3"} +{"id": "01296", "text": "On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost a_{i} Egyptian pounds. If Sagheer buys k items with indices x_1, x_2, ..., x_{k}, then the cost of item x_{j} is a_{x}_{j} + x_{j}\u00b7k for 1 \u2264 j \u2264 k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.\n\nSagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?\n\n\n-----Input-----\n\nThe first line contains two integers n and S (1 \u2264 n \u2264 10^5 and 1 \u2264 S \u2264 10^9)\u00a0\u2014 the number of souvenirs in the market and Sagheer's budget.\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5)\u00a0\u2014 the base costs of the souvenirs.\n\n\n-----Output-----\n\nOn a single line, print two integers k, T\u00a0\u2014 the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.\n\n\n-----Examples-----\nInput\n3 11\n2 3 5\n\nOutput\n2 11\n\nInput\n4 100\n1 2 5 6\n\nOutput\n4 54\n\nInput\n1 7\n7\n\nOutput\n0 0\n\n\n\n-----Note-----\n\nIn the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.\n\nIn the second example, he can buy all items as they will cost him [5, 10, 17, 22].\n\nIn the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it."} +{"id": "01297", "text": "You will receive 3 points for solving this problem.\n\nManao is designing the genetic code for a new type of algae to efficiently produce fuel. Specifically, Manao is focusing on a stretch of DNA that encodes one protein. The stretch of DNA is represented by a string containing only the characters 'A', 'T', 'G' and 'C'.\n\nManao has determined that if the stretch of DNA contains a maximal sequence of consecutive identical nucleotides that is of even length, then the protein will be nonfunctional. For example, consider a protein described by DNA string \"GTTAAAG\". It contains four maximal sequences of consecutive identical nucleotides: \"G\", \"TT\", \"AAA\", and \"G\". The protein is nonfunctional because sequence \"TT\" has even length.\n\nManao is trying to obtain a functional protein from the protein he currently has. Manao can insert additional nucleotides into the DNA stretch. Each additional nucleotide is a character from the set {'A', 'T', 'G', 'C'}. Manao wants to determine the minimum number of insertions necessary to make the DNA encode a functional protein.\n\n\n-----Input-----\n\nThe input consists of a single line, containing a string s of length n (1 \u2264 n \u2264 100). Each character of s will be from the set {'A', 'T', 'G', 'C'}.\n\nThis problem doesn't have subproblems. You will get 3 points for the correct submission.\n\n\n-----Output-----\n\nThe program should print on one line a single integer representing the minimum number of 'A', 'T', 'G', 'C' characters that are required to be inserted into the input string in order to make all runs of identical characters have odd length.\n\n\n-----Examples-----\nInput\nGTTAAAG\n\nOutput\n1\n\nInput\nAACCAACCAAAAC\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example, it is sufficient to insert a single nucleotide of any type between the two 'T's in the sequence to restore the functionality of the protein."} +{"id": "01298", "text": "Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.\n\nOnce he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.\n\nNow Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.\n\n\n-----Input-----\n\nFirst line of the input contains a single integer n (1 \u2264 n \u2264 2\u00b710^5), the length of the string that Andreid has.\n\nThe second line contains the string of length n consisting only from zeros and ones.\n\n\n-----Output-----\n\nOutput the minimum length of the string that may remain after applying the described operations several times.\n\n\n-----Examples-----\nInput\n4\n1100\n\nOutput\n0\n\nInput\n5\n01010\n\nOutput\n1\n\nInput\n8\n11101111\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first sample test it is possible to change the string like the following: $1100 \\rightarrow 10 \\rightarrow(\\text{empty})$.\n\nIn the second sample test it is possible to change the string like the following: $01010 \\rightarrow 010 \\rightarrow 0$.\n\nIn the third sample test it is possible to change the string like the following: $11101111 \\rightarrow 111111$."} +{"id": "01299", "text": "Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.\n\nThis time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b (1 \u2264 a \u2264 b \u2264 n - k + 1, b - a \u2265 k) and sign all laws with numbers lying in the segments [a;\u00a0a + k - 1] and [b;\u00a0b + k - 1] (borders are included).\n\nAs mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (2 \u2264 n \u2264 2\u00b710^5, 0 < 2k \u2264 n) \u2014 the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x_1, x_2, ..., x_{n} \u2014 the absurdity of each law (1 \u2264 x_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint two integers a, b \u2014 the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a;\u00a0a + k - 1] and [b;\u00a0b + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b.\n\n\n-----Examples-----\nInput\n5 2\n3 6 1 1 6\n\nOutput\n1 4\n\nInput\n6 2\n1 1 1 1 1 1\n\nOutput\n1 3\n\n\n\n-----Note-----\n\nIn the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16.\n\nIn the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 4."} +{"id": "01300", "text": "You are given array $a$ of length $n$. You can choose one segment $[l, r]$ ($1 \\le l \\le r \\le n$) and integer value $k$ (positive, negative or even zero) and change $a_l, a_{l + 1}, \\dots, a_r$ by $k$ each (i.e. $a_i := a_i + k$ for each $l \\le i \\le r$).\n\nWhat is the maximum possible number of elements with value $c$ that can be obtained after one such operation?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $c$ ($1 \\le n \\le 5 \\cdot 10^5$, $1 \\le c \\le 5 \\cdot 10^5$) \u2014 the length of array and the value $c$ to obtain.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 5 \\cdot 10^5$) \u2014 array $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible number of elements with value $c$ which can be obtained after performing operation described above.\n\n\n-----Examples-----\nInput\n6 9\n9 9 9 9 9 9\n\nOutput\n6\n\nInput\n3 2\n6 2 6\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example we can choose any segment and $k = 0$. The array will stay same.\n\nIn the second example we can choose segment $[1, 3]$ and $k = -4$. The array will become $[2, -2, 2]$."} +{"id": "01301", "text": "You are solving the crossword problem K from IPSC 2014. You solved all the clues except for one: who does Eevee evolve into? You are not very into pokemons, but quick googling helped you find out, that Eevee can evolve into eight different pokemons: Vaporeon, Jolteon, Flareon, Espeon, Umbreon, Leafeon, Glaceon, and Sylveon.\n\nYou know the length of the word in the crossword, and you already know some letters. Designers of the crossword made sure that the answer is unambiguous, so you can assume that exactly one pokemon out of the 8 that Eevee evolves into fits the length and the letters given. Your task is to find it.\n\n\n-----Input-----\n\nFirst line contains an integer n (6 \u2264 n \u2264 8) \u2013 the length of the string.\n\nNext line contains a string consisting of n characters, each of which is either a lower case english letter (indicating a known letter) or a dot character (indicating an empty cell in the crossword).\n\n\n-----Output-----\n\nPrint a name of the pokemon that Eevee can evolve into that matches the pattern in the input. Use lower case letters only to print the name (in particular, do not capitalize the first letter).\n\n\n-----Examples-----\nInput\n7\nj......\n\nOutput\njolteon\n\nInput\n7\n...feon\n\nOutput\nleafeon\n\nInput\n7\n.l.r.o.\n\nOutput\nflareon\n\n\n\n-----Note-----\n\nHere's a set of names in a form you can paste into your solution:\n\n[\"vaporeon\", \"jolteon\", \"flareon\", \"espeon\", \"umbreon\", \"leafeon\", \"glaceon\", \"sylveon\"]\n\n{\"vaporeon\", \"jolteon\", \"flareon\", \"espeon\", \"umbreon\", \"leafeon\", \"glaceon\", \"sylveon\"}"} +{"id": "01302", "text": "Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n.\n\nLet\u2019s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element p_{i} of permutation p_1, p_2, ... , p_{n} is good if gcd(i, p_{i}) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn\u2019t know any beautiful permutation. Your task is to help him to find at least one of them.\n\n\n-----Input-----\n\nThe single line contains two integers n and k (1 \u2264 n \u2264 10^5, 0 \u2264 k \u2264 n).\n\n\n-----Output-----\n\nIn a single line print either any beautiful permutation or -1, if such permutation doesn\u2019t exist.\n\nIf there are multiple suitable permutations, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n4 2\n\nOutput\n2 4 3 1\nInput\n1 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful.\n\nThe second sample has no beautiful permutations."} +{"id": "01303", "text": "Little X and Little Z are good friends. They always chat online. But both of them have schedules.\n\nLittle Z has fixed schedule. He always online at any moment of time between a_1 and b_1, between a_2 and b_2, ..., between a_{p} and b_{p} (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c_1 and d_1, between c_2 and d_2, ..., between c_{q} and d_{q} (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [c_{i} + t, d_{i} + t] (for all i).\n\nIf at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?\n\n\n-----Input-----\n\nThe first line contains four space-separated integers p, q, l, r (1 \u2264 p, q \u2264 50;\u00a00 \u2264 l \u2264 r \u2264 1000).\n\nEach of the next p lines contains two space-separated integers a_{i}, b_{i} (0 \u2264 a_{i} < b_{i} \u2264 1000). Each of the next q lines contains two space-separated integers c_{j}, d_{j} (0 \u2264 c_{j} < d_{j} \u2264 1000).\n\nIt's guaranteed that b_{i} < a_{i} + 1 and d_{j} < c_{j} + 1 for all valid i and j.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the number of moments of time from the segment [l, r] which suit for online conversation.\n\n\n-----Examples-----\nInput\n1 1 0 4\n2 3\n0 1\n\nOutput\n3\n\nInput\n2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17\n\nOutput\n20"} +{"id": "01304", "text": "Two bears are playing tic-tac-toe via mail. It's boring for them to play usual tic-tac-toe game, so they are a playing modified version of this game. Here are its rules.\n\nThe game is played on the following field. [Image] \n\nPlayers are making moves by turns. At first move a player can put his chip in any cell of any small field. For following moves, there are some restrictions: if during last move the opposite player put his chip to cell with coordinates (x_{l}, y_{l}) in some small field, the next move should be done in one of the cells of the small field with coordinates (x_{l}, y_{l}). For example, if in the first move a player puts his chip to lower left cell of central field, then the second player on his next move should put his chip into some cell of lower left field (pay attention to the first test case). If there are no free cells in the required field, the player can put his chip to any empty cell on any field.\n\nYou are given current state of the game and coordinates of cell in which the last move was done. You should find all cells in which the current player can put his chip.\n\nA hare works as a postman in the forest, he likes to foul bears. Sometimes he changes the game field a bit, so the current state of the game could be unreachable. However, after his changes the cell where the last move was done is not empty. You don't need to find if the state is unreachable or not, just output possible next moves according to the rules.\n\n\n-----Input-----\n\nFirst 11 lines contains descriptions of table with 9 rows and 9 columns which are divided into 9 small fields by spaces and empty lines. Each small field is described by 9 characters without spaces and empty lines. character \"x\" (ASCII-code 120) means that the cell is occupied with chip of the first player, character \"o\" (ASCII-code 111) denotes a field occupied with chip of the second player, character \".\" (ASCII-code 46) describes empty cell.\n\nThe line after the table contains two integers x and y (1 \u2264 x, y \u2264 9). They describe coordinates of the cell in table where the last move was done. Rows in the table are numbered from up to down and columns are numbered from left to right.\n\nIt's guaranteed that cell where the last move was done is filled with \"x\" or \"o\". Also, it's guaranteed that there is at least one empty cell. It's not guaranteed that current state of game is reachable.\n\n\n-----Output-----\n\nOutput the field in same format with characters \"!\" (ASCII-code 33) on positions where the current player can put his chip. All other cells should not be modified.\n\n\n-----Examples-----\nInput\n... ... ...\n... ... ...\n... ... ...\n\n... ... ...\n... ... ...\n... x.. ...\n\n... ... ...\n... ... ...\n... ... ...\n6 4\n\nOutput\n... ... ... \n... ... ... \n... ... ... \n\n... ... ... \n... ... ... \n... x.. ... \n\n!!! ... ... \n!!! ... ... \n!!! ... ... \n\n\nInput\nxoo x.. x..\nooo ... ...\nooo ... ...\n\nx.. x.. x..\n... ... ...\n... ... ...\n\nx.. x.. x..\n... ... ...\n... ... ...\n7 4\n\nOutput\nxoo x!! x!! \nooo !!! !!! \nooo !!! !!! \n\nx!! x!! x!! \n!!! !!! !!! \n!!! !!! !!! \n\nx!! x!! x!! \n!!! !!! !!! \n!!! !!! !!! \n\n\nInput\no.. ... ...\n... ... ...\n... ... ...\n\n... xxx ...\n... xox ...\n... ooo ...\n\n... ... ...\n... ... ...\n... ... ...\n5 5\n\nOutput\no!! !!! !!! \n!!! !!! !!! \n!!! !!! !!! \n\n!!! xxx !!! \n!!! xox !!! \n!!! ooo !!! \n\n!!! !!! !!! \n!!! !!! !!! \n!!! !!! !!! \n\n\n\n\n-----Note-----\n\nIn the first test case the first player made a move to lower left cell of central field, so the second player can put a chip only to cells of lower left field.\n\nIn the second test case the last move was done to upper left cell of lower central field, however all cells in upper left field are occupied, so the second player can put his chip to any empty cell.\n\nIn the third test case the last move was done to central cell of central field, so current player can put his chip to any cell of central field, which is already occupied, so he can move anywhere. Pay attention that this state of the game is unreachable."} +{"id": "01305", "text": "The new \"Die Hard\" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A \"Die Hard\" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 \u2014 the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.\n\n\n-----Output-----\n\nPrint \"YES\" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print \"NO\".\n\n\n-----Examples-----\nInput\n4\n25 25 50 50\n\nOutput\nYES\n\nInput\n2\n25 100\n\nOutput\nNO\n\nInput\n4\n50 50 25 25\n\nOutput\nNO"} +{"id": "01306", "text": "Peter has a sequence of integers a_1, a_2, ..., a_{n}. Peter wants all numbers in the sequence to equal h. He can perform the operation of \"adding one on the segment [l, r]\": add one to all elements of the sequence with indices from l to r (inclusive). At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments [l_1, r_1] and [l_2, r_2], where Peter added one, the following inequalities hold: l_1 \u2260 l_2 and r_1 \u2260 r_2.\n\nHow many distinct ways are there to make all numbers in the sequence equal h? Print this number of ways modulo 1000000007\u00a0(10^9 + 7). Two ways are considered distinct if one of them has a segment that isn't in the other way.\n\n\n-----Input-----\n\nThe first line contains two integers n, h (1 \u2264 n, h \u2264 2000). The next line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 2000).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem modulo 1000000007\u00a0(10^9 + 7).\n\n\n-----Examples-----\nInput\n3 2\n1 1 1\n\nOutput\n4\n\nInput\n5 1\n1 1 1 1 1\n\nOutput\n1\n\nInput\n4 3\n3 2 1 1\n\nOutput\n0"} +{"id": "01307", "text": "Mahmoud and Ehab play a game called the even-odd game. Ehab chooses his favorite integer n and then they take turns, starting from Mahmoud. In each player's turn, he has to choose an integer a and subtract it from n such that: 1 \u2264 a \u2264 n. If it's Mahmoud's turn, a has to be even, but if it's Ehab's turn, a has to be odd. \n\nIf the current player can't choose any number satisfying the conditions, he loses. Can you determine the winner if they both play optimally?\n\n\n-----Input-----\n\nThe only line contains an integer n (1 \u2264 n \u2264 10^9), the number at the beginning of the game.\n\n\n-----Output-----\n\nOutput \"Mahmoud\" (without quotes) if Mahmoud wins and \"Ehab\" (without quotes) otherwise.\n\n\n-----Examples-----\nInput\n1\n\nOutput\nEhab\nInput\n2\n\nOutput\nMahmoud\n\n\n-----Note-----\n\nIn the first sample, Mahmoud can't choose any integer a initially because there is no positive even integer less than or equal to 1 so Ehab wins.\n\nIn the second sample, Mahmoud has to choose a = 2 and subtract it from n. It's Ehab's turn and n = 0. There is no positive odd integer less than or equal to 0 so Mahmoud wins."} +{"id": "01308", "text": "You are given a string s of length n consisting of lowercase English letters.\n\nFor two given strings s and t, say S is the set of distinct characters of s and T is the set of distinct characters of t. The strings s and t are isomorphic if their lengths are equal and there is a one-to-one mapping (bijection) f between S and T for which f(s_{i}) = t_{i}. Formally: f(s_{i}) = t_{i} for any index i, for any character $x \\in S$ there is exactly one character $y \\in T$ that f(x) = y, for any character $y \\in T$ there is exactly one character $x \\in S$ that f(x) = y. \n\nFor example, the strings \"aababc\" and \"bbcbcz\" are isomorphic. Also the strings \"aaaww\" and \"wwwaa\" are isomorphic. The following pairs of strings are not isomorphic: \"aab\" and \"bbb\", \"test\" and \"best\".\n\nYou have to handle m queries characterized by three integers x, y, len (1 \u2264 x, y \u2264 n - len + 1). For each query check if two substrings s[x... x + len - 1] and s[y... y + len - 1] are isomorphic.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m (1 \u2264 n \u2264 2\u00b710^5, 1 \u2264 m \u2264 2\u00b710^5) \u2014 the length of the string s and the number of queries.\n\nThe second line contains string s consisting of n lowercase English letters.\n\nThe following m lines contain a single query on each line: x_{i}, y_{i} and len_{i} (1 \u2264 x_{i}, y_{i} \u2264 n, 1 \u2264 len_{i} \u2264 n - max(x_{i}, y_{i}) + 1) \u2014 the description of the pair of the substrings to check.\n\n\n-----Output-----\n\nFor each query in a separate line print \"YES\" if substrings s[x_{i}... x_{i} + len_{i} - 1] and s[y_{i}... y_{i} + len_{i} - 1] are isomorphic and \"NO\" otherwise.\n\n\n-----Example-----\nInput\n7 4\nabacaba\n1 1 1\n1 4 2\n2 1 3\n2 4 3\n\nOutput\nYES\nYES\nNO\nYES\n\n\n\n-----Note-----\n\nThe queries in the example are following: substrings \"a\" and \"a\" are isomorphic: f(a) = a; substrings \"ab\" and \"ca\" are isomorphic: f(a) = c, f(b) = a; substrings \"bac\" and \"aba\" are not isomorphic since f(b) and f(c) must be equal to a at same time; substrings \"bac\" and \"cab\" are isomorphic: f(b) = c, f(a) = a, f(c) = b."} +{"id": "01309", "text": "Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.\n\nNow the party is ready to start its journey, but firstly they have to choose kayaks. There are 2\u00b7n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is w_{i}, and weight is an important matter in kayaking \u2014 if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.\n\nFormally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.\n\nHelp the party to determine minimum possible total instability! \n\n\n-----Input-----\n\nThe first line contains one number n (2 \u2264 n \u2264 50).\n\nThe second line contains 2\u00b7n integer numbers w_1, w_2, ..., w_2n, where w_{i} is weight of person i (1 \u2264 w_{i} \u2264 1000).\n\n\n-----Output-----\n\nPrint minimum possible total instability.\n\n\n-----Examples-----\nInput\n2\n1 2 3 4\n\nOutput\n1\n\nInput\n4\n1 3 4 6 3 4 100 200\n\nOutput\n5"} +{"id": "01310", "text": "Little Petya likes arrays that consist of non-negative integers a lot. Recently his mom has presented him one such array consisting of n elements. Petya immediately decided to find there a segment of consecutive elements, such that the xor of all numbers from this segment was maximal possible. Help him with that.\n\nThe xor operation is the bitwise exclusive \"OR\", that is denoted as \"xor\" in Pascal and \"^\" in C/C++/Java.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100) \u2014 the number of elements in the array. The second line contains the space-separated integers from the array. All numbers are non-negative integers strictly less than 2^30.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the required maximal xor of a segment of consecutive elements.\n\n\n-----Examples-----\nInput\n5\n1 2 1 1 2\n\nOutput\n3\n\nInput\n3\n1 2 7\n\nOutput\n7\n\nInput\n4\n4 2 4 8\n\nOutput\n14\n\n\n\n-----Note-----\n\nIn the first sample one of the optimal segments is the segment that consists of the first and the second array elements, if we consider the array elements indexed starting from one.\n\nThe second sample contains only one optimal segment, which contains exactly one array element (element with index three)."} +{"id": "01311", "text": "The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.\n\nConsider n distinct points on a line. Let the i-th point have the coordinate x_{i} and weight w_{i}. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |x_{i} - x_{j}| \u2265 w_{i} + w_{j}.\n\nFind the size of the maximum clique in such graph.\n\n\n-----Input-----\n\nThe first line contains the integer n (1 \u2264 n \u2264 200 000) \u2014 the number of points.\n\nEach of the next n lines contains two numbers x_{i}, w_{i} (0 \u2264 x_{i} \u2264 10^9, 1 \u2264 w_{i} \u2264 10^9) \u2014 the coordinate and the weight of a point. All x_{i} are different.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of vertexes in the maximum clique of the given graph.\n\n\n-----Examples-----\nInput\n4\n2 3\n3 1\n6 1\n0 2\n\nOutput\n3\n\n\n\n-----Note-----\n\nIf you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!\n\nThe picture for the sample test. [Image]"} +{"id": "01312", "text": "Polycarpus has got n candies and m friends (n \u2265 m). He wants to make a New Year present with candies to each friend. Polycarpus is planning to present all candies and he wants to do this in the fairest (that is, most equal) manner. He wants to choose such a_{i}, where a_{i} is the number of candies in the i-th friend's present, that the maximum a_{i} differs from the least a_{i} as little as possible.\n\nFor example, if n is divisible by m, then he is going to present the same number of candies to all his friends, that is, the maximum a_{i} won't differ from the minimum one.\n\n\n-----Input-----\n\nThe single line of the input contains a pair of space-separated positive integers n, m (1 \u2264 n, m \u2264 100;n \u2265 m) \u2014 the number of candies and the number of Polycarpus's friends.\n\n\n-----Output-----\n\nPrint the required sequence a_1, a_2, ..., a_{m}, where a_{i} is the number of candies in the i-th friend's present. All numbers a_{i} must be positive integers, total up to n, the maximum one should differ from the minimum one by the smallest possible value.\n\n\n-----Examples-----\nInput\n12 3\n\nOutput\n4 4 4 \nInput\n15 4\n\nOutput\n3 4 4 4 \nInput\n18 7\n\nOutput\n2 2 2 3 3 3 3 \n\n\n-----Note-----\n\nPrint a_{i} in any order, separate the numbers by spaces."} +{"id": "01313", "text": "Two players play a simple game. Each player is provided with a box with balls. First player's box contains exactly n_1 balls and second player's box contains exactly n_2 balls. In one move first player can take from 1 to k_1 balls from his box and throw them away. Similarly, the second player can take from 1 to k_2 balls from his box in his move. Players alternate turns and the first player starts the game. The one who can't make a move loses. Your task is to determine who wins if both players play optimally.\n\n\n-----Input-----\n\nThe first line contains four integers n_1, n_2, k_1, k_2. All numbers in the input are from 1 to 50.\n\nThis problem doesn't have subproblems. You will get 3 points for the correct submission.\n\n\n-----Output-----\n\nOutput \"First\" if the first player wins and \"Second\" otherwise.\n\n\n-----Examples-----\nInput\n2 2 1 2\n\nOutput\nSecond\n\nInput\n2 1 1 1\n\nOutput\nFirst\n\n\n\n-----Note-----\n\nConsider the first sample test. Each player has a box with 2 balls. The first player draws a single ball from his box in one move and the second player can either take 1 or 2 balls from his box in one move. No matter how the first player acts, the second player can always win if he plays wisely."} +{"id": "01314", "text": "Bob is a pirate looking for the greatest treasure the world has ever seen. The treasure is located at the point $T$, which coordinates to be found out.\n\nBob travelled around the world and collected clues of the treasure location at $n$ obelisks. These clues were in an ancient language, and he has only decrypted them at home. Since he does not know which clue belongs to which obelisk, finding the treasure might pose a challenge. Can you help him?\n\nAs everyone knows, the world is a two-dimensional plane. The $i$-th obelisk is at integer coordinates $(x_i, y_i)$. The $j$-th clue consists of $2$ integers $(a_j, b_j)$ and belongs to the obelisk $p_j$, where $p$ is some (unknown) permutation on $n$ elements. It means that the treasure is located at $T=(x_{p_j} + a_j, y_{p_j} + b_j)$. This point $T$ is the same for all clues.\n\nIn other words, each clue belongs to exactly one of the obelisks, and each obelisk has exactly one clue that belongs to it. A clue represents the vector from the obelisk to the treasure. The clues must be distributed among the obelisks in such a way that they all point to the same position of the treasure.\n\nYour task is to find the coordinates of the treasure. If there are multiple solutions, you may print any of them.\n\nNote that you don't need to find the permutation. Permutations are used only in order to explain the problem.\n\n\n-----Input-----\n\nThe first line contains an integer $n$\u00a0($1 \\leq n \\leq 1000$)\u00a0\u2014 the number of obelisks, that is also equal to the number of clues.\n\nEach of the next $n$ lines contains two integers $x_i$, $y_i$\u00a0($-10^6 \\leq x_i, y_i \\leq 10^6$)\u00a0\u2014 the coordinates of the $i$-th obelisk. All coordinates are distinct, that is $x_i \\neq x_j$ or $y_i \\neq y_j$ will be satisfied for every $(i, j)$ such that $i \\neq j$. \n\nEach of the next $n$ lines contains two integers $a_i$, $b_i$\u00a0($-2 \\cdot 10^6 \\leq a_i, b_i \\leq 2 \\cdot 10^6$)\u00a0\u2014 the direction of the $i$-th clue. All coordinates are distinct, that is $a_i \\neq a_j$ or $b_i \\neq b_j$ will be satisfied for every $(i, j)$ such that $i \\neq j$. \n\nIt is guaranteed that there exists a permutation $p$, such that for all $i,j$ it holds $\\left(x_{p_i} + a_i, y_{p_i} + b_i\\right) = \\left(x_{p_j} + a_j, y_{p_j} + b_j\\right)$. \n\n\n-----Output-----\n\nOutput a single line containing two integers $T_x, T_y$\u00a0\u2014 the coordinates of the treasure.\n\nIf there are multiple answers, you may print any of them.\n\n\n-----Examples-----\nInput\n2\n2 5\n-6 4\n7 -2\n-1 -3\n\nOutput\n1 2\n\nInput\n4\n2 2\n8 2\n-7 0\n-2 6\n1 -14\n16 -12\n11 -18\n7 -14\n\nOutput\n9 -12\n\n\n\n-----Note-----\n\nAs $n = 2$, we can consider all permutations on two elements. \n\nIf $p = [1, 2]$, then the obelisk $(2, 5)$ holds the clue $(7, -2)$, which means that the treasure is hidden at $(9, 3)$. The second obelisk $(-6, 4)$ would give the clue $(-1,-3)$ and the treasure at $(-7, 1)$. However, both obelisks must give the same location, hence this is clearly not the correct permutation.\n\nIf the hidden permutation is $[2, 1]$, then the first clue belongs to the second obelisk and the second clue belongs to the first obelisk. Hence $(-6, 4) + (7, -2) = (2,5) + (-1,-3) = (1, 2)$, so $T = (1,2)$ is the location of the treasure. [Image] \n\nIn the second sample, the hidden permutation is $[2, 3, 4, 1]$."} +{"id": "01315", "text": "Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b.\n\nResidents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them.\n\nCan you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 200 000) \u2014 the number of residents who stand in the line.\n\nThe second line contains n space-separated integers a_{i} (0 \u2264 a_{i} \u2264 10^9), where a_{i} is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. \n\n\n-----Output-----\n\nIf it is impossible to make all the residents happy, print \":(\" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n2\n11 8\n\nOutput\n9 10 \nInput\n5\n10 9 7 10 6\n\nOutput\n:(\n\nInput\n3\n12 3 3\n\nOutput\n4 4 10 \n\n\n-----Note-----\n\nIn the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. \n\nIn the second sample it is impossible to achieve the desired result.\n\nIn the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy."} +{"id": "01316", "text": "Given a string $s$ of length $n$ and integer $k$ ($1 \\le k \\le n$). The string $s$ has a level $x$, if $x$ is largest non-negative integer, such that it's possible to find in $s$: $x$ non-intersecting (non-overlapping) substrings of length $k$, all characters of these $x$ substrings are the same (i.e. each substring contains only one distinct character and this character is the same for all the substrings). \n\nA substring is a sequence of consecutive (adjacent) characters, it is defined by two integers $i$ and $j$ ($1 \\le i \\le j \\le n$), denoted as $s[i \\dots j]$ = \"$s_{i}s_{i+1} \\dots s_{j}$\".\n\nFor example, if $k = 2$, then: the string \"aabb\" has level $1$ (you can select substring \"aa\"), the strings \"zzzz\" and \"zzbzz\" has level $2$ (you can select two non-intersecting substrings \"zz\" in each of them), the strings \"abed\" and \"aca\" have level $0$ (you can't find at least one substring of the length $k=2$ containing the only distinct character). \n\nZuhair gave you the integer $k$ and the string $s$ of length $n$. You need to find $x$, the level of the string $s$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$)\u00a0\u2014 the length of the string and the value of $k$.\n\nThe second line contains the string $s$ of length $n$ consisting only of lowercase Latin letters.\n\n\n-----Output-----\n\nPrint a single integer $x$\u00a0\u2014 the level of the string.\n\n\n-----Examples-----\nInput\n8 2\naaacaabb\n\nOutput\n2\n\nInput\n2 1\nab\n\nOutput\n1\n\nInput\n4 2\nabab\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, we can select $2$ non-intersecting substrings consisting of letter 'a': \"(aa)ac(aa)bb\", so the level is $2$.\n\nIn the second example, we can select either substring \"a\" or \"b\" to get the answer $1$."} +{"id": "01317", "text": "Arkady and his friends love playing checkers on an $n \\times n$ field. The rows and the columns of the field are enumerated from $1$ to $n$.\n\nThe friends have recently won a championship, so Arkady wants to please them with some candies. Remembering an old parable (but not its moral), Arkady wants to give to his friends one set of candies per each cell of the field: the set of candies for cell $(i, j)$ will have exactly $(i^2 + j^2)$ candies of unique type.\n\nThere are $m$ friends who deserve the present. How many of these $n \\times n$ sets of candies can be split equally into $m$ parts without cutting a candy into pieces? Note that each set has to be split independently since the types of candies in different sets are different.\n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $m$ ($1 \\le n \\le 10^9$, $1 \\le m \\le 1000$)\u00a0\u2014 the size of the field and the number of parts to split the sets into.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of sets that can be split equally.\n\n\n-----Examples-----\nInput\n3 3\n\nOutput\n1\n\nInput\n6 5\n\nOutput\n13\n\nInput\n1000000000 1\n\nOutput\n1000000000000000000\n\n\n\n-----Note-----\n\nIn the first example, only the set for cell $(3, 3)$ can be split equally ($3^2 + 3^2 = 18$, which is divisible by $m=3$).\n\nIn the second example, the sets for the following cells can be divided equally: $(1, 2)$ and $(2, 1)$, since $1^2 + 2^2 = 5$, which is divisible by $5$; $(1, 3)$ and $(3, 1)$; $(2, 4)$ and $(4, 2)$; $(2, 6)$ and $(6, 2)$; $(3, 4)$ and $(4, 3)$; $(3, 6)$ and $(6, 3)$; $(5, 5)$. \n\nIn the third example, sets in all cells can be divided equally, since $m = 1$."} +{"id": "01318", "text": "Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!\n\nA restaurant called \"Dijkstra's Place\" has started thinking about optimizing the booking system. \n\nThere are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} \u2014 the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.\n\nWe know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.\n\nUnfortunately, there only are k tables in the restaurant. For each table, we know r_{i} \u2014 the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.\n\nYour task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 1000) \u2014 the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 \u2264 c_{i}, p_{i} \u2264 1000) \u2014 the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.\n\nThe next line contains integer k (1 \u2264 k \u2264 1000) \u2014 the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 \u2264 r_{i} \u2264 1000) \u2014 the maximum number of people that can sit at each table.\n\n\n-----Output-----\n\nIn the first line print two integers: m, s \u2014 the number of accepted requests and the total money you get from these requests, correspondingly.\n\nThen print m lines \u2014 each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.\n\nIf there are multiple optimal answers, print any of them.\n\n\n-----Examples-----\nInput\n3\n10 50\n2 100\n5 30\n3\n4 6 9\n\nOutput\n2 130\n2 1\n3 2"} +{"id": "01319", "text": "Ayrat has number n, represented as it's prime factorization p_{i} of size m, i.e. n = p_1\u00b7p_2\u00b7...\u00b7p_{m}. Ayrat got secret information that that the product of all divisors of n taken modulo 10^9 + 7 is the password to the secret data base. Now he wants to calculate this value.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer m (1 \u2264 m \u2264 200 000)\u00a0\u2014 the number of primes in factorization of n. \n\nThe second line contains m primes numbers p_{i} (2 \u2264 p_{i} \u2264 200 000).\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the product of all divisors of n modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n2\n2 3\n\nOutput\n36\n\nInput\n3\n2 3 2\n\nOutput\n1728\n\n\n\n-----Note-----\n\nIn the first sample n = 2\u00b73 = 6. The divisors of 6 are 1, 2, 3 and 6, their product is equal to 1\u00b72\u00b73\u00b76 = 36.\n\nIn the second sample 2\u00b73\u00b72 = 12. The divisors of 12 are 1, 2, 3, 4, 6 and 12. 1\u00b72\u00b73\u00b74\u00b76\u00b712 = 1728."} +{"id": "01320", "text": "Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird!\n\nThe cake is a n \u00d7 n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be?\n\nPlease, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column.\n\n\n-----Input-----\n\nIn the first line of the input, you are given a single integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the length of the side of the cake.\n\nThen follow n lines, each containing n characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'.\n\n\n-----Output-----\n\nPrint the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.\n\n\n-----Examples-----\nInput\n3\n.CC\nC..\nC.C\n\nOutput\n4\n\nInput\n4\nCC..\nC..C\n.CC.\n.CC.\n\nOutput\n9\n\n\n\n-----Note-----\n\nIf we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are: (1, 2) and (1, 3) (3, 1) and (3, 3) Pieces that share the same column are: (2, 1) and (3, 1) (1, 3) and (3, 3)"} +{"id": "01321", "text": "One day n friends met at a party, they hadn't seen each other for a long time and so they decided to make a group photo together. \n\nSimply speaking, the process of taking photos can be described as follows. On the photo, each photographed friend occupies a rectangle of pixels: the i-th of them occupies the rectangle of width w_{i} pixels and height h_{i} pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is W \u00d7 H, where W is the total sum of all widths and H is the maximum height of all the photographed friends.\n\nAs is usually the case, the friends made n photos \u2014 the j-th (1 \u2264 j \u2264 n) photo had everybody except for the j-th friend as he was the photographer.\n\nPrint the minimum size of each made photo in pixels. \n\n\n-----Input-----\n\nThe first line contains integer n (2 \u2264 n \u2264 200 000) \u2014 the number of friends. \n\nThen n lines follow: the i-th line contains information about the i-th friend. The line contains a pair of integers w_{i}, h_{i} (1 \u2264 w_{i} \u2264 10, 1 \u2264 h_{i} \u2264 1000) \u2014 the width and height in pixels of the corresponding rectangle.\n\n\n-----Output-----\n\nPrint n space-separated numbers b_1, b_2, ..., b_{n}, where b_{i} \u2014 the total number of pixels on the minimum photo containing all friends expect for the i-th one.\n\n\n-----Examples-----\nInput\n3\n1 10\n5 5\n10 1\n\nOutput\n75 110 60 \nInput\n3\n2 1\n1 2\n2 1\n\nOutput\n6 4 6"} +{"id": "01322", "text": "Sasha and Ira are two best friends. But they aren\u2019t just friends, they are software engineers and experts in artificial intelligence. They are developing an algorithm for two bots playing a two-player game. The game is cooperative and turn based. In each turn, one of the players makes a move (it doesn\u2019t matter which player, it's possible that players turns do not alternate). \n\nAlgorithm for bots that Sasha and Ira are developing works by keeping track of the state the game is in. Each time either bot makes a move, the state changes. And, since the game is very dynamic, it will never go back to the state it was already in at any point in the past.\n\nSasha and Ira are perfectionists and want their algorithm to have an optimal winning strategy. They have noticed that in the optimal winning strategy, both bots make exactly N moves each. But, in order to find the optimal strategy, their algorithm needs to analyze all possible states of the game (they haven\u2019t learned about alpha-beta pruning yet) and pick the best sequence of moves.\n\nThey are worried about the efficiency of their algorithm and are wondering what is the total number of states of the game that need to be analyzed? \n\n\n-----Input-----\n\nThe first and only line contains integer N. 1 \u2264 N \u2264 10^6 \n\n\n-----Output-----\n\nOutput should contain a single integer \u2013 number of possible states modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n19\n\n\n\n-----Note-----\n\nStart: Game is in state A. Turn 1: Either bot can make a move (first bot is red and second bot is blue), so there are two possible states after the first turn \u2013 B and C. Turn 2: In both states B and C, either bot can again make a turn, so the list of possible states is expanded to include D, E, F and G. Turn 3: Red bot already did N=2 moves when in state D, so it cannot make any more moves there. It can make moves when in state E, F and G, so states I, K and M are added to the list. Similarly, blue bot cannot make a move when in state G, but can when in D, E and F, so states H, J and L are added. Turn 4: Red bot already did N=2 moves when in states H, I and K, so it can only make moves when in J, L and M, so states P, R and S are added. Blue bot cannot make a move when in states J, L and M, but only when in H, I and K, so states N, O and Q are added. \n\nOverall, there are 19 possible states of the game their algorithm needs to analyze.\n\n[Image]"} +{"id": "01323", "text": "Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic.\n\nSuppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has a_{i} rows from A. Similarly, second cluster containing table B has n partitions, i-th one having b_{i} rows from B. \n\nIn one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this.\n\n\n-----Input-----\n\nFirst line contains two integer numbers, m and n (1 \u2264 m, n \u2264 10^5). Second line contains description of the first cluster with m space separated integers, a_{i} (1 \u2264 a_{i} \u2264 10^9). Similarly, third line describes second cluster with n space separated integers, b_{i} (1 \u2264 b_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint one integer \u2014 minimal number of copy operations.\n\n\n-----Examples-----\nInput\n2 2\n2 6\n3 100\n\nOutput\n11\n\nInput\n2 3\n10 10\n1 1 1\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations\n\nIn the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2\u00b73 = 6 copy operations."} +{"id": "01324", "text": "Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called \"Black Square\" on his super cool touchscreen phone.\n\nIn this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly a_{i} calories on touching the i-th strip.\n\nYou've got a string s, describing the process of the game and numbers a_1, a_2, a_3, a_4. Calculate how many calories Jury needs to destroy all the squares?\n\n\n-----Input-----\n\nThe first line contains four space-separated integers a_1, a_2, a_3, a_4 (0 \u2264 a_1, a_2, a_3, a_4 \u2264 10^4).\n\nThe second line contains string s (1 \u2264 |s| \u2264 10^5), where the \u0456-th character of the string equals \"1\", if on the i-th second of the game the square appears on the first strip, \"2\", if it appears on the second strip, \"3\", if it appears on the third strip, \"4\", if it appears on the fourth strip.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the total number of calories that Jury wastes.\n\n\n-----Examples-----\nInput\n1 2 3 4\n123214\n\nOutput\n13\n\nInput\n1 5 3 2\n11221\n\nOutput\n13"} +{"id": "01325", "text": "Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.\n\nThere is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 \u2264 i \u2264 n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).\n\nWhen Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.\n\nInitially, the text cursor is at position p. \n\nBecause Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n (1 \u2264 n \u2264 10^5) and p (1 \u2264 p \u2264 n), the length of Nam's string and the initial position of the text cursor.\n\nThe next line contains n lowercase characters of Nam's string.\n\n\n-----Output-----\n\nPrint the minimum number of presses needed to change string into a palindrome.\n\n\n-----Examples-----\nInput\n8 3\naeabcaez\n\nOutput\n6\n\n\n\n-----Note-----\n\nA string is a palindrome if it reads the same forward or reversed.\n\nIn the sample test, initial Nam's string is: $\\text{aeabcaez}$ (cursor position is shown bold).\n\nIn optimal solution, Nam may do 6 following steps:[Image]\n\nThe result, $\\text{zeaccaez}$, is now a palindrome."} +{"id": "01326", "text": "For a positive integer X, let f(X) be the number of positive divisors of X.\nGiven a positive integer N, find \\sum_{K=1}^N K\\times f(K).\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^7\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the value \\sum_{K=1}^N K\\times f(K).\n\n-----Sample Input-----\n4\n\n-----Sample Output-----\n23\n\nWe have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\\times 1 + 2\\times 2 + 3\\times 2 + 4\\times 3 =23."} +{"id": "01327", "text": "Takahashi became a pastry chef and opened a shop La Confiserie d'ABC to celebrate AtCoder Beginner Contest 100.\nThe shop sells N kinds of cakes.\n\nEach kind of cake has three parameters \"beauty\", \"tastiness\" and \"popularity\". The i-th kind of cake has the beauty of x_i, the tastiness of y_i and the popularity of z_i.\n\nThese values may be zero or negative.\nRingo has decided to have M pieces of cakes here. He will choose the set of cakes as follows:\n - Do not have two or more pieces of the same kind of cake.\n - Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity).\nFind the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.\n\n-----Constraints-----\n - N is an integer between 1 and 1 \\ 000 (inclusive).\n - M is an integer between 0 and N (inclusive).\n - x_i, y_i, z_i \\ (1 \\leq i \\leq N) are integers between -10 \\ 000 \\ 000 \\ 000 and 10 \\ 000 \\ 000 \\ 000 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nx_1 y_1 z_1\nx_2 y_2 z_2\n : :\nx_N y_N z_N\n\n-----Output-----\nPrint the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.\n\n-----Sample Input-----\n5 3\n3 1 4\n1 5 9\n2 6 5\n3 5 8\n9 7 9\n\n-----Sample Output-----\n56\n\nConsider having the 2-nd, 4-th and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:\n - Beauty: 1 + 3 + 9 = 13\n - Tastiness: 5 + 5 + 7 = 17\n - Popularity: 9 + 8 + 9 = 26\nThe value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 13 + 17 + 26 = 56. This is the maximum value."} +{"id": "01328", "text": "Dolphin is planning to generate a small amount of a certain chemical substance C.\n\nIn order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.\n\nHe does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.\n\nThe pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.\n\nThe package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).\n\nDolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.\n\nFind the minimum amount of money required to generate the substance C.\n\nIf it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact. \n\n-----Constraints-----\n - 1\u2266N\u226640 \n - 1\u2266a_i,b_i\u226610 \n - 1\u2266c_i\u2266100 \n - 1\u2266M_a,M_b\u226610 \n - gcd(M_a,M_b)=1\n - a_i, b_i, c_i, M_a and M_b are integers.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN M_a M_b \na_1 b_1 c_1 \na_2 b_2 c_2\n: \na_N b_N c_N \n\n-----Output-----\nPrint the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.\n\n-----Sample Input-----\n3 1 1\n1 2 1\n2 1 2\n3 3 10\n\n-----Sample Output-----\n3\n\nThe amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.\n\nIn this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.\n\nThe total price of these packages is 3 yen."} +{"id": "01329", "text": "You are given an integer N. Among the divisors of N! (= 1 \\times 2 \\times ... \\times N), how many Shichi-Go numbers (literally \"Seven-Five numbers\") are there?\nHere, a Shichi-Go number is a positive integer that has exactly 75 divisors.\n\n-----Note-----\nWhen a positive integer A divides a positive integer B, A is said to a divisor of B.\nFor example, 6 has four divisors: 1, 2, 3 and 6.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the number of the Shichi-Go numbers that are divisors of N!.\n\n-----Sample Input-----\n9\n\n-----Sample Output-----\n0\n\nThere are no Shichi-Go numbers among the divisors of 9! = 1 \\times 2 \\times ... \\times 9 = 362880."} +{"id": "01330", "text": "There are $n$ students and $m$ clubs in a college. The clubs are numbered from $1$ to $m$. Each student has a potential $p_i$ and is a member of the club with index $c_i$. Initially, each student is a member of exactly one club. A technical fest starts in the college, and it will run for the next $d$ days. There is a coding competition every day in the technical fest. \n\nEvery day, in the morning, exactly one student of the college leaves their club. Once a student leaves their club, they will never join any club again. Every day, in the afternoon, the director of the college will select one student from each club (in case some club has no members, nobody is selected from that club) to form a team for this day's coding competition. The strength of a team is the mex of potentials of the students in the team. The director wants to know the maximum possible strength of the team for each of the coming $d$ days. Thus, every day the director chooses such team, that the team strength is maximized.\n\nThe mex of the multiset $S$ is the smallest non-negative integer that is not present in $S$. For example, the mex of the $\\{0, 1, 1, 2, 4, 5, 9\\}$ is $3$, the mex of $\\{1, 2, 3\\}$ is $0$ and the mex of $\\varnothing$ (empty set) is $0$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\leq m \\leq n \\leq 5000$), the number of students and the number of clubs in college.\n\nThe second line contains $n$ integers $p_1, p_2, \\ldots, p_n$ ($0 \\leq p_i < 5000$), where $p_i$ is the potential of the $i$-th student.\n\nThe third line contains $n$ integers $c_1, c_2, \\ldots, c_n$ ($1 \\leq c_i \\leq m$), which means that $i$-th student is initially a member of the club with index $c_i$.\n\nThe fourth line contains an integer $d$ ($1 \\leq d \\leq n$), number of days for which the director wants to know the maximum possible strength of the team. \n\nEach of the next $d$ lines contains an integer $k_i$ ($1 \\leq k_i \\leq n$), which means that $k_i$-th student lefts their club on the $i$-th day. It is guaranteed, that the $k_i$-th student has not left their club earlier.\n\n\n-----Output-----\n\nFor each of the $d$ days, print the maximum possible strength of the team on that day.\n\n\n-----Examples-----\nInput\n5 3\n0 1 2 2 0\n1 2 2 3 2\n5\n3\n2\n4\n5\n1\n\nOutput\n3\n1\n1\n1\n0\n\nInput\n5 3\n0 1 2 2 1\n1 3 2 3 2\n5\n4\n2\n3\n5\n1\n\nOutput\n3\n2\n2\n1\n0\n\nInput\n5 5\n0 1 2 4 5\n1 2 3 4 5\n4\n2\n3\n5\n4\n\nOutput\n1\n1\n1\n1\n\n\n\n-----Note-----\n\nConsider the first example:\n\nOn the first day, student $3$ leaves their club. Now, the remaining students are $1$, $2$, $4$ and $5$. We can select students $1$, $2$ and $4$ to get maximum possible strength, which is $3$. Note, that we can't select students $1$, $2$ and $5$, as students $2$ and $5$ belong to the same club. Also, we can't select students $1$, $3$ and $4$, since student $3$ has left their club.\n\nOn the second day, student $2$ leaves their club. Now, the remaining students are $1$, $4$ and $5$. We can select students $1$, $4$ and $5$ to get maximum possible strength, which is $1$.\n\nOn the third day, the remaining students are $1$ and $5$. We can select students $1$ and $5$ to get maximum possible strength, which is $1$.\n\nOn the fourth day, the remaining student is $1$. We can select student $1$ to get maximum possible strength, which is $1$. \n\nOn the fifth day, no club has students and so the maximum possible strength is $0$."} +{"id": "01331", "text": "Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer a_{i}\u00a0\u2014 number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute. \n\nVitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.\n\nVitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on. \n\n\n-----Input-----\n\nFirst line contains three integers n, m and k (1 \u2264 k \u2264 n \u2264 2\u00b710^5, 1 \u2264 m \u2264 10^6)\u00a0\u2014 number of alarm clocks, and conditions of Vitalya's waking up. \n\nSecond line contains sequence of distinct integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^6) in which a_{i} equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 10^6 minutes. \n\n\n-----Output-----\n\nOutput minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.\n\n\n-----Examples-----\nInput\n3 3 2\n3 5 1\n\nOutput\n1\n\nInput\n5 10 3\n12 8 18 25 1\n\nOutput\n0\n\nInput\n7 7 2\n7 3 4 1 6 5 2\n\nOutput\n6\n\nInput\n2 2 2\n1 3\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn first example Vitalya should turn off first alarm clock which rings at minute 3.\n\nIn second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.\n\nIn third example Vitalya should turn off any 6 alarm clocks."} +{"id": "01332", "text": "There are five people playing a game called \"Generosity\". Each person gives some non-zero number of coins b as an initial bet. After all players make their bets of b coins, the following operation is repeated for several times: a coin is passed from one player to some other player.\n\nYour task is to write a program that can, given the number of coins each player has at the end of the game, determine the size b of the initial bet or find out that such outcome of the game cannot be obtained for any positive number of coins b in the initial bet.\n\n\n-----Input-----\n\nThe input consists of a single line containing five integers c_1, c_2, c_3, c_4 and c_5 \u2014 the number of coins that the first, second, third, fourth and fifth players respectively have at the end of the game (0 \u2264 c_1, c_2, c_3, c_4, c_5 \u2264 100).\n\n\n-----Output-----\n\nPrint the only line containing a single positive integer b \u2014 the number of coins in the initial bet of each player. If there is no such value of b, then print the only value \"-1\" (quotes for clarity).\n\n\n-----Examples-----\nInput\n2 5 4 0 4\n\nOutput\n3\n\nInput\n4 5 9 2 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample the following sequence of operations is possible: One coin is passed from the fourth player to the second player; One coin is passed from the fourth player to the fifth player; One coin is passed from the first player to the third player; One coin is passed from the fourth player to the second player."} +{"id": "01333", "text": "Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead.\n\nA snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r, c). The tail of the snake is located at (1, 1), then it's body extends to (1, m), then goes down 2 rows to (3, m), then goes left to (3, 1) and so on.\n\nYour task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and the snake cells should be filled with number signs ('#').\n\nConsider sample tests in order to understand the snake pattern.\n\n\n-----Input-----\n\nThe only line contains two integers: n and m (3 \u2264 n, m \u2264 50). \n\nn is an odd number.\n\n\n-----Output-----\n\nOutput n lines. Each line should contain a string consisting of m characters. Do not output spaces.\n\n\n-----Examples-----\nInput\n3 3\n\nOutput\n###\n..#\n###\n\nInput\n3 4\n\nOutput\n####\n...#\n####\n\nInput\n5 3\n\nOutput\n###\n..#\n###\n#..\n###\n\nInput\n9 9\n\nOutput\n#########\n........#\n#########\n#........\n#########\n........#\n#########\n#........\n#########"} +{"id": "01334", "text": "And where the are the phone numbers?\n\nYou are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t.\n\nIt's guaranteed that the answer exists.\n\nNote that the set of letters is a set, not a multiset. For example, the set of letters of abadaba is {a, b, d}.\n\nString p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that p_{i} < q_{i} and for all j < i it is satisfied that p_{j} = q_{j}. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abec, afa is not lexicographically smaller than ab and a is not lexicographically smaller than a.\n\n\n-----Input-----\n\nThe first line of input contains two space separated integers n and k (1 \u2264 n, k \u2264 100 000)\u00a0\u2014 the length of s and the required length of t.\n\nThe second line of input contains the string s consisting of n lowercase English letters.\n\n\n-----Output-----\n\nOutput the string t conforming to the requirements above.\n\nIt's guaranteed that the answer exists.\n\n\n-----Examples-----\nInput\n3 3\nabc\n\nOutput\naca\n\nInput\n3 2\nabc\n\nOutput\nac\n\nInput\n3 3\nayy\n\nOutput\nyaa\n\nInput\n2 3\nba\n\nOutput\nbaa\n\n\n\n-----Note-----\n\nIn the first example the list of strings t of length 3, such that the set of letters of t is a subset of letters of s is as follows: aaa, aab, aac, aba, abb, abc, aca, acb, .... Among them, those are lexicographically greater than abc: aca, acb, .... Out of those the lexicographically smallest is aca."} +{"id": "01335", "text": "Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab.\n\nThere are $n$ solutions, the $i$-th of them should be tested on $a_i$ tests, testing one solution on one test takes $1$ second. The solutions are judged in the order from $1$ to $n$. There are $k$ testing processes which test solutions simultaneously. Each of them can test at most one solution at a time.\n\nAt any time moment $t$ when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id $i$, then it is being tested on the first test from time moment $t$ till time moment $t + 1$, then on the second test till time moment $t + 2$ and so on. This solution is fully tested at time moment $t + a_i$, and after that the testing process immediately starts testing another solution.\n\nConsider some time moment, let there be exactly $m$ fully tested solutions by this moment. There is a caption \"System testing: $d$%\" on the page with solutions, where $d$ is calculated as\n\n$$d = round\\left(100\\cdot\\frac{m}{n}\\right),$$\n\nwhere $round(x) = \\lfloor{x + 0.5}\\rfloor$ is a function which maps every real to the nearest integer.\n\nVasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test $q$, and the caption says \"System testing: $q$%\". Find the number of interesting solutions.\n\nPlease note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter.\n\n\n-----Input-----\n\nThe first line contains two positive integers $n$ and $k$ ($1 \\le n \\le 1000$, $1 \\le k \\le 100$) standing for the number of submissions and the number of testing processes respectively.\n\nThe second line contains $n$ positive integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 150$), where $a_i$ is equal to the number of tests the $i$-th submission is to be run on.\n\n\n-----Output-----\n\nOutput the only integer\u00a0\u2014 the number of interesting submissions.\n\n\n-----Examples-----\nInput\n2 2\n49 100\n\nOutput\n1\n\nInput\n4 2\n32 100 33 1\n\nOutput\n2\n\nInput\n14 5\n48 19 6 9 50 20 3 42 38 43 36 21 44 6\n\nOutput\n5\n\n\n\n-----Note-----\n\nConsider the first example. At time moment $0$ both solutions start testing. At time moment $49$ the first solution is fully tested, so at time moment $49.5$ the second solution is being tested on the test $50$, and the caption says \"System testing: $50$%\" (because there is one fully tested solution out of two). So, the second solution is interesting.\n\nConsider the second example. At time moment $0$ the first and the second solutions start testing. At time moment $32$ the first solution is fully tested, the third solution starts testing, the caption says \"System testing: $25$%\". At time moment $32 + 24.5 = 56.5$ the third solutions is being tested on test $25$, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment $32 + 33 = 65$, the fourth solution is fully tested at time moment $65 + 1 = 66$. The captions becomes \"System testing: $75$%\", and at time moment $74.5$ the second solution is being tested on test $75$. So, this solution is also interesting. Overall, there are two interesting solutions."} +{"id": "01336", "text": "There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has $n$ different matryoshkas. Any matryoshka is a figure of volume $out_i$ with an empty space inside of volume $in_i$ (of course, $out_i > in_i$).\n\nYou don't have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let's call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll \u2014 inside the third one and so on. Matryoshka $i$ can be nested inside matryoshka $j$ if $out_i \\le in_j$. So only the last doll will take space inside your bag.\n\nLet's call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it's equal to $in_{i_1} + (in_{i_2} - out_{i_1}) + (in_{i_3} - out_{i_2}) + \\dots + (in_{i_k} - out_{i_{k-1}})$, where $i_1$, $i_2$, ..., $i_k$ are the indices of the chosen dolls in the order they are nested in each other.\n\nFinally, let's call a nested subset of the given sequence as big enough if there isn't any doll from the sequence that can be added to the nested subset without breaking its nested property.\n\nYou want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index $i$ such that one of the subsets contains the $i$-th doll, and another subset doesn't.\n\nSince the answer can be large, print it modulo $10^9 + 7$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of matryoshkas.\n\nThe next $n$ lines contain a description of each doll: two integers $out_i$ and $in_i$ ($1 \\le in_i < out_i \\le 10^9$) \u2014 the outer and inners volumes of the $i$-th matryoshka.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of big enough nested subsets such that extra space of each of these subsets is minimum possible. Since the answer can be large, print it modulo $10^9 + 7$.\n\n\n-----Example-----\nInput\n7\n4 1\n4 2\n4 2\n2 1\n5 4\n6 4\n3 2\n\nOutput\n6\n\n\n\n-----Note-----\n\nThere are $6$ big enough nested subsets with minimum possible extra space in the example: $\\{1, 5\\}$: we can't add any other matryoshka and keep it nested; it's extra space is $1$; $\\{1, 6\\}$; $\\{2, 4, 5\\}$; $\\{2, 4, 6\\}$; $\\{3, 4, 5\\}$; $\\{3, 4, 6\\}$. \n\nThere are no more \"good\" subsets because, for example, subset $\\{6, 7\\}$ is not big enough (we can add the $4$-th matryoshka to it) or subset $\\{4, 6, 7\\}$ has extra space equal to $2$."} +{"id": "01337", "text": "Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 10^9.\n\nIn the evening after the conference, all n scientists decided to go to the cinema. There are m movies in the cinema they came to. Each of the movies is characterized by two distinct numbers\u00a0\u2014 the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different). \n\nScientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.\n\n\n-----Input-----\n\nThe first line of the input contains a positive integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the number of scientists.\n\nThe second line contains n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9), where a_{i} is the index of a language, which the i-th scientist knows.\n\nThe third line contains a positive integer m (1 \u2264 m \u2264 200 000)\u00a0\u2014 the number of movies in the cinema. \n\nThe fourth line contains m positive integers b_1, b_2, ..., b_{m} (1 \u2264 b_{j} \u2264 10^9), where b_{j} is the index of the audio language of the j-th movie.\n\nThe fifth line contains m positive integers c_1, c_2, ..., c_{m} (1 \u2264 c_{j} \u2264 10^9), where c_{j} is the index of subtitles language of the j-th movie.\n\nIt is guaranteed that audio languages and subtitles language are different for each movie, that is b_{j} \u2260 c_{j}. \n\n\n-----Output-----\n\nPrint the single integer\u00a0\u2014 the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists. \n\nIf there are several possible answers print any of them.\n\n\n-----Examples-----\nInput\n3\n2 3 2\n2\n3 2\n2 3\n\nOutput\n2\n\nInput\n6\n6 3 1 1 3 7\n5\n1 2 3 4 5\n2 3 4 5 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied.\n\nIn the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly two scientists will be very pleased and all the others will be not satisfied."} +{"id": "01338", "text": "You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum:$f(p) = \\sum_{i = 1}^{n} \\sum_{j = i}^{n} \\operatorname{min}(p_{i}, p_{i + 1}, \\ldots p_{j})$\n\nFind the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p).\n\n\n-----Input-----\n\nThe single line of input contains two integers n and m (1 \u2264 m \u2264 cnt_{n}), where cnt_{n} is the number of permutations of length n with maximum possible value of f(p).\n\nThe problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem B1 (3 points), the constraint 1 \u2264 n \u2264 8 will hold. In subproblem B2 (4 points), the constraint 1 \u2264 n \u2264 50 will hold. \n\n\n-----Output-----\n\nOutput n number forming the required permutation.\n\n\n-----Examples-----\nInput\n2 2\n\nOutput\n2 1 \n\nInput\n3 2\n\nOutput\n1 3 2 \n\n\n\n-----Note-----\n\nIn the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order."} +{"id": "01339", "text": "A coordinate line has n segments, the i-th segment starts at the position l_{i} and ends at the position r_{i}. We will denote such a segment as [l_{i}, r_{i}].\n\nYou have suggested that one of the defined segments covers all others. In other words, there is such segment in the given set, which contains all other ones. Now you want to test your assumption. Find in the given set the segment which covers all other segments, and print its number. If such a segment doesn't exist, print -1.\n\nFormally we will assume that segment [a, b] covers segment [c, d], if they meet this condition a \u2264 c \u2264 d \u2264 b. \n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of segments. Next n lines contain the descriptions of the segments. The i-th line contains two space-separated integers l_{i}, r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 10^9) \u2014 the borders of the i-th segment.\n\nIt is guaranteed that no two segments coincide.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of the segment that covers all other segments in the set. If there's no solution, print -1.\n\nThe segments are numbered starting from 1 in the order in which they appear in the input.\n\n\n-----Examples-----\nInput\n3\n1 1\n2 2\n3 3\n\nOutput\n-1\n\nInput\n6\n1 5\n2 3\n1 10\n7 10\n7 7\n10 10\n\nOutput\n3"} +{"id": "01340", "text": "Monocarp has arranged $n$ colored marbles in a row. The color of the $i$-th marble is $a_i$. Monocarp likes ordered things, so he wants to rearrange marbles in such a way that all marbles of the same color form a contiguos segment (and there is only one such segment for each color). \n\nIn other words, Monocarp wants to rearrange marbles so that, for every color $j$, if the leftmost marble of color $j$ is $l$-th in the row, and the rightmost marble of this color has position $r$ in the row, then every marble from $l$ to $r$ has color $j$.\n\nTo achieve his goal, Monocarp can do the following operation any number of times: choose two neighbouring marbles, and swap them.\n\nYou have to calculate the minimum number of operations Monocarp has to perform to rearrange the marbles. Note that the order of segments of marbles having equal color does not matter, it is only required that, for every color, all the marbles of this color form exactly one contiguous segment.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ $(2 \\le n \\le 4 \\cdot 10^5)$ \u2014 the number of marbles.\n\nThe second line contains an integer sequence $a_1, a_2, \\dots, a_n$ $(1 \\le a_i \\le 20)$, where $a_i$ is the color of the $i$-th marble.\n\n\n-----Output-----\n\nPrint the minimum number of operations Monocarp has to perform to achieve his goal.\n\n\n-----Examples-----\nInput\n7\n3 4 2 3 4 2 2\n\nOutput\n3\n\nInput\n5\n20 1 14 10 2\n\nOutput\n0\n\nInput\n13\n5 5 4 4 3 5 7 6 5 4 4 6 5\n\nOutput\n21\n\n\n\n-----Note-----\n\nIn the first example three operations are enough. Firstly, Monocarp should swap the third and the fourth marbles, so the sequence of colors is $[3, 4, 3, 2, 4, 2, 2]$. Then Monocarp should swap the second and the third marbles, so the sequence is $[3, 3, 4, 2, 4, 2, 2]$. And finally, Monocarp should swap the fourth and the fifth marbles, so the sequence is $[3, 3, 4, 4, 2, 2, 2]$. \n\nIn the second example there's no need to perform any operations."} +{"id": "01341", "text": "There is a sequence of colorful stones. The color of each stone is one of red, green, or blue. You are given a string s. The i-th (1-based) character of s represents the color of the i-th stone. If the character is \"R\", \"G\", or \"B\", the color of the corresponding stone is red, green, or blue, respectively.\n\nInitially Squirrel Liss is standing on the first stone. You perform instructions one or more times.\n\nEach instruction is one of the three types: \"RED\", \"GREEN\", or \"BLUE\". After an instruction c, if Liss is standing on a stone whose colors is c, Liss will move one stone forward, else she will not move.\n\nYou are given a string t. The number of instructions is equal to the length of t, and the i-th character of t represents the i-th instruction.\n\nCalculate the final position of Liss (the number of the stone she is going to stand on in the end) after performing all the instructions, and print its 1-based position. It is guaranteed that Liss don't move out of the sequence.\n\n\n-----Input-----\n\nThe input contains two lines. The first line contains the string s (1 \u2264 |s| \u2264 50). The second line contains the string t (1 \u2264 |t| \u2264 50). The characters of each string will be one of \"R\", \"G\", or \"B\". It is guaranteed that Liss don't move out of the sequence.\n\n\n-----Output-----\n\nPrint the final 1-based position of Liss in a single line.\n\n\n-----Examples-----\nInput\nRGB\nRRR\n\nOutput\n2\n\nInput\nRRRBGBRBBB\nBBBRR\n\nOutput\n3\n\nInput\nBRRBGBRGRBGRGRRGGBGBGBRGBRGRGGGRBRRRBRBBBGRRRGGBBB\nBBRBGGRGRGBBBRBGRBRBBBBRBRRRBGBBGBBRRBBGGRBRRBRGRB\n\nOutput\n15"} +{"id": "01342", "text": "There are n boxes with colored balls on the table. Colors are numbered from 1 to n. i-th box contains a_{i} balls, all of which have color i. You have to write a program that will divide all balls into sets such that: each ball belongs to exactly one of the sets, there are no empty sets, there is no set containing two (or more) balls of different colors (each set contains only balls of one color), there are no two sets such that the difference between their sizes is greater than 1. \n\nPrint the minimum possible number of sets.\n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 500).\n\nThe second line contains n integer numbers a_1, a_2, ... , a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint one integer number \u2014 the minimum possible number of sets.\n\n\n-----Examples-----\nInput\n3\n4 7 8\n\nOutput\n5\n\nInput\n2\n2 7\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color."} +{"id": "01343", "text": "Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.\n\nTo bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a_1, a_2, ..., a_{k}.\n\nUnforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid\u00a0\u2014 for every kilometer of path between storage and bakery Masha should pay 1 ruble.\n\nFormally, Masha will pay x roubles, if she will open the bakery in some city b (a_{i} \u2260 b for every 1 \u2264 i \u2264 k) and choose a storage in some city s (s = a_{j} for some 1 \u2264 j \u2264 k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).\n\nMasha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, m and k (1 \u2264 n, m \u2264 10^5, 0 \u2264 k \u2264 n)\u00a0\u2014 the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.\n\nThen m lines follow. Each of them contains three integers u, v and l (1 \u2264 u, v \u2264 n, 1 \u2264 l \u2264 10^9, u \u2260 v) meaning that there is a road between cities u and v of length of l kilometers .\n\nIf k > 0, then the last line of the input contains k distinct integers a_1, a_2, ..., a_{k} (1 \u2264 a_{i} \u2264 n)\u00a0\u2014 the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.\n\n\n-----Output-----\n\nPrint the minimum possible amount of rubles Masha should pay for flour delivery in the only line.\n\nIf the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.\n\n\n-----Examples-----\nInput\n5 4 2\n1 2 5\n1 2 3\n2 3 4\n1 4 10\n1 5\n\nOutput\n3\nInput\n3 1 1\n1 2 3\n3\n\nOutput\n-1\n\n\n-----Note-----\n\n[Image]\n\nImage illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened."} +{"id": "01344", "text": "You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.\n\nA subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray strictly greater than previous.\n\n\n-----Input-----\n\nThe first line contains single positive integer n (1 \u2264 n \u2264 10^5) \u2014 the number of integers.\n\nThe second line contains n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint the maximum length of an increasing subarray of the given array.\n\n\n-----Examples-----\nInput\n5\n1 7 2 11 15\n\nOutput\n3\n\nInput\n6\n100 100 100 100 100 100\n\nOutput\n1\n\nInput\n3\n1 2 3\n\nOutput\n3"} +{"id": "01345", "text": "Vasya has found a piece of paper with an array written on it. The array consists of n integers a_1, a_2, ..., a_{n}. Vasya noticed that the following condition holds for the array a_{i} \u2264 a_{i} + 1 \u2264 2\u00b7a_{i} for any positive integer i (i < n).\n\nVasya wants to add either a \"+\" or a \"-\" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs \"+\" and \"-\" before each number so that the value of expression s meets the limits 0 \u2264 s \u2264 a_1. Print a sequence of signs \"+\" and \"-\", satisfying the given limits. It is guaranteed that the solution for the problem exists.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the size of the array. The second line contains space-separated integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9) \u2014 the original array. \n\nIt is guaranteed that the condition a_{i} \u2264 a_{i} + 1 \u2264 2\u00b7a_{i} fulfills for any positive integer i (i < n).\n\n\n-----Output-----\n\nIn a single line print the sequence of n characters \"+\" and \"-\", where the i-th character is the sign that is placed in front of number a_{i}. The value of the resulting expression s must fit into the limits 0 \u2264 s \u2264 a_1. If there are multiple solutions, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n4\n1 2 3 5\n\nOutput\n+++-\nInput\n3\n3 3 5\n\nOutput\n++-"} +{"id": "01346", "text": "It is Professor R's last class of his teaching career. Every time Professor R taught a class, he gave a special problem for the students to solve. You being his favourite student, put your heart into solving it one last time.\n\nYou are given two polynomials $f(x) = a_0 + a_1x + \\dots + a_{n-1}x^{n-1}$ and $g(x) = b_0 + b_1x + \\dots + b_{m-1}x^{m-1}$, with positive integral coefficients. It is guaranteed that the cumulative GCD of the coefficients is equal to $1$ for both the given polynomials. In other words, $gcd(a_0, a_1, \\dots, a_{n-1}) = gcd(b_0, b_1, \\dots, b_{m-1}) = 1$. Let $h(x) = f(x)\\cdot g(x)$. Suppose that $h(x) = c_0 + c_1x + \\dots + c_{n+m-2}x^{n+m-2}$. \n\nYou are also given a prime number $p$. Professor R challenges you to find any $t$ such that $c_t$ isn't divisible by $p$. He guarantees you that under these conditions such $t$ always exists. If there are several such $t$, output any of them.\n\nAs the input is quite large, please use fast input reading methods.\n\n\n-----Input-----\n\nThe first line of the input contains three integers, $n$, $m$ and $p$ ($1 \\leq n, m \\leq 10^6, 2 \\leq p \\leq 10^9$), \u00a0\u2014 $n$ and $m$ are the number of terms in $f(x)$ and $g(x)$ respectively (one more than the degrees of the respective polynomials) and $p$ is the given prime number.\n\nIt is guaranteed that $p$ is prime.\n\nThe second line contains $n$ integers $a_0, a_1, \\dots, a_{n-1}$ ($1 \\leq a_{i} \\leq 10^{9}$)\u00a0\u2014 $a_i$ is the coefficient of $x^{i}$ in $f(x)$.\n\nThe third line contains $m$ integers $b_0, b_1, \\dots, b_{m-1}$ ($1 \\leq b_{i} \\leq 10^{9}$) \u00a0\u2014 $b_i$ is the coefficient of $x^{i}$ in $g(x)$.\n\n\n-----Output-----\n\nPrint a single integer $t$ ($0\\le t \\le n+m-2$) \u00a0\u2014 the appropriate power of $x$ in $h(x)$ whose coefficient isn't divisible by the given prime $p$. If there are multiple powers of $x$ that satisfy the condition, print any.\n\n\n-----Examples-----\nInput\n3 2 2\n1 1 2\n2 1\n\nOutput\n1\n\nInput\n2 2 999999937\n2 1\n3 1\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first test case, $f(x)$ is $2x^2 + x + 1$ and $g(x)$ is $x + 2$, their product $h(x)$ being $2x^3 + 5x^2 + 3x + 2$, so the answer can be 1 or 2 as both 3 and 5 aren't divisible by 2.\n\nIn the second test case, $f(x)$ is $x + 2$ and $g(x)$ is $x + 3$, their product $h(x)$ being $x^2 + 5x + 6$, so the answer can be any of the powers as no coefficient is divisible by the given prime."} +{"id": "01347", "text": "After you had helped Fedor to find friends in the \u00abCall of Soldiers 3\u00bb game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all. Now Fedor is going to change the essay using the synonym dictionary of the English language.\n\nFedor does not want to change the meaning of the essay. So the only change he would do: change a word from essay to one of its synonyms, basing on a replacement rule from the dictionary. Fedor may perform this operation any number of times.\n\nAs a result, Fedor wants to get an essay which contains as little letters \u00abR\u00bb (the case doesn't matter) as possible. If there are multiple essays with minimum number of \u00abR\u00bbs he wants to get the one with minimum length (length of essay is the sum of the lengths of all the words in it). Help Fedor get the required essay.\n\nPlease note that in this problem the case of letters doesn't matter. For example, if the synonym dictionary says that word cat can be replaced with word DOG, then it is allowed to replace the word Cat with the word doG.\n\n\n-----Input-----\n\nThe first line contains a single integer m (1 \u2264 m \u2264 10^5) \u2014 the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 10^5 characters.\n\nThe next line contains a single integer n (0 \u2264 n \u2264 10^5) \u2014 the number of pairs of words in synonym dictionary. The i-th of the next n lines contains two space-separated non-empty words x_{i} and y_{i}. They mean that word x_{i} can be replaced with word y_{i} (but not vise versa). It is guaranteed that the total length of all pairs of synonyms doesn't exceed 5\u00b710^5 characters.\n\nAll the words at input can only consist of uppercase and lowercase letters of the English alphabet.\n\n\n-----Output-----\n\nPrint two integers \u2014 the minimum number of letters \u00abR\u00bb in an optimal essay and the minimum length of an optimal essay.\n\n\n-----Examples-----\nInput\n3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y\n\nOutput\n2 6\n\nInput\n2\nRuruRu fedya\n1\nruruRU fedor\n\nOutput\n1 10"} +{"id": "01348", "text": "Valera had an undirected connected graph without self-loops and multiple edges consisting of n vertices. The graph had an interesting property: there were at most k edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to n.\n\nOne day Valera counted the shortest distances from one of the graph vertices to all other ones and wrote them out in array d. Thus, element d[i] of the array shows the shortest distance from the vertex Valera chose to vertex number i.\n\nThen something irreparable terrible happened. Valera lost the initial graph. However, he still has the array d. Help him restore the lost graph.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and k (1 \u2264 k < n \u2264 10^5). Number n shows the number of vertices in the original graph. Number k shows that at most k edges were adjacent to each vertex in the original graph.\n\nThe second line contains space-separated integers d[1], d[2], ..., d[n] (0 \u2264 d[i] < n). Number d[i] shows the shortest distance from the vertex Valera chose to the vertex number i.\n\n\n-----Output-----\n\nIf Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer m (0 \u2264 m \u2264 10^6) \u2014 the number of edges in the found graph.\n\nIn each of the next m lines print two space-separated integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n;\u00a0a_{i} \u2260 b_{i}), denoting the edge that connects vertices with numbers a_{i} and b_{i}. The graph shouldn't contain self-loops and multiple edges. If there are multiple possible answers, print any of them.\n\n\n-----Examples-----\nInput\n3 2\n0 1 1\n\nOutput\n3\n1 2\n1 3\n3 2\n\nInput\n4 2\n2 0 1 3\n\nOutput\n3\n1 3\n1 4\n2 3\n\nInput\n3 1\n0 0 0\n\nOutput\n-1"} +{"id": "01349", "text": "It is winter now, and Max decided it's about time he watered the garden.\n\nThe garden can be represented as n consecutive garden beds, numbered from 1 to n. k beds contain water taps (i-th tap is located in the bed x_{i}), which, if turned on, start delivering water to neighbouring beds. If the tap on the bed x_{i} is turned on, then after one second has passed, the bed x_{i} will be watered; after two seconds have passed, the beds from the segment [x_{i} - 1, x_{i} + 1] will be watered (if they exist); after j seconds have passed (j is an integer number), the beds from the segment [x_{i} - (j - 1), x_{i} + (j - 1)] will be watered (if they exist). Nothing changes during the seconds, so, for example, we can't say that the segment [x_{i} - 2.5, x_{i} + 2.5] will be watered after 2.5 seconds have passed; only the segment [x_{i} - 2, x_{i} + 2] will be watered at that moment.\n\n $\\left. \\begin{array}{|c|c|c|c|c|} \\hline 1 & {2} & {3} & {4} & {5} \\\\ \\hline \\end{array} \\right.$ The garden from test 1. White colour denotes a garden bed without a tap, red colour \u2014 a garden bed with a tap. \n\n $\\left. \\begin{array}{|c|c|c|c|c|} \\hline 1 & {2} & {3} & {4} & {5} \\\\ \\hline \\end{array} \\right.$ The garden from test 1 after 2 seconds have passed after turning on the tap. White colour denotes an unwatered garden bed, blue colour \u2014 a watered bed. \n\nMax wants to turn on all the water taps at the same moment, and now he wonders, what is the minimum number of seconds that have to pass after he turns on some taps until the whole garden is watered. Help him to find the answer!\n\n\n-----Input-----\n\nThe first line contains one integer t \u2014 the number of test cases to solve (1 \u2264 t \u2264 200).\n\nThen t test cases follow. The first line of each test case contains two integers n and k (1 \u2264 n \u2264 200, 1 \u2264 k \u2264 n) \u2014 the number of garden beds and water taps, respectively.\n\nNext line contains k integers x_{i} (1 \u2264 x_{i} \u2264 n) \u2014 the location of i-th water tap. It is guaranteed that for each $i \\in [ 2 ; k ]$ condition x_{i} - 1 < x_{i} holds.\n\nIt is guaranteed that the sum of n over all test cases doesn't exceed 200.\n\nNote that in hacks you have to set t = 1.\n\n\n-----Output-----\n\nFor each test case print one integer \u2014 the minimum number of seconds that have to pass after Max turns on some of the water taps, until the whole garden is watered.\n\n\n-----Example-----\nInput\n3\n5 1\n3\n3 3\n1 2 3\n4 1\n1\n\nOutput\n3\n1\n4\n\n\n\n-----Note-----\n\nThe first example consists of 3 tests:\n\n There are 5 garden beds, and a water tap in the bed 3. If we turn it on, then after 1 second passes, only bed 3 will be watered; after 2 seconds pass, beds [1, 3] will be watered, and after 3 seconds pass, everything will be watered. There are 3 garden beds, and there is a water tap in each one. If we turn all of them on, then everything will be watered after 1 second passes. There are 4 garden beds, and only one tap in the bed 1. It will take 4 seconds to water, for example, bed 4."} +{"id": "01350", "text": "You are given a string $s$ of length $n$, which consists only of the first $k$ letters of the Latin alphabet. All letters in string $s$ are uppercase.\n\nA subsequence of string $s$ is a string that can be derived from $s$ by deleting some of its symbols without changing the order of the remaining symbols. For example, \"ADE\" and \"BD\" are subsequences of \"ABCDE\", but \"DEA\" is not.\n\nA subsequence of $s$ called good if the number of occurences of each of the first $k$ letters of the alphabet is the same.\n\nFind the length of the longest good subsequence of $s$. \n\n\n-----Input-----\n\nThe first line of the input contains integers $n$ ($1\\le n \\le 10^5$) and $k$ ($1 \\le k \\le 26$).\n\nThe second line of the input contains the string $s$ of length $n$. String $s$ only contains uppercase letters from 'A' to the $k$-th letter of Latin alphabet.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the length of the longest good subsequence of string $s$.\n\n\n-----Examples-----\nInput\n9 3\nACAABCCAB\n\nOutput\n6\nInput\n9 4\nABCABCABC\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first example, \"ACBCAB\" (\"ACAABCCAB\") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence \"CAB\" also has the same frequency of these letters, but doesn't have the maximum possible length.\n\nIn the second example, none of the subsequences can have 'D', hence the answer is $0$."} +{"id": "01351", "text": "You have two integers $l$ and $r$. Find an integer $x$ which satisfies the conditions below:\n\n $l \\le x \\le r$. All digits of $x$ are different. \n\nIf there are multiple answers, print any of them.\n\n\n-----Input-----\n\nThe first line contains two integers $l$ and $r$ ($1 \\le l \\le r \\le 10^{5}$).\n\n\n-----Output-----\n\nIf an answer exists, print any of them. Otherwise, print $-1$.\n\n\n-----Examples-----\nInput\n121 130\n\nOutput\n123\n\nInput\n98766 100000\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example, $123$ is one of the possible answers. However, $121$ can't be the answer, because there are multiple $1$s on different digits.\n\nIn the second example, there is no valid answer."} +{"id": "01352", "text": "You are given an array consisting of $n$ integers $a_1, a_2, \\dots , a_n$ and an integer $x$. It is guaranteed that for every $i$, $1 \\le a_i \\le x$.\n\nLet's denote a function $f(l, r)$ which erases all values such that $l \\le a_i \\le r$ from the array $a$ and returns the resulting array. For example, if $a = [4, 1, 1, 4, 5, 2, 4, 3]$, then $f(2, 4) = [1, 1, 5]$.\n\nYour task is to calculate the number of pairs $(l, r)$ such that $1 \\le l \\le r \\le x$ and $f(l, r)$ is sorted in non-descending order. Note that the empty array is also considered sorted.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $x$ ($1 \\le n, x \\le 10^6$) \u2014 the length of array $a$ and the upper limit for its elements, respectively.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots a_n$ ($1 \\le a_i \\le x$).\n\n\n-----Output-----\n\nPrint the number of pairs $1 \\le l \\le r \\le x$ such that $f(l, r)$ is sorted in non-descending order.\n\n\n-----Examples-----\nInput\n3 3\n2 3 1\n\nOutput\n4\n\nInput\n7 4\n1 3 1 2 2 4 3\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first test case correct pairs are $(1, 1)$, $(1, 2)$, $(1, 3)$ and $(2, 3)$.\n\nIn the second test case correct pairs are $(1, 3)$, $(1, 4)$, $(2, 3)$, $(2, 4)$, $(3, 3)$ and $(3, 4)$."} +{"id": "01353", "text": "Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found out that she can buy a special ticket for m rides (she can buy it several times). It costs b rubles. Ann did the math; she will need to use subway n times. Help Ann, tell her what is the minimum sum of money she will have to spend to make n rides?\n\n\n-----Input-----\n\nThe single line contains four space-separated integers n, m, a, b (1 \u2264 n, m, a, b \u2264 1000) \u2014 the number of rides Ann has planned, the number of rides covered by the m ride ticket, the price of a one ride ticket and the price of an m ride ticket. \n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum sum in rubles that Ann will need to spend.\n\n\n-----Examples-----\nInput\n6 2 1 2\n\nOutput\n6\n\nInput\n5 2 2 3\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first sample one of the optimal solutions is: each time buy a one ride ticket. There are other optimal solutions. For example, buy three m ride tickets."} +{"id": "01354", "text": "Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 \u00d7 n table).\n\nAt the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 \u00d7 a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other.\n\nAfter that Bob makes a sequence of \"shots\". He names cells of the field and Alice either says that the cell is empty (\"miss\"), or that the cell belongs to some ship (\"hit\").\n\nBut here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a \"miss\". \n\nHelp Bob catch Alice cheating \u2014 find Bob's first move, such that after it you can be sure that Alice cheated.\n\n\n-----Input-----\n\nThe first line of the input contains three integers: n, k and a (1 \u2264 n, k, a \u2264 2\u00b710^5) \u2014 the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other.\n\nThe second line contains integer m (1 \u2264 m \u2264 n) \u2014 the number of Bob's moves.\n\nThe third line contains m distinct integers x_1, x_2, ..., x_{m}, where x_{i} is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print \"-1\".\n\n\n-----Examples-----\nInput\n11 3 3\n5\n4 8 6 1 11\n\nOutput\n3\n\nInput\n5 1 3\n2\n1 5\n\nOutput\n-1\n\nInput\n5 1 3\n1\n3\n\nOutput\n1"} +{"id": "01355", "text": "Given simple (without self-intersections) n-gon. It is not necessary convex. Also you are given m lines. For each line find the length of common part of the line and the n-gon.\n\nThe boundary of n-gon belongs to polygon. It is possible that n-gon contains 180-degree angles.\n\n\n-----Input-----\n\nThe first line contains integers n and m (3 \u2264 n \u2264 1000;1 \u2264 m \u2264 100). The following n lines contain coordinates of polygon vertices (in clockwise or counterclockwise direction). All vertices are distinct.\n\nThe following m lines contain line descriptions. Each of them contains two distict points of a line by their coordinates.\n\nAll given in the input coordinates are real numbers, given with at most two digits after decimal point. They do not exceed 10^5 by absolute values.\n\n\n-----Output-----\n\nPrint m lines, the i-th line should contain the length of common part of the given n-gon and the i-th line. The answer will be considered correct if the absolute or relative error doesn't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n4 3\n0 0\n1 0\n1 1\n0 1\n0 0 1 1\n0 0 0 1\n0 0 1 -1\n\nOutput\n1.41421356237309514547\n1.00000000000000000000\n0.00000000000000000000"} +{"id": "01356", "text": "Alice has a string $s$. She really likes the letter \"a\". She calls a string good if strictly more than half of the characters in that string are \"a\"s. For example \"aaabb\", \"axaa\" are good strings, and \"baca\", \"awwwa\", \"\" (empty string) are not.\n\nAlice can erase some characters from her string $s$. She would like to know what is the longest string remaining after erasing some characters (possibly zero) to get a good string. It is guaranteed that the string has at least one \"a\" in it, so the answer always exists.\n\n\n-----Input-----\n\nThe first line contains a string $s$ ($1 \\leq |s| \\leq 50$) consisting of lowercase English letters. It is guaranteed that there is at least one \"a\" in $s$.\n\n\n-----Output-----\n\nPrint a single integer, the length of the longest good string that Alice can get after erasing some characters from $s$.\n\n\n-----Examples-----\nInput\nxaxxxxa\n\nOutput\n3\n\nInput\naaabaa\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first example, it's enough to erase any four of the \"x\"s. The answer is $3$ since that is the maximum number of characters that can remain.\n\nIn the second example, we don't need to erase any characters."} +{"id": "01357", "text": "Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.\n\nXenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number a_{i} and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (2 \u2264 n \u2264 10^5, 1 \u2264 m \u2264 10^5). The second line contains m integers a_1, a_2, ..., a_{m} (1 \u2264 a_{i} \u2264 n). Note that Xenia can have multiple consecutive tasks in one house.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the time Xenia needs to complete all tasks.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n4 3\n3 2 3\n\nOutput\n6\n\nInput\n4 3\n2 3 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 \u2192 2 \u2192 3 \u2192 4 \u2192 1 \u2192 2 \u2192 3. This is optimal sequence. So, she needs 6 time units."} +{"id": "01358", "text": "Santa Claus likes palindromes very much. There was his birthday recently. k of his friends came to him to congratulate him, and each of them presented to him a string s_{i} having the same length n. We denote the beauty of the i-th string by a_{i}. It can happen that a_{i} is negative\u00a0\u2014 that means that Santa doesn't find this string beautiful at all.\n\nSanta Claus is crazy about palindromes. He is thinking about the following question: what is the maximum possible total beauty of a palindrome which can be obtained by concatenating some (possibly all) of the strings he has? Each present can be used at most once. Note that all strings have the same length n.\n\nRecall that a palindrome is a string that doesn't change after one reverses it.\n\nSince the empty string is a palindrome too, the answer can't be negative. Even if all a_{i}'s are negative, Santa can obtain the empty string.\n\n\n-----Input-----\n\nThe first line contains two positive integers k and n divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1 \u2264 k, n \u2264 100 000; n\u00b7k\u00a0 \u2264 100 000).\n\nk lines follow. The i-th of them contains the string s_{i} and its beauty a_{i} ( - 10 000 \u2264 a_{i} \u2264 10 000). The string consists of n lowercase English letters, and its beauty is integer. Some of strings may coincide. Also, equal strings can have different beauties.\n\n\n-----Output-----\n\nIn the only line print the required maximum possible beauty.\n\n\n-----Examples-----\nInput\n7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4\n\nOutput\n12\n\nInput\n3 1\na 1\na 2\na 3\n\nOutput\n6\n\nInput\n2 5\nabcde 10000\nabcde 10000\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order)."} +{"id": "01359", "text": "Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!\n\nTomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections a, b, c and d, such that there are two paths from a to c \u2014 one through b and the other one through d, he calls the group a \"damn rhombus\". Note that pairs (a, b), (b, c), (a, d), (d, c) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below: [Image] \n\nOther roads between any of the intersections don't make the rhombus any more appealing to Tomash, so the four intersections remain a \"damn rhombus\" for him.\n\nGiven that the capital of Berland has n intersections and m roads and all roads are unidirectional and are known in advance, find the number of \"damn rhombi\" in the city.\n\nWhen rhombi are compared, the order of intersections b and d doesn't matter.\n\n\n-----Input-----\n\nThe first line of the input contains a pair of integers n, m (1 \u2264 n \u2264 3000, 0 \u2264 m \u2264 30000) \u2014 the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of integers a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n;a_{i} \u2260 b_{i}) \u2014 the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.\n\nIt is not guaranteed that you can get from any intersection to any other one.\n\n\n-----Output-----\n\nPrint the required number of \"damn rhombi\".\n\n\n-----Examples-----\nInput\n5 4\n1 2\n2 3\n1 4\n4 3\n\nOutput\n1\n\nInput\n4 12\n1 2\n1 3\n1 4\n2 1\n2 3\n2 4\n3 1\n3 2\n3 4\n4 1\n4 2\n4 3\n\nOutput\n12"} +{"id": "01360", "text": "Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.\n\nAccording to the schedule, a student can take the exam for the i-th subject on the day number a_{i}. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day b_{i} (b_{i} < a_{i}). Thus, Valera can take an exam for the i-th subject either on day a_{i}, or on day b_{i}. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number a_{i}.\n\nValera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.\n\n\n-----Input-----\n\nThe first line contains a single positive integer n (1 \u2264 n \u2264 5000) \u2014 the number of exams Valera will take.\n\nEach of the next n lines contains two positive space-separated integers a_{i} and b_{i} (1 \u2264 b_{i} < a_{i} \u2264 10^9) \u2014 the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.\n\n\n-----Examples-----\nInput\n3\n5 2\n3 1\n4 2\n\nOutput\n2\n\nInput\n3\n6 1\n5 2\n4 3\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.\n\nIn the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject."} +{"id": "01361", "text": "Mike is trying rock climbing but he is awful at it. \n\nThere are n holds on the wall, i-th hold is at height a_{i} off the ground. Besides, let the sequence a_{i} increase, that is, a_{i} < a_{i} + 1 for all i from 1 to n - 1; we will call such sequence a track. Mike thinks that the track a_1, ..., a_{n} has difficulty $d = \\operatorname{max}_{1 \\leq i \\leq n - 1}(a_{i + 1} - a_{i})$. In other words, difficulty equals the maximum distance between two holds that are adjacent in height.\n\nToday Mike decided to cover the track with holds hanging on heights a_1, ..., a_{n}. To make the problem harder, Mike decided to remove one hold, that is, remove one element of the sequence (for example, if we take the sequence (1, 2, 3, 4, 5) and remove the third element from it, we obtain the sequence (1, 2, 4, 5)). However, as Mike is awful at climbing, he wants the final difficulty (i.e. the maximum difference of heights between adjacent holds after removing the hold) to be as small as possible among all possible options of removing a hold. The first and last holds must stay at their positions.\n\nHelp Mike determine the minimum difficulty of the track after removing one hold.\n\n\n-----Input-----\n\nThe first line contains a single integer n (3 \u2264 n \u2264 100)\u00a0\u2014 the number of holds.\n\nThe next line contains n space-separated integers a_{i} (1 \u2264 a_{i} \u2264 1000), where a_{i} is the height where the hold number i hangs. The sequence a_{i} is increasing (i.e. each element except for the first one is strictly larger than the previous one).\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum difficulty of the track after removing a single hold.\n\n\n-----Examples-----\nInput\n3\n1 4 6\n\nOutput\n5\n\nInput\n5\n1 2 3 4 5\n\nOutput\n2\n\nInput\n5\n1 2 3 7 8\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample you can remove only the second hold, then the sequence looks like (1, 6), the maximum difference of the neighboring elements equals 5.\n\nIn the second test after removing every hold the difficulty equals 2.\n\nIn the third test you can obtain sequences (1, 3, 7, 8), (1, 2, 7, 8), (1, 2, 3, 8), for which the difficulty is 4, 5 and 5, respectively. Thus, after removing the second element we obtain the optimal answer \u2014 4."} +{"id": "01362", "text": "A sum of p rubles is charged from Arkady's mobile phone account every day in the morning. Among the following m days, there are n days when Arkady will top up the account: in the day d_{i} he will deposit t_{i} rubles on his mobile phone account. Arkady will always top up the account before the daily payment will be done. There will be no other payments nor tops up in the following m days.\n\nDetermine the number of days starting from the 1-st to the m-th such that the account will have a negative amount on it after the daily payment (i.\u00a0e. in evening). Initially the account's balance is zero rubles.\n\n\n-----Input-----\n\nThe first line contains three integers n, p and m (1 \u2264 n \u2264 100 000, 1 \u2264 p \u2264 10^9, 1 \u2264 m \u2264 10^9, n \u2264 m) \u2014 the number of days Arkady will top up the account, the amount of the daily payment, and the number of days you should check.\n\nThe i-th of the following n lines contains two integers d_{i} and t_{i} (1 \u2264 d_{i} \u2264 m, 1 \u2264 t_{i} \u2264 10^9) \u2014 the index of the day when Arkady will make the i-th top up, and the amount he will deposit on this day. It is guaranteed that the indices of the days are distinct and are given in increasing order, i.\u00a0e. d_{i} > d_{i} - 1 for all i from 2 to n.\n\n\n-----Output-----\n\nPrint the number of days from the 1-st to the m-th such that the account will have a negative amount on it after the daily payment.\n\n\n-----Examples-----\nInput\n3 6 7\n2 13\n4 20\n7 9\n\nOutput\n3\n\nInput\n5 4 100\n10 70\n15 76\n21 12\n30 100\n67 85\n\nOutput\n26\n\n\n\n-----Note-----\n\nIn the first example the balance will change as following (remember, initially the balance is zero): in the first day 6 rubles will be charged, the balance in the evening will be equal to - 6; in the second day Arkady will deposit 13 rubles, then 6 rubles will be charged, the balance in the evening will be equal to 1; in the third day 6 rubles will be charged, the balance in the evening will be equal to - 5; in the fourth day Arkady will deposit 20 rubles, then 6 rubles will be charged, the balance in the evening will be equal to 9; in the fifth day 6 rubles will be charged, the balance in the evening will be equal to 3; in the sixth day 6 rubles will be charged, the balance in the evening will be equal to - 3; in the seventh day Arkady will deposit 9 rubles, then 6 rubles will be charged, the balance in the evening will be equal to 0. \n\nThus, in the end of the first, third and sixth days the balance will be negative in the end of the day."} +{"id": "01363", "text": "\u0417\u0430\u0432\u0442\u0440\u0430 \u0443 \u0445\u043e\u043a\u043a\u0435\u0439\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0438\u0442 \u0415\u0432\u0433\u0435\u043d\u0438\u0439, \u0432\u0430\u0436\u043d\u044b\u0439 \u043c\u0430\u0442\u0447. \u0415\u0432\u0433\u0435\u043d\u0438\u044e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0448\u0435\u0441\u0442\u044c \u0438\u0433\u0440\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0439\u0434\u0443\u0442 \u043d\u0430 \u043b\u0435\u0434 \u0432 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u043c \u0441\u043e\u0441\u0442\u0430\u0432\u0435: \u043e\u0434\u0438\u043d \u0432\u0440\u0430\u0442\u0430\u0440\u044c, \u0434\u0432\u0430 \u0437\u0430\u0449\u0438\u0442\u043d\u0438\u043a\u0430 \u0438 \u0442\u0440\u0438 \u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445.\n\n\u0422\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0439 \u0441\u043e\u0441\u0442\u0430\u0432, \u0415\u0432\u0433\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u043e\u043b\u043d\u0443\u0435\u0442, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u0440\u0430\u0441\u0438\u0432\u0430 \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043d\u0430 \u043b\u044c\u0434\u0443, \u0447\u0435\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0438\u0433\u0440\u043e\u043a\u043e\u0432. \u0410 \u0438\u043c\u0435\u043d\u043d\u043e, \u0415\u0432\u0433\u0435\u043d\u0438\u0439 \u0445\u043e\u0447\u0435\u0442 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0439 \u0441\u043e\u0441\u0442\u0430\u0432, \u0447\u0442\u043e\u0431\u044b \u043d\u043e\u043c\u0435\u0440\u0430 \u043b\u044e\u0431\u044b\u0445 \u0434\u0432\u0443\u0445 \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u0438\u0437 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u0430\u0432\u0430 \u043e\u0442\u043b\u0438\u0447\u0430\u043b\u0438\u0441\u044c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435, \u0447\u0435\u043c \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0433\u0440\u043e\u043a\u0438 \u0441 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438 13, 14, 10, 18, 15 \u0438 20 \u0443\u0441\u0442\u0440\u043e\u044f\u0442 \u0415\u0432\u0433\u0435\u043d\u0438\u044f, \u0430 \u0435\u0441\u043b\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430 \u043b\u0435\u0434 \u0432\u044b\u0439\u0434\u0443\u0442 \u0438\u0433\u0440\u043e\u043a\u0438 \u0441 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438 8 \u0438 17, \u0442\u043e \u044d\u0442\u043e \u043d\u0435 \u0443\u0441\u0442\u0440\u043e\u0438\u0442 \u0415\u0432\u0433\u0435\u043d\u0438\u044f.\n\n\u041f\u0440\u043e \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u0432\u0430\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u043d\u0430 \u043a\u0430\u043a\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043e\u043d \u0438\u0433\u0440\u0430\u0435\u0442 (\u0432\u0440\u0430\u0442\u0430\u0440\u044c, \u0437\u0430\u0449\u0438\u0442\u043d\u0438\u043a \u0438\u043b\u0438 \u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0439), \u0430 \u0442\u0430\u043a\u0436\u0435 \u0435\u0433\u043e \u043d\u043e\u043c\u0435\u0440. \u0412 \u0445\u043e\u043a\u043a\u0435\u0435 \u043d\u043e\u043c\u0435\u0440\u0430 \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0434\u0443\u0442 \u043f\u043e\u0434\u0440\u044f\u0434. \u041f\u043e\u0441\u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0445 \u0441\u043e\u0441\u0442\u0430\u0432\u043e\u0432 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0440\u0430\u0442\u0430\u0440\u044f, \u0434\u0432\u0443\u0445 \u0437\u0430\u0449\u0438\u0442\u043d\u0438\u043a\u043e\u0432 \u0438 \u0442\u0440\u0435\u0445 \u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0415\u0432\u0433\u0435\u043d\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u043e\u0441\u044c \u0435\u0433\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043a\u0440\u0430\u0441\u043e\u0442\u044b.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u041f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0442\u0440\u0438 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 g, d \u0438 f (1 \u2264 g \u2264 1 000, 1 \u2264 d \u2264 1 000, 1 \u2264 f \u2264 1 000)\u00a0\u2014 \u0447\u0438\u0441\u043b\u043e \u0432\u0440\u0430\u0442\u0430\u0440\u0435\u0439, \u0437\u0430\u0449\u0438\u0442\u043d\u0438\u043a\u043e\u0432 \u0438 \u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 \u0415\u0432\u0433\u0435\u043d\u0438\u044f. \n\n\u0412\u0442\u043e\u0440\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 g \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u043a\u0430\u0436\u0434\u043e\u0435 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u043e\u0442 1 \u0434\u043e 100 000\u00a0\u2014 \u043d\u043e\u043c\u0435\u0440\u0430 \u0432\u0440\u0430\u0442\u0430\u0440\u0435\u0439.\n\n\u0422\u0440\u0435\u0442\u044c\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 d \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u043a\u0430\u0436\u0434\u043e\u0435 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u043e\u0442 1 \u0434\u043e 100 000\u00a0\u2014 \u043d\u043e\u043c\u0435\u0440\u0430 \u0437\u0430\u0449\u0438\u0442\u043d\u0438\u043a\u043e\u0432.\n\n\u0427\u0435\u0442\u0432\u0435\u0440\u0442\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 f \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u043a\u0430\u0436\u0434\u043e\u0435 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u043e\u0442 1 \u0434\u043e 100 000\u00a0\u2014 \u043d\u043e\u043c\u0435\u0440\u0430 \u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445.\n\n\u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u043d\u0435 \u043f\u0440\u0435\u0432\u043e\u0441\u0445\u043e\u0434\u0438\u0442 1 000, \u0442.\u00a0\u0435. g + d + f \u2264 1 000. \u0412\u0441\u0435 g + d + f \u043d\u043e\u043c\u0435\u0440\u043e\u0432 \u0438\u0433\u0440\u043e\u043a\u043e\u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043e\u0434\u043d\u043e \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e\u00a0\u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0445 \u0441\u043e\u0441\u0442\u0430\u0432\u043e\u0432.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1 2 3\n15\n10 19\n20 11 13\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2 3 4\n16 40\n20 12 19\n13 21 11 10\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n6\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u0441\u043e\u0441\u0442\u0430\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0442\u0432\u0435\u0442 1.\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0438\u0433\u0440\u043e\u0432\u044b\u0435 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f (\u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u0440\u0430\u0442\u0430\u0440\u044c-\u0437\u0430\u0449\u0438\u0442\u043d\u0438\u043a-\u0437\u0430\u0449\u0438\u0442\u043d\u0438\u043a-\u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0439-\u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0439-\u043d\u0430\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0439): 16 20 12 13 21 11 16 20 12 13 11 10 16 20 19 13 21 11 16 20 19 13 11 10 16 12 19 13 21 11 16 12 19 13 11 10 \n\n\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u2014 6."} +{"id": "01364", "text": "Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers $n$ pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy.\n\nThe pieces of sushi are of two types: either with tuna or with eel. Let's denote the type of the $i$-th from the left sushi as $t_i$, where $t_i = 1$ means it is with tuna, and $t_i = 2$ means it is with eel.\n\nArkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment $[2, 2, 2, 1, 1, 1]$ is valid, but subsegment $[1, 2, 1, 2, 1, 2]$ is not, because both halves contain both types of sushi.\n\nFind the length of the longest continuous subsegment of sushi Arkady can buy.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 100\\,000$)\u00a0\u2014 the number of pieces of sushi.\n\nThe second line contains $n$ integers $t_1$, $t_2$, ..., $t_n$ ($t_i = 1$, denoting a sushi with tuna or $t_i = 2$, denoting a sushi with eel), representing the types of sushi from left to right.\n\nIt is guaranteed that there is at least one piece of sushi of each type. Note that it means that there is at least one valid continuous segment.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum length of a valid continuous segment.\n\n\n-----Examples-----\nInput\n7\n2 2 2 1 1 2 2\n\nOutput\n4\n\nInput\n6\n1 2 1 2 1 2\n\nOutput\n2\n\nInput\n9\n2 2 1 1 1 2 2 2 2\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first example Arkady can choose the subsegment $[2, 2, 1, 1]$ or the subsegment $[1, 1, 2, 2]$ with length $4$.\n\nIn the second example there is no way but to choose one of the subsegments $[2, 1]$ or $[1, 2]$ with length $2$.\n\nIn the third example Arkady's best choice is the subsegment $[1, 1, 1, 2, 2, 2]$."} +{"id": "01365", "text": "Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. \n\nOstap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them.\n\nAs this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 10^9 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 n \u2264 100, 0 \u2264 k \u2264 min(20, n - 1))\u00a0\u2014 the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k.\n\nEach of the next n - 1 lines contain two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n)\u00a0\u2014 indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the remainder of division of the number of ways to paint the tree by 1 000 000 007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n2 0\n1 2\n\nOutput\n1\n\nInput\n2 1\n1 2\n\nOutput\n3\n\nInput\n4 1\n1 2\n2 3\n3 4\n\nOutput\n9\n\nInput\n7 2\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7\n\nOutput\n91\n\n\n\n-----Note-----\n\nIn the first sample, Ostap has to paint both vertices black.\n\nIn the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.\n\nIn the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}."} +{"id": "01366", "text": "Sereja and his friends went to a picnic. The guys had n soda bottles just for it. Sereja forgot the bottle opener as usual, so the guys had to come up with another way to open bottles.\n\nSereja knows that the i-th bottle is from brand a_{i}, besides, you can use it to open other bottles of brand b_{i}. You can use one bottle to open multiple other bottles. Sereja can open bottle with opened bottle or closed bottle.\n\nKnowing this, Sereja wants to find out the number of bottles they've got that they won't be able to open in any way. Help him and find this number.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100) \u2014 the number of bottles. The next n lines contain the bottles' description. The i-th line contains two integers a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 1000) \u2014 the description of the i-th bottle.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n4\n1 1\n2 2\n3 3\n4 4\n\nOutput\n4\n\nInput\n4\n1 2\n2 3\n3 4\n4 1\n\nOutput\n0"} +{"id": "01367", "text": "Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom \"Graph Theory\". In total, the season has n episodes, numbered with integers from 1 to n.\n\nPolycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch?\n\n\n-----Input-----\n\nThe first line of the input contains integer n (2 \u2264 n \u2264 100000)\u00a0\u2014 the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n.\n\nThe second line contains n - 1 integer a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n)\u00a0\u2014 the numbers of episodes that Polycarpus has watched. All values of a_{i} are distinct.\n\n\n-----Output-----\n\nPrint the number of the episode that Polycarpus hasn't watched.\n\n\n-----Examples-----\nInput\n10\n3 8 10 1 7 9 6 5 2\n\nOutput\n4"} +{"id": "01368", "text": "You are given N items.\n\nThe value of the i-th item (1 \\leq i \\leq N) is v_i.\n\nYour have to select at least A and at most B of these items.\n\nUnder this condition, find the maximum possible arithmetic mean of the values of selected items.\n\nAdditionally, find the number of ways to select items so that the mean of the values of selected items is maximized. \n\n-----Constraints-----\n - 1 \\leq N \\leq 50\n - 1 \\leq A,B \\leq N\n - 1 \\leq v_i \\leq 10^{15}\n - Each v_i is an integer.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN A B\nv_1\nv_2\n...\nv_N\n\n-----Output-----\nPrint two lines.\n\nThe first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most 10^{-6}.\n\nThe second line should contain the number of ways to select items so that the mean of the values of selected items is maximized.\n\n-----Sample Input-----\n5 2 2\n1 2 3 4 5\n\n-----Sample Output-----\n4.500000\n1\n\nThe mean of the values of selected items will be maximized when selecting the fourth and fifth items. Hence, the first line of the output should contain 4.5.\n\nThere is no other way to select items so that the mean of the values will be 4.5, and thus the second line of the output should contain 1."} +{"id": "01369", "text": "Given are N points (x_i, y_i) in a two-dimensional plane.\nFind the minimum radius of a circle such that all the points are inside or on it.\n\n-----Constraints-----\n - 2 \\leq N \\leq 50\n - 0 \\leq x_i \\leq 1000\n - 0 \\leq y_i \\leq 1000\n - The given N points are all different.\n - The values in input are all integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nx_1 y_1\n:\nx_N y_N\n\n-----Output-----\nPrint the minimum radius of a circle such that all the N points are inside or on it.\nYour output will be considered correct if the absolute or relative error from our answer is at most 10^{-6}.\n\n-----Sample Input-----\n2\n0 0\n1 0\n\n-----Sample Output-----\n0.500000000000000000\n\nBoth points are contained in the circle centered at (0.5,0) with a radius of 0.5."} +{"id": "01370", "text": "We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.\nThe square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1.\nWe will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.\nHow many times do we need to cut the bar so that every block after the cuts has K or less white squares?\n\n-----Constraints-----\n - 1 \\leq H \\leq 10\n - 1 \\leq W \\leq 1000\n - 1 \\leq K \\leq H \\times W\n - S_{i,j} is 0 or 1.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W K\nS_{1,1}S_{1,2}...S_{1,W}\n:\nS_{H,1}S_{H,2}...S_{H,W}\n\n-----Output-----\nPrint the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares.\n\n-----Sample Input-----\n3 5 4\n11100\n10001\n00111\n\n-----Sample Output-----\n2\n\nFor example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works.\nNote that we cannot cut the bar in the ways shown in the two figures to the right."} +{"id": "01371", "text": "Given is an integer S.\nFind how many sequences there are whose terms are all integers greater than or equal to 3, and whose sum is equal to S.\nThe answer can be very large, so output it modulo 10^9 + 7.\n\n-----Constraints-----\n - 1 \\leq S \\leq 2000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n7\n\n-----Sample Output-----\n3\n\n3 sequences satisfy the condition: \\{3,4\\}, \\{4,3\\} and \\{7\\}."} +{"id": "01372", "text": "Ibis is fighting with a monster.\nThe health of the monster is H.\nIbis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.\nThe same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.\nIbis wins when the health of the monster becomes 0 or below.\nFind the minimum total Magic Points that have to be consumed before winning.\n\n-----Constraints-----\n - 1 \\leq H \\leq 10^4\n - 1 \\leq N \\leq 10^3\n - 1 \\leq A_i \\leq 10^4\n - 1 \\leq B_i \\leq 10^4\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH N\nA_1 B_1\n:\nA_N B_N\n\n-----Output-----\nPrint the minimum total Magic Points that have to be consumed before winning.\n\n-----Sample Input-----\n9 3\n8 3\n4 2\n2 1\n\n-----Sample Output-----\n4\n\nFirst, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.\nThen, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.\nIn this way, we can win at the total cost of 4 Magic Points."} +{"id": "01373", "text": "We have N+1 integers: 10^{100}, 10^{100}+1, ..., 10^{100}+N.\nWe will choose K or more of these integers. Find the number of possible values of the sum of the chosen numbers, modulo (10^9+7).\n\n-----Constraints-----\n - 1 \\leq N \\leq 2\\times 10^5\n - 1 \\leq K \\leq N+1\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\n\n-----Output-----\nPrint the number of possible values of the sum, modulo (10^9+7).\n\n-----Sample Input-----\n3 2\n\n-----Sample Output-----\n10\n\nThe sum can take 10 values, as follows:\n - (10^{100})+(10^{100}+1)=2\\times 10^{100}+1\n - (10^{100})+(10^{100}+2)=2\\times 10^{100}+2\n - (10^{100})+(10^{100}+3)=(10^{100}+1)+(10^{100}+2)=2\\times 10^{100}+3\n - (10^{100}+1)+(10^{100}+3)=2\\times 10^{100}+4\n - (10^{100}+2)+(10^{100}+3)=2\\times 10^{100}+5\n - (10^{100})+(10^{100}+1)+(10^{100}+2)=3\\times 10^{100}+3\n - (10^{100})+(10^{100}+1)+(10^{100}+3)=3\\times 10^{100}+4\n - (10^{100})+(10^{100}+2)+(10^{100}+3)=3\\times 10^{100}+5\n - (10^{100}+1)+(10^{100}+2)+(10^{100}+3)=3\\times 10^{100}+6\n - (10^{100})+(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=4\\times 10^{100}+6"} +{"id": "01374", "text": "We will define the median of a sequence b of length M, as follows:\n - Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.\nFor example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.\nSnuke comes up with the following problem.\nYou are given a sequence a of length N.\nFor each pair (l, r) (1 \\leq l \\leq r \\leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a.\nWe will list m_{l, r} for all pairs (l, r) to create a new sequence m.\nFind the median of m.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - a_i is an integer.\n - 1 \\leq a_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the median of m.\n\n-----Sample Input-----\n3\n10 30 20\n\n-----Sample Output-----\n30\n\nThe median of each contiguous subsequence of a is as follows:\n - The median of (10) is 10.\n - The median of (30) is 30.\n - The median of (20) is 20.\n - The median of (10, 30) is 30.\n - The median of (30, 20) is 30.\n - The median of (10, 30, 20) is 20.\nThus, m = (10, 30, 20, 30, 30, 20) and the median of m is 30."} +{"id": "01375", "text": "You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same. \n\nMore formally, you need to find the number of such pairs of indices i, j (2 \u2264 i \u2264 j \u2264 n - 1), that $\\sum_{k = 1}^{i - 1} a_{k} = \\sum_{k = i}^{j} a_{k} = \\sum_{k = j + 1}^{n} a_{k}$.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 5\u00b710^5), showing how many numbers are in the array. The second line contains n integers a[1], a[2], ..., a[n] (|a[i]| \u2264 10^9) \u2014 the elements of array a.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of ways to split the array into three parts with the same sum.\n\n\n-----Examples-----\nInput\n5\n1 2 3 0 3\n\nOutput\n2\n\nInput\n4\n0 1 -1 0\n\nOutput\n1\n\nInput\n2\n4 1\n\nOutput\n0"} +{"id": "01376", "text": "Sasha and Dima want to buy two $n$-tier cakes. Each cake should consist of $n$ different tiers: from the size of $1$ to the size of $n$. Tiers should go in order from the smallest to the biggest (from top to bottom).\n\nThey live on the same street, there are $2 \\cdot n$ houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the $i$-th house you can buy a tier of the size $a_i$ ($1 \\le a_i \\le n$).\n\nSince the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: $1$, then $2$, then $3$ and so on up to $n$.\n\nInitially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly $1$.\n\n\n-----Input-----\n\nThe first line of the input contains an integer number $n$ \u2014 the number of tiers in each cake ($1 \\le n \\le 10^5$).\n\nThe second line contains $2 \\cdot n$ integers $a_1, a_2, \\dots, a_{2n}$ ($1 \\le a_i \\le n$), where $a_i$ is equal to the size of the tier, which can be bought in the $i$-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from $1$ to $n$ occurs in $a$ exactly two times.\n\n\n-----Output-----\n\nPrint one number \u00a0\u2014 the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy $n$ tiers in ascending order of their sizes.\n\n\n-----Examples-----\nInput\n3\n1 1 2 2 3 3\n\nOutput\n9\n\nInput\n2\n2 1 1 2\n\nOutput\n5\n\nInput\n4\n4 1 3 2 2 3 1 4\n\nOutput\n17\n\n\n\n-----Note-----\n\nIn the first example, the possible optimal sequence of actions is: Sasha buys a tier of size $1$ near the $1$-st house ($a_1=1$); Dima goes to the house $2$; Dima buys a tier of size $1$ near the $2$-nd house ($a_2=1$); Sasha goes to the house $4$; Sasha buys a tier of size $2$ near the $4$-th house ($a_4=2$); Sasha goes to the house $5$; Sasha buys a tier of size $3$ near the $5$-th house ($a_5=3$); Dima goes to the house $3$; Dima buys a tier of size $2$ near the $3$-rd house ($a_3=2$); Dima goes to the house $6$; Dima buys a tier of size $3$ near the $6$-th house ($a_6=3$). \n\nSo, Sasha goes the distance $3+1=4$, and Dima goes the distance $1+1+3=5$. In total, they cover a distance of $4+5=9$. You can make sure that with any other sequence of actions they will walk no less distance."} +{"id": "01377", "text": "There are $n$ pillars aligned in a row and numbered from $1$ to $n$.\n\nInitially each pillar contains exactly one disk. The $i$-th pillar contains a disk having radius $a_i$.\n\nYou can move these disks from one pillar to another. You can take a disk from pillar $i$ and place it on top of pillar $j$ if all these conditions are met:\n\n there is no other pillar between pillars $i$ and $j$. Formally, it means that $|i - j| = 1$; pillar $i$ contains exactly one disk; either pillar $j$ contains no disks, or the topmost disk on pillar $j$ has radius strictly greater than the radius of the disk you move. \n\nWhen you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.\n\nYou may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $n$ disks on the same pillar simultaneously?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($3 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of pillars.\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_i$ ($1 \\le a_i \\le n$), where $a_i$ is the radius of the disk initially placed on the $i$-th pillar. All numbers $a_i$ are distinct.\n\n\n-----Output-----\n\nPrint YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).\n\n\n-----Examples-----\nInput\n4\n1 3 4 2\n\nOutput\nYES\n\nInput\n3\n3 1 2\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first case it is possible to place all disks on pillar $3$ using the following sequence of actions:\n\n take the disk with radius $3$ from pillar $2$ and place it on top of pillar $3$; take the disk with radius $1$ from pillar $1$ and place it on top of pillar $2$; take the disk with radius $2$ from pillar $4$ and place it on top of pillar $3$; take the disk with radius $1$ from pillar $2$ and place it on top of pillar $3$."} +{"id": "01378", "text": "Bill is a famous mathematician in BubbleLand. Thanks to his revolutionary math discoveries he was able to make enough money to build a beautiful house. Unfortunately, for not paying property tax on time, court decided to punish Bill by making him lose a part of his property.\n\nBill\u2019s property can be observed as a convex regular 2n-sided polygon A_0 A_1... A_2n - 1 A_2n, A_2n = A_0, with sides of the exactly 1 meter in length. \n\nCourt rules for removing part of his property are as follows: Split every edge A_{k} A_{k} + 1, k = 0... 2n - 1 in n equal parts of size 1 / n with points P_0, P_1, ..., P_{n} - 1 On every edge A_2k A_2k + 1, k = 0... n - 1 court will choose one point B_2k = P_{i} for some i = 0, ..., n - 1 such that $\\cup_{i = 0}^{n - 1} B_{2i} = \\cup_{i = 0}^{n - 1} P_{i}$ On every edge A_2k + 1A_2k + 2, k = 0...n - 1 Bill will choose one point B_2k + 1 = P_{i} for some i = 0, ..., n - 1 such that $\\cup_{i = 0}^{n - 1} B_{2 i + 1} = \\cup_{i = 0}^{n - 1} P_{i}$ Bill gets to keep property inside of 2n-sided polygon B_0 B_1... B_2n - 1 \n\nLuckily, Bill found out which B_2k points the court chose. Even though he is a great mathematician, his house is very big and he has a hard time calculating. Therefore, he is asking you to help him choose points so he maximizes area of property he can keep.\n\n\n-----Input-----\n\nThe first line contains one integer number n (2 \u2264 n \u2264 50000), representing number of edges of 2n-sided polygon.\n\nThe second line contains n distinct integer numbers B_2k (0 \u2264 B_2k \u2264 n - 1, k = 0... n - 1) separated by a single space, representing points the court chose. If B_2k = i, the court chose point P_{i} on side A_2k A_2k + 1.\n\n\n-----Output-----\n\nOutput contains n distinct integers separated by a single space representing points B_1, B_3, ..., B_2n - 1 Bill should choose in order to maximize the property area. If there are multiple solutions that maximize the area, return any of them.\n\n\n-----Example-----\nInput\n3\n0 1 2\n\nOutput\n0 2 1\n\n\n\n-----Note-----\n\nTo maximize area Bill should choose points: B_1 = P_0, B_3 = P_2, B_5 = P_1\n\n[Image]"} +{"id": "01379", "text": "Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \\dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). \n\nHowever, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day.\n\nFor each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. \n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$, $d$ $(1 \\le n \\le 2\\cdot10^{5}, n \\le m \\le 10^{9}, 1 \\le d \\le m)$\u00a0\u2014 the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks.\n\nThe second line contains $n$ distinct integers $a_1, a_2, \\dots, a_n$ $(1 \\le a_i \\le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break.\n\n\n-----Output-----\n\nIn the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. \n\nIn the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them.\n\n\n-----Examples-----\nInput\n4 5 3\n3 5 1 2\n\nOutput\n3\n3 1 1 2 \n\nInput\n10 10 1\n10 5 7 4 6 3 2 1 9 8\n\nOutput\n2\n2 1 1 2 2 1 2 1 1 2 \n\n\n\n-----Note-----\n\nIn the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$).\n\nIn the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day."} +{"id": "01380", "text": "The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 \u2264 i \u2264 n) tree from the left has height a_{i} meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1 \u2264 i < n), a_{i} + 1 - a_{i} = k, where k is the number the Queen chose.\n\nUnfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes?\n\n\n-----Input-----\n\nThe first line contains two space-separated integers: n, k (1 \u2264 n, k \u2264 1000). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1000) \u2014 the heights of the trees in the row. \n\n\n-----Output-----\n\nIn the first line print a single integer p \u2014 the minimum number of minutes the gardener needs. In the next p lines print the description of his actions. \n\nIf the gardener needs to increase the height of the j-th (1 \u2264 j \u2264 n) tree from the left by x (x \u2265 1) meters, then print in the corresponding line \"+\u00a0j\u00a0x\". If the gardener needs to decrease the height of the j-th (1 \u2264 j \u2264 n) tree from the left by x (x \u2265 1) meters, print on the corresponding line \"-\u00a0j\u00a0x\".\n\nIf there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n4 1\n1 2 1 5\n\nOutput\n2\n+ 3 2\n- 4 1\n\nInput\n4 1\n1 2 3 4\n\nOutput\n0"} +{"id": "01381", "text": "To make a paper airplane, one has to use a rectangular piece of paper. From a sheet of standard size you can make $s$ airplanes.\n\nA group of $k$ people decided to make $n$ airplanes each. They are going to buy several packs of paper, each of them containing $p$ sheets, and then distribute the sheets between the people. Each person should have enough sheets to make $n$ airplanes. How many packs should they buy?\n\n\n-----Input-----\n\nThe only line contains four integers $k$, $n$, $s$, $p$ ($1 \\le k, n, s, p \\le 10^4$)\u00a0\u2014 the number of people, the number of airplanes each should make, the number of airplanes that can be made using one sheet and the number of sheets in one pack, respectively.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of packs they should buy.\n\n\n-----Examples-----\nInput\n5 3 2 3\n\nOutput\n4\n\nInput\n5 3 100 1\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first sample they have to buy $4$ packs of paper: there will be $12$ sheets in total, and giving $2$ sheets to each person is enough to suit everyone's needs.\n\nIn the second sample they have to buy a pack for each person as they can't share sheets."} +{"id": "01382", "text": "After Vitaly was expelled from the university, he became interested in the graph theory.\n\nVitaly especially liked the cycles of an odd length in which each vertex occurs at most once.\n\nVitaly was wondering how to solve the following problem. You are given an undirected graph consisting of n vertices and m edges, not necessarily connected, without parallel edges and loops. You need to find t \u2014 the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find w \u2014 the number of ways to add t edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.\n\nTwo ways to add edges to the graph are considered equal if they have the same sets of added edges.\n\nSince Vitaly does not study at the university, he asked you to help him with this task.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m ($3 \\leq n \\leq 10^{5}, 0 \\leq m \\leq \\operatorname{min}(\\frac{n(n - 1)}{2}, 10^{5})$\u00a0\u2014\u00a0the number of vertices in the graph and the number of edges in the graph.\n\nNext m lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n)\u00a0\u2014\u00a0the vertices that are connected by the i-th edge. All numbers in the lines are separated by a single space.\n\nIt is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected.\n\n\n-----Output-----\n\nPrint in the first line of the output two space-separated integers t and w\u00a0\u2014\u00a0the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.\n\n\n-----Examples-----\nInput\n4 4\n1 2\n1 3\n4 2\n4 3\n\nOutput\n1 2\n\nInput\n3 3\n1 2\n2 3\n3 1\n\nOutput\n0 1\n\nInput\n3 0\n\nOutput\n3 1\n\n\n\n-----Note-----\n\nThe simple cycle is a cycle that doesn't contain any vertex twice."} +{"id": "01383", "text": "You are given a positive integer $m$ and two integer sequence: $a=[a_1, a_2, \\ldots, a_n]$ and $b=[b_1, b_2, \\ldots, b_n]$. Both of these sequence have a length $n$.\n\nPermutation is a sequence of $n$ different positive integers from $1$ to $n$. For example, these sequences are permutations: $[1]$, $[1,2]$, $[2,1]$, $[6,7,3,4,1,2,5]$. These are not: $[0]$, $[1,1]$, $[2,3]$.\n\nYou need to find the non-negative integer $x$, and increase all elements of $a_i$ by $x$, modulo $m$ (i.e. you want to change $a_i$ to $(a_i + x) \\bmod m$), so it would be possible to rearrange elements of $a$ to make it equal $b$, among them you need to find the smallest possible $x$.\n\nIn other words, you need to find the smallest non-negative integer $x$, for which it is possible to find some permutation $p=[p_1, p_2, \\ldots, p_n]$, such that for all $1 \\leq i \\leq n$, $(a_i + x) \\bmod m = b_{p_i}$, where $y \\bmod m$\u00a0\u2014 remainder of division of $y$ by $m$.\n\nFor example, if $m=3$, $a = [0, 0, 2, 1], b = [2, 0, 1, 1]$, you can choose $x=1$, and $a$ will be equal to $[1, 1, 0, 2]$ and you can rearrange it to make it equal $[2, 0, 1, 1]$, which is equal to $b$.\n\n\n-----Input-----\n\nThe first line contains two integers $n,m$ ($1 \\leq n \\leq 2000, 1 \\leq m \\leq 10^9$): number of elemens in arrays and $m$.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\leq a_i < m$).\n\nThe third line contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($0 \\leq b_i < m$).\n\nIt is guaranteed that there exists some non-negative integer $x$, such that it would be possible to find some permutation $p_1, p_2, \\ldots, p_n$ such that $(a_i + x) \\bmod m = b_{p_i}$.\n\n\n-----Output-----\n\nPrint one integer, the smallest non-negative integer $x$, such that it would be possible to find some permutation $p_1, p_2, \\ldots, p_n$ such that $(a_i + x) \\bmod m = b_{p_i}$ for all $1 \\leq i \\leq n$.\n\n\n-----Examples-----\nInput\n4 3\n0 0 2 1\n2 0 1 1\n\nOutput\n1\n\nInput\n3 2\n0 0 0\n1 1 1\n\nOutput\n1\n\nInput\n5 10\n0 0 0 1 2\n2 1 0 0 0\n\nOutput\n0"} +{"id": "01384", "text": "Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job.\n\nDuring all his career Hideo has produced n games. Some of them were successful, some were not. Hideo wants to remove several of them (possibly zero) from his CV to make a better impression on employers. As a result there should be no unsuccessful game which comes right after successful one in his CV.\n\nMore formally, you are given an array s_1, s_2, ..., s_{n} of zeros and ones. Zero corresponds to an unsuccessful game, one \u2014 to a successful one. Games are given in order they were produced, and Hideo can't swap these values. He should remove some elements from this array in such a way that no zero comes right after one.\n\nBesides that, Hideo still wants to mention as much games in his CV as possible. Help this genius of a man determine the maximum number of games he can leave in his CV.\n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 100).\n\nThe second line contains n space-separated integer numbers s_1, s_2, ..., s_{n} (0 \u2264 s_{i} \u2264 1). 0 corresponds to an unsuccessful game, 1 \u2014 to a successful one.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of games Hideo can leave in his CV so that no unsuccessful game comes after a successful one.\n\n\n-----Examples-----\nInput\n4\n1 1 0 1\n\nOutput\n3\n\nInput\n6\n0 1 0 0 1 0\n\nOutput\n4\n\nInput\n1\n0\n\nOutput\n1"} +{"id": "01385", "text": "The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document.\n\nIn the Pindows operating system a strings are the lexemes of the command line \u2014 the first of them is understood as the name of the program to run and the following lexemes are its arguments. For example, as we execute the command \" run.exe one, two . \", we give four lexemes to the Pindows command line: \"run.exe\", \"one,\", \"two\", \".\". More formally, if we run a command that can be represented as string s (that has no quotes), then the command line lexemes are maximal by inclusion substrings of string s that contain no spaces.\n\nTo send a string with spaces or an empty string as a command line lexeme, we can use double quotes. The block of characters that should be considered as one lexeme goes inside the quotes. Embedded quotes are prohibited \u2014 that is, for each occurrence of character \"\"\" we should be able to say clearly that the quotes are opening or closing. For example, as we run the command \"\"run.exe o\" \"\" \" ne, \" two . \" \" \", we give six lexemes to the Pindows command line: \"run.exe o\", \"\" (an empty string), \" ne, \", \"two\", \".\", \" \" (a single space).\n\nIt is guaranteed that each lexeme of the command line is either surrounded by spaces on both sides or touches the corresponding command border. One of its consequences is: the opening brackets are either the first character of the string or there is a space to the left of them.\n\nYou have a string that consists of uppercase and lowercase English letters, digits, characters \".,?!\"\" and spaces. It is guaranteed that this string is a correct OS Pindows command line string. Print all lexemes of this command line string. Consider the character \"\"\" to be used only in order to denote a single block of characters into one command line lexeme. In particular, the consequence is that the given string has got an even number of such characters.\n\n\n-----Input-----\n\nThe single line contains a non-empty string s. String s consists of at most 10^5 characters. Each character is either an uppercase or a lowercase English letter, or a digit, or one of the \".,?!\"\" signs, or a space.\n\nIt is guaranteed that the given string is some correct command line string of the OS Pindows. It is guaranteed that the given command line string contains at least one lexeme.\n\n\n-----Output-----\n\nIn the first line print the first lexeme, in the second line print the second one and so on. To make the output clearer, print the \"<\" (less) character to the left of your lexemes and the \">\" (more) character to the right. Print the lexemes in the order in which they occur in the command.\n\nPlease, follow the given output format strictly. For more clarifications on the output format see the test samples.\n\n\n-----Examples-----\nInput\n\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"\n\nOutput\n\n<>\n< 2ne, >\n\n<.>\n< >\n\nInput\n firstarg second \"\" \n\nOutput\n\n\n<>"} +{"id": "01386", "text": "Bob is decorating his kitchen, more precisely, the floor. He has found a prime candidate for the tiles he will use. They come in a simple form factor\u00a0\u2014\u00a0a square tile that is diagonally split into white and black part as depicted in the figure below. [Image] \n\nThe dimension of this tile is perfect for this kitchen, as he will need exactly $w \\times h$ tiles without any scraps. That is, the width of the kitchen is $w$ tiles, and the height is $h$ tiles. As each tile can be rotated in one of four ways, he still needs to decide on how exactly he will tile the floor. There is a single aesthetic criterion that he wants to fulfil: two adjacent tiles must not share a colour on the edge\u00a0\u2014\u00a0i.e. one of the tiles must have a white colour on the shared border, and the second one must be black. [Image] The picture on the left shows one valid tiling of a $3 \\times 2$ kitchen. The picture on the right shows an invalid arrangement, as the bottom two tiles touch with their white parts. \n\nFind the number of possible tilings. As this number may be large, output its remainder when divided by $998244353$ (a prime number). \n\n\n-----Input-----\n\nThe only line contains two space separated integers $w$, $h$\u00a0($1 \\leq w,h \\leq 1\\,000$)\u00a0\u2014\u00a0the width and height of the kitchen, measured in tiles.\n\n\n-----Output-----\n\nOutput a single integer $n$\u00a0\u2014\u00a0the remainder of the number of tilings when divided by $998244353$.\n\n\n-----Examples-----\nInput\n2 2\n\nOutput\n16\n\nInput\n2 4\n\nOutput\n64"} +{"id": "01387", "text": "New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 \u00d7 n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.\n\nSo, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1 positive integers a_1, a_2, ..., a_{n} - 1. For every integer i where 1 \u2264 i \u2264 n - 1 the condition 1 \u2264 a_{i} \u2264 n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 \u2264 i \u2264 n - 1) portal connects cell i and cell (i + a_{i}), and one can travel from cell i to cell (i + a_{i}) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + a_{i}) to cell i using the i-th portal. It is easy to see that because of condition 1 \u2264 a_{i} \u2264 n - i one can't leave the Line World using portals.\n\nCurrently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n (3 \u2264 n \u2264 3 \u00d7 10^4) and t (2 \u2264 t \u2264 n) \u2014 the number of cells, and the index of the cell which I want to go to.\n\nThe second line contains n - 1 space-separated integers a_1, a_2, ..., a_{n} - 1 (1 \u2264 a_{i} \u2264 n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.\n\n\n-----Output-----\n\nIf I can go to cell t using the transportation system, print \"YES\". Otherwise, print \"NO\".\n\n\n-----Examples-----\nInput\n8 4\n1 2 1 2 1 2 1\n\nOutput\nYES\n\nInput\n8 5\n1 2 1 2 1 1 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.\n\nIn the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit."} +{"id": "01388", "text": "Ashish has a tree consisting of $n$ nodes numbered $1$ to $n$ rooted at node $1$. The $i$-th node in the tree has a cost $a_i$, and binary digit $b_i$ is written in it. He wants to have binary digit $c_i$ written in the $i$-th node in the end.\n\nTo achieve this, he can perform the following operation any number of times: Select any $k$ nodes from the subtree of any node $u$, and shuffle the digits in these nodes as he wishes, incurring a cost of $k \\cdot a_u$. Here, he can choose $k$ ranging from $1$ to the size of the subtree of $u$. \n\nHe wants to perform the operations in such a way that every node finally has the digit corresponding to its target.\n\nHelp him find the minimum total cost he needs to spend so that after all the operations, every node $u$ has digit $c_u$ written in it, or determine that it is impossible.\n\n\n-----Input-----\n\nFirst line contains a single integer $n$ $(1 \\le n \\le 2 \\cdot 10^5)$ denoting the number of nodes in the tree.\n\n$i$-th line of the next $n$ lines contains 3 space-separated integers $a_i$, $b_i$, $c_i$ $(1 \\leq a_i \\leq 10^9, 0 \\leq b_i, c_i \\leq 1)$ \u00a0\u2014 the cost of the $i$-th node, its initial digit and its goal digit.\n\nEach of the next $n - 1$ lines contain two integers $u$, $v$ $(1 \\leq u, v \\leq n, \\text{ } u \\ne v)$, meaning that there is an edge between nodes $u$ and $v$ in the tree.\n\n\n-----Output-----\n\nPrint the minimum total cost to make every node reach its target digit, and $-1$ if it is impossible.\n\n\n-----Examples-----\nInput\n5\n1 0 1\n20 1 0\n300 0 1\n4000 0 0\n50000 1 0\n1 2\n2 3\n2 4\n1 5\n\nOutput\n4\nInput\n5\n10000 0 1\n2000 1 0\n300 0 1\n40 0 0\n1 1 0\n1 2\n2 3\n2 4\n1 5\n\nOutput\n24000\nInput\n2\n109 0 1\n205 0 1\n1 2\n\nOutput\n-1\n\n\n-----Note-----\n\nThe tree corresponding to samples $1$ and $2$ are: [Image]\n\nIn sample $1$, we can choose node $1$ and $k = 4$ for a cost of $4 \\cdot 1$ = $4$ and select nodes ${1, 2, 3, 5}$, shuffle their digits and get the desired digits in every node.\n\nIn sample $2$, we can choose node $1$ and $k = 2$ for a cost of $10000 \\cdot 2$, select nodes ${1, 5}$ and exchange their digits, and similarly, choose node $2$ and $k = 2$ for a cost of $2000 \\cdot 2$, select nodes ${2, 3}$ and exchange their digits to get the desired digits in every node.\n\nIn sample $3$, it is impossible to get the desired digits, because there is no node with digit $1$ initially."} +{"id": "01389", "text": "The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.\n\nLet's consider a rectangular image that is represented with a table of size n \u00d7 m. The table elements are integers that specify the brightness of each pixel in the image.\n\nA feature also is a rectangular table of size n \u00d7 m. Each cell of a feature is painted black or white.\n\nTo calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.\n\nSome examples of the most popular Haar features are given below. [Image] \n\nYour task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.\n\nA prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.\n\nYou have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values \u200b\u200bat any prefix rectangle, multiply it by any integer and add to variable value.\n\nYou are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m (1 \u2264 n, m \u2264 100) \u2014 the number of rows and columns in the feature.\n\nNext n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to \"W\", if this element of the feature is white and \"B\" if it is black.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum number of operations that you need to make to calculate the value of the feature.\n\n\n-----Examples-----\nInput\n6 8\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW\n\nOutput\n2\n\nInput\n3 3\nWBW\nBWW\nWWW\n\nOutput\n4\n\nInput\n3 6\nWWBBWW\nWWBBWW\nWWBBWW\n\nOutput\n3\n\nInput\n4 4\nBBBB\nBBBB\nBBBB\nBBBW\n\nOutput\n4\n\n\n\n-----Note-----\n\nThe first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 \u00d7 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:\n\n add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); $\\left. \\begin{array}{|r|r|r|r|r|r|r|r|} \\hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\\\ \\hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\\\ \\hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\\\ \\hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\\\ \\hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\\\ \\hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\\\ \\hline \\end{array} \\right.$\n\n add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient - 2 and variable value. [Image] \n\nThus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required."} +{"id": "01390", "text": "The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces).\n\nThe shop assistant told the teacher that there are m puzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consists of f_1 pieces, the second one consists of f_2 pieces and so on.\n\nMs. Manana doesn't want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. Let A be the number of pieces in the largest puzzle that the teacher buys and B be the number of pieces in the smallest such puzzle. She wants to choose such n puzzles that A - B is minimum possible. Help the teacher and find the least possible value of A - B.\n\n\n-----Input-----\n\nThe first line contains space-separated integers n and m (2 \u2264 n \u2264 m \u2264 50). The second line contains m space-separated integers f_1, f_2, ..., f_{m} (4 \u2264 f_{i} \u2264 1000) \u2014 the quantities of pieces in the puzzles sold in the shop.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the least possible difference the teacher can obtain.\n\n\n-----Examples-----\nInput\n4 6\n10 12 10 7 5 22\n\nOutput\n5\n\n\n\n-----Note-----\n\nSample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5."} +{"id": "01391", "text": "A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them.\n\nThe renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs p_{j} rubles.\n\nIn total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has b_{i} personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike.\n\nEach boy can rent at most one bike, one cannot give his bike to somebody else.\n\nWhat maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible?\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, m and a (1 \u2264 n, m \u2264 10^5; 0 \u2264 a \u2264 10^9). The second line contains the sequence of integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 10^4), where b_{i} is the amount of the i-th boy's personal money. The third line contains the sequence of integers p_1, p_2, ..., p_{m} (1 \u2264 p_{j} \u2264 10^9), where p_{j} is the price for renting the j-th bike.\n\n\n-----Output-----\n\nPrint two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0.\n\n\n-----Examples-----\nInput\n2 2 10\n5 5\n7 6\n\nOutput\n2 3\n\nInput\n4 5 2\n8 1 1 2\n6 3 7 5 2\n\nOutput\n3 8\n\n\n\n-----Note-----\n\nIn the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money."} +{"id": "01392", "text": "Let's call a number k-good if it contains all digits not exceeding k (0, ..., k). You've got a number k and an array a containing n numbers. Find out how many k-good numbers are in a (count each number every time it occurs in array a).\n\n\n-----Input-----\n\nThe first line contains integers n and k (1 \u2264 n \u2264 100, 0 \u2264 k \u2264 9). The i-th of the following n lines contains integer a_{i} without leading zeroes (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of k-good numbers in a.\n\n\n-----Examples-----\nInput\n10 6\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n\nOutput\n10\n\nInput\n2 1\n1\n10\n\nOutput\n1"} +{"id": "01393", "text": "Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message \u2014 string s of length n, consisting of uppercase and lowercase English letters. Tanya can't write yet, so she found a newspaper and decided to cut out the letters and glue them into the postcard to achieve string s. The newspaper contains string t, consisting of uppercase and lowercase English letters. We know that the length of string t greater or equal to the length of the string s.\n\nThe newspaper may possibly have too few of some letters needed to make the text and too many of some other letters. That's why Tanya wants to cut some n letters out of the newspaper and make a message of length exactly n, so that it looked as much as possible like s. If the letter in some position has correct value and correct letter case (in the string s and in the string that Tanya will make), then she shouts joyfully \"YAY!\", and if the letter in the given position has only the correct value but it is in the wrong case, then the girl says \"WHOOPS\".\n\nTanya wants to make such message that lets her shout \"YAY!\" as much as possible. If there are multiple ways to do this, then her second priority is to maximize the number of times she says \"WHOOPS\". Your task is to help Tanya make the message.\n\n\n-----Input-----\n\nThe first line contains line s (1 \u2264 |s| \u2264 2\u00b710^5), consisting of uppercase and lowercase English letters \u2014 the text of Tanya's message.\n\nThe second line contains line t (|s| \u2264 |t| \u2264 2\u00b710^5), consisting of uppercase and lowercase English letters \u2014 the text written in the newspaper.\n\nHere |a| means the length of the string a.\n\n\n-----Output-----\n\nPrint two integers separated by a space: the first number is the number of times Tanya shouts \"YAY!\" while making the message, the second number is the number of times Tanya says \"WHOOPS\" while making the message. \n\n\n-----Examples-----\nInput\nAbC\nDCbA\n\nOutput\n3 0\n\nInput\nABC\nabc\n\nOutput\n0 3\n\nInput\nabacaba\nAbaCaBA\n\nOutput\n3 4"} +{"id": "01394", "text": "Bob has a string $s$ consisting of lowercase English letters. He defines $s'$ to be the string after removing all \"a\" characters from $s$ (keeping all other characters in the same order). He then generates a new string $t$ by concatenating $s$ and $s'$. In other words, $t=s+s'$ (look at notes for an example).\n\nYou are given a string $t$. Your task is to find some $s$ that Bob could have used to generate $t$. It can be shown that if an answer exists, it will be unique.\n\n\n-----Input-----\n\nThe first line of input contains a string $t$ ($1 \\leq |t| \\leq 10^5$) consisting of lowercase English letters.\n\n\n-----Output-----\n\nPrint a string $s$ that could have generated $t$. It can be shown if an answer exists, it is unique. If no string exists, print \":(\" (without double quotes, there is no space between the characters).\n\n\n-----Examples-----\nInput\naaaaa\n\nOutput\naaaaa\n\nInput\naacaababc\n\nOutput\n:(\n\nInput\nababacacbbcc\n\nOutput\nababacac\n\nInput\nbaba\n\nOutput\n:(\n\n\n\n-----Note-----\n\nIn the first example, we have $s = $ \"aaaaa\", and $s' = $ \"\".\n\nIn the second example, no such $s$ can work that will generate the given $t$.\n\nIn the third example, we have $s = $ \"ababacac\", and $s' = $ \"bbcc\", and $t = s + s' = $ \"ababacacbbcc\"."} +{"id": "01395", "text": "Stepan has a very big positive integer.\n\nLet's consider all cyclic shifts of Stepan's integer (if we look at his integer like at a string) which are also integers (i.e. they do not have leading zeros). Let's call such shifts as good shifts. For example, for the integer 10203 the good shifts are the integer itself 10203 and integers 20310 and 31020.\n\nStepan wants to know the minimum remainder of the division by the given number m among all good shifts. Your task is to determine the minimum remainder of the division by m.\n\n\n-----Input-----\n\nThe first line contains the integer which Stepan has. The length of Stepan's integer is between 2 and 200 000 digits, inclusive. It is guaranteed that Stepan's integer does not contain leading zeros.\n\nThe second line contains the integer m (2 \u2264 m \u2264 10^8) \u2014 the number by which Stepan divides good shifts of his integer.\n\n\n-----Output-----\n\nPrint the minimum remainder which Stepan can get if he divides all good shifts of his integer by the given number m.\n\n\n-----Examples-----\nInput\n521\n3\n\nOutput\n2\n\nInput\n1001\n5\n\nOutput\n0\n\nInput\n5678901234567890123456789\n10000\n\nOutput\n123\n\n\n\n-----Note-----\n\nIn the first example all good shifts of the integer 521 (good shifts are equal to 521, 215 and 152) has same remainder 2 when dividing by 3.\n\nIn the second example there are only two good shifts: the Stepan's integer itself and the shift by one position to the right. The integer itself is 1001 and the remainder after dividing it by 5 equals 1. The shift by one position to the right equals to 1100 and the remainder after dividing it by 5 equals 0, which is the minimum possible remainder."} +{"id": "01396", "text": "Iahub is training for the IOI. What is a better way to train than playing a Zuma-like game? \n\nThere are n balls put in a row. Each ball is colored in one of k colors. Initially the row doesn't contain three or more contiguous balls with the same color. Iahub has a single ball of color x. He can insert his ball at any position in the row (probably, between two other balls). If at any moment there are three or more contiguous balls of the same color in the row, they are destroyed immediately. This rule is applied multiple times, until there are no more sets of 3 or more contiguous balls of the same color. \n\nFor example, if Iahub has the row of balls [black, black, white, white, black, black] and a white ball, he can insert the ball between two white balls. Thus three white balls are destroyed, and then four black balls become contiguous, so all four balls are destroyed. The row will not contain any ball in the end, so Iahub can destroy all 6 balls.\n\nIahub wants to destroy as many balls as possible. You are given the description of the row of balls, and the color of Iahub's ball. Help Iahub train for the IOI by telling him the maximum number of balls from the row he can destroy.\n\n\n-----Input-----\n\nThe first line of input contains three integers: n (1 \u2264 n \u2264 100), k (1 \u2264 k \u2264 100) and x (1 \u2264 x \u2264 k). The next line contains n space-separated integers c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 k). Number c_{i} means that the i-th ball in the row has color c_{i}.\n\nIt is guaranteed that the initial row of balls will never contain three or more contiguous balls of the same color. \n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of balls Iahub can destroy.\n\n\n-----Examples-----\nInput\n6 2 2\n1 1 2 2 1 1\n\nOutput\n6\n\nInput\n1 1 1\n1\n\nOutput\n0"} +{"id": "01397", "text": "A country has n cities. Initially, there is no road in the country. One day, the king decides to construct some roads connecting pairs of cities. Roads can be traversed either way. He wants those roads to be constructed in such a way that it is possible to go from each city to any other city by traversing at most two roads. You are also given m pairs of cities \u2014 roads cannot be constructed between these pairs of cities.\n\nYour task is to construct the minimum number of roads that still satisfy the above conditions. The constraints will guarantee that this is always possible.\n\n\n-----Input-----\n\nThe first line consists of two integers n and m $(1 \\leq n \\leq 10^{3}, 0 \\leq m < \\frac{n}{2})$.\n\nThen m lines follow, each consisting of two integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n, a_{i} \u2260 b_{i}), which means that it is not possible to construct a road connecting cities a_{i} and b_{i}. Consider the cities are numbered from 1 to n.\n\nIt is guaranteed that every pair of cities will appear at most once in the input.\n\n\n-----Output-----\n\nYou should print an integer s: the minimum number of roads that should be constructed, in the first line. Then s lines should follow, each consisting of two integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n, a_{i} \u2260 b_{i}), which means that a road should be constructed between cities a_{i} and b_{i}.\n\nIf there are several solutions, you may print any of them.\n\n\n-----Examples-----\nInput\n4 1\n1 3\n\nOutput\n3\n1 2\n4 2\n2 3\n\n\n\n-----Note-----\n\nThis is one possible solution of the example: [Image] \n\nThese are examples of wrong solutions: [Image] The above solution is wrong because it doesn't use the minimum number of edges (4 vs 3). In addition, it also tries to construct a road between cities 1 and 3, while the input specifies that it is not allowed to construct a road between the pair. [Image] The above solution is wrong because you need to traverse at least 3 roads to go from city 1 to city 3, whereas in your country it must be possible to go from any city to another by traversing at most 2 roads. [Image] Finally, the above solution is wrong because it must be possible to go from any city to another, whereas it is not possible in this country to go from city 1 to 3, 2 to 3, and 4 to 3."} +{"id": "01398", "text": "One day Vasya was on a physics practical, performing the task on measuring the capacitance. He followed the teacher's advice and did as much as n measurements, and recorded the results in the notebook. After that he was about to show the results to the teacher, but he remembered that at the last lesson, the teacher had made his friend Petya redo the experiment because the largest and the smallest results differed by more than two times. Vasya is lazy, and he does not want to redo the experiment. He wants to do the task and go home play computer games. So he decided to cheat: before Vasya shows the measurements to the teacher, he will erase some of them, so as to make the largest and the smallest results of the remaining measurements differ in no more than two times. In other words, if the remaining measurements have the smallest result x, and the largest result y, then the inequality y \u2264 2\u00b7x must fulfill. Of course, to avoid the teacher's suspicion, Vasya wants to remove as few measurement results as possible from his notes.\n\nHelp Vasya, find what minimum number of measurement results he will have to erase from his notes so that the largest and the smallest of the remaining results of the measurements differed in no more than two times.\n\n\n-----Input-----\n\nThe first line contains integer n (2 \u2264 n \u2264 10^5) \u2014 the number of measurements Vasya made. The second line contains n integers c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 5000) \u2014 the results of the measurements. The numbers on the second line are separated by single spaces.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of results Vasya will have to remove.\n\n\n-----Examples-----\nInput\n6\n4 5 3 8 3 7\n\nOutput\n2\n\nInput\n4\n4 3 2 4\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one will be 4."} +{"id": "01399", "text": "You are given $n$ segments on a Cartesian plane. Each segment's endpoints have integer coordinates. Segments can intersect with each other. No two segments lie on the same line.\n\nCount the number of distinct points with integer coordinates, which are covered by at least one segment.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 1000$) \u2014 the number of segments.\n\nEach of the next $n$ lines contains four integers $Ax_i, Ay_i, Bx_i, By_i$ ($-10^6 \\le Ax_i, Ay_i, Bx_i, By_i \\le 10^6$) \u2014 the coordinates of the endpoints $A$, $B$ ($A \\ne B$) of the $i$-th segment.\n\nIt is guaranteed that no two segments lie on the same line.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of distinct points with integer coordinates, which are covered by at least one segment.\n\n\n-----Examples-----\nInput\n9\n0 0 4 4\n-1 5 4 0\n4 0 4 4\n5 2 11 2\n6 1 6 7\n5 6 11 6\n10 1 10 7\n7 0 9 8\n10 -1 11 -1\n\nOutput\n42\n\nInput\n4\n-1 2 1 2\n-1 0 1 0\n-1 0 0 3\n0 3 1 0\n\nOutput\n7\n\n\n\n-----Note-----\n\nThe image for the first example: [Image] \n\nSeveral key points are marked blue, the answer contains some non-marked points as well.\n\nThe image for the second example: [Image]"} +{"id": "01400", "text": "Today Adilbek is taking his probability theory test. Unfortunately, when Adilbek arrived at the university, there had already been a long queue of students wanting to take the same test. Adilbek has estimated that he will be able to start the test only $T$ seconds after coming. \n\nFortunately, Adilbek can spend time without revising any boring theorems or formulas. He has an app on this smartphone which contains $n$ Japanese crosswords to solve. Adilbek has decided to solve them all one by one in the order they are listed in the app, without skipping any crossword. For each crossword, a number $t_i$ is given that represents the time it takes an average crossword expert to solve this crossword (the time is given in seconds).\n\nAdilbek is a true crossword expert, but, unfortunately, he is sometimes unlucky in choosing the way to solve the crossword. So, it takes him either $t_i$ seconds or $t_i + 1$ seconds to solve the $i$-th crossword, equiprobably (with probability $\\frac{1}{2}$ he solves the crossword in exactly $t_i$ seconds, and with probability $\\frac{1}{2}$ he has to spend an additional second to finish the crossword). All these events are independent.\n\nAfter $T$ seconds pass (or after solving the last crossword, if he manages to do it in less than $T$ seconds), Adilbek closes the app (if he finishes some crossword at the same moment, that crossword is considered solved; otherwise Adilbek does not finish solving the current crossword at all). He thinks it would be an interesting probability theory problem to calculate $E$ \u2014 the expected number of crosswords he will be able to solve completely. Can you calculate it? \n\nRecall that the expected value of a discrete random variable is the probability-weighted average of all possible values \u2014 in this problem it means that the expected value of the number of solved crosswords can be calculated as $E = \\sum \\limits_{i = 0}^{n} i p_i$, where $p_i$ is the probability that Adilbek will solve exactly $i$ crosswords. \n\nWe can represent $E$ as rational fraction $\\frac{P}{Q}$ with $Q > 0$. To give the answer, you should print $P \\cdot Q^{-1} \\bmod (10^9 + 7)$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $T$ ($1 \\le n \\le 2 \\cdot 10^5$, $1 \\le T \\le 2 \\cdot 10^{14}$) \u2014 the number of crosswords and the time Adilbek has to spend, respectively.\n\nThe second line contains $n$ integers $t_1, t_2, \\dots, t_n$ ($1 \\le t_i \\le 10^9$), where $t_i$ is the time it takes a crossword expert to solve the $i$-th crossword.\n\nNote that Adilbek solves the crosswords in the order they are given in the input without skipping any of them.\n\n\n-----Output-----\n\nPrint one integer \u2014 the expected value of the number of crosswords Adilbek solves in $T$ seconds, expressed in the form of $P \\cdot Q^{-1} \\bmod (10^9 + 7)$.\n\n\n-----Examples-----\nInput\n3 5\n2 2 2\n\nOutput\n750000007\n\nInput\n3 5\n2 1 2\n\nOutput\n125000003\n\n\n\n-----Note-----\n\nThe answer for the first sample is equal to $\\frac{14}{8}$.\n\nThe answer for the second sample is equal to $\\frac{17}{8}$."} +{"id": "01401", "text": "Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on.\n\nThe girl noticed that some of the tree's vertices are sad, so she decided to play with them. Let's call vertex v sad if there is a vertex u in subtree of vertex v such that dist(v, u) > a_{u}, where a_{u} is the number written on vertex u, dist(v, u) is the sum of the numbers written on the edges on the path from v to u.\n\nLeaves of a tree are vertices connected to a single vertex by a single edge, but the root of a tree is a leaf if and only if the tree consists of a single vertex\u00a0\u2014 root.\n\nThus Alyona decided to remove some of tree leaves until there will be no any sad vertex left in the tree. What is the minimum number of leaves Alyona needs to remove?\n\n\n-----Input-----\n\nIn the first line of the input integer n (1 \u2264 n \u2264 10^5) is given\u00a0\u2014 the number of vertices in the tree.\n\nIn the second line the sequence of n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) is given, where a_{i} is the number written on vertex i.\n\nThe next n - 1 lines describe tree edges: i^{th} of them consists of two integers p_{i} and c_{i} (1 \u2264 p_{i} \u2264 n, - 10^9 \u2264 c_{i} \u2264 10^9), meaning that there is an edge connecting vertices i + 1 and p_{i} with number c_{i} written on it.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the minimum number of leaves Alyona needs to remove such that there will be no any sad vertex left in the tree.\n\n\n-----Example-----\nInput\n9\n88 22 83 14 95 91 98 53 11\n3 24\n7 -8\n1 67\n1 64\n9 65\n5 12\n6 -80\n3 8\n\nOutput\n5\n\n\n\n-----Note-----\n\nThe following image represents possible process of removing leaves from the tree: [Image]"} +{"id": "01402", "text": "Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i and j (1 \u2264 i, j \u2264 n), such that s_{i} > w_{i} and s_{j} < w_{j}. Here sign s_{i} represents the i-th digit of string s, similarly, w_{j} represents the j-th digit of string w.\n\nA string's template is a string that consists of digits and question marks (\"?\").\n\nYaroslav has two string templates, each of them has length n. Yaroslav wants to count the number of ways to replace all question marks by some integers in both templates, so as to make the resulting strings incomparable. Note that the obtained strings can contain leading zeroes and that distinct question marks can be replaced by distinct or the same integers.\n\nHelp Yaroslav, calculate the remainder after dividing the described number of ways by 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the length of both templates. The second line contains the first template \u2014 a string that consists of digits and characters \"?\". The string's length equals n. The third line contains the second template in the same format.\n\n\n-----Output-----\n\nIn a single line print the remainder after dividing the answer to the problem by number 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n2\n90\n09\n\nOutput\n1\n\nInput\n2\n11\n55\n\nOutput\n0\n\nInput\n5\n?????\n?????\n\nOutput\n993531194\n\n\n\n-----Note-----\n\nThe first test contains no question marks and both strings are incomparable, so the answer is 1.\n\nThe second test has no question marks, but the given strings are comparable, so the answer is 0."} +{"id": "01403", "text": "You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.\n\nYou know that you have $n$ bacteria in the Petri dish and size of the $i$-th bacteria is $a_i$. Also you know intergalactic positive integer constant $K$.\n\nThe $i$-th bacteria can swallow the $j$-th bacteria if and only if $a_i > a_j$ and $a_i \\le a_j + K$. The $j$-th bacteria disappear, but the $i$-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria $i$ can swallow any bacteria $j$ if $a_i > a_j$ and $a_i \\le a_j + K$. The swallow operations go one after another.\n\nFor example, the sequence of bacteria sizes $a=[101, 53, 42, 102, 101, 55, 54]$ and $K=1$. The one of possible sequences of swallows is: $[101, 53, 42, 102, \\underline{101}, 55, 54]$ $\\to$ $[101, \\underline{53}, 42, 102, 55, 54]$ $\\to$ $[\\underline{101}, 42, 102, 55, 54]$ $\\to$ $[42, 102, 55, \\underline{54}]$ $\\to$ $[42, 102, 55]$. In total there are $3$ bacteria remained in the Petri dish.\n\nSince you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.\n\n\n-----Input-----\n\nThe first line contains two space separated positive integers $n$ and $K$ ($1 \\le n \\le 2 \\cdot 10^5$, $1 \\le K \\le 10^6$) \u2014 number of bacteria and intergalactic constant $K$.\n\nThe second line contains $n$ space separated integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^6$) \u2014 sizes of bacteria you have.\n\n\n-----Output-----\n\nPrint the only integer \u2014 minimal possible number of bacteria can remain.\n\n\n-----Examples-----\nInput\n7 1\n101 53 42 102 101 55 54\n\nOutput\n3\n\nInput\n6 5\n20 15 10 15 20 25\n\nOutput\n1\n\nInput\n7 1000000\n1 1 1 1 1 1 1\n\nOutput\n7\n\n\n\n-----Note-----\n\nThe first example is clarified in the problem statement.\n\nIn the second example an optimal possible sequence of swallows is: $[20, 15, 10, 15, \\underline{20}, 25]$ $\\to$ $[20, 15, 10, \\underline{15}, 25]$ $\\to$ $[20, 15, \\underline{10}, 25]$ $\\to$ $[20, \\underline{15}, 25]$ $\\to$ $[\\underline{20}, 25]$ $\\to$ $[25]$.\n\nIn the third example no bacteria can swallow any other bacteria."} +{"id": "01404", "text": "Ivan unexpectedly saw a present from one of his previous birthdays. It is array of $n$ numbers from $1$ to $200$. Array is old and some numbers are hard to read. Ivan remembers that for all elements at least one of its neighbours ls not less than it, more formally:\n\n$a_{1} \\le a_{2}$,\n\n$a_{n} \\le a_{n-1}$ and\n\n$a_{i} \\le max(a_{i-1}, \\,\\, a_{i+1})$ for all $i$ from $2$ to $n-1$.\n\nIvan does not remember the array and asks to find the number of ways to restore it. Restored elements also should be integers from $1$ to $200$. Since the number of ways can be big, print it modulo $998244353$.\n\n\n-----Input-----\n\nFirst line of input contains one integer $n$ ($2 \\le n \\le 10^{5}$)\u00a0\u2014 size of the array.\n\nSecond line of input contains $n$ integers $a_{i}$\u00a0\u2014 elements of array. Either $a_{i} = -1$ or $1 \\le a_{i} \\le 200$. $a_{i} = -1$ means that $i$-th element can't be read.\n\n\n-----Output-----\n\nPrint number of ways to restore the array modulo $998244353$.\n\n\n-----Examples-----\nInput\n3\n1 -1 2\n\nOutput\n1\n\nInput\n2\n-1 -1\n\nOutput\n200\n\n\n\n-----Note-----\n\nIn the first example, only possible value of $a_{2}$ is $2$.\n\nIn the second example, $a_{1} = a_{2}$ so there are $200$ different values because all restored elements should be integers between $1$ and $200$."} +{"id": "01405", "text": "Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f_0 and f_1 are arbitrary f_{n} + 2 = f_{n} + 1 + f_{n} for all n \u2265 0. \n\nYou are given some sequence of integers a_1, a_2, ..., a_{n}. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (2 \u2264 n \u2264 1000)\u00a0\u2014 the length of the sequence a_{i}.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (|a_{i}| \u2264 10^9).\n\n\n-----Output-----\n\nPrint the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement.\n\n\n-----Examples-----\nInput\n3\n1 2 -1\n\nOutput\n3\n\nInput\n5\n28 35 7 14 21\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample, if we rearrange elements of the sequence as - 1, 2, 1, the whole sequence a_{i} would be Fibonacci-ish.\n\nIn the second sample, the optimal way to rearrange elements is $7$, $14$, $21$, $35$, 28."} +{"id": "01406", "text": "Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days.\n\nPlease help Pashmak with his weird idea. Assume that each bus has an unlimited capacity.\n\n\n-----Input-----\n\nThe first line of input contains three space-separated integers n, k, d (1 \u2264 n, d \u2264 1000;\u00a01 \u2264 k \u2264 10^9).\n\n\n-----Output-----\n\nIf there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k.\n\n\n-----Examples-----\nInput\n3 2 2\n\nOutput\n1 1 2 \n1 2 1 \n\nInput\n3 2 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nNote that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day."} +{"id": "01407", "text": "You've got an n \u00d7 m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.\n\nYou are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not. \n\nA matrix is prime if at least one of the two following conditions fulfills: the matrix has a row with prime numbers only; the matrix has a column with prime numbers only; \n\nYour task is to count the minimum number of moves needed to get a prime matrix from the one you've got.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 500) \u2014 the number of rows and columns in the matrix, correspondingly.\n\nEach of the following n lines contains m integers \u2014 the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 10^5.\n\nThe numbers in the lines are separated by single spaces.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.\n\n\n-----Examples-----\nInput\n3 3\n1 2 3\n5 6 1\n4 4 1\n\nOutput\n1\n\nInput\n2 3\n4 8 8\n9 2 9\n\nOutput\n3\n\nInput\n2 2\n1 3\n4 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.\n\nIn the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.\n\nIn the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2."} +{"id": "01408", "text": "Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is t_{i} and its pages' width is equal to w_{i}. The thickness of each book is either 1 or 2. All books have the same page heights. $1$ \n\nShaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. [Image] \n\nHelp Shaass to find the minimum total thickness of the vertical books that we can achieve.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n, (1 \u2264 n \u2264 100). Each of the next n lines contains two integers t_{i} and w_{i} denoting the thickness and width of the i-th book correspondingly, (1 \u2264 t_{i} \u2264 2, 1 \u2264 w_{i} \u2264 100).\n\n\n-----Output-----\n\nOn the only line of the output print the minimum total thickness of the vertical books that we can achieve.\n\n\n-----Examples-----\nInput\n5\n1 12\n1 3\n2 15\n2 5\n2 1\n\nOutput\n5\n\nInput\n3\n1 10\n2 1\n2 4\n\nOutput\n3"} +{"id": "01409", "text": "The Saratov State University Olympiad Programmers Training Center (SSU OPTC) has n students. For each student you know the number of times he/she has participated in the ACM ICPC world programming championship. According to the ACM ICPC rules, each person can participate in the world championship at most 5 times.\n\nThe head of the SSU OPTC is recently gathering teams to participate in the world championship. Each team must consist of exactly three people, at that, any person cannot be a member of two or more teams. What maximum number of teams can the head make if he wants each team to participate in the world championship with the same members at least k times?\n\n\n-----Input-----\n\nThe first line contains two integers, n and k (1 \u2264 n \u2264 2000;\u00a01 \u2264 k \u2264 5). The next line contains n integers: y_1, y_2, ..., y_{n} (0 \u2264 y_{i} \u2264 5), where y_{i} shows the number of times the i-th person participated in the ACM ICPC world championship.\n\n\n-----Output-----\n\nPrint a single number \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n5 2\n0 4 5 1 0\n\nOutput\n1\n\nInput\n6 4\n0 1 2 3 4 5\n\nOutput\n0\n\nInput\n6 5\n0 0 0 0 0 0\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample only one team could be made: the first, the fourth and the fifth participants.\n\nIn the second sample no teams could be created.\n\nIn the third sample two teams could be created. Any partition into two teams fits."} +{"id": "01410", "text": "You are given a tree consisting of $n$ vertices. A tree is an undirected connected acyclic graph. [Image] Example of a tree. \n\nYou have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.\n\nYou have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples $(x, y, z)$ such that $x \\neq y, y \\neq z, x \\neq z$, $x$ is connected by an edge with $y$, and $y$ is connected by an edge with $z$. The colours of $x$, $y$ and $z$ should be pairwise distinct. Let's call a painting which meets this condition good.\n\nYou have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ $(3 \\le n \\le 100\\,000)$ \u2014 the number of vertices.\n\nThe second line contains a sequence of integers $c_{1, 1}, c_{1, 2}, \\dots, c_{1, n}$ $(1 \\le c_{1, i} \\le 10^{9})$, where $c_{1, i}$ is the cost of painting the $i$-th vertex into the first color.\n\nThe third line contains a sequence of integers $c_{2, 1}, c_{2, 2}, \\dots, c_{2, n}$ $(1 \\le c_{2, i} \\le 10^{9})$, where $c_{2, i}$ is the cost of painting the $i$-th vertex into the second color.\n\nThe fourth line contains a sequence of integers $c_{3, 1}, c_{3, 2}, \\dots, c_{3, n}$ $(1 \\le c_{3, i} \\le 10^{9})$, where $c_{3, i}$ is the cost of painting the $i$-th vertex into the third color.\n\nThen $(n - 1)$ lines follow, each containing two integers $u_j$ and $v_j$ $(1 \\le u_j, v_j \\le n, u_j \\neq v_j)$ \u2014 the numbers of vertices connected by the $j$-th undirected edge. It is guaranteed that these edges denote a tree.\n\n\n-----Output-----\n\nIf there is no good painting, print $-1$.\n\nOtherwise, print the minimum cost of a good painting in the first line. In the second line print $n$ integers $b_1, b_2, \\dots, b_n$ $(1 \\le b_i \\le 3)$, where the $i$-th integer should denote the color of the $i$-th vertex. If there are multiple good paintings with minimum cost, print any of them.\n\n\n-----Examples-----\nInput\n3\n3 2 3\n4 3 2\n3 1 3\n1 2\n2 3\n\nOutput\n6\n1 3 2 \n\nInput\n5\n3 4 2 1 2\n4 2 1 5 4\n5 3 2 1 1\n1 2\n3 2\n4 3\n5 3\n\nOutput\n-1\n\nInput\n5\n3 4 2 1 2\n4 2 1 5 4\n5 3 2 1 1\n1 2\n3 2\n4 3\n5 4\n\nOutput\n9\n1 3 2 1 3 \n\n\n\n-----Note-----\n\nAll vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color $1$, the second vertex \u2014 into color $3$, and the third vertex \u2014 into color $2$. The cost of this painting is $3 + 2 + 1 = 6$."} +{"id": "01411", "text": "It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices.\n\nIt is known that the i-th contestant will eat s_{i} slices of pizza, and gain a_{i} happiness for each slice of type 1 pizza they eat, and b_{i} happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved?\n\n\n-----Input-----\n\nThe first line of input will contain integers N and S (1 \u2264 N \u2264 10^5, 1 \u2264 S \u2264 10^5), the number of contestants and the number of slices per pizza, respectively. N lines follow.\n\nThe i-th such line contains integers s_{i}, a_{i}, and b_{i} (1 \u2264 s_{i} \u2264 10^5, 1 \u2264 a_{i} \u2264 10^5, 1 \u2264 b_{i} \u2264 10^5), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively.\n\n\n-----Output-----\n\nPrint the maximum total happiness that can be achieved.\n\n\n-----Examples-----\nInput\n3 12\n3 5 7\n4 6 7\n5 9 5\n\nOutput\n84\n\nInput\n6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6\n\nOutput\n314\n\n\n\n-----Note-----\n\nIn the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3\u00b75 + 4\u00b76 + 5\u00b79 = 84, and if you buy a type 2 pizza, the total happiness will be 3\u00b77 + 4\u00b77 + 5\u00b75 = 74."} +{"id": "01412", "text": "All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket. \n\nThe goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has n discount coupons, the i-th of them can be used with products with ids ranging from l_{i} to r_{i}, inclusive. Today Fedor wants to take exactly k coupons with him.\n\nFedor wants to choose the k coupons in such a way that the number of such products x that all coupons can be used with this product x is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor!\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 k \u2264 n \u2264 3\u00b710^5)\u00a0\u2014 the number of coupons Fedor has, and the number of coupons he wants to choose.\n\nEach of the next n lines contains two integers l_{i} and r_{i} ( - 10^9 \u2264 l_{i} \u2264 r_{i} \u2264 10^9)\u00a0\u2014 the description of the i-th coupon. The coupons can be equal.\n\n\n-----Output-----\n\nIn the first line print single integer\u00a0\u2014 the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted.\n\nIn the second line print k distinct integers p_1, p_2, ..., p_{k} (1 \u2264 p_{i} \u2264 n)\u00a0\u2014 the ids of the coupons which Fedor should choose.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n4 2\n1 100\n40 70\n120 130\n125 180\n\nOutput\n31\n1 2 \n\nInput\n3 2\n1 12\n15 20\n25 30\n\nOutput\n0\n1 2 \n\nInput\n5 2\n1 10\n5 15\n14 50\n30 70\n99 100\n\nOutput\n21\n3 4 \n\n\n\n-----Note-----\n\nIn the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total.\n\nIn the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example."} +{"id": "01413", "text": "You are given a string $s=s_1s_2\\dots s_n$ of length $n$, which only contains digits $1$, $2$, ..., $9$.\n\nA substring $s[l \\dots r]$ of $s$ is a string $s_l s_{l + 1} s_{l + 2} \\ldots s_r$. A substring $s[l \\dots r]$ of $s$ is called even if the number represented by it is even. \n\nFind the number of even substrings of $s$. Note, that even if some substrings are equal as strings, but have different $l$ and $r$, they are counted as different substrings.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 65000$)\u00a0\u2014 the length of the string $s$.\n\nThe second line contains a string $s$ of length $n$. The string $s$ consists only of digits $1$, $2$, ..., $9$.\n\n\n-----Output-----\n\nPrint the number of even substrings of $s$.\n\n\n-----Examples-----\nInput\n4\n1234\n\nOutput\n6\nInput\n4\n2244\n\nOutput\n10\n\n\n-----Note-----\n\nIn the first example, the $[l, r]$ pairs corresponding to even substrings are: $s[1 \\dots 2]$\n\n $s[2 \\dots 2]$\n\n $s[1 \\dots 4]$\n\n $s[2 \\dots 4]$\n\n $s[3 \\dots 4]$\n\n $s[4 \\dots 4]$ \n\nIn the second example, all $10$ substrings of $s$ are even substrings. Note, that while substrings $s[1 \\dots 1]$ and $s[2 \\dots 2]$ both define the substring \"2\", they are still counted as different substrings."} +{"id": "01414", "text": "Inna and Dima bought a table of size n \u00d7 m in the shop. Each cell of the table contains a single letter: \"D\", \"I\", \"M\", \"A\".\n\nInna loves Dima, so she wants to go through his name as many times as possible as she moves through the table. For that, Inna acts as follows:\n\n initially, Inna chooses some cell of the table where letter \"D\" is written; then Inna can move to some side-adjacent table cell that contains letter \"I\"; then from this cell she can go to one of the side-adjacent table cells that contains the written letter \"M\"; then she can go to a side-adjacent cell that contains letter \"A\". Then Inna assumes that she has gone through her sweetheart's name; Inna's next move can be going to one of the side-adjacent table cells that contains letter \"D\" and then walk on through name DIMA in the similar manner. Inna never skips a letter. So, from the letter \"D\" she always goes to the letter \"I\", from the letter \"I\" she always goes the to letter \"M\", from the letter \"M\" she always goes to the letter \"A\", and from the letter \"A\" she always goes to the letter \"D\". \n\nDepending on the choice of the initial table cell, Inna can go through name DIMA either an infinite number of times or some positive finite number of times or she can't go through his name once. Help Inna find out what maximum number of times she can go through name DIMA.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 10^3). \n\nThen follow n lines that describe Inna and Dima's table. Each line contains m characters. Each character is one of the following four characters: \"D\", \"I\", \"M\", \"A\". \n\nNote that it is not guaranteed that the table contains at least one letter \"D\".\n\n\n-----Output-----\n\nIf Inna cannot go through name DIMA once, print on a single line \"Poor Dima!\" without the quotes. If there is the infinite number of names DIMA Inna can go through, print \"Poor Inna!\" without the quotes. Otherwise print a single integer \u2014 the maximum number of times Inna can go through name DIMA.\n\n\n-----Examples-----\nInput\n1 2\nDI\n\nOutput\nPoor Dima!\n\nInput\n2 2\nMA\nID\n\nOutput\nPoor Inna!\n\nInput\n5 5\nDIMAD\nDIMAI\nDIMAM\nDDMAA\nAAMID\n\nOutput\n4\n\n\n\n-----Note-----\n\nNotes to the samples:\n\nIn the first test sample, Inna cannot go through name DIMA a single time.\n\nIn the second test sample, Inna can go through the infinite number of words DIMA. For that, she should move in the clockwise direction starting from the lower right corner.\n\nIn the third test sample the best strategy is to start from the cell in the upper left corner of the table. Starting from this cell, Inna can go through name DIMA four times."} +{"id": "01415", "text": "The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x_0, y_0) of a rectangular squared field of size x \u00d7 y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x\u00b7y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same.\n\nAfter placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code.\n\nMoving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it.\n\nThe tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.\n\n\n-----Input-----\n\nThe first line of the input contains four integers x, y, x_0, y_0 (1 \u2264 x, y \u2264 500, 1 \u2264 x_0 \u2264 x, 1 \u2264 y_0 \u2264 y)\u00a0\u2014 the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right.\n\nThe second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.\n\n\n-----Output-----\n\nPrint the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.\n\n\n-----Examples-----\nInput\n3 4 2 2\nUURDRDRL\n\nOutput\n1 1 0 1 1 1 1 0 6\n\nInput\n2 2 2 2\nULD\n\nOutput\n1 1 1 1\n\n\n\n-----Note-----\n\nIn the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: $(2,2) \\rightarrow(1,2) \\rightarrow(1,2) \\rightarrow(1,3) \\rightarrow(2,3) \\rightarrow(2,4) \\rightarrow(3,4) \\rightarrow(3,4) \\rightarrow(3,3)$."} +{"id": "01416", "text": "Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most a_{i} milliliters of water.\n\nIt turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; Pasha pours the same amount of water to each girl; Pasha pours the same amount of water to each boy; if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. \n\nIn the other words, each boy should get two times more water than each girl does.\n\nPasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends.\n\n\n-----Input-----\n\nThe first line of the input contains two integers, n and w (1 \u2264 n \u2264 10^5, 1 \u2264 w \u2264 10^9)\u00a0\u2014 the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters.\n\nThe second line of the input contains the sequence of integers a_{i} (1 \u2264 a_{i} \u2264 10^9, 1 \u2264 i \u2264 2n)\u00a0\u2014\u00a0the capacities of Pasha's tea cups in milliliters.\n\n\n-----Output-----\n\nPrint a single real number \u2014 the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n2 4\n1 1 1 1\n\nOutput\n3\nInput\n3 18\n4 4 4 2 2 2\n\nOutput\n18\nInput\n1 5\n2 3\n\nOutput\n4.5\n\n\n-----Note-----\n\nPasha also has candies that he is going to give to girls but that is another task..."} +{"id": "01417", "text": "Let $n$ be an integer. Consider all permutations on integers $1$ to $n$ in lexicographic order, and concatenate them into one big sequence $P$. For example, if $n = 3$, then $P = [1, 2, 3, 1, 3, 2, 2, 1, 3, 2, 3, 1, 3, 1, 2, 3, 2, 1]$. The length of this sequence is $n \\cdot n!$.\n\nLet $1 \\leq i \\leq j \\leq n \\cdot n!$ be a pair of indices. We call the sequence $(P_i, P_{i+1}, \\dots, P_{j-1}, P_j)$ a subarray of $P$. \n\nYou are given $n$. Find the number of distinct subarrays of $P$. Since this number may be large, output it modulo $998244353$ (a prime number). \n\n\n-----Input-----\n\nThe only line contains one integer $n$\u00a0($1 \\leq n \\leq 10^6$), as described in the problem statement.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the number of distinct subarrays, modulo $998244353$.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n8\n\nInput\n10\n\nOutput\n19210869\n\n\n\n-----Note-----\n\nIn the first example, the sequence $P = [1, 2, 2, 1]$. It has eight distinct subarrays: $[1]$, $[2]$, $[1, 2]$, $[2, 1]$, $[2, 2]$, $[1, 2, 2]$, $[2, 2, 1]$ and $[1, 2, 2, 1]$."} +{"id": "01418", "text": "You're given an integer $n$. For every integer $i$ from $2$ to $n$, assign a positive integer $a_i$ such that the following conditions hold: For any pair of integers $(i,j)$, if $i$ and $j$ are coprime, $a_i \\neq a_j$. The maximal value of all $a_i$ should be minimized (that is, as small as possible). \n\nA pair of integers is called coprime if their greatest common divisor is $1$.\n\n\n-----Input-----\n\nThe only line contains the integer $n$ ($2 \\le n \\le 10^5$).\n\n\n-----Output-----\n\nPrint $n-1$ integers, $a_2$, $a_3$, $\\ldots$, $a_n$ ($1 \\leq a_i \\leq n$). \n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n1 2 1 \nInput\n3\n\nOutput\n2 1\n\n\n-----Note-----\n\nIn the first example, notice that $3$ and $4$ are coprime, so $a_3 \\neq a_4$. Also, notice that $a=[1,2,3]$ satisfies the first condition, but it's not a correct answer because its maximal value is $3$."} +{"id": "01419", "text": "The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:\n\nThere are space-separated non-empty words of lowercase and uppercase Latin letters.\n\nThere are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen. \n\nIt is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word. \n\nWhen the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.\n\nThe ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.\n\nYou should write a program that will find minimal width of the ad.\n\n\n-----Input-----\n\nThe first line contains number k (1 \u2264 k \u2264 10^5).\n\nThe second line contains the text of the ad \u2014 non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 10^6 characters.\n\n\n-----Output-----\n\nOutput minimal width of the ad.\n\n\n-----Examples-----\nInput\n4\ngarage for sa-le\n\nOutput\n7\n\nInput\n4\nEdu-ca-tion-al Ro-unds are so fun\n\nOutput\n10\n\n\n\n-----Note-----\n\nHere all spaces are replaced with dots.\n\nIn the first example one of possible results after all word wraps looks like this:\n\ngarage.\n\nfor.\n\nsa-\n\nle\n\n\n\nThe second example:\n\nEdu-ca-\n\ntion-al.\n\nRo-unds.\n\nare.so.fun"} +{"id": "01420", "text": "Vanya walks late at night along a straight street of length l, lit by n lanterns. Consider the coordinate system with the beginning of the street corresponding to the point 0, and its end corresponding to the point l. Then the i-th lantern is at the point a_{i}. The lantern lights all points of the street that are at the distance of at most d from it, where d is some positive number, common for all lanterns. \n\nVanya wonders: what is the minimum light radius d should the lanterns have to light the whole street?\n\n\n-----Input-----\n\nThe first line contains two integers n, l (1 \u2264 n \u2264 1000, 1 \u2264 l \u2264 10^9)\u00a0\u2014 the number of lanterns and the length of the street respectively. \n\nThe next line contains n integers a_{i} (0 \u2264 a_{i} \u2264 l). Multiple lanterns can be located at the same point. The lanterns may be located at the ends of the street.\n\n\n-----Output-----\n\nPrint the minimum light radius d, needed to light the whole street. The answer will be considered correct if its absolute or relative error doesn't exceed 10^{ - 9}.\n\n\n-----Examples-----\nInput\n7 15\n15 5 3 7 9 14 0\n\nOutput\n2.5000000000\n\nInput\n2 5\n2 5\n\nOutput\n2.0000000000\n\n\n\n-----Note-----\n\nConsider the second sample. At d = 2 the first lantern will light the segment [0, 4] of the street, and the second lantern will light segment [3, 5]. Thus, the whole street will be lit."} +{"id": "01421", "text": "Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. \n\nThey took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer a_{i}\u00a0\u2014 pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.\n\nThe prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.\n\nOur friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.\n\nPrint the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 2\u00b710^5)\u00a0\u2014 the number of gifts.\n\nThe next line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the pleasantness of the gifts.\n\nThe next (n - 1) lines contain two numbers each. The i-th of these lines contains integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i})\u00a0\u2014 the description of the tree's edges. It means that gifts with numbers u_{i} and v_{i} are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: v_{i} hangs on u_{i} or u_{i} hangs on v_{i}. \n\nIt is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.\n\n\n-----Output-----\n\nIf it is possible for Chloe and Vladik to choose prizes without fighting, print single integer\u00a0\u2014 the maximum possible sum of pleasantness they can get together.\n\nOtherwise print Impossible.\n\n\n-----Examples-----\nInput\n8\n0 5 -1 4 3 2 6 5\n1 2\n2 4\n2 5\n1 3\n3 6\n6 7\n6 8\n\nOutput\n25\nInput\n4\n1 -5 1 1\n1 2\n1 4\n2 3\n\nOutput\n2\nInput\n1\n-1\n\nOutput\nImpossible"} +{"id": "01422", "text": "Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.\n\nSimply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 \u2264 i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.\n\nYou are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on \u200b\u200bthe scales or to say that it can't be done.\n\n\n-----Input-----\n\nThe first line contains a string consisting of exactly ten zeroes and ones: the i-th (i \u2265 1) character in the line equals \"1\" if Xenia has i kilo weights, otherwise the character equals \"0\". The second line contains integer m (1 \u2264 m \u2264 1000).\n\n\n-----Output-----\n\nIn the first line print \"YES\", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line \"NO\". If you can put m weights on the scales, then print in the next line m integers \u2014 the weights' weights in the order you put them on the scales.\n\nIf there are multiple solutions, you can print any of them.\n\n\n-----Examples-----\nInput\n0000000101\n3\n\nOutput\nYES\n8 10 8\n\nInput\n1000000000\n2\n\nOutput\nNO"} +{"id": "01423", "text": "Dasha logged into the system and began to solve problems. One of them is as follows:\n\nGiven two sequences a and b of length n each you need to write a sequence c of length n, the i-th element of which is calculated as follows: c_{i} = b_{i} - a_{i}.\n\nAbout sequences a and b we know that their elements are in the range from l to r. More formally, elements satisfy the following conditions: l \u2264 a_{i} \u2264 r and l \u2264 b_{i} \u2264 r. About sequence c we know that all its elements are distinct.\n\n [Image] \n\nDasha wrote a solution to that problem quickly, but checking her work on the standard test was not so easy. Due to an error in the test system only the sequence a and the compressed sequence of the sequence c were known from that test.\n\nLet's give the definition to a compressed sequence. A compressed sequence of sequence c of length n is a sequence p of length n, so that p_{i} equals to the number of integers which are less than or equal to c_{i} in the sequence c. For example, for the sequence c = [250, 200, 300, 100, 50] the compressed sequence will be p = [4, 3, 5, 2, 1]. Pay attention that in c all integers are distinct. Consequently, the compressed sequence contains all integers from 1 to n inclusively.\n\nHelp Dasha to find any sequence b for which the calculated compressed sequence of sequence c is correct.\n\n\n-----Input-----\n\nThe first line contains three integers n, l, r (1 \u2264 n \u2264 10^5, 1 \u2264 l \u2264 r \u2264 10^9) \u2014 the length of the sequence and boundaries of the segment where the elements of sequences a and b are.\n\nThe next line contains n integers a_1, a_2, ..., a_{n} (l \u2264 a_{i} \u2264 r) \u2014 the elements of the sequence a.\n\nThe next line contains n distinct integers p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 n) \u2014 the compressed sequence of the sequence c.\n\n\n-----Output-----\n\nIf there is no the suitable sequence b, then in the only line print \"-1\".\n\nOtherwise, in the only line print n integers \u2014 the elements of any suitable sequence b.\n\n\n-----Examples-----\nInput\n5 1 5\n1 1 1 1 1\n3 1 5 4 2\n\nOutput\n3 1 5 4 2 \nInput\n4 2 9\n3 4 8 9\n3 2 1 4\n\nOutput\n2 2 2 9 \nInput\n6 1 5\n1 1 1 1 1 1\n2 3 5 4 1 6\n\nOutput\n-1\n\n\n\n-----Note-----\n\nSequence b which was found in the second sample is suitable, because calculated sequence c = [2 - 3, 2 - 4, 2 - 8, 9 - 9] = [ - 1, - 2, - 6, 0] (note that c_{i} = b_{i} - a_{i}) has compressed sequence equals to p = [3, 2, 1, 4]."} +{"id": "01424", "text": "After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game \u00abCall of Soldiers 3\u00bb.\n\nThe game has (m + 1) players and n types of soldiers in total. Players \u00abCall of Soldiers 3\u00bb are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer x_{i}. Consider binary representation of x_{i}: if the j-th bit of number x_{i} equal to one, then the army of the i-th player has soldiers of the j-th type. \n\nFedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, k (1 \u2264 k \u2264 n \u2264 20;\u00a01 \u2264 m \u2264 1000).\n\nThe i-th of the next (m + 1) lines contains a single integer x_{i} (1 \u2264 x_{i} \u2264 2^{n} - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of Fedor's potential friends.\n\n\n-----Examples-----\nInput\n7 3 1\n8\n5\n111\n17\n\nOutput\n0\n\nInput\n3 3 3\n1\n2\n3\n4\n\nOutput\n3"} +{"id": "01425", "text": "You are given $n$ numbers $a_1, a_2, \\ldots, a_n$. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?\n\nFor example, for the array $[1, 4, 5, 6, 7, 8]$, the arrangement on the left is valid, while arrangement on the right is not, as $5\\ge 4 + 1$ and $8> 1 + 6$. [Image] \n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($3\\le n \\le 10^5$)\u00a0\u2014 the number of numbers.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\le 10^9$)\u00a0\u2014 the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).\n\n\n-----Output-----\n\nIf there is no solution, output \"NO\" in the first line. \n\nIf there is a solution, output \"YES\" in the first line. In the second line output $n$ numbers\u00a0\u2014 elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.\n\n\n-----Examples-----\nInput\n3\n2 4 3\n\nOutput\nYES\n4 2 3 \nInput\n5\n1 2 3 4 4\n\nOutput\nYES\n4 4 2 1 3\nInput\n3\n13 8 5\n\nOutput\nNO\nInput\n4\n1 10 100 1000\n\nOutput\nNO\n\n\n-----Note-----\n\nOne of the possible arrangements is shown in the first example: \n\n$4< 2 + 3$;\n\n$2 < 4 + 3$;\n\n$3< 4 + 2$.\n\nOne of the possible arrangements is shown in the second example.\n\nNo matter how we arrange $13, 8, 5$ in a circle in the third example, $13$ will have $8$ and $5$ as neighbors, but $13\\ge 8 + 5$. \n\nThere is no solution in the fourth example."} +{"id": "01426", "text": "Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.\nG consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.\nFirst, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.\nDetermine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.\n\n-----Constraints-----\n - 2 \\leq N \\leq 10^5\n - 0 \\leq M \\leq \\min(10^5, N (N-1))\n - 1 \\leq u_i, v_i \\leq N(1 \\leq i \\leq M)\n - u_i \\neq v_i (1 \\leq i \\leq M)\n - If i \\neq j, (u_i, v_i) \\neq (u_j, v_j).\n - 1 \\leq S, T \\leq N\n - S \\neq T\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nu_1 v_1\n:\nu_M v_M\nS T\n\n-----Output-----\nIf Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.\nIf he can, print the minimum number of ken-ken-pa needed to reach vertex T.\n\n-----Sample Input-----\n4 4\n1 2\n2 3\n3 4\n4 1\n1 3\n\n-----Sample Output-----\n2\n\nKen can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \\rightarrow 2 \\rightarrow 3 \\rightarrow 4 in the first ken-ken-pa, then 4 \\rightarrow 1 \\rightarrow 2 \\rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed."} +{"id": "01427", "text": "Given are N positive integers A_1,...,A_N.\nConsider positive integers B_1, ..., B_N that satisfy the following condition.\nCondition: For any i, j such that 1 \\leq i < j \\leq N, A_i B_i = A_j B_j holds.\nFind the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.\nSince the answer can be enormous, print the sum modulo (10^9 +7).\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^4\n - 1 \\leq A_i \\leq 10^6\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 ... A_N\n\n-----Output-----\nPrint the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).\n\n-----Sample Input-----\n3\n2 3 4\n\n-----Sample Output-----\n13\n\nLet B_1=6, B_2=4, and B_3=3, and the condition will be satisfied."} +{"id": "01428", "text": "There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.\nThese squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.\nWe say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \\leq i,j,x,y \\leq N:\n - If (i+j) \\% 3=(x+y) \\% 3, the color of (i,j) and the color of (x,y) are the same.\n - If (i+j) \\% 3 \\neq (x+y) \\% 3, the color of (i,j) and the color of (x,y) are different.\nHere, X \\% Y represents X modulo Y.\nWe will repaint zero or more squares so that the grid will be a good grid.\nFor a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.\nFind the minimum possible sum of the wrongness of all the squares.\n\n-----Constraints-----\n - 1 \\leq N \\leq 500\n - 3 \\leq C \\leq 30\n - 1 \\leq D_{i,j} \\leq 1000 (i \\neq j),D_{i,j}=0 (i=j)\n - 1 \\leq c_{i,j} \\leq C\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN C\nD_{1,1} ... D_{1,C}\n:\nD_{C,1} ... D_{C,C}\nc_{1,1} ... c_{1,N}\n:\nc_{N,1} ... c_{N,N}\n\n-----Output-----\nIf the minimum possible sum of the wrongness of all the squares is x, print x.\n\n-----Sample Input-----\n2 3\n0 1 1\n1 0 1\n1 4 0\n1 2\n3 3\n\n-----Sample Output-----\n3\n\n - Repaint (1,1) to Color 2. The wrongness of (1,1) becomes D_{1,2}=1.\n - Repaint (1,2) to Color 3. The wrongness of (1,2) becomes D_{2,3}=1.\n - Repaint (2,2) to Color 1. The wrongness of (2,2) becomes D_{3,1}=1.\nIn this case, the sum of the wrongness of all the squares is 3.\nNote that D_{i,j} \\neq D_{j,i} is possible."} +{"id": "01429", "text": "We have a string S of length N consisting of A, T, C, and G.\nStrings T_1 and T_2 of the same length are said to be complementary when, for every i (1 \\leq i \\leq l), the i-th character of T_1 and the i-th character of T_2 are complementary. Here, A and T are complementary to each other, and so are C and G.\nFind the number of non-empty contiguous substrings T of S that satisfies the following condition:\n - There exists a string that is a permutation of T and is complementary to T.\nHere, we distinguish strings that originate from different positions in S, even if the contents are the same.\n\n-----Constraints-----\n - 1 \\leq N \\leq 5000\n - S consists of A, T, C, and G.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN S\n\n-----Output-----\nPrint the number of non-empty contiguous substrings T of S that satisfies the condition.\n\n-----Sample Input-----\n4 AGCT\n\n-----Sample Output-----\n2\n\nThe following two substrings satisfy the condition:\n - GC (the 2-nd through 3-rd characters) is complementary to CG, which is a permutation of GC.\n - AGCT (the 1-st through 4-th characters) is complementary to TCGA, which is a permutation of AGCT."} +{"id": "01430", "text": "N people are arranged in a row from left to right.\nYou are given a string S of length N consisting of 0 and 1, and a positive integer K.\nThe i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.\nYou will give the following direction at most K times (possibly zero):\nDirection: Choose integers l and r satisfying 1 \\leq l \\leq r \\leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.\nFind the maximum possible number of consecutive people standing on hands after at most K directions.\n\n-----Constraints-----\n - N is an integer satisfying 1 \\leq N \\leq 10^5.\n - K is an integer satisfying 1 \\leq K \\leq 10^5.\n - The length of the string S is N.\n - Each character of the string S is 0 or 1.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nS\n\n-----Output-----\nPrint the maximum possible number of consecutive people standing on hands after at most K directions.\n\n-----Sample Input-----\n5 1\n00010\n\n-----Sample Output-----\n4\n\nWe can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:\n - Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left."} +{"id": "01431", "text": "There are N empty boxes arranged in a row from left to right.\nThe integer i is written on the i-th box from the left (1 \\leq i \\leq N).\nFor each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.\nWe say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:\n - For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.\nDoes there exist a good set of choices? If the answer is yes, find one good set of choices.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 2 \\times 10^5\n - a_i is 0 or 1.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\nIf a good set of choices does not exist, print -1.\nIf a good set of choices exists, print one such set of choices in the following format:\nM\nb_1 b_2 ... b_M\n\nwhere M denotes the number of boxes that will contain a ball, and b_1,\\ b_2,\\ ...,\\ b_M are the integers written on these boxes, in any order.\n\n-----Sample Input-----\n3\n1 0 0\n\n-----Sample Output-----\n1\n1\n\nConsider putting a ball only in the box with 1 written on it.\n - There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.\n - There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.\n - There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.\nThus, the condition is satisfied, so this set of choices is good."} +{"id": "01432", "text": "There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.\nBetween these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \\leq i \\leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).\nWhen Mountain i (1 \\leq i \\leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).\nOne day, each of the mountains received a non-negative even number of liters of rain.\nAs a result, Dam i (1 \\leq i \\leq N) accumulated a total of A_i liters of water.\nFind the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.\n\n-----Constraints-----\n - All values in input are integers.\n - 3 \\leq N \\leq 10^5-1\n - N is an odd number.\n - 0 \\leq A_i \\leq 10^9\n - The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.\n\n-----Sample Input-----\n3\n2 2 4\n\n-----Sample Output-----\n4 0 4\n\nIf we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:\n - Dam 1 should have accumulated \\frac{4}{2} + \\frac{0}{2} = 2 liters of water.\n - Dam 2 should have accumulated \\frac{0}{2} + \\frac{4}{2} = 2 liters of water.\n - Dam 3 should have accumulated \\frac{4}{2} + \\frac{4}{2} = 4 liters of water."} +{"id": "01433", "text": "Theater stage is a rectangular field of size n \u00d7 m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.\n\nYou are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above)\u00a0\u2014 left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.\n\nA position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. \n\nCount the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.\n\n\n-----Input-----\n\nThe first line contains two positive integers n and m (1 \u2264 n, m \u2264 1000)\u00a0\u2014 the number of rows and the number of columns in the plan.\n\nThe next n lines contain m integers, 0 or 1 each\u00a0\u2014 the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of good positions for placing the spotlight.\n\n\n-----Examples-----\nInput\n2 4\n0 1 0 0\n1 0 1 0\n\nOutput\n9\n\nInput\n4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0\n\nOutput\n20\n\n\n\n-----Note-----\n\nIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. \n\nTherefore, there are 9 good positions in this example."} +{"id": "01434", "text": "Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degree_{v} and s_{v}, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). \n\nNext day Misha couldn't remember what graph he initially had. Misha has values degree_{v} and s_{v} left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 2^16), the number of vertices in the graph.\n\nThe i-th of the next lines contains numbers degree_{i} and s_{i} (0 \u2264 degree_{i} \u2264 n - 1, 0 \u2264 s_{i} < 2^16), separated by a space.\n\n\n-----Output-----\n\nIn the first line print number m, the number of edges of the graph.\n\nNext print m lines, each containing two distinct numbers, a and b (0 \u2264 a \u2264 n - 1, 0 \u2264 b \u2264 n - 1), corresponding to edge (a, b).\n\nEdges can be printed in any order; vertices of the edge can also be printed in any order.\n\n\n-----Examples-----\nInput\n3\n2 3\n1 0\n1 0\n\nOutput\n2\n1 0\n2 0\n\nInput\n2\n1 1\n1 0\n\nOutput\n1\n0 1\n\n\n\n-----Note-----\n\nThe XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as \"^\", and in Pascal \u2014 as \"xor\"."} +{"id": "01435", "text": "Inna loves digit 9 very much. That's why she asked Dima to write a small number consisting of nines. But Dima must have misunderstood her and he wrote a very large number a, consisting of digits from 1 to 9.\n\nInna wants to slightly alter the number Dima wrote so that in the end the number contained as many digits nine as possible. In one move, Inna can choose two adjacent digits in a number which sum equals 9 and replace them by a single digit 9.\n\nFor instance, Inna can alter number 14545181 like this: 14545181 \u2192 1945181 \u2192 194519 \u2192 19919. Also, she can use this method to transform number 14545181 into number 19991. Inna will not transform it into 149591 as she can get numbers 19919 and 19991 which contain more digits nine.\n\nDima is a programmer so he wants to find out how many distinct numbers containing as many digits nine as possible Inna can get from the written number. Help him with this challenging task.\n\n\n-----Input-----\n\nThe first line of the input contains integer a (1 \u2264 a \u2264 10^100000). Number a doesn't have any zeroes.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the answer to the problem. It is guaranteed that the answer to the problem doesn't exceed 2^63 - 1.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n369727\n\nOutput\n2\n\nInput\n123456789987654321\n\nOutput\n1\n\nInput\n1\n\nOutput\n1\n\n\n\n-----Note-----\n\nNotes to the samples\n\nIn the first sample Inna can get the following numbers: 369727 \u2192 99727 \u2192 9997, 369727 \u2192 99727 \u2192 9979.\n\nIn the second sample, Inna can act like this: 123456789987654321 \u2192 12396789987654321 \u2192 1239678998769321."} +{"id": "01436", "text": "The police department of your city has just started its journey. Initially, they don\u2019t have any manpower. So, they started hiring new recruits in groups.\n\nMeanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.\n\nIf there is no police officer free (isn't busy with crime) during the occurrence of a crime, it will go untreated.\n\nGiven the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated.\n\n\n-----Input-----\n\nThe first line of input will contain an integer n\u00a0(1 \u2264 n \u2264 10^5), the number of events. The next line will contain n space-separated integers.\n\nIf the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers will be recruited at a time.\n\n\n-----Output-----\n\nPrint a single integer, the number of crimes which will go untreated.\n\n\n-----Examples-----\nInput\n3\n-1 -1 1\n\nOutput\n2\n\nInput\n8\n1 -1 1 -1 -1 1 1 1\n\nOutput\n1\n\nInput\n11\n-1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1\n\nOutput\n8\n\n\n\n-----Note-----\n\nLets consider the second example: Firstly one person is hired. Then crime appears, the last hired person will investigate this crime. One more person is hired. One more crime appears, the last hired person will investigate this crime. Crime appears. There is no free policeman at the time, so this crime will go untreated. One more person is hired. One more person is hired. One more person is hired. \n\nThe answer is one, as one crime (on step 5) will go untreated."} +{"id": "01437", "text": "While walking down the street Vanya saw a label \"Hide&Seek\". Because he is a programmer, he used & as a bitwise AND for these two words represented as a integers in base 64 and got new word. Now Vanya thinks of some string s and wants to know the number of pairs of words of length |s| (length of s), such that their bitwise AND is equal to s. As this number can be large, output it modulo 10^9 + 7.\n\nTo represent the string as a number in numeral system with base 64 Vanya uses the following rules: digits from '0' to '9' correspond to integers from 0 to 9; letters from 'A' to 'Z' correspond to integers from 10 to 35; letters from 'a' to 'z' correspond to integers from 36 to 61; letter '-' correspond to integer 62; letter '_' correspond to integer 63. \n\n\n-----Input-----\n\nThe only line of the input contains a single word s (1 \u2264 |s| \u2264 100 000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of possible pairs of words, such that their bitwise AND is equal to string s modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\nz\n\nOutput\n3\n\nInput\nV_V\n\nOutput\n9\n\nInput\nCodeforces\n\nOutput\n130653412\n\n\n\n-----Note-----\n\nFor a detailed definition of bitwise AND we recommend to take a look in the corresponding article in Wikipedia.\n\nIn the first sample, there are 3 possible solutions: z&_ = 61&63 = 61 = z _&z = 63&61 = 61 = z z&z = 61&61 = 61 = z"} +{"id": "01438", "text": "This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.\n\nWaking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value a_{i}\u00a0\u2014 how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.\n\nApollinaria has b_{i} gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.\n\nYour task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.\n\n\n-----Input-----\n\nThe first line of the input contains two positive integers n and k (1 \u2264 n, k \u2264 1000)\u00a0\u2014 the number of ingredients and the number of grams of the magic powder.\n\nThe second line contains the sequence a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.\n\nThe third line contains the sequence b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.\n\n\n-----Output-----\n\nPrint the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.\n\n\n-----Examples-----\nInput\n3 1\n2 1 4\n11 3 16\n\nOutput\n4\n\nInput\n4 3\n4 3 5 6\n11 12 14 20\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.\n\nIn the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer."} +{"id": "01439", "text": "You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m.\n\nCheck if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m.\n\n\n-----Input-----\n\nThe first line contains two numbers, n and m (1 \u2264 n \u2264 10^6, 2 \u2264 m \u2264 10^3) \u2014 the size of the original sequence and the number such that sum should be divisible by it.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nIn the single line print either \"YES\" (without the quotes) if there exists the sought subsequence, or \"NO\" (without the quotes), if such subsequence doesn't exist.\n\n\n-----Examples-----\nInput\n3 5\n1 2 3\n\nOutput\nYES\n\nInput\n1 6\n5\n\nOutput\nNO\n\nInput\n4 6\n3 1 1 3\n\nOutput\nYES\n\nInput\n6 6\n5 5 5 5 5 5\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.\n\nIn the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.\n\nIn the third sample test you need to choose two numbers 3 on the ends.\n\nIn the fourth sample test you can take the whole subsequence."} +{"id": "01440", "text": "Pavel has several sticks with lengths equal to powers of two.\n\nHe has $a_0$ sticks of length $2^0 = 1$, $a_1$ sticks of length $2^1 = 2$, ..., $a_{n-1}$ sticks of length $2^{n-1}$. \n\nPavel wants to make the maximum possible number of triangles using these sticks. The triangles should have strictly positive area, each stick can be used in at most one triangle.\n\nIt is forbidden to break sticks, and each triangle should consist of exactly three sticks.\n\nFind the maximum possible number of triangles.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 300\\,000$)\u00a0\u2014 the number of different lengths of sticks.\n\nThe second line contains $n$ integers $a_0$, $a_1$, ..., $a_{n-1}$ ($1 \\leq a_i \\leq 10^9$), where $a_i$ is the number of sticks with the length equal to $2^i$.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum possible number of non-degenerate triangles that Pavel can make.\n\n\n-----Examples-----\nInput\n5\n1 2 2 2 2\n\nOutput\n3\n\nInput\n3\n1 1 1\n\nOutput\n0\n\nInput\n3\n3 3 3\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example, Pavel can, for example, make this set of triangles (the lengths of the sides of the triangles are listed): $(2^0, 2^4, 2^4)$, $(2^1, 2^3, 2^3)$, $(2^1, 2^2, 2^2)$.\n\nIn the second example, Pavel cannot make a single triangle.\n\nIn the third example, Pavel can, for example, create this set of triangles (the lengths of the sides of the triangles are listed): $(2^0, 2^0, 2^0)$, $(2^1, 2^1, 2^1)$, $(2^2, 2^2, 2^2)$."} +{"id": "01441", "text": "Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very creative teacher. One of his favorite warm-up exercises is throwing balls. Students line up. Each one gets a single ball in the beginning. The balls are numbered from 1 to n (by the demand of the inventory commission). [Image] Figure 1. The initial position for n = 5. \n\nAfter receiving the balls the students perform the warm-up exercise. The exercise takes place in a few throws. For each throw the teacher chooses any two arbitrary different students who will participate in it. The selected students throw their balls to each other. Thus, after each throw the students remain in their positions, and the two balls are swapped. [Image] Figure 2. The example of a throw. \n\nIn this case there was a throw between the students, who were holding the 2-nd and the 4-th balls. Since the warm-up has many exercises, each of them can only continue for little time. Therefore, for each student we know the maximum number of throws he can participate in. For this lessons maximum number of throws will be 1 or 2.\n\nNote that after all phases of the considered exercise any ball can end up with any student. Smart Beaver decided to formalize it and introduced the concept of the \"ball order\". The ball order is a sequence of n numbers that correspond to the order of balls in the line. The first number will match the number of the ball of the first from the left student in the line, the second number will match the ball of the second student, and so on. For example, in figure 2 the order of the balls was (1, 2, 3, 4, 5), and after the throw it was (1, 4, 3, 2, 5). Smart beaver knows the number of students and for each student he knows the maximum number of throws in which he can participate. And now he is wondering: what is the number of distinct ways of ball orders by the end of the exercise.\n\n\n-----Input-----\n\nThe first line contains a single number n \u2014 the number of students in the line and the number of balls. The next line contains exactly n space-separated integers. Each number corresponds to a student in the line (the i-th number corresponds to the i-th from the left student in the line) and shows the number of throws he can participate in.\n\nThe input limits for scoring 30 points are (subproblem D1): 1 \u2264 n \u2264 10. \n\nThe input limits for scoring 70 points are (subproblems D1+D2): 1 \u2264 n \u2264 500. \n\nThe input limits for scoring 100 points are (subproblems D1+D2+D3): 1 \u2264 n \u2264 1000000. \n\n\n-----Output-----\n\nThe output should contain a single integer \u2014 the number of variants of ball orders after the warm up exercise is complete. As the number can be rather large, print it modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n5\n1 2 2 1 2\n\nOutput\n120\n\nInput\n8\n1 2 2 1 2 1 1 2\n\nOutput\n16800"} +{"id": "01442", "text": "In this task you need to process a set of stock exchange orders and use them to create order book.\n\nAn order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price p_{i}, direction d_{i} \u2014 buy or sell, and integer q_{i}. This means that the participant is ready to buy or sell q_{i} stocks at price p_{i} for one stock. A value q_{i} is also known as a volume of an order.\n\nAll orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders.\n\nAn order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order.\n\nAn order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book.\n\nYou are given n stock exhange orders. Your task is to print order book of depth s for these orders.\n\n\n-----Input-----\n\nThe input starts with two positive integers n and s (1 \u2264 n \u2264 1000, 1 \u2264 s \u2264 50), the number of orders and the book depth.\n\nNext n lines contains a letter d_{i} (either 'B' or 'S'), an integer p_{i} (0 \u2264 p_{i} \u2264 10^5) and an integer q_{i} (1 \u2264 q_{i} \u2264 10^4) \u2014 direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order.\n\n\n-----Output-----\n\nPrint no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input.\n\n\n-----Examples-----\nInput\n6 2\nB 10 3\nS 50 2\nS 40 1\nS 50 6\nB 20 4\nB 25 10\n\nOutput\nS 50 8\nS 40 1\nB 25 10\nB 20 4\n\n\n\n-----Note-----\n\nDenote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample.\n\nYou need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders."} +{"id": "01443", "text": "Bob is a duck. He wants to get to Alice's nest, so that those two can duck! [Image] Duck is the ultimate animal! (Image courtesy of See Bang) \n\nThe journey can be represented as a straight line, consisting of $n$ segments. Bob is located to the left of the first segment, while Alice's nest is on the right of the last segment. Each segment has a length in meters, and also terrain type: grass, water or lava. \n\nBob has three movement types: swimming, walking and flying. He can switch between them or change his direction at any point in time (even when he is located at a non-integer coordinate), and doing so doesn't require any extra time. Bob can swim only on the water, walk only on the grass and fly over any terrain. Flying one meter takes $1$ second, swimming one meter takes $3$ seconds, and finally walking one meter takes $5$ seconds.\n\nBob has a finite amount of energy, called stamina. Swimming and walking is relaxing for him, so he gains $1$ stamina for every meter he walks or swims. On the other hand, flying is quite tiring, and he spends $1$ stamina for every meter flown. Staying in place does not influence his stamina at all. Of course, his stamina can never become negative. Initially, his stamina is zero.\n\nWhat is the shortest possible time in which he can reach Alice's nest? \n\n\n-----Input-----\n\nThe first line contains a single integer $n$\u00a0($1 \\leq n \\leq 10^5$)\u00a0\u2014 the number of segments of terrain. \n\nThe second line contains $n$ integers $l_1, l_2, \\dots, l_n$\u00a0($1 \\leq l_i \\leq 10^{12}$). The $l_i$ represents the length of the $i$-th terrain segment in meters.\n\nThe third line contains a string $s$ consisting of $n$ characters \"G\", \"W\", \"L\", representing Grass, Water and Lava, respectively. \n\nIt is guaranteed that the first segment is not Lava.\n\n\n-----Output-----\n\nOutput a single integer $t$\u00a0\u2014 the minimum time Bob needs to reach Alice. \n\n\n-----Examples-----\nInput\n1\n10\nG\n\nOutput\n30\n\nInput\n2\n10 10\nWL\n\nOutput\n40\n\nInput\n2\n1 2\nWL\n\nOutput\n8\n\nInput\n3\n10 10 10\nGLW\n\nOutput\n80\n\n\n\n-----Note-----\n\nIn the first sample, Bob first walks $5$ meters in $25$ seconds. Then he flies the remaining $5$ meters in $5$ seconds.\n\nIn the second sample, Bob first swims $10$ meters in $30$ seconds. Then he flies over the patch of lava for $10$ seconds.\n\nIn the third sample, the water pond is much smaller. Bob first swims over the water pond, taking him $3$ seconds. However, he cannot fly over the lava just yet, as he only has one stamina while he needs two. So he swims back for half a meter, and then half a meter forward, taking him $3$ seconds in total. Now he has $2$ stamina, so he can spend $2$ seconds flying over the lava.\n\nIn the fourth sample, he walks for $50$ seconds, flies for $10$ seconds, swims for $15$ seconds, and finally flies for $5$ seconds."} +{"id": "01444", "text": "Berland is going through tough times \u2014 the dirt price has dropped and that is a blow to the country's economy. Everybody knows that Berland is the top world dirt exporter!\n\nThe President of Berland was forced to leave only k of the currently existing n subway stations.\n\nThe subway stations are located on a straight line one after another, the trains consecutively visit the stations as they move. You can assume that the stations are on the Ox axis, the i-th station is at point with coordinate x_{i}. In such case the distance between stations i and j is calculated by a simple formula |x_{i} - x_{j}|.\n\nCurrently, the Ministry of Transport is choosing which stations to close and which ones to leave. Obviously, the residents of the capital won't be too enthusiastic about the innovation, so it was decided to show the best side to the people. The Ministry of Transport wants to choose such k stations that minimize the average commute time in the subway!\n\nAssuming that the train speed is constant (it is a fixed value), the average commute time in the subway is calculated as the sum of pairwise distances between stations, divided by the number of pairs (that is $\\frac{n \\cdot(n - 1)}{2}$) and divided by the speed of the train.\n\nHelp the Minister of Transport to solve this difficult problem. Write a program that, given the location of the stations selects such k stations that the average commute time in the subway is minimized.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (3 \u2264 n \u2264 3\u00b710^5) \u2014 the number of the stations before the innovation. The second line contains the coordinates of the stations x_1, x_2, ..., x_{n} ( - 10^8 \u2264 x_{i} \u2264 10^8). The third line contains integer k (2 \u2264 k \u2264 n - 1) \u2014 the number of stations after the innovation.\n\nThe station coordinates are distinct and not necessarily sorted.\n\n\n-----Output-----\n\nPrint a sequence of k distinct integers t_1, t_2, ..., t_{k} (1 \u2264 t_{j} \u2264 n) \u2014 the numbers of the stations that should be left after the innovation in arbitrary order. Assume that the stations are numbered 1 through n in the order they are given in the input. The number of stations you print must have the minimum possible average commute time among all possible ways to choose k stations. If there are multiple such ways, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n3\n1 100 101\n2\n\nOutput\n2 3 \n\n\n-----Note-----\n\nIn the sample testcase the optimal answer is to destroy the first station (with x = 1). The average commute time will be equal to 1 in this way."} +{"id": "01445", "text": "Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number a_{i}, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.\n\nIn this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i \u2264 n - i + 1.\n\nAfter performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday\u00a0\u2014 restore the initial order of the cubes using information of their current location.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 2\u00b710^5)\u00a0\u2014 the number of cubes.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9), where a_{i} is the number written on the i-th cube after Dima has changed their order.\n\n\n-----Output-----\n\nPrint n integers, separated by spaces\u00a0\u2014 the numbers written on the cubes in their initial order.\n\nIt can be shown that the answer is unique.\n\n\n-----Examples-----\nInput\n7\n4 3 7 6 9 1 2\n\nOutput\n2 3 9 6 7 1 4\nInput\n8\n6 1 4 2 5 6 9 2\n\nOutput\n2 1 6 2 5 4 9 6\n\n\n-----Note-----\n\nConsider the first sample. At the begining row was [2, 3, 9, 6, 7, 1, 4]. After first operation row was [4, 1, 7, 6, 9, 3, 2]. After second operation row was [4, 3, 9, 6, 7, 1, 2]. After third operation row was [4, 3, 7, 6, 9, 1, 2]. At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4]."} +{"id": "01446", "text": "Dima took up the biology of bacteria, as a result of his experiments, he invented k types of bacteria. Overall, there are n bacteria at his laboratory right now, and the number of bacteria of type i equals c_{i}. For convenience, we will assume that all the bacteria are numbered from 1 to n. The bacteria of type c_{i} are numbered from $(\\sum_{k = 1}^{i - 1} c_{k}) + 1$ to $\\sum_{k = 1}^{i} c_{k}$.\n\nWith the help of special equipment Dima can move energy from some bacteria into some other one. Of course, the use of such equipment is not free. Dima knows m ways to move energy from some bacteria to another one. The way with number i can be described with integers u_{i}, v_{i} and x_{i} mean that this way allows moving energy from bacteria with number u_{i} to bacteria with number v_{i} or vice versa for x_{i} dollars.\n\nDima's Chef (Inna) calls the type-distribution correct if there is a way (may be non-direct) to move energy from any bacteria of the particular type to any other bacteria of the same type (between any two bacteria of the same type) for zero cost.\n\nAs for correct type-distribution the cost of moving the energy depends only on the types of bacteria help Inna to determine is the type-distribution correct? If it is, print the matrix d with size k \u00d7 k. Cell d[i][j] of this matrix must be equal to the minimal possible cost of energy-moving from bacteria with type i to bacteria with type j.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, k (1 \u2264 n \u2264 10^5;\u00a00 \u2264 m \u2264 10^5;\u00a01 \u2264 k \u2264 500). The next line contains k integers c_1, c_2, ..., c_{k} (1 \u2264 c_{i} \u2264 n). Each of the next m lines contains three integers u_{i}, v_{i}, x_{i} (1 \u2264 u_{i}, v_{i} \u2264 10^5;\u00a00 \u2264 x_{i} \u2264 10^4). It is guaranteed that $\\sum_{i = 1}^{k} c_{i} = n$.\n\n\n-----Output-----\n\nIf Dima's type-distribution is correct, print string \u00abYes\u00bb, and then k lines: in the i-th line print integers d[i][1], d[i][2], ..., d[i][k] (d[i][i] = 0). If there is no way to move energy from bacteria i to bacteria j appropriate d[i][j] must equal to -1. If the type-distribution isn't correct print \u00abNo\u00bb.\n\n\n-----Examples-----\nInput\n4 4 2\n1 3\n2 3 0\n3 4 0\n2 4 1\n2 1 2\n\nOutput\nYes\n0 2\n2 0\n\nInput\n3 1 2\n2 1\n1 2 0\n\nOutput\nYes\n0 -1\n-1 0\n\nInput\n3 2 2\n2 1\n1 2 0\n2 3 1\n\nOutput\nYes\n0 1\n1 0\n\nInput\n3 0 2\n1 2\n\nOutput\nNo"} +{"id": "01447", "text": "Alex enjoys performing magic tricks. He has a trick that requires a deck of n cards. He has m identical decks of n different cards each, which have been mixed together. When Alex wishes to perform the trick, he grabs n cards at random and performs the trick with those. The resulting deck looks like a normal deck, but may have duplicates of some cards.\n\nThe trick itself is performed as follows: first Alex allows you to choose a random card from the deck. You memorize the card and put it back in the deck. Then Alex shuffles the deck, and pulls out a card. If the card matches the one you memorized, the trick is successful.\n\nYou don't think Alex is a very good magician, and that he just pulls a card randomly from the deck. Determine the probability of the trick being successful if this is the case.\n\n\n-----Input-----\n\nFirst line of the input consists of two integers n and m (1 \u2264 n, m \u2264 1000), separated by space \u2014 number of cards in each deck, and number of decks.\n\n\n-----Output-----\n\nOn the only line of the output print one floating point number \u2013 probability of Alex successfully performing the trick. Relative or absolute error of your answer should not be higher than 10^{ - 6}.\n\n\n-----Examples-----\nInput\n2 2\n\nOutput\n0.6666666666666666\n\nInput\n4 4\n\nOutput\n0.4000000000000000\n\nInput\n1 2\n\nOutput\n1.0000000000000000\n\n\n\n-----Note-----\n\nIn the first sample, with probability $\\frac{1}{3}$ Alex will perform the trick with two cards with the same value from two different decks. In this case the trick is guaranteed to succeed.\n\nWith the remaining $\\frac{2}{3}$ probability he took two different cards, and the probability of pulling off the trick is $\\frac{1}{2}$.\n\nThe resulting probability is $\\frac{1}{3} \\times 1 + \\frac{2}{3} \\times \\frac{1}{2} = \\frac{2}{3}$"} +{"id": "01448", "text": "Vasya owns a cornfield which can be defined with two integers $n$ and $d$. The cornfield can be represented as rectangle with vertices having Cartesian coordinates $(0, d), (d, 0), (n, n - d)$ and $(n - d, n)$.\n\n [Image] An example of a cornfield with $n = 7$ and $d = 2$. \n\nVasya also knows that there are $m$ grasshoppers near the field (maybe even inside it). The $i$-th grasshopper is at the point $(x_i, y_i)$. Vasya does not like when grasshoppers eat his corn, so for each grasshopper he wants to know whether its position is inside the cornfield (including the border) or outside.\n\nHelp Vasya! For each grasshopper determine if it is inside the field (including the border).\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $d$ ($1 \\le d < n \\le 100$).\n\nThe second line contains a single integer $m$ ($1 \\le m \\le 100$) \u2014 the number of grasshoppers.\n\nThe $i$-th of the next $m$ lines contains two integers $x_i$ and $y_i$ ($0 \\le x_i, y_i \\le n$) \u2014 position of the $i$-th grasshopper.\n\n\n-----Output-----\n\nPrint $m$ lines. The $i$-th line should contain \"YES\" if the position of the $i$-th grasshopper lies inside or on the border of the cornfield. Otherwise the $i$-th line should contain \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n7 2\n4\n2 4\n4 1\n6 3\n4 5\n\nOutput\nYES\nNO\nNO\nYES\n\nInput\n8 7\n4\n4 4\n2 8\n8 1\n6 1\n\nOutput\nYES\nNO\nYES\nYES\n\n\n\n-----Note-----\n\nThe cornfield from the first example is pictured above. Grasshoppers with indices $1$ (coordinates $(2, 4)$) and $4$ (coordinates $(4, 5)$) are inside the cornfield.\n\nThe cornfield from the second example is pictured below. Grasshoppers with indices $1$ (coordinates $(4, 4)$), $3$ (coordinates $(8, 1)$) and $4$ (coordinates $(6, 1)$) are inside the cornfield. [Image]"} +{"id": "01449", "text": "You are given a non-decreasing array of non-negative integers $a_1, a_2, \\ldots, a_n$. Also you are given a positive integer $k$.\n\nYou want to find $m$ non-decreasing arrays of non-negative integers $b_1, b_2, \\ldots, b_m$, such that: The size of $b_i$ is equal to $n$ for all $1 \\leq i \\leq m$. For all $1 \\leq j \\leq n$, $a_j = b_{1, j} + b_{2, j} + \\ldots + b_{m, j}$. In the other word, array $a$ is the sum of arrays $b_i$. The number of different elements in the array $b_i$ is at most $k$ for all $1 \\leq i \\leq m$. \n\nFind the minimum possible value of $m$, or report that there is no possible $m$.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\leq t \\leq 100$): the number of test cases.\n\nThe first line of each test case contains two integers $n$, $k$ ($1 \\leq n \\leq 100$, $1 \\leq k \\leq n$).\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\leq a_1 \\leq a_2 \\leq \\ldots \\leq a_n \\leq 100$, $a_n > 0$).\n\n\n-----Output-----\n\nFor each test case print a single integer: the minimum possible value of $m$. If there is no such $m$, print $-1$.\n\n\n-----Example-----\nInput\n6\n4 1\n0 0 0 1\n3 1\n3 3 3\n11 3\n0 1 2 2 3 3 3 4 4 4 4\n5 3\n1 2 3 4 5\n9 4\n2 2 3 5 7 11 13 13 17\n10 7\n0 1 1 2 3 3 4 5 5 6\n\nOutput\n-1\n1\n2\n2\n2\n1\n\n\n\n-----Note-----\n\nIn the first test case, there is no possible $m$, because all elements of all arrays should be equal to $0$. But in this case, it is impossible to get $a_4 = 1$ as the sum of zeros.\n\nIn the second test case, we can take $b_1 = [3, 3, 3]$. $1$ is the smallest possible value of $m$.\n\nIn the third test case, we can take $b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2]$ and $b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]$. It's easy to see, that $a_i = b_{1, i} + b_{2, i}$ for all $i$ and the number of different elements in $b_1$ and in $b_2$ is equal to $3$ (so it is at most $3$). It can be proven that $2$ is the smallest possible value of $m$."} +{"id": "01450", "text": "You are given a ternary string (it is a string which consists only of characters '0', '1' and '2').\n\nYou can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace \"01\" with \"10\" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace \"12\" with \"21\" or vice versa).\n\nFor example, for string \"010210\" we can perform the following moves: \"010210\" $\\rightarrow$ \"100210\"; \"010210\" $\\rightarrow$ \"001210\"; \"010210\" $\\rightarrow$ \"010120\"; \"010210\" $\\rightarrow$ \"010201\". \n\nNote than you cannot swap \"02\" $\\rightarrow$ \"20\" and vice versa. You cannot perform any other operations with the given string excluding described above.\n\nYou task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).\n\nString $a$ is lexicographically less than string $b$ (if strings $a$ and $b$ have the same length) if there exists some position $i$ ($1 \\le i \\le |a|$, where $|s|$ is the length of the string $s$) such that for every $j < i$ holds $a_j = b_j$, and $a_i < b_i$.\n\n\n-----Input-----\n\nThe first line of the input contains the string $s$ consisting only of characters '0', '1' and '2', its length is between $1$ and $10^5$ (inclusive).\n\n\n-----Output-----\n\nPrint a single string \u2014 the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).\n\n\n-----Examples-----\nInput\n100210\n\nOutput\n001120\n\nInput\n11222121\n\nOutput\n11112222\n\nInput\n20\n\nOutput\n20"} +{"id": "01451", "text": "Roma (a popular Russian name that means 'Roman') loves the Little Lvov Elephant's lucky numbers.\n\nLet us remind you that lucky numbers are positive integers whose decimal representation only contains lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.\n\nRoma's got n positive integers. He wonders, how many of those integers have not more than k lucky digits? Help him, write the program that solves the problem.\n\n\n-----Input-----\n\nThe first line contains two integers n, k (1 \u2264 n, k \u2264 100). The second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^9) \u2014 the numbers that Roma has. \n\nThe numbers in the lines are separated by single spaces.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n3 4\n1 2 4\n\nOutput\n3\n\nInput\n3 2\n447 44 77\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample all numbers contain at most four lucky digits, so the answer is 3.\n\nIn the second sample number 447 doesn't fit in, as it contains more than two lucky digits. All other numbers are fine, so the answer is 2."} +{"id": "01452", "text": "Suppose there is a $h \\times w$ grid consisting of empty or full cells. Let's make some definitions:\n\n $r_{i}$ is the number of consecutive full cells connected to the left side in the $i$-th row ($1 \\le i \\le h$). In particular, $r_i=0$ if the leftmost cell of the $i$-th row is empty. $c_{j}$ is the number of consecutive full cells connected to the top end in the $j$-th column ($1 \\le j \\le w$). In particular, $c_j=0$ if the topmost cell of the $j$-th column is empty. \n\nIn other words, the $i$-th row starts exactly with $r_i$ full cells. Similarly, the $j$-th column starts exactly with $c_j$ full cells.\n\n [Image] These are the $r$ and $c$ values of some $3 \\times 4$ grid. Black cells are full and white cells are empty. \n\nYou have values of $r$ and $c$. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of $r$ and $c$. Since the answer can be very large, find the answer modulo $1000000007\\,(10^{9} + 7)$. In other words, find the remainder after division of the answer by $1000000007\\,(10^{9} + 7)$.\n\n\n-----Input-----\n\nThe first line contains two integers $h$ and $w$ ($1 \\le h, w \\le 10^{3}$)\u00a0\u2014 the height and width of the grid.\n\nThe second line contains $h$ integers $r_{1}, r_{2}, \\ldots, r_{h}$ ($0 \\le r_{i} \\le w$)\u00a0\u2014 the values of $r$.\n\nThe third line contains $w$ integers $c_{1}, c_{2}, \\ldots, c_{w}$ ($0 \\le c_{j} \\le h$)\u00a0\u2014 the values of $c$.\n\n\n-----Output-----\n\nPrint the answer modulo $1000000007\\,(10^{9} + 7)$.\n\n\n-----Examples-----\nInput\n3 4\n0 3 1\n0 2 3 0\n\nOutput\n2\n\nInput\n1 1\n0\n1\n\nOutput\n0\n\nInput\n19 16\n16 16 16 16 15 15 0 5 0 4 9 9 1 4 4 0 8 16 12\n6 12 19 15 8 6 19 19 14 6 9 16 10 11 15 4\n\nOutput\n797922655\n\n\n\n-----Note-----\n\nIn the first example, this is the other possible case.\n\n [Image] \n\nIn the second example, it's impossible to make a grid to satisfy such $r$, $c$ values.\n\nIn the third example, make sure to print answer modulo $(10^9 + 7)$."} +{"id": "01453", "text": "Tsumugi brought $n$ delicious sweets to the Light Music Club. They are numbered from $1$ to $n$, where the $i$-th sweet has a sugar concentration described by an integer $a_i$.\n\nYui loves sweets, but she can eat at most $m$ sweets each day for health reasons.\n\nDays are $1$-indexed (numbered $1, 2, 3, \\ldots$). Eating the sweet $i$ at the $d$-th day will cause a sugar penalty of $(d \\cdot a_i)$, as sweets become more sugary with time. A sweet can be eaten at most once.\n\nThe total sugar penalty will be the sum of the individual penalties of each sweet eaten.\n\nSuppose that Yui chooses exactly $k$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get?\n\nSince Yui is an undecided girl, she wants you to answer this question for every value of $k$ between $1$ and $n$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le m \\le n \\le 200\\ 000$).\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 200\\ 000$).\n\n\n-----Output-----\n\nYou have to output $n$ integers $x_1, x_2, \\ldots, x_n$ on a single line, separed by spaces, where $x_k$ is the minimum total sugar penalty Yui can get if she eats exactly $k$ sweets.\n\n\n-----Examples-----\nInput\n9 2\n6 19 3 4 4 2 6 7 8\n\nOutput\n2 5 11 18 30 43 62 83 121\n\nInput\n1 1\n7\n\nOutput\n7\n\n\n\n-----Note-----\n\nLet's analyze the answer for $k = 5$ in the first example. Here is one of the possible ways to eat $5$ sweets that minimize total sugar penalty: Day $1$: sweets $1$ and $4$ Day $2$: sweets $5$ and $3$ Day $3$ : sweet $6$ \n\nTotal penalty is $1 \\cdot a_1 + 1 \\cdot a_4 + 2 \\cdot a_5 + 2 \\cdot a_3 + 3 \\cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $5$ sweets, hence $x_5 = 30$."} +{"id": "01454", "text": "In this problem, a $n \\times m$ rectangular matrix $a$ is called increasing if, for each row of $i$, when go from left to right, the values strictly increase (that is, $a_{i,1}0$ moves.\n\nThe third configuration is described in the statement."} +{"id": "01466", "text": "You are given an undirected connected weighted graph consisting of $n$ vertices and $m$ edges. Let's denote the length of the shortest path from vertex $1$ to vertex $i$ as $d_i$. \n\nYou have to erase some edges of the graph so that at most $k$ edges remain. Let's call a vertex $i$ good if there still exists a path from $1$ to $i$ with length $d_i$ after erasing the edges.\n\nYour goal is to erase the edges in such a way that the number of good vertices is maximized.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $k$ ($2 \\le n \\le 3 \\cdot 10^5$, $1 \\le m \\le 3 \\cdot 10^5$, $n - 1 \\le m$, $0 \\le k \\le m$) \u2014 the number of vertices and edges in the graph, and the maximum number of edges that can be retained in the graph, respectively.\n\nThen $m$ lines follow, each containing three integers $x$, $y$, $w$ ($1 \\le x, y \\le n$, $x \\ne y$, $1 \\le w \\le 10^9$), denoting an edge connecting vertices $x$ and $y$ and having weight $w$.\n\nThe given graph is connected (any vertex can be reached from any other vertex) and simple (there are no self-loops, and for each unordered pair of vertices there exists at most one edge connecting these vertices).\n\n\n-----Output-----\n\nIn the first line print $e$ \u2014 the number of edges that should remain in the graph ($0 \\le e \\le k$).\n\nIn the second line print $e$ distinct integers from $1$ to $m$ \u2014 the indices of edges that should remain in the graph. Edges are numbered in the same order they are given in the input. The number of good vertices should be as large as possible.\n\n\n-----Examples-----\nInput\n3 3 2\n1 2 1\n3 2 1\n1 3 3\n\nOutput\n2\n1 2 \nInput\n4 5 2\n4 1 8\n2 4 1\n2 1 3\n3 4 9\n3 1 5\n\nOutput\n2\n3 2"} +{"id": "01467", "text": "You are given an array $a_1, a_2, \\dots, a_n$ of integer numbers.\n\nYour task is to divide the array into the maximum number of segments in such a way that:\n\n each element is contained in exactly one segment; each segment contains at least one element; there doesn't exist a non-empty subset of segments such that bitwise XOR of the numbers from them is equal to $0$. \n\nPrint the maximum number of segments the array can be divided into. Print -1 if no suitable division exists.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the size of the array.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint the maximum number of segments the array can be divided into while following the given constraints. Print -1 if no suitable division exists.\n\n\n-----Examples-----\nInput\n4\n5 5 7 2\n\nOutput\n2\n\nInput\n3\n1 2 3\n\nOutput\n-1\n\nInput\n3\n3 1 10\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example $2$ is the maximum number. If you divide the array into $\\{[5], [5, 7, 2]\\}$, the XOR value of the subset of only the second segment is $5 \\oplus 7 \\oplus 2 = 0$. $\\{[5, 5], [7, 2]\\}$ has the value of the subset of only the first segment being $5 \\oplus 5 = 0$. However, $\\{[5, 5, 7], [2]\\}$ will lead to subsets $\\{[5, 5, 7]\\}$ of XOR $7$, $\\{[2]\\}$ of XOR $2$ and $\\{[5, 5, 7], [2]\\}$ of XOR $5 \\oplus 5 \\oplus 7 \\oplus 2 = 5$.\n\nLet's take a look at some division on $3$ segments \u2014 $\\{[5], [5, 7], [2]\\}$. It will produce subsets:\n\n $\\{[5]\\}$, XOR $5$; $\\{[5, 7]\\}$, XOR $2$; $\\{[5], [5, 7]\\}$, XOR $7$; $\\{[2]\\}$, XOR $2$; $\\{[5], [2]\\}$, XOR $7$; $\\{[5, 7], [2]\\}$, XOR $0$; $\\{[5], [5, 7], [2]\\}$, XOR $5$; \n\nAs you can see, subset $\\{[5, 7], [2]\\}$ has its XOR equal to $0$, which is unacceptable. You can check that for other divisions of size $3$ or $4$, non-empty subset with $0$ XOR always exists.\n\nThe second example has no suitable divisions.\n\nThe third example array can be divided into $\\{[3], [1], [10]\\}$. No subset of these segments has its XOR equal to $0$."} +{"id": "01468", "text": "Vasya is an administrator of a public page of organization \"Mouse and keyboard\" and his everyday duty is to publish news from the world of competitive programming. For each news he also creates a list of hashtags to make searching for a particular topic more comfortable. For the purpose of this problem we define hashtag as a string consisting of lowercase English letters and exactly one symbol '#' located at the beginning of the string. The length of the hashtag is defined as the number of symbols in it without the symbol '#'.\n\nThe head administrator of the page told Vasya that hashtags should go in lexicographical order (take a look at the notes section for the definition).\n\nVasya is lazy so he doesn't want to actually change the order of hashtags in already published news. Instead, he decided to delete some suffixes (consecutive characters at the end of the string) of some of the hashtags. He is allowed to delete any number of characters, even the whole string except for the symbol '#'. Vasya wants to pick such a way to delete suffixes that the total number of deleted symbols is minimum possible. If there are several optimal solutions, he is fine with any of them.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 500 000)\u00a0\u2014 the number of hashtags being edited now.\n\nEach of the next n lines contains exactly one hashtag of positive length.\n\nIt is guaranteed that the total length of all hashtags (i.e. the total length of the string except for characters '#') won't exceed 500 000.\n\n\n-----Output-----\n\nPrint the resulting hashtags in any of the optimal solutions.\n\n\n-----Examples-----\nInput\n3\n#book\n#bigtown\n#big\n\nOutput\n#b\n#big\n#big\n\nInput\n3\n#book\n#cool\n#cold\n\nOutput\n#book\n#co\n#cold\n\nInput\n4\n#car\n#cart\n#art\n#at\n\nOutput\n#\n#\n#art\n#at\n\nInput\n3\n#apple\n#apple\n#fruit\n\nOutput\n#apple\n#apple\n#fruit\n\n\n\n-----Note-----\n\nWord a_1, a_2, ..., a_{m} of length m is lexicographically not greater than word b_1, b_2, ..., b_{k} of length k, if one of two conditions hold: at first position i, such that a_{i} \u2260 b_{i}, the character a_{i} goes earlier in the alphabet than character b_{i}, i.e. a has smaller character than b in the first position where they differ; if there is no such position i and m \u2264 k, i.e. the first word is a prefix of the second or two words are equal. \n\nThe sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word.\n\nFor the words consisting of lowercase English letters the lexicographical order coincides with the alphabet word order in the dictionary.\n\nAccording to the above definition, if a hashtag consisting of one character '#' it is lexicographically not greater than any other valid hashtag. That's why in the third sample we can't keep first two hashtags unchanged and shorten the other two."} +{"id": "01469", "text": "You are given an integer L. Construct a directed graph that satisfies the conditions below. The graph may contain multiple edges between the same pair of vertices. It can be proved that such a graph always exists.\n - The number of vertices, N, is at most 20. The vertices are given ID numbers from 1 to N.\n - The number of edges, M, is at most 60. Each edge has an integer length between 0 and 10^6 (inclusive).\n - Every edge is directed from the vertex with the smaller ID to the vertex with the larger ID. That is, 1,2,...,N is one possible topological order of the vertices.\n - There are exactly L different paths from Vertex 1 to Vertex N. The lengths of these paths are all different, and they are integers between 0 and L-1.\nHere, the length of a path is the sum of the lengths of the edges contained in that path, and two paths are considered different when the sets of the edges contained in those paths are different.\n\n-----Constraints-----\n - 2 \\leq L \\leq 10^6\n - L is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nL\n\n-----Output-----\nIn the first line, print N and M, the number of the vertices and edges in your graph.\nIn the i-th of the following M lines, print three integers u_i,v_i and w_i, representing the starting vertex, the ending vertex and the length of the i-th edge.\nIf there are multiple solutions, any of them will be accepted.\n\n-----Sample Input-----\n4\n\n-----Sample Output-----\n8 10\n1 2 0\n2 3 0\n3 4 0\n1 5 0\n2 6 0\n3 7 0\n4 8 0\n5 6 1\n6 7 1\n7 8 1\n\nIn the graph represented by the sample output, there are four paths from Vertex 1 to N=8:\n - 1 \u2192 2 \u2192 3 \u2192 4 \u2192 8 with length 0\n - 1 \u2192 2 \u2192 3 \u2192 7 \u2192 8 with length 1\n - 1 \u2192 2 \u2192 6 \u2192 7 \u2192 8 with length 2\n - 1 \u2192 5 \u2192 6 \u2192 7 \u2192 8 with length 3\nThere are other possible solutions."} +{"id": "01470", "text": "Snuke has decided to play with a six-sided die. Each of its six sides shows an integer 1 through 6, and two numbers on opposite sides always add up to 7.\nSnuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:\n - Operation: Rotate the die 90\u00b0 toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain y points where y is the number written in the side facing upward.\nFor example, let us consider the situation where the side showing 1 faces upward, the near side shows 5 and the right side shows 4, as illustrated in the figure.\nIf the die is rotated toward the right as shown in the figure, the side showing 3 will face upward.\nBesides, the side showing 4 will face upward if the die is rotated toward the left, the side showing 2 will face upward if the die is rotated toward the front, and the side showing 5 will face upward if the die is rotated toward the back.\nFind the minimum number of operation Snuke needs to perform in order to score at least x points in total.\n\n-----Constraints-----\n - 1 \u2266 x \u2266 10^{15}\n - x is an integer.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nx\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n7\n\n-----Sample Output-----\n2\n"} +{"id": "01471", "text": "We have a tree with N vertices numbered 1 to N.\nThe i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.\nYour objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:\n - For any two vertices painted in the same color, the distance between them is an even number.\nFind a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq u_i < v_i \\leq N\n - 1 \\leq w_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nu_1 v_1 w_1\nu_2 v_2 w_2\n.\n.\n.\nu_{N - 1} v_{N - 1} w_{N - 1}\n\n-----Output-----\nPrint a coloring of the vertices that satisfies the condition, in N lines.\nThe i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.\nIf there are multiple colorings that satisfy the condition, any of them will be accepted.\n\n-----Sample Input-----\n3\n1 2 2\n2 3 1\n\n-----Sample Output-----\n0\n0\n1\n"} +{"id": "01472", "text": "We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:\n - For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.\n - There is an edge between Vertex X and Vertex Y.\nFor each k=1,2,...,N-1, solve the problem below:\n - Find the number of pairs of integers (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j in G is k.\n\n-----Constraints-----\n - 3 \\leq N \\leq 2 \\times 10^3\n - 1 \\leq X,Y \\leq N\n - X+1 < Y\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN X Y\n\n-----Output-----\nFor each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.\n\n-----Sample Input-----\n5 2 4\n\n-----Sample Output-----\n5\n4\n1\n0\n\nThe graph in this input is as follows:\n\n\n\n\n\nThere are five pairs (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\\,,(2,3)\\,,(2,4)\\,,(3,4)\\,,(4,5).\n\n\nThere are four pairs (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\\,,(1,4)\\,,(2,5)\\,,(3,5).\n\n\nThere is one pair (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).\n\n\nThere are no pairs (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j is 4."} +{"id": "01473", "text": "During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.\n\nStanding in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).\n\nAfter that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.\n\nHelp the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.\n\n\n-----Input-----\n\nThe first line contains integer n (2 \u2264 n \u2264 2\u00b710^5) \u2014 the number of students in the queue. \n\nThen n lines follow, i-th line contains the pair of integers a_{i}, b_{i} (0 \u2264 a_{i}, b_{i} \u2264 10^6), where a_{i} is the ID number of a person in front of a student and b_{i} is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.\n\nThe ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.\n\n\n-----Output-----\n\nPrint a sequence of n integers x_1, x_2, ..., x_{n} \u2014 the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.\n\n\n-----Examples-----\nInput\n4\n92 31\n0 7\n31 0\n7 141\n\nOutput\n92 7 31 141 \n\n\n\n-----Note-----\n\nThe picture illustrates the queue for the first sample. [Image]"} +{"id": "01474", "text": "Long ago, Vasily built a good fence at his country house. Vasily calls a fence good, if it is a series of n consecutively fastened vertical boards of centimeter width, the height of each in centimeters is a positive integer. The house owner remembers that the height of the i-th board to the left is h_{i}.\n\nToday Vasily decided to change the design of the fence he had built, by cutting his top connected part so that the fence remained good. The cut part should consist of only the upper parts of the boards, while the adjacent parts must be interconnected (share a non-zero length before cutting out of the fence).\n\nYou, as Vasily's curious neighbor, will count the number of possible ways to cut exactly one part as is described above. Two ways to cut a part are called distinct, if for the remaining fences there is such i, that the height of the i-th boards vary.\n\nAs Vasily's fence can be very high and long, get the remainder after dividing the required number of ways by 1 000 000 007 (10^9 + 7).\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 1 000 000)\u00a0\u2014 the number of boards in Vasily's fence.\n\nThe second line contains n space-separated numbers h_1, h_2, ..., h_{n} (1 \u2264 h_{i} \u2264 10^9), where h_{i} equals the height of the i-th board to the left.\n\n\n-----Output-----\n\nPrint the remainder after dividing r by 1 000 000 007, where r is the number of ways to cut exactly one connected part so that the part consisted of the upper parts of the boards and the remaining fence was good.\n\n\n-----Examples-----\nInput\n2\n1 1\n\nOutput\n0\n\nInput\n3\n3 4 2\n\nOutput\n13\n\n\n\n-----Note-----\n\nFrom the fence from the first example it is impossible to cut exactly one piece so as the remaining fence was good.\n\nAll the possible variants of the resulting fence from the second sample look as follows (the grey shows the cut out part): [Image]"} +{"id": "01475", "text": "There are b blocks of digits. Each one consisting of the same n digits, which are given to you in the input. Wet Shark must choose exactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit 1 from the first block and digit 2 from the second block, he gets the integer 12. \n\nWet Shark then takes this number modulo x. Please, tell him how many ways he can choose one digit from each block so that he gets exactly k as the final result. As this number may be too large, print it modulo 10^9 + 7.\n\nNote, that the number of ways to choose some digit in the block is equal to the number of it's occurrences. For example, there are 3 ways to choose digit 5 from block 3 5 6 7 8 9 5 1 1 1 1 5.\n\n\n-----Input-----\n\nThe first line of the input contains four space-separated integers, n, b, k and x (2 \u2264 n \u2264 50 000, 1 \u2264 b \u2264 10^9, 0 \u2264 k < x \u2264 100, x \u2265 2)\u00a0\u2014 the number of digits in one block, the number of blocks, interesting remainder modulo x and modulo x itself.\n\nThe next line contains n space separated integers a_{i} (1 \u2264 a_{i} \u2264 9), that give the digits contained in each block.\n\n\n-----Output-----\n\nPrint the number of ways to pick exactly one digit from each blocks, such that the resulting integer equals k modulo x.\n\n\n-----Examples-----\nInput\n12 1 5 10\n3 5 6 7 8 9 5 1 1 1 1 5\n\nOutput\n3\n\nInput\n3 2 1 2\n6 2 2\n\nOutput\n0\n\nInput\n3 2 1 2\n3 1 2\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the second sample possible integers are 22, 26, 62 and 66. None of them gives the remainder 1 modulo 2.\n\nIn the third sample integers 11, 13, 21, 23, 31 and 33 have remainder 1 modulo 2. There is exactly one way to obtain each of these integers, so the total answer is 6."} +{"id": "01476", "text": "An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.\n\nYour task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.\n\n\n-----Input-----\n\nA single line contains integer n (1 \u2264 n \u2264 5000) \u2014 the number of students at an exam.\n\n\n-----Output-----\n\nIn the first line print integer k \u2014 the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.\n\nIn the second line print k distinct integers a_1, a_2, ..., a_{k} (1 \u2264 a_{i} \u2264 n), where a_{i} is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |a_{i} - a_{i} + 1| \u2260 1 for all i from 1 to k - 1.\n\nIf there are several possible answers, output any of them.\n\n\n-----Examples-----\nInput\n6\nOutput\n6\n1 5 3 6 2 4\nInput\n3\n\nOutput\n2\n1 3"} +{"id": "01477", "text": "You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depends on have already completed.\n\nSome of the tasks in the graph can only be executed on a coprocessor, and the rest can only be executed on the main processor. In one coprocessor call you can send it a set of tasks which can only be executed on it. For each task of the set, all tasks on which it depends must be either already completed or be included in the set. The main processor starts the program execution and gets the results of tasks executed on the coprocessor automatically.\n\nFind the minimal number of coprocessor calls which are necessary to execute the given program.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers N (1 \u2264 N \u2264 10^5) \u2014 the total number of tasks given, and M (0 \u2264 M \u2264 10^5) \u2014 the total number of dependencies between tasks.\n\nThe next line contains N space-separated integers $E_{i} \\in \\{0,1 \\}$. If E_{i} = 0, task i can only be executed on the main processor, otherwise it can only be executed on the coprocessor.\n\nThe next M lines describe the dependencies between tasks. Each line contains two space-separated integers T_1 and T_2 and means that task T_1 depends on task T_2 (T_1 \u2260 T_2). Tasks are indexed from 0 to N - 1. All M pairs (T_1, T_2) are distinct. It is guaranteed that there are no circular dependencies between tasks.\n\n\n-----Output-----\n\nOutput one line containing an integer \u2014 the minimal number of coprocessor calls necessary to execute the program.\n\n\n-----Examples-----\nInput\n4 3\n0 1 0 1\n0 1\n1 2\n2 3\n\nOutput\n2\n\nInput\n4 3\n1 1 1 0\n0 1\n0 2\n3 0\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first test, tasks 1 and 3 can only be executed on the coprocessor. The dependency graph is linear, so the tasks must be executed in order 3 -> 2 -> 1 -> 0. You have to call coprocessor twice: first you call it for task 3, then you execute task 2 on the main processor, then you call it for for task 1, and finally you execute task 0 on the main processor.\n\nIn the second test, tasks 0, 1 and 2 can only be executed on the coprocessor. Tasks 1 and 2 have no dependencies, and task 0 depends on tasks 1 and 2, so all three tasks 0, 1 and 2 can be sent in one coprocessor call. After that task 3 is executed on the main processor."} +{"id": "01478", "text": "A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.\n\nEach comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment.\n\nWhen Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: at first, the text of the comment is written; after that the number of comments is written, for which this comment is a parent comment (i.\u00a0e. the number of the replies to this comments); after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma.\n\nFor example, if the comments look like: [Image] \n\nthen the first comment is written as \"hello,2,ok,0,bye,0\", the second is written as \"test,0\", the third comment is written as \"one,1,two,2,a,0,b,0\". The whole comments feed is written as: \"hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0\". For a given comments feed in the format specified above print the comments in a different format: at first, print a integer d\u00a0\u2014 the maximum depth of nesting comments; after that print d lines, the i-th of them corresponds to nesting level i; for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. \n\n\n-----Input-----\n\nThe first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. \n\nIt is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros.\n\nThe length of the whole string does not exceed 10^6. It is guaranteed that given structure of comments is valid. \n\n\n-----Output-----\n\nPrint comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.\n\n\n-----Examples-----\nInput\nhello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0\n\nOutput\n3\nhello test one \nok bye two \na b \n\n\n\nInput\na,5,A,0,a,0,A,0,a,0,A,0\n\nOutput\n2\na \nA a A a A \n\n\n\nInput\nA,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0\n\nOutput\n4\nA K M \nB F H L N O \nC D G I P \nE J \n\n\n\n\n-----Note-----\n\nThe first example is explained in the statements."} +{"id": "01479", "text": "Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. [Image] \n\nThe park can be represented as a rectangular n \u00d7 m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.\n\nOm Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). \n\nWe know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.\n\nEach time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, k (2 \u2264 n, m \u2264 2000;\u00a00 \u2264 k \u2264 m(n - 1)). \n\nEach of the next n lines contains m characters \u2014 the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals \".\", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: \"L\" (meaning that this cell has a spider at time 0, moving left), \"R\" (a spider moving right), \"U\" (a spider moving up), \"D\" (a spider moving down). \n\nIt is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.\n\n\n-----Output-----\n\nPrint m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.\n\n\n-----Examples-----\nInput\n3 3 4\n...\nR.L\nR.U\n\nOutput\n0 2 2 \nInput\n2 2 2\n..\nRL\n\nOutput\n1 1 \nInput\n2 2 2\n..\nLR\n\nOutput\n0 0 \nInput\n3 4 8\n....\nRRLL\nUUUU\n\nOutput\n1 3 3 1 \nInput\n2 2 2\n..\nUU\n\nOutput\n0 0 \n\n\n-----Note-----\n\nConsider the first sample. The notes below show how the spider arrangement changes on the field over time:\n\n... ... ..U ...\n\nR.L -> .*U -> L.R -> ...\n\nR.U .R. ..R ...\n\n\n\n\n\nCharacter \"*\" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2."} +{"id": "01480", "text": "n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out a_{i} people in clockwise order, starting from the next person. The last one to be pointed at by the leader is eliminated, and the next player after him becomes the new leader.\n\nFor example, if there are children with numbers [8, 10, 13, 14, 16] currently in the circle, the leader is child 13 and a_{i} = 12, then counting-out rhyme ends on child 16, who is eliminated. Child 8 becomes the leader.\n\nYou have to write a program which prints the number of the child to be eliminated on every step.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and k (2 \u2264 n \u2264 100, 1 \u2264 k \u2264 n - 1).\n\nThe next line contains k integer numbers a_1, a_2, ..., a_{k} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.\n\n\n-----Examples-----\nInput\n7 5\n10 4 11 4 1\n\nOutput\n4 2 5 6 1 \n\nInput\n3 2\n2 5\n\nOutput\n3 2 \n\n\n\n-----Note-----\n\nLet's consider first example: In the first step child 4 is eliminated, child 5 becomes the leader. In the second step child 2 is eliminated, child 3 becomes the leader. In the third step child 5 is eliminated, child 6 becomes the leader. In the fourth step child 6 is eliminated, child 7 becomes the leader. In the final step child 1 is eliminated, child 3 becomes the leader."} +{"id": "01481", "text": "Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?\n\nGiven a n \u00d7 n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces.\n\n\n-----Output-----\n\nPrint \"YES\" or \"NO\" (without the quotes) depending on the answer to the problem.\n\n\n-----Examples-----\nInput\n3\nxxo\nxox\noxx\n\nOutput\nYES\n\nInput\n4\nxxxo\nxoxo\noxox\nxxxx\n\nOutput\nNO"} +{"id": "01482", "text": "One day Ms Swan bought an orange in a shop. The orange consisted of n\u00b7k segments, numbered with integers from 1 to n\u00b7k. \n\nThere were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 \u2264 i \u2264 k) child wrote the number a_{i} (1 \u2264 a_{i} \u2264 n\u00b7k). All numbers a_{i} accidentally turned out to be different.\n\nNow the children wonder, how to divide the orange so as to meet these conditions: each child gets exactly n orange segments; the i-th child gets the segment with number a_{i} for sure; no segment goes to two children simultaneously. \n\nHelp the children, divide the orange and fulfill the requirements, described above.\n\n\n-----Input-----\n\nThe first line contains two integers n, k (1 \u2264 n, k \u2264 30). The second line contains k space-separated integers a_1, a_2, ..., a_{k} (1 \u2264 a_{i} \u2264 n\u00b7k), where a_{i} is the number of the orange segment that the i-th child would like to get.\n\nIt is guaranteed that all numbers a_{i} are distinct.\n\n\n-----Output-----\n\nPrint exactly n\u00b7k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.\n\nYou can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.\n\n\n-----Examples-----\nInput\n2 2\n4 1\n\nOutput\n2 4 \n1 3 \n\nInput\n3 1\n2\n\nOutput\n3 2 1"} +{"id": "01483", "text": "In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of $n$ students doing yet another trick. \n\nLet's assume that all these students are numbered from $1$ to $n$. The teacher came to student $a$ and put a hole in his badge. The student, however, claimed that the main culprit is some other student $p_a$.\n\nAfter that, the teacher came to student $p_a$ and made a hole in his badge as well. The student in reply said that the main culprit was student $p_{p_a}$.\n\nThis process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.\n\nAfter that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.\n\nYou don't know the first student who was caught by the teacher. However, you know all the numbers $p_i$. Your task is to find out for every student $a$, who would be the student with two holes in the badge if the first caught student was $a$.\n\n\n-----Input-----\n\nThe first line of the input contains the only integer $n$ ($1 \\le n \\le 1000$)\u00a0\u2014 the number of the naughty students.\n\nThe second line contains $n$ integers $p_1$, ..., $p_n$ ($1 \\le p_i \\le n$), where $p_i$ indicates the student who was reported to the teacher by student $i$.\n\n\n-----Output-----\n\nFor every student $a$ from $1$ to $n$ print which student would receive two holes in the badge, if $a$ was the first student caught by the teacher.\n\n\n-----Examples-----\nInput\n3\n2 3 2\n\nOutput\n2 2 3 \n\nInput\n3\n1 2 3\n\nOutput\n1 2 3 \n\n\n\n-----Note-----\n\nThe picture corresponds to the first example test case.\n\n $8$ \n\nWhen $a = 1$, the teacher comes to students $1$, $2$, $3$, $2$, in this order, and the student $2$ is the one who receives a second hole in his badge.\n\nWhen $a = 2$, the teacher comes to students $2$, $3$, $2$, and the student $2$ gets a second hole in his badge. When $a = 3$, the teacher will visit students $3$, $2$, $3$ with student $3$ getting a second hole in his badge.\n\nFor the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge."} +{"id": "01484", "text": "Let's denote that some array $b$ is bad if it contains a subarray $b_l, b_{l+1}, \\dots, b_{r}$ of odd length more than $1$ ($l < r$ and $r - l + 1$ is odd) such that $\\forall i \\in \\{0, 1, \\dots, r - l\\}$ $b_{l + i} = b_{r - i}$.\n\nIf an array is not bad, it is good.\n\nNow you are given an array $a_1, a_2, \\dots, a_n$. Some elements are replaced by $-1$. Calculate the number of good arrays you can obtain by replacing each $-1$ with some integer from $1$ to $k$.\n\nSince the answer can be large, print it modulo $998244353$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($2 \\le n, k \\le 2 \\cdot 10^5$) \u2014 the length of array $a$ and the size of \"alphabet\", i. e., the upper bound on the numbers you may use to replace $-1$.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($a_i = -1$ or $1 \\le a_i \\le k$) \u2014 the array $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of good arrays you can get, modulo $998244353$.\n\n\n-----Examples-----\nInput\n2 3\n-1 -1\n\nOutput\n9\n\nInput\n5 2\n1 -1 -1 1 2\n\nOutput\n0\n\nInput\n5 3\n1 -1 -1 1 2\n\nOutput\n2\n\nInput\n4 200000\n-1 -1 12345 -1\n\nOutput\n735945883"} +{"id": "01485", "text": "There are three horses living in a horse land: one gray, one white and one gray-and-white. The horses are really amusing animals, which is why they adore special cards. Each of those cards must contain two integers, the first one on top, the second one in the bottom of the card. Let's denote a card with a on the top and b in the bottom as (a, b).\n\nEach of the three horses can paint the special cards. If you show an (a, b) card to the gray horse, then the horse can paint a new (a + 1, b + 1) card. If you show an (a, b) card, such that a and b are even integers, to the white horse, then the horse can paint a new $(\\frac{a}{2}, \\frac{b}{2})$ card. If you show two cards (a, b) and (b, c) to the gray-and-white horse, then he can paint a new (a, c) card.\n\nPolycarpus really wants to get n special cards (1, a_1), (1, a_2), ..., (1, a_{n}). For that he is going to the horse land. He can take exactly one (x, y) card to the horse land, such that 1 \u2264 x < y \u2264 m. How many ways are there to choose the card so that he can perform some actions in the horse land and get the required cards?\n\nPolycarpus can get cards from the horses only as a result of the actions that are described above. Polycarpus is allowed to get additional cards besides the cards that he requires.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n \u2264 10^5, 2 \u2264 m \u2264 10^9). The second line contains the sequence of integers a_1, a_2, ..., a_{n} (2 \u2264 a_{i} \u2264 10^9). Note, that the numbers in the sequence can coincide.\n\nThe numbers in the lines are separated by single spaces.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem. \n\nPlease, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n1 6\n2\n\nOutput\n11\n\nInput\n1 6\n7\n\nOutput\n14\n\nInput\n2 10\n13 7\n\nOutput\n36"} +{"id": "01486", "text": "All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position x_{i} \u2014 a coordinate on the Ox axis. No two cities are located at a single point.\n\nLineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in).\n\nStrange but true, the cost of sending the letter is exactly equal to the distance between the sender's city and the recipient's city.\n\nFor each city calculate two values \u200b\u200bmin_{i} and max_{i}, where min_{i} is the minimum cost of sending a letter from the i-th city to some other city, and max_{i} is the the maximum cost of sending a letter from the i-th city to some other city\n\n\n-----Input-----\n\nThe first line of the input contains integer n (2 \u2264 n \u2264 10^5) \u2014 the number of cities in Lineland. The second line contains the sequence of n distinct integers x_1, x_2, ..., x_{n} ( - 10^9 \u2264 x_{i} \u2264 10^9), where x_{i} is the x-coordinate of the i-th city. All the x_{i}'s are distinct and follow in ascending order.\n\n\n-----Output-----\n\nPrint n lines, the i-th line must contain two integers min_{i}, max_{i}, separated by a space, where min_{i} is the minimum cost of sending a letter from the i-th city, and max_{i} is the maximum cost of sending a letter from the i-th city.\n\n\n-----Examples-----\nInput\n4\n-5 -2 2 7\n\nOutput\n3 12\n3 9\n4 7\n5 12\n\nInput\n2\n-1 1\n\nOutput\n2 2\n2 2"} +{"id": "01487", "text": "Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:\n\nWe will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that s_{i} isn't equal to t_{i}. \n\nAs besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.\n\nIt's time for Susie to go to bed, help her find such string p or state that it is impossible.\n\n\n-----Input-----\n\nThe first line contains string s of length n. \n\nThe second line contains string t of length n.\n\nThe length of string n is within range from 1 to 10^5. It is guaranteed that both strings contain only digits zero and one.\n\n\n-----Output-----\n\nPrint a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line \"impossible\" (without the quotes).\n\nIf there are multiple possible answers, print any of them.\n\n\n-----Examples-----\nInput\n0001\n1011\n\nOutput\n0011\n\nInput\n000\n111\n\nOutput\nimpossible\n\n\n\n-----Note-----\n\nIn the first sample different answers are possible, namely \u2014 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101."} +{"id": "01488", "text": "Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a_1, a_2, ..., a_{n}. The number a_{k} represents that the kth destination is at distance a_{k} kilometers from the starting point. No two destinations are located in the same place. \n\nIahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. \n\nThe distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a \"route\" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. \n\nIahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.\n\n\n-----Input-----\n\nThe first line contains integer n (2 \u2264 n \u2264 10^5). Next line contains n distinct integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^7).\n\n\n-----Output-----\n\nOutput two integers \u2014 the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.\n\n\n-----Examples-----\nInput\n3\n2 3 5\n\nOutput\n22 3\n\n\n-----Note-----\n\nConsider 6 possible routes: [2, 3, 5]: total distance traveled: |2 \u2013 0| + |3 \u2013 2| + |5 \u2013 3| = 5; [2, 5, 3]: |2 \u2013 0| + |5 \u2013 2| + |3 \u2013 5| = 7; [3, 2, 5]: |3 \u2013 0| + |2 \u2013 3| + |5 \u2013 2| = 7; [3, 5, 2]: |3 \u2013 0| + |5 \u2013 3| + |2 \u2013 5| = 8; [5, 2, 3]: |5 \u2013 0| + |2 \u2013 5| + |3 \u2013 2| = 9; [5, 3, 2]: |5 \u2013 0| + |3 \u2013 5| + |2 \u2013 3| = 8. \n\nThe average travel distance is $\\frac{1}{6} \\cdot(5 + 7 + 7 + 8 + 9 + 8)$ = $\\frac{44}{6}$ = $\\frac{22}{3}$."} +{"id": "01489", "text": "You are given n distinct points on a plane with integral coordinates. For each point you can either draw a vertical line through it, draw a horizontal line through it, or do nothing.\n\nYou consider several coinciding straight lines as a single one. How many distinct pictures you can get? Print the answer modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of points.\n\nn lines follow. The (i + 1)-th of these lines contains two integers x_{i}, y_{i} ( - 10^9 \u2264 x_{i}, y_{i} \u2264 10^9)\u00a0\u2014 coordinates of the i-th point.\n\nIt is guaranteed that all points are distinct.\n\n\n-----Output-----\n\nPrint the number of possible distinct pictures modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n4\n1 1\n1 2\n2 1\n2 2\n\nOutput\n16\n\nInput\n2\n-1 -1\n0 1\n\nOutput\n9\n\n\n\n-----Note-----\n\nIn the first example there are two vertical and two horizontal lines passing through the points. You can get pictures with any subset of these lines. For example, you can get the picture containing all four lines in two ways (each segment represents a line containing it). The first way: [Image] The second way: [Image] \n\nIn the second example you can work with two points independently. The number of pictures is 3^2 = 9."} +{"id": "01490", "text": "In Berland recently a new collection of toys went on sale. This collection consists of 10^9 types of toys, numbered with integers from 1 to 10^9. A toy from the new collection of the i-th type costs i bourles.\n\nTania has managed to collect n different types of toys a_1, a_2, ..., a_{n} from the new collection. Today is Tanya's birthday, and her mother decided to spend no more than m bourles on the gift to the daughter. Tanya will choose several different types of toys from the new collection as a gift. Of course, she does not want to get a type of toy which she already has.\n\nTanya wants to have as many distinct types of toys in her collection as possible as the result. The new collection is too diverse, and Tanya is too little, so she asks you to help her in this.\n\n\n-----Input-----\n\nThe first line contains two integers n (1 \u2264 n \u2264 100 000) and m (1 \u2264 m \u2264 10^9)\u00a0\u2014 the number of types of toys that Tanya already has and the number of bourles that her mom is willing to spend on buying new toys.\n\nThe next line contains n distinct integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the types of toys that Tanya already has.\n\n\n-----Output-----\n\nIn the first line print a single integer k\u00a0\u2014 the number of different types of toys that Tanya should choose so that the number of different types of toys in her collection is maximum possible. Of course, the total cost of the selected toys should not exceed m.\n\nIn the second line print k distinct space-separated integers t_1, t_2, ..., t_{k} (1 \u2264 t_{i} \u2264 10^9)\u00a0\u2014 the types of toys that Tanya should choose.\n\nIf there are multiple answers, you may print any of them. Values of t_{i} can be printed in any order.\n\n\n-----Examples-----\nInput\n3 7\n1 3 4\n\nOutput\n2\n2 5 \n\nInput\n4 14\n4 6 12 8\n\nOutput\n4\n7 2 3 1\n\n\n\n-----Note-----\n\nIn the first sample mom should buy two toys: one toy of the 2-nd type and one toy of the 5-th type. At any other purchase for 7 bourles (assuming that the toys of types 1, 3 and 4 have already been bought), it is impossible to buy two and more toys."} +{"id": "01491", "text": "Ann and Borya have n piles with candies and n is even number. There are a_{i} candies in pile with number i.\n\nAnn likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this candy is new and doesn't belong to any other pile) or remove one candy (if there is at least one candy in this pile). \n\nFind out minimal number of moves that is required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer.\n\n\n-----Input-----\n\nFirst line contains one even integer n (2 \u2264 n \u2264 200 000)\u00a0\u2014 number of piles with candies.\n\nSecond line contains sequence of integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 amounts of candies in each pile.\n\n\n-----Output-----\n\nOutput minimal number of steps required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0.\n\n\n-----Examples-----\nInput\n4\n12 14 30 4\n\nOutput\n2\n\nInput\n6\n0 0 0 0 0 0\n\nOutput\n6\n\nInput\n6\n120 110 23 34 25 45\n\nOutput\n3\n\nInput\n10\n121 56 78 81 45 100 1 0 54 78\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn first example you can satisfy condition in two moves. During each move you should add one candy to second pile. After it size of second pile becomes 16. After that Borya and Ann will have two piles with number of candies which is a square of integer (second and fourth pile) and two piles with number of candies which is not a square of any integer (first and third pile).\n\nIn second example you should add two candies to any three piles."} +{"id": "01492", "text": "The Berland Forest can be represented as an infinite cell plane. Every cell contains a tree. That is, contained before the recent events.\n\nA destructive fire raged through the Forest, and several trees were damaged by it. Precisely speaking, you have a $n \\times m$ rectangle map which represents the damaged part of the Forest. The damaged trees were marked as \"X\" while the remaining ones were marked as \".\". You are sure that all burnt trees are shown on the map. All the trees outside the map are undamaged.\n\nThe firemen quickly extinguished the fire, and now they are investigating the cause of it. The main version is that there was an arson: at some moment of time (let's consider it as $0$) some trees were set on fire. At the beginning of minute $0$, only the trees that were set on fire initially were burning. At the end of each minute, the fire spread from every burning tree to each of $8$ neighboring trees. At the beginning of minute $T$, the fire was extinguished.\n\nThe firemen want to find the arsonists as quickly as possible. The problem is, they know neither the value of $T$ (how long the fire has been raging) nor the coordinates of the trees that were initially set on fire. They want you to find the maximum value of $T$ (to know how far could the arsonists escape) and a possible set of trees that could be initially set on fire.\n\nNote that you'd like to maximize value $T$ but the set of trees can be arbitrary.\n\n\n-----Input-----\n\nThe first line contains two integer $n$ and $m$ ($1 \\le n, m \\le 10^6$, $1 \\le n \\cdot m \\le 10^6$) \u2014 the sizes of the map.\n\nNext $n$ lines contain the map. The $i$-th line corresponds to the $i$-th row of the map and contains $m$-character string. The $j$-th character of the $i$-th string is \"X\" if the corresponding tree is burnt and \".\" otherwise.\n\nIt's guaranteed that the map contains at least one \"X\".\n\n\n-----Output-----\n\nIn the first line print the single integer $T$ \u2014 the maximum time the Forest was on fire. In the next $n$ lines print the certificate: the map ($n \\times m$ rectangle) where the trees that were set on fire are marked as \"X\" and all other trees are marked as \".\".\n\n\n-----Examples-----\nInput\n3 6\nXXXXXX\nXXXXXX\nXXXXXX\n\nOutput\n1\n......\n.X.XX.\n......\n\nInput\n10 10\n.XXXXXX...\n.XXXXXX...\n.XXXXXX...\n.XXXXXX...\n.XXXXXXXX.\n...XXXXXX.\n...XXXXXX.\n...XXXXXX.\n...XXXXXX.\n..........\n\nOutput\n2\n..........\n..........\n...XX.....\n..........\n..........\n..........\n.....XX...\n..........\n..........\n..........\n\nInput\n4 5\nX....\n..XXX\n..XXX\n..XXX\n\nOutput\n0\nX....\n..XXX\n..XXX\n..XXX"} +{"id": "01493", "text": "DZY loves chessboard, and he enjoys playing with it.\n\nHe has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.\n\nYou task is to find any suitable placement of chessmen on the given chessboard.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m (1 \u2264 n, m \u2264 100).\n\nEach of the next n lines contains a string of m characters: the j-th character of the i-th string is either \".\" or \"-\". A \".\" means that the corresponding cell (in the i-th row and the j-th column) is good, while a \"-\" means it is bad.\n\n\n-----Output-----\n\nOutput must contain n lines, each line must contain a string of m characters. The j-th character of the i-th string should be either \"W\", \"B\" or \"-\". Character \"W\" means the chessman on the cell is white, \"B\" means it is black, \"-\" means the cell is a bad cell.\n\nIf multiple answers exist, print any of them. It is guaranteed that at least one answer exists.\n\n\n-----Examples-----\nInput\n1 1\n.\n\nOutput\nB\n\nInput\n2 2\n..\n..\n\nOutput\nBW\nWB\n\nInput\n3 3\n.-.\n---\n--.\nOutput\nB-B\n---\n--B\n\n\n-----Note-----\n\nIn the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.\n\nIn the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output.\n\nIn the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are."} +{"id": "01494", "text": "Dreamoon has a string s and a pattern string p. He first removes exactly x characters from s obtaining string s' as a result. Then he calculates $\\operatorname{occ}(s^{\\prime}, p)$ that is defined as the maximal number of non-overlapping substrings equal to p that can be found in s'. He wants to make this number as big as possible.\n\nMore formally, let's define $\\operatorname{ans}(x)$ as maximum value of $\\operatorname{occ}(s^{\\prime}, p)$ over all s' that can be obtained by removing exactly x characters from s. Dreamoon wants to know $\\operatorname{ans}(x)$ for all x from 0 to |s| where |s| denotes the length of string s.\n\n\n-----Input-----\n\nThe first line of the input contains the string s (1 \u2264 |s| \u2264 2 000).\n\nThe second line of the input contains the string p (1 \u2264 |p| \u2264 500).\n\nBoth strings will only consist of lower case English letters.\n\n\n-----Output-----\n\nPrint |s| + 1 space-separated integers in a single line representing the $\\operatorname{ans}(x)$ for all x from 0 to |s|.\n\n\n-----Examples-----\nInput\naaaaa\naa\n\nOutput\n2 2 1 1 0 0\n\nInput\naxbaxxb\nab\n\nOutput\n0 1 1 2 1 1 0 0\n\n\n\n-----Note-----\n\nFor the first sample, the corresponding optimal values of s' after removal 0 through |s| = 5 characters from s are {\"aaaaa\", \"aaaa\", \"aaa\", \"aa\", \"a\", \"\"}. \n\nFor the second sample, possible corresponding optimal values of s' are {\"axbaxxb\", \"abaxxb\", \"axbab\", \"abab\", \"aba\", \"ab\", \"a\", \"\"}."} +{"id": "01495", "text": "Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.\n\nAmr has n different types of chemicals. Each chemical i has an initial volume of a_{i} liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.\n\nTo do this, Amr can do two different kind of operations. Choose some chemical i and double its current volume so the new volume will be 2a_{i} Choose some chemical i and divide its volume by two (integer division) so the new volume will be $\\lfloor \\frac{a_{i}}{2} \\rfloor$ \n\nSuppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?\n\n\n-----Input-----\n\nThe first line contains one number n (1 \u2264 n \u2264 10^5), the number of chemicals.\n\nThe second line contains n space separated integers a_{i} (1 \u2264 a_{i} \u2264 10^5), representing the initial volume of the i-th chemical in liters.\n\n\n-----Output-----\n\nOutput one integer the minimum number of operations required to make all the chemicals volumes equal.\n\n\n-----Examples-----\nInput\n3\n4 8 2\n\nOutput\n2\nInput\n3\n3 5 6\n\nOutput\n5\n\n\n-----Note-----\n\nIn the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.\n\nIn the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1."} +{"id": "01496", "text": "Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.\n\nNura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.\n\nEach day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days.\n\nHelp Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.\n\n\n-----Input-----\n\nFirst line contains four integers n, m, k, s (1 \u2264 n \u2264 2\u00b710^5, 1 \u2264 k \u2264 m \u2264 2\u00b710^5, 1 \u2264 s \u2264 10^9) \u2014 number of days, total number and required number of gadgets, number of burles Nura has.\n\nSecond line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^6) \u2014 the cost of one dollar in burles on i-th day.\n\nThird line contains n integers b_{i} (1 \u2264 b_{i} \u2264 10^6) \u2014 the cost of one pound in burles on i-th day.\n\nEach of the next m lines contains two integers t_{i}, c_{i} (1 \u2264 t_{i} \u2264 2, 1 \u2264 c_{i} \u2264 10^6) \u2014 type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.\n\n\n-----Output-----\n\nIf Nura can't buy k gadgets print the only line with the number -1.\n\nOtherwise the first line should contain integer d \u2014 the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers q_{i}, d_{i} \u2014 the number of gadget and the day gadget should be bought. All values q_{i} should be different, but the values d_{i} can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n.\n\nIn case there are multiple possible solutions, print any of them.\n\n\n-----Examples-----\nInput\n5 4 2 2\n1 2 3 2 1\n3 2 1 2 3\n1 1\n2 1\n1 2\n2 2\n\nOutput\n3\n1 1\n2 3\n\nInput\n4 3 2 200\n69 70 71 72\n104 105 106 107\n1 1\n2 2\n1 2\n\nOutput\n-1\n\nInput\n4 3 1 1000000000\n900000 910000 940000 990000\n990000 999000 999900 999990\n1 87654\n2 76543\n1 65432\n\nOutput\n-1"} +{"id": "01497", "text": "Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.\n\nReturn the maximum number of rows that she can make completely clean.\n\n\n-----Input-----\n\nThe first line of input will be a single integer n (1 \u2264 n \u2264 100).\n\nThe next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.\n\n\n-----Output-----\n\nThe output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.\n\n\n-----Examples-----\nInput\n4\n0101\n1000\n1111\n0101\n\nOutput\n2\n\nInput\n3\n111\n111\n111\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.\n\nIn the second sample, everything is already clean, so Ohana doesn't need to do anything."} +{"id": "01498", "text": "There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id\u00a0\u2014 integer from 1 to n.\n\nIt is known that during the day q tasks will come, the i-th of them is characterized with three integers: t_{i}\u00a0\u2014 the moment in seconds in which the task will come, k_{i}\u00a0\u2014 the number of servers needed to perform it, and d_{i}\u00a0\u2014 the time needed to perform this task in seconds. All t_{i} are distinct.\n\nTo perform the i-th task you need k_{i} servers which are unoccupied in the second t_{i}. After the servers begin to perform the task, each of them will be busy over the next d_{i} seconds. Thus, they will be busy in seconds t_{i}, t_{i} + 1, ..., t_{i} + d_{i} - 1. For performing the task, k_{i} servers with the smallest ids will be chosen from all the unoccupied servers. If in the second t_{i} there are not enough unoccupied servers, the task is ignored.\n\nWrite the program that determines which tasks will be performed and which will be ignored.\n\n\n-----Input-----\n\nThe first line contains two positive integers n and q (1 \u2264 n \u2264 100, 1 \u2264 q \u2264 10^5) \u2014 the number of servers and the number of tasks. \n\nNext q lines contains three integers each, the i-th line contains integers t_{i}, k_{i} and d_{i} (1 \u2264 t_{i} \u2264 10^6, 1 \u2264 k_{i} \u2264 n, 1 \u2264 d_{i} \u2264 1000)\u00a0\u2014 the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. \n\n\n-----Output-----\n\nPrint q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1.\n\n\n-----Examples-----\nInput\n4 3\n1 3 2\n2 2 1\n3 4 3\n\nOutput\n6\n-1\n10\n\nInput\n3 2\n3 2 3\n5 1 2\n\nOutput\n3\n3\n\nInput\n8 6\n1 3 20\n4 2 1\n6 5 5\n10 1 1\n15 3 6\n21 8 8\n\nOutput\n6\n9\n30\n-1\n15\n36\n\n\n\n-----Note-----\n\nIn the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10).\n\nIn the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task."} +{"id": "01499", "text": "Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4n.\n\nConsider that m (m \u2264 4n) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to m (in the order of their entering the bus). The pattern of the seat occupation is as below:\n\n1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , n-th row left window seat, n-th row right window seat.\n\nAfter occupying all the window seats (for m > 2n) the non-window seats are occupied:\n\n1-st row left non-window seat, 1-st row right non-window seat, ... , n-th row left non-window seat, n-th row right non-window seat.\n\nAll the passengers go to a single final destination. In the final destination, the passengers get off in the given order.\n\n1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , n-th row left non-window seat, n-th row left window seat, n-th row right non-window seat, n-th row right window seat. [Image] The seating for n = 9 and m = 36. \n\nYou are given the values n and m. Output m numbers from 1 to m, the order in which the passengers will get off the bus.\n\n\n-----Input-----\n\nThe only line contains two integers, n and m (1 \u2264 n \u2264 100, 1 \u2264 m \u2264 4n) \u2014 the number of pairs of rows and the number of passengers.\n\n\n-----Output-----\n\nPrint m distinct integers from 1 to m \u2014 the order in which the passengers will get off the bus.\n\n\n-----Examples-----\nInput\n2 7\n\nOutput\n5 1 6 2 7 3 4\n\nInput\n9 36\n\nOutput\n19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18"} +{"id": "01500", "text": "\u041a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0432 \u0442\u0435\u043f\u043b\u0443\u044e \u043f\u043e\u0433\u043e\u0434\u0443 \u043c\u043d\u043e\u0433\u0438\u0435 \u0436\u0438\u0442\u0435\u043b\u0438 \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u0433\u043e\u0440\u043e\u0434\u043e\u0432 \u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438 \u0433\u043e\u0440\u043e\u0434\u0441\u043a\u043e\u0433\u043e \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u0430. \u0412\u043e\u0442 \u0438 \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0448\u043a\u043e\u043b\u044b \u0434\u043e \u0434\u043e\u043c\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0433\u043e\u0440\u043e\u0434\u0441\u043a\u0438\u0435 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u044b.\n\n\u0428\u043a\u043e\u043b\u0430 \u0438 \u0434\u043e\u043c \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u043f\u0440\u044f\u043c\u043e\u0439 \u0443\u043b\u0438\u0446\u0435, \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430 \u0442\u043e\u0439 \u0436\u0435 \u0443\u043b\u0438\u0446\u0435 \u0435\u0441\u0442\u044c n \u0442\u043e\u0447\u0435\u043a, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0432 \u043f\u0440\u043e\u043a\u0430\u0442 \u0438\u043b\u0438 \u0441\u0434\u0430\u0442\u044c \u0435\u0433\u043e. \u041f\u0435\u0440\u0432\u044b\u0439 \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0442\u043e\u0447\u043a\u0435 x_1 \u043a\u0438\u043b\u043e\u043c\u0435\u0442\u0440\u043e\u0432 \u0432\u0434\u043e\u043b\u044c \u0443\u043b\u0438\u0446\u044b, \u0432\u0442\u043e\u0440\u043e\u0439\u00a0\u2014 \u0432 \u0442\u043e\u0447\u043a\u0435 x_2 \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435, n-\u0439 \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0442\u043e\u0447\u043a\u0435 x_{n}. \u0428\u043a\u043e\u043b\u0430 \u0410\u0440\u043a\u0430\u0434\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0442\u043e\u0447\u043a\u0435 x_1 (\u0442\u043e \u0435\u0441\u0442\u044c \u0442\u0430\u043c \u0436\u0435, \u0433\u0434\u0435 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442), \u0430 \u0434\u043e\u043c\u00a0\u2014 \u0432 \u0442\u043e\u0447\u043a\u0435 x_{n} (\u0442\u043e \u0435\u0441\u0442\u044c \u0442\u0430\u043c \u0436\u0435, \u0433\u0434\u0435 \u0438 n-\u0439 \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442). \u0418\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e x_{i} < x_{i} + 1 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 1 \u2264 i < n.\n\n\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u0430, \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u0440\u0430\u0442\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0432 \u043f\u0440\u043e\u043a\u0430\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0442\u043e\u0447\u0435\u043a \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u0430, \u043e\u0434\u043d\u0430\u043a\u043e, \u043e\u043d \u0442\u0443\u0442 \u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u0437\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434, \u0438 \u043e\u0442\u0441\u0447\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u043e\u0439\u0434\u0435\u0442 \u0437\u0430\u043d\u043e\u0432\u043e. \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u0440\u0430\u0442\u044c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0430 \u0432 \u043f\u0440\u043e\u043a\u0430\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. \u0415\u0441\u043b\u0438 \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u0440\u0435\u0448\u0430\u0435\u0442 \u0432\u0437\u044f\u0442\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0432 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0442\u043e\u0447\u043a\u0435 \u043f\u0440\u043e\u043a\u0430\u0442\u0430, \u0442\u043e \u043e\u043d \u0441\u0434\u0430\u0451\u0442 \u0442\u043e\u0442 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d \u0434\u043e \u043d\u0435\u0433\u043e \u0434\u043e\u0435\u0445\u0430\u043b, \u0431\u0435\u0440\u0451\u0442 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u043d\u043e\u0432\u044b\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u043d\u0430 \u043d\u0451\u043c \u0441\u0432\u043e\u0451 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435.\n\n\u0417\u0430 \u043e\u0442\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0430, \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u0443\u0441\u043f\u0435\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0435\u0445\u0430\u0442\u044c \u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 k \u043a\u0438\u043b\u043e\u043c\u0435\u0442\u0440\u043e\u0432 \u0432\u0434\u043e\u043b\u044c \u0443\u043b\u0438\u0446\u044b. \n\n\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u0435, \u0441\u043c\u043e\u0436\u0435\u0442 \u043b\u0438 \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u0434\u043e\u0435\u0445\u0430\u0442\u044c \u043d\u0430 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0430\u0445 \u043e\u0442 \u0448\u043a\u043e\u043b\u044b \u0434\u043e \u0434\u043e\u043c\u0430, \u0438 \u0435\u0441\u043b\u0438 \u0434\u0430, \u0442\u043e \u043a\u0430\u043a\u043e\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437 \u0435\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u044f\u0442\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0432 \u043f\u0440\u043e\u043a\u0430\u0442, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434? \u0423\u0447\u0442\u0438\u0442\u0435, \u0447\u0442\u043e \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u043d\u0435 \u043d\u0430\u043c\u0435\u0440\u0435\u043d \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0445\u043e\u0434\u0438\u0442\u044c \u043f\u0435\u0448\u043a\u043e\u043c.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 n \u0438 k (2 \u2264 n \u2264 1 000, 1 \u2264 k \u2264 100 000) \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u043e\u0432 \u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0435\u0445\u0430\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0435.\n\n\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b x_1, x_2, ..., x_{n} (0 \u2264 x_1 < x_2 < ... < x_{n} \u2264 100 000) \u2014 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u0442\u043e\u0447\u0435\u043a, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u044b. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u043e\u0432 \u0437\u0430\u0434\u0430\u043d\u044b \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044f.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0415\u0441\u043b\u0438 \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0448\u043a\u043e\u043b\u044b \u0434\u043e \u0434\u043e\u043c\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0430\u0445, \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 -1. \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0410\u0440\u043a\u0430\u0434\u0438\u044e \u043d\u0443\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0432 \u0442\u043e\u0447\u043a\u0430\u0445 \u043f\u0440\u043e\u043a\u0430\u0442\u0430.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n4 4\n3 6 8 10\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2 9\n10 20\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n-1\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n12 3\n4 6 7 9 10 11 13 15 17 18 20 21\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n6\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u0437\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u0435 \u0438 \u0434\u043e\u0435\u0445\u0430\u0442\u044c \u043d\u0430 \u043d\u0451\u043c \u0434\u043e \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u0430. \u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u0435 \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u0437\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d \u0441\u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u043e\u0433\u043e \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u0430, \u0440\u044f\u0434\u043e\u043c \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0435\u0433\u043e \u0434\u043e\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0410\u0440\u043a\u0430\u0434\u0438\u044e \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0430 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0448\u043a\u043e\u043b\u044b \u0434\u043e \u0434\u043e\u043c\u0430.\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0430 \u0432\u0435\u043b\u043e\u043f\u0440\u043e\u043a\u0430\u0442\u0430, \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 10. \u041d\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0435\u0445\u0430\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0435, \u0440\u0430\u0432\u043d\u043e 9. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0410\u0440\u043a\u0430\u0434\u0438\u0439 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0448\u043a\u043e\u043b\u044b \u0434\u043e \u0434\u043e\u043c\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0430\u0445."} +{"id": "01501", "text": "Tavas is a strange creature. Usually \"zzz\" comes out of people's mouth while sleeping, but string s of length n comes out from Tavas' mouth instead. [Image] \n\nToday Tavas fell asleep in Malekas' place. While he was sleeping, Malekas did a little process on s. Malekas has a favorite string p. He determined all positions x_1 < x_2 < ... < x_{k} where p matches s. More formally, for each x_{i} (1 \u2264 i \u2264 k) he condition s_{x}_{i}s_{x}_{i} + 1... s_{x}_{i} + |p| - 1 = p is fullfilled.\n\nThen Malekas wrote down one of subsequences of x_1, x_2, ... x_{k} (possibly, he didn't write anything) on a piece of paper. Here a sequence b is a subsequence of sequence a if and only if we can turn a into b by removing some of its elements (maybe no one of them or all).\n\nAfter Tavas woke up, Malekas told him everything. He couldn't remember string s, but he knew that both p and s only contains lowercase English letters and also he had the subsequence he had written on that piece of paper.\n\nTavas wonders, what is the number of possible values of s? He asked SaDDas, but he wasn't smart enough to solve this. So, Tavas asked you to calculate this number for him.\n\nAnswer can be very large, so Tavas wants you to print the answer modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line contains two integers n and m, the length of s and the length of the subsequence Malekas wrote down (1 \u2264 n \u2264 10^6 and 0 \u2264 m \u2264 n - |p| + 1).\n\nThe second line contains string p (1 \u2264 |p| \u2264 n).\n\nThe next line contains m space separated integers y_1, y_2, ..., y_{m}, Malekas' subsequence (1 \u2264 y_1 < y_2 < ... < y_{m} \u2264 n - |p| + 1).\n\n\n-----Output-----\n\nIn a single line print the answer modulo 1000 000 007.\n\n\n-----Examples-----\nInput\n6 2\nioi\n1 3\n\nOutput\n26\n\nInput\n5 2\nioi\n1 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample test all strings of form \"ioioi?\" where the question mark replaces arbitrary English letter satisfy.\n\nHere |x| denotes the length of string x.\n\nPlease note that it's possible that there is no such string (answer is 0)."} +{"id": "01502", "text": "[Image] \n\n\n-----Input-----\n\nThe input contains a single integer $a$ ($0 \\le a \\le 15$).\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Example-----\nInput\n3\n\nOutput\n13"} +{"id": "01503", "text": "Acingel is a small town. There was only one doctor here\u00a0\u2014 Miss Ada. She was very friendly and nobody has ever said something bad about her, so who could've expected that Ada will be found dead in her house? Mr Gawry, world-famous detective, is appointed to find the criminal. He asked $m$ neighbours of Ada about clients who have visited her in that unlucky day. Let's number the clients from $1$ to $n$. Each neighbour's testimony is a permutation of these numbers, which describes the order in which clients have been seen by the asked neighbour.\n\nHowever, some facts are very suspicious\u00a0\u2013 how it is that, according to some of given permutations, some client has been seen in the morning, while in others he has been seen in the evening? \"In the morning some of neighbours must have been sleeping!\"\u00a0\u2014 thinks Gawry\u00a0\u2014 \"and in the evening there's been too dark to see somebody's face...\". Now he wants to delete some prefix and some suffix (both prefix and suffix can be empty) in each permutation, so that they'll be non-empty and equal to each other after that\u00a0\u2014 some of the potential criminals may disappear, but the testimony won't stand in contradiction to each other.\n\nIn how many ways he can do it? Two ways are called different if the remaining common part is different.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 100\\,000$, $1 \\le m \\le 10$)\u00a0\u2014 the number of suspects and the number of asked neighbors.\n\nEach of the next $m$ lines contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le n$). It is guaranteed that these integers form a correct permutation (that is, each number from $1$ to $n$ appears exactly once).\n\n\n-----Output-----\n\nOutput a single integer denoting the number of ways to delete some prefix and some suffix of each permutation (possibly empty), such that the remaining parts will be equal and non-empty.\n\n\n-----Examples-----\nInput\n3 2\n1 2 3\n2 3 1\n\nOutput\n4\n\nInput\n5 6\n1 2 3 4 5\n2 3 1 4 5\n3 4 5 1 2\n3 5 4 2 1\n2 3 5 4 1\n1 2 3 4 5\n\nOutput\n5\n\nInput\n2 2\n1 2\n2 1\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example, all possible common parts are $[1]$, $[2]$, $[3]$ and $[2, 3]$.\n\nIn the second and third examples, you can only leave common parts of length $1$."} +{"id": "01504", "text": "You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \\dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \\dots, [bl_n, br_n]$.\n\nInitially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$.\n\nIn one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$.\n\nLet's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\\sum\\limits_{i=1}^{n}{\\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$.\n\nWhat is the minimum number of steps you need to make $I$ greater or equal to $k$?\n\n\n-----Input-----\n\nThe first line contains the single integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains two integers $n$ and $k$ ($1 \\le n \\le 2 \\cdot 10^5$; $1 \\le k \\le 10^9$)\u00a0\u2014 the length of lists and the minimum required total intersection.\n\nThe second line of each test case contains two integers $l_1$ and $r_1$ ($1 \\le l_1 \\le r_1 \\le 10^9$)\u00a0\u2014 the segment all $[al_i, ar_i]$ are equal to initially.\n\nThe third line of each test case contains two integers $l_2$ and $r_2$ ($1 \\le l_2 \\le r_2 \\le 10^9$)\u00a0\u2014 the segment all $[bl_i, br_i]$ are equal to initially.\n\nIt's guaranteed that the sum of $n$ doesn't exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nPrint $t$ integers\u00a0\u2014 one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$.\n\n\n-----Example-----\nInput\n3\n3 5\n1 2\n3 4\n2 1000000000\n1 1\n999999999 999999999\n10 3\n5 10\n7 8\n\nOutput\n7\n2000000000\n0\n\n\n\n-----Note-----\n\nIn the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \\text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \\text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\\\ + \\text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$\n\nIn the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps.\n\nIn the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps."} +{"id": "01505", "text": "Petya has recently started working as a programmer in the IT city company that develops computer games.\n\nBesides game mechanics implementation to create a game it is necessary to create tool programs that can be used by game designers to create game levels. Petya's first assignment is to create a tool that allows to paint different arrows on the screen.\n\nA user of this tool will choose a point on the screen, specify a vector (the arrow direction) and vary several parameters to get the required graphical effect. In the first version of the program Petya decided to limit parameters of the arrow by the following: a point with coordinates (px, py), a nonzero vector with coordinates (vx, vy), positive scalars a, b, c, d, a > c.\n\nThe produced arrow should have the following properties. The arrow consists of a triangle and a rectangle. The triangle is isosceles with base of length a and altitude of length b perpendicular to the base. The rectangle sides lengths are c and d. Point (px, py) is situated in the middle of the triangle base and in the middle of side of rectangle that has length c. Area of intersection of the triangle and the rectangle is zero. The direction from (px, py) point to the triangle vertex opposite to base containing the point coincides with direction of (vx, vy) vector.\n\nEnumerate the arrow points coordinates in counter-clockwise order starting from the tip.\n\n [Image] \n\n\n-----Input-----\n\nThe only line of the input contains eight integers px, py, vx, vy ( - 1000 \u2264 px, py, vx, vy \u2264 1000, vx^2 + vy^2 > 0), a, b, c, d (1 \u2264 a, b, c, d \u2264 1000, a > c).\n\n\n-----Output-----\n\nOutput coordinates of the arrow points in counter-clockwise order. Each line should contain two coordinates, first x, then y. Relative or absolute error should not be greater than 10^{ - 9}.\n\n\n-----Examples-----\nInput\n8 8 0 2 8 3 4 5\n\nOutput\n8.000000000000 11.000000000000\n4.000000000000 8.000000000000\n6.000000000000 8.000000000000\n6.000000000000 3.000000000000\n10.000000000000 3.000000000000\n10.000000000000 8.000000000000\n12.000000000000 8.000000000000"} +{"id": "01506", "text": "You are given an array a of length n. We define f_{a} the following way:\n\n Initially f_{a} = 0, M = 1; for every 2 \u2264 i \u2264 n if a_{M} < a_{i} then we set f_{a} = f_{a} + a_{M} and then set M = i. \n\nCalculate the sum of f_{a} over all n! permutations of the array a modulo 10^9 + 7.\n\nNote: two elements are considered different if their indices differ, so for every array a there are exactly n! permutations.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 1 000 000) \u2014 the size of array a.\n\nSecond line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint the only integer, the sum of f_{a} over all n! permutations of the array a modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n2\n1 3\n\nOutput\n1\nInput\n3\n1 1 2\n\nOutput\n4\n\n\n-----Note-----\n\nFor the second example all the permutations are:\n\n p = [1, 2, 3] : f_{a} is equal to 1; p = [1, 3, 2] : f_{a} is equal to 1; p = [2, 1, 3] : f_{a} is equal to 1; p = [2, 3, 1] : f_{a} is equal to 1; p = [3, 1, 2] : f_{a} is equal to 0; p = [3, 2, 1] : f_{a} is equal to 0. \n\nWhere p is the array of the indices of initial array a. The sum of f_{a} is equal to 4."} +{"id": "01507", "text": "[Image] \n\nIt's the end of July\u00a0\u2013 the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in the kingdom of Sweetland in case it turns out to reach the wrong hands. So it's a necessity to not let any uninvited guests in.\n\nThere are 26 entrances in Jelly Castle, enumerated with uppercase English letters from A to Z. Because of security measures, each guest is known to be assigned an entrance he should enter the castle through. The door of each entrance is opened right before the first guest's arrival and closed right after the arrival of the last guest that should enter the castle through this entrance. No two guests can enter the castle simultaneously.\n\nFor an entrance to be protected from possible intrusion, a candy guard should be assigned to it. There are k such guards in the castle, so if there are more than k opened doors, one of them is going to be left unguarded! Notice that a guard can't leave his post until the door he is assigned to is closed.\n\nSlastyona had a suspicion that there could be uninvited guests at the evening. She knows the order in which the invited guests entered the castle, and wants you to help her check whether there was a moment when more than k doors were opened.\n\n\n-----Input-----\n\nTwo integers are given in the first string: the number of guests n and the number of guards k (1 \u2264 n \u2264 10^6, 1 \u2264 k \u2264 26).\n\nIn the second string, n uppercase English letters s_1s_2... s_{n} are given, where s_{i} is the entrance used by the i-th guest.\n\n\n-----Output-----\n\nOutput \u00abYES\u00bb if at least one door was unguarded during some time, and \u00abNO\u00bb otherwise.\n\nYou can output each letter in arbitrary case (upper or lower).\n\n\n-----Examples-----\nInput\n5 1\nAABBB\n\nOutput\nNO\n\nInput\n5 1\nABABB\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample case, the door A is opened right before the first guest's arrival and closed when the second guest enters the castle. The door B is opened right before the arrival of the third guest, and closed after the fifth one arrives. One guard can handle both doors, as the first one is closed before the second one is opened.\n\nIn the second sample case, the door B is opened before the second guest's arrival, but the only guard can't leave the door A unattended, as there is still one more guest that should enter the castle through this door."} +{"id": "01508", "text": "You want to arrange n integers a_1, a_2, ..., a_{n} in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers.\n\nMore formally, let's denote some arrangement as a sequence of integers x_1, x_2, ..., x_{n}, where sequence x is a permutation of sequence a. The value of such an arrangement is (x_1 - x_2) + (x_2 - x_3) + ... + (x_{n} - 1 - x_{n}).\n\nFind the largest possible value of an arrangement. Then, output the lexicographically smallest sequence x that corresponds to an arrangement of the largest possible value.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (2 \u2264 n \u2264 100). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (|a_{i}| \u2264 1000).\n\n\n-----Output-----\n\nPrint the required sequence x_1, x_2, ..., x_{n}. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.\n\n\n-----Examples-----\nInput\n5\n100 -100 50 0 -50\n\nOutput\n100 -50 0 50 -100 \n\n\n\n-----Note-----\n\nIn the sample test case, the value of the output arrangement is (100 - ( - 50)) + (( - 50) - 0) + (0 - 50) + (50 - ( - 100)) = 200. No other arrangement has a larger value, and among all arrangements with the value of 200, the output arrangement is the lexicographically smallest one.\n\nSequence x_1, x_2, ... , x_{p} is lexicographically smaller than sequence y_1, y_2, ... , y_{p} if there exists an integer r (0 \u2264 r < p) such that x_1 = y_1, x_2 = y_2, ... , x_{r} = y_{r} and x_{r} + 1 < y_{r} + 1."} +{"id": "01509", "text": "The Kingdom of Kremland is a tree (a connected undirected graph without cycles) consisting of $n$ vertices. Each vertex $i$ has its own value $a_i$. All vertices are connected in series by edges. Formally, for every $1 \\leq i < n$ there is an edge between the vertices of $i$ and $i+1$.\n\nDenote the function $f(l, r)$, which takes two integers $l$ and $r$ ($l \\leq r$):\n\n \u00a0\u00a0 We leave in the tree only vertices whose values \u200b\u200brange from $l$ to $r$. \u00a0\u00a0 The value of the function will be the number of connected components in the new graph. \n\nYour task is to calculate the following sum: $$\\sum_{l=1}^{n} \\sum_{r=l}^{n} f(l, r) $$\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^5$)\u00a0\u2014 the number of vertices in the tree.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq n$)\u00a0\u2014 the values of the vertices.\n\n\n-----Output-----\n\nPrint one number\u00a0\u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n3\n2 1 3\n\nOutput\n7\nInput\n4\n2 1 1 3\n\nOutput\n11\nInput\n10\n1 5 2 5 5 3 10 6 5 1\n\nOutput\n104\n\n\n-----Note-----\n\nIn the first example, the function values \u200b\u200bwill be as follows: $f(1, 1)=1$ (there is only a vertex with the number $2$, which forms one component) $f(1, 2)=1$ (there are vertices $1$ and $2$ that form one component) $f(1, 3)=1$ (all vertices remain, one component is obtained) $f(2, 2)=1$ (only vertex number $1$) $f(2, 3)=2$ (there are vertices $1$ and $3$ that form two components) $f(3, 3)=1$ (only vertex $3$) Totally out $7$.\n\nIn the second example, the function values \u200b\u200bwill be as follows: $f(1, 1)=1$ $f(1, 2)=1$ $f(1, 3)=1$ $f(1, 4)=1$ $f(2, 2)=1$ $f(2, 3)=2$ $f(2, 4)=2$ $f(3, 3)=1$ $f(3, 4)=1$ $f(4, 4)=0$ (there is no vertex left, so the number of components is $0$) Totally out $11$."} +{"id": "01510", "text": "Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays a and b by their father. The array a is given to Devu and b to his brother. \n\nAs Devu is really a naughty kid, he wants the minimum value of his array a should be at least as much as the maximum value of his brother's array b. \n\nNow you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times.\n\nYou need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting. \n\n\n-----Input-----\n\nThe first line contains two space-separated integers n, m (1 \u2264 n, m \u2264 10^5). The second line will contain n space-separated integers representing content of the array a (1 \u2264 a_{i} \u2264 10^9). The third line will contain m space-separated integers representing content of the array b (1 \u2264 b_{i} \u2264 10^9).\n\n\n-----Output-----\n\nYou need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.\n\n\n-----Examples-----\nInput\n2 2\n2 3\n3 5\n\nOutput\n3\n\nInput\n3 2\n1 2 3\n3 4\n\nOutput\n4\n\nInput\n3 2\n4 5 6\n1 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn example 1, you can increase a_1 by 1 and decrease b_2 by 1 and then again decrease b_2 by 1. Now array a will be [3; 3] and array b will also be [3; 3]. Here minimum element of a is at least as large as maximum element of b. So minimum number of operations needed to satisfy Devu's condition are 3.\n\nIn example 3, you don't need to do any operation, Devu's condition is already satisfied."} +{"id": "01511", "text": "The research center Q has developed a new multi-core processor. The processor consists of n cores and has k cells of cache memory. Consider the work of this processor.\n\nAt each cycle each core of the processor gets one instruction: either do nothing, or the number of the memory cell (the core will write an information to the cell). After receiving the command, the core executes it immediately. Sometimes it happens that at one cycle, multiple cores try to write the information into a single cell. Unfortunately, the developers did not foresee the possibility of resolving conflicts between cores, so in this case there is a deadlock: all these cores and the corresponding memory cell are locked forever. Each of the locked cores ignores all further commands, and no core in the future will be able to record an information into the locked cell. If any of the cores tries to write an information into some locked cell, it is immediately locked.\n\nThe development team wants to explore the deadlock situation. Therefore, they need a program that will simulate the processor for a given set of instructions for each core within m cycles . You're lucky, this interesting work is entrusted to you. According to the instructions, during the m cycles define for each core the number of the cycle, during which it will become locked. It is believed that initially all cores and all memory cells are not locked.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, k (1 \u2264 n, m, k \u2264 100). Then follow n lines describing instructions. The i-th line contains m integers: x_{i}1, x_{i}2, ..., x_{im} (0 \u2264 x_{ij} \u2264 k), where x_{ij} is the instruction that must be executed by the i-th core at the j-th cycle. If x_{ij} equals 0, then the corresponding instruction is \u00abdo nothing\u00bb. But if x_{ij} is a number from 1 to k, then the corresponding instruction is \u00abwrite information to the memory cell number x_{ij}\u00bb.\n\nWe assume that the cores are numbered from 1 to n, the work cycles are numbered from 1 to m and the memory cells are numbered from 1 to k.\n\n\n-----Output-----\n\nPrint n lines. In the i-th line print integer t_{i}. This number should be equal to 0 if the i-th core won't be locked, or it should be equal to the number of the cycle when this core will be locked.\n\n\n-----Examples-----\nInput\n4 3 5\n1 0 0\n1 0 2\n2 3 1\n3 2 0\n\nOutput\n1\n1\n3\n0\n\nInput\n3 2 2\n1 2\n1 2\n2 2\n\nOutput\n1\n1\n0\n\nInput\n1 1 1\n0\n\nOutput\n0"} +{"id": "01512", "text": "You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible.\n\nWe remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 \u2264 j < i) the following holds: a_{j} < a_{i}. \n\n\n-----Input-----\n\nThe first line contains the only integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the length of the permutation.\n\nThe second line contains n integers p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 n)\u00a0\u2014 the permutation. All the integers are distinct.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one.\n\n\n-----Examples-----\nInput\n1\n1\n\nOutput\n1\n\nInput\n5\n5 1 2 3 4\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example the only element can be removed."} +{"id": "01513", "text": "You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired.\n\nYou have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \\ldots, s+t-1$.\n\nYou are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap.\n\nTime is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces?\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $k$ ($1 \\le n \\le 10^5$, $n \\le m \\le 10^9$, $1 \\le k \\le n$)\u00a0\u2014 the number of broken segments, the length of the stick and the maximum number of pieces you can use.\n\nThe second line contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($1 \\le b_i \\le m$)\u00a0\u2014 the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \\ldots < b_n$.\n\n\n-----Output-----\n\nPrint the minimum total length of the pieces.\n\n\n-----Examples-----\nInput\n4 100 2\n20 30 75 80\n\nOutput\n17\n\nInput\n5 100 3\n1 2 4 60 87\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$.\n\nIn the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$."} +{"id": "01514", "text": "A permutation of length $k$ is a sequence of $k$ integers from $1$ to $k$ containing each integer exactly once. For example, the sequence $[3, 1, 2]$ is a permutation of length $3$.\n\nWhen Neko was five, he thought of an array $a$ of $n$ positive integers and a permutation $p$ of length $n - 1$. Then, he performed the following:\n\n Constructed an array $b$ of length $n-1$, where $b_i = \\min(a_i, a_{i+1})$. Constructed an array $c$ of length $n-1$, where $c_i = \\max(a_i, a_{i+1})$. Constructed an array $b'$ of length $n-1$, where $b'_i = b_{p_i}$. Constructed an array $c'$ of length $n-1$, where $c'_i = c_{p_i}$. \n\nFor example, if the array $a$ was $[3, 4, 6, 5, 7]$ and permutation $p$ was $[2, 4, 1, 3]$, then Neko would have constructed the following arrays:\n\n $b = [3, 4, 5, 5]$ $c = [4, 6, 6, 7]$ $b' = [4, 5, 3, 5]$ $c' = [6, 7, 4, 6]$ \n\nThen, he wrote two arrays $b'$ and $c'$ on a piece of paper and forgot about it. 14 years later, when he was cleaning up his room, he discovered this old piece of paper with two arrays $b'$ and $c'$ written on it. However he can't remember the array $a$ and permutation $p$ he used.\n\nIn case Neko made a mistake and there is no array $a$ and permutation $p$ resulting in such $b'$ and $c'$, print -1. Otherwise, help him recover any possible array $a$. \n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($2 \\leq n \\leq 10^5$)\u00a0\u2014 the number of elements in array $a$.\n\nThe second line contains $n-1$ integers $b'_1, b'_2, \\ldots, b'_{n-1}$ ($1 \\leq b'_i \\leq 10^9$).\n\nThe third line contains $n-1$ integers $c'_1, c'_2, \\ldots, c'_{n-1}$ ($1 \\leq c'_i \\leq 10^9$).\n\n\n-----Output-----\n\nIf Neko made a mistake and there is no array $a$ and a permutation $p$ leading to the $b'$ and $c'$, print -1. Otherwise, print $n$ positive integers $a_i$ ($1 \\le a_i \\le 10^9$), denoting the elements of the array $a$.\n\nIf there are multiple possible solutions, print any of them. \n\n\n-----Examples-----\nInput\n5\n4 5 3 5\n6 7 4 6\n\nOutput\n3 4 6 5 7 \n\nInput\n3\n2 4\n3 2\n\nOutput\n-1\n\nInput\n8\n2 3 1 1 2 4 3\n3 4 4 2 5 5 4\n\nOutput\n3 4 5 2 1 4 3 2 \n\n\n\n-----Note-----\n\nThe first example is explained is the problem statement.\n\nIn the third example, for $a = [3, 4, 5, 2, 1, 4, 3, 2]$, a possible permutation $p$ is $[7, 1, 5, 4, 3, 2, 6]$. In that case, Neko would have constructed the following arrays:\n\n $b = [3, 4, 2, 1, 1, 3, 2]$ $c = [4, 5, 5, 2, 4, 4, 3]$ $b' = [2, 3, 1, 1, 2, 4, 3]$ $c' = [3, 4, 4, 2, 5, 5, 4]$"} +{"id": "01515", "text": "Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a \"very beautiful number\". But the problem is, he's left his paper with the number in the teachers' office.\n\nThe teacher remembers that the \"very beautiful number\" was strictly positive, didn't contain any leading zeroes, had the length of exactly p decimal digits, and if we move the last digit of the number to the beginning, it grows exactly x times. Besides, the teacher is sure that among all such numbers the \"very beautiful number\" is minimal possible.\n\nThe teachers' office isn't near and the teacher isn't young. But we've passed the test and we deserved the right to see the \"very beautiful number\". Help to restore the justice, find the \"very beautiful number\" for us!\n\n\n-----Input-----\n\nThe single line contains integers p, x (1 \u2264 p \u2264 10^6, 1 \u2264 x \u2264 9).\n\n\n-----Output-----\n\nIf the teacher's made a mistake and such number doesn't exist, then print on a single line \"Impossible\" (without the quotes). Otherwise, print the \"very beautiful number\" without leading zeroes.\n\n\n-----Examples-----\nInput\n6 5\n\nOutput\n142857\nInput\n1 2\n\nOutput\nImpossible\n\nInput\n6 4\n\nOutput\n102564\n\n\n-----Note-----\n\nSample 1: 142857\u00b75 = 714285.\n\nSample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is \"Impossible\"."} +{"id": "01516", "text": "This problem differs from the next one only in the presence of the constraint on the equal length of all numbers $a_1, a_2, \\dots, a_n$. Actually, this problem is a subtask of the problem D2 from the same contest and the solution of D2 solves this subtask too.\n\nA team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don't know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem.\n\nLet's denote a function that alternates digits of two numbers $f(a_1 a_2 \\dots a_{p - 1} a_p, b_1 b_2 \\dots b_{q - 1} b_q)$, where $a_1 \\dots a_p$ and $b_1 \\dots b_q$ are digits of two integers written in the decimal notation without leading zeros.\n\nIn other words, the function $f(x, y)$ alternately shuffles the digits of the numbers $x$ and $y$ by writing them from the lowest digits to the older ones, starting with the number $y$. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below.\n\nFor example: $$f(1111, 2222) = 12121212$$ $$f(7777, 888) = 7787878$$ $$f(33, 44444) = 4443434$$ $$f(555, 6) = 5556$$ $$f(111, 2222) = 2121212$$\n\nFormally, if $p \\ge q$ then $f(a_1 \\dots a_p, b_1 \\dots b_q) = a_1 a_2 \\dots a_{p - q + 1} b_1 a_{p - q + 2} b_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$; if $p < q$ then $f(a_1 \\dots a_p, b_1 \\dots b_q) = b_1 b_2 \\dots b_{q - p} a_1 b_{q - p + 1} a_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$. \n\nMishanya gives you an array consisting of $n$ integers $a_i$. All numbers in this array are of equal length (that is, they consist of the same number of digits). Your task is to help students to calculate $\\sum_{i = 1}^{n}\\sum_{j = 1}^{n} f(a_i, a_j)$ modulo $998\\,244\\,353$.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($1 \\le n \\le 100\\,000$) \u2014 the number of elements in the array. The second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) \u2014 the elements of the array. All numbers $a_1, a_2, \\dots, a_n$ are of equal length (that is, they consist of the same number of digits).\n\n\n-----Output-----\n\nPrint the answer modulo $998\\,244\\,353$.\n\n\n-----Examples-----\nInput\n3\n12 33 45\n\nOutput\n26730\nInput\n2\n123 456\n\nOutput\n1115598\nInput\n1\n1\n\nOutput\n11\nInput\n5\n1000000000 1000000000 1000000000 1000000000 1000000000\n\nOutput\n265359409"} +{"id": "01517", "text": "Permutation p is an ordered set of integers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as p_{i}. We'll call number n the size or the length of permutation p_1, p_2, ..., p_{n}.\n\nPetya decided to introduce the sum operation on the set of permutations of length n. Let's assume that we are given two permutations of length n: a_1, a_2, ..., a_{n} and b_1, b_2, ..., b_{n}. Petya calls the sum of permutations a and b such permutation c of length n, where c_{i} = ((a_{i} - 1 + b_{i} - 1) mod n) + 1 (1 \u2264 i \u2264 n).\n\nOperation $x \\text{mod} y$ means taking the remainder after dividing number x by number y.\n\nObviously, not for all permutations a and b exists permutation c that is sum of a and b. That's why Petya got sad and asked you to do the following: given n, count the number of such pairs of permutations a and b of length n, that exists permutation c that is sum of a and b. The pair of permutations x, y (x \u2260 y) and the pair of permutations y, x are considered distinct pairs.\n\nAs the answer can be rather large, print the remainder after dividing it by 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe single line contains integer n (1 \u2264 n \u2264 16).\n\n\n-----Output-----\n\nIn the single line print a single non-negative integer \u2014 the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n3\n\nOutput\n18\n\nInput\n5\n\nOutput\n1800"} +{"id": "01518", "text": "You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times):\n\n choose two indexes, i and j (1 \u2264 i < j \u2264 n; (j - i + 1) is a prime number); swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). \n\nYou do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 \u2264 a[i] \u2264 n).\n\n\n-----Output-----\n\nIn the first line, print integer k (0 \u2264 k \u2264 5n) \u2014 the number of used operations. Next, print the operations. Each operation must be printed as \"i j\" (1 \u2264 i < j \u2264 n; (j - i + 1) is a prime).\n\nIf there are multiple answers, you can print any of them.\n\n\n-----Examples-----\nInput\n3\n3 2 1\n\nOutput\n1\n1 3\n\nInput\n2\n1 2\n\nOutput\n0\n\nInput\n4\n4 2 3 1\n\nOutput\n3\n2 4\n1 2\n2 4"} +{"id": "01519", "text": "Vasya has recently got a job as a cashier at a local store. His day at work is $L$ minutes long. Vasya has already memorized $n$ regular customers, the $i$-th of which comes after $t_{i}$ minutes after the beginning of the day, and his service consumes $l_{i}$ minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. \n\nVasya is a bit lazy, so he likes taking smoke breaks for $a$ minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day?\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $L$ and $a$ ($0 \\le n \\le 10^{5}$, $1 \\le L \\le 10^{9}$, $1 \\le a \\le L$).\n\nThe $i$-th of the next $n$ lines contains two integers $t_{i}$ and $l_{i}$ ($0 \\le t_{i} \\le L - 1$, $1 \\le l_{i} \\le L$). It is guaranteed that $t_{i} + l_{i} \\le t_{i + 1}$ and $t_{n} + l_{n} \\le L$.\n\n\n-----Output-----\n\nOutput one integer \u00a0\u2014 the maximum number of breaks.\n\n\n-----Examples-----\nInput\n2 11 3\n0 1\n1 1\n\nOutput\n3\nInput\n0 5 2\n\nOutput\n2\nInput\n1 3 2\n1 2\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first sample Vasya can take $3$ breaks starting after $2$, $5$ and $8$ minutes after the beginning of the day.\n\nIn the second sample Vasya can take $2$ breaks starting after $0$ and $2$ minutes after the beginning of the day.\n\nIn the third sample Vasya can't take any breaks."} +{"id": "01520", "text": "Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza's recipe, while Denis invented a string multiplication. According to Denis, the result of multiplication (product) of strings $s$ of length $m$ and $t$ is a string $t + s_1 + t + s_2 + \\ldots + t + s_m + t$, where $s_i$ denotes the $i$-th symbol of the string $s$, and \"+\" denotes string concatenation. For example, the product of strings \"abc\" and \"de\" is a string \"deadebdecde\", while the product of the strings \"ab\" and \"z\" is a string \"zazbz\". Note, that unlike the numbers multiplication, the product of strings $s$ and $t$ is not necessarily equal to product of $t$ and $s$.\n\nRoman was jealous of Denis, since he invented such a cool operation, and hence decided to invent something string-related too. Since Roman is beauty-lover, he decided to define the beauty of the string as the length of the longest substring, consisting of only one letter. For example, the beauty of the string \"xayyaaabca\" is equal to $3$, since there is a substring \"aaa\", while the beauty of the string \"qwerqwer\" is equal to $1$, since all neighboring symbols in it are different.\n\nIn order to entertain Roman, Denis wrote down $n$ strings $p_1, p_2, p_3, \\ldots, p_n$ on the paper and asked him to calculate the beauty of the string $( \\ldots (((p_1 \\cdot p_2) \\cdot p_3) \\cdot \\ldots ) \\cdot p_n$, where $s \\cdot t$ denotes a multiplication of strings $s$ and $t$. Roman hasn't fully realized how Denis's multiplication works, so he asked you for a help. Denis knows, that Roman is very impressionable, he guarantees, that the beauty of the resulting string is at most $10^9$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\leq n \\leq 100\\,000$)\u00a0\u2014 the number of strings, wroted by Denis.\n\nNext $n$ lines contain non-empty strings $p_1, p_2, \\ldots, p_n$, consisting of lowercase english letters.\n\nIt's guaranteed, that the total length of the strings $p_i$ is at most $100\\,000$, and that's the beauty of the resulting product is at most $10^9$.\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the beauty of the product of the strings.\n\n\n-----Examples-----\nInput\n3\na\nb\na\n\nOutput\n3\n\nInput\n2\nbnn\na\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, the product of strings is equal to \"abaaaba\".\n\nIn the second example, the product of strings is equal to \"abanana\"."} +{"id": "01521", "text": "DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number x_{i}, DZY will put it into the bucket numbered h(x_{i}), where h(x) is the hash function. In this problem we will assume, that h(x) = x\u00a0mod\u00a0p. Operation a\u00a0mod\u00a0b denotes taking a remainder after division a by b.\n\nHowever, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a \"conflict\" happens. Suppose the first conflict happens right after the i-th insertion, you should output i. If no conflict happens, just output -1.\n\n\n-----Input-----\n\nThe first line contains two integers, p and n (2 \u2264 p, n \u2264 300). Then n lines follow. The i-th of them contains an integer x_{i} (0 \u2264 x_{i} \u2264 10^9).\n\n\n-----Output-----\n\nOutput a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n10 5\n0\n21\n53\n41\n53\n\nOutput\n4\n\nInput\n5 5\n0\n1\n2\n3\n4\n\nOutput\n-1"} +{"id": "01522", "text": "After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that simple. Vitaly is in the first room of the house with n room located in a line and numbered starting from one from left to right. You can go from the first room to the second room, from the second room to the third room and so on \u2014 you can go from the (n - 1)-th room to the n-th room. Thus, you can go to room x only from room x - 1.\n\nThe potato pie is located in the n-th room and Vitaly needs to go there. \n\nEach pair of consecutive rooms has a door between them. In order to go to room x from room x - 1, you need to open the door between the rooms with the corresponding key. \n\nIn total the house has several types of doors (represented by uppercase Latin letters) and several types of keys (represented by lowercase Latin letters). The key of type t can open the door of type T if and only if t and T are the same letter, written in different cases. For example, key f can open door F.\n\nEach of the first n - 1 rooms contains exactly one key of some type that Vitaly can use to get to next rooms. Once the door is open with some key, Vitaly won't get the key from the keyhole but he will immediately run into the next room. In other words, each key can open no more than one door.\n\nVitaly realizes that he may end up in some room without the key that opens the door to the next room. Before the start his run for the potato pie Vitaly can buy any number of keys of any type that is guaranteed to get to room n.\n\nGiven the plan of the house, Vitaly wants to know what is the minimum number of keys he needs to buy to surely get to the room n, which has a delicious potato pie. Write a program that will help Vitaly find out this number.\n\n\n-----Input-----\n\nThe first line of the input contains a positive integer n (2 \u2264 n \u2264 10^5)\u00a0\u2014\u00a0the number of rooms in the house.\n\nThe second line of the input contains string s of length 2\u00b7n - 2. Let's number the elements of the string from left to right, starting from one. \n\nThe odd positions in the given string s contain lowercase Latin letters\u00a0\u2014\u00a0the types of the keys that lie in the corresponding rooms. Thus, each odd position i of the given string s contains a lowercase Latin letter \u2014 the type of the key that lies in room number (i + 1) / 2.\n\nThe even positions in the given string contain uppercase Latin letters \u2014 the types of doors between the rooms. Thus, each even position i of the given string s contains an uppercase letter \u2014 the type of the door that leads from room i / 2 to room i / 2 + 1.\n\n\n-----Output-----\n\nPrint the only integer \u2014 the minimum number of keys that Vitaly needs to buy to surely get from room one to room n.\n\n\n-----Examples-----\nInput\n3\naAbB\n\nOutput\n0\n\nInput\n4\naBaCaB\n\nOutput\n3\n\nInput\n5\nxYyXzZaZ\n\nOutput\n2"} +{"id": "01523", "text": "The kingdom of Lazyland is the home to $n$ idlers. These idlers are incredibly lazy and create many problems to their ruler, the mighty King of Lazyland. \n\nToday $k$ important jobs for the kingdom ($k \\le n$) should be performed. Every job should be done by one person and every person can do at most one job. The King allowed every idler to choose one job they wanted to do and the $i$-th idler has chosen the job $a_i$. \n\nUnfortunately, some jobs may not be chosen by anyone, so the King has to persuade some idlers to choose another job. The King knows that it takes $b_i$ minutes to persuade the $i$-th idler. He asked his minister of labour to calculate the minimum total time he needs to spend persuading the idlers to get all the jobs done. Can you help him? \n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 10^5$)\u00a0\u2014 the number of idlers and the number of jobs.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le k$)\u00a0\u2014 the jobs chosen by each idler.\n\nThe third line of the input contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($1 \\le b_i \\le 10^9$)\u00a0\u2014 the time the King needs to spend to persuade the $i$-th idler.\n\n\n-----Output-----\n\nThe only line of the output should contain one number\u00a0\u2014 the minimum total time the King needs to spend persuading the idlers to get all the jobs done.\n\n\n-----Examples-----\nInput\n8 7\n1 1 3 1 5 3 7 1\n5 7 4 8 1 3 5 2\n\nOutput\n10\n\nInput\n3 3\n3 1 2\n5 3 4\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example the optimal plan is to persuade idlers 1, 6, and 8 to do jobs 2, 4, and 6.\n\nIn the second example each job was chosen by some idler, so there is no need to persuade anyone."} +{"id": "01524", "text": "Given is a string S consisting of L and R.\nLet N be the length of S. There are N squares arranged from left to right, and the i-th character of S from the left is written on the i-th square from the left.\nThe character written on the leftmost square is always R, and the character written on the rightmost square is always L.\nInitially, one child is standing on each square.\nEach child will perform the move below 10^{100} times:\n - Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right.\nFind the number of children standing on each square after the children performed the moves.\n\n-----Constraints-----\n - S is a string of length between 2 and 10^5 (inclusive).\n - Each character of S is L or R.\n - The first and last characters of S are R and L, respectively.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the number of children standing on each square after the children performed the moves, in order from left to right.\n\n-----Sample Input-----\nRRLRL\n\n-----Sample Output-----\n0 1 2 1 1\n\n - After each child performed one move, the number of children standing on each square is 0, 2, 1, 1, 1 from left to right.\n - After each child performed two moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.\n - After each child performed 10^{100} moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right."} +{"id": "01525", "text": "Amidakuji is a traditional method of lottery in Japan.\nTo make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical line.\nA valid amidakuji is an amidakuji that satisfies the following conditions:\n - No two horizontal lines share an endpoint.\n - The two endpoints of each horizontal lines must be at the same height.\n - A horizontal line must connect adjacent vertical lines.\n\nFind the number of the valid amidakuji that satisfy the following condition, modulo 1\\ 000\\ 000\\ 007: if we trace the path from the top of the leftmost vertical line to the bottom, always following horizontal lines when we encounter them, we reach the bottom of the K-th vertical line from the left.\nFor example, in the following amidakuji, we will reach the bottom of the fourth vertical line from the left.\n\n-----Constraints-----\n - H is an integer between 1 and 100 (inclusive).\n - W is an integer between 1 and 8 (inclusive).\n - K is an integer between 1 and W (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W K\n\n-----Output-----\nPrint the number of the amidakuji that satisfy the condition, modulo 1\\ 000\\ 000\\ 007.\n\n-----Sample Input-----\n1 3 2\n\n-----Sample Output-----\n1\n\nOnly the following one amidakuji satisfies the condition:"} +{"id": "01526", "text": "You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order:\n - Choose two among A, B and C, then increase both by 1.\n - Choose one among A, B and C, then increase it by 2.\nIt can be proved that we can always make A, B and C all equal by repeatedly performing these operations.\n\n-----Constraints-----\n - 0 \\leq A,B,C \\leq 50\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B C\n\n-----Output-----\nPrint the minimum number of operations required to make A, B and C all equal.\n\n-----Sample Input-----\n2 5 4\n\n-----Sample Output-----\n2\n\nWe can make A, B and C all equal by the following operations:\n - Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively.\n - Increase A by 2. Now, A, B, C are 5, 5, 5, respectively."} +{"id": "01527", "text": "Takahashi has a maze, which is a grid of H \\times W squares with H horizontal rows and W vertical columns.\nThe square at the i-th row from the top and the j-th column is a \"wall\" square if S_{ij} is #, and a \"road\" square if S_{ij} is ..\nFrom a road square, you can move to a horizontally or vertically adjacent road square.\nYou cannot move out of the maze, move to a wall square, or move diagonally.\nTakahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.\nAoki will then travel from the starting square to the goal square, in the minimum number of moves required.\nIn this situation, find the maximum possible number of moves Aoki has to make.\n\n-----Constraints-----\n - 1 \\leq H,W \\leq 20\n - S_{ij} is . or #.\n - S contains at least two occurrences of ..\n - Any road square can be reached from any road square in zero or more moves.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\nS_{11}...S_{1W}\n:\nS_{H1}...S_{HW}\n\n-----Output-----\nPrint the maximum possible number of moves Aoki has to make.\n\n-----Sample Input-----\n3 3\n...\n...\n...\n\n-----Sample Output-----\n4\n\nIf Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves."} +{"id": "01528", "text": "In some other world, today is Christmas.\nMr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:\n - A level-0 burger is a patty.\n - A level-L burger (L \\geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom.\nFor example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.\nThe burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?\n\n-----Constraints-----\n - 1 \\leq N \\leq 50\n - 1 \\leq X \\leq ( the total number of layers in a level-N burger )\n - N and X are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN X\n\n-----Output-----\nPrint the number of patties in the bottom-most X layers from the bottom of a level-N burger.\n\n-----Sample Input-----\n2 7\n\n-----Sample Output-----\n4\n\nThere are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB)."} +{"id": "01529", "text": "One day, liouzhou_101 got a chat record of Freda and Rainbow. Out of curiosity, he wanted to know which sentences were said by Freda, and which were said by Rainbow. According to his experience, he thought that Freda always said \"lala.\" at the end of her sentences, while Rainbow always said \"miao.\" at the beginning of his sentences. For each sentence in the chat record, help liouzhou_101 find whose sentence it is. \n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 10), number of sentences in the chat record. Each of the next n lines contains a sentence. A sentence is a string that contains only Latin letters (A-Z, a-z), underline (_), comma (,), point (.) and space ( ). Its length doesn\u2019t exceed 100.\n\n\n-----Output-----\n\nFor each sentence, output \"Freda's\" if the sentence was said by Freda, \"Rainbow's\" if the sentence was said by Rainbow, or \"OMG>.< I don't know!\" if liouzhou_101 can\u2019t recognize whose sentence it is. He can\u2019t recognize a sentence if it begins with \"miao.\" and ends with \"lala.\", or satisfies neither of the conditions. \n\n\n-----Examples-----\nInput\n5\nI will go to play with you lala.\nwow, welcome.\nmiao.lala.\nmiao.\nmiao .\n\nOutput\nFreda's\nOMG>.< I don't know!\nOMG>.< I don't know!\nRainbow's\nOMG>.< I don't know!"} +{"id": "01530", "text": "(\u041b\u0435\u0433\u0435\u043d\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0430 \u043f\u043e\u0437\u0436\u0435) \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c n \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b. \u041f\u0435\u0440\u0432\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c \u0434\u0435\u0440\u0435\u0432\u0430, \u0434\u043b\u044f \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u043d\u0443\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e n - 1 \u0447\u0438\u0441\u043b\u0430 \u0435\u0433\u043e \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043a\u0430 \u0432 \u0434\u0435\u0440\u0435\u0432\u0435.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043e\u0434\u043d\u043e \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e n (2 \u2264 n \u2264 10^5)\u00a0\u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0447\u0438c\u0435\u043b. \u0412\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u043e\u0431\u0435\u043b \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u044b n \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b a_{i} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0414\u043b\u044f \u0432\u0441\u0435\u0445 i > 1 \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043a\u0430 a_{i}\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3\n1 2 3\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1 2\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n5\n4 2 3 1 6\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n4 2 2 4"} +{"id": "01531", "text": "\u2014 Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? \n\n \u2014 Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? \n\nAt this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.\n\nLet's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal a_{i} \u2014 some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!\n\nThe Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; and of course, the walk should be successful: there must be at least two trees in the woodland belt left. \n\nNow help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?\n\n\n-----Input-----\n\nThe first line contains a single integer n \u2014 the initial number of trees in the woodland belt, 2 \u2264 n. The second line contains space-separated integers a_{i} \u2014 the esthetic appeals of each tree. All esthetic appeals do not exceed 10^9 in their absolute value. to get 30 points, you need to solve the problem with constraints: n \u2264 100 (subproblem A1); to get 100 points, you need to solve the problem with constraints: n \u2264 3\u00b710^5 (subproblems A1+A2). \n\n\n-----Output-----\n\nIn the first line print two integers \u2014 the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.\n\nIn the next line print k integers \u2014 the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.\n\nIf there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.\n\n\n-----Examples-----\nInput\n5\n1 2 3 1 2\n\nOutput\n8 1\n1 \nInput\n5\n1 -2 3 1 -2\n\nOutput\n5 2\n2 5"} +{"id": "01532", "text": "Kate has a set $S$ of $n$ integers $\\{1, \\dots, n\\} $. \n\nShe thinks that imperfection of a subset $M \\subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \\neq b$. \n\nKate is a very neat girl and for each $k \\in \\{2, \\dots, n\\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$. \n\nPlease, help Kate to find $I_2$, $I_3$, ..., $I_n$.\n\n\n-----Input-----\n\nThe first and only line in the input consists of only one integer $n$ ($2\\le n \\le 5 \\cdot 10^5$) \u00a0\u2014 the size of the given set $S$.\n\n\n-----Output-----\n\nOutput contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1 \nInput\n3\n\nOutput\n1 1 \n\n\n-----Note-----\n\nFirst sample: answer is 1, because $gcd(1, 2) = 1$.\n\nSecond sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\\{2,3\\}$ and $\\{1, 2, 3\\}$."} +{"id": "01533", "text": "Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.\n\nHe has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.\n\nFormally, for a name s_{i} in the i-th line, output \"YES\" (without quotes) if there exists an index j such that s_{i} = s_{j} and j < i, otherwise, output \"NO\" (without quotes).\n\n\n-----Input-----\n\nFirst line of input contains an integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of names in the list.\n\nNext n lines each contain a string s_{i}, consisting of lowercase English letters. The length of each string is between 1 and 100.\n\n\n-----Output-----\n\nOutput n lines each containing either \"YES\" or \"NO\" (without quotes), depending on whether this string was already present in the stream or not.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n6\ntom\nlucius\nginny\nharry\nginny\nharry\n\nOutput\nNO\nNO\nNO\nNO\nYES\nYES\n\nInput\n3\na\na\na\n\nOutput\nNO\nYES\nYES\n\n\n\n-----Note-----\n\nIn test case 1, for i = 5 there exists j = 3 such that s_{i} = s_{j} and j < i, which means that answer for i = 5 is \"YES\"."} +{"id": "01534", "text": "One day Nikita found the string containing letters \"a\" and \"b\" only. \n\nNikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters \"a\" and the 2-nd contains only letters \"b\".\n\nNikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?\n\n\n-----Input-----\n\nThe first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters \"a\" and \"b\". \n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum possible size of beautiful string Nikita can get.\n\n\n-----Examples-----\nInput\nabba\n\nOutput\n4\nInput\nbab\n\nOutput\n2\n\n\n-----Note-----\n\nIt the first sample the string is already beautiful.\n\nIn the second sample he needs to delete one of \"b\" to make it beautiful."} +{"id": "01535", "text": "There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane. \n\nHan Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x_0, y_0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (x_0, y_0).\n\nYour task is to determine what minimum number of shots Han Solo needs to defeat all the stormtroopers.\n\nThe gun is the newest invention, it shoots very quickly and even after a very large number of shots the stormtroopers don't have enough time to realize what's happening and change their location. \n\n\n-----Input-----\n\nThe first line contains three integers n, x_0 \u0438 y_0 (1 \u2264 n \u2264 1000, - 10^4 \u2264 x_0, y_0 \u2264 10^4) \u2014 the number of stormtroopers on the battle field and the coordinates of your gun.\n\nNext n lines contain two integers each x_{i}, y_{i} ( - 10^4 \u2264 x_{i}, y_{i} \u2264 10^4) \u2014 the coordinates of the stormtroopers on the battlefield. It is guaranteed that no stormtrooper stands at the same point with the gun. Multiple stormtroopers can stand at the same point.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of shots Han Solo needs to destroy all the stormtroopers. \n\n\n-----Examples-----\nInput\n4 0 0\n1 1\n2 2\n2 0\n-1 -1\n\nOutput\n2\n\nInput\n2 1 2\n1 1\n1 0\n\nOutput\n1\n\n\n\n-----Note-----\n\nExplanation to the first and second samples from the statement, respectively: [Image]"} +{"id": "01536", "text": "This is an easier version of the problem. In this version, $n \\le 2000$.\n\nThere are $n$ distinct points in three-dimensional space numbered from $1$ to $n$. The $i$-th point has coordinates $(x_i, y_i, z_i)$. The number of points $n$ is even.\n\nYou'd like to remove all $n$ points using a sequence of $\\frac{n}{2}$ snaps. In one snap, you can remove any two points $a$ and $b$ that have not been removed yet and form a perfectly balanced pair. A pair of points $a$ and $b$ is perfectly balanced if no other point $c$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $a$ and $b$.\n\nFormally, point $c$ lies within the axis-aligned minimum bounding box of points $a$ and $b$ if and only if $\\min(x_a, x_b) \\le x_c \\le \\max(x_a, x_b)$, $\\min(y_a, y_b) \\le y_c \\le \\max(y_a, y_b)$, and $\\min(z_a, z_b) \\le z_c \\le \\max(z_a, z_b)$. Note that the bounding box might be degenerate. \n\nFind a way to remove all points in $\\frac{n}{2}$ snaps.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 2000$; $n$ is even), denoting the number of points.\n\nEach of the next $n$ lines contains three integers $x_i$, $y_i$, $z_i$ ($-10^8 \\le x_i, y_i, z_i \\le 10^8$), denoting the coordinates of the $i$-th point.\n\nNo two points coincide.\n\n\n-----Output-----\n\nOutput $\\frac{n}{2}$ pairs of integers $a_i, b_i$ ($1 \\le a_i, b_i \\le n$), denoting the indices of points removed on snap $i$. Every integer between $1$ and $n$, inclusive, must appear in your output exactly once.\n\nWe can show that it is always possible to remove all points. If there are many solutions, output any of them.\n\n\n-----Examples-----\nInput\n6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0\n\nOutput\n3 6\n5 1\n2 4\n\nInput\n8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3\n\nOutput\n4 5\n1 6\n2 7\n3 8\n\n\n\n-----Note-----\n\nIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $z = 0$ plane). Note that order of removing matters: for example, points $5$ and $1$ don't form a perfectly balanced pair initially, but they do after point $3$ is removed. [Image]"} +{"id": "01537", "text": "Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint \u2014 black and white.\n\nThere is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \\le k \\le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \\le i, j \\le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \\le i' \\le i + k - 1$ and $j \\le j' \\le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white.\n\nA white line is a row or a column without any black cells.\n\nGildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2000$) \u2014 the number of rows and columns, and the size of the eraser.\n\nThe next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell.\n\n\n-----Output-----\n\nPrint one integer: the maximum number of white lines after using the eraser exactly once.\n\n\n-----Examples-----\nInput\n4 2\nBWWW\nWBBW\nWBBW\nWWWB\n\nOutput\n4\n\nInput\n3 1\nBWB\nWWB\nBWB\n\nOutput\n2\n\nInput\n5 3\nBWBBB\nBWBBB\nBBBBB\nBBBBB\nWBBBW\n\nOutput\n2\n\nInput\n2 2\nBW\nWB\n\nOutput\n4\n\nInput\n2 1\nWW\nWW\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW\n\nWWWW\n\nWWWW\n\nWWWB\n\n\n\nThen there are four white lines \u2014 the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column.\n\nIn the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line.\n\nIn the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$."} +{"id": "01538", "text": "Mishka has got n empty boxes. For every i (1 \u2264 i \u2264 n), i-th box is a cube with side length a_{i}.\n\nMishka can put a box i into another box j if the following conditions are met:\n\n i-th box is not put into another box; j-th box doesn't contain any other boxes; box i is smaller than box j (a_{i} < a_{j}). \n\nMishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.\n\nHelp Mishka to determine the minimum possible number of visible boxes!\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 5000) \u2014 the number of boxes Mishka has got.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9), where a_{i} is the side length of i-th box.\n\n\n-----Output-----\n\nPrint the minimum possible number of visible boxes.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n1\n\nInput\n4\n4 2 4 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example it is possible to put box 1 into box 2, and 2 into 3.\n\nIn the second example Mishka can put box 2 into box 3, and box 4 into box 1."} +{"id": "01539", "text": "Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable.\n\nIn total the table Arthur bought has n legs, the length of the i-th leg is l_{i}.\n\nArthur decided to make the table stable and remove some legs. For each of them Arthur determined number d_{i}\u00a0\u2014\u00a0the amount of energy that he spends to remove the i-th leg.\n\nA table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths.\n\nYour task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014\u00a0the initial number of legs in the table Arthur bought.\n\nThe second line of the input contains a sequence of n integers l_{i} (1 \u2264 l_{i} \u2264 10^5), where l_{i} is equal to the length of the i-th leg of the table.\n\nThe third line of the input contains a sequence of n integers d_{i} (1 \u2264 d_{i} \u2264 200), where d_{i} is the number of energy units that Arthur spends on removing the i-th leg off the table.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of energy units that Arthur needs to spend in order to make the table stable.\n\n\n-----Examples-----\nInput\n2\n1 5\n3 2\n\nOutput\n2\n\nInput\n3\n2 4 4\n1 1 1\n\nOutput\n0\n\nInput\n6\n2 2 1 1 3 3\n4 3 5 5 2 1\n\nOutput\n8"} +{"id": "01540", "text": "The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.\n\nR2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.\n\nThe R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers n, m and k (2 \u2264 n \u2264 2\u00b710^4;\u00a01 \u2264 m \u2264 10;\u00a01 \u2264 k \u2264 2\u00b710^5) \u2014 the number of the employees, the number of chats and the number of events in the log, correspondingly. \n\nNext n lines contain matrix a of size n \u00d7 m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as a_{ij}) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.\n\nNext k lines contain the description of the log events. The i-th line contains two space-separated integers x_{i} and y_{i} (1 \u2264 x_{i} \u2264 n;\u00a01 \u2264 y_{i} \u2264 m) which mean that the employee number x_{i} sent one message to chat number y_{i}. It is guaranteed that employee number x_{i} is a participant of chat y_{i}. It is guaranteed that each chat contains at least two employees.\n\n\n-----Output-----\n\nPrint in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.\n\n\n-----Examples-----\nInput\n3 4 5\n1 1 1 1\n1 0 1 1\n1 1 0 0\n1 1\n3 1\n1 3\n2 4\n3 2\n\nOutput\n3 3 1 \nInput\n4 3 4\n0 1 1\n1 0 1\n1 1 1\n0 0 0\n1 2\n2 1\n3 1\n1 3\n\nOutput\n0 2 3 0"} +{"id": "01541", "text": "You have a description of a lever as string s. We'll represent the string length as record |s|, then the lever looks as a horizontal bar with weights of length |s| - 1 with exactly one pivot. We will assume that the bar is a segment on the Ox axis between points 0 and |s| - 1.\n\nThe decoding of the lever description is given below.\n\n If the i-th character of the string equals \"^\", that means that at coordinate i there is the pivot under the bar. If the i-th character of the string equals \"=\", that means that at coordinate i there is nothing lying on the bar. If the i-th character of the string equals digit c (1-9), that means that at coordinate i there is a weight of mass c on the bar. \n\nYour task is, given the lever description, print if it will be in balance or not. Assume that the bar doesn't weight anything. Assume that the bar initially is in balance then all weights are simultaneously put on it. After that the bar either tilts to the left, or tilts to the right, or is in balance.\n\n\n-----Input-----\n\nThe first line contains the lever description as a non-empty string s (3 \u2264 |s| \u2264 10^6), consisting of digits (1-9) and characters \"^\" and \"=\". It is guaranteed that the line contains exactly one character \"^\". It is guaranteed that the pivot of the lever isn't located in any end of the lever bar.\n\nTo solve the problem you may need 64-bit integer numbers. Please, do not forget to use them in your programs.\n\n\n-----Output-----\n\nPrint \"left\" if the given lever tilts to the left, \"right\" if it tilts to the right and \"balance\", if it is in balance.\n\n\n-----Examples-----\nInput\n=^==\n\nOutput\nbalance\n\nInput\n9===^==1\n\nOutput\nleft\n\nInput\n2==^7==\n\nOutput\nright\n\nInput\n41^52==\n\nOutput\nbalance\n\n\n\n-----Note-----\n\nAs you solve the problem, you may find the following link useful to better understand how a lever functions: http://en.wikipedia.org/wiki/Lever.\n\nThe pictures to the examples:\n\n [Image] \n\n [Image] \n\n [Image] \n\n $\\Delta \\Delta \\Delta \\Delta$"} +{"id": "01542", "text": "Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink \"Beecola\", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to x_{i} coins.\n\nVasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent m_{i} coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of \"Beecola\".\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of shops in the city that sell Vasiliy's favourite drink.\n\nThe second line contains n integers x_{i} (1 \u2264 x_{i} \u2264 100 000)\u00a0\u2014 prices of the bottles of the drink in the i-th shop.\n\nThe third line contains a single integer q (1 \u2264 q \u2264 100 000)\u00a0\u2014 the number of days Vasiliy plans to buy the drink.\n\nThen follow q lines each containing one integer m_{i} (1 \u2264 m_{i} \u2264 10^9)\u00a0\u2014 the number of coins Vasiliy can spent on the i-th day.\n\n\n-----Output-----\n\nPrint q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.\n\n\n-----Example-----\nInput\n5\n3 10 8 6 11\n4\n1\n10\n3\n11\n\nOutput\n0\n4\n1\n5\n\n\n\n-----Note-----\n\nOn the first day, Vasiliy won't be able to buy a drink in any of the shops.\n\nOn the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.\n\nOn the third day, Vasiliy can buy a drink only in the shop number 1.\n\nFinally, on the last day Vasiliy can buy a drink in any shop."} +{"id": "01543", "text": "The cities of Byteland and Berland are located on the axis $Ox$. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line $Ox$ there are three types of cities: the cities of Byteland, the cities of Berland, disputed cities. \n\nRecently, the project BNET has been launched \u2014 a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected.\n\nThe countries agreed to connect the pairs of cities with BNET cables in such a way that: If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables, If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables. \n\nThus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities.\n\nThe cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected.\n\nEach city is a point on the line $Ox$. It is technically possible to connect the cities $a$ and $b$ with a cable so that the city $c$ ($a < c < b$) is not connected to this cable, where $a$, $b$ and $c$ are simultaneously coordinates of the cities $a$, $b$ and $c$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 2 \\cdot 10^{5}$) \u2014 the number of cities.\n\nThe following $n$ lines contains an integer $x_i$ and the letter $c_i$ ($-10^{9} \\le x_i \\le 10^{9}$) \u2014 the coordinate of the city and its type. If the city belongs to Byteland, $c_i$ equals to 'B'. If the city belongs to Berland, $c_i$ equals to \u00abR\u00bb. If the city is disputed, $c_i$ equals to 'P'. \n\nAll cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.\n\n\n-----Output-----\n\nPrint the minimal total length of such set of cables, that if we delete all Berland cities ($c_i$='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities ($c_i$='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.\n\n\n-----Examples-----\nInput\n4\n-5 R\n0 P\n3 P\n7 B\n\nOutput\n12\n\nInput\n5\n10 R\n14 B\n16 B\n21 R\n32 R\n\nOutput\n24\n\n\n\n-----Note-----\n\nIn the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be $5 + 3 + 4 = 12$.\n\nIn the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates $10, 21, 32$, so to connect them you need two cables of length $11$ and $11$. The cities of Byteland have coordinates $14$ and $16$, so to connect them you need one cable of length $2$. Thus, the total length of all cables is $11 + 11 + 2 = 24$."} +{"id": "01544", "text": "Because of budget cuts one IT company established new non-financial reward system instead of bonuses.\n\nTwo kinds of actions are rewarded: fixing critical bugs and suggesting new interesting features. A man who fixed a critical bug gets \"I fixed a critical bug\" pennant on his table. A man who suggested a new interesting feature gets \"I suggested a new feature\" pennant on his table.\n\nBecause of the limited budget of the new reward system only 5 \"I fixed a critical bug\" pennants and 3 \"I suggested a new feature\" pennants were bought.\n\nIn order to use these pennants for a long time they were made challenge ones. When a man fixes a new critical bug one of the earlier awarded \"I fixed a critical bug\" pennants is passed on to his table. When a man suggests a new interesting feature one of the earlier awarded \"I suggested a new feature\" pennants is passed on to his table.\n\nOne man can have several pennants of one type and of course he can have pennants of both types on his table. There are n tables in the IT company. Find the number of ways to place the pennants on these tables given that each pennant is situated on one of the tables and each table is big enough to contain any number of pennants.\n\n\n-----Input-----\n\nThe only line of the input contains one integer n (1 \u2264 n \u2264 500) \u2014 the number of tables in the IT company.\n\n\n-----Output-----\n\nOutput one integer \u2014 the amount of ways to place the pennants on n tables.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n24"} +{"id": "01545", "text": "Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That's because this magical paper doesn't allow character number i in the English alphabet to be written on it in a string of length more than a_{i}. For example, if a_1 = 2 he can't write character 'a' on this paper in a string of length 3 or more. String \"aa\" is allowed while string \"aaa\" is not.\n\nMahmoud decided to split the message into some non-empty substrings so that he can write every substring on an independent magical paper and fulfill the condition. The sum of their lengths should be n and they shouldn't overlap. For example, if a_1 = 2 and he wants to send string \"aaa\", he can split it into \"a\" and \"aa\" and use 2 magical papers, or into \"a\", \"a\" and \"a\" and use 3 magical papers. He can't split it into \"aa\" and \"aa\" because the sum of their lengths is greater than n. He can split the message into single string if it fulfills the conditions.\n\nA substring of string s is a string that consists of some consecutive characters from string s, strings \"ab\", \"abc\" and \"b\" are substrings of string \"abc\", while strings \"acb\" and \"ac\" are not. Any string is a substring of itself.\n\nWhile Mahmoud was thinking of how to split the message, Ehab told him that there are many ways to split it. After that Mahmoud asked you three questions: How many ways are there to split the string into substrings such that every substring fulfills the condition of the magical paper, the sum of their lengths is n and they don't overlap? Compute the answer modulo 10^9 + 7. What is the maximum length of a substring that can appear in some valid splitting? What is the minimum number of substrings the message can be spit in? \n\nTwo ways are considered different, if the sets of split positions differ. For example, splitting \"aa|a\" and \"a|aa\" are considered different splittings of message \"aaa\".\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^3) denoting the length of the message.\n\nThe second line contains the message s of length n that consists of lowercase English letters.\n\nThe third line contains 26 integers a_1, a_2, ..., a_26 (1 \u2264 a_{x} \u2264 10^3)\u00a0\u2014 the maximum lengths of substring each letter can appear in.\n\n\n-----Output-----\n\nPrint three lines.\n\nIn the first line print the number of ways to split the message into substrings and fulfill the conditions mentioned in the problem modulo 10^9 + 7.\n\nIn the second line print the length of the longest substring over all the ways.\n\nIn the third line print the minimum number of substrings over all the ways.\n\n\n-----Examples-----\nInput\n3\naab\n2 3 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\nOutput\n3\n2\n2\n\nInput\n10\nabcdeabcde\n5 5 5 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n\nOutput\n401\n4\n3\n\n\n\n-----Note-----\n\nIn the first example the three ways to split the message are: a|a|b aa|b a|ab \n\nThe longest substrings are \"aa\" and \"ab\" of length 2.\n\nThe minimum number of substrings is 2 in \"a|ab\" or \"aa|b\".\n\nNotice that \"aab\" is not a possible splitting because the letter 'a' appears in a substring of length 3, while a_1 = 2."} +{"id": "01546", "text": "One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a \u2260 b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and \u0441 are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2\u00b7n arcs.\n\nFor example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).\n\nMuch time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2\u00b7n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (5 \u2264 n \u2264 10^5) that shows, how many numbers were written on the board. Next 2\u00b7n lines contain pairs of integers a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n, a_{i} \u2260 b_{i}) \u2014 the numbers that were connected by the arcs.\n\nIt is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.\n\n\n-----Output-----\n\nIf Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number \"-1\" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. \n\nIf there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.\n\n\n-----Examples-----\nInput\n5\n1 2\n2 3\n3 4\n4 5\n5 1\n1 3\n2 4\n3 5\n4 1\n5 2\n\nOutput\n1 2 3 4 5 \nInput\n6\n5 6\n4 3\n5 3\n2 4\n6 1\n3 1\n6 2\n2 5\n1 4\n3 6\n1 2\n4 5\n\nOutput\n1 2 4 5 3 6"} +{"id": "01547", "text": "Kris works in a large company \"Blake Technologies\". As a best engineer of the company he was assigned a task to develop a printer that will be able to print horizontal and vertical strips. First prototype is already built and Kris wants to tests it. He wants you to implement the program that checks the result of the printing.\n\nPrinter works with a rectangular sheet of paper of size n \u00d7 m. Consider the list as a table consisting of n rows and m columns. Rows are numbered from top to bottom with integers from 1 to n, while columns are numbered from left to right with integers from 1 to m. Initially, all cells are painted in color 0.\n\nYour program has to support two operations: Paint all cells in row r_{i} in color a_{i}; Paint all cells in column c_{i} in color a_{i}. \n\nIf during some operation i there is a cell that have already been painted, the color of this cell also changes to a_{i}.\n\nYour program has to print the resulting table after k operation.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, m and k (1 \u2264 n, m \u2264 5000, n\u00b7m \u2264 100 000, 1 \u2264 k \u2264 100 000)\u00a0\u2014 the dimensions of the sheet and the number of operations, respectively.\n\nEach of the next k lines contains the description of exactly one query: 1\u00a0r_{i}\u00a0a_{i} (1 \u2264 r_{i} \u2264 n, 1 \u2264 a_{i} \u2264 10^9), means that row r_{i} is painted in color a_{i}; 2\u00a0c_{i}\u00a0a_{i} (1 \u2264 c_{i} \u2264 m, 1 \u2264 a_{i} \u2264 10^9), means that column c_{i} is painted in color a_{i}. \n\n\n-----Output-----\n\nPrint n lines containing m integers each\u00a0\u2014 the resulting table after all operations are applied.\n\n\n-----Examples-----\nInput\n3 3 3\n1 1 3\n2 2 1\n1 2 2\n\nOutput\n3 1 3 \n2 2 2 \n0 1 0 \n\nInput\n5 3 5\n1 1 1\n1 3 1\n1 5 1\n2 1 1\n2 3 1\n\nOutput\n1 1 1 \n1 0 1 \n1 1 1 \n1 0 1 \n1 1 1 \n\n\n\n-----Note-----\n\nThe figure below shows all three operations for the first sample step by step. The cells that were painted on the corresponding step are marked gray. [Image]"} +{"id": "01548", "text": "Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacher's Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.\n\nThe tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point $(0, 0)$. While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to $OX$ or $OY$ axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.\n\nAlexey wants to make a polyline in such a way that its end is as far as possible from $(0, 0)$. Please help him to grow the tree this way.\n\nNote that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 100\\,000$)\u00a0\u2014 the number of sticks Alexey got as a present.\n\nThe second line contains $n$ integers $a_1, \\ldots, a_n$ ($1 \\le a_i \\le 10\\,000$) \u2014 the lengths of the sticks.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the square of the largest possible distance from $(0, 0)$ to the tree end.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n26\nInput\n4\n1 1 2 2\n\nOutput\n20\n\n\n-----Note-----\n\nThe following pictures show optimal trees for example tests. The squared distance in the first example equals $5 \\cdot 5 + 1 \\cdot 1 = 26$, and in the second example $4 \\cdot 4 + 2 \\cdot 2 = 20$.\n\n [Image] \n\n [Image]"} +{"id": "01549", "text": "Misha was interested in water delivery from childhood. That's why his mother sent him to the annual Innovative Olympiad in Irrigation (IOI). Pupils from all Berland compete there demonstrating their skills in watering. It is extremely expensive to host such an olympiad, so after the first $n$ olympiads the organizers introduced the following rule of the host city selection.\n\nThe host cities of the olympiads are selected in the following way. There are $m$ cities in Berland wishing to host the olympiad, they are numbered from $1$ to $m$. The host city of each next olympiad is determined as the city that hosted the olympiad the smallest number of times before. If there are several such cities, the city with the smallest index is selected among them.\n\nMisha's mother is interested where the olympiad will be held in some specific years. The only information she knows is the above selection rule and the host cities of the first $n$ olympiads. Help her and if you succeed, she will ask Misha to avoid flooding your house.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $q$ ($1 \\leq n, m, q \\leq 500\\,000$)\u00a0\u2014 the number of olympiads before the rule was introduced, the number of cities in Berland wishing to host the olympiad, and the number of years Misha's mother is interested in, respectively.\n\nThe next line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq m$), where $a_i$ denotes the city which hosted the olympiad in the $i$-th year. Note that before the rule was introduced the host city was chosen arbitrarily.\n\nEach of the next $q$ lines contains an integer $k_i$ ($n + 1 \\leq k_i \\leq 10^{18}$)\u00a0\u2014 the year number Misha's mother is interested in host city in.\n\n\n-----Output-----\n\nPrint $q$ integers. The $i$-th of them should be the city the olympiad will be hosted in the year $k_i$.\n\n\n-----Examples-----\nInput\n6 4 10\n3 1 1 1 2 2\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n\nOutput\n4\n3\n4\n2\n3\n4\n1\n2\n3\n4\n\nInput\n4 5 4\n4 4 5 1\n15\n9\n13\n6\n\nOutput\n5\n3\n3\n3\n\n\n\n-----Note-----\n\nIn the first example Misha's mother is interested in the first $10$ years after the rule was introduced. The host cities these years are 4, 3, 4, 2, 3, 4, 1, 2, 3, 4.\n\nIn the second example the host cities after the new city is introduced are 2, 3, 1, 2, 3, 5, 1, 2, 3, 4, 5, 1."} +{"id": "01550", "text": "You got a box with a combination lock. The lock has a display showing n digits. There are two buttons on the box, each button changes digits on the display. You have quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the display one position to the right (the last digit becomes the first one). For example, if the display is currently showing number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068.\n\nYou know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of digits on the display.\n\nThe second line contains n digits\u00a0\u2014 the initial state of the display.\n\n\n-----Output-----\n\nPrint a single line containing n digits\u00a0\u2014 the desired state of the display containing the smallest possible number.\n\n\n-----Examples-----\nInput\n3\n579\n\nOutput\n024\n\nInput\n4\n2014\n\nOutput\n0142"} +{"id": "01551", "text": "n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 10^6. For each person we know in which direction (left or right) he is facing, and his maximum speed.\n\nYou can put a bomb in some point with non-negative integer coordinate, and blow it up. At this moment all people will start running with their maximum speed in the direction they are facing. Also, two strange rays will start propagating from the bomb with speed s: one to the right, and one to the left. Of course, the speed s is strictly greater than people's maximum speed.\n\nThe rays are strange because if at any moment the position and the direction of movement of some ray and some person coincide, then the speed of the person immediately increases by the speed of the ray.\n\nYou need to place the bomb is such a point that the minimum time moment in which there is a person that has run through point 0, and there is a person that has run through point 10^6, is as small as possible. In other words, find the minimum time moment t such that there is a point you can place the bomb to so that at time moment t some person has run through 0, and some person has run through point 10^6.\n\n\n-----Input-----\n\nThe first line contains two integers n and s (2 \u2264 n \u2264 10^5, 2 \u2264 s \u2264 10^6)\u00a0\u2014 the number of people and the rays' speed.\n\nThe next n lines contain the description of people. The i-th of these lines contains three integers x_{i}, v_{i} and t_{i} (0 < x_{i} < 10^6, 1 \u2264 v_{i} < s, 1 \u2264 t_{i} \u2264 2)\u00a0\u2014 the coordinate of the i-th person on the line, his maximum speed and the direction he will run to (1 is to the left, i.e. in the direction of coordinate decrease, 2 is to the right, i.e. in the direction of coordinate increase), respectively.\n\nIt is guaranteed that the points 0 and 10^6 will be reached independently of the bomb's position.\n\n\n-----Output-----\n\nPrint the minimum time needed for both points 0 and 10^6 to be reached.\n\nYour answer is considered correct if its absolute or relative error doesn't exceed 10^{ - 6}. Namely, if your answer is a, and the jury's answer is b, then your answer is accepted, if $\\frac{|a - b|}{\\operatorname{max}(1,|b|)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n2 999\n400000 1 2\n500000 1 1\n\nOutput\n500000.000000000000000000000000000000\n\nInput\n2 1000\n400000 500 1\n600000 500 2\n\nOutput\n400.000000000000000000000000000000\n\n\n\n-----Note-----\n\nIn the first example, it is optimal to place the bomb at a point with a coordinate of 400000. Then at time 0, the speed of the first person becomes 1000 and he reaches the point 10^6 at the time 600. The bomb will not affect on the second person, and he will reach the 0 point at the time 500000.\n\nIn the second example, it is optimal to place the bomb at the point 500000. The rays will catch up with both people at the time 200. At this time moment, the first is at the point with a coordinate of 300000, and the second is at the point with a coordinate of 700000. Their speed will become 1500 and at the time 400 they will simultaneously run through points 0 and 10^6."} +{"id": "01552", "text": "The School \u21160 of the capital of Berland has n children studying in it. All the children in this school are gifted: some of them are good at programming, some are good at maths, others are good at PE (Physical Education). Hence, for each child we know value t_{i}: t_{i} = 1, if the i-th child is good at programming, t_{i} = 2, if the i-th child is good at maths, t_{i} = 3, if the i-th child is good at PE \n\nEach child happens to be good at exactly one of these three subjects.\n\nThe Team Scientific Decathlon Olympias requires teams of three students. The school teachers decided that the teams will be composed of three children that are good at different subjects. That is, each team must have one mathematician, one programmer and one sportsman. Of course, each child can be a member of no more than one team.\n\nWhat is the maximum number of teams that the school will be able to present at the Olympiad? How should the teams be formed for that?\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 5000) \u2014 the number of children in the school. The second line contains n integers t_1, t_2, ..., t_{n} (1 \u2264 t_{i} \u2264 3), where t_{i} describes the skill of the i-th child.\n\n\n-----Output-----\n\nIn the first line output integer w \u2014 the largest possible number of teams. \n\nThen print w lines, containing three numbers in each line. Each triple represents the indexes of the children forming the team. You can print both the teams, and the numbers in the triplets in any order. The children are numbered from 1 to n in the order of their appearance in the input. Each child must participate in no more than one team. If there are several solutions, print any of them.\n\nIf no teams can be compiled, print the only line with value w equal to 0.\n\n\n-----Examples-----\nInput\n7\n1 3 1 3 2 1 2\n\nOutput\n2\n3 5 2\n6 7 4\n\nInput\n4\n2 1 1 2\n\nOutput\n0"} +{"id": "01553", "text": "Alyona has recently bought a miniature fridge that can be represented as a matrix with $h$ rows and $2$ columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part.\n\n [Image] An example of a fridge with $h = 7$ and two shelves. The shelves are shown in black. The picture corresponds to the first example. \n\nAlyona has $n$ bottles of milk that she wants to put in the fridge. The $i$-th bottle is $a_i$ cells tall and $1$ cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.\n\nAlyona is interested in the largest integer $k$ such that she can put bottles $1$, $2$, ..., $k$ in the fridge at the same time. Find this largest $k$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $h$ ($1 \\le n \\le 10^3$, $1 \\le h \\le 10^9$)\u00a0\u2014 the number of bottles and the height of the fridge.\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \\le a_i \\le h$)\u00a0\u2014 the heights of the bottles.\n\n\n-----Output-----\n\nPrint the single integer $k$\u00a0\u2014 the maximum integer such that Alyona can put the bottles $1$, $2$, ..., $k$ in the fridge at the same time. If Alyona can put all bottles in the fridge, print $n$. It is easy to see that Alyona can always put at least one bottle in the fridge.\n\n\n-----Examples-----\nInput\n5 7\n2 3 5 4 1\n\nOutput\n3\n\nInput\n10 10\n9 1 1 1 1 1 1 1 1 1\n\nOutput\n4\n\nInput\n5 10\n3 1 4 2 4\n\nOutput\n5\n\n\n\n-----Note-----\n\nOne of optimal locations in the first example is shown on the picture in the statement.\n\nOne of optimal locations in the second example is shown on the picture below.\n\n [Image] \n\nOne of optimal locations in the third example is shown on the picture below.\n\n [Image]"} +{"id": "01554", "text": "There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type a_{i}.\n\nLet's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type.\n\nSplit the row of the pearls to the maximal number of good segments. Note that each pearl should appear in exactly one segment of the partition.\n\nAs input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 3\u00b710^5) \u2014 the number of pearls in a row.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^9) \u2013 the type of the i-th pearl.\n\n\n-----Output-----\n\nOn the first line print integer k \u2014 the maximal number of segments in a partition of the row.\n\nEach of the next k lines should contain two integers l_{j}, r_{j} (1 \u2264 l_{j} \u2264 r_{j} \u2264 n) \u2014 the number of the leftmost and the rightmost pearls in the j-th segment.\n\nNote you should print the correct partition of the row of the pearls, so each pearl should be in exactly one segment and all segments should contain two pearls of the same type.\n\nIf there are several optimal solutions print any of them. You can print the segments in any order.\n\nIf there are no correct partitions of the row print the number \"-1\".\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 1\n\nOutput\n1\n1 5\n\nInput\n5\n1 2 3 4 5\n\nOutput\n-1\n\nInput\n7\n1 2 1 3 1 2 1\n\nOutput\n2\n1 3\n4 7"} +{"id": "01555", "text": "Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review \u00a0\u2014 in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer.\n\nOnce, during a revision of a restaurant of Celtic medieval cuisine named \u00abPoisson\u00bb, that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes.\n\nThe gourmet tasted a set of $n$ dishes on the first day and a set of $m$ dishes on the second day. He made a table $a$ of size $n \\times m$, in which he described his impressions. If, according to the expert, dish $i$ from the first set was better than dish $j$ from the second set, then $a_{ij}$ is equal to \">\", in the opposite case $a_{ij}$ is equal to \"<\". Dishes also may be equally good, in this case $a_{ij}$ is \"=\".\n\nNow Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if $a_{ij}$ is \"<\", then the number assigned to dish $i$ from the first set should be less than the number of dish $j$ from the second set, if $a_{ij}$ is \">\", then it should be greater, and finally if $a_{ij}$ is \"=\", then the numbers should be the same.\n\nHelp Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible.\n\n\n-----Input-----\n\nThe first line contains integers $n$ and $m$ ($1 \\leq n, m \\leq 1000$)\u00a0\u2014 the number of dishes in both days.\n\nEach of the next $n$ lines contains a string of $m$ symbols. The $j$-th symbol on $i$-th line is $a_{ij}$. All strings consist only of \"<\", \">\" and \"=\".\n\n\n-----Output-----\n\nThe first line of output should contain \"Yes\", if it's possible to do a correct evaluation for all the dishes, or \"No\" otherwise.\n\nIf case an answer exist, on the second line print $n$ integers\u00a0\u2014 evaluations of dishes from the first set, and on the third line print $m$ integers\u00a0\u2014 evaluations of dishes from the second set.\n\n\n-----Examples-----\nInput\n3 4\n>>>>\n>>>>\n>>>>\n\nOutput\nYes\n2 2 2 \n1 1 1 1 \n\nInput\n3 3\n>>>\n<<<\n>>>\n\nOutput\nYes\n3 1 3 \n2 2 2 \n\nInput\n3 2\n==\n=<\n==\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be $2$, for all dishes of the first day.\n\nIn the third sample, the table is contradictory\u00a0\u2014 there is no possible evaluation of the dishes that satisfies it."} +{"id": "01556", "text": "Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 \u2264 i \u2264 n) and replaces the i-th element of array a_{i} either with a_{i} + x or with a_{i} - x. Please note that the operation may be applied more than once to the same position.\n\nMaxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. $\\prod_{i = 1}^{n} a_{i}$) can reach, if Maxim would apply no more than k operations to it. Please help him in that.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, k and x (1 \u2264 n, k \u2264 200 000, 1 \u2264 x \u2264 10^9)\u00a0\u2014 the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} ($|a_{i}|\\leq 10^{9}$)\u00a0\u2014 the elements of the array found by Maxim.\n\n\n-----Output-----\n\nPrint n integers b_1, b_2, ..., b_{n} in the only line\u00a0\u2014 the array elements after applying no more than k operations to the array. In particular, $a_{i} \\equiv b_{i} \\operatorname{mod} x$ should stay true for every 1 \u2264 i \u2264 n, but the product of all array elements should be minimum possible.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n5 3 1\n5 4 3 5 2\n\nOutput\n5 4 3 5 -1 \n\nInput\n5 3 1\n5 4 3 5 5\n\nOutput\n5 4 0 5 5 \n\nInput\n5 3 1\n5 4 4 5 5\n\nOutput\n5 1 4 5 5 \n\nInput\n3 2 7\n5 4 2\n\nOutput\n5 11 -5"} +{"id": "01557", "text": "Vova is again playing some computer game, now an RPG. In the game Vova's character received a quest: to slay the fearsome monster called Modcrab.\n\nAfter two hours of playing the game Vova has tracked the monster and analyzed its tactics. The Modcrab has h_2 health points and an attack power of a_2. Knowing that, Vova has decided to buy a lot of strong healing potions and to prepare for battle.\n\nVova's character has h_1 health points and an attack power of a_1. Also he has a large supply of healing potions, each of which increases his current amount of health points by c_1 when Vova drinks a potion. All potions are identical to each other. It is guaranteed that c_1 > a_2.\n\nThe battle consists of multiple phases. In the beginning of each phase, Vova can either attack the monster (thus reducing its health by a_1) or drink a healing potion (it increases Vova's health by c_1; Vova's health can exceed h_1). Then, if the battle is not over yet, the Modcrab attacks Vova, reducing his health by a_2. The battle ends when Vova's (or Modcrab's) health drops to 0 or lower. It is possible that the battle ends in a middle of a phase after Vova's attack.\n\nOf course, Vova wants to win the fight. But also he wants to do it as fast as possible. So he wants to make up a strategy that will allow him to win the fight after the minimum possible number of phases.\n\nHelp Vova to make up a strategy! You may assume that Vova never runs out of healing potions, and that he can always win.\n\n\n-----Input-----\n\nThe first line contains three integers h_1, a_1, c_1 (1 \u2264 h_1, a_1 \u2264 100, 2 \u2264 c_1 \u2264 100) \u2014 Vova's health, Vova's attack power and the healing power of a potion.\n\nThe second line contains two integers h_2, a_2 (1 \u2264 h_2 \u2264 100, 1 \u2264 a_2 < c_1) \u2014 the Modcrab's health and his attack power.\n\n\n-----Output-----\n\nIn the first line print one integer n denoting the minimum number of phases required to win the battle.\n\nThen print n lines. i-th line must be equal to HEAL if Vova drinks a potion in i-th phase, or STRIKE if he attacks the Modcrab.\n\nThe strategy must be valid: Vova's character must not be defeated before slaying the Modcrab, and the monster's health must be 0 or lower after Vova's last action.\n\nIf there are multiple optimal solutions, print any of them.\n\n\n-----Examples-----\nInput\n10 6 100\n17 5\n\nOutput\n4\nSTRIKE\nHEAL\nSTRIKE\nSTRIKE\n\nInput\n11 6 100\n12 5\n\nOutput\n2\nSTRIKE\nSTRIKE\n\n\n\n-----Note-----\n\nIn the first example Vova's character must heal before or after his first attack. Otherwise his health will drop to zero in 2 phases while he needs 3 strikes to win.\n\nIn the second example no healing needed, two strikes are enough to get monster to zero health and win with 6 health left."} +{"id": "01558", "text": "Vanya wants to pass n exams and get the academic scholarship. He will get the scholarship if the average grade mark for all the exams is at least avg. The exam grade cannot exceed r. Vanya has passed the exams and got grade a_{i} for the i-th exam. To increase the grade for the i-th exam by 1 point, Vanya must write b_{i} essays. He can raise the exam grade multiple times.\n\nWhat is the minimum number of essays that Vanya needs to write to get scholarship?\n\n\n-----Input-----\n\nThe first line contains three integers n, r, avg (1 \u2264 n \u2264 10^5, 1 \u2264 r \u2264 10^9, 1 \u2264 avg \u2264 min(r, 10^6))\u00a0\u2014 the number of exams, the maximum grade and the required grade point average, respectively.\n\nEach of the following n lines contains space-separated integers a_{i} and b_{i} (1 \u2264 a_{i} \u2264 r, 1 \u2264 b_{i} \u2264 10^6).\n\n\n-----Output-----\n\nIn the first line print the minimum number of essays.\n\n\n-----Examples-----\nInput\n5 5 4\n5 2\n4 7\n3 1\n3 2\n2 5\n\nOutput\n4\n\nInput\n2 5 4\n5 2\n5 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample Vanya can write 2 essays for the 3rd exam to raise his grade by 2 points and 2 essays for the 4th exam to raise his grade by 1 point.\n\nIn the second sample, Vanya doesn't need to write any essays as his general point average already is above average."} +{"id": "01559", "text": "Alice became interested in periods of integer numbers. We say positive $X$ integer number is periodic with length $L$ if there exists positive integer number $P$ with $L$ digits such that $X$ can be written as $PPPP\u2026P$. For example:\n\n$X = 123123123$ is periodic number with length $L = 3$ and $L = 9$\n\n$X = 42424242$ is periodic number with length $L = 2,L = 4$ and $L = 8$\n\n$X = 12345$ is periodic number with length $L = 5$\n\nFor given positive period length $L$ and positive integer number $A$, Alice wants to find smallest integer number $X$ strictly greater than $A$ that is periodic with length L.\n\n\n-----Input-----\n\nFirst line contains one positive integer number $L \\ (1 \\leq L \\leq 10^5)$ representing length of the period. Second line contains one positive integer number $A \\ (1 \\leq A \\leq 10^{100 000})$.\n\n\n-----Output-----\n\nOne positive integer number representing smallest positive number that is periodic with length $L$ and is greater than $A$.\n\n\n-----Examples-----\nInput\n3\n123456\n\nOutput\n124124\n\nInput\n3\n12345\n\nOutput\n100100\n\n\n\n-----Note-----\n\nIn first example 124124 is the smallest number greater than 123456 that can be written with period L = 3 (P = 124).\n\nIn the second example 100100 is the smallest number greater than 12345 with period L = 3 (P=100)"} +{"id": "01560", "text": "Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.\n\nAnatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.\n\nHelp Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of cockroaches.\n\nThe second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.\n\n\n-----Examples-----\nInput\n5\nrbbrr\n\nOutput\n1\n\nInput\n5\nbbbbb\n\nOutput\n2\n\nInput\n3\nrbr\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.\n\nIn the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.\n\nIn the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0."} +{"id": "01561", "text": "Suppose that you are in a campus and have to go for classes day by day. As you may see, when you hurry to a classroom, you surprisingly find that many seats there are already occupied. Today you and your friends went for class, and found out that some of the seats were occupied.\n\nThe classroom contains $n$ rows of seats and there are $m$ seats in each row. Then the classroom can be represented as an $n \\times m$ matrix. The character '.' represents an empty seat, while '*' means that the seat is occupied. You need to find $k$ consecutive empty seats in the same row or column and arrange those seats for you and your friends. Your task is to find the number of ways to arrange the seats. Two ways are considered different if sets of places that students occupy differs.\n\n\n-----Input-----\n\nThe first line contains three positive integers $n,m,k$ ($1 \\leq n, m, k \\leq 2\\,000$), where $n,m$ represent the sizes of the classroom and $k$ is the number of consecutive seats you need to find.\n\nEach of the next $n$ lines contains $m$ characters '.' or '*'. They form a matrix representing the classroom, '.' denotes an empty seat, and '*' denotes an occupied seat.\n\n\n-----Output-----\n\nA single number, denoting the number of ways to find $k$ empty seats in the same row or column.\n\n\n-----Examples-----\nInput\n2 3 2\n**.\n...\n\nOutput\n3\n\nInput\n1 2 2\n..\n\nOutput\n1\n\nInput\n3 3 4\n.*.\n*.*\n.*.\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, there are three ways to arrange those seats. You can take the following seats for your arrangement. $(1,3)$, $(2,3)$ $(2,2)$, $(2,3)$ $(2,1)$, $(2,2)$"} +{"id": "01562", "text": "You are on the island which can be represented as a $n \\times m$ table. The rows are numbered from $1$ to $n$ and the columns are numbered from $1$ to $m$. There are $k$ treasures on the island, the $i$-th of them is located at the position $(r_i, c_i)$.\n\nInitially you stand at the lower left corner of the island, at the position $(1, 1)$. If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from $(r, c)$ to $(r+1, c)$), left (from $(r, c)$ to $(r, c-1)$), or right (from position $(r, c)$ to $(r, c+1)$). Because of the traps, you can't move down.\n\nHowever, moving up is also risky. You can move up only if you are in a safe column. There are $q$ safe columns: $b_1, b_2, \\ldots, b_q$. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures.\n\n\n-----Input-----\n\nThe first line contains integers $n$, $m$, $k$ and $q$ ($2 \\le n, \\, m, \\, k, \\, q \\le 2 \\cdot 10^5$, $q \\le m$)\u00a0\u2014 the number of rows, the number of columns, the number of treasures in the island and the number of safe columns.\n\nEach of the next $k$ lines contains two integers $r_i, c_i$, ($1 \\le r_i \\le n$, $1 \\le c_i \\le m$)\u00a0\u2014 the coordinates of the cell with a treasure. All treasures are located in distinct cells.\n\nThe last line contains $q$ distinct integers $b_1, b_2, \\ldots, b_q$ ($1 \\le b_i \\le m$) \u2014 the indices of safe columns.\n\n\n-----Output-----\n\nPrint the minimum number of moves required to collect all the treasures.\n\n\n-----Examples-----\nInput\n3 3 3 2\n1 1\n2 1\n3 1\n2 3\n\nOutput\n6\nInput\n3 5 3 2\n1 2\n2 3\n3 1\n1 5\n\nOutput\n8\nInput\n3 6 3 2\n1 6\n2 2\n3 4\n1 6\n\nOutput\n15\n\n\n-----Note-----\n\nIn the first example you should use the second column to go up, collecting in each row treasures from the first column. [Image] \n\nIn the second example, it is optimal to use the first column to go up. [Image] \n\nIn the third example, it is optimal to collect the treasure at cell $(1;6)$, go up to row $2$ at column $6$, then collect the treasure at cell $(2;2)$, go up to the top row at column $1$ and collect the last treasure at cell $(3;4)$. That's a total of $15$ moves. [Image]"} +{"id": "01563", "text": "You've got an undirected graph, consisting of n vertices and m edges. We will consider the graph's vertices numbered with integers from 1 to n. Each vertex of the graph has a color. The color of the i-th vertex is an integer c_{i}.\n\nLet's consider all vertices of the graph, that are painted some color k. Let's denote a set of such as V(k). Let's denote the value of the neighbouring color diversity for color k as the cardinality of the set Q(k) = {c_{u}\u00a0: \u00a0c_{u} \u2260 k and there is vertex v belonging to set V(k) such that nodes v and u are connected by an edge of the graph}.\n\nYour task is to find such color k, which makes the cardinality of set Q(k) maximum. In other words, you want to find the color that has the most diverse neighbours. Please note, that you want to find such color k, that the graph has at least one vertex with such color.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n, m (1 \u2264 n, m \u2264 10^5) \u2014 the number of vertices end edges of the graph, correspondingly. The second line contains a sequence of integers c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 10^5) \u2014 the colors of the graph vertices. The numbers on the line are separated by spaces.\n\nNext m lines contain the description of the edges: the i-th line contains two space-separated integers a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n;\u00a0a_{i} \u2260 b_{i}) \u2014 the numbers of the vertices, connected by the i-th edge. \n\nIt is guaranteed that the given graph has no self-loops or multiple edges.\n\n\n-----Output-----\n\nPrint the number of the color which has the set of neighbours with the maximum cardinality. It there are multiple optimal colors, print the color with the minimum number. Please note, that you want to find such color, that the graph has at least one vertex with such color.\n\n\n-----Examples-----\nInput\n6 6\n1 1 2 3 5 8\n1 2\n3 2\n1 4\n4 3\n4 5\n4 6\n\nOutput\n3\n\nInput\n5 6\n4 2 5 2 4\n1 2\n2 3\n3 1\n5 3\n5 4\n3 4\n\nOutput\n2"} +{"id": "01564", "text": "Monocarp has got two strings $s$ and $t$ having equal length. Both strings consist of lowercase Latin letters \"a\" and \"b\". \n\nMonocarp wants to make these two strings $s$ and $t$ equal to each other. He can do the following operation any number of times: choose an index $pos_1$ in the string $s$, choose an index $pos_2$ in the string $t$, and swap $s_{pos_1}$ with $t_{pos_2}$.\n\nYou have to determine the minimum number of operations Monocarp has to perform to make $s$ and $t$ equal, and print any optimal sequence of operations \u2014 or say that it is impossible to make these strings equal.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ $(1 \\le n \\le 2 \\cdot 10^{5})$ \u2014 the length of $s$ and $t$.\n\nThe second line contains one string $s$ consisting of $n$ characters \"a\" and \"b\". \n\nThe third line contains one string $t$ consisting of $n$ characters \"a\" and \"b\". \n\n\n-----Output-----\n\nIf it is impossible to make these strings equal, print $-1$.\n\nOtherwise, in the first line print $k$ \u2014 the minimum number of operations required to make the strings equal. In each of the next $k$ lines print two integers \u2014 the index in the string $s$ and the index in the string $t$ that should be used in the corresponding swap operation. \n\n\n-----Examples-----\nInput\n4\nabab\naabb\n\nOutput\n2\n3 3\n3 2\n\nInput\n1\na\nb\n\nOutput\n-1\n\nInput\n8\nbabbaabb\nabababaa\n\nOutput\n3\n2 6\n1 3\n7 8\n\n\n\n-----Note-----\n\nIn the first example two operations are enough. For example, you can swap the third letter in $s$ with the third letter in $t$. Then $s = $ \"abbb\", $t = $ \"aaab\". Then swap the third letter in $s$ and the second letter in $t$. Then both $s$ and $t$ are equal to \"abab\".\n\nIn the second example it's impossible to make two strings equal."} +{"id": "01565", "text": "Dima worked all day and wrote down on a long paper strip his favorite number $n$ consisting of $l$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf.\n\nTo solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip.\n\nDima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain.\n\n\n-----Input-----\n\nThe first line contains a single integer $l$ ($2 \\le l \\le 100\\,000$)\u00a0\u2014 the length of the Dima's favorite number.\n\nThe second line contains the positive integer $n$ initially written on the strip: the Dima's favorite number.\n\nThe integer $n$ consists of exactly $l$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the smallest number Dima can obtain.\n\n\n-----Examples-----\nInput\n7\n1234567\n\nOutput\n1801\n\nInput\n3\n101\n\nOutput\n11\n\n\n\n-----Note-----\n\nIn the first example Dima can split the number $1234567$ into integers $1234$ and $567$. Their sum is $1801$.\n\nIn the second example Dima can split the number $101$ into integers $10$ and $1$. Their sum is $11$. Note that it is impossible to split the strip into \"1\" and \"01\" since the numbers can't start with zeros."} +{"id": "01566", "text": "The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good.\n\nHeidi knows that the lair can be represented as a rectangle on a lattice, with sides parallel to the axes. Each vertex of the polygon occupies an integer point on the lattice. For each cell of the lattice, Heidi can check the level of Zombie Contamination. This level is an integer between 0 and 4, equal to the number of corners of the cell that are inside or on the border of the rectangle.\n\nAs a test, Heidi wants to check that her Zombie Contamination level checker works. Given the output of the checker, Heidi wants to know whether it could have been produced by a single non-zero area rectangular-shaped lair (with axis-parallel sides). [Image]\n\n\n-----Input-----\n\nThe first line of each test case contains one integer N, the size of the lattice grid (5 \u2264 N \u2264 50). The next N lines each contain N characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4.\n\nCells are given in the same order as they are shown in the picture above: rows go in the decreasing value of y coordinate, and in one row cells go in the order of increasing x coordinate. This means that the first row corresponds to cells with coordinates (1, N), ..., (N, N) and the last row corresponds to cells with coordinates (1, 1), ..., (N, 1).\n\n\n-----Output-----\n\nThe first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise.\n\n\n-----Example-----\nInput\n6\n000000\n000000\n012100\n024200\n012100\n000000\n\nOutput\nYes\n\n\n\n-----Note-----\n\nThe lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (x_1, y_1), (x_1, y_2), (x_2, y_1), (x_2, y_2)), has a non-zero area and be contained inside of the grid (that is, 0 \u2264 x_1 < x_2 \u2264 N, 0 \u2264 y_1 < y_2 \u2264 N), and result in the levels of Zombie Contamination as reported in the input."} +{"id": "01567", "text": "We define $x \\bmod y$ as the remainder of division of $x$ by $y$ ($\\%$ operator in C++ or Java, mod operator in Pascal).\n\nLet's call an array of positive integers $[a_1, a_2, \\dots, a_k]$ stable if for every permutation $p$ of integers from $1$ to $k$, and for every non-negative integer $x$, the following condition is met:\n\n $ (((x \\bmod a_1) \\bmod a_2) \\dots \\bmod a_{k - 1}) \\bmod a_k = (((x \\bmod a_{p_1}) \\bmod a_{p_2}) \\dots \\bmod a_{p_{k - 1}}) \\bmod a_{p_k} $ \n\nThat is, for each non-negative integer $x$, the value of $(((x \\bmod a_1) \\bmod a_2) \\dots \\bmod a_{k - 1}) \\bmod a_k$ does not change if we reorder the elements of the array $a$.\n\nFor two given integers $n$ and $k$, calculate the number of stable arrays $[a_1, a_2, \\dots, a_k]$ such that $1 \\le a_1 < a_2 < \\dots < a_k \\le n$.\n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $k$ ($1 \\le n, k \\le 5 \\cdot 10^5$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of stable arrays $[a_1, a_2, \\dots, a_k]$ such that $1 \\le a_1 < a_2 < \\dots < a_k \\le n$. Since the answer may be large, print it modulo $998244353$.\n\n\n-----Examples-----\nInput\n7 3\n\nOutput\n16\n\nInput\n3 7\n\nOutput\n0\n\nInput\n1337 42\n\nOutput\n95147305\n\nInput\n1 1\n\nOutput\n1\n\nInput\n500000 1\n\nOutput\n500000"} +{"id": "01568", "text": "There are n incoming messages for Vasya. The i-th message is going to be received after t_{i} minutes. Each message has a cost, which equals to A initially. After being received, the cost of a message decreases by B each minute (it can become negative). Vasya can read any message after receiving it at any moment of time. After reading the message, Vasya's bank account receives the current cost of this message. Initially, Vasya's bank account is at 0.\n\nAlso, each minute Vasya's bank account receives C\u00b7k, where k is the amount of received but unread messages.\n\nVasya's messages are very important to him, and because of that he wants to have all messages read after T minutes.\n\nDetermine the maximum amount of money Vasya's bank account can hold after T minutes.\n\n\n-----Input-----\n\nThe first line contains five integers n, A, B, C and T (1 \u2264 n, A, B, C, T \u2264 1000).\n\nThe second string contains n integers t_{i} (1 \u2264 t_{i} \u2264 T).\n\n\n-----Output-----\n\nOutput one integer \u00a0\u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n4 5 5 3 5\n1 5 5 4\n\nOutput\n20\n\nInput\n5 3 1 1 3\n2 2 2 1 1\n\nOutput\n15\n\nInput\n5 5 3 4 5\n1 2 3 4 5\n\nOutput\n35\n\n\n\n-----Note-----\n\nIn the first sample the messages must be read immediately after receiving, Vasya receives A points for each message, n\u00b7A = 20 in total.\n\nIn the second sample the messages can be read at any integer moment.\n\nIn the third sample messages must be read at the moment T. This way Vasya has 1, 2, 3, 4 and 0 unread messages at the corresponding minutes, he gets 40 points for them. When reading messages, he receives (5 - 4\u00b73) + (5 - 3\u00b73) + (5 - 2\u00b73) + (5 - 1\u00b73) + 5 = - 5 points. This is 35 in total."} +{"id": "01569", "text": "Breaking Good is a new video game which a lot of gamers want to have. There is a certain level in the game that is really difficult even for experienced gamers.\n\nWalter William, the main character of the game, wants to join a gang called Los Hermanos (The Brothers). The gang controls the whole country which consists of n cities with m bidirectional roads connecting them. There is no road is connecting a city to itself and for any two cities there is at most one road between them. The country is connected, in the other words, it is possible to reach any city from any other city using the given roads. \n\nThe roads aren't all working. There are some roads which need some more work to be performed to be completely functioning.\n\nThe gang is going to rob a bank! The bank is located in city 1. As usual, the hardest part is to escape to their headquarters where the police can't get them. The gang's headquarters is in city n. To gain the gang's trust, Walter is in charge of this operation, so he came up with a smart plan.\n\nFirst of all the path which they are going to use on their way back from city 1 to their headquarters n must be as short as possible, since it is important to finish operation as fast as possible.\n\nThen, gang has to blow up all other roads in country that don't lay on this path, in order to prevent any police reinforcements. In case of non-working road, they don't have to blow up it as it is already malfunctional. \n\nIf the chosen path has some roads that doesn't work they'll have to repair those roads before the operation.\n\nWalter discovered that there was a lot of paths that satisfied the condition of being shortest possible so he decided to choose among them a path that minimizes the total number of affected roads (both roads that have to be blown up and roads to be repaired).\n\nCan you help Walter complete his task and gain the gang's trust?\n\n\n-----Input-----\n\nThe first line of input contains two integers n, m (2 \u2264 n \u2264 10^5, $0 \\leq m \\leq \\operatorname{min}(\\frac{n(n - 1)}{2}, 10^{5})$), the number of cities and number of roads respectively.\n\nIn following m lines there are descriptions of roads. Each description consists of three integers x, y, z (1 \u2264 x, y \u2264 n, $z \\in \\{0,1 \\}$) meaning that there is a road connecting cities number x and y. If z = 1, this road is working, otherwise it is not.\n\n\n-----Output-----\n\nIn the first line output one integer k, the minimum possible number of roads affected by gang.\n\nIn the following k lines output three integers describing roads that should be affected. Each line should contain three integers x, y, z (1 \u2264 x, y \u2264 n, $z \\in \\{0,1 \\}$), cities connected by a road and the new state of a road. z = 1 indicates that the road between cities x and y should be repaired and z = 0 means that road should be blown up. \n\nYou may output roads in any order. Each affected road should appear exactly once. You may output cities connected by a single road in any order. If you output a road, it's original state should be different from z.\n\nAfter performing all operations accroding to your plan, there should remain working only roads lying on some certain shortest past between city 1 and n.\n\nIf there are multiple optimal answers output any.\n\n\n-----Examples-----\nInput\n2 1\n1 2 0\n\nOutput\n1\n1 2 1\n\nInput\n4 4\n1 2 1\n1 3 0\n2 3 1\n3 4 1\n\nOutput\n3\n1 2 0\n1 3 1\n2 3 0\n\nInput\n8 9\n1 2 0\n8 3 0\n2 3 1\n1 4 1\n8 7 0\n1 5 1\n4 6 1\n5 7 0\n6 8 0\n\nOutput\n3\n2 3 0\n1 5 0\n6 8 1\n\n\n\n-----Note-----\n\nIn the first test the only path is 1 - 2\n\nIn the second test the only shortest path is 1 - 3 - 4\n\nIn the third test there are multiple shortest paths but the optimal is 1 - 4 - 6 - 8"} +{"id": "01570", "text": "A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay i\u00b7k dollars for the i-th banana). \n\nHe has n dollars. How many dollars does he have to borrow from his friend soldier to buy w bananas?\n\n\n-----Input-----\n\nThe first line contains three positive integers k, n, w (1 \u2264 k, w \u2264 1000, 0 \u2264 n \u2264 10^9), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants. \n\n\n-----Output-----\n\nOutput one integer \u2014 the amount of dollars that the soldier must borrow from his friend. If he doesn't have to borrow money, output 0.\n\n\n-----Examples-----\nInput\n3 17 4\n\nOutput\n13"} +{"id": "01571", "text": "Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. [Image]\u00a0\n\nKaavi has a string $T$ of length $m$ and all the strings with the prefix $T$ are magic spells. Kaavi also has a string $S$ of length $n$ and an empty string $A$.\n\nDuring the divination, Kaavi needs to perform a sequence of operations. There are two different operations: Delete the first character of $S$ and add it at the front of $A$. Delete the first character of $S$ and add it at the back of $A$.\n\nKaavi can perform no more than $n$ operations. To finish the divination, she wants to know the number of different operation sequences to make $A$ a magic spell (i.e. with the prefix $T$). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo $998\\,244\\,353$.\n\nTwo operation sequences are considered different if they are different in length or there exists an $i$ that their $i$-th operation is different. \n\nA substring is a contiguous sequence of characters within a string. A prefix of a string $S$ is a substring of $S$ that occurs at the beginning of $S$.\n\n\n-----Input-----\n\nThe first line contains a string $S$ of length $n$ ($1 \\leq n \\leq 3000$).\n\nThe second line contains a string $T$ of length $m$ ($1 \\leq m \\leq n$).\n\nBoth strings contain only lowercase Latin letters.\n\n\n-----Output-----\n\nThe output contains only one integer \u00a0\u2014 the answer modulo $998\\,244\\,353$.\n\n\n-----Examples-----\nInput\nabab\nba\n\nOutput\n12\nInput\ndefineintlonglong\nsignedmain\n\nOutput\n0\nInput\nrotator\nrotator\n\nOutput\n4\nInput\ncacdcdbbbb\nbdcaccdbbb\n\nOutput\n24\n\n\n-----Note-----\n\nThe first test:\n\n$\\text{baba abab bbaa baab baab}$\n\nThe red ones are the magic spells. In the first operation, Kaavi can either add the first character \"a\" at the front or the back of $A$, although the results are the same, they are considered as different operations. So the answer is $6\\times2=12$."} +{"id": "01572", "text": "You have array a_1, a_2, ..., a_{n}. Segment [l, r] (1 \u2264 l \u2264 r \u2264 n) is good if a_{i} = a_{i} - 1 + a_{i} - 2, for all i (l + 2 \u2264 i \u2264 r).\n\nLet's define len([l, r]) = r - l + 1, len([l, r]) is the length of the segment [l, r]. Segment [l_1, r_1], is longer than segment [l_2, r_2], if len([l_1, r_1]) > len([l_2, r_2]).\n\nYour task is to find a good segment of the maximum length in array a. Note that a segment of length 1 or 2 is always good.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5) \u2014 the number of elements in the array. The second line contains integers: a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint the length of the longest good segment in array a.\n\n\n-----Examples-----\nInput\n10\n1 2 3 5 8 13 21 34 55 89\n\nOutput\n10\n\nInput\n5\n1 1 1 1 1\n\nOutput\n2"} +{"id": "01573", "text": "Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. \n\nKefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers, n and d (1 \u2264 n \u2264 10^5, $1 \\leq d \\leq 10^{9}$) \u2014 the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.\n\nNext n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type m_{i}, s_{i} (0 \u2264 m_{i}, s_{i} \u2264 10^9) \u2014 the amount of money and the friendship factor, respectively. \n\n\n-----Output-----\n\nPrint the maximum total friendship factir that can be reached.\n\n\n-----Examples-----\nInput\n4 5\n75 5\n0 100\n150 20\n75 1\n\nOutput\n100\n\nInput\n5 100\n0 7\n11 32\n99 10\n46 8\n87 54\n\nOutput\n111\n\n\n\n-----Note-----\n\nIn the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.\n\nIn the second sample test we can take all the friends."} +{"id": "01574", "text": "Do you know a story about the three musketeers? Anyway, you will learn about its origins now.\n\nRichelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys.\n\nThere are n warriors. Richelimakieu wants to choose three of them to become musketeers but it's not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn't be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers.\n\nHelp Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers, n and m (3 \u2264 n \u2264 4000, 0 \u2264 m \u2264 4000) \u2014 respectively number of warriors and number of pairs of warriors knowing each other.\n\ni-th of the following m lines contains two space-separated integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n, a_{i} \u2260 b_{i}). Warriors a_{i} and b_{i} know each other. Each pair of warriors will be listed at most once.\n\n\n-----Output-----\n\nIf Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print \"-1\" (without the quotes).\n\n\n-----Examples-----\nInput\n5 6\n1 2\n1 3\n2 3\n2 4\n3 4\n4 5\n\nOutput\n2\n\nInput\n7 4\n2 1\n3 6\n5 1\n1 7\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample Richelimakieu should choose a triple 1, 2, 3. The first musketeer doesn't know anyone except other two musketeers so his recognition is 0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition 1 because he knows warrior 4. Sum of recognitions is 0 + 1 + 1 = 2.\n\nThe other possible triple is 2, 3, 4 but it has greater sum of recognitions, equal to 1 + 1 + 1 = 3.\n\nIn the second sample there is no triple of warriors knowing each other."} +{"id": "01575", "text": "Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.\n\nHe knows that the receptionist starts working after t_{s} minutes have passed after midnight and closes after t_{f} minutes have passed after midnight (so that (t_{f} - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). \n\nVasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. [Image] \"Reception 1\" \n\nFor each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.\n\nVasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!\n\n\n-----Input-----\n\nThe first line contains three integers: the point of time when the receptionist begins to work t_{s}, the point of time when the receptionist stops working t_{f} and the time the receptionist spends on each visitor t. The second line contains one integer n\u00a0\u2014 the amount of visitors (0 \u2264 n \u2264 100 000). The third line contains positive integers in non-decreasing order\u00a0\u2014 the points of time when the visitors arrive to the passport office.\n\nAll times are set in minutes and do not exceed 10^12; it is guaranteed that t_{s} < t_{f}. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.\n\n\n-----Output-----\n\nPrint single non-negative integer\u00a0\u2014 the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.\n\n\n-----Examples-----\nInput\n10 15 2\n2\n10 13\n\nOutput\n12\nInput\n8 17 3\n4\n3 4 5 8\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight.\n\nIn the second example, Vasya has to come before anyone else to be served."} +{"id": "01576", "text": "Polycarp loves ciphers. He has invented his own cipher called Right-Left.\n\nRight-Left cipher is used for strings. To encrypt the string $s=s_{1}s_{2} \\dots s_{n}$ Polycarp uses the following algorithm: he writes down $s_1$, he appends the current word with $s_2$ (i.e. writes down $s_2$ to the right of the current result), he prepends the current word with $s_3$ (i.e. writes down $s_3$ to the left of the current result), he appends the current word with $s_4$ (i.e. writes down $s_4$ to the right of the current result), he prepends the current word with $s_5$ (i.e. writes down $s_5$ to the left of the current result), and so on for each position until the end of $s$. \n\nFor example, if $s$=\"techno\" the process is: \"t\" $\\to$ \"te\" $\\to$ \"cte\" $\\to$ \"cteh\" $\\to$ \"ncteh\" $\\to$ \"ncteho\". So the encrypted $s$=\"techno\" is \"ncteho\".\n\nGiven string $t$ \u2014 the result of encryption of some string $s$. Your task is to decrypt it, i.e. find the string $s$.\n\n\n-----Input-----\n\nThe only line of the input contains $t$ \u2014 the result of encryption of some string $s$. It contains only lowercase Latin letters. The length of $t$ is between $1$ and $50$, inclusive.\n\n\n-----Output-----\n\nPrint such string $s$ that after encryption it equals $t$.\n\n\n-----Examples-----\nInput\nncteho\n\nOutput\ntechno\n\nInput\nerfdcoeocs\n\nOutput\ncodeforces\n\nInput\nz\n\nOutput\nz"} +{"id": "01577", "text": "Anton likes to play chess, and so does his friend Danik.\n\nOnce they have played n games in a row. For each game it's known who was the winner\u00a0\u2014 Anton or Danik. None of the games ended with a tie.\n\nNow Anton wonders, who won more games, he or Danik? Help him determine this.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of games played.\n\nThe second line contains a string s, consisting of n uppercase English letters 'A' and 'D'\u00a0\u2014 the outcome of each of the games. The i-th character of the string is equal to 'A' if the Anton won the i-th game and 'D' if Danik won the i-th game.\n\n\n-----Output-----\n\nIf Anton won more games than Danik, print \"Anton\" (without quotes) in the only line of the output.\n\nIf Danik won more games than Anton, print \"Danik\" (without quotes) in the only line of the output.\n\nIf Anton and Danik won the same number of games, print \"Friendship\" (without quotes).\n\n\n-----Examples-----\nInput\n6\nADAAAA\n\nOutput\nAnton\n\nInput\n7\nDDDAADA\n\nOutput\nDanik\n\nInput\n6\nDADADA\n\nOutput\nFriendship\n\n\n\n-----Note-----\n\nIn the first sample, Anton won 6 games, while Danik\u00a0\u2014 only 1. Hence, the answer is \"Anton\".\n\nIn the second sample, Anton won 3 games and Danik won 4 games, so the answer is \"Danik\".\n\nIn the third sample, both Anton and Danik won 3 games and the answer is \"Friendship\"."} +{"id": "01578", "text": "For an integer N, we will choose a permutation \\{P_1, P_2, ..., P_N\\} of \\{1, 2, ..., N\\}.\nThen, for each i=1,2,...,N, let M_i be the remainder when i is divided by P_i.\nFind the maximum possible value of M_1 + M_2 + \\cdots + M_N.\n\n-----Constraints-----\n - N is an integer satisfying 1 \\leq N \\leq 10^9.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the maximum possible value of M_1 + M_2 + \\cdots + M_N.\n\n-----Sample Input-----\n2\n\n-----Sample Output-----\n1\n\nWhen the permutation \\{P_1, P_2\\} = \\{2, 1\\} is chosen, M_1 + M_2 = 1 + 0 = 1."} +{"id": "01579", "text": "There are N dots in a two-dimensional plane. The coordinates of the i-th dot are (x_i, y_i).\nWe will repeat the following operation as long as possible:\n - Choose four integers a, b, c, d (a \\neq c, b \\neq d) such that there are dots at exactly three of the positions (a, b), (a, d), (c, b) and (c, d), and add a dot at the remaining position.\nWe can prove that we can only do this operation a finite number of times. Find the maximum number of times we can do the operation.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - 1 \\leq x_i, y_i \\leq 10^5\n - If i \\neq j, x_i \\neq x_j or y_i \\neq y_j.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nx_1 y_1\n:\nx_N y_N\n\n-----Output-----\nPrint the maximum number of times we can do the operation.\n\n-----Sample Input-----\n3\n1 1\n5 1\n5 5\n\n-----Sample Output-----\n1\n\nBy choosing a = 1, b = 1, c = 5, d = 5, we can add a dot at (1, 5). We cannot do the operation any more, so the maximum number of operations is 1."} +{"id": "01580", "text": "There are N cards placed face down in a row. On each card, an integer 1 or 2 is written.\nLet A_i be the integer written on the i-th card.\nYour objective is to guess A_1, A_2, ..., A_N correctly.\nYou know the following facts:\n - For each i = 1, 2, ..., M, the value A_{X_i} + A_{Y_i} + Z_i is an even number.\nYou are a magician and can use the following magic any number of times:\nMagic: Choose one card and know the integer A_i written on it. The cost of using this magic is 1.\nWhat is the minimum cost required to determine all of A_1, A_2, ..., A_N?\nIt is guaranteed that there is no contradiction in given input.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^5\n - 1 \\leq X_i < Y_i \\leq N\n - 1 \\leq Z_i \\leq 100\n - The pairs (X_i, Y_i) are distinct.\n - There is no contradiction in input. (That is, there exist integers A_1, A_2, ..., A_N that satisfy the conditions.)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nX_1 Y_1 Z_1\nX_2 Y_2 Z_2\n\\vdots\nX_M Y_M Z_M\n\n-----Output-----\nPrint the minimum total cost required to determine all of A_1, A_2, ..., A_N.\n\n-----Sample Input-----\n3 1\n1 2 1\n\n-----Sample Output-----\n2\n\nYou can determine all of A_1, A_2, A_3 by using the magic for the first and third cards."} +{"id": "01581", "text": "Find the number of sequences of length K consisting of positive integers such that the product of any two adjacent elements is at most N, modulo 10^9+7.\n\n-----Constraints-----\n - 1\\leq N\\leq 10^9\n - 1 2\\leq K\\leq 100 (fixed at 21:33 JST)\n - N and K are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\n\n-----Output-----\nPrint the number of sequences, modulo 10^9+7.\n\n-----Sample Input-----\n3 2\n\n-----Sample Output-----\n5\n\n(1,1), (1,2), (1,3), (2,1), and (3,1) satisfy the condition."} +{"id": "01582", "text": "Given is a positive integer N.\n\nFind the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:\n - When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n25\n\n-----Sample Output-----\n17\n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22)."} +{"id": "01583", "text": "Takahashi has a water bottle with the shape of a rectangular prism whose base is a square of side a~\\mathrm{cm} and whose height is b~\\mathrm{cm}. (The thickness of the bottle can be ignored.)\nWe will pour x~\\mathrm{cm}^3 of water into the bottle, and gradually tilt the bottle around one of the sides of the base.\nWhen will the water be spilled? More formally, find the maximum angle in which we can tilt the bottle without spilling any water.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq a \\leq 100\n - 1 \\leq b \\leq 100\n - 1 \\leq x \\leq a^2b\n\n-----Input-----\nInput is given from Standard Input in the following format:\na b x\n\n-----Output-----\nPrint the maximum angle in which we can tilt the bottle without spilling any water, in degrees.\nYour output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6}.\n\n-----Sample Input-----\n2 2 4\n\n-----Sample Output-----\n45.0000000000\n\nThis bottle has a cubic shape, and it is half-full. The water gets spilled when we tilt the bottle more than 45 degrees."} +{"id": "01584", "text": "Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.\nHe is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:\n - a < b + c\n - b < c + a\n - c < a + b\nHow many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.\n\n-----Constraints-----\n - All values in input are integers.\n - 3 \\leq N \\leq 2 \\times 10^3\n - 1 \\leq L_i \\leq 10^3\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nL_1 L_2 ... L_N\n\n-----Constraints-----\nPrint the number of different triangles that can be formed.\n\n-----Sample Input-----\n4\n3 4 2 1\n\n-----Sample Output-----\n1\n\nOnly one triangle can be formed: the triangle formed by the first, second, and third sticks."} +{"id": "01585", "text": "As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.\nThe sequence A needs to satisfy the conditions below:\n - A consists of integers between X and Y (inclusive).\n - For each 1\\leq i \\leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.\nFind the maximum possible length of the sequence.\n\n-----Constraints-----\n - 1 \\leq X \\leq Y \\leq 10^{18}\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y\n\n-----Output-----\nPrint the maximum possible length of the sequence.\n\n-----Sample Input-----\n3 20\n\n-----Sample Output-----\n3\n\nThe sequence 3,6,18 satisfies the conditions."} +{"id": "01586", "text": "For an integer n not less than 0, let us define f(n) as follows:\n - f(n) = 1 (if n < 2)\n - f(n) = n f(n-2) (if n \\geq 2)\nGiven is an integer N. Find the number of trailing zeros in the decimal notation of f(N).\n\n-----Constraints-----\n - 0 \\leq N \\leq 10^{18}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the number of trailing zeros in the decimal notation of f(N).\n\n-----Sample Input-----\n12\n\n-----Sample Output-----\n1\n\nf(12) = 12 \u00d7 10 \u00d7 8 \u00d7 6 \u00d7 4 \u00d7 2 = 46080, which has one trailing zero."} +{"id": "01587", "text": "An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \\leq i \\leq N) is given to you as a character c_i; R stands for red and W stands for white.\nYou can do the following two kinds of operations any number of times in any order:\n - Choose two stones (not necessarily adjacent) and swap them.\n - Choose one stone and change its color (from red to white and vice versa).\nAccording to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?\n\n-----Constraints-----\n - 2 \\leq N \\leq 200000\n - c_i is R or W.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nc_{1}c_{2}...c_{N}\n\n-----Output-----\nPrint an integer representing the minimum number of operations needed.\n\n-----Sample Input-----\n4\nWWRR\n\n-----Sample Output-----\n2\n\nFor example, the two operations below will achieve the objective.\n - Swap the 1-st and 3-rd stones from the left, resulting in RWWR.\n - Change the color of the 4-th stone from the left, resulting in RWWW."} +{"id": "01588", "text": "Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris.\n\nThere are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds: $\\sum_{x \\in X}(x - 1) = \\sum_{y \\in Y}(s - y)$ \"Are you kidding me?\", asks Chris.\n\nFor example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7.\n\n $\\left. \\begin{array}{l l l l l l}{1} & {2} & {3} & {4} & {5} & {6} & {7} & {8} \\end{array} \\right.$ \n\nHowever, now Chris has exactly s = 10^6 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y!\n\n\n-----Input-----\n\nThe first line of input contains a single integer n (1 \u2264 n \u2264 5\u00b710^5), the number of blocks in the set X. The next line contains n distinct space-separated integers x_1, x_2, ..., x_{n} (1 \u2264 x_{i} \u2264 10^6), the numbers of the blocks in X.\n\nNote: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.\n\n\n-----Output-----\n\nIn the first line of output print a single integer m (1 \u2264 m \u2264 10^6 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y_1, y_2, ..., y_{m} (1 \u2264 y_{i} \u2264 10^6), such that the required equality holds. The sets X and Y should not intersect, i.e. x_{i} \u2260 y_{j} for all i, j (1 \u2264 i \u2264 n; 1 \u2264 j \u2264 m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them.\n\n\n-----Examples-----\nInput\n3\n1 4 5\n\nOutput\n2\n999993 1000000\nInput\n1\n1\n\nOutput\n1\n1000000"} +{"id": "01589", "text": "One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.\n\nVitaly sees a building of n floors and 2\u00b7m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2\u00b7m from left to right, then the j-th flat of the i-th floor has windows 2\u00b7j - 1 and 2\u00b7j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.\n\nGiven the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 100)\u00a0\u2014 the number of floors in the house and the number of flats on each floor respectively.\n\nNext n lines describe the floors from top to bottom and contain 2\u00b7m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping.\n\n\n-----Examples-----\nInput\n2 2\n0 0 0 1\n1 0 1 1\n\nOutput\n3\n\nInput\n1 3\n1 1 0 1 0 0\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.\n\nIn the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off."} +{"id": "01590", "text": "You are given an array $a_1, a_2, \\dots, a_n$. All $a_i$ are pairwise distinct.\n\nLet's define function $f(l, r)$ as follows: let's define array $b_1, b_2, \\dots, b_{r - l + 1}$, where $b_i = a_{l - 1 + i}$; sort array $b$ in increasing order; result of the function $f(l, r)$ is $\\sum\\limits_{i = 1}^{r - l + 1}{b_i \\cdot i}$. \n\nCalculate $\\left(\\sum\\limits_{1 \\le l \\le r \\le n}{f(l, r)}\\right) \\mod (10^9+7)$, i.e. total sum of $f$ for all subsegments of $a$ modulo $10^9+7$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 5 \\cdot 10^5$) \u2014 the length of array $a$.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$, $a_i \\neq a_j$ for $i \\neq j$) \u2014 array $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the total sum of $f$ for all subsegments of $a$ modulo $10^9+7$\n\n\n-----Examples-----\nInput\n4\n5 2 4 7\n\nOutput\n167\n\nInput\n3\n123456789 214365879 987654321\n\nOutput\n582491518\n\n\n\n-----Note-----\n\nDescription of the first example: $f(1, 1) = 5 \\cdot 1 = 5$; $f(1, 2) = 2 \\cdot 1 + 5 \\cdot 2 = 12$; $f(1, 3) = 2 \\cdot 1 + 4 \\cdot 2 + 5 \\cdot 3 = 25$; $f(1, 4) = 2 \\cdot 1 + 4 \\cdot 2 + 5 \\cdot 3 + 7 \\cdot 4 = 53$; $f(2, 2) = 2 \\cdot 1 = 2$; $f(2, 3) = 2 \\cdot 1 + 4 \\cdot 2 = 10$; $f(2, 4) = 2 \\cdot 1 + 4 \\cdot 2 + 7 \\cdot 3 = 31$; $f(3, 3) = 4 \\cdot 1 = 4$; $f(3, 4) = 4 \\cdot 1 + 7 \\cdot 2 = 18$; $f(4, 4) = 7 \\cdot 1 = 7$;"} +{"id": "01591", "text": "Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated?\n\nThere are $n$ students living in a building, and for each of them the favorite drink $a_i$ is known. So you know $n$ integers $a_1, a_2, \\dots, a_n$, where $a_i$ ($1 \\le a_i \\le k$) is the type of the favorite drink of the $i$-th student. The drink types are numbered from $1$ to $k$.\n\nThere are infinite number of drink sets. Each set consists of exactly two portions of the same drink. In other words, there are $k$ types of drink sets, the $j$-th type contains two portions of the drink $j$. The available number of sets of each of the $k$ types is infinite.\n\nYou know that students will receive the minimum possible number of sets to give all students exactly one drink. Obviously, the number of sets will be exactly $\\lceil \\frac{n}{2} \\rceil$, where $\\lceil x \\rceil$ is $x$ rounded up.\n\nAfter students receive the sets, they will distribute their portions by their choice: each student will get exactly one portion. Note, that if $n$ is odd then one portion will remain unused and the students' teacher will drink it.\n\nWhat is the maximum number of students that can get their favorite drink if $\\lceil \\frac{n}{2} \\rceil$ sets will be chosen optimally and students will distribute portions between themselves optimally?\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n, k \\le 1\\,000$) \u2014 the number of students in the building and the number of different drinks.\n\nThe next $n$ lines contain student's favorite drinks. The $i$-th line contains a single integer from $1$ to $k$ \u2014 the type of the favorite drink of the $i$-th student.\n\n\n-----Output-----\n\nPrint exactly one integer \u2014 the maximum number of students that can get a favorite drink.\n\n\n-----Examples-----\nInput\n5 3\n1\n3\n1\n1\n2\n\nOutput\n4\n\nInput\n10 3\n2\n1\n3\n2\n3\n3\n1\n3\n1\n2\n\nOutput\n9\n\n\n\n-----Note-----\n\nIn the first example, students could choose three sets with drinks $1$, $1$ and $2$ (so they will have two sets with two drinks of the type $1$ each and one set with two drinks of the type $2$, so portions will be $1, 1, 1, 1, 2, 2$). This way all students except the second one will get their favorite drinks.\n\nAnother possible answer is sets with drinks $1$, $2$ and $3$. In this case the portions will be $1, 1, 2, 2, 3, 3$. Then all the students except one will gain their favorite drinks. The only student that will not gain the favorite drink will be a student with $a_i = 1$ (i.e. the first, the third or the fourth)."} +{"id": "01592", "text": "Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. \n\nFor that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers t_{i} and c_{i} \u2014 the receiving time (the second) and the number of the text messages, correspondingly.\n\nPolycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows:\n\n If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x. If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue. \n\nGiven the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^3) \u2014 the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers t_{i} and c_{i} (1 \u2264 t_{i}, c_{i} \u2264 10^6) \u2014 the time (the second) when the i-th task was received and the number of messages to send, correspondingly.\n\nIt is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, t_{i} < t_{i} + 1 for all integer i (1 \u2264 i < n).\n\n\n-----Output-----\n\nIn a single line print two space-separated integers \u2014 the time when the last text message was sent and the maximum queue size at a certain moment of time.\n\n\n-----Examples-----\nInput\n2\n1 1\n2 1\n\nOutput\n3 1\n\nInput\n1\n1000000 10\n\nOutput\n1000010 10\n\nInput\n3\n3 3\n4 3\n5 3\n\nOutput\n12 7\n\n\n\n-----Note-----\n\nIn the first test sample: \n\n second 1: the first message has appeared in the queue, the queue's size is 1; second 2: the first message is sent, the second message has been received, the queue's size is 1; second 3: the second message is sent, the queue's size is 0, \n\nThus, the maximum size of the queue is 1, the last message was sent at the second 3."} +{"id": "01593", "text": "The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city.\n\nThe city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (x_{i}, y_{i}) with the population of k_{i} people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city.\n\nYour goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and s (1 \u2264 n \u2264 10^3; 1 \u2264 s < 10^6) \u2014 the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers \u2014 the x_{i} and y_{i} coordinate values of the i-th location and the number k_{i} of people in it (1 \u2264 k_{i} < 10^6). Each coordinate is an integer and doesn't exceed 10^4 in its absolute value.\n\nIt is guaranteed that no two locations are at the same point and no location is at point (0;\u00a00).\n\n\n-----Output-----\n\nIn the output, print \"-1\" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number \u2014 the minimum radius of the circle that the city needs to expand to in order to become a megacity.\n\nThe answer is considered correct if the absolute or relative error don't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n4 999998\n1 1 1\n2 2 1\n3 3 1\n2 -2 1\n\nOutput\n2.8284271\n\nInput\n4 999998\n1 1 2\n2 2 1\n3 3 1\n2 -2 1\n\nOutput\n1.4142136\n\nInput\n2 1\n1 1 999997\n2 2 1\n\nOutput\n-1"} +{"id": "01594", "text": "Eugeny loves listening to music. He has n songs in his play list. We know that song number i has the duration of t_{i} minutes. Eugeny listens to each song, perhaps more than once. He listens to song number i c_{i} times. Eugeny's play list is organized as follows: first song number 1 plays c_1 times, then song number 2 plays c_2 times, ..., in the end the song number n plays c_{n} times.\n\nEugeny took a piece of paper and wrote out m moments of time when he liked a song. Now for each such moment he wants to know the number of the song that played at that moment. The moment x means that Eugeny wants to know which song was playing during the x-th minute of his listening to the play list.\n\nHelp Eugeny and calculate the required numbers of songs.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 10^5). The next n lines contain pairs of integers. The i-th line contains integers c_{i}, t_{i} (1 \u2264 c_{i}, t_{i} \u2264 10^9) \u2014 the description of the play list. It is guaranteed that the play list's total duration doesn't exceed 10^9 $(\\sum_{i = 1}^{n} c_{i} \\cdot t_{i} \\leq 10^{9})$.\n\nThe next line contains m positive integers v_1, v_2, ..., v_{m}, that describe the moments Eugeny has written out. It is guaranteed that there isn't such moment of time v_{i}, when the music doesn't play any longer. It is guaranteed that v_{i} < v_{i} + 1 (i < m).\n\nThe moment of time v_{i} means that Eugeny wants to know which song was playing during the v_{i}-th munite from the start of listening to the playlist.\n\n\n-----Output-----\n\nPrint m integers \u2014 the i-th number must equal the number of the song that was playing during the v_{i}-th minute after Eugeny started listening to the play list.\n\n\n-----Examples-----\nInput\n1 2\n2 8\n1 16\n\nOutput\n1\n1\n\nInput\n4 9\n1 2\n2 1\n1 1\n2 2\n1 2 3 4 5 6 7 8 9\n\nOutput\n1\n1\n2\n2\n3\n4\n4\n4\n4"} +{"id": "01595", "text": "At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.\n\nFortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of $\\sum_{x \\in S} \\text{lowbit}(x)$ was equal to sum; here lowbit(x) equals 2^{k} where k is the position of the first one in the binary representation of x. For example, lowbit(10010_2) = 10_2, lowbit(10001_2) = 1_2, lowbit(10000_2) = 10000_2 (binary representation). \n\nCan you help Picks and find any set S, that satisfies all the above conditions?\n\n\n-----Input-----\n\nThe first line contains two integers: sum, limit (1 \u2264 sum, limit \u2264 10^5).\n\n\n-----Output-----\n\nIn the first line print an integer n (1 \u2264 n \u2264 10^5), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.\n\nIf it's impossible to find a suitable set, print -1.\n\n\n-----Examples-----\nInput\n5 5\n\nOutput\n2\n4 5\n\nInput\n4 3\n\nOutput\n3\n2 3 1\n\nInput\n5 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.\n\nIn sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4."} +{"id": "01596", "text": "Constanze is the smartest girl in her village but she has bad eyesight.\n\nOne day, she was able to invent an incredible machine! When you pronounce letters, the machine will inscribe them onto a piece of paper. For example, if you pronounce 'c', 'o', 'd', and 'e' in that order, then the machine will inscribe \"code\" onto the paper. Thanks to this machine, she can finally write messages without using her glasses.\n\nHowever, her dumb friend Akko decided to play a prank on her. Akko tinkered with the machine so that if you pronounce 'w', it will inscribe \"uu\" instead of \"w\", and if you pronounce 'm', it will inscribe \"nn\" instead of \"m\"! Since Constanze had bad eyesight, she was not able to realize what Akko did.\n\nThe rest of the letters behave the same as before: if you pronounce any letter besides 'w' and 'm', the machine will just inscribe it onto a piece of paper.\n\nThe next day, I received a letter in my mailbox. I can't understand it so I think it's either just some gibberish from Akko, or Constanze made it using her machine. But since I know what Akko did, I can just list down all possible strings that Constanze's machine would have turned into the message I got and see if anything makes sense.\n\nBut I need to know how much paper I will need, and that's why I'm asking you for help. Tell me the number of strings that Constanze's machine would've turned into the message I got.\n\nBut since this number can be quite large, tell me instead its remainder when divided by $10^9+7$.\n\nIf there are no strings that Constanze's machine would've turned into the message I got, then print $0$.\n\n\n-----Input-----\n\nInput consists of a single line containing a string $s$ ($1 \\leq |s| \\leq 10^5$) \u2014 the received message. $s$ contains only lowercase Latin letters.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of strings that Constanze's machine would've turned into the message $s$, modulo $10^9+7$.\n\n\n-----Examples-----\nInput\nouuokarinn\n\nOutput\n4\n\nInput\nbanana\n\nOutput\n1\n\nInput\nnnn\n\nOutput\n3\n\nInput\namanda\n\nOutput\n0\n\n\n\n-----Note-----\n\nFor the first example, the candidate strings are the following: \"ouuokarinn\", \"ouuokarim\", \"owokarim\", and \"owokarinn\".\n\nFor the second example, there is only one: \"banana\".\n\nFor the third example, the candidate strings are the following: \"nm\", \"mn\" and \"nnn\".\n\nFor the last example, there are no candidate strings that the machine can turn into \"amanda\", since the machine won't inscribe 'm'."} +{"id": "01597", "text": "Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the \"Hobbit\" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.\n\nThere are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as $\\operatorname{lcp}(a, b)$. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.\n\nFind the matching between students and pseudonyms with the maximum quality.\n\n\n-----Input-----\n\nThe first line contains number n (1 \u2264 n \u2264 100 000) \u2014 the number of students in the summer school.\n\nNext n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.\n\nThe last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.\n\nThe total length of all the names and pseudonyms doesn't exceed 800 000 characters.\n\n\n-----Output-----\n\nIn the first line print the maximum possible quality of matching pseudonyms to students.\n\nIn the next n lines describe the optimal matching. Each line must have the form a b (1 \u2264 a, b \u2264 n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.\n\nThe matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.\n\n\n-----Examples-----\nInput\n5\ngennady\ngalya\nboris\nbill\ntoshik\nbilbo\ntorin\ngendalf\nsmaug\ngaladriel\n\nOutput\n11\n4 1\n2 5\n1 3\n5 2\n3 4\n\n\n\n-----Note-----\n\nThe first test from the statement the match looks as follows: bill \u2192 bilbo (lcp = 3) galya \u2192 galadriel (lcp = 3) gennady \u2192 gendalf (lcp = 3) toshik \u2192 torin (lcp = 2) boris \u2192 smaug (lcp = 0)"} +{"id": "01598", "text": "The only difference between easy and hard versions is the length of the string. You can hack this problem only if you solve both problems.\n\nKirk has a binary string $s$ (a string which consists of zeroes and ones) of length $n$ and he is asking you to find a binary string $t$ of the same length which satisfies the following conditions:\n\n\n\n For any $l$ and $r$ ($1 \\leq l \\leq r \\leq n$) the length of the longest non-decreasing subsequence of the substring $s_{l}s_{l+1} \\ldots s_{r}$ is equal to the length of the longest non-decreasing subsequence of the substring $t_{l}t_{l+1} \\ldots t_{r}$;\n\n The number of zeroes in $t$ is the maximum possible.\n\nA non-decreasing subsequence of a string $p$ is a sequence of indices $i_1, i_2, \\ldots, i_k$ such that $i_1 < i_2 < \\ldots < i_k$ and $p_{i_1} \\leq p_{i_2} \\leq \\ldots \\leq p_{i_k}$. The length of the subsequence is $k$.\n\nIf there are multiple substrings which satisfy the conditions, output any.\n\n\n-----Input-----\n\nThe first line contains a binary string of length not more than $2\\: 000$.\n\n\n-----Output-----\n\nOutput a binary string which satisfied the above conditions. If there are many such strings, output any of them.\n\n\n-----Examples-----\nInput\n110\n\nOutput\n010\n\nInput\n010\n\nOutput\n010\n\nInput\n0001111\n\nOutput\n0000000\n\nInput\n0111001100111011101000\n\nOutput\n0011001100001011101000\n\n\n\n-----Note-----\n\nIn the first example: \n\n For the substrings of the length $1$ the length of the longest non-decreasing subsequnce is $1$; For $l = 1, r = 2$ the longest non-decreasing subsequnce of the substring $s_{1}s_{2}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{2}$ is $01$; For $l = 1, r = 3$ the longest non-decreasing subsequnce of the substring $s_{1}s_{3}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{3}$ is $00$; For $l = 2, r = 3$ the longest non-decreasing subsequnce of the substring $s_{2}s_{3}$ is $1$ and the longest non-decreasing subsequnce of the substring $t_{2}t_{3}$ is $1$; \n\nThe second example is similar to the first one."} +{"id": "01599", "text": "Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.\n\nYou've got string s = s_1s_2... s_{n} (n is the length of the string), consisting only of characters \".\" and \"#\" and m queries. Each query is described by a pair of integers l_{i}, r_{i} (1 \u2264 l_{i} < r_{i} \u2264 n). The answer to the query l_{i}, r_{i} is the number of such integers i (l_{i} \u2264 i < r_{i}), that s_{i} = s_{i} + 1.\n\nIlya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.\n\n\n-----Input-----\n\nThe first line contains string s of length n (2 \u2264 n \u2264 10^5). It is guaranteed that the given string only consists of characters \".\" and \"#\".\n\nThe next line contains integer m (1 \u2264 m \u2264 10^5) \u2014 the number of queries. Each of the next m lines contains the description of the corresponding query. The i-th line contains integers l_{i}, r_{i} (1 \u2264 l_{i} < r_{i} \u2264 n).\n\n\n-----Output-----\n\nPrint m integers \u2014 the answers to the queries in the order in which they are given in the input.\n\n\n-----Examples-----\nInput\n......\n4\n3 4\n2 3\n1 6\n2 6\n\nOutput\n1\n1\n5\n4\n\nInput\n#..###\n5\n1 3\n5 6\n1 5\n3 6\n3 4\n\nOutput\n1\n1\n2\n2\n0"} +{"id": "01600", "text": "One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.\n\nAt the end of the day there were n castles built by friends. Castles are numbered from 1 to n, and the height of the i-th castle is equal to h_{i}. When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that condition h_{i} \u2264 h_{i} + 1 holds for all i from 1 to n - 1.\n\nSquidward suggested the following process of sorting castles: Castles are split into blocks\u00a0\u2014 groups of consecutive castles. Therefore the block from i to j will include castles i, i + 1, ..., j. A block may consist of a single castle. The partitioning is chosen in such a way that every castle is a part of exactly one block. Each block is sorted independently from other blocks, that is the sequence h_{i}, h_{i} + 1, ..., h_{j} becomes sorted. The partitioning should satisfy the condition that after each block is sorted, the sequence h_{i} becomes sorted too. This may always be achieved by saying that the whole sequence is a single block. \n\nEven Patrick understands that increasing the number of blocks in partitioning will ease the sorting process. Now friends ask you to count the maximum possible number of blocks in a partitioning that satisfies all the above requirements.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of castles Spongebob, Patrick and Squidward made from sand during the day.\n\nThe next line contains n integers h_{i} (1 \u2264 h_{i} \u2264 10^9). The i-th of these integers corresponds to the height of the i-th castle.\n\n\n-----Output-----\n\nPrint the maximum possible number of blocks in a valid partitioning.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n3\n\nInput\n4\n2 1 3 2\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample the partitioning looks like that: [1][2][3].\n\n [Image] \n\nIn the second sample the partitioning is: [2, 1][3, 2]\n\n [Image]"} +{"id": "01601", "text": "This is a harder version of the problem. In this version, $n \\le 50\\,000$.\n\nThere are $n$ distinct points in three-dimensional space numbered from $1$ to $n$. The $i$-th point has coordinates $(x_i, y_i, z_i)$. The number of points $n$ is even.\n\nYou'd like to remove all $n$ points using a sequence of $\\frac{n}{2}$ snaps. In one snap, you can remove any two points $a$ and $b$ that have not been removed yet and form a perfectly balanced pair. A pair of points $a$ and $b$ is perfectly balanced if no other point $c$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $a$ and $b$.\n\nFormally, point $c$ lies within the axis-aligned minimum bounding box of points $a$ and $b$ if and only if $\\min(x_a, x_b) \\le x_c \\le \\max(x_a, x_b)$, $\\min(y_a, y_b) \\le y_c \\le \\max(y_a, y_b)$, and $\\min(z_a, z_b) \\le z_c \\le \\max(z_a, z_b)$. Note that the bounding box might be degenerate. \n\nFind a way to remove all points in $\\frac{n}{2}$ snaps.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 50\\,000$; $n$ is even), denoting the number of points.\n\nEach of the next $n$ lines contains three integers $x_i$, $y_i$, $z_i$ ($-10^8 \\le x_i, y_i, z_i \\le 10^8$), denoting the coordinates of the $i$-th point.\n\nNo two points coincide.\n\n\n-----Output-----\n\nOutput $\\frac{n}{2}$ pairs of integers $a_i, b_i$ ($1 \\le a_i, b_i \\le n$), denoting the indices of points removed on snap $i$. Every integer between $1$ and $n$, inclusive, must appear in your output exactly once.\n\nWe can show that it is always possible to remove all points. If there are many solutions, output any of them.\n\n\n-----Examples-----\nInput\n6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0\n\nOutput\n3 6\n5 1\n2 4\n\nInput\n8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3\n\nOutput\n4 5\n1 6\n2 7\n3 8\n\n\n\n-----Note-----\n\nIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $z = 0$ plane). Note that order of removing matters: for example, points $5$ and $1$ don't form a perfectly balanced pair initially, but they do after point $3$ is removed. [Image]"} +{"id": "01602", "text": "Anu has created her own function $f$: $f(x, y) = (x | y) - y$ where $|$ denotes the bitwise OR operation. For example, $f(11, 6) = (11|6) - 6 = 15 - 6 = 9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x, y)$ is also nonnegative. \n\nShe would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.\n\nA value of an array $[a_1, a_2, \\dots, a_n]$ is defined as $f(f(\\dots f(f(a_1, a_2), a_3), \\dots a_{n-1}), a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$).\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 10^9$). Elements of the array are not guaranteed to be different.\n\n\n-----Output-----\n\nOutput $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n4\n4 0 11 6\n\nOutput\n11 6 4 0\nInput\n1\n13\n\nOutput\n13 \n\n\n-----Note-----\n\nIn the first testcase, value of the array $[11, 6, 4, 0]$ is $f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$.\n\n$[11, 4, 0, 6]$ is also a valid answer."} +{"id": "01603", "text": "Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is v_{i}. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and r\u00a0(1 \u2264 l \u2264 r \u2264 n), and you should tell her $\\sum_{i = l}^{r} v_{i}$. Let u_{i} be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and r\u00a0(1 \u2264 l \u2264 r \u2264 n), and you should tell her $\\sum_{i = l}^{r} u_{i}$. \n\nFor every question you should give the correct answer, or Kuriyama Mirai will say \"fuyukai desu\" and then become unhappy.\n\n\n-----Input-----\n\nThe first line contains an integer n\u00a0(1 \u2264 n \u2264 10^5). The second line contains n integers: v_1, v_2, ..., v_{n}\u00a0(1 \u2264 v_{i} \u2264 10^9) \u2014 costs of the stones. \n\nThe third line contains an integer m\u00a0(1 \u2264 m \u2264 10^5) \u2014 the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and r\u00a0(1 \u2264 l \u2264 r \u2264 n;\u00a01 \u2264 type \u2264 2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one.\n\n\n-----Output-----\n\nPrint m lines. Each line must contain an integer \u2014 the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input.\n\n\n-----Examples-----\nInput\n6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6\n\nOutput\n24\n9\n28\n\nInput\n4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2\n\nOutput\n10\n15\n5\n15\n5\n5\n2\n12\n3\n5\n\n\n\n-----Note-----\n\nPlease note that the answers to the questions may overflow 32-bit integer type."} +{"id": "01604", "text": "Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened.\n\nOri was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door.\n\nThere are $n$ lamps with Spirit Tree's light. Sein knows the time of turning on and off for the $i$-th lamp\u00a0\u2014 $l_i$ and $r_i$ respectively. To open the door you have to choose $k$ lamps in such a way that there will be a moment of time when they all will be turned on.\n\nWhile Sein decides which of the $k$ lamps to pick, Ori is interested: how many ways there are to pick such $k$ lamps that the door will open? It may happen that Sein may be wrong and there are no such $k$ lamps. The answer might be large, so print it modulo $998\\,244\\,353$.\n\n\n-----Input-----\n\nFirst line contains two integers $n$ and $k$ ($1 \\le n \\le 3 \\cdot 10^5$, $1 \\le k \\le n$)\u00a0\u2014 total number of lamps and the number of lamps that must be turned on simultaneously.\n\nNext $n$ lines contain two integers $l_i$ ans $r_i$ ($1 \\le l_i \\le r_i \\le 10^9$)\u00a0\u2014 period of time when $i$-th lamp is turned on.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the answer to the task modulo $998\\,244\\,353$.\n\n\n-----Examples-----\nInput\n7 3\n1 7\n3 8\n4 5\n6 7\n1 3\n5 10\n8 9\n\nOutput\n9\nInput\n3 1\n1 1\n2 2\n3 3\n\nOutput\n3\nInput\n3 2\n1 1\n2 2\n3 3\n\nOutput\n0\nInput\n3 3\n1 3\n2 3\n3 3\n\nOutput\n1\nInput\n5 2\n1 3\n2 4\n3 5\n4 6\n5 7\n\nOutput\n7\n\n\n-----Note-----\n\nIn first test case there are nine sets of $k$ lamps: $(1, 2, 3)$, $(1, 2, 4)$, $(1, 2, 5)$, $(1, 2, 6)$, $(1, 3, 6)$, $(1, 4, 6)$, $(2, 3, 6)$, $(2, 4, 6)$, $(2, 6, 7)$.\n\nIn second test case $k=1$, so the answer is 3.\n\nIn third test case there are no such pairs of lamps.\n\nIn forth test case all lamps are turned on in a time $3$, so the answer is 1.\n\nIn fifth test case there are seven sets of $k$ lamps: $(1, 2)$, $(1, 3)$, $(2, 3)$, $(2, 4)$, $(3, 4)$, $(3, 5)$, $(4, 5)$."} +{"id": "01605", "text": "We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, \"aabba\" is good, because after the merging step it will become \"aba\".\n\nGiven a string, you have to find two values: the number of good substrings of even length; the number of good substrings of odd length. \n\n\n-----Input-----\n\nThe first line of the input contains a single string of length n (1 \u2264 n \u2264 10^5). Each character of the string will be either 'a' or 'b'.\n\n\n-----Output-----\n\nPrint two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.\n\n\n-----Examples-----\nInput\nbb\n\nOutput\n1 2\n\nInput\nbaab\n\nOutput\n2 4\n\nInput\nbabb\n\nOutput\n2 5\n\nInput\nbabaa\n\nOutput\n2 7\n\n\n\n-----Note-----\n\nIn example 1, there are three good substrings (\"b\", \"b\", and \"bb\"). One of them has even length and two of them have odd length.\n\nIn example 2, there are six good substrings (i.e. \"b\", \"a\", \"a\", \"b\", \"aa\", \"baab\"). Two of them have even length and four of them have odd length.\n\nIn example 3, there are seven good substrings (i.e. \"b\", \"a\", \"b\", \"b\", \"bb\", \"bab\", \"babb\"). Two of them have even length and five of them have odd length.\n\nDefinitions\n\nA substring s[l, r] (1 \u2264 l \u2264 r \u2264 n) of string s = s_1s_2... s_{n} is string s_{l}s_{l} + 1... s_{r}.\n\nA string s = s_1s_2... s_{n} is a palindrome if it is equal to string s_{n}s_{n} - 1... s_1."} +{"id": "01606", "text": "Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.\n\nThe dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n \u00d7 n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.\n\nFortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A: $\\left(\\begin{array}{l l l}{1} & {1} & {1} \\\\{0} & {1} & {1} \\\\{1} & {0} & {0} \\end{array} \\right)$ \n\nThe unusual square of A is equal to (1\u00b71 + 1\u00b70 + 1\u00b71) + (0\u00b71 + 1\u00b71 + 1\u00b70) + (1\u00b71 + 0\u00b71 + 0\u00b70) = 0 + 1 + 1 = 0.\n\nHowever, there is much more to the homework. Chris has to process q queries; each query can be one of the following: given a row index i, flip all the values in the i-th row in A; given a column index i, flip all the values in the i-th column in A; find the unusual square of A. \n\nTo flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.\n\nGiven the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?\n\n\n-----Input-----\n\nThe first line of input contains an integer n (1 \u2264 n \u2264 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line a_{ij} (0 \u2264 a_{ij} \u2264 1) is the element on the intersection of the i-th row and the j-th column of A.\n\nThe next line of input contains an integer q (1 \u2264 q \u2264 10^6), the number of queries. Each of the next q lines describes a single query, which can be one of the following: 1 i \u2014 flip the values of the i-th row; 2 i \u2014 flip the values of the i-th column; 3 \u2014 output the unusual square of A. \n\nNote: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.\n\n\n-----Output-----\n\nLet the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.\n\n\n-----Examples-----\nInput\n3\n1 1 1\n0 1 1\n1 0 0\n12\n3\n2 3\n3\n2 2\n2 2\n1 3\n3\n3\n1 2\n2 1\n1 1\n3\n\nOutput\n01001"} +{"id": "01607", "text": "You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white.\n\nTwo cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$.\n\nLet's call some bicoloring beautiful if it has exactly $k$ components.\n\nCount the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$.\n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $k$ ($1 \\le n \\le 1000$, $1 \\le k \\le 2n$) \u2014 the number of columns in a grid and the number of components required.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of beautiful bicolorings modulo $998244353$.\n\n\n-----Examples-----\nInput\n3 4\n\nOutput\n12\n\nInput\n4 1\n\nOutput\n2\n\nInput\n1 2\n\nOutput\n2\n\n\n\n-----Note-----\n\nOne of possible bicolorings in sample $1$: [Image]"} +{"id": "01608", "text": "Let's call a non-empty sequence of positive integers a_1, a_2... a_{k} coprime if the greatest common divisor of all elements of this sequence is equal to 1.\n\nGiven an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 10^9 + 7.\n\nNote that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1].\n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 100000).\n\nThe second line contains n integer numbers a_1, a_2... a_{n} (1 \u2264 a_{i} \u2264 100000).\n\n\n-----Output-----\n\nPrint the number of coprime subsequences of a modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n5\n\nInput\n4\n1 1 1 1\n\nOutput\n15\n\nInput\n7\n1 3 5 15 3 105 35\n\nOutput\n100\n\n\n\n-----Note-----\n\nIn the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 \n\nIn the second example all subsequences are coprime."} +{"id": "01609", "text": "Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep the track of everything.\n\nDuring an audit, you were surprised to find out that the items are not numbered sequentially, and some items even share the same inventory number! There is an urgent need to fix it. You have chosen to make the numbers of the items sequential, starting with 1. Changing a number is quite a time-consuming process, and you would like to make maximum use of the current numbering.\n\nYou have been given information on current inventory numbers for n items in the company. Renumber items so that their inventory numbers form a permutation of numbers from 1 to n by changing the number of as few items as possible. Let us remind you that a set of n numbers forms a permutation if all the numbers are in the range from 1 to n, and no two numbers are equal.\n\n\n-----Input-----\n\nThe first line contains a single integer n\u00a0\u2014 the number of items (1 \u2264 n \u2264 10^5).\n\nThe second line contains n numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5)\u00a0\u2014 the initial inventory numbers of the items.\n\n\n-----Output-----\n\nPrint n numbers\u00a0\u2014 the final inventory numbers of the items in the order they occur in the input. If there are multiple possible answers, you may print any of them.\n\n\n-----Examples-----\nInput\n3\n1 3 2\n\nOutput\n1 3 2 \n\nInput\n4\n2 2 3 3\n\nOutput\n2 1 3 4 \n\nInput\n1\n2\n\nOutput\n1 \n\n\n\n-----Note-----\n\nIn the first test the numeration is already a permutation, so there is no need to change anything.\n\nIn the second test there are two pairs of equal numbers, in each pair you need to replace one number.\n\nIn the third test you need to replace 2 by 1, as the numbering should start from one."} +{"id": "01610", "text": "Permutation p is an ordered set of integers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as p_{i}. We'll call number n the size or the length of permutation p_1, p_2, ..., p_{n}.\n\nThe decreasing coefficient of permutation p_1, p_2, ..., p_{n} is the number of such i (1 \u2264 i < n), that p_{i} > p_{i} + 1.\n\nYou have numbers n and k. Your task is to print the permutation of length n with decreasing coefficient k.\n\n\n-----Input-----\n\nThe single line contains two space-separated integers: n, k (1 \u2264 n \u2264 10^5, 0 \u2264 k < n) \u2014 the permutation length and the decreasing coefficient.\n\n\n-----Output-----\n\nIn a single line print n space-separated integers: p_1, p_2, ..., p_{n} \u2014 the permutation of length n with decreasing coefficient k. \n\nIf there are several permutations that meet this condition, print any of them. It is guaranteed that the permutation with the sought parameters exists.\n\n\n-----Examples-----\nInput\n5 2\n\nOutput\n1 5 2 4 3\n\nInput\n3 0\n\nOutput\n1 2 3\n\nInput\n3 2\n\nOutput\n3 2 1"} +{"id": "01611", "text": "[Image] \n\nAs some of you know, cubism is a trend in art, where the problem of constructing volumetrical shape on a plane with a combination of three-dimensional geometric shapes comes to the fore. \n\nA famous sculptor Cicasso, whose self-portrait you can contemplate, hates cubism. He is more impressed by the idea to transmit two-dimensional objects through three-dimensional objects by using his magnificent sculptures. And his new project is connected with this. Cicasso wants to make a coat for the haters of anticubism. To do this, he wants to create a sculpture depicting a well-known geometric primitive \u2014 convex polygon.\n\nCicasso prepared for this a few blanks, which are rods with integer lengths, and now he wants to bring them together. The i-th rod is a segment of length l_{i}.\n\nThe sculptor plans to make a convex polygon with a nonzero area, using all rods he has as its sides. Each rod should be used as a side to its full length. It is forbidden to cut, break or bend rods. However, two sides may form a straight angle $180^{\\circ}$.\n\nCicasso knows that it is impossible to make a convex polygon with a nonzero area out of the rods with the lengths which he had chosen. Cicasso does not want to leave the unused rods, so the sculptor decides to make another rod-blank with an integer length so that his problem is solvable. Of course, he wants to make it as short as possible, because the materials are expensive, and it is improper deed to spend money for nothing. \n\nHelp sculptor! \n\n\n-----Input-----\n\nThe first line contains an integer n (3 \u2264 n \u2264 10^5) \u2014 a number of rod-blanks.\n\nThe second line contains n integers l_{i} (1 \u2264 l_{i} \u2264 10^9) \u2014 lengths of rods, which Cicasso already has. It is guaranteed that it is impossible to make a polygon with n vertices and nonzero area using the rods Cicasso already has.\n\n\n-----Output-----\n\nPrint the only integer z \u2014 the minimum length of the rod, so that after adding it it can be possible to construct convex polygon with (n + 1) vertices and nonzero area from all of the rods.\n\n\n-----Examples-----\nInput\n3\n1 2 1\n\nOutput\n1\n\nInput\n5\n20 4 3 2 1\n\nOutput\n11\n\n\n\n-----Note-----\n\nIn the first example triangle with sides {1 + 1 = 2, 2, 1} can be formed from a set of lengths {1, 1, 1, 2}. \n\nIn the second example you can make a triangle with lengths {20, 11, 4 + 3 + 2 + 1 = 10}."} +{"id": "01612", "text": "Lately, a national version of a bingo game has become very popular in Berland. There are n players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the i-th player contains m_{i} numbers.\n\nDuring the game the host takes numbered balls one by one from a bag. He reads the number aloud in a high and clear voice and then puts the ball away. All participants cross out the number if it occurs on their cards. The person who crosses out all numbers from his card first, wins. If multiple people cross out all numbers from their cards at the same time, there are no winners in the game. At the beginning of the game the bag contains 100 balls numbered 1 through 100, the numbers of all balls are distinct.\n\nYou are given the cards for each player. Write a program that determines whether a player can win the game at the most favorable for him scenario or not.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 100) \u2014 the number of the players. Then follow n lines, each line describes a player's card. The line that describes a card starts from integer m_{i} (1 \u2264 m_{i} \u2264 100) that shows how many numbers the i-th player's card has. Then follows a sequence of integers a_{i}, 1, a_{i}, 2, ..., a_{i}, m_{i} (1 \u2264 a_{i}, k \u2264 100) \u2014 the numbers on the i-th player's card. The numbers in the lines are separated by single spaces.\n\nIt is guaranteed that all the numbers on each card are distinct.\n\n\n-----Output-----\n\nPrint n lines, the i-th line must contain word \"YES\" (without the quotes), if the i-th player can win, and \"NO\" (without the quotes) otherwise.\n\n\n-----Examples-----\nInput\n3\n1 1\n3 2 4 1\n2 10 11\n\nOutput\nYES\nNO\nYES\n\nInput\n2\n1 1\n1 1\n\nOutput\nNO\nNO"} +{"id": "01613", "text": "\u041f\u0430\u043c\u044f\u0442\u044c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 n \u044f\u0447\u0435\u0435\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0441\u0442\u0440\u043e\u0435\u043d\u044b \u0432 \u0440\u044f\u0434. \u041f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u0443\u0435\u043c \u044f\u0447\u0435\u0439\u043a\u0438 \u043e\u0442 1 \u0434\u043e n \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e. \u041f\u0440\u043e \u043a\u0430\u0436\u0434\u0443\u044e \u044f\u0447\u0435\u0439\u043a\u0443 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0430 \u043e\u043d\u0430 \u0438\u043b\u0438 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 \u043a\u0430\u043a\u043e\u043c\u0443-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 (\u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u043d\u0430 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442).\n\n\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0449\u0438\u0435 \u0435\u043c\u0443 \u044f\u0447\u0435\u0439\u043a\u0438 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432\u0438\u0434\u0430 \u00ab\u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0437\u0430\u043d\u044f\u0442\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u0438 \u0432 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0443\u044e, \u0430 \u0437\u0430\u043d\u044f\u0442\u0443\u044e \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439\u00bb \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0449\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c \u044f\u0447\u0435\u0439\u043a\u0438 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430. \u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043b\u044e\u0431\u0430\u044f \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0430\u0432\u0435\u0435 (\u0438\u043c\u0435\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043d\u043e\u043c\u0435\u0440) \u043b\u044e\u0431\u043e\u0439 \u0437\u0430\u043d\u044f\u0442\u043e\u0439.\n\n\u0412\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u043e\u0434\u043d\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u0438 \u0432 \u0434\u0440\u0443\u0433\u0443\u044e, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e, \u0447\u0442\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u044f\u0447\u0435\u0435\u043a \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0434\u0435\u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u043d\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u0430\u043c\u0438\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u044f\u0447\u0435\u0439\u043a\u0438, \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0449\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 i, \u043d\u0430\u0445\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0440\u0430\u043d\u044c\u0448\u0435 \u0432\u0441\u0435\u0445 \u044f\u0447\u0435\u0435\u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 j, \u0442\u043e \u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0439 \u044d\u0442\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f.\n\n\u0421\u0447\u0438\u0442\u0430\u0439\u0442\u0435, \u0447\u0442\u043e \u043d\u043e\u043c\u0435\u0440\u0430 \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b, \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u0430 \u044f\u0447\u0435\u0439\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u0437\u0430\u043d\u044f\u0442\u0430 \u043a\u0430\u043a\u0438\u043c-\u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0447\u0438\u0441\u043b\u043e n (1 \u2264 n \u2264 200 000)\u00a0\u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044f\u0447\u0435\u0435\u043a \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430.\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 n \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n), \u0433\u0434\u0435 a_{i} \u0440\u0430\u0432\u043d\u043e \u043b\u0438\u0431\u043e 0 (\u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e i-\u044f \u044f\u0447\u0435\u0439\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0430), \u043b\u0438\u0431\u043e \u043d\u043e\u043c\u0435\u0440\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 i-\u044f \u044f\u0447\u0435\u0439\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 a_{i} \u043d\u0435 \u0440\u0430\u0432\u043d\u043e 0.\n\n\u041f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u043d\u044b \u0446\u0435\u043b\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u043e\u0442 1 \u0434\u043e n \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u043d\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043e\u0434\u043d\u043e \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e\u00a0\u2014 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u0434\u0435\u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n4\n0 2 2 1\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n8\n0 8 8 8 0 4 4 2\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n4\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u0432\u0443\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439: \u041f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u044f\u0447\u0435\u0439\u043a\u0438 \u0432 \u043f\u0435\u0440\u0432\u0443\u044e. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u044c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u043f\u0440\u0438\u043c\u0435\u0442 \u0432\u0438\u0434: 2\u00a02\u00a00\u00a01. \u041f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u0438 \u0432 \u0442\u0440\u0435\u0442\u044c\u044e. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u044c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u043f\u0440\u0438\u043c\u0435\u0442 \u0432\u0438\u0434: 2\u00a02\u00a01\u00a00."} +{"id": "01614", "text": "Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to a_{i}.\n\nConsider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and h (1 \u2264 n \u2264 1000, 1 \u2264 h \u2264 1000)\u00a0\u2014 the number of friends and the height of the fence, respectively.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 2h), the i-th of them is equal to the height of the i-th person.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum possible valid width of the road.\n\n\n-----Examples-----\nInput\n3 7\n4 5 14\n\nOutput\n4\n\nInput\n6 1\n1 1 1 1 1 1\n\nOutput\n6\n\nInput\n6 5\n7 6 8 9 10 5\n\nOutput\n11\n\n\n\n-----Note-----\n\nIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.\n\nIn the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.\n\nIn the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11."} +{"id": "01615", "text": "Little penguin Polo adores integer segments, that is, pairs of integers [l;\u00a0r] (l \u2264 r). \n\nHe has a set that consists of n integer segments: [l_1;\u00a0r_1], [l_2;\u00a0r_2], ..., [l_{n};\u00a0r_{n}]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l;\u00a0r] to either segment [l - 1;\u00a0r], or to segment [l;\u00a0r + 1].\n\nThe value of a set of segments that consists of n segments [l_1;\u00a0r_1], [l_2;\u00a0r_2], ..., [l_{n};\u00a0r_{n}] is the number of integers x, such that there is integer j, for which the following inequality holds, l_{j} \u2264 x \u2264 r_{j}.\n\nFind the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n, k \u2264 10^5). Each of the following n lines contain a segment as a pair of integers l_{i} and r_{i} ( - 10^5 \u2264 l_{i} \u2264 r_{i} \u2264 10^5), separated by a space.\n\nIt is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 \u2264 i < j \u2264 n) the following inequality holds, min(r_{i}, r_{j}) < max(l_{i}, l_{j}).\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n2 3\n1 2\n3 4\n\nOutput\n2\n\nInput\n3 7\n1 2\n3 3\n4 7\n\nOutput\n0"} +{"id": "01616", "text": "You are given an array $a$ of length $n$ that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.\n\nA 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.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the length of $a$.\n\nThe second line contains $n$ integers $a_1$, $a_2$, $\\ldots$, $a_{n}$ ($1 \\le a_i \\le 10^6$)\u00a0\u2014 the elements of the array $a$.\n\n\n-----Output-----\n\nOutput the length of the shortest non-empty subsequence of $a$ product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print \"-1\".\n\n\n-----Examples-----\nInput\n3\n1 4 6\n\nOutput\n1\nInput\n4\n2 3 6 6\n\nOutput\n2\nInput\n3\n6 15 10\n\nOutput\n3\nInput\n4\n2 3 5 7\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first sample, you can choose a subsequence $[1]$.\n\nIn the second sample, you can choose a subsequence $[6, 6]$.\n\nIn the third sample, you can choose a subsequence $[6, 15, 10]$.\n\nIn the fourth sample, there is no such subsequence."} +{"id": "01617", "text": "There are $n$ people sitting in a circle, numbered from $1$ to $n$ in the order in which they are seated. That is, for all $i$ from $1$ to $n-1$, the people with id $i$ and $i+1$ are adjacent. People with id $n$ and $1$ are adjacent as well.\n\nThe person with id $1$ initially has a ball. He picks a positive integer $k$ at most $n$, and passes the ball to his $k$-th neighbour in the direction of increasing ids, that person passes the ball to his $k$-th neighbour in the same direction, and so on until the person with the id $1$ gets the ball back. When he gets it back, people do not pass the ball any more.\n\nFor instance, if $n = 6$ and $k = 4$, the ball is passed in order $[1, 5, 3, 1]$. \n\nConsider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be $1 + 5 + 3 = 9$.\n\nFind and report the set of possible fun values for all choices of positive integer $k$. It can be shown that under the constraints of the problem, the ball always gets back to the $1$-st player after finitely many steps, and there are no more than $10^5$ possible fun values for given $n$.\n\n\n-----Input-----\n\nThe only line consists of a single integer $n$\u00a0($2 \\leq n \\leq 10^9$)\u00a0\u2014 the number of people playing with the ball.\n\n\n-----Output-----\n\nSuppose the set of all fun values is $f_1, f_2, \\dots, f_m$.\n\nOutput a single line containing $m$ space separated integers $f_1$ through $f_m$ in increasing order.\n\n\n-----Examples-----\nInput\n6\n\nOutput\n1 5 9 21\n\nInput\n16\n\nOutput\n1 10 28 64 136\n\n\n\n-----Note-----\n\nIn the first sample, we've already shown that picking $k = 4$ yields fun value $9$, as does $k = 2$. Picking $k = 6$ results in fun value of $1$. For $k = 3$ we get fun value $5$ and with $k = 1$ or $k = 5$ we get $21$. [Image] \n\nIn the second sample, the values $1$, $10$, $28$, $64$ and $136$ are achieved for instance for $k = 16$, $8$, $4$, $10$ and $11$, respectively."} +{"id": "01618", "text": "Dima's got a staircase that consists of n stairs. The first stair is at height a_1, the second one is at a_2, the last one is at a_{n} (1 \u2264 a_1 \u2264 a_2 \u2264 ... \u2264 a_{n}). \n\nDima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The i-th box has width w_{i} and height h_{i}. Dima throws each box vertically down on the first w_{i} stairs of the staircase, that is, the box covers stairs with numbers 1, 2, ..., w_{i}. Each thrown box flies vertically down until at least one of the two following events happen: the bottom of the box touches the top of a stair; the bottom of the box touches the top of a box, thrown earlier. \n\nWe only consider touching of the horizontal sides of stairs and boxes, at that touching with the corners isn't taken into consideration. Specifically, that implies that a box with width w_{i} cannot touch the stair number w_{i} + 1.\n\nYou are given the description of the staircase and the sequence in which Dima threw the boxes at it. For each box, determine how high the bottom of the box after landing will be. Consider a box to fall after the previous one lands.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of stairs in the staircase. The second line contains a non-decreasing sequence, consisting of n integers, a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9;\u00a0a_{i} \u2264 a_{i} + 1).\n\nThe next line contains integer m (1 \u2264 m \u2264 10^5) \u2014 the number of boxes. Each of the following m lines contains a pair of integers w_{i}, h_{i} (1 \u2264 w_{i} \u2264 n;\u00a01 \u2264 h_{i} \u2264 10^9) \u2014 the size of the i-th thrown box.\n\nThe numbers in the lines are separated by spaces.\n\n\n-----Output-----\n\nPrint m integers \u2014 for each box the height, where the bottom of the box will be after landing. Print the answers for the boxes in the order, in which the boxes are given in the input.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n5\n1 2 3 6 6\n4\n1 1\n3 1\n1 1\n4 3\n\nOutput\n1\n3\n4\n6\n\nInput\n3\n1 2 3\n2\n1 1\n3 1\n\nOutput\n1\n3\n\nInput\n1\n1\n5\n1 2\n1 10\n1 10\n1 10\n1 10\n\nOutput\n1\n3\n13\n23\n33\n\n\n\n-----Note-----\n\nThe first sample are shown on the picture. [Image]"} +{"id": "01619", "text": "Cowboy Beblop is a funny little boy who likes sitting at his computer. He somehow obtained two elastic hoops in the shape of 2D polygons, which are not necessarily convex. Since there's no gravity on his spaceship, the hoops are standing still in the air. Since the hoops are very elastic, Cowboy Beblop can stretch, rotate, translate or shorten their edges as much as he wants.\n\nFor both hoops, you are given the number of their vertices, as well as the position of each vertex, defined by the X , Y and Z coordinates. The vertices are given in the order they're connected: the 1st\u202fvertex is connected to the 2nd, which is connected to the 3rd, etc., and the last vertex is connected to the first one. Two hoops are connected if it's impossible to pull them to infinity in different directions by manipulating their edges, without having their edges or vertices intersect at any point \u2013 just like when two links of a chain are connected. The polygons' edges do not intersect or overlap. \n\nTo make things easier, we say that two polygons are well-connected, if the edges of one polygon cross the area of the other polygon in two different directions (from the upper and lower sides of the plane defined by that polygon) a different number of times.\n\nCowboy Beblop is fascinated with the hoops he has obtained and he would like to know whether they are well-connected or not. Since he\u2019s busy playing with his dog, Zwei, he\u2019d like you to figure it out for him. He promised you some sweets if you help him! \n\n\n-----Input-----\n\nThe first line of input contains an integer n (3 \u2264 n \u2264 100 000), which denotes the number of edges of the first polygon. The next N lines each contain the integers x, y and z ( - 1 000 000 \u2264 x, y, z \u2264 1 000 000)\u00a0\u2014 coordinates of the vertices, in the manner mentioned above. The next line contains an integer m (3 \u2264 m \u2264 100 000) , denoting the number of edges of the second polygon, followed by m lines containing the coordinates of the second polygon\u2019s vertices.\n\nIt is guaranteed that both polygons are simple (no self-intersections), and in general that the obtained polygonal lines do not intersect each other. Also, you can assume that no 3 consecutive points of a polygon lie on the same line.\n\n\n-----Output-----\n\nYour output should contain only one line, with the words \"YES\" or \"NO\", depending on whether the two given polygons are well-connected. \n\n\n-----Example-----\nInput\n4\n0 0 0\n2 0 0\n2 2 0\n0 2 0\n4\n1 1 -1\n1 1 1\n1 3 1\n1 3 -1\n\nOutput\nYES\n\n\n\n-----Note-----\n\nOn the picture below, the two polygons are well-connected, as the edges of the vertical polygon cross the area of the horizontal one exactly once in one direction (for example, from above to below), and zero times in the other (in this case, from below to above). Note that the polygons do not have to be parallel to any of the xy-,xz-,yz- planes in general. [Image]"} +{"id": "01620", "text": "In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.\n\nHe is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings \"abc\" and \"abca\" suit him, while the string \"aba\" doesn't. He also want the number of letters 'c' in his string to be as little as possible.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 2\u00b710^5)\u00a0\u2014 the length of the string.\n\n\n-----Output-----\n\nPrint the string that satisfies all the constraints.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n2\n\nOutput\naa\n\nInput\n3\n\nOutput\nbba\n\n\n\n-----Note-----\n\nA palindrome is a sequence of characters which reads the same backward and forward."} +{"id": "01621", "text": "DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter c DZY knows its value w_{c}. For each special string s = s_1s_2... s_{|}s| (|s| is the length of the string) he represents its value with a function f(s), where $f(s) = \\sum_{i = 1}^{|s|}(w_{s_{i}} \\cdot i)$\n\nNow DZY has a string s. He wants to insert k lowercase letters into this string in order to get the largest possible value of the resulting string. Can you help him calculate the largest possible value he could get? \n\n\n-----Input-----\n\nThe first line contains a single string s\u00a0(1 \u2264 |s| \u2264 10^3).\n\nThe second line contains a single integer k\u00a0(0 \u2264 k \u2264 10^3).\n\nThe third line contains twenty-six integers from w_{a} to w_{z}. Each such number is non-negative and doesn't exceed 1000.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the largest possible value of the resulting string DZY could get.\n\n\n-----Examples-----\nInput\nabc\n3\n1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n\nOutput\n41\n\n\n\n-----Note-----\n\nIn the test sample DZY can obtain \"abcbbc\", value = 1\u00b71 + 2\u00b72 + 3\u00b72 + 4\u00b72 + 5\u00b72 + 6\u00b72 = 41."} +{"id": "01622", "text": "There are n employees working in company \"X\" (let's number them from 1 to n for convenience). Initially the employees didn't have any relationships among each other. On each of m next days one of the following events took place: either employee y became the boss of employee x (at that, employee x didn't have a boss before); or employee x gets a packet of documents and signs them; then he gives the packet to his boss. The boss signs the documents and gives them to his boss and so on (the last person to sign the documents sends them to the archive); or comes a request of type \"determine whether employee x signs certain documents\". \n\nYour task is to write a program that will, given the events, answer the queries of the described type. At that, it is guaranteed that throughout the whole working time the company didn't have cyclic dependencies.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 10^5) \u2014 the number of employees and the number of events. \n\nEach of the next m lines contains the description of one event (the events are given in the chronological order). The first number of the line determines the type of event t (1 \u2264 t \u2264 3). If t = 1, then next follow two integers x and y (1 \u2264 x, y \u2264 n) \u2014 numbers of the company employees. It is guaranteed that employee x doesn't have the boss currently. If t = 2, then next follow integer x (1 \u2264 x \u2264 n) \u2014 the number of the employee who got a document packet. If t = 3, then next follow two integers x and i (1 \u2264 x \u2264 n;\u00a01 \u2264 i \u2264 [number of packets that have already been given]) \u2014 the employee and the number of the document packet for which you need to find out information. The document packets are numbered started from 1 in the chronological order. \n\nIt is guaranteed that the input has at least one query of the third type.\n\n\n-----Output-----\n\nFor each query of the third type print \"YES\" if the employee signed the document package and \"NO\" otherwise. Print all the words without the quotes.\n\n\n-----Examples-----\nInput\n4 9\n1 4 3\n2 4\n3 3 1\n1 2 3\n2 2\n3 1 2\n1 3 1\n2 2\n3 1 3\n\nOutput\nYES\nNO\nYES"} +{"id": "01623", "text": "Mislove had an array $a_1$, $a_2$, $\\cdots$, $a_n$ of $n$ positive integers, but he has lost it. He only remembers the following facts about it:\n\n\n\n The number of different numbers in the array is not less than $l$ and is not greater than $r$;\n\n For each array's element $a_i$ either $a_i = 1$ or $a_i$ is even and there is a number $\\dfrac{a_i}{2}$ in the array.\n\nFor example, if $n=5$, $l=2$, $r=3$ then an array could be $[1,2,2,4,4]$ or $[1,1,1,1,2]$; but it couldn't be $[1,2,2,4,8]$ because this array contains $4$ different numbers; it couldn't be $[1,2,2,3,3]$ because $3$ is odd and isn't equal to $1$; and it couldn't be $[1,1,2,2,16]$ because there is a number $16$ in the array but there isn't a number $\\frac{16}{2} = 8$.\n\nAccording to these facts, he is asking you to count the minimal and the maximal possible sums of all elements in an array. \n\n\n-----Input-----\n\nThe only input line contains three integers $n$, $l$ and $r$ ($1 \\leq n \\leq 1\\,000$, $1 \\leq l \\leq r \\leq \\min(n, 20)$)\u00a0\u2014 an array's size, the minimal number and the maximal number of distinct elements in an array.\n\n\n-----Output-----\n\nOutput two numbers\u00a0\u2014 the minimal and the maximal possible sums of all elements in an array.\n\n\n-----Examples-----\nInput\n4 2 2\n\nOutput\n5 7\n\nInput\n5 1 5\n\nOutput\n5 31\n\n\n\n-----Note-----\n\nIn the first example, an array could be the one of the following: $[1,1,1,2]$, $[1,1,2,2]$ or $[1,2,2,2]$. In the first case the minimal sum is reached and in the last case the maximal sum is reached.\n\nIn the second example, the minimal sum is reached at the array $[1,1,1,1,1]$, and the maximal one is reached at the array $[1,2,4,8,16]$."} +{"id": "01624", "text": "Lunar New Year is approaching, and Bob is struggling with his homework \u2013 a number division problem.\n\nThere are $n$ positive integers $a_1, a_2, \\ldots, a_n$ on Bob's homework paper, where $n$ is always an even number. Bob is asked to divide those numbers into groups, where each group must contain at least $2$ numbers. Suppose the numbers are divided into $m$ groups, and the sum of the numbers in the $j$-th group is $s_j$. Bob's aim is to minimize the sum of the square of $s_j$, that is $$\\sum_{j = 1}^{m} s_j^2.$$\n\nBob is puzzled by this hard problem. Could you please help him solve it?\n\n\n-----Input-----\n\nThe first line contains an even integer $n$ ($2 \\leq n \\leq 3 \\cdot 10^5$), denoting that there are $n$ integers on Bob's homework paper.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^4$), describing the numbers you need to deal with.\n\n\n-----Output-----\n\nA single line containing one integer, denoting the minimum of the sum of the square of $s_j$, which is $$\\sum_{i = j}^{m} s_j^2,$$ where $m$ is the number of groups.\n\n\n-----Examples-----\nInput\n4\n8 5 2 3\n\nOutput\n164\n\nInput\n6\n1 1 1 2 2 2\n\nOutput\n27\n\n\n\n-----Note-----\n\nIn the first sample, one of the optimal solutions is to divide those $4$ numbers into $2$ groups $\\{2, 8\\}, \\{5, 3\\}$. Thus the answer is $(2 + 8)^2 + (5 + 3)^2 = 164$.\n\nIn the second sample, one of the optimal solutions is to divide those $6$ numbers into $3$ groups $\\{1, 2\\}, \\{1, 2\\}, \\{1, 2\\}$. Thus the answer is $(1 + 2)^2 + (1 + 2)^2 + (1 + 2)^2 = 27$."} +{"id": "01625", "text": "Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve.\n\nHe's got a square 2^{n} \u00d7 2^{n}-sized matrix and 4^{n} integers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that the beauty of the resulting matrix with numbers is maximum.\n\nThe beauty of a 2^{n} \u00d7 2^{n}-sized matrix is an integer, obtained by the following algorithm: Find the maximum element in the matrix. Let's denote it as m. If n = 0, then the beauty of the matrix equals m. Otherwise, a matrix can be split into 4 non-intersecting 2^{n} - 1 \u00d7 2^{n} - 1-sized submatrices, then the beauty of the matrix equals the sum of number m and other four beauties of the described submatrices. \n\nAs you can see, the algorithm is recursive.\n\nHelp Ilya, solve the problem and print the resulting maximum beauty of the matrix.\n\n\n-----Input-----\n\nThe first line contains integer 4^{n} (1 \u2264 4^{n} \u2264 2\u00b710^6). The next line contains 4^{n} integers a_{i} (1 \u2264 a_{i} \u2264 10^9) \u2014 the numbers you need to arrange in the 2^{n} \u00d7 2^{n}-sized matrix.\n\n\n-----Output-----\n\nOn a single line print the maximum value of the beauty of the described matrix.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n1\n13\n\nOutput\n13\n\nInput\n4\n1 2 3 4\n\nOutput\n14\n\n\n\n-----Note-----\n\nConsider the second sample. You need to arrange the numbers in the matrix as follows:\n\n1 2\n\n3 4\n\n\n\nThen the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14."} +{"id": "01626", "text": "Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits.\n\nAlso Pasha has a number k and two sequences of length n / k (n is divisible by k) a_1, a_2, ..., a_{n} / k and b_1, b_2, ..., b_{n} / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2\u00b7k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit b_{i} and is divisible by a_{i} if represented as an integer. \n\nTo represent the block of length k as an integer, let's write it out as a sequence c_1, c_2,...,c_{k}. Then the integer is calculated as the result of the expression c_1\u00b710^{k} - 1 + c_2\u00b710^{k} - 2 + ... + c_{k}.\n\nPasha asks you to calculate the number of good phone numbers of length n, for the given k, a_{i} and b_{i}. As this number can be too big, print it modulo 10^9 + 7. \n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 n \u2264 100 000, 1 \u2264 k \u2264 min(n, 9))\u00a0\u2014 the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k.\n\nThe second line of the input contains n / k space-separated positive integers\u00a0\u2014 sequence a_1, a_2, ..., a_{n} / k (1 \u2264 a_{i} < 10^{k}).\n\nThe third line of the input contains n / k space-separated positive integers\u00a0\u2014 sequence b_1, b_2, ..., b_{n} / k (0 \u2264 b_{i} \u2264 9). \n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of good phone numbers of length n modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n6 2\n38 56 49\n7 3 4\n\nOutput\n8\n\nInput\n8 2\n1 22 3 44\n5 4 3 2\n\nOutput\n32400\n\n\n\n-----Note-----\n\nIn the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698."} +{"id": "01627", "text": "Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places.\n\nThe robber girl was angry at first, but then she decided to arrange the animals herself. She repeatedly names numbers l and r such that r - l + 1 is even. After that animals that occupy positions between l and r inclusively are rearranged as follows: the animal at position l swaps places with the animal at position l + 1, the animal l + 2 swaps with the animal l + 3, ..., finally, the animal at position r - 1 swaps with the animal r.\n\nHelp the robber girl to arrange the animals in the order of non-decreasing height. You should name at most 20 000 segments, since otherwise the robber girl will become bored and will start scaring the animals again.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100)\u00a0\u2014 number of animals in the robber girl's zoo.\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9), where a_{i} is the height of the animal occupying the i-th place.\n\n\n-----Output-----\n\nPrint the sequence of operations that will rearrange the animals by non-decreasing height.\n\nThe output should contain several lines, i-th of the lines should contain two space-separated integers l_{i} and r_{i} (1 \u2264 l_{i} < r_{i} \u2264 n)\u00a0\u2014 descriptions of segments the robber girl should name. The segments should be described in the order the operations are performed.\n\nThe number of operations should not exceed 20 000.\n\nIf the animals are arranged correctly from the start, you are allowed to output nothing.\n\n\n-----Examples-----\nInput\n4\n2 1 4 3\n\nOutput\n1 4\n\nInput\n7\n36 28 57 39 66 69 68\n\nOutput\n1 4\n6 7\n\nInput\n5\n1 2 1 2 1\n\nOutput\n2 5\n3 4\n1 4\n1 4\n\n\n\n-----Note-----\n\nNote that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed."} +{"id": "01628", "text": "Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string s, consisting of characters \"x\" and \"y\", and uses two following operations at runtime: Find two consecutive characters in the string, such that the first of them equals \"y\", and the second one equals \"x\" and swap them. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string. Find in the string two consecutive characters, such that the first of them equals \"x\" and the second one equals \"y\". Remove these characters from the string. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string. \n\nThe input for the new algorithm is string s, and the algorithm works as follows: If you can apply at least one of the described operations to the string, go to step 2 of the algorithm. Otherwise, stop executing the algorithm and print the current string. If you can apply operation 1, then apply it. Otherwise, apply operation 2. After you apply the operation, go to step 1 of the algorithm. \n\nNow Vitaly wonders, what is going to be printed as the result of the algorithm's work, if the input receives string s.\n\n\n-----Input-----\n\nThe first line contains a non-empty string s. \n\nIt is guaranteed that the string only consists of characters \"x\" and \"y\". It is guaranteed that the string consists of at most 10^6 characters. It is guaranteed that as the result of the algorithm's execution won't be an empty string.\n\n\n-----Output-----\n\nIn the only line print the string that is printed as the result of the algorithm's work, if the input of the algorithm input receives string s.\n\n\n-----Examples-----\nInput\nx\n\nOutput\nx\n\nInput\nyxyxy\n\nOutput\ny\n\nInput\nxxxxxy\n\nOutput\nxxxx\n\n\n\n-----Note-----\n\nIn the first test the algorithm will end after the first step of the algorithm, as it is impossible to apply any operation. Thus, the string won't change.\n\nIn the second test the transformation will be like this: string \"yxyxy\" transforms into string \"xyyxy\"; string \"xyyxy\" transforms into string \"xyxyy\"; string \"xyxyy\" transforms into string \"xxyyy\"; string \"xxyyy\" transforms into string \"xyy\"; string \"xyy\" transforms into string \"y\". \n\nAs a result, we've got string \"y\". \n\nIn the third test case only one transformation will take place: string \"xxxxxy\" transforms into string \"xxxx\". Thus, the answer will be string \"xxxx\"."} +{"id": "01629", "text": "Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.\n\nOnce Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on. \n\nFor example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.\n\nAt this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x \u2014 the number of the box, where he put the last of the taken out balls.\n\nHe asks you to help to find the initial arrangement of the balls in the boxes.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and x (2 \u2264 n \u2264 10^5, 1 \u2264 x \u2264 n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a_1, a_2, ..., a_{n}, where integer a_{i} (0 \u2264 a_{i} \u2264 10^9, a_{x} \u2260 0) represents the number of balls in the box with index i after Vasya completes all the actions. \n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nPrint n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n4 4\n4 3 1 6\n\nOutput\n3 2 5 4 \nInput\n5 2\n3 2 0 2 7\n\nOutput\n2 1 4 1 6 \nInput\n3 3\n2 3 1\n\nOutput\n1 2 3"} +{"id": "01630", "text": "Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.\n\nEach club's full name consist of two words: the team's name and the hometown's name, for example, \"DINAMO BYTECITY\". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that: the short name is the same as three first letters of the team's name, for example, for the mentioned club it is \"DIN\", or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is \"DIB\". \n\nApart from this, there is a rule that if for some club x the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club x. For example, if the above mentioned club has short name \"DIB\", then no club for which the first option is chosen can have short name equal to \"DIN\". However, it is possible that some club have short name \"DIN\", where \"DI\" are the first two letters of the team's name, and \"N\" is the first letter of hometown's name. Of course, no two teams can have the same short name.\n\nHelp Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen. \n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of clubs in the league.\n\nEach of the next n lines contains two words\u00a0\u2014 the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20.\n\n\n-----Output-----\n\nIt it is not possible to choose short names and satisfy all constraints, print a single line \"NO\".\n\nOtherwise, in the first line print \"YES\". Then print n lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n2\nDINAMO BYTECITY\nFOOTBALL MOSCOW\n\nOutput\nYES\nDIN\nFOO\n\nInput\n2\nDINAMO BYTECITY\nDINAMO BITECITY\n\nOutput\nNO\n\nInput\n3\nPLAYFOOTBALL MOSCOW\nPLAYVOLLEYBALL SPB\nGOGO TECHNOCUP\n\nOutput\nYES\nPLM\nPLS\nGOG\n\nInput\n3\nABC DEF\nABC EFG\nABD OOO\n\nOutput\nYES\nABD\nABE\nABO\n\n\n\n-----Note-----\n\nIn the first sample Innokenty can choose first option for both clubs.\n\nIn the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.\n\nIn the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club.\n\nIn the fourth example note that it is possible that the chosen short name for some club x is the same as the first option of another club y if the first options of x and y are different."} +{"id": "01631", "text": "Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: \"Fox\"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. \n\nAfter checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!\n\nShe wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.\n\nLexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: s_{i} \u2260 t_{i}. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters s_{i} and t_{i} according to their order in alphabet.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 100): number of names.\n\nEach of the following n lines contain one string name_{i} (1 \u2264 |name_{i}| \u2264 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.\n\n\n-----Output-----\n\nIf there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'\u2013'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).\n\nOtherwise output a single word \"Impossible\" (without quotes).\n\n\n-----Examples-----\nInput\n3\nrivest\nshamir\nadleman\n\nOutput\nbcdefghijklmnopqrsatuvwxyz\n\nInput\n10\ntourist\npetr\nwjmzbmr\nyeputons\nvepifanov\nscottwu\noooooooooooooooo\nsubscriber\nrowdark\ntankengineer\n\nOutput\nImpossible\n\nInput\n10\npetr\negor\nendagorion\nfeferivan\nilovetanyaromanova\nkostka\ndmitriyh\nmaratsnowbear\nbredorjaguarturnik\ncgyforever\n\nOutput\naghjlnopefikdmbcqrstuvwxyz\n\nInput\n7\ncar\ncare\ncareful\ncarefully\nbecarefuldontforgetsomething\notherwiseyouwillbehacked\ngoodluck\n\nOutput\nacbdefhijklmnogpqrstuvwxyz"} +{"id": "01632", "text": "Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds.\n\nAndrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew?\n\n\n-----Input-----\n\nThe first line of input contains a single integer n (2 \u2264 n \u2264 2000) \u2014 the number of balls in the jar.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 5000) \u2014 the number written on the ith ball. It is guaranteed that no two balls have the same number.\n\n\n-----Output-----\n\nPrint a single real value \u2014 the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. \n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n0.0000000000\n\nInput\n3\n1 2 10\n\nOutput\n0.0740740741\n\n\n\n-----Note-----\n\nIn the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total.\n\nIn the second case, each game could've had three outcomes \u2014 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability $\\frac{1}{3} \\cdot \\frac{1}{3} \\cdot \\frac{2}{3} = \\frac{2}{27}$."} +{"id": "01633", "text": "Pasha loves his phone and also putting his hair up... But the hair is now irrelevant.\n\nPasha has installed a new game to his phone. The goal of the game is following. There is a rectangular field consisting of n row with m pixels in each row. Initially, all the pixels are colored white. In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy's move the pixel does not change, that is, it remains black. Pasha loses the game when a 2 \u00d7 2 square consisting of black pixels is formed. \n\nPasha has made a plan of k moves, according to which he will paint pixels. Each turn in his plan is represented as a pair of numbers i and j, denoting respectively the row and the column of the pixel to be colored on the current move.\n\nDetermine whether Pasha loses if he acts in accordance with his plan, and if he does, on what move the 2 \u00d7 2 square consisting of black pixels is formed.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, m, k (1 \u2264 n, m \u2264 1000, 1 \u2264 k \u2264 10^5)\u00a0\u2014 the number of rows, the number of columns and the number of moves that Pasha is going to perform. \n\nThe next k lines contain Pasha's moves in the order he makes them. Each line contains two integers i and j (1 \u2264 i \u2264 n, 1 \u2264 j \u2264 m), representing the row number and column number of the pixel that was painted during a move.\n\n\n-----Output-----\n\nIf Pasha loses, print the number of the move when the 2 \u00d7 2 square consisting of black pixels is formed.\n\nIf Pasha doesn't lose, that is, no 2 \u00d7 2 square consisting of black pixels is formed during the given k moves, print 0.\n\n\n-----Examples-----\nInput\n2 2 4\n1 1\n1 2\n2 1\n2 2\n\nOutput\n4\n\nInput\n2 3 6\n2 3\n2 2\n1 3\n2 2\n1 2\n1 1\n\nOutput\n5\n\nInput\n5 3 7\n2 3\n1 2\n1 1\n4 1\n3 1\n5 3\n3 2\n\nOutput\n0"} +{"id": "01634", "text": "Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m.\n\nPublic transport is not free. There are 4 types of tickets: A ticket for one ride on some bus or trolley. It costs c_1 burles; A ticket for an unlimited number of rides on some bus or on some trolley. It costs c_2 burles; A ticket for an unlimited number of rides on all buses or all trolleys. It costs c_3 burles; A ticket for an unlimited number of rides on all buses and trolleys. It costs c_4 burles. \n\nVasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets.\n\n\n-----Input-----\n\nThe first line contains four integers c_1, c_2, c_3, c_4 (1 \u2264 c_1, c_2, c_3, c_4 \u2264 1000) \u2014 the costs of the tickets.\n\nThe second line contains two integers n and m (1 \u2264 n, m \u2264 1000) \u2014 the number of buses and trolleys Vasya is going to use.\n\nThe third line contains n integers a_{i} (0 \u2264 a_{i} \u2264 1000) \u2014 the number of times Vasya is going to use the bus number i.\n\nThe fourth line contains m integers b_{i} (0 \u2264 b_{i} \u2264 1000) \u2014 the number of times Vasya is going to use the trolley number i.\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum sum of burles Vasya will have to spend on the tickets.\n\n\n-----Examples-----\nInput\n1 3 7 19\n2 3\n2 5\n4 4 4\n\nOutput\n12\n\nInput\n4 3 2 1\n1 3\n798\n1 2 3\n\nOutput\n1\n\nInput\n100 100 8 100\n3 5\n7 94 12\n100 1 47 0 42\n\nOutput\n16\n\n\n\n-----Note-----\n\nIn the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2\u00b71) + 3 + 7 = 12 burles.\n\nIn the second sample the profitable strategy is to buy one ticket of the fourth type.\n\nIn the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys."} +{"id": "01635", "text": "Vlad likes to eat in cafes very much. During his life, he has visited cafes n times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research.\n\nFirst of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes he visited in a row, in order of visiting them. Now, Vlad wants to find such a cafe that his last visit to that cafe was before his last visits to every other cafe. In other words, he wants to find such a cafe that he hasn't been there for as long as possible. Help Vlad to find that cafe.\n\n\n-----Input-----\n\nIn first line there is one integer n (1 \u2264 n \u2264 2\u00b710^5)\u00a0\u2014 number of cafes indices written by Vlad.\n\nIn second line, n numbers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 2\u00b710^5) are written\u00a0\u2014 indices of cafes in order of being visited by Vlad. Vlad could visit some cafes more than once. Note that in numeration, some indices could be omitted.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 index of the cafe that Vlad hasn't visited for as long as possible.\n\n\n-----Examples-----\nInput\n5\n1 3 2 1 2\n\nOutput\n3\n\nInput\n6\n2 1 2 2 4 1\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn first test, there are three cafes, and the last visits to cafes with indices 1 and 2 were after the last visit to cafe with index 3; so this cafe is the answer. \n\nIn second test case, there are also three cafes, but with indices 1, 2 and 4. Cafes with indices 1 and 4 were visited after the last visit of cafe with index 2, so the answer is 2. Note that Vlad could omit some numbers while numerating the cafes."} +{"id": "01636", "text": "Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 \u2264 x' \u2264 x and 0 \u2264 y' \u2264 y also belong to this set.\n\nNow Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' \u2265 x and y' \u2265 y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4.\n\nWilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}.\n\nNow Wilbur asks you to help him with this challenge.\n\n\n-----Input-----\n\nThe first line of the input consists of a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of points in the set Wilbur is playing with.\n\nNext follow n lines with points descriptions. Each line contains two integers x and y (0 \u2264 x, y \u2264 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 \u2264 x' \u2264 x and 0 \u2264 y' \u2264 y, are also present in the input.\n\nThe last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 \u2264 w_{i} \u2264 100 000)\u00a0\u2014 the required special value of the point that gets number i in any aesthetically pleasing numbering.\n\n\n-----Output-----\n\nIf there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print \"YES\" on the first line of the output. Otherwise, print \"NO\".\n\nIf a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n5\n2 0\n0 0\n1 0\n1 1\n0 1\n0 -1 -2 1 0\n\nOutput\nYES\n0 0\n1 0\n2 0\n0 1\n1 1\n\nInput\n3\n1 0\n0 0\n2 0\n0 1 2\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}.\n\nIn the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist."} +{"id": "01637", "text": "Zibi is a competitive programming coach. There are $n$ competitors who want to be prepared well. The training contests are quite unusual\u00a0\u2013 there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems.\n\nRules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better).\n\nWe know that the $i$-th competitor will always have score $x_i$ when he codes the first task and $y_i$ when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest.\n\nZibi wants all competitors to write a contest with each other. However, there are $m$ pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($2 \\le n \\le 300\\,000$, $0 \\le m \\le 300\\,000$)\u00a0\u2014 the number of participants and the number of pairs of people who will not write a contest together.\n\nEach of the next $n$ lines contains two integers $x_i$ and $y_i$ ($-10^9 \\le x_i, y_i \\le 10^9$)\u00a0\u2014 the scores which will the $i$-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both $x_i$ and $y_i$ same.\n\nEach of the next $m$ lines contain two integers $u_i$ and $v_i$ ($1 \\le u_i, v_i \\le n$, $u_i \\ne v_i$)\u00a0\u2014 indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once.\n\n\n-----Output-----\n\nOutput $n$ integers\u00a0\u2014 the sum of scores for all participants in the same order as they appear in the input.\n\n\n-----Examples-----\nInput\n3 2\n1 2\n2 3\n1 3\n1 2\n2 3\n\nOutput\n3 0 3 \nInput\n3 3\n1 2\n2 3\n1 3\n1 2\n2 3\n1 3\n\nOutput\n0 0 0 \nInput\n5 3\n-1 3\n2 4\n1 1\n3 5\n2 2\n1 4\n2 3\n3 5\n\nOutput\n4 14 4 16 10 \n\n\n-----Note-----\n\nIn the first example, there will be only one team consisting of persons $1$ and $3$. The optimal strategy for them is to assign the first task to the $3$-rd person and the second task to the $1$-st person, this will lead to score equal to $1 + 2 = 3$.\n\nIn the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case..."} +{"id": "01638", "text": "This is an easier version of the problem. In this version $n \\le 1000$\n\nThe 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.\n\nArchitects 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.\n\nFormally, 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$.\n\nThe 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.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 1000$)\u00a0\u2014 the number of plots.\n\nThe second line contains the integers $m_1, m_2, \\ldots, m_n$ ($1 \\leq m_i \\leq 10^9$)\u00a0\u2014 the limit on the number of floors for every possible number of floors for a skyscraper on each plot.\n\n\n-----Output-----\n\nPrint $n$ integers $a_i$\u00a0\u2014 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.\n\nIf there are multiple answers possible, print any of them.\n\n\n-----Examples-----\nInput\n5\n1 2 3 2 1\n\nOutput\n1 2 3 2 1 \n\nInput\n3\n10 6 8\n\nOutput\n10 6 6 \n\n\n\n-----Note-----\n\nIn the first example, you can build all skyscrapers with the highest possible height.\n\nIn 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."} +{"id": "01639", "text": "Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 \u2264 i \u2264 n) he makes a_{i} money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence a_{i}. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.\n\nHelp Kefa cope with this task!\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5).\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the length of the maximum non-decreasing subsegment of sequence a.\n\n\n-----Examples-----\nInput\n6\n2 2 1 3 4 1\n\nOutput\n3\nInput\n3\n2 2 9\n\nOutput\n3\n\n\n-----Note-----\n\nIn the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.\n\nIn the second test the maximum non-decreasing subsegment is the numbers from the first to the third one."} +{"id": "01640", "text": "Let's denote a function \n\n$d(x, y) = \\left\\{\\begin{array}{ll}{y - x,} & {\\text{if}|x - y|> 1} \\\\{0,} & {\\text{if}|x - y|\\leq 1} \\end{array} \\right.$\n\nYou are given an array a consisting of n integers. You have to calculate the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 \u2264 i \u2264 j \u2264 n.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 200000) \u2014 the number of elements in a.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 elements of the array. \n\n\n-----Output-----\n\nPrint one integer \u2014 the sum of d(a_{i}, a_{j}) over all pairs (i, j) such that 1 \u2264 i \u2264 j \u2264 n.\n\n\n-----Examples-----\nInput\n5\n1 2 3 1 3\n\nOutput\n4\n\nInput\n4\n6 6 5 5\n\nOutput\n0\n\nInput\n4\n6 6 4 4\n\nOutput\n-8\n\n\n\n-----Note-----\n\nIn the first example:\n\n d(a_1, a_2) = 0; d(a_1, a_3) = 2; d(a_1, a_4) = 0; d(a_1, a_5) = 2; d(a_2, a_3) = 0; d(a_2, a_4) = 0; d(a_2, a_5) = 0; d(a_3, a_4) = - 2; d(a_3, a_5) = 0; d(a_4, a_5) = 2."} +{"id": "01641", "text": "Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in t minutes. There is a straight road of length s from the service to the cinema. Let's introduce a coordinate system so that the car rental service is at the point 0, and the cinema is at the point s.\n\nThere are k gas stations along the road, and at each of them you can fill a car with any amount of fuel for free! Consider that this operation doesn't take any time, i.e. is carried out instantly.\n\nThere are n cars in the rental service, i-th of them is characterized with two integers c_{i} and v_{i}\u00a0\u2014 the price of this car rent and the capacity of its fuel tank in liters. It's not allowed to fuel a car with more fuel than its tank capacity v_{i}. All cars are completely fueled at the car rental service.\n\nEach of the cars can be driven in one of two speed modes: normal or accelerated. In the normal mode a car covers 1 kilometer in 2 minutes, and consumes 1 liter of fuel. In the accelerated mode a car covers 1 kilometer in 1 minutes, but consumes 2 liters of fuel. The driving mode can be changed at any moment and any number of times.\n\nYour task is to choose a car with minimum price such that Vasya can reach the cinema before the show starts, i.e. not later than in t minutes. Assume that all cars are completely fueled initially.\n\n\n-----Input-----\n\nThe first line contains four positive integers n, k, s and t (1 \u2264 n \u2264 2\u00b710^5, 1 \u2264 k \u2264 2\u00b710^5, 2 \u2264 s \u2264 10^9, 1 \u2264 t \u2264 2\u00b710^9)\u00a0\u2014 the number of cars at the car rental service, the number of gas stations along the road, the length of the road and the time in which the film starts. \n\nEach of the next n lines contains two positive integers c_{i} and v_{i} (1 \u2264 c_{i}, v_{i} \u2264 10^9)\u00a0\u2014 the price of the i-th car and its fuel tank capacity.\n\nThe next line contains k distinct integers g_1, g_2, ..., g_{k} (1 \u2264 g_{i} \u2264 s - 1)\u00a0\u2014 the positions of the gas stations on the road in arbitrary order.\n\n\n-----Output-----\n\nPrint the minimum rent price of an appropriate car, i.e. such car that Vasya will be able to reach the cinema before the film starts (not later than in t minutes). If there is no appropriate car, print -1.\n\n\n-----Examples-----\nInput\n3 1 8 10\n10 8\n5 7\n11 9\n3\n\nOutput\n10\n\nInput\n2 2 10 18\n10 4\n20 6\n5 3\n\nOutput\n20\n\n\n\n-----Note-----\n\nIn the first sample, Vasya can reach the cinema in time using the first or the third cars, but it would be cheaper to choose the first one. Its price is equal to 10, and the capacity of its fuel tank is 8. Then Vasya can drive to the first gas station in the accelerated mode in 3 minutes, spending 6 liters of fuel. After that he can full the tank and cover 2 kilometers in the normal mode in 4 minutes, spending 2 liters of fuel. Finally, he drives in the accelerated mode covering the remaining 3 kilometers in 3 minutes and spending 6 liters of fuel."} +{"id": "01642", "text": "You are given a convex polygon P with n distinct vertices p_1, p_2, ..., p_{n}. Vertex p_{i} has coordinates (x_{i}, y_{i}) in the 2D plane. These vertices are listed in clockwise order.\n\nYou can choose a real number D and move each vertex of the polygon a distance of at most D from their original positions.\n\nFind the maximum value of D such that no matter how you move the vertices, the polygon does not intersect itself and stays convex.\n\n\n-----Input-----\n\nThe first line has one integer n (4 \u2264 n \u2264 1 000)\u00a0\u2014 the number of vertices.\n\nThe next n lines contain the coordinates of the vertices. Line i contains two integers x_{i} and y_{i} ( - 10^9 \u2264 x_{i}, y_{i} \u2264 10^9)\u00a0\u2014 the coordinates of the i-th vertex. These points are guaranteed to be given in clockwise order, and will form a strictly convex polygon (in particular, no three consecutive points lie on the same straight line).\n\n\n-----Output-----\n\nPrint one real number D, which is the maximum real number such that no matter how you move the vertices, the polygon stays convex.\n\nYour answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}.\n\nNamely, let's assume that your answer is a and the answer of the jury is b. The checker program will consider your answer correct if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n4\n0 0\n0 1\n1 1\n1 0\n\nOutput\n0.3535533906\n\nInput\n6\n5 0\n10 0\n12 -4\n10 -8\n5 -8\n3 -4\n\nOutput\n1.0000000000\n\n\n\n-----Note-----\n\nHere is a picture of the first sample\n\n[Image]\n\nHere is an example of making the polygon non-convex.\n\n[Image]\n\nThis is not an optimal solution, since the maximum distance we moved one point is \u2248 0.4242640687, whereas we can make it non-convex by only moving each point a distance of at most \u2248 0.3535533906."} +{"id": "01643", "text": "The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.\n\nKirk has a binary string $s$ (a string which consists of zeroes and ones) of length $n$ and he is asking you to find a binary string $t$ of the same length which satisfies the following conditions: For any $l$ and $r$ ($1 \\leq l \\leq r \\leq n$) the length of the longest non-decreasing subsequence of the substring $s_{l}s_{l+1} \\ldots s_{r}$ is equal to the length of the longest non-decreasing subsequence of the substring $t_{l}t_{l+1} \\ldots t_{r}$; The number of zeroes in $t$ is the maximum possible.\n\nA non-decreasing subsequence of a string $p$ is a sequence of indices $i_1, i_2, \\ldots, i_k$ such that $i_1 < i_2 < \\ldots < i_k$ and $p_{i_1} \\leq p_{i_2} \\leq \\ldots \\leq p_{i_k}$. The length of the subsequence is $k$.\n\nIf there are multiple substrings which satisfy the conditions, output any.\n\n\n-----Input-----\n\nThe first line contains a binary string of length not more than $10^5$.\n\n\n-----Output-----\n\nOutput a binary string which satisfied the above conditions. If there are many such strings, output any of them.\n\n\n-----Examples-----\nInput\n110\n\nOutput\n010\n\nInput\n010\n\nOutput\n010\n\nInput\n0001111\n\nOutput\n0000000\n\nInput\n0111001100111011101000\n\nOutput\n0011001100001011101000\n\n\n\n-----Note-----\n\nIn the first example: For the substrings of the length $1$ the length of the longest non-decreasing subsequnce is $1$; For $l = 1, r = 2$ the longest non-decreasing subsequnce of the substring $s_{1}s_{2}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{2}$ is $01$; For $l = 1, r = 3$ the longest non-decreasing subsequnce of the substring $s_{1}s_{3}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{3}$ is $00$; For $l = 2, r = 3$ the longest non-decreasing subsequnce of the substring $s_{2}s_{3}$ is $1$ and the longest non-decreasing subsequnce of the substring $t_{2}t_{3}$ is $1$; \n\nThe second example is similar to the first one."} +{"id": "01644", "text": "Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.\n\nThere are n rings in factory's stock. The i-th ring has inner radius a_{i}, outer radius b_{i} and height h_{i}. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: Outer radiuses form a non-increasing sequence, i.e. one can put the j-th ring on the i-th ring only if b_{j} \u2264 b_{i}. Rings should not fall one into the the other. That means one can place ring j on the ring i only if b_{j} > a_{i}. The total height of all rings used should be maximum possible. \n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of rings in factory's stock.\n\nThe i-th of the next n lines contains three integers a_{i}, b_{i} and h_{i} (1 \u2264 a_{i}, b_{i}, h_{i} \u2264 10^9, b_{i} > a_{i})\u00a0\u2014 inner radius, outer radius and the height of the i-th ring respectively.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the maximum height of the tower that can be obtained.\n\n\n-----Examples-----\nInput\n3\n1 5 1\n2 6 2\n3 7 3\n\nOutput\n6\n\nInput\n4\n1 2 1\n1 3 3\n4 6 2\n5 7 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1.\n\nIn the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4."} +{"id": "01645", "text": "Eugene likes working with arrays. And today he needs your help in solving one challenging task.\n\nAn array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.\n\nLet's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $[-1, 2, -3]$ is good, as all arrays $[-1]$, $[-1, 2]$, $[-1, 2, -3]$, $[2]$, $[2, -3]$, $[-3]$ have nonzero sums of elements. However, array $[-1, 2, -1, -3]$ isn't good, as his subarray $[-1, 2, -1]$ has sum of elements equal to $0$.\n\nHelp Eugene to calculate the number of nonempty good subarrays of a given array $a$.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($1 \\le n \\le 2 \\times 10^5$) \u00a0\u2014 the length of array $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^9 \\le a_i \\le 10^9$) \u00a0\u2014 the elements of $a$. \n\n\n-----Output-----\n\nOutput a single integer \u00a0\u2014 the number of good subarrays of $a$.\n\n\n-----Examples-----\nInput\n3\n1 2 -3\n\nOutput\n5\n\nInput\n3\n41 -41 41\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample, the following subarrays are good: $[1]$, $[1, 2]$, $[2]$, $[2, -3]$, $[-3]$. However, the subarray $[1, 2, -3]$ isn't good, as its subarray $[1, 2, -3]$ has sum of elements equal to $0$.\n\nIn the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $[41, -41, 41]$ isn't good, as its subarray $[41, -41]$ has sum of elements equal to $0$."} +{"id": "01646", "text": "String can be called correct if it consists of characters \"0\" and \"1\" and there are no redundant leading zeroes. Here are some examples: \"0\", \"10\", \"1001\".\n\nYou are given a correct string s.\n\nYou can perform two different operations on this string: swap any pair of adjacent characters (for example, \"101\" $\\rightarrow$ \"110\"); replace \"11\" with \"1\" (for example, \"110\" $\\rightarrow$ \"10\"). \n\nLet val(s) be such a number that s is its binary representation.\n\nCorrect string a is less than some other correct string b iff val(a) < val(b).\n\nYour task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).\n\n\n-----Input-----\n\nThe first line contains integer number n (1 \u2264 n \u2264 100) \u2014 the length of string s.\n\nThe second line contains the string s consisting of characters \"0\" and \"1\". It is guaranteed that the string s is correct.\n\n\n-----Output-----\n\nPrint one string \u2014 the minimum correct string that you can obtain from the given one.\n\n\n-----Examples-----\nInput\n4\n1001\n\nOutput\n100\n\nInput\n1\n1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example you can obtain the answer by the following sequence of operations: \"1001\" $\\rightarrow$ \"1010\" $\\rightarrow$ \"1100\" $\\rightarrow$ \"100\".\n\nIn the second example you can't obtain smaller answer no matter what operations you use."} +{"id": "01647", "text": "Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.\n\nThis story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.\n\nMore formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c_1, c_2) (where c_1 and c_2 are some lowercase English letters), which can arbitrary number of times transform a single letter c_1 to c_2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells. \n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5) \u2014 the length of the letterings.\n\nThe second line contains a string with length n, consisting of lowercase English letters\u00a0\u2014 the lettering on Valya's pullover.\n\nThe third line contains the lettering on Tolya's t-shirt in the same format.\n\n\n-----Output-----\n\nIn the first line output a single integer\u00a0\u2014 the minimum amount of mana t required for rescuing love of Valya and Tolya.\n\nIn the next t lines output pairs of space-separated lowercase English letters\u00a0\u2014 spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.\n\nIf there are many optimal answers, output any.\n\n\n-----Examples-----\nInput\n3\nabb\ndad\n\nOutput\n2\na d\nb a\nInput\n8\ndrpepper\ncocacola\n\nOutput\n7\nl e\ne d\nd c\nc p\np o\no r\nr a\n\n\n\n-----Note-----\n\nIn first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'."} +{"id": "01648", "text": "There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.\nFirst, Snuke will arrange the N balls in a row from left to right.\nThen, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.\nHow many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \\leq i \\leq K.\n\n-----Constraints-----\n - 1 \\leq K \\leq N \\leq 2000\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\n\n-----Output-----\nPrint K lines. The i-th line (1 \\leq i \\leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.\n\n-----Sample Input-----\n5 3\n\n-----Sample Output-----\n3\n6\n1\n\nThere are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).\nThere are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).\nThere is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B)."} +{"id": "01649", "text": "Snuke has four cookies with deliciousness A, B, C, and D. He will choose one or more from those cookies and eat them. Is it possible that the sum of the deliciousness of the eaten cookies is equal to that of the remaining cookies?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq A,B,C,D \\leq 10^{8}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B C D\n\n-----Output-----\nIf it is possible that the sum of the deliciousness of the eaten cookies is equal to that of the remaining cookies, print Yes; otherwise, print No.\n\n-----Sample Input-----\n1 3 2 4\n\n-----Sample Output-----\nYes\n\n - If he eats the cookies with deliciousness 1 and 4, the sum of the deliciousness of the eaten cookies will be equal to that of the remaining cookies."} +{"id": "01650", "text": "You are given a positive integer L in base two.\nHow many pairs of non-negative integers (a, b) satisfy the following conditions?\n - a + b \\leq L\n - a + b = a \\mbox{ XOR } b\nSince there can be extremely many such pairs, print the count modulo 10^9 + 7.\n What is XOR?\nThe XOR of integers A and B, A \\mbox{ XOR } B, is defined as follows:\n - When A \\mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.\nFor example, 3 \\mbox{ XOR } 5 = 6. (In base two: 011 \\mbox{ XOR } 101 = 110.)\n\n-----Constraints-----\n - L is given in base two, without leading zeros.\n - 1 \\leq L < 2^{100\\ 001}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nL\n\n-----Output-----\nPrint the number of pairs (a, b) that satisfy the conditions, modulo 10^9 + 7.\n\n-----Sample Input-----\n10\n\n-----Sample Output-----\n5\n\nFive pairs (a, b) satisfy the conditions: (0, 0), (0, 1), (1, 0), (0, 2) and (2, 0)."} +{"id": "01651", "text": "Given are integers S and P.\nIs there a pair of positive integers (N,M) such that N + M = S and N \\times M = P?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq S,P \\leq 10^{12}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS P\n\n-----Output-----\nIf there is a pair of positive integers (N,M) such that N + M = S and N \\times M = P, print Yes; otherwise, print No.\n\n-----Sample Input-----\n3 2\n\n-----Sample Output-----\nYes\n\n - For example, we have N+M=3 and N \\times M =2 for N=1,M=2."} +{"id": "01652", "text": "You are given a string S consisting of lowercase English letters.\nAnother string T is initially empty.\nDetermine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:\n - Append one of the following at the end of T: dream, dreamer, erase and eraser.\n\n-----Constraints-----\n - 1\u2266|S|\u226610^5\n - S consists of lowercase English letters.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nS\n\n-----Output-----\nIf it is possible to obtain S = T, print YES. Otherwise, print NO.\n\n-----Sample Input-----\nerasedream\n\n-----Sample Output-----\nYES\n\nAppend erase and dream at the end of T in this order, to obtain S = T."} +{"id": "01653", "text": "Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A $\\rightarrow$ BC B $\\rightarrow$ AC C $\\rightarrow$ AB AAA $\\rightarrow$ empty string \n\nNote that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.\n\n\n-----Input-----\n\nThe first line contains a string S (1 \u2264 |S| \u2264 10^5). The second line contains a string T (1 \u2264 |T| \u2264 10^5), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'.\n\nThe third line contains the number of queries Q (1 \u2264 Q \u2264 10^5).\n\nThe following Q lines describe queries. The i-th of these lines contains four space separated integers a_{i}, b_{i}, c_{i}, d_{i}. These represent the i-th query: is it possible to create T[c_{i}..d_{i}] from S[a_{i}..b_{i}] by applying the above transitions finite amount of times?\n\nHere, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U.\n\nIt is guaranteed that 1 \u2264 a \u2264 b \u2264 |S| and 1 \u2264 c \u2264 d \u2264 |T|.\n\n\n-----Output-----\n\nPrint a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise.\n\n\n-----Example-----\nInput\nAABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3\n\nOutput\n10011\n\n\n\n-----Note-----\n\nIn the first query we can achieve the result, for instance, by using transitions $A A B \\rightarrow A A A C \\rightarrow \\operatorname{AAA} A B \\rightarrow A B$.\n\nThe third query asks for changing AAB to A\u00a0\u2014 but in this case we are not able to get rid of the character 'B'."} +{"id": "01654", "text": "You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. \n\nSuitability of string s is calculated by following metric:\n\nAny two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.\n\nYou should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.\n\n\n-----Input-----\n\nThe first line contains string s (1 \u2264 |s| \u2264 10^6).\n\nThe second line contains string t (1 \u2264 |t| \u2264 10^6).\n\n\n-----Output-----\n\nPrint string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.\n\nIf there are multiple strings with maximal suitability then print any of them.\n\n\n-----Examples-----\nInput\n?aa?\nab\n\nOutput\nbaab\n\nInput\n??b?\nza\n\nOutput\nazbz\n\nInput\nabcd\nabacaba\n\nOutput\nabcd\n\n\n\n-----Note-----\n\nIn the first example string \"baab\" can be transformed to \"abab\" with swaps, this one has suitability of 2. That means that string \"baab\" also has suitability of 2.\n\nIn the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, \"azbz\" is just one of them.\n\nIn the third example there are no '?' characters and the suitability of the string is 0."} +{"id": "01655", "text": "Hands that shed innocent blood!\n\nThere are n guilty people in a line, the i-th of them holds a claw with length L_{i}. The bell rings and every person kills some of people in front of him. All people kill others at the same time. Namely, the i-th person kills the j-th person if and only if j < i and j \u2265 i - L_{i}.\n\nYou are given lengths of the claws. You need to find the total number of alive people after the bell rings.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 10^6) \u2014 the number of guilty people.\n\nSecond line contains n space-separated integers L_1, L_2, ..., L_{n} (0 \u2264 L_{i} \u2264 10^9), where L_{i} is the length of the i-th person's claw.\n\n\n-----Output-----\n\nPrint one integer \u2014 the total number of alive people after the bell rings.\n\n\n-----Examples-----\nInput\n4\n0 1 0 10\n\nOutput\n1\n\nInput\n2\n0 0\n\nOutput\n2\n\nInput\n10\n1 1 3 0 0 0 2 1 0 3\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn first sample the last person kills everyone in front of him."} +{"id": "01656", "text": "Recall that string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly zero or all) characters. For example, for the string $a$=\"wowwo\", the following strings are subsequences: \"wowwo\", \"wowo\", \"oo\", \"wow\", \"\", and others, but the following are not subsequences: \"owoo\", \"owwwo\", \"ooo\".\n\nThe wow factor of a string is the number of its subsequences equal to the word \"wow\". Bob wants to write a string that has a large wow factor. However, the \"w\" key on his keyboard is broken, so he types two \"v\"s instead. \n\nLittle did he realise that he may have introduced more \"w\"s than he thought. Consider for instance the string \"ww\". Bob would type it as \"vvvv\", but this string actually contains three occurrences of \"w\": \"vvvv\" \"vvvv\" \"vvvv\" \n\nFor example, the wow factor of the word \"vvvovvv\" equals to four because there are four wows: \"vvvovvv\" \"vvvovvv\" \"vvvovvv\" \"vvvovvv\" \n\nNote that the subsequence \"vvvovvv\" does not count towards the wow factor, as the \"v\"s have to be consecutive.\n\nFor a given string $s$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $s$ from another string replacing \"w\" with \"vv\". For example, $s$ can be equal to \"vov\".\n\n\n-----Input-----\n\nThe input contains a single non-empty string $s$, consisting only of characters \"v\" and \"o\". The length of $s$ is at most $10^6$.\n\n\n-----Output-----\n\nOutput a single integer, the wow factor of $s$.\n\n\n-----Examples-----\nInput\nvvvovvv\n\nOutput\n4\n\nInput\nvvovooovovvovoovoovvvvovovvvov\n\nOutput\n100\n\n\n\n-----Note-----\n\nThe first example is explained in the legend."} +{"id": "01657", "text": "You have n devices that you want to use simultaneously.\n\nThe i-th device uses a_{i} units of power per second. This usage is continuous. That is, in \u03bb seconds, the device will use \u03bb\u00b7a_{i} units of power. The i-th device currently has b_{i} units of power stored. All devices can store an arbitrary amount of power.\n\nYou have a single charger that can plug to any single device. The charger will add p units of power per second to a device. This charging is continuous. That is, if you plug in a device for \u03bb seconds, it will gain \u03bb\u00b7p units of power. You can switch which device is charging at any arbitrary unit of time (including real numbers), and the time it takes to switch is negligible.\n\nYou are wondering, what is the maximum amount of time you can use the devices until one of them hits 0 units of power.\n\nIf you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.\n\n\n-----Input-----\n\nThe first line contains two integers, n and p (1 \u2264 n \u2264 100 000, 1 \u2264 p \u2264 10^9)\u00a0\u2014 the number of devices and the power of the charger.\n\nThis is followed by n lines which contain two integers each. Line i contains the integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 100 000)\u00a0\u2014 the power of the device and the amount of power stored in the device in the beginning.\n\n\n-----Output-----\n\nIf you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.\n\nYour answer will be considered correct if its absolute or relative error does not exceed 10^{ - 4}.\n\nNamely, let's assume that your answer is a and the answer of the jury is b. The checker program will consider your answer correct if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-4}$.\n\n\n-----Examples-----\nInput\n2 1\n2 2\n2 1000\n\nOutput\n2.0000000000\nInput\n1 100\n1 1\n\nOutput\n-1\n\nInput\n3 5\n4 3\n5 2\n6 1\n\nOutput\n0.5000000000\n\n\n-----Note-----\n\nIn sample test 1, you can charge the first device for the entire time until it hits zero power. The second device has enough power to last this time without being charged.\n\nIn sample test 2, you can use the device indefinitely.\n\nIn sample test 3, we can charge the third device for 2 / 5 of a second, then switch to charge the second device for a 1 / 10 of a second."} +{"id": "01658", "text": "When Darth Vader gets bored, he sits down on the sofa, closes his eyes and thinks of an infinite rooted tree where each node has exactly n sons, at that for each node, the distance between it an its i-th left child equals to d_{i}. The Sith Lord loves counting the number of nodes in the tree that are at a distance at most x from the root. The distance is the sum of the lengths of edges on the path between nodes.\n\nBut he has got used to this activity and even grew bored of it. 'Why does he do that, then?' \u2014 you may ask. It's just that he feels superior knowing that only he can solve this problem. \n\nDo you want to challenge Darth Vader himself? Count the required number of nodes. As the answer can be rather large, find it modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and x (1 \u2264 n \u2264 10^5, 0 \u2264 x \u2264 10^9) \u2014 the number of children of each node and the distance from the root within the range of which you need to count the nodes.\n\nThe next line contains n space-separated integers d_{i} (1 \u2264 d_{i} \u2264 100) \u2014 the length of the edge that connects each node with its i-th child.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of vertexes in the tree at distance from the root equal to at most x. \n\n\n-----Examples-----\nInput\n3 3\n1 2 3\n\nOutput\n8\n\n\n\n-----Note-----\n\nPictures to the sample (the yellow color marks the nodes the distance to which is at most three) [Image]"} +{"id": "01659", "text": "After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.\n\nAt the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the queue wants either to take several ice cream packs for himself and his friends or to give several ice cream packs to Kay and Gerda (carriers that bring ice cream have to stand in the same queue).\n\nIf a carrier with d ice cream packs comes to the house, then Kay and Gerda take all his packs. If a child who wants to take d ice cream packs comes to the house, then Kay and Gerda will give him d packs if they have enough ice cream, otherwise the child will get no ice cream at all and will leave in distress.\n\nKay wants to find the amount of ice cream they will have after all people will leave from the queue, and Gerda wants to find the number of distressed kids.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and x (1 \u2264 n \u2264 1000, 0 \u2264 x \u2264 10^9).\n\nEach of the next n lines contains a character '+' or '-', and an integer d_{i}, separated by a space (1 \u2264 d_{i} \u2264 10^9). Record \"+ d_{i}\" in i-th line means that a carrier with d_{i} ice cream packs occupies i-th place from the start of the queue, and record \"- d_{i}\" means that a child who wants to take d_{i} packs stands in i-th place.\n\n\n-----Output-----\n\nPrint two space-separated integers\u00a0\u2014 number of ice cream packs left after all operations, and number of kids that left the house in distress.\n\n\n-----Examples-----\nInput\n5 7\n+ 5\n- 10\n- 20\n+ 40\n- 20\n\nOutput\n22 1\n\nInput\n5 17\n- 16\n- 2\n- 98\n+ 100\n- 98\n\nOutput\n3 2\n\n\n\n-----Note-----\n\nConsider the first sample. Initially Kay and Gerda have 7 packs of ice cream. Carrier brings 5 more, so now they have 12 packs. A kid asks for 10 packs and receives them. There are only 2 packs remaining. Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed. Carrier bring 40 packs, now Kay and Gerda have 42 packs. Kid asks for 20 packs and receives them. There are 22 packs remaining."} +{"id": "01660", "text": "Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.\n\nYou are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.\n\nHelp Pashmak, print the number of edges in the required path.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (2 \u2264 n \u2264 3\u00b710^5;\u00a01 \u2264 m \u2264 min(n\u00b7(n - 1), 3\u00b710^5)). Then, m lines follows. The i-th line contains three space separated integers: u_{i}, v_{i}, w_{i} (1 \u2264 u_{i}, v_{i} \u2264 n;\u00a01 \u2264 w_{i} \u2264 10^5) which indicates that there's a directed edge with weight w_{i} from vertex u_{i} to vertex v_{i}.\n\nIt's guaranteed that the graph doesn't contain self-loops and multiple edges.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n3 3\n1 2 1\n2 3 1\n3 1 1\n\nOutput\n1\n\nInput\n3 3\n1 2 1\n2 3 2\n3 1 3\n\nOutput\n3\n\nInput\n6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first sample the maximum trail can be any of this trails: $1 \\rightarrow 2,2 \\rightarrow 3,3 \\rightarrow 1$.\n\nIn the second sample the maximum trail is $1 \\rightarrow 2 \\rightarrow 3 \\rightarrow 1$.\n\nIn the third sample the maximum trail is $1 \\rightarrow 2 \\rightarrow 5 \\rightarrow 4 \\rightarrow 3 \\rightarrow 2 \\rightarrow 6$."} +{"id": "01661", "text": "Maxim wants to buy some games at the local game shop. There are $n$ games in the shop, the $i$-th game costs $c_i$.\n\nMaxim has a wallet which can be represented as an array of integers. His wallet contains $m$ bills, the $j$-th bill has value $a_j$.\n\nGames in the shop are ordered from left to right, Maxim tries to buy every game in that order.\n\nWhen Maxim stands at the position $i$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $i$-th game using this bill. After Maxim tried to buy the $n$-th game, he leaves the shop.\n\nMaxim buys the $i$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $i$-th game. If he successfully buys the $i$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.\n\nFor example, for array $c = [2, 4, 5, 2, 4]$ and array $a = [5, 3, 4, 6]$ the following process takes place: Maxim buys the first game using the first bill (its value is $5$), the bill disappears, after that the second bill (with value $3$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $c_2 > a_2$, the same with the third game, then he buys the fourth game using the bill of value $a_2$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $a_3$.\n\nYour task is to get the number of games Maxim will buy.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n, m \\le 1000$) \u2014 the number of games and the number of bills in Maxim's wallet.\n\nThe second line of the input contains $n$ integers $c_1, c_2, \\dots, c_n$ ($1 \\le c_i \\le 1000$), where $c_i$ is the cost of the $i$-th game.\n\nThe third line of the input contains $m$ integers $a_1, a_2, \\dots, a_m$ ($1 \\le a_j \\le 1000$), where $a_j$ is the value of the $j$-th bill from the Maxim's wallet.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of games Maxim will buy.\n\n\n-----Examples-----\nInput\n5 4\n2 4 5 2 4\n5 3 4 6\n\nOutput\n3\n\nInput\n5 2\n20 40 50 20 40\n19 20\n\nOutput\n0\n\nInput\n6 4\n4 8 15 16 23 42\n1000 1000 1000 1000\n\nOutput\n4\n\n\n\n-----Note-----\n\nThe first example is described in the problem statement.\n\nIn the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.\n\nIn the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet."} +{"id": "01662", "text": "Sereja loves integer sequences very much. He especially likes stairs.\n\nSequence a_1, a_2, ..., a_{|}a| (|a| is the length of the sequence) is stairs if there is such index i (1 \u2264 i \u2264 |a|), that the following condition is met: a_1 < a_2 < ... < a_{i} - 1 < a_{i} > a_{i} + 1 > ... > a_{|}a| - 1 > a_{|}a|.\n\nFor example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't.\n\nSereja has m cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the table?\n\n\n-----Input-----\n\nThe first line contains integer m (1 \u2264 m \u2264 10^5) \u2014 the number of Sereja's cards. The second line contains m integers b_{i} (1 \u2264 b_{i} \u2264 5000) \u2014 the numbers on the Sereja's cards.\n\n\n-----Output-----\n\nIn the first line print the number of cards you can put on the table. In the second line print the resulting stairs.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n\nOutput\n5\n5 4 3 2 1\n\nInput\n6\n1 1 2 2 3 3\n\nOutput\n5\n1 2 3 2 1"} +{"id": "01663", "text": "Sometimes it is not easy to come to an agreement in a bargain. Right now Sasha and Vova can't come to an agreement: Sasha names a price as high as possible, then Vova wants to remove as many digits from the price as possible. In more details, Sasha names some integer price $n$, Vova removes a non-empty substring of (consecutive) digits from the price, the remaining digits close the gap, and the resulting integer is the price.\n\nFor example, is Sasha names $1213121$, Vova can remove the substring $1312$, and the result is $121$.\n\nIt is allowed for result to contain leading zeros. If Vova removes all digits, the price is considered to be $0$.\n\nSasha wants to come up with some constraints so that Vova can't just remove all digits, but he needs some arguments supporting the constraints. To start with, he wants to compute the sum of all possible resulting prices after Vova's move.\n\nHelp Sasha to compute this sum. Since the answer can be very large, print it modulo $10^9 + 7$.\n\n\n-----Input-----\n\nThe first and only line contains a single integer $n$ ($1 \\le n < 10^{10^5}$).\n\n\n-----Output-----\n\nIn the only line print the required sum modulo $10^9 + 7$.\n\n\n-----Examples-----\nInput\n107\n\nOutput\n42\n\nInput\n100500100500\n\nOutput\n428101984\n\n\n\n-----Note-----\n\nConsider the first example.\n\nVova can choose to remove $1$, $0$, $7$, $10$, $07$, or $107$. The results are $07$, $17$, $10$, $7$, $1$, $0$. Their sum is $42$."} +{"id": "01664", "text": "Let's analyze a program written on some strange programming language. The variables in this language have names consisting of $1$ to $4$ characters, and each character is a lowercase or an uppercase Latin letter, or a digit. There is an extra constraint that the first character should not be a digit.\n\nThere are four types of operations in the program, each denoted by one of the characters: $, ^, # or &.\n\nEach line of the program has one of the following formats: =, where and are valid variable names; =, where , and are valid variable names, and is an operation character. \n\nThe program is executed line-by-line, and the result of execution is stored in a variable having the name res. If res is never assigned in the program, then the result will be equal to the value of res before running the program.\n\nTwo programs are called equivalent if no matter which operations do characters $, ^, # and & denote (but, obviously, performing the same operation on the same arguments gives the same result) and which values do variables have before execution of program, the value of res after running the first program is equal to the value of res after running the second program (the programs are executed independently).\n\nYou are given a program consisting of $n$ lines. Your task is to write a program consisting of minimum possible number of lines that is equivalent to the program you are given.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 1000$) \u2014 the number of lines in the program.\n\nThen $n$ lines follow \u2014 the program itself. Each line corresponds to the format described in the statement and has no extra whitespaces.\n\n\n-----Output-----\n\nIn the first line print $k$ \u2014 the minimum number of lines in the equivalent program.\n\nThen print $k$ lines without any whitespaces \u2014 an equivalent program having exactly $k$ lines, in the same format it is described in the statement.\n\n\n-----Examples-----\nInput\n4\nc=aa#bb\nd12=c\nres=c^d12\ntmp=aa$c\n\nOutput\n2\naaaa=aa#bb\nres=aaaa^aaaa\n\nInput\n2\nmax=aaaa$bbbb\nmin=bbbb^aaaa\n\nOutput\n0"} +{"id": "01665", "text": "You are given a tree consisting of $n$ nodes. You want to write some labels on the tree's edges such that the following conditions hold:\n\n Every label is an integer between $0$ and $n-2$ inclusive. All the written labels are distinct. The largest value among $MEX(u,v)$ over all pairs of nodes $(u,v)$ is as small as possible. \n\nHere, $MEX(u,v)$ denotes the smallest non-negative integer that isn't written on any edge on the unique simple path from node $u$ to node $v$.\n\n\n-----Input-----\n\nThe first line contains the integer $n$ ($2 \\le n \\le 10^5$)\u00a0\u2014 the number of nodes in the tree.\n\nEach of the next $n-1$ lines contains two space-separated integers $u$ and $v$ ($1 \\le u,v \\le n$) that mean there's an edge between nodes $u$ and $v$. It's guaranteed that the given graph is a tree.\n\n\n-----Output-----\n\nOutput $n-1$ integers. The $i^{th}$ of them will be the number written on the $i^{th}$ edge (in the input order).\n\n\n-----Examples-----\nInput\n3\n1 2\n1 3\n\nOutput\n0\n1\n\nInput\n6\n1 2\n1 3\n2 4\n2 5\n5 6\n\nOutput\n0\n3\n2\n4\n1\n\n\n-----Note-----\n\nThe tree from the second sample:\n\n[Image]"} +{"id": "01666", "text": "Petya and Vasya are tossing a coin. Their friend Valera is appointed as a judge. The game is very simple. First Vasya tosses a coin x times, then Petya tosses a coin y times. If the tossing player gets head, he scores one point. If he gets tail, nobody gets any points. The winner is the player with most points by the end of the game. If boys have the same number of points, the game finishes with a draw.\n\nAt some point, Valera lost his count, and so he can not say exactly what the score is at the end of the game. But there are things he remembers for sure. He remembers that the entire game Vasya got heads at least a times, and Petya got heads at least b times. Moreover, he knows that the winner of the game was Vasya. Valera wants to use this information to know every possible outcome of the game, which do not contradict his memories.\n\n\n-----Input-----\n\nThe single line contains four integers x, y, a, b (1 \u2264 a \u2264 x \u2264 100, 1 \u2264 b \u2264 y \u2264 100). The numbers on the line are separated by a space.\n\n\n-----Output-----\n\nIn the first line print integer n \u2014 the number of possible outcomes of the game. Then on n lines print the outcomes. On the i-th line print a space-separated pair of integers c_{i}, d_{i} \u2014 the number of heads Vasya and Petya got in the i-th outcome of the game, correspondingly. Print pairs of integers (c_{i}, d_{i}) in the strictly increasing order.\n\nLet us remind you that the pair of numbers (p_1, q_1) is less than the pair of numbers (p_2, q_2), if p_1 < p_2, or p_1 = p_2 and also q_1 < q_2.\n\n\n-----Examples-----\nInput\n3 2 1 1\n\nOutput\n3\n2 1\n3 1\n3 2\n\nInput\n2 4 2 2\n\nOutput\n0"} +{"id": "01667", "text": "For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations.\n\nMax is a young biologist. For $n$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $k$ that if the shark in some day traveled the distance strictly less than $k$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $k$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $k$.\n\nThe shark never returned to the same location after it has moved from it. Thus, in the sequence of $n$ days we can find consecutive nonempty segments when the shark traveled the distance less than $k$ in each of the days: each such segment corresponds to one location. Max wants to choose such $k$ that the lengths of all such segments are equal.\n\nFind such integer $k$, that the number of locations is as large as possible. If there are several such $k$, print the smallest one.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^5$) \u2014 the number of days.\n\nThe second line contains $n$ distinct positive integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^9$) \u2014 the distance traveled in each of the day.\n\n\n-----Output-----\n\nPrint a single integer $k$, such that the shark was in each location the same number of days, the number of locations is maximum possible satisfying the first condition, $k$ is smallest possible satisfying the first and second conditions. \n\n\n-----Examples-----\nInput\n8\n1 2 7 3 4 8 5 6\n\nOutput\n7\nInput\n6\n25 1 2 3 14 36\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first example the shark travels inside a location on days $1$ and $2$ (first location), then on $4$-th and $5$-th days (second location), then on $7$-th and $8$-th days (third location). There are three locations in total.\n\nIn the second example the shark only moves inside a location on the $2$-nd day, so there is only one location."} +{"id": "01668", "text": "A PIN code is a string that consists of exactly $4$ digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0.\n\nPolycarp has $n$ ($2 \\le n \\le 10$) bank cards, the PIN code of the $i$-th card is $p_i$.\n\nPolycarp has recently read a recommendation that it is better to set different PIN codes on different cards. Thus he wants to change the minimal number of digits in the PIN codes of his cards so that all $n$ codes would become different.\n\nFormally, in one step, Polycarp picks $i$-th card ($1 \\le i \\le n$), then in its PIN code $p_i$ selects one position (from $1$ to $4$), and changes the digit in this position to any other. He needs to change the minimum number of digits so that all PIN codes become different.\n\nPolycarp quickly solved this problem. Can you solve it?\n\n\n-----Input-----\n\nThe first line contains integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases in the input. Then test cases follow.\n\nThe first line of each of $t$ test sets contains a single integer $n$ ($2 \\le n \\le 10$) \u2014 the number of Polycarp's bank cards. The next $n$ lines contain the PIN codes $p_1, p_2, \\dots, p_n$ \u2014 one per line. The length of each of them is $4$. All PIN codes consist of digits only.\n\n\n-----Output-----\n\nPrint the answers to $t$ test sets. The answer to each set should consist of a $n + 1$ lines\n\nIn the first line print $k$ \u2014 the least number of changes to make all PIN codes different. In the next $n$ lines output the changed PIN codes in the order corresponding to their appearance in the input. If there are several optimal answers, print any of them.\n\n\n-----Example-----\nInput\n3\n2\n1234\n0600\n2\n1337\n1337\n4\n3139\n3139\n3139\n3139\n\nOutput\n0\n1234\n0600\n1\n1337\n1237\n3\n3139\n3138\n3939\n6139"} +{"id": "01669", "text": "International Coding Procedures Company (ICPC) writes all its code in Jedi Script (JS) programming language. JS does not get compiled, but is delivered for execution in its source form. Sources contain comments, extra whitespace (including trailing and leading spaces), and other non-essential features that make them quite large but do not contribute to the semantics of the code, so the process of minification is performed on source files before their delivery to execution to compress sources while preserving their semantics.\n\nYou are hired by ICPC to write JS minifier for ICPC. Fortunately, ICPC adheres to very strict programming practices and their JS sources are quite restricted in grammar. They work only on integer algorithms and do not use floating point numbers and strings. \n\nEvery JS source contains a sequence of lines. Each line contains zero or more tokens that can be separated by spaces. On each line, a part of the line that starts with a hash character ('#' code 35), including the hash character itself, is treated as a comment and is ignored up to the end of the line.\n\nEach line is parsed into a sequence of tokens from left to right by repeatedly skipping spaces and finding the longest possible token starting at the current parsing position, thus transforming the source code into a sequence of tokens. All the possible tokens are listed below: A reserved token is any kind of operator, separator, literal, reserved word, or a name of a library function that should be preserved during the minification process. Reserved tokens are fixed strings of non-space ASCII characters that do not contain the hash character ('#' code 35). All reserved tokens are given as an input to the minification process. A number token consists of a sequence of digits, where a digit is a character from zero ('0') to nine ('9') inclusive. A word token consists of a sequence of characters from the following set: lowercase letters, uppercase letters, digits, underscore ('_' code 95), and dollar sign ('$' code 36). A word does not start with a digit. \n\nNote, that during parsing the longest sequence of characters that satisfies either a number or a word definition, but that appears in the list of reserved tokens, is considered to be a reserved token instead.\n\nDuring the minification process words are renamed in a systematic fashion using the following algorithm: Take a list of words that consist only of lowercase letters ordered first by their length, then lexicographically: \"a\", \"b\", ..., \"z\", \"aa\", \"ab\", ..., excluding reserved tokens, since they are not considered to be words. This is the target word list. Rename the first word encountered in the input token sequence to the first word in the target word list and all further occurrences of the same word in the input token sequence, too. Rename the second new word encountered in the input token sequence to the second word in the target word list, and so on. \n\nThe goal of the minification process is to convert the given source to the shortest possible line (counting spaces) that still parses to the same sequence of tokens with the correspondingly renamed words using these JS parsing rules. \n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($0 \\le n \\le 40$)\u00a0\u2014 the number of reserved tokens.\n\nThe second line of the input contains the list of reserved tokens separated by spaces without repetitions in the list. Each reserved token is at least one and at most 20 characters long and contains only characters with ASCII codes from 33 (exclamation mark) to 126 (tilde) inclusive, with exception of a hash character ('#' code 35).\n\nThe third line of the input contains a single integer $m$ ($1 \\le m \\le 40$)\u00a0\u2014 the number of lines in the input source code.\n\nNext $m$ lines contain the input source, each source line is at most 80 characters long (counting leading and trailing spaces). Each line contains only characters with ASCII codes from 32 (space) to 126 (tilde) inclusive. The source code is valid and fully parses into a sequence of tokens.\n\n\n-----Output-----\n\nWrite to the output a single line that is the result of the minification process on the input source code. The output source line shall parse to the same sequence of tokens as the input source with the correspondingly renamed words and shall contain the minimum possible number of spaces needed for that. If there are multiple ways to insert the minimum possible number of spaces into the output, use any way. \n\n\n-----Examples-----\nInput\n16\nfun while return var { } ( ) , ; > = + ++ - --\n9\nfun fib(num) { # compute fibs\n var return_value = 1, prev = 0, temp;\n while (num > 0) {\n temp = return_value; return_value = return_value + prev;\n prev = temp;\n num--;\n }\n return return_value;\n}\n\nOutput\nfun a(b){var c=1,d=0,e;while(b>0){e=c;c=c+d;d=e;b--;}return c;}\n\nInput\n10\n( ) + ++ : -> >> >>: b c)\n2\n($val1++ + +4 kb) >> :out\nb-> + 10 >>: t # using >>: \n\nOutput\n(a+++ +4c )>> :d b->+10>>:e"} +{"id": "01670", "text": "Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.\n\nVasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.\n\n\n-----Input-----\n\nThe first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.\n\nNext follows number n (1 \u2264 n \u2264 90) \u2014 the number of fouls. \n\nEach of the following n lines contains information about a foul in the following form: first goes number t (1 \u2264 t \u2264 90) \u2014 the minute when the foul occurs; then goes letter \"h\" or letter \"a\" \u2014 if the letter is \"h\", then the card was given to a home team player, otherwise the card was given to an away team player; then goes the player's number m (1 \u2264 m \u2264 99); then goes letter \"y\" or letter \"r\" \u2014 if the letter is \"y\", that means that the yellow card was given, otherwise the red card was given. \n\nThe players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.\n\n\n-----Output-----\n\nFor each event when a player received his first red card in a chronological order print a string containing the following information: The name of the team to which the player belongs; the player's number in his team; the minute when he received the card. \n\nIf no player received a card, then you do not need to print anything.\n\nIt is possible case that the program will not print anything to the output (if there were no red cards).\n\n\n-----Examples-----\nInput\nMC\nCSKA\n9\n28 a 3 y\n62 h 25 y\n66 h 42 y\n70 h 25 y\n77 a 4 y\n79 a 25 y\n82 h 42 r\n89 h 16 y\n90 a 13 r\n\nOutput\nMC 25 70\nMC 42 82\nCSKA 13 90"} +{"id": "01671", "text": "In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are m_{i} tasks assigned to the i-th server.\n\nIn order to balance the load for each server, you want to reassign some tasks to make the difference between the most loaded server and the least loaded server as small as possible. In other words you want to minimize expression m_{a} - m_{b}, where a is the most loaded server and b is the least loaded one.\n\nIn one second you can reassign a single task. Thus in one second you can choose any pair of servers and move a single task from one server to another.\n\nWrite a program to find the minimum number of seconds needed to balance the load of servers.\n\n\n-----Input-----\n\nThe first line contains positive number n (1 \u2264 n \u2264 10^5) \u2014 the number of the servers. \n\nThe second line contains the sequence of non-negative integers m_1, m_2, ..., m_{n} (0 \u2264 m_{i} \u2264 2\u00b710^4), where m_{i} is the number of tasks assigned to the i-th server.\n\n\n-----Output-----\n\nPrint the minimum number of seconds required to balance the load.\n\n\n-----Examples-----\nInput\n2\n1 6\n\nOutput\n2\n\nInput\n7\n10 11 10 11 10 11 11\n\nOutput\n0\n\nInput\n5\n1 2 3 4 5\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2.\n\nIn the second example the load is already balanced.\n\nA possible sequence of task movements for the third example is: move a task from server #4 to server #1 (the sequence m becomes: 2 2 3 3 5); then move task from server #5 to server #1 (the sequence m becomes: 3 2 3 3 4); then move task from server #5 to server #2 (the sequence m becomes: 3 3 3 3 3). \n\nThe above sequence is one of several possible ways to balance the load of servers in three seconds."} +{"id": "01672", "text": "Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a \"plus\") and negative (a \"minus\"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.\n\nMike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. [Image] \n\nMike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 100000) \u2014 the number of magnets. Then n lines follow. The i-th line (1 \u2264 i \u2264 n) contains either characters \"01\", if Mike put the i-th magnet in the \"plus-minus\" position, or characters \"10\", if Mike put the magnet in the \"minus-plus\" position.\n\n\n-----Output-----\n\nOn the single line of the output print the number of groups of magnets.\n\n\n-----Examples-----\nInput\n6\n10\n10\n10\n01\n10\n10\n\nOutput\n3\n\nInput\n4\n01\n01\n10\n10\n\nOutput\n2\n\n\n\n-----Note-----\n\nThe first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.\n\nThe second testcase has two groups, each consisting of two magnets."} +{"id": "01673", "text": "Let's call beauty of an array $b_1, b_2, \\ldots, b_n$ ($n > 1$) \u00a0\u2014 $\\min\\limits_{1 \\leq i < j \\leq n} |b_i - b_j|$.\n\nYou're given an array $a_1, a_2, \\ldots a_n$ and a number $k$. Calculate the sum of beauty over all subsequences of the array of length exactly $k$. As this number can be very large, output it modulo $998244353$.\n\nA 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.\n\n\n-----Input-----\n\nThe first line contains integers $n, k$ ($2 \\le k \\le n \\le 1000$).\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 10^5$).\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 the sum of beauty over all subsequences of the array of length exactly $k$. As this number can be very large, output it modulo $998244353$.\n\n\n-----Examples-----\nInput\n4 3\n1 7 3 5\n\nOutput\n8\nInput\n5 5\n1 10 100 1000 10000\n\nOutput\n9\n\n\n-----Note-----\n\nIn the first example, there are $4$ subsequences of length $3$\u00a0\u2014 $[1, 7, 3]$, $[1, 3, 5]$, $[7, 3, 5]$, $[1, 7, 5]$, each of which has beauty $2$, so answer is $8$.\n\nIn the second example, there is only one subsequence of length $5$\u00a0\u2014 the whole array, which has the beauty equal to $|10-1| = 9$."} +{"id": "01674", "text": "You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.\n\nYou are playing the game on the new generation console so your gamepad have $26$ buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.\n\nYou are given a sequence of hits, the $i$-th hit deals $a_i$ units of damage to the opponent's character. To perform the $i$-th hit you have to press the button $s_i$ on your gamepad. Hits are numbered from $1$ to $n$.\n\nYou know that if you press some button more than $k$ times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.\n\nTo perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of $a_i$ over all $i$ for the hits which weren't skipped.\n\nNote that if you skip the hit then the counter of consecutive presses the button won't reset.\n\nYour task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$) \u2014 the number of hits and the maximum number of times you can push the same button in a row.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the damage of the $i$-th hit.\n\nThe third line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters \u2014 the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit).\n\n\n-----Output-----\n\nPrint one integer $dmg$ \u2014 the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons.\n\n\n-----Examples-----\nInput\n7 3\n1 5 16 18 7 2 10\nbaaaaca\n\nOutput\n54\n\nInput\n5 5\n2 4 1 3 1000\naaaaa\n\nOutput\n1010\n\nInput\n5 4\n2 4 1 3 1000\naaaaa\n\nOutput\n1009\n\nInput\n8 1\n10 15 2 1 4 8 15 16\nqqwweerr\n\nOutput\n41\n\nInput\n6 3\n14 18 9 19 2 15\ncccccc\n\nOutput\n52\n\nInput\n2 1\n10 10\nqq\n\nOutput\n10\n\n\n\n-----Note-----\n\nIn the first example you can choose hits with numbers $[1, 3, 4, 5, 6, 7]$ with the total damage $1 + 16 + 18 + 7 + 2 + 10 = 54$.\n\nIn the second example you can choose all hits so the total damage is $2 + 4 + 1 + 3 + 1000 = 1010$.\n\nIn the third example you can choose all hits expect the third one so the total damage is $2 + 4 + 3 + 1000 = 1009$.\n\nIn the fourth example you can choose hits with numbers $[2, 3, 6, 8]$. Only this way you can reach the maximum total damage $15 + 2 + 8 + 16 = 41$.\n\nIn the fifth example you can choose only hits with numbers $[2, 4, 6]$ with the total damage $18 + 19 + 15 = 52$.\n\nIn the sixth example you can change either first hit or the second hit (it does not matter) with the total damage $10$."} +{"id": "01675", "text": "Consider a football tournament where n teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the i-th team has color x_{i} and the kit for away games of this team has color y_{i} (x_{i} \u2260 y_{i}).\n\nIn the tournament, each team plays exactly one home game and exactly one away game with each other team (n(n - 1) games in total). The team, that plays the home game, traditionally plays in its home kit. The team that plays an away game plays in its away kit. However, if two teams has the kits of the same color, they cannot be distinguished. In this case the away team plays in its home kit.\n\nCalculate how many games in the described tournament each team plays in its home kit and how many games it plays in its away kit.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 10^5) \u2014 the number of teams. Next n lines contain the description of the teams. The i-th line contains two space-separated numbers x_{i}, y_{i} (1 \u2264 x_{i}, y_{i} \u2264 10^5;\u00a0x_{i} \u2260 y_{i}) \u2014 the color numbers for the home and away kits of the i-th team.\n\n\n-----Output-----\n\nFor each team, print on a single line two space-separated integers \u2014 the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input.\n\n\n-----Examples-----\nInput\n2\n1 2\n2 1\n\nOutput\n2 0\n2 0\n\nInput\n3\n1 2\n2 1\n1 3\n\nOutput\n3 1\n4 0\n2 2"} +{"id": "01676", "text": "In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment t_{i} and needs to be processed for d_{i} units of time. All t_{i} are guaranteed to be distinct.\n\nWhen a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed. \n\nAs soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.\n\nFor each query find the moment when the server will finish to process it or print -1 if this query will be rejected.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and b (1 \u2264 n, b \u2264 200 000)\u00a0\u2014 the number of queries and the maximum possible size of the query queue.\n\nThen follow n lines with queries descriptions (in chronological order). Each description consists of two integers t_{i} and d_{i} (1 \u2264 t_{i}, d_{i} \u2264 10^9), where t_{i} is the moment of time when the i-th query appears and d_{i} is the time server needs to process it. It is guaranteed that t_{i} - 1 < t_{i} for all i > 1.\n\n\n-----Output-----\n\nPrint the sequence of n integers e_1, e_2, ..., e_{n}, where e_{i} is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if the corresponding query will be rejected.\n\n\n-----Examples-----\nInput\n5 1\n2 9\n4 8\n10 9\n15 2\n19 1\n\nOutput\n11 19 -1 21 22 \n\nInput\n4 1\n2 8\n4 8\n10 9\n15 2\n\nOutput\n10 18 27 -1 \n\n\n\n-----Note-----\n\nConsider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22."} +{"id": "01677", "text": "Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: a_1 = p, where p is some integer; a_{i} = a_{i} - 1 + ( - 1)^{i} + 1\u00b7q (i > 1), where q is some integer. \n\nRight now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression.\n\nSequence s_1, s_2, ..., s_{k} is a subsequence of sequence b_1, b_2, ..., b_{n}, if there is such increasing sequence of indexes i_1, i_2, ..., i_{k} (1 \u2264 i_1 < i_2 < ... < i_{k} \u2264 n), that b_{i}_{j} = s_{j}. In other words, sequence s can be obtained from b by crossing out some elements.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 4000). The next line contains n integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 10^6).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the length of the required longest subsequence.\n\n\n-----Examples-----\nInput\n2\n3 5\n\nOutput\n2\n\nInput\n4\n10 20 10 30\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first test the sequence actually is the suitable subsequence. \n\nIn the second test the following subsequence fits: 10, 20, 10."} +{"id": "01678", "text": "Petya has an array $a$ consisting of $n$ integers. He has learned partial sums recently, and now he can calculate the sum of elements on any segment of the array really fast. The segment is a non-empty sequence of elements standing one next to another in the array.\n\nNow he wonders what is the number of segments in his array with the sum less than $t$. Help Petya to calculate this number.\n\nMore formally, you are required to calculate the number of pairs $l, r$ ($l \\le r$) such that $a_l + a_{l+1} + \\dots + a_{r-1} + a_r < t$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $t$ ($1 \\le n \\le 200\\,000, |t| \\le 2\\cdot10^{14}$).\n\nThe second line contains a sequence of integers $a_1, a_2, \\dots, a_n$ ($|a_{i}| \\le 10^{9}$) \u2014 the description of Petya's array. Note that there might be negative, zero and positive elements.\n\n\n-----Output-----\n\nPrint the number of segments in Petya's array with the sum of elements less than $t$.\n\n\n-----Examples-----\nInput\n5 4\n5 -1 3 4 -1\n\nOutput\n5\n\nInput\n3 0\n-1 2 -3\n\nOutput\n4\n\nInput\n4 -1\n-2 1 -2 3\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example the following segments have sum less than $4$: $[2, 2]$, sum of elements is $-1$ $[2, 3]$, sum of elements is $2$ $[3, 3]$, sum of elements is $3$ $[4, 5]$, sum of elements is $3$ $[5, 5]$, sum of elements is $-1$"} +{"id": "01679", "text": "Polycarp has just invented a new binary protocol for data transmission. He is encoding positive integer decimal number to binary string using following algorithm:\n\n Each digit is represented with number of '1' characters equal to the value of that digit (for 0 it is zero ones). Digits are written one by one in order corresponding to number and separated by single '0' character. \n\nThough Polycarp learnt how to encode the numbers, he has no idea how to decode them back. Help him calculate the decoded number.\n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 89) \u2014 length of the string s.\n\nThe second line contains string s \u2014 sequence of '0' and '1' characters, number in its encoded format. It is guaranteed that the number corresponding to the string is positive and doesn't exceed 10^9. The string always starts with '1'.\n\n\n-----Output-----\n\nPrint the decoded number.\n\n\n-----Examples-----\nInput\n3\n111\n\nOutput\n3\n\nInput\n9\n110011101\n\nOutput\n2031"} +{"id": "01680", "text": "Vasya has the sequence consisting of n integers. Vasya consider the pair of integers x and y k-interesting, if their binary representation differs from each other exactly in k bits. For example, if k = 2, the pair of integers x = 5 and y = 3 is k-interesting, because their binary representation x=101 and y=011 differs exactly in two bits.\n\nVasya wants to know how many pairs of indexes (i, j) are in his sequence so that i < j and the pair of integers a_{i} and a_{j} is k-interesting. Your task is to help Vasya and determine this number.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (2 \u2264 n \u2264 10^5, 0 \u2264 k \u2264 14) \u2014 the number of integers in Vasya's sequence and the number of bits in which integers in k-interesting pair should differ.\n\nThe second line contains the sequence a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^4), which Vasya has.\n\n\n-----Output-----\n\nPrint the number of pairs (i, j) so that i < j and the pair of integers a_{i} and a_{j} is k-interesting.\n\n\n-----Examples-----\nInput\n4 1\n0 3 2 1\n\nOutput\n4\n\nInput\n6 0\n200 100 100 100 200 200\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first test there are 4 k-interesting pairs: (1, 3), (1, 4), (2, 3), (2, 4). \n\nIn the second test k = 0. Consequently, integers in any k-interesting pair should be equal to themselves. Thus, for the second test there are 6 k-interesting pairs: (1, 5), (1, 6), (2, 3), (2, 4), (3, 4), (5, 6)."} +{"id": "01681", "text": "Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.\n\nThe garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.\n\nVasya wants the garland to be as attractive as possible, so he wants to maximize the total area of \u200b\u200bm pieces of paper in the garland. Calculate what the maximum total area of \u200b\u200bthe pieces of paper in the garland Vasya can get.\n\n\n-----Input-----\n\nThe first line contains a non-empty sequence of n (1 \u2264 n \u2264 1000) small English letters (\"a\"...\"z\"). Each letter means that Vasya has a sheet of paper of the corresponding color.\n\nThe second line contains a non-empty sequence of m (1 \u2264 m \u2264 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.\n\n\n-----Output-----\n\nPrint an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.\n\n\n-----Examples-----\nInput\naaabbac\naabbccac\n\nOutput\n6\n\nInput\na\nz\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.\n\nIn the second test sample Vasya cannot make a garland at all \u2014 he doesn't have a sheet of color z."} +{"id": "01682", "text": "Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is a_{i}, and after a week of discounts its price will be b_{i}.\n\nNot all of sellers are honest, so now some products could be more expensive than after a week of discounts.\n\nIgor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.\n\n\n-----Input-----\n\nIn the first line there are two positive integer numbers n and k (1 \u2264 n \u2264 2\u00b710^5, 0 \u2264 k \u2264 n) \u2014 total number of items to buy and minimal number of items Igor wants to by right now.\n\nThe second line contains sequence of integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^4) \u2014 prices of items during discounts (i.e. right now).\n\nThe third line contains sequence of integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 10^4) \u2014 prices of items after discounts (i.e. after a week).\n\n\n-----Output-----\n\nPrint the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.\n\n\n-----Examples-----\nInput\n3 1\n5 4 6\n3 1 5\n\nOutput\n10\n\nInput\n5 3\n3 4 7 10 3\n4 5 5 12 5\n\nOutput\n25\n\n\n\n-----Note-----\n\nIn the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6 + 3 + 1 = 10.\n\nIn the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3 + 4 + 10 + 3 + 5 = 25."} +{"id": "01683", "text": "This problem differs from the previous one only in the absence of the constraint on the equal length of all numbers $a_1, a_2, \\dots, a_n$.\n\nA team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don't know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem.\n\nLet's denote a function that alternates digits of two numbers $f(a_1 a_2 \\dots a_{p - 1} a_p, b_1 b_2 \\dots b_{q - 1} b_q)$, where $a_1 \\dots a_p$ and $b_1 \\dots b_q$ are digits of two integers written in the decimal notation without leading zeros.\n\nIn other words, the function $f(x, y)$ alternately shuffles the digits of the numbers $x$ and $y$ by writing them from the lowest digits to the older ones, starting with the number $y$. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below.\n\nFor example: $$f(1111, 2222) = 12121212$$ $$f(7777, 888) = 7787878$$ $$f(33, 44444) = 4443434$$ $$f(555, 6) = 5556$$ $$f(111, 2222) = 2121212$$\n\nFormally, if $p \\ge q$ then $f(a_1 \\dots a_p, b_1 \\dots b_q) = a_1 a_2 \\dots a_{p - q + 1} b_1 a_{p - q + 2} b_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$; if $p < q$ then $f(a_1 \\dots a_p, b_1 \\dots b_q) = b_1 b_2 \\dots b_{q - p} a_1 b_{q - p + 1} a_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$. \n\nMishanya gives you an array consisting of $n$ integers $a_i$, your task is to help students to calculate $\\sum_{i = 1}^{n}\\sum_{j = 1}^{n} f(a_i, a_j)$ modulo $998\\,244\\,353$.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($1 \\le n \\le 100\\,000$) \u2014 the number of elements in the array. The second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) \u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint the answer modulo $998\\,244\\,353$.\n\n\n-----Examples-----\nInput\n3\n12 3 45\n\nOutput\n12330\nInput\n2\n123 456\n\nOutput\n1115598"} +{"id": "01684", "text": "Inaka has a disc, the circumference of which is $n$ units. The circumference is equally divided by $n$ points numbered clockwise from $1$ to $n$, such that points $i$ and $i + 1$ ($1 \\leq i < n$) are adjacent, and so are points $n$ and $1$.\n\nThere are $m$ straight segments on the disc, the endpoints of which are all among the aforementioned $n$ points.\n\nInaka wants to know if her image is rotationally symmetrical, i.e. if there is an integer $k$ ($1 \\leq k < n$), such that if all segments are rotated clockwise around the center of the circle by $k$ units, the new image will be the same as the original one.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers $n$ and $m$ ($2 \\leq n \\leq 100\\,000$, $1 \\leq m \\leq 200\\,000$)\u00a0\u2014 the number of points and the number of segments, respectively.\n\nThe $i$-th of the following $m$ lines contains two space-separated integers $a_i$ and $b_i$ ($1 \\leq a_i, b_i \\leq n$, $a_i \\neq b_i$) that describe a segment connecting points $a_i$ and $b_i$.\n\nIt is guaranteed that no segments coincide.\n\n\n-----Output-----\n\nOutput one line\u00a0\u2014 \"Yes\" if the image is rotationally symmetrical, and \"No\" otherwise (both excluding quotation marks).\n\nYou can output each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n12 6\n1 3\n3 7\n5 7\n7 11\n9 11\n11 3\n\nOutput\nYes\n\nInput\n9 6\n4 5\n5 6\n7 8\n8 9\n1 2\n2 3\n\nOutput\nYes\n\nInput\n10 3\n1 2\n3 2\n7 2\n\nOutput\nNo\n\nInput\n10 2\n1 6\n2 7\n\nOutput\nYes\n\n\n\n-----Note-----\n\nThe first two examples are illustrated below. Both images become the same as their respective original ones after a clockwise rotation of $120$ degrees around the center.\n\n [Image]"} +{"id": "01685", "text": "T is a complete binary tree consisting of n vertices. It means that exactly one vertex is a root, and each vertex is either a leaf (and doesn't have children) or an inner node (and has exactly two children). All leaves of a complete binary tree have the same depth (distance from the root). So n is a number such that n + 1 is a power of 2.\n\nIn the picture you can see a complete binary tree with n = 15. [Image] \n\nVertices are numbered from 1 to n in a special recursive way: we recursively assign numbers to all vertices from the left subtree (if current vertex is not a leaf), then assign a number to the current vertex, and then recursively assign numbers to all vertices from the right subtree (if it exists). In the picture vertices are numbered exactly using this algorithm. It is clear that for each size of a complete binary tree exists exactly one way to give numbers to all vertices. This way of numbering is called symmetric.\n\nYou have to write a program that for given n answers q queries to the tree.\n\nEach query consists of an integer number u_{i} (1 \u2264 u_{i} \u2264 n) and a string s_{i}, where u_{i} is the number of vertex, and s_{i} represents the path starting from this vertex. String s_{i} doesn't contain any characters other than 'L', 'R' and 'U', which mean traverse to the left child, to the right child and to the parent, respectively. Characters from s_{i} have to be processed from left to right, considering that u_{i} is the vertex where the path starts. If it's impossible to process a character (for example, to go to the left child of a leaf), then you have to skip it. The answer is the number of vertex where the path represented by s_{i} ends.\n\nFor example, if u_{i} = 4 and s_{i} = \u00abUURL\u00bb, then the answer is 10.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and q (1 \u2264 n \u2264 10^18, q \u2265 1). n is such that n + 1 is a power of 2.\n\nThe next 2q lines represent queries; each query consists of two consecutive lines. The first of these two lines contains u_{i} (1 \u2264 u_{i} \u2264 n), the second contains non-empty string s_{i}. s_{i} doesn't contain any characters other than 'L', 'R' and 'U'.\n\nIt is guaranteed that the sum of lengths of s_{i} (for each i such that 1 \u2264 i \u2264 q) doesn't exceed 10^5.\n\n\n-----Output-----\n\nPrint q numbers, i-th number must be the answer to the i-th query.\n\n\n-----Example-----\nInput\n15 2\n4\nUURL\n8\nLRLLLLLLLL\n\nOutput\n10\n5"} +{"id": "01686", "text": "The problem uses a simplified TCP/IP address model, please make sure you've read the statement attentively.\n\nPolycarpus has found a job, he is a system administrator. One day he came across n IP addresses. Each IP address is a 32 bit number, represented as a group of four 8-bit numbers (without leading zeroes), separated by dots. For example, the record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.01 do not. In this problem an arbitrary group of four 8-bit numbers is a correct IP address.\n\nHaving worked as an administrator for some time, Polycarpus learned that if you know the IP address, you can use the subnet mask to get the address of the network that has this IP addess.\n\nThe subnet mask is an IP address that has the following property: if we write this IP address as a 32 bit string, that it is representable as \"11...11000..000\". In other words, the subnet mask first has one or more one bits, and then one or more zero bits (overall there are 32 bits). For example, the IP address 2.0.0.0 is not a correct subnet mask as its 32-bit record looks as 00000010000000000000000000000000.\n\nTo get the network address of the IP address, you need to perform the operation of the bitwise \"and\" of the IP address and the subnet mask. For example, if the subnet mask is 255.192.0.0, and the IP address is 192.168.1.2, then the network address equals 192.128.0.0. In the bitwise \"and\" the result has a bit that equals 1 if and only if both operands have corresponding bits equal to one.\n\nNow Polycarpus wants to find all networks to which his IP addresses belong. Unfortunately, Polycarpus lost subnet mask. Fortunately, Polycarpus remembers that his IP addresses belonged to exactly k distinct networks. Help Polycarpus find the subnet mask, such that his IP addresses will belong to exactly k distinct networks. If there are several such subnet masks, find the one whose bit record contains the least number of ones. If such subnet mask do not exist, say so.\n\n\n-----Input-----\n\nThe first line contains two integers, n and k (1 \u2264 k \u2264 n \u2264 10^5) \u2014 the number of IP addresses and networks. The next n lines contain the IP addresses. It is guaranteed that all IP addresses are distinct.\n\n\n-----Output-----\n\nIn a single line print the IP address of the subnet mask in the format that is described in the statement, if the required subnet mask exists. Otherwise, print -1.\n\n\n-----Examples-----\nInput\n5 3\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3\n\nOutput\n255.255.254.0\nInput\n5 2\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3\n\nOutput\n255.255.0.0\nInput\n2 1\n255.0.0.1\n0.0.0.2\n\nOutput\n-1"} +{"id": "01687", "text": "Ksusha is a beginner coder. Today she starts studying arrays. She has array a_1, a_2, ..., a_{n}, consisting of n positive integers.\n\nHer university teacher gave her a task. Find such number in the array, that all array elements are divisible by it. Help her and find the number!\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5), showing how many numbers the array has. The next line contains integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 the array elements.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number from the array, such that all array elements are divisible by it. If such number doesn't exist, print -1.\n\nIf there are multiple answers, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n3\n2 2 4\n\nOutput\n2\n\nInput\n5\n2 1 3 1 6\n\nOutput\n1\n\nInput\n3\n2 3 5\n\nOutput\n-1"} +{"id": "01688", "text": "Your favorite music streaming platform has formed a perfectly balanced playlist exclusively for you. The playlist consists of $n$ tracks numbered from $1$ to $n$. The playlist is automatic and cyclic: whenever track $i$ finishes playing, track $i+1$ starts playing automatically; after track $n$ goes track $1$.\n\nFor each track $i$, you have estimated its coolness $a_i$. The higher $a_i$ is, the cooler track $i$ is.\n\nEvery morning, you choose a track. The playlist then starts playing from this track in its usual cyclic fashion. At any moment, you remember the maximum coolness $x$ of already played tracks. Once you hear that a track with coolness strictly less than $\\frac{x}{2}$ (no rounding) starts playing, you turn off the music immediately to keep yourself in a good mood.\n\nFor each track $i$, find out how many tracks you will listen to before turning off the music if you start your morning with track $i$, or determine that you will never turn the music off. Note that if you listen to the same track several times, every time must be counted.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 10^5$), denoting the number of tracks in the playlist.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$), denoting coolnesses of the tracks.\n\n\n-----Output-----\n\nOutput $n$ integers $c_1, c_2, \\ldots, c_n$, where $c_i$ is either the number of tracks you will listen to if you start listening from track $i$ or $-1$ if you will be listening to music indefinitely.\n\n\n-----Examples-----\nInput\n4\n11 5 2 7\n\nOutput\n1 1 3 2\n\nInput\n4\n3 2 5 3\n\nOutput\n5 4 3 6\n\nInput\n3\n4 3 6\n\nOutput\n-1 -1 -1\n\n\n\n-----Note-----\n\nIn the first example, here is what will happen if you start with... track $1$: listen to track $1$, stop as $a_2 < \\frac{a_1}{2}$. track $2$: listen to track $2$, stop as $a_3 < \\frac{a_2}{2}$. track $3$: listen to track $3$, listen to track $4$, listen to track $1$, stop as $a_2 < \\frac{\\max(a_3, a_4, a_1)}{2}$. track $4$: listen to track $4$, listen to track $1$, stop as $a_2 < \\frac{\\max(a_4, a_1)}{2}$. \n\nIn the second example, if you start with track $4$, you will listen to track $4$, listen to track $1$, listen to track $2$, listen to track $3$, listen to track $4$ again, listen to track $1$ again, and stop as $a_2 < \\frac{max(a_4, a_1, a_2, a_3, a_4, a_1)}{2}$. Note that both track $1$ and track $4$ are counted twice towards the result."} +{"id": "01689", "text": "ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has n rows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.\n\nZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of rows of seats in the bus.\n\nThen, n lines follow. Each line contains exactly 5 characters, the first two of them denote the first pair of seats in the row, the third character denotes the walkway (it always equals '|') and the last two of them denote the second pair of seats in the row. \n\nEach character, except the walkway, equals to 'O' or to 'X'. 'O' denotes an empty seat, 'X' denotes an occupied seat. See the sample cases for more details. \n\n\n-----Output-----\n\nIf it is possible for Chris and ZS to sit at neighbouring empty seats, print \"YES\" (without quotes) in the first line. In the next n lines print the bus configuration, where the characters in the pair of seats for Chris and ZS is changed with characters '+'. Thus the configuration should differ from the input one by exactly two charaters (they should be equal to 'O' in the input and to '+' in the output).\n\nIf there is no pair of seats for Chris and ZS, print \"NO\" (without quotes) in a single line.\n\nIf there are multiple solutions, you may print any of them.\n\n\n-----Examples-----\nInput\n6\nOO|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n\nOutput\nYES\n++|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX\n\nInput\n4\nXO|OX\nXO|XX\nOX|OX\nXX|OX\n\nOutput\nNO\n\nInput\n5\nXX|XX\nXX|XX\nXO|OX\nXO|OO\nOX|XO\n\nOutput\nYES\nXX|XX\nXX|XX\nXO|OX\nXO|++\nOX|XO\n\n\n\n-----Note-----\n\nNote that the following is an incorrect configuration for the first sample case because the seats must be in the same pair.\n\nO+|+X\n\nXO|XX\n\nOX|OO\n\nXX|OX\n\nOO|OO\n\nOO|XX"} +{"id": "01690", "text": "You went to the store, selling $n$ types of chocolates. There are $a_i$ chocolates of type $i$ in stock.\n\nYou have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $x_i$ chocolates of type $i$ (clearly, $0 \\le x_i \\le a_i$), then for all $1 \\le j < i$ at least one of the following must hold: $x_j = 0$ (you bought zero chocolates of type $j$) $x_j < x_i$ (you bought less chocolates of type $j$ than of type $i$) \n\nFor example, the array $x = [0, 0, 1, 2, 10]$ satisfies the requirement above (assuming that all $a_i \\ge x_i$), while arrays $x = [0, 1, 0]$, $x = [5, 5]$ and $x = [3, 2]$ don't.\n\nCalculate the maximum number of chocolates you can buy.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$), denoting the number of types of chocolate.\n\nThe next line contains $n$ integers $a_i$ ($1 \\le a_i \\le 10^9$), denoting the number of chocolates of each type.\n\n\n-----Output-----\n\nPrint the maximum number of chocolates you can buy.\n\n\n-----Examples-----\nInput\n5\n1 2 1 3 6\n\nOutput\n10\nInput\n5\n3 2 5 4 10\n\nOutput\n20\nInput\n4\n1 1 1 1\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first example, it is optimal to buy: $0 + 0 + 1 + 3 + 6$ chocolates.\n\nIn the second example, it is optimal to buy: $1 + 2 + 3 + 4 + 10$ chocolates.\n\nIn the third example, it is optimal to buy: $0 + 0 + 0 + 1$ chocolates."} +{"id": "01691", "text": "PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. \n\nHe chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: \n\nAssume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex.\n\nYour task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides.\n\n\n-----Input-----\n\nThere are only two numbers in the input: n and k (5 \u2264 n \u2264 10^6, 2 \u2264 k \u2264 n - 2, gcd(n, k) = 1).\n\n\n-----Output-----\n\nYou should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines.\n\n\n-----Examples-----\nInput\n5 2\n\nOutput\n2 3 5 8 11 \nInput\n10 3\n\nOutput\n2 3 4 6 9 12 16 21 26 31 \n\n\n-----Note-----\n\nThe greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder.\n\nFor the first sample testcase, you should output \"2 3 5 8 11\". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]"} +{"id": "01692", "text": "Max wants to buy a new skateboard. He has calculated the amount of money that is needed to buy a new skateboard. He left a calculator on the floor and went to ask some money from his parents. Meanwhile his little brother Yusuf came and started to press the keys randomly. Unfortunately Max has forgotten the number which he had calculated. The only thing he knows is that the number is divisible by 4.\n\nYou are given a string s consisting of digits (the number on the display of the calculator after Yusuf randomly pressed the keys). Your task is to find the number of substrings which are divisible by 4. A substring can start with a zero.\n\nA substring of a string is a nonempty sequence of consecutive characters.\n\nFor example if string s is 124 then we have four substrings that are divisible by 4: 12, 4, 24 and 124. For the string 04 the answer is three: 0, 4, 04.\n\nAs input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.\n\n\n-----Input-----\n\nThe only line contains string s (1 \u2264 |s| \u2264 3\u00b710^5). The string s contains only digits from 0 to 9.\n\n\n-----Output-----\n\nPrint integer a \u2014 the number of substrings of the string s that are divisible by 4.\n\nNote that the answer can be huge, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.\n\n\n-----Examples-----\nInput\n124\n\nOutput\n4\n\nInput\n04\n\nOutput\n3\n\nInput\n5810438174\n\nOutput\n9"} +{"id": "01693", "text": "This is a harder version of the problem. In this version $n \\le 500\\,000$\n\nThe 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.\n\nArchitects 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.\n\nFormally, 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$.\n\nThe 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.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 500\\,000$)\u00a0\u2014 the number of plots.\n\nThe second line contains the integers $m_1, m_2, \\ldots, m_n$ ($1 \\leq m_i \\leq 10^9$)\u00a0\u2014 the limit on the number of floors for every possible number of floors for a skyscraper on each plot.\n\n\n-----Output-----\n\nPrint $n$ integers $a_i$\u00a0\u2014 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.\n\nIf there are multiple answers possible, print any of them.\n\n\n-----Examples-----\nInput\n5\n1 2 3 2 1\n\nOutput\n1 2 3 2 1 \n\nInput\n3\n10 6 8\n\nOutput\n10 6 6 \n\n\n\n-----Note-----\n\nIn the first example, you can build all skyscrapers with the highest possible height.\n\nIn 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."} +{"id": "01694", "text": "Xenia the vigorous detective faced n (n \u2265 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. \n\nSpy 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.\n\nBut 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 \u2264 l_{i} \u2264 r_{i} \u2264 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.\n\nYou'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).\n\n\n-----Input-----\n\nThe first line contains four integers n, m, s and f (1 \u2264 n, m \u2264 10^5;\u00a01 \u2264 s, f \u2264 n;\u00a0s \u2260 f;\u00a0n \u2265 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 \u2264 t_{i} \u2264 10^9, 1 \u2264 l_{i} \u2264 r_{i} \u2264 n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}.\n\n\n-----Output-----\n\nPrint 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\".\n\nAs 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.\n\nIf there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.\n\n\n-----Examples-----\nInput\n3 5 1 3\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n10 1 3\n\nOutput\nXXRR"} +{"id": "01695", "text": "A class of students wrote a multiple-choice test.\n\nThere are $n$ students in the class. The test had $m$ questions, each of them had $5$ possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question $i$ worth $a_i$ points. Incorrect answers are graded with zero points.\n\nThe students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class. \n\n\n-----Input-----\n\nThe first line contains integers $n$ and $m$ ($1 \\le n, m \\le 1000$)\u00a0\u2014 the number of students in the class and the number of questions in the test.\n\nEach of the next $n$ lines contains string $s_i$ ($|s_i| = m$), describing an answer of the $i$-th student. The $j$-th character represents the student answer (A, B, C, D or E) on the $j$-th question.\n\nThe last line contains $m$ integers $a_1, a_2, \\ldots, a_m$ ($1 \\le a_i \\le 1000$)\u00a0\u2014 the number of points for the correct answer for every question.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum possible total score of the class.\n\n\n-----Examples-----\nInput\n2 4\nABCD\nABCE\n1 2 3 4\n\nOutput\n16\nInput\n3 3\nABC\nBCD\nCDE\n5 4 12\n\nOutput\n21\n\n\n-----Note-----\n\nIn the first example, one of the most optimal test answers is \"ABCD\", this way the total number of points will be $16$.\n\nIn the second example, one of the most optimal test answers is \"CCC\", this way each question will be answered by exactly one student and the total number of points is $5 + 4 + 12 = 21$."} +{"id": "01696", "text": "The capital of Berland looks like a rectangle of size n \u00d7 m of the square blocks of same size.\n\nFire!\n\nIt is known that k + 1 blocks got caught on fire (k + 1 \u2264 n\u00b7m). 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.\n\nThe 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.\n\nBerland 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.\n\nHelp Berland Fire Department to estimate the minimal time it takes the fire to lighten up the whole city.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and k (1 \u2264 n, m \u2264 10^9, 1 \u2264 k \u2264 500).\n\nEach of the next k lines contain two integers x_{i} and y_{i} (1 \u2264 x_{i} \u2264 n, 1 \u2264 y_{i} \u2264 m) \u2014 coordinates of the i-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct.\n\n\n-----Output-----\n\nPrint the minimal time it takes the fire to lighten up the whole city (in minutes).\n\n\n-----Examples-----\nInput\n7 7 3\n1 2\n2 1\n5 5\n\nOutput\n3\n\nInput\n10 5 1\n3 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example the last block can have coordinates (4, 4).\n\nIn the second example the last block can have coordinates (8, 3)."} +{"id": "01697", "text": "Fox Ciel is playing a mobile puzzle game called \"Two Dots\". The basic levels are played on a board of size n \u00d7 m cells, like this:\n\n[Image]\n\nEach cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.\n\nThe key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d_1, d_2, ..., d_{k} a cycle if and only if it meets the following condition:\n\n These k dots are different: if i \u2260 j then d_{i} is different from d_{j}. k is at least 4. All dots belong to the same color. For all 1 \u2264 i \u2264 k - 1: d_{i} and d_{i} + 1 are adjacent. Also, d_{k} and d_1 should also be adjacent. Cells x and y are called adjacent if they share an edge. \n\nDetermine if there exists a cycle on the field.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (2 \u2264 n, m \u2264 50): the number of rows and columns of the board.\n\nThen n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.\n\n\n-----Output-----\n\nOutput \"Yes\" if there exists a cycle, and \"No\" otherwise.\n\n\n-----Examples-----\nInput\n3 4\nAAAA\nABCA\nAAAA\n\nOutput\nYes\n\nInput\n3 4\nAAAA\nABCA\nAADA\n\nOutput\nNo\n\nInput\n4 4\nYYYR\nBYBY\nBBBY\nBBBY\n\nOutput\nYes\n\nInput\n7 6\nAAAAAB\nABBBAB\nABAAAB\nABABBB\nABAAAB\nABBBAB\nAAAAAB\n\nOutput\nYes\n\nInput\n2 13\nABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn first sample test all 'A' form a cycle.\n\nIn second sample there is no such cycle.\n\nThe third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red)."} +{"id": "01698", "text": "One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task.\n\nLet's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following way. We have n people standing on the first floor, the i-th person wants to go to the f_{i}-th floor. Unfortunately, there is only one elevator and its capacity equal to k (that is at most k people can use it simultaneously). Initially the elevator is located on the first floor. The elevator needs |a - b| seconds to move from the a-th floor to the b-th floor (we don't count the time the people need to get on and off the elevator).\n\nWhat is the minimal number of seconds that is needed to transport all the people to the corresponding floors and then return the elevator to the first floor?\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n, k \u2264 2000) \u2014 the number of people and the maximal capacity of the elevator.\n\nThe next line contains n integers: f_1, f_2, ..., f_{n} (2 \u2264 f_{i} \u2264 2000), where f_{i} denotes the target floor of the i-th person.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the minimal time needed to achieve the goal.\n\n\n-----Examples-----\nInput\n3 2\n2 3 4\n\nOutput\n8\n\nInput\n4 2\n50 100 50 100\n\nOutput\n296\n\nInput\n10 3\n2 2 2 2 2 2 2 2 2 2\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn first sample, an optimal solution is: The elevator takes up person #1 and person #2. It goes to the 2nd floor. Both people go out of the elevator. The elevator goes back to the 1st floor. Then the elevator takes up person #3. And it goes to the 2nd floor. It picks up person #2. Then it goes to the 3rd floor. Person #2 goes out. Then it goes to the 4th floor, where person #3 goes out. The elevator goes back to the 1st floor."} +{"id": "01699", "text": "While resting on the ship after the \"Russian Code Cup\" a boy named Misha invented an interesting game. He promised to give his quadrocopter to whoever will be the first one to make a rectangular table of size n \u00d7 m, consisting of positive integers such that the sum of the squares of numbers for each row and each column was also a square.\n\nSince checking the correctness of the table manually is difficult, Misha asks you to make each number in the table to not exceed 10^8.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 100) \u00a0\u2014 the size of the table. \n\n\n-----Output-----\n\nPrint the table that meets the condition: n lines containing m integers, separated by spaces. If there are multiple possible answers, you are allowed to print anyone. It is guaranteed that there exists at least one correct answer.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n1\nInput\n1 2\n\nOutput\n3 4"} +{"id": "01700", "text": "A string is called bracket sequence if it does not contain any characters other than \"(\" and \")\". A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters \"+\" and \"1\" into this sequence. For example, \"\", \"(())\" and \"()()\" are RBS and \")(\" and \"(()\" are not.\n\nWe can see that each opening bracket in RBS is paired with some closing bracket, and, using this fact, we can define nesting depth of the RBS as maximum number of bracket pairs, such that the $2$-nd pair lies inside the $1$-st one, the $3$-rd one \u2014 inside the $2$-nd one and so on. For example, nesting depth of \"\" is $0$, \"()()()\" is $1$ and \"()((())())\" is $3$.\n\nNow, you are given RBS $s$ of even length $n$. You should color each bracket of $s$ into one of two colors: red or blue. Bracket sequence $r$, consisting only of red brackets, should be RBS, and bracket sequence, consisting only of blue brackets $b$, should be RBS. Any of them can be empty. You are not allowed to reorder characters in $s$, $r$ or $b$. No brackets can be left uncolored.\n\nAmong all possible variants you should choose one that minimizes maximum of $r$'s and $b$'s nesting depth. If there are multiple solutions you can print any of them.\n\n\n-----Input-----\n\nThe first line contains an even integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of RBS $s$.\n\nThe second line contains regular bracket sequence $s$ ($|s| = n$, $s_i \\in \\{$\"(\", \")\"$\\}$).\n\n\n-----Output-----\n\nPrint single string $t$ of length $n$ consisting of \"0\"-s and \"1\"-s. If $t_i$ is equal to 0 then character $s_i$ belongs to RBS $r$, otherwise $s_i$ belongs to $b$.\n\n\n-----Examples-----\nInput\n2\n()\n\nOutput\n11\n\nInput\n4\n(())\n\nOutput\n0101\n\nInput\n10\n((()())())\n\nOutput\n0110001111\n\n\n-----Note-----\n\nIn the first example one of optimal solutions is $s = $ \"$\\color{blue}{()}$\". $r$ is empty and $b = $ \"$()$\". The answer is $\\max(0, 1) = 1$.\n\nIn the second example it's optimal to make $s = $ \"$\\color{red}{(}\\color{blue}{(}\\color{red}{)}\\color{blue}{)}$\". $r = b = $ \"$()$\" and the answer is $1$.\n\nIn the third example we can make $s = $ \"$\\color{red}{(}\\color{blue}{((}\\color{red}{)()}\\color{blue}{)())}$\". $r = $ \"$()()$\" and $b = $ \"$(()())$\" and the answer is $2$."} +{"id": "01701", "text": "As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. The school has n servers. Each server has a name and an ip (names aren't necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we'll assume that an nginx command is of form \"command ip;\" where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers.\n\n [Image] \n\nEach ip is of form \"a.b.c.d\" where a, b, c and d are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has m commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is \"command ip;\" Dustin has to replace it with \"command ip; #name\" where name is the name of the server with ip equal to ip.\n\nDustin doesn't know anything about nginx, so he panicked again and his friends asked you to do his task for him.\n\n\n-----Input-----\n\nThe first line of input contains two integers n and m (1 \u2264 n, m \u2264 1000).\n\nThe next n lines contain the names and ips of the servers. Each line contains a string name, name of the server and a string ip, ip of the server, separated by space (1 \u2264 |name| \u2264 10, name only consists of English lowercase letters). It is guaranteed that all ip are distinct.\n\nThe next m lines contain the commands in the configuration file. Each line is of form \"command ip;\" (1 \u2264 |command| \u2264 10, command only consists of English lowercase letters). It is guaranteed that ip belongs to one of the n school servers.\n\n\n-----Output-----\n\nPrint m lines, the commands in the configuration file after Dustin did his task.\n\n\n-----Examples-----\nInput\n2 2\nmain 192.168.0.2\nreplica 192.168.0.1\nblock 192.168.0.1;\nproxy 192.168.0.2;\n\nOutput\nblock 192.168.0.1; #replica\nproxy 192.168.0.2; #main\n\nInput\n3 5\ngoogle 8.8.8.8\ncodeforces 212.193.33.27\nserver 138.197.64.57\nredirect 138.197.64.57;\nblock 8.8.8.8;\ncf 212.193.33.27;\nunblock 8.8.8.8;\ncheck 138.197.64.57;\n\nOutput\nredirect 138.197.64.57; #server\nblock 8.8.8.8; #google\ncf 212.193.33.27; #codeforces\nunblock 8.8.8.8; #google\ncheck 138.197.64.57; #server"} +{"id": "01702", "text": "Vasya and Petya take part in a Codeforces round. The round lasts for two hours and contains five problems.\n\nFor this round the dynamic problem scoring is used. If you were lucky not to participate in any Codeforces round with dynamic problem scoring, here is what it means. The maximum point value of the problem depends on the ratio of the number of participants who solved the problem to the total number of round participants. Everyone who made at least one submission is considered to be participating in the round.\n\n$\\left. \\begin{array}{|l|r|} \\hline \\text{Solvers fraction} & {\\text{Maximum point value}} \\\\ \\hline(1 / 2,1 ] & {500} \\\\ \\hline(1 / 4,1 / 2 ] & {1000} \\\\ \\hline(1 / 8,1 / 4 ] & {1500} \\\\ \\hline(1 / 16,1 / 8 ] & {2000} \\\\ \\hline(1 / 32,1 / 16 ] & {2500} \\\\ \\hline [ 0,1 / 32 ] & {3000} \\\\ \\hline \\end{array} \\right.$\n\nPay attention to the range bounds. For example, if 40 people are taking part in the round, and 10 of them solve a particular problem, then the solvers fraction is equal to 1 / 4, and the problem's maximum point value is equal to 1500.\n\nIf the problem's maximum point value is equal to x, then for each whole minute passed from the beginning of the contest to the moment of the participant's correct submission, the participant loses x / 250 points. For example, if the problem's maximum point value is 2000, and the participant submits a correct solution to it 40 minutes into the round, this participant will be awarded with 2000\u00b7(1 - 40 / 250) = 1680 points for this problem.\n\nThere are n participants in the round, including Vasya and Petya. For each participant and each problem, the number of minutes which passed between the beginning of the contest and the submission of this participant to this problem is known. It's also possible that this participant made no submissions to this problem.\n\nWith two seconds until the end of the round, all participants' submissions have passed pretests, and not a single hack attempt has been made. Vasya believes that no more submissions or hack attempts will be made in the remaining two seconds, and every submission will pass the system testing.\n\nUnfortunately, Vasya is a cheater. He has registered 10^9 + 7 new accounts for the round. Now Vasya can submit any of his solutions from these new accounts in order to change the maximum point values of the problems. Vasya can also submit any wrong solutions to any problems. Note that Vasya can not submit correct solutions to the problems he hasn't solved.\n\nVasya seeks to score strictly more points than Petya in the current round. Vasya has already prepared the scripts which allow to obfuscate his solutions and submit them into the system from any of the new accounts in just fractions of seconds. However, Vasya doesn't want to make his cheating too obvious, so he wants to achieve his goal while making submissions from the smallest possible number of new accounts.\n\nFind the smallest number of new accounts Vasya needs in order to beat Petya (provided that Vasya's assumptions are correct), or report that Vasya can't achieve his goal.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 120)\u00a0\u2014 the number of round participants, including Vasya and Petya.\n\nEach of the next n lines contains five integers a_{i}, 1, a_{i}, 2..., a_{i}, 5 ( - 1 \u2264 a_{i}, j \u2264 119)\u00a0\u2014 the number of minutes passed between the beginning of the round and the submission of problem j by participant i, or -1 if participant i hasn't solved problem j.\n\nIt is guaranteed that each participant has made at least one successful submission.\n\nVasya is listed as participant number 1, Petya is listed as participant number 2, all the other participants are listed in no particular order.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the number of new accounts Vasya needs to beat Petya, or -1 if Vasya can't achieve his goal.\n\n\n-----Examples-----\nInput\n2\n5 15 40 70 115\n50 45 40 30 15\n\nOutput\n2\n\nInput\n3\n55 80 10 -1 -1\n15 -1 79 60 -1\n42 -1 13 -1 -1\n\nOutput\n3\n\nInput\n5\n119 119 119 119 119\n0 0 0 0 -1\n20 65 12 73 77\n78 112 22 23 11\n1 78 60 111 62\n\nOutput\n27\n\nInput\n4\n-1 20 40 77 119\n30 10 73 50 107\n21 29 -1 64 98\n117 65 -1 -1 -1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example, Vasya's optimal strategy is to submit the solutions to the last three problems from two new accounts. In this case the first two problems will have the maximum point value of 1000, while the last three problems will have the maximum point value of 500. Vasya's score will be equal to 980 + 940 + 420 + 360 + 270 = 2970 points, while Petya will score just 800 + 820 + 420 + 440 + 470 = 2950 points.\n\nIn the second example, Vasya has to make a single unsuccessful submission to any problem from two new accounts, and a single successful submission to the first problem from the third new account. In this case, the maximum point values of the problems will be equal to 500, 1500, 1000, 1500, 3000. Vasya will score 2370 points, while Petya will score just 2294 points.\n\nIn the third example, Vasya can achieve his goal by submitting the solutions to the first four problems from 27 new accounts. The maximum point values of the problems will be equal to 500, 500, 500, 500, 2000. Thanks to the high cost of the fifth problem, Vasya will manage to beat Petya who solved the first four problems very quickly, but couldn't solve the fifth one."} +{"id": "01703", "text": "A bracket sequence is a string containing only characters \"(\" and \")\".\n\nA regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters \"1\" and \"+\" between the original characters of the sequence. For example, bracket sequences \"()()\", \"(())\" are regular (the resulting expressions are: \"(1)+(1)\", \"((1+1)+1)\"), and \")(\" and \"(\" are not.\n\nYou are given $n$ bracket sequences $s_1, s_2, \\dots , s_n$. Calculate the number of pairs $i, j \\, (1 \\le i, j \\le n)$ such that the bracket sequence $s_i + s_j$ is a regular bracket sequence. Operation $+$ means concatenation i.e. \"()(\" + \")()\" = \"()()()\".\n\nIf $s_i + s_j$ and $s_j + s_i$ are regular bracket sequences and $i \\ne j$, then both pairs $(i, j)$ and $(j, i)$ must be counted in the answer. Also, if $s_i + s_i$ is a regular bracket sequence, the pair $(i, i)$ must be counted in the answer.\n\n\n-----Input-----\n\nThe first line contains one integer $n \\, (1 \\le n \\le 3 \\cdot 10^5)$ \u2014 the number of bracket sequences. The following $n$ lines contain bracket sequences \u2014 non-empty strings consisting only of characters \"(\" and \")\". The sum of lengths of all bracket sequences does not exceed $3 \\cdot 10^5$.\n\n\n-----Output-----\n\nIn the single line print a single integer \u2014 the number of pairs $i, j \\, (1 \\le i, j \\le n)$ such that the bracket sequence $s_i + s_j$ is a regular bracket sequence.\n\n\n-----Examples-----\nInput\n3\n)\n()\n(\n\nOutput\n2\n\nInput\n2\n()\n()\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, suitable pairs are $(3, 1)$ and $(2, 2)$.\n\nIn the second example, any pair is suitable, namely $(1, 1), (1, 2), (2, 1), (2, 2)$."} +{"id": "01704", "text": "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. \n\nThe 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: \n\n [Image] \n\nAfter 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)? \n\nIt is allowed that the number includes leading zeros.\n\n\n-----Input-----\n\nThe first line contains integer $n$ $(1 \\leq n \\leq 2000)$ \u00a0\u2014 the number of digits on scoreboard and $k$ $(0 \\leq k \\leq 2000)$ \u00a0\u2014 the number of segments that stopped working.\n\nThe next $n$ lines contain one binary string of length $7$, the $i$-th of which encodes the $i$-th digit of the scoreboard.\n\nEach 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.\n\n [Image] \n\nThus, the sequences \"1110111\", \"0010010\", \"1011101\", \"1011011\", \"0111010\", \"1101011\", \"1101111\", \"1010010\", \"1111111\", \"1111011\" encode in sequence all digits from $0$ to $9$ inclusive.\n\n\n-----Output-----\n\nOutput a single number consisting of $n$ digits \u00a0\u2014 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.\n\n\n-----Examples-----\nInput\n1 7\n0000000\n\nOutput\n8\nInput\n2 5\n0010010\n0010010\n\nOutput\n97\nInput\n3 5\n0100001\n1001001\n1010011\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first test, we are obliged to include all $7$ sticks and get one $8$ digit on the scoreboard.\n\nIn 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 \u00a0\u2014 $97$.\n\nIn the third test, it is impossible to turn on exactly $5$ sticks so that a sequence of numbers appears on the scoreboard."} +{"id": "01705", "text": "Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.\n\nThere are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index $k$ such that Mr. Black can exit the house after opening the first $k$ doors.\n\nWe have to note that Mr. Black opened each door at most once, and in the end all doors became open.\n\n\n-----Input-----\n\nThe first line contains integer $n$ ($2 \\le n \\le 200\\,000$)\u00a0\u2014 the number of doors.\n\nThe next line contains $n$ integers: the sequence in which Mr. Black opened the doors. The $i$-th of these integers is equal to $0$ in case the $i$-th opened door is located in the left exit, and it is equal to $1$ in case it is in the right exit.\n\nIt is guaranteed that there is at least one door located in the left exit and there is at least one door located in the right exit.\n\n\n-----Output-----\n\nPrint the smallest integer $k$ such that after Mr. Black opened the first $k$ doors, he was able to exit the house.\n\n\n-----Examples-----\nInput\n5\n0 0 1 0 0\n\nOutput\n3\n\nInput\n4\n1 0 0 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment.\n\nWhen he opened the third door, all doors from the right exit became open, so Mr. Black was able to exit the house.\n\nIn the second example when the first two doors were opened, there was open closed door in each of the exit.\n\nWith three doors opened Mr. Black was able to use the left exit."} +{"id": "01706", "text": "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. \n\nThe 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$.\n\nThe 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$.\n\nNote that characters in the string in this problem are indexed from $1$.\n\nFor 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.\n\nYour 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$\n\nIt 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.\n\n\n-----Input-----\n\nThe only line contains the string $S$ ($3 \\le |s| \\le 10^5$) of lowercase letters from the English alphabet.\n\n\n-----Output-----\n\nThe first line should contain $k$ ($0\\le k \\le 30$) \u00a0\u2014 the number of operations performed.\n\nEach 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.\n\nThe length of the resulting palindrome must not exceed $10^6$.\n\n\n-----Examples-----\nInput\nabac\n\nOutput\n2\nR 2\nR 5\n\nInput\nacccc\n\nOutput\n2\nL 4\nL 2\n\nInput\nhannah\n\nOutput\n0\n\n\n-----Note-----\n\nFor the first example the following operations are performed:\n\nabac $\\to$ abacab $\\to$ abacaba\n\nThe second sample performs the following operations: acccc $\\to$ cccacccc $\\to$ ccccacccc\n\nThe third example is already a palindrome so no operations are required."} +{"id": "01707", "text": "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.\n\nHere $|z|$ denotes the absolute value of $z$.\n\nNow, 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.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u00a0\u2014 the number of choices.\n\nThe second line contains $n$ pairwise distinct integers $a_1, a_2, \\dots, a_n$ ($-10^9 \\le a_i \\le 10^9$)\u00a0\u2014 the choices Jose is considering.\n\n\n-----Output-----\n\nPrint a single integer number\u00a0\u2014 the number of unordered pairs $\\{x, y\\}$ formed by different numbers from Jose's choices that could make the legend true.\n\n\n-----Examples-----\nInput\n3\n2 5 -3\n\nOutput\n2\n\nInput\n2\n3 6\n\nOutput\n1\n\n\n\n-----Note-----\n\nConsider 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$:\n\n [Image] \n\nThe 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]$:\n\n [Image] \n\nAs 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.\n\nIn the second sample, the only pair is $\\{3, 6\\}$, and the situation looks as follows:\n\n [Image] \n\nNote that even though Arrayland and Vectorland share $3$ as endpoint, we still consider Arrayland to be completely inside of Vectorland."} +{"id": "01708", "text": "Lunar New Year is approaching, and Bob is planning to go for a famous restaurant \u2014 \"Alice's\".\n\nThe restaurant \"Alice's\" serves $n$ kinds of food. The cost for the $i$-th kind is always $c_i$. Initially, the restaurant has enough ingredients for serving exactly $a_i$ dishes of the $i$-th kind. In the New Year's Eve, $m$ customers will visit Alice's one after another and the $j$-th customer will order $d_j$ dishes of the $t_j$-th kind of food. The $(i + 1)$-st customer will only come after the $i$-th customer is completely served.\n\nSuppose there are $r_i$ dishes of the $i$-th kind remaining (initially $r_i = a_i$). When a customer orders $1$ dish of the $i$-th kind, the following principles will be processed. If $r_i > 0$, the customer will be served exactly $1$ dish of the $i$-th kind. The cost for the dish is $c_i$. Meanwhile, $r_i$ will be reduced by $1$. Otherwise, the customer will be served $1$ dish of the cheapest available kind of food if there are any. If there are multiple cheapest kinds of food, the one with the smallest index among the cheapest will be served. The cost will be the cost for the dish served and the remain for the corresponding dish will be reduced by $1$. If there are no more dishes at all, the customer will leave angrily. Therefore, no matter how many dishes are served previously, the cost for the customer is $0$.\n\nIf the customer doesn't leave after the $d_j$ dishes are served, the cost for the customer will be the sum of the cost for these $d_j$ dishes.\n\nPlease determine the total cost for each of the $m$ customers.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\leq n, m \\leq 10^5$), representing the number of different kinds of food and the number of customers, respectively.\n\nThe second line contains $n$ positive integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^7$), where $a_i$ denotes the initial remain of the $i$-th kind of dishes.\n\nThe third line contains $n$ positive integers $c_1, c_2, \\ldots, c_n$ ($1 \\leq c_i \\leq 10^6$), where $c_i$ denotes the cost of one dish of the $i$-th kind.\n\nThe following $m$ lines describe the orders of the $m$ customers respectively. The $j$-th line contains two positive integers $t_j$ and $d_j$ ($1 \\leq t_j \\leq n$, $1 \\leq d_j \\leq 10^7$), representing the kind of food and the number of dishes the $j$-th customer orders, respectively.\n\n\n-----Output-----\n\nPrint $m$ lines. In the $j$-th line print the cost for the $j$-th customer.\n\n\n-----Examples-----\nInput\n8 5\n8 6 2 1 4 5 7 5\n6 3 3 2 6 2 3 2\n2 8\n1 4\n4 7\n3 4\n6 10\n\nOutput\n22\n24\n14\n10\n39\n\nInput\n6 6\n6 6 6 6 6 6\n6 66 666 6666 66666 666666\n1 6\n2 6\n3 6\n4 6\n5 6\n6 66\n\nOutput\n36\n396\n3996\n39996\n399996\n0\n\nInput\n6 6\n6 6 6 6 6 6\n6 66 666 6666 66666 666666\n1 6\n2 13\n3 6\n4 11\n5 6\n6 6\n\nOutput\n36\n11058\n99996\n4333326\n0\n0\n\n\n\n-----Note-----\n\nIn the first sample, $5$ customers will be served as follows. Customer $1$ will be served $6$ dishes of the $2$-nd kind, $1$ dish of the $4$-th kind, and $1$ dish of the $6$-th kind. The cost is $6 \\cdot 3 + 1 \\cdot 2 + 1 \\cdot 2 = 22$. The remain of the $8$ kinds of food will be $\\{8, 0, 2, 0, 4, 4, 7, 5\\}$. Customer $2$ will be served $4$ dishes of the $1$-st kind. The cost is $4 \\cdot 6 = 24$. The remain will be $\\{4, 0, 2, 0, 4, 4, 7, 5\\}$. Customer $3$ will be served $4$ dishes of the $6$-th kind, $3$ dishes of the $8$-th kind. The cost is $4 \\cdot 2 + 3 \\cdot 2 = 14$. The remain will be $\\{4, 0, 2, 0, 4, 0, 7, 2\\}$. Customer $4$ will be served $2$ dishes of the $3$-rd kind, $2$ dishes of the $8$-th kind. The cost is $2 \\cdot 3 + 2 \\cdot 2 = 10$. The remain will be $\\{4, 0, 0, 0, 4, 0, 7, 0\\}$. Customer $5$ will be served $7$ dishes of the $7$-th kind, $3$ dishes of the $1$-st kind. The cost is $7 \\cdot 3 + 3 \\cdot 6 = 39$. The remain will be $\\{1, 0, 0, 0, 4, 0, 0, 0\\}$.\n\nIn the second sample, each customer is served what they order except the last one, who leaves angrily without paying. For example, the second customer is served $6$ dishes of the second kind, so the cost is $66 \\cdot 6 = 396$.\n\nIn the third sample, some customers may not be served what they order. For example, the second customer is served $6$ dishes of the second kind, $6$ of the third and $1$ of the fourth, so the cost is $66 \\cdot 6 + 666 \\cdot 6 + 6666 \\cdot 1 = 11058$."} +{"id": "01709", "text": "ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 1 to n from left to right.\n\nInitially, tree i has color c_{i}. ZS the Coder and Chris the Baboon recognizes only m different colors, so 0 \u2264 c_{i} \u2264 m, where c_{i} = 0 means that tree i is uncolored.\n\nZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees with c_{i} = 0. They can color each of them them in any of the m colors from 1 to m. Coloring the i-th tree with color j requires exactly p_{i}, j litres of paint.\n\nThe two friends define the beauty of a coloring of the trees as the minimum number of contiguous groups (each group contains some subsegment of trees) you can split all the n trees into so that each group contains trees of the same color. For example, if the colors of the trees from left to right are 2, 1, 1, 1, 3, 2, 2, 3, 1, 3, the beauty of the coloring is 7, since we can partition the trees into 7 contiguous groups of the same color : {2}, {1, 1, 1}, {3}, {2, 2}, {3}, {1}, {3}. \n\nZS the Coder and Chris the Baboon wants to color all uncolored trees so that the beauty of the coloring is exactly k. They need your help to determine the minimum amount of paint (in litres) needed to finish the job.\n\nPlease note that the friends can't color the trees that are already colored.\n\n\n-----Input-----\n\nThe first line contains three integers, n, m and k (1 \u2264 k \u2264 n \u2264 100, 1 \u2264 m \u2264 100)\u00a0\u2014 the number of trees, number of colors and beauty of the resulting coloring respectively.\n\nThe second line contains n integers c_1, c_2, ..., c_{n} (0 \u2264 c_{i} \u2264 m), the initial colors of the trees. c_{i} equals to 0 if the tree number i is uncolored, otherwise the i-th tree has color c_{i}.\n\nThen n lines follow. Each of them contains m integers. The j-th number on the i-th of them line denotes p_{i}, j (1 \u2264 p_{i}, j \u2264 10^9)\u00a0\u2014 the amount of litres the friends need to color i-th tree with color j. p_{i}, j's are specified even for the initially colored trees, but such trees still can't be colored.\n\n\n-----Output-----\n\nPrint a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty k, print - 1.\n\n\n-----Examples-----\nInput\n3 2 2\n0 0 0\n1 2\n3 4\n5 6\n\nOutput\n10\nInput\n3 2 2\n2 1 2\n1 3\n2 4\n3 5\n\nOutput\n-1\nInput\n3 2 2\n2 0 0\n1 3\n2 4\n3 5\n\nOutput\n5\nInput\n3 2 3\n2 1 2\n1 3\n2 4\n3 5\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first sample case, coloring the trees with colors 2, 1, 1 minimizes the amount of paint used, which equals to 2 + 3 + 5 = 10. Note that 1, 1, 1 would not be valid because the beauty of such coloring equals to 1 ({1, 1, 1} is a way to group the trees into a single group of the same color).\n\nIn the second sample case, all the trees are colored, but the beauty of the coloring is 3, so there is no valid coloring, and the answer is - 1.\n\nIn the last sample case, all the trees are colored and the beauty of the coloring matches k, so no paint is used and the answer is 0."} +{"id": "01710", "text": "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. \n\nNastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 2\u00b710^5, 1 \u2264 k \u2264 10^5), where n is the length of the array and k is the constant described above.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^8)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nIn the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k.\n\n\n-----Examples-----\nInput\n1 1\n1\n\nOutput\n1\n\nInput\n4 2\n6 3 8 1\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn 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$.\n\nThere are two suitable subsegments in the second example \u2014 [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$."} +{"id": "01711", "text": "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$). \n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($2 \\le n \\le m \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of arrays that meet all of the aforementioned conditions, taken modulo $998244353$.\n\n\n-----Examples-----\nInput\n3 4\n\nOutput\n6\n\nInput\n3 5\n\nOutput\n10\n\nInput\n42 1337\n\nOutput\n806066790\n\nInput\n100000 200000\n\nOutput\n707899035\n\n\n\n-----Note-----\n\nThe arrays in the first example are: $[1, 2, 1]$; $[1, 3, 1]$; $[1, 4, 1]$; $[2, 3, 2]$; $[2, 4, 2]$; $[3, 4, 3]$."} +{"id": "01712", "text": "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. \n\nVanya 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.\n\n\n-----Input-----\n\nThe first line contains three integers n,x,y (1 \u2264 n \u2264 10^5, 1 \u2264 x, y \u2264 10^6) \u2014 the number of monsters, the frequency of Vanya's and Vova's attack, correspondingly.\n\nNext n lines contain integers a_{i} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the number of hits needed do destroy the i-th monster.\n\n\n-----Output-----\n\nPrint 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.\n\n\n-----Examples-----\nInput\n4 3 2\n1\n2\n3\n4\n\nOutput\nVanya\nVova\nVanya\nBoth\n\nInput\n2 1 1\n1\n2\n\nOutput\nBoth\nBoth\n\n\n\n-----Note-----\n\nIn 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.\n\nIn the second sample Vanya and Vova make the first and second hit simultaneously at time 1."} +{"id": "01713", "text": "Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.\n\nFirst Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p_1, the glass from the second position to position p_2 and so on. That is, a glass goes from position i to position p_{i}. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.\n\nAfter all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.\n\n\n-----Input-----\n\nThe first line contains three integers: n, s, t (1 \u2264 n \u2264 10^5;\u00a01 \u2264 s, t \u2264 n) \u2014 the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 n) \u2014 the shuffling operation parameters. It is guaranteed that all p_{i}'s are distinct.\n\nNote that s can equal t.\n\n\n-----Output-----\n\nIf the marble can move from position s to position t, then print on a single line a non-negative integer \u2014 the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.\n\n\n-----Examples-----\nInput\n4 2 1\n2 3 4 1\n\nOutput\n3\n\nInput\n4 3 3\n4 1 3 2\n\nOutput\n0\n\nInput\n4 3 4\n1 2 3 4\n\nOutput\n-1\n\nInput\n3 1 3\n2 1 3\n\nOutput\n-1"} +{"id": "01714", "text": "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}.\n\nSimon has a positive integer n and a non-negative integer k, such that 2k \u2264 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$.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 50000, 0 \u2264 2k \u2264 n).\n\n\n-----Output-----\n\nPrint 2n integers a_1, a_2, ..., a_2n \u2014 the required permutation a. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them.\n\n\n-----Examples-----\nInput\n1 0\n\nOutput\n1 2\nInput\n2 1\n\nOutput\n3 2 1 4\n\nInput\n4 0\n\nOutput\n2 7 4 6 1 3 5 8\n\n\n\n-----Note-----\n\nRecord |x| represents the absolute value of number x. \n\nIn the first sample |1 - 2| - |1 - 2| = 0.\n\nIn the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2.\n\nIn the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0."} +{"id": "01715", "text": "Along a road running in an east-west direction, there are A shrines and B temples.\nThe i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.\nAnswer the following Q queries:\n - Query i (1 \\leq i \\leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)\n\n-----Constraints-----\n - 1 \\leq A, B \\leq 10^5\n - 1 \\leq Q \\leq 10^5\n - 1 \\leq s_1 < s_2 < ... < s_A \\leq 10^{10}\n - 1 \\leq t_1 < t_2 < ... < t_B \\leq 10^{10}\n - 1 \\leq x_i \\leq 10^{10}\n - s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B Q\ns_1\n:\ns_A\nt_1\n:\nt_B\nx_1\n:\nx_Q\n\n-----Output-----\nPrint Q lines. The i-th line should contain the answer to the i-th query.\n\n-----Sample Input-----\n2 3 4\n100\n600\n400\n900\n1000\n150\n2000\n899\n799\n\n-----Sample Output-----\n350\n1400\n301\n399\n\nThere are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.\n - Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.\n - Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.\n - Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.\n - Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple."} +{"id": "01716", "text": "In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east.\nA company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i).\nTakahashi the king is interested in the following Q matters:\n - The number of the trains that runs strictly within the section from City p_i to City q_i, that is, the number of trains j such that p_i \\leq L_j and R_j \\leq q_i.\nAlthough he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.\n\n-----Constraints-----\n - N is an integer between 1 and 500 (inclusive).\n - M is an integer between 1 and 200 \\ 000 (inclusive).\n - Q is an integer between 1 and 100 \\ 000 (inclusive).\n - 1 \\leq L_i \\leq R_i \\leq N (1 \\leq i \\leq M)\n - 1 \\leq p_i \\leq q_i \\leq N (1 \\leq i \\leq Q)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M Q\nL_1 R_1\nL_2 R_2\n:\nL_M R_M\np_1 q_1\np_2 q_2\n:\np_Q q_Q\n\n-----Output-----\nPrint Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i.\n\n-----Sample Input-----\n2 3 1\n1 1\n1 2\n2 2\n1 2\n\n-----Sample Output-----\n3\n\nAs all the trains runs within the section from City 1 to City 2, the answer to the only query is 3."} +{"id": "01717", "text": "We have an integer N.\nPrint 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.\nUnder the constraints of this problem, there is always at least one such integer x.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 30\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint 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.\nIf there are multiple such integers, any of them will be accepted.\n\n-----Sample Input-----\n3\n\n-----Sample Output-----\n7\n\nThe remainder when 7 is divided by 2 is 1, and the remainder when 7 is divided by 3 is 1, too.\n7 is an integer between 3 and 10^{13}, so this is a desirable output."} +{"id": "01718", "text": "There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.\nOn this sequence, Snuke can perform the following operation:\n - Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.\nSnuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.\nFind the minimum number of operations required.\nIt can be proved that, Under the constraints of this problem, this objective is always achievable.\n\n-----Constraints-----\n - 2 \\leq K \\leq N \\leq 100000\n - A_1, A_2, ..., A_N is a permutation of 1, 2, ..., N.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the minimum number of operations required.\n\n-----Sample Input-----\n4 3\n2 3 1 4\n\n-----Sample Output-----\n2\n\nOne optimal strategy is as follows:\n - In the first operation, choose the first, second and third elements. The sequence A becomes 1, 1, 1, 4.\n - In the second operation, choose the second, third and fourth elements. The sequence A becomes 1, 1, 1, 1."} +{"id": "01719", "text": "You are given an integer N. Find the number of strings of length N that satisfy the following conditions, modulo 10^9+7:\n - The string does not contain characters other than A, C, G and T.\n - The string does not contain AGC as a substring.\n - The condition above cannot be violated by swapping two adjacent characters once.\n\n-----Notes-----\nA substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.\nFor example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.\n\n-----Constraints-----\n - 3 \\leq N \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the number of strings of length N that satisfy the following conditions, modulo 10^9+7.\n\n-----Sample Input-----\n3\n\n-----Sample Output-----\n61\n\nThere are 4^3 = 64 strings of length 3 that do not contain characters other than A, C, G and T. Among them, only AGC, ACG and GAC violate the condition, so the answer is 64 - 3 = 61."} +{"id": "01720", "text": "Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.\n\nFormally, her room can be represented as a field of n \u00d7 m cells, each cell of which is empty or littered with cans.\n\nOlya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.\n\nNow Olya needs to get from cell (x_1, y_1) to cell (x_2, y_2). How many seconds will it take her if she moves optimally?\n\nIt's guaranteed that cells (x_1, y_1) and (x_2, y_2) are empty. These cells can coincide.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and k (1 \u2264 n, m, k \u2264 1000) \u2014 the sizes of the room and Olya's speed.\n\nThen n lines follow containing m characters each, the i-th of them contains on j-th position \"#\", if the cell (i, j) is littered with cans, and \".\" otherwise.\n\nThe last line contains four integers x_1, y_1, x_2, y_2 (1 \u2264 x_1, x_2 \u2264 n, 1 \u2264 y_1, y_2 \u2264 m) \u2014 the coordinates of the first and the last cells.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum time it will take Olya to get from (x_1, y_1) to (x_2, y_2).\n\nIf it's impossible to get from (x_1, y_1) to (x_2, y_2), print -1.\n\n\n-----Examples-----\nInput\n3 4 4\n....\n###.\n....\n1 1 3 1\n\nOutput\n3\nInput\n3 4 1\n....\n###.\n....\n1 1 3 1\n\nOutput\n8\nInput\n2 2 1\n.#\n#.\n1 1 2 2\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.\n\nIn second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.\n\nOlya does not recommend drinking energy drinks and generally believes that this is bad."} +{"id": "01721", "text": "You are given a string $s$. You have to reverse it \u2014 that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal \u2014 and so on. For example, if your goal is to reverse the string \"abddea\", you should get the string \"aeddba\". To accomplish your goal, you can swap the neighboring elements of the string. \n\nYour task is to calculate the minimum number of swaps you have to perform to reverse the given string.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 200\\,000$) \u2014 the length of $s$.\n\nThe second line contains $s$ \u2014 a string consisting of $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of swaps of neighboring elements you have to perform to reverse the string.\n\n\n-----Examples-----\nInput\n5\naaaza\n\nOutput\n2\n\nInput\n6\ncbaabc\n\nOutput\n0\n\nInput\n9\nicpcsguru\n\nOutput\n30\n\n\n\n-----Note-----\n\nIn the first example, you have to swap the third and the fourth elements, so the string becomes \"aazaa\". Then you have to swap the second and the third elements, so the string becomes \"azaaa\". So, it is possible to reverse the string in two swaps.\n\nSince the string in the second example is a palindrome, you don't have to do anything to reverse it."} +{"id": "01722", "text": "There are $n$ students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let $x$ be the number of such pairs of students in a split. Pairs $(a, b)$ and $(b, a)$ are the same and counted only once.\n\nFor example, if there are $6$ students: \"olivia\", \"jacob\", \"tanya\", \"jack\", \"oliver\" and \"jessica\", then: splitting into two classrooms (\"jack\", \"jacob\", \"jessica\", \"tanya\") and (\"olivia\", \"oliver\") will give $x=4$ ($3$ chatting pairs in the first classroom, $1$ chatting pair in the second classroom), splitting into two classrooms (\"jack\", \"tanya\", \"olivia\") and (\"jessica\", \"oliver\", \"jacob\") will give $x=1$ ($0$ chatting pairs in the first classroom, $1$ chatting pair in the second classroom). \n\nYou are given the list of the $n$ names. What is the minimum $x$ we can obtain by splitting the students into classrooms?\n\nNote that it is valid to place all of the students in one of the classrooms, leaving the other one empty.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1\\leq n \\leq 100$)\u00a0\u2014 the number of students.\n\nAfter this $n$ lines follow.\n\nThe $i$-th line contains the name of the $i$-th student.\n\nIt is guaranteed each name is a string of lowercase English letters of length at most $20$. Note that multiple students may share the same name.\n\n\n-----Output-----\n\nThe output must consist of a single integer $x$\u00a0\u2014 the minimum possible number of chatty pairs.\n\n\n-----Examples-----\nInput\n4\njorge\njose\noscar\njerry\n\nOutput\n1\n\nInput\n7\nkambei\ngorobei\nshichiroji\nkyuzo\nheihachi\nkatsushiro\nkikuchiyo\n\nOutput\n2\n\nInput\n5\nmike\nmike\nmike\nmike\nmike\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample the minimum number of pairs is $1$. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.\n\nIn the second sample the minimum number of pairs is $2$. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.\n\nIn the third sample the minimum number of pairs is $4$. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom."} +{"id": "01723", "text": "Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:\n\nGiven 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. \n\nThe 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.\n\nEhab 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.\n\n\n-----Input-----\n\nThe only line contains an integer n (2 \u2264 n \u2264 10^5), the number of nodes in the desired trees.\n\n\n-----Output-----\n\nThe 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.\n\nIf the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 \u2264 u, v \u2264 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.\n\nIf there are multiple answers, you can print any of them.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n-1\n1 2\n\nInput\n8\n\nOutput\n1 2\n1 3\n2 4\n2 5\n3 6\n4 7\n4 8\n1 2\n1 3\n2 4\n2 5\n2 6\n3 7\n6 8\n\n\n-----Note-----\n\nIn 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.\n\nIn the second sample:\n\nIn 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]"} +{"id": "01724", "text": "Valera has array a, consisting of n integers a_0, a_1, ..., a_{n} - 1, and function f(x), taking an integer from 0 to 2^{n} - 1 as its single argument. Value f(x) is calculated by formula $f(x) = \\sum_{i = 0}^{n - 1} a_{i} \\cdot b i t(i)$, where value bit(i) equals one if the binary representation of number x contains a 1 on the i-th position, and zero otherwise.\n\nFor example, if n = 4 and x = 11 (11 = 2^0 + 2^1 + 2^3), then f(x) = a_0 + a_1 + a_3.\n\nHelp Valera find the maximum of function f(x) among all x, for which an inequality holds: 0 \u2264 x \u2264 m.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of array elements. The next line contains n space-separated integers a_0, a_1, ..., a_{n} - 1 (0 \u2264 a_{i} \u2264 10^4) \u2014 elements of array a.\n\nThe third line contains a sequence of digits zero and one without spaces s_0s_1... s_{n} - 1 \u2014 the binary representation of number m. Number m equals $\\sum_{i = 0}^{n - 1} 2^{i} \\cdot s_{i}$.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum value of function f(x) for all $x \\in [ 0 . . m ]$.\n\n\n-----Examples-----\nInput\n2\n3 8\n10\n\nOutput\n3\n\nInput\n5\n17 0 10 2 1\n11010\n\nOutput\n27\n\n\n\n-----Note-----\n\nIn the first test case m = 2^0 = 1, f(0) = 0, f(1) = a_0 = 3.\n\nIn the second sample m = 2^0 + 2^1 + 2^3 = 11, the maximum value of function equals f(5) = a_0 + a_2 = 17 + 10 = 27."} +{"id": "01725", "text": "Little penguin Polo has an n \u00d7 m matrix, consisting of integers. Let's index the matrix rows from 1 to n from top to bottom and let's index the columns from 1 to m from left to right. Let's represent the matrix element on the intersection of row i and column j as a_{ij}.\n\nIn one move the penguin can add or subtract number d from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and d (1 \u2264 n, m \u2264 100, 1 \u2264 d \u2264 10^4) \u2014 the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element a_{ij} (1 \u2264 a_{ij} \u2264 10^4).\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print \"-1\" (without the quotes).\n\n\n-----Examples-----\nInput\n2 2 2\n2 4\n6 8\n\nOutput\n4\n\nInput\n1 2 7\n6 7\n\nOutput\n-1"} +{"id": "01726", "text": "Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.\n\nBut she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is a_{i}. If some free time remains, she can spend it on reading.\n\nHelp Luba to determine the minimum number of day when she finishes reading.\n\nIt is guaranteed that the answer doesn't exceed n.\n\nRemember that there are 86400 seconds in a day.\n\n\n-----Input-----\n\nThe first line contains two integers n and t (1 \u2264 n \u2264 100, 1 \u2264 t \u2264 10^6) \u2014 the number of days and the time required to read the book.\n\nThe second line contains n integers a_{i} (0 \u2264 a_{i} \u2264 86400) \u2014 the time Luba has to spend on her work during i-th day.\n\n\n-----Output-----\n\nPrint the minimum day Luba can finish reading the book.\n\nIt is guaranteed that answer doesn't exceed n.\n\n\n-----Examples-----\nInput\n2 2\n86400 86398\n\nOutput\n2\n\nInput\n2 86400\n0 86400\n\nOutput\n1"} +{"id": "01727", "text": "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.\n\nThere 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. \n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of trees.\n\nNext n lines contain pairs of integers x_{i}, h_{i} (1 \u2264 x_{i}, h_{i} \u2264 10^9) \u2014 the coordinate and the height of the \u0456-th tree.\n\nThe pairs are given in the order of ascending x_{i}. No two trees are located at the point with the same coordinate.\n\n\n-----Output-----\n\nPrint a single number \u2014 the maximum number of trees that you can cut down by the given rules.\n\n\n-----Examples-----\nInput\n5\n1 2\n2 1\n5 10\n10 9\n19 1\n\nOutput\n3\n\nInput\n5\n1 2\n2 1\n5 10\n10 9\n20 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample you can fell the trees like that: fell the 1-st tree to the left \u2014 now it occupies segment [ - 1;1] fell the 2-nd tree to the right \u2014 now it occupies segment [2;3] leave the 3-rd tree \u2014 it occupies point 5 leave the 4-th tree \u2014 it occupies point 10 fell the 5-th tree to the right \u2014 now it occupies segment [19;20] \n\nIn the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19]."} +{"id": "01728", "text": "You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1.\n\nEach vertex has a color, let's denote the color of vertex v by c_{v}. Initially c_{v} = 0.\n\nYou 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.\n\nIt is guaranteed that you have to color each vertex in a color different from 0.\n\nYou can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory).\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 10^4)\u00a0\u2014 the number of vertices in the tree.\n\nThe second line contains n - 1 integers p_2, p_3, ..., p_{n} (1 \u2264 p_{i} < i), where p_{i} means that there is an edge between vertices i and p_{i}.\n\nThe third line contains n integers c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 n), where c_{i} is the color you should color the i-th vertex into.\n\nIt is guaranteed that the given graph is a tree. \n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of steps you have to perform to color the tree into given colors.\n\n\n-----Examples-----\nInput\n6\n1 2 2 1 5\n2 1 1 1 1 1\n\nOutput\n3\n\nInput\n7\n1 1 2 3 1 4\n3 3 1 1 1 2 3\n\nOutput\n5\n\n\n\n-----Note-----\n\nThe tree from the first sample is shown on the picture (numbers are vetices' indices):\n\n$A$\n\nOn first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors):\n\n[Image]\n\nOn seond step we color all vertices in the subtree of vertex 5 into color 1:\n\n[Image]\n\nOn third step we color all vertices in the subtree of vertex 2 into color 1:\n\n[Image]\n\nThe tree from the second sample is shown on the picture (numbers are vetices' indices):\n\n[Image]\n\nOn first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors):\n\n[Image]\n\nOn second step we color all vertices in the subtree of vertex 3 into color 1:\n\n$8$\n\nOn third step we color all vertices in the subtree of vertex 6 into color 2:\n\n[Image]\n\nOn fourth step we color all vertices in the subtree of vertex 4 into color 1:\n\n[Image]\n\nOn fith step we color all vertices in the subtree of vertex 7 into color 3:\n\n[Image]"} +{"id": "01729", "text": "There is a programming language in which every program is a non-empty sequence of \"<\" and \">\" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. Current character pointer (CP); Direction pointer (DP) which can point left or right; \n\nInitially CP points to the leftmost character of the sequence and DP points to the right.\n\nWe repeat the following steps until the first moment that CP points to somewhere outside the sequence. If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one. If CP is pointing to \"<\" or \">\" then the direction of DP changes to \"left\" or \"right\" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is \"<\" or \">\" then the previous character will be erased from the sequence. \n\nIf at any moment the CP goes outside of the sequence the execution is terminated.\n\nIt's obvious the every program in this language terminates after some steps.\n\nWe have a sequence s_1, s_2, ..., s_{n} of \"<\", \">\" and digits. You should answer q queries. Each query gives you l and r and asks how many of each digit will be printed if we run the sequence s_{l}, s_{l} + 1, ..., s_{r} as an independent program in this language.\n\n\n-----Input-----\n\nThe first line of input contains two integers n and q (1 \u2264 n, q \u2264 100) \u2014 represents the length of the sequence s and the number of queries. \n\nThe second line contains s, a sequence of \"<\", \">\" and digits (0..9) written from left to right. Note, that the characters of s are not separated with spaces. \n\nThe next q lines each contains two integers l_{i} and r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n) \u2014 the i-th query.\n\n\n-----Output-----\n\nFor each query print 10 space separated integers: x_0, x_1, ..., x_9 where x_{i} equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in input.\n\n\n-----Examples-----\nInput\n7 4\n1>3>22<\n1 3\n4 7\n7 7\n1 7\n\nOutput\n0 1 0 1 0 0 0 0 0 0 \n2 2 2 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n2 3 2 1 0 0 0 0 0 0"} +{"id": "01730", "text": "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.\n\nA 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 \u2264 i < d) nodes v_{i} and v_{i} + 1 are connected by an edge of the graph.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, k (3 \u2264 n, m \u2264 10^5;\u00a02 \u2264 k \u2264 n - 1) \u2014 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 \u2264 a_{i}, b_{i} \u2264 n;\u00a0a_{i} \u2260 b_{i}) \u2014 the indexes of the graph nodes that are connected by the i-th edge. \n\nIt 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.\n\n\n-----Output-----\n\nIn the first line print integer r (r \u2265 k + 1) \u2014 the length of the found cycle. In the next line print r distinct integers v_1, v_2, ..., v_{r} (1 \u2264 v_{i} \u2264 n) \u2014 the found simple cycle.\n\nIt is guaranteed that the answer exists. If there are multiple correct answers, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n3 3 2\n1 2\n2 3\n3 1\n\nOutput\n3\n1 2 3 \nInput\n4 6 3\n4 3\n1 2\n1 3\n1 4\n2 3\n2 4\n\nOutput\n4\n3 4 1 2"} +{"id": "01731", "text": "You are given two integers $n$ and $m$. Calculate the number of pairs of arrays $(a, b)$ such that:\n\n the length of both arrays is equal to $m$; each element of each array is an integer between $1$ and $n$ (inclusive); $a_i \\le b_i$ for any index $i$ from $1$ to $m$; array $a$ is sorted in non-descending order; array $b$ is sorted in non-ascending order. \n\nAs the result can be very large, you should print it modulo $10^9+7$.\n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $m$ ($1 \\le n \\le 1000$, $1 \\le m \\le 10$).\n\n\n-----Output-----\n\nPrint one integer \u2013 the number of arrays $a$ and $b$ satisfying the conditions described above modulo $10^9+7$.\n\n\n-----Examples-----\nInput\n2 2\n\nOutput\n5\n\nInput\n10 1\n\nOutput\n55\n\nInput\n723 9\n\nOutput\n157557417\n\n\n\n-----Note-----\n\nIn the first test there are $5$ suitable arrays: $a = [1, 1], b = [2, 2]$; $a = [1, 2], b = [2, 2]$; $a = [2, 2], b = [2, 2]$; $a = [1, 1], b = [2, 1]$; $a = [1, 1], b = [1, 1]$."} +{"id": "01732", "text": "Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.\n\nThere are also n cards, each card has 2 attributes: length l_{i} and cost c_{i}. If she pays c_{i} dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length l_{i}, i. e. from cell x to cell (x - l_{i}) or cell (x + l_{i}).\n\nShe wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. \n\nIf this is possible, calculate the minimal cost.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 300), number of cards.\n\nThe second line contains n numbers l_{i} (1 \u2264 l_{i} \u2264 10^9), the jump lengths of cards.\n\nThe third line contains n numbers c_{i} (1 \u2264 c_{i} \u2264 10^5), the costs of cards.\n\n\n-----Output-----\n\nIf it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.\n\n\n-----Examples-----\nInput\n3\n100 99 9900\n1 1 1\n\nOutput\n2\n\nInput\n5\n10 20 30 40 50\n1 1 1 1 1\n\nOutput\n-1\n\nInput\n7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10\n\nOutput\n6\n\nInput\n8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n\nOutput\n7237\n\n\n\n-----Note-----\n\nIn first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.\n\nIn the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1."} +{"id": "01733", "text": "Kuro is living in a country called Uberland, consisting of $n$ towns, numbered from $1$ to $n$, and $n - 1$ bidirectional roads connecting these towns. It is possible to reach each town from any other. Each road connects two towns $a$ and $b$. Kuro loves walking and he is planning to take a walking marathon, in which he will choose a pair of towns $(u, v)$ ($u \\neq v$) and walk from $u$ using the shortest path to $v$ (note that $(u, v)$ is considered to be different from $(v, u)$).\n\nOddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index $x$) and Beetopia (denoted with the index $y$). Flowrisa is a town where there are many strong-scent flowers, and Beetopia is another town where many bees live. In particular, Kuro will avoid any pair of towns $(u, v)$ if on the path from $u$ to $v$, he reaches Beetopia after he reached Flowrisa, since the bees will be attracted with the flower smell on Kuro\u2019s body and sting him.\n\nKuro wants to know how many pair of city $(u, v)$ he can take as his route. Since he\u2019s not really bright, he asked you to help him with this problem.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $x$ and $y$ ($1 \\leq n \\leq 3 \\cdot 10^5$, $1 \\leq x, y \\leq n$, $x \\ne y$) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.\n\n$n - 1$ lines follow, each line contains two integers $a$ and $b$ ($1 \\leq a, b \\leq n$, $a \\ne b$), describes a road connecting two towns $a$ and $b$.\n\nIt is guaranteed that from each town, we can reach every other town in the city using the given roads. That is, the given map of towns and roads is a tree.\n\n\n-----Output-----\n\nA single integer resembles the number of pair of towns $(u, v)$ that Kuro can use as his walking route.\n\n\n-----Examples-----\nInput\n3 1 3\n1 2\n2 3\n\nOutput\n5\nInput\n3 1 3\n1 2\n1 3\n\nOutput\n4\n\n\n-----Note-----\n\nOn the first example, Kuro can choose these pairs: $(1, 2)$: his route would be $1 \\rightarrow 2$, $(2, 3)$: his route would be $2 \\rightarrow 3$, $(3, 2)$: his route would be $3 \\rightarrow 2$, $(2, 1)$: his route would be $2 \\rightarrow 1$, $(3, 1)$: his route would be $3 \\rightarrow 2 \\rightarrow 1$. \n\nKuro can't choose pair $(1, 3)$ since his walking route would be $1 \\rightarrow 2 \\rightarrow 3$, in which Kuro visits town $1$ (Flowrisa) and then visits town $3$ (Beetopia), which is not allowed (note that pair $(3, 1)$ is still allowed because although Kuro visited Flowrisa and Beetopia, he did not visit them in that order).\n\nOn the second example, Kuro can choose the following pairs: $(1, 2)$: his route would be $1 \\rightarrow 2$, $(2, 1)$: his route would be $2 \\rightarrow 1$, $(3, 2)$: his route would be $3 \\rightarrow 1 \\rightarrow 2$, $(3, 1)$: his route would be $3 \\rightarrow 1$."} +{"id": "01734", "text": "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.\n\nThere 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:\n\n 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. \n\nFor 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.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 70000) \u2014 the total number of phone contacts in Polycarp's contacts.\n\nThe 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.\n\n\n-----Output-----\n\nPrint 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.\n\n\n-----Examples-----\nInput\n3\n123456789\n100000000\n100123456\n\nOutput\n9\n000\n01\n\nInput\n4\n123456789\n193456789\n134567819\n934567891\n\nOutput\n2\n193\n81\n91"} +{"id": "01735", "text": "Two people are playing a game with a string $s$, consisting of lowercase latin letters. \n\nOn a player's turn, he should choose two consecutive equal letters in the string and delete them. \n\nFor example, if the string is equal to \"xaax\" than there is only one possible turn: delete \"aa\", so the string will become \"xx\". A player not able to make a turn loses.\n\nYour task is to determine which player will win if both play optimally.\n\n\n-----Input-----\n\nThe only line contains the string $s$, consisting of lowercase latin letters ($1 \\leq |s| \\leq 100\\,000$), where $|s|$ means the length of a string $s$.\n\n\n-----Output-----\n\nIf the first player wins, print \"Yes\". If the second player wins, print \"No\".\n\n\n-----Examples-----\nInput\nabacaba\n\nOutput\nNo\n\nInput\niiq\n\nOutput\nYes\n\nInput\nabba\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first example the first player is unable to make a turn, so he loses.\n\nIn the second example first player turns the string into \"q\", then second player is unable to move, so he loses."} +{"id": "01736", "text": "When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs a_{i} minutes to read the i-th book.\n\nValera decided to choose an arbitrary book with number i and read the books one by one, starting from this book. In other words, he will first read book number i, then book number i + 1, then book number i + 2 and so on. He continues the process until he either runs out of the free time or finishes reading the n-th book. Valera reads each book up to the end, that is, he doesn't start reading the book if he doesn't have enough free time to finish reading it. \n\nPrint the maximum number of books Valera can read.\n\n\n-----Input-----\n\nThe first line contains two integers n and t (1 \u2264 n \u2264 10^5;\u00a01 \u2264 t \u2264 10^9) \u2014 the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^4), where number a_{i} shows the number of minutes that the boy needs to read the i-th book.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of books Valera can read.\n\n\n-----Examples-----\nInput\n4 5\n3 1 2 1\n\nOutput\n3\n\nInput\n3 3\n2 2 3\n\nOutput\n1"} +{"id": "01737", "text": "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.\n\nA project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version \u2014 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.\n\nYou'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). \n\nIt'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.\n\nMore 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. \n\nOutput all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.\n\n\n-----Input-----\n\nThe first line contains an only integer n (1 \u2264 n \u2264 1 000) \u2014 the number of projects in Vaja.\n\nThe 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.\n\nIt's guaranteed that there are no cyclic dependencies. \n\n\n-----Output-----\n\nOutput all Polycarp's project's dependencies in lexicographical order.\n\n\n-----Examples-----\nInput\n4\na 3\n2\nb 1\nc 1\n\u00a0\nb 2\n0\n\u00a0\nb 1\n1\nb 2\n\u00a0\nc 1\n1\nb 2\n\nOutput\n2\nb 1\nc 1\n\nInput\n9\ncodehorses 5\n3\nwebfrmk 6\nmashadb 1\nmashadb 2\n\u00a0\ncommons 2\n0\n\u00a0\nmashadb 3\n0\n\u00a0\nwebfrmk 6\n2\nmashadb 3\ncommons 2\n\u00a0\nextra 4\n1\nextra 3\n\u00a0\nextra 3\n0\n\u00a0\nextra 1\n0\n\u00a0\nmashadb 1\n1\nextra 3\n\u00a0\nmashadb 2\n1\nextra 1\n\nOutput\n4\ncommons 2\nextra 1\nmashadb 2\nwebfrmk 6\n\nInput\n3\nabc 1\n2\nabc 3\ncba 2\n\nabc 3\n0\n\ncba 2\n0\n\nOutput\n1\ncba 2\n\n\n\n-----Note-----\n\nThe 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 \u00aba\u00bb (version 3) depends on are painted black. [Image] \n\nThe 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 \u00abcodehorses\u00bb (version 5) depends on are paint it black. Note that \u00abextra 1\u00bb is chosen instead of \u00abextra 3\u00bb since \u00abmashadb 1\u00bb and all of its dependencies are ignored due to \u00abmashadb 2\u00bb. [Image]"} +{"id": "01738", "text": "Ivan wants to write a letter to his friend. The letter is a string s consisting of lowercase Latin letters.\n\nUnfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string s instead of the string itself.\n\nThe compressed version of string s is a sequence of strings c_1, s_1, c_2, s_2, ..., c_{k}, s_{k}, where c_{i} is the decimal representation of number a_{i} (without any leading zeroes) and s_{i} is some string consisting of lowercase Latin letters. If Ivan writes string s_1 exactly a_1 times, then string s_2 exactly a_2 times, and so on, the result will be string s.\n\nThe length of a compressed version is |c_1| + |s_1| + |c_2| + |s_2|... |c_{k}| + |s_{k}|. Among all compressed versions Ivan wants to choose a version such that its length is minimum possible. Help Ivan to determine minimum possible length.\n\n\n-----Input-----\n\nThe only line of input contains one string s consisting of lowercase Latin letters (1 \u2264 |s| \u2264 8000).\n\n\n-----Output-----\n\nOutput one integer number \u2014 the minimum possible length of a compressed version of s.\n\n\n-----Examples-----\nInput\naaaaaaaaaa\n\nOutput\n3\n\nInput\nabcab\n\nOutput\n6\n\nInput\ncczabababab\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first example Ivan will choose this compressed version: c_1 is 10, s_1 is a.\n\nIn the second example Ivan will choose this compressed version: c_1 is 1, s_1 is abcab.\n\nIn the third example Ivan will choose this compressed version: c_1 is 2, s_1 is c, c_2 is 1, s_2 is z, c_3 is 4, s_3 is ab."} +{"id": "01739", "text": "Simon has a prime number x and an array of non-negative integers a_1, a_2, ..., a_{n}.\n\nSimon loves fractions very much. Today he wrote out number $\\frac{1}{x^{a} 1} + \\frac{1}{x^{a_{2}}} + \\ldots + \\frac{1}{x^{a_{n}}}$ on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: $\\frac{s}{t}$, where number t equals x^{a}_1 + a_2 + ... + a_{n}. Now Simon wants to reduce the resulting fraction. \n\nHelp him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe first line contains two positive integers n and x (1 \u2264 n \u2264 10^5, 2 \u2264 x \u2264 10^9) \u2014 the size of the array and the prime number.\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (0 \u2264 a_1 \u2264 a_2 \u2264 ... \u2264 a_{n} \u2264 10^9). \n\n\n-----Output-----\n\nPrint a single number \u2014 the answer to the problem modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n2 2\n2 2\n\nOutput\n8\n\nInput\n3 3\n1 2 3\n\nOutput\n27\n\nInput\n2 2\n29 29\n\nOutput\n73741817\n\nInput\n4 5\n0 0 0 0\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample $\\frac{1}{4} + \\frac{1}{4} = \\frac{4 + 4}{16} = \\frac{8}{16}$. Thus, the answer to the problem is 8.\n\nIn the second sample, $\\frac{1}{3} + \\frac{1}{9} + \\frac{1}{27} = \\frac{243 + 81 + 27}{729} = \\frac{351}{729}$. The answer to the problem is 27, as 351 = 13\u00b727, 729 = 27\u00b727.\n\nIn the third sample the answer to the problem is 1073741824\u00a0mod\u00a01000000007 = 73741817.\n\nIn the fourth sample $\\frac{1}{1} + \\frac{1}{1} + \\frac{1}{1} + \\frac{1}{1} = \\frac{4}{1}$. Thus, the answer to the problem is 1."} +{"id": "01740", "text": "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.\n\nObserving 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. \n\nSince Asya has never putted partitions back, after $n - 1$ days the cage contained a single cell, having all kittens.\n\nFor 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.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 150\\,000$)\u00a0\u2014 the number of kittens.\n\nEach 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$)\u00a0\u2014 indices of kittens, which got together due to the border removal on the corresponding day.\n\nIt's guaranteed, that the kittens $x_i$ and $y_i$ were in the different cells before this day.\n\n\n-----Output-----\n\nFor every cell from $1$ to $n$ print a single integer\u00a0\u2014 the index of the kitten from $1$ to $n$, who was originally in it.\n\nAll printed integers must be distinct.\n\nIt's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.\n\n\n-----Example-----\nInput\n5\n1 4\n2 5\n3 1\n4 5\n\nOutput\n3 1 4 2 5\n\n\n\n-----Note-----\n\nThe answer for the example contains one of several possible initial arrangements of the kittens.\n\nThe 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]"} +{"id": "01741", "text": "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.\n\nThe 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.\n\nFor 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.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 10^5$) \u2014 the number of animals. \n\nEach of the next $n$ lines contains two integers $x_{i}$, $y_{i}$ ($-10^7 \\le x_{i}, y_{i} \\le 10^7$) \u2014 the coordinates of the $i$-th animal's lair. It is guaranteed that $y_{i} \\neq 0$. No two lairs coincide.\n\n\n-----Output-----\n\nIf 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}$.\n\nFormally, 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}$.\n\n\n-----Examples-----\nInput\n1\n0 1\n\nOutput\n0.5\nInput\n3\n0 1\n0 2\n0 -3\n\nOutput\n-1\n\nInput\n2\n0 1\n1 1\n\nOutput\n0.625\n\n\n-----Note-----\n\nIn the first sample it is optimal to build the reserve with the radius equal to $0.5$ and the center in $(0,\\ 0.5)$.\n\nIn the second sample it is impossible to build a reserve.\n\nIn 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})$."} +{"id": "01742", "text": "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.\n\nFormally, 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. \n\nNastya asks you to find the maximal number of places in queue she can move forward. \n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\leq n \\leq 3 \\cdot 10^{5}$, $0 \\leq m \\leq 5 \\cdot 10^{5}$)\u00a0\u2014 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.\n\nThe second line contains $n$ integers $p_1$, $p_2$, ..., $p_n$\u00a0\u2014 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.\n\nThe $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.\n\nNastya is the last person in the queue, i.e. the pupil with number $p_n$.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of places in queue she can move forward.\n\n\n-----Examples-----\nInput\n2 1\n1 2\n1 2\n\nOutput\n1\nInput\n3 3\n3 1 2\n1 2\n3 1\n3 2\n\nOutput\n2\nInput\n5 2\n3 1 5 4 2\n5 2\n5 4\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first example Nastya can just change places with the first pupil in the queue.\n\nOptimal 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$. \n\nThe queue looks like $[3, 1, 2]$, then $[1, 3, 2]$, then $[1, 2, 3]$, and finally $[2, 1, 3]$ after these operations."} +{"id": "01743", "text": "Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more. \n\nDima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?\n\nInna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.\n\nHelp Inna maximize the total joy the hares radiate. :)\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 3000) \u2014 the number of hares. Then three lines follow, each line has n integers. The first line contains integers a_1 a_2 ... a_{n}. The second line contains b_1, b_2, ..., b_{n}. The third line contains c_1, c_2, ..., c_{n}. The following limits are fulfilled: 0 \u2264 a_{i}, b_{i}, c_{i} \u2264 10^5.\n\nNumber a_{i} in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number b_{i} in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number \u0441_{i} in the third line shows the joy that hare number i radiates if both his adjacent hares are full.\n\n\n-----Output-----\n\nIn a single line, print the maximum possible total joy of the hares Inna can get by feeding them.\n\n\n-----Examples-----\nInput\n4\n1 2 3 4\n4 3 2 1\n0 1 1 0\n\nOutput\n13\n\nInput\n7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3\n\nOutput\n44\n\nInput\n3\n1 1 1\n1 2 1\n1 1 1\n\nOutput\n4"} +{"id": "01744", "text": "The only difference between easy and hard versions is constraints.\n\nIf you write a solution in Python, then prefer to send it in PyPy to speed up execution time.\n\nA session has begun at Beland State University. Many students are taking exams.\n\nPolygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. \n\nStudents take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student.\n\nThe duration of the whole exam for all students is $M$ minutes ($\\max t_i \\le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam.\n\nFor each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam.\n\nFor each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $M$ ($1 \\le n \\le 2 \\cdot 10^5$, $1 \\le M \\le 2 \\cdot 10^7$)\u00a0\u2014 the number of students and the total duration of the exam in minutes, respectively.\n\nThe second line of the input contains $n$ integers $t_i$ ($1 \\le t_i \\le 100$)\u00a0\u2014 time in minutes that $i$-th student spends to answer to a ticket.\n\nIt's guaranteed that all values of $t_i$ are not greater than $M$.\n\n\n-----Output-----\n\nPrint $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam.\n\n\n-----Examples-----\nInput\n7 15\n1 2 3 4 5 6 7\n\nOutput\n0 0 0 0 0 2 3 \nInput\n5 100\n80 40 40 40 60\n\nOutput\n0 1 1 2 3 \n\n\n-----Note-----\n\nThe explanation for the example 1.\n\nPlease note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$.\n\nIn order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$).\n\nIn order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$)."} +{"id": "01745", "text": "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.\n\nThe city is an n \u00d7 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 '#'.\n\nThe 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. \n\nHere are some samples of allowed piping routes: \n\n ....# ....# .*..#\n\n ***** ****. .***.\n\n ..#.. ..#*. ..#*.\n\n #...# #..*# #..*#\n\n ..... ...*. ...*.\n\n\n\nHere are some samples of forbidden piping routes: \n\n .**.# *...# .*.*#\n\n ..... ****. .*.*.\n\n ..#.. ..#*. .*#*.\n\n #...# #..*# #*.*#\n\n ..... ...*. .***.\n\n\n\nIn these samples the pipes are represented by characters ' * '.\n\nYou were asked to write a program that calculates the number of distinct ways to make exactly one pipe in the city. \n\nThe two ways to make a pipe are considered distinct if they are distinct in at least one square.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n, m (2 \u2264 n, m \u2264 2000)\u00a0\u2014\u00a0the height and width of Berland map.\n\nEach of the next n lines contains m characters \u2014 the map of the city. \n\nIf the square of the map is marked by character '.', then the square is empty and the pipe can through it. \n\nIf the square of the map is marked by character '#', then the square is full and the pipe can't through it.\n\n\n-----Output-----\n\nIn the first line of the output print a single integer \u2014 the number of distinct ways to create a pipe.\n\n\n-----Examples-----\nInput\n3 3\n...\n..#\n...\n\nOutput\n3\nInput\n4 2\n..\n..\n..\n..\n\nOutput\n2\n\nInput\n4 5\n#...#\n#...#\n###.#\n###.#\n\nOutput\n4\n\n\n-----Note-----\n\nIn the first sample there are 3 ways to make a pipe (the squares of the pipe are marked by characters ' * '): \n\n .*. .*. ...\n\n .*# **# **#\n\n .*. ... .*."} +{"id": "01746", "text": "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.\n\nLet'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.\n\nThe definition of a rooted tree can be found here.\n\n\n-----Input-----\n\nThe first line contains one integer n\u00a0\u2014 the number of vertices in the tree (3 \u2264 n \u2264 1 000). Each of the next n - 1 lines contains one integer p_{i} (1 \u2264 i \u2264 n - 1)\u00a0\u2014 the index of the parent of the i + 1-th vertex (1 \u2264 p_{i} \u2264 i).\n\nVertex 1 is the root. It's guaranteed that the root has at least 2 children.\n\n\n-----Output-----\n\nPrint \"Yes\" if the tree is a spruce and \"No\" otherwise.\n\n\n-----Examples-----\nInput\n4\n1\n1\n1\n\nOutput\nYes\n\nInput\n7\n1\n1\n1\n2\n2\n2\n\nOutput\nNo\n\nInput\n8\n1\n1\n1\n1\n3\n3\n3\n\nOutput\nYes\n\n\n\n-----Note-----\n\nThe first example:\n\n[Image]\n\nThe second example:\n\n$8$\n\nIt is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.\n\nThe third example:\n\n[Image]"} +{"id": "01747", "text": "The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values.\n\nFind any longest k-good segment.\n\nAs the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.\n\n\n-----Input-----\n\nThe first line contains two integers n, k (1 \u2264 k \u2264 n \u2264 5\u00b710^5) \u2014 the number of elements in a and the parameter k.\n\nThe second line contains n integers a_{i} (0 \u2264 a_{i} \u2264 10^6) \u2014 the elements of the array a.\n\n\n-----Output-----\n\nPrint two integers l, r (1 \u2264 l \u2264 r \u2264 n) \u2014 the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right.\n\n\n-----Examples-----\nInput\n5 5\n1 2 3 4 5\n\nOutput\n1 5\n\nInput\n9 3\n6 5 1 2 3 2 1 4 5\n\nOutput\n3 7\n\nInput\n3 1\n1 2 3\n\nOutput\n1 1"} +{"id": "01748", "text": "Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift\u00a0\u2014 a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume V_{i} and put it in her garden.\n\nEach day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is T_{i}, each pile will reduce its volume by T_{i}. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other. \n\nNote that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.\n\nYou are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day. \n\n\n-----Input-----\n\nThe first line contains a single integer N (1 \u2264 N \u2264 10^5)\u00a0\u2014 the number of days. \n\nThe second line contains N integers V_1, V_2, ..., V_{N} (0 \u2264 V_{i} \u2264 10^9), where V_{i} is the initial size of a snow pile made on the day i.\n\nThe third line contains N integers T_1, T_2, ..., T_{N} (0 \u2264 T_{i} \u2264 10^9), where T_{i} is the temperature on the day i.\n\n\n-----Output-----\n\nOutput a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.\n\n\n-----Examples-----\nInput\n3\n10 10 5\n5 7 2\n\nOutput\n5 12 4\n\nInput\n5\n30 25 20 15 10\n9 10 12 4 13\n\nOutput\n9 20 35 11 25\n\n\n\n-----Note-----\n\nIn the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day."} +{"id": "01749", "text": "Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a_1, a_2, ..., a_{n}.\n\nWhile Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b_1, b_2, ..., b_{n}. \n\nStepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).\n\nYour task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.\n\n\n-----Input-----\n\nThe first line contains three integers n, l, r (1 \u2264 n \u2264 10^5, 1 \u2264 l \u2264 r \u2264 n) \u2014 the number of Vasya's cubes and the positions told by Stepan.\n\nThe second line contains the sequence a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n) \u2014 the sequence of integers written on cubes in the Vasya's order.\n\nThe third line contains the sequence b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 n) \u2014 the sequence of integers written on cubes after Stepan rearranged their order.\n\nIt is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.\n\n\n-----Output-----\n\nPrint \"LIE\" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print \"TRUTH\" (without quotes).\n\n\n-----Examples-----\nInput\n5 2 4\n3 4 2 3 1\n3 2 3 4 1\n\nOutput\nTRUTH\n\nInput\n3 1 2\n1 2 3\n3 1 2\n\nOutput\nLIE\n\nInput\n4 2 4\n1 1 1 1\n1 1 1 1\n\nOutput\nTRUTH\n\n\n\n-----Note-----\n\nIn the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).\n\nIn the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.\n\nIn the third example for any values l and r there is a situation when Stepan said the truth."} +{"id": "01750", "text": "Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.\n\nThe park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.\n\nAndryusha wants to use as little different colors as possible. Help him to choose the colors!\n\n\n-----Input-----\n\nThe first line contains single integer n (3 \u2264 n \u2264 2\u00b710^5)\u00a0\u2014 the number of squares in the park.\n\nEach of the next (n - 1) lines contains two integers x and y (1 \u2264 x, y \u2264 n)\u00a0\u2014 the indices of two squares directly connected by a path.\n\nIt is guaranteed that any square is reachable from any other using the paths.\n\n\n-----Output-----\n\nIn the first line print single integer k\u00a0\u2014 the minimum number of colors Andryusha has to use.\n\nIn the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.\n\n\n-----Examples-----\nInput\n3\n2 3\n1 3\n\nOutput\n3\n1 3 2 \nInput\n5\n2 3\n5 3\n4 3\n1 3\n\nOutput\n5\n1 3 2 5 4 \nInput\n5\n2 1\n3 2\n4 3\n5 4\n\nOutput\n3\n1 2 3 1 2 \n\n\n-----Note-----\n\nIn the first sample the park consists of three squares: 1 \u2192 3 \u2192 2. Thus, the balloon colors have to be distinct. [Image] Illustration for the first sample. \n\nIn the second example there are following triples of consequently connected squares: 1 \u2192 3 \u2192 2 1 \u2192 3 \u2192 4 1 \u2192 3 \u2192 5 2 \u2192 3 \u2192 4 2 \u2192 3 \u2192 5 4 \u2192 3 \u2192 5 We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. [Image] Illustration for the second sample. \n\nIn the third example there are following triples: 1 \u2192 2 \u2192 3 2 \u2192 3 \u2192 4 3 \u2192 4 \u2192 5 We can see that one or two colors is not enough, but there is an answer that uses three colors only. [Image] Illustration for the third sample."} +{"id": "01751", "text": "A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).\n\nConsider a permutation $p$ of length $n$, we build a graph of size $n$ using it as follows: For every $1 \\leq i \\leq n$, find the largest $j$ such that $1 \\leq j < i$ and $p_j > p_i$, and add an undirected edge between node $i$ and node $j$ For every $1 \\leq i \\leq n$, find the smallest $j$ such that $i < j \\leq n$ and $p_j > p_i$, and add an undirected edge between node $i$ and node $j$ \n\nIn cases where no such $j$ exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.\n\nFor clarity, consider as an example $n = 4$, and $p = [3,1,4,2]$; here, the edges of the graph are $(1,3),(2,1),(2,3),(4,3)$.\n\nA permutation $p$ is cyclic if the graph built using $p$ has at least one simple cycle. \n\nGiven $n$, find the number of cyclic permutations of length $n$. Since the number may be very large, output it modulo $10^9+7$.\n\nPlease refer to the Notes section for the formal definition of a simple cycle\n\n\n-----Input-----\n\nThe first and only line contains a single integer $n$ ($3 \\le n \\le 10^6$).\n\n\n-----Output-----\n\nOutput a single integer $0 \\leq x < 10^9+7$, the number of cyclic permutations of length $n$ modulo $10^9+7$.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n16\nInput\n583291\n\nOutput\n135712853\n\n\n-----Note-----\n\nThere are $16$ cyclic permutations for $n = 4$. $[4,2,1,3]$ is one such permutation, having a cycle of length four: $4 \\rightarrow 3 \\rightarrow 2 \\rightarrow 1 \\rightarrow 4$.\n\nNodes $v_1$, $v_2$, $\\ldots$, $v_k$ form a simple cycle if the following conditions hold: $k \\geq 3$. $v_i \\neq v_j$ for any pair of indices $i$ and $j$. ($1 \\leq i < j \\leq k$) $v_i$ and $v_{i+1}$ share an edge for all $i$ ($1 \\leq i < k$), and $v_1$ and $v_k$ share an edge."} +{"id": "01752", "text": "Cowboy Vlad has a birthday today! There are $n$ children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standing next to each other, and it will be difficult for them to hold hands. Therefore, children want to stand in a circle so that the maximum difference between the growth of two neighboring children would be minimal possible.\n\nFormally, let's number children from $1$ to $n$ in a circle order, that is, for every $i$ child with number $i$ will stand next to the child with number $i+1$, also the child with number $1$ stands next to the child with number $n$. Then we will call the discomfort of the circle the maximum absolute difference of heights of the children, who stand next to each other.\n\nPlease help children to find out how they should reorder themselves, so that the resulting discomfort is smallest possible.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\leq n \\leq 100$)\u00a0\u2014 the number of the children who came to the cowboy Vlad's birthday.\n\nThe second line contains integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^9$) denoting heights of every child.\n\n\n-----Output-----\n\nPrint exactly $n$ integers\u00a0\u2014 heights of the children in the order in which they should stand in a circle. You can start printing a circle with any child.\n\nIf there are multiple possible answers, print any of them.\n\n\n-----Examples-----\nInput\n5\n2 1 1 3 2\n\nOutput\n1 2 3 2 1\n\nInput\n3\n30 10 20\n\nOutput\n10 20 30\n\n\n\n-----Note-----\n\nIn the first example, the discomfort of the circle is equal to $1$, since the corresponding absolute differences are $1$, $1$, $1$ and $0$. Note, that sequences $[2, 3, 2, 1, 1]$ and $[3, 2, 1, 1, 2]$ form the same circles and differ only by the selection of the starting point.\n\nIn the second example, the discomfort of the circle is equal to $20$, since the absolute difference of $10$ and $30$ is equal to $20$."} +{"id": "01753", "text": "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.\n\nIvan 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}$.\n\nLet'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.\n\nIvan 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.\n\nPlease help Ivan find such an arrangement.\n\n\n-----Input-----\n\nThe 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})$)\u00a0\u2014 number of colors and number of pairs of colors which harmonize with each other.\n\nIn 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.\n\n\n-----Output-----\n\nPrint $n$ blocks, $i$-th of them describes rooks of $i$-th color.\n\nIn the first line of block print one number $a_{i}$ ($1 \\le a_{i} \\le 5000$)\u00a0\u2014 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}$)\u00a0\u2014 coordinates of the next rook.\n\nAll rooks must be on different cells.\n\nTotal number of rooks must not exceed $5000$.\n\nIt is guaranteed that the solution exists.\n\n\n-----Examples-----\nInput\n3 2\n1 2\n2 3\n\nOutput\n2\n3 4\n1 4\n4\n1 2\n2 2\n2 4\n5 4\n1\n5 1\n\nInput\n3 3\n1 2\n2 3\n3 1\n\nOutput\n1\n1 1\n1\n1 2\n1\n1 3\n\nInput\n3 1\n1 3\n\nOutput\n1\n1 1\n1\n2 2\n1\n3 1\n\n\n\n-----Note-----\n\nRooks arrangements for all three examples (red is color $1$, green is color $2$ and blue is color $3$).\n\n$\\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.$\n\n$\\left. \\begin{array}{|l|l|l|l|} \\hline 2 & {} & {} & {} \\\\ \\hline 1 & {} & {} & {} \\\\ \\hline & {1} & {2} & {3} \\\\ \\hline \\end{array} \\right.$\n\n$\\left. \\begin{array}{|l|l|l|l|} \\hline 2 & {} & {} & {} \\\\ \\hline 1 & {} & {} & {} \\\\ \\hline & {1} & {2} & {3} \\\\ \\hline \\end{array} \\right.$"} +{"id": "01754", "text": "Everybody knows that the $m$-coder Tournament will happen soon. $m$ schools participate in the tournament, and only one student from each school participates.\n\nThere are a total of $n$ students in those schools. Before the tournament, all students put their names and the names of their schools into the Technogoblet of Fire. After that, Technogoblet selects the strongest student from each school to participate. \n\nArkady is a hacker who wants to have $k$ Chosen Ones selected by the Technogoblet. Unfortunately, not all of them are the strongest in their schools, but Arkady can make up some new school names and replace some names from Technogoblet with those. You can't use each made-up name more than once. In that case, Technogoblet would select the strongest student in those made-up schools too.\n\nYou know the power of each student and schools they study in. Calculate the minimal number of schools Arkady has to make up so that $k$ Chosen Ones would be selected by the Technogoblet.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $k$ ($1 \\le n \\le 100$, $1 \\le m, k \\le n$)\u00a0\u2014 the total number of students, the number of schools and the number of the Chosen Ones.\n\nThe second line contains $n$ different integers $p_1, p_2, \\ldots, p_n$ ($1 \\le p_i \\le n$), where $p_i$ denotes the power of $i$-th student. The bigger the power, the stronger the student.\n\nThe third line contains $n$ integers $s_1, s_2, \\ldots, s_n$ ($1 \\le s_i \\le m$), where $s_i$ denotes the school the $i$-th student goes to. At least one student studies in each of the schools. \n\nThe fourth line contains $k$ different integers $c_1, c_2, \\ldots, c_k$ ($1 \\le c_i \\le n$) \u00a0\u2014 the id's of the Chosen Ones.\n\n\n-----Output-----\n\nOutput a single integer \u00a0\u2014 the minimal number of schools to be made up by Arkady so that $k$ Chosen Ones would be selected by the Technogoblet.\n\n\n-----Examples-----\nInput\n7 3 1\n1 5 3 4 6 7 2\n1 3 1 2 1 2 3\n3\n\nOutput\n1\n\nInput\n8 4 4\n1 2 3 4 5 6 7 8\n4 3 2 1 4 3 2 1\n3 4 5 6\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example there's just a single Chosen One with id $3$. His power is equal to $3$, but in the same school $1$, there's a student with id $5$ and power $6$, and that means inaction would not lead to the latter being chosen. If we, however, make up a new school (let its id be $4$) for the Chosen One, Technogoblet would select students with ids $2$ (strongest in $3$), $5$ (strongest in $1$), $6$ (strongest in $2$) and $3$ (strongest in $4$).\n\nIn the second example, you can change the school of student $3$ to the made-up $5$ and the school of student $4$ to the made-up $6$. It will cause the Technogoblet to choose students $8$, $7$, $6$, $5$, $3$ and $4$."} +{"id": "01755", "text": "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$.\n\nIn 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$.\n\nCalculate 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.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n \\le 5 \\cdot 10^5$, $0 \\le k \\le n$) \u2014 the size of the array $a$ and the set $b$, respectively.\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \\le a_i \\le 10^9$).\n\nThen, 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.\n\n\n-----Output-----\n\nIf it is impossible to make the array $a$ increasing using the given operations, print $-1$.\n\nOtherwise, print one integer \u2014 the minimum number of operations you have to perform.\n\n\n-----Examples-----\nInput\n7 2\n1 2 1 1 3 5 1\n3 5\n\nOutput\n4\n\nInput\n3 3\n1 3 2\n1 2 3\n\nOutput\n-1\n\nInput\n5 0\n4 3 1 2 3\n\nOutput\n2\n\nInput\n10 3\n1 3 5 6 12 9 8 10 13 15\n2 4 9\n\nOutput\n3"} +{"id": "01756", "text": "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. \n\nYou 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.\n\nThey 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.\n\nThe 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.\n\nYou 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). \n\nPlease note that your trip should not necessarily begin and end in the same year.\n\n\n-----Input-----\n\nThe first line of input contains two integers $n$ and $x$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of months in the year and the number of days you can spend with your friend.\n\nThe 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$).\n\nIt is guaranteed that $1 \\le x \\le d_1 + d_2 + \\ldots + d_n$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life.\n\n\n-----Examples-----\nInput\n3 2\n1 3 1\n\nOutput\n5\nInput\n3 6\n3 3 3\n\nOutput\n12\nInput\n5 6\n4 2 3 1 3\n\nOutput\n15\n\n\n-----Note-----\n\nIn 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.\n\nIn 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.\n\nIn 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."} +{"id": "01757", "text": "Eleven wants to choose a new name for herself. As a bunch of geeks, her friends suggested an algorithm to choose a name for her. Eleven wants her name to have exactly n characters. [Image] \n\nHer friend suggested that her name should only consist of uppercase and lowercase letters 'O'. More precisely, they suggested that the i-th letter of her name should be 'O' (uppercase) if i is a member of Fibonacci sequence, and 'o' (lowercase) otherwise. The letters in the name are numbered from 1 to n. Fibonacci sequence is the sequence f where f_1 = 1, f_2 = 1, f_{n} = f_{n} - 2 + f_{n} - 1 (n > 2). \n\nAs her friends are too young to know what Fibonacci sequence is, they asked you to help Eleven determine her new name.\n\n\n-----Input-----\n\nThe first and only line of input contains an integer n (1 \u2264 n \u2264 1000).\n\n\n-----Output-----\n\nPrint Eleven's new name on the first and only line of output.\n\n\n-----Examples-----\nInput\n8\n\nOutput\nOOOoOooO\n\nInput\n15\n\nOutput\nOOOoOooOooooOoo"} +{"id": "01758", "text": "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.\n\nIn one operation, he can choose any subsequence of $s$ and rotate it clockwise once.\n\nFor 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}$.\n\nA 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.\n\nTo 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.\n\nDetermine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. \n\n\n-----Input-----\n\nThe first line contains a single integer $n$ $(1 \\le n \\le 10^6)$\u00a0\u2014 the length of the strings.\n\nThe second line contains the binary string $s$ of length $n$.\n\nThe third line contains the binary string $t$ of length $n$.\n\n\n-----Output-----\n\nIf it is impossible to convert $s$ to $t$ after any number of operations, print $-1$.\n\nOtherwise, print the minimum number of operations required.\n\n\n-----Examples-----\nInput\n6\n010000\n000001\n\nOutput\n1\nInput\n10\n1111100000\n0000011111\n\nOutput\n5\nInput\n8\n10101010\n01010101\n\nOutput\n1\nInput\n10\n1111100000\n1111100001\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first test, Naman can choose the subsequence corresponding to indices $\\{2, 6\\}$ and rotate it once to convert $s$ into $t$.\n\nIn 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.\n\nIn the last test, it is impossible to convert $s$ into $t$."} +{"id": "01759", "text": "A well-known art union called \"Kalevich is Alive!\" manufactures objects d'art (pictures). The union consists of n painters who decided to organize their work as follows.\n\nEach painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these n colors. Adding the j-th color to the i-th picture takes the j-th painter t_{ij} units of time.\n\nOrder is important everywhere, so the painters' work is ordered by the following rules: Each picture is first painted by the first painter, then by the second one, and so on. That is, after the j-th painter finishes working on the picture, it must go to the (j + 1)-th painter (if j < n); each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on; each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest; as soon as the j-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter. \n\nGiven that the painters start working at time 0, find for each picture the time when it is ready for sale.\n\n\n-----Input-----\n\nThe first line of the input contains integers m, n (1 \u2264 m \u2264 50000, 1 \u2264 n \u2264 5), where m is the number of pictures and n is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains n integers t_{i}1, t_{i}2, ..., t_{in} (1 \u2264 t_{ij} \u2264 1000), where t_{ij} is the time the j-th painter needs to work on the i-th picture.\n\n\n-----Output-----\n\nPrint the sequence of m integers r_1, r_2, ..., r_{m}, where r_{i} is the moment when the n-th painter stopped working on the i-th picture.\n\n\n-----Examples-----\nInput\n5 1\n1\n2\n3\n4\n5\n\nOutput\n1 3 6 10 15 \nInput\n4 2\n2 5\n3 1\n5 3\n10 1\n\nOutput\n7 8 13 21"} +{"id": "01760", "text": "The academic year has just begun, but lessons and olympiads have already occupied all the free time. It is not a surprise that today Olga fell asleep on the Literature. She had a dream in which she was on a stairs. \n\nThe stairs consists of n steps. The steps are numbered from bottom to top, it means that the lowest step has number 1, and the highest step has number n. Above each of them there is a pointer with the direction (up or down) Olga should move from this step. As soon as Olga goes to the next step, the direction of the pointer (above the step she leaves) changes. It means that the direction \"up\" changes to \"down\", the direction \"down\" \u00a0\u2014\u00a0 to the direction \"up\".\n\nOlga always moves to the next step in the direction which is shown on the pointer above the step. \n\nIf Olga moves beyond the stairs, she will fall and wake up. Moving beyond the stairs is a moving down from the first step or moving up from the last one (it means the n-th) step. \n\nIn one second Olga moves one step up or down according to the direction of the pointer which is located above the step on which Olga had been at the beginning of the second. \n\nFor each step find the duration of the dream if Olga was at this step at the beginning of the dream.\n\nOlga's fall also takes one second, so if she was on the first step and went down, she would wake up in the next second.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 10^6)\u00a0\u2014 the number of steps on the stairs.\n\nThe second line contains a string s with the length n\u00a0\u2014 it denotes the initial direction of pointers on the stairs. The i-th character of string s denotes the direction of the pointer above i-th step, and is either 'U' (it means that this pointer is directed up), or 'D' (it means this pointed is directed down).\n\nThe pointers are given in order from bottom to top.\n\n\n-----Output-----\n\nPrint n numbers, the i-th of which is equal either to the duration of Olga's dream or to - 1 if Olga never goes beyond the stairs, if in the beginning of sleep she was on the i-th step.\n\n\n-----Examples-----\nInput\n3\nUUD\n\nOutput\n5 6 3 \nInput\n10\nUUDUDUUDDU\n\nOutput\n5 12 23 34 36 27 18 11 6 1"} +{"id": "01761", "text": "Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.\n\nDima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the \"less\" characters (<) and the digit three (3). After applying the code, a test message looks like that: <3word_1<3word_2<3 ... word_{n}<3.\n\nEncoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs \"more\" and \"less\" into any places of the message.\n\nInna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 10^5. \n\nThe last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 10^5. A text message can contain only small English letters, digits and signs more and less.\n\n\n-----Output-----\n\nIn a single line, print \"yes\" (without the quotes), if Dima decoded the text message correctly, and \"no\" (without the quotes) otherwise.\n\n\n-----Examples-----\nInput\n3\ni\nlove\nyou\n<3i<3love<23you<3\n\nOutput\nyes\n\nInput\n7\ni\nam\nnot\nmain\nin\nthe\nfamily\n<3i<>3am<3the<3<3family<3\n\nOutput\nno\n\n\n\n-----Note-----\n\nPlease note that Dima got a good old kick in the pants for the second sample from the statement."} +{"id": "01762", "text": "A social network for dogs called DH (DogHouse) has k special servers to recompress uploaded videos of cute cats. After each video is uploaded, it should be recompressed on one (any) of the servers, and only after that it can be saved in the social network.\n\nWe know that each server takes one second to recompress a one minute fragment. Thus, any server takes m seconds to recompress a m minute video.\n\nWe know the time when each of the n videos were uploaded to the network (in seconds starting from the moment all servers started working). All videos appear at different moments of time and they are recompressed in the order they appear. If some video appeared at time s, then its recompressing can start at that very moment, immediately. Some videos can await recompressing when all the servers are busy. In this case, as soon as a server is available, it immediately starts recompressing another video. The videos that await recompressing go in a queue. If by the moment the videos started being recompressed some servers are available, then any of them starts recompressing the video.\n\nFor each video find the moment it stops being recompressed.\n\n\n-----Input-----\n\nThe first line of the input contains integers n and k (1 \u2264 n, k \u2264 5\u00b710^5) \u2014 the number of videos and servers, respectively.\n\nNext n lines contain the descriptions of the videos as pairs of integers s_{i}, m_{i} (1 \u2264 s_{i}, m_{i} \u2264 10^9), where s_{i} is the time in seconds when the i-th video appeared and m_{i} is its duration in minutes. It is guaranteed that all the s_{i}'s are distinct and the videos are given in the chronological order of upload, that is in the order of increasing s_{i}.\n\n\n-----Output-----\n\nPrint n numbers e_1, e_2, ..., e_{n}, where e_{i} is the time in seconds after the servers start working, when the i-th video will be recompressed.\n\n\n-----Examples-----\nInput\n3 2\n1 5\n2 5\n3 5\n\nOutput\n6\n7\n11\n\nInput\n6 1\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000\n5 1000000000\n6 3\n\nOutput\n1000000001\n2000000001\n3000000001\n4000000001\n5000000001\n5000000004"} +{"id": "01763", "text": "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.\n\nYou 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$.\n\nYou cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $0$.\n\nWhat is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?\n\n\n-----Input-----\n\nThe 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}$)\u00a0\u2014 the number of pillars and the costs of operations.\n\nThe second line contains $N$ integers $h_{i}$ ($0 \\le h_{i} \\le 10^{9}$)\u00a0\u2014 initial heights of pillars.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimal cost of restoration.\n\n\n-----Examples-----\nInput\n3 1 100 100\n1 3 8\n\nOutput\n12\n\nInput\n3 100 1 100\n1 3 8\n\nOutput\n9\n\nInput\n3 100 100 1\n1 3 8\n\nOutput\n4\n\nInput\n5 1 2 4\n5 5 3 6 5\n\nOutput\n4\n\nInput\n5 1 2 2\n5 5 3 6 5\n\nOutput\n3"} +{"id": "01764", "text": "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.\n\nTo 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.\n\nPetya 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?\n\n\n-----Input-----\n\nThe first line contains a single integer n\u00a0\u2014 the length of the sequence of games (1 \u2264 n \u2264 10^5).\n\nThe 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.\n\nIt is not guaranteed that at least one option for numbers s and t corresponds to the given record.\n\n\n-----Output-----\n\nIn the first line print a single number k\u00a0\u2014 the number of options for numbers s and t.\n\nIn each of the following k lines print two integers s_{i} and t_{i}\u00a0\u2014 the option for numbers s and t. Print the options in the order of increasing s_{i}, and for equal s_{i}\u00a0\u2014 in the order of increasing t_{i}.\n\n\n-----Examples-----\nInput\n5\n1 2 1 2 1\n\nOutput\n2\n1 3\n3 1\n\nInput\n4\n1 1 1 1\n\nOutput\n3\n1 4\n2 2\n4 1\n\nInput\n4\n1 2 1 2\n\nOutput\n0\n\nInput\n8\n2 1 2 1 1 1 1 1\n\nOutput\n3\n1 6\n2 3\n6 1"} +{"id": "01765", "text": "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. \n\nThe 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. \n\nTell 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.\n\nHere 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 \u2014 by \"and\".\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_1 < a_2 < ... < a_{n} \u2264 10^9).\n\n\n-----Output-----\n\nIn 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} \u2014 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.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n\nOutput\n2\n4 5\n\nInput\n3\n1 2 4\n\nOutput\n1\n4"} +{"id": "01766", "text": "Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.\n\nSereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.\n\nInna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 1000) \u2014 the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000.\n\n\n-----Output-----\n\nOn a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.\n\n\n-----Examples-----\nInput\n4\n4 1 2 10\n\nOutput\n12 5\n\nInput\n7\n1 2 3 4 5 6 7\n\nOutput\n16 12\n\n\n\n-----Note-----\n\nIn the first sample Sereja will take cards with numbers 10 and 2, so Sereja's sum is 12. Dima will take cards with numbers 4 and 1, so Dima's sum is 5."} +{"id": "01767", "text": "Blake is a CEO of a large company called \"Blake Technologies\". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.\n\nWe define function f(x, l, r) as a bitwise OR of integers x_{l}, x_{l} + 1, ..., x_{r}, where x_{i} is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 \u2264 l \u2264 r \u2264 n. [Image] \n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the length of the arrays.\n\nThe second line contains n integers a_{i} (0 \u2264 a_{i} \u2264 10^9).\n\nThe third line contains n integers b_{i} (0 \u2264 b_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 \u2264 l \u2264 r \u2264 n.\n\n\n-----Examples-----\nInput\n5\n1 2 4 3 2\n2 3 3 12 1\n\nOutput\n22\nInput\n10\n13 2 7 11 8 4 9 8 5 1\n5 7 18 9 2 3 0 11 8 6\n\nOutput\n46\n\n\n-----Note-----\n\nBitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.\n\nIn the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.\n\nIn the second sample, the maximum value is obtained for l = 1 and r = 9."} +{"id": "01768", "text": "Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!\n\nStill unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c \u2014 Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.\n\nFor instance, let's say the garland is represented by \"kooomo\", and Brother Koyomi's favourite colour is \"o\". Among all subsegments containing pieces of \"o\" only, \"ooo\" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.\n\nBut problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.\n\n\n-----Input-----\n\nThe first line of input contains a positive integer n (1 \u2264 n \u2264 1 500) \u2014 the length of the garland.\n\nThe second line contains n lowercase English letters s_1s_2... s_{n} as a string \u2014 the initial colours of paper pieces on the garland.\n\nThe third line contains a positive integer q (1 \u2264 q \u2264 200 000) \u2014 the number of plans Nadeko has.\n\nThe next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 \u2264 m_{i} \u2264 n) \u2014 the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} \u2014 Koyomi's possible favourite colour.\n\n\n-----Output-----\n\nOutput q lines: for each work plan, output one line containing an integer \u2014 the largest Koyomity achievable after repainting the garland according to it.\n\n\n-----Examples-----\nInput\n6\nkoyomi\n3\n1 o\n4 o\n4 m\n\nOutput\n3\n6\n5\n\nInput\n15\nyamatonadeshiko\n10\n1 a\n2 a\n3 a\n4 a\n5 a\n1 b\n2 b\n3 b\n4 b\n5 b\n\nOutput\n3\n4\n5\n7\n8\n1\n2\n3\n4\n5\n\nInput\n10\naaaaaaaaaa\n2\n10 b\n10 z\n\nOutput\n10\n10\n\n\n\n-----Note-----\n\nIn the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the \"y\" piece to become \"o\" results in \"kooomi\", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and \"oooooo\" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and \"mmmmmi\" and \"kmmmmm\" both result in a Koyomity of 5."} +{"id": "01769", "text": "Hiking club \"Up the hill\" just returned from a walk. Now they are trying to remember which hills they've just walked through.\n\nIt 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.\n\nThey 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.\n\nHelp them by suggesting some possible stop heights satisfying numbers from the travel journal.\n\n\n-----Input-----\n\nIn 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\u00a0\u2014 the number of days of walking down the hill (A + B + 1 = N, 1 \u2264 N \u2264 100 000).\n\n\n-----Output-----\n\nOutput N space-separated distinct integers from 1 to N inclusive, denoting possible heights of the stops in order of visiting.\n\n\n-----Examples-----\nInput\n0\n1\n\nOutput\n2 1 \n\nInput\n2\n1\nOutput\n1 3 4 2"} +{"id": "01770", "text": "Vasya is reading a e-book. The file of the book consists of $n$ pages, numbered from $1$ to $n$. The screen is currently displaying the contents of page $x$, and Vasya wants to read the page $y$. There are two buttons on the book which allow Vasya to scroll $d$ pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of $10$ pages, and $d = 3$, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page \u2014 to the first or to the fifth; from the sixth page \u2014 to the third or to the ninth; from the eighth \u2014 to the fifth or to the tenth.\n\nHelp Vasya to calculate the minimum number of times he needs to press a button to move to page $y$.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10^3$) \u2014 the number of testcases.\n\nEach testcase is denoted by a line containing four integers $n$, $x$, $y$, $d$ ($1\\le n, d \\le 10^9$, $1 \\le x, y \\le n$) \u2014 the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.\n\n\n-----Output-----\n\nPrint one line for each test.\n\nIf Vasya can move from page $x$ to page $y$, print the minimum number of times he needs to press a button to do it. Otherwise print $-1$.\n\n\n-----Example-----\nInput\n3\n10 4 5 2\n5 1 3 4\n20 4 19 3\n\nOutput\n4\n-1\n5\n\n\n\n-----Note-----\n\nIn the first test case the optimal sequence is: $4 \\rightarrow 2 \\rightarrow 1 \\rightarrow 3 \\rightarrow 5$.\n\nIn the second test case it is possible to get to pages $1$ and $5$.\n\nIn the third test case the optimal sequence is: $4 \\rightarrow 7 \\rightarrow 10 \\rightarrow 13 \\rightarrow 16 \\rightarrow 19$."} +{"id": "01771", "text": "Gathering darkness shrouds the woods and the world. The moon sheds its light on the boat and the river.\n\n\"To curtain off the moonlight should be hardly possible; the shades present its mellow beauty and restful nature.\" Intonates Mino.\n\n\"See? The clouds are coming.\" Kanno gazes into the distance.\n\n\"That can't be better,\" Mino turns to Kanno. \n\nThe sky can be seen as a one-dimensional axis. The moon is at the origin whose coordinate is $0$.\n\nThere 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$.\n\nFurthermore, 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$.\n\nWith 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.\n\nYou 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.\n\n\n-----Input-----\n\nThe 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$)\u00a0\u2014 the number of clouds, the length of each cloud and the maximum wind speed, respectively.\n\nThe $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\\}$)\u00a0\u2014 the initial position and the velocity of the $i$-th cloud, respectively.\n\nThe input guarantees that for all $1 \\leq i \\lt j \\leq n$, $\\lvert x_i - x_j \\rvert \\geq l$.\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 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$.\n\n\n-----Examples-----\nInput\n5 1 2\n-2 1\n2 1\n3 -1\n5 -1\n7 -1\n\nOutput\n4\n\nInput\n4 10 1\n-20 1\n-10 -1\n0 1\n10 -1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, the initial positions and velocities of clouds are illustrated below.\n\n [Image] \n\nThe 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$. \n\nBelow 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.\n\n [Image] \n\nIn the second example, the only pair is $(1, 4)$, covering the moon at time $15$ with $w = 0$.\n\nNote that all the times and wind velocities given above are just examples among infinitely many choices."} +{"id": "01772", "text": "A flower shop has got n bouquets, and the i-th bouquet consists of a_{i} flowers. Vasya, the manager of the shop, decided to make large bouquets from these bouquets. \n\nVasya thinks that a bouquet is large if it is made of two or more initial bouquets, and there is a constraint: the total number of flowers in a large bouquet should be odd. Each of the initial bouquets can be a part of at most one large bouquet. If an initial bouquet becomes a part of a large bouquet, all its flowers are included in the large bouquet.\n\nDetermine the maximum possible number of large bouquets Vasya can make. \n\n\n-----Input-----\n\nThe first line contains a single positive integer n (1 \u2264 n \u2264 10^5) \u2014 the number of initial bouquets.\n\nThe second line contains a sequence of integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^6) \u2014 the number of flowers in each of the initial bouquets.\n\n\n-----Output-----\n\nPrint the maximum number of large bouquets Vasya can make. \n\n\n-----Examples-----\nInput\n5\n2 3 4 2 7\n\nOutput\n2\n\nInput\n6\n2 2 6 8 6 12\n\nOutput\n0\n\nInput\n3\n11 4 10\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example Vasya can make 2 large bouquets. For example, the first bouquet can contain the first and the fifth initial bouquets (the total number of flowers is then equal to 9), and the second bouquet can consist of the second and the third initial bouquets (the total number of flowers is then equal to 7). The fourth initial bouquet is unused in this scheme. \n\nIn the second example it is not possible to form a single bouquet with odd number of flowers.\n\nIn the third example Vasya can make one large bouquet. For example, he can make it using all three initial bouquets. The size of the large bouquet is then equal to 11 + 4 + 10 = 25."} +{"id": "01773", "text": "Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.\n\nLala Land has exactly n apple trees. Tree number i is located in a position x_{i} and has a_{i} apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in x = 0 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing.\n\nWhat is the maximum number of apples he can collect?\n\n\n-----Input-----\n\nThe first line contains one number n (1 \u2264 n \u2264 100), the number of apple trees in Lala Land.\n\nThe following n lines contains two integers each x_{i}, a_{i} ( - 10^5 \u2264 x_{i} \u2264 10^5, x_{i} \u2260 0, 1 \u2264 a_{i} \u2264 10^5), representing the position of the i-th tree and number of apples on it.\n\nIt's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.\n\n\n-----Output-----\n\nOutput the maximum number of apples Amr can collect.\n\n\n-----Examples-----\nInput\n2\n-1 5\n1 5\n\nOutput\n10\nInput\n3\n-2 2\n1 4\n-1 3\n\nOutput\n9\nInput\n3\n1 9\n3 5\n7 10\n\nOutput\n9\n\n\n-----Note-----\n\nIn the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.\n\nIn the second sample test the optimal solution is to go left to x = - 1, collect apples from there, then the direction will be reversed, Amr has to go to x = 1, collect apples from there, then the direction will be reversed and Amr goes to the final tree x = - 2.\n\nIn the third sample test the optimal solution is to go right to x = 1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left."} +{"id": "01774", "text": "The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset \u2014 a language called HQ...\n\n\n-----Input-----\n\nThe only line of the input is a string between 1 and 10^6 characters long.\n\n\n-----Output-----\n\nOutput \"Yes\" or \"No\".\n\n\n-----Examples-----\nInput\nHHHH\n\nOutput\nYes\n\nInput\nHQHQH\n\nOutput\nNo\n\nInput\nHHQHHQH\n\nOutput\nNo\n\nInput\nHHQQHHQQHH\n\nOutput\nYes\n\n\n\n-----Note-----\n\nThe rest of the problem statement was destroyed by a stray raccoon. We are terribly sorry for the inconvenience."} +{"id": "01775", "text": "An army of n droids is lined up in one row. Each droid is described by m integers a_1, a_2, ..., a_{m}, where a_{i} is the number of details of the i-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has m weapons, the i-th weapon can affect all the droids in the army by destroying one detail of the i-th type (if the droid doesn't have details of this type, nothing happens to it). \n\nA droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most k shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?\n\n\n-----Input-----\n\nThe first line contains three integers n, m, k (1 \u2264 n \u2264 10^5, 1 \u2264 m \u2264 5, 0 \u2264 k \u2264 10^9) \u2014 the number of droids, the number of detail types and the number of available shots, respectively.\n\nNext n lines follow describing the droids. Each line contains m integers a_1, a_2, ..., a_{m} (0 \u2264 a_{i} \u2264 10^8), where a_{i} is the number of details of the i-th type for the respective robot.\n\n\n-----Output-----\n\nPrint m space-separated integers, where the i-th number is the number of shots from the weapon of the i-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length.\n\nIf there are multiple optimal solutions, print any of them. \n\nIt is not necessary to make exactly k shots, the number of shots can be less.\n\n\n-----Examples-----\nInput\n5 2 4\n4 0\n1 2\n2 1\n0 2\n1 3\n\nOutput\n2 2\n\nInput\n3 2 4\n1 2\n1 3\n2 2\n\nOutput\n1 3\n\n\n\n-----Note-----\n\nIn the first test the second, third and fourth droids will be destroyed. \n\nIn the second test the first and second droids will be destroyed."} +{"id": "01776", "text": "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.\n\nAn 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.\n\nOn 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:\n\nGiven 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.\n\n\n-----Input-----\n\nThe first line contains a string E (1 \u2264 |E| \u2264 10^4) \u2014 a valid Ahmes arithmetic expression. All operators are erased and replaced with '?'.\n\nThe second line contains two space-separated integers P and M (0 \u2264 min(P, M) \u2264 100) \u2014 the number of plus and minus operators, respectively. \n\nIt is guaranteed that P + M = the number of erased operators.\n\n\n-----Output-----\n\nPrint one line containing the answer to the problem.\n\n\n-----Examples-----\nInput\n(1?1)\n1 0\n\nOutput\n2\n\nInput\n(2?(1?2))\n1 1\n\nOutput\n1\n\nInput\n((1?(5?7))?((6?2)?7))\n3 2\n\nOutput\n18\n\nInput\n((1?(5?7))?((6?2)?7))\n2 3\n\nOutput\n16\n\n\n\n-----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."} +{"id": "01777", "text": "One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.\n\nA bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters \"+\" and \"1\" into this sequence. For example, the sequences \"(())()\", \"()\" and \"(()(()))\" are correct, while the bracket sequences \")(\", \"(()\" and \"(()))(\" are not correct.\n\nYuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.\n\nThis problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\leq n \\leq 10^5$) \u2014 the number of bracket sequences.\n\nEach of the following $n$ lines contains one bracket sequence \u2014 a non-empty string which consists only of characters \"(\" and \")\".\n\nThe sum of lengths of all bracket sequences in the input is at most $5 \\cdot 10^5$.\n\nNote that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of pairs which can be made, adhering to the conditions in the statement.\n\n\n-----Examples-----\nInput\n7\n)())\n)\n((\n((\n(\n)\n)\n\nOutput\n2\n\nInput\n4\n(\n((\n(((\n(())\n\nOutput\n0\n\nInput\n2\n(())\n()\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, it's optimal to construct two pairs: \"(( \u00a0\u00a0\u00a0 )())\" and \"( \u00a0\u00a0\u00a0 )\"."} +{"id": "01778", "text": "Two players A and B have a list of $n$ integers each. They both want to maximize the subtraction between their score and their opponent's score. \n\nIn one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an element from his opponent's list (assuming his opponent's list is not empty).\n\nNote, that in case there are equal elements in the list only one of them will be affected in the operations above. For example, if there are elements $\\{1, 2, 2, 3\\}$ in a list and you decided to choose $2$ for the next turn, only a single instance of $2$ will be deleted (and added to the score, if necessary). \n\nThe player A starts the game and the game stops when both lists are empty. Find the difference between A's score and B's score at the end of the game, if both of the players are playing optimally.\n\nOptimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. In this problem, it means that each player, each time makes a move, which maximizes the final difference between his score and his opponent's score, knowing that the opponent is doing the same.\n\n\n-----Input-----\n\nThe first line of input contains an integer $n$ ($1 \\le n \\le 100\\,000$)\u00a0\u2014 the sizes of the list.\n\nThe second line contains $n$ integers $a_i$ ($1 \\le a_i \\le 10^6$), describing the list of the player A, who starts the game.\n\nThe third line contains $n$ integers $b_i$ ($1 \\le b_i \\le 10^6$), describing the list of the player B.\n\n\n-----Output-----\n\nOutput the difference between A's score and B's score ($A-B$) if both of them are playing optimally.\n\n\n-----Examples-----\nInput\n2\n1 4\n5 1\n\nOutput\n0\nInput\n3\n100 100 100\n100 100 100\n\nOutput\n0\nInput\n2\n2 1\n5 6\n\nOutput\n-3\n\n\n-----Note-----\n\nIn the first example, the game could have gone as follows: A removes $5$ from B's list. B removes $4$ from A's list. A takes his $1$. B takes his $1$. \n\nHence, A's score is $1$, B's score is $1$ and difference is $0$.\n\nThere is also another optimal way of playing: A removes $5$ from B's list. B removes $4$ from A's list. A removes $1$ from B's list. B removes $1$ from A's list. \n\nThe difference in the scores is still $0$.\n\nIn the second example, irrespective of the moves the players make, they will end up with the same number of numbers added to their score, so the difference will be $0$."} +{"id": "01779", "text": "There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet.\n\nYou are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order. \n\nYou are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout.\n\nSince all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters.\n\n\n-----Input-----\n\nThe first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout.\n\nThe second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout.\n\nThe third line contains a non-empty string s consisting of lowercase and uppercase English letters and digits. This is the text typed in the first layout. The length of s does not exceed 1000.\n\n\n-----Output-----\n\nPrint the text if the same keys were pressed in the second layout.\n\n\n-----Examples-----\nInput\nqwertyuiopasdfghjklzxcvbnm\nveamhjsgqocnrbfxdtwkylupzi\nTwccpQZAvb2017\n\nOutput\nHelloVKCup2017\n\nInput\nmnbvcxzlkjhgfdsapoiuytrewq\nasdfghjklqwertyuiopzxcvbnm\n7abaCABAABAcaba7\n\nOutput\n7uduGUDUUDUgudu7"} +{"id": "01780", "text": "Eugeny has array a = a_1, a_2, ..., a_{n}, consisting of n integers. Each integer a_{i} equals to -1, or to 1. Also, he has m queries: Query number i is given as a pair of integers l_{i}, r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n). The response to the query will be integer 1, if the elements of array a can be rearranged so as the sum a_{l}_{i} + a_{l}_{i} + 1 + ... + a_{r}_{i} = 0, otherwise the response to the query will be integer 0. \n\nHelp Eugeny, answer all his queries.\n\n\n-----Input-----\n\nThe first line contains integers n and m (1 \u2264 n, m \u2264 2\u00b710^5). The second line contains n integers a_1, a_2, ..., a_{n} (a_{i} = -1, 1). Next m lines contain Eugene's queries. The i-th line contains integers l_{i}, r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n).\n\n\n-----Output-----\n\nPrint m integers \u2014 the responses to Eugene's queries in the order they occur in the input.\n\n\n-----Examples-----\nInput\n2 3\n1 -1\n1 1\n1 2\n2 2\n\nOutput\n0\n1\n0\n\nInput\n5 5\n-1 1 1 1 -1\n1 1\n2 3\n3 5\n2 5\n1 5\n\nOutput\n0\n1\n0\n1\n0"} +{"id": "01781", "text": "\u0412 \u0441\u0430\u043c\u043e\u043b\u0451\u0442\u0435 \u0435\u0441\u0442\u044c n \u0440\u044f\u0434\u043e\u0432 \u043c\u0435\u0441\u0442. \u0415\u0441\u043b\u0438 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0440\u044f\u0434\u044b \u0441\u0432\u0435\u0440\u0445\u0443, \u0442\u043e \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u044f\u0434\u0443 \u0435\u0441\u0442\u044c 3 \u043c\u0435\u0441\u0442\u0430 \u0441\u043b\u0435\u0432\u0430, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0445\u043e\u0434 \u043c\u0435\u0436\u0434\u0443 \u0440\u044f\u0434\u0430\u043c\u0438, \u0437\u0430\u0442\u0435\u043c 4 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430, \u0437\u0430\u0442\u0435\u043c \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0445\u043e\u0434 \u043c\u0435\u0436\u0434\u0443 \u0440\u044f\u0434\u0430\u043c\u0438, \u0430 \u0437\u0430\u0442\u0435\u043c \u0435\u0449\u0451 3 \u043c\u0435\u0441\u0442\u0430 \u0441\u043f\u0440\u0430\u0432\u0430.\n\n\u0418\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u0435\u0441\u0442\u0430 \u0443\u0436\u0435 \u0437\u0430\u043d\u044f\u0442\u044b \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u0430\u043c\u0438. \u0412\u0441\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0432\u0438\u0434\u0430 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432 \u2014 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0435 (\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u043b\u0435\u0442\u0430\u044e\u0442) \u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0435. \n\n\u041f\u0435\u0440\u0435\u0434 \u0432\u0430\u043c\u0438 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0430\u0441\u0441\u0430\u0434\u0438\u0442\u044c \u0435\u0449\u0451 k \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0441\u043e\u0441\u0435\u0434\u0435\u0439 \u0443 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432 \u0431\u044b\u043b\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c. \u0414\u0432\u0430 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u0430 \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f \u0441\u043e\u0441\u0435\u0434\u044f\u043c\u0438, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0441\u0438\u0434\u044f\u0442 \u0432 \u043e\u0434\u043d\u043e\u043c \u0440\u044f\u0434\u0443 \u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u043d\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u0435\u0441\u0442 \u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u043c\u0435\u0436\u0434\u0443 \u0440\u044f\u0434\u0430\u043c\u0438. \u0415\u0441\u043b\u0438 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u043c \u0434\u043b\u044f \u0434\u0432\u0443\u0445 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432, \u0442\u043e \u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0432 \u0441\u0443\u043c\u043c\u0435 \u0441\u043e\u0441\u0435\u0434\u0435\u0439 \u0434\u0432\u0430\u0436\u0434\u044b.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 n \u0438 k (1 \u2264 n \u2264 100, 1 \u2264 k \u2264 10\u00b7n) \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u044f\u0434\u043e\u0432 \u043c\u0435\u0441\u0442 \u0432 \u0441\u0430\u043c\u043e\u043b\u0451\u0442\u0435 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u0430\u0434\u0438\u0442\u044c.\n\n\u0414\u0430\u043b\u0435\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0440\u044f\u0434\u043e\u0432 \u043c\u0435\u0441\u0442 \u0441\u0430\u043c\u043e\u043b\u0451\u0442\u0430 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u044f\u0434\u0443 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435. \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0440\u0430\u0432\u0435\u043d '-', \u0442\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0445\u043e\u0434 \u043c\u0435\u0436\u0434\u0443 \u0440\u044f\u0434\u0430\u043c\u0438. \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0440\u0430\u0432\u0435\u043d '.', \u0442\u043e \u044d\u0442\u043e \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e. \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0440\u0430\u0432\u0435\u043d 'S', \u0442\u043e \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043c\u0435\u0441\u0442\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0438\u0434\u0435\u0442\u044c \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0439 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440. \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0440\u0430\u0432\u0435\u043d 'P', \u0442\u043e \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043c\u0435\u0441\u0442\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0438\u0434\u0435\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440. \n\n\u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0445 \u043c\u0435\u0441\u0442 \u043d\u0435 \u043c\u0435\u043d\u044c\u0448\u0435 k. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432\u0441\u0435 \u0440\u044f\u0434\u044b \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c\u0443 \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u0438 \u0444\u043e\u0440\u043c\u0430\u0442\u0443.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0441\u043e\u0441\u0435\u0434\u0435\u0439 \u0443 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432.\n\n\u0414\u0430\u043b\u0435\u0435 \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043f\u043b\u0430\u043d \u0440\u0430\u0441\u0441\u0430\u0434\u043a\u0438 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0441\u0435\u0434\u0435\u0439 \u0443 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432, \u0432 \u0442\u043e\u043c \u0436\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0435, \u0447\u0442\u043e \u0438 \u0432\u043e \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u0432 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0430\u0434\u0438\u0442\u044c \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 k \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432, \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u0447\u043d\u0443\u044e \u0431\u0443\u043a\u0432\u0443 'x' \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 '.'. \n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1 2\nSP.-SS.S-S.S\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n5\nSPx-SSxS-S.S\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n4 9\nPP.-PPPS-S.S\nPSP-PPSP-.S.\n.S.-S..P-SS.\nP.S-P.PP-PSP\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n15\nPPx-PPPS-S.S\nPSP-PPSP-xSx\nxSx-SxxP-SSx\nP.S-PxPP-PSP\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0430\u0434\u0438\u0442\u044c \u0435\u0449\u0451 \u0434\u0432\u0443\u0445 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432. \u0414\u043b\u044f \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043e\u0441\u0435\u0434\u0435\u0439 \u0443 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432, \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0430\u0434\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445 \u043d\u0430 \u0442\u0440\u0435\u0442\u044c\u0435 \u0441\u043b\u0435\u0432\u0430 \u043c\u0435\u0441\u0442\u043e, \u0430 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0430 \u043b\u044e\u0431\u043e\u0435 \u0438\u0437 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u0434\u0432\u0443\u0445 \u043c\u0435\u0441\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0432\u044b\u0431\u043e\u0440\u0430 \u043c\u0435\u0441\u0442\u0430 \u043e\u043d \u0441\u0442\u0430\u043d\u0435\u0442 \u0441\u043e\u0441\u0435\u0434\u043e\u043c \u0434\u0432\u0443\u0445 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432. \n\n\u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e, \u0443 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u043e\u0433\u043e \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0438\u0434\u0438\u0442 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u043b\u0435\u0432\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u043e\u0441\u0435\u0434. \u0422\u0430\u043a\u0436\u0435 \u043d\u0430 \u0447\u0435\u0442\u0432\u0451\u0440\u0442\u043e\u043c \u0438 \u043f\u044f\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u0445 \u0441\u043b\u0435\u0432\u0430 \u0441\u0438\u0434\u044f\u0442 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0435 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u044b, \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u0441\u043e\u0441\u0435\u0434\u044f\u043c\u0438 \u0434\u0440\u0443\u0433 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0430 (\u0447\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043a \u0441\u0443\u043c\u043c\u0435 2).\n\n\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0441\u0430\u0434\u043a\u0438 \u0435\u0449\u0451 \u0434\u0432\u0443\u0445 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432, \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0441\u0435\u0434\u0435\u0439 \u0443 \u0441\u0442\u0430\u0442\u0443\u0441\u043d\u044b\u0445 \u043f\u0430\u0441\u0441\u0430\u0436\u0438\u0440\u043e\u0432 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u043e \u043f\u044f\u0442\u0438."} +{"id": "01782", "text": "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.\n\nWe'll say that the secret is safe if the following conditions are hold: for any two indexes i, j (1 \u2264 i < j \u2264 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}| \u2265 3 should hold). \n\nLet 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 \u2264 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.\n\nYour 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.\n\n\n-----Input-----\n\nThe input consists of a single line which contains two integers n and k (2 \u2264 k \u2264 n \u2264 10^6) \u2014 the number of words in the secret and the number of the Keepers. The numbers are separated by a single space.\n\n\n-----Output-----\n\nIf 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.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n11 3\n\nOutput\n3 1 2 1 1 2 3 2 2 3 1\n\nInput\n5 2\n\nOutput\n-1"} +{"id": "01783", "text": "It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!\n\nWhen Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last n days. So now he has a sequence a_1, a_2, ..., a_{n}, where a_{i} is the sleep time on the i-th day.\n\nThe number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider k consecutive days as a week. So there will be n - k + 1 weeks to take into consideration. For example, if k = 2, n = 3 and a = [3, 4, 7], then the result is $\\frac{(3 + 4) +(4 + 7)}{2} = 9$.\n\nYou should write a program which will calculate average sleep times of Polycarp over all weeks.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and k (1 \u2264 k \u2264 n \u2264 2\u00b710^5).\n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5).\n\n\n-----Output-----\n\nOutput average sleeping time over all weeks. \n\nThe answer is considered to be correct if its absolute or relative error does not exceed 10^{ - 6}. In particular, it is enough to output real number with at least 6 digits after the decimal point.\n\n\n-----Examples-----\nInput\n3 2\n3 4 7\n\nOutput\n9.0000000000\n\nInput\n1 1\n10\n\nOutput\n10.0000000000\n\nInput\n8 2\n1 2 4 100000 123 456 789 1\n\nOutput\n28964.2857142857\n\n\n\n-----Note-----\n\nIn the third example there are n - k + 1 = 7 weeks, so the answer is sums of all weeks divided by 7."} +{"id": "01784", "text": "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.\n\nIn 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 \u2264 c \u2264 k, 1 \u2264 i, j \u2264 n the following condition must be satisfied |b_{i}, c - b_{j}, c| \u2264 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.\n\n\n-----Input-----\n\nThe first line of the input contains positive integers n and k (1 \u2264 n, k \u2264 100), separated by a space \u2014 the number of piles and the number of colors respectively.\n\nThe second line contains n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100) denoting number of pebbles in each of the piles.\n\n\n-----Output-----\n\nIf there is no way to paint the pebbles satisfying the given condition, output \"NO\" (without quotes) .\n\nOtherwise 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 \u2264 j \u2264 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.\n\n\n-----Examples-----\nInput\n4 4\n1 2 3 4\n\nOutput\nYES\n1\n1 4\n1 2 4\n1 2 3 4\n\nInput\n5 2\n3 2 4 1 3\n\nOutput\nNO\n\nInput\n5 4\n3 2 4 3 5\n\nOutput\nYES\n1 2 3\n1 3\n1 2 3 4\n1 3 4\n1 1 2 3 4"} +{"id": "01785", "text": "Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences.\n\nLet's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Function h(s, t) can be used to define the function of Vasya distance \u03c1(s, t): $\\rho(s, t) = \\sum_{i = 0}^{n - 1} \\sum_{j = 0}^{n - 1} h(\\operatorname{shift}(s, i), \\operatorname{shift}(t, j))$ where $\\operatorname{shift}(s, i)$ is obtained from string s, by applying left circular shift i times. For example, \u03c1(\"AGC\", \"CGT\") = h(\"AGC\", \"CGT\") + h(\"AGC\", \"GTC\") + h(\"AGC\", \"TCG\") + h(\"GCA\", \"CGT\") + h(\"GCA\", \"GTC\") + h(\"GCA\", \"TCG\") + h(\"CAG\", \"CGT\") + h(\"CAG\", \"GTC\") + h(\"CAG\", \"TCG\") = 1 + 1 + 0 + 0 + 1 + 1 + 1 + 0 + 1 = 6\n\nVasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value. Formally speaking, t must satisfy the equation: $\\rho(s, t) = \\operatorname{max}_{u :|u|=|s|} \\rho(s, u)$.\n\nVasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 10^5).\n\nThe second line of the input contains a single string of length n, consisting of characters \"ACGT\".\n\n\n-----Output-----\n\nPrint a single number\u00a0\u2014 the answer modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n1\nC\n\nOutput\n1\n\nInput\n2\nAG\n\nOutput\n4\n\nInput\n3\nTTT\n\nOutput\n1\n\n\n\n-----Note-----\n\nPlease note that if for two distinct strings t_1 and t_2 values \u03c1(s, t_1) \u0438 \u03c1(s, t_2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one.\n\nIn the first sample, there is \u03c1(\"C\", \"C\") = 1, for the remaining strings t of length 1 the value of \u03c1(s, t) is 0.\n\nIn the second sample, \u03c1(\"AG\", \"AG\") = \u03c1(\"AG\", \"GA\") = \u03c1(\"AG\", \"AA\") = \u03c1(\"AG\", \"GG\") = 4.\n\nIn the third sample, \u03c1(\"TTT\", \"TTT\") = 27"} +{"id": "01786", "text": "Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm \u00d7 h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how.\n\nIn 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.\n\nAfter 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.\n\nLeonid offers to divide the labor \u2014 he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree?\n\n\n-----Input-----\n\nThe first line contains three integers w, h, n (2 \u2264 w, h \u2264 200 000, 1 \u2264 n \u2264 200 000).\n\nNext n lines contain the descriptions of the cuts. Each description has the form H\u00a0y or V\u00a0x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 \u2264 y \u2264 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 \u2264 x \u2264 w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts.\n\n\n-----Output-----\n\nAfter each cut print on a single line the area of the maximum available glass fragment in mm^2.\n\n\n-----Examples-----\nInput\n4 3 4\nH 2\nV 2\nV 3\nV 1\n\nOutput\n8\n4\n4\n2\n\nInput\n7 6 5\nH 4\nV 3\nV 5\nH 2\nV 1\n\nOutput\n28\n16\n12\n6\n4\n\n\n\n-----Note-----\n\nPicture for the first sample test: [Image] Picture for the second sample test: $\\square$"} +{"id": "01787", "text": "The Fair Nut found a string $s$. The string consists of lowercase Latin letters. The Nut is a curious guy, so he wants to find the number of strictly increasing sequences $p_1, p_2, \\ldots, p_k$, such that: For each $i$ ($1 \\leq i \\leq k$), $s_{p_i} =$ 'a'. For each $i$ ($1 \\leq i < k$), there is such $j$ that $p_i < j < p_{i + 1}$ and $s_j =$ 'b'. \n\nThe Nut is upset because he doesn't know how to find the number. Help him.\n\nThis number should be calculated modulo $10^9 + 7$.\n\n\n-----Input-----\n\nThe first line contains the string $s$ ($1 \\leq |s| \\leq 10^5$) consisting of lowercase Latin letters.\n\n\n-----Output-----\n\nIn a single line print the answer to the problem\u00a0\u2014 the number of such sequences $p_1, p_2, \\ldots, p_k$ modulo $10^9 + 7$.\n\n\n-----Examples-----\nInput\nabbaa\n\nOutput\n5\nInput\nbaaaa\n\nOutput\n4\nInput\nagaa\n\nOutput\n3\n\n\n-----Note-----\n\nIn the first example, there are $5$ possible sequences. $[1]$, $[4]$, $[5]$, $[1, 4]$, $[1, 5]$.\n\nIn the second example, there are $4$ possible sequences. $[2]$, $[3]$, $[4]$, $[5]$.\n\nIn the third example, there are $3$ possible sequences. $[1]$, $[3]$, $[4]$."} +{"id": "01788", "text": "Takahashi has two integers X and Y.\nHe computed X + Y and X - Y, and the results were A and B, respectively.\nNow he cannot remember what X and Y were. Find X and Y for him.\n\n-----Constraints-----\n - -100 \\leq A, B \\leq 100\n - For the given integers A and B, there uniquely exist integers X and Y such that X + Y = A and X - Y = B.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint X and Y.\n\n-----Sample Input-----\n2 -2\n\n-----Sample Output-----\n0 2\n\nIf X = 0 and Y = 2, they match the situation: 0 + 2 = 2 and 0 - 2 = -2."} +{"id": "01789", "text": "There are two 100-story buildings, called A and B. (In this problem, the ground floor is called the first floor.)\nFor each i = 1,\\dots, 100, the i-th floor of A and that of B are connected by a corridor.\nAlso, 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.\nYou can traverse each of those corridors in both directions, and it takes you x minutes to get to the other end.\nAdditionally, 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.\nFind the minimum time needed to reach the b-th floor of B from the a-th floor of A.\n\n-----Constraints-----\n - 1 \\leq a,b,x,y \\leq 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\na b x y\n\n-----Output-----\nPrint the minimum time needed to reach the b-th floor of B from the a-th floor of A.\n\n-----Sample Input-----\n2 1 1 5\n\n-----Sample Output-----\n1\n\nThere 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.\nThis is the fastest way to get there, since taking the stairs just once costs you 5 minutes."} +{"id": "01790", "text": "Arkady's morning seemed to be straight of his nightmare. He overslept through the whole morning and, still half-asleep, got into the tram that arrived the first. Some time after, leaving the tram, he realized that he was not sure about the line number of the tram he was in.\n\nDuring his ride, Arkady woke up several times and each time he saw the tram stopping at some stop. For each stop he knows which lines of tram stop there. Given this information, can you help Arkady determine what are the possible lines of the tram he was in?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 100$)\u00a0\u2014 the number of stops Arkady saw.\n\nThe next $n$ lines describe the stops. Each of them starts with a single integer $r$ ($1 \\le r \\le 100$)\u00a0\u2014 the number of tram lines that stop there. $r$ distinct integers follow, each one between $1$ and $100$, inclusive,\u00a0\u2014 the line numbers. They can be in arbitrary order.\n\nIt is guaranteed that Arkady's information is consistent, i.e. there is at least one tram line that Arkady could take.\n\n\n-----Output-----\n\nPrint all tram lines that Arkady could be in, in arbitrary order.\n\n\n-----Examples-----\nInput\n3\n3 1 4 6\n2 1 4\n5 10 5 6 4 1\n\nOutput\n1 4 \n\nInput\n5\n1 1\n10 10 9 8 7 100 5 4 3 99 1\n5 1 2 3 4 5\n5 4 1 3 2 5\n4 10 1 5 3\n\nOutput\n1 \n\n\n\n-----Note-----\n\nConsider the first example. Arkady woke up three times. The first time he saw a stop with lines $1$, $4$, $6$. The second time he saw a stop with lines $1$, $4$. The third time he saw a stop with lines $10$, $5$, $6$, $4$ and $1$. He can be in a tram of one of two lines: $1$ or $4$."} +{"id": "01791", "text": "You will receive 5 points for solving this problem.\n\nManao 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.\n\nWe 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:\n\n\"ABCDEF|GHIJK\" | \"A|BCDEFGHIJK\" | \"AB|RACA|DAB|RA\" | \"X|XXXXX|X|X|XXXXXX\"\n\n | | | XXXXXX\n\n KJIHG | KJIHGFEDCB | AR | X\n\n ABCDEF | A | DAB | X\n\n | | ACAR | XXXXX\n\n | | AB | X\n\n\n\nOne last example for \"ABCD|EFGH|IJ|K\": \n\n K\n\nIJ\n\nHGFE\n\nABCD\n\n\n\nManao 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.\n\n\n-----Input-----\n\nThe input will consist of one line containing a single string of n characters with 1 \u2264 n \u2264 1000 and no spaces. All characters of the string will be uppercase letters.\n\nThis problem doesn't have subproblems. You will get 5 points for the correct submission.\n\n\n-----Output-----\n\nPrint a single integer \u2014 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.\n\n\n-----Examples-----\nInput\nABRACADABRA\n\nOutput\n3\n\nInput\nABBBCBDB\n\nOutput\n3\n\nInput\nAB\n\nOutput\n1\n\n\n\n-----Note-----\n\nConsider the first example. Manao can create a pile of three 'A's using the folding \"AB|RACAD|ABRA\", which results in the following structure: \n\nABRA\n\nDACAR\n\n AB\n\n\n\nIn the second example, Manao can create a pile of three 'B's using the following folding: \"AB|BB|CBDB\". \n\nCBDB\n\nBB\n\nAB\n\n\n\nAnother way for Manao to create a pile of three 'B's with \"ABBBCBDB\" is the following folding: \"AB|B|BCBDB\". \n\n BCBDB\n\n B\n\nAB\n\n\n\nIn the third example, there are no folds performed and the string is just written in one line."} +{"id": "01792", "text": "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.\n\nThe 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.\n\nThe galaxy can be represented as an undirected graph with $n$ planets (nodes) and $m$ wormholes (edges), each connecting two planets.\n\nA total of $s$ empire spaceships and $b$ rebel bases are located at different planets in the galaxy.\n\nEach 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$.\n\nEach base is given a location $x$, and a defensive strength $d$.\n\nA 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 \n\nVader 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.\n\nVader 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.\n\nTherefore, for each base that is attacked, the rebels lose $k$ gold.\n\nHowever, 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.\n\nOf course, dummy bases do not contain any gold, but creating such a dummy base costs $h$ gold.\n\nWhat is the minimum gold the rebels can lose if they create an optimal number of dummy bases?\n\n\n-----Input-----\n\nThe 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.\n\nThe next $m$ lines contain two integers $u$ and $v$ ($1 \\leq u$, $v \\leq n$) denoting an undirected edge between the two nodes.\n\nThe 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.\n\nThe 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.\n\nThe 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.\n\n\n-----Output-----\n\nPrint a single integer, the minimum cost in terms of gold.\n\n\n-----Example-----\nInput\n6 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\nOutput\n12\n\n\n\n-----Note-----\n\nOne way to minimize the cost is to build $4$ dummy bases, for a total cost of $4 \\times 3 = 12$.\n\nOne empire spaceship will be assigned to attack each of these dummy bases, resulting in zero actual bases attacked."} +{"id": "01793", "text": "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.\n\nA 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}$.\n\n\n-----Input-----\n\nThe 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}$)\u00a0\u2014 the number of vertices, the maximum number of vertices in one path and the maximum sum in one path.\n\nThe second line contains $n$ integers $w_1, w_2, \\ldots, w_n$ ($1 \\le w_i \\le 10^9$)\u00a0\u2014 the numbers in the vertices of the tree.\n\nThe 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.\n\n\n-----Output-----\n\nOutput one number \u00a0\u2014 the minimum number of vertical paths. If it is impossible to split the tree, output $-1$.\n\n\n-----Examples-----\nInput\n3 1 3\n1 2 3\n1 1\n\nOutput\n3\nInput\n3 3 6\n1 2 3\n1 1\n\nOutput\n2\nInput\n1 1 10000\n10001\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first sample the tree is split into $\\{1\\},\\ \\{2\\},\\ \\{3\\}$.\n\nIn the second sample the tree is split into $\\{1,\\ 2\\},\\ \\{3\\}$ or $\\{1,\\ 3\\},\\ \\{2\\}$.\n\nIn the third sample it is impossible to split the tree."} +{"id": "01794", "text": "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.\n\nLet \u03c1_{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 \u03c1_{i}, j among all pairs 1 \u2264 i < j \u2264 n.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (2 \u2264 n \u2264 100 000)\u00a0\u2014 the number of stations.\n\nThe second line contains n - 1 integer a_{i} (i + 1 \u2264 a_{i} \u2264 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.\n\n\n-----Output-----\n\nPrint the sum of \u03c1_{i}, j among all pairs of 1 \u2264 i < j \u2264 n.\n\n\n-----Examples-----\nInput\n4\n4 4 4\n\nOutput\n6\n\nInput\n5\n2 3 5 5\n\nOutput\n17\n\n\n\n-----Note-----\n\nIn 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.\n\nConsider the second sample: \u03c1_{1, 2} = 1 \u03c1_{1, 3} = 2 \u03c1_{1, 4} = 3 \u03c1_{1, 5} = 3 \u03c1_{2, 3} = 1 \u03c1_{2, 4} = 2 \u03c1_{2, 5} = 2 \u03c1_{3, 4} = 1 \u03c1_{3, 5} = 1 \u03c1_{4, 5} = 1 \n\nThus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17."} +{"id": "01795", "text": "As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number f_{i}, where 1 \u2264 f_{i} \u2264 n and f_{i} \u2260 i.\n\nWe call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 5000)\u00a0\u2014 the number of planes.\n\nThe second line contains n integers f_1, f_2, ..., f_{n} (1 \u2264 f_{i} \u2264 n, f_{i} \u2260 i), meaning that the i-th plane likes the f_{i}-th.\n\n\n-----Output-----\n\nOutput \u00abYES\u00bb if there is a love triangle consisting of planes on Earth. Otherwise, output \u00abNO\u00bb.\n\nYou can output any letter in lower case or in upper case.\n\n\n-----Examples-----\nInput\n5\n2 4 5 1 3\n\nOutput\nYES\n\nInput\n5\n5 5 5 5 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.\n\nIn second example there are no love triangles."} +{"id": "01796", "text": "The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.\n\nThe language is that peculiar as it has exactly one variable, called x. Also, there are two operations:\n\n Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. \n\nA statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters \"+\", \"-\", \"X\". Executing a statement means applying the operation it contains.\n\nA programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.\n\nYou're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 150) \u2014 the number of statements in the programme.\n\nNext n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter \u00abX\u00bb). Thus, there are no empty statements. The operation and the variable can be written in any order.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the final value of x.\n\n\n-----Examples-----\nInput\n1\n++X\n\nOutput\n1\n\nInput\n2\nX++\n--X\n\nOutput\n0"} +{"id": "01797", "text": "The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself.\n\nThere are n stations in the subway. It was built according to the Bertown Transport Law:\n\n For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. \n\nThe President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 \u2264 x, y \u2264 n).\n\nThe mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes.\n\nThe mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! \n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 100000) \u2014 the number of stations.\n\nThe second line contains n integer numbers p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 n) \u2014 the current structure of the subway. All these numbers are distinct.\n\n\n-----Output-----\n\nPrint one number \u2014 the maximum possible value of convenience.\n\n\n-----Examples-----\nInput\n3\n2 1 3\n\nOutput\n9\n\nInput\n5\n1 5 4 3 2\n\nOutput\n17\n\n\n\n-----Note-----\n\nIn the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3).\n\nIn the second example the mayor can change p_2 to 4 and p_3 to 5."} +{"id": "01798", "text": "One day Jeff got hold of an integer sequence a_1, a_2, ..., a_{n} of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold:\n\n x occurs in sequence a. Consider all positions of numbers x in the sequence a (such i, that a_{i} = x). These numbers, sorted in the increasing order, must form an arithmetic progression. \n\nHelp Jeff, find all x that meet the problem conditions.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5). The next line contains integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5). The numbers are separated by spaces.\n\n\n-----Output-----\n\nIn the first line print integer t \u2014 the number of valid x. On each of the next t lines print two integers x and p_{x}, where x is current suitable value, p_{x} is the common difference between numbers in the progression (if x occurs exactly once in the sequence, p_{x} must equal 0). Print the pairs in the order of increasing x.\n\n\n-----Examples-----\nInput\n1\n2\n\nOutput\n1\n2 0\n\nInput\n8\n1 2 1 3 1 2 1 5\n\nOutput\n4\n1 2\n2 4\n3 0\n5 0\n\n\n\n-----Note-----\n\nIn the first test 2 occurs exactly once in the sequence, ergo p_2 = 0."} +{"id": "01799", "text": "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.\n\nYou 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.\n\nA 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.\n\nLimak 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.\n\nWhat is the best place Limak can get?\n\n\n-----Input-----\n\nThe first line of the standard input contains one integer n (2 \u2264 n \u2264 300 000)\u00a0\u2014 the number of teams.\n\nThe i-th of n following lines contains two integers t_{i} and w_{i} (0 \u2264 t_{i} \u2264 w_{i} \u2264 10^18)\u00a0\u2014 respectively the number of balloons and the weight of the i-th team. Limak is a member of the first team.\n\n\n-----Output-----\n\nPrint one integer denoting the best place Limak can get.\n\n\n-----Examples-----\nInput\n8\n20 1000\n32 37\n40 1000\n45 50\n16 16\n16 16\n14 1000\n2 1000\n\nOutput\n3\n\nInput\n7\n4 4\n4 4\n4 4\n4 4\n4 4\n4 4\n5 5\n\nOutput\n2\n\nInput\n7\n14000000003 1000000000000000000\n81000000000 88000000000\n5000000000 7000000000\n15000000000 39000000000\n46000000000 51000000000\n0 1000000000\n0 0\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn 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. \n\nIn the second sample, Limak has the second place and he can't improve it.\n\nIn 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)."} +{"id": "01800", "text": "Each month Blake gets the report containing main economic indicators of the company \"Blake Technologies\". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).\n\nEmployees of the \"Blake Technologies\" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 200 000)\u00a0\u2014 the number of commodities in the report and the number of managers, respectively.\n\nThe second line contains n integers a_{i} (|a_{i}| \u2264 10^9)\u00a0\u2014 the initial report before it gets to the first manager.\n\nThen follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \\in \\{1,2 \\}$, 1 \u2264 r_{i} \u2264 n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.\n\n\n-----Output-----\n\nPrint n integers\u00a0\u2014 the final report, which will be passed to Blake by manager number m.\n\n\n-----Examples-----\nInput\n3 1\n1 2 3\n2 2\n\nOutput\n2 1 3 \nInput\n4 2\n1 2 4 3\n2 3\n1 2\n\nOutput\n2 4 1 3 \n\n\n-----Note-----\n\nIn the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.\n\nIn the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake."} +{"id": "01801", "text": "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).\n\nNow Dima wants to count the number of distinct sequences of points of length 2\u00b7n 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.\n\nDima considers two assembled sequences (p_1, q_1), (p_2, q_2), ..., (p_{2\u00b7}n, q_{2\u00b7}n) and (x_1, y_1), (x_2, y_2), ..., (x_{2\u00b7}n, y_{2\u00b7}n) distinct, if there is such i (1 \u2264 i \u2264 2\u00b7n), that (p_{i}, q_{i}) \u2260 (x_{i}, y_{i}).\n\nAs the answer can be rather large, print the remainder from dividing the answer by number m.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5). The second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9). The third line contains n integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 10^9). The numbers in the lines are separated by spaces.\n\nThe last line contains integer m (2 \u2264 m \u2264 10^9 + 7).\n\n\n-----Output-----\n\nIn the single line print the remainder after dividing the answer to the problem by number m. \n\n\n-----Examples-----\nInput\n1\n1\n2\n7\n\nOutput\n1\n\nInput\n2\n1 2\n2 3\n11\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample you can get only one sequence: (1, 1), (2, 1). \n\nIn 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."} +{"id": "01802", "text": "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.\n\nTo 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.\n\nHelp 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.\n\n\n-----Input-----\n\nThe single line contains integer n (2 \u2264 n \u2264 10^3) \u2014 the number of students. The second line contains n space-separated integers \u2014 the heights of all students. The height of one student is an integer which absolute value doesn't exceed 10^4.\n\n\n-----Output-----\n\nIn 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 \u2014 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.\n\nIf there are multiple possible answers, you can print any of them.\n\n\n-----Examples-----\nInput\n5\n-3 -4 -2 -3 3\n\nOutput\n2\n-3 1\n\nInput\n5\n2 -3 -1 -4 3\n\nOutput\n1\n-4 2\n\n\n\n-----Note-----\n\nLets look at the first sample. We can proceed in the following manner:\n\n 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. \n\nTo 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."} +{"id": "01803", "text": "Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are a_{i} oskols sitting on the i-th wire. $40$ \n\nSometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. \n\nShaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n, (1 \u2264 n \u2264 100). The next line contains a list of space-separated integers a_1, a_2, ..., a_{n}, (0 \u2264 a_{i} \u2264 100). \n\nThe third line contains an integer m, (0 \u2264 m \u2264 100). Each of the next m lines contains two integers x_{i} and y_{i}. The integers mean that for the i-th time Shaass shoot the y_{i}-th (from left) bird on the x_{i}-th wire, (1 \u2264 x_{i} \u2264 n, 1 \u2264 y_{i}). It's guaranteed there will be at least y_{i} birds on the x_{i}-th wire at that moment.\n\n\n-----Output-----\n\nOn the i-th line of the output print the number of birds on the i-th wire.\n\n\n-----Examples-----\nInput\n5\n10 10 10 10 10\n5\n2 5\n3 13\n2 12\n1 13\n4 6\n\nOutput\n0\n12\n5\n0\n16\n\nInput\n3\n2 4 1\n1\n2 2\n\nOutput\n3\n0\n3"} +{"id": "01804", "text": "Think of New York as a rectangular grid consisting of N vertical avenues numerated from 1 to N and M horizontal streets numerated 1 to M. C friends are staying at C hotels located at some street-avenue crossings. They are going to celebrate birthday of one of them in the one of H restaurants also located at some street-avenue crossings. They also want that the maximum distance covered by one of them while traveling to the restaurant to be minimum possible. Help friends choose optimal restaurant for a celebration.\n\nSuppose that the distance between neighboring crossings are all the same equal to one kilometer.\n\n\n-----Input-----\n\nThe first line contains two integers N \u0438 M\u00a0\u2014 size of the city (1 \u2264 N, M \u2264 10^9). In the next line there is a single integer C (1 \u2264 C \u2264 10^5)\u00a0\u2014 the number of hotels friends stayed at. Following C lines contain descriptions of hotels, each consisting of two coordinates x and y (1 \u2264 x \u2264 N, 1 \u2264 y \u2264 M). The next line contains an integer H\u00a0\u2014 the number of restaurants (1 \u2264 H \u2264 10^5). Following H lines contain descriptions of restaurants in the same format.\n\nSeveral restaurants and hotels may be located near the same crossing.\n\n\n-----Output-----\n\nIn the first line output the optimal distance. In the next line output index of a restaurant that produces this optimal distance. If there are several possibilities, you are allowed to output any of them.\n\n\n-----Examples-----\nInput\n10 10\n2\n1 1\n3 3\n2\n1 10\n4 4\n\nOutput\n6\n2"} +{"id": "01805", "text": "Let's denote correct match equation (we will denote it as CME) an equation $a + b = c$ there all integers $a$, $b$ and $c$ are greater than zero.\n\nFor example, equations $2 + 2 = 4$ (||+||=||||) and $1 + 2 = 3$ (|+||=|||) are CME but equations $1 + 2 = 4$ (|+||=||||), $2 + 2 = 3$ (||+||=|||), and $0 + 1 = 1$ (+|=|) are not.\n\nNow, you have $n$ matches. You want to assemble a CME using all your matches. Unfortunately, it is possible that you can't assemble the CME using all matches. But you can buy some extra matches and then assemble CME!\n\nFor example, if $n = 2$, you can buy two matches and assemble |+|=||, and if $n = 5$ you can buy one match and assemble ||+|=|||. [Image] \n\nCalculate the minimum number of matches which you have to buy for assembling CME.\n\nNote, that you have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line contains one integer $q$ ($1 \\le q \\le 100$)\u00a0\u2014 the number of queries.\n\nThe only line of each query contains one integer $n$ ($2 \\le n \\le 10^9$)\u00a0\u2014 the number of matches.\n\n\n-----Output-----\n\nFor each test case print one integer in single line\u00a0\u2014 the minimum number of matches which you have to buy for assembling CME. \n\n\n-----Example-----\nInput\n4\n2\n5\n8\n11\n\nOutput\n2\n1\n0\n1\n\n\n\n-----Note-----\n\nThe first and second queries are explained in the statement.\n\nIn the third query, you can assemble $1 + 3 = 4$ (|+|||=||||) without buying matches.\n\nIn the fourth query, buy one match and assemble $2 + 4 = 6$ (||+||||=||||||)."} +{"id": "01806", "text": "You are given $n$ intervals in form $[l; r]$ on a number line.\n\nYou are also given $m$ queries in form $[x; y]$. What is the minimal number of intervals you have to take so that every point (not necessarily integer) from $x$ to $y$ is covered by at least one of them? \n\nIf you can't choose intervals so that every point from $x$ to $y$ is covered, then print -1 for that query.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$) \u2014 the number of intervals and the number of queries, respectively.\n\nEach of the next $n$ lines contains two integer numbers $l_i$ and $r_i$ ($0 \\le l_i < r_i \\le 5 \\cdot 10^5$) \u2014 the given intervals.\n\nEach of the next $m$ lines contains two integer numbers $x_i$ and $y_i$ ($0 \\le x_i < y_i \\le 5 \\cdot 10^5$) \u2014 the queries.\n\n\n-----Output-----\n\nPrint $m$ integer numbers. The $i$-th number should be the answer to the $i$-th query: either the minimal number of intervals you have to take so that every point (not necessarily integer) from $x_i$ to $y_i$ is covered by at least one of them or -1 if you can't choose intervals so that every point from $x_i$ to $y_i$ is covered.\n\n\n-----Examples-----\nInput\n2 3\n1 3\n2 4\n1 3\n1 4\n3 4\n\nOutput\n1\n2\n1\n\nInput\n3 4\n1 3\n1 3\n4 5\n1 2\n1 3\n1 4\n1 5\n\nOutput\n1\n1\n-1\n-1\n\n\n\n-----Note-----\n\nIn the first example there are three queries:\n\n query $[1; 3]$ can be covered by interval $[1; 3]$; query $[1; 4]$ can be covered by intervals $[1; 3]$ and $[2; 4]$. There is no way to cover $[1; 4]$ by a single interval; query $[3; 4]$ can be covered by interval $[2; 4]$. It doesn't matter that the other points are covered besides the given query. \n\nIn the second example there are four queries:\n\n query $[1; 2]$ can be covered by interval $[1; 3]$. Note that you can choose any of the two given intervals $[1; 3]$; query $[1; 3]$ can be covered by interval $[1; 3]$; query $[1; 4]$ can't be covered by any set of intervals; query $[1; 5]$ can't be covered by any set of intervals. Note that intervals $[1; 3]$ and $[4; 5]$ together don't cover $[1; 5]$ because even non-integer points should be covered. Here $3.5$, for example, isn't covered."} +{"id": "01807", "text": "Once Max found an electronic calculator from his grandfather Dovlet's chest. He noticed that the numbers were written with seven-segment indicators (https://en.wikipedia.org/wiki/Seven-segment_display). [Image] \n\nMax starts to type all the values from a to b. After typing each number Max resets the calculator. Find the total number of segments printed on the calculator.\n\nFor example if a = 1 and b = 3 then at first the calculator will print 2 segments, then \u2014 5 segments and at last it will print 5 segments. So the total number of printed segments is 12.\n\n\n-----Input-----\n\nThe only line contains two integers a, b (1 \u2264 a \u2264 b \u2264 10^6) \u2014 the first and the last number typed by Max.\n\n\n-----Output-----\n\nPrint the only integer a \u2014 the total number of printed segments.\n\n\n-----Examples-----\nInput\n1 3\n\nOutput\n12\n\nInput\n10 15\n\nOutput\n39"} +{"id": "01808", "text": "Luba has to do n chores today. i-th chore takes a_{i} units of time to complete. It is guaranteed that for every $i \\in [ 2 . . n ]$ the condition a_{i} \u2265 a_{i} - 1 is met, so the sequence is sorted.\n\nAlso Luba can work really hard on some chores. She can choose not more than k any chores and do each of them in x units of time instead of a_{i} ($x < \\operatorname{min}_{i = 1}^{n} a_{i}$).\n\nLuba is very responsible, so she has to do all n chores, and now she wants to know the minimum time she needs to do everything. Luba cannot do two chores simultaneously.\n\n\n-----Input-----\n\nThe first line contains three integers n, k, x\u00a0(1 \u2264 k \u2264 n \u2264 100, 1 \u2264 x \u2264 99) \u2014 the number of chores Luba has to do, the number of chores she can do in x units of time, and the number x itself.\n\nThe second line contains n integer numbers a_{i}\u00a0(2 \u2264 a_{i} \u2264 100) \u2014 the time Luba has to spend to do i-th chore.\n\nIt is guaranteed that $x < \\operatorname{min}_{i = 1}^{n} a_{i}$, and for each $i \\in [ 2 . . n ]$ a_{i} \u2265 a_{i} - 1.\n\n\n-----Output-----\n\nPrint one number \u2014 minimum time Luba needs to do all n chores.\n\n\n-----Examples-----\nInput\n4 2 2\n3 6 7 10\n\nOutput\n13\n\nInput\n5 2 1\n100 100 100 100 100\n\nOutput\n302\n\n\n\n-----Note-----\n\nIn the first example the best option would be to do the third and the fourth chore, spending x = 2 time on each instead of a_3 and a_4, respectively. Then the answer is 3 + 6 + 2 + 2 = 13.\n\nIn the second example Luba can choose any two chores to spend x time on them instead of a_{i}. So the answer is 100\u00b73 + 2\u00b71 = 302."} +{"id": "01809", "text": "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 \u2264 i \u2264 n) book is w_{i}.\n\nAs 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. \n\n[Image] \n\nHe decided to read books for m days. In the j-th (1 \u2264 j \u2264 m) day, he will read the book that is numbered with integer b_{j} (1 \u2264 b_{j} \u2264 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.\n\nAfter 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?\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n (2 \u2264 n \u2264 500) and m (1 \u2264 m \u2264 1000) \u2014 the number of books, and the number of days for which Jaehyun would read books.\n\nThe second line contains n space-separated integers w_1, w_2, ..., w_{n} (1 \u2264 w_{i} \u2264 100) \u2014 the weight of each book.\n\nThe third line contains m space separated integers b_1, b_2, ..., b_{m} (1 \u2264 b_{j} \u2264 n) \u2014 the order of books that he would read. Note that he can read the same book more than once.\n\n\n-----Output-----\n\nPrint the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.\n\n\n-----Examples-----\nInput\n3 5\n1 2 3\n1 3 2 3 1\n\nOutput\n12\n\n\n\n-----Note-----\n\nHere's a picture depicting the example. Each vertical column presents the stacked books. [Image]"} +{"id": "01810", "text": "IT City company developing computer games invented a new way to reward its employees. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is divisible by all numbers from 2 to 10 every developer of this game gets a small bonus.\n\nA game designer Petya knows that the company is just about to release a new game that was partly developed by him. On the basis of his experience he predicts that n people will buy the game during the first month. Now Petya wants to determine how many times he will get the bonus. Help him to know it.\n\n\n-----Input-----\n\nThe only line of the input contains one integer n (1 \u2264 n \u2264 10^18) \u2014 the prediction on the number of people who will buy the game.\n\n\n-----Output-----\n\nOutput one integer showing how many numbers from 1 to n are divisible by all numbers from 2 to 10.\n\n\n-----Examples-----\nInput\n3000\n\nOutput\n1"} +{"id": "01811", "text": "Ksusha the Squirrel is standing at the beginning of a straight road, divided into n sectors. The sectors are numbered 1 to n, from left to right. Initially, Ksusha stands in sector 1. \n\nKsusha wants to walk to the end of the road, that is, get to sector n. Unfortunately, there are some rocks on the road. We know that Ksusha hates rocks, so she doesn't want to stand in sectors that have rocks.\n\nKsusha the squirrel keeps fit. She can jump from sector i to any of the sectors i + 1, i + 2, ..., i + k. \n\nHelp Ksusha! Given the road description, say if she can reach the end of the road (note, she cannot stand on a rock)?\n\n\n-----Input-----\n\nThe first line contains two integers n and k (2 \u2264 n \u2264 3\u00b710^5, 1 \u2264 k \u2264 3\u00b710^5). The next line contains n characters \u2014 the description of the road: the i-th character equals \".\", if the i-th sector contains no rocks. Otherwise, it equals \"#\".\n\nIt is guaranteed that the first and the last characters equal \".\".\n\n\n-----Output-----\n\nPrint \"YES\" (without the quotes) if Ksusha can reach the end of the road, otherwise print \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n2 1\n..\n\nOutput\nYES\n\nInput\n5 2\n.#.#.\n\nOutput\nYES\n\nInput\n7 3\n.#.###.\n\nOutput\nNO"} +{"id": "01812", "text": "You are given n positive integers a_1, a_2, ..., a_{n}.\n\nFor every a_{i} you need to find a positive integer k_{i} such that the decimal notation of 2^{k}_{i} contains the decimal notation of a_{i} as a substring among its last min(100, length(2^{k}_{i})) digits. Here length(m) is the length of the decimal notation of m.\n\nNote that you don't have to minimize k_{i}. The decimal notations in this problem do not contain leading zeros.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 2 000)\u00a0\u2014 the number of integers a_{i}.\n\nEach of the next n lines contains a positive integer a_{i} (1 \u2264 a_{i} < 10^11).\n\n\n-----Output-----\n\nPrint n lines. The i-th of them should contain a positive integer k_{i} such that the last min(100, length(2^{k}_{i})) digits of 2^{k}_{i} contain the decimal notation of a_{i} as a substring. Integers k_{i} must satisfy 1 \u2264 k_{i} \u2264 10^50.\n\nIt can be shown that the answer always exists under the given constraints. If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n2\n8\n2\n\nOutput\n3\n1\n\nInput\n2\n3\n4857\n\nOutput\n5\n20"} +{"id": "01813", "text": "Alexandra has a paper strip with n numbers on it. Let's call them a_{i} from left to right.\n\nNow Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:\n\n\n\n Each piece should contain at least l numbers.\n\n The difference between the maximal and the minimal number on the piece should be at most s.\n\nPlease help Alexandra to find the minimal number of pieces meeting the condition above.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers n, s, l (1 \u2264 n \u2264 10^5, 0 \u2264 s \u2264 10^9, 1 \u2264 l \u2264 10^5).\n\nThe second line contains n integers a_{i} separated by spaces ( - 10^9 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nOutput the minimal number of strip pieces.\n\nIf there are no ways to split the strip, output -1.\n\n\n-----Examples-----\nInput\n7 2 2\n1 3 1 2 4 1 2\n\nOutput\n3\n\nInput\n7 2 2\n1 100 1 100 1 100 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nFor the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].\n\nFor the second sample, we can't let 1 and 100 be on the same piece, so no solution exists."} +{"id": "01814", "text": "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.\n\nThe 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.\n\nYou 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)$?\"\n\n\n-----Input-----\n\nThe 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$)\u00a0\u2014 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.\n\nThe 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.\n\nThe 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.\n\nThe fourth line contains a single integer $q$ ($1 \\leq q \\leq 10^5$)\u00a0\u2014 the number of queries.\n\nThe 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$)\u00a0\u2014 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.\u00a0e. $y_1$ and $y_2$ are not among $l_i$ and $e_i$.\n\n\n-----Output-----\n\nPrint $q$ integers, one per line\u00a0\u2014 the answers for the queries.\n\n\n-----Example-----\nInput\n5 6 1 1 3\n2\n5\n3\n1 1 5 6\n1 3 5 4\n3 3 5 3\n\nOutput\n7\n5\n4\n\n\n\n-----Note-----\n\nIn 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.\n\nIn 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."} +{"id": "01815", "text": "This problem is same as the previous one, but has larger constraints.\n\nShiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.\n\nFor each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences.\n\nFor example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. \n\nSince Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^5$)\u00a0\u2014 the total number of days.\n\nThe second line contains $n$ integers $u_1, u_2, \\ldots, u_n$ ($1 \\leq u_i \\leq 10^5$)\u00a0\u2014 the colors of the ribbons the cats wear. \n\n\n-----Output-----\n\nPrint a single integer $x$\u00a0\u2014 the largest possible streak of days.\n\n\n-----Examples-----\nInput\n13\n1 1 1 2 2 2 3 3 3 4 4 4 5\n\nOutput\n13\nInput\n5\n10 100 20 200 1\n\nOutput\n5\nInput\n1\n100000\n\nOutput\n1\nInput\n7\n3 2 1 1 4 5 1\n\nOutput\n6\nInput\n6\n1 1 1 2 2 2\n\nOutput\n5\n\n\n-----Note-----\n\nIn the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak.\n\nIn the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once."} +{"id": "01816", "text": "HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.\n\nOne of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.\n\nFind the time need to read file split to n fragments. The i-th sector contains the f_{i}-th fragment of the file (1 \u2264 f_{i} \u2264 n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th.\n\nIt takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time.\n\n\n-----Input-----\n\nThe first line contains a positive integer n (1 \u2264 n \u2264 2\u00b710^5) \u2014 the number of fragments.\n\nThe second line contains n different integers f_{i} (1 \u2264 f_{i} \u2264 n) \u2014 the number of the fragment written in the i-th sector.\n\n\n-----Output-----\n\nPrint the only integer \u2014 the number of time units needed to read the file.\n\n\n-----Examples-----\nInput\n3\n3 1 2\n\nOutput\n3\n\nInput\n5\n1 3 5 4 2\n\nOutput\n10\n\n\n\n-----Note-----\n\nIn the second example the head moves in the following way: 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units \n\nSo the answer to the second example is 4 + 3 + 2 + 1 = 10."} +{"id": "01817", "text": "Two players play a game.\n\nInitially there are $n$ integers $a_1, a_2, \\ldots, a_n$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i.\u00a0e. $n - 1$ turns are made. The first player makes the first move, then players alternate turns.\n\nThe first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it.\n\nYou want to know what number will be left on the board after $n - 1$ turns if both players make optimal moves.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 1000$)\u00a0\u2014 the number of numbers on the board.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^6$).\n\n\n-----Output-----\n\nPrint one number that will be left on the board.\n\n\n-----Examples-----\nInput\n3\n2 1 3\n\nOutput\n2\nInput\n3\n2 2 2\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first sample, the first player erases $3$ and the second erases $1$. $2$ is left on the board.\n\nIn the second sample, $2$ is left on the board regardless of the actions of the players."} +{"id": "01818", "text": "Dima got into number sequences. Now he's got sequence a_1, a_2, ..., a_{n}, consisting of n positive integers. Also, Dima has got a function f(x), which can be defined with the following recurrence: f(0) = 0; f(2\u00b7x) = f(x); f(2\u00b7x + 1) = f(x) + 1. \n\nDima wonders, how many pairs of indexes (i, j) (1 \u2264 i < j \u2264 n) are there, such that f(a_{i}) = f(a_{j}). Help him, count the number of such pairs. \n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5). The second line contains n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\nThe numbers in the lines are separated by single spaces.\n\n\n-----Output-----\n\nIn a single line print the answer to the problem.\n\nPlease, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n3\n1 2 4\n\nOutput\n3\n\nInput\n3\n5 3 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample any pair (i, j) will do, so the answer is 3.\n\nIn the second sample only pair (1, 2) will do."} +{"id": "01819", "text": "You have a list of numbers from $1$ to $n$ written from left to right on the blackboard.\n\nYou perform an algorithm consisting of several steps (steps are $1$-indexed). On the $i$-th step you wipe the $i$-th number (considering only remaining numbers). You wipe the whole number (not one digit). $\\left. \\begin{array}{r}{1234567 \\ldots} \\\\{234567 \\ldots} \\\\{24567 \\ldots} \\end{array} \\right.$ \n\nWhen there are less than $i$ numbers remaining, you stop your algorithm. \n\nNow you wonder: what is the value of the $x$-th remaining number after the algorithm is stopped?\n\n\n-----Input-----\n\nThe first line contains one integer $T$ ($1 \\le T \\le 100$) \u2014 the number of queries. The next $T$ lines contain queries \u2014 one per line. All queries are independent.\n\nEach line contains two space-separated integers $n$ and $x$ ($1 \\le x < n \\le 10^{9}$) \u2014 the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least $x$ numbers.\n\n\n-----Output-----\n\nPrint $T$ integers (one per query) \u2014 the values of the $x$-th number after performing the algorithm for the corresponding queries.\n\n\n-----Example-----\nInput\n3\n3 1\n4 2\n69 6\n\nOutput\n2\n4\n12"} +{"id": "01820", "text": "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})$. \n\nFind 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.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains one integer $n$ ($3 \\le n \\le 5 \\cdot 10^4$)\u00a0\u2014 the length of the array $a$.\n\nThe 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$)\u00a0\u2014 the array $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case print the answer to it in one line.\n\nIf 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.\n\nOtherwise, print -1.\n\n\n-----Example-----\nInput\n3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000\n\nOutput\n2 3 6\n-1\n1 2 3\n\n\n\n-----Note-----\n\nIn the first test case it is impossible with sides $6$, $11$ and $18$. Note, that this is not the only correct answer.\n\nIn the second test case you always can construct a non-degenerate triangle."} +{"id": "01821", "text": "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! \n\nAlice 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.\n\nSubsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.\n\nSubsequence forms a melody when each two adjacent notes either differs by 1 or are congruent modulo 7.\n\nYou 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.\n\n\n-----Input-----\n\nThe first line contains one integer number n (2 \u2264 n \u2264 5000).\n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5) \u2014 notes written on a sheet.\n\n\n-----Output-----\n\nPrint maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody.\n\n\n-----Examples-----\nInput\n4\n1 2 4 5\n\nOutput\n4\n\nInput\n6\n62 22 60 61 48 49\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example subsequences [1, 2] and [4, 5] give length 4 in total.\n\nIn 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."} +{"id": "01822", "text": "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.\n\n(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.\n\nThe 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.\n\nHaving 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...\n\nAs 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.\n\n\n-----Input-----\n\nThe first line contains two integers n (1 \u2264 n \u2264 10^3) and x (1 \u2264 x \u2264 n) \u2014 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.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 n) \u2014 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.\n\nThe input limits for scoring 30 points are (subproblem B1): It is guaranteed that the number of zero elements a_{i} doesn't exceed 20. \n\nThe input limits for scoring 100 points are (subproblems B1+B2): The number of zero elements a_{i} is arbitrary. \n\n\n-----Output-----\n\nPrint all possible positions of the Smart Beaver in the line in the increasing order.\n\n\n-----Examples-----\nInput\n6 1\n2 0 4 0 6 0\n\nOutput\n2\n4\n6\n\nInput\n6 2\n2 3 0 5 6 0\n\nOutput\n2\n5\n\nInput\n4 1\n0 0 0 0\n\nOutput\n1\n2\n3\n4\n\nInput\n6 2\n0 0 1 0 4 5\n\nOutput\n1\n3\n4\n6\n\n\n\n-----Note----- [Image] Picture for the fourth test."} +{"id": "01823", "text": "A film festival is coming up in the city N. The festival will last for exactly n days and each day will have a premiere of exactly one film. Each film has a genre \u2014 an integer from 1 to k.\n\nOn the i-th day the festival will show a movie of genre a_{i}. We know that a movie of each of k genres occurs in the festival programme at least once. In other words, each integer from 1 to k occurs in the sequence a_1, a_2, ..., a_{n} at least once.\n\nValentine is a movie critic. He wants to watch some movies of the festival and then describe his impressions on his site.\n\nAs any creative person, Valentine is very susceptive. After he watched the movie of a certain genre, Valentine forms the mood he preserves until he watches the next movie. If the genre of the next movie is the same, it does not change Valentine's mood. If the genres are different, Valentine's mood changes according to the new genre and Valentine has a stress.\n\nValentine can't watch all n movies, so he decided to exclude from his to-watch list movies of one of the genres. In other words, Valentine is going to choose exactly one of the k genres and will skip all the movies of this genre. He is sure to visit other movies.\n\nValentine wants to choose such genre x (1 \u2264 x \u2264 k), that the total number of after-movie stresses (after all movies of genre x are excluded) were minimum.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (2 \u2264 k \u2264 n \u2264 10^5), where n is the number of movies and k is the number of genres.\n\nThe second line of the input contains a sequence of n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 k), where a_{i} is the genre of the i-th movie. It is guaranteed that each number from 1 to k occurs at least once in this sequence.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of the genre (from 1 to k) of the excluded films. If there are multiple answers, print the genre with the minimum number.\n\n\n-----Examples-----\nInput\n10 3\n1 1 2 3 2 3 3 1 1 3\n\nOutput\n3\nInput\n7 3\n3 1 3 2 3 1 2\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first sample if we exclude the movies of the 1st genre, the genres 2, 3, 2, 3, 3, 3 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1, 1, 3, 3, 3, 1, 1, 3 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1, 1, 2, 2, 1, 1 remain, that is 2 stresses.\n\nIn the second sample whatever genre Valentine excludes, he will have exactly 3 stresses."} +{"id": "01824", "text": "A and B are preparing themselves for programming contests.\n\nB loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.\n\nInitially, 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.\n\nHowever, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared \u2014 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.\n\nCan you help B find out exactly what two errors he corrected?\n\n\n-----Input-----\n\nThe first line of the input contains integer n (3 \u2264 n \u2264 10^5) \u2014 the initial number of compilation errors.\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 the errors the compiler displayed for the first time. \n\nThe third line contains n - 1 space-separated integers b_1, b_2, ..., b_{n} - 1 \u2014 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. \n\nThe fourth line contains n - 2 space-separated integers \u0441_1, \u0441_2, ..., \u0441_{n} - 2 \u2014 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. \n\n\n-----Output-----\n\nPrint two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively. \n\n\n-----Examples-----\nInput\n5\n1 5 8 123 7\n123 7 5 1\n5 1 7\n\nOutput\n8\n123\n\nInput\n6\n1 4 3 3 5 7\n3 7 5 4 3\n4 3 7 5\n\nOutput\n1\n3\n\n\n\n-----Note-----\n\nIn the first test sample B first corrects the error number 8, then the error number 123.\n\nIn 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."} +{"id": "01825", "text": "Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one. \n\nIvan knows some information about the string s. Namely, he remembers, that string t_{i} occurs in string s at least k_{i} times or more, he also remembers exactly k_{i} positions where the string t_{i} occurs in string s: these positions are x_{i}, 1, x_{i}, 2, ..., x_{i}, k_{i}. He remembers n such strings t_{i}.\n\nYou are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings t_{i} and string s consist of small English letters only.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 10^5) \u2014 the number of strings Ivan remembers.\n\nThe next n lines contain information about the strings. The i-th of these lines contains non-empty string t_{i}, then positive integer k_{i}, which equal to the number of times the string t_{i} occurs in string s, and then k_{i} distinct positive integers x_{i}, 1, x_{i}, 2, ..., x_{i}, k_{i} in increasing order \u2014 positions, in which occurrences of the string t_{i} in the string s start. It is guaranteed that the sum of lengths of strings t_{i} doesn't exceed 10^6, 1 \u2264 x_{i}, j \u2264 10^6, 1 \u2264 k_{i} \u2264 10^6, and the sum of all k_{i} doesn't exceed 10^6. The strings t_{i} can coincide.\n\nIt is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.\n\n\n-----Output-----\n\nPrint lexicographically minimal string that fits all the information Ivan remembers. \n\n\n-----Examples-----\nInput\n3\na 4 1 3 5 7\nab 2 1 5\nca 1 4\n\nOutput\nabacaba\n\nInput\n1\na 1 3\n\nOutput\naaa\n\nInput\n3\nab 1 1\naba 1 3\nab 2 3 5\n\nOutput\nababab"} +{"id": "01826", "text": "Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.\n\nIn the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.\n\nYour problem is to print the minimum possible length of the sequence of moves after the replacements.\n\n\n-----Input-----\n\nThe first line of the input contains one integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the length of the sequence. The second line contains the sequence consisting of n characters U and R.\n\n\n-----Output-----\n\nPrint the minimum possible length of the sequence of moves after all replacements are done.\n\n\n-----Examples-----\nInput\n5\nRUURU\n\nOutput\n3\n\nInput\n17\nUUURRRRRUUURURUUU\n\nOutput\n13\n\n\n\n-----Note-----\n\nIn the first test the shortened sequence of moves may be DUD (its length is 3).\n\nIn the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13)."} +{"id": "01827", "text": "\u0412\u0430\u0441\u044f \u043a\u0443\u043f\u0438\u043b \u0441\u0442\u043e\u043b, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e n \u043d\u043e\u0436\u0435\u043a. \u041a\u0430\u0436\u0434\u0430\u044f \u043d\u043e\u0436\u043a\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0447\u0430\u0441\u0442\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c. \u041a\u0430\u0436\u0434\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b, \u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0438\u0437 \u0432\u0441\u0435\u0445 2n \u0447\u0430\u0441\u0442\u0435\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c n \u043d\u043e\u0436\u0435\u043a \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0439 \u0434\u043b\u0438\u043d\u044b. \u041f\u0440\u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u043d\u043e\u0436\u043a\u0438 \u043b\u044e\u0431\u044b\u0435 \u0434\u0432\u0435 \u0447\u0430\u0441\u0442\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u044b \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c. \u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0432\u0441\u0435 \u043d\u043e\u0436\u043a\u0438 \u0441\u0442\u043e\u043b\u0430 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d\u044b, \u0430 \u0432\u0430\u043c \u0437\u0430\u0434\u0430\u043d\u044b \u0434\u043b\u0438\u043d\u044b 2n \u0447\u0430\u0441\u0442\u0435\u0439 \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.\n\n\u041f\u043e\u043c\u043e\u0433\u0438\u0442\u0435 \u0412\u0430\u0441\u0435 \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u043d\u043e\u0436\u043a\u0438 \u0441\u0442\u043e\u043b\u0430 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0439 \u0434\u043b\u0438\u043d\u044b, \u0440\u0430\u0437\u0431\u0438\u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0435 2n \u0447\u0430\u0441\u0442\u0438 \u043d\u0430 \u043f\u0430\u0440\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u041a\u0430\u0436\u0434\u0430\u044f \u043d\u043e\u0436\u043a\u0430 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0440\u043e\u0432\u043d\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0447\u0430\u0441\u0442\u0435\u0439, \u043d\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043d\u043e\u0436\u043a\u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0443 \u0447\u0430\u0441\u0442\u044c.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0437\u0430\u0434\u0430\u043d\u043e \u0447\u0438\u0441\u043b\u043e n (1 \u2264 n \u2264 1000)\u00a0\u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u043e\u0436\u0435\u043a \u0443 \u0441\u0442\u043e\u043b\u0430, \u043a\u0443\u043f\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0412\u0430\u0441\u0435\u0439.\n\n\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438\u0437 2n \u0446\u0435\u043b\u044b\u0445 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b a_1, a_2, ..., a_2n (1 \u2264 a_{i} \u2264 100 000)\u00a0\u2014 \u0434\u043b\u0438\u043d\u044b \u0447\u0430\u0441\u0442\u0435\u0439 \u043d\u043e\u0436\u0435\u043a \u0441\u0442\u043e\u043b\u0430 \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 n \u0441\u0442\u0440\u043e\u043a \u043f\u043e \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439\u00a0\u2014 \u0434\u043b\u0438\u043d\u044b \u0447\u0430\u0441\u0442\u0435\u0439 \u043d\u043e\u0436\u0435\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0434\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c n \u043d\u043e\u0436\u0435\u043a \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0439 \u0434\u043b\u0438\u043d\u044b. \u0415\u0441\u043b\u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3\n1 3 2 4 5 3\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1 5\n2 4\n3 3\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3\n1 1 1 2 2 2\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n1 2\n2 1\n1 2"} +{"id": "01828", "text": "Maria participates in a bicycle race.\n\nThe speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.\n\nLet's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa).\n\nMaria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored.\n\nHelp Maria get ready for the competition\u00a0\u2014 determine the number of dangerous turns on the track.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (4 \u2264 n \u2264 1000)\u00a0\u2014 the number of straight sections of the track.\n\nThe following (n + 1)-th line contains pairs of integers (x_{i}, y_{i}) ( - 10 000 \u2264 x_{i}, y_{i} \u2264 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (x_{i}, y_{i}) and ends at the point (x_{i} + 1, y_{i} + 1).\n\nIt is guaranteed that:\n\n the first straight section is directed to the north; the southernmost (and if there are several, then the most western of among them) point of the track is the first point; the last point coincides with the first one (i.e., the start position); any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point); no pair of points (except for the first and last one) is the same; no two adjacent straight sections are directed in the same direction or in opposite directions. \n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of dangerous turns on the track.\n\n\n-----Examples-----\nInput\n6\n0 0\n0 1\n1 1\n1 2\n2 2\n2 0\n0 0\n\nOutput\n1\n\nInput\n16\n1 1\n1 5\n3 5\n3 7\n2 7\n2 9\n6 9\n6 7\n5 7\n5 3\n4 3\n4 4\n3 4\n3 2\n5 2\n5 1\n1 1\n\nOutput\n6\n\n\n\n-----Note-----\n\nThe first sample corresponds to the picture:\n\n [Image] \n\nThe picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1."} +{"id": "01829", "text": "PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses.\n\nYou're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?\n\n\n-----Input-----\n\nThe first input line contains two integers n and m (1 \u2264 n, m \u2264 10^3)\u00a0\u2014 number of words PolandBall and EnemyBall know, respectively.\n\nThen n strings follow, one per line\u00a0\u2014 words familiar to PolandBall.\n\nThen m strings follow, one per line\u00a0\u2014 words familiar to EnemyBall.\n\nNote that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players.\n\nEach word is non-empty and consists of no more than 500 lowercase English alphabet letters.\n\n\n-----Output-----\n\nIn a single line of print the answer\u00a0\u2014 \"YES\" if PolandBall wins and \"NO\" otherwise. Both Balls play optimally.\n\n\n-----Examples-----\nInput\n5 1\npolandball\nis\na\ncool\ncharacter\nnope\n\nOutput\nYES\nInput\n2 2\nkremowka\nwadowicka\nkremowka\nwiedenska\n\nOutput\nYES\nInput\n1 2\na\na\nb\n\nOutput\nNO\n\n\n-----Note-----\n\nIn the first example PolandBall knows much more words and wins effortlessly.\n\nIn the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins."} +{"id": "01830", "text": "Vasya has the square chessboard of size n \u00d7 n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.\n\nThe cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.\n\nYou are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n \u2264 100 000, 1 \u2264 m \u2264 min(100 000, n^2))\u00a0\u2014 the size of the board and the number of rooks. \n\nEach of the next m lines contains integers x_{i} and y_{i} (1 \u2264 x_{i}, y_{i} \u2264 n)\u00a0\u2014 the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.\n\n\n-----Output-----\n\nPrint m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.\n\n\n-----Examples-----\nInput\n3 3\n1 1\n3 1\n2 2\n\nOutput\n4 2 0 \n\nInput\n5 2\n1 5\n5 1\n\nOutput\n16 9 \n\nInput\n100000 1\n300 400\n\nOutput\n9999800001 \n\n\n\n-----Note-----\n\nOn the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.\n\n [Image]"} +{"id": "01831", "text": "One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect \u2013 it's the opposite! Independent researchers confirmed that the nervous system of a zombie is highly complicated \u2013 it consists of n brains (much like a cow has several stomachs). They are interconnected by brain connectors, which are veins capable of transmitting thoughts between brains. There are two important properties such a brain network should have to function properly: It should be possible to exchange thoughts between any two pairs of brains (perhaps indirectly, through other brains). There should be no redundant brain connectors, that is, removing any brain connector would make property 1 false. \n\nIf both properties are satisfied, we say that the nervous system is valid. Unfortunately (?), if the system is not valid, the zombie stops thinking and becomes (even more) dead. Your task is to analyze a given nervous system of a zombie and find out whether it is valid.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and m (1 \u2264 n, m \u2264 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains a\u2002b it connects (1 \u2264 a, b \u2264 n, a \u2260 b).\n\n\n-----Output-----\n\nThe output consists of one line, containing either yes or no depending on whether the nervous system is valid.\n\n\n-----Examples-----\nInput\n4 4\n1 2\n2 3\n3 1\n4 1\n\nOutput\nno\n\nInput\n6 5\n1 2\n2 3\n3 4\n4 5\n3 6\n\nOutput\nyes"} +{"id": "01832", "text": "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$.\n\nKoa the Koala initially has $n+1$ strings $s_1, s_2, \\dots, s_{n+1}$.\n\nFor each $i$ ($1 \\le i \\le n$) she calculated $a_i$\u00a0\u2014 the length of the longest common prefix of $s_i$ and $s_{i+1}$.\n\nSeveral days later Koa found these numbers, but she couldn't remember the strings.\n\nSo 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?\n\nIf there are many answers print any. We can show that answer always exists for the given constraints. \n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains $t$ ($1 \\le t \\le 100$)\u00a0\u2014 the number of test cases. Description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 100$)\u00a0\u2014 the number of elements in the list $a$.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 50$)\u00a0\u2014 the elements of $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $100$.\n\n\n-----Output-----\n\nFor each test case:\n\nOutput $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$.\n\nIf there are many answers print any. We can show that answer always exists for the given constraints.\n\n\n-----Example-----\nInput\n4\n4\n1 2 4 2\n2\n5 3\n3\n1 3 1\n3\n0 0 0\n\nOutput\naeren\nari\narousal\naround\nari\nmonogon\nmonogamy\nmonthly\nkevinvu\nkuroni\nkurioni\nkorone\nanton\nloves\nadhoc\nproblems\n\n\n-----Note-----\n\nIn the $1$-st test case one of the possible answers is $s = [aeren, ari, arousal, around, ari]$.\n\nLengths 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$"} +{"id": "01833", "text": "You are given an integer array $a_1, a_2, \\ldots, a_n$.\n\nThe array $b$ is called to be a subsequence of $a$ if it is possible to remove some elements from $a$ to get $b$.\n\nArray $b_1, b_2, \\ldots, b_k$ is called to be good if it is not empty and for every $i$ ($1 \\le i \\le k$) $b_i$ is divisible by $i$.\n\nFind the number of good subsequences in $a$ modulo $10^9 + 7$. \n\nTwo subsequences are considered different if index sets of numbers included in them are different. That is, the values \u200bof the elements \u200bdo not matter in the comparison of subsequences. In particular, the array $a$ has exactly $2^n - 1$ different subsequences (excluding an empty subsequence).\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 100\\,000$)\u00a0\u2014 the length of the array $a$.\n\nThe next line contains integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^6$).\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the number of good subsequences taken modulo $10^9 + 7$.\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n3\nInput\n5\n2 2 1 22 14\n\nOutput\n13\n\n\n-----Note-----\n\nIn the first example, all three non-empty possible subsequences are good: $\\{1\\}$, $\\{1, 2\\}$, $\\{2\\}$\n\nIn the second example, the possible good subsequences are: $\\{2\\}$, $\\{2, 2\\}$, $\\{2, 22\\}$, $\\{2, 14\\}$, $\\{2\\}$, $\\{2, 22\\}$, $\\{2, 14\\}$, $\\{1\\}$, $\\{1, 22\\}$, $\\{1, 14\\}$, $\\{22\\}$, $\\{22, 14\\}$, $\\{14\\}$.\n\nNote, that some subsequences are listed more than once, since they occur in the original array multiple times."} +{"id": "01834", "text": "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:\n\n a_{i} \u2265 a_{i} - 1 for all even i, a_{i} \u2264 a_{i} - 1 for all odd i > 1. \n\nFor example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn\u2019t z-sorted.\n\nCan you make the array z-sorted?\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 1000) \u2014 the number of elements in the array a.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^9) \u2014 the elements of the array a.\n\n\n-----Output-----\n\nIf it's possible to make the array a z-sorted print n space separated integers a_{i} \u2014 the elements after z-sort. Otherwise print the only word \"Impossible\".\n\n\n-----Examples-----\nInput\n4\n1 2 2 1\n\nOutput\n1 2 1 2\n\nInput\n5\n1 3 2 2 5\n\nOutput\n1 5 2 3 2"} +{"id": "01835", "text": "A palindrome is a string $t$ which reads the same backward as forward (formally, $t[i] = t[|t| + 1 - i]$ for all $i \\in [1, |t|]$). Here $|t|$ denotes the length of a string $t$. For example, the strings 010, 1001 and 0 are palindromes.\n\nYou have $n$ binary strings $s_1, s_2, \\dots, s_n$ (each $s_i$ consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings \u2014 there are no restrictions.\n\nFormally, in one move you: choose four integer numbers $x, a, y, b$ such that $1 \\le x, y \\le n$ and $1 \\le a \\le |s_x|$ and $1 \\le b \\le |s_y|$ (where $x$ and $y$ are string indices and $a$ and $b$ are positions in strings $s_x$ and $s_y$ respectively), swap (exchange) the characters $s_x[a]$ and $s_y[b]$. \n\nWhat is the maximum number of strings you can make palindromic simultaneously?\n\n\n-----Input-----\n\nThe first line contains single integer $Q$ ($1 \\le Q \\le 50$) \u2014 the number of test cases.\n\nThe first line on each test case contains single integer $n$ ($1 \\le n \\le 50$) \u2014 the number of binary strings you have.\n\nNext $n$ lines contains binary strings $s_1, s_2, \\dots, s_n$ \u2014 one per line. It's guaranteed that $1 \\le |s_i| \\le 50$ and all strings constist of zeroes and/or ones.\n\n\n-----Output-----\n\nPrint $Q$ integers \u2014 one per test case. The $i$-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the $i$-th test case.\n\n\n-----Example-----\nInput\n4\n1\n0\n3\n1110\n100110\n010101\n2\n11111\n000001\n2\n001\n11100111\n\nOutput\n1\n2\n2\n2\n\n\n\n-----Note-----\n\nIn the first test case, $s_1$ is palindrome, so the answer is $1$.\n\nIn the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make $s_1 = \\text{0110}$, $s_2 = \\text{111111}$ and $s_3 = \\text{010000}$.\n\nIn the third test case we can make both strings palindromic. For example, $s_1 = \\text{11011}$ and $s_2 = \\text{100001}$.\n\nIn the last test case $s_2$ is palindrome and you can make $s_1$ palindrome, for example, by swapping $s_1[2]$ and $s_1[3]$."} +{"id": "01836", "text": "This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions: Only segments already presented on the picture can be painted; The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; The numbers of points from the beginning of the tail to the end should strictly increase. \n\nMasha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.\n\nNote that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.\n\n\n-----Input-----\n\nFirst line of the input contains two integers n and m(2 \u2264 n \u2264 100 000, 1 \u2264 m \u2264 200 000)\u00a0\u2014 the number of points and the number segments on the picture respectively. \n\nThen follow m lines, each containing two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i})\u00a0\u2014 the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.\n\n\n-----Output-----\n\nPrint the maximum possible value of the hedgehog's beauty.\n\n\n-----Examples-----\nInput\n8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7\n\nOutput\n9\n\nInput\n4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n\nOutput\n12\n\n\n\n-----Note-----\n\nThe picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3\u00b73 = 9.\n\n[Image]"} +{"id": "01837", "text": "A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, sequence [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] are not.\n\nA fixed point of a function is a point that is mapped to itself by the function. A permutation can be regarded as a bijective function. We'll get a definition of a fixed point in a permutation. An integer i is a fixed point of permutation a_0, a_1, ..., a_{n} - 1 if and only if a_{i} = i. For example, permutation [0, 2, 1] has 1 fixed point and permutation [0, 1, 2] has 3 fixed points.\n\nYou are given permutation a. You are allowed to swap two elements of the permutation at most once. Your task is to maximize the number of fixed points in the resulting permutation. Note that you are allowed to make at most one swap operation.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5). The second line contains n integers a_0, a_1, ..., a_{n} - 1 \u2014 the given permutation.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum possible number of fixed points in the permutation after at most one swap operation.\n\n\n-----Examples-----\nInput\n5\n0 1 3 4 2\n\nOutput\n3"} +{"id": "01838", "text": "Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i.\u00a0e. the two words mean the same) and antonymy (i.\u00a0e. the two words mean the opposite). From time to time he discovers a new relation between two words.\n\nHe know that if two words have a relation between them, then each of them has relations with the words that has relations with the other. For example, if like means love and love is the opposite of hate, then like is also the opposite of hate. One more example: if love is the opposite of hate and hate is the opposite of like, then love means like, and so on.\n\nSometimes Mahmoud discovers a wrong relation. A wrong relation is a relation that makes two words equal and opposite at the same time. For example if he knows that love means like and like is the opposite of hate, and then he figures out that hate means like, the last relation is absolutely wrong because it makes hate and like opposite and have the same meaning at the same time.\n\nAfter Mahmoud figured out many relations, he was worried that some of them were wrong so that they will make other relations also wrong, so he decided to tell every relation he figured out to his coder friend Ehab and for every relation he wanted to know is it correct or wrong, basing on the previously discovered relations. If it is wrong he ignores it, and doesn't check with following relations.\n\nAfter adding all relations, Mahmoud asked Ehab about relations between some words based on the information he had given to him. Ehab is busy making a Codeforces round so he asked you for help.\n\n\n-----Input-----\n\nThe first line of input contains three integers n, m and q (2 \u2264 n \u2264 10^5, 1 \u2264 m, q \u2264 10^5) where n is the number of words in the dictionary, m is the number of relations Mahmoud figured out and q is the number of questions Mahmoud asked after telling all relations.\n\nThe second line contains n distinct words a_1, a_2, ..., a_{n} consisting of small English letters with length not exceeding 20, which are the words in the dictionary.\n\nThen m lines follow, each of them contains an integer t (1 \u2264 t \u2264 2) followed by two different words x_{i} and y_{i} which has appeared in the dictionary words. If t = 1, that means x_{i} has a synonymy relation with y_{i}, otherwise x_{i} has an antonymy relation with y_{i}.\n\nThen q lines follow, each of them contains two different words which has appeared in the dictionary. That are the pairs of words Mahmoud wants to know the relation between basing on the relations he had discovered.\n\nAll words in input contain only lowercase English letters and their lengths don't exceed 20 characters. In all relations and in all questions the two words are different.\n\n\n-----Output-----\n\nFirst, print m lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print \"NO\" (without quotes) and ignore it, otherwise print \"YES\" (without quotes).\n\nAfter that print q lines, one per each question. If the two words have the same meaning, output 1. If they are opposites, output 2. If there is no relation between them, output 3.\n\nSee the samples for better understanding.\n\n\n-----Examples-----\nInput\n3 3 4\nhate love like\n1 love like\n2 love hate\n1 hate like\nlove like\nlove hate\nlike hate\nhate like\n\nOutput\nYES\nYES\nNO\n1\n2\n2\n2\n\nInput\n8 6 5\nhi welcome hello ihateyou goaway dog cat rat\n1 hi welcome\n1 ihateyou goaway\n2 hello ihateyou\n2 hi goaway\n2 hi hello\n1 hi hello\ndog cat\ndog hi\nhi hello\nihateyou goaway\nwelcome ihateyou\n\nOutput\nYES\nYES\nYES\nYES\nNO\nYES\n3\n3\n1\n1\n2"} +{"id": "01839", "text": "City X consists of n vertical and n horizontal infinite roads, forming n \u00d7 n intersections. Roads (both vertical and horizontal) are numbered from 1 to n, and the intersections are indicated by the numbers of the roads that form them.\n\nSand roads have long been recognized out of date, so the decision was made to asphalt them. To do this, a team of workers was hired and a schedule of work was made, according to which the intersections should be asphalted.\n\nRoad repairs are planned for n^2 days. On the i-th day of the team arrives at the i-th intersection in the list and if none of the two roads that form the intersection were already asphalted they asphalt both roads. Otherwise, the team leaves the intersection, without doing anything with the roads.\n\nAccording to the schedule of road works tell in which days at least one road will be asphalted.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 50) \u2014 the number of vertical and horizontal roads in the city. \n\nNext n^2 lines contain the order of intersections in the schedule. The i-th of them contains two numbers h_{i}, v_{i} (1 \u2264 h_{i}, v_{i} \u2264 n), separated by a space, and meaning that the intersection that goes i-th in the timetable is at the intersection of the h_{i}-th horizontal and v_{i}-th vertical roads. It is guaranteed that all the intersections in the timetable are distinct.\n\n\n-----Output-----\n\nIn the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1.\n\n\n-----Examples-----\nInput\n2\n1 1\n1 2\n2 1\n2 2\n\nOutput\n1 4 \n\nInput\n1\n1 1\n\nOutput\n1 \n\n\n\n-----Note-----\n\nIn the sample the brigade acts like that: On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road; On the second day the brigade of the workers comes to the intersection of the 1-st horizontal and the 2-nd vertical road. The 2-nd vertical road hasn't been asphalted, but as the 1-st horizontal road has been asphalted on the first day, the workers leave and do not asphalt anything; On the third day the brigade of the workers come to the intersection of the 2-nd horizontal and the 1-st vertical road. The 2-nd horizontal road hasn't been asphalted but as the 1-st vertical road has been asphalted on the first day, the workers leave and do not asphalt anything; On the fourth day the brigade come to the intersection formed by the intersection of the 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted, the workers asphalt the 2-nd vertical and the 2-nd horizontal road."} +{"id": "01840", "text": "Heidi and Doctor Who hopped out of the TARDIS and found themselves at EPFL in 2018. They were surrounded by stormtroopers and Darth Vader was approaching. Miraculously, they managed to escape to a nearby rebel base but the Doctor was very confused. Heidi reminded him that last year's HC2 theme was Star Wars. Now he understood, and he's ready to face the evils of the Empire!\n\nThe rebels have $s$ spaceships, each with a certain attacking power $a$.\n\nThey want to send their spaceships to destroy the empire bases and steal enough gold and supplies in order to keep the rebellion alive.\n\nThe empire has $b$ bases, each with a certain defensive power $d$, and a certain amount of gold $g$.\n\nA spaceship can attack all the bases which have a defensive power less than or equal to its attacking power.\n\nIf a spaceship attacks a base, it steals all the gold in that base.\n\nThe rebels are still undecided which spaceship to send out first, so they asked for the Doctor's help. They would like to know, for each spaceship, the maximum amount of gold it can steal.\n\n\n-----Input-----\n\nThe first line contains integers $s$ and $b$ ($1 \\leq s, b \\leq 10^5$), the number of spaceships and the number of bases, respectively.\n\nThe second line contains $s$ integers $a$ ($0 \\leq a \\leq 10^9$), the attacking power of each spaceship.\n\nThe next $b$ lines contain integers $d, g$ ($0 \\leq d \\leq 10^9$, $0 \\leq g \\leq 10^4$), the defensive power and the gold of each base, respectively.\n\n\n-----Output-----\n\nPrint $s$ integers, the maximum amount of gold each spaceship can steal, in the same order as the spaceships are given in the input.\n\n\n-----Example-----\nInput\n5 4\n1 3 5 2 4\n0 1\n4 2\n2 8\n9 4\n\nOutput\n1 9 11 9 11\n\n\n\n-----Note-----\n\nThe first spaceship can only attack the first base.\n\nThe second spaceship can attack the first and third bases.\n\nThe third spaceship can attack the first, second and third bases."} +{"id": "01841", "text": "Sereja has an array a, consisting of n integers a_1, a_2, ..., a_{n}. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l_1, l_2, ..., l_{m} (1 \u2264 l_{i} \u2264 n). For each number l_{i} he wants to know how many distinct numbers are staying on the positions l_{i}, l_{i} + 1, ..., n. Formally, he want to find the number of distinct numbers among a_{l}_{i}, a_{l}_{i} + 1, ..., a_{n}.?\n\nSereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each l_{i}.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 10^5). The second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5) \u2014 the array elements.\n\nNext m lines contain integers l_1, l_2, ..., l_{m}. The i-th line contains integer l_{i} (1 \u2264 l_{i} \u2264 n).\n\n\n-----Output-----\n\nPrint m lines \u2014 on the i-th line print the answer to the number l_{i}.\n\n\n-----Examples-----\nInput\n10 10\n1 2 3 4 1 2 3 4 100000 99999\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n\nOutput\n6\n6\n6\n6\n6\n5\n4\n3\n2\n1"} +{"id": "01842", "text": "The Department of economic development of IT City created a model of city development till year 2100.\n\nTo prepare report about growth perspectives it is required to get growth estimates from the model.\n\nTo get the growth estimates it is required to solve a quadratic equation. Since the Department of economic development of IT City creates realistic models only, that quadratic equation has a solution, moreover there are exactly two different real roots.\n\nThe greater of these roots corresponds to the optimistic scenario, the smaller one corresponds to the pessimistic one. Help to get these estimates, first the optimistic, then the pessimistic one.\n\n\n-----Input-----\n\nThe only line of the input contains three integers a, b, c ( - 1000 \u2264 a, b, c \u2264 1000) \u2014 the coefficients of ax^2 + bx + c = 0 equation.\n\n\n-----Output-----\n\nIn the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10^{ - 6}.\n\n\n-----Examples-----\nInput\n1 30 200\n\nOutput\n-10.000000000000000\n-20.000000000000000"} +{"id": "01843", "text": "In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum.\n\nFor example, for n = 4 the sum is equal to - 1 - 2 + 3 - 4 = - 4, because 1, 2 and 4 are 2^0, 2^1 and 2^2 respectively.\n\nCalculate the answer for t values of n.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer t (1 \u2264 t \u2264 100) \u2014 the number of values of n to be processed.\n\nEach of next t lines contains a single integer n (1 \u2264 n \u2264 10^9).\n\n\n-----Output-----\n\nPrint the requested sum for each of t integers n given in the input.\n\n\n-----Examples-----\nInput\n2\n4\n1000000000\n\nOutput\n-4\n499999998352516354\n\n\n\n-----Note-----\n\nThe answer for the first sample is explained in the statement."} +{"id": "01844", "text": "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$.\n\nIt 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?\n\n\n-----Input-----\n\nThe first line contains an only integer $n$ ($1 \\le n \\le 300\\,000$)\u00a0\u2014 the number of integers in the sequence.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 300\\,000$).\n\n\n-----Output-----\n\nIf there is no subset of the given sequence with gcd equal to $1$, output -1.\n\nOtherwise, output exactly one integer\u00a0\u2014 the size of the smallest subset with gcd equal to $1$.\n\n\n-----Examples-----\nInput\n3\n10 6 15\n\nOutput\n3\n\nInput\n3\n2 4 6\n\nOutput\n-1\n\nInput\n7\n30 60 21 42 70 15 30\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn 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$.\n\nIn the second example, for all subsets of numbers the gcd is at least $2$."} +{"id": "01845", "text": "One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by $n$ machines, and the power of the $i$-th machine is $a_i$. \n\nThis year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer $x$, then choose one machine and reduce the power of its machine by $x$ times, and at the same time increase the power of one another machine by $x$ times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices $i$ and $j$, and one integer $x$ such that $x$ is a divisor of $a_i$, and change powers as following: $a_i = \\frac{a_i}{x}$, $a_j = a_j \\cdot x$\n\nSasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him!\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 5 \\cdot 10^4$)\u00a0\u2014 the number of machines.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 100$)\u00a0\u2014 the powers of the machines.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 minimum total power.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n\nOutput\n14\n\nInput\n4\n4 2 4 4\n\nOutput\n14\n\nInput\n5\n2 4 2 3 7\n\nOutput\n18\n\n\n\n-----Note-----\n\nIn the first example, the farmer can reduce the power of the $4$-th machine by $2$ times, and increase the power of the $1$-st machine by $2$ times, then the powers will be: $[2, 2, 3, 2, 5]$.\n\nIn the second example, the farmer can reduce the power of the $3$-rd machine by $2$ times, and increase the power of the $2$-nd machine by $2$ times. At the same time, the farmer can leave is be as it is and the total power won't change.\n\nIn the third example, it is optimal to leave it be as it is."} +{"id": "01846", "text": "Scientists say a lot about the problems of global warming and cooling of the Earth. Indeed, such natural phenomena strongly influence all life on our planet.\n\nOur hero Vasya is quite concerned about the problems. He decided to try a little experiment and observe how outside daily temperature changes. He hung out a thermometer on the balcony every morning and recorded the temperature. He had been measuring the temperature for the last n days. Thus, he got a sequence of numbers t_1, t_2, ..., t_{n}, where the i-th number is the temperature on the i-th day.\n\nVasya analyzed the temperature statistics in other cities, and came to the conclusion that the city has no environmental problems, if first the temperature outside is negative for some non-zero number of days, and then the temperature is positive for some non-zero number of days. More formally, there must be a positive integer k (1 \u2264 k \u2264 n - 1) such that t_1 < 0, t_2 < 0, ..., t_{k} < 0 and t_{k} + 1 > 0, t_{k} + 2 > 0, ..., t_{n} > 0. In particular, the temperature should never be zero. If this condition is not met, Vasya decides that his city has environmental problems, and gets upset.\n\nYou do not want to upset Vasya. Therefore, you want to select multiple values of temperature and modify them to satisfy Vasya's condition. You need to know what the least number of temperature values needs to be changed for that.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 10^5) \u2014 the number of days for which Vasya has been measuring the temperature. \n\nThe second line contains a sequence of n integers t_1, t_2, ..., t_{n} (|t_{i}| \u2264 10^9) \u2014 the sequence of temperature values. Numbers t_{i} are separated by single spaces.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the given task.\n\n\n-----Examples-----\nInput\n4\n-1 1 -2 1\n\nOutput\n1\n\nInput\n5\n0 -1 1 2 -5\n\nOutput\n2\n\n\n\n-----Note-----\n\nNote to the first sample: there are two ways to change exactly one number so that the sequence met Vasya's condition. You can either replace the first number 1 by any negative number or replace the number -2 by any positive number."} +{"id": "01847", "text": "The black king is standing on a chess field consisting of 10^9 rows and 10^9 columns. We will consider the rows of the field numbered with integers from 1 to 10^9 from top to bottom. The columns are similarly numbered with integers from 1 to 10^9 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).\n\nYou know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers r_{i}, a_{i}, b_{i} (a_{i} \u2264 b_{i}), denoting that cells in columns from number a_{i} to number b_{i} inclusive in the r_{i}-th row are allowed.\n\nYour task is to find the minimum number of moves the king needs to get from square (x_0, y_0) to square (x_1, y_1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.\n\nLet us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.\n\n\n-----Input-----\n\nThe first line contains four space-separated integers x_0, y_0, x_1, y_1 (1 \u2264 x_0, y_0, x_1, y_1 \u2264 10^9), denoting the initial and the final positions of the king.\n\nThe second line contains a single integer n (1 \u2264 n \u2264 10^5), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers r_{i}, a_{i}, b_{i} (1 \u2264 r_{i}, a_{i}, b_{i} \u2264 10^9, a_{i} \u2264 b_{i}), denoting that cells in columns from number a_{i} to number b_{i} inclusive in the r_{i}-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.\n\nIt is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 10^5.\n\n\n-----Output-----\n\nIf there is no path between the initial and final position along allowed cells, print -1.\n\nOtherwise print a single integer \u2014 the minimum number of moves the king needs to get from the initial position to the final one.\n\n\n-----Examples-----\nInput\n5 7 6 11\n3\n5 3 8\n6 7 11\n5 2 5\n\nOutput\n4\n\nInput\n3 4 3 10\n3\n3 1 4\n4 5 9\n3 10 10\n\nOutput\n6\n\nInput\n1 1 2 10\n2\n1 1 3\n2 6 10\n\nOutput\n-1"} +{"id": "01848", "text": "There are n pictures delivered for the new exhibition. The i-th painting has beauty a_{i}. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.\n\nWe are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 \u2264 i \u2264 n - 1), such that a_{i} + 1 > a_{i}.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of painting.\n\nThe second line contains the sequence a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1000), where a_{i} means the beauty of the i-th painting.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the maximum possible number of neighbouring pairs, such that a_{i} + 1 > a_{i}, after the optimal rearrangement.\n\n\n-----Examples-----\nInput\n5\n20 30 10 50 40\n\nOutput\n4\n\nInput\n4\n200 100 100 200\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample, the optimal order is: 10, 20, 30, 40, 50.\n\nIn the second sample, the optimal order is: 100, 200, 100, 200."} +{"id": "01849", "text": "You wrote down all integers from $0$ to $10^n - 1$, padding them with leading zeroes so their lengths are exactly $n$. For example, if $n = 3$ then you wrote out 000, 001, ..., 998, 999.\n\nA block in an integer $x$ is a consecutive segment of equal digits that cannot be extended to the left or to the right.\n\nFor example, in the integer $00027734000$ there are three blocks of length $1$, one block of length $2$ and two blocks of length $3$.\n\nFor all integers $i$ from $1$ to $n$ count the number of blocks of length $i$ among the written down integers.\n\nSince these integers may be too large, print them modulo $998244353$.\n\n\n-----Input-----\n\nThe only line contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nIn the only line print $n$ integers. The $i$-th integer is equal to the number of blocks of length $i$.\n\nSince these integers may be too large, print them modulo $998244353$.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n10\n\nInput\n2\n\nOutput\n180 10"} +{"id": "01850", "text": "Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race.\n\nGiven the current ranking in the competition and points distribution for the next race, your task is to calculate the best possible ranking for a given astronaut after the next race. It's guaranteed that given astronaut will have unique number of points before the race.\n\n\n-----Input-----\n\nThe first line contains two integer numbers $N$ ($1 \\leq N \\leq 200000$) representing number of F1 astronauts, and current position of astronaut $D$ ($1 \\leq D \\leq N$) you want to calculate best ranking for (no other competitor will have the same number of points before the race).\n\nThe second line contains $N$ integer numbers $S_k$ ($0 \\leq S_k \\leq 10^8$, $k=1...N$), separated by a single space, representing current ranking of astronauts. Points are sorted in non-increasing order.\n\nThe third line contains $N$ integer numbers $P_k$ ($0 \\leq P_k \\leq 10^8$, $k=1...N$), separated by a single space, representing point awards for the next race. Points are sorted in non-increasing order, so winner of the race gets the maximum number of points.\n\n\n-----Output-----\n\nOutput contains one integer number \u2014 the best possible ranking for astronaut after the race. If multiple astronauts have the same score after the race, they all share the best ranking.\n\n\n-----Example-----\nInput\n4 3\n50 30 20 10\n15 10 7 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIf the third ranked astronaut wins the race, he will have 35 points. He cannot take the leading position, but he can overtake the second position if the second ranked astronaut finishes the race at the last position."} +{"id": "01851", "text": "Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The $i$-th page contains some mystery that will be explained on page $a_i$ ($a_i \\ge i$).\n\nIvan wants to read the whole book. Each day, he reads the first page he didn't read earlier, and continues to read the following pages one by one, until all the mysteries he read about are explained and clear to him (Ivan stops if there does not exist any page $i$ such that Ivan already has read it, but hasn't read page $a_i$). After that, he closes the book and continues to read it on the following day from the next page.\n\nHow many days will it take to read the whole book?\n\n\n-----Input-----\n\nThe first line contains single integer $n$ ($1 \\le n \\le 10^4$) \u2014 the number of pages in the book.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($i \\le a_i \\le n$), where $a_i$ is the number of page which contains the explanation of the mystery on page $i$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of days it will take to read the whole book.\n\n\n-----Example-----\nInput\n9\n1 3 3 6 7 6 8 8 9\n\nOutput\n4\n\n\n\n-----Note-----\n\nExplanation of the example test:\n\nDuring the first day Ivan will read only the first page. During the second day Ivan will read pages number $2$ and $3$. During the third day \u2014 pages $4$-$8$. During the fourth (and the last) day Ivan will read remaining page number $9$."} +{"id": "01852", "text": "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. \n\nYou 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.\n\n\n-----Input-----\n\nThe first line contains integer $n$ ($2 \\le n \\le 17$).\n\nThe $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$)\u00a0\u2014 meaning there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nPrint two lines.\n\nIn the first line, print a single integer\u00a0\u2014 the number of answers. If given tree cannot be made by McDic's generation, then print $0$.\n\nIn 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.\n\n\n-----Examples-----\nInput\n4\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\nOutput\n1\n3\n\nInput\n2\n1 2\n\nOutput\n2\n1 2\n\nInput\n3\n1 2\n2 3\n3 4\n4 5\n5 6\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, $3$ is the only possible answer. $8$ \n\nIn the second example, there are $2$ possible answers. [Image] \n\nIn the third example, the tree can't be generated by McDic's generation."} +{"id": "01853", "text": "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\".\n\nAfter 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.\n\nShe 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. \n\nNow 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. \n\nHelp Vasya find two such arrays of length $n$, or find out that there are no such arrays for his sets of pairs.\n\n\n-----Input-----\n\nThe first line of input contains two integers $n$, $m$\u00a0\u2014 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$).\n\nEach of the following $m$ lines contains two integers $a_i$, $b_i$ \u00a0\u2014 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.\n\n\n-----Output-----\n\nThe 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\".\n\nIf 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$.\n\n\n-----Examples-----\nInput\n1 0\n\nOutput\nNO\n\nInput\n3 1\n1 2\n\nOutput\nYES\n1 3 2 \n1 3 1 \n\nInput\n4 3\n1 2\n1 3\n2 4\n\nOutput\nYES\n1 3 4 2 \n1 3 4 1"} +{"id": "01854", "text": "Graph constructive problems are back! This time the graph you are asked to build should match the following properties.\n\nThe graph is connected if and only if there exists a path between every pair of vertices.\n\nThe 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.\n\nThe degree of a vertex is the number of edges incident to it.\n\nGiven 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. \n\nOutput the resulting graph or report that no solution exists.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($3 \\le n \\le 500$) \u2014 the number of vertices in the graph.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n - 1$) \u2014 the upper limits to vertex degrees.\n\n\n-----Output-----\n\nPrint \"NO\" if no graph can be constructed under the given conditions.\n\nOtherwise print \"YES\" and the diameter of the resulting graph in the first line.\n\nThe second line should contain a single integer $m$ \u2014 the number of edges in the resulting graph.\n\nThe $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$) \u2014 the description of the $i$-th edge. The graph should contain no multiple edges \u2014 for each pair $(x, y)$ you output, you should output no more pairs $(x, y)$ or $(y, x)$.\n\n\n-----Examples-----\nInput\n3\n2 2 2\n\nOutput\nYES 2\n2\n1 2\n2 3\n\nInput\n5\n1 4 1 1 1\n\nOutput\nYES 2\n4\n1 2\n3 2\n4 2\n5 2\n\nInput\n3\n1 1 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nHere are the graphs for the first two example cases. Both have diameter of $2$. [Image] $d_1 = 1 \\le a_1 = 2$\n\n$d_2 = 2 \\le a_2 = 2$\n\n$d_3 = 1 \\le a_3 = 2$ [Image] $d_1 = 1 \\le a_1 = 1$\n\n$d_2 = 4 \\le a_2 = 4$\n\n$d_3 = 1 \\le a_3 = 1$\n\n$d_4 = 1 \\le a_4 = 1$"} +{"id": "01855", "text": "You are given a permutation $p_1, p_2, \\ldots, p_n$ of integers from $1$ to $n$ and an integer $k$, such that $1 \\leq k \\leq n$. A permutation means that every number from $1$ to $n$ is contained in $p$ exactly once.\n\nLet's consider all partitions of this permutation into $k$ disjoint segments. Formally, a partition is a set of segments $\\{[l_1, r_1], [l_2, r_2], \\ldots, [l_k, r_k]\\}$, such that:\n\n $1 \\leq l_i \\leq r_i \\leq n$ for all $1 \\leq i \\leq k$; For all $1 \\leq j \\leq n$ there exists exactly one segment $[l_i, r_i]$, such that $l_i \\leq j \\leq r_i$. \n\nTwo partitions are different if there exists a segment that lies in one partition but not the other.\n\nLet's calculate the partition value, defined as $\\sum\\limits_{i=1}^{k} {\\max\\limits_{l_i \\leq j \\leq r_i} {p_j}}$, for all possible partitions of the permutation into $k$ disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by $998\\,244\\,353$.\n\n\n-----Input-----\n\nThe first line contains two integers, $n$ and $k$ ($1 \\leq k \\leq n \\leq 200\\,000$)\u00a0\u2014 the size of the given permutation and the number of segments in a partition.\n\nThe second line contains $n$ different integers $p_1, p_2, \\ldots, p_n$ ($1 \\leq p_i \\leq n$)\u00a0\u2014 the given permutation.\n\n\n-----Output-----\n\nPrint two integers\u00a0\u2014 the maximum possible partition value over all partitions of the permutation into $k$ disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo $998\\,244\\,353$.\n\nPlease note that you should only find the second value modulo $998\\,244\\,353$.\n\n\n-----Examples-----\nInput\n3 2\n2 1 3\n\nOutput\n5 2\n\nInput\n5 5\n2 1 5 3 4\n\nOutput\n15 1\n\nInput\n7 3\n2 7 3 1 5 4 6\n\nOutput\n18 6\n\n\n\n-----Note-----\n\nIn the first test, for $k = 2$, there exists only two valid partitions: $\\{[1, 1], [2, 3]\\}$ and $\\{[1, 2], [3, 3]\\}$. For each partition, the partition value is equal to $2 + 3 = 5$. So, the maximum possible value is $5$ and the number of partitions is $2$.\n\nIn the third test, for $k = 3$, the partitions with the maximum possible partition value are $\\{[1, 2], [3, 5], [6, 7]\\}$, $\\{[1, 3], [4, 5], [6, 7]\\}$, $\\{[1, 4], [5, 5], [6, 7]\\}$, $\\{[1, 2], [3, 6], [7, 7]\\}$, $\\{[1, 3], [4, 6], [7, 7]\\}$, $\\{[1, 4], [5, 6], [7, 7]\\}$. For all of them, the partition value is equal to $7 + 5 + 6 = 18$. \n\nThe partition $\\{[1, 2], [3, 4], [5, 7]\\}$, however, has the partition value $7 + 3 + 6 = 16$. This is not the maximum possible value, so we don't count it."} +{"id": "01856", "text": "One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $n$ passwords \u2014 strings, consists of small Latin letters.\n\nHacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $a$ and $b$ as follows: two passwords $a$ and $b$ are equivalent if there is a letter, that exists in both $a$ and $b$; two passwords $a$ and $b$ are equivalent if there is a password $c$ from the list, which is equivalent to both $a$ and $b$. \n\nIf a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.\n\nFor example, if the list contain passwords \"a\", \"b\", \"ab\", \"d\", then passwords \"a\", \"b\", \"ab\" are equivalent to each other, but the password \"d\" is not equivalent to any other password from list. In other words, if: admin's password is \"b\", then you can access to system by using any of this passwords: \"a\", \"b\", \"ab\"; admin's password is \"d\", then you can access to system by using only \"d\". \n\nOnly one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.\n\n\n-----Input-----\n\nThe first line contain integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 number of passwords in the list. Next $n$ lines contains passwords from the list \u2013 non-empty strings $s_i$, with length at most $50$ letters. Some of the passwords may be equal.\n\nIt is guaranteed that the total length of all passwords does not exceed $10^6$ letters. All of them consist only of lowercase Latin letters.\n\n\n-----Output-----\n\nIn a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.\n\n\n-----Examples-----\nInput\n4\na\nb\nab\nd\n\nOutput\n2\nInput\n3\nab\nbc\nabc\n\nOutput\n1\nInput\n1\ncodeforces\n\nOutput\n1\n\n\n-----Note-----\n\nIn the second example hacker need to use any of the passwords to access the system."} +{"id": "01857", "text": "The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n^2 intersections.\n\nThe city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.\n\nHelp the park administration count the number of ways to place the benches.\n\n\n-----Input-----\n\nThe only line of the input contains one integer n (5 \u2264 n \u2264 100) \u2014 the number of east to west paths and north to south paths.\n\n\n-----Output-----\n\nOutput one integer \u2014 the number of ways to place the benches.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n120"} +{"id": "01858", "text": "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$ \n\nHere $\\mathrm{sum}(S)$ denotes the sum of all elements present in set $S$ and $\\mathrm{gcd}$ means thegreatest common divisor.\n\nEvery integer number from $1$ to $n$ should be present in exactly one of $S_1$ or $S_2$.\n\n\n-----Input-----\n\nThe only line of the input contains a single integer $n$ ($1 \\le n \\le 45\\,000$)\n\n\n-----Output-----\n\nIf such partition doesn't exist, print \"No\" (quotes for clarity).\n\nOtherwise, print \"Yes\" (quotes for clarity), followed by two lines, describing $S_1$ and $S_2$ respectively.\n\nEach set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty.\n\nIf there are multiple possible partitions\u00a0\u2014 print any of them.\n\n\n-----Examples-----\nInput\n1\n\nOutput\nNo\nInput\n3\n\nOutput\nYes\n1 2\n2 1 3 \n\n\n\n-----Note-----\n\nIn the first example, there is no way to partition a single number into two non-empty sets, hence the answer is \"No\".\n\nIn 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."} +{"id": "01859", "text": "You are given an integer number $n$. The following algorithm is applied to it:\n\n if $n = 0$, then end algorithm; find the smallest prime divisor $d$ of $n$; subtract $d$ from $n$ and go to step $1$. \n\nDetermine the number of subtrations the algorithm will make.\n\n\n-----Input-----\n\nThe only line contains a single integer $n$ ($2 \\le n \\le 10^{10}$).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of subtractions the algorithm will make.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n1\n\nInput\n4\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example $5$ is the smallest prime divisor, thus it gets subtracted right away to make a $0$.\n\nIn the second example $2$ is the smallest prime divisor at both steps."} +{"id": "01860", "text": "The numbers of all offices in the new building of the Tax Office of IT City will have lucky numbers.\n\nLucky number is a number that consists of digits 7 and 8 only. Find the maximum number of offices in the new building of the Tax Office given that a door-plate can hold a number not longer than n digits.\n\n\n-----Input-----\n\nThe only line of input contains one integer n (1 \u2264 n \u2264 55) \u2014 the maximum length of a number that a door-plate can hold.\n\n\n-----Output-----\n\nOutput one integer \u2014 the maximum number of offices, than can have unique lucky numbers not longer than n digits.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n6"} +{"id": "01861", "text": "Bees Alice and Alesya gave beekeeper Polina famous card game \"Set\" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature \u2014 color, number, shape, and shading \u2014 the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.\n\n[Image]\n\nPolina came up with a new game called \"Hyperset\". In her game, there are $n$ cards with $k$ features, each feature has three possible values: \"S\", \"E\", or \"T\". The original \"Set\" game can be viewed as \"Hyperset\" with $k = 4$.\n\nSimilarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.\n\nUnfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.\n\n\n-----Input-----\n\nThe first line of each test contains two integers $n$ and $k$ ($1 \\le n \\le 1500$, $1 \\le k \\le 30$)\u00a0\u2014 number of cards and number of features.\n\nEach of the following $n$ lines contains a card description: a string consisting of $k$ letters \"S\", \"E\", \"T\". The $i$-th character of this string decribes the $i$-th feature of that card. All cards are distinct.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the number of ways to choose three cards that form a set.\n\n\n-----Examples-----\nInput\n3 3\nSET\nETS\nTSE\n\nOutput\n1\nInput\n3 4\nSETE\nETSE\nTSES\n\nOutput\n0\nInput\n5 4\nSETT\nTEST\nEEET\nESTE\nSTES\n\nOutput\n2\n\n\n-----Note-----\n\nIn the third example test, these two triples of cards are sets: \"SETT\", \"TEST\", \"EEET\" \"TEST\", \"ESTE\", \"STES\""} +{"id": "01862", "text": "Andryusha is an orderly boy and likes to keep things in their place.\n\nToday he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.\n\nAndryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time? \n\n\n-----Input-----\n\nThe first line contains the single integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of sock pairs.\n\nThe second line contains 2n integers x_1, x_2, ..., x_2n (1 \u2264 x_{i} \u2264 n), which describe the order in which Andryusha took the socks from the bag. More precisely, x_{i} means that the i-th sock Andryusha took out was from pair x_{i}.\n\nIt is guaranteed that Andryusha took exactly two socks of each pair.\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the maximum number of socks that were on the table at the same time.\n\n\n-----Examples-----\nInput\n1\n1 1\n\nOutput\n1\n\nInput\n3\n2 1 1 3 2 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.\n\nIn the second example Andryusha behaved as follows: Initially the table was empty, he took out a sock from pair 2 and put it on the table. Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table. Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe. Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table. Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe. Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe. Thus, at most two socks were on the table at the same time."} +{"id": "01863", "text": "The Bitlandians are quite weird people. They have very peculiar customs.\n\nAs 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.\n\nThe kids are excited because just as is customary, they're going to be paid for the job! \n\nOverall 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.\n\nUncle 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.\n\nHelp Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^6) \u2014 the number of eggs.\n\nNext n lines contain two integers a_{i} and g_{i} each (0 \u2264 a_{i}, g_{i} \u2264 1000;\u00a0a_{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.\n\n\n-----Output-----\n\nIf it is impossible to assign the painting, print \"-1\" (without quotes).\n\nOtherwise 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}| \u2264 500. \n\nIf there are several solutions, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n2\n1 999\n999 1\n\nOutput\nAG\n\nInput\n3\n400 600\n400 600\n400 600\n\nOutput\nAGA"} +{"id": "01864", "text": "A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimum unfortunate sum?\n\n\n-----Input-----\n\nThe first line contains number n (1 \u2264 n \u2264 1000) \u2014 the number of values of the banknotes that used in Geraldion. \n\nThe second line contains n distinct space-separated numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^6) \u2014 the values of the banknotes.\n\n\n-----Output-----\n\nPrint a single line \u2014 the minimum unfortunate sum. If there are no unfortunate sums, print - 1.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n\nOutput\n-1"} +{"id": "01865", "text": "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.\n\nNote that in this problem you do not have to minimize the number of swaps \u2014 your task is to find any sequence that is no longer than n.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 3000) \u2014 the number of array elements. The second line contains elements of array: a_0, a_1, ..., a_{n} - 1 ( - 10^9 \u2264 a_{i} \u2264 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.\n\n\n-----Output-----\n\nIn the first line print k (0 \u2264 k \u2264 n) \u2014 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 \u2264 i, j \u2264 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.\n\nIf there are multiple answers, print any of them. It is guaranteed that at least one answer exists.\n\n\n-----Examples-----\nInput\n5\n5 2 5 1 4\n\nOutput\n2\n0 3\n4 2\n\nInput\n6\n10 20 20 40 60 60\n\nOutput\n0\n\nInput\n2\n101 100\n\nOutput\n1\n0 1"} +{"id": "01866", "text": "You are given an integer $n$.\n\nYou 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.\n\nLet'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.\n\nLet's make an array $a_1, a_2, \\ldots, a_n$, where $a_i = i$ initially.\n\nYou 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$. \n\nIn 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$.\n\nIn the end, there should be at most two different numbers in the array $a$.\n\nIt should be true for any function $f$.\n\nFind any possible list of pairs. The number of pairs should not exceed $5 \\cdot 10^5$.\n\n\n-----Input-----\n\nThe single line contains a single integer $n$ ($1 \\leq n \\leq 15\\,000$).\n\n\n-----Output-----\n\nIn the first line print $q$ ($0 \\leq q \\leq 5 \\cdot 10^5$)\u00a0\u2014 the number of pairs.\n\nIn 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$).\n\nThe condition described in the statement should be satisfied.\n\nIf there exists multiple answers you can print any of them.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n1\n1 2\n\nInput\n4\n\nOutput\n2\n1 2\n3 4\n\n\n\n-----Note-----\n\nIn 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.\n\nIn 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."} +{"id": "01867", "text": "Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller.\n\nAmr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array.\n\nHelp Amr by choosing the smallest subsegment possible.\n\n\n-----Input-----\n\nThe first line contains one number n (1 \u2264 n \u2264 10^5), the size of the array.\n\nThe second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^6), representing elements of the array.\n\n\n-----Output-----\n\nOutput two integers l, r (1 \u2264 l \u2264 r \u2264 n), the beginning and the end of the subsegment chosen respectively.\n\nIf there are several possible answers you may output any of them. \n\n\n-----Examples-----\nInput\n5\n1 1 2 2 1\n\nOutput\n1 5\nInput\n5\n1 2 2 3 1\n\nOutput\n2 3\nInput\n6\n1 2 2 1 1 2\n\nOutput\n1 5\n\n\n-----Note-----\n\nA subsegment B of an array A from l to r is an array of size r - l + 1 where B_{i} = A_{l} + i - 1 for all 1 \u2264 i \u2264 r - l + 1"} +{"id": "01868", "text": "Consider some set of distinct characters $A$ and some string $S$, consisting of exactly $n$ characters, where each character is present in $A$.\n\nYou are given an array of $m$ integers $b$ ($b_1 < b_2 < \\dots < b_m$). \n\nYou are allowed to perform the following move on the string $S$:\n\n 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$. \n\nFor 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\".\n\nThe move can be performed arbitrary number of times (possibly zero). Any $i$ can be selected multiple times over these moves.\n\nLet'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$.\n\nThe task is simple. Count the number of distinct strings.\n\nThe answer can be huge enough, so calculate it modulo $998244353$.\n\n\n-----Input-----\n\nThe 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$) \u2014 the length of the strings, the size of the array $b$ and the size of the set $A$, respectively.\n\nThe 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$).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of distinct strings of length $n$ with characters from set $A$ modulo $998244353$.\n\n\n-----Examples-----\nInput\n3 1 2\n1\n\nOutput\n6\n\nInput\n9 2 26\n2 3\n\nOutput\n150352234\n\nInput\n12 3 1\n2 5 6\n\nOutput\n1\n\n\n\n-----Note-----\n\nHere 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. \n\n \"aaa\" \"aab\" = \"baa\" \"aba\" \"abb\" = \"bba\" \"bab\" \"bbb\""} +{"id": "01869", "text": "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$.\n\nYou 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$. \n\nWhat is the minimum number of operations required to delete all elements from the multiset?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 5000$).\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of operations required to delete all elements from the multiset.\n\n\n-----Examples-----\nInput\n4\n1 4 1 1\n\nOutput\n2\n\nInput\n5\n1 0 1 0 1\n\nOutput\n3"} +{"id": "01870", "text": "ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! \n\nMore formally, if you typed a word at second a and then the next word at second b, then if b - a \u2264 c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen.\n\nFor example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.\n\nYou're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.\n\n\n-----Input-----\n\nThe first line contains two integers n and c (1 \u2264 n \u2264 100 000, 1 \u2264 c \u2264 10^9)\u00a0\u2014 the number of words ZS the Coder typed and the crazy computer delay respectively.\n\nThe next line contains n integers t_1, t_2, ..., t_{n} (1 \u2264 t_1 < t_2 < ... < t_{n} \u2264 10^9), where t_{i} denotes the second when ZS the Coder typed the i-th word.\n\n\n-----Output-----\n\nPrint a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second t_{n}.\n\n\n-----Examples-----\nInput\n6 5\n1 3 8 14 19 20\n\nOutput\n3\nInput\n6 1\n1 3 5 7 9 10\n\nOutput\n2\n\n\n-----Note-----\n\nThe first sample is already explained in the problem statement.\n\nFor the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 \u2264 1."} +{"id": "01871", "text": "Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the i^{th} subject has c_{i} chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously.\n\nLet us say that his initial per chapter learning power of a subject is x hours. In other words he can learn a chapter of a particular subject in x hours.\n\nWell Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly). Note that his per chapter learning power can not be less than 1 hour.\n\nYou can teach him the n subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all the subjects and you will be free to do some enjoying task rather than teaching a dumb guy.\n\nPlease be careful that answer might not fit in 32 bit data type.\n\n\n-----Input-----\n\nThe first line will contain two space separated integers n, x (1 \u2264 n, x \u2264 10^5). The next line will contain n space separated integers: c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 10^5).\n\n\n-----Output-----\n\nOutput a single integer representing the answer to the problem.\n\n\n-----Examples-----\nInput\n2 3\n4 1\n\nOutput\n11\n\nInput\n4 2\n5 1 2 1\n\nOutput\n10\n\nInput\n3 3\n1 1 1\n\nOutput\n6\n\n\n\n-----Note-----\n\nLook at the first example. Consider the order of subjects: 1, 2. When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 \u00d7 1 = 2 hours. Hence you will need to spend 12 + 2 = 14 hours.\n\nConsider the order of subjects: 2, 1. When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take 3 \u00d7 1 = 3 hours to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject will take 2 \u00d7 4 = 8 hours. Hence you will need to spend 11 hours.\n\nSo overall, minimum of both the cases is 11 hours.\n\nLook at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter. When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours per chapter. In total it takes 6 hours."} +{"id": "01872", "text": "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.\n\nA \"star\" figure having n \u2265 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] \n\n\n-----Input-----\n\nThe only line of the input contains two integers n (5 \u2264 n < 10^9, n is prime) and r (1 \u2264 r \u2264 10^9) \u2014 the number of the star corners and the radius of the circumcircle correspondingly.\n\n\n-----Output-----\n\nOutput one number \u2014 the star area. The relative error of your answer should not be greater than 10^{ - 7}.\n\n\n-----Examples-----\nInput\n7 10\n\nOutput\n108.395919545675"} +{"id": "01873", "text": "Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres.\n\nIn the bookshop, Jack decides to buy two books of different genres.\n\nBased on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.\n\nThe books are given by indices of their genres. The genres are numbered from 1 to m.\n\n\n-----Input-----\n\nThe first line contains two positive integers n and m (2 \u2264 n \u2264 2\u00b710^5, 2 \u2264 m \u2264 10) \u2014 the number of books in the bookstore and the number of genres.\n\nThe second line contains a sequence a_1, a_2, ..., a_{n}, where a_{i} (1 \u2264 a_{i} \u2264 m) equals the genre of the i-th book.\n\nIt is guaranteed that for each genre there is at least one book of that genre.\n\n\n-----Output-----\n\nPrint the only integer \u2014 the number of ways in which Jack can choose books.\n\nIt is guaranteed that the answer doesn't exceed the value 2\u00b710^9.\n\n\n-----Examples-----\nInput\n4 3\n2 1 3 1\n\nOutput\n5\n\nInput\n7 4\n4 2 3 1 2 4 3\n\nOutput\n18\n\n\n\n-----Note-----\n\nThe answer to the first test sample equals 5 as Sasha can choose: the first and second books, the first and third books, the first and fourth books, the second and third books, the third and fourth books."} +{"id": "01874", "text": "IT City administration has no rest because of the fame of the Pyramids in Egypt. There is a project of construction of pyramid complex near the city in the place called Emerald Walley. The distinction of the complex is that its pyramids will be not only quadrangular as in Egypt but also triangular and pentagonal. Of course the amount of the city budget funds for the construction depends on the pyramids' volume. Your task is to calculate the volume of the pilot project consisting of three pyramids \u2014 one triangular, one quadrangular and one pentagonal.\n\nThe first pyramid has equilateral triangle as its base, and all 6 edges of the pyramid have equal length. The second pyramid has a square as its base and all 8 edges of the pyramid have equal length. The third pyramid has a regular pentagon as its base and all 10 edges of the pyramid have equal length. [Image] \n\n\n-----Input-----\n\nThe only line of the input contains three integers l_3, l_4, l_5 (1 \u2264 l_3, l_4, l_5 \u2264 1000) \u2014 the edge lengths of triangular, quadrangular and pentagonal pyramids correspondingly.\n\n\n-----Output-----\n\nOutput one number \u2014 the total volume of the pyramids. Absolute or relative error should not be greater than 10^{ - 9}.\n\n\n-----Examples-----\nInput\n2 5 3\n\nOutput\n38.546168065709"} +{"id": "01875", "text": "Iahub has drawn a set of n points in the cartesian plane which he calls \"special points\". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners). Please note that a quadrilateral doesn't have to be convex. A special quadrilateral is one which has all four vertices in the set of special points. Given the set of special points, please calculate the maximal area of a special quadrilateral. \n\n\n-----Input-----\n\nThe first line contains integer n (4 \u2264 n \u2264 300). Each of the next n lines contains two integers: x_{i}, y_{i} ( - 1000 \u2264 x_{i}, y_{i} \u2264 1000) \u2014 the cartesian coordinates of ith special point. It is guaranteed that no three points are on the same line. It is guaranteed that no two points coincide. \n\n\n-----Output-----\n\nOutput a single real number \u2014 the maximal area of a special quadrilateral. The answer will be considered correct if its absolute or relative error does't exceed 10^{ - 9}.\n\n\n-----Examples-----\nInput\n5\n0 0\n0 4\n4 0\n4 4\n2 3\n\nOutput\n16.000000\n\n\n-----Note-----\n\nIn the test example we can choose first 4 points to be the vertices of the quadrilateral. They form a square by side 4, so the area is 4\u00b74 = 16."} +{"id": "01876", "text": "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.\n\nYou 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] \n\nConsider 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]$.\n\nThere 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$.\n\n\n-----Input-----\n\nThe 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.\n\nEach 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).\n\n\n-----Output-----\n\nPrint the number of good sequences modulo $10^9 + 7$.\n\n\n-----Examples-----\nInput\n4 4\n1 2 1\n2 3 1\n3 4 1\n\nOutput\n252\nInput\n4 6\n1 2 0\n1 3 0\n1 4 0\n\nOutput\n0\nInput\n3 5\n1 2 1\n2 3 0\n\nOutput\n210\n\n\n-----Note-----\n\nIn 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]$ \n\nIn the second example, all edges are red, hence there aren't any good sequences."} +{"id": "01877", "text": "Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens to go from one kingdom to another. Each time a citizen passes through a gate, he has to pay one silver coin.\n\nThe world can be represented by the first quadrant of a plane and the wall is built along the identity line (i.e. the line with the equation x = y). Any point below the wall belongs to the first kingdom while any point above the wall belongs to the second kingdom. There is a gate at any integer point on the line (i.e. at points (0, 0), (1, 1), (2, 2), ...). The wall and the gates do not belong to any of the kingdoms. \n\nFafa is at the gate at position (0, 0) and he wants to walk around in the two kingdoms. He knows the sequence S of moves he will do. This sequence is a string where each character represents a move. The two possible moves Fafa will do are 'U' (move one step up, from (x, y) to (x, y + 1)) and 'R' (move one step right, from (x, y) to (x + 1, y)). \n\nFafa wants to know the number of silver coins he needs to pay to walk around the two kingdoms following the sequence S. Note that if Fafa visits a gate without moving from one kingdom to another, he pays no silver coins. Also assume that he doesn't pay at the gate at point (0, 0), i.\u00a0e. he is initially on the side he needs. \n\n\n-----Input-----\n\nThe first line of the input contains single integer n (1 \u2264 n \u2264 10^5) \u2014 the number of moves in the walking sequence.\n\nThe second line contains a string S of length n consisting of the characters 'U' and 'R' describing the required moves. Fafa will follow the sequence S in order from left to right.\n\n\n-----Output-----\n\nOn a single line, print one integer representing the number of silver coins Fafa needs to pay at the gates to follow the sequence S.\n\n\n-----Examples-----\nInput\n1\nU\n\nOutput\n0\n\nInput\n6\nRURUUR\n\nOutput\n1\n\nInput\n7\nURRRUUU\n\nOutput\n2\n\n\n\n-----Note-----\n\nThe figure below describes the third sample. The red arrows represent the sequence of moves Fafa will follow. The green gates represent the gates at which Fafa have to pay silver coins. [Image]"} +{"id": "01878", "text": "Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. \n\nIn this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100) \u2014 the number of rectangles.\n\nEach of the following n lines contains four integers x_1, y_1, x_2, y_2 (1 \u2264 x_1 \u2264 x_2 \u2264 100, 1 \u2264 y_1 \u2264 y_2 \u2264 100), where x_1 and y_1 are the number of the column and row of the lower left cell and x_2 and y_2 are the number of the column and row of the upper right cell of a rectangle.\n\n\n-----Output-----\n\nIn a single line print the sum of all values in the cells of the table.\n\n\n-----Examples-----\nInput\n2\n1 1 2 3\n2 2 3 3\n\nOutput\n10\n\nInput\n2\n1 1 3 3\n1 1 3 3\n\nOutput\n18\n\n\n\n-----Note-----\n\nNote to the first sample test:\n\nValues of the table in the first three rows and columns will be as follows:\n\n121\n\n121\n\n110\n\nSo, the sum of values will be equal to 10.\n\nNote to the second sample test:\n\nValues of the table in the first three rows and columns will be as follows:\n\n222\n\n222\n\n222\n\nSo, the sum of values will be equal to 18."} +{"id": "01879", "text": "The polar bears are going fishing. They plan to sail from (s_{x}, s_{y}) to (e_{x}, e_{y}). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). If the wind blows to the east, the boat will move to (x + 1, y). If the wind blows to the south, the boat will move to (x, y - 1). If the wind blows to the west, the boat will move to (x - 1, y). If the wind blows to the north, the boat will move to (x, y + 1). \n\nAlternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (e_{x}, e_{y})?\n\n\n-----Input-----\n\nThe first line contains five integers t, s_{x}, s_{y}, e_{x}, e_{y} (1 \u2264 t \u2264 10^5, - 10^9 \u2264 s_{x}, s_{y}, e_{x}, e_{y} \u2264 10^9). The starting location and the ending location will be different.\n\nThe second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: \"E\" (east), \"S\" (south), \"W\" (west) and \"N\" (north).\n\n\n-----Output-----\n\nIf they can reach (e_{x}, e_{y}) within t seconds, print the earliest time they can achieve it. Otherwise, print \"-1\" (without quotes).\n\n\n-----Examples-----\nInput\n5 0 0 1 1\nSESNW\n\nOutput\n4\n\nInput\n10 5 3 3 6\nNENSWESNEE\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.\n\nIn the second sample, they cannot sail to the destination."} +{"id": "01880", "text": "The protection of a popular program developed by one of IT City companies is organized the following way. After installation it outputs a random five digit number which should be sent in SMS to a particular phone number. In response an SMS activation code arrives.\n\nA young hacker Vasya disassembled the program and found the algorithm that transforms the shown number into the activation code. Note: it is clear that Vasya is a law-abiding hacker, and made it for a noble purpose \u2014 to show the developer the imperfection of their protection.\n\nThe found algorithm looks the following way. At first the digits of the number are shuffled in the following order . For example the shuffle of 12345 should lead to 13542. On the second stage the number is raised to the fifth power. The result of the shuffle and exponentiation of the number 12345 is 455\u00a0422\u00a0043\u00a0125\u00a0550\u00a0171\u00a0232. The answer is the 5 last digits of this result. For the number 12345 the answer should be 71232.\n\nVasya is going to write a keygen program implementing this algorithm. Can you do the same?\n\n\n-----Input-----\n\nThe only line of the input contains a positive integer five digit number for which the activation code should be found.\n\n\n-----Output-----\n\nOutput exactly 5 digits without spaces between them \u2014 the found activation code of the program.\n\n\n-----Examples-----\nInput\n12345\n\nOutput\n71232"} +{"id": "01881", "text": "Professor Ibrahim has prepared the final homework for his algorithm\u2019s class. He asked his students to implement the Posterization Image Filter.\n\nTheir 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).\n\nTo 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\u2019s 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.\n\nFinally, the students will replace the color of each pixel in the array with that color\u2019s assigned group key.\n\nTo better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $k$ to the right. \n\n [Image] \n\nTo 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.\n\n\n-----Input-----\n\nThe 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.\n\nThe 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.\n\n\n-----Output-----\n\nPrint $n$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.\n\n\n-----Examples-----\nInput\n4 3\n2 14 3 4\n\nOutput\n0 12 3 3\n\nInput\n5 2\n0 2 1 255 254\n\nOutput\n0 1 1 254 254\n\n\n\n-----Note-----\n\nOne possible way to group colors and assign keys for the first sample:\n\nColor $2$ belongs to the group $[0,2]$, with group key $0$.\n\nColor $14$ belongs to the group $[12,14]$, with group key $12$.\n\nColors $3$ and $4$ belong to group $[3, 5]$, with group key $3$.\n\nOther groups won't affect the result so they are not listed here."} +{"id": "01882", "text": "You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly t_{i} milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either.\n\nUnfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer a_{i} to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than a_{i} problems overall (including problem i).\n\nFormally, suppose you solve problems p_1, p_2, ..., p_{k} during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k \u2264 a_{p}_{j}.\n\nYou have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do.\n\n\n-----Input-----\n\nThe first line contains two integers n and T (1 \u2264 n \u2264 2\u00b710^5; 1 \u2264 T \u2264 10^9)\u00a0\u2014 the number of problems in the exam and the length of the exam in milliseconds, respectively.\n\nEach of the next n lines contains two integers a_{i} and t_{i} (1 \u2264 a_{i} \u2264 n; 1 \u2264 t_{i} \u2264 10^4). The problems are numbered from 1 to n.\n\n\n-----Output-----\n\nIn the first line, output a single integer s\u00a0\u2014 your maximum possible final score.\n\nIn the second line, output a single integer k (0 \u2264 k \u2264 n)\u00a0\u2014 the number of problems you should solve.\n\nIn the third line, output k distinct integers p_1, p_2, ..., p_{k} (1 \u2264 p_{i} \u2264 n)\u00a0\u2014 the indexes of problems you should solve, in any order.\n\nIf there are several optimal sets of problems, you may output any of them.\n\n\n-----Examples-----\nInput\n5 300\n3 100\n4 150\n4 80\n2 90\n2 300\n\nOutput\n2\n3\n3 1 4\n\nInput\n2 100\n1 787\n2 788\n\nOutput\n0\n0\n\n\nInput\n2 100\n2 42\n2 58\n\nOutput\n2\n2\n1 2\n\n\n\n-----Note-----\n\nIn the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points.\n\nIn the second example, the length of the exam is catastrophically not enough to solve even a single problem.\n\nIn the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile."} +{"id": "01883", "text": "Valera's finally decided to go on holiday! He packed up and headed for a ski resort.\n\nValera'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.\n\nValera 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.\n\nValera 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 \u2265 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 \u2264 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). \n\nHelp Valera. Find such path that meets all the criteria of our hero!\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of objects.\n\nThe second line contains n space-separated integers type_1, type_2, ..., type_{n} \u2014 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.\n\nThe third line of the input contains n space-separated integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 n) \u2014 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.\n\n\n-----Output-----\n\nIn the first line print k \u2014 the maximum possible path length for Valera. In the second line print k integers v_1, v_2, ..., v_{k} \u2014 the path. If there are multiple solutions, you can print any of them.\n\n\n-----Examples-----\nInput\n5\n0 0 0 0 1\n0 1 2 3 4\n\nOutput\n5\n1 2 3 4 5\n\nInput\n5\n0 0 1 0 1\n0 1 2 2 4\n\nOutput\n2\n4 5\n\nInput\n4\n1 0 0 0\n2 3 4 2\n\nOutput\n1\n1"} +{"id": "01884", "text": "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.\n\nIn 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.\n\nThe 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.\n\nEach 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.\n\nDetermine 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.\n\n\n-----Input-----\n\nThe 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$)\u00a0\u2014 the number of servers that the department may use, and resource units requirements for each of the services.\n\nThe second line contains $n$ space-separated integers $c_1, c_2, \\ldots, c_n$ ($1 \\leq c_i \\leq 10^9$)\u00a0\u2014 the number of resource units provided by each of the servers.\n\n\n-----Output-----\n\nIf it is impossible to deploy both services using the given servers, print the only word \"No\" (without the quotes).\n\nOtherwise print the word \"Yes\" (without the quotes). \n\nIn the second line print two integers $k_1$ and $k_2$ ($1 \\leq k_1, k_2 \\leq n$)\u00a0\u2014 the number of servers used for each of the services.\n\nIn the third line print $k_1$ integers, the indices of the servers that will be used for the first service.\n\nIn the fourth line print $k_2$ integers, the indices of the servers that will be used for the second service.\n\nNo 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.\n\n\n-----Examples-----\nInput\n6 8 16\n3 5 2 9 8 7\n\nOutput\nYes\n3 2\n1 2 6\n5 4\nInput\n4 20 32\n21 11 11 12\n\nOutput\nYes\n1 3\n1\n2 3 4\n\nInput\n4 11 32\n5 5 16 16\n\nOutput\nNo\n\nInput\n5 12 20\n7 8 4 11 9\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn 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.\n\nIn 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."} +{"id": "01885", "text": "One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate.\n\n\n-----Input-----\n\nThe only line of the input contains one integer n (7 \u2264 n \u2264 777) \u2014 the number of potential employees that sent resumes.\n\n\n-----Output-----\n\nOutput one integer \u2014 the number of different variants of group composition.\n\n\n-----Examples-----\nInput\n7\n\nOutput\n29"} +{"id": "01886", "text": "Capitalization is writing a word with its first letter as a capital letter. Your task is to capitalize the given word.\n\nNote, that during capitalization all the letters except the first one remains unchanged.\n\n\n-----Input-----\n\nA single line contains a non-empty word. This word consists of lowercase and uppercase English letters. The length of the word will not exceed 10^3.\n\n\n-----Output-----\n\nOutput the given word after capitalization.\n\n\n-----Examples-----\nInput\nApPLe\n\nOutput\nApPLe\n\nInput\nkonjac\n\nOutput\nKonjac"} +{"id": "01887", "text": "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.\n\n [Image] \n\nNow 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. \n\nDemid 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.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the number of students in each row.\n\nThe 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.\n\nThe 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.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum possible total height of players in a team Demid can choose.\n\n\n-----Examples-----\nInput\n5\n9 3 5 7 3\n5 8 1 4 5\n\nOutput\n29\n\nInput\n3\n1 2 9\n10 1 1\n\nOutput\n19\n\nInput\n1\n7\n4\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first example Demid can choose the following team as follows: [Image] \n\nIn the second example Demid can choose the following team as follows: [Image]"} +{"id": "01888", "text": "Imagine that there is a group of three friends: A, B and \u0421. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.\n\nThis task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 100;\u00a00 \u2264 m \u2264 10^4). The next m lines contain the debts. The i-th line contains three integers a_{i}, b_{i}, c_{i} (1 \u2264 a_{i}, b_{i} \u2264 n;\u00a0a_{i} \u2260 b_{i};\u00a01 \u2264 c_{i} \u2264 100), which mean that person a_{i} owes person b_{i} c_{i} rubles.\n\nAssume that the people are numbered by integers from 1 to n.\n\nIt is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum sum of debts in the optimal rearrangement.\n\n\n-----Examples-----\nInput\n5 3\n1 2 10\n2 3 1\n2 4 1\n\nOutput\n10\n\nInput\n3 0\n\nOutput\n0\n\nInput\n4 3\n1 2 1\n2 3 1\n3 1 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.\n\nIn the second sample, there are no debts.\n\nIn the third sample, you can annul all the debts."} +{"id": "01889", "text": "Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n \u00d7 m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes. [Image] \n\nThey play for q rounds. In each round, Mike chooses a bear (i, j) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears.\n\nScore of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row.\n\nSince bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round. \n\n\n-----Input-----\n\nThe first line of input contains three integers n, m and q (1 \u2264 n, m \u2264 500 and 1 \u2264 q \u2264 5000).\n\nThe next n lines contain the grid description. There are m integers separated by spaces in each line. Each of these numbers is either 0 (for mouth) or 1 (for eyes).\n\nThe next q lines contain the information about the rounds. Each of them contains two integers i and j (1 \u2264 i \u2264 n and 1 \u2264 j \u2264 m), the row number and the column number of the bear changing his state.\n\n\n-----Output-----\n\nAfter each round, print the current score of the bears.\n\n\n-----Examples-----\nInput\n5 4 5\n0 1 1 0\n1 0 0 1\n0 1 1 0\n1 0 0 1\n0 0 0 0\n1 1\n1 4\n1 1\n4 2\n4 3\n\nOutput\n3\n4\n3\n3\n4"} +{"id": "01890", "text": "There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his \"magic number\" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.\n\nNow Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (10^9 + 7). Two ways are different, if the set of deleted positions in s differs.\n\nLook at the input part of the statement, s is given in a special form.\n\n\n-----Input-----\n\nIn the first line you're given a string a (1 \u2264 |a| \u2264 10^5), containing digits only. In the second line you're given an integer k (1 \u2264 k \u2264 10^9). The plate s is formed by concatenating k copies of a together. That is n = |a|\u00b7k.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the required number of ways modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n1256\n1\n\nOutput\n4\n\nInput\n13990\n2\n\nOutput\n528\n\nInput\n555\n2\n\nOutput\n63\n\n\n\n-----Note-----\n\nIn the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.\n\nIn the second case, remember to concatenate the copies of a. The actual plate is 1399013990.\n\nIn the third case, except deleting all digits, any choice will do. Therefore there are 2^6 - 1 = 63 possible ways to delete digits."} +{"id": "01891", "text": "Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base.\n\nLet we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $2$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \\cdot n_a \\cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base.\n\n\n-----Input-----\n\nThe first line contains four integers $n$, $k$, $A$ and $B$ ($1 \\leq n \\leq 30$, $1 \\leq k \\leq 10^5$, $1 \\leq A,B \\leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question.\n\nThe second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \\ldots, a_{k}$ ($1 \\leq a_{i} \\leq 2^n$), where $a_{i}$ represents the position of avenger in the base.\n\n\n-----Output-----\n\nOutput one integer \u2014 the minimum power needed to destroy the avengers base.\n\n\n-----Examples-----\nInput\n2 2 1 2\n1 3\n\nOutput\n6\n\nInput\n3 2 1 2\n1 7\n\nOutput\n8\n\n\n\n-----Note-----\n\nConsider the first example.\n\nOne option for Thanos is to burn the whole base $1-4$ with power $2 \\cdot 2 \\cdot 4 = 16$.\n\nOtherwise he can divide the base into two parts $1-2$ and $3-4$.\n\nFor base $1-2$, he can either burn it with power $2 \\cdot 1 \\cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$.\n\nFor base $1-1$, he can burn it with power $2 \\cdot 1 \\cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. \n\nSimilarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$."} +{"id": "01892", "text": "In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of the block. Instead, code blocks are defined by indentation.\n\nWe will consider an extremely simplified subset of Python with only two types of statements.\n\nSimple statements are written in a single line, one per line. An example of a simple statement is assignment.\n\nFor statements are compound statements: they contain one or several other statements. For statement consists of a header written in a separate line which starts with \"for\" prefix, and loop body. Loop body is a block of statements indented one level further than the header of the loop. Loop body can contain both types of statements. Loop body can't be empty.\n\nYou are given a sequence of statements without indentation. Find the number of ways in which the statements can be indented to form a valid Python program.\n\n\n-----Input-----\n\nThe first line contains a single integer N (1 \u2264 N \u2264 5000)\u00a0\u2014 the number of commands in the program. N lines of the program follow, each line describing a single command. Each command is either \"f\" (denoting \"for statement\") or \"s\" (\"simple statement\"). It is guaranteed that the last line is a simple statement.\n\n\n-----Output-----\n\nOutput one line containing an integer - the number of ways the given sequence of statements can be indented modulo 10^9 + 7. \n\n\n-----Examples-----\nInput\n4\ns\nf\nf\ns\n\nOutput\n1\n\nInput\n4\nf\ns\nf\ns\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first test case, there is only one way to indent the program: the second for statement must be part of the body of the first one.\n\nsimple statement\n\nfor statement\n\n for statement\n\n simple statement\n\n\n\nIn the second test case, there are two ways to indent the program: the second for statement can either be part of the first one's body or a separate statement following the first one.\n\nfor statement\n\n simple statement\n\n for statement\n\n simple statement\n\n\n\nor\n\nfor statement\n\n simple statement\n\nfor statement\n\n simple statement"} +{"id": "01893", "text": "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.\n\nMoore'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.\n\nYou 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.\n\n\n-----Input-----\n\nThe only line of the input contains a pair of integers n (1000 \u2264 n \u2264 10\u00a0000) and t (0 \u2264 t \u2264 2\u00a0000\u00a0000\u00a0000)\u00a0\u2014 the number of transistors in the initial time and the number of seconds passed since the initial time.\n\n\n-----Output-----\n\nOutput one number \u2014 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}.\n\n\n-----Examples-----\nInput\n1000 1000000\n\nOutput\n1011.060722383550382782399454922040"} +{"id": "01894", "text": "Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path.\n\nJoe'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.\n\nNow 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.\n\nJoe 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). \n\nJoe calms down as soon as he reaches any cell of the first floor.\n\nThe figure below shows an example Joe's movements around the house.\n\n [Image] \n\nDetermine how many seconds Joe will need to calm down.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (2 \u2264 n \u2264 100, 1 \u2264 m \u2264 10^4).\n\nNext 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 \u2014 a line that consists of m characters: \".\" means an empty cell, \"+\" means bricks and \"#\" means a concrete wall.\n\nIt is guaranteed that the first cell of the n-th floor is empty.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of seconds Joe needs to reach the first floor; or else, print word \"Never\" (without the quotes), if it can never happen.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n3 5\n..+.#\n#+..+\n+.#+.\n\nOutput\n14\nInput\n4 10\n...+.##+.+\n+#++..+++#\n++.#++++..\n.+##.++#.+\n\nOutput\n42\n\nInput\n2 2\n..\n++\n\nOutput\nNever"} +{"id": "01895", "text": "Hiasat registered a new account in NeckoForces and when his friends found out about that, each one of them asked to use his name as Hiasat's handle.\n\nLuckily for Hiasat, he can change his handle in some points in time. Also he knows the exact moments friends will visit his profile page. Formally, you are given a sequence of events of two types:\n\n $1$\u00a0\u2014 Hiasat can change his handle. $2$ $s$\u00a0\u2014 friend $s$ visits Hiasat's profile. \n\nThe friend $s$ will be happy, if each time he visits Hiasat's profile his handle would be $s$.\n\nHiasat asks you to help him, find the maximum possible number of happy friends he can get.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 10^5, 1 \\le m \\le 40$)\u00a0\u2014 the number of events and the number of friends.\n\nThen $n$ lines follow, each denoting an event of one of two types: $1$\u00a0\u2014 Hiasat can change his handle. $2$ $s$\u00a0\u2014 friend $s$ ($1 \\le |s| \\le 40$) visits Hiasat's profile. \n\nIt's guaranteed, that each friend's name consists only of lowercase Latin letters.\n\nIt's guaranteed, that the first event is always of the first type and each friend will visit Hiasat's profile at least once.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum number of happy friends.\n\n\n-----Examples-----\nInput\n5 3\n1\n2 motarack\n2 mike\n1\n2 light\n\nOutput\n2\n\nInput\n4 3\n1\n2 alice\n2 bob\n2 tanyaromanova\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, the best way is to change the handle to the \"motarack\" in the first event and to the \"light\" in the fourth event. This way, \"motarack\" and \"light\" will be happy, but \"mike\" will not.\n\nIn the second example, you can choose either \"alice\", \"bob\" or \"tanyaromanova\" and only that friend will be happy."} +{"id": "01896", "text": "After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known \"Heroes of Might & Magic\". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.\n\nSome of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.\n\nIt is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell. [Image] \n\n\n-----Input-----\n\nThe only line of the input contains one integer n (0 \u2264 n \u2264 10^9).\n\n\n-----Output-----\n\nOutput one integer \u2014 the number of hexagons situated not farther than n cells away from a given cell.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n19"} +{"id": "01897", "text": "When Sasha was studying in the seventh grade, he started listening to music a lot. In order to evaluate which songs he likes more, he introduced the notion of the song's prettiness. The title of the song is a word consisting of uppercase Latin letters. The prettiness of the song is the prettiness of its title.\n\nLet's define the simple prettiness of a word as the ratio of the number of vowels in the word to the number of all letters in the word.\n\nLet's define the prettiness of a word as the sum of simple prettiness of all the substrings of the word.\n\nMore formally, let's define the function vowel(c) which is equal to 1, if c is a vowel, and to 0 otherwise. Let s_{i} be the i-th character of string s, and s_{i}..j be the substring of word s, staring at the i-th character and ending at the j-th character (s_{is}_{i} + 1... s_{j}, i \u2264 j).\n\nThen the simple prettiness of s is defined by the formula:$\\operatorname{simple}(s) = \\frac{\\sum_{i = 1}^{|s|} \\operatorname{vowel}(s_{i})}{|s|}$\n\nThe prettiness of s equals $\\sum_{1 \\leq i \\leq j \\leq|s|} \\operatorname{simple}(s_{i . . j})$\n\nFind the prettiness of the given song title.\n\nWe assume that the vowels are I, E, A, O, U, Y.\n\n\n-----Input-----\n\nThe input contains a single string s (1 \u2264 |s| \u2264 5\u00b710^5) \u2014 the title of the song.\n\n\n-----Output-----\n\nPrint the prettiness of the song with the absolute or relative error of at most 10^{ - 6}.\n\n\n-----Examples-----\nInput\nIEAIAIO\n\nOutput\n28.0000000\n\nInput\nBYOB\n\nOutput\n5.8333333\n\nInput\nYISVOWEL\n\nOutput\n17.0500000\n\n\n\n-----Note-----\n\nIn the first sample all letters are vowels. The simple prettiness of each substring is 1. The word of length 7 has 28 substrings. So, the prettiness of the song equals to 28."} +{"id": "01898", "text": "Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings.\n\nHulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, second one is love, third one is hate and so on...\n\nFor example if n = 1, then his feeling is \"I hate it\" or if n = 2 it's \"I hate that I love it\", and if n = 3 it's \"I hate that I love that I hate it\" and so on.\n\nPlease help Dr. Banner.\n\n\n-----Input-----\n\nThe only line of the input contains a single integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of layers of love and hate.\n\n\n-----Output-----\n\nPrint Dr.Banner's feeling in one line.\n\n\n-----Examples-----\nInput\n1\n\nOutput\nI hate it\n\nInput\n2\n\nOutput\nI hate that I love it\n\nInput\n3\n\nOutput\nI hate that I love that I hate it"} +{"id": "01899", "text": "You are a given a list of integers $a_1, a_2, \\ldots, a_n$ and $s$ of its segments $[l_j; r_j]$ (where $1 \\le l_j \\le r_j \\le n$).\n\nYou need to select exactly $m$ segments in such a way that the $k$-th order statistic of the multiset of $a_i$, where $i$ is contained in at least one segment, is the smallest possible. If it's impossible to select a set of $m$ segments in such a way that the multiset contains at least $k$ elements, print -1.\n\nThe $k$-th order statistic of a multiset is the value of the $k$-th element after sorting the multiset in non-descending order.\n\n\n-----Input-----\n\nThe first line contains four integers $n$, $s$, $m$ and $k$ ($1 \\le m \\le s \\le 1500$, $1 \\le k \\le n \\le 1500$)\u00a0\u2014 the size of the list, the number of segments, the number of segments to choose and the statistic number.\n\nThe second line contains $n$ integers $a_i$ ($1 \\le a_i \\le 10^9$)\u00a0\u2014 the values of the numbers in the list.\n\nEach of the next $s$ lines contains two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le n$)\u00a0\u2014 the endpoints of the segments.\n\nIt is possible that some segments coincide.\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the smallest possible $k$-th order statistic, or -1 if it's impossible to choose segments in a way that the multiset contains at least $k$ elements.\n\n\n-----Examples-----\nInput\n4 3 2 2\n3 1 3 2\n1 2\n2 3\n4 4\n\nOutput\n2\n\nInput\n5 2 1 1\n1 2 3 4 5\n2 4\n1 5\n\nOutput\n1\n\nInput\n5 3 3 5\n5 5 2 1 1\n1 2\n2 3\n3 4\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example, one possible solution is to choose the first and the third segment. Together they will cover three elements of the list (all, except for the third one). This way the $2$-nd order statistic for the covered elements is $2$.\n\n[Image]"} +{"id": "01900", "text": "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}.\n\nToday 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.\u00a0e. if we write down the cities where the participants came from, we get the sequence a_1, a_2, ..., a_{n} repeated m times).\n\nAfter 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.\n\nHelp 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.\n\n\n-----Input-----\n\nThe first line contains three integers n, k and m (1 \u2264 n \u2264 10^5, 2 \u2264 k \u2264 10^9, 1 \u2264 m \u2264 10^9).\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5), where a_{i} is the number of city, person from which must take seat i in the bus. \n\n\n-----Output-----\n\nOutput the number of remaining participants in the line.\n\n\n-----Examples-----\nInput\n4 2 5\n1 2 3 1\n\nOutput\n12\n\nInput\n1 9 10\n1\n\nOutput\n1\n\nInput\n3 2 10\n1 2 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn 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."} +{"id": "01901", "text": "Vova promised himself that he would never play computer games... But recently Firestorm \u2014 a well-known game developing company \u2014 published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.\n\nNow he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.\n\nVova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants c_{i} gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.\n\nThe quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?\n\nTake a look at the notes if you think you haven't understood the problem completely.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and m (1 \u2264 n \u2264 10^5, 0 \u2264 m \u2264 10^5) \u2014 the number of characters in Overcity and the number of pairs of friends.\n\nThe second line contains n integer numbers c_{i} (0 \u2264 c_{i} \u2264 10^9) \u2014 the amount of gold i-th character asks to start spreading the rumor.\n\nThen m lines follow, each containing a pair of numbers (x_{i}, y_{i}) which represent that characters x_{i} and y_{i} are friends (1 \u2264 x_{i}, y_{i} \u2264 n, x_{i} \u2260 y_{i}). It is guaranteed that each pair is listed at most once.\n\n\n-----Output-----\n\nPrint one number \u2014 the minimum amount of gold Vova has to spend in order to finish the quest.\n\n\n-----Examples-----\nInput\n5 2\n2 5 3 4 8\n1 4\n4 5\n\nOutput\n10\n\nInput\n10 0\n1 2 3 4 5 6 7 8 9 10\n\nOutput\n55\n\nInput\n10 5\n1 6 2 7 3 8 4 9 5 10\n1 2\n3 4\n5 6\n7 8\n9 10\n\nOutput\n15\n\n\n\n-----Note-----\n\nIn the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.\n\nIn the second example Vova has to bribe everyone.\n\nIn the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters."} +{"id": "01902", "text": "Arcady is a copywriter. His today's task is to type up an already well-designed story using his favorite text editor.\n\nArcady 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 \u00abcodeforces\u00bb, \u00abcoding\u00bb and \u00abcodeforces\u00bb once again, then there will be no autocompletion attempt for \u00abcod\u00bb, but if he proceeds with \u00abcode\u00bb, the editor will propose \u00abcodeforces\u00bb.\n\nWith 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?\n\nA 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 \u00abit's well-known that tic-tac-toe is a paper-and-pencil game for two players, x and o.\u00bb.\n\n\n-----Input-----\n\nThe only line contains Arcady's text, consisting only of lowercase latin letters, spaces, line feeds and the following punctuation signs: \u00ab.\u00bb, \u00ab,\u00bb, \u00ab?\u00bb, \u00ab!\u00bb, \u00ab'\u00bb and \u00ab-\u00bb. The total amount of symbols doesn't exceed 3\u00b710^5. It's guaranteed that all lines are non-empty.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of clicks.\n\n\n-----Examples-----\nInput\nsnow affects sports such as skiing, snowboarding, and snowmachine travel.\nsnowboarding is a recreational activity and olympic and paralympic sport.\n\nOutput\n141\n\nInput\n'co-co-co, codeforces?!'\n\nOutput\n25\n\nInput\nthun-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\nOutput\n183\n\n\n\n-----Note-----\n\nIn sample case one it's optimal to use autocompletion for the first instance of \u00absnowboarding\u00bb after typing up \u00absn\u00bb and for the second instance of \u00absnowboarding\u00bb after typing up \u00absnowb\u00bb. This will save 7 clicks.\n\nIn sample case two it doesn't matter whether to use autocompletion or not."} +{"id": "01903", "text": "Stepan is a very experienced olympiad participant. He has n cups for Physics olympiads and m cups for Informatics olympiads. Each cup is characterized by two parameters \u2014 its significance c_{i} and width w_{i}.\n\nStepan decided to expose some of his cups on a shelf with width d in such a way, that: there is at least one Physics cup and at least one Informatics cup on the shelf, the total width of the exposed cups does not exceed d, from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance x is exposed, then all the cups for this subject with significance greater than x must be exposed too). \n\nYour task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width d, considering all the rules described above. The total significance is the sum of significances of all the exposed cups.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and d (1 \u2264 n, m \u2264 100 000, 1 \u2264 d \u2264 10^9) \u2014 the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf.\n\nEach of the following n lines contains two integers c_{i} and w_{i} (1 \u2264 c_{i}, w_{i} \u2264 10^9) \u2014 significance and width of the i-th cup for Physics olympiads.\n\nEach of the following m lines contains two integers c_{j} and w_{j} (1 \u2264 c_{j}, w_{j} \u2264 10^9) \u2014 significance and width of the j-th cup for Informatics olympiads.\n\n\n-----Output-----\n\nPrint the maximum possible total significance, which Stepan can get exposing cups on the shelf with width d, considering all the rules described in the statement.\n\nIf there is no way to expose cups on the shelf, then print 0.\n\n\n-----Examples-----\nInput\n3 1 8\n4 2\n5 5\n4 2\n3 2\n\nOutput\n8\n\nInput\n4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4\n\nOutput\n11\n\nInput\n2 2 2\n5 3\n6 3\n4 2\n8 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8."} +{"id": "01904", "text": "Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length $n$ consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements. \n\nVasya doesn't want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is $0$, and removing $i$-th character increases the ambiguity by $a_i$ (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still $4$ even though you delete it from the string had).\n\nVasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!\n\nRecall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 10^5$) \u2014 the length of the statement.\n\nThe second line contains one string $s$ of length $n$, consisting of lowercase Latin letters \u2014 the statement written by Vasya.\n\nThe third line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 998244353$).\n\n\n-----Output-----\n\nPrint minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.\n\n\n-----Examples-----\nInput\n6\nhhardh\n3 2 9 11 7 1\n\nOutput\n5\n\nInput\n8\nhhzarwde\n3 2 6 9 4 8 7 1\n\nOutput\n4\n\nInput\n6\nhhaarr\n1 2 3 4 5 6\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, first two characters are removed so the result is ardh.\n\nIn the second example, $5$-th character is removed so the result is hhzawde.\n\nIn the third example there's no need to remove anything."} +{"id": "01905", "text": "Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.\n\nThat element can store information about the matrix of integers size n \u00d7 m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.\n\nArtem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.\n\nArtem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, m and q (1 \u2264 n, m \u2264 100, 1 \u2264 q \u2264 10 000)\u00a0\u2014 dimensions of the matrix and the number of turns in the experiment, respectively.\n\nNext q lines contain turns descriptions, one per line. Each description starts with an integer t_{i} (1 \u2264 t_{i} \u2264 3) that defines the type of the operation. For the operation of first and second type integer r_{i} (1 \u2264 r_{i} \u2264 n) or c_{i} (1 \u2264 c_{i} \u2264 m) follows, while for the operations of the third type three integers r_{i}, c_{i} and x_{i} (1 \u2264 r_{i} \u2264 n, 1 \u2264 c_{i} \u2264 m, - 10^9 \u2264 x_{i} \u2264 10^9) are given.\n\nOperation of the first type (t_{i} = 1) means that signal comes to the input corresponding to row r_{i}, that is it will shift cyclically. Operation of the second type (t_{i} = 2) means that column c_{i} will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row r_{i} and column c_{i} stores value x_{i}.\n\n\n-----Output-----\n\nPrint the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 10^9 by their absolute value.\n\nIf there are multiple valid solutions, output any of them.\n\n\n-----Examples-----\nInput\n2 2 6\n2 1\n2 2\n3 1 1 1\n3 2 2 2\n3 1 2 8\n3 2 1 8\n\nOutput\n8 2 \n1 8 \n\nInput\n3 3 2\n1 2\n3 2 2 5\n\nOutput\n0 0 0 \n0 0 5 \n0 0 0"} +{"id": "01906", "text": "IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following way. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is not divisible by any number from 2 to 10 every developer of this game gets a small bonus.\n\nA game designer Petya knows that the company is just about to release a new game that was partly developed by him. On the basis of his experience he predicts that n people will buy the game during the first month. Now Petya wants to determine how many times he will get the bonus. Help him to know it.\n\n\n-----Input-----\n\nThe only line of the input contains one integer n (1 \u2264 n \u2264 10^18) \u2014 the prediction on the number of people who will buy the game.\n\n\n-----Output-----\n\nOutput one integer showing how many numbers from 1 to n are not divisible by any number from 2 to 10.\n\n\n-----Examples-----\nInput\n12\n\nOutput\n2"} +{"id": "01907", "text": "The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it?\n\nThe 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 \u2264 i < j \u2264 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.\n\nTsukihi, 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] \n\nBut no one keeps moving for the whole night after all, so the whole night's time is divided into two halves \u2014 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] \n\nBy 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.\n\n\n-----Input-----\n\nThe first line of input contains a positive integer n (1 \u2264 n \u2264 1 000) \u2014 the number of dancers.\n\nThe 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 \u2264 x_{i}, y_{i} \u2264 10^6, 1 \u2264 r_{i} \u2264 10^6), describing a circular movement range centered at (x_{i}, y_{i}) with radius r_{i}.\n\n\n-----Output-----\n\nOutput one decimal number \u2014 the largest achievable sum of spaciousness over two halves of the night.\n\nThe 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}$.\n\n\n-----Examples-----\nInput\n5\n2 1 6\n0 4 1\n2 -1 3\n1 -2 1\n4 -1 1\n\nOutput\n138.23007676\n\nInput\n8\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\nOutput\n289.02652413\n\n\n\n-----Note-----\n\nThe first sample corresponds to the illustrations in the legend."} +{"id": "01908", "text": "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...\n\nThere 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$).\n\nLee 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.\n\nThe 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$.\n\nLee 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.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($2 \\le n \\le 10^5$; $1 \\le m \\le 2 \\cdot 10^5$)\u00a0\u2014 the number of different food types and the number of Lee's friends. \n\nThe second line contains $n$ integers $w_1, w_2, \\ldots, w_n$ ($0 \\le w_i \\le 10^6$)\u00a0\u2014 the number of plates of each food type.\n\nThe $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$)\u00a0\u2014 the favorite types of food of the $i$-th friend. \n\n\n-----Output-----\n\nIf Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive).\n\nAlso, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them.\n\n\n-----Examples-----\nInput\n3 3\n1 2 1\n1 2\n2 3\n1 3\n\nOutput\nALIVE\n3 2 1 \n\nInput\n3 2\n1 1 0\n1 2\n1 3\n\nOutput\nALIVE\n2 1 \n\nInput\n4 4\n1 2 0 1\n1 3\n1 2\n2 3\n2 4\n\nOutput\nALIVE\n1 3 2 4 \n\nInput\n5 5\n1 1 1 2 1\n3 4\n1 2\n2 3\n4 5\n4 5\n\nOutput\nALIVE\n5 4 1 3 2 \n\nInput\n4 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\nOutput\nDEAD\n\n\n\n-----Note-----\n\nIn the first example, any of the following orders of friends are correct : $[1, 3, 2]$, $[3, 1, 2]$, $[2, 3, 1]$, $[3, 2, 1]$.\n\nIn 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."} +{"id": "01909", "text": "You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.\n\nInna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.\n\nOverall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.\n\nInna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n, k\u00a0(1 \u2264 k \u2264 n \u2264 10^5). The second line contains n integers a_1, a_2, ..., a_{n}\u00a0(1 \u2264 a_{i} \u2264 10^3), where a_{i} is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.\n\nIt is guaranteed that n is divisible by k.\n\n\n-----Output-----\n\nIn a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.\n\n\n-----Examples-----\nInput\n6 2\n3 2 1 6 5 4\n\nOutput\n1\n\nInput\n10 5\n1 3 5 7 9 9 4 1 8 5\n\nOutput\n3\n\n\n\n-----Note-----\n\nExplanation of the first example.\n\nIf Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12. \n\nExplanation of the second example.\n\nIn the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3."} +{"id": "01910", "text": "To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes.\n\nThe parking lot before the office consists of one line of (2n - 2) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever.\n\nLooking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly n successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way.\n\n\n-----Input-----\n\nThe only line of the input contains one integer n (3 \u2264 n \u2264 30) \u2014 the amount of successive cars of the same make.\n\n\n-----Output-----\n\nOutput one integer \u2014 the number of ways to fill the parking lot by cars of four makes using the described way.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n24\n\n\n-----Note-----\n\nLet's denote car makes in the following way: A \u2014 Aston Martin, B \u2014 Bentley, M \u2014 Mercedes-Maybach, Z \u2014 Zaporozhets. For n = 3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM\n\nOriginally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City."} +{"id": "01911", "text": "You are given a sorted array $a_1, a_2, \\dots, a_n$ (for each index $i > 1$ condition $a_i \\ge a_{i-1}$ holds) and an integer $k$.\n\nYou are asked to divide this array into $k$ non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. \n\nLet $max(i)$ be equal to the maximum in the $i$-th subarray, and $min(i)$ be equal to the minimum in the $i$-th subarray. The cost of division is equal to $\\sum\\limits_{i=1}^{k} (max(i) - min(i))$. For example, if $a = [2, 4, 5, 5, 8, 11, 19]$ and we divide it into $3$ subarrays in the following way: $[2, 4], [5, 5], [8, 11, 19]$, then the cost of division is equal to $(4 - 2) + (5 - 5) + (19 - 8) = 13$.\n\nCalculate the minimum cost you can obtain by dividing the array $a$ into $k$ non-empty consecutive subarrays. \n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 3 \\cdot 10^5$).\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($ 1 \\le a_i \\le 10^9$, $a_i \\ge a_{i-1}$). \n\n\n-----Output-----\n\nPrint the minimum cost you can obtain by dividing the array $a$ into $k$ nonempty consecutive subarrays. \n\n\n-----Examples-----\nInput\n6 3\n4 8 15 16 23 42\n\nOutput\n12\n\nInput\n4 4\n1 3 3 7\n\nOutput\n0\n\nInput\n8 1\n1 1 2 3 5 8 13 21\n\nOutput\n20\n\n\n\n-----Note-----\n\nIn the first test we can divide array $a$ in the following way: $[4, 8, 15, 16], [23], [42]$."} +{"id": "01912", "text": "Boboniu gives you\n\n $r$ red balls, $g$ green balls, $b$ blue balls, $w$ white balls. \n\nHe allows you to do the following operation as many times as you want: \n\n Pick a red ball, a green ball, and a blue ball and then change their color to white. \n\nYou should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. \n\n\n-----Input-----\n\nThe first line contains one integer $T$ ($1\\le T\\le 100$) denoting the number of test cases.\n\nFor each of the next $T$ cases, the first line contains four integers $r$, $g$, $b$ and $w$ ($0\\le r,g,b,w\\le 10^9$).\n\n\n-----Output-----\n\nFor each test case, print \"Yes\" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print \"No\".\n\n\n-----Example-----\nInput\n4\n0 1 1 1\n8 1 9 3\n0 0 0 0\n1000000000 1000000000 1000000000 1000000000\n\nOutput\nNo\nYes\nYes\nYes\n\n\n\n-----Note-----\n\nIn the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome.\n\nIn the second test case, after doing one operation, changing $(8,1,9,3)$ to $(7,0,8,6)$, one of those possible palindromes may be \"rrrwwwbbbbrbbbbwwwrrr\".\n\nA palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, \"rggbwbggr\", \"b\", \"gg\" are palindromes while \"rgbb\", \"gbbgr\" are not. Notice that an empty word, phrase, or sequence is palindrome."} +{"id": "01913", "text": "It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse!\n\nThere are exactly n distinct countries in the world and the i-th country added a_{i} tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful.\n\nYour task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case.\n\n\n-----Input-----\n\nThe first line of the input contains the number of countries n (1 \u2264 n \u2264 100 000). The second line contains n non-negative integers a_{i} without leading zeroes\u00a0\u2014 the number of tanks of the i-th country.\n\nIt is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.\n\n\n-----Output-----\n\nPrint a single number without leading zeroes\u00a0\u2014 the product of the number of tanks presented by each country.\n\n\n-----Examples-----\nInput\n3\n5 10 1\n\nOutput\n50\nInput\n4\n1 1 10 11\n\nOutput\n110\nInput\n5\n0 3 1 100 1\n\nOutput\n0\n\n\n-----Note-----\n\nIn sample 1 numbers 10 and 1 are beautiful, number 5 is not not.\n\nIn sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful.\n\nIn sample 3 number 3 is not beautiful, all others are beautiful."} +{"id": "01914", "text": "You are given a string $t$ and $n$ strings $s_1, s_2, \\dots, s_n$. All strings consist of lowercase Latin letters.\n\nLet $f(t, s)$ be the number of occurences of string $s$ in string $t$. For example, $f('\\text{aaabacaa}', '\\text{aa}') = 3$, and $f('\\text{ababa}', '\\text{aba}') = 2$.\n\nCalculate the value of $\\sum\\limits_{i=1}^{n} \\sum\\limits_{j=1}^{n} f(t, s_i + s_j)$, where $s + t$ is the concatenation of strings $s$ and $t$. Note that if there are two pairs $i_1$, $j_1$ and $i_2$, $j_2$ such that $s_{i_1} + s_{j_1} = s_{i_2} + s_{j_2}$, you should include both $f(t, s_{i_1} + s_{j_1})$ and $f(t, s_{i_2} + s_{j_2})$ in answer.\n\n\n-----Input-----\n\nThe first line contains string $t$ ($1 \\le |t| \\le 2 \\cdot 10^5$).\n\nThe second line contains integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$).\n\nEach of next $n$ lines contains string $s_i$ ($1 \\le |s_i| \\le 2 \\cdot 10^5$).\n\nIt is guaranteed that $\\sum\\limits_{i=1}^{n} |s_i| \\le 2 \\cdot 10^5$. All strings consist of lowercase English letters.\n\n\n-----Output-----\n\nPrint one integer \u2014 the value of $\\sum\\limits_{i=1}^{n} \\sum\\limits_{j=1}^{n} f(t, s_i + s_j)$.\n\n\n-----Examples-----\nInput\naaabacaa\n2\na\naa\n\nOutput\n5\n\nInput\naaabacaa\n4\na\na\na\nb\n\nOutput\n33"} +{"id": "01915", "text": "An n \u00d7 n table a is defined as follows:\n\n The first row and the first column contain ones, that is: a_{i}, 1 = a_{1, }i = 1 for all i = 1, 2, ..., n. Each of the remaining numbers in the table is equal to the sum of the number above it and the number to the left of it. In other words, the remaining elements are defined by the formula a_{i}, j = a_{i} - 1, j + a_{i}, j - 1. \n\nThese conditions define all the values in the table.\n\nYou are given a number n. You need to determine the maximum value in the n \u00d7 n table defined by the rules above.\n\n\n-----Input-----\n\nThe only line of input contains a positive integer n (1 \u2264 n \u2264 10) \u2014 the number of rows and columns of the table.\n\n\n-----Output-----\n\nPrint a single line containing a positive integer m \u2014 the maximum value in the table.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\nInput\n5\n\nOutput\n70\n\n\n-----Note-----\n\nIn the second test the rows of the table look as follows: {1, 1, 1, 1, 1}, {1, 2, 3, 4, 5}, {1, 3, 6, 10, 15}, {1, 4, 10, 20, 35}, {1, 5, 15, 35, 70}."} +{"id": "01916", "text": "Boboniu likes bit operations. He wants to play a game with you.\n\nBoboniu gives you two sequences of non-negative integers $a_1,a_2,\\ldots,a_n$ and $b_1,b_2,\\ldots,b_m$.\n\nFor each $i$ ($1\\le i\\le n$), you're asked to choose a $j$ ($1\\le j\\le m$) and let $c_i=a_i\\& b_j$, where $\\&$ denotes the bitwise AND operation. Note that you can pick the same $j$ for different $i$'s.\n\nFind the minimum possible $c_1 | c_2 | \\ldots | c_n$, where $|$ denotes the bitwise OR operation.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1\\le n,m\\le 200$).\n\nThe next line contains $n$ integers $a_1,a_2,\\ldots,a_n$ ($0\\le a_i < 2^9$).\n\nThe next line contains $m$ integers $b_1,b_2,\\ldots,b_m$ ($0\\le b_i < 2^9$).\n\n\n-----Output-----\n\nPrint one integer: the minimum possible $c_1 | c_2 | \\ldots | c_n$.\n\n\n-----Examples-----\nInput\n4 2\n2 6 4 0\n2 4\n\nOutput\n2\nInput\n7 6\n1 9 1 9 8 1 0\n1 1 4 5 1 4\n\nOutput\n0\nInput\n8 5\n179 261 432 162 82 43 10 38\n379 357 202 184 197\n\nOutput\n147\n\n\n-----Note-----\n\nFor the first example, we have $c_1=a_1\\& b_2=0$, $c_2=a_2\\& b_1=2$, $c_3=a_3\\& b_1=0$, $c_4 = a_4\\& b_1=0$.Thus $c_1 | c_2 | c_3 |c_4 =2$, and this is the minimal answer we can get."} +{"id": "01917", "text": "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.)\n\nYou 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\u00e9sum\u00e9 has limited space, but you want to carefully choose them in such a way that maximizes your chances of getting hired.\n\nYou 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:\n\n$$ f(b_1,\\ldots,b_n)=\\sum\\limits_{i=1}^n b_i(a_i-b_i^2). $$\n\nHere, $b_i$ denotes the number of projects of type $i$ you include in your r\u00e9sum\u00e9. Of course, you cannot include more projects than you have completed, so you require $0\\le b_i \\le a_i$ for all $i$.\n\nYour r\u00e9sum\u00e9 only has enough room for $k$ projects, and you will absolutely not be hired if your r\u00e9sum\u00e9 has empty space, so you require $\\sum\\limits_{i=1}^n b_i=k$.\n\nFind values for $b_1,\\ldots, b_n$ that maximize the value of $f(b_1,\\ldots,b_n)$ while satisfying the above two constraints.\n\n\n-----Input-----\n\nThe 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$)\u00a0\u2014 the number of types of programming projects and the r\u00e9sum\u00e9 size, respectively.\n\nThe next line contains $n$ integers $a_1,\\ldots,a_n$ ($1\\le a_i\\le 10^9$)\u00a0\u2014 $a_i$ is equal to the number of completed projects of type $i$.\n\n\n-----Output-----\n\nIn 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.\n\nNote that you do not have to output the value $f(b_1,\\ldots,b_n)$.\n\n\n-----Examples-----\nInput\n10 32\n1 2 3 4 5 5 5 5 5 5\n\nOutput\n1 2 3 3 3 4 4 4 4 4 \n\nInput\n5 8\n4 4 8 2 1\n\nOutput\n2 2 2 1 1 \n\n\n\n-----Note-----\n\nFor 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$.\n\nFor the second test, the optimal answer is $f=9$."} +{"id": "01918", "text": "Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength p_{i}.\n\nThe way to split up game pieces is split into several steps:\n\n First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. Alice will get all the pieces marked A and Bob will get all the pieces marked B. \n\nThe strength of a player is then the sum of strengths of the pieces in the group.\n\nGiven Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 5\u00b710^5) \u2014 the number of game pieces.\n\nThe second line contains n integers p_{i} (1 \u2264 p_{i} \u2264 10^9) \u2014 the strength of the i-th piece.\n\nThe third line contains n characters A or B \u2014 the assignment of teams after the first step (after Alice's step).\n\n\n-----Output-----\n\nPrint the only integer a \u2014 the maximum strength Bob can achieve.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\nABABA\n\nOutput\n11\n\nInput\n5\n1 2 3 4 5\nAAAAA\n\nOutput\n15\n\nInput\n1\n1\nB\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample Bob should flip the suffix of length one.\n\nIn the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.\n\nIn the third sample Bob should do nothing."} +{"id": "01919", "text": "There is a legend in the IT City college. A student that failed to answer all questions on the game theory exam is given one more chance by his professor. The student has to play a game with the professor.\n\nThe game is played on a square field consisting of n \u00d7 n cells. Initially all cells are empty. On each turn a player chooses and paint an empty cell that has no common sides with previously painted cells. Adjacent corner of painted cells is allowed. On the next turn another player does the same, then the first one and so on. The player with no cells to paint on his turn loses.\n\nThe professor have chosen the field size n and allowed the student to choose to be the first or the second player in the game. What should the student choose to win the game? Both players play optimally.\n\n\n-----Input-----\n\nThe only line of the input contains one integer n (1 \u2264 n \u2264 10^18) \u2014 the size of the field.\n\n\n-----Output-----\n\nOutput number 1, if the player making the first turn wins when both players play optimally, otherwise print number 2.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\nInput\n2\n\nOutput\n2"} +{"id": "01920", "text": "Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from a_{i} to b_{i}. Of course, Famil Door wants to have as many friends celebrating together with him as possible.\n\nFar cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.\n\nFamil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 5000)\u00a0\u2014 then number of Famil Door's friends.\n\nThen follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers a_{i} and b_{i} (1 \u2264 a_{i} \u2264 b_{i} \u2264 366), providing that the i-th friend can come to the party from day a_{i} to day b_{i} inclusive.\n\n\n-----Output-----\n\nPrint the maximum number of people that may come to Famil Door's party.\n\n\n-----Examples-----\nInput\n4\nM 151 307\nF 343 352\nF 117 145\nM 24 128\n\nOutput\n2\n\nInput\n6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample, friends 3 and 4 can come on any day in range [117, 128].\n\nIn the second sample, friends with indices 3, 4, 5 and 6 can come on day 140."} +{"id": "01921", "text": "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.\n\nLet'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.\n\nHelp Yura to find the smallest time needed to get home.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$\u00a0\u2014 the size of the city and the number of instant-movement locations ($1 \\le n \\le 10^9$, $0 \\le m \\le 10^5$).\n\nThe next line contains four integers $s_x$ $s_y$ $f_x$ $f_y$\u00a0\u2014 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$).\n\nEach of the next $m$ lines contains two integers $x_i$ $y_i$\u00a0\u2014 coordinates of the $i$-th instant-movement location ($1 \\le x_i, y_i \\le n$).\n\n\n-----Output-----\n\nIn the only line print the minimum time required to get home.\n\n\n-----Examples-----\nInput\n5 3\n1 1 5 5\n1 2\n4 1\n3 3\n\nOutput\n5\n\nInput\n84 5\n67 59 41 2\n39 56\n7 2\n15 3\n74 18\n22 7\n\nOutput\n42\n\n\n\n-----Note-----\n\nIn 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)$."} +{"id": "01922", "text": "There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.\nThe front and back sides of these cards can be distinguished, and initially every card faces up.\nWe will perform the following operation once for each square contains a card:\n - For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.\nIt can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.\nFind the number of cards that face down after all the operations.\n\n-----Constraints-----\n - 1 \\leq N,M \\leq 10^9\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\n\n-----Output-----\nPrint the number of cards that face down after all the operations.\n\n-----Sample Input-----\n2 2\n\n-----Sample Output-----\n0\n\nWe will flip every card in any of the four operations. Thus, after all the operations, all cards face up."} +{"id": "01923", "text": "Snuke is having a barbeque party.\nAt the party, he will make N servings of Skewer Meal.\nExample of a serving of Skewer Meal\nHe has a stock of 2N skewers, all of which will be used in Skewer Meal. The length of the i-th skewer is L_i.\nAlso, he has an infinite supply of ingredients.\nTo make a serving of Skewer Meal, he picks 2 skewers and threads ingredients onto those skewers.\nLet the length of the shorter skewer be x, then the serving can hold the maximum of x ingredients.\nWhat is the maximum total number of ingredients that his N servings of Skewer Meal can hold, if he uses the skewers optimally?\n\n-----Constraints-----\n - 1\u2266N\u2266100\n - 1\u2266L_i\u2266100\n - For each i, L_i is an integer.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\nL_1 L_2 ... L_{2N}\n\n-----Output-----\nPrint the maximum total number of ingredients that Snuke's N servings of Skewer Meal can hold.\n\n-----Sample Input-----\n2\n1 3 1 2\n\n-----Sample Output-----\n3\n\nIf he makes a serving using the first and third skewers, and another using the second and fourth skewers, each serving will hold 1 and 2 ingredients, for the total of 3."} +{"id": "01924", "text": "Snuke is standing on a two-dimensional plane. In one operation, he can move by 1 in the positive x-direction, or move by 1 in the positive y-direction.\nLet us define a function f(r, c) as follows:\n - f(r,c) := (The number of paths from the point (0, 0) to the point (r, c) that Snuke can trace by repeating the operation above)\nGiven are integers r_1, r_2, c_1, and c_2.\nFind the sum of f(i, j) over all pair of integers (i, j) such that r_1 \u2264 i \u2264 r_2 and c_1 \u2264 j \u2264 c_2, and compute this value modulo (10^9+7).\n\n-----Constraints-----\n - 1 \u2264 r_1 \u2264 r_2 \u2264 10^6\n - 1 \u2264 c_1 \u2264 c_2 \u2264 10^6\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nr_1 c_1 r_2 c_2\n\n-----Output-----\nPrint the sum of f(i, j) modulo (10^9+7).\n\n-----Sample Input-----\n1 1 2 2\n\n-----Sample Output-----\n14\n\nFor example, there are two paths from the point (0, 0) to the point (1, 1): (0,0) \u2192 (0,1) \u2192 (1,1) and (0,0) \u2192 (1,0) \u2192 (1,1), so f(1,1)=2.\nSimilarly, f(1,2)=3, f(2,1)=3, and f(2,2)=6. Thus, the sum is 14."} +{"id": "01925", "text": "Given are integers A, B, and N.\nFind the maximum possible value of floor(Ax/B) - A \u00d7 floor(x/B) for a non-negative integer x not greater than N.\nHere floor(t) denotes the greatest integer not greater than the real number t.\n\n-----Constraints-----\n - 1 \u2264 A \u2264 10^{6}\n - 1 \u2264 B \u2264 10^{12}\n - 1 \u2264 N \u2264 10^{12}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B N\n\n-----Output-----\nPrint the maximum possible value of floor(Ax/B) - A \u00d7 floor(x/B) for a non-negative integer x not greater than N, as an integer.\n\n-----Sample Input-----\n5 7 4\n\n-----Sample Output-----\n2\n\nWhen x=3, floor(Ax/B)-A\u00d7floor(x/B) = floor(15/7) - 5\u00d7floor(3/7) = 2. This is the maximum value possible."} +{"id": "01926", "text": "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.\n\nThe 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.\n\nAndrew 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).\n\nHelp Andrew cope with the task!\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 2\u00b710^5).\n\nThe second line contains n space-separated integers a_1, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nin a single line print n - 1 integers, separate the consecutive numbers with a single space \u2014 the number of elements for which the property of the k-ary heap is violated, for k = 1, 2, ..., n - 1.\n\n\n-----Examples-----\nInput\n5\n1 5 4 3 2\n\nOutput\n3 2 1 0\n\nInput\n6\n2 2 2 2 2 2\n\nOutput\n0 0 0 0 0\n\n\n\n-----Note-----\n\nPictures 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] \n\nIn the second sample all elements are equal, so the property holds for all pairs."} +{"id": "01927", "text": "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$.\n\nRecall 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.\n\nSo 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.\n\nInitially, Polycarp's recent chat list $p$ looks like $1, 2, \\dots, n$ (in other words, it is an identity permutation).\n\nAfter 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.\n\nFor 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]$. \n\nFor 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.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 3 \\cdot 10^5$) \u2014 the number of Polycarp's friends and the number of received messages, respectively.\n\nThe second line contains $m$ integers $a_1, a_2, \\dots, a_m$ ($1 \\le a_i \\le n$) \u2014 the descriptions of the received messages.\n\n\n-----Output-----\n\nPrint $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.\n\n\n-----Examples-----\nInput\n5 4\n3 5 1 4\n\nOutput\n1 3\n2 5\n1 4\n1 5\n1 5\n\nInput\n4 3\n1 2 4\n\nOutput\n1 3\n1 2\n3 4\n1 4\n\n\n\n-----Note-----\n\nIn 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]$ \n\nSo, 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)$.\n\nIn 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]$"} +{"id": "01928", "text": "Inna loves sweets very much. That's why she decided to play a game called \"Sweet Matrix\".\n\nInna sees an n \u00d7 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.\n\nEach 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.\n\nHelp Inna to minimize the penalty in the game.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, m and k (1 \u2264 n, m \u2264 50, 1 \u2264 k \u2264 n\u00b7m).\n\n\n-----Output-----\n\nIn the first line print an integer \u2014 Inna's minimum penalty in the game.\n\nIn 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.\n\nPlease follow the output format strictly! If your program passes the first pretest, then the output format is correct.\n\n\n-----Examples-----\nInput\n4 4 4\n\nOutput\n8\n(1,1) (2,1) (2,2)\n(1,1) (1,2)\n(1,1) (2,1)\n(1,1)\n\n\n\n-----Note-----\n\nNote to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it \u2014 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.\n\nNote 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."} +{"id": "01929", "text": "The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city.\n\nFor this reason, he made the n prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime he/she has committed. The greater the number, the more severe his/her crime was.\n\nThen, the mayor told you to choose the c prisoners, who will be transferred to the other prison. He also imposed two conditions. They are,\n\n The chosen c prisoners has to form a contiguous segment of prisoners. Any of the chosen prisoner's crime level should not be greater then t. Because, that will make the prisoner a severe criminal and the mayor doesn't want to take the risk of his running away during the transfer. \n\nFind the number of ways you can choose the c prisoners.\n\n\n-----Input-----\n\nThe first line of input will contain three space separated integers n\u00a0(1 \u2264 n \u2264 2\u00b710^5), t\u00a0(0 \u2264 t \u2264 10^9) and c\u00a0(1 \u2264 c \u2264 n). The next line will contain n space separated integers, the i^{th} integer is the severity i^{th} prisoner's crime. The value of crime severities will be non-negative and will not exceed 10^9. \n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of ways you can choose the c prisoners.\n\n\n-----Examples-----\nInput\n4 3 3\n2 3 1 1\n\nOutput\n2\n\nInput\n1 1 1\n2\n\nOutput\n0\n\nInput\n11 4 2\n2 2 0 7 3 2 2 4 9 1 4\n\nOutput\n6"} +{"id": "01930", "text": "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. An inversion in a permutation $p$ is a pair of indices $(i, j)$ such that $i > j$ and $a_i < a_j$. For example, a permutation $[4, 1, 3, 2]$ contains $4$ inversions: $(2, 1)$, $(3, 1)$, $(4, 1)$, $(4, 3)$.\n\nYou are given a permutation $p$ of size $n$. However, the numbers on some positions are replaced by $-1$. Let the valid permutation be such a replacement of $-1$ in this sequence back to numbers from $1$ to $n$ in such a way that the resulting sequence is a permutation of size $n$.\n\nThe given sequence was turned into a valid permutation randomly with the equal probability of getting each valid permutation.\n\nCalculate the expected total number of inversions in the resulting valid permutation.\n\nIt can be shown that it is in the form of $\\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \\ne 0$. Report the value of $P \\cdot Q^{-1} \\pmod {998244353}$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of the sequence.\n\nThe second line contains $n$ integers $p_1, p_2, \\dots, p_n$ ($-1 \\le p_i \\le n$, $p_i \\ne 0$) \u2014 the initial sequence.\n\nIt is guaranteed that all elements not equal to $-1$ are pairwise distinct.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the expected total number of inversions in the resulting valid permutation.\n\nIt can be shown that it is in the form of $\\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \\ne 0$. Report the value of $P \\cdot Q^{-1} \\pmod {998244353}$.\n\n\n-----Examples-----\nInput\n3\n3 -1 -1\n\nOutput\n499122179\n\nInput\n2\n1 2\n\nOutput\n0\n\nInput\n2\n-1 -1\n\nOutput\n499122177\n\n\n\n-----Note-----\n\nIn the first example two resulting valid permutations are possible:\n\n $[3, 1, 2]$ \u2014 $2$ inversions; $[3, 2, 1]$ \u2014 $3$ inversions. \n\nThe expected value is $\\frac{2 \\cdot 1 + 3 \\cdot 1}{2} = 2.5$.\n\nIn the second example no $-1$ are present, thus the only valid permutation is possible \u2014 the given one. It has $0$ inversions.\n\nIn the third example there are two resulting valid permutations \u2014 one with $0$ inversions and one with $1$ inversion."} +{"id": "01931", "text": "A card pyramid of height $1$ is constructed by resting two cards against each other. For $h>1$, a card pyramid of height $h$ is constructed by placing a card pyramid of height $h-1$ onto a base. A base consists of $h$ pyramids of height $1$, and $h-1$ cards on top. For example, card pyramids of heights $1$, $2$, and $3$ look as follows: $\\wedge A A$ \n\nYou start with $n$ cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed?\n\n\n-----Input-----\n\nEach test consists of multiple test cases. The first line contains a single integer $t$ ($1\\le t\\le 1000$)\u00a0\u2014 the number of test cases. Next $t$ lines contain descriptions of test cases.\n\nEach test case contains a single integer $n$ ($1\\le n\\le 10^9$)\u00a0\u2014 the number of cards.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^9$.\n\n\n-----Output-----\n\nFor each test case output a single integer\u00a0\u2014 the number of pyramids you will have constructed in the end.\n\n\n-----Example-----\nInput\n5\n3\n14\n15\n24\n1\n\nOutput\n1\n2\n1\n3\n0\n\n\n\n-----Note-----\n\nIn the first test, you construct a pyramid of height $1$ with $2$ cards. There is $1$ card remaining, which is not enough to build a pyramid.\n\nIn the second test, you build two pyramids, each of height $2$, with no cards remaining.\n\nIn the third test, you build one pyramid of height $3$, with no cards remaining.\n\nIn the fourth test, you build one pyramid of height $3$ with $9$ cards remaining. Then you build a pyramid of height $2$ with $2$ cards remaining. Then you build a final pyramid of height $1$ with no cards remaining.\n\nIn the fifth test, one card is not enough to build any pyramids."} +{"id": "01932", "text": "Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons: \n\n Tetrahedron. Tetrahedron has 4 triangular faces. Cube. Cube has 6 square faces. Octahedron. Octahedron has 8 triangular faces. Dodecahedron. Dodecahedron has 12 pentagonal faces. Icosahedron. Icosahedron has 20 triangular faces. \n\nAll five kinds of polyhedrons are shown on the picture below:\n\n [Image] \n\nAnton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the number of polyhedrons in Anton's collection.\n\nEach of the following n lines of the input contains a string s_{i}\u00a0\u2014 the name of the i-th polyhedron in Anton's collection. The string can look like this:\n\n \"Tetrahedron\" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron. \"Cube\" (without quotes), if the i-th polyhedron in Anton's collection is a cube. \"Octahedron\" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron. \"Dodecahedron\" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron. \"Icosahedron\" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron. \n\n\n-----Output-----\n\nOutput one number\u00a0\u2014 the total number of faces in all the polyhedrons in Anton's collection.\n\n\n-----Examples-----\nInput\n4\nIcosahedron\nCube\nTetrahedron\nDodecahedron\n\nOutput\n42\n\nInput\n3\nDodecahedron\nOctahedron\nOctahedron\n\nOutput\n28\n\n\n\n-----Note-----\n\nIn the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces."} +{"id": "01933", "text": "Ivan is playing a strange game.\n\nHe has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:\n\n Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i}, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. \n\nOf course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.\n\n\n-----Input-----\n\nThe first line contains three integer numbers n, m and k (1 \u2264 k \u2264 n \u2264 100, 1 \u2264 m \u2264 100).\n\nThen n lines follow, i-th of them contains m integer numbers \u2014 the elements of i-th row of matrix a. Each number is either 0 or 1.\n\n\n-----Output-----\n\nPrint two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.\n\n\n-----Examples-----\nInput\n4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1\n\nOutput\n4 1\n\nInput\n3 2 1\n1 0\n0 1\n0 0\n\nOutput\n2 0\n\n\n\n-----Note-----\n\nIn the first example Ivan will replace the element a_{1, 2}."} +{"id": "01934", "text": "You are given sequence a_1, a_2, ..., a_{n} and m queries l_{j}, r_{j} (1 \u2264 l_{j} \u2264 r_{j} \u2264 n). For each query you need to print the minimum distance between such pair of elements a_{x} and a_{y} (x \u2260 y), that: both indexes of the elements lie within range [l_{j}, r_{j}], that is, l_{j} \u2264 x, y \u2264 r_{j}; the values of the elements are equal, that is a_{x} = a_{y}. \n\nThe text above understands distance as |x - y|.\n\n\n-----Input-----\n\nThe first line of the input contains a pair of integers n, m (1 \u2264 n, m \u2264 5\u00b710^5) \u2014 the length of the sequence and the number of queries, correspondingly. \n\nThe second line contains the sequence of integers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9). \n\nNext m lines contain the queries, one per line. Each query is given by a pair of numbers l_{j}, r_{j} (1 \u2264 l_{j} \u2264 r_{j} \u2264 n) \u2014 the indexes of the query range limits.\n\n\n-----Output-----\n\nPrint m integers \u2014 the answers to each query. If there is no valid match for some query, please print -1 as an answer to this query.\n\n\n-----Examples-----\nInput\n5 3\n1 1 2 3 2\n1 5\n2 4\n3 5\n\nOutput\n1\n-1\n2\n\nInput\n6 5\n1 2 1 3 2 3\n4 6\n1 3\n2 5\n2 4\n1 6\n\nOutput\n2\n2\n3\n-1\n2"} +{"id": "01935", "text": "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. \n\nOn 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.\n\nDenis 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.\n\nFormally, 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.\n\nHe knows many different integers $d_1, d_2, \\ldots, d_m$, where $0 \\leq d_i \\leq n$ \u00a0\u2014 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.\n\nUnfortunately, 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. \n\nDenis has crossed the road as soon as his coordinate becomes equal to $n$.\n\nThis 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.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ $(1 \\leq n \\leq 10^6, 2 \\leq m \\leq min(n + 1, 10^4))$ \u00a0\u2014 road width and the number of safety islands.\n\nThe second line contains $m$ distinct integers $d_1, d_2, \\ldots, d_m$ $(0 \\leq d_i \\leq n)$ \u00a0\u2014 the points where the safety islands are located. It is guaranteed that there are $0$ and $n$ among them.\n\nThe third line contains two integers $g, r$ $(1 \\leq g, r \\leq 1000)$ \u00a0\u2014 the time that the green light stays on and the time that the red light stays on.\n\n\n-----Output-----\n\nOutput a single integer \u00a0\u2014 the minimum time for which Denis can cross the road with obeying all the rules.\n\nIf it is impossible to cross the road output $-1$.\n\n\n-----Examples-----\nInput\n15 5\n0 3 7 14 15\n11 11\n\nOutput\n45\nInput\n13 4\n0 3 7 13\n9 9\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first test, the optimal route is: \u00a0\u00a0\u00a0\u00a0 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. \u00a0\u00a0\u00a0\u00a0 for the second green light reaches $14$. Wait for the red light again. \u00a0\u00a0\u00a0\u00a0 for $1$ second go to $15$. As a result, Denis is at the end of the road. \n\nIn total, $45$ seconds are obtained.\n\nIn the second test, it is impossible to cross the road according to all the rules."} +{"id": "01936", "text": "Let $LCM(x, y)$ be the minimum positive integer that is divisible by both $x$ and $y$. For example, $LCM(13, 37) = 481$, $LCM(9, 6) = 18$.\n\nYou are given two integers $l$ and $r$. Find two integers $x$ and $y$ such that $l \\le x < y \\le r$ and $l \\le LCM(x, y) \\le r$.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10000$) \u2014 the number of test cases.\n\nEach test case is represented by one line containing two integers $l$ and $r$ ($1 \\le l < r \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case, print two integers:\n\n if it is impossible to find integers $x$ and $y$ meeting the constraints in the statement, print two integers equal to $-1$; otherwise, print the values of $x$ and $y$ (if there are multiple valid answers, you may print any of them). \n\n\n-----Example-----\nInput\n4\n1 1337\n13 69\n2 4\n88 89\n\nOutput\n6 7\n14 21\n2 4\n-1 -1"} +{"id": "01937", "text": "Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.\n\nThere were $n$ classes of that subject during the semester and on $i$-th class professor mentioned some non-negative integer $a_i$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. \n\nSounds easy enough for those who attended every class, doesn't it?\n\nObviously Mishka didn't attend any classes. However, professor left some clues on the values of $a$ to help out students like Mishka: $a$ was sorted in non-decreasing order ($a_1 \\le a_2 \\le \\dots \\le a_n$); $n$ was even; the following sequence $b$, consisting of $\\frac n 2$ elements, was formed and given out to students: $b_i = a_i + a_{n - i + 1}$. \n\nProfessor also mentioned that any sequence $a$, which produces sequence $b$ with the presented technique, will be acceptable.\n\nHelp Mishka to pass that last exam. Restore any sorted sequence $a$ of non-negative integers, which produces sequence $b$ with the presented technique. It is guaranteed that there exists at least one correct sequence $a$, which produces the given sequence $b$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of sequence $a$. $n$ is always even.\n\nThe second line contains $\\frac n 2$ integers $b_1, b_2, \\dots, b_{\\frac n 2}$ ($0 \\le b_i \\le 10^{18}$) \u2014 sequence $b$, where $b_i = a_i + a_{n - i + 1}$.\n\nIt is guaranteed that there exists at least one correct sequence $a$, which produces the given sequence $b$.\n\n\n-----Output-----\n\nPrint $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 10^{18}$) in a single line.\n\n$a_1 \\le a_2 \\le \\dots \\le a_n$ should be satisfied.\n\n$b_i = a_i + a_{n - i + 1}$ should be satisfied for all valid $i$.\n\n\n-----Examples-----\nInput\n4\n5 6\n\nOutput\n2 3 3 3\n\nInput\n6\n2 1 2\n\nOutput\n0 0 1 1 1 2"} +{"id": "01938", "text": "Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define f([l, r]) = r - l + 1 to be the number of integer points in the segment [l, r] with l \u2264 r (say that $f(\\varnothing) = 0$). You are given two integers n and k and n closed intervals [l_{i}, r_{i}] on OX axis and you have to find: $\\sum_{1 \\leq i_{1} < i_{2} < \\ldots < i_{k} \\leq n} f([ l_{i_{1}}, r_{i_{1}} ] \\cap [ l_{i_{2}}, r_{i_{2}} ] \\cap \\ldots \\cap [ l_{i_{k}}, r_{i_{k}} ])$ \n\nIn other words, you should find the sum of the number of integer points in the intersection of any k of the segments. \n\nAs the answer may be very large, output it modulo 1000000007 (10^9 + 7).\n\nMike can't solve this problem so he needs your help. You will help him, won't you? \n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 k \u2264 n \u2264 200 000)\u00a0\u2014 the number of segments and the number of segments in intersection groups respectively.\n\nThen n lines follow, the i-th line contains two integers l_{i}, r_{i} ( - 10^9 \u2264 l_{i} \u2264 r_{i} \u2264 10^9), describing i-th segment bounds.\n\n\n-----Output-----\n\nPrint one integer number\u00a0\u2014 the answer to Mike's problem modulo 1000000007 (10^9 + 7) in the only line.\n\n\n-----Examples-----\nInput\n3 2\n1 2\n1 3\n2 3\n\nOutput\n5\n\nInput\n3 3\n1 3\n1 3\n1 3\n\nOutput\n3\n\nInput\n3 1\n1 2\n2 3\n3 4\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first example: \n\n$f([ 1,2 ] \\cap [ 1,3 ]) = f([ 1,2 ]) = 2$;\n\n$f([ 1,2 ] \\cap [ 2,3 ]) = f([ 2,2 ]) = 1$;\n\n$f([ 1,3 ] \\cap [ 2,3 ]) = f([ 2,3 ]) = 2$.\n\nSo the answer is 2 + 1 + 2 = 5."} +{"id": "01939", "text": "Levko loves tables that consist of n rows and n columns very much. He especially loves beautiful tables. A table is beautiful to Levko if the sum of elements in each row and column of the table equals k.\n\nUnfortunately, he doesn't know any such table. Your task is to help him to find at least one of them. \n\n\n-----Input-----\n\nThe single line contains two integers, n and k (1 \u2264 n \u2264 100, 1 \u2264 k \u2264 1000).\n\n\n-----Output-----\n\nPrint any beautiful table. Levko doesn't like too big numbers, so all elements of the table mustn't exceed 1000 in their absolute value.\n\nIf there are multiple suitable tables, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n2 4\n\nOutput\n1 3\n3 1\n\nInput\n4 7\n\nOutput\n2 1 0 4\n4 0 2 1\n1 3 3 0\n0 3 2 2\n\n\n\n-----Note-----\n\nIn the first sample the sum in the first row is 1 + 3 = 4, in the second row \u2014 3 + 1 = 4, in the first column \u2014 1 + 3 = 4 and in the second column \u2014 3 + 1 = 4. There are other beautiful tables for this sample.\n\nIn the second sample the sum of elements in each row and each column equals 7. Besides, there are other tables that meet the statement requirements."} +{"id": "01940", "text": "Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.\n\nShe has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are w_{i} pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.\n\nHelp her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 10^5, 1 \u2264 k \u2264 10^9)\u00a0\u2014 the number of different pebble types and number of pebbles Anastasia can place in one pocket.\n\nThe second line contains n integers w_1, w_2, ..., w_{n} (1 \u2264 w_{i} \u2264 10^4)\u00a0\u2014 number of pebbles of each type. \n\n\n-----Output-----\n\nThe only line of output contains one integer\u00a0\u2014 the minimum number of days Anastasia needs to collect all the pebbles.\n\n\n-----Examples-----\nInput\n3 2\n2 3 4\n\nOutput\n3\n\nInput\n5 4\n3 1 8 9 7\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type\u00a0\u2014 on the second day, and of third type\u00a0\u2014 on the third day.\n\nOptimal sequence of actions in the second sample case: In the first day Anastasia collects 8 pebbles of the third type. In the second day she collects 8 pebbles of the fourth type. In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type. In the fourth day she collects 7 pebbles of the fifth type. In the fifth day she collects 1 pebble of the second type."} +{"id": "01941", "text": "Karafs is some kind of vegetable in shape of an 1 \u00d7 h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. [Image] \n\nEach Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is s_{i} = A + (i - 1) \u00d7 B.\n\nFor a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.\n\nNow SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l \u2264 r and sequence s_{l}, s_{l} + 1, ..., s_{r} can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.\n\n\n-----Input-----\n\nThe first line of input contains three integers A, B and n (1 \u2264 A, B \u2264 10^6, 1 \u2264 n \u2264 10^5).\n\nNext n lines contain information about queries. i-th line contains integers l, t, m (1 \u2264 l, t, m \u2264 10^6) for i-th query.\n\n\n-----Output-----\n\nFor each query, print its answer in a single line.\n\n\n-----Examples-----\nInput\n2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8\n\nOutput\n4\n-1\n8\n-1\n\nInput\n1 5 2\n1 5 10\n2 7 4\n\nOutput\n1\n2"} +{"id": "01942", "text": "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.\n\nYou should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices).\n\nWe 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$ \u2014 a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once.\n\nFind the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.\n\nSince the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \\dots, v_r$.\n\n\n-----Input-----\n\nThe first line contains the single integer $T$ ($1 \\le T \\le 100$) \u2014 the number of test cases.\n\nNext $T$ lines contain test cases \u2014 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$) \u2014 the number of vertices in $K_n$, and segment of the cycle to print.\n\nIt'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$.\n\n\n-----Output-----\n\nFor 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.\n\n\n-----Example-----\nInput\n3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n\nOutput\n1 2 1 \n1 3 2 3 \n1 \n\n\n\n-----Note-----\n\nIn the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$.\n\nIn the third test case, it's quite obvious that the cycle should start and end in vertex $1$."} +{"id": "01943", "text": "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\u00b7i - 1 will play against team 2\u00b7i. 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\u00b7i - 1 will play against the winner of the previous round's game 2\u00b7i.\n\nEvery 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.\n\nFor 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.\n\n\n-----Input-----\n\nInput will begin with a line containing N (2 \u2264 N \u2264 6).\n\n2^{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.\n\n\n-----Output-----\n\nPrint the maximum possible expected score over all possible brackets. Your answer must be correct to within an absolute or relative error of 10^{ - 9}.\n\nFormally, 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}$.\n\n\n-----Examples-----\nInput\n2\n0 40 100 100\n60 0 40 40\n0 60 0 45\n0 60 55 0\n\nOutput\n1.75\n\nInput\n3\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\nOutput\n12\n\nInput\n2\n0 21 41 26\n79 0 97 33\n59 3 0 91\n74 67 9 0\n\nOutput\n3.141592\n\n\n\n-----Note-----\n\nIn 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."} +{"id": "01944", "text": "One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop.\n\nPlease, check the guess of Alex. You are given descriptions of n laptops. Determine whether two described above laptops exist.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^5) \u2014 the number of laptops.\n\nNext n lines contain two integers each, a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n), where a_{i} is the price of the i-th laptop, and b_{i} is the number that represents the quality of the i-th laptop (the larger the number is, the higher is the quality).\n\nAll a_{i} are distinct. All b_{i} are distinct. \n\n\n-----Output-----\n\nIf Alex is correct, print \"Happy Alex\", otherwise print \"Poor Alex\" (without the quotes).\n\n\n-----Examples-----\nInput\n2\n1 2\n2 1\n\nOutput\nHappy Alex"} +{"id": "01945", "text": "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.\n\nMisha 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.\n\n\n-----Input-----\n\nThe first line contains integer q (1 \u2264 q \u2264 1000), the number of handle change requests.\n\nNext q lines contain the descriptions of the requests, one per line.\n\nEach 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.\n\nThe 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.\n\n\n-----Output-----\n\nIn the first line output the integer n \u2014 the number of users that changed their handles at least once.\n\nIn 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.\n\nEach user who changes the handle must occur exactly once in this description.\n\n\n-----Examples-----\nInput\n5\nMisha ILoveCodeforces\nVasya Petrov\nPetrov VasyaPetrov123\nILoveCodeforces MikeMirzayanov\nPetya Ivanov\n\nOutput\n3\nPetya Ivanov\nMisha MikeMirzayanov\nVasya VasyaPetrov123"} +{"id": "01946", "text": "Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.\n\nIn order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.\n\nAll elements are enumerated with integers. The ChemForces company has discovered $n$ distinct chemical elements with indices $a_1, a_2, \\ldots, a_n$, and will get an income of $x_i$ Berland rubles if the $i$-th element from this list is in the set of this company.\n\nThe TopChemist company discovered $m$ distinct chemical elements with indices $b_1, b_2, \\ldots, b_m$, and it will get an income of $y_j$ Berland rubles for including the $j$-th element from this list to its set.\n\nIn other words, the first company can present any subset of elements from $\\{a_1, a_2, \\ldots, a_n\\}$ (possibly empty subset), the second company can present any subset of elements from $\\{b_1, b_2, \\ldots, b_m\\}$ (possibly empty subset). There shouldn't be equal elements in the subsets.\n\nHelp the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^5$) \u00a0\u2014 the number of elements discovered by ChemForces.\n\nThe $i$-th of the next $n$ lines contains two integers $a_i$ and $x_i$ ($1 \\leq a_i \\leq 10^9$, $1 \\leq x_i \\leq 10^9$) \u00a0\u2014 the index of the $i$-th element and the income of its usage on the exhibition. It is guaranteed that all $a_i$ are distinct.\n\nThe next line contains a single integer $m$ ($1 \\leq m \\leq 10^5$) \u00a0\u2014 the number of chemicals invented by TopChemist.\n\nThe $j$-th of the next $m$ lines contains two integers $b_j$ and $y_j$, ($1 \\leq b_j \\leq 10^9$, $1 \\leq y_j \\leq 10^9$) \u00a0\u2014 the index of the $j$-th element and the income of its usage on the exhibition. It is guaranteed that all $b_j$ are distinct.\n\n\n-----Output-----\n\nPrint the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.\n\n\n-----Examples-----\nInput\n3\n1 2\n7 2\n3 10\n4\n1 4\n2 4\n3 4\n4 4\n\nOutput\n24\n\nInput\n1\n1000000000 239\n3\n14 15\n92 65\n35 89\n\nOutput\n408\n\n\n\n-----Note-----\n\nIn the first example ChemForces can choose the set ($3, 7$), while TopChemist can choose ($1, 2, 4$). This way the total income is $(10 + 2) + (4 + 4 + 4) = 24$.\n\nIn the second example ChemForces can choose the only element $10^9$, while TopChemist can choose ($14, 92, 35$). This way the total income is $(239) + (15 + 65 + 89) = 408$."} +{"id": "01947", "text": "Alice's hair is growing by leaps and bounds. Maybe the cause of it is the excess of vitamins, or maybe it is some black magic...\n\nTo prevent this, Alice decided to go to the hairdresser. She wants for her hair length to be at most $l$ centimeters after haircut, where $l$ is her favorite number. Suppose, that the Alice's head is a straight line on which $n$ hairlines grow. Let's number them from $1$ to $n$. With one swing of the scissors the hairdresser can shorten all hairlines on any segment to the length $l$, given that all hairlines on that segment had length strictly greater than $l$. The hairdresser wants to complete his job as fast as possible, so he will make the least possible number of swings of scissors, since each swing of scissors takes one second.\n\nAlice hasn't decided yet when she would go to the hairdresser, so she asked you to calculate how much time the haircut would take depending on the time she would go to the hairdresser. In particular, you need to process queries of two types: $0$\u00a0\u2014 Alice asks how much time the haircut would take if she would go to the hairdresser now. $1$ $p$ $d$\u00a0\u2014 $p$-th hairline grows by $d$ centimeters. \n\nNote, that in the request $0$ Alice is interested in hypothetical scenario of taking a haircut now, so no hairlines change their length.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $l$ ($1 \\le n, m \\le 100\\,000$, $1 \\le l \\le 10^9$)\u00a0\u2014 the number of hairlines, the number of requests and the favorite number of Alice.\n\nThe second line contains $n$ integers $a_i$ ($1 \\le a_i \\le 10^9$)\u00a0\u2014 the initial lengths of all hairlines of Alice.\n\nEach of the following $m$ lines contains a request in the format described in the statement.\n\nThe request description starts with an integer $t_i$. If $t_i = 0$, then you need to find the time the haircut would take. Otherwise, $t_i = 1$ and in this moment one hairline grows. The rest of the line than contains two more integers: $p_i$ and $d_i$ ($1 \\le p_i \\le n$, $1 \\le d_i \\le 10^9$)\u00a0\u2014 the number of the hairline and the length it grows by.\n\n\n-----Output-----\n\nFor each query of type $0$ print the time the haircut would take.\n\n\n-----Example-----\nInput\n4 7 3\n1 2 3 4\n0\n1 2 3\n0\n1 1 3\n0\n1 3 1\n0\n\nOutput\n1\n2\n2\n1\n\n\n\n-----Note-----\n\nConsider the first example: Initially lengths of hairlines are equal to $1, 2, 3, 4$ and only $4$-th hairline is longer $l=3$, and hairdresser can cut it in $1$ second. Then Alice's second hairline grows, the lengths of hairlines are now equal to $1, 5, 3, 4$ Now haircut takes two seonds: two swings are required: for the $4$-th hairline and for the $2$-nd. Then Alice's first hairline grows, the lengths of hairlines are now equal to $4, 5, 3, 4$ The haircut still takes two seconds: with one swing hairdresser can cut $4$-th hairline and with one more swing cut the segment from $1$-st to $2$-nd hairline. Then Alice's third hairline grows, the lengths of hairlines are now equal to $4, 5, 4, 4$ Now haircut takes only one second: with one swing it is possible to cut the segment from $1$-st hairline to the $4$-th."} +{"id": "01948", "text": "Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of n vertices. Vertex 1 is the root of the tree.\n\nAlice starts at vertex 1 and Bob starts at vertex x (x \u2260 1). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.\n\nThe game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.\n\nYou should write a program which will determine how many moves will the game last.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n and x (2 \u2264 n \u2264 2\u00b710^5, 2 \u2264 x \u2264 n).\n\nEach of the next n - 1 lines contains two integer numbers a and b (1 \u2264 a, b \u2264 n) \u2014 edges of the tree. It is guaranteed that the edges form a valid tree.\n\n\n-----Output-----\n\nPrint the total number of moves Alice and Bob will make.\n\n\n-----Examples-----\nInput\n4 3\n1 2\n2 3\n2 4\n\nOutput\n4\n\nInput\n5 2\n1 2\n2 3\n3 4\n2 5\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first example the tree looks like this:\n\n [Image] \n\nThe red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:\n\nB: stay at vertex 3\n\nA: go to vertex 2\n\nB: stay at vertex 3\n\nA: go to vertex 3\n\nIn the second example the tree looks like this:\n\n [Image] \n\nThe moves in the optimal strategy are:\n\nB: go to vertex 3\n\nA: go to vertex 2\n\nB: go to vertex 4\n\nA: go to vertex 3\n\nB: stay at vertex 4\n\nA: go to vertex 4"} +{"id": "01949", "text": "You're given an array $a$. You should repeat the following operation $k$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.\n\n\n-----Input-----\n\nThe first line contains integers $n$ and $k$ $(1 \\le n,k \\le 10^5)$, the length of the array and the number of operations you should perform.\n\nThe 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.\n\n\n-----Output-----\n\nPrint the minimum non-zero element before each operation in a new line.\n\n\n-----Examples-----\nInput\n3 5\n1 2 3\n\nOutput\n1\n1\n1\n0\n0\n\nInput\n4 2\n10 3 5 3\n\nOutput\n3\n2\n\n\n\n-----Note-----\n\nIn the first sample:\n\nIn the first step: the array is $[1,2,3]$, so the minimum non-zero element is 1.\n\nIn the second step: the array is $[0,1,2]$, so the minimum non-zero element is 1.\n\nIn the third step: the array is $[0,0,1]$, so the minimum non-zero element is 1.\n\nIn the fourth and fifth step: the array is $[0,0,0]$, so we printed 0.\n\nIn the second sample:\n\nIn the first step: the array is $[10,3,5,3]$, so the minimum non-zero element is 3.\n\nIn the second step: the array is $[7,0,2,0]$, so the minimum non-zero element is 2."} +{"id": "01950", "text": "Ivan has n different boxes. The first of them contains some balls of n different colors.\n\nIvan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 \u2264 i \u2264 n) i-th box will contain all balls with color i.\n\nIn order to do this, Ivan will make some turns. Each turn he does the following: Ivan chooses any non-empty box and takes all balls from this box; Then Ivan chooses any k empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into k non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either k = 2 or k = 3. \n\nThe penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.\n\nHelp Ivan to determine the minimum possible penalty of the game!\n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 200000) \u2014 the number of boxes and colors.\n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9), where a_{i} is the number of balls with color i.\n\n\n-----Output-----\n\nPrint one number \u2014 the minimum possible penalty of the game.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n6\n\nInput\n4\n2 3 4 5\n\nOutput\n19\n\n\n\n-----Note-----\n\nIn the first example you take all the balls from the first box, choose k = 3 and sort all colors to corresponding boxes. Penalty is 6.\n\nIn the second example you make two turns: Take all the balls from the first box, choose k = 3, put balls of color 3 to the third box, of color 4 \u2014 to the fourth box and the rest put back into the first box. Penalty is 14; Take all the balls from the first box, choose k = 2, put balls of color 1 to the first box, of color 2 \u2014 to the second box. Penalty is 5. \n\nTotal penalty is 19."} +{"id": "01951", "text": "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.\n\nTenten keeps a record for all events, and she ends up with a list of the following types of records:\n\n + means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought. \n\nToday 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!\n\n\n-----Input-----\n\nThe first line contains the only integer $n$ ($1\\leq n\\leq 10^5$) standing for the number of shurikens. \n\nThe 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.\n\n\n-----Output-----\n\nIf 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\".\n\nIn 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.\n\n\n-----Examples-----\nInput\n4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4\n\nOutput\nYES\n4 2 3 1 \n\nInput\n1\n- 1\n+\n\nOutput\nNO\n\nInput\n3\n+\n+\n+\n- 2\n- 1\n- 3\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn 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.\n\nIn the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.\n\nIn 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."} +{"id": "01952", "text": "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!\n\nInitially, 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 \u2014 the $i$-th test is an array of size $m_i$ ($1 \\le m_i \\le k$).\n\nYour 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$.\n\nSo 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. \n\nPrint the minimum possible number of testcases you can achieve and the sizes of arrays included in each testcase.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n, k \\le 2 \\cdot 10^5$)\u00a0\u2014 the number of initial tests and the limit for the size of each array.\n\nThe second line contains $n$ integers $m_1, m_2, \\dots, m_n$ ($1 \\le m_i \\le k$)\u00a0\u2014 the sizes of the arrays in the original tests.\n\nThe 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.\n\n\n-----Output-----\n\nIn the first line print a single integer $ans$ ($1 \\le ans \\le n$)\u00a0\u2014 the minimum number of testcases you can achieve.\n\nEach of the next $ans$ lines should contain the description of a testcase in the following format:\n\n$t$ $a_1$ $a_2$ $\\dots$ $a_{t}$ ($1 \\le t\\le n$)\u00a0\u2014 the testcase includes $t$ arrays, $a_i$ is the size of the $i$-th array in that testcase.\n\nEach 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$.\n\nNote that the answer always exists due to $c_k \\ge 1$ (and therefore $c_1 \\ge 1$).\n\nIf there are multiple answers, you can output any one of them.\n\n\n-----Examples-----\nInput\n4 3\n1 2 2 3\n4 1 1\n\nOutput\n3\n1 2\n2 1 3\n1 2\n\nInput\n6 10\n5 8 1 10 8 7\n6 6 4 4 3 2 2 2 1 1\n\nOutput\n2\n3 8 5 7\n3 10 8 1\n\nInput\n5 1\n1 1 1 1 1\n5\n\nOutput\n1\n5 1 1 1 1 1\n\nInput\n5 1\n1 1 1 1 1\n1\n\nOutput\n5\n1 1\n1 1\n1 1\n1 1\n1 1\n\n\n\n-----Note-----\n\nIn 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$.\n\nNote that there are multiple valid answers for this test. For example, testcases with sizes $[[2], [1, 2], [3]]$ would also be correct.\n\nHowever, 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.\n\nNote 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."} +{"id": "01953", "text": "Little girl Susie went shopping with her mom and she wondered how to improve service quality. \n\nThere are n people in the queue. For each person we know time t_{i} needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. \n\nHelp Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5).\n\nThe next line contains n integers t_{i} (1 \u2264 t_{i} \u2264 10^9), separated by spaces.\n\n\n-----Output-----\n\nPrint a single number \u2014 the maximum number of not disappointed people in the queue.\n\n\n-----Examples-----\nInput\n5\n15 2 1 5 3\n\nOutput\n4\n\n\n\n-----Note-----\n\nValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5."} +{"id": "01954", "text": "Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. \n\nHe wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that i^{th} friend is given a_{i} sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every a_{i}.\n\nPlease find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (10^9 + 7).\n\nTo make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe first line contains an integer q representing the number of queries (1 \u2264 q \u2264 10^5). Each of the next q lines contains two space space-separated integers n, f (1 \u2264 f \u2264 n \u2264 10^5).\n\n\n-----Output-----\n\nFor each query, output a single integer in a line corresponding to the answer of each query.\n\n\n-----Examples-----\nInput\n5\n6 2\n7 2\n6 3\n6 4\n7 4\n\nOutput\n2\n6\n9\n10\n20\n\n\n\n-----Note-----\n\nFor first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1].\n\nFor second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning."} +{"id": "01955", "text": "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.\n\nAbout 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. \n\nOn 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. \n\nAbout each subject Vasiliy know a number a_{i}\u00a0\u2014 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.\n\nYour 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. \n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 10^5)\u00a0\u2014 the number of days in the exam period and the number of subjects. \n\nThe second line contains n integers d_1, d_2, ..., d_{n} (0 \u2264 d_{i} \u2264 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. \n\nThe third line contains m positive integers a_1, a_2, ..., a_{m} (1 \u2264 a_{i} \u2264 10^5), where a_{i} is the number of days that are needed to prepare before passing the exam on the subject i.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.\n\n\n-----Examples-----\nInput\n7 2\n0 1 0 2 1 0 2\n2 1\n\nOutput\n5\n\nInput\n10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4\n\nOutput\n9\n\nInput\n5 1\n1 1 1 1 1\n5\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn 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.\n\nIn 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. \n\nIn the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it."} +{"id": "01956", "text": "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\"...\n\nThe 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. \n\nThe 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.\n\nNow 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.\n\nThe winner of the last round is the winner of the game, and the loser of the last round is the loser of the game.\n\nDetermine 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.\n\n\n-----Input-----\n\nThe first line contains the integer $t$ ($1 \\le t \\le 10^5$)\u00a0\u2014 the number of rounds the game has. \n\nThen $t$ lines follow, each contains two integers $s_i$ and $e_i$ ($1 \\le s_i \\le e_i \\le 10^{18}$)\u00a0\u2014 the $i$-th round's information.\n\nThe 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.\n\n\n-----Output-----\n\nPrint two integers.\n\nThe first one should be 1 if Lee can be the winner independent of Ice Bear's moves, and 0 otherwise.\n\nThe second one should be 1 if Lee can be the loser independent of Ice Bear's moves, and 0 otherwise.\n\n\n-----Examples-----\nInput\n3\n5 8\n1 4\n3 10\n\nOutput\n1 1\n\nInput\n4\n1 2\n2 3\n3 4\n4 5\n\nOutput\n0 0\n\nInput\n1\n1 1\n\nOutput\n0 1\n\nInput\n2\n1 9\n4 5\n\nOutput\n0 0\n\nInput\n2\n1 2\n2 8\n\nOutput\n1 0\n\nInput\n6\n216986951114298167 235031205335543871\n148302405431848579 455670351549314242\n506251128322958430 575521452907339082\n1 768614336404564650\n189336074809158272 622104412002885672\n588320087414024192 662540324268197150\n\nOutput\n1 0\n\n\n\n-----Note-----\n\nRemember, whoever writes an integer greater than $e_i$ loses."} +{"id": "01957", "text": "Arkady wants to water his only flower. Unfortunately, he has a very poor watering system that was designed for $n$ flowers and so it looks like a pipe with $n$ holes. Arkady can only use the water that flows from the first hole.\n\nArkady can block some of the holes, and then pour $A$ liters of water into the pipe. After that, the water will flow out from the non-blocked holes proportionally to their sizes $s_1, s_2, \\ldots, s_n$. In other words, if the sum of sizes of non-blocked holes is $S$, and the $i$-th hole is not blocked, $\\frac{s_i \\cdot A}{S}$ liters of water will flow out of it.\n\nWhat is the minimum number of holes Arkady should block to make at least $B$ liters of water flow out of the first hole?\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $A$, $B$ ($1 \\le n \\le 100\\,000$, $1 \\le B \\le A \\le 10^4$)\u00a0\u2014 the number of holes, the volume of water Arkady will pour into the system, and the volume he wants to get out of the first hole.\n\nThe second line contains $n$ integers $s_1, s_2, \\ldots, s_n$ ($1 \\le s_i \\le 10^4$)\u00a0\u2014 the sizes of the holes.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of holes Arkady should block.\n\n\n-----Examples-----\nInput\n4 10 3\n2 2 2 2\n\nOutput\n1\n\nInput\n4 80 20\n3 2 1 4\n\nOutput\n0\n\nInput\n5 10 10\n1000 1 1 1 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example Arkady should block at least one hole. After that, $\\frac{10 \\cdot 2}{6} \\approx 3.333$ liters of water will flow out of the first hole, and that suits Arkady.\n\nIn the second example even without blocking any hole, $\\frac{80 \\cdot 3}{10} = 24$ liters will flow out of the first hole, that is not less than $20$.\n\nIn the third example Arkady has to block all holes except the first to make all water flow out of the first hole."} +{"id": "01958", "text": "Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.\n\nShe precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.\n\nSo each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).\n\nFor each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).\n\nPrint the total money grandma should have at the end of the day to check if some buyers cheated her.\n\n\n-----Input-----\n\nThe first line contains two integers n and p (1 \u2264 n \u2264 40, 2 \u2264 p \u2264 1000) \u2014 the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.\n\nThe next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.\n\nIt is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.\n\n\n-----Output-----\n\nPrint the only integer a \u2014 the total money grandma should have at the end of the day.\n\nNote that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.\n\n\n-----Examples-----\nInput\n2 10\nhalf\nhalfplus\n\nOutput\n15\n\nInput\n3 10\nhalfplus\nhalfplus\nhalfplus\n\nOutput\n55\n\n\n\n-----Note-----\n\nIn the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer."} +{"id": "01959", "text": "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. \n\nNikolay 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. \n\nA 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.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (2 \u2264 n \u2264 2\u00b710^5, 1 \u2264 m \u2264 10^9)\u00a0\u2014 the number of cards Eugeny has and the number of cards Nikolay has. It is guaranteed that n is even.\n\nThe second line contains a sequence of n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the numbers on Eugeny's cards.\n\n\n-----Output-----\n\nIf there is no answer, print -1.\n\nOtherwise, in the first line print the minimum number of exchanges. In the second line print n integers\u00a0\u2014 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.\n\nIf there are multiple answers, it is allowed to print any of them.\n\n\n-----Examples-----\nInput\n6 2\n5 6 7 9 4 5\n\nOutput\n1\n5 6 7 9 4 2 \n\nInput\n8 6\n7 7 7 7 8 8 8 8\n\nOutput\n6\n7 2 4 6 8 1 3 5 \n\nInput\n4 1\n4 2 1 10\n\nOutput\n-1"} +{"id": "01960", "text": "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.\n\nNam created a sequence a consisting of n (1 \u2264 n \u2264 10^5) elements a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5). A subsequence a_{i}_1, a_{i}_2, ..., a_{i}_{k} where 1 \u2264 i_1 < i_2 < ... < i_{k} \u2264 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. \n\nNam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 \u2264 i \u2264 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. \n\nSince 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.\n\n\n-----Input-----\n\nThe first line contains the single integer n (1 \u2264 n \u2264 10^5) denoting the number of elements of sequence a.\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5).\n\n\n-----Output-----\n\nPrint 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.\n\n\n-----Examples-----\nInput\n1\n4\n\nOutput\n3\n\nInput\n4\n1 3 2 5\n\nOutput\n3223\n\nInput\n4\n1 5 2 3\n\nOutput\n3133\n\n\n\n-----Note-----\n\nIn 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}.\n\nIn 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}."} +{"id": "01961", "text": "Student Andrey has been skipping physical education lessons for the whole term, and now he must somehow get a passing grade on this subject. Obviously, it is impossible to do this by legal means, but Andrey doesn't give up. Having obtained an empty certificate from a local hospital, he is going to use his knowledge of local doctor's handwriting to make a counterfeit certificate of illness. However, after writing most of the certificate, Andrey suddenly discovered that doctor's signature is impossible to forge. Or is it?\n\nFor simplicity, the signature is represented as an $n\\times m$ grid, where every cell is either filled with ink or empty. Andrey's pen can fill a $3\\times3$ square without its central cell if it is completely contained inside the grid, as shown below. \n\nxxx\n\nx.x\n\nxxx\n\n \n\nDetermine whether is it possible to forge the signature on an empty $n\\times m$ grid.\n\n\n-----Input-----\n\nThe first line of input contains two integers $n$ and $m$ ($3 \\le n, m \\le 1000$).\n\nThen $n$ lines follow, each contains $m$ characters. Each of the characters is either '.', representing an empty cell, or '#', representing an ink filled cell.\n\n\n-----Output-----\n\nIf Andrey can forge the signature, output \"YES\". Otherwise output \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n3 3\n###\n#.#\n###\n\nOutput\nYES\nInput\n3 3\n###\n###\n###\n\nOutput\nNO\nInput\n4 3\n###\n###\n###\n###\n\nOutput\nYES\nInput\n5 7\n.......\n.#####.\n.#.#.#.\n.#####.\n.......\n\nOutput\nYES\n\n\n-----Note-----\n\nIn the first sample Andrey can paint the border of the square with the center in $(2, 2)$.\n\nIn the second sample the signature is impossible to forge.\n\nIn the third sample Andrey can paint the borders of the squares with the centers in $(2, 2)$ and $(3, 2)$: we have a clear paper: \n\n...\n\n...\n\n...\n\n...\n\n use the pen with center at $(2, 2)$. \n\n###\n\n#.#\n\n###\n\n...\n\n use the pen with center at $(3, 2)$. \n\n###\n\n###\n\n###\n\n###\n\n \n\nIn the fourth sample Andrey can paint the borders of the squares with the centers in $(3, 3)$ and $(3, 5)$."} +{"id": "01962", "text": "You have m = n\u00b7k wooden staves. The i-th stave has length a_{i}. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.\n\nLet volume v_{j} of barrel j be equal to the length of the minimal stave in it. [Image] \n\nYou want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |v_{x} - v_{y}| \u2264 l for any 1 \u2264 x \u2264 n and 1 \u2264 y \u2264 n.\n\nPrint maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers n, k and l (1 \u2264 n, k \u2264 10^5, 1 \u2264 n\u00b7k \u2264 10^5, 0 \u2264 l \u2264 10^9).\n\nThe second line contains m = n\u00b7k space-separated integers a_1, a_2, ..., a_{m} (1 \u2264 a_{i} \u2264 10^9) \u2014 lengths of staves.\n\n\n-----Output-----\n\nPrint single integer \u2014 maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |v_{x} - v_{y}| \u2264 l for any 1 \u2264 x \u2264 n and 1 \u2264 y \u2264 n.\n\n\n-----Examples-----\nInput\n4 2 1\n2 2 1 2 3 2 2 3\n\nOutput\n7\n\nInput\n2 1 0\n10 10\n\nOutput\n20\n\nInput\n1 2 1\n5 2\n\nOutput\n2\n\nInput\n3 2 1\n1 2 3 4 5 6\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].\n\nIn the second example you can form the following barrels: [10], [10].\n\nIn the third example you can form the following barrels: [2, 5].\n\nIn the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough."} +{"id": "01963", "text": "Simon has an array a_1, a_2, ..., a_{n}, consisting of n positive integers. Today Simon asked you to find a pair of integers l, r (1 \u2264 l \u2264 r \u2264 n), such that the following conditions hold: there is integer j (l \u2264 j \u2264 r), such that all integers a_{l}, a_{l} + 1, ..., a_{r} are divisible by a_{j}; value r - l takes the maximum value among all pairs for which condition 1 is true; \n\nHelp Simon, find the required pair of numbers (l, r). If there are multiple required pairs find all of them.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 3\u00b710^5).\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^6).\n\n\n-----Output-----\n\nPrint two integers in the first line \u2014 the number of required pairs and the maximum value of r - l. On the following line print all l values from optimal pairs in increasing order.\n\n\n-----Examples-----\nInput\n5\n4 6 9 3 6\n\nOutput\n1 3\n2 \n\nInput\n5\n1 3 5 7 9\n\nOutput\n1 4\n1 \n\nInput\n5\n2 3 5 7 11\n\nOutput\n5 0\n1 2 3 4 5 \n\n\n\n-----Note-----\n\nIn the first sample the pair of numbers is right, as numbers 6, 9, 3 are divisible by 3.\n\nIn the second sample all numbers are divisible by number 1.\n\nIn the third sample all numbers are prime, so conditions 1 and 2 are true only for pairs of numbers (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)."} +{"id": "01964", "text": "Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products.\n\nThere are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has k_{i} people. The j-th person standing in the queue to the i-th cashier has m_{i}, j items in the basket. Vasya knows that: the cashier needs 5 seconds to scan one item; after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change. \n\nOf course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of cashes in the shop. The second line contains n space-separated integers: k_1, k_2, ..., k_{n} (1 \u2264 k_{i} \u2264 100), where k_{i} is the number of people in the queue to the i-th cashier.\n\nThe i-th of the next n lines contains k_{i} space-separated integers: m_{i}, 1, m_{i}, 2, ..., m_{i}, k_{i} (1 \u2264 m_{i}, j \u2264 100)\u00a0\u2014 the number of products the j-th person in the queue for the i-th cash has.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of seconds Vasya needs to get to the cashier.\n\n\n-----Examples-----\nInput\n1\n1\n1\n\nOutput\n20\n\nInput\n4\n1 4 3 2\n100\n1 2 2 3\n1 9 1\n7 8\n\nOutput\n100\n\n\n\n-----Note-----\n\nIn the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100\u00b75 + 15 = 515 seconds. But if he chooses the second queue, he will need 1\u00b75 + 2\u00b75 + 2\u00b75 + 3\u00b75 + 4\u00b715 = 100 seconds. He will need 1\u00b75 + 9\u00b75 + 1\u00b75 + 3\u00b715 = 100 seconds for the third one and 7\u00b75 + 8\u00b75 + 2\u00b715 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue."} +{"id": "01965", "text": "A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).\n\nKilljoy's account is already infected and has a rating equal to $x$. Its rating is constant. There are $n$ accounts except hers, numbered from $1$ to $n$. The $i$-th account's initial rating is $a_i$. Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed.\n\nContests are regularly held on Codeforces. In each contest, any of these $n$ accounts (including infected ones) can participate. Killjoy can't participate. After each contest ratings are changed this way: each participant's rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.\n\nFind out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.\n\nIt can be proven that all accounts can be infected in some finite number of contests.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ $(1 \\le t \\le 100)$\u00a0\u2014 the number of test cases. The next $2t$ lines contain the descriptions of all test cases.\n\nThe first line of each test case contains two integers $n$ and $x$ ($2 \\le n \\le 10^3$, $-4000 \\le x \\le 4000$)\u00a0\u2014 the number of accounts on Codeforces and the rating of Killjoy's account.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\dots, a_n$ $(-4000 \\le a_i \\le 4000)$\u00a0\u2014 the ratings of other accounts.\n\n\n-----Output-----\n\nFor each test case output the minimal number of contests needed to infect all accounts.\n\n\n-----Example-----\nInput\n3\n2 69\n68 70\n6 4\n4 4 4 4 4 4\n9 38\n-21 83 50 -59 -77 15 -71 -78 20\n\nOutput\n1\n0\n2\n\n\n\n-----Note-----\n\nIn the first test case it's possible to make all ratings equal to $69$. First account's rating will increase by $1$, and second account's rating will decrease by $1$, so the sum of all changes will be equal to zero.\n\nIn the second test case all accounts will be instantly infected, because all ratings (including Killjoy's account's rating) are equal to $4$."} +{"id": "01966", "text": "Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color a_{k}, i, j; 1 being black and 0 being white. \n\nNow Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.\n\n\n-----Input-----\n\nThe first line contains odd integer n (1 \u2264 n \u2264 100) \u2014 the size of all pieces of the board. \n\nThen 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.\n\n\n-----Output-----\n\nPrint one number \u2014 minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.\n\n\n-----Examples-----\nInput\n1\n0\n\n0\n\n1\n\n0\n\nOutput\n1\n\nInput\n3\n101\n010\n101\n\n101\n000\n101\n\n010\n101\n011\n\n010\n101\n010\n\nOutput\n2"} +{"id": "01967", "text": "Polycarp is writing the prototype of a graphic editor. He has already made up his mind that the basic image transformations in his editor will be: rotate the image 90 degrees clockwise, flip the image horizontally (symmetry relative to the vertical line, that is, the right part of the image moves to the left, and vice versa) and zooming on the image. He is sure that that there is a large number of transformations that can be expressed through these three.\n\nHe has recently stopped implementing all three transformations for monochrome images. To test this feature, he asked you to write a code that will consecutively perform three actions with a monochrome image: first it will rotate the image 90 degrees clockwise, then it will flip the image horizontally and finally, it will zoom in twice on the image (that is, it will double all the linear sizes).\n\nImplement this feature to help Polycarp test his editor.\n\n\n-----Input-----\n\nThe first line contains two integers, w and h (1 \u2264 w, h \u2264 100) \u2014 the width and height of an image in pixels. The picture is given in h lines, each line contains w characters \u2014 each character encodes the color of the corresponding pixel of the image. The line consists only of characters \".\" and \"*\", as the image is monochrome.\n\n\n-----Output-----\n\nPrint 2w lines, each containing 2h characters \u2014 the result of consecutive implementing of the three transformations, described above.\n\n\n-----Examples-----\nInput\n3 2\n.*.\n.*.\n\nOutput\n....\n....\n****\n****\n....\n....\n\nInput\n9 20\n**.......\n****.....\n******...\n*******..\n..******.\n....****.\n......***\n*.....***\n*********\n*********\n*********\n*********\n....**...\n...****..\n..******.\n.********\n****..***\n***...***\n**.....**\n*.......*\n\nOutput\n********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********"} +{"id": "01968", "text": "Valera is a collector. Once he wanted to expand his collection with exactly one antique item.\n\nValera knows n sellers of antiques, the i-th of them auctioned k_{i} items. Currently the auction price of the j-th object of the i-th seller is s_{ij}. Valera gets on well with each of the n sellers. He is perfectly sure that if he outbids the current price of one of the items in the auction (in other words, offers the seller the money that is strictly greater than the current price of the item at the auction), the seller of the object will immediately sign a contract with him.\n\nUnfortunately, Valera has only v units of money. Help him to determine which of the n sellers he can make a deal with.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n, v (1 \u2264 n \u2264 50;\u00a010^4 \u2264 v \u2264 10^6) \u2014 the number of sellers and the units of money the Valera has.\n\nThen n lines follow. The i-th line first contains integer k_{i} (1 \u2264 k_{i} \u2264 50) the number of items of the i-th seller. Then go k_{i} space-separated integers s_{i}1, s_{i}2, ..., s_{ik}_{i} (10^4 \u2264 s_{ij} \u2264 10^6) \u2014 the current prices of the items of the i-th seller. \n\n\n-----Output-----\n\nIn the first line, print integer p \u2014 the number of sellers with who Valera can make a deal.\n\nIn the second line print p space-separated integers q_1, q_2, ..., q_{p} (1 \u2264 q_{i} \u2264 n) \u2014 the numbers of the sellers with who Valera can make a deal. Print the numbers of the sellers in the increasing order. \n\n\n-----Examples-----\nInput\n3 50000\n1 40000\n2 20000 60000\n3 10000 70000 190000\n\nOutput\n3\n1 2 3\n\nInput\n3 50000\n1 50000\n3 100000 120000 110000\n3 120000 110000 120000\n\nOutput\n0\n\n\n\n\n-----Note-----\n\nIn the first sample Valera can bargain with each of the sellers. He can outbid the following items: a 40000 item from the first seller, a 20000 item from the second seller, and a 10000 item from the third seller.\n\nIn the second sample Valera can not make a deal with any of the sellers, as the prices of all items in the auction too big for him."} +{"id": "01969", "text": "Lunar New Year is approaching, and you bought a matrix with lots of \"crosses\".\n\nThis matrix $M$ of size $n \\times n$ contains only 'X' and '.' (without quotes). The element in the $i$-th row and the $j$-th column $(i, j)$ is defined as $M(i, j)$, where $1 \\leq i, j \\leq n$. We define a cross appearing in the $i$-th row and the $j$-th column ($1 < i, j < n$) if and only if $M(i, j) = M(i - 1, j - 1) = M(i - 1, j + 1) = M(i + 1, j - 1) = M(i + 1, j + 1) = $ 'X'.\n\nThe following figure illustrates a cross appearing at position $(2, 2)$ in a $3 \\times 3$ matrix. \n\nX.X\n\n.X.\n\nX.X\n\n \n\nYour task is to find out the number of crosses in the given matrix $M$. Two crosses are different if and only if they appear in different rows or columns.\n\n\n-----Input-----\n\nThe first line contains only one positive integer $n$ ($1 \\leq n \\leq 500$), denoting the size of the matrix $M$.\n\nThe following $n$ lines illustrate the matrix $M$. Each line contains exactly $n$ characters, each of them is 'X' or '.'. The $j$-th element in the $i$-th line represents $M(i, j)$, where $1 \\leq i, j \\leq n$.\n\n\n-----Output-----\n\nOutput a single line containing only one integer number $k$ \u2014 the number of crosses in the given matrix $M$.\n\n\n-----Examples-----\nInput\n5\n.....\n.XXX.\n.XXX.\n.XXX.\n.....\n\nOutput\n1\n\nInput\n2\nXX\nXX\n\nOutput\n0\n\nInput\n6\n......\nX.X.X.\n.X.X.X\nX.X.X.\n.X.X.X\n......\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample, a cross appears at $(3, 3)$, so the answer is $1$.\n\nIn the second sample, no crosses appear since $n < 3$, so the answer is $0$.\n\nIn the third sample, crosses appear at $(3, 2)$, $(3, 4)$, $(4, 3)$, $(4, 5)$, so the answer is $4$."} +{"id": "01970", "text": "A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard.\n\nPetya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count.\n\nPetya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board.\n\nPlease see the test case analysis.\n\n\n-----Input-----\n\nThe first line contains number t (1 \u2264 t \u2264 50) \u2014 the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters \".\", \"#\", \"K\", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.\n\n\n-----Output-----\n\nFor each test, print on a single line the answer to the problem: \"YES\", if the semiknights can meet and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n2\n........\n........\n......#.\nK..##..#\n.......#\n...##..#\n......#.\nK.......\n\n........\n........\n..#.....\n..#..#..\n..####..\n...##...\n........\n....K#K#\n\nOutput\nYES\nNO\n\n\n\n-----Note-----\n\nConsider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7).\n\nOn the second board the semiknights will never meet."} +{"id": "01971", "text": "A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q^2 = [2, 3, 4, 5, 1].\n\nThis problem is about the inverse operation: given the permutation p you task is to find such permutation q that q^2 = p. If there are several such q find any of them.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^6) \u2014 the number of elements in permutation p.\n\nThe second line contains n distinct integers p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 n) \u2014 the elements of permutation p.\n\n\n-----Output-----\n\nIf there is no permutation q such that q^2 = p print the number \"-1\".\n\nIf the answer exists print it. The only line should contain n different integers q_{i} (1 \u2264 q_{i} \u2264 n) \u2014 the elements of the permutation q. If there are several solutions print any of them.\n\n\n-----Examples-----\nInput\n4\n2 1 4 3\n\nOutput\n3 4 2 1\n\nInput\n4\n2 1 3 4\n\nOutput\n-1\n\nInput\n5\n2 3 4 5 1\n\nOutput\n4 5 1 2 3"} +{"id": "01972", "text": "You are given an array $a$ consisting of $500000$ integers (numbered from $1$ to $500000$). Initially all elements of $a$ are zero.\n\nYou have to process two types of queries to this array: $1$ $x$ $y$\u00a0\u2014 increase $a_x$ by $y$; $2$ $x$ $y$\u00a0\u2014 compute $\\sum\\limits_{i \\in R(x, y)} a_i$, where $R(x, y)$ is the set of all integers from $1$ to $500000$ which have remainder $y$ modulo $x$. \n\nCan you process all the queries?\n\n\n-----Input-----\n\nThe first line contains one integer $q$ ($1 \\le q \\le 500000$) \u2014 the number of queries.\n\nThen $q$ lines follow, each describing a query. The $i$-th line contains three integers $t_i$, $x_i$ and $y_i$ ($1 \\le t_i \\le 2$). If $t_i = 1$, then it is a query of the first type, $1 \\le x_i \\le 500000$, and $-1000 \\le y_i \\le 1000$. If $t_i = 2$, then it it a query of the second type, $1 \\le x_i \\le 500000$, and $0 \\le y_i < x_i$.\n\nIt is guaranteed that there will be at least one query of type $2$.\n\n\n-----Output-----\n\nFor each query of type $2$ print one integer \u2014 the answer to it.\n\n\n-----Example-----\nInput\n5\n1 3 4\n2 3 0\n2 4 3\n1 4 -4\n2 1 0\n\nOutput\n4\n4\n0"} +{"id": "01973", "text": "This problem is same as the next one, but has smaller constraints.\n\nShiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.\n\nFor each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences.\n\nFor example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. \n\nSince Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^5$)\u00a0\u2014 the total number of days.\n\nThe second line contains $n$ integers $u_1, u_2, \\ldots, u_n$ ($1 \\leq u_i \\leq 10$)\u00a0\u2014 the colors of the ribbons the cats wear. \n\n\n-----Output-----\n\nPrint a single integer $x$\u00a0\u2014 the largest possible streak of days.\n\n\n-----Examples-----\nInput\n13\n1 1 1 2 2 2 3 3 3 4 4 4 5\n\nOutput\n13\nInput\n5\n10 2 5 4 1\n\nOutput\n5\nInput\n1\n10\n\nOutput\n1\nInput\n7\n3 2 1 1 4 5 1\n\nOutput\n6\nInput\n6\n1 1 1 2 2 2\n\nOutput\n5\n\n\n-----Note-----\n\nIn the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak.\n\nIn the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once."} +{"id": "01974", "text": "There are $n$ robbers at coordinates $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ and $m$ searchlight at coordinates $(c_1, d_1)$, $(c_2, d_2)$, ..., $(c_m, d_m)$. \n\nIn one move you can move each robber to the right (increase $a_i$ of each robber by one) or move each robber up (increase $b_i$ of each robber by one). Note that you should either increase all $a_i$ or all $b_i$, you can't increase $a_i$ for some points and $b_i$ for some other points.\n\nSearchlight $j$ can see a robber $i$ if $a_i \\leq c_j$ and $b_i \\leq d_j$. \n\nA configuration of robbers is safe if no searchlight can see a robber (i.e. if there is no pair $i,j$ such that searchlight $j$ can see a robber $i$).\n\nWhat is the minimum number of moves you need to perform to reach a safe configuration?\n\n\n-----Input-----\n\nThe first line of input contains two integers $n$ and $m$ ($1 \\leq n, m \\leq 2000$): the number of robbers and the number of searchlight.\n\nEach of the next $n$ lines contains two integers $a_i$, $b_i$ ($0 \\leq a_i, b_i \\leq 10^6$), coordinates of robbers.\n\nEach of the next $m$ lines contains two integers $c_i$, $d_i$ ($0 \\leq c_i, d_i \\leq 10^6$), coordinates of searchlights.\n\n\n-----Output-----\n\nPrint one integer: the minimum number of moves you need to perform to reach a safe configuration.\n\n\n-----Examples-----\nInput\n1 1\n0 0\n2 3\n\nOutput\n3\n\nInput\n2 3\n1 6\n6 1\n10 1\n1 10\n7 7\n\nOutput\n4\n\nInput\n1 2\n0 0\n0 0\n0 0\n\nOutput\n1\n\nInput\n7 3\n0 8\n3 8\n2 7\n0 10\n5 5\n7 0\n3 5\n6 6\n3 11\n11 5\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first test, you can move each robber to the right three times. After that there will be one robber in the coordinates $(3, 0)$.\n\nThe configuration of the robbers is safe, because the only searchlight can't see the robber, because it is in the coordinates $(2, 3)$ and $3 > 2$.\n\nIn the second test, you can move each robber to the right two times and two times up. After that robbers will be in the coordinates $(3, 8)$, $(8, 3)$.\n\nIt's easy the see that the configuration of the robbers is safe.\n\nIt can be proved that you can't reach a safe configuration using no more than $3$ moves."} +{"id": "01975", "text": "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. \n\nHelp Fox Ciel to make a schedule that they can dance as many songs as possible.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 100) \u2014 the number of boys and girls in the dancing room.\n\n\n-----Output-----\n\nIn the first line print k \u2014 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.\n\n\n-----Examples-----\nInput\n2 1\n\nOutput\n2\n1 1\n2 1\n\nInput\n2 2\n\nOutput\n3\n1 1\n1 2\n2 2\n\n\n\n-----Note-----\n\nIn 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).\n\nAnd in test case 2, we have 2 boys with 2 girls, the answer is 3."} +{"id": "01976", "text": "Recently Luba bought a monitor. Monitor is a rectangular matrix of size n \u00d7 m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k \u00d7 k consisting entirely of broken pixels. She knows that q pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all q pixels stopped working).\n\n\n-----Input-----\n\nThe first line contains four integer numbers n, m, k, q\u00a0(1 \u2264 n, m \u2264 500, 1 \u2264 k \u2264 min(n, m), 0 \u2264 q \u2264 n\u00b7m) \u2014 the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.\n\nEach of next q lines contain three integer numbers x_{i}, y_{i}, t_{i}\u00a0(1 \u2264 x_{i} \u2264 n, 1 \u2264 y_{i} \u2264 m, 0 \u2264 t_{ \u2264 10}^9) \u2014 coordinates of i-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.\n\nWe consider that pixel is already broken at moment t_{i}.\n\n\n-----Output-----\n\nPrint one number \u2014 the minimum moment the monitor became broken, or \"-1\" if it's still not broken after these q pixels stopped working.\n\n\n-----Examples-----\nInput\n2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2\n\nOutput\n8\n\nInput\n3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100\n\nOutput\n-1"} +{"id": "01977", "text": "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.\n\nConsider 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$.\n\nFind the lexicographically smallest string that can be associated with a path after changing letters in at most $k$ cells of the matrix.\n\nA string $a$ is lexicographically smaller than a string $b$, if the first different letter in $a$ and $b$ is smaller in $a$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n \\le 2000$, $0 \\le k \\le n^2$) \u2014 the size of the matrix and the number of letters you can change.\n\nEach of the next $n$ lines contains a string of $n$ lowercase English letters denoting one row of the matrix.\n\n\n-----Output-----\n\nOutput the lexicographically smallest string that can be associated with some valid path after changing no more than $k$ letters in the matrix.\n\n\n-----Examples-----\nInput\n4 2\nabcd\nbcde\nbcad\nbcde\n\nOutput\naaabcde\n\nInput\n5 3\nbwwwz\nhrhdh\nsepsp\nsqfaf\najbvw\n\nOutput\naaaepfafw\n\nInput\n7 6\nypnxnnp\npnxonpm\nnxanpou\nxnnpmud\nnhtdudu\nnpmuduh\npmutsnz\n\nOutput\naaaaaaadudsnz\n\n\n\n-----Note-----\n\nIn 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."} +{"id": "01978", "text": "The main characters have been omitted to be short.\n\nYou 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}$.\n\nDefine 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.\n\nA 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.\n\nIf there are multiple shortest good subsequences, output any of them.\n\n \n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 100$)\u00a0\u2014 the number of vertexes in a graph. \n\nThe 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.\n\nThe next line contains a single integer $m$ ($2 \\le m \\le 10^6$)\u00a0\u2014 the number of vertexes in the path. \n\nThe next line contains $m$ integers $p_1, p_2, \\ldots, p_m$ ($1 \\le p_i \\le n$)\u00a0\u2014 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}$.\n\n\n-----Output-----\n\nIn the first line output a single integer $k$ ($2 \\leq k \\leq m$)\u00a0\u2014 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$)\u00a0\u2014 the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.\n\n\n-----Examples-----\nInput\n4\n0110\n0010\n0001\n1000\n4\n1 2 3 4\n\nOutput\n3\n1 2 4 \nInput\n4\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\nOutput\n11\n1 2 4 2 4 2 4 2 4 2 4 \nInput\n3\n011\n101\n110\n7\n1 2 3 1 3 2 1\n\nOutput\n7\n1 2 3 1 3 2 1 \nInput\n4\n0110\n0001\n0001\n1000\n3\n1 2 4\n\nOutput\n2\n1 4 \n\n\n-----Note-----\n\nBelow you can see the graph from the first example:\n\n[Image]\n\nThe 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$.\n\nIn 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.\n\nIn the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$."} +{"id": "01979", "text": "After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $n$. Let's call them $a$ and $b$.\n\nNote that a permutation of $n$ elements is a sequence of numbers $a_1, a_2, \\ldots, a_n$, in which every number from $1$ to $n$ appears exactly once. \n\nThe message can be decoded by an arrangement of sequence $a$ and $b$, such that the number of matching pairs of elements between them is maximum. A pair of elements $a_i$ and $b_j$ is said to match if: $i = j$, that is, they are at the same index. $a_i = b_j$ \n\nHis two disciples are allowed to perform the following operation any number of times: choose a number $k$ and cyclically shift one of the permutations to the left or right $k$ times. \n\nA single cyclic shift to the left on any permutation $c$ is an operation that sets $c_1:=c_2, c_2:=c_3, \\ldots, c_n:=c_1$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $c$ is an operation that sets $c_1:=c_n, c_2:=c_1, \\ldots, c_n:=c_{n-1}$ simultaneously.\n\nHelp Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ $(1 \\le n \\le 2 \\cdot 10^5)$\u00a0\u2014 the size of the arrays.\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \\le a_i \\le n)$ \u2014 the elements of the first permutation.\n\nThe third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \\le b_i \\le n)$ \u2014 the elements of the second permutation.\n\n\n-----Output-----\n\nPrint the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n2 3 4 5 1\n\nOutput\n5\nInput\n5\n5 4 3 2 1\n1 2 3 4 5\n\nOutput\n1\nInput\n4\n1 3 2 4\n4 2 3 1\n\nOutput\n2\n\n\n-----Note-----\n\nFor the first case: $b$ can be shifted to the right by $k = 1$. The resulting permutations will be $\\{1, 2, 3, 4, 5\\}$ and $\\{1, 2, 3, 4, 5\\}$.\n\nFor the second case: The operation is not required. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $1$.\n\nFor the third case: $b$ can be shifted to the left by $k = 1$. The resulting permutations will be $\\{1, 3, 2, 4\\}$ and $\\{2, 3, 1, 4\\}$. Positions $2$ and $4$ have matching pairs of elements. For all possible rotations of $a$ and $b$, the number of matching pairs won't exceed $2$."} +{"id": "01980", "text": "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.\n\nVasya 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.\n\nIn order to create a contest he needs to choose a consecutive subsegment of tasks.\n\nSo 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$. \n\nCalculate the maximum profit that Vasya can earn by taking a consecutive segment of tasks.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $a$ ($1 \\le n \\le 3 \\cdot 10^5$, $1 \\le a \\le 10^9$) \u2014 the number of proposed tasks and the profit for a single problem, respectively.\n\nEach 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}$).\n\n\n-----Output-----\n\nPrint one integer \u2014 maximum amount of burles Vasya can earn.\n\n\n-----Examples-----\nInput\n5 10\n1 15\n5 3\n6 11\n7 2\n11 22\n\nOutput\n13\n\nInput\n3 5\n1 8\n2 19\n3 11\n\nOutput\n0"} +{"id": "01981", "text": "Kefa decided to celebrate his first big salary by going to the restaurant. \n\nHe lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.\n\nThe leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. \n\nYour task is to help Kefa count the number of restaurants where he can go.\n\n\n-----Input-----\n\nThe first line contains two integers, n and m (2 \u2264 n \u2264 10^5, 1 \u2264 m \u2264 n) \u2014 the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.\n\nThe second line contains n integers a_1, a_2, ..., a_{n}, where each a_{i} either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat).\n\nNext n - 1 lines contains the edges of the tree in the format \"x_{i} y_{i}\" (without the quotes) (1 \u2264 x_{i}, y_{i} \u2264 n, x_{i} \u2260 y_{i}), where x_{i} and y_{i} are the vertices of the tree, connected by an edge. \n\nIt is guaranteed that the given set of edges specifies a tree.\n\n\n-----Output-----\n\nA single integer \u2014 the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.\n\n\n-----Examples-----\nInput\n4 1\n1 1 0 0\n1 2\n1 3\n1 4\n\nOutput\n2\n\nInput\n7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n\nOutput\n2\n\n\n\n-----Note-----\n\nLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.\n\nNote to the first sample test: $80$ The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.\n\nNote to the second sample test: $88$ The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7."} +{"id": "01982", "text": "You are given two integers $n$ and $k$. Your task is to find if $n$ can be represented as a sum of $k$ distinct positive odd (not divisible by $2$) integers or not.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^5$) \u2014 the number of test cases.\n\nThe next $t$ lines describe test cases. The only line of the test case contains two integers $n$ and $k$ ($1 \\le n, k \\le 10^7$).\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 \"YES\" (without quotes) if $n$ can be represented as a sum of $k$ distinct positive odd (not divisible by $2$) integers and \"NO\" otherwise.\n\n\n-----Example-----\nInput\n6\n3 1\n4 2\n10 3\n10 2\n16 4\n16 5\n\nOutput\nYES\nYES\nNO\nYES\nYES\nNO\n\n\n\n-----Note-----\n\nIn the first test case, you can represent $3$ as $3$.\n\nIn the second test case, the only way to represent $4$ is $1+3$.\n\nIn the third test case, you cannot represent $10$ as the sum of three distinct positive odd integers.\n\nIn the fourth test case, you can represent $10$ as $3+7$, for example.\n\nIn the fifth test case, you can represent $16$ as $1+3+5+7$.\n\nIn the sixth test case, you cannot represent $16$ as the sum of five distinct positive odd integers."} +{"id": "01983", "text": "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?\n\nA 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.\n\n\n-----Input-----\n\nThe first line contains an integer $t$\u00a0\u2014 the number of test cases you need to solve. The description of the test cases follows.\n\nThe first line of each test case contains an integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the number of elements in the array $a$.\n\nThe second line contains $n$ space-separated integers $a_1$, $a_2$, $\\ldots$, $a_{n}$ ($1 \\le a_i \\le 10^9$)\u00a0\u2014 the elements of the array $a$.\n\nThe sum of $n$ across the test cases doesn't exceed $10^5$.\n\n\n-----Output-----\n\nFor each testcase, output the length of the longest increasing subsequence of $a$ if you concatenate it to itself $n$ times.\n\n\n-----Example-----\nInput\n2\n3\n3 2 1\n6\n3 1 4 1 5 9\n\nOutput\n3\n5\n\n\n\n-----Note-----\n\nIn 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.\n\nIn the second sample, the longest increasing subsequence will be $[1,3,4,5,9]$."} +{"id": "01984", "text": "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 \u00d7 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.\n\n [Image] \n\nWhen you transmit information via a network, you want to minimize traffic \u2014 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:\n\n You can transmit the whole level A. Then you need to transmit n\u00b7m 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\u00b7w 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. \n\nYour task is to find a way to transfer all the k levels and minimize the traffic.\n\n\n-----Input-----\n\nThe first line contains four integers n, m, k, w (1 \u2264 n, m \u2264 10;\u00a01 \u2264 k, w \u2264 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.\n\n\n-----Output-----\n\nIn the first line print the required minimum number of transferred bytes.\n\nThen 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.\n\nIf there are multiple optimal solutions, you can print any of them.\n\n\n-----Examples-----\nInput\n2 3 3 2\nA.A\n...\nA.a\n..C\nX.Y\n...\n\nOutput\n14\n1 0\n2 1\n3 1\n\nInput\n1 1 4 1\nA\n.\nB\n.\n\nOutput\n3\n1 0\n2 0\n4 2\n3 0\n\nInput\n1 3 5 2\nABA\nBBB\nBBA\nBAB\nABB\n\nOutput\n11\n1 0\n3 1\n2 3\n4 2\n5 1"} +{"id": "01985", "text": "Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i.\u00a0e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave a_{i} points.\n\nPolycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n \u2264 k) values b_1, b_2, ..., b_{n} (it is guaranteed that all values b_{j} are distinct). It is possible that Polycarp remembers not all of the scores announced, i.\u00a0e. n < k. Note that the initial score wasn't announced.\n\nYour task is to determine the number of options for the score the participant could have before the judges rated the participant.\n\n\n-----Input-----\n\nThe first line contains two integers k and n (1 \u2264 n \u2264 k \u2264 2 000) \u2014 the number of jury members and the number of scores Polycarp remembers.\n\nThe second line contains k integers a_1, a_2, ..., a_{k} ( - 2 000 \u2264 a_{i} \u2264 2 000) \u2014 jury's marks in chronological order.\n\nThe third line contains n distinct integers b_1, b_2, ..., b_{n} ( - 4 000 000 \u2264 b_{j} \u2264 4 000 000) \u2014 the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.\n\n\n-----Output-----\n\nPrint the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print \"0\" (without quotes).\n\n\n-----Examples-----\nInput\n4 1\n-5 5 0 20\n10\n\nOutput\n3\n\nInput\n2 2\n-2000 -2000\n3998000 4000000\n\nOutput\n1\n\n\n\n-----Note-----\n\nThe answer for the first example is 3 because initially the participant could have - 10, 10 or 15 points.\n\nIn the second example there is only one correct initial score equaling to 4 002 000."} +{"id": "01986", "text": "Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.\n\nThe Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers f_{i} and t_{i}. Value t_{i} shows the time the Rabbits need to lunch in the i-th restaurant. If time t_{i} exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal f_{i} - (t_{i} - k). Otherwise, the Rabbits get exactly f_{i} units of joy.\n\nYour task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value. \n\n\n-----Input-----\n\nThe first line contains two space-separated integers \u2014 n (1 \u2264 n \u2264 10^4) and k (1 \u2264 k \u2264 10^9) \u2014 the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers \u2014 f_{i} (1 \u2264 f_{i} \u2264 10^9) and t_{i} (1 \u2264 t_{i} \u2264 10^9) \u2014 the characteristics of the i-th restaurant.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the maximum joy value that the Rabbits will get from the lunch. \n\n\n-----Examples-----\nInput\n2 5\n3 3\n4 5\n\nOutput\n4\n\nInput\n4 6\n5 8\n3 6\n2 3\n2 2\n\nOutput\n3\n\nInput\n1 5\n1 7\n\nOutput\n-1"} +{"id": "01987", "text": "Pupils decided to go to amusement park. Some of them were with parents. In total, n people came to the park and they all want to get to the most extreme attraction and roll on it exactly once.\n\nTickets for group of x people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is c_1 + c_2\u00b7(x - 1)^2 (in particular, if the group consists of one person, then the price is c_1). \n\nAll pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group. \n\n\n-----Input-----\n\nThe first line contains three integers n, c_1 and c_2 (1 \u2264 n \u2264 200 000, 1 \u2264 c_1, c_2 \u2264 10^7)\u00a0\u2014 the number of visitors and parameters for determining the ticket prices for a group.\n\nThe second line contains the string of length n, which consists of zeros and ones. If the i-th symbol of the string is zero, then the i-th visitor is a pupil, otherwise the i-th person is an adult. It is guaranteed that there is at least one adult. It is possible that there are no pupils.\n\n\n-----Output-----\n\nPrint the minimum price of visiting the most extreme attraction for all pupils and their parents. Each of them should roll on the attraction exactly once.\n\n\n-----Examples-----\nInput\n3 4 1\n011\n\nOutput\n8\n\nInput\n4 7 2\n1101\n\nOutput\n18\n\n\n\n-----Note-----\n\nIn the first test one group of three people should go to the attraction. Then they have to pay 4 + 1 * (3 - 1)^2 = 8.\n\nIn the second test it is better to go to the attraction in two groups. The first group should consist of two adults (for example, the first and the second person), the second should consist of one pupil and one adult (the third and the fourth person). Then each group will have a size of two and for each the price of ticket is 7 + 2 * (2 - 1)^2 = 9. Thus, the total price for two groups is 18."} +{"id": "01988", "text": "Vasya has a string $s$ of length $n$. He decides to make the following modification to the string: Pick an integer $k$, ($1 \\leq k \\leq n$). For $i$ from $1$ to $n-k+1$, reverse the substring $s[i:i+k-1]$ of $s$. For example, if string $s$ is qwer and $k = 2$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $2$) weqr (after reversing the second substring of length $2$) werq (after reversing the last substring of length $2$) Hence, the resulting string after modifying $s$ with $k = 2$ is werq. \n\nVasya wants to choose a $k$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $k$. Among all such $k$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.\n\nA string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \\ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. \n\n\n-----Input-----\n\nEach test contains multiple test cases. \n\nThe first line contains the number of test cases $t$ ($1 \\le t \\le 5000$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 5000$)\u00a0\u2014 the length of the string $s$.\n\nThe second line of each test case contains the string $s$ of $n$ lowercase latin letters.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $5000$.\n\n\n-----Output-----\n\nFor each testcase output two lines:\n\nIn the first line output the lexicographically smallest string $s'$ achievable after the above-mentioned modification. \n\nIn the second line output the appropriate value of $k$ ($1 \\leq k \\leq n$) that you chose for performing the modification. If there are multiple values of $k$ that give the lexicographically smallest string, output the smallest value of $k$ among them.\n\n\n-----Example-----\nInput\n6\n4\nabab\n6\nqwerty\n5\naaaaa\n6\nalaska\n9\nlfpbavjsm\n1\np\n\nOutput\nabab\n1\nertyqw\n3\naaaaa\n1\naksala\n6\navjsmbpfl\n5\np\n1\n\n\n\n-----Note-----\n\nIn the first testcase of the first sample, the string modification results for the sample abab are as follows : for $k = 1$ : abab for $k = 2$ : baba for $k = 3$ : abab for $k = 4$ : baba\n\nThe lexicographically smallest string achievable through modification is abab for $k = 1$ and $3$. Smallest value of $k$ needed to achieve is hence $1$."} +{"id": "01989", "text": "Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.\n\nThere is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l \u2264 k \u2264 r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 \u2264 i < j \u2264 n) such that f(1, i, a_{i}) > f(j, n, a_{j}).\n\nHelp Pashmak with the test.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (1 \u2264 n \u2264 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n7\n1 2 1 1 2 2 1\n\nOutput\n8\n\nInput\n3\n1 1 1\n\nOutput\n1\n\nInput\n5\n1 2 3 4 5\n\nOutput\n0"} +{"id": "01990", "text": "The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text.\n\nYour 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.\n\nInitially, the cursor is in the first (leftmost) character.\n\nAlso, 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.\n\nYour editor must check, whether the current line is the correct text. Text is correct if the brackets in them form the correct bracket sequence.\n\nFormally, 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 \u2014 is (, the last \u2014 is ), and all the rest form a CT, then the whole line is a CT; two consecutively written CT is also CT. \n\nExamples of correct texts: hello(codeforces), round, ((i)(write))edi(tor)s, ( me). Examples of incorrect texts: hello)oops(, round), ((me).\n\nThe user uses special commands to work with your editor. Each command has its symbol, which must be written to execute this command.\n\nThe correspondence of commands and characters is as follows: L \u2014 move the cursor one character to the left (remains in place if it already points to the first character); R \u2014 move the cursor one character to the right; any lowercase Latin letter or bracket (( or )) \u2014 write the entered character to the position where the cursor is now. \n\nFor a complete understanding, take a look at the first example and its illustrations in the note below.\n\nYou are given a string containing the characters that the user entered. For the brackets coloring module's work, after each command you need to:\n\n 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. \n\nIf 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 (()(()())())(()) \u2014 is $3$.\n\nWrite a program that prints the minimal number of colors after processing each command.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 10^6$) \u2014 the number of commands. \n\nThe second line contains $s$ \u2014 a sequence of commands. The string $s$ consists of $n$ characters. It is guaranteed that all characters in a string are valid commands.\n\n\n-----Output-----\n\nIn a single line print $n$ integers, where the $i$-th number is:\n\n $-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. \n\n\n-----Examples-----\nInput\n11\n(RaRbR)L)L(\n\nOutput\n-1 -1 -1 -1 -1 -1 1 1 -1 -1 2 \nInput\n11\n(R)R(R)Ra)c\n\nOutput\n-1 -1 1 1 -1 -1 1 1 1 -1 1 \n\n\n-----Note-----\n\nIn the first example, the text in the editor will take the following form:\n\n (\n\n^ (\n\n ^ (a\n\n ^ (a\n\n ^ (ab\n\n ^ (ab\n\n ^ (ab)\n\n ^ (ab)\n\n ^ (a))\n\n ^ (a))\n\n ^ (())\n\n ^"} +{"id": "01991", "text": "Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across $n$ sessions follow the identity permutation (ie. in the first game he scores $1$ point, in the second game he scores $2$ points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! \n\nDefine a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on $[1,2,3]$ can yield $[3,1,2]$ but it cannot yield $[3,2,1]$ since the $2$ is in the same position. \n\nGiven a permutation of $n$ integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed $10^{18}$.\n\nAn array $a$ is a subarray of an array $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 100$). Description of the test cases follows.\n\nThe first line of each test case contains integer $n$ ($1 \\leq n \\leq 2 \\cdot 10^5$) \u00a0\u2014 the length of the given permutation.\n\nThe second line of each test case contains $n$ integers $a_{1},a_{2},...,a_{n}$ ($1 \\leq a_{i} \\leq n$) \u00a0\u2014 the initial permutation.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.\n\n\n-----Example-----\nInput\n2\n5\n1 2 3 4 5\n7\n3 2 4 5 1 6 7\n\nOutput\n0\n2\n\n\n\n-----Note-----\n\nIn the first permutation, it is already sorted so no exchanges are needed.\n\nIt can be shown that you need at least $2$ exchanges to sort the second permutation.\n\n$[3, 2, 4, 5, 1, 6, 7]$\n\nPerform special exchange on range ($1, 5$)\n\n$[4, 1, 2, 3, 5, 6, 7]$\n\nPerform special exchange on range ($1, 4$)\n\n$[1, 2, 3, 4, 5, 6, 7]$"} +{"id": "01992", "text": "Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k + 1)-th to the 2k-th ones are on the second screen and so on (the last screen may be partially empty).\n\nInitially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen t, Anya needs to make the following gestures: first she scrolls to the required screen number t, by making t - 1 gestures (if the icon is on the screen t), and then make another gesture \u2014 press the icon of the required application exactly once to launch it.\n\nAfter the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1.\n\nAll applications are numbered from 1 to n. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change.\n\nAnya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order? \n\nNote that one application may be launched multiple times.\n\n\n-----Input-----\n\nThe first line of the input contains three numbers n, m, k (1 \u2264 n, m, k \u2264 10^5)\u00a0\u2014\u00a0the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen.\n\nThe next line contains n integers, permutation a_1, a_2, ..., a_{n}\u00a0\u2014\u00a0the initial order of icons from left to right in the menu (from the first to the last one), a_{i}\u00a0\u2014\u00a0 is the id of the application, whose icon goes i-th in the menu. Each integer from 1 to n occurs exactly once among a_{i}.\n\nThe third line contains m integers b_1, b_2, ..., b_{m}(1 \u2264 b_{i} \u2264 n)\u00a0\u2014\u00a0the ids of the launched applications in the planned order. One application may be launched multiple times.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of gestures that Anya needs to make to launch all the applications in the desired order.\n\n\n-----Examples-----\nInput\n8 3 3\n1 2 3 4 5 6 7 8\n7 8 1\n\nOutput\n7\n\nInput\n5 4 2\n3 1 5 2 4\n4 4 4 4\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8. \n\nAfter application 7 is launched, we get the new arrangement of the icons\u00a0\u2014\u00a0(123)(457)(68). To launch it Anya makes 3 gestures. \n\nAfter application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures. \n\nAfter application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture.\n\nIn total, Anya makes 7 gestures."} +{"id": "01993", "text": "You are given a rectangular field of n \u00d7 m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are marked with '*'. Let's call two empty cells adjacent if they share a side.\n\nLet's call a connected component any non-extendible set of cells such that any two of them are connected by the path of adjacent cells. It is a typical well-known definition of a connected component.\n\nFor each impassable cell (x, y) imagine that it is an empty cell (all other cells remain unchanged) and find the size (the number of cells) of the connected component which contains (x, y). You should do it for each impassable cell independently.\n\nThe answer should be printed as a matrix with n rows and m columns. The j-th symbol of the i-th row should be \".\" if the cell is empty at the start. Otherwise the j-th symbol of the i-th row should contain the only digit \u2014- the answer modulo 10. The matrix should be printed without any spaces.\n\nTo make your output faster it is recommended to build the output as an array of n strings having length m and print it as a sequence of lines. It will be much faster than writing character-by-character.\n\nAs input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 1000) \u2014 the number of rows and columns in the field.\n\nEach of the next n lines contains m symbols: \".\" for empty cells, \"*\" for impassable cells.\n\n\n-----Output-----\n\nPrint the answer as a matrix as described above. See the examples to precise the format of the output.\n\n\n-----Examples-----\nInput\n3 3\n*.*\n.*.\n*.*\n\nOutput\n3.3\n.5.\n3.3\n\nInput\n4 5\n**..*\n..***\n.*.*.\n*.*.*\n\nOutput\n46..3\n..732\n.6.4.\n5.4.3\n\n\n\n-----Note-----\n\nIn first example, if we imagine that the central cell is empty then it will be included to component of size 5 (cross). If any of the corner cell will be empty then it will be included to component of size 3 (corner)."} +{"id": "01994", "text": "You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. \n\nLet's introduce several definitions: A substring s[i..j] (1 \u2264 i \u2264 j \u2264 |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 \u2264 l \u2264 |s|) is string s[1..l]. The suffix of string s of length l (1 \u2264 l \u2264 |s|) is string s[|s| - l + 1..|s|]. \n\nYour task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.\n\n\n-----Input-----\n\nThe single line contains a sequence of characters s_1s_2...s_{|}s| (1 \u2264 |s| \u2264 10^5) \u2014 string s. The string only consists of uppercase English letters.\n\n\n-----Output-----\n\nIn the first line, print integer k (0 \u2264 k \u2264 |s|) \u2014 the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}.\n\n\n-----Examples-----\nInput\nABACABA\n\nOutput\n3\n1 4\n3 2\n7 1\n\nInput\nAAA\n\nOutput\n3\n1 3\n2 2\n3 1"} +{"id": "01995", "text": "You are given a string s and should process m queries. Each query is described by two 1-based indices l_{i}, r_{i} and integer k_{i}. It means that you should cyclically shift the substring s[l_{i}... r_{i}] k_{i} times. The queries should be processed one after another in the order they are given.\n\nOne operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.\n\nFor example, if the string s is abacaba and the query is l_1 = 3, r_1 = 6, k_1 = 1 then the answer is abbacaa. If after that we would process the query l_2 = 1, r_2 = 4, k_2 = 2 then we would get the string baabcaa.\n\n\n-----Input-----\n\nThe first line of the input contains the string s (1 \u2264 |s| \u2264 10 000) in its initial state, where |s| stands for the length of s. It contains only lowercase English letters.\n\nSecond line contains a single integer m (1 \u2264 m \u2264 300)\u00a0\u2014 the number of queries.\n\nThe i-th of the next m lines contains three integers l_{i}, r_{i} and k_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 |s|, 1 \u2264 k_{i} \u2264 1 000 000)\u00a0\u2014 the description of the i-th query.\n\n\n-----Output-----\n\nPrint the resulting string s after processing all m queries.\n\n\n-----Examples-----\nInput\nabacaba\n2\n3 6 1\n1 4 2\n\nOutput\nbaabcaa\n\n\n\n-----Note-----\n\nThe sample is described in problem statement."} +{"id": "01996", "text": "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.\n\nValentin 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.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of actions Valentin did.\n\nThe 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\u00a0\u2014 a lowercase English letter. \n\nAll words consist only of lowercase English letters. The total length of all words does not exceed 10^5.\n\nIt 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.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.\n\n\n-----Examples-----\nInput\n5\n! abc\n. ad\n. b\n! cd\n? c\n\nOutput\n1\n\nInput\n8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e\n\nOutput\n2\n\nInput\n7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn 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. \n\nIn 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.\n\nIn the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks."} +{"id": "01997", "text": "Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.\n\nThere 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.\n\nInitially, 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.\n\nDemiurges 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.\n\n\n-----Input-----\n\nThe first line contains a single integer n\u00a0\u2014 the number of nodes in the tree (1 \u2264 n \u2264 2\u00b710^5).\n\nEach of the next n - 1 lines contains two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n)\u00a0\u2014 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.\n\n\n-----Output-----\n\nPrint two space-separated integers \u2014 the maximum possible and the minimum possible result of the game.\n\n\n-----Examples-----\nInput\n5\n1 2\n1 3\n2 4\n2 5\n\nOutput\n3 2\n\nInput\n6\n1 2\n1 3\n3 4\n1 5\n5 6\n\nOutput\n3 3\n\n\n\n-----Note-----\n\nConsider 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.\n\nIn 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."} +{"id": "01998", "text": "Galya is playing one-dimensional Sea Battle on a 1 \u00d7 n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other.\n\nGalya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called \"hit\") or not (this case is called \"miss\").\n\nGalya has already made k shots, all of them were misses.\n\nYour task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.\n\nIt is guaranteed that there is at least one valid ships placement.\n\n\n-----Input-----\n\nThe first line contains four positive integers n, a, b, k (1 \u2264 n \u2264 2\u00b710^5, 1 \u2264 a, b \u2264 n, 0 \u2264 k \u2264 n - 1)\u00a0\u2014 the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made.\n\nThe second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. \n\n\n-----Output-----\n\nIn the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.\n\nIn the second line print the cells Galya should shoot at.\n\nEach cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left.\n\nIf there are multiple answers, you can print any of them.\n\n\n-----Examples-----\nInput\n5 1 2 1\n00100\n\nOutput\n2\n4 2\n\nInput\n13 3 2 3\n1000000010001\n\nOutput\n2\n7 11\n\n\n\n-----Note-----\n\nThere is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the \"1\" character). So, it is necessary to make two shots: one at the left part, and one at the right part."} +{"id": "01999", "text": "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$).\n\nDetermine how the array will look after described operations are performed.\n\nFor 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]$.\n\nIf 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]$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 150\\,000$) \u2014 the number of elements in the array.\n\nThe second line contains a sequence from $n$ elements $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^{9}$) \u2014 the elements of the array.\n\n\n-----Output-----\n\nIn the first line print an integer $k$ \u2014 the number of elements in the array after all the performed operations. In the second line print $k$ integers \u2014 the elements of the array after all the performed operations.\n\n\n-----Examples-----\nInput\n7\n3 4 1 2 2 1 1\n\nOutput\n4\n3 8 2 1 \n\nInput\n5\n1 1 3 1 1\n\nOutput\n2\n3 4 \n\nInput\n5\n10 40 20 50 30\n\nOutput\n5\n10 40 20 50 30 \n\n\n\n-----Note-----\n\nThe first two examples were considered in the statement.\n\nIn the third example all integers in the given array are distinct, so it will not change."} +{"id": "02000", "text": "You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}).\n\n\n-----Input-----\n\nThe first line contains the single positive integer n (1 \u2264 n \u2264 10^5) \u2014 the number of integers.\n\nThe second line contains n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.\n\n\n-----Examples-----\nInput\n4\n7 3 2 1\n\nOutput\n2\n\nInput\n3\n1 1 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).\n\nIn the second example all pairs of indexes (i, j) (where i < j) include in answer."} +{"id": "02001", "text": "JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.\n\nFirst, he splits the Banh-mi into $n$ parts, places them on a row and numbers them from $1$ through $n$. For each part $i$, he defines the deliciousness of the part as $x_i \\in \\{0, 1\\}$. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the $i$-th part then his enjoyment of the Banh-mi will increase by $x_i$ and the deliciousness of all the remaining parts will also increase by $x_i$. The initial enjoyment of JATC is equal to $0$.\n\nFor example, suppose the deliciousness of $3$ parts are $[0, 1, 0]$. If JATC eats the second part then his enjoyment will become $1$ and the deliciousness of remaining parts will become $[1, \\_, 1]$. Next, if he eats the first part then his enjoyment will become $2$ and the remaining parts will become $[\\_, \\_, 2]$. After eating the last part, JATC's enjoyment will become $4$.\n\nHowever, JATC doesn't want to eat all the parts but to save some for later. He gives you $q$ queries, each of them consisting of two integers $l_i$ and $r_i$. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range $[l_i, r_i]$ in some order.\n\nAll the queries are independent of each other. Since the answer to the query could be very large, print it modulo $10^9+7$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $q$ ($1 \\le n, q \\le 100\\,000$).\n\nThe second line contains a string of $n$ characters, each character is either '0' or '1'. The $i$-th character defines the deliciousness of the $i$-th part.\n\nEach of the following $q$ lines contains two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le n$)\u00a0\u2014 the segment of the corresponding query.\n\n\n-----Output-----\n\nPrint $q$ lines, where $i$-th of them contains a single integer\u00a0\u2014 the answer to the $i$-th query modulo $10^9 + 7$.\n\n\n-----Examples-----\nInput\n4 2\n1011\n1 4\n3 4\n\nOutput\n14\n3\n\nInput\n3 2\n111\n1 2\n3 3\n\nOutput\n3\n1\n\n\n\n-----Note-----\n\nIn the first example: For query $1$: One of the best ways for JATC to eats those parts is in this order: $1$, $4$, $3$, $2$. For query $2$: Both $3$, $4$ and $4$, $3$ ordering give the same answer. \n\nIn the second example, any order of eating parts leads to the same answer."} +{"id": "02002", "text": "Kamil likes streaming the competitive programming videos. His MeTube channel has recently reached $100$ million subscribers. In order to celebrate this, he posted a video with an interesting problem he couldn't solve yet. Can you help him?\n\nYou're given a tree \u2014 a connected undirected graph consisting of $n$ vertices connected by $n - 1$ edges. The tree is rooted at vertex $1$. A vertex $u$ is called an ancestor of $v$ if it lies on the shortest path between the root and $v$. In particular, a vertex is an ancestor of itself.\n\nEach vertex $v$ is assigned its beauty $x_v$ \u2014 a non-negative integer not larger than $10^{12}$. This allows us to define the beauty of a path. Let $u$ be an ancestor of $v$. Then we define the beauty $f(u, v)$ as the greatest common divisor of the beauties of all vertices on the shortest path between $u$ and $v$. Formally, if $u=t_1, t_2, t_3, \\dots, t_k=v$ are the vertices on the shortest path between $u$ and $v$, then $f(u, v) = \\gcd(x_{t_1}, x_{t_2}, \\dots, x_{t_k})$. Here, $\\gcd$ denotes the greatest common divisor of a set of numbers. In particular, $f(u, u) = \\gcd(x_u) = x_u$.\n\nYour task is to find the sum\n\n$$ \\sum_{u\\text{ is an ancestor of }v} f(u, v). $$\n\nAs the result might be too large, please output it modulo $10^9 + 7$.\n\nNote that for each $y$, $\\gcd(0, y) = \\gcd(y, 0) = y$. In particular, $\\gcd(0, 0) = 0$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 100\\,000$) \u2014 the number of vertices in the tree.\n\nThe following line contains $n$ integers $x_1, x_2, \\dots, x_n$ ($0 \\le x_i \\le 10^{12}$). The value $x_v$ denotes the beauty of vertex $v$.\n\nThe following $n - 1$ lines describe the edges of the tree. Each of them contains two integers $a, b$ ($1 \\le a, b \\le n$, $a \\neq b$) \u2014 the vertices connected by a single edge.\n\n\n-----Output-----\n\nOutput the sum of the beauties on all paths $(u, v)$ such that $u$ is ancestor of $v$. This sum should be printed modulo $10^9 + 7$.\n\n\n-----Examples-----\nInput\n5\n4 5 6 0 8\n1 2\n1 3\n1 4\n4 5\n\nOutput\n42\n\nInput\n7\n0 2 3 0 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n\nOutput\n30\n\n\n\n-----Note-----\n\nThe following figure shows all $10$ possible paths for which one endpoint is an ancestor of another endpoint. The sum of beauties of all these paths is equal to $42$: [Image]"} +{"id": "02003", "text": "Author has gone out of the stories about Vasiliy, so here is just a formal task description.\n\nYou are given q queries and a multiset A, initially containing only integer 0. There are three types of queries: \"+ x\"\u00a0\u2014 add integer x to multiset A. \"- x\"\u00a0\u2014 erase one occurrence of integer x from multiset A. It's guaranteed that at least one x is present in the multiset A before this query. \"? x\"\u00a0\u2014 you are given integer x and need to compute the value $\\operatorname{max}_{y \\in A}(x \\oplus y)$, i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer x and some integer y from the multiset A.\n\nMultiset is a set, where equal elements are allowed.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer q (1 \u2264 q \u2264 200 000)\u00a0\u2014 the number of queries Vasiliy has to perform.\n\nEach of the following q lines of the input contains one of three characters '+', '-' or '?' and an integer x_{i} (1 \u2264 x_{i} \u2264 10^9). It's guaranteed that there is at least one query of the third type.\n\nNote, that the integer 0 will always be present in the set A.\n\n\n-----Output-----\n\nFor each query of the type '?' print one integer\u00a0\u2014 the maximum value of bitwise exclusive OR (XOR) of integer x_{i} and some integer from the multiset A.\n\n\n-----Example-----\nInput\n10\n+ 8\n+ 9\n+ 11\n+ 6\n+ 1\n? 3\n- 8\n? 3\n? 8\n? 11\n\nOutput\n11\n10\n14\n13\n\n\n\n-----Note-----\n\nAfter first five operations multiset A contains integers 0, 8, 9, 11, 6 and 1.\n\nThe answer for the sixth query is integer $11 = 3 \\oplus 8$\u00a0\u2014 maximum among integers $3 \\oplus 0 = 3$, $3 \\oplus 9 = 10$, $3 \\oplus 11 = 8$, $3 \\oplus 6 = 5$ and $3 \\oplus 1 = 2$."} +{"id": "02004", "text": "Slava plays his favorite game \"Peace Lightning\". Now he is flying a bomber on a very specific map.\n\nFormally, map is a checkered field of size 1 \u00d7 n, the cells of which are numbered from 1 to n, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he flies very high, but he can drop a bomb in any cell. All tanks in this cell will be damaged.\n\nIf a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell n can only move to the cell n - 1, a tank in the cell 1 can only move to the cell 2). If a tank takes damage for the second time, it's counted as destroyed and never moves again. The tanks move only when they are damaged for the first time, they do not move by themselves.\n\nHelp Slava to destroy all tanks using as few bombs as possible.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 100 000) \u2014 the size of the map.\n\n\n-----Output-----\n\nIn the first line print m \u2014 the minimum number of bombs Slava needs to destroy all tanks.\n\nIn the second line print m integers k_1, k_2, ..., k_{m}. The number k_{i} means that the i-th bomb should be dropped at the cell k_{i}.\n\nIf there are multiple answers, you can print any of them.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n3\n2 1 2 \nInput\n3\n\nOutput\n4\n2 1 3 2"} +{"id": "02005", "text": "Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are n people who plan to move to the cities. The wealth of the i of them is equal to a_{i}. Authorities plan to build two cities, first for n_1 people and second for n_2 people. Of course, each of n candidates can settle in only one of the cities. Thus, first some subset of candidates of size n_1 settle in the first city and then some subset of size n_2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home.\n\nTo make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth a_{i} among all its residents divided by the number of them (n_1 or n_2 depending on the city). The division should be done in real numbers without any rounding.\n\nPlease, help authorities find the optimal way to pick residents for two cities.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, n_1 and n_2 (1 \u2264 n, n_1, n_2 \u2264 100 000, n_1 + n_2 \u2264 n)\u00a0\u2014 the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100 000), the i-th of them is equal to the wealth of the i-th candidate.\n\n\n-----Output-----\n\nPrint one real value\u00a0\u2014 the maximum possible sum of arithmetic means of wealth of cities' residents. You answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. \n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n2 1 1\n1 5\n\nOutput\n6.00000000\n\nInput\n4 2 1\n1 4 2 3\n\nOutput\n6.50000000\n\n\n\n-----Note-----\n\nIn the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second.\n\nIn the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (a_3 + a_4) / 2 + a_2 = (3 + 2) / 2 + 4 = 6.5"} +{"id": "02006", "text": "Inna likes sweets and a game called the \"Candy Matrix\". Today, she came up with the new game \"Candy Matrix 2: Reload\".\n\nThe field for the new game is a rectangle table of size n \u00d7 m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout \"Let's go!\". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:\n\n some dwarf in one of the chosen lines is located in the rightmost cell of his row; some dwarf in the chosen lines is located in the cell with the candy. \n\nThe point of the game is to transport all the dwarves to the candy cells.\n\nInna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n \u2264 1000;\u00a02 \u2264 m \u2264 1000). \n\nNext n lines each contain m characters \u2014 the game field for the \"Candy Martix 2: Reload\". Character \"*\" represents an empty cell of the field, character \"G\" represents a dwarf and character \"S\" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character \"G\" and one character \"S\".\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.\n\n\n-----Examples-----\nInput\n3 4\n*G*S\nG**S\n*G*S\n\nOutput\n2\n\nInput\n1 3\nS*G\n\nOutput\n-1"} +{"id": "02007", "text": "You are given a graph with $n$ nodes and $m$ directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are \"abaca\", then the value of that path is $3$. Your task is find a path whose value is the largest.\n\n\n-----Input-----\n\nThe first line contains two positive integers $n, m$ ($1 \\leq n, m \\leq 300\\,000$), denoting that the graph has $n$ nodes and $m$ directed edges.\n\nThe second line contains a string $s$ with only lowercase English letters. The $i$-th character is the letter assigned to the $i$-th node.\n\nThen $m$ lines follow. Each line contains two integers $x, y$ ($1 \\leq x, y \\leq n$), describing a directed edge from $x$ to $y$. Note that $x$ can be equal to $y$ and there can be multiple edges between $x$ and $y$. Also the graph can be not connected.\n\n\n-----Output-----\n\nOutput a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.\n\n\n-----Examples-----\nInput\n5 4\nabaca\n1 2\n1 3\n3 4\n4 5\n\nOutput\n3\n\nInput\n6 6\nxzyabc\n1 2\n3 1\n2 3\n5 4\n4 3\n6 4\n\nOutput\n-1\n\nInput\n10 14\nxzyzyzyzqx\n1 2\n2 4\n3 5\n4 5\n2 6\n6 8\n6 5\n2 10\n3 9\n10 9\n4 6\n1 10\n2 8\n3 7\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample, the path with largest value is $1 \\to 3 \\to 4 \\to 5$. The value is $3$ because the letter 'a' appears $3$ times."} +{"id": "02008", "text": "During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers\u00a0\u2014 $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $a_i \\cdot (j-1) + b_i \\cdot (n-j)$.\n\nThe director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.\n\nAlthough Stas is able to solve such problems, this was not given to him. He turned for help to you.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^5$)\u00a0\u2014 the number of people in the queue.\n\nEach of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \\leq a_i, b_i \\leq 10^8$)\u00a0\u2014 the characteristic of the student $i$, initially on the position $i$.\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 minimum total dissatisfaction which can be achieved by rearranging people in the queue.\n\n\n-----Examples-----\nInput\n3\n4 2\n2 3\n6 1\n\nOutput\n12\nInput\n4\n2 4\n3 3\n7 1\n2 3\n\nOutput\n25\nInput\n10\n5 10\n12 4\n31 45\n20 55\n30 17\n29 30\n41 32\n7 1\n5 5\n3 15\n\nOutput\n1423\n\n\n-----Note-----\n\nIn the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \\cdot 1+2 \\cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \\cdot 2+3 \\cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \\cdot 0+1 \\cdot 2=2$. The total dissatisfaction will be $12$.\n\nIn the second example, you need to put people in this order: ($3, 2, 4, 1$). The total dissatisfaction will be $25$."} +{"id": "02009", "text": "Alice lives on a flat planet that can be modeled as a square grid of size $n \\times n$, with rows and columns enumerated from $1$ to $n$. We represent the cell at the intersection of row $r$ and column $c$ with ordered pair $(r, c)$. Each cell in the grid is either land or water.\n\n [Image] An example planet with $n = 5$. It also appears in the first sample test. \n\nAlice resides in land cell $(r_1, c_1)$. She wishes to travel to land cell $(r_2, c_2)$. At any moment, she may move to one of the cells adjacent to where she is\u2014in one of the four directions (i.e., up, down, left, or right).\n\nUnfortunately, Alice cannot swim, and there is no viable transportation means other than by foot (i.e., she can walk only on land). As a result, Alice's trip may be impossible.\n\nTo help Alice, you plan to create at most one tunnel between some two land cells. The tunnel will allow Alice to freely travel between the two endpoints. Indeed, creating a tunnel is a lot of effort: the cost of creating a tunnel between cells $(r_s, c_s)$ and $(r_t, c_t)$ is $(r_s-r_t)^2 + (c_s-c_t)^2$.\n\nFor now, your task is to find the minimum possible cost of creating at most one tunnel so that Alice could travel from $(r_1, c_1)$ to $(r_2, c_2)$. If no tunnel needs to be created, the cost is $0$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\leq n \\leq 50$) \u2014 the width of the square grid.\n\nThe second line contains two space-separated integers $r_1$ and $c_1$ ($1 \\leq r_1, c_1 \\leq n$) \u2014 denoting the cell where Alice resides.\n\nThe third line contains two space-separated integers $r_2$ and $c_2$ ($1 \\leq r_2, c_2 \\leq n$) \u2014 denoting the cell to which Alice wishes to travel.\n\nEach of the following $n$ lines contains a string of $n$ characters. The $j$-th character of the $i$-th such line ($1 \\leq i, j \\leq n$) is 0 if $(i, j)$ is land or 1 if $(i, j)$ is water.\n\nIt is guaranteed that $(r_1, c_1)$ and $(r_2, c_2)$ are land.\n\n\n-----Output-----\n\nPrint an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from $(r_1, c_1)$ to $(r_2, c_2)$.\n\n\n-----Examples-----\nInput\n5\n1 1\n5 5\n00001\n11111\n00111\n00110\n00110\n\nOutput\n10\n\nInput\n3\n1 3\n3 1\n010\n101\n010\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first sample, a tunnel between cells $(1, 4)$ and $(4, 5)$ should be created. The cost of doing so is $(1-4)^2 + (4-5)^2 = 10$, which is optimal. This way, Alice could walk from $(1, 1)$ to $(1, 4)$, use the tunnel from $(1, 4)$ to $(4, 5)$, and lastly walk from $(4, 5)$ to $(5, 5)$.\n\nIn the second sample, clearly a tunnel between cells $(1, 3)$ and $(3, 1)$ needs to be created. The cost of doing so is $(1-3)^2 + (3-1)^2 = 8$."} +{"id": "02010", "text": "Sereja has got an array, consisting of n integers, a_1, a_2, ..., a_{n}. Sereja is an active boy, so he is now going to complete m operations. Each operation will have one of the three forms: Make v_{i}-th array element equal to x_{i}. In other words, perform the assignment a_{v}_{i} = x_{i}. Increase each array element by y_{i}. In other words, perform n assignments a_{i} = a_{i} + y_{i} (1 \u2264 i \u2264 n). Take a piece of paper and write out the q_{i}-th array element. That is, the element a_{q}_{i}. \n\nHelp Sereja, complete all his operations.\n\n\n-----Input-----\n\nThe first line contains integers n, m (1 \u2264 n, m \u2264 10^5). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 the original array.\n\nNext m lines describe operations, the i-th line describes the i-th operation. The first number in the i-th line is integer t_{i} (1 \u2264 t_{i} \u2264 3) that represents the operation type. If t_{i} = 1, then it is followed by two integers v_{i} and x_{i}, (1 \u2264 v_{i} \u2264 n, 1 \u2264 x_{i} \u2264 10^9). If t_{i} = 2, then it is followed by integer y_{i} (1 \u2264 y_{i} \u2264 10^4). And if t_{i} = 3, then it is followed by integer q_{i} (1 \u2264 q_{i} \u2264 n).\n\n\n-----Output-----\n\nFor each third type operation print value a_{q}_{i}. Print the values in the order, in which the corresponding queries follow in the input.\n\n\n-----Examples-----\nInput\n10 11\n1 2 3 4 5 6 7 8 9 10\n3 2\n3 9\n2 10\n3 1\n3 10\n1 1 10\n2 10\n2 10\n3 1\n3 10\n3 9\n\nOutput\n2\n9\n11\n20\n30\n40\n39"} +{"id": "02011", "text": "There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt.\n\nSometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. \n\nWhen this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \\neq c$ or $b \\neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by\u00a0$z$ and increase $d(c,b)$ and $d(a,d)$ by\u00a0$z$, where $0 < z \\leq \\min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. \n\nThe total debt is defined as the sum of all debts:\n\n$$\\Sigma_d = \\sum_{a,b} d(a,b)$$\n\nYour goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt.\n\n\n-----Input-----\n\nThe first line contains two space separated integers $n$\u00a0($1 \\leq n \\leq 10^5$) and $m$\u00a0($0 \\leq m \\leq 3\\cdot 10^5$), representing the number of people and the number of debts, respectively.\n\n$m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$\u00a0($1 \\leq u_i, v_i \\leq n, u_i \\neq v_i$), $d_i$\u00a0($1 \\leq d_i \\leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$.\n\n\n-----Output-----\n\nOn the first line print an integer $m'$\u00a0($0 \\leq m' \\leq 3\\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint.\n\nAfter that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \\leq u_i, v_i \\leq n$, $u_i \\neq v_i$ and $0 < d_i \\leq 10^{18}$.\n\nFor each pair $i \\neq j$, it should hold that $u_i \\neq u_j$ or $v_i \\neq v_j$. In other words, each pair of people can be included at most once in the output.\n\n\n-----Examples-----\nInput\n3 2\n1 2 10\n2 3 5\n\nOutput\n2\n1 2 5\n1 3 5\n\nInput\n3 3\n1 2 10\n2 3 15\n3 1 10\n\nOutput\n1\n2 3 5\n\nInput\n4 2\n1 2 12\n3 4 8\n\nOutput\n2\n1 2 12\n3 4 8\n\nInput\n3 4\n2 3 1\n2 3 2\n2 3 4\n2 3 8\n\nOutput\n1\n2 3 15\n\n\n\n-----Note-----\n\nIn the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. \n\nIn the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$."} +{"id": "02012", "text": "Stepan had a favorite string s which consisted of the lowercase letters of the Latin alphabet. \n\nAfter graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers c_1, c_2, ..., c_{n}, where n equals the length of the string s, and c_{i} equals the number of substrings in the string s with the length i, consisting of the same letters. The substring is a sequence of consecutive characters in the string s.\n\nFor example, if the Stepan's favorite string is equal to \"tttesst\", the sequence c looks like: c = [7, 3, 1, 0, 0, 0, 0].\n\nStepan asks you to help to repair his favorite string s according to the given sequence c_1, c_2, ..., c_{n}. \n\n\n-----Input-----\n\nThe first line contains the integer n (1 \u2264 n \u2264 2000) \u2014 the length of the Stepan's favorite string.\n\nThe second line contains the sequence of integers c_1, c_2, ..., c_{n} (0 \u2264 c_{i} \u2264 2000), where c_{i} equals the number of substrings of the string s with the length i, consisting of the same letters.\n\nIt is guaranteed that the input data is such that the answer always exists.\n\n\n-----Output-----\n\nPrint the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet. \n\n\n-----Examples-----\nInput\n6\n6 3 1 0 0 0\n\nOutput\nkkrrrq\nInput\n4\n4 0 0 0\n\nOutput\nabcd\n\n\n\n-----Note-----\n\nIn the first test Stepan's favorite string, for example, can be the string \"kkrrrq\", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3)."} +{"id": "02013", "text": "Petya has a rectangular Board of size $n \\times m$. Initially, $k$ chips are placed on the board, $i$-th chip is located in the cell at the intersection of $sx_i$-th row and $sy_i$-th column.\n\nIn one action, Petya can move all the chips to the left, right, down or up by $1$ cell.\n\nIf the chip was in the $(x, y)$ cell, then after the operation: left, its coordinates will be $(x, y - 1)$; right, its coordinates will be $(x, y + 1)$; down, its coordinates will be $(x + 1, y)$; up, its coordinates will be $(x - 1, y)$. \n\nIf the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.\n\nNote that several chips can be located in the same cell.\n\nFor each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.\n\nSince Petya does not have a lot of free time, he is ready to do no more than $2nm$ actions.\n\nYou have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in $2nm$ actions.\n\n\n-----Input-----\n\nThe first line contains three integers $n, m, k$ ($1 \\le n, m, k \\le 200$) \u2014 the number of rows and columns of the board and the number of chips, respectively.\n\nThe next $k$ lines contains two integers each $sx_i, sy_i$ ($ 1 \\le sx_i \\le n, 1 \\le sy_i \\le m$) \u2014 the starting position of the $i$-th chip.\n\nThe next $k$ lines contains two integers each $fx_i, fy_i$ ($ 1 \\le fx_i \\le n, 1 \\le fy_i \\le m$) \u2014 the position that the $i$-chip should visit at least once.\n\n\n-----Output-----\n\nIn the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.\n\nIn the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters $L, R, D, U$ respectively.\n\nIf the required sequence does not exist, print -1 in the single line.\n\n\n-----Examples-----\nInput\n3 3 2\n1 2\n2 1\n3 3\n3 2\n\nOutput\n3\nDRD\nInput\n5 4 3\n3 4\n3 1\n3 3\n5 3\n1 3\n1 4\n\nOutput\n9\nDDLUUUURR"} +{"id": "02014", "text": "Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?\n\nYou can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 1000;\u00a02 \u2264 k \u2264 5). Each of the next k lines contains integers 1, 2, ..., n in some order \u2014 description of the current permutation.\n\n\n-----Output-----\n\nPrint the length of the longest common subsequence.\n\n\n-----Examples-----\nInput\n4 3\n1 4 2 3\n4 1 2 3\n1 2 4 3\n\nOutput\n3\n\n\n\n-----Note-----\n\nThe answer for the first test sample is subsequence [1, 2, 3]."} +{"id": "02015", "text": "Polycarp is sad \u2014 New Year is coming in few days but there is still no snow in his city. To bring himself New Year mood, he decided to decorate his house with some garlands.\n\nThe local store introduced a new service this year, called \"Build your own garland\". So you can buy some red, green and blue lamps, provide them and the store workers will solder a single garland of them. The resulting garland will have all the lamps you provided put in a line. Moreover, no pair of lamps of the same color will be adjacent to each other in this garland!\n\nFor example, if you provide $3$ red, $3$ green and $3$ blue lamps, the resulting garland can look like this: \"RGBRBGBGR\" (\"RGB\" being the red, green and blue color, respectively). Note that it's ok to have lamps of the same color on the ends of the garland.\n\nHowever, if you provide, say, $1$ red, $10$ green and $2$ blue lamps then the store workers won't be able to build any garland of them. Any garland consisting of these lamps will have at least one pair of lamps of the same color adjacent to each other. Note that the store workers should use all the lamps you provided.\n\nSo Polycarp has bought some sets of lamps and now he wants to know if the store workers can build a garland from each of them.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 100$) \u2014 the number of sets of lamps Polycarp has bought.\n\nEach of the next $t$ lines contains three integers $r$, $g$ and $b$ ($1 \\le r, g, b \\le 10^9$) \u2014 the number of red, green and blue lamps in the set, respectively.\n\n\n-----Output-----\n\nPrint $t$ lines \u2014 for each set of lamps print \"Yes\" if the store workers can build a garland from them and \"No\" otherwise.\n\n\n-----Example-----\nInput\n3\n3 3 3\n1 10 2\n2 1 1\n\nOutput\nYes\nNo\nYes\n\n\n\n-----Note-----\n\nThe first two sets are desribed in the statement.\n\nThe third set produces garland \"RBRG\", for example."} +{"id": "02016", "text": "Vova plays a computer game known as Mages and Monsters. Vova's character is a mage. Though as he has just started, his character knows no spells.\n\nVova's character can learn new spells during the game. Every spell is characterized by two values x_{i} and y_{i} \u2014 damage per second and mana cost per second, respectively. Vova doesn't have to use a spell for an integer amount of seconds. More formally, if he uses a spell with damage x and mana cost y for z seconds, then he will deal x\u00b7z damage and spend y\u00b7z mana (no rounding). If there is no mana left (mana amount is set in the start of the game and it remains the same at the beginning of every fight), then character won't be able to use any spells. It is prohibited to use multiple spells simultaneously.\n\nAlso Vova can fight monsters. Every monster is characterized by two values t_{j} and h_{j} \u2014 monster kills Vova's character in t_{j} seconds and has h_{j} health points. Mana refills after every fight (or Vova's character revives with full mana reserve), so previous fights have no influence on further ones.\n\nVova's character kills a monster, if he deals h_{j} damage to it in no more than t_{j} seconds using his spells (it is allowed to use more than one spell in a fight) and spending no more mana than he had at the beginning of the fight. If monster's health becomes zero exactly in t_{j} seconds (it means that the monster and Vova's character kill each other at the same time), then Vova wins the fight.\n\nYou have to write a program which can answer two types of queries:\n\n 1 x y \u2014 Vova's character learns new spell which deals x damage per second and costs y mana per second. 2 t h \u2014 Vova fights the monster which kills his character in t seconds and has h health points. \n\nNote that queries are given in a different form. Also remember that Vova's character knows no spells at the beginning of the game.\n\nFor every query of second type you have to determine if Vova is able to win the fight with corresponding monster.\n\n\n-----Input-----\n\nThe first line contains two integer numbers q and m (2 \u2264 q \u2264 10^5, 1 \u2264 m \u2264 10^12) \u2014 the number of queries and the amount of mana at the beginning of every fight.\n\ni-th of each next q lines contains three numbers k_{i}, a_{i} and b_{i} (1 \u2264 k_{i} \u2264 2, 1 \u2264 a_{i}, b_{i} \u2264 10^6). \n\nUsing them you can restore queries this way: let j be the index of the last query of second type with positive answer (j = 0 if there were none of these). If k_{i} = 1, then character learns spell with x = (a_{i} + j) mod 10^6 + 1, y = (b_{i} + j) mod 10^6 + 1. If k_{i} = 2, then you have to determine if Vova is able to win the fight against monster with t = (a_{i} + j) mod 10^6 + 1, h = (b_{i} + j) mod 10^6 + 1. \n\n\n-----Output-----\n\nFor every query of second type print YES if Vova is able to win the fight with corresponding monster and NO otherwise.\n\n\n-----Example-----\nInput\n3 100\n1 4 9\n2 19 49\n2 19 49\n\nOutput\nYES\nNO\n\n\n\n-----Note-----\n\nIn first example Vova's character at first learns the spell with 5 damage and 10 mana cost per second. Next query is a fight with monster which can kill character in 20 seconds and has 50 health points. Vova kills it in 10 seconds (spending 100 mana). Next monster has 52 health, so Vova can't deal that much damage with only 100 mana."} +{"id": "02017", "text": "There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We performed n swap operations with this sequence. A swap(a, b) is an operation of swapping the elements of the sequence on positions a and b. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (i, j), that i < j and p_{i} > p_{j}.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of swap operations applied to the sequence.\n\nEach of the next n lines contains two integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 10^9, a_{i} \u2260 b_{i})\u00a0\u2014 the arguments of the swap operation.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of inversions in the resulting sequence.\n\n\n-----Examples-----\nInput\n2\n4 2\n1 4\n\nOutput\n4\n\nInput\n3\n1 6\n3 4\n2 5\n\nOutput\n15\n\n\n\n-----Note-----\n\nIn the first sample the sequence is being modified as follows: $\\{1,2,3,4,5, \\ldots \\} \\rightarrow \\{1,4,3,2,5, \\ldots \\} \\rightarrow \\{2,4,3,1,5 \\ldots \\}$. It has 4 inversions formed by index pairs (1, 4), (2, 3), (2, 4) and (3, 4)."} +{"id": "02018", "text": "Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by $n$ sectors, and the outer area is equally divided by $m$ sectors. A wall exists between each pair of sectors of same area (inner or outer), but there is no wall between the inner area and the outer area. A wall always exists at the 12 o'clock position.\n\n $0$ \n\nThe inner area's sectors are denoted as $(1,1), (1,2), \\dots, (1,n)$ in clockwise direction. The outer area's sectors are denoted as $(2,1), (2,2), \\dots, (2,m)$ in the same manner. For a clear understanding, see the example image above.\n\nAmugae wants to know if he can move from one sector to another sector. He has $q$ questions.\n\nFor each question, check if he can move between two given sectors.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $q$ ($1 \\le n, m \\le 10^{18}$, $1 \\le q \\le 10^4$)\u00a0\u2014 the number of sectors in the inner area, the number of sectors in the outer area and the number of questions.\n\nEach of the next $q$ lines contains four integers $s_x$, $s_y$, $e_x$, $e_y$ ($1 \\le s_x, e_x \\le 2$; if $s_x = 1$, then $1 \\le s_y \\le n$, otherwise $1 \\le s_y \\le m$; constraints on $e_y$ are similar). Amague wants to know if it is possible to move from sector $(s_x, s_y)$ to sector $(e_x, e_y)$.\n\n\n-----Output-----\n\nFor each question, print \"YES\" if Amugae can move from $(s_x, s_y)$ to $(e_x, e_y)$, and \"NO\" otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n4 6 3\n1 1 2 3\n2 6 1 2\n2 6 2 4\n\nOutput\nYES\nNO\nYES\n\n\n\n-----Note-----\n\nExample is shown on the picture in the statement."} +{"id": "02019", "text": "Alica and Bob are playing a game.\n\nInitially they have a binary string $s$ consisting of only characters 0 and 1.\n\nAlice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, Alice makes the third one, and so on. During each move, the current player must choose two different adjacent characters of string $s$ and delete them. For example, if $s = 1011001$ then the following moves are possible: delete $s_1$ and $s_2$: $\\textbf{10}11001 \\rightarrow 11001$; delete $s_2$ and $s_3$: $1\\textbf{01}1001 \\rightarrow 11001$; delete $s_4$ and $s_5$: $101\\textbf{10}01 \\rightarrow 10101$; delete $s_6$ and $s_7$: $10110\\textbf{01} \\rightarrow 10110$. \n\nIf a player can't make any move, they lose. Both players play optimally. You have to determine if Alice can win.\n\n\n-----Input-----\n\nFirst line contains one integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases.\n\nOnly line of each test case contains one string $s$ ($1 \\le |s| \\le 100$), consisting of only characters 0 and 1.\n\n\n-----Output-----\n\nFor each test case print answer in the single line.\n\nIf Alice can win print DA (YES in Russian) in any register. Otherwise print NET (NO in Russian) in any register.\n\n\n-----Example-----\nInput\n3\n01\n1111\n0011\n\nOutput\nDA\nNET\nNET\n\n\n\n-----Note-----\n\nIn the first test case after Alice's move string $s$ become empty and Bob can not make any move.\n\nIn the second test case Alice can not make any move initially.\n\nIn the third test case after Alice's move string $s$ turn into $01$. Then, after Bob's move string $s$ become empty and Alice can not make any move."} +{"id": "02020", "text": "Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 \u00d7 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.\n\nThe morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game: First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal. Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off. \n\nInna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers x_{i}, y_{i} \u2014 the coordinates of the i-th alarm clock (0 \u2264 x_{i}, y_{i} \u2264 100).\n\nNote that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the minimum number of segments Inna will have to draw if she acts optimally.\n\n\n-----Examples-----\nInput\n4\n0 0\n0 1\n0 2\n1 0\n\nOutput\n2\n\nInput\n4\n0 0\n0 1\n1 0\n1 1\n\nOutput\n2\n\nInput\n4\n1 1\n1 2\n2 3\n3 3\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample, Inna first chooses type \"vertical segments\", and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments.\n\nIn the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game."} +{"id": "02021", "text": "You came to a local shop and want to buy some chocolate bars. There are $n$ bars in the shop, $i$-th of them costs $a_i$ coins (and you want to buy all of them).\n\nYou have $m$ different coupons that allow you to buy chocolate bars. $i$-th coupon allows you to buy $q_i$ chocolate bars while you have to pay only for the $q_i - 1$ most expensive ones (so, the cheapest bar of those $q_i$ bars is for free).\n\nYou can use only one coupon; if you use coupon $i$, you have to choose $q_i$ bars and buy them using the coupon, and buy all the remaining $n - q_i$ bars without any discounts.\n\nTo decide which coupon to choose, you want to know what will be the minimum total amount of money you have to pay if you use one of the coupons optimally.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 3 \\cdot 10^5$) \u2014 the number of chocolate bars in the shop.\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the cost of $i$-th chocolate bar.\n\nThe third line contains one integer $m$ ($1 \\le m \\le n - 1$) \u2014 the number of coupons you have.\n\nThe fourth line contains $m$ integers $q_1$, $q_2$, ..., $q_m$ ($2 \\le q_i \\le n$), where $q_i$ is the number of chocolate bars you have to buy using $i$-th coupon so that the least expensive of them will be for free. All values of $q_i$ are pairwise distinct.\n\n\n-----Output-----\n\nPrint $m$ integers, $i$-th of them should be the minimum amount of money you have to pay if you buy $q_i$ bars with $i$-th coupon, and all the remaining bars one by one for their full price.\n\n\n-----Example-----\nInput\n7\n7 1 3 1 4 10 8\n2\n3 4\n\nOutput\n27\n30\n\n\n\n-----Note-----\n\nConsider the first example.\n\nIf we use the first coupon, we may choose chocolate bars having indices $1$, $6$ and $7$, and we pay $18$ coins for them and $9$ coins for all other bars.\n\nIf we use the second coupon, we may choose chocolate bars having indices $1$, $5$, $6$ and $7$, and we pay $25$ coins for them and $5$ coins for all other bars."} +{"id": "02022", "text": "Given a connected undirected graph with $n$ vertices and an integer $k$, you have to either: either find an independent set that has exactly $\\lceil\\frac{k}{2}\\rceil$ vertices. or find a simple cycle of length at most $k$. \n\nAn independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. \n\nI have a proof that for any input you can always solve at least one of these problems, but it's left as an exercise for the reader.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$, and $k$ ($3 \\le k \\le n \\le 10^5$, $n-1 \\le m \\le 2 \\cdot 10^5$)\u00a0\u2014 the number of vertices and edges in the graph, and the parameter $k$ from the statement.\n\nEach of the next $m$ lines contains two integers $u$ and $v$ ($1 \\le u,v \\le n$) that mean there's an edge between vertices $u$ and $v$. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.\n\n\n-----Output-----\n\nIf you choose to solve the first problem, then on the first line print $1$, followed by a line containing $\\lceil\\frac{k}{2}\\rceil$ distinct integers not exceeding $n$, the vertices in the desired independent set.\n\nIf you, however, choose to solve the second problem, then on the first line print $2$, followed by a line containing one integer, $c$, representing the length of the found cycle, followed by a line containing $c$ distinct integers not exceeding $n$, the vertices in the desired cycle, in the order they appear in the cycle.\n\n\n-----Examples-----\nInput\n4 4 3\n1 2\n2 3\n3 4\n4 1\n\nOutput\n1\n1 3 \nInput\n4 5 3\n1 2\n2 3\n3 4\n4 1\n2 4\n\nOutput\n2\n3\n2 3 4 \nInput\n4 6 3\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n\nOutput\n2\n3\n1 2 3 \nInput\n5 4 5\n1 2\n1 3\n2 4\n2 5\n\nOutput\n1\n1 4 5 \n\n\n-----Note-----\n\nIn the first sample:\n\n[Image]\n\nNotice that printing the independent set $\\{2,4\\}$ is also OK, but printing the cycle $1-2-3-4$ isn't, because its length must be at most $3$.\n\nIn the second sample:\n\n$N$\n\nNotice that printing the independent set $\\{1,3\\}$ or printing the cycle $2-1-4$ is also OK.\n\nIn the third sample:\n\n[Image]\n\nIn the fourth sample:\n\n$A$"} +{"id": "02023", "text": "We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.\n\nMore formally, you've got two arrays of integers a_1, a_2, ..., a_{n} and b_1, b_2, ..., b_{n} of length n. Also, you've got m queries of two types: Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute b_{y} + q = a_{x} + q for all integer q (0 \u2264 q < k). The given operation is correct \u2014 both subsegments do not touch unexistent elements. Determine the value in position x of array b, that is, find value b_{x}. \n\nFor each query of the second type print the result \u2014 the value of the corresponding element of array b.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m (1 \u2264 n, m \u2264 10^5) \u2014 the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a_1, a_2, ..., a_{n} (|a_{i}| \u2264 10^9). The third line contains an array of integers b_1, b_2, ..., b_{n} (|b_{i}| \u2264 10^9).\n\nNext m lines contain the descriptions of the queries. The i-th line first contains integer t_{i} \u2014 the type of the i-th query (1 \u2264 t_{i} \u2264 2). If t_{i} = 1, then the i-th query means the copying operation. If t_{i} = 2, then the i-th query means taking the value in array b. If t_{i} = 1, then the query type is followed by three integers x_{i}, y_{i}, k_{i} (1 \u2264 x_{i}, y_{i}, k_{i} \u2264 n) \u2014 the parameters of the copying query. If t_{i} = 2, then the query type is followed by integer x_{i} (1 \u2264 x_{i} \u2264 n) \u2014 the position in array b.\n\nAll numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.\n\n\n-----Output-----\n\nFor each second type query print the result on a single line.\n\n\n-----Examples-----\nInput\n5 10\n1 2 0 -1 3\n3 1 5 -2 0\n2 5\n1 3 3 3\n2 5\n2 4\n2 1\n1 2 1 4\n2 1\n2 4\n1 4 2 1\n2 2\n\nOutput\n0\n3\n-1\n3\n2\n3\n-1"} +{"id": "02024", "text": "$n$ fishermen have just returned from a fishing vacation. The $i$-th fisherman has caught a fish of weight $a_i$.\n\nFishermen are going to show off the fish they caught to each other. To do so, they firstly choose an order in which they show their fish (each fisherman shows his fish exactly once, so, formally, the order of showing fish is a permutation of integers from $1$ to $n$). Then they show the fish they caught according to the chosen order. When a fisherman shows his fish, he might either become happy, become sad, or stay content.\n\nSuppose a fisherman shows a fish of weight $x$, and the maximum weight of a previously shown fish is $y$ ($y = 0$ if that fisherman is the first to show his fish). Then:\n\n if $x \\ge 2y$, the fisherman becomes happy; if $2x \\le y$, the fisherman becomes sad; if none of these two conditions is met, the fisherman stays content. \n\nLet's call an order in which the fishermen show their fish emotional if, after all fishermen show their fish according to this order, each fisherman becomes either happy or sad. Calculate the number of emotional orders modulo $998244353$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 5000$).\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of emotional orders, taken modulo $998244353$.\n\n\n-----Examples-----\nInput\n4\n1 1 4 9\n\nOutput\n20\n\nInput\n4\n4 3 2 1\n\nOutput\n0\n\nInput\n3\n4 2 1\n\nOutput\n6\n\nInput\n8\n42 1337 13 37 420 666 616 97\n\nOutput\n19200"} +{"id": "02025", "text": "You are given several queries. In the i-th query you are given a single positive integer n_{i}. You are to represent n_{i} as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.\n\nAn integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.\n\n\n-----Input-----\n\nThe first line contains single integer q (1 \u2264 q \u2264 10^5)\u00a0\u2014 the number of queries.\n\nq lines follow. The (i + 1)-th line contains single integer n_{i} (1 \u2264 n_{i} \u2264 10^9)\u00a0\u2014 the i-th query.\n\n\n-----Output-----\n\nFor each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.\n\n\n-----Examples-----\nInput\n1\n12\n\nOutput\n3\n\nInput\n2\n6\n8\n\nOutput\n1\n2\n\nInput\n3\n1\n2\n3\n\nOutput\n-1\n-1\n-1\n\n\n\n-----Note-----\n\n12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.\n\n8 = 4 + 4, 6 can't be split into several composite summands.\n\n1, 2, 3 are less than any composite number, so they do not have valid splittings."} +{"id": "02026", "text": "Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p_1, p_2, ..., p_{m} with integer coordinates, do the following: denote its initial location by p_0. First, the robot will move from p_0 to p_1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p_1, it'll move to p_2, again, choosing one of the shortest ways, then to p_3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.\n\nWhile Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.\n\n\n-----Input-----\n\nThe first line of input contains the only positive integer n (1 \u2264 n \u2264 2\u00b710^5) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R\u00a0\u2014 to the right, U\u00a0\u2014 to the top and D\u00a0\u2014 to the bottom. Have a look at the illustrations for better explanation.\n\n\n-----Output-----\n\nThe only line of input should contain the minimum possible length of the sequence.\n\n\n-----Examples-----\nInput\n4\nRURD\n\nOutput\n2\n\nInput\n6\nRRULDD\n\nOutput\n2\n\nInput\n26\nRRRULURURUULULLLDLDDRDRDLD\n\nOutput\n7\n\nInput\n3\nRLL\n\nOutput\n2\n\nInput\n4\nLRLR\n\nOutput\n4\n\n\n\n-----Note-----\n\nThe illustrations to the first three tests are given below.\n\n[Image] [Image] [Image]\n\nThe last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence."} +{"id": "02027", "text": "There are n integers b_1, b_2, ..., b_{n} written in a row. For all i from 1 to n, values a_{i} are defined by the crows performing the following procedure:\n\n The crow sets a_{i} initially 0. The crow then adds b_{i} to a_{i}, subtracts b_{i} + 1, adds the b_{i} + 2 number, and so on until the n'th number. Thus, a_{i} = b_{i} - b_{i} + 1 + b_{i} + 2 - b_{i} + 3.... \n\nMemory gives you the values a_1, a_2, ..., a_{n}, and he now wants you to find the initial numbers b_1, b_2, ..., b_{n} written in the row? Can you do it?\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (2 \u2264 n \u2264 100 000)\u00a0\u2014 the number of integers written in the row.\n\nThe next line contains n, the i'th of which is a_{i} ( - 10^9 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the value of the i'th number.\n\n\n-----Output-----\n\nPrint n integers corresponding to the sequence b_1, b_2, ..., b_{n}. It's guaranteed that the answer is unique and fits in 32-bit integer type.\n\n\n-----Examples-----\nInput\n5\n6 -4 8 -2 3\n\nOutput\n2 4 6 1 3 \n\nInput\n5\n3 -2 -1 5 6\n\nOutput\n1 -3 4 11 6 \n\n\n\n-----Note-----\n\nIn the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and - 4 = 4 - 6 + 1 - 3.\n\nIn the second sample test, the sequence 1, - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6."} +{"id": "02028", "text": "A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement.\n\nHow many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 10^9 + 7.\n\n\n-----Input-----\n\nInput will begin with a line containing N (1 \u2264 N \u2264 100000), the number of engineers. \n\nN lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2\u00b7N. It is guaranteed that no two engineers sit at the same desk.\n\n\n-----Output-----\n\nPrint the number of possible assignments, modulo 1000000007 = 10^9 + 7.\n\n\n-----Examples-----\nInput\n4\n1 5\n5 2\n3 7\n7 3\n\nOutput\n6\n\nInput\n5\n1 10\n2 10\n3 10\n4 10\n5 5\n\nOutput\n5\n\n\n\n-----Note-----\n\nThese are the possible assignments for the first example: 1 5 3 7 1 2 3 7 5 2 3 7 1 5 7 3 1 2 7 3 5 2 7 3"} +{"id": "02029", "text": "You are given a tree (an undirected connected graph without cycles) and an integer $s$.\n\nVanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $s$. At the same time, he wants to make the diameter of the tree as small as possible.\n\nLet's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.\n\nFind the minimum possible diameter that Vanya can get.\n\n\n-----Input-----\n\nThe first line contains two integer numbers $n$ and $s$ ($2 \\leq n \\leq 10^5$, $1 \\leq s \\leq 10^9$) \u2014 the number of vertices in the tree and the sum of edge weights.\n\nEach of the following $n\u22121$ lines contains two space-separated integer numbers $a_i$ and $b_i$ ($1 \\leq a_i, b_i \\leq n$, $a_i \\neq b_i$) \u2014 the indexes of vertices connected by an edge. The edges are undirected.\n\nIt is guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nPrint the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $s$.\n\nYour answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$.\n\nFormally, let your answer be $a$, and the jury's answer be $b$. Your answer is considered correct if $\\frac {|a-b|} {max(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n4 3\n1 2\n1 3\n1 4\n\nOutput\n2.000000000000000000\nInput\n6 1\n2 1\n2 3\n2 5\n5 4\n5 6\n\nOutput\n0.500000000000000000\nInput\n5 5\n1 2\n2 3\n3 4\n3 5\n\nOutput\n3.333333333333333333\n\n\n-----Note-----\n\nIn the first example it is necessary to put weights like this: [Image] \n\nIt is easy to see that the diameter of this tree is $2$. It can be proved that it is the minimum possible diameter.\n\nIn the second example it is necessary to put weights like this: [Image]"} +{"id": "02030", "text": "Little Artyom decided to study probability theory. He found a book with a lot of nice exercises and now wants you to help him with one of them.\n\nConsider two dices. When thrown each dice shows some integer from 1 to n inclusive. For each dice the probability of each outcome is given (of course, their sum is 1), and different dices may have different probability distributions.\n\nWe throw both dices simultaneously and then calculate values max(a, b) and min(a, b), where a is equal to the outcome of the first dice, while b is equal to the outcome of the second dice. You don't know the probability distributions for particular values on each dice, but you know the probability distributions for max(a, b) and min(a, b). That is, for each x from 1 to n you know the probability that max(a, b) would be equal to x and the probability that min(a, b) would be equal to x. Find any valid probability distribution for values on the dices. It's guaranteed that the input data is consistent, that is, at least one solution exists.\n\n\n-----Input-----\n\nFirst line contains the integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of different values for both dices.\n\nSecond line contains an array consisting of n real values with up to 8 digits after the decimal point \u00a0\u2014 probability distribution for max(a, b), the i-th of these values equals to the probability that max(a, b) = i. It's guaranteed that the sum of these values for one dice is 1. The third line contains the description of the distribution min(a, b) in the same format.\n\n\n-----Output-----\n\nOutput two descriptions of the probability distribution for a on the first line and for b on the second line. \n\nThe answer will be considered correct if each value of max(a, b) and min(a, b) probability distribution values does not differ by more than 10^{ - 6} from ones given in input. Also, probabilities should be non-negative and their sums should differ from 1 by no more than 10^{ - 6}.\n\n\n-----Examples-----\nInput\n2\n0.25 0.75\n0.75 0.25\n\nOutput\n0.5 0.5 \n0.5 0.5 \n\nInput\n3\n0.125 0.25 0.625\n0.625 0.25 0.125\n\nOutput\n0.25 0.25 0.5 \n0.5 0.25 0.25"} +{"id": "02031", "text": "This is the easier version of the problem. In this version $1 \\le n, m \\le 100$. You can hack this problem only if you solve and lock both problems.\n\nYou are given a sequence of integers $a=[a_1,a_2,\\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$:\n\n $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. \n\nSuppose that an additional non-negative integer $k$ ($1 \\le k \\le n$) is given, then the subsequence is called optimal if:\n\n it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. \n\nRecall that the sequence $b=[b_1, b_2, \\dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \\dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \\le t \\le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t v$). A portal can be used as follows: if you are currently in the castle $u$, you may send one warrior to defend castle $v$. \n\nObviously, when you order your warrior to defend some castle, he leaves your army.\n\nYou capture the castles in fixed order: you have to capture the first one, then the second one, and so on. After you capture the castle $i$ (but only before capturing castle $i + 1$) you may recruit new warriors from castle $i$, leave a warrior to defend castle $i$, and use any number of portals leading from castle $i$ to other castles having smaller numbers. As soon as you capture the next castle, these actions for castle $i$ won't be available to you.\n\nIf, during some moment in the game, you don't have enough warriors to capture the next castle, you lose. Your goal is to maximize the sum of importance values over all defended castles (note that you may hire new warriors in the last castle, defend it and use portals leading from it even after you capture it \u2014 your score will be calculated afterwards).\n\nCan you determine an optimal strategy of capturing and defending the castles?\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $k$ ($1 \\le n \\le 5000$, $0 \\le m \\le \\min(\\frac{n(n - 1)}{2}, 3 \\cdot 10^5)$, $0 \\le k \\le 5000$) \u2014 the number of castles, the number of portals and initial size of your army, respectively.\n\nThen $n$ lines follow. The $i$-th line describes the $i$-th castle with three integers $a_i$, $b_i$ and $c_i$ ($0 \\le a_i, b_i, c_i \\le 5000$) \u2014 the number of warriors required to capture the $i$-th castle, the number of warriors available for hire in this castle and its importance value.\n\nThen $m$ lines follow. The $i$-th line describes the $i$-th portal with two integers $u_i$ and $v_i$ ($1 \\le v_i < u_i \\le n$), meaning that the portal leads from the castle $u_i$ to the castle $v_i$. There are no two same portals listed.\n\nIt is guaranteed that the size of your army won't exceed $5000$ under any circumstances (i. e. $k + \\sum\\limits_{i = 1}^{n} b_i \\le 5000$).\n\n\n-----Output-----\n\nIf it's impossible to capture all the castles, print one integer $-1$.\n\nOtherwise, print one integer equal to the maximum sum of importance values of defended castles.\n\n\n-----Examples-----\nInput\n4 3 7\n7 4 17\n3 0 8\n11 2 0\n13 3 5\n3 1\n2 1\n4 3\n\nOutput\n5\n\nInput\n4 3 7\n7 4 17\n3 0 8\n11 2 0\n13 3 5\n3 1\n2 1\n4 1\n\nOutput\n22\n\nInput\n4 3 7\n7 4 17\n3 0 8\n11 2 0\n14 3 5\n3 1\n2 1\n4 3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nThe best course of action in the first example is as follows:\n\n capture the first castle; hire warriors from the first castle, your army has $11$ warriors now; capture the second castle; capture the third castle; hire warriors from the third castle, your army has $13$ warriors now; capture the fourth castle; leave one warrior to protect the fourth castle, your army has $12$ warriors now. \n\nThis course of action (and several other ones) gives $5$ as your total score.\n\nThe best course of action in the second example is as follows:\n\n capture the first castle; hire warriors from the first castle, your army has $11$ warriors now; capture the second castle; capture the third castle; hire warriors from the third castle, your army has $13$ warriors now; capture the fourth castle; leave one warrior to protect the fourth castle, your army has $12$ warriors now; send one warrior to protect the first castle through the third portal, your army has $11$ warriors now. \n\nThis course of action (and several other ones) gives $22$ as your total score.\n\nIn the third example it's impossible to capture the last castle: you need $14$ warriors to do so, but you can accumulate no more than $13$ without capturing it."} +{"id": "02033", "text": "The map of Bertown can be represented as a set of $n$ intersections, numbered from $1$ to $n$ and connected by $m$ one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection $v$ to another intersection $u$ is the path that starts in $v$, ends in $u$ and has the minimum length among all such paths.\n\nPolycarp lives near the intersection $s$ and works in a building near the intersection $t$. Every day he gets from $s$ to $t$ by car. Today he has chosen the following path to his workplace: $p_1$, $p_2$, ..., $p_k$, where $p_1 = s$, $p_k = t$, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from $s$ to $t$.\n\nPolycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection $s$, the system chooses some shortest path from $s$ to $t$ and shows it to Polycarp. Let's denote the next intersection in the chosen path as $v$. If Polycarp chooses to drive along the road from $s$ to $v$, then the navigator shows him the same shortest path (obviously, starting from $v$ as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection $w$ instead, the navigator rebuilds the path: as soon as Polycarp arrives at $w$, the navigation system chooses some shortest path from $w$ to $t$ and shows it to Polycarp. The same process continues until Polycarp arrives at $t$: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.\n\nHere is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path $[1, 2, 3, 4]$ ($s = 1$, $t = 4$): \n\nWhen Polycarp starts at $1$, the system chooses some shortest path from $1$ to $4$. There is only one such path, it is $[1, 5, 4]$; Polycarp chooses to drive to $2$, which is not along the path chosen by the system. When Polycarp arrives at $2$, the navigator rebuilds the path by choosing some shortest path from $2$ to $4$, for example, $[2, 6, 4]$ (note that it could choose $[2, 3, 4]$); Polycarp chooses to drive to $3$, which is not along the path chosen by the system. When Polycarp arrives at $3$, the navigator rebuilds the path by choosing the only shortest path from $3$ to $4$, which is $[3, 4]$; Polycarp arrives at $4$ along the road chosen by the navigator, so the system does not have to rebuild anything. \n\nOverall, we get $2$ rebuilds in this scenario. Note that if the system chose $[2, 3, 4]$ instead of $[2, 6, 4]$ during the second step, there would be only $1$ rebuild (since Polycarp goes along the path, so the system maintains the path $[3, 4]$ during the third step).\n\nThe example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($2 \\le n \\le m \\le 2 \\cdot 10^5$) \u2014 the number of intersections and one-way roads in Bertown, respectively.\n\nThen $m$ lines follow, each describing a road. Each line contains two integers $u$ and $v$ ($1 \\le u, v \\le n$, $u \\ne v$) denoting a road from intersection $u$ to intersection $v$. All roads in Bertown are pairwise distinct, which means that each ordered pair $(u, v)$ appears at most once in these $m$ lines (but if there is a road $(u, v)$, the road $(v, u)$ can also appear).\n\nThe following line contains one integer $k$ ($2 \\le k \\le n$) \u2014 the number of intersections in Polycarp's path from home to his workplace.\n\nThe last line contains $k$ integers $p_1$, $p_2$, ..., $p_k$ ($1 \\le p_i \\le n$, all these integers are pairwise distinct) \u2014 the intersections along Polycarp's path in the order he arrived at them. $p_1$ is the intersection where Polycarp lives ($s = p_1$), and $p_k$ is the intersection where Polycarp's workplace is situated ($t = p_k$). It is guaranteed that for every $i \\in [1, k - 1]$ the road from $p_i$ to $p_{i + 1}$ exists, so the path goes along the roads of Bertown. \n\n\n-----Output-----\n\nPrint two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.\n\n\n-----Examples-----\nInput\n6 9\n1 5\n5 4\n1 2\n2 3\n3 4\n4 1\n2 6\n6 4\n4 2\n4\n1 2 3 4\n\nOutput\n1 2\n\nInput\n7 7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 1\n7\n1 2 3 4 5 6 7\n\nOutput\n0 0\n\nInput\n8 13\n8 7\n8 6\n7 5\n7 4\n6 5\n6 4\n5 3\n5 2\n4 3\n4 2\n3 1\n2 1\n1 8\n5\n8 7 5 2 1\n\nOutput\n0 3"} +{"id": "02034", "text": "Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.\n\nThe President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).\n\nIn order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.\n\nHelp the Ministry of Transport to find the minimum possible number of separate cities after the reform.\n\n\n-----Input-----\n\nThe first line of the input contains two positive integers, n and m \u2014 the number of the cities and the number of roads in Berland (2 \u2264 n \u2264 100 000, 1 \u2264 m \u2264 100 000). \n\nNext m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 \u2264 x_{i}, y_{i} \u2264 n, x_{i} \u2260 y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.\n\nIt is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of separated cities after the reform.\n\n\n-----Examples-----\nInput\n4 3\n2 1\n1 3\n4 3\n\nOutput\n1\n\nInput\n5 5\n2 1\n1 3\n2 3\n2 5\n4 3\n\nOutput\n0\n\nInput\n6 5\n1 2\n2 3\n4 5\n4 6\n5 6\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample the following road orientation is allowed: $1 \\rightarrow 2$, $1 \\rightarrow 3$, $3 \\rightarrow 4$.\n\nThe second sample: $1 \\rightarrow 2$, $3 \\rightarrow 1$, $2 \\rightarrow 3$, $2 \\rightarrow 5$, $3 \\rightarrow 4$.\n\nThe third sample: $1 \\rightarrow 2$, $2 \\rightarrow 3$, $4 \\rightarrow 5$, $5 \\rightarrow 6$, $6 \\rightarrow 4$."} +{"id": "02035", "text": "The map of the capital of Berland can be viewed on the infinite coordinate plane. Each point with integer coordinates contains a building, and there are streets connecting every building to four neighbouring buildings. All streets are parallel to the coordinate axes.\n\nThe main school of the capital is located in $(s_x, s_y)$. There are $n$ students attending this school, the $i$-th of them lives in the house located in $(x_i, y_i)$. It is possible that some students live in the same house, but no student lives in $(s_x, s_y)$.\n\nAfter classes end, each student walks from the school to his house along one of the shortest paths. So the distance the $i$-th student goes from the school to his house is $|s_x - x_i| + |s_y - y_i|$.\n\nThe Provision Department of Berland has decided to open a shawarma tent somewhere in the capital (at some point with integer coordinates). It is considered that the $i$-th student will buy a shawarma if at least one of the shortest paths from the school to the $i$-th student's house goes through the point where the shawarma tent is located. It is forbidden to place the shawarma tent at the point where the school is located, but the coordinates of the shawarma tent may coincide with the coordinates of the house of some student (or even multiple students).\n\nYou want to find the maximum possible number of students buying shawarma and the optimal location for the tent itself.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $s_x$, $s_y$ ($1 \\le n \\le 200\\,000$, $0 \\le s_x, s_y \\le 10^{9}$) \u2014 the number of students and the coordinates of the school, respectively.\n\nThen $n$ lines follow. The $i$-th of them contains two integers $x_i$, $y_i$ ($0 \\le x_i, y_i \\le 10^{9}$) \u2014 the location of the house where the $i$-th student lives. Some locations of houses may coincide, but no student lives in the same location where the school is situated.\n\n\n-----Output-----\n\nThe output should consist of two lines. The first of them should contain one integer $c$ \u2014 the maximum number of students that will buy shawarmas at the tent. \n\nThe second line should contain two integers $p_x$ and $p_y$ \u2014 the coordinates where the tent should be located. If there are multiple answers, print any of them. Note that each of $p_x$ and $p_y$ should be not less than $0$ and not greater than $10^{9}$.\n\n\n-----Examples-----\nInput\n4 3 2\n1 3\n4 2\n5 1\n4 1\n\nOutput\n3\n4 2\n\nInput\n3 100 100\n0 0\n0 0\n100 200\n\nOutput\n2\n99 100\n\nInput\n7 10 12\n5 6\n20 23\n15 4\n16 5\n4 54\n12 1\n4 15\n\nOutput\n4\n10 11\n\n\n\n-----Note-----\n\nIn the first example, If we build the shawarma tent in $(4, 2)$, then the students living in $(4, 2)$, $(4, 1)$ and $(5, 1)$ will visit it.\n\nIn the second example, it is possible to build the shawarma tent in $(1, 1)$, then both students living in $(0, 0)$ will visit it."} +{"id": "02036", "text": "Boboniu likes playing chess with his employees. As we know, no employee can beat the boss in the chess game, so Boboniu has never lost in any round.\n\nYou are a new applicant for his company. Boboniu will test you with the following chess question:\n\nConsider a $n\\times m$ grid (rows are numbered from $1$ to $n$, and columns are numbered from $1$ to $m$). You have a chess piece, and it stands at some cell $(S_x,S_y)$ which is not on the border (i.e. $2 \\le S_x \\le n-1$ and $2 \\le S_y \\le m-1$).\n\nFrom the cell $(x,y)$, you can move your chess piece to $(x,y')$ ($1\\le y'\\le m, y' \\neq y$) or $(x',y)$ ($1\\le x'\\le n, x'\\neq x$). In other words, the chess piece moves as a rook. From the cell, you can move to any cell on the same row or column.\n\nYour goal is to visit each cell exactly once. Can you find a solution?\n\nNote that cells on the path between two adjacent cells in your route are not counted as visited, and it is not required to return to the starting point.\n\n\n-----Input-----\n\nThe only line of the input contains four integers $n$, $m$, $S_x$ and $S_y$ ($3\\le n,m\\le 100$, $2 \\le S_x \\le n-1$, $2 \\le S_y \\le m-1$) \u2014 the number of rows, the number of columns, and the initial position of your chess piece, respectively.\n\n\n-----Output-----\n\nYou should print $n\\cdot m$ lines.\n\nThe $i$-th line should contain two integers $x_i$ and $y_i$ ($1 \\leq x_i \\leq n$, $1 \\leq y_i \\leq m$), denoting the $i$-th cell that you visited. You should print exactly $nm$ pairs $(x_i, y_i)$, they should cover all possible pairs $(x_i, y_i)$, such that $1 \\leq x_i \\leq n$, $1 \\leq y_i \\leq m$.\n\nWe can show that under these constraints there always exists a solution. If there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n3 3 2 2\n\nOutput\n2 2\n1 2\n1 3\n2 3\n3 3\n3 2\n3 1\n2 1\n1 1\n\nInput\n3 4 2 2\n\nOutput\n2 2\n2 1\n2 3\n2 4\n1 4\n3 4\n3 3\n3 2\n3 1\n1 1\n1 2\n1 3\n\n\n\n-----Note-----\n\nPossible routes for two examples: [Image]"} +{"id": "02037", "text": "Arkady coordinates rounds on some not really famous competitive programming platform. Each round features $n$ problems of distinct difficulty, the difficulties are numbered from $1$ to $n$.\n\nTo hold a round Arkady needs $n$ new (not used previously) problems, one for each difficulty. As for now, Arkady creates all the problems himself, but unfortunately, he can't just create a problem of a desired difficulty. Instead, when he creates a problem, he evaluates its difficulty from $1$ to $n$ and puts it into the problems pool.\n\nAt each moment when Arkady can choose a set of $n$ new problems of distinct difficulties from the pool, he holds a round with these problems and removes them from the pool. Arkady always creates one problem at a time, so if he can hold a round after creating a problem, he immediately does it.\n\nYou are given a sequence of problems' difficulties in the order Arkady created them. For each problem, determine whether Arkady held the round right after creating this problem, or not. Initially the problems pool is empty.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 10^5$)\u00a0\u2014 the number of difficulty levels and the number of problems Arkady created.\n\nThe second line contains $m$ integers $a_1, a_2, \\ldots, a_m$ ($1 \\le a_i \\le n$)\u00a0\u2014 the problems' difficulties in the order Arkady created them.\n\n\n-----Output-----\n\nPrint a line containing $m$ digits. The $i$-th digit should be $1$ if Arkady held the round after creation of the $i$-th problem, and $0$ otherwise.\n\n\n-----Examples-----\nInput\n3 11\n2 3 1 2 2 2 3 2 2 3 1\n\nOutput\n00100000001\n\nInput\n4 8\n4 1 3 3 2 3 3 3\n\nOutput\n00001000\n\n\n\n-----Note-----\n\nIn the first example Arkady held the round after the first three problems, because they are of distinct difficulties, and then only after the last problem."} +{"id": "02038", "text": "You are given a permutation $p$ of integers from $1$ to $n$, where $n$ is an even number. \n\nYour goal is to sort the permutation. To do so, you can perform zero or more operations of the following type: take two indices $i$ and $j$ such that $2 \\cdot |i - j| \\geq n$ and swap $p_i$ and $p_j$. \n\nThere is no need to minimize the number of operations, however you should use no more than $5 \\cdot n$ operations. One can show that it is always possible to do that.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\leq n \\leq 3 \\cdot 10^5$, $n$ is even)\u00a0\u2014 the length of the permutation. \n\nThe second line contains $n$ distinct integers $p_1, p_2, \\ldots, p_n$ ($1 \\le p_i \\le n$)\u00a0\u2014 the given permutation.\n\n\n-----Output-----\n\nOn the first line print $m$ ($0 \\le m \\le 5 \\cdot n$)\u00a0\u2014 the number of swaps to perform.\n\nEach of the following $m$ lines should contain integers $a_i, b_i$ ($1 \\le a_i, b_i \\le n$, $|a_i - b_i| \\ge \\frac{n}{2}$)\u00a0\u2014 the indices that should be swapped in the corresponding swap.\n\nNote that there is no need to minimize the number of operations. We can show that an answer always exists.\n\n\n-----Examples-----\nInput\n2\n2 1\n\nOutput\n1\n1 2\nInput\n4\n3 4 1 2\n\nOutput\n4\n1 4\n1 4\n1 3\n2 4\n\nInput\n6\n2 5 3 1 4 6\n\nOutput\n3\n1 5\n2 5\n1 4\n\n\n\n-----Note-----\n\nIn the first example, when one swap elements on positions $1$ and $2$, the array becomes sorted.\n\nIn the second example, pay attention that there is no need to minimize number of swaps.\n\nIn the third example, after swapping elements on positions $1$ and $5$ the array becomes: $[4, 5, 3, 1, 2, 6]$. After swapping elements on positions $2$ and $5$ the array becomes $[4, 2, 3, 1, 5, 6]$ and finally after swapping elements on positions $1$ and $4$ the array becomes sorted: $[1, 2, 3, 4, 5, 6]$."} +{"id": "02039", "text": "You are given an array a. Some element of this array a_{i} is a local minimum iff it is strictly less than both of its neighbours (that is, a_{i} < a_{i} - 1 and a_{i} < a_{i} + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, a_{i} > a_{i} - 1 and a_{i} > a_{i} + 1). Since a_1 and a_{n} have only one neighbour each, they are neither local minima nor local maxima.\n\nAn element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 1000) \u2014 the number of elements in array a.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1000) \u2014 the elements of array a.\n\n\n-----Output-----\n\nPrint the number of local extrema in the given array.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n0\n\nInput\n4\n1 5 2 5\n\nOutput\n2"} +{"id": "02040", "text": "Vasya had a strictly increasing sequence of positive integers a_1, ..., a_{n}. Vasya used it to build a new sequence b_1, ..., b_{n}, where b_{i} is the sum of digits of a_{i}'s decimal representation. Then sequence a_{i} got lost and all that remained is sequence b_{i}.\n\nVasya wonders what the numbers a_{i} could be like. Of all the possible options he likes the one sequence with the minimum possible last number a_{n}. Help Vasya restore the initial sequence.\n\nIt is guaranteed that such a sequence always exists.\n\n\n-----Input-----\n\nThe first line contains a single integer number n (1 \u2264 n \u2264 300).\n\nNext n lines contain integer numbers b_1, ..., b_{n} \u00a0\u2014 the required sums of digits. All b_{i} belong to the range 1 \u2264 b_{i} \u2264 300.\n\n\n-----Output-----\n\nPrint n integer numbers, one per line\u00a0\u2014 the correct option for numbers a_{i}, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to b_{i}. \n\nIf there are multiple sequences with least possible number a_{n}, print any of them. Print the numbers without leading zeroes.\n\n\n-----Examples-----\nInput\n3\n1\n2\n3\n\nOutput\n1\n2\n3\n\nInput\n3\n3\n2\n1\n\nOutput\n3\n11\n100"} +{"id": "02041", "text": "This is the harder version of the problem. In this version, $1 \\le n, m \\le 2\\cdot10^5$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems.\n\nYou are given a sequence of integers $a=[a_1,a_2,\\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. \n\nSuppose that an additional non-negative integer $k$ ($1 \\le k \\le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. \n\nRecall that the sequence $b=[b_1, b_2, \\dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \\dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \\le t \\le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t 1) such that for every 1 \u2264 i < k there is a road from town A_{i} to town A_{i} + 1 and another road from town A_{k} to town A_1. In other words, the roads are confusing if some of them form a directed cycle of some towns.\n\nNow ZS the Coder wonders how many sets of roads (there are 2^{n} variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing.\n\nNote that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.\n\n\n-----Input-----\n\nThe first line of the input contains single integer n (2 \u2264 n \u2264 2\u00b710^5)\u00a0\u2014 the number of towns in Udayland.\n\nThe next line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n, a_{i} \u2260 i), a_{i} denotes a road going from town i to town a_{i}.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n3\n2 3 1\n\nOutput\n6\n\nInput\n4\n2 1 1 1\n\nOutput\n8\n\nInput\n5\n2 4 2 5 3\n\nOutput\n28\n\n\n\n-----Note-----\n\nConsider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are $1 \\rightarrow 2$, $2 \\rightarrow 3$, $3 \\rightarrow 1$ initially. Number the roads 1 to 3 in this order. \n\nThe sets of roads that ZS the Coder can flip (to make them not confusing) are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. Note that the empty set is invalid because if no roads are flipped, then towns 1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip.\n\nThe sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing.\n\n[Image]"} +{"id": "02074", "text": "Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places.\n\nMunhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is c_{ij}.\n\nJack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 100) \u2014 the number of streets and avenues in Munhattan.\n\nEach of the next n lines contains m integers c_{ij} (1 \u2264 c_{ij} \u2264 10^9) \u2014 the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.\n\n\n-----Output-----\n\nPrint the only integer a \u2014 the cost of the dinner for Jack and Emma.\n\n\n-----Examples-----\nInput\n3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1\n\nOutput\n2\n\nInput\n3 3\n1 2 3\n2 3 1\n3 1 2\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2.\n\nIn the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1."} +{"id": "02075", "text": "As Famil Door\u2019s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!\n\nThe sequence of round brackets is called valid if and only if: the total number of opening brackets is equal to the total number of closing brackets; for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets. \n\nGabi bought a string s of length m (m \u2264 n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.\n\nNow he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 10^9 + 7.\n\n\n-----Input-----\n\nFirst line contains n and m (1 \u2264 m \u2264 n \u2264 100 000, n - m \u2264 2000)\u00a0\u2014 the desired length of the string and the length of the string bought by Gabi, respectively.\n\nThe second line contains string s of length m consisting of characters '(' and ')' only.\n\n\n-----Output-----\n\nPrint the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n4 1\n(\n\nOutput\n4\n\nInput\n4 4\n(())\n\nOutput\n1\n\nInput\n4 3\n(((\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample there are four different valid pairs: p = \"(\", q = \"))\" p = \"()\", q = \")\" p = \"\", q = \"())\" p = \"\", q = \")()\" \n\nIn the second sample the only way to obtain a desired string is choose empty p and q.\n\nIn the third sample there is no way to get a valid sequence of brackets."} +{"id": "02076", "text": "Alice is playing with some stones.\n\nNow there are three numbered heaps of stones. The first of them contains $a$ stones, the second of them contains $b$ stones and the third of them contains $c$ stones.\n\nEach time she can do one of two operations: take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones); take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones). \n\nShe wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has $0$ stones. Can you help her?\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\leq t \\leq 100$) \u00a0\u2014 the number of test cases. Next $t$ lines describe test cases in the following format:\n\nLine contains three non-negative integers $a$, $b$ and $c$, separated by spaces ($0 \\leq a,b,c \\leq 100$)\u00a0\u2014 the number of stones in the first, the second and the third heap, respectively.\n\nIn hacks it is allowed to use only one test case in the input, so $t = 1$ should be satisfied.\n\n\n-----Output-----\n\nPrint $t$ lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer \u00a0\u2014 the maximum possible number of stones that Alice can take after making some operations. \n\n\n-----Example-----\nInput\n3\n3 4 5\n1 0 5\n5 3 2\n\nOutput\n9\n0\n6\n\n\n\n-----Note-----\n\nFor the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is $9$. It is impossible to make some operations to take more than $9$ stones, so the answer is $9$."} +{"id": "02077", "text": "Today Johnny wants to increase his contribution. His plan assumes writing $n$ blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.\n\nThere are $n$ different topics, numbered from $1$ to $n$ sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most $n - 1$ neighbors.\n\nFor example, if already written neighbors of the current blog have topics number $1$, $3$, $1$, $5$, and $2$, Johnny will choose the topic number $4$ for the current blog, because topics number $1$, $2$ and $3$ are already covered by neighbors and topic number $4$ isn't covered.\n\nAs a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ $(1 \\leq n \\leq 5 \\cdot 10^5)$ and $m$ $(0 \\leq m \\leq 5 \\cdot 10^5)$\u00a0\u2014 the number of blogs and references, respectively.\n\nEach of the following $m$ lines contains two integers $a$ and $b$ ($a \\neq b$; $1 \\leq a, b \\leq n$), which mean that there is a reference between blogs $a$ and $b$. It's guaranteed that the graph doesn't contain multiple edges.\n\nThe last line contains $n$ integers $t_1, t_2, \\ldots, t_n$, $i$-th of them denotes desired topic number of the $i$-th blog ($1 \\le t_i \\le n$).\n\n\n-----Output-----\n\nIf the solution does not exist, then write $-1$. Otherwise, output $n$ distinct integers $p_1, p_2, \\ldots, p_n$ $(1 \\leq p_i \\leq n)$, which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n3 3\n1 2\n2 3\n3 1\n2 1 3\n\nOutput\n2 1 3\n\nInput\n3 3\n1 2\n2 3\n3 1\n1 1 1\n\nOutput\n-1\n\nInput\n5 3\n1 2\n2 3\n4 5\n2 1 2 2 1\n\nOutput\n2 5 1 3 4\n\n\n\n-----Note-----\n\nIn the first example, Johnny starts with writing blog number $2$, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number $1$, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number $3$, it has references to blogs number $1$ and $2$ so it receives the third topic.\n\nSecond example: There does not exist any permutation fulfilling given conditions.\n\nThird example: First Johnny writes blog $2$, it receives the topic $1$. Then he writes blog $5$, it receives the topic $1$ too because it doesn't have reference to single already written blog $2$. Then he writes blog number $1$, it has reference to blog number $2$ with topic $1$, so it receives the topic $2$. Then he writes blog number $3$ which has reference to blog $2$, so it receives the topic $2$. Then he ends with writing blog number $4$ which has reference to blog $5$ and receives the topic $2$."} +{"id": "02078", "text": "There is a square of size $10^6 \\times 10^6$ on the coordinate plane with four points $(0, 0)$, $(0, 10^6)$, $(10^6, 0)$, and $(10^6, 10^6)$ as its vertices.\n\nYou are going to draw segments on the plane. All segments are either horizontal or vertical and intersect with at least one side of the square.\n\nNow you are wondering how many pieces this square divides into after drawing all segments. Write a program calculating the number of pieces of the square.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($0 \\le n, m \\le 10^5$)\u00a0\u2014 the number of horizontal segments and the number of vertical segments.\n\nThe next $n$ lines contain descriptions of the horizontal segments. The $i$-th line contains three integers $y_i$, $lx_i$ and $rx_i$ ($0 < y_i < 10^6$; $0 \\le lx_i < rx_i \\le 10^6$), which means the segment connects $(lx_i, y_i)$ and $(rx_i, y_i)$.\n\nThe next $m$ lines contain descriptions of the vertical segments. The $i$-th line contains three integers $x_i$, $ly_i$ and $ry_i$ ($0 < x_i < 10^6$; $0 \\le ly_i < ry_i \\le 10^6$), which means the segment connects $(x_i, ly_i)$ and $(x_i, ry_i)$.\n\nIt's guaranteed that there are no two segments on the same line, and each segment intersects with at least one of square's sides.\n\n\n-----Output-----\n\nPrint the number of pieces the square is divided into after drawing all the segments.\n\n\n-----Example-----\nInput\n3 3\n2 3 1000000\n4 0 4\n3 0 1000000\n4 0 1\n2 0 5\n3 1 1000000\n\nOutput\n7\n\n\n-----Note-----\n\n The sample is like this: [Image]"} +{"id": "02079", "text": "In the Bus of Characters there are $n$ rows of seat, each having $2$ seats. The width of both seats in the $i$-th row is $w_i$ centimeters. All integers $w_i$ are distinct.\n\nInitially the bus is empty. On each of $2n$ stops one passenger enters the bus. There are two types of passengers: an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. \n\nYou are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 200\\,000$) \u2014 the number of rows in the bus.\n\nThe second line contains the sequence of integers $w_1, w_2, \\dots, w_n$ ($1 \\le w_i \\le 10^{9}$), where $w_i$ is the width of each of the seats in the $i$-th row. It is guaranteed that all $w_i$ are distinct.\n\nThe third line contains a string of length $2n$, consisting of digits '0' and '1' \u2014 the description of the order the passengers enter the bus. If the $j$-th character is '0', then the passenger that enters the bus on the $j$-th stop is an introvert. If the $j$-th character is '1', the the passenger that enters the bus on the $j$-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i.\u00a0e. both numbers equal $n$), and for each extrovert there always is a suitable row.\n\n\n-----Output-----\n\nPrint $2n$ integers \u2014 the rows the passengers will take. The order of passengers should be the same as in input.\n\n\n-----Examples-----\nInput\n2\n3 1\n0011\n\nOutput\n2 1 1 2 \n\nInput\n6\n10 8 9 11 13 5\n010010011101\n\nOutput\n6 6 2 3 3 1 4 4 1 2 5 5 \n\n\n\n-----Note-----\n\nIn the first example the first passenger (introvert) chooses the row $2$, because it has the seats with smallest width. The second passenger (introvert) chooses the row $1$, because it is the only empty row now. The third passenger (extrovert) chooses the row $1$, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row $2$, because it is the only row with an empty place."} +{"id": "02080", "text": "Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level.\n\nIvan decided that there should be exactly n_{i} vertices in the graph representing level i, and the edges have to be bidirectional. When constructing the graph, Ivan is interested in special edges called bridges. An edge between two vertices u and v is called a bridge if this edge belongs to every path between u and v (and these vertices will belong to different connected components if we delete this edge). For each level Ivan wants to construct a graph where at least half of the edges are bridges. He also wants to maximize the number of edges in each constructed graph.\n\nSo the task Ivan gave you is: given q numbers n_1, n_2, ..., n_{q}, for each i tell the maximum number of edges in a graph with n_{i} vertices, if at least half of the edges are bridges. Note that the graphs cannot contain multiple edges or self-loops.\n\n\n-----Input-----\n\nThe first line of input file contains a positive integer q (1 \u2264 q \u2264 100 000) \u2014 the number of graphs Ivan needs to construct.\n\nThen q lines follow, i-th line contains one positive integer n_{i} (1 \u2264 n_{i} \u2264 2\u00b710^9) \u2014 the number of vertices in i-th graph.\n\nNote that in hacks you have to use q = 1.\n\n\n-----Output-----\n\nOutput q numbers, i-th of them must be equal to the maximum number of edges in i-th graph.\n\n\n-----Example-----\nInput\n3\n3\n4\n6\n\nOutput\n2\n3\n6\n\n\n\n-----Note-----\n\nIn the first example it is possible to construct these graphs: 1 - 2, 1 - 3; 1 - 2, 1 - 3, 2 - 4; 1 - 2, 1 - 3, 2 - 3, 1 - 4, 2 - 5, 3 - 6."} +{"id": "02081", "text": "You are given an array a consisting of n elements. The imbalance value of some subsegment of this array is the difference between the maximum and minimum element from this segment. The imbalance value of the array is the sum of imbalance values of all subsegments of this array.\n\nFor example, the imbalance value of array [1, 4, 1] is 9, because there are 6 different subsegments of this array: [1] (from index 1 to index 1), imbalance value is 0; [1, 4] (from index 1 to index 2), imbalance value is 3; [1, 4, 1] (from index 1 to index 3), imbalance value is 3; [4] (from index 2 to index 2), imbalance value is 0; [4, 1] (from index 2 to index 3), imbalance value is 3; [1] (from index 3 to index 3), imbalance value is 0; \n\nYou have to determine the imbalance value of the array a.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 10^6) \u2014 size of the array a.\n\nThe second line contains n integers a_1, a_2... a_{n} (1 \u2264 a_{i} \u2264 10^6) \u2014 elements of the array.\n\n\n-----Output-----\n\nPrint one integer \u2014 the imbalance value of a.\n\n\n-----Example-----\nInput\n3\n1 4 1\n\nOutput\n9"} +{"id": "02082", "text": "Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains a_{i} animals in it. Also there are m roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo from any other area using the roads.\n\nOur child is very smart. Imagine the child want to go from area p to area q. Firstly he considers all the simple routes from p to q. For each route the child writes down the number, that is equal to the minimum number of animals among the route areas. Let's denote the largest of the written numbers as f(p, q). Finally, the child chooses one of the routes for which he writes down the value f(p, q).\n\nAfter the child has visited the zoo, he thinks about the question: what is the average value of f(p, q) for all pairs p, q (p \u2260 q)? Can you answer his question?\n\n\n-----Input-----\n\nThe first line contains two integers n and m (2 \u2264 n \u2264 10^5; 0 \u2264 m \u2264 10^5). The second line contains n integers: a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^5). Then follow m lines, each line contains two integers x_{i} and y_{i} (1 \u2264 x_{i}, y_{i} \u2264 n; x_{i} \u2260 y_{i}), denoting the road between areas x_{i} and y_{i}.\n\nAll roads are bidirectional, each pair of areas is connected by at most one road.\n\n\n-----Output-----\n\nOutput a real number \u2014 the value of $\\frac{\\sum_{p, q, p \\neq q} f(p, q)}{n(n - 1)}$.\n\nThe answer will be considered correct if its relative or absolute error doesn't exceed 10^{ - 4}.\n\n\n-----Examples-----\nInput\n4 3\n10 20 30 40\n1 3\n2 3\n4 3\n\nOutput\n16.666667\n\nInput\n3 3\n10 20 30\n1 2\n2 3\n3 1\n\nOutput\n13.333333\n\nInput\n7 8\n40 20 10 30 20 50 40\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n1 4\n5 7\n\nOutput\n18.571429\n\n\n\n-----Note-----\n\nConsider the first sample. There are 12 possible situations:\n\n p = 1, q = 3, f(p, q) = 10. p = 2, q = 3, f(p, q) = 20. p = 4, q = 3, f(p, q) = 30. p = 1, q = 2, f(p, q) = 10. p = 2, q = 4, f(p, q) = 20. p = 4, q = 1, f(p, q) = 10. \n\nAnother 6 cases are symmetrical to the above. The average is $\\frac{(10 + 20 + 30 + 10 + 20 + 10) \\times 2}{12} \\approx 16.666667$.\n\nConsider the second sample. There are 6 possible situations:\n\n p = 1, q = 2, f(p, q) = 10. p = 2, q = 3, f(p, q) = 20. p = 1, q = 3, f(p, q) = 10. \n\nAnother 3 cases are symmetrical to the above. The average is $\\frac{(10 + 20 + 10) \\times 2}{6} \\approx 13.333333$."} +{"id": "02083", "text": "In this problem you will have to deal with a real algorithm that is used in the VK social network.\n\nAs in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60\u00a0seconds = 1\u00a0min and T = 86400\u00a0seconds = 1\u00a0day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.\n\nHowever, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.\n\nLet's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource a_{t} at each moment of time t (1 \u2264 t \u2264 n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.\n\n// setting this constant value correctly can adjust \n\n// the time range for which statistics will be calculated\n\ndouble c = some constant value; \n\n\n\n// as the result of the algorithm's performance this variable will contain \n\n// the mean number of queries for the last \n\n// T seconds by the current moment of time\n\ndouble mean = 0.0; \n\n\n\nfor t = 1..n: // at each second, we do the following:\n\n // a_{t} is the number of queries that came at the last second;\n\n mean = (mean + a_{t} / T) / c;\n\n\n\nThus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value a_{x} at t - T + 1 \u2264 x \u2264 t. \n\nThe advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.\n\nHowever before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data. \n\nYou are given n values a_{t}, integer T and real number c. Also, you are given m moments p_{j} (1 \u2264 j \u2264 m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula $\\frac{a_{p_{j} - T + 1} + a_{p_{j}} - T + 2 + \\ldots + a_{p_{j}}}{T}$. The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula $\\frac{|\\text{approx-real}|}{\\text{real}}$, where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 2\u00b710^5), integer T (1 \u2264 T \u2264 n) and real number c (1 < c \u2264 100) \u2014 the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point.\n\nThe next line contains n integers a_{t} (1 \u2264 a_{t} \u2264 10^6) \u2014 the number of queries to the service at each moment of time.\n\nThe next line contains integer m (1 \u2264 m \u2264 n) \u2014 the number of moments of time when we are interested in the mean number of queries for the last T seconds.\n\nThe next line contains m integers p_{j} (T \u2264 p_{j} \u2264 n), representing another moment of time for which we need statistics. Moments p_{j} are strictly increasing.\n\n\n-----Output-----\n\nPrint m lines. The j-th line must contain three numbers real, approx and error, where: [Image] is the real mean number of queries for the last T seconds; approx is calculated by the given algorithm and equals mean at the moment of time t = p_{j} (that is, after implementing the p_{j}-th iteration of the cycle); $\\text{error} = \\frac{|\\text{approx-real}|}{\\text{real}}$ is the relative error of the approximate algorithm. \n\nThe numbers you printed will be compared to the correct numbers with the relative or absolute error 10^{ - 4}. It is recommended to print the numbers with at least five digits after the decimal point.\n\n\n-----Examples-----\nInput\n1 1 2.000000\n1\n1\n1\n\nOutput\n1.000000 0.500000 0.500000\n\nInput\n11 4 1.250000\n9 11 7 5 15 6 6 6 6 6 6\n8\n4 5 6 7 8 9 10 11\n\nOutput\n8.000000 4.449600 0.443800\n9.500000 6.559680 0.309507\n8.250000 6.447744 0.218455\n8.000000 6.358195 0.205226\n8.250000 6.286556 0.237993\n6.000000 6.229245 0.038207\n6.000000 6.183396 0.030566\n6.000000 6.146717 0.024453\n\nInput\n13 4 1.250000\n3 3 3 3 3 20 3 3 3 3 3 3 3\n10\n4 5 6 7 8 9 10 11 12 13\n\nOutput\n3.000000 1.771200 0.409600\n3.000000 2.016960 0.327680\n7.250000 5.613568 0.225715\n7.250000 5.090854 0.297813\n7.250000 4.672684 0.355492\n7.250000 4.338147 0.401635\n3.000000 4.070517 0.356839\n3.000000 3.856414 0.285471\n3.000000 3.685131 0.228377\n3.000000 3.548105 0.182702"} +{"id": "02084", "text": "The marmots have prepared a very easy problem for this year's HC^2 \u2013 this one. It involves numbers n, k and a sequence of n positive integers a_1, a_2, ..., a_{n}. They also came up with a beautiful and riveting story for the problem statement. It explains what the input means, what the program should output, and it also reads like a good criminal.\n\nHowever I, Heidi, will have none of that. As my joke for today, I am removing the story from the statement and replacing it with these two unhelpful paragraphs. Now solve the problem, fools!\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and k (1 \u2264 k \u2264 n \u2264 2200). The second line contains n space-separated integers a_1, ..., a_{n} (1 \u2264 a_{i} \u2264 10^4).\n\n\n-----Output-----\n\nOutput one number.\n\n\n-----Examples-----\nInput\n8 5\n1 1 1 1 1 1 1 1\n\nOutput\n5\nInput\n10 3\n16 8 2 4 512 256 32 128 64 1\n\nOutput\n7\nInput\n5 1\n20 10 50 30 46\n\nOutput\n10\nInput\n6 6\n6 6 6 6 6 6\n\nOutput\n36\nInput\n1 1\n100\n\nOutput\n100"} +{"id": "02085", "text": "Ridhiman challenged Ashish to find the maximum valued subsequence of an array $a$ of size $n$ consisting of positive integers. \n\nThe value of a non-empty subsequence of $k$ elements of $a$ is defined as $\\sum 2^i$ over all integers $i \\ge 0$ such that at least $\\max(1, k - 2)$ elements of the subsequence have the $i$-th bit set in their binary representation (value $x$ has the $i$-th bit set in its binary representation if $\\lfloor \\frac{x}{2^i} \\rfloor \\mod 2$ is equal to $1$). \n\nRecall that $b$ is a subsequence of $a$, if $b$ can be obtained by deleting some(possibly zero) elements from $a$.\n\nHelp Ashish find the maximum value he can get by choosing some subsequence of $a$.\n\n\n-----Input-----\n\nThe first line of the input consists of a single integer $n$ $(1 \\le n \\le 500)$\u00a0\u2014 the size of $a$.\n\nThe next line consists of $n$ space-separated integers\u00a0\u2014 the elements of the array $(1 \\le a_i \\le 10^{18})$.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum value Ashish can get by choosing some subsequence of $a$.\n\n\n-----Examples-----\nInput\n3\n2 1 3\n\nOutput\n3\nInput\n3\n3 1 4\n\nOutput\n7\nInput\n1\n1\n\nOutput\n1\nInput\n4\n7 7 1 1\n\nOutput\n7\n\n\n-----Note-----\n\nFor the first test case, Ashish can pick the subsequence $\\{{2, 3}\\}$ of size $2$. The binary representation of $2$ is 10 and that of $3$ is 11. Since $\\max(k - 2, 1)$ is equal to $1$, the value of the subsequence is $2^0 + 2^1$ (both $2$ and $3$ have $1$-st bit set in their binary representation and $3$ has $0$-th bit set in its binary representation). Note that he could also pick the subsequence $\\{{3\\}}$ or $\\{{2, 1, 3\\}}$.\n\nFor the second test case, Ashish can pick the subsequence $\\{{3, 4\\}}$ with value $7$.\n\nFor the third test case, Ashish can pick the subsequence $\\{{1\\}}$ with value $1$.\n\nFor the fourth test case, Ashish can pick the subsequence $\\{{7, 7\\}}$ with value $7$."} +{"id": "02086", "text": "In distant future on Earth day lasts for n hours and that's why there are n timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to n are used, i.e. there is no time \"0 hours\", instead of it \"n hours\" is used. When local time in the 1-st timezone is 1 hour, local time in the i-th timezone is i hours.\n\nSome online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are a_{i} people from i-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than s hours 00 minutes local time and ends not later than f hours 00 minutes local time. Values s and f are equal for all time zones. If the contest starts at f hours 00 minutes local time, the person won't participate in it.\n\nHelp platform select such an hour, that the number of people who will participate in the contest is maximum. \n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 100 000)\u00a0\u2014 the number of hours in day.\n\nThe second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10 000), where a_{i} is the number of people in the i-th timezone who want to participate in the contest.\n\nThe third line contains two space-separated integers s and f (1 \u2264 s < f \u2264 n).\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n1 3\n\nOutput\n3\n\nInput\n5\n1 2 3 4 1\n1 3\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate.\n\nIn second example only people from the third and the fourth timezones will participate."} +{"id": "02087", "text": "Given are three positive integers A, B, and C. Compute the following value modulo 998244353:\n\\sum_{a=1}^{A} \\sum_{b=1}^{B} \\sum_{c=1}^{C} abc\n\n-----Constraints-----\n - 1 \\leq A, B, C \\leq 10^9\n\n-----Input-----\nInput is given from standard input in the following format:\nA B C\n\n-----Output-----\nPrint the value modulo 998244353.\n\n-----Sample Input-----\n1 2 3\n\n-----Sample Output-----\n18\n\nWe have: (1 \\times 1 \\times 1) + (1 \\times 1 \\times 2) + (1 \\times 1 \\times 3) + (1 \\times 2 \\times 1) + (1 \\times 2 \\times 2) + (1 \\times 2 \\times 3) = 1 + 2 + 3 + 2 + 4 + 6 = 18."} +{"id": "02088", "text": "There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.\n\nA subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.\n\nA leaf is such a junction that its subtree contains exactly one junction.\n\nThe New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.\n\nArkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the number of junctions in the tree.\n\nThe second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \\le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.\n\n\n-----Output-----\n\nOutput $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.\n\n\n-----Examples-----\nInput\n3\n1 1\n\nOutput\n1 1 2 \n\nInput\n5\n1 1 3 3\n\nOutput\n1 1 1 2 3 \n\n\n\n-----Note-----\n\nIn the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.\n\nIn the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then."} +{"id": "02089", "text": "Little town Nsk consists of n junctions connected by m bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junction by these roads. The distance between two junctions is equal to the minimum possible number of roads on a path between them.\n\nIn order to improve the transportation system, the city council asks mayor to build one new road. The problem is that the mayor has just bought a wonderful new car and he really enjoys a ride from his home, located near junction s to work located near junction t. Thus, he wants to build a new road in such a way that the distance between these two junctions won't decrease. \n\nYou are assigned a task to compute the number of pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built the distance between s and t won't decrease.\n\n\n-----Input-----\n\nThe firt line of the input contains integers n, m, s and t (2 \u2264 n \u2264 1000, 1 \u2264 m \u2264 1000, 1 \u2264 s, t \u2264 n, s \u2260 t)\u00a0\u2014 the number of junctions and the number of roads in Nsk, as well as the indices of junctions where mayors home and work are located respectively. The i-th of the following m lines contains two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i}), meaning that this road connects junctions u_{i} and v_{i} directly. It is guaranteed that there is a path between any two junctions and no two roads connect the same pair of junctions.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the number of pairs of junctions not connected by a direct road, such that building a road between these two junctions won't decrease the distance between junctions s and t.\n\n\n-----Examples-----\nInput\n5 4 1 5\n1 2\n2 3\n3 4\n4 5\n\nOutput\n0\n\nInput\n5 4 3 5\n1 2\n2 3\n3 4\n4 5\n\nOutput\n5\n\nInput\n5 6 1 5\n1 2\n1 3\n1 4\n4 5\n3 5\n2 5\n\nOutput\n3"} +{"id": "02090", "text": "You have a playlist consisting of $n$ songs. The $i$-th song is characterized by two numbers $t_i$ and $b_i$ \u2014 its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of $3$ songs having lengths $[5, 7, 4]$ and beauty values $[11, 14, 6]$ is equal to $(5 + 7 + 4) \\cdot 6 = 96$.\n\nYou need to choose at most $k$ songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 3 \\cdot 10^5$) \u2013 the number of songs in the playlist and the maximum number of songs you can choose, respectively.\n\nEach of the next $n$ lines contains two integers $t_i$ and $b_i$ ($1 \\le t_i, b_i \\le 10^6$) \u2014 the length and beauty of $i$-th song.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum pleasure you can get.\n\n\n-----Examples-----\nInput\n4 3\n4 7\n15 1\n3 6\n6 8\n\nOutput\n78\n\nInput\n5 3\n12 31\n112 4\n100 100\n13 55\n55 50\n\nOutput\n10000\n\n\n\n-----Note-----\n\nIn the first test case we can choose songs ${1, 3, 4}$, so the total pleasure is $(4 + 3 + 6) \\cdot 6 = 78$.\n\nIn the second test case we can choose song $3$. The total pleasure will be equal to $100 \\cdot 100 = 10000$."} +{"id": "02091", "text": "This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.\n\nYou are given a string $s$, consisting of lowercase English letters. Find the longest string, $t$, which satisfies the following conditions: The length of $t$ does not exceed the length of $s$. $t$ is a palindrome. There exists two strings $a$ and $b$ (possibly empty), such that $t = a + b$ ( \"$+$\" represents concatenation), and $a$ is prefix of $s$ while $b$ is suffix of $s$. \n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains a single integer $t$ ($1 \\leq t \\leq 10^5$), the number of test cases. The next $t$ lines each describe a test case.\n\nEach test case is a non-empty string $s$, consisting of lowercase English letters.\n\nIt is guaranteed that the sum of lengths of strings over all test cases does not exceed $10^6$.\n\n\n-----Output-----\n\nFor each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.\n\n\n-----Example-----\nInput\n5\na\nabcdfdcecba\nabbaxyzyx\ncodeforces\nacbba\n\nOutput\na\nabcdfdcba\nxyzyx\nc\nabba\n\n\n\n-----Note-----\n\nIn the first test, the string $s = $\"a\" satisfies all conditions.\n\nIn the second test, the string \"abcdfdcba\" satisfies all conditions, because: Its length is $9$, which does not exceed the length of the string $s$, which equals $11$. It is a palindrome. \"abcdfdcba\" $=$ \"abcdfdc\" $+$ \"ba\", and \"abcdfdc\" is a prefix of $s$ while \"ba\" is a suffix of $s$. \n\nIt can be proven that there does not exist a longer string which satisfies the conditions.\n\nIn the fourth test, the string \"c\" is correct, because \"c\" $=$ \"c\" $+$ \"\" and $a$ or $b$ can be empty. The other possible solution for this test is \"s\"."} +{"id": "02092", "text": "You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$.\n\nThe level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located).\n\nThe level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.\n\nYou have $t$ seconds to complete the level \u2014 that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:\n\n if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time). \n\nNote that after each action both your coordinate and the coordinate of your squad should be integers.\n\nYou have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds.\n\n\n-----Input-----\n\nThe first line contains four integers $m$, $n$, $k$ and $t$ ($1 \\le m, n, k, t \\le 2 \\cdot 10^5$, $n < t$) \u2014 the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.\n\nThe second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier.\n\nThen $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \\le l_i \\le r_i \\le n$, $1 \\le d_i \\le 2 \\cdot 10^5$) \u2014 the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds.\n\n\n-----Example-----\nInput\n5 6 4 14\n1 2 3 4 5\n1 5 2\n1 2 5\n2 3 5\n3 5 3\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows:\n\n go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad. \n\nThe whole plan can be executed in $13$ seconds."} +{"id": "02093", "text": "One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series \"Tufurama\". He was pretty surprised when he got results only for season 7 episode 3 with his search query of \"Watch Tufurama season 3 episode 7 online full hd free\". This got Polycarp confused \u2014 what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.\n\nTV series have n seasons (numbered 1 through n), the i-th season has a_{i} episodes (numbered 1 through a_{i}). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 2\u00b710^5) \u2014 the number of seasons.\n\nThe second line contains n integers separated by space a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 number of episodes in each season.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n\nOutput\n0\n\nInput\n3\n8 12 7\n\nOutput\n3\n\nInput\n3\n3 2 1\n\nOutput\n2\n\n\n\n-----Note-----\n\nPossible pairs in the second example: x = 1, y = 2 (season 1 episode 2 [Image] season 2 episode 1); x = 2, y = 3 (season 2 episode 3 [Image] season 3 episode 2); x = 1, y = 3 (season 1 episode 3 [Image] season 3 episode 1). \n\nIn the third example: x = 1, y = 2 (season 1 episode 2 [Image] season 2 episode 1); x = 1, y = 3 (season 1 episode 3 [Image] season 3 episode 1)."} +{"id": "02094", "text": "Your program fails again. This time it gets \"Wrong answer on test 233\".\n\nThis is the harder version of the problem. In this version, $1 \\le n \\le 2\\cdot10^5$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems.\n\nThe problem is to finish $n$ one-choice-questions. Each of the questions contains $k$ options, and only one of them is correct. The answer to the $i$-th question is $h_{i}$, and if your answer of the question $i$ is $h_{i}$, you earn $1$ point, otherwise, you earn $0$ points for this question. The values $h_1, h_2, \\dots, h_n$ are known to you in this problem.\n\nHowever, you have a mistake in your program. It moves the answer clockwise! Consider all the $n$ answers are written in a circle. Due to the mistake in your program, they are shifted by one cyclically.\n\nFormally, the mistake moves the answer for the question $i$ to the question $i \\bmod n + 1$. So it moves the answer for the question $1$ to question $2$, the answer for the question $2$ to the question $3$, ..., the answer for the question $n$ to the question $1$.\n\nWe call all the $n$ answers together an answer suit. There are $k^n$ possible answer suits in total.\n\nYou're wondering, how many answer suits satisfy the following condition: after moving clockwise by $1$, the total number of points of the new answer suit is strictly larger than the number of points of the old one. You need to find the answer modulo $998\\,244\\,353$.\n\nFor example, if $n = 5$, and your answer suit is $a=[1,2,3,4,5]$, it will submitted as $a'=[5,1,2,3,4]$ because of a mistake. If the correct answer suit is $h=[5,2,2,3,4]$, the answer suit $a$ earns $1$ point and the answer suite $a'$ earns $4$ points. Since $4 > 1$, the answer suit $a=[1,2,3,4,5]$ should be counted.\n\n\n-----Input-----\n\nThe first line contains two integers $n$, $k$ ($1 \\le n \\le 2\\cdot10^5$, $1 \\le k \\le 10^9$)\u00a0\u2014 the number of questions and the number of possible answers to each question.\n\nThe following line contains $n$ integers $h_1, h_2, \\dots, h_n$, ($1 \\le h_{i} \\le k)$\u00a0\u2014 answers to the questions.\n\n\n-----Output-----\n\nOutput one integer: the number of answers suits satisfying the given condition, modulo $998\\,244\\,353$.\n\n\n-----Examples-----\nInput\n3 3\n1 3 1\n\nOutput\n9\n\nInput\n5 5\n1 1 4 2 2\n\nOutput\n1000\n\nInput\n6 2\n1 1 2 2 1 1\n\nOutput\n16\n\n\n\n-----Note-----\n\nFor the first example, valid answer suits are $[2,1,1], [2,1,2], [2,1,3], [3,1,1], [3,1,2], [3,1,3], [3,2,1], [3,2,2], [3,2,3]$."} +{"id": "02095", "text": "Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph.\n\nThere are n toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an n \u00d7 n matrix \u0410: there is a number on the intersection of the \u0456-th row and j-th column that describes the result of the collision of the \u0456-th and the j-th car: - 1: if this pair of cars never collided. - 1 occurs only on the main diagonal of the matrix. 0: if no car turned over during the collision. 1: if only the i-th car turned over during the collision. 2: if only the j-th car turned over during the collision. 3: if both cars turned over during the collision. \n\nSusie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task?\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100) \u2014 the number of cars.\n\nEach of the next n lines contains n space-separated integers that determine matrix A. \n\nIt is guaranteed that on the main diagonal there are - 1, and - 1 doesn't appear anywhere else in the matrix.\n\nIt is guaranteed that the input is correct, that is, if A_{ij} = 1, then A_{ji} = 2, if A_{ij} = 3, then A_{ji} = 3, and if A_{ij} = 0, then A_{ji} = 0.\n\n\n-----Output-----\n\nPrint the number of good cars and in the next line print their space-separated indices in the increasing order.\n\n\n-----Examples-----\nInput\n3\n-1 0 0\n0 -1 1\n0 2 -1\n\nOutput\n2\n1 3 \nInput\n4\n-1 3 3 3\n3 -1 3 3\n3 3 -1 3\n3 3 3 -1\n\nOutput\n0"} +{"id": "02096", "text": "Valera has 2\u00b7n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap. \n\nValera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer \u2014 the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.\n\nValera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100). The second line contains 2\u00b7n space-separated integers a_{i} (10 \u2264 a_{i} \u2264 99), denoting the numbers on the cubes.\n\n\n-----Output-----\n\nIn the first line print a single number \u2014 the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2\u00b7n numbers b_{i} (1 \u2264 b_{i} \u2264 2). The numbers mean: the i-th cube belongs to the b_{i}-th heap in your division.\n\nIf there are multiple optimal ways to split the cubes into the heaps, print any of them.\n\n\n-----Examples-----\nInput\n1\n10 99\n\nOutput\n1\n2 1 \n\nInput\n2\n13 24 13 45\n\nOutput\n4\n1 2 2 1 \n\n\n\n-----Note-----\n\nIn the first test case Valera can put the first cube in the first heap, and second cube \u2014 in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.\n\nIn the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345."} +{"id": "02097", "text": "Guy-Manuel and Thomas have an array $a$ of $n$ integers [$a_1, a_2, \\dots, a_n$]. In one step they can add $1$ to any element of the array. Formally, in one step they can choose any integer index $i$ ($1 \\le i \\le n$) and do $a_i := a_i + 1$.\n\nIf either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.\n\nWhat is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $a_1 + a_2 +$ $\\dots$ $+ a_n \\ne 0$ and $a_1 \\cdot a_2 \\cdot$ $\\dots$ $\\cdot a_n \\ne 0$.\n\n\n-----Input-----\n\nEach test contains multiple test cases. \n\nThe first line contains the number of test cases $t$ ($1 \\le t \\le 10^3$). The description of the test cases follows.\n\nThe first line of each test case contains an integer $n$ ($1 \\le n \\le 100$)\u00a0\u2014 the size of the array.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-100 \\le a_i \\le 100$)\u00a0\u2014 elements of the array .\n\n\n-----Output-----\n\nFor each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.\n\n\n-----Example-----\nInput\n4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1\n\nOutput\n1\n2\n0\n2\n\n\n\n-----Note-----\n\nIn the first test case, the sum is $0$. If we add $1$ to the first element, the array will be $[3,-1,-1]$, the sum will be equal to $1$ and the product will be equal to $3$.\n\nIn the second test case, both product and sum are $0$. If we add $1$ to the second and the third element, the array will be $[-1,1,1,1]$, the sum will be equal to $2$ and the product will be equal to $-1$. It can be shown that fewer steps can't be enough.\n\nIn the third test case, both sum and product are non-zero, we don't need to do anything.\n\nIn the fourth test case, after adding $1$ twice to the first element the array will be $[2,-2,1]$, the sum will be $1$ and the product will be $-4$."} +{"id": "02098", "text": "Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$. For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.\n\nMonocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 1\\,000$)\u00a0\u2014 the number of vertices in the tree.\n\nEach of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ($1 \\le a_i < b_i \\le n$)\u00a0\u2014 the maximal indices of vertices in the components formed if the $i$-th edge is removed.\n\n\n-----Output-----\n\nIf there is no such tree that can produce the given list of pairs, print \"NO\" (without quotes).\n\nOtherwise print \"YES\" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ($1 \\le x_i, y_i \\le n$)\u00a0\u2014 vertices connected by an edge.\n\nNote: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.\n\n\n-----Examples-----\nInput\n4\n3 4\n1 4\n3 4\n\nOutput\nYES\n1 3\n3 2\n2 4\n\nInput\n3\n1 3\n1 3\n\nOutput\nNO\n\nInput\n3\n1 2\n2 3\n\nOutput\nNO\n\n\n\n-----Note-----\n\nPossible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs. [Image]"} +{"id": "02099", "text": "Polycarpus got an internship in one well-known social network. His test task is to count the number of unique users who have visited a social network during the day. Polycarpus was provided with information on all user requests for this time period. For each query, we know its time... and nothing else, because Polycarpus has already accidentally removed the user IDs corresponding to the requests from the database. Thus, it is now impossible to determine whether any two requests are made by the same person or by different people.\n\nBut wait, something is still known, because that day a record was achieved \u2014 M simultaneous users online! In addition, Polycarpus believes that if a user made a request at second s, then he was online for T seconds after that, that is, at seconds s, s + 1, s + 2, ..., s + T - 1. So, the user's time online can be calculated as the union of time intervals of the form [s, s + T - 1] over all times s of requests from him.\n\nGuided by these thoughts, Polycarpus wants to assign a user ID to each request so that: the number of different users online did not exceed M at any moment, at some second the number of distinct users online reached value M, the total number of users (the number of distinct identifiers) was as much as possible. \n\nHelp Polycarpus cope with the test.\n\n\n-----Input-----\n\nThe first line contains three integers n, M and T (1 \u2264 n, M \u2264 20 000, 1 \u2264 T \u2264 86400) \u2014 the number of queries, the record number of online users and the time when the user was online after a query was sent. Next n lines contain the times of the queries in the format \"hh:mm:ss\", where hh are hours, mm are minutes, ss are seconds. The times of the queries follow in the non-decreasing order, some of them can coincide. It is guaranteed that all the times and even all the segments of type [s, s + T - 1] are within one 24-hour range (from 00:00:00 to 23:59:59). \n\n\n-----Output-----\n\nIn the first line print number R \u2014 the largest possible number of distinct users. The following n lines should contain the user IDs for requests in the same order in which the requests are given in the input. User IDs must be integers from 1 to R. The requests of the same user must correspond to the same identifiers, the requests of distinct users must correspond to distinct identifiers. If there are multiple solutions, print any of them. If there is no solution, print \"No solution\" (without the quotes).\n\n\n-----Examples-----\nInput\n4 2 10\n17:05:53\n17:05:58\n17:06:01\n22:39:47\n\nOutput\n3\n1\n2\n2\n3\n\nInput\n1 2 86400\n00:00:00\n\nOutput\nNo solution\n\n\n\n-----Note-----\n\nConsider the first sample. The user who sent the first request was online from 17:05:53 to 17:06:02, the user who sent the second request was online from 17:05:58 to 17:06:07, the user who sent the third request, was online from 17:06:01 to 17:06:10. Thus, these IDs cannot belong to three distinct users, because in that case all these users would be online, for example, at 17:06:01. That is impossible, because M = 2. That means that some two of these queries belonged to the same user. One of the correct variants is given in the answer to the sample. For it user 1 was online from 17:05:53 to 17:06:02, user 2 \u2014 from 17:05:58 to 17:06:10 (he sent the second and third queries), user 3 \u2014 from 22:39:47 to 22:39:56.\n\nIn the second sample there is only one query. So, only one user visited the network within the 24-hour period and there couldn't be two users online on the network simultaneously. (The time the user spent online is the union of time intervals for requests, so users who didn't send requests could not be online in the network.)"} +{"id": "02100", "text": "One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.\n\nKarlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.\n\nAnd now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.\n\nKarlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.\n\nYour task is to write a program that will determine the required number of seconds t.\n\n\n-----Input-----\n\nThe first input line contains a single integer n \u2014 the number of cupboards in the kitchen (2 \u2264 n \u2264 10^4). Then follow n lines, each containing two integers l_{i} and r_{i} (0 \u2264 l_{i}, r_{i} \u2264 1). Number l_{i} equals one, if the left door of the i-th cupboard is opened, otherwise number l_{i} equals zero. Similarly, number r_{i} equals one, if the right door of the i-th cupboard is opened, otherwise number r_{i} equals zero.\n\nThe numbers in the lines are separated by single spaces.\n\n\n-----Output-----\n\nIn the only output line print a single integer t \u2014 the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.\n\n\n-----Examples-----\nInput\n5\n0 1\n1 0\n0 1\n1 1\n0 1\n\nOutput\n3"} +{"id": "02101", "text": "You are given three multisets of pairs of colored sticks: $R$ pairs of red sticks, the first pair has length $r_1$, the second pair has length $r_2$, $\\dots$, the $R$-th pair has length $r_R$; $G$ pairs of green sticks, the first pair has length $g_1$, the second pair has length $g_2$, $\\dots$, the $G$-th pair has length $g_G$; $B$ pairs of blue sticks, the first pair has length $b_1$, the second pair has length $b_2$, $\\dots$, the $B$-th pair has length $b_B$; \n\nYou are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. \n\nThus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.\n\nEach pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.\n\nWhat is the maximum area you can achieve?\n\n\n-----Input-----\n\nThe first line contains three integers $R$, $G$, $B$ ($1 \\le R, G, B \\le 200$)\u00a0\u2014 the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.\n\nThe second line contains $R$ integers $r_1, r_2, \\dots, r_R$ ($1 \\le r_i \\le 2000$)\u00a0\u2014 the lengths of sticks in each pair of red sticks.\n\nThe third line contains $G$ integers $g_1, g_2, \\dots, g_G$ ($1 \\le g_i \\le 2000$)\u00a0\u2014 the lengths of sticks in each pair of green sticks.\n\nThe fourth line contains $B$ integers $b_1, b_2, \\dots, b_B$ ($1 \\le b_i \\le 2000$)\u00a0\u2014 the lengths of sticks in each pair of blue sticks.\n\n\n-----Output-----\n\nPrint the maximum possible total area of the constructed rectangles.\n\n\n-----Examples-----\nInput\n1 1 1\n3\n5\n4\n\nOutput\n20\n\nInput\n2 1 3\n9 5\n1\n2 8 5\n\nOutput\n99\n\nInput\n10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11\n\nOutput\n372\n\n\n\n-----Note-----\n\nIn the first example you can construct one of these rectangles: red and green with sides $3$ and $5$, red and blue with sides $3$ and $4$ and green and blue with sides $5$ and $4$. The best area of them is $4 \\times 5 = 20$.\n\nIn the second example the best rectangles are: red/blue $9 \\times 8$, red/blue $5 \\times 5$, green/blue $2 \\times 1$. So the total area is $72 + 25 + 2 = 99$.\n\nIn the third example the best rectangles are: red/green $19 \\times 8$ and red/blue $20 \\times 11$. The total area is $152 + 220 = 372$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color."} +{"id": "02102", "text": "After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$.\n\nTayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference.\n\nFor example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] \n\n\n-----Input-----\n\nThe first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \\leq a_{i} \\leq 10^{9}$) which describe the Tayuya's strings.\n\nThe second line contains the only integer $n$ ($1 \\leq n \\leq 100\\,000$) standing for the number of notes in the melody.\n\nThe third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \\leq b_{i} \\leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\\leq i\\leq n$ and $1\\leq j\\leq 6$, in other words, you can play each note on any string.\n\n\n-----Output-----\n\nPrint the minimal possible difference of the maximal and the minimal indices of used frets.\n\n\n-----Examples-----\nInput\n1 4 100 10 30 5\n6\n101 104 105 110 130 200\n\nOutput\n0\n\nInput\n1 1 2 2 3 3\n7\n13 4 11 12 11 13 12\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] \n\nIn the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]"} +{"id": "02103", "text": "Given an array $a$ of length $n$, find another array, $b$, of length $n$ such that:\n\n for each $i$ $(1 \\le i \\le n)$ $MEX(\\{b_1$, $b_2$, $\\ldots$, $b_i\\})=a_i$. \n\nThe $MEX$ of a set of integers is the smallest non-negative integer that doesn't belong to this set.\n\nIf such array doesn't exist, determine this.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the length of the array $a$.\n\nThe second line contains $n$ integers $a_1$, $a_2$, $\\ldots$, $a_n$ ($0 \\le a_i \\le i$)\u00a0\u2014 the elements of the array $a$. It's guaranteed that $a_i \\le a_{i+1}$ for $1\\le i < n$. \n\n\n-----Output-----\n\nIf there's no such array, print a single line containing $-1$.\n\nOtherwise, print a single line containing $n$ integers $b_1$, $b_2$, $\\ldots$, $b_n$ ($0 \\le b_i \\le 10^6$)\n\nIf there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n0 1 2 \nInput\n4\n0 0 0 2\n\nOutput\n1 3 4 0 \nInput\n3\n1 1 3\n\nOutput\n0 2 1 \n\n\n-----Note-----\n\nIn the second test case, other answers like $[1,1,1,0]$, for example, are valid."} +{"id": "02104", "text": "You are given a set of all integers from $l$ to $r$ inclusive, $l < r$, $(r - l + 1) \\le 3 \\cdot 10^5$ and $(r - l)$ is always odd.\n\nYou want to split these numbers into exactly $\\frac{r - l + 1}{2}$ pairs in such a way that for each pair $(i, j)$ the greatest common divisor of $i$ and $j$ is equal to $1$. Each number should appear in exactly one of the pairs.\n\nPrint the resulting pairs or output that no solution exists. If there are multiple solutions, print any of them.\n\n\n-----Input-----\n\nThe only line contains two integers $l$ and $r$ ($1 \\le l < r \\le 10^{18}$, $r - l + 1 \\le 3 \\cdot 10^5$, $(r - l)$ is odd).\n\n\n-----Output-----\n\nIf any solution exists, print \"YES\" in the first line. Each of the next $\\frac{r - l + 1}{2}$ lines should contain some pair of integers. GCD of numbers in each pair should be equal to $1$. All $(r - l + 1)$ numbers should be pairwise distinct and should have values from $l$ to $r$ inclusive.\n\nIf there are multiple solutions, print any of them.\n\nIf there exists no solution, print \"NO\".\n\n\n-----Example-----\nInput\n1 8\n\nOutput\nYES\n2 7\n4 1\n3 8\n6 5"} +{"id": "02105", "text": "Happy new year! The year 2020 is also known as Year Gyeongja (\uacbd\uc790\ub144, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.\n\nThere are two sequences of $n$ strings $s_1, s_2, s_3, \\ldots, s_{n}$ and $m$ strings $t_1, t_2, t_3, \\ldots, t_{m}$. These strings contain only lowercase letters. There might be duplicates among these strings.\n\nLet's call a concatenation of strings $x$ and $y$ as the string that is obtained by writing down strings $x$ and $y$ one right after another without changing the order. For example, the concatenation of the strings \"code\" and \"forces\" is the string \"codeforces\".\n\nThe year 1 has a name which is the concatenation of the two strings $s_1$ and $t_1$. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.\n\nFor example, if $n = 3, m = 4, s = ${\"a\", \"b\", \"c\"}, $t =$ {\"d\", \"e\", \"f\", \"g\"}, the following table denotes the resulting year names. Note that the names of the years may repeat. [Image] \n\nYou are given two sequences of strings of size $n$ and $m$ and also $q$ queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?\n\n\n-----Input-----\n\nThe first line contains two integers $n, m$ ($1 \\le n, m \\le 20$).\n\nThe next line contains $n$ strings $s_1, s_2, \\ldots, s_{n}$. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least $1$ and at most $10$.\n\nThe next line contains $m$ strings $t_1, t_2, \\ldots, t_{m}$. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least $1$ and at most $10$.\n\nAmong the given $n + m$ strings may be duplicates (that is, they are not necessarily all different).\n\nThe next line contains a single integer $q$ ($1 \\le q \\le 2\\,020$).\n\nIn the next $q$ lines, an integer $y$ ($1 \\le y \\le 10^9$) is given, denoting the year we want to know the name for.\n\n\n-----Output-----\n\nPrint $q$ lines. For each line, print the name of the year as per the rule described above.\n\n\n-----Example-----\nInput\n10 12\nsin im gye gap eul byeong jeong mu gi gyeong\nyu sul hae ja chuk in myo jin sa o mi sin\n14\n1\n2\n3\n4\n10\n11\n12\n13\n73\n2016\n2017\n2018\n2019\n2020\n\nOutput\nsinyu\nimsul\ngyehae\ngapja\ngyeongo\nsinmi\nimsin\ngyeyu\ngyeyu\nbyeongsin\njeongyu\nmusul\ngihae\ngyeongja\n\n\n\n-----Note-----\n\nThe first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal."} +{"id": "02106", "text": "There are n cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as c_1, c_2, ..., c_{n}. The Old Peykan wants to travel from city c_1 to c_{n} using roads. There are (n - 1) one way roads, the i-th road goes from city c_{i} to city c_{i} + 1 and is d_{i} kilometers long.\n\nThe Old Peykan travels 1 kilometer in 1 hour and consumes 1 liter of fuel during this time.\n\nEach city c_{i} (except for the last city c_{n}) has a supply of s_{i} liters of fuel which immediately transfers to the Old Peykan if it passes the city or stays in it. This supply refreshes instantly k hours after it transfers. The Old Peykan can stay in a city for a while and fill its fuel tank many times. \n\nInitially (at time zero) the Old Peykan is at city c_1 and s_1 liters of fuel is transferred to it's empty tank from c_1's supply. The Old Peykan's fuel tank capacity is unlimited. Old Peykan can not continue its travel if its tank is emptied strictly between two cities.\n\nFind the minimum time the Old Peykan needs to reach city c_{n}.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers m and k (1 \u2264 m, k \u2264 1000). The value m specifies the number of roads between cities which is equal to n - 1.\n\nThe next line contains m space-separated integers d_1, d_2, ..., d_{m} (1 \u2264 d_{i} \u2264 1000) and the following line contains m space-separated integers s_1, s_2, ..., s_{m} (1 \u2264 s_{i} \u2264 1000).\n\n\n-----Output-----\n\nIn the only line of the output print a single integer \u2014 the minimum time required for The Old Peykan to reach city c_{n} from city c_1.\n\n\n-----Examples-----\nInput\n4 6\n1 2 5 2\n2 3 3 4\n\nOutput\n10\n\nInput\n2 3\n5 6\n5 5\n\nOutput\n14\n\n\n\n-----Note-----\n\nIn the second sample above, the Old Peykan stays in c_1 for 3 hours."} +{"id": "02107", "text": "Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with n strings and m frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the i-th string holding it on the j-th fret the guitar produces a note, let's denote it as a_{ij}. We know that Dima's guitar can produce k distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that a_{ij} = a_{pq} at (i, j) \u2260 (p, q).\n\nDima has already written a song \u2014 a sequence of s notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein).\n\nWe'll represent a way to play a song as a sequence of pairs (x_{i}, y_{i}) (1 \u2264 i \u2264 s), such that the x_{i}-th string on the y_{i}-th fret produces the i-th note from the song. The complexity of moving between pairs (x_1, y_1) and (x_2, y_2) equals $|x_{1} - x_{2}|$ + $|y_{1} - y_{2}|$. The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs.\n\nHelp Dima determine the maximum complexity of the way to play his song! The guy's gotta look cool!\n\n\n-----Input-----\n\nThe first line of the input contains four integers n, m, k and s (1 \u2264 n, m \u2264 2000, 1 \u2264 k \u2264 9, 2 \u2264 s \u2264 10^5). \n\nThen follow n lines, each containing m integers a_{ij} (1 \u2264 a_{ij} \u2264 k). The number in the i-th row and the j-th column (a_{ij}) means a note that the guitar produces on the i-th string and the j-th fret.\n\nThe last line of the input contains s integers q_{i} (1 \u2264 q_{i} \u2264 k) \u2014 the sequence of notes of the song.\n\n\n-----Output-----\n\nIn a single line print a single number \u2014 the maximum possible complexity of the song.\n\n\n-----Examples-----\nInput\n4 6 5 7\n3 1 2 2 3 1\n3 2 2 2 5 5\n4 2 2 2 5 3\n3 2 2 1 4 3\n2 3 1 4 1 5 1\n\nOutput\n8\n\nInput\n4 4 9 5\n4 7 9 5\n1 2 1 7\n8 3 4 9\n5 7 7 2\n7 1 9 2 5\n\nOutput\n4"} +{"id": "02108", "text": "You are given an undirected graph without self-loops or multiple edges which consists of $n$ vertices and $m$ edges. Also you are given three integers $n_1$, $n_2$ and $n_3$.\n\nCan you label each vertex with one of three numbers 1, 2 or 3 in such way, that: Each vertex should be labeled by exactly one number 1, 2 or 3; The total number of vertices with label 1 should be equal to $n_1$; The total number of vertices with label 2 should be equal to $n_2$; The total number of vertices with label 3 should be equal to $n_3$; $|col_u - col_v| = 1$ for each edge $(u, v)$, where $col_x$ is the label of vertex\u00a0$x$. \n\nIf there are multiple valid labelings, print any of them.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 5000$; $0 \\le m \\le 10^5$)\u00a0\u2014 the number of vertices and edges in the graph.\n\nThe second line contains three integers $n_1$, $n_2$ and $n_3$ ($0 \\le n_1, n_2, n_3 \\le n$)\u00a0\u2014 the number of labels 1, 2 and 3, respectively. It's guaranteed that $n_1 + n_2 + n_3 = n$.\n\nNext $m$ lines contan description of edges: the $i$-th line contains two integers $u_i$, $v_i$ ($1 \\le u_i, v_i \\le n$; $u_i \\neq v_i$) \u2014 the vertices the $i$-th edge connects. It's guaranteed that the graph doesn't contain self-loops or multiple edges.\n\n\n-----Output-----\n\nIf valid labeling exists then print \"YES\" (without quotes) in the first line. In the second line print string of length $n$ consisting of 1, 2 and 3. The $i$-th letter should be equal to the label of the $i$-th vertex.\n\nIf there is no valid labeling, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n6 3\n2 2 2\n3 1\n5 4\n2 5\n\nOutput\nYES\n112323\n\nInput\n5 9\n0 2 3\n1 2\n1 3\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5\n\nOutput\nNO"} +{"id": "02109", "text": "Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: \n\n The product of all numbers in the first set is less than zero ( < 0). The product of all numbers in the second set is greater than zero ( > 0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. \n\nHelp Vitaly. Divide the given array.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (3 \u2264 n \u2264 100). The second line contains n space-separated distinct integers a_1, a_2, ..., a_{n} (|a_{i}| \u2264 10^3) \u2014 the array elements.\n\n\n-----Output-----\n\nIn the first line print integer n_1 (n_1 > 0) \u2014 the number of elements in the first set. Then print n_1 numbers \u2014 the elements that got to the first set.\n\nIn the next line print integer n_2 (n_2 > 0) \u2014 the number of elements in the second set. Then print n_2 numbers \u2014 the elements that got to the second set.\n\nIn the next line print integer n_3 (n_3 > 0) \u2014 the number of elements in the third set. Then print n_3 numbers \u2014 the elements that got to the third set.\n\nThe printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n3\n-1 2 0\n\nOutput\n1 -1\n1 2\n1 0\n\nInput\n4\n-1 -2 -3 0\n\nOutput\n1 -1\n2 -3 -2\n1 0"} +{"id": "02110", "text": "There are many freight trains departing from Kirnes planet every day. One day on that planet consists of $h$ hours, and each hour consists of $m$ minutes, where $m$ is an even number. Currently, there are $n$ freight trains, and they depart every day at the same time: $i$-th train departs at $h_i$ hours and $m_i$ minutes.\n\nThe government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at $0$ hours and $t$ minutes, where $0 \\le t < {m \\over 2}$, the second tram departs $m \\over 2$ minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.\n\nTo allow passengers to board the tram safely, the tram must arrive $k$ minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at $0$ hours and $t$ minutes, where $t < k$, then the freight trains can not depart during the last $k - t$ minutes of the day.\n\n $O$ A schematic picture of the correct way to run passenger trams. Here $h=2$ (therefore, the number of passenger trams is $2h=4$), the number of freight trains is $n=6$. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length $k$ before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments. \n\nUnfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of $t$ to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.\n\n\n-----Input-----\n\nThe first line of input contains four integers $n$, $h$, $m$, $k$ ($1 \\le n \\le 100\\,000$, $1 \\le h \\le 10^9$, $2 \\le m \\le 10^9$, $m$ is even, $1 \\le k \\le {m \\over 2}$)\u00a0\u2014 the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram.\n\n$n$ lines follow, each contains two integers $h_i$ and $m_i$ ($0 \\le h_i < h$, $0 \\le m_i < m$)\u00a0\u2014 the time when $i$-th freight train departs. It is guaranteed that no freight trains depart at the same time.\n\n\n-----Output-----\n\nThe first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time $t$. Second line of output should contain freight trains that need to be canceled.\n\n\n-----Examples-----\nInput\n2 24 60 15\n16 0\n17 15\n\nOutput\n0 0\n\n\nInput\n2 24 60 16\n16 0\n17 15\n\nOutput\n1 0\n2 \n\n\n\n-----Note-----\n\nIn the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram.\n\nIn the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if $t \\in [1, 15]$, then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If $t = 0$ or $t \\in [16, 29]$, then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose $t = 0$. Another possible option is to cancel the first train and choose $t = 13$."} +{"id": "02111", "text": "Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.\n\nA bar of chocolate can be presented as an n \u00d7 n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.\n\nAfter each action, he wants to know how many pieces he ate as a result of this action.\n\n\n-----Input-----\n\nThe first line contains integers n (1 \u2264 n \u2264 10^9) and q (1 \u2264 q \u2264 2\u00b710^5) \u2014 the size of the chocolate bar and the number of actions.\n\nNext q lines contain the descriptions of the actions: the i-th of them contains numbers x_{i} and y_{i} (1 \u2264 x_{i}, y_{i} \u2264 n, x_{i} + y_{i} = n + 1) \u2014 the numbers of the column and row of the chosen cell and the character that represents the direction (L \u2014 left, U \u2014 up).\n\n\n-----Output-----\n\nPrint q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.\n\n\n-----Examples-----\nInput\n6 5\n3 4 U\n6 1 L\n2 5 L\n1 6 U\n4 3 U\n\nOutput\n4\n3\n2\n1\n2\n\nInput\n10 6\n2 9 U\n10 1 U\n1 10 U\n8 3 L\n10 1 L\n6 5 U\n\nOutput\n9\n1\n10\n6\n0\n2\n\n\n\n-----Note-----\n\nPictures to the sample tests:\n\n[Image]\n\nThe pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.\n\nIn the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything."} +{"id": "02112", "text": "There are $n$ warriors in a row. The power of the $i$-th warrior is $a_i$. All powers are pairwise distinct.\n\nYou have two types of spells which you may cast: Fireball: you spend $x$ mana and destroy exactly $k$ consecutive warriors; Berserk: you spend $y$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. \n\nFor example, let the powers of warriors be $[2, 3, 7, 8, 11, 5, 4]$, and $k = 3$. If you cast Berserk on warriors with powers $8$ and $11$, the resulting sequence of powers becomes $[2, 3, 7, 11, 5, 4]$. Then, for example, if you cast Fireball on consecutive warriors with powers $[7, 11, 5]$, the resulting sequence of powers becomes $[2, 3, 4]$.\n\nYou want to turn the current sequence of warriors powers $a_1, a_2, \\dots, a_n$ into $b_1, b_2, \\dots, b_m$. Calculate the minimum amount of mana you need to spend on it.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$)\u00a0\u2014 the length of sequence $a$ and the length of sequence $b$ respectively.\n\nThe second line contains three integers $x, k, y$ ($1 \\le x, y, \\le 10^9; 1 \\le k \\le n$)\u00a0\u2014 the cost of fireball, the range of fireball and the cost of berserk respectively.\n\nThe third line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$). It is guaranteed that all integers $a_i$ are pairwise distinct.\n\nThe fourth line contains $m$ integers $b_1, b_2, \\dots, b_m$ ($1 \\le b_i \\le n$). It is guaranteed that all integers $b_i$ are pairwise distinct.\n\n\n-----Output-----\n\nPrint the minimum amount of mana for turning the sequnce $a_1, a_2, \\dots, a_n$ into $b_1, b_2, \\dots, b_m$, or $-1$ if it is impossible.\n\n\n-----Examples-----\nInput\n5 2\n5 2 3\n3 1 4 5 2\n3 5\n\nOutput\n8\n\nInput\n4 4\n5 1 4\n4 3 1 2\n2 4 3 1\n\nOutput\n-1\n\nInput\n4 4\n2 1 11\n1 3 2 4\n1 3 2 4\n\nOutput\n0"} +{"id": "02113", "text": "Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.\n\nA tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.\n\nDr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?\n\nA loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .\n\n\n-----Input-----\n\nThe first line of input contains an integer n\u00a0\u2014 the number of nodes in the tree (1 \u2264 n \u2264 10^5).\n\nThe next n - 1 lines contain integers u and v (1 \u2264 u, v \u2264 n, u \u2260 v)\u00a0\u2014 the description of the edges of the tree.\n\nIt's guaranteed that the given graph is a tree. \n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.\n\n\n-----Examples-----\nInput\n3\n1 2\n1 3\n\nOutput\n0\n\nInput\n5\n1 2\n2 3\n3 4\n4 5\n\nOutput\n2\n\n\n\n-----Note-----\n\nTree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)\n\nBipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graph\n\nIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.\n\nIn the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5)."} +{"id": "02114", "text": "Egor wants to achieve a rating of 1600 points on the well-known chess portal ChessForces and he needs your help!\n\nBefore you start solving the problem, Egor wants to remind you how the chess pieces move. Chess rook moves along straight lines up and down, left and right, as many squares as it wants. And when it wants, it can stop. The queen walks in all directions vertically and diagonally at any distance. You can see the examples below. [Image] \n\nTo reach the goal, Egor should research the next topic:\n\nThere is an $N \\times N$ board. Each cell of the board has a number from $1$ to $N ^ 2$ in it and numbers in all cells are distinct.\n\nIn the beginning, some chess figure stands in the cell with the number $1$. Note that this cell is already considered as visited. After that every move is determined by the following rules: Among all not visited yet cells to which the figure can get in one move, it goes to the cell that has minimal number. If all accessible cells were already visited and some cells are not yet visited, then the figure is teleported to the not visited cell that has minimal number. If this step happens, the piece pays a fee of $1$ vun. If all cells are already visited, the process is stopped. \n\nEgor should find an $N \\times N$ board on which the rook pays strictly less vuns than the queen during the round with this numbering. Help him to find such $N \\times N$ numbered board, or tell that it doesn't exist.\n\n\n-----Input-----\n\nThe only line contains one integer $N$ \u00a0\u2014 the size of the board, $1\\le N \\le 500$.\n\n\n-----Output-----\n\nThe output should contain $N$ lines.\n\nIn $i$-th line output $N$ numbers \u00a0\u2014 numbers on the $i$-th row of the board. All numbers from $1$ to $N \\times N$ must be used exactly once.\n\nOn your board rook must pay strictly less vuns than the queen.\n\nIf there are no solutions, print $-1$.\n\nIf there are several solutions, you can output any of them. \n\n\n-----Examples-----\nInput\n1\n\nOutput\n-1\nInput\n4\n\nOutput\n4 3 6 12 \n7 5 9 15 \n14 1 11 10 \n13 8 16 2 \n\n\n\n-----Note-----\n\nIn case we have $1 \\times 1$ board, both rook and queen do not have a chance to pay fees.\n\nIn second sample rook goes through cells $1 \\to 3 \\to 4 \\to 6 \\to 9 \\to 5 \\to 7 \\to 13 \\to 2 \\to 8 \\to 16 \\to 11 \\to 10 \\to 12 \\to 15 \\to \\textbf{(1 vun)} \\to 14$. \n\nQueen goes through $1 \\to 3 \\to 4 \\to 2 \\to 5 \\to 6 \\to 9 \\to 7 \\to 13 \\to 8 \\to 11 \\to 10 \\to 12 \\to 15 \\to \\textbf{(1 vun)} \\to 14 \\to \\textbf{(1 vun)} \\to 16$.\n\nAs a result rook pays 1 vun and queen pays 2 vuns."} +{"id": "02115", "text": "You are given a sequence of positive integers a_1, a_2, ..., a_{n}. \n\nWhile possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements). If the two integers are equal to x, delete both and insert a single integer x + 1 on their place. This way the number of elements in the sequence is decreased by 1 on each step. \n\nYou stop performing the operation when there is no pair of equal consecutive elements.\n\nFor example, if the initial sequence is [5, 2, 1, 1, 2, 2], then after the first operation you get [5, 2, 2, 2, 2], after the second \u2014 [5, 3, 2, 2], after the third \u2014 [5, 3, 3], and finally after the fourth you get [5, 4]. After that there are no equal consecutive elements left in the sequence, so you stop the process.\n\nDetermine the final sequence after you stop performing the operation.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 2\u00b710^5) \u2014 the number of elements in the sequence.\n\nThe second line contains the sequence of integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nIn the first line print a single integer k \u2014 the number of elements in the sequence after you stop performing the operation. \n\nIn the second line print k integers\u00a0\u2014 the sequence after you stop performing the operation.\n\n\n-----Examples-----\nInput\n6\n5 2 1 1 2 2\n\nOutput\n2\n5 4 \nInput\n4\n1000000000 1000000000 1000000000 1000000000\n\nOutput\n1\n1000000002 \nInput\n7\n4 10 22 11 12 5 6\n\nOutput\n7\n4 10 22 11 12 5 6 \n\n\n-----Note-----\n\nThe first example is described in the statements.\n\nIn the second example the initial sequence is [1000000000, 1000000000, 1000000000, 1000000000]. After the first operation the sequence is equal to [1000000001, 1000000000, 1000000000]. After the second operation the sequence is [1000000001, 1000000001]. After the third operation the sequence is [1000000002].\n\nIn the third example there are no two equal consecutive elements initially, so the sequence does not change."} +{"id": "02116", "text": "Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect\" service which allows users to shop online. \n\nThe store contains k items. n customers have already used the above service. Each user paid for m items. Let a_{ij} denote the j-th item in the i-th person's order.\n\nDue to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items a_{ij} (1 \u2264 j \u2264 m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(a_{i}1) + pos(a_{i}2) + ... + pos(a_{im}) for the i-th customer.\n\nWhen Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating.\n\nYour task is to calculate the total time it takes for Ayush to process all the orders.\n\nYou can assume that the market has endless stock.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and k (1 \u2264 n, k \u2264 100, 1 \u2264 m \u2264 k) \u2014 the number of users, the number of items each user wants to buy and the total number of items at the market.\n\nThe next line contains k distinct integers p_{l} (1 \u2264 p_{l} \u2264 k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k.\n\nEach of the next n lines contains m distinct integers a_{ij} (1 \u2264 a_{ij} \u2264 k) \u2014 the order of the i-th person.\n\n\n-----Output-----\n\nPrint the only integer t \u2014 the total time needed for Ayush to process all the orders.\n\n\n-----Example-----\nInput\n2 2 5\n3 4 1 2 5\n1 5\n3 1\n\nOutput\n14\n\n\n\n-----Note-----\n\nCustomer 1 wants the items 1 and 5.\n\npos(1) = 3, so the new positions are: [1, 3, 4, 2, 5].\n\npos(5) = 5, so the new positions are: [5, 1, 3, 4, 2].\n\nTime taken for the first customer is 3 + 5 = 8.\n\nCustomer 2 wants the items 3 and 1.\n\npos(3) = 3, so the new positions are: [3, 5, 1, 4, 2].\n\npos(1) = 3, so the new positions are: [1, 3, 5, 4, 2].\n\nTime taken for the second customer is 3 + 3 = 6.\n\nTotal time is 8 + 6 = 14.\n\nFormally pos(x) is the index of x in the current row."} +{"id": "02117", "text": "The Resistance is trying to take control over as many planets of a particular solar system as possible. Princess Heidi is in charge of the fleet, and she must send ships to some planets in order to maximize the number of controlled planets.\n\nThe Galaxy contains N planets, connected by bidirectional hyperspace tunnels in such a way that there is a unique path between every pair of the planets.\n\nA planet is controlled by the Resistance if there is a Resistance ship in its orbit, or if the planet lies on the shortest path between some two planets that have Resistance ships in their orbits.\n\nHeidi has not yet made up her mind as to how many ships to use. Therefore, she is asking you to compute, for every K = 1, 2, 3, ..., N, the maximum number of planets that can be controlled with a fleet consisting of K ships.\n\n\n-----Input-----\n\nThe first line of the input contains an integer N (1 \u2264 N \u2264 10^5) \u2013 the number of planets in the galaxy.\n\nThe next N - 1 lines describe the hyperspace tunnels between the planets. Each of the N - 1 lines contains two space-separated integers u and v (1 \u2264 u, v \u2264 N) indicating that there is a bidirectional hyperspace tunnel between the planets u and v. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets.\n\n\n-----Output-----\n\nOn a single line, print N space-separated integers. The K-th number should correspond to the maximum number of planets that can be controlled by the Resistance using a fleet of K ships.\n\n\n-----Examples-----\nInput\n3\n1 2\n2 3\n\nOutput\n1 3 3 \nInput\n4\n1 2\n3 2\n4 2\n\nOutput\n1 3 4 4 \n\n\n-----Note-----\n\nConsider the first example. If K = 1, then Heidi can only send one ship to some planet and control it. However, for K \u2265 2, sending ships to planets 1 and 3 will allow the Resistance to control all planets."} +{"id": "02118", "text": "Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows: If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l \u2264 i < r - 1 a[i] \u2264 a[i + 1]), then end the function call; Let $\\operatorname{mid} = \\lfloor \\frac{l + r}{2} \\rfloor$; Call mergesort(a, l, mid); Call mergesort(a, mid, r); Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn't call any other functions. \n\nThe array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n).\n\nThe number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort \u2014 mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted.\n\nIvan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k.\n\nHelp Ivan to find an array he wants!\n\n\n-----Input-----\n\nThe first line contains two numbers n and k (1 \u2264 n \u2264 100000, 1 \u2264 k \u2264 200000) \u2014 the size of a desired permutation and the number of mergesort calls required to sort it.\n\n\n-----Output-----\n\nIf a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn't exist, output - 1. Otherwise output n integer numbers a[0], a[1], ..., a[n - 1] \u2014 the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n3 3\n\nOutput\n2 1 3 \nInput\n4 1\n\nOutput\n1 2 3 4 \nInput\n5 6\n\nOutput\n-1"} +{"id": "02119", "text": "Vasya owns three big integers \u2014 $a, l, r$. Let's define a partition of $x$ such a sequence of strings $s_1, s_2, \\dots, s_k$ that $s_1 + s_2 + \\dots + s_k = x$, where $+$ is a concatanation of strings. $s_i$ is the $i$-th element of the partition. For example, number $12345$ has the following partitions: [\"1\", \"2\", \"3\", \"4\", \"5\"], [\"123\", \"4\", \"5\"], [\"1\", \"2345\"], [\"12345\"] and lots of others.\n\nLet's call some partition of $a$ beautiful if each of its elements contains no leading zeros.\n\nVasya want to know the number of beautiful partitions of number $a$, which has each of $s_i$ satisfy the condition $l \\le s_i \\le r$. Note that the comparison is the integer comparison, not the string one.\n\nHelp Vasya to count the amount of partitions of number $a$ such that they match all the given requirements. The result can be rather big, so print it modulo $998244353$.\n\n\n-----Input-----\n\nThe first line contains a single integer $a~(1 \\le a \\le 10^{1000000})$.\n\nThe second line contains a single integer $l~(0 \\le l \\le 10^{1000000})$.\n\nThe third line contains a single integer $r~(0 \\le r \\le 10^{1000000})$.\n\nIt is guaranteed that $l \\le r$.\n\nIt is also guaranteed that numbers $a, l, r$ contain no leading zeros.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the amount of partitions of number $a$ such that they match all the given requirements modulo $998244353$.\n\n\n-----Examples-----\nInput\n135\n1\n15\n\nOutput\n2\n\nInput\n10000\n0\n9\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first test case, there are two good partitions $13+5$ and $1+3+5$.\n\nIn the second test case, there is one good partition $1+0+0+0+0$."} +{"id": "02120", "text": "On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy.\n\nThe toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as v_{i}. The child spend v_{f}_1 + v_{f}_2 + ... + v_{f}_{k} energy for removing part i where f_1, f_2, ..., f_{k} are the parts that are directly connected to the i-th and haven't been removed.\n\nHelp the child to find out, what is the minimum total energy he should spend to remove all n parts.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 1000; 0 \u2264 m \u2264 2000). The second line contains n integers: v_1, v_2, ..., v_{n} (0 \u2264 v_{i} \u2264 10^5). Then followed m lines, each line contains two integers x_{i} and y_{i}, representing a rope from part x_{i} to part y_{i} (1 \u2264 x_{i}, y_{i} \u2264 n;\u00a0x_{i} \u2260 y_{i}).\n\nConsider all the parts are numbered from 1 to n.\n\n\n-----Output-----\n\nOutput the minimum total energy the child should spend to remove all n parts of the toy.\n\n\n-----Examples-----\nInput\n4 3\n10 20 30 40\n1 4\n1 2\n2 3\n\nOutput\n40\n\nInput\n4 4\n100 100 100 100\n1 2\n2 3\n2 4\n3 4\n\nOutput\n400\n\nInput\n7 10\n40 10 20 10 20 80 40\n1 5\n4 7\n4 5\n5 2\n5 7\n6 4\n1 6\n1 3\n4 3\n1 4\n\nOutput\n160\n\n\n\n-----Note-----\n\nOne of the optimal sequence of actions in the first sample is: First, remove part 3, cost of the action is 20. Then, remove part 2, cost of the action is 10. Next, remove part 4, cost of the action is 10. At last, remove part 1, cost of the action is 0. \n\nSo the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum.\n\nIn the second sample, the child will spend 400 no matter in what order he will remove the parts."} +{"id": "02121", "text": "For his computer science class, Jacob builds a model tree with sticks and balls containing n nodes in the shape of a tree. Jacob has spent a_{i} minutes building the i-th ball in the tree.\n\nJacob's teacher will evaluate his model and grade Jacob based on the effort he has put in. However, she does not have enough time to search his whole tree to determine this; Jacob knows that she will examine the first k nodes in a DFS-order traversal of the tree. She will then assign Jacob a grade equal to the minimum a_{i} she finds among those k nodes.\n\nThough Jacob does not have enough time to rebuild his model, he can choose the root node that his teacher starts from. Furthermore, he can rearrange the list of neighbors of each node in any order he likes. Help Jacob find the best grade he can get on this assignment.\n\nA DFS-order traversal is an ordering of the nodes of a rooted tree, built by a recursive DFS-procedure initially called on the root of the tree. When called on a given node v, the procedure does the following: Print v. Traverse the list of neighbors of the node v in order and iteratively call DFS-procedure on each one. Do not call DFS-procedure on node u if you came to node v directly from u. \n\n\n-----Input-----\n\nThe first line of the input contains two positive integers, n and k (2 \u2264 n \u2264 200 000, 1 \u2264 k \u2264 n)\u00a0\u2014 the number of balls in Jacob's tree and the number of balls the teacher will inspect.\n\nThe second line contains n integers, a_{i} (1 \u2264 a_{i} \u2264 1 000 000), the time Jacob used to build the i-th ball.\n\nEach of the next n - 1 lines contains two integers u_{i}, v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i}) representing a connection in Jacob's tree between balls u_{i} and v_{i}.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum grade Jacob can get by picking the right root of the tree and rearranging the list of neighbors.\n\n\n-----Examples-----\nInput\n5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3\n\nOutput\n3\n\nInput\n4 2\n1 5 5 5\n1 2\n1 3\n1 4\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample, Jacob can root the tree at node 2 and order 2's neighbors in the order 4, 1, 5 (all other nodes have at most two neighbors). The resulting preorder traversal is 2, 4, 1, 3, 5, and the minimum a_{i} of the first 3 nodes is 3.\n\nIn the second sample, it is clear that any preorder traversal will contain node 1 as either its first or second node, so Jacob cannot do better than a grade of 1."} +{"id": "02122", "text": "Whereas humans nowadays read fewer and fewer books on paper, book readership among marmots has surged. Heidi has expanded the library and is now serving longer request sequences.\n\n\n-----Input-----\n\nSame as the easy version, but the limits have changed: 1 \u2264 n, k \u2264 400 000.\n\n\n-----Output-----\n\nSame as the easy version.\n\n\n-----Examples-----\nInput\n4 100\n1 2 2 1\n\nOutput\n2\n\nInput\n4 1\n1 2 2 1\n\nOutput\n3\n\nInput\n4 2\n1 2 3 1\n\nOutput\n3"} +{"id": "02123", "text": "Caisa solved the problem with the sugar and now he is on the way back to home. \n\nCaisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height h_{i}. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by h_{k} - h_{k} + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time. \n\nInitially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5). The next line contains n integers h_1, h_2, ..., h_{n} (1 \u2264 h_{i} \u2264 10^5) representing the heights of the pylons.\n\n\n-----Output-----\n\nPrint a single number representing the minimum number of dollars paid by Caisa.\n\n\n-----Examples-----\nInput\n5\n3 4 3 2 4\n\nOutput\n4\n\nInput\n3\n4 4 4\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon."} +{"id": "02124", "text": "Recently Vladik discovered a new entertainment\u00a0\u2014 coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.\n\nAt first, he need to download t chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages.\n\nVladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages.\n\nHe has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats!\n\n\n-----Input-----\n\nThe first line contains single integer t (1 \u2264 t \u2264 10) \u2014 the number of chats. The t chats follow. Each chat is given in the following format.\n\nThe first line of each chat description contains single integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of users in the chat.\n\nThe next line contains n space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters.\n\nThe next line contains single integer m (1 \u2264 m \u2264 100)\u00a0\u2014 the number of messages in the chat. The next m line contain the messages in the following formats, one per line: :\u00a0\u2014 the format of a message with known sender. The username should appear in the list of usernames of the chat. :\u00a0\u2014 the format of a message with unknown sender. \n\nThe text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters.\n\nWe say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text \"Vasya, masha13 and Kate!\" can mention users \"Vasya\", \"masha13\", \"and\" and \"Kate\", but not \"masha\".\n\nIt is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender.\n\n\n-----Output-----\n\nPrint the information about the t chats in the following format:\n\nIf it is not possible to recover senders, print single line \"Impossible\" for this chat. Otherwise print m messages in the following format:\n\n:\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi\n\nOutput\nnetman: Hello, Vladik!\nVladik: Hi\n\nInput\n1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine\n\nOutput\nImpossible\n\nInput\n2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\n?: yes, netman\nbanany2001: yes of course.\n\nOutput\nImpossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course."} +{"id": "02125", "text": "Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \\cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns. \n\nThe colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.\n\nInnokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image]\u00a0[Image]\u00a0[Image]\n\nThese subrectangles are flags. [Image]\u00a0[Image]\u00a0[Image]\u00a0[Image]\u00a0[Image]\u00a0[Image]\n\nThese subrectangles are not flags. \n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 1\\,000$)\u00a0\u2014 the number of rows and the number of columns on the blanket.\n\nEach of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.\n\n\n-----Output-----\n\nIn the only line print the number of subrectangles which form valid flags.\n\n\n-----Examples-----\nInput\n4 3\naaa\nbbb\nccb\nddd\n\nOutput\n6\n\nInput\n6 1\na\na\nb\nb\nc\nc\n\nOutput\n1\n\n\n\n-----Note----- [Image]\u00a0[Image]\n\nThe selected subrectangles are flags in the first example."} +{"id": "02126", "text": "Luckily, Serval got onto the right bus, and he came to the kindergarten on time. After coming to kindergarten, he found the toy bricks very funny.\n\nHe has a special interest to create difficult problems for others to solve. This time, with many $1 \\times 1 \\times 1$ toy bricks, he builds up a 3-dimensional object. We can describe this object with a $n \\times m$ matrix, such that in each cell $(i,j)$, there are $h_{i,j}$ bricks standing on the top of each other.\n\nHowever, Serval doesn't give you any $h_{i,j}$, and just give you the front view, left view, and the top view of this object, and he is now asking you to restore the object. Note that in the front view, there are $m$ columns, and in the $i$-th of them, the height is the maximum of $h_{1,i},h_{2,i},\\dots,h_{n,i}$. It is similar for the left view, where there are $n$ columns. And in the top view, there is an $n \\times m$ matrix $t_{i,j}$, where $t_{i,j}$ is $0$ or $1$. If $t_{i,j}$ equals $1$, that means $h_{i,j}>0$, otherwise, $h_{i,j}=0$.\n\nHowever, Serval is very lonely because others are bored about his unsolvable problems before, and refused to solve this one, although this time he promises there will be at least one object satisfying all the views. As his best friend, can you have a try?\n\n\n-----Input-----\n\nThe first line contains three positive space-separated integers $n, m, h$ ($1\\leq n, m, h \\leq 100$)\u00a0\u2014 the length, width and height.\n\nThe second line contains $m$ non-negative space-separated integers $a_1,a_2,\\dots,a_m$, where $a_i$ is the height in the $i$-th column from left to right of the front view ($0\\leq a_i \\leq h$).\n\nThe third line contains $n$ non-negative space-separated integers $b_1,b_2,\\dots,b_n$ ($0\\leq b_j \\leq h$), where $b_j$ is the height in the $j$-th column from left to right of the left view.\n\nEach of the following $n$ lines contains $m$ numbers, each is $0$ or $1$, representing the top view, where $j$-th number of $i$-th row is $1$ if $h_{i, j}>0$, and $0$ otherwise.\n\nIt is guaranteed that there is at least one structure satisfying the input.\n\n\n-----Output-----\n\nOutput $n$ lines, each of them contains $m$ integers, the $j$-th number in the $i$-th line should be equal to the height in the corresponding position of the top view. If there are several objects satisfying the views, output any one of them.\n\n\n-----Examples-----\nInput\n3 7 3\n2 3 0 0 2 0 1\n2 1 3\n1 0 0 0 1 0 0\n0 0 0 0 0 0 1\n1 1 0 0 0 0 0\n\nOutput\n1 0 0 0 2 0 0\n0 0 0 0 0 0 1\n2 3 0 0 0 0 0\n\nInput\n4 5 5\n3 5 2 0 4\n4 2 5 4\n0 0 0 0 1\n1 0 1 0 0\n0 1 0 0 0\n1 1 1 0 0\n\nOutput\n0 0 0 0 4\n1 0 2 0 0\n0 5 0 0 0\n3 4 1 0 0\n\n\n\n-----Note-----\n\n [Image] \n\nThe graph above illustrates the object in the first example.\n\n [Image] \n\n [Image] \n\nThe first graph illustrates the object in the example output for the second example, and the second graph shows the three-view drawing of it."} +{"id": "02127", "text": "Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.\n\nBerland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).\n\nA bill $x \\times y$ fits into some wallet $h \\times w$ if either $x \\le h$ and $y \\le w$ or $y \\le h$ and $x \\le w$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.\n\nNow you are asked to perform the queries of two types:\n\n $+~x~y$ \u2014 Polycarp earns a bill of size $x \\times y$; $?~h~w$ \u2014 Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $h \\times w$. \n\nIt is guaranteed that there is at least one query of type $1$ before the first query of type $2$ and that there is at least one query of type $2$ in the input data.\n\nFor each query of type $2$ print \"YES\" if all the bills he has earned to this moment fit into a wallet of given size. Print \"NO\" otherwise.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 5 \\cdot 10^5$) \u2014 the number of queries.\n\nEach of the next $n$ lines contains a query of one of these two types:\n\n $+~x~y$ ($1 \\le x, y \\le 10^9$) \u2014 Polycarp earns a bill of size $x \\times y$; $?~h~w$ ($1 \\le h, w \\le 10^9$) \u2014 Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $h \\times w$. \n\nIt is guaranteed that there is at least one query of type $1$ before the first query of type $2$ and that there is at least one query of type $2$ in the input data.\n\n\n-----Output-----\n\nFor each query of type $2$ print \"YES\" if all the bills he has earned to this moment fit into a wallet of given size. Print \"NO\" otherwise.\n\n\n-----Example-----\nInput\n9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1\n\nOutput\nNO\nYES\nYES\nYES\nNO\n\n\n\n-----Note-----\n\nThe queries of type $2$ of the example:\n\n Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $1 \\times 5$ fit (all the others don't, thus it's \"NO\")."} +{"id": "02128", "text": "Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror \"Am I beautiful?\". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\\frac{p_i}{100}$ for all $1 \\le i \\le n$.\n\nCreatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the $1$-st mirror again. \n\nYou need to calculate the expected number of days until Creatnx becomes happy.\n\nThis number should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\\frac{p}{q}$, where $p$ and $q$ are integers and $q \\not \\equiv 0 \\pmod{M}$. Output the integer equal to $p \\cdot q^{-1} \\bmod M$. In other words, output such an integer $x$ that $0 \\le x < M$ and $x \\cdot q \\equiv p \\pmod{M}$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1\\le n\\le 2\\cdot 10^5$)\u00a0\u2014 the number of mirrors.\n\nThe second line contains $n$ integers $p_1, p_2, \\ldots, p_n$ ($1 \\leq p_i \\leq 100$).\n\n\n-----Output-----\n\nPrint the answer modulo $998244353$ in a single line.\n\n\n-----Examples-----\nInput\n1\n50\n\nOutput\n2\n\nInput\n3\n10 20 50\n\nOutput\n112\n\n\n\n-----Note-----\n\nIn the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability $\\frac{1}{2}$. So, the expected number of days until Creatnx becomes happy is $2$."} +{"id": "02129", "text": "There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads.\n\nThe road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it.\n\n\n-----Input-----\n\nThe first line contains a positive integer t (1 \u2264 t \u2264 200)\u00a0\u2014 the number of testsets in the input.\n\nEach of the testsets is given in the following way. The first line contains two integers n and m (1 \u2264 n \u2264 200, 0 \u2264 m \u2264 n\u00b7(n - 1) / 2)\u00a0\u2014 the number of cities and the number of roads in Berland. \n\nThe next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 \u2264 u, v \u2264 n)\u00a0\u2014 the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities.\n\nIt is guaranteed that the total number of cities in all testset of input data doesn't exceed 200.\n\nPay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one.\n\n\n-----Output-----\n\nFor each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it.\n\nIn the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. \n\n\n-----Example-----\nInput\n2\n5 5\n2 1\n4 5\n2 3\n1 3\n3 5\n7 2\n3 7\n4 2\n\nOutput\n3\n1 3\n3 5\n5 4\n3 2\n2 1\n3\n2 4\n3 7"} +{"id": "02130", "text": "Vitya has learned that the answer for The Ultimate Question of Life, the Universe, and Everything is not the integer 54 42, but an increasing integer sequence $a_1, \\ldots, a_n$. In order to not reveal the secret earlier than needed, Vitya encrypted the answer and obtained the sequence $b_1, \\ldots, b_n$ using the following rules:\n\n $b_1 = a_1$;\n\n $b_i = a_i \\oplus a_{i - 1}$ for all $i$ from 2 to $n$, where $x \\oplus y$ is the bitwise XOR of $x$ and $y$. \n\nIt is easy to see that the original sequence can be obtained using the rule $a_i = b_1 \\oplus \\ldots \\oplus b_i$.\n\nHowever, some time later Vitya discovered that the integers $b_i$ in the cypher got shuffled, and it can happen that when decrypted using the rule mentioned above, it can produce a sequence that is not increasing. In order to save his reputation in the scientific community, Vasya decided to find some permutation of integers $b_i$ so that the sequence $a_i = b_1 \\oplus \\ldots \\oplus b_i$ is strictly increasing. Help him find such a permutation or determine that it is impossible.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^5$).\n\nThe second line contains $n$ integers $b_1, \\ldots, b_n$ ($1 \\leq b_i < 2^{60}$).\n\n\n-----Output-----\n\nIf there are no valid permutations, print a single line containing \"No\".\n\nOtherwise in the first line print the word \"Yes\", and in the second line print integers $b'_1, \\ldots, b'_n$\u00a0\u2014 a valid permutation of integers $b_i$. The unordered multisets $\\{b_1, \\ldots, b_n\\}$ and $\\{b'_1, \\ldots, b'_n\\}$ should be equal, i.\u00a0e. for each integer $x$ the number of occurrences of $x$ in the first multiset should be equal to the number of occurrences of $x$ in the second multiset. Apart from this, the sequence $a_i = b'_1 \\oplus \\ldots \\oplus b'_i$ should be strictly increasing.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\nNo\n\nInput\n6\n4 7 7 12 31 61\n\nOutput\nYes\n4 12 7 31 7 61 \n\n\n\n-----Note-----\n\nIn the first example no permutation is valid.\n\nIn the second example the given answer lead to the sequence $a_1 = 4$, $a_2 = 8$, $a_3 = 15$, $a_4 = 16$, $a_5 = 23$, $a_6 = 42$."} +{"id": "02131", "text": "Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!\n\nHe created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!\n\nThe decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path.\n\nHelp Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\leq n \\leq 10^{5}$) the number of nodes in the tree.\n\nEach of the next $n - 1$ lines contains two integers $a_i$ and $b_i$ ($1 \\leq a_i, b_i \\leq n$, $a_i \\neq b_i$)\u00a0\u2014 the edges of the tree. It is guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nIf there are no decompositions, print the only line containing \"No\".\n\nOtherwise in the first line print \"Yes\", and in the second line print the number of paths in the decomposition $m$. \n\nEach of the next $m$ lines should contain two integers $u_i$, $v_i$ ($1 \\leq u_i, v_i \\leq n$, $u_i \\neq v_i$) denoting that one of the paths in the decomposition is the simple path between nodes $u_i$ and $v_i$. \n\nEach pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order.\n\nIf there are multiple decompositions, print any.\n\n\n-----Examples-----\nInput\n4\n1 2\n2 3\n3 4\n\nOutput\nYes\n1\n1 4\n\nInput\n6\n1 2\n2 3\n3 4\n2 5\n3 6\n\nOutput\nNo\n\nInput\n5\n1 2\n1 3\n1 4\n1 5\n\nOutput\nYes\n4\n1 2\n1 3\n1 4\n1 5\n\n\n\n-----Note-----\n\nThe tree from the first example is shown on the picture below: [Image] The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.\n\nThe tree from the second example is shown on the picture below: [Image] We can show that there are no valid decompositions of this tree.\n\nThe tree from the third example is shown on the picture below: [Image] The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions."} +{"id": "02132", "text": "Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types.\n\n speed limit: this sign comes with a positive integer number \u2014 maximal speed of the car after the sign (cancel the action of the previous sign of this type); overtake is allowed: this sign means that after some car meets it, it can overtake any other car; no speed limit: this sign cancels speed limit if any (car can move with arbitrary speed after this sign); no overtake allowed: some car can't overtake any other car after this sign. \n\nPolycarp goes past the signs consequentially, each new sign cancels the action of all the previous signs of it's kind (speed limit/overtake). It is possible that two or more \"no overtake allowed\" signs go one after another with zero \"overtake is allowed\" signs between them. It works with \"no speed limit\" and \"overtake is allowed\" signs as well.\n\nIn the beginning of the ride overtake is allowed and there is no speed limit.\n\nYou are given the sequence of events in chronological order \u2014 events which happened to Polycarp during the ride. There are events of following types:\n\n Polycarp changes the speed of his car to specified (this event comes with a positive integer number); Polycarp's car overtakes the other car; Polycarp's car goes past the \"speed limit\" sign (this sign comes with a positive integer); Polycarp's car goes past the \"overtake is allowed\" sign; Polycarp's car goes past the \"no speed limit\"; Polycarp's car goes past the \"no overtake allowed\"; \n\nIt is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).\n\nAfter the exam Polycarp can justify his rule violations by telling the driving instructor that he just didn't notice some of the signs. What is the minimal number of signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view?\n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 2\u00b710^5) \u2014 number of events.\n\nEach of the next n lines starts with integer t (1 \u2264 t \u2264 6) \u2014 the type of the event.\n\nAn integer s (1 \u2264 s \u2264 300) follows in the query of the first and the third type (if it is the query of first type, then it's new speed of Polycarp's car, if it is the query of third type, then it's new speed limit).\n\nIt is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).\n\n\n-----Output-----\n\nPrint the minimal number of road signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view.\n\n\n-----Examples-----\nInput\n11\n1 100\n3 70\n4\n2\n3 120\n5\n3 120\n6\n1 150\n4\n3 300\n\nOutput\n2\n\nInput\n5\n1 100\n3 200\n2\n4\n5\n\nOutput\n0\n\nInput\n7\n1 20\n2\n6\n4\n6\n6\n2\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example Polycarp should say he didn't notice the \"speed limit\" sign with the limit of 70 and the second \"speed limit\" sign with the limit of 120.\n\nIn the second example Polycarp didn't make any rule violation.\n\nIn the third example Polycarp should say he didn't notice both \"no overtake allowed\" that came after \"overtake is allowed\" sign."} +{"id": "02133", "text": "Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph.\n\nThere are n vertices in the tree, each of them is painted black or white. Anton doesn't like multicolored trees, so he wants to change the tree such that all vertices have the same color (black or white).\n\nTo change the colors Anton can use only operations of one type. We denote it as paint(v), where v is some vertex of the tree. This operation changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color (including v and u). For example, consider the tree [Image] \n\nand apply operation paint(3) to get the following: [Image] \n\nAnton is interested in the minimum number of operation he needs to perform in order to make the colors of all vertices equal.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the number of vertices in the tree.\n\nThe second line contains n integers color_{i} (0 \u2264 color_{i} \u2264 1)\u00a0\u2014 colors of the vertices. color_{i} = 0 means that the i-th vertex is initially painted white, while color_{i} = 1 means it's initially painted black.\n\nThen follow n - 1 line, each of them contains a pair of integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i})\u00a0\u2014 indices of vertices connected by the corresponding edge. It's guaranteed that all pairs (u_{i}, v_{i}) are distinct, i.e. there are no multiple edges.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the minimum number of operations Anton has to apply in order to make all vertices of the tree black or all vertices of the tree white.\n\n\n-----Examples-----\nInput\n11\n0 0 0 1 1 0 1 0 0 1 1\n1 2\n1 3\n2 4\n2 5\n5 6\n5 7\n3 8\n3 9\n3 10\n9 11\n\nOutput\n2\n\nInput\n4\n0 0 0 0\n1 2\n2 3\n3 4\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, the tree is the same as on the picture. If we first apply operation paint(3) and then apply paint(6), the tree will become completely black, so the answer is 2.\n\nIn the second sample, the tree is already white, so there is no need to apply any operations and the answer is 0."} +{"id": "02134", "text": "Marcin is a coach in his university. There are $n$ students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other.\n\nLet's focus on the students. They are indexed with integers from $1$ to $n$. Each of them can be described with two integers $a_i$ and $b_i$; $b_i$ is equal to the skill level of the $i$-th student (the higher, the better). Also, there are $60$ known algorithms, which are numbered with integers from $0$ to $59$. If the $i$-th student knows the $j$-th algorithm, then the $j$-th bit ($2^j$) is set in the binary representation of $a_i$. Otherwise, this bit is not set.\n\nStudent $x$ thinks that he is better than student $y$ if and only if $x$ knows some algorithm which $y$ doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group.\n\nMarcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\leq n \\leq 7000$) \u2014 the number of students interested in the camp.\n\nThe second line contains $n$ integers. The $i$-th of them is $a_i$ ($0 \\leq a_i < 2^{60}$).\n\nThe third line contains $n$ integers. The $i$-th of them is $b_i$ ($1 \\leq b_i \\leq 10^9$).\n\n\n-----Output-----\n\nOutput one integer which denotes the maximum sum of $b_i$ over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0.\n\n\n-----Examples-----\nInput\n4\n3 2 3 6\n2 8 5 10\n\nOutput\n15\n\nInput\n3\n1 2 3\n1 2 3\n\nOutput\n0\n\nInput\n1\n0\n1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of $b_i$.\n\nIn the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset."} +{"id": "02135", "text": "They say \"years are like dominoes, tumbling one after the other\". But would a year fit into a grid? I don't think so.\n\nLimak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right.\n\nAlso, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid.\n\nLimak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle?\n\n\n-----Input-----\n\nThe first line of the input contains two integers h and w (1 \u2264 h, w \u2264 500)\u00a0\u2013 the number of rows and the number of columns, respectively.\n\nThe next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#'\u00a0\u2014 denoting an empty or forbidden cell, respectively.\n\nThe next line contains a single integer q (1 \u2264 q \u2264 100 000)\u00a0\u2014 the number of queries.\n\nEach of the next q lines contains four integers r1_{i}, c1_{i}, r2_{i}, c2_{i} (1 \u2264 r1_{i} \u2264 r2_{i} \u2264 h, 1 \u2264 c1_{i} \u2264 c2_{i} \u2264 w)\u00a0\u2014 the i-th query. Numbers r1_{i} and c1_{i} denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2_{i} and c2_{i} denote the row and the column (respectively) of the bottom right cell of the rectangle.\n\n\n-----Output-----\n\nPrint q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.\n\n\n-----Examples-----\nInput\n5 8\n....#..#\n.#......\n##.#....\n##..#.##\n........\n4\n1 1 2 3\n4 1 4 1\n1 2 4 5\n2 5 5 8\n\nOutput\n4\n0\n10\n15\n\nInput\n7 39\n.......................................\n.###..###..#..###.....###..###..#..###.\n...#..#.#..#..#.........#..#.#..#..#...\n.###..#.#..#..###.....###..#.#..#..###.\n.#....#.#..#....#.....#....#.#..#..#.#.\n.###..###..#..###.....###..###..#..###.\n.......................................\n6\n1 1 3 20\n2 10 6 30\n2 10 7 30\n2 2 7 7\n1 7 7 7\n1 8 7 8\n\nOutput\n53\n89\n120\n23\n0\n2\n\n\n\n-----Note-----\n\nA red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. [Image]"} +{"id": "02136", "text": "Pink Floyd are pulling a prank on Roger Waters. They know he doesn't like walls, he wants to be able to walk freely, so they are blocking him from exiting his room which can be seen as a grid.\n\nRoger Waters has a square grid of size $n\\times n$ and he wants to traverse his grid from the upper left ($1,1$) corner to the lower right corner ($n,n$). Waters can move from a square to any other square adjacent by a side, as long as he is still in the grid. Also except for the cells ($1,1$) and ($n,n$) every cell has a value $0$ or $1$ in it.\n\nBefore starting his traversal he will pick either a $0$ or a $1$ and will be able to only go to cells values in which are equal to the digit he chose. The starting and finishing cells ($1,1$) and ($n,n$) are exempt from this rule, he may go through them regardless of picked digit. Because of this the cell ($1,1$) takes value the letter 'S' and the cell ($n,n$) takes value the letter 'F'.\n\nFor example, in the first example test case, he can go from ($1, 1$) to ($n, n$) by using the zeroes on this path: ($1, 1$), ($2, 1$), ($2, 2$), ($2, 3$), ($3, 3$), ($3, 4$), ($4, 4$)\n\nThe rest of the band (Pink Floyd) wants Waters to not be able to do his traversal, so while he is not looking they will invert at most two cells in the grid (from $0$ to $1$ or vice versa). They are afraid they will not be quick enough and asked for your help in choosing the cells. Note that you cannot invert cells $(1, 1)$ and $(n, n)$.\n\nWe can show that there always exists a solution for the given constraints.\n\nAlso note that Waters will pick his digit of the traversal after the band has changed his grid, so he must not be able to reach ($n,n$) no matter what digit he picks.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 50$). Description of the test cases follows.\n\nThe first line of each test case contains one integers $n$ ($3 \\le n \\le 200$).\n\nThe following $n$ lines of each test case contain the binary grid, square ($1, 1$) being colored in 'S' and square ($n, n$) being colored in 'F'.\n\nThe sum of values of $n$ doesn't exceed $200$.\n\n\n-----Output-----\n\nFor each test case output on the first line an integer $c$ ($0 \\le c \\le 2$) \u00a0\u2014 the number of inverted cells.\n\nIn $i$-th of the following $c$ lines, print the coordinates of the $i$-th cell you inverted. You may not invert the same cell twice. Note that you cannot invert cells $(1, 1)$ and $(n, n)$.\n\n\n-----Example-----\nInput\n3\n4\nS010\n0001\n1000\n111F\n3\nS10\n101\n01F\n5\nS0101\n00000\n01111\n11111\n0001F\n\nOutput\n1\n3 4\n2\n1 2\n2 1\n0\n\n\n\n-----Note-----\n\nFor the first test case, after inverting the cell, we get the following grid:\n\n\n\nS010\n\n0001\n\n1001\n\n111F"} +{"id": "02137", "text": "Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.\n\nThere are $n$ ghosts in the universe, they move in the $OXY$ plane, each one of them has its own velocity that does not change in time: $\\overrightarrow{V} = V_{x}\\overrightarrow{i} + V_{y}\\overrightarrow{j}$ where $V_{x}$ is its speed on the $x$-axis and $V_{y}$ is on the $y$-axis.\n\nA ghost $i$ has experience value $EX_i$, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.\n\nAs the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind $GX = \\sum_{i=1}^{n} EX_i$ will never increase.\n\nTameem is a red giant, he took a picture of the cartesian plane at a certain moment of time $T$, and magically all the ghosts were aligned on a line of the form $y = a \\cdot x + b$. You have to compute what will be the experience index of the ghost kind $GX$ in the indefinite future, this is your task for today.\n\nNote that when Tameem took the picture, $GX$ may already be greater than $0$, because many ghosts may have scared one another at any moment between $[-\\infty, T]$.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $a$ and $b$ ($1 \\leq n \\leq 200000$, $1 \\leq |a| \\leq 10^9$, $0 \\le |b| \\le 10^9$)\u00a0\u2014 the number of ghosts in the universe and the parameters of the straight line.\n\nEach of the next $n$ lines contains three integers $x_i$, $V_{xi}$, $V_{yi}$ ($-10^9 \\leq x_i \\leq 10^9$, $-10^9 \\leq V_{x i}, V_{y i} \\leq 10^9$), where $x_i$ is the current $x$-coordinate of the $i$-th ghost (and $y_i = a \\cdot x_i + b$).\n\nIt is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all $(i,j)$ $x_i \\neq x_j$ for $i \\ne j$.\n\n\n-----Output-----\n\nOutput one line: experience index of the ghost kind $GX$ in the indefinite future.\n\n\n-----Examples-----\nInput\n4 1 1\n1 -1 -1\n2 1 1\n3 1 1\n4 -1 -1\n\nOutput\n8\n\nInput\n3 1 0\n-1 1 0\n0 0 -1\n1 -1 -2\n\nOutput\n6\n\nInput\n3 1 0\n0 0 0\n1 0 0\n2 0 0\n\nOutput\n0\n\n\n\n-----Note-----\n\nThere are four collisions $(1,2,T-0.5)$, $(1,3,T-1)$, $(2,4,T+1)$, $(3,4,T+0.5)$, where $(u,v,t)$ means a collision happened between ghosts $u$ and $v$ at moment $t$. At each collision, each ghost gained one experience point, this means that $GX = 4 \\cdot 2 = 8$.\n\nIn the second test, all points will collide when $t = T + 1$. [Image] \n\nThe red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity."} +{"id": "02138", "text": "You are given a sequence of n positive integers d_1, d_2, ..., d_{n} (d_1 < d_2 < ... < d_{n}). Your task is to construct an undirected graph such that:\n\n there are exactly d_{n} + 1 vertices; there are no self-loops; there are no multiple edges; there are no more than 10^6 edges; its degree set is equal to d. \n\nVertices should be numbered 1 through (d_{n} + 1).\n\nDegree sequence is an array a with length equal to the number of vertices in a graph such that a_{i} is the number of vertices adjacent to i-th vertex.\n\nDegree set is a sorted in increasing order sequence of all distinct values from the degree sequence.\n\nIt is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 10^6 edges.\n\nPrint the resulting graph.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 300) \u2014 the size of the degree set.\n\nThe second line contains n integers d_1, d_2, ..., d_{n} (1 \u2264 d_{i} \u2264 1000, d_1 < d_2 < ... < d_{n}) \u2014 the degree set.\n\n\n-----Output-----\n\nIn the first line print one integer m (1 \u2264 m \u2264 10^6) \u2014 the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 10^6 edges.\n\nEach of the next m lines should contain two integers v_{i} and u_{i} (1 \u2264 v_{i}, u_{i} \u2264 d_{n} + 1) \u2014 the description of the i-th edge.\n\n\n-----Examples-----\nInput\n3\n2 3 4\n\nOutput\n8\n3 1\n4 2\n4 5\n2 5\n5 1\n3 2\n2 1\n5 3\n\nInput\n3\n1 2 3\n\nOutput\n4\n1 2\n1 3\n1 4\n2 3"} +{"id": "02139", "text": "The bear has a string s = s_1s_2... s_{|}s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 \u2264 i \u2264 j \u2264 |s|), that string x(i, j) = s_{i}s_{i} + 1... s_{j} contains at least one string \"bear\" as a substring.\n\nString x(i, j) contains string \"bear\", if there is such index k (i \u2264 k \u2264 j - 3), that s_{k} = b, s_{k} + 1 = e, s_{k} + 2 = a, s_{k} + 3 = r.\n\nHelp the bear cope with the given problem.\n\n\n-----Input-----\n\nThe first line contains a non-empty string s (1 \u2264 |s| \u2264 5000). It is guaranteed that the string only consists of lowercase English letters.\n\n\n-----Output-----\n\nPrint a single number \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\nbearbtear\n\nOutput\n6\n\nInput\nbearaabearc\n\nOutput\n20\n\n\n\n-----Note-----\n\nIn the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9).\n\nIn the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11)."} +{"id": "02140", "text": "Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.\n\nPasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string\u00a0\u2014\u00a0each day he chose integer a_{i} and reversed a piece of string (a segment) from position a_{i} to position |s| - a_{i} + 1. It is guaranteed that 2\u00b7a_{i} \u2264 |s|.\n\nYou face the following task: determine what Pasha's string will look like after m days.\n\n\n-----Input-----\n\nThe first line of the input contains Pasha's string s of length from 2 to 2\u00b710^5 characters, consisting of lowercase Latin letters.\n\nThe second line contains a single integer m (1 \u2264 m \u2264 10^5)\u00a0\u2014\u00a0 the number of days when Pasha changed his string.\n\nThe third line contains m space-separated elements a_{i} (1 \u2264 a_{i}; 2\u00b7a_{i} \u2264 |s|)\u00a0\u2014\u00a0the position from which Pasha started transforming the string on the i-th day.\n\n\n-----Output-----\n\nIn the first line of the output print what Pasha's string s will look like after m days.\n\n\n-----Examples-----\nInput\nabcdef\n1\n2\n\nOutput\naedcbf\n\nInput\nvwxyz\n2\n2 2\n\nOutput\nvwxyz\n\nInput\nabcdef\n3\n1 2 3\n\nOutput\nfbdcea"} +{"id": "02141", "text": "You are given a chess board with $n$ rows and $n$ columns. Initially all cells of the board are empty, and you have to put a white or a black knight into each cell of the board.\n\nA knight is a chess piece that can attack a piece in cell ($x_2$, $y_2$) from the cell ($x_1$, $y_1$) if one of the following conditions is met:\n\n $|x_1 - x_2| = 2$ and $|y_1 - y_2| = 1$, or $|x_1 - x_2| = 1$ and $|y_1 - y_2| = 2$. \n\nHere are some examples of which cells knight can attack. In each of the following pictures, if the knight is currently in the blue cell, it can attack all red cells (and only them).\n\n [Image] \n\nA duel of knights is a pair of knights of different colors such that these knights attack each other. You have to put a knight (a white one or a black one) into each cell in such a way that the number of duels is maximum possible.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($3 \\le n \\le 100$) \u2014 the number of rows (and columns) in the board.\n\n\n-----Output-----\n\nPrint $n$ lines with $n$ characters in each line. The $j$-th character in the $i$-th line should be W, if the cell ($i$, $j$) contains a white knight, or B, if it contains a black knight. The number of duels should be maximum possible. If there are multiple optimal answers, print any of them.\n\n\n-----Example-----\nInput\n3\n\nOutput\nWBW\nBBB\nWBW\n\n\n\n-----Note-----\n\nIn the first example, there are $8$ duels:\n\n the white knight in ($1$, $1$) attacks the black knight in ($3$, $2$); the white knight in ($1$, $1$) attacks the black knight in ($2$, $3$); the white knight in ($1$, $3$) attacks the black knight in ($3$, $2$); the white knight in ($1$, $3$) attacks the black knight in ($2$, $1$); the white knight in ($3$, $1$) attacks the black knight in ($1$, $2$); the white knight in ($3$, $1$) attacks the black knight in ($2$, $3$); the white knight in ($3$, $3$) attacks the black knight in ($1$, $2$); the white knight in ($3$, $3$) attacks the black knight in ($2$, $1$)."} +{"id": "02142", "text": "You are given two arrays of integers $a_1,\\ldots,a_n$ and $b_1,\\ldots,b_m$.\n\nYour task is to find a non-empty array $c_1,\\ldots,c_k$ that is a subsequence of $a_1,\\ldots,a_n$, and also a subsequence of $b_1,\\ldots,b_m$. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it.\n\nA sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) elements. For example, $[3,1]$ is a subsequence of $[3,2,1]$ and $[4,3,1]$, but not a subsequence of $[1,3,3,7]$ and $[3,10,4]$.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1\\le t\\le 1000$) \u00a0\u2014 the number of test cases. Next $3t$ lines contain descriptions of test cases.\n\nThe first line of each test case contains two integers $n$ and $m$ ($1\\le n,m\\le 1000$) \u00a0\u2014 the lengths of the two arrays.\n\nThe second line of each test case contains $n$ integers $a_1,\\ldots,a_n$ ($1\\le a_i\\le 1000$) \u00a0\u2014 the elements of the first array.\n\nThe third line of each test case contains $m$ integers $b_1,\\ldots,b_m$ ($1\\le b_i\\le 1000$) \u00a0\u2014 the elements of the second array.\n\nIt is guaranteed that the sum of $n$ and the sum of $m$ across all test cases does not exceed $1000$ ($\\sum\\limits_{i=1}^t n_i, \\sum\\limits_{i=1}^t m_i\\le 1000$).\n\n\n-----Output-----\n\nFor each test case, output \"YES\" if a solution exists, or \"NO\" otherwise.\n\nIf the answer is \"YES\", on the next line output an integer $k$ ($1\\le k\\le 1000$) \u00a0\u2014 the length of the array, followed by $k$ integers $c_1,\\ldots,c_k$ ($1\\le c_i\\le 1000$) \u00a0\u2014 the elements of the array.\n\nIf there are multiple solutions with the smallest possible $k$, output any.\n\n\n-----Example-----\nInput\n5\n4 5\n10 8 6 4\n1 2 3 4 5\n1 1\n3\n3\n1 1\n3\n2\n5 3\n1000 2 2 2 3\n3 1 5\n5 5\n1 2 3 4 5\n1 2 3 4 5\n\nOutput\nYES\n1 4\nYES\n1 3\nNO\nYES\n1 3\nYES\n1 2\n\n\n\n-----Note-----\n\nIn the first test case, $[4]$ is a subsequence of $[10, 8, 6, 4]$ and $[1, 2, 3, 4, 5]$. This array has length $1$, it is the smallest possible length of a subsequence of both $a$ and $b$.\n\nIn the third test case, no non-empty subsequences of both $[3]$ and $[2]$ exist, so the answer is \"NO\"."} +{"id": "02143", "text": "Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest children in that age to code. That is why he decided to give each child two sweets.\n\nMike has $n$ sweets with sizes $a_1, a_2, \\ldots, a_n$. All his sweets have different sizes. That is, there is no such pair $(i, j)$ ($1 \\leq i, j \\leq n$) such that $i \\ne j$ and $a_i = a_j$.\n\nSince Mike has taught for many years, he knows that if he gives two sweets with sizes $a_i$ and $a_j$ to one child and $a_k$ and $a_p$ to another, where $(a_i + a_j) \\neq (a_k + a_p)$, then a child who has a smaller sum of sizes will be upset. That is, if there are two children who have different sums of sweets, then one of them will be upset. Apparently, Mike does not want somebody to be upset. \n\nMike wants to invite children giving each of them two sweets. Obviously, he can't give one sweet to two or more children. His goal is to invite as many children as he can. \n\nSince Mike is busy preparing to his first lecture in the elementary school, he is asking you to find the maximum number of children he can invite giving each of them two sweets in such way that nobody will be upset.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\leq n \\leq 1\\,000$)\u00a0\u2014 the number of sweets Mike has.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^5$)\u00a0\u2014 the sizes of the sweets. It is guaranteed that all integers are distinct.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the maximum number of children Mike can invite giving each of them two sweets in such way that nobody will be upset.\n\n\n-----Examples-----\nInput\n8\n1 8 3 11 4 9 2 7\n\nOutput\n3\n\nInput\n7\n3 1 7 11 9 2 12\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example, Mike can give $9+2=11$ to one child, $8+3=11$ to another one, and $7+4=11$ to the third child. Therefore, Mike can invite three children. Note that it is not the only solution.\n\nIn the second example, Mike can give $3+9=12$ to one child and $1+11$ to another one. Therefore, Mike can invite two children. Note that it is not the only solution."} +{"id": "02144", "text": "You are given two integers $a$ and $m$. Calculate the number of integers $x$ such that $0 \\le x < m$ and $\\gcd(a, m) = \\gcd(a + x, m)$.\n\nNote: $\\gcd(a, b)$ is the greatest common divisor of $a$ and $b$.\n\n\n-----Input-----\n\nThe first line contains the single integer $T$ ($1 \\le T \\le 50$) \u2014 the number of test cases.\n\nNext $T$ lines contain test cases \u2014 one per line. Each line contains two integers $a$ and $m$ ($1 \\le a < m \\le 10^{10}$).\n\n\n-----Output-----\n\nPrint $T$ integers \u2014 one per test case. For each test case print the number of appropriate $x$-s.\n\n\n-----Example-----\nInput\n3\n4 9\n5 10\n42 9999999967\n\nOutput\n6\n1\n9999999966\n\n\n\n-----Note-----\n\nIn the first test case appropriate $x$-s are $[0, 1, 3, 4, 6, 7]$.\n\nIn the second test case the only appropriate $x$ is $0$."} +{"id": "02145", "text": "Recently Petya walked in the forest and found a magic stick.\n\nSince Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $a$ is even, then the spell will turn it into $\\frac{3a}{2}$; If the chosen number $a$ is greater than one, then the spell will turn it into $a-1$. \n\nNote that if the number is even and greater than one, then Petya can choose which spell to apply.\n\nPetya now has only one number $x$. He wants to know if his favorite number $y$ can be obtained from $x$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $x$ as it is.\n\n\n-----Input-----\n\nThe first line contains single integer $T$ ($1 \\le T \\le 10^4$) \u2014 the number of test cases. Each test case consists of two lines.\n\nThe first line of each test case contains two integers $x$ and $y$ ($1 \\le x, y \\le 10^9$) \u2014 the current number and the number that Petya wants to get.\n\n\n-----Output-----\n\nFor the $i$-th test case print the answer on it \u2014 YES if Petya can get the number $y$ from the number $x$ using known spells, and NO otherwise.\n\nYou may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).\n\n\n-----Example-----\nInput\n7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234\n\nOutput\nYES\nYES\nNO\nYES\nNO\nYES\nYES"} +{"id": "02146", "text": "Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.\n\nCity consists of n intersections numbered from 1 to n. Mike starts walking from his house located at the intersection number 1 and goes along some sequence of intersections. Walking from intersection number i to intersection j requires |i - j| units of energy. The total energy spent by Mike to visit a sequence of intersections p_1 = 1, p_2, ..., p_{k} is equal to $\\sum_{i = 1}^{k - 1}|p_{i} - p_{i + 1}$ units of energy.\n\nOf course, walking would be boring if there were no shortcuts. A shortcut is a special path that allows Mike walking from one intersection to another requiring only 1 unit of energy. There are exactly n shortcuts in Mike's city, the i^{th} of them allows walking from intersection i to intersection a_{i} (i \u2264 a_{i} \u2264 a_{i} + 1) (but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence p_1 = 1, p_2, ..., p_{k} then for each 1 \u2264 i < k satisfying p_{i} + 1 = a_{p}_{i} and a_{p}_{i} \u2260 p_{i} Mike will spend only 1 unit of energy instead of |p_{i} - p_{i} + 1| walking from the intersection p_{i} to intersection p_{i} + 1. For example, if Mike chooses a sequence p_1 = 1, p_2 = a_{p}_1, p_3 = a_{p}_2, ..., p_{k} = a_{p}_{k} - 1, he spends exactly k - 1 units of total energy walking around them.\n\nBefore going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each 1 \u2264 i \u2264 n Mike is interested in finding minimum possible total energy of some sequence p_1 = 1, p_2, ..., p_{k} = i.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 200 000)\u00a0\u2014 the number of Mike's city intersection.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (i \u2264 a_{i} \u2264 n , $a_{i} \\leq a_{i + 1} \\forall i < n)$, describing shortcuts of Mike's city, allowing to walk from intersection i to intersection a_{i} using only 1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from a_{i} to i).\n\n\n-----Output-----\n\nIn the only line print n integers m_1, m_2, ..., m_{n}, where m_{i} denotes the least amount of total energy required to walk from intersection 1 to intersection i.\n\n\n-----Examples-----\nInput\n3\n2 2 3\n\nOutput\n0 1 2 \n\nInput\n5\n1 2 3 4 5\n\nOutput\n0 1 2 3 4 \n\nInput\n7\n4 4 4 4 7 7 7\n\nOutput\n0 1 2 1 2 3 3 \n\n\n\n-----Note-----\n\nIn the first sample case desired sequences are:\n\n1: 1; m_1 = 0;\n\n2: 1, 2; m_2 = 1;\n\n3: 1, 3; m_3 = |3 - 1| = 2.\n\nIn the second sample case the sequence for any intersection 1 < i is always 1, i and m_{i} = |1 - i|.\n\nIn the third sample case\u00a0\u2014 consider the following intersection sequences:\n\n1: 1; m_1 = 0;\n\n2: 1, 2; m_2 = |2 - 1| = 1;\n\n3: 1, 4, 3; m_3 = 1 + |4 - 3| = 2;\n\n4: 1, 4; m_4 = 1;\n\n5: 1, 4, 5; m_5 = 1 + |4 - 5| = 2;\n\n6: 1, 4, 6; m_6 = 1 + |4 - 6| = 3;\n\n7: 1, 4, 5, 7; m_7 = 1 + |4 - 5| + 1 = 3."} +{"id": "02147", "text": "A Large Software Company develops its own social network. Analysts have found that during the holidays, major sporting events and other significant events users begin to enter the network more frequently, resulting in great load increase on the infrastructure.\n\nAs part of this task, we assume that the social network is 4n processes running on the n servers. All servers are absolutely identical machines, each of which has a volume of RAM of 1 GB = 1024 MB ^{(1)}. Each process takes 100 MB of RAM on the server. At the same time, the needs of maintaining the viability of the server takes about 100 more megabytes of RAM. Thus, each server may have up to 9 different processes of social network.\n\nNow each of the n servers is running exactly 4 processes. However, at the moment of peak load it is sometimes necessary to replicate the existing 4n processes by creating 8n new processes instead of the old ones. More formally, there is a set of replication rules, the i-th (1 \u2264 i \u2264 4n) of which has the form of a_{i} \u2192 (b_{i}, c_{i}), where a_{i}, b_{i} and c_{i} (1 \u2264 a_{i}, b_{i}, c_{i} \u2264 n) are the numbers of servers. This means that instead of an old process running on server a_{i}, there should appear two new copies of the process running on servers b_{i} and c_{i}. The two new replicated processes can be on the same server (i.e., b_{i} may be equal to c_{i}) or even on the same server where the original process was (i.e. a_{i} may be equal to b_{i} or c_{i}). During the implementation of the rule a_{i} \u2192 (b_{i}, c_{i}) first the process from the server a_{i} is destroyed, then appears a process on the server b_{i}, then appears a process on the server c_{i}.\n\nThere is a set of 4n rules, destroying all the original 4n processes from n servers, and creating after their application 8n replicated processes, besides, on each of the n servers will be exactly 8 processes. However, the rules can only be applied consecutively, and therefore the amount of RAM of the servers imposes limitations on the procedure for the application of the rules.\n\nAccording to this set of rules determine the order in which you want to apply all the 4n rules so that at any given time the memory of each of the servers contained at most 9 processes (old and new together), or tell that it is impossible.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 30 000) \u2014 the number of servers of the social network.\n\nNext 4n lines contain the rules of replicating processes, the i-th (1 \u2264 i \u2264 4n) of these lines as form a_{i}, b_{i}, c_{i} (1 \u2264 a_{i}, b_{i}, c_{i} \u2264 n) and describes rule a_{i} \u2192 (b_{i}, c_{i}).\n\nIt is guaranteed that each number of a server from 1 to n occurs four times in the set of all a_{i}, and eight times among a set that unites all b_{i} and c_{i}.\n\n\n-----Output-----\n\nIf the required order of performing rules does not exist, print \"NO\" (without the quotes).\n\nOtherwise, print in the first line \"YES\" (without the quotes), and in the second line \u2014 a sequence of 4n numbers from 1 to 4n, giving the numbers of the rules in the order they are applied. The sequence should be a permutation, that is, include each number from 1 to 4n exactly once.\n\nIf there are multiple possible variants, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n2\n1 2 2\n1 2 2\n1 2 2\n1 2 2\n2 1 1\n2 1 1\n2 1 1\n2 1 1\n\nOutput\nYES\n1 2 5 6 3 7 4 8\n\nInput\n3\n1 2 3\n1 1 1\n1 1 1\n1 1 1\n2 1 3\n2 2 2\n2 2 2\n2 2 2\n3 1 2\n3 3 3\n3 3 3\n3 3 3\n\nOutput\nYES\n2 3 4 6 7 8 10 11 12 1 5 9\n\n\n\n-----Note-----\n\n^{(1)} To be extremely accurate, we should note that the amount of server memory is 1 GiB = 1024 MiB and processes require 100 MiB RAM where a gibibyte (GiB) is the amount of RAM of 2^30 bytes and a mebibyte (MiB) is the amount of RAM of 2^20 bytes.\n\nIn the first sample test the network uses two servers, each of which initially has four launched processes. In accordance with the rules of replication, each of the processes must be destroyed and twice run on another server. One of the possible answers is given in the statement: after applying rules 1 and 2 the first server will have 2 old running processes, and the second server will have 8 (4 old and 4 new) processes. After we apply rules 5 and 6, both servers will have 6 running processes (2 old and 4 new). After we apply rules 3 and 7, both servers will have 7 running processes (1 old and 6 new), and after we apply rules 4 and 8, each server will have 8 running processes. At no time the number of processes on a single server exceeds 9.\n\nIn the second sample test the network uses three servers. On each server, three processes are replicated into two processes on the same server, and the fourth one is replicated in one process for each of the two remaining servers. As a result of applying rules 2, 3, 4, 6, 7, 8, 10, 11, 12 each server would have 7 processes (6 old and 1 new), as a result of applying rules 1, 5, 9 each server will have 8 processes. At no time the number of processes on a single server exceeds 9."} +{"id": "02148", "text": "Carol is currently curling.\n\nShe has n disks each with radius r on the 2D plane. \n\nInitially she has all these disks above the line y = 10^100.\n\nShe then will slide the disks towards the line y = 0 one by one in order from 1 to n. \n\nWhen she slides the i-th disk, she will place its center at the point (x_{i}, 10^100). She will then push it so the disk\u2019s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. \n\nCompute the y-coordinates of centers of all the disks after all disks have been pushed.\n\n\n-----Input-----\n\nThe first line will contain two integers n and r (1 \u2264 n, r \u2264 1 000), the number of disks, and the radius of the disks, respectively.\n\nThe next line will contain n integers x_1, x_2, ..., x_{n} (1 \u2264 x_{i} \u2264 1 000)\u00a0\u2014 the x-coordinates of the disks.\n\n\n-----Output-----\n\nPrint a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10^{ - 6}.\n\nNamely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$ for all coordinates.\n\n\n-----Example-----\nInput\n6 2\n5 5 6 8 3 12\n\nOutput\n2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613\n\n\n\n-----Note-----\n\nThe final positions of the disks will look as follows: [Image] \n\nIn particular, note the position of the last disk."} +{"id": "02149", "text": "Your program fails again. This time it gets \"Wrong answer on test 233\".\n\nThis is the easier version of the problem. In this version $1 \\le n \\le 2000$. You can hack this problem only if you solve and lock both problems.\n\nThe problem is about a test containing $n$ one-choice-questions. Each of the questions contains $k$ options, and only one of them is correct. The answer to the $i$-th question is $h_{i}$, and if your answer of the question $i$ is $h_{i}$, you earn $1$ point, otherwise, you earn $0$ points for this question. The values $h_1, h_2, \\dots, h_n$ are known to you in this problem.\n\nHowever, you have a mistake in your program. It moves the answer clockwise! Consider all the $n$ answers are written in a circle. Due to the mistake in your program, they are shifted by one cyclically.\n\nFormally, the mistake moves the answer for the question $i$ to the question $i \\bmod n + 1$. So it moves the answer for the question $1$ to question $2$, the answer for the question $2$ to the question $3$, ..., the answer for the question $n$ to the question $1$.\n\nWe call all the $n$ answers together an answer suit. There are $k^n$ possible answer suits in total.\n\nYou're wondering, how many answer suits satisfy the following condition: after moving clockwise by $1$, the total number of points of the new answer suit is strictly larger than the number of points of the old one. You need to find the answer modulo $998\\,244\\,353$.\n\nFor example, if $n = 5$, and your answer suit is $a=[1,2,3,4,5]$, it will submitted as $a'=[5,1,2,3,4]$ because of a mistake. If the correct answer suit is $h=[5,2,2,3,4]$, the answer suit $a$ earns $1$ point and the answer suite $a'$ earns $4$ points. Since $4 > 1$, the answer suit $a=[1,2,3,4,5]$ should be counted.\n\n\n-----Input-----\n\nThe first line contains two integers $n$, $k$ ($1 \\le n \\le 2000$, $1 \\le k \\le 10^9$)\u00a0\u2014 the number of questions and the number of possible answers to each question.\n\nThe following line contains $n$ integers $h_1, h_2, \\dots, h_n$, ($1 \\le h_{i} \\le k)$\u00a0\u2014 answers to the questions.\n\n\n-----Output-----\n\nOutput one integer: the number of answers suits satisfying the given condition, modulo $998\\,244\\,353$.\n\n\n-----Examples-----\nInput\n3 3\n1 3 1\n\nOutput\n9\n\nInput\n5 5\n1 1 4 2 2\n\nOutput\n1000\n\n\n\n-----Note-----\n\nFor the first example, valid answer suits are $[2,1,1], [2,1,2], [2,1,3], [3,1,1], [3,1,2], [3,1,3], [3,2,1], [3,2,2], [3,2,3]$."} +{"id": "02150", "text": "Alicia has an array, $a_1, a_2, \\ldots, a_n$, of non-negative integers. For each $1 \\leq i \\leq n$, she has found a non-negative integer $x_i = max(0, a_1, \\ldots, a_{i-1})$. Note that for $i=1$, $x_i = 0$.\n\nFor example, if Alicia had the array $a = \\{0, 1, 2, 0, 3\\}$, then $x = \\{0, 0, 1, 2, 2\\}$.\n\nThen, she calculated an array, $b_1, b_2, \\ldots, b_n$: $b_i = a_i - x_i$.\n\nFor example, if Alicia had the array $a = \\{0, 1, 2, 0, 3\\}$, $b = \\{0-0, 1-0, 2-1, 0-2, 3-2\\} = \\{0, 1, 1, -2, 1\\}$.\n\nAlicia gives you the values $b_1, b_2, \\ldots, b_n$ and asks you to restore the values $a_1, a_2, \\ldots, a_n$. Can you help her solve the problem?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($3 \\leq n \\leq 200\\,000$)\u00a0\u2013 the number of elements in Alicia's array.\n\nThe next line contains $n$ integers, $b_1, b_2, \\ldots, b_n$ ($-10^9 \\leq b_i \\leq 10^9$).\n\nIt is guaranteed that for the given array $b$ there is a solution $a_1, a_2, \\ldots, a_n$, for all elements of which the following is true: $0 \\leq a_i \\leq 10^9$.\n\n\n-----Output-----\n\nPrint $n$ integers, $a_1, a_2, \\ldots, a_n$ ($0 \\leq a_i \\leq 10^9$), such that if you calculate $x$ according to the statement, $b_1$ will be equal to $a_1 - x_1$, $b_2$ will be equal to $a_2 - x_2$, ..., and $b_n$ will be equal to $a_n - x_n$.\n\nIt is guaranteed that there exists at least one solution for the given tests. It can be shown that the solution is unique.\n\n\n-----Examples-----\nInput\n5\n0 1 1 -2 1\n\nOutput\n0 1 2 0 3 \nInput\n3\n1000 999999000 -1000000000\n\nOutput\n1000 1000000000 0 \nInput\n5\n2 1 2 2 3\n\nOutput\n2 3 5 7 10 \n\n\n-----Note-----\n\nThe first test was described in the problem statement.\n\nIn the second test, if Alicia had an array $a = \\{1000, 1000000000, 0\\}$, then $x = \\{0, 1000, 1000000000\\}$ and $b = \\{1000-0, 1000000000-1000, 0-1000000000\\} = \\{1000, 999999000, -1000000000\\}$."} +{"id": "02151", "text": "You are given a sequence $s$ consisting of $n$ digits from $1$ to $9$.\n\nYou have to divide it into at least two segments (segment \u2014 is a consecutive sequence of elements) (in other words, you have to place separators between some digits of the sequence) in such a way that each element belongs to exactly one segment and if the resulting division will be represented as an integer numbers sequence then each next element of this sequence will be strictly greater than the previous one.\n\nMore formally: if the resulting division of the sequence is $t_1, t_2, \\dots, t_k$, where $k$ is the number of element in a division, then for each $i$ from $1$ to $k-1$ the condition $t_{i} < t_{i + 1}$ (using numerical comparing, it means that the integer representations of strings are compared) should be satisfied.\n\nFor example, if $s=654$ then you can divide it into parts $[6, 54]$ and it will be suitable division. But if you will divide it into parts $[65, 4]$ then it will be bad division because $65 > 4$. If $s=123$ then you can divide it into parts $[1, 23]$, $[1, 2, 3]$ but not into parts $[12, 3]$.\n\nYour task is to find any suitable division for each of the $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 300$) \u2014 the number of queries.\n\nThe first line of the $i$-th query contains one integer number $n_i$ ($2 \\le n_i \\le 300$) \u2014 the number of digits in the $i$-th query.\n\nThe second line of the $i$-th query contains one string $s_i$ of length $n_i$ consisting only of digits from $1$ to $9$.\n\n\n-----Output-----\n\nIf the sequence of digits in the $i$-th query cannot be divided into at least two parts in a way described in the problem statement, print the single line \"NO\" for this query.\n\nOtherwise in the first line of the answer to this query print \"YES\", on the second line print $k_i$ \u2014 the number of parts in your division of the $i$-th query sequence and in the third line print $k_i$ strings $t_{i, 1}, t_{i, 2}, \\dots, t_{i, k_i}$ \u2014 your division. Parts should be printed in order of the initial string digits. It means that if you write the parts one after another without changing their order then you'll get the string $s_i$.\n\nSee examples for better understanding.\n\n\n-----Example-----\nInput\n4\n6\n654321\n4\n1337\n2\n33\n4\n2122\n\nOutput\nYES\n3\n6 54 321\nYES\n3\n1 3 37\nNO\nYES\n2\n21 22"} +{"id": "02152", "text": "Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly a_{i} kilograms of meat. [Image] \n\nThere is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for p_{i} dollars per kilogram. Malek knows all numbers a_1, ..., a_{n} and p_1, ..., p_{n}. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.\n\nMalek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days. \n\n\n-----Input-----\n\nThe first line of input contains integer n (1 \u2264 n \u2264 10^5), the number of days.\n\nIn the next n lines, i-th line contains two integers a_{i} and p_{i} (1 \u2264 a_{i}, p_{i} \u2264 100), the amount of meat Duff needs and the cost of meat in that day.\n\n\n-----Output-----\n\nPrint the minimum money needed to keep Duff happy for n days, in one line.\n\n\n-----Examples-----\nInput\n3\n1 3\n2 2\n3 1\n\nOutput\n10\n\nInput\n3\n1 3\n2 1\n3 2\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.\n\nIn the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day."} +{"id": "02153", "text": "There are $n$ beautiful skyscrapers in New York, the height of the $i$-th one is $h_i$. Today some villains have set on fire first $n - 1$ of them, and now the only safety building is $n$-th skyscraper.\n\nLet's call a jump from $i$-th skyscraper to $j$-th ($i < j$) discrete, if all skyscrapers between are strictly lower or higher than both of them. Formally, jump is discrete, if $i < j$ and one of the following conditions satisfied: $i + 1 = j$ $\\max(h_{i + 1}, \\ldots, h_{j - 1}) < \\min(h_i, h_j)$ $\\max(h_i, h_j) < \\min(h_{i + 1}, \\ldots, h_{j - 1})$. \n\nAt the moment, Vasya is staying on the first skyscraper and wants to live a little longer, so his goal is to reach $n$-th skyscraper with minimal count of discrete jumps. Help him with calcualting this number.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 3 \\cdot 10^5$) \u2014 total amount of skyscrapers.\n\nThe second line contains $n$ integers $h_1, h_2, \\ldots, h_n$ ($1 \\le h_i \\le 10^9$) \u2014 heights of skyscrapers.\n\n\n-----Output-----\n\nPrint single number $k$ \u2014 minimal amount of discrete jumps. We can show that an answer always exists.\n\n\n-----Examples-----\nInput\n5\n1 3 1 4 5\n\nOutput\n3\nInput\n4\n4 2 2 4\n\nOutput\n1\nInput\n2\n1 1\n\nOutput\n1\nInput\n5\n100 1 100 1 100\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first testcase, Vasya can jump in the following way: $1 \\rightarrow 2 \\rightarrow 4 \\rightarrow 5$.\n\nIn the second and third testcases, we can reach last skyscraper in one jump.\n\nSequence of jumps in the fourth testcase: $1 \\rightarrow 3 \\rightarrow 5$."} +{"id": "02154", "text": "You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.\n\n\n-----Input-----\n\nInput begins with an integer N (2 \u2264 N \u2264 3\u00b710^5), the number of days.\n\nFollowing this is a line with exactly N integers p_1, p_2, ..., p_{N} (1 \u2264 p_{i} \u2264 10^6). The price of one share of stock on the i-th day is given by p_{i}.\n\n\n-----Output-----\n\nPrint the maximum amount of money you can end up with at the end of N days.\n\n\n-----Examples-----\nInput\n9\n10 5 4 7 9 12 6 2 10\n\nOutput\n20\n\nInput\n20\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4\n\nOutput\n41\n\n\n\n-----Note-----\n\nIn the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20."} +{"id": "02155", "text": "Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.\n\nSonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.\n\nThe Manhattan distance between two cells ($x_1$, $y_1$) and ($x_2$, $y_2$) is defined as $|x_1 - x_2| + |y_1 - y_2|$. For example, the Manhattan distance between the cells $(5, 2)$ and $(7, 1)$ equals to $|5-7|+|2-1|=3$. [Image] Example of a rhombic matrix. \n\nNote that rhombic matrices are uniquely defined by $n$, $m$, and the coordinates of the cell containing the zero.\n\nShe drew a $n\\times m$ rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of $n\\cdot m$ numbers). Note that Sonya will not give you $n$ and $m$, so only the sequence of numbers in this matrix will be at your disposal.\n\nWrite a program that finds such an $n\\times m$ rhombic matrix whose elements are the same as the elements in the sequence in some order.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1\\leq t\\leq 10^6$)\u00a0\u2014 the number of cells in the matrix.\n\nThe second line contains $t$ integers $a_1, a_2, \\ldots, a_t$ ($0\\leq a_i< t$)\u00a0\u2014 the values in the cells in arbitrary order.\n\n\n-----Output-----\n\nIn the first line, print two positive integers $n$ and $m$ ($n \\times m = t$)\u00a0\u2014 the size of the matrix.\n\nIn the second line, print two integers $x$ and $y$ ($1\\leq x\\leq n$, $1\\leq y\\leq m$)\u00a0\u2014 the row number and the column number where the cell with $0$ is located.\n\nIf there are multiple possible answers, print any of them. If there is no solution, print the single integer $-1$.\n\n\n-----Examples-----\nInput\n20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4\n\nOutput\n4 5\n2 2\n\nInput\n18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1\n\nOutput\n3 6\n2 3\n\nInput\n6\n2 1 0 2 1 2\n\nOutput\n-1\n\n\n\n-----Note-----\n\nYou can see the solution to the first example in the legend. You also can choose the cell $(2, 2)$ for the cell where $0$ is located. You also can choose a $5\\times 4$ matrix with zero at $(4, 2)$.\n\nIn the second example, there is a $3\\times 6$ matrix, where the zero is located at $(2, 3)$ there.\n\nIn the third example, a solution does not exist."} +{"id": "02156", "text": "Consider a sequence of digits of length $2^k$ $[a_1, a_2, \\ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\\bmod 10$ for $0\\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$.\n\nLess formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers).\n\nPerform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \\ldots, a_{2^k}])$ denote the number of candies we get in this process. \n\nFor example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then:\n\nAfter the first operation the sequence becomes $[(8 + 7)\\bmod 10, (3 + 1)\\bmod 10, (7 + 0)\\bmod 10, (9 + 4)\\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \\ge 10$ and $9 + 4 \\ge 10$.\n\nAfter the second operation the sequence becomes $[(5 + 4)\\bmod 10, (7 + 3)\\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \\ge 10$. \n\nAfter the final operation sequence becomes $[(9 + 0) \\bmod 10]$ $=$ $[9]$. \n\nTherefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total.\n\nYou are given a sequence of digits of length $n$ $s_1, s_2, \\ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \\ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the length of the sequence.\n\nThe second line contains $n$ digits $s_1, s_2, \\ldots, s_n$ ($0 \\le s_i \\le 9$).\n\nThe third line contains a single integer $q$ ($1 \\le q \\le 10^5$)\u00a0\u2014 the number of queries.\n\nEach of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \\le l_i \\le r_i \\le n$)\u00a0\u2014 $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$.\n\n\n-----Output-----\n\nOutput $q$ lines, in $i$-th line output single integer\u00a0\u2014 $f([s_{l_i}, s_{l_i + 1}, \\ldots, s_{r_i}])$, answer to the $i$-th query.\n\n\n-----Examples-----\nInput\n8\n8 7 3 1 7 0 9 4\n3\n1 8\n2 5\n7 7\n\nOutput\n3\n1\n0\n\nInput\n6\n0 1 2 3 3 5\n3\n1 2\n1 4\n3 6\n\nOutput\n0\n0\n1\n\n\n\n-----Note-----\n\nThe first example illustrates an example from the statement.\n\n$f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \\to [(7 + 3)\\bmod 10, (1 + 7)\\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \\ge 10$ $\\to$ $[(0 + 8) \\bmod 10]$ $=$ $[8]$, so we get only $1$ candy.\n\n$f([9]) = 0$ as we don't perform operations with it."} +{"id": "02157", "text": "The little girl loves the problems on array queries very much.\n\nOne day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive.\n\nThe little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n (1 \u2264 n \u2264 2\u00b710^5) and q (1 \u2264 q \u2264 2\u00b710^5) \u2014 the number of elements in the array and the number of queries, correspondingly.\n\nThe next line contains n space-separated integers a_{i} (1 \u2264 a_{i} \u2264 2\u00b710^5) \u2014 the array elements.\n\nEach of the following q lines contains two space-separated integers l_{i} and r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n) \u2014 the i-th query.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the maximum sum of query replies after the array elements are reordered.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n3 3\n5 3 2\n1 2\n2 3\n1 3\n\nOutput\n25\n\nInput\n5 3\n5 2 4 1 3\n1 5\n2 3\n2 3\n\nOutput\n33"} +{"id": "02158", "text": "Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: \"Send the fool further!\", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.\n\nHeidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?\n\nHeidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.\n\nJenny is given the number 0.\n\n\n-----Input-----\n\nThe first line of the input contains the number of friends n (3 \u2264 n \u2264 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 \u2264 u, v \u2264 n - 1, 1 \u2264 c \u2264 10^4), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.\n\nIt is guaranteed that the social network of the input forms a tree.\n\n\n-----Output-----\n\nOutput a single integer \u2013 the maximum sum of costs.\n\n\n-----Examples-----\nInput\n4\n0 1 4\n0 2 2\n2 3 3\n\nOutput\n5\n\nInput\n6\n1 2 3\n0 2 100\n1 4 2\n0 3 7\n3 5 10\n\nOutput\n105\n\nInput\n11\n1 0 1664\n2 0 881\n3 2 4670\n4 2 1555\n5 1 1870\n6 2 1265\n7 2 288\n8 7 2266\n9 2 1536\n10 6 3378\n\nOutput\n5551\n\n\n\n-----Note-----\n\nIn the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2)."} +{"id": "02159", "text": "Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color t_{i}.\n\nFor a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant.\n\nThere are $\\frac{n \\cdot(n + 1)}{2}$ non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 5000)\u00a0\u2014 the number of balls.\n\nThe second line contains n integers t_1, t_2, ..., t_{n} (1 \u2264 t_{i} \u2264 n) where t_{i} is the color of the i-th ball.\n\n\n-----Output-----\n\nPrint n integers. The i-th of them should be equal to the number of intervals where i is a dominant color.\n\n\n-----Examples-----\nInput\n4\n1 2 1 2\n\nOutput\n7 3 0 0 \n\nInput\n3\n1 1 1\n\nOutput\n6 0 0 \n\n\n\n-----Note-----\n\nIn the first sample, color 2 is dominant in three intervals: An interval [2, 2] contains one ball. This ball's color is 2 so it's clearly a dominant color. An interval [4, 4] contains one ball, with color 2 again. An interval [2, 4] contains two balls of color 2 and one ball of color 1. \n\nThere are 7 more intervals and color 1 is dominant in all of them."} +{"id": "02160", "text": "Alice and Bob are playing a game on a line with $n$ cells. There are $n$ cells labeled from $1$ through $n$. For each $i$ from $1$ to $n-1$, cells $i$ and $i+1$ are adjacent.\n\nAlice initially has a token on some cell on the line, and Bob tries to guess where it is. \n\nBob guesses a sequence of line cell numbers $x_1, x_2, \\ldots, x_k$ in order. In the $i$-th question, Bob asks Alice if her token is currently on cell $x_i$. That is, Alice can answer either \"YES\" or \"NO\" to each Bob's question.\n\nAt most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer \"NO\" to all of Bob's questions.\n\nNote that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.\n\nYou are given $n$ and Bob's questions $x_1, \\ldots, x_k$. You would like to count the number of scenarios that let Alice answer \"NO\" to all of Bob's questions. \n\nLet $(a,b)$ denote a scenario where Alice starts at cell $a$ and ends at cell $b$. Two scenarios $(a_i, b_i)$ and $(a_j, b_j)$ are different if $a_i \\neq a_j$ or $b_i \\neq b_j$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\leq n,k \\leq 10^5$)\u00a0\u2014 the number of cells and the number of questions Bob asked.\n\nThe second line contains $k$ integers $x_1, x_2, \\ldots, x_k$ ($1 \\leq x_i \\leq n$)\u00a0\u2014 Bob's questions.\n\n\n-----Output-----\n\nPrint a single integer, the number of scenarios that let Alice answer \"NO\" to all of Bob's questions.\n\n\n-----Examples-----\nInput\n5 3\n5 1 4\n\nOutput\n9\n\nInput\n4 8\n1 2 3 4 4 3 2 1\n\nOutput\n0\n\nInput\n100000 1\n42\n\nOutput\n299997\n\n\n\n-----Note-----\n\nThe notation $(i,j)$ denotes a scenario where Alice starts at cell $i$ and ends at cell $j$.\n\nIn the first example, the valid scenarios are $(1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5)$. For example, $(3,4)$ is valid since Alice can start at cell $3$, stay there for the first three questions, then move to cell $4$ after the last question. \n\n$(4,5)$ is valid since Alice can start at cell $4$, stay there for the first question, the move to cell $5$ for the next two questions. Note that $(4,5)$ is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.\n\nIn the second example, Alice has no valid scenarios.\n\nIn the last example, all $(i,j)$ where $|i-j| \\leq 1$ except for $(42, 42)$ are valid scenarios."} +{"id": "02161", "text": "Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.\n\nVasya decided to organize information about the phone numbers of friends. You will be given n strings \u2014 all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.\n\nVasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.\n\nThe task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.\n\nRead the examples to understand statement and format of the output better.\n\n\n-----Input-----\n\nFirst line contains the integer n (1 \u2264 n \u2264 20)\u00a0\u2014 number of entries in Vasya's phone books. \n\nThe following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.\n\n\n-----Output-----\n\nPrint out the ordered information about the phone numbers of Vasya's friends. First output m\u00a0\u2014 number of friends that are found in Vasya's phone books.\n\nThe following m lines must contain entries in the following format \"name number_of_phone_numbers phone_numbers\". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.\n\nEntries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.\n\n\n-----Examples-----\nInput\n2\nivan 1 00123\nmasha 1 00123\n\nOutput\n2\nmasha 1 00123 \nivan 1 00123 \n\nInput\n3\nkarl 2 612 12\npetr 1 12\nkatya 1 612\n\nOutput\n3\nkatya 1 612 \npetr 1 12 \nkarl 1 612 \n\nInput\n4\nivan 3 123 123 456\nivan 2 456 456\nivan 8 789 3 23 6 56 9 89 2\ndasha 2 23 789\n\nOutput\n2\ndasha 2 23 789 \nivan 4 789 123 2 456"} +{"id": "02162", "text": "A team of three programmers is going to play a contest. The contest consists of $n$ problems, numbered from $1$ to $n$. Each problem is printed on a separate sheet of paper. The participants have decided to divide the problem statements into three parts: the first programmer took some prefix of the statements (some number of first paper sheets), the third contestant took some suffix of the statements (some number of last paper sheets), and the second contestant took all remaining problems. But something went wrong \u2014 the statements were printed in the wrong order, so the contestants have received the problems in some random order.\n\nThe first contestant has received problems $a_{1, 1}, a_{1, 2}, \\dots, a_{1, k_1}$. The second one has received problems $a_{2, 1}, a_{2, 2}, \\dots, a_{2, k_2}$. The third one has received all remaining problems ($a_{3, 1}, a_{3, 2}, \\dots, a_{3, k_3}$).\n\nThe contestants don't want to play the contest before they redistribute the statements. They want to redistribute them so that the first contestant receives some prefix of the problemset, the third contestant receives some suffix of the problemset, and the second contestant receives all the remaining problems.\n\nDuring one move, some contestant may give one of their problems to other contestant. What is the minimum number of moves required to redistribute the problems?\n\nIt is possible that after redistribution some participant (or even two of them) will not have any problems.\n\n\n-----Input-----\n\nThe first line contains three integers $k_1, k_2$ and $k_3$ ($1 \\le k_1, k_2, k_3 \\le 2 \\cdot 10^5, k_1 + k_2 + k_3 \\le 2 \\cdot 10^5$) \u2014 the number of problems initially taken by the first, the second and the third participant, respectively.\n\nThe second line contains $k_1$ integers $a_{1, 1}, a_{1, 2}, \\dots, a_{1, k_1}$ \u2014 the problems initially taken by the first participant.\n\nThe third line contains $k_2$ integers $a_{2, 1}, a_{2, 2}, \\dots, a_{2, k_2}$ \u2014 the problems initially taken by the second participant.\n\nThe fourth line contains $k_3$ integers $a_{3, 1}, a_{3, 2}, \\dots, a_{3, k_3}$ \u2014 the problems initially taken by the third participant.\n\nIt is guaranteed that no problem has been taken by two (or three) participants, and each integer $a_{i, j}$ meets the condition $1 \\le a_{i, j} \\le n$, where $n = k_1 + k_2 + k_3$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of moves required to redistribute the problems so that the first participant gets the prefix of the problemset, the third participant gets the suffix of the problemset, and the second participant gets all of the remaining problems.\n\n\n-----Examples-----\nInput\n2 1 2\n3 1\n4\n2 5\n\nOutput\n1\n\nInput\n3 2 1\n3 2 1\n5 4\n6\n\nOutput\n0\n\nInput\n2 1 3\n5 6\n4\n1 2 3\n\nOutput\n3\n\nInput\n1 5 1\n6\n5 1 2 4 7\n3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example the third contestant should give the problem $2$ to the first contestant, so the first contestant has $3$ first problems, the third contestant has $1$ last problem, and the second contestant has $1$ remaining problem.\n\nIn the second example the distribution of problems is already valid: the first contestant has $3$ first problems, the third contestant has $1$ last problem, and the second contestant has $2$ remaining problems.\n\nThe best course of action in the third example is to give all problems to the third contestant.\n\nThe best course of action in the fourth example is to give all problems to the second contestant."} +{"id": "02163", "text": "For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence).\n\nYou are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe only line contains two integers n and m (1 \u2264 n, m \u2264 10^6) \u2014 the number of elements in arrays and the upper bound for elements.\n\n\n-----Output-----\n\nPrint the only integer c \u2014 the desired sum modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n1 3\n\nOutput\n6\n\nInput\n2 2\n\nOutput\n14\n\nInput\n3 3\n\nOutput\n174"} +{"id": "02164", "text": "This is the easy version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.\n\nYou are given a string $s$, consisting of lowercase English letters. Find the longest string, $t$, which satisfies the following conditions: The length of $t$ does not exceed the length of $s$. $t$ is a palindrome. There exists two strings $a$ and $b$ (possibly empty), such that $t = a + b$ ( \"$+$\" represents concatenation), and $a$ is prefix of $s$ while $b$ is suffix of $s$. \n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains a single integer $t$ ($1 \\leq t \\leq 1000$), the number of test cases. The next $t$ lines each describe a test case.\n\nEach test case is a non-empty string $s$, consisting of lowercase English letters.\n\nIt is guaranteed that the sum of lengths of strings over all test cases does not exceed $5000$.\n\n\n-----Output-----\n\nFor each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.\n\n\n-----Example-----\nInput\n5\na\nabcdfdcecba\nabbaxyzyx\ncodeforces\nacbba\n\nOutput\na\nabcdfdcba\nxyzyx\nc\nabba\n\n\n\n-----Note-----\n\nIn the first test, the string $s = $\"a\" satisfies all conditions.\n\nIn the second test, the string \"abcdfdcba\" satisfies all conditions, because: Its length is $9$, which does not exceed the length of the string $s$, which equals $11$. It is a palindrome. \"abcdfdcba\" $=$ \"abcdfdc\" $+$ \"ba\", and \"abcdfdc\" is a prefix of $s$ while \"ba\" is a suffix of $s$. \n\nIt can be proven that there does not exist a longer string which satisfies the conditions.\n\nIn the fourth test, the string \"c\" is correct, because \"c\" $=$ \"c\" $+$ \"\" and $a$ or $b$ can be empty. The other possible solution for this test is \"s\"."} +{"id": "02165", "text": "Consider a system of n water taps all pouring water into the same container. The i-th water tap can be set to deliver any amount of water from 0 to a_{i} ml per second (this amount may be a real number). The water delivered by i-th tap has temperature t_{i}.\n\nIf for every $i \\in [ 1, n ]$ you set i-th tap to deliver exactly x_{i} ml of water per second, then the resulting temperature of water will be $\\frac{\\sum_{i = 1}^{n} x_{i} t_{i}}{\\sum_{i = 1}^{n} x_{i}}$ (if $\\sum_{i = 1}^{n} x_{i} = 0$, then to avoid division by zero we state that the resulting water temperature is 0).\n\nYou have to set all the water taps in such a way that the resulting temperature is exactly T. What is the maximum amount of water you may get per second if its temperature has to be T?\n\n\n-----Input-----\n\nThe first line contains two integers n and T (1 \u2264 n \u2264 200000, 1 \u2264 T \u2264 10^6) \u2014 the number of water taps and the desired temperature of water, respectively.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^6) where a_{i} is the maximum amount of water i-th tap can deliver per second.\n\nThe third line contains n integers t_1, t_2, ..., t_{n} (1 \u2264 t_{i} \u2264 10^6) \u2014 the temperature of water each tap delivers.\n\n\n-----Output-----\n\nPrint the maximum possible amount of water with temperature exactly T you can get per second (if it is impossible to obtain water with such temperature, then the answer is considered to be 0).\n\nYour answer is considered correct if its absolute or relative error doesn't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n2 100\n3 10\n50 150\n\nOutput\n6.000000000000000\n\nInput\n3 9\n5 5 30\n6 6 10\n\nOutput\n40.000000000000000\n\nInput\n2 12\n1 3\n10 15\n\nOutput\n1.666666666666667"} +{"id": "02166", "text": "Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.\n\nThe girl finds an important permutation for the research. The permutation contains n distinct integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n). She replaces some of permutation elements with -1 value as a revenge. \n\nWhen Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element a_{k} which has value equal to k (a_{k} = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe first line contains integer n (2 \u2264 n \u2264 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1. \n\nIt's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.\n\n\n-----Output-----\n\nOutput a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n5\n-1 -1 4 3 -1\n\nOutput\n2\n\n\n\n-----Note-----\n\nFor the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point."} +{"id": "02167", "text": "Polycarpus has an array, consisting of n integers a_1, a_2, ..., a_{n}. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:\n\n he chooses two elements of the array a_{i}, a_{j} (i \u2260 j); he simultaneously increases number a_{i} by 1 and decreases number a_{j} by 1, that is, executes a_{i} = a_{i} + 1 and a_{j} = a_{j} - 1. \n\nThe given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times. \n\nNow he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the array size. The second line contains space-separated integers a_1, a_2, ..., a_{n} (|a_{i}| \u2264 10^4) \u2014 the original array.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.\n\n\n-----Examples-----\nInput\n2\n2 1\n\nOutput\n1\n\nInput\n3\n1 4 1\n\nOutput\n3"} +{"id": "02168", "text": "A conglomerate consists of $n$ companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.\n\nBut anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.\n\nTo fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.\n\nSure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$\u00a0\u2014 the number of companies in the conglomerate ($1 \\le n \\le 2 \\cdot 10^5$). Each of the next $n$ lines describes a company. \n\nA company description start with an integer $m_i$\u00a0\u2014 the number of its employees ($1 \\le m_i \\le 2 \\cdot 10^5$). Then $m_i$ integers follow: the salaries of the employees. All salaries are positive and do not exceed $10^9$. \n\nThe total number of employees in all companies does not exceed $2 \\cdot 10^5$. \n\n\n-----Output-----\n\nOutput a single integer \u2014 the minimal total increase of all employees that allows to merge all companies.\n\n\n-----Example-----\nInput\n3\n2 4 3\n2 2 1\n3 1 1 1\n\nOutput\n13\n\n\n\n-----Note-----\n\nOne of the optimal merging strategies is the following. First increase all salaries in the second company by $2$, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries $[4, 3, 4, 3]$ and $[1, 1, 1]$. To merge them, increase the salaries in the second of those by $3$. The total increase is $2 + 2 + 3 + 3 + 3 = 13$."} +{"id": "02169", "text": "We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).\nThe integers from 1 through H\u00d7W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.\nYou, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.\nYou now have to take Q practical tests of your ability as a magical girl.\nThe i-th test will be conducted as follows:\n - Initially, a piece is placed on the square where the integer L_i is written.\n - Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.\n - Here, it is guaranteed that R_i-L_i is a multiple of D.\nFor each test, find the sum of magic points consumed during that test.\n\n-----Constraints-----\n - 1 \\leq H,W \\leq 300\n - 1 \\leq D \\leq H\u00d7W\n - 1 \\leq A_{i,j} \\leq H\u00d7W\n - A_{i,j} \\neq A_{x,y} ((i,j) \\neq (x,y))\n - 1 \\leq Q \\leq 10^5\n - 1 \\leq L_i \\leq R_i \\leq H\u00d7W\n - (R_i-L_i) is a multiple of D.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W D\nA_{1,1} A_{1,2} ... A_{1,W}\n:\nA_{H,1} A_{H,2} ... A_{H,W}\nQ\nL_1 R_1\n:\nL_Q R_Q\n\n-----Output-----\nFor each test, print the sum of magic points consumed during that test.\nOutput should be in the order the tests are conducted.\n\n-----Sample Input-----\n3 3 2\n1 4 3\n2 5 7\n8 9 6\n1\n4 8\n\n-----Sample Output-----\n5\n\n - 4 is written in Square (1,2).\n - 6 is written in Square (3,3).\n - 8 is written in Square (3,1).\nThus, the sum of magic points consumed during the first test is (|3-1|+|3-2|)+(|3-3|+|1-3|)=5."} +{"id": "02170", "text": "Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \\cdots, A_{N} and B_1, B_2, \\cdots, B_{N}, that satisfy all of the following conditions:\n - A_i \\neq B_i, for every i such that 1\\leq i\\leq N.\n - A_i \\neq A_j and B_i \\neq B_j, for every (i, j) such that 1\\leq i < j\\leq N.\nSince the count can be enormous, print it modulo (10^9+7).\n\n-----Constraints-----\n - 1\\leq N \\leq M \\leq 5\\times10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\n\n-----Output-----\nPrint the count modulo (10^9+7).\n\n-----Sample Input-----\n2 2\n\n-----Sample Output-----\n2\n\nA_1=1,A_2=2,B_1=2,B_2=1 and A_1=2,A_2=1,B_1=1,B_2=2 satisfy the conditions."} +{"id": "02171", "text": "This morning Chef wants to jump a little. In a few minutes he will arrive at the point 0. Then he will perform a lot of jumps in such a sequence: 1-jump, 2-jump, 3-jump, 1-jump, 2-jump, 3-jump, 1-jump, and so on.\n1-jump means that if Chef is at the point x, he will jump to the point x+1. \n2-jump means that if Chef is at the point x, he will jump to the point x+2. \n3-jump means that if Chef is at the point x, he will jump to the point x+3. \nBefore the start Chef asks you: will he arrive at the point a after some number of jumps?\n\n-----Input-----\n\nThe first line contains a single integer a denoting the point Chef asks about. \n\n-----Output-----\nOutput \"yes\" without a quotes if Chef can arrive at point a or \"no\" without a quotes otherwise.\n\n-----Constraints-----\n\n- 0 \u2264 a \u2264 1018\n\n-----Example-----\nInput:\n0\n\nOutput:\nyes\n\nInput:\n1\n\nOutput:\nyes\n\nInput:\n2\n\nOutput:\nno\n\nInput:\n3\n\nOutput:\nyes\n\nInput:\n6\n\nOutput:\nyes\n\nInput:\n7\n\nOutput:\nyes\n\nInput:\n10\n\nOutput:\nno\n\n-----Explanation-----\nThe first reached points are: 0 (+1) 1 (+2) 3 (+3) 6 (+1) 7, and so on."} +{"id": "02172", "text": "You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.\n\nYou know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning.\n\nYou can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language.\n\nYou are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes.\n\n\n-----Input-----\n\nThe first line contains two integers, n and m (1 \u2264 n \u2264 3000, 1 \u2264 m \u2264 3000) \u2014 the number of words in the professor's lecture and the number of words in each of these languages.\n\nThe following m lines contain the words. The i-th line contains two strings a_{i}, b_{i} meaning that the word a_{i} belongs to the first language, the word b_{i} belongs to the second language, and these two words have the same meaning. It is guaranteed that no word occurs in both languages, and each word occurs in its language exactly once.\n\nThe next line contains n space-separated strings c_1, c_2, ..., c_{n} \u2014 the text of the lecture. It is guaranteed that each of the strings c_{i} belongs to the set of strings {a_1, a_2, ... a_{m}}.\n\nAll the strings in the input are non-empty, each consisting of no more than 10 lowercase English letters.\n\n\n-----Output-----\n\nOutput exactly n words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input.\n\n\n-----Examples-----\nInput\n4 3\ncodeforces codesecrof\ncontest round\nletter message\ncodeforces contest letter contest\n\nOutput\ncodeforces round letter round\n\nInput\n5 3\njoll wuqrd\neuzf un\nhbnyiyc rsoqqveh\nhbnyiyc joll joll euzf joll\n\nOutput\nhbnyiyc joll joll un joll"} +{"id": "02173", "text": "One very well-known internet resource site (let's call it X) has come up with a New Year adventure. Specifically, they decided to give ratings to all visitors.\n\nThere are n users on the site, for each user we know the rating value he wants to get as a New Year Present. We know that user i wants to get at least a_{i} rating units as a present.\n\nThe X site is administered by very creative and thrifty people. On the one hand, they want to give distinct ratings and on the other hand, the total sum of the ratings in the present must be as small as possible.\n\nHelp site X cope with the challenging task of rating distribution. Find the optimal distribution.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 3\u00b710^5) \u2014 the number of users on the site. The next line contains integer sequence a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint a sequence of integers b_1, b_2, ..., b_{n}. Number b_{i} means that user i gets b_{i} of rating as a present. The printed sequence must meet the problem conditions. \n\nIf there are multiple optimal solutions, print any of them.\n\n\n-----Examples-----\nInput\n3\n5 1 1\n\nOutput\n5 1 2\n\nInput\n1\n1000000000\n\nOutput\n1000000000"} +{"id": "02174", "text": "Permutation p is an ordered set of integers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as p_{i}. We'll call number n the size or the length of permutation p_1, p_2, ..., p_{n}.\n\nYou have a sequence of integers a_1, a_2, ..., a_{n}. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 3\u00b710^5) \u2014 the size of the sought permutation. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint a single number \u2014 the minimum number of moves.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n2\n3 0\n\nOutput\n2\n\nInput\n3\n-1 -1 2\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).\n\nIn the second sample you need 6 moves to build permutation (1, 3, 2)."} +{"id": "02175", "text": "There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is a_{i} liters. [Image] \n\nInitially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.\n\nYour task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries: Add x_{i} liters of water to the p_{i}-th vessel; Print the number of liters of water in the k_{i}-th vessel. \n\nWhen you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.\n\n\n-----Input-----\n\nThe first line contains integer n \u2014 the number of vessels (1 \u2264 n \u2264 2\u00b710^5). The second line contains n integers a_1, a_2, ..., a_{n} \u2014 the vessels' capacities (1 \u2264 a_{i} \u2264 10^9). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m \u2014 the number of queries (1 \u2264 m \u2264 2\u00b710^5). Each of the next m lines contains the description of one query. The query of the first type is represented as \"1\u00a0p_{i}\u00a0x_{i}\", the query of the second type is represented as \"2\u00a0k_{i}\" (1 \u2264 p_{i} \u2264 n, 1 \u2264 x_{i} \u2264 10^9, 1 \u2264 k_{i} \u2264 n).\n\n\n-----Output-----\n\nFor each query, print on a single line the number of liters of water in the corresponding vessel.\n\n\n-----Examples-----\nInput\n2\n5 10\n6\n1 1 4\n2 1\n1 2 5\n1 1 4\n2 1\n2 2\n\nOutput\n4\n5\n8\n\nInput\n3\n5 10 8\n6\n1 1 12\n2 2\n1 1 6\n1 3 2\n2 2\n2 3\n\nOutput\n7\n10\n5"} +{"id": "02176", "text": "You are given a sequence of $n$ pairs of integers: $(a_1, b_1), (a_2, b_2), \\dots , (a_n, b_n)$. This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: $s = [(1, 2), (3, 2), (3, 1)]$ is bad because the sequence of first elements is sorted: $[1, 3, 3]$; $s = [(1, 2), (3, 2), (1, 2)]$ is bad because the sequence of second elements is sorted: $[2, 2, 2]$; $s = [(1, 1), (2, 2), (3, 3)]$ is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; $s = [(1, 3), (3, 3), (2, 2)]$ is good because neither the sequence of first elements $([1, 3, 2])$ nor the sequence of second elements $([3, 3, 2])$ is sorted. \n\nCalculate the number of permutations of size $n$ such that after applying this permutation to the sequence $s$ it turns into a good sequence. \n\nA permutation $p$ of size $n$ is a sequence $p_1, p_2, \\dots , p_n$ consisting of $n$ distinct integers from $1$ to $n$ ($1 \\le p_i \\le n$). If you apply permutation $p_1, p_2, \\dots , p_n$ to the sequence $s_1, s_2, \\dots , s_n$ you get the sequence $s_{p_1}, s_{p_2}, \\dots , s_{p_n}$. For example, if $s = [(1, 2), (1, 3), (2, 3)]$ and $p = [2, 3, 1]$ then $s$ turns into $[(1, 3), (2, 3), (1, 2)]$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 3 \\cdot 10^5$).\n\nThe next $n$ lines contains description of sequence $s$. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \\le a_i, b_i \\le n$) \u2014 the first and second elements of $i$-th pair in the sequence.\n\nThe sequence $s$ may contain equal elements.\n\n\n-----Output-----\n\nPrint the number of permutations of size $n$ such that after applying this permutation to the sequence $s$ it turns into a good sequence. Print the answer modulo $998244353$ (a prime number).\n\n\n-----Examples-----\nInput\n3\n1 1\n2 2\n3 1\n\nOutput\n3\n\nInput\n4\n2 3\n2 2\n2 1\n2 4\n\nOutput\n0\n\nInput\n3\n1 1\n1 1\n2 3\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn first test case there are six permutations of size $3$: if $p = [1, 2, 3]$, then $s = [(1, 1), (2, 2), (3, 1)]$ \u2014 bad sequence (sorted by first elements); if $p = [1, 3, 2]$, then $s = [(1, 1), (3, 1), (2, 2)]$ \u2014 bad sequence (sorted by second elements); if $p = [2, 1, 3]$, then $s = [(2, 2), (1, 1), (3, 1)]$ \u2014 good sequence; if $p = [2, 3, 1]$, then $s = [(2, 2), (3, 1), (1, 1)]$ \u2014 good sequence; if $p = [3, 1, 2]$, then $s = [(3, 1), (1, 1), (2, 2)]$ \u2014 bad sequence (sorted by second elements); if $p = [3, 2, 1]$, then $s = [(3, 1), (2, 2), (1, 1)]$ \u2014 good sequence."} +{"id": "02177", "text": "You are given two integers $A$ and $B$, calculate the number of pairs $(a, b)$ such that $1 \\le a \\le A$, $1 \\le b \\le B$, and the equation $a \\cdot b + a + b = conc(a, b)$ is true; $conc(a, b)$ is the concatenation of $a$ and $b$ (for example, $conc(12, 23) = 1223$, $conc(100, 11) = 10011$). $a$ and $b$ should not contain leading zeroes.\n\n\n-----Input-----\n\nThe first line contains $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nEach test case contains two integers $A$ and $B$ $(1 \\le A, B \\le 10^9)$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of pairs $(a, b)$ such that $1 \\le a \\le A$, $1 \\le b \\le B$, and the equation $a \\cdot b + a + b = conc(a, b)$ is true.\n\n\n-----Example-----\nInput\n3\n1 11\n4 2\n191 31415926\n\nOutput\n1\n0\n1337\n\n\n\n-----Note-----\n\nThere is only one suitable pair in the first test case: $a = 1$, $b = 9$ ($1 + 9 + 1 \\cdot 9 = 19$)."} +{"id": "02178", "text": "Vasya has got $n$ books, numbered from $1$ to $n$, arranged in a stack. The topmost book has number $a_1$, the next one \u2014 $a_2$, and so on. The book at the bottom of the stack has number $a_n$. All numbers are distinct.\n\nVasya wants to move all the books to his backpack in $n$ steps. During $i$-th step he wants to move the book number $b_i$ into his backpack. If the book with number $b_i$ is in the stack, he takes this book and all the books above the book $b_i$, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order $[1, 2, 3]$ (book $1$ is the topmost), and Vasya moves the books in the order $[2, 1, 3]$, then during the first step he will move two books ($1$ and $2$), during the second step he will do nothing (since book $1$ is already in the backpack), and during the third step \u2014 one book (the book number $3$). Note that $b_1, b_2, \\dots, b_n$ are distinct.\n\nHelp Vasya! Tell him the number of books he will put into his backpack during each step.\n\n\n-----Input-----\n\nThe first line contains one integer $n~(1 \\le n \\le 2 \\cdot 10^5)$ \u2014 the number of books in the stack.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n~(1 \\le a_i \\le n)$ denoting the stack of books.\n\nThe third line contains $n$ integers $b_1, b_2, \\dots, b_n~(1 \\le b_i \\le n)$ denoting the steps Vasya is going to perform.\n\nAll numbers $a_1 \\dots a_n$ are distinct, the same goes for $b_1 \\dots b_n$.\n\n\n-----Output-----\n\nPrint $n$ integers. The $i$-th of them should be equal to the number of books Vasya moves to his backpack during the $i$-th step.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n2 1 3\n\nOutput\n2 0 1 \n\nInput\n5\n3 1 4 2 5\n4 5 1 3 2\n\nOutput\n3 2 0 0 0 \n\nInput\n6\n6 5 4 3 2 1\n6 5 3 4 2 1\n\nOutput\n1 1 2 0 1 1 \n\n\n\n-----Note-----\n\nThe first example is described in the statement.\n\nIn the second example, during the first step Vasya will move the books $[3, 1, 4]$. After that only books $2$ and $5$ remain in the stack ($2$ is above $5$). During the second step Vasya will take the books $2$ and $5$. After that the stack becomes empty, so during next steps Vasya won't move any books."} +{"id": "02179", "text": "Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.\n\nLet's assume that we are given a connected weighted undirected graph G = (V, E) (here V is the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graph G_1 = (V, E_1) that is a tree with the set of edges E_1 that is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to any vertex to G and to G_1 are the same. \n\nYou are given a connected weighted undirected graph G and vertex u. Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.\n\n\n-----Input-----\n\nThe first line contains two numbers, n and m (1 \u2264 n \u2264 3\u00b710^5, 0 \u2264 m \u2264 3\u00b710^5) \u2014 the number of vertices and edges of the graph, respectively.\n\nNext m lines contain three integers each, representing an edge \u2014 u_{i}, v_{i}, w_{i} \u2014 the numbers of vertices connected by an edge and the weight of the edge (u_{i} \u2260 v_{i}, 1 \u2264 w_{i} \u2264 10^9). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.\n\nThe last line of the input contains integer u (1 \u2264 u \u2264 n) \u2014 the number of the start vertex.\n\n\n-----Output-----\n\nIn the first line print the minimum total weight of the edges of the tree.\n\nIn the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n3 3\n1 2 1\n2 3 1\n1 3 2\n3\n\nOutput\n2\n1 2 \n\nInput\n4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4\n\nOutput\n4\n2 3 4 \n\n\n\n-----Note-----\n\nIn the first sample there are two possible shortest path trees:\n\n with edges 1 \u2013 3 and 2 \u2013 3 (the total weight is 3); with edges 1 \u2013 2 and 2 \u2013 3 (the total weight is 2); \n\nAnd, for example, a tree with edges 1 \u2013 2 and 1 \u2013 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1."} +{"id": "02180", "text": "Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x\u20131, y), (x, y + 1) and (x, y\u20131).\n\nIahub wants to know how many Coders can be placed on an n \u00d7 n chessboard, so that no Coder attacks any other Coder.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 1000).\n\n\n-----Output-----\n\nOn the first line print an integer, the maximum number of Coders that can be placed on the chessboard.\n\nOn each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.\n\nIf there are multiple correct answers, you can print any.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n2\nC.\n.C"} +{"id": "02181", "text": "Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0, 0) and the length of the side equals a meters. The sides of the square are parallel to coordinate axes.\n\nAs the length of the marathon race is very long, Valera needs to have extra drink during the race. The coach gives Valera a bottle of drink each d meters of the path. We know that Valera starts at the point with coordinates (0, 0) and runs counter-clockwise. That is, when Valera covers a meters, he reaches the point with coordinates (a, 0). We also know that the length of the marathon race equals nd + 0.5 meters. \n\nHelp Valera's coach determine where he should be located to help Valera. Specifically, determine the coordinates of Valera's positions when he covers d, 2\u00b7d, ..., n\u00b7d meters.\n\n\n-----Input-----\n\nThe first line contains two space-separated real numbers a and d (1 \u2264 a, d \u2264 10^5), given with precision till 4 decimal digits after the decimal point. Number a denotes the length of the square's side that describes the stadium. Number d shows that after each d meters Valera gets an extra drink.\n\nThe second line contains integer n (1 \u2264 n \u2264 10^5) showing that Valera needs an extra drink n times.\n\n\n-----Output-----\n\nPrint n lines, each line should contain two real numbers x_{i} and y_{i}, separated by a space. Numbers x_{i} and y_{i} in the i-th line mean that Valera is at point with coordinates (x_{i}, y_{i}) after he covers i\u00b7d meters. Your solution will be considered correct if the absolute or relative error doesn't exceed 10^{ - 4}.\n\nNote, that this problem have huge amount of output data. Please, do not use cout stream for output in this problem.\n\n\n-----Examples-----\nInput\n2 5\n2\n\nOutput\n1.0000000000 2.0000000000\n2.0000000000 0.0000000000\n\nInput\n4.147 2.8819\n6\n\nOutput\n2.8819000000 0.0000000000\n4.1470000000 1.6168000000\n3.7953000000 4.1470000000\n0.9134000000 4.1470000000\n0.0000000000 2.1785000000\n0.7034000000 0.0000000000"} +{"id": "02182", "text": "Bob is a competitive programmer. He wants to become red, and for that he needs a strict training regime. He went to the annual meeting of grandmasters and asked $n$ of them how much effort they needed to reach red.\n\n\"Oh, I just spent $x_i$ hours solving problems\", said the $i$-th of them. \n\nBob wants to train his math skills, so for each answer he wrote down the number of minutes ($60 \\cdot x_i$), thanked the grandmasters and went home. Bob could write numbers with leading zeroes \u2014 for example, if some grandmaster answered that he had spent $2$ hours, Bob could write $000120$ instead of $120$.\n\nAlice wanted to tease Bob and so she took the numbers Bob wrote down, and for each of them she did one of the following independently: rearranged its digits, or wrote a random number. \n\nThis way, Alice generated $n$ numbers, denoted $y_1$, ..., $y_n$.\n\nFor each of the numbers, help Bob determine whether $y_i$ can be a permutation of a number divisible by $60$ (possibly with leading zeroes).\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 418$)\u00a0\u2014 the number of grandmasters Bob asked.\n\nThen $n$ lines follow, the $i$-th of which contains a single integer $y_i$\u00a0\u2014 the number that Alice wrote down.\n\nEach of these numbers has between $2$ and $100$ digits '0' through '9'. They can contain leading zeroes.\n\n\n-----Output-----\n\nOutput $n$ lines.\n\nFor each $i$, output the following. If it is possible to rearrange the digits of $y_i$ such that the resulting number is divisible by $60$, output \"red\" (quotes for clarity). Otherwise, output \"cyan\".\n\n\n-----Example-----\nInput\n6\n603\n006\n205\n228\n1053\n0000000000000000000000000000000000000000000000\n\nOutput\nred\nred\ncyan\ncyan\ncyan\nred\n\n\n\n-----Note-----\n\nIn the first example, there is one rearrangement that yields a number divisible by $60$, and that is $360$.\n\nIn the second example, there are two solutions. One is $060$ and the second is $600$.\n\nIn the third example, there are $6$ possible rearrangments: $025$, $052$, $205$, $250$, $502$, $520$. None of these numbers is divisible by $60$.\n\nIn the fourth example, there are $3$ rearrangements: $228$, $282$, $822$.\n\nIn the fifth example, none of the $24$ rearrangements result in a number divisible by $60$.\n\nIn the sixth example, note that $000\\dots0$ is a valid solution."} +{"id": "02183", "text": "\u0422\u0440\u0438 \u0431\u0440\u0430\u0442\u0430 \u0434\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438\u0441\u044c \u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0435. \u041f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u0443\u0435\u043c \u0431\u0440\u0430\u0442\u044c\u0435\u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: \u043f\u0443\u0441\u0442\u044c \u0441\u0442\u0430\u0440\u0448\u0438\u0439 \u0431\u0440\u0430\u0442 \u0438\u043c\u0435\u0435\u0442 \u043d\u043e\u043c\u0435\u0440 1, \u0441\u0440\u0435\u0434\u043d\u0438\u0439 \u0431\u0440\u0430\u0442 \u0438\u043c\u0435\u0435\u0442 \u043d\u043e\u043c\u0435\u0440 2, \u0430 \u043c\u043b\u0430\u0434\u0448\u0438\u0439 \u0431\u0440\u0430\u0442\u00a0\u2014 \u043d\u043e\u043c\u0435\u0440 3. \n\n\u041a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u0441\u0442\u0440\u0435\u0447\u0438, \u043e\u0434\u0438\u043d \u0438\u0437 \u0431\u0440\u0430\u0442\u044c\u0435\u0432 \u043e\u043f\u043e\u0437\u0434\u0430\u043b. \u041f\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u043d\u043e\u043c\u0435\u0440\u0430\u043c \u0434\u0432\u0443\u0445 \u0431\u0440\u0430\u0442\u044c\u0435\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u0448\u043b\u0438 \u0432\u043e\u0432\u0440\u0435\u043c\u044f, \u0432\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u043e\u043c\u0435\u0440 \u043e\u043f\u043e\u0437\u0434\u0430\u0432\u0448\u0435\u0433\u043e \u0431\u0440\u0430\u0442\u0430.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0434\u0432\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 a \u0438 b (1 \u2264 a, b \u2264 3, a \u2260 b)\u00a0\u2014 \u043d\u043e\u043c\u0435\u0440\u0430 \u0431\u0440\u0430\u0442\u044c\u0435\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u0448\u043b\u0438 \u043d\u0430 \u0432\u0441\u0442\u0440\u0435\u0447\u0443 \u0432\u043e\u0432\u0440\u0435\u043c\u044f. \u041d\u043e\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u044b \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e\u00a0\u2014 \u043d\u043e\u043c\u0435\u0440 \u0431\u0440\u0430\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u043e\u0437\u0434\u0430\u043b \u043d\u0430 \u0432\u0441\u0442\u0440\u0435\u0447\u0443.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3 1\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n2"} +{"id": "02184", "text": "You are given a boolean function of three variables which is defined by its truth table. You need to find an expression of minimum length that equals to this function. The expression may consist of: Operation AND ('&', ASCII code 38) Operation OR ('|', ASCII code 124) Operation NOT ('!', ASCII code 33) Variables x, y and z (ASCII codes 120-122) Parentheses ('(', ASCII code 40, and ')', ASCII code 41) \n\nIf more than one expression of minimum length exists, you should find the lexicographically smallest one.\n\nOperations have standard priority. NOT has the highest priority, then AND goes, and OR has the lowest priority. The expression should satisfy the following grammar:\n\nE ::= E '|' T | T\n\nT ::= T '&' F | F\n\nF ::= '!' F | '(' E ')' | 'x' | 'y' | 'z'\n\n\n-----Input-----\n\nThe first line contains one integer n\u00a0\u2014 the number of functions in the input (1 \u2264 n \u2264 10 000).\n\nThe following n lines contain descriptions of functions, the i-th of them contains a string of length 8 that consists of digits 0 and 1\u00a0\u2014 the truth table of the i-th function. The digit on position j (0 \u2264 j < 8) equals to the value of the function in case of $x = \\lfloor \\frac{j}{4} \\rfloor$, $y = \\lfloor \\frac{j}{2} \\rfloor \\operatorname{mod} 2$ and $z = j \\operatorname{mod} 2$.\n\n\n-----Output-----\n\nYou should output n lines, the i-th line should contain the expression of minimum length which equals to the i-th function. If there is more than one such expression, output the lexicographically smallest of them. Expressions should satisfy the given grammar and shouldn't contain white spaces.\n\n\n-----Example-----\nInput\n4\n00110011\n00000111\n11110000\n00011111\n\nOutput\ny\n(y|z)&x\n!x\nx|y&z\n\n\n\n-----Note-----\n\nThe truth table for the second function:\n\n[Image]"} +{"id": "02185", "text": "You're given two arrays $a[1 \\dots n]$ and $b[1 \\dots n]$, both of the same length $n$.\n\nIn order to perform a push operation, you have to choose three integers $l, r, k$ satisfying $1 \\le l \\le r \\le n$ and $k > 0$. Then, you will add $k$ to elements $a_l, a_{l+1}, \\ldots, a_r$.\n\nFor example, if $a = [3, 7, 1, 4, 1, 2]$ and you choose $(l = 3, r = 5, k = 2)$, the array $a$ will become $[3, 7, \\underline{3, 6, 3}, 2]$.\n\nYou can do this operation at most once. Can you make array $a$ equal to array $b$?\n\n(We consider that $a = b$ if and only if, for every $1 \\le i \\le n$, $a_i = b_i$)\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 20$) \u2014 the number of test cases in the input.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 100\\ 000$) \u2014 the number of elements in each array.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 1000$).\n\nThe third line of each test case contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($1 \\le b_i \\le 1000$).\n\nIt is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case, output one line containing \"YES\" if it's possible to make arrays $a$ and $b$ equal by performing at most once the described operation or \"NO\" if it's impossible.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n4\n6\n3 7 1 4 1 2\n3 7 3 6 3 2\n5\n1 1 1 1 1\n1 2 1 3 1\n2\n42 42\n42 42\n1\n7\n6\n\nOutput\nYES\nNO\nYES\nNO\n\n\n\n-----Note-----\n\nThe first test case is described in the statement: we can perform a push operation with parameters $(l=3, r=5, k=2)$ to make $a$ equal to $b$.\n\nIn the second test case, we would need at least two operations to make $a$ equal to $b$.\n\nIn the third test case, arrays $a$ and $b$ are already equal.\n\nIn the fourth test case, it's impossible to make $a$ equal to $b$, because the integer $k$ has to be positive."} +{"id": "02186", "text": "Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: \"Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position\".\n\nWatto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.\n\n\n-----Input-----\n\nThe first line contains two non-negative numbers n and m (0 \u2264 n \u2264 3\u00b710^5, 0 \u2264 m \u2264 3\u00b710^5) \u2014 the number of the initial strings and the number of queries, respectively.\n\nNext follow n non-empty strings that are uploaded to the memory of the mechanism.\n\nNext follow m non-empty strings that are the queries to the mechanism.\n\nThe total length of lines in the input doesn't exceed 6\u00b710^5. Each line consists only of letters 'a', 'b', 'c'.\n\n\n-----Output-----\n\nFor each query print on a single line \"YES\" (without the quotes), if the memory of the mechanism contains the required string, otherwise print \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac\n\nOutput\nYES\nNO\nNO"} +{"id": "02187", "text": "Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.\n\nOmkar currently has $n$ supports arranged in a line, the $i$-th of which has height $a_i$. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In $1$ operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add $1$ to each of their heights. \n\nHelp Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!\n\nAn array $b$ is a subsegment of an array $c$ if $b$ can be obtained from $c$ by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.\n\nAn array $b_1, b_2, \\dots, b_n$ is called nondecreasing if $b_i\\le b_{i+1}$ for every $i$ from $1$ to $n-1$.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\leq t \\leq 100$). Description of the test cases follows.\n\nThe first line of each test case contains an integer $n$ ($1 \\leq n \\leq 2 \\cdot 10^5$)\u00a0\u2014 the number of supports Omkar has.\n\nThe second line of each test case contains $n$ integers $a_{1},a_{2},...,a_{n}$ $(0 \\leq a_{i} \\leq 10^9)$\u00a0\u2014 the heights of the supports.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case, output a single integer\u00a0\u2014 the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.\n\n\n-----Example-----\nInput\n3\n4\n5 3 2 5\n5\n1 2 3 5 3\n3\n1 1 1\n\nOutput\n3\n2\n0\n\n\n\n-----Note-----\n\nThe subarray with which Omkar performs the operation is bolded.\n\nIn the first test case:\n\nFirst operation:\n\n$[5, 3, \\textbf{2}, 5] \\to [5, 3, \\textbf{3}, 5]$\n\nSecond operation:\n\n$[5, \\textbf{3}, \\textbf{3}, 5] \\to [5, \\textbf{4}, \\textbf{4}, 5]$\n\nThird operation:\n\n$[5, \\textbf{4}, \\textbf{4}, 5] \\to [5, \\textbf{5}, \\textbf{5}, 5]$\n\nIn the third test case, the array is already nondecreasing, so Omkar does $0$ operations."} +{"id": "02188", "text": "You are given $n$ pairs of integers $(a_1, b_1), (a_2, b_2), \\ldots, (a_n, b_n)$. All of the integers in the pairs are distinct and are in the range from $1$ to $2 \\cdot n$ inclusive.\n\nLet's call a sequence of integers $x_1, x_2, \\ldots, x_{2k}$ good if either $x_1 < x_2 > x_3 < \\ldots < x_{2k-2} > x_{2k-1} < x_{2k}$, or $x_1 > x_2 < x_3 > \\ldots > x_{2k-2} < x_{2k-1} > x_{2k}$. \n\nYou need to choose a subset of distinct indices $i_1, i_2, \\ldots, i_t$ and their order in a way that if you write down all numbers from the pairs in a single sequence (the sequence would be $a_{i_1}, b_{i_1}, a_{i_2}, b_{i_2}, \\ldots, a_{i_t}, b_{i_t}$), this sequence is good.\n\nWhat is the largest subset of indices you can choose? You also need to construct the corresponding index sequence $i_1, i_2, \\ldots, i_t$.\n\n\n-----Input-----\n\nThe first line contains single integer $n$ ($2 \\leq n \\leq 3 \\cdot 10^5$)\u00a0\u2014 the number of pairs.\n\nEach of the next $n$ lines contain two numbers\u00a0\u2014 $a_i$ and $b_i$ ($1 \\le a_i, b_i \\le 2 \\cdot n$)\u00a0\u2014 the elements of the pairs.\n\nIt is guaranteed that all integers in the pairs are distinct, that is, every integer from $1$ to $2 \\cdot n$ is mentioned exactly once.\n\n\n-----Output-----\n\nIn the first line print a single integer $t$\u00a0\u2014 the number of pairs in the answer.\n\nThen print $t$ distinct integers $i_1, i_2, \\ldots, i_t$\u00a0\u2014 the indexes of pairs in the corresponding order.\n\n\n-----Examples-----\nInput\n5\n1 7\n6 4\n2 10\n9 8\n3 5\n\nOutput\n3\n1 5 3\n\nInput\n3\n5 4\n3 2\n6 1\n\nOutput\n3\n3 2 1\n\n\n\n-----Note-----\n\nThe final sequence in the first example is $1 < 7 > 3 < 5 > 2 < 10$.\n\nThe final sequence in the second example is $6 > 1 < 3 > 2 < 5 > 4$."} +{"id": "02189", "text": "You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected.\n\nYou should assign labels to all vertices in such a way that:\n\n Labels form a valid permutation of length n \u2014 an integer sequence such that each integer from 1 to n appears exactly once in it. If there exists an edge from vertex v to vertex u then label_{v} should be smaller than label_{u}. Permutation should be lexicographically smallest among all suitable. \n\nFind such sequence of labels to satisfy all the conditions.\n\n\n-----Input-----\n\nThe first line contains two integer numbers n, m (2 \u2264 n \u2264 10^5, 1 \u2264 m \u2264 10^5).\n\nNext m lines contain two integer numbers v and u (1 \u2264 v, u \u2264 n, v \u2260 u) \u2014 edges of the graph. Edges are directed, graph doesn't contain loops or multiple edges.\n\n\n-----Output-----\n\nPrint n numbers \u2014 lexicographically smallest correct permutation of labels of vertices.\n\n\n-----Examples-----\nInput\n3 3\n1 2\n1 3\n3 2\n\nOutput\n1 3 2 \n\nInput\n4 5\n3 1\n4 1\n2 3\n3 4\n2 4\n\nOutput\n4 1 2 3 \n\nInput\n5 4\n3 1\n2 1\n2 3\n4 5\n\nOutput\n3 1 2 4 5"} +{"id": "02190", "text": "You are given $n$ positive integers $a_1, \\ldots, a_n$, and an integer $k \\geq 2$. Count the number of pairs $i, j$ such that $1 \\leq i < j \\leq n$, and there exists an integer $x$ such that $a_i \\cdot a_j = x^k$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($2 \\leq n \\leq 10^5$, $2 \\leq k \\leq 100$).\n\nThe second line contains $n$ integers $a_1, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^5$).\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of suitable pairs.\n\n\n-----Example-----\nInput\n6 3\n1 3 9 8 24 1\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the sample case, the suitable pairs are: $a_1 \\cdot a_4 = 8 = 2^3$; $a_1 \\cdot a_6 = 1 = 1^3$; $a_2 \\cdot a_3 = 27 = 3^3$; $a_3 \\cdot a_5 = 216 = 6^3$; $a_4 \\cdot a_6 = 8 = 2^3$."} +{"id": "02191", "text": "Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won $x$ rounds in a row. For example, if Bob won five rounds in a row and $x = 2$, then two sets ends.\n\nYou know that Alice and Bob have already played $n$ rounds, and you know the results of some rounds. For each $x$ from $1$ to $n$, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins $x$ rounds in a row. It is possible that the last set is still not finished \u2014 in that case, you should not count it in the answer.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 10^6$)\u00a0\u2014 the number of rounds.\n\nThe second line contains one string $s$ of length $n$ \u2014 the descriptions of rounds. If the $i$-th element of the string is 0, then Alice won the $i$-th round; if it is 1, then Bob won the $i$-th round, and if it is ?, then you don't know who won the $i$-th round.\n\n\n-----Output-----\n\nIn the only line print $n$ integers. The $i$-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins $i$ rounds in a row.\n\n\n-----Examples-----\nInput\n6\n11?000\n\nOutput\n6 3 2 1 0 0 \n\nInput\n5\n01?01\n\nOutput\n5 1 0 0 0 \n\nInput\n12\n???1??????1?\n\nOutput\n12 6 4 3 2 2 1 1 1 1 1 1 \n\n\n\n-----Note-----\n\nLet's consider the first test case:\n\n if $x = 1$ and $s = 110000$ or $s = 111000$ then there are six finished sets; if $x = 2$ and $s = 110000$ then there are three finished sets; if $x = 3$ and $s = 111000$ then there are two finished sets; if $x = 4$ and $s = 110000$ then there is one finished set; if $x = 5$ then there are no finished sets; if $x = 6$ then there are no finished sets."} +{"id": "02192", "text": "Chubby Yang is studying linear equations right now. He came up with a nice problem. In the problem you are given an n \u00d7 n matrix W, consisting of integers, and you should find two n \u00d7 n matrices A and B, all the following conditions must hold: A_{ij} = A_{ji}, for all i, j (1 \u2264 i, j \u2264 n); B_{ij} = - B_{ji}, for all i, j (1 \u2264 i, j \u2264 n); W_{ij} = A_{ij} + B_{ij}, for all i, j (1 \u2264 i, j \u2264 n). \n\nCan you solve the problem?\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 170). Each of the following n lines contains n integers. The j-th integer in the i-th line is W_{ij} (0 \u2264 |W_{ij}| < 1717).\n\n\n-----Output-----\n\nThe first n lines must contain matrix A. The next n lines must contain matrix B. Print the matrices in the format equal to format of matrix W in input. It is guaranteed that the answer exists. If there are multiple answers, you are allowed to print any of them.\n\nThe answer will be considered correct if the absolute or relative error doesn't exceed 10^{ - 4}.\n\n\n-----Examples-----\nInput\n2\n1 4\n3 2\n\nOutput\n1.00000000 3.50000000\n3.50000000 2.00000000\n0.00000000 0.50000000\n-0.50000000 0.00000000\n\nInput\n3\n1 2 3\n4 5 6\n7 8 9\n\nOutput\n1.00000000 3.00000000 5.00000000\n3.00000000 5.00000000 7.00000000\n5.00000000 7.00000000 9.00000000\n0.00000000 -1.00000000 -2.00000000\n1.00000000 0.00000000 -1.00000000\n2.00000000 1.00000000 0.00000000"} +{"id": "02193", "text": "Egor is a famous Russian singer, rapper, actor and blogger, and finally he decided to give a concert in the sunny Republic of Dagestan.\n\nThere are $n$ cities in the republic, some of them are connected by $m$ directed roads without any additional conditions. In other words, road system of Dagestan represents an arbitrary directed graph. Egor will arrive to the city $1$, travel to the city $n$ by roads along some path, give a concert and fly away.\n\nAs any famous artist, Egor has lots of haters and too annoying fans, so he can travel only by safe roads. There are two types of the roads in Dagestan, black and white: black roads are safe at night only, and white roads \u2014 in the morning. Before the trip Egor's manager's going to make a schedule: for each city he'll specify it's color, black or white, and then if during the trip they visit some city, the only time they can leave it is determined by the city's color: night, if it's black, and morning, if it's white. After creating the schedule Egor chooses an available path from $1$ to $n$, and for security reasons it has to be the shortest possible.\n\nEgor's manager likes Dagestan very much and wants to stay here as long as possible, so he asks you to make such schedule that there would be no path from $1$ to $n$ or the shortest path's length would be greatest possible.\n\nA path is one city or a sequence of roads such that for every road (excluding the first one) the city this road goes from is equal to the city previous road goes into. Egor can move only along paths consisting of safe roads only. \n\nThe path length is equal to the number of roads in it. The shortest path in a graph is a path with smallest length.\n\n\n-----Input-----\n\nThe first line contains two integers $n$, $m$ ($1 \\leq n \\leq 500000$, $0 \\leq m \\leq 500000$) \u2014 the number of cities and the number of roads.\n\nThe $i$-th of next $m$ lines contains three integers \u2014 $u_i$, $v_i$ and $t_i$ ($1 \\leq u_i, v_i \\leq n$, $t_i \\in \\{0, 1\\}$) \u2014 numbers of cities connected by road and its type, respectively ($0$ \u2014 night road, $1$ \u2014 morning road).\n\n\n-----Output-----\n\nIn the first line output the length of the desired path (or $-1$, if it's possible to choose such schedule that there's no path from $1$ to $n$).\n\nIn the second line output the desired schedule \u2014 a string of $n$ digits, where $i$-th digit is $0$, if the $i$-th city is a night one, and $1$ if it's a morning one.\n\nIf there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n3 4\n1 2 0\n1 3 1\n2 3 0\n2 3 1\n\nOutput\n2\n011\nInput\n4 8\n1 1 0\n1 3 0\n1 3 1\n3 2 0\n2 1 0\n3 4 1\n2 4 0\n2 4 1\n\nOutput\n3\n1101\nInput\n5 10\n1 2 0\n1 3 1\n1 4 0\n2 3 0\n2 3 1\n2 5 0\n3 4 0\n3 4 1\n4 2 1\n4 5 0\n\nOutput\n-1\n11111\n\n\n-----Note-----\n\nFor the first sample, if we paint city $1$ white, the shortest path is $1 \\rightarrow 3$. Otherwise, it's $1 \\rightarrow 2 \\rightarrow 3$ regardless of other cities' colors.\n\nFor the second sample, we should paint city $3$ black, and there are both black and white roads going from $2$ to $4$. Note that there can be a road connecting a city with itself."} +{"id": "02194", "text": "You are given an array $a$ of length $2^n$. You should process $q$ queries on it. Each query has one of the following $4$ types: $Replace(x, k)$\u00a0\u2014 change $a_x$ to $k$; $Reverse(k)$\u00a0\u2014 reverse each subarray $[(i-1) \\cdot 2^k+1, i \\cdot 2^k]$ for all $i$ ($i \\ge 1$); $Swap(k)$\u00a0\u2014 swap subarrays $[(2i-2) \\cdot 2^k+1, (2i-1) \\cdot 2^k]$ and $[(2i-1) \\cdot 2^k+1, 2i \\cdot 2^k]$ for all $i$ ($i \\ge 1$); $Sum(l, r)$\u00a0\u2014 print the sum of the elements of subarray $[l, r]$. \n\nWrite a program that can quickly process given queries.\n\n\n-----Input-----\n\nThe first line contains two integers $n$, $q$ ($0 \\le n \\le 18$; $1 \\le q \\le 10^5$)\u00a0\u2014 the length of array $a$ and the number of queries.\n\nThe second line contains $2^n$ integers $a_1, a_2, \\ldots, a_{2^n}$ ($0 \\le a_i \\le 10^9$).\n\nNext $q$ lines contains queries\u00a0\u2014 one per line. Each query has one of $4$ types: \"$1$ $x$ $k$\" ($1 \\le x \\le 2^n$; $0 \\le k \\le 10^9$)\u00a0\u2014 $Replace(x, k)$; \"$2$ $k$\" ($0 \\le k \\le n$)\u00a0\u2014 $Reverse(k)$; \"$3$ $k$\" ($0 \\le k < n$)\u00a0\u2014 $Swap(k)$; \"$4$ $l$ $r$\" ($1 \\le l \\le r \\le 2^n$)\u00a0\u2014 $Sum(l, r)$. \n\nIt is guaranteed that there is at least one $Sum$ query.\n\n\n-----Output-----\n\nPrint the answer for each $Sum$ query.\n\n\n-----Examples-----\nInput\n2 3\n7 4 9 9\n1 2 8\n3 1\n4 2 4\n\nOutput\n24\n\nInput\n3 8\n7 0 8 8 7 1 5 2\n4 3 7\n2 1\n3 2\n4 1 6\n2 3\n1 5 16\n4 8 8\n3 0\n\nOutput\n29\n22\n1\n\n\n\n-----Note-----\n\nIn the first sample, initially, the array $a$ is equal to $\\{7,4,9,9\\}$.\n\nAfter processing the first query. the array $a$ becomes $\\{7,8,9,9\\}$.\n\nAfter processing the second query, the array $a_i$ becomes $\\{9,9,7,8\\}$\n\nTherefore, the answer to the third query is $9+7+8=24$.\n\nIn the second sample, initially, the array $a$ is equal to $\\{7,0,8,8,7,1,5,2\\}$. What happens next is: $Sum(3, 7)$ $\\to$ $8 + 8 + 7 + 1 + 5 = 29$; $Reverse(1)$ $\\to$ $\\{0,7,8,8,1,7,2,5\\}$; $Swap(2)$ $\\to$ $\\{1,7,2,5,0,7,8,8\\}$; $Sum(1, 6)$ $\\to$ $1 + 7 + 2 + 5 + 0 + 7 = 22$; $Reverse(3)$ $\\to$ $\\{8,8,7,0,5,2,7,1\\}$; $Replace(5, 16)$ $\\to$ $\\{8,8,7,0,16,2,7,1\\}$; $Sum(8, 8)$ $\\to$ $1$; $Swap(0)$ $\\to$ $\\{8,8,0,7,2,16,1,7\\}$."} +{"id": "02195", "text": "You are given two integers $x$ and $y$. You can perform two types of operations: Pay $a$ dollars and increase or decrease any of these integers by $1$. For example, if $x = 0$ and $y = 7$ there are four possible outcomes after this operation: $x = 0$, $y = 6$; $x = 0$, $y = 8$; $x = -1$, $y = 7$; $x = 1$, $y = 7$. \n\n Pay $b$ dollars and increase or decrease both integers by $1$. For example, if $x = 0$ and $y = 7$ there are two possible outcomes after this operation: $x = -1$, $y = 6$; $x = 1$, $y = 8$. \n\nYour goal is to make both given integers equal zero simultaneously, i.e. $x = y = 0$. There are no other requirements. In particular, it is possible to move from $x=1$, $y=0$ to $x=y=0$.\n\nCalculate the minimum amount of dollars you have to spend on it.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$)\u00a0\u2014 the number of testcases.\n\nThe first line of each test case contains two integers $x$ and $y$ ($0 \\le x, y \\le 10^9$).\n\nThe second line of each test case contains two integers $a$ and $b$ ($1 \\le a, b \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case print one integer\u00a0\u2014 the minimum amount of dollars you have to spend.\n\n\n-----Example-----\nInput\n2\n1 3\n391 555\n0 0\n9 4\n\nOutput\n1337\n0\n\n\n\n-----Note-----\n\nIn the first test case you can perform the following sequence of operations: first, second, first. This way you spend $391 + 555 + 391 = 1337$ dollars.\n\nIn the second test case both integers are equal to zero initially, so you dont' have to spend money."} +{"id": "02196", "text": "Ivan has got an array of n non-negative integers a_1, a_2, ..., a_{n}. Ivan knows that the array is sorted in the non-decreasing order. \n\nIvan wrote out integers 2^{a}_1, 2^{a}_2, ..., 2^{a}_{n} on a piece of paper. Now he wonders, what minimum number of integers of form 2^{b} (b \u2265 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2^{v} - 1 for some integer v (v \u2265 0). \n\nHelp Ivan, find the required quantity of numbers.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5). The second input line contains n space-separated integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 2\u00b710^9). It is guaranteed that a_1 \u2264 a_2 \u2264 ... \u2264 a_{n}.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n4\n0 1 1 1\n\nOutput\n0\n\nInput\n1\n3\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample you do not need to add anything, the sum of numbers already equals 2^3 - 1 = 7.\n\nIn the second sample you need to add numbers 2^0, 2^1, 2^2."} +{"id": "02197", "text": "Dexterina and Womandark have been arch-rivals since they\u2019ve known each other. Since both are super-intelligent teenage girls, they\u2019ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they\u2019ve given to one another, their score is equal and they\u2019re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim.\n\nNim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0, x]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game.\n\nWomandark is coming up with a brand new and evil idea on how to thwart Dexterina\u2019s plans, so she hasn\u2019t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n (1 \u2264 n \u2264 10^9) and x (1 \u2264 x \u2264 100)\u00a0\u2014 the number of heaps and the maximum number of objects in a heap, respectively. The second line contains x + 1 real numbers, given with up to 6 decimal places each: P(0), P(1), ... , P(X). Here, P(i) is the probability of a heap having exactly i objects in start of a game. It's guaranteed that the sum of all P(i) is equal to 1.\n\n\n-----Output-----\n\nOutput a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10^{ - 6}.\n\n\n-----Example-----\nInput\n2 2\n0.500000 0.250000 0.250000\n\nOutput\n0.62500000"} +{"id": "02198", "text": "Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as the following sequence of steps: find a substring \"..\" (two consecutive periods) in string s, of all occurrences of the substring let's choose the first one, and replace this substring with string \".\". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains no two consecutive periods, then nothing happens.\n\nLet's define f(s) as the minimum number of operations of replacement to perform, so that the string does not have any two consecutive periods left.\n\nYou need to process m queries, the i-th results in that the character at position x_{i} (1 \u2264 x_{i} \u2264 n) of string s is assigned value c_{i}. After each operation you have to calculate and output the value of f(s).\n\nHelp Daniel to process all queries.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 300 000) the length of the string and the number of queries.\n\nThe second line contains string s, consisting of n lowercase English letters and period signs.\n\nThe following m lines contain the descriptions of queries. The i-th line contains integer x_{i} and c_{i} (1 \u2264 x_{i} \u2264 n, c_{i} \u2014 a lowercas English letter or a period sign), describing the query of assigning symbol c_{i} to position x_{i}.\n\n\n-----Output-----\n\nPrint m numbers, one per line, the i-th of these numbers must be equal to the value of f(s) after performing the i-th assignment.\n\n\n-----Examples-----\nInput\n10 3\n.b..bz....\n1 h\n3 c\n9 f\n\nOutput\n4\n3\n1\n\nInput\n4 4\n.cc.\n2 .\n3 .\n2 a\n1 a\n\nOutput\n1\n3\n1\n1\n\n\n\n-----Note-----\n\nNote to the first sample test (replaced periods are enclosed in square brackets).\n\nThe original string is \".b..bz....\". after the first query f(hb..bz....) = 4\u00a0\u00a0\u00a0\u00a0(\"hb[..]bz....\" \u2192 \"hb.bz[..]..\" \u2192 \"hb.bz[..].\" \u2192 \"hb.bz[..]\" \u2192 \"hb.bz.\") after the second query f(hb\u0441.bz....) = 3\u00a0\u00a0\u00a0\u00a0(\"hb\u0441.bz[..]..\" \u2192 \"hb\u0441.bz[..].\" \u2192 \"hb\u0441.bz[..]\" \u2192 \"hb\u0441.bz.\") after the third query f(hb\u0441.bz..f.) = 1\u00a0\u00a0\u00a0\u00a0(\"hb\u0441.bz[..]f.\" \u2192 \"hb\u0441.bz.f.\")\n\nNote to the second sample test.\n\nThe original string is \".cc.\". after the first query: f(..c.) = 1\u00a0\u00a0\u00a0\u00a0(\"[..]c.\" \u2192 \".c.\") after the second query: f(....) = 3\u00a0\u00a0\u00a0\u00a0(\"[..]..\" \u2192 \"[..].\" \u2192 \"[..]\" \u2192 \".\") after the third query: f(.a..) = 1\u00a0\u00a0\u00a0\u00a0(\".a[..]\" \u2192 \".a.\") after the fourth query: f(aa..) = 1\u00a0\u00a0\u00a0\u00a0(\"aa[..]\" \u2192 \"aa.\")"} +{"id": "02199", "text": "You are given a multiset S consisting of positive integers (initially empty). There are two kind of queries: Add a positive integer to S, the newly added integer is not less than any number in it. Find a subset s of the set S such that the value $\\operatorname{max}(s) - \\operatorname{mean}(s)$ is maximum possible. Here max(s) means maximum value of elements in s, $\\operatorname{mean}(s)$\u00a0\u2014 the average value of numbers in s. Output this maximum possible value of $\\operatorname{max}(s) - \\operatorname{mean}(s)$. \n\n\n-----Input-----\n\nThe first line contains a single integer Q (1 \u2264 Q \u2264 5\u00b710^5)\u00a0\u2014 the number of queries.\n\nEach of the next Q lines contains a description of query. For queries of type 1 two integers 1 and x are given, where x (1 \u2264 x \u2264 10^9) is a number that you should add to S. It's guaranteed that x is not less than any number in S. For queries of type 2, a single integer 2 is given.\n\nIt's guaranteed that the first query has type 1, i.\u00a0e. S is not empty when a query of type 2 comes.\n\n\n-----Output-----\n\nOutput the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line.\n\nYour answer is considered correct, if each of your answers has absolute or relative error not greater than 10^{ - 6}.\n\nFormally, 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^{-6}$.\n\n\n-----Examples-----\nInput\n6\n1 3\n2\n1 4\n2\n1 8\n2\n\nOutput\n0.0000000000\n0.5000000000\n3.0000000000\n\nInput\n4\n1 1\n1 4\n1 5\n2\n\nOutput\n2.0000000000"} +{"id": "02200", "text": "Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\\lfloor \\frac{w \\cdot a}{b} \\rfloor$ dollars. \n\nMashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.\n\n\n-----Input-----\n\nThe first line of input contains three space-separated integers n, a, b\u00a0(1 \u2264 n \u2264 10^5;\u00a01 \u2264 a, b \u2264 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n}\u00a0(1 \u2264 x_{i} \u2264 10^9).\n\n\n-----Output-----\n\nOutput n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.\n\n\n-----Examples-----\nInput\n5 1 4\n12 6 11 9 1\n\nOutput\n0 2 3 1 1 \nInput\n3 1 2\n1 2 3\n\nOutput\n1 0 1 \nInput\n1 1 1\n1\n\nOutput\n0"} +{"id": "02201", "text": "Johnny drives a truck and must deliver a package from his hometown to the district center. His hometown is located at point 0 on a number line, and the district center is located at the point d.\n\nJohnny's truck has a gas tank that holds exactly n liters, and his tank is initially full. As he drives, the truck consumes exactly one liter per unit distance traveled. Moreover, there are m gas stations located at various points along the way to the district center. The i-th station is located at the point x_{i} on the number line and sells an unlimited amount of fuel at a price of p_{i} dollars per liter. Find the minimum cost Johnny must pay for fuel to successfully complete the delivery.\n\n\n-----Input-----\n\nThe first line of input contains three space separated integers d, n, and m (1 \u2264 n \u2264 d \u2264 10^9, 1 \u2264 m \u2264 200 000)\u00a0\u2014 the total distance to the district center, the volume of the gas tank, and the number of gas stations, respectively.\n\nEach of the next m lines contains two integers x_{i}, p_{i} (1 \u2264 x_{i} \u2264 d - 1, 1 \u2264 p_{i} \u2264 10^6)\u00a0\u2014 the position and cost of gas at the i-th gas station. It is guaranteed that the positions of the gas stations are distinct.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum cost to complete the delivery. If there is no way to complete the delivery, print -1.\n\n\n-----Examples-----\nInput\n10 4 4\n3 5\n5 8\n6 3\n8 4\n\nOutput\n22\n\nInput\n16 5 2\n8 2\n5 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample, Johnny's truck holds 4 liters. He can drive 3 units to the first gas station, buy 2 liters of gas there (bringing the tank to 3 liters total), drive 3 more units to the third gas station, buy 4 liters there to fill up his tank, and then drive straight to the district center. His total cost is 2\u00b75 + 4\u00b73 = 22 dollars.\n\nIn the second sample, there is no way for Johnny to make it to the district center, as his tank cannot hold enough gas to take him from the latest gas station to the district center."} +{"id": "02202", "text": "Rebel spy Heidi has just obtained the plans for the Death Star from the Empire and, now on her way to safety, she is trying to break the encryption of the plans (of course they are encrypted \u2013 the Empire may be evil, but it is not stupid!). The encryption has several levels of security, and here is how the first one looks.\n\nHeidi is presented with a screen that shows her a sequence of integers A and a positive integer p. She knows that the encryption code is a single number S, which is defined as follows:\n\nDefine the score of X to be the sum of the elements of X modulo p.\n\nHeidi is given a sequence A that consists of N integers, and also given an integer p. She needs to split A into 2 parts such that: Each part contains at least 1 element of A, and each part consists of contiguous elements of A. The two parts do not overlap. The total sum S of the scores of those two parts is maximized. This is the encryption code. \n\nOutput the sum S, which is the encryption code.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integer N and p (2 \u2264 N \u2264 100 000, 2 \u2264 p \u2264 10 000) \u2013 the number of elements in A, and the modulo for computing scores, respectively.\n\nThe second line contains N space-separated integers which are the elements of A. Each integer is from the interval [1, 1 000 000].\n\n\n-----Output-----\n\nOutput the number S as described in the problem statement.\n\n\n-----Examples-----\nInput\n4 10\n3 4 7 2\n\nOutput\n16\n\nInput\n10 12\n16 3 24 13 9 8 7 5 12 12\n\nOutput\n13\n\n\n\n-----Note-----\n\nIn the first example, the score is maximized if the input sequence is split into two parts as (3, 4), (7, 2). It gives the total score of $(3 + 4 \\operatorname{mod} 10) +(7 + 2 \\operatorname{mod} 10) = 16$.\n\nIn the second example, the score is maximized if the first part consists of the first three elements, and the second part consists of the rest. Then, the score is $(16 + 3 + 24 \\operatorname{mod} 12) +(13 + 9 + 8 + 7 + 5 + 12 + 12 \\operatorname{mod} 12) = 7 + 6 = 13$."} +{"id": "02203", "text": "Amr bought a new video game \"Guess Your Way Out! II\". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.\n\nLet's index all the nodes of the tree such that The root is number 1 Each internal node i (i \u2264 2^{h} - 1 - 1) will have a left child with index = 2i and a right child with index = 2i + 1 \n\nThe level of a node is defined as 1 for a root, or 1 + level of parent of the node otherwise. The vertices of the level h are called leaves. The exit to the maze is located at some leaf node n, the player doesn't know where the exit is so he has to guess his way out! \n\nIn the new version of the game the player is allowed to ask questions on the format \"Does the ancestor(exit, i) node number belong to the range [L, R]?\". Here ancestor(v, i) is the ancestor of a node v that located in the level i. The game will answer with \"Yes\" or \"No\" only. The game is designed such that it doesn't always answer correctly, and sometimes it cheats to confuse the player!.\n\nAmr asked a lot of questions and got confused by all these answers, so he asked you to help him. Given the questions and its answers, can you identify whether the game is telling contradictory information or not? If the information is not contradictory and the exit node can be determined uniquely, output its number. If the information is not contradictory, but the exit node isn't defined uniquely, output that the number of questions is not sufficient. Otherwise output that the information is contradictory.\n\n\n-----Input-----\n\nThe first line contains two integers h, q (1 \u2264 h \u2264 50, 0 \u2264 q \u2264 10^5), the height of the tree and the number of questions respectively.\n\nThe next q lines will contain four integers each i, L, R, ans (1 \u2264 i \u2264 h, 2^{i} - 1 \u2264 L \u2264 R \u2264 2^{i} - 1, $\\text{ans} \\in \\{0,1 \\}$), representing a question as described in the statement with its answer (ans = 1 if the answer is \"Yes\" and ans = 0 if the answer is \"No\").\n\n\n-----Output-----\n\nIf the information provided by the game is contradictory output \"Game cheated!\" without the quotes.\n\nElse if you can uniquely identify the exit to the maze output its index. \n\nOtherwise output \"Data not sufficient!\" without the quotes.\n\n\n-----Examples-----\nInput\n3 1\n3 4 6 0\n\nOutput\n7\nInput\n4 3\n4 10 14 1\n3 6 6 0\n2 3 3 1\n\nOutput\n14\nInput\n4 2\n3 4 6 1\n4 12 15 1\n\nOutput\nData not sufficient!\nInput\n4 2\n3 4 5 1\n2 3 3 1\n\nOutput\nGame cheated!\n\n\n-----Note-----\n\nNode u is an ancestor of node v if and only if u is the same node as v, u is the parent of node v, or u is an ancestor of the parent of node v. \n\nIn the first sample test there are 4 leaf nodes 4, 5, 6, 7. The first question says that the node isn't in the range [4, 6] so the exit is node number 7.\n\nIn the second sample test there are 8 leaf nodes. After the first question the exit is in the range [10, 14]. After the second and the third questions only node number 14 is correct. Check the picture below to fully understand.\n\n[Image]"} +{"id": "02204", "text": "Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly $n$ flowers.\n\nVladimir went to a flower shop, and he was amazed to see that there are $m$ types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the $i$-th type happiness of his wife increases by $a_i$ and after receiving each consecutive flower of this type her happiness increases by $b_i$. That is, if among the chosen flowers there are $x_i > 0$ flowers of type $i$, his wife gets $a_i + (x_i - 1) \\cdot b_i$ additional happiness (and if there are no flowers of type $i$, she gets nothing for this particular type).\n\nPlease help Vladimir to choose exactly $n$ flowers to maximize the total happiness of his wife.\n\n\n-----Input-----\n\nThe first line contains the only integer $t$ ($1 \\leq t \\leq 10\\,000$), the number of test cases. It is followed by $t$ descriptions of the test cases.\n\nEach test case description starts with two integers $n$ and $m$ ($1 \\le n \\le 10^9$, $1 \\le m \\le 100\\,000$), the number of flowers Vladimir needs to choose and the number of types of available flowers.\n\nThe following $m$ lines describe the types of flowers: each line contains integers $a_i$ and $b_i$ ($0 \\le a_i, b_i \\le 10^9$) for $i$-th available type of flowers.\n\nThe test cases are separated by a blank line. It is guaranteed that the sum of values $m$ among all test cases does not exceed $100\\,000$.\n\n\n-----Output-----\n\nFor each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly $n$ flowers optimally.\n\n\n-----Example-----\nInput\n2\n4 3\n5 0\n1 4\n2 2\n\n5 3\n5 2\n4 2\n3 1\n\nOutput\n14\n16\n\n\n\n-----Note-----\n\nIn the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals $5 + (1 + 2 \\cdot 4) = 14$.\n\nIn the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals $(5 + 1 \\cdot 2) + (4 + 1 \\cdot 2) + 3 = 16$."} +{"id": "02205", "text": "People in the Tomskaya region like magic formulas very much. You can see some of them below.\n\nImagine you are given a sequence of positive integer numbers p_1, p_2, ..., p_{n}. Lets write down some magic formulas:$q_{i} = p_{i} \\oplus(i \\operatorname{mod} 1) \\oplus(i \\operatorname{mod} 2) \\oplus \\cdots \\oplus(i \\operatorname{mod} n)$$Q = q_{1} \\oplus q_{2} \\oplus \\ldots \\oplus q_{n}$\n\nHere, \"mod\" means the operation of taking the residue after dividing.\n\nThe expression $x \\oplus y$ means applying the bitwise xor (excluding \"OR\") operation to integers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by \"^\", in Pascal \u2014 by \"xor\".\n\nPeople in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence p, calculate the value of Q.\n\n\n-----Input-----\n\nThe first line of the input contains the only integer n (1 \u2264 n \u2264 10^6). The next line contains n integers: p_1, p_2, ..., p_{n} (0 \u2264 p_{i} \u2264 2\u00b710^9).\n\n\n-----Output-----\n\nThe only line of output should contain a single integer \u2014 the value of Q.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n3"} +{"id": "02206", "text": "There are n points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw n - 1 segments, each connecting any two marked points, in such a way that all points have to be connected with each other (directly or indirectly).\n\nBut there are some restrictions. Firstly, some pairs of points cannot be connected directly and have to be connected undirectly. Secondly, the segments you draw must not intersect in any point apart from the marked points (that is, if any two segments intersect and their intersection is not a marked point, then the picture you have drawn is invalid).\n\nHow many ways are there to connect all vertices with n - 1 segments? Two ways are considered different iff there exist some pair of points such that a segment is drawn between them in the first way of connection, but it is not drawn between these points in the second one. Since the answer might be large, output it modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line contains one number n (3 \u2264 n \u2264 500) \u2014 the number of marked points.\n\nThen n lines follow, each containing n elements. a_{i}, j (j-th element of line i) is equal to 1 iff you can connect points i and j directly (otherwise a_{i}, j = 0). It is guaranteed that for any pair of points a_{i}, j = a_{j}, i, and for any point a_{i}, i = 0.\n\n\n-----Output-----\n\nPrint the number of ways to connect points modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n3\n0 0 1\n0 0 1\n1 1 0\n\nOutput\n1\n\nInput\n4\n0 1 1 1\n1 0 1 1\n1 1 0 1\n1 1 1 0\n\nOutput\n12\n\nInput\n3\n0 0 0\n0 0 1\n0 1 0\n\nOutput\n0"} +{"id": "02207", "text": "\"The zombies are lurking outside. Waiting. Moaning. And when they come...\"\n\n\"When they come?\"\n\n\"I hope the Wall is high enough.\"\n\nZombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for help. Go forth and explore the wall! Report how many disconnected segments there are.\n\nThe wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between, is C columns wide.\n\n\n-----Input-----\n\nThe first line of the input consists of two space-separated integers R and C, 1 \u2264 R, C \u2264 100. The next R lines provide a description of the columns as follows: each of the R lines contains a string of length C, the c-th character of line r is B if there is a brick in column c and row R - r + 1, and . otherwise. The input will contain at least one character B and it will be valid.\n\n\n-----Output-----\n\nThe number of wall segments in the input configuration.\n\n\n-----Examples-----\nInput\n3 7\n.......\n.......\n.BB.B..\n\nOutput\n2\n\nInput\n4 5\n..B..\n..B..\nB.B.B\nBBB.B\n\nOutput\n2\n\nInput\n4 6\n..B...\nB.B.BB\nBBB.BB\nBBBBBB\n\nOutput\n1\n\nInput\n1 1\nB\n\nOutput\n1\n\nInput\n10 7\n.......\n.......\n.......\n.......\n.......\n.......\n.......\n.......\n...B...\nB.BB.B.\n\nOutput\n3\n\nInput\n8 8\n........\n........\n........\n........\n.B......\n.B.....B\n.B.....B\n.BB...BB\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second."} +{"id": "02208", "text": "Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you)\u00a0\u2014 who knows? \n\nEvery one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of $\\operatorname{max}_{i = l}^{r} a_{i}$ while !Mike can instantly tell the value of $\\operatorname{min}_{i = l} b_{i}$.\n\nNow suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r) (1 \u2264 l \u2264 r \u2264 n) (so he will make exactly n(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs $\\operatorname{max}_{i = l}^{r} a_{i} = \\operatorname{min}_{i = l} b_{i}$ is satisfied.\n\nHow many occasions will the robot count?\n\n\n-----Input-----\n\nThe first line contains only integer n (1 \u2264 n \u2264 200 000).\n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the sequence a.\n\nThe third line contains n integer numbers b_1, b_2, ..., b_{n} ( - 10^9 \u2264 b_{i} \u2264 10^9)\u00a0\u2014 the sequence b.\n\n\n-----Output-----\n\nPrint the only integer number\u00a0\u2014 the number of occasions the robot will count, thus for how many pairs $\\operatorname{max}_{i = l}^{r} a_{i} = \\operatorname{min}_{i = l} b_{i}$ is satisfied.\n\n\n-----Examples-----\nInput\n6\n1 2 3 2 1 4\n6 7 1 2 3 2\n\nOutput\n2\n\nInput\n3\n3 3 3\n1 1 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nThe occasions in the first sample case are:\n\n1.l = 4,r = 4 since max{2} = min{2}.\n\n2.l = 4,r = 5 since max{2, 1} = min{2, 3}.\n\nThere are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1."} +{"id": "02209", "text": "Pushok the dog has been chasing Imp for a few hours already. $48$ \n\nFortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. \n\nWhile moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string \"sh\" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. \n\nThe robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.\n\nHelp Imp to find the maximum noise he can achieve by changing the order of the strings.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of strings in robot's memory.\n\nNext n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maxumum possible noise Imp can achieve by changing the order of the strings.\n\n\n-----Examples-----\nInput\n4\nssh\nhs\ns\nhhhs\n\nOutput\n18\n\nInput\n2\nh\ns\n\nOutput\n1\n\n\n\n-----Note-----\n\nThe optimal concatenation in the first sample is ssshhshhhs."} +{"id": "02210", "text": "Ayush and Ashish play a game on an unrooted tree consisting of $n$ nodes numbered $1$ to $n$. Players make the following move in turns: Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to $1$. \n\nA tree is a connected undirected graph without cycles.\n\nThere is a special node numbered $x$. The player who removes this node wins the game. \n\nAyush moves first. Determine the winner of the game if each player plays optimally.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $t$ $(1 \\leq t \\leq 10)$\u00a0\u2014 the number of testcases. The description of the test cases follows.\n\nThe first line of each testcase contains two integers $n$ and $x$ $(1\\leq n \\leq 1000, 1 \\leq x \\leq n)$\u00a0\u2014 the number of nodes in the tree and the special node respectively.\n\nEach of the next $n-1$ lines contain two integers $u$, $v$ $(1 \\leq u, v \\leq n, \\text{ } u \\ne v)$, meaning that there is an edge between nodes $u$ and $v$ in the tree.\n\n\n-----Output-----\n\nFor every test case, if Ayush wins the game, print \"Ayush\", otherwise print \"Ashish\" (without quotes).\n\n\n-----Examples-----\nInput\n1\n3 1\n2 1\n3 1\n\nOutput\nAshish\n\nInput\n1\n3 2\n1 2\n1 3\n\nOutput\nAyush\n\n\n\n-----Note-----\n\nFor the $1$st test case, Ayush can only remove node $2$ or $3$, after which node $1$ becomes a leaf node and Ashish can remove it in his turn.\n\nFor the $2$nd test case, Ayush can remove node $2$ in the first move itself."} +{"id": "02211", "text": "Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string s. To determine if a string is good or not the game uses rules. Overall there are n rules. Each rule is described by a group of three (p, l, r), where p is a string and l and r (l \u2264 r) are integers. We\u2019ll say that string t complies with rule (p, l, r), if the number of occurrences of string t in string p lies between l and r, inclusive. For example, string \"ab\", complies with rules (\"ab\", 1, 2) and (\"aab\", 0, 1), but does not comply with rules (\"cd\", 1, 2) and (\"abab\", 0, 1).\n\nA substring s[l... r] (1 \u2264 l \u2264 r \u2264 |s|) of string s = s_1s_2... s_{|}s| (|s| is a length of s) is string s_{l}s_{l} + 1... s_{r}.\n\nConsider a number of occurrences of string t in string p as a number of pairs of integers l, r (1 \u2264 l \u2264 r \u2264 |p|) such that p[l... r] = t.\n\nWe\u2019ll say that string t is good if it complies with all n rules. Smart Beaver asks you to help him to write a program that can calculate the number of distinct good substrings of string s. Two substrings s[x... y] and s[z... w] are cosidered to be distinct iff s[x... y] \u2260 s[z... w].\n\n\n-----Input-----\n\nThe first line contains string s. The second line contains integer n. Next n lines contain the rules, one per line. Each of these lines contains a string and two integers p_{i}, l_{i}, r_{i}, separated by single spaces (0 \u2264 l_{i} \u2264 r_{i} \u2264 |p_{i}|). It is guaranteed that all the given strings are non-empty and only contain lowercase English letters.\n\nThe input limits for scoring 30 points are (subproblem G1): 0 \u2264 n \u2264 10. The length of string s and the maximum length of string p is \u2264 200. \n\nThe input limits for scoring 70 points are (subproblems G1+G2): 0 \u2264 n \u2264 10. The length of string s and the maximum length of string p is \u2264 2000. \n\nThe input limits for scoring 100 points are (subproblems G1+G2+G3): 0 \u2264 n \u2264 10. The length of string s and the maximum length of string p is \u2264 50000. \n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of good substrings of string s.\n\n\n-----Examples-----\nInput\naaab\n2\naa 0 0\naab 1 1\n\nOutput\n3\n\nInput\nltntlnen\n3\nn 0 0\nttlneenl 1 4\nlelllt 1 1\n\nOutput\n2\n\nInput\na\n0\n\nOutput\n1\n\n\n\n-----Note-----\n\nThere are three good substrings in the first sample test: \u00abaab\u00bb, \u00abab\u00bb and \u00abb\u00bb.\n\nIn the second test only substrings \u00abe\u00bb and \u00abt\u00bb are good."} +{"id": "02212", "text": "Find an n \u00d7 n matrix with different numbers from 1 to n^2, so the sum in each row, column and both main diagonals are odd.\n\n\n-----Input-----\n\nThe only line contains odd integer n (1 \u2264 n \u2264 49).\n\n\n-----Output-----\n\nPrint n lines with n integers. All the integers should be different and from 1 to n^2. The sum in each row, column and both main diagonals should be odd.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n3\n\nOutput\n2 1 4\n3 5 7\n6 9 8"} +{"id": "02213", "text": "Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers.\n\nIahub can choose a pair of distinct indices i and j (1 \u2264 i, j \u2264 m, i \u2260 j). Then in each array the values at positions i and j are swapped only if the value at position i is strictly greater than the value at position j.\n\nIahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the n arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most $\\frac{m(m - 1)}{2}$ (at most $\\frac{m(m - 1)}{2}$ pairs). Help Iahub, find any suitable array.\n\n\n-----Input-----\n\nThe first line contains three integers n (1 \u2264 n \u2264 1000), m (1 \u2264 m \u2264 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element x of the array i, 1 \u2264 x \u2264 10^6 holds.\n\n\n-----Output-----\n\nOn the first line of the output print an integer p, the size of the array (p can be at most $\\frac{m(m - 1)}{2}$). Each of the next p lines must contain two distinct integers i and j (1 \u2264 i, j \u2264 m, i \u2260 j), representing the chosen indices.\n\nIf there are multiple correct answers, you can print any.\n\n\n-----Examples-----\nInput\n2 5 0\n1 3 2 5 4\n1 4 3 2 5\n\nOutput\n3\n2 4\n2 3\n4 5\n\nInput\n3 2 1\n1 2\n2 3\n3 4\n\nOutput\n1\n2 1\n\n\n\n-----Note-----\n\nConsider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5]."} +{"id": "02214", "text": "A binary matrix is called good if every even length square sub-matrix has an odd number of ones. \n\nGiven a binary matrix $a$ consisting of $n$ rows and $m$ columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all. \n\nAll the terms above have their usual meanings\u00a0\u2014 refer to the Notes section for their formal definitions. \n\n\n-----Input-----\n\nThe first line of input contains two integers $n$ and $m$ ($1 \\leq n \\leq m \\leq 10^6$ and $n\\cdot m \\leq 10^6$) \u00a0\u2014 the number of rows and columns in $a$, respectively. \n\nThe following $n$ lines each contain $m$ characters, each of which is one of 0 and 1. If the $j$-th character on the $i$-th line is 1, then $a_{i,j} = 1$. Similarly, if the $j$-th character on the $i$-th line is 0, then $a_{i,j} = 0$.\n\n\n-----Output-----\n\nOutput the minimum number of cells you need to change to make $a$ good, or output $-1$ if it's not possible at all.\n\n\n-----Examples-----\nInput\n3 3\n101\n001\n110\n\nOutput\n2\nInput\n7 15\n000100001010010\n100111010110001\n101101111100100\n010000111111010\n111010010100001\n000011001111101\n111111011010011\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first case, changing $a_{1,1}$ to $0$ and $a_{2,2}$ to $1$ is enough. \n\nYou can verify that there is no way to make the matrix in the second case good. \n\nSome definitions\u00a0\u2014 A binary matrix is one in which every element is either $1$ or $0$. A sub-matrix is described by $4$ parameters\u00a0\u2014 $r_1$, $r_2$, $c_1$, and $c_2$; here, $1 \\leq r_1 \\leq r_2 \\leq n$ and $1 \\leq c_1 \\leq c_2 \\leq m$. This sub-matrix contains all elements $a_{i,j}$ that satisfy both $r_1 \\leq i \\leq r_2$ and $c_1 \\leq j \\leq c_2$. A sub-matrix is, further, called an even length square if $r_2-r_1 = c_2-c_1$ and $r_2-r_1+1$ is divisible by $2$."} +{"id": "02215", "text": "Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.\n\nThere are $n$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $i$-th position. Thus each of $n$ positions should contain exactly one flower: a rose or a lily.\n\nShe knows that exactly $m$ people will visit this exhibition. The $i$-th visitor will visit all flowers from $l_i$ to $r_i$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.\n\nSonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1\\leq n, m\\leq 10^3$)\u00a0\u2014 the number of flowers and visitors respectively.\n\nEach of the next $m$ lines contains two integers $l_i$ and $r_i$ ($1\\leq l_i\\leq r_i\\leq n$), meaning that $i$-th visitor will visit all flowers from $l_i$ to $r_i$ inclusive.\n\n\n-----Output-----\n\nPrint the string of $n$ characters. The $i$-th symbol should be \u00ab0\u00bb if you want to put a rose in the $i$-th position, otherwise \u00ab1\u00bb if you want to put a lily.\n\nIf there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n5 3\n1 3\n2 4\n2 5\n\nOutput\n01100\nInput\n6 3\n5 6\n1 4\n4 6\n\nOutput\n110010\n\n\n-----Note-----\n\nIn the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions;\n\n in the segment $[1\\ldots3]$, there are one rose and two lilies, so the beauty is equal to $1\\cdot 2=2$; in the segment $[2\\ldots4]$, there are one rose and two lilies, so the beauty is equal to $1\\cdot 2=2$; in the segment $[2\\ldots5]$, there are two roses and two lilies, so the beauty is equal to $2\\cdot 2=4$. \n\nThe total beauty is equal to $2+2+4=8$.\n\nIn the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions;\n\n in the segment $[5\\ldots6]$, there are one rose and one lily, so the beauty is equal to $1\\cdot 1=1$; in the segment $[1\\ldots4]$, there are two roses and two lilies, so the beauty is equal to $2\\cdot 2=4$; in the segment $[4\\ldots6]$, there are two roses and one lily, so the beauty is equal to $2\\cdot 1=2$. \n\nThe total beauty is equal to $1+4+2=7$."} +{"id": "02216", "text": "Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns \u2013 starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).\n\nValera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x_1, y_1), (x_2, y_2), ..., (x_{r}, y_{r}), that: r \u2265 2; for any integer i (1 \u2264 i \u2264 r - 1) the following equation |x_{i} - x_{i} + 1| + |y_{i} - y_{i} + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. \n\nValera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. \n\nHelp Valera to arrange k tubes on his rectangle table in a fancy manner.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers n, m, k (2 \u2264 n, m \u2264 300; 2 \u2264 2k \u2264 n\u00b7m) \u2014 the number of rows, the number of columns and the number of tubes, correspondingly. \n\n\n-----Output-----\n\nPrint k lines. In the i-th line print the description of the i-th tube: first print integer r_{i} (the number of tube cells), then print 2r_{i} integers x_{i}1, y_{i}1, x_{i}2, y_{i}2, ..., x_{ir}_{i}, y_{ir}_{i} (the sequence of table cells).\n\nIf there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists. \n\n\n-----Examples-----\nInput\n3 3 3\n\nOutput\n3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3\n\nInput\n2 3 1\n\nOutput\n6 1 1 1 2 1 3 2 3 2 2 2 1\n\n\n\n-----Note-----\n\nPicture for the first sample: [Image] \n\nPicture for the second sample: [Image]"} +{"id": "02217", "text": "You are given a positive integer $D$. Let's build the following graph from it: each vertex is a divisor of $D$ (not necessarily prime, $1$ and $D$ itself are also included); two vertices $x$ and $y$ ($x > y$) have an undirected edge between them if $x$ is divisible by $y$ and $\\frac x y$ is a prime; the weight of an edge is the number of divisors of $x$ that are not divisors of $y$. \n\nFor example, here is the graph for $D=12$: [Image] \n\nEdge $(4,12)$ has weight $3$ because $12$ has divisors $[1,2,3,4,6,12]$ and $4$ has divisors $[1,2,4]$. Thus, there are $3$ divisors of $12$ that are not divisors of $4$ \u2014 $[3,6,12]$.\n\nThere is no edge between $3$ and $2$ because $3$ is not divisible by $2$. There is no edge between $12$ and $3$ because $\\frac{12}{3}=4$ is not a prime.\n\nLet the length of the path between some vertices $v$ and $u$ in the graph be the total weight of edges on it. For example, path $[(1, 2), (2, 6), (6, 12), (12, 4), (4, 2), (2, 6)]$ has length $1+2+2+3+1+2=11$. The empty path has length $0$.\n\nSo the shortest path between two vertices $v$ and $u$ is the path that has the minimal possible length.\n\nTwo paths $a$ and $b$ are different if there is either a different number of edges in them or there is a position $i$ such that $a_i$ and $b_i$ are different edges.\n\nYou are given $q$ queries of the following form: $v$ $u$ \u2014 calculate the number of the shortest paths between vertices $v$ and $u$. \n\nThe answer for each query might be large so print it modulo $998244353$.\n\n\n-----Input-----\n\nThe first line contains a single integer $D$ ($1 \\le D \\le 10^{15}$) \u2014 the number the graph is built from.\n\nThe second line contains a single integer $q$ ($1 \\le q \\le 3 \\cdot 10^5$) \u2014 the number of queries.\n\nEach of the next $q$ lines contains two integers $v$ and $u$ ($1 \\le v, u \\le D$). It is guaranteed that $D$ is divisible by both $v$ and $u$ (both $v$ and $u$ are divisors of $D$).\n\n\n-----Output-----\n\nPrint $q$ integers \u2014 for each query output the number of the shortest paths between the two given vertices modulo $998244353$.\n\n\n-----Examples-----\nInput\n12\n3\n4 4\n12 1\n3 4\n\nOutput\n1\n3\n1\n\nInput\n1\n1\n1 1\n\nOutput\n1\n\nInput\n288807105787200\n4\n46 482955026400\n12556830686400 897\n414 12556830686400\n4443186242880 325\n\nOutput\n547558588\n277147129\n457421435\n702277623\n\n\n\n-----Note-----\n\nIn the first example: The first query is only the empty path \u2014 length $0$; The second query are paths $[(12, 4), (4, 2), (2, 1)]$ (length $3+1+1=5$), $[(12, 6), (6, 2), (2, 1)]$ (length $2+2+1=5$) and $[(12, 6), (6, 3), (3, 1)]$ (length $2+2+1=5$). The third query is only the path $[(3, 1), (1, 2), (2, 4)]$ (length $1+1+1=3$)."} +{"id": "02218", "text": "General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants.\n\nAll soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier.\n\nOn each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers.\n\nHelp Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty.\n\n\n-----Input-----\n\nThe first line contains two integers n, k (1 \u2264 n \u2264 50; 1 \u2264 k \u2264 $\\frac{n(n + 1)}{2}$) \u2014 the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^7) \u2014 the beauties of the battalion soldiers.\n\nIt is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty.\n\n\n-----Output-----\n\nPrint k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 \u2264 c_{i} \u2264 n) \u2014 the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i \u2014 the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order.\n\nSeparate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n3 3\n1 2 3\n\nOutput\n1 1\n1 2\n2 3 2\n\nInput\n2 1\n7 12\n\nOutput\n1 12"} +{"id": "02219", "text": "You are given an integer $n$ and an integer $k$.\n\nIn one step you can do one of the following moves: decrease $n$ by $1$; divide $n$ by $k$ if $n$ is divisible by $k$. \n\nFor example, if $n = 27$ and $k = 3$ you can do the following steps: $27 \\rightarrow 26 \\rightarrow 25 \\rightarrow 24 \\rightarrow 8 \\rightarrow 7 \\rightarrow 6 \\rightarrow 2 \\rightarrow 1 \\rightarrow 0$.\n\nYou are asked to calculate the minimum number of steps to reach $0$ from $n$. \n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of queries.\n\nThe only line of each query contains two integers $n$ and $k$ ($1 \\le n \\le 10^{18}$, $2 \\le k \\le 10^{18}$).\n\n\n-----Output-----\n\nFor each query print the minimum number of steps to reach $0$ from $n$ in single line. \n\n\n-----Example-----\nInput\n2\n59 3\n1000000000000000000 10\n\nOutput\n8\n19\n\n\n\n-----Note-----\n\nSteps for the first test case are: $59 \\rightarrow 58 \\rightarrow 57 \\rightarrow 19 \\rightarrow 18 \\rightarrow 6 \\rightarrow 2 \\rightarrow 1 \\rightarrow 0$.\n\nIn the second test case you have to divide $n$ by $k$ $18$ times and then decrease $n$ by $1$."} +{"id": "02220", "text": "There are $n$ emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The $i$-th emote increases the opponent's happiness by $a_i$ units (we all know that emotes in this game are used to make opponents happy).\n\nYou have time to use some emotes only $m$ times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than $k$ times in a row (otherwise the opponent will think that you're trolling him).\n\nNote that two emotes $i$ and $j$ ($i \\ne j$) such that $a_i = a_j$ are considered different.\n\nYou have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n, m$ and $k$ ($2 \\le n \\le 2 \\cdot 10^5$, $1 \\le k \\le m \\le 2 \\cdot 10^9$) \u2014 the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is value of the happiness of the $i$-th emote.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.\n\n\n-----Examples-----\nInput\n6 9 2\n1 3 3 7 4 2\n\nOutput\n54\n\nInput\n3 1000000000 1\n1000000000 987654321 1000000000\n\nOutput\n1000000000000000000\n\n\n\n-----Note-----\n\nIn the first example you may use emotes in the following sequence: $4, 4, 5, 4, 4, 5, 4, 4, 5$."} +{"id": "02221", "text": "You a captain of a ship. Initially you are standing in a point $(x_1, y_1)$ (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point $(x_2, y_2)$. \n\nYou know the weather forecast \u2014 the string $s$ of length $n$, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side $s_1$, the second day \u2014 $s_2$, the $n$-th day \u2014 $s_n$ and $(n+1)$-th day \u2014 $s_1$ again and so on. \n\nShip coordinates change the following way: if wind blows the direction U, then the ship moves from $(x, y)$ to $(x, y + 1)$; if wind blows the direction D, then the ship moves from $(x, y)$ to $(x, y - 1)$; if wind blows the direction L, then the ship moves from $(x, y)$ to $(x - 1, y)$; if wind blows the direction R, then the ship moves from $(x, y)$ to $(x + 1, y)$. \n\nThe ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point $(x, y)$ it will move to the point $(x - 1, y + 1)$, and if it goes the direction U, then it will move to the point $(x, y + 2)$.\n\nYou task is to determine the minimal number of days required for the ship to reach the point $(x_2, y_2)$.\n\n\n-----Input-----\n\nThe first line contains two integers $x_1, y_1$ ($0 \\le x_1, y_1 \\le 10^9$) \u2014 the initial coordinates of the ship.\n\nThe second line contains two integers $x_2, y_2$ ($0 \\le x_2, y_2 \\le 10^9$) \u2014 the coordinates of the destination point.\n\nIt is guaranteed that the initial coordinates and destination point coordinates are different.\n\nThe third line contains a single integer $n$ ($1 \\le n \\le 10^5$) \u2014 the length of the string $s$.\n\nThe fourth line contains the string $s$ itself, consisting only of letters U, D, L and R.\n\n\n-----Output-----\n\nThe only line should contain the minimal number of days required for the ship to reach the point $(x_2, y_2)$.\n\nIf it's impossible then print \"-1\".\n\n\n-----Examples-----\nInput\n0 0\n4 6\n3\nUUU\n\nOutput\n5\n\nInput\n0 3\n0 0\n3\nUDD\n\nOutput\n3\n\nInput\n0 0\n0 1\n1\nL\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example the ship should perform the following sequence of moves: \"RRRRU\". Then its coordinates will change accordingly: $(0, 0)$ $\\rightarrow$ $(1, 1)$ $\\rightarrow$ $(2, 2)$ $\\rightarrow$ $(3, 3)$ $\\rightarrow$ $(4, 4)$ $\\rightarrow$ $(4, 6)$.\n\nIn the second example the ship should perform the following sequence of moves: \"DD\" (the third day it should stay in place). Then its coordinates will change accordingly: $(0, 3)$ $\\rightarrow$ $(0, 3)$ $\\rightarrow$ $(0, 1)$ $\\rightarrow$ $(0, 0)$.\n\nIn the third example the ship can never reach the point $(0, 1)$."} +{"id": "02222", "text": "Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. \n\nAs a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree.\n\nA tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a node $v$ is the last different from $v$ vertex on the path from the root to the vertex $v$. Children of vertex $v$ are all nodes for which $v$ is the parent. A vertex is a leaf if it has no children.\n\nThe rooted tree Serval owns has $n$ nodes, node $1$ is the root. Serval will write some numbers into all nodes of the tree. However, there are some restrictions. Each of the nodes except leaves has an operation $\\max$ or $\\min$ written in it, indicating that the number in this node should be equal to the maximum or minimum of all the numbers in its sons, respectively. \n\nAssume that there are $k$ leaves in the tree. Serval wants to put integers $1, 2, \\ldots, k$ to the $k$ leaves (each number should be used exactly once). He loves large numbers, so he wants to maximize the number in the root. As his best friend, can you help him?\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($2 \\leq n \\leq 3\\cdot 10^5$), the size of the tree.\n\nThe second line contains $n$ integers, the $i$-th of them represents the operation in the node $i$. $0$ represents $\\min$ and $1$ represents $\\max$. If the node is a leaf, there is still a number of $0$ or $1$, but you can ignore it.\n\nThe third line contains $n-1$ integers $f_2, f_3, \\ldots, f_n$ ($1 \\leq f_i \\leq i-1$), where $f_i$ represents the parent of the node $i$.\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 the maximum possible number in the root of the tree.\n\n\n-----Examples-----\nInput\n6\n1 0 1 1 0 1\n1 2 2 2 2\n\nOutput\n1\n\nInput\n5\n1 0 1 0 1\n1 1 1 1\n\nOutput\n4\n\nInput\n8\n1 0 0 1 0 1 1 0\n1 1 2 2 3 3 3\n\nOutput\n4\n\nInput\n9\n1 1 0 0 1 0 1 0 1\n1 1 2 2 3 3 4 4\n\nOutput\n5\n\n\n\n-----Note-----\n\nPictures below explain the examples. The numbers written in the middle of the nodes are their indices, and the numbers written on the top are the numbers written in the nodes.\n\nIn the first example, no matter how you arrange the numbers, the answer is $1$. [Image] \n\nIn the second example, no matter how you arrange the numbers, the answer is $4$. [Image] \n\nIn the third example, one of the best solution to achieve $4$ is to arrange $4$ and $5$ to nodes $4$ and $5$. [Image] \n\nIn the fourth example, the best solution is to arrange $5$ to node $5$. [Image]"} +{"id": "02223", "text": "You're given a tree with $n$ vertices.\n\nYour task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 10^5$) denoting the size of the tree. \n\nThe next $n - 1$ lines contain two integers $u$, $v$ ($1 \\le u, v \\le n$) each, describing the vertices connected by the $i$-th edge.\n\nIt's guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nOutput a single integer $k$ \u2014 the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property.\n\n\n-----Examples-----\nInput\n4\n2 4\n4 1\n3 1\n\nOutput\n1\nInput\n3\n1 2\n1 3\n\nOutput\n-1\nInput\n10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5\n\nOutput\n4\nInput\n2\n1 2\n\nOutput\n0\n\n\n-----Note-----\n\nIn the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each.\n\nIn the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$."} +{"id": "02224", "text": "Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.\n\nThe distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.\n\nManao has heard of m settlements affected by the Book of Evil. Their numbers are p_1, p_2, ..., p_{m}. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers n, m and d (1 \u2264 m \u2264 n \u2264 100000;\u00a00 \u2264 d \u2264 n - 1). The second line contains m distinct space-separated integers p_1, p_2, ..., p_{m} (1 \u2264 p_{i} \u2264 n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers a_{i} and b_{i} representing the ends of this path.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.\n\n\n-----Examples-----\nInput\n6 2 3\n1 2\n1 5\n2 3\n3 4\n4 5\n5 6\n\nOutput\n3\n\n\n\n-----Note-----\n\nSample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.\n\n [Image]"} +{"id": "02225", "text": "Xenia the beginner programmer has a sequence a, consisting of 2^{n} non-negative integers: a_1, a_2, ..., a_2^{n}. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.\n\nNamely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a_1\u00a0or\u00a0a_2, a_3\u00a0or\u00a0a_4, ..., a_2^{n} - 1\u00a0or\u00a0a_2^{n}, consisting of 2^{n} - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v.\n\nLet's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4) \u2192 (1\u00a0or\u00a02 = 3, 3\u00a0or\u00a04 = 7) \u2192 (3\u00a0xor\u00a07 = 4). The result is v = 4.\n\nYou are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional m queries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment a_{p} = b. After each query, you need to print the new value v for the new sequence a.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 17, 1 \u2264 m \u2264 10^5). The next line contains 2^{n} integers a_1, a_2, ..., a_2^{n} (0 \u2264 a_{i} < 2^30). Each of the next m lines contains queries. The i-th line contains integers p_{i}, b_{i} (1 \u2264 p_{i} \u2264 2^{n}, 0 \u2264 b_{i} < 2^30) \u2014 the i-th query.\n\n\n-----Output-----\n\nPrint m integers \u2014 the i-th integer denotes value v for sequence a after the i-th query.\n\n\n-----Examples-----\nInput\n2 4\n1 6 3 5\n1 4\n3 4\n1 2\n1 2\n\nOutput\n1\n3\n3\n3\n\n\n\n-----Note-----\n\nFor more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation"} +{"id": "02226", "text": "You are given a simple weighted connected undirected graph, consisting of $n$ vertices and $m$ edges.\n\nA path in the graph of length $k$ is a sequence of $k+1$ vertices $v_1, v_2, \\dots, v_{k+1}$ such that for each $i$ $(1 \\le i \\le k)$ the edge $(v_i, v_{i+1})$ is present in the graph. A path from some vertex $v$ also has vertex $v_1=v$. Note that edges and vertices are allowed to be included in the path multiple times.\n\nThe weight of the path is the total weight of edges in it.\n\nFor each $i$ from $1$ to $q$ consider a path from vertex $1$ of length $i$ of the maximum weight. What is the sum of weights of these $q$ paths?\n\nAnswer can be quite large, so print it modulo $10^9+7$.\n\n\n-----Input-----\n\nThe first line contains a three integers $n$, $m$, $q$ ($2 \\le n \\le 2000$; $n - 1 \\le m \\le 2000$; $m \\le q \\le 10^9$)\u00a0\u2014 the number of vertices in the graph, the number of edges in the graph and the number of lengths that should be included in the answer.\n\nEach of the next $m$ lines contains a description of an edge: three integers $v$, $u$, $w$ ($1 \\le v, u \\le n$; $1 \\le w \\le 10^6$)\u00a0\u2014 two vertices $v$ and $u$ are connected by an undirected edge with weight $w$. The graph contains no loops and no multiple edges. It is guaranteed that the given edges form a connected graph.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the sum of the weights of the paths from vertex $1$ of maximum weights of lengths $1, 2, \\dots, q$ modulo $10^9+7$.\n\n\n-----Examples-----\nInput\n7 8 25\n1 2 1\n2 3 10\n3 4 2\n1 5 2\n5 6 7\n6 4 15\n5 3 1\n1 7 3\n\nOutput\n4361\n\nInput\n2 1 5\n1 2 4\n\nOutput\n60\n\nInput\n15 15 23\n13 10 12\n11 14 12\n2 15 5\n4 10 8\n10 2 4\n10 7 5\n3 10 1\n5 6 11\n1 13 8\n9 15 4\n4 2 9\n11 15 1\n11 12 14\n10 8 12\n3 6 11\n\nOutput\n3250\n\nInput\n5 10 10000000\n2 4 798\n1 5 824\n5 2 558\n4 1 288\n3 4 1890\n3 1 134\n2 3 1485\n4 5 284\n3 5 1025\n1 2 649\n\nOutput\n768500592\n\n\n\n-----Note-----\n\nHere is the graph for the first example: [Image] \n\nSome maximum weight paths are: length $1$: edges $(1, 7)$\u00a0\u2014 weight $3$; length $2$: edges $(1, 2), (2, 3)$\u00a0\u2014 weight $1+10=11$; length $3$: edges $(1, 5), (5, 6), (6, 4)$\u00a0\u2014 weight $2+7+15=24$; length $4$: edges $(1, 5), (5, 6), (6, 4), (6, 4)$\u00a0\u2014 weight $2+7+15+15=39$; $\\dots$ \n\nSo the answer is the sum of $25$ terms: $3+11+24+39+\\dots$\n\nIn the second example the maximum weight paths have weights $4$, $8$, $12$, $16$ and $20$."} +{"id": "02227", "text": "Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.\n\nVolodya calls a string powerful if it starts with \"heavy\" and ends with \"metal\". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.\n\nFor simplicity, let us assume that Volodya's text can be represented as a single string.\n\n\n-----Input-----\n\nInput contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 10^6 characters.\n\n\n-----Output-----\n\nPrint exactly one number \u2014 the number of powerful substrings of the given string.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\nheavymetalisheavymetal\n\nOutput\n3\nInput\nheavymetalismetal\n\nOutput\n2\nInput\ntrueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou\n\nOutput\n3\n\n\n-----Note-----\n\nIn the first sample the string \"heavymetalisheavymetal\" contains powerful substring \"heavymetal\" twice, also the whole string \"heavymetalisheavymetal\" is certainly powerful.\n\nIn the second sample the string \"heavymetalismetal\" contains two powerful substrings: \"heavymetal\" and \"heavymetalismetal\"."} +{"id": "02228", "text": "During one of the space missions, humans have found an evidence of previous life at one of the planets. They were lucky enough to find a book with birth and death years of each individual that had been living at this planet. What's interesting is that these years are in the range $(1, 10^9)$! Therefore, the planet was named Longlifer.\n\nIn order to learn more about Longlifer's previous population, scientists need to determine the year with maximum number of individuals that were alive, as well as the number of alive individuals in that year. Your task is to help scientists solve this problem!\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the number of people.\n\nEach of the following $n$ lines contain two integers $b$ and $d$ ($1 \\le b \\lt d \\le 10^9$) representing birth and death year (respectively) of each individual.\n\n\n-----Output-----\n\nPrint two integer numbers separated by blank character, $y$ \u00a0\u2014 the year with a maximum number of people alive and $k$ \u00a0\u2014 the number of people alive in year $y$.\n\nIn the case of multiple possible solutions, print the solution with minimum year.\n\n\n-----Examples-----\nInput\n3\n1 5\n2 4\n5 6\n\nOutput\n2 2\n\nInput\n4\n3 4\n4 5\n4 6\n8 10\n\nOutput\n4 2\n\n\n\n-----Note-----\n\nYou can assume that an individual living from $b$ to $d$ has been born at the beginning of $b$ and died at the beginning of $d$, and therefore living for $d$ - $b$ years."} +{"id": "02229", "text": "Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same length such that:\n\n b is lexicographically greater than or equal to a. b_{i} \u2265 2. b is pairwise coprime: for every 1 \u2264 i < j \u2264 n, b_{i} and b_{j} are coprime, i.\u00a0e. GCD(b_{i}, b_{j}) = 1, where GCD(w, z) is the greatest common divisor of w and z. \n\nEhab wants to choose a special array so he wants the lexicographically minimal array between all the variants. Can you find it?\n\nAn array x is lexicographically greater than an array y if there exists an index i such than x_{i} > y_{i} and x_{j} = y_{j} for all 1 \u2264 j < i. An array x is equal to an array y if x_{i} = y_{i} for all 1 \u2264 i \u2264 n.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^5), the number of elements in a and b.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (2 \u2264 a_{i} \u2264 10^5), the elements of a.\n\n\n-----Output-----\n\nOutput n space-separated integers, the i-th of them representing b_{i}.\n\n\n-----Examples-----\nInput\n5\n2 3 5 4 13\n\nOutput\n2 3 5 7 11 \nInput\n3\n10 3 7\n\nOutput\n10 3 7 \n\n\n-----Note-----\n\nNote that in the second sample, the array is already pairwise coprime so we printed it."} +{"id": "02230", "text": "Gerald has n younger brothers and their number happens to be even. One day he bought n^2 candy bags. One bag has one candy, one bag has two candies, one bag has three candies and so on. In fact, for each integer k from 1 to n^2 he has exactly one bag with k candies. \n\nHelp him give n bags of candies to each brother so that all brothers got the same number of candies.\n\n\n-----Input-----\n\nThe single line contains a single integer n (n is even, 2 \u2264 n \u2264 100) \u2014 the number of Gerald's brothers.\n\n\n-----Output-----\n\nLet's assume that Gerald indexes his brothers with numbers from 1 to n. You need to print n lines, on the i-th line print n integers \u2014 the numbers of candies in the bags for the i-th brother. Naturally, all these numbers should be distinct and be within limits from 1 to n^2. You can print the numbers in the lines in any order. \n\nIt is guaranteed that the solution exists at the given limits.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1 4\n2 3\n\n\n\n-----Note-----\n\nThe sample shows Gerald's actions if he has two brothers. In this case, his bags contain 1, 2, 3 and 4 candies. He can give the bags with 1 and 4 candies to one brother and the bags with 2 and 3 to the other brother."} +{"id": "02231", "text": "You have $n$ sticks of the given lengths.\n\nYour task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.\n\nLet $S$ be the area of the rectangle and $P$ be the perimeter of the rectangle. \n\nThe chosen rectangle should have the value $\\frac{P^2}{S}$ minimal possible. The value is taken without any rounding.\n\nIf there are multiple answers, print any of them.\n\nEach testcase contains several lists of sticks, for each of them you are required to solve the problem separately.\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($T \\ge 1$) \u2014 the number of lists of sticks in the testcase.\n\nThen $2T$ lines follow \u2014 lines $(2i - 1)$ and $2i$ of them describe the $i$-th list. The first line of the pair contains a single integer $n$ ($4 \\le n \\le 10^6$) \u2014 the number of sticks in the $i$-th list. The second line of the pair contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_j \\le 10^4$) \u2014 lengths of the sticks in the $i$-th list.\n\nIt is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle.\n\nThe total number of sticks in all $T$ lists doesn't exceed $10^6$ in each testcase.\n\n\n-----Output-----\n\nPrint $T$ lines. The $i$-th line should contain the answer to the $i$-th list of the input. That is the lengths of the four sticks you choose from the $i$-th list, so that they form a rectangle and the value $\\frac{P^2}{S}$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order.\n\nIf there are multiple answers, print any of them.\n\n\n-----Example-----\nInput\n3\n4\n7 2 2 7\n8\n2 8 1 4 8 2 1 5\n5\n5 5 5 5 5\n\nOutput\n2 7 7 2\n2 2 1 1\n5 5 5 5\n\n\n\n-----Note-----\n\nThere is only one way to choose four sticks in the first list, they form a rectangle with sides $2$ and $7$, its area is $2 \\cdot 7 = 14$, perimeter is $2(2 + 7) = 18$. $\\frac{18^2}{14} \\approx 23.143$.\n\nThe second list contains subsets of four sticks that can form rectangles with sides $(1, 2)$, $(2, 8)$ and $(1, 8)$. Their values are $\\frac{6^2}{2} = 18$, $\\frac{20^2}{16} = 25$ and $\\frac{18^2}{8} = 40.5$, respectively. The minimal one of them is the rectangle $(1, 2)$.\n\nYou can choose any four of the $5$ given sticks from the third list, they will form a square with side $5$, which is still a rectangle with sides $(5, 5)$."} +{"id": "02232", "text": "You are given an undirected unweighted tree consisting of $n$ vertices.\n\nAn undirected tree is a connected undirected graph with $n - 1$ edges.\n\nYour task is to choose two pairs of vertices of this tree (all the chosen vertices should be distinct) $(x_1, y_1)$ and $(x_2, y_2)$ in such a way that neither $x_1$ nor $y_1$ belong to the simple path from $x_2$ to $y_2$ and vice versa (neither $x_2$ nor $y_2$ should not belong to the simple path from $x_1$ to $y_1$).\n\nIt is guaranteed that it is possible to choose such pairs for the given tree.\n\nAmong all possible ways to choose such pairs you have to choose one with the maximum number of common vertices between paths from $x_1$ to $y_1$ and from $x_2$ to $y_2$. And among all such pairs you have to choose one with the maximum total length of these two paths.\n\nIt is guaranteed that the answer with at least two common vertices exists for the given tree.\n\nThe length of the path is the number of edges in it.\n\nThe simple path is the path that visits each vertex at most once.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ \u2014 the number of vertices in the tree ($6 \\le n \\le 2 \\cdot 10^5$).\n\nEach of the next $n - 1$ lines describes the edges of the tree.\n\nEdge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects ($1 \\le u_i, v_i \\le n$, $u_i \\ne v_i$).\n\nIt is guaranteed that the given edges form a tree.\n\nIt is guaranteed that the answer with at least two common vertices exists for the given tree.\n\n\n-----Output-----\n\nPrint any two pairs of vertices satisfying the conditions described in the problem statement.\n\nIt is guaranteed that it is possible to choose such pairs for the given tree.\n\n\n-----Examples-----\nInput\n7\n1 4\n1 5\n1 6\n2 3\n2 4\n4 7\n\nOutput\n3 6\n7 5\n\nInput\n9\n9 3\n3 5\n1 2\n4 3\n4 7\n1 7\n4 6\n3 8\n\nOutput\n2 9\n6 8\n\nInput\n10\n6 8\n10 3\n3 7\n5 8\n1 7\n7 2\n2 9\n2 8\n1 4\n\nOutput\n10 6\n4 5\n\nInput\n11\n1 2\n2 3\n3 4\n1 5\n1 6\n6 7\n5 8\n5 9\n4 10\n4 11\n\nOutput\n9 11\n8 10\n\n\n\n-----Note-----\n\nThe picture corresponding to the first example: [Image]\n\nThe intersection of two paths is $2$ (vertices $1$ and $4$) and the total length is $4 + 3 = 7$.\n\nThe picture corresponding to the second example: [Image]\n\nThe intersection of two paths is $2$ (vertices $3$ and $4$) and the total length is $5 + 3 = 8$.\n\nThe picture corresponding to the third example: $88^{\\circ}$\n\nThe intersection of two paths is $3$ (vertices $2$, $7$ and $8$) and the total length is $5 + 5 = 10$.\n\nThe picture corresponding to the fourth example: $\\$ 8$\n\nThe intersection of two paths is $5$ (vertices $1$, $2$, $3$, $4$ and $5$) and the total length is $6 + 6 = 12$."} +{"id": "02233", "text": "This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.\n\nAfter struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.\n\nUjan has two distinct strings $s$ and $t$ of length $n$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $i$ and $j$ ($1 \\le i,j \\le n$, the values $i$ and $j$ can be equal or different), and swaps the characters $s_i$ and $t_j$. Can he succeed?\n\nNote that he has to perform this operation exactly once. He has to perform this operation.\n\n\n-----Input-----\n\nThe first line contains a single integer $k$ ($1 \\leq k \\leq 10$), the number of test cases.\n\nFor each of the test cases, the first line contains a single integer $n$ ($2 \\leq n \\leq 10^4$), the length of the strings $s$ and $t$. \n\nEach of the next two lines contains the strings $s$ and $t$, each having length exactly $n$. The strings consist only of lowercase English letters. It is guaranteed that strings are different.\n\n\n-----Output-----\n\nFor each test case, output \"Yes\" if Ujan can make the two strings equal and \"No\" otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca\n\nOutput\nYes\nNo\nNo\nNo\n\n\n\n-----Note-----\n\nIn the first test case, Ujan can swap characters $s_1$ and $t_4$, obtaining the word \"house\".\n\nIn the second test case, it is not possible to make the strings equal using exactly one swap of $s_i$ and $t_j$."} +{"id": "02234", "text": "We have a point $A$ with coordinate $x = n$ on $OX$-axis. We'd like to find an integer point $B$ (also on $OX$-axis), such that the absolute difference between the distance from $O$ to $B$ and the distance from $A$ to $B$ is equal to $k$. [Image] The description of the first test case. \n\nSince sometimes it's impossible to find such point $B$, we can, in one step, increase or decrease the coordinate of $A$ by $1$. What is the minimum number of steps we should do to make such point $B$ exist?\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 6000$)\u00a0\u2014 the number of test cases.\n\nThe only line of each test case contains two integers $n$ and $k$ ($0 \\le n, k \\le 10^6$)\u00a0\u2014 the initial position of point $A$ and desirable absolute difference.\n\n\n-----Output-----\n\nFor each test case, print the minimum number of steps to make point $B$ exist.\n\n\n-----Example-----\nInput\n6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000\n\nOutput\n0\n3\n1000000\n0\n1\n0\n\n\n\n-----Note-----\n\nIn the first test case (picture above), if we set the coordinate of $B$ as $2$ then the absolute difference will be equal to $|(2 - 0) - (4 - 2)| = 0$ and we don't have to move $A$. So the answer is $0$.\n\nIn the second test case, we can increase the coordinate of $A$ by $3$ and set the coordinate of $B$ as $0$ or $8$. The absolute difference will be equal to $|8 - 0| = 8$, so the answer is $3$. [Image]"} +{"id": "02235", "text": "A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare.\n\nThe fare is constructed in the following manner. There are three types of tickets: a ticket for one trip costs 20 byteland rubles, a ticket for 90 minutes costs 50 byteland rubles, a ticket for one day (1440 minutes) costs 120 byteland rubles. \n\nNote that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute.\n\nTo simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b.\n\nYou have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.\n\n\n-----Input-----\n\nThe first line of input contains integer number n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of trips made by passenger.\n\nEach of the following n lines contains the time of trip t_{i} (0 \u2264 t_{i} \u2264 10^9), measured in minutes from the time of starting the system. All t_{i} are different, given in ascending order, i.\u00a0e. t_{i} + 1 > t_{i} holds for all 1 \u2264 i < n.\n\n\n-----Output-----\n\nOutput n integers. For each trip, print the sum the passenger is charged after it.\n\n\n-----Examples-----\nInput\n3\n10\n20\n30\n\nOutput\n20\n20\n10\n\nInput\n10\n13\n45\n46\n60\n103\n115\n126\n150\n256\n516\n\nOutput\n20\n20\n10\n0\n20\n0\n0\n20\n20\n10\n\n\n\n-----Note-----\n\nIn the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only."} +{"id": "02236", "text": "There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.\n\nVasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.\n\nThere is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.\n\nVasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of banks.\n\nThe second line contains n integers a_{i} ( - 10^9 \u2264 a_{i} \u2264 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.\n\n\n-----Output-----\n\nPrint the minimum number of operations required to change balance in each bank to zero.\n\n\n-----Examples-----\nInput\n3\n5 0 -5\n\nOutput\n1\n\nInput\n4\n-1 0 1 0\n\nOutput\n2\n\nInput\n4\n1 2 3 -6\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample, Vasya may transfer 5 from the first bank to the third.\n\nIn the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.\n\nIn the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth."} +{"id": "02237", "text": "Kuro has just learned about permutations and he is really excited to create a new permutation type. He has chosen $n$ distinct positive integers and put all of them in a set $S$. Now he defines a magical permutation to be: A permutation of integers from $0$ to $2^x - 1$, where $x$ is a non-negative integer. The bitwise xor of any two consecutive elements in the permutation is an element in $S$.\n\nSince Kuro is really excited about magical permutations, he wants to create the longest magical permutation possible. In other words, he wants to find the largest non-negative integer $x$ such that there is a magical permutation of integers from $0$ to $2^x - 1$. Since he is a newbie in the subject, he wants you to help him find this value of $x$ and also the magical permutation for that $x$.\n\n\n-----Input-----\n\nThe first line contains the integer $n$ ($1 \\leq n \\leq 2 \\cdot 10^5$)\u00a0\u2014 the number of elements in the set $S$.\n\nThe next line contains $n$ distinct integers $S_1, S_2, \\ldots, S_n$ ($1 \\leq S_i \\leq 2 \\cdot 10^5$)\u00a0\u2014 the elements in the set $S$.\n\n\n-----Output-----\n\nIn the first line print the largest non-negative integer $x$, such that there is a magical permutation of integers from $0$ to $2^x - 1$.\n\nThen print $2^x$ integers describing a magical permutation of integers from $0$ to $2^x - 1$. If there are multiple such magical permutations, print any of them.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n2\n0 1 3 2 \nInput\n2\n2 3\n\nOutput\n2\n0 2 1 3 \nInput\n4\n1 2 3 4\n\nOutput\n3\n0 1 3 2 6 7 5 4 \nInput\n2\n2 4\n\nOutput\n0\n0 \nInput\n1\n20\n\nOutput\n0\n0 \nInput\n1\n1\n\nOutput\n1\n0 1 \n\n\n-----Note-----\n\nIn the first example, $0, 1, 3, 2$ is a magical permutation since: $0 \\oplus 1 = 1 \\in S$ $1 \\oplus 3 = 2 \\in S$ $3 \\oplus 2 = 1 \\in S$\n\nWhere $\\oplus$ denotes bitwise xor operation."} +{"id": "02238", "text": "Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n > 1) is an n \u00d7 n matrix with a diamond inscribed into it.\n\nYou are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character \"D\". All other cells of the matrix should be represented by character \"*\". Look at the examples to understand what you need to draw.\n\n\n-----Input-----\n\nThe only line contains an integer n (3 \u2264 n \u2264 101; n is odd). \n\n\n-----Output-----\n\nOutput a crystal of size n.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n*D*\nDDD\n*D*\n\nInput\n5\n\nOutput\n**D**\n*DDD*\nDDDDD\n*DDD*\n**D**\n\nInput\n7\n\nOutput\n***D***\n**DDD**\n*DDDDD*\nDDDDDDD\n*DDDDD*\n**DDD**\n***D***"} +{"id": "02239", "text": "Mishka got a six-faced dice. It has integer numbers from $2$ to $7$ written on its faces (all numbers on faces are different, so this is an almost usual dice).\n\nMishka wants to get exactly $x$ points by rolling his dice. The number of points is just a sum of numbers written at the topmost face of the dice for all the rolls Mishka makes.\n\nMishka doesn't really care about the number of rolls, so he just wants to know any number of rolls he can make to be able to get exactly $x$ points for them. Mishka is very lucky, so if the probability to get $x$ points with chosen number of rolls is non-zero, he will be able to roll the dice in such a way. Your task is to print this number. It is guaranteed that at least one answer exists.\n\nMishka is also very curious about different number of points to score so you have to answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of queries.\n\nEach of the next $t$ lines contains one integer each. The $i$-th line contains one integer $x_i$ ($2 \\le x_i \\le 100$) \u2014 the number of points Mishka wants to get.\n\n\n-----Output-----\n\nPrint $t$ lines. In the $i$-th line print the answer to the $i$-th query (i.e. any number of rolls Mishka can make to be able to get exactly $x_i$ points for them). It is guaranteed that at least one answer exists.\n\n\n-----Example-----\nInput\n4\n2\n13\n37\n100\n\nOutput\n1\n3\n8\n27\n\n\n-----Note-----\n\nIn the first query Mishka can roll a dice once and get $2$ points.\n\nIn the second query Mishka can roll a dice $3$ times and get points $5$, $5$ and $3$ (for example).\n\nIn the third query Mishka can roll a dice $8$ times and get $5$ points $7$ times and $2$ points with the remaining roll.\n\nIn the fourth query Mishka can roll a dice $27$ times and get $2$ points $11$ times, $3$ points $6$ times and $6$ points $10$ times."} +{"id": "02240", "text": "One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a signal $s$ towards a faraway galaxy. Recently they've received a response $t$ which they believe to be a response from aliens! The scientists now want to check if the signal $t$ is similar to $s$.\n\nThe original signal $s$ was a sequence of zeros and ones (everyone knows that binary code is the universe-wide language). The returned signal $t$, however, does not look as easy as $s$, but the scientists don't give up! They represented $t$ as a sequence of English letters and say that $t$ is similar to $s$ if you can replace all zeros in $s$ with some string $r_0$ and all ones in $s$ with some other string $r_1$ and obtain $t$. The strings $r_0$ and $r_1$ must be different and non-empty.\n\nPlease help Arkady's friend and find the number of possible replacements for zeros and ones (the number of pairs of strings $r_0$ and $r_1$) that transform $s$ to $t$.\n\n\n-----Input-----\n\nThe first line contains a string $s$ ($2 \\le |s| \\le 10^5$) consisting of zeros and ones\u00a0\u2014 the original signal.\n\nThe second line contains a string $t$ ($1 \\le |t| \\le 10^6$) consisting of lowercase English letters only\u00a0\u2014 the received signal.\n\nIt is guaranteed, that the string $s$ contains at least one '0' and at least one '1'.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of pairs of strings $r_0$ and $r_1$ that transform $s$ to $t$.\n\nIn case there are no such pairs, print $0$.\n\n\n-----Examples-----\nInput\n01\naaaaaa\n\nOutput\n4\n\nInput\n001\nkokokokotlin\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example, the possible pairs $(r_0, r_1)$ are as follows: \"a\", \"aaaaa\" \"aa\", \"aaaa\" \"aaaa\", \"aa\" \"aaaaa\", \"a\" \n\nThe pair \"aaa\", \"aaa\" is not allowed, since $r_0$ and $r_1$ must be different.\n\nIn the second example, the following pairs are possible: \"ko\", \"kokotlin\" \"koko\", \"tlin\""} +{"id": "02241", "text": "Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much. \n\nA song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 \u2264 v \u2264 a_{i}; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal b_{i}. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes x_{i} and y_{i} (x_{i} + y_{i} = b_{i}) correspondingly, Sereja's joy rises by x_{i}\u00b7y_{i}. \n\nSereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of notes in the song. The second line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^6). The third line contains n integers b_{i} (1 \u2264 b_{i} \u2264 10^6).\n\n\n-----Output-----\n\nIn a single line print an integer \u2014 the maximum possible joy Sereja feels after he listens to a song.\n\n\n-----Examples-----\nInput\n3\n1 1 2\n2 2 3\n\nOutput\n4\n\nInput\n1\n2\n5\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1\u00b71 + 1\u00b71 + 1\u00b72 = 4.\n\nIn the second sample, there is no such pair (x, y), that 1 \u2264 x, y \u2264 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1."} +{"id": "02242", "text": "Given is a string S consisting of digits from 1 through 9.\nFind the number of pairs of integers (i,j) (1 \u2264 i \u2264 j \u2264 |S|) that satisfy the following condition:\nCondition: In base ten, the i-th through j-th characters of S form an integer that is a multiple of 2019.\n\n-----Constraints-----\n - 1 \u2264 |S| \u2264 200000\n - S is a string consisting of digits from 1 through 9.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the number of pairs of integers (i,j) (1 \u2264 i \u2264 j \u2264 |S|) that satisfy the condition.\n\n-----Sample Input-----\n1817181712114\n\n-----Sample Output-----\n3\n\nThree pairs - (1,5), (5,9), and (9,13) - satisfy the condition."} +{"id": "02243", "text": "Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer t_{i}. The bigger this value is, the better the friendship is. No two friends have the same value t_{i}.\n\nSpring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.\n\nThe system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them\u00a0\u2014 those with biggest t_{i}.\n\nYour task is to handle queries of two types: \"1 id\"\u00a0\u2014 Friend id becomes online. It's guaranteed that he wasn't online before. \"2 id\"\u00a0\u2014 Check whether friend id is displayed by the system. Print \"YES\" or \"NO\" in a separate line. \n\nAre you able to help Limak and answer all queries of the second type?\n\n\n-----Input-----\n\nThe first line contains three integers n, k and q (1 \u2264 n, q \u2264 150 000, 1 \u2264 k \u2264 min(6, n))\u00a0\u2014 the number of friends, the maximum number of displayed online friends and the number of queries, respectively.\n\nThe second line contains n integers t_1, t_2, ..., t_{n} (1 \u2264 t_{i} \u2264 10^9) where t_{i} describes how good is Limak's relation with the i-th friend.\n\nThe i-th of the following q lines contains two integers type_{i} and id_{i} (1 \u2264 type_{i} \u2264 2, 1 \u2264 id_{i} \u2264 n)\u00a0\u2014 the i-th query. If type_{i} = 1 then a friend id_{i} becomes online. If type_{i} = 2 then you should check whether a friend id_{i} is displayed.\n\nIt's guaranteed that no two queries of the first type will have the same id_{i} becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (type_{i} = 2) so the output won't be empty.\n\n\n-----Output-----\n\nFor each query of the second type print one line with the answer\u00a0\u2014 \"YES\" (without quotes) if the given friend is displayed and \"NO\" (without quotes) otherwise.\n\n\n-----Examples-----\nInput\n4 2 8\n300 950 500 200\n1 3\n2 4\n2 3\n1 1\n1 2\n2 1\n2 2\n2 3\n\nOutput\nNO\nYES\nNO\nYES\nYES\n\nInput\n6 3 9\n50 20 51 17 99 24\n1 3\n1 4\n1 5\n1 2\n2 4\n2 2\n1 1\n2 4\n2 3\n\nOutput\nNO\nYES\nNO\nYES\n\n\n\n-----Note-----\n\nIn the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries: \"1 3\"\u00a0\u2014 Friend 3 becomes online. \"2 4\"\u00a0\u2014 We should check if friend 4 is displayed. He isn't even online and thus we print \"NO\". \"2 3\"\u00a0\u2014 We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print \"YES\". \"1 1\"\u00a0\u2014 Friend 1 becomes online. The system now displays both friend 1 and friend 3. \"1 2\"\u00a0\u2014 Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t_1 < t_2, t_3) so friend 1 won't be displayed \"2 1\"\u00a0\u2014 Print \"NO\". \"2 2\"\u00a0\u2014 Print \"YES\". \"2 3\"\u00a0\u2014 Print \"YES\"."} +{"id": "02244", "text": "The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else.\n\nToday is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another.\n\nHowever, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist.\n\n\n-----Input-----\n\nThe first line contains space-separated integers n and m $(2 \\leq n \\leq 3 \\cdot 10^{4} ; 1 \\leq m \\leq \\operatorname{min}(10^{5}, \\frac{n(n - 1)}{2}))$ \u2014 the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n;\u00a0a_{i} \u2260 b_{i}), these integers indicate that the person number a_{i} owes money to a person a number b_{i}. Assume that all the employees are numbered from 1 to n.\n\nIt is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously.\n\n\n-----Output-----\n\nPrint -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on.\n\nIf there are multiple correct orders, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n2 1\n1 2\n\nOutput\n2 1 \n\nInput\n3 3\n1 2\n2 3\n3 1\n\nOutput\n2 1 3"} +{"id": "02245", "text": "Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one).\n\nPlayers take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell i - 1, i - 2 or i - k). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number i < k. The player who can't make a move loses the game.\n\nWho wins if both participants play optimally?\n\nAlice and Bob would like to play several games, so you should determine the winner in each game.\n\n\n-----Input-----\n\nThe first line contains the single integer T (1 \u2264 T \u2264 100) \u2014 the number of games. Next T lines contain one game per line. All games are independent.\n\nEach of the next T lines contains two integers n and k (0 \u2264 n \u2264 10^9, 3 \u2264 k \u2264 10^9) \u2014 the length of the strip and the constant denoting the third move, respectively.\n\n\n-----Output-----\n\nFor each game, print Alice if Alice wins this game and Bob otherwise.\n\n\n-----Example-----\nInput\n4\n0 3\n3 3\n3 4\n4 4\n\nOutput\nBob\nAlice\nBob\nAlice"} +{"id": "02246", "text": "There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.\n\nTheon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can\u2019t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. \n\nLet the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://en.wikipedia.org/wiki/Expected_value.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100000)\u00a0\u2014 number of cities.\n\nThen n - 1 lines follow. The i-th line of these lines contains two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i})\u00a0\u2014 the cities connected by the i-th road.\n\nIt is guaranteed that one can reach any city from any other by the roads.\n\n\n-----Output-----\n\nPrint a number\u00a0\u2014 the expected length of their journey. The journey starts in the city 1.\n\nYour answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}.\n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n4\n1 2\n1 3\n2 4\n\nOutput\n1.500000000000000\n\nInput\n5\n1 2\n1 3\n3 4\n2 5\n\nOutput\n2.000000000000000\n\n\n\n-----Note-----\n\nIn the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5.\n\nIn the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2."} +{"id": "02247", "text": "There is a special offer in Vasya's favourite supermarket: if the customer buys $a$ chocolate bars, he or she may take $b$ additional bars for free. This special offer can be used any number of times.\n\nVasya currently has $s$ roubles, and he wants to get as many chocolate bars for free. Each chocolate bar costs $c$ roubles. Help Vasya to calculate the maximum possible number of chocolate bars he can get!\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of testcases.\n\nEach of the next $t$ lines contains four integers $s, a, b, c~(1 \\le s, a, b, c \\le 10^9)$ \u2014 the number of roubles Vasya has, the number of chocolate bars you have to buy to use the special offer, the number of bars you get for free, and the cost of one bar, respectively.\n\n\n-----Output-----\n\nPrint $t$ lines. $i$-th line should contain the maximum possible number of chocolate bars Vasya can get in $i$-th test.\n\n\n-----Example-----\nInput\n2\n10 3 1 1\n1000000000 1 1000000000 1\n\nOutput\n13\n1000000001000000000\n\n\n\n-----Note-----\n\nIn the first test of the example Vasya can buy $9$ bars, get $3$ for free, buy another bar, and so he will get $13$ bars.\n\nIn the second test Vasya buys $1000000000$ bars and gets $1000000000000000000$ for free. So he has $1000000001000000000$ bars."} +{"id": "02248", "text": "Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of n brains and m brain connectors joining some pairs of brains together. It was observed that the intellectual abilities of a zombie depend mainly on the topology of its nervous system. More precisely, we define the distance between two brains u and v (1 \u2264 u, v \u2264 n) as the minimum number of brain connectors used when transmitting a thought between these two brains. The brain latency of a zombie is defined to be the maximum distance between any two of its brains. Researchers conjecture that the brain latency is the crucial parameter which determines how smart a given zombie is. Help them test this conjecture by writing a program to compute brain latencies of nervous systems.\n\nIn this problem you may assume that any nervous system given in the input is valid, i.e., it satisfies conditions (1) and (2) from the easy version.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and m (1 \u2264 n, m \u2264 100000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains a\u2002b it connects (1 \u2264 a, b \u2264 n and a \u2260 b).\n\n\n-----Output-----\n\nPrint one number \u2013 the brain latency.\n\n\n-----Examples-----\nInput\n4 3\n1 2\n1 3\n1 4\n\nOutput\n2\nInput\n5 4\n1 2\n2 3\n3 4\n3 5\n\nOutput\n3"} +{"id": "02249", "text": "Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.\n\nSonya has drawn $n$ numbers in a row, $a_i$ is located in the $i$-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sonya will give a number to each robot (they can be either same or different) and run them. When a robot is running, it is moving toward to another robot, reading numbers in the row. When a robot is reading a number that is equal to the number that was given to that robot, it will turn off and stay in the same position.\n\nSonya does not want robots to break, so she will give such numbers that robots will stop before they meet. That is, the girl wants them to stop at different positions so that the first robot is to the left of the second one.\n\nFor example, if the numbers $[1, 5, 4, 1, 3]$ are written, and Sonya gives the number $1$ to the first robot and the number $4$ to the second one, the first robot will stop in the $1$-st position while the second one in the $3$-rd position. In that case, robots will not meet each other. As a result, robots will not be broken. But if Sonya gives the number $4$ to the first robot and the number $5$ to the second one, they will meet since the first robot will stop in the $3$-rd position while the second one is in the $2$-nd position.\n\nSonya understands that it does not make sense to give a number that is not written in the row because a robot will not find this number and will meet the other robot.\n\nSonya is now interested in finding the number of different pairs that she can give to robots so that they will not meet. In other words, she wants to know the number of pairs ($p$, $q$), where she will give $p$ to the first robot and $q$ to the second one. Pairs ($p_i$, $q_i$) and ($p_j$, $q_j$) are different if $p_i\\neq p_j$ or $q_i\\neq q_j$.\n\nUnfortunately, Sonya is busy fixing robots that broke after a failed launch. That is why she is asking you to find the number of pairs that she can give to robots so that they will not meet.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1\\leq n\\leq 10^5$)\u00a0\u2014 the number of numbers in a row.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1\\leq a_i\\leq 10^5$)\u00a0\u2014 the numbers in a row.\n\n\n-----Output-----\n\nPrint one number\u00a0\u2014 the number of possible pairs that Sonya can give to robots so that they will not meet.\n\n\n-----Examples-----\nInput\n5\n1 5 4 1 3\n\nOutput\n9\n\nInput\n7\n1 2 1 1 1 3 2\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first example, Sonya can give pairs ($1$, $1$), ($1$, $3$), ($1$, $4$), ($1$, $5$), ($4$, $1$), ($4$, $3$), ($5$, $1$), ($5$, $3$), and ($5$, $4$).\n\nIn the second example, Sonya can give pairs ($1$, $1$), ($1$, $2$), ($1$, $3$), ($2$, $1$), ($2$, $2$), ($2$, $3$), and ($3$, $2$)."} +{"id": "02250", "text": "Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \\leq j \\leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$.\n\nCurrently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players.\n\nUnfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player \u00a0\u2014 i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. \n\nOmkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^4$). The descriptions of the test cases follows.\n\nThe first line of each test case contains one integer $n$ ($3 \\leq n \\leq 2 \\cdot 10^5$) \u00a0\u2014 the amount of players (and therefore beds) in this game of Bed Wars.\n\nThe second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy.\n\nIt can be proven that it is always possible for Omkar to achieve this under the given constraints.\n\n\n-----Example-----\nInput\n5\n4\nRLRL\n6\nLRRRRL\n8\nRLLRRRLL\n12\nLLLLRRLRRRLL\n5\nRRRRR\n\nOutput\n0\n1\n1\n3\n2\n\n\n\n-----Note-----\n\nIn the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$.\n\nIn the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$."} +{"id": "02251", "text": "Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color c_{i}, connecting vertex a_{i} and b_{i}.\n\nMr. Kitayuta wants you to process the following q queries.\n\nIn the i-th query, he gives you two integers \u2014 u_{i} and v_{i}.\n\nFind the number of the colors that satisfy the following condition: the edges of that color connect vertex u_{i} and vertex v_{i} directly or indirectly.\n\n\n-----Input-----\n\nThe first line of the input contains space-separated two integers \u2014 n and m (2 \u2264 n \u2264 100, 1 \u2264 m \u2264 100), denoting the number of the vertices and the number of the edges, respectively.\n\nThe next m lines contain space-separated three integers \u2014 a_{i}, b_{i} (1 \u2264 a_{i} < b_{i} \u2264 n) and c_{i} (1 \u2264 c_{i} \u2264 m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i \u2260 j, (a_{i}, b_{i}, c_{i}) \u2260 (a_{j}, b_{j}, c_{j}).\n\nThe next line contains a integer \u2014 q (1 \u2264 q \u2264 100), denoting the number of the queries.\n\nThen follows q lines, containing space-separated two integers \u2014 u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n). It is guaranteed that u_{i} \u2260 v_{i}.\n\n\n-----Output-----\n\nFor each query, print the answer in a separate line.\n\n\n-----Examples-----\nInput\n4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4\n\nOutput\n2\n1\n0\n\nInput\n5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4\n\nOutput\n1\n1\n1\n1\n2\n\n\n\n-----Note-----\n\nLet's consider the first sample. [Image] The figure above shows the first sample. Vertex 1 and vertex 2 are connected by color 1 and 2. Vertex 3 and vertex 4 are connected by color 3. Vertex 1 and vertex 4 are not connected by any single color."} +{"id": "02252", "text": "Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p_1, p_2, ..., p_{n}], where p_{i} denotes the number of page that should be read i-th in turn.\n\nSometimes Vladik\u2019s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x\u00a0\u2014 what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has p_{x} changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.\n\n\n-----Input-----\n\nFirst line contains two space-separated integers n, m (1 \u2264 n, m \u2264 10^4)\u00a0\u2014 length of permutation and number of times Vladik's mom sorted some subsegment of the book.\n\nSecond line contains n space-separated integers p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 n)\u00a0\u2014 permutation P. Note that elements in permutation are distinct.\n\nEach of the next m lines contains three space-separated integers l_{i}, r_{i}, x_{i} (1 \u2264 l_{i} \u2264 x_{i} \u2264 r_{i} \u2264 n)\u00a0\u2014 left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.\n\n\n-----Output-----\n\nFor each mom\u2019s sorting on it\u2019s own line print \"Yes\", if page which is interesting to Vladik hasn't changed, or \"No\" otherwise.\n\n\n-----Examples-----\nInput\n5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3\n\nOutput\nYes\nNo\nYes\nYes\nNo\n\nInput\n6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3\n\nOutput\nYes\nNo\nYes\nNo\nYes\n\n\n\n-----Note-----\n\nExplanation of first test case: [1, 2, 3, 4, 5]\u00a0\u2014 permutation after sorting, 3-rd element hasn\u2019t changed, so answer is \"Yes\". [3, 4, 5, 2, 1]\u00a0\u2014 permutation after sorting, 1-st element has changed, so answer is \"No\". [5, 2, 3, 4, 1]\u00a0\u2014 permutation after sorting, 3-rd element hasn\u2019t changed, so answer is \"Yes\". [5, 4, 3, 2, 1]\u00a0\u2014 permutation after sorting, 4-th element hasn\u2019t changed, so answer is \"Yes\". [5, 1, 2, 3, 4]\u00a0\u2014 permutation after sorting, 3-rd element has changed, so answer is \"No\"."} +{"id": "02253", "text": "We just discovered a new data structure in our research group: a suffix three!\n\nIt's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in.\n\nIt's super simple, 100% accurate, and doesn't involve advanced machine learning algorithms.\n\nLet us tell you how it works.\n\n If a sentence ends with \"po\" the language is Filipino. If a sentence ends with \"desu\" or \"masu\" the language is Japanese. If a sentence ends with \"mnida\" the language is Korean. \n\nGiven this, we need you to implement a suffix three that can differentiate Filipino, Japanese, and Korean.\n\nOh, did I say three suffixes? I meant four.\n\n\n-----Input-----\n\nThe first line of input contains a single integer $t$ ($1 \\leq t \\leq 30$) denoting the number of test cases. The next lines contain descriptions of the test cases. \n\nEach test case consists of a single line containing a single string denoting the sentence. Spaces are represented as underscores (the symbol \"_\") for ease of reading. The sentence has at least $1$ and at most $1000$ characters, and consists only of lowercase English letters and underscores. The sentence has no leading or trailing underscores and no two consecutive underscores. It is guaranteed that the sentence ends with one of the four suffixes mentioned above.\n\n\n-----Output-----\n\nFor each test case, print a single line containing either \"FILIPINO\", \"JAPANESE\", or \"KOREAN\" (all in uppercase, without quotes), depending on the detected language.\n\n\n-----Example-----\nInput\n8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_mustang_ay_namamasu\n\nOutput\nFILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n\n\n\n-----Note-----\n\nThe first sentence ends with \"po\", so it is written in Filipino.\n\nThe second and third sentences end with \"desu\" and \"masu\", so they are written in Japanese.\n\nThe fourth sentence ends with \"mnida\", so it is written in Korean."} +{"id": "02254", "text": "Vasya has a sequence $a$ consisting of $n$ integers $a_1, a_2, \\dots, a_n$. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number $6$ $(\\dots 00000000110_2)$ into $3$ $(\\dots 00000000011_2)$, $12$ $(\\dots 000000001100_2)$, $1026$ $(\\dots 10000000010_2)$ and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence.\n\nVasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with bitwise exclusive or of all elements equal to $0$.\n\nFor the given sequence $a_1, a_2, \\ldots, a_n$ Vasya'd like to calculate number of integer pairs $(l, r)$ such that $1 \\le l \\le r \\le n$ and sequence $a_l, a_{l + 1}, \\dots, a_r$ is good.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 3 \\cdot 10^5$) \u2014 length of the sequence.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^{18}$) \u2014 the sequence $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of pairs $(l, r)$ such that $1 \\le l \\le r \\le n$ and the sequence $a_l, a_{l + 1}, \\dots, a_r$ is good.\n\n\n-----Examples-----\nInput\n3\n6 7 14\n\nOutput\n2\n\nInput\n4\n1 2 1 16\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example pairs $(2, 3)$ and $(1, 3)$ are valid. Pair $(2, 3)$ is valid since $a_2 = 7 \\rightarrow 11$, $a_3 = 14 \\rightarrow 11$ and $11 \\oplus 11 = 0$, where $\\oplus$ \u2014 bitwise exclusive or. Pair $(1, 3)$ is valid since $a_1 = 6 \\rightarrow 3$, $a_2 = 7 \\rightarrow 13$, $a_3 = 14 \\rightarrow 14$ and $3 \\oplus 13 \\oplus 14 = 0$.\n\nIn the second example pairs $(1, 2)$, $(2, 3)$, $(3, 4)$ and $(1, 4)$ are valid."} +{"id": "02255", "text": "Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.\n\nThe park can be represented as a connected graph with $n$ nodes and $m$ bidirectional edges. Initially Bob is at the node $1$ and he records $1$ on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes $a_1, a_2, \\ldots, a_n$ is recorded.\n\nWandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.\n\nA sequence $x$ is lexicographically smaller than a sequence $y$ if and only if one of the following holds: $x$ is a prefix of $y$, but $x \\ne y$ (this is impossible in this problem as all considered sequences have the same length); in the first position where $x$ and $y$ differ, the sequence $x$ has a smaller element than the corresponding element in $y$. \n\n\n-----Input-----\n\nThe first line contains two positive integers $n$ and $m$ ($1 \\leq n, m \\leq 10^5$), denoting the number of nodes and edges, respectively.\n\nThe following $m$ lines describe the bidirectional edges in the graph. The $i$-th of these lines contains two integers $u_i$ and $v_i$ ($1 \\leq u_i, v_i \\leq n$), representing the nodes the $i$-th edge connects.\n\nNote that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.\n\n\n-----Output-----\n\nOutput a line containing the lexicographically smallest sequence $a_1, a_2, \\ldots, a_n$ Bob can record.\n\n\n-----Examples-----\nInput\n3 2\n1 2\n1 3\n\nOutput\n1 2 3 \n\nInput\n5 5\n1 4\n3 4\n5 4\n3 2\n1 5\n\nOutput\n1 4 3 2 5 \n\nInput\n10 10\n1 4\n6 8\n2 5\n3 7\n9 4\n5 6\n3 4\n8 10\n8 9\n1 10\n\nOutput\n1 4 3 7 9 8 6 5 2 10 \n\n\n\n-----Note-----\n\nIn the first sample, Bob's optimal wandering path could be $1 \\rightarrow 2 \\rightarrow 1 \\rightarrow 3$. Therefore, Bob will obtain the sequence $\\{1, 2, 3\\}$, which is the lexicographically smallest one.\n\nIn the second sample, Bob's optimal wandering path could be $1 \\rightarrow 4 \\rightarrow 3 \\rightarrow 2 \\rightarrow 3 \\rightarrow 4 \\rightarrow 1 \\rightarrow 5$. Therefore, Bob will obtain the sequence $\\{1, 4, 3, 2, 5\\}$, which is the lexicographically smallest one."} +{"id": "02256", "text": "You are the gym teacher in the school.\n\nThere are $n$ students in the row. And there are two rivalling students among them. The first one is in position $a$, the second in position $b$. Positions are numbered from $1$ to $n$ from left to right.\n\nSince they are rivals, you want to maximize the distance between them. If students are in positions $p$ and $s$ respectively, then distance between them is $|p - s|$. \n\nYou can do the following operation at most $x$ times: choose two adjacent (neighbouring) students and swap them.\n\nCalculate the maximum distance between two rivalling students after at most $x$ swaps.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThe only line of each test case contains four integers $n$, $x$, $a$ and $b$ ($2 \\le n \\le 100$, $0 \\le x \\le 100$, $1 \\le a, b \\le n$, $a \\neq b$) \u2014 the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.\n\n\n-----Output-----\n\nFor each test case print one integer \u2014 the maximum distance between two rivaling students which you can obtain.\n\n\n-----Example-----\nInput\n3\n5 1 3 2\n100 33 100 1\n6 0 2 3\n\nOutput\n2\n99\n1\n\n\n\n-----Note-----\n\nIn the first test case you can swap students in positions $3$ and $4$. And then the distance between the rivals is equal to $|4 - 2| = 2$.\n\nIn the second test case you don't have to swap students. \n\nIn the third test case you can't swap students."} +{"id": "02257", "text": "A flowerbed has many flowers and two fountains.\n\nYou can adjust the water pressure and set any values r_1(r_1 \u2265 0) and r_2(r_2 \u2265 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r_1 and r_2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r_1, or the distance to the second fountain doesn't exceed r_2. It's OK if some flowers are watered by both fountains.\n\nYou need to decrease the amount of water you need, that is set such r_1 and r_2 that all the flowers are watered and the r_1^2 + r_2^2 is minimum possible. Find this minimum value.\n\n\n-----Input-----\n\nThe first line of the input contains integers n, x_1, y_1, x_2, y_2 (1 \u2264 n \u2264 2000, - 10^7 \u2264 x_1, y_1, x_2, y_2 \u2264 10^7)\u00a0\u2014 the number of flowers, the coordinates of the first and the second fountain.\n\nNext follow n lines. The i-th of these lines contains integers x_{i} and y_{i} ( - 10^7 \u2264 x_{i}, y_{i} \u2264 10^7)\u00a0\u2014 the coordinates of the i-th flower.\n\nIt is guaranteed that all n + 2 points in the input are distinct.\n\n\n-----Output-----\n\nPrint the minimum possible value r_1^2 + r_2^2. Note, that in this problem optimal answer is always integer.\n\n\n-----Examples-----\nInput\n2 -1 0 5 3\n0 2\n5 2\n\nOutput\n6\n\nInput\n4 0 0 5 0\n9 4\n8 3\n-1 0\n1 4\n\nOutput\n33\n\n\n\n-----Note-----\n\nThe first sample is (r_1^2 = 5, r_2^2 = 1): $0^{\\circ}$ The second sample is (r_1^2 = 1, r_2^2 = 32): [Image]"} +{"id": "02258", "text": "Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if:\n\n $1 \\le u < v \\le n$. $a_u > a_v$. \n\nMadeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions:\n\n all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. \n\nConstruct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($1 \\le n \\le 1000$) \u2014 the length of the array.\n\nNext line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \\le a_i \\le 10^9)$ \u00a0\u2014 elements of the array.\n\n\n-----Output-----\n\nPrint -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \\le m \\le \\dfrac{n(n-1)}{2}$) \u2014 number of pairs in the list.\n\nThe $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \\le u_i < v_i\\le n$).\n\nIf there are multiple possible answers, you may find any of them.\n\n\n-----Examples-----\nInput\n3\n3 1 2\n\nOutput\n2\n1 3\n1 2\n\nInput\n4\n1 8 1 6\n\nOutput\n2\n2 4\n2 3\n\nInput\n5\n1 1 1 2 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample test case the array will change in this order $[3,1,2] \\rightarrow [2,1,3] \\rightarrow [1,2,3]$.\n\nIn the second sample test case it will be $[1,8,1,6] \\rightarrow [1,6,1,8] \\rightarrow [1,1,6,8]$.\n\nIn the third sample test case the array is already sorted."} +{"id": "02259", "text": "Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). \n\nprocedure bubbleSortGraph()\n\n build a graph G with n vertices and 0 edges\n\n repeat\n\n swapped = false\n\n for i = 1 to n - 1 inclusive do:\n\n if a[i] > a[i + 1] then\n\n add an undirected edge in G between a[i] and a[i + 1]\n\n swap( a[i], a[i + 1] )\n\n swapped = true\n\n end if\n\n end for\n\n until not swapped \n\n /* repeat the algorithm as long as swapped value is true. */ \n\nend procedure\n\n\n\nFor a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph.\n\n\n-----Input-----\n\nThe first line of the input contains an integer n (2 \u2264 n \u2264 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n).\n\n\n-----Output-----\n\nOutput a single integer \u2014 the answer to the problem. \n\n\n-----Examples-----\nInput\n3\n3 1 2\n\nOutput\n2\n\n\n\n-----Note-----\n\nConsider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]."} +{"id": "02260", "text": "The HR manager was disappointed again. The last applicant failed the interview the same way as 24 previous ones. \"Do I give such a hard task?\" \u2014 the HR manager thought. \"Just raise number 5 to the power of n and get last two digits of the number. Yes, of course, n can be rather big, and one cannot find the power using a calculator, but we need people who are able to think, not just follow the instructions.\"\n\nCould you pass the interview in the machine vision company in IT City?\n\n\n-----Input-----\n\nThe only line of the input contains a single integer n (2 \u2264 n \u2264 2\u00b710^18) \u2014 the power in which you need to raise number 5.\n\n\n-----Output-----\n\nOutput the last two digits of 5^{n} without spaces between them.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n25"} +{"id": "02261", "text": "The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or - 1 and any two vectors are orthogonal. Just as a reminder, two vectors in n-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is: $\\sum_{i = 1}^{n} a_{i} \\cdot b_{i} = 0$.\n\nDanil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2^{k} vectors in 2^{k}-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it?\n\n\n-----Input-----\n\nThe only line of the input contains a single integer k (0 \u2264 k \u2264 9).\n\n\n-----Output-----\n\nPrint 2^{k} lines consisting of 2^{k} characters each. The j-th character of the i-th line must be equal to ' * ' if the j-th coordinate of the i-th vector is equal to - 1, and must be equal to ' + ' if it's equal to + 1. It's guaranteed that the answer always exists.\n\nIf there are many correct answers, print any.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n++**\n+*+*\n++++\n+**+\n\n\n-----Note-----\n\nConsider all scalar products in example: Vectors 1 and 2: ( + 1)\u00b7( + 1) + ( + 1)\u00b7( - 1) + ( - 1)\u00b7( + 1) + ( - 1)\u00b7( - 1) = 0 Vectors 1 and 3: ( + 1)\u00b7( + 1) + ( + 1)\u00b7( + 1) + ( - 1)\u00b7( + 1) + ( - 1)\u00b7( + 1) = 0 Vectors 1 and 4: ( + 1)\u00b7( + 1) + ( + 1)\u00b7( - 1) + ( - 1)\u00b7( - 1) + ( - 1)\u00b7( + 1) = 0 Vectors 2 and 3: ( + 1)\u00b7( + 1) + ( - 1)\u00b7( + 1) + ( + 1)\u00b7( + 1) + ( - 1)\u00b7( + 1) = 0 Vectors 2 and 4: ( + 1)\u00b7( + 1) + ( - 1)\u00b7( - 1) + ( + 1)\u00b7( - 1) + ( - 1)\u00b7( + 1) = 0 Vectors 3 and 4: ( + 1)\u00b7( + 1) + ( + 1)\u00b7( - 1) + ( + 1)\u00b7( - 1) + ( + 1)\u00b7( + 1) = 0"} +{"id": "02262", "text": "In Aramic language words can only represent objects.\n\nWords in Aramic have special properties: A word is a root if it does not contain the same letter more than once. A root and all its permutations represent the same object. The root $x$ of a word $y$ is the word that contains all letters that appear in $y$ in a way that each letter appears once. For example, the root of \"aaaa\", \"aa\", \"aaa\" is \"a\", the root of \"aabb\", \"bab\", \"baabb\", \"ab\" is \"ab\". Any word in Aramic represents the same object as its root. \n\nYou have an ancient script in Aramic. What is the number of different objects mentioned in the script?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\leq n \\leq 10^3$)\u00a0\u2014 the number of words in the script.\n\nThe second line contains $n$ words $s_1, s_2, \\ldots, s_n$\u00a0\u2014 the script itself. The length of each string does not exceed $10^3$.\n\nIt is guaranteed that all characters of the strings are small latin letters.\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 the number of different objects mentioned in the given ancient Aramic script.\n\n\n-----Examples-----\nInput\n5\na aa aaa ab abb\n\nOutput\n2\nInput\n3\namer arem mrea\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first test, there are two objects mentioned. The roots that represent them are \"a\",\"ab\".\n\nIn the second test, there is only one object, its root is \"amer\", the other strings are just permutations of \"amer\"."} +{"id": "02263", "text": "New Year is coming in Tree World! In this world, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to n, and the roads are numbered by integers from 1 to n - 1. Let's define d(u, v) as total length of roads on the path between city u and city v.\n\nAs an annual event, people in Tree World repairs exactly one road per year. As a result, the length of one road decreases. It is already known that in the i-th year, the length of the r_{i}-th road is going to become w_{i}, which is shorter than its length before. Assume that the current year is year 1.\n\nThree Santas are planning to give presents annually to all the children in Tree World. In order to do that, they need some preparation, so they are going to choose three distinct cities c_1, c_2, c_3 and make exactly one warehouse in each city. The k-th (1 \u2264 k \u2264 3) Santa will take charge of the warehouse in city c_{k}.\n\nIt is really boring for the three Santas to keep a warehouse alone. So, they decided to build an only-for-Santa network! The cost needed to build this network equals to d(c_1, c_2) + d(c_2, c_3) + d(c_3, c_1) dollars. Santas are too busy to find the best place, so they decided to choose c_1, c_2, c_3 randomly uniformly over all triples of distinct numbers from 1 to n. Santas would like to know the expected value of the cost needed to build the network.\n\nHowever, as mentioned, each year, the length of exactly one road decreases. So, the Santas want to calculate the expected after each length change. Help them to calculate the value.\n\n\n-----Input-----\n\nThe first line contains an integer n (3 \u2264 n \u2264 10^5) \u2014 the number of cities in Tree World.\n\nNext n - 1 lines describe the roads. The i-th line of them (1 \u2264 i \u2264 n - 1) contains three space-separated integers a_{i}, b_{i}, l_{i} (1 \u2264 a_{i}, b_{i} \u2264 n, a_{i} \u2260 b_{i}, 1 \u2264 l_{i} \u2264 10^3), denoting that the i-th road connects cities a_{i} and b_{i}, and the length of i-th road is l_{i}.\n\nThe next line contains an integer q (1 \u2264 q \u2264 10^5) \u2014 the number of road length changes.\n\nNext q lines describe the length changes. The j-th line of them (1 \u2264 j \u2264 q) contains two space-separated integers r_{j}, w_{j} (1 \u2264 r_{j} \u2264 n - 1, 1 \u2264 w_{j} \u2264 10^3). It means that in the j-th repair, the length of the r_{j}-th road becomes w_{j}. It is guaranteed that w_{j} is smaller than the current length of the r_{j}-th road. The same road can be repaired several times.\n\n\n-----Output-----\n\nOutput q numbers. For each given change, print a line containing the expected cost needed to build the network in Tree World. The answer will be considered correct if its absolute and relative error doesn't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n3\n2 3 5\n1 3 3\n5\n1 4\n2 2\n1 2\n2 1\n1 1\n\nOutput\n14.0000000000\n12.0000000000\n8.0000000000\n6.0000000000\n4.0000000000\n\nInput\n6\n1 5 3\n5 3 2\n6 1 7\n1 4 4\n5 2 3\n5\n1 2\n2 1\n3 5\n4 1\n5 2\n\nOutput\n19.6000000000\n18.6000000000\n16.6000000000\n13.6000000000\n12.6000000000\n\n\n\n-----Note-----\n\nConsider the first sample. There are 6 triples: (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1). Because n = 3, the cost needed to build the network is always d(1, 2) + d(2, 3) + d(3, 1) for all the triples. So, the expected cost equals to d(1, 2) + d(2, 3) + d(3, 1)."} +{"id": "02264", "text": "Your math teacher gave you the following problem:\n\nThere are $n$ segments on the $x$-axis, $[l_1; r_1], [l_2; r_2], \\ldots, [l_n; r_n]$. The segment $[l; r]$ includes the bounds, i.e. it is a set of such $x$ that $l \\le x \\le r$. The length of the segment $[l; r]$ is equal to $r - l$.\n\nTwo segments $[a; b]$ and $[c; d]$ have a common point (intersect) if there exists $x$ that $a \\leq x \\leq b$ and $c \\leq x \\leq d$. For example, $[2; 5]$ and $[3; 10]$ have a common point, but $[5; 6]$ and $[1; 4]$ don't have.\n\nYou should add one segment, which has at least one common point with each of the given segments and as short as possible (i.e. has minimal length). The required segment can degenerate to be a point (i.e a segment with length zero). The added segment may or may not be among the given $n$ segments.\n\nIn other words, you need to find a segment $[a; b]$, such that $[a; b]$ and every $[l_i; r_i]$ have a common point for each $i$, and $b-a$ is minimal.\n\n\n-----Input-----\n\nThe first line contains integer number $t$ ($1 \\le t \\le 100$)\u00a0\u2014 the number of test cases in the input. Then $t$ test cases follow.\n\nThe first line of each test case contains one integer $n$ ($1 \\le n \\le 10^{5}$)\u00a0\u2014 the number of segments. The following $n$ lines contain segment descriptions: the $i$-th of them contains two integers $l_i,r_i$ ($1 \\le l_i \\le r_i \\le 10^{9}$).\n\nThe sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case, output one integer\u00a0\u2014 the smallest possible length of the segment which has at least one common point with all given segments.\n\n\n-----Example-----\nInput\n4\n3\n4 5\n5 9\n7 7\n5\n11 19\n4 17\n16 16\n3 12\n14 17\n1\n1 10\n1\n1 1\n\nOutput\n2\n4\n0\n0\n\n\n\n-----Note-----\n\nIn the first test case of the example, we can choose the segment $[5;7]$ as the answer. It is the shortest segment that has at least one common point with all given segments."} +{"id": "02265", "text": "Vus the Cossack has two binary strings, that is, strings that consist only of \"0\" and \"1\". We call these strings $a$ and $b$. It is known that $|b| \\leq |a|$, that is, the length of $b$ is at most the length of $a$.\n\nThe Cossack considers every substring of length $|b|$ in string $a$. Let's call this substring $c$. He matches the corresponding characters in $b$ and $c$, after which he counts the number of positions where the two strings are different. We call this function $f(b, c)$.\n\nFor example, let $b = 00110$, and $c = 11000$. In these strings, the first, second, third and fourth positions are different.\n\nVus the Cossack counts the number of such substrings $c$ such that $f(b, c)$ is even.\n\nFor example, let $a = 01100010$ and $b = 00110$. $a$ has four substrings of the length $|b|$: $01100$, $11000$, $10001$, $00010$. $f(00110, 01100) = 2$; $f(00110, 11000) = 4$; $f(00110, 10001) = 4$; $f(00110, 00010) = 1$. \n\nSince in three substrings, $f(b, c)$ is even, the answer is $3$.\n\nVus can not find the answer for big strings. That is why he is asking you to help him.\n\n\n-----Input-----\n\nThe first line contains a binary string $a$ ($1 \\leq |a| \\leq 10^6$)\u00a0\u2014 the first string.\n\nThe second line contains a binary string $b$ ($1 \\leq |b| \\leq |a|$)\u00a0\u2014 the second string.\n\n\n-----Output-----\n\nPrint one number\u00a0\u2014 the answer.\n\n\n-----Examples-----\nInput\n01100010\n00110\n\nOutput\n3\n\nInput\n1010111110\n0110\n\nOutput\n4\n\n\n\n-----Note-----\n\nThe first example is explained in the legend.\n\nIn the second example, there are five substrings that satisfy us: $1010$, $0101$, $1111$, $1111$."} +{"id": "02266", "text": "Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.\n\nRuling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most d kilometers along the roads. [Image] \n\nThere are n cities in the country, numbered from 1 to n, connected only by exactly n - 1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has k police stations located in some cities. In particular, the city's structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city.\n\nHowever, Zane feels like having as many as n - 1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible.\n\nHelp Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads.\n\n\n-----Input-----\n\nThe first line contains three integers n, k, and d (2 \u2264 n \u2264 3\u00b710^5, 1 \u2264 k \u2264 3\u00b710^5, 0 \u2264 d \u2264 n - 1)\u00a0\u2014 the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.\n\nThe second line contains k integers p_1, p_2, ..., p_{k} (1 \u2264 p_{i} \u2264 n)\u00a0\u2014 each denoting the city each police station is located in.\n\nThe i-th of the following n - 1 lines contains two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i})\u00a0\u2014 the cities directly connected by the road with index i.\n\nIt is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within d kilometers.\n\n\n-----Output-----\n\nIn the first line, print one integer s that denotes the maximum number of roads that can be shut down.\n\nIn the second line, print s distinct integers, the indices of such roads, in any order.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n6 2 4\n1 6\n1 2\n2 3\n3 4\n4 5\n5 6\n\nOutput\n1\n5\n\nInput\n6 3 2\n1 5 6\n1 2\n1 3\n1 4\n1 5\n5 6\n\nOutput\n2\n4 5 \n\n\n-----Note-----\n\nIn the first sample, if you shut down road 5, all cities can still reach a police station within k = 4 kilometers.\n\nIn the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line."} +{"id": "02267", "text": "You're given a list of n strings a_1, a_2, ..., a_{n}. You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest.\n\nGiven the list of strings, output the lexicographically smallest concatenation.\n\n\n-----Input-----\n\nThe first line contains integer n \u2014 the number of strings (1 \u2264 n \u2264 5\u00b710^4).\n\nEach of the next n lines contains one string a_{i} (1 \u2264 |a_{i}| \u2264 50) consisting of only lowercase English letters. The sum of string lengths will not exceed 5\u00b710^4.\n\n\n-----Output-----\n\nPrint the only string a \u2014 the lexicographically smallest string concatenation.\n\n\n-----Examples-----\nInput\n4\nabba\nabacaba\nbcd\ner\n\nOutput\nabacabaabbabcder\n\nInput\n5\nx\nxx\nxxa\nxxaa\nxxaaa\n\nOutput\nxxaaaxxaaxxaxxx\n\nInput\n3\nc\ncb\ncba\n\nOutput\ncbacbc"} +{"id": "02268", "text": "The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding\u00a0\u2014 an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name.\n\nFor this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters x_{i} by y_{i}, and all the letters y_{i} by x_{i}. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that x_{i} coincides with y_{i}. The version of the name received after the work of the last designer becomes the new name of the corporation.\n\nManager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive.\n\nSatisfy Arkady's curiosity and tell him the final version of the name.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 200 000)\u00a0\u2014 the length of the initial name and the number of designers hired, respectively.\n\nThe second line consists of n lowercase English letters and represents the original name of the corporation.\n\nNext m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters x_{i} and y_{i}.\n\n\n-----Output-----\n\nPrint the new name of the corporation.\n\n\n-----Examples-----\nInput\n6 1\npolice\np m\n\nOutput\nmolice\n\nInput\n11 6\nabacabadaba\na b\nb c\na d\ne g\nf a\nb b\n\nOutput\ncdcbcdcfcdc\n\n\n\n-----Note-----\n\nIn the second sample the name of the corporation consecutively changes as follows: $\\text{abacabadaba} \\rightarrow \\text{babcbabdbab}$\n\n$\\text{babcbabdbab} \\rightarrow \\text{cacbcacdcac}$\n\n$\\text{cacbcacdcac} \\rightarrow \\text{cdcbcdcacdc}$\n\n[Image]\n\n[Image]\n\n[Image]"} +{"id": "02269", "text": "You are given a string $s$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $s$ such that it contains each of these three characters at least once.\n\nA contiguous substring of string $s$ is a string that can be obtained from $s$ by removing some (possibly zero) characters from the beginning of $s$ and some (possibly zero) characters from the end of $s$.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 20000$) \u2014 the number of test cases.\n\nEach test case consists of one line containing the string $s$ ($1 \\le |s| \\le 200000$). It is guaranteed that each character of $s$ is either 1, 2, or 3.\n\nThe sum of lengths of all strings in all test cases does not exceed $200000$.\n\n\n-----Output-----\n\nFor each test case, print one integer \u2014 the length of the shortest contiguous substring of $s$ containing all three types of characters at least once. If there is no such substring, print $0$ instead.\n\n\n-----Example-----\nInput\n7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121\n\nOutput\n3\n3\n4\n4\n0\n0\n4\n\n\n\n-----Note-----\n\nConsider the example test:\n\nIn the first test case, the substring 123 can be used.\n\nIn the second test case, the substring 213 can be used.\n\nIn the third test case, the substring 1223 can be used.\n\nIn the fourth test case, the substring 3221 can be used.\n\nIn the fifth test case, there is no character 3 in $s$.\n\nIn the sixth test case, there is no character 1 in $s$.\n\nIn the seventh test case, the substring 3112 can be used."} +{"id": "02270", "text": "This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).\n\nApplejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $n$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format:\n\n $+$ $x$: the storehouse received a plank with length $x$ $-$ $x$: one plank with length $x$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $x$). \n\nApplejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!\n\nWe remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$): the initial amount of planks at the company's storehouse, the second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^5$): the lengths of the planks.\n\nThe third line contains a single integer $q$ ($1 \\le q \\le 10^5$): the number of events in the company. Each of the next $q$ lines contains a description of the events in a given format: the type of the event (a symbol $+$ or $-$) is given first, then goes the integer $x$ ($1 \\le x \\le 10^5$).\n\n\n-----Output-----\n\nAfter every event in the company, print \"YES\" if two storages of the required shape can be built from the planks of that company's set, and print \"NO\" otherwise. You can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n6\n1 1 1 2 1 1\n6\n+ 2\n+ 1\n- 1\n+ 2\n- 1\n+ 2\n\nOutput\nNO\nYES\nNO\nNO\nNO\nYES\n\n\n\n-----Note-----\n\nAfter the second event Applejack can build a rectangular storage using planks with lengths $1$, $2$, $1$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$.\n\nAfter the sixth event Applejack can build a rectangular storage using planks with lengths $2$, $2$, $2$, $2$ and a square storage using planks with lengths $1$, $1$, $1$, $1$."} +{"id": "02271", "text": "Heidi has finally found the mythical Tree of Life \u2013 a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.\n\nOn the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).\n\nTo decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree \u2013 these are paths of length 2, i.e., consisting of two edges. Help her!\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n \u2013 the number of vertices in the tree (1 \u2264 n \u2264 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a\u2002b \u2013 the labels of the vertices connected by the edge (1 \u2264 a < b \u2264 n). It is guaranteed that the input represents a tree.\n\n\n-----Output-----\n\nPrint one integer \u2013 the number of lifelines in the tree.\n\n\n-----Examples-----\nInput\n4\n1 2\n1 3\n1 4\n\nOutput\n3\nInput\n5\n1 2\n2 3\n3 4\n3 5\n\nOutput\n4\n\n\n-----Note-----\n\nIn the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5."} +{"id": "02272", "text": "In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I_1 from our set to interval I_2 from our set if there is a sequence of successive moves starting from I_1 so that we can reach I_2.\n\nYour program should handle the queries of the following two types: \"1 x y\" (x < y) \u2014 add the new interval (x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals. \"2 a b\" (a \u2260 b) \u2014 answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval? \n\nAnswer all the queries. Note, that initially you have an empty set of intervals.\n\n\n-----Input-----\n\nThe first line of the input contains integer n denoting the number of queries, (1 \u2264 n \u2264 100). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 10^9 by their absolute value.\n\nIt's guaranteed that all queries are correct.\n\n\n-----Output-----\n\nFor each query of the second type print \"YES\" or \"NO\" on a separate line depending on the answer.\n\n\n-----Examples-----\nInput\n5\n1 1 5\n1 5 11\n2 1 2\n1 2 9\n2 1 2\n\nOutput\nNO\nYES"} +{"id": "02273", "text": "You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected.\n\nLet's make a definition.\n\nLet $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let $f(v_{1}, v_{2})$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $v_1$. There are no edges with both endpoints in vertex set $v_2$. For every two vertices $x$ and $y$ such that $x$ is in $v_1$ and $y$ is in $v_2$, there is an edge between $x$ and $y$. \n\nCreate three vertex sets ($v_{1}$, $v_{2}$, $v_{3}$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $f(v_{1}, v_{2})$, $f(v_{2}, v_{3})$, $f(v_{3}, v_{1})$ are all true. \n\nIs it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($3 \\le n \\le 10^{5}$, $0 \\le m \\le \\text{min}(3 \\cdot 10^{5}, \\frac{n(n-1)}{2})$)\u00a0\u2014 the number of vertices and edges in the graph.\n\nThe $i$-th of the next $m$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \\le a_{i} \\lt b_{i} \\le n$)\u00a0\u2014 it means there is an edge between $a_{i}$ and $b_{i}$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected.\n\n\n-----Output-----\n\nIf the answer exists, print $n$ integers. $i$-th integer means the vertex set number (from $1$ to $3$) of $i$-th vertex. Otherwise, print $-1$.\n\nIf there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n6 11\n1 2\n1 3\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6\n\nOutput\n1 2 2 3 3 3 \nInput\n4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example, if $v_{1} = \\{ 1 \\}$, $v_{2} = \\{ 2, 3 \\}$, and $v_{3} = \\{ 4, 5, 6 \\}$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like \"2 3 3 1 1 1\" will be accepted as well. [Image] \n\nIn the second example, it's impossible to make such vertex sets."} +{"id": "02274", "text": "Consider a conveyor belt represented using a grid consisting of $n$ rows and $m$ columns. The cell in the $i$-th row from the top and the $j$-th column from the left is labelled $(i,j)$. \n\nEvery cell, except $(n,m)$, has a direction R (Right) or D (Down) assigned to it. If the cell $(i,j)$ is assigned direction R, any luggage kept on that will move to the cell $(i,j+1)$. Similarly, if the cell $(i,j)$ is assigned direction D, any luggage kept on that will move to the cell $(i+1,j)$. If at any moment, the luggage moves out of the grid, it is considered to be lost. \n\nThere is a counter at the cell $(n,m)$ from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell $(i,j)$, any luggage placed in this cell should eventually end up in the cell $(n,m)$. \n\nThis may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.\n\nPlease note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10$). Description of the test cases follows.\n\nThe first line of each test case contains two integers $n, m$ ($1 \\le n \\le 100$, $1 \\le m \\le 100$) \u00a0\u2014 the number of rows and columns, respectively.\n\nThe following $n$ lines each contain $m$ characters. The $j$-th character in the $i$-th line, $a_{i,j}$ is the initial direction of the cell $(i, j)$. Please note that $a_{n,m}=$ C.\n\n\n-----Output-----\n\nFor each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional. \n\n\n-----Example-----\nInput\n4\n3 3\nRRD\nDDR\nRRC\n1 4\nDDDC\n6 9\nRDDDDDRRR\nRRDDRRDDD\nRRDRDRRDR\nDDDDRDDRR\nDRRDRDDDR\nDDRDRRDDC\n1 1\nC\n\nOutput\n1\n3\n9\n0\n\n\n\n-----Note-----\n\nIn the first case, just changing the direction of $(2,3)$ to D is enough.\n\nYou can verify that the resulting belt is functional. For example, if we place any luggage at $(2,2)$, it first moves to $(3,2)$ and then to $(3,3)$. \n\nIn the second case, we have no option but to change the first $3$ cells from D to R making the grid equal to RRRC."} +{"id": "02275", "text": "It's a walking tour day in SIS.Winter, so $t$ groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.\n\nInitially, some students are angry. Let's describe a group of students by a string of capital letters \"A\" and \"P\": \"A\" corresponds to an angry student \"P\" corresponds to a patient student \n\nSuch string describes the row from the last to the first student.\n\nEvery minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index $i$ in the string describing a group then they will throw a snowball at the student that corresponds to the character with index $i+1$ (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them.\n\n[Image]\n\nLet's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry.\n\nYour task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$\u00a0\u2014 the number of groups of students ($1 \\le t \\le 100$). The following $2t$ lines contain descriptions of groups of students.\n\nThe description of the group starts with an integer $k_i$ ($1 \\le k_i \\le 100$)\u00a0\u2014 the number of students in the group, followed by a string $s_i$, consisting of $k_i$ letters \"A\" and \"P\", which describes the $i$-th group of students.\n\n\n-----Output-----\n\nFor every group output single integer\u00a0\u2014 the last moment a student becomes angry.\n\n\n-----Examples-----\nInput\n1\n4\nPPAP\n\nOutput\n1\n\nInput\n3\n12\nAPPAPPPAPPPP\n3\nAAP\n3\nPPA\n\nOutput\n4\n1\n0\n\n\n\n-----Note-----\n\nIn the first test, after $1$ minute the state of students becomes PPAA. After that, no new angry students will appear.\n\nIn the second tets, state of students in the first group is: after $1$ minute\u00a0\u2014 AAPAAPPAAPPP after $2$ minutes\u00a0\u2014 AAAAAAPAAAPP after $3$ minutes\u00a0\u2014 AAAAAAAAAAAP after $4$ minutes all $12$ students are angry \n\nIn the second group after $1$ minute, all students are angry."} +{"id": "02276", "text": "Vasya has a string $s$ of length $n$ consisting only of digits 0 and 1. Also he has an array $a$ of length $n$. \n\nVasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets $a_x$ points for erasing substring of length $x$.\n\nVasya wants to maximize his total points, so help him with this! \n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the length of string $s$.\n\nThe second line contains string $s$, consisting only of digits 0 and 1.\n\nThe third line contains $n$ integers $a_1, a_2, \\dots a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the number of points for erasing the substring of length $i$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum total points Vasya can get.\n\n\n-----Examples-----\nInput\n7\n1101001\n3 4 9 100 1 2 3\n\nOutput\n109\n\nInput\n5\n10101\n3 10 15 15 15\n\nOutput\n23\n\n\n\n-----Note-----\n\nIn the first example the optimal sequence of erasings is: 1101001 $\\rightarrow$ 111001 $\\rightarrow$ 11101 $\\rightarrow$ 1111 $\\rightarrow$ $\\varnothing$.\n\nIn the second example the optimal sequence of erasings is: 10101 $\\rightarrow$ 1001 $\\rightarrow$ 11 $\\rightarrow$ $\\varnothing$."} +{"id": "02277", "text": "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. An inversion in a permutation p is a pair of indices (i, j) such that i > j and a_{i} < a_{j}. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).\n\nYou are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].\n\nAfter each query you have to determine whether the number of inversions is odd or even.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 1500) \u2014 the size of the permutation. \n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n) \u2014 the elements of the permutation. These integers are pairwise distinct.\n\nThe third line contains one integer m (1 \u2264 m \u2264 2\u00b710^5) \u2014 the number of queries to process.\n\nThen m lines follow, i-th line containing two integers l_{i}, r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n) denoting that i-th query is to reverse a segment [l_{i}, r_{i}] of the permutation. All queries are performed one after another.\n\n\n-----Output-----\n\nPrint m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n2\n1 2\n2 3\n\nOutput\nodd\neven\n\nInput\n4\n1 2 4 3\n4\n1 1\n1 4\n1 4\n2 3\n\nOutput\nodd\nodd\nodd\neven\n\n\n\n-----Note-----\n\nThe first example:\n\n after the first query a = [2, 1, 3], inversion: (2, 1); after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2). \n\nThe second example:\n\n a = [1, 2, 4, 3], inversion: (4, 3); a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3); a = [1, 2, 4, 3], inversion: (4, 3); a = [1, 4, 2, 3], inversions: (3, 2), (4, 2)."} +{"id": "02278", "text": "Given a positive integer $m$, we say that a sequence $x_1, x_2, \\dots, x_n$ of positive integers is $m$-cute if for every index $i$ such that $2 \\le i \\le n$ it holds that $x_i = x_{i - 1} + x_{i - 2} + \\dots + x_1 + r_i$ for some positive integer $r_i$ satisfying $1 \\le r_i \\le m$.\n\nYou will be given $q$ queries consisting of three positive integers $a$, $b$ and $m$. For each query you must determine whether or not there exists an $m$-cute sequence whose first term is $a$ and whose last term is $b$. If such a sequence exists, you must additionally find an example of it.\n\n\n-----Input-----\n\nThe first line contains an integer number $q$ ($1 \\le q \\le 10^3$)\u00a0\u2014 the number of queries.\n\nEach of the following $q$ lines contains three integers $a$, $b$, and $m$ ($1 \\le a, b, m \\le 10^{14}$, $a \\leq b$), describing a single query.\n\n\n-----Output-----\n\nFor each query, if no $m$-cute sequence whose first term is $a$ and whose last term is $b$ exists, print $-1$.\n\nOtherwise print an integer $k$ ($1 \\le k \\leq 50$), followed by $k$ integers $x_1, x_2, \\dots, x_k$ ($1 \\le x_i \\le 10^{14}$). These integers must satisfy $x_1 = a$, $x_k = b$, and that the sequence $x_1, x_2, \\dots, x_k$ is $m$-cute.\n\nIt can be shown that under the problem constraints, for each query either no $m$-cute sequence exists, or there exists one with at most $50$ terms.\n\nIf there are multiple possible sequences, you may print any of them.\n\n\n-----Example-----\nInput\n2\n5 26 2\n3 9 1\n\nOutput\n4 5 6 13 26\n-1\n\n\n\n-----Note-----\n\nConsider the sample. In the first query, the sequence $5, 6, 13, 26$ is valid since $6 = 5 + \\bf{\\color{blue} 1}$, $13 = 6 + 5 + {\\bf\\color{blue} 2}$ and $26 = 13 + 6 + 5 + {\\bf\\color{blue} 2}$ have the bold values all between $1$ and $2$, so the sequence is $2$-cute. Other valid sequences, such as $5, 7, 13, 26$ are also accepted.\n\nIn the second query, the only possible $1$-cute sequence starting at $3$ is $3, 4, 8, 16, \\dots$, which does not contain $9$."} +{"id": "02279", "text": "There is a programing contest named SnakeUp, 2n people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct.\n\nEvery contestant hopes that he can find a teammate so that their team\u2019s strength is as high as possible. That is, a contestant will form a team with highest strength possible by choosing a teammate from ones who are willing to be a teammate with him/her. More formally, two people A and B may form a team if each of them is the best possible teammate (among the contestants that remain unpaired) for the other one. \n\nCan you determine who will be each person\u2019s teammate?\n\n\n-----Input-----\n\nThere are 2n lines in the input. \n\nThe first line contains an integer n (1 \u2264 n \u2264 400) \u2014 the number of teams to be formed.\n\nThe i-th line (i > 1) contains i - 1 numbers a_{i}1, a_{i}2, ... , a_{i}(i - 1). Here a_{ij} (1 \u2264 a_{ij} \u2264 10^6, all a_{ij} are distinct) denotes the strength of a team consisting of person i and person j (people are numbered starting from 1.)\n\n\n-----Output-----\n\nOutput a line containing 2n numbers. The i-th number should represent the number of teammate of i-th person.\n\n\n-----Examples-----\nInput\n2\n6\n1 2\n3 4 5\n\nOutput\n2 1 4 3\n\nInput\n3\n487060\n3831 161856\n845957 794650 976977\n83847 50566 691206 498447\n698377 156232 59015 382455 626960\n\nOutput\n6 5 4 3 2 1\n\n\n\n-----Note-----\n\nIn the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively."} +{"id": "02280", "text": "Let's denote a $k$-step ladder as the following structure: exactly $k + 2$ wooden planks, of which\n\n two planks of length at least $k+1$ \u2014 the base of the ladder; $k$ planks of length at least $1$ \u2014 the steps of the ladder; \n\nNote that neither the base planks, nor the steps planks are required to be equal.\n\nFor example, ladders $1$ and $3$ are correct $2$-step ladders and ladder $2$ is a correct $1$-step ladder. On the first picture the lengths of planks are $[3, 3]$ for the base and $[1]$ for the step. On the second picture lengths are $[3, 3]$ for the base and $[2]$ for the step. On the third picture lengths are $[3, 4]$ for the base and $[2, 3]$ for the steps. \n\n $H - 1 =$ \n\nYou have $n$ planks. The length of the $i$-th planks is $a_i$. You don't have a saw, so you can't cut the planks you have. Though you have a hammer and nails, so you can assemble the improvised \"ladder\" from the planks.\n\nThe question is: what is the maximum number $k$ such that you can choose some subset of the given planks and assemble a $k$-step ladder using them?\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\le T \\le 100$) \u2014 the number of queries. The queries are independent.\n\nEach query consists of two lines. The first line contains a single integer $n$ ($2 \\le n \\le 10^5$) \u2014 the number of planks you have.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^5$) \u2014 the lengths of the corresponding planks.\n\nIt's guaranteed that the total number of planks from all queries doesn't exceed $10^5$.\n\n\n-----Output-----\n\nPrint $T$ integers \u2014 one per query. The $i$-th integer is the maximum number $k$, such that you can choose some subset of the planks given in the $i$-th query and assemble a $k$-step ladder using them.\n\nPrint $0$ if you can't make even $1$-step ladder from the given set of planks.\n\n\n-----Example-----\nInput\n4\n4\n1 3 1 3\n3\n3 3 2\n5\n2 3 3 4 2\n3\n1 1 2\n\nOutput\n2\n1\n2\n0\n\n\n\n-----Note-----\n\nExamples for the queries $1-3$ are shown at the image in the legend section.\n\nThe Russian meme to express the quality of the ladders:\n\n [Image]"} +{"id": "02281", "text": "You have array a that contains all integers from 1 to n twice. You can arbitrary permute any numbers in a.\n\nLet number i be in positions x_{i}, y_{i} (x_{i} < y_{i}) in the permuted array a. Let's define the value d_{i} = y_{i} - x_{i} \u2014 the distance between the positions of the number i. Permute the numbers in array a to minimize the value of the sum $s = \\sum_{i = 1}^{n}(n - i) \\cdot|d_{i} + i - n$.\n\n\n-----Input-----\n\nThe only line contains integer n (1 \u2264 n \u2264 5\u00b710^5).\n\n\n-----Output-----\n\nPrint 2n integers \u2014 the permuted array a that minimizes the value of the sum s.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1 1 2 2\n\nInput\n1\n\nOutput\n1 1"} +{"id": "02282", "text": "Today, Mezo is playing a game. Zoma, a character in that game, is initially at position $x = 0$. Mezo starts sending $n$ commands to Zoma. There are two possible commands: 'L' (Left) sets the position $x: =x - 1$; 'R' (Right) sets the position $x: =x + 1$. \n\nUnfortunately, Mezo's controller malfunctions sometimes. Some commands are sent successfully and some are ignored. If the command is ignored then the position $x$ doesn't change and Mezo simply proceeds to the next command.\n\nFor example, if Mezo sends commands \"LRLR\", then here are some possible outcomes (underlined commands are sent successfully): \"LRLR\" \u2014 Zoma moves to the left, to the right, to the left again and to the right for the final time, ending up at position $0$; \"LRLR\" \u2014 Zoma recieves no commands, doesn't move at all and ends up at position $0$ as well; \"LRLR\" \u2014 Zoma moves to the left, then to the left again and ends up in position $-2$. \n\nMezo doesn't know which commands will be sent successfully beforehand. Thus, he wants to know how many different positions may Zoma end up at.\n\n\n-----Input-----\n\nThe first line contains $n$ $(1 \\le n \\le 10^5)$ \u2014 the number of commands Mezo sends.\n\nThe second line contains a string $s$ of $n$ commands, each either 'L' (Left) or 'R' (Right).\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of different positions Zoma may end up at.\n\n\n-----Example-----\nInput\n4\nLRLR\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the example, Zoma may end up anywhere between $-2$ and $2$."} +{"id": "02283", "text": "Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of $n$ vertices and $m$ edges.\n\nThe weight of the $i$-th vertex is $a_i$.\n\nThe weight of the $i$-th edge is $w_i$.\n\nA subgraph of a graph is some set of the graph vertices and some set of the graph edges. The set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices. \n\nThe weight of a subgraph is the sum of the weights of its edges, minus the sum of the weights of its vertices. You need to find the maximum weight of subgraph of given graph. The given graph does not contain loops and multiple edges.\n\n\n-----Input-----\n\nThe first line contains two numbers $n$ and $m$ ($1 \\le n \\le 10^3, 0 \\le m \\le 10^3$) - the number of vertices and edges in the graph, respectively.\n\nThe next line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) - the weights of the vertices of the graph.\n\nThe following $m$ lines contain edges: the $i$-e edge is defined by a triple of integers $v_i, u_i, w_i$ ($1 \\le v_i, u_i \\le n, 1 \\le w_i \\le 10^9, v_i \\neq u_i$). This triple means that between the vertices $v_i$ and $u_i$ there is an edge of weight $w_i$. It is guaranteed that the graph does not contain loops and multiple edges.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum weight of the subgraph of the given graph.\n\n\n-----Examples-----\nInput\n4 5\n1 5 2 2\n1 3 4\n1 4 4\n3 4 5\n3 2 2\n4 2 2\n\nOutput\n8\n\nInput\n3 3\n9 7 8\n1 2 1\n2 3 2\n1 3 3\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test example, the optimal subgraph consists of the vertices ${1, 3, 4}$ and has weight $4 + 4 + 5 - (1 + 2 + 2) = 8$. In the second test case, the optimal subgraph is empty."} +{"id": "02284", "text": "The marmots need to prepare k problems for HC^2 over n days. Each problem, once prepared, also has to be printed.\n\nThe preparation of a problem on day i (at most one per day) costs a_{i} CHF, and the printing of a problem on day i (also at most one per day) costs b_{i} CHF. Of course, a problem cannot be printed before it has been prepared (but doing both on the same day is fine).\n\nWhat is the minimum cost of preparation and printing?\n\n\n-----Input-----\n\nThe first line of input contains two space-separated integers n and k (1 \u2264 k \u2264 n \u2264 2200). The second line contains n space-separated integers a_1, ..., a_{n} ($1 \\leq a_{i} \\leq 10^{9}$) \u2014 the preparation costs. The third line contains n space-separated integers b_1, ..., b_{n} ($1 \\leq b_{i} \\leq 10^{9}$) \u2014 the printing costs.\n\n\n-----Output-----\n\nOutput the minimum cost of preparation and printing k problems \u2014 that is, the minimum possible sum a_{i}_1 + a_{i}_2 + ... + a_{i}_{k} + b_{j}_1 + b_{j}_2 + ... + b_{j}_{k}, where 1 \u2264 i_1 < i_2 < ... < i_{k} \u2264 n, 1 \u2264 j_1 < j_2 < ... < j_{k} \u2264 n and i_1 \u2264 j_1, i_2 \u2264 j_2, ..., i_{k} \u2264 j_{k}.\n\n\n-----Example-----\nInput\n8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 1\n\nOutput\n32\n\n\n-----Note-----\n\nIn the sample testcase, one optimum solution is to prepare the first problem on day 1 and print it on day 1, prepare the second problem on day 2 and print it on day 4, prepare the third problem on day 3 and print it on day 5, and prepare the fourth problem on day 6 and print it on day 8."} +{"id": "02285", "text": "An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons \u2014 8 blocks in total, each block has four hexadecimal digits. Here is an example of the correct record of a IPv6 address: \"0124:5678:90ab:cdef:0124:5678:90ab:cdef\". We'll call such format of recording an IPv6-address full.\n\nBesides the full record of an IPv6 address there is a short record format. The record of an IPv6 address can be shortened by removing one or more leading zeroes at the beginning of each block. However, each block should contain at least one digit in the short format. For example, the leading zeroes can be removed like that: \"a56f:00d3:0000:0124:0001:f19a:1000:0000\" \u2192 \"a56f:d3:0:0124:01:f19a:1000:00\". There are more ways to shorten zeroes in this IPv6 address.\n\nSome IPv6 addresses contain long sequences of zeroes. Continuous sequences of 16-bit zero blocks can be shortened to \"::\". A sequence can consist of one or several consecutive blocks, with all 16 bits equal to 0. \n\nYou can see examples of zero block shortenings below:\n\n \"a56f:00d3:0000:0124:0001:0000:0000:0000\" \u2192 \"a56f:00d3:0000:0124:0001::\"; \"a56f:0000:0000:0124:0001:0000:1234:0ff0\" \u2192 \"a56f::0124:0001:0000:1234:0ff0\"; \"a56f:0000:0000:0000:0001:0000:1234:0ff0\" \u2192 \"a56f:0000::0000:0001:0000:1234:0ff0\"; \"a56f:00d3:0000:0124:0001:0000:0000:0000\" \u2192 \"a56f:00d3:0000:0124:0001::0000\"; \"0000:0000:0000:0000:0000:0000:0000:0000\" \u2192 \"::\". \n\nIt is not allowed to shorten zero blocks in the address more than once. This means that the short record can't contain the sequence of characters \"::\" more than once. Otherwise, it will sometimes be impossible to determine the number of zero blocks, each represented by a double colon.\n\nThe format of the record of the IPv6 address after removing the leading zeroes and shortening the zero blocks is called short.\n\nYou've got several short records of IPv6 addresses. Restore their full record.\n\n\n-----Input-----\n\nThe first line contains a single integer n \u2014 the number of records to restore (1 \u2264 n \u2264 100).\n\nEach of the following n lines contains a string \u2014 the short IPv6 addresses. Each string only consists of string characters \"0123456789abcdef:\".\n\nIt is guaranteed that each short address is obtained by the way that is described in the statement from some full IPv6 address.\n\n\n-----Output-----\n\nFor each short IPv6 address from the input print its full record on a separate line. Print the full records for the short IPv6 addresses in the order, in which the short records follow in the input.\n\n\n-----Examples-----\nInput\n6\na56f:d3:0:0124:01:f19a:1000:00\na56f:00d3:0000:0124:0001::\na56f::0124:0001:0000:1234:0ff0\na56f:0000::0000:0001:0000:1234:0ff0\n::\n0ea::4d:f4:6:0\n\nOutput\na56f:00d3:0000:0124:0001:f19a:1000:0000\na56f:00d3:0000:0124:0001:0000:0000:0000\na56f:0000:0000:0124:0001:0000:1234:0ff0\na56f:0000:0000:0000:0001:0000:1234:0ff0\n0000:0000:0000:0000:0000:0000:0000:0000\n00ea:0000:0000:0000:004d:00f4:0006:0000"} +{"id": "02286", "text": "This is the easy version of the problem. The difference is constraints on the number of wise men and the time limit. You can make hacks only if all versions of this task are solved.\n\n$n$ wise men live in a beautiful city. Some of them know each other.\n\nFor each of the $n!$ possible permutations $p_1, p_2, \\ldots, p_n$ of the wise men, let's generate a binary string of length $n-1$: for each $1 \\leq i < n$ set $s_i=1$ if $p_i$ and $p_{i+1}$ know each other, and $s_i=0$ otherwise. \n\nFor all possible $2^{n-1}$ binary strings, find the number of permutations that produce this binary string.\n\n\n-----Input-----\n\nThe first line of input contains one integer $n$ ($2 \\leq n \\leq 14)$ \u00a0\u2014 the number of wise men in the city.\n\nThe next $n$ lines contain a binary string of length $n$ each, such that the $j$-th character of the $i$-th string is equal to '1' if wise man $i$ knows wise man $j$, and equals '0' otherwise.\n\nIt is guaranteed that if the $i$-th man knows the $j$-th man, then the $j$-th man knows $i$-th man and no man knows himself.\n\n\n-----Output-----\n\nPrint $2^{n-1}$ space-separated integers. For each $0 \\leq x < 2^{n-1}$:\n\n Let's consider a string $s$ of length $n-1$, such that $s_i = \\lfloor \\frac{x}{2^{i-1}} \\rfloor \\bmod 2$ for all $1 \\leq i \\leq n - 1$. The $(x+1)$-th number should be equal to the required answer for $s$. \n\n\n-----Examples-----\nInput\n3\n011\n101\n110\n\nOutput\n0 0 0 6 \n\nInput\n4\n0101\n1000\n0001\n1010\n\nOutput\n2 2 6 2 2 6 2 2 \n\n\n\n-----Note-----\n\nIn the first test, each wise man knows each other, so every permutation will produce the string $11$.\n\nIn the second test:\n\n If $p = \\{1, 2, 3, 4\\}$, the produced string is $101$, because wise men $1$ and $2$ know each other, $2$ and $3$ don't know each other, and $3$ and $4$ know each other; If $p = \\{4, 1, 2, 3\\}$, the produced string is $110$, because wise men $1$ and $4$ know each other, $1$ and $2$ know each other and $2$, and $3$ don't know each other; If $p = \\{1, 3, 2, 4\\}$, the produced string is $000$, because wise men $1$ and $3$ don't know each other, $3$ and $2$ don't know each other, and $2$ and $4$ don't know each other."} +{"id": "02287", "text": "You are given a string $s$. Each character is either 0 or 1.\n\nYou want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.\n\nYou may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase?\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThen $t$ lines follow, each representing a test case. Each line contains one string $s$ ($1 \\le |s| \\le 100$); each character of $s$ is either 0 or 1.\n\n\n-----Output-----\n\nPrint $t$ integers, where the $i$-th integer is the answer to the $i$-th testcase (the minimum number of 0's that you have to erase from $s$).\n\n\n-----Example-----\nInput\n3\n010011\n0\n1111000\n\nOutput\n2\n0\n0\n\n\n\n-----Note-----\n\nIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111)."} +{"id": "02288", "text": "Ashish has $n$ elements arranged in a line. \n\nThese elements are represented by two integers $a_i$\u00a0\u2014 the value of the element and $b_i$\u00a0\u2014 the type of the element (there are only two possible types: $0$ and $1$). He wants to sort the elements in non-decreasing values of $a_i$.\n\nHe can perform the following operation any number of times: Select any two elements $i$ and $j$ such that $b_i \\ne b_j$ and swap them. That is, he can only swap two elements of different types in one move. \n\nTell him if he can sort the elements in non-decreasing values of $a_i$ after performing any number of operations.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ $(1 \\le t \\le 100)$\u00a0\u2014 the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains one integer $n$ $(1 \\le n \\le 500)$\u00a0\u2014 the size of the arrays.\n\nThe second line contains $n$ integers $a_i$ $(1 \\le a_i \\le 10^5)$ \u00a0\u2014 the value of the $i$-th element.\n\nThe third line containts $n$ integers $b_i$ $(b_i \\in \\{0, 1\\})$ \u00a0\u2014 the type of the $i$-th element.\n\n\n-----Output-----\n\nFor each test case, print \"Yes\" or \"No\" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value.\n\nYou may print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n5\n4\n10 20 20 30\n0 1 0 1\n3\n3 1 2\n0 1 1\n4\n2 2 4 8\n1 1 1 1\n3\n5 15 4\n0 0 0\n4\n20 10 100 50\n1 0 0 1\n\nOutput\nYes\nYes\nYes\nNo\nYes\n\n\n\n-----Note-----\n\nFor the first case: The elements are already in sorted order.\n\nFor the second case: Ashish may first swap elements at positions $1$ and $2$, then swap elements at positions $2$ and $3$.\n\nFor the third case: The elements are already in sorted order.\n\nFor the fourth case: No swap operations may be performed as there is no pair of elements $i$ and $j$ such that $b_i \\ne b_j$. The elements cannot be sorted.\n\nFor the fifth case: Ashish may swap elements at positions $3$ and $4$, then elements at positions $1$ and $2$."} +{"id": "02289", "text": "Ivar the Boneless is a great leader. He is trying to capture Kattegat from Lagertha. The war has begun and wave after wave Ivar's warriors are falling in battle.\n\nIvar has $n$ warriors, he places them on a straight line in front of the main gate, in a way that the $i$-th warrior stands right after $(i-1)$-th warrior. The first warrior leads the attack.\n\nEach attacker can take up to $a_i$ arrows before he falls to the ground, where $a_i$ is the $i$-th warrior's strength.\n\nLagertha orders her warriors to shoot $k_i$ arrows during the $i$-th minute, the arrows one by one hit the first still standing warrior. After all Ivar's warriors fall and all the currently flying arrows fly by, Thor smashes his hammer and all Ivar's warriors get their previous strengths back and stand up to fight again. In other words, if all warriors die in minute $t$, they will all be standing to fight at the end of minute $t$.\n\nThe battle will last for $q$ minutes, after each minute you should tell Ivar what is the number of his standing warriors.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $q$ ($1 \\le n, q \\leq 200\\,000$)\u00a0\u2014 the number of warriors and the number of minutes in the battle.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^9$) that represent the warriors' strengths.\n\nThe third line contains $q$ integers $k_1, k_2, \\ldots, k_q$ ($1 \\leq k_i \\leq 10^{14}$), the $i$-th of them represents Lagertha's order at the $i$-th minute: $k_i$ arrows will attack the warriors.\n\n\n-----Output-----\n\nOutput $q$ lines, the $i$-th of them is the number of standing warriors after the $i$-th minute.\n\n\n-----Examples-----\nInput\n5 5\n1 2 1 2 1\n3 10 1 1 1\n\nOutput\n3\n5\n4\n4\n3\n\nInput\n4 4\n1 2 3 4\n9 1 10 6\n\nOutput\n1\n4\n4\n1\n\n\n\n-----Note-----\n\nIn the first example: after the 1-st minute, the 1-st and 2-nd warriors die. after the 2-nd minute all warriors die (and all arrows left over are wasted), then they will be revived thus answer is 5\u00a0\u2014 all warriors are alive. after the 3-rd minute, the 1-st warrior dies. after the 4-th minute, the 2-nd warrior takes a hit and his strength decreases by 1. after the 5-th minute, the 2-nd warrior dies."} +{"id": "02290", "text": "You're given an undirected graph with $n$ nodes and $m$ edges. Nodes are numbered from $1$ to $n$.\n\nThe graph is considered harmonious if and only if the following property holds: For every triple of integers $(l, m, r)$ such that $1 \\le l < m < r \\le n$, if there exists a path going from node $l$ to node $r$, then there exists a path going from node $l$ to node $m$. \n\nIn other words, in a harmonious graph, if from a node $l$ we can reach a node $r$ through edges ($l < r$), then we should able to reach nodes $(l+1), (l+2), \\ldots, (r-1)$ too.\n\nWhat is the minimum number of edges we need to add to make the graph harmonious? \n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($3 \\le n \\le 200\\ 000$ and $1 \\le m \\le 200\\ 000$).\n\nThe $i$-th of the next $m$ lines contains two integers $u_i$ and $v_i$ ($1 \\le u_i, v_i \\le n$, $u_i \\neq v_i$), that mean that there's an edge between nodes $u$ and $v$.\n\nIt is guaranteed that the given graph is simple (there is no self-loop, and there is at most one edge between every pair of nodes).\n\n\n-----Output-----\n\nPrint the minimum number of edges we have to add to the graph to make it harmonious.\n\n\n-----Examples-----\nInput\n14 8\n1 2\n2 7\n3 4\n6 3\n5 7\n3 8\n6 8\n11 12\n\nOutput\n1\n\nInput\n200000 3\n7 9\n9 8\n4 5\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, the given graph is not harmonious (for instance, $1 < 6 < 7$, node $1$ can reach node $7$ through the path $1 \\rightarrow 2 \\rightarrow 7$, but node $1$ can't reach node $6$). However adding the edge $(2, 4)$ is sufficient to make it harmonious.\n\nIn the second example, the given graph is already harmonious."} +{"id": "02291", "text": "Today, as a friendship gift, Bakry gave Badawy $n$ integers $a_1, a_2, \\dots, a_n$ and challenged him to choose an integer $X$ such that the value $\\underset{1 \\leq i \\leq n}{\\max} (a_i \\oplus X)$ is minimum possible, where $\\oplus$ denotes the bitwise XOR operation.\n\nAs always, Badawy is too lazy, so you decided to help him and find the minimum possible value of $\\underset{1 \\leq i \\leq n}{\\max} (a_i \\oplus X)$.\n\n\n-----Input-----\n\nThe first line contains integer $n$ ($1\\le n \\le 10^5$).\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 2^{30}-1$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible value of $\\underset{1 \\leq i \\leq n}{\\max} (a_i \\oplus X)$.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n2\n\nInput\n2\n1 5\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample, we can choose $X = 3$.\n\nIn the second sample, we can choose $X = 5$."} +{"id": "02292", "text": "Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid.\n\nEach test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid.\n\nAn operation on array $a$ is: select an integer $k$ $(1 \\le k \\le \\lfloor\\frac{n}{2}\\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ \n\nFor example, if array $a$ initially is $\\{1, 2, 3, 4, 5, 6\\}$, after performing an operation with $k = 2$, it is transformed into $\\{5, 6, 3, 4, 1, 2\\}$.\n\nGiven the set of test cases, help them determine if each one is valid or invalid.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ $(1 \\le t \\le 500)$\u00a0\u2014 the number of test cases. The description of each test case is as follows.\n\nThe first line of each test case contains a single integer $n$ $(1 \\le n \\le 500)$\u00a0\u2014 the size of the arrays.\n\nThe second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \\le a_i \\le 10^9)$ \u2014 elements of array $a$.\n\nThe third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \\le b_i \\le 10^9)$ \u2014 elements of array $b$.\n\n\n-----Output-----\n\nFor each test case, print \"Yes\" if the given input is valid. Otherwise print \"No\".\n\nYou may print the answer in any case.\n\n\n-----Example-----\nInput\n5\n2\n1 2\n2 1\n3\n1 2 3\n1 2 3\n3\n1 2 4\n1 3 4\n4\n1 2 3 2\n3 1 2 2\n3\n1 2 3\n1 3 2\n\nOutput\nyes\nyes\nNo\nyes\nNo\n\n\n\n-----Note-----\n\nFor the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$.\n\nFor the second test case, $a$ is already equal to $b$.\n\nFor the third test case, it is impossible since we cannot obtain $3$ in $a$.\n\nFor the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$.\n\nFor the fifth test case, it is impossible to convert $a$ to $b$."} +{"id": "02293", "text": "Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia?\n\nThere are $n$ stores numbered from $1$ to $n$ in Nlogonia. The $i$-th of these stores offers a positive integer $a_i$.\n\nEach day among the last $m$ days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day.\n\nDora considers Swiper to be her rival, and she considers that she beat Swiper on day $i$ if and only if the least common multiple of the numbers she bought on day $i$ is strictly greater than the least common multiple of the numbers that Swiper bought on day $i$.\n\nThe least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection.\n\nHowever, Dora forgot the values of $a_i$. Help Dora find out if there are positive integer values of $a_i$ such that she beat Swiper on every day. You don't need to find what are the possible values of $a_i$ though.\n\nNote that it is possible for some values of $a_i$ to coincide in a solution.\n\n\n-----Input-----\n\nThe first line contains integers $m$ and $n$ ($1\\leq m \\leq 50$, $1\\leq n \\leq 10^4$)\u00a0\u2014 the number of days and the number of stores.\n\nAfter this $m$ lines follow, the $i$-th line starts with an integer $s_i$ ($1\\leq s_i \\leq n-1$), the number of integers Dora bought on day $i$, followed by $s_i$ distinct integers, the indices of the stores where Dora bought an integer on the $i$-th day. The indices are between $1$ and $n$.\n\n\n-----Output-----\n\nOutput must consist of a single line containing \"possible\" if there exist positive integers $a_i$ such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print \"impossible\".\n\nNote that you don't have to restore the integers themselves.\n\n\n-----Examples-----\nInput\n2 5\n3 1 2 3\n3 3 4 5\n\nOutput\npossible\n\nInput\n10 10\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n\nOutput\nimpossible\n\n\n\n-----Note-----\n\nIn the first sample, a possible choice for the values of the $a_i$ is $3, 4, 3, 5, 2$. On the first day, Dora buys the integers $3, 4$ and $3$, whose LCM is $12$, while Swiper buys integers $5$ and $2$, whose LCM is $10$. On the second day, Dora buys $3, 5$ and $2$, whose LCM is $30$, and Swiper buys integers $3$ and $4$, whose LCM is $12$."} +{"id": "02294", "text": "Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.\n\nArtem wants to create a basic multiset of integers. He wants these structure to support operations of three types:\n\n Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer. Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset. Count the number of instances of the given integer that are stored in the multiset. \n\nBut what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example.\n\n First Artem adds integer 5 to the multiset at the 1-st moment of time. Then Artem adds integer 3 to the multiset at the moment 5. Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1. Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0. Then Artem goes back in time again and removes 5 from the multiset at moment 3. Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment 3. \n\nNote that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes.\n\nHelp Artem implement time travellers multiset.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of Artem's queries.\n\nThen follow n lines with queries descriptions. Each of them contains three integers a_{i}, t_{i} and x_{i} (1 \u2264 a_{i} \u2264 3, 1 \u2264 t_{i}, x_{i} \u2264 10^9)\u00a0\u2014 type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent.\n\n\n-----Output-----\n\nFor each ask operation output the number of instances of integer being queried at the given moment of time.\n\n\n-----Examples-----\nInput\n6\n1 1 5\n3 5 5\n1 2 5\n3 6 5\n2 3 5\n3 7 5\n\nOutput\n1\n2\n1\n\nInput\n3\n1 1 1\n2 2 1\n3 3 1\n\nOutput\n0"} +{"id": "02295", "text": "You are given an array a consisting of n positive integers. You pick two integer numbers l and r from 1 to n, inclusive (numbers are picked randomly, equiprobably and independently). If l > r, then you swap values of l and r. You have to calculate the expected value of the number of unique elements in segment of the array from index l to index r, inclusive (1-indexed).\n\n\n-----Input-----\n\nThe first line contains one integer number n (1 \u2264 n \u2264 10^6). The second line contains n integer numbers a_1, a_2, ... a_{n} (1 \u2264 a_{i} \u2264 10^6) \u2014 elements of the array.\n\n\n-----Output-----\n\nPrint one number \u2014 the expected number of unique elements in chosen segment. \n\nYour answer will be considered correct if its absolute or relative error doesn't exceed 10^{ - 4} \u2014 formally, the answer is correct if $\\operatorname{min}(|x - y|, \\frac{|x - y|}{x}) \\leq 10^{-4}$, where x is jury's answer, and y is your answer.\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n1.500000\n\nInput\n2\n2 2\n\nOutput\n1.000000"} +{"id": "02296", "text": "Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.\n\nYesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.\n\nHag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with $n$ vertices.\n\nHag brought two pins and pinned the polygon with them in the $1$-st and $2$-nd vertices to the wall. His dad has $q$ queries to Hag of two types. $1$ $f$ $t$: pull a pin from the vertex $f$, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex $t$. $2$ $v$: answer what are the coordinates of the vertex $v$. \n\nPlease help Hag to answer his father's queries.\n\nYou can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $q$ ($3\\leq n \\leq 10\\,000$, $1 \\leq q \\leq 200000$)\u00a0\u2014 the number of vertices in the polygon and the number of queries.\n\nThe next $n$ lines describe the wooden polygon, the $i$-th line contains two integers $x_i$ and $y_i$ ($|x_i|, |y_i|\\leq 10^8$)\u00a0\u2014 the coordinates of the $i$-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.\n\nThe next $q$ lines describe the queries, one per line. Each query starts with its type $1$ or $2$. Each query of the first type continues with two integers $f$ and $t$ ($1 \\le f, t \\le n$)\u00a0\u2014 the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex $f$ contains a pin. Each query of the second type continues with a single integer $v$ ($1 \\le v \\le n$)\u00a0\u2014 the vertex the coordinates of which Hag should tell his father.\n\nIt is guaranteed that there is at least one query of the second type.\n\n\n-----Output-----\n\nThe output should contain the answer to each query of second type\u00a0\u2014 two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed $10^{-4}$.\n\nFormally, 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^{-4}$\n\n\n-----Examples-----\nInput\n3 4\n0 0\n2 0\n2 2\n1 1 2\n2 1\n2 2\n2 3\n\nOutput\n3.4142135624 -1.4142135624\n2.0000000000 0.0000000000\n0.5857864376 -1.4142135624\n\nInput\n3 2\n-1 1\n0 0\n1 1\n1 1 2\n2 1\n\nOutput\n1.0000000000 -1.0000000000\n\n\n\n-----Note-----\n\nIn the first test note the initial and the final state of the wooden polygon. [Image] \n\nRed Triangle is the initial state and the green one is the triangle after rotation around $(2,0)$.\n\nIn the second sample note that the polygon rotates $180$ degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him."} +{"id": "02297", "text": "Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them.\n\nRecently, she was presented with an array $a$ of the size of $10^9$ elements that is filled as follows: $a_1 = -1$ $a_2 = 2$ $a_3 = -3$ $a_4 = 4$ $a_5 = -5$ And so on ... \n\nThat is, the value of the $i$-th element of the array $a$ is calculated using the formula $a_i = i \\cdot (-1)^i$.\n\nShe immediately came up with $q$ queries on this array. Each query is described with two numbers: $l$ and $r$. The answer to a query is the sum of all the elements of the array at positions from $l$ to $r$ inclusive.\n\nMargarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you\u00a0\u2014 the best programmer.\n\nHelp her find the answers!\n\n\n-----Input-----\n\nThe first line contains a single integer $q$ ($1 \\le q \\le 10^3$)\u00a0\u2014 the number of the queries.\n\nEach of the next $q$ lines contains two integers $l$ and $r$ ($1 \\le l \\le r \\le 10^9$)\u00a0\u2014 the descriptions of the queries.\n\n\n-----Output-----\n\nPrint $q$ lines, each containing one number\u00a0\u2014 the answer to the query. \n\n\n-----Example-----\nInput\n5\n1 3\n2 5\n5 5\n4 4\n2 3\n\nOutput\n-2\n-2\n-5\n4\n-1\n\n\n\n-----Note-----\n\nIn the first query, you need to find the sum of the elements of the array from position $1$ to position $3$. The sum is equal to $a_1 + a_2 + a_3 = -1 + 2 -3 = -2$.\n\nIn the second query, you need to find the sum of the elements of the array from position $2$ to position $5$. The sum is equal to $a_2 + a_3 + a_4 + a_5 = 2 -3 + 4 - 5 = -2$.\n\nIn the third query, you need to find the sum of the elements of the array from position $5$ to position $5$. The sum is equal to $a_5 = -5$.\n\nIn the fourth query, you need to find the sum of the elements of the array from position $4$ to position $4$. The sum is equal to $a_4 = 4$.\n\nIn the fifth query, you need to find the sum of the elements of the array from position $2$ to position $3$. The sum is equal to $a_2 + a_3 = 2 - 3 = -1$."} +{"id": "02298", "text": "You are given two integers $a$ and $b$, and $q$ queries. The $i$-th query consists of two numbers $l_i$ and $r_i$, and the answer to it is the number of integers $x$ such that $l_i \\le x \\le r_i$, and $((x \\bmod a) \\bmod b) \\ne ((x \\bmod b) \\bmod a)$. Calculate the answer for each query.\n\nRecall that $y \\bmod z$ is the remainder of the division of $y$ by $z$. For example, $5 \\bmod 3 = 2$, $7 \\bmod 8 = 7$, $9 \\bmod 4 = 1$, $9 \\bmod 9 = 0$.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$)\u00a0\u2014 the number of test cases. Then the test cases follow.\n\nThe first line of each test case contains three integers $a$, $b$ and $q$ ($1 \\le a, b \\le 200$; $1 \\le q \\le 500$).\n\nThen $q$ lines follow, each containing two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le 10^{18}$) for the corresponding query.\n\n\n-----Output-----\n\nFor each test case, print $q$ integers\u00a0\u2014 the answers to the queries of this test case in the order they appear.\n\n\n-----Example-----\nInput\n2\n4 6 5\n1 1\n1 3\n1 5\n1 7\n1 9\n7 10 2\n7 8\n100 200\n\nOutput\n0 0 0 2 4 \n0 91"} +{"id": "02299", "text": "During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.\n\nNow she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j \u2264 a_{i} + 1, j for all i from 1 to n - 1.\n\nTeacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j \u2264 a_{i} + 1, j for all i from l to r - 1 inclusive.\n\nAlyona is too small to deal with this task and asks you to help!\n\n\n-----Input-----\n\nThe first line of the input contains two positive integers n and m (1 \u2264 n\u00b7m \u2264 100 000)\u00a0\u2014 the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.\n\nEach of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 \u2264 a_{i}, j \u2264 10^9).\n\nThe next line of the input contains an integer k (1 \u2264 k \u2264 100 000)\u00a0\u2014 the number of task that teacher gave to Alyona.\n\nThe i-th of the next k lines contains two integers l_{i} and r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n).\n\n\n-----Output-----\n\nPrint \"Yes\" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print \"No\".\n\n\n-----Example-----\nInput\n5 4\n1 2 3 5\n3 1 3 2\n4 5 2 3\n5 5 3 2\n4 4 3 4\n6\n1 1\n2 5\n4 5\n3 5\n1 3\n1 5\n\nOutput\nYes\nNo\nYes\nYes\nYes\nNo\n\n\n\n-----Note-----\n\nIn the sample, the whole table is not sorted in any column. However, rows 1\u20133 are sorted in column 1, while rows 4\u20135 are sorted in column 3."} +{"id": "02300", "text": "By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher:\n\nYou are given a sequence of integers a_1, a_2, ..., a_{n}. Your task is to perform on it m consecutive operations of the following type: For given numbers x_{i} and v_{i} assign value v_{i} to element a_{x}_{i}. For given numbers l_{i} and r_{i} you've got to calculate sum $\\sum_{x = 0}^{r_{i} - l_{i}}(f_{x} \\cdot a_{l_{i} + x})$, where f_0 = f_1 = 1 and at i \u2265 2: f_{i} = f_{i} - 1 + f_{i} - 2. For a group of three numbers l_{i} r_{i} d_{i} you should increase value a_{x} by d_{i} for all x (l_{i} \u2264 x \u2264 r_{i}). \n\nSmart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 2\u00b710^5) \u2014 the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^5). Then follow m lines, each describes an operation. Each line starts with an integer t_{i} (1 \u2264 t_{i} \u2264 3) \u2014 the operation type: if t_{i} = 1, then next follow two integers x_{i} v_{i} (1 \u2264 x_{i} \u2264 n, 0 \u2264 v_{i} \u2264 10^5); if t_{i} = 2, then next follow two integers l_{i} r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n); if t_{i} = 3, then next follow three integers l_{i} r_{i} d_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n, 0 \u2264 d_{i} \u2264 10^5). \n\nThe input limits for scoring 30 points are (subproblem E1): It is guaranteed that n does not exceed 100, m does not exceed 10000 and there will be no queries of the 3-rd type. \n\nThe input limits for scoring 70 points are (subproblems E1+E2): It is guaranteed that there will be queries of the 1-st and 2-nd type only. \n\nThe input limits for scoring 100 points are (subproblems E1+E2+E3): No extra limitations. \n\n\n-----Output-----\n\nFor each query print the calculated sum modulo 1000000000 (10^9).\n\n\n-----Examples-----\nInput\n5 5\n1 3 1 2 4\n2 1 4\n2 1 5\n2 2 4\n1 3 10\n2 1 5\n\nOutput\n12\n32\n8\n50\n\nInput\n5 4\n1 3 1 2 4\n3 1 4 1\n2 2 4\n1 2 10\n2 1 5\n\nOutput\n12\n45"} +{"id": "02301", "text": "This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem.\n\nToday is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal.\n\nAn ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.\n\nYou can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ $(1 \\le n \\le 10^5)$\u00a0\u2014 the number of ice spheres in the shop.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ $(1 \\le a_i \\le 10^9)$\u00a0\u2014 the prices of ice spheres.\n\n\n-----Output-----\n\nIn the first line print the maximum number of ice spheres that Sage can buy.\n\nIn the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.\n\n\n-----Example-----\nInput\n7\n1 3 2 2 4 5 4\n\nOutput\n3\n3 1 4 2 4 2 5 \n\n\n-----Note-----\n\nIn the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each."} +{"id": "02302", "text": "Bob is playing a game of Spaceship Solitaire. The goal of this game is to build a spaceship. In order to do this, he first needs to accumulate enough resources for the construction. There are $n$ types of resources, numbered $1$ through $n$. Bob needs at least $a_i$ pieces of the $i$-th resource to build the spaceship. The number $a_i$ is called the goal for resource $i$.\n\nEach resource takes $1$ turn to produce and in each turn only one resource can be produced. However, there are certain milestones that speed up production. Every milestone is a triple $(s_j, t_j, u_j)$, meaning that as soon as Bob has $t_j$ units of the resource $s_j$, he receives one unit of the resource $u_j$ for free, without him needing to spend a turn. It is possible that getting this free resource allows Bob to claim reward for another milestone. This way, he can obtain a large number of resources in a single turn.\n\nThe game is constructed in such a way that there are never two milestones that have the same $s_j$ and $t_j$, that is, the award for reaching $t_j$ units of resource $s_j$ is at most one additional resource.\n\nA bonus is never awarded for $0$ of any resource, neither for reaching the goal $a_i$ nor for going past the goal \u2014 formally, for every milestone $0 < t_j < a_{s_j}$.\n\nA bonus for reaching certain amount of a resource can be the resource itself, that is, $s_j = u_j$.\n\nInitially there are no milestones. You are to process $q$ updates, each of which adds, removes or modifies a milestone. After every update, output the minimum number of turns needed to finish the game, that is, to accumulate at least $a_i$ of $i$-th resource for each $i \\in [1, n]$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 2 \\cdot 10^5$)\u00a0\u2014 the number of types of resources.\n\nThe second line contains $n$ space separated integers $a_1, a_2, \\dots, a_n$ ($1 \\leq a_i \\leq 10^9$), the $i$-th of which is the goal for the $i$-th resource.\n\nThe third line contains a single integer $q$ ($1 \\leq q \\leq 10^5$)\u00a0\u2014 the number of updates to the game milestones.\n\nThen $q$ lines follow, the $j$-th of which contains three space separated integers $s_j$, $t_j$, $u_j$ ($1 \\leq s_j \\leq n$, $1 \\leq t_j < a_{s_j}$, $0 \\leq u_j \\leq n$). For each triple, perform the following actions: First, if there is already a milestone for obtaining $t_j$ units of resource $s_j$, it is removed. If $u_j = 0$, no new milestone is added. If $u_j \\neq 0$, add the following milestone: \"For reaching $t_j$ units of resource $s_j$, gain one free piece of $u_j$.\" Output the minimum number of turns needed to win the game. \n\n\n-----Output-----\n\nOutput $q$ lines, each consisting of a single integer, the $i$-th represents the answer after the $i$-th update.\n\n\n-----Example-----\nInput\n2\n2 3\n5\n2 1 1\n2 2 1\n1 1 1\n2 1 2\n2 2 0\n\nOutput\n4\n3\n3\n2\n3\n\n\n\n-----Note-----\n\nAfter the first update, the optimal strategy is as follows. First produce $2$ once, which gives a free resource $1$. Then, produce $2$ twice and $1$ once, for a total of four turns.\n\nAfter the second update, the optimal strategy is to produce $2$ three times \u2014 the first two times a single unit of resource $1$ is also granted.\n\nAfter the third update, the game is won as follows. First produce $2$ once. This gives a free unit of $1$. This gives additional bonus of resource $1$. After the first turn, the number of resources is thus $[2, 1]$. Next, produce resource $2$ again, which gives another unit of $1$. After this, produce one more unit of $2$. \n\nThe final count of resources is $[3, 3]$, and three turns are needed to reach this situation. Notice that we have more of resource $1$ than its goal, which is of no use."} +{"id": "02303", "text": "Given an input string s, reverse the order of the words.\n\nA word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.\n\nReturn a string of the words in reverse order concatenated by a single space.\n\nNote that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.\n\n\nExample 1:\n\nInput: s = \"the sky is blue\"\nOutput: \"blue is sky the\"\nExample 2:\n\nInput: s = \" hello world \"\nOutput: \"world hello\"\nExplanation: Your reversed string should not contain leading or trailing spaces.\nExample 3:\n\nInput: s = \"a good example\"\nOutput: \"example good a\"\nExplanation: You need to reduce multiple spaces between two words to a single space in the reversed string.\nExample 4:\n\nInput: s = \" Bob Loves Alice \"\nOutput: \"Alice Loves Bob\"\nExample 5:\n\nInput: s = \"Alice does not even like bob\"\nOutput: \"bob like even not does Alice\"\n \n\nConstraints:\n\n1 <= s.length <= 104\ns contains English letters (upper-case and lower-case), digits, and spaces ' '.\nThere is at least one word in s."} +{"id": "02304", "text": "There are N people standing on the x-axis.\nLet the coordinate of Person i be x_i.\nFor every i, x_i is an integer between 0 and 10^9 (inclusive).\nIt is possible that more than one person is standing at the same coordinate.\nYou will given M pieces of information regarding the positions of these people.\nThe i-th piece of information has the form (L_i, R_i, D_i).\nThis means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.\nIt turns out that some of these M pieces of information may be incorrect.\nDetermine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100 000\n - 0 \\leq M \\leq 200 000\n - 1 \\leq L_i, R_i \\leq N (1 \\leq i \\leq M)\n - 0 \\leq D_i \\leq 10 000 (1 \\leq i \\leq M)\n - L_i \\neq R_i (1 \\leq i \\leq M)\n - If i \\neq j, then (L_i, R_i) \\neq (L_j, R_j) and (L_i, R_i) \\neq (R_j, L_j).\n - D_i are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nL_1 R_1 D_1\nL_2 R_2 D_2\n:\nL_M R_M D_M\n\n-----Output-----\nIf there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.\n\n-----Sample Input-----\n3 3\n1 2 1\n2 3 1\n1 3 2\n\n-----Sample Output-----\nYes\n\nSome possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103)."} +{"id": "02305", "text": "We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.\nAdditionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.\nFor each k=1, 2, ..., N, solve the following problem:\n - Find the number of simple paths that visit a vertex painted in the color k one or more times.\nNote: The simple paths from Vertex u to v and from v to u are not distinguished.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq c_i \\leq N\n - 1 \\leq a_i,b_i \\leq N\n - The given graph is a tree.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nc_1 c_2 ... c_N\na_1 b_1\n:\na_{N-1} b_{N-1}\n\n-----Output-----\nPrint the answers for k = 1, 2, ..., N in order, each in its own line.\n\n-----Sample Input-----\n3\n1 2 1\n1 2\n2 3\n\n-----Sample Output-----\n5\n4\n0\n\nLet P_{i,j} denote the simple path connecting Vertex i and j.\nThere are 5 simple paths that visit a vertex painted in the color 1 one or more times:\nP_{1,1}\\,,\\,P_{1,2}\\,,\\,P_{1,3}\\,,\\,P_{2,3}\\,,\\,P_{3,3} \nThere are 4 simple paths that visit a vertex painted in the color 2 one or more times:\nP_{1,2}\\,,\\,P_{1,3}\\,,\\,P_{2,2}\\,,\\,P_{2,3} \nThere are no simple paths that visit a vertex painted in the color 3 one or more times."} +{"id": "02306", "text": "In the year 2168, AtCoder Inc., which is much larger than now, is starting a limited express train service called AtCoder Express.\nIn the plan developed by the president Takahashi, the trains will run as follows:\n - A train will run for (t_1 + t_2 + t_3 + ... + t_N) seconds.\n - In the first t_1 seconds, a train must run at a speed of at most v_1 m/s (meters per second). Similarly, in the subsequent t_2 seconds, a train must run at a speed of at most v_2 m/s, and so on.\nAccording to the specifications of the trains, the acceleration of a train must be always within \u00b11m/s^2. Additionally, a train must stop at the beginning and the end of the run.\nFind the maximum possible distance that a train can cover in the run.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 1 \\leq t_i \\leq 200\n - 1 \\leq v_i \\leq 100\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nt_1 t_2 t_3 \u2026 t_N\nv_1 v_2 v_3 \u2026 v_N\n\n-----Output-----\nPrint the maximum possible that a train can cover in the run.\n\nOutput is considered correct if its absolute difference from the judge's output is at most 10^{-3}.\n\n-----Sample Input-----\n1\n100\n30\n\n-----Sample Output-----\n2100.000000000000000\n\n\nThe maximum distance is achieved when a train runs as follows:\n - In the first 30 seconds, it accelerates at a rate of 1m/s^2, covering 450 meters.\n - In the subsequent 40 seconds, it maintains the velocity of 30m/s, covering 1200 meters.\n - In the last 30 seconds, it decelerates at the acceleration of -1m/s^2, covering 450 meters.\nThe total distance covered is 450 + 1200 + 450 = 2100 meters."} +{"id": "02307", "text": "Kattapa, as you all know was one of the greatest warriors of his time. The kingdom of Maahishmati had never lost a battle under him (as army-chief), and the reason for that was their really powerful army, also called as Mahasena.\nKattapa was known to be a very superstitious person. He believed that a soldier is \"lucky\" if the soldier is holding an even number of weapons, and \"unlucky\" otherwise. He considered the army as \"READY FOR BATTLE\" if the count of \"lucky\" soldiers is strictly greater than the count of \"unlucky\" soldiers, and \"NOT READY\" otherwise.\nGiven the number of weapons each soldier is holding, your task is to determine whether the army formed by all these soldiers is \"READY FOR BATTLE\" or \"NOT READY\".\nNote: You can find the definition of an even number here.\n\n-----Input-----\n\nThe first line of input consists of a single integer N denoting the number of soldiers. The second line of input consists of N space separated integers A1, A2, ..., AN, where Ai denotes the number of weapons that the ith soldier is holding.\n\n-----Output-----\nGenerate one line output saying \"READY FOR BATTLE\", if the army satisfies the conditions that Kattapa requires or \"NOT READY\" otherwise (quotes for clarity).\n\n-----Constraints-----\n- 1 \u2264 N \u2264 100\n- 1 \u2264 Ai \u2264 100\n\n-----Example 1-----\nInput:\n1\n1\n\nOutput:\nNOT READY\n\n-----Example 2-----\nInput:\n1\n2\n\nOutput:\nREADY FOR BATTLE\n\n-----Example 3-----\nInput:\n4\n11 12 13 14\n\nOutput:\nNOT READY\n\n-----Example 4-----\nInput:\n3\n2 3 4\n\nOutput:\nREADY FOR BATTLE\n\n-----Example 5-----\nInput:\n5\n1 2 3 4 5\n\nOutput:\nNOT READY\n\n-----Explanation-----\n\n- Example 1: For the first example, N = 1 and the array A = [1]. There is only 1 soldier and he is holding 1 weapon, which is odd. The number of soldiers holding an even number of weapons = 0, and number of soldiers holding an odd number of weapons = 1. Hence, the answer is \"NOT READY\" since the number of soldiers holding an even number of weapons is not greater than the number of soldiers holding an odd number of weapons.\n\n- Example 2: For the second example, N = 1 and the array A = [2]. There is only 1 soldier and he is holding 2 weapons, which is even. The number of soldiers holding an even number of weapons = 1, and number of soldiers holding an odd number of weapons = 0. Hence, the answer is \"READY FOR BATTLE\" since the number of soldiers holding an even number of weapons is greater than the number of soldiers holding an odd number of weapons.\n\n- Example 3: For the third example, N = 4 and the array A = [11, 12, 13, 14]. The 1st soldier is holding 11 weapons (which is odd), the 2nd soldier is holding 12 weapons (which is even), the 3rd soldier is holding 13 weapons (which is odd), and the 4th soldier is holding 14 weapons (which is even). The number of soldiers holding an even number of weapons = 2, and number of soldiers holding an odd number of weapons = 2. Notice that we have an equal number of people holding even number of weapons and odd number of weapons. The answer here is \"NOT READY\" since the number of soldiers holding an even number of weapons is not strictly greater than the number of soldiers holding an odd number of weapons.\n\n- Example 4: For the fourth example, N = 3 and the array A = [2, 3, 4]. The 1st soldier is holding 2 weapons (which is even), the 2nd soldier is holding 3 weapons (which is odd), and the 3rd soldier is holding 4 weapons (which is even). The number of soldiers holding an even number of weapons = 2, and number of soldiers holding an odd number of weapons = 1. Hence, the answer is \"READY FOR BATTLE\" since the number of soldiers holding an even number of weapons is greater than the number of soldiers holding an odd number of weapons.\n\n- Example 5: For the fifth example, N = 5 and the array A = [1, 2, 3, 4, 5]. The 1st soldier is holding 1 weapon (which is odd), the 2nd soldier is holding 2 weapons (which is even), the 3rd soldier is holding 3 weapons (which is odd), the 4th soldier is holding 4 weapons (which is even), and the 5th soldier is holding 5 weapons (which is odd). The number of soldiers holding an even number of weapons = 2, and number of soldiers holding an odd number of weapons = 3. Hence, the answer is \"NOT READY\" since the number of soldiers holding an even number of weapons is not greater than the number of soldiers holding an odd number of weapons."} +{"id": "02308", "text": "You are given two binary strings $x$ and $y$, which are binary representations of some two integers (let's denote these integers as $f(x)$ and $f(y)$). You can choose any integer $k \\ge 0$, calculate the expression $s_k = f(x) + f(y) \\cdot 2^k$ and write the binary representation of $s_k$ in reverse order (let's denote it as $rev_k$). For example, let $x = 1010$ and $y = 11$; you've chosen $k = 1$ and, since $2^1 = 10_2$, so $s_k = 1010_2 + 11_2 \\cdot 10_2 = 10000_2$ and $rev_k = 00001$.\n\nFor given $x$ and $y$, you need to choose such $k$ that $rev_k$ is lexicographically minimal (read notes if you don't know what does \"lexicographically\" means).\n\nIt's guaranteed that, with given constraints, $k$ exists and is finite.\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\le T \\le 100$) \u2014 the number of queries.\n\nNext $2T$ lines contain a description of queries: two lines per query. The first line contains one binary string $x$, consisting of no more than $10^5$ characters. Each character is either 0 or 1.\n\nThe second line contains one binary string $y$, consisting of no more than $10^5$ characters. Each character is either 0 or 1.\n\nIt's guaranteed, that $1 \\le f(y) \\le f(x)$ (where $f(x)$ is the integer represented by $x$, and $f(y)$ is the integer represented by $y$), both representations don't have any leading zeroes, the total length of $x$ over all queries doesn't exceed $10^5$, and the total length of $y$ over all queries doesn't exceed $10^5$.\n\n\n-----Output-----\n\nPrint $T$ integers (one per query). For each query print such $k$ that $rev_k$ is lexicographically minimal.\n\n\n-----Example-----\nInput\n4\n1010\n11\n10001\n110\n1\n1\n1010101010101\n11110000\n\nOutput\n1\n3\n0\n0\n\n\n\n-----Note-----\n\nThe first query was described in the legend.\n\nIn the second query, it's optimal to choose $k = 3$. The $2^3 = 1000_2$ so $s_3 = 10001_2 + 110_2 \\cdot 1000_2 = 10001 + 110000 = 1000001$ and $rev_3 = 1000001$. For example, if $k = 0$, then $s_0 = 10111$ and $rev_0 = 11101$, but $rev_3 = 1000001$ is lexicographically smaller than $rev_0 = 11101$.\n\nIn the third query $s_0 = 10$ and $rev_0 = 01$. For example, $s_2 = 101$ and $rev_2 = 101$. And $01$ is lexicographically smaller than $101$.\n\nThe quote from Wikipedia: \"To determine which of two strings of characters comes when arranging in lexicographical order, their first letters are compared. If they differ, then the string whose first letter comes earlier in the alphabet comes before the other string. If the first letters are the same, then the second letters are compared, and so on. If a position is reached where one string has no more letters to compare while the other does, then the first (shorter) string is deemed to come first in alphabetical order.\""} +{"id": "02309", "text": "You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible.\n\nEach lyric consists of two lines. Each line consists of two words separated by whitespace. \n\nA lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. \n\nAlso, letters \"a\", \"e\", \"o\", \"i\", and \"u\" are vowels. Note that \"y\" is never vowel.\n\nFor example of a beautiful lyric, \"hello hellooowww\" \n\n\"whatsup yowowowow\" is a beautiful lyric because there are two vowels each in \"hello\" and \"whatsup\", four vowels each in \"hellooowww\" and \"yowowowow\" (keep in mind that \"y\" is not a vowel), and the last vowel of each line is \"o\".\n\nFor example of a not beautiful lyric, \"hey man\"\n\n\"iam mcdic\" is not a beautiful lyric because \"hey\" and \"iam\" don't have same number of vowels and the last vowels of two lines are different (\"a\" in the first and \"i\" in the second).\n\nHow many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times.\n\n\n-----Input-----\n\nThe first line contains single integer $n$ ($1 \\le n \\le 10^{5}$)\u00a0\u2014 the number of words.\n\nThe $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters\u00a0\u2014 the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel.\n\n\n-----Output-----\n\nIn the first line, print $m$\u00a0\u2014 the number of maximum possible beautiful lyrics.\n\nIn next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric).\n\nIf there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n14\nwow\nthis\nis\nthe\nfirst\nmcdics\ncodeforces\nround\nhooray\ni\nam\nproud\nabout\nthat\n\nOutput\n3\nabout proud\nhooray round\nwow first\nthis is\ni that\nmcdics am\n\nInput\n7\narsijo\nsuggested\nthe\nidea\nfor\nthis\nproblem\n\nOutput\n0\n\nInput\n4\nsame\nsame\nsame\ndiffer\n\nOutput\n1\nsame differ\nsame same\n\n\n\n-----Note-----\n\nIn the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. \"about proud hooray round\" forms a beautiful lyric because \"about\" and \"hooray\" have same number of vowels, \"proud\" and \"round\" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word \"codeforces\".\n\nIn the second example, you cannot form any beautiful lyric from given words.\n\nIn the third example, you can use the word \"same\" up to three times."} +{"id": "02310", "text": "Polycarp is flying in the airplane. Finally, it is his favorite time \u2014 the lunchtime. The BerAvia company stewardess is giving food consecutively to all the passengers from the 1-th one to the last one. Polycarp is sitting on seat m, that means, he will be the m-th person to get food.\n\nThe flight menu has k dishes in total and when Polycarp boarded the flight, he had time to count the number of portions of each dish on board. Thus, he knows values a_1, a_2, ..., a_{k}, where a_{i} is the number of portions of the i-th dish.\n\nThe stewardess has already given food to m - 1 passengers, gave Polycarp a polite smile and asked him what he would prefer. That's when Polycarp realized that they might have run out of some dishes by that moment. For some of the m - 1 passengers ahead of him, he noticed what dishes they were given. Besides, he's heard some strange mumbling from some of the m - 1 passengers ahead of him, similar to phrase 'I'm disappointed'. That happened when a passenger asked for some dish but the stewardess gave him a polite smile and said that they had run out of that dish. In that case the passenger needed to choose some other dish that was available. If Polycarp heard no more sounds from a passenger, that meant that the passenger chose his dish at the first try.\n\nHelp Polycarp to find out for each dish: whether they could have run out of the dish by the moment Polyarp was served or that dish was definitely available.\n\n\n-----Input-----\n\nEach test in this problem consists of one or more input sets. First goes a string that contains a single integer t (1 \u2264 t \u2264 100 000) \u2014 the number of input data sets in the test. Then the sets follow, each set is preceded by an empty line.\n\nThe first line of each set of the input contains integers m, k (2 \u2264 m \u2264 100 000, 1 \u2264 k \u2264 100 000) \u2014 the number of Polycarp's seat and the number of dishes, respectively.\n\nThe second line contains a sequence of k integers a_1, a_2, ..., a_{k} (1 \u2264 a_{i} \u2264 100 000), where a_{i} is the initial number of portions of the i-th dish.\n\nThen m - 1 lines follow, each line contains the description of Polycarp's observations about giving food to a passenger sitting in front of him: the j-th line contains a pair of integers t_{j}, r_{j} (0 \u2264 t_{j} \u2264 k, 0 \u2264 r_{j} \u2264 1), where t_{j} is the number of the dish that was given to the j-th passenger (or 0, if Polycarp didn't notice what dish was given to the passenger), and r_{j} \u2014 a 1 or a 0, depending on whether the j-th passenger was or wasn't disappointed, respectively.\n\nWe know that sum a_{i} equals at least m, that is,Polycarp will definitely get some dish, even if it is the last thing he wanted. It is guaranteed that the data is consistent.\n\nSum m for all input sets doesn't exceed 100 000. Sum k for all input sets doesn't exceed 100 000.\n\n\n-----Output-----\n\nFor each input set print the answer as a single line. Print a string of k letters \"Y\" or \"N\". Letter \"Y\" in position i should be printed if they could have run out of the i-th dish by the time the stewardess started serving Polycarp.\n\n\n-----Examples-----\nInput\n2\n\n3 4\n2 3 2 1\n1 0\n0 0\n\n5 5\n1 2 1 3 1\n3 0\n0 0\n2 1\n4 0\n\nOutput\nYNNY\nYYYNY\n\n\n\n-----Note-----\n\nIn the first input set depending on the choice of the second passenger the situation could develop in different ways: If he chose the first dish, then by the moment the stewardess reaches Polycarp, they will have run out of the first dish; If he chose the fourth dish, then by the moment the stewardess reaches Polycarp, they will have run out of the fourth dish; Otherwise, Polycarp will be able to choose from any of the four dishes. \n\nThus, the answer is \"YNNY\".\n\nIn the second input set there is, for example, the following possible scenario. First, the first passenger takes the only third dish, then the second passenger takes the second dish. Then, the third passenger asks for the third dish, but it is not available, so he makes disappointed muttering and ends up with the second dish. Then the fourth passenger takes the fourth dish, and Polycarp ends up with the choice between the first, fourth and fifth dish.\n\nLikewise, another possible scenario is when by the time the stewardess comes to Polycarp, they will have run out of either the first or the fifth dish (this can happen if one of these dishes is taken by the second passenger). It is easy to see that there is more than enough of the fourth dish, so Polycarp can always count on it. Thus, the answer is \"YYYNY\"."} +{"id": "02311", "text": "You are given an array $a$ of length $n$ and array $b$ of length $m$ both consisting of only integers $0$ and $1$. Consider a matrix $c$ of size $n \\times m$ formed by following rule: $c_{i, j} = a_i \\cdot b_j$ (i.e. $a_i$ multiplied by $b_j$). It's easy to see that $c$ consists of only zeroes and ones too.\n\nHow many subrectangles of size (area) $k$ consisting only of ones are there in $c$?\n\nA subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers $x_1, x_2, y_1, y_2$ ($1 \\le x_1 \\le x_2 \\le n$, $1 \\le y_1 \\le y_2 \\le m$) a subrectangle $c[x_1 \\dots x_2][y_1 \\dots y_2]$ is an intersection of the rows $x_1, x_1+1, x_1+2, \\dots, x_2$ and the columns $y_1, y_1+1, y_1+2, \\dots, y_2$.\n\nThe size (area) of a subrectangle is the total number of cells in it.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $k$ ($1 \\leq n, m \\leq 40\\,000, 1 \\leq k \\leq n \\cdot m$), length of array $a$, length of array $b$ and required size of subrectangles.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\leq a_i \\leq 1$), elements of $a$.\n\nThe third line contains $m$ integers $b_1, b_2, \\ldots, b_m$ ($0 \\leq b_i \\leq 1$), elements of $b$.\n\n\n-----Output-----\n\nOutput single integer\u00a0\u2014 the number of subrectangles of $c$ with size (area) $k$ consisting only of ones.\n\n\n-----Examples-----\nInput\n3 3 2\n1 0 1\n1 1 1\n\nOutput\n4\n\nInput\n3 5 4\n1 1 1\n1 1 1 1 1\n\nOutput\n14\n\n\n\n-----Note-----\n\nIn first example matrix $c$ is:\n\n $\\left(\\begin{array}{l l l}{1} & {1} & {1} \\\\{0} & {0} & {0} \\\\{1} & {1} & {1} \\end{array} \\right)$ \n\nThere are $4$ subrectangles of size $2$ consisting of only ones in it:\n\n [Image] \n\nIn second example matrix $c$ is:\n\n $\\left(\\begin{array}{l l l l l}{1} & {1} & {1} & {1} & {1} \\\\{1} & {1} & {1} & {1} & {1} \\\\{1} & {1} & {1} & {1} & {1} \\end{array} \\right)$"} +{"id": "02312", "text": "You're given an array $b$ of length $n$. Let's define another array $a$, also of length $n$, for which $a_i = 2^{b_i}$ ($1 \\leq i \\leq n$). \n\nValerii says that every two non-intersecting subarrays of $a$ have different sums of elements. You want to determine if he is wrong. More formally, you need to determine if there exist four integers $l_1,r_1,l_2,r_2$ that satisfy the following conditions: $1 \\leq l_1 \\leq r_1 \\lt l_2 \\leq r_2 \\leq n$; $a_{l_1}+a_{l_1+1}+\\ldots+a_{r_1-1}+a_{r_1} = a_{l_2}+a_{l_2+1}+\\ldots+a_{r_2-1}+a_{r_2}$. \n\nIf such four integers exist, you will prove Valerii wrong. Do they exist?\n\nAn array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 100$). Description of the test cases follows.\n\nThe first line of every test case contains a single integer $n$ ($2 \\le n \\le 1000$).\n\nThe second line of every test case contains $n$ integers $b_1,b_2,\\ldots,b_n$ ($0 \\le b_i \\le 10^9$). \n\n\n-----Output-----\n\nFor every test case, if there exist two non-intersecting subarrays in $a$ that have the same sum, output YES on a separate line. Otherwise, output NO on a separate line. \n\nAlso, note that each letter can be in any case. \n\n\n-----Example-----\nInput\n2\n6\n4 3 0 1 2 0\n2\n2 5\n\nOutput\nYES\nNO\n\n\n\n-----Note-----\n\nIn the first case, $a = [16,8,1,2,4,1]$. Choosing $l_1 = 1$, $r_1 = 1$, $l_2 = 2$ and $r_2 = 6$ works because $16 = (8+1+2+4+1)$.\n\nIn the second case, you can verify that there is no way to select to such subarrays."} +{"id": "02313", "text": "You are creating a level for a video game. The level consists of $n$ rooms placed in a circle. The rooms are numbered $1$ through $n$. Each room contains exactly one exit: completing the $j$-th room allows you to go the $(j+1)$-th room (and completing the $n$-th room allows you to go the $1$-st room).\n\nYou are given the description of the multiset of $n$ chests: the $i$-th chest has treasure value $c_i$.\n\nEach chest can be of one of two types: regular chest\u00a0\u2014 when a player enters a room with this chest, he grabs the treasure and proceeds to the next room; mimic chest\u00a0\u2014 when a player enters a room with this chest, the chest eats him alive, and he loses. \n\nThe player starts in a random room with each room having an equal probability of being chosen. The players earnings is equal to the total value of treasure chests he'd collected before he lost.\n\nYou are allowed to choose the order the chests go into the rooms. For each $k$ from $1$ to $n$ place the chests into the rooms in such a way that:\n\n each room contains exactly one chest; exactly $k$ chests are mimics; the expected value of players earnings is minimum possible. \n\nPlease note that for each $k$ the placement is chosen independently.\n\nIt can be shown that it is in the form of $\\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \\ne 0$. Report the values of $P \\cdot Q^{-1} \\pmod {998244353}$.\n\n\n-----Input-----\n\nThe first contains a single integer $n$ ($2 \\le n \\le 3 \\cdot 10^5$)\u00a0\u2014 the number of rooms and the number of chests.\n\nThe second line contains $n$ integers $c_1, c_2, \\dots, c_n$ ($1 \\le c_i \\le 10^6$)\u00a0\u2014 the treasure values of each chest.\n\n\n-----Output-----\n\nPrint $n$ integers\u00a0\u2014 the $k$\u00a0-th value should be equal to the minimum possible expected value of players earnings if the chests are placed into the rooms in some order and exactly $k$ of the chests are mimics.\n\nIt can be shown that it is in the form of $\\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \\ne 0$. Report the values of $P \\cdot Q^{-1} \\pmod {998244353}$.\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n499122177 0 \n\nInput\n8\n10 4 3 6 5 10 7 5\n\nOutput\n499122193 249561095 249561092 873463811 499122178 124780545 623902721 0 \n\n\n\n-----Note-----\n\nIn the first example the exact values of minimum expected values are: $\\frac 1 2$, $\\frac 0 2$.\n\nIn the second example the exact values of minimum expected values are: $\\frac{132} 8$, $\\frac{54} 8$, $\\frac{30} 8$, $\\frac{17} 8$, $\\frac{12} 8$, $\\frac 7 8$, $\\frac 3 8$, $\\frac 0 8$."} +{"id": "02314", "text": "Warawreh created a great company called Nanosoft. The only thing that Warawreh still has to do is to place a large picture containing its logo on top of the company's building.\n\nThe logo of Nanosoft can be described as four squares of the same size merged together into one large square. The top left square is colored with red, the top right square is colored with green, the bottom left square is colored with yellow and the bottom right square is colored with blue.\n\nAn Example of some correct logos:\n\n[Image]\n\nAn Example of some incorrect logos:\n\n[Image]\n\nWarawreh went to Adhami's store in order to buy the needed picture. Although Adhami's store is very large he has only one picture that can be described as a grid of $n$ rows and $m$ columns. The color of every cell in the picture will be green (the symbol 'G'), red (the symbol 'R'), yellow (the symbol 'Y') or blue (the symbol 'B').\n\nAdhami gave Warawreh $q$ options, in every option he gave him a sub-rectangle from that picture and told him that he can cut that sub-rectangle for him. To choose the best option, Warawreh needs to know for every option the maximum area of sub-square inside the given sub-rectangle that can be a Nanosoft logo. If there are no such sub-squares, the answer is $0$.\n\nWarawreh couldn't find the best option himself so he asked you for help, can you help him?\n\n\n-----Input-----\n\nThe first line of input contains three integers $n$, $m$ and $q$ $(1 \\leq n , m \\leq 500, 1 \\leq q \\leq 3 \\cdot 10^{5})$ \u00a0\u2014 the number of row, the number columns and the number of options.\n\nFor the next $n$ lines, every line will contain $m$ characters. In the $i$-th line the $j$-th character will contain the color of the cell at the $i$-th row and $j$-th column of the Adhami's picture. The color of every cell will be one of these: {'G','Y','R','B'}.\n\nFor the next $q$ lines, the input will contain four integers $r_1$, $c_1$, $r_2$ and $c_2$ $(1 \\leq r_1 \\leq r_2 \\leq n, 1 \\leq c_1 \\leq c_2 \\leq m)$. In that option, Adhami gave to Warawreh a sub-rectangle of the picture with the upper-left corner in the cell $(r_1, c_1)$ and with the bottom-right corner in the cell $(r_2, c_2)$.\n\n\n-----Output-----\n\nFor every option print the maximum area of sub-square inside the given sub-rectangle, which can be a NanoSoft Logo. If there are no such sub-squares, print $0$.\n\n\n-----Examples-----\nInput\n5 5 5\nRRGGB\nRRGGY\nYYBBG\nYYBBR\nRBBRG\n1 1 5 5\n2 2 5 5\n2 2 3 3\n1 1 3 5\n4 4 5 5\n\nOutput\n16\n4\n4\n4\n0\n\nInput\n6 10 5\nRRRGGGRRGG\nRRRGGGRRGG\nRRRGGGYYBB\nYYYBBBYYBB\nYYYBBBRGRG\nYYYBBBYBYB\n1 1 6 10\n1 3 3 10\n2 2 6 6\n1 7 6 10\n2 1 5 10\n\nOutput\n36\n4\n16\n16\n16\n\nInput\n8 8 8\nRRRRGGGG\nRRRRGGGG\nRRRRGGGG\nRRRRGGGG\nYYYYBBBB\nYYYYBBBB\nYYYYBBBB\nYYYYBBBB\n1 1 8 8\n5 2 5 7\n3 1 8 6\n2 3 5 8\n1 2 6 8\n2 1 5 5\n2 1 7 7\n6 5 7 5\n\nOutput\n64\n0\n16\n4\n16\n4\n36\n0\n\n\n\n-----Note-----\n\nPicture for the first test:\n\n[Image]\n\nThe pictures from the left to the right corresponds to the options. The border of the sub-rectangle in the option is marked with black, the border of the sub-square with the maximal possible size, that can be cut is marked with gray."} +{"id": "02315", "text": "You are given a range of positive integers from $l$ to $r$.\n\nFind such a pair of integers $(x, y)$ that $l \\le x, y \\le r$, $x \\ne y$ and $x$ divides $y$.\n\nIf there are multiple answers, print any of them.\n\nYou are also asked to answer $T$ independent queries.\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\le T \\le 1000$) \u2014 the number of queries.\n\nEach of the next $T$ lines contains two integers $l$ and $r$ ($1 \\le l \\le r \\le 998244353$) \u2014 inclusive borders of the range.\n\nIt is guaranteed that testset only includes queries, which have at least one suitable pair.\n\n\n-----Output-----\n\nPrint $T$ lines, each line should contain the answer \u2014 two integers $x$ and $y$ such that $l \\le x, y \\le r$, $x \\ne y$ and $x$ divides $y$. The answer in the $i$-th line should correspond to the $i$-th query from the input.\n\nIf there are multiple answers, print any of them.\n\n\n-----Example-----\nInput\n3\n1 10\n3 14\n1 10\n\nOutput\n1 7\n3 9\n5 10"} +{"id": "02316", "text": "Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. \n\nOne day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon.[Image]\u00a0\n\nThe dragon has a hit point of $x$ initially. When its hit point goes to $0$ or under $0$, it will be defeated. In order to defeat the dragon, Kana can cast the two following types of spells. Void Absorption \n\nAssume that the dragon's current hit point is $h$, after casting this spell its hit point will become $\\left\\lfloor \\frac{h}{2} \\right\\rfloor + 10$. Here $\\left\\lfloor \\frac{h}{2} \\right\\rfloor$ denotes $h$ divided by two, rounded down. Lightning Strike \n\nThis spell will decrease the dragon's hit point by $10$. Assume that the dragon's current hit point is $h$, after casting this spell its hit point will be lowered to $h-10$.\n\nDue to some reasons Kana can only cast no more than $n$ Void Absorptions and $m$ Lightning Strikes. She can cast the spells in any order and doesn't have to cast all the spells. Kana isn't good at math, so you are going to help her to find out whether it is possible to defeat the dragon.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 1000$) \u00a0\u2014 the number of test cases.\n\nThe next $t$ lines describe test cases. For each test case the only line contains three integers $x$, $n$, $m$ ($1\\le x \\le 10^5$, $0\\le n,m\\le30$) \u00a0\u2014 the dragon's intitial hit point, the maximum number of Void Absorptions and Lightning Strikes Kana can cast respectively.\n\n\n-----Output-----\n\nIf it is possible to defeat the dragon, print \"YES\" (without quotes). Otherwise, print \"NO\" (without quotes).\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n7\n100 3 4\n189 3 4\n64 2 3\n63 2 3\n30 27 7\n10 9 1\n69117 21 2\n\nOutput\nYES\nNO\nNO\nYES\nYES\nYES\nYES\n\n\n\n-----Note-----\n\nOne possible casting sequence of the first test case is shown below: Void Absorption $\\left\\lfloor \\frac{100}{2} \\right\\rfloor + 10=60$. Lightning Strike $60-10=50$. Void Absorption $\\left\\lfloor \\frac{50}{2} \\right\\rfloor + 10=35$. Void Absorption $\\left\\lfloor \\frac{35}{2} \\right\\rfloor + 10=27$. Lightning Strike $27-10=17$. Lightning Strike $17-10=7$. Lightning Strike $7-10=-3$."} +{"id": "02317", "text": "Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages.\n\nThe river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b).\n\nThe west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points A_{i} = (a, y_{i}). The villagers there are plain and simple, so their pathways are straight segments as well.\n\nThe east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points B_{i} = (b, y'_{i}). The lengths of all these paths are known, the length of the path that leads from the eastern village to point B_{i}, equals l_{i}.\n\nThe villagers want to choose exactly one point on the left bank of river A_{i}, exactly one point on the right bank B_{j} and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OA_{i}| + |A_{i}B_{j}| + l_{j}, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x_1, y_1) and (x_2, y_2) equals $\\sqrt{(x_{1} - x_{2})^{2} +(y_{1} - y_{2})^{2}}$.\n\nHelp them and find the required pair of points.\n\n\n-----Input-----\n\nThe first line contains integers n, m, a, b (1 \u2264 n, m \u2264 10^5, 0 < a < b < 10^6). \n\nThe second line contains n integers in the ascending order: the i-th integer determines the coordinate of point A_{i} and equals y_{i} (|y_{i}| \u2264 10^6). \n\nThe third line contains m integers in the ascending order: the i-th integer determines the coordinate of point B_{i} and equals y'_{i} (|y'_{i}| \u2264 10^6). \n\nThe fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point B_{i}, and equals l_{i} (1 \u2264 l_{i} \u2264 10^6).\n\nIt is guaranteed, that there is such a point C with abscissa at least b, that |B_{i}C| \u2264 l_{i} for all i (1 \u2264 i \u2264 m). It is guaranteed that no two points A_{i} coincide. It is guaranteed that no two points B_{i} coincide.\n\n\n-----Output-----\n\nPrint two integers \u2014 the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input.\n\nIf there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10^{ - 6} in absolute or relative value.\n\n\n-----Examples-----\nInput\n3 2 3 5\n-2 -1 4\n-1 2\n7 3\n\nOutput\n2 2"} +{"id": "02318", "text": "Methodius received an email from his friend Polycarp. However, Polycarp's keyboard is broken, so pressing a key on it once may cause the corresponding symbol to appear more than once (if you press a key on a regular keyboard, it prints exactly one symbol).\n\nFor example, as a result of typing the word \"hello\", the following words could be printed: \"hello\", \"hhhhello\", \"hheeeellllooo\", but the following could not be printed: \"hell\", \"helo\", \"hhllllooo\".\n\nNote, that when you press a key, the corresponding symbol must appear (possibly, more than once). The keyboard is broken in a random manner, it means that pressing the same key you can get the different number of letters in the result.\n\nFor each word in the letter, Methodius has guessed what word Polycarp actually wanted to write, but he is not sure about it, so he asks you to help him.\n\nYou are given a list of pairs of words. For each pair, determine if the second word could be printed by typing the first one on Polycarp's keyboard.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 10^5$) \u2014 the number of pairs to check. Further input contains $n$ descriptions of pairs.\n\nThe first line of each description contains a single non-empty word $s$ consisting of lowercase Latin letters. The second line of the description contains a single non-empty word $t$ consisting of lowercase Latin letters. The lengths of both strings are not greater than $10^6$.\n\nIt is guaranteed that the total length of all words $s$ in the input is not greater than $10^6$. Also, it is guaranteed that the total length of all words $t$ in the input is not greater than $10^6$.\n\n\n-----Output-----\n\nOutput $n$ lines. In the $i$-th line for the $i$-th pair of words $s$ and $t$ print YES if the word $t$ could be printed by typing the word $s$. Otherwise, print NO.\n\n\n-----Examples-----\nInput\n4\nhello\nhello\nhello\nhelloo\nhello\nhlllloo\nhello\nhelo\n\nOutput\nYES\nYES\nNO\nNO\n\nInput\n5\naa\nbb\ncodeforces\ncodeforce\npolycarp\npoolycarpp\naaaa\naaaab\nabcdefghijklmnopqrstuvwxyz\nzabcdefghijklmnopqrstuvwxyz\n\nOutput\nNO\nNO\nYES\nNO\nNO"} +{"id": "02319", "text": "You are given two strings $s$ and $t$, each of length $n$ and consisting of lowercase Latin alphabets. You want to make $s$ equal to $t$. \n\nYou can perform the following operation on $s$ any number of times to achieve it\u00a0\u2014 Choose any substring of $s$ and rotate it clockwise once, that is, if the selected substring is $s[l,l+1...r]$, then it becomes $s[r,l,l + 1 ... r - 1]$. All the remaining characters of $s$ stay in their position. \n\nFor example, on rotating the substring $[2,4]$ , string \"abcde\" becomes \"adbce\". \n\nA string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.\n\nFind the minimum number of operations required to convert $s$ to $t$, or determine that it's impossible.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $t$ $(1\\leq t \\leq 2000)$\u00a0\u2014 the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ $(1\\leq n \\leq 2000)$\u00a0\u2014 the length of the strings. \n\nThe second and the third lines contain strings $s$ and $t$ respectively.\n\nThe sum of $n$ over all the test cases does not exceed $2000$.\n\n\n-----Output-----\n\nFor each test case, output the minimum number of operations to convert $s$ to $t$. If it is not possible to convert $s$ to $t$, output $-1$ instead.\n\n\n-----Example-----\nInput\n6\n1\na\na\n2\nab\nba\n3\nabc\ncab\n3\nabc\ncba\n4\nabab\nbaba\n4\nabcc\naabc\n\nOutput\n0\n1\n1\n2\n1\n-1\n\n\n\n-----Note-----\n\nFor the $1$-st test case, since $s$ and $t$ are equal, you don't need to apply any operation.\n\nFor the $2$-nd test case, you only need to apply one operation on the entire string ab to convert it to ba.\n\nFor the $3$-rd test case, you only need to apply one operation on the entire string abc to convert it to cab.\n\nFor the $4$-th test case, you need to apply the operation twice: first on the entire string abc to convert it to cab and then on the substring of length $2$ beginning at the second character to convert it to cba.\n\nFor the $5$-th test case, you only need to apply one operation on the entire string abab to convert it to baba.\n\nFor the $6$-th test case, it is not possible to convert string $s$ to $t$."} +{"id": "02320", "text": "The problem was inspired by Pied Piper story. After a challenge from Hooli's compression competitor Nucleus, Richard pulled an all-nighter to invent a new approach to compression: middle-out.\n\nYou are given two strings $s$ and $t$ of the same length $n$. Their characters are numbered from $1$ to $n$ from left to right (i.e. from the beginning to the end).\n\nIn a single move you can do the following sequence of actions:\n\n choose any valid index $i$ ($1 \\le i \\le n$), move the $i$-th character of $s$ from its position to the beginning of the string or move the $i$-th character of $s$ from its position to the end of the string. \n\nNote, that the moves don't change the length of the string $s$. You can apply a move only to the string $s$.\n\nFor example, if $s=$\"test\" in one move you can obtain:\n\n if $i=1$ and you move to the beginning, then the result is \"test\" (the string doesn't change), if $i=2$ and you move to the beginning, then the result is \"etst\", if $i=3$ and you move to the beginning, then the result is \"stet\", if $i=4$ and you move to the beginning, then the result is \"ttes\", if $i=1$ and you move to the end, then the result is \"estt\", if $i=2$ and you move to the end, then the result is \"tste\", if $i=3$ and you move to the end, then the result is \"tets\", if $i=4$ and you move to the end, then the result is \"test\" (the string doesn't change). \n\nYou want to make the string $s$ equal to the string $t$. What is the minimum number of moves you need? If it is impossible to transform $s$ to $t$, print -1.\n\n\n-----Input-----\n\nThe first line contains integer $q$ ($1 \\le q \\le 100$) \u2014 the number of independent test cases in the input.\n\nEach test case is given in three lines. The first line of a test case contains $n$ ($1 \\le n \\le 100$) \u2014 the length of the strings $s$ and $t$. The second line contains $s$, the third line contains $t$. Both strings $s$ and $t$ have length $n$ and contain only lowercase Latin letters.\n\nThere are no constraints on the sum of $n$ in the test (i.e. the input with $q=100$ and all $n=100$ is allowed).\n\n\n-----Output-----\n\nFor every test print minimum possible number of moves, which are needed to transform $s$ into $t$, or -1, if it is impossible to do.\n\n\n-----Examples-----\nInput\n3\n9\niredppipe\npiedpiper\n4\nestt\ntest\n4\ntste\ntest\n\nOutput\n2\n1\n2\n\nInput\n4\n1\na\nz\n5\nadhas\ndasha\n5\naashd\ndasha\n5\naahsd\ndasha\n\nOutput\n-1\n2\n2\n3\n\n\n\n-----Note-----\n\nIn the first example, the moves in one of the optimal answers are:\n\n for the first test case $s=$\"iredppipe\", $t=$\"piedpiper\": \"iredppipe\" $\\rightarrow$ \"iedppiper\" $\\rightarrow$ \"piedpiper\"; for the second test case $s=$\"estt\", $t=$\"test\": \"estt\" $\\rightarrow$ \"test\"; for the third test case $s=$\"tste\", $t=$\"test\": \"tste\" $\\rightarrow$ \"etst\" $\\rightarrow$ \"test\"."} +{"id": "02321", "text": "You have a string $s$ of length $n$ consisting of only characters > and <. You may do some operations with this string, for each operation you have to choose some character that still remains in the string. If you choose a character >, the character that comes right after it is deleted (if the character you chose was the last one, nothing happens). If you choose a character <, the character that comes right before it is deleted (if the character you chose was the first one, nothing happens).\n\nFor example, if we choose character > in string > > < >, the string will become to > > >. And if we choose character < in string > <, the string will become to <.\n\nThe string is good if there is a sequence of operations such that after performing it only one character will remain in the string. For example, the strings >, > > are good. \n\nBefore applying the operations, you may remove any number of characters from the given string (possibly none, possibly up to $n - 1$, but not the whole string). You need to calculate the minimum number of characters to be deleted from string $s$ so that it becomes good.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2013 the number of test cases. Each test case is represented by two lines.\n\nThe first line of $i$-th test case contains one integer $n$ ($1 \\le n \\le 100$) \u2013 the length of string $s$.\n\nThe second line of $i$-th test case contains string $s$, consisting of only characters > and <.\n\n\n-----Output-----\n\nFor each test case print one line.\n\nFor $i$-th test case print the minimum number of characters to be deleted from string $s$ so that it becomes good.\n\n\n-----Example-----\nInput\n3\n2\n<>\n3\n><<\n1\n>\n\nOutput\n1\n0\n0\n\n\n\n-----Note-----\n\nIn the first test case we can delete any character in string <>.\n\nIn the second test case we don't need to delete any characters. The string > < < is good, because we can perform the following sequence of operations: > < < $\\rightarrow$ < < $\\rightarrow$ <."} +{"id": "02322", "text": "\u0412\u0430\u043c \u0437\u0430\u0434\u0430\u043d\u043e \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u043e\u0435 \u043a\u043b\u0435\u0442\u0447\u0430\u0442\u043e\u0435 \u043f\u043e\u043b\u0435, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0438\u0437 n \u0441\u0442\u0440\u043e\u043a \u0438 m \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432. \u041f\u043e\u043b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0446\u0438\u043a\u043b \u0438\u0437 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u00ab*\u00bb, \u0442\u0430\u043a\u043e\u0439 \u0447\u0442\u043e: \u0446\u0438\u043a\u043b \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438, \u043f\u043e\u0441\u0435\u0442\u0438\u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u0435\u0433\u043e \u043a\u043b\u0435\u0442\u043a\u0443 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u044f\u0441\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u0432\u0435\u0440\u0445/\u0432\u043d\u0438\u0437/\u0432\u043f\u0440\u0430\u0432\u043e/\u0432\u043b\u0435\u0432\u043e \u043d\u0430 \u043e\u0434\u043d\u0443 \u043a\u043b\u0435\u0442\u043a\u0443; \u0446\u0438\u043a\u043b \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0430\u043c\u043e\u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0439 \u0438 \u0441\u0430\u043c\u043e\u043a\u0430\u0441\u0430\u043d\u0438\u0439, \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u043a\u043b\u0435\u0442\u043a\u0438 \u0446\u0438\u043a\u043b\u0430 \u0441\u043e\u0441\u0435\u0434\u0441\u0442\u0432\u0443\u044e\u0442 \u043f\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0442\u043e\u0433\u0434\u0430 \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0435 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0438 \u0432\u0434\u043e\u043b\u044c \u0446\u0438\u043a\u043b\u0430 (\u0441\u0430\u043c\u043e\u043a\u0430\u0441\u0430\u043d\u0438\u0435 \u043f\u043e \u0443\u0433\u043b\u0443 \u0442\u043e\u0436\u0435 \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u043e). \n\n\u041d\u0438\u0436\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u044b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432: [Image] \n\n\u0412\u0441\u0435 \u043a\u043b\u0435\u0442\u043a\u0438 \u043f\u043e\u043b\u044f, \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0435 \u043e\u0442 \u0446\u0438\u043a\u043b\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0441\u0438\u043c\u0432\u043e\u043b \u00ab.\u00bb. \u0426\u0438\u043a\u043b \u043d\u0430 \u043f\u043e\u043b\u0435 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d. \u041f\u043e\u0441\u0435\u0449\u0430\u0442\u044c \u043a\u043b\u0435\u0442\u043a\u0438, \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0435 \u043e\u0442 \u0446\u0438\u043a\u043b\u0430, \u0420\u043e\u0431\u043e\u0442\u0443 \u043d\u0435\u043b\u044c\u0437\u044f.\n\n\u0412 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043a\u043b\u0435\u0442\u043e\u043a \u0446\u0438\u043a\u043b\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0420\u043e\u0431\u043e\u0442. \u042d\u0442\u0430 \u043a\u043b\u0435\u0442\u043a\u0430 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c \u00abS\u00bb. \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434 \u0434\u043b\u044f \u0420\u043e\u0431\u043e\u0442\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438 \u0446\u0438\u043a\u043b. \u041a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u0447\u0435\u0442\u044b\u0440\u0451\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u0443\u043a\u0432\u043e\u0439 \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0420\u043e\u0431\u043e\u0442\u0430 \u043d\u0430 \u043e\u0434\u043d\u0443 \u043a\u043b\u0435\u0442\u043a\u0443: \u00abU\u00bb\u00a0\u2014 \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u043b\u0435\u0442\u043a\u0443 \u0432\u0432\u0435\u0440\u0445, \u00abR\u00bb\u00a0\u2014 \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u043b\u0435\u0442\u043a\u0443 \u0432\u043f\u0440\u0430\u0432\u043e, \u00abD\u00bb\u00a0\u2014 \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u043b\u0435\u0442\u043a\u0443 \u0432\u043d\u0438\u0437, \u00abL\u00bb\u00a0\u2014 \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u043b\u0435\u0442\u043a\u0443 \u0432\u043b\u0435\u0432\u043e. \n\n\u0420\u043e\u0431\u043e\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0431\u043e\u0439\u0442\u0438 \u0446\u0438\u043a\u043b, \u043f\u043e\u0431\u044b\u0432\u0430\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0435\u0433\u043e \u043a\u043b\u0435\u0442\u043a\u0435 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 (\u043a\u0440\u043e\u043c\u0435 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u0442\u043e\u0447\u043a\u0438\u00a0\u2014 \u0432 \u043d\u0435\u0439 \u043e\u043d \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442 \u0441\u0432\u043e\u0439 \u043f\u0443\u0442\u044c).\n\n\u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u0438\u0441\u043a\u043e\u043c\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434, \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043b\u044e\u0431\u043e\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u0445\u043e\u0434\u0430 \u0446\u0438\u043a\u043b\u0430.\n\n\n-----\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0434\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 n \u0438 m (3 \u2264 n, m \u2264 100) \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0435\u0442\u0447\u0430\u0442\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e.\n\n\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 n \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043f\u043e m \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u2014 \u00ab.\u00bb, \u00ab*\u00bb \u0438\u043b\u0438 \u00abS\u00bb. \u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0435 \u043e\u0442 \u00ab.\u00bb \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u0446\u0438\u043a\u043b \u0431\u0435\u0437 \u0441\u0430\u043c\u043e\u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0439 \u0438 \u0441\u0430\u043c\u043e\u043a\u0430\u0441\u0430\u043d\u0438\u0439. \u0422\u0430\u043a\u0436\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430 \u043f\u043e\u043b\u0435 \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u043d\u0430 \u043a\u043b\u0435\u0442\u043a\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u00abS\u00bb \u0438 \u0447\u0442\u043e \u043e\u043d\u0430 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 \u0446\u0438\u043a\u043b\u0443. \u0420\u043e\u0431\u043e\u0442 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0441\u0435\u0449\u0430\u0442\u044c \u043a\u043b\u0435\u0442\u043a\u0438, \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c \u00ab.\u00bb.\n\n\n-----\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0438\u0441\u043a\u043e\u043c\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434 \u0434\u043b\u044f \u0420\u043e\u0431\u043e\u0442\u0430. \u041d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u0445\u043e\u0434\u0430 \u0446\u0438\u043a\u043b\u0430 \u0420\u043e\u0431\u043e\u0442\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u044b\u043c.\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0440\u044b-----\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n3 3\n***\n*.*\n*S*\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nLUURRDDL\n\n\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n6 7\n.***...\n.*.*...\n.*.S**.\n.*...**\n.*....*\n.******\n\n\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nUULLDDDDDRRRRRUULULL\n\n\n\n-----\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435-----\n\n\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0434\u043b\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u043e \u0447\u0430\u0441\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0435\u043b\u043a\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0440\u043e\u0431\u043e\u0442\u043e\u043c \u043a\u043b\u0435\u0442\u043e\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c: \u043a\u043b\u0435\u0442\u043a\u0430 (3, 2); \u043a\u043b\u0435\u0442\u043a\u0430 (3, 1); \u043a\u043b\u0435\u0442\u043a\u0430 (2, 1); \u043a\u043b\u0435\u0442\u043a\u0430 (1, 1); \u043a\u043b\u0435\u0442\u043a\u0430 (1, 2); \u043a\u043b\u0435\u0442\u043a\u0430 (1, 3); \u043a\u043b\u0435\u0442\u043a\u0430 (2, 3); \u043a\u043b\u0435\u0442\u043a\u0430 (3, 3); \u043a\u043b\u0435\u0442\u043a\u0430 (3, 2)."} +{"id": "02323", "text": "Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.\n\nIn return, the fleas made a bigger ukulele for her: it has $n$ strings, and each string has $(10^{18} + 1)$ frets numerated from $0$ to $10^{18}$. The fleas use the array $s_1, s_2, \\ldots, s_n$ to describe the ukulele's tuning, that is, the pitch of the $j$-th fret on the $i$-th string is the integer $s_i + j$.\n\nMiyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.\n\nEach question is in the form of: \"How many different pitches are there, if we consider frets between $l$ and $r$ (inclusive) on all strings?\"\n\nMiyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!\n\nFormally, you are given a matrix with $n$ rows and $(10^{18}+1)$ columns, where the cell in the $i$-th row and $j$-th column ($0 \\le j \\le 10^{18}$) contains the integer $s_i + j$. You are to answer $q$ queries, in the $k$-th query you have to answer the number of distinct integers in the matrix from the $l_k$-th to the $r_k$-th columns, inclusive.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\leq n \\leq 100\\,000$)\u00a0\u2014 the number of strings.\n\nThe second line contains $n$ integers $s_1, s_2, \\ldots, s_n$ ($0 \\leq s_i \\leq 10^{18}$)\u00a0\u2014 the tuning of the ukulele.\n\nThe third line contains an integer $q$ ($1 \\leq q \\leq 100\\,000$)\u00a0\u2014 the number of questions.\n\nThe $k$-th among the following $q$ lines contains two integers $l_k$\uff0c$r_k$ ($0 \\leq l_k \\leq r_k \\leq 10^{18}$)\u00a0\u2014 a question from the fleas.\n\n\n-----Output-----\n\nOutput one number for each question, separated by spaces\u00a0\u2014 the number of different pitches.\n\n\n-----Examples-----\nInput\n6\n3 1 4 1 5 9\n3\n7 7\n0 2\n8 17\n\nOutput\n5 10 18\n\nInput\n2\n1 500000000000000000\n2\n1000000000000000000 1000000000000000000\n0 1000000000000000000\n\nOutput\n2 1500000000000000000\n\n\n\n-----Note-----\n\nFor the first example, the pitches on the $6$ strings are as follows.\n\n$$ \\begin{matrix} \\textbf{Fret} & \\textbf{0} & \\textbf{1} & \\textbf{2} & \\textbf{3} & \\textbf{4} & \\textbf{5} & \\textbf{6} & \\textbf{7} & \\ldots \\\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & \\dots \\\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \\dots \\\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & \\dots \\\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \\dots \\\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & \\dots \\\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & \\dots \\end{matrix} $$\n\nThere are $5$ different pitches on fret $7$\u00a0\u2014 $8, 10, 11, 12, 16$.\n\nThere are $10$ different pitches on frets $0, 1, 2$\u00a0\u2014 $1, 2, 3, 4, 5, 6, 7, 9, 10, 11$."} +{"id": "02324", "text": "Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.\n\nA string is 1-palindrome if and only if it reads the same backward as forward.\n\nA string is k-palindrome (k > 1) if and only if: Its left half equals to its right half. Its left and right halfs are non-empty (k - 1)-palindromes. \n\nThe left half of string t is its prefix of length \u230a|t| / 2\u230b, and right half\u00a0\u2014 the suffix of the same length. \u230a|t| / 2\u230b denotes the length of string t divided by 2, rounded down.\n\nNote that each substring is counted as many times as it appears in the string. For example, in the string \"aaa\" the substring \"a\" appears 3 times.\n\n\n-----Input-----\n\nThe first line contains the string s (1 \u2264 |s| \u2264 5000) consisting of lowercase English letters.\n\n\n-----Output-----\n\nPrint |s| integers\u00a0\u2014 palindromic characteristics of string s.\n\n\n-----Examples-----\nInput\nabba\n\nOutput\n6 1 0 0 \n\nInput\nabacaba\n\nOutput\n12 4 1 0 0 0 0 \n\n\n\n-----Note-----\n\nIn the first example 1-palindromes are substring \u00aba\u00bb, \u00abb\u00bb, \u00abb\u00bb, \u00aba\u00bb, \u00abbb\u00bb, \u00ababba\u00bb, the substring \u00abbb\u00bb is 2-palindrome. There are no 3- and 4-palindromes here."} +{"id": "02325", "text": "You are given two positive integer numbers x and y. An array F is called an y-factorization of x iff the following conditions are met: There are y elements in F, and all of them are integer numbers; $\\prod_{i = 1}^{y} F_{i} = x$. \n\nYou have to count the number of pairwise distinct arrays that are y-factorizations of x. Two arrays A and B are considered different iff there exists at least one index i (1 \u2264 i \u2264 y) such that A_{i} \u2260 B_{i}. Since the answer can be very large, print it modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line contains one integer q (1 \u2264 q \u2264 10^5) \u2014 the number of testcases to solve.\n\nThen q lines follow, each containing two integers x_{i} and y_{i} (1 \u2264 x_{i}, y_{i} \u2264 10^6). Each of these lines represents a testcase.\n\n\n-----Output-----\n\nPrint q integers. i-th integer has to be equal to the number of y_{i}-factorizations of x_{i} modulo 10^9 + 7.\n\n\n-----Example-----\nInput\n2\n6 3\n4 2\n\nOutput\n36\n6\n\n\n\n-----Note-----\n\nIn the second testcase of the example there are six y-factorizations: { - 4, - 1}; { - 2, - 2}; { - 1, - 4}; {1, 4}; {2, 2}; {4, 1}."} +{"id": "02326", "text": "The sequence of integers $a_1, a_2, \\dots, a_k$ is called a good array if $a_1 = k - 1$ and $a_1 > 0$. For example, the sequences $[3, -1, 44, 0], [1, -99]$ are good arrays, and the sequences $[3, 7, 8], [2, 5, 4, 1], [0]$ \u2014 are not.\n\nA sequence of integers is called good if it can be divided into a positive number of good arrays. Each good array should be a subsegment of sequence and each element of the sequence should belong to exactly one array. For example, the sequences $[2, -3, 0, 1, 4]$, $[1, 2, 3, -3, -9, 4]$ are good, and the sequences $[2, -3, 0, 1]$, $[1, 2, 3, -3 -9, 4, 1]$ \u2014 are not.\n\nFor a given sequence of numbers, count the number of its subsequences that are good sequences, and print the number of such subsequences modulo 998244353.\n\n\n-----Input-----\n\nThe first line contains the number $n~(1 \\le n \\le 10^3)$ \u2014 the length of the initial sequence. The following line contains $n$ integers $a_1, a_2, \\dots, a_n~(-10^9 \\le a_i \\le 10^9)$ \u2014 the sequence itself.\n\n\n-----Output-----\n\nIn the single line output one integer \u2014 the number of subsequences of the original sequence that are good sequences, taken modulo 998244353.\n\n\n-----Examples-----\nInput\n3\n2 1 1\n\nOutput\n2\n\nInput\n4\n1 1 1 1\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first test case, two good subsequences \u2014 $[a_1, a_2, a_3]$ and $[a_2, a_3]$.\n\nIn the second test case, seven good subsequences \u2014 $[a_1, a_2, a_3, a_4], [a_1, a_2], [a_1, a_3], [a_1, a_4], [a_2, a_3], [a_2, a_4]$ and $[a_3, a_4]$."} +{"id": "02327", "text": "The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the presented tasks are lovely, but don't show the truth about competitors' skills.\n\nThe boy is now looking at the ratings of consecutive participants written in a binary system. He thinks that the more such ratings differ, the more unfair is that such people are next to each other. He defines the difference between two numbers as the number of bit positions, where one number has zero, and another has one (we suppose that numbers are padded with leading zeros to the same length). For example, the difference of $5 = 101_2$ and $14 = 1110_2$ equals to $3$, since $0101$ and $1110$ differ in $3$ positions. Johnny defines the unfairness of the contest as the sum of such differences counted for neighboring participants.\n\nJohnny has just sent you the rating sequence and wants you to find the unfairness of the competition. You have noticed that you've got a sequence of consecutive integers from $0$ to $n$. That's strange, but the boy stubbornly says that everything is right. So help him and find the desired unfairness for received numbers.\n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains one integer $t$ ($1 \\leq t \\leq 10\\,000$)\u00a0\u2014 the number of test cases. The following $t$ lines contain a description of test cases.\n\nThe first and only line in each test case contains a single integer $n$ ($1 \\leq n \\leq 10^{18})$.\n\n\n-----Output-----\n\nOutput $t$ lines. For each test case, you should output a single line with one integer\u00a0\u2014 the unfairness of the contest if the rating sequence equals to $0$, $1$, ..., $n - 1$, $n$.\n\n\n-----Example-----\nInput\n5\n5\n7\n11\n1\n2000000000000\n\nOutput\n8\n11\n19\n1\n3999999999987\n\n\n\n-----Note-----\n\nFor $n = 5$ we calculate unfairness of the following sequence (numbers from $0$ to $5$ written in binary with extra leading zeroes, so they all have the same length): $000$ $001$ $010$ $011$ $100$ $101$ \n\nThe differences are equal to $1$, $2$, $1$, $3$, $1$ respectively, so unfairness is equal to $1 + 2 + 1 + 3 + 1 = 8$."} +{"id": "02328", "text": "At first, there was a legend related to the name of the problem, but now it's just a formal statement.\n\nYou are given $n$ points $a_1, a_2, \\dots, a_n$ on the $OX$ axis. Now you are asked to find such an integer point $x$ on $OX$ axis that $f_k(x)$ is minimal possible.\n\nThe function $f_k(x)$ can be described in the following way: form a list of distances $d_1, d_2, \\dots, d_n$ where $d_i = |a_i - x|$ (distance between $a_i$ and $x$); sort list $d$ in non-descending order; take $d_{k + 1}$ as a result. \n\nIf there are multiple optimal answers you can print any of them.\n\n\n-----Input-----\n\nThe first line contains single integer $T$ ($ 1 \\le T \\le 2 \\cdot 10^5$) \u2014 number of queries. Next $2 \\cdot T$ lines contain descriptions of queries. All queries are independent. \n\nThe first line of each query contains two integers $n$, $k$ ($1 \\le n \\le 2 \\cdot 10^5$, $0 \\le k < n$) \u2014 the number of points and constant $k$.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_1 < a_2 < \\dots < a_n \\le 10^9$) \u2014 points in ascending order.\n\nIt's guaranteed that $\\sum{n}$ doesn't exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nPrint $T$ integers \u2014 corresponding points $x$ which have minimal possible value of $f_k(x)$. If there are multiple answers you can print any of them.\n\n\n-----Example-----\nInput\n3\n3 2\n1 2 5\n2 1\n1 1000000000\n1 0\n4\n\nOutput\n3\n500000000\n4"} +{"id": "02329", "text": "You have a set of $n$ discs, the $i$-th disc has radius $i$. Initially, these discs are split among $m$ towers: each tower contains at least one disc, and the discs in each tower are sorted in descending order of their radii from bottom to top.\n\nYou would like to assemble one tower containing all of those discs. To do so, you may choose two different towers $i$ and $j$ (each containing at least one disc), take several (possibly all) top discs from the tower $i$ and put them on top of the tower $j$ in the same order, as long as the top disc of tower $j$ is bigger than each of the discs you move. You may perform this operation any number of times.\n\nFor example, if you have two towers containing discs $[6, 4, 2, 1]$ and $[8, 7, 5, 3]$ (in order from bottom to top), there are only two possible operations:\n\n move disc $1$ from the first tower to the second tower, so the towers are $[6, 4, 2]$ and $[8, 7, 5, 3, 1]$; move discs $[2, 1]$ from the first tower to the second tower, so the towers are $[6, 4]$ and $[8, 7, 5, 3, 2, 1]$. \n\nLet the difficulty of some set of towers be the minimum number of operations required to assemble one tower containing all of the discs. For example, the difficulty of the set of towers $[[3, 1], [2]]$ is $2$: you may move the disc $1$ to the second tower, and then move both discs from the second tower to the first tower.\n\nYou are given $m - 1$ queries. Each query is denoted by two numbers $a_i$ and $b_i$, and means \"merge the towers $a_i$ and $b_i$\" (that is, take all discs from these two towers and assemble a new tower containing all of them in descending order of their radii from top to bottom). The resulting tower gets index $a_i$.\n\nFor each $k \\in [0, m - 1]$, calculate the difficulty of the set of towers after the first $k$ queries are performed.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($2 \\le m \\le n \\le 2 \\cdot 10^5$) \u2014 the number of discs and the number of towers, respectively.\n\nThe second line contains $n$ integers $t_1$, $t_2$, ..., $t_n$ ($1 \\le t_i \\le m$), where $t_i$ is the index of the tower disc $i$ belongs to. Each value from $1$ to $m$ appears in this sequence at least once.\n\nThen $m - 1$ lines follow, denoting the queries. Each query is represented by two integers $a_i$ and $b_i$ ($1 \\le a_i, b_i \\le m$, $a_i \\ne b_i$), meaning that, during the $i$-th query, the towers with indices $a_i$ and $b_i$ are merged ($a_i$ and $b_i$ are chosen in such a way that these towers exist before the $i$-th query).\n\n\n-----Output-----\n\nPrint $m$ integers. The $k$-th integer ($0$-indexed) should be equal to the difficulty of the set of towers after the first $k$ queries are performed.\n\n\n-----Example-----\nInput\n7 4\n1 2 3 3 1 4 3\n3 1\n2 3\n2 4\n\nOutput\n5\n4\n2\n0\n\n\n\n-----Note-----\n\nThe towers in the example are:\n\n before the queries: $[[5, 1], [2], [7, 4, 3], [6]]$; after the first query: $[[2], [7, 5, 4, 3, 1], [6]]$; after the second query: $[[7, 5, 4, 3, 2, 1], [6]]$; after the third query, there is only one tower: $[7, 6, 5, 4, 3, 2, 1]$."} +{"id": "02330", "text": "Hanh lives in a shared apartment. There are $n$ people (including Hanh) living there, each has a private fridge. \n\n$n$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $n$ people can open it.\n\n [Image] For exampe, in the picture there are $n=4$ people and $5$ chains. The first person knows passcodes of two chains: $1-4$ and $1-2$. The fridge $1$ can be open by its owner (the person $1$), also two people $2$ and $4$ (acting together) can open it. \n\nThe weights of these fridges are $a_1, a_2, \\ldots, a_n$. To make a steel chain connecting fridges $u$ and $v$, you have to pay $a_u + a_v$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. \n\nHanh's apartment landlord asks you to create exactly $m$ steel chains so that all fridges are private. A fridge is private if and only if, among $n$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $i$ is not private if there exists the person $j$ ($i \\ne j$) that the person $j$ can open the fridge $i$.\n\nFor example, in the picture all the fridges are private. On the other hand, if there are $n=2$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).\n\nOf course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $m$ chains, and if yes, output any solution that minimizes the total cost. \n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \\le T \\le 10$). Then the descriptions of the test cases follow.\n\nThe first line of each test case contains two integers $n$, $m$ ($2 \\le n \\le 1000$, $1 \\le m \\le n$)\u00a0\u2014 the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 10^4$)\u00a0\u2014 weights of all fridges.\n\n\n-----Output-----\n\nFor each test case:\n\n If there is no solution, print a single integer $-1$. Otherwise, print a single integer $c$\u00a0\u2014 the minimum total cost. The $i$-th of the next $m$ lines contains two integers $u_i$ and $v_i$ ($1 \\le u_i, v_i \\le n$, $u_i \\ne v_i$), meaning that the $i$-th steel chain connects fridges $u_i$ and $v_i$. An arbitrary number of chains can be between a pair of fridges. \n\nIf there are multiple answers, print any.\n\n\n-----Example-----\nInput\n3\n4 4\n1 1 1 1\n3 1\n1 2 3\n3 3\n1 2 3\n\nOutput\n8\n1 2\n4 3\n3 2\n4 1\n-1\n12\n3 2\n1 2\n3 1"} +{"id": "02331", "text": "Consider the set of all nonnegative integers: ${0, 1, 2, \\dots}$. Given two integers $a$ and $b$ ($1 \\le a, b \\le 10^4$). We paint all the numbers in increasing number first we paint $0$, then we paint $1$, then $2$ and so on.\n\nEach number is painted white or black. We paint a number $i$ according to the following rules: if $i = 0$, it is colored white; if $i \\ge a$ and $i - a$ is colored white, $i$ is also colored white; if $i \\ge b$ and $i - b$ is colored white, $i$ is also colored white; if $i$ is still not colored white, it is colored black. \n\nIn this way, each nonnegative integer gets one of two colors.\n\nFor example, if $a=3$, $b=5$, then the colors of the numbers (in the order from $0$) are: white ($0$), black ($1$), black ($2$), white ($3$), black ($4$), white ($5$), white ($6$), black ($7$), white ($8$), white ($9$), ...\n\nNote that: It is possible that there are infinitely many nonnegative integers colored black. For example, if $a = 10$ and $b = 10$, then only $0, 10, 20, 30$ and any other nonnegative integers that end in $0$ when written in base 10 are white. The other integers are colored black. It is also possible that there are only finitely many nonnegative integers colored black. For example, when $a = 1$ and $b = 10$, then there is no nonnegative integer colored black at all. \n\nYour task is to determine whether or not the number of nonnegative integers colored black is infinite.\n\nIf there are infinitely many nonnegative integers colored black, simply print a line containing \"Infinite\" (without the quotes). Otherwise, print \"Finite\" (without the quotes).\n\n\n-----Input-----\n\nThe first line of input contains a single integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases in the input. Then $t$ lines follow, each line contains two space-separated integers $a$ and $b$ ($1 \\le a, b \\le 10^4$).\n\n\n-----Output-----\n\nFor each test case, print one line containing either \"Infinite\" or \"Finite\" (without the quotes). Output is case-insensitive (i.e. \"infinite\", \"inFiNite\" or \"finiTE\" are all valid answers).\n\n\n-----Example-----\nInput\n4\n10 10\n1 10\n6 9\n7 3\n\nOutput\nInfinite\nFinite\nInfinite\nFinite"} +{"id": "02332", "text": "Mahmoud wants to send a message to his friend Ehab. Their language consists of n words numbered from 1 to n. Some words have the same meaning so there are k groups of words such that all the words in some group have the same meaning.\n\nMahmoud knows that the i-th word can be sent with cost a_{i}. For each word in his message, Mahmoud can either replace it with another word of the same meaning or leave it as it is. Can you help Mahmoud determine the minimum cost of sending the message?\n\nThe cost of sending the message is the sum of the costs of sending every word in it.\n\n\n-----Input-----\n\nThe first line of input contains integers n, k and m (1 \u2264 k \u2264 n \u2264 10^5, 1 \u2264 m \u2264 10^5)\u00a0\u2014 the number of words in their language, the number of groups of words, and the number of words in Mahmoud's message respectively.\n\nThe second line contains n strings consisting of lowercase English letters of length not exceeding 20 which represent the words. It's guaranteed that the words are distinct.\n\nThe third line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) where a_{i} is the cost of sending the i-th word.\n\nThe next k lines describe the groups of words of same meaning. The next k lines each start with an integer x (1 \u2264 x \u2264 n) which means that there are x words in this group, followed by x integers which represent the indices of words in this group. It's guaranteed that each word appears in exactly one group.\n\nThe next line contains m space-separated words which represent Mahmoud's message. Each of these words appears in the list of language's words.\n\n\n-----Output-----\n\nThe only line should contain the minimum cost to send the message after replacing some words (maybe none) with some words of the same meaning.\n\n\n-----Examples-----\nInput\n5 4 4\ni loser am the second\n100 1 1 5 10\n1 1\n1 3\n2 2 5\n1 4\ni am the second\n\nOutput\n107\nInput\n5 4 4\ni loser am the second\n100 20 1 5 10\n1 1\n1 3\n2 2 5\n1 4\ni am the second\n\nOutput\n116\n\n\n-----Note-----\n\nIn the first sample, Mahmoud should replace the word \"second\" with the word \"loser\" because it has less cost so the cost will be 100+1+5+1=107.\n\nIn the second sample, Mahmoud shouldn't do any replacement so the cost will be 100+1+5+10=116."} +{"id": "02333", "text": "You have an array $a_1, a_2, \\dots, a_n$. \n\nLet's call some subarray $a_l, a_{l + 1}, \\dots , a_r$ of this array a subpermutation if it contains all integers from $1$ to $r-l+1$ exactly once. For example, array $a = [2, 2, 1, 3, 2, 3, 1]$ contains $6$ subarrays which are subpermutations: $[a_2 \\dots a_3]$, $[a_2 \\dots a_4]$, $[a_3 \\dots a_3]$, $[a_3 \\dots a_5]$, $[a_5 \\dots a_7]$, $[a_7 \\dots a_7]$.\n\nYou are asked to calculate the number of subpermutations.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 3 \\cdot 10^5$).\n\nThe second line contains $n$ integers $a_1, a_2, \\dots , a_n$ ($1 \\le a_i \\le n$). \n\nThis array can contain the same integers.\n\n\n-----Output-----\n\nPrint the number of subpermutations of the array $a$.\n\n\n-----Examples-----\nInput\n8\n2 4 1 3 4 2 1 2\n\nOutput\n7\n\nInput\n5\n1 1 2 1 2\n\nOutput\n6\n\n\n\n-----Note-----\n\nThere are $7$ subpermutations in the first test case. Their segments of indices are $[1, 4]$, $[3, 3]$, $[3, 6]$, $[4, 7]$, $[6, 7]$, $[7, 7]$ and $[7, 8]$.\n\nIn the second test case $6$ subpermutations exist: $[1, 1]$, $[2, 2]$, $[2, 3]$, $[3, 4]$, $[4, 4]$ and $[4, 5]$."} +{"id": "02334", "text": "After finding and moving to the new planet that supports human life, discussions started on which currency should be used. After long negotiations, Bitcoin was ultimately chosen as the universal currency.\n\nThese were the great news for Alice, whose grandfather got into Bitcoin mining in 2013, and accumulated a lot of them throughout the years. Unfortunately, when paying something in bitcoin everyone can see how many bitcoins you have in your public address wallet. \n\nThis worried Alice, so she decided to split her bitcoins among multiple different addresses, so that every address has at most $x$ satoshi (1 bitcoin = $10^8$ satoshi). She can create new public address wallets for free and is willing to pay $f$ fee in satoshi per transaction to ensure acceptable speed of transfer. The fee is deducted from the address the transaction was sent from. Tell Alice how much total fee in satoshi she will need to pay to achieve her goal.\n\n\n-----Input-----\n\nFirst line contains number $N$ ($1 \\leq N \\leq 200\\,000$) representing total number of public addresses Alice has.\n\nNext line contains $N$ integer numbers $a_i$ ($1 \\leq a_i \\leq 10^9$) separated by a single space, representing how many satoshi Alice has in her public addresses.\n\nLast line contains two numbers $x$, $f$ ($1 \\leq f < x \\leq 10^9$) representing maximum number of satoshies Alice can have in one address, as well as fee in satoshies she is willing to pay per transaction. \n\n\n-----Output-----\n\nOutput one integer number representing total fee in satoshi Alice will need to pay to achieve her goal.\n\n\n-----Example-----\nInput\n3\n13 7 6\n6 2\n\nOutput\n4\n\n\n\n-----Note-----\n\nAlice can make two transactions in a following way:\n\n0. 13 7 6 (initial state)\n\n1. 6 7 6 5 (create new address and transfer from first public address 5 satoshies)\n\n2. 6 4 6 5 1 (create new address and transfer from second address 1 satoshi)\n\nSince cost per transaction is 2 satoshies, total fee is 4."} +{"id": "02335", "text": "Roy and Biv have a set of n points on the infinite number line.\n\nEach point has one of 3 colors: red, green, or blue.\n\nRoy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects.\n\nThey want to do this in such a way that they will both see that all the points are connected (either directly or indirectly).\n\nHowever, there is a catch: Roy cannot see the color red and Biv cannot see the color blue.\n\nTherefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected).\n\nHelp them compute the minimum cost way to choose edges to satisfy the above constraints.\n\n\n-----Input-----\n\nThe first line will contain an integer n (1 \u2264 n \u2264 300 000), the number of points.\n\nThe next n lines will contain two tokens p_{i} and c_{i} (p_{i} is an integer, 1 \u2264 p_{i} \u2264 10^9, c_{i} is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the i-th point and the color of the i-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.\n\n\n-----Output-----\n\nPrint a single integer, the minimum cost way to solve the problem.\n\n\n-----Examples-----\nInput\n4\n1 G\n5 R\n10 B\n15 G\n\nOutput\n23\n\nInput\n4\n1 G\n2 R\n3 B\n10 G\n\nOutput\n12\n\n\n\n-----Note-----\n\nIn the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively."} +{"id": "02336", "text": "To stay woke and attentive during classes, Karen needs some coffee! [Image] \n\nKaren, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed \"The Art of the Covfefe\".\n\nShe knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between l_{i} and r_{i} degrees, inclusive, to achieve the optimal taste.\n\nKaren thinks that a temperature is admissible if at least k recipes recommend it.\n\nKaren has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range?\n\n\n-----Input-----\n\nThe first line of input contains three integers, n, k (1 \u2264 k \u2264 n \u2264 200000), and q (1 \u2264 q \u2264 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively.\n\nThe next n lines describe the recipes. Specifically, the i-th line among these contains two integers l_{i} and r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 200000), describing that the i-th recipe suggests that the coffee be brewed between l_{i} and r_{i} degrees, inclusive.\n\nThe next q lines describe the questions. Each of these lines contains a and b, (1 \u2264 a \u2264 b \u2264 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive.\n\n\n-----Output-----\n\nFor each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.\n\n\n-----Examples-----\nInput\n3 2 4\n91 94\n92 97\n97 99\n92 94\n93 97\n95 96\n90 100\n\nOutput\n3\n3\n0\n4\n\nInput\n2 1 1\n1 1\n200000 200000\n90 100\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test case, Karen knows 3 recipes. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. \n\nA temperature is admissible if at least 2 recipes recommend it.\n\nShe asks 4 questions.\n\nIn her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible.\n\nIn her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible.\n\nIn her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none.\n\nIn her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible.\n\nIn the second test case, Karen knows 2 recipes. The first one, \"wikiHow to make Cold Brew Coffee\", recommends brewing the coffee at exactly 1 degree. The second one, \"What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?\", recommends brewing the coffee at exactly 200000 degrees. \n\nA temperature is admissible if at least 1 recipe recommends it.\n\nIn her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none."} +{"id": "02337", "text": "George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer b_{i}.\n\nTo make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a_1, at least one with complexity exactly a_2, ..., and at least one with complexity exactly a_{n}. Of course, the round can also have problems with other complexities.\n\nGeorge has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c \u2265 d), by changing limits on the input data.\n\nHowever, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 3000) \u2014 the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_1 < a_2 < ... < a_{n} \u2264 10^6) \u2014 the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b_1, b_2, ..., b_{m} (1 \u2264 b_1 \u2264 b_2... \u2264 b_{m} \u2264 10^6) \u2014 the complexities of the problems prepared by George. \n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem.\n\n\n-----Examples-----\nInput\n3 5\n1 2 3\n1 2 2 3 3\n\nOutput\n0\n\nInput\n3 5\n1 2 3\n1 1 1 1 1\n\nOutput\n2\n\nInput\n3 1\n2 3 4\n1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample the set of the prepared problems meets the requirements for a good round.\n\nIn the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.\n\nIn the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4."} +{"id": "02338", "text": "You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (x_{i}, y_{i}). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: Operation has format \"1 k dir\". To perform the operation robot have to move in direction dir k (k \u2265 1) times. There are only 4 directions the robot can move in: \"R\", \"L\", \"U\", \"D\". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. Operation has format \"2\". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. Operation has format \"3\". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. \n\nHelp the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5) \u2014 the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (x_{i}, y_{i}) ( - 10^9 \u2264 x_{i}, y_{i} \u2264 10^9) \u2014 the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). \n\n\n-----Output-----\n\nIn a single line print a single integer k \u2014 the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k \u2264 10^6.\n\n\n-----Examples-----\nInput\n2\n1 1\n-1 -1\n\nOutput\n12\n1 1 R\n1 1 U\n2\n1 1 L\n1 1 D\n3\n1 1 L\n1 1 D\n2\n1 1 R\n1 1 U\n3\n\nInput\n3\n5 0\n0 5\n1 0\n\nOutput\n12\n1 1 R\n2\n1 1 L\n3\n1 5 R\n2\n1 5 L\n3\n1 5 U\n2\n1 5 D\n3"} +{"id": "02339", "text": "Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace \"Zmey-Gorynych\", and offers Alex a job: if he shows his programming skills by solving a task, he'll work as a cybersecurity specialist. Otherwise, he'll be delivering some doubtful products for the next two years.\n\nYou're given $n$ positive integers $a_1, a_2, \\dots, a_n$. Using each of them exactly at once, you're to make such sequence $b_1, b_2, \\dots, b_n$ that sequence $c_1, c_2, \\dots, c_n$ is lexicographically maximal, where $c_i=GCD(b_1,\\dots,b_i)$ - the greatest common divisor of the first $i$ elements of $b$. \n\nAlexander is really afraid of the conditions of this simple task, so he asks you to solve it.\n\nA sequence $a$ is lexicographically smaller than a sequence $b$ if and only if one of the following holds:\n\n\n\n $a$ is a prefix of $b$, but $a \\ne b$;\n\n in the first position where $a$ and $b$ differ, the sequence $a$ has a smaller element than the corresponding element in $b$.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^3$). Description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 10^3$) \u00a0\u2014 the length of the sequence $a$.\n\nThe second line of each test case contains $n$ integers $a_1,\\dots,a_n$ ($1 \\le a_i \\le 10^3$) \u00a0\u2014 the sequence $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.\n\n\n-----Output-----\n\nFor each test case output the answer in a single line \u00a0\u2014 the desired sequence $b$. If there are multiple answers, print any.\n\n\n-----Example-----\nInput\n7\n2\n2 5\n4\n1 8 2 3\n3\n3 8 9\n5\n64 25 75 100 50\n1\n42\n6\n96 128 88 80 52 7\n5\n2 4 8 16 17\n\nOutput\n5 2 \n8 2 1 3 \n9 3 8 \n100 50 25 75 64 \n42 \n128 96 80 88 52 7 \n17 2 4 8 16 \n\n\n\n-----Note-----\n\nIn the first test case of the example, there are only two possible permutations $b$ \u00a0\u2014 $[2, 5]$ and $[5, 2]$: for the first one $c=[2, 1]$, for the second one $c=[5, 1]$.\n\nIn the third test case of the example, number $9$ should be the first in $b$, and $GCD(9, 3)=3$, $GCD(9, 8)=1$, so the second number of $b$ should be $3$.\n\nIn the seventh test case of the example, first four numbers pairwise have a common divisor (a power of two), but none of them can be the first in the optimal permutation $b$."} +{"id": "02340", "text": "You are playing a game where your character should overcome different obstacles. The current problem is to come down from a cliff. The cliff has height $h$, and there is a moving platform on each height $x$ from $1$ to $h$.\n\nEach platform is either hidden inside the cliff or moved out. At first, there are $n$ moved out platforms on heights $p_1, p_2, \\dots, p_n$. The platform on height $h$ is moved out (and the character is initially standing there).\n\nIf you character is standing on some moved out platform on height $x$, then he can pull a special lever, which switches the state of two platforms: on height $x$ and $x - 1$. In other words, the platform you are currently standing on will hide in the cliff and the platform one unit below will change it state: it will hide if it was moved out or move out if it was hidden. In the second case, you will safely land on it. Note that this is the only way to move from one platform to another.\n\nYour character is quite fragile, so it can safely fall from the height no more than $2$. In other words falling from the platform $x$ to platform $x - 2$ is okay, but falling from $x$ to $x - 3$ (or lower) is certain death. \n\nSometimes it's not possible to come down from the cliff, but you can always buy (for donate currency) several magic crystals. Each magic crystal can be used to change the state of any single platform (except platform on height $h$, which is unaffected by the crystals). After being used, the crystal disappears.\n\nWhat is the minimum number of magic crystal you need to buy to safely land on the $0$ ground level?\n\n\n-----Input-----\n\nThe first line contains one integer $q$ ($1 \\le q \\le 100$) \u2014 the number of queries. Each query contains two lines and is independent of all other queries.\n\nThe first line of each query contains two integers $h$ and $n$ ($1 \\le h \\le 10^9$, $1 \\le n \\le \\min(h, 2 \\cdot 10^5)$) \u2014 the height of the cliff and the number of moved out platforms.\n\nThe second line contains $n$ integers $p_1, p_2, \\dots, p_n$ ($h = p_1 > p_2 > \\dots > p_n \\ge 1$) \u2014 the corresponding moved out platforms in the descending order of their heights.\n\nThe sum of $n$ over all queries does not exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each query print one integer \u2014 the minimum number of magic crystals you have to spend to safely come down on the ground level (with height $0$).\n\n\n-----Example-----\nInput\n4\n3 2\n3 1\n8 6\n8 7 6 5 3 2\n9 6\n9 8 5 4 3 1\n1 1\n1\n\nOutput\n0\n1\n2\n0"} +{"id": "02341", "text": "Carousel Boutique is busy again! Rarity has decided to visit the pony ball and she surely needs a new dress, because going out in the same dress several times is a sign of bad manners. First of all, she needs a dress pattern, which she is going to cut out from the rectangular piece of the multicolored fabric.\n\nThe piece of the multicolored fabric consists of $n \\times m$ separate square scraps. Since Rarity likes dresses in style, a dress pattern must only include scraps sharing the same color. A dress pattern must be the square, and since Rarity is fond of rhombuses, the sides of a pattern must form a $45^{\\circ}$ angle with sides of a piece of fabric (that way it will be resembling the traditional picture of a rhombus).\n\nExamples of proper dress patterns: [Image] Examples of improper dress patterns: [Image] The first one consists of multi-colored scraps, the second one goes beyond the bounds of the piece of fabric, the third one is not a square with sides forming a $45^{\\circ}$ angle with sides of the piece of fabric.\n\nRarity wonders how many ways to cut out a dress pattern that satisfies all the conditions that do exist. Please help her and satisfy her curiosity so she can continue working on her new masterpiece!\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 2000$). Each of the next $n$ lines contains $m$ characters: lowercase English letters, the $j$-th of which corresponds to scrap in the current line and in the $j$-th column. Scraps having the same letter share the same color, scraps having different letters have different colors.\n\n\n-----Output-----\n\nPrint a single integer: the number of ways to cut out a dress pattern to satisfy all of Rarity's conditions.\n\n\n-----Examples-----\nInput\n3 3\naaa\naaa\naaa\n\nOutput\n10\n\nInput\n3 4\nabab\nbaba\nabab\n\nOutput\n12\n\nInput\n5 5\nzbacg\nbaaac\naaaaa\neaaad\nweadd\n\nOutput\n31\n\n\n\n-----Note-----\n\nIn the first example, all the dress patterns of size $1$ and one of size $2$ are satisfactory.\n\nIn the second example, only the dress patterns of size $1$ are satisfactory."} +{"id": "02342", "text": "Vivek has encountered a problem. He has a maze that can be represented as an $n \\times m$ grid. Each of the grid cells may represent the following: Empty\u00a0\u2014 '.' Wall\u00a0\u2014 '#' Good person \u00a0\u2014 'G' Bad person\u00a0\u2014 'B' \n\nThe only escape from the maze is at cell $(n, m)$.\n\nA person can move to a cell only if it shares a side with their current cell and does not contain a wall. Vivek wants to block some of the empty cells by replacing them with walls in such a way, that all the good people are able to escape, while none of the bad people are able to. A cell that initially contains 'G' or 'B' cannot be blocked and can be travelled through.\n\nHelp him determine if there exists a way to replace some (zero or more) empty cells with walls to satisfy the above conditions.\n\nIt is guaranteed that the cell $(n,m)$ is empty. Vivek can also block this cell.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ $(1 \\le t \\le 100)$\u00a0\u2014 the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$, $m$ $(1 \\le n, m \\le 50)$\u00a0\u2014 the number of rows and columns in the maze.\n\nEach of the next $n$ lines contain $m$ characters. They describe the layout of the maze. If a character on a line equals '.', the corresponding cell is empty. If it equals '#', the cell has a wall. 'G' corresponds to a good person and 'B' corresponds to a bad person.\n\n\n-----Output-----\n\nFor each test case, print \"Yes\" if there exists a way to replace some empty cells with walls to satisfy the given conditions. Otherwise print \"No\"\n\nYou may print every letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n6\n1 1\n.\n1 2\nG.\n2 2\n#B\nG.\n2 3\nG.#\nB#.\n3 3\n#B.\n#..\nGG.\n2 2\n#B\nB.\n\nOutput\nYes\nYes\nNo\nNo\nYes\nYes\n\n\n\n-----Note-----\n\nFor the first and second test cases, all conditions are already satisfied.\n\nFor the third test case, there is only one empty cell $(2,2)$, and if it is replaced with a wall then the good person at $(1,2)$ will not be able to escape.\n\nFor the fourth test case, the good person at $(1,1)$ cannot escape.\n\nFor the fifth test case, Vivek can block the cells $(2,3)$ and $(2,2)$.\n\nFor the last test case, Vivek can block the destination cell $(2, 2)$."} +{"id": "02343", "text": "Recently, Olya received a magical square with the size of $2^n\\times 2^n$.\n\nIt seems to her sister that one square is boring. Therefore, she asked Olya to perform exactly $k$ splitting operations.\n\nA Splitting operation is an operation during which Olya takes a square with side $a$ and cuts it into 4 equal squares with side $\\dfrac{a}{2}$. If the side of the square is equal to $1$, then it is impossible to apply a splitting operation to it (see examples for better understanding).\n\nOlya is happy to fulfill her sister's request, but she also wants the condition of Olya's happiness to be satisfied after all operations.\n\nThe condition of Olya's happiness will be satisfied if the following statement is fulfilled:\n\nLet the length of the side of the lower left square be equal to $a$, then the length of the side of the right upper square should also be equal to $a$. There should also be a path between them that consists only of squares with the side of length $a$. All consecutive squares on a path should have a common side.\n\nObviously, as long as we have one square, these conditions are met. So Olya is ready to fulfill her sister's request only under the condition that she is satisfied too. Tell her: is it possible to perform exactly $k$ splitting operations in a certain order so that the condition of Olya's happiness is satisfied? If it is possible, tell also the size of the side of squares of which the path from the lower left square to the upper right one will consist.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10^3$)\u00a0\u2014 the number of tests.\n\nEach of the following $t$ lines contains two integers $n_i$ and $k_i$ ($1 \\le n_i \\le 10^9, 1 \\le k_i \\le 10^{18}$)\u00a0\u2014 the description of the $i$-th test, which means that initially Olya's square has size of $2^{n_i}\\times 2^{n_i}$ and Olya's sister asks her to do exactly $k_i$ splitting operations.\n\n\n-----Output-----\n\nPrint $t$ lines, where in the $i$-th line you should output \"YES\" if it is possible to perform $k_i$ splitting operations in the $i$-th test in such a way that the condition of Olya's happiness is satisfied or print \"NO\" otherwise. If you printed \"YES\", then also print the $log_2$ of the length of the side of the squares through space, along which you can build a path from the lower left square to the upper right one.\n\nYou can output each letter in any case (lower or upper).\n\nIf there are multiple answers, print any.\n\n\n-----Example-----\nInput\n3\n1 1\n2 2\n2 12\n\nOutput\nYES 0\nYES 1\nNO\n\n\n\n-----Note-----\n\nIn each of the illustrations, the pictures are shown in order in which Olya applied the operations. The recently-created squares are highlighted with red.\n\nIn the first test, Olya can apply splitting operations in the following order: [Image] Olya applies one operation on the only existing square. \n\nThe condition of Olya's happiness will be met, since there is a path of squares of the same size from the lower left square to the upper right one: [Image] \n\nThe length of the sides of the squares on the path is $1$. $log_2(1) = 0$.\n\nIn the second test, Olya can apply splitting operations in the following order: [Image] Olya applies the first operation on the only existing square. She applies the second one on the right bottom square. \n\nThe condition of Olya's happiness will be met, since there is a path of squares of the same size from the lower left square to the upper right one: [Image] \n\nThe length of the sides of the squares on the path is $2$. $log_2(2) = 1$.\n\nIn the third test, it takes $5$ operations for Olya to make the square look like this: [Image] \n\nSince it requires her to perform $7$ splitting operations, and it is impossible to perform them on squares with side equal to $1$, then Olya cannot do anything more and the answer is \"NO\"."} +{"id": "02344", "text": "Vasya wants to buy himself a nice new car. Unfortunately, he lacks some money. Currently he has exactly 0 burles.\n\nHowever, the local bank has $n$ credit offers. Each offer can be described with three numbers $a_i$, $b_i$ and $k_i$. Offers are numbered from $1$ to $n$. If Vasya takes the $i$-th offer, then the bank gives him $a_i$ burles at the beginning of the month and then Vasya pays bank $b_i$ burles at the end of each month for the next $k_i$ months (including the month he activated the offer). Vasya can take the offers any order he wants.\n\nEach month Vasya can take no more than one credit offer. Also each credit offer can not be used more than once. Several credits can be active at the same time. It implies that Vasya pays bank the sum of $b_i$ over all the $i$ of active credits at the end of each month.\n\nVasya wants to buy a car in the middle of some month. He just takes all the money he currently has and buys the car of that exact price.\n\nVasya don't really care what he'll have to pay the bank back after he buys a car. He just goes out of the country on his car so that the bank can't find him anymore.\n\nWhat is the maximum price that car can have?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 500$) \u2014 the number of credit offers.\n\nEach of the next $n$ lines contains three integers $a_i$, $b_i$ and $k_i$ ($1 \\le a_i, b_i, k_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum price of the car.\n\n\n-----Examples-----\nInput\n4\n10 9 2\n20 33 1\n30 115 1\n5 3 2\n\nOutput\n32\n\nInput\n3\n40 1 2\n1000 1100 5\n300 2 1\n\nOutput\n1337\n\n\n\n-----Note-----\n\nIn the first example, the following sequence of offers taken is optimal: 4 $\\rightarrow$ 3.\n\nThe amount of burles Vasya has changes the following way: 5 $\\rightarrow$ 32 $\\rightarrow$ -86 $\\rightarrow$ .... He takes the money he has in the middle of the second month (32 burles) and buys the car.\n\nThe negative amount of money means that Vasya has to pay the bank that amount of burles.\n\nIn the second example, the following sequence of offers taken is optimal: 3 $\\rightarrow$ 1 $\\rightarrow$ 2.\n\nThe amount of burles Vasya has changes the following way: 0 $\\rightarrow$ 300 $\\rightarrow$ 338 $\\rightarrow$ 1337 $\\rightarrow$ 236 $\\rightarrow$ -866 $\\rightarrow$ ...."} +{"id": "02345", "text": "You are given $n$ integers $a_1, a_2, \\dots, a_n$, such that for each $1\\le i \\le n$ holds $i-n\\le a_i\\le i-1$.\n\nFind some nonempty subset of these integers, whose sum is equal to $0$. It can be shown that such a subset exists under given constraints. If there are several possible subsets with zero-sum, you can find any of them.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^6$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1\\le n \\le 10^6$).\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($i-n \\le a_i \\le i-1$).\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.\n\n\n-----Output-----\n\nFor each test case, output two lines.\n\nIn the first line, output $s$ ($1\\le s \\le n$)\u00a0\u2014 the number of elements in your subset.\n\nIn the second line, output $s$ integers $i_1, i_2, \\dots, i_s$ ($1\\le i_k \\le n$). All integers have to be pairwise different, and $a_{i_1} + a_{i_2} + \\dots + a_{i_s}$ has to be equal to $0$. If there are several possible subsets with zero-sum, you can find any of them.\n\n\n-----Example-----\nInput\n2\n5\n0 1 2 3 4\n4\n-3 1 1 1\n\nOutput\n1\n1 \n4\n1 4 3 2 \n\n\n\n-----Note-----\n\nIn the first example, we get sum is $a_1 = 0$.\n\nIn the second example, we get sum is $a_1 + a_4 + a_3 + a_2 = 0$."} +{"id": "02346", "text": "You are given a rooted tree with vertices numerated from $1$ to $n$. A tree is a connected graph without cycles. A rooted tree has a special vertex named root.\n\nAncestors of the vertex $i$ are all vertices on the path from the root to the vertex $i$, except the vertex $i$ itself. The parent of the vertex $i$ is the nearest to the vertex $i$ ancestor of $i$. Each vertex is a child of its parent. In the given tree the parent of the vertex $i$ is the vertex $p_i$. For the root, the value $p_i$ is $-1$.\n\n [Image] An example of a tree with $n=8$, the root is vertex $5$. The parent of the vertex $2$ is vertex $3$, the parent of the vertex $1$ is vertex $5$. The ancestors of the vertex $6$ are vertices $4$ and $5$, the ancestors of the vertex $7$ are vertices $8$, $3$ and $5$ \n\nYou noticed that some vertices do not respect others. In particular, if $c_i = 1$, then the vertex $i$ does not respect any of its ancestors, and if $c_i = 0$, it respects all of them.\n\nYou decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex $v$, all children of $v$ become connected with the parent of $v$.\n\n [Image] An example of deletion of the vertex $7$. \n\nOnce there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the number of vertices in the tree.\n\nThe next $n$ lines describe the tree: the $i$-th line contains two integers $p_i$ and $c_i$ ($1 \\le p_i \\le n$, $0 \\le c_i \\le 1$), where $p_i$ is the parent of the vertex $i$, and $c_i = 0$, if the vertex $i$ respects its parents, and $c_i = 1$, if the vertex $i$ does not respect any of its parents. The root of the tree has $-1$ instead of the parent index, also, $c_i=0$ for the root. It is guaranteed that the values $p_i$ define a rooted tree with $n$ vertices.\n\n\n-----Output-----\n\nIn case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer $-1$.\n\n\n-----Examples-----\nInput\n5\n3 1\n1 1\n-1 0\n2 1\n3 0\n\nOutput\n1 2 4 \n\nInput\n5\n-1 0\n1 1\n1 1\n2 0\n3 0\n\nOutput\n-1\n\nInput\n8\n2 1\n-1 0\n1 0\n1 1\n1 1\n4 0\n5 1\n7 0\n\nOutput\n5 \n\n\n\n-----Note-----\n\nThe deletion process in the first example is as follows (see the picture below, the vertices with $c_i=1$ are in yellow):\n\n first you will delete the vertex $1$, because it does not respect ancestors and all its children (the vertex $2$) do not respect it, and $1$ is the smallest index among such vertices; the vertex $2$ will be connected with the vertex $3$ after deletion; then you will delete the vertex $2$, because it does not respect ancestors and all its children (the only vertex $4$) do not respect it; the vertex $4$ will be connected with the vertex $3$; then you will delete the vertex $4$, because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth); you will just delete the vertex $4$; there are no more vertices to delete. \n\n [Image] \n\nIn the second example you don't need to delete any vertex:\n\n vertices $2$ and $3$ have children that respect them; vertices $4$ and $5$ respect ancestors. \n\n [Image] \n\nIn the third example the tree will change this way:\n\n [Image]"} +{"id": "02347", "text": "Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.\n\nPolycarp decided to store the hash of the password, generated by the following algorithm: take the password $p$, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain $p'$ ($p'$ can still be equal to $p$); generate two random strings, consisting of lowercase Latin letters, $s_1$ and $s_2$ (any of these strings can be empty); the resulting hash $h = s_1 + p' + s_2$, where addition is string concatenation. \n\nFor example, let the password $p =$ \"abacaba\". Then $p'$ can be equal to \"aabcaab\". Random strings $s1 =$ \"zyx\" and $s2 =$ \"kjh\". Then $h =$ \"zyxaabcaabkjh\".\n\nNote that no letters could be deleted or added to $p$ to obtain $p'$, only the order could be changed.\n\nNow Polycarp asks you to help him to implement the password check module. Given the password $p$ and the hash $h$, check that $h$ can be the hash for the password $p$.\n\nYour program should answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThe first line of each test case contains a non-empty string $p$, consisting of lowercase Latin letters. The length of $p$ does not exceed $100$.\n\nThe second line of each test case contains a non-empty string $h$, consisting of lowercase Latin letters. The length of $h$ does not exceed $100$.\n\n\n-----Output-----\n\nFor each test case print the answer to it \u2014 \"YES\" if the given hash $h$ could be obtained from the given password $p$ or \"NO\" otherwise.\n\n\n-----Example-----\nInput\n5\nabacaba\nzyxaabcaabkjh\nonetwothree\nthreetwoone\none\nzzonneyy\none\nnone\ntwenty\nten\n\nOutput\nYES\nYES\nNO\nYES\nNO\n\n\n\n-----Note-----\n\nThe first test case is explained in the statement.\n\nIn the second test case both $s_1$ and $s_2$ are empty and $p'=$ \"threetwoone\" is $p$ shuffled.\n\nIn the third test case the hash could not be obtained from the password.\n\nIn the fourth test case $s_1=$ \"n\", $s_2$ is empty and $p'=$ \"one\" is $p$ shuffled (even thought it stayed the same). \n\nIn the fifth test case the hash could not be obtained from the password."} +{"id": "02348", "text": "-----Input-----\n\nThe only line of the input contains a 7-digit hexadecimal number. The first \"digit\" of the number is letter A, the rest of the \"digits\" are decimal digits 0-9.\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Examples-----\nInput\nA278832\n\nOutput\n0\n\nInput\nA089956\n\nOutput\n0\n\nInput\nA089957\n\nOutput\n1\n\nInput\nA144045\n\nOutput\n1"} +{"id": "02349", "text": "On the well-known testing system MathForces, a draw of $n$ rating units is arranged. The rating will be distributed according to the following algorithm: if $k$ participants take part in this event, then the $n$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remain\u00a0\u2014 it is not given to any of the participants.\n\nFor example, if $n = 5$ and $k = 3$, then each participant will recieve an $1$ rating unit, and also $2$ rating units will remain unused. If $n = 5$, and $k = 6$, then none of the participants will increase their rating.\n\nVasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.\n\nFor example, if $n=5$, then the answer is equal to the sequence $0, 1, 2, 5$. Each of the sequence values (and only them) can be obtained as $\\lfloor n/k \\rfloor$ for some positive integer $k$ (where $\\lfloor x \\rfloor$ is the value of $x$ rounded down): $0 = \\lfloor 5/7 \\rfloor$, $1 = \\lfloor 5/5 \\rfloor$, $2 = \\lfloor 5/2 \\rfloor$, $5 = \\lfloor 5/1 \\rfloor$.\n\nWrite a program that, for a given $n$, finds a sequence of all possible rating increments.\n\n\n-----Input-----\n\nThe first line contains integer number $t$ ($1 \\le t \\le 10$)\u00a0\u2014 the number of test cases in the input. Then $t$ test cases follow.\n\nEach line contains an integer $n$ ($1 \\le n \\le 10^9$)\u00a0\u2014 the total number of the rating units being drawn.\n\n\n-----Output-----\n\nOutput the answers for each of $t$ test cases. Each answer should be contained in two lines.\n\nIn the first line print a single integer $m$\u00a0\u2014 the number of different rating increment values that Vasya can get.\n\nIn the following line print $m$ integers in ascending order\u00a0\u2014 the values of possible rating increments.\n\n\n-----Example-----\nInput\n4\n5\n11\n1\n3\n\nOutput\n4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"} +{"id": "02350", "text": "During the quarantine, Sicromoft has more free time to create the new functions in \"Celex-2021\". The developers made a new function GAZ-GIZ, which infinitely fills an infinite table to the right and down from the upper left corner as follows:\n\n [Image] The cell with coordinates $(x, y)$ is at the intersection of $x$-th row and $y$-th column. Upper left cell $(1,1)$ contains an integer $1$.\n\nThe developers of the SUM function don't sleep either. Because of the boredom, they teamed up with the developers of the RAND function, so they added the ability to calculate the sum on an arbitrary path from one cell to another, moving down or right. Formally, from the cell $(x,y)$ in one step you can move to the cell $(x+1, y)$ or $(x, y+1)$. \n\nAfter another Dinwows update, Levian started to study \"Celex-2021\" (because he wants to be an accountant!). After filling in the table with the GAZ-GIZ function, he asked you to calculate the quantity of possible different amounts on the path from a given cell $(x_1, y_1)$ to another given cell $(x_2, y_2$), if you can only move one cell down or right.\n\nFormally, consider all the paths from the cell $(x_1, y_1)$ to cell $(x_2, y_2)$ such that each next cell in the path is located either to the down or to the right of the previous one. Calculate the number of different sums of elements for all such paths.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 57179$) \u2014 the number of test cases.\n\nEach of the following $t$ lines contains four natural numbers $x_1$, $y_1$, $x_2$, $y_2$ ($1 \\le x_1 \\le x_2 \\le 10^9$, $1 \\le y_1 \\le y_2 \\le 10^9$) \u2014 coordinates of the start and the end cells. \n\n\n-----Output-----\n\nFor each test case, in a separate line, print the number of possible different sums on the way from the start cell to the end cell.\n\n\n-----Example-----\nInput\n4\n1 1 2 2\n1 2 2 4\n179 1 179 100000\n5 7 5 7\n\nOutput\n2\n3\n1\n1\n\n\n\n-----Note-----\n\nIn the first test case there are two possible sums: $1+2+5=8$ and $1+3+5=9$. [Image]"} +{"id": "02351", "text": "Vasya has got an array consisting of $n$ integers, and two integers $k$ and $len$ in addition. All numbers in the array are either between $1$ and $k$ (inclusive), or equal to $-1$. The array is good if there is no segment of $len$ consecutive equal numbers.\n\nVasya will replace each $-1$ with some number from $1$ to $k$ (inclusive) in such a way that the resulting array is good. Tell him the number of ways to do this replacement. Since the answer may be large, print it modulo $998244353$.\n\n\n-----Input-----\n\nThe first line contains three integers $n, k$ and $len$ ($1 \\le n \\le 10^5, 1 \\le k \\le 100, 1 \\le len \\le n$).\n\nThe second line contains $n$ numbers \u2014 the array. Each number is either $-1$ or between $1$ and $k$ (inclusive).\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of ways to replace each $-1$ with some number from $1$ to $k$ (inclusive) so the array is good. The answer may be large, so print it modulo $998244353$.\n\n\n-----Examples-----\nInput\n5 2 3\n1 -1 1 -1 2\n\nOutput\n2\n\nInput\n6 3 2\n1 1 -1 -1 -1 -1\n\nOutput\n0\n\nInput\n10 42 7\n-1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n\nOutput\n645711643\n\n\n\n-----Note-----\n\nPossible answers in the first test: $[1, 2, 1, 1, 2]$; $[1, 2, 1, 2, 2]$. \n\nThere is no way to make the array good in the second test, since first two elements are equal.\n\nThere are too many answers in the third test, so we won't describe any of them."} +{"id": "02352", "text": "After a hard-working week Polycarp prefers to have fun. Polycarp's favorite entertainment is drawing snakes. He takes a rectangular checkered sheet of paper of size $n \\times m$ (where $n$ is the number of rows, $m$ is the number of columns) and starts to draw snakes in cells.\n\nPolycarp draws snakes with lowercase Latin letters. He always draws the first snake with the symbol 'a', the second snake with the symbol 'b', the third snake with the symbol 'c' and so on. All snakes have their own unique symbol. There are only $26$ letters in the Latin alphabet, Polycarp is very tired and he doesn't want to invent new symbols, so the total number of drawn snakes doesn't exceed $26$.\n\nSince by the end of the week Polycarp is very tired, he draws snakes as straight lines without bends. So each snake is positioned either vertically or horizontally. Width of any snake equals $1$, i.e. each snake has size either $1 \\times l$ or $l \\times 1$, where $l$ is snake's length. Note that snakes can't bend.\n\nWhen Polycarp draws a new snake, he can use already occupied cells for drawing the snake. In this situation, he draws the snake \"over the top\" and overwrites the previous value in the cell.\n\nRecently when Polycarp was at work he found a checkered sheet of paper with Latin letters. He wants to know if it is possible to get this sheet of paper from an empty sheet by drawing some snakes according to the rules described above. If it is possible, he is interested in a way to draw snakes.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^5$) \u2014 the number of test cases to solve. Then $t$ test cases follow.\n\nThe first line of the test case description contains two integers $n$, $m$ ($1 \\le n,m \\le 2000$)\u00a0\u2014 length and width of the checkered sheet of paper respectively.\n\nNext $n$ lines of test case description contain $m$ symbols, which are responsible for the content of the corresponding cell on the sheet. It can be either lowercase Latin letter or symbol dot ('.'), which stands for an empty cell.\n\nIt is guaranteed that the total area of all sheets in one test doesn't exceed $4\\cdot10^6$.\n\n\n-----Output-----\n\nPrint the answer for each test case in the input.\n\nIn the first line of the output for a test case print YES if it is possible to draw snakes, so that you can get a sheet of paper from the input. If it is impossible, print NO.\n\nIf the answer to this question is positive, then print the way to draw snakes in the following format. In the next line print one integer $k$ ($0 \\le k \\le 26$)\u00a0\u2014 number of snakes. Then print $k$ lines, in each line print four integers $r_{1,i}$, $c_{1,i}$, $r_{2,i}$ and $c_{2,i}$\u00a0\u2014 coordinates of extreme cells for the $i$-th snake ($1 \\le r_{1,i}, r_{2,i} \\le n$, $1 \\le c_{1,i}, c_{2,i} \\le m$). Snakes should be printed in order of their drawing. If there are multiple solutions, you are allowed to print any of them.\n\nNote that Polycarp starts drawing of snakes with an empty sheet of paper.\n\n\n-----Examples-----\nInput\n1\n5 6\n...a..\n..bbb.\n...a..\n.cccc.\n...a..\n\nOutput\nYES\n3\n1 4 5 4\n2 3 2 5\n4 2 4 5\n\nInput\n3\n3 3\n...\n...\n...\n4 4\n..c.\nadda\nbbcb\n....\n3 5\n..b..\naaaaa\n..b..\n\nOutput\nYES\n0\nYES\n4\n2 1 2 4\n3 1 3 4\n1 3 3 3\n2 2 2 3\nNO\n\nInput\n2\n3 3\n...\n.a.\n...\n2 2\nbb\ncc\n\nOutput\nYES\n1\n2 2 2 2\nYES\n3\n1 1 1 2\n1 1 1 2\n2 1 2 2"} +{"id": "02353", "text": "Polycarp has spent the entire day preparing problems for you. Now he has to sleep for at least $a$ minutes to feel refreshed.\n\nPolycarp can only wake up by hearing the sound of his alarm. So he has just fallen asleep and his first alarm goes off in $b$ minutes.\n\nEvery time Polycarp wakes up, he decides if he wants to sleep for some more time or not. If he's slept for less than $a$ minutes in total, then he sets his alarm to go off in $c$ minutes after it is reset and spends $d$ minutes to fall asleep again. Otherwise, he gets out of his bed and proceeds with the day.\n\nIf the alarm goes off while Polycarp is falling asleep, then he resets his alarm to go off in another $c$ minutes and tries to fall asleep for $d$ minutes again.\n\nYou just want to find out when will Polycarp get out of his bed or report that it will never happen.\n\nPlease check out the notes for some explanations of the example.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of testcases.\n\nThe only line of each testcase contains four integers $a, b, c, d$ ($1 \\le a, b, c, d \\le 10^9$)\u00a0\u2014 the time Polycarp has to sleep for to feel refreshed, the time before the first alarm goes off, the time before every succeeding alarm goes off and the time Polycarp spends to fall asleep.\n\n\n-----Output-----\n\nFor each test case print one integer. If Polycarp never gets out of his bed then print -1. Otherwise, print the time it takes for Polycarp to get out of his bed.\n\n\n-----Example-----\nInput\n7\n10 3 6 4\n11 3 6 4\n5 9 4 10\n6 5 2 3\n1 1 1 1\n3947465 47342 338129 123123\n234123843 13 361451236 361451000\n\nOutput\n27\n27\n9\n-1\n1\n6471793\n358578060125049\n\n\n\n-----Note-----\n\nIn the first testcase Polycarp wakes up after $3$ minutes. He only rested for $3$ minutes out of $10$ minutes he needed. So after that he sets his alarm to go off in $6$ minutes and spends $4$ minutes falling asleep. Thus, he rests for $2$ more minutes, totaling in $3+2=5$ minutes of sleep. Then he repeats the procedure three more times and ends up with $11$ minutes of sleep. Finally, he gets out of his bed. He spent $3$ minutes before the first alarm and then reset his alarm four times. The answer is $3+4 \\cdot 6 = 27$.\n\nThe second example is almost like the first one but Polycarp needs $11$ minutes of sleep instead of $10$. However, that changes nothing because he gets $11$ minutes with these alarm parameters anyway.\n\nIn the third testcase Polycarp wakes up rested enough after the first alarm. Thus, the answer is $b=9$.\n\nIn the fourth testcase Polycarp wakes up after $5$ minutes. Unfortunately, he keeps resetting his alarm infinitely being unable to rest for even a single minute :("} +{"id": "02354", "text": "You are given a chessboard of size $n \\times n$. It is filled with numbers from $1$ to $n^2$ in the following way: the first $\\lceil \\frac{n^2}{2} \\rceil$ numbers from $1$ to $\\lceil \\frac{n^2}{2} \\rceil$ are written in the cells with even sum of coordinates from left to right from top to bottom. The rest $n^2 - \\lceil \\frac{n^2}{2} \\rceil$ numbers from $\\lceil \\frac{n^2}{2} \\rceil + 1$ to $n^2$ are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation $\\lceil\\frac{x}{y}\\rceil$ means division $x$ by $y$ rounded up.\n\nFor example, the left board on the following picture is the chessboard which is given for $n=4$ and the right board is the chessboard which is given for $n=5$. [Image] \n\nYou are given $q$ queries. The $i$-th query is described as a pair $x_i, y_i$. The answer to the $i$-th query is the number written in the cell $x_i, y_i$ ($x_i$ is the row, $y_i$ is the column). Rows and columns are numbered from $1$ to $n$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $q$ ($1 \\le n \\le 10^9$, $1 \\le q \\le 10^5$) \u2014 the size of the board and the number of queries.\n\nThe next $q$ lines contain two integers each. The $i$-th line contains two integers $x_i, y_i$ ($1 \\le x_i, y_i \\le n$) \u2014 description of the $i$-th query.\n\n\n-----Output-----\n\nFor each query from $1$ to $q$ print the answer to this query. The answer to the $i$-th query is the number written in the cell $x_i, y_i$ ($x_i$ is the row, $y_i$ is the column). Rows and columns are numbered from $1$ to $n$. Queries are numbered from $1$ to $q$ in order of the input.\n\n\n-----Examples-----\nInput\n4 5\n1 1\n4 4\n4 3\n3 2\n2 4\n\nOutput\n1\n8\n16\n13\n4\n\nInput\n5 4\n2 1\n4 2\n3 3\n3 4\n\nOutput\n16\n9\n7\n20\n\n\n\n-----Note-----\n\nAnswers to the queries from examples are on the board in the picture from the problem statement."} +{"id": "02355", "text": "Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill: the graph contains exactly 2n + p edges; the graph doesn't contain self-loops and multiple edges; for any integer k (1 \u2264 k \u2264 n), any subgraph consisting of k vertices contains at most 2k + p edges. \n\nA subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices. \n\nYour task is to find a p-interesting graph consisting of n vertices.\n\n\n-----Input-----\n\nThe first line contains a single integer t (1 \u2264 t \u2264 5) \u2014 the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 \u2264 n \u2264 24; p \u2265 0; $2 n + p \\leq \\frac{n(n - 1)}{2}$) \u2014 the number of vertices in the graph and the interest value for the appropriate test. \n\nIt is guaranteed that the required graph exists.\n\n\n-----Output-----\n\nFor each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n;\u00a0a_{i} \u2260 b_{i}) \u2014 two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n. \n\nPrint the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.\n\n\n-----Examples-----\nInput\n1\n6 0\n\nOutput\n1 2\n1 3\n1 4\n1 5\n1 6\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6"} +{"id": "02356", "text": "You are given an array $a_1, a_2, \\dots , a_n$. Array is good if for each pair of indexes $i < j$ the condition $j - a_j \\ne i - a_i$ holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).\n\nFor example, if $a = [1, 1, 3, 5]$, then shuffled arrays $[1, 3, 5, 1]$, $[3, 5, 1, 1]$ and $[5, 3, 1, 1]$ are good, but shuffled arrays $[3, 1, 5, 1]$, $[1, 1, 3, 5]$ and $[1, 1, 5, 3]$ aren't.\n\nIt's guaranteed that it's always possible to shuffle an array to meet this condition.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThe first line of each test case contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the length of array $a$.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\dots , a_n$ ($1 \\le a_i \\le 100$).\n\n\n-----Output-----\n\nFor each test case print the shuffled version of the array $a$ which is good.\n\n\n-----Example-----\nInput\n3\n1\n7\n4\n1 1 3 5\n6\n3 2 1 5 6 4\n\nOutput\n7\n1 5 1 3\n2 4 6 1 3 5"} +{"id": "02357", "text": "Let's call an array $t$ dominated by value $v$ in the next situation.\n\nAt first, array $t$ should have at least $2$ elements. Now, let's calculate number of occurrences of each number $num$ in $t$ and define it as $occ(num)$. Then $t$ is dominated (by $v$) if (and only if) $occ(v) > occ(v')$ for any other number $v'$. For example, arrays $[1, 2, 3, 4, 5, 2]$, $[11, 11]$ and $[3, 2, 3, 2, 3]$ are dominated (by $2$, $11$ and $3$ respectevitely) but arrays $[3]$, $[1, 2]$ and $[3, 3, 2, 2, 1]$ are not.\n\nSmall remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not.\n\nYou are given array $a_1, a_2, \\dots, a_n$. Calculate its shortest dominated subarray or say that there are no such subarrays.\n\nThe subarray of $a$ is a contiguous part of the array $a$, i. e. the array $a_i, a_{i + 1}, \\dots, a_j$ for some $1 \\le i \\le j \\le n$.\n\n\n-----Input-----\n\nThe first line contains single integer $T$ ($1 \\le T \\le 1000$) \u2014 the number of test cases. Each test case consists of two lines.\n\nThe first line contains single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of the array $a$.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$) \u2014 the corresponding values of the array $a$.\n\nIt's guaranteed that the total length of all arrays in one test doesn't exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nPrint $T$ integers \u2014 one per test case. For each test case print the only integer \u2014 the length of the shortest dominated subarray, or $-1$ if there are no such subarrays.\n\n\n-----Example-----\nInput\n4\n1\n1\n6\n1 2 3 4 5 1\n9\n4 1 2 4 5 4 3 2 1\n4\n3 3 3 3\n\nOutput\n-1\n6\n3\n2\n\n\n\n-----Note-----\n\nIn the first test case, there are no subarrays of length at least $2$, so the answer is $-1$.\n\nIn the second test case, the whole array is dominated (by $1$) and it's the only dominated subarray.\n\nIn the third test case, the subarray $a_4, a_5, a_6$ is the shortest dominated subarray.\n\nIn the fourth test case, all subarrays of length more than one are dominated."} +{"id": "02358", "text": "Now that Kuroni has reached 10 years old, he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!\n\nWe say that a string formed by $n$ characters '(' or ')' is simple if its length $n$ is even and positive, its first $\\frac{n}{2}$ characters are '(', and its last $\\frac{n}{2}$ characters are ')'. For example, the strings () and (()) are simple, while the strings )( and ()() are not simple.\n\nKuroni will be given a string formed by characters '(' and ')' (the given string is not necessarily simple). An operation consists of choosing a subsequence of the characters of the string that forms a simple string and removing all the characters of this subsequence from the string. Note that this subsequence doesn't have to be continuous. For example, he can apply the operation to the string ')()(()))', to choose a subsequence of bold characters, as it forms a simple string '(())', delete these bold characters from the string and to get '))()'. \n\nKuroni has to perform the minimum possible number of operations on the string, in such a way that no more operations can be performed on the remaining string. The resulting string does not have to be empty.\n\nSince the given string is too large, Kuroni is unable to figure out how to minimize the number of operations. Can you help him do it instead?\n\nA sequence of characters $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters.\n\n\n-----Input-----\n\nThe only line of input contains a string $s$ ($1 \\le |s| \\le 1000$) formed by characters '(' and ')', where $|s|$ is the length of $s$.\n\n\n-----Output-----\n\nIn the first line, print an integer $k$ \u00a0\u2014 the minimum number of operations you have to apply. Then, print $2k$ lines describing the operations in the following format:\n\nFor each operation, print a line containing an integer $m$ \u00a0\u2014 the number of characters in the subsequence you will remove.\n\nThen, print a line containing $m$ integers $1 \\le a_1 < a_2 < \\dots < a_m$ \u00a0\u2014 the indices of the characters you will remove. All integers must be less than or equal to the length of the current string, and the corresponding subsequence must form a simple string.\n\nIf there are multiple valid sequences of operations with the smallest $k$, you may print any of them.\n\n\n-----Examples-----\nInput\n(()((\n\nOutput\n1\n2\n1 3 \n\nInput\n)(\n\nOutput\n0\n\nInput\n(()())\n\nOutput\n1\n4\n1 2 5 6 \n\n\n\n-----Note-----\n\nIn the first sample, the string is '(()(('. The operation described corresponds to deleting the bolded subsequence. The resulting string is '(((', and no more operations can be performed on it. Another valid answer is choosing indices $2$ and $3$, which results in the same final string.\n\nIn the second sample, it is already impossible to perform any operations."} +{"id": "02359", "text": "There are two infinite sources of water: hot water of temperature $h$; cold water of temperature $c$ ($c < h$). \n\nYou perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $\\dots$ and so on $\\dots$ \n\nNote that you always start with the cup of hot water.\n\nThe barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.\n\nYou want to achieve a temperature as close as possible to $t$. So if the temperature in the barrel is $t_b$, then the absolute difference of $t_b$ and $t$ ($|t_b - t|$) should be as small as possible.\n\nHow many cups should you pour into the barrel, so that the temperature in it is as close as possible to $t$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\le T \\le 3 \\cdot 10^4$)\u00a0\u2014 the number of testcases.\n\nEach of the next $T$ lines contains three integers $h$, $c$ and $t$ ($1 \\le c < h \\le 10^6$; $c \\le t \\le h$)\u00a0\u2014 the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.\n\n\n-----Output-----\n\nFor each testcase print a single positive integer\u00a0\u2014 the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $t$.\n\n\n-----Example-----\nInput\n3\n30 10 20\n41 15 30\n18 13 18\n\nOutput\n2\n7\n1\n\n\n\n-----Note-----\n\nIn the first testcase the temperature after $2$ poured cups: $1$ hot and $1$ cold is exactly $20$. So that is the closest we can achieve.\n\nIn the second testcase the temperature after $7$ poured cups: $4$ hot and $3$ cold is about $29.857$. Pouring more water won't get us closer to $t$ than that.\n\nIn the third testcase the temperature after $1$ poured cup: $1$ hot is $18$. That's exactly equal to $t$."} +{"id": "02360", "text": "Recently n students from city S moved to city P to attend a programming camp.\n\nThey moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea.\n\ni-th student comes to the end of the queue at the beginning of l_{i}-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of r_{i}-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. \n\nFor each student determine the second he will use the teapot and get his tea (if he actually gets it).\n\n\n-----Input-----\n\nThe first line contains one integer t \u2014 the number of test cases to solve (1 \u2264 t \u2264 1000).\n\nThen t test cases follow. The first line of each test case contains one integer n (1 \u2264 n \u2264 1000) \u2014 the number of students.\n\nThen n lines follow. Each line contains two integer l_{i}, r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 5000) \u2014 the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea.\n\nIt is guaranteed that for every $i \\in [ 2 ; n ]$ condition l_{i} - 1 \u2264 l_{i} holds.\n\nThe sum of n over all test cases doesn't exceed 1000.\n\nNote that in hacks you have to set t = 1.\n\n\n-----Output-----\n\nFor each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea.\n\n\n-----Example-----\nInput\n2\n2\n1 3\n1 4\n3\n1 5\n1 1\n2 3\n\nOutput\n1 2 \n1 0 2 \n\n\n\n-----Note-----\n\nThe example contains 2 tests:\n\n During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea."} +{"id": "02361", "text": "The game of Berland poker is played with a deck of $n$ cards, $m$ of which are jokers. $k$ players play this game ($n$ is divisible by $k$).\n\nAt the beginning of the game, each player takes $\\frac{n}{k}$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $x - y$, where $x$ is the number of jokers in the winner's hand, and $y$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $0$ points.\n\nHere are some examples: $n = 8$, $m = 3$, $k = 2$. If one player gets $3$ jokers and $1$ plain card, and another player gets $0$ jokers and $4$ plain cards, then the first player is the winner and gets $3 - 0 = 3$ points; $n = 4$, $m = 2$, $k = 4$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $0$ points; $n = 9$, $m = 6$, $k = 3$. If the first player gets $3$ jokers, the second player gets $1$ joker and $2$ plain cards, and the third player gets $2$ jokers and $1$ plain card, then the first player is the winner, and he gets $3 - 2 = 1$ point; $n = 42$, $m = 0$, $k = 7$. Since there are no jokers, everyone gets $0$ jokers, everyone is a winner, and everyone gets $0$ points. \n\nGiven $n$, $m$ and $k$, calculate the maximum number of points a player can get for winning the game.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 500$) \u2014 the number of test cases.\n\nThen the test cases follow. Each test case contains three integers $n$, $m$ and $k$ ($2 \\le n \\le 50$, $0 \\le m \\le n$, $2 \\le k \\le n$, $k$ is a divisors of $n$).\n\n\n-----Output-----\n\nFor each test case, print one integer \u2014 the maximum number of points a player can get for winning the game.\n\n\n-----Example-----\nInput\n4\n8 3 2\n4 2 4\n9 6 3\n42 0 7\n\nOutput\n3\n0\n1\n0\n\n\n\n-----Note-----\n\nTest cases of the example are described in the statement."} +{"id": "02362", "text": "You are given a tree consisting of $n$ vertices. A number is written on each vertex; the number on vertex $i$ is equal to $a_i$.\n\nLet's denote the function $g(x, y)$ as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex $x$ to vertex $y$ (including these two vertices). Also let's denote $dist(x, y)$ as the number of vertices on the simple path between vertices $x$ and $y$, including the endpoints. $dist(x, x) = 1$ for every vertex $x$.\n\nYour task is calculate the maximum value of $dist(x, y)$ among such pairs of vertices that $g(x, y) > 1$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ \u2014 the number of vertices $(1 \\le n \\le 2 \\cdot 10^5)$.\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \\le a_i \\le 2 \\cdot 10^5)$ \u2014 the numbers written on vertices.\n\nThen $n - 1$ lines follow, each containing two integers $x$ and $y$ $(1 \\le x, y \\le n, x \\ne y)$ denoting an edge connecting vertex $x$ with vertex $y$. It is guaranteed that these edges form a tree.\n\n\n-----Output-----\n\nIf there is no pair of vertices $x, y$ such that $g(x, y) > 1$, print $0$. Otherwise print the maximum value of $dist(x, y)$ among such pairs.\n\n\n-----Examples-----\nInput\n3\n2 3 4\n1 2\n2 3\n\nOutput\n1\n\nInput\n3\n2 3 4\n1 3\n2 3\n\nOutput\n2\n\nInput\n3\n1 1 1\n1 2\n2 3\n\nOutput\n0"} +{"id": "02363", "text": "You've got two numbers. As long as they are both larger than zero, they go through the same operation: subtract the lesser number from the larger one. If they equal substract one number from the another. For example, one operation transforms pair (4,17) to pair (4,13), it transforms (5,5) to (0,5).\n\nYou've got some number of pairs (a_{i}, b_{i}). How many operations will be performed for each of them?\n\n\n-----Input-----\n\nThe first line contains the number of pairs n (1 \u2264 n \u2264 1000). Then follow n lines, each line contains a pair of positive integers a_{i}, b_{i} (1 \u2264 a_{i}, b_{i} \u2264 10^9).\n\n\n-----Output-----\n\nPrint the sought number of operations for each pair on a single line.\n\n\n-----Examples-----\nInput\n2\n4 17\n7 987654321\n\nOutput\n8\n141093479"} +{"id": "02364", "text": "Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.\n\nThe path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is $n$ km. Let's say that Moscow is situated at the point with coordinate $0$ km, and Saratov \u2014 at coordinate $n$ km.\n\nDriving for a long time may be really difficult. Formally, if Leha has already covered $i$ kilometers since he stopped to have a rest, he considers the difficulty of covering $(i + 1)$-th kilometer as $a_{i + 1}$. It is guaranteed that for every $i \\in [1, n - 1]$ $a_i \\le a_{i + 1}$. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.\n\nFortunately, there may be some rest sites between Moscow and Saratov. Every integer point from $1$ to $n - 1$ may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty $a_1$, the kilometer after it \u2014 difficulty $a_2$, and so on.\n\nFor example, if $n = 5$ and there is a rest site in coordinate $2$, the difficulty of journey will be $2a_1 + 2a_2 + a_3$: the first kilometer will have difficulty $a_1$, the second one \u2014 $a_2$, then Leha will have a rest, and the third kilometer will have difficulty $a_1$, the fourth \u2014 $a_2$, and the last one \u2014 $a_3$. Another example: if $n = 7$ and there are rest sites in coordinates $1$ and $5$, the difficulty of Leha's journey is $3a_1 + 2a_2 + a_3 + a_4$.\n\nLeha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are $2^{n - 1}$ different distributions of rest sites (two distributions are different if there exists some point $x$ such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate $p$ \u2014 the expected value of difficulty of his journey.\n\nObviously, $p \\cdot 2^{n - 1}$ is an integer number. You have to calculate it modulo $998244353$.\n\n\n-----Input-----\n\nThe first line contains one number $n$ ($1 \\le n \\le 10^6$) \u2014 the distance from Moscow to Saratov.\n\nThe second line contains $n$ integer numbers $a_1$, $a_2$, ..., $a_n$ ($1 \\le a_1 \\le a_2 \\le \\dots \\le a_n \\le 10^6$), where $a_i$ is the difficulty of $i$-th kilometer after Leha has rested.\n\n\n-----Output-----\n\nPrint one number \u2014 $p \\cdot 2^{n - 1}$, taken modulo $998244353$.\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n5\n\nInput\n4\n1 3 3 7\n\nOutput\n60"} +{"id": "02365", "text": "Return the result of evaluating a given boolean expression, represented as a string.\nAn expression can either be:\n\n\"t\", evaluating to True;\n\"f\", evaluating to False;\n\"!(expr)\", evaluating to the logical NOT of the inner expression expr;\n\"&(expr1,expr2,...)\", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...;\n\"|(expr1,expr2,...)\", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...\n\n\u00a0\nExample 1:\nInput: expression = \"!(f)\"\nOutput: true\n\nExample 2:\nInput: expression = \"|(f,t)\"\nOutput: true\n\nExample 3:\nInput: expression = \"&(t,f)\"\nOutput: false\n\nExample 4:\nInput: expression = \"|(&(t,f,t),!(t))\"\nOutput: false\n\n\u00a0\nConstraints:\n\n1 <= expression.length <= 20000\nexpression[i]\u00a0consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}.\nexpression is a valid expression representing a boolean, as given in the description."} +{"id": "02366", "text": "We have N balls. The i-th ball has an integer A_i written on it.\n\nFor each k=1, 2, ..., N, solve the following problem and print the answer. \n - Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.\n\n-----Constraints-----\n - 3 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq A_i \\leq N\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nFor each k=1,2,...,N, print a line containing the answer.\n\n-----Sample Input-----\n5\n1 1 2 1 2\n\n-----Sample Output-----\n2\n2\n3\n2\n3\n\nConsider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.\n\nFrom these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.\n\nThus, the answer for k=1 is 2."} +{"id": "02367", "text": "We have a large square grid with H rows and W columns.\nIroha is now standing in the top-left cell.\nShe will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.\nHowever, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A\u00d7B forbidden cells.) There is no restriction on entering the other cells.\nFind the number of ways she can travel to the bottom-right cell.\nSince this number can be extremely large, print the number modulo 10^9+7.\n\n-----Constraints-----\n - 1 \u2266 H, W \u2266 100,000\n - 1 \u2266 A < H\n - 1 \u2266 B < W\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nH W A B\n\n-----Output-----\nPrint the number of ways she can travel to the bottom-right cell, modulo 10^9+7.\n\n-----Sample Input-----\n2 3 1 1\n\n-----Sample Output-----\n2\n\nWe have a 2\u00d73 grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: \"Right, Right, Down\" and \"Right, Down, Right\"."} +{"id": "02368", "text": "Given is a simple undirected graph with N vertices and M edges. The i-th edge connects Vertex c_i and Vertex d_i.\nInitially, Vertex i has the value a_i written on it. You want to change the values on Vertex 1, \\ldots, Vertex N to b_1, \\cdots, b_N, respectively, by doing the operation below zero or more times.\n - Choose an edge, and let Vertex x and Vertex y be the vertices connected by that edge. Choose one of the following and do it:\n - Decrease the value a_x by 1, and increase the value a_y by 1.\n - Increase the value a_x by 1, and decrease the value a_y by 1.\nDetermine whether it is possible to achieve the objective by properly doing the operation.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - 0 \\leq M \\leq 2 \\times 10^5\n - -10^9 \\leq a_i,b_i \\leq 10^9\n - 1 \\leq c_i,d_i \\leq N\n - The given graph is simple, that is, has no self-loops and no multi-edges.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\na_1 \\cdots a_N\nb_1 \\cdots b_N\nc_1 d_1\n\\vdots\nc_M d_M\n\n-----Output-----\nPrint Yes if it is possible to achieve the objective by properly doing the operation, and No otherwise.\n\n-----Sample Input-----\n3 2\n1 2 3\n2 2 2\n1 2\n2 3\n\n-----Sample Output-----\nYes\n\nYou can achieve the objective by, for example, doing the operation as follows:\n - In the first operation, choose the edge connecting Vertex 1 and 2. Then, increase a_1 by 1 and decrease a_2 by 1.\n - In the second operation, choose the edge connecting Vertex 2 and 3. Then, increase a_2 by 1 and decrease a_3 by 1.\nThis sequence of operations makes a_1=2, a_2=2, and a_3=2."} +{"id": "02369", "text": "For a finite set of integers X, let f(X)=\\max X - \\min X.\nGiven are N integers A_1,...,A_N.\nWe will choose K of them and let S be the set of the integers chosen. If we distinguish elements with different indices even when their values are the same, there are {}_N C_K ways to make this choice. Find the sum of f(S) over all those ways.\nSince the answer can be enormous, print it \\bmod (10^9+7).\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - 1 \\leq K \\leq N\n - |A_i| \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nA_1 ... A_N\n\n-----Output-----\nPrint the answer \\bmod (10^9+7).\n\n-----Sample Input-----\n4 2\n1 1 3 4\n\n-----Sample Output-----\n11\n\nThere are six ways to choose S: \\{1,1\\},\\{1,3\\},\\{1,4\\},\\{1,3\\},\\{1,4\\}, \\{3,4\\} (we distinguish the two 1s). The value of f(S) for these choices are 0,2,3,2,3,1, respectively, for the total of 11."} +{"id": "02370", "text": "In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads.\nThe following are known about the road network:\n - People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.\n - Different roads may have had different lengths, but all the lengths were positive integers.\nSnuke the archeologist found a table with N rows and N columns, A, in the ruin of Takahashi Kingdom.\nHe thought that it represented the shortest distances between the cities along the roads in the kingdom.\nDetermine whether there exists a road network such that for each u and v, the integer A_{u, v} at the u-th row and v-th column of A is equal to the length of the shortest path from City u to City v.\nIf such a network exist, find the shortest possible total length of the roads.\n\n-----Constraints-----\n - 1 \\leq N \\leq 300\n - If i \u2260 j, 1 \\leq A_{i, j} = A_{j, i} \\leq 10^9.\n - A_{i, i} = 0\n\n-----Inputs-----\nInput is given from Standard Input in the following format:\nN\nA_{1, 1} A_{1, 2} ... A_{1, N}\nA_{2, 1} A_{2, 2} ... A_{2, N}\n...\nA_{N, 1} A_{N, 2} ... A_{N, N}\n\n-----Outputs-----\nIf there exists no network that satisfies the condition, print -1.\nIf it exists, print the shortest possible total length of the roads.\n\n-----Sample Input-----\n3\n0 1 3\n1 0 2\n3 2 0\n\n-----Sample Output-----\n3\n\nThe network below satisfies the condition:\n - City 1 and City 2 is connected by a road of length 1.\n - City 2 and City 3 is connected by a road of length 2.\n - City 3 and City 1 is not connected by a road."} +{"id": "02371", "text": "We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.\nTwo people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:\n - Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.\nThe game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.\nX will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?\n\n-----Constraints-----\n - All input values are integers.\n - 1 \\leq N \\leq 2000\n - 1 \\leq Z, W, a_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN Z W\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the score.\n\n-----Sample Input-----\n3 100 100\n10 1000 100\n\n-----Sample Output-----\n900\n\nIf X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900."} +{"id": "02372", "text": "A maze is composed of a grid of H \\times W squares - H vertical, W horizontal.\nThe square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..\nThere is a magician in (C_h,C_w). He can do the following two kinds of moves:\n - Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.\n - Move B: Use magic to warp himself to a road square in the 5\\times 5 area centered at the square he is currently in.\nIn either case, he cannot go out of the maze.\nAt least how many times does he need to use the magic to reach (D_h, D_w)?\n\n-----Constraints-----\n - 1 \\leq H,W \\leq 10^3\n - 1 \\leq C_h,D_h \\leq H\n - 1 \\leq C_w,D_w \\leq W\n - S_{ij} is # or ..\n - S_{C_h C_w} and S_{D_h D_w} are ..\n - (C_h,C_w) \\neq (D_h,D_w)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\nC_h C_w\nD_h D_w\nS_{11}\\ldots S_{1W}\n\\vdots\nS_{H1}\\ldots S_{HW}\n\n-----Output-----\nPrint the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.\n\n-----Sample Input-----\n4 4\n1 1\n4 4\n..#.\n..#.\n.#..\n.#..\n\n-----Sample Output-----\n1\n\nFor example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.\nNote that he cannot walk diagonally."} +{"id": "02373", "text": "You are given a permutation p_1,p_2,...,p_N consisting of 1,2,..,N.\nYou can perform the following operation any number of times (possibly zero):\nOperation: Swap two adjacent elements in the permutation.\nYou want to have p_i \u2260 i for all 1\u2264i\u2264N.\nFind the minimum required number of operations to achieve this.\n\n-----Constraints-----\n - 2\u2264N\u226410^5\n - p_1,p_2,..,p_N is a permutation of 1,2,..,N.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\np_1 p_2 .. p_N\n\n-----Output-----\nPrint the minimum required number of operations\n\n-----Sample Input-----\n5\n1 4 3 5 2\n\n-----Sample Output-----\n2\n\nSwap 1 and 4, then swap 1 and 3. p is now 4,3,1,5,2 and satisfies the condition.\nThis is the minimum possible number, so the answer is 2."} +{"id": "02374", "text": "After being invaded by the Kingdom of AlDebaran, bombs are planted throughout our country, AtCoder Kingdom.\nFortunately, our military team called ABC has managed to obtain a device that is a part of the system controlling the bombs.\nThere are N bombs, numbered 1 to N, planted in our country. Bomb i is planted at the coordinate A_i. It is currently activated if B_i=1, and deactivated if B_i=0.\nThe device has M cords numbered 1 to M. If we cut Cord j, the states of all the bombs planted between the coordinates L_j and R_j (inclusive) will be switched - from activated to deactivated, and vice versa.\nDetermine whether it is possible to deactivate all the bombs at the same time. If the answer is yes, output a set of cords that should be cut.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq A_i \\leq 10^9\\ (1 \\leq i \\leq N)\n - A_i are pairwise distinct.\n - B_i is 0 or 1. (1 \\leq i \\leq N)\n - 1 \\leq M \\leq 2 \\times 10^5\n - 1 \\leq L_j \\leq R_j \\leq 10^9\\ (1 \\leq j \\leq M)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 B_1\n:\nA_N B_N\nL_1 R_1\n:\nL_M R_M\n\n-----Output-----\nIf it is impossible to deactivate all the bombs at the same time, print -1. If it is possible to do so, print a set of cords that should be cut, as follows:\nk\nc_1 c_2 \\dots c_k\n\nHere, k is the number of cords (possibly 0), and c_1, c_2, \\dots, c_k represent the cords that should be cut. 1 \\leq c_1 < c_2 < \\dots < c_k \\leq M must hold.\n\n-----Sample Input-----\n3 4\n5 1\n10 1\n8 0\n1 10\n4 5\n6 7\n8 9\n\n-----Sample Output-----\n2\n1 4\n\nThere are two activated bombs at the coordinates 5, 10, and one deactivated bomb at the coordinate 8.\nCutting Cord 1 switches the states of all the bombs planted between the coordinates 1 and 10, that is, all of the three bombs.\nCutting Cord 4 switches the states of all the bombs planted between the coordinates 8 and 9, that is, Bomb 3.\nThus, we can deactivate all the bombs by cutting Cord 1 and Cord 4."} +{"id": "02375", "text": "Alice and Brown loves games. Today, they will play the following game.\nIn this game, there are two piles initially consisting of X and Y stones, respectively.\nAlice and Bob alternately perform the following operation, starting from Alice:\n - Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1\u2264i) can be freely chosen as long as there is a sufficient number of stones in the pile.\nThe player who becomes unable to perform the operation, loses the game.\nGiven X and Y, determine the winner of the game, assuming that both players play optimally.\n\n-----Constraints-----\n - 0 \u2264 X, Y \u2264 10^{18}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y\n\n-----Output-----\nPrint the winner: either Alice or Brown.\n\n-----Sample Input-----\n2 1\n\n-----Sample Output-----\nBrown\n\nAlice can do nothing but taking two stones from the pile containing two stones. As a result, the piles consist of zero and two stones, respectively. Then, Brown will take the two stones, and the piles will consist of one and zero stones, respectively. Alice will be unable to perform the operation anymore, which means Brown's victory."} +{"id": "02376", "text": "You have N items and a bag of strength W.\nThe i-th item has a weight of w_i and a value of v_i.\nYou will select some of the items and put them in the bag.\nHere, the total weight of the selected items needs to be at most W.\nYour objective is to maximize the total value of the selected items.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 100\n - 1 \u2264 W \u2264 10^9\n - 1 \u2264 w_i \u2264 10^9\n - For each i = 2,3,...,N, w_1 \u2264 w_i \u2264 w_1 + 3.\n - 1 \u2264 v_i \u2264 10^7\n - W, each w_i and v_i are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN W\nw_1 v_1\nw_2 v_2\n:\nw_N v_N\n\n-----Output-----\nPrint the maximum possible total value of the selected items.\n\n-----Sample Input-----\n4 6\n2 1\n3 4\n4 10\n3 4\n\n-----Sample Output-----\n11\n\nThe first and third items should be selected."} +{"id": "02377", "text": "You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, \u2026, Katana N, and can perform the following two kinds of attacks in any order:\n - Wield one of the katana you have. When you wield Katana i (1 \u2264 i \u2264 N), the monster receives a_i points of damage. The same katana can be wielded any number of times.\n - Throw one of the katana you have. When you throw Katana i (1 \u2264 i \u2264 N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.\nThe monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?\n\n-----Constraints-----\n - 1 \u2264 N \u2264 10^5\n - 1 \u2264 H \u2264 10^9\n - 1 \u2264 a_i \u2264 b_i \u2264 10^9\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN H\na_1 b_1\n:\na_N b_N\n\n-----Output-----\nPrint the minimum total number of attacks required to vanish the monster.\n\n-----Sample Input-----\n1 10\n3 5\n\n-----Sample Output-----\n3\n\nYou have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster."} +{"id": "02378", "text": "Given is a tree T with N vertices. The i-th edge connects Vertex A_i and B_i (1 \\leq A_i,B_i \\leq N).\nNow, each vertex is painted black with probability 1/2 and white with probability 1/2, which is chosen independently from other vertices. Then, let S be the smallest subtree (connected subgraph) of T containing all the vertices painted black. (If no vertex is painted black, S is the empty graph.)\nLet the holeyness of S be the number of white vertices contained in S. Find the expected holeyness of S.\nSince the answer is a rational number, we ask you to print it \\bmod 10^9+7, as described in Notes.\n\n-----Notes-----\nWhen you print a rational number, first write it as a fraction \\frac{y}{x}, where x, y are integers, and x is not divisible by 10^9 + 7\n(under the constraints of the problem, such representation is always possible).\nThen, you need to print the only integer z between 0 and 10^9 + 6, inclusive, that satisfies xz \\equiv y \\pmod{10^9 + 7}.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq A_i,B_i \\leq N\n - The given graph is a tree.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 B_1\n:\nA_{N-1} B_{N-1}\n\n-----Output-----\nPrint the expected holeyness of S, \\bmod 10^9+7.\n\n-----Sample Input-----\n3\n1 2\n2 3\n\n-----Sample Output-----\n125000001\n\nIf the vertices 1, 2, 3 are painted black, white, black, respectively, the holeyness of S is 1.\nOtherwise, the holeyness is 0, so the expected holeyness is 1/8.\nSince 8 \\times 125000001 \\equiv 1 \\pmod{10^9+7}, we should print 125000001."} +{"id": "02379", "text": "Takahashi has decided to work on K days of his choice from the N days starting with tomorrow.\nYou are given an integer C and a string S. Takahashi will choose his workdays as follows:\n - After working for a day, he will refrain from working on the subsequent C days.\n - If the i-th character of S is x, he will not work on Day i, where Day 1 is tomorrow, Day 2 is the day after tomorrow, and so on.\nFind all days on which Takahashi is bound to work.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq K \\leq N\n - 0 \\leq C \\leq N\n - The length of S is N.\n - Each character of S is o or x.\n - Takahashi can choose his workdays so that the conditions in Problem Statement are satisfied.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K C\nS\n\n-----Output-----\nPrint all days on which Takahashi is bound to work in ascending order, one per line.\n\n-----Sample Input-----\n11 3 2\nooxxxoxxxoo\n\n-----Sample Output-----\n6\n\nTakahashi is going to work on 3 days out of the 11 days. After working for a day, he will refrain from working on the subsequent 2 days.\nThere are four possible choices for his workdays: Day 1,6,10, Day 1,6,11, Day 2,6,10, and Day 2,6,11.\nThus, he is bound to work on Day 6."} +{"id": "02380", "text": "You have N cards. On the i-th card, an integer A_i is written.\nFor each j = 1, 2, ..., M in this order, you will perform the following operation once:\nOperation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.\nFind the maximum possible sum of the integers written on the N cards after the M operations.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^5\n - 1 \\leq A_i, C_i \\leq 10^9\n - 1 \\leq B_i \\leq N\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 A_2 ... A_N\nB_1 C_1\nB_2 C_2\n\\vdots\nB_M C_M\n\n-----Output-----\nPrint the maximum possible sum of the integers written on the N cards after the M operations.\n\n-----Sample Input-----\n3 2\n5 1 4\n2 3\n1 5\n\n-----Sample Output-----\n14\n\nBy replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result."} +{"id": "02381", "text": "Given are N integers A_1,\\ldots,A_N.\nWe will choose exactly K of these elements. Find the maximum possible product of the chosen elements.\nThen, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).\n\n-----Constraints-----\n - 1 \\leq K \\leq N \\leq 2\\times 10^5\n - |A_i| \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nA_1 \\ldots A_N\n\n-----Output-----\nPrint the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).\n\n-----Sample Input-----\n4 2\n1 2 -3 -4\n\n-----Sample Output-----\n12\n\nThe possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12."} +{"id": "02382", "text": "We have one slime.\nYou can set the health of this slime to any integer value of your choice.\nA slime reproduces every second by spawning another slime that has strictly less health. You can freely choose the health of each new slime. The first reproduction of our slime will happen in one second.\nDetermine if it is possible to set the healths of our first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals a multiset S.\nHere S is a multiset containing 2^N (possibly duplicated) integers: S_1,~S_2,~...,~S_{2^N}.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 18\n - 1 \\leq S_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS_1 S_2 ... S_{2^N}\n\n-----Output-----\nIf it is possible to set the healths of the first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals S, print Yes; otherwise, print No.\n\n-----Sample Input-----\n2\n4 2 3 1\n\n-----Sample Output-----\nYes\n\nWe will show one way to make the multiset of the healths of the slimes that will exist in 2 seconds equal to S.\nFirst, set the health of the first slime to 4.\nBy letting the first slime spawn a slime whose health is 3, the healths of the slimes that exist in 1 second can be 4,~3.\nThen, by letting the first slime spawn a slime whose health is 2, and letting the second slime spawn a slime whose health is 1, the healths of the slimes that exist in 2 seconds can be 4,~3,~2,~1, which is equal to S as multisets."} +{"id": "02383", "text": "We have N bricks arranged in a row from left to right.\nThe i-th brick from the left (1 \\leq i \\leq N) has an integer a_i written on it.\nAmong them, you can break at most N-1 bricks of your choice.\nLet us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \\leq i \\leq K), the i-th of those brick from the left has the integer i written on it.\nFind the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 200000\n - 1 \\leq a_i \\leq N\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.\n\n-----Sample Input-----\n3\n2 1 2\n\n-----Sample Output-----\n1\n\nIf we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied."} +{"id": "02384", "text": "Given is an integer sequence A_1, ..., A_N of length N.\nWe will choose exactly \\left\\lfloor \\frac{N}{2} \\right\\rfloor elements from this sequence so that no two adjacent elements are chosen.\nFind the maximum possible sum of the chosen elements.\nHere \\lfloor x \\rfloor denotes the greatest integer not greater than x.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2\\times 10^5\n - |A_i|\\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 ... A_N\n\n-----Output-----\nPrint the maximum possible sum of the chosen elements.\n\n-----Sample Input-----\n6\n1 2 3 4 5 6\n\n-----Sample Output-----\n12\n\nChoosing 2, 4, and 6 makes the sum 12, which is the maximum possible value."} +{"id": "02385", "text": "We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.\nFor each k=1, ..., N, solve the problem below:\n - Consider writing a number on each vertex in the tree in the following manner:\n - First, write 1 on Vertex k.\n - Then, for each of the numbers 2, ..., N in this order, write the number on the vertex chosen as follows:\n - Choose a vertex that still does not have a number written on it and is adjacent to a vertex with a number already written on it. If there are multiple such vertices, choose one of them at random.\n - Find the number of ways in which we can write the numbers on the vertices, modulo (10^9+7).\n\n-----Constraints-----\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq a_i,b_i \\leq N\n - The given graph is a tree.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 b_1\n:\na_{N-1} b_{N-1}\n\n-----Output-----\nFor each k=1, 2, ..., N in this order, print a line containing the answer to the problem.\n\n-----Sample Input-----\n3\n1 2\n1 3\n\n-----Sample Output-----\n2\n1\n1\n\nThe graph in this input is as follows:\n\nFor k=1, there are two ways in which we can write the numbers on the vertices, as follows:\n - Writing 1, 2, 3 on Vertex 1, 2, 3, respectively\n - Writing 1, 3, 2 on Vertex 1, 2, 3, respectively"} +{"id": "02386", "text": "Snuke has an integer sequence A of length N.\nHe will freely choose an integer b.\nHere, he will get sad if A_i and b+i are far from each other.\nMore specifically, the sadness of Snuke is calculated as follows:\n - abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))\nHere, abs(x) is a function that returns the absolute value of x.\nFind the minimum possible sadness of Snuke.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq A_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the minimum possible sadness of Snuke.\n\n-----Sample Input-----\n5\n2 2 3 5 5\n\n-----Sample Output-----\n2\n\nIf we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.\nAny choice of b does not make the sadness of Snuke less than 2, so the answer is 2."} +{"id": "02387", "text": "A bracket sequence is a string that is one of the following:\n - An empty string;\n - The concatenation of (, A, and ) in this order, for some bracket sequence A ;\n - The concatenation of A and B in this order, for some non-empty bracket sequences A and B /\nGiven are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^6\n - The total length of the strings S_i is at most 10^6.\n - S_i is a non-empty string consisting of ( and ).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS_1\n:\nS_N\n\n-----Output-----\nIf a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.\n\n-----Sample Input-----\n2\n)\n(()\n\n-----Sample Output-----\nYes\n\nConcatenating (() and ) in this order forms a bracket sequence."} +{"id": "02388", "text": "There are N robots numbered 1 to N placed on a number line. Robot i is placed at coordinate X_i. When activated, it will travel the distance of D_i in the positive direction, and then it will be removed from the number line. All the robots move at the same speed, and their sizes are ignorable.\nTakahashi, who is a mischievous boy, can do the following operation any number of times (possibly zero) as long as there is a robot remaining on the number line.\n - Choose a robot and activate it. This operation cannot be done when there is a robot moving.\nWhile Robot i is moving, if it touches another robot j that is remaining in the range [X_i, X_i + D_i) on the number line, Robot j also gets activated and starts moving. This process is repeated recursively.\nHow many possible sets of robots remaining on the number line are there after Takahashi does the operation some number of times? Compute this count modulo 998244353, since it can be enormous.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - -10^9 \\leq X_i \\leq 10^9\n - 1 \\leq D_i \\leq 10^9\n - X_i \\neq X_j (i \\neq j)\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nX_1 D_1\n:\nX_N D_N\n\n-----Output-----\nPrint the number of possible sets of robots remaining on the number line, modulo 998244353.\n\n-----Sample Input-----\n2\n1 5\n3 3\n\n-----Sample Output-----\n3\n\nThere are three possible sets of robots remaining on the number line: \\{1, 2\\}, \\{1\\}, and \\{\\}.\nThese can be achieved as follows:\n - If Takahashi activates nothing, the robots \\{1, 2\\} will remain.\n - If Takahashi activates Robot 1, it will activate Robot 2 while moving, after which there will be no robots on the number line. This state can also be reached by activating Robot 2 and then Robot 1.\n - If Takahashi activates Robot 2 and finishes doing the operation, the robot \\{1\\} will remain."} +{"id": "02389", "text": "There is a game that involves three variables, denoted A, B, and C.\nAs the game progresses, there will be N events where you are asked to make a choice.\nEach of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.\nAfter each choice, none of A, B, and C should be negative.\nDetermine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - 0 \\leq A,B,C \\leq 10^9\n - N, A, B, C are integers.\n - s_i is AB, AC, or BC.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN A B C\ns_1\ns_2\n:\ns_N\n\n-----Output-----\nIf it is possible to make N choices under the condition, print Yes; otherwise, print No.\nAlso, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.\n\n-----Sample Input-----\n2 1 3 0\nAB\nAC\n\n-----Sample Output-----\nYes\nA\nC\n\nYou can successfully make two choices, as follows:\n - In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.\n - In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1."} +{"id": "02390", "text": "\"Teishi-zushi\", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.\nNakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.\nNakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.\nWhenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 10^5\n - 2 \u2264 C \u2264 10^{14}\n - 1 \u2264 x_1 < x_2 < ... < x_N < C\n - 1 \u2264 v_i \u2264 10^9\n - All values in input are integers.\n\n-----Subscores-----\n - 300 points will be awarded for passing the test set satisfying N \u2264 100.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN C\nx_1 v_1\nx_2 v_2\n:\nx_N v_N\n\n-----Output-----\nIf Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.\n\n-----Sample Input-----\n3 20\n2 80\n9 120\n16 1\n\n-----Sample Output-----\n191\n\nThere are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value."} +{"id": "02391", "text": "Given are two sequences a=\\{a_0,\\ldots,a_{N-1}\\} and b=\\{b_0,\\ldots,b_{N-1}\\} of N non-negative integers each.\nSnuke will choose an integer k such that 0 \\leq k < N and an integer x not less than 0, to make a new sequence of length N, a'=\\{a_0',\\ldots,a_{N-1}'\\}, as follows:\n - a_i'= a_{i+k \\mod N}\\ XOR \\ x\nFind all pairs (k,x) such that a' will be equal to b.What is \\mbox{ XOR }?\n\nThe XOR of integers A and B, A \\mbox{ XOR } B, is defined as follows:\n\n - When A \\mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.\nFor example, 3 \\mbox{ XOR } 5 = 6. (In base two: 011 \\mbox{ XOR } 101 = 110.)\n\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - 0 \\leq a_i,b_i < 2^{30}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_0 a_1 ... a_{N-1}\nb_0 b_1 ... b_{N-1}\n\n-----Output-----\nPrint all pairs (k, x) such that a' and b will be equal, using one line for each pair, in ascending order of k (ascending order of x for pairs with the same k).\nIf there are no such pairs, the output should be empty.\n\n-----Sample Input-----\n3\n0 2 1\n1 2 3\n\n-----Sample Output-----\n1 3\n\nIf (k,x)=(1,3),\n - a_0'=(a_1\\ XOR \\ 3)=1\n - a_1'=(a_2\\ XOR \\ 3)=2\n - a_2'=(a_0\\ XOR \\ 3)=3\nand we have a' = b."} +{"id": "02392", "text": "Chef loves arrays. But he really loves a specific kind of them - Rainbow Arrays. \nThe array is a Rainbow Array if it has such a structure:\n- The first a1 elements equal to 1. \n- The next a2 elements equal to 2. \n- The next a3 elements equal to 3. \n- The next a4 elements equal to 4. \n- The next a5 elements equal to 5. \n- The next a6 elements equal to 6. \n- The next a7 elements equal to 7. \n- The next a6 elements equal to 6. \n- The next a5 elements equal to 5. \n- The next a4 elements equal to 4. \n- The next a3 elements equal to 3. \n- The next a2 elements equal to 2. \n- The next a1 elements equal to 1. \n- ai is a positive integer, the variables with the same index (a1 in the first statement and a1 in the last one, for example) are equal. \n- There are no any other elements in array. \n\nFor example, {1,1,2,2,2,3,4,5,5,6,7,7,7,6,5,5,4,3,2,2,2,1,1} is a Rainbow Array.\n\nThe array {1,2,3,4,5,6,7,6,6,5,4,3,2,1} is not a Rainbow Array, because the sizes of the blocks with the element 6 are different. \nPlease help Chef to count the number of different Rainbow Arrays that contain exactly N elements. \n\n-----Input-----\nThe first line contains a single integer N. \n\n-----Output-----\nOutput the number of different Rainbow Arrays with N elements, modulo 10^9+7. \n\n-----Constraints-----\n- 1 \u2264 N \u2264 106\n\n-----Example-----\nInput #1:\n10 \n\nOutput #1:\n0\n\nInput #2:\n13\n\nOutput #2:\n1\n\nInput #3:\n14\n\nOutput #3:\n1\n\nInput #4:\n15\n\nOutput #4:\n7"} +{"id": "02393", "text": "You are given a non-empty string $s=s_1s_2\\dots s_n$, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string \"one\" or at least one string \"two\" (or both at the same time) as a substring. In other words, Polycarp does not like the string $s$ if there is an integer $j$ ($1 \\le j \\le n-2$), that $s_{j}s_{j+1}s_{j+2}=$\"one\" or $s_{j}s_{j+1}s_{j+2}=$\"two\".\n\nFor example:\n\n Polycarp does not like strings \"oneee\", \"ontwow\", \"twone\" and \"oneonetwo\" (they all have at least one substring \"one\" or \"two\"), Polycarp likes strings \"oonnee\", \"twwwo\" and \"twnoe\" (they have no substrings \"one\" and \"two\"). \n\nPolycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time.\n\nFor example, if the string looks like $s=$\"onetwone\", then if Polycarp selects two indices $3$ and $6$, then \"onetwone\" will be selected and the result is \"ontwne\".\n\nWhat is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be?\n\n\n-----Input-----\n\nThe first line of the input contains an integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases in the input. Next, the test cases are given.\n\nEach test case consists of one non-empty string $s$. Its length does not exceed $1.5\\cdot10^5$. The string $s$ consists only of lowercase Latin letters.\n\nIt is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed $1.5\\cdot10^6$.\n\n\n-----Output-----\n\nPrint an answer for each test case in the input in order of their appearance.\n\nThe first line of each answer should contain $r$ ($0 \\le r \\le |s|$) \u2014 the required minimum number of positions to be removed, where $|s|$ is the length of the given line. The second line of each answer should contain $r$ different integers \u2014 the indices themselves for removal in any order. Indices are numbered from left to right from $1$ to the length of the string. If $r=0$, then the second line can be skipped (or you can print empty). If there are several answers, print any of them.\n\n\n-----Examples-----\nInput\n4\nonetwone\ntestme\noneoneone\ntwotwo\n\nOutput\n2\n6 3\n0\n\n3\n4 1 7 \n2\n1 4\n\nInput\n10\nonetwonetwooneooonetwooo\ntwo\none\ntwooooo\nttttwo\nttwwoo\nooone\nonnne\noneeeee\noneeeeeeetwooooo\n\nOutput\n6\n18 11 12 1 6 21 \n1\n1 \n1\n3 \n1\n2 \n1\n6 \n0\n\n1\n4 \n0\n\n1\n1 \n2\n1 11 \n\n\n\n-----Note-----\n\nIn the first example, answers are:\n\n \"onetwone\", \"testme\" \u2014 Polycarp likes it, there is nothing to remove, \"oneoneone\", \"twotwo\". \n\nIn the second example, answers are: \"onetwonetwooneooonetwooo\", \"two\", \"one\", \"twooooo\", \"ttttwo\", \"ttwwoo\" \u2014 Polycarp likes it, there is nothing to remove, \"ooone\", \"onnne\" \u2014 Polycarp likes it, there is nothing to remove, \"oneeeee\", \"oneeeeeeetwooooo\"."} +{"id": "02394", "text": "A tree is an undirected connected graph without cycles. The distance between two vertices is the number of edges in a simple path between them.\n\nLimak is a little polar bear. He lives in a tree that consists of n vertices, numbered 1 through n.\n\nLimak recently learned how to jump. He can jump from a vertex to any vertex within distance at most k.\n\nFor a pair of vertices (s, t) we define f(s, t) as the minimum number of jumps Limak needs to get from s to t. Your task is to find the sum of f(s, t) over all pairs of vertices (s, t) such that s < t.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (2 \u2264 n \u2264 200 000, 1 \u2264 k \u2264 5)\u00a0\u2014 the number of vertices in the tree and the maximum allowed jump distance respectively.\n\nThe next n - 1 lines describe edges in the tree. The i-th of those lines contains two integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n)\u00a0\u2014 the indices on vertices connected with i-th edge.\n\nIt's guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nPrint one integer, denoting the sum of f(s, t) over all pairs of vertices (s, t) such that s < t.\n\n\n-----Examples-----\nInput\n6 2\n1 2\n1 3\n2 4\n2 5\n4 6\n\nOutput\n20\n\nInput\n13 3\n1 2\n3 2\n4 2\n5 2\n3 6\n10 6\n6 7\n6 13\n5 8\n5 9\n9 11\n11 12\n\nOutput\n114\n\nInput\n3 5\n2 1\n3 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample, the given tree has 6 vertices and it's displayed on the drawing below. Limak can jump to any vertex within distance at most 2. For example, from the vertex 5 he can jump to any of vertices: 1, 2 and 4 (well, he can also jump to the vertex 5 itself). [Image] \n\nThere are $\\frac{n \\cdot(n - 1)}{2} = 15$ pairs of vertices (s, t) such that s < t. For 5 of those pairs Limak would need two jumps: (1, 6), (3, 4), (3, 5), (3, 6), (5, 6). For other 10 pairs one jump is enough. So, the answer is 5\u00b72 + 10\u00b71 = 20.\n\nIn the third sample, Limak can jump between every two vertices directly. There are 3 pairs of vertices (s < t), so the answer is 3\u00b71 = 3."} +{"id": "02395", "text": "Let's say string $s$ has period $k$ if $s_i = s_{i + k}$ for all $i$ from $1$ to $|s| - k$ ($|s|$ means length of string $s$) and $k$ is the minimum positive integer with this property.\n\nSome examples of a period: for $s$=\"0101\" the period is $k=2$, for $s$=\"0000\" the period is $k=1$, for $s$=\"010\" the period is $k=2$, for $s$=\"0011\" the period is $k=4$.\n\nYou are given string $t$ consisting only of 0's and 1's and you need to find such string $s$ that: String $s$ consists only of 0's and 1's; The length of $s$ doesn't exceed $2 \\cdot |t|$; String $t$ is a subsequence of string $s$; String $s$ has smallest possible period among all strings that meet conditions 1\u20143. \n\nLet us recall that $t$ is a subsequence of $s$ if $t$ can be derived from $s$ by deleting zero or more elements (any) without changing the order of the remaining elements. For example, $t$=\"011\" is a subsequence of $s$=\"10101\".\n\n\n-----Input-----\n\nThe first line contains single integer $T$ ($1 \\le T \\le 100$)\u00a0\u2014 the number of test cases.\n\nNext $T$ lines contain test cases \u2014 one per line. Each line contains string $t$ ($1 \\le |t| \\le 100$) consisting only of 0's and 1's.\n\n\n-----Output-----\n\nPrint one string for each test case \u2014 string $s$ you needed to find. If there are multiple solutions print any one of them.\n\n\n-----Example-----\nInput\n4\n00\n01\n111\n110\n\nOutput\n00\n01\n11111\n1010\n\n\n-----Note-----\n\nIn the first and second test cases, $s = t$ since it's already one of the optimal solutions. Answers have periods equal to $1$ and $2$, respectively.\n\nIn the third test case, there are shorter optimal solutions, but it's okay since we don't need to minimize the string $s$. String $s$ has period equal to $1$."} +{"id": "02396", "text": "The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form $\\frac{a + b}{c}$.\n\nTo plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!\n\n\n-----Input-----\n\nThe first line of the input contains a single integer m (1 \u2264 m \u2264 200 000) \u2013 the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.\n\n\n-----Output-----\n\nPrint a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).\n\n\n-----Example-----\nInput\n4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7\n\nOutput\n1 2 2 1 \n\n\n-----Note-----\n\nIn the sample testcase, the second and the third ship will both end up at the coordinate 3.\n\nNote that this problem has only two versions \u2013 easy and hard."} +{"id": "02397", "text": "You are given an array $a_1, a_2, \\dots, a_n$ and an integer $k$.\n\nYou are asked to divide this array into $k$ non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let $f(i)$ be the index of subarray the $i$-th element belongs to. Subarrays are numbered from left to right and from $1$ to $k$.\n\nLet the cost of division be equal to $\\sum\\limits_{i=1}^{n} (a_i \\cdot f(i))$. For example, if $a = [1, -2, -3, 4, -5, 6, -7]$ and we divide it into $3$ subbarays in the following way: $[1, -2, -3], [4, -5], [6, -7]$, then the cost of division is equal to $1 \\cdot 1 - 2 \\cdot 1 - 3 \\cdot 1 + 4 \\cdot 2 - 5 \\cdot 2 + 6 \\cdot 3 - 7 \\cdot 3 = -9$.\n\nCalculate the maximum cost you can obtain by dividing the array $a$ into $k$ non-empty consecutive subarrays. \n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 3 \\cdot 10^5$).\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($ |a_i| \\le 10^6$). \n\n\n-----Output-----\n\nPrint the maximum cost you can obtain by dividing the array $a$ into $k$ nonempty consecutive subarrays. \n\n\n-----Examples-----\nInput\n5 2\n-1 -2 5 -4 8\n\nOutput\n15\n\nInput\n7 6\n-3 0 -1 -2 -2 -4 -1\n\nOutput\n-45\n\nInput\n4 1\n3 -1 6 0\n\nOutput\n8"} +{"id": "02398", "text": "Alice has a cute cat. To keep her cat fit, Alice wants to design an exercising walk for her cat! \n\nInitially, Alice's cat is located in a cell $(x,y)$ of an infinite grid. According to Alice's theory, cat needs to move: exactly $a$ steps left: from $(u,v)$ to $(u-1,v)$; exactly $b$ steps right: from $(u,v)$ to $(u+1,v)$; exactly $c$ steps down: from $(u,v)$ to $(u,v-1)$; exactly $d$ steps up: from $(u,v)$ to $(u,v+1)$. \n\nNote that the moves can be performed in an arbitrary order. For example, if the cat has to move $1$ step left, $3$ steps right and $2$ steps down, then the walk right, down, left, right, right, down is valid.\n\nAlice, however, is worrying that her cat might get lost if it moves far away from her. So she hopes that her cat is always in the area $[x_1,x_2]\\times [y_1,y_2]$, i.e. for every cat's position $(u,v)$ of a walk $x_1 \\le u \\le x_2$ and $y_1 \\le v \\le y_2$ holds.\n\nAlso, note that the cat can visit the same cell multiple times.\n\nCan you help Alice find out if there exists a walk satisfying her wishes?\n\nFormally, the walk should contain exactly $a+b+c+d$ unit moves ($a$ to the left, $b$ to the right, $c$ to the down, $d$ to the up). Alice can do the moves in any order. Her current position $(u, v)$ should always satisfy the constraints: $x_1 \\le u \\le x_2$, $y_1 \\le v \\le y_2$. The staring point is $(x, y)$.\n\nYou are required to answer $t$ test cases independently.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^3$) \u2014 the number of testcases. \n\nThe first line of each test case contains four integers $a$, $b$, $c$, $d$ ($0 \\le a,b,c,d \\le 10^8$, $a+b+c+d \\ge 1$).\n\nThe second line of the test case contains six integers $x$, $y$, $x_1$, $y_1$, $x_2$, $y_2$ ($-10^8 \\le x_1\\le x \\le x_2 \\le 10^8$, $-10^8 \\le y_1 \\le y \\le y_2 \\le 10^8$).\n\n\n-----Output-----\n\nFor each test case, output \"YES\" in a separate line, if there exists a walk satisfying her wishes. Otherwise, output \"NO\" in a separate line. \n\nYou can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n6\n3 2 2 2\n0 0 -2 -2 2 2\n3 1 4 1\n0 0 -1 -1 1 1\n1 1 1 1\n1 1 1 1 1 1\n0 0 0 1\n0 0 0 0 0 1\n5 1 1 1\n0 0 -100 -100 0 100\n1 1 5 1\n0 0 -100 -100 100 0\n\nOutput\nYes\nNo\nNo\nYes\nYes\nYes\n\n\n\n-----Note-----\n\nIn the first test case, one valid exercising walk is $$(0,0)\\rightarrow (-1,0) \\rightarrow (-2,0)\\rightarrow (-2,1) \\rightarrow (-2,2)\\rightarrow (-1,2)\\rightarrow(0,2)\\rightarrow (0,1)\\rightarrow (0,0) \\rightarrow (-1,0)$$"} +{"id": "02399", "text": "Alice and Bob play a game. Initially they have a string $s_1, s_2, \\dots, s_n$, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length $a$, and Bob must select a substring of length $b$. It is guaranteed that $a > b$.\n\nFor example, if $s =$ ...X.. and $a = 3$, $b = 2$, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string $s =$ ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.\n\nWhoever is unable to make a move, loses. You have to determine who wins if they both play optimally.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line contains one integer $q$ ($1 \\le q \\le 3 \\cdot 10^5$) \u2014 the number of queries.\n\nThe first line of each query contains two integers $a$ and $b$ ($1 \\le b < a \\le 3 \\cdot 10^5$).\n\nThe second line of each query contains the string $s$ ($1 \\le |s| \\le 3 \\cdot 10^5$), consisting of only characters . and X.\n\nIt is guaranteed that sum of all $|s|$ over all queries not exceed $3 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case print YES if Alice can win and NO otherwise.\n\nYou may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).\n\n\n-----Example-----\nInput\n3\n3 2\nXX......XX...X\n4 2\nX...X.X..X\n5 3\n.......X..X\n\nOutput\nYES\nNO\nYES\n\n\n\n-----Note-----\n\nIn the first query Alice can select substring $s_3 \\dots s_5$. After that $s$ turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.\n\nIn the second query Alice can not win because she cannot even make one move.\n\nIn the third query Alice can choose substring $s_2 \\dots s_6$. After that $s$ turns into .XXXXX.X..X, and Bob can't make a move after that."} +{"id": "02400", "text": "DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew $n$ distinct lines, given by equations $y = x + p_i$ for some distinct $p_1, p_2, \\ldots, p_n$.\n\nThen JLS drew on the same paper sheet $m$ distinct lines given by equations $y = -x + q_i$ for some distinct $q_1, q_2, \\ldots, q_m$.\n\nDLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 1000$), the number of test cases in the input. Then follow the test case descriptions.\n\nThe first line of a test case contains an integer $n$ ($1 \\le n \\le 10^5$), the number of lines drawn by DLS.\n\nThe second line of a test case contains $n$ distinct integers $p_i$ ($0 \\le p_i \\le 10^9$) describing the lines drawn by DLS. The integer $p_i$ describes a line given by the equation $y = x + p_i$.\n\nThe third line of a test case contains an integer $m$ ($1 \\le m \\le 10^5$), the number of lines drawn by JLS.\n\nThe fourth line of a test case contains $m$ distinct integers $q_i$ ($0 \\le q_i \\le 10^9$) describing the lines drawn by JLS. The integer $q_i$ describes a line given by the equation $y = -x + q_i$.\n\nThe sum of the values of $n$ over all test cases in the input does not exceed $10^5$. Similarly, the sum of the values of $m$ over all test cases in the input does not exceed $10^5$.\n\nIn hacks it is allowed to use only one test case in the input, so $t=1$ should be satisfied.\n\n\n-----Output-----\n\nFor each test case in the input print a single integer\u00a0\u2014 the number of line pairs with integer intersection points. \n\n\n-----Example-----\nInput\n3\n3\n1 3 2\n2\n0 3\n1\n1\n1\n1\n1\n2\n1\n1\n\nOutput\n3\n1\n0\n\n\n\n-----Note-----\n\nThe picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates. [Image]"} +{"id": "02401", "text": "Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the N attendees love to play a very risky game...\n\nEvery zombie gets a number n_{i} (1 \u2264 n_{i} \u2264 N) written on his forehead. Although no zombie can see his own number, he can see the numbers written on the foreheads of all N - 1 fellows. Note that not all numbers have to be unique (they can even all be the same). From this point on, no more communication between zombies is allowed. Observation is the only key to success. When the cuckoo clock strikes midnight, all attendees have to simultaneously guess the number on their own forehead. If at least one of them guesses his number correctly, all zombies survive and go home happily. On the other hand, if not a single attendee manages to guess his number correctly, all of them are doomed to die!\n\nZombies aren't very bright creatures though, and Heidi has to act fast if she does not want to jeopardize her life. She has one single option: by performing some quick surgery on the brain she managed to get from the chest, she has the ability to remotely reprogram the decision-making strategy of all attendees for their upcoming midnight game! Can you suggest a sound strategy to Heidi which, given the rules of the game, ensures that at least one attendee will guess his own number correctly, for any possible sequence of numbers on the foreheads?\n\nGiven a zombie's rank R and the N - 1 numbers n_{i} on the other attendees' foreheads, your program will have to return the number that the zombie of rank R shall guess. Those answers define your strategy, and we will check if it is flawless or not.\n\n\n-----Input-----\n\nThe first line of input contains a single integer T (1 \u2264 T \u2264 50000): the number of scenarios for which you have to make a guess.\n\nThe T scenarios follow, described on two lines each: The first line holds two integers, N (2 \u2264 N \u2264 6), the number of attendees, and R (1 \u2264 R \u2264 N), the rank of the zombie who has to make the guess. The second line lists N - 1 integers: the numbers on the foreheads of all other attendees, listed in increasing order of the attendees' rank. (Every zombie knows the rank of every other zombie.) \n\n\n-----Output-----\n\nFor every scenario, output a single integer: the number that the zombie of rank R shall guess, based on the numbers n_{i} on his N - 1 fellows' foreheads.\n\n\n-----Examples-----\nInput\n4\n2 1\n1\n2 2\n1\n2 1\n2\n2 2\n2\n\nOutput\n1\n2\n2\n1\n\nInput\n2\n5 2\n2 2 2 2\n6 4\n3 2 6 1 2\n\nOutput\n5\n2\n\n\n\n-----Note-----\n\nFor instance, if there were N = 2 two attendees, a successful strategy could be: The zombie of rank 1 always guesses the number he sees on the forehead of the zombie of rank 2. The zombie of rank 2 always guesses the opposite of the number he sees on the forehead of the zombie of rank 1."} +{"id": "02402", "text": "Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $n$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.\n\nSuppose in the first round participant A took $x$-th place and in the second round\u00a0\u2014 $y$-th place. Then the total score of the participant A is sum $x + y$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $i$ from $1$ to $n$ exactly one participant took $i$-th place in first round and exactly one participant took $i$-th place in second round.\n\nRight after the end of the Olympiad, Nikolay was informed that he got $x$-th place in first round and $y$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question.\n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\le t \\le 100$)\u00a0\u2014 the number of test cases to solve.\n\nEach of the following $t$ lines contains integers $n$, $x$, $y$ ($1 \\leq n \\leq 10^9$, $1 \\le x, y \\le n$)\u00a0\u2014 the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round.\n\n\n-----Output-----\n\nPrint two integers\u00a0\u2014 the minimum and maximum possible overall place Nikolay could take.\n\n\n-----Examples-----\nInput\n1\n5 1 3\n\nOutput\n1 3\n\nInput\n1\n6 3 4\n\nOutput\n2 6\n\n\n\n-----Note-----\n\nExplanation for the first example:\n\nSuppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: $\\left. \\begin{array}{|c|c|c|c|c|} \\hline \\text{Participant} & {\\text{Round 1}} & {\\text{Round 2}} & {\\text{Total score}} & {\\text{Place}} \\\\ \\hline A & {1} & {3} & {4} & {1} \\\\ \\hline B & {2} & {4} & {6} & {3} \\\\ \\hline C & {3} & {5} & {8} & {5} \\\\ \\hline D & {4} & {1} & {5} & {2} \\\\ \\hline E & {5} & {2} & {7} & {4} \\\\ \\hline \\end{array} \\right.$ \n\nHowever, the results of the Olympiad could also look like this: $\\left. \\begin{array}{|c|c|c|c|c|} \\hline \\text{Participant} & {\\text{Round 1}} & {\\text{Round 2}} & {\\text{Total score}} & {\\text{Place}} \\\\ \\hline A & {1} & {3} & {4} & {3} \\\\ \\hline B & {2} & {2} & {4} & {3} \\\\ \\hline C & {3} & {1} & {4} & {3} \\\\ \\hline D & {4} & {4} & {8} & {4} \\\\ \\hline E & {5} & {5} & {10} & {5} \\\\ \\hline \\end{array} \\right.$ \n\nIn the first case Nikolay would have taken first place, and in the second\u00a0\u2014 third place."} +{"id": "02403", "text": "Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...\n\nThere are two arrays $a$ and $b$ of length $n$. Initially, an $ans$ is equal to $0$ and the following operation is defined: Choose position $i$ ($1 \\le i \\le n$); Add $a_i$ to $ans$; If $b_i \\neq -1$ then add $a_i$ to $a_{b_i}$. \n\nWhat is the maximum $ans$ you can get by performing the operation on each $i$ ($1 \\le i \\le n$) exactly once?\n\nUncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.\n\n\n-----Input-----\n\nThe first line contains the integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$)\u00a0\u2014 the length of arrays $a$ and $b$.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($\u221210^6 \\le a_i \\le 10^6$).\n\nThe third line contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($1 \\le b_i \\le n$ or $b_i = -1$).\n\nAdditional constraint: it's guaranteed that for any $i$ ($1 \\le i \\le n$) the sequence $b_i, b_{b_i}, b_{b_{b_i}}, \\ldots$ is not cyclic, in other words it will always end with $-1$.\n\n\n-----Output-----\n\nIn the first line, print the maximum $ans$ you can get.\n\nIn the second line, print the order of operations: $n$ different integers $p_1, p_2, \\ldots, p_n$ ($1 \\le p_i \\le n$). The $p_i$ is the position which should be chosen at the $i$-th step. If there are multiple orders, print any of them.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n2 3 -1\n\nOutput\n10\n1 2 3 \n\nInput\n2\n-1 100\n2 -1\n\nOutput\n99\n2 1 \n\nInput\n10\n-10 -1 2 2 5 -2 -3 -4 2 -6\n-1 -1 2 2 -1 5 5 7 7 9\n\nOutput\n-9\n3 5 6 1 9 4 10 7 8 2"} +{"id": "02404", "text": "There was once young lass called Mary, \n\nWhose jokes were occasionally scary. \n\nOn this April's Fool \n\nFixed limerick rules \n\nAllowed her to trip the unwary.\n\n\n\nCan she fill all the lines\n\nTo work at all times?\n\nOn juggling the words\n\nRight around two-thirds\n\nShe nearly ran out of rhymes.\n\n\n\n\n-----Input-----\n\nThe input contains a single integer $a$ ($4 \\le a \\le 998$). Not every integer in the range is a valid input for the problem; you are guaranteed that the input will be a valid integer.\n\n\n-----Output-----\n\nOutput a single number.\n\n\n-----Examples-----\nInput\n35\n\nOutput\n57\n\nInput\n57\n\nOutput\n319\n\nInput\n391\n\nOutput\n1723"} +{"id": "02405", "text": "A factory produces thimbles in bulk. Typically, it can produce up to a thimbles a day. However, some of the machinery is defective, so it can currently only produce b thimbles each day. The factory intends to choose a k-day period to do maintenance and construction; it cannot produce any thimbles during this time, but will be restored to its full production of a thimbles per day after the k days are complete.\n\nInitially, no orders are pending. The factory receives updates of the form d_{i}, a_{i}, indicating that a_{i} new orders have been placed for the d_{i}-th day. Each order requires a single thimble to be produced on precisely the specified day. The factory may opt to fill as many or as few of the orders in a single batch as it likes.\n\nAs orders come in, the factory owner would like to know the maximum number of orders he will be able to fill if he starts repairs on a given day p_{i}. Help the owner answer his questions.\n\n\n-----Input-----\n\nThe first line contains five integers n, k, a, b, and q (1 \u2264 k \u2264 n \u2264 200 000, 1 \u2264 b < a \u2264 10 000, 1 \u2264 q \u2264 200 000)\u00a0\u2014 the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively.\n\nThe next q lines contain the descriptions of the queries. Each query is of one of the following two forms: 1 d_{i} a_{i} (1 \u2264 d_{i} \u2264 n, 1 \u2264 a_{i} \u2264 10 000), representing an update of a_{i} orders on day d_{i}, or 2 p_{i} (1 \u2264 p_{i} \u2264 n - k + 1), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day p_{i}? \n\nIt's guaranteed that the input will contain at least one query of the second type.\n\n\n-----Output-----\n\nFor each query of the second type, print a line containing a single integer \u2014 the maximum number of orders that the factory can fill over all n days.\n\n\n-----Examples-----\nInput\n5 2 2 1 8\n1 1 2\n1 5 3\n1 2 1\n2 2\n1 4 2\n1 3 2\n2 1\n2 3\n\nOutput\n3\n6\n4\n\nInput\n5 4 10 1 6\n1 1 5\n1 5 5\n1 3 2\n1 5 2\n2 1\n2 2\n\nOutput\n7\n1\n\n\n\n-----Note-----\n\nConsider the first sample.\n\nWe produce up to 1 thimble a day currently and will produce up to 2 thimbles a day after repairs. Repairs take 2 days.\n\nFor the first question, we are able to fill 1 order on day 1, no orders on days 2 and 3 since we are repairing, no orders on day 4 since no thimbles have been ordered for that day, and 2 orders for day 5 since we are limited to our production capacity, for a total of 3 orders filled.\n\nFor the third question, we are able to fill 1 order on day 1, 1 order on day 2, and 2 orders on day 5, for a total of 4 orders."} +{"id": "02406", "text": "Omkar is standing at the foot of Celeste mountain. The summit is $n$ meters away from him, and he can see all of the mountains up to the summit, so for all $1 \\leq j \\leq n$ he knows that the height of the mountain at the point $j$ meters away from himself is $h_j$ meters. It turns out that for all $j$ satisfying $1 \\leq j \\leq n - 1$, $h_j < h_{j + 1}$ (meaning that heights are strictly increasing).\n\nSuddenly, a landslide occurs! While the landslide is occurring, the following occurs: every minute, if $h_j + 2 \\leq h_{j + 1}$, then one square meter of dirt will slide from position $j + 1$ to position $j$, so that $h_{j + 1}$ is decreased by $1$ and $h_j$ is increased by $1$. These changes occur simultaneously, so for example, if $h_j + 2 \\leq h_{j + 1}$ and $h_{j + 1} + 2 \\leq h_{j + 2}$ for some $j$, then $h_j$ will be increased by $1$, $h_{j + 2}$ will be decreased by $1$, and $h_{j + 1}$ will be both increased and decreased by $1$, meaning that in effect $h_{j + 1}$ is unchanged during that minute.\n\nThe landslide ends when there is no $j$ such that $h_j + 2 \\leq h_{j + 1}$. Help Omkar figure out what the values of $h_1, \\dots, h_n$ will be after the landslide ends. It can be proven that under the given constraints, the landslide will always end in finitely many minutes.\n\nNote that because of the large amount of input, it is recommended that your code uses fast IO.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 10^6$). \n\nThe second line contains $n$ integers $h_1, h_2, \\dots, h_n$ satisfying $0 \\leq h_1 < h_2 < \\dots < h_n \\leq 10^{12}$\u00a0\u2014 the heights.\n\n\n-----Output-----\n\nOutput $n$ integers, where the $j$-th integer is the value of $h_j$ after the landslide has stopped.\n\n\n-----Example-----\nInput\n4\n2 6 7 8\n\nOutput\n5 5 6 7\n\n\n\n-----Note-----\n\nInitially, the mountain has heights $2, 6, 7, 8$.\n\nIn the first minute, we have $2 + 2 \\leq 6$, so $2$ increases to $3$ and $6$ decreases to $5$, leaving $3, 5, 7, 8$.\n\nIn the second minute, we have $3 + 2 \\leq 5$ and $5 + 2 \\leq 7$, so $3$ increases to $4$, $5$ is unchanged, and $7$ decreases to $6$, leaving $4, 5, 6, 8$.\n\nIn the third minute, we have $6 + 2 \\leq 8$, so $6$ increases to $7$ and $8$ decreases to $7$, leaving $4, 5, 7, 7$.\n\nIn the fourth minute, we have $5 + 2 \\leq 7$, so $5$ increases to $6$ and $7$ decreases to $6$, leaving $4, 6, 6, 7$.\n\nIn the fifth minute, we have $4 + 2 \\leq 6$, so $4$ increases to $5$ and $6$ decreases to $5$, leaving $5, 5, 6, 7$.\n\nIn the sixth minute, nothing else can change so the landslide stops and our answer is $5, 5, 6, 7$."} +{"id": "02407", "text": "Ivan plays an old action game called Heretic. He's stuck on one of the final levels of this game, so he needs some help with killing the monsters.\n\nThe main part of the level is a large corridor (so large and narrow that it can be represented as an infinite coordinate line). The corridor is divided into two parts; let's assume that the point $x = 0$ is where these parts meet.\n\nThe right part of the corridor is filled with $n$ monsters \u2014 for each monster, its initial coordinate $x_i$ is given (and since all monsters are in the right part, every $x_i$ is positive).\n\nThe left part of the corridor is filled with crusher traps. If some monster enters the left part of the corridor or the origin (so, its current coordinate becomes less than or equal to $0$), it gets instantly killed by a trap.\n\nThe main weapon Ivan uses to kill the monsters is the Phoenix Rod. It can launch a missile that explodes upon impact, obliterating every monster caught in the explosion and throwing all other monsters away from the epicenter. Formally, suppose that Ivan launches a missile so that it explodes in the point $c$. Then every monster is either killed by explosion or pushed away. Let some monster's current coordinate be $y$, then:\n\n if $c = y$, then the monster is killed; if $y < c$, then the monster is pushed $r$ units to the left, so its current coordinate becomes $y - r$; if $y > c$, then the monster is pushed $r$ units to the right, so its current coordinate becomes $y + r$. \n\nIvan is going to kill the monsters as follows: choose some integer point $d$ and launch a missile into that point, then wait until it explodes and all the monsters which are pushed to the left part of the corridor are killed by crusher traps, then, if at least one monster is still alive, choose another integer point (probably the one that was already used) and launch a missile there, and so on.\n\nWhat is the minimum number of missiles Ivan has to launch in order to kill all of the monsters? You may assume that every time Ivan fires the Phoenix Rod, he chooses the impact point optimally.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line contains one integer $q$ ($1 \\le q \\le 10^5$) \u2014 the number of queries.\n\nThe first line of each query contains two integers $n$ and $r$ ($1 \\le n, r \\le 10^5$)\u00a0\u2014 the number of enemies and the distance that the enemies are thrown away from the epicenter of the explosion.\n\nThe second line of each query contains $n$ integers $x_i$ ($1 \\le x_i \\le 10^5$)\u00a0\u2014 the initial positions of the monsters.\n\nIt is guaranteed that sum of all $n$ over all queries does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each query print one integer\u00a0\u2014 the minimum number of shots from the Phoenix Rod required to kill all monsters.\n\n\n-----Example-----\nInput\n2\n3 2\n1 3 5\n4 1\n5 2 3 5\n\nOutput\n2\n2\n\n\n\n-----Note-----\n\nIn the first test case, Ivan acts as follows: choose the point $3$, the first monster dies from a crusher trap at the point $-1$, the second monster dies from the explosion, the third monster is pushed to the point $7$; choose the point $7$, the third monster dies from the explosion. \n\nIn the second test case, Ivan acts as follows: choose the point $5$, the first and fourth monsters die from the explosion, the second monster is pushed to the point $1$, the third monster is pushed to the point $2$; choose the point $2$, the first monster dies from a crusher trap at the point $0$, the second monster dies from the explosion."} +{"id": "02408", "text": "This problem is same as the next one, but has smaller constraints.\n\nIt was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.\n\nAt the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $(x_i, y_i)$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.\n\nSelena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 50$)\u00a0\u2014 the number of electric poles.\n\nEach of the following $n$ lines contains two integers $x_i$, $y_i$ ($-10^4 \\le x_i, y_i \\le 10^4$)\u00a0\u2014 the coordinates of the poles.\n\nIt is guaranteed that all of these $n$ points are distinct.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of pairs of wires that are intersecting.\n\n\n-----Examples-----\nInput\n4\n0 0\n1 1\n0 3\n1 2\n\nOutput\n14\n\nInput\n4\n0 0\n0 2\n0 4\n2 0\n\nOutput\n6\n\nInput\n3\n-1 -1\n1 0\n3 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example: [Image] \n\nIn the second example: [Image] \n\nNote that the three poles $(0, 0)$, $(0, 2)$ and $(0, 4)$ are connected by a single wire.\n\nIn the third example: [Image]"} +{"id": "02409", "text": "The only difference between easy and hard versions is on constraints. In this version constraints are higher. You can make hacks only if all versions of the problem are solved.\n\nKoa the Koala is at the beach!\n\nThe beach consists (from left to right) of a shore, $n+1$ meters of sea and an island at $n+1$ meters from the shore.\n\nShe measured the depth of the sea at $1, 2, \\dots, n$ meters from the shore and saved them in array $d$. $d_i$ denotes the depth of the sea at $i$ meters from the shore for $1 \\le i \\le n$.\n\nLike any beach this one has tide, the intensity of the tide is measured by parameter $k$ and affects all depths from the beginning at time $t=0$ in the following way:\n\n For a total of $k$ seconds, each second, tide increases all depths by $1$.\n\n Then, for a total of $k$ seconds, each second, tide decreases all depths by $1$.\n\n This process repeats again and again (ie. depths increase for $k$ seconds then decrease for $k$ seconds and so on ...).\n\nFormally, let's define $0$-indexed array $p = [0, 1, 2, \\ldots, k - 2, k - 1, k, k - 1, k - 2, \\ldots, 2, 1]$ of length $2k$. At time $t$ ($0 \\le t$) depth at $i$ meters from the shore equals $d_i + p[t \\bmod 2k]$ ($t \\bmod 2k$ denotes the remainder of the division of $t$ by $2k$). Note that the changes occur instantaneously after each second, see the notes for better understanding. \n\nAt time $t=0$ Koa is standing at the shore and wants to get to the island. Suppose that at some time $t$ ($0 \\le t$) she is at $x$ ($0 \\le x \\le n$) meters from the shore:\n\n In one second Koa can swim $1$ meter further from the shore ($x$ changes to $x+1$) or not swim at all ($x$ stays the same), in both cases $t$ changes to $t+1$.\n\n As Koa is a bad swimmer, the depth of the sea at the point where she is can't exceed $l$ at integer points of time (or she will drown). More formally, if Koa is at $x$ ($1 \\le x \\le n$) meters from the shore at the moment $t$ (for some integer $t\\ge 0$), the depth of the sea at this point \u00a0\u2014 $d_x + p[t \\bmod 2k]$ \u00a0\u2014 can't exceed $l$. In other words, $d_x + p[t \\bmod 2k] \\le l$ must hold always.\n\n Once Koa reaches the island at $n+1$ meters from the shore, she stops and can rest.\n\nNote that while Koa swims tide doesn't have effect on her (ie. she can't drown while swimming). Note that Koa can choose to stay on the shore for as long as she needs and neither the shore or the island are affected by the tide (they are solid ground and she won't drown there). \n\nKoa wants to know whether she can go from the shore to the island. Help her!\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u00a0\u2014 the number of test cases. Description of the test cases follows.\n\nThe first line of each test case contains three integers $n$, $k$ and $l$ ($1 \\le n \\le 3 \\cdot 10^5; 1 \\le k \\le 10^9; 1 \\le l \\le 10^9$)\u00a0\u2014 the number of meters of sea Koa measured and parameters $k$ and $l$.\n\nThe second line of each test case contains $n$ integers $d_1, d_2, \\ldots, d_n$ ($0 \\le d_i \\le 10^9$) \u00a0\u2014 the depths of each meter of sea Koa measured.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $3 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case:\n\nPrint Yes if Koa can get from the shore to the island, and No otherwise.\n\nYou may print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n7\n2 1 1\n1 0\n5 2 3\n1 2 3 2 2\n4 3 4\n0 2 4 3\n2 3 5\n3 0\n7 2 3\n3 0 2 1 3 0 1\n7 1 4\n4 4 3 0 2 4 2\n5 2 3\n1 2 3 2 2\n\nOutput\nYes\nNo\nYes\nYes\nYes\nNo\nNo\n\n\n\n-----Note-----\n\nIn the following $s$ denotes the shore, $i$ denotes the island, $x$ denotes distance from Koa to the shore, the underline denotes the position of Koa, and values in the array below denote current depths, affected by tide, at $1, 2, \\dots, n$ meters from the shore.\n\nIn test case $1$ we have $n = 2, k = 1, l = 1, p = [ 0, 1 ]$.\n\nKoa wants to go from shore (at $x = 0$) to the island (at $x = 3$). Let's describe a possible solution:\n\n Initially at $t = 0$ the beach looks like this: $[\\underline{s}, 1, 0, i]$. At $t = 0$ if Koa would decide to swim to $x = 1$, beach would look like: $[s, \\underline{2}, 1, i]$ at $t = 1$, since $2 > 1$ she would drown. So Koa waits $1$ second instead and beach looks like $[\\underline{s}, 2, 1, i]$ at $t = 1$. At $t = 1$ Koa swims to $x = 1$, beach looks like $[s, \\underline{1}, 0, i]$ at $t = 2$. Koa doesn't drown because $1 \\le 1$. At $t = 2$ Koa swims to $x = 2$, beach looks like $[s, 2, \\underline{1}, i]$ at $t = 3$. Koa doesn't drown because $1 \\le 1$. At $t = 3$ Koa swims to $x = 3$, beach looks like $[s, 1, 0, \\underline{i}]$ at $t = 4$. At $t = 4$ Koa is at $x = 3$ and she made it! \n\nWe can show that in test case $2$ Koa can't get to the island."} +{"id": "02410", "text": "Today, Yasser and Adel are at the shop buying cupcakes. There are $n$ cupcake types, arranged from $1$ to $n$ on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type $i$ is an integer $a_i$. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.\n\nYasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.\n\nOn the other hand, Adel will choose some segment $[l, r]$ $(1 \\le l \\le r \\le n)$ that does not include all of cupcakes (he can't choose $[l, r] = [1, n]$) and buy exactly one cupcake of each of types $l, l + 1, \\dots, r$.\n\nAfter that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice.\n\nFor example, let the tastinesses of the cupcakes be $[7, 4, -1]$. Yasser will buy all of them, the total tastiness will be $7 + 4 - 1 = 10$. Adel can choose segments $[7], [4], [-1], [7, 4]$ or $[4, -1]$, their total tastinesses are $7, 4, -1, 11$ and $3$, respectively. Adel can choose segment with tastiness $11$, and as $10$ is not strictly greater than $11$, Yasser won't be happy :(\n\nFind out if Yasser will be happy after visiting the shop.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains $n$ ($2 \\le n \\le 10^5$).\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^9 \\le a_i \\le 10^9$), where $a_i$ represents the tastiness of the $i$-th type of cupcake.\n\nIt is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case, print \"YES\", if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice. Otherwise, print \"NO\".\n\n\n-----Example-----\nInput\n3\n4\n1 2 3 4\n3\n7 4 -1\n3\n5 -5 5\n\nOutput\nYES\nNO\nNO\n\n\n\n-----Note-----\n\nIn the first example, the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes.\n\nIn the second example, Adel will choose the segment $[1, 2]$ with total tastiness $11$, which is not less than the total tastiness of all cupcakes, which is $10$.\n\nIn the third example, Adel can choose the segment $[3, 3]$ with total tastiness of $5$. Note that Yasser's cupcakes' total tastiness is also $5$, so in that case, the total tastiness of Yasser's cupcakes isn't strictly greater than the total tastiness of Adel's cupcakes."} +{"id": "02411", "text": "This problem is same as the previous one, but has larger constraints.\n\nIt was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.\n\nAt the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $(x_i, y_i)$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.\n\nSelena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 1000$)\u00a0\u2014 the number of electric poles.\n\nEach of the following $n$ lines contains two integers $x_i$, $y_i$ ($-10^4 \\le x_i, y_i \\le 10^4$)\u00a0\u2014 the coordinates of the poles.\n\nIt is guaranteed that all of these $n$ points are distinct.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of pairs of wires that are intersecting.\n\n\n-----Examples-----\nInput\n4\n0 0\n1 1\n0 3\n1 2\n\nOutput\n14\n\nInput\n4\n0 0\n0 2\n0 4\n2 0\n\nOutput\n6\n\nInput\n3\n-1 -1\n1 0\n3 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example:\n\n [Image] \n\nIn the second example:\n\n [Image] \n\nNote that the three poles $(0, 0)$, $(0, 2)$ and $(0, 4)$ are connected by a single wire.\n\nIn the third example:\n\n [Image]"} +{"id": "02412", "text": "A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000011223388 are not.\n\nYou are given a string $s$ of length $n$, consisting of digits.\n\nIn one operation you can delete any character from string $s$. For example, it is possible to obtain strings 112, 111 or 121 from string 1121.\n\nYou need to determine whether there is such a sequence of operations (possibly empty), after which the string $s$ becomes a telephone number.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThe first line of each test case contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the length of string $s$.\n\nThe second line of each test case contains the string $s$ ($|s| = n$) consisting of digits.\n\n\n-----Output-----\n\nFor each test print one line.\n\nIf there is a sequence of operations, after which $s$ becomes a telephone number, print YES.\n\nOtherwise, print NO.\n\n\n-----Example-----\nInput\n2\n13\n7818005553535\n11\n31415926535\n\nOutput\nYES\nNO\n\n\n\n-----Note-----\n\nIn the first test case you need to delete the first and the third digits. Then the string 7818005553535 becomes 88005553535."} +{"id": "02413", "text": "Nikolay lives in a two-storied house. There are $n$ rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between $1$ and $n$). \n\nIf Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers $i$ and $i+1$ on each floor are neighbouring, for all $1 \\leq i \\leq n - 1$. There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room $x$ on the first floor and the room $x$ on the second floor, then Nikolay can use it to move from one room to another.\n\n $\\text{floor}$ The picture illustrates a house with $n = 4$. There is a staircase between the room $2$ on the first floor and the room $2$ on the second floor, and another staircase between the room $4$ on the first floor and the room $4$ on the second floor. The arrows denote possible directions in which Nikolay can move. The picture corresponds to the string \"0101\" in the input. \n\n \n\nNikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won't enter a room where he has already been). \n\nCalculate the maximum number of rooms Nikolay can visit during his tour, if:\n\n he can start in any room on any floor of his choice, and he won't visit the same room twice. \n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases in the input. Then test cases follow. Each test case consists of two lines.\n\nThe first line contains one integer $n$ $(1 \\le n \\le 1\\,000)$ \u2014 the number of rooms on each floor.\n\nThe second line contains one string consisting of $n$ characters, each character is either a '0' or a '1'. If the $i$-th character is a '1', then there is a staircase between the room $i$ on the first floor and the room $i$ on the second floor. If the $i$-th character is a '0', then there is no staircase between the room $i$ on the first floor and the room $i$ on the second floor.\n\nIn hacks it is allowed to use only one test case in the input, so $t = 1$ should be satisfied.\n\n\n-----Output-----\n\nFor each test case print one integer \u2014 the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice.\n\n\n-----Example-----\nInput\n4\n5\n00100\n8\n00000000\n5\n11111\n3\n110\n\nOutput\n6\n8\n10\n6\n\n\n\n-----Note-----\n\nIn the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then \u2014 to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then \u2014 to the fifth room on the second floor. So, Nikolay visits $6$ rooms.\n\nThere are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room).\n\nIn the third test case it is possible to visit all rooms: first floor, first room $\\rightarrow$ second floor, first room $\\rightarrow$ second floor, second room $\\rightarrow$ first floor, second room $\\rightarrow$ first floor, third room $\\rightarrow$ second floor, third room $\\rightarrow$ second floor, fourth room $\\rightarrow$ first floor, fourth room $\\rightarrow$ first floor, fifth room $\\rightarrow$ second floor, fifth room.\n\nIn the fourth test case it is also possible to visit all rooms: second floor, third room $\\rightarrow$ second floor, second room $\\rightarrow$ second floor, first room $\\rightarrow$ first floor, first room $\\rightarrow$ first floor, second room $\\rightarrow$ first floor, third room."} +{"id": "02414", "text": "You are given two integers $a$ and $b$. Print $a+b$.\n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases in the input. Then $t$ test cases follow.\n\nEach test case is given as a line of two integers $a$ and $b$ ($-1000 \\le a, b \\le 1000$).\n\n\n-----Output-----\n\nPrint $t$ integers \u2014 the required numbers $a+b$.\n\n\n-----Example-----\nInput\n4\n1 5\n314 15\n-99 99\n123 987\n\nOutput\n6\n329\n0\n1110"} +{"id": "02415", "text": "-----Input-----\n\nThe input consists of a single string of uppercase letters A-Z. The length of the string is between 1 and 10 characters, inclusive.\n\n\n-----Output-----\n\nOutput \"YES\" or \"NO\".\n\n\n-----Examples-----\nInput\nGENIUS\n\nOutput\nYES\n\nInput\nDOCTOR\n\nOutput\nNO\n\nInput\nIRENE\n\nOutput\nYES\n\nInput\nMARY\n\nOutput\nNO\n\nInput\nSMARTPHONE\n\nOutput\nNO\n\nInput\nREVOLVER\n\nOutput\nYES\n\nInput\nHOLMES\n\nOutput\nNO\n\nInput\nWATSON\n\nOutput\nYES"} +{"id": "02416", "text": "Ksenia has an array $a$ consisting of $n$ positive integers $a_1, a_2, \\ldots, a_n$. \n\nIn one operation she can do the following: choose three distinct indices $i$, $j$, $k$, and then change all of $a_i, a_j, a_k$ to $a_i \\oplus a_j \\oplus a_k$ simultaneously, where $\\oplus$ denotes the bitwise XOR operation. \n\nShe wants to make all $a_i$ equal in at most $n$ operations, or to determine that it is impossible to do so. She wouldn't ask for your help, but please, help her!\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($3 \\leq n \\leq 10^5$)\u00a0\u2014 the length of $a$.\n\nThe second line contains $n$ integers, $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^9$)\u00a0\u2014 elements of $a$.\n\n\n-----Output-----\n\nPrint YES or NO in the first line depending on whether it is possible to make all elements equal in at most $n$ operations.\n\nIf it is possible, print an integer $m$ ($0 \\leq m \\leq n$), which denotes the number of operations you do.\n\nIn each of the next $m$ lines, print three distinct integers $i, j, k$, representing one operation. \n\nIf there are many such operation sequences possible, print any. Note that you do not have to minimize the number of operations.\n\n\n-----Examples-----\nInput\n5\n4 2 1 7 2\n\nOutput\nYES\n1\n1 3 4\nInput\n4\n10 4 49 22\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example, the array becomes $[4 \\oplus 1 \\oplus 7, 2, 4 \\oplus 1 \\oplus 7, 4 \\oplus 1 \\oplus 7, 2] = [2, 2, 2, 2, 2]$."} +{"id": "02417", "text": "Consider a tunnel on a one-way road. During a particular day, $n$ cars numbered from $1$ to $n$ entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.\n\nA traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced.\n\nThanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time.\n\nTraffic regulations prohibit overtaking inside the tunnel. If car $i$ overtakes any other car $j$ inside the tunnel, car $i$ must be fined. However, each car can be fined at most once.\n\nFormally, let's say that car $i$ definitely overtook car $j$ if car $i$ entered the tunnel later than car $j$ and exited the tunnel earlier than car $j$. Then, car $i$ must be fined if and only if it definitely overtook at least one other car.\n\nFind the number of cars that must be fined. \n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 10^5$), denoting the number of cars.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le n$), denoting the ids of cars in order of entering the tunnel. All $a_i$ are pairwise distinct.\n\nThe third line contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($1 \\le b_i \\le n$), denoting the ids of cars in order of exiting the tunnel. All $b_i$ are pairwise distinct.\n\n\n-----Output-----\n\nOutput the number of cars to be fined.\n\n\n-----Examples-----\nInput\n5\n3 5 2 1 4\n4 3 2 5 1\n\nOutput\n2\n\nInput\n7\n5 2 3 6 7 1 4\n2 3 6 7 1 4 5\n\nOutput\n6\n\nInput\n2\n1 2\n1 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nThe first example is depicted below:\n\n[Image]\n\nCar $2$ definitely overtook car $5$, while car $4$ definitely overtook cars $1$, $2$, $3$ and $5$. Cars $2$ and $4$ must be fined.\n\nIn the second example car $5$ was definitely overtaken by all other cars.\n\nIn the third example no car must be fined."} +{"id": "02418", "text": "You are given a sequence of $n$ integers $a_1, a_2, \\ldots, a_n$.\n\nYou have to construct two sequences of integers $b$ and $c$ with length $n$ that satisfy: for every $i$ ($1\\leq i\\leq n$) $b_i+c_i=a_i$ $b$ is non-decreasing, which means that for every $1 \\& \\> b$ is as large as possible. In other words, you'd like to compute the following function:\n\n$$f(a) = \\max_{0 < b < a}{gcd(a \\oplus b, a \\> \\& \\> b)}.$$\n\nHere $\\oplus$ denotes the bitwise XOR operation, and $\\&$ denotes the bitwise AND operation.\n\nThe greatest common divisor of two integers $x$ and $y$ is the largest integer $g$ such that both $x$ and $y$ are divided by $g$ without remainder.\n\nYou are given $q$ integers $a_1, a_2, \\ldots, a_q$. For each of these integers compute the largest possible value of the greatest common divisor (when $b$ is chosen optimally). \n\n\n-----Input-----\n\nThe first line contains an integer $q$ ($1 \\le q \\le 10^3$)\u00a0\u2014 the number of integers you need to compute the answer for.\n\nAfter that $q$ integers are given, one per line: $a_1, a_2, \\ldots, a_q$ ($2 \\le a_i \\le 2^{25} - 1$)\u00a0\u2014 the integers you need to compute the answer for. \n\n\n-----Output-----\n\nFor each integer, print the answer in the same order as the integers are given in input.\n\n\n-----Example-----\nInput\n3\n2\n3\n5\n\nOutput\n3\n1\n7\n\n\n\n-----Note-----\n\nFor the first integer the optimal choice is $b = 1$, then $a \\oplus b = 3$, $a \\> \\& \\> b = 0$, and the greatest common divisor of $3$ and $0$ is $3$.\n\nFor the second integer one optimal choice is $b = 2$, then $a \\oplus b = 1$, $a \\> \\& \\> b = 2$, and the greatest common divisor of $1$ and $2$ is $1$.\n\nFor the third integer the optimal choice is $b = 2$, then $a \\oplus b = 7$, $a \\> \\& \\> b = 0$, and the greatest common divisor of $7$ and $0$ is $7$."} +{"id": "02426", "text": "You are given an array $a$ consisting of $n$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $2$) or determine that there is no such subset.\n\nBoth the given array and required subset may contain equal values.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\leq t \\leq 100$), number of test cases to solve. Descriptions of $t$ test cases follow.\n\nA description of each test case consists of two lines. The first line contains a single integer $n$ ($1 \\leq n \\leq 100$), length of array $a$.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 100$), elements of $a$. The given array $a$ can contain equal values (duplicates).\n\n\n-----Output-----\n\nFor each test case output $-1$ if there is no such subset of elements. Otherwise output positive integer $k$, number of elements in the required subset. Then output $k$ distinct integers ($1 \\leq p_i \\leq n$), indexes of the chosen elements. If there are multiple solutions output any of them.\n\n\n-----Example-----\nInput\n3\n3\n1 4 3\n1\n15\n2\n3 5\n\nOutput\n1\n2\n-1\n2\n1 2\n\n\n\n-----Note-----\n\nThere are three test cases in the example.\n\nIn the first test case, you can choose the subset consisting of only the second element. Its sum is $4$ and it is even.\n\nIn the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.\n\nIn the third test case, the subset consisting of all array's elements has even sum."} +{"id": "02427", "text": "Yurii is sure he can do everything. Can he solve this task, though?\n\nHe has an array $a$ consisting of $n$ positive integers. Let's call a subarray $a[l...r]$ good if the following conditions are simultaneously satisfied: $l+1 \\leq r-1$, i.\u00a0e. the subarray has length at least $3$; $(a_l \\oplus a_r) = (a_{l+1}+a_{l+2}+\\ldots+a_{r-2}+a_{r-1})$, where $\\oplus$ denotes the bitwise XOR operation. \n\nIn other words, a subarray is good if the bitwise XOR of the two border elements is equal to the sum of the rest of the elements. \n\nYurii wants to calculate the total number of good subarrays. What is it equal to?\n\nAn array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($3 \\leq n \\leq 2\\cdot 10^5$)\u00a0\u2014 the length of $a$. \n\nThe second line contains $n$ integers $a_1,a_2,\\ldots,a_n$ ($1 \\leq a_i \\lt 2^{30}$)\u00a0\u2014 elements of $a$. \n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the number of good subarrays. \n\n\n-----Examples-----\nInput\n8\n3 1 2 3 1 2 3 15\n\nOutput\n6\nInput\n10\n997230370 58052053 240970544 715275815 250707702 156801523 44100666 64791577 43523002 480196854\n\nOutput\n2\n\n\n-----Note-----\n\nThere are $6$ good subarrays in the example: $[3,1,2]$ (twice) because $(3 \\oplus 2) = 1$; $[1,2,3]$ (twice) because $(1 \\oplus 3) = 2$; $[2,3,1]$ because $(2 \\oplus 1) = 3$; $[3,1,2,3,1,2,3,15]$ because $(3 \\oplus 15) = (1+2+3+1+2+3)$."} +{"id": "02428", "text": "You are given a string $s$. You can build new string $p$ from $s$ using the following operation no more than two times: choose any subsequence $s_{i_1}, s_{i_2}, \\dots, s_{i_k}$ where $1 \\le i_1 < i_2 < \\dots < i_k \\le |s|$; erase the chosen subsequence from $s$ ($s$ can become empty); concatenate chosen subsequence to the right of the string $p$ (in other words, $p = p + s_{i_1}s_{i_2}\\dots s_{i_k}$). \n\nOf course, initially the string $p$ is empty. \n\nFor example, let $s = \\text{ababcd}$. At first, let's choose subsequence $s_1 s_4 s_5 = \\text{abc}$ \u2014 we will get $s = \\text{bad}$ and $p = \\text{abc}$. At second, let's choose $s_1 s_2 = \\text{ba}$ \u2014 we will get $s = \\text{d}$ and $p = \\text{abcba}$. So we can build $\\text{abcba}$ from $\\text{ababcd}$.\n\nCan you build a given string $t$ using the algorithm above?\n\n\n-----Input-----\n\nThe first line contains the single integer $T$ ($1 \\le T \\le 100$) \u2014 the number of test cases.\n\nNext $2T$ lines contain test cases \u2014 two per test case. The first line contains string $s$ consisting of lowercase Latin letters ($1 \\le |s| \\le 400$) \u2014 the initial string.\n\nThe second line contains string $t$ consisting of lowercase Latin letters ($1 \\le |t| \\le |s|$) \u2014 the string you'd like to build.\n\nIt's guaranteed that the total length of strings $s$ doesn't exceed $400$.\n\n\n-----Output-----\n\nPrint $T$ answers \u2014 one per test case. Print YES (case insensitive) if it's possible to build $t$ and NO (case insensitive) otherwise.\n\n\n-----Example-----\nInput\n4\nababcd\nabcba\na\nb\ndefi\nfed\nxyz\nx\n\nOutput\nYES\nNO\nNO\nYES"} +{"id": "02429", "text": "Phoenix has $n$ coins with weights $2^1, 2^2, \\dots, 2^n$. He knows that $n$ is even.\n\nHe wants to split the coins into two piles such that each pile has exactly $\\frac{n}{2}$ coins and the difference of weights between the two piles is minimized. Formally, let $a$ denote the sum of weights in the first pile, and $b$ denote the sum of weights in the second pile. Help Phoenix minimize $|a-b|$, the absolute value of $a-b$.\n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains an integer $t$ ($1 \\le t \\le 100$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains an integer $n$ ($2 \\le n \\le 30$; $n$ is even)\u00a0\u2014 the number of coins that Phoenix has. \n\n\n-----Output-----\n\nFor each test case, output one integer\u00a0\u2014 the minimum possible difference of weights between the two piles.\n\n\n-----Example-----\nInput\n2\n2\n4\n\nOutput\n2\n6\n\n\n\n-----Note-----\n\nIn the first test case, Phoenix has two coins with weights $2$ and $4$. No matter how he divides the coins, the difference will be $4-2=2$.\n\nIn the second test case, Phoenix has four coins of weight $2$, $4$, $8$, and $16$. It is optimal for Phoenix to place coins with weights $2$ and $16$ in one pile, and coins with weights $4$ and $8$ in another pile. The difference is $(2+16)-(4+8)=6$."} +{"id": "02430", "text": "Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is h_{i}. Liss wants to eat all nuts.\n\nNow Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height h of the tree i (1 \u2264 i \u2264 n - 1), she jumps to height h of the tree i + 1. This action can't be performed if h > h_{i} + 1. \n\nCompute the minimal time (in seconds) required to eat all nuts.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^5) \u2014 the number of trees.\n\nNext n lines contains the height of trees: i-th line contains an integer h_{i} (1 \u2264 h_{i} \u2264 10^4) \u2014 the height of the tree with the number i.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimal time required to eat all nuts in seconds.\n\n\n-----Examples-----\nInput\n2\n1\n2\n\nOutput\n5\n\nInput\n5\n2\n1\n2\n1\n1\n\nOutput\n14"} +{"id": "02431", "text": "The Red Kingdom is attacked by the White King and the Black King!\n\nThe Kingdom is guarded by $n$ castles, the $i$-th castle is defended by $a_i$ soldiers. To conquer the Red Kingdom, the Kings have to eliminate all the defenders. \n\nEach day the White King launches an attack on one of the castles. Then, at night, the forces of the Black King attack a castle (possibly the same one). Then the White King attacks a castle, then the Black King, and so on. The first attack is performed by the White King.\n\nEach attack must target a castle with at least one alive defender in it. There are three types of attacks:\n\n a mixed attack decreases the number of defenders in the targeted castle by $x$ (or sets it to $0$ if there are already less than $x$ defenders); an infantry attack decreases the number of defenders in the targeted castle by $y$ (or sets it to $0$ if there are already less than $y$ defenders); a cavalry attack decreases the number of defenders in the targeted castle by $z$ (or sets it to $0$ if there are already less than $z$ defenders). \n\nThe mixed attack can be launched at any valid target (at any castle with at least one soldier). However, the infantry attack cannot be launched if the previous attack on the targeted castle had the same type, no matter when and by whom it was launched. The same applies to the cavalry attack. A castle that was not attacked at all can be targeted by any type of attack.\n\nThe King who launches the last attack will be glorified as the conqueror of the Red Kingdom, so both Kings want to launch the last attack (and they are wise enough to find a strategy that allows them to do it no matter what are the actions of their opponent, if such strategy exists). The White King is leading his first attack, and you are responsible for planning it. Can you calculate the number of possible options for the first attack that allow the White King to launch the last attack? Each option for the first attack is represented by the targeted castle and the type of attack, and two options are different if the targeted castles or the types of attack are different.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases.\n\nThen, the test cases follow. Each test case is represented by two lines. \n\nThe first line contains four integers $n$, $x$, $y$ and $z$ ($1 \\le n \\le 3 \\cdot 10^5$, $1 \\le x, y, z \\le 5$). \n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \\le a_i \\le 10^{18}$).\n\nIt is guaranteed that the sum of values of $n$ over all test cases in the input does not exceed $3 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case, print the answer to it: the number of possible options for the first attack of the White King (or $0$, if the Black King can launch the last attack no matter how the White King acts).\n\n\n-----Examples-----\nInput\n3\n2 1 3 4\n7 6\n1 1 2 3\n1\n1 1 2 2\n3\n\nOutput\n2\n3\n0\n\nInput\n10\n6 5 4 5\n2 3 2 3 1 3\n1 5 2 3\n10\n4 4 2 3\n8 10 8 5\n2 2 1 4\n8 5\n3 5 3 5\n9 2 10\n4 5 5 5\n2 10 4 2\n2 3 1 4\n1 10\n3 1 5 3\n9 8 7\n2 5 4 5\n8 8\n3 5 1 4\n5 5 10\n\nOutput\n0\n2\n1\n2\n5\n12\n5\n0\n0\n2"} +{"id": "02432", "text": "-----Input-----\n\nThe input contains a single integer $a$ ($0 \\le a \\le 63$).\n\n\n-----Output-----\n\nOutput a single number.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n2\n\nInput\n5\n\nOutput\n24\n\nInput\n35\n\nOutput\n50"} +{"id": "02433", "text": "There are two types of burgers in your restaurant \u2014 hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet. \n\nYou have $b$ buns, $p$ beef patties and $f$ chicken cutlets in your restaurant. You can sell one hamburger for $h$ dollars and one chicken burger for $c$ dollars. Calculate the maximum profit you can achieve.\n\nYou have to answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2013 the number of queries.\n\nThe first line of each query contains three integers $b$, $p$ and $f$ ($1 \\le b, ~p, ~f \\le 100$) \u2014 the number of buns, beef patties and chicken cutlets in your restaurant.\n\nThe second line of each query contains two integers $h$ and $c$ ($1 \\le h, ~c \\le 100$) \u2014 the hamburger and chicken burger prices in your restaurant.\n\n\n-----Output-----\n\nFor each query print one integer \u2014 the maximum profit you can achieve.\n\n\n-----Example-----\nInput\n3\n15 2 3\n5 10\n7 5 2\n10 12\n1 100 100\n100 100\n\nOutput\n40\n34\n0\n\n\n\n-----Note-----\n\nIn first query you have to sell two hamburgers and three chicken burgers. Your income is $2 \\cdot 5 + 3 \\cdot 10 = 40$.\n\nIn second query you have to ell one hamburgers and two chicken burgers. Your income is $1 \\cdot 10 + 2 \\cdot 12 = 34$.\n\nIn third query you can not create any type of burgers because because you have only one bun. So your income is zero."} +{"id": "02434", "text": "You are given two integers $n$ and $m$ ($m < n$). Consider a convex regular polygon of $n$ vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length). [Image] Examples of convex regular polygons \n\nYour task is to say if it is possible to build another convex regular polygon with $m$ vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases.\n\nThe next $t$ lines describe test cases. Each test case is given as two space-separated integers $n$ and $m$ ($3 \\le m < n \\le 100$) \u2014 the number of vertices in the initial polygon and the number of vertices in the polygon you want to build.\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 \"YES\" (without quotes), if it is possible to build another convex regular polygon with $m$ vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon and \"NO\" otherwise.\n\n\n-----Example-----\nInput\n2\n6 3\n7 3\n\nOutput\nYES\nNO\n\n\n\n-----Note----- $0$ The first test case of the example \n\nIt can be shown that the answer for the second test case of the example is \"NO\"."} +{"id": "02435", "text": "You are given an array consisting of $n$ integers $a_1$, $a_2$, ..., $a_n$. Initially $a_x = 1$, all other elements are equal to $0$.\n\nYou have to perform $m$ operations. During the $i$-th operation, you choose two indices $c$ and $d$ such that $l_i \\le c, d \\le r_i$, and swap $a_c$ and $a_d$.\n\nCalculate the number of indices $k$ such that it is possible to choose the operations so that $a_k = 1$ in the end.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 100$)\u00a0\u2014 the number of test cases. Then the description of $t$ testcases follow.\n\nThe first line of each test case contains three integers $n$, $x$ and $m$ ($1 \\le n \\le 10^9$; $1 \\le m \\le 100$; $1 \\le x \\le n$).\n\nEach of next $m$ lines contains the descriptions of the operations; the $i$-th line contains two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le n$).\n\n\n-----Output-----\n\nFor each test case print one integer \u2014 the number of indices $k$ such that it is possible to choose the operations so that $a_k = 1$ in the end.\n\n\n-----Example-----\nInput\n3\n6 4 3\n1 6\n2 3\n5 5\n4 1 2\n2 4\n1 2\n3 3 2\n2 3\n1 2\n\nOutput\n6\n2\n3\n\n\n\n-----Note-----\n\nIn the first test case, it is possible to achieve $a_k = 1$ for every $k$. To do so, you may use the following operations: swap $a_k$ and $a_4$; swap $a_2$ and $a_2$; swap $a_5$ and $a_5$. \n\nIn the second test case, only $k = 1$ and $k = 2$ are possible answers. To achieve $a_1 = 1$, you have to swap $a_1$ and $a_1$ during the second operation. To achieve $a_2 = 1$, you have to swap $a_1$ and $a_2$ during the second operation."} +{"id": "02436", "text": "Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.\n\nShe has $n$ friends who are also grannies (Maria is not included in this number). The $i$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $a_i$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $i$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $a_i$.\n\nGrannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $1$). All the remaining $n$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $a_i$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $i$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $a_i$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. \n\nYour task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!\n\nConsider an example: if $n=6$ and $a=[1,5,4,5,1,9]$, then: at the first step Maria can call grannies with numbers $1$ and $5$, each of them will see two grannies at the moment of going out into the yard (note that $a_1=1 \\le 2$ and $a_5=1 \\le 2$); at the second step, Maria can call grannies with numbers $2$, $3$ and $4$, each of them will see five grannies at the moment of going out into the yard (note that $a_2=5 \\le 5$, $a_3=4 \\le 5$ and $a_4=5 \\le 5$); the $6$-th granny cannot be called into the yard \u00a0\u2014 therefore, the answer is $6$ (Maria herself and another $5$ grannies). \n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases in the input. Then test cases follow.\n\nThe first line of a test case contains a single integer $n$ ($1 \\le n \\le 10^5$) \u2014 the number of grannies (Maria is not included in this number).\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 2\\cdot10^5$).\n\nIt is guaranteed that the sum of the values $n$ over all test cases of the input does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case, print a single integer $k$ ($1 \\le k \\le n + 1$) \u2014 the maximum possible number of grannies in the courtyard.\n\n\n-----Example-----\nInput\n4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6\n\nOutput\n6\n1\n6\n4\n\n\n\n-----Note-----\n\nIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.\n\nIn the second test case in the example, no one can be in the yard, so Maria will remain there alone.\n\nThe third test case in the example is described in the details above.\n\nIn the fourth test case in the example, on the first step Maria can call grannies with numbers $1$, $2$ and $3$. If on the second step Maria calls $4$ or $5$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $4$-th granny or the $5$-th granny separately (one of them). If she calls both: $4$ and $5$, then when they appear, they will see $4+1=5$ grannies. Despite the fact that it is enough for the $4$-th granny, the $5$-th granny is not satisfied. So, Maria cannot call both the $4$-th granny and the $5$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total."} +{"id": "02437", "text": "Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:\n\nYou have an array $a$ consisting of $n$ positive integers. An operation consists of choosing an element and either adding $1$ to it or subtracting $1$ from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not $1$. Find the minimum number of operations needed to make the array good.\n\nUnable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u00a0\u2014 the number of elements in the array.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$. ($1 \\le a_i \\le 10^{12}$) \u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint a single integer \u00a0\u2014 the minimum number of operations required to make the array good.\n\n\n-----Examples-----\nInput\n3\n6 2 4\n\nOutput\n0\n\nInput\n5\n9 8 7 3 1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, the first array is already good, since the greatest common divisor of all the elements is $2$.\n\nIn the second example, we may apply the following operations:\n\n Add $1$ to the second element, making it equal to $9$. Subtract $1$ from the third element, making it equal to $6$. Add $1$ to the fifth element, making it equal to $2$. Add $1$ to the fifth element again, making it equal to $3$. \n\nThe greatest common divisor of all elements will then be equal to $3$, so the array will be good. It can be shown that no sequence of three or less operations can make the array good."} +{"id": "02438", "text": "The string $t_1t_2 \\dots t_k$ is good if each letter of this string belongs to at least one palindrome of length greater than 1.\n\nA palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not.\n\nHere are some examples of good strings: $t$ = AABBB (letters $t_1$, $t_2$ belong to palindrome $t_1 \\dots t_2$ and letters $t_3$, $t_4$, $t_5$ belong to palindrome $t_3 \\dots t_5$); $t$ = ABAA (letters $t_1$, $t_2$, $t_3$ belong to palindrome $t_1 \\dots t_3$ and letter $t_4$ belongs to palindrome $t_3 \\dots t_4$); $t$ = AAAAA (all letters belong to palindrome $t_1 \\dots t_5$); \n\nYou are given a string $s$ of length $n$, consisting of only letters A and B.\n\nYou have to calculate the number of good substrings of string $s$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 3 \\cdot 10^5$) \u2014 the length of the string $s$.\n\nThe second line contains the string $s$, consisting of letters A and B.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of good substrings of string $s$.\n\n\n-----Examples-----\nInput\n5\nAABBB\n\nOutput\n6\n\nInput\n3\nAAA\n\nOutput\n3\n\nInput\n7\nAAABABB\n\nOutput\n15\n\n\n\n-----Note-----\n\nIn the first test case there are six good substrings: $s_1 \\dots s_2$, $s_1 \\dots s_4$, $s_1 \\dots s_5$, $s_3 \\dots s_4$, $s_3 \\dots s_5$ and $s_4 \\dots s_5$.\n\nIn the second test case there are three good substrings: $s_1 \\dots s_2$, $s_1 \\dots s_3$ and $s_2 \\dots s_3$."} +{"id": "02439", "text": "You are given an array of $n$ integers $a_1,a_2,\\dots,a_n$.\n\nYou have to create an array of $n$ integers $b_1,b_2,\\dots,b_n$ such that: The array $b$ is a rearrangement of the array $a$, that is, it contains the same values and each value appears the same number of times in the two arrays. In other words, the multisets $\\{a_1,a_2,\\dots,a_n\\}$ and $\\{b_1,b_2,\\dots,b_n\\}$ are equal.\n\nFor example, if $a=[1,-1,0,1]$, then $b=[-1,1,1,0]$ and $b=[0,1,-1,1]$ are rearrangements of $a$, but $b=[1,-1,-1,0]$ and $b=[1,0,2,-3]$ are not rearrangements of $a$. For all $k=1,2,\\dots,n$ the sum of the first $k$ elements of $b$ is nonzero. Formally, for all $k=1,2,\\dots,n$, it must hold $$b_1+b_2+\\cdots+b_k\\not=0\\,.$$ \n\nIf an array $b_1,b_2,\\dots, b_n$ with the required properties does not exist, you have to print NO.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains an integer $t$ ($1\\le t \\le 1000$) \u2014 the number of test cases. The description of the test cases follows.\n\nThe first line of each testcase contains one integer $n$ ($1\\le n\\le 50$) \u00a0\u2014 the length of the array $a$.\n\nThe second line of each testcase contains $n$ integers $a_1,a_2,\\dots, a_n$ ($-50\\le a_i\\le 50$) \u00a0\u2014 the elements of $a$.\n\n\n-----Output-----\n\nFor each testcase, if there is not an array $b_1,b_2,\\dots,b_n$ with the required properties, print a single line with the word NO.\n\nOtherwise print a line with the word YES, followed by a line with the $n$ integers $b_1,b_2,\\dots,b_n$. \n\nIf there is more than one array $b_1,b_2,\\dots,b_n$ satisfying the required properties, you can print any of them.\n\n\n-----Example-----\nInput\n4\n4\n1 -2 3 -4\n3\n0 0 0\n5\n1 -1 1 -1 1\n6\n40 -31 -9 0 13 -40\n\nOutput\nYES\n1 -2 3 -4\nNO\nYES\n1 1 -1 1 -1\nYES\n-40 13 40 0 -9 -31\n\n\n\n-----Note-----\n\nExplanation of the first testcase: An array with the desired properties is $b=[1,-2,3,-4]$. For this array, it holds: The first element of $b$ is $1$. The sum of the first two elements of $b$ is $-1$. The sum of the first three elements of $b$ is $2$. The sum of the first four elements of $b$ is $-2$. \n\nExplanation of the second testcase: Since all values in $a$ are $0$, any rearrangement $b$ of $a$ will have all elements equal to $0$ and therefore it clearly cannot satisfy the second property described in the statement (for example because $b_1=0$). Hence in this case the answer is NO.\n\nExplanation of the third testcase: An array with the desired properties is $b=[1, 1, -1, 1, -1]$. For this array, it holds: The first element of $b$ is $1$. The sum of the first two elements of $b$ is $2$. The sum of the first three elements of $b$ is $1$. The sum of the first four elements of $b$ is $2$. The sum of the first five elements of $b$ is $1$. \n\nExplanation of the fourth testcase: An array with the desired properties is $b=[-40,13,40,0,-9,-31]$. For this array, it holds: The first element of $b$ is $-40$. The sum of the first two elements of $b$ is $-27$. The sum of the first three elements of $b$ is $13$. The sum of the first four elements of $b$ is $13$. The sum of the first five elements of $b$ is $4$. The sum of the first six elements of $b$ is $-27$."} +{"id": "02440", "text": "Gildong was hiking a mountain, walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree?\n\nThen he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too many tree problems, he wanted to see if similar techniques in trees can be used in 1-trees as well. Instead of solving it by himself, he's going to test you by providing queries on 1-trees.\n\nFirst, he'll provide you a tree (not 1-tree) with $n$ vertices, then he will ask you $q$ queries. Each query contains $5$ integers: $x$, $y$, $a$, $b$, and $k$. This means you're asked to determine if there exists a path from vertex $a$ to $b$ that contains exactly $k$ edges after adding a bidirectional edge between vertices $x$ and $y$. A path can contain the same vertices and same edges multiple times. All queries are independent of each other; i.e. the added edge in a query is removed in the next query.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($3 \\le n \\le 10^5$), the number of vertices of the tree.\n\nNext $n-1$ lines contain two integers $u$ and $v$ ($1 \\le u,v \\le n$, $u \\ne v$) each, which means there is an edge between vertex $u$ and $v$. All edges are bidirectional and distinct.\n\nNext line contains an integer $q$ ($1 \\le q \\le 10^5$), the number of queries Gildong wants to ask.\n\nNext $q$ lines contain five integers $x$, $y$, $a$, $b$, and $k$ each ($1 \\le x,y,a,b \\le n$, $x \\ne y$, $1 \\le k \\le 10^9$) \u2013 the integers explained in the description. It is guaranteed that the edge between $x$ and $y$ does not exist in the original tree.\n\n\n-----Output-----\n\nFor each query, print \"YES\" if there exists a path that contains exactly $k$ edges from vertex $a$ to $b$ after adding an edge between vertices $x$ and $y$. Otherwise, print \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n5\n1 2\n2 3\n3 4\n4 5\n5\n1 3 1 2 2\n1 4 1 3 2\n1 4 1 3 3\n4 2 3 3 9\n5 2 3 3 9\n\nOutput\nYES\nYES\nNO\nYES\nNO\n\n\n\n-----Note-----\n\nThe image below describes the tree (circles and solid lines) and the added edges for each query (dotted lines). [Image] \n\nPossible paths for the queries with \"YES\" answers are: $1$-st query: $1$ \u2013 $3$ \u2013 $2$ $2$-nd query: $1$ \u2013 $2$ \u2013 $3$ $4$-th query: $3$ \u2013 $4$ \u2013 $2$ \u2013 $3$ \u2013 $4$ \u2013 $2$ \u2013 $3$ \u2013 $4$ \u2013 $2$ \u2013 $3$"} +{"id": "02441", "text": "Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.\n\nTo ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either i = j or the police patrol car can go to j from i and then come back to i.\n\nBuilding checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.\n\nYou have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.\n\n\n-----Input-----\n\nIn the first line, you will be given an integer n, number of junctions (1 \u2264 n \u2264 10^5). In the next line, n space-separated integers will be given. The i^{th} integer is the cost of building checkpost at the i^{th} junction (costs will be non-negative and will not exceed 10^9).\n\nThe next line will contain an integer m\u00a0(0 \u2264 m \u2264 3\u00b710^5). And each of the next m lines contains two integers u_{i} and v_{i}\u00a0(1 \u2264 u_{i}, v_{i} \u2264 n;\u00a0u \u2260 v). A pair u_{i}, v_{i} means, that there is a one-way road which goes from u_{i} to v_{i}. There will not be more than one road between two nodes in the same direction.\n\n\n-----Output-----\n\nPrint two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n3\n1 2 3\n3\n1 2\n2 3\n3 2\n\nOutput\n3 1\n\nInput\n5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1\n\nOutput\n8 2\n\nInput\n10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9\n\nOutput\n15 6\n\nInput\n2\n7 91\n2\n1 2\n2 1\n\nOutput\n7 1"} +{"id": "02442", "text": "Given a set of integers (it can contain equal elements).\n\nYou have to split it into two subsets $A$ and $B$ (both of them can contain equal elements or be empty). You have to maximize the value of $mex(A)+mex(B)$.\n\nHere $mex$ of a set denotes the smallest non-negative integer that doesn't exist in the set. For example: $mex(\\{1,4,0,2,2,1\\})=3$ $mex(\\{3,3,2,1,3,0,0\\})=4$ $mex(\\varnothing)=0$ ($mex$ for empty set) \n\nThe set is splitted into two subsets $A$ and $B$ if for any integer number $x$ the number of occurrences of $x$ into this set is equal to the sum of the number of occurrences of $x$ into $A$ and the number of occurrences of $x$ into $B$.\n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains an integer $t$ ($1\\leq t\\leq 100$) \u2014 the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains an integer $n$ ($1\\leq n\\leq 100$) \u2014 the size of the set.\n\nThe second line of each testcase contains $n$ integers $a_1,a_2,\\dots a_n$ ($0\\leq a_i\\leq 100$) \u2014 the numbers in the set.\n\n\n-----Output-----\n\nFor each test case, print the maximum value of $mex(A)+mex(B)$.\n\n\n-----Example-----\nInput\n4\n6\n0 2 1 5 0 1\n3\n0 1 2\n4\n0 2 0 1\n6\n1 2 3 4 5 6\n\nOutput\n5\n3\n4\n0\n\n\n\n-----Note-----\n\nIn the first test case, $A=\\left\\{0,1,2\\right\\},B=\\left\\{0,1,5\\right\\}$ is a possible choice.\n\nIn the second test case, $A=\\left\\{0,1,2\\right\\},B=\\varnothing$ is a possible choice.\n\nIn the third test case, $A=\\left\\{0,1,2\\right\\},B=\\left\\{0\\right\\}$ is a possible choice.\n\nIn the fourth test case, $A=\\left\\{1,3,5\\right\\},B=\\left\\{2,4,6\\right\\}$ is a possible choice."} +{"id": "02443", "text": "You are given an integer m, and a list of n distinct integers between 0 and m - 1.\n\nYou would like to construct a sequence satisfying the properties: Each element is an integer between 0 and m - 1, inclusive. All prefix products of the sequence modulo m are distinct. No prefix product modulo m appears as an element of the input list. The length of the sequence is maximized. \n\nConstruct any sequence satisfying the properties above.\n\n\n-----Input-----\n\nThe first line of input contains two integers n and m (0 \u2264 n < m \u2264 200 000)\u00a0\u2014 the number of forbidden prefix products and the modulus.\n\nIf n is non-zero, the next line of input contains n distinct integers between 0 and m - 1, the forbidden prefix products. If n is zero, this line doesn't exist.\n\n\n-----Output-----\n\nOn the first line, print the number k, denoting the length of your sequence.\n\nOn the second line, print k space separated integers, denoting your sequence.\n\n\n-----Examples-----\nInput\n0 5\n\nOutput\n5\n1 2 4 3 0\n\nInput\n3 10\n2 9 1\n\nOutput\n6\n3 9 2 9 8 0\n\n\n\n-----Note-----\n\nFor the first case, the prefix products of this sequence modulo m are [1, 2, 3, 4, 0].\n\nFor the second case, the prefix products of this sequence modulo m are [3, 7, 4, 6, 8, 0]."} +{"id": "02444", "text": "There are $n$ seats in the train's car and there is exactly one passenger occupying every seat. The seats are numbered from $1$ to $n$ from left to right. The trip is long, so each passenger will become hungry at some moment of time and will go to take boiled water for his noodles. The person at seat $i$ ($1 \\leq i \\leq n$) will decide to go for boiled water at minute $t_i$.\n\nTank with a boiled water is located to the left of the $1$-st seat. In case too many passengers will go for boiled water simultaneously, they will form a queue, since there can be only one passenger using the tank at each particular moment of time. Each passenger uses the tank for exactly $p$ minutes. We assume that the time it takes passengers to go from their seat to the tank is negligibly small. \n\nNobody likes to stand in a queue. So when the passenger occupying the $i$-th seat wants to go for a boiled water, he will first take a look on all seats from $1$ to $i - 1$. In case at least one of those seats is empty, he assumes that those people are standing in a queue right now, so he would be better seating for the time being. However, at the very first moment he observes that all seats with numbers smaller than $i$ are busy, he will go to the tank.\n\nThere is an unspoken rule, that in case at some moment several people can go to the tank, than only the leftmost of them (that is, seating on the seat with smallest number) will go to the tank, while all others will wait for the next moment.\n\nYour goal is to find for each passenger, when he will receive the boiled water for his noodles.\n\n\n-----Input-----\n\nThe first line contains integers $n$ and $p$ ($1 \\leq n \\leq 100\\,000$, $1 \\leq p \\leq 10^9$)\u00a0\u2014 the number of people and the amount of time one person uses the tank.\n\nThe second line contains $n$ integers $t_1, t_2, \\dots, t_n$ ($0 \\leq t_i \\leq 10^9$)\u00a0\u2014 the moments when the corresponding passenger will go for the boiled water.\n\n\n-----Output-----\n\nPrint $n$ integers, where $i$-th of them is the time moment the passenger on $i$-th seat will receive his boiled water.\n\n\n-----Example-----\nInput\n5 314\n0 310 942 628 0\n\nOutput\n314 628 1256 942 1570 \n\n\n\n-----Note-----\n\nConsider the example.\n\nAt the $0$-th minute there were two passengers willing to go for a water, passenger $1$ and $5$, so the first passenger has gone first, and returned at the $314$-th minute. At this moment the passenger $2$ was already willing to go for the water, so the passenger $2$ has gone next, and so on. In the end, $5$-th passenger was last to receive the boiled water."} +{"id": "02445", "text": "One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. \n\nThe game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $1\\times1$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. \n\nThe blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.\n\nRainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!\n\nSince the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.\n\nThe chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\le T \\le 100$): the number of grids of the games. \n\nEach of the next $T$ lines contains a single integer $n$ ($1 \\le n \\le 10^9$): the size of the side of the grid of the game. \n\n\n-----Output-----\n\nFor each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.\n\n\n-----Example-----\nInput\n2\n3\n4\n\nOutput\n2\n3\n\n\n\n-----Note-----\n\nFor $3\\times3$ grid ponies can make two following moves: [Image]"} +{"id": "02446", "text": "Given a sequence of integers a_1, ..., a_{n} and q queries x_1, ..., x_{q} on it. For each query x_{i} you have to count the number of pairs (l, r) such that 1 \u2264 l \u2264 r \u2264 n and gcd(a_{l}, a_{l} + 1, ..., a_{r}) = x_{i}.\n\n$\\operatorname{gcd}(v_{1}, v_{2}, \\ldots, v_{n})$ is a greatest common divisor of v_1, v_2, ..., v_{n}, that is equal to a largest positive integer that divides all v_{i}.\n\n\n-----Input-----\n\nThe first line of the input contains integer n, (1 \u2264 n \u2264 10^5), denoting the length of the sequence. The next line contains n space separated integers a_1, ..., a_{n}, (1 \u2264 a_{i} \u2264 10^9).\n\nThe third line of the input contains integer q, (1 \u2264 q \u2264 3 \u00d7 10^5), denoting the number of queries. Then follows q lines, each contain an integer x_{i}, (1 \u2264 x_{i} \u2264 10^9).\n\n\n-----Output-----\n\nFor each query print the result in a separate line.\n\n\n-----Examples-----\nInput\n3\n2 6 3\n5\n1\n2\n3\n4\n6\n\nOutput\n1\n2\n2\n0\n1\n\nInput\n7\n10 20 3 15 1000 60 16\n10\n1\n2\n3\n4\n5\n6\n10\n20\n60\n1000\n\nOutput\n14\n0\n2\n2\n2\n0\n2\n2\n1\n1"} +{"id": "02447", "text": "Shubham has a binary string $s$. A binary string is a string containing only characters \"0\" and \"1\".\n\nHe can perform the following operation on the string any amount of times: Select an index of the string, and flip the character at that index. This means, if the character was \"0\", it becomes \"1\", and vice versa. \n\nA string is called good if it does not contain \"010\" or \"101\" as a subsequence \u00a0\u2014 for instance, \"1001\" contains \"101\" as a subsequence, hence it is not a good string, while \"1000\" doesn't contain neither \"010\" nor \"101\" as subsequences, so it is a good string.\n\nWhat is the minimum number of operations he will have to perform, so that the string becomes good? It can be shown that with these operations we can make any string good.\n\nA string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $t$ $(1\\le t \\le 100)$\u00a0\u2014 the number of test cases.\n\nEach of the next $t$ lines contains a binary string $s$ $(1 \\le |s| \\le 1000)$.\n\n\n-----Output-----\n\nFor every string, output the minimum number of operations required to make it good.\n\n\n-----Example-----\nInput\n7\n001\n100\n101\n010\n0\n1\n001100\n\nOutput\n0\n0\n1\n1\n0\n0\n2\n\n\n\n-----Note-----\n\nIn test cases $1$, $2$, $5$, $6$ no operations are required since they are already good strings.\n\nFor the $3$rd test case: \"001\" can be achieved by flipping the first character \u00a0\u2014 and is one of the possible ways to get a good string.\n\nFor the $4$th test case: \"000\" can be achieved by flipping the second character \u00a0\u2014 and is one of the possible ways to get a good string.\n\nFor the $7$th test case: \"000000\" can be achieved by flipping the third and fourth characters \u00a0\u2014 and is one of the possible ways to get a good string."} +{"id": "02448", "text": "Let $n$ be a positive integer. Let $a, b, c$ be nonnegative integers such that $a + b + c = n$.\n\nAlice and Bob are gonna play rock-paper-scissors $n$ times. Alice knows the sequences of hands that Bob will play. However, Alice has to play rock $a$ times, paper $b$ times, and scissors $c$ times.\n\nAlice wins if she beats Bob in at least $\\lceil \\frac{n}{2} \\rceil$ ($\\frac{n}{2}$ rounded up to the nearest integer) hands, otherwise Alice loses.\n\nNote that in rock-paper-scissors:\n\n rock beats scissors; paper beats rock; scissors beat paper. \n\nThe task is, given the sequence of hands that Bob will play, and the numbers $a, b, c$, determine whether or not Alice can win. And if so, find any possible sequence of hands that Alice can use to win.\n\nIf there are multiple answers, print any of them.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThen, $t$ testcases follow, each consisting of three lines: The first line contains a single integer $n$ ($1 \\le n \\le 100$). The second line contains three integers, $a, b, c$ ($0 \\le a, b, c \\le n$). It is guaranteed that $a + b + c = n$. The third line contains a string $s$ of length $n$. $s$ is made up of only 'R', 'P', and 'S'. The $i$-th character is 'R' if for his $i$-th Bob plays rock, 'P' if paper, and 'S' if scissors. \n\n\n-----Output-----\n\nFor each testcase: If Alice cannot win, print \"NO\" (without the quotes). Otherwise, print \"YES\" (without the quotes). Also, print a string $t$ of length $n$ made up of only 'R', 'P', and 'S' \u2014 a sequence of hands that Alice can use to win. $t$ must contain exactly $a$ 'R's, $b$ 'P's, and $c$ 'S's. If there are multiple answers, print any of them. \n\nThe \"YES\" / \"NO\" part of the output is case-insensitive (i.e. \"yEs\", \"no\" or \"YEs\" are all valid answers). Note that 'R', 'P' and 'S' are case-sensitive.\n\n\n-----Example-----\nInput\n2\n3\n1 1 1\nRPS\n3\n3 0 0\nRPS\n\nOutput\nYES\nPSR\nNO\n\n\n\n-----Note-----\n\nIn the first testcase, in the first hand, Alice plays paper and Bob plays rock, so Alice beats Bob. In the second hand, Alice plays scissors and Bob plays paper, so Alice beats Bob. In the third hand, Alice plays rock and Bob plays scissors, so Alice beats Bob. Alice beat Bob 3 times, and $3 \\ge \\lceil \\frac{3}{2} \\rceil = 2$, so Alice wins.\n\nIn the second testcase, the only sequence of hands that Alice can play is \"RRR\". Alice beats Bob only in the last hand, so Alice can't win. $1 < \\lceil \\frac{3}{2} \\rceil = 2$."} +{"id": "02449", "text": "You are given an integer m.\n\nLet M = 2^{m} - 1.\n\nYou are also given a set of n integers denoted as the set T. The integers will be provided in base 2 as n binary strings of length m.\n\nA set of integers S is called \"good\" if the following hold. If $a \\in S$, then [Image]. If $a, b \\in S$, then $a \\text{AND} b \\in S$ $T \\subseteq S$ All elements of S are less than or equal to M. \n\nHere, $XOR$ and $\\text{AND}$ refer to the bitwise XOR and bitwise AND operators, respectively.\n\nCount the number of good sets S, modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line will contain two integers m and n (1 \u2264 m \u2264 1 000, 1 \u2264 n \u2264 min(2^{m}, 50)).\n\nThe next n lines will contain the elements of T. Each line will contain exactly m zeros and ones. Elements of T will be distinct.\n\n\n-----Output-----\n\nPrint a single integer, the number of good sets modulo 10^9 + 7. \n\n\n-----Examples-----\nInput\n5 3\n11010\n00101\n11000\n\nOutput\n4\n\nInput\n30 2\n010101010101010010101010101010\n110110110110110011011011011011\n\nOutput\n860616440\n\n\n\n-----Note-----\n\nAn example of a valid set S is {00000, 00101, 00010, 00111, 11000, 11010, 11101, 11111}."} +{"id": "02450", "text": "You might have remembered Theatre square from the problem 1A. Now it's finally getting repaved.\n\nThe square still has a rectangular shape of $n \\times m$ meters. However, the picture is about to get more complicated now. Let $a_{i,j}$ be the $j$-th square in the $i$-th row of the pavement.\n\nYou are given the picture of the squares: if $a_{i,j} = $ \"*\", then the $j$-th square in the $i$-th row should be black; if $a_{i,j} = $ \".\", then the $j$-th square in the $i$-th row should be white. \n\nThe black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: $1 \\times 1$ tiles\u00a0\u2014 each tile costs $x$ burles and covers exactly $1$ square; $1 \\times 2$ tiles\u00a0\u2014 each tile costs $y$ burles and covers exactly $2$ adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into $1 \\times 1$ tiles. \n\nYou should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.\n\nWhat is the smallest total price of the tiles needed to cover all the white squares?\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 500$)\u00a0\u2014 the number of testcases. Then the description of $t$ testcases follow.\n\nThe first line of each testcase contains four integers $n$, $m$, $x$ and $y$ ($1 \\le n \\le 100$; $1 \\le m \\le 1000$; $1 \\le x, y \\le 1000$)\u00a0\u2014 the size of the Theatre square, the price of the $1 \\times 1$ tile and the price of the $1 \\times 2$ tile.\n\nEach of the next $n$ lines contains $m$ characters. The $j$-th character in the $i$-th line is $a_{i,j}$. If $a_{i,j} = $ \"*\", then the $j$-th square in the $i$-th row should be black, and if $a_{i,j} = $ \".\", then the $j$-th square in the $i$-th row should be white.\n\nIt's guaranteed that the sum of $n \\times m$ over all testcases doesn't exceed $10^5$.\n\n\n-----Output-----\n\nFor each testcase print a single integer\u00a0\u2014 the smallest total price of the tiles needed to cover all the white squares in burles.\n\n\n-----Example-----\nInput\n4\n1 1 10 1\n.\n1 2 10 1\n..\n2 1 10 1\n.\n.\n3 3 3 7\n..*\n*..\n.*.\n\nOutput\n10\n1\n20\n18\n\n\n\n-----Note-----\n\nIn the first testcase you are required to use a single $1 \\times 1$ tile, even though $1 \\times 2$ tile is cheaper. So the total price is $10$ burles.\n\nIn the second testcase you can either use two $1 \\times 1$ tiles and spend $20$ burles or use a single $1 \\times 2$ tile and spend $1$ burle. The second option is cheaper, thus the answer is $1$.\n\nThe third testcase shows that you can't rotate $1 \\times 2$ tiles. You still have to use two $1 \\times 1$ tiles for the total price of $20$.\n\nIn the fourth testcase the cheapest way is to use $1 \\times 1$ tiles everywhere. The total cost is $6 \\cdot 3 = 18$."} +{"id": "02451", "text": "You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.\n\nThe building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 \u2264 i \u2264 n - 1) on every floor x, where a \u2264 x \u2264 b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building.\n\n [Image]\n\nThe picture illustrates the first example. \n\nYou have given k pairs of locations (t_{a}, f_{a}), (t_{b}, f_{b}): floor f_{a} of tower t_{a} and floor f_{b} of tower t_{b}. For each pair you need to determine the minimum walking time between these locations.\n\n\n-----Input-----\n\nThe first line of the input contains following integers:\n\n n: the number of towers in the building (1 \u2264 n \u2264 10^8), h: the number of floors in each tower (1 \u2264 h \u2264 10^8), a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 \u2264 a \u2264 b \u2264 h), k: total number of queries (1 \u2264 k \u2264 10^4). \n\nNext k lines contain description of the queries. Each description consists of four integers t_{a}, f_{a}, t_{b}, f_{b} (1 \u2264 t_{a}, t_{b} \u2264 n, 1 \u2264 f_{a}, f_{b} \u2264 h). This corresponds to a query to find the minimum travel time between f_{a}-th floor of the t_{a}-th tower and f_{b}-th floor of the t_{b}-th tower.\n\n\n-----Output-----\n\nFor each query print a single integer: the minimum walking time between the locations in minutes.\n\n\n-----Example-----\nInput\n3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3\n\nOutput\n1\n4\n2"} +{"id": "02452", "text": "A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).\n\nFor a positive integer $n$, we call a permutation $p$ of length $n$ good if the following condition holds for every pair $i$ and $j$ ($1 \\le i \\le j \\le n$)\u00a0\u2014 $(p_i \\text{ OR } p_{i+1} \\text{ OR } \\ldots \\text{ OR } p_{j-1} \\text{ OR } p_{j}) \\ge j-i+1$, where $\\text{OR}$ denotes the bitwise OR operation. \n\nIn other words, a permutation $p$ is good if for every subarray of $p$, the $\\text{OR}$ of all elements in it is not less than the number of elements in that subarray. \n\nGiven a positive integer $n$, output any good permutation of length $n$. We can show that for the given constraints such a permutation always exists.\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 100$). Description of the test cases follows.\n\nThe first and only line of every test case contains a single integer $n$ ($1 \\le n \\le 100$).\n\n\n-----Output-----\n\nFor every test, output any good permutation of length $n$ on a separate line. \n\n\n-----Example-----\nInput\n3\n1\n3\n7\n\nOutput\n1\n3 1 2\n4 3 5 2 7 1 6\n\n\n\n-----Note-----\n\nFor $n = 3$, $[3,1,2]$ is a good permutation. Some of the subarrays are listed below. $3\\text{ OR }1 = 3 \\geq 2$ $(i = 1,j = 2)$ $3\\text{ OR }1\\text{ OR }2 = 3 \\geq 3$ $(i = 1,j = 3)$ $1\\text{ OR }2 = 3 \\geq 2$ $(i = 2,j = 3)$ $1 \\geq 1$ $(i = 2,j = 2)$ \n\nSimilarly, you can verify that $[4,3,5,2,7,1,6]$ is also good."} +{"id": "02453", "text": "You are given $n$ segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.\n\nYour task is the following: for every $k \\in [1..n]$, calculate the number of points with integer coordinates such that the number of segments that cover these points equals $k$. A segment with endpoints $l_i$ and $r_i$ covers point $x$ if and only if $l_i \\le x \\le r_i$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of segments.\n\nThe next $n$ lines contain segments. The $i$-th line contains a pair of integers $l_i, r_i$ ($0 \\le l_i \\le r_i \\le 10^{18}$) \u2014 the endpoints of the $i$-th segment.\n\n\n-----Output-----\n\nPrint $n$ space separated integers $cnt_1, cnt_2, \\dots, cnt_n$, where $cnt_i$ is equal to the number of points such that the number of segments that cover these points equals to $i$.\n\n\n-----Examples-----\nInput\n3\n0 3\n1 3\n3 8\n\nOutput\n6 2 1 \n\nInput\n3\n1 3\n2 4\n5 7\n\nOutput\n5 2 0 \n\n\n\n-----Note-----\n\nThe picture describing the first example:\n\n[Image]\n\nPoints with coordinates $[0, 4, 5, 6, 7, 8]$ are covered by one segment, points $[1, 2]$ are covered by two segments and point $[3]$ is covered by three segments.\n\nThe picture describing the second example:\n\n[Image]\n\nPoints $[1, 4, 5, 6, 7]$ are covered by one segment, points $[2, 3]$ are covered by two segments and there are no points covered by three segments."} +{"id": "02454", "text": "John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N.\n\nJohn first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules.\n\nTo select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey.\n\nHe also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey.\n\nIn how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 10^9 + 7.\n\n\n-----Input-----\n\nIn the first line, a positive integer N (1 \u2264 N \u2264 100000), the number of cities. \n\nIn the next N - 1 lines, two numbers a, b (1 \u2264 a, b \u2264 N) separated by a single space meaning that cities a and b are connected by a bidirectional road.\n\n\n-----Output-----\n\nOutput one number, the answer to the problem modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n4\n\nInput\n3\n1 2\n2 3\n\nOutput\n10\n\n\n\n-----Note-----\n\nExample 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4.\n\nExample 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4 + 2 + 4 = 10 options."} +{"id": "02455", "text": "There always is something to choose from! And now, instead of \"Noughts and Crosses\", Inna choose a very unusual upgrade of this game. The rules of the game are given below:\n\nThere is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: \"X\" or \"O\". Then the player chooses two positive integers a and b (a\u00b7b = 12), after that he makes a table of size a \u00d7 b from the cards he put on the table as follows: the first b cards form the first row of the table, the second b cards form the second row of the table and so on, the last b cards form the last (number a) row of the table. The player wins if some column of the table contain characters \"X\" on all cards. Otherwise, the player loses.\n\nInna has already put 12 cards on the table in a row. But unfortunately, she doesn't know what numbers a and b to choose. Help her win the game: print to her all the possible ways of numbers a, b that she can choose and win.\n\n\n-----Input-----\n\nThe first line of the input contains integer t (1 \u2264 t \u2264 100). This value shows the number of sets of test data in the input. Next follows the description of each of the t tests on a separate line.\n\nThe description of each test is a string consisting of 12 characters, each character is either \"X\", or \"O\". The i-th character of the string shows the character that is written on the i-th card from the start.\n\n\n-----Output-----\n\nFor each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair a, b. Next, print on this line the pairs in the format axb. Print the pairs in the order of increasing first parameter (a). Separate the pairs in the line by whitespaces.\n\n\n-----Examples-----\nInput\n4\nOXXXOXOOXOOX\nOXOXOXOXOXOX\nXXXXXXXXXXXX\nOOOOOOOOOOOO\n\nOutput\n3 1x12 2x6 4x3\n4 1x12 2x6 3x4 6x2\n6 1x12 2x6 3x4 4x3 6x2 12x1\n0"} +{"id": "02456", "text": "A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily $7$ days!\n\nIn detail, she can choose any integer $k$ which satisfies $1 \\leq k \\leq r$, and set $k$ days as the number of days in a week.\n\nAlice is going to paint some $n$ consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.\n\nShe wants to make all of the painted cells to be connected by side. It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side.\n\nAlice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar's sides.\n\nFor example, in the picture, a week has $4$ days and Alice paints $5$ consecutive days. [1] and [2] are different shapes, but [1] and [3] are equal shapes. [Image] \n\nAlice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive $n$ days and paints them in calendar started in one of the days of the week. As was said before, she considers only shapes, there all cells are connected by side.\n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases. Next $t$ lines contain descriptions of test cases.\n\nFor each test case, the only line contains two integers $n$, $r$ ($1 \\le n \\le 10^9, 1 \\le r \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case, print a single integer \u00a0\u2014 the answer to the problem.\n\nPlease note, that the answer for some test cases won't fit into $32$-bit integer type, so you should use at least $64$-bit integer type in your programming language.\n\n\n-----Example-----\nInput\n5\n3 4\n3 2\n3 1\n13 7\n1010000 9999999\n\nOutput\n4\n3\n1\n28\n510049495001\n\n\n\n-----Note-----\n\nIn the first test case, Alice can set $1,2,3$ or $4$ days as the number of days in a week.\n\nThere are $6$ possible paintings shown in the picture, but there are only $4$ different shapes. So, the answer is $4$. Notice that the last example in the picture is an invalid painting because all cells are not connected by sides. [Image] \n\nIn the last test case, be careful with the overflow issue, described in the output format."} +{"id": "02457", "text": "Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.\n\nIn total, Nastya dropped $n$ grains. Nastya read that each grain weighs some integer number of grams from $a - b$ to $a + b$, inclusive (numbers $a$ and $b$ are known), and the whole package of $n$ grains weighs from $c - d$ to $c + d$ grams, inclusive (numbers $c$ and $d$ are known). The weight of the package is the sum of the weights of all $n$ grains in it.\n\nHelp Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $i$-th grain weighs some integer number $x_i$ $(a - b \\leq x_i \\leq a + b)$, and in total they weigh from $c - d$ to $c + d$, inclusive ($c - d \\leq \\sum\\limits_{i=1}^{n}{x_i} \\leq c + d$).\n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains a single integer $t$ $(1 \\leq t \\leq 1000)$ \u00a0\u2014 the number of test cases. \n\nThe next $t$ lines contain descriptions of the test cases, each line contains $5$ integers: $n$ $(1 \\leq n \\leq 1000)$ \u00a0\u2014 the number of grains that Nastya counted and $a, b, c, d$ $(0 \\leq b < a \\leq 1000, 0 \\leq d < c \\leq 1000)$ \u00a0\u2014 numbers that determine the possible weight of one grain of rice (from $a - b$ to $a + b$) and the possible total weight of the package (from $c - d$ to $c + d$).\n\n\n-----Output-----\n\nFor each test case given in the input print \"Yes\", if the information about the weights is not inconsistent, and print \"No\" if $n$ grains with masses from $a - b$ to $a + b$ cannot make a package with a total mass from $c - d$ to $c + d$.\n\n\n-----Example-----\nInput\n5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1\n\nOutput\nYes\nNo\nYes\nNo\nYes\n\n\n\n-----Note-----\n\nIn the first test case of the example, we can assume that each grain weighs $17$ grams, and a pack $119$ grams, then really Nastya could collect the whole pack.\n\nIn the third test case of the example, we can assume that each grain weighs $16$ grams, and a pack $128$ grams, then really Nastya could collect the whole pack.\n\nIn the fifth test case of the example, we can be assumed that $3$ grains of rice weigh $2$, $2$, and $3$ grams, and a pack is $7$ grams, then really Nastya could collect the whole pack.\n\nIn the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains."} +{"id": "02458", "text": "We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.\n\nBut, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.\n\nNow Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nInput contains several test cases.\n\nThe first line contains two integers t and k (1 \u2264 t, k \u2264 10^5), where t represents the number of test cases.\n\nThe next t lines contain two integers a_{i} and b_{i} (1 \u2264 a_{i} \u2264 b_{i} \u2264 10^5), describing the i-th test.\n\n\n-----Output-----\n\nPrint t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between a_{i} and b_{i} flowers at dinner modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n3 2\n1 3\n2 3\n4 4\n\nOutput\n6\n5\n5\n\n\n\n-----Note----- For K = 2 and length 1 Marmot can eat (R). For K = 2 and length 2 Marmot can eat (RR) and (WW). For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR). For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR)."} +{"id": "02459", "text": "You are given an array a of size n, and q queries to it. There are queries of two types: 1 l_{i} r_{i} \u2014 perform a cyclic shift of the segment [l_{i}, r_{i}] to the right. That is, for every x such that l_{i} \u2264 x < r_{i} new value of a_{x} + 1 becomes equal to old value of a_{x}, and new value of a_{l}_{i} becomes equal to old value of a_{r}_{i}; 2 l_{i} r_{i} \u2014 reverse the segment [l_{i}, r_{i}]. \n\nThere are m important indices in the array b_1, b_2, ..., b_{m}. For each i such that 1 \u2264 i \u2264 m you have to output the number that will have index b_{i} in the array after all queries are performed.\n\n\n-----Input-----\n\nThe first line contains three integer numbers n, q and m (1 \u2264 n, q \u2264 2\u00b710^5, 1 \u2264 m \u2264 100). \n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9). \n\nThen q lines follow. i-th of them contains three integer numbers t_{i}, l_{i}, r_{i}, where t_{i} is the type of i-th query, and [l_{i}, r_{i}] is the segment where this query is performed (1 \u2264 t_{i} \u2264 2, 1 \u2264 l_{i} \u2264 r_{i} \u2264 n). \n\nThe last line contains m integer numbers b_1, b_2, ..., b_{m} (1 \u2264 b_{i} \u2264 n) \u2014 important indices of the array. \n\n\n-----Output-----\n\nPrint m numbers, i-th of which is equal to the number at index b_{i} after all queries are done.\n\n\n-----Example-----\nInput\n6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3\n\nOutput\n3 3 1 5 2"} +{"id": "02460", "text": "Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5.\n\nLyft has become so popular so that it is now used by all $m$ taxi drivers in the city, who every day transport the rest of the city residents\u00a0\u2014 $n$ riders.\n\nEach resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same).\n\nThe Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected.\n\nBut one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver $i$ the number $a_{i}$\u00a0\u2014 the number of riders that would call the $i$-th taxi driver when all drivers and riders are at their home?\n\nThe taxi driver can neither transport himself nor other taxi drivers.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n,m \\le 10^5$)\u00a0\u2014 number of riders and taxi drivers.\n\nThe second line contains $n + m$ integers $x_1, x_2, \\ldots, x_{n+m}$ ($1 \\le x_1 < x_2 < \\ldots < x_{n+m} \\le 10^9$), where $x_i$ is the coordinate where the $i$-th resident lives. \n\nThe third line contains $n + m$ integers $t_1, t_2, \\ldots, t_{n+m}$ ($0 \\le t_i \\le 1$). If $t_i = 1$, then the $i$-th resident is a taxi driver, otherwise $t_i = 0$.\n\nIt is guaranteed that the number of $i$ such that $t_i = 1$ is equal to $m$.\n\n\n-----Output-----\n\nPrint $m$ integers $a_1, a_2, \\ldots, a_{m}$, where $a_i$ is the answer for the $i$-th taxi driver. The taxi driver has the number $i$ if among all the taxi drivers he lives in the $i$-th smallest coordinate (see examples for better understanding).\n\n\n-----Examples-----\nInput\n3 1\n1 2 3 10\n0 0 1 0\n\nOutput\n3 \nInput\n3 2\n2 3 4 5 6\n1 0 0 0 1\n\nOutput\n2 1 \nInput\n1 4\n2 4 6 10 15\n1 1 1 1 0\n\nOutput\n0 0 0 1 \n\n\n-----Note-----\n\nIn the first example, we have only one taxi driver, which means an order from any of $n$ riders will go to him.\n\nIn the second example, the first taxi driver lives at the point with the coordinate $2$, and the second one lives at the point with the coordinate $6$. Obviously, the nearest taxi driver to the rider who lives on the $3$ coordinate is the first one, and to the rider who lives on the coordinate $5$ is the second one. The rider who lives on the $4$ coordinate has the same distance to the first and the second taxi drivers, but since the first taxi driver has a smaller coordinate, the call from this rider will go to the first taxi driver.\n\nIn the third example, we have one rider and the taxi driver nearest to him is the fourth one."} +{"id": "02461", "text": "Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to a_{i}.\n\nIlya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.\n\nFor each vertex the answer must be considered independently.\n\nThe beauty of the root equals to number written on it.\n\n\n-----Input-----\n\nFirst line contains one integer number n\u00a0\u2014 the number of vertices in tree (1 \u2264 n \u2264 2\u00b710^5).\n\nNext line contains n integer numbers a_{i} (1 \u2264 i \u2264 n, 1 \u2264 a_{i} \u2264 2\u00b710^5).\n\nEach of next n - 1 lines contains two integer numbers x and y (1 \u2264 x, y \u2264 n, x \u2260 y), which means that there is an edge (x, y) in the tree.\n\n\n-----Output-----\n\nOutput n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.\n\n\n-----Examples-----\nInput\n2\n6 2\n1 2\n\nOutput\n6 6 \n\nInput\n3\n6 2 3\n1 2\n1 3\n\nOutput\n6 6 6 \n\nInput\n1\n10\n\nOutput\n10"} +{"id": "02462", "text": "Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task.\n\nRecently, out of blue Captain Flint has been interested in math and even defined a new class of integers. Let's define a positive integer $x$ as nearly prime if it can be represented as $p \\cdot q$, where $1 < p < q$ and $p$ and $q$ are prime numbers. For example, integers $6$ and $10$ are nearly primes (since $2 \\cdot 3 = 6$ and $2 \\cdot 5 = 10$), but integers $1$, $3$, $4$, $16$, $17$ or $44$ are not.\n\nCaptain Flint guessed an integer $n$ and asked you: can you represent it as the sum of $4$ different positive integers where at least $3$ of them should be nearly prime.\n\nUncle Bogdan easily solved the task and joined the crew. Can you do the same?\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases.\n\nNext $t$ lines contain test cases\u00a0\u2014 one per line. The first and only line of each test case contains the single integer $n$ $(1 \\le n \\le 2 \\cdot 10^5)$\u00a0\u2014 the number Flint guessed.\n\n\n-----Output-----\n\nFor each test case print: YES and $4$ different positive integers such that at least $3$ of them are nearly prime and their sum is equal to $n$ (if there are multiple answers print any of them); NO if there is no way to represent $n$ as the sum of $4$ different positive integers where at least $3$ of them are nearly prime. You can print each character of YES or NO in any case.\n\n\n-----Example-----\nInput\n7\n7\n23\n31\n36\n44\n100\n258\n\nOutput\nNO\nNO\nYES\n14 10 6 1\nYES\n5 6 10 15\nYES\n6 7 10 21\nYES\n2 10 33 55\nYES\n10 21 221 6\n\n\n-----Note-----\n\nIn the first and second test cases, it can be proven that there are no four different positive integers such that at least three of them are nearly prime.\n\nIn the third test case, $n=31=2 \\cdot 7 + 2 \\cdot 5 + 2 \\cdot 3 + 1$: integers $14$, $10$, $6$ are nearly prime.\n\nIn the fourth test case, $n=36=5 + 2 \\cdot 3 + 2 \\cdot 5 + 3 \\cdot 5$: integers $6$, $10$, $15$ are nearly prime.\n\nIn the fifth test case, $n=44=2 \\cdot 3 + 7 + 2 \\cdot 5 + 3 \\cdot 7$: integers $6$, $10$, $21$ are nearly prime.\n\nIn the sixth test case, $n=100=2 + 2 \\cdot 5 + 3 \\cdot 11 + 5 \\cdot 11$: integers $10$, $33$, $55$ are nearly prime.\n\nIn the seventh test case, $n=258=2 \\cdot 5 + 3 \\cdot 7 + 13 \\cdot 17 + 2 \\cdot 3$: integers $10$, $21$, $221$, $6$ are nearly prime."} +{"id": "02463", "text": "This is the easy version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem.\n\nToday is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version all prices are different.\n\nAn ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.\n\nYou can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ $(1 \\le n \\le 10^5)$\u00a0\u2014 the number of ice spheres in the shop.\n\nThe second line contains $n$ different integers $a_1, a_2, \\dots, a_n$ $(1 \\le a_i \\le 10^9)$\u00a0\u2014 the prices of ice spheres.\n\n\n-----Output-----\n\nIn the first line print the maximum number of ice spheres that Sage can buy.\n\nIn the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.\n\n\n-----Example-----\nInput\n5\n1 2 3 4 5\n\nOutput\n2\n3 1 4 2 5 \n\n\n-----Note-----\n\nIn the example it's not possible to place ice spheres in any order so that Sage would buy $3$ of them. If the ice spheres are placed like this $(3, 1, 4, 2, 5)$, then Sage will buy two spheres: one for $1$ and one for $2$, because they are cheap."} +{"id": "02464", "text": "You are given a tree (an undirected connected acyclic graph) consisting of $n$ vertices and $n - 1$ edges. A number is written on each edge, each number is either $0$ (let's call such edges $0$-edges) or $1$ (those are $1$-edges).\n\nLet's call an ordered pair of vertices $(x, y)$ ($x \\ne y$) valid if, while traversing the simple path from $x$ to $y$, we never go through a $0$-edge after going through a $1$-edge. Your task is to calculate the number of valid pairs in the tree.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 200000$) \u2014 the number of vertices in the tree.\n\nThen $n - 1$ lines follow, each denoting an edge of the tree. Each edge is represented by three integers $x_i$, $y_i$ and $c_i$ ($1 \\le x_i, y_i \\le n$, $0 \\le c_i \\le 1$, $x_i \\ne y_i$) \u2014 the vertices connected by this edge and the number written on it, respectively.\n\nIt is guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of valid pairs of vertices.\n\n\n-----Example-----\nInput\n7\n2 1 1\n3 2 0\n4 2 1\n5 2 0\n6 7 1\n7 2 1\n\nOutput\n34\n\n\n\n-----Note-----\n\nThe picture corresponding to the first example:\n\n[Image]"} +{"id": "02465", "text": "You are given an angle $\\text{ang}$. \n\nThe Jury asks You to find such regular $n$-gon (regular polygon with $n$ vertices) that it has three vertices $a$, $b$ and $c$ (they can be non-consecutive) with $\\angle{abc} = \\text{ang}$ or report that there is no such $n$-gon. [Image] \n\nIf there are several answers, print the minimal one. It is guarantied that if answer exists then it doesn't exceed $998244353$.\n\n\n-----Input-----\n\nThe first line contains single integer $T$ ($1 \\le T \\le 180$) \u2014 the number of queries. \n\nEach of the next $T$ lines contains one integer $\\text{ang}$ ($1 \\le \\text{ang} < 180$) \u2014 the angle measured in degrees. \n\n\n-----Output-----\n\nFor each query print single integer $n$ ($3 \\le n \\le 998244353$) \u2014 minimal possible number of vertices in the regular $n$-gon or $-1$ if there is no such $n$.\n\n\n-----Example-----\nInput\n4\n54\n50\n2\n178\n\nOutput\n10\n18\n90\n180\n\n\n\n-----Note-----\n\nThe answer for the first query is on the picture above.\n\nThe answer for the second query is reached on a regular $18$-gon. For example, $\\angle{v_2 v_1 v_6} = 50^{\\circ}$.\n\nThe example angle for the third query is $\\angle{v_{11} v_{10} v_{12}} = 2^{\\circ}$.\n\nIn the fourth query, minimal possible $n$ is $180$ (not $90$)."} +{"id": "02466", "text": "Given a collection of distinct integers, return all possible permutations.\n\nExample:\n\n\nInput: [1,2,3]\nOutput:\n[\n [1,2,3],\n [1,3,2],\n [2,1,3],\n [2,3,1],\n [3,1,2],\n [3,2,1]\n]"} +{"id": "02467", "text": "Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.\n\nNote:\n\n\n All numbers will be positive integers.\n The solution set must not contain duplicate combinations.\n\n\nExample 1:\n\n\nInput: k = 3, n = 7\nOutput: [[1,2,4]]\n\n\nExample 2:\n\n\nInput: k = 3, n = 9\nOutput: [[1,2,6], [1,3,5], [2,3,4]]"} +{"id": "02468", "text": "Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.\n\u00a0\nExample 1:\nInput: s = \"(()\"\nOutput: 2\nExplanation: The longest valid parentheses substring is \"()\".\n\nExample 2:\nInput: s = \")()())\"\nOutput: 4\nExplanation: The longest valid parentheses substring is \"()()\".\n\nExample 3:\nInput: s = \"\"\nOutput: 0\n\n\u00a0\nConstraints:\n\n0 <= s.length <= 3 * 104\ns[i] is '(', or ')'."} +{"id": "02469", "text": "Given an integer array of size n, find all elements that appear more than \u230a n/3 \u230b times.\n\nNote: The algorithm should run in linear time and in O(1) space.\n\nExample 1:\n\n\nInput: [3,2,3]\nOutput: [3]\n\nExample 2:\n\n\nInput: [1,1,1,3,3,2,2,2]\nOutput: [1,2]"} +{"id": "02470", "text": "Given two integer arrays\u00a0arr1 and arr2, return the minimum number of operations (possibly zero) needed\u00a0to make arr1 strictly increasing.\nIn one operation, you can choose two indices\u00a00 <=\u00a0i < arr1.length\u00a0and\u00a00 <= j < arr2.length\u00a0and do the assignment\u00a0arr1[i] = arr2[j].\nIf there is no way to make\u00a0arr1\u00a0strictly increasing,\u00a0return\u00a0-1.\n\u00a0\nExample 1:\nInput: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]\nOutput: 1\nExplanation: Replace 5 with 2, then arr1 = [1, 2, 3, 6, 7].\n\nExample 2:\nInput: arr1 = [1,5,3,6,7], arr2 = [4,3,1]\nOutput: 2\nExplanation: Replace 5 with 3 and then replace 3 with 4. arr1 = [1, 3, 4, 6, 7].\n\nExample 3:\nInput: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]\nOutput: -1\nExplanation: You can't make arr1 strictly increasing.\n\u00a0\nConstraints:\n\n1 <= arr1.length, arr2.length <= 2000\n0 <= arr1[i], arr2[i] <= 10^9"} +{"id": "02471", "text": "We have a grid with H rows and W columns. At first, all cells were painted white.\nSnuke painted N of these cells. The i-th ( 1 \\leq i \\leq N ) cell he painted is the cell at the a_i-th row and b_i-th column.\nCompute the following:\n - For each integer j ( 0 \\leq j \\leq 9 ), how many subrectangles of size 3\u00d73 of the grid contains exactly j black cells, after Snuke painted N cells?\n\n-----Constraints-----\n - 3 \\leq H \\leq 10^9\n - 3 \\leq W \\leq 10^9\n - 0 \\leq N \\leq min(10^5,H\u00d7W)\n - 1 \\leq a_i \\leq H (1 \\leq i \\leq N)\n - 1 \\leq b_i \\leq W (1 \\leq i \\leq N)\n - (a_i, b_i) \\neq (a_j, b_j) (i \\neq j)\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nH W N\na_1 b_1\n:\na_N b_N\n\n-----Output-----\nPrint 10 lines.\nThe (j+1)-th ( 0 \\leq j \\leq 9 ) line should contain the number of the subrectangles of size 3\u00d73 of the grid that contains exactly j black cells.\n\n-----Sample Input-----\n4 5 8\n1 1\n1 4\n1 5\n2 3\n3 1\n3 2\n3 4\n4 4\n\n-----Sample Output-----\n0\n0\n0\n2\n4\n0\n0\n0\n0\n0\n\n\nThere are six subrectangles of size 3\u00d73. Two of them contain three black cells each, and the remaining four contain four black cells each."} +{"id": "02472", "text": "Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.\nLet the current time be time 0. Kizahashi has N jobs numbered 1 to N.\nIt takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.\nKizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.\nCan Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq A_i, B_i \\leq 10^9 (1 \\leq i \\leq N)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 B_1\n.\n.\n.\nA_N B_N\n\n-----Output-----\nIf Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.\n\n-----Sample Input-----\n5\n2 4\n1 9\n1 8\n4 9\n3 12\n\n-----Sample Output-----\nYes\n\nHe can complete all the jobs in time by, for example, doing them in the following order:\n - Do Job 2 from time 0 to 1.\n - Do Job 1 from time 1 to 3.\n - Do Job 4 from time 3 to 7.\n - Do Job 3 from time 7 to 8.\n - Do Job 5 from time 8 to 11.\nNote that it is fine to complete Job 3 exactly at the deadline, time 8."} +{"id": "02473", "text": "We have N points in a two-dimensional plane.\n\nThe coordinates of the i-th point (1 \\leq i \\leq N) are (x_i,y_i).\n\nLet us consider a rectangle whose sides are parallel to the coordinate axes that contains K or more of the N points in its interior.\n\nHere, points on the sides of the rectangle are considered to be in the interior.\n\nFind the minimum possible area of such a rectangle. \n\n-----Constraints-----\n - 2 \\leq K \\leq N \\leq 50 \n - -10^9 \\leq x_i,y_i \\leq 10^9 (1 \\leq i \\leq N) \n - x_i\u2260x_j (1 \\leq i B holds.\nAt least how many explosions do you need to cause in order to vanish all the monsters?\n\n-----Constraints-----\n - All input values are integers.\n - 1 \u2264 N \u2264 10^5\n - 1 \u2264 B < A \u2264 10^9\n - 1 \u2264 h_i \u2264 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN A B\nh_1\nh_2\n:\nh_N\n\n-----Output-----\nPrint the minimum number of explosions that needs to be caused in order to vanish all the monsters.\n\n-----Sample Input-----\n4 5 3\n8\n7\n4\n2\n\n-----Sample Output-----\n2\n\nYou can vanish all the monsters in two explosion, as follows:\n - First, cause an explosion centered at the monster with 8 health. The healths of the four monsters become 3, 4, 1 and -1, respectively, and the last monster vanishes.\n - Second, cause an explosion centered at the monster with 4 health remaining. The healths of the three remaining monsters become 0, -1 and -2, respectively, and all the monsters are now vanished."} +{"id": "02519", "text": "We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.\nWe will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.\n\n-----Constraints-----\n - 1 \u2264 K \u2264 N \u2264 200000\n - 1 \u2264 p_i \u2264 1000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\np_1 ... p_N\n\n-----Output-----\nPrint the maximum possible value of the expected value of the sum of the numbers shown.\nYour output will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.\n\n-----Sample Input-----\n5 3\n1 2 2 4 5\n\n-----Sample Output-----\n7.000000000000\n\nWhen we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve."} +{"id": "02520", "text": "An SNS has N users - User 1, User 2, \\cdots, User N.\nBetween these N users, there are some relationships - M friendships and K blockships.\nFor each i = 1, 2, \\cdots, M, there is a bidirectional friendship between User A_i and User B_i.\nFor each i = 1, 2, \\cdots, K, there is a bidirectional blockship between User C_i and User D_i.\nWe define User a to be a friend candidate for User b when all of the following four conditions are satisfied:\n - a \\neq b.\n - There is not a friendship between User a and User b.\n - There is not a blockship between User a and User b.\n - There exists a sequence c_0, c_1, c_2, \\cdots, c_L consisting of integers between 1 and N (inclusive) such that c_0 = a, c_L = b, and there is a friendship between User c_i and c_{i+1} for each i = 0, 1, \\cdots, L - 1.\nFor each user i = 1, 2, ... N, how many friend candidates does it have?\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \u2264 N \u2264 10^5\n - 0 \\leq M \\leq 10^5\n - 0 \\leq K \\leq 10^5\n - 1 \\leq A_i, B_i \\leq N\n - A_i \\neq B_i\n - 1 \\leq C_i, D_i \\leq N\n - C_i \\neq D_i\n - (A_i, B_i) \\neq (A_j, B_j) (i \\neq j)\n - (A_i, B_i) \\neq (B_j, A_j)\n - (C_i, D_i) \\neq (C_j, D_j) (i \\neq j)\n - (C_i, D_i) \\neq (D_j, C_j)\n - (A_i, B_i) \\neq (C_j, D_j)\n - (A_i, B_i) \\neq (D_j, C_j)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M K\nA_1 B_1\n\\vdots\nA_M B_M\nC_1 D_1\n\\vdots\nC_K D_K\n\n-----Output-----\nPrint the answers in order, with space in between.\n\n-----Sample Input-----\n4 4 1\n2 1\n1 3\n3 2\n3 4\n4 1\n\n-----Sample Output-----\n0 1 0 1\n\nThere is a friendship between User 2 and 3, and between 3 and 4. Also, there is no friendship or blockship between User 2 and 4. Thus, User 4 is a friend candidate for User 2.\nHowever, neither User 1 or 3 is a friend candidate for User 2, so User 2 has one friend candidate."} +{"id": "02521", "text": "Let N be a positive integer.\nThere is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).\nSnuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.\nHere, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').\nFind the maximum possible score of a'.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 10^5\n - a_i is an integer.\n - 1 \u2264 a_i \u2264 10^9\n\n-----Partial Score-----\n - In the test set worth 300 points, N \u2264 1000.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_{3N}\n\n-----Output-----\nPrint the maximum possible score of a'.\n\n-----Sample Input-----\n2\n3 1 4 1 5 9\n\n-----Sample Output-----\n1\n\nWhen a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1."} +{"id": "02522", "text": "Given are two sequences A and B, both of length N.\nA and B are each sorted in the ascending order.\nCheck if it is possible to reorder the terms of B so that for each i (1 \\leq i \\leq N) A_i \\neq B_i holds, and if it is possible, output any of the reorderings that achieve it.\n\n-----Constraints-----\n - 1\\leq N \\leq 2 \\times 10^5\n - 1\\leq A_i,B_i \\leq N\n - A and B are each sorted in the ascending order.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 \\cdots A_N\nB_1 B_2 \\cdots B_N\n\n-----Output-----\nIf there exist no reorderings that satisfy the condition, print No.\nIf there exists a reordering that satisfies the condition, print Yes on the first line.\nAfter that, print a reordering of B on the second line, separating terms with a whitespace.\nIf there are multiple reorderings that satisfy the condition, you can print any of them.\n\n-----Sample Input-----\n6\n1 1 1 2 2 3\n1 1 1 2 2 3\n\n-----Sample Output-----\nYes\n2 2 3 1 1 1\n"} +{"id": "02523", "text": "You are given a string S consisting of 0 and 1.\nFind the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times.\n - Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\\geq K must be satisfied). For each integer i such that l\\leq i\\leq r, do the following: if S_i is 0, replace it with 1; if S_i is 1, replace it with 0.\n\n-----Constraints-----\n - 1\\leq |S|\\leq 10^5\n - S_i(1\\leq i\\leq N) is either 0 or 1.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times.\n\n-----Sample Input-----\n010\n\n-----Sample Output-----\n2\n\nWe can turn all the characters of S into 0 by the following operations:\n - Perform the operation on the segment S[1,3] with length 3. S is now 101.\n - Perform the operation on the segment S[1,2] with length 2. S is now 011.\n - Perform the operation on the segment S[2,3] with length 2. S is now 000."} +{"id": "02524", "text": "We have N integers. The i-th integer is A_i.\nFind \\sum_{i=1}^{N-1}\\sum_{j=i+1}^{N} (A_i \\mbox{ XOR } A_j), modulo (10^9+7).What is \\mbox{ XOR }?\n\nThe XOR of integers A and B, A \\mbox{ XOR } B, is defined as follows:\n\n - When A \\mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.\nFor example, 3 \\mbox{ XOR } 5 = 6. (In base two: 011 \\mbox{ XOR } 101 = 110.)\n\n\n-----Constraints-----\n - 2 \\leq N \\leq 3 \\times 10^5\n - 0 \\leq A_i < 2^{60}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the value \\sum_{i=1}^{N-1}\\sum_{j=i+1}^{N} (A_i \\mbox{ XOR } A_j), modulo (10^9+7).\n\n-----Sample Input-----\n3\n1 2 3\n\n-----Sample Output-----\n6\n\nWe have (1\\mbox{ XOR } 2)+(1\\mbox{ XOR } 3)+(2\\mbox{ XOR } 3)=3+2+1=6."} +{"id": "02525", "text": "Takahashi has a string S consisting of lowercase English letters.\nStarting with this string, he will produce a new one in the procedure given as follows.\nThe procedure consists of Q operations. In Operation i (1 \\leq i \\leq Q), an integer T_i is provided, which means the following:\n - If T_i = 1: reverse the string S.\n - If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.\n - If F_i = 1 : Add C_i to the beginning of the string S.\n - If F_i = 2 : Add C_i to the end of the string S.\nHelp Takahashi by finding the final string that results from the procedure.\n\n-----Constraints-----\n - 1 \\leq |S| \\leq 10^5\n - S consists of lowercase English letters.\n - 1 \\leq Q \\leq 2 \\times 10^5\n - T_i = 1 or 2.\n - F_i = 1 or 2, if provided.\n - C_i is a lowercase English letter, if provided.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\nQ\nQuery_1\n:\nQuery_Q\n\nIn the 3-rd through the (Q+2)-th lines, Query_i is one of the following:\n1\n\nwhich means T_i = 1, and:\n2 F_i C_i\n\nwhich means T_i = 2.\n\n-----Output-----\nPrint the resulting string.\n\n-----Sample Input-----\na\n4\n2 1 p\n1\n2 2 c\n1\n\n-----Sample Output-----\ncpa\n\nThere will be Q = 4 operations. Initially, S is a.\n - Operation 1: Add p at the beginning of S. S becomes pa.\n - Operation 2: Reverse S. S becomes ap.\n - Operation 3: Add c at the end of S. S becomes apc.\n - Operation 4: Reverse S. S becomes cpa.\nThus, the resulting string is cpa."} +{"id": "02526", "text": "You are going to eat X red apples and Y green apples.\n\nYou have A red apples of deliciousness p_1,p_2, \\dots, p_A, B green apples of deliciousness q_1,q_2, \\dots, q_B, and C colorless apples of deliciousness r_1,r_2, \\dots, r_C.\n\nBefore eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.\n\nFrom the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.\n\nFind the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.\n\n-----Constraints-----\n - 1 \\leq X \\leq A \\leq 10^5\n - 1 \\leq Y \\leq B \\leq 10^5\n - 1 \\leq C \\leq 10^5\n - 1 \\leq p_i \\leq 10^9\n - 1 \\leq q_i \\leq 10^9\n - 1 \\leq r_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y A B C\np_1 p_2 ... p_A\nq_1 q_2 ... q_B\nr_1 r_2 ... r_C\n\n-----Output-----\nPrint the maximum possible sum of the deliciousness of the eaten apples.\n\n-----Sample Input-----\n1 2 2 2 1\n2 4\n5 1\n3\n\n-----Sample Output-----\n12\n\nThe maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:\n - Eat the 2-nd red apple.\n - Eat the 1-st green apple.\n - Paint the 1-st colorless apple green and eat it."} +{"id": "02527", "text": "Dilku and Bhopu live in Artland. Dilku loves Bhopu and he writes a message : \u201ciloveyou\u201d\non paper and wishes to send it to Bhopu. As Dilku is busy making an artwork, he asks his friend Raj to send the message to Bhopu. However Raj has a condition that he may add/remove some characters and jumble the letters of the message.\nAs Bhopu understands Dilku, she can read \u201ciloveyou\u201d from the message if all the characters of the string \u201ciloveyou\u201d are in the message received by her. Bhopu is happy if she can read \u201ciloveyou\u201d from the message. Otherwise, she is sad. Tell whether Bhopu is happy or sad.\n\n-----Input-----\nInput contains a string S, where S is the message received by Bhopu. String S consists of only lowercase letters.\n\n-----Output-----\nOutput \u201chappy\u201d if Bhopu is happy and \u201csad\u201d if Bhopu is sad.\n\n-----Constraints-----\n1 \u2264 |S| \u2264 100\nWhere |S| denotes length of message string S\n\n-----Example-----\nInput 1:\niloveyou\n\nOutput 1:\nhappy\n\nInput 2:\nulrvysioqjifo\n\nOutput 2:\nsad\n\nInput 3:\nabcvleouioydef\n\nOutput 3:\nhappy"} +{"id": "02528", "text": "Chef loves research! Now he is looking for subarray of maximal length with non-zero product.\nChef has an array A with N elements: A1, A2, ..., AN. \n\nSubarray Aij of array A is elements from index i to index j: Ai, Ai+1, ..., Aj. \nProduct of subarray Aij is product of all its elements (from ith to jth). \n\n-----Input-----\n- First line contains sinlge integer N denoting the number of elements.\n- Second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array. \n\n-----Output-----\n- In a single line print single integer - the maximal length of subarray with non-zero product. \n\n-----Constraints-----\n- 1 \u2264 N \u2264 100000\n- 0 \u2264 Ai \u2264 10000\n\n-----Example-----\nInput:\n6\n1 0 2 3 0 4\n\nOutput:\n2\n\nInput:\n1\n0\n\nOutput:\n0\n\nInput:\n3\n1 0 1\n\nOutput:\n1\n\n-----Explanation-----\nFor the first sample subarray is: {2, 3}. \nFor the second sample there are no subbarays with non-zero product. \nFor the third sample subbarays is {1}, (the first element, or the third one)."} +{"id": "02529", "text": "Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US.\n\nCalculate Pooja's account balance after an attempted transaction. \n\n-----Input-----\nPositive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.\n\nNonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance.\n\n-----Output-----\nOutput the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance.\n\n-----Example - Successful Transaction-----\nInput:\n30 120.00\n\nOutput:\n89.50\n\n-----Example - Incorrect Withdrawal Amount (not multiple of 5)-----\nInput:\n42 120.00\n\nOutput:\n120.00\n\n-----Example - Insufficient Funds-----\nInput:\n300 120.00\n\nOutput:\n120.00"} +{"id": "02530", "text": "Chefs from all over the globe gather each year for an international convention. Each chef represents some country. Please, note that more than one chef can represent a country.\nEach of them presents their best dish to the audience. The audience then sends emails to a secret and secure mail server, with the subject being the name of the chef whom they wish to elect as the \"Chef of the Year\".\nYou will be given the list of the subjects of all the emails. Find the country whose chefs got the most number of votes, and also the chef who got elected as the \"Chef of the Year\" (the chef who got the most number of votes).\nNote 1\nIf several countries got the maximal number of votes, consider the country with the lexicographically smaller name among them to be a winner. Similarly if several chefs got the maximal number of votes, consider the chef with the lexicographically smaller name among them to be a winner.\nNote 2\nThe string A = a1a2...an is called lexicographically smaller then the string B = b1b2...bm in the following two cases:\n- there exists index i \u2264 min{n, m} such that aj = bj for 1 \u2264 j < i and ai < bi;\n- A is a proper prefix of B, that is, n < m and aj = bj for 1 \u2264 j \u2264 n.\nThe characters in strings are compared by their ASCII codes.\nRefer to function strcmp in C or to standard comparator < for string data structure in C++ for details.\n\n-----Input-----\nThe first line of the input contains two space-separated integers N and M denoting the number of chefs and the number of emails respectively. Each of the following N lines contains two space-separated strings, denoting the name of the chef and his country respectively. Each of the following M lines contains one string denoting the subject of the email.\n\n-----Output-----\nOutput should consist of two lines. The first line should contain the name of the country whose chefs got the most number of votes. The second line should contain the name of the chef who is elected as the \"Chef of the Year\".\n\n-----Constraints-----\n- 1 \u2264 N \u2264 10000 (104)\n- 1 \u2264 M \u2264 100000 (105)\n- Each string in the input contains only letters of English alphabets (uppercase or lowercase)\n- Each string in the input has length not exceeding 10\n- All chef names will be distinct\n- Subject of each email will coincide with the name of one of the chefs\n\n-----Example 1-----\nInput:\n1 3\nLeibniz Germany\nLeibniz\nLeibniz\nLeibniz\n\nOutput:\nGermany\nLeibniz\n\n-----Example 2-----\nInput:\n4 5\nRamanujan India\nTorricelli Italy\nGauss Germany\nLagrange Italy\nRamanujan\nTorricelli\nTorricelli\nRamanujan\nLagrange\n\nOutput:\nItaly\nRamanujan\n\n-----Example 3-----\nInput:\n2 2\nNewton England\nEuclid Greece\nNewton\nEuclid\n\nOutput:\nEngland\nEuclid\n\n-----Explanation-----\nExample 1. Here we have only one chef Leibniz and he is from Germany. Clearly, all votes are for him. So Germany is the country-winner and Leibniz is the \"Chef of the Year\".\nExample 2. Here we have chefs Torricelli and Lagrange from Italy, chef Ramanujan from India and chef Gauss from Germany. Torricelli got 2 votes, while Lagrange got one vote. Hence the Italy got 3 votes in all. Ramanujan got also 2 votes. And so India got 2 votes in all. Finally Gauss got no votes leaving Germany without votes. So the country-winner is Italy without any ties. But we have two chefs with 2 votes: Torricelli and Ramanujan. But since the string \"Ramanujan\" is lexicographically smaller than \"Torricelli\", then Ramanujan is the \"Chef of the Year\".\nExample 3. Here we have two countries with 1 vote: England and Greece. Since the string \"England\" is lexicographically smaller than \"Greece\", then England is the country-winner. Next, we have two chefs with 1 vote: Newton and Euclid. Since the string \"Euclid\" is lexicographically smaller than \"Newton\", then Euclid is the \"Chef of the Year\"."} +{"id": "02531", "text": "You are given a sequence of integers $a_1, a_2, ..., a_N$. An element ak is said to be an average element if there are indices $i, j$ (with $i \\neq j$) such that $a_k = \\frac{a_i + a_j}{2}$.\nIn the sequence\n371022171537102217153 \\quad 7 \\quad 10 \\quad 22 \\quad 17 \\quad 15\nfor $i=1, j=5$ and $k=3$, we get $a_k = \\frac{a_i + a_j}{2}$. Thus $a_3 = 10$ is an average element in this sequence. You can check that $a_3$ is the only average element in this sequence.\nConsider the sequence\n371031837103183 \\quad 7 \\quad 10 \\quad 3 \\quad 18\nWith $i=1, j=4$ and $k=1$ we get $a_k = \\frac{a_i + a_j}{2}$. Thus $a_1=3$ is an average element. We could also choose $i=1, j=4$ and $k=4$ and get $a_k = \\frac{a_i + a_j}{2}$. You can check that $a_1$ and $a_4$ are the only average elements of this sequence.\nOn the other hand, the sequence\n38111730381117303 \\quad 8 \\quad 11 \\quad 17 \\quad 30\nhas no average elements.\nYour task is to count the number of average elements in the given sequence.\n\n-----Input:-----\nThe first line contains a single integer $N$ indicating the number of elements in the sequence. This is followed by $N$ lines containing one integer each (Line $i+1$ contains $a_i$). (You may assume that $a_i + a_j$ would not exceed MAXINT for any $i$ and $j$).\n\n-----Output:-----\nThe output must consist of a single line containing a single integer $k$ indicating the number of average elements in the given sequence.\n\n-----Constraints:-----\n- You may assume that $1 \\leq N \\leq 10000$.\n- In $30 \\%$ of the inputs $1 \\leq N \\leq 200$.\n- In $60 \\%$ of the inputs $1 \\leq N \\leq 5000$.\n\n-----Sample Input 1:-----\n6\n3\n7\n10\n17\n22\n15\n\n-----Sample Output 1:-----\n1\n\n-----Sample Input 2:-----\n5\n3\n7\n10\n3\n18\n\n-----Sample Output 2:-----\n2\n\n-----Sample Input 3;-----\n5\n3\n8\n11\n17\n30\n\n-----Sample Output 3:-----\n0"} +{"id": "02532", "text": "Ram has invented a magic sequence. Each element of the sequence is defined by the same recursive definition - take some linear combination of previous elements (whose coefficients are fixed) and add to them the n-th powers of some integers. Formally: Xn = Xn-1*a1 + ... + Xn-i*ai + b1*d1^n + ... + bj*dj^n, for some integer constants p,q,a1,...,ap,b1,..., bq,d1,..., dq. Of course, as the values can quickly grow, he computed them modulo a fixed value: 10^6. He wrote many consecutive values of the sequence, but then he lost most of his work. All he has now, is 10 consecutive values taken from somewhere in the sequence (he doesn't know at what n they begin), and the recursive rule. And he would like to recover the sequence, or at the very least, to be able to write the next 10 values taken from the sequence.\n\n-----Input-----\nFirst, two integers, 0<=p<=4, 0<=q<=4. Then come the descriptions of the coefficients, -100 <= a1,...,ap,b1,..., bq,d1,..., dq <= 100. Then, the following 10 integers are Xn,X(n+1),...,X(n+9) for some unknown n.\n\n-----Output-----\n10 integers - X(n+10),X(n+11),...,X(n+19)\n\n-----Example-----\nInput:\n\n1 1\n\n1\n\n1\n\n1\n\n11 12 13 14 15 16 17 18 19 20\n\nOutput:\n\n21 22 23 24 25 26 27 28 29 30\n\nInput:\n\n1 1\n\n1\n\n1\n\n2\n\n1 3 7 15 31 63 127 255 511 1023\n\n\n\nOutput:\n\n2047 4095 8191 16383 32767 65535 131071 262143 524287 48575\n\nInput:\n\n2 0\n\n1 1\n\n1 1 2 3 5 8 13 21 34 55\n\n\nOutput:\n\n89 144 233 377 610 987 1597 2584 4181 6765"} +{"id": "02533", "text": "The grand kingdom of Mancunia is famous for its tourist attractions and visitors flock to it throughout the year. King Mancunian is the benevolent ruler of the prosperous country whose main source of revenue is, of course, tourism.\n\nThe country can be represented by a network of unidirectional roads between the cities. But Mancunian, our benign monarch, has a headache. The road network of the country is not tourist-friendly and this is affecting revenue. To increase the GDP of the nation, he wants to redirect some of the roads to make the road network tourist-friendly and hence, ensure happiness and prosperity for his loyal subjects.\n\nNow is the time for some formal definitions. :(\n\nA road network is said to be tourist-friendly if for every city in the country, if a tourist starts his journey there, there is a path she can take to visit each and every city of the nation and traverse each road exactly once before ending up at the city where she started.\n\nGiven a description of the road network of Mancunia, can you come up with a scheme to redirect some (possibly none) of the roads to make it tourist-friendly?\n\n-----Input-----\nThe first line contains two integers N and E denoting the number of cities and roads in the beautiful country of Mancunia respectively.\nEach of the next E lines contains two integers a and b implying that there is a unidirectional road from city a to city b.\n\nIt is guaranteed that there aren't multiple roads in the exact same direction and that there is no road from a city to itself.\n\n-----Output-----\nIf there is no solution, print \"NO\" (without quotes). Else, print \"YES\" (without quotes), followed by exactly E lines.\nThe ith line of output should represent the ith road in the input. It should have two integers a and b denoting that the final orientation of that road is from a to b.\n\n-----Constraints-----\n\n- 1 \u2264 N \u2264 100000\n- 1 \u2264 E \u2264 200000\n- 1 \u2264 a, b \u2264 N\n\nSubtask 1: (20 points)\n- 1 \u2264 N \u2264 20\n- 1 \u2264 E \u2264 21\n\nSubtask 2: (80 points)\n- Same as original constraints\n\n-----Example 1-----\nInput:\n3 3\n1 2\n2 3\n3 1\n\nOutput:\nYES\n1 2\n2 3\n3 1\n\n-----Example 2-----\nInput:\n3 2\n1 2\n2 3\n\nOutput:\nNO\n\n-----Example 3-----\nInput:\n5 6\n1 2\n2 3\n3 4\n2 4\n2 5\n1 5\n\nOutput:\nYES\n1 2\n2 3\n3 4\n4 2\n2 5\n5 1"} +{"id": "02534", "text": "Our Chef is very happy that his son was selected for training in one of the finest culinary schools of the world.\nSo he and his wife decide to buy a gift for the kid as a token of appreciation.\nUnfortunately, the Chef hasn't been doing good business lately, and is in no mood on splurging money.\nOn the other hand, the boy's mother wants to buy something big and expensive.\nTo settle the matter like reasonable parents, they play a game.\n\nThey spend the whole day thinking of various gifts and write them down in a huge matrix.\nEach cell of the matrix contains the gift's cost.\nThen they decide that the mother will choose a row number r while the father will choose a column number c,\nthe item from the corresponding cell will be gifted to the kid in a couple of days. \n\nThe boy observes all of this secretly.\nHe is smart enough to understand that his parents will ultimately choose a gift whose cost is smallest in its row,\nbut largest in its column.\nIf no such gift exists, then our little chef has no option but to keep guessing.\nAs the matrix is huge, he turns to you for help.\n\nHe knows that sometimes the gift is not determined uniquely even if a gift exists whose cost is smallest in its row,\nbut largest in its column.\nHowever, since the boy is so smart, he realizes that the gift's cost is determined uniquely.\nYour task is to tell him the gift's cost which is smallest in its row,\nbut largest in its column, or to tell him no such gift exists.\n\n-----Input-----\nFirst line contains two integers R and C, the number of rows and columns in the matrix respectively. Then follow R lines, each containing C space separated integers - the costs of different gifts.\n\n-----Output-----\nPrint a single integer - a value in the matrix that is smallest in its row but highest in its column. If no such value exists, then print \"GUESS\" (without quotes of course) \n\n-----Constraints-----\n1 <= R, C <= 100 \nAll gift costs are positive and less than 100000000 (10^8) \n\n-----Example 1-----\nInput:\n2 3\n9 8 8\n2 6 11\n\nOutput:\n8\n\n-----Example 2-----\nInput:\n3 3\n9 8 11\n2 6 34\n5 9 11\n\nOutput:\nGUESS\n\n-----Example 3-----\nInput:\n2 2\n10 10\n10 10\n\nOutput:\n10\n\n-----Explanation of Sample Cases-----\nExample 1: The first row contains 9, 8, 8. Observe that both 8 are the minimum. Considering the first 8, look at the corresponding column (containing 8 and 6). Here, 8 is the largest element in that column. So it will be chosen.\nExample 2: There is no value in the matrix that is smallest in its row but largest in its column.\nExample 3: The required gift in matrix is not determined uniquely, but the required cost is determined uniquely."} +{"id": "02535", "text": "A daily train consists of N cars. Let's consider one particular car. It has 54 places numbered consecutively from 1 to 54, some of which are already booked and some are still free. The places are numbered in the following fashion:\n\nThe car is separated into 9 compartments of 6 places each, as shown in the picture. So, the 1st compartment consists of places 1, 2, 3, 4, 53 and 54, the 2nd compartment consists of places 5, 6, 7, 8, 51 and 52, and so on.\n\nA group of X friends wants to buy tickets for free places, all of which are in one compartment (it's much funnier to travel together). You are given the information about free and booked places in each of the N cars. Find the number of ways to sell the friends exactly X tickets in one compartment (note that the order in which the tickets are sold doesn't matter).\n\n-----Input-----\nThe first line of the input contains two integers X and N (1 \u2264 X \u2264 6, 1 \u2264 N \u2264 10) separated by a single space. Each of the following N lines contains the information about one car which is a string of length 54 consisting of '0' and '1'. The i-th character (numbered from 1) is '0' if place i in the corresponding car is free, and is '1' if place i is already booked.\n\n-----Output-----\nOutput just one integer -- the requested number of ways.\n\n-----Example-----\nInput:\n1 3\n100101110000001011000001111110010011110010010111000101\n001010000000101111100000000000000111101010101111111010\n011110011110000001010100101110001011111010001001111010\n\nOutput:\n85\n\nInput:\n6 3\n100101110000001011000001111110010011110010010111000101\n001010000000101111100000000000000111101010101111111010\n011110011110000001010100101110001011111010001001111010\n\nOutput:\n1\n\nInput:\n3 2\n000000000000000000000000000000000000000000000000000000\n000000000000000000000000000000000000000000000000000000\n\nOutput:\n360\n\nExplanation:\nIn the first test case, any of the free places can be sold. In the second test case, the only free compartment in the train is compartment 3 in the first car (places 9, 10, 11, 12, 49 and 50 are all free). In the third test case, the train is still absolutely free; as there are 20 ways to sell 3 tickets in an empty compartment, the answer is 2 * 9 * 20 = 360."} +{"id": "02536", "text": "Mike is given a matrix A, N and M are numbers of rows and columns respectively. A1, 1 is the number in the top left corner. All the numbers in A are non-negative integers. He also has L pairs of integers (ik, jk). His task is to calculate Ai1, j1 + Ai2, j2 + ... + AiL, jL.\n\nUnfortunately, Mike forgot if Ai, j was a number in the i'th row and j'th column or vice versa, if Ai, j was a number in the j'th row and i'th column.\n\nSo, Mike decided to calculate both E1 = Ai1, j1 + Ai2, j2 + ... + AiL, jL and E2 = Aj1, i1 + Aj2, i2 + ... + AjL, iL. If it is impossible to calculate E1(i.e. one of the summands doesn't exist), then let's assume, that it is equal to -1. If it is impossible to calculate E2, then let's also assume, that it is equal to -1.\n\nYour task is to calculate max(E1, E2).\n\n-----Input-----\n\nThe first line contains two integers N and M, denoting the number of rows and the number of columns respectively.\n\nEach of next N lines contains M integers. The j'th integer in the (i + 1)'th line of the input denotes Ai, j.\n\nThe (N + 2)'th line contains an integer L, denoting the number of pairs of integers, that Mike has.\n\nEach of next L lines contains a pair of integers. The (N + 2 + k)-th line in the input contains a pair (ik, jk).\n\n-----Output-----\nThe first line should contain an integer, denoting max(E1, E2).\n\n-----Examples-----\nInput:\n3 2\n1 2\n4 5\n7 0\n2\n1 2\n2 2\nOutput:\n9\n\nInput:\n1 3\n1 2 3\n2\n1 3\n3 1\nOutput:\n-1\n\nInput:\n1 3\n1 2 3\n2\n1 1\n3 1\nOutput:\n4\n\n-----Explanation-----\n\nIn the first test case N equals to 3, M equals to 2, L equals to 2. E1 = 2 + 5 = 7, E2 = 4 + 5 = 9. The answer is max(E1, E2) = max(7, 9) = 9;\n\nIn the second test case N equals to 1, M equals to 3, L equals to 2. It is impossible to calculate E1 and E2, because A3, 1 doesn't exist. So the answer is max(E1, E2) = max(-1, -1) = -1;\n\nIn the third test case N equals to 1, M equals to 3, L equals to 2. It is impossible to calculate E1, because A3, 1 doesn't exist. So E1 is equal to -1. E2 = 1 + 3 = 4. The answer is max(E1, E2) = max(-1,4) = 4.\n\n-----Scoring-----\n\n1 \u2264 ik, jk \u2264 500 for each test case.\n\nSubtask 1 (10 points): 1 \u2264 N, M, L \u2264 5, 0 \u2264 Ai, j \u2264 10;\n\nSubtask 2 (12 points): 1 \u2264 N, M, L \u2264 300, 0 \u2264 Ai, j \u2264 106, all the numbers in A are equal;\n\nSubtask 3 (20 points): 1 \u2264 N, M, L \u2264 300, 0 \u2264 Ai, j \u2264 109;\n\nSubtask 4 (26 points): 1 \u2264 N, M, L \u2264 500, 0 \u2264 Ai, j \u2264 109;\n\nSubtask 5 (32 points): 1 \u2264 N, M \u2264 500, 1 \u2264 L \u2264 250 000, 0 \u2264 Ai, j \u2264 109."} +{"id": "02537", "text": "You are given three strings $s$, $t$ and $p$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.\n\nDuring each operation you choose any character from $p$, erase it from $p$ and insert it into string $s$ (you may insert this character anywhere you want: in the beginning of $s$, in the end or between any two consecutive characters). \n\nFor example, if $p$ is aba, and $s$ is de, then the following outcomes are possible (the character we erase from $p$ and insert into $s$ is highlighted):\n\n aba $\\rightarrow$ ba, de $\\rightarrow$ ade; aba $\\rightarrow$ ba, de $\\rightarrow$ dae; aba $\\rightarrow$ ba, de $\\rightarrow$ dea; aba $\\rightarrow$ aa, de $\\rightarrow$ bde; aba $\\rightarrow$ aa, de $\\rightarrow$ dbe; aba $\\rightarrow$ aa, de $\\rightarrow$ deb; aba $\\rightarrow$ ab, de $\\rightarrow$ ade; aba $\\rightarrow$ ab, de $\\rightarrow$ dae; aba $\\rightarrow$ ab, de $\\rightarrow$ dea; \n\nYour goal is to perform several (maybe zero) operations so that $s$ becomes equal to $t$. Please determine whether it is possible.\n\nNote that you have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line contains one integer $q$ ($1 \\le q \\le 100$) \u2014 the number of queries. Each query is represented by three consecutive lines.\n\nThe first line of each query contains the string $s$ ($1 \\le |s| \\le 100$) consisting of lowercase Latin letters.\n\nThe second line of each query contains the string $t$ ($1 \\le |t| \\le 100$) consisting of lowercase Latin letters.\n\nThe third line of each query contains the string $p$ ($1 \\le |p| \\le 100$) consisting of lowercase Latin letters.\n\n\n-----Output-----\n\nFor each query print YES if it is possible to make $s$ equal to $t$, and NO otherwise.\n\nYou may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).\n\n\n-----Example-----\nInput\n4\nab\nacxb\ncax\na\naaaa\naaabbcc\na\naaaa\naabbcc\nab\nbaaa\naaaaa\n\nOutput\nYES\nYES\nNO\nNO\n\n\n\n-----Note-----\n\nIn the first test case there is the following sequence of operation: $s = $ ab, $t = $ acxb, $p = $ cax; $s = $ acb, $t = $ acxb, $p = $ ax; $s = $ acxb, $t = $ acxb, $p = $ a. \n\nIn the second test case there is the following sequence of operation: $s = $ a, $t = $ aaaa, $p = $ aaabbcc; $s = $ aa, $t = $ aaaa, $p = $ aabbcc; $s = $ aaa, $t = $ aaaa, $p = $ abbcc; $s = $ aaaa, $t = $ aaaa, $p = $ bbcc."} +{"id": "02538", "text": "You play your favourite game yet another time. You chose the character you didn't play before. It has $str$ points of strength and $int$ points of intelligence. Also, at start, the character has $exp$ free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by $1$ or raise intelligence by $1$).\n\nSince you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).\n\nCalculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.\n\n\n-----Input-----\n\nThe first line contains the single integer $T$ ($1 \\le T \\le 100$) \u2014 the number of queries. Next $T$ lines contain descriptions of queries \u2014 one per line.\n\nThis line contains three integers $str$, $int$ and $exp$ ($1 \\le str, int \\le 10^8$, $0 \\le exp \\le 10^8$) \u2014 the initial strength and intelligence of the character and the number of free points, respectively.\n\n\n-----Output-----\n\nPrint $T$ integers \u2014 one per query. For each query print the number of different character builds you can create.\n\n\n-----Example-----\nInput\n4\n5 3 4\n2 1 0\n3 5 5\n4 10 6\n\nOutput\n3\n1\n2\n0\n\n\n\n-----Note-----\n\nIn the first query there are only three appropriate character builds: $(str = 7, int = 5)$, $(8, 4)$ and $(9, 3)$. All other builds are either too smart or don't use all free points.\n\nIn the second query there is only one possible build: $(2, 1)$.\n\nIn the third query there are two appropriate builds: $(7, 6)$, $(8, 5)$.\n\nIn the fourth query all builds have too much brains."} +{"id": "02539", "text": "Let's denote as L(x, p) an infinite sequence of integers y such that gcd(p, y) = 1 and y > x (where gcd is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of L(x, p) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of L(7, 22), respectively.\n\nYou have to process t queries. Each query is denoted by three integers x, p and k, and the answer to this query is k-th element of L(x, p).\n\n\n-----Input-----\n\nThe first line contains one integer t (1 \u2264 t \u2264 30000) \u2014 the number of queries to process.\n\nThen t lines follow. i-th line contains three integers x, p and k for i-th query (1 \u2264 x, p, k \u2264 10^6).\n\n\n-----Output-----\n\nPrint t integers, where i-th integer is the answer to i-th query.\n\n\n-----Examples-----\nInput\n3\n7 22 1\n7 22 2\n7 22 3\n\nOutput\n9\n13\n15\n\nInput\n5\n42 42 42\n43 43 43\n44 44 44\n45 45 45\n46 46 46\n\nOutput\n187\n87\n139\n128\n141"} +{"id": "02540", "text": "You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour.\n\nLet's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it's possible that two or more colours will be dominating in the subtree of some vertex.\n\nThe subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.\n\nFor each vertex v find the sum of all dominating colours in the subtree of vertex v.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) \u2014 the number of vertices in the tree.\n\nThe second line contains n integers c_{i} (1 \u2264 c_{i} \u2264 n), c_{i} \u2014 the colour of the i-th vertex.\n\nEach of the next n - 1 lines contains two integers x_{j}, y_{j} (1 \u2264 x_{j}, y_{j} \u2264 n) \u2014 the edge of the tree. The first vertex is the root of the tree.\n\n\n-----Output-----\n\nPrint n integers \u2014 the sums of dominating colours for each vertex.\n\n\n-----Examples-----\nInput\n4\n1 2 3 4\n1 2\n2 3\n2 4\n\nOutput\n10 9 3 4\n\nInput\n15\n1 2 3 1 2 3 3 1 1 3 2 2 1 2 3\n1 2\n1 3\n1 4\n1 14\n1 15\n2 5\n2 6\n2 7\n3 8\n3 9\n3 10\n4 11\n4 12\n4 13\n\nOutput\n6 5 4 3 2 3 3 1 1 3 2 2 1 2 3"} +{"id": "02541", "text": "You're given Q queries of the form (L, R). \n\nFor each query you have to find the number of such x that L \u2264 x \u2264 R and there exist integer numbers a > 0, p > 1 such that x = a^{p}.\n\n\n-----Input-----\n\nThe first line contains the number of queries Q (1 \u2264 Q \u2264 10^5).\n\nThe next Q lines contains two integers L, R each (1 \u2264 L \u2264 R \u2264 10^18).\n\n\n-----Output-----\n\nOutput Q lines \u2014 the answers to the queries.\n\n\n-----Example-----\nInput\n6\n1 4\n9 9\n5 7\n12 29\n137 591\n1 1000000\n\nOutput\n2\n1\n0\n3\n17\n1111\n\n\n\n-----Note-----\n\nIn query one the suitable numbers are 1 and 4."} +{"id": "02542", "text": "Let's call left cyclic shift of some string $t_1 t_2 t_3 \\dots t_{n - 1} t_n$ as string $t_2 t_3 \\dots t_{n - 1} t_n t_1$.\n\nAnalogically, let's call right cyclic shift of string $t$ as string $t_n t_1 t_2 t_3 \\dots t_{n - 1}$.\n\nLet's say string $t$ is good if its left cyclic shift is equal to its right cyclic shift.\n\nYou are given string $s$ which consists of digits 0\u20139.\n\nWhat is the minimum number of characters you need to erase from $s$ to make it good?\n\n\n-----Input-----\n\nThe first line contains single integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases.\n\nNext $t$ lines contains test cases\u00a0\u2014 one per line. The first and only line of each test case contains string $s$ ($2 \\le |s| \\le 2 \\cdot 10^5$). Each character $s_i$ is digit 0\u20139.\n\nIt's guaranteed that the total length of strings doesn't exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case, print the minimum number of characters you need to erase from $s$ to make it good.\n\n\n-----Example-----\nInput\n3\n95831\n100120013\n252525252525\n\nOutput\n3\n5\n0\n\n\n\n-----Note-----\n\nIn the first test case, you can erase any $3$ characters, for example, the $1$-st, the $3$-rd, and the $4$-th. You'll get string 51 and it is good.\n\nIn the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good.\n\nIn the third test case, the given string $s$ is already good."} +{"id": "02543", "text": "Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array a of length n and array b of length m. He introduced a function f(j) which is defined for integers j, which satisfy 0 \u2264 j \u2264 m - n. Suppose, c_{i} = a_{i} - b_{i} + j. Then f(j) = |c_1 - c_2 + c_3 - c_4... c_{n}|. More formally, $f(j) =|\\sum_{i = 1}^{n}(- 1)^{i - 1} *(a_{i} - b_{i + j})|$. \n\nDr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid j. They found it a bit easy, so Dr. Evil made their task harder. He will give them q update queries. During each update they should add an integer x_{i} to all elements in a in range [l_{i};r_{i}] i.e. they should add x_{i} to a_{l}_{i}, a_{l}_{i} + 1, ... , a_{r}_{i} and then they should calculate the minimum value of f(j) for all valid j.\n\nPlease help Mahmoud and Ehab.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and q (1 \u2264 n \u2264 m \u2264 10^5, 1 \u2264 q \u2264 10^5)\u00a0\u2014 number of elements in a, number of elements in b and number of queries, respectively.\n\nThe second line contains n integers a_1, a_2, ..., a_{n}. ( - 10^9 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 elements of a.\n\nThe third line contains m integers b_1, b_2, ..., b_{m}. ( - 10^9 \u2264 b_{i} \u2264 10^9)\u00a0\u2014 elements of b.\n\nThen q lines follow describing the queries. Each of them contains three integers l_{i} r_{i} x_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n, - 10^9 \u2264 x \u2264 10^9)\u00a0\u2014 range to be updated and added value.\n\n\n-----Output-----\n\nThe first line should contain the minimum value of the function f before any update.\n\nThen output q lines, the i-th of them should contain the minimum value of the function f after performing the i-th update .\n\n\n-----Example-----\nInput\n5 6 3\n1 2 3 4 5\n1 2 3 4 5 6\n1 1 10\n1 1 -9\n1 5 -1\n\nOutput\n0\n9\n0\n0\n\n\n\n-----Note-----\n\nFor the first example before any updates it's optimal to choose j = 0, f(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0.\n\nAfter the first update a becomes {11, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9.\n\nAfter the second update a becomes {2, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(2 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6)| = |0| = 0.\n\nAfter the third update a becomes {1, 1, 2, 3, 4} and it's optimal to choose j = 0, f(0) = |(1 - 1) - (1 - 2) + (2 - 3) - (3 - 4) + (4 - 5)| = |0| = 0."} +{"id": "02544", "text": "Fishing Prince loves trees, and he especially loves trees with only one centroid. The tree is a connected graph without cycles.\n\nA vertex is a centroid of a tree only when you cut this vertex (remove it and remove all edges from this vertex), the size of the largest connected component of the remaining graph is the smallest possible.\n\nFor example, the centroid of the following tree is $2$, because when you cut it, the size of the largest connected component of the remaining graph is $2$ and it can't be smaller. [Image] \n\nHowever, in some trees, there might be more than one centroid, for example: [Image] \n\nBoth vertex $1$ and vertex $2$ are centroids because the size of the largest connected component is $3$ after cutting each of them.\n\nNow Fishing Prince has a tree. He should cut one edge of the tree (it means to remove the edge). After that, he should add one edge. The resulting graph after these two operations should be a tree. He can add the edge that he cut.\n\nHe wants the centroid of the resulting tree to be unique. Help him and find any possible way to make the operations. It can be proved, that at least one such way always exists.\n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains an integer $t$ ($1\\leq t\\leq 10^4$) \u2014 the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains an integer $n$ ($3\\leq n\\leq 10^5$) \u2014 the number of vertices.\n\nEach of the next $n-1$ lines contains two integers $x, y$ ($1\\leq x,y\\leq n$). It means, that there exists an edge connecting vertices $x$ and $y$.\n\nIt's guaranteed that the given graph is a tree.\n\nIt's guaranteed that the sum of $n$ for all test cases does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case, print two lines.\n\nIn the first line print two integers $x_1, y_1$ ($1 \\leq x_1, y_1 \\leq n$), which means you cut the edge between vertices $x_1$ and $y_1$. There should exist edge connecting vertices $x_1$ and $y_1$.\n\nIn the second line print two integers $x_2, y_2$ ($1 \\leq x_2, y_2 \\leq n$), which means you add the edge between vertices $x_2$ and $y_2$.\n\nThe graph after these two operations should be a tree.\n\nIf there are multiple solutions you can print any.\n\n\n-----Example-----\nInput\n2\n5\n1 2\n1 3\n2 4\n2 5\n6\n1 2\n1 3\n1 4\n2 5\n2 6\n\nOutput\n1 2\n1 2\n1 3\n2 3\n\n\n-----Note-----\n\nNote that you can add the same edge that you cut.\n\nIn the first test case, after cutting and adding the same edge, the vertex $2$ is still the only centroid.\n\nIn the second test case, the vertex $2$ becomes the only centroid after cutting the edge between vertices $1$ and $3$ and adding the edge between vertices $2$ and $3$."} +{"id": "02545", "text": "You are given two integers $a$ and $b$. You may perform any number of operations on them (possibly zero).\n\nDuring each operation you should choose any positive integer $x$ and set $a := a - x$, $b := b - 2x$ or $a := a - 2x$, $b := b - x$. Note that you may choose different values of $x$ in different operations.\n\nIs it possible to make $a$ and $b$ equal to $0$ simultaneously?\n\nYour program should answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThen the test cases follow, each test case is represented by one line containing two integers $a$ and $b$ for this test case ($0 \\le a, b \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case print the answer to it \u2014 YES if it is possible to make $a$ and $b$ equal to $0$ simultaneously, and NO otherwise.\n\nYou may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).\n\n\n-----Example-----\nInput\n3\n6 9\n1 1\n1 2\n\nOutput\nYES\nNO\nYES\n\n\n\n-----Note-----\n\nIn the first test case of the example two operations can be used to make both $a$ and $b$ equal to zero: choose $x = 4$ and set $a := a - x$, $b := b - 2x$. Then $a = 6 - 4 = 2$, $b = 9 - 8 = 1$; choose $x = 1$ and set $a := a - 2x$, $b := b - x$. Then $a = 2 - 2 = 0$, $b = 1 - 1 = 0$."} +{"id": "02546", "text": "You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$).\n\nYou have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible.\n\nTo find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. \n\nIt is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \\dots + l_n \\le s$.\n\nNote that you don't have to spend all your $s$ dollars on salaries.\n\nYou have to answer $t$ test cases.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 2 \\cdot 10^5$) \u2014 the number of test cases.\n\nThe first line of each query contains two integers $n$ and $s$ ($1 \\le n < 2 \\cdot 10^5$, $1 \\le s \\le 2 \\cdot 10^{14}$) \u2014 the number of employees and the amount of money you have. The value $n$ is not divisible by $2$.\n\nThe following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le 10^9$).\n\nIt is guaranteed that the sum of all $n$ over all queries does not exceed $2 \\cdot 10^5$.\n\nIt is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\\sum\\limits_{i=1}^{n} l_i \\le s$.\n\n\n-----Output-----\n\nFor each test case print one integer \u2014 the maximum median salary that you can obtain.\n\n\n-----Example-----\nInput\n3\n3 26\n10 12\n1 4\n10 11\n1 1337\n1 1000000000\n5 26\n4 4\n2 4\n6 8\n5 6\n2 7\n\nOutput\n11\n1337\n6\n\n\n\n-----Note-----\n\nIn the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$.\n\nIn the second test case, you have to pay $1337$ dollars to the only employee.\n\nIn the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$."} +{"id": "02547", "text": "Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.\n\nSummer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read exactly $m$ books before all entertainments. Alice and Bob will read each book together to end this exercise faster.\n\nThere are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ \u2014 the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not).\n\nSo they need to choose exactly $m$ books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these $m$ books is minimized (they are children and want to play and joy as soon a possible). \n\nThe set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set.\n\nYour task is to help them and find any suitable set of books or determine that it is impossible to find such a set.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n$, $m$ and $k$ ($1 \\le k \\le m \\le n \\le 2 \\cdot 10^5$).\n\nThe next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \\le t_i \\le 10^4$, $0 \\le a_i, b_i \\le 1$), where: $t_i$ \u2014 the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. \n\n\n-----Output-----\n\nIf there is no solution, print only one integer -1.\n\nIf the solution exists, print $T$ in the first line \u2014 the minimum total reading time of the suitable set of books. In the second line print $m$ distinct integers from $1$ to $n$ in any order \u2014 indices of books which are in the set you found.\n\nIf there are several answers, print any of them.\n\n\n-----Examples-----\nInput\n6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n\nOutput\n24\n6 5 1 \nInput\n6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n\nOutput\n39\n4 6 5"} +{"id": "02548", "text": "You are given an array $a_1, a_2, \\dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \\dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\\sum\\limits_{i=l}^{r} a_i = r - l + 1$).\n\nFor example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \\dots 1} = [1], a_{2 \\dots 3} = [2, 0]$ and $a_{1 \\dots 3} = [1, 2, 0]$.\n\nCalculate the number of good subarrays of the array $a$.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains one integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the length of the array $a$.\n\nThe second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case print one integer \u2014 the number of good subarrays of the array $a$.\n\n\n-----Example-----\nInput\n3\n3\n120\n5\n11011\n6\n600005\n\nOutput\n3\n6\n1\n\n\n\n-----Note-----\n\nThe first test case is considered in the statement.\n\nIn the second test case, there are $6$ good subarrays: $a_{1 \\dots 1}$, $a_{2 \\dots 2}$, $a_{1 \\dots 2}$, $a_{4 \\dots 4}$, $a_{5 \\dots 5}$ and $a_{4 \\dots 5}$. \n\nIn the third test case there is only one good subarray: $a_{2 \\dots 6}$."} +{"id": "02549", "text": "You are playing a computer game. In this game, you have to fight $n$ monsters.\n\nTo defend from monsters, you need a shield. Each shield has two parameters: its current durability $a$ and its defence rating $b$. Each monster has only one parameter: its strength $d$.\n\nWhen you fight a monster with strength $d$ while having a shield with current durability $a$ and defence $b$, there are three possible outcomes: if $a = 0$, then you receive $d$ damage; if $a > 0$ and $d \\ge b$, you receive no damage, but the current durability of the shield decreases by $1$; if $a > 0$ and $d < b$, nothing happens. \n\nThe $i$-th monster has strength $d_i$, and you will fight each of the monsters exactly once, in some random order (all $n!$ orders are equiprobable). You have to consider $m$ different shields, the $i$-th shield has initial durability $a_i$ and defence rating $b_i$. For each shield, calculate the expected amount of damage you will receive if you take this shield and fight the given $n$ monsters in random order.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$) \u2014 the number of monsters and the number of shields, respectively.\n\nThe second line contains $n$ integers $d_1$, $d_2$, ..., $d_n$ ($1 \\le d_i \\le 10^9$), where $d_i$ is the strength of the $i$-th monster.\n\nThen $m$ lines follow, the $i$-th of them contains two integers $a_i$ and $b_i$ ($1 \\le a_i \\le n$; $1 \\le b_i \\le 10^9$) \u2014 the description of the $i$-th shield.\n\n\n-----Output-----\n\nPrint $m$ integers, where the $i$-th integer represents the expected damage you receive with the $i$-th shield as follows: it can be proven that, for each shield, the expected damage is an irreducible fraction $\\dfrac{x}{y}$, where $y$ is coprime with $998244353$. You have to print the value of $x \\cdot y^{-1} \\bmod 998244353$, where $y^{-1}$ is the inverse element for $y$ ($y \\cdot y^{-1} \\bmod 998244353 = 1$).\n\n\n-----Examples-----\nInput\n3 2\n1 3 1\n2 1\n1 2\n\nOutput\n665496237\n1\n\nInput\n3 3\n4 2 6\n3 1\n1 2\n2 3\n\nOutput\n0\n8\n665496236"} +{"id": "02550", "text": "$n$ students are taking an exam. The highest possible score at this exam is $m$. Let $a_{i}$ be the score of the $i$-th student. You have access to the school database which stores the results of all students.\n\nYou can change each student's score as long as the following conditions are satisfied: All scores are integers $0 \\leq a_{i} \\leq m$ The average score of the class doesn't change. \n\nYou are student $1$ and you would like to maximize your own score.\n\nFind the highest possible score you can assign to yourself such that all conditions are satisfied.\n\n\n-----Input-----\n\nEach test contains multiple test cases. \n\nThe first line contains the number of test cases $t$ ($1 \\le t \\le 200$). The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $m$ ($1 \\leq n \\leq 10^{3}$, $1 \\leq m \\leq 10^{5}$) \u00a0\u2014 the number of students and the highest possible score respectively.\n\nThe second line of each testcase contains $n$ integers $a_1, a_2, \\dots, a_n$ ($ 0 \\leq a_{i} \\leq m$) \u00a0\u2014 scores of the students.\n\n\n-----Output-----\n\nFor each testcase, output one integer \u00a0\u2014 the highest possible score you can assign to yourself such that both conditions are satisfied._\n\n\n-----Example-----\nInput\n2\n4 10\n1 2 3 4\n4 5\n1 2 3 4\n\nOutput\n10\n5\n\n\n\n-----Note-----\n\nIn the first case, $a = [1,2,3,4] $, with average of $2.5$. You can change array $a$ to $[10,0,0,0]$. Average remains $2.5$, and all conditions are satisfied.\n\nIn the second case, $0 \\leq a_{i} \\leq 5$. You can change $a$ to $[5,1,1,3]$. You cannot increase $a_{1}$ further as it will violate condition $0\\le a_i\\le m$."} +{"id": "02551", "text": "You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$). \n\nNote that after this operation string $s$ doesn't change.\n\nCalculate the minimum number of such operations to turn string $z$ into string $t$. \n\n\n-----Input-----\n\nThe first line contains the integer $T$ ($1 \\le T \\le 100$) \u2014 the number of test cases.\n\nThe first line of each testcase contains one string $s$ ($1 \\le |s| \\le 10^5$) consisting of lowercase Latin letters.\n\nThe second line of each testcase contains one string $t$ ($1 \\le |t| \\le 10^5$) consisting of lowercase Latin letters.\n\nIt is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each testcase, print one integer \u2014 the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.\n\n\n-----Example-----\nInput\n3\naabce\nace\nabacaba\naax\nty\nyyt\n\nOutput\n1\n-1\n3"} +{"id": "02552", "text": "Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.\n\nThere are $n$ cities and $n\u22121$ undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from $1$ to $n$ and the city $1$ is a capital. In other words, the country has a tree structure.\n\nThere are $m$ citizens living in the country. A $p_i$ people live in the $i$-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths. \n\nEvery person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.\n\nHappiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the $i$-th city calculates a happiness index $h_i$ as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.\n\nHappiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.\n\nUncle Bogdan successfully solved the problem. Can you do the same?\n\nMore formally, You need to check: \"Is it possible that, after all people return home, for each city $i$ the happiness index will be equal exactly to $h_i$\".\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 10000$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains two integers $n$ and $m$ ($1 \\le n \\le 10^5$; $0 \\le m \\le 10^9$)\u00a0\u2014 the number of cities and citizens.\n\nThe second line of each test case contains $n$ integers $p_1, p_2, \\ldots, p_{n}$ ($0 \\le p_i \\le m$; $p_1 + p_2 + \\ldots + p_{n} = m$), where $p_i$ is the number of people living in the $i$-th city.\n\nThe third line contains $n$ integers $h_1, h_2, \\ldots, h_{n}$ ($-10^9 \\le h_i \\le 10^9$), where $h_i$ is the calculated happiness index of the $i$-th city.\n\nNext $n \u2212 1$ lines contain description of the roads, one per line. Each line contains two integers $x_i$ and $y_i$ ($1 \\le x_i, y_i \\le n$; $x_i \\neq y_i$), where $x_i$ and $y_i$ are cities connected by the $i$-th road.\n\nIt's guaranteed that the sum of $n$ from all test cases doesn't exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case, print YES, if the collected data is correct, or NO\u00a0\u2014 otherwise. You can print characters in YES or NO in any case.\n\n\n-----Examples-----\nInput\n2\n7 4\n1 0 1 1 0 1 0\n4 0 0 -1 0 -1 0\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n5 11\n1 2 5 2 1\n-11 -2 -6 -2 -1\n1 2\n1 3\n1 4\n3 5\n\nOutput\nYES\nYES\n\nInput\n2\n4 4\n1 1 1 1\n4 1 -3 -1\n1 2\n1 3\n1 4\n3 13\n3 3 7\n13 1 4\n1 2\n1 3\n\nOutput\nNO\nNO\n\n\n\n-----Note-----\n\nLet's look at the first test case of the first sample: [Image] \n\nAt first, all citizens are in the capital. Let's describe one of possible scenarios: a person from city $1$: he lives in the capital and is in good mood; a person from city $4$: he visited cities $1$ and $4$, his mood was ruined between cities $1$ and $4$; a person from city $3$: he visited cities $1$ and $3$ in good mood; a person from city $6$: he visited cities $1$, $3$ and $6$, his mood was ruined between cities $1$ and $3$; In total, $h_1 = 4 - 0 = 4$, $h_2 = 0$, $h_3 = 1 - 1 = 0$, $h_4 = 0 - 1 = -1$, $h_5 = 0$, $h_6 = 0 - 1 = -1$, $h_7 = 0$. \n\nThe second case of the first test: $\\text{of}_{0}$ \n\nAll people have already started in bad mood in the capital\u00a0\u2014 this is the only possible scenario.\n\nThe first case of the second test: $\\text{of} 0$ \n\nThe second case of the second test: [Image] \n\nIt can be proven that there is no way to achieve given happiness indexes in both cases of the second test."} +{"id": "02553", "text": "Shubham has an array $a$ of size $n$, and wants to select exactly $x$ elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.\n\nTell him whether he can do so.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $t$ $(1\\le t \\le 100)$\u00a0\u2014 the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $x$ $(1 \\le x \\le n \\le 1000)$\u00a0\u2014 the length of the array and the number of elements you need to choose.\n\nThe next line of each test case contains $n$ integers $a_1, a_2, \\dots, a_n$ $(1 \\le a_i \\le 1000)$\u00a0\u2014 elements of the array.\n\n\n-----Output-----\n\nFor each test case, print \"Yes\" or \"No\" depending on whether it is possible to choose $x$ elements such that their sum is odd.\n\nYou may print every letter in any case you want.\n\n\n-----Example-----\nInput\n5\n1 1\n999\n1 1\n1000\n2 1\n51 50\n2 2\n51 50\n3 3\n101 102 103\n\nOutput\nYes\nNo\nYes\nYes\nNo\n\n\n\n-----Note-----\n\nFor $1$st case: We must select element $999$, and the sum is odd.\n\nFor $2$nd case: We must select element $1000$, so overall sum is not odd.\n\nFor $3$rd case: We can select element $51$.\n\nFor $4$th case: We must select both elements $50$ and $51$ \u00a0\u2014 so overall sum is odd.\n\nFor $5$th case: We must select all elements \u00a0\u2014 but overall sum is not odd."} +{"id": "02554", "text": "You are given an array $a$ consisting of $n$ integers. Indices of the array start from zero (i. e. the first element is $a_0$, the second one is $a_1$, and so on).\n\nYou can reverse at most one subarray (continuous subsegment) of this array. Recall that the subarray of $a$ with borders $l$ and $r$ is $a[l; r] = a_l, a_{l + 1}, \\dots, a_{r}$.\n\nYour task is to reverse such a subarray that the sum of elements on even positions of the resulting array is maximized (i. e. the sum of elements $a_0, a_2, \\dots, a_{2k}$ for integer $k = \\lfloor\\frac{n-1}{2}\\rfloor$ should be maximum possible).\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2 \\cdot 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of $a$. The second line of the test case contains $n$ integers $a_0, a_1, \\dots, a_{n-1}$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the $i$-th element of $a$.\n\nIt is guaranteed that the sum of $n$ does not exceed $2 \\cdot 10^5$ ($\\sum n \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each test case, print the answer on the separate line \u2014 the maximum possible sum of elements on even positions after reversing at most one subarray (continuous subsegment) of $a$.\n\n\n-----Example-----\nInput\n4\n8\n1 7 3 4 7 6 2 9\n5\n1 2 1 2 1\n10\n7 8 4 5 7 6 8 9 7 3\n4\n3 1 2 1\n\nOutput\n26\n5\n37\n5"} +{"id": "02555", "text": "This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved.\n\nPikachu is a cute and friendly pok\u00e9mon living in the wild pikachu herd.\n\nBut it has become known recently that infamous team R wanted to steal all these pok\u00e9mon! Pok\u00e9mon trainer Andrew decided to help Pikachu to build a pok\u00e9mon army to resist.\n\nFirst, Andrew counted all the pok\u00e9mon\u00a0\u2014 there were exactly $n$ pikachu. The strength of the $i$-th pok\u00e9mon is equal to $a_i$, and all these numbers are distinct.\n\nAs an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \\le b_1 < b_2 < \\dots < b_k \\le n$, and his army will consist of pok\u00e9mons with forces $a_{b_1}, a_{b_2}, \\dots, a_{b_k}$.\n\nThe strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \\dots$.\n\nAndrew is experimenting with pok\u00e9mon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pok\u00e9mon.\n\nAndrew wants to know the maximal stregth of the army he can achieve with the initial pok\u00e9mon placement. He also needs to know the maximal strength after each operation.\n\nHelp Andrew and the pok\u00e9mon, or team R will realize their tricky plan!\n\n\n-----Input-----\n\nEach test contains multiple test cases.\n\nThe first line contains one positive integer $t$ ($1 \\le t \\le 10^3$) denoting the number of test cases. Description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $q$ ($1 \\le n \\le 3 \\cdot 10^5, 0 \\le q \\le 3 \\cdot 10^5$) denoting the number of pok\u00e9mon and number of operations respectively.\n\nThe second line contains $n$ distinct positive integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$) denoting the strengths of the pok\u00e9mon.\n\n$i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le n$) denoting the indices of pok\u00e9mon that were swapped in the $i$-th operation.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $3 \\cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \\cdot 10^5$. \n\n\n-----Output-----\n\nFor each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap.\n\n\n-----Example-----\nInput\n3\n3 1\n1 3 2\n1 2\n2 2\n1 2\n1 2\n1 2\n7 5\n1 2 5 4 3 6 7\n1 2\n6 7\n3 4\n1 2\n2 3\n\nOutput\n3\n4\n2\n2\n2\n9\n10\n10\n10\n9\n11\n\n\n\n-----Note-----\n\nLet's look at the third test case:\n\nInitially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$.\n\nAfter first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$.\n\nAfter second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$.\n\nAfter third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$.\n\nAfter forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$.\n\nAfter all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$."} +{"id": "02556", "text": "Several days ago you bought a new house and now you are planning to start a renovation. Since winters in your region can be very cold you need to decide how to heat rooms in your house.\n\nYour house has $n$ rooms. In the $i$-th room you can install at most $c_i$ heating radiators. Each radiator can have several sections, but the cost of the radiator with $k$ sections is equal to $k^2$ burles.\n\nSince rooms can have different sizes, you calculated that you need at least $sum_i$ sections in total in the $i$-th room. \n\nFor each room calculate the minimum cost to install at most $c_i$ radiators with total number of sections not less than $sum_i$.\n\n\n-----Input-----\n\nThe first line contains single integer $n$ ($1 \\le n \\le 1000$) \u2014 the number of rooms.\n\nEach of the next $n$ lines contains the description of some room. The $i$-th line contains two integers $c_i$ and $sum_i$ ($1 \\le c_i, sum_i \\le 10^4$) \u2014 the maximum number of radiators and the minimum total number of sections in the $i$-th room, respectively.\n\n\n-----Output-----\n\nFor each room print one integer \u2014 the minimum possible cost to install at most $c_i$ radiators with total number of sections not less than $sum_i$.\n\n\n-----Example-----\nInput\n4\n1 10000\n10000 1\n2 6\n4 6\n\nOutput\n100000000\n1\n18\n10\n\n\n\n-----Note-----\n\nIn the first room, you can install only one radiator, so it's optimal to use the radiator with $sum_1$ sections. The cost of the radiator is equal to $(10^4)^2 = 10^8$.\n\nIn the second room, you can install up to $10^4$ radiators, but since you need only one section in total, it's optimal to buy one radiator with one section.\n\nIn the third room, there $7$ variants to install radiators: $[6, 0]$, $[5, 1]$, $[4, 2]$, $[3, 3]$, $[2, 4]$, $[1, 5]$, $[0, 6]$. The optimal variant is $[3, 3]$ and it costs $3^2+ 3^2 = 18$."} +{"id": "02557", "text": "Shuseki Kingdom is the world's leading nation for innovation and technology. There are n cities in the kingdom, numbered from 1 to n.\n\nThanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city x to city y cannot be used to travel from city y to city x. The transportation within each city is extremely developed, therefore if a pipe from city x to city y and a pipe from city y to city z are both constructed, people will be able to travel from city x to city z instantly.\n\nMr. Kitayuta is also involved in national politics. He considers that the transportation between the m pairs of city (a_{i}, b_{i}) (1 \u2264 i \u2264 m) is important. He is planning to construct teleportation pipes so that for each important pair (a_{i}, b_{i}), it will be possible to travel from city a_{i} to city b_{i} by using one or more teleportation pipes (but not necessarily from city b_{i} to city a_{i}). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n and m (2 \u2264 n \u2264 10^5, 1 \u2264 m \u2264 10^5), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively.\n\nThe following m lines describe the important pairs. The i-th of them (1 \u2264 i \u2264 m) contains two space-separated integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 n, a_{i} \u2260 b_{i}), denoting that it must be possible to travel from city a_{i} to city b_{i} by using one or more teleportation pipes (but not necessarily from city b_{i} to city a_{i}). It is guaranteed that all pairs (a_{i}, b_{i}) are distinct.\n\n\n-----Output-----\n\nPrint the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose.\n\n\n-----Examples-----\nInput\n4 5\n1 2\n1 3\n1 4\n2 3\n2 4\n\nOutput\n3\n\nInput\n4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4\n\nOutput\n4\n\n\n\n-----Note-----\n\nFor the first sample, one of the optimal ways to construct pipes is shown in the image below: [Image] \n\nFor the second sample, one of the optimal ways is shown below: [Image]"} +{"id": "02558", "text": "Meka-Naruto plays a computer game. His character has the following ability: given an enemy hero, deal $a$ instant damage to him, and then heal that enemy $b$ health points at the end of every second, for exactly $c$ seconds, starting one second after the ability is used. That means that if the ability is used at time $t$, the enemy's health decreases by $a$ at time $t$, and then increases by $b$ at time points $t + 1$, $t + 2$, ..., $t + c$ due to this ability.\n\nThe ability has a cooldown of $d$ seconds, i.\u00a0e. if Meka-Naruto uses it at time moment $t$, next time he can use it is the time $t + d$. Please note that he can only use the ability at integer points in time, so all changes to the enemy's health also occur at integer times only.\n\nThe effects from different uses of the ability may stack with each other; that is, the enemy which is currently under $k$ spells gets $k\\cdot b$ amount of heal this time. Also, if several health changes occur at the same moment, they are all counted at once.\n\nNow Meka-Naruto wonders if he can kill the enemy by just using the ability each time he can (that is, every $d$ seconds). The enemy is killed if their health points become $0$ or less. Assume that the enemy's health is not affected in any way other than by Meka-Naruto's character ability. What is the maximal number of health points the enemy can have so that Meka-Naruto is able to kill them?\n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1\\leq t\\leq 10^5$) standing for the number of testcases.\n\nEach test case is described with one line containing four numbers $a$, $b$, $c$ and $d$ ($1\\leq a, b, c, d\\leq 10^6$) denoting the amount of instant damage, the amount of heal per second, the number of heals and the ability cooldown, respectively.\n\n\n-----Output-----\n\nFor each testcase in a separate line print $-1$ if the skill can kill an enemy hero with an arbitrary number of health points, otherwise print the maximal number of health points of the enemy that can be killed.\n\n\n-----Example-----\nInput\n7\n1 1 1 1\n2 2 2 2\n1 2 3 4\n4 3 2 1\n228 21 11 3\n239 21 11 3\n1000000 1 1000000 1\n\nOutput\n1\n2\n1\n5\n534\n-1\n500000500000\n\n\n\n-----Note-----\n\nIn the first test case of the example each unit of damage is cancelled in a second, so Meka-Naruto cannot deal more than 1 damage.\n\nIn the fourth test case of the example the enemy gets: $4$ damage ($1$-st spell cast) at time $0$; $4$ damage ($2$-nd spell cast) and $3$ heal ($1$-st spell cast) at time $1$ (the total of $5$ damage to the initial health); $4$ damage ($3$-nd spell cast) and $6$ heal ($1$-st and $2$-nd spell casts) at time $2$ (the total of $3$ damage to the initial health); and so on. \n\nOne can prove that there is no time where the enemy gets the total of $6$ damage or more, so the answer is $5$. Please note how the health is recalculated: for example, $8$-health enemy would not die at time $1$, as if we first subtracted $4$ damage from his health and then considered him dead, before adding $3$ heal.\n\nIn the sixth test case an arbitrarily healthy enemy can be killed in a sufficient amount of time.\n\nIn the seventh test case the answer does not fit into a 32-bit integer type."} +{"id": "02559", "text": "Welcome! Everything is fine.\n\nYou have arrived in The Medium Place, the place between The Good Place and The Bad Place. You are assigned a task that will either make people happier or torture them for eternity.\n\nYou have a list of $k$ pairs of people who have arrived in a new inhabited neighborhood. You need to assign each of the $2k$ people into one of the $2k$ houses. Each person will be the resident of exactly one house, and each house will have exactly one resident.\n\nOf course, in the neighborhood, it is possible to visit friends. There are $2k - 1$ roads, each of which connects two houses. It takes some time to traverse a road. We will specify the amount of time it takes in the input. The neighborhood is designed in such a way that from anyone's house, there is exactly one sequence of distinct roads you can take to any other house. In other words, the graph with the houses as vertices and the roads as edges is a tree.\n\nThe truth is, these $k$ pairs of people are actually soulmates. We index them from $1$ to $k$. We denote by $f(i)$ the amount of time it takes for the $i$-th pair of soulmates to go to each other's houses.\n\nAs we have said before, you will need to assign each of the $2k$ people into one of the $2k$ houses. You have two missions, one from the entities in The Good Place and one from the entities of The Bad Place. Here they are: The first mission, from The Good Place, is to assign the people into the houses such that the sum of $f(i)$ over all pairs $i$ is minimized. Let's define this minimized sum as $G$. This makes sure that soulmates can easily and efficiently visit each other; The second mission, from The Bad Place, is to assign the people into the houses such that the sum of $f(i)$ over all pairs $i$ is maximized. Let's define this maximized sum as $B$. This makes sure that soulmates will have a difficult time to visit each other. \n\nWhat are the values of $G$ and $B$?\n\n\n-----Input-----\n\nThe first line of input contains a single integer $t$ ($1 \\le t \\le 500$) denoting the number of test cases. The next lines contain descriptions of the test cases.\n\nThe first line of each test case contains a single integer $k$ denoting the number of pairs of people ($1 \\le k \\le 10^5$). The next $2k - 1$ lines describe the roads; the $i$-th of them contains three space-separated integers $a_i, b_i, t_i$ which means that the $i$-th road connects the $a_i$-th and $b_i$-th houses with a road that takes $t_i$ units of time to traverse ($1 \\le a_i, b_i \\le 2k$, $a_i \\neq b_i$, $1 \\le t_i \\le 10^6$). It is guaranteed that the given roads define a tree structure.\n\nIt is guaranteed that the sum of the $k$ in a single file is at most $3 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case, output a single line containing two space-separated integers $G$ and $B$. \n\n\n-----Example-----\nInput\n2\n3\n1 2 3\n3 2 4\n2 4 3\n4 5 6\n5 6 5\n2\n1 2 1\n1 3 2\n1 4 3\n\nOutput\n15 33\n6 6\n\n\n\n-----Note-----\n\nFor the sample test case, we have a minimum sum equal to $G = 15$. One way this can be achieved is with the following assignment: The first pair of people get assigned to houses $5$ and $6$, giving us $f(1) = 5$; The second pair of people get assigned to houses $1$ and $4$, giving us $f(2) = 6$; The third pair of people get assigned to houses $3$ and $2$, giving us $f(3) = 4$. \n\nNote that the sum of the $f(i)$ is $5 + 6 + 4 = 15$. \n\nWe also have a maximum sum equal to $B = 33$. One way this can be achieved is with the following assignment: The first pair of people get assigned to houses $1$ and $4$, giving us $f(1) = 6$; The second pair of people get assigned to houses $6$ and $2$, giving us $f(2) = 14$; The third pair of people get assigned to houses $3$ and $5$, giving us $f(3) = 13$. \n\nNote that the sum of the $f(i)$ is $6 + 14 + 13 = 33$."} +{"id": "02560", "text": "Alexey, a merry Berland entrant, got sick of the gray reality and he zealously wants to go to university. There are a lot of universities nowadays, so Alexey is getting lost in the diversity \u2014 he has not yet decided what profession he wants to get. At school, he had bad grades in all subjects, and it's only thanks to wealthy parents that he was able to obtain the graduation certificate.\n\nThe situation is complicated by the fact that each high education institution has the determined amount of voluntary donations, paid by the new students for admission \u2014 n_{i} berubleys. He cannot pay more than n_{i}, because then the difference between the paid amount and n_{i} can be regarded as a bribe!\n\nEach rector is wearing the distinctive uniform of his university. Therefore, the uniform's pockets cannot contain coins of denomination more than r_{i}. The rector also does not carry coins of denomination less than l_{i} in his pocket \u2014 because if everyone pays him with so small coins, they gather a lot of weight and the pocket tears. Therefore, a donation can be paid only by coins of denomination x berubleys, where l_{i} \u2264 x \u2264 r_{i} (Berland uses coins of any positive integer denomination). Alexey can use the coins of different denominations and he can use the coins of the same denomination any number of times. When Alexey was first confronted with such orders, he was puzzled because it turned out that not all universities can accept him! Alexey is very afraid of going into the army (even though he had long wanted to get the green uniform, but his dad says that the army bullies will beat his son and he cannot pay to ensure the boy's safety). So, Alexey wants to know for sure which universities he can enter so that he could quickly choose his alma mater.\n\nThanks to the parents, Alexey is not limited in money and we can assume that he has an unlimited number of coins of each type.\n\nIn other words, you are given t requests, each of them contains numbers n_{i}, l_{i}, r_{i}. For each query you need to answer, whether it is possible to gather the sum of exactly n_{i} berubleys using only coins with an integer denomination from l_{i} to r_{i} berubleys. You can use coins of different denominations. Coins of each denomination can be used any number of times.\n\n\n-----Input-----\n\nThe first line contains the number of universities t, (1 \u2264 t \u2264 1000) Each of the next t lines contain three space-separated integers: n_{i}, l_{i}, r_{i} (1 \u2264 n_{i}, l_{i}, r_{i} \u2264 10^9;\u00a0l_{i} \u2264 r_{i}).\n\n\n-----Output-----\n\nFor each query print on a single line: either \"Yes\", if Alexey can enter the university, or \"No\" otherwise.\n\n\n-----Examples-----\nInput\n2\n5 2 3\n6 4 5\n\nOutput\nYes\nNo\n\n\n\n-----Note-----\n\nYou can pay the donation to the first university with two coins: one of denomination 2 and one of denomination 3 berubleys. The donation to the second university cannot be paid."} +{"id": "02561", "text": "Colossal!\u00a0\u2014 exclaimed Hawk-nose.\u00a0\u2014 A programmer! That's exactly what we are looking for.Arkadi and Boris Strugatsky. Monday starts on Saturday\n\nReading the book \"Equations of Mathematical Magic\" Roman Oira-Oira and Cristobal Junta found an interesting equation: $a - (a \\oplus x) - x = 0$ for some given $a$, where $\\oplus$ stands for a bitwise exclusive or (XOR) of two integers (this operation is denoted as ^ or xor in many modern programming languages). Oira-Oira quickly found some $x$, which is the solution of the equation, but Cristobal Junta decided that Oira-Oira's result is not interesting enough, so he asked his colleague how many non-negative solutions of this equation exist. This task turned out to be too difficult for Oira-Oira, so he asks you to help.\n\n\n-----Input-----\n\nEach test contains several possible values of $a$ and your task is to find the number of equation's solution for each of them. The first line contains an integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of these values.\n\nThe following $t$ lines contain the values of parameter $a$, each value is an integer from $0$ to $2^{30} - 1$ inclusive.\n\n\n-----Output-----\n\nFor each value of $a$ print exactly one integer\u00a0\u2014 the number of non-negative solutions of the equation for the given value of the parameter. Print answers in the same order as values of $a$ appear in the input.\n\nOne can show that the number of solutions is always finite.\n\n\n-----Example-----\nInput\n3\n0\n2\n1073741823\n\nOutput\n1\n2\n1073741824\n\n\n\n-----Note-----\n\nLet's define the bitwise exclusive OR (XOR) operation. Given two integers $x$ and $y$, consider their binary representations (possibly with leading zeroes): $x_k \\dots x_2 x_1 x_0$ and $y_k \\dots y_2 y_1 y_0$. Here, $x_i$ is the $i$-th bit of the number $x$ and $y_i$ is the $i$-th bit of the number $y$. Let $r = x \\oplus y$ be the result of the XOR operation of $x$ and $y$. Then $r$ is defined as $r_k \\dots r_2 r_1 r_0$ where:\n\n$$ r_i = \\left\\{ \\begin{aligned} 1, ~ \\text{if} ~ x_i \\ne y_i \\\\ 0, ~ \\text{if} ~ x_i = y_i \\end{aligned} \\right. $$\n\nFor the first value of the parameter, only $x = 0$ is a solution of the equation.\n\nFor the second value of the parameter, solutions are $x = 0$ and $x = 2$."} +{"id": "02562", "text": "Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers.\n\nHe has created a method to know how strong his army is. Let the i-th soldier\u2019s strength be a_{i}. For some k he calls i_1, i_2, ..., i_{k} a clan if i_1 < i_2 < i_3 < ... < i_{k} and gcd(a_{i}_1, a_{i}_2, ..., a_{i}_{k}) > 1 . He calls the strength of that clan k\u00b7gcd(a_{i}_1, a_{i}_2, ..., a_{i}_{k}). Then he defines the strength of his army by the sum of strengths of all possible clans.\n\nYour task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (10^9 + 7).\n\nGreatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 200000)\u00a0\u2014 the size of the army.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1000000)\u00a0\u2014 denoting the strengths of his soldiers.\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the strength of John Snow's army modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n3\n3 3 1\n\nOutput\n12\n\nInput\n4\n2 3 4 6\n\nOutput\n39\n\n\n\n-----Note-----\n\nIn the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1\u00b73 + 1\u00b73 + 2\u00b73 = 12"} +{"id": "02563", "text": "You are given a huge integer $a$ consisting of $n$ digits ($n$ is between $1$ and $3 \\cdot 10^5$, inclusive). It may contain leading zeros.\n\nYou can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by $2$). \n\nFor example, if $a = 032867235$ you can get the following integers in a single operation: $302867235$ if you swap the first and the second digits; $023867235$ if you swap the second and the third digits; $032876235$ if you swap the fifth and the sixth digits; $032862735$ if you swap the sixth and the seventh digits; $032867325$ if you swap the seventh and the eighth digits. \n\nNote, that you can't swap digits on positions $2$ and $4$ because the positions are not adjacent. Also, you can't swap digits on positions $3$ and $4$ because the digits have the same parity.\n\nYou can perform any number (possibly, zero) of such operations.\n\nFind the minimum integer you can obtain.\n\nNote that the resulting integer also may contain leading zeros.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases in the input.\n\nThe only line of each test case contains the integer $a$, its length $n$ is between $1$ and $3 \\cdot 10^5$, inclusive.\n\nIt is guaranteed that the sum of all values $n$ does not exceed $3 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case print line \u2014 the minimum integer you can obtain.\n\n\n-----Example-----\nInput\n3\n0709\n1337\n246432\n\nOutput\n0079\n1337\n234642\n\n\n\n-----Note-----\n\nIn the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): $0 \\underline{\\textbf{70}} 9 \\rightarrow 0079$.\n\nIn the second test case, the initial integer is optimal. \n\nIn the third test case you can perform the following sequence of operations: $246 \\underline{\\textbf{43}} 2 \\rightarrow 24 \\underline{\\textbf{63}}42 \\rightarrow 2 \\underline{\\textbf{43}} 642 \\rightarrow 234642$."} +{"id": "02564", "text": "Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a \"+=\" operation that adds the right-hand side value to the left-hand side variable. For example, performing \"a += b\" when a = $2$, b = $3$ changes the value of a to $5$ (the value of b does not change).\n\nIn a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations \"a += b\" or \"b += a\". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value $n$. What is the smallest number of operations he has to perform?\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\leq T \\leq 100$)\u00a0\u2014 the number of test cases.\n\nEach of the following $T$ lines describes a single test case, and contains three integers $a, b, n$ ($1 \\leq a, b \\leq n \\leq 10^9$)\u00a0\u2014 initial values of a and b, and the value one of the variables has to exceed, respectively.\n\n\n-----Output-----\n\nFor each test case print a single integer\u00a0\u2014 the smallest number of operations needed. Separate answers with line breaks.\n\n\n-----Example-----\nInput\n2\n1 2 3\n5 4 100\n\nOutput\n2\n7\n\n\n\n-----Note-----\n\nIn the first case we cannot make a variable exceed $3$ in one operation. One way of achieving this in two operations is to perform \"b += a\" twice."} +{"id": "02565", "text": "You are given two sequences $a_1, a_2, \\dots, a_n$ and $b_1, b_2, \\dots, b_n$. Each element of both sequences is either $0$, $1$ or $2$. The number of elements $0$, $1$, $2$ in the sequence $a$ is $x_1$, $y_1$, $z_1$ respectively, and the number of elements $0$, $1$, $2$ in the sequence $b$ is $x_2$, $y_2$, $z_2$ respectively.\n\nYou can rearrange the elements in both sequences $a$ and $b$ however you like. After that, let's define a sequence $c$ as follows:\n\n$c_i = \\begin{cases} a_i b_i & \\mbox{if }a_i > b_i \\\\ 0 & \\mbox{if }a_i = b_i \\\\ -a_i b_i & \\mbox{if }a_i < b_i \\end{cases}$\n\nYou'd like to make $\\sum_{i=1}^n c_i$ (the sum of all elements of the sequence $c$) as large as possible. What is the maximum possible sum?\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10^4$)\u00a0\u2014 the number of test cases.\n\nEach test case consists of two lines. The first line of each test case contains three integers $x_1$, $y_1$, $z_1$ ($0 \\le x_1, y_1, z_1 \\le 10^8$)\u00a0\u2014 the number of $0$-s, $1$-s and $2$-s in the sequence $a$.\n\nThe second line of each test case also contains three integers $x_2$, $y_2$, $z_2$ ($0 \\le x_2, y_2, z_2 \\le 10^8$; $x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$)\u00a0\u2014 the number of $0$-s, $1$-s and $2$-s in the sequence $b$.\n\n\n-----Output-----\n\nFor each test case, print the maximum possible sum of the sequence $c$.\n\n\n-----Example-----\nInput\n3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1\n\nOutput\n4\n2\n0\n\n\n\n-----Note-----\n\nIn the first sample, one of the optimal solutions is:\n\n$a = \\{2, 0, 1, 1, 0, 2, 1\\}$\n\n$b = \\{1, 0, 1, 0, 2, 1, 0\\}$\n\n$c = \\{2, 0, 0, 0, 0, 2, 0\\}$\n\nIn the second sample, one of the optimal solutions is:\n\n$a = \\{0, 2, 0, 0, 0\\}$\n\n$b = \\{1, 1, 0, 1, 0\\}$\n\n$c = \\{0, 2, 0, 0, 0\\}$\n\nIn the third sample, the only possible solution is:\n\n$a = \\{2\\}$\n\n$b = \\{2\\}$\n\n$c = \\{0\\}$"} +{"id": "02566", "text": "Berland State University invites people from all over the world as guest students. You can come to the capital of Berland and study with the best teachers in the country.\n\nBerland State University works every day of the week, but classes for guest students are held on the following schedule. You know the sequence of seven integers $a_1, a_2, \\dots, a_7$ ($a_i = 0$ or $a_i = 1$): $a_1=1$ if and only if there are classes for guest students on Sundays; $a_2=1$ if and only if there are classes for guest students on Mondays; ... $a_7=1$ if and only if there are classes for guest students on Saturdays. \n\nThe classes for guest students are held in at least one day of a week.\n\nYou want to visit the capital of Berland and spend the minimum number of days in it to study $k$ days as a guest student in Berland State University. Write a program to find the length of the shortest continuous period of days to stay in the capital to study exactly $k$ days as a guest student.\n\n\n-----Input-----\n\nThe first line of the input contains integer $t$ ($1 \\le t \\le 10\\,000$) \u2014 the number of test cases to process. For each test case independently solve the problem and print the answer. \n\nEach test case consists of two lines. The first of them contains integer $k$ ($1 \\le k \\le 10^8$) \u2014 the required number of days to study as a guest student. The second line contains exactly seven integers $a_1, a_2, \\dots, a_7$ ($a_i = 0$ or $a_i = 1$) where $a_i=1$ if and only if classes for guest students are held on the $i$-th day of a week.\n\n\n-----Output-----\n\nPrint $t$ lines, the $i$-th line should contain the answer for the $i$-th test case \u2014 the length of the shortest continuous period of days you need to stay to study exactly $k$ days as a guest student.\n\n\n-----Example-----\nInput\n3\n2\n0 1 0 0 0 0 0\n100000000\n1 0 0 0 1 0 1\n1\n1 0 0 0 0 0 0\n\nOutput\n8\n233333332\n1\n\n\n\n-----Note-----\n\nIn the first test case you must arrive to the capital of Berland on Monday, have classes on this day, spend a week until next Monday and have classes on the next Monday. In total you need to spend $8$ days in the capital of Berland."} +{"id": "02567", "text": "A binary string is a string where each character is either 0 or 1. Two binary strings $a$ and $b$ of equal length are similar, if they have the same character in some position (there exists an integer $i$ such that $a_i = b_i$). For example: 10010 and 01111 are similar (they have the same character in position $4$); 10010 and 11111 are similar; 111 and 111 are similar; 0110 and 1001 are not similar. \n\nYou are given an integer $n$ and a binary string $s$ consisting of $2n-1$ characters. Let's denote $s[l..r]$ as the contiguous substring of $s$ starting with $l$-th character and ending with $r$-th character (in other words, $s[l..r] = s_l s_{l + 1} s_{l + 2} \\dots s_r$).\n\nYou have to construct a binary string $w$ of length $n$ which is similar to all of the following strings: $s[1..n]$, $s[2..n+1]$, $s[3..n+2]$, ..., $s[n..2n-1]$.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 50$).\n\nThe second line of each test case contains the binary string $s$ of length $2n - 1$. Each character $s_i$ is either 0 or 1.\n\n\n-----Output-----\n\nFor each test case, print the corresponding binary string $w$ of length $n$. If there are multiple such strings \u2014 print any of them. It can be shown that at least one string $w$ meeting the constraints always exists.\n\n\n-----Example-----\nInput\n4\n1\n1\n3\n00000\n4\n1110000\n2\n101\n\nOutput\n1\n000\n1010\n00\n\n\n\n-----Note-----\n\nThe explanation of the sample case (equal characters in equal positions are bold):\n\nThe first test case: $\\mathbf{1}$ is similar to $s[1..1] = \\mathbf{1}$. \n\nThe second test case: $\\mathbf{000}$ is similar to $s[1..3] = \\mathbf{000}$; $\\mathbf{000}$ is similar to $s[2..4] = \\mathbf{000}$; $\\mathbf{000}$ is similar to $s[3..5] = \\mathbf{000}$. \n\nThe third test case: $\\mathbf{1}0\\mathbf{10}$ is similar to $s[1..4] = \\mathbf{1}1\\mathbf{10}$; $\\mathbf{1}01\\mathbf{0}$ is similar to $s[2..5] = \\mathbf{1}10\\mathbf{0}$; $\\mathbf{10}1\\mathbf{0}$ is similar to $s[3..6] = \\mathbf{10}0\\mathbf{0}$; $1\\mathbf{0}1\\mathbf{0}$ is similar to $s[4..7] = 0\\mathbf{0}0\\mathbf{0}$. \n\nThe fourth test case: $0\\mathbf{0}$ is similar to $s[1..2] = 1\\mathbf{0}$; $\\mathbf{0}0$ is similar to $s[2..3] = \\mathbf{0}1$."} +{"id": "02568", "text": "You are given a string $s$ consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode: res = 0\n\nfor init = 0 to inf\n\n cur = init\n\n ok = true\n\n for i = 1 to |s|\n\n res = res + 1\n\n if s[i] == '+'\n\n cur = cur + 1\n\n else\n\n cur = cur - 1\n\n if cur < 0\n\n ok = false\n\n break\n\n if ok\n\n break\n\n\n\nNote that the $inf$ denotes infinity, and the characters of the string are numbered from $1$ to $|s|$.\n\nYou have to calculate the value of the $res$ after the process ends.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases.\n\nThe only lines of each test case contains string $s$ ($1 \\le |s| \\le 10^6$) consisting only of characters + and -.\n\nIt's guaranteed that sum of $|s|$ over all test cases doesn't exceed $10^6$.\n\n\n-----Output-----\n\nFor each test case print one integer \u2014 the value of the $res$ after the process ends.\n\n\n-----Example-----\nInput\n3\n--+-\n---\n++--+-\n\nOutput\n7\n9\n6"} +{"id": "02569", "text": "Lee tried so hard to make a good div.2 D problem to balance his recent contest, but it still doesn't feel good at all. Lee invented it so tediously slow that he managed to develop a phobia about div.2 D problem setting instead. And now he is hiding behind the bushes...\n\nLet's define a Rooted Dead Bush (RDB) of level $n$ as a rooted tree constructed as described below.\n\nA rooted dead bush of level $1$ is a single vertex. To construct an RDB of level $i$ we, at first, construct an RDB of level $i-1$, then for each vertex $u$: if $u$ has no children then we will add a single child to it; if $u$ has one child then we will add two children to it; if $u$ has more than one child, then we will skip it. \n\n [Image] Rooted Dead Bushes of level $1$, $2$ and $3$. \n\nLet's define a claw as a rooted tree with four vertices: one root vertex (called also as center) with three children. It looks like a claw:\n\n [Image] The center of the claw is the vertex with label $1$. \n\nLee has a Rooted Dead Bush of level $n$. Initially, all vertices of his RDB are green.\n\nIn one move, he can choose a claw in his RDB, if all vertices in the claw are green and all vertices of the claw are children of its center, then he colors the claw's vertices in yellow.\n\nHe'd like to know the maximum number of yellow vertices he can achieve. Since the answer might be very large, print it modulo $10^9+7$.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10^4$)\u00a0\u2014 the number of test cases.\n\nNext $t$ lines contain test cases\u00a0\u2014 one per line.\n\nThe first line of each test case contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^6$)\u00a0\u2014 the level of Lee's RDB.\n\n\n-----Output-----\n\nFor each test case, print a single integer\u00a0\u2014 the maximum number of yellow vertices Lee can make modulo $10^9 + 7$.\n\n\n-----Example-----\nInput\n7\n1\n2\n3\n4\n5\n100\n2000000\n\nOutput\n0\n0\n4\n4\n12\n990998587\n804665184\n\n\n\n-----Note-----\n\nIt's easy to see that the answer for RDB of level $1$ or $2$ is $0$.\n\nThe answer for RDB of level $3$ is $4$ since there is only one claw we can choose: $\\{1, 2, 3, 4\\}$.\n\nThe answer for RDB of level $4$ is $4$ since we can choose either single claw $\\{1, 3, 2, 4\\}$ or single claw $\\{2, 7, 5, 6\\}$. There are no other claws in the RDB of level $4$ (for example, we can't choose $\\{2, 1, 7, 6\\}$, since $1$ is not a child of center vertex $2$).\n\n $\\therefore$ Rooted Dead Bush of level 4."} +{"id": "02570", "text": "You are given two arrays $a$ and $b$, each consisting of $n$ positive integers, and an integer $x$. Please determine if one can rearrange the elements of $b$ so that $a_i + b_i \\leq x$ holds for each $i$ ($1 \\le i \\le n$).\n\n\n-----Input-----\n\nThe first line of input contains one integer $t$ ($1 \\leq t \\leq 100$)\u00a0\u2014 the number of test cases. $t$ blocks follow, each describing an individual test case.\n\nThe first line of each test case contains two integers $n$ and $x$ ($1 \\leq n \\leq 50$; $1 \\leq x \\leq 1000$)\u00a0\u2014 the length of arrays $a$ and $b$, and the parameter $x$, described in the problem statement.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_1 \\le a_2 \\le \\dots \\le a_n \\leq x$)\u00a0\u2014 the elements of array $a$ in non-descending order.\n\nThe third line of each test case contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($1 \\leq b_1 \\le b_2 \\le \\dots \\le b_n \\leq x$)\u00a0\u2014 the elements of array $b$ in non-descending order.\n\nTest cases are separated by a blank line.\n\n\n-----Output-----\n\nFor each test case print Yes if one can rearrange the corresponding array $b$ so that $a_i + b_i \\leq x$ holds for each $i$ ($1 \\le i \\le n$) or No otherwise.\n\nEach character can be printed in any case.\n\n\n-----Example-----\nInput\n4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5\n\nOutput\nYes\nYes\nNo\nNo\n\n\n\n-----Note-----\n\nIn the first test case, one can rearrange $b$ so it'll look like $[1, 2, 1]$. In this case, $1 + 1 \\leq 4$; $2 + 2 \\leq 4$; $3 + 1 \\leq 4$.\n\nIn the second test case, one can set $b$ to $[5, 2]$, then $1 + 5 \\leq 6$; $4 + 2 \\leq 6$.\n\nIn the third test case, no matter how one shuffles array $b$, $a_4 + b_4 = 4 + b_4 > 4$.\n\nIn the fourth test case, there is only one rearrangement of array $b$ and it doesn't satisfy the condition since $5 + 5 > 5$."} +{"id": "02571", "text": "Naruto has sneaked into the Orochimaru's lair and is now looking for Sasuke. There are $T$ rooms there. Every room has a door into it, each door can be described by the number $n$ of seals on it and their integer energies $a_1$, $a_2$, ..., $a_n$. All energies $a_i$ are nonzero and do not exceed $100$ by absolute value. Also, $n$ is even.\n\nIn order to open a door, Naruto must find such $n$ seals with integer energies $b_1$, $b_2$, ..., $b_n$ that the following equality holds: $a_{1} \\cdot b_{1} + a_{2} \\cdot b_{2} + ... + a_{n} \\cdot b_{n} = 0$. All $b_i$ must be nonzero as well as $a_i$ are, and also must not exceed $100$ by absolute value. Please find required seals for every room there.\n\n\n-----Input-----\n\nThe first line contains the only integer $T$ ($1 \\leq T \\leq 1000$) standing for the number of rooms in the Orochimaru's lair. The other lines contain descriptions of the doors.\n\nEach description starts with the line containing the only even integer $n$ ($2 \\leq n \\leq 100$) denoting the number of seals.\n\nThe following line contains the space separated sequence of nonzero integers $a_1$, $a_2$, ..., $a_n$ ($|a_{i}| \\leq 100$, $a_{i} \\neq 0$) denoting the energies of seals.\n\n\n-----Output-----\n\nFor each door print a space separated sequence of nonzero integers $b_1$, $b_2$, ..., $b_n$ ($|b_{i}| \\leq 100$, $b_{i} \\neq 0$) denoting the seals that can open the door. If there are multiple valid answers, print any. It can be proven that at least one answer always exists.\n\n\n-----Example-----\nInput\n2\n2\n1 100\n4\n1 2 3 6\n\nOutput\n-100 1\n1 1 1 -1\n\n\n\n-----Note-----\n\nFor the first door Naruto can use energies $[-100, 1]$. The required equality does indeed hold: $1 \\cdot (-100) + 100 \\cdot 1 = 0$.\n\nFor the second door Naruto can use, for example, energies $[1, 1, 1, -1]$. The required equality also holds: $1 \\cdot 1 + 2 \\cdot 1 + 3 \\cdot 1 + 6 \\cdot (-1) = 0$."} +{"id": "02572", "text": "A matrix of size $n \\times m$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $(a_1, a_2, \\dots , a_k)$ is a palindrome, if for any integer $i$ ($1 \\le i \\le k$) the equality $a_i = a_{k - i + 1}$ holds.\n\nSasha owns a matrix $a$ of size $n \\times m$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.\n\nHelp him!\n\n\n-----Input-----\n\nThe first line contains a single integer $t$\u00a0\u2014 the number of test cases ($1 \\le t \\le 10$). The $t$ tests follow.\n\nThe first line of each test contains two integers $n$ and $m$ ($1 \\le n, m \\le 100$)\u00a0\u2014 the size of the matrix.\n\nEach of the next $n$ lines contains $m$ integers $a_{i, j}$ ($0 \\le a_{i, j} \\le 10^9$)\u00a0\u2014 the elements of the matrix.\n\n\n-----Output-----\n\nFor each test output the smallest number of operations required to make the matrix nice.\n\n\n-----Example-----\nInput\n2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18\n\nOutput\n8\n42\n\n\n\n-----Note-----\n\nIn the first test case we can, for example, obtain the following nice matrix in $8$ operations:\n\n\n\n2 2\n\n4 4\n\n4 4\n\n2 2\n\n\n\nIn the second test case we can, for example, obtain the following nice matrix in $42$ operations:\n\n\n\n5 6 6 5\n\n6 6 6 6\n\n5 6 6 5"} +{"id": "02573", "text": "You are given a chessboard consisting of $n$ rows and $n$ columns. Rows are numbered from bottom to top from $1$ to $n$. Columns are numbered from left to right from $1$ to $n$. The cell at the intersection of the $x$-th column and the $y$-th row is denoted as $(x, y)$. Furthermore, the $k$-th column is a special column. \n\nInitially, the board is empty. There are $m$ changes to the board. During the $i$-th change one pawn is added or removed from the board. The current board is good if we can move all pawns to the special column by the followings rules: Pawn in the cell $(x, y)$ can be moved to the cell $(x, y + 1)$, $(x - 1, y + 1)$ or $(x + 1, y + 1)$; You can make as many such moves as you like; Pawns can not be moved outside the chessboard; Each cell can not contain more than one pawn. \n\nThe current board may not always be good. To fix it, you can add new rows to the board. New rows are added at the top, i. e. they will have numbers $n+1, n+2, n+3, \\dots$.\n\nAfter each of $m$ changes, print one integer \u2014 the minimum number of rows which you have to add to make the board good.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $k$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5; 1 \\le k \\le n$) \u2014 the size of the board, the index of the special column and the number of changes respectively.\n\nThen $m$ lines follow. The $i$-th line contains two integers $x$ and $y$ ($1 \\le x, y \\le n$) \u2014 the index of the column and the index of the row respectively. If there is no pawn in the cell $(x, y)$, then you add a pawn to this cell, otherwise \u2014 you remove the pawn from this cell.\n\n\n-----Output-----\n\nAfter each change print one integer \u2014 the minimum number of rows which you have to add to make the board good.\n\n\n-----Example-----\nInput\n5 3 5\n4 4\n3 5\n2 4\n3 4\n3 5\n\nOutput\n0\n1\n2\n2\n1"} +{"id": "02574", "text": "You are given an array of integers $a_1,a_2,\\ldots,a_n$. Find the maximum possible value of $a_ia_ja_ka_la_t$ among all five indices $(i, j, k, l, t)$ ($i1$, $a_{i-1} \\le a_i$ must hold.\n\nTo achieve his goal, Wheatley can exchange two neighbouring cubes. It means that for any $i>1$ you can exchange cubes on positions $i-1$ and $i$.\n\nBut there is a problem: Wheatley is very impatient. If Wheatley needs more than $\\frac{n \\cdot (n-1)}{2}-1$ exchange operations, he won't do this boring work.\n\nWheatly wants to know: can cubes be sorted under this conditions?\n\n\n-----Input-----\n\nEach test contains multiple test cases.\n\nThe first line contains one positive integer $t$ ($1 \\le t \\le 1000$), denoting the number of test cases. Description of the test cases follows.\n\nThe first line of each test case contains one positive integer $n$ ($2 \\le n \\le 5 \\cdot 10^4$)\u00a0\u2014 number of cubes.\n\nThe second line contains $n$ positive integers $a_i$ ($1 \\le a_i \\le 10^9$)\u00a0\u2014 volumes of cubes.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case, print a word in a single line: \"YES\" (without quotation marks) if the cubes can be sorted and \"NO\" (without quotation marks) otherwise.\n\n\n-----Example-----\nInput\n3\n5\n5 3 2 1 4\n6\n2 2 2 2 2 2\n2\n2 1\n\nOutput\nYES\nYES\nNO\n\n\n\n-----Note-----\n\nIn the first test case it is possible to sort all the cubes in $7$ exchanges.\n\nIn the second test case the cubes are already sorted.\n\nIn the third test case we can make $0$ exchanges, but the cubes are not sorted yet, so the answer is \"NO\"."} +{"id": "02602", "text": "Anna is a girl so brave that she is loved by everyone in the city and citizens love her cookies. She is planning to hold a party with cookies. Now she has $a$ vanilla cookies and $b$ chocolate cookies for the party.\n\nShe invited $n$ guests of the first type and $m$ guests of the second type to the party. They will come to the party in some order. After coming to the party, each guest will choose the type of cookie (vanilla or chocolate) to eat. There is a difference in the way how they choose that type:\n\nIf there are $v$ vanilla cookies and $c$ chocolate cookies at the moment, when the guest comes, then if the guest of the first type: if $v>c$ the guest selects a vanilla cookie. Otherwise, the guest selects a chocolate cookie. if the guest of the second type: if $v>c$ the guest selects a chocolate cookie. Otherwise, the guest selects a vanilla cookie. \n\nAfter that: If there is at least one cookie of the selected type, the guest eats one. Otherwise (there are no cookies of the selected type), the guest gets angry and returns to home. \n\nAnna wants to know if there exists some order of guests, such that no one guest gets angry. Your task is to answer her question.\n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases. Next $t$ lines contain descriptions of test cases.\n\nFor each test case, the only line contains four integers $a$, $b$, $n$, $m$ ($0 \\le a,b,n,m \\le 10^{18}, n+m \\neq 0$).\n\n\n-----Output-----\n\nFor each test case, print the answer in one line. If there exists at least one valid order, print \"Yes\". Otherwise, print \"No\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n6\n2 2 1 2\n0 100 0 1\n12 13 25 1\n27 83 14 25\n0 0 1 0\n1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000\n\nOutput\nYes\nNo\nNo\nYes\nNo\nYes\n\n\n\n-----Note-----\n\nIn the first test case, let's consider the order $\\{1, 2, 2\\}$ of types of guests. Then: The first guest eats a chocolate cookie. After that, there are $2$ vanilla cookies and $1$ chocolate cookie. The second guest eats a chocolate cookie. After that, there are $2$ vanilla cookies and $0$ chocolate cookies. The last guest selects a chocolate cookie, but there are no chocolate cookies. So, the guest gets angry. \n\nSo, this order can't be chosen by Anna.\n\nLet's consider the order $\\{2, 2, 1\\}$ of types of guests. Then: The first guest eats a vanilla cookie. After that, there is $1$ vanilla cookie and $2$ chocolate cookies. The second guest eats a vanilla cookie. After that, there are $0$ vanilla cookies and $2$ chocolate cookies. The last guest eats a chocolate cookie. After that, there are $0$ vanilla cookies and $1$ chocolate cookie. \n\nSo, the answer to this test case is \"Yes\".\n\nIn the fifth test case, it is illustrated, that the number of cookies ($a + b$) can be equal to zero, but the number of guests ($n + m$) can't be equal to zero.\n\nIn the sixth test case, be careful about the overflow of $32$-bit integer type."} +{"id": "02603", "text": "You are given an array $a_1, a_2, \\dots, a_n$ where all $a_i$ are integers and greater than $0$.\n\n In one operation, you can choose two different indices $i$ and $j$ ($1 \\le i, j \\le n$). If $gcd(a_i, a_j)$ is equal to the minimum element of the whole array $a$, you can swap $a_i$ and $a_j$. $gcd(x, y)$ denotes the greatest common divisor (GCD) of integers $x$ and $y$.\n\n Now you'd like to make $a$ non-decreasing using the operation any number of times (possibly zero). Determine if you can do this.\n\n An array $a$ is non-decreasing if and only if $a_1 \\le a_2 \\le \\ldots \\le a_n$.\n\n\n-----Input-----\n\n The first line contains one integer $t$ ($1 \\le t \\le 10^4$)\u00a0\u2014 the number of test cases.\n\n The first line of each test case contains one integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the length of array $a$.\n\n The second line of each test case contains $n$ positive integers $a_1, a_2, \\ldots a_n$ ($1 \\le a_i \\le 10^9$)\u00a0\u2014 the array itself.\n\n It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.\n\n\n-----Output-----\n\n For each test case, output \"YES\" if it is possible to make the array $a$ non-decreasing using the described operation, or \"NO\" if it is impossible to do so.\n\n\n-----Example-----\nInput\n4\n1\n8\n6\n4 3 6 6 2 9\n4\n4 5 6 7\n5\n7 5 2 2 4\n\nOutput\nYES\nYES\nYES\nNO\n\n\n\n-----Note-----\n\n In the first and third sample, the array is already non-decreasing.\n\n In the second sample, we can swap $a_1$ and $a_3$ first, and swap $a_1$ and $a_5$ second to make the array non-decreasing.\n\n In the forth sample, we cannot the array non-decreasing using the operation."} +{"id": "02604", "text": "Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.\n\nThe pizza is a circle of radius r and center at the origin. Pizza consists of the main part \u2014 circle of radius r - d with center at the origin, and crust around the main part of the width d. Pieces of sausage are also circles. The radius of the i\u00a0-th piece of the sausage is r_{i}, and the center is given as a pair (x_{i}, y_{i}).\n\nGleb asks you to help determine the number of pieces of sausage caught on the crust. A piece of sausage got on the crust, if it completely lies on the crust.\n\n\n-----Input-----\n\nFirst string contains two integer numbers r and d (0 \u2264 d < r \u2264 500)\u00a0\u2014 the radius of pizza and the width of crust.\n\nNext line contains one integer number n\u00a0\u2014 the number of pieces of sausage (1 \u2264 n \u2264 10^5).\n\nEach of next n lines contains three integer numbers x_{i}, y_{i} and r_{i} ( - 500 \u2264 x_{i}, y_{i} \u2264 500, 0 \u2264 r_{i} \u2264 500), where x_{i} and y_{i} are coordinates of the center of i-th peace of sausage, r_{i}\u00a0\u2014 radius of i-th peace of sausage.\n\n\n-----Output-----\n\nOutput the number of pieces of sausage that lay on the crust.\n\n\n-----Examples-----\nInput\n8 4\n7\n7 8 1\n-7 3 2\n0 2 1\n0 -2 2\n-3 -3 1\n0 6 2\n5 3 1\n\nOutput\n2\n\nInput\n10 8\n4\n0 0 9\n0 0 10\n1 0 1\n1 0 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nBelow is a picture explaining the first example. Circles of green color denote pieces of sausage lying on the crust.\n\n [Image]"} +{"id": "02605", "text": "Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXX\u00a0\u2014 beautiful, but little-known northern country.\n\nHere are some interesting facts about XXX: XXX consists of n cities, k of whose (just imagine!) are capital cities. All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals to c_{i}. All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route 1 \u2014 2 \u2014 ... \u2014 n \u2014 1. Formally, for every 1 \u2264 i < n there is a road between i-th and i + 1-th city, and another one between 1-st and n-th city. Each capital city is connected with each other city directly by the roads. Formally, if city x is a capital city, then for every 1 \u2264 i \u2264 n, i \u2260 x, there is a road between cities x and i. There is at most one road between any two cities. Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities i and j, price of passing it equals c_{i}\u00b7c_{j}.\n\nMishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities a and b (a < b), such that there is a road between a and b you are to find sum of products c_{a}\u00b7c_{b}. Will you help her?\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (3 \u2264 n \u2264 100 000, 1 \u2264 k \u2264 n)\u00a0\u2014 the number of cities in XXX and the number of capital cities among them.\n\nThe second line of the input contains n integers c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 10 000)\u00a0\u2014 beauty values of the cities.\n\nThe third line of the input contains k distinct integers id_1, id_2, ..., id_{k} (1 \u2264 id_{i} \u2264 n)\u00a0\u2014 indices of capital cities. Indices are given in ascending order.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 summary price of passing each of the roads in XXX.\n\n\n-----Examples-----\nInput\n4 1\n2 3 1 2\n3\n\nOutput\n17\nInput\n5 2\n3 5 2 2 4\n1 4\n\nOutput\n71\n\n\n-----Note-----\n\nThis image describes first sample case:\n\n[Image]\n\nIt is easy to see that summary price is equal to 17.\n\nThis image describes second sample case:\n\n[Image]\n\nIt is easy to see that summary price is equal to 71."} +{"id": "02606", "text": "Chef Monocarp has just put $n$ dishes into an oven. He knows that the $i$-th dish has its optimal cooking time equal to $t_i$ minutes.\n\nAt any positive integer minute $T$ Monocarp can put no more than one dish out of the oven. If the $i$-th dish is put out at some minute $T$, then its unpleasant value is $|T - t_i|$\u00a0\u2014 the absolute difference between $T$ and $t_i$. Once the dish is out of the oven, it can't go back in.\n\nMonocarp should put all the dishes out of the oven. What is the minimum total unpleasant value Monocarp can obtain?\n\n\n-----Input-----\n\nThe first line contains a single integer $q$ ($1 \\le q \\le 200$)\u00a0\u2014 the number of testcases.\n\nThen $q$ testcases follow.\n\nThe first line of the testcase contains a single integer $n$ ($1 \\le n \\le 200$)\u00a0\u2014 the number of dishes in the oven.\n\nThe second line of the testcase contains $n$ integers $t_1, t_2, \\dots, t_n$ ($1 \\le t_i \\le n$)\u00a0\u2014 the optimal cooking time for each dish.\n\nThe sum of $n$ over all $q$ testcases doesn't exceed $200$.\n\n\n-----Output-----\n\nPrint a single integer for each testcase\u00a0\u2014 the minimum total unpleasant value Monocarp can obtain when he puts out all the dishes out of the oven. Remember that Monocarp can only put the dishes out at positive integer minutes and no more than one dish at any minute.\n\n\n-----Example-----\nInput\n6\n6\n4 2 4 4 5 2\n7\n7 7 7 7 7 7 7\n1\n1\n5\n5 1 2 4 3\n4\n1 4 4 4\n21\n21 8 1 4 1 5 21 1 8 21 11 21 11 3 12 8 19 15 9 11 13\n\nOutput\n4\n12\n0\n0\n2\n21\n\n\n\n-----Note-----\n\nIn the first example Monocarp can put out the dishes at minutes $3, 1, 5, 4, 6, 2$. That way the total unpleasant value will be $|4 - 3| + |2 - 1| + |4 - 5| + |4 - 4| + |6 - 5| + |2 - 2| = 4$.\n\nIn the second example Monocarp can put out the dishes at minutes $4, 5, 6, 7, 8, 9, 10$.\n\nIn the third example Monocarp can put out the dish at minute $1$.\n\nIn the fourth example Monocarp can put out the dishes at minutes $5, 1, 2, 4, 3$.\n\nIn the fifth example Monocarp can put out the dishes at minutes $1, 3, 4, 5$."} +{"id": "02607", "text": "A string is called beautiful if no two consecutive characters are equal. For example, \"ababcb\", \"a\" and \"abab\" are beautiful strings, while \"aaaaaa\", \"abaa\" and \"bb\" are not.\n\nAhcl wants to construct a beautiful string. He has a string $s$, consisting of only characters 'a', 'b', 'c' and '?'. Ahcl needs to replace each character '?' with one of the three characters 'a', 'b' or 'c', such that the resulting string is beautiful. Please help him!\n\nMore formally, after replacing all characters '?', the condition $s_i \\neq s_{i+1}$ should be satisfied for all $1 \\leq i \\leq |s| - 1$, where $|s|$ is the length of the string $s$.\n\n\n-----Input-----\n\nThe first line contains positive integer $t$ ($1 \\leq t \\leq 1000$)\u00a0\u2014 the number of test cases. Next $t$ lines contain the descriptions of test cases.\n\nEach line contains a non-empty string $s$ consisting of only characters 'a', 'b', 'c' and '?'. \n\nIt is guaranteed that in each test case a string $s$ has at least one character '?'. The sum of lengths of strings $s$ in all test cases does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case given in the input print the answer in the following format:\n\n If it is impossible to create a beautiful string, print \"-1\" (without quotes); Otherwise, print the resulting beautiful string after replacing all '?' characters. If there are multiple answers, you can print any of them. \n\n\n-----Example-----\nInput\n3\na???cb\na??bbc\na?b?c\n\nOutput\nababcb\n-1\nacbac\n\n\n\n-----Note-----\n\nIn the first test case, all possible correct answers are \"ababcb\", \"abcacb\", \"abcbcb\", \"acabcb\" and \"acbacb\". The two answers \"abcbab\" and \"abaabc\" are incorrect, because you can replace only '?' characters and the resulting string must be beautiful.\n\nIn the second test case, it is impossible to create a beautiful string, because the $4$-th and $5$-th characters will be always equal.\n\nIn the third test case, the only answer is \"acbac\"."} +{"id": "02608", "text": "Recently, Masha was presented with a chessboard with a height of $n$ and a width of $m$.\n\nThe rows on the chessboard are numbered from $1$ to $n$ from bottom to top. The columns are numbered from $1$ to $m$ from left to right. Therefore, each cell can be specified with the coordinates $(x,y)$, where $x$ is the column number, and $y$ is the row number (do not mix up).\n\nLet us call a rectangle with coordinates $(a,b,c,d)$ a rectangle lower left point of which has coordinates $(a,b)$, and the upper right one\u00a0\u2014 $(c,d)$.\n\nThe chessboard is painted black and white as follows:\n\n [Image] \n\n An example of a chessboard. \n\nMasha was very happy with the gift and, therefore, invited her friends Maxim and Denis to show off. The guys decided to make her a treat\u00a0\u2014 they bought her a can of white and a can of black paint, so that if the old board deteriorates, it can be repainted. When they came to Masha, something unpleasant happened: first, Maxim went over the threshold and spilled white paint on the rectangle $(x_1,y_1,x_2,y_2)$. Then after him Denis spilled black paint on the rectangle $(x_3,y_3,x_4,y_4)$.\n\nTo spill paint of color $color$ onto a certain rectangle means that all the cells that belong to the given rectangle become $color$. The cell dyeing is superimposed on each other (if at first some cell is spilled with white paint and then with black one, then its color will be black).\n\nMasha was shocked! She drove away from the guests and decided to find out how spoiled the gift was. For this, she needs to know the number of cells of white and black color. Help her find these numbers!\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^3$)\u00a0\u2014 the number of test cases.\n\nEach of them is described in the following format:\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n,m \\le 10^9$)\u00a0\u2014 the size of the board.\n\nThe second line contains four integers $x_1$, $y_1$, $x_2$, $y_2$ ($1 \\le x_1 \\le x_2 \\le m, 1 \\le y_1 \\le y_2 \\le n$)\u00a0\u2014 the coordinates of the rectangle, the white paint was spilled on.\n\nThe third line contains four integers $x_3$, $y_3$, $x_4$, $y_4$ ($1 \\le x_3 \\le x_4 \\le m, 1 \\le y_3 \\le y_4 \\le n$)\u00a0\u2014 the coordinates of the rectangle, the black paint was spilled on.\n\n\n-----Output-----\n\nOutput $t$ lines, each of which contains two numbers\u00a0\u2014 the number of white and black cells after spilling paint, respectively.\n\n\n-----Example-----\nInput\n5\n2 2\n1 1 2 2\n1 1 2 2\n3 4\n2 2 3 2\n3 1 4 3\n1 5\n1 1 5 1\n3 1 5 1\n4 4\n1 1 4 2\n1 3 4 4\n3 4\n1 2 4 2\n2 1 3 3\n\nOutput\n0 4\n3 9\n2 3\n8 8\n4 8\n\n\n\n-----Note-----\n\nExplanation for examples:\n\nThe first picture of each illustration shows how the field looked before the dyes were spilled. The second picture of each illustration shows how the field looked after Maxim spoiled white dye (the rectangle on which the dye was spilled is highlighted with red). The third picture in each illustration shows how the field looked after Denis spoiled black dye (the rectangle on which the dye was spilled is highlighted with red).\n\nIn the first test, the paint on the field changed as follows:\n\n [Image] \n\nIn the second test, the paint on the field changed as follows:\n\n [Image] \n\nIn the third test, the paint on the field changed as follows:\n\n [Image] \n\nIn the fourth test, the paint on the field changed as follows:\n\n [Image] \n\nIn the fifth test, the paint on the field changed as follows:\n\n [Image]"} +{"id": "02609", "text": "There are $n$ segments on a $Ox$ axis $[l_1, r_1]$, $[l_2, r_2]$, ..., $[l_n, r_n]$. Segment $[l, r]$ covers all points from $l$ to $r$ inclusive, so all $x$ such that $l \\le x \\le r$.\n\nSegments can be placed arbitrarily \u00a0\u2014 be inside each other, coincide and so on. Segments can degenerate into points, that is $l_i=r_i$ is possible.\n\nUnion of the set of segments is such a set of segments which covers exactly the same set of points as the original set. For example: if $n=3$ and there are segments $[3, 6]$, $[100, 100]$, $[5, 8]$ then their union is $2$ segments: $[3, 8]$ and $[100, 100]$; if $n=5$ and there are segments $[1, 2]$, $[2, 3]$, $[4, 5]$, $[4, 6]$, $[6, 6]$ then their union is $2$ segments: $[1, 3]$ and $[4, 6]$. \n\nObviously, a union is a set of pairwise non-intersecting segments.\n\nYou are asked to erase exactly one segment of the given $n$ so that the number of segments in the union of the rest $n-1$ segments is maximum possible.\n\nFor example, if $n=4$ and there are segments $[1, 4]$, $[2, 3]$, $[3, 6]$, $[5, 7]$, then: erasing the first segment will lead to $[2, 3]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the second segment will lead to $[1, 4]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the third segment will lead to $[1, 4]$, $[2, 3]$, $[5, 7]$ remaining, which have $2$ segments in their union; erasing the fourth segment will lead to $[1, 4]$, $[2, 3]$, $[3, 6]$ remaining, which have $1$ segment in their union. \n\nThus, you are required to erase the third segment to get answer $2$.\n\nWrite a program that will find the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.\n\nNote that if there are multiple equal segments in the given set, then you can erase only one of them anyway. So the set after erasing will have exactly $n-1$ segments.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10^4$)\u00a0\u2014 the number of test cases in the test. Then the descriptions of $t$ test cases follow.\n\nThe first of each test case contains a single integer $n$ ($2 \\le n \\le 2\\cdot10^5$)\u00a0\u2014 the number of segments in the given set. Then $n$ lines follow, each contains a description of a segment \u2014 a pair of integers $l_i$, $r_i$ ($-10^9 \\le l_i \\le r_i \\le 10^9$), where $l_i$ and $r_i$ are the coordinates of the left and right borders of the $i$-th segment, respectively.\n\nThe segments are given in an arbitrary order.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2\\cdot10^5$.\n\n\n-----Output-----\n\nPrint $t$ integers \u2014 the answers to the $t$ given test cases in the order of input. The answer is the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.\n\n\n-----Example-----\nInput\n3\n4\n1 4\n2 3\n3 6\n5 7\n3\n5 5\n5 5\n5 5\n6\n3 3\n1 1\n5 5\n1 5\n2 2\n4 4\n\nOutput\n2\n1\n5"} +{"id": "02610", "text": "This is the hard version of this problem. The only difference is the constraint on $k$ \u2014 the number of gifts in the offer. In this version: $2 \\le k \\le n$.\n\nVasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky\u00a0\u2014 today the offer \"$k$ of goods for the price of one\" is held in store.\n\nUsing this offer, Vasya can buy exactly $k$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.\n\nMore formally, for each good, its price is determined by $a_i$\u00a0\u2014 the number of coins it costs. Initially, Vasya has $p$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:\n\n Vasya can buy one good with the index $i$ if he currently has enough coins (i.e $p \\ge a_i$). After buying this good, the number of Vasya's coins will decrease by $a_i$, (i.e it becomes $p := p - a_i$). Vasya can buy a good with the index $i$, and also choose exactly $k-1$ goods, the price of which does not exceed $a_i$, if he currently has enough coins (i.e $p \\ge a_i$). Thus, he buys all these $k$ goods, and his number of coins decreases by $a_i$ (i.e it becomes $p := p - a_i$). \n\nPlease note that each good can be bought no more than once.\n\nFor example, if the store now has $n=5$ goods worth $a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$, respectively, $k=2$, and Vasya has $6$ coins, then he can buy $3$ goods. A good with the index $1$ will be bought by Vasya without using the offer and he will pay $2$ coins. Goods with the indices $2$ and $3$ Vasya will buy using the offer and he will pay $4$ coins. It can be proved that Vasya can not buy more goods with six coins.\n\nHelp Vasya to find out the maximum number of goods he can buy.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10^4$)\u00a0\u2014 the number of test cases in the test.\n\nThe next lines contain a description of $t$ test cases. \n\nThe first line of each test case contains three integers $n, p, k$ ($2 \\le n \\le 2 \\cdot 10^5$, $1 \\le p \\le 2\\cdot10^9$, $2 \\le k \\le n$)\u00a0\u2014 the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.\n\nThe second line of each test case contains $n$ integers $a_i$ ($1 \\le a_i \\le 10^4$)\u00a0\u2014 the prices of goods.\n\nIt is guaranteed that the sum of $n$ for all test cases does not exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case in a separate line print one integer $m$\u00a0\u2014 the maximum number of goods that Vasya can buy.\n\n\n-----Example-----\nInput\n8\n5 6 2\n2 4 3 5 7\n5 11 2\n2 4 3 5 7\n3 2 3\n4 2 6\n5 2 3\n10 1 3 9 2\n2 10000 2\n10000 10000\n2 9999 2\n10000 10000\n4 6 4\n3 2 3 2\n5 5 3\n1 2 2 1 2\n\nOutput\n3\n4\n1\n1\n2\n0\n4\n5"} +{"id": "02611", "text": "There are $n$ models in the shop numbered from $1$ to $n$, with sizes $s_1, s_2, \\ldots, s_n$.\n\nOrac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes).\n\nOrac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indices $i_j$ and $i_{j+1}$ (note that $i_j < i_{j+1}$, because Orac arranged them properly), $i_{j+1}$ is divisible by $i_j$ and $s_{i_j} < s_{i_{j+1}}$.\n\nFor example, for $6$ models with sizes $\\{3, 6, 7, 7, 7, 7\\}$, he can buy models with indices $1$, $2$, and $6$, and the obtained arrangement will be beautiful. Also, note that the arrangement with exactly one model is also considered beautiful.\n\nOrac wants to know the maximum number of models that he can buy, and he may ask you these queries many times.\n\n\n-----Input-----\n\nThe first line contains one integer $t\\ (1 \\le t\\le 100)$: the number of queries.\n\nEach query contains two lines. The first line contains one integer $n\\ (1\\le n\\le 100\\,000)$: the number of models in the shop, and the second line contains $n$ integers $s_1,\\dots,s_n\\ (1\\le s_i\\le 10^9)$: the sizes of models.\n\nIt is guaranteed that the total sum of $n$ is at most $100\\,000$.\n\n\n-----Output-----\n\nPrint $t$ lines, the $i$-th of them should contain the maximum number of models that Orac can buy for the $i$-th query.\n\n\n-----Example-----\nInput\n4\n4\n5 3 4 6\n7\n1 4 2 3 6 4 9\n5\n5 4 3 2 1\n1\n9\n\nOutput\n2\n3\n1\n1\n\n\n\n-----Note-----\n\nIn the first query, for example, Orac can buy models with indices $2$ and $4$, the arrangement will be beautiful because $4$ is divisible by $2$ and $6$ is more than $3$. By enumerating, we can easily find that there are no beautiful arrangements with more than two models. \n\nIn the second query, Orac can buy models with indices $1$, $3$, and $6$. By enumerating, we can easily find that there are no beautiful arrangements with more than three models. \n\nIn the third query, there are no beautiful arrangements with more than one model."} +{"id": "02612", "text": "You are given an array $a_1, a_2, \\dots, a_n$, consisting of $n$ positive integers. \n\nInitially you are standing at index $1$ and have a score equal to $a_1$. You can perform two kinds of moves: move right\u00a0\u2014 go from your current index $x$ to $x+1$ and add $a_{x+1}$ to your score. This move can only be performed if $x1$. Also, you can't perform two or more moves to the left in a row. \n\nYou want to perform exactly $k$ moves. Also, there should be no more than $z$ moves to the left among them.\n\nWhat is the maximum score you can achieve?\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$)\u00a0\u2014 the number of testcases.\n\nThe first line of each testcase contains three integers $n, k$ and $z$ ($2 \\le n \\le 10^5$, $1 \\le k \\le n - 1$, $0 \\le z \\le min(5, k)$)\u00a0\u2014 the number of elements in the array, the total number of moves you should perform and the maximum number of moves to the left you can perform.\n\nThe second line of each testcase contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^4$)\u00a0\u2014 the given array.\n\nThe sum of $n$ over all testcases does not exceed $3 \\cdot 10^5$.\n\n\n-----Output-----\n\nPrint $t$ integers\u00a0\u2014 for each testcase output the maximum score you can achieve if you make exactly $k$ moves in total, no more than $z$ of them are to the left and there are no two or more moves to the left in a row.\n\n\n-----Example-----\nInput\n4\n5 4 0\n1 5 4 3 2\n5 4 1\n1 5 4 3 2\n5 4 4\n10 20 30 40 50\n10 7 3\n4 6 8 2 9 9 7 4 10 9\n\nOutput\n15\n19\n150\n56\n\n\n\n-----Note-----\n\nIn the first testcase you are not allowed to move left at all. So you make four moves to the right and obtain the score $a_1 + a_2 + a_3 + a_4 + a_5$.\n\nIn the second example you can move one time to the left. So we can follow these moves: right, right, left, right. The score will be $a_1 + a_2 + a_3 + a_2 + a_3$.\n\nIn the third example you can move four times to the left but it's not optimal anyway, you can just move four times to the right and obtain the score $a_1 + a_2 + a_3 + a_4 + a_5$."} +{"id": "02613", "text": "Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling.\n\nPinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them.\n\nPinkie Pie can eat the patty-cakes in any order. She is impatient about eating all the patty-cakes up so she asks you to help her to count the greatest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating!\n\nPinkie Pie is going to buy more bags of patty-cakes so she asks you to solve this problem for several bags!\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\le T \\le 100$): the number of bags for which you need to solve the problem.\n\nThe first line of each bag description contains a single integer $n$ ($2 \\le n \\le 10^5$): the number of patty-cakes in it. The second line of the bag description contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le n$): the information of patty-cakes' fillings: same fillings are defined as same integers, different fillings are defined as different integers. It is guaranteed that each bag contains at least two patty-cakes with the same filling. \n\nIt is guaranteed that the sum of $n$ over all bags does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each bag print in separate line one single integer: the largest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating for that bag.\n\n\n-----Example-----\nInput\n4\n7\n1 7 1 6 4 4 6\n8\n1 1 4 6 4 6 4 7\n3\n3 3 3\n6\n2 5 2 3 1 4\n\nOutput\n3\n2\n0\n4\n\n\n\n-----Note-----\n\nFor the first bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): $1$, $6$, $4$, $7$, $1$, $6$, $4$ (in this way, the minimum distance is equal to $3$).\n\nFor the second bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): $1$, $4$, $6$, $7$, $4$, $1$, $6$, $4$ (in this way, the minimum distance is equal to $2$)."} +{"id": "02614", "text": "Consider some positive integer $x$. Its prime factorization will be of form $x = 2^{k_1} \\cdot 3^{k_2} \\cdot 5^{k_3} \\cdot \\dots$\n\nLet's call $x$ elegant if the greatest common divisor of the sequence $k_1, k_2, \\dots$ is equal to $1$. For example, numbers $5 = 5^1$, $12 = 2^2 \\cdot 3$, $72 = 2^3 \\cdot 3^2$ are elegant and numbers $8 = 2^3$ ($GCD = 3$), $2500 = 2^2 \\cdot 5^4$ ($GCD = 2$) are not.\n\nCount the number of elegant integers from $2$ to $n$.\n\nEach testcase contains several values of $n$, for each of them you are required to solve the problem separately.\n\n\n-----Input-----\n\nThe first line contains a single integer $T$ ($1 \\le T \\le 10^5$) \u2014 the number of values of $n$ in the testcase.\n\nEach of the next $T$ lines contains a single integer $n_i$ ($2 \\le n_i \\le 10^{18}$).\n\n\n-----Output-----\n\nPrint $T$ lines \u2014 the $i$-th line should contain the number of elegant numbers from $2$ to $n_i$.\n\n\n-----Example-----\nInput\n4\n4\n2\n72\n10\n\nOutput\n2\n1\n61\n6\n\n\n\n-----Note-----\n\nHere is the list of non-elegant numbers up to $10$:\n\n $4 = 2^2, GCD = 2$; $8 = 2^3, GCD = 3$; $9 = 3^2, GCD = 2$. \n\nThe rest have $GCD = 1$."} +{"id": "02615", "text": "There are $n$ piles of stones, where the $i$-th pile has $a_i$ stones. Two people play a game, where they take alternating turns removing stones.\n\nIn a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player who cannot make a move (because all piles are empty) loses the game. If both players play optimally, determine the winner of the game.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1\\le t\\le 1000$) \u00a0\u2014 the number of test cases. Next $2t$ lines contain descriptions of test cases.\n\nThe first line of each test case contains a single integer $n$ ($1\\le n\\le 10^5$) \u00a0\u2014 the number of piles.\n\nThe second line of each test case contains $n$ integers $a_1,\\ldots,a_n$ ($1\\le a_i\\le 10^9$) \u00a0\u2014 $a_i$ is equal to the number of stones in the $i$-th pile.\n\nIt is guaranteed that the sum of $n$ for all test cases does not exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case, if the player who makes the first move will win, output \"First\". Otherwise, output \"Second\".\n\n\n-----Example-----\nInput\n7\n3\n2 5 4\n8\n1 1 1 1 1 1 1 1\n6\n1 2 3 4 5 6\n6\n1 1 2 1 2 2\n1\n1000000000\n5\n1 2 2 1 1\n3\n1 1 1\n\nOutput\nFirst\nSecond\nSecond\nFirst\nFirst\nSecond\nFirst\n\n\n\n-----Note-----\n\nIn the first test case, the first player will win the game. His winning strategy is: The first player should take the stones from the first pile. He will take $1$ stone. The numbers of stones in piles will be $[1, 5, 4]$. The second player should take the stones from the first pile. He will take $1$ stone because he can't take any other number of stones. The numbers of stones in piles will be $[0, 5, 4]$. The first player should take the stones from the second pile because the first pile is empty. He will take $4$ stones. The numbers of stones in piles will be $[0, 1, 4]$. The second player should take the stones from the second pile because the first pile is empty. He will take $1$ stone because he can't take any other number of stones. The numbers of stones in piles will be $[0, 0, 4]$. The first player should take the stones from the third pile because the first and second piles are empty. He will take $4$ stones. The numbers of stones in piles will be $[0, 0, 0]$. The second player will lose the game because all piles will be empty."} +{"id": "02616", "text": "Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.\n\nInitially, on day $1$, there is one bacterium with mass $1$.\n\nEvery day, some number of bacteria will split (possibly zero or all). When a bacterium of mass $m$ splits, it becomes two bacteria of mass $\\frac{m}{2}$ each. For example, a bacterium of mass $3$ can split into two bacteria of mass $1.5$.\n\nAlso, every night, the mass of every bacteria will increase by one.\n\nPhoenix is wondering if it is possible for the total mass of all the bacteria to be exactly $n$. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!\n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains an integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases.\n\nThe first line of each test case contains an integer $n$ ($2 \\le n \\le 10^9$)\u00a0\u2014 the sum of bacteria masses that Phoenix is interested in. \n\n\n-----Output-----\n\nFor each test case, if there is no way for the bacteria to exactly achieve total mass $n$, print -1. Otherwise, print two lines.\n\nThe first line should contain an integer $d$ \u00a0\u2014 the minimum number of nights needed.\n\nThe next line should contain $d$ integers, with the $i$-th integer representing the number of bacteria that should split on the $i$-th day.\n\nIf there are multiple solutions, print any.\n\n\n-----Example-----\nInput\n3\n9\n11\n2\n\nOutput\n3\n1 0 2 \n3\n1 1 2\n1\n0 \n\n\n-----Note-----\n\nIn the first test case, the following process results in bacteria with total mass $9$: Day $1$: The bacterium with mass $1$ splits. There are now two bacteria with mass $0.5$ each. Night $1$: All bacteria's mass increases by one. There are now two bacteria with mass $1.5$. Day $2$: None split. Night $2$: There are now two bacteria with mass $2.5$. Day $3$: Both bacteria split. There are now four bacteria with mass $1.25$. Night $3$: There are now four bacteria with mass $2.25$. The total mass is $2.25+2.25+2.25+2.25=9$. It can be proved that $3$ is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.\n\n$ $\n\nIn the second test case, the following process results in bacteria with total mass $11$: Day $1$: The bacterium with mass $1$ splits. There are now two bacteria with mass $0.5$. Night $1$: There are now two bacteria with mass $1.5$. Day $2$: One bacterium splits. There are now three bacteria with masses $0.75$, $0.75$, and $1.5$. Night $2$: There are now three bacteria with masses $1.75$, $1.75$, and $2.5$. Day $3$: The bacteria with mass $1.75$ and the bacteria with mass $2.5$ split. There are now five bacteria with masses $0.875$, $0.875$, $1.25$, $1.25$, and $1.75$. Night $3$: There are now five bacteria with masses $1.875$, $1.875$, $2.25$, $2.25$, and $2.75$. The total mass is $1.875+1.875+2.25+2.25+2.75=11$. It can be proved that $3$ is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.\n\n$ $\n\nIn the third test case, the bacterium does not split on day $1$, and then grows to mass $2$ during night $1$."} +{"id": "02617", "text": "You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!\n\nYou have $n$ tickets to sell. The price of the $i$-th ticket is $p_i$. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: The $x\\%$ of the price of each the $a$-th sold ticket ($a$-th, $2a$-th, $3a$-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. The $y\\%$ of the price of each the $b$-th sold ticket ($b$-th, $2b$-th, $3b$-th and so on) in the order you chose is aimed for pollution abatement. \n\nIf the ticket is in both programs then the $(x + y) \\%$ are used for environmental activities. Also, it's known that all prices are multiples of $100$, so there is no need in any rounding.\n\nFor example, if you'd like to sell tickets with prices $[400, 100, 300, 200]$ and the cinema pays $10\\%$ of each $2$-nd sold ticket and $20\\%$ of each $3$-rd sold ticket, then arranging them in order $[100, 200, 300, 400]$ will lead to contribution equal to $100 \\cdot 0 + 200 \\cdot 0.1 + 300 \\cdot 0.2 + 400 \\cdot 0.1 = 120$. But arranging them in order $[100, 300, 400, 200]$ will lead to $100 \\cdot 0 + 300 \\cdot 0.1 + 400 \\cdot 0.2 + 200 \\cdot 0.1 = 130$.\n\nNature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least $k$ in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least $k$.\n\n\n-----Input-----\n\nThe first line contains a single integer $q$ ($1 \\le q \\le 100$)\u00a0\u2014 the number of independent queries. Each query consists of $5$ lines.\n\nThe first line of each query contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$)\u00a0\u2014 the number of tickets.\n\nThe second line contains $n$ integers $p_1, p_2, \\dots, p_n$ ($100 \\le p_i \\le 10^9$, $p_i \\bmod 100 = 0$)\u00a0\u2014 the corresponding prices of tickets.\n\nThe third line contains two integers $x$ and $a$ ($1 \\le x \\le 100$, $x + y \\le 100$, $1 \\le a \\le n$)\u00a0\u2014 the parameters of the first program.\n\nThe fourth line contains two integers $y$ and $b$ ($1 \\le y \\le 100$, $x + y \\le 100$, $1 \\le b \\le n$)\u00a0\u2014 the parameters of the second program.\n\nThe fifth line contains single integer $k$ ($1 \\le k \\le 10^{14}$)\u00a0\u2014 the required total contribution.\n\nIt's guaranteed that the total number of tickets per test doesn't exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nPrint $q$ integers\u00a0\u2014 one per query. \n\nFor each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least $k$ if you can sell tickets in any order.\n\nIf the total contribution can not be achieved selling all the tickets, print $-1$.\n\n\n-----Example-----\nInput\n4\n1\n100\n50 1\n49 1\n100\n8\n100 200 100 200 100 200 100 100\n10 2\n15 3\n107\n3\n1000000000 1000000000 1000000000\n50 1\n50 1\n3000000000\n5\n200 100 100 100 100\n69 5\n31 2\n90\n\nOutput\n-1\n6\n3\n4\n\n\n\n-----Note-----\n\nIn the first query the total contribution is equal to $50 + 49 = 99 < 100$, so it's impossible to gather enough money.\n\nIn the second query you can rearrange tickets in a following way: $[100, 100, 200, 200, 100, 200, 100, 100]$ and the total contribution from the first $6$ tickets is equal to $100 \\cdot 0 + 100 \\cdot 0.1 + 200 \\cdot 0.15 + 200 \\cdot 0.1 + 100 \\cdot 0 + 200 \\cdot 0.25 = 10 + 30 + 20 + 50 = 110$.\n\nIn the third query the full price of each ticket goes to the environmental activities.\n\nIn the fourth query you can rearrange tickets as $[100, 200, 100, 100, 100]$ and the total contribution from the first $4$ tickets is $100 \\cdot 0 + 200 \\cdot 0.31 + 100 \\cdot 0 + 100 \\cdot 0.31 = 62 + 31 = 93$."} +{"id": "02618", "text": "The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (x_{i}, y_{i}), a maximum brightness c, equal for all stars, and an initial brightness s_{i} (0 \u2264 s_{i} \u2264 c).\n\nOver time the stars twinkle. At moment 0 the i-th star has brightness s_{i}. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 \u2264 c, and 0, otherwise.\n\nYou want to look at the sky q times. In the i-th time you will look at the moment t_{i} and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x_1i, y_1i) and the upper right\u00a0\u2014 (x_2i, y_2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.\n\nA star lies in a rectangle if it lies on its border or lies strictly inside it.\n\n\n-----Input-----\n\nThe first line contains three integers n, q, c (1 \u2264 n, q \u2264 10^5, 1 \u2264 c \u2264 10)\u00a0\u2014 the number of the stars, the number of the views and the maximum brightness of the stars.\n\nThe next n lines contain the stars description. The i-th from these lines contains three integers x_{i}, y_{i}, s_{i} (1 \u2264 x_{i}, y_{i} \u2264 100, 0 \u2264 s_{i} \u2264 c \u2264 10)\u00a0\u2014 the coordinates of i-th star and its initial brightness.\n\nThe next q lines contain the views description. The i-th from these lines contains five integers t_{i}, x_1i, y_1i, x_2i, y_2i (0 \u2264 t_{i} \u2264 10^9, 1 \u2264 x_1i < x_2i \u2264 100, 1 \u2264 y_1i < y_2i \u2264 100)\u00a0\u2014 the moment of the i-th view and the coordinates of the viewed rectangle.\n\n\n-----Output-----\n\nFor each view print the total brightness of the viewed stars.\n\n\n-----Examples-----\nInput\n2 3 3\n1 1 1\n3 2 0\n2 1 1 2 2\n0 2 1 4 5\n5 1 1 5 5\n\nOutput\n3\n0\n3\n\nInput\n3 4 5\n1 1 2\n2 3 0\n3 3 1\n0 1 1 100 100\n1 2 2 4 4\n2 2 1 4 7\n1 50 50 51 51\n\nOutput\n3\n3\n5\n0\n\n\n\n-----Note-----\n\nLet's consider the first example.\n\nAt the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.\n\nAt the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.\n\nAt the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3."} +{"id": "02619", "text": "A permutation is a sequence of integers from $1$ to $n$ of length $n$ containing each number exactly once. For example, $[1]$, $[4, 3, 5, 1, 2]$, $[3, 2, 1]$\u00a0\u2014 are permutations, and $[1, 1]$, $[4, 3, 1]$, $[2, 3, 4]$\u00a0\u2014 no.\n\nPermutation $a$ is lexicographically smaller than permutation $b$ (they have the same length $n$), if in the first index $i$ in which they differ, $a[i] < b[i]$. For example, the permutation $[1, 3, 2, 4]$ is lexicographically smaller than the permutation $[1, 3, 4, 2]$, because the first two elements are equal, and the third element in the first permutation is smaller than in the second.\n\nThe next permutation for a permutation $a$ of length $n$\u00a0\u2014 is the lexicographically smallest permutation $b$ of length $n$ that lexicographically larger than $a$. For example: for permutation $[2, 1, 4, 3]$ the next permutation is $[2, 3, 1, 4]$; for permutation $[1, 2, 3]$ the next permutation is $[1, 3, 2]$; for permutation $[2, 1]$ next permutation does not exist. \n\nYou are given the number $n$\u00a0\u2014 the length of the initial permutation. The initial permutation has the form $a = [1, 2, \\ldots, n]$. In other words, $a[i] = i$ ($1 \\le i \\le n$).\n\nYou need to process $q$ queries of two types: $1$ $l$ $r$: query for the sum of all elements on the segment $[l, r]$. More formally, you need to find $a[l] + a[l + 1] + \\ldots + a[r]$. $2$ $x$: $x$ times replace the current permutation with the next permutation. For example, if $x=2$ and the current permutation has the form $[1, 3, 4, 2]$, then we should perform such a chain of replacements $[1, 3, 4, 2] \\rightarrow [1, 4, 2, 3] \\rightarrow [1, 4, 3, 2]$. \n\nFor each query of the $1$-st type output the required sum.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ ($2 \\le n \\le 2 \\cdot 10^5$) and $q$ ($1 \\le q \\le 2 \\cdot 10^5$), where $n$\u00a0\u2014 the length of the initial permutation, and $q$\u00a0\u2014 the number of queries.\n\nThe next $q$ lines contain a single query of the $1$-st or $2$-nd type. The $1$-st type query consists of three integers $1$, $l$ and $r$ $(1 \\le l \\le r \\le n)$, the $2$-nd type query consists of two integers $2$ and $x$ $(1 \\le x \\le 10^5)$.\n\nIt is guaranteed that all requests of the $2$-nd type are possible to process.\n\n\n-----Output-----\n\nFor each query of the $1$-st type, output on a separate line one integer\u00a0\u2014 the required sum.\n\n\n-----Example-----\nInput\n4 4\n1 2 4\n2 3\n1 1 2\n1 3 4\n\nOutput\n9\n4\n6\n\n\n\n-----Note-----\n\nInitially, the permutation has the form $[1, 2, 3, 4]$. Queries processing is as follows: $2 + 3 + 4 = 9$; $[1, 2, 3, 4] \\rightarrow [1, 2, 4, 3] \\rightarrow [1, 3, 2, 4] \\rightarrow [1, 3, 4, 2]$; $1 + 3 = 4$; $4 + 2 = 6$"} +{"id": "02620", "text": "Gildong is playing a video game called Block Adventure. In Block Adventure, there are $n$ columns of blocks in a row, and the columns are numbered from $1$ to $n$. All blocks have equal heights. The height of the $i$-th column is represented as $h_i$, which is the number of blocks stacked in the $i$-th column.\n\nGildong plays the game as a character that can stand only on the top of the columns. At the beginning, the character is standing on the top of the $1$-st column. The goal of the game is to move the character to the top of the $n$-th column.\n\nThe character also has a bag that can hold infinitely many blocks. When the character is on the top of the $i$-th column, Gildong can take one of the following three actions as many times as he wants: if there is at least one block on the column, remove one block from the top of the $i$-th column and put it in the bag; if there is at least one block in the bag, take one block out of the bag and place it on the top of the $i$-th column; if $i < n$ and $|h_i - h_{i+1}| \\le k$, move the character to the top of the $i+1$-st column. $k$ is a non-negative integer given at the beginning of the game. Note that it is only possible to move to the next column. \n\nIn actions of the first two types the character remains in the $i$-th column, and the value $h_i$ changes.\n\nThe character initially has $m$ blocks in the bag. Gildong wants to know if it is possible to win the game. Help Gildong find the answer to his question.\n\n\n-----Input-----\n\nEach test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 1000$). Description of the test cases follows.\n\nThe first line of each test case contains three integers $n$, $m$, and $k$ ($1 \\le n \\le 100$, $0 \\le m \\le 10^6$, $0 \\le k \\le 10^6$) \u2014 the number of columns in the game, the number of blocks in the character's bag at the beginning, and the non-negative integer $k$ described in the statement.\n\nThe second line of each test case contains $n$ integers. The $i$-th integer is $h_i$ ($0 \\le h_i \\le 10^6$), the initial height of the $i$-th column.\n\n\n-----Output-----\n\nFor each test case, print \"YES\" if it is possible to win the game. Otherwise, print \"NO\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Example-----\nInput\n5\n3 0 1\n4 3 5\n3 1 2\n1 4 7\n4 10 0\n10 20 10 20\n2 5 5\n0 11\n1 9 9\n99\n\nOutput\nYES\nNO\nYES\nNO\nYES\n\n\n\n-----Note-----\n\nIn the first case, Gildong can take one block from the $1$-st column, move to the $2$-nd column, put the block on the $2$-nd column, then move to the $3$-rd column.\n\nIn the second case, Gildong has to put the block in his bag on the $1$-st column to get to the $2$-nd column. But it is impossible to get to the $3$-rd column because $|h_2 - h_3| = 3 > k$ and there is no way to decrease the gap.\n\nIn the fifth case, the character is already on the $n$-th column from the start so the game is won instantly."} +{"id": "02621", "text": "The stardate is 1983, and Princess Heidi is getting better at detecting the Death Stars. This time, two Rebel spies have yet again given Heidi two maps with the possible locations of the Death Star. Since she got rid of all double agents last time, she knows that both maps are correct, and indeed show the map of the solar system that contains the Death Star. However, this time the Empire has hidden the Death Star very well, and Heidi needs to find a place that appears on both maps in order to detect the Death Star.\n\nThe first map is an N \u00d7 M grid, each cell of which shows some type of cosmic object that is present in the corresponding quadrant of space. The second map is an M \u00d7 N grid. Heidi needs to align those two maps in such a way that they overlap over some M \u00d7 M section in which all cosmic objects are identical. Help Heidi by identifying where such an M \u00d7 M section lies within both maps.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers N and M (1 \u2264 N \u2264 2000, 1 \u2264 M \u2264 200, M \u2264 N). The next N lines each contain M lower-case Latin characters (a-z), denoting the first map. Different characters correspond to different cosmic object types. The next M lines each contain N characters, describing the second map in the same format. \n\n\n-----Output-----\n\nThe only line of the output should contain two space-separated integers i and j, denoting that the section of size M \u00d7 M in the first map that starts at the i-th row is equal to the section of the second map that starts at the j-th column. Rows and columns are numbered starting from 1.\n\nIf there are several possible ways to align the maps, Heidi will be satisfied with any of those. It is guaranteed that a solution exists.\n\n\n-----Example-----\nInput\n10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nnoise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo\n\nOutput\n4 6\n\n\n\n-----Note-----\n\nThe 5-by-5 grid for the first test case looks like this: \n\nmayth\n\neforc\n\nebewi\n\nthyou\n\nhctwo"} +{"id": "02622", "text": "Phoenix has a string $s$ consisting of lowercase Latin letters. He wants to distribute all the letters of his string into $k$ non-empty strings $a_1, a_2, \\dots, a_k$ such that every letter of $s$ goes to exactly one of the strings $a_i$. The strings $a_i$ do not need to be substrings of $s$. Phoenix can distribute letters of $s$ and rearrange the letters within each string $a_i$ however he wants.\n\nFor example, if $s = $ baba and $k=2$, Phoenix may distribute the letters of his string in many ways, such as: ba and ba a and abb ab and ab aa and bb \n\nBut these ways are invalid: baa and ba b and ba baba and empty string ($a_i$ should be non-empty) \n\nPhoenix wants to distribute the letters of his string $s$ into $k$ strings $a_1, a_2, \\dots, a_k$ to minimize the lexicographically maximum string among them, i.\u00a0e. minimize $max(a_1, a_2, \\dots, a_k)$. Help him find the optimal distribution and print the minimal possible value of $max(a_1, a_2, \\dots, a_k)$.\n\nString $x$ is lexicographically less than string $y$ if either $x$ is a prefix of $y$ and $x \\ne y$, or there exists an index $i$ ($1 \\le i \\le min(|x|, |y|))$ such that $x_i$ < $y_i$ and for every $j$ $(1 \\le j < i)$ $x_j = y_j$. Here $|x|$ denotes the length of the string $x$.\n\n\n-----Input-----\n\nThe input consists of multiple test cases. The first line contains an integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases. Each test case consists of two lines.\n\nThe first line of each test case consists of two integers $n$ and $k$ ($1 \\le k \\le n \\le 10^5$)\u00a0\u2014 the length of string $s$ and the number of non-empty strings, into which Phoenix wants to distribute letters of $s$, respectively.\n\nThe second line of each test case contains a string $s$ of length $n$ consisting only of lowercase Latin letters.\n\nIt is guaranteed that the sum of $n$ over all test cases is $\\le 10^5$.\n\n\n-----Output-----\n\nPrint $t$ answers\u00a0\u2014 one per test case. The $i$-th answer should be the minimal possible value of $max(a_1, a_2, \\dots, a_k)$ in the $i$-th test case.\n\n\n-----Example-----\nInput\n6\n4 2\nbaba\n5 2\nbaacb\n5 3\nbaacb\n5 3\naaaaa\n6 4\naaxxzz\n7 1\nphoenix\n\nOutput\nab\nabbc\nb\naa\nx\nehinopx\n\n\n\n-----Note-----\n\nIn the first test case, one optimal solution is to distribute baba into ab and ab. \n\nIn the second test case, one optimal solution is to distribute baacb into abbc and a.\n\nIn the third test case, one optimal solution is to distribute baacb into ac, ab, and b.\n\nIn the fourth test case, one optimal solution is to distribute aaaaa into aa, aa, and a.\n\nIn the fifth test case, one optimal solution is to distribute aaxxzz into az, az, x, and x.\n\nIn the sixth test case, one optimal solution is to distribute phoenix into ehinopx."} +{"id": "02623", "text": "You are given an array of $n$ integers $a_1, a_2, \\ldots, a_n$.\n\nYou will perform $q$ operations. In the $i$-th operation, you have a symbol $s_i$ which is either \"<\" or \">\" and a number $x_i$.\n\nYou make a new array $b$ such that $b_j = -a_j$ if $a_j s_i x_i$ and $b_j = a_j$ otherwise (i.e. if $s_i$ is '>', then all $a_j > x_i$ will be flipped). After doing all these replacements, $a$ is set to be $b$.\n\nYou want to know what your final array looks like after all operations.\n\n\n-----Input-----\n\nThe first line contains two integers $n,q$ ($1 \\leq n,q \\leq 10^5$)\u00a0\u2014 the number of integers and the number of queries.\n\nThe next line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($-10^5 \\leq a_i \\leq 10^5$)\u00a0\u2014 the numbers.\n\nEach of the next $q$ lines contains a character and an integer $s_i, x_i$. ($s_i \\in \\{<, >\\}, -10^5 \\leq x_i \\leq 10^5$)\u00a0\u2013 the queries.\n\n\n-----Output-----\n\nPrint $n$ integers $c_1, c_2, \\ldots, c_n$ representing the array after all operations.\n\n\n-----Examples-----\nInput\n11 3\n-5 -4 -3 -2 -1 0 1 2 3 4 5\n> 2\n> -4\n< 5\n\nOutput\n5 4 -3 -2 -1 0 1 2 -3 4 5\n\nInput\n5 5\n0 1 -2 -1 2\n< -2\n< -1\n< 0\n< 1\n< 2\n\nOutput\n0 -1 2 -1 2\n\n\n\n-----Note-----\n\nIn the first example, the array goes through the following changes: Initial: $[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]$ $> 2$: $[-5, -4, -3, -2, -1, 0, 1, 2, -3, -4, -5]$ $> -4$: $[-5, -4, 3, 2, 1, 0, -1, -2, 3, -4, -5]$ $< 5$: $[5, 4, -3, -2, -1, 0, 1, 2, -3, 4, 5]$"} +{"id": "02624", "text": "Today at the lesson of mathematics, Petya learns about the digital root.\n\nThe digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached. \n\nLet's denote the digital root of $x$ as $S(x)$. Then $S(5)=5$, $S(38)=S(3+8=11)=S(1+1=2)=2$, $S(10)=S(1+0=1)=1$.\n\nAs a homework Petya got $n$ tasks of the form: find $k$-th positive number whose digital root is $x$.\n\nPetya has already solved all the problems, but he doesn't know if it's right. Your task is to solve all $n$ tasks from Petya's homework.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^3$) \u2014 the number of tasks in Petya's homework. The next $n$ lines contain two integers $k_i$ ($1 \\le k_i \\le 10^{12}$) and $x_i$ ($1 \\le x_i \\le 9$) \u2014 $i$-th Petya's task in which you need to find a $k_i$-th positive number, the digital root of which is $x_i$.\n\n\n-----Output-----\n\nOutput $n$ lines, $i$-th line should contain a single integer \u2014 the answer to the $i$-th problem.\n\n\n-----Example-----\nInput\n3\n1 5\n5 2\n3 1\n\nOutput\n5\n38\n19"} +{"id": "02625", "text": "You have integer $n$. Calculate how many ways are there to fully cover belt-like area of $4n-2$ triangles with diamond shapes. \n\nDiamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. \n\n$2$ coverings are different if some $2$ triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one.\n\nPlease look at pictures below for better understanding.\n\n $\\theta$ On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill.\n\n[Image] These are the figures of the area you want to fill for $n = 1, 2, 3, 4$. \n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^{4}$)\u00a0\u2014 the number of test cases.\n\nEach of the next $t$ lines contains a single integer $n$ ($1 \\le n \\le 10^{9}$).\n\n\n-----Output-----\n\nFor each test case, print the number of ways to fully cover belt-like area of $4n-2$ triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed $10^{18}$.\n\n\n-----Example-----\nInput\n2\n2\n1\n\nOutput\n2\n1\n\n\n\n-----Note-----\n\nIn the first test case, there are the following $2$ ways to fill the area:\n\n [Image] \n\nIn the second test case, there is a unique way to fill the area:\n\n [Image]"} +{"id": "02626", "text": "Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.\n\nExample:\n\n\nInput:\n[\n [\"1\",\"0\",\"1\",\"0\",\"0\"],\n [\"1\",\"0\",\"1\",\"1\",\"1\"],\n [\"1\",\"1\",\"1\",\"1\",\"1\"],\n [\"1\",\"0\",\"0\",\"1\",\"0\"]\n]\nOutput: 6"} +{"id": "02627", "text": "The gray code is a binary numeral system where two successive values differ in only one bit.\n\nGiven a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.\n\nExample 1:\n\n\nInput:\u00a02\nOutput:\u00a0[0,1,3,2]\nExplanation:\n00 - 0\n01 - 1\n11 - 3\n10 - 2\n\nFor a given\u00a0n, a gray code sequence may not be uniquely defined.\nFor example, [0,2,3,1] is also a valid gray code sequence.\n\n00 - 0\n10 - 2\n11 - 3\n01 - 1\n\n\nExample 2:\n\n\nInput:\u00a00\nOutput:\u00a0[0]\nExplanation: We define the gray code sequence to begin with 0.\n\u00a0 A gray code sequence of n has size = 2n, which for n = 0 the size is 20 = 1.\n\u00a0 Therefore, for n = 0 the gray code sequence is [0]."} +{"id": "02628", "text": "Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.\n\nExample:\n\n\nInput: 3\nOutput:\n[\n [ 1, 2, 3 ],\n [ 8, 9, 4 ],\n [ 7, 6, 5 ]\n]"} +{"id": "02629", "text": "A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).\n\nThe robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).\n\nNow consider if some obstacles are added to the grids. How many unique paths would there be?\n\n\n\nAn obstacle and empty space is marked as 1 and 0 respectively in the grid.\n\nNote: m and n will be at most 100.\n\nExample 1:\n\n\nInput:\n[\n\u00a0 [0,0,0],\n\u00a0 [0,1,0],\n\u00a0 [0,0,0]\n]\nOutput: 2\nExplanation:\nThere is one obstacle in the middle of the 3x3 grid above.\nThere are two ways to reach the bottom-right corner:\n1. Right -> Right -> Down -> Down\n2. Down -> Down -> Right -> Right"} +{"id": "02630", "text": "There are a total of n courses you have to take, labeled from 0 to n-1.\n\nSome courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]\n\nGiven the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?\n\nExample 1:\n\n\nInput: 2, [[1,0]] \nOutput: true\nExplanation:\u00a0There are a total of 2 courses to take. \n\u00a0 To take course 1 you should have finished course 0. So it is possible.\n\nExample 2:\n\n\nInput: 2, [[1,0],[0,1]]\nOutput: false\nExplanation:\u00a0There are a total of 2 courses to take. \n\u00a0 To take course 1 you should have finished course 0, and to take course 0 you should\n\u00a0 also have finished course 1. So it is impossible.\n\n\nNote:\n\n\n The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.\n You may assume that there are no duplicate edges in the input prerequisites."} +{"id": "02631", "text": "Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.\n\nNote: You can only move either down or right at any point in time.\n\nExample:\n\n\nInput:\n[\n\u00a0 [1,3,1],\n [1,5,1],\n [4,2,1]\n]\nOutput: 7\nExplanation: Because the path 1\u21923\u21921\u21921\u21921 minimizes the sum."} +{"id": "02632", "text": "table.dungeon, .dungeon th, .dungeon td {\n border:3px solid black;\n}\n\n .dungeon th, .dungeon td {\n text-align: center;\n height: 70px;\n width: 70px;\n}\n\nThe demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.\n\nThe knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.\n\nSome of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).\n\nIn order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.\n\n\u00a0\n\nWrite a function to determine the knight's minimum initial health so that he is able to rescue the princess.\n\nFor example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.\n\n\n \n \n -2 (K)\n -3\n 3\n \n \n -5\n -10\n 1\n \n \n 10\n 30\n -5 (P)\n \n \n\n\n\u00a0\n\nNote:\n\n\n The knight's health has no upper bound.\n Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned."} +{"id": "02633", "text": "Given a set of distinct integers, nums, return all possible subsets (the power set).\n\nNote: The solution set must not contain duplicate subsets.\n\nExample:\n\n\nInput: nums = [1,2,3]\nOutput:\n[\n [3],\n\u00a0 [1],\n\u00a0 [2],\n\u00a0 [1,2,3],\n\u00a0 [1,3],\n\u00a0 [2,3],\n\u00a0 [1,2],\n\u00a0 []\n]"} +{"id": "02634", "text": "Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.\n\nExample 1:\n\n\nInput:\n[\n [ 1, 2, 3 ],\n [ 4, 5, 6 ],\n [ 7, 8, 9 ]\n]\nOutput: [1,2,3,6,9,8,7,4,5]\n\n\nExample 2:\n\nInput:\n[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9,10,11,12]\n]\nOutput: [1,2,3,4,8,12,11,10,9,5,6,7]"} +{"id": "02635", "text": "A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).\n \n\nThe geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 \u2264 Li, Ri \u2264 INT_MAX, 0 < Hi \u2264 INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.\n\nFor instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .\n\nThe output is a list of \"key points\" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.\n\nFor instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].\n\nNotes:\n\n\n The number of buildings in any input list is guaranteed to be in the range [0, 10000].\n The input list is already sorted in ascending order by the left x position Li.\n The output list must be sorted by the x position.\n There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]"} +{"id": "02636", "text": "Given a collection of numbers that might contain duplicates, return all possible unique permutations.\n\nExample:\n\n\nInput: [1,1,2]\nOutput:\n[\n [1,1,2],\n [1,2,1],\n [2,1,1]\n]"} +{"id": "02637", "text": "Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.\n\nFor example, given the following triangle\n\n\n[\n [2],\n [3,4],\n [6,5,7],\n [4,1,8,3]\n]\n\n\nThe minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).\n\nNote:\n\nBonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle."} +{"id": "02638", "text": "Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).\n\nNote: The solution set must not contain duplicate subsets.\n\nExample:\n\n\nInput: [1,2,2]\nOutput:\n[\n [2],\n [1],\n [1,2,2],\n [2,2],\n [1,2],\n []\n]"} +{"id": "02639", "text": "There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.\nSnuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.\nThe lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.\nIn each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.\nSnuke wants to maximize the number of squares lighted by the lamp.\nYou are given H strings S_i (1 \\leq i \\leq H), each of length W. If the j-th character (1 \\leq j \\leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.\nFind the maximum possible number of squares lighted by the lamp.\n\n-----Constraints-----\n - 1 \\leq H \\leq 2,000\n - 1 \\leq W \\leq 2,000\n - S_i is a string of length W consisting of # and ..\n - . occurs at least once in one of the strings S_i (1 \\leq i \\leq H).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\nS_1\n:\nS_H\n\n-----Output-----\nPrint the maximum possible number of squares lighted by the lamp.\n\n-----Sample Input-----\n4 6\n#..#..\n.....#\n....#.\n#.#...\n\n-----Sample Output-----\n8\n\nIf Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares."} +{"id": "02640", "text": "We have N colored balls arranged in a row from left to right; the color of the i-th ball from the left is c_i.\nYou are given Q queries. The i-th query is as follows: how many different colors do the l_i-th through r_i-th balls from the left have?\n\n-----Constraints-----\n - 1\\leq N,Q \\leq 5 \\times 10^5\n - 1\\leq c_i \\leq N\n - 1\\leq l_i \\leq r_i \\leq N\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN Q\nc_1 c_2 \\cdots c_N\nl_1 r_1\nl_2 r_2\n:\nl_Q r_Q\n\n-----Output-----\nPrint Q lines. The i-th line should contain the response to the i-th query.\n\n-----Sample Input-----\n4 3\n1 2 1 3\n1 3\n2 4\n3 3\n\n-----Sample Output-----\n2\n3\n1\n\n - The 1-st, 2-nd, and 3-rd balls from the left have the colors 1, 2, and 1 - two different colors.\n - The 2-st, 3-rd, and 4-th balls from the left have the colors 2, 1, and 3 - three different colors.\n - The 3-rd ball from the left has the color 1 - just one color."} +{"id": "02641", "text": "We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively.\nWe will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.\nThe i-th and j-th sardines (i \\neq j) are on bad terms if and only if A_i \\cdot A_j + B_i \\cdot B_j = 0.\nIn how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 2 \\times 10^5\n - -10^{18} \\leq A_i, B_i \\leq 10^{18}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 B_1\n:\nA_N B_N\n\n-----Output-----\nPrint the count modulo 1000000007.\n\n-----Sample Input-----\n3\n1 2\n-1 1\n2 -1\n\n-----Sample Output-----\n5\n\nThere are five ways to choose the set of sardines, as follows:\n - The 1-st\n - The 1-st and 2-nd\n - The 2-nd\n - The 2-nd and 3-rd\n - The 3-rd"} +{"id": "02642", "text": "We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}.\nProcess the following q queries in order:\n - The i-th query contains three integers n_i, x_i, and m_i.\nLet a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \\begin{eqnarray} a_j = \\begin{cases} x_i & ( j = 0 ) \\\\ a_{j - 1} + d_{(j - 1)~\\textrm{mod}~k} & ( 0 < j \\leq n_i - 1 ) \\end{cases}\\end{eqnarray}\nPrint the number of j~(0 \\leq j < n_i - 1) such that (a_j~\\textrm{mod}~m_i) < (a_{j + 1}~\\textrm{mod}~m_i).\nHere (y~\\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0).\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq k, q \\leq 5000\n - 0 \\leq d_i \\leq 10^9\n - 2 \\leq n_i \\leq 10^9\n - 0 \\leq x_i \\leq 10^9\n - 2 \\leq m_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nk q\nd_0 d_1 ... d_{k - 1}\nn_1 x_1 m_1\nn_2 x_2 m_2\n:\nn_q x_q m_q\n\n-----Output-----\nPrint q lines.\nThe i-th line should contain the response to the i-th query.\n\n-----Sample Input-----\n3 1\n3 1 4\n5 3 2\n\n-----Sample Output-----\n1\n\nFor the first query, the sequence {a_j} will be 3,6,7,11,14.\n - (a_0~\\textrm{mod}~2) > (a_1~\\textrm{mod}~2)\n - (a_1~\\textrm{mod}~2) < (a_2~\\textrm{mod}~2)\n - (a_2~\\textrm{mod}~2) = (a_3~\\textrm{mod}~2)\n - (a_3~\\textrm{mod}~2) > (a_4~\\textrm{mod}~2)\nThus, the response to this query should be 1."} +{"id": "02643", "text": "We have a permutation P = P_1, P_2, \\ldots, P_N of 1, 2, \\ldots, N.\nYou have to do the following N - 1 operations on P, each exactly once, in some order:\n - Swap P_1 and P_2.\n - Swap P_2 and P_3.\n\\vdots\n - Swap P_{N-1} and P_N.\nYour task is to sort P in ascending order by configuring the order of operations.\nIf it is impossible, print -1 instead.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 2 \\times 10^5\n - P is a permutation of 1, 2, \\ldots, N.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nP_1 P_2 \\ldots P_N\n\n-----Output-----\nIf it is impossible to sort P in ascending order by configuring the order of operations, print -1.\nOtherwise, print N-1 lines to represent a sequence of operations that sorts P in ascending order.\nThe i-th line (1 \\leq i \\leq N - 1) should contain j, where the i-th operation swaps P_j and P_{j + 1}.\nIf there are multiple such sequences of operations, any of them will be accepted.\n\n-----Sample Input-----\n5\n2 4 1 5 3\n\n-----Sample Output-----\n4\n2\n3\n1\n\nThe following sequence of operations sort P in ascending order:\n - First, swap P_4 and P_5, turning P into 2, 4, 1, 3, 5.\n - Then, swap P_2 and P_3, turning P into 2, 1, 4, 3, 5.\n - Then, swap P_3 and P_4, turning P into 2, 1, 3, 4, 5.\n - Finally, swap P_1 and P_2, turning P into 1, 2, 3, 4, 5."} +{"id": "02644", "text": "AtCoDeer the deer and his friend TopCoDeer is playing a game.\nThe game consists of N turns.\nIn each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition:\n(\u203b) After each turn, (the number of times the player has played Paper)\u2266(the number of times the player has played Rock).\nEach player's score is calculated by (the number of turns where the player wins) - (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.\n(For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)\nWith his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the N turns, before the game starts.\nPlan AtCoDeer's gesture in each turn to maximize AtCoDeer's score.\nThe gesture that TopCoDeer will play in each turn is given by a string s. If the i-th (1\u2266i\u2266N) character in s is g, TopCoDeer will play Rock in the i-th turn. Similarly, if the i-th (1\u2266i\u2266N) character of s in p, TopCoDeer will play Paper in the i-th turn.\n\n-----Constraints-----\n - 1\u2266N\u226610^5\n - N=|s|\n - Each character in s is g or p.\n - The gestures represented by s satisfy the condition (\u203b).\n\n-----Input-----\nThe input is given from Standard Input in the following format:\ns\n\n-----Output-----\nPrint the AtCoDeer's maximum possible score.\n\n-----Sample Input-----\ngpg\n\n-----Sample Output-----\n0\n\nPlaying the same gesture as the opponent in each turn results in the score of 0, which is the maximum possible score."} +{"id": "02645", "text": "There is a cave.\nThe cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.\nIt is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.\nSince it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.\n - If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.\nDetermine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 2 \\times 10^5\n - 1 \\leq A_i, B_i \\leq N\\ (1 \\leq i \\leq M)\n - A_i \\neq B_i\\ (1 \\leq i \\leq M)\n - One can travel between any two rooms by traversing passages.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 B_1\n:\nA_M B_M\n\n-----Output-----\nIf there is no way to place signposts satisfying the objective, print No.\nOtherwise, print N lines. The first line should contain Yes, and the i-th line (2 \\leq i \\leq N) should contain the integer representing the room indicated by the signpost in Room i.\n\n-----Sample Input-----\n4 4\n1 2\n2 3\n3 4\n4 2\n\n-----Sample Output-----\nYes\n1\n2\n2\n\nIf we place the signposts as described in the sample output, the following happens:\n - Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \\to 1. This is the minimum number of passages possible.\n - Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \\to 2 \\to 1. This is the minimum number of passages possible.\n - Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \\to 2 \\to 1. This is the minimum number of passages possible.\nThus, the objective is satisfied."} +{"id": "02646", "text": "We have an H \\times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).\nSnuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.\nBefore Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.\nWhen the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares. \nThe color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.\n\n"} +{"id": "02647", "text": "Snuke has decided to play a game using cards.\nHe has a deck consisting of N cards. On the i-th card from the top, an integer A_i is written.\nHe will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, N is odd, which guarantees that at least one card can be kept.\nOperation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.\n\n-----Constraints-----\n - 3 \u2266 N \u2266 10^{5}\n - N is odd.\n - 1 \u2266 A_i \u2266 10^{5}\n - A_i is an integer.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\nA_1 A_2 A_3 ... A_{N}\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n5\n1 2 1 3 7\n\n-----Sample Output-----\n3\n\nOne optimal solution is to perform the operation once, taking out two cards with 1 and one card with 2. One card with 1 and another with 2 will be eaten, and the remaining card with 1 will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: 1, 3 and 7."} +{"id": "02648", "text": "There are N points on the 2D plane, i-th of which is located on (x_i, y_i).\nThere can be multiple points that share the same coordinate.\nWhat is the maximum possible Manhattan distance between two distinct points?\nHere, the Manhattan distance between two points (x_i, y_i) and (x_j, y_j) is defined by |x_i-x_j| + |y_i-y_j|.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq x_i,y_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nx_1 y_1\nx_2 y_2\n:\nx_N y_N\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n3\n1 1\n2 4\n3 2\n\n-----Sample Output-----\n4\n\nThe Manhattan distance between the first point and the second point is |1-2|+|1-4|=4, which is maximum possible."} +{"id": "02649", "text": "There are N infants registered in AtCoder, numbered 1 to N, and 2\\times 10^5 kindergartens, numbered 1 to 2\\times 10^5.\nInfant i has a rating of A_i and initially belongs to Kindergarten B_i.\nFrom now on, Q transfers will happen.\nAfter the j-th transfer, Infant C_j will belong to Kindergarten D_j.\nHere, we define the evenness as follows. For each kindergarten with one or more infants registered in AtCoder, let us find the highest rating of an infant in the kindergarten. The evenness is then defined as the lowest among those ratings.\nFor each of the Q transfers, find the evenness just after the transfer.\n\n-----Constraints-----\n - 1 \\leq N,Q \\leq 2 \\times 10^5\n - 1 \\leq A_i \\leq 10^9\n - 1 \\leq C_j \\leq N\n - 1 \\leq B_i,D_j \\leq 2 \\times 10^5\n - All values in input are integers.\n - In the j-th transfer, Infant C_j changes the kindergarten it belongs to.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN Q\nA_1 B_1\nA_2 B_2\n:\nA_N B_N\nC_1 D_1\nC_2 D_2\n:\nC_Q D_Q\n\n-----Output-----\nPrint Q lines.\nThe j-th line should contain the evenness just after the j-th transfer.\n\n-----Sample Input-----\n6 3\n8 1\n6 2\n9 3\n1 1\n2 2\n1 3\n4 3\n2 1\n1 2\n\n-----Sample Output-----\n6\n2\n6\n\nInitially, Infant 1, 4 belongs to Kindergarten 1, Infant 2, 5 belongs to Kindergarten 2, and Infant 3, 6 belongs to Kindergarten 3.\nAfter the 1-st transfer that makes Infant 4 belong to Kindergarten 3, Infant 1 belongs to Kindergarten 1, Infant 2, 5 belong to Kindergarten 2, and Infant 3, 4, 6 belong to Kindergarten 3. The highest ratings of an infant in Kindergarten 1, 2, 3 are 8, 6, 9, respectively. The lowest among them is 6, so the 1-st line in the output should contain 6.\nAfter the 2-nd transfer that makes Infant 2 belong to Kindergarten 1, Infant 1, 2 belong to Kindergarten 1, Infant 5 belongs to Kindergarten 2, and Infant 3, 4, 6 belong to Kindergarten 3. The highest ratings of an infant in Kindergarten 1, 2, 3 are 8, 2, 9, respectively. The lowest among them is 2, so the 2-nd line in the output should contain 2.\nAfter the 3-rd transfer that makes Infant 1 belong to Kindergarten 2, Infant 2 belongs to Kindergarten 1, Infant 1, 5 belong to Kindergarten 2, and Infant 3, 4, 6 belong to Kindergarten 3. The highest ratings of an infant in Kindergarten 1, 2, 3 are 6, 8, 9, respectively. The lowest among them is 6, so the 3-rd line in the output should contain 6."} +{"id": "02650", "text": "On a two-dimensional plane, there are m lines drawn parallel to the x axis, and n lines drawn parallel to the y axis.\nAmong the lines parallel to the x axis, the i-th from the bottom is represented by y = y_i.\nSimilarly, among the lines parallel to the y axis, the i-th from the left is represented by x = x_i.\nFor every rectangle that is formed by these lines, find its area, and print the total area modulo 10^9+7.\nThat is, for every quadruple (i,j,k,l) satisfying 1\\leq i < j\\leq n and 1\\leq k < l\\leq m, find the area of the rectangle formed by the lines x=x_i, x=x_j, y=y_k and y=y_l, and print the sum of these areas modulo 10^9+7.\n\n-----Constraints-----\n - 2 \\leq n,m \\leq 10^5\n - -10^9 \\leq x_1 < ... < x_n \\leq 10^9\n - -10^9 \\leq y_1 < ... < y_m \\leq 10^9\n - x_i and y_i are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn m\nx_1 x_2 ... x_n\ny_1 y_2 ... y_m\n\n-----Output-----\nPrint the total area of the rectangles, modulo 10^9+7.\n\n-----Sample Input-----\n3 3\n1 3 4\n1 3 6\n\n-----Sample Output-----\n60\n\nThe following figure illustrates this input:\n\nThe total area of the nine rectangles A, B, ..., I shown in the following figure, is 60."} +{"id": "02651", "text": "There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.\nYou can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.\nYour objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?\n\n-----Constraints-----\n - 2 \u2264 N \u2264 10^5\n - 0 \u2264 x_i,y_i \u2264 10^9\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nx_1 y_1\nx_2 y_2\n:\nx_N y_N\n\n-----Output-----\nPrint the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.\n\n-----Sample Input-----\n3\n1 5\n3 9\n7 8\n\n-----Sample Output-----\n3\n\nBuild a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen."} +{"id": "02652", "text": "Given is a rooted tree with N vertices numbered 1 to N.\nThe root is Vertex 1, and the i-th edge (1 \\leq i \\leq N - 1) connects Vertex a_i and b_i.\nEach of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.\nNow, the following Q operations will be performed:\n - Operation j (1 \\leq j \\leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.\nFind the value of the counter on each vertex after all operations.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq Q \\leq 2 \\times 10^5\n - 1 \\leq a_i < b_i \\leq N\n - 1 \\leq p_j \\leq N\n - 1 \\leq x_j \\leq 10^4\n - The given graph is a tree.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN Q\na_1 b_1\n:\na_{N-1} b_{N-1}\np_1 x_1\n:\np_Q x_Q\n\n-----Output-----\nPrint the values of the counters on Vertex 1, 2, \\ldots, N after all operations, in this order, with spaces in between.\n\n-----Sample Input-----\n4 3\n1 2\n2 3\n2 4\n2 10\n1 100\n3 1\n\n-----Sample Output-----\n100 110 111 110\n\nThe tree in this input is as follows:\n\nEach operation changes the values of the counters on the vertices as follows:\n - Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.\n - Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.\n - Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively."} +{"id": "02653", "text": "There are N integers X_1, X_2, \\cdots, X_N, and we know that A_i \\leq X_i \\leq B_i.\nFind the number of different values that the median of X_1, X_2, \\cdots, X_N can take.\n\n-----Notes-----\nThe median of X_1, X_2, \\cdots, X_N is defined as follows. Let x_1, x_2, \\cdots, x_N be the result of sorting X_1, X_2, \\cdots, X_N in ascending order.\n - If N is odd, the median is x_{(N+1)/2};\n - if N is even, the median is (x_{N/2} + x_{N/2+1}) / 2.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq A_i \\leq B_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 B_1\nA_2 B_2\n:\nA_N B_N\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n2\n1 2\n2 3\n\n-----Sample Output-----\n3\n\n - If X_1 = 1 and X_2 = 2, the median is \\frac{3}{2};\n - if X_1 = 1 and X_2 = 3, the median is 2;\n - if X_1 = 2 and X_2 = 2, the median is 2;\n - if X_1 = 2 and X_2 = 3, the median is \\frac{5}{2}.\nThus, the median can take three values: \\frac{3}{2}, 2, and \\frac{5}{2}."} +{"id": "02654", "text": "Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.\nThe N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.\nWhen each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.\nWhat is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq A_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 \\dots A_N\n\n-----Output-----\nPrint the maximum total comfort the N players can get.\n\n-----Sample Input-----\n4\n2 2 1 3\n\n-----Sample Output-----\n7\n\nBy arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.\n\nThey cannot get the total comfort greater than 7, so the answer is 7."} +{"id": "02655", "text": "How many strings can be obtained by applying the following operation on a string S exactly K times: \"choose one lowercase English letter and insert it somewhere\"?\nThe answer can be enormous, so print it modulo (10^9+7).\n\n-----Constraints-----\n - K is an integer between 1 and 10^6 (inclusive).\n - S is a string of length between 1 and 10^6 (inclusive) consisting of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK\nS\n\n-----Output-----\nPrint the number of strings satisfying the condition, modulo (10^9+7).\n\n-----Sample Input-----\n5\noof\n\n-----Sample Output-----\n575111451\n\nFor example, we can obtain proofend, moonwolf, and onionpuf, while we cannot obtain oofsix, oofelevennn, voxafolt, or fooooooo."} +{"id": "02656", "text": "Let {\\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.\nFrom n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\\rm comb}(a_i,a_j) is maximized.\nIf there are multiple pairs that maximize the value, any of them is accepted.\n\n-----Constraints-----\n - 2 \\leq n \\leq 10^5\n - 0 \\leq a_i \\leq 10^9\n - a_1,a_2,...,a_n are pairwise distinct.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn\na_1 a_2 ... a_n\n\n-----Output-----\nPrint a_i and a_j that you selected, with a space in between.\n\n-----Sample Input-----\n5\n6 9 4 2 11\n\n-----Sample Output-----\n11 6\n\n\\rm{comb}(a_i,a_j) for each possible selection is as follows:\n - \\rm{comb}(4,2)=6 \n - \\rm{comb}(6,2)=15 \n - \\rm{comb}(6,4)=15 \n - \\rm{comb}(9,2)=36 \n - \\rm{comb}(9,4)=126 \n - \\rm{comb}(9,6)=84 \n - \\rm{comb}(11,2)=55 \n - \\rm{comb}(11,4)=330 \n - \\rm{comb}(11,6)=462 \n - \\rm{comb}(11,9)=55\nThus, we should print 11 and 6."} +{"id": "02657", "text": "The Kingdom of Takahashi has N towns, numbered 1 through N.\nThere is one teleporter in each town. The teleporter in Town i (1 \\leq i \\leq N) sends you to Town A_i.\nTakahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.\nHelp the king by writing a program that answers this question.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq A_i \\leq N\n - 1 \\leq K \\leq 10^{18}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nA_1 A_2 \\dots A_N\n\n-----Output-----\nPrint the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.\n\n-----Sample Input-----\n4 5\n3 2 4 1\n\n-----Sample Output-----\n4\n\nIf we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \\to 3 \\to 4 \\to 1 \\to 3 \\to 4."} +{"id": "02658", "text": "Let S(n) denote the sum of the digits in the decimal notation of n.\nFor example, S(123) = 1 + 2 + 3 = 6.\nWe will call an integer n a Snuke number when, for all positive integers m such that m > n, \\frac{n}{S(n)} \\leq \\frac{m}{S(m)} holds.\nGiven an integer K, list the K smallest Snuke numbers.\n\n-----Constraints-----\n - 1 \\leq K\n - The K-th smallest Snuke number is not greater than 10^{15}.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK\n\n-----Output-----\nPrint K lines. The i-th line should contain the i-th smallest Snuke number.\n\n-----Sample Input-----\n10\n\n-----Sample Output-----\n1\n2\n3\n4\n5\n6\n7\n8\n9\n19\n"} +{"id": "02659", "text": "There is a function f(x), which is initially a constant function f(x) = 0.\nWe will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:\n - An update query 1 a b: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x).\n - An evaluation query 2: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value.\nWe can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq Q \\leq 2 \\times 10^5\n - -10^9 \\leq a, b \\leq 10^9\n - The first query is an update query.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nQ\nQuery_1\n:\nQuery_Q\n\nSee Sample Input 1 for an example.\n\n-----Output-----\nFor each evaluation query, print a line containing the response, in the order in which the queries are given.\nThe response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between.\n\n-----Sample Input-----\n4\n1 4 2\n2\n1 1 -8\n2\n\n-----Sample Output-----\n4 2\n1 -3\n\nIn the first evaluation query, f(x) = |x - 4| + 2, which attains the minimum value of 2 at x = 4.\nIn the second evaluation query, f(x) = |x - 1| + |x - 4| - 6, which attains the minimum value of -3 when 1 \\leq x \\leq 4. Among the multiple values of x that minimize f(x), we ask you to print the minimum, that is, 1."} +{"id": "02660", "text": "Given are an integer N and arrays S, T, U, and V, each of length N.\nConstruct an N\u00d7N matrix a that satisfy the following conditions:\n - a_{i,j} is an integer.\n - 0 \\leq a_{i,j} \\lt 2^{64}.\n - If S_{i} = 0, the bitwise AND of the elements in the i-th row is U_{i}.\n - If S_{i} = 1, the bitwise OR of the elements in the i-th row is U_{i}.\n - If T_{i} = 0, the bitwise AND of the elements in the i-th column is V_{i}.\n - If T_{i} = 1, the bitwise OR of the elements in the i-th column is V_{i}.\nHowever, there may be cases where no matrix satisfies the conditions.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 500 \n - 0 \\leq S_{i} \\leq 1 \n - 0 \\leq T_{i} \\leq 1 \n - 0 \\leq U_{i} \\lt 2^{64} \n - 0 \\leq V_{i} \\lt 2^{64} \n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS_{1} S_{2} ... S_{N}\nT_{1} T_{2} ... T_{N}\nU_{1} U_{2} ... U_{N}\nV_{1} V_{2} ... V_{N}\n\n-----Output-----\nIf there exists a matrix that satisfies the conditions, print one such matrix in the following format:\na_{1,1} ... a_{1,N}\n:\na_{N,1} ... a_{N,N}\n\nNote that any matrix satisfying the conditions is accepted.\nIf no matrix satisfies the conditions, print -1.\n\n-----Sample Input-----\n2\n0 1\n1 0\n1 1\n1 0\n\n-----Sample Output-----\n1 1\n1 0\n\nIn Sample Input 1, we need to find a matrix such that:\n - the bitwise AND of the elements in the 1-st row is 1;\n - the bitwise OR of the elements in the 2-nd row is 1;\n - the bitwise OR of the elements in the 1-st column is 1;\n - the bitwise AND of the elements in the 2-nd column is 0."} +{"id": "02661", "text": "You are given a sequence with N integers: A = \\{ A_1, A_2, \\cdots, A_N \\}.\nFor each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:\n - If A_i and A_j (i < j) are painted with the same color, A_i < A_j.\nFind the minimum number of colors required to satisfy the condition.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - 0 \\leq A_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1\n:\nA_N\n\n-----Output-----\nPrint the minimum number of colors required to satisfy the condition.\n\n-----Sample Input-----\n5\n2\n1\n4\n5\n3\n\n-----Sample Output-----\n2\n\nWe can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue."} +{"id": "02662", "text": "Chef likes arrays a lot. Today, he found an array A consisting of N positive integers.\nLet L denote the sorted (in non-increasing order) list of size N*(N+1)/2 containing the sums of all possible contiguous subarrays of A. Chef is interested in finding the first K elements from the list L. Can you help him in accomplishing this task?\n\n-----Input-----\nThere is only a single test case per input file.\nThe first line of input contains two space separated integer numbers N and K denoting the size of the array and the number of the maximal sums you need to find.\nThe following line contains N space separated integer numbers denoting the array A.\n\n-----Output-----\nOutput K space separated integers where the ith integer denotes the ith element of L.\n\n-----Constraints-----\n- 1 \u2264 N \u2264 105\n- 1 \u2264 K \u2264 min(N*(N+1)/2, 105)\n- 1 \u2264 Ai \u2264 109\n\n-----Subtasks-----\n- Subtask 1 (47 pts) : 1 \u2264 N \u2264 1000, 1 \u2264 K \u2264 min{N*(N+1)/2, 105}\n- Subtask 2 (53 pts) : 1 \u2264 N \u2264 105, 1 \u2264 K \u2264 min{N*(N+1)/2, 105}\n\n-----Example-----\nInput 13 4\n1 3 4\n\nOutput 18 7 4 4\n\nInput 23 3\n10 2 7\n\nOutput 219 12 10\n\n-----Explanation-----\nTest 1:\n\nThe first 4 elements of it are [8, 7, 4, 4]."} +{"id": "02663", "text": "Chef has bought ten balls of five colours. There are two balls of each colour. Balls of same colour have same weight. Let us enumerate colours by numbers from 1 to 5. Chef knows that all balls, except two (of same colour), weigh exactly one kilogram. He also knows that each of these two balls is exactly 1 kg heavier than other balls.\n\nYou need to find the colour which balls are heavier than others. \nTo do that, you can use mechanical scales with two weighing pans. As your scales are very accurate, you can know the exact difference of weights of objects from first and second pans. Formally, the scales will give you the difference (signed difference) of weights of the objects put into the first pan and the second pan. See the following examples for details.\n\n- If you put two balls of the same colour on your scales, each ball to one pan, the scales will tell you that difference is \"0\".\n- But if you put into the first pan some balls of total weight 3 kg, and into the second pan of 5 kg, then scales will tell you \"-2\" because the second pan is 2 kg heavier than first. \n- Similarly, if you put 5 kg weight in first pan and 3 kg in the second pan, then scale will tell you \"2\" as first pan is 2 kg heavier than second.\n\n-----Input & Output-----\n- The interaction process have two phases. At first phase you perform sequence of weighings on the mechanical scales. At the second phase you should output the colour of the heavier balls.\n- To use the mechanical scales, you should print \"1\"(without quotes) and then print two lines, the first line will describe the enumeration of colours of balls on the first pan and second line should that of second pan.\n- To describe some pan, you need to print one integer n - the number of balls you put in this pan, followed by n space-separated integers - colours of the balls you put in this pan. \n- Once you have printed required data, you can read from the standard input one integer - the difference of weights of the first and the second pans.\n- To output the colour of the heavier balls, you should print \"2\"(without quotes) and one integer in next line - colour of the heavier balls. \n\n-----Constraints-----\n- Each colour you print should be between 1 and 5.\n- In each weighings, you can use at most two balls of same colour.\n- Note that you can use scales as much times as you want (even zero).\n- Once your program printed the colour of the heavier balls, it should finish its work, i.e. you should exit the program. \n- Do not forget to flush standard output after each line you print.\n\n-----Subtasks-----\n- If you output incorrect colour (i.e. colour you printed is not that of heavier balls), your score will be zero.\n- Let K will be the number of times you used scales.\n- Then your score will be 100/K points.\n- Please note that if K equals to zero, your score also will also be 100 points.\n\n-----Example 1-----\n\nPlease note that the content in left side denotes output of your program where content in the right side denotes the response of judge which you should read as input. \n\nInput and Output\n1\n1 1\n1 1\t\n\t\t\t\t\t0\n1\n1 1\n1 5\n\t\t\t\t\t-1\n2\n5\n\n-----Example 2-----\nInput and Output\n1\n3 1 2 5\n0\n\t\t\t\t\t4\n1\n0\n1 2\n\t\t\t\t\t-1\n1\n0\n1 5\n\t\t\t\t\t-2\n2\n5\n\n-----Explanation-----\nIn the first example, you first printed :\n\n1\n1 1\n1 1\n\nThis means that you want to use the scale. Each of the first and second pan has one ball of colour 1.\nDon't forget to flush the standard output after printing this.\n\nAfter that you should read the input, the difference of weights between the two pans, in this case, it will be 0. \n\nNow you printed : \n\n1\n1 1\n1 5\n\nIt means, that you decided to use scale again. Now first pan contains one ball of colour 1 and the second pan contains one ball of colour 5.\n\nNow, you should read the input, the difference of weights between the two pans, in this case, it will be -1.\n\nNow, you realize that ball of colour 5 is heavier than of colour 1. It means that 5th colour is the colour of heaviest ball. So your print 2 followed by 5 in separate lines and exit your program.\n\nYou have used scale total 2 times. Hence your score will be 100/2 = 50.\n\nIn the second example, your score will be 100 / 3 = 33.3333333\n\n-----Notes-----\n\nPlease note that clearing the output buffer can be done by using fflush(stdout) command or by setting the proper type of buffering at the beginning of the execution - setlinebuf(stdout). Failure to flush the output buffer will result in Time Limit Exceeded verdict.\n\nThere are 5 test files. For each of the test file, score you will get is described above. The total sum of scores of your program for all the test files will be displayed on the contest page. This will be considered your final score for the problem. \n\nTotal points awarded for this problem will be equal to (your score) / (best score achieved in the contest) * 100."} +{"id": "02664", "text": "Chef loves squares! You are given N points with integers coordinates, Chef asks you to find out how many points he should add to these set of N points, so that one could create at least one square having its vertices from the points of the resulting set. Note that the square created need not to be parallel to the axis.\n\n-----Input-----\nThe first line contains singe integer N. \nEach of next N lines contains two integers Xi and Yi denotine the coordinates of i-th point. \n\n-----Output-----\nIn a single line print single integer - the minimal number of points Chef need to paint to receive at least one square. \n\n-----Constraints-----\n- 0 \u2264 N \u2264 2000\n- -10^6 \u2264 Xi, Yi \u2264 10^6\n- There are NO coincided points\n\n-----Example-----\nInput:\n3\n0 0\n2 2\n3 3\n\nOutput:\n2\n\nInput:\n5\n0 0\n100 100\n200 200\n100 0\n0 100\n\nOutput:\n0\n\n-----Explanation-----\nFor the first example Chef can add points (2, 0), (0, 2) or (2, 3), (3, 2)\nFor the second example Chef already has square (0, 0), (100, 0), (0, 100), (100, 100)."} +{"id": "02665", "text": "Ramu was a lazy farmer. He had inherited a fairly large farm and a nice house from his father. Ramu leased out the farm land to others and earned a rather handsome income. His father used to keep a buffalo at home and sell its milk but the buffalo died a few days after his father did.\nRamu too wanted to make some money from buffaloes, but in a quite a different way. He decided that his future lay in speculating on buffaloes. In the market in his village, buffaloes were bought and sold everyday. The price fluctuated over the year, but on any single day the price was always the same.\nHe decided that he would buy buffaloes when the price was low and sell them when the price was high and, in the process, accummulate great wealth. Unfortunately his house had space for just one buffalo and so he could own at most one buffalo at any time.\nBefore he entered the buffalo market, he decided to examine to examine the variation in the price of buffaloes over the last few days and determine the maximum profit he could have made. Suppose, the price of a buffalo over the last $10$ days varied as\n1012811111012151310101281111101215131010\\quad 12\\quad 8\\quad 11\\quad 11\\quad 10\\quad 12\\quad 15\\quad 13\\quad 10\nRamu is a lazy fellow and he reckoned that he would have been willing to visit the market at most $5$ times (each time to either buy or sell a buffalo) over the last $10$ days. Given this, the maximum profit he could have made is $9$ rupees. To achieve this, he buys a buffalo on day $1$, sells it on day $2$, buys one more on day $3$ and sells it on day $8$. If he was a little less lazy and was willing to visit the market $6$ times, then he could have made more money. He could have bought on day $1$, sold on day $2$, bought on day $3$, sold on day $4$, bought on day $6$ and sold on day $8$ to make a profit of $10$ rupees.\nYour task is help Ramu calculate the maximum amount he can earn by speculating on buffaloes, given a history of daily buffalo prices over a period and a limit on how many times Ramu is willing to go to the market during this period.\n\n-----Input:-----\n- The first line of the input contains two integers $N$ and $K$, where $N$ is the number of days for which the price data is available and $K$ is the maximum number of times that Ramu is willing to visit the cattle market. \n- The next $N$ lines (line $2, 3,...,N+1$) contain a single positive integer each. The integer on line $i+1$, $1 \\leq i \\leq N$, indicates the price of a buffalo on day $i$.\n\n-----Output:-----\nA single nonnegative integer indicating that maximum amount of profit that Ramu can make if he were to make at most $K$ trips to the market.\n\n-----Constraints:-----\n- $1 \\leq N \\leq 400$.\n- $1 \\leq K \\leq 400$.\n- $0 \\leq$ price of a buffalo on any day $\\leq 1000$\n\n-----Sample Input 1:-----\n10 5\n10\n12\n8\n11\n11\n10\n12\n15\n13\n10\n\n-----Sample Output 1:-----\n9\n\n-----Sample Input 2:-----\n10 6\n10\n12\n8\n11\n11\n10\n12\n15\n13\n10\n\n-----Sample Output 2:-----\n10"} +{"id": "02666", "text": "Are you fond of collecting some kind of stuff? Mike is crazy about collecting stamps. He is an active member of Stamp Collecting \u0421ommunity(SCC). \n\nSCC consists of N members which are fond of philately. A few days ago Mike argued with the others from SCC. Mike told them that all stamps of the members could be divided in such a way that i'th member would get i postage stamps. Now Mike wants to know if he was right. The next SCC meeting is tomorrow. Mike still has no answer.\n\nSo, help Mike! There are N members in the SCC, i'th member has Ci stamps in his collection. Your task is to determine if it is possible to redistribute C1 + C2 + ... + Cn stamps among the members of SCC thus that i'th member would get i stamps.\n\n-----Input-----\nThe first line contains one integer N, denoting the number of members of SCC.\nThe second line contains N integers Ci, denoting the numbers of the stamps in the collection of i'th member.\n\n-----Output-----\nThe first line should contain YES, if we can obtain the required division, otherwise NO.\n\n-----Constraints-----\n1 \u2264 N \u2264 100 000;\n1 \u2264 Ci \u2264 109.\n\n-----Examples-----\nInput:\n5\n7 4 1 1 2\n\nOutput:\nYES\n\nInput:\n5\n1 1 1 1 1\n\nOutput:\nNO"} +{"id": "02667", "text": "Today, Chef woke up to find that he had no clean socks. Doing laundry is such a turn-off for Chef, that in such a situation, he always buys new socks instead of cleaning the old dirty ones. He arrived at the fashion store with money rupees in his pocket and started looking for socks. Everything looked good, but then Chef saw a new jacket which cost jacketCost rupees. The jacket was so nice that he could not stop himself from buying it.\n\nInterestingly, the shop only stocks one kind of socks, enabling them to take the unsual route of selling single socks, instead of the more common way of selling in pairs. Each of the socks costs sockCost rupees.\n\nChef bought as many socks as he could with his remaining money. It's guaranteed that the shop has more socks than Chef can buy. But now, he is interested in the question: will there be a day when he will have only 1 clean sock, if he uses a pair of socks each day starting tommorow? If such an unlucky day exists, output \"Unlucky Chef\", otherwise output \"Lucky Chef\". Remember that Chef never cleans or reuses any socks used once.\n\n-----Input-----\nThe first line of input contains three integers \u2014 jacketCost, sockCost, money \u2014 denoting the cost of a jacket, cost of a single sock, and the initial amount of money Chef has, respectively.\n\n-----Output-----\nIn a single line, output \"Unlucky Chef\" if such a day exists. Otherwise, output \"Lucky Chef\". \n\n-----Constraints-----\n- 1 \u2264 jacketCost \u2264 money \u2264 109\n- 1 \u2264 sockCost \u2264 109\n\n-----Example-----\nInput:\n1 2 3\n\nOutput:\nUnlucky Chef\n\nInput:\n1 2 6\n\nOutput:\nLucky Chef\n\n-----Subtasks-----\n- Subtask 1: jacketCost, money, sockCost \u2264 103. Points - 20\n- Subtask 2: Original constraints. Points - 80\n\n-----Explanation-----\nTest #1:\nWhen Chef arrived at the shop, he had 3 rupees. After buying the jacket, he has 2 rupees left, enough to buy only 1 sock.\nTest #2:\nChef had 6 rupees in the beginning. After buying the jacket, he has 5 rupees left, enough to buy a pair of socks for 4 rupees."} +{"id": "02668", "text": "The lockdown hasn\u2019t been great for our chef as his restaurant business got sabotaged because of lockdown, nevertheless no worries our chef is a multi-talented guy. Chef has decided to be a freelancer and work remotely. According to chef\u2019s maths, he should be able to work on maximum tasks assuming that he can only work on a single task at a time. \nAssume that chef does N tasks. The start and finish time units of those tasks are given. Select the maximum number of tasks that can be performed by a chef, assuming that a he can only work on a single task at a time.\n\n-----Input:-----\n- The first input contains of size of array N representing the total number of tasks.\n- The second and third lines contains N space-seperated integers representing the starting and finish time of the the tasks.\n\n-----Output:-----\nA single line containing indices of tasks that chef will be able to do.\n\n-----Constraints-----\n- $1 \\leq N \\leq 10^5$\n- Note: The lists have been sorted based on the ending times of the tasks.\n\n-----Sample Input 1:-----\n3\n\n10 12 20\n\n20 25 30 \n\n-----Sample Output 1:-----\n0 2 \n\n-----Sample Input 2:-----\n6\n\n1 2 0 6 3 7\n\n2 4 5 7 9 10 \n\n-----Sample Output 2:-----\n0 1 3 5"} +{"id": "02669", "text": "A pair of strings $(\u03b1, \u03b2)$ is called a subpair of a string $x$ if $x$ = $x_1+$$\u03b1+$$x_2+$$\u03b2+$$x_3$ (where a+b means concatenation of strings a and b) for some (possibly empty) strings $x1, x2$ and $x3$. We are given two strings and we need to find one subpair from each string such that : \nLet $(a,b) , (c,d) $be subpair of $string1$ and $string2$ respectively and $X$ $=$ $a$ + $b$ + $c$ + $d$\n- $X$ is a palindrome\n- $|a| = |d|$\n- $|b| = |c|$\n- $|X|$ is maximum\n\n-----Input Format:-----\n- First line will contain $T$, number of testcases. Then the testcases follow. \n- Each testcase contains of a single line of input, two strings str1, str2. \n\n-----Output Format:-----\n- For each testcase, output in a single line representing the length of palindrome |X|.\n\n-----Constraints-----\n- $1 \\leq T \\leq 5$\n- $2 \\leq |str1| \\leq 10^3$\n- $2 \\leq |str2| \\leq 10^3$\n\n-----Sample Input 1:-----\n1\nabgcd dchbag\n\n-----Sample Output 1:-----\n8\n\n-----Sample Input 2:-----\n4 \n\naaa aaa\n\nzaaax yaaaw\n\nzax yaw\n\nzx yw\n\n-----Sample Output 2:-----\n6\n\n6\n\n2\n\n0 \n\n-----EXPLANATION:-----\nSample Testcase 1: The subpairs are (\"ab\",\"cd\") and (\"dc\",\"ba\"). When the subpairs are concatenated string is \"abcddcba\" which is a pallindrome , |\"ab\"| = |\"ba\"|, |\"cd\"| = |\"dc\"| and has the maximum length equal to 8."} +{"id": "02670", "text": "Andy got a box of candies for Christmas. In fact, he discovered that the box contained several identical smaller boxes, and they could contain even smaller boxes, and so on. Formally, we say that candies are boxes of level 0, and for 1 \u2264 i \u2264 n, a level i box contains ai boxes of level i\u2009-\u20091. The largest box has level n. Andy realized that it can take quite a long time to open all the boxes before he actually gets to eat some candies, so he put the box aside in frustration.\n\nBut today being his birthday, some friends came to visit Andy, and Andy decided to share some candies with them. In order to do that, he must open some of the boxes. Naturally, Andy can not open a box that is still inside an unopened box. If Andy wants to retrieve X candies, what is the least number of boxes he must open? You must help him answer many such queries. Each query is independent.\n\n-----Input-----\n- The first line contains two integers n and m, which refer to the level of the largest box, and the number of queries respectively.\n- The second line contains n integers a1,\u2009...,\u2009an.\n- The third line contains m integers X1,\u2009...,\u2009Xm.\n\n-----Output-----\n- Print m integers each in a new line, ith of them equal to the smallest number of boxes Andy must open in order to retrieve at least Xi candies.\n\n-----Constraints-----\n- 1\u2009\u2264\u2009n,m\u2009\u2264\u2009300000\n- 1\u2009\u2264\u2009ai\u2009\u2264\u2009109\n- 1\u2009\u2264\u2009Xi\u2009\u2264\u20091012\n- It is guaranteed that the total number of candies is at least Xi for all i\n\n-----Example-----\nInput 1:\n5 1\n1 1 1 1 1\n1\n\nOutput 1:\n5\n\nInput 2:\n3 3\n3 3 3\n2 8 13\n\nOutput 2:\n3\n5\n8\n\n-----Explanation-----\nTestcase 1: The only candy is contained in five levels of boxes. \nTestcase 2: In the third query, for 13 candies, Andy should open the largest box, two level-2 boxes, and finally five of six available level-1 boxes. Each of those boxes will contain 3 level-0 boxes (which are candies). So he'll have 15 candies in total, but he needs only 13 of them."} +{"id": "02671", "text": "Guys don\u2019t misinterpret this it is three only.\n\n-----Input:-----\n- First line will contain an integer $X$.\n\n-----Output:-----\nA single line containing the answer to the problem modulo 1000000007.\n\n-----Constraints-----\n- $1 \\leq X < 10^5$\n\n-----Sample Input 1:-----\n1\n\n-----Sample Output 1:-----\n3\n\n-----Sample Input 2:-----\n2\n\n-----Sample Output 2:-----\n14"} +{"id": "02672", "text": "Chef loves games! But he likes to invent his own. Now he plays game \"Digit Jump\". Chef has a sequence of digits $S_{1}, S_{2}, \\ldots , S_{N}$. He is staying in the first digit $S_{1}$ and wants to reach the last digit $S_{N}$ in the minimal number of jumps. \nWhile staying in some index $i$ Chef can jump into $i - 1$ and $i + 1$, but he can't jump out from sequence. Or he can jump into any digit with the same value $S_i$. \nHelp Chef to find the minimal number of jumps he need to reach digit $S_{N}$ from digit $S_1$. \n\n-----Input-----\nInput contains a single line consist of string $S$ of length $N$ - the sequence of digits.\n\n-----Output-----\nIn a single line print single integer - the minimal number of jumps he needs.\n\n-----Constraints-----\n- $1\\leq N \\leq 10^5$\n- Each symbol of $S$ is a digit from $0$ to $9$. \n\n-----Example Input 1-----\n01234567890\n\n-----Example Output 1-----\n1\n\n-----Example Input 2-----\n012134444444443\n\n-----Example Output 2-----\n4\n\n-----Explanation-----\nTest Case 1: Chef can directly jump from the first digit (it is $0$) to the last (as it is also $0$).\nTest Case 2: Chef should follow the following path: $1 - 2 - 4 - 5 - 15$."} +{"id": "02673", "text": "Is the MRP of my new shoes exclusive or inclusive of taxes?\n\n-----Input:-----\n- First line will contain an integer $P$\n\n-----Output:-----\nFor each testcase, print either 'Inclusive' or 'Exclusive' without quotes.\n\n-----Constraints-----\n- $100 \\leq P \\leq 999$\n\n-----Sample Input 1:-----\n123\n\n-----Sample Output 1:-----\nExclusive\n\n-----Sample Input 2:-----\n111\n\n-----Sample Output 2:-----\nInclusive"} +{"id": "02674", "text": "There are $n$ red balls kept on the positive $X$ axis, and $m$ blue balls kept on the positive $Y$ axis. You are given the positions of the balls. For each $i$ from $1$ to $n$, the $i$-th red ball has the coordinates $(x_i, 0)$, where $x_i$ is a positive integer. For each $i$ from $1$ to $m$, the $i$-th blue ball has the coordinates $(0, y_i)$, where $ y_i$ is a positive integer. \nIt is given that all $x_i$ are distinct. Also, all $y_i$ are distinct.\nAt the time $t= 0$, for each $i$ from $1$ to $n$, the $i^{\\text{th}}$ red ball is thrown towards positive $Y$ axis with a speed of $u_i$( that is, with velocity vector $(0, u_i)$). Simultaneously (at time $t = 0$), for each $i$ from $1$ to $m$, the $i^{\\text{th}}$ blue ball is thrown towards positive $X$ axis with a speed of $v_i$ (that is, with velocity vector $(v_i, 0)$).\nTwo balls are said to collide if they are at the same position at the same time. When two balls collide, they disappear, and hence no longer collide with other balls. (See sample examples for clarification).\nFind the total number of collisions the balls will have.\n\n-----Input-----\n- First line contains $n$ and $m$, the number of red balls, and the number of blue balls, respectively. \n- $i^{\\text{th}}$ of the next $n$ lines contains two space separated integers $x_i$ and $u_i$, the position and speed of the $i^{\\text{th}}$ red ball respectively\n- $i^{\\text{th}}$ of the next $m$ lines contains two space separated integers $y_i$ and $v_i$, the position and speed of the $i^{\\text{th}}$ blue ball respectively\n\n-----Output-----\nPrint the number of collisions.\n\n-----Constraints-----\n- $1 \\le n, m \\le 10^5$\n- $1 \\le x_i, u_i, y_i, v_i \\le 10^9$\n- for all $1 \\le i < j \\le n, x_i \\neq x_j$\n- for all $1 \\le i < j \\le m, y_i \\neq y_j$\n\n-----Example Input 1-----\n1 1\n1 2\n2 1\n\n-----Example Output 1-----\n1\n\n-----Example Input 2-----\n1 2\n1 2\n2 1\n1 2\n\n-----Example Output 2-----\n1\n\n-----Explanation-----\nExample case 1: The balls collide at t = 1, at the coordinates (1, 2). \nExample case 2: The red ball and the second blue ball collide at time 0.5 at coordinates (1, 1). Note that the first blue ball would have collided with the red ball at t = 1 (like in sample input # 1), if the second blue ball wasn't present. But since the red ball disappears at t = 0.5, its collision with first blue ball doesn't happen. The total number of collisions is 1."} +{"id": "02675", "text": "Teddy and Freddy are two friends. Teddy has a pile of strings of size $N$. Each string $Si$ in the pile has length less or equal to $100$ ($len(Si) \\leq 100$). \nTeddy and Freddy like playing with strings. Teddy gives Freddy a string $B$ of size $M$.\nTeddy and Freddy like playing with strings. Teddy gives Freddy a string $B$ of size $M$.\nHe asks Freddy to find the count of the unique substrings of $B$ present in pile of strings of size $N$.\nFreddy is busy with the some task, so he asks for your help.Help Freddy by giving the unique substrings of $B$ present in pile of strings of size $N$.\nNote: The substrings having same permutation of characters are considered same.\n\n-----Input:-----\n- First line will contain $N$, number of strings. Then the strings follow. \n- Next $N$ lines will contain a string \n- $N+2$ line contains the $M$ - length of string B\n- Following line contains the string B\n\n-----Output:-----\nFor each testcase, output in a single line number of unique strings of B.\n\n-----Constraints-----\n- $1 \\leq N \\leq 100$\n- $2 \\leq len(Si) \\leq 100$\n- $2 \\leq M \\leq 100$\n\n-----Sample Input:-----\n4\na\nabc \nabcd\nabcde\n5\naaaaa\n\n-----Sample Output:-----\n1\n\n-----Sample Input:-----\n4\na\naa\naaa\naaaa\n5\naaaaa\n\n-----Sample Output:-----\n4\n\n-----EXPLANATION:-----\nTestCase 1: Only substring of $aaaaa$ that is present in the pile of strings is $a$. So the answer is 1"} +{"id": "02676", "text": "$Alien$ likes to give challenges to $Codezen$ team to check their implementation ability, this time he has given a string $S$ and asked to find out whether the given string is a $GOOD$ string or not.\nAccording to the challenge $GOOD$ String is a string that has at least $3$ consecutive vowels and at least $5$ different consonants.\nFor example $ACQUAINTANCES$ is a $ GOOD $String ,as it contains $3$ consecutive vowels $UAI$ and $5$ different consonants $ {C, Q, N, T, S} $\nbut $ACQUAINT $ is not ,as it contain $3$ vowels but have only $ 4$ different consonants \n${C, Q, N, T}$.\n\n-----Input:-----\nA string input is given from Standard Input that consists of only capital letters. \n\n-----Output:-----\nOutput a single line that is either $GOOD$ (if string is a good string ) or $-1$(if string is not good).\n\n-----Constraints-----\n- $8 \\leq S \\leq 1000$\n\n-----Sample Input 1:-----\nAMBITIOUSNESS\n\n-----Sample Output 2:-----\nGOOD\n\n-----Sample Input 2:-----\nCOOEY\n\n-----Sample Output 2:-----\n-1"} +{"id": "02677", "text": "Zonal Computing Olympiad 2015, 29 Nov 2014\n\nAn interval is a pair of positive integers [a, b] with a \u2264 b. It is meant to denote the set of integers that lie between the values a and b. For example [3,5] denotes the set {3,4,5} while the interval [3, 3] denotes the set {3}.\n\nWe say that an interval [a, b] is covered by an integer i, if i belongs to the set defined by [a, b]. For example interval [3, 5] is covered by 3 and so is the interval [3, 3].\n\nGiven a set of intervals I, and a set of integers S we say that I is covered by S if for each interval [a, b] in I there is an integer i in S such that [a, b] is covered by i. For example, the set {[3, 5], [3, 3]} is covered by the set {3}. The set of intervals {[6, 9], [3, 5], [4, 8]} is covered by the set {4, 5, 8}. It is also covered by the set {4, 7}.\n\nWe would like to compute, for any set of intervals I, the size of the smallest set S that covers it. You can check that for the set of intervals {[6, 9], [3, 5], [4, 8]} the answer is 2 while for the set of intervals {[3, 5], [3, 3]} the answer is 1.\n\nYour program should take the set of intervals as input and output the size of the smallest set that covers it as the answer.\n\n-----Input format-----\nThe first line contains a single integer N, giving the number of intervals in the input.\n\nThis is followed by N lines, each containing two integers separated by a space describing an interval, with the first integer guaranteed to be less than or equal to the second integer.\n\n-----Output format-----\nOutput a single integer giving the size of the smallest set of integers that covers the given set of intervals.\n\n-----Test data-----\nYou may assume that all integers in the input are in the range 1 to 10^8 inclusive.\n\nSubtask 1 (100 marks) : 1 \u2264 N \u2264 5000.\n\n-----Sample Input 1-----\n2 \n3 5 \n3 3\n\n-----Sample Output 1-----\n1\n\n-----Sample Input 2-----\n3 \n6 9 \n3 5 \n4 8\n\n-----Sample Output 2-----\n2"} +{"id": "02678", "text": "It is the last day of covid and you are coming back to your home with two empty sacks. On\nthe way to your home, you found Ambani brothers donating all their wealth in form of coins\nafter getting fed up with all the money they have. The coins have both positive and negative\nvalues and they are laid down in a line. Now as you don\u2019t want to go with an empty sack so\nyou also decided to take some of the money being given by Ambani\u2019s. But anyone who\nwants the money has to follow a rule that he/she can choose an only contiguous segment of\ncoins which are lying in a line (you can also imagine it like an array of coins) whether its value\nis positive or negative.\nFormally according to the rule if you are given an array [-1,2,3,4,2,0] you can only choose\nsubarrays like [2,3], [4,2,0], [-1] , [-1,2,3,4,2,0] etc.\nNow as you have two siblings you decided to fill both the sacks with some coins. Your task is\nto choose the coins in such a way that the sum of values of all the coins(including sacks 1\nand 2) is maximum and both sacks are non-empty.\nFor e.g.:\nIf the array of coins is:\n-1 2 3 4 -3 -5 6 7 1\nFollowing are some ways you can choose the coins:\n2 3 4 and -5 6 7\n-1 2 3 and 6 7 1\n2 3 4 and 6 7 1\nand so on\u2026.\nYou can see that among the given ways the 3rd way will yield maximum values (2+3+4) +\n(6+7+1)=23\nNote: \nBoth the sack should be non-empty.\nA subarray is a contiguous part of an array. An array that is inside another array.\nA coin can be inserted in one and only one sacks i.e you cannot choose a coin more than one time.\n\n-----Input:-----\n- The first line contains $n$, the number of coins in the array $c$.\n- The next line will contain $n$ space integers denoting the value $coin$ $c[i]$.\n\n-----Output:-----\nOne and the only line containing the maximum possible sum of values of all the coins in\nboth sacks.\n\n-----Constraints-----\n- $2 \\leq n \\leq 10^5$\n- $-10^5 \\leq c[i] \\leq 10^5$\n\n-----Sample Input 1:-----\n9\n\n-1 2 3 4 -3 -5 6 7 1\n\n-----Sample Output 1:-----\n23\n\n-----Sample Input 2:-----\n6\n\n-10 -1 -30 -40 -3 -11\n\n-----Sample Output 2:-----\n-4\n\n-----EXPLANATION:-----\nIn first test case you can easily observe that the sum will be maximum if you choose [2,3,4]\nand [6,7,1] contiguous subarrays from the given array."} +{"id": "02679", "text": "Little Rohu from Pluto is the best bomb defusor on the entire planet. She has saved the planet from getting destruced multiple times now. Of course, she's proud of her achievments. But, to prepare herself for more complicated situations, she keeps challenging herself to handle tricky tasks.\nHer current challenge involves the following things:\n- A square matrix of the form N*N. \n- B bombs planted at various cells of the square matrix.\n- There can be multiple bombs at a given cell. \n- Only the four corners of the square matrix have a bomb defusor, so if a bomb is moved to any one of the four corners, it will be defused.\n\nFrom a given cell, any bomb can be moved in all the four directions, North, South, East, West respectively.\n\nRohu is given the number of bombs and the dimensions of the square matrix she's going to be dealing with, you've to help her figure out the minimum number of moves required to defuse all the bombs.\n\nNote-1: She's not going to enter the matrix, she's just going to move the bombs from outside. \nNote-2: The matrix is 1-indexed. \n\n-----Input-----\n- The first line of each test case contains two integers N, denoting the dimension of the square matrix, and B denoting the number of bombs. This will be followed by two lines, where the first line will denote the x-coordinate of all the bombs, and the second line will denote the y-coordinate of all the bombs. x[0], y[0] will be the position of the first bomb, ... , x[B-1], y[B-1] will be the position of the Bth bomb.\n\n-----Output-----\n- Output a single line containing the minimum number of moves required.\n\n-----Subtasks-----\n- 1 \u2264 N, B \u2264 100000: 50 points\n\n- 1 \u2264 N, B \u2264 1000: 30 points\n\n- 1 \u2264 N, B \u2264 10: 20 points\n\n-----Constraints-----\n- 1 \u2264 N, B \u2264 100000\n\n-----Example 1-----\nInput:\n3 3\n1 2 3\n3 2 1\n\nOutput:\n2\n\n-----Example 2-----\nInput:\n2 1\n1\n1\n\nOutput:\n0\n\n-----Explanation-----\nExample case 1:Only the bomb at {2,2} needs to be moved in two steps.\nExample case 2:No bomb needs to be moved anywhere."} +{"id": "02680", "text": "Write a program to obtain 2 numbers $($ $A$ $and$ $B$ $)$ and an arithmetic operator $(C)$ and then design a $calculator$ depending upon the operator entered by the user.\nSo for example if C=\"+\", you have to sum the two numbers.\nIf C=\"-\", you have to subtract the two numbers.\nIf C=\" * \", you have to print the product.\nIf C=\" / \", you have to divide the two numbers.\n\n-----Input:-----\n- First line will contain the first number $A$.\n- Second line will contain the second number $B$.\n- Third line will contain the operator $C$, that is to be performed on A and B.\n\n-----Output:-----\nOutput a single line containing the answer, obtained by, performing the operator on the numbers. Your output will be considered to be correct if the difference between your output and the actual answer is not more than $10^{-6}$.\n\n-----Constraints-----\n- $-1000 \\leq A \\leq 1000$\n- $-1000 \\leq B \\leq 1000$ $and$ $B \\neq 0$\n- $C$ $can$ $only$ $be$ $one$ $of$ $these$ $4$ $operators$ {\" + \", \" - \", \" * \", \" / \"}\n\n-----Sample Input:-----\n8\n2\n/\n\n-----Sample Output:-----\n4.0\n\n-----Sample Input:-----\n5\n3\n+\n\n-----Sample Output:-----\n8"} +{"id": "02681", "text": "Mathison has bought a new deck of cards that contains 2N cards, numbered and ordered from 0 to 2N-1.\nMathison is getting bored and decides to learn the Dynamo shuffle (or Sybil cut) - a flashy manner to shuffle cards. Unfortunately, the Dynamo shuffle is a little too complicated so Mathison decides to create his own shuffle.\n\nThis newly invented shuffle is done in N steps. At step k (0 \u2264 k < N) the deck is divided into 2k equal-sized decks\nwhere each one contains cards that lie on consecutive positions.\nEach one of those decks is then reordered: all the cards that lie on even positions are placed first followed by all cards that lie on odd positions\n(the order is preserved in each one of the two subsequences and all positions are 0-based). Then all the decks are put back together (preserving the order of decks).\n\nMathison has spent hours practising the shuffle and he now believes that he has perfected his technique. However, Chef doesn't believe him yet so he asks Mathison to answer Q questions that given a deck of size 2N where i-th card is labelled i, find the position of the card labelled K in the final, shuffled deck.\n\n-----Input-----\nThe first line of the input file will contain one integer, Q, representing the number of Chef's questions.\nEach of the next Q lines will contain a pair of integers, N and K.\n\n-----Output-----\nThe output file will contain Q lines, each one representing the answer to one of Chef's questions.\n\n-----Constraints-----\n- 1 \u2264 Q \u2264 1000\n- 1 \u2264 N \u2264 64\n- 0 \u2264 K < 2N\n\n-----Subtaks-----\nSubtask #1 (30 points):\n\n- 1 \u2264 N \u2264 10\n\nSubtask #2 (30 points):\n\n- 1 \u2264 N \u2264 32\n\nSubtask #3 (40 points):\n\n- Original constraints\n\n-----Example-----\nInput:\n3\n3 4\n3 3\n3 2\n\nOutput:\n1\n6\n2\n\nInput:\n1\n64 11047805202224836936\n\nOutput:\n1337369305470044825\n\n-----Explanation-----\nIn all questions, we have N = 3. Therefore, we have a deck with 8 cards.\nThe shuffling is done in three steps:\n\nStep 0: We divide {0, 1, 2, 3, 4, 5, 6, 7} in 20 decks. We get only one deck.\nThe deck is reordered into {0, 2, 4, 6, 1, 3, 5, 7}.\n\nStep 1: We divide {0, 2, 4, 6, 1, 3, 5, 7} in 21 decks. We get two decks: {0, 2, 4, 6} and {1, 3, 5, 7}.\n{0, 2, 4, 6} is reordered into {0, 4, 2, 6} while {1, 3, 5, 7} is reordered into {1, 5, 3, 7}.\nWe get {0, 4, 2, 6, 1, 5, 3, 7} when we put the decks back together.\n\nStep 2: We divide {0, 4, 2, 6, 1, 5, 3, 7} in 22 decks. We get four decks: {0, 4}, {2, 6}, {1, 5} and {3, 7}.\nEach one of the four decks stays the same after it is reordered (as there are only two elements to reorder).\nWe get the final, shuffled deck: {0, 4, 2, 6, 1, 5, 3, 7}.\n\nThe card labelled 4 is on position 1.\nThe card labelled 3 is on position 6.\nThe card labelled 2 is on position 2."} +{"id": "02682", "text": "Tic-Tac-Toe used to be Chef's favourite game during his childhood. Reminiscing in his childhood memories, he decided to create his own \"Tic-Tac-Toe\", with rules being similar to the original Tic-Tac-Toe, other than the fact that it is played on an NxN board.\n\nThe game is played between two players taking turns. First player puts an 'X' in an empty cell of the board, then the second player puts an 'O' in some other free cell. If the first player has K continuous X's or the second player has K continuous O's in row, column or diagonal, then he wins.\n\nChef started playing this new \"Tic-Tac-Toe\" with his assistant, beginning as the first player himself (that is, Chef plays 'X's). Currently, the game is ongoign, and it's Chef's turn. However, he needs to leave soon to begin tonight's dinner preparations, and has time to play only one more move. If he can win in one move, output \"YES\", otherwise output \"NO\" (without quotes). It is guaranteed that no player has already completed the winning criterion before this turn, and that it's a valid \"Tic-Tac-Toe\" game.\n\n-----Input-----\nThe first line of input contains one integer T denoting the number of testcases. First line of each testcase contains two integers N and K, next N lines contains N characters each. Each character is either an 'X' denoting that the first player used this cell, an 'O' meaning that the second player used this cell, or a '.' (a period) representing a free cell.\n\n-----Output-----\nFor each testcase, output, in a single line, \"YES\" if Chef can win in one move, or \"NO\" otherwise.\n\n-----Constraints-----\n- 1 \u2264 T \u2264 100\n- 3 \u2264 N \u2264 20\n- 1 \u2264 K \u2264 N\n\n-----Example-----\nInput:\n3\n3 3\nXOX\nO.O\nXOX\n3 1\n...\n...\n...\n3 2\n...\n...\n...\n\nOutput:\nYES\nYES\nNO\n\nInput:\n1\n4 4\nXOXO\nOX..\nXO..\nOXOX\n\nOutput:\nYES\n\n-----Subtasks-----\n- Subtask 1: K = 1. Points - 10\n- Subtask 2: N = K = 3. Points - 30\n- Subtask 3: Original constraints. Points - 60\n\n-----Explanation-----\nTest #1:\n\nIn first testcase, put 'X' in (2, 2), in second we can put 'X' in any cell and win. \nTest #2:\n\nIf you put an 'X' in (3, 3), there will be four 'X's on the main diagonal (1, 1) - (2, 2) - (3, 3) - (4, 4)."} +{"id": "02683", "text": "As we all know, a palindrome is a word that equals its reverse. Here are some examples of palindromes: malayalam, gag, appa, amma.\nWe consider any sequence consisting of the letters of the English alphabet to be a word. So axxb,abbba and bbbccddx are words for our purpose. And aaabbaaa, abbba and bbb are examples of palindromes.\nBy a subword of a word, we mean a contiguous subsequence of the word. For example the subwords of the word abbba are a, b, ab, bb, ba, abb, bbb, bba, abbb, bbba and abbba.\nIn this task you will given a word and you must find the longest subword of this word that is also a palindrome.\nFor example if the given word is abbba then the answer is abbba. If the given word is abcbcabbacba then the answer is bcabbacb.\n\n-----Input:-----\nThe first line of the input contains a single integer $N$ indicating the length of the word. The following line contains a single word of length $N$, made up of the letters a,b,\u2026, z.\n\n-----Output:-----\nThe first line of the output must contain a single integer indicating the length of the longest subword of the given word that is a palindrome. The second line must contain a subword that is a palindrome and which of maximum length. If there is more than one subword palindrome of maximum length, it suffices to print out any one.\n\n-----Constraints:-----\n- $1 \\leq N \\leq 5000$. \n- You may assume that in $30 \\%$ of the inputs $1 \\leq N \\leq 300$.\n\n-----Sample Input 1:-----\n5\nabbba\n\n-----Sample Output 1:-----\n5\nabbba\n\n-----Sample Input 2:-----\n12\nabcbcabbacba\n\n-----Sample Output 2:-----\n8\nbcabbacb"} +{"id": "02684", "text": "You are given a string S of numbers. Dr. Dragoon has eaten up some numbers of the string. You decide to fix the string by putting the numbers 0 and 1 in the empty spaces. \nThe base cost for putting the \u20180\u2019 is x and putting \u20181\u2019 is y . The cost of putting a number is the base cost multiplied by the number of times you use the letter till that position (including the position ). The local mayor is happy with your work and commissions you for every number you put. \n\nMore specifically , if you put , say 0 in 5th position, the mayor will count the number of occurrences of 0 till 5th position and pay you the same amount. You wonder what the minimum cost for fixing the string is. Note \u2013 The answer can also be negative, they denote that you got an overall profit.\n\nInput Format: \n\nThe input consists of 2 lines. First-line consists of the string of numbers S. \u2018?\u2019 denotes an empty position. \n\nThe second line consists of 2 integers x,y, the base cost of 0 and 1. \n\nOutput Format: \n\nOutput 1 integer, the minimum cost of fixing the string.\n\nConstraints : \n\n1<=|S|<=100\n\n1<=x,y<=100 \n\nSample Input: \n\n501?1?\n\n6 5\n\nSample Output: \n\n6\n\nSample Input: \n\n1001?11?\n\n5 23\n\nSample Output: \n\n8\n\nExplanation : \n\nFill 0 in the first place, the net cost will be 6-2=4. In the second place fill, 1 the net cost will be 5-3=2 Hence total cost is 4+2=6. \n\nIn second test case we will fill both spaces with zero , filling first zero costs 1*5-3=2 and filling the other zero costs 2*5 - 4 =6 , so total being 8."} +{"id": "02685", "text": "Heroes in Indian movies are capable of superhuman feats. For example, they can jump between buildings, jump onto and from running trains, catch bullets with their hands and teeth and so on. A perceptive follower of such movies would have noticed that there are limits to what even the superheroes can do. For example, if the hero could directly jump to his ultimate destination, that would reduce the action sequence to nothing and thus make the movie quite boring. So he typically labours through a series of superhuman steps to reach his ultimate destination.\nIn this problem, our hero has to save his wife/mother/child/dog/\u2026 held captive by the nasty villain on the top floor of a tall building in the centre of Bombay/Bangkok/Kuala Lumpur/\u2026. Our hero is on top of a (different) building. In order to make the action \"interesting\" the director has decided that the hero can only jump between buildings that are \"close\" to each other. The director decides which pairs of buildings are close enough and which are not.\nGiven the list of buildings, the identity of the building where the hero begins his search, the identity of the building where the captive (wife/mother/child/dog\u2026) is held, and the set of pairs of buildings that the hero can jump across, your aim is determine whether it is possible for the hero to reach the captive. And, if he can reach the captive he would like to do so with minimum number of jumps.\nHere is an example. There are $5$ buildings, numbered $1,2,...,5$, the hero stands on building $1$ and the captive is on building $4$. The director has decided that buildings $1$ and $3$, $2$ and $3, 1$ and $2, 3$ and $5$ and $4$ and $5$ are close enough for the hero to jump across. The hero can save the captive by jumping from $1$ to $3$ and then from $3$ to $5$ and finally from $5$ to $4$. (Note that if $i$ and $j$ are close then the hero can jump from $i$ to $j$ as well as from $j$ to $i$.). In this example, the hero could have also reached $4$ by jumping from $1$ to $2, 2$ to $3, 3$ to $5$ and finally from $5$ to $4$. The first route uses $3$ jumps while the second one uses $4$ jumps. You can verify that $3$ jumps is the best possible.\nIf the director decides that the only pairs of buildings that are close enough are $1$ and $3$, $1$ and $2$ and $4$ and $5$, then the hero would not be able to reach building $4$ to save the captive.\n\n-----Input:-----\nThe first line of the input contains two integers $N$ and $M$. $N$ is the number of buildings: we assume that our buildings are numbered $1,2,...,N$. $M$ is the number of pairs of buildings that the director lists as being close enough to jump from one to the other. Each of the next $M$ lines, lines $2,...,M+1$, contains a pair of integers representing a pair of buildings that are close. Line $i+1$ contains integers $A_i$ and $B_i$, $1 \\leq A_i \\leq N$ and $1 \\leq B_i \\leq N$, indicating that buildings $A_i$ and $B_i$ are close enough. The last line, line $M+2$ contains a pair of integers $S$ and $T$, where $S$ is the building from which the Hero starts his search and $T$ is the building where the captive is held.\n\n-----Output:-----\nIf the hero cannot reach the captive print $0$. If the hero can reach the captive print out a single integer indicating the number of jumps in the shortest route (in terms of the number of jumps) to reach the captive.\n\n-----Constraints:-----\n- $1 \\leq N \\leq 3500$.\n- $1 \\leq M \\leq 1000000$.\n- In at least $50 \\%$ of the inputs $1 \\leq N \\leq 1000$ and $1 \\leq M \\leq 200000$.\n\n-----Sample Input 1:-----\n5 5\n1 3\n2 3\n1 2\n3 5\n4 5 \n1 4\n\n-----Sample Output 1:-----\n3 \n\n-----Sample Input 2:-----\n5 3\n1 3\n1 2\n4 5\n1 4\n\n-----Sample Output 2:-----\n0"} +{"id": "02686", "text": "Knights' tournaments were quite popular in the Middle Ages. A lot of boys were dreaming of becoming a knight, while a lot of girls were dreaming of marrying a knight on a white horse.\n\nIn this problem we consider one of these tournaments. \n\nLet's us call a tournament binary, if it runs according to the scheme described below:\n\n- Exactly N knights take part in the tournament, N=2K for some integer K > 0.\n\t\t\n- Each knight has a unique skill called strength, described as an integer from the interval [1, N].\n\t\t\n- Initially, all the knights are standing in a line, waiting for a battle. Since all their strengths are unique, each initial configuration can be described as a permutation of numbers from 1 to N.\n\t\t\n- There are exactly K rounds in the tournament, 2K - i + 1 knights take part in the i'th round. The K'th round is called the final.\n\t\t\n- The i'th round runs in the following way: for each positive integer j \u2264 2K - i happens a battle between a knight on the 2\u2219j'th position and a knight on the 2\u2219j+1'th position. The strongest of two continues his tournament, taking the j'th position on the next round, while the weakest of two is forced to leave.\n\t\t\n- The only knight, who has won K rounds, is the winner. The only knight, who has won K - 1 rounds, but lost the final, is the runner-up. \n\t\n\nAs you can see from the scheme, the winner is always the same, an initial configuration doesn't change anything. So, your task is to determine chances of each knight to appear in the final.\n\nFormally, for each knight you need to count the number of initial configurations, which will lead him to the final. Since the number can be extremly huge, you are asked to do all the calculations under modulo 109 + 9.\n\n-----Input-----\n\nThe first line contains the only integer K, denoting the number of rounds of the tournament.\n\n-----Output-----\nOutput should consist of 2K lines. The i'th line should contain the number of initial configurations, which lead the participant with strength equals to i to the final.\n\n-----Constraints-----\n1 \u2264 K < 20\n\n\n-----Examples-----\nInput:\n1\n\nOutput:\n2\n2\n\nInput:\n2\n\nOutput:\n0\n8\n16\n24\n\n-----Explanation-----\n\nIn the first example we have N=2 knights. Let's consider each initial configuration that could appear and simulate the tournament.\n\n(1, 2) -> (2)\n\n(2, 1) -> (2)\n\nIn the second example we have N=4 knights. Let's consider some initial configurations that could appear and simulate the tournament.\n\n(1, 2, 3, 4) -> (2, 4) -> (4)\n\n(3, 2, 4, 1) -> (3, 4) -> (4)\n\n(4, 1, 3, 2) -> (4, 3) -> (4)"} +{"id": "02687", "text": "We consider permutations of the numbers $1,..., N$ for some $N$. By permutation we mean a rearrangment of the number $1,...,N$. For example\n24517638245176382 \\quad 4 \\quad 5 \\quad 1 \\quad 7 \\quad 6 \\quad 3 \\quad 8\nis a permutation of $1,2,...,8$. Of course,\n12345678123456781 \\quad 2 \\quad 3 \\quad 4 \\quad 5 \\quad 6 \\quad 7 \\quad 8\nis also a permutation of $1,2,...,8$.\nWe can \"walk around\" a permutation in a interesting way and here is how it is done for the permutation above:\nStart at position $1$. At position $1$ we have $2$ and so we go to position $2$. Here we find $4$ and so we go to position $4$. Here we find $1$, which is a position that we have already visited. This completes the first part of our walk and we denote this walk by ($1$ $2$ $4$ $1$). Such a walk is called a cycle. An interesting property of such walks, that you may take for granted, is that the position we revisit will always be the one we started from!\nWe continue our walk by jumping to first unvisited position, in this case position $3$ and continue in the same manner. This time we find $5$ at position $3$ and so we go to position $5$ and find $7$ and we go to position $7$ and find $3$ and thus we get the cycle ($3$ $5$ $7$ $3$). Next we start at position $6$ and get ($6$ $6$), and finally we start at position $8$ and get the cycle ($8$ $8$). We have exhausted all the positions. Our walk through this permutation consists of $4$ cycles.\nOne can carry out this walk through any permutation and obtain a set of cycles as the result. Your task is to print out the cycles that result from walking through a given permutation.\n\n-----Input:-----\nThe first line of the input is a positive integer $N$ indicating the length of the permutation. The next line contains $N$ integers and is a permutation of $1,2,...,N$.\n\n-----Output:-----\nThe first line of the output must contain a single integer $k$ denoting the number of cycles in the permutation. Line $2$ should describe the first cycle, line $3$ the second cycle and so on and line $k+1$ should describe the $k^{th}$ cycle.\n\n-----Constraints:-----\n- $1 \\leq N \\leq 1000$.\n\n-----Sample input 1:-----\n8\n2 4 5 1 7 6 3 8\n\n-----Sample output 1:-----\n4\n1 2 4 1\n3 5 7 3\n6 6\n8 8 \n\n-----Sample input 2:-----\n8\n1 2 3 4 5 6 7 8\n\n-----Sample output 2:-----\n8\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n7 7\n8 8"} +{"id": "02688", "text": "$Riggi$ is a spy of $KBI$ and he is on a secret mission, he is spying an Underworld Don $Anit$.\n$Riggi$ is spying him since 5 years and he is not able to find any evidence against $Anit$. \n$KBI$ told $Riggi$ they will send him a code string in a special format that will inform him whether he has continue or return back from mission.\nAccording to $KBI$ firstly he has to find what is the original string and then he has to check string is palindrome or not .\nIf its a palindrome then he has to leave the mission and return back else continue spying $Anit$. \nRules to find original string :\n1:-If in Code string any integer(N) followed by a string of alphabets that starts with '+' and ends with '-' then he has to repeat that string N times , like 3+acg- = acgacgacg.\n2:-If there is no integer present before string then print the string is repeated 1 time.\nlike bc=bc.\nExample of conversion from Code string to original string : 2+ac-3+kb-j=acackbkbkbj \n\n-----Input:-----\n- Code string $S$ is taken as input .\n\n-----Output:-----\n- Print string $Continue$ if code string $S$ is not a palindrome else print $Return$ .\n\n-----Constraints-----\n- $1 \\leq S \\leq 1000$\n- $1 \\leq N \\leq 1000$\n\n-----Sample Input 1:-----\n3+xy-bb3+yx-\n\n-----Sample Output 1:-----\nReturn\n\n-----Sample Input 2:-----\n3+xy-bb3+xy-\n\n-----Sample Output 2:-----\nContinue\n\n-----EXPLANATION:-----\nSample 1:- original string will be xyxyxybbyxyxyx which is a palindrome hence print $Return$.\nSample 2:- original string will be xyxyxybbxyxyxy which is not a palindrome hence print \n$Continue$."} +{"id": "02689", "text": "Alisha has a string of length n. Each character is either 'a', 'b' or 'c'. She has to select two characters s[i] and s[j] such that s[i] != s[j] and i,j are valid indexes. She has to find the maximum value of the absolute difference between i and j i.e abs(i-j) .\nSince Alisha is busy with her Semester exams help her find the maximum distance where distance is the maximum value of absolute difference between i and j i.e abs(i-j) .\n\n-----Input:-----\n- The first and the only line contains the string s with each character either 'a', 'b', or 'c'. \n\n-----Output:-----\nPrint a single integer the maximum absolute difference between i and j. \n\n-----Constraints-----\n- $1 \\leq n \\leq 10^5$\n- s[i] can either be 'a', 'b' or 'c'.\n\n-----Subtasks-----\n- 40 points : $1 \\leq n \\leq 100$\n- 60 points : $1 \\leq n \\leq 10^5$\n\n-----Sample Input1:-----\naabcaaa\n\n-----Sample Output1:-----\n4\n\n-----Sample Input2:-----\naba\n\n-----Sample Output2:-----\n1"} +{"id": "02690", "text": "Tim buy a string of length N from his friend which consist of \u2018d\u2019 and \u2018u\u2019 letters only\n,but now Tim wants to sell the string at maximum cost. \nThe maximum cost of string is defined as the maximum length of a Substring (consecutive subsequence) consisting of equal letters.\nTim can change at most P characters of string (\u2018d\u2019 to \u2018u\u2019 and \u2018u\u2019 to \u2018d\u2019) you have to find out the maximum cost of string?\n\n-----Input:-----\n- First line will contain $N$ and $P$ .\n- Second line will contain the String $S$.\n\n-----Output:-----\nMaximum cost of string S.\n\n-----Constraints-----\n- $1 \\leq N\\leq 10^5$\n- $0 \\leq P \\leq N$\n\n-----Sample Input 1:-----\n4 2\n$duud$\n\n-----Sample Output 1:-----\n4\n\n-----Sample Input 2 :-----\n10 1\n$dduddudddu$\n\n-----Sample Output 2:-----\n6\n\n-----EXPLANATION:-----\nIn the first sample input , We can obtain both strings $dddd$ and $uuuu$ .\nIn the second sample, the optimal answer is obtained with the string $dddddudd$ or with the string $dduddddd$"} +{"id": "02691", "text": "Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.\nSince Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie! \nGiven $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.\n\n-----Input:-----\n- First line will contain $T$, number of testcases. Then the testcases follow. \n- Each testcase contains of two space separated integers $N, B$. \n\n-----Output:-----\nFor each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.\n\n-----Constraints-----\n- $1 \\leq T \\leq 1000$\n- $1 \\leq N \\leq 1000000000$\n- $2 \\leq B \\leq 1000000000$\n\n-----Sample Input 1:-----\n1\n3 2\n\n-----Sample Output 1:-----\n2\n\n-----Sample Input 2:-----\n1\n11 2\n\n-----Sample Output 2:-----\n6\n\n-----Explanation 2:-----\nChef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed!"} +{"id": "02692", "text": "This is a rather simple problem to describe. You will be given three numbers $S, P$ and $k$. Your task is to find if there are integers $n_1, n_2,...,n_k$ such that $n_1 + n_2 +...+ n_k = S$, $n_1 \\cdot n_2 \\cdot ... \\cdot n_k = P$. If such integers exist, print them out. If no such sequence of integers exist, then print \"NO\".\nFor example if $S=11, P=48$ and $k=3$ then $3, 4$ and $4$ is a solution. On the other hand, if $S=11, P=100$ and $k=3$, there is no solution and you should print \"NO\".\n\n-----Input:-----\nA single line with three integers $S, P$ and $k$.\n\n-----Output:-----\nA single word \"NO\" or a seqence of $k$ integers $n_1, n_2,..., n_k$ on a single line. (The $n_i$'s must add up to $S$ and their product must be $P$).\n\n-----Constraints:-----\n- $1 \\leq k \\leq 4$.\n- $1 \\leq S \\leq 1000$.\n- $1 \\leq P \\leq 1000$.\n\n-----Sample input 1:-----\n11 48 3\n\n-----Sample output 1:-----\n3 4 4 \n\n-----Sample input 2:-----\n11 100 3\n\n-----Sample output 2:-----\nNO"} +{"id": "02693", "text": "Zonal Computing Olympiad 2013, 10 Nov 2012\n\nSpaceman Spiff has crash landed on Planet Quorg. He has to reach his ship quickly. But the evil Yukbarfs have stolen many Death Ray Blasters and have placed them along the way. You'll have to help him out!\n\nSpaceman Spiff is initially at the top left corner (1,1) of a rectangular N \u00d7 M grid . He needs to reach the bottom right corner (N,M). He can only move down or right. He moves at the speed of 1 cell per second. He has to move every second\u2014that is, he cannot stop and wait at any cell.\n\nThere are K special cells that contain the Death Ray Blasters planted by the Yukbarfs. Each Blaster has a starting time t and a frequency f. It first fires at time t seconds, followed by another round at time t+f seconds, then at time t+2f seconds \u2026. When a Blaster fires, it simultaneously emits four pulses, one in each of the four directions: up, down, left and right. The pulses travel at 1 cell per second.\n\nSuppose a blaster is located at (x,y) with starting time t and frequency f. At time t seconds, it shoots its first set of pulses. The pulse travelling upwards will be at the cell (x,y-s) at time t+s seconds. At this time, the pulse travelling left will be at cell (x-s,y), the pulse travelling right will be at cell (x+s,y) and the pulse travelling down will be at cell (x,y+s). It will fire next at time t+f seconds. If a pulse crosses an edge of the grid, it disappears. Pulses do not affect each other if they meet. They continue along their original path. At any time, if Spaceman Spiff and a pulse are in the same cell, he dies. That is the only way pulses interact with Spaceman Spiff. Spaceman Spiff can also never be on a cell which has a blaster. Given these, you should find the least time (in seconds) in which Spaceman Spiff can reach his ship safely. \n\nAs an example consider a 4\u00d74 grid that has only one Blaster, at (3,2), with starting time 1 and frequency 3. In the grids below, S denotes Spaceman Spiff, B denotes the blaster and P denotes a pulse. The sequence of grids describes a successful attempt to reach his ship that takes 6 seconds.\nt=0 t=1 t=2 t=3 \nS . . . . S . . . . S . . P . S\n. . . . . . . . . P . . . . . .\n. B . . . P . . P B P . . B . P\n. . . . . . . . . P . . . . . .\n\nt=4 t=5 t=6\n. . . . . . . . . P . .\n. . . S . P . . . . . .\n. P . . P B P S . B . P\n. . . . . P . . . . . S\n\n-----Input format-----\nLine 1: Three space separated integers N, M and K, describing the number of rows and columns in the grid and the number of Blasters, respectively.\n\nLines 2 to K+1: These lines describe the K blasters. Each line has four space separated integers. The first two integers on the line denote the row and column where the Blaster is located, the third integer is its starting time, and the fourth integer is its frequency.\n\n-----Output format-----\nThe first line of output must either consist of the word YES, if Spaceman Spiff can reach his ship safely, or the word NO, if he cannot do so. If the output on the first line is YES then the second line should contain a single integer giving the least time, in seconds, that it takes him to reach his ship safely.\n\n-----Sample Input 1-----\n4 4 1\n3 2 1 3\n\n-----Sample Output 1-----\nYES\n6\n\n-----Sample Input 2-----\n5 5 2\n5 1 1 2\n4 4 1 2\n\n-----Sample Output 2-----\nYES\n8\n\n-----Test data-----\nIn all subtasks, you may assume that:\n- \n2 \u2264 N,M \u2264 2500. \n- \nAll the frequencies are guaranteed to be integers between 1 and 3000, inclusive.\n- \nAll the starting times are guaranteed to be integers between 0 and 3000, inclusive.\n- \nAll the coordinates of the Blasters are guaranteed to be valid cells in the N\u00d7M grid. No two Blasters will be on the same cell.\n\n- Subtask 1 (30 marks) : K = 1.\n- Subtask 2 (70 marks) : 1 \u2264 K \u2264 2500.\n\n-----Live evaluation data-----\n- Subtask 1: Testcases 0,1.\n- Subtask 2: Testcases 2,3,4,5."} +{"id": "02694", "text": "Gotham City is again under attack. This time Joker has given an open challenge to solve the following problem in order to save Gotham. Help Batman in protecting Gotham by solving the problem.\nYou are given two strings A and B composed of lowercase letters of Latin alphabet. B can be obtained by removing some characters from A(including 0). You are given an array containing any random permutation of numbers $1$ to $N$, where $N$ is the length of the string A. You have to remove the letters of A in the given order of indices. Note that after removing one letter, the indices of other letters do not change. Return the maximum number of indices you can remove in the given order such that B is still obtainable from A.\n\n-----Input:-----\n- First two lines contains the strings A and B respectively.\n- The third line contains a random permutation of numbers from $1$ to $N$ representing some order of indices of string A.\n\n-----Output:-----\nPrint a single number denoting the maximum number of indices you can remove in the given order such that B is still obtainable from A.\n\n-----Constraints-----\n- $1 \\leq |B| \\leq |A| \\leq 200000$\n\n-----Sample Input #1:-----\nxxyxxy\nxyy\n1 5 4 6 3 2\n\n-----Sample Output #1:-----\n3\n\n-----Sample Input #2:-----\njphokenixr\njoker\n2 9 3 7 8 1 6 5 4 10\n\n-----Sample Output #2:-----\n5\n\n-----EXPLANATION:-----\nFor the first example removing characters in order,\nxxyxxy => _xyxxy => _xyx_y => _xy__y.\nIf we remove any other element now then we can not obtain 'xxy'. So the ans is 3."} +{"id": "02695", "text": "Every year the professor selects the top two students from his class and takes them on a fun filled field trip, this year Ali and Zafar were selected.\nThe professor took them to a place where there was a never-ending queue of solid metal cubes. The professor could see $n$ cubes at a time. On the day before, he had painted the first $n$ cubes from the left with various colours.\nAli was allowed to choose any painted cube to initially stand on (say $X$). Zafar was asked to stand on the last painted cube (the $n^{th}$ cube) with the professor.\nThe two students were given an activity each:\n- Ali had to shout out the colour of the cube he is standing on.\n- Zafar had to paint the adjacent cube on the right of him with that colour.\nBoth Ali and Zafar had to take one step to the right each time they performed their activity.\nThe Professor jumped along with Zafar, and never left his side. Throughout the activity he was looking leftwards keeping an eye on Ali. They were given a gold star and were allowed to go home when all the cubes that the professor could see at the time were painted with the same color.\nAli wanted to choose a position furthest from the professor and also wanted the gold star.\nCan you help Ali to choose this position $X$?\nThe cubes are numbered from 1 starting from the first painted cube.\nThe colours are represented as integers.\n\n-----Input:-----\n- The first line contains one integer n, the number of cubes initially painted.\n- The second line contains n space-separated integers: $a_1$,\u2009$a_2$,\u2009$\\dots$,\u2009$a_n$, the colours the professor chose for the first $n$ cubes.\n\n-----Output:-----\nPrint a single line containing one integer $X$, the position Ali should initially stand on.\n\n-----Constraints-----\n- $1 \\leq n \\leq 10^{6}$\n- $0 \\leq a_i \\leq 999$\n\n-----Sample Input 1:-----\n4\n\n3 3 8 8 \n\n-----Sample Output 1:-----\n3\n\n-----Sample Input 2:-----\n4\n\n2 8 7 3\n\n-----Sample Output 2:-----\n4"} +{"id": "02696", "text": "A greek once sifted some numbers. And it did something wonderful!\n\n-----Input:-----\n- First line will contain an integer $N$\n\n-----Output:-----\nOutput in a single line answer to the problem.\n\n-----Constraints-----\n- $1 \\leq N \\leq 10^5$\n\n-----Sample Input 1:-----\n10\n\n-----Sample Output 1:-----\n4\n\n-----Sample Input 2:-----\n20\n\n-----Sample Output 2:-----\n8"} +{"id": "02697", "text": "Chef loves cooking. Also, he is very hygienic. Hence, Chef grows his own vegetables in his mini kitchen garden. He has $M$ lanes in which he grows vegetables. The $ith$ lane has $Ai$ vegetables growing in it.\n\nVegetables in $ith$ lane become edible on day $Di$ and day $Di+1$(that is, the vegetables are edible only on days $Di$ and $Di+1$,where $Di+1$ is the next day of $Di$). For Example, if $Di$ is 2, then vegetable is edible on day 2 and day 3 only. If not collected in these days, they cannot be cooked.\n\nAlso, Chef is a lone worker. Hence, with respect to his capacity, he can collect only $V$ vegetables per day.\nBut, Chef is a hard worker, and is willing to work every day, starting from day 1.\n\nChef is busy sowing the seeds. So, he assigns you the task to find out the maximum number of vegetables he can collect, provided that he collects optimally.\n\n-----Input:-----\n- First line contains of 2 space separated integers $M$ and $V$, number of lanes and capacity of Chef.\n- Next $M$ lines follow\n- Each $ith$ line contains 2 space separated integers, $Di$ and $Ai$, the day when the vegetable of the $ith$ lane become edible and the number of vegetables in that lane.\n\n-----Output:-----\nFor each testcase, output in a single integer, that is the maximum number of vegetables that can be cooked\n\n-----Constraints-----\n- $1 \\leq M,V \\leq 3000$\n- $1 \\leq Di,Ai \\leq 3000$\n\n-----Subtasks-----\n- 20 points : $1 \\leq M \\leq 100$\n- 30 points : $1 \\leq M \\leq 1000$\n- 50 points : $Original Constraints$\n\n-----Sample Input 1:-----\n2 4\n1 6\n2 5\n\n-----Sample Output 1:-----\n11\n\n-----Sample Input 2:-----\n3 3\n1 4\n6 2\n5 3\n\n-----Sample Output 2:-----\n9\n\n-----EXPLANATION:-----\nIn the first sample, in order to obtain the optimal answer, you should do the following:-\n- On day 1, collect 4 vegetables from lane 1\n- On day 2, collect 2 vegetables from lane 1 and 2 vegetables from lane 2\n- On day 3, collect 3 vegetables from lane 2\nIn the first sample, in order to obtain the optimal answer, you should do the following:-\n- On day 1, collect 3 vegetables from lane 1\n- On day 2, collect 1 vegetable from lane 1\n- On day 5, collect 3 vegetables from lane 3\n- On day 6, collect 2 vegetables from lane 2"} +{"id": "02698", "text": "Rashmi loves the festival of Diwali as she gets to spend time with family and enjoy the festival. Before she can fully enjoy the festival she needs to complete the homework assigned by her teacher. Since Rashmi is smart , she has solved all the problems but is struck at one tricky pattern question.\nYour Task is to help Rashmi solve the problem so that she can enjoy the festival with her family.\nThe Problem she is struck on is defined like this:\nGiven an integer N you need to generate the pattern according to following example:\nExample:\n\nInput:\n\n3 \nOutput:\n\n1 4 10\n\n2 5 11\n\n4 10 22\n\n3 6 12 \n\n-----Input:-----\nThe first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.\nThe next line of each contains T space separated integers N.\n\n-----Output:-----\nFor each N print the required pattern.\n\n-----Constraints:-----\n$1 \\leq T \\leq 10^5$\n$1 \\leq N \\leq 30$\n\n-----Sample Input:-----\n2\n3 5\n\n-----Sample Output:-----\n1 4 10\n\n2 5 11\n\n4 10 22\n\n3 6 12\n\n1 4 10 22 46\n\n2 5 11 23 47\n\n4 10 22 46 94\n\n3 6 12 24 48 \n\n-----Sample Input:-----\n1\n\n4 \n\n-----Sample Output:-----\n1 4 10 22\n\n2 5 11 23\n\n4 10 22 46\n\n3 6 12 24"} +{"id": "02699", "text": "Chef likes inequalities. Please help him to solve next one.\nGiven four integers a, b, c, d. Find number of solutions x < y, where a \u2264 x \u2264 b and c \u2264 y \u2264 d and x, y integers.\n\n-----Input-----\nThe first line contains an integer T denoting number of tests.\nFirst line of each test case contains four positive integer numbers a, b, c and d.\n\n-----Output-----\nFor each test case, output a single number each in separate line denoting number of integer solutions as asked in the problem.\n\n-----Constraints-----\n- 1 \u2264 T \u2264 20 \n- 1 \u2264 a, b, c, d \u2264 106 \n\n-----Subtasks-----\n- Subtask #1: (30 points) 1 \u2264 a, b, c, d \u2264 103.\n- Subtask #2: (70 points) Original constraints.\n\n-----Example-----\nInput:1\n2 3 3 4\n\nOutput:3\n\nInput:1\n2 999999 1 1000000\n\nOutput:499998500001"} +{"id": "02700", "text": "In Chef's house there are N apples lying in a row on the floor. These apples are numbered from 1 (left most one) to N (right most one). The types of apples are also numbered by positive integers, and the type of apple i is Ti.\nChef has recently brought two trained dogs. Both of the dogs are too intelligent to know the smell of each type of apple. If Chef gives a dog an integer x, and releases it at one end of the row of apples, then the dog smells each apple one by one. Once the dog find an apple of type x, the dog picks the apple and back to Chef's room immidiately. If there is no apple of type x, then the dog will back without any apples.\nNow Chef wants to eat two apples as soon as possible. Here the apples must have distinct types, and the sum of the types must be equal to K. Chef can release the dogs from either of the ends, namely, he can leave (both at left end) or (both at right end) or (one at left end and one at right end) and he can release them at the same time. The dogs take one second to smell each apple. However the dogs can run rapidly, so the time for moving can be ignored. What is the minimum time (in seconds) to get the desired apples from his dogs?\n\n-----Input-----\nThe first line of input contains two space-separated integers N and K, denoting the number of apples and the required sum respectively. Then the next line contains N space-separated integers T1, T2, ..., TN, denoting the types of the apples.\n\n-----Output-----\nPrint one integer describing the minimum number of seconds that Chef needs to wait till he gets the desired apples. If Chef cannot get the desired apples, then output \"-1\" without quotes.\n\n-----Constraints-----\n- 2 \u2264 N \u2264 500000 (5 \u00d7 105)\n- 1 \u2264 K \u2264 1000000 (106)\n- 1 \u2264 Ti \u2264 1000000 (106)\n\n-----Example-----\nSample Input 1:\n5 5\n2 4 3 2 1\n\nSample Output 1:\n2\n\nSample Input 2:\n5 5\n2 4 9 2 5\n\nSample Output 2:\n-1\n\n-----Explanation-----\nIn the first example, if Chef leaves the first dog from left and gives it integer 4, and the second dog from right and gives it integer 1, then the first dog takes 2 seconds and the second dog takes 1 second to get the apples. Thus Chef needs to wait 2 seconds. In any other way, Chef can't get the desired apples in less than 2 seconds.\nIn the second example, Chef cannot get two apples such that the sum of their types is 5 so the answer is \"-1\"."} +{"id": "02701", "text": "There has been yet another murder in the Shady city of Riverdale. This murder is being investigated by none other than the Riverdale's Finest- Jughead Jones & Betty Cooper. This murder has been done exactly in the same manner as all the murders happening since the return of the deadly game Gargoyle &Griffins. Betty has decided to put an end to these murders, so they decide to interrogate each and every person in the neighbourhood.\nAs they don't know these people personally they want to first get to know about their character i.e whether a particular person is a Truth\u2212speaking\u2212person$Truth-speaking-person$ or a False\u2212speaking\u2212person$False-speaking-person$. Jughead devises a strategy of interrogating the civilians.\nJughead decides that they will collect statements from all the people in the neighbourhood about every other person living in the neighbourhood. Each person speaks a statement in form of an array consisting of T$T$ and F$ F$, which tells us what he thinks about the ith$ith$ person. Let there be N$N$ people living in the neighbourhood. So if a person i$i$ is giving his/her statement, he/her will always call himself/herself a True person i.e Statement[i]=T$Statement[i]=T$.\nSo Jughead says that they will select the maximum$maximum$ number$number$ of people that maybe speaking the truth such that their statements don't contradict and then interrogate them further about the murder.\nHelp them pick the max no. of Truth speaking people.\nNote$Note$- A person speaking falsely$falsely$ doesn't mean that the complement of his statement would be the truth$truth$. If a person is declared false$false$(i.e not included in the set) after Betty and Jughead pick their set of truth speaking people with non-contradicting statements, the person declared false might not be speaking falsely$falsely$ about anyone(except saying he himself is a true$true$ speaking person which contradicts with the selected statements) but since the selected Set of statements feels him to be a false$false$ speaking person he won't be included in the set. \nBut if a person is tagged as truth$truth$ speaking person then their Statement must be entirely correct and should not contradict with the chosen set of truth$truth$ speaking people. All truth$truth$ speaking people mentioned in the selected statements should be part of the set and all the people declared false$false$ in the statements shouldn't be part of the set.\nSee example for clarity.\n\n-----Input:-----\n- First line will contain N$N$, the number of people in the neighbourhood. Then the Statements of ith$ith$ person follow. \n- Each Statement contains a single line of input, an array of length N$N$ consisting of T$T$ and F$F$.\n\n-----Output:-----\nPrint a single line denoting the maximum no. of people that might be speaking the truth.\n\n-----Constraints-----\n- 1\u2264N\u22641000$1 \\leq N \\leq 1000$\n- Statement$Statement$i$i$[i]=T$[i]=T$\n\n-----Sample Input1:-----\n5\nT T F F F\nT T F F F\nT T T F F\nF F F T T\nF F F T T\n\n-----Sample Output1:-----\n2\n\n-----Sample Input2:-----\n3\nT T T\nT T T\nF F T\n\n-----Sample Output2:-----\n1\n\n-----EXPLANATION:-----\nIn Sample 1\nWe can consider the 1st$1st$ and 2nd$2nd$ person to be speaking the truth. We can also consider the 4th$4th$ and the 5th$5th$ person to be speaking the truth. \nNote$Note$: we cannot select 1st$1st$ 2nd$2nd$ and 3rd$3rd$ person because the 1st$1st$ and the second person have labelled the 3rd person as a false speaking person, therefore if we select all 3 contradiction will arise.\nIn sample 2\nSet contains only the 3rd person"} +{"id": "02702", "text": "Binod and his family live in Codingland. They have a festival called N-Halloween.\nThe festival is celebrated for N consecutive days. On each day Binod gets some candies from his mother. He may or may not take them. On a given day , Binod will be sad if he collected candies on that day and he does not have at least X candies remaining from the candies he collected that day at the end of that day . His friend Pelu who is very fond of candies asks for X candies from Binod on a day if Binod collects any candy on that day. He does so immediately after Binod collects them. Binod being a good friend never denies him. \n\nGiven a list a where ai denotes the number of candies Binod may take on the ith day and Q queries having a number X , find the maximum number of candies Binod can collect so that he won't be sad after the festival ends.\nInput format: \n\nFirst-line contains two integers N and Q denoting the number of festival days and number of queries respectively.\n\nThe second line is the array whose ith element is the maximum number of candies Binod may\ncollect on that day.\n\nEach of the next Q lines contains the number X i.e the minimum number of candies Binod wants to have at the end of every day on which he collects any candy.\nOutput format: \n\nFor every query output, a single integer denoting the max number of candies Binod may\ncollect (for the given X ) to be happy after the festival ends.\nConstraints: \n\n1<=N,Q<=105 \n\n1<=Q<=105 \n\n0<=ai<=109 \n\n0<=X<=109 \n\nSample Input : \n\n5 2\n\n4 6 5 8 7\n\n1\n\n2\n\nSample Output : \n\n30\n\n30\n\nSample Input: \n\n6 3 \n\n20 10 12 3 30 5 \n\n2 \n\n6 \n\n13 \n\nSample Output \n\n77 \n\n62 \n\n30 \n\nExplanation: \n\nIn the first query of sample input 1, Binod can collect the maximum number of given chocolates for a given day each day as after the end of every day Binod will have a number of collected\nchocolates on that day greater than equal to 1 even after giving 1 chocolate to Pelu. So the\nthe answer is 4+6+5+8+7=30."} +{"id": "02703", "text": "Devu has an array A consisting of N positive integers. He would like to perform following operation on array.\n\n- Pick some two elements a, b in the array (a could be same as b, but their corresponding indices in the array should not be same).\nRemove both the elements a and b and instead add a number x such that x lies between min(a, b) and max(a, b), both inclusive, (i.e. min(a, b) \u2264 x \u2264 max(a, b)).\n\nNow, as you know after applying the above operation N - 1 times, Devu will end up with a single number in the array. He is wondering whether it is possible to do the operations in such a way that he ends up a number t. \n\nHe asks your help in answering Q such queries, each of them will contain an integer t and you have to tell whether it is possible to end up t. \n\n-----Input-----\nThere is only one test case per test file.\nFirst line of the input contains two space separated integers N, Q denoting number of elements in A and number of queries for which Devu asks your help, respectively\nSecond line contains N space separated integers denoting the content of array A.\n\nEach of the next Q lines, will contain a single integer t corresponding to the query.\n\n-----Output-----\nOutput Q lines, each containing \"Yes\" or \"No\" (both without quotes) corresponding to the answer of corresponding query.\n\n-----Constraints-----\n- 1 \u2264 N, Q \u2264 105\n- 0 \u2264 t \u2264 109\n\n-----Subtasks-----\nSubtask #1 : 30 points\n- 1 \u2264 Ai \u2264 2\n\nSubtask #2 : 70 points\n- 1 \u2264 Ai \u2264 109\n\n-----Example-----\nInput 1:\n1 2\n1\n1\n2\n\nOutput:\nYes\nNo\n\nInput 2:\n2 4\n1 3\n1\n2\n3\n4\n\nOutput:\nYes\nYes\nYes\nNo\n\n-----Explanation-----\nIn the first example, Devu can't apply any operation. So the final element in the array will be 1 itself. \n\nIn the second example,\nDevu can replace 1 and 3 with any of the numbers among 1, 2, 3. Hence final element of the array could be 1, 2 or 3."} +{"id": "02704", "text": "Uppal-balu is busy in extraction of gold from ores. He got an assignment from his favorite professor MH sir, but he got stuck in a problem. Can you help Uppal-balu to solve the problem.\nYou are given an array $a_1$, $a_2$, $\\dots$, $a_n$ of length $n$. You can perform the following operations on it:\n- Choose an index $i$ ($1 \\leq i \\leq n$), and replace $a_i$ by $a_{i} + 1$ or $a_{i} - 1$, which means add or subtract one to the element at index $i$.\nBeauty of the array is defined as maximum length of a subarray containing numbers which give same remainder upon dividing it by $k$ (that is, ($a_l$ mod $k$) = ($a_{l+1}$ mod $k$) = $\\dots$ = ($a_r$ mod $k$) for some $1 \\leq l \\leq r \\leq n$).\nYou need to calculate the beauty of the array $a_1$, $a_2$, $\\dots$, $a_n$ after applying at most $m$ operations.\nNOTE:\n- The subarray of a is a contiguous part of the array $a$, i.\u2009e. the array $a_l$, $a_{l+1}$, $\\dots$, $a_r$ for some $1 \\leq l \\leq r \\leq n$.\n- $x$ mod $y$ means the remainder of $x$ after dividing it by $y$.\n\n-----Input:-----\n- First line of input contains 3 integers $n$, $m$ and $k$. \n- The second line contains $n$ integers $a_1$, $a_2$, $\\dots$, $a_n$.\n\n-----Output:-----\nOutput in a single line the beauty of the array after applying at most m operations.\n\n-----Constraints-----\n- $1 \\leq n,m \\leq 2\\cdot10^{5}$\n- $1 \\leq k \\leq 7$\n- $1 \\leq a_i \\leq 10^9$\n\n-----Sample Input 1:-----\n7 3 4\n\n8 2 3 7 8 1 1 \n\n-----Sample Output 1:-----\n5\n\n-----Sample Input 2:-----\n8 3 5\n\n7 2 1 3 6 5 6 2 \n\n-----Sample Output 2:-----\n5"} +{"id": "02705", "text": "Jack and Jill got into a serious fight. Jack didn't not belive Jill when she said that her memory was far better than the others. He asked her to prove it as follows :- \n\n- Jack would dictate a long list of numbers to her. At any point after he has dictated at least \"k\" numbers, he can ask her what is the k-th minimum number (the k-th number if the numbers are arranged in increasing order) in the list so far. \n- She succeeds in proving herself if she can tell the k-th minimum number every time he asks. \n- The number of queries (dictate a new number or ask her the k-th minimum of the numbers given till then) has to be pre-decided before he starts asking. \n\nJill succeeded in proving herself by convincing Jack quite a few times. Now, Jill remembered that Jack had claimed that he was good at programming. It's her turn to ask him to prove his skill. Jack doesn't want to lose to her and comes to you for help. He asks you to make the program for him. \n\n-----Input-----\nJill has agreed to let Jack decide the format of the input and here's what he has decided. First line of the input contains n, the number of queries. Next line contains k. Next n lines contain a query each, say q. If q >= 0 then the number is being 'dictated' to the system and needs to be remembered. If q = -1, then your program should return the k-th minimum number presented so far . It is given that 'q' can be -1 only if at least k numbers have already been given.\n\n-----Output-----\nFor every q = -1, you have to output the k-th minimum number given as input so far (not counting -1 as an input)\n\n-----Example-----\nInput1:\n6\n2\n3\n2\n-1\n-1\n1\n-1\n\nOutput1:\n3\n3\n2\n\nInput2:\n10\n5\n3\n6\n1\n4\n2\n9\n3\n1\n0\n-1\n\nOutput2:\n3"} +{"id": "02706", "text": "Given an array A of n non-negative integers. Find the number of ways to partition/divide the array into subarrays, such that mex in each subarray is not more than k. For example, mex of the arrays [1, 2] will be 0, and that of [0, 2] will be 1, and that of [0, 1, 2] will be 3. Due to the fact that the answer can turn out to be quite large, calculate it modulo 109\u2009+\u20097.\n\n-----Input-----\n- The first line of the input contains two integers n, k denoting the number of elements and limit of mex.\n- The second line contains n space-separated integers A1, A2, ... , An .\n\n-----Output-----\n- Output a single integer corresponding to the answer of the problem.\n\n-----Constraints-----\n- 1 \u2264 n \u2264 5 * 105\n- 0 \u2264 k, A[i] \u2264 109\n\n-----Example-----\nInput:\n3 1\n0 1 2\n\nOutput:\n2\n\nInput:\n10 3\n0 1 2 3 4 0 1 2 5 3\n\nOutput:\n379\n\n-----Explanation-----\nExample 1. The valid ways of partitioning will be [[0], [1, 2]] (mex of first subarray is 1, while that of the second is zero), and [[0], [1], [2]] (mex of first subarray is 1, and that of others is 0). There is no other way to partition the array such that mex is less than or equal to 1. For example, [[0, 1], [2]] is not a valid partitioning as mex of first subarray is 2 which is more than 1."} +{"id": "02707", "text": "Ayush is learning how to decrease a number by one, but he does it wrong with a number consisting of two or more digits. Ayush subtracts one from a number by the following algorithm:\nif the last digit of the number is non-zero, he decreases the number by one.\nif the last digit of the number is zero, he divides the number by 10.\nYou are given an integer number n\nAyush will subtract one from it k times. Your task is to print the result after all k subtractions.\nIt is guaranteed that the result will be a positive integer number.\nInput\nThe first line of the input contains two integers n and k (2 \u2264 n \u2264 10^9, 1 \u2264 k \u2264 50) -- the number from which Ayush will subtract and the number of subtractions respectively.\nOutput\nPrint one integer number \u2014 the result of the decreasing n by one k times.\nIt is guaranteed that the result will be a positive integer number.\nEXAMPLE\nSample Input 1\n512 4\nSample Output 1\n50\nSample Input 2\n1000000000 9\nSample Output 2\n1"} +{"id": "02708", "text": "In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th city consists of a_{i} soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.\n\nCheck if is it possible that after roaming there will be exactly b_{i} soldiers in the i-th city.\n\n\n-----Input-----\n\nFirst line of input consists of two integers n and m (1 \u2264 n \u2264 100, 0 \u2264 m \u2264 200).\n\nNext line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 100).\n\nNext line contains n integers b_1, b_2, ..., b_{n} (0 \u2264 b_{i} \u2264 100).\n\nThen m lines follow, each of them consists of two integers p and q (1 \u2264 p, q \u2264 n, p \u2260 q) denoting that there is an undirected road between cities p and q. \n\nIt is guaranteed that there is at most one road between each pair of cities.\n\n\n-----Output-----\n\nIf the conditions can not be met output single word \"NO\".\n\nOtherwise output word \"YES\" and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i \u2260 j) or how many soldiers should stay in city i (if i = j).\n\nIf there are several possible answers you may output any of them.\n\n\n-----Examples-----\nInput\n4 4\n1 2 6 3\n3 5 3 1\n1 2\n2 3\n3 4\n4 2\n\nOutput\nYES\n1 0 0 0 \n2 0 0 0 \n0 5 1 0 \n0 0 2 1 \n\nInput\n2 0\n1 2\n2 1\n\nOutput\nNO"} +{"id": "02709", "text": "While exploring the old caves, researchers found a book, or more precisely, a stash of mixed pages from a book. Luckily, all of the original pages are present and each page contains its number. Therefore, the researchers can reconstruct the book.\n\nAfter taking a deeper look into the contents of these pages, linguists think that this may be some kind of dictionary. What's interesting is that this ancient civilization used an alphabet which is a subset of the English alphabet, however, the order of these letters in the alphabet is not like the one in the English language.\n\nGiven the contents of pages that researchers have found, your task is to reconstruct the alphabet of this ancient civilization using the provided pages from the dictionary.\n\n\n-----Input-----\n\nFirst-line contains two integers: $n$ and $k$ ($1 \\le n, k \\le 10^3$) \u2014 the number of pages that scientists have found and the number of words present at each page. Following $n$ groups contain a line with a single integer $p_i$ ($0 \\le n \\lt 10^3$) \u2014 the number of $i$-th page, as well as $k$ lines, each line containing one of the strings (up to $100$ characters) written on the page numbered $p_i$.\n\n\n-----Output-----\n\nOutput a string representing the reconstructed alphabet of this ancient civilization. If the book found is not a dictionary, output \"IMPOSSIBLE\" without quotes. In case there are multiple solutions, output any of them.\n\n\n-----Example-----\nInput\n3 3\n2\nb\nb\nbbac\n0\na\naca\nacba\n1\nab\nc\nccb\n\nOutput\nacb"} +{"id": "02710", "text": "Yura is tasked to build a closed fence in shape of an arbitrary non-degenerate simple quadrilateral. He's already got three straight fence segments with known lengths $a$, $b$, and $c$. Now he needs to find out some possible integer length $d$ of the fourth straight fence segment so that he can build the fence using these four segments. In other words, the fence should have a quadrilateral shape with side lengths equal to $a$, $b$, $c$, and $d$. Help Yura, find any possible length of the fourth side.\n\nA non-degenerate simple quadrilateral is such a quadrilateral that no three of its corners lie on the same line, and it does not cross itself.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$\u00a0\u2014 the number of test cases ($1 \\le t \\le 1000$). The next $t$ lines describe the test cases.\n\nEach line contains three integers $a$, $b$, and $c$\u00a0\u2014 the lengths of the three fence segments ($1 \\le a, b, c \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case print a single integer $d$\u00a0\u2014 the length of the fourth fence segment that is suitable for building the fence. If there are multiple answers, print any. We can show that an answer always exists.\n\n\n-----Example-----\nInput\n2\n1 2 3\n12 34 56\n\nOutput\n4\n42\n\n\n\n-----Note-----\n\nWe can build a quadrilateral with sides $1$, $2$, $3$, $4$.\n\nWe can build a quadrilateral with sides $12$, $34$, $56$, $42$."} +{"id": "02711", "text": "You are given an undirected unweighted graph consisting of $n$ vertices and $m$ edges.\n\nYou have to write a number on each vertex of the graph. Each number should be $1$, $2$ or $3$. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd.\n\nCalculate the number of possible ways to write numbers $1$, $2$ and $3$ on vertices so the graph becomes beautiful. Since this number may be large, print it modulo $998244353$.\n\nNote that you have to write exactly one number on each vertex.\n\nThe graph does not have any self-loops or multiple edges.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 3 \\cdot 10^5$) \u2014 the number of tests in the input.\n\nThe first line of each test contains two integers $n$ and $m$ ($1 \\le n \\le 3 \\cdot 10^5, 0 \\le m \\le 3 \\cdot 10^5$) \u2014 the number of vertices and the number of edges, respectively. Next $m$ lines describe edges: $i$-th line contains two integers $u_i$, $ v_i$ ($1 \\le u_i, v_i \\le n; u_i \\neq v_i$) \u2014 indices of vertices connected by $i$-th edge.\n\nIt is guaranteed that $\\sum\\limits_{i=1}^{t} n \\le 3 \\cdot 10^5$ and $\\sum\\limits_{i=1}^{t} m \\le 3 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test print one line, containing one integer \u2014 the number of possible ways to write numbers $1$, $2$, $3$ on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo $998244353$.\n\n\n-----Example-----\nInput\n2\n2 1\n1 2\n4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n\nOutput\n4\n0\n\n\n\n-----Note-----\n\nPossible ways to distribute numbers in the first test: the vertex $1$ should contain $1$, and $2$ should contain $2$; the vertex $1$ should contain $3$, and $2$ should contain $2$; the vertex $1$ should contain $2$, and $2$ should contain $1$; the vertex $1$ should contain $2$, and $2$ should contain $3$. \n\nIn the second test there is no way to distribute numbers."} +{"id": "02712", "text": "We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller.\n - Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.\nIt can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.\nYou are given an integer K. Find an integer sequence a_i such that the number of times we will perform the above operation is exactly K. It can be shown that there is always such a sequence under the constraints on input and output in this problem.\n\n-----Constraints-----\n - 0 \u2264 K \u2264 50 \\times 10^{16}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK\n\n-----Output-----\nPrint a solution in the following format:\nN\na_1 a_2 ... a_N\n\nHere, 2 \u2264 N \u2264 50 and 0 \u2264 a_i \u2264 10^{16} + 1000 must hold.\n\n-----Sample Input-----\n0\n\n-----Sample Output-----\n4\n3 3 3 3\n"} +{"id": "02713", "text": "Musicians of a popular band \"Flayer\" have announced that they are going to \"make their exit\" with a world tour. Of course, they will visit Berland as well.\n\nThere are n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i-th route can be used to go from city v_{i} to city u_{i} (and from u_{i} to v_{i}), and it costs w_{i} coins to use this route.\n\nEach city will be visited by \"Flayer\", and the cost of the concert ticket in i-th city is a_{i} coins.\n\nYou have friends in every city of Berland, and they, knowing about your programming skills, asked you to calculate the minimum possible number of coins they have to pay to visit the concert. For every city i you have to compute the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j \u2260 i).\n\nFormally, for every $i \\in [ 1, n ]$ you have to calculate $\\operatorname{min}_{j = 1} 2 d(i, j) + a_{j}$, where d(i, j) is the minimum number of coins you have to spend to travel from city i to city j. If there is no way to reach city j from city i, then we consider d(i, j) to be infinitely large.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (2 \u2264 n \u2264 2\u00b710^5, 1 \u2264 m \u2264 2\u00b710^5).\n\nThen m lines follow, i-th contains three integers v_{i}, u_{i} and w_{i} (1 \u2264 v_{i}, u_{i} \u2264 n, v_{i} \u2260 u_{i}, 1 \u2264 w_{i} \u2264 10^12) denoting i-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (v, u) neither extra (v, u) nor (u, v) present in input.\n\nThe next line contains n integers a_1, a_2, ... a_{k} (1 \u2264 a_{i} \u2264 10^12) \u2014 price to attend the concert in i-th city.\n\n\n-----Output-----\n\nPrint n integers. i-th of them must be equal to the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j \u2260 i).\n\n\n-----Examples-----\nInput\n4 2\n1 2 4\n2 3 7\n6 20 1 25\n\nOutput\n6 14 1 25 \n\nInput\n3 3\n1 2 1\n2 3 1\n1 3 1\n30 10 20\n\nOutput\n12 10 12"} +{"id": "02714", "text": "You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.\n\nExample 1:\n\n\nInput:\ns = \"barfoothefoobarman\",\nwords = [\"foo\",\"bar\"]\nOutput: [0,9]\nExplanation: Substrings starting at index 0 and 9 are \"barfoor\" and \"foobar\" respectively.\nThe output order does not matter, returning [9,0] is fine too.\n\n\nExample 2:\n\n\nInput:\ns = \"wordgoodstudentgoodword\",\nwords = [\"word\",\"student\"]\nOutput: []"} +{"id": "02715", "text": "Compare two version numbers version1 and version2.\nIf version1 > version2 return 1;\u00a0if version1 < version2 return -1;otherwise return 0.\n\nYou may assume that the version strings are non-empty and contain only digits and the . character.\nThe . character does not represent a decimal point and is used to separate number sequences.\nFor instance, 2.5 is not \"two and a half\" or \"half way to version three\", it is the fifth second-level revision of the second first-level revision.\n\nExample 1:\n\n\nInput: version1 = \"0.1\", version2 = \"1.1\"\nOutput: -1\n\nExample 2:\n\n\nInput: version1 = \"1.0.1\", version2 = \"1\"\nOutput: 1\n\nExample 3:\n\n\nInput: version1 = \"7.5.2.4\", version2 = \"7.5.3\"\nOutput: -1"} +{"id": "02716", "text": "Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.\n\nYour algorithm's runtime complexity must be in the order of O(log n).\n\nIf the target is not found in the array, return [-1, -1].\n\nExample 1:\n\n\nInput: nums = [5,7,7,8,8,10], target = 8\nOutput: [3,4]\n\nExample 2:\n\n\nInput: nums = [5,7,7,8,8,10], target = 6\nOutput: [-1,-1]"} +{"id": "02717", "text": "Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.\n\nA mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.\n\n\n\nExample:\n\n\nInput: \"23\"\nOutput: [\"ad\", \"ae\", \"af\", \"bd\", \"be\", \"bf\", \"cd\", \"ce\", \"cf\"].\n\n\nNote:\n\nAlthough the above answer is in lexicographical order, your answer could be in any order you want."} +{"id": "02718", "text": "We have a grid with H rows and W columns of squares.\nSnuke is painting these squares in colors 1, 2, ..., N.\nHere, the following conditions should be satisfied:\n - For each i (1 \u2264 i \u2264 N), there are exactly a_i squares painted in Color i. Here, a_1 + a_2 + ... + a_N = H W.\n - For each i (1 \u2264 i \u2264 N), the squares painted in Color i are 4-connected. That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i.\nFind a way to paint the squares so that the conditions are satisfied.\nIt can be shown that a solution always exists.\n\n-----Constraints-----\n - 1 \u2264 H, W \u2264 100\n - 1 \u2264 N \u2264 H W\n - a_i \u2265 1\n - a_1 + a_2 + ... + a_N = H W\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\nN\na_1 a_2 ... a_N\n\n-----Output-----\nPrint one way to paint the squares that satisfies the conditions.\nOutput in the following format:\nc_{1 1} ... c_{1 W}\n:\nc_{H 1} ... c_{H W}\n\nHere, c_{i j} is the color of the square at the i-th row from the top and j-th column from the left.\n\n-----Sample Input-----\n2 2\n3\n2 1 1\n\n-----Sample Output-----\n1 1\n2 3\n\nBelow is an example of an invalid solution:\n1 2\n3 1\n\nThis is because the squares painted in Color 1 are not 4-connected."} +{"id": "02719", "text": "Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.\n\n\n\nFor example, given n = 3, a solution set is:\n\n\n[\n \"((()))\",\n \"(()())\",\n \"(())()\",\n \"()(())\",\n \"()()()\"\n]"} +{"id": "02720", "text": "Given a collection of intervals, merge all overlapping intervals.\n\nExample 1:\n\n\nInput: [[1,3],[2,6],[8,10],[15,18]]\nOutput: [[1,6],[8,10],[15,18]]\nExplanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].\n\n\nExample 2:\n\n\nInput: [[1,4],[4,5]]\nOutput: [[1,5]]\nExplanation: Intervals [1,4] and [4,5] are considerred overlapping."} +{"id": "02721", "text": "Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates\u00a0where the candidate numbers sums to target.\n\nEach number in candidates\u00a0may only be used once in the combination.\n\nNote:\n\n\n All numbers (including target) will be positive integers.\n The solution set must not contain duplicate combinations.\n\n\nExample 1:\n\n\nInput: candidates =\u00a0[10,1,2,7,6,1,5], target =\u00a08,\nA solution set is:\n[\n [1, 7],\n [1, 2, 5],\n [2, 6],\n [1, 1, 6]\n]\n\n\nExample 2:\n\n\nInput: candidates =\u00a0[2,5,2,1,2], target =\u00a05,\nA solution set is:\n[\n\u00a0 [1,2,2],\n\u00a0 [5]\n]"} +{"id": "02722", "text": "Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).\n\nYou may assume that the intervals were initially sorted according to their start times.\n\nExample 1:\n\n\nInput: intervals = [[1,3],[6,9]], newInterval = [2,5]\nOutput: [[1,5],[6,9]]\n\n\nExample 2:\n\n\nInput: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]\nOutput: [[1,2],[3,10],[12,16]]\nExplanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10]."} +{"id": "02723", "text": "Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both voodoo and melee are unbalanced, while neither noon nor a is.\nYou are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.\n\n-----Constraints-----\n - 2 \u2266 |s| \u2266 10^5\n - s consists of lowercase letters.\n\n-----Partial Score-----\n - 200 points will be awarded for passing the test set satisfying 2 \u2266 N \u2266 100.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\ns\n\n-----Output-----\nIf there exists no unbalanced substring of s, print -1 -1.\nIf there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 \u2266 a < b \u2266 |s|), and print a b. If there exists more than one such substring, any of them will be accepted.\n\n-----Sample Input-----\nneeded\n\n-----Sample Output-----\n2 5\n\nThe string s_2 s_3 s_4 s_5 = eede is unbalanced. There are also other unbalanced substrings. For example, the output 2 6 will also be accepted."} +{"id": "02724", "text": "Ash like soup very much! So, on the Raksha Bandhan day, his sister gave him a soup maker as a gift. Soup maker in the ith hour will make volume Vi liters of soup and pours it in a bowl.\nEach hour, the Volume of soup in every bowl is reduced due to evaporation. More precisely, when the temperature on a given hour is Ti, the Volume of soup in every bowl will reduce its volume by Ti. If this would reduce the volume of soup to or below zero, Bowl gets empty. All bowls are independent of each other.\nNote that the Volume of soup in every bowl made in an hour i already lose part of its volume at the same hour. In an extreme case, this may mean that there is no soup left in the bowl at the end of a particular hour.\nYou are given the initial volumes of soup in bowls and the temperature on each hour. Determine the total volume of soup evaporated in each hour.\nInput\nThe first line contains a single integer N (1\u2009\u2264\u2009N\u2009\u2264\u200910^5) \u2014 the number of hours.\nThe second line contains N integers V 1,\u2009V 2,\u2009\u2026,\u2009V N (0\u2009\u2264\u2009V i\u2009\u2264\u200910^9), where V i is the initial volume of soup made in an hour i.\nThe third line contains N integers T 1,\u2009T 2,\u2009\u2026,\u2009T N (0\u2009\u2264\u2009T i\u2009\u2264\u200910^9), where T i is the temperature in an hour i.\nOutput\nOutput a single line with N integers, where the i-th integer represents the total volume of soup melted in an hour i.\nExamples\nInput\n3\n10 10 5\n5 7 2\nOutput\n5 12 4\nInput\n5\n30 25 20 15 10\n9 10 12 4 13\nOutput\n9 20 35 11 25\nNote\nIn the first sample, In the first hour, 10 liters of soup is prepared, which evaporates to the size of 5 at the same hour. In the second hour, another 10 liters of soup is made. Since it is a bit warmer than the hour before, the first bowl gets empty while the second bowl shrinks to 3. At the end of the second hour, only one bowl with 3 liters soup is left. In the third hour, another bowl with less volume of soup is made, but as the temperature dropped too, both bowls survive till the end of the hour."} +{"id": "02725", "text": "-----INOI 2017, Problem 2, Training-----\nAsh and his Pokemon Pikachu are going on a journey. Ash has planned his route\nfor the journey so that it passes through N cities, numbered 1, 2, \u2026, N, and in this order.\n\nWhen they set out, Pikachu has an initial strength of Sin as well as an experience\nvalue (XV) of 0. As they travel they may increase his strength and experience value\nin a manner to be described below.\n\nIn each city, Ash can choose either to train Pikachu or let Pikachu battle the\nGym-leader (but not both). The Gym-leader in ith city has experience E[i]. If\nPikachu enters a city i with strength S and decides to train, then this\nincreases his strength by the cube of the sum of the digits in his current\nstrength. For example, if he entered a city with a strength of 12, then\ntraining will increase his strength to 12 + (1+2)3 = 39. On the other hand,\nif he enters city i with strength S and battles the Gym-leader, then this\nincreases his experience value XV by S*E[i].\n\nAsh wants your help to find out the maximum XV that Pikachu can attain \nat the end of his journey.\n\n-----Input-----\n- The first line contains two space separated integers, N and Sin, which are the number of cities, and the initial strength, respectively.\n\n- The second line contains N space separated integers, which correspond to E[1], E[2],..., E[N].\n\n-----Output-----\n- A single integer which is the maximum XV that Pikachu can attain.\n\n-----Constraints-----\nFor all test cases you may assume that: \n\n- 1 \u2264 N \u2264 5000\n- 0 \u2264 Sin \u2264 109\n- 0 \u2264 E[i] \u2264 104\n\nSubtask 1: For 10% of the score,\n\n- N \u2264 20 and Sin = 1\n\nSubtask 2: For further 40% of the score,\n\n- E[i] = k for all i\ni.e. E[i] is some constant k, for all i\n\nSubtask 3: For further 50% of the score,\n\n- \nNo further constraints.\n\n\n-----Example-----\nInput 1:\n2 12\n5 10\n\nOutput 1:\n390\n\n-----Explanation 1-----\nSuppose Pikachu trains in the first city, his strength will increase to 39, because as explained above, 12 + (1+2)3 = 39. If he battles in the first city, his XV will increase by (12*5) = 60.\n\nIf Pikachu trains in the first city, and then battles in the second city, his XV will increase by 39*10 = 390. So, the final XV will be 0+390 = 390. You can check that you cannot do better than this. Hence the answer is 390.\n\nInput 2:\n4 1\n100 1 6 2\n\nOutput 2:\n120\n\n-----Explanation 2-----\nPikachu battles in the first city, trains in the second and third, and then battles in the fourth city. So his strength going into each of the four cities is (1, 1, 2, 10). And so he gains 1*100 XV from the first city and 10*2 XV from the fourth city. This gets him a total of 120 XV, and you can verify that nothing better can be done.\n\nNote:\nYou may download the problem statements PDF and test cases zipped files here: http://pd.codechef.com/com/inoi/problems.zip. Please feel free to use them during the contest. \nPassword for the PDF and test cases zip files: DPtrumpsGreedy2017"} +{"id": "02726", "text": "You are given an integer $x$ of $n$ digits $a_1, a_2, \\ldots, a_n$, which make up its decimal notation in order from left to right.\n\nAlso, you are given a positive integer $k < n$.\n\nLet's call integer $b_1, b_2, \\ldots, b_m$ beautiful if $b_i = b_{i+k}$ for each $i$, such that $1 \\leq i \\leq m - k$.\n\nYou need to find the smallest beautiful integer $y$, such that $y \\geq x$. \n\n\n-----Input-----\n\nThe first line of input contains two integers $n, k$ ($2 \\leq n \\leq 200\\,000, 1 \\leq k < n$): the number of digits in $x$ and $k$.\n\nThe next line of input contains $n$ digits $a_1, a_2, \\ldots, a_n$ ($a_1 \\neq 0$, $0 \\leq a_i \\leq 9$): digits of $x$.\n\n\n-----Output-----\n\nIn the first line print one integer $m$: the number of digits in $y$.\n\nIn the next line print $m$ digits $b_1, b_2, \\ldots, b_m$ ($b_1 \\neq 0$, $0 \\leq b_i \\leq 9$): digits of $y$.\n\n\n-----Examples-----\nInput\n3 2\n353\n\nOutput\n3\n353\n\nInput\n4 2\n1234\n\nOutput\n4\n1313"} +{"id": "02727", "text": "Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong excuse: he faced a space-time paradox! Space and time replaced each other.\n\nThe entire universe turned into an enormous clock face with three hands\u00a0\u2014 hour, minute, and second. Time froze, and clocks now show the time h hours, m minutes, s seconds.\n\nLast time Misha talked with the coordinator at t_1 o'clock, so now he stands on the number t_1 on the clock face. The contest should be ready by t_2 o'clock. In the terms of paradox it means that Misha has to go to number t_2 somehow. Note that he doesn't have to move forward only: in these circumstances time has no direction.\n\nClock hands are very long, and Misha cannot get round them. He also cannot step over as it leads to the collapse of space-time. That is, if hour clock points 12 and Misha stands at 11 then he cannot move to 1 along the top arc. He has to follow all the way round the clock center (of course, if there are no other hands on his way).\n\nGiven the hands' positions, t_1, and t_2, find if Misha can prepare the contest on time (or should we say on space?). That is, find if he can move from t_1 to t_2 by the clock face.\n\n\n-----Input-----\n\nFive integers h, m, s, t_1, t_2 (1 \u2264 h \u2264 12, 0 \u2264 m, s \u2264 59, 1 \u2264 t_1, t_2 \u2264 12, t_1 \u2260 t_2).\n\nMisha's position and the target time do not coincide with the position of any hand.\n\n\n-----Output-----\n\nPrint \"YES\" (quotes for clarity), if Misha can prepare the contest on time, and \"NO\" otherwise.\n\nYou can print each character either upper- or lowercase (\"YeS\" and \"yes\" are valid when the answer is \"YES\").\n\n\n-----Examples-----\nInput\n12 30 45 3 11\n\nOutput\nNO\n\nInput\n12 0 1 12 1\n\nOutput\nYES\n\nInput\n3 47 0 4 9\n\nOutput\nYES\n\n\n\n-----Note-----\n\nThe three examples are shown on the pictures below from left to right. The starting position of Misha is shown with green, the ending position is shown with pink. Note that the positions of the hands on the pictures are not exact, but are close to the exact and the answer is the same. $\\oplus 0 \\theta$"} +{"id": "02728", "text": "THE SxPLAY & KIV\u039b - \u6f02\u6d41 KIV\u039b & Nikki Simmons - Perspectives\n\nWith a new body, our idol Aroma White (or should we call her Kaori Minamiya?) begins to uncover her lost past through the OS space.\n\nThe space can be considered a 2D plane, with an infinite number of data nodes, indexed from $0$, with their coordinates defined as follows: The coordinates of the $0$-th node is $(x_0, y_0)$ For $i > 0$, the coordinates of $i$-th node is $(a_x \\cdot x_{i-1} + b_x, a_y \\cdot y_{i-1} + b_y)$ \n\nInitially Aroma stands at the point $(x_s, y_s)$. She can stay in OS space for at most $t$ seconds, because after this time she has to warp back to the real world. She doesn't need to return to the entry point $(x_s, y_s)$ to warp home.\n\nWhile within the OS space, Aroma can do the following actions: From the point $(x, y)$, Aroma can move to one of the following points: $(x-1, y)$, $(x+1, y)$, $(x, y-1)$ or $(x, y+1)$. This action requires $1$ second. If there is a data node at where Aroma is staying, she can collect it. We can assume this action costs $0$ seconds. Of course, each data node can be collected at most once. \n\nAroma wants to collect as many data as possible before warping back. Can you help her in calculating the maximum number of data nodes she could collect within $t$ seconds?\n\n\n-----Input-----\n\nThe first line contains integers $x_0$, $y_0$, $a_x$, $a_y$, $b_x$, $b_y$ ($1 \\leq x_0, y_0 \\leq 10^{16}$, $2 \\leq a_x, a_y \\leq 100$, $0 \\leq b_x, b_y \\leq 10^{16}$), which define the coordinates of the data nodes.\n\nThe second line contains integers $x_s$, $y_s$, $t$ ($1 \\leq x_s, y_s, t \\leq 10^{16}$)\u00a0\u2013 the initial Aroma's coordinates and the amount of time available.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum number of data nodes Aroma can collect within $t$ seconds.\n\n\n-----Examples-----\nInput\n1 1 2 3 1 0\n2 4 20\n\nOutput\n3\nInput\n1 1 2 3 1 0\n15 27 26\n\nOutput\n2\nInput\n1 1 2 3 1 0\n2 2 1\n\nOutput\n0\n\n\n-----Note-----\n\nIn all three examples, the coordinates of the first $5$ data nodes are $(1, 1)$, $(3, 3)$, $(7, 9)$, $(15, 27)$ and $(31, 81)$ (remember that nodes are numbered from $0$).\n\nIn the first example, the optimal route to collect $3$ nodes is as follows: Go to the coordinates $(3, 3)$ and collect the $1$-st node. This takes $|3 - 2| + |3 - 4| = 2$ seconds. Go to the coordinates $(1, 1)$ and collect the $0$-th node. This takes $|1 - 3| + |1 - 3| = 4$ seconds. Go to the coordinates $(7, 9)$ and collect the $2$-nd node. This takes $|7 - 1| + |9 - 1| = 14$ seconds. \n\nIn the second example, the optimal route to collect $2$ nodes is as follows: Collect the $3$-rd node. This requires no seconds. Go to the coordinates $(7, 9)$ and collect the $2$-th node. This takes $|15 - 7| + |27 - 9| = 26$ seconds. \n\nIn the third example, Aroma can't collect any nodes. She should have taken proper rest instead of rushing into the OS space like that."} +{"id": "02729", "text": "Firecrackers scare Nian the monster, but they're wayyyyy too noisy! Maybe fireworks make a nice complement.\n\nLittle Tommy is watching a firework show. As circular shapes spread across the sky, a splendid view unfolds on the night of Lunar New Year's eve.\n\nA wonder strikes Tommy. How many regions are formed by the circles on the sky? We consider the sky as a flat plane. A region is a connected part of the plane with positive area, whose bound consists of parts of bounds of the circles and is a curve or several curves without self-intersections, and that does not contain any curve other than its boundaries. Note that exactly one of the regions extends infinitely.\n\n\n-----Input-----\n\nThe first line of input contains one integer n (1 \u2264 n \u2264 3), denoting the number of circles.\n\nThe following n lines each contains three space-separated integers x, y and r ( - 10 \u2264 x, y \u2264 10, 1 \u2264 r \u2264 10), describing a circle whose center is (x, y) and the radius is r. No two circles have the same x, y and r at the same time.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of regions on the plane.\n\n\n-----Examples-----\nInput\n3\n0 0 1\n2 0 1\n4 0 1\n\nOutput\n4\n\nInput\n3\n0 0 2\n3 0 2\n6 0 2\n\nOutput\n6\n\nInput\n3\n0 0 2\n2 0 2\n1 1 2\n\nOutput\n8\n\n\n\n-----Note-----\n\nFor the first example, $000$ \n\nFor the second example, [Image] \n\nFor the third example, $\\text{Q)}$"} +{"id": "02730", "text": "You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coordinate axes. Find whether the two squares intersect.\n\nThe interior of the square is considered to be part of the square, i.e. if one square is completely inside another, they intersect. If the two squares only share one common point, they are also considered to intersect.\n\n\n-----Input-----\n\nThe input data consists of two lines, one for each square, both containing 4 pairs of integers. Each pair represents coordinates of one vertex of the square. Coordinates within each line are either in clockwise or counterclockwise order.\n\nThe first line contains the coordinates of the square with sides parallel to the coordinate axes, the second line contains the coordinates of the square at 45 degrees.\n\nAll the values are integer and between $-100$ and $100$.\n\n\n-----Output-----\n\nPrint \"Yes\" if squares intersect, otherwise print \"No\".\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n0 0 6 0 6 6 0 6\n1 3 3 5 5 3 3 1\n\nOutput\nYES\n\nInput\n0 0 6 0 6 6 0 6\n7 3 9 5 11 3 9 1\n\nOutput\nNO\n\nInput\n6 0 6 6 0 6 0 0\n7 4 4 7 7 10 10 7\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example the second square lies entirely within the first square, so they do intersect.\n\nIn the second sample squares do not have any points in common.\n\nHere are images corresponding to the samples: [Image] [Image] [Image]"} +{"id": "02731", "text": "Tokitsukaze and CSL are playing a little game of stones.\n\nIn the beginning, there are $n$ piles of stones, the $i$-th pile of which has $a_i$ stones. The two players take turns making moves. Tokitsukaze moves first. On each turn the player chooses a nonempty pile and removes exactly one stone from the pile. A player loses if all of the piles are empty before his turn, or if after removing the stone, two piles (possibly empty) contain the same number of stones. Supposing that both players play optimally, who will win the game?\n\nConsider an example: $n=3$ and sizes of piles are $a_1=2$, $a_2=3$, $a_3=0$. It is impossible to choose the empty pile, so Tokitsukaze has two choices: the first and the second piles. If she chooses the first pile then the state will be $[1, 3, 0]$ and it is a good move. But if she chooses the second pile then the state will be $[2, 2, 0]$ and she immediately loses. So the only good move for her is to choose the first pile. \n\nSupposing that both players always take their best moves and never make mistakes, who will win the game?\n\nNote that even if there are two piles with the same number of stones at the beginning, Tokitsukaze may still be able to make a valid first move. It is only necessary that there are no two piles with the same number of stones after she moves.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the number of piles.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_1, a_2, \\ldots, a_n \\le 10^9$), which mean the $i$-th pile has $a_i$ stones.\n\n\n-----Output-----\n\nPrint \"sjfnb\" (without quotes) if Tokitsukaze will win, or \"cslnb\" (without quotes) if CSL will win. Note the output characters are case-sensitive.\n\n\n-----Examples-----\nInput\n1\n0\n\nOutput\ncslnb\n\nInput\n2\n1 0\n\nOutput\ncslnb\n\nInput\n2\n2 2\n\nOutput\nsjfnb\n\nInput\n3\n2 3 1\n\nOutput\nsjfnb\n\n\n\n-----Note-----\n\nIn the first example, Tokitsukaze cannot take any stone, so CSL will win.\n\nIn the second example, Tokitsukaze can only take a stone from the first pile, and then, even though they have no stone, these two piles will have the same number of stones, which implies CSL will win.\n\nIn the third example, Tokitsukaze will win. Here is one of the optimal ways:\n\n Firstly, Tokitsukaze can choose the first pile and take a stone from that pile. Then, CSL can only choose the first pile, because if he chooses the second pile, he will lose immediately. Finally, Tokitsukaze can choose the second pile, and then CSL will have no choice but to lose. \n\nIn the fourth example, they only have one good choice at any time, so Tokitsukaze can make the game lasting as long as possible and finally win."} +{"id": "02732", "text": "A new dog show on TV is starting next week. On the show dogs are required to demonstrate bottomless stomach, strategic thinking and self-preservation instinct. You and your dog are invited to compete with other participants and naturally you want to win!\n\nOn the show a dog needs to eat as many bowls of dog food as possible (bottomless stomach helps here). Dogs compete separately of each other and the rules are as follows:\n\nAt the start of the show the dog and the bowls are located on a line. The dog starts at position x = 0 and n bowls are located at positions x = 1, x = 2, ..., x = n. The bowls are numbered from 1 to n from left to right. After the show starts the dog immediately begins to run to the right to the first bowl.\n\nThe food inside bowls is not ready for eating at the start because it is too hot (dog's self-preservation instinct prevents eating). More formally, the dog can eat from the i-th bowl after t_{i} seconds from the start of the show or later.\n\nIt takes dog 1 second to move from the position x to the position x + 1. The dog is not allowed to move to the left, the dog runs only to the right with the constant speed 1 distance unit per second. When the dog reaches a bowl (say, the bowl i), the following cases are possible: the food had cooled down (i.e. it passed at least t_{i} seconds from the show start): the dog immediately eats the food and runs to the right without any stop, the food is hot (i.e. it passed less than t_{i} seconds from the show start): the dog has two options: to wait for the i-th bowl, eat the food and continue to run at the moment t_{i} or to skip the i-th bowl and continue to run to the right without any stop. \n\nAfter T seconds from the start the show ends. If the dog reaches a bowl of food at moment T the dog can not eat it. The show stops before T seconds if the dog had run to the right of the last bowl.\n\nYou need to help your dog create a strategy with which the maximum possible number of bowls of food will be eaten in T seconds.\n\n\n-----Input-----\n\nTwo integer numbers are given in the first line - n and T (1 \u2264 n \u2264 200 000, 1 \u2264 T \u2264 2\u00b710^9) \u2014 the number of bowls of food and the time when the dog is stopped.\n\nOn the next line numbers t_1, t_2, ..., t_{n} (1 \u2264 t_{i} \u2264 10^9) are given, where t_{i} is the moment of time when the i-th bowl of food is ready for eating.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the maximum number of bowls of food the dog will be able to eat in T seconds.\n\n\n-----Examples-----\nInput\n3 5\n1 5 3\n\nOutput\n2\n\nInput\n1 2\n1\n\nOutput\n1\n\nInput\n1 1\n1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example the dog should skip the second bowl to eat from the two bowls (the first and the third)."} +{"id": "02733", "text": "Suppose you have two polynomials $A(x) = \\sum_{k = 0}^{n} a_{k} x^{k}$ and $B(x) = \\sum_{k = 0}^{m} b_{k} x^{k}$. Then polynomial $A(x)$ can be uniquely represented in the following way:$A(x) = B(x) \\cdot D(x) + R(x), \\operatorname{deg} R(x) < \\operatorname{deg} B(x)$\n\nThis can be done using long division. Here, $\\operatorname{deg} P(x)$ denotes the degree of polynomial P(x). $R(x)$ is called the remainder of division of polynomial $A(x)$ by polynomial $B(x)$, it is also denoted as $A \\operatorname{mod} B$. \n\nSince there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials $(A, B)$. If the polynomial $B(x)$ is zero, the result is $A(x)$, otherwise the result is the value the algorithm returns for pair $(B, A \\operatorname{mod} B)$. On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question. \n\nYou are given an integer n. You have to build two polynomials with degrees not greater than n, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of x) are equal to one, and the described Euclid's algorithm performs exactly n steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair $(A, B)$ to pair $(B, A \\operatorname{mod} B)$. \n\n\n-----Input-----\n\nYou are given a single integer n (1 \u2264 n \u2264 150)\u00a0\u2014 the number of steps of the algorithm you need to reach.\n\n\n-----Output-----\n\nPrint two polynomials in the following format.\n\nIn the first line print a single integer m (0 \u2264 m \u2264 n)\u00a0\u2014 the degree of the polynomial. \n\nIn the second line print m + 1 integers between - 1 and 1\u00a0\u2014 the coefficients of the polynomial, from constant to leading. \n\nThe degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly n steps when called using these polynomials.\n\nIf there is no answer for the given n, print -1.\n\nIf there are multiple answer, print any of them.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n0 1\n0\n1\n\nInput\n2\n\nOutput\n2\n-1 0 1\n1\n0 1\n\n\n\n-----Note-----\n\nIn the second example you can print polynomials x^2 - 1 and x. The sequence of transitions is(x^2 - 1, x) \u2192 (x, - 1) \u2192 ( - 1, 0).\n\nThere are two steps in it."} +{"id": "02734", "text": "The elections to Berland parliament are happening today. Voting is in full swing!\n\nTotally there are n candidates, they are numbered from 1 to n. Based on election results k (1 \u2264 k \u2264 n) top candidates will take seats in the parliament.\n\nAfter the end of the voting the number of votes for each candidate is calculated. In the resulting table the candidates are ordered by the number of votes. In case of tie (equal number of votes) they are ordered by the time of the last vote given. The candidate with ealier last vote stands higher in the resulting table.\n\nSo in the resulting table candidates are sorted by the number of votes (more votes stand for the higher place) and if two candidates have equal number of votes they are sorted by the time of last vote (earlier last vote stands for the higher place).\n\nThere is no way for a candidate with zero votes to take a seat in the parliament. So it is possible that less than k candidates will take a seat in the parliament.\n\nIn Berland there are m citizens who can vote. Each of them will vote for some candidate. Each citizen will give a vote to exactly one of n candidates. There is no option \"against everyone\" on the elections. It is not accepted to spoil bulletins or not to go to elections. So each of m citizens will vote for exactly one of n candidates.\n\nAt the moment a citizens have voted already (1 \u2264 a \u2264 m). This is an open election, so for each citizen it is known the candidate for which the citizen has voted. Formally, the j-th citizen voted for the candidate g_{j}. The citizens who already voted are numbered in chronological order; i.e. the (j + 1)-th citizen voted after the j-th.\n\nThe remaining m - a citizens will vote before the end of elections, each of them will vote for one of n candidates.\n\nYour task is to determine for each of n candidates one of the three possible outcomes:\n\n a candidate will be elected to the parliament regardless of votes of the remaining m - a citizens; a candidate has chance to be elected to the parliament after all n citizens have voted; a candidate has no chances to be elected to the parliament regardless of votes of the remaining m - a citizens. \n\n\n-----Input-----\n\nThe first line contains four integers n, k, m and a (1 \u2264 k \u2264 n \u2264 100, 1 \u2264 m \u2264 100, 1 \u2264 a \u2264 m) \u2014 the number of candidates, the number of seats in the parliament, the number of Berland citizens and the number of citizens who already have voted.\n\nThe second line contains a sequence of a integers g_1, g_2, ..., g_{a} (1 \u2264 g_{j} \u2264 n), where g_{j} is the candidate for which the j-th citizen has voted. Citizens who already voted are numbered in increasing order of voting times.\n\n\n-----Output-----\n\nPrint the sequence consisting of n integers r_1, r_2, ..., r_{n} where:\n\n r_{i} = 1 means that the i-th candidate is guaranteed to take seat in the parliament regardless of votes of the remaining m - a citizens; r_{i} = 2 means that the i-th candidate has a chance to take a seat in the parliament, i.e. the remaining m - a citizens can vote in such a way that the candidate will take a seat in the parliament; r_{i} = 3 means that the i-th candidate will not take a seat in the parliament regardless of votes of the remaining m - a citizens. \n\n\n-----Examples-----\nInput\n3 1 5 4\n1 2 1 3\n\nOutput\n1 3 3 \nInput\n3 1 5 3\n1 3 1\n\nOutput\n2 3 2 \nInput\n3 2 5 3\n1 3 1\n\nOutput\n1 2 2"} +{"id": "02735", "text": "The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation.\n\nHe calls a number special if the minimum number of operations to reduce it to 1 is k.\n\nHe wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!\n\nSince the answer can be large, output it modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n < 2^1000).\n\nThe second line contains integer k (0 \u2264 k \u2264 1000).\n\nNote that n is given in its binary representation without any leading zeros.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the number of special numbers not greater than n, modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n110\n2\n\nOutput\n3\n\nInput\n111111011\n2\n\nOutput\n169\n\n\n\n-----Note-----\n\nIn the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2)."} +{"id": "02736", "text": "It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin.\n\nWe can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (x_{i}, y_{i}). Both Adil and Bera can carry only one bottle at once each. \n\nFor both Adil and Bera the process looks as follows: Choose to stop or to continue to collect bottles. If the choice was to continue then choose some bottle and walk towards it. Pick this bottle and walk to the recycling bin. Go to step 1. \n\nAdil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles.\n\nThey want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin.\n\n\n-----Input-----\n\nFirst line of the input contains six integers a_{x}, a_{y}, b_{x}, b_{y}, t_{x} and t_{y} (0 \u2264 a_{x}, a_{y}, b_{x}, b_{y}, t_{x}, t_{y} \u2264 10^9)\u00a0\u2014 initial positions of Adil, Bera and recycling bin respectively.\n\nThe second line contains a single integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of bottles on the ground.\n\nThen follow n lines, each of them contains two integers x_{i} and y_{i} (0 \u2264 x_{i}, y_{i} \u2264 10^9)\u00a0\u2014 position of the i-th bottle.\n\nIt's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct.\n\n\n-----Output-----\n\nPrint one real number\u00a0\u2014 the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}.\n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n3 1 1 2 0 0\n3\n1 1\n2 1\n2 3\n\nOutput\n11.084259940083\n\nInput\n5 0 4 2 2 0\n5\n5 2\n3 0\n5 5\n3 5\n3 3\n\nOutput\n33.121375178000\n\n\n\n-----Note-----\n\nConsider the first sample.\n\nAdil will use the following path: $(3,1) \\rightarrow(2,1) \\rightarrow(0,0) \\rightarrow(1,1) \\rightarrow(0,0)$.\n\nBera will use the following path: $(1,2) \\rightarrow(2,3) \\rightarrow(0,0)$.\n\nAdil's path will be $1 + \\sqrt{5} + \\sqrt{2} + \\sqrt{2}$ units long, while Bera's path will be $\\sqrt{2} + \\sqrt{13}$ units long."} +{"id": "02737", "text": "Tanechka is shopping in the toy shop. There are exactly $n$ toys in the shop for sale, the cost of the $i$-th toy is $i$ burles. She wants to choose two toys in such a way that their total cost is $k$ burles. How many ways to do that does she have?\n\nEach toy appears in the shop exactly once. Pairs $(a, b)$ and $(b, a)$ are considered equal. Pairs $(a, b)$, where $a=b$, are not allowed.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$, $k$ ($1 \\le n, k \\le 10^{14}$) \u2014 the number of toys and the expected total cost of the pair of toys.\n\n\n-----Output-----\n\nPrint the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is $k$ burles.\n\n\n-----Examples-----\nInput\n8 5\n\nOutput\n2\n\nInput\n8 15\n\nOutput\n1\n\nInput\n7 20\n\nOutput\n0\n\nInput\n1000000000000 1000000000001\n\nOutput\n500000000000\n\n\n\n-----Note-----\n\nIn the first example Tanechka can choose the pair of toys ($1, 4$) or the pair of toys ($2, 3$).\n\nIn the second example Tanechka can choose only the pair of toys ($7, 8$).\n\nIn the third example choosing any pair of toys will lead to the total cost less than $20$. So the answer is 0.\n\nIn the fourth example she can choose the following pairs: $(1, 1000000000000)$, $(2, 999999999999)$, $(3, 999999999998)$, ..., $(500000000000, 500000000001)$. The number of such pairs is exactly $500000000000$."} +{"id": "02738", "text": "You've got a string $a_1, a_2, \\dots, a_n$, consisting of zeros and ones.\n\nLet's call a sequence of consecutive elements $a_i, a_{i + 1}, \\ldots, a_j$ ($1\\leq i\\leq j\\leq n$) a substring of string $a$. \n\nYou can apply the following operations any number of times: Choose some substring of string $a$ (for example, you can choose entire string) and reverse it, paying $x$ coins for it (for example, \u00ab0101101\u00bb $\\to$ \u00ab0111001\u00bb); Choose some substring of string $a$ (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones\u00a0\u2014 by zeros), paying $y$ coins for it (for example, \u00ab0101101\u00bb $\\to$ \u00ab0110001\u00bb). \n\nYou can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.\n\nWhat is the minimum number of coins you need to spend to get a string consisting only of ones?\n\n\n-----Input-----\n\nThe first line of input contains integers $n$, $x$ and $y$ ($1 \\leq n \\leq 300\\,000, 0 \\leq x, y \\leq 10^9$)\u00a0\u2014 length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring).\n\nThe second line contains the string $a$ of length $n$, consisting of zeros and ones.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum total cost of operations you need to spend to get a string consisting only of ones. Print $0$, if you do not need to perform any operations.\n\n\n-----Examples-----\nInput\n5 1 10\n01000\n\nOutput\n11\n\nInput\n5 10 1\n01000\n\nOutput\n2\n\nInput\n7 2 3\n1111111\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, at first you need to reverse substring $[1 \\dots 2]$, and then you need to invert substring $[2 \\dots 5]$. \n\nThen the string was changed as follows:\n\n\u00ab01000\u00bb $\\to$ \u00ab10000\u00bb $\\to$ \u00ab11111\u00bb.\n\nThe total cost of operations is $1 + 10 = 11$.\n\nIn the second sample, at first you need to invert substring $[1 \\dots 1]$, and then you need to invert substring $[3 \\dots 5]$. \n\nThen the string was changed as follows:\n\n\u00ab01000\u00bb $\\to$ \u00ab11000\u00bb $\\to$ \u00ab11111\u00bb.\n\nThe overall cost is $1 + 1 = 2$.\n\nIn the third example, string already consists only of ones, so the answer is $0$."} +{"id": "02739", "text": "The well-known Fibonacci sequence $F_0, F_1, F_2,\\ldots $ is defined as follows: $F_0 = 0, F_1 = 1$. For each $i \\geq 2$: $F_i = F_{i - 1} + F_{i - 2}$. \n\nGiven an increasing arithmetic sequence of positive integers with $n$ elements: $(a, a + d, a + 2\\cdot d,\\ldots, a + (n - 1)\\cdot d)$.\n\nYou need to find another increasing arithmetic sequence of positive integers with $n$ elements $(b, b + e, b + 2\\cdot e,\\ldots, b + (n - 1)\\cdot e)$ such that: $0 < b, e < 2^{64}$, for all $0\\leq i < n$, the decimal representation of $a + i \\cdot d$ appears as substring in the last $18$ digits of the decimal representation of $F_{b + i \\cdot e}$ (if this number has less than $18$ digits, then we consider all its digits). \n\n\n-----Input-----\n\nThe first line contains three positive integers $n$, $a$, $d$ ($1 \\leq n, a, d, a + (n - 1) \\cdot d < 10^6$).\n\n\n-----Output-----\n\nIf no such arithmetic sequence exists, print $-1$.\n\nOtherwise, print two integers $b$ and $e$, separated by space in a single line ($0 < b, e < 2^{64}$).\n\nIf there are many answers, you can output any of them.\n\n\n-----Examples-----\nInput\n3 1 1\n\nOutput\n2 1\nInput\n5 1 2\n\nOutput\n19 5\n\n\n\n-----Note-----\n\nIn the first test case, we can choose $(b, e) = (2, 1)$, because $F_2 = 1, F_3 = 2, F_4 = 3$.\n\nIn the second test case, we can choose $(b, e) = (19, 5)$ because: $F_{19} = 4181$ contains $1$; $F_{24} = 46368$ contains $3$; $F_{29} = 514229$ contains $5$; $F_{34} = 5702887$ contains $7$; $F_{39} = 63245986$ contains $9$."} +{"id": "02740", "text": "The Holmes children are fighting over who amongst them is the cleverest.\n\nMycroft asked Sherlock and Eurus to find value of f(n), where f(1) = 1 and for n \u2265 2, f(n) is the number of distinct ordered positive integer pairs (x, y) that satisfy x + y = n and gcd(x, y) = 1. The integer gcd(a, b) is the greatest common divisor of a and b.\n\nSherlock said that solving this was child's play and asked Mycroft to instead get the value of $g(n) = \\sum_{d|n} f(n / d)$. Summation is done over all positive integers d that divide n.\n\nEurus was quietly observing all this and finally came up with her problem to astonish both Sherlock and Mycroft.\n\nShe defined a k-composite function F_{k}(n) recursively as follows:\n\n$F_{k}(n) = \\left\\{\\begin{array}{ll}{f(g(n)),} & {\\text{for} k = 1} \\\\{g(F_{k - 1}(n)),} & {\\text{for} k > 1 \\text{and} k \\operatorname{mod} 2 = 0} \\\\{f(F_{k - 1}(n)),} & {\\text{for} k > 1 \\text{and} k \\operatorname{mod} 2 = 1} \\end{array} \\right.$\n\nShe wants them to tell the value of F_{k}(n) modulo 1000000007.\n\n\n-----Input-----\n\nA single line of input contains two space separated integers n (1 \u2264 n \u2264 10^12) and k (1 \u2264 k \u2264 10^12) indicating that Eurus asks Sherlock and Mycroft to find the value of F_{k}(n) modulo 1000000007.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the value of F_{k}(n) modulo 1000000007.\n\n\n-----Examples-----\nInput\n7 1\n\nOutput\n6\nInput\n10 2\n\nOutput\n4\n\n\n-----Note-----\n\nIn the first case, there are 6 distinct ordered pairs (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1) satisfying x + y = 7 and gcd(x, y) = 1. Hence, f(7) = 6. So, F_1(7) = f(g(7)) = f(f(7) + f(1)) = f(6 + 1) = f(7) = 6."} +{"id": "02741", "text": "Berkomnadzor\u00a0\u2014 Federal Service for Supervision of Communications, Information Technology and Mass Media\u00a0\u2014 is a Berland federal executive body that protects ordinary residents of Berland from the threats of modern internet.\n\nBerkomnadzor maintains a list of prohibited IPv4 subnets (blacklist) and a list of allowed IPv4 subnets (whitelist). All Internet Service Providers (ISPs) in Berland must configure the network equipment to block access to all IPv4 addresses matching the blacklist. Also ISPs must provide access (that is, do not block) to all IPv4 addresses matching the whitelist. If an IPv4 address does not match either of those lists, it's up to the ISP to decide whether to block it or not. An IPv4 address matches the blacklist (whitelist) if and only if it matches some subnet from the blacklist (whitelist). An IPv4 address can belong to a whitelist and to a blacklist at the same time, this situation leads to a contradiction (see no solution case in the output description).\n\nAn IPv4 address is a 32-bit unsigned integer written in the form $a.b.c.d$, where each of the values $a,b,c,d$ is called an octet and is an integer from $0$ to $255$ written in decimal notation. For example, IPv4 address $192.168.0.1$ can be converted to a 32-bit number using the following expression $192 \\cdot 2^{24} + 168 \\cdot 2^{16} + 0 \\cdot 2^8 + 1 \\cdot 2^0$. First octet $a$ encodes the most significant (leftmost) $8$ bits, the octets $b$ and $c$\u00a0\u2014 the following blocks of $8$ bits (in this order), and the octet $d$ encodes the least significant (rightmost) $8$ bits.\n\nThe IPv4 network in Berland is slightly different from the rest of the world. There are no reserved or internal addresses in Berland and use all $2^{32}$ possible values.\n\nAn IPv4 subnet is represented either as $a.b.c.d$ or as $a.b.c.d/x$ (where $0 \\le x \\le 32$). A subnet $a.b.c.d$ contains a single address $a.b.c.d$. A subnet $a.b.c.d/x$ contains all IPv4 addresses with $x$ leftmost (most significant) bits equal to $x$ leftmost bits of the address $a.b.c.d$. It is required that $32 - x$ rightmost (least significant) bits of subnet $a.b.c.d/x$ are zeroes.\n\nNaturally it happens that all addresses matching subnet $a.b.c.d/x$ form a continuous range. The range starts with address $a.b.c.d$ (its rightmost $32 - x$ bits are zeroes). The range ends with address which $x$ leftmost bits equal to $x$ leftmost bits of address $a.b.c.d$, and its $32 - x$ rightmost bits are all ones. Subnet contains exactly $2^{32-x}$ addresses. Subnet $a.b.c.d/32$ contains exactly one address and can also be represented by just $a.b.c.d$.\n\nFor example subnet $192.168.0.0/24$ contains range of 256 addresses. $192.168.0.0$ is the first address of the range, and $192.168.0.255$ is the last one.\n\nBerkomnadzor's engineers have devised a plan to improve performance of Berland's global network. Instead of maintaining both whitelist and blacklist they want to build only a single optimised blacklist containing minimal number of subnets. The idea is to block all IPv4 addresses matching the optimised blacklist and allow all the rest addresses. Of course, IPv4 addresses from the old blacklist must remain blocked and all IPv4 addresses from the old whitelist must still be allowed. Those IPv4 addresses which matched neither the old blacklist nor the old whitelist may be either blocked or allowed regardless of their accessibility before. \n\nPlease write a program which takes blacklist and whitelist as input and produces optimised blacklist. The optimised blacklist must contain the minimal possible number of subnets and satisfy all IPv4 addresses accessibility requirements mentioned above. \n\nIPv4 subnets in the source lists may intersect arbitrarily. Please output a single number -1 if some IPv4 address matches both source whitelist and blacklist.\n\n\n-----Input-----\n\nThe first line of the input contains single integer $n$ ($1 \\le n \\le 2\\cdot10^5$) \u2014 total number of IPv4 subnets in the input.\n\nThe following $n$ lines contain IPv4 subnets. Each line starts with either '-' or '+' sign, which indicates if the subnet belongs to the blacklist or to the whitelist correspondingly. It is followed, without any spaces, by the IPv4 subnet in $a.b.c.d$ or $a.b.c.d/x$ format ($0 \\le x \\le 32$). The blacklist always contains at least one subnet.\n\nAll of the IPv4 subnets given in the input are valid. Integer numbers do not start with extra leading zeroes. The provided IPv4 subnets can intersect arbitrarily.\n\n\n-----Output-----\n\nOutput -1, if there is an IPv4 address that matches both the whitelist and the blacklist. Otherwise output $t$ \u2014 the length of the optimised blacklist, followed by $t$ subnets, with each subnet on a new line. Subnets may be printed in arbitrary order. All addresses matching the source blacklist must match the optimised blacklist. All addresses matching the source whitelist must not match the optimised blacklist. You can print a subnet $a.b.c.d/32$ in any of two ways: as $a.b.c.d/32$ or as $a.b.c.d$.\n\nIf there is more than one solution, output any.\n\n\n-----Examples-----\nInput\n1\n-149.154.167.99\n\nOutput\n1\n0.0.0.0/0\n\nInput\n4\n-149.154.167.99\n+149.154.167.100/30\n+149.154.167.128/25\n-149.154.167.120/29\n\nOutput\n2\n149.154.167.99\n149.154.167.120/29\n\nInput\n5\n-127.0.0.4/31\n+127.0.0.8\n+127.0.0.0/30\n-195.82.146.208/29\n-127.0.0.6/31\n\nOutput\n2\n195.0.0.0/8\n127.0.0.4/30\n\nInput\n2\n+127.0.0.1/32\n-127.0.0.1\n\nOutput\n-1"} +{"id": "02742", "text": "Let's call a string a phone number if it has length 11 and fits the pattern \"8xxxxxxxxxx\", where each \"x\" is replaced by a digit.\n\nFor example, \"80123456789\" and \"80000000000\" are phone numbers, while \"8012345678\" and \"79000000000\" are not.\n\nYou have $n$ cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.\n\n\n-----Input-----\n\nThe first line contains an integer $n$\u00a0\u2014 the number of cards with digits that you have ($1 \\leq n \\leq 100$).\n\nThe second line contains a string of $n$ digits (characters \"0\", \"1\", ..., \"9\") $s_1, s_2, \\ldots, s_n$. The string will not contain any other characters, such as leading or trailing spaces.\n\n\n-----Output-----\n\nIf at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.\n\n\n-----Examples-----\nInput\n11\n00000000008\n\nOutput\n1\n\nInput\n22\n0011223344556677889988\n\nOutput\n2\n\nInput\n11\n31415926535\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, one phone number, \"8000000000\", can be made from these cards.\n\nIn the second example, you can make two phone numbers from the cards, for example, \"80123456789\" and \"80123456789\".\n\nIn the third example you can't make any phone number from the given cards."} +{"id": "02743", "text": "On the way to school, Karen became fixated on the puzzle game on her phone! [Image] \n\nThe game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0.\n\nOne move consists of choosing one row or column, and adding 1 to all of the cells in that row or column.\n\nTo win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j.\n\nKaren is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task!\n\n\n-----Input-----\n\nThe first line of input contains two integers, n and m (1 \u2264 n, m \u2264 100), the number of rows and the number of columns in the grid, respectively.\n\nThe next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 \u2264 g_{i}, j \u2264 500).\n\n\n-----Output-----\n\nIf there is an error and it is actually not possible to beat the level, output a single integer -1.\n\nOtherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level.\n\nThe next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 \u2264 x \u2264 n) describing a move of the form \"choose the x-th row\". col x, (1 \u2264 x \u2264 m) describing a move of the form \"choose the x-th column\". \n\nIf there are multiple optimal solutions, output any one of them.\n\n\n-----Examples-----\nInput\n3 5\n2 2 2 3 2\n0 0 0 1 0\n1 1 1 2 1\n\nOutput\n4\nrow 1\nrow 1\ncol 4\nrow 3\n\nInput\n3 3\n0 0 0\n0 1 0\n0 0 0\n\nOutput\n-1\n\nInput\n3 3\n1 1 1\n1 1 1\n1 1 1\n\nOutput\n3\nrow 1\nrow 2\nrow 3\n\n\n\n-----Note-----\n\nIn the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] \n\nIn the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center.\n\nIn the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] \n\nNote that this is not the only solution; another solution, among others, is col 1, col 2, col 3."} +{"id": "02744", "text": "In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, all cakes are ready at the same moment t minutes after they started baking. Arkady needs at least n cakes to complete a task, but he currently don't have any. However, he has infinitely many ingredients and one oven. Moreover, Arkady can build one more similar oven to make the process faster, it would take d minutes to build the oven. While the new oven is being built, only old one can bake cakes, after the new oven is built, both ovens bake simultaneously. Arkady can't build more than one oven.\n\nDetermine if it is reasonable to build the second oven, i.e. will it decrease the minimum time needed to get n cakes or not. If the time needed with the second oven is the same as with one oven, then it is unreasonable.\n\n\n-----Input-----\n\nThe only line contains four integers n, t, k, d (1 \u2264 n, t, k, d \u2264 1 000)\u00a0\u2014 the number of cakes needed, the time needed for one oven to bake k cakes, the number of cakes baked at the same time, the time needed to build the second oven. \n\n\n-----Output-----\n\nIf it is reasonable to build the second oven, print \"YES\". Otherwise print \"NO\".\n\n\n-----Examples-----\nInput\n8 6 4 5\n\nOutput\nYES\n\nInput\n8 6 4 6\n\nOutput\nNO\n\nInput\n10 3 11 4\n\nOutput\nNO\n\nInput\n4 2 1 4\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example it is possible to get 8 cakes in 12 minutes using one oven. The second oven can be built in 5 minutes, so after 6 minutes the first oven bakes 4 cakes, the second oven bakes 4 more ovens after 11 minutes. Thus, it is reasonable to build the second oven. \n\nIn the second example it doesn't matter whether we build the second oven or not, thus it takes 12 minutes to bake 8 cakes in both cases. Thus, it is unreasonable to build the second oven.\n\nIn the third example the first oven bakes 11 cakes in 3 minutes, that is more than needed 10. It is unreasonable to build the second oven, because its building takes more time that baking the needed number of cakes using the only oven."} +{"id": "02745", "text": "Iahub got lost in a very big desert. The desert can be represented as a n \u00d7 n square matrix, where each cell is a zone of the desert. The cell (i, j) represents the cell at row i and column j (1 \u2264 i, j \u2264 n). Iahub can go from one cell (i, j) only down or right, that is to cells (i + 1, j) or (i, j + 1). \n\nAlso, there are m cells that are occupied by volcanoes, which Iahub cannot enter. \n\nIahub is initially at cell (1, 1) and he needs to travel to cell (n, n). Knowing that Iahub needs 1 second to travel from one cell to another, find the minimum time in which he can arrive in cell (n, n).\n\n\n-----Input-----\n\nThe first line contains two integers n (1 \u2264 n \u2264 10^9) and m (1 \u2264 m \u2264 10^5). Each of the next m lines contains a pair of integers, x and y (1 \u2264 x, y \u2264 n), representing the coordinates of the volcanoes.\n\nConsider matrix rows are numbered from 1 to n from top to bottom, and matrix columns are numbered from 1 to n from left to right. There is no volcano in cell (1, 1). No two volcanoes occupy the same location. \n\n\n-----Output-----\n\nPrint one integer, the minimum time in which Iahub can arrive at cell (n, n). If no solution exists (there is no path to the final cell), print -1.\n\n\n-----Examples-----\nInput\n4 2\n1 3\n1 4\n\nOutput\n6\n\nInput\n7 8\n1 6\n2 6\n3 5\n3 6\n4 3\n5 1\n5 2\n5 3\n\nOutput\n12\n\nInput\n2 2\n1 2\n2 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nConsider the first sample. A possible road is: (1, 1) \u2192 (1, 2) \u2192 (2, 2) \u2192 (2, 3) \u2192 (3, 3) \u2192 (3, 4) \u2192 (4, 4)."} +{"id": "02746", "text": "Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.\n\nk experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.\n\nDetermine if Snark and Philip can make an interesting problemset!\n\n\n-----Input-----\n\nThe first line contains two integers n, k (1 \u2264 n \u2264 10^5, 1 \u2264 k \u2264 4)\u00a0\u2014 the number of problems and the number of experienced teams.\n\nEach of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.\n\n\n-----Output-----\n\nPrint \"YES\" (quotes for clarity), if it is possible to make an interesting problemset, and \"NO\" otherwise.\n\nYou can print each character either upper- or lowercase (\"YeS\" and \"yes\" are valid when the answer is \"YES\").\n\n\n-----Examples-----\nInput\n5 3\n1 0 1\n1 1 0\n1 0 0\n1 0 0\n1 0 0\n\nOutput\nNO\n\nInput\n3 2\n1 0\n1 1\n0 1\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example you can't make any interesting problemset, because the first team knows all problems.\n\nIn the second example you can choose the first and the third problems."} +{"id": "02747", "text": "Today Pari and Arya are playing a game called Remainders.\n\nPari chooses two positive integer x and k, and tells Arya k but not x. Arya have to find the value $x \\text{mod} k$. There are n ancient numbers c_1, c_2, ..., c_{n} and Pari has to tell Arya $x \\operatorname{mod} c_{i}$ if Arya wants. Given k and the ancient values, tell us if Arya has a winning strategy independent of value of x or not. Formally, is it true that Arya can understand the value $x \\text{mod} k$ for any positive integer x?\n\nNote, that $x \\text{mod} y$ means the remainder of x after dividing it by y.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 n, k \u2264 1 000 000)\u00a0\u2014 the number of ancient integers and value k that is chosen by Pari.\n\nThe second line contains n integers c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 1 000 000).\n\n\n-----Output-----\n\nPrint \"Yes\" (without quotes) if Arya has a winning strategy independent of value of x, or \"No\" (without quotes) otherwise.\n\n\n-----Examples-----\nInput\n4 5\n2 3 5 12\n\nOutput\nYes\n\nInput\n2 7\n2 3\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first sample, Arya can understand $x \\operatorname{mod} 5$ because 5 is one of the ancient numbers.\n\nIn the second sample, Arya can't be sure what $x \\text{mod} 7$ is. For example 1 and 7 have the same remainders after dividing by 2 and 3, but they differ in remainders after dividing by 7."} +{"id": "02748", "text": "Jzzhu has a big rectangular chocolate bar that consists of n \u00d7 m unit squares. He wants to cut this bar exactly k times. Each cut must meet the following requirements:\n\n each cut should be straight (horizontal or vertical); each cut should go along edges of unit squares (it is prohibited to divide any unit chocolate square with cut); each cut should go inside the whole chocolate bar, and all cuts must be distinct. \n\nThe picture below shows a possible way to cut a 5 \u00d7 6 chocolate for 5 times.\n\n [Image] \n\nImagine Jzzhu have made k cuts and the big chocolate is splitted into several pieces. Consider the smallest (by area) piece of the chocolate, Jzzhu wants this piece to be as large as possible. What is the maximum possible area of smallest piece he can get with exactly k cuts? The area of a chocolate piece is the number of unit squares in it.\n\n\n-----Input-----\n\nA single line contains three integers n, m, k (1 \u2264 n, m \u2264 10^9;\u00a01 \u2264 k \u2264 2\u00b710^9).\n\n\n-----Output-----\n\nOutput a single integer representing the answer. If it is impossible to cut the big chocolate k times, print -1.\n\n\n-----Examples-----\nInput\n3 4 1\n\nOutput\n6\n\nInput\n6 4 2\n\nOutput\n8\n\nInput\n2 3 4\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample, Jzzhu can cut the chocolate following the picture below:\n\n [Image] \n\nIn the second sample the optimal division looks like this:\n\n [Image] \n\nIn the third sample, it's impossible to cut a 2 \u00d7 3 chocolate 4 times."} +{"id": "02749", "text": "A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.\n\nThe students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.\n\nHowever, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number. \n\nAfter all the swaps each compartment should either have no student left, or have a company of three or four students. \n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^6) \u2014 the number of compartments in the carriage. The second line contains n integers a_1, a_2, ..., a_{n} showing how many students ride in each compartment (0 \u2264 a_{i} \u2264 4). It is guaranteed that at least one student is riding in the train.\n\n\n-----Output-----\n\nIf no sequence of swapping seats with other people leads to the desired result, print number \"-1\" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.\n\n\n-----Examples-----\nInput\n5\n1 2 2 4 3\n\nOutput\n2\n\nInput\n3\n4 1 1\n\nOutput\n2\n\nInput\n4\n0 3 0 4\n\nOutput\n0"} +{"id": "02750", "text": "Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad\u2014'1' for a correctly identified cow and '0' otherwise.\n\nHowever, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.\n\nKevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring\u2014that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.\n\n\n-----Input-----\n\nThe first line contains the number of questions on the olympiad n (1 \u2264 n \u2264 100 000).\n\nThe following line contains a binary string of length n representing Kevin's results on the USAICO. \n\n\n-----Output-----\n\nOutput a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.\n\n\n-----Examples-----\nInput\n8\n10000011\n\nOutput\n5\n\nInput\n2\n01\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.\n\nIn the second sample, Kevin can flip the entire string and still have the same score."} +{"id": "02751", "text": "As you have noticed, there are lovely girls in Arpa\u2019s land.\n\nPeople in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crush_{i}. [Image] \n\nSomeday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.\n\nThe game consists of rounds. Assume person x wants to start a round, he calls crush_{x} and says: \"Oww...wwf\" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crush_{x} calls crush_{crush}_{x} and says: \"Oww...wwf\" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an \"Owf\" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.\n\nMehrdad has an evil plan to make the game more funny, he wants to find smallest t (t \u2265 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.\n\nSome strange fact in Arpa's land is that someone can be himself's crush (i.e. crush_{i} = i).\n\n\n-----Input-----\n\nThe first line of input contains integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of people in Arpa's land.\n\nThe second line contains n integers, i-th of them is crush_{i} (1 \u2264 crush_{i} \u2264 n)\u00a0\u2014 the number of i-th person's crush.\n\n\n-----Output-----\n\nIf there is no t satisfying the condition, print -1. Otherwise print such smallest t.\n\n\n-----Examples-----\nInput\n4\n2 3 1 4\n\nOutput\n3\n\nInput\n4\n4 4 4 4\n\nOutput\n-1\n\nInput\n4\n2 1 4 3\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first sample suppose t = 3. \n\nIf the first person starts some round:\n\nThe first person calls the second person and says \"Owwwf\", then the second person calls the third person and says \"Owwf\", then the third person calls the first person and says \"Owf\", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.\n\nThe process is similar for the second and the third person.\n\nIf the fourth person starts some round:\n\nThe fourth person calls himself and says \"Owwwf\", then he calls himself again and says \"Owwf\", then he calls himself for another time and says \"Owf\", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.\n\nIn the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa."} +{"id": "02752", "text": "Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:\n\n on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. \n\nOn each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).\n\nFind the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has \u2014 he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.\n\n\n-----Input-----\n\nThe first line contains a positive integer n (1 \u2264 n \u2264 100) \u2014 the number of days of Vasya's vacations.\n\nThe second line contains the sequence of integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 3) separated by space, where: \n\n a_{i} equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; a_{i} equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; a_{i} equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; a_{i} equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.\n\n\n-----Output-----\n\nPrint the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:\n\n to do sport on any two consecutive days, to write the contest on any two consecutive days. \n\n\n-----Examples-----\nInput\n4\n\n1 3 2 0\n\n\nOutput\n2\n\n\nInput\n7\n\n1 3 3 2 1 2 3\n\n\nOutput\n0\n\n\nInput\n2\n\n2 2\n\n\nOutput\n1\n\n\n\n\n-----Note-----\n\nIn the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.\n\nIn the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.\n\nIn the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day."} +{"id": "02753", "text": "Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it.\n\nBut I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater than n. Can you help me to find the maximum possible least common multiple of these three integers?\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^6) \u2014 the n mentioned in the statement.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum possible LCM of three not necessarily distinct positive integers that are not greater than n.\n\n\n-----Examples-----\nInput\n9\n\nOutput\n504\n\nInput\n7\n\nOutput\n210\n\n\n\n-----Note-----\n\nThe least common multiple of some positive integers is the least positive integer which is multiple for each of them.\n\nThe result may become very large, 32-bit integer won't be enough. So using 64-bit integers is recommended.\n\nFor the last example, we can chose numbers 7, 6, 5 and the LCM of them is 7\u00b76\u00b75 = 210. It is the maximum value we can get."} +{"id": "02754", "text": "You are given $n$ rectangles on a plane with coordinates of their bottom left and upper right points. Some $(n-1)$ of the given $n$ rectangles have some common point. A point belongs to a rectangle if this point is strictly inside the rectangle or belongs to its boundary.\n\nFind any point with integer coordinates that belongs to at least $(n-1)$ given rectangles.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 132\\,674$) \u2014 the number of given rectangles.\n\nEach the next $n$ lines contains four integers $x_1$, $y_1$, $x_2$ and $y_2$ ($-10^9 \\le x_1 < x_2 \\le 10^9$, $-10^9 \\le y_1 < y_2 \\le 10^9$) \u2014 the coordinates of the bottom left and upper right corners of a rectangle.\n\n\n-----Output-----\n\nPrint two integers $x$ and $y$ \u2014 the coordinates of any point that belongs to at least $(n-1)$ given rectangles.\n\n\n-----Examples-----\nInput\n3\n0 0 1 1\n1 1 2 2\n3 0 4 1\n\nOutput\n1 1\n\nInput\n3\n0 0 1 1\n0 1 1 2\n1 0 2 1\n\nOutput\n1 1\n\nInput\n4\n0 0 5 5\n0 0 4 4\n1 1 4 4\n1 1 4 4\n\nOutput\n1 1\n\nInput\n5\n0 0 10 8\n1 2 6 7\n2 3 5 6\n3 4 4 5\n8 1 9 2\n\nOutput\n3 4\n\n\n\n-----Note-----\n\nThe picture below shows the rectangles in the first and second samples. The possible answers are highlighted. [Image] \n\nThe picture below shows the rectangles in the third and fourth samples. [Image]"} +{"id": "02755", "text": "Limak is a little polar bear. He has n balls, the i-th ball has size t_{i}.\n\nLimak wants to give one ball to each of his three friends. Giving gifts isn't easy\u00a0\u2014 there are two rules Limak must obey to make friends happy: No two friends can get balls of the same size. No two friends can get balls of sizes that differ by more than 2. \n\nFor example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2).\n\nYour task is to check whether Limak can choose three balls that satisfy conditions above.\n\n\n-----Input-----\n\nThe first line of the input contains one integer n (3 \u2264 n \u2264 50)\u00a0\u2014 the number of balls Limak has.\n\nThe second line contains n integers t_1, t_2, ..., t_{n} (1 \u2264 t_{i} \u2264 1000) where t_{i} denotes the size of the i-th ball.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n4\n18 55 16 17\n\nOutput\nYES\n\nInput\n6\n40 41 43 44 44 44\n\nOutput\nNO\n\nInput\n8\n5 972 3 4 1 4 970 971\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17.\n\nIn the second sample, there is no way to give gifts to three friends without breaking the rules.\n\nIn the third sample, there is even more than one way to choose balls: Choose balls with sizes 3, 4 and 5. Choose balls with sizes 972, 970, 971."} +{"id": "02756", "text": "There are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that they all have integer $y$-coordinates, and their $x$-coordinate is equal to $-100$, while the second group is positioned in such a way that they all have integer $y$-coordinates, and their $x$-coordinate is equal to $100$.\n\nEach spaceship in both groups will simultaneously shoot two laser shots (infinite ray that destroys any spaceship it touches), one towards each of the small spaceships, all at the same time. The small spaceships will be able to avoid all the laser shots, and now want to position themselves at some locations with $x=0$ (with not necessarily integer $y$-coordinates), such that the rays shot at them would destroy as many of the enemy spaceships as possible. Find the largest numbers of spaceships that can be destroyed this way, assuming that the enemy spaceships can't avoid laser shots.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 60$), the number of enemy spaceships with $x = -100$ and the number of enemy spaceships with $x = 100$, respectively.\n\nThe second line contains $n$ integers $y_{1,1}, y_{1,2}, \\ldots, y_{1,n}$ ($|y_{1,i}| \\le 10\\,000$) \u2014 the $y$-coordinates of the spaceships in the first group.\n\nThe third line contains $m$ integers $y_{2,1}, y_{2,2}, \\ldots, y_{2,m}$ ($|y_{2,i}| \\le 10\\,000$) \u2014 the $y$-coordinates of the spaceships in the second group.\n\nThe $y$ coordinates are not guaranteed to be unique, even within a group.\n\n\n-----Output-----\n\nPrint a single integer \u2013 the largest number of enemy spaceships that can be destroyed.\n\n\n-----Examples-----\nInput\n3 9\n1 2 3\n1 2 3 7 8 9 11 12 13\n\nOutput\n9\n\nInput\n5 5\n1 2 3 4 5\n1 2 3 4 5\n\nOutput\n10\n\n\n\n-----Note-----\n\nIn the first example the first spaceship can be positioned at $(0, 2)$, and the second \u2013 at $(0, 7)$. This way all the enemy spaceships in the first group and $6$ out of $9$ spaceships in the second group will be destroyed.\n\nIn the second example the first spaceship can be positioned at $(0, 3)$, and the second can be positioned anywhere, it will be sufficient to destroy all the enemy spaceships."} +{"id": "02757", "text": "Vasya and Petya wrote down all integers from 1 to n to play the \"powers\" game (n can be quite large; however, Vasya and Petya are not confused by this fact).\n\nPlayers choose numbers in turn (Vasya chooses first). If some number x is chosen at the current turn, it is forbidden to choose x or all of its other positive integer powers (that is, x^2, x^3, ...) at the next turns. For instance, if the number 9 is chosen at the first turn, one cannot choose 9 or 81 later, while it is still allowed to choose 3 or 27. The one who cannot make a move loses.\n\nWho wins if both Vasya and Petya play optimally?\n\n\n-----Input-----\n\nInput contains single integer n (1 \u2264 n \u2264 10^9).\n\n\n-----Output-----\n\nPrint the name of the winner \u2014 \"Vasya\" or \"Petya\" (without quotes).\n\n\n-----Examples-----\nInput\n1\n\nOutput\nVasya\n\nInput\n2\n\nOutput\nPetya\n\nInput\n8\n\nOutput\nPetya\n\n\n\n-----Note-----\n\nIn the first sample Vasya will choose 1 and win immediately.\n\nIn the second sample no matter which number Vasya chooses during his first turn, Petya can choose the remaining number and win."} +{"id": "02758", "text": "Innopolis University scientists continue to investigate the periodic table. There are n\u00b7m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 \u2264 r \u2264 n, 1 \u2264 c \u2264 m) in the table.\n\nRecently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r_1, c_1), (r_1, c_2), (r_2, c_1), where r_1 \u2260 r_2 and c_1 \u2260 c_2, then we can produce element (r_2, c_2).\n\n [Image] \n\nSamples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.\n\nInnopolis University scientists already have samples of q elements. They want to obtain samples of all n\u00b7m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, q (1 \u2264 n, m \u2264 200 000; 0 \u2264 q \u2264 min(n\u00b7m, 200 000)), the chemical table dimensions and the number of elements scientists already have.\n\nThe following q lines contain two integers r_{i}, c_{i} (1 \u2264 r_{i} \u2264 n, 1 \u2264 c_{i} \u2264 m), each describes an element that scientists already have. All elements in the input are different.\n\n\n-----Output-----\n\nPrint the minimal number of elements to be purchased.\n\n\n-----Examples-----\nInput\n2 2 3\n1 2\n2 2\n2 1\n\nOutput\n0\n\nInput\n1 5 3\n1 3\n1 1\n1 5\n\nOutput\n2\n\nInput\n4 3 6\n1 2\n1 3\n2 2\n2 3\n3 1\n3 3\n\nOutput\n1\n\n\n\n-----Note-----\n\nFor each example you have a picture which illustrates it.\n\nThe first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.\n\nThe second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.\n\nTest 1\n\nWe can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.\n\n [Image] \n\nTest 2\n\nWe cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.\n\n [Image] \n\nTest 3\n\nThere are several possible solutions. One of them is illustrated below.\n\nNote that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.\n\n [Image]"} +{"id": "02759", "text": "Given are an integer N and four characters c_{\\mathrm{AA}}, c_{\\mathrm{AB}}, c_{\\mathrm{BA}}, and c_{\\mathrm{BB}}.\nHere, it is guaranteed that each of those four characters is A or B.\nSnuke has a string s, which is initially AB.\nLet |s| denote the length of s.\nSnuke can do the four kinds of operations below zero or more times in any order:\n - Choose i such that 1 \\leq i < |s|, s_{i} = A, s_{i+1} = A and insert c_{\\mathrm{AA}} between the i-th and (i+1)-th characters of s.\n - Choose i such that 1 \\leq i < |s|, s_{i} = A, s_{i+1} = B and insert c_{\\mathrm{AB}} between the i-th and (i+1)-th characters of s.\n - Choose i such that 1 \\leq i < |s|, s_{i} = B, s_{i+1} = A and insert c_{\\mathrm{BA}} between the i-th and (i+1)-th characters of s.\n - Choose i such that 1 \\leq i < |s|, s_{i} = B, s_{i+1} = B and insert c_{\\mathrm{BB}} between the i-th and (i+1)-th characters of s.\nFind the number, modulo (10^9+7), of strings that can be s when Snuke has done the operations so that the length of s becomes N.\n\n-----Constraints-----\n - 2 \\leq N \\leq 1000\n - Each of c_{\\mathrm{AA}}, c_{\\mathrm{AB}}, c_{\\mathrm{BA}}, and c_{\\mathrm{BB}} is A or B.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nc_{\\mathrm{AA}}\nc_{\\mathrm{AB}}\nc_{\\mathrm{BA}}\nc_{\\mathrm{BB}}\n\n-----Output-----\nPrint the number, modulo (10^9+7), of strings that can be s when Snuke has done the operations so that the length of s becomes N.\n\n-----Sample Input-----\n4\nA\nB\nB\nA\n\n-----Sample Output-----\n2\n\n - There are two strings that can be s when Snuke is done: ABAB and ABBB."} +{"id": "02760", "text": "Bash has set out on a journey to become the greatest Pokemon master. To get his first Pokemon, he went to Professor Zulu's Lab. Since Bash is Professor Zulu's favourite student, Zulu allows him to take as many Pokemon from his lab as he pleases.\n\nBut Zulu warns him that a group of k > 1 Pokemon with strengths {s_1, s_2, s_3, ..., s_{k}} tend to fight among each other if gcd(s_1, s_2, s_3, ..., s_{k}) = 1 (see notes for gcd definition).\n\nBash, being smart, does not want his Pokemon to fight among each other. However, he also wants to maximize the number of Pokemon he takes from the lab. Can you help Bash find out the maximum number of Pokemon he can take? \n\nNote: A Pokemon cannot fight with itself.\n\n\n-----Input-----\n\nThe input consists of two lines.\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^5), the number of Pokemon in the lab.\n\nThe next line contains n space separated integers, where the i-th of them denotes s_{i} (1 \u2264 s_{i} \u2264 10^5), the strength of the i-th Pokemon.\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the maximum number of Pokemons Bash can take.\n\n\n-----Examples-----\nInput\n3\n2 3 4\n\nOutput\n2\n\nInput\n5\n2 3 4 6 7\n\nOutput\n3\n\n\n\n-----Note-----\n\ngcd (greatest common divisor) of positive integers set {a_1, a_2, ..., a_{n}} is the maximum positive integer that divides all the integers {a_1, a_2, ..., a_{n}}.\n\nIn the first sample, we can take Pokemons with strengths {2, 4} since gcd(2, 4) = 2.\n\nIn the second sample, we can take Pokemons with strengths {2, 4, 6}, and there is no larger group with gcd \u2260 1."} +{"id": "02761", "text": "We have a string S of length N consisting of A, B, and C.\nYou can do the following operation on S zero or more times:\n - Choose i (1 \\leq i \\leq |S| - 1) such that S_i \\neq S_{i + 1}. Replace S_i with the character (among A, B, and C) that is different from both S_i and S_{i + 1}, and remove S_{i + 1} from S.\nFind the number of distinct strings that S can be after zero or more operations, and print the count modulo (10^9+7).\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^6\n - S is a string of length N consisting of A, B, and C.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS\n\n-----Output-----\nPrint the number of distinct strings that S can be after zero or more operations, modulo (10^9+7).\n\n-----Sample Input-----\n5\nABAAC\n\n-----Sample Output-----\n11\n\nFor example, the following sequence of operations turns S into ACB:\n - First, choose i=2. We replace S_2 with C and remove S_3, turning S into ACAC.\n - Then, choose i=3. We replace S_3 with B and remove S_4, turning S into ACB."} +{"id": "02762", "text": "Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h_1 and height of Abol is h_2. Each second, Mike waters Abol and Xaniar.\n\n [Image] \n\nSo, if height of Xaniar is h_1 and height of Abol is h_2, after one second height of Xaniar will become $(x_{1} h_{1} + y_{1}) \\operatorname{mod} m$ and height of Abol will become $(x_{2} h_{2} + y_{2}) \\operatorname{mod} m$ where x_1, y_1, x_2 and y_2 are some integer numbers and $a \\operatorname{mod} b$ denotes the remainder of a modulo b.\n\nMike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is a_1 and height of Abol is a_2.\n\nMike has asked you for your help. Calculate the minimum time or say it will never happen.\n\n\n-----Input-----\n\nThe first line of input contains integer m (2 \u2264 m \u2264 10^6).\n\nThe second line of input contains integers h_1 and a_1 (0 \u2264 h_1, a_1 < m).\n\nThe third line of input contains integers x_1 and y_1 (0 \u2264 x_1, y_1 < m).\n\nThe fourth line of input contains integers h_2 and a_2 (0 \u2264 h_2, a_2 < m).\n\nThe fifth line of input contains integers x_2 and y_2 (0 \u2264 x_2, y_2 < m).\n\nIt is guaranteed that h_1 \u2260 a_1 and h_2 \u2260 a_2.\n\n\n-----Output-----\n\nPrint the minimum number of seconds until Xaniar reaches height a_1 and Abol reaches height a_2 or print -1 otherwise.\n\n\n-----Examples-----\nInput\n5\n4 2\n1 1\n0 1\n2 3\n\nOutput\n3\n\nInput\n1023\n1 2\n1 0\n1 2\n1 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample, heights sequences are following:\n\nXaniar: $4 \\rightarrow 0 \\rightarrow 1 \\rightarrow 2$\n\nAbol: $0 \\rightarrow 3 \\rightarrow 4 \\rightarrow 1$"} +{"id": "02763", "text": "There are infinitely many cards, numbered 1, 2, 3, ...\nInitially, Cards x_1, x_2, ..., x_N are face up, and the others are face down.\nSnuke can perform the following operation repeatedly:\n - Select a prime p greater than or equal to 3. Then, select p consecutive cards and flip all of them.\nSnuke's objective is to have all the cards face down.\nFind the minimum number of operations required to achieve the objective.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 100\n - 1 \u2264 x_1 < x_2 < ... < x_N \u2264 10^7\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nx_1 x_2 ... x_N\n\n-----Output-----\nPrint the minimum number of operations required to achieve the objective.\n\n-----Sample Input-----\n2\n4 5\n\n-----Sample Output-----\n2\n\nBelow is one way to achieve the objective in two operations:\n - Select p = 5 and flip Cards 1, 2, 3, 4 and 5.\n - Select p = 3 and flip Cards 1, 2 and 3."} +{"id": "02764", "text": "An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \\ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \\leq i \\leq n - 1$.\n\nTrans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.\n\nHowever, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?\n\n\n-----Input-----\n\nThe only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \\leq 10^5$).\n\n\n-----Output-----\n\nIf it is impossible to construct a beautiful sequence satisfying the above constraints, print \"NO\" (without quotes) in one line.\n\nOtherwise, print \"YES\" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces\u00a0\u2014 a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.\n\nIf there are multiple answers, you can print any of them.\n\n\n-----Examples-----\nInput\n2 2 2 1\n\nOutput\nYES\n0 1 0 1 2 3 2\n\nInput\n1 2 3 4\n\nOutput\nNO\n\nInput\n2 2 2 3\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.\n\nIt can be proved, that it is impossible to construct beautiful sequences in the second and third tests."} +{"id": "02765", "text": "You are given a table consisting of n rows and m columns.\n\nNumbers in each row form a permutation of integers from 1 to m.\n\nYou are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order.\n\nYou have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 n, m \u2264 20)\u00a0\u2014 the number of rows and the number of columns in the given table. \n\nEach of next n lines contains m integers\u00a0\u2014 elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m.\n\n\n-----Output-----\n\nIf there is a way to obtain the identity permutation in each row by following the given rules, print \"YES\" (without quotes) in the only line of the output. Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n2 4\n1 3 2 4\n1 3 4 2\n\nOutput\nYES\n\nInput\n4 4\n1 2 3 4\n2 3 4 1\n3 4 1 2\n4 1 2 3\n\nOutput\nNO\n\nInput\n3 6\n2 1 3 4 5 6\n1 2 4 3 5 6\n1 2 3 4 6 5\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample, one can act in the following way: Swap second and third columns. Now the table is 1\u00a02\u00a03\u00a04 1\u00a04\u00a03\u00a02 In the second row, swap the second and the fourth elements. Now the table is 1\u00a02\u00a03\u00a04 1\u00a02\u00a03\u00a04"} +{"id": "02766", "text": "Tarly has two different type of items, food boxes and wine barrels. There are f food boxes and w wine barrels. Tarly stores them in various stacks and each stack can consist of either food boxes or wine barrels but not both. The stacks are placed in a line such that no two stacks of food boxes are together and no two stacks of wine barrels are together.\n\nThe height of a stack is defined as the number of items in the stack. Two stacks are considered different if either their heights are different or one of them contains food and other contains wine.\n\nJon Snow doesn't like an arrangement if any stack of wine barrels has height less than or equal to h. What is the probability that Jon Snow will like the arrangement if all arrangement are equiprobably?\n\nTwo arrangement of stacks are considered different if exists such i, that i-th stack of one arrangement is different from the i-th stack of the other arrangement.\n\n\n-----Input-----\n\nThe first line of input contains three integers f, w, h (0 \u2264 f, w, h \u2264 10^5) \u2014 number of food boxes, number of wine barrels and h is as described above. It is guaranteed that he has at least one food box or at least one wine barrel.\n\n\n-----Output-----\n\nOutput the probability that Jon Snow will like the arrangement. The probability is of the form [Image], then you need to output a single integer p\u00b7q^{ - 1} mod (10^9 + 7).\n\n\n-----Examples-----\nInput\n1 1 1\n\nOutput\n0\n\nInput\n1 2 1\n\nOutput\n666666672\n\n\n\n-----Note-----\n\nIn the first example f = 1, w = 1 and h = 1, there are only two possible arrangement of stacks and Jon Snow doesn't like any of them.\n\nIn the second example f = 1, w = 2 and h = 1, there are three arrangements. Jon Snow likes the (1) and (3) arrangement. So the probabilty is $\\frac{2}{3}$. [Image]"} +{"id": "02767", "text": "DZY has a sequence a, consisting of n integers.\n\nWe'll call a sequence a_{i}, a_{i} + 1, ..., a_{j} (1 \u2264 i \u2264 j \u2264 n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.\n\nYour task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.\n\nYou only need to output the length of the subsegment you find.\n\n\n-----Input-----\n\nThe first line contains integer n\u00a0(1 \u2264 n \u2264 10^5). The next line contains n integers a_1, a_2, ..., a_{n}\u00a0(1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nIn a single line print the answer to the problem \u2014 the maximum length of the required subsegment.\n\n\n-----Examples-----\nInput\n6\n7 2 3 1 5 6\n\nOutput\n5\n\n\n\n-----Note-----\n\nYou can choose subsegment a_2, a_3, a_4, a_5, a_6 and change its 3rd element (that is a_4) to 4."} +{"id": "02768", "text": "Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens.\n\nAll characters in this game are lowercase English letters. There are two players: Mister B and his competitor.\n\nInitially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to \"abcde\").\n\nThe players take turns appending letters to string s. Mister B moves first.\n\nMister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move.\n\nMister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is \"bfdd\", the computer chooses string t equal to \"aceg\"). After that the chosen string t is appended to the end of s.\n\nMister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1.\n\n\n-----Input-----\n\nFirst and only line contains four space-separated integers: a, b, l and r (1 \u2264 a, b \u2264 12, 1 \u2264 l \u2264 r \u2264 10^9) \u2014 the numbers of letters each player appends and the bounds of the segment.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s.\n\n\n-----Examples-----\nInput\n1 1 1 8\n\nOutput\n2\nInput\n4 2 2 6\n\nOutput\n3\nInput\n3 7 4 6\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first sample test one of optimal strategies generate string s = \"abababab...\", that's why answer is 2.\n\nIn the second sample test string s = \"abcdbcaefg...\" can be obtained, chosen segment will look like \"bcdbc\", that's why answer is 3.\n\nIn the third sample test string s = \"abczzzacad...\" can be obtained, chosen, segment will look like \"zzz\", that's why answer is 1."} +{"id": "02769", "text": "Let us call a pair of integer numbers m-perfect, if at least one number in the pair is greater than or equal to m. Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not.\n\nTwo integers x, y are written on the blackboard. It is allowed to erase one of them and replace it with the sum of the numbers, (x + y).\n\nWhat is the minimum number of such operations one has to perform in order to make the given pair of integers m-perfect?\n\n\n-----Input-----\n\nSingle line of the input contains three integers x, y and m ( - 10^18 \u2264 x, y, m \u2264 10^18).\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in C++. It is preffered to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nPrint the minimum number of operations or \"-1\" (without quotes), if it is impossible to transform the given pair to the m-perfect one.\n\n\n-----Examples-----\nInput\n1 2 5\n\nOutput\n2\n\nInput\n-1 4 15\n\nOutput\n4\n\nInput\n0 -1 5\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first sample the following sequence of operations is suitable: (1, 2) $\\rightarrow$ (3, 2) $\\rightarrow$ (5, 2).\n\nIn the second sample: (-1, 4) $\\rightarrow$ (3, 4) $\\rightarrow$ (7, 4) $\\rightarrow$ (11, 4) $\\rightarrow$ (15, 4).\n\nFinally, in the third sample x, y cannot be made positive, hence there is no proper sequence of operations."} +{"id": "02770", "text": "One common way of digitalizing sound is to record sound intensity at particular time moments. For each time moment intensity is recorded as a non-negative integer. Thus we can represent a sound file as an array of $n$ non-negative integers.\n\nIf there are exactly $K$ distinct values in the array, then we need $k = \\lceil \\log_{2} K \\rceil$ bits to store each value. It then takes $nk$ bits to store the whole file.\n\nTo reduce the memory consumption we need to apply some compression. One common way is to reduce the number of possible intensity values. We choose two integers $l \\le r$, and after that all intensity values are changed in the following way: if the intensity value is within the range $[l;r]$, we don't change it. If it is less than $l$, we change it to $l$; if it is greater than $r$, we change it to $r$. You can see that we lose some low and some high intensities.\n\nYour task is to apply this compression in such a way that the file fits onto a disk of size $I$ bytes, and the number of changed elements in the array is minimal possible.\n\nWe remind you that $1$ byte contains $8$ bits.\n\n$k = \\lceil log_{2} K \\rceil$ is the smallest integer such that $K \\le 2^{k}$. In particular, if $K = 1$, then $k = 0$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $I$ ($1 \\le n \\le 4 \\cdot 10^{5}$, $1 \\le I \\le 10^{8}$)\u00a0\u2014 the length of the array and the size of the disk in bytes, respectively.\n\nThe next line contains $n$ integers $a_{i}$ ($0 \\le a_{i} \\le 10^{9}$)\u00a0\u2014 the array denoting the sound file.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimal possible number of changed elements.\n\n\n-----Examples-----\nInput\n6 1\n2 1 2 3 4 3\n\nOutput\n2\n\nInput\n6 2\n2 1 2 3 4 3\n\nOutput\n0\n\nInput\n6 1\n1 1 2 2 3 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example we can choose $l=2, r=3$. The array becomes 2 2 2 3 3 3, the number of distinct elements is $K=2$, and the sound file fits onto the disk. Only two values are changed.\n\nIn the second example the disk is larger, so the initial file fits it and no changes are required.\n\nIn the third example we have to change both 1s or both 3s."} +{"id": "02771", "text": "You are given names of two days of the week.\n\nPlease, determine whether it is possible that during some non-leap year the first day of some month was equal to the first day of the week you are given, while the first day of the next month was equal to the second day of the week you are given. Both months should belong to one year.\n\nIn this problem, we consider the Gregorian calendar to be used. The number of months in this calendar is equal to 12. The number of days in months during any non-leap year is: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31.\n\nNames of the days of the week are given with lowercase English letters: \"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\", \"saturday\", \"sunday\".\n\n\n-----Input-----\n\nThe input consists of two lines, each of them containing the name of exactly one day of the week. It's guaranteed that each string in the input is from the set \"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\", \"saturday\", \"sunday\".\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if such situation is possible during some non-leap year. Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\nmonday\ntuesday\n\nOutput\nNO\n\nInput\nsunday\nsunday\n\nOutput\nYES\n\nInput\nsaturday\ntuesday\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the second sample, one can consider February 1 and March 1 of year 2015. Both these days were Sundays.\n\nIn the third sample, one can consider July 1 and August 1 of year 2017. First of these two days is Saturday, while the second one is Tuesday."} +{"id": "02772", "text": "You are given a positive integer $n$.\n\nLet $S(x)$ be sum of digits in base 10 representation of $x$, for example, $S(123) = 1 + 2 + 3 = 6$, $S(0) = 0$.\n\nYour task is to find two integers $a, b$, such that $0 \\leq a, b \\leq n$, $a + b = n$ and $S(a) + S(b)$ is the largest possible among all such pairs.\n\n\n-----Input-----\n\nThe only line of input contains an integer $n$ $(1 \\leq n \\leq 10^{12})$.\n\n\n-----Output-----\n\nPrint largest $S(a) + S(b)$ among all pairs of integers $a, b$, such that $0 \\leq a, b \\leq n$ and $a + b = n$.\n\n\n-----Examples-----\nInput\n35\n\nOutput\n17\n\nInput\n10000000000\n\nOutput\n91\n\n\n\n-----Note-----\n\nIn the first example, you can choose, for example, $a = 17$ and $b = 18$, so that $S(17) + S(18) = 1 + 7 + 1 + 8 = 17$. It can be shown that it is impossible to get a larger answer.\n\nIn the second test example, you can choose, for example, $a = 5000000001$ and $b = 4999999999$, with $S(5000000001) + S(4999999999) = 91$. It can be shown that it is impossible to get a larger answer."} +{"id": "02773", "text": "Recently, a start up by two students of a state university of city F gained incredible popularity. Now it's time to start a new company. But what do we call it?\n\nThe market analysts came up with a very smart plan: the name of the company should be identical to its reflection in a mirror! In other words, if we write out the name of the company on a piece of paper in a line (horizontally, from left to right) with large English letters, then put this piece of paper in front of the mirror, then the reflection of the name in the mirror should perfectly match the line written on the piece of paper.\n\nThere are many suggestions for the company name, so coming up to the mirror with a piece of paper for each name wouldn't be sensible. The founders of the company decided to automatize this process. They asked you to write a program that can, given a word, determine whether the word is a 'mirror' word or not.\n\n\n-----Input-----\n\nThe first line contains a non-empty name that needs to be checked. The name contains at most 10^5 large English letters. The name will be written with the next sans serif font: $\\text{ABCDEFGHI JKLMNOPQRSTUVWXYZ}$\n\n\n-----Output-----\n\nPrint 'YES' (without the quotes), if the given name matches its mirror reflection. Otherwise, print 'NO' (without the quotes).\n\n\n-----Examples-----\nInput\nAHA\n\nOutput\nYES\n\nInput\nZ\n\nOutput\nNO\n\nInput\nXO\n\nOutput\nNO"} +{"id": "02774", "text": "\"Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come.\" \u2014 The Night's Watch oath.\n\nWith that begins the watch of Jon Snow. He is assigned the task to support the stewards.\n\nThis time he has n stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him.\n\nCan you find how many stewards will Jon support?\n\n\n-----Input-----\n\nFirst line consists of a single integer n (1 \u2264 n \u2264 10^5) \u2014 the number of stewards with Jon Snow.\n\nSecond line consists of n space separated integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9) representing the values assigned to the stewards.\n\n\n-----Output-----\n\nOutput a single integer representing the number of stewards which Jon will feed.\n\n\n-----Examples-----\nInput\n2\n1 5\n\nOutput\n0\nInput\n3\n1 2 5\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first sample, Jon Snow cannot support steward with strength 1 because there is no steward with strength less than 1 and he cannot support steward with strength 5 because there is no steward with strength greater than 5.\n\nIn the second sample, Jon Snow can support steward with strength 2 because there are stewards with strength less than 2 and greater than 2."} +{"id": "02775", "text": "Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operation. There are four move operations at all: 'U': go up, (x, y) \u2192 (x, y+1); 'D': go down, (x, y) \u2192 (x, y-1); 'L': go left, (x, y) \u2192 (x-1, y); 'R': go right, (x, y) \u2192 (x+1, y). \n\nThe robot will do the operations in s from left to right, and repeat it infinite times. Help Fox Ciel to determine if after some steps the robot will located in (a, b).\n\n\n-----Input-----\n\nThe first line contains two integers a and b, ( - 10^9 \u2264 a, b \u2264 10^9). The second line contains a string s (1 \u2264 |s| \u2264 100, s only contains characters 'U', 'D', 'L', 'R') \u2014 the command.\n\n\n-----Output-----\n\nPrint \"Yes\" if the robot will be located at (a, b), and \"No\" otherwise.\n\n\n-----Examples-----\nInput\n2 2\nRU\n\nOutput\nYes\n\nInput\n1 2\nRU\n\nOutput\nNo\n\nInput\n-1 1000000000\nLRRLU\n\nOutput\nYes\n\nInput\n0 0\nD\n\nOutput\nYes\n\n\n\n-----Note-----\n\nIn the first and second test case, command string is \"RU\", so the robot will go right, then go up, then right, and then up and so on.\n\nThe locations of its moves are (0, 0) \u2192 (1, 0) \u2192 (1, 1) \u2192 (2, 1) \u2192 (2, 2) \u2192 ...\n\nSo it can reach (2, 2) but not (1, 2)."} +{"id": "02776", "text": "For a positive integer n, let us define f(n) as the number of digits in base 10.\nYou are given an integer S.\nCount the number of the pairs of positive integers (l, r) (l \\leq r) such that f(l) + f(l + 1) + ... + f(r) = S, and find the count modulo 10^9 + 7.\n\n-----Constraints-----\n - 1 \\leq S \\leq 10^8\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n1\n\n-----Sample Output-----\n9\n\nThere are nine pairs (l, r) that satisfies the condition: (1, 1), (2, 2), ..., (9, 9)."} +{"id": "02777", "text": "You are given $n$ integer numbers $a_1, a_2, \\dots, a_n$. Consider graph on $n$ nodes, in which nodes $i$, $j$ ($i\\neq j$) are connected if and only if, $a_i$ AND $a_j\\neq 0$, where AND denotes the bitwise AND operation.\n\nFind the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ $(1 \\le n \\le 10^5)$\u00a0\u2014 number of numbers.\n\nThe second line contains $n$ integer numbers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 10^{18}$).\n\n\n-----Output-----\n\nIf the graph doesn't have any cycles, output $-1$. Else output the length of the shortest cycle.\n\n\n-----Examples-----\nInput\n4\n3 6 28 9\n\nOutput\n4\nInput\n5\n5 12 9 16 48\n\nOutput\n3\nInput\n4\n1 2 4 8\n\nOutput\n-1\n\n\n-----Note-----\n\nIn the first example, the shortest cycle is $(9, 3, 6, 28)$.\n\nIn the second example, the shortest cycle is $(5, 12, 9)$.\n\nThe graph has no cycles in the third example."} +{"id": "02778", "text": "Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of $n$ light bulbs in a single row. Each bulb has a number from $1$ to $n$ (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.[Image]\n\nVadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by $2$). For example, the complexity of 1 4 2 3 5 is $2$ and the complexity of 1 3 5 7 6 4 2 is $1$.\n\nNo one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 100$)\u00a0\u2014 the number of light bulbs on the garland.\n\nThe second line contains $n$ integers $p_1,\\ p_2,\\ \\ldots,\\ p_n$ ($0 \\le p_i \\le n$)\u00a0\u2014 the number on the $i$-th bulb, or $0$ if it was removed.\n\n\n-----Output-----\n\nOutput a single number\u00a0\u2014 the minimum complexity of the garland.\n\n\n-----Examples-----\nInput\n5\n0 5 0 2 3\n\nOutput\n2\n\nInput\n7\n1 0 0 5 0 0 2\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only $(5, 4)$ and $(2, 3)$ are the pairs of adjacent bulbs that have different parity.\n\nIn the second case, one of the correct answers is 1 7 3 5 6 4 2."} +{"id": "02779", "text": "Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate.\n\nThe path consists of $n$ consecutive tiles, numbered from $1$ to $n$. Ujan will paint each tile in some color. He will consider the path aesthetic if for any two different tiles with numbers $i$ and $j$, such that $|j - i|$ is a divisor of $n$ greater than $1$, they have the same color. Formally, the colors of two tiles with numbers $i$ and $j$ should be the same if $|i-j| > 1$ and $n \\bmod |i-j| = 0$ (where $x \\bmod y$ is the remainder when dividing $x$ by $y$).\n\nUjan wants to brighten up space. What is the maximum number of different colors that Ujan can use, so that the path is aesthetic?\n\n\n-----Input-----\n\nThe first line of input contains a single integer $n$ ($1 \\leq n \\leq 10^{12}$), the length of the path.\n\n\n-----Output-----\n\nOutput a single integer, the maximum possible number of colors that the path can be painted in.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n2\n\nInput\n5\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first sample, two colors is the maximum number. Tiles $1$ and $3$ should have the same color since $4 \\bmod |3-1| = 0$. Also, tiles $2$ and $4$ should have the same color since $4 \\bmod |4-2| = 0$.\n\nIn the second sample, all five colors can be used. [Image]"} +{"id": "02780", "text": "There are n students at Berland State University. Every student has two skills, each measured as a number: a_{i} \u2014 the programming skill and b_{i} \u2014 the sports skill.\n\nIt is announced that an Olympiad in programming and sports will be held soon. That's why Berland State University should choose two teams: one to take part in the programming track and one to take part in the sports track.\n\nThere should be exactly p students in the programming team and exactly s students in the sports team. A student can't be a member of both teams.\n\nThe university management considers that the strength of the university on the Olympiad is equal to the sum of two values: the programming team strength and the sports team strength. The strength of a team is the sum of skills of its members in the corresponding area, so the strength of the programming team is the sum of all a_{i} and the strength of the sports team is the sum of all b_{i} over corresponding team members.\n\nHelp Berland State University to compose two teams to maximize the total strength of the university on the Olympiad.\n\n\n-----Input-----\n\nThe first line contains three positive integer numbers n, p and s (2 \u2264 n \u2264 3000, p + s \u2264 n) \u2014 the number of students, the size of the programming team and the size of the sports team.\n\nThe second line contains n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 3000), where a_{i} is the programming skill of the i-th student.\n\nThe third line contains n positive integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 3000), where b_{i} is the sports skill of the i-th student.\n\n\n-----Output-----\n\nIn the first line, print the the maximum strength of the university on the Olympiad. In the second line, print p numbers \u2014 the members of the programming team. In the third line, print s numbers \u2014 the members of the sports team.\n\nThe students are numbered from 1 to n as they are given in the input. All numbers printed in the second and in the third lines should be distinct and can be printed in arbitrary order.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n5 2 2\n1 3 4 5 2\n5 3 2 1 4\n\nOutput\n18\n3 4 \n1 5 \n\nInput\n4 2 2\n10 8 8 3\n10 7 9 4\n\nOutput\n31\n1 2 \n3 4 \n\nInput\n5 3 1\n5 2 5 1 7\n6 3 1 6 3\n\nOutput\n23\n1 3 5 \n4"} +{"id": "02781", "text": "One day student Vasya was sitting on a lecture and mentioned a string s_1s_2... s_{n}, consisting of letters \"a\", \"b\" and \"c\" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties: G has exactly n vertices, numbered from 1 to n. For all pairs of vertices i and j, where i \u2260 j, there is an edge connecting them if and only if characters s_{i} and s_{j} are either equal or neighbouring in the alphabet. That is, letters in pairs \"a\"-\"b\" and \"b\"-\"c\" are neighbouring, while letters \"a\"-\"c\" are not. \n\nVasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m $(1 \\leq n \\leq 500,0 \\leq m \\leq \\frac{n(n - 1)}{2})$\u00a0\u2014 the number of vertices and edges in the graph found by Petya, respectively.\n\nEach of the next m lines contains two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i})\u00a0\u2014 the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.\n\n\n-----Output-----\n\nIn the first line print \"Yes\" (without the quotes), if the string s Petya is interested in really exists and \"No\" (without the quotes) otherwise.\n\nIf the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters \"a\", \"b\" and \"c\" only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.\n\n\n-----Examples-----\nInput\n2 1\n1 2\n\nOutput\nYes\naa\n\nInput\n4 3\n1 2\n1 3\n1 4\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings \"aa\", \"ab\", \"ba\", \"bb\", \"bc\", \"cb\", \"cc\" meets the graph's conditions. \n\nIn the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c."} +{"id": "02782", "text": "The Tower of Hanoi is a well-known mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.\n\nThe objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: Only one disk can be moved at a time. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. No disk may be placed on top of a smaller disk. \n\nWith three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2^{n} - 1, where n is the number of disks. (c) Wikipedia.\n\nSmallY's puzzle is very similar to the famous Tower of Hanoi. In the Tower of Hanoi puzzle you need to solve a puzzle in minimum number of moves, in SmallY's puzzle each move costs some money and you need to solve the same puzzle but for minimal cost. At the beginning of SmallY's puzzle all n disks are on the first rod. Moving a disk from rod i to rod j (1 \u2264 i, j \u2264 3) costs t_{ij} units of money. The goal of the puzzle is to move all the disks to the third rod.\n\nIn the problem you are given matrix t and an integer n. You need to count the minimal cost of solving SmallY's puzzle, consisting of n disks.\n\n\n-----Input-----\n\nEach of the first three lines contains three integers \u2014 matrix t. The j-th integer in the i-th line is t_{ij} (1 \u2264 t_{ij} \u2264 10000;\u00a0i \u2260 j). The following line contains a single integer n (1 \u2264 n \u2264 40) \u2014 the number of disks.\n\nIt is guaranteed that for all i (1 \u2264 i \u2264 3), t_{ii} = 0.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum cost of solving SmallY's puzzle.\n\n\n-----Examples-----\nInput\n0 1 1\n1 0 1\n1 1 0\n3\n\nOutput\n7\n\nInput\n0 2 2\n1 0 100\n1 2 0\n3\n\nOutput\n19\n\nInput\n0 2 1\n1 0 100\n1 2 0\n5\n\nOutput\n87"} +{"id": "02783", "text": "Bash wants to become a Pokemon master one day. Although he liked a lot of Pokemon, he has always been fascinated by Bulbasaur the most. Soon, things started getting serious and his fascination turned into an obsession. Since he is too young to go out and catch Bulbasaur, he came up with his own way of catching a Bulbasaur.\n\nEach day, he takes the front page of the newspaper. He cuts out the letters one at a time, from anywhere on the front page of the newspaper to form the word \"Bulbasaur\" (without quotes) and sticks it on his wall. Bash is very particular about case\u00a0\u2014 the first letter of \"Bulbasaur\" must be upper case and the rest must be lower case. By doing this he thinks he has caught one Bulbasaur. He then repeats this step on the left over part of the newspaper. He keeps doing this until it is not possible to form the word \"Bulbasaur\" from the newspaper.\n\nGiven the text on the front page of the newspaper, can you tell how many Bulbasaurs he will catch today?\n\nNote: uppercase and lowercase letters are considered different.\n\n\n-----Input-----\n\nInput contains a single line containing a string s (1 \u2264 |s| \u2264 10^5)\u00a0\u2014 the text on the front page of the newspaper without spaces and punctuation marks. |s| is the length of the string s.\n\nThe string s contains lowercase and uppercase English letters, i.e. $s_{i} \\in \\{a, b, \\ldots, z, A, B, \\ldots, Z \\}$.\n\n\n-----Output-----\n\nOutput a single integer, the answer to the problem.\n\n\n-----Examples-----\nInput\nBulbbasaur\n\nOutput\n1\n\nInput\nF\n\nOutput\n0\n\nInput\naBddulbasaurrgndgbualdBdsagaurrgndbb\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first case, you could pick: Bulbbasaur.\n\nIn the second case, there is no way to pick even a single Bulbasaur.\n\nIn the third case, you can rearrange the string to BulbasaurBulbasauraddrgndgddgargndbb to get two words \"Bulbasaur\"."} +{"id": "02784", "text": "There is an H \\times W grid (H vertical, W horizontal), where each square contains a lowercase English letter.\nSpecifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i.\nSnuke can apply the following operation to this grid any number of times:\n - Choose two different rows and swap them. Or, choose two different columns and swap them.\nSnuke wants this grid to be symmetric.\nThat is, for any 1 \\leq i \\leq H and 1 \\leq j \\leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal.\nDetermine if Snuke can achieve this objective.\n\n-----Constraints-----\n - 1 \\leq H \\leq 12\n - 1 \\leq W \\leq 12\n - |S_i| = W\n - S_i consists of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\nS_1\nS_2\n:\nS_H\n\n-----Output-----\nIf Snuke can make the grid symmetric, print YES; if he cannot, print NO.\n\n-----Sample Input-----\n2 3\narc\nrac\n\n-----Sample Output-----\nYES\n\nIf the second and third columns from the left are swapped, the grid becomes symmetric, as shown in the image below:"} +{"id": "02785", "text": "There are N non-negative integers written on a blackboard. The i-th integer is A_i.\nTakahashi can perform the following two kinds of operations any number of times in any order:\n - Select one integer written on the board (let this integer be X). Write 2X on the board, without erasing the selected integer.\n - Select two integers, possibly the same, written on the board (let these integers be X and Y). Write X XOR Y (XOR stands for bitwise xor) on the blackboard, without erasing the selected integers.\nHow many different integers not exceeding X can be written on the blackboard? We will also count the integers that are initially written on the board.\nSince the answer can be extremely large, find the count modulo 998244353.\n\n-----Constraints-----\n - 1 \\leq N \\leq 6\n - 1 \\leq X < 2^{4000}\n - 1 \\leq A_i < 2^{4000}(1\\leq i\\leq N)\n - All input values are integers.\n - X and A_i(1\\leq i\\leq N) are given in binary notation, with the most significant digit in each of them being 1.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN X\nA_1\n:\nA_N\n\n-----Output-----\nPrint the number of different integers not exceeding X that can be written on the blackboard.\n\n-----Sample Input-----\n3 111\n1111\n10111\n10010\n\n-----Sample Output-----\n4\n\nInitially, 15, 23 and 18 are written on the blackboard. Among the integers not exceeding 7, four integers, 0, 3, 5 and 6, can be written.\nFor example, 6 can be written as follows:\n - Double 15 to write 30.\n - Take XOR of 30 and 18 to write 12.\n - Double 12 to write 24.\n - Take XOR of 30 and 24 to write 6."} +{"id": "02786", "text": "Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothing. Once one of the players scores exactly k points, the score is reset and a new set begins.\n\nAcross all the sets Misha scored a points in total, and Vanya scored b points. Given this information, determine the maximum number of sets they could have played, or that the situation is impossible.\n\nNote that the game consisted of several complete sets.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers k, a and b (1 \u2264 k \u2264 10^9, 0 \u2264 a, b \u2264 10^9, a + b > 0).\n\n\n-----Output-----\n\nIf the situation is impossible, print a single number -1. Otherwise, print the maximum possible number of sets.\n\n\n-----Examples-----\nInput\n11 11 5\n\nOutput\n1\n\nInput\n11 2 3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nNote that the rules of the game in this problem differ from the real table tennis game, for example, the rule of \"balance\" (the winning player has to be at least two points ahead to win a set) has no power within the present problem."} +{"id": "02787", "text": "Kostya likes Codeforces contests very much. However, he is very disappointed that his solutions are frequently hacked. That's why he decided to obfuscate (intentionally make less readable) his code before upcoming contest.\n\nTo obfuscate the code, Kostya first looks at the first variable name used in his program and replaces all its occurrences with a single symbol a, then he looks at the second variable name that has not been replaced yet, and replaces all its occurrences with b, and so on. Kostya is well-mannered, so he doesn't use any one-letter names before obfuscation. Moreover, there are at most 26 unique identifiers in his programs.\n\nYou are given a list of identifiers of some program with removed spaces and line breaks. Check if this program can be a result of Kostya's obfuscation.\n\n\n-----Input-----\n\nIn the only line of input there is a string S of lowercase English letters (1 \u2264 |S| \u2264 500)\u00a0\u2014 the identifiers of a program with removed whitespace characters.\n\n\n-----Output-----\n\nIf this program can be a result of Kostya's obfuscation, print \"YES\" (without quotes), otherwise print \"NO\".\n\n\n-----Examples-----\nInput\nabacaba\n\nOutput\nYES\n\nInput\njinotega\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample case, one possible list of identifiers would be \"number string number character number string number\". Here how Kostya would obfuscate the program:\n\n\n\n replace all occurences of number with a, the result would be \"a string a character a string a\",\n\n replace all occurences of string with b, the result would be \"a b a character a b a\",\n\n replace all occurences of character with c, the result would be \"a b a c a b a\",\n\n all identifiers have been replaced, thus the obfuscation is finished."} +{"id": "02788", "text": "Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on.\n\nDuring cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.\n\nIt is known that the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off.\n\n\n-----Input-----\n\nThe single line contains three integers k, d and t (1 \u2264 k, d, t \u2264 10^18).\n\n\n-----Output-----\n\nPrint a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10^{ - 9}.\n\nNamely, let's assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if $\\frac{|x - y|}{\\operatorname{max}(1, y)} \\leq 10^{-9}$.\n\n\n-----Examples-----\nInput\n3 2 6\n\nOutput\n6.5\n\nInput\n4 2 20\n\nOutput\n20.0\n\n\n\n-----Note-----\n\nIn the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for $\\frac{3}{6}$. Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for $\\frac{1}{12}$. Thus, after four minutes the chicken will be cooked for $\\frac{3}{6} + \\frac{1}{12} = \\frac{7}{12}$. Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready $\\frac{7}{12} + \\frac{2.5}{6} = 1$.\n\nIn the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes."} +{"id": "02789", "text": "All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.\n\nTreasure Island may be represented as a rectangular table $n \\times m$ which is surrounded by the ocean. Let us number rows of the field with consecutive integers from $1$ to $n$ from top to bottom and columns with consecutive integers from $1$ to $m$ from left to right. Denote the cell in $r$-th row and $c$-th column as $(r, c)$. Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell $(n, m)$.\n\nVasya got off the ship in cell $(1, 1)$. Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell $(x, y)$ he can move only to cells $(x+1, y)$ and $(x, y+1)$. Of course Vasya can't move through cells with impassable forests.\n\nEvil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells $(1, 1)$ where Vasya got off his ship and $(n, m)$ where the treasure is hidden.\n\nHelp Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.\n\n\n-----Input-----\n\nFirst line of input contains two positive integers $n$, $m$ ($3 \\le n \\cdot m \\le 1\\,000\\,000$), sizes of the island.\n\nFollowing $n$ lines contains strings $s_i$ of length $m$ describing the island, $j$-th character of string $s_i$ equals \"#\" if cell $(i, j)$ contains an impassable forest and \".\" if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell $(1, 1)$, i.e. the first cell of the first row, and he wants to reach cell $(n, m)$, i.e. the last cell of the last row.\n\nIt's guaranteed, that cells $(1, 1)$ and $(n, m)$ are empty.\n\n\n-----Output-----\n\nPrint the only integer $k$, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.\n\n\n-----Examples-----\nInput\n2 2\n..\n..\n\nOutput\n2\n\nInput\n4 4\n....\n#.#.\n....\n.#..\n\nOutput\n1\n\nInput\n3 4\n....\n.##.\n....\n\nOutput\n2\n\n\n\n-----Note-----\n\nThe following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from $(1, 1)$ to $(n, m)$. Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from $(1, 1)$ to $(n, m)$ impossible. [Image]"} +{"id": "02790", "text": "Takahashi is about to assemble a character figure, consisting of N parts called Part 1, Part 2, ..., Part N and N-1 connecting components. Parts are distinguishable, but connecting components are not.\nPart i has d_i holes, called Hole 1, Hole 2, ..., Hole d_i, into which a connecting component can be inserted. These holes in the parts are distinguishable.\nEach connecting component will be inserted into two holes in different parts, connecting these two parts. It is impossible to insert multiple connecting components into a hole.\nThe character figure is said to be complete when it has the following properties:\n - All of the N-1 components are used to connect parts.\n - Consider a graph with N vertices corresponding to the parts and N-1 undirected edges corresponding to the pairs of vertices connected by a connecting component. Then, this graph is connected.\nTwo ways A and B to make the figure complete are considered the same when the following is satisfied: for every pair of holes, A uses a connecting component to connect these holes if and only if B uses one to connect them.\nFind the number of ways to make the figure complete. Since the answer can be enormous, find the count modulo 998244353.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq d_i < 998244353\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nd_1 d_2 \\cdots d_N\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n3\n1 1 3\n\n-----Sample Output-----\n6\n\nOne way to make the figure complete is to connect Hole 1 in Part 1 and Hole 3 in Part 3 and then connect Hole 1 in Part 2 and Hole 1 in Part 3."} +{"id": "02791", "text": "You have an integer sequence of length N: a_1, a_2, ..., a_N.\nYou repeatedly perform the following operation until the length of the sequence becomes 1:\n - First, choose an element of the sequence.\n - If that element is at either end of the sequence, delete the element.\n - If that element is not at either end of the sequence, replace the element with the sum of the two elements that are adjacent to it. Then, delete those two elements.\nYou would like to maximize the final element that remains in the sequence.\nFind the maximum possible value of the final element, and the way to achieve it.\n\n-----Constraints-----\n - All input values are integers.\n - 2 \\leq N \\leq 1000\n - |a_i| \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\n - In the first line, print the maximum possible value of the final element in the sequence.\n - In the second line, print the number of operations that you perform.\n - In the (2+i)-th line, if the element chosen in the i-th operation is the x-th element from the left in the sequence at that moment, print x.\n - If there are multiple ways to achieve the maximum value of the final element, any of them may be printed.\n\n-----Sample Input-----\n5\n1 4 3 7 5\n\n-----Sample Output-----\n11\n3\n1\n4\n2\n\nThe sequence would change as follows:\n - After the first operation: 4, 3, 7, 5\n - After the second operation: 4, 3, 7\n - After the third operation: 11(4+7)"} +{"id": "02792", "text": "Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). \n\nThere are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all.\n\nIn this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away.\n\nFor example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and t (1 \u2264 n \u2264 200 000, 1 \u2264 t \u2264 10^9)\u00a0\u2014 the length of Efim's grade and the number of seconds till the end of the break respectively.\n\nThe second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0.\n\n\n-----Output-----\n\nPrint the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.\n\n\n-----Examples-----\nInput\n6 1\n10.245\n\nOutput\n10.25\n\nInput\n6 2\n10.245\n\nOutput\n10.3\n\nInput\n3 100\n9.2\n\nOutput\n9.2\n\n\n\n-----Note-----\n\nIn the first two samples Efim initially has grade 10.245. \n\nDuring the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect.\n\nIn the third sample the optimal strategy is to not perform any rounding at all."} +{"id": "02793", "text": "For each string s consisting of characters '0' and '1' one can define four integers a_00, a_01, a_10 and a_11, where a_{xy} is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. \n\nIn these problem you are given four integers a_00, a_01, a_10, a_11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000.\n\n\n-----Input-----\n\nThe only line of the input contains four non-negative integers a_00, a_01, a_10 and a_11. Each of them doesn't exceed 10^9.\n\n\n-----Output-----\n\nIf there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print \"Impossible\". The length of your answer must not exceed 1 000 000.\n\n\n-----Examples-----\nInput\n1 2 3 4\n\nOutput\nImpossible\n\nInput\n1 2 2 1\n\nOutput\n0110"} +{"id": "02794", "text": "A game field is a strip of 1 \u00d7 n square cells. In some cells there are Packmen, in some cells\u00a0\u2014 asterisks, other cells are empty.\n\nPackman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.\n\nIn the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.\n\nYour task is to determine minimum possible time after which Packmen can eat all the asterisks.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 10^5) \u2014 the length of the game field.\n\nThe second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i \u2014 the cell i is empty. If there is symbol '*' in position i \u2014 in the cell i contains an asterisk. If there is symbol 'P' in position i \u2014 Packman is in the cell i.\n\nIt is guaranteed that on the game field there is at least one Packman and at least one asterisk.\n\n\n-----Output-----\n\nPrint minimum possible time after which Packmen can eat all asterisks.\n\n\n-----Examples-----\nInput\n7\n*..P*P*\n\nOutput\n3\n\nInput\n10\n.**PP.*P.*\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.\n\nIn the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field."} +{"id": "02795", "text": "Imagine you have an infinite 2D plane with Cartesian coordinate system. Some of the integral points are blocked, and others are not. Two integral points A and B on the plane are 4-connected if and only if: the Euclidean distance between A and B is one unit and neither A nor B is blocked; or there is some integral point C, such that A is 4-connected with C, and C is 4-connected with B. \n\nLet's assume that the plane doesn't contain blocked points. Consider all the integral points of the plane whose Euclidean distance from the origin is no more than n, we'll name these points special. Chubby Yang wants to get the following property: no special point is 4-connected to some non-special point. To get the property she can pick some integral points of the plane and make them blocked. What is the minimum number of points she needs to pick?\n\n\n-----Input-----\n\nThe first line contains an integer n (0 \u2264 n \u2264 4\u00b710^7).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of points that should be blocked.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n4\n\nInput\n2\n\nOutput\n8\n\nInput\n3\n\nOutput\n16"} +{"id": "02796", "text": "You are given a rectangle grid. That grid's size is n \u00d7 m. Let's denote the coordinate system on the grid. So, each point on the grid will have coordinates \u2014 a pair of integers (x, y) (0 \u2264 x \u2264 n, 0 \u2264 y \u2264 m).\n\nYour task is to find a maximum sub-rectangle on the grid (x_1, y_1, x_2, y_2) so that it contains the given point (x, y), and its length-width ratio is exactly (a, b). In other words the following conditions must hold: 0 \u2264 x_1 \u2264 x \u2264 x_2 \u2264 n, 0 \u2264 y_1 \u2264 y \u2264 y_2 \u2264 m, $\\frac{x_{2} - x_{1}}{y_{2} - y_{1}} = \\frac{a}{b}$.\n\nThe sides of this sub-rectangle should be parallel to the axes. And values x_1, y_1, x_2, y_2 should be integers. [Image] \n\nIf there are multiple solutions, find the rectangle which is closest to (x, y). Here \"closest\" means the Euclid distance between (x, y) and the center of the rectangle is as small as possible. If there are still multiple solutions, find the lexicographically minimum one. Here \"lexicographically minimum\" means that we should consider the sub-rectangle as sequence of integers (x_1, y_1, x_2, y_2), so we can choose the lexicographically minimum one.\n\n\n-----Input-----\n\nThe first line contains six integers n, m, x, y, a, b (1 \u2264 n, m \u2264 10^9, 0 \u2264 x \u2264 n, 0 \u2264 y \u2264 m, 1 \u2264 a \u2264 n, 1 \u2264 b \u2264 m).\n\n\n-----Output-----\n\nPrint four integers x_1, y_1, x_2, y_2, which represent the founded sub-rectangle whose left-bottom point is (x_1, y_1) and right-up point is (x_2, y_2).\n\n\n-----Examples-----\nInput\n9 9 5 5 2 1\n\nOutput\n1 3 9 7\n\nInput\n100 100 52 50 46 56\n\nOutput\n17 8 86 92"} +{"id": "02797", "text": "A robot is put at the origin in a two-dimensional plane.\nInitially, the robot is facing in the positive x-axis direction.\nThis robot will be given an instruction sequence s.\ns consists of the following two kinds of letters, and will be executed in order from front to back.\n - F : Move in the current direction by distance 1.\n - T : Turn 90 degrees, either clockwise or counterclockwise.\nThe objective of the robot is to be at coordinates (x, y) after all the instructions are executed.\nDetermine whether this objective is achievable.\n\n-----Constraints-----\n - s consists of F and T.\n - 1 \\leq |s| \\leq 8 000\n - x and y are integers.\n - |x|, |y| \\leq |s|\n\n-----Input-----\nInput is given from Standard Input in the following format:\ns\nx y\n\n-----Output-----\nIf the objective is achievable, print Yes; if it is not, print No.\n\n-----Sample Input-----\nFTFFTFFF\n4 2\n\n-----Sample Output-----\nYes\n\nThe objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T."} +{"id": "02798", "text": "Fox Ciel studies number theory.\n\nShe thinks a non-empty set S contains non-negative integers is perfect if and only if for any $a, b \\in S$ (a can be equal to b), $(a \\text{xor} b) \\in S$. Where operation xor means exclusive or operation (http://en.wikipedia.org/wiki/Exclusive_or).\n\nPlease calculate the number of perfect sets consisting of integers not greater than k. The answer can be very large, so print it modulo 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe first line contains an integer k (0 \u2264 k \u2264 10^9).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of required sets modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n1\n\nOutput\n2\n\nInput\n2\n\nOutput\n3\n\nInput\n3\n\nOutput\n5\n\nInput\n4\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn example 1, there are 2 such sets: {0} and {0, 1}. Note that {1} is not a perfect set since 1 xor 1 = 0 and {1} doesn't contain zero.\n\nIn example 4, there are 6 such sets: {0}, {0, 1}, {0, 2}, {0, 3}, {0, 4} and {0, 1, 2, 3}."} +{"id": "02799", "text": "Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters.\n\nMaxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (a_{i}) represents the number of meters the guest is going to take up if he sits at the restaurant table.\n\nLong before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than p. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table.\n\nMaxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible n! orders of guests in the queue. Help Maxim, calculate this number.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 50) \u2014 the number of guests in the restaurant. The next line contains integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 50) \u2014 the guests' sizes in meters. The third line contains integer p (1 \u2264 p \u2264 50) \u2014 the table's length in meters. \n\nThe numbers in the lines are separated by single spaces.\n\n\n-----Output-----\n\nIn a single line print a real number \u2014 the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10^{ - 4}.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n3\n\nOutput\n1.3333333333\n\n\n\n-----Note-----\n\nIn the first sample the people will come in the following orders: (1, 2, 3) \u2014 there will be two people in the restaurant; (1, 3, 2) \u2014 there will be one person in the restaurant; (2, 1, 3) \u2014 there will be two people in the restaurant; (2, 3, 1) \u2014 there will be one person in the restaurant; (3, 1, 2) \u2014 there will be one person in the restaurant; (3, 2, 1) \u2014 there will be one person in the restaurant. \n\nIn total we get (2 + 1 + 2 + 1 + 1 + 1) / 6 = 8 / 6 = 1.(3)."} +{"id": "02800", "text": "Jon Snow now has to fight with White Walkers. He has n rangers, each of which has his own strength. Also Jon Snow has his favourite number x. Each ranger can fight with a white walker only if the strength of the white walker equals his strength. He however thinks that his rangers are weak and need to improve. Jon now thinks that if he takes the bitwise XOR of strengths of some of rangers with his favourite number x, he might get soldiers of high strength. So, he decided to do the following operation k times: \n\n Arrange all the rangers in a straight line in the order of increasing strengths.\n\n Take the bitwise XOR (is written as $\\oplus$) of the strength of each alternate ranger with x and update it's strength.\n\n Suppose, Jon has 5 rangers with strengths [9, 7, 11, 15, 5] and he performs the operation 1 time with x = 2. He first arranges them in the order of their strengths, [5, 7, 9, 11, 15]. Then he does the following: \n\n The strength of first ranger is updated to $5 \\oplus 2$, i.e. 7.\n\n The strength of second ranger remains the same, i.e. 7.\n\n The strength of third ranger is updated to $9 \\oplus 2$, i.e. 11.\n\n The strength of fourth ranger remains the same, i.e. 11.\n\n The strength of fifth ranger is updated to $15 \\oplus 2$, i.e. 13.\n\n The new strengths of the 5 rangers are [7, 7, 11, 11, 13]\n\nNow, Jon wants to know the maximum and minimum strength of the rangers after performing the above operations k times. He wants your help for this task. Can you help him?\n\n\n-----Input-----\n\nFirst line consists of three integers n, k, x (1 \u2264 n \u2264 10^5, 0 \u2264 k \u2264 10^5, 0 \u2264 x \u2264 10^3) \u2014 number of rangers Jon has, the number of times Jon will carry out the operation and Jon's favourite number respectively.\n\nSecond line consists of n integers representing the strengths of the rangers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^3).\n\n\n-----Output-----\n\nOutput two integers, the maximum and the minimum strength of the rangers after performing the operation k times.\n\n\n-----Examples-----\nInput\n5 1 2\n9 7 11 15 5\n\nOutput\n13 7\nInput\n2 100000 569\n605 986\n\nOutput\n986 605"} +{"id": "02801", "text": "In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by a_{i}. Each extension can't be used more than once, the extensions can be used in any order.\n\nNow Arkady's field has size h \u00d7 w. He wants to enlarge it so that it is possible to place a rectangle of size a \u00d7 b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal.\n\n\n-----Input-----\n\nThe first line contains five integers a, b, h, w and n (1 \u2264 a, b, h, w, n \u2264 100 000)\u00a0\u2014 the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (2 \u2264 a_{i} \u2264 100 000), where a_{i} equals the integer a side multiplies by when the i-th extension is applied.\n\n\n-----Output-----\n\nPrint the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0.\n\n\n-----Examples-----\nInput\n3 3 2 4 4\n2 5 4 10\n\nOutput\n1\n\nInput\n3 3 3 3 5\n2 3 5 4 2\n\nOutput\n0\n\nInput\n5 5 1 2 3\n2 2 3\n\nOutput\n-1\n\nInput\n3 4 1 1 3\n2 3 2\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field."} +{"id": "02802", "text": "Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.\n\nOverall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has).\n\nThe aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints. \n\nA color hint goes like that: a player names some color and points at all the cards of this color. \n\nSimilarly goes the value hint. A player names some value and points at all the cards that contain the value.\n\nDetermine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 100) \u2014 the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters \u2014 R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of hints that the other players should make.\n\n\n-----Examples-----\nInput\n2\nG3 G3\n\nOutput\n0\n\nInput\n4\nG4 R4 R3 B3\n\nOutput\n2\n\nInput\n5\nB1 Y1 W1 G1 R1\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample Borya already knows for each card that it is a green three.\n\nIn the second sample we can show all fours and all red cards.\n\nIn the third sample you need to make hints about any four colors."} +{"id": "02803", "text": "Nick has n bottles of soda left after his birthday. Each bottle is described by two values: remaining amount of soda a_{i} and bottle volume b_{i} (a_{i} \u2264 b_{i}).\n\nNick has decided to pour all remaining soda into minimal number of bottles, moreover he has to do it as soon as possible. Nick spends x seconds to pour x units of soda from one bottle to another.\n\nNick asks you to help him to determine k \u2014 the minimal number of bottles to store all remaining soda and t \u2014 the minimal time to pour soda into k bottles. A bottle can't store more soda than its volume. All remaining soda should be saved.\n\n\n-----Input-----\n\nThe first line contains positive integer n (1 \u2264 n \u2264 100) \u2014 the number of bottles.\n\nThe second line contains n positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 100), where a_{i} is the amount of soda remaining in the i-th bottle.\n\nThe third line contains n positive integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 100), where b_{i} is the volume of the i-th bottle.\n\nIt is guaranteed that a_{i} \u2264 b_{i} for any i.\n\n\n-----Output-----\n\nThe only line should contain two integers k and t, where k is the minimal number of bottles that can store all the soda and t is the minimal time to pour the soda into k bottles.\n\n\n-----Examples-----\nInput\n4\n3 3 4 3\n4 7 6 5\n\nOutput\n2 6\n\nInput\n2\n1 1\n100 100\n\nOutput\n1 1\n\nInput\n5\n10 30 5 6 24\n10 41 7 8 24\n\nOutput\n3 11\n\n\n\n-----Note-----\n\nIn the first example Nick can pour soda from the first bottle to the second bottle. It will take 3 seconds. After it the second bottle will contain 3 + 3 = 6 units of soda. Then he can pour soda from the fourth bottle to the second bottle and to the third bottle: one unit to the second and two units to the third. It will take 1 + 2 = 3 seconds. So, all the soda will be in two bottles and he will spend 3 + 3 = 6 seconds to do it."} +{"id": "02804", "text": "Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either 'A', or the letter 'B'. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter 'A', Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter 'B', then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over.\n\nYou know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with.\n\n\n-----Input-----\n\nThe first line of the input contains two integers, x, y (1 \u2264 x, y \u2264 10^18, xy > 1) \u2014 the number of oranges and apples that were initially in the bag.\n\n\n-----Output-----\n\nPrint any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 10^6 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 10^6 characters. If there are several possible answers, you are allowed to print any of them.\n\nIf the sequence of cards that meet the problem statement does not not exist, print a single word Impossible.\n\n\n-----Examples-----\nInput\n1 4\n\nOutput\n3B\n\nInput\n2 2\n\nOutput\nImpossible\n\nInput\n3 2\n\nOutput\n1A1B\n\n\n\n-----Note-----\n\nIn the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples.\n\nIn second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges.\n\nIn the third sample, cards contain letters 'AB', so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples."} +{"id": "02805", "text": "As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where p is an odd prime number, the functional equation states that $f(k x \\operatorname{mod} p) \\equiv k \\cdot f(x) \\operatorname{mod} p$ \n\nfor some function $f : \\{0,1,2, \\cdots, p - 1 \\} \\rightarrow \\{0,1,2, \\cdots, p - 1 \\}$. (This equation should hold for any integer x in the range 0 to p - 1, inclusive.)\n\nIt turns out that f can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions f that satisfy this equation. Since the answer may be very large, you should print your result modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe input consists of two space-separated integers p and k (3 \u2264 p \u2264 1 000 000, 0 \u2264 k \u2264 p - 1) on a single line. It is guaranteed that p is an odd prime number.\n\n\n-----Output-----\n\nPrint a single integer, the number of distinct functions f modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n3 2\n\nOutput\n3\n\nInput\n5 4\n\nOutput\n25\n\n\n\n-----Note-----\n\nIn the first sample, p = 3 and k = 2. The following functions work: f(0) = 0, f(1) = 1, f(2) = 2. f(0) = 0, f(1) = 2, f(2) = 1. f(0) = f(1) = f(2) = 0."} +{"id": "02806", "text": "Given is a simple undirected graph with N vertices and M edges.\nIts vertices are numbered 1, 2, \\ldots, N and its edges are numbered 1, 2, \\ldots, M.\nOn Vertex i (1 \\leq i \\leq N) two integers A_i and B_i are written.\nEdge i (1 \\leq i \\leq M) connects Vertices U_i and V_i.\nSnuke picks zero or more vertices and delete them.\nDeleting Vertex i costs A_i.\nWhen a vertex is deleted, edges that are incident to the vertex are also deleted.\nThe score after deleting vertices is calculated as follows:\n - The score is the sum of the scores of all connected components.\n - The score of a connected component is the absolute value of the sum of B_i of the vertices in the connected component.\nSnuke's profit is (score) - (the sum of costs).\nFind the maximum possible profit Snuke can gain.\n\n-----Constraints-----\n - 1 \\leq N \\leq 300\n - 1 \\leq M \\leq 300\n - 1 \\leq A_i \\leq 10^6\n - -10^6 \\leq B_i \\leq 10^6\n - 1 \\leq U_i,V_i \\leq N\n - The given graph does not contain self loops or multiple edges.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 A_2 \\cdots A_N\nB_1 B_2 \\cdots B_N\nU_1 V_1\nU_2 V_2\n\\vdots\nU_M V_M\n\n-----Output-----\nPrint the maximum possible profit Snuke can gain.\n\n-----Sample Input-----\n4 4\n4 1 2 3\n0 2 -3 1\n1 2\n2 3\n3 4\n4 2\n\n-----Sample Output-----\n1\n\nDeleting Vertex 2 costs 1.\nAfter that, the graph is separated into two connected components.\nThe score of the component consisting of Vertex 1 is |0| = 0. The score of the component consisting of Vertices 3 and 4 is |(-3) + 1| = 2.\nTherefore, Snuke's profit is 0 + 2 - 1 = 1.\nHe cannot gain more than 1, so the answer is 1."} +{"id": "02807", "text": "There is a pond with a rectangular shape.\nThe pond is divided into a grid with H rows and W columns of squares.\nWe will denote the square at the i-th row from the top and j-th column from the left by (i,\\ j).\nSome of the squares in the pond contains a lotus leaf floating on the water.\nOn one of those leaves, S, there is a frog trying to get to another leaf T.\nThe state of square (i,\\ j) is given to you by a character a_{ij}, as follows:\n - . : A square without a leaf.\n - o : A square with a leaf floating on the water.\n - S : A square with the leaf S.\n - T : A square with the leaf T.\nThe frog will repeatedly perform the following action to get to the leaf T: \"jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located.\"\nSnuke is trying to remove some of the leaves, other than S and T, so that the frog cannot get to the leaf T.\nDetermine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.\n\n-----Constraints-----\n - 2 \u2264 H, W \u2264 100\n - a_{ij} is ., o, S or T.\n - There is exactly one S among a_{ij}.\n - There is exactly one T among a_{ij}.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\na_{11} ... a_{1W}\n:\na_{H1} ... a_{HW}\n\n-----Output-----\nIf the objective is achievable, print the minimum necessary number of leaves to remove.\nOtherwise, print -1 instead.\n\n-----Sample Input-----\n3 3\nS.o\n.o.\no.T\n\n-----Sample Output-----\n2\n\nRemove the upper-right and lower-left leaves."} +{"id": "02808", "text": "Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.\n\nHowever, all Mike has is lots of identical resistors with unit resistance R_0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements: one resistor; an element and one resistor plugged in sequence; an element and one resistor plugged in parallel. [Image] \n\nWith the consecutive connection the resistance of the new element equals R = R_{e} + R_0. With the parallel connection the resistance of the new element equals $R = \\frac{1}{\\frac{1}{R_{e}} + \\frac{1}{R_{0}}}$. In this case R_{e} equals the resistance of the element being connected.\n\nMike needs to assemble an element with a resistance equal to the fraction $\\frac{a}{b}$. Determine the smallest possible number of resistors he needs to make such an element.\n\n\n-----Input-----\n\nThe single input line contains two space-separated integers a and b (1 \u2264 a, b \u2264 10^18). It is guaranteed that the fraction $\\frac{a}{b}$ is irreducible. It is guaranteed that a solution always exists.\n\n\n-----Output-----\n\nPrint a single number \u2014 the answer to the problem.\n\nPlease do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is recommended to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n1\n\nInput\n3 2\n\nOutput\n3\n\nInput\n199 200\n\nOutput\n200\n\n\n\n-----Note-----\n\nIn the first sample, one resistor is enough.\n\nIn the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance $\\frac{1}{\\frac{1}{1} + \\frac{1}{1}} + 1 = \\frac{3}{2}$. We cannot make this element using two resistors."} +{"id": "02809", "text": "Takahashi and Aoki are playing a stone-taking game. Initially, there are N piles of stones, and the i-th pile contains A_i stones and has an associated integer K_i.\nStarting from Takahashi, Takahashi and Aoki take alternate turns to perform the following operation:\n - Choose a pile. If the i-th pile is selected and there are X stones left in the pile, remove some number of stones between 1 and floor(X/K_i) (inclusive) from the pile.\nThe player who first becomes unable to perform the operation loses the game. Assuming that both players play optimally, determine the winner of the game.\nHere, floor(x) represents the largest integer not greater than x.\n\n-----Constraints-----\n - 1 \\leq N \\leq 200\n - 1 \\leq A_i,K_i \\leq 10^9\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 K_1\n:\nA_N K_N\n\n-----Output-----\nIf Takahashi will win, print Takahashi; if Aoki will win, print Aoki.\n\n-----Sample Input-----\n2\n5 2\n3 3\n\n-----Sample Output-----\nAoki\n\nInitially, from the first pile at most floor(5/2)=2 stones can be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.\n - If Takahashi first takes two stones from the first pile, from the first pile at most floor(3/2)=1 stone can now be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.\n - Then, if Aoki takes one stone from the second pile, from the first pile at most floor(3/2)=1 stone can be removed at a time, and from the second pile no more stones can be removed (since floor(2/3)=0).\n - Then, if Takahashi takes one stone from the first pile, from the first pile at most floor(2/2)=1 stone can now be removed at a time, and from the second pile no more stones can be removed.\n - Then, if Aoki takes one stone from the first pile, from the first pile at most floor(1/2)=0 stones can now be removed at a time, and from the second pile no more stones can be removed.\nNo more operation can be performed, thus Aoki wins. If Takahashi plays differently, Aoki can also win by play accordingly."} +{"id": "02810", "text": "Little C loves number \u00ab3\u00bb very much. He loves all things about it.\n\nNow he is playing a game on a chessboard of size $n \\times m$. The cell in the $x$-th row and in the $y$-th column is called $(x,y)$. Initially, The chessboard is empty. Each time, he places two chessmen on two different empty cells, the Manhattan distance between which is exactly $3$. The Manhattan distance between two cells $(x_i,y_i)$ and $(x_j,y_j)$ is defined as $|x_i-x_j|+|y_i-y_j|$.\n\nHe want to place as many chessmen as possible on the chessboard. Please help him find the maximum number of chessmen he can place.\n\n\n-----Input-----\n\nA single line contains two integers $n$ and $m$ ($1 \\leq n,m \\leq 10^9$) \u2014 the number of rows and the number of columns of the chessboard.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of chessmen Little C can place.\n\n\n-----Examples-----\nInput\n2 2\n\nOutput\n0\nInput\n3 3\n\nOutput\n8\n\n\n-----Note-----\n\nIn the first example, the Manhattan distance between any two cells is smaller than $3$, so the answer is $0$.\n\nIn the second example, a possible solution is $(1,1)(3,2)$, $(1,2)(3,3)$, $(2,1)(1,3)$, $(3,1)(2,3)$."} +{"id": "02811", "text": "Two participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a communication channel you have access to without revealing it to you.\n\nBoth participants communicated to each other a set of pairs of numbers, that includes the pair given to them. Each pair in the communicated sets comprises two different numbers.\n\nDetermine if you can with certainty deduce the common number, or if you can determine with certainty that both participants know the number but you do not.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 12$) \u2014 the number of pairs the first participant communicated to the second and vice versa.\n\nThe second line contains $n$ pairs of integers, each between $1$ and $9$, \u2014 pairs of numbers communicated from first participant to the second.\n\nThe third line contains $m$ pairs of integers, each between $1$ and $9$, \u2014 pairs of numbers communicated from the second participant to the first.\n\nAll pairs within each set are distinct (in particular, if there is a pair $(1,2)$, there will be no pair $(2,1)$ within the same set), and no pair contains the same number twice.\n\nIt is guaranteed that the two sets do not contradict the statements, in other words, there is pair from the first set and a pair from the second set that share exactly one number.\n\n\n-----Output-----\n\nIf you can deduce the shared number with certainty, print that number.\n\nIf you can with certainty deduce that both participants know the shared number, but you do not know it, print $0$.\n\nOtherwise print $-1$.\n\n\n-----Examples-----\nInput\n2 2\n1 2 3 4\n1 5 3 4\n\nOutput\n1\n\nInput\n2 2\n1 2 3 4\n1 5 6 4\n\nOutput\n0\n\nInput\n2 3\n1 2 4 5\n1 2 1 3 2 3\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example the first participant communicated pairs $(1,2)$ and $(3,4)$, and the second communicated $(1,5)$, $(3,4)$. Since we know that the actual pairs they received share exactly one number, it can't be that they both have $(3,4)$. Thus, the first participant has $(1,2)$ and the second has $(1,5)$, and at this point you already know the shared number is $1$.\n\nIn the second example either the first participant has $(1,2)$ and the second has $(1,5)$, or the first has $(3,4)$ and the second has $(6,4)$. In the first case both of them know the shared number is $1$, in the second case both of them know the shared number is $4$. You don't have enough information to tell $1$ and $4$ apart.\n\nIn the third case if the first participant was given $(1,2)$, they don't know what the shared number is, since from their perspective the second participant might have been given either $(1,3)$, in which case the shared number is $1$, or $(2,3)$, in which case the shared number is $2$. While the second participant does know the number with certainty, neither you nor the first participant do, so the output is $-1$."} +{"id": "02812", "text": "You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.\n\nYou are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.\n\nFor example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.\n\n\n-----Input-----\n\nThe first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively.\n\nThe second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.\n\n\n-----Output-----\n\nThe only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.\n\n\n-----Examples-----\nInput\n24\n17:30\n\nOutput\n17:30\n\nInput\n12\n17:30\n\nOutput\n07:30\n\nInput\n24\n99:99\n\nOutput\n09:09"} +{"id": "02813", "text": "We have an undirected weighted graph with N vertices and M edges.\nThe i-th edge in the graph connects Vertex U_i and Vertex V_i, and has a weight of W_i.\nAdditionally, you are given an integer X.\nFind the number of ways to paint each edge in this graph either white or black such that the following condition is met, modulo 10^9 + 7:\n - The graph has a spanning tree that contains both an edge painted white and an edge painted black. Furthermore, among such spanning trees, the one with the smallest weight has a weight of X.\nHere, the weight of a spanning tree is the sum of the weights of the edges contained in the spanning tree.\n\n-----Constraints-----\n - 1 \\leq N \\leq 1 000\n - 1 \\leq M \\leq 2 000\n - 1 \\leq U_i, V_i \\leq N (1 \\leq i \\leq M)\n - 1 \\leq W_i \\leq 10^9 (1 \\leq i \\leq M)\n - If i \\neq j, then (U_i, V_i) \\neq (U_j, V_j) and (U_i, V_i) \\neq (V_j, U_j).\n - U_i \\neq V_i (1 \\leq i \\leq M)\n - The given graph is connected.\n - 1 \\leq X \\leq 10^{12}\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nX\nU_1 V_1 W_1\nU_2 V_2 W_2\n:\nU_M V_M W_M\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n3 3\n2\n1 2 1\n2 3 1\n3 1 1\n\n-----Sample Output-----\n6\n"} +{"id": "02814", "text": "To improve the boomerang throwing skills of the animals, Zookeeper has set up an $n \\times n$ grid with some targets, where each row and each column has at most $2$ targets each. The rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $n$ from left to right. \n\n For each column, Zookeeper will throw a boomerang from the bottom of the column (below the grid) upwards. When the boomerang hits any target, it will bounce off, make a $90$ degree turn to the right and fly off in a straight line in its new direction. The boomerang can hit multiple targets and does not stop until it leaves the grid.\n\n [Image] \n\nIn the above example, $n=6$ and the black crosses are the targets. The boomerang in column $1$ (blue arrows) bounces $2$ times while the boomerang in column $3$ (red arrows) bounces $3$ times.\n\n The boomerang in column $i$ hits exactly $a_i$ targets before flying out of the grid. It is known that $a_i \\leq 3$.\n\nHowever, Zookeeper has lost the original positions of the targets. Thus, he asks you to construct a valid configuration of targets that matches the number of hits for each column, or tell him that no such configuration exists. If multiple valid configurations exist, you may print any of them.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ $(1 \\leq n \\leq 10^5)$.\n\n The next line contains $n$ integers $a_1,a_2,\\ldots,a_n$ $(0 \\leq a_i \\leq 3)$.\n\n\n-----Output-----\n\nIf no configuration of targets exist, print $-1$.\n\n Otherwise, on the first line print a single integer $t$ $(0 \\leq t \\leq 2n)$: the number of targets in your configuration. \n\n Then print $t$ lines with two spaced integers each per line. Each line should contain two integers $r$ and $c$ $(1 \\leq r,c \\leq n)$, where $r$ is the target's row and $c$ is the target's column. All targets should be different. \n\n Every row and every column in your configuration should have at most two targets each. \n\n\n-----Examples-----\nInput\n6\n2 0 3 0 1 1\n\nOutput\n5\n2 1\n2 5\n3 3\n3 6\n5 6\n\nInput\n1\n0\n\nOutput\n0\n\nInput\n6\n3 2 2 2 1 1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nFor the first test, the answer configuration is the same as in the picture from the statement.\n\n For the second test, the boomerang is not supposed to hit anything, so we can place $0$ targets.\n\n For the third test, the following configuration of targets matches the number of hits, but is not allowed as row $3$ has $4$ targets.\n\n [Image] \n\nIt can be shown for this test case that no valid configuration of targets will result in the given number of target hits."} +{"id": "02815", "text": "Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars.\n\nThere are $n$ banknote denominations on Mars: the value of $i$-th banknote is $a_i$. Natasha has an infinite number of banknotes of each denomination.\n\nMartians have $k$ fingers on their hands, so they use a number system with base $k$. In addition, the Martians consider the digit $d$ (in the number system with base $k$) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base $k$ is $d$, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet.\n\nDetermine for which values $d$ Natasha can make the Martians happy.\n\nNatasha can use only her banknotes. Martians don't give her change.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n \\le 100\\,000$, $2 \\le k \\le 100\\,000$)\u00a0\u2014 the number of denominations of banknotes and the base of the number system on Mars.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$)\u00a0\u2014 denominations of banknotes on Mars.\n\nAll numbers are given in decimal notation.\n\n\n-----Output-----\n\nOn the first line output the number of values $d$ for which Natasha can make the Martians happy.\n\nIn the second line, output all these values in increasing order.\n\nPrint all numbers in decimal notation.\n\n\n-----Examples-----\nInput\n2 8\n12 20\n\nOutput\n2\n0 4 \nInput\n3 10\n10 20 30\n\nOutput\n1\n0 \n\n\n-----Note-----\n\nConsider the first test case. It uses the octal number system.\n\nIf you take one banknote with the value of $12$, you will get $14_8$ in octal system. The last digit is $4_8$.\n\nIf you take one banknote with the value of $12$ and one banknote with the value of $20$, the total value will be $32$. In the octal system, it is $40_8$. The last digit is $0_8$.\n\nIf you take two banknotes with the value of $20$, the total value will be $40$, this is $50_8$ in the octal system. The last digit is $0_8$.\n\nNo other digits other than $0_8$ and $4_8$ can be obtained. Digits $0_8$ and $4_8$ could also be obtained in other ways.\n\nThe second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero."} +{"id": "02816", "text": "A team of furry rescue rangers was sitting idle in their hollow tree when suddenly they received a signal of distress. In a few moments they were ready, and the dirigible of the rescue chipmunks hit the road.\n\nWe assume that the action takes place on a Cartesian plane. The headquarters of the rescuers is located at point (x_1, y_1), and the distress signal came from the point (x_2, y_2).\n\nDue to Gadget's engineering talent, the rescuers' dirigible can instantly change its current velocity and direction of movement at any moment and as many times as needed. The only limitation is: the speed of the aircraft relative to the air can not exceed $v_{\\operatorname{max}}$ meters per second.\n\nOf course, Gadget is a true rescuer and wants to reach the destination as soon as possible. The matter is complicated by the fact that the wind is blowing in the air and it affects the movement of the dirigible. According to the weather forecast, the wind will be defined by the vector (v_{x}, v_{y}) for the nearest t seconds, and then will change to (w_{x}, w_{y}). These vectors give both the direction and velocity of the wind. Formally, if a dirigible is located at the point (x, y), while its own velocity relative to the air is equal to zero and the wind (u_{x}, u_{y}) is blowing, then after $T$ seconds the new position of the dirigible will be $(x + \\tau \\cdot u_{x}, y + \\tau \\cdot u_{y})$.\n\nGadget is busy piloting the aircraft, so she asked Chip to calculate how long will it take them to reach the destination if they fly optimally. He coped with the task easily, but Dale is convinced that Chip has given the random value, aiming only not to lose the face in front of Gadget. Dale has asked you to find the right answer.\n\nIt is guaranteed that the speed of the wind at any moment of time is strictly less than the maximum possible speed of the airship relative to the air.\n\n\n-----Input-----\n\nThe first line of the input contains four integers x_1, y_1, x_2, y_2 (|x_1|, |y_1|, |x_2|, |y_2| \u2264 10 000)\u00a0\u2014 the coordinates of the rescuers' headquarters and the point, where signal of the distress came from, respectively. \n\nThe second line contains two integers $v_{\\operatorname{max}}$ and t (0 < v, t \u2264 1000), which are denoting the maximum speed of the chipmunk dirigible relative to the air and the moment of time when the wind changes according to the weather forecast, respectively. \n\nNext follow one per line two pairs of integer (v_{x}, v_{y}) and (w_{x}, w_{y}), describing the wind for the first t seconds and the wind that will blow at all the remaining time, respectively. It is guaranteed that $v_{x}^{2} + v_{y}^{2} < v_{\\operatorname{max}}^{2}$ and $w_{x}^{2} + w_{y}^{2} < v_{\\operatorname{max}}^{2}$.\n\n\n-----Output-----\n\nPrint a single real value\u00a0\u2014 the minimum time the rescuers need to get to point (x_2, y_2). You answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. \n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n0 0 5 5\n3 2\n-1 -1\n-1 0\n\nOutput\n3.729935587093555327\n\nInput\n0 0 0 1000\n100 1000\n-50 0\n50 0\n\nOutput\n11.547005383792516398"} +{"id": "02817", "text": "We have N bags numbered 1 through N and N dishes numbered 1 through N.\nBag i contains a_i coins, and each dish has nothing on it initially.\nTaro the first and Jiro the second will play a game against each other.\nThey will alternately take turns, with Taro the first going first.\nIn each player's turn, the player can make one of the following two moves:\n - When one or more bags contain coin(s): Choose one bag that contains coin(s) and one dish, then move all coins in the chosen bag onto the chosen dish. (The chosen dish may already have coins on it, or not.)\n - When no bag contains coins: Choose one dish with coin(s) on it, then remove one or more coins from the chosen dish.\nThe player who first becomes unable to make a move loses. Determine the winner of the game when the two players play optimally.\nYou are given T test cases. Solve each of them.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq T \\leq 10^5\n - 1 \\leq N \\leq 10^{5}\n - 1 \\leq a_i \\leq 10^9\n - In one input file, the sum of N does not exceed 2 \\times 10^5.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nT\n\\mathrm{case}_1\n\\vdots\n\\mathrm{case}_T\n\nEach case is in the following format:\nN\na_1 a_2 \\cdots a_N\n\n-----Output-----\nPrint T lines. The i-th line should contain First if Taro the first wins in the i-th test case, and Second if Jiro the second wins in the test case.\n\n-----Sample Input-----\n3\n1\n10\n2\n1 2\n21\n476523737 103976339 266993 706803678 802362985 892644371 953855359 196462821 817301757 409460796 773943961 488763959 405483423 616934516 710762957 239829390 55474813 818352359 312280585 185800870 255245162\n\n-----Sample Output-----\nSecond\nFirst\nSecond\n\n - In test case 1, Jiro the second wins. Below is one sequence of moves that results in Jiro's win:\n - In Taro the first's turn, he can only choose Bag 1 and move the coins onto Dish 1.\n - In Jiro the second's turn, he can choose Dish 1 and remove all coins from it, making Taro fail to make a move and lose.\n - Note that when there is a bag that contains coin(s), a player can only make a move in which he chooses a bag that contains coin(s) and moves the coin(s) onto a dish.\n - Similarly, note that when there is no bag that contains coin(s), a player can only make a move in which he chooses a dish and removes one or more coins."} +{"id": "02818", "text": "You are given an integer sequence A of length N and an integer K.\nYou will perform the following operation on this sequence Q times:\n - Choose a contiguous subsequence of length K, then remove the smallest element among the K elements contained in the chosen subsequence (if there are multiple such elements, choose one of them as you like).\nLet X and Y be the values of the largest and smallest element removed in the Q operations. You would like X-Y to be as small as possible.\nFind the smallest possible value of X-Y when the Q operations are performed optimally.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2000\n - 1 \\leq K \\leq N\n - 1 \\leq Q \\leq N-K+1\n - 1 \\leq A_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K Q\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the smallest possible value of X-Y.\n\n-----Sample Input-----\n5 3 2\n4 3 1 5 2\n\n-----Sample Output-----\n1\n\nIn the first operation, whichever contiguous subsequence of length 3 we choose, the minimum element in it is 1.\nThus, the first operation removes A_3=1 and now we have A=(4,3,5,2).\nIn the second operation, it is optimal to choose (A_2,A_3,A_4)=(3,5,2) as the contiguous subsequence of length 3 and remove A_4=2.\nIn this case, the largest element removed is 2, and the smallest is 1, so their difference is 2-1=1."} +{"id": "02819", "text": "You have a team of N people. For a particular task, you can pick any non-empty subset of people. The cost of having x people for the task is x^{k}. \n\nOutput the sum of costs over all non-empty subsets of people.\n\n\n-----Input-----\n\nOnly line of input contains two integers N (1 \u2264 N \u2264 10^9) representing total number of people and k (1 \u2264 k \u2264 5000).\n\n\n-----Output-----\n\nOutput the sum of costs for all non empty subsets modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n1\n\nInput\n3 2\n\nOutput\n24\n\n\n\n-----Note-----\n\nIn the first example, there is only one non-empty subset {1} with cost 1^1 = 1.\n\nIn the second example, there are seven non-empty subsets.\n\n- {1} with cost 1^2 = 1\n\n- {2} with cost 1^2 = 1\n\n- {1, 2} with cost 2^2 = 4\n\n- {3} with cost 1^2 = 1\n\n- {1, 3} with cost 2^2 = 4\n\n- {2, 3} with cost 2^2 = 4\n\n- {1, 2, 3} with cost 3^2 = 9\n\nThe total cost is 1 + 1 + 4 + 1 + 4 + 4 + 9 = 24."} +{"id": "02820", "text": "A never-ending, fast-changing and dream-like world unfolds, as the secret door opens.\n\nA world is an unordered graph G, in whose vertex set V(G) there are two special vertices s(G) and t(G). An initial world has vertex set {s(G), t(G)} and an edge between them.\n\nA total of n changes took place in an initial world. In each change, a new vertex w is added into V(G), an existing edge (u, v) is chosen, and two edges (u, w) and (v, w) are added into E(G). Note that it's possible that some edges are chosen in more than one change.\n\nIt's known that the capacity of the minimum s-t cut of the resulting graph is m, that is, at least m edges need to be removed in order to make s(G) and t(G) disconnected.\n\nCount the number of non-similar worlds that can be built under the constraints, modulo 10^9 + 7. We define two worlds similar, if they are isomorphic and there is isomorphism in which the s and t vertices are not relabelled. Formally, two worlds G and H are considered similar, if there is a bijection between their vertex sets $f : V(G) \\rightarrow V(H)$, such that: f(s(G)) = s(H); f(t(G)) = t(H); Two vertices u and v of G are adjacent in G if and only if f(u) and f(v) are adjacent in H. \n\n\n-----Input-----\n\nThe first and only line of input contains two space-separated integers n, m (1 \u2264 n, m \u2264 50) \u2014 the number of operations performed and the minimum cut, respectively.\n\n\n-----Output-----\n\nOutput one integer \u2014 the number of non-similar worlds that can be built, modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n3 2\n\nOutput\n6\n\nInput\n4 4\n\nOutput\n3\n\nInput\n7 3\n\nOutput\n1196\n\nInput\n31 8\n\nOutput\n64921457\n\n\n\n-----Note-----\n\nIn the first example, the following 6 worlds are pairwise non-similar and satisfy the constraints, with s(G) marked in green, t(G) marked in blue, and one of their minimum cuts in light blue.\n\n [Image] \n\nIn the second example, the following 3 worlds satisfy the constraints.\n\n [Image]"} +{"id": "02821", "text": "Pavel loves grid mazes. A grid maze is an n \u00d7 m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.\n\nPavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him.\n\n\n-----Input-----\n\nThe first line contains three integers n, m, k (1 \u2264 n, m \u2264 500, 0 \u2264 k < s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze.\n\nEach of the next n lines contains m characters. They describe the original maze. If a character on a line equals \".\", then the corresponding cell is empty and if the character equals \"#\", then the cell is a wall.\n\n\n-----Output-----\n\nPrint n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as \"X\", the other cells must be left without changes (that is, \".\" and \"#\").\n\nIt is guaranteed that a solution exists. If there are multiple solutions you can output any of them.\n\n\n-----Examples-----\nInput\n3 4 2\n#..#\n..#.\n#...\n\nOutput\n#.X#\nX.#.\n#...\n\nInput\n5 4 5\n#...\n#.#.\n.#..\n...#\n.#.#\n\nOutput\n#XXX\n#X#.\nX#..\n...#\n.#.#"} +{"id": "02822", "text": "In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is p_{i}-th inflorescence and p_{i} < i.\n\nOnce tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to p_{a}-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.\n\nHelp Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.\n\n\n-----Input-----\n\nFirst line of input contains single integer number n (2 \u2264 n \u2264 100 000) \u00a0\u2014 number of inflorescences.\n\nSecond line of input contains sequence of n - 1 integer numbers p_2, p_3, ..., p_{n} (1 \u2264 p_{i} < i), where p_{i} is number of inflorescence into which the apple from i-th inflorescence rolls down.\n\n\n-----Output-----\n\nSingle line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.\n\n\n-----Examples-----\nInput\n3\n1 1\n\nOutput\n1\n\nInput\n5\n1 2 2 2\n\nOutput\n3\n\nInput\n18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.\n\nIn the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it."} +{"id": "02823", "text": "Determine if there exists a sequence obtained by permuting 1,2,...,N that satisfies the following conditions:\n - The length of its longest increasing subsequence is A.\n - The length of its longest decreasing subsequence is B.\nIf it exists, construct one such sequence.\n\n-----Notes-----\nA subsequence of a sequence P is a sequence that can be obtained by extracting some of the elements in P without changing the order.\nA longest increasing subsequence of a sequence P is a sequence with the maximum length among the subsequences of P that are monotonically increasing.\nSimilarly, a longest decreasing subsequence of a sequence P is a sequence with the maximum length among the subsequences of P that are monotonically decreasing.\n\n-----Constraints-----\n - 1 \\leq N,A,B \\leq 3\\times 10^5\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN A B\n\n-----Output-----\nIf there are no sequences that satisfy the conditions, print -1.\nOtherwise, print N integers. The i-th integer should be the i-th element of the sequence that you constructed.\n\n-----Sample Input-----\n5 3 2\n\n-----Sample Output-----\n2 4 1 5 3\n\nOne longest increasing subsequence of this sequence is {2,4,5}, and one longest decreasing subsequence of it is {4,3}."} +{"id": "02824", "text": "Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!\n\nRecently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset. \n\nTo not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed $1$.\n\nHelp Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found here.\n\n\n-----Input-----\n\nThe first line contains the number of vertices $n$ ($2 \\le n \\le 700$).\n\nThe second line features $n$ distinct integers $a_i$ ($2 \\le a_i \\le 10^9$)\u00a0\u2014 the values of vertices in ascending order.\n\n\n-----Output-----\n\nIf it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than $1$, print \"Yes\" (quotes for clarity).\n\nOtherwise, print \"No\" (quotes for clarity).\n\n\n-----Examples-----\nInput\n6\n3 6 9 18 36 108\n\nOutput\nYes\n\nInput\n2\n7 17\n\nOutput\nNo\n\nInput\n9\n4 8 10 12 15 18 33 44 81\n\nOutput\nYes\n\n\n\n-----Note-----\n\nThe picture below illustrates one of the possible trees for the first example. [Image] \n\nThe picture below illustrates one of the possible trees for the third example. [Image]"} +{"id": "02825", "text": "We have N gemstones labeled 1 through N.\nYou can perform the following operation any number of times (possibly zero).\n - Select a positive integer x, and smash all the gems labeled with multiples of x.\nThen, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan).\nHowever, a_i may be negative, in which case you will be charged money.\nBy optimally performing the operation, how much yen can you earn?\n\n-----Constraints-----\n - All input values are integers.\n - 1 \\leq N \\leq 100\n - |a_i| \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the maximum amount of money that can be earned.\n\n-----Sample Input-----\n6\n1 2 -6 4 5 3\n\n-----Sample Output-----\n12\n\nIt is optimal to smash Gem 3 and 6."} +{"id": "02826", "text": "You are given an array of positive integers a_1, a_2, ..., a_{n} \u00d7 T of length n \u00d7 T. We know that for any i > n it is true that a_{i} = a_{i} - n. Find the length of the longest non-decreasing sequence of the given array.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers: n, T (1 \u2264 n \u2264 100, 1 \u2264 T \u2264 10^7). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 300).\n\n\n-----Output-----\n\nPrint a single number \u2014 the length of a sought sequence.\n\n\n-----Examples-----\nInput\n4 3\n3 1 4 2\n\nOutput\n5\n\n\n\n-----Note-----\n\nThe array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence."} +{"id": "02827", "text": "Some time ago Mister B detected a strange signal from the space, which he started to study.\n\nAfter some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.\n\nLet's define the deviation of a permutation p as $\\sum_{i = 1}^{i = n}|p [ i ] - i|$.\n\nFind a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.\n\nLet's denote id k (0 \u2264 k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:\n\n k = 0: shift p_1, p_2, ... p_{n}, k = 1: shift p_{n}, p_1, ... p_{n} - 1, ..., k = n - 1: shift p_2, p_3, ... p_{n}, p_1. \n\n\n-----Input-----\n\nFirst line contains single integer n (2 \u2264 n \u2264 10^6) \u2014 the length of the permutation.\n\nThe second line contains n space-separated integers p_1, p_2, ..., p_{n} (1 \u2264 p_{i} \u2264 n)\u00a0\u2014 the elements of the permutation. It is guaranteed that all elements are distinct.\n\n\n-----Output-----\n\nPrint two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n0 0\n\nInput\n3\n2 3 1\n\nOutput\n0 1\n\nInput\n3\n3 2 1\n\nOutput\n2 1\n\n\n\n-----Note-----\n\nIn the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.\n\nIn the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.\n\nIn the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts."} +{"id": "02828", "text": "Recently, the Fair Nut has written $k$ strings of length $n$, consisting of letters \"a\" and \"b\". He calculated $c$\u00a0\u2014 the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time.\n\nThen, he lost his sheet with strings. He remembers that all written strings were lexicographically not smaller than string $s$ and not bigger than string $t$. He is interested: what is the maximum value of $c$ that he could get.\n\nA string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \\ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\leq n \\leq 5 \\cdot 10^5$, $1 \\leq k \\leq 10^9$).\n\nThe second line contains a string $s$ ($|s| = n$)\u00a0\u2014 the string consisting of letters \"a\" and \"b.\n\nThe third line contains a string $t$ ($|t| = n$)\u00a0\u2014 the string consisting of letters \"a\" and \"b.\n\nIt is guaranteed that string $s$ is lexicographically not bigger than $t$.\n\n\n-----Output-----\n\nPrint one number\u00a0\u2014 maximal value of $c$.\n\n\n-----Examples-----\nInput\n2 4\naa\nbb\n\nOutput\n6\n\nInput\n3 3\naba\nbba\n\nOutput\n8\n\nInput\n4 5\nabbb\nbaaa\n\nOutput\n8\n\n\n\n-----Note-----\n\nIn the first example, Nut could write strings \"aa\", \"ab\", \"ba\", \"bb\". These $4$ strings are prefixes of at least one of the written strings, as well as \"a\" and \"b\". Totally, $6$ strings.\n\nIn the second example, Nut could write strings \"aba\", \"baa\", \"bba\".\n\nIn the third example, there are only two different strings that Nut could write. If both of them are written, $c=8$."} +{"id": "02829", "text": "Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece of paper and wrote down eight lines, each containing three integers \u2014 coordinates of cube's vertex (a single line contains coordinates of a single vertex, each vertex is written exactly once), put the paper on the table and left. While Peter was away, his little brother Nick decided to play with the numbers on the paper. In one operation Nick could swap some numbers inside a single line (Nick didn't swap numbers from distinct lines). Nick could have performed any number of such operations.\n\nWhen Peter returned and found out about Nick's mischief, he started recollecting the original coordinates. Help Peter restore the original position of the points or else state that this is impossible and the numbers were initially recorded incorrectly.\n\n\n-----Input-----\n\nEach of the eight lines contains three space-separated integers \u2014 the numbers written on the piece of paper after Nick's mischief. All numbers do not exceed 10^6 in their absolute value.\n\n\n-----Output-----\n\nIf there is a way to restore the cube, then print in the first line \"YES\". In each of the next eight lines print three integers \u2014 the restored coordinates of the points. The numbers in the i-th output line must be a permutation of the numbers in i-th input line. The numbers should represent the vertices of a cube with non-zero length of a side. If there are multiple possible ways, print any of them.\n\nIf there is no valid way, print \"NO\" (without the quotes) in the first line. Do not print anything else.\n\n\n-----Examples-----\nInput\n0 0 0\n0 0 1\n0 0 1\n0 0 1\n0 1 1\n0 1 1\n0 1 1\n1 1 1\n\nOutput\nYES\n0 0 0\n0 0 1\n0 1 0\n1 0 0\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n\nInput\n0 0 0\n0 0 0\n0 0 0\n0 0 0\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n\nOutput\nNO"} +{"id": "02830", "text": "You are given an array of $n$ integers. You need to split all integers into two groups so that the GCD of all integers in the first group is equal to one and the GCD of all integers in the second group is equal to one.\n\nThe GCD of a group of integers is the largest non-negative integer that divides all the integers in the group.\n\nBoth groups have to be non-empty.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\leq n \\leq 10^5$).\n\nThe second line contains $n$ integers $a_1$, $a_2$, $\\ldots$, $a_n$ ($1 \\leq a_i \\leq 10^9$)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nIn the first line print \"YES\" (without quotes), if it is possible to split the integers into two groups as required, and \"NO\" (without quotes) otherwise.\n\nIf it is possible to split the integers, in the second line print $n$ integers, where the $i$-th integer is equal to $1$ if the integer $a_i$ should be in the first group, and $2$ otherwise.\n\nIf there are multiple solutions, print any.\n\n\n-----Examples-----\nInput\n4\n2 3 6 7\n\nOutput\nYES\n2 2 1 1 \n\nInput\n5\n6 15 35 77 22\n\nOutput\nYES\n2 1 2 1 1 \n\nInput\n5\n6 10 15 1000 75\n\nOutput\nNO"} +{"id": "02831", "text": "Andrew was very excited to participate in Olympiad of Metropolises. Days flew by quickly, and Andrew is already at the airport, ready to go home. He has $n$ rubles left, and would like to exchange them to euro and dollar bills. Andrew can mix dollar bills and euro bills in whatever way he wants. The price of one dollar is $d$ rubles, and one euro costs $e$ rubles.\n\nRecall that there exist the following dollar bills: $1$, $2$, $5$, $10$, $20$, $50$, $100$, and the following euro bills\u00a0\u2014 $5$, $10$, $20$, $50$, $100$, $200$ (note that, in this problem we do not consider the $500$ euro bill, it is hard to find such bills in the currency exchange points). Andrew can buy any combination of bills, and his goal is to minimize the total number of rubles he will have after the exchange.\n\nHelp him\u00a0\u2014 write a program that given integers $n$, $e$ and $d$, finds the minimum number of rubles Andrew can get after buying dollar and euro bills.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\leq n \\leq 10^8$)\u00a0\u2014 the initial sum in rubles Andrew has. \n\nThe second line of the input contains one integer $d$ ($30 \\leq d \\leq 100$)\u00a0\u2014 the price of one dollar in rubles. \n\nThe third line of the input contains integer $e$ ($30 \\leq e \\leq 100$)\u00a0\u2014 the price of one euro in rubles.\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 the minimum number of rubles Andrew can have after buying dollar and euro bills optimally.\n\n\n-----Examples-----\nInput\n100\n60\n70\n\nOutput\n40\n\nInput\n410\n55\n70\n\nOutput\n5\n\nInput\n600\n60\n70\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, we can buy just $1$ dollar because there is no $1$ euro bill.\n\nIn the second example, optimal exchange is to buy $5$ euro and $1$ dollar.\n\nIn the third example, optimal exchange is to buy $10$ dollars in one bill."} +{"id": "02832", "text": "You are given $n$ integers. You need to choose a subset and put the chosen numbers in a beautiful rectangle (rectangular matrix). Each chosen number should occupy one of its rectangle cells, each cell must be filled with exactly one chosen number. Some of the $n$ numbers may not be chosen.\n\nA rectangle (rectangular matrix) is called beautiful if in each row and in each column all values are different.\n\nWhat is the largest (by the total number of cells) beautiful rectangle you can construct? Print the rectangle itself.\n\n\n-----Input-----\n\nThe first line contains $n$ ($1 \\le n \\le 4\\cdot10^5$). The second line contains $n$ integers ($1 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nIn the first line print $x$ ($1 \\le x \\le n$) \u2014 the total number of cells of the required maximum beautiful rectangle. In the second line print $p$ and $q$ ($p \\cdot q=x$): its sizes. In the next $p$ lines print the required rectangle itself. If there are several answers, print any.\n\n\n-----Examples-----\nInput\n12\n3 1 4 1 5 9 2 6 5 3 5 8\n\nOutput\n12\n3 4\n1 2 3 5\n3 1 5 4\n5 6 8 9\n\nInput\n5\n1 1 1 1 1\n\nOutput\n1\n1 1\n1"} +{"id": "02833", "text": "There are N squares arranged in a row.\nThe squares are numbered 1, 2, ..., N, from left to right.\nSnuke is painting each square in red, green or blue.\nAccording to his aesthetic sense, the following M conditions must all be satisfied.\nThe i-th condition is:\n - There are exactly x_i different colors among squares l_i, l_i + 1, ..., r_i.\nIn how many ways can the squares be painted to satisfy all the conditions?\nFind the count modulo 10^9+7.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 300\n - 1 \u2264 M \u2264 300\n - 1 \u2264 l_i \u2264 r_i \u2264 N\n - 1 \u2264 x_i \u2264 3\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nl_1 r_1 x_1\nl_2 r_2 x_2\n:\nl_M r_M x_M\n\n-----Output-----\nPrint the number of ways to paint the squares to satisfy all the conditions, modulo 10^9+7.\n\n-----Sample Input-----\n3 1\n1 3 3\n\n-----Sample Output-----\n6\n\nThe six ways are:\n - RGB\n - RBG\n - GRB\n - GBR\n - BRG\n - BGR\nwhere R, G and B correspond to red, green and blue squares, respectively."} +{"id": "02834", "text": "For integers b (b \\geq 2) and n (n \\geq 1), let the function f(b,n) be defined as follows:\n - f(b,n) = n, when n < b\n - f(b,n) = f(b,\\,{\\rm floor}(n / b)) + (n \\ {\\rm mod} \\ b), when n \\geq b\nHere, {\\rm floor}(n / b) denotes the largest integer not exceeding n / b,\nand n \\ {\\rm mod} \\ b denotes the remainder of n divided by b.\nLess formally, f(b,n) is equal to the sum of the digits of n written in base b.\nFor example, the following hold:\n - f(10,\\,87654)=8+7+6+5+4=30\n - f(100,\\,87654)=8+76+54=138\nYou are given integers n and s.\nDetermine if there exists an integer b (b \\geq 2) such that f(b,n)=s.\nIf the answer is positive, also find the smallest such b.\n\n-----Constraints-----\n - 1 \\leq n \\leq 10^{11}\n - 1 \\leq s \\leq 10^{11}\n - n,\\,s are integers.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nn\ns\n\n-----Output-----\nIf there exists an integer b (b \\geq 2) such that f(b,n)=s, print the smallest such b.\nIf such b does not exist, print -1 instead.\n\n-----Sample Input-----\n87654\n30\n\n-----Sample Output-----\n10\n"} +{"id": "02835", "text": "There is a string s of length 3 or greater.\nNo two neighboring characters in s are equal.\nTakahashi and Aoki will play a game against each other.\nThe two players alternately performs the following operation, Takahashi going first:\n - Remove one of the characters in s, excluding both ends. However, a character cannot be removed if removal of the character would result in two neighboring equal characters in s.\nThe player who becomes unable to perform the operation, loses the game. Determine which player will win when the two play optimally.\n\n-----Constraints-----\n - 3 \u2264 |s| \u2264 10^5\n - s consists of lowercase English letters.\n - No two neighboring characters in s are equal.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\ns\n\n-----Output-----\nIf Takahashi will win, print First. If Aoki will win, print Second.\n\n-----Sample Input-----\naba\n\n-----Sample Output-----\nSecond\n\nTakahashi, who goes first, cannot perform the operation, since removal of the b, which is the only character not at either ends of s, would result in s becoming aa, with two as neighboring."} +{"id": "02836", "text": "You have a string of decimal digits s. Let's define b_{ij} = s_{i}\u00b7s_{j}. Find in matrix b the number of such rectangles that the sum b_{ij} for all cells (i, j) that are the elements of the rectangle equals a in each rectangle.\n\nA rectangle in a matrix is a group of four integers (x, y, z, t) (x \u2264 y, z \u2264 t). The elements of the rectangle are all cells (i, j) such that x \u2264 i \u2264 y, z \u2264 j \u2264 t.\n\n\n-----Input-----\n\nThe first line contains integer a (0 \u2264 a \u2264 10^9), the second line contains a string of decimal integers s (1 \u2264 |s| \u2264 4000).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to a problem.\n\nPlease, do not write the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n10\n12345\n\nOutput\n6\n\nInput\n16\n439873893693495623498263984765\n\nOutput\n40"} +{"id": "02837", "text": "The only difference between easy and hard versions is constraints.\n\nNauuo is a girl who loves random picture websites.\n\nOne day she made a random picture website by herself which includes $n$ pictures.\n\nWhen Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The $i$-th picture has a non-negative weight $w_i$, and the probability of the $i$-th picture being displayed is $\\frac{w_i}{\\sum_{j=1}^nw_j}$. That is to say, the probability of a picture to be displayed is proportional to its weight.\n\nHowever, Nauuo discovered that some pictures she does not like were displayed too often. \n\nTo solve this problem, she came up with a great idea: when she saw a picture she likes, she would add $1$ to its weight; otherwise, she would subtract $1$ from its weight.\n\nNauuo will visit the website $m$ times. She wants to know the expected weight of each picture after all the $m$ visits modulo $998244353$. Can you help her?\n\nThe expected weight of the $i$-th picture can be denoted by $\\frac {q_i} {p_i}$ where $\\gcd(p_i,q_i)=1$, you need to print an integer $r_i$ satisfying $0\\le r_i<998244353$ and $r_i\\cdot p_i\\equiv q_i\\pmod{998244353}$. It can be proved that such $r_i$ exists and is unique.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1\\le n\\le 50$, $1\\le m\\le 50$) \u2014 the number of pictures and the number of visits to the website.\n\nThe second line contains $n$ integers $a_1,a_2,\\ldots,a_n$ ($a_i$ is either $0$ or $1$) \u2014 if $a_i=0$ , Nauuo does not like the $i$-th picture; otherwise Nauuo likes the $i$-th picture. It is guaranteed that there is at least one picture which Nauuo likes.\n\nThe third line contains $n$ integers $w_1,w_2,\\ldots,w_n$ ($1\\le w_i\\le50$) \u2014 the initial weights of the pictures.\n\n\n-----Output-----\n\nThe output contains $n$ integers $r_1,r_2,\\ldots,r_n$ \u2014 the expected weights modulo $998244353$.\n\n\n-----Examples-----\nInput\n2 1\n0 1\n2 1\n\nOutput\n332748119\n332748119\n\nInput\n1 2\n1\n1\n\nOutput\n3\n\nInput\n3 3\n0 1 1\n4 3 5\n\nOutput\n160955686\n185138929\n974061117\n\n\n\n-----Note-----\n\nIn the first example, if the only visit shows the first picture with a probability of $\\frac 2 3$, the final weights are $(1,1)$; if the only visit shows the second picture with a probability of $\\frac1 3$, the final weights are $(2,2)$.\n\nSo, both expected weights are $\\frac2 3\\cdot 1+\\frac 1 3\\cdot 2=\\frac4 3$ .\n\nBecause $332748119\\cdot 3\\equiv 4\\pmod{998244353}$, you need to print $332748119$ instead of $\\frac4 3$ or $1.3333333333$.\n\nIn the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, $w_1$ will be increased by $1$.\n\nSo, the expected weight is $1+2=3$.\n\nNauuo is very naughty so she didn't give you any hint of the third example."} +{"id": "02838", "text": "In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substring of ABCDEF.\n\nYou are given two strings s_1, s_2 and another string called virus. Your task is to find the longest common subsequence of s_1 and s_2, such that it doesn't contain virus as a substring.\n\n\n-----Input-----\n\nThe input contains three strings in three separate lines: s_1, s_2 and virus (1 \u2264 |s_1|, |s_2|, |virus| \u2264 100). Each string consists only of uppercase English letters.\n\n\n-----Output-----\n\nOutput the longest common subsequence of s_1 and s_2 without virus as a substring. If there are multiple answers, any of them will be accepted. \n\nIf there is no valid common subsequence, output 0.\n\n\n-----Examples-----\nInput\nAJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ\n\nOutput\nORZ\n\nInput\nAA\nA\nA\n\nOutput\n0"} +{"id": "02839", "text": "A monster is attacking the Cyberland!\n\nMaster Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF).\n\nDuring the battle, every second the monster's HP decrease by max(0, ATK_{Y} - DEF_{M}), while Yang's HP decreases by max(0, ATK_{M} - DEF_{Y}), where index Y denotes Master Yang and index M denotes monster. Both decreases happen simultaneously Once monster's HP \u2264 0 and the same time Master Yang's HP > 0, Master Yang wins.\n\nMaster Yang can buy attributes from the magic shop of Cyberland: h bitcoins per HP, a bitcoins per ATK, and d bitcoins per DEF.\n\nNow Master Yang wants to know the minimum number of bitcoins he can spend in order to win.\n\n\n-----Input-----\n\nThe first line contains three integers HP_{Y}, ATK_{Y}, DEF_{Y}, separated by a space, denoting the initial HP, ATK and DEF of Master Yang.\n\nThe second line contains three integers HP_{M}, ATK_{M}, DEF_{M}, separated by a space, denoting the HP, ATK and DEF of the monster.\n\nThe third line contains three integers h, a, d, separated by a space, denoting the price of 1\u00a0HP, 1\u00a0ATK and 1\u00a0DEF.\n\nAll numbers in input are integer and lie between 1 and 100 inclusively.\n\n\n-----Output-----\n\nThe only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win.\n\n\n-----Examples-----\nInput\n1 2 1\n1 100 1\n1 100 100\n\nOutput\n99\n\nInput\n100 100 100\n1 1 1\n1 1 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nFor the first sample, prices for ATK and DEF are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left.\n\nFor the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything."} +{"id": "02840", "text": "Maxim loves to fill in a matrix in a special manner. Here is a pseudocode of filling in a matrix of size (m + 1) \u00d7 (m + 1):\n\n[Image]\n\nMaxim asks you to count, how many numbers m (1 \u2264 m \u2264 n) are there, such that the sum of values in the cells in the row number m + 1 of the resulting matrix equals t.\n\nExpression (x xor y) means applying the operation of bitwise excluding \"OR\" to numbers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by character \"^\", in Pascal \u2014 by \"xor\".\n\n\n-----Input-----\n\nA single line contains two integers n and t (1 \u2264 n, t \u2264 10^12, t \u2264 n + 1).\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the answer to the problem. \n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n1\n\nInput\n3 2\n\nOutput\n1\n\nInput\n3 3\n\nOutput\n0\n\nInput\n1000000000000 1048576\n\nOutput\n118606527258"} +{"id": "02841", "text": "Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again.\n\nThe device is powered by two wires \"plus\" and \"minus\". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] \n\nMike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the \"plus\" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut.\n\nTo understand the problem better please read the notes to the test samples.\n\n\n-----Input-----\n\nThe single line of the input contains a sequence of characters \"+\" and \"-\" of length n (1 \u2264 n \u2264 100000). The i-th (1 \u2264 i \u2264 n) position of the sequence contains the character \"+\", if on the i-th step from the wall the \"plus\" wire runs above the \"minus\" wire, and the character \"-\" otherwise.\n\n\n-----Output-----\n\nPrint either \"Yes\" (without the quotes) if the wires can be untangled or \"No\" (without the quotes) if the wires cannot be untangled.\n\n\n-----Examples-----\nInput\n-++-\n\nOutput\nYes\n\nInput\n+-\n\nOutput\nNo\n\nInput\n++\n\nOutput\nYes\n\nInput\n-\n\nOutput\nNo\n\n\n\n-----Note-----\n\nThe first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the \"plus\" wire lower, thus eliminating the two crosses in the middle, and then draw it under the \"minus\" wire, eliminating also the remaining two crosses.\n\nIn the second testcase the \"plus\" wire makes one full revolution around the \"minus\" wire. Thus the wires cannot be untangled: [Image] \n\nIn the third testcase the \"plus\" wire simply runs above the \"minus\" wire twice in sequence. The wires can be untangled by lifting \"plus\" and moving it higher: [Image] \n\nIn the fourth testcase the \"minus\" wire runs above the \"plus\" wire once. The wires cannot be untangled without moving the device itself: [Image]"} +{"id": "02842", "text": "Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.\n\nFormally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.\n\nPeter decided to tie his car to point P and now he is wondering what is the area of \u200b\u200bthe region that will be cleared from snow. Help him.\n\n\n-----Input-----\n\nThe first line of the input contains three integers\u00a0\u2014 the number of vertices of the polygon n ($3 \\leq n \\leq 100000$), and coordinates of point P.\n\nEach of the next n lines contains two integers\u00a0\u2014 coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.\n\nAll the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.\n\n\n-----Output-----\n\nPrint a single real value number\u00a0\u2014 the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. \n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n3 0 0\n0 1\n-1 2\n1 2\n\nOutput\n12.566370614359172464\n\nInput\n4 1 -1\n0 0\n1 2\n2 0\n1 1\n\nOutput\n21.991148575128551812\n\n\n\n-----Note-----\n\nIn the first sample snow will be removed from that area:\n\n $0$"} +{"id": "02843", "text": "Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.\n\nA block with side a has volume a^3. A tower consisting of blocks with sides a_1, a_2, ..., a_{k} has the total volume a_1^3 + a_2^3 + ... + a_{k}^3.\n\nLimak is going to build a tower. First, he asks you to tell him a positive integer X\u00a0\u2014 the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.\n\nLimak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.\n\nCan you help Limak? Find the maximum number of blocks his tower can have and the maximum X \u2264 m that results this number of blocks.\n\n\n-----Input-----\n\nThe only line of the input contains one integer m (1 \u2264 m \u2264 10^15), meaning that Limak wants you to choose X between 1 and m, inclusive.\n\n\n-----Output-----\n\nPrint two integers\u00a0\u2014 the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.\n\n\n-----Examples-----\nInput\n48\n\nOutput\n9 42\n\nInput\n6\n\nOutput\n6 6\n\n\n\n-----Note-----\n\nIn the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.\n\nIn more detail, after choosing X = 42 the process of building a tower is: Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. The second added block has side 2, so the remaining volume is 15 - 8 = 7. Finally, Limak adds 7 blocks with side 1, one by one. \n\nSo, there are 9 blocks in the tower. The total volume is is 3^3 + 2^3 + 7\u00b71^3 = 27 + 8 + 7 = 42."} +{"id": "02844", "text": "Petya's friends made him a birthday present \u2014 a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself. \n\nTo make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning \"(\" into \")\" or vice versa) isn't allowed. \n\nWe remind that bracket sequence $s$ is called correct if: $s$ is empty; $s$ is equal to \"($t$)\", where $t$ is correct bracket sequence; $s$ is equal to $t_1 t_2$, i.e. concatenation of $t_1$ and $t_2$, where $t_1$ and $t_2$ are correct bracket sequences. \n\nFor example, \"(()())\", \"()\" are correct, while \")(\" and \"())\" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct.\n\n\n-----Input-----\n\nFirst of line of input contains a single number $n$ ($1 \\leq n \\leq 200\\,000$)\u00a0\u2014 length of the sequence which Petya received for his birthday.\n\nSecond line of the input contains bracket sequence of length $n$, containing symbols \"(\" and \")\".\n\n\n-----Output-----\n\nPrint \"Yes\" if Petya can make his sequence correct moving at most one bracket. Otherwise print \"No\".\n\n\n-----Examples-----\nInput\n2\n)(\n\nOutput\nYes\n\nInput\n3\n(()\n\nOutput\nNo\n\nInput\n2\n()\n\nOutput\nYes\n\nInput\n10\n)))))(((((\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first example, Petya can move first bracket to the end, thus turning the sequence into \"()\", which is correct bracket sequence.\n\nIn the second example, there is no way to move at most one bracket so that the sequence becomes correct.\n\nIn the third example, the sequence is already correct and there's no need to move brackets."} +{"id": "02845", "text": "One day Greg and his friends were walking in the forest. Overall there were n people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold people with the total weight of at most k kilograms.\n\nGreg immediately took a piece of paper and listed there the weights of all people in his group (including himself). It turned out that each person weights either 50 or 100 kilograms. Now Greg wants to know what minimum number of times the boat needs to cross the river to transport the whole group to the other bank. The boat needs at least one person to navigate it from one bank to the other. As the boat crosses the river, it can have any non-zero number of passengers as long as their total weight doesn't exceed k.\n\nAlso Greg is wondering, how many ways there are to transport everybody to the other side in the minimum number of boat rides. Two ways are considered distinct if during some ride they have distinct sets of people on the boat.\n\nHelp Greg with this problem.\n\n \n\n\n-----Input-----\n\nThe first line contains two integers n, k (1 \u2264 n \u2264 50, 1 \u2264 k \u2264 5000) \u2014 the number of people, including Greg, and the boat's weight limit. The next line contains n integers \u2014 the people's weights. A person's weight is either 50 kilos or 100 kilos.\n\nYou can consider Greg and his friends indexed in some way.\n\n\n-----Output-----\n\nIn the first line print an integer \u2014 the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer -1.\n\nIn the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (10^9 + 7). If transporting everyone to the other bank is impossible, print integer 0.\n\n\n-----Examples-----\nInput\n1 50\n50\n\nOutput\n1\n1\n\nInput\n3 100\n50 50 100\n\nOutput\n5\n2\n\nInput\n2 50\n50 50\n\nOutput\n-1\n0\n\n\n\n-----Note-----\n\nIn the first test Greg walks alone and consequently, he needs only one ride across the river.\n\nIn the second test you should follow the plan:\n\n transport two 50 kg. people; transport one 50 kg. person back; transport one 100 kg. person; transport one 50 kg. person back; transport two 50 kg. people. \n\nThat totals to 5 rides. Depending on which person to choose at step 2, we can get two distinct ways."} +{"id": "02846", "text": "SIHanatsuka - EMber SIHanatsuka - ATONEMENT\n\nBack in time, the seven-year-old Nora used to play lots of games with her creation ROBO_Head-02, both to have fun and enhance his abilities.\n\nOne day, Nora's adoptive father, Phoenix Wyle, brought Nora $n$ boxes of toys. Before unpacking, Nora decided to make a fun game for ROBO.\n\nShe labelled all $n$ boxes with $n$ distinct integers $a_1, a_2, \\ldots, a_n$ and asked ROBO to do the following action several (possibly zero) times:\n\n Pick three distinct indices $i$, $j$ and $k$, such that $a_i \\mid a_j$ and $a_i \\mid a_k$. In other words, $a_i$ divides both $a_j$ and $a_k$, that is $a_j \\bmod a_i = 0$, $a_k \\bmod a_i = 0$. After choosing, Nora will give the $k$-th box to ROBO, and he will place it on top of the box pile at his side. Initially, the pile is empty. After doing so, the box $k$ becomes unavailable for any further actions. \n\nBeing amused after nine different tries of the game, Nora asked ROBO to calculate the number of possible different piles having the largest amount of boxes in them. Two piles are considered different if there exists a position where those two piles have different boxes.\n\nSince ROBO was still in his infant stages, and Nora was still too young to concentrate for a long time, both fell asleep before finding the final answer. Can you help them?\n\nAs the number of such piles can be very large, you should print the answer modulo $10^9 + 7$.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($3 \\le n \\le 60$), denoting the number of boxes.\n\nThe second line contains $n$ distinct integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 60$), where $a_i$ is the label of the $i$-th box.\n\n\n-----Output-----\n\nPrint the number of distinct piles having the maximum number of boxes that ROBO_Head can have, modulo $10^9 + 7$.\n\n\n-----Examples-----\nInput\n3\n2 6 8\n\nOutput\n2\n\nInput\n5\n2 3 4 9 12\n\nOutput\n4\n\nInput\n4\n5 7 2 9\n\nOutput\n1\n\n\n\n-----Note-----\n\nLet's illustrate the box pile as a sequence $b$, with the pile's bottommost box being at the leftmost position.\n\nIn the first example, there are $2$ distinct piles possible: $b = [6]$ ($[2, \\mathbf{6}, 8] \\xrightarrow{(1, 3, 2)} [2, 8]$) $b = [8]$ ($[2, 6, \\mathbf{8}] \\xrightarrow{(1, 2, 3)} [2, 6]$) \n\nIn the second example, there are $4$ distinct piles possible: $b = [9, 12]$ ($[2, 3, 4, \\mathbf{9}, 12] \\xrightarrow{(2, 5, 4)} [2, 3, 4, \\mathbf{12}] \\xrightarrow{(1, 3, 4)} [2, 3, 4]$) $b = [4, 12]$ ($[2, 3, \\mathbf{4}, 9, 12] \\xrightarrow{(1, 5, 3)} [2, 3, 9, \\mathbf{12}] \\xrightarrow{(2, 3, 4)} [2, 3, 9]$) $b = [4, 9]$ ($[2, 3, \\mathbf{4}, 9, 12] \\xrightarrow{(1, 5, 3)} [2, 3, \\mathbf{9}, 12] \\xrightarrow{(2, 4, 3)} [2, 3, 12]$) $b = [9, 4]$ ($[2, 3, 4, \\mathbf{9}, 12] \\xrightarrow{(2, 5, 4)} [2, 3, \\mathbf{4}, 12] \\xrightarrow{(1, 4, 3)} [2, 3, 12]$) \n\nIn the third sequence, ROBO can do nothing at all. Therefore, there is only $1$ valid pile, and that pile is empty."} +{"id": "02847", "text": "During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers.\n\nFor a given list of pairs of integers $(a_1, b_1)$, $(a_2, b_2)$, ..., $(a_n, b_n)$ their WCD is arbitrary integer greater than $1$, such that it divides at least one element in each pair. WCD may not exist for some lists.\n\nFor example, if the list looks like $[(12, 15), (25, 18), (10, 24)]$, then their WCD can be equal to $2$, $3$, $5$ or $6$ (each of these numbers is strictly greater than $1$ and divides at least one number in each pair).\n\nYou're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 150\\,000$)\u00a0\u2014 the number of pairs.\n\nEach of the next $n$ lines contains two integer values $a_i$, $b_i$ ($2 \\le a_i, b_i \\le 2 \\cdot 10^9$).\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the WCD of the set of pairs. \n\nIf there are multiple possible answers, output any; if there is no answer, print $-1$.\n\n\n-----Examples-----\nInput\n3\n17 18\n15 24\n12 15\n\nOutput\n6\nInput\n2\n10 16\n7 17\n\nOutput\n-1\n\nInput\n5\n90 108\n45 105\n75 40\n165 175\n33 30\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example the answer is $6$ since it divides $18$ from the first pair, $24$ from the second and $12$ from the third ones. Note that other valid answers will also be accepted.\n\nIn the second example there are no integers greater than $1$ satisfying the conditions.\n\nIn the third example one of the possible answers is $5$. Note that, for example, $15$ is also allowed, but it's not necessary to maximize the output."} +{"id": "02848", "text": "There are two strings s and t, consisting only of letters a and b. You can make the following operation several times: choose a prefix of s, a prefix of t and swap them. Prefixes can be empty, also a prefix can coincide with a whole string. \n\nYour task is to find a sequence of operations after which one of the strings consists only of a letters and the other consists only of b letters. The number of operations should be minimized.\n\n\n-----Input-----\n\nThe first line contains a string s (1 \u2264 |s| \u2264 2\u00b710^5).\n\nThe second line contains a string t (1 \u2264 |t| \u2264 2\u00b710^5).\n\nHere |s| and |t| denote the lengths of s and t, respectively. It is guaranteed that at least one of the strings contains at least one a letter and at least one of the strings contains at least one b letter.\n\n\n-----Output-----\n\nThe first line should contain a single integer n (0 \u2264 n \u2264 5\u00b710^5)\u00a0\u2014 the number of operations.\n\nEach of the next n lines should contain two space-separated integers a_{i}, b_{i}\u00a0\u2014 the lengths of prefixes of s and t to swap, respectively.\n\nIf there are multiple possible solutions, you can print any of them. It's guaranteed that a solution with given constraints exists.\n\n\n-----Examples-----\nInput\nbab\nbb\n\nOutput\n2\n1 0\n1 3\n\nInput\nbbbb\naaa\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, you can solve the problem in two operations: Swap the prefix of the first string with length 1 and the prefix of the second string with length 0. After this swap, you'll have strings ab and bbb. Swap the prefix of the first string with length 1 and the prefix of the second string with length 3. After this swap, you'll have strings bbbb and a. \n\nIn the second example, the strings are already appropriate, so no operations are needed."} +{"id": "02849", "text": "We have a tree with N vertices. Vertex 1 is the root of the tree, and the parent of Vertex i (2 \\leq i \\leq N) is Vertex P_i.\nTo each vertex in the tree, Snuke will allocate a color, either black or white, and a non-negative integer weight.\nSnuke has a favorite integer sequence, X_1, X_2, ..., X_N, so he wants to allocate colors and weights so that the following condition is satisfied for all v.\n - The total weight of the vertices with the same color as v among the vertices contained in the subtree whose root is v, is X_v.\nHere, the subtree whose root is v is the tree consisting of Vertex v and all of its descendants.\nDetermine whether it is possible to allocate colors and weights in this way.\n\n-----Constraints-----\n - 1 \\leq N \\leq 1 000\n - 1 \\leq P_i \\leq i - 1\n - 0 \\leq X_i \\leq 5 000\n\n-----Inputs-----\nInput is given from Standard Input in the following format:\nN\nP_2 P_3 ... P_N\nX_1 X_2 ... X_N\n\n-----Outputs-----\nIf it is possible to allocate colors and weights to the vertices so that the condition is satisfied, print POSSIBLE; otherwise, print IMPOSSIBLE.\n\n-----Sample Input-----\n3\n1 1\n4 3 2\n\n-----Sample Output-----\nPOSSIBLE\n\nFor example, the following allocation satisfies the condition:\n - Set the color of Vertex 1 to white and its weight to 2.\n - Set the color of Vertex 2 to black and its weight to 3.\n - Set the color of Vertex 3 to white and its weight to 2.\nThere are also other possible allocations."} +{"id": "02850", "text": "In \"Takahashi-ya\", a ramen restaurant, basically they have one menu: \"ramen\", but N kinds of toppings are also offered. When a customer orders a bowl of ramen, for each kind of topping, he/she can choose whether to put it on top of his/her ramen or not. There is no limit on the number of toppings, and it is allowed to have all kinds of toppings or no topping at all. That is, considering the combination of the toppings, 2^N types of ramen can be ordered.\nAkaki entered Takahashi-ya. She is thinking of ordering some bowls of ramen that satisfy both of the following two conditions:\n - Do not order multiple bowls of ramen with the exactly same set of toppings.\n - Each of the N kinds of toppings is on two or more bowls of ramen ordered.\nYou are given N and a prime number M. Find the number of the sets of bowls of ramen that satisfy these conditions, disregarding order, modulo M. Since she is in extreme hunger, ordering any number of bowls of ramen is fine.\n\n-----Constraints-----\n - 2 \\leq N \\leq 3000\n - 10^8 \\leq M \\leq 10^9 + 9\n - N is an integer.\n - M is a prime number.\n\n-----Subscores-----\n - 600 points will be awarded for passing the test set satisfying N \u2264 50.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\n\n-----Output-----\nPrint the number of the sets of bowls of ramen that satisfy the conditions, disregarding order, modulo M.\n\n-----Sample Input-----\n2 1000000007\n\n-----Sample Output-----\n2\n\nLet the two kinds of toppings be A and B. Four types of ramen can be ordered: \"no toppings\", \"with A\", \"with B\" and \"with A, B\". There are two sets of ramen that satisfy the conditions:\n - The following three ramen: \"with A\", \"with B\", \"with A, B\".\n - Four ramen, one for each type."} +{"id": "02851", "text": "You are given two integers $a$ and $b$. Moreover, you are given a sequence $s_0, s_1, \\dots, s_{n}$. All values in $s$ are integers $1$ or $-1$. It's known that sequence is $k$-periodic and $k$ divides $n+1$. In other words, for each $k \\leq i \\leq n$ it's satisfied that $s_{i} = s_{i - k}$.\n\nFind out the non-negative remainder of division of $\\sum \\limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$ by $10^{9} + 9$.\n\nNote that the modulo is unusual!\n\n\n-----Input-----\n\nThe first line contains four integers $n, a, b$ and $k$ $(1 \\leq n \\leq 10^{9}, 1 \\leq a, b \\leq 10^{9}, 1 \\leq k \\leq 10^{5})$.\n\nThe second line contains a sequence of length $k$ consisting of characters '+' and '-'. \n\nIf the $i$-th character (0-indexed) is '+', then $s_{i} = 1$, otherwise $s_{i} = -1$.\n\nNote that only the first $k$ members of the sequence are given, the rest can be obtained using the periodicity property.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 value of given expression modulo $10^{9} + 9$.\n\n\n-----Examples-----\nInput\n2 2 3 3\n+-+\n\nOutput\n7\n\nInput\n4 1 5 1\n-\n\nOutput\n999999228\n\n\n\n-----Note-----\n\nIn the first example:\n\n$(\\sum \\limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$ = $2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$ = 7\n\nIn the second example:\n\n$(\\sum \\limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \\equiv 999999228 \\pmod{10^{9} + 9}$."} +{"id": "02852", "text": "You are given three sticks with positive integer lengths of a, b, and c centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the length of any stick.\n\nDetermine the number of ways to increase the lengths of some sticks so that you can form from them a non-degenerate (that is, having a positive area) triangle. Two ways are considered different, if the length of some stick is increased by different number of centimeters in them.\n\n\n-----Input-----\n\nThe single line contains 4 integers a, b, c, l (1 \u2264 a, b, c \u2264 3\u00b710^5, 0 \u2264 l \u2264 3\u00b710^5).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of ways to increase the sizes of the sticks by the total of at most l centimeters, so that you can make a non-degenerate triangle from it.\n\n\n-----Examples-----\nInput\n1 1 1 2\n\nOutput\n4\n\nInput\n1 2 3 1\n\nOutput\n2\n\nInput\n10 2 1 7\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter.\n\nIn the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn't meet the conditions."} +{"id": "02853", "text": "A sequence of non-negative integers a_1, a_2, ..., a_{n} of length n is called a wool sequence if and only if there exists two integers l and r (1 \u2264 l \u2264 r \u2264 n) such that $a_{l} \\oplus a_{l + 1} \\oplus \\cdots \\oplus a_{r} = 0$. In other words each wool sequence contains a subsequence of consecutive elements with xor equal to 0.\n\nThe expression $x \\oplus y$ means applying the operation of a bitwise xor to numbers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is marked as \"^\", in Pascal \u2014 as \"xor\".\n\nIn this problem you are asked to compute the number of sequences made of n integers from 0 to 2^{m} - 1 that are not a wool sequence. You should print this number modulo 1000000009 (10^9 + 9).\n\n\n-----Input-----\n\nThe only line of input contains two space-separated integers n and m (1 \u2264 n, m \u2264 10^5).\n\n\n-----Output-----\n\nPrint the required number of sequences modulo 1000000009 (10^9 + 9) on the only line of output.\n\n\n-----Examples-----\nInput\n3 2\n\nOutput\n6\n\n\n\n-----Note-----\n\nSequences of length 3 made of integers 0, 1, 2 and 3 that are not a wool sequence are (1, 3, 1), (1, 2, 1), (2, 1, 2), (2, 3, 2), (3, 1, 3) and (3, 2, 3)."} +{"id": "02854", "text": "Given is a tree with N vertices numbered 1 to N, and N-1 edges numbered 1 to N-1.\nEdge i connects Vertex a_i and b_i bidirectionally and has a length of 1.\nSnuke will paint each vertex white or black.\nThe niceness of a way of painting the graph is \\max(X, Y), where X is the maximum among the distances between white vertices, and Y is the maximum among the distances between black vertices.\nHere, if there is no vertex of one color, we consider the maximum among the distances between vertices of that color to be 0.\nThere are 2^N ways of painting the graph. Compute the sum of the nicenesses of all those ways, modulo (10^{9}+7).\n\n-----Constraints-----\n - 2 \\leq N \\leq 2 \\times 10^{5}\n - 1 \\leq a_i, b_i \\leq N\n - The given graph is a tree.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 b_1\n\\vdots\na_{N-1} b_{N-1}\n\n-----Output-----\nPrint the sum of the nicenesses of the ways of painting the graph, modulo (10^{9}+7).\n\n-----Sample Input-----\n2\n1 2\n\n-----Sample Output-----\n2\n\n - If we paint Vertex 1 and 2 the same color, the niceness will be 1; if we paint them different colors, the niceness will be 0.\n - The sum of those nicenesses is 2."} +{"id": "02855", "text": "Nauuo is a girl who loves playing cards.\n\nOne day she was playing cards but found that the cards were mixed with some empty ones.\n\nThere are $n$ cards numbered from $1$ to $n$, and they were mixed with another $n$ empty cards. She piled up the $2n$ cards and drew $n$ of them. The $n$ cards in Nauuo's hands are given. The remaining $n$ cards in the pile are also given in the order from top to bottom.\n\nIn one operation she can choose a card in her hands and play it \u2014 put it at the bottom of the pile, then draw the top card from the pile.\n\nNauuo wants to make the $n$ numbered cards piled up in increasing order (the $i$-th card in the pile from top to bottom is the card $i$) as quickly as possible. Can you tell her the minimum number of operations?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1\\le n\\le 2\\cdot 10^5$) \u2014 the number of numbered cards.\n\nThe second line contains $n$ integers $a_1,a_2,\\ldots,a_n$ ($0\\le a_i\\le n$) \u2014 the initial cards in Nauuo's hands. $0$ represents an empty card.\n\nThe third line contains $n$ integers $b_1,b_2,\\ldots,b_n$ ($0\\le b_i\\le n$) \u2014 the initial cards in the pile, given in order from top to bottom. $0$ represents an empty card.\n\nIt is guaranteed that each number from $1$ to $n$ appears exactly once, either in $a_{1..n}$ or $b_{1..n}$.\n\n\n-----Output-----\n\nThe output contains a single integer \u2014 the minimum number of operations to make the $n$ numbered cards piled up in increasing order.\n\n\n-----Examples-----\nInput\n3\n0 2 0\n3 0 1\n\nOutput\n2\nInput\n3\n0 2 0\n1 0 3\n\nOutput\n4\nInput\n11\n0 0 0 5 0 0 0 4 0 0 11\n9 2 6 0 8 1 7 0 3 0 10\n\nOutput\n18\n\n\n-----Note-----\n\nExample 1\n\nWe can play the card $2$ and draw the card $3$ in the first operation. After that, we have $[0,3,0]$ in hands and the cards in the pile are $[0,1,2]$ from top to bottom.\n\nThen, we play the card $3$ in the second operation. The cards in the pile are $[1,2,3]$, in which the cards are piled up in increasing order.\n\nExample 2\n\nPlay an empty card and draw the card $1$, then play $1$, $2$, $3$ in order."} +{"id": "02856", "text": "You are given two strings $s$ and $t$. The string $s$ consists of lowercase Latin letters and at most one wildcard character '*', the string $t$ consists only of lowercase Latin letters. The length of the string $s$ equals $n$, the length of the string $t$ equals $m$.\n\nThe wildcard character '*' in the string $s$ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $s$ can be replaced with anything. If it is possible to replace a wildcard character '*' in $s$ to obtain a string $t$, then the string $t$ matches the pattern $s$.\n\nFor example, if $s=$\"aba*aba\" then the following strings match it \"abaaba\", \"abacaba\" and \"abazzzaba\", but the following strings do not match: \"ababa\", \"abcaaba\", \"codeforces\", \"aba1aba\", \"aba?aba\".\n\nIf the given string $t$ matches the given string $s$, print \"YES\", otherwise print \"NO\".\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$) \u2014 the length of the string $s$ and the length of the string $t$, respectively.\n\nThe second line contains string $s$ of length $n$, which consists of lowercase Latin letters and at most one wildcard character '*'.\n\nThe third line contains string $t$ of length $m$, which consists only of lowercase Latin letters.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes), if you can obtain the string $t$ from the string $s$. Otherwise print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n6 10\ncode*s\ncodeforces\n\nOutput\nYES\n\nInput\n6 5\nvk*cup\nvkcup\n\nOutput\nYES\n\nInput\n1 1\nv\nk\n\nOutput\nNO\n\nInput\n9 6\ngfgf*gfgf\ngfgfgf\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example a wildcard character '*' can be replaced with a string \"force\". So the string $s$ after this replacement is \"codeforces\" and the answer is \"YES\".\n\nIn the second example a wildcard character '*' can be replaced with an empty string. So the string $s$ after this replacement is \"vkcup\" and the answer is \"YES\".\n\nThere is no wildcard character '*' in the third example and the strings \"v\" and \"k\" are different so the answer is \"NO\".\n\nIn the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string $t$ so the answer is \"NO\"."} +{"id": "02857", "text": "Andrey needs one more problem to conduct a programming contest. He has n friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends \u2014 the probability that this friend will come up with a problem if Andrey asks him.\n\nHelp Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100) \u2014 the number of Andrey's friends. The second line contains n real numbers p_{i} (0.0 \u2264 p_{i} \u2264 1.0) \u2014 the probability that the i-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point.\n\n\n-----Output-----\n\nPrint a single real number \u2014 the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10^{ - 9}.\n\n\n-----Examples-----\nInput\n4\n0.1 0.2 0.3 0.8\n\nOutput\n0.800000000000\n\nInput\n2\n0.1 0.2\n\nOutput\n0.260000000000\n\n\n\n-----Note-----\n\nIn the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one.\n\nIn the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1\u00b70.8 + 0.9\u00b70.2 = 0.26."} +{"id": "02858", "text": "On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v_1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v_2. In order to avoid seasick, each of the pupils want to get into the bus no more than once.\n\nDetermine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. \n\n\n-----Input-----\n\nThe first line of the input contains five positive integers n, l, v_1, v_2 and k (1 \u2264 n \u2264 10 000, 1 \u2264 l \u2264 10^9, 1 \u2264 v_1 < v_2 \u2264 10^9, 1 \u2264 k \u2264 n)\u00a0\u2014 the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. \n\n\n-----Output-----\n\nPrint the real number\u00a0\u2014 the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n5 10 1 2 5\n\nOutput\n5.0000000000\n\nInput\n3 6 1 2 1\n\nOutput\n4.7142857143\n\n\n\n-----Note-----\n\nIn the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5."} +{"id": "02859", "text": "Jeff got 2n real numbers a_1, a_2, ..., a_2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly \"adjust\" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: choose indexes i and j (i \u2260 j) that haven't been chosen yet; round element a_{i} to the nearest integer that isn't more than a_{i} (assign to a_{i}: \u230a a_{i}\u00a0\u230b); round element a_{j} to the nearest integer that isn't less than a_{j} (assign to a_{j}: \u2308 a_{j}\u00a0\u2309). \n\nNevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 2000). The next line contains 2n real numbers a_1, a_2, ..., a_2n (0 \u2264 a_{i} \u2264 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.\n\n\n-----Output-----\n\nIn a single line print a single real number \u2014 the required difference with exactly three digits after the decimal point.\n\n\n-----Examples-----\nInput\n3\n0.000 0.500 0.750 1.000 2.000 3.000\n\nOutput\n0.250\n\nInput\n3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896\n\nOutput\n0.279\n\n\n\n-----Note-----\n\nIn the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25."} +{"id": "02860", "text": "Polycarp takes part in a quadcopter competition. According to the rules a flying robot should:\n\n start the race from some point of a field, go around the flag, close cycle returning back to the starting point. \n\nPolycarp knows the coordinates of the starting point (x_1, y_1) and the coordinates of the point where the flag is situated (x_2, y_2). Polycarp\u2019s quadcopter can fly only parallel to the sides of the field each tick changing exactly one coordinate by 1. It means that in one tick the quadcopter can fly from the point (x, y) to any of four points: (x - 1, y), (x + 1, y), (x, y - 1) or (x, y + 1).\n\nThus the quadcopter path is a closed cycle starting and finishing in (x_1, y_1) and containing the point (x_2, y_2) strictly inside.\n\n [Image] The picture corresponds to the first example: the starting (and finishing) point is in (1, 5) and the flag is in (5, 2). \n\nWhat is the minimal length of the quadcopter path?\n\n\n-----Input-----\n\nThe first line contains two integer numbers x_1 and y_1 ( - 100 \u2264 x_1, y_1 \u2264 100) \u2014 coordinates of the quadcopter starting (and finishing) point.\n\nThe second line contains two integer numbers x_2 and y_2 ( - 100 \u2264 x_2, y_2 \u2264 100) \u2014 coordinates of the flag.\n\nIt is guaranteed that the quadcopter starting point and the flag do not coincide.\n\n\n-----Output-----\n\nPrint the length of minimal path of the quadcopter to surround the flag and return back.\n\n\n-----Examples-----\nInput\n1 5\n5 2\n\nOutput\n18\n\nInput\n0 1\n0 0\n\nOutput\n8"} +{"id": "02861", "text": "Let's introduce a number system which is based on a roman digits. There are digits I, V, X, L which correspond to the numbers $1$, $5$, $10$ and $50$ respectively. The use of other roman digits is not allowed.\n\nNumbers in this system are written as a sequence of one or more digits. We define the value of the sequence simply as the sum of digits in it.\n\nFor example, the number XXXV evaluates to $35$ and the number IXI\u00a0\u2014 to $12$.\n\nPay attention to the difference to the traditional roman system\u00a0\u2014 in our system any sequence of digits is valid, moreover the order of digits doesn't matter, for example IX means $11$, not $9$.\n\nOne can notice that this system is ambiguous, and some numbers can be written in many different ways. Your goal is to determine how many distinct integers can be represented by exactly $n$ roman digits I, V, X, L.\n\n\n-----Input-----\n\nThe only line of the input file contains a single integer $n$ ($1 \\le n \\le 10^9$)\u00a0\u2014 the number of roman digits to use.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the number of distinct integers which can be represented using $n$ roman digits exactly.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n4\n\nInput\n2\n\nOutput\n10\n\nInput\n10\n\nOutput\n244\n\n\n\n-----Note-----\n\nIn the first sample there are exactly $4$ integers which can be represented\u00a0\u2014 I, V, X and L.\n\nIn the second sample it is possible to represent integers $2$ (II), $6$ (VI), $10$ (VV), $11$ (XI), $15$ (XV), $20$ (XX), $51$ (IL), $55$ (VL), $60$ (XL) and $100$ (LL)."} +{"id": "02862", "text": "You are given an array $a_{1}, a_{2}, \\ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.\n\nIn other words, at most one time you can choose two integers $l$ and $r$ ($1 \\leq l \\leq r \\leq n$) and delete integers $a_l, a_{l+1}, \\ldots, a_r$ from the array. Remaining elements should be pairwise distinct. \n\nFind the minimum size of the subsegment you need to remove to make all remaining elements distinct.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer $n$ ($1 \\le n \\le 2000$)\u00a0\u2014 the number of elements in the given array.\n\nThe next line contains $n$ spaced integers $a_{1}, a_{2}, \\ldots, a_{n}$ ($1 \\le a_{i} \\le 10^{9}$)\u00a0\u2014 the elements of the array. \n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n0\n\nInput\n4\n1 1 2 2\n\nOutput\n2\n\nInput\n5\n1 4 1 4 9\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.\n\nIn the second example you can remove the subsegment from index $2$ to $3$.\n\nIn the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$."} +{"id": "02863", "text": "A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.\n\nB now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.\n\nYou have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print \"YES\", otherwise print \"NO\" (without the quotes).\n\n\n-----Input-----\n\nThe first and only line consists of a string $S$ ($ 1 \\le |S| \\le 5\\,000 $). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.\n\n\n-----Output-----\n\nPrint \"YES\" or \"NO\", according to the condition.\n\n\n-----Examples-----\nInput\naaabccc\n\nOutput\nYES\n\nInput\nbbacc\n\nOutput\nNO\n\nInput\naabc\n\nOutput\nYES\n\n\n\n-----Note-----\n\nConsider first example: the number of 'c' is equal to the number of 'a'. \n\nConsider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.\n\nConsider third example: the number of 'c' is equal to the number of 'b'."} +{"id": "02864", "text": "An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 100 000)\u00a0\u2014 the number of cars in the train. \n\nThe second line contains n integers p_{i} (1 \u2264 p_{i} \u2264 n, p_{i} \u2260 p_{j} if i \u2260 j)\u00a0\u2014 the sequence of the numbers of the cars in the train.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of actions needed to sort the railway cars.\n\n\n-----Examples-----\nInput\n5\n4 1 2 5 3\n\nOutput\n2\n\nInput\n4\n4 1 3 2\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train."} +{"id": "02865", "text": "Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.\n\nThe dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability $\\frac{1}{m}$. Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.\n\n\n-----Input-----\n\nA single line contains two integers m and n (1 \u2264 m, n \u2264 10^5).\n\n\n-----Output-----\n\nOutput a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 ^{ - 4}.\n\n\n-----Examples-----\nInput\n6 1\n\nOutput\n3.500000000000\n\nInput\n6 3\n\nOutput\n4.958333333333\n\nInput\n2 2\n\nOutput\n1.750000000000\n\n\n\n-----Note-----\n\nConsider the third test example. If you've made two tosses: You can get 1 in the first toss, and 2 in the second. Maximum equals to 2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2. \n\nThe probability of each outcome is 0.25, that is expectation equals to: $(2 + 1 + 2 + 2) \\cdot 0.25 = \\frac{7}{4}$\n\nYou can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value"} +{"id": "02866", "text": "In the snake exhibition, there are $n$ rooms (numbered $0$ to $n - 1$) arranged in a circle, with a snake in each room. The rooms are connected by $n$ conveyor belts, and the $i$-th conveyor belt connects the rooms $i$ and $(i+1) \\bmod n$. In the other words, rooms $0$ and $1$, $1$ and $2$, $\\ldots$, $n-2$ and $n-1$, $n-1$ and $0$ are connected with conveyor belts.\n\nThe $i$-th conveyor belt is in one of three states: If it is clockwise, snakes can only go from room $i$ to $(i+1) \\bmod n$. If it is anticlockwise, snakes can only go from room $(i+1) \\bmod n$ to $i$. If it is off, snakes can travel in either direction. [Image] \n\nAbove is an example with $4$ rooms, where belts $0$ and $3$ are off, $1$ is clockwise, and $2$ is anticlockwise.\n\nEach snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there?\n\n\n-----Input-----\n\nEach test contains multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 1000$): the number of test cases. The description of the test cases follows. \n\n The first line of each test case description contains a single integer $n$ ($2 \\le n \\le 300\\,000$): the number of rooms.\n\n The next line of each test case description contains a string $s$ of length $n$, consisting of only '<', '>' and '-'. If $s_{i} = $ '>', the $i$-th conveyor belt goes clockwise. If $s_{i} = $ '<', the $i$-th conveyor belt goes anticlockwise. If $s_{i} = $ '-', the $i$-th conveyor belt is off. \n\nIt is guaranteed that the sum of $n$ among all test cases does not exceed $300\\,000$.\n\n\n-----Output-----\n\nFor each test case, output the number of returnable rooms.\n\n\n-----Example-----\nInput\n4\n4\n-><-\n5\n>>>>>\n3\n<--\n2\n<>\n\nOutput\n3\n5\n3\n0\n\n\n\n-----Note-----\n\nIn the first test case, all rooms are returnable except room $2$. The snake in the room $2$ is trapped and cannot exit. This test case corresponds to the picture from the problem statement.\n\n In the second test case, all rooms are returnable by traveling on the series of clockwise belts."} +{"id": "02867", "text": "Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy!\n\nThe Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street.\n\nThe street is split into n equal length parts from left to right, the i-th part is characterized by two integers: width of road s_{i} and width of lawn g_{i}. [Image] \n\nFor each of n parts the Mayor should decide the size of lawn to demolish. For the i-th part he can reduce lawn width by integer x_{i} (0 \u2264 x_{i} \u2264 g_{i}). After it new road width of the i-th part will be equal to s'_{i} = s_{i} + x_{i} and new lawn width will be equal to g'_{i} = g_{i} - x_{i}.\n\nOn the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each i (1 \u2264 i < n) the inequation |s'_{i} + 1 - s'_{i}| \u2264 1 should hold. Initially this condition might not be true.\n\nYou need to find the the total width of lawns the Mayor will destroy according to his plan.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 2\u00b710^5) \u2014 number of parts of the street.\n\nEach of the following n lines contains two integers s_{i}, g_{i} (1 \u2264 s_{i} \u2264 10^6, 0 \u2264 g_{i} \u2264 10^6) \u2014 current width of road and width of the lawn on the i-th part of the street.\n\n\n-----Output-----\n\nIn the first line print the total width of lawns which will be removed.\n\nIn the second line print n integers s'_1, s'_2, ..., s'_{n} (s_{i} \u2264 s'_{i} \u2264 s_{i} + g_{i}) \u2014 new widths of the road starting from the first part and to the last.\n\nIf there is no solution, print the only integer -1 in the first line.\n\n\n-----Examples-----\nInput\n3\n4 5\n4 5\n4 10\n\nOutput\n16\n9 9 10 \n\nInput\n4\n1 100\n100 1\n1 100\n100 1\n\nOutput\n202\n101 101 101 101 \n\nInput\n3\n1 1\n100 100\n1 1\n\nOutput\n-1"} +{"id": "02868", "text": "Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. \n\nFrom the window in your room, you see the sequence of n hills, where i-th of them has height a_{i}. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.\n\nThe Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?\n\nHowever, the exact value of k is not yet determined, so could you please calculate answers for all k in range $1 \\leq k \\leq \\lceil \\frac{n}{2} \\rceil$? Here $\\lceil \\frac{n}{2} \\rceil$ denotes n divided by two, rounded up.\n\n\n-----Input-----\n\nThe first line of input contains the only integer n (1 \u2264 n \u2264 5000)\u2014the number of the hills in the sequence.\n\nSecond line contains n integers a_{i} (1 \u2264 a_{i} \u2264 100 000)\u2014the heights of the hills in the sequence.\n\n\n-----Output-----\n\nPrint exactly $\\lceil \\frac{n}{2} \\rceil$ numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses.\n\n\n-----Examples-----\nInput\n5\n1 1 1 1 1\n\nOutput\n1 2 2 \n\nInput\n3\n1 2 3\n\nOutput\n0 2 \n\nInput\n5\n1 2 3 2 2\n\nOutput\n0 1 3 \n\n\n\n-----Note-----\n\nIn the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.\n\nIn the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction."} +{"id": "02869", "text": "The new camp by widely-known over the country Spring Programming Camp is going to start soon. Hence, all the team of friendly curators and teachers started composing the camp's schedule. After some continuous discussion, they came up with a schedule $s$, which can be represented as a binary string, in which the $i$-th symbol is '1' if students will write the contest in the $i$-th day and '0' if they will have a day off.\n\nAt the last moment Gleb said that the camp will be the most productive if it runs with the schedule $t$ (which can be described in the same format as schedule $s$). Since the number of days in the current may be different from number of days in schedule $t$, Gleb required that the camp's schedule must be altered so that the number of occurrences of $t$ in it as a substring is maximum possible. At the same time, the number of contest days and days off shouldn't change, only their order may change.\n\nCould you rearrange the schedule in the best possible way?\n\n\n-----Input-----\n\nThe first line contains string $s$ ($1 \\leqslant |s| \\leqslant 500\\,000$), denoting the current project of the camp's schedule.\n\nThe second line contains string $t$ ($1 \\leqslant |t| \\leqslant 500\\,000$), denoting the optimal schedule according to Gleb.\n\nStrings $s$ and $t$ contain characters '0' and '1' only.\n\n\n-----Output-----\n\nIn the only line print the schedule having the largest number of substrings equal to $t$. Printed schedule should consist of characters '0' and '1' only and the number of zeros should be equal to the number of zeros in $s$ and the number of ones should be equal to the number of ones in $s$.\n\nIn case there multiple optimal schedules, print any of them.\n\n\n-----Examples-----\nInput\n101101\n110\n\nOutput\n110110\nInput\n10010110\n100011\n\nOutput\n01100011\n\nInput\n10\n11100\n\nOutput\n01\n\n\n-----Note-----\n\nIn the first example there are two occurrences, one starting from first position and one starting from fourth position.\n\nIn the second example there is only one occurrence, which starts from third position. Note, that the answer is not unique. For example, if we move the first day (which is a day off) to the last position, the number of occurrences of $t$ wouldn't change.\n\nIn the third example it's impossible to make even a single occurrence."} +{"id": "02870", "text": "Sereja has an n \u00d7 m rectangular table a, each cell of the table contains a zero or a number one. Sereja wants his table to meet the following requirement: each connected component of the same values forms a rectangle with sides parallel to the sides of the table. Rectangles should be filled with cells, that is, if a component form a rectangle of size h \u00d7 w, then the component must contain exactly hw cells.\n\nA connected component of the same values is a set of cells of the table that meet the following conditions: every two cells of the set have the same value; the cells of the set form a connected region on the table (two cells are connected if they are adjacent in some row or some column of the table); it is impossible to add any cell to the set unless we violate the two previous conditions. \n\nCan Sereja change the values of at most k cells of the table so that the table met the described requirement? What minimum number of table cells should he change in this case?\n\n\n-----Input-----\n\nThe first line contains integers n, m and k (1 \u2264 n, m \u2264 100;\u00a01 \u2264 k \u2264 10). Next n lines describe the table a: the i-th of them contains m integers a_{i}1, a_{i}2, ..., a_{im} (0 \u2264 a_{i}, j \u2264 1) \u2014 the values in the cells of the i-th row.\n\n\n-----Output-----\n\nPrint -1, if it is impossible to meet the requirement. Otherwise, print the minimum number of cells which should be changed.\n\n\n-----Examples-----\nInput\n5 5 2\n1 1 1 1 1\n1 1 1 1 1\n1 1 0 1 1\n1 1 1 1 1\n1 1 1 1 1\n\nOutput\n1\n\nInput\n3 4 1\n1 0 0 0\n0 1 1 1\n1 1 1 0\n\nOutput\n-1\n\nInput\n3 4 1\n1 0 0 1\n0 1 1 0\n1 0 0 1\n\nOutput\n0"} +{"id": "02871", "text": "Sasha grew up and went to first grade. To celebrate this event her mother bought her a multiplication table $M$ with $n$ rows and $n$ columns such that $M_{ij}=a_i \\cdot a_j$ where $a_1, \\dots, a_n$ is some sequence of positive integers.\n\nOf course, the girl decided to take it to school with her. But while she was having lunch, hooligan Grisha erased numbers on the main diagonal and threw away the array $a_1, \\dots, a_n$. Help Sasha restore the array!\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($3 \\leqslant n \\leqslant 10^3$), the size of the table. \n\nThe next $n$ lines contain $n$ integers each. The $j$-th number of the $i$-th line contains the number $M_{ij}$ ($1 \\leq M_{ij} \\leq 10^9$). The table has zeroes on the main diagonal, that is, $M_{ii}=0$.\n\n\n-----Output-----\n\nIn a single line print $n$ integers, the original array $a_1, \\dots, a_n$ ($1 \\leq a_i \\leq 10^9$). It is guaranteed that an answer exists. If there are multiple answers, print any.\n\n\n-----Examples-----\nInput\n5\n0 4 6 2 4\n4 0 6 2 4\n6 6 0 3 6\n2 2 3 0 2\n4 4 6 2 0\n\nOutput\n2 2 3 1 2 \nInput\n3\n0 99990000 99970002\n99990000 0 99980000\n99970002 99980000 0\n\nOutput\n9999 10000 9998"} +{"id": "02872", "text": "Elections in Berland are coming. There are only two candidates \u2014 Alice and Bob.\n\nThe main Berland TV channel plans to show political debates. There are $n$ people who want to take part in the debate as a spectator. Each person is described by their influence and political views. There are four kinds of political views: supporting none of candidates (this kind is denoted as \"00\"), supporting Alice but not Bob (this kind is denoted as \"10\"), supporting Bob but not Alice (this kind is denoted as \"01\"), supporting both candidates (this kind is denoted as \"11\"). \n\nThe direction of the TV channel wants to invite some of these people to the debate. The set of invited spectators should satisfy three conditions: at least half of spectators support Alice (i.e. $2 \\cdot a \\ge m$, where $a$ is number of spectators supporting Alice and $m$ is the total number of spectators), at least half of spectators support Bob (i.e. $2 \\cdot b \\ge m$, where $b$ is number of spectators supporting Bob and $m$ is the total number of spectators), the total influence of spectators is maximal possible. \n\nHelp the TV channel direction to select such non-empty set of spectators, or tell that this is impossible.\n\n\n-----Input-----\n\nThe first line contains integer $n$ ($1 \\le n \\le 4\\cdot10^5$) \u2014 the number of people who want to take part in the debate as a spectator.\n\nThese people are described on the next $n$ lines. Each line describes a single person and contains the string $s_i$ and integer $a_i$ separated by space ($1 \\le a_i \\le 5000$), where $s_i$ denotes person's political views (possible values \u2014 \"00\", \"10\", \"01\", \"11\") and $a_i$ \u2014 the influence of the $i$-th person.\n\n\n-----Output-----\n\nPrint a single integer \u2014 maximal possible total influence of a set of spectators so that at least half of them support Alice and at least half of them support Bob. If it is impossible print 0 instead.\n\n\n-----Examples-----\nInput\n6\n11 6\n10 4\n01 3\n00 3\n00 7\n00 9\n\nOutput\n22\n\nInput\n5\n11 1\n01 1\n00 100\n10 1\n01 1\n\nOutput\n103\n\nInput\n6\n11 19\n10 22\n00 18\n00 29\n11 29\n10 28\n\nOutput\n105\n\nInput\n3\n00 5000\n00 5000\n00 5000\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example $4$ spectators can be invited to maximize total influence: $1$, $2$, $3$ and $6$. Their political views are: \"11\", \"10\", \"01\" and \"00\". So in total $2$ out of $4$ spectators support Alice and $2$ out of $4$ spectators support Bob. The total influence is $6+4+3+9=22$.\n\nIn the second example the direction can select all the people except the $5$-th person.\n\nIn the third example the direction can select people with indices: $1$, $4$, $5$ and $6$.\n\nIn the fourth example it is impossible to select any non-empty set of spectators."} +{"id": "02873", "text": "Soon there will be held the world's largest programming contest, but the testing system still has m bugs. The contest organizer, a well-known university, has no choice but to attract university students to fix all the bugs. The university has n students able to perform such work. The students realize that they are the only hope of the organizers, so they don't want to work for free: the i-th student wants to get c_{i} 'passes' in his subjects (regardless of the volume of his work).\n\nBugs, like students, are not the same: every bug is characterized by complexity a_{j}, and every student has the level of his abilities b_{i}. Student i can fix a bug j only if the level of his abilities is not less than the complexity of the bug: b_{i} \u2265 a_{j}, and he does it in one day. Otherwise, the bug will have to be fixed by another student. Of course, no student can work on a few bugs in one day. All bugs are not dependent on each other, so they can be corrected in any order, and different students can work simultaneously.\n\nThe university wants to fix all the bugs as quickly as possible, but giving the students the total of not more than s passes. Determine which students to use for that and come up with the schedule of work saying which student should fix which bug.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers: n, m and s (1 \u2264 n, m \u2264 10^5, 0 \u2264 s \u2264 10^9)\u00a0\u2014 the number of students, the number of bugs in the system and the maximum number of passes the university is ready to give the students.\n\nThe next line contains m space-separated integers a_1, a_2,\u00a0..., a_{m} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the bugs' complexities.\n\nThe next line contains n space-separated integers b_1, b_2,\u00a0..., b_{n} (1 \u2264 b_{i} \u2264 10^9)\u00a0\u2014 the levels of the students' abilities.\n\nThe next line contains n space-separated integers c_1, c_2,\u00a0..., c_{n} (0 \u2264 c_{i} \u2264 10^9)\u00a0\u2014 the numbers of the passes the students want to get for their help.\n\n\n-----Output-----\n\nIf the university can't correct all bugs print \"NO\".\n\nOtherwise, on the first line print \"YES\", and on the next line print m space-separated integers: the i-th of these numbers should equal the number of the student who corrects the i-th bug in the optimal answer. The bugs should be corrected as quickly as possible (you must spend the minimum number of days), and the total given passes mustn't exceed s. If there are multiple optimal answers, you can output any of them.\n\n\n-----Examples-----\nInput\n3 4 9\n1 3 1 2\n2 1 3\n4 3 6\n\nOutput\nYES\n2 3 2 3\n\nInput\n3 4 10\n2 3 1 2\n2 1 3\n4 3 6\n\nOutput\nYES\n1 3 1 3\n\nInput\n3 4 9\n2 3 1 2\n2 1 3\n4 3 6\n\nOutput\nYES\n3 3 2 3\n\nInput\n3 4 5\n1 3 1 2\n2 1 3\n5 3 6\n\nOutput\nNO\n\n\n\n-----Note-----\n\nConsider the first sample.\n\nThe third student (with level 3) must fix the 2nd and 4th bugs (complexities 3 and 2 correspondingly) and the second student (with level 1) must fix the 1st and 3rd bugs (their complexity also equals 1). Fixing each bug takes one day for each student, so it takes 2 days to fix all bugs (the students can work in parallel).\n\nThe second student wants 3 passes for his assistance, the third student wants 6 passes. It meets the university's capabilities as it is ready to give at most 9 passes."} +{"id": "02874", "text": "Little Petya likes permutations a lot. Recently his mom has presented him permutation q_1, q_2, ..., q_{n} of length n.\n\nA permutation a of length n is a sequence of integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 n), all integers there are distinct. \n\nThere is only one thing Petya likes more than permutations: playing with little Masha. As it turns out, Masha also has a permutation of length n. Petya decided to get the same permutation, whatever the cost may be. For that, he devised a game with the following rules: Before the beginning of the game Petya writes permutation 1, 2, ..., n on the blackboard. After that Petya makes exactly k moves, which are described below. During a move Petya tosses a coin. If the coin shows heads, he performs point 1, if the coin shows tails, he performs point 2. Let's assume that the board contains permutation p_1, p_2, ..., p_{n} at the given moment. Then Petya removes the written permutation p from the board and writes another one instead: p_{q}_1, p_{q}_2, ..., p_{q}_{n}. In other words, Petya applies permutation q (which he has got from his mother) to permutation p. All actions are similar to point 1, except that Petya writes permutation t on the board, such that: t_{q}_{i} = p_{i} for all i from 1 to n. In other words, Petya applies a permutation that is inverse to q to permutation p. \n\nWe know that after the k-th move the board contained Masha's permutation s_1, s_2, ..., s_{n}. Besides, we know that throughout the game process Masha's permutation never occurred on the board before the k-th move. Note that the game has exactly k moves, that is, throughout the game the coin was tossed exactly k times.\n\nYour task is to determine whether the described situation is possible or else state that Petya was mistaken somewhere. See samples and notes to them for a better understanding.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n, k \u2264 100). The second line contains n space-separated integers q_1, q_2, ..., q_{n} (1 \u2264 q_{i} \u2264 n) \u2014 the permutation that Petya's got as a present. The third line contains Masha's permutation s, in the similar format.\n\nIt is guaranteed that the given sequences q and s are correct permutations.\n\n\n-----Output-----\n\nIf the situation that is described in the statement is possible, print \"YES\" (without the quotes), otherwise print \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n4 1\n2 3 4 1\n1 2 3 4\n\nOutput\nNO\n\nInput\n4 1\n4 3 1 2\n3 4 2 1\n\nOutput\nYES\n\nInput\n4 3\n4 3 1 2\n3 4 2 1\n\nOutput\nYES\n\nInput\n4 2\n4 3 1 2\n2 1 4 3\n\nOutput\nYES\n\nInput\n4 1\n4 3 1 2\n2 1 4 3\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample Masha's permutation coincides with the permutation that was written on the board before the beginning of the game. Consequently, that violates the condition that Masha's permutation never occurred on the board before k moves were performed.\n\nIn the second sample the described situation is possible, in case if after we toss a coin, we get tails.\n\nIn the third sample the possible coin tossing sequence is: heads-tails-tails.\n\nIn the fourth sample the possible coin tossing sequence is: heads-heads."} +{"id": "02875", "text": "Ivan places knights on infinite chessboard. Initially there are $n$ knights. If there is free cell which is under attack of at least $4$ knights then he places new knight in this cell. Ivan repeats this until there are no such free cells. One can prove that this process is finite. One can also prove that position in the end does not depend on the order in which new knights are placed.\n\nIvan asked you to find initial placement of exactly $n$ knights such that in the end there will be at least $\\lfloor \\frac{n^{2}}{10} \\rfloor$ knights.\n\n\n-----Input-----\n\nThe only line of input contains one integer $n$ ($1 \\le n \\le 10^{3}$)\u00a0\u2014 number of knights in the initial placement.\n\n\n-----Output-----\n\nPrint $n$ lines. Each line should contain $2$ numbers $x_{i}$ and $y_{i}$ ($-10^{9} \\le x_{i}, \\,\\, y_{i} \\le 10^{9}$)\u00a0\u2014 coordinates of $i$-th knight. For all $i \\ne j$, $(x_{i}, \\,\\, y_{i}) \\ne (x_{j}, \\,\\, y_{j})$ should hold. In other words, all knights should be in different cells.\n\nIt is guaranteed that the solution exists.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n1 1\n3 1\n1 5\n4 4\n\nInput\n7\n\nOutput\n2 1\n1 2\n4 1\n5 2\n2 6\n5 7\n6 6\n\n\n\n-----Note-----\n\nLet's look at second example:\n\n$\\left. \\begin{array}{|l|l|l|l|l|l|l|l|l|} \\hline 7 & {} & {} & {} & {} & {0} & {} & {} \\\\ \\hline 6 & {} & {0} & {} & {} & {} & {0} & {} \\\\ \\hline 5 & {} & {} & {} & {2} & {} & {} & {} \\\\ \\hline 4 & {} & {} & {} & {} & {} & {} & {} \\\\ \\hline 3 & {} & {} & {1} & {} & {} & {} & {} \\\\ \\hline 2 & {0} & {} & {} & {} & {0} & {} & {} \\\\ \\hline 1 & {} & {0} & {} & {0} & {} & {} & {} \\\\ \\hline & {1} & {2} & {3} & {4} & {5} & {6} & {7} \\\\ \\hline \\end{array} \\right.$\n\nGreen zeroes are initial knights. Cell $(3, \\,\\, 3)$ is under attack of $4$ knights in cells $(1, \\,\\, 2)$, $(2, \\,\\, 1)$, $(4, \\,\\, 1)$ and $(5, \\,\\, 2)$, therefore Ivan will place a knight in this cell. Cell $(4, \\,\\, 5)$ is initially attacked by only $3$ knights in cells $(2, \\,\\, 6)$, $(5, \\,\\, 7)$ and $(6, \\,\\, 6)$. But new knight in cell $(3, \\,\\, 3)$ also attacks cell $(4, \\,\\, 5)$, now it is attacked by $4$ knights and Ivan will place another knight in this cell. There are no more free cells which are attacked by $4$ or more knights, so the process stops. There are $9$ knights in the end, which is not less than $\\lfloor \\frac{7^{2}}{10} \\rfloor = 4$."} +{"id": "02876", "text": "Two pirates Polycarpus and Vasily play a very interesting game. They have n chests with coins, the chests are numbered with integers from 1 to n. Chest number i has a_{i} coins. \n\nPolycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer x (2\u00b7x + 1 \u2264 n) and take a coin from each chest with numbers x, 2\u00b7x, 2\u00b7x + 1. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.\n\nPolycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 100) \u2014 the number of chests with coins. The second line contains a sequence of space-separated integers: a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 1000), where a_{i} is the number of coins in the chest number i at the beginning of the game.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.\n\n\n-----Examples-----\nInput\n1\n1\n\nOutput\n-1\n\nInput\n3\n1 2 3\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.\n\nIn the second sample there is only one possible move x = 1. This move should be repeated at least 3 times to empty the third chest."} +{"id": "02877", "text": "In order to put away old things and welcome a fresh new year, a thorough cleaning of the house is a must.\n\nLittle Tommy finds an old polynomial and cleaned it up by taking it modulo another. But now he regrets doing this...\n\nGiven two integers p and k, find a polynomial f(x) with non-negative integer coefficients strictly less than k, whose remainder is p when divided by (x + k). That is, f(x) = q(x)\u00b7(x + k) + p, where q(x) is a polynomial (not necessarily with integer coefficients).\n\n\n-----Input-----\n\nThe only line of input contains two space-separated integers p and k (1 \u2264 p \u2264 10^18, 2 \u2264 k \u2264 2 000).\n\n\n-----Output-----\n\nIf the polynomial does not exist, print a single integer -1, or output two lines otherwise.\n\nIn the first line print a non-negative integer d \u2014 the number of coefficients in the polynomial.\n\nIn the second line print d space-separated integers a_0, a_1, ..., a_{d} - 1, describing a polynomial $f(x) = \\sum_{i = 0}^{d - 1} a_{i} \\cdot x^{i}$ fulfilling the given requirements. Your output should satisfy 0 \u2264 a_{i} < k for all 0 \u2264 i \u2264 d - 1, and a_{d} - 1 \u2260 0.\n\nIf there are many possible solutions, print any of them.\n\n\n-----Examples-----\nInput\n46 2\n\nOutput\n7\n0 1 0 0 1 1 1\n\nInput\n2018 214\n\nOutput\n3\n92 205 1\n\n\n\n-----Note-----\n\nIn the first example, f(x) = x^6 + x^5 + x^4 + x = (x^5 - x^4 + 3x^3 - 6x^2 + 12x - 23)\u00b7(x + 2) + 46.\n\nIn the second example, f(x) = x^2 + 205x + 92 = (x - 9)\u00b7(x + 214) + 2018."} +{"id": "02878", "text": "Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired flat in Moscow costs q dollars. Mikhail is determined to follow his dreams and registered at a freelance site.\n\nHe has suggestions to work on n distinct projects. Mikhail has already evaluated that the participation in the i-th project will increase his experience by a_{i} per day and bring b_{i} dollars per day. As freelance work implies flexible working hours, Mikhail is free to stop working on one project at any time and start working on another project. Doing so, he receives the respective share of experience and money. Mikhail is only trying to become a cool programmer, so he is able to work only on one project at any moment of time.\n\nFind the real value, equal to the minimum number of days Mikhail needs to make his dream come true.\n\nFor example, suppose Mikhail is suggested to work on three projects and a_1 = 6, b_1 = 2, a_2 = 1, b_2 = 3, a_3 = 2, b_3 = 6. Also, p = 20 and q = 20. In order to achieve his aims Mikhail has to work for 2.5 days on both first and third projects. Indeed, a_1\u00b72.5 + a_2\u00b70 + a_3\u00b72.5 = 6\u00b72.5 + 1\u00b70 + 2\u00b72.5 = 20 and b_1\u00b72.5 + b_2\u00b70 + b_3\u00b72.5 = 2\u00b72.5 + 3\u00b70 + 6\u00b72.5 = 20.\n\n\n-----Input-----\n\nThe first line of the input contains three integers n, p and q (1 \u2264 n \u2264 100 000, 1 \u2264 p, q \u2264 1 000 000)\u00a0\u2014 the number of projects and the required number of experience and money.\n\nEach of the next n lines contains two integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 1 000 000)\u00a0\u2014 the daily increase in experience and daily income for working on the i-th project.\n\n\n-----Output-----\n\nPrint a real value\u00a0\u2014 the minimum number of days Mikhail needs to get the required amount of experience and money. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. \n\nNamely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\\frac{|a - b|}{\\operatorname{max}(1, b)} \\leq 10^{-6}$.\n\n\n-----Examples-----\nInput\n3 20 20\n6 2\n1 3\n2 6\n\nOutput\n5.000000000000000\n\nInput\n4 1 1\n2 3\n3 2\n2 3\n3 2\n\nOutput\n0.400000000000000\n\n\n\n-----Note-----\n\nFirst sample corresponds to the example in the problem statement."} +{"id": "02879", "text": "Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.\n\nFirst, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation.\n\nNote that to display number 0 section of the watches is required to have at least one place.\n\nLittle robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number.\n\n\n-----Input-----\n\nThe first line of the input contains two integers, given in the decimal notation, n and m (1 \u2264 n, m \u2264 10^9)\u00a0\u2014 the number of hours in one day and the number of minutes in one hour, respectively.\n\n\n-----Output-----\n\nPrint one integer in decimal notation\u00a0\u2014 the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.\n\n\n-----Examples-----\nInput\n2 3\n\nOutput\n4\n\nInput\n8 2\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2).\n\nIn the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1)."} +{"id": "02880", "text": "Edogawa Conan got tired of solving cases, and invited his friend, Professor Agasa, over. They decided to play a game of cards. Conan has n cards, and the i-th card has a number a_{i} written on it.\n\nThey take turns playing, starting with Conan. In each turn, the player chooses a card and removes it. Also, he removes all cards having a number strictly lesser than the number on the chosen card. Formally, if the player chooses the i-th card, he removes that card and removes the j-th card for all j such that a_{j} < a_{i}.\n\nA player loses if he cannot make a move on his turn, that is, he loses if there are no cards left. Predict the outcome of the game, assuming both players play optimally.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of cards Conan has. \n\nThe next line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5), where a_{i} is the number on the i-th card.\n\n\n-----Output-----\n\nIf Conan wins, print \"Conan\" (without quotes), otherwise print \"Agasa\" (without quotes).\n\n\n-----Examples-----\nInput\n3\n4 5 7\n\nOutput\nConan\n\nInput\n2\n1 1\n\nOutput\nAgasa\n\n\n\n-----Note-----\n\nIn the first example, Conan can just choose the card having number 7 on it and hence remove all the cards. After that, there are no cards left on Agasa's turn.\n\nIn the second example, no matter which card Conan chooses, there will be one one card left, which Agasa can choose. After that, there are no cards left when it becomes Conan's turn again."} +{"id": "02881", "text": "You are given two integers A and B.\nPrint a grid where each square is painted white or black that satisfies the following conditions, in the format specified in Output section:\n - Let the size of the grid be h \\times w (h vertical, w horizontal). Both h and w are at most 100.\n - The set of the squares painted white is divided into exactly A connected components.\n - The set of the squares painted black is divided into exactly B connected components.\nIt can be proved that there always exist one or more solutions under the conditions specified in Constraints section.\nIf there are multiple solutions, any of them may be printed.\n\n-----Notes-----\nTwo squares painted white, c_1 and c_2, are called connected when the square c_2 can be reached from the square c_1 passing only white squares by repeatedly moving up, down, left or right to an adjacent square.\nA set of squares painted white, S, forms a connected component when the following conditions are met:\n - Any two squares in S are connected.\n - No pair of a square painted white that is not included in S and a square included in S is connected.\nA connected component of squares painted black is defined similarly.\n\n-----Constraints-----\n - 1 \\leq A \\leq 500\n - 1 \\leq B \\leq 500\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nOutput should be in the following format:\n - In the first line, print integers h and w representing the size of the grid you constructed, with a space in between.\n - Then, print h more lines. The i-th (1 \\leq i \\leq h) of these lines should contain a string s_i as follows:\n - If the square at the i-th row and j-th column (1 \\leq j \\leq w) in the grid is painted white, the j-th character in s_i should be ..\n - If the square at the i-th row and j-th column (1 \\leq j \\leq w) in the grid is painted black, the j-th character in s_i should be #.\n\n-----Sample Input-----\n2 3\n\n-----Sample Output-----\n3 3\n##.\n..#\n#.#\n\nThis output corresponds to the grid below:"} +{"id": "02882", "text": "Nearly each project of the F company has a whole team of developers working on it. They often are in different rooms of the office in different cities and even countries. To keep in touch and track the results of the project, the F company conducts shared online meetings in a Spyke chat.\n\nOne day the director of the F company got hold of the records of a part of an online meeting of one successful team. The director watched the record and wanted to talk to the team leader. But how can he tell who the leader is? The director logically supposed that the leader is the person who is present at any conversation during a chat meeting. In other words, if at some moment of time at least one person is present on the meeting, then the leader is present on the meeting.\n\nYou are the assistant director. Given the 'user logged on'/'user logged off' messages of the meeting in the chronological order, help the director determine who can be the leader. Note that the director has the record of only a continuous part of the meeting (probably, it's not the whole meeting).\n\n\n-----Input-----\n\nThe first line contains integers n and m (1 \u2264 n, m \u2264 10^5) \u2014 the number of team participants and the number of messages. Each of the next m lines contains a message in the format: '+ id': the record means that the person with number id (1 \u2264 id \u2264 n) has logged on to the meeting. '- id': the record means that the person with number id (1 \u2264 id \u2264 n) has logged off from the meeting. \n\nAssume that all the people of the team are numbered from 1 to n and the messages are given in the chronological order. It is guaranteed that the given sequence is the correct record of a continuous part of the meeting. It is guaranteed that no two log on/log off events occurred simultaneously.\n\n\n-----Output-----\n\nIn the first line print integer k (0 \u2264 k \u2264 n) \u2014 how many people can be leaders. In the next line, print k integers in the increasing order \u2014 the numbers of the people who can be leaders.\n\nIf the data is such that no member of the team can be a leader, print a single number 0.\n\n\n-----Examples-----\nInput\n5 4\n+ 1\n+ 2\n- 2\n- 1\n\nOutput\n4\n1 3 4 5 \nInput\n3 2\n+ 1\n- 2\n\nOutput\n1\n3 \nInput\n2 4\n+ 1\n- 1\n+ 2\n- 2\n\nOutput\n0\n\nInput\n5 6\n+ 1\n- 1\n- 3\n+ 3\n+ 4\n- 4\n\nOutput\n3\n2 3 5 \nInput\n2 4\n+ 1\n- 2\n+ 2\n- 1\n\nOutput\n0"} +{"id": "02883", "text": "You are given two arrays $a$ and $b$ of positive integers, with length $n$ and $m$ respectively. \n\nLet $c$ be an $n \\times m$ matrix, where $c_{i,j} = a_i \\cdot b_j$. \n\nYou need to find a subrectangle of the matrix $c$ such that the sum of its elements is at most $x$, and its area (the total number of elements) is the largest possible.\n\nFormally, you need to find the largest number $s$ such that it is possible to choose integers $x_1, x_2, y_1, y_2$ subject to $1 \\leq x_1 \\leq x_2 \\leq n$, $1 \\leq y_1 \\leq y_2 \\leq m$, $(x_2 - x_1 + 1) \\times (y_2 - y_1 + 1) = s$, and $$\\sum_{i=x_1}^{x_2}{\\sum_{j=y_1}^{y_2}{c_{i,j}}} \\leq x.$$\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\leq n, m \\leq 2000$).\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\leq a_i \\leq 2000$).\n\nThe third line contains $m$ integers $b_1, b_2, \\ldots, b_m$ ($1 \\leq b_i \\leq 2000$).\n\nThe fourth line contains a single integer $x$ ($1 \\leq x \\leq 2 \\cdot 10^{9}$).\n\n\n-----Output-----\n\nIf it is possible to choose four integers $x_1, x_2, y_1, y_2$ such that $1 \\leq x_1 \\leq x_2 \\leq n$, $1 \\leq y_1 \\leq y_2 \\leq m$, and $\\sum_{i=x_1}^{x_2}{\\sum_{j=y_1}^{y_2}{c_{i,j}}} \\leq x$, output the largest value of $(x_2 - x_1 + 1) \\times (y_2 - y_1 + 1)$ among all such quadruplets, otherwise output $0$.\n\n\n-----Examples-----\nInput\n3 3\n1 2 3\n1 2 3\n9\n\nOutput\n4\n\nInput\n5 1\n5 4 2 4 5\n2\n5\n\nOutput\n1\n\n\n\n-----Note-----\n\nMatrix from the first sample and the chosen subrectangle (of blue color):\n\n [Image] \n\nMatrix from the second sample and the chosen subrectangle (of blue color):\n\n $\\left. \\begin{array}{l l l l l}{10} & {8} & {4} & {8} & {10} \\end{array} \\right.$"} +{"id": "02884", "text": "Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.\n\nPaul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers: n and p (1 \u2264 n \u2264 1000; 1 \u2264 p \u2264 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).\n\n\n-----Output-----\n\nIf the lexicographically next tolerable string of the same length exists, print it. Otherwise, print \"NO\" (without the quotes).\n\n\n-----Examples-----\nInput\n3 3\ncba\n\nOutput\nNO\n\nInput\n3 4\ncba\n\nOutput\ncbd\n\nInput\n4 4\nabcd\n\nOutput\nabda\n\n\n\n-----Note-----\n\nString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s_1 = t_1, ..., s_{i} = t_{i}, s_{i} + 1 > t_{i} + 1.\n\nThe lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.\n\nA palindrome is a string that reads the same forward or reversed."} +{"id": "02885", "text": "\"Duel!\"\n\nBetting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.\n\nThere are $n$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $k$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $n$ cards face the same direction after one's move, the one who takes this move will win.\n\nPrincess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 10^5$).\n\nThe second line contains a single string of length $n$ that only consists of $0$ and $1$, representing the situation of these $n$ cards, where the color side of the $i$-th card faces up if the $i$-th character is $1$, or otherwise, it faces down and the $i$-th character is $0$.\n\n\n-----Output-----\n\nPrint \"once again\" (without quotes) if the total number of their moves can exceed $10^9$, which is considered a draw.\n\nIn other cases, print \"tokitsukaze\" (without quotes) if Tokitsukaze will win, or \"quailty\" (without quotes) if Quailty will win.\n\nNote that the output characters are case-sensitive, and any wrong spelling would be rejected.\n\n\n-----Examples-----\nInput\n4 2\n0101\n\nOutput\nquailty\n\nInput\n6 1\n010101\n\nOutput\nonce again\n\nInput\n6 5\n010101\n\nOutput\ntokitsukaze\n\nInput\n4 1\n0011\n\nOutput\nonce again\n\n\n\n-----Note-----\n\nIn the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win.\n\nIn the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw.\n\nIn the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down.\n\nThe fourth example can be explained in the same way as the second example does."} +{"id": "02886", "text": "There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.\n\nYou are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.\n\n\n-----Input-----\n\nThe first line contains three integers n, k and p (1 \u2264 n \u2264 1 000, n \u2264 k \u2264 2 000, 1 \u2264 p \u2264 10^9) \u2014 the number of people, the number of keys and the office location.\n\nThe second line contains n distinct integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 positions in which people are located initially. The positions are given in arbitrary order.\n\nThe third line contains k distinct integers b_1, b_2, ..., b_{k} (1 \u2264 b_{j} \u2264 10^9) \u2014 positions of the keys. The positions are given in arbitrary order.\n\nNote that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.\n\n\n-----Output-----\n\nPrint the minimum time (in seconds) needed for all n to reach the office with keys.\n\n\n-----Examples-----\nInput\n2 4 50\n20 100\n60 10 40 80\n\nOutput\n50\n\nInput\n1 2 10\n11\n15 7\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys."} +{"id": "02887", "text": "Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through $n \\cdot k$ cities. The cities are numerated from $1$ to $n \\cdot k$, the distance between the neighboring cities is exactly $1$ km.\n\nSergey does not like beetles, he loves burgers. Fortunately for him, there are $n$ fast food restaurants on the circle, they are located in the $1$-st, the $(k + 1)$-st, the $(2k + 1)$-st, and so on, the $((n-1)k + 1)$-st cities, i.e. the distance between the neighboring cities with fast food restaurants is $k$ km.\n\nSergey began his journey at some city $s$ and traveled along the circle, making stops at cities each $l$ km ($l > 0$), until he stopped in $s$ once again. Sergey then forgot numbers $s$ and $l$, but he remembers that the distance from the city $s$ to the nearest fast food restaurant was $a$ km, and the distance from the city he stopped at after traveling the first $l$ km from $s$ to the nearest fast food restaurant was $b$ km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.\n\nNow Sergey is interested in two integers. The first integer $x$ is the minimum number of stops (excluding the first) Sergey could have done before returning to $s$. The second integer $y$ is the maximum number of stops (excluding the first) Sergey could have done before returning to $s$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n, k \\le 100\\,000$)\u00a0\u2014 the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.\n\nThe second line contains two integers $a$ and $b$ ($0 \\le a, b \\le \\frac{k}{2}$)\u00a0\u2014 the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.\n\n\n-----Output-----\n\nPrint the two integers $x$ and $y$.\n\n\n-----Examples-----\nInput\n2 3\n1 1\n\nOutput\n1 6\n\nInput\n3 2\n0 0\n\nOutput\n1 3\n\nInput\n1 10\n5 3\n\nOutput\n5 5\n\n\n\n-----Note-----\n\nIn the first example the restaurants are located in the cities $1$ and $4$, the initial city $s$ could be $2$, $3$, $5$, or $6$. The next city Sergey stopped at could also be at cities $2, 3, 5, 6$. Let's loop through all possible combinations of these cities. If both $s$ and the city of the first stop are at the city $2$ (for example, $l = 6$), then Sergey is at $s$ after the first stop already, so $x = 1$. In other pairs Sergey needs $1, 2, 3$, or $6$ stops to return to $s$, so $y = 6$.\n\nIn the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so $l$ is $2$, $4$, or $6$. Thus $x = 1$, $y = 3$.\n\nIn the third example there is only one restaurant, so the possible locations of $s$ and the first stop are: $(6, 8)$ and $(6, 4)$. For the first option $l = 2$, for the second $l = 8$. In both cases Sergey needs $x=y=5$ stops to go to $s$."} +{"id": "02888", "text": "Snuke has an integer sequence, a, of length N. The i-th element of a (1-indexed) is a_{i}.\nHe can perform the following operation any number of times:\n - Operation: Choose integers x and y between 1 and N (inclusive), and add a_x to a_y.\nHe would like to perform this operation between 0 and 2N times (inclusive) so that a satisfies the condition below. Show one such sequence of operations.\nIt can be proved that such a sequence of operations always exists under the constraints in this problem.\n - Condition: a_1 \\leq a_2 \\leq ... \\leq a_{N}\n\n-----Constraints-----\n - 2 \\leq N \\leq 50\n - -10^{6} \\leq a_i \\leq 10^{6}\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_{N}\n\n-----Output-----\nLet m be the number of operations in your solution. In the first line, print m.\nIn the i-th of the subsequent m lines, print the numbers x and y chosen in the i-th operation, with a space in between.\nThe output will be considered correct if m is between 0 and 2N (inclusive) and a satisfies the condition after the m operations.\n\n-----Sample Input-----\n3\n-2 5 -1\n\n-----Sample Output-----\n2\n2 3\n3 3\n\n - After the first operation, a = (-2,5,4).\n - After the second operation, a = (-2,5,8), and the condition is now satisfied."} +{"id": "02889", "text": "Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes.\n\nFrom the top view each magical box looks like a square with side length equal to 2^{k} (k is an integer, k \u2265 0) units. A magical box v can be put inside a magical box u, if side length of v is strictly less than the side length of u. In particular, Emuskald can put 4 boxes of side length 2^{k} - 1 into one box of side length 2^{k}, or as in the following figure:\n\n [Image] \n\nEmuskald is about to go on tour performing around the world, and needs to pack his magical boxes for the trip. He has decided that the best way to pack them would be inside another magical box, but magical boxes are quite expensive to make. Help him find the smallest magical box that can fit all his boxes.\n\n\n-----Input-----\n\nThe first line of input contains an integer n (1 \u2264 n \u2264 10^5), the number of different sizes of boxes Emuskald has. Each of following n lines contains two integers k_{i} and a_{i} (0 \u2264 k_{i} \u2264 10^9, 1 \u2264 a_{i} \u2264 10^9), which means that Emuskald has a_{i} boxes with side length 2^{k}_{i}. It is guaranteed that all of k_{i} are distinct.\n\n\n-----Output-----\n\nOutput a single integer p, such that the smallest magical box that can contain all of Emuskald\u2019s boxes has side length 2^{p}.\n\n\n-----Examples-----\nInput\n2\n0 3\n1 5\n\nOutput\n3\n\nInput\n1\n0 4\n\nOutput\n1\n\nInput\n2\n1 10\n2 2\n\nOutput\n3\n\n\n\n-----Note-----\n\nPicture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture.\n\nIn the second test case, we can put all four small boxes into a box with side length 2."} +{"id": "02890", "text": "Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is c_{i}. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.\n\nLooking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values x, such that Arya will be able to make x using some subset of coins with the sum k.\n\nFormally, Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum x using these coins.\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n, k \u2264 500)\u00a0\u2014 the number of coins and the price of the chocolate, respectively.\n\nNext line will contain n integers c_1, c_2, ..., c_{n} (1 \u2264 c_{i} \u2264 500)\u00a0\u2014 the values of Pari's coins.\n\nIt's guaranteed that one can make value k using these coins.\n\n\n-----Output-----\n\nFirst line of the output must contain a single integer q\u2014 the number of suitable values x. Then print q integers in ascending order\u00a0\u2014 the values that Arya can make for some subset of coins of Pari that pays for the chocolate.\n\n\n-----Examples-----\nInput\n6 18\n5 6 1 10 12 2\n\nOutput\n16\n0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18 \n\nInput\n3 50\n25 25 50\n\nOutput\n3\n0 25 50"} +{"id": "02891", "text": "You have $n$ coins, each of the same value of $1$.\n\nDistribute them into packets such that any amount $x$ ($1 \\leq x \\leq n$) can be formed using some (possibly one or all) number of these packets.\n\nEach packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single $x$, however it may be reused for the formation of other $x$'s.\n\nFind the minimum number of packets in such a distribution.\n\n\n-----Input-----\n\nThe only line contains a single integer $n$ ($1 \\leq n \\leq 10^9$)\u00a0\u2014 the number of coins you have.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the minimum possible number of packets, satisfying the condition above.\n\n\n-----Examples-----\nInput\n6\n\nOutput\n3\nInput\n2\n\nOutput\n2\n\n\n-----Note-----\n\nIn the first example, three packets with $1$, $2$ and $3$ coins can be made to get any amount $x$ ($1\\leq x\\leq 6$). To get $1$ use the packet with $1$ coin. To get $2$ use the packet with $2$ coins. To get $3$ use the packet with $3$ coins. To get $4$ use packets with $1$ and $3$ coins. To get $5$ use packets with $2$ and $3$ coins To get $6$ use all packets. \n\nIn the second example, two packets with $1$ and $1$ coins can be made to get any amount $x$ ($1\\leq x\\leq 2$)."} +{"id": "02892", "text": "Many years have passed, and n friends met at a party again. Technologies have leaped forward since the last meeting, cameras with timer appeared and now it is not obligatory for one of the friends to stand with a camera, and, thus, being absent on the photo.\n\nSimply speaking, the process of photographing can be described as follows. Each friend occupies a rectangle of pixels on the photo: the i-th of them in a standing state occupies a w_{i} pixels wide and a h_{i} pixels high rectangle. But also, each person can lie down for the photo, and then he will occupy a h_{i} pixels wide and a w_{i} pixels high rectangle.\n\nThe total photo will have size W \u00d7 H, where W is the total width of all the people rectangles, and H is the maximum of the heights. The friends want to determine what minimum area the group photo can they obtain if no more than n / 2 of them can lie on the ground (it would be strange if more than n / 2 gentlemen lie on the ground together, isn't it?..)\n\nHelp them to achieve this goal.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 1000) \u2014 the number of friends.\n\nThe next n lines have two integers w_{i}, h_{i} (1 \u2264 w_{i}, h_{i} \u2264 1000) each, representing the size of the rectangle, corresponding to the i-th friend.\n\n\n-----Output-----\n\nPrint a single integer equal to the minimum possible area of the photo containing all friends if no more than n / 2 of them can lie on the ground.\n\n\n-----Examples-----\nInput\n3\n10 1\n20 2\n30 3\n\nOutput\n180\n\nInput\n3\n3 1\n2 2\n4 3\n\nOutput\n21\n\nInput\n1\n5 10\n\nOutput\n50"} +{"id": "02893", "text": "Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most x_{i} boxes on its top (we'll call x_{i} the strength of the box). \n\nSince all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile.[Image]\n\nFox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than x_{i} boxes on the top of i-th box. What is the minimal number of piles she needs to construct?\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 100). The next line contains n integers x_1, x_2, ..., x_{n} (0 \u2264 x_{i} \u2264 100).\n\n\n-----Output-----\n\nOutput a single integer \u2014 the minimal possible number of piles.\n\n\n-----Examples-----\nInput\n3\n0 0 10\n\nOutput\n2\n\nInput\n5\n0 1 2 3 4\n\nOutput\n1\n\nInput\n4\n0 0 0 0\n\nOutput\n4\n\nInput\n9\n0 1 0 2 0 1 1 2 10\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2.[Image]\n\nIn example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom).[Image]"} +{"id": "02894", "text": "You are given N points (x_i,y_i) located on a two-dimensional plane.\nConsider a subset S of the N points that forms a convex polygon.\nHere, we say a set of points S forms a convex polygon when there exists a convex polygon with a positive area that has the same set of vertices as S. All the interior angles of the polygon must be strictly less than 180\u00b0.\nFor example, in the figure above, {A,C,E} and {B,D,E} form convex polygons; {A,C,D,E}, {A,B,C,E}, {A,B,C}, {D,E} and {} do not.\nFor a given set S, let n be the number of the points among the N points that are inside the convex hull of S (including the boundary and vertices). Then, we will define the score of S as 2^{n-|S|}.\nCompute the scores of all possible sets S that form convex polygons, and find the sum of all those scores.\nHowever, since the sum can be extremely large, print the sum modulo 998244353.\n\n-----Constraints-----\n - 1\u2264N\u2264200\n - 0\u2264x_i,y_i<10^4 (1\u2264i\u2264N)\n - If i\u2260j, x_i\u2260x_j or y_i\u2260y_j.\n - x_i and y_i are integers.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\nx_1 y_1\nx_2 y_2\n:\nx_N y_N\n\n-----Output-----\nPrint the sum of all the scores modulo 998244353.\n\n-----Sample Input-----\n4\n0 0\n0 1\n1 0\n1 1\n\n-----Sample Output-----\n5\n\nWe have five possible sets as S, four sets that form triangles and one set that forms a square. Each of them has a score of 2^0=1, so the answer is 5."} +{"id": "02895", "text": "There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball.\nThe integer written on the i-th ball from the left (1 \u2264 i \u2264 2N) is a_i, and the color of this ball is represented by a letter c_i.\nc_i = W represents the ball is white; c_i = B represents the ball is black.\nTakahashi the human wants to achieve the following objective:\n - For every pair of integers (i,j) such that 1 \u2264 i < j \u2264 N, the white ball with i written on it is to the left of the white ball with j written on it.\n - For every pair of integers (i,j) such that 1 \u2264 i < j \u2264 N, the black ball with i written on it is to the left of the black ball with j written on it.\nIn order to achieve this, he can perform the following operation:\n - Swap two adjacent balls.\nFind the minimum number of operations required to achieve the objective.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 2000\n - 1 \u2264 a_i \u2264 N\n - c_i = W or c_i = B.\n - If i \u2260 j, (a_i,c_i) \u2260 (a_j,c_j).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nc_1 a_1\nc_2 a_2\n:\nc_{2N} a_{2N}\n\n-----Output-----\nPrint the minimum number of operations required to achieve the objective.\n\n-----Sample Input-----\n3\nB 1\nW 2\nB 3\nW 1\nW 3\nB 2\n\n-----Sample Output-----\n4\n\nThe objective can be achieved in four operations, for example, as follows:\n - Swap the black 3 and white 1.\n - Swap the white 1 and white 2.\n - Swap the black 3 and white 3.\n - Swap the black 3 and black 2."} +{"id": "02896", "text": "There are $b$ boys and $g$ girls participating in Olympiad of Metropolises. There will be a board games tournament in the evening and $n$ participants have accepted the invitation. The organizers do not know how many boys and girls are among them.\n\nOrganizers are preparing red badges for girls and blue ones for boys.\n\nVasya prepared $n+1$ decks of badges. The $i$-th (where $i$ is from $0$ to $n$, inclusive) deck contains $i$ blue badges and $n-i$ red ones. The total number of badges in any deck is exactly $n$.\n\nDetermine the minimum number of decks among these $n+1$ that Vasya should take, so that there will be a suitable deck no matter how many girls and boys there will be among the participants of the tournament.\n\n\n-----Input-----\n\nThe first line contains an integer $b$ ($1 \\le b \\le 300$), the number of boys. \n\nThe second line contains an integer $g$ ($1 \\le g \\le 300$), the number of girls. \n\nThe third line contains an integer $n$ ($1 \\le n \\le b + g$), the number of the board games tournament participants.\n\n\n-----Output-----\n\nOutput the only integer, the minimum number of badge decks that Vasya could take.\n\n\n-----Examples-----\nInput\n5\n6\n3\n\nOutput\n4\n\nInput\n5\n3\n5\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, each of 4 decks should be taken: (0 blue, 3 red), (1 blue, 2 red), (2 blue, 1 red), (3 blue, 0 red).\n\nIn the second example, 4 decks should be taken: (2 blue, 3 red), (3 blue, 2 red), (4 blue, 1 red), (5 blue, 0 red). Piles (0 blue, 5 red) and (1 blue, 4 red) can not be used."} +{"id": "02897", "text": "Given an array a_1, a_2, ..., a_{n} of n integers, find the largest number in the array that is not a perfect square.\n\nA number x is said to be a perfect square if there exists an integer y such that x = y^2.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the number of elements in the array.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} ( - 10^6 \u2264 a_{i} \u2264 10^6)\u00a0\u2014 the elements of the array.\n\nIt is guaranteed that at least one element of the array is not a perfect square.\n\n\n-----Output-----\n\nPrint the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists.\n\n\n-----Examples-----\nInput\n2\n4 2\n\nOutput\n2\n\nInput\n8\n1 2 4 8 16 32 64 576\n\nOutput\n32\n\n\n\n-----Note-----\n\nIn the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2."} +{"id": "02898", "text": "Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration $\\frac{a_{i}}{1000}$. Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration $\\frac{n}{1000}$. The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.\n\nCarbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.\n\nHelp them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration $\\frac{n}{1000}$. Assume that the friends have unlimited amount of each Coke type.\n\n\n-----Input-----\n\nThe first line contains two integers n, k (0 \u2264 n \u2264 1000, 1 \u2264 k \u2264 10^6)\u00a0\u2014 carbon dioxide concentration the friends want and the number of Coke types.\n\nThe second line contains k integers a_1, a_2, ..., a_{k} (0 \u2264 a_{i} \u2264 1000)\u00a0\u2014 carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.\n\n\n-----Output-----\n\nPrint the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration $\\frac{n}{1000}$, or -1 if it is impossible.\n\n\n-----Examples-----\nInput\n400 4\n100 300 450 500\n\nOutput\n2\n\nInput\n50 2\n100 25\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample case, we can achieve concentration $\\frac{400}{1000}$ using one liter of Coke of types $\\frac{300}{1000}$ and $\\frac{500}{1000}$: $\\frac{300 + 500}{1000 + 1000} = \\frac{400}{1000}$.\n\nIn the second case, we can achieve concentration $\\frac{50}{1000}$ using two liters of $\\frac{25}{1000}$ type and one liter of $\\frac{100}{1000}$ type: $\\frac{25 + 25 + 100}{3 \\cdot 1000} = \\frac{50}{1000}$."} +{"id": "02899", "text": "Takahashi and Aoki are going to together construct a sequence of integers.\nFirst, Takahashi will provide a sequence of integers a, satisfying all of the following conditions:\n - The length of a is N.\n - Each element in a is an integer between 1 and K, inclusive.\n - a is a palindrome, that is, reversing the order of elements in a will result in the same sequence as the original.\nThen, Aoki will perform the following operation an arbitrary number of times:\n - Move the first element in a to the end of a.\nHow many sequences a can be obtained after this procedure, modulo 10^9+7?\n\n-----Constraints-----\n - 1\u2264N\u226410^9\n - 1\u2264K\u226410^9\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN K\n\n-----Output-----\nPrint the number of the sequences a that can be obtained after the procedure, modulo 10^9+7.\n\n-----Sample Input-----\n4 2\n\n-----Sample Output-----\n6\n\nThe following six sequences can be obtained:\n - (1, 1, 1, 1)\n - (1, 1, 2, 2)\n - (1, 2, 2, 1)\n - (2, 2, 1, 1)\n - (2, 1, 1, 2)\n - (2, 2, 2, 2)"} +{"id": "02900", "text": "Kuro and Shiro are playing with a board composed of n squares lining up in a row.\nThe squares are numbered 1 to n from left to right, and Square s has a mark on it.\nFirst, for each square, Kuro paints it black or white with equal probability, independently from other squares. Then, he puts on Square s a stone of the same color as the square.\nKuro and Shiro will play a game using this board and infinitely many black stones and white stones. In this game, Kuro and Shiro alternately put a stone as follows, with Kuro going first:\n - Choose an empty square adjacent to a square with a stone on it. Let us say Square i is chosen.\n - Put on Square i a stone of the same color as the square.\n - If there are squares other than Square i that contain a stone of the same color as the stone just placed, among such squares, let Square j be the one nearest to Square i. Change the color of every stone between Square i and Square j to the color of Square i.\nThe game ends when the board has no empty square.\nKuro plays optimally to maximize the number of black stones at the end of the game, while Shiro plays optimally to maximize the number of white stones at the end of the game.\nFor each of the cases s=1,\\dots,n, find the expected value, modulo 998244353, of the number of black stones at the end of the game.\n\n-----Notes-----\nWhen the expected value in question is represented as an irreducible fraction p/q, there uniquely exists an integer r such that rq=p ~(\\text{mod } 998244353) and 0 \\leq r \\lt 998244353, which we ask you to find.\n\n-----Constraints-----\n - 1 \\leq n \\leq 2\\times 10^5\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn\n\n-----Output-----\nPrint n values.\nThe i-th value should be the expected value, modulo 998244353, of the number of black stones at the end of the game for the case s=i.\n\n-----Sample Input-----\n3\n\n-----Sample Output-----\n499122178\n499122178\n499122178\n\nLet us use b to represent a black square and w to represent a white square.\nThere are eight possible boards: www, wwb, wbw, wbb, bww, bwb, bbw, and bbb, which are chosen with equal probability.\nFor each of these boards, there will be 0, 1, 0, 2, 1, 3, 2, and 3 black stones at the end of the game, respectively, regardless of the value of s.\nThus, the expected number of stones is (0+1+0+2+1+3+2+3)/8 = 3/2, and the answer is r = 499122178, which satisfies 2r = 3 ~(\\text{mod } 998244353) and 0 \\leq r \\lt 998244353."} +{"id": "02901", "text": "Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.\n\nA permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if $\\forall i(1 \\leq i \\leq n), a_{i} + b_{i} \\equiv c_{i} \\operatorname{mod} n$. The sign a_{i} denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing a_{i} + b_{i} by n and dividing c_{i} by n are equal.\n\nNow, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5).\n\n\n-----Output-----\n\nIf no Lucky Permutation Triple of length n exists print -1.\n\nOtherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line \u2014 permutation b, the third \u2014 permutation c.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n5\n\nOutput\n1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3\n\nInput\n2\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: $1 + 1 \\equiv 2 \\equiv 2 \\operatorname{mod} 5$; $4 + 0 \\equiv 4 \\equiv 4 \\operatorname{mod} 5$; $3 + 2 \\equiv 0 \\equiv 0 \\operatorname{mod} 5$; $2 + 4 \\equiv 6 \\equiv 1 \\operatorname{mod} 5$; $0 + 3 \\equiv 3 \\equiv 3 \\operatorname{mod} 5$. \n\nIn Sample 2, you can easily notice that no lucky permutation triple exists."} +{"id": "02902", "text": "The BFS algorithm is defined as follows. Consider an undirected graph with vertices numbered from $1$ to $n$. Initialize $q$ as a new queue containing only vertex $1$, mark the vertex $1$ as used. Extract a vertex $v$ from the head of the queue $q$. Print the index of vertex $v$. Iterate in arbitrary order through all such vertices $u$ that $u$ is a neighbor of $v$ and is not marked yet as used. Mark the vertex $u$ as used and insert it into the tail of the queue $q$. If the queue is not empty, continue from step 2. Otherwise finish. \n\nSince the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print.\n\nIn this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex $1$. The tree is an undirected graph, such that there is exactly one simple path between any two vertices.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) which denotes the number of nodes in the tree. \n\nThe following $n - 1$ lines describe the edges of the tree. Each of them contains two integers $x$ and $y$ ($1 \\le x, y \\le n$)\u00a0\u2014 the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree.\n\nThe last line contains $n$ distinct integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le n$)\u00a0\u2014 the sequence to check.\n\n\n-----Output-----\n\nPrint \"Yes\" (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and \"No\" (quotes for clarity) otherwise.\n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n4\n1 2\n1 3\n2 4\n1 2 3 4\n\nOutput\nYes\nInput\n4\n1 2\n1 3\n2 4\n1 2 4 3\n\nOutput\nNo\n\n\n-----Note-----\n\nBoth sample tests have the same tree in them.\n\nIn this tree, there are two valid BFS orderings: $1, 2, 3, 4$, $1, 3, 2, 4$. \n\nThe ordering $1, 2, 4, 3$ doesn't correspond to any valid BFS order."} +{"id": "02903", "text": "Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.\n\nThere are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.\n\nYou know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.\n\nGather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.\n\n\n-----Input-----\n\nThe first line of input contains three integers n, m and k (1 \u2264 n \u2264 10^5, 0 \u2264 m \u2264 10^5, 1 \u2264 k \u2264 10^6). \n\nThe i-th of the following m lines contains the description of the i-th flight defined by four integers d_{i}, f_{i}, t_{i} and c_{i} (1 \u2264 d_{i} \u2264 10^6, 0 \u2264 f_{i} \u2264 n, 0 \u2264 t_{i} \u2264 n, 1 \u2264 c_{i} \u2264 10^6, exactly one of f_{i} and t_{i} equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.\n\n\n-----Output-----\n\nOutput the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities.\n\nIf it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output \"-1\" (without the quotes).\n\n\n-----Examples-----\nInput\n2 6 5\n1 1 0 5000\n3 2 0 5500\n2 2 0 6000\n15 0 2 9000\n9 0 1 7000\n8 0 2 6500\n\nOutput\n24500\n\nInput\n2 4 5\n1 2 0 5000\n2 1 0 4500\n2 1 0 3000\n8 0 1 6000\n\nOutput\n-1\n\n\n\n-----Note-----\n\nThe optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.\n\nIn the second sample it is impossible to send jury member from city 2 back home from Metropolis."} +{"id": "02904", "text": "You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle \u03b1. [Image] \n\nYour task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.\n\n\n-----Input-----\n\nThe first line contains three integers w, h, \u03b1 (1 \u2264 w, h \u2264 10^6;\u00a00 \u2264 \u03b1 \u2264 180). Angle \u03b1 is given in degrees.\n\n\n-----Output-----\n\nIn a single line print a real number \u2014 the area of the region which belongs to both given rectangles.\n\nThe answer will be considered correct if its relative or absolute error doesn't exceed 10^{ - 6}.\n\n\n-----Examples-----\nInput\n1 1 45\n\nOutput\n0.828427125\n\nInput\n6 4 30\n\nOutput\n19.668384925\n\n\n\n-----Note-----\n\nThe second sample has been drawn on the picture above."} +{"id": "02905", "text": "Fox Ciel is playing a card game with her friend Jiro.\n\nJiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.\n\nNow is Ciel's battle phase, Ciel can do the following operation many times: Choose one of her cards X. This card mustn't be chosen before. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then: If Y's position is Attack, then (X's strength) \u2265 (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength). If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage. \n\nCiel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 100) \u2014 the number of cards Jiro and Ciel have.\n\nEach of the next n lines contains a string position and an integer strength (0 \u2264 strength \u2264 8000) \u2014 the position and strength of Jiro's current card. Position is the string \"ATK\" for attack, and the string \"DEF\" for defense.\n\nEach of the next m lines contains an integer strength (0 \u2264 strength \u2264 8000) \u2014 the strength of Ciel's current card.\n\n\n-----Output-----\n\nOutput an integer: the maximal damage Jiro can get.\n\n\n-----Examples-----\nInput\n2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500\n\nOutput\n3000\n\nInput\n3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001\n\nOutput\n992\n\nInput\n2 4\nDEF 0\nATK 0\n0\n0\n1\n1\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack \"ATK 2000\" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the \"DEF 1700\" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.\n\nIn the second test case, she should use the \"1001\" card to attack the \"ATK 100\" card, then use the \"101\" card to attack the \"ATK 10\" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.\n\nIn the third test case note that she can destroy the \"ATK 0\" card by a card with strength equal to 0, but she can't destroy a \"DEF 0\" card with that card."} +{"id": "02906", "text": "A popular reality show is recruiting a new cast for the third season! $n$ candidates numbered from $1$ to $n$ have been interviewed. The candidate $i$ has aggressiveness level $l_i$, and recruiting this candidate will cost the show $s_i$ roubles.\n\nThe show host reviewes applications of all candidates from $i=1$ to $i=n$ by increasing of their indices, and for each of them she decides whether to recruit this candidate or not. If aggressiveness level of the candidate $i$ is strictly higher than that of any already accepted candidates, then the candidate $i$ will definitely be rejected. Otherwise the host may accept or reject this candidate at her own discretion. The host wants to choose the cast so that to maximize the total profit.\n\nThe show makes revenue as follows. For each aggressiveness level $v$ a corresponding profitability value $c_v$ is specified, which can be positive as well as negative. All recruited participants enter the stage one by one by increasing of their indices. When the participant $i$ enters the stage, events proceed as follows:\n\n The show makes $c_{l_i}$ roubles, where $l_i$ is initial aggressiveness level of the participant $i$. If there are two participants with the same aggressiveness level on stage, they immediately start a fight. The outcome of this is:\n\n the defeated participant is hospitalized and leaves the show. aggressiveness level of the victorious participant is increased by one, and the show makes $c_t$ roubles, where $t$ is the new aggressiveness level. \n\n The fights continue until all participants on stage have distinct aggressiveness levels. \n\nIt is allowed to select an empty set of participants (to choose neither of the candidates).\n\nThe host wants to recruit the cast so that the total profit is maximized. The profit is calculated as the total revenue from the events on stage, less the total expenses to recruit all accepted participants (that is, their total $s_i$). Help the host to make the show as profitable as possible.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 2000$) \u2014 the number of candidates and an upper bound for initial aggressiveness levels.\n\nThe second line contains $n$ integers $l_i$ ($1 \\le l_i \\le m$) \u2014 initial aggressiveness levels of all candidates.\n\nThe third line contains $n$ integers $s_i$ ($0 \\le s_i \\le 5000$) \u2014 the costs (in roubles) to recruit each of the candidates.\n\nThe fourth line contains $n + m$ integers $c_i$ ($|c_i| \\le 5000$) \u2014 profitability for each aggrressiveness level.\n\nIt is guaranteed that aggressiveness level of any participant can never exceed $n + m$ under given conditions.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the largest profit of the show.\n\n\n-----Examples-----\nInput\n5 4\n4 3 1 2 1\n1 2 1 2 1\n1 2 3 4 5 6 7 8 9\n\nOutput\n6\n\nInput\n2 2\n1 2\n0 0\n2 1 -100 -100\n\nOutput\n2\n\nInput\n5 4\n4 3 2 1 1\n0 2 6 7 4\n12 12 12 6 -3 -5 3 10 -4\n\nOutput\n62\n\n\n\n-----Note-----\n\nIn the first sample case it is optimal to recruit candidates $1, 2, 3, 5$. Then the show will pay $1 + 2 + 1 + 1 = 5$ roubles for recruitment. The events on stage will proceed as follows:\n\n a participant with aggressiveness level $4$ enters the stage, the show makes $4$ roubles; a participant with aggressiveness level $3$ enters the stage, the show makes $3$ roubles; a participant with aggressiveness level $1$ enters the stage, the show makes $1$ rouble; a participant with aggressiveness level $1$ enters the stage, the show makes $1$ roubles, a fight starts. One of the participants leaves, the other one increases his aggressiveness level to $2$. The show will make extra $2$ roubles for this. \n\nTotal revenue of the show will be $4 + 3 + 1 + 1 + 2=11$ roubles, and the profit is $11 - 5 = 6$ roubles.\n\nIn the second sample case it is impossible to recruit both candidates since the second one has higher aggressiveness, thus it is better to recruit the candidate $1$."} +{"id": "02907", "text": "Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: They are equal. If we split string a into two halves of the same size a_1 and a_2, and string b into two halves of the same size b_1 and b_2, then one of the following is correct: a_1 is equivalent to b_1, and a_2 is equivalent to b_2 a_1 is equivalent to b_2, and a_2 is equivalent to b_1 \n\nAs a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.\n\nGerald has already completed this home task. Now it's your turn!\n\n\n-----Input-----\n\nThe first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length.\n\n\n-----Output-----\n\nPrint \"YES\" (without the quotes), if these two strings are equivalent, and \"NO\" (without the quotes) otherwise.\n\n\n-----Examples-----\nInput\naaba\nabaa\n\nOutput\nYES\n\nInput\naabb\nabab\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample you should split the first string into strings \"aa\" and \"ba\", the second one \u2014 into strings \"ab\" and \"aa\". \"aa\" is equivalent to \"aa\"; \"ab\" is equivalent to \"ba\" as \"ab\" = \"a\" + \"b\", \"ba\" = \"b\" + \"a\".\n\nIn the second sample the first string can be splitted into strings \"aa\" and \"bb\", that are equivalent only to themselves. That's why string \"aabb\" is equivalent only to itself and to string \"bbaa\"."} +{"id": "02908", "text": "Seyyed and MoJaK are friends of Sajjad. Sajjad likes a permutation. Seyyed wants to change the permutation in a way that Sajjad won't like it. Seyyed thinks more swaps yield more probability to do that, so he makes MoJaK to perform a swap between every pair of positions (i, j), where i < j, exactly once. MoJaK doesn't like to upset Sajjad.\n\nGiven the permutation, determine whether it is possible to swap all pairs of positions so that the permutation stays the same. If it is possible find how to do that. \n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 1000)\u00a0\u2014 the size of the permutation.\n\nAs the permutation is not important, you can consider a_{i} = i, where the permutation is a_1, a_2, ..., a_{n}.\n\n\n-----Output-----\n\nIf it is not possible to swap all pairs of positions so that the permutation stays the same, print \"NO\",\n\nOtherwise print \"YES\", then print $\\frac{n(n - 1)}{2}$ lines: the i-th of these lines should contain two integers a and b (a < b)\u00a0\u2014 the positions where the i-th swap is performed.\n\n\n-----Examples-----\nInput\n3\n\nOutput\nNO\n\nInput\n1\n\nOutput\nYES"} +{"id": "02909", "text": "Polycarp is a beginner programmer. He is studying how to use a command line.\n\nPolycarp faced the following problem. There are n files in a directory and he needs to delete some of them. Polycarp wants to run a single delete command with filename pattern as an argument. All the files to be deleted should match the pattern and all other files shouldn't match the pattern.\n\nPolycarp doesn't know about an asterisk '*', the only special character he knows is a question mark '?' which matches any single character. All other characters in the pattern match themselves only.\n\nFormally, a pattern matches a filename if and only if they have equal lengths and all characters in the corresponding positions are equal except when the character in the pattern is '?', in which case the corresponding filename character does not matter.\n\nFor example, the filename pattern \"a?ba?\": matches filenames \"aabaa\", \"abba.\", \"a.ba9\" and \"a.ba.\"; does not match filenames \"aaba\", \"abaab\", \"aabaaa\" and \"aabaa.\". \n\nHelp Polycarp find a pattern which matches files to be deleted and only them or report if there is no such pattern.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (1 \u2264 m \u2264 n \u2264 100) \u2014 the total number of files and the number of files to be deleted.\n\nThe following n lines contain filenames, single filename per line. All filenames are non-empty strings containing only lowercase English letters, digits and dots ('.'). The length of each filename doesn't exceed 100. It is guaranteed that all filenames are distinct.\n\nThe last line of the input contains m distinct integer numbers in ascending order a_1, a_2, ..., a_{m} (1 \u2264 a_{i} \u2264 n) \u2014 indices of files to be deleted. All files are indexed from 1 to n in order of their appearance in the input.\n\n\n-----Output-----\n\nIf the required pattern exists, print \"Yes\" in the first line of the output. The second line should contain the required pattern. If there are multiple solutions, print any of them.\n\nIf the required pattern doesn't exist, print the only line containing \"No\".\n\n\n-----Examples-----\nInput\n3 2\nab\nac\ncd\n1 2\n\nOutput\nYes\na?\n\nInput\n5 3\ntest\ntezt\ntest.\n.est\ntes.\n1 4 5\n\nOutput\nYes\n?es?\n\nInput\n4 4\na\nb\nc\ndd\n1 2 3 4\n\nOutput\nNo\n\nInput\n6 3\n.svn\n.git\n....\n...\n..\n.\n1 2 3\n\nOutput\nYes\n.???"} +{"id": "02910", "text": "Given is an integer sequence of length N: A_1, A_2, \\cdots, A_N.\nAn integer sequence X, which is also of length N, will be chosen randomly by independently choosing X_i from a uniform distribution on the integers 1, 2, \\ldots, A_i for each i (1 \\leq i \\leq N).\nCompute the expected value of the length of the longest increasing subsequence of this sequence X, modulo 1000000007.\nMore formally, under the constraints of the problem, we can prove that the expected value can be represented as a rational number, that is, an irreducible fraction \\frac{P}{Q}, and there uniquely exists an integer R such that R \\times Q \\equiv P \\pmod {1000000007} and 0 \\leq R < 1000000007, so print this integer R.\n\n-----Notes-----\nA subsequence of a sequence X is a sequence obtained by extracting some of the elements of X and arrange them without changing the order. The longest increasing subsequence of a sequence X is the sequence of the greatest length among the strictly increasing subsequences of X.\n\n-----Constraints-----\n - 1 \\leq N \\leq 6\n - 1 \\leq A_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 \\cdots A_N\n\n-----Output-----\nPrint the expected value modulo 1000000007.\n\n-----Sample Input-----\n3\n1 2 3\n\n-----Sample Output-----\n2\n\nX becomes one of the following, with probability 1/6 for each:\n - X = (1,1,1), for which the length of the longest increasing subsequence is 1;\n - X = (1,1,2), for which the length of the longest increasing subsequence is 2;\n - X = (1,1,3), for which the length of the longest increasing subsequence is 2;\n - X = (1,2,1), for which the length of the longest increasing subsequence is 2;\n - X = (1,2,2), for which the length of the longest increasing subsequence is 2;\n - X = (1,2,3), for which the length of the longest increasing subsequence is 3.\nThus, the expected value of the length of the longest increasing subsequence is (1 + 2 + 2 + 2 + 2 + 3) / 6 \\equiv 2 \\pmod {1000000007}."} +{"id": "02911", "text": "Let N be an even number.\nThere is a tree with N vertices.\nThe vertices are numbered 1, 2, ..., N.\nFor each i (1 \\leq i \\leq N - 1), the i-th edge connects Vertex x_i and y_i.\nSnuke would like to decorate the tree with ribbons, as follows.\nFirst, he will divide the N vertices into N / 2 pairs.\nHere, each vertex must belong to exactly one pair.\nThen, for each pair (u, v), put a ribbon through all the edges contained in the shortest path between u and v.\nSnuke is trying to divide the vertices into pairs so that the following condition is satisfied: \"for every edge, there is at least one ribbon going through it.\"\nHow many ways are there to divide the vertices into pairs, satisfying this condition?\nFind the count modulo 10^9 + 7.\nHere, two ways to divide the vertices into pairs are considered different when there is a pair that is contained in one of the two ways but not in the other.\n\n-----Constraints-----\n - N is an even number.\n - 2 \\leq N \\leq 5000\n - 1 \\leq x_i, y_i \\leq N\n - The given graph is a tree.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nx_1 y_1\nx_2 y_2\n:\nx_{N - 1} y_{N - 1}\n\n-----Output-----\nPrint the number of the ways to divide the vertices into pairs, satisfying the condition, modulo 10^9 + 7.\n\n-----Sample Input-----\n4\n1 2\n2 3\n3 4\n\n-----Sample Output-----\n2\n\nThere are three possible ways to divide the vertices into pairs, as shown below, and two satisfy the condition: the middle one and the right one."} +{"id": "02912", "text": "Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon's place as maester of Castle Black. Jon agrees to Sam's proposal and Sam sets off his journey to the Citadel. However becoming a trainee at the Citadel is not a cakewalk and hence the maesters at the Citadel gave Sam a problem to test his eligibility. \n\nInitially Sam has a list with a single element n. Then he has to perform certain operations on this list. In each operation Sam must remove any element x, such that x > 1, from the list and insert at the same position $\\lfloor \\frac{x}{2} \\rfloor$, $x \\operatorname{mod} 2$, $\\lfloor \\frac{x}{2} \\rfloor$ sequentially. He must continue with these operations until all the elements in the list are either 0 or 1.\n\nNow the masters want the total number of 1s in the range l to r (1-indexed). Sam wants to become a maester but unfortunately he cannot solve this problem. Can you help Sam to pass the eligibility test?\n\n\n-----Input-----\n\nThe first line contains three integers n, l, r (0 \u2264 n < 2^50, 0 \u2264 r - l \u2264 10^5, r \u2265 1, l \u2265 1) \u2013 initial element and the range l to r.\n\nIt is guaranteed that r is not greater than the length of the final list.\n\n\n-----Output-----\n\nOutput the total number of 1s in the range l to r in the final sequence.\n\n\n-----Examples-----\nInput\n7 2 5\n\nOutput\n4\n\nInput\n10 3 10\n\nOutput\n5\n\n\n\n-----Note-----\n\nConsider first example:\n\n$[ 7 ] \\rightarrow [ 3,1,3 ] \\rightarrow [ 1,1,1,1,3 ] \\rightarrow [ 1,1,1,1,1,1,1 ] \\rightarrow [ 1,1,1,1,1,1,1 ]$\n\nElements on positions from 2-nd to 5-th in list is [1, 1, 1, 1]. The number of ones is 4.\n\nFor the second example:\n\n$[ 10 ] \\rightarrow [ 1,0,1,1,1,0,1,0,1,0,1,1,1,0,1 ]$\n\nElements on positions from 3-rd to 10-th in list is [1, 1, 1, 0, 1, 0, 1, 0]. The number of ones is 5."} +{"id": "02913", "text": "Arseny likes to organize parties and invite people to it. However, not only friends come to his parties, but friends of his friends, friends of friends of his friends and so on. That's why some of Arseny's guests can be unknown to him. He decided to fix this issue using the following procedure.\n\nAt each step he selects one of his guests A, who pairwise introduces all of his friends to each other. After this action any two friends of A become friends. This process is run until all pairs of guests are friends.\n\nArseny doesn't want to spend much time doing it, so he wants to finish this process using the minimum number of steps. Help Arseny to do it.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 22; $0 \\leq m \\leq \\frac{n \\cdot(n - 1)}{2}$)\u00a0\u2014 the number of guests at the party (including Arseny) and the number of pairs of people which are friends.\n\nEach of the next m lines contains two integers u and v (1 \u2264 u, v \u2264 n; u \u2260 v), which means that people with numbers u and v are friends initially. It's guaranteed that each pair of friends is described not more than once and the graph of friendship is connected.\n\n\n-----Output-----\n\nIn the first line print the minimum number of steps required to make all pairs of guests friends.\n\nIn the second line print the ids of guests, who are selected at each step.\n\nIf there are multiple solutions, you can output any of them.\n\n\n-----Examples-----\nInput\n5 6\n1 2\n1 3\n2 3\n2 5\n3 4\n4 5\n\nOutput\n2\n2 3 \nInput\n4 4\n1 2\n1 3\n1 4\n3 4\n\nOutput\n1\n1 \n\n\n-----Note-----\n\nIn the first test case there is no guest who is friend of all other guests, so at least two steps are required to perform the task. After second guest pairwise introduces all his friends, only pairs of guests (4, 1) and (4, 2) are not friends. Guest 3 or 5 can introduce them.\n\nIn the second test case guest number 1 is a friend of all guests, so he can pairwise introduce all guests in one step."} +{"id": "02914", "text": "Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size a_{i} dollars.\n\nEach player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?\n\n\n-----Input-----\n\nFirst line of input contains an integer n (2 \u2264 n \u2264 10^5), the number of players.\n\nThe second line contains n integer numbers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9) \u2014 the bids of players.\n\n\n-----Output-----\n\nPrint \"Yes\" (without the quotes) if players can make their bids become equal, or \"No\" otherwise.\n\n\n-----Examples-----\nInput\n4\n75 150 75 50\n\nOutput\nYes\n\nInput\n3\n100 150 250\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.\n\nIt can be shown that in the second sample test there is no way to make all bids equal."} +{"id": "02915", "text": "Yaroslav has an array, consisting of (2\u00b7n - 1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yaroslav can select exactly n array elements, and multiply each of them by -1.\n\nYaroslav is now wondering: what maximum sum of array elements can be obtained if it is allowed to perform any number of described operations?\n\nHelp Yaroslav.\n\n\n-----Input-----\n\nThe first line contains an integer n (2 \u2264 n \u2264 100). The second line contains (2\u00b7n - 1) integers \u2014 the array elements. The array elements do not exceed 1000 in their absolute value.\n\n\n-----Output-----\n\nIn a single line print the answer to the problem \u2014 the maximum sum that Yaroslav can get.\n\n\n-----Examples-----\nInput\n2\n50 50 50\n\nOutput\n150\n\nInput\n2\n-1 -100 -1\n\nOutput\n100\n\n\n\n-----Note-----\n\nIn the first sample you do not need to change anything. The sum of elements equals 150.\n\nIn the second sample you need to change the sign of the first two elements. Then we get the sum of the elements equal to 100."} +{"id": "02916", "text": "Limak is a little polar bear. Polar bears hate long strings and thus they like to compress them. You should also know that Limak is so young that he knows only first six letters of the English alphabet: 'a', 'b', 'c', 'd', 'e' and 'f'.\n\nYou are given a set of q possible operations. Limak can perform them in any order, any operation may be applied any number of times. The i-th operation is described by a string a_{i} of length two and a string b_{i} of length one. No two of q possible operations have the same string a_{i}.\n\nWhen Limak has a string s he can perform the i-th operation on s if the first two letters of s match a two-letter string a_{i}. Performing the i-th operation removes first two letters of s and inserts there a string b_{i}. See the notes section for further clarification.\n\nYou may note that performing an operation decreases the length of a string s exactly by 1. Also, for some sets of operations there may be a string that cannot be compressed any further, because the first two letters don't match any a_{i}.\n\nLimak wants to start with a string of length n and perform n - 1 operations to finally get a one-letter string \"a\". In how many ways can he choose the starting string to be able to get \"a\"? Remember that Limak can use only letters he knows.\n\n\n-----Input-----\n\nThe first line contains two integers n and q (2 \u2264 n \u2264 6, 1 \u2264 q \u2264 36)\u00a0\u2014 the length of the initial string and the number of available operations.\n\nThe next q lines describe the possible operations. The i-th of them contains two strings a_{i} and b_{i} (|a_{i}| = 2, |b_{i}| = 1). It's guaranteed that a_{i} \u2260 a_{j} for i \u2260 j and that all a_{i} and b_{i} consist of only first six lowercase English letters.\n\n\n-----Output-----\n\nPrint the number of strings of length n that Limak will be able to transform to string \"a\" by applying only operations given in the input.\n\n\n-----Examples-----\nInput\n3 5\nab a\ncc c\nca a\nee c\nff d\n\nOutput\n4\n\nInput\n2 8\naf e\ndc d\ncc f\nbc b\nda b\neb a\nbb b\nff c\n\nOutput\n1\n\nInput\n6 2\nbb a\nba a\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first sample, we count initial strings of length 3 from which Limak can get a required string \"a\". There are 4 such strings: \"abb\", \"cab\", \"cca\", \"eea\". The first one Limak can compress using operation 1 two times (changing \"ab\" to a single \"a\"). The first operation would change \"abb\" to \"ab\" and the second operation would change \"ab\" to \"a\".\n\nOther three strings may be compressed as follows: \"cab\" $\\rightarrow$ \"ab\" $\\rightarrow$ \"a\" \"cca\" $\\rightarrow$ \"ca\" $\\rightarrow$ \"a\" \"eea\" $\\rightarrow$ \"ca\" $\\rightarrow$ \"a\" \n\nIn the second sample, the only correct initial string is \"eb\" because it can be immediately compressed to \"a\"."} +{"id": "02917", "text": "Little Johnny has recently learned about set theory. Now he is studying binary relations. You've probably heard the term \"equivalence relation\". These relations are very important in many areas of mathematics. For example, the equality of the two numbers is an equivalence relation.\n\nA set \u03c1 of pairs (a, b) of elements of some set A is called a binary relation on set A. For two elements a and b of the set A we say that they are in relation \u03c1, if pair $(a, b) \\in \\rho$, in this case we use a notation $a \\stackrel{\\rho}{\\sim} b$.\n\nBinary relation is equivalence relation, if: It is reflexive (for any a it is true that $a \\stackrel{\\rho}{\\sim} a$); It is symmetric (for any a, b it is true that if $a \\stackrel{\\rho}{\\sim} b$, then $b \\stackrel{\\rho}{\\sim} a$); It is transitive (if $a \\stackrel{\\rho}{\\sim} b$ and $b \\stackrel{\\rho}{\\sim} c$, than $a \\stackrel{\\rho}{\\sim} c$).\n\nLittle Johnny is not completely a fool and he noticed that the first condition is not necessary! Here is his \"proof\":\n\nTake any two elements, a and b. If $a \\stackrel{\\rho}{\\sim} b$, then $b \\stackrel{\\rho}{\\sim} a$ (according to property (2)), which means $a \\stackrel{\\rho}{\\sim} a$ (according to property (3)).\n\nIt's very simple, isn't it? However, you noticed that Johnny's \"proof\" is wrong, and decided to show him a lot of examples that prove him wrong.\n\nHere's your task: count the number of binary relations over a set of size n such that they are symmetric, transitive, but not an equivalence relations (i.e. they are not reflexive).\n\nSince their number may be very large (not 0, according to Little Johnny), print the remainder of integer division of this number by 10^9 + 7.\n\n\n-----Input-----\n\nA single line contains a single integer n (1 \u2264 n \u2264 4000).\n\n\n-----Output-----\n\nIn a single line print the answer to the problem modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n2\n\nOutput\n3\n\nInput\n3\n\nOutput\n10\n\n\n\n-----Note-----\n\nIf n = 1 there is only one such relation\u00a0\u2014 an empty one, i.e. $\\rho = \\varnothing$. In other words, for a single element x of set A the following is hold: [Image].\n\nIf n = 2 there are three such relations. Let's assume that set A consists of two elements, x and y. Then the valid relations are $\\rho = \\varnothing$, \u03c1 = {(x, x)}, \u03c1 = {(y, y)}. It is easy to see that the three listed binary relations are symmetric and transitive relations, but they are not equivalence relations."} +{"id": "02918", "text": "There is a polyline going through points (0, 0) \u2013 (x, x) \u2013 (2x, 0) \u2013 (3x, x) \u2013 (4x, 0) \u2013 ... - (2kx, 0) \u2013 (2kx + x, x) \u2013 .... \n\nWe know that the polyline passes through the point (a, b). Find minimum positive value x such that it is true or determine that there is no such x.\n\n\n-----Input-----\n\nOnly one line containing two positive integers a and b (1 \u2264 a, b \u2264 10^9).\n\n\n-----Output-----\n\nOutput the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10^{ - 9}. If there is no such x then output - 1 as the answer.\n\n\n-----Examples-----\nInput\n3 1\n\nOutput\n1.000000000000\n\nInput\n1 3\n\nOutput\n-1\n\nInput\n4 1\n\nOutput\n1.250000000000\n\n\n\n-----Note-----\n\nYou can see following graphs for sample 1 and sample 3. [Image] [Image]"} +{"id": "02919", "text": "Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on $n - 2$ intermediate planets. Formally: we number all the planets from $1$ to $n$. $1$ is Earth, $n$ is Mars. Natasha will make exactly $n$ flights: $1 \\to 2 \\to \\ldots n \\to 1$.\n\nFlight from $x$ to $y$ consists of two phases: take-off from planet $x$ and landing to planet $y$. This way, the overall itinerary of the trip will be: the $1$-st planet $\\to$ take-off from the $1$-st planet $\\to$ landing to the $2$-nd planet $\\to$ $2$-nd planet $\\to$ take-off from the $2$-nd planet $\\to$ $\\ldots$ $\\to$ landing to the $n$-th planet $\\to$ the $n$-th planet $\\to$ take-off from the $n$-th planet $\\to$ landing to the $1$-st planet $\\to$ the $1$-st planet.\n\nThe mass of the rocket together with all the useful cargo (but without fuel) is $m$ tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that $1$ ton of fuel can lift off $a_i$ tons of rocket from the $i$-th planet or to land $b_i$ tons of rocket onto the $i$-th planet. \n\nFor example, if the weight of rocket is $9$ tons, weight of fuel is $3$ tons and take-off coefficient is $8$ ($a_i = 8$), then $1.5$ tons of fuel will be burnt (since $1.5 \\cdot 8 = 9 + 3$). The new weight of fuel after take-off will be $1.5$ tons. \n\nPlease note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well.\n\nHelp Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn't need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 1000$)\u00a0\u2014 number of planets.\n\nThe second line contains the only integer $m$ ($1 \\le m \\le 1000$)\u00a0\u2014 weight of the payload.\n\nThe third line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 1000$), where $a_i$ is the number of tons, which can be lifted off by one ton of fuel.\n\nThe fourth line contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($1 \\le b_i \\le 1000$), where $b_i$ is the number of tons, which can be landed by one ton of fuel. \n\nIt is guaranteed, that if Natasha can make a flight, then it takes no more than $10^9$ tons of fuel.\n\n\n-----Output-----\n\nIf Natasha can fly to Mars through $(n - 2)$ planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number $-1$.\n\nIt is guaranteed, that if Natasha can make a flight, then it takes no more than $10^9$ tons of fuel.\n\nThe answer will be considered correct if its absolute or relative error doesn't exceed $10^{-6}$. Formally, let your answer be $p$, and the jury's answer be $q$. Your answer is considered correct if $\\frac{|p - q|}{\\max{(1, |q|)}} \\le 10^{-6}$.\n\n\n-----Examples-----\nInput\n2\n12\n11 8\n7 5\n\nOutput\n10.0000000000\n\nInput\n3\n1\n1 4 1\n2 5 3\n\nOutput\n-1\n\nInput\n6\n2\n4 6 3 3 5 6\n2 6 3 6 5 3\n\nOutput\n85.4800000000\n\n\n\n-----Note-----\n\nLet's consider the first example.\n\nInitially, the mass of a rocket with fuel is $22$ tons. At take-off from Earth one ton of fuel can lift off $11$ tons of cargo, so to lift off $22$ tons you need to burn $2$ tons of fuel. Remaining weight of the rocket with fuel is $20$ tons. During landing on Mars, one ton of fuel can land $5$ tons of cargo, so for landing $20$ tons you will need to burn $4$ tons of fuel. There will be $16$ tons of the rocket with fuel remaining. While taking off from Mars, one ton of fuel can raise $8$ tons of cargo, so to lift off $16$ tons you will need to burn $2$ tons of fuel. There will be $14$ tons of rocket with fuel after that. During landing on Earth, one ton of fuel can land $7$ tons of cargo, so for landing $14$ tons you will need to burn $2$ tons of fuel. Remaining weight is $12$ tons, that is, a rocket without any fuel.\n\nIn the second case, the rocket will not be able even to take off from Earth."} +{"id": "02920", "text": "A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.\n\nThere are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs $(i + j) \\operatorname{mod}(n + 1)$ and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of schools.\n\n\n-----Output-----\n\nPrint single integer: the minimum cost of tickets needed to visit all schools.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n0\n\nInput\n10\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example we can buy a ticket between the schools that costs $(1 + 2) \\operatorname{mod}(2 + 1) = 0$."} +{"id": "02921", "text": "What are you doing at the end of the world? Are you busy? Will you save us?\n\n\n\n[Image]\n\nNephren is playing a game with little leprechauns.\n\nShe gives them an infinite array of strings, f_{0... \u221e}.\n\nf_0 is \"What are you doing at the end of the world? Are you busy? Will you save us?\".\n\nShe wants to let more people know about it, so she defines f_{i} = \"What are you doing while sending \"f_{i} - 1\"? Are you busy? Will you send \"f_{i} - 1\"?\" for all i \u2265 1.\n\nFor example, f_1 is\n\n\"What are you doing while sending \"What are you doing at the end of the world? Are you busy? Will you save us?\"? Are you busy? Will you send \"What are you doing at the end of the world? Are you busy? Will you save us?\"?\". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f_1.\n\nIt can be seen that the characters in f_{i} are letters, question marks, (possibly) quotation marks and spaces.\n\nNephren will ask the little leprechauns q times. Each time she will let them find the k-th character of f_{n}. The characters are indexed starting from 1. If f_{n} consists of less than k characters, output '.' (without quotes).\n\nCan you answer her queries?\n\n\n-----Input-----\n\nThe first line contains one integer q (1 \u2264 q \u2264 10)\u00a0\u2014 the number of Nephren's questions.\n\nEach of the next q lines describes Nephren's question and contains two integers n and k (0 \u2264 n \u2264 10^5, 1 \u2264 k \u2264 10^18).\n\n\n-----Output-----\n\nOne line containing q characters. The i-th character in it should be the answer for the i-th query.\n\n\n-----Examples-----\nInput\n3\n1 1\n1 2\n1 111111111111\n\nOutput\nWh.\nInput\n5\n0 69\n1 194\n1 139\n0 47\n1 66\n\nOutput\nabdef\nInput\n10\n4 1825\n3 75\n3 530\n4 1829\n4 1651\n3 187\n4 584\n4 255\n4 774\n2 474\n\nOutput\nAreyoubusy\n\n\n-----Note-----\n\nFor the first two examples, refer to f_0 and f_1 given in the legend."} +{"id": "02922", "text": "You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.\n\n\n-----Input-----\n\nThe only line of the input contains a rebus. It's guaranteed that it contains no more than 100 question marks, integer n is positive and doesn't exceed 1 000 000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.\n\n\n-----Output-----\n\nThe first line of the output should contain \"Possible\" (without quotes) if rebus has a solution and \"Impossible\" (without quotes) otherwise.\n\nIf the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to n. Follow the format given in the samples.\n\n\n-----Examples-----\nInput\n? + ? - ? + ? + ? = 42\n\nOutput\nPossible\n9 + 13 - 39 + 28 + 31 = 42\n\nInput\n? - ? = 1\n\nOutput\nImpossible\n\nInput\n? = 1000000\n\nOutput\nPossible\n1000000 = 1000000"} +{"id": "02923", "text": "Consider an N \\times N matrix. Let us denote by a_{i, j} the entry in the i-th row and j-th column. For a_{i, j} where i=1 or j=1 holds, its value is one of 0, 1 and 2 and given in the input. The remaining entries are defined as follows:\n - a_{i,j} = \\mathrm{mex}(a_{i-1,j}, a_{i,j-1}) (2 \\leq i, j \\leq N) where \\mathrm{mex}(x, y) is defined by the following table:\\mathrm{mex}(x, y)y=0y=1y=2x=0121x=1200x=2100\nHow many entries of the matrix are 0, 1, and 2, respectively?\n\n-----Constraints-----\n - 1 \\leq N \\leq 500{,}000\n - a_{i,j}'s given in input are one of 0, 1 and 2.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_{1, 1} a_{1, 1} ... a_{1, N}\na_{2, 1}\n:\na_{N, 1}\n\n-----Output-----\nPrint the number of 0's, 1's, and 2's separated by whitespaces.\n\n-----Sample Input-----\n4\n1 2 0 2\n0\n0\n0\n\n-----Sample Output-----\n7 4 5\n\nThe matrix is as follows:\n1 2 0 2\n0 1 2 0\n0 2 0 1\n0 1 2 0\n"} +{"id": "02924", "text": "Panic is rising in the committee for doggo standardization\u00a0\u2014 the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.\n\nThe committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.\n\nUnfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color $x$ such that there are currently at least two puppies of color $x$ and recolor all puppies of the color $x$ into some arbitrary color $y$. Luckily, this operation can be applied multiple times (including zero).\n\nFor example, if the number of puppies is $7$ and their colors are represented as the string \"abababc\", then in one operation Slava can get the results \"zbzbzbc\", \"bbbbbbc\", \"aaaaaac\", \"acacacc\" and others. However, if the current color sequence is \"abababc\", then he can't choose $x$='c' right now, because currently only one puppy has the color 'c'.\n\nHelp Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^5$)\u00a0\u2014 the number of puppies.\n\nThe second line contains a string $s$ of length $n$ consisting of lowercase Latin letters, where the $i$-th symbol denotes the $i$-th puppy's color.\n\n\n-----Output-----\n\nIf it's possible to recolor all puppies into one color, print \"Yes\".\n\nOtherwise print \"No\".\n\nOutput the answer without quotation signs.\n\n\n-----Examples-----\nInput\n6\naabddc\n\nOutput\nYes\n\nInput\n3\nabc\n\nOutput\nNo\n\nInput\n3\njjj\n\nOutput\nYes\n\n\n\n-----Note-----\n\nIn the first example Slava can perform the following steps: take all puppies of color 'a' (a total of two) and recolor them into 'b'; take all puppies of color 'd' (a total of two) and recolor them into 'c'; take all puppies of color 'b' (three puppies for now) and recolor them into 'c'. \n\nIn the second example it's impossible to recolor any of the puppies.\n\nIn the third example all the puppies' colors are the same; thus there's no need to recolor anything."} +{"id": "02925", "text": "Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer p_{i} (1 \u2264 p_{i} \u2264 n).\n\nLittle penguin Polo loves walking around this village. The walk looks like that. First he stands by a house number x. Then he goes to the house whose number is written on the plaque of house x (that is, to house p_{x}), then he goes to the house whose number is written on the plaque of house p_{x} (that is, to house p_{p}_{x}), and so on.\n\nWe know that: When the penguin starts walking from any house indexed from 1 to k, inclusive, he can walk to house number 1. When the penguin starts walking from any house indexed from k + 1 to n, inclusive, he definitely cannot walk to house number 1. When the penguin starts walking from house number 1, he can get back to house number 1 after some non-zero number of walks from a house to a house. \n\nYou need to find the number of ways you may write the numbers on the houses' plaques so as to fulfill the three above described conditions. Print the remainder after dividing this number by 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe single line contains two space-separated integers n and k (1 \u2264 n \u2264 1000, 1 \u2264 k \u2264 min(8, n)) \u2014 the number of the houses and the number k from the statement.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the answer to the problem modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n5 2\n\nOutput\n54\n\nInput\n7 4\n\nOutput\n1728"} +{"id": "02926", "text": "Consider a table of size $n \\times m$, initially fully white. Rows are numbered $1$ through $n$ from top to bottom, columns $1$ through $m$ from left to right. Some square inside the table with odd side length was painted black. Find the center of this square.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 115$) \u2014 the number of rows and the number of columns in the table.\n\nThe $i$-th of the next $n$ lines contains a string of $m$ characters $s_{i1} s_{i2} \\ldots s_{im}$ ($s_{ij}$ is 'W' for white cells and 'B' for black cells), describing the $i$-th row of the table.\n\n\n-----Output-----\n\nOutput two integers $r$ and $c$ ($1 \\le r \\le n$, $1 \\le c \\le m$) separated by a space \u2014 the row and column numbers of the center of the black square.\n\n\n-----Examples-----\nInput\n5 6\nWWBBBW\nWWBBBW\nWWBBBW\nWWWWWW\nWWWWWW\n\nOutput\n2 4\n\nInput\n3 3\nWWW\nBWW\nWWW\n\nOutput\n2 1"} +{"id": "02927", "text": "This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.\n\nAlice received a set of Toy Train\u2122 from Bob. It consists of one train and a connected railway network of $n$ stations, enumerated from $1$ through $n$. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station $i$ is station $i+1$ if $1 \\leq i < n$ or station $1$ if $i = n$. It takes the train $1$ second to travel to its next station as described.\n\nBob gave Alice a fun task before he left: to deliver $m$ candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from $1$ through $m$. Candy $i$ ($1 \\leq i \\leq m$), now at station $a_i$, should be delivered to station $b_i$ ($a_i \\neq b_i$). [Image] The blue numbers on the candies correspond to $b_i$ values. The image corresponds to the $1$-st example. \n\nThe train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.\n\nNow, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers $n$ and $m$ ($2 \\leq n \\leq 100$; $1 \\leq m \\leq 200$) \u2014 the number of stations and the number of candies, respectively.\n\nThe $i$-th of the following $m$ lines contains two space-separated integers $a_i$ and $b_i$ ($1 \\leq a_i, b_i \\leq n$; $a_i \\neq b_i$) \u2014 the station that initially contains candy $i$ and the destination station of the candy, respectively.\n\n\n-----Output-----\n\nIn the first and only line, print $n$ space-separated integers, the $i$-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station $i$.\n\n\n-----Examples-----\nInput\n5 7\n2 4\n5 1\n2 3\n3 4\n4 1\n5 3\n3 5\n\nOutput\n10 9 10 10 9 \n\nInput\n2 3\n1 2\n1 2\n1 2\n\nOutput\n5 6 \n\n\n\n-----Note-----\n\nConsider the second sample.\n\nIf the train started at station $1$, the optimal strategy is as follows. Load the first candy onto the train. Proceed to station $2$. This step takes $1$ second. Deliver the first candy. Proceed to station $1$. This step takes $1$ second. Load the second candy onto the train. Proceed to station $2$. This step takes $1$ second. Deliver the second candy. Proceed to station $1$. This step takes $1$ second. Load the third candy onto the train. Proceed to station $2$. This step takes $1$ second. Deliver the third candy. \n\nHence, the train needs $5$ seconds to complete the tasks.\n\nIf the train were to start at station $2$, however, it would need to move to station $1$ before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is $5+1 = 6$ seconds."} +{"id": "02928", "text": "Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation a_{i}x + b_{i}y + c_{i} = 0, where a_{i} and b_{i} are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.\n\nYour home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).\n\nDetermine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers x_1, y_1 ( - 10^6 \u2264 x_1, y_1 \u2264 10^6) \u2014 the coordinates of your home.\n\nThe second line contains two integers separated by a space x_2, y_2 ( - 10^6 \u2264 x_2, y_2 \u2264 10^6) \u2014 the coordinates of the university you are studying at.\n\nThe third line contains an integer n (1 \u2264 n \u2264 300) \u2014 the number of roads in the city. The following n lines contain 3 space-separated integers ( - 10^6 \u2264 a_{i}, b_{i}, c_{i} \u2264 10^6; |a_{i}| + |b_{i}| > 0) \u2014 the coefficients of the line a_{i}x + b_{i}y + c_{i} = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).\n\n\n-----Output-----\n\nOutput the answer to the problem.\n\n\n-----Examples-----\nInput\n1 1\n-1 -1\n2\n0 1 0\n1 0 0\n\nOutput\n2\n\nInput\n1 1\n-1 -1\n3\n1 0 0\n0 1 0\n1 1 -3\n\nOutput\n2\n\n\n\n-----Note-----\n\nPictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): [Image] [Image]"} +{"id": "02929", "text": "Kevin and Nicky Sun have invented a new game called Lieges of Legendre. In this game, two players take turns modifying the game state with Kevin moving first. Initially, the game is set up so that there are n piles of cows, with the i-th pile containing a_{i} cows. During each player's turn, that player calls upon the power of Sunlight, and uses it to either:\n\n Remove a single cow from a chosen non-empty pile. Choose a pile of cows with even size 2\u00b7x (x > 0), and replace it with k piles of x cows each. \n\nThe player who removes the last cow wins. Given n, k, and a sequence a_1, a_2, ..., a_{n}, help Kevin and Nicky find the winner, given that both sides play in optimal way.\n\n\n-----Input-----\n\nThe first line of the input contains two space-separated integers n and k (1 \u2264 n \u2264 100 000, 1 \u2264 k \u2264 10^9).\n\nThe second line contains n integers, a_1, a_2, ... a_{n} (1 \u2264 a_{i} \u2264 10^9) describing the initial state of the game. \n\n\n-----Output-----\n\nOutput the name of the winning player, either \"Kevin\" or \"Nicky\" (without quotes).\n\n\n-----Examples-----\nInput\n2 1\n3 4\n\nOutput\nKevin\n\nInput\n1 2\n3\n\nOutput\nNicky\n\n\n\n-----Note-----\n\nIn the second sample, Nicky can win in the following way: Kevin moves first and is forced to remove a cow, so the pile contains two cows after his move. Next, Nicky replaces this pile of size 2 with two piles of size 1. So the game state is now two piles of size 1. Kevin then removes one of the remaining cows and Nicky wins by removing the other."} +{"id": "02930", "text": "Artsem has a friend Saunders from University of Chicago. Saunders presented him with the following problem.\n\nLet [n] denote the set {1, ..., n}. We will also write f: [x] \u2192 [y] when a function f is defined in integer points 1, ..., x, and all its values are integers from 1 to y.\n\nNow then, you are given a function f: [n] \u2192 [n]. Your task is to find a positive integer m, and two functions g: [n] \u2192 [m], h: [m] \u2192 [n], such that g(h(x)) = x for all $x \\in [ m ]$, and h(g(x)) = f(x) for all $x \\in [ n ]$, or determine that finding these is impossible.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^5).\n\nThe second line contains n space-separated integers\u00a0\u2014 values f(1), ..., f(n) (1 \u2264 f(i) \u2264 n).\n\n\n-----Output-----\n\nIf there is no answer, print one integer -1.\n\nOtherwise, on the first line print the number m (1 \u2264 m \u2264 10^6). On the second line print n numbers g(1), ..., g(n). On the third line print m numbers h(1), ..., h(m).\n\nIf there are several correct answers, you may output any of them. It is guaranteed that if a valid answer exists, then there is an answer satisfying the above restrictions.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n3\n1 2 3\n1 2 3\n\nInput\n3\n2 2 2\n\nOutput\n1\n1 1 1\n2\n\nInput\n2\n2 1\n\nOutput\n-1"} +{"id": "02931", "text": "As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2^{n} members and coincidentally Natalia Fan Club also has 2^{n} members. Each member of MDC is assigned a unique id i from 0 to 2^{n} - 1. The same holds for each member of NFC.\n\nOne of the parts of this tradition is one by one dance, where each member of MDC dances with a member of NFC. A dance pair is a pair of numbers (a, b) such that member a from MDC dances with member b from NFC.\n\nThe complexity of a pairs' assignment is the number of pairs of dancing pairs (a, b) and (c, d) such that a < c and b > d.\n\nYou are given a binary number of length n named x. We know that member i from MDC dances with member $i \\oplus x$ from NFC. Your task is to calculate the complexity of this assignment modulo 1000000007 (10^9 + 7).\n\nExpression $x \\oplus y$ denotes applying \u00abXOR\u00bb to numbers x and y. This operation exists in all modern programming languages, for example, in C++ and Java it denotes as \u00ab^\u00bb, in Pascal \u2014 \u00abxor\u00bb.\n\n\n-----Input-----\n\nThe first line of input contains a binary number x of lenght n, (1 \u2264 n \u2264 100).\n\nThis number may contain leading zeros.\n\n\n-----Output-----\n\nPrint the complexity of the given dance assignent modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n11\n\nOutput\n6\n\nInput\n01\n\nOutput\n2\n\nInput\n1\n\nOutput\n1"} +{"id": "02932", "text": "You are given an integer m as a product of integers a_1, a_2, ... a_{n} $(m = \\prod_{i = 1}^{n} a_{i})$. Your task is to find the number of distinct decompositions of number m into the product of n ordered positive integers.\n\nDecomposition into n products, given in the input, must also be considered in the answer. As the answer can be very large, print it modulo 1000000007 (10^9 + 7).\n\n\n-----Input-----\n\nThe first line contains positive integer n (1 \u2264 n \u2264 500). The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nIn a single line print a single number k \u2014 the number of distinct decompositions of number m into n ordered multipliers modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n1\n15\n\nOutput\n1\n\nInput\n3\n1 1 2\n\nOutput\n3\n\nInput\n2\n5 7\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the second sample, the get a decomposition of number 2, you need any one number out of three to equal 2, and the rest to equal 1.\n\nIn the third sample, the possible ways of decomposing into ordered multipliers are [7,5], [5,7], [1,35], [35,1].\n\nA decomposition of positive integer m into n ordered multipliers is a cortege of positive integers b = {b_1, b_2, ... b_{n}} such that $m = \\prod_{i = 1}^{n} b_{i}$. Two decompositions b and c are considered different, if there exists index i such that b_{i} \u2260 c_{i}."} +{"id": "02933", "text": "A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle\u00a0\u2014 a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.\n\nThe islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.\n\nDetermine if it is possible for the islanders to arrange the statues in the desired order.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 200 000)\u00a0\u2014 the total number of islands.\n\nThe second line contains n space-separated integers a_{i} (0 \u2264 a_{i} \u2264 n - 1)\u00a0\u2014 the statue currently placed on the i-th island. If a_{i} = 0, then the island has no statue. It is guaranteed that the a_{i} are distinct.\n\nThe third line contains n space-separated integers b_{i} (0 \u2264 b_{i} \u2264 n - 1) \u2014 the desired statues of the ith island. Once again, b_{i} = 0 indicates the island desires no statue. It is guaranteed that the b_{i} are distinct.\n\n\n-----Output-----\n\nPrint \"YES\" (without quotes) if the rearrangement can be done in the existing network, and \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n3\n1 0 2\n2 0 1\n\nOutput\nYES\n\nInput\n2\n1 0\n0 1\n\nOutput\nYES\n\nInput\n4\n1 2 3 0\n0 3 2 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.\n\nIn the second sample, the islanders can simply move statue 1 from island 1 to island 2.\n\nIn the third sample, no sequence of movements results in the desired position."} +{"id": "02934", "text": "You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume.\n\nYou have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task can be any, the second task on each computer must use strictly less power than the first. You will assign between 1 and 2 tasks to each computer. You will then first execute the first task on each computer, wait for all of them to complete, and then execute the second task on each computer that has two tasks assigned.\n\nIf the average compute power per utilized processor (the sum of all consumed powers for all tasks presently running divided by the number of utilized processors) across all computers exceeds some unknown threshold during the execution of the first tasks, the entire system will blow up. There is no restriction on the second tasks execution. Find the lowest threshold for which it is possible.\n\nDue to the specifics of the task, you need to print the answer multiplied by 1000 and rounded up.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 50) \u2014 the number of tasks.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^8), where a_{i} represents the amount of power required for the i-th task.\n\nThe third line contains n integers b_1, b_2, ..., b_{n} (1 \u2264 b_{i} \u2264 100), where b_{i} is the number of processors that i-th task will utilize.\n\n\n-----Output-----\n\nPrint a single integer value \u2014 the lowest threshold for which it is possible to assign all tasks in such a way that the system will not blow up after the first round of computation, multiplied by 1000 and rounded up.\n\n\n-----Examples-----\nInput\n6\n8 10 9 9 8 10\n1 1 1 1 1 1\n\nOutput\n9000\n\nInput\n6\n8 10 9 9 8 10\n1 10 5 5 1 10\n\nOutput\n1160\n\n\n\n-----Note-----\n\nIn the first example the best strategy is to run each task on a separate computer, getting average compute per processor during the first round equal to 9.\n\nIn the second task it is best to run tasks with compute 10 and 9 on one computer, tasks with compute 10 and 8 on another, and tasks with compute 9 and 8 on the last, averaging (10 + 10 + 9) / (10 + 10 + 5) = 1.16 compute power per processor during the first round."} +{"id": "02935", "text": "Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the n coders on the meeting said: 'I know for sure that either x or y did it!'\n\nThe head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders' opinions. That's why the head wants to make such a choice that at least p of n coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects?\n\nNote that even if some coder was chosen as a suspect, he can agree with the head's choice if he named the other chosen coder at the meeting.\n\n\n-----Input-----\n\nThe first line contains integers n and p (3 \u2264 n \u2264 3\u00b710^5;\u00a00 \u2264 p \u2264 n) \u2014 the number of coders in the F company and the minimum number of agreed people.\n\nEach of the next n lines contains two integers x_{i}, y_{i} (1 \u2264 x_{i}, y_{i} \u2264 n) \u2014 the numbers of coders named by the i-th coder. It is guaranteed that x_{i} \u2260 i, \u00a0y_{i} \u2260 i, \u00a0x_{i} \u2260 y_{i}.\n\n\n-----Output-----\n\nPrint a single integer \u2013\u2013 the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets (1, 2) \u0438 (2, 1) are considered identical.\n\n\n-----Examples-----\nInput\n4 2\n2 3\n1 4\n1 4\n2 1\n\nOutput\n6\n\nInput\n8 6\n5 6\n5 7\n5 8\n6 2\n2 1\n7 3\n1 3\n1 4\n\nOutput\n1"} +{"id": "02936", "text": "You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor.\n\nWhat is the minimum number of operations you need to make all of the elements equal to 1?\n\n\n-----Input-----\n\nThe first line of the input contains one integer n (1 \u2264 n \u2264 2000) \u2014 the number of elements in the array.\n\nThe second line contains n space separated integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.\n\n\n-----Examples-----\nInput\n5\n2 2 3 4 6\n\nOutput\n5\n\nInput\n4\n2 4 6 8\n\nOutput\n-1\n\nInput\n3\n2 6 9\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample you can turn all numbers to 1 using the following 5 moves:\n\n [2, 2, 3, 4, 6]. [2, 1, 3, 4, 6] [2, 1, 3, 1, 6] [2, 1, 1, 1, 6] [1, 1, 1, 1, 6] [1, 1, 1, 1, 1] \n\nWe can prove that in this case it is not possible to make all numbers one using less than 5 moves."} +{"id": "02937", "text": "First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. \n\nFor example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the \"root\" of the word \u2014 some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction \u2014 it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word \"suffix\" to describe a morpheme but not the few last characters of the string as you may used to). \n\nHere is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. \n\nTwo strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. \n\nLet's look at the example: the word abacabaca is given. This word can be obtained in the following ways: [Image], where the root of the word is overlined, and suffixes are marked by \"corners\". Thus, the set of possible suffixes for this word is {aca, ba, ca}. \n\n\n-----Input-----\n\nThe only line contains a string s (5 \u2264 |s| \u2264 10^4) consisting of lowercase English letters.\n\n\n-----Output-----\n\nOn the first line print integer k \u2014 a number of distinct possible suffixes. On the next k lines print suffixes. \n\nPrint suffixes in lexicographical (alphabetical) order. \n\n\n-----Examples-----\nInput\nabacabaca\n\nOutput\n3\naca\nba\nca\n\nInput\nabaca\n\nOutput\n0\n\n\n\n-----Note-----\n\nThe first test was analysed in the problem statement. \n\nIn the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix."} +{"id": "02938", "text": "The famous global economic crisis is approaching rapidly, so the states of Berman, Berance and Bertaly formed an alliance and allowed the residents of all member states to freely pass through the territory of any of them. In addition, it was decided that a road between the states should be built to guarantee so that one could any point of any country can be reached from any point of any other State.\n\nSince roads are always expensive, the governments of the states of the newly formed alliance asked you to help them assess the costs. To do this, you have been issued a map that can be represented as a rectangle table consisting of n rows and m columns. Any cell of the map either belongs to one of three states, or is an area where it is allowed to build a road, or is an area where the construction of the road is not allowed. A cell is called passable, if it belongs to one of the states, or the road was built in this cell. From any passable cells you can move up, down, right and left, if the cell that corresponds to the movement exists and is passable.\n\nYour task is to construct a road inside a minimum number of cells, so that it would be possible to get from any cell of any state to any cell of any other state using only passable cells.\n\nIt is guaranteed that initially it is possible to reach any cell of any state from any cell of this state, moving only along its cells. It is also guaranteed that for any state there is at least one cell that belongs to it.\n\n\n-----Input-----\n\nThe first line of the input contains the dimensions of the map n and m (1 \u2264 n, m \u2264 1000)\u00a0\u2014 the number of rows and columns respectively.\n\nEach of the next n lines contain m characters, describing the rows of the map. Digits from 1 to 3 represent the accessory to the corresponding state. The character '.' corresponds to the cell where it is allowed to build a road and the character '#' means no construction is allowed in this cell.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of cells you need to build a road inside in order to connect all the cells of all states. If such a goal is unachievable, print -1.\n\n\n-----Examples-----\nInput\n4 5\n11..2\n#..22\n#.323\n.#333\nOutput\n2\nInput\n1 5\n1#2#3\n\nOutput\n-1"} +{"id": "02939", "text": "A bracketed sequence is called correct (regular) if by inserting \"+\" and \"1\" you can get a well-formed mathematical expression from it. For example, sequences \"(())()\", \"()\" and \"(()(()))\" are correct, while \")(\", \"(()\" and \"(()))(\" are not.\n\nThe teacher gave Dmitry's class a very strange task\u00a0\u2014 she asked every student to come up with a sequence of arbitrary length, consisting only of opening and closing brackets. After that all the students took turns naming the sequences they had invented. When Dima's turn came, he suddenly realized that all his classmates got the correct bracketed sequence, and whether he got the correct bracketed sequence, he did not know.\n\nDima suspects now that he simply missed the word \"correct\" in the task statement, so now he wants to save the situation by modifying his sequence slightly. More precisely, he can the arbitrary number of times (possibly zero) perform the reorder operation.\n\nThe reorder operation consists of choosing an arbitrary consecutive subsegment (substring) of the sequence and then reordering all the characters in it in an arbitrary way. Such operation takes $l$ nanoseconds, where $l$ is the length of the subsegment being reordered. It's easy to see that reorder operation doesn't change the number of opening and closing brackets. For example for \"))((\" he can choose the substring \")(\" and do reorder \")()(\" (this operation will take $2$ nanoseconds).\n\nSince Dima will soon have to answer, he wants to make his sequence correct as fast as possible. Help him to do this, or determine that it's impossible.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^6$)\u00a0\u2014 the length of Dima's sequence.\n\nThe second line contains string of length $n$, consisting of characters \"(\" and \")\" only.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum number of nanoseconds to make the sequence correct or \"-1\" if it is impossible to do so.\n\n\n-----Examples-----\nInput\n8\n))((())(\n\nOutput\n6\n\nInput\n3\n(()\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example we can firstly reorder the segment from first to the fourth character, replacing it with \"()()\", the whole sequence will be \"()()())(\". And then reorder the segment from the seventh to eighth character, replacing it with \"()\". In the end the sequence will be \"()()()()\", while the total time spent is $4 + 2 = 6$ nanoseconds."} +{"id": "02940", "text": "BigData Inc. is a corporation that has n data centers indexed from 1 to n that are located all over the world. These data centers provide storage for client data (you can figure out that client data is really big!).\n\nMain feature of services offered by BigData Inc. is the access availability guarantee even under the circumstances of any data center having an outage. Such a guarantee is ensured by using the two-way replication. Two-way replication is such an approach for data storage that any piece of data is represented by two identical copies that are stored in two different data centers.\n\nFor each of m company clients, let us denote indices of two different data centers storing this client data as c_{i}, 1 and c_{i}, 2.\n\nIn order to keep data centers operational and safe, the software running on data center computers is being updated regularly. Release cycle of BigData Inc. is one day meaning that the new version of software is being deployed to the data center computers each day.\n\nData center software update is a non-trivial long process, that is why there is a special hour-long time frame that is dedicated for data center maintenance. During the maintenance period, data center computers are installing software updates, and thus they may be unavailable. Consider the day to be exactly h hours long. For each data center there is an integer u_{j} (0 \u2264 u_{j} \u2264 h - 1) defining the index of an hour of day, such that during this hour data center j is unavailable due to maintenance.\n\nSumming up everything above, the condition u_{c}_{i}, 1 \u2260 u_{c}_{i}, 2 should hold for each client, or otherwise his data may be unaccessible while data centers that store it are under maintenance.\n\nDue to occasional timezone change in different cities all over the world, the maintenance time in some of the data centers may change by one hour sometimes. Company should be prepared for such situation, that is why they decided to conduct an experiment, choosing some non-empty subset of data centers, and shifting the maintenance time for them by an hour later (i.e. if u_{j} = h - 1, then the new maintenance hour would become 0, otherwise it would become u_{j} + 1). Nonetheless, such an experiment should not break the accessibility guarantees, meaning that data of any client should be still available during any hour of a day after the data center maintenance times are changed.\n\nSuch an experiment would provide useful insights, but changing update time is quite an expensive procedure, that is why the company asked you to find out the minimum number of data centers that have to be included in an experiment in order to keep the data accessibility guarantees.\n\n\n-----Input-----\n\nThe first line of input contains three integers n, m and h (2 \u2264 n \u2264 100 000, 1 \u2264 m \u2264 100 000, 2 \u2264 h \u2264 100 000), the number of company data centers, number of clients and the day length of day measured in hours. \n\nThe second line of input contains n integers u_1, u_2, ..., u_{n} (0 \u2264 u_{j} < h), j-th of these numbers is an index of a maintenance hour for data center j. \n\nEach of the next m lines contains two integers c_{i}, 1 and c_{i}, 2 (1 \u2264 c_{i}, 1, c_{i}, 2 \u2264 n, c_{i}, 1 \u2260 c_{i}, 2), defining the data center indices containing the data of client i.\n\nIt is guaranteed that the given maintenance schedule allows each client to access at least one copy of his data at any moment of day.\n\n\n-----Output-----\n\nIn the first line print the minimum possible number of data centers k (1 \u2264 k \u2264 n) that have to be included in an experiment in order to keep the data available for any client.\n\nIn the second line print k distinct integers x_1, x_2, ..., x_{k} (1 \u2264 x_{i} \u2264 n), the indices of data centers whose maintenance time will be shifted by one hour later. Data center indices may be printed in any order.\n\nIf there are several possible answers, it is allowed to print any of them. It is guaranteed that at there is at least one valid choice of data centers.\n\n\n-----Examples-----\nInput\n3 3 5\n4 4 0\n1 3\n3 2\n3 1\n\nOutput\n1\n3 \nInput\n4 5 4\n2 1 0 3\n4 3\n3 2\n1 2\n1 4\n1 3\n\nOutput\n4\n1 2 3 4 \n\n\n-----Note-----\n\nConsider the first sample test. The given answer is the only way to conduct an experiment involving the only data center. In such a scenario the third data center has a maintenance during the hour 1, and no two data centers storing the information of the same client have maintenance at the same hour.\n\nOn the other hand, for example, if we shift the maintenance time on hour later for the first data center, then the data of clients 1 and 3 will be unavailable during the hour 0."} +{"id": "02941", "text": "Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area.\n\nTo prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of $n$ rows and $m$ columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side.\n\nIvan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo $10^9 + 7$.\n\n\n-----Input-----\n\nThe only line contains two integers $n$ and $m$ ($1 \\le n, m \\le 100\\,000$), the number of rows and the number of columns of the field.\n\n\n-----Output-----\n\nPrint one integer, the number of random pictures modulo $10^9 + 7$.\n\n\n-----Example-----\nInput\n2 3\n\nOutput\n8\n\n\n\n-----Note-----\n\nThe picture below shows all possible random pictures of size $2$ by $3$. [Image]"} +{"id": "02942", "text": "Let's call an array consisting of n integer numbers a_1, a_2, ..., a_{n}, beautiful if it has the following property:\n\n consider all pairs of numbers x, y (x \u2260 y), such that number x occurs in the array a and number y occurs in the array a; for each pair x, y must exist some position j (1 \u2264 j < n), such that at least one of the two conditions are met, either a_{j} = x, a_{j} + 1 = y, or a_{j} = y, a_{j} + 1 = x. \n\nSereja wants to build a beautiful array a, consisting of n integers. But not everything is so easy, Sereja's friend Dima has m coupons, each contains two integers q_{i}, w_{i}. Coupon i costs w_{i} and allows you to use as many numbers q_{i} as you want when constructing the array a. Values q_{i} are distinct. Sereja has no coupons, so Dima and Sereja have made the following deal. Dima builds some beautiful array a of n elements. After that he takes w_{i} rubles from Sereja for each q_{i}, which occurs in the array a. Sereja believed his friend and agreed to the contract, and now he is wondering, what is the maximum amount of money he can pay.\n\nHelp Sereja, find the maximum amount of money he can pay to Dima.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 2\u00b710^6, 1 \u2264 m \u2264 10^5). Next m lines contain pairs of integers. The i-th line contains numbers q_{i}, w_{i} (1 \u2264 q_{i}, w_{i} \u2264 10^5).\n\nIt is guaranteed that all q_{i} are distinct.\n\n\n-----Output-----\n\nIn a single line print maximum amount of money (in rubles) Sereja can pay.\n\nPlease, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n5 2\n1 2\n2 3\n\nOutput\n5\n\nInput\n100 3\n1 2\n2 1\n3 1\n\nOutput\n4\n\nInput\n1 2\n1 1\n2 100\n\nOutput\n100\n\n\n\n-----Note-----\n\nIn the first sample Sereja can pay 5 rubles, for example, if Dima constructs the following array: [1, 2, 1, 2, 2]. There are another optimal arrays for this test.\n\nIn the third sample Sereja can pay 100 rubles, if Dima constructs the following array: [2]."} +{"id": "02943", "text": "Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside.\n\nThe text is a string $s$ of lowercase Latin letters. She considers a string $t$ as hidden in string $s$ if $t$ exists as a subsequence of $s$ whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices $1$, $3$, and $5$, which form an arithmetic progression with a common difference of $2$. Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of $S$ are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message!\n\nFor example, in the string aaabb, a is hidden $3$ times, b is hidden $2$ times, ab is hidden $6$ times, aa is hidden $3$ times, bb is hidden $1$ time, aab is hidden $2$ times, aaa is hidden $1$ time, abb is hidden $1$ time, aaab is hidden $1$ time, aabb is hidden $1$ time, and aaabb is hidden $1$ time. The number of occurrences of the secret message is $6$.\n\n\n-----Input-----\n\nThe first line contains a string $s$ of lowercase Latin letters ($1 \\le |s| \\le 10^5$) \u2014 the text that Bessie intercepted.\n\n\n-----Output-----\n\nOutput a single integer \u00a0\u2014 the number of occurrences of the secret message.\n\n\n-----Examples-----\nInput\naaabb\n\nOutput\n6\n\nInput\nusaco\n\nOutput\n1\n\nInput\nlol\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example, these are all the hidden strings and their indice sets: a occurs at $(1)$, $(2)$, $(3)$ b occurs at $(4)$, $(5)$ ab occurs at $(1,4)$, $(1,5)$, $(2,4)$, $(2,5)$, $(3,4)$, $(3,5)$ aa occurs at $(1,2)$, $(1,3)$, $(2,3)$ bb occurs at $(4,5)$ aab occurs at $(1,3,5)$, $(2,3,4)$ aaa occurs at $(1,2,3)$ abb occurs at $(3,4,5)$ aaab occurs at $(1,2,3,4)$ aabb occurs at $(2,3,4,5)$ aaabb occurs at $(1,2,3,4,5)$ Note that all the sets of indices are arithmetic progressions.\n\nIn the second example, no hidden string occurs more than once.\n\nIn the third example, the hidden string is the letter l."} +{"id": "02944", "text": "Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.\n\nOne day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?\n\nThe formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.\n\n\n-----Input-----\n\nThe single line contains a single integer n (1 \u2264 n \u2264 10^17).\n\nPlease, do not use the %lld specifier to read or write 64 bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Output-----\n\nIn a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n4\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.\n\nIn the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives."} +{"id": "02945", "text": "Note that girls in Arpa\u2019s land are really attractive.\n\nArpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw n pairs of friends sitting around a table. i-th pair consisted of a boy, sitting on the a_{i}-th chair, and his girlfriend, sitting on the b_{i}-th chair. The chairs were numbered 1 through 2n in clockwise direction. There was exactly one person sitting on each chair.\n\n [Image] \n\nThere were two types of food: Kooft and Zahre-mar. Now Mehrdad wonders, was there any way to serve food for the guests such that: Each person had exactly one type of food, No boy had the same type of food as his girlfriend, Among any three guests sitting on consecutive chairs, there was two of them who had different type of food. Note that chairs 2n and 1 are considered consecutive. \n\nFind the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 10^5)\u00a0\u2014 the number of pairs of guests.\n\nThe i-th of the next n lines contains a pair of integers a_{i} and b_{i} (1 \u2264 a_{i}, b_{i} \u2264 2n)\u00a0\u2014 the number of chair on which the boy in the i-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed that there was exactly one person sitting on each chair. \n\n\n-----Output-----\n\nIf there is no solution, print -1.\n\nOtherwise print n lines, the i-th of them should contain two integers which represent the type of food for the i-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, otherwise print 2.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Example-----\nInput\n3\n1 4\n2 5\n3 6\n\nOutput\n1 2\n2 1\n1 2"} +{"id": "02946", "text": "Your friend recently gave you some slimes for your birthday. You have n slimes all initially with value 1.\n\nYou are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other n - 1 slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value v, you combine them together to create a slime with value v + 1.\n\nYou would like to see what the final state of the row is after you've added all n slimes. Please print the values of the slimes in the row from left to right.\n\n\n-----Input-----\n\nThe first line of the input will contain a single integer, n (1 \u2264 n \u2264 100 000).\n\n\n-----Output-----\n\nOutput a single line with k integers, where k is the number of slimes in the row after you've finished the procedure described in the problem statement. The i-th of these numbers should be the value of the i-th slime from the left.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n2\n\nOutput\n2\n\nInput\n3\n\nOutput\n2 1\n\nInput\n8\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first sample, we only have a single slime with value 1. The final state of the board is just a single slime with value 1.\n\nIn the second sample, we perform the following steps:\n\nInitially we place a single slime in a row by itself. Thus, row is initially 1.\n\nThen, we will add another slime. The row is now 1 1. Since two rightmost slimes have the same values, we should replace these slimes with one with value 2. Thus, the final state of the board is 2.\n\nIn the third sample, after adding the first two slimes, our row is 2. After adding one more slime, the row becomes 2 1.\n\nIn the last sample, the steps look as follows: 1 2 2 1 3 3 1 3 2 3 2 1 4"} +{"id": "02947", "text": "Kolya has a string s of length n consisting of lowercase and uppercase Latin letters and digits.\n\nHe wants to rearrange the symbols in s and cut it into the minimum number of parts so that each part is a palindrome and all parts have the same lengths. A palindrome is a string which reads the same backward as forward, such as madam or racecar.\n\nYour task is to help Kolya and determine the minimum number of palindromes of equal lengths to cut s into, if it is allowed to rearrange letters in s before cuttings.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 4\u00b710^5) \u2014 the length of string s.\n\nThe second line contains a string s of length n consisting of lowercase and uppercase Latin letters and digits.\n\n\n-----Output-----\n\nPrint to the first line an integer k \u2014 minimum number of palindromes into which you can cut a given string.\n\nPrint to the second line k strings \u2014 the palindromes themselves. Separate them by a space. You are allowed to print palindromes in arbitrary order. All of them should have the same length.\n\n\n-----Examples-----\nInput\n6\naabaac\n\nOutput\n2\naba aca \nInput\n8\n0rTrT022\n\nOutput\n1\n02TrrT20 \nInput\n2\naA\n\nOutput\n2\na A"} +{"id": "02948", "text": "Polycarpus takes part in the \"Field of Wonders\" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden.\n\nThe game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is \"abacaba\" and the player tells the letter \"a\", the host will reveal letters at all positions, occupied by \"a\": 1, 3, 5 and 7 (positions are numbered from left to right starting from 1).\n\nPolycarpus knows m words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these m words.\n\nAt current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed.\n\nIt is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed.\n\nYour task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 50) \u2014 the length of the hidden word.\n\nThe following line describes already revealed letters. It contains the string of length n, which consists of lowercase Latin letters and symbols \"*\". If there is a letter at some position, then this letter was already revealed. If the position contains symbol \"*\", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed.\n\nThe third line contains an integer m (1 \u2264 m \u2264 1000) \u2014 the number of words of length n, which Polycarpus knows. The following m lines contain the words themselves \u2014 n-letter strings of lowercase Latin letters. All words are distinct.\n\nIt is guaranteed that the hidden word appears as one of the given m words. Before the current move Polycarp has told exactly the letters which are currently revealed.\n\n\n-----Output-----\n\nOutput the single integer \u2014 the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero.\n\n\n-----Examples-----\nInput\n4\na**d\n2\nabcd\nacbd\n\nOutput\n2\n\nInput\n5\nlo*er\n2\nlover\nloser\n\nOutput\n0\n\nInput\n3\na*a\n2\naaa\naba\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example Polycarpus can tell letters \"b\" and \"c\", which assuredly will be revealed.\n\nThe second example contains no letters which can be told as it is not clear, which of the letters \"v\" or \"s\" is located at the third position of the hidden word.\n\nIn the third example Polycarpus exactly knows that the hidden word is \"aba\", because in case it was \"aaa\", then the second letter \"a\" would have already been revealed in one of previous turns."} +{"id": "02949", "text": "Instructors of Some Informatics School make students go to bed.\n\nThe house contains n rooms, in each room exactly b students were supposed to sleep. However, at the time of curfew it happened that many students are not located in their assigned rooms. The rooms are arranged in a row and numbered from 1 to n. Initially, in i-th room there are a_{i} students. All students are currently somewhere in the house, therefore a_1 + a_2 + ... + a_{n} = nb. Also 2 instructors live in this house.\n\nThe process of curfew enforcement is the following. One instructor starts near room 1 and moves toward room n, while the second instructor starts near room n and moves toward room 1. After processing current room, each instructor moves on to the next one. Both instructors enter rooms and move simultaneously, if n is odd, then only the first instructor processes the middle room. When all rooms are processed, the process ends.\n\nWhen an instructor processes a room, she counts the number of students in the room, then turns off the light, and locks the room. Also, if the number of students inside the processed room is not equal to b, the instructor writes down the number of this room into her notebook (and turns off the light, and locks the room). Instructors are in a hurry (to prepare the study plan for the next day), so they don't care about who is in the room, but only about the number of students.\n\nWhile instructors are inside the rooms, students can run between rooms that are not locked and not being processed. A student can run by at most d rooms, that is she can move to a room with number that differs my at most d. Also, after (or instead of) running each student can hide under a bed in a room she is in. In this case the instructor will not count her during the processing. In each room any number of students can hide simultaneously.\n\nFormally, here is what's happening: A curfew is announced, at this point in room i there are a_{i} students. Each student can run to another room but not further than d rooms away from her initial room, or stay in place. After that each student can optionally hide under a bed. Instructors enter room 1 and room n, they count students there and lock the room (after it no one can enter or leave this room). Each student from rooms with numbers from 2 to n - 1 can run to another room but not further than d rooms away from her current room, or stay in place. Each student can optionally hide under a bed. Instructors move from room 1 to room 2 and from room n to room n - 1. This process continues until all rooms are processed. \n\nLet x_1 denote the number of rooms in which the first instructor counted the number of non-hidden students different from b, and x_2 be the same number for the second instructor. Students know that the principal will only listen to one complaint, therefore they want to minimize the maximum of numbers x_{i}. Help them find this value if they use the optimal strategy.\n\n\n-----Input-----\n\nThe first line contains three integers n, d and b (2 \u2264 n \u2264 100 000, 1 \u2264 d \u2264 n - 1, 1 \u2264 b \u2264 10 000), number of rooms in the house, running distance of a student, official number of students in a room.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9), i-th of which stands for the number of students in the i-th room before curfew announcement.\n\nIt is guaranteed that a_1 + a_2 + ... + a_{n} = nb.\n\n\n-----Output-----\n\nOutput one integer, the minimal possible value of the maximum of x_{i}.\n\n\n-----Examples-----\nInput\n5 1 1\n1 0 0 0 4\n\nOutput\n1\n\nInput\n6 1 2\n3 8 0 1 0 0\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first sample the first three rooms are processed by the first instructor, and the last two are processed by the second instructor. One of the optimal strategies is the following: firstly three students run from room 5 to room 4, on the next stage two of them run to room 3, and one of those two hides under a bed. This way, the first instructor writes down room 2, and the second writes down nothing.\n\nIn the second sample one of the optimal strategies is the following: firstly all students in room 1 hide, all students from room 2 run to room 3. On the next stage one student runs from room 3 to room 4, and 5 students hide. This way, the first instructor writes down rooms 1 and 2, the second instructor writes down rooms 5 and 6."} +{"id": "02950", "text": "Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable properties. Help Rikhail to convince the scientific community in this!\n\nLet us remind you that a number is called prime if it is integer larger than one, and is not divisible by any positive integer other than itself and one.\n\nRikhail calls a number a palindromic if it is integer, positive, and its decimal representation without leading zeros is a palindrome, i.e. reads the same from left to right and right to left.\n\nOne problem with prime numbers is that there are too many of them. Let's introduce the following notation: \u03c0(n)\u00a0\u2014 the number of primes no larger than n, rub(n)\u00a0\u2014 the number of palindromic numbers no larger than n. Rikhail wants to prove that there are a lot more primes than palindromic ones.\n\nHe asked you to solve the following problem: for a given value of the coefficient A find the maximum n, such that \u03c0(n) \u2264 A\u00b7rub(n).\n\n\n-----Input-----\n\nThe input consists of two positive integers p, q, the numerator and denominator of the fraction that is the value of A\u00a0($A = \\frac{p}{q}$,\u00a0$p, q \\leq 10^{4}, \\frac{1}{42} \\leq \\frac{p}{q} \\leq 42$).\n\n\n-----Output-----\n\nIf such maximum number exists, then print it. Otherwise, print \"Palindromic tree is better than splay tree\" (without the quotes).\n\n\n-----Examples-----\nInput\n1 1\n\nOutput\n40\n\nInput\n1 42\n\nOutput\n1\n\nInput\n6 4\n\nOutput\n172"} +{"id": "02951", "text": "\u00c6sir - CHAOS \u00c6sir - V.\n\n\"Everything has been planned out. No more hidden concerns. The condition of Cytus is also perfect.\n\nThe time right now...... 00:01:12......\n\nIt's time.\"\n\nThe emotion samples are now sufficient. After almost 3 years, it's time for Ivy to awake her bonded sister, Vanessa.\n\nThe system inside A.R.C.'s Library core can be considered as an undirected graph with infinite number of processing nodes, numbered with all positive integers ($1, 2, 3, \\ldots$). The node with a number $x$ ($x > 1$), is directly connected with a node with number $\\frac{x}{f(x)}$, with $f(x)$ being the lowest prime divisor of $x$.\n\nVanessa's mind is divided into $n$ fragments. Due to more than 500 years of coma, the fragments have been scattered: the $i$-th fragment is now located at the node with a number $k_i!$ (a factorial of $k_i$).\n\nTo maximize the chance of successful awakening, Ivy decides to place the samples in a node $P$, so that the total length of paths from each fragment to $P$ is smallest possible. If there are multiple fragments located at the same node, the path from that node to $P$ needs to be counted multiple times.\n\nIn the world of zeros and ones, such a requirement is very simple for Ivy. Not longer than a second later, she has already figured out such a node.\n\nBut for a mere human like you, is this still possible?\n\nFor simplicity, please answer the minimal sum of paths' lengths from every fragment to the emotion samples' assembly node $P$.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 10^6$)\u00a0\u2014 number of fragments of Vanessa's mind.\n\nThe second line contains $n$ integers: $k_1, k_2, \\ldots, k_n$ ($0 \\le k_i \\le 5000$), denoting the nodes where fragments of Vanessa's mind are located: the $i$-th fragment is at the node with a number $k_i!$.\n\n\n-----Output-----\n\nPrint a single integer, denoting the minimal sum of path from every fragment to the node with the emotion samples (a.k.a. node $P$).\n\nAs a reminder, if there are multiple fragments at the same node, the distance from that node to $P$ needs to be counted multiple times as well.\n\n\n-----Examples-----\nInput\n3\n2 1 4\n\nOutput\n5\n\nInput\n4\n3 1 4 4\n\nOutput\n6\n\nInput\n4\n3 1 4 1\n\nOutput\n6\n\nInput\n5\n3 1 4 1 5\n\nOutput\n11\n\n\n\n-----Note-----\n\nConsidering the first $24$ nodes of the system, the node network will look as follows (the nodes $1!$, $2!$, $3!$, $4!$ are drawn bold):\n\n[Image]\n\nFor the first example, Ivy will place the emotion samples at the node $1$. From here:\n\n The distance from Vanessa's first fragment to the node $1$ is $1$. The distance from Vanessa's second fragment to the node $1$ is $0$. The distance from Vanessa's third fragment to the node $1$ is $4$. \n\nThe total length is $5$.\n\nFor the second example, the assembly node will be $6$. From here:\n\n The distance from Vanessa's first fragment to the node $6$ is $0$. The distance from Vanessa's second fragment to the node $6$ is $2$. The distance from Vanessa's third fragment to the node $6$ is $2$. The distance from Vanessa's fourth fragment to the node $6$ is again $2$. \n\nThe total path length is $6$."} +{"id": "02952", "text": "Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a friend of Iahub, managed to get hold of the problem before the contest. Because he wants to make sure Iahub will be the one qualified, he tells Iahub the following task.\n\nYou're given an (1-based) array a with n elements. Let's define function f(i, j) (1 \u2264 i, j \u2264 n) as (i - j)^2 + g(i, j)^2. Function g is calculated by the following pseudo-code:\n\n\n\nint g(int i, int j) {\n\n int sum = 0;\n\n for (int k = min(i, j) + 1; k <= max(i, j); k = k + 1)\n\n sum = sum + a[k];\n\n return sum;\n\n}\n\n\n\nFind a value min_{i} \u2260 j\u00a0\u00a0f(i, j).\n\nProbably by now Iahub already figured out the solution to this problem. Can you?\n\n\n-----Input-----\n\nThe first line of input contains a single integer n (2 \u2264 n \u2264 100000). Next line contains n integers a[1], a[2], ..., a[n] ( - 10^4 \u2264 a[i] \u2264 10^4). \n\n\n-----Output-----\n\nOutput a single integer \u2014 the value of min_{i} \u2260 j\u00a0\u00a0f(i, j).\n\n\n-----Examples-----\nInput\n4\n1 0 0 -1\n\nOutput\n1\n\nInput\n2\n1 -1\n\nOutput\n2"} +{"id": "02953", "text": "You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined $E = \\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$. You have to perform exactly k_1 operations on array A and exactly k_2 operations on array B. In one operation, you have to choose one element of the array and increase or decrease it by 1.\n\nOutput the minimum possible value of error after k_1 operations on array A and k_2 operations on array B have been performed.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers n (1 \u2264 n \u2264 10^3), k_1 and k_2 (0 \u2264 k_1 + k_2 \u2264 10^3, k_1 and k_2 are non-negative) \u2014 size of arrays and number of operations to perform on A and B respectively.\n\nSecond line contains n space separated integers a_1, a_2, ..., a_{n} ( - 10^6 \u2264 a_{i} \u2264 10^6) \u2014 array A.\n\nThird line contains n space separated integers b_1, b_2, ..., b_{n} ( - 10^6 \u2264 b_{i} \u2264 10^6)\u2014 array B.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the minimum possible value of $\\sum_{i = 1}^{n}(a_{i} - b_{i})^{2}$ after doing exactly k_1 operations on array A and exactly k_2 operations on array B.\n\n\n-----Examples-----\nInput\n2 0 0\n1 2\n2 3\n\nOutput\n2\nInput\n2 1 0\n1 2\n2 2\n\nOutput\n0\nInput\n2 5 7\n3 4\n14 4\n\nOutput\n1\n\n\n-----Note-----\n\nIn the first sample case, we cannot perform any operations on A or B. Therefore the minimum possible error E = (1 - 2)^2 + (2 - 3)^2 = 2. \n\nIn the second sample case, we are required to perform exactly one operation on A. In order to minimize error, we increment the first element of A by 1. Now, A = [2, 2]. The error is now E = (2 - 2)^2 + (2 - 2)^2 = 0. This is the minimum possible error obtainable.\n\nIn the third sample case, we can increase the first element of A to 8, using the all of the 5 moves available to us. Also, the first element of B can be reduced to 8 using the 6 of the 7 available moves. Now A = [8, 4] and B = [8, 4]. The error is now E = (8 - 8)^2 + (4 - 4)^2 = 0, but we are still left with 1 move for array B. Increasing the second element of B to 5 using the left move, we get B = [8, 5] and E = (8 - 8)^2 + (4 - 5)^2 = 1."} +{"id": "02954", "text": "There is a string S of length N consisting of characters 0 and 1. You will perform the following operation for each i = 1, 2, ..., m:\n - Arbitrarily permute the characters within the substring of S starting at the l_i-th character from the left and extending through the r_i-th character.\nHere, the sequence l_i is non-decreasing.\nHow many values are possible for S after the M operations, modulo 1000000007(= 10^9+7)?\n\n-----Constraints-----\n - 2\u2266N\u22663000\n - 1\u2266M\u22663000\n - S consists of characters 0 and 1.\n - The length of S equals N.\n - 1\u2266l_i < r_i\u2266N\n - l_i \u2266 l_{i+1}\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN M\nS\nl_1 r_1\n:\nl_M r_M\n\n-----Output-----\nPrint the number of the possible values for S after the M operations, modulo 1000000007.\n\n-----Sample Input-----\n5 2\n01001\n2 4\n3 5\n\n-----Sample Output-----\n6\n\nAfter the first operation, S can be one of the following three: 01001, 00101 and 00011.\nAfter the second operation, S can be one of the following six: 01100, 01010, 01001, 00011, 00101 and 00110."} +{"id": "02955", "text": "Gerald got a very curious hexagon for his birthday. The boy found out that all the angles of the hexagon are equal to $120^{\\circ}$. Then he measured the length of its sides, and found that each of them is equal to an integer number of centimeters. There the properties of the hexagon ended and Gerald decided to draw on it.\n\nHe painted a few lines, parallel to the sides of the hexagon. The lines split the hexagon into regular triangles with sides of 1 centimeter. Now Gerald wonders how many triangles he has got. But there were so many of them that Gerald lost the track of his counting. Help the boy count the triangles.\n\n\n-----Input-----\n\nThe first and the single line of the input contains 6 space-separated integers a_1, a_2, a_3, a_4, a_5 and a_6 (1 \u2264 a_{i} \u2264 1000) \u2014 the lengths of the sides of the hexagons in centimeters in the clockwise order. It is guaranteed that the hexagon with the indicated properties and the exactly such sides exists.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of triangles with the sides of one 1 centimeter, into which the hexagon is split.\n\n\n-----Examples-----\nInput\n1 1 1 1 1 1\n\nOutput\n6\n\nInput\n1 2 1 2 1 2\n\nOutput\n13\n\n\n\n-----Note-----\n\nThis is what Gerald's hexagon looks like in the first sample:\n\n$\\theta$\n\nAnd that's what it looks like in the second sample:\n\n$A$"} +{"id": "02956", "text": "Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a_1, a_2, ..., a_{n} are good.\n\nNow she is interested in good sequences. A sequence x_1, x_2, ..., x_{k} is called good if it satisfies the following three conditions: The sequence is strictly increasing, i.e. x_{i} < x_{i} + 1 for each i (1 \u2264 i \u2264 k - 1). No two adjacent elements are coprime, i.e. gcd(x_{i}, x_{i} + 1) > 1 for each i (1 \u2264 i \u2264 k - 1) (where gcd(p, q) denotes the greatest common divisor of the integers p and q). All elements of the sequence are good integers. \n\nFind the length of the longest good sequence.\n\n\n-----Input-----\n\nThe input consists of two lines. The first line contains a single integer n (1 \u2264 n \u2264 10^5) \u2014 the number of good integers. The second line contains a single-space separated list of good integers a_1, a_2, ..., a_{n} in strictly increasing order (1 \u2264 a_{i} \u2264 10^5;\u00a0a_{i} < a_{i} + 1).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the length of the longest good sequence.\n\n\n-----Examples-----\nInput\n5\n2 3 4 6 9\n\nOutput\n4\n\nInput\n9\n1 2 3 5 6 7 8 9 10\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4."} +{"id": "02957", "text": "You are an intergalactic surgeon and you have an alien patient. For the purposes of this problem, we can and we will model this patient's body using a $2 \\times (2k + 1)$ rectangular grid. The alien has $4k + 1$ distinct organs, numbered $1$ to $4k + 1$.\n\nIn healthy such aliens, the organs are arranged in a particular way. For example, here is how the organs of a healthy such alien would be positioned, when viewed from the top, for $k = 4$: [Image] \n\nHere, the E represents empty space. \n\nIn general, the first row contains organs $1$ to $2k + 1$ (in that order from left to right), and the second row contains organs $2k + 2$ to $4k + 1$ (in that order from left to right) and then empty space right after. \n\nYour patient's organs are complete, and inside their body, but they somehow got shuffled around! Your job, as an intergalactic surgeon, is to put everything back in its correct position. All organs of the alien must be in its body during the entire procedure. This means that at any point during the procedure, there is exactly one cell (in the grid) that is empty. In addition, you can only move organs around by doing one of the following things: You can switch the positions of the empty space E with any organ to its immediate left or to its immediate right (if they exist). In reality, you do this by sliding the organ in question to the empty space; You can switch the positions of the empty space E with any organ to its immediate top or its immediate bottom (if they exist) only if the empty space is on the leftmost column, rightmost column or in the centermost column. Again, you do this by sliding the organ in question to the empty space. \n\nYour job is to figure out a sequence of moves you must do during the surgical procedure in order to place back all $4k + 1$ internal organs of your patient in the correct cells. If it is impossible to do so, you must say so.\n\n\n-----Input-----\n\nThe first line of input contains a single integer $t$ ($1 \\le t \\le 4$) denoting the number of test cases. The next lines contain descriptions of the test cases.\n\nEach test case consists of three lines. The first line contains a single integer $k$ ($1 \\le k \\le 15$) which determines the size of the grid. Then two lines follow. Each of them contains $2k + 1$ space-separated integers or the letter E. They describe the first and second rows of organs, respectively. It is guaranteed that all $4k + 1$ organs are present and there is exactly one E.\n\n\n-----Output-----\n\nFor each test case, first, print a single line containing either: SURGERY COMPLETE if it is possible to place back all internal organs in the correct locations; SURGERY FAILED if it is impossible. \n\nIf it is impossible, then this is the only line of output for the test case. However, if it is possible, output a few more lines describing the sequence of moves to place the organs in the correct locations. \n\nThe sequence of moves will be a (possibly empty) string of letters u, d, l or r, representing sliding the organ that's directly above, below, to the left or to the right of the empty space, respectively, into the empty space. Print the sequence of moves in the following line, as such a string. \n\nFor convenience, you may use shortcuts to reduce the size of your output. You may use uppercase letters as shortcuts for sequences of moves. For example, you could choose T to represent the string lddrr. These shortcuts may also include other shortcuts on their own! For example, you could choose E to represent TruT, etc.\n\nYou may use any number of uppercase letters (including none) as shortcuts. The only requirements are the following: The total length of all strings in your output for a single case is at most $10^4$; There must be no cycles involving the shortcuts that are reachable from the main sequence; The resulting sequence of moves is finite, after expanding all shortcuts. Note that the final sequence of moves (after expanding) may be much longer than $10^4$; the only requirement is that it's finite. \n\nAs an example, if T = lddrr, E = TruT and R = rrr, then TurTlER expands to: TurTlER lddrrurTlER lddrrurlddrrlER lddrrurlddrrlTruTR lddrrurlddrrllddrrruTR lddrrurlddrrllddrrrulddrrR lddrrurlddrrllddrrrulddrrrrr \n\nTo use shortcuts, print each one of them in a single line as the uppercase letter, then space, and then the string that this shortcut represents. They may be printed in any order. At the end of all of those, print a single line containing DONE. \n\nNote: You still need to print DONE even if you don't plan on using shortcuts.\n\nYour sequence does not need to be the shortest. Any valid sequence of moves (satisfying the requirements above) will be accepted.\n\n\n-----Example-----\nInput\n2\n3\n1 2 3 5 6 E 7\n8 9 10 4 11 12 13\n11\n34 45 6 22 16 43 38 44 5 4 41 14 7 29 28 19 9 18 42 8 17 33 1\nE 15 40 36 31 24 10 2 21 11 32 23 30 27 35 25 13 12 39 37 26 20 3\n\nOutput\nSURGERY COMPLETE\nIR\nR SrS\nS rr\nI lldll\nDONE\nSURGERY FAILED\n\n\n\n-----Note-----\n\nThere are three shortcuts defined in the first sample output: R = SrS S = rr I = lldll \n\nThe sequence of moves is IR and it expands to: IR lldllR lldllSrS lldllrrrS lldllrrrrr"} +{"id": "02958", "text": "For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:\n\n $f(i, j) = \\left\\{\\begin{array}{ll}{P [ i ]} & {\\text{if} j = 1} \\\\{f(P [ i ], j - 1)} & {\\text{otherwise}} \\end{array} \\right.$ \n\nLet g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.\n\nFor given N, A, B, find a permutation P of integers from 1 to N such that for 1 \u2264 i \u2264 N, g(i) equals either A or B.\n\n\n-----Input-----\n\nThe only line contains three integers N, A, B (1 \u2264 N \u2264 10^6, 1 \u2264 A, B \u2264 N).\n\n\n-----Output-----\n\nIf no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.\n\n\n-----Examples-----\nInput\n9 2 5\n\nOutput\n6 5 8 3 4 1 9 2 7\nInput\n3 2 1\n\nOutput\n1 2 3 \n\n\n-----Note-----\n\nIn the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5 \n\nIn the second example, g(1) = g(2) = g(3) = 1"} +{"id": "02959", "text": "Enough is enough. Too many times it happened that Vasya forgot to dispose of garbage and his apartment stank afterwards. Now he wants to create a garbage disposal plan and stick to it.\n\nFor each of next $n$ days Vasya knows $a_i$ \u2014 number of units of garbage he will produce on the $i$-th day. Each unit of garbage must be disposed of either on the day it was produced or on the next day. Vasya disposes of garbage by putting it inside a bag and dropping the bag into a garbage container. Each bag can contain up to $k$ units of garbage. It is allowed to compose and drop multiple bags into a garbage container in a single day.\n\nBeing economical, Vasya wants to use as few bags as possible. You are to compute the minimum number of bags Vasya needs to dispose of all of his garbage for the given $n$ days. No garbage should be left after the $n$-th day.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n \\le 2\\cdot10^5, 1 \\le k \\le 10^9$) \u2014 number of days to consider and bag's capacity. The second line contains $n$ space separated integers $a_i$ ($0 \\le a_i \\le 10^9$) \u2014 the number of units of garbage produced on the $i$-th day.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the minimum number of bags Vasya needs to dispose of all garbage. Each unit of garbage should be disposed on the day it was produced or on the next day. No garbage can be left after the $n$-th day. In a day it is allowed to compose and drop multiple bags.\n\n\n-----Examples-----\nInput\n3 2\n3 2 1\n\nOutput\n3\n\nInput\n5 1\n1000000000 1000000000 1000000000 1000000000 1000000000\n\nOutput\n5000000000\n\nInput\n3 2\n1 0 1\n\nOutput\n2\n\nInput\n4 4\n2 8 4 1\n\nOutput\n4"} +{"id": "02960", "text": "Is there anything better than going to the zoo after a tiresome week at work? No wonder Grisha feels the same while spending the entire weekend accompanied by pretty striped zebras. \n\nInspired by this adventure and an accidentally found plasticine pack (represented as a sequence of black and white stripes), Grisha now wants to select several consequent (contiguous) pieces of alternating colors to create a zebra. Let's call the number of selected pieces the length of the zebra.\n\nBefore assembling the zebra Grisha can make the following operation $0$ or more times. He splits the sequence in some place into two parts, then reverses each of them and sticks them together again. For example, if Grisha has pieces in the order \"bwbbw\" (here 'b' denotes a black strip, and 'w' denotes a white strip), then he can split the sequence as bw|bbw (here the vertical bar represents the cut), reverse both parts and obtain \"wbwbb\".\n\nDetermine the maximum possible length of the zebra that Grisha can produce.\n\n\n-----Input-----\n\nThe only line contains a string $s$ ($1 \\le |s| \\le 10^5$, where $|s|$ denotes the length of the string $s$) comprised of lowercase English letters 'b' and 'w' only, where 'w' denotes a white piece and 'b' denotes a black piece.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum possible zebra length.\n\n\n-----Examples-----\nInput\nbwwwbwwbw\n\nOutput\n5\n\nInput\nbwwbwwb\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example one of the possible sequence of operations is bwwwbww|bw $\\to$ w|wbwwwbwb $\\to$ wbwbwwwbw, that gives the answer equal to $5$.\n\nIn the second example no operation can increase the answer."} +{"id": "02961", "text": "You are playing some computer game. One of its levels puts you in a maze consisting of n lines, each of which contains m cells. Each cell either is free or is occupied by an obstacle. The starting cell is in the row r and column c. In one step you can move one square up, left, down or right, if the target cell is not occupied by an obstacle. You can't move beyond the boundaries of the labyrinth.\n\nUnfortunately, your keyboard is about to break, so you can move left no more than x times and move right no more than y times. There are no restrictions on the number of moves up and down since the keys used to move up and down are in perfect condition.\n\nNow you would like to determine for each cell whether there exists a sequence of moves that will put you from the starting cell to this particular one. How many cells of the board have this property?\n\n\n-----Input-----\n\nThe first line contains two integers n, m (1 \u2264 n, m \u2264 2000)\u00a0\u2014 the number of rows and the number columns in the labyrinth respectively.\n\nThe second line contains two integers r, c (1 \u2264 r \u2264 n, 1 \u2264 c \u2264 m)\u00a0\u2014 index of the row and index of the column that define the starting cell.\n\nThe third line contains two integers x, y (0 \u2264 x, y \u2264 10^9)\u00a0\u2014 the maximum allowed number of movements to the left and to the right respectively.\n\nThe next n lines describe the labyrinth. Each of them has length of m and consists only of symbols '.' and '*'. The j-th character of the i-th line corresponds to the cell of labyrinth at row i and column j. Symbol '.' denotes the free cell, while symbol '*' denotes the cell with an obstacle.\n\nIt is guaranteed, that the starting cell contains no obstacles.\n\n\n-----Output-----\n\nPrint exactly one integer\u00a0\u2014 the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself.\n\n\n-----Examples-----\nInput\n4 5\n3 2\n1 2\n.....\n.***.\n...**\n*....\n\nOutput\n10\n\nInput\n4 4\n2 2\n0 1\n....\n..*.\n....\n....\n\nOutput\n7\n\n\n\n-----Note-----\n\nCells, reachable in the corresponding example, are marked with '+'.\n\nFirst example: \n\n+++..\n\n+***.\n\n+++**\n\n*+++.\n\n \n\nSecond example: \n\n.++.\n\n.+*.\n\n.++.\n\n.++."} +{"id": "02962", "text": "You have a set of $n$ weights. You know that their masses are $a_1$, $a_2$, ..., $a_n$ grams, but you don't know which of them has which mass. You can't distinguish the weights.\n\nHowever, your friend does know the mass of each weight. You can ask your friend to give you exactly $k$ weights with the total mass $m$ (both parameters $k$ and $m$ are chosen by you), and your friend will point to any valid subset of weights, if it is possible.\n\nYou are allowed to make this query only once. Find the maximum possible number of weights you can reveal after this query.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 100$)\u00a0\u2014 the number of weights.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 100$)\u00a0\u2014 the masses of the weights.\n\n\n-----Output-----\n\nPrint the maximum number of weights you can learn the masses for after making a single query.\n\n\n-----Examples-----\nInput\n4\n1 4 2 2\n\nOutput\n2\n\nInput\n6\n1 2 4 4 4 9\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example we can ask for a subset of two weights with total mass being equal to $4$, and the only option is to get $\\{2, 2\\}$.\n\nAnother way to obtain the same result is to ask for a subset of two weights with the total mass of $5$ and get $\\{1, 4\\}$. It is easy to see that the two remaining weights have mass of $2$ grams each.\n\nIn the second example we can ask for a subset of two weights with total mass being $8$, and the only answer is $\\{4, 4\\}$. We can prove it is not possible to learn masses for three weights in one query, but we won't put the proof here."} +{"id": "02963", "text": "Suppose you are given a string $s$ of length $n$ consisting of lowercase English letters. You need to compress it using the smallest possible number of coins.\n\nTo compress the string, you have to represent $s$ as a concatenation of several non-empty strings: $s = t_{1} t_{2} \\ldots t_{k}$. The $i$-th of these strings should be encoded with one of the two ways: if $|t_{i}| = 1$, meaning that the current string consists of a single character, you can encode it paying $a$ coins; if $t_{i}$ is a substring of $t_{1} t_{2} \\ldots t_{i - 1}$, then you can encode it paying $b$ coins. \n\nA string $x$ is a substring of a string $y$ if $x$ can be obtained from $y$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.\n\nSo your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string $s$.\n\n\n-----Input-----\n\nThe first line contains three positive integers, separated by spaces: $n$, $a$ and $b$ ($1 \\leq n, a, b \\leq 5000$)\u00a0\u2014 the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before.\n\nThe second line contains a single string $s$, consisting of $n$ lowercase English letters.\n\n\n-----Output-----\n\nOutput a single integer \u2014 the smallest possible number of coins you need to spend to compress $s$.\n\n\n-----Examples-----\nInput\n3 3 1\naba\n\nOutput\n7\n\nInput\n4 1 1\nabcd\n\nOutput\n4\n\nInput\n4 10 1\naaaa\n\nOutput\n12\n\n\n\n-----Note-----\n\nIn the first sample case, you can set $t_{1} =$ 'a', $t_{2} =$ 'b', $t_{3} =$ 'a' and pay $3 + 3 + 1 = 7$ coins, since $t_{3}$ is a substring of $t_{1}t_{2}$.\n\nIn the second sample, you just need to compress every character by itself.\n\nIn the third sample, you set $t_{1} = t_{2} =$ 'a', $t_{3} =$ 'aa' and pay $10 + 1 + 1 = 12$ coins, since $t_{2}$ is a substring of $t_{1}$ and $t_{3}$ is a substring of $t_{1} t_{2}$."} +{"id": "02964", "text": "Snuke has decided to play with N cards and a deque (that is, a double-ended queue).\nEach card shows an integer from 1 through N, and the deque is initially empty.\nSnuke will insert the cards at the beginning or the end of the deque one at a time, in order from 1 to N.\nThen, he will perform the following action N times: take out the card from the beginning or the end of the deque and eat it.\nAfterwards, we will construct an integer sequence by arranging the integers written on the eaten cards, in the order they are eaten. Among the sequences that can be obtained in this way, find the number of the sequences such that the K-th element is 1. Print the answer modulo 10^{9} + 7.\n\n-----Constraints-----\n - 1 \u2266 K \u2266 N \u2266 2{,}000\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN K\n\n-----Output-----\nPrint the answer modulo 10^{9} + 7.\n\n-----Sample Input-----\n2 1\n\n-----Sample Output-----\n1\n\nThere is one sequence satisfying the condition: 1,2. One possible way to obtain this sequence is the following:\n - Insert both cards, 1 and 2, at the end of the deque.\n - Eat the card at the beginning of the deque twice."} +{"id": "02965", "text": "Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value a_{i}.\n\nMolly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of k. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment.\n\nHelp her to do so in finding the total number of such segments.\n\n\n-----Input-----\n\nThe first line of input contains two integers, n and k, the number of chemicals and the number, such that the total affection value is a non-negative power of this number k. (1 \u2264 n \u2264 10^5, 1 \u2264 |k| \u2264 10).\n\nNext line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 affection values of chemicals.\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the number of valid segments.\n\n\n-----Examples-----\nInput\n4 2\n2 2 2 2\n\nOutput\n8\n\nInput\n4 -3\n3 -6 -3 12\n\nOutput\n3\n\n\n\n-----Note-----\n\nDo keep in mind that k^0 = 1.\n\nIn the first sample, Molly can get following different affection values: 2: segments [1, 1], [2, 2], [3, 3], [4, 4];\n\n 4: segments [1, 2], [2, 3], [3, 4];\n\n 6: segments [1, 3], [2, 4];\n\n 8: segments [1, 4]. \n\nOut of these, 2, 4 and 8 are powers of k = 2. Therefore, the answer is 8.\n\nIn the second sample, Molly can choose segments [1, 2], [3, 3], [3, 4]."} +{"id": "02966", "text": "In the evening Polycarp decided to analyze his today's travel expenses on public transport.\n\nThe bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop to the other and back. There is at most one bus running between a pair of stops.\n\nPolycarp made n trips on buses. About each trip the stop where he started the trip and the the stop where he finished are known. The trips follow in the chronological order in Polycarp's notes.\n\nIt is known that one trip on any bus costs a burles. In case when passenger makes a transshipment the cost of trip decreases to b burles (b < a). A passenger makes a transshipment if the stop on which he boards the bus coincides with the stop where he left the previous bus. Obviously, the first trip can not be made with transshipment.\n\nFor example, if Polycarp made three consecutive trips: \"BerBank\" $\\rightarrow$ \"University\", \"University\" $\\rightarrow$ \"BerMall\", \"University\" $\\rightarrow$ \"BerBank\", then he payed a + b + a = 2a + b burles. From the BerBank he arrived to the University, where he made transshipment to the other bus and departed to the BerMall. Then he walked to the University and returned to the BerBank by bus.\n\nAlso Polycarp can buy no more than k travel cards. Each travel card costs f burles. The travel card for a single bus route makes free of charge any trip by this route (in both directions). Once purchased, a travel card can be used any number of times in any direction.\n\nWhat is the smallest amount of money Polycarp could have spent today if he can buy no more than k travel cards?\n\n\n-----Input-----\n\nThe first line contains five integers n, a, b, k, f (1 \u2264 n \u2264 300, 1 \u2264 b < a \u2264 100, 0 \u2264 k \u2264 300, 1 \u2264 f \u2264 1000) where: n \u2014 the number of Polycarp trips, a \u2014 the cost of a regualar single trip, b \u2014 the cost of a trip after a transshipment, k \u2014 the maximum number of travel cards Polycarp can buy, f \u2014 the cost of a single travel card. \n\nThe following n lines describe the trips in the chronological order. Each line contains exactly two different words separated by a single space \u2014 the name of the start stop and the name of the finish stop of the trip. All names consist of uppercase and lowercase English letters and have lengths between 1 to 20 letters inclusive. Uppercase and lowercase letters should be considered different.\n\n\n-----Output-----\n\nPrint the smallest amount of money Polycarp could have spent today, if he can purchase no more than k travel cards.\n\n\n-----Examples-----\nInput\n3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank\n\nOutput\n11\n\nInput\n4 2 1 300 1000\na A\nA aa\naa AA\nAA a\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example Polycarp can buy travel card for the route \"BerBank $\\leftrightarrow$ University\" and spend 8 burles. Note that his second trip \"University\" $\\rightarrow$ \"BerMall\" was made after transshipment, so for this trip Polycarp payed 3 burles. So the minimum total sum equals to 8 + 3 = 11 burles.\n\nIn the second example it doesn't make sense to buy travel cards. Note that each of Polycarp trip (except the first) was made with transshipment. So the minimum total sum equals to 2 + 1 + 1 + 1 = 5 burles."} +{"id": "02967", "text": "Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food. \n\nIahub asks Iahubina: can you build a rooted tree, such that\n\n each internal node (a node with at least one son) has at least two sons; node i has c_{i} nodes in its subtree? \n\nIahubina has to guess the tree. Being a smart girl, she realized that it's possible no tree can follow Iahub's restrictions. In this way, Iahub will eat all the food. You need to help Iahubina: determine if there's at least one tree following Iahub's restrictions. The required tree must contain n nodes.\n\n\n-----Input-----\n\nThe first line of the input contains integer n (1 \u2264 n \u2264 24). Next line contains n positive integers: the i-th number represents c_{i} (1 \u2264 c_{i} \u2264 n).\n\n\n-----Output-----\n\nOutput on the first line \"YES\" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output \"NO\" (without quotes). \n\n\n-----Examples-----\nInput\n4\n1 1 1 4\n\nOutput\nYES\nInput\n5\n1 1 5 2 1\n\nOutput\nNO"} +{"id": "02968", "text": "Vasya came up with his own weather forecasting method. He knows the information about the average air temperature for each of the last n days. Assume that the average air temperature for each day is integral.\n\nVasya believes that if the average temperatures over the last n days form an arithmetic progression, where the first term equals to the average temperature on the first day, the second term equals to the average temperature on the second day and so on, then the average temperature of the next (n + 1)-th day will be equal to the next term of the arithmetic progression. Otherwise, according to Vasya's method, the temperature of the (n + 1)-th day will be equal to the temperature of the n-th day.\n\nYour task is to help Vasya predict the average temperature for tomorrow, i. e. for the (n + 1)-th day.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 100) \u2014 the number of days for which the average air temperature is known.\n\nThe second line contains a sequence of integers t_1, t_2, ..., t_{n} ( - 1000 \u2264 t_{i} \u2264 1000)\u00a0\u2014 where t_{i} is the average temperature in the i-th day.\n\n\n-----Output-----\n\nPrint the average air temperature in the (n + 1)-th day, which Vasya predicts according to his method. Note that the absolute value of the predicted temperature can exceed 1000.\n\n\n-----Examples-----\nInput\n5\n10 5 0 -5 -10\n\nOutput\n-15\n\nInput\n4\n1 1 1 1\n\nOutput\n1\n\nInput\n3\n5 1 -5\n\nOutput\n-5\n\nInput\n2\n900 1000\n\nOutput\n1100\n\n\n\n-----Note-----\n\nIn the first example the sequence of the average temperatures is an arithmetic progression where the first term is 10 and each following terms decreases by 5. So the predicted average temperature for the sixth day is - 10 - 5 = - 15.\n\nIn the second example the sequence of the average temperatures is an arithmetic progression where the first term is 1 and each following terms equals to the previous one. So the predicted average temperature in the fifth day is 1.\n\nIn the third example the average temperatures do not form an arithmetic progression, so the average temperature of the fourth day equals to the temperature of the third day and equals to - 5.\n\nIn the fourth example the sequence of the average temperatures is an arithmetic progression where the first term is 900 and each the following terms increase by 100. So predicted average temperature in the third day is 1000 + 100 = 1100."} +{"id": "02969", "text": "Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.\n\nYou are given a tree with $n$ nodes. In the beginning, $0$ is written on all edges. In one operation, you can choose any $2$ distinct leaves $u$, $v$ and any real number $x$ and add $x$ to values written on all edges on the simple path between $u$ and $v$.\n\nFor example, on the picture below you can see the result of applying two operations to the graph: adding $2$ on the path from $7$ to $6$, and then adding $-0.5$ on the path from $4$ to $5$. \n\n [Image] \n\nIs it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?\n\nLeaf is a node of a tree of degree $1$. Simple path is a path that doesn't contain any node twice.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 10^5$)\u00a0\u2014 the number of nodes.\n\nEach of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \\le u, v \\le n$, $u \\neq v$), meaning that there is an edge between nodes $u$ and $v$. It is guaranteed that these edges form a tree.\n\n\n-----Output-----\n\nIf there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output \"NO\". \n\nOtherwise, output \"YES\". \n\nYou can print each letter in any case (upper or lower).\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\nYES\nInput\n3\n1 2\n2 3\n\nOutput\nNO\nInput\n5\n1 2\n1 3\n1 4\n2 5\n\nOutput\nNO\nInput\n6\n1 2\n1 3\n1 4\n2 5\n2 6\n\nOutput\nYES\n\n\n-----Note-----\n\nIn the first example, we can add any real $x$ to the value written on the only edge $(1, 2)$.\n\n [Image] \n\nIn the second example, one of configurations that we can't reach is $0$ written on $(1, 2)$ and $1$ written on $(2, 3)$.\n\n $8$ \n\nBelow you can see graphs from examples $3$, $4$:\n\n [Image] \n\n [Image]"} +{"id": "02970", "text": "Boy Dima gave Julian a birthday present\u00a0\u2014 set $B$ consisting of positive integers. However, he didn't know, that Julian hates sets, but enjoys bipartite graphs more than anything else!\n\nJulian was almost upset, but her friend Alex said, that he can build an undirected graph using this set in such a way: let all integer numbers be vertices, then connect any two $i$ and $j$ with an edge if $|i - j|$ belongs to $B$.\n\nUnfortunately, Julian doesn't like the graph, that was built using $B$. Alex decided to rectify the situation, so he wants to erase some numbers from $B$, so that graph built using the new set is bipartite. The difficulty of this task is that the graph, Alex has to work with, has an infinite number of vertices and edges! It is impossible to solve this task alone, so Alex asks you for help. Write a program that erases a subset of minimum size from $B$ so that graph constructed on the new set is bipartite.\n\nRecall, that graph is bipartite if all its vertices can be divided into two disjoint sets such that every edge connects a vertex from different sets.\n\n\n-----Input-----\n\nFirst line contains an integer $n ~ (1 \\leqslant n \\leqslant 200\\,000)$\u00a0\u2014 size of $B$\n\nSecond line contains $n$ integers $b_1, b_2, \\ldots, b_n ~ (1 \\leqslant b_i \\leqslant 10^{18})$\u00a0\u2014 numbers of $B$, all $b_i$ are unique\n\n\n-----Output-----\n\nIn the first line print single integer $k$ \u2013 the number of erased elements. In the second line print $k$ integers\u00a0\u2014 values of erased elements.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n1\n2 \nInput\n2\n2 6\n\nOutput\n0"} +{"id": "02971", "text": "We have a board with a 2 \\times N grid.\nSnuke covered the board with N dominoes without overlaps.\nHere, a domino can cover a 1 \\times 2 or 2 \\times 1 square.\nThen, Snuke decided to paint these dominoes using three colors: red, cyan and green.\nTwo dominoes that are adjacent by side should be painted by different colors.\nHere, it is not always necessary to use all three colors.\nFind the number of such ways to paint the dominoes, modulo 1000000007.\nThe arrangement of the dominoes is given to you as two strings S_1 and S_2 in the following manner:\n - Each domino is represented by a different English letter (lowercase or uppercase).\n - The j-th character in S_i represents the domino that occupies the square at the i-th row from the top and j-th column from the left.\n\n-----Constraints-----\n - 1 \\leq N \\leq 52\n - |S_1| = |S_2| = N\n - S_1 and S_2 consist of lowercase and uppercase English letters.\n - S_1 and S_2 represent a valid arrangement of dominoes.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS_1\nS_2\n\n-----Output-----\nPrint the number of such ways to paint the dominoes, modulo 1000000007.\n\n-----Sample Input-----\n3\naab\nccb\n\n-----Sample Output-----\n6\n\nThere are six ways as shown below:"} +{"id": "02972", "text": "Consider a table G of size n \u00d7 m such that G(i, j) = GCD(i, j) for all 1 \u2264 i \u2264 n, 1 \u2264 j \u2264 m. GCD(a, b) is the greatest common divisor of numbers a and b.\n\nYou have a sequence of positive integer numbers a_1, a_2, ..., a_{k}. We say that this sequence occurs in table G if it coincides with consecutive elements in some row, starting from some position. More formally, such numbers 1 \u2264 i \u2264 n and 1 \u2264 j \u2264 m - k + 1 should exist that G(i, j + l - 1) = a_{l} for all 1 \u2264 l \u2264 k.\n\nDetermine if the sequence a occurs in table G.\n\n\n-----Input-----\n\nThe first line contains three space-separated integers n, m and k (1 \u2264 n, m \u2264 10^12; 1 \u2264 k \u2264 10000). The second line contains k space-separated integers a_1, a_2, ..., a_{k} (1 \u2264 a_{i} \u2264 10^12).\n\n\n-----Output-----\n\nPrint a single word \"YES\", if the given sequence occurs in table G, otherwise print \"NO\".\n\n\n-----Examples-----\nInput\n100 100 5\n5 2 1 2 1\n\nOutput\nYES\n\nInput\n100 8 5\n5 2 1 2 1\n\nOutput\nNO\n\nInput\n100 100 7\n1 2 3 4 5 6 7\n\nOutput\nNO\n\n\n\n-----Note-----\n\nSample 1. The tenth row of table G starts from sequence {1, 2, 1, 2, 5, 2, 1, 2, 1, 10}. As you can see, elements from fifth to ninth coincide with sequence a.\n\nSample 2. This time the width of table G equals 8. Sequence a doesn't occur there."} +{"id": "02973", "text": "There is a square grid of size $n \\times n$. Some cells are colored in black, all others are colored in white. In one operation you can select some rectangle and color all its cells in white. It costs $\\min(h, w)$ to color a rectangle of size $h \\times w$. You are to make all cells white for minimum total cost.\n\nThe square is large, so we give it to you in a compressed way. The set of black cells is the union of $m$ rectangles.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 10^{9}$, $0 \\le m \\le 50$)\u00a0\u2014 the size of the square grid and the number of black rectangles.\n\nEach of the next $m$ lines contains 4 integers $x_{i1}$ $y_{i1}$ $x_{i2}$ $y_{i2}$ ($1 \\le x_{i1} \\le x_{i2} \\le n$, $1 \\le y_{i1} \\le y_{i2} \\le n$)\u00a0\u2014 the coordinates of the bottom-left and the top-right corner cells of the $i$-th black rectangle.\n\nThe rectangles may intersect.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the minimum total cost of painting the whole square in white.\n\n\n-----Examples-----\nInput\n10 2\n4 1 5 10\n1 4 10 5\n\nOutput\n4\n\nInput\n7 6\n2 1 2 1\n4 2 4 3\n2 5 2 5\n2 3 5 3\n1 2 1 2\n3 2 5 3\n\nOutput\n3\n\n\n\n-----Note-----\n\nThe examples and some of optimal solutions are shown on the pictures below.\n\n [Image]"} +{"id": "02974", "text": "Slime has a sequence of positive integers $a_1, a_2, \\ldots, a_n$.\n\nIn one operation Orac can choose an arbitrary subsegment $[l \\ldots r]$ of this sequence and replace all values $a_l, a_{l + 1}, \\ldots, a_r$ to the value of median of $\\{a_l, a_{l + 1}, \\ldots, a_r\\}$.\n\nIn this problem, for the integer multiset $s$, the median of $s$ is equal to the $\\lfloor \\frac{|s|+1}{2}\\rfloor$-th smallest number in it. For example, the median of $\\{1,4,4,6,5\\}$ is $4$, and the median of $\\{1,7,5,8\\}$ is $5$.\n\nSlime wants Orac to make $a_1 = a_2 = \\ldots = a_n = k$ using these operations.\n\nOrac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.\n\n\n-----Input-----\n\nThe first line of the input is a single integer $t$: the number of queries.\n\nThe first line of each query contains two integers $n\\ (1\\le n\\le 100\\,000)$ and $k\\ (1\\le k\\le 10^9)$, the second line contains $n$ positive integers $a_1,a_2,\\dots,a_n\\ (1\\le a_i\\le 10^9)$\n\nThe total sum of $n$ is at most $100\\,000$.\n\n\n-----Output-----\n\nThe output should contain $t$ lines. The $i$-th line should be equal to 'yes' if it is possible to make all integers $k$ in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.\n\n\n-----Example-----\nInput\n5\n5 3\n1 5 2 6 1\n1 6\n6\n3 2\n1 2 3\n4 3\n3 1 2 3\n10 3\n1 2 3 4 5 6 7 8 9 10\n\nOutput\nno\nyes\nyes\nno\nyes\n\n\n\n-----Note-----\n\nIn the first query, Orac can't turn all elements into $3$.\n\nIn the second query, $a_1=6$ is already satisfied.\n\nIn the third query, Orac can select the complete array and turn all elements into $2$.\n\nIn the fourth query, Orac can't turn all elements into $3$.\n\nIn the fifth query, Orac can select $[1,6]$ at first and then select $[2,10]$."} +{"id": "02975", "text": "Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special. \n\nAlyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers l_{i} and r_{i}, and its elements are a[l_{i}], a[l_{i} + 1], ..., a[r_{i}].\n\nAlyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible. \n\nYou are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.\n\nThe mex of a set S is a minimum possible non-negative integer that is not in S.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n, m \u2264 10^5).\n\nThe next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers l_{i} and r_{i} (1 \u2264 l_{i} \u2264 r_{i} \u2264 n), that describe the subarray a[l_{i}], a[l_{i} + 1], ..., a[r_{i}].\n\n\n-----Output-----\n\nIn the first line print single integer\u00a0\u2014 the maximum possible minimum mex.\n\nIn the second line print n integers\u00a0\u2014 the array a. All the elements in a should be between 0 and 10^9.\n\nIt is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 10^9.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n5 3\n1 3\n2 5\n4 5\n\nOutput\n2\n1 0 2 1 0\n\nInput\n4 2\n1 4\n2 4\n\nOutput\n3\n5 2 0 1\n\n\n-----Note-----\n\nThe first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2."} +{"id": "02976", "text": "Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are m switches. Each switch control doors of some rooms, but each door is controlled by exactly two switches.\n\nYou are given the initial configuration of the doors. Toggling any switch, that is, turning it ON when it is OFF, or turning it OFF when it is ON, toggles the condition of the doors that this switch controls. Say, we toggled switch 1, which was connected to room 1, 2 and 3 which were respectively locked, unlocked and unlocked. Then, after toggling the switch, they become unlocked, locked and locked.\n\nYou need to tell Sherlock, if there exists a way to unlock all doors at the same time.\n\n\n-----Input-----\n\nFirst line of input contains two integers n and m (2 \u2264 n \u2264 10^5, 2 \u2264 m \u2264 10^5)\u00a0\u2014 the number of rooms and the number of switches.\n\nNext line contains n space-separated integers r_1, r_2, ..., r_{n} (0 \u2264 r_{i} \u2264 1) which tell the status of room doors. The i-th room is locked if r_{i} = 0, otherwise it is unlocked.\n\nThe i-th of next m lines contains an integer x_{i} (0 \u2264 x_{i} \u2264 n) followed by x_{i} distinct integers separated by space, denoting the number of rooms controlled by the i-th switch followed by the room numbers that this switch controls. It is guaranteed that the room numbers are in the range from 1 to n. It is guaranteed that each door is controlled by exactly two switches.\n\n\n-----Output-----\n\nOutput \"YES\" without quotes, if it is possible to open all doors at the same time, otherwise output \"NO\" without quotes.\n\n\n-----Examples-----\nInput\n3 3\n1 0 1\n2 1 3\n2 1 2\n2 2 3\n\nOutput\nNO\nInput\n3 3\n1 0 1\n3 1 2 3\n1 2\n2 1 3\n\nOutput\nYES\nInput\n3 3\n1 0 1\n3 1 2 3\n2 1 2\n1 3\n\nOutput\nNO\n\n\n-----Note-----\n\nIn the second example input, the initial statuses of the doors are [1, 0, 1] (0 means locked, 1\u00a0\u2014 unlocked).\n\nAfter toggling switch 3, we get [0, 0, 0] that means all doors are locked.\n\nThen, after toggling switch 1, we get [1, 1, 1] that means all doors are unlocked.\n\nIt can be seen that for the first and for the third example inputs it is not possible to make all doors unlocked."} +{"id": "02977", "text": "Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful. \n\nBelow there was also written that a string is called beautiful if for each i (1 \u2264 i \u2264 |s|) there are no more ')' characters than '(' characters among the first i characters of s and also the total number of '(' characters is equal to the total number of ')' characters. \n\nHelp Malek open the door by telling him for each '#' character how many ')' characters he must replace it with.\n\n\n-----Input-----\n\nThe first line of the input contains a string s (1 \u2264 |s| \u2264 10^5). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that s contains at least one '#' character.\n\n\n-----Output-----\n\nIf there is no way of replacing '#' characters which leads to a beautiful string print - 1. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with.\n\nIf there are several possible answers, you may output any of them.\n\n\n-----Examples-----\nInput\n(((#)((#)\n\nOutput\n1\n2\n\nInput\n()((#((#(#()\n\nOutput\n2\n2\n1\nInput\n#\n\nOutput\n-1\n\nInput\n(#)\n\nOutput\n-1\n\n\n\n-----Note-----\n\n|s| denotes the length of the string s."} +{"id": "02978", "text": "User ainta loves to play with cards. He has a cards containing letter \"o\" and b cards containing letter \"x\". He arranges the cards in a row, and calculates the score of the deck by the formula below. At first, the score is 0. For each block of contiguous \"o\"s with length x the score increases by x^2. For each block of contiguous \"x\"s with length y the score decreases by y^2. \u00a0\n\nFor example, if a = 6, b = 3 and ainta have arranged the cards in the order, that is described by string \"ooxoooxxo\", the score of the deck equals 2^2 - 1^2 + 3^2 - 2^2 + 1^2 = 9. That is because the deck has 5 blocks in total: \"oo\", \"x\", \"ooo\", \"xx\", \"o\".\n\nUser ainta likes big numbers, so he wants to maximize the score with the given cards. Help ainta make the score as big as possible. Note, that he has to arrange all his cards.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers a and b (0 \u2264 a, b \u2264 10^5;\u00a0a + b \u2265 1) \u2014 the number of \"o\" cards and the number of \"x\" cards.\n\n\n-----Output-----\n\nIn the first line print a single integer v \u2014 the maximum score that ainta can obtain.\n\nIn the second line print a + b characters describing the deck. If the k-th card of the deck contains \"o\", the k-th character must be \"o\". If the k-th card of the deck contains \"x\", the k-th character must be \"x\". The number of \"o\" characters must be equal to a, and the number of \"x \" characters must be equal to b. If there are many ways to maximize v, print any.\n\nPlease, do not write the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.\n\n\n-----Examples-----\nInput\n2 3\n\nOutput\n-1\nxoxox\n\nInput\n4 0\n\nOutput\n16\noooo\nInput\n0 4\n\nOutput\n-16\nxxxx"} +{"id": "02979", "text": "Alice, Bob and Charlie are playing Card Game for Three, as below:\n - At first, each of the three players has a deck consisting of some number of cards. Alice's deck has N cards, Bob's deck has M cards, and Charlie's deck has K cards. Each card has a letter a, b or c written on it. The orders of the cards in the decks cannot be rearranged.\n - The players take turns. Alice goes first.\n - If the current player's deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says a, Alice takes the next turn.)\n - If the current player's deck is empty, the game ends and the current player wins the game.\nThere are 3^{N+M+K} possible patters of the three player's initial decks. Among these patterns, how many will lead to Alice's victory?\nSince the answer can be large, print the count modulo 1\\,000\\,000\\,007 (=10^9+7).\n\n-----Constraints-----\n - 1 \\leq N \\leq 3\u00d710^5\n - 1 \\leq M \\leq 3\u00d710^5\n - 1 \\leq K \\leq 3\u00d710^5\n\n-----Partial Scores-----\n - 500 points will be awarded for passing the test set satisfying the following: 1 \\leq N \\leq 1000, 1 \\leq M \\leq 1000, 1 \\leq K \\leq 1000.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN M K\n\n-----Output-----\nPrint the answer modulo 1\\,000\\,000\\,007 (=10^9+7).\n\n-----Sample Input-----\n1 1 1\n\n-----Sample Output-----\n17\n\n - If Alice's card is a, then Alice will win regardless of Bob's and Charlie's card. There are 3\u00d73=9 such patterns.\n - If Alice's card is b, Alice will only win when Bob's card is a, or when Bob's card is c and Charlie's card is a. There are 3+1=4 such patterns.\n - If Alice's card is c, Alice will only win when Charlie's card is a, or when Charlie's card is b and Bob's card is a. There are 3+1=4 such patterns.\nThus, there are total of 9+4+4=17 patterns that will lead to Alice's victory."} +{"id": "02980", "text": "Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by $n$ streets along the Eastern direction and $m$ streets across the Southern direction. Naturally, this city has $nm$ intersections. At any intersection of $i$-th Eastern street and $j$-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings.\n\nWhen Dora passes through the intersection of the $i$-th Eastern and $j$-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change.\n\nFormally, on every of $nm$ intersections Dora solves an independent problem. She sees $n + m - 1$ skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result \"greater\", \"smaller\" or \"equal\". Now Dora wants to select some integer $x$ and assign every skyscraper a height from $1$ to $x$. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible $x$.\n\nFor example, if the intersection and the two streets corresponding to it look as follows: [Image] \n\nThen it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons \"less\", \"equal\", \"greater\" inside the Eastern street and inside the Southern street are preserved) [Image] \n\nThe largest used number is $5$, hence the answer for this intersection would be $5$.\n\nHelp Dora to compute the answers for each intersection.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n, m \\le 1000$)\u00a0\u2014 the number of streets going in the Eastern direction and the number of the streets going in Southern direction.\n\nEach of the following $n$ lines contains $m$ integers $a_{i,1}$, $a_{i,2}$, ..., $a_{i,m}$ ($1 \\le a_{i,j} \\le 10^9$). The integer $a_{i,j}$, located on $j$-th position in the $i$-th line denotes the height of the skyscraper at the intersection of the $i$-th Eastern street and $j$-th Southern direction.\n\n\n-----Output-----\n\nPrint $n$ lines containing $m$ integers each. The integer $x_{i,j}$, located on $j$-th position inside the $i$-th line is an answer for the problem at the intersection of $i$-th Eastern street and $j$-th Southern street.\n\n\n-----Examples-----\nInput\n2 3\n1 2 1\n2 1 2\n\nOutput\n2 2 2 \n2 2 2 \n\nInput\n2 2\n1 2\n3 4\n\nOutput\n2 3 \n3 2 \n\n\n\n-----Note-----\n\nIn the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights.\n\nIn the second example, the answers are as follows: For the intersection of the first line and the first column [Image] For the intersection of the first line and the second column [Image] For the intersection of the second line and the first column [Image] For the intersection of the second line and the second column [Image]"} +{"id": "02981", "text": "The only difference between easy and hard versions is constraints.\n\nNauuo is a girl who loves random picture websites.\n\nOne day she made a random picture website by herself which includes $n$ pictures.\n\nWhen Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The $i$-th picture has a non-negative weight $w_i$, and the probability of the $i$-th picture being displayed is $\\frac{w_i}{\\sum_{j=1}^nw_j}$. That is to say, the probability of a picture to be displayed is proportional to its weight.\n\nHowever, Nauuo discovered that some pictures she does not like were displayed too often. \n\nTo solve this problem, she came up with a great idea: when she saw a picture she likes, she would add $1$ to its weight; otherwise, she would subtract $1$ from its weight.\n\nNauuo will visit the website $m$ times. She wants to know the expected weight of each picture after all the $m$ visits modulo $998244353$. Can you help her?\n\nThe expected weight of the $i$-th picture can be denoted by $\\frac {q_i} {p_i}$ where $\\gcd(p_i,q_i)=1$, you need to print an integer $r_i$ satisfying $0\\le r_i<998244353$ and $r_i\\cdot p_i\\equiv q_i\\pmod{998244353}$. It can be proved that such $r_i$ exists and is unique.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1\\le n\\le 2\\cdot 10^5$, $1\\le m\\le 3000$) \u2014 the number of pictures and the number of visits to the website.\n\nThe second line contains $n$ integers $a_1,a_2,\\ldots,a_n$ ($a_i$ is either $0$ or $1$) \u2014 if $a_i=0$ , Nauuo does not like the $i$-th picture; otherwise Nauuo likes the $i$-th picture. It is guaranteed that there is at least one picture which Nauuo likes.\n\nThe third line contains $n$ positive integers $w_1,w_2,\\ldots,w_n$ ($w_i \\geq 1$) \u2014 the initial weights of the pictures. It is guaranteed that the sum of all the initial weights does not exceed $998244352-m$.\n\n\n-----Output-----\n\nThe output contains $n$ integers $r_1,r_2,\\ldots,r_n$ \u2014 the expected weights modulo $998244353$.\n\n\n-----Examples-----\nInput\n2 1\n0 1\n2 1\n\nOutput\n332748119\n332748119\n\nInput\n1 2\n1\n1\n\nOutput\n3\n\nInput\n3 3\n0 1 1\n4 3 5\n\nOutput\n160955686\n185138929\n974061117\n\n\n\n-----Note-----\n\nIn the first example, if the only visit shows the first picture with a probability of $\\frac 2 3$, the final weights are $(1,1)$; if the only visit shows the second picture with a probability of $\\frac1 3$, the final weights are $(2,2)$.\n\nSo, both expected weights are $\\frac2 3\\cdot 1+\\frac 1 3\\cdot 2=\\frac4 3$ .\n\nBecause $332748119\\cdot 3\\equiv 4\\pmod{998244353}$, you need to print $332748119$ instead of $\\frac4 3$ or $1.3333333333$.\n\nIn the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, $w_1$ will be increased by $1$.\n\nSo, the expected weight is $1+2=3$.\n\nNauuo is very naughty so she didn't give you any hint of the third example."} +{"id": "02982", "text": "Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. \n\nAfter the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 5\u00b710^5) \u2014 the number of elements in the array. The next line contains n integers a_{i} (1 \u2264 a_{i} \u2264 10^6) \u2014 the values of the array elements.\n\n\n-----Output-----\n\nIn a single line print a single integer \u2014 the maximum number of points Artem can get.\n\n\n-----Examples-----\nInput\n5\n3 1 5 2 6\n\nOutput\n11\n\nInput\n5\n1 2 3 4 5\n\nOutput\n6\n\nInput\n5\n1 100 101 100 1\n\nOutput\n102"} +{"id": "02983", "text": "Someone give a strange birthday present to Ivan. It is hedgehog\u00a0\u2014 connected undirected graph in which one vertex has degree at least $3$ (we will call it center) and all other vertices has degree 1. Ivan thought that hedgehog is too boring and decided to make himself $k$-multihedgehog.\n\nLet us define $k$-multihedgehog as follows: $1$-multihedgehog is hedgehog: it has one vertex of degree at least $3$ and some vertices of degree 1. For all $k \\ge 2$, $k$-multihedgehog is $(k-1)$-multihedgehog in which the following changes has been made for each vertex $v$ with degree 1: let $u$ be its only neighbor; remove vertex $v$, create a new hedgehog with center at vertex $w$ and connect vertices $u$ and $w$ with an edge. New hedgehogs can differ from each other and the initial gift. \n\nThereby $k$-multihedgehog is a tree. Ivan made $k$-multihedgehog but he is not sure that he did not make any mistakes. That is why he asked you to check if his tree is indeed $k$-multihedgehog.\n\n\n-----Input-----\n\nFirst line of input contains $2$ integers $n$, $k$ ($1 \\le n \\le 10^{5}$, $1 \\le k \\le 10^{9}$)\u00a0\u2014 number of vertices and hedgehog parameter.\n\nNext $n-1$ lines contains two integers $u$ $v$ ($1 \\le u, \\,\\, v \\le n; \\,\\, u \\ne v$)\u00a0\u2014 indices of vertices connected by edge.\n\nIt is guaranteed that given graph is a tree.\n\n\n-----Output-----\n\nPrint \"Yes\" (without quotes), if given graph is $k$-multihedgehog, and \"No\" (without quotes) otherwise.\n\n\n-----Examples-----\nInput\n14 2\n1 4\n2 4\n3 4\n4 13\n10 5\n11 5\n12 5\n14 5\n5 13\n6 7\n8 6\n13 6\n9 6\n\nOutput\nYes\n\nInput\n3 1\n1 3\n2 3\n\nOutput\nNo\n\n\n\n-----Note-----\n\n2-multihedgehog from the first example looks like this:\n\n[Image]\n\nIts center is vertex $13$. Hedgehogs created on last step are: [4 (center), 1, 2, 3], [6 (center), 7, 8, 9], [5 (center), 10, 11, 12, 13].\n\nTree from second example is not a hedgehog because degree of center should be at least $3$."} +{"id": "02984", "text": "A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exist since real magnets have two poles, but this is a programming contest problem, so we don't care.\n\nThere is an $n\\times m$ grid. Initially, you may place some north magnets and some south magnets into the cells. You are allowed to place as many magnets as you like, even multiple in the same cell.\n\nAn operation is performed as follows. Choose a north magnet and a south magnet to activate. If they are in the same row or the same column and they occupy different cells, then the north magnet moves one unit closer to the south magnet. Otherwise, if they occupy the same cell or do not share a row or column, then nothing changes. Note that the south magnets are immovable.\n\nEach cell of the grid is colored black or white. Let's consider ways to place magnets in the cells so that the following conditions are met.\n\n There is at least one south magnet in every row and every column. If a cell is colored black, then it is possible for a north magnet to occupy this cell after some sequence of operations from the initial placement. If a cell is colored white, then it is impossible for a north magnet to occupy this cell after some sequence of operations from the initial placement. \n\nDetermine if it is possible to place magnets such that these conditions are met. If it is possible, find the minimum number of north magnets required (there are no requirements on the number of south magnets).\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1\\le n,m\\le 1000$) \u00a0\u2014 the number of rows and the number of columns, respectively.\n\nThe next $n$ lines describe the coloring. The $i$-th of these lines contains a string of length $m$, where the $j$-th character denotes the color of the cell in row $i$ and column $j$. The characters \"#\" and \".\" represent black and white, respectively. It is guaranteed, that the string will not contain any other characters.\n\n\n-----Output-----\n\nOutput a single integer, the minimum possible number of north magnets required.\n\nIf there is no placement of magnets that satisfies all conditions, print a single integer $-1$.\n\n\n-----Examples-----\nInput\n3 3\n.#.\n###\n##.\n\nOutput\n1\n\nInput\n4 2\n##\n.#\n.#\n##\n\nOutput\n-1\n\nInput\n4 5\n....#\n####.\n.###.\n.#...\n\nOutput\n2\n\nInput\n2 1\n.\n#\n\nOutput\n-1\n\nInput\n3 5\n.....\n.....\n.....\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first test, here is an example placement of magnets:\n\n [Image] \n\nIn the second test, we can show that no required placement of magnets exists. Here are three example placements that fail to meet the requirements. The first example violates rule $3$ since we can move the north magnet down onto a white square. The second example violates rule $2$ since we cannot move the north magnet to the bottom-left black square by any sequence of operations. The third example violates rule $1$ since there is no south magnet in the first column.\n\n [Image] \n\nIn the third test, here is an example placement of magnets. We can show that there is no required placement of magnets with fewer north magnets.\n\n [Image] \n\nIn the fourth test, we can show that no required placement of magnets exists. Here are two example placements that fail to meet the requirements. The first example violates rule $1$ since there is no south magnet in the first row. The second example violates rules $1$ and $3$ since there is no south magnet in the second row and we can move the north magnet up one unit onto a white square.\n\n [Image] \n\nIn the fifth test, we can put the south magnet in each cell and no north magnets. Because there are no black cells, it will be a correct placement."} +{"id": "02985", "text": "Initially there was an array $a$ consisting of $n$ integers. Positions in it are numbered from $1$ to $n$.\n\nExactly $q$ queries were performed on the array. During the $i$-th query some segment $(l_i, r_i)$ $(1 \\le l_i \\le r_i \\le n)$ was selected and values of elements on positions from $l_i$ to $r_i$ inclusive got changed to $i$. The order of the queries couldn't be changed and all $q$ queries were applied. It is also known that every position from $1$ to $n$ got covered by at least one segment.\n\nWe could have offered you the problem about checking if some given array (consisting of $n$ integers with values from $1$ to $q$) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.\n\nSo the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to $0$.\n\nYour task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.\n\nIf there are multiple possible arrays then print any of them.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $q$ ($1 \\le n, q \\le 2 \\cdot 10^5$) \u2014 the number of elements of the array and the number of queries perfomed on it.\n\nThe second line contains $n$ integer numbers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le q$) \u2014 the resulting array. If element at some position $j$ is equal to $0$ then the value of element at this position can be any integer from $1$ to $q$.\n\n\n-----Output-----\n\nPrint \"YES\" if the array $a$ can be obtained by performing $q$ queries. Segments $(l_i, r_i)$ $(1 \\le l_i \\le r_i \\le n)$ are chosen separately for each query. Every position from $1$ to $n$ should be covered by at least one segment. \n\nOtherwise print \"NO\".\n\nIf some array can be obtained then print $n$ integers on the second line \u2014 the $i$-th number should be equal to the $i$-th element of the resulting array and should have value from $1$ to $q$. This array should be obtainable by performing exactly $q$ queries.\n\nIf there are multiple possible arrays then print any of them.\n\n\n-----Examples-----\nInput\n4 3\n1 0 2 3\n\nOutput\nYES\n1 2 2 3\n\nInput\n3 10\n10 10 10\n\nOutput\nYES\n10 10 10 \n\nInput\n5 6\n6 5 6 2 2\n\nOutput\nNO\n\nInput\n3 5\n0 0 0\n\nOutput\nYES\n5 4 2\n\n\n\n-----Note-----\n\nIn the first example you can also replace $0$ with $1$ but not with $3$.\n\nIn the second example it doesn't really matter what segments to choose until query $10$ when the segment is $(1, 3)$.\n\nThe third example showcases the fact that the order of queries can't be changed, you can't firstly set $(1, 3)$ to $6$ and after that change $(2, 2)$ to $5$. The segment of $5$ should be applied before segment of $6$.\n\nThere is a lot of correct resulting arrays for the fourth example."} +{"id": "02986", "text": "The GCD table G of size n \u00d7 n for an array of positive integers a of length n is defined by formula $g_{ij} = \\operatorname{gcd}(a_{i}, a_{j})$ \n\nLet us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both x and y, it is denoted as $\\operatorname{gcd}(x, y)$. For example, for array a = {4, 3, 6, 2} of length 4 the GCD table will look as follows: [Image] \n\nGiven all the numbers of the GCD table G, restore array a.\n\n\n-----Input-----\n\nThe first line contains number n (1 \u2264 n \u2264 500) \u2014 the length of array a. The second line contains n^2 space-separated numbers \u2014 the elements of the GCD table of G for array a. \n\nAll the numbers in the table are positive integers, not exceeding 10^9. Note that the elements are given in an arbitrary order. It is guaranteed that the set of the input data corresponds to some array a.\n\n\n-----Output-----\n\nIn the single line print n positive integers \u2014 the elements of array a. If there are multiple possible solutions, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n4\n2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2\n\nOutput\n4 3 6 2\nInput\n1\n42\n\nOutput\n42 \nInput\n2\n1 1 1 1\n\nOutput\n1 1"} +{"id": "02987", "text": "There are $n$ points on the plane, the $i$-th of which is at $(x_i, y_i)$. Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area.\n\nThe strange area is enclosed by three lines, $x = l$, $y = a$ and $x = r$, as its left side, its bottom side and its right side respectively, where $l$, $r$ and $a$ can be any real numbers satisfying that $l < r$. The upper side of the area is boundless, which you can regard as a line parallel to the $x$-axis at infinity. The following figure shows a strange rectangular area. [Image] \n\nA point $(x_i, y_i)$ is in the strange rectangular area if and only if $l < x_i < r$ and $y_i > a$. For example, in the above figure, $p_1$ is in the area while $p_2$ is not.\n\nTokitsukaze wants to know how many different non-empty sets she can obtain by picking all the points in a strange rectangular area, where we think two sets are different if there exists at least one point in one set of them but not in the other.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\leq n \\leq 2 \\times 10^5$)\u00a0\u2014 the number of points on the plane.\n\nThe $i$-th of the next $n$ lines contains two integers $x_i$, $y_i$ ($1 \\leq x_i, y_i \\leq 10^9$)\u00a0\u2014 the coordinates of the $i$-th point.\n\nAll points are distinct.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of different non-empty sets of points she can obtain.\n\n\n-----Examples-----\nInput\n3\n1 1\n1 2\n1 3\n\nOutput\n3\n\nInput\n3\n1 1\n2 1\n3 1\n\nOutput\n6\n\nInput\n4\n2 1\n2 2\n3 1\n3 2\n\nOutput\n6\n\n\n\n-----Note-----\n\nFor the first example, there is exactly one set having $k$ points for $k = 1, 2, 3$, so the total number is $3$.\n\nFor the second example, the numbers of sets having $k$ points for $k = 1, 2, 3$ are $3$, $2$, $1$ respectively, and their sum is $6$.\n\nFor the third example, as the following figure shows, there are $2$ sets having one point; $3$ sets having two points; $1$ set having four points. \n\nTherefore, the number of different non-empty sets in this example is $2 + 3 + 0 + 1 = 6$. [Image]"} +{"id": "02988", "text": "You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an n \u00d7 n grid. The rows are numbered 1 through n from top to bottom, and the columns are numbered 1 through n from left to right. At the far side of the room lies a door locked with evil magical forces. The following inscriptions are written on the door: The cleaning of all evil will awaken the door! \n\nBeing a very senior adventurer, you immediately realize what this means. You notice that every single cell in the grid are initially evil. You should purify all of these cells.\n\nThe only method of tile purification known to you is by casting the \"Purification\" spell. You cast this spell on a single tile \u2014 then, all cells that are located in the same row and all cells that are located in the same column as the selected tile become purified (including the selected tile)! It is allowed to purify a cell more than once.\n\nYou would like to purify all n \u00d7 n cells while minimizing the number of times you cast the \"Purification\" spell. This sounds very easy, but you just noticed that some tiles are particularly more evil than the other tiles. You cannot cast the \"Purification\" spell on those particularly more evil tiles, not even after they have been purified. They can still be purified if a cell sharing the same row or the same column gets selected by the \"Purification\" spell.\n\nPlease find some way to purify all the cells with the minimum number of spells cast. Print -1 if there is no such way.\n\n\n-----Input-----\n\nThe first line will contain a single integer n (1 \u2264 n \u2264 100). Then, n lines follows, each contains n characters. The j-th character in the i-th row represents the cell located at row i and column j. It will be the character 'E' if it is a particularly more evil cell, and '.' otherwise.\n\n\n-----Output-----\n\nIf there exists no way to purify all the cells, output -1. Otherwise, if your solution casts x \"Purification\" spells (where x is the minimum possible number of spells), output x lines. Each line should consist of two integers denoting the row and column numbers of the cell on which you should cast the \"Purification\" spell.\n\n\n-----Examples-----\nInput\n3\n.E.\nE.E\n.E.\n\nOutput\n1 1\n2 2\n3 3\n\nInput\n3\nEEE\nE..\nE.E\n\nOutput\n-1\n\nInput\n5\nEE.EE\nE.EE.\nE...E\n.EE.E\nEE.EE\n\nOutput\n3 3\n1 3\n2 2\n4 4\n5 3\n\n\n-----Note-----\n\nThe first example is illustrated as follows. Purple tiles are evil tiles that have not yet been purified. Red tile is the tile on which \"Purification\" is cast. Yellow tiles are the tiles being purified as a result of the current \"Purification\" spell. Green tiles are tiles that have been purified previously. [Image] \n\nIn the second example, it is impossible to purify the cell located at row 1 and column 1.\n\nFor the third example: [Image]"} +{"id": "02989", "text": "As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation:\n\n$f(a, l, r) = \\sum_{i = l}^{r} a [ i ] ; m(a) = \\operatorname{max}_{1 \\leq l \\leq r \\leq n} f(a, l, r)$\n\nA swap operation is the following sequence of actions:\n\n choose two indexes i, j (i \u2260 j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. \n\nWhat maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations?\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 200;\u00a01 \u2264 k \u2264 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 \u2264 a[i] \u2264 1000).\n\n\n-----Output-----\n\nIn a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations.\n\n\n-----Examples-----\nInput\n10 2\n10 -1 2 2 2 2 2 2 -1 10\n\nOutput\n32\n\nInput\n5 10\n-1 -1 -1 -1 -1\n\nOutput\n-1"} +{"id": "02990", "text": "You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. \n\nFind the maximum possible value of [Image] after performing at most k operations optimally.\n\n\n-----Input-----\n\nThe first line contains three integers n, k and x (1 \u2264 n \u2264 200 000, 1 \u2264 k \u2264 10, 2 \u2264 x \u2264 8).\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 10^9).\n\n\n-----Output-----\n\nOutput the maximum value of a bitwise OR of sequence elements after performing operations.\n\n\n-----Examples-----\nInput\n3 1 2\n1 1 1\n\nOutput\n3\n\nInput\n4 2 3\n1 2 4 8\n\nOutput\n79\n\n\n\n-----Note-----\n\nFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. \n\nFor the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result."} +{"id": "02991", "text": "Takahashi throws N dice, each having K sides with all integers from 1 to K. The dice are NOT pairwise distinguishable.\nFor each i=2,3,...,2K, find the following value modulo 998244353:\n - The number of combinations of N sides shown by the dice such that the sum of no two different sides is i.\nNote that the dice are NOT distinguishable, that is, two combinations are considered different when there exists an integer k such that the number of dice showing k is different in those two.\n\n-----Constraints-----\n - 1 \\leq K \\leq 2000\n - 2 \\leq N \\leq 2000\n - K and N are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK N\n\n-----Output-----\nPrint 2K-1 integers. The t-th of them (1\\leq t\\leq 2K-1) should be the answer for i=t+1.\n\n-----Sample Input-----\n3 3\n\n-----Sample Output-----\n7\n7\n4\n7\n7\n\n - For i=2, the combinations (1,2,2),(1,2,3),(1,3,3),(2,2,2),(2,2,3),(2,3,3),(3,3,3) satisfy the condition, so the answer is 7.\n - For i=3, the combinations (1,1,1),(1,1,3),(1,3,3),(2,2,2),(2,2,3),(2,3,3),(3,3,3) satisfy the condition, so the answer is 7.\n - For i=4, the combinations (1,1,1),(1,1,2),(2,3,3),(3,3,3) satisfy the condition, so the answer is 4."} +{"id": "02992", "text": "Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town. \n\nIn Treeland there are 2k universities which are located in different towns. \n\nRecently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done! \n\nTo have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible. \n\nHelp the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1. \n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (2 \u2264 n \u2264 200 000, 1 \u2264 k \u2264 n / 2)\u00a0\u2014 the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n. \n\nThe second line contains 2k distinct integers u_1, u_2, ..., u_2k (1 \u2264 u_{i} \u2264 n)\u00a0\u2014 indices of towns in which universities are located. \n\nThe next n - 1 line contains the description of roads. Each line contains the pair of integers x_{j} and y_{j} (1 \u2264 x_{j}, y_{j} \u2264 n), which means that the j-th road connects towns x_{j} and y_{j}. All of them are two-way roads. You can move from any town to any other using only these roads. \n\n\n-----Output-----\n\nPrint the maximum possible sum of distances in the division of universities into k pairs.\n\n\n-----Examples-----\nInput\n7 2\n1 5 6 2\n1 3\n3 2\n4 5\n3 7\n4 3\n4 6\n\nOutput\n6\n\nInput\n9 3\n3 2 1 6 5 9\n8 9\n3 2\n2 7\n3 4\n7 6\n4 5\n2 1\n2 8\n\nOutput\n9\n\n\n\n-----Note-----\n\nThe figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example. \n\n [Image]"} +{"id": "02993", "text": "Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.\n\nOleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days. \n\n\n-----Input-----\n\nIn the only line of input data there is a non-empty string s consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as |s|) does not exceed 200 000 characters.\n\n\n-----Output-----\n\nIf there is a way to divide history into zebra subsequences, in the first line of output you should print an integer k (1 \u2264 k \u2264 |s|), the resulting number of subsequences. In the i-th of following k lines first print the integer l_{i} (1 \u2264 l_{i} \u2264 |s|), which is the length of the i-th subsequence, and then l_{i} indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to n must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.\n\nSubsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of k.\n\n\n-----Examples-----\nInput\n0010100\n\nOutput\n3\n3 1 3 4\n3 2 5 6\n1 7\n\nInput\n111\n\nOutput\n-1"} +{"id": "02994", "text": "It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has n gyms. The i-th gym has g_{i} Pokemon in it. There are m distinct Pokemon types in the Himalayan region numbered from 1 to m. There is a special evolution camp set up in the fest which claims to evolve any Pokemon. The type of a Pokemon could change after evolving, subject to the constraint that if two Pokemon have the same type before evolving, they will have the same type after evolving. Also, if two Pokemon have different types before evolving, they will have different types after evolving. It is also possible that a Pokemon has the same type before and after evolving. \n\nFormally, an evolution plan is a permutation f of {1, 2, ..., m}, such that f(x) = y means that a Pokemon of type x evolves into a Pokemon of type y.\n\nThe gym leaders are intrigued by the special evolution camp and all of them plan to evolve their Pokemons. The protocol of the mountain states that in each gym, for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. They now want to find out how many distinct evolution plans exist which satisfy the protocol.\n\nTwo evolution plans f_1 and f_2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon type in the two plans, i. e. there exists an i such that f_1(i) \u2260 f_2(i).\n\nYour task is to find how many distinct evolution plans are possible such that if all Pokemon in all the gyms are evolved, the number of Pokemon of each type in each of the gyms remains the same. As the answer can be large, output it modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (1 \u2264 n \u2264 10^5, 1 \u2264 m \u2264 10^6)\u00a0\u2014 the number of gyms and the number of Pokemon types.\n\nThe next n lines contain the description of Pokemons in the gyms. The i-th of these lines begins with the integer g_{i} (1 \u2264 g_{i} \u2264 10^5)\u00a0\u2014 the number of Pokemon in the i-th gym. After that g_{i} integers follow, denoting types of the Pokemons in the i-th gym. Each of these integers is between 1 and m.\n\nThe total number of Pokemons (the sum of all g_{i}) does not exceed 5\u00b710^5.\n\n\n-----Output-----\n\nOutput the number of valid evolution plans modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n2 3\n2 1 2\n2 2 3\n\nOutput\n1\n\nInput\n1 3\n3 1 2 3\n\nOutput\n6\n\nInput\n2 4\n2 1 2\n3 2 3 4\n\nOutput\n2\n\nInput\n2 2\n3 2 2 1\n2 1 2\n\nOutput\n1\n\nInput\n3 7\n2 1 2\n2 3 4\n3 5 6 7\n\nOutput\n24\n\n\n\n-----Note-----\n\nIn the first case, the only possible evolution plan is: $1 \\rightarrow 1,2 \\rightarrow 2,3 \\rightarrow 3$\n\nIn the second case, any permutation of (1, 2, 3) is valid.\n\nIn the third case, there are two possible plans: $1 \\rightarrow 1,2 \\rightarrow 2,3 \\rightarrow 4,4 \\rightarrow 3$ $1 \\rightarrow 1,2 \\rightarrow 2,3 \\rightarrow 3,4 \\rightarrow 4$\n\nIn the fourth case, the only possible evolution plan is: $1 \\rightarrow 1,2 \\rightarrow 2$"} +{"id": "02995", "text": "Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \\sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\\cdot(- 1)^{i - l}$\n\nIn the above formula, 1 \u2264 l < r \u2264 n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.\n\n\n-----Input-----\n\nThe first line contains single integer n (2 \u2264 n \u2264 10^5)\u00a0\u2014 the size of the array a.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (-10^9 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the array elements.\n\n\n-----Output-----\n\nPrint the only integer\u00a0\u2014 the maximum value of f.\n\n\n-----Examples-----\nInput\n5\n1 4 2 3 1\n\nOutput\n3\nInput\n4\n1 5 4 7\n\nOutput\n6\n\n\n-----Note-----\n\nIn the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].\n\nIn the second case maximal value of f is reachable only on the whole array."} +{"id": "02996", "text": "One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.\n\nThe maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (1 \u2264 i \u2264 n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number p_{i}, where 1 \u2264 p_{i} \u2264 i.\n\nIn order not to get lost, Vasya decided to act as follows. Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1. Let's assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room p_{i}), otherwise Vasya uses the first portal. \n\nHelp Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^3)\u00a0\u2014 the number of rooms. The second line contains n integers p_{i} (1 \u2264 p_{i} \u2264 i). Each p_{i} denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (10^9 + 7).\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n4\n\nInput\n4\n1 1 2 3\n\nOutput\n20\n\nInput\n5\n1 1 1 1 1\n\nOutput\n62"} +{"id": "02997", "text": "You invited $n$ guests to dinner! You plan to arrange one or more circles of chairs. Each chair is going to be either occupied by one guest, or be empty. You can make any number of circles. \n\nYour guests happen to be a little bit shy, so the $i$-th guest wants to have a least $l_i$ free chairs to the left of his chair, and at least $r_i$ free chairs to the right. The \"left\" and \"right\" directions are chosen assuming all guests are going to be seated towards the center of the circle. Note that when a guest is the only one in his circle, the $l_i$ chairs to his left and $r_i$ chairs to his right may overlap.\n\nWhat is smallest total number of chairs you have to use?\n\n\n-----Input-----\n\nFirst line contains one integer $n$ \u00a0\u2014 number of guests, ($1 \\leqslant n \\leqslant 10^5$). \n\nNext $n$ lines contain $n$ pairs of space-separated integers $l_i$ and $r_i$ ($0 \\leqslant l_i, r_i \\leqslant 10^9$).\n\n\n-----Output-----\n\nOutput a single integer\u00a0\u2014 the smallest number of chairs you have to use.\n\n\n-----Examples-----\nInput\n3\n1 1\n1 1\n1 1\n\nOutput\n6\n\nInput\n4\n1 2\n2 1\n3 5\n5 3\n\nOutput\n15\n\nInput\n1\n5 6\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the second sample the only optimal answer is to use two circles: a circle with $5$ chairs accomodating guests $1$ and $2$, and another one with $10$ chairs accomodationg guests $3$ and $4$.\n\nIn the third sample, you have only one circle with one person. The guest should have at least five free chairs to his left, and at least six free chairs to his right to the next person, which is in this case the guest herself. So, overall number of chairs should be at least 6+1=7."} +{"id": "02998", "text": "There are n types of coins in Byteland. Conveniently, the denomination of the coin type k divides the denomination of the coin type k + 1, the denomination of the coin type 1 equals 1 tugrick. The ratio of the denominations of coin types k + 1 and k equals a_{k}. It is known that for each x there are at most 20 coin types of denomination x.\n\nByteasar has b_{k} coins of type k with him, and he needs to pay exactly m tugricks. It is known that Byteasar never has more than 3\u00b710^5 coins with him. Byteasar want to know how many ways there are to pay exactly m tugricks. Two ways are different if there is an integer k such that the amount of coins of type k differs in these two ways. As all Byteland citizens, Byteasar wants to know the number of ways modulo 10^9 + 7.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 3\u00b710^5)\u00a0\u2014 the number of coin types.\n\nThe second line contains n - 1 integers a_1, a_2, ..., a_{n} - 1 (1 \u2264 a_{k} \u2264 10^9)\u00a0\u2014 the ratios between the coin types denominations. It is guaranteed that for each x there are at most 20 coin types of denomination x.\n\nThe third line contains n non-negative integers b_1, b_2, ..., b_{n}\u00a0\u2014 the number of coins of each type Byteasar has. It is guaranteed that the sum of these integers doesn't exceed 3\u00b710^5.\n\nThe fourth line contains single integer m (0 \u2264 m < 10^10000)\u00a0\u2014 the amount in tugricks Byteasar needs to pay.\n\n\n-----Output-----\n\nPrint single integer\u00a0\u2014 the number of ways to pay exactly m tugricks modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n1\n\n4\n2\n\nOutput\n1\n\nInput\n2\n1\n4 4\n2\n\nOutput\n3\n\nInput\n3\n3 3\n10 10 10\n17\n\nOutput\n6\n\n\n\n-----Note-----\n\nIn the first example Byteasar has 4 coins of denomination 1, and he has to pay 2 tugricks. There is only one way.\n\nIn the second example Byteasar has 4 coins of each of two different types of denomination 1, he has to pay 2 tugricks. There are 3 ways: pay one coin of the first type and one coin of the other, pay two coins of the first type, and pay two coins of the second type.\n\nIn the third example the denominations are equal to 1, 3, 9."} +{"id": "02999", "text": "Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly a_{i} bugs in every line of code that he writes. \n\nLet's call a sequence of non-negative integers v_1, v_2, ..., v_{n} a plan, if v_1 + v_2 + ... + v_{n} = m. The programmers follow the plan like that: in the beginning the first programmer writes the first v_1 lines of the given task, then the second programmer writes v_2 more lines of the given task, and so on. In the end, the last programmer writes the remaining lines of the code. Let's call a plan good, if all the written lines of the task contain at most b bugs in total.\n\nYour task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integer mod.\n\n\n-----Input-----\n\nThe first line contains four integers n, m, b, mod (1 \u2264 n, m \u2264 500, 0 \u2264 b \u2264 500; 1 \u2264 mod \u2264 10^9 + 7)\u00a0\u2014 the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer.\n\nThe next line contains n space-separated integers a_1, a_2, ..., a_{n} (0 \u2264 a_{i} \u2264 500)\u00a0\u2014 the number of bugs per line for each programmer.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the answer to the problem modulo mod.\n\n\n-----Examples-----\nInput\n3 3 3 100\n1 1 1\n\nOutput\n10\n\nInput\n3 6 5 1000000007\n1 2 3\n\nOutput\n0\n\nInput\n3 5 6 11\n1 2 1\n\nOutput\n0"} +{"id": "03000", "text": "You are given a text consisting of n lines. Each line contains some space-separated words, consisting of lowercase English letters.\n\nWe define a syllable as a string that contains exactly one vowel and any arbitrary number (possibly none) of consonants. In English alphabet following letters are considered to be vowels: 'a', 'e', 'i', 'o', 'u' and 'y'.\n\nEach word of the text that contains at least one vowel can be divided into syllables. Each character should be a part of exactly one syllable. For example, the word \"mamma\" can be divided into syllables as \"ma\" and \"mma\", \"mam\" and \"ma\", and \"mamm\" and \"a\". Words that consist of only consonants should be ignored.\n\nThe verse patterns for the given text is a sequence of n integers p_1, p_2, ..., p_{n}. Text matches the given verse pattern if for each i from 1 to n one can divide words of the i-th line in syllables in such a way that the total number of syllables is equal to p_{i}.\n\nYou are given the text and the verse pattern. Check, if the given text matches the given verse pattern.\n\n\n-----Input-----\n\nThe first line of the input contains a single integer n (1 \u2264 n \u2264 100)\u00a0\u2014 the number of lines in the text.\n\nThe second line contains integers p_1, ..., p_{n} (0 \u2264 p_{i} \u2264 100)\u00a0\u2014 the verse pattern.\n\nNext n lines contain the text itself. Text consists of lowercase English letters and spaces. It's guaranteed that all lines are non-empty, each line starts and ends with a letter and words are separated by exactly one space. The length of each line doesn't exceed 100 characters.\n\n\n-----Output-----\n\nIf the given text matches the given verse pattern, then print \"YES\" (without quotes) in the only line of the output. Otherwise, print \"NO\" (without quotes).\n\n\n-----Examples-----\nInput\n3\n2 2 3\nintel\ncode\nch allenge\n\nOutput\nYES\n\nInput\n4\n1 2 3 1\na\nbcdefghi\njklmnopqrstu\nvwxyz\n\nOutput\nNO\n\nInput\n4\n13 11 15 15\nto be or not to be that is the question\nwhether tis nobler in the mind to suffer\nthe slings and arrows of outrageous fortune\nor to take arms against a sea of troubles\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first sample, one can split words into syllables in the following way: in-tel\n\nco-de\n\nch al-len-ge\n\n\n\nSince the word \"ch\" in the third line doesn't contain vowels, we can ignore it. As the result we get 2 syllabels in first two lines and 3 syllables in the third one."} +{"id": "03001", "text": "Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks: Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman. Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman. \n\nAfter guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?\n\n\n-----Input-----\n\nThe first line contains a single integer n (1 \u2264 n \u2264 3\u00b710^5). The second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^6) \u2014 the initial group that is given to Toastman.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the largest possible score.\n\n\n-----Examples-----\nInput\n3\n3 1 5\n\nOutput\n26\n\nInput\n1\n10\n\nOutput\n10\n\n\n\n-----Note-----\n\nConsider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions."} +{"id": "03002", "text": "Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is a_{i} meters high. \n\nVladimir has just planted n bamboos in a row, each of which has height 0 meters right now, but they grow 1 meter each day. In order to make the partition nice Vladimir can cut each bamboo once at any height (no greater that the height of the bamboo), and then the bamboo will stop growing.\n\nVladimir wants to check the bamboos each d days (i.e. d days after he planted, then after 2d days and so on), and cut the bamboos that reached the required height. Vladimir wants the total length of bamboo parts he will cut off to be no greater than k meters.\n\nWhat is the maximum value d he can choose so that he can achieve what he wants without cutting off more than k meters of bamboo?\n\n\n-----Input-----\n\nThe first line contains two integers n and k (1 \u2264 n \u2264 100, 1 \u2264 k \u2264 10^11)\u00a0\u2014 the number of bamboos and the maximum total length of cut parts, in meters.\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the required heights of bamboos, in meters.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum value of d such that Vladimir can reach his goal.\n\n\n-----Examples-----\nInput\n3 4\n1 3 5\n\nOutput\n3\n\nInput\n3 40\n10 30 50\n\nOutput\n32\n\n\n\n-----Note-----\n\nIn the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters."} +{"id": "03003", "text": "A deadly virus is sweeping across the globe! You are part of an elite group of programmers tasked with tracking the spread of the virus. You have been given the timestamps of people entering and exiting a room. Now your job is to determine how the virus will spread. Hurry up, time is of the essence!\n\nThere are $N$ people in this scenario, numbered from $1$ to $N$. Every day, the $i$th person enters and exits the room at time $s_ i$ and $t_ i$ respectively. At the beginning of the $1$st day, there are $C$ people infected with the virus, whose indices will be given in the input.\n\nWhen an infected person comes into contact with an uninfected person, the uninfected person will become infected at the start of the next day. Hence, they can only start spreading the infection from the next day onward. An infected person will always remain infected.\n\nTwo people are in contact with each other if they are in the room at the same time. This includes cases where one person leaves and another person enters at the exact same time. Also, if a person enters and exits the room at the same time ($s_ i$ = $t_ i$), they are considered to be in contact with everyone in the room at that time. Due to safe-distancing requirements, no more than $50$ people can be in the room at one time.\n\nYour job is to print the indices of the people who will be infected after $D$ days. This includes people who came into contact with an infected person on the $D$th day but will only become infected at the start of the $D+1$th day.\n\nGiven below are the visualizations for sample inputs $1$ and $2$.\n\nNote: In sample $2$, person $2$ and $4$ will get infected after day $1$. They can only spread the infection to person $3$ and $5$ on day $2$. For $D = 1$, the infected people are $1$, $2$, $4$. If $D$ had been $2$, person $3$ and $5$ would have also been infected.\n\n-----Input-----\nThe first line contains two integers, $1 \\le N \\le 20000$, and $1 \\le D \\le 50000$, the number of people and number of days respectively.\n\nThe second line contains an integer $1 \\le C \\le N$, the initial number of infected people, followed by $C$ integers, the indices of the infected people. Indices are from $1$ to $N$.\n\nThe next $N$ lines describe the entry and exit times of the $N$ people, in order from $1$ to $N$. Each line contains two integers, $s_ i$ and $t_ i$, the entry and exit time respectively. $0 \\le s_ i \\le t_ i \\le 10^9$.\n\n-----Output-----\nPrint a list of indices of people infected after $D$ days, in ascending order. The indices should be all on one line, separated by a single space.\n\n-----Subtasks-----\n - ($40$ Points): $N \\le 5000$. $D = 1$, $C = 1$. Initially, only the person with index $1$ is infected.\n - ($30$ Points): $N \\le 5000$, $D \\le 5$.\n - ($20$ Points): $N \\le 5000$.\n - ($10$ Points): No additional constraint.\n\n-----Examples-----\nSample Input 1:\n9 1\n1 1\n5 10\n1 3\n11 14\n5 5\n10 10\n3 6\n6 12\n7 7\n4 11\nSample Output 1:\n1 4 5 6 7 8 9\n\nSample Input 2:\n5 1\n1 1\n3 3\n2 3\n1 2\n3 4\n4 5\nSample Output 2:\n1 2 4\n\nSample Input 3:\n5 1\n1 1\n3 3\n3 3\n4 4\n4 4\n5 5\nSample Output 3:\n1 2"} +{"id": "03004", "text": "Emuskald is an avid horticulturist and owns the world's longest greenhouse \u2014 it is effectively infinite in length.\n\nOver the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.\n\nEmuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.\n\nOf course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.\n\n\n-----Input-----\n\nThe first line of input contains two space-separated integers n and m (1 \u2264 n, m \u2264 5000, n \u2265 m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number s_{i} (1 \u2264 s_{i} \u2264 m), and one real number x_{i} (0 \u2264 x_{i} \u2264 10^9), the species and position of the i-th plant. Each x_{i} will contain no more than 6 digits after the decimal point.\n\nIt is guaranteed that all x_{i} are different; there is at least one plant of each species; the plants are given in order \"from left to the right\", that is in the ascending order of their x_{i} coordinates (x_{i} < x_{i} + 1, 1 \u2264 i < n).\n\n\n-----Output-----\n\nOutput a single integer \u2014 the minimum number of plants to be replanted.\n\n\n-----Examples-----\nInput\n3 2\n2 1\n1 2.0\n1 3.100\n\nOutput\n1\n\nInput\n3 3\n1 5.0\n2 5.5\n3 6.0\n\nOutput\n0\n\nInput\n6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.\n\nIn the second test case, the species are already in the correct order, so no replanting is needed."} +{"id": "03005", "text": "A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = x\u00b7k.\n\nYou're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and k (1 \u2264 n \u2264 10^5, 1 \u2264 k \u2264 10^9). The next line contains a list of n distinct positive integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9).\n\nAll the numbers in the lines are separated by single spaces.\n\n\n-----Output-----\n\nOn the only line of the output print the size of the largest k-multiple free subset of {a_1, a_2, ..., a_{n}}.\n\n\n-----Examples-----\nInput\n6 2\n2 3 6 5 4 10\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}."} +{"id": "03006", "text": "Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.\n\nGiven a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it a_{k}) and delete it, at that all elements equal to a_{k} + 1 and a_{k} - 1 also must be deleted from the sequence. That step brings a_{k} points to the player. \n\nAlex is a perfectionist, so he decided to get as many points as possible. Help him.\n\n\n-----Input-----\n\nThe first line contains integer n (1 \u2264 n \u2264 10^5) that shows how many numbers are in Alex's sequence. \n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^5).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum number of points that Alex can earn.\n\n\n-----Examples-----\nInput\n2\n1 2\n\nOutput\n2\n\nInput\n3\n1 2 3\n\nOutput\n4\n\nInput\n9\n1 2 1 3 2 2 2 2 3\n\nOutput\n10\n\n\n\n-----Note-----\n\nConsider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points."} +{"id": "03007", "text": "How many infinite sequences a_1, a_2, ... consisting of {{1, ... ,n}} satisfy the following conditions?\n - The n-th and subsequent elements are all equal. That is, if n \\leq i,j, a_i = a_j.\n - For every integer i, the a_i elements immediately following the i-th element are all equal. That is, if i < j < k\\leq i+a_i, a_j = a_k.\nFind the count modulo 10^9+7.\n\n-----Constraints-----\n - 1 \\leq n \\leq 10^6\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn\n\n-----Output-----\nPrint how many sequences satisfy the conditions, modulo 10^9+7.\n\n-----Sample Input-----\n2\n\n-----Sample Output-----\n4\n\nThe four sequences that satisfy the conditions are:\n - 1, 1, 1, ...\n - 1, 2, 2, ...\n - 2, 1, 1, ...\n - 2, 2, 2, ..."} +{"id": "03008", "text": "Snuke is buying a lamp.\nThe light of the lamp can be adjusted to m levels of brightness, represented by integers from 1 through m, by the two buttons on the remote control.\nThe first button is a \"forward\" button. When this button is pressed, the brightness level is increased by 1, except when the brightness level is m, in which case the brightness level becomes 1.\nThe second button is a \"favorite\" button. When this button is pressed, the brightness level becomes the favorite brightness level x, which is set when the lamp is purchased.\nSnuke is thinking of setting the favorite brightness level x so that he can efficiently adjust the brightness.\nHe is planning to change the brightness n-1 times. In the i-th change, the brightness level is changed from a_i to a_{i+1}. The initial brightness level is a_1.\nFind the number of times Snuke needs to press the buttons when x is set to minimize this number.\n\n-----Constraints-----\n - 2 \\leq n,m \\leq 10^5\n - 1 \\leq a_i\\leq m\n - a_i \\neq a_{i+1}\n - n, m and a_i are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn m\na_1 a_2 \u2026 a_n\n\n-----Output-----\nPrint the minimum number of times Snuke needs to press the buttons.\n\n-----Sample Input-----\n4 6\n1 5 1 4\n\n-----Sample Output-----\n5\n\nWhen the favorite brightness level is set to 1, 2, 3, 4, 5 and 6, Snuke needs to press the buttons 8, 9, 7, 5, 6 and 9 times, respectively.\nThus, Snuke should set the favorite brightness level to 4.\nIn this case, the brightness is adjusted as follows:\n - In the first change, press the favorite button once, then press the forward button once.\n - In the second change, press the forward button twice.\n - In the third change, press the favorite button once."} +{"id": "03009", "text": "Polycarpus just has been out of luck lately! As soon as he found a job in the \"Binary Cat\" cafe, the club got burgled. All ice-cream was stolen.\n\nOn the burglary night Polycarpus kept a careful record of all club visitors. Each time a visitor entered the club, Polycarpus put down character \"+\" in his notes. Similarly, each time a visitor left the club, Polycarpus put character \"-\" in his notes. We know that all cases of going in and out happened consecutively, that is, no two events happened at the same time. Polycarpus doesn't remember whether there was somebody in the club at the moment when his shift begun and at the moment when it ended.\n\nRight now the police wonders what minimum number of distinct people Polycarpus could have seen. Assume that he sees anybody coming in or out of the club. Each person could have come in or out an arbitrary number of times.\n\n\n-----Input-----\n\nThe only line of the input contains a sequence of characters \"+\" and \"-\", the characters are written one after another without any separators. The characters are written in the order, in which the corresponding events occurred. The given sequence has length from 1 to 300 characters, inclusive.\n\n\n-----Output-----\n\nPrint the sought minimum number of people\n\n\n-----Examples-----\nInput\n+-+-+\n\nOutput\n1\n\nInput\n---\nOutput\n3"} +{"id": "03010", "text": "Natasha travels around Mars in the Mars rover. But suddenly it broke down, namely\u00a0\u2014 the logical scheme inside it. The scheme is an undirected tree (connected acyclic graph) with a root in the vertex $1$, in which every leaf (excluding root) is an input, and all other vertices are logical elements, including the root, which is output. One bit is fed to each input. One bit is returned at the output.\n\nThere are four types of logical elements: AND ($2$ inputs), OR ($2$ inputs), XOR ($2$ inputs), NOT ($1$ input). Logical elements take values from their direct descendants (inputs) and return the result of the function they perform. Natasha knows the logical scheme of the Mars rover, as well as the fact that only one input is broken. In order to fix the Mars rover, she needs to change the value on this input.\n\nFor each input, determine what the output will be if Natasha changes this input.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 10^6$)\u00a0\u2014 the number of vertices in the graph (both inputs and elements).\n\nThe $i$-th of the next $n$ lines contains a description of $i$-th vertex: the first word \"AND\", \"OR\", \"XOR\", \"NOT\" or \"IN\" (means the input of the scheme) is the vertex type. If this vertex is \"IN\", then the value of this input follows ($0$ or $1$), otherwise follow the indices of input vertices of this element: \"AND\", \"OR\", \"XOR\" have $2$ inputs, whereas \"NOT\" has $1$ input. The vertices are numbered from one.\n\nIt is guaranteed that input data contains a correct logical scheme with an output produced by the vertex $1$.\n\n\n-----Output-----\n\nPrint a string of characters '0' and '1' (without quotes)\u00a0\u2014 answers to the problem for each input in the ascending order of their vertex indices.\n\n\n-----Example-----\nInput\n10\nAND 9 4\nIN 1\nIN 1\nXOR 6 5\nAND 3 7\nIN 0\nNOT 10\nIN 1\nIN 1\nAND 2 8\n\nOutput\n10110\n\n\n-----Note-----\n\nThe original scheme from the example (before the input is changed):\n\n[Image]\n\nGreen indicates bits '1', yellow indicates bits '0'.\n\nIf Natasha changes the input bit $2$ to $0$, then the output will be $1$.\n\nIf Natasha changes the input bit $3$ to $0$, then the output will be $0$.\n\nIf Natasha changes the input bit $6$ to $1$, then the output will be $1$.\n\nIf Natasha changes the input bit $8$ to $0$, then the output will be $1$.\n\nIf Natasha changes the input bit $9$ to $0$, then the output will be $0$."} +{"id": "03011", "text": "Sereja has two sequences a and b and number p. Sequence a consists of n integers a_1, a_2, ..., a_{n}. Similarly, sequence b consists of m integers b_1, b_2, ..., b_{m}. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)\u00b7p \u2264 n;\u00a0q \u2265 1), such that sequence b can be obtained from sequence a_{q}, a_{q} + p, a_{q} + 2p, ..., a_{q} + (m - 1)p by rearranging elements.\n\nSereja needs to rush to the gym, so he asked to find all the described positions of q.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and p (1 \u2264 n, m \u2264 2\u00b710^5, 1 \u2264 p \u2264 2\u00b710^5). The next line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9). The next line contains m integers b_1, b_2, ..., b_{m} (1 \u2264 b_{i} \u2264 10^9).\n\n\n-----Output-----\n\nIn the first line print the number of valid qs. In the second line, print the valid values in the increasing order.\n\n\n-----Examples-----\nInput\n5 3 1\n1 2 3 2 1\n1 2 3\n\nOutput\n2\n1 3\n\nInput\n6 3 2\n1 3 2 2 3 1\n1 2 3\n\nOutput\n2\n1 2"} +{"id": "03012", "text": "Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries.\n\nThe world can be modeled as an undirected graph with n nodes and m edges. k of the nodes are home to the governments of the k countries that make up the world.\n\nThere is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable.\n\nHongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.\n\n\n-----Input-----\n\nThe first line of input will contain three integers n, m and k (1 \u2264 n \u2264 1 000, 0 \u2264 m \u2264 100 000, 1 \u2264 k \u2264 n)\u00a0\u2014 the number of vertices and edges in the graph, and the number of vertices that are homes of the government. \n\nThe next line of input will contain k integers c_1, c_2, ..., c_{k} (1 \u2264 c_{i} \u2264 n). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world.\n\nThe following m lines of input will contain two integers u_{i} and v_{i} (1 \u2264 u_{i}, v_{i} \u2264 n). This denotes an undirected edge between nodes u_{i} and v_{i}.\n\nIt is guaranteed that the graph described by the input is stable.\n\n\n-----Output-----\n\nOutput a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.\n\n\n-----Examples-----\nInput\n4 1 2\n1 3\n1 2\n\nOutput\n2\n\nInput\n3 3 1\n2\n1 2\n1 3\n2 3\n\nOutput\n0\n\n\n\n-----Note-----\n\nFor the first sample test, the graph looks like this: [Image] Vertices 1 and 3 are special. The optimal solution is to connect vertex 4 to vertices 1 and 2. This adds a total of 2 edges. We cannot add any more edges, since vertices 1 and 3 cannot have any path between them.\n\nFor the second sample test, the graph looks like this: $\\infty$ We cannot add any more edges to this graph. Note that we are not allowed to add self-loops, and the graph must be simple."} +{"id": "03013", "text": "You are given a sequence of integers $a_1, a_2, \\dots, a_n$. You need to paint elements in colors, so that: If we consider any color, all elements of this color must be divisible by the minimal element of this color. The number of used colors must be minimized. \n\nFor example, it's fine to paint elements $[40, 10, 60]$ in a single color, because they are all divisible by $10$. You can use any color an arbitrary amount of times (in particular, it is allowed to use a color only once). The elements painted in one color do not need to be consecutive.\n\nFor example, if $a=[6, 2, 3, 4, 12]$ then two colors are required: let's paint $6$, $3$ and $12$ in the first color ($6$, $3$ and $12$ are divisible by $3$) and paint $2$ and $4$ in the second color ($2$ and $4$ are divisible by $2$). For example, if $a=[10, 7, 15]$ then $3$ colors are required (we can simply paint each element in an unique color).\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 100$), where $n$ is the length of the given sequence.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$). These numbers can contain duplicates.\n\n\n-----Output-----\n\nPrint the minimal number of colors to paint all the given numbers in a valid way.\n\n\n-----Examples-----\nInput\n6\n10 2 3 5 4 2\n\nOutput\n3\n\nInput\n4\n100 100 100 100\n\nOutput\n1\n\nInput\n8\n7 6 5 4 3 2 2 3\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, one possible way to paint the elements in $3$ colors is:\n\n paint in the first color the elements: $a_1=10$ and $a_4=5$, paint in the second color the element $a_3=3$, paint in the third color the elements: $a_2=2$, $a_5=4$ and $a_6=2$. \n\nIn the second example, you can use one color to paint all the elements.\n\nIn the third example, one possible way to paint the elements in $4$ colors is:\n\n paint in the first color the elements: $a_4=4$, $a_6=2$ and $a_7=2$, paint in the second color the elements: $a_2=6$, $a_5=3$ and $a_8=3$, paint in the third color the element $a_3=5$, paint in the fourth color the element $a_1=7$."} +{"id": "03014", "text": "You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library.\n\nSome long and uninteresting story was removed...\n\nThe alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large version of a letter x is denoted by x'. BSCII encoding, which is used everywhere in Bookland, is made in that way so that large letters are presented in the order of the numbers they are denoted by, and small letters are presented in the order of the numbers they are denoted by, but all large letters are before all small letters. For example, the following conditions hold: 2 < 3, 2' < 3', 3' < 2.\n\nA word x_1, x_2, ..., x_{a} is not lexicographically greater than y_1, y_2, ..., y_{b} if one of the two following conditions holds: a \u2264 b and x_1 = y_1, ..., x_{a} = y_{a}, i.e. the first word is the prefix of the second word; there is a position 1 \u2264 j \u2264 min(a, b), such that x_1 = y_1, ..., x_{j} - 1 = y_{j} - 1 and x_{j} < y_{j}, i.e. at the first position where the words differ the first word has a smaller letter than the second word has. \n\nFor example, the word \"3' 7 5\" is before the word \"2 4' 6\" in lexicographical order. It is said that sequence of words is in lexicographical order if each word is not lexicographically greater than the next word in the sequence.\n\nDenis has a sequence of words consisting of small letters only. He wants to change some letters to large (let's call this process a capitalization) in such a way that the sequence of words is in lexicographical order. However, he soon realized that for some reason he can't change a single letter in a single word. He only can choose a letter and change all of its occurrences in all words to large letters. He can perform this operation any number of times with arbitrary letters of Bookland's alphabet.\n\nHelp Denis to choose which letters he needs to capitalize (make large) in order to make the sequence of words lexicographically ordered, or determine that it is impossible.\n\nNote that some words can be equal.\n\n\n-----Input-----\n\nThe first line contains two integers n and m (2 \u2264 n \u2264 100 000, 1 \u2264 m \u2264 100 000)\u00a0\u2014 the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to m.\n\nEach of the next n lines contains a description of one word in format l_{i}, s_{i}, 1, s_{i}, 2, ..., s_{i}, l_{i} (1 \u2264 l_{i} \u2264 100 000, 1 \u2264 s_{i}, j \u2264 m), where l_{i} is the length of the word, and s_{i}, j is the sequence of letters in the word. The words are given in the order Denis has them in the sequence.\n\nIt is guaranteed that the total length of all words is not greater than 100 000.\n\n\n-----Output-----\n\nIn the first line print \"Yes\" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print \"No\" (without quotes).\n\nIf the required is possible, in the second line print k\u00a0\u2014 the number of letters Denis has to capitalize (make large), and in the third line print k distinct integers\u00a0\u2014 these letters. Note that you don't need to minimize the value k.\n\nYou can print the letters in any order. If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n4 3\n1 2\n1 1\n3 1 3 2\n2 1 1\n\nOutput\nYes\n2\n2 3 \nInput\n6 5\n2 1 2\n2 1 2\n3 1 2 3\n2 1 5\n2 4 4\n2 4 4\n\nOutput\nYes\n0\n\nInput\n4 3\n4 3 2 2 1\n3 1 1 3\n3 2 3 3\n2 3 1\n\nOutput\nNo\n\n\n\n-----Note-----\n\nIn the first example after Denis makes letters 2 and 3 large, the sequence looks like the following: 2' 1 1 3' 2' 1 1 \n\nThe condition 2' < 1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order. As the first letters of the third and the fourth words are the same, and 3' < 1, then the third word is not lexicographically larger than the fourth word.\n\nIn the second example the words are in lexicographical order from the beginning, so Denis can do nothing.\n\nIn the third example there is no set of letters such that if Denis capitalizes them, the sequence becomes lexicographically ordered."} +{"id": "03015", "text": "Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.\n\nFor permutation p = p_0, p_1, ..., p_{n}, Polo has defined its beauty \u2014 number $(0 \\oplus p_{0}) +(1 \\oplus p_{1}) + \\cdots +(n \\oplus p_{n})$.\n\nExpression $x \\oplus y$ means applying the operation of bitwise excluding \"OR\" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as \"^\" and in Pascal \u2014 as \"xor\".\n\nHelp him find among all permutations of integers from 0 to n the permutation with the maximum beauty.\n\n\n-----Input-----\n\nThe single line contains a positive integer n (1 \u2264 n \u2264 10^6).\n\n\n-----Output-----\n\nIn the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.\n\nIf there are several suitable permutations, you are allowed to print any of them.\n\n\n-----Examples-----\nInput\n4\n\nOutput\n20\n0 2 1 4 3"} +{"id": "03016", "text": "After the war, the supersonic rocket became the most common public transportation.\n\nEach supersonic rocket consists of two \"engines\". Each engine is a set of \"power sources\". The first engine has $n$ power sources, and the second one has $m$ power sources. A power source can be described as a point $(x_i, y_i)$ on a 2-D plane. All points in each engine are different.\n\nYou can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: $(x_i, y_i)$ becomes $(x_i+a, y_i+b)$, $a$ and $b$ can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: $(x_i, y_i)$ becomes $(x_i \\cos \\theta - y_i \\sin \\theta, x_i \\sin \\theta + y_i \\cos \\theta)$, $\\theta$ can be any real number. In other words, all power sources will be rotated.\n\nThe engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources $A(x_a, y_a)$ and $B(x_b, y_b)$ exist, then for all real number $k$ that $0 \\lt k \\lt 1$, a new power source will be created $C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)$. Then, this procedure will be repeated again with all new and old power sources. After that, the \"power field\" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).\n\nA supersonic rocket is \"safe\" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.\n\nGiven a supersonic rocket, check whether it is safe or not.\n\n\n-----Input-----\n\nThe first line contains two integers $n$, $m$ ($3 \\le n, m \\le 10^5$)\u00a0\u2014 the number of power sources in each engine.\n\nEach of the next $n$ lines contains two integers $x_i$ and $y_i$ ($0\\leq x_i, y_i\\leq 10^8$)\u00a0\u2014 the coordinates of the $i$-th power source in the first engine.\n\nEach of the next $m$ lines contains two integers $x_i$ and $y_i$ ($0\\leq x_i, y_i\\leq 10^8$)\u00a0\u2014 the coordinates of the $i$-th power source in the second engine.\n\nIt is guaranteed that there are no two or more power sources that are located in the same point in each engine.\n\n\n-----Output-----\n\nPrint \"YES\" if the supersonic rocket is safe, otherwise \"NO\".\n\nYou can print each letter in an arbitrary case (upper or lower).\n\n\n-----Examples-----\nInput\n3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n1 1\n\nOutput\nYES\n\nInput\n3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n0 0\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe first sample: [Image] Those near pairs of blue and orange points actually coincide. \n\nFirst, manipulate the first engine: use the second operation with $\\theta = \\pi$ (to rotate all power sources $180$ degrees).\n\nThe power sources in the first engine become $(0, 0)$, $(0, -2)$, and $(-2, 0)$. [Image] \n\nSecond, manipulate the second engine: use the first operation with $a = b = -2$.\n\nThe power sources in the second engine become $(-2, 0)$, $(0, 0)$, $(0, -2)$, and $(-1, -1)$. [Image] \n\nYou can examine that destroying any point, the power field formed by the two engines are always the solid triangle $(0, 0)$, $(-2, 0)$, $(0, -2)$.\n\nIn the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it."} +{"id": "03017", "text": "In Morse code, an letter of English alphabet is represented as a string of some length from $1$ to $4$. Moreover, each Morse code representation of an English letter contains only dots and dashes. In this task, we will represent a dot with a \"0\" and a dash with a \"1\".\n\nBecause there are $2^1+2^2+2^3+2^4 = 30$ strings with length $1$ to $4$ containing only \"0\" and/or \"1\", not all of them correspond to one of the $26$ English letters. In particular, each string of \"0\" and/or \"1\" of length at most $4$ translates into a distinct English letter, except the following four strings that do not correspond to any English alphabet: \"0011\", \"0101\", \"1110\", and \"1111\".\n\nYou will work with a string $S$, which is initially empty. For $m$ times, either a dot or a dash will be appended to $S$, one at a time. Your task is to find and report, after each of these modifications to string $S$, the number of non-empty sequences of English letters that are represented with some substring of $S$ in Morse code.\n\nSince the answers can be incredibly tremendous, print them modulo $10^9 + 7$.\n\n\n-----Input-----\n\nThe first line contains an integer $m$ ($1 \\leq m \\leq 3\\,000$)\u00a0\u2014 the number of modifications to $S$. \n\nEach of the next $m$ lines contains either a \"0\" (representing a dot) or a \"1\" (representing a dash), specifying which character should be appended to $S$.\n\n\n-----Output-----\n\nPrint $m$ lines, the $i$-th of which being the answer after the $i$-th modification to $S$.\n\n\n-----Examples-----\nInput\n3\n1\n1\n1\n\nOutput\n1\n3\n7\n\nInput\n5\n1\n0\n1\n0\n1\n\nOutput\n1\n4\n10\n22\n43\n\nInput\n9\n1\n1\n0\n0\n0\n1\n1\n0\n1\n\nOutput\n1\n3\n10\n24\n51\n109\n213\n421\n833\n\n\n\n-----Note-----\n\nLet us consider the first sample after all characters have been appended to $S$, so S is \"111\".\n\nAs you can see, \"1\", \"11\", and \"111\" all correspond to some distinct English letter. In fact, they are translated into a 'T', an 'M', and an 'O', respectively. All non-empty sequences of English letters that are represented with some substring of $S$ in Morse code, therefore, are as follows. \"T\" (translates into \"1\") \"M\" (translates into \"11\") \"O\" (translates into \"111\") \"TT\" (translates into \"11\") \"TM\" (translates into \"111\") \"MT\" (translates into \"111\") \"TTT\" (translates into \"111\") \n\nAlthough unnecessary for this task, a conversion table from English alphabets into Morse code can be found here."} +{"id": "03018", "text": "Given is an undirected graph G consisting of N vertices numbered 1 through N and M edges numbered 1 through M.\nEdge i connects Vertex a_i and Vertex b_i bidirectionally.\nG is said to be a good graph when both of the conditions below are satisfied. It is guaranteed that G is initially a good graph.\n - Vertex 1 and Vertex N are not connected.\n - There are no self-loops and no multi-edges.\nTaro the first and Jiro the second will play a game against each other.\nThey will alternately take turns, with Taro the first going first.\nIn each player's turn, the player can do the following operation:\n - Operation: Choose vertices u and v, then add to G an edge connecting u and v bidirectionally.\nThe player whose addition of an edge results in G being no longer a good graph loses. Determine the winner of the game when the two players play optimally.\nYou are given T test cases. Solve each of them.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq T \\leq 10^5\n - 2 \\leq N \\leq 10^{5}\n - 0 \\leq M \\leq \\min(N(N-1)/2,10^{5})\n - 1 \\leq a_i,b_i \\leq N\n - The given graph is a good graph.\n - In one input file, the sum of N and that of M do not exceed 2 \\times 10^5.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nT\n\\mathrm{case}_1\n\\vdots\n\\mathrm{case}_T\n\nEach case is in the following format:\nN M\na_1 b_1\n\\vdots\na_M b_M\n\n-----Output-----\nPrint T lines. The i-th line should contain First if Taro the first wins in the i-th test case, and Second if Jiro the second wins in the test case.\n\n-----Sample Input-----\n3\n3 0\n6 2\n1 2\n2 3\n15 10\n12 14\n8 3\n10 1\n14 6\n12 6\n1 9\n13 1\n2 5\n3 9\n7 2\n\n-----Sample Output-----\nFirst\nSecond\nFirst\n\n - In test case 1, Taro the first wins. Below is one sequence of moves that results in Taro's win:\n - In Taro the first's turn, he adds an edge connecting Vertex 1 and 2, after which the graph is still good.\n - Then, whichever two vertices Jiro the second would choose to connect with an edge, the graph would no longer be good.\n - Thus, Taro wins."} +{"id": "03019", "text": "Mike and Ann are sitting in the classroom. The lesson is boring, so they decided to play an interesting game. Fortunately, all they need to play this game is a string $s$ and a number $k$ ($0 \\le k < |s|$).\n\nAt the beginning of the game, players are given a substring of $s$ with left border $l$ and right border $r$, both equal to $k$ (i.e. initially $l=r=k$). Then players start to make moves one by one, according to the following rules: A player chooses $l^{\\prime}$ and $r^{\\prime}$ so that $l^{\\prime} \\le l$, $r^{\\prime} \\ge r$ and $s[l^{\\prime}, r^{\\prime}]$ is lexicographically less than $s[l, r]$. Then the player changes $l$ and $r$ in this way: $l := l^{\\prime}$, $r := r^{\\prime}$. Ann moves first. The player, that can't make a move loses.\n\nRecall that a substring $s[l, r]$ ($l \\le r$) of a string $s$ is a continuous segment of letters from s that starts at position $l$ and ends at position $r$. For example, \"ehn\" is a substring ($s[3, 5]$) of \"aaaehnsvz\" and \"ahz\" is not.\n\nMike and Ann were playing so enthusiastically that they did not notice the teacher approached them. Surprisingly, the teacher didn't scold them, instead of that he said, that he can figure out the winner of the game before it starts, even if he knows only $s$ and $k$.\n\nUnfortunately, Mike and Ann are not so keen in the game theory, so they ask you to write a program, that takes $s$ and determines the winner for all possible $k$.\n\n\n-----Input-----\n\nThe first line of the input contains a single string $s$ ($1 \\leq |s| \\leq 5 \\cdot 10^5$) consisting of lowercase English letters.\n\n\n-----Output-----\n\nPrint $|s|$ lines.\n\nIn the line $i$ write the name of the winner (print Mike or Ann) in the game with string $s$ and $k = i$, if both play optimally\n\n\n-----Examples-----\nInput\nabba\n\nOutput\nMike\nAnn\nAnn\nMike\n\nInput\ncba\n\nOutput\nMike\nMike\nMike"} +{"id": "03020", "text": "You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i_1, j_1), (i_2, j_2), ..., (i_{m}, j_{m}). Each good pair (i_{k}, j_{k}) meets the following conditions: i_{k} + j_{k} is an odd number and 1 \u2264 i_{k} < j_{k} \u2264 n.\n\nIn one operation you can perform a sequence of actions: take one of the good pairs (i_{k}, j_{k}) and some integer v (v > 1), which divides both numbers a[i_{k}] and a[j_{k}]; divide both numbers by v, i. e. perform the assignments: $a [ i_{k} ] = \\frac{a [ i_{k} ]}{v}$ and $a [ j_{k} ] = \\frac{a [ j_{k} ]}{v}$. \n\nDetermine the maximum number of operations you can sequentially perform on the given array. Note that one pair may be used several times in the described operations.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers n, m (2 \u2264 n \u2264 100, 1 \u2264 m \u2264 100).\n\nThe second line contains n space-separated integers a[1], a[2], ..., a[n] (1 \u2264 a[i] \u2264 10^9) \u2014 the description of the array.\n\nThe following m lines contain the description of good pairs. The k-th line contains two space-separated integers i_{k}, j_{k} (1 \u2264 i_{k} < j_{k} \u2264 n, i_{k} + j_{k} is an odd number).\n\nIt is guaranteed that all the good pairs are distinct.\n\n\n-----Output-----\n\nOutput the answer for the problem.\n\n\n-----Examples-----\nInput\n3 2\n8 3 8\n1 2\n2 3\n\nOutput\n0\n\nInput\n3 2\n8 12 8\n1 2\n2 3\n\nOutput\n2"} +{"id": "03021", "text": "Little penguin Polo adores strings. But most of all he adores strings of length n.\n\nOne day he wanted to find a string that meets the following conditions: The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct. No two neighbouring letters of a string coincide; that is, if we represent a string as s = s_1s_2... s_{n}, then the following inequality holds, s_{i} \u2260 s_{i} + 1(1 \u2264 i < n). Among all strings that meet points 1 and 2, the required string is lexicographically smallest. \n\nHelp him find such string or state that such string doesn't exist.\n\nString x = x_1x_2... x_{p} is lexicographically less than string y = y_1y_2... y_{q}, if either p < q and x_1 = y_1, x_2 = y_2, ... , x_{p} = y_{p}, or there is such number r (r < p, r < q), that x_1 = y_1, x_2 = y_2, ... , x_{r} = y_{r} and x_{r} + 1 < y_{r} + 1. The characters of the strings are compared by their ASCII codes.\n\n\n-----Input-----\n\nA single line contains two positive integers n and k (1 \u2264 n \u2264 10^6, 1 \u2264 k \u2264 26) \u2014 the string's length and the number of distinct letters.\n\n\n-----Output-----\n\nIn a single line print the required string. If there isn't such string, print \"-1\" (without the quotes).\n\n\n-----Examples-----\nInput\n7 4\n\nOutput\nababacd\n\nInput\n4 7\n\nOutput\n-1"} +{"id": "03022", "text": "A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.\n\nA performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a_1, a_2, ..., a_{n}.\n\nLittle Tommy is among them. He would like to choose an interval [l, r] (1 \u2264 l \u2264 r \u2264 n), then reverse a_{l}, a_{l} + 1, ..., a_{r} so that the length of the longest non-decreasing subsequence of the new sequence is maximum.\n\nA non-decreasing subsequence is a sequence of indices p_1, p_2, ..., p_{k}, such that p_1 < p_2 < ... < p_{k} and a_{p}_1 \u2264 a_{p}_2 \u2264 ... \u2264 a_{p}_{k}. The length of the subsequence is k.\n\n\n-----Input-----\n\nThe first line contains an integer n (1 \u2264 n \u2264 2000), denoting the length of the original sequence.\n\nThe second line contains n space-separated integers, describing the original sequence a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 2, i = 1, 2, ..., n).\n\n\n-----Output-----\n\nPrint a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.\n\n\n-----Examples-----\nInput\n4\n1 2 1 2\n\nOutput\n4\n\nInput\n10\n1 1 2 2 2 1 1 2 2 1\n\nOutput\n9\n\n\n\n-----Note-----\n\nIn the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.\n\nIn the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9."} +{"id": "03023", "text": "Vasya has a graph containing both directed (oriented) and undirected (non-oriented) edges. There can be multiple edges between a pair of vertices.\n\nVasya has picked a vertex s from the graph. Now Vasya wants to create two separate plans:\n\n to orient each undirected edge in one of two possible directions to maximize number of vertices reachable from vertex s; to orient each undirected edge in one of two possible directions to minimize number of vertices reachable from vertex s. \n\nIn each of two plans each undirected edge must become directed. For an edge chosen directions can differ in two plans.\n\nHelp Vasya find the plans.\n\n\n-----Input-----\n\nThe first line contains three integers n, m and s (2 \u2264 n \u2264 3\u00b710^5, 1 \u2264 m \u2264 3\u00b710^5, 1 \u2264 s \u2264 n) \u2014 number of vertices and edges in the graph, and the vertex Vasya has picked.\n\nThe following m lines contain information about the graph edges. Each line contains three integers t_{i}, u_{i} and v_{i} (1 \u2264 t_{i} \u2264 2, 1 \u2264 u_{i}, v_{i} \u2264 n, u_{i} \u2260 v_{i}) \u2014 edge type and vertices connected by the edge. If t_{i} = 1 then the edge is directed and goes from the vertex u_{i} to the vertex v_{i}. If t_{i} = 2 then the edge is undirected and it connects the vertices u_{i} and v_{i}.\n\nIt is guaranteed that there is at least one undirected edge in the graph.\n\n\n-----Output-----\n\nThe first two lines should describe the plan which maximizes the number of reachable vertices. The lines three and four should describe the plan which minimizes the number of reachable vertices.\n\nA description of each plan should start with a line containing the number of reachable vertices. The second line of a plan should consist of f symbols '+' and '-', where f is the number of undirected edges in the initial graph. Print '+' as the j-th symbol of the string if the j-th undirected edge (u, v) from the input should be oriented from u to v. Print '-' to signify the opposite direction (from v to u). Consider undirected edges to be numbered in the same order they are given in the input.\n\nIf there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n2 2 1\n1 1 2\n2 2 1\n\nOutput\n2\n-\n2\n+\n\nInput\n6 6 3\n2 2 6\n1 4 5\n2 3 4\n1 4 1\n1 3 1\n2 2 3\n\nOutput\n6\n++-\n2\n+-+"} +{"id": "03024", "text": "You have number a, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7.\n\nNumber a doesn't contain any leading zeroes and contains digits 1, 6, 8, 9 (it also can contain another digits). The resulting number also mustn't contain any leading zeroes.\n\n\n-----Input-----\n\nThe first line contains positive integer a in the decimal record. It is guaranteed that the record of number a contains digits: 1, 6, 8, 9. Number a doesn't contain any leading zeroes. The decimal representation of number a contains at least 4 and at most 10^6 characters.\n\n\n-----Output-----\n\nPrint a number in the decimal notation without leading zeroes \u2014 the result of the permutation.\n\nIf it is impossible to rearrange the digits of the number a in the required manner, print 0.\n\n\n-----Examples-----\nInput\n1689\n\nOutput\n1869\n\nInput\n18906\n\nOutput\n18690"} +{"id": "03025", "text": "In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network\u00a0\u2014 for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.\n\nA train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.\n\nYou've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety\u00a0\u2014 in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.\n\nUnder these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.\n\n\n-----Input-----\n\nThe first line of the input contains two integers n and m (2 \u2264 n \u2264 400, 0 \u2264 m \u2264 n(n - 1) / 2)\u00a0\u2014 the number of towns and the number of railways respectively.\n\nEach of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 \u2264 u, v \u2264 n, u \u2260 v).\n\nYou may assume that there is at most one railway connecting any two towns.\n\n\n-----Output-----\n\nOutput one integer\u00a0\u2014 the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output - 1.\n\n\n-----Examples-----\nInput\n4 2\n1 3\n3 4\n\nOutput\n2\n\nInput\n4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n\nOutput\n-1\n\nInput\n5 5\n4 2\n3 5\n4 5\n5 1\n1 2\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first sample, the train can take the route $1 \\rightarrow 3 \\rightarrow 4$ and the bus can take the route $1 \\rightarrow 2 \\rightarrow 4$. Note that they can arrive at town 4 at the same time.\n\nIn the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4."} +{"id": "03026", "text": "Leha decided to move to a quiet town Vi\u010dkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vi\u010dkopolis.\n\nLet's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point x_{i}. Moreover the coordinates of all computers are distinct. \n\nLeha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.\n\nLeha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression $\\sum_{a \\subseteq A, a \\neq \\varnothing} F(a)$. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, $F(a) = \\operatorname{max}_{i, j \\in a}|x_{i} - x_{j}|$. Since the required sum can be quite large Noora asks to find it modulo 10^9 + 7.\n\nThough, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.\n\n\n-----Input-----\n\nThe first line contains one integer n (1 \u2264 n \u2264 3\u00b710^5) denoting the number of hacked computers.\n\nThe second line contains n integers x_1, x_2, ..., x_{n} (1 \u2264 x_{i} \u2264 10^9) denoting the coordinates of hacked computers. It is guaranteed that all x_{i} are distinct.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the required sum modulo 10^9 + 7.\n\n\n-----Examples-----\nInput\n2\n4 7\n\nOutput\n3\n\nInput\n3\n4 3 1\n\nOutput\n9\n\n\n\n-----Note-----\n\nThere are three non-empty subsets in the first sample test:$\\{4 \\}$, $\\{7 \\}$ and $\\{4,7 \\}$. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.\n\nThere are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: $\\{4,3 \\}$, $\\{4,1 \\}$, $\\{3,1 \\}$, $\\{4,3,1 \\}$. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9."} +{"id": "03027", "text": "This is the harder version of the problem. In this version, $1 \\le n \\le 10^6$ and $0 \\leq a_i \\leq 10^6$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems\n\nChristmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces.\n\nSince Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \\ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. \n\nCharlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 10^6$)\u00a0\u2014 the number of chocolate boxes.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 10^6$)\u00a0\u2014 the number of chocolate pieces in the $i$-th box.\n\nIt is guaranteed that at least one of $a_1, a_2, \\ldots, a_n$ is positive.\n\n\n-----Output-----\n\nIf there is no way for Charlie to make Alice happy, print $-1$.\n\nOtherwise, print a single integer $x$\u00a0\u2014 the minimum number of seconds for Charlie to help Bob make Alice happy.\n\n\n-----Examples-----\nInput\n3\n4 8 5\n\nOutput\n9\n\nInput\n5\n3 10 2 1 5\n\nOutput\n2\n\nInput\n4\n0 5 15 10\n\nOutput\n0\n\nInput\n1\n1\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example, Charlie can move all chocolate pieces to the second box. Each box will be divisible by $17$.\n\nIn the second example, Charlie can move a piece from box $2$ to box $3$ and a piece from box $4$ to box $5$. Each box will be divisible by $3$.\n\nIn the third example, each box is already divisible by $5$.\n\nIn the fourth example, since Charlie has no available move, he cannot help Bob make Alice happy."} +{"id": "03028", "text": "Recently, Tokitsukaze found an interesting game. Tokitsukaze had $n$ items at the beginning of this game. However, she thought there were too many items, so now she wants to discard $m$ ($1 \\le m \\le n$) special items of them.\n\nThese $n$ items are marked with indices from $1$ to $n$. In the beginning, the item with index $i$ is placed on the $i$-th position. Items are divided into several pages orderly, such that each page contains exactly $k$ positions and the last positions on the last page may be left empty.\n\nTokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded.\n\n [Image] Consider the first example from the statement: $n=10$, $m=4$, $k=5$, $p=[3, 5, 7, 10]$. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices $3$ and $5$. After, the first page remains to be special. It contains $[1, 2, 4, 6, 7]$, Tokitsukaze discards the special item with index $7$. After, the second page is special (since it is the first page containing a special item). It contains $[9, 10]$, Tokitsukaze discards the special item with index $10$. \n\nTokitsukaze wants to know the number of operations she would do in total.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $k$ ($1 \\le n \\le 10^{18}$, $1 \\le m \\le 10^5$, $1 \\le m, k \\le n$)\u00a0\u2014 the number of items, the number of special items to be discarded and the number of positions in each page.\n\nThe second line contains $m$ distinct integers $p_1, p_2, \\ldots, p_m$ ($1 \\le p_1 < p_2 < \\ldots < p_m \\le n$)\u00a0\u2014 the indices of special items which should be discarded.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the number of operations that Tokitsukaze would do in total.\n\n\n-----Examples-----\nInput\n10 4 5\n3 5 7 10\n\nOutput\n3\n\nInput\n13 4 5\n7 8 9 10\n\nOutput\n1\n\n\n\n-----Note-----\n\nFor the first example:\n\n In the first operation, Tokitsukaze would focus on the first page $[1, 2, 3, 4, 5]$ and discard items with indices $3$ and $5$; In the second operation, Tokitsukaze would focus on the first page $[1, 2, 4, 6, 7]$ and discard item with index $7$; In the third operation, Tokitsukaze would focus on the second page $[9, 10]$ and discard item with index $10$. \n\nFor the second example, Tokitsukaze would focus on the second page $[6, 7, 8, 9, 10]$ and discard all special items at once."} +{"id": "03029", "text": "It is a holiday season, and Koala is decorating his house with cool lights! He owns $n$ lights, all of which flash periodically.\n\nAfter taking a quick glance at them, Koala realizes that each of his lights can be described with two parameters $a_i$ and $b_i$. Light with parameters $a_i$ and $b_i$ will toggle (on to off, or off to on) every $a_i$ seconds starting from the $b_i$-th second. In other words, it will toggle at the moments $b_i$, $b_i + a_i$, $b_i + 2 \\cdot a_i$ and so on.\n\nYou know for each light whether it's initially on or off and its corresponding parameters $a_i$ and $b_i$. Koala is wondering what is the maximum number of lights that will ever be on at the same time. So you need to find that out.\n\n [Image] Here is a graphic for the first example. \n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 100$), the number of lights.\n\nThe next line contains a string $s$ of $n$ characters. The $i$-th character is \"1\", if the $i$-th lamp is initially on. Otherwise, $i$-th character is \"0\".\n\nThe $i$-th of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \\le a_i, b_i \\le 5$) \u00a0\u2014 the parameters of the $i$-th light.\n\n\n-----Output-----\n\nPrint a single integer\u00a0\u2014 the maximum number of lights that will ever be on at the same time.\n\n\n-----Examples-----\nInput\n3\n101\n3 3\n3 2\n3 1\n\nOutput\n2\n\nInput\n4\n1111\n3 4\n5 2\n3 1\n3 2\n\nOutput\n4\n\nInput\n6\n011100\n5 3\n5 5\n2 4\n3 5\n4 2\n1 5\n\nOutput\n6\n\n\n\n-----Note-----\n\nFor first example, the lamps' states are shown in the picture above. The largest number of simultaneously on lamps is $2$ (e.g. at the moment $2$).\n\nIn the second example, all lights are initially on. So the answer is $4$."} +{"id": "03030", "text": "Let $s$ be some string consisting of symbols \"0\" or \"1\". Let's call a string $t$ a substring of string $s$, if there exists such number $1 \\leq l \\leq |s| - |t| + 1$ that $t = s_l s_{l+1} \\ldots s_{l + |t| - 1}$. Let's call a substring $t$ of string $s$ unique, if there exist only one such $l$. \n\nFor example, let $s = $\"1010111\". A string $t = $\"010\" is an unique substring of $s$, because $l = 2$ is the only one suitable number. But, for example $t = $\"10\" isn't a unique substring of $s$, because $l = 1$ and $l = 3$ are suitable. And for example $t =$\"00\" at all isn't a substring of $s$, because there is no suitable $l$.\n\nToday Vasya solved the following problem at the informatics lesson: given a string consisting of symbols \"0\" and \"1\", the task is to find the length of its minimal unique substring. He has written a solution to this problem and wants to test it. He is asking you to help him.\n\nYou are given $2$ positive integers $n$ and $k$, such that $(n \\bmod 2) = (k \\bmod 2)$, where $(x \\bmod 2)$ is operation of taking remainder of $x$ by dividing on $2$. Find any string $s$ consisting of $n$ symbols \"0\" or \"1\", such that the length of its minimal unique substring is equal to $k$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$, separated by spaces ($1 \\leq k \\leq n \\leq 100\\,000$, $(k \\bmod 2) = (n \\bmod 2)$).\n\n\n-----Output-----\n\nPrint a string $s$ of length $n$, consisting of symbols \"0\" and \"1\". Minimal length of the unique substring of $s$ should be equal to $k$. You can find any suitable string. It is guaranteed, that there exists at least one such string.\n\n\n-----Examples-----\nInput\n4 4\n\nOutput\n1111\nInput\n5 3\n\nOutput\n01010\nInput\n7 3\n\nOutput\n1011011\n\n\n\n-----Note-----\n\nIn the first test, it's easy to see, that the only unique substring of string $s = $\"1111\" is all string $s$, which has length $4$.\n\nIn the second test a string $s = $\"01010\" has minimal unique substring $t =$\"101\", which has length $3$.\n\nIn the third test a string $s = $\"1011011\" has minimal unique substring $t =$\"110\", which has length $3$."} +{"id": "03031", "text": "As we all know Barney's job is \"PLEASE\" and he has not much to do at work. That's why he started playing \"cups and key\". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's heart is under the middle cup. [Image] \n\nThen at one turn Barney swaps the cup in the middle with any of other two cups randomly (he choses each with equal probability), so the chosen cup becomes the middle one. Game lasts n turns and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it was at the start.\n\nAfter n-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney was distracted while making turns and doesn't know if the key is under the middle cup. That's why he asked you to tell him the probability that girl guessed right.\n\nNumber n of game turns can be extremely large, that's why Barney did not give it to you. Instead he gave you an array a_1, a_2, ..., a_{k} such that $n = \\prod_{i = 1}^{k} a_{i}$ \n\nin other words, n is multiplication of all elements of the given array.\n\nBecause of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In other words you need to find it as a fraction p / q such that $\\operatorname{gcd}(p, q) = 1$, where $gcd$ is the greatest common divisor. Since p and q can be extremely large, you only need to find the remainders of dividing each of them by 10^9 + 7.\n\nPlease note that we want $gcd$ of p and q to be 1, not $gcd$ of their remainders after dividing by 10^9 + 7.\n\n\n-----Input-----\n\nThe first line of input contains a single integer k (1 \u2264 k \u2264 10^5)\u00a0\u2014 the number of elements in array Barney gave you.\n\nThe second line contains k integers a_1, a_2, ..., a_{k} (1 \u2264 a_{i} \u2264 10^18)\u00a0\u2014 the elements of the array.\n\n\n-----Output-----\n\nIn the only line of output print a single string x / y where x is the remainder of dividing p by 10^9 + 7 and y is the remainder of dividing q by 10^9 + 7.\n\n\n-----Examples-----\nInput\n1\n2\n\nOutput\n1/2\n\nInput\n3\n1 1 1\n\nOutput\n0/1"} +{"id": "03032", "text": "Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote a_{i}. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).\n\nLet's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.\n\nThe vertex v controls the vertex u (v \u2260 u) if and only if u is in the subtree of v and dist(v, u) \u2264 a_{u}.\n\nAlyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that v controls u.\n\n\n-----Input-----\n\nThe first line contains single integer n (1 \u2264 n \u2264 2\u00b710^5).\n\nThe second line contains n integers a_1, a_2, ..., a_{n} (1 \u2264 a_{i} \u2264 10^9)\u00a0\u2014 the integers written in the vertices.\n\nThe next (n - 1) lines contain two integers each. The i-th of these lines contains integers p_{i} and w_{i} (1 \u2264 p_{i} \u2264 n, 1 \u2264 w_{i} \u2264 10^9)\u00a0\u2014 the parent of the (i + 1)-th vertex in the tree and the number written on the edge between p_{i} and (i + 1).\n\nIt is guaranteed that the given graph is a tree.\n\n\n-----Output-----\n\nPrint n integers\u00a0\u2014 the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.\n\n\n-----Examples-----\nInput\n5\n2 5 1 4 6\n1 7\n1 1\n3 5\n3 6\n\nOutput\n1 0 1 0 0\n\nInput\n5\n9 7 8 6 5\n1 1\n2 1\n3 1\n4 1\n\nOutput\n4 3 2 1 0\n\n\n\n-----Note-----\n\nIn the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1 controls the vertex 5)."} +{"id": "03033", "text": "A group of n friends enjoys playing popular video game Toda 2. There is a rating system describing skill level of each player, initially the rating of the i-th friend is r_{i}.\n\nThe friends decided to take part in the championship as a team. But they should have equal ratings to be allowed to compose a single team consisting of all n friends. So the friends are faced with the problem: how to make all their ratings equal.\n\nOne way to change ratings is to willingly lose in some matches. Friends can form a party consisting of two to five (but not more than n) friends and play a match in the game. When the party loses, the rating of each of its members decreases by 1. A rating can't become negative, so r_{i} = 0 doesn't change after losing.\n\nThe friends can take part in multiple matches, each time making a party from any subset of friends (but remember about constraints on party size: from 2 to 5 members).\n\nThe friends want to make their ratings equal but as high as possible.\n\nHelp the friends develop a strategy of losing the matches so that all their ratings become equal and the resulting rating is maximum possible.\n\n\n-----Input-----\n\nThe first line contains a single integer n (2 \u2264 n \u2264 100) \u2014 the number of friends.\n\nThe second line contains n non-negative integers r_1, r_2, ..., r_{n} (0 \u2264 r_{i} \u2264 100), where r_{i} is the initial rating of the i-th friend.\n\n\n-----Output-----\n\nIn the first line, print a single integer R \u2014 the final rating of each of the friends.\n\nIn the second line, print integer t \u2014 the number of matches the friends have to play. Each of the following t lines should contain n characters '0' or '1', where the j-th character of the i-th line is equal to:\n\n '0', if friend j should not play in match i, '1', if friend j should play in match i. \n\nEach line should contain between two and five characters '1', inclusive.\n\nThe value t should not exceed 10^4, it is guaranteed that such solution exists. \n\nRemember that you shouldn't minimize the value t, but you should maximize R. If there are multiple solutions, print any of them.\n\n\n-----Examples-----\nInput\n5\n4 5 1 7 4\n\nOutput\n1\n8\n01010\n00011\n01010\n10010\n00011\n11000\n00011\n11000\n\nInput\n2\n1 2\n\nOutput\n0\n2\n11\n11\n\nInput\n3\n1 1 1\n\nOutput\n1\n0"} +{"id": "03034", "text": "AtCoDeer the deer has N square tiles. The tiles are numbered 1 through N, and the number given to each tile is written on one side of the tile. Also, each corner of each tile is painted in one of the 1000 colors, which are represented by the integers 0 between 999. The top-left, top-right, bottom-right and bottom-left corner of the tile with the number i are painted in color C_{i,0}, C_{i,1}, C_{i,2} and C_{i,3}, respectively, when seen in the direction of the number written on the tile (See Figure 1).\nFigure 1: The correspondence between the colors of a tile and the input\nAtCoDeer is constructing a cube using six of these tiles, under the following conditions:\n - For each tile, the side with the number must face outward.\n - For each vertex of the cube, the three corners of the tiles that forms it must all be painted in the same color.\nHelp him by finding the number of the different cubes that can be constructed under the conditions.\nSince each tile has a number written on it, two cubes are considered different if the set of the used tiles are different, or the tiles are used in different directions, even if the formation of the colors are the same. (Each tile can be used in one of the four directions, obtained by 90\u00b0 rotations.) Two cubes are considered the same only if rotating one in the three dimensional space can obtain an exact copy of the other, including the directions of the tiles.\nFigure 2: The four directions of a tile\n\n-----Constraints-----\n - 6\u2266N\u2266400\n - 0\u2266C_{i,j}\u2266999 (1\u2266i\u2266N , 0\u2266j\u22663)\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\nC_{1,0} C_{1,1} C_{1,2} C_{1,3}\nC_{2,0} C_{2,1} C_{2,2} C_{2,3}\n:\nC_{N,0} C_{N,1} C_{N,2} C_{N,3}\n\n-----Output-----\nPrint the number of the different cubes that can be constructed under the conditions.\n\n-----Sample Input-----\n6\n0 1 2 3\n0 4 6 1\n1 6 7 2\n2 7 5 3\n6 4 5 7\n4 0 3 5\n\n-----Sample Output-----\n1\n\nThe cube below can be constructed."} +{"id": "03035", "text": "You are given an unweighted tree with $n$ vertices. Recall that a tree is a connected undirected graph without cycles.\n\nYour task is to choose three distinct vertices $a, b, c$ on this tree such that the number of edges which belong to at least one of the simple paths between $a$ and $b$, $b$ and $c$, or $a$ and $c$ is the maximum possible. See the notes section for a better understanding.\n\nThe simple path is the path that visits each vertex at most once.\n\n\n-----Input-----\n\nThe first line contains one integer number $n$ ($3 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of vertices in the tree. \n\nNext $n - 1$ lines describe the edges of the tree in form $a_i, b_i$ ($1 \\le a_i$, $b_i \\le n$, $a_i \\ne b_i$). It is guaranteed that given graph is a tree.\n\n\n-----Output-----\n\nIn the first line print one integer $res$ \u2014 the maximum number of edges which belong to at least one of the simple paths between $a$ and $b$, $b$ and $c$, or $a$ and $c$.\n\nIn the second line print three integers $a, b, c$ such that $1 \\le a, b, c \\le n$ and $a \\ne, b \\ne c, a \\ne c$.\n\nIf there are several answers, you can print any.\n\n\n-----Example-----\nInput\n8\n1 2\n2 3\n3 4\n4 5\n4 6\n3 7\n3 8\n\nOutput\n5\n1 8 6\n\n\n\n-----Note-----\n\nThe picture corresponding to the first example (and another one correct answer):\n\n[Image]\n\nIf you choose vertices $1, 5, 6$ then the path between $1$ and $5$ consists of edges $(1, 2), (2, 3), (3, 4), (4, 5)$, the path between $1$ and $6$ consists of edges $(1, 2), (2, 3), (3, 4), (4, 6)$ and the path between $5$ and $6$ consists of edges $(4, 5), (4, 6)$. The union of these paths is $(1, 2), (2, 3), (3, 4), (4, 5), (4, 6)$ so the answer is $5$. It can be shown that there is no better answer."} +{"id": "03036", "text": "Recently you have received two positive integer numbers $x$ and $y$. You forgot them, but you remembered a shuffled list containing all divisors of $x$ (including $1$ and $x$) and all divisors of $y$ (including $1$ and $y$). If $d$ is a divisor of both numbers $x$ and $y$ at the same time, there are two occurrences of $d$ in the list.\n\nFor example, if $x=4$ and $y=6$ then the given list can be any permutation of the list $[1, 2, 4, 1, 2, 3, 6]$. Some of the possible lists are: $[1, 1, 2, 4, 6, 3, 2]$, $[4, 6, 1, 1, 2, 3, 2]$ or $[1, 6, 3, 2, 4, 1, 2]$.\n\nYour problem is to restore suitable positive integer numbers $x$ and $y$ that would yield the same list of divisors (possibly in different order).\n\nIt is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers $x$ and $y$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 128$) \u2014 the number of divisors of $x$ and $y$.\n\nThe second line of the input contains $n$ integers $d_1, d_2, \\dots, d_n$ ($1 \\le d_i \\le 10^4$), where $d_i$ is either divisor of $x$ or divisor of $y$. If a number is divisor of both numbers $x$ and $y$ then there are two copies of this number in the list.\n\n\n-----Output-----\n\nPrint two positive integer numbers $x$ and $y$ \u2014 such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.\n\n\n-----Example-----\nInput\n10\n10 2 8 1 2 4 1 20 4 5\n\nOutput\n20 8"} +{"id": "03037", "text": "You are given a matrix $a$ of size $n \\times m$ consisting of integers.\n\nYou can choose no more than $\\left\\lfloor\\frac{m}{2}\\right\\rfloor$ elements in each row. Your task is to choose these elements in such a way that their sum is divisible by $k$ and this sum is the maximum.\n\nIn other words, you can choose no more than a half (rounded down) of elements in each row, you have to find the maximum sum of these elements divisible by $k$.\n\nNote that you can choose zero elements (and the sum of such set is $0$).\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n$, $m$ and $k$ ($1 \\le n, m, k \\le 70$) \u2014 the number of rows in the matrix, the number of columns in the matrix and the value of $k$. The next $n$ lines contain $m$ elements each, where the $j$-th element of the $i$-th row is $a_{i, j}$ ($1 \\le a_{i, j} \\le 70$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum sum divisible by $k$ you can obtain.\n\n\n-----Examples-----\nInput\n3 4 3\n1 2 3 4\n5 2 2 2\n7 1 1 4\n\nOutput\n24\n\nInput\n5 5 4\n1 2 4 2 1\n3 5 1 2 4\n1 5 7 1 2\n3 8 7 1 2\n8 4 7 1 6\n\nOutput\n56\n\n\n\n-----Note-----\n\nIn the first example, the optimal answer is $2$ and $4$ in the first row, $5$ and $2$ in the second row and $7$ and $4$ in the third row. The total sum is $2 + 4 + 5 + 2 + 7 + 4 = 24$."} +{"id": "03038", "text": "The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).\n\nYou are given a sequence $a$ consisting of $n$ integers.\n\nYou are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).\n\nFor example, for the sequence $[1, 2, 4, 3, 2]$ the answer is $4$ (you take $1$ and the sequence becomes $[2, 4, 3, 2]$, then you take the rightmost element $2$ and the sequence becomes $[2, 4, 3]$, then you take $3$ and the sequence becomes $[2, 4]$ and then you take $4$ and the sequence becomes $[2]$, the obtained increasing sequence is $[1, 2, 3, 4]$).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nIn the first line of the output print $k$ \u2014 the maximum number of elements in a strictly increasing sequence you can obtain.\n\nIn the second line print a string $s$ of length $k$, where the $j$-th character of this string $s_j$ should be 'L' if you take the leftmost element during the $j$-th move and 'R' otherwise. If there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n5\n1 2 4 3 2\n\nOutput\n4\nLRRR\n\nInput\n7\n1 3 5 6 5 4 2\n\nOutput\n6\nLRLRRR\n\nInput\n3\n2 2 2\n\nOutput\n1\nR\n\nInput\n4\n1 2 4 3\n\nOutput\n4\nLLRR\n\n\n\n-----Note-----\n\nThe first example is described in the problem statement."} +{"id": "03039", "text": "You are given a sequence $a_1, a_2, \\dots, a_n$ consisting of $n$ integers.\n\nYou can choose any non-negative integer $D$ (i.e. $D \\ge 0$), and for each $a_i$ you can:\n\n add $D$ (only once), i. e. perform $a_i := a_i + D$, or subtract $D$ (only once), i. e. perform $a_i := a_i - D$, or leave the value of $a_i$ unchanged. \n\nIt is possible that after an operation the value $a_i$ becomes negative.\n\nYour goal is to choose such minimum non-negative integer $D$ and perform changes in such a way, that all $a_i$ are equal (i.e. $a_1=a_2=\\dots=a_n$).\n\nPrint the required $D$ or, if it is impossible to choose such value $D$, print -1.\n\nFor example, for array $[2, 8]$ the value $D=3$ is minimum possible because you can obtain the array $[5, 5]$ if you will add $D$ to $2$ and subtract $D$ from $8$. And for array $[1, 4, 7, 7]$ the value $D=3$ is also minimum possible. You can add it to $1$ and subtract it from $7$ and obtain the array $[4, 4, 4, 4]$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$) \u2014 the sequence $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum non-negative integer value $D$ such that if you add this value to some $a_i$, subtract this value from some $a_i$ and leave some $a_i$ without changes, all obtained values become equal.\n\nIf it is impossible to choose such value $D$, print -1.\n\n\n-----Examples-----\nInput\n6\n1 4 4 7 4 1\n\nOutput\n3\n\nInput\n5\n2 2 5 2 5\n\nOutput\n3\n\nInput\n4\n1 3 3 7\n\nOutput\n-1\n\nInput\n2\n2 8\n\nOutput\n3"} +{"id": "03040", "text": "There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you will take a look from above and assume that the bottom left corner of the table has coordinates $(0, 0)$, and coordinate axes are left and bottom sides of the table, then the bottom left corner of the white sheet has coordinates $(x_1, y_1)$, and the top right \u2014 $(x_2, y_2)$.\n\nAfter that two black sheets of paper are placed on the table. Sides of both black sheets are also parallel to the sides of the table. Coordinates of the bottom left corner of the first black sheet are $(x_3, y_3)$, and the top right \u2014 $(x_4, y_4)$. Coordinates of the bottom left corner of the second black sheet are $(x_5, y_5)$, and the top right \u2014 $(x_6, y_6)$. [Image] Example of three rectangles. \n\nDetermine if some part of the white sheet can be seen from the above after the two black sheets are placed. The part of the white sheet can be seen if there is at least one point lying not strictly inside the white sheet and strictly outside of both black sheets.\n\n\n-----Input-----\n\nThe first line of the input contains four integers $x_1, y_1, x_2, y_2$ $(0 \\le x_1 < x_2 \\le 10^{6}, 0 \\le y_1 < y_2 \\le 10^{6})$ \u2014 coordinates of the bottom left and the top right corners of the white sheet.\n\nThe second line of the input contains four integers $x_3, y_3, x_4, y_4$ $(0 \\le x_3 < x_4 \\le 10^{6}, 0 \\le y_3 < y_4 \\le 10^{6})$ \u2014 coordinates of the bottom left and the top right corners of the first black sheet.\n\nThe third line of the input contains four integers $x_5, y_5, x_6, y_6$ $(0 \\le x_5 < x_6 \\le 10^{6}, 0 \\le y_5 < y_6 \\le 10^{6})$ \u2014 coordinates of the bottom left and the top right corners of the second black sheet.\n\nThe sides of each sheet of paper are parallel (perpendicular) to the coordinate axes.\n\n\n-----Output-----\n\nIf some part of the white sheet can be seen from the above after the two black sheets are placed, print \"YES\" (without quotes). Otherwise print \"NO\".\n\n\n-----Examples-----\nInput\n2 2 4 4\n1 1 3 5\n3 1 5 5\n\nOutput\nNO\n\nInput\n3 3 7 5\n0 0 4 6\n0 0 7 4\n\nOutput\nYES\n\nInput\n5 2 10 5\n3 1 7 6\n8 1 11 7\n\nOutput\nYES\n\nInput\n0 0 1000000 1000000\n0 0 499999 1000000\n500000 0 1000000 1000000\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example the white sheet is fully covered by black sheets.\n\nIn the second example the part of the white sheet can be seen after two black sheets are placed. For example, the point $(6.5, 4.5)$ lies not strictly inside the white sheet and lies strictly outside of both black sheets."} +{"id": "03041", "text": "Let's denote a function $f(x)$ in such a way: we add $1$ to $x$, then, while there is at least one trailing zero in the resulting number, we remove that zero. For example, $f(599) = 6$: $599 + 1 = 600 \\rightarrow 60 \\rightarrow 6$; $f(7) = 8$: $7 + 1 = 8$; $f(9) = 1$: $9 + 1 = 10 \\rightarrow 1$; $f(10099) = 101$: $10099 + 1 = 10100 \\rightarrow 1010 \\rightarrow 101$. \n\nWe say that some number $y$ is reachable from $x$ if we can apply function $f$ to $x$ some (possibly zero) times so that we get $y$ as a result. For example, $102$ is reachable from $10098$ because $f(f(f(10098))) = f(f(10099)) = f(101) = 102$; and any number is reachable from itself.\n\nYou are given a number $n$; your task is to count how many different numbers are reachable from $n$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 10^9$).\n\n\n-----Output-----\n\nPrint one integer: the number of different numbers that are reachable from $n$.\n\n\n-----Examples-----\nInput\n1098\n\nOutput\n20\n\nInput\n10\n\nOutput\n19\n\n\n\n-----Note-----\n\nThe numbers that are reachable from $1098$ are:\n\n$1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1098, 1099$."} +{"id": "03042", "text": "There are $n$ friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.\n\nFor each friend the value $f_i$ is known: it is either $f_i = 0$ if the $i$-th friend doesn't know whom he wants to give the gift to or $1 \\le f_i \\le n$ if the $i$-th friend wants to give the gift to the friend $f_i$.\n\nYou want to fill in the unknown values ($f_i = 0$) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.\n\nIf there are several answers, you can print any.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of friends.\n\nThe second line of the input contains $n$ integers $f_1, f_2, \\dots, f_n$ ($0 \\le f_i \\le n$, $f_i \\ne i$, all $f_i \\ne 0$ are distinct), where $f_i$ is the either $f_i = 0$ if the $i$-th friend doesn't know whom he wants to give the gift to or $1 \\le f_i \\le n$ if the $i$-th friend wants to give the gift to the friend $f_i$. It is also guaranteed that there is at least two values $f_i = 0$.\n\n\n-----Output-----\n\nPrint $n$ integers $nf_1, nf_2, \\dots, nf_n$, where $nf_i$ should be equal to $f_i$ if $f_i \\ne 0$ or the number of friend whom the $i$-th friend wants to give the gift to. All values $nf_i$ should be distinct, $nf_i$ cannot be equal to $i$. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself.\n\nIf there are several answers, you can print any.\n\n\n-----Examples-----\nInput\n5\n5 0 0 2 4\n\nOutput\n5 3 1 2 4 \n\nInput\n7\n7 0 0 1 4 0 6\n\nOutput\n7 3 2 1 4 5 6 \n\nInput\n7\n7 4 0 3 0 5 1\n\nOutput\n7 4 2 3 6 5 1 \n\nInput\n5\n2 1 0 0 0\n\nOutput\n2 1 4 5 3"} +{"id": "03043", "text": "You are given an array $a$ consisting of $n$ integer numbers.\n\nYou have to color this array in $k$ colors in such a way that: Each element of the array should be colored in some color; For each $i$ from $1$ to $k$ there should be at least one element colored in the $i$-th color in the array; For each $i$ from $1$ to $k$ all elements colored in the $i$-th color should be distinct. \n\nObviously, such coloring might be impossible. In this case, print \"NO\". Otherwise print \"YES\" and any coloring (i.e. numbers $c_1, c_2, \\dots c_n$, where $1 \\le c_i \\le k$ and $c_i$ is the color of the $i$-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 5000$) \u2014 the length of the array $a$ and the number of colors, respectively.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 5000$) \u2014 elements of the array $a$.\n\n\n-----Output-----\n\nIf there is no answer, print \"NO\". Otherwise print \"YES\" and any coloring (i.e. numbers $c_1, c_2, \\dots c_n$, where $1 \\le c_i \\le k$ and $c_i$ is the color of the $i$-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n4 2\n1 2 2 3\n\nOutput\nYES\n1 1 2 2\n\nInput\n5 2\n3 2 1 2 3\n\nOutput\nYES\n2 1 1 2 1\n\nInput\n5 2\n2 1 1 2 1\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example the answer $2~ 1~ 2~ 1$ is also acceptable.\n\nIn the second example the answer $1~ 1~ 1~ 2~ 2$ is also acceptable.\n\nThere exist other acceptable answers for both examples."} +{"id": "03044", "text": "You are given a huge decimal number consisting of $n$ digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1.\n\nYou may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change 0 to 1 or 1 to 0. It is possible that after some operation you can obtain a number with leading zeroes, but it does not matter for this problem.\n\nYou are also given two integers $0 \\le y < x < n$. Your task is to calculate the minimum number of operations you should perform to obtain the number that has remainder $10^y$ modulo $10^x$. In other words, the obtained number should have remainder $10^y$ when divided by $10^x$.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n, x, y$ ($0 \\le y < x < n \\le 2 \\cdot 10^5$) \u2014 the length of the number and the integers $x$ and $y$, respectively.\n\nThe second line of the input contains one decimal number consisting of $n$ digits, each digit of this number is either 0 or 1. It is guaranteed that the first digit of the number is 1.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of operations you should perform to obtain the number having remainder $10^y$ modulo $10^x$. In other words, the obtained number should have remainder $10^y$ when divided by $10^x$.\n\n\n-----Examples-----\nInput\n11 5 2\n11010100101\n\nOutput\n1\n\nInput\n11 5 1\n11010100101\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example the number will be $11010100100$ after performing one operation. It has remainder $100$ modulo $100000$.\n\nIn the second example the number will be $11010100010$ after performing three operations. It has remainder $10$ modulo $100000$."} +{"id": "03045", "text": "You are given an array $a$ consisting of $n$ integers.\n\nYour task is to determine if $a$ has some subsequence of length at least $3$ that is a palindrome.\n\nRecall that an array $b$ is called a subsequence of the array $a$ if $b$ can be obtained by removing some (possibly, zero) elements from $a$ (not necessarily consecutive) without changing the order of remaining elements. For example, $[2]$, $[1, 2, 1, 3]$ and $[2, 3]$ are subsequences of $[1, 2, 1, 3]$, but $[1, 1, 2]$ and $[4]$ are not.\n\nAlso, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $a$ of length $n$ is the palindrome if $a_i = a_{n - i - 1}$ for all $i$ from $1$ to $n$. For example, arrays $[1234]$, $[1, 2, 1]$, $[1, 3, 2, 2, 3, 1]$ and $[10, 100, 10]$ are palindromes, but arrays $[1, 2]$ and $[1, 2, 3, 1]$ are not.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nNext $2t$ lines describe test cases. The first line of the test case contains one integer $n$ ($3 \\le n \\le 5000$) \u2014 the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$), where $a_i$ is the $i$-th element of $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $5000$ ($\\sum n \\le 5000$).\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 \"YES\" (without quotes) if $a$ has some subsequence of length at least $3$ that is a palindrome and \"NO\" otherwise.\n\n\n-----Example-----\nInput\n5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5\n\nOutput\nYES\nYES\nNO\nYES\nNO\n\n\n\n-----Note-----\n\nIn the first test case of the example, the array $a$ has a subsequence $[1, 2, 1]$ which is a palindrome.\n\nIn the second test case of the example, the array $a$ has two subsequences of length $3$ which are palindromes: $[2, 3, 2]$ and $[2, 2, 2]$.\n\nIn the third test case of the example, the array $a$ has no subsequences of length at least $3$ which are palindromes.\n\nIn the fourth test case of the example, the array $a$ has one subsequence of length $4$ which is a palindrome: $[1, 2, 2, 1]$ (and has two subsequences of length $3$ which are palindromes: both are $[1, 2, 1]$).\n\nIn the fifth test case of the example, the array $a$ has no subsequences of length at least $3$ which are palindromes."} +{"id": "03046", "text": "You are given a long decimal number $a$ consisting of $n$ digits from $1$ to $9$. You also have a function $f$ that maps every digit from $1$ to $9$ to some (possibly the same) digit from $1$ to $9$.\n\nYou can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $a$, and replace each digit $x$ from this segment with $f(x)$. For example, if $a = 1337$, $f(1) = 1$, $f(3) = 5$, $f(7) = 3$, and you choose the segment consisting of three rightmost digits, you get $1553$ as the result.\n\nWhat is the maximum possible number you can obtain applying this operation no more than once?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of digits in $a$.\n\nThe second line contains a string of $n$ characters, denoting the number $a$. Each character is a decimal digit from $1$ to $9$.\n\nThe third line contains exactly $9$ integers $f(1)$, $f(2)$, ..., $f(9)$ ($1 \\le f(i) \\le 9$).\n\n\n-----Output-----\n\nPrint the maximum number you can get after applying the operation described in the statement no more than once.\n\n\n-----Examples-----\nInput\n4\n1337\n1 2 5 4 6 6 3 1 9\n\nOutput\n1557\n\nInput\n5\n11111\n9 8 7 6 5 4 3 2 1\n\nOutput\n99999\n\nInput\n2\n33\n1 1 1 1 1 1 1 1 1\n\nOutput\n33"} +{"id": "03047", "text": "You are given three integers $a \\le b \\le c$.\n\nIn one move, you can add $+1$ or $-1$ to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this operation several times with one number. Note that you cannot make non-positive numbers using such operations.\n\nYou have to perform the minimum number of such operations in order to obtain three integers $A \\le B \\le C$ such that $B$ is divisible by $A$ and $C$ is divisible by $B$.\n\nYou have to answer $t$ independent test cases. \n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThe next $t$ lines describe test cases. Each test case is given on a separate line as three space-separated integers $a, b$ and $c$ ($1 \\le a \\le b \\le c \\le 10^4$).\n\n\n-----Output-----\n\nFor each test case, print the answer. In the first line print $res$ \u2014 the minimum number of operations you have to perform to obtain three integers $A \\le B \\le C$ such that $B$ is divisible by $A$ and $C$ is divisible by $B$. On the second line print any suitable triple $A, B$ and $C$.\n\n\n-----Example-----\nInput\n8\n1 2 3\n123 321 456\n5 10 15\n15 18 21\n100 100 101\n1 22 29\n3 19 38\n6 30 46\n\nOutput\n1\n1 1 3\n102\n114 228 456\n4\n4 8 16\n6\n18 18 18\n1\n100 100 100\n7\n1 22 22\n2\n1 19 38\n8\n6 24 48"} +{"id": "03048", "text": "You are given an array $a$ consisting of $n$ integer numbers.\n\nLet instability of the array be the following value: $\\max\\limits_{i = 1}^{n} a_i - \\min\\limits_{i = 1}^{n} a_i$.\n\nYou have to remove exactly one element from this array to minimize instability of the resulting $(n-1)$-elements array. Your task is to calculate the minimum possible instability.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 10^5$) \u2014 the number of elements in the array $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^5$) \u2014 elements of the array $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible instability of the array if you have to remove exactly one element from the array $a$.\n\n\n-----Examples-----\nInput\n4\n1 3 3 7\n\nOutput\n2\n\nInput\n2\n1 100000\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example you can remove $7$ then instability of the remaining array will be $3 - 1 = 2$.\n\nIn the second example you can remove either $1$ or $100000$ then instability of the remaining array will be $100000 - 100000 = 0$ and $1 - 1 = 0$ correspondingly."} +{"id": "03049", "text": "Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$.\n\nThere are three values about each exam: $s_i$ \u2014 the day, when questions for the $i$-th exam will be published, $d_i$ \u2014 the day of the $i$-th exam ($s_i < d_i$), $c_i$ \u2014 number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive. \n\nThere are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \\le j < d_i$.\n\nIt is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days.\n\nFind the schedule for Petya to prepare for all exams and pass them, or report that it is impossible.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ $(2 \\le n \\le 100, 1 \\le m \\le n)$ \u2014 the number of days and the number of exams.\n\nEach of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \\le s_i < d_i \\le n, 1 \\le c_i \\le n)$ \u2014 the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam. \n\nGuaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given.\n\n\n-----Output-----\n\nIf Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is: $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), zero, if in the $j$-th day Petya will have a rest, $i$ ($1 \\le i \\le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it).\n\nAssume that the exams are numbered in order of appearing in the input, starting from $1$.\n\nIf there are multiple schedules, print any of them.\n\n\n-----Examples-----\nInput\n5 2\n1 3 1\n1 5 1\n\nOutput\n1 2 3 0 3 \n\nInput\n3 2\n1 3 1\n1 2 1\n\nOutput\n-1\n\nInput\n10 3\n4 7 2\n1 10 3\n8 9 1\n\nOutput\n2 2 2 1 1 0 4 3 4 4 \n\n\n\n-----Note-----\n\nIn the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams.\n\nIn the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams."} +{"id": "03050", "text": "Polycarp plays \"Game 23\". Initially he has a number $n$ and his goal is to transform it to $m$. In one move, he can multiply $n$ by $2$ or multiply $n$ by $3$. He can perform any number of moves.\n\nPrint the number of moves needed to transform $n$ to $m$. Print -1 if it is impossible to do so.\n\nIt is easy to prove that any way to transform $n$ to $m$ contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).\n\n\n-----Input-----\n\nThe only line of the input contains two integers $n$ and $m$ ($1 \\le n \\le m \\le 5\\cdot10^8$).\n\n\n-----Output-----\n\nPrint the number of moves to transform $n$ to $m$, or -1 if there is no solution.\n\n\n-----Examples-----\nInput\n120 51840\n\nOutput\n7\n\nInput\n42 42\n\nOutput\n0\n\nInput\n48 72\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example, the possible sequence of moves is: $120 \\rightarrow 240 \\rightarrow 720 \\rightarrow 1440 \\rightarrow 4320 \\rightarrow 12960 \\rightarrow 25920 \\rightarrow 51840.$ The are $7$ steps in total.\n\nIn the second example, no moves are needed. Thus, the answer is $0$.\n\nIn the third example, it is impossible to transform $48$ to $72$."} +{"id": "03051", "text": "You are given a string $t$ consisting of $n$ lowercase Latin letters and an integer number $k$.\n\nLet's define a substring of some string $s$ with indices from $l$ to $r$ as $s[l \\dots r]$.\n\nYour task is to construct such string $s$ of minimum possible length that there are exactly $k$ positions $i$ such that $s[i \\dots i + n - 1] = t$. In other words, your task is to construct such string $s$ of minimum possible length that there are exactly $k$ substrings of $s$ equal to $t$.\n\nIt is guaranteed that the answer is always unique.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n, k \\le 50$) \u2014 the length of the string $t$ and the number of substrings.\n\nThe second line of the input contains the string $t$ consisting of exactly $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nPrint such string $s$ of minimum possible length that there are exactly $k$ substrings of $s$ equal to $t$.\n\nIt is guaranteed that the answer is always unique.\n\n\n-----Examples-----\nInput\n3 4\naba\n\nOutput\nababababa\n\nInput\n3 2\ncat\n\nOutput\ncatcat"} +{"id": "03052", "text": "Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array $a=[1, 3, 3, 7]$ is good because there is the element $a_4=7$ which equals to the sum $1 + 3 + 3$.\n\nYou are given an array $a$ consisting of $n$ integers. Your task is to print all indices $j$ of this array such that after removing the $j$-th element from the array it will be good (let's call such indices nice).\n\nFor example, if $a=[8, 3, 5, 2]$, the nice indices are $1$ and $4$: if you remove $a_1$, the array will look like $[3, 5, 2]$ and it is good; if you remove $a_4$, the array will look like $[8, 3, 5]$ and it is good. \n\nYou have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in the array $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^6$) \u2014 elements of the array $a$.\n\n\n-----Output-----\n\nIn the first line print one integer $k$ \u2014 the number of indices $j$ of the array $a$ such that after removing the $j$-th element from the array it will be good (i.e. print the number of the nice indices).\n\nIn the second line print $k$ distinct integers $j_1, j_2, \\dots, j_k$ in any order \u2014 nice indices of the array $a$.\n\nIf there are no such indices in the array $a$, just print $0$ in the first line and leave the second line empty or do not print it at all.\n\n\n-----Examples-----\nInput\n5\n2 5 1 2 2\n\nOutput\n3\n4 1 5\nInput\n4\n8 3 5 2\n\nOutput\n2\n1 4 \n\nInput\n5\n2 1 2 4 3\n\nOutput\n0\n\n\n\n\n-----Note-----\n\nIn the first example you can remove any element with the value $2$ so the array will look like $[5, 1, 2, 2]$. The sum of this array is $10$ and there is an element equals to the sum of remaining elements ($5 = 1 + 2 + 2$).\n\nIn the second example you can remove $8$ so the array will look like $[3, 5, 2]$. The sum of this array is $10$ and there is an element equals to the sum of remaining elements ($5 = 3 + 2$). You can also remove $2$ so the array will look like $[8, 3, 5]$. The sum of this array is $16$ and there is an element equals to the sum of remaining elements ($8 = 3 + 5$).\n\nIn the third example you cannot make the given array good by removing exactly one element."} +{"id": "03053", "text": "The only difference between the easy and the hard versions is constraints.\n\nA subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string \"abaca\" the following strings are subsequences: \"abaca\", \"aba\", \"aaa\", \"a\" and \"\" (empty string). But the following strings are not subsequences: \"aabaca\", \"cb\" and \"bcaa\".\n\nYou are given a string $s$ consisting of $n$ lowercase Latin letters.\n\nIn one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).\n\nYour task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n \\le 100, 1 \\le k \\le 10^{12}$) \u2014 the length of the string and the size of the set, correspondingly.\n\nThe second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nPrint one integer \u2014 if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it.\n\n\n-----Examples-----\nInput\n4 5\nasdf\n\nOutput\n4\n\nInput\n5 6\naaaaa\n\nOutput\n15\n\nInput\n5 7\naaaaa\n\nOutput\n-1\n\nInput\n10 100\najihiushda\n\nOutput\n233\n\n\n\n-----Note-----\n\nIn the first example we can generate $S$ = { \"asdf\", \"asd\", \"adf\", \"asf\", \"sdf\" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$."} +{"id": "03054", "text": "You are given an undirected unweighted connected graph consisting of $n$ vertices and $m$ edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.\n\nYour task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label $1$ on it) is equal to $D$ (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $m$ and $D$ ($2 \\le n \\le 2 \\cdot 10^5$, $n - 1 \\le m \\le min(2 \\cdot 10^5, \\frac{n(n-1)}{2}), 1 \\le D < n$) \u2014 the number of vertices, the number of edges and required degree of the first vertex, respectively.\n\nThe following $m$ lines denote edges: edge $i$ is represented by a pair of integers $v_i$, $u_i$ ($1 \\le v_i, u_i \\le n$, $u_i \\ne v_i$), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair ($v_i, u_i$) there are no other pairs ($v_i, u_i$) or ($u_i, v_i$) in the list of edges, and for each pair $(v_i, u_i)$ the condition $v_i \\ne u_i$ is satisfied.\n\n\n-----Output-----\n\nIf there is no spanning tree satisfying the condition from the problem statement, print \"NO\" in the first line.\n\nOtherwise print \"YES\" in the first line and then print $n-1$ lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label $1$ on it) is equal to $D$. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge $(v, u)$ is considered the same as the edge $(u, v)$).\n\nIf there are multiple possible answers, print any of them.\n\n\n-----Examples-----\nInput\n4 5 1\n1 2\n1 3\n1 4\n2 3\n3 4\n\nOutput\nYES\n2 1\n2 3\n3 4\n\nInput\n4 5 3\n1 2\n1 3\n1 4\n2 3\n3 4\n\nOutput\nYES\n1 2\n1 3\n4 1\n\nInput\n4 4 3\n1 2\n1 4\n2 3\n3 4\n\nOutput\nNO\n\n\n\n-----Note-----\n\nThe picture corresponding to the first and second examples: [Image]\n\nThe picture corresponding to the third example: [Image]"} +{"id": "03055", "text": "Polycarp is going to participate in the contest. It starts at $h_1:m_1$ and ends at $h_2:m_2$. It is guaranteed that the contest lasts an even number of minutes (i.e. $m_1 \\% 2 = m_2 \\% 2$, where $x \\% y$ is $x$ modulo $y$). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes.\n\nPolycarp wants to know the time of the midpoint of the contest. For example, if the contest lasts from $10:00$ to $11:00$ then the answer is $10:30$, if the contest lasts from $11:10$ to $11:12$ then the answer is $11:11$.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $h_1$ and $m_1$ in the format hh:mm.\n\nThe second line of the input contains two integers $h_2$ and $m_2$ in the same format (hh:mm).\n\nIt is guaranteed that $0 \\le h_1, h_2 \\le 23$ and $0 \\le m_1, m_2 \\le 59$.\n\nIt is guaranteed that the contest lasts an even number of minutes (i.e. $m_1 \\% 2 = m_2 \\% 2$, where $x \\% y$ is $x$ modulo $y$). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes.\n\n\n-----Output-----\n\nPrint two integers $h_3$ and $m_3$ ($0 \\le h_3 \\le 23, 0 \\le m_3 \\le 59$) corresponding to the midpoint of the contest in the format hh:mm. Print each number as exactly two digits (prepend a number with leading zero if needed), separate them with ':'.\n\n\n-----Examples-----\nInput\n10:00\n11:00\n\nOutput\n10:30\n\nInput\n11:10\n11:12\n\nOutput\n11:11\n\nInput\n01:02\n03:02\n\nOutput\n02:02"} +{"id": "03056", "text": "-----Input-----\n\nThe input contains a single integer a (1 \u2264 a \u2264 64).\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1\n\nInput\n4\n\nOutput\n2\n\nInput\n27\n\nOutput\n5\n\nInput\n42\n\nOutput\n6"} +{"id": "03057", "text": "You are given $n$ segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.\n\nThe intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn't empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or $0$ in case the intersection is an empty set.\n\nFor example, the intersection of segments $[1;5]$ and $[3;10]$ is $[3;5]$ (length $2$), the intersection of segments $[1;5]$ and $[5;7]$ is $[5;5]$ (length $0$) and the intersection of segments $[1;5]$ and $[6;6]$ is an empty set (length $0$).\n\nYour task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining $(n - 1)$ segments has the maximal possible length.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 3 \\cdot 10^5$) \u2014 the number of segments in the sequence.\n\nEach of the next $n$ lines contains two integers $l_i$ and $r_i$ ($0 \\le l_i \\le r_i \\le 10^9$) \u2014 the description of the $i$-th segment.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximal possible length of the intersection of $(n - 1)$ remaining segments after you remove exactly one segment from the sequence.\n\n\n-----Examples-----\nInput\n4\n1 3\n2 6\n0 4\n3 3\n\nOutput\n1\n\nInput\n5\n2 6\n1 3\n0 4\n1 20\n0 4\n\nOutput\n2\n\nInput\n3\n4 5\n1 2\n9 20\n\nOutput\n0\n\nInput\n2\n3 10\n1 5\n\nOutput\n7\n\n\n\n-----Note-----\n\nIn the first example you should remove the segment $[3;3]$, the intersection will become $[2;3]$ (length $1$). Removing any other segment will result in the intersection $[3;3]$ (length $0$).\n\nIn the second example you should remove the segment $[1;3]$ or segment $[2;6]$, the intersection will become $[2;4]$ (length $2$) or $[1;3]$ (length $2$), respectively. Removing any other segment will result in the intersection $[2;3]$ (length $1$).\n\nIn the third example the intersection will become an empty set no matter the segment you remove.\n\nIn the fourth example you will get the intersection $[3;10]$ (length $7$) if you remove the segment $[1;5]$ or the intersection $[1;5]$ (length $4$) if you remove the segment $[3;10]$."} +{"id": "03058", "text": "Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.\n\nThe current state of the wall can be respresented by a sequence $a$ of $n$ integers, with $a_i$ being the height of the $i$-th part of the wall.\n\nVova can only use $2 \\times 1$ bricks to put in the wall (he has infinite supply of them, however).\n\nVova can put bricks only horizontally on the neighbouring parts of the wall of equal height. It means that if for some $i$ the current height of part $i$ is the same as for part $i + 1$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $1$ of the wall or to the right of part $n$ of it).\n\nNote that Vova can't put bricks vertically.\n\nVova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. \n\nCan Vova complete the wall using any amount of bricks (possibly zero)?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of parts in the wall.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) \u2014 the initial heights of the parts of the wall.\n\n\n-----Output-----\n\nPrint \"YES\" if Vova can complete the wall using any amount of bricks (possibly zero).\n\nPrint \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n5\n2 1 1 2 5\n\nOutput\nYES\n\nInput\n3\n4 5 3\n\nOutput\nNO\n\nInput\n2\n10 10\n\nOutput\nYES\n\n\n\n-----Note-----\n\nIn the first example Vova can put a brick on parts 2 and 3 to make the wall $[2, 2, 2, 2, 5]$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $[5, 5, 5, 5, 5]$.\n\nIn the second example Vova can put no bricks in the wall.\n\nIn the third example the wall is already complete."} +{"id": "03059", "text": "The only difference between the easy and the hard versions is constraints.\n\nA subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string \"abaca\" the following strings are subsequences: \"abaca\", \"aba\", \"aaa\", \"a\" and \"\" (empty string). But the following strings are not subsequences: \"aabaca\", \"cb\" and \"bcaa\".\n\nYou are given a string $s$ consisting of $n$ lowercase Latin letters.\n\nIn one move you can take any subsequence $t$ of the given string and add it to the set $S$. The set $S$ can't contain duplicates. This move costs $n - |t|$, where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).\n\nYour task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n, k \\le 100$) \u2014 the length of the string and the size of the set, correspondingly.\n\nThe second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nPrint one integer \u2014 if it is impossible to obtain the set $S$ of size $k$, print -1. Otherwise, print the minimum possible total cost to do it.\n\n\n-----Examples-----\nInput\n4 5\nasdf\n\nOutput\n4\n\nInput\n5 6\naaaaa\n\nOutput\n15\n\nInput\n5 7\naaaaa\n\nOutput\n-1\n\nInput\n10 100\najihiushda\n\nOutput\n233\n\n\n\n-----Note-----\n\nIn the first example we can generate $S$ = { \"asdf\", \"asd\", \"adf\", \"asf\", \"sdf\" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$. So the total cost of $S$ is $4$."} +{"id": "03060", "text": "Polycarp has a cat and his cat is a real gourmet! Dependent on a day of the week he eats certain type of food: on Mondays, Thursdays and Sundays he eats fish food; on Tuesdays and Saturdays he eats rabbit stew; on other days of week he eats chicken stake. \n\nPolycarp plans to go on a trip and already packed his backpack. His backpack contains: $a$ daily rations of fish food; $b$ daily rations of rabbit stew; $c$ daily rations of chicken stakes. \n\nPolycarp has to choose such day of the week to start his trip that his cat can eat without additional food purchases as long as possible. Print the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally.\n\n\n-----Input-----\n\nThe first line of the input contains three positive integers $a$, $b$ and $c$ ($1 \\le a, b, c \\le 7\\cdot10^8$) \u2014 the number of daily rations of fish food, rabbit stew and chicken stakes in Polycarps backpack correspondingly.\n\n\n-----Output-----\n\nPrint the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally.\n\n\n-----Examples-----\nInput\n2 1 1\n\nOutput\n4\n\nInput\n3 2 2\n\nOutput\n7\n\nInput\n1 100 1\n\nOutput\n3\n\nInput\n30 20 10\n\nOutput\n39\n\n\n\n-----Note-----\n\nIn the first example the best day for start of the trip is Sunday. In this case, during Sunday and Monday the cat will eat fish food, during Tuesday \u2014 rabbit stew and during Wednesday \u2014 chicken stake. So, after four days of the trip all food will be eaten.\n\nIn the second example Polycarp can start his trip in any day of the week. In any case there are food supplies only for one week in Polycarps backpack.\n\nIn the third example Polycarp can start his trip in any day, excluding Wednesday, Saturday and Sunday. In this case, the cat will eat three different dishes in three days. Nevertheless that after three days of a trip there will be $99$ portions of rabbit stew in a backpack, can cannot eat anything in fourth day of a trip."} +{"id": "03061", "text": "Masha has $n$ types of tiles of size $2 \\times 2$. Each cell of the tile contains one integer. Masha has an infinite number of tiles of each type.\n\nMasha decides to construct the square of size $m \\times m$ consisting of the given tiles. This square also has to be a symmetric with respect to the main diagonal matrix, and each cell of this square has to be covered with exactly one tile cell, and also sides of tiles should be parallel to the sides of the square. All placed tiles cannot intersect with each other. Also, each tile should lie inside the square. See the picture in Notes section for better understanding.\n\nSymmetric with respect to the main diagonal matrix is such a square $s$ that for each pair $(i, j)$ the condition $s[i][j] = s[j][i]$ holds. I.e. it is true that the element written in the $i$-row and $j$-th column equals to the element written in the $j$-th row and $i$-th column.\n\nYour task is to determine if Masha can construct a square of size $m \\times m$ which is a symmetric matrix and consists of tiles she has. Masha can use any number of tiles of each type she has to construct the square. Note that she can not rotate tiles, she can only place them in the orientation they have in the input.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains two integers $n$ and $m$ ($1 \\le n \\le 100$, $1 \\le m \\le 100$) \u2014 the number of types of tiles and the size of the square Masha wants to construct.\n\nThe next $2n$ lines of the test case contain descriptions of tiles types. Types of tiles are written one after another, each type is written on two lines. \n\nThe first line of the description contains two positive (greater than zero) integers not exceeding $100$ \u2014 the number written in the top left corner of the tile and the number written in the top right corner of the tile of the current type. The second line of the description contains two positive (greater than zero) integers not exceeding $100$ \u2014 the number written in the bottom left corner of the tile and the number written in the bottom right corner of the tile of the current type.\n\nIt is forbidden to rotate tiles, it is only allowed to place them in the orientation they have in the input.\n\n\n-----Output-----\n\nFor each test case print the answer: \"YES\" (without quotes) if Masha can construct the square of size $m \\times m$ which is a symmetric matrix. Otherwise, print \"NO\" (withtout quotes).\n\n\n-----Example-----\nInput\n6\n3 4\n1 2\n5 6\n5 7\n7 4\n8 9\n9 8\n2 5\n1 1\n1 1\n2 2\n2 2\n1 100\n10 10\n10 10\n1 2\n4 5\n8 4\n2 2\n1 1\n1 1\n1 2\n3 4\n1 2\n1 1\n1 1\n\nOutput\nYES\nNO\nYES\nNO\nYES\nYES\n\n\n\n-----Note-----\n\nThe first test case of the input has three types of tiles, they are shown on the picture below. [Image] \n\nMasha can construct, for example, the following square of size $4 \\times 4$ which is a symmetric matrix: $\\left. \\begin{array}{|c|c|c|c|} \\hline 5 & {7} & {8} & {9} \\\\ \\hline 7 & {4} & {9} & {8} \\\\ \\hline 8 & {9} & {5} & {7} \\\\ \\hline 9 & {8} & {7} & {4} \\\\ \\hline \\end{array} \\right.$"} +{"id": "03062", "text": "You are given an integer sequence $1, 2, \\dots, n$. You have to divide it into two sets $A$ and $B$ in such a way that each element belongs to exactly one set and $|sum(A) - sum(B)|$ is minimum possible.\n\nThe value $|x|$ is the absolute value of $x$ and $sum(S)$ is the sum of elements of the set $S$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^9$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible value of $|sum(A) - sum(B)|$ if you divide the initial sequence $1, 2, \\dots, n$ into two sets $A$ and $B$.\n\n\n-----Examples-----\nInput\n3\n\nOutput\n0\n\nInput\n5\n\nOutput\n1\n\nInput\n6\n\nOutput\n1\n\n\n\n-----Note-----\n\nSome (not all) possible answers to examples:\n\nIn the first example you can divide the initial sequence into sets $A = \\{1, 2\\}$ and $B = \\{3\\}$ so the answer is $0$.\n\nIn the second example you can divide the initial sequence into sets $A = \\{1, 3, 4\\}$ and $B = \\{2, 5\\}$ so the answer is $1$.\n\nIn the third example you can divide the initial sequence into sets $A = \\{1, 4, 5\\}$ and $B = \\{2, 3, 6\\}$ so the answer is $1$."} +{"id": "03063", "text": "You are given a bracket sequence $s$ (not necessarily a regular one). A bracket sequence is a string containing only characters '(' and ')'.\n\nA regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences \"()()\" and \"(())\" are regular (the resulting expressions are: \"(1)+(1)\" and \"((1+1)+1)\"), and \")(\", \"(\" and \")\" are not.\n\nYour problem is to calculate the number of regular bracket sequences of length $2n$ containing the given bracket sequence $s$ as a substring (consecutive sequence of characters) modulo $10^9+7$ ($1000000007$).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the half-length of the resulting regular bracket sequences (the resulting sequences must have length equal to $2n$).\n\nThe second line of the input contains one string $s$ ($1 \\le |s| \\le 200$) \u2014 the string $s$ that should be a substring in each of the resulting regular bracket sequences ($|s|$ is the length of $s$).\n\n\n-----Output-----\n\nPrint only one integer \u2014 the number of regular bracket sequences containing the given bracket sequence $s$ as a substring. Since this number can be huge, print it modulo $10^9+7$ ($1000000007$).\n\n\n-----Examples-----\nInput\n5\n()))()\n\nOutput\n5\n\nInput\n3\n(()\n\nOutput\n4\n\nInput\n2\n(((\n\nOutput\n0\n\n\n\n-----Note-----\n\nAll regular bracket sequences satisfying the conditions above for the first example: \"(((()))())\"; \"((()()))()\"; \"((()))()()\"; \"(()(()))()\"; \"()((()))()\". \n\nAll regular bracket sequences satisfying the conditions above for the second example: \"((()))\"; \"(()())\"; \"(())()\"; \"()(())\". \n\nAnd there is no regular bracket sequences of length $4$ containing \"(((\" as a substring in the third example."} +{"id": "03064", "text": "You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.\n\nIn one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.\n\nWhat is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.\n\n\n-----Output-----\n\nIf it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.\n\nNote that you can swap only adjacent digits in the given number.\n\n\n-----Examples-----\nInput\n5071\n\nOutput\n4\n\nInput\n705\n\nOutput\n1\n\nInput\n1241367\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example one of the possible sequences of moves is 5071 $\\rightarrow$ 5701 $\\rightarrow$ 7501 $\\rightarrow$ 7510 $\\rightarrow$ 7150."} +{"id": "03065", "text": "This is a hard version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.\n\nYou are given a string $s$ consisting of $n$ lowercase Latin letters.\n\nYou have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$).\n\nAfter coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.\n\nThe goal is to make the string sorted, i.e. all characters should be in alphabetical order.\n\nYour task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of $s$.\n\nThe second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nIn the first line print one integer $res$ ($1 \\le res \\le n$) \u2014 the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps.\n\nIn the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array $c$ of length $n$, where $1 \\le c_i \\le res$ and $c_i$ means the color of the $i$-th character.\n\n\n-----Examples-----\nInput\n9\nabacbecfd\n\nOutput\n2\n1 1 2 1 2 1 2 1 2 \n\nInput\n8\naaabbcbb\n\nOutput\n2\n1 2 1 2 1 2 1 1\n\nInput\n7\nabcdedc\n\nOutput\n3\n1 1 1 1 1 2 3 \n\nInput\n5\nabcde\n\nOutput\n1\n1 1 1 1 1"} +{"id": "03066", "text": "You are given $n$ strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings.\n\nString $a$ is a substring of string $b$ if it is possible to choose several consecutive letters in $b$ in such a way that they form $a$. For example, string \"for\" is contained as a substring in strings \"codeforces\", \"for\" and \"therefore\", but is not contained as a substring in strings \"four\", \"fofo\" and \"rof\".\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 100$) \u2014 the number of strings.\n\nThe next $n$ lines contain the given strings. The number of letters in each string is from $1$ to $100$, inclusive. Each string consists of lowercase English letters.\n\nSome strings might be equal.\n\n\n-----Output-----\n\nIf it is impossible to reorder $n$ given strings in required order, print \"NO\" (without quotes).\n\nOtherwise print \"YES\" (without quotes) and $n$ given strings in required order.\n\n\n-----Examples-----\nInput\n5\na\naba\nabacaba\nba\naba\n\nOutput\nYES\na\nba\naba\naba\nabacaba\n\nInput\n5\na\nabacaba\nba\naba\nabab\n\nOutput\nNO\n\nInput\n3\nqwerty\nqwerty\nqwerty\n\nOutput\nYES\nqwerty\nqwerty\nqwerty\n\n\n\n-----Note-----\n\nIn the second example you cannot reorder the strings because the string \"abab\" is not a substring of the string \"abacaba\"."} +{"id": "03067", "text": "Mishka started participating in a programming contest. There are $n$ problems in the contest. Mishka's problem-solving skill is equal to $k$.\n\nMishka arranges all problems from the contest into a list. Because of his weird principles, Mishka only solves problems from one of the ends of the list. Every time, he chooses which end (left or right) he will solve the next problem from. Thus, each problem Mishka solves is either the leftmost or the rightmost problem in the list.\n\nMishka cannot solve a problem with difficulty greater than $k$. When Mishka solves the problem, it disappears from the list, so the length of the list decreases by $1$. Mishka stops when he is unable to solve any problem from any end of the list.\n\nHow many problems can Mishka solve?\n\n\n-----Input-----\n\nThe first line of input contains two integers $n$ and $k$ ($1 \\le n, k \\le 100$) \u2014 the number of problems in the contest and Mishka's problem-solving skill.\n\nThe second line of input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$), where $a_i$ is the difficulty of the $i$-th problem. The problems are given in order from the leftmost to the rightmost in the list.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of problems Mishka can solve.\n\n\n-----Examples-----\nInput\n8 4\n4 2 3 1 5 1 6 4\n\nOutput\n5\n\nInput\n5 2\n3 1 2 1 3\n\nOutput\n0\n\nInput\n5 100\n12 34 55 43 21\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example, Mishka can solve problems in the following order: $[4, 2, 3, 1, 5, 1, 6, 4] \\rightarrow [2, 3, 1, 5, 1, 6, 4] \\rightarrow [2, 3, 1, 5, 1, 6] \\rightarrow [3, 1, 5, 1, 6] \\rightarrow [1, 5, 1, 6] \\rightarrow [5, 1, 6]$, so the number of solved problems will be equal to $5$.\n\nIn the second example, Mishka can't solve any problem because the difficulties of problems from both ends are greater than $k$.\n\nIn the third example, Mishka's solving skill is so amazing that he can solve all the problems."} +{"id": "03068", "text": "There is an infinite board of square tiles. Initially all tiles are white.\n\nVova has a red marker and a blue marker. Red marker can color $a$ tiles. Blue marker can color $b$ tiles. If some tile isn't white then you can't use marker of any color on it. Each marker must be drained completely, so at the end there should be exactly $a$ red tiles and exactly $b$ blue tiles across the board.\n\nVova wants to color such a set of tiles that:\n\n they would form a rectangle, consisting of exactly $a+b$ colored tiles; all tiles of at least one color would also form a rectangle. \n\nHere are some examples of correct colorings:\n\n [Image] \n\nHere are some examples of incorrect colorings:\n\n [Image] \n\nAmong all correct colorings Vova wants to choose the one with the minimal perimeter. What is the minimal perimeter Vova can obtain?\n\nIt is guaranteed that there exists at least one correct coloring.\n\n\n-----Input-----\n\nA single line contains two integers $a$ and $b$ ($1 \\le a, b \\le 10^{14}$) \u2014 the number of tiles red marker should color and the number of tiles blue marker should color, respectively.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimal perimeter of a colored rectangle Vova can obtain by coloring exactly $a$ tiles red and exactly $b$ tiles blue.\n\nIt is guaranteed that there exists at least one correct coloring.\n\n\n-----Examples-----\nInput\n4 4\n\nOutput\n12\n\nInput\n3 9\n\nOutput\n14\n\nInput\n9 3\n\nOutput\n14\n\nInput\n3 6\n\nOutput\n12\n\nInput\n506 2708\n\nOutput\n3218\n\n\n\n-----Note-----\n\nThe first four examples correspond to the first picture of the statement.\n\nNote that for there exist multiple correct colorings for all of the examples.\n\nIn the first example you can also make a rectangle with sides $1$ and $8$, though its perimeter will be $18$ which is greater than $8$.\n\nIn the second example you can make the same resulting rectangle with sides $3$ and $4$, but red tiles will form the rectangle with sides $1$ and $3$ and blue tiles will form the rectangle with sides $3$ and $3$."} +{"id": "03069", "text": "This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.\n\nYou are given a string $s$ consisting of $n$ lowercase Latin letters.\n\nYou have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$).\n\nAfter coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.\n\nThe goal is to make the string sorted, i.e. all characters should be in alphabetical order.\n\nYour task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 200$) \u2014 the length of $s$.\n\nThe second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nIf it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print \"NO\" (without quotes) in the first line.\n\nOtherwise, print \"YES\" in the first line and any correct coloring in the second line (the coloring is the string consisting of $n$ characters, the $i$-th character should be '0' if the $i$-th character is colored the first color and '1' otherwise).\n\n\n-----Examples-----\nInput\n9\nabacbecfd\n\nOutput\nYES\n001010101\n\nInput\n8\naaabbcbb\n\nOutput\nYES\n01011011\n\nInput\n7\nabcdedc\n\nOutput\nNO\n\nInput\n5\nabcde\n\nOutput\nYES\n00000"} +{"id": "03070", "text": "Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)\nHere, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.\nIf multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.\n\n-----Constraints-----\n - 1 \\leq A \\leq B \\leq 100\n - A and B are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nIf there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.\n\n-----Sample Input-----\n2 2\n\n-----Sample Output-----\n25\n\nIf the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:\n - When the consumption tax rate is 8 percent: \\lfloor 25 \\times 0.08 \\rfloor = \\lfloor 2 \\rfloor = 2 yen.\n - When the consumption tax rate is 10 percent: \\lfloor 25 \\times 0.1 \\rfloor = \\lfloor 2.5 \\rfloor = 2 yen.\nThus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25."} +{"id": "03071", "text": "Polycarp has to solve exactly $n$ problems to improve his programming skill before an important programming competition. But this competition will be held very soon, most precisely, it will start in $k$ days. It means that Polycarp has exactly $k$ days for training!\n\nPolycarp doesn't want to procrastinate, so he wants to solve at least one problem during each of $k$ days. He also doesn't want to overwork, so if he solves $x$ problems during some day, he should solve no more than $2x$ problems during the next day. And, at last, he wants to improve his skill, so if he solves $x$ problems during some day, he should solve at least $x+1$ problem during the next day.\n\nMore formally: let $[a_1, a_2, \\dots, a_k]$ be the array of numbers of problems solved by Polycarp. The $i$-th element of this array is the number of problems Polycarp solves during the $i$-th day of his training. Then the following conditions must be satisfied: sum of all $a_i$ for $i$ from $1$ to $k$ should be $n$; $a_i$ should be greater than zero for each $i$ from $1$ to $k$; the condition $a_i < a_{i + 1} \\le 2 a_i$ should be satisfied for each $i$ from $1$ to $k-1$. \n\nYour problem is to find any array $a$ of length $k$ satisfying the conditions above or say that it is impossible to do it.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n \\le 10^9, 1 \\le k \\le 10^5$) \u2014 the number of problems Polycarp wants to solve and the number of days Polycarp wants to train.\n\n\n-----Output-----\n\nIf it is impossible to find any array $a$ of length $k$ satisfying Polycarp's rules of training, print \"NO\" in the first line.\n\nOtherwise print \"YES\" in the first line, then print $k$ integers $a_1, a_2, \\dots, a_k$ in the second line, where $a_i$ should be the number of problems Polycarp should solve during the $i$-th day. If there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n26 6\n\nOutput\nYES\n1 2 4 5 6 8 \n\nInput\n8 3\n\nOutput\nNO\n\nInput\n1 1\n\nOutput\nYES\n1 \n\nInput\n9 4\n\nOutput\nNO"} +{"id": "03072", "text": "The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.\n\nPolycarp is a very famous freelancer. His current rating is $r$ units.\n\nSome very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer.\n\nPolycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether.\n\nTo gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.\n\nYour task is to calculate the maximum possible size of such subset of projects.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $r$ ($1 \\le n \\le 100, 1 \\le r \\le 30000$) \u2014 the number of projects and the initial rating of Polycarp, respectively.\n\nThe next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \\le a_i \\le 30000$, $-300 \\le b_i \\le 300$) \u2014 the rating required to complete the $i$-th project and the rating change after the project completion.\n\n\n-----Output-----\n\nPrint one integer \u2014 the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose.\n\n\n-----Examples-----\nInput\n3 4\n4 6\n10 -2\n8 -1\n\nOutput\n3\n\nInput\n5 20\n45 -6\n34 -15\n10 34\n1 27\n40 -45\n\nOutput\n5\n\nInput\n3 2\n300 -300\n1 299\n1 123\n\nOutput\n3"} +{"id": "03073", "text": "Let's call some square matrix with integer values in its cells palindromic if it doesn't change after the order of rows is reversed and it doesn't change after the order of columns is reversed.\n\nFor example, the following matrices are palindromic: $\\left[ \\begin{array}{l l l}{1} & {3} & {1} \\\\{3} & {1} & {3} \\\\{1} & {3} & {1} \\end{array} \\right] \\quad \\left[ \\begin{array}{l l l l}{1} & {2} & {2} & {1} \\\\{8} & {2} & {2} & {8} \\\\{8} & {2} & {2} & {8} \\\\{1} & {2} & {2} & {1} \\end{array} \\right]$ \n\nThe following matrices are not palindromic because they change after the order of rows is reversed: $\\left[ \\begin{array}{l l l}{1} & {3} & {1} \\\\{3} & {1} & {3} \\\\{2} & {3} & {2} \\end{array} \\right] \\rightarrow \\left[ \\begin{array}{l l l}{2} & {3} & {2} \\\\{3} & {1} & {3} \\\\{1} & {3} & {1} \\end{array} \\right] \\quad \\left[ \\begin{array}{l l l l}{1} & {8} & {8} & {9} \\\\{2} & {4} & {3} & {2} \\\\{1} & {3} & {4} & {1} \\\\{9} & {8} & {8} & {1} \\end{array} \\right] \\rightarrow \\left[ \\begin{array}{l l l l}{9} & {8} & {8} & {1} \\\\{1} & {3} & {4} & {1} \\\\{2} & {4} & {3} & {2} \\\\{1} & {8} & {8} & {9} \\end{array} \\right]$ \n\nThe following matrices are not palindromic because they change after the order of columns is reversed: $\\left[ \\begin{array}{l l l}{1} & {3} & {2} \\\\{3} & {1} & {3} \\\\{1} & {3} & {2} \\end{array} \\right] \\rightarrow \\left[ \\begin{array}{l l l}{2} & {3} & {1} \\\\{3} & {1} & {3} \\\\{2} & {3} & {1} \\end{array} \\right] \\quad \\left[ \\begin{array}{l l l l}{1} & {2} & {1} & {9} \\\\{8} & {4} & {3} & {8} \\\\{8} & {3} & {4} & {8} \\\\{9} & {2} & {1} & {1} \\end{array} \\right] \\rightarrow \\left[ \\begin{array}{l l l l}{9} & {1} & {2} & {1} \\\\{8} & {3} & {4} & {8} \\\\{8} & {4} & {3} & {8} \\\\{1} & {1} & {2} & {9} \\end{array} \\right]$ \n\nYou are given $n^2$ integers. Put them into a matrix of $n$ rows and $n$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic. If there are multiple answers, print any. If there is no solution, print \"NO\".\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 20$).\n\nThe second line contains $n^2$ integers $a_1, a_2, \\dots, a_{n^2}$ ($1 \\le a_i \\le 1000$) \u2014 the numbers to put into a matrix of $n$ rows and $n$ columns.\n\n\n-----Output-----\n\nIf it is possible to put all of the $n^2$ numbers into a matrix of $n$ rows and $n$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic, then print \"YES\". Then print $n$ lines with $n$ space-separated numbers \u2014 the resulting matrix.\n\nIf it's impossible to construct any matrix, then print \"NO\".\n\nYou can print each letter in any case (upper or lower). For example, \"YeS\", \"no\" and \"yES\" are all acceptable.\n\n\n-----Examples-----\nInput\n4\n1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1\n\nOutput\nYES\n1 2 2 1\n8 2 2 8\n8 2 2 8\n1 2 2 1\n\nInput\n3\n1 1 1 1 1 3 3 3 3\n\nOutput\nYES\n1 3 1\n3 1 3\n1 3 1\n\nInput\n4\n1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1\n\nOutput\nNO\n\nInput\n1\n10\n\nOutput\nYES\n10 \n\n\n\n-----Note-----\n\nNote that there exist multiple answers for the first two examples."} +{"id": "03074", "text": "The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.\n\nPolycarp is a very famous freelancer. His current rating is $r$ units.\n\nSome very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer.\n\nIs it possible to complete all the projects? Formally, write a program to check if such an order of the projects exists, that Polycarp has enough rating before starting each project, and he has non-negative rating after completing each project.\n\nIn other words, you have to check that there exists such an order of projects in which Polycarp will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $r$ ($1 \\le n \\le 100, 1 \\le r \\le 30000$) \u2014 the number of projects and the initial rating of Polycarp, respectively.\n\nThe next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \\le a_i \\le 30000$, $-300 \\le b_i \\le 300$) \u2014 the rating required to complete the $i$-th project and the rating change after the project completion.\n\n\n-----Output-----\n\nPrint \"YES\" or \"NO\".\n\n\n-----Examples-----\nInput\n3 4\n4 6\n10 -2\n8 -1\n\nOutput\nYES\n\nInput\n3 5\n4 -5\n4 -2\n1 3\n\nOutput\nYES\n\nInput\n4 4\n5 2\n5 -3\n2 1\n4 -2\n\nOutput\nYES\n\nInput\n3 10\n10 0\n10 -10\n30 0\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example, the possible order is: $1, 2, 3$.\n\nIn the second example, the possible order is: $2, 3, 1$.\n\nIn the third example, the possible order is: $3, 1, 4, 2$."} +{"id": "03075", "text": "There is a river of width $n$. The left bank of the river is cell $0$ and the right bank is cell $n + 1$ (more formally, the river can be represented as a sequence of $n + 2$ cells numbered from $0$ to $n + 1$). There are also $m$ wooden platforms on a river, the $i$-th platform has length $c_i$ (so the $i$-th platform takes $c_i$ consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed $n$.\n\nYou are standing at $0$ and want to reach $n+1$ somehow. If you are standing at the position $x$, you can jump to any position in the range $[x + 1; x + d]$. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if $d=1$, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells $0$ and $n+1$ belong to wooden platforms.\n\nYou want to know if it is possible to reach $n+1$ from $0$ if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.\n\nNote that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).\n\nFor example, if $n=7$, $m=3$, $d=2$ and $c = [1, 2, 1]$, then one of the ways to reach $8$ from $0$ is follow:\n\n [Image] The first example: $n=7$. \n\n\n-----Input-----\n\nThe first line of the input contains three integers $n$, $m$ and $d$ ($1 \\le n, m, d \\le 1000, m \\le n$) \u2014 the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.\n\nThe second line of the input contains $m$ integers $c_1, c_2, \\dots, c_m$ ($1 \\le c_i \\le n, \\sum\\limits_{i=1}^{m} c_i \\le n$), where $c_i$ is the length of the $i$-th platform.\n\n\n-----Output-----\n\nIf it is impossible to reach $n+1$ from $0$, print NO in the first line. Otherwise, print YES in the first line and the array $a$ of length $n$ in the second line \u2014 the sequence of river cells (excluding cell $0$ and cell $n + 1$).\n\nIf the cell $i$ does not belong to any platform, $a_i$ should be $0$. Otherwise, it should be equal to the index of the platform ($1$-indexed, platforms are numbered from $1$ to $m$ in order of input) to which the cell $i$ belongs.\n\nNote that all $a_i$ equal to $1$ should form a contiguous subsegment of the array $a$ of length $c_1$, all $a_i$ equal to $2$ should form a contiguous subsegment of the array $a$ of length $c_2$, ..., all $a_i$ equal to $m$ should form a contiguous subsegment of the array $a$ of length $c_m$. The leftmost position of $2$ in $a$ should be greater than the rightmost position of $1$, the leftmost position of $3$ in $a$ should be greater than the rightmost position of $2$, ..., the leftmost position of $m$ in $a$ should be greater than the rightmost position of $m-1$.\n\nSee example outputs for better understanding.\n\n\n-----Examples-----\nInput\n7 3 2\n1 2 1\n\nOutput\nYES\n0 1 0 2 2 0 3 \n\nInput\n10 1 11\n1\n\nOutput\nYES\n0 0 0 0 0 0 0 0 0 1 \n\nInput\n10 1 5\n2\n\nOutput\nYES\n0 0 0 0 1 1 0 0 0 0 \n\n\n\n-----Note-----\n\nConsider the first example: the answer is $[0, 1, 0, 2, 2, 0, 3]$. The sequence of jumps you perform is $0 \\rightarrow 2 \\rightarrow 4 \\rightarrow 5 \\rightarrow 7 \\rightarrow 8$.\n\nConsider the second example: it does not matter how to place the platform because you always can jump from $0$ to $11$.\n\nConsider the third example: the answer is $[0, 0, 0, 0, 1, 1, 0, 0, 0, 0]$. The sequence of jumps you perform is $0 \\rightarrow 5 \\rightarrow 6 \\rightarrow 11$."} +{"id": "03076", "text": "The only difference between easy and hard versions is the length of the string.\n\nYou are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$).\n\nFor example, the strings \"test\", \"tst\", \"tt\", \"et\" and \"\" are subsequences of the string \"test\". But the strings \"tset\", \"se\", \"contest\" are not subsequences of the string \"test\".\n\nYou want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$.\n\nIf you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \\dots s_{l-1} s_{r+1} s_{r+2} \\dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$).\n\nYour task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$.\n\n\n-----Input-----\n\nThe first line of the input contains one string $s$ consisting of at least $1$ and at most $200$ lowercase Latin letters.\n\nThe second line of the input contains one string $t$ consisting of at least $1$ and at most $200$ lowercase Latin letters.\n\nIt is guaranteed that $t$ is a subsequence of $s$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$.\n\n\n-----Examples-----\nInput\nbbaba\nbb\n\nOutput\n3\n\nInput\nbaaba\nab\n\nOutput\n2\n\nInput\nabcde\nabcde\n\nOutput\n0\n\nInput\nasdfasdf\nfasd\n\nOutput\n3"} +{"id": "03077", "text": "$\\text{A}$ \n\n\n-----Input-----\n\nThe input contains a single floating-point number x with exactly 6 decimal places (0 < x < 5).\n\n\n-----Output-----\n\nOutput two integers separated by a single space. Each integer should be between 1 and 10, inclusive. If several solutions exist, output any of them. Solution will exist for all tests.\n\n\n-----Examples-----\nInput\n1.200000\n\nOutput\n3 2\n\nInput\n2.572479\n\nOutput\n10 3\n\nInput\n4.024922\n\nOutput\n9 9"} +{"id": "03078", "text": "You are given three integers $n$, $d$ and $k$.\n\nYour task is to construct an undirected tree on $n$ vertices with diameter $d$ and degree of each vertex at most $k$, or say that it is impossible.\n\nAn undirected tree is a connected undirected graph with $n - 1$ edges.\n\nDiameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.\n\nDegree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $u$ it is the number of edges $(u, v)$ that belong to the tree, where $v$ is any other vertex of a tree).\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n$, $d$ and $k$ ($1 \\le n, d, k \\le 4 \\cdot 10^5$).\n\n\n-----Output-----\n\nIf there is no tree satisfying the conditions above, print only one word \"NO\" (without quotes).\n\nOtherwise in the first line print \"YES\" (without quotes), and then print $n - 1$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $1$ to $n$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1\n\n\n-----Examples-----\nInput\n6 3 3\n\nOutput\nYES\n3 1\n4 1\n1 2\n5 2\n2 6\n\nInput\n6 2 3\n\nOutput\nNO\n\nInput\n10 4 3\n\nOutput\nYES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7\n\nInput\n8 5 3\n\nOutput\nYES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"} +{"id": "03079", "text": "You are given three integers $a$, $b$ and $x$. Your task is to construct a binary string $s$ of length $n = a + b$ such that there are exactly $a$ zeroes, exactly $b$ ones and exactly $x$ indices $i$ (where $1 \\le i < n$) such that $s_i \\ne s_{i + 1}$. It is guaranteed that the answer always exists.\n\nFor example, for the string \"01010\" there are four indices $i$ such that $1 \\le i < n$ and $s_i \\ne s_{i + 1}$ ($i = 1, 2, 3, 4$). For the string \"111001\" there are two such indices $i$ ($i = 3, 5$).\n\nRecall that binary string is a non-empty sequence of characters where each character is either 0 or 1.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $a$, $b$ and $x$ ($1 \\le a, b \\le 100, 1 \\le x < a + b)$.\n\n\n-----Output-----\n\nPrint only one string $s$, where $s$ is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.\n\n\n-----Examples-----\nInput\n2 2 1\n\nOutput\n1100\n\nInput\n3 3 3\n\nOutput\n101100\n\nInput\n5 3 6\n\nOutput\n01010100\n\n\n\n-----Note-----\n\nAll possible answers for the first example: 1100; 0011. \n\nAll possible answers for the second example: 110100; 101100; 110010; 100110; 011001; 001101; 010011; 001011."} +{"id": "03080", "text": "You are given two strings $s$ and $t$ both of length $2$ and both consisting only of characters 'a', 'b' and 'c'.\n\nPossible examples of strings $s$ and $t$: \"ab\", \"ca\", \"bb\".\n\nYou have to find a string $res$ consisting of $3n$ characters, $n$ characters should be 'a', $n$ characters should be 'b' and $n$ characters should be 'c' and $s$ and $t$ should not occur in $res$ as substrings.\n\nA substring of a string is a contiguous subsequence of that string. So, the strings \"ab\", \"ac\" and \"cc\" are substrings of the string \"abacc\", but the strings \"bc\", \"aa\" and \"cb\" are not substrings of the string \"abacc\".\n\nIf there are multiple answers, you can print any of them.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 10^5$) \u2014 the number of characters 'a', 'b' and 'c' in the resulting string.\n\nThe second line of the input contains one string $s$ of length $2$ consisting of characters 'a', 'b' and 'c'.\n\nThe third line of the input contains one string $t$ of length $2$ consisting of characters 'a', 'b' and 'c'.\n\n\n-----Output-----\n\nIf it is impossible to find the suitable string, print \"NO\" on the first line. \n\nOtherwise print \"YES\" on the first line and string $res$ on the second line. $res$ should consist of $3n$ characters, $n$ characters should be 'a', $n$ characters should be 'b' and $n$ characters should be 'c' and $s$ and $t$ should not occur in $res$ as substrings.\n\nIf there are multiple answers, you can print any of them.\n\n\n-----Examples-----\nInput\n2\nab\nbc\n\nOutput\nYES\nacbbac\n\nInput\n3\naa\nbc\n\nOutput\nYES\ncacbacbab\n\nInput\n1\ncb\nac\n\nOutput\nYES\nabc"} +{"id": "03081", "text": "An array of integers $p_1, p_2, \\dots, p_n$ is called a permutation if it contains each number from $1$ to $n$ exactly once. For example, the following arrays are permutations: $[3, 1, 2]$, $[1]$, $[1, 2, 3, 4, 5]$ and $[4, 3, 1, 2]$. The following arrays are not permutations: $[2]$, $[1, 1]$, $[2, 3, 4]$.\n\nPolycarp invented a really cool permutation $p_1, p_2, \\dots, p_n$ of length $n$. It is very disappointing, but he forgot this permutation. He only remembers the array $q_1, q_2, \\dots, q_{n-1}$ of length $n-1$, where $q_i=p_{i+1}-p_i$.\n\nGiven $n$ and $q=q_1, q_2, \\dots, q_{n-1}$, help Polycarp restore the invented permutation.\n\n\n-----Input-----\n\nThe first line contains the integer $n$ ($2 \\le n \\le 2\\cdot10^5$) \u2014 the length of the permutation to restore. The second line contains $n-1$ integers $q_1, q_2, \\dots, q_{n-1}$ ($-n < q_i < n$).\n\n\n-----Output-----\n\nPrint the integer -1 if there is no such permutation of length $n$ which corresponds to the given array $q$. Otherwise, if it exists, print $p_1, p_2, \\dots, p_n$. Print any such permutation if there are many of them.\n\n\n-----Examples-----\nInput\n3\n-2 1\n\nOutput\n3 1 2 \nInput\n5\n1 1 1 1\n\nOutput\n1 2 3 4 5 \nInput\n4\n-1 2 2\n\nOutput\n-1"} +{"id": "03082", "text": "You are given $n$ chips on a number line. The $i$-th chip is placed at the integer coordinate $x_i$. Some chips can have equal coordinates.\n\nYou can perform each of the two following types of moves any (possibly, zero) number of times on any chip:\n\n Move the chip $i$ by $2$ to the left or $2$ to the right for free (i.e. replace the current coordinate $x_i$ with $x_i - 2$ or with $x_i + 2$); move the chip $i$ by $1$ to the left or $1$ to the right and pay one coin for this move (i.e. replace the current coordinate $x_i$ with $x_i - 1$ or with $x_i + 1$). \n\nNote that it's allowed to move chips to any integer coordinate, including negative and zero.\n\nYour task is to find the minimum total number of coins required to move all $n$ chips to the same coordinate (i.e. all $x_i$ should be equal after some sequence of moves).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the number of chips.\n\nThe second line of the input contains $n$ integers $x_1, x_2, \\dots, x_n$ ($1 \\le x_i \\le 10^9$), where $x_i$ is the coordinate of the $i$-th chip.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum total number of coins required to move all $n$ chips to the same coordinate.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\n1\n\nInput\n5\n2 2 2 3 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example you need to move the first chip by $2$ to the right and the second chip by $1$ to the right or move the third chip by $2$ to the left and the second chip by $1$ to the left so the answer is $1$.\n\nIn the second example you need to move two chips with coordinate $3$ by $1$ to the left so the answer is $2$."} +{"id": "03083", "text": "Takahashi is standing on a multiplication table with infinitely many rows and columns.\nThe square (i,j) contains the integer i \\times j. Initially, Takahashi is standing at (1,1).\nIn one move, he can move from (i,j) to either (i+1,j) or (i,j+1).\nGiven an integer N, find the minimum number of moves needed to reach a square that contains N.\n\n-----Constraints-----\n - 2 \\leq N \\leq 10^{12}\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the minimum number of moves needed to reach a square that contains the integer N.\n\n-----Sample Input-----\n10\n\n-----Sample Output-----\n5\n\n(2,5) can be reached in five moves. We cannot reach a square that contains 10 in less than five moves."} +{"id": "03084", "text": "Alice and Bob have decided to play the game \"Rock, Paper, Scissors\". \n\nThe game consists of several rounds, each round is independent of each other. In each round, both players show one of the following things at the same time: rock, paper or scissors. If both players showed the same things then the round outcome is a draw. Otherwise, the following rules applied:\n\n if one player showed rock and the other one showed scissors, then the player who showed rock is considered the winner and the other one is considered the loser; if one player showed scissors and the other one showed paper, then the player who showed scissors is considered the winner and the other one is considered the loser; if one player showed paper and the other one showed rock, then the player who showed paper is considered the winner and the other one is considered the loser. \n\nAlice and Bob decided to play exactly $n$ rounds of the game described above. Alice decided to show rock $a_1$ times, show scissors $a_2$ times and show paper $a_3$ times. Bob decided to show rock $b_1$ times, show scissors $b_2$ times and show paper $b_3$ times. Though, both Alice and Bob did not choose the sequence in which they show things. It is guaranteed that $a_1 + a_2 + a_3 = n$ and $b_1 + b_2 + b_3 = n$.\n\nYour task is to find two numbers:\n\n the minimum number of round Alice can win; the maximum number of rounds Alice can win. \n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 10^{9}$) \u2014 the number of rounds.\n\nThe second line of the input contains three integers $a_1, a_2, a_3$ ($0 \\le a_i \\le n$) \u2014 the number of times Alice will show rock, scissors and paper, respectively. It is guaranteed that $a_1 + a_2 + a_3 = n$.\n\nThe third line of the input contains three integers $b_1, b_2, b_3$ ($0 \\le b_j \\le n$) \u2014 the number of times Bob will show rock, scissors and paper, respectively. It is guaranteed that $b_1 + b_2 + b_3 = n$.\n\n\n-----Output-----\n\nPrint two integers: the minimum and the maximum number of rounds Alice can win.\n\n\n-----Examples-----\nInput\n2\n0 1 1\n1 1 0\n\nOutput\n0 1\n\nInput\n15\n5 5 5\n5 5 5\n\nOutput\n0 15\n\nInput\n3\n0 0 3\n3 0 0\n\nOutput\n3 3\n\nInput\n686\n479 178 29\n11 145 530\n\nOutput\n22 334\n\nInput\n319\n10 53 256\n182 103 34\n\nOutput\n119 226\n\n\n\n-----Note-----\n\nIn the first example, Alice will not win any rounds if she shows scissors and then paper and Bob shows rock and then scissors. In the best outcome, Alice will win one round if she shows paper and then scissors, and Bob shows rock and then scissors.\n\nIn the second example, Alice will not win any rounds if Bob shows the same things as Alice each round.\n\nIn the third example, Alice always shows paper and Bob always shows rock so Alice will win all three rounds anyway."} +{"id": "03085", "text": "This problem is given in two editions, which differ exclusively in the constraints on the number $n$.\n\nYou are given an array of integers $a[1], a[2], \\dots, a[n].$ A block is a sequence of contiguous (consecutive) elements $a[l], a[l+1], \\dots, a[r]$ ($1 \\le l \\le r \\le n$). Thus, a block is defined by a pair of indices $(l, r)$.\n\nFind a set of blocks $(l_1, r_1), (l_2, r_2), \\dots, (l_k, r_k)$ such that:\n\n They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks $(l_i, r_i)$ and $(l_j, r_j$) where $i \\neq j$ either $r_i < l_j$ or $r_j < l_i$. For each block the sum of its elements is the same. Formally, $$a[l_1]+a[l_1+1]+\\dots+a[r_1]=a[l_2]+a[l_2+1]+\\dots+a[r_2]=$$ $$\\dots =$$ $$a[l_k]+a[l_k+1]+\\dots+a[r_k].$$ The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks $(l_1', r_1'), (l_2', r_2'), \\dots, (l_{k'}', r_{k'}')$ satisfying the above two requirements with $k' > k$. \n\n $\\left. \\begin{array}{|l|l|l|l|l|l|} \\hline 4 & {1} & {2} & {2} & {1} & {5} & {3} \\\\ \\hline \\end{array} \\right.$ The picture corresponds to the first example. Blue boxes illustrate blocks. \n\nWrite a program to find such a set of blocks.\n\n\n-----Input-----\n\nThe first line contains integer $n$ ($1 \\le n \\le 50$) \u2014 the length of the given array. The second line contains the sequence of elements $a[1], a[2], \\dots, a[n]$ ($-10^5 \\le a_i \\le 10^5$).\n\n\n-----Output-----\n\nIn the first line print the integer $k$ ($1 \\le k \\le n$). The following $k$ lines should contain blocks, one per line. In each line print a pair of indices $l_i, r_i$ ($1 \\le l_i \\le r_i \\le n$) \u2014 the bounds of the $i$-th block. You can print blocks in any order. If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n7\n4 1 2 2 1 5 3\n\nOutput\n3\n7 7\n2 3\n4 5\n\nInput\n11\n-5 -4 -3 -2 -1 0 1 2 3 4 5\n\nOutput\n2\n3 4\n1 1\n\nInput\n4\n1 1 1 1\n\nOutput\n4\n4 4\n1 1\n2 2\n3 3"} +{"id": "03086", "text": "Everybody knows of spaghetti sort. You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you...\n\nYou come up with the following algorithm. For each number in the array a_{i}, build a stack of a_{i} ravioli. The image shows the stack for a_{i} = 4.\n\n [Image] \n\nArrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed.\n\nAt first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack.\n\nGiven an input array, figure out whether the described algorithm will sort it correctly.\n\n\n-----Input-----\n\nThe first line of input contains a single number n (1 \u2264 n \u2264 10) \u2014 the size of the array.\n\nThe second line of input contains n space-separated integers a_{i} (1 \u2264 a_{i} \u2264 100) \u2014 the elements of the array.\n\n\n-----Output-----\n\nOutput \"YES\" if the array can be sorted using the described procedure and \"NO\" if it can not.\n\n\n-----Examples-----\nInput\n3\n1 2 3\n\nOutput\nYES\n\nInput\n3\n3 1 2\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}."} +{"id": "03087", "text": "You are given two strings $s$ and $t$. Both strings have length $n$ and consist of lowercase Latin letters. The characters in the strings are numbered from $1$ to $n$.\n\nYou can successively perform the following move any number of times (possibly, zero): swap any two adjacent (neighboring) characters of $s$ (i.e. for any $i = \\{1, 2, \\dots, n - 1\\}$ you can swap $s_i$ and $s_{i + 1})$. \n\nYou can't apply a move to the string $t$. The moves are applied to the string $s$ one after another.\n\nYour task is to obtain the string $t$ from the string $s$. Find any way to do it with at most $10^4$ such moves.\n\nYou do not have to minimize the number of moves, just find any sequence of moves of length $10^4$ or less to transform $s$ into $t$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 50$) \u2014 the length of strings $s$ and $t$.\n\nThe second line of the input contains the string $s$ consisting of $n$ lowercase Latin letters.\n\nThe third line of the input contains the string $t$ consisting of $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nIf it is impossible to obtain the string $t$ using moves, print \"-1\".\n\nOtherwise in the first line print one integer $k$ \u2014 the number of moves to transform $s$ to $t$. Note that $k$ must be an integer number between $0$ and $10^4$ inclusive.\n\nIn the second line print $k$ integers $c_j$ ($1 \\le c_j < n$), where $c_j$ means that on the $j$-th move you swap characters $s_{c_j}$ and $s_{c_j + 1}$.\n\nIf you do not need to apply any moves, print a single integer $0$ in the first line and either leave the second line empty or do not print it at all.\n\n\n-----Examples-----\nInput\n6\nabcdef\nabdfec\n\nOutput\n4\n3 5 4 5 \n\nInput\n4\nabcd\naccd\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example the string $s$ changes as follows: \"abcdef\" $\\rightarrow$ \"abdcef\" $\\rightarrow$ \"abdcfe\" $\\rightarrow$ \"abdfce\" $\\rightarrow$ \"abdfec\".\n\nIn the second example there is no way to transform the string $s$ into the string $t$ through any allowed moves."} +{"id": "03088", "text": "Ivan wants to play a game with you. He picked some string $s$ of length $n$ consisting only of lowercase Latin letters. \n\nYou don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from $1$ to $n-1$), but he didn't tell you which strings are prefixes and which are suffixes.\n\nIvan wants you to guess which of the given $2n-2$ strings are prefixes of the given string and which are suffixes. It may be impossible to guess the string Ivan picked (since multiple strings may give the same set of suffixes and prefixes), but Ivan will accept your answer if there is at least one string that is consistent with it. Let the game begin!\n\n\n-----Input-----\n\nThe first line of the input contains one integer number $n$ ($2 \\le n \\le 100$) \u2014 the length of the guessed string $s$.\n\nThe next $2n-2$ lines are contain prefixes and suffixes, one per line. Each of them is the string of length from $1$ to $n-1$ consisting only of lowercase Latin letters. They can be given in arbitrary order.\n\nIt is guaranteed that there are exactly $2$ strings of each length from $1$ to $n-1$. It is also guaranteed that these strings are prefixes and suffixes of some existing string of length $n$.\n\n\n-----Output-----\n\nPrint one string of length $2n-2$ \u2014 the string consisting only of characters 'P' and 'S'. The number of characters 'P' should be equal to the number of characters 'S'. The $i$-th character of this string should be 'P' if the $i$-th of the input strings is the prefix and 'S' otherwise.\n\nIf there are several possible answers, you can print any.\n\n\n-----Examples-----\nInput\n5\nba\na\nabab\na\naba\nbaba\nab\naba\n\nOutput\nSPPSPSPS\n\nInput\n3\na\naa\naa\na\n\nOutput\nPPSS\n\nInput\n2\na\nc\n\nOutput\nPS\n\n\n\n-----Note-----\n\nThe only string which Ivan can guess in the first example is \"ababa\".\n\nThe only string which Ivan can guess in the second example is \"aaa\". Answers \"SPSP\", \"SSPP\" and \"PSPS\" are also acceptable.\n\nIn the third example Ivan can guess the string \"ac\" or the string \"ca\". The answer \"SP\" is also acceptable."} +{"id": "03089", "text": "Salve, mi amice.\n\nEt tu quidem de lapis philosophorum. Barba non facit philosophum. Labor omnia vincit. Non potest creatio ex nihilo. Necesse est partibus.\n\nRp:\n\n\u00a0\u00a0\u00a0\u00a0I Aqua Fortis\n\n\u00a0\u00a0\u00a0\u00a0I Aqua Regia\n\n\u00a0\u00a0\u00a0\u00a0II Amalgama\n\n\u00a0\u00a0\u00a0\u00a0VII Minium\n\n\u00a0\u00a0\u00a0\u00a0IV Vitriol\n\nMisce in vitro et \u00e6stus, et nil admirari. Festina lente, et nulla tenaci invia est via.\n\nFac et spera,\n\nVale,\n\nNicolas Flamel\n\n\n-----Input-----\n\nThe first line of input contains several space-separated integers a_{i} (0 \u2264 a_{i} \u2264 100).\n\n\n-----Output-----\n\nPrint a single integer.\n\n\n-----Examples-----\nInput\n2 4 6 8 10\n\nOutput\n1"} +{"id": "03090", "text": "There is a house with $n$ flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of $n$ integer numbers $a_1, a_2, \\dots, a_n$, where $a_i = 1$ if in the $i$-th flat the light is on and $a_i = 0$ otherwise.\n\nVova thinks that people in the $i$-th flats are disturbed and cannot sleep if and only if $1 < i < n$ and $a_{i - 1} = a_{i + 1} = 1$ and $a_i = 0$.\n\nVova is concerned by the following question: what is the minimum number $k$ such that if people from exactly $k$ pairwise distinct flats will turn off the lights then nobody will be disturbed? Your task is to find this number $k$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($3 \\le n \\le 100$) \u2014 the number of flats in the house.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($a_i \\in \\{0, 1\\}$), where $a_i$ is the state of light in the $i$-th flat.\n\n\n-----Output-----\n\nPrint only one integer \u2014 the minimum number $k$ such that if people from exactly $k$ pairwise distinct flats will turn off the light then nobody will be disturbed.\n\n\n-----Examples-----\nInput\n10\n1 1 0 1 1 0 1 0 1 0\n\nOutput\n2\n\nInput\n5\n1 1 0 0 0\n\nOutput\n0\n\nInput\n4\n1 1 1 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example people from flats $2$ and $7$ or $4$ and $7$ can turn off the light and nobody will be disturbed. It can be shown that there is no better answer in this example.\n\nThere are no disturbed people in second and third examples."} +{"id": "03091", "text": "You are given an array $a$ consisting of $n$ integers.\n\nYour task is to say the number of such positive integers $x$ such that $x$ divides each number from the array. In other words, you have to find the number of common divisors of all elements in the array.\n\nFor example, if the array $a$ will be $[2, 4, 6, 2, 10]$, then $1$ and $2$ divide each number from the array (so the answer for this test is $2$).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 4 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^{12}$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of such positive integers $x$ such that $x$ divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array).\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n\nOutput\n1\n\nInput\n6\n6 90 12 18 30 18\n\nOutput\n4"} +{"id": "03092", "text": "Polycarp has $n$ coins, the value of the $i$-th coin is $a_i$. Polycarp wants to distribute all the coins between his pockets, but he cannot put two coins with the same value into the same pocket.\n\nFor example, if Polycarp has got six coins represented as an array $a = [1, 2, 4, 3, 3, 2]$, he can distribute the coins into two pockets as follows: $[1, 2, 3], [2, 3, 4]$.\n\nPolycarp wants to distribute all the coins with the minimum number of used pockets. Help him to do that.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the number of coins.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$) \u2014 values of coins.\n\n\n-----Output-----\n\nPrint only one integer \u2014 the minimum number of pockets Polycarp needs to distribute all the coins so no two coins with the same value are put into the same pocket.\n\n\n-----Examples-----\nInput\n6\n1 2 4 3 3 2\n\nOutput\n2\n\nInput\n1\n100\n\nOutput\n1"} +{"id": "03093", "text": "Vova's house is an array consisting of $n$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $i$-th element of the array is $1$ if there is a heater in the position $i$, otherwise the $i$-th element of the array is $0$.\n\nEach heater has a value $r$ ($r$ is the same for all heaters). This value means that the heater at the position $pos$ can warm up all the elements in range $[pos - r + 1; pos + r - 1]$.\n\nVova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater. \n\nVova's target is to warm up the whole house (all the elements of the array), i.e. if $n = 6$, $r = 2$ and heaters are at positions $2$ and $5$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $3$ elements will be warmed up by the first heater and the last $3$ elements will be warmed up by the second heater).\n\nInitially, all the heaters are off.\n\nBut from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.\n\nYour task is to find this number of heaters or say that it is impossible to warm up the whole house.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $r$ ($1 \\le n, r \\le 1000$) \u2014 the number of elements in the array and the value of heaters.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 1$) \u2014 the Vova's house description.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.\n\n\n-----Examples-----\nInput\n6 2\n0 1 1 0 0 1\n\nOutput\n3\n\nInput\n5 3\n1 0 0 0 1\n\nOutput\n2\n\nInput\n5 10\n0 0 0 0 0\n\nOutput\n-1\n\nInput\n10 3\n0 0 1 1 0 1 0 0 0 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example the heater at the position $2$ warms up elements $[1; 3]$, the heater at the position $3$ warms up elements $[2, 4]$ and the heater at the position $6$ warms up elements $[5; 6]$ so the answer is $3$.\n\nIn the second example the heater at the position $1$ warms up elements $[1; 3]$ and the heater at the position $5$ warms up elements $[3; 5]$ so the answer is $2$.\n\nIn the third example there are no heaters so the answer is -1.\n\nIn the fourth example the heater at the position $3$ warms up elements $[1; 5]$, the heater at the position $6$ warms up elements $[4; 8]$ and the heater at the position $10$ warms up elements $[8; 10]$ so the answer is $3$."} +{"id": "03094", "text": "Given is a positive integer N.\nHow many tuples (A,B,C) of positive integers satisfy A \\times B + C = N?\n\n-----Constraints-----\n - 2 \\leq N \\leq 10^6\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n3\n\n-----Sample Output-----\n3\n\nThere are 3 tuples of integers that satisfy A \\times B + C = 3: (A, B, C) = (1, 1, 2), (1, 2, 1), (2, 1, 1)."} +{"id": "03095", "text": "You are given a bracket sequence $s$ consisting of $n$ opening '(' and closing ')' brackets.\n\nA regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences \"()()\", \"(())\" are regular (the resulting expressions are: \"(1)+(1)\", \"((1+1)+1)\"), and \")(\" and \"(\" are not.\n\nYou can change the type of some bracket $s_i$. It means that if $s_i = $ ')' then you can change it to '(' and vice versa.\n\nYour task is to calculate the number of positions $i$ such that if you change the type of the $i$-th bracket, then the resulting bracket sequence becomes regular.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 10^6$) \u2014 the length of the bracket sequence.\n\nThe second line of the input contains the string $s$ consisting of $n$ opening '(' and closing ')' brackets.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of positions $i$ such that if you change the type of the $i$-th bracket, then the resulting bracket sequence becomes regular.\n\n\n-----Examples-----\nInput\n6\n(((())\n\nOutput\n3\n\nInput\n6\n()()()\n\nOutput\n0\n\nInput\n1\n)\n\nOutput\n0\n\nInput\n8\n)))(((((\n\nOutput\n0"} +{"id": "03096", "text": "The only difference between easy and hard versions is the length of the string.\n\nYou are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$).\n\nFor example, the strings \"test\", \"tst\", \"tt\", \"et\" and \"\" are subsequences of the string \"test\". But the strings \"tset\", \"se\", \"contest\" are not subsequences of the string \"test\".\n\nYou want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$.\n\nIf you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \\dots s_{l-1} s_{r+1} s_{r+2} \\dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$).\n\nYour task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$.\n\n\n-----Input-----\n\nThe first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \\cdot 10^5$ lowercase Latin letters.\n\nThe second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \\cdot 10^5$ lowercase Latin letters.\n\nIt is guaranteed that $t$ is a subsequence of $s$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$.\n\n\n-----Examples-----\nInput\nbbaba\nbb\n\nOutput\n3\n\nInput\nbaaba\nab\n\nOutput\n2\n\nInput\nabcde\nabcde\n\nOutput\n0\n\nInput\nasdfasdf\nfasd\n\nOutput\n3"} +{"id": "03097", "text": "Given are integers a,b,c and d.\nIf x and y are integers and a \\leq x \\leq b and c\\leq y \\leq d hold, what is the maximum possible value of x \\times y?\n\n-----Constraints-----\n - -10^9 \\leq a \\leq b \\leq 10^9\n - -10^9 \\leq c \\leq d \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\na b c d\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n1 2 1 1\n\n-----Sample Output-----\n2\n\nIf x = 1 and y = 1 then x \\times y = 1.\nIf x = 2 and y = 1 then x \\times y = 2.\nTherefore, the answer is 2."} +{"id": "03098", "text": "Takahashi made N problems for competitive programming.\nThe problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).\nHe is dividing the problems into two categories by choosing an integer K, as follows:\n - A problem with difficulty K or higher will be for ARCs.\n - A problem with difficulty lower than K will be for ABCs.\nHow many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?\n\n-----Problem Statement-----\n - 2 \\leq N \\leq 10^5\n - N is an even number.\n - 1 \\leq d_i \\leq 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nd_1 d_2 ... d_N\n\n-----Output-----\nPrint the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.\n\n-----Sample Input-----\n6\n9 1 4 4 6 7\n\n-----Sample Output-----\n2\n\nIf we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.\nThus, the answer is 2."} +{"id": "03099", "text": "Vova had a pretty weird sleeping schedule. There are $h$ hours in a day. Vova will sleep exactly $n$ times. The $i$-th time he will sleep exactly after $a_i$ hours from the time he woke up. You can assume that Vova woke up exactly at the beginning of this story (the initial time is $0$). Each time Vova sleeps exactly one day (in other words, $h$ hours).\n\nVova thinks that the $i$-th sleeping time is good if he starts to sleep between hours $l$ and $r$ inclusive.\n\nVova can control himself and before the $i$-th time can choose between two options: go to sleep after $a_i$ hours or after $a_i - 1$ hours.\n\nYour task is to say the maximum number of good sleeping times Vova can obtain if he acts optimally.\n\n\n-----Input-----\n\nThe first line of the input contains four integers $n, h, l$ and $r$ ($1 \\le n \\le 2000, 3 \\le h \\le 2000, 0 \\le l \\le r < h$) \u2014 the number of times Vova goes to sleep, the number of hours in a day and the segment of the good sleeping time.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i < h$), where $a_i$ is the number of hours after which Vova goes to sleep the $i$-th time.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of good sleeping times Vova can obtain if he acts optimally.\n\n\n-----Example-----\nInput\n7 24 21 23\n16 17 14 20 20 11 22\n\nOutput\n3\n\n\n\n-----Note-----\n\nThe maximum number of good times in the example is $3$.\n\nThe story starts from $t=0$. Then Vova goes to sleep after $a_1 - 1$ hours, now the time is $15$. This time is not good. Then Vova goes to sleep after $a_2 - 1$ hours, now the time is $15 + 16 = 7$. This time is also not good. Then Vova goes to sleep after $a_3$ hours, now the time is $7 + 14 = 21$. This time is good. Then Vova goes to sleep after $a_4 - 1$ hours, now the time is $21 + 19 = 16$. This time is not good. Then Vova goes to sleep after $a_5$ hours, now the time is $16 + 20 = 12$. This time is not good. Then Vova goes to sleep after $a_6$ hours, now the time is $12 + 11 = 23$. This time is good. Then Vova goes to sleep after $a_7$ hours, now the time is $23 + 22 = 21$. This time is also good."} +{"id": "03100", "text": "You are given a problemset consisting of $n$ problems. The difficulty of the $i$-th problem is $a_i$. It is guaranteed that all difficulties are distinct and are given in the increasing order.\n\nYou have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let $a_{i_1}, a_{i_2}, \\dots, a_{i_p}$ be the difficulties of the selected problems in increasing order. Then for each $j$ from $1$ to $p-1$ $a_{i_{j + 1}} \\le a_{i_j} \\cdot 2$ should hold. It means that the contest consisting of only one problem is always valid.\n\nAmong all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of problems in the problemset.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) \u2014 difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order.\n\n\n-----Output-----\n\nPrint a single integer \u2014 maximum number of problems in the contest satisfying the condition in the problem statement.\n\n\n-----Examples-----\nInput\n10\n1 2 5 6 7 10 21 23 24 49\n\nOutput\n4\n\nInput\n5\n2 10 50 110 250\n\nOutput\n1\n\nInput\n6\n4 7 12 100 150 199\n\nOutput\n3\n\n\n\n-----Note-----\n\nDescription of the first example: there are $10$ valid contests consisting of $1$ problem, $10$ valid contests consisting of $2$ problems ($[1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]$), $5$ valid contests consisting of $3$ problems ($[5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]$) and a single valid contest consisting of $4$ problems ($[5, 6, 7, 10]$).\n\nIn the second example all the valid contests consist of $1$ problem.\n\nIn the third example are two contests consisting of $3$ problems: $[4, 7, 12]$ and $[100, 150, 199]$."} +{"id": "03101", "text": "You are given an array $a$ consisting of $n$ integers $a_1, a_2, \\dots, a_n$.\n\nYour problem is to find such pair of indices $i, j$ ($1 \\le i < j \\le n$) that $lcm(a_i, a_j)$ is minimum possible.\n\n$lcm(x, y)$ is the least common multiple of $x$ and $y$ (minimum positive number such that both $x$ and $y$ are divisors of this number).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 10^6$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^7$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nPrint two integers $i$ and $j$ ($1 \\le i < j \\le n$) such that the value of $lcm(a_i, a_j)$ is minimum among all valid pairs $i, j$. If there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n5\n2 4 8 3 6\n\nOutput\n1 2\n\nInput\n5\n5 2 11 3 7\n\nOutput\n2 4\n\nInput\n6\n2 5 10 1 10 2\n\nOutput\n1 4"} +{"id": "03102", "text": "You are given a string $s$ consisting of exactly $n$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.\n\nYour task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').\n\nAmong all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.\n\nNote that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.\n\nIt is guaranteed that the answer exists.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($3 \\le n \\le 3 \\cdot 10^5$, $n$ is divisible by $3$) \u2014 the number of characters in $s$.\n\nThe second line contains the string $s$ consisting of exactly $n$ characters '0', '1' and '2'.\n\n\n-----Output-----\n\nPrint one string \u2014 the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements.\n\nBecause $n$ is divisible by $3$ it is obvious that the answer exists. And it is obvious that there is only one possible answer.\n\n\n-----Examples-----\nInput\n3\n121\n\nOutput\n021\n\nInput\n6\n000000\n\nOutput\n001122\n\nInput\n6\n211200\n\nOutput\n211200\n\nInput\n6\n120110\n\nOutput\n120120"} +{"id": "03103", "text": "There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.\nHe can climb up one or two steps at a time.\nHowever, the treads of the a_1-th, a_2-th, a_3-th, \\ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.\nHow many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?\nFind the count modulo 1\\ 000\\ 000\\ 007.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - 0 \\leq M \\leq N-1\n - 1 \\leq a_1 < a_2 < ... < a_M \\leq N-1\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\na_1\na_2\n .\n .\n .\na_M\n\n-----Output-----\nPrint the number of ways to climb up the stairs under the condition, modulo 1\\ 000\\ 000\\ 007.\n\n-----Sample Input-----\n6 1\n3\n\n-----Sample Output-----\n4\n\nThere are four ways to climb up the stairs, as follows:\n - 0 \\to 1 \\to 2 \\to 4 \\to 5 \\to 6\n - 0 \\to 1 \\to 2 \\to 4 \\to 6\n - 0 \\to 2 \\to 4 \\to 5 \\to 6\n - 0 \\to 2 \\to 4 \\to 6"} +{"id": "03104", "text": "Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.\nMore specifically, in one move, he can go from coordinate x to x + D or x - D.\nHe wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.\nFind the minimum possible absolute value of the coordinate of the destination.\n\n-----Constraints-----\n - -10^{15} \\leq X \\leq 10^{15}\n - 1 \\leq K \\leq 10^{15}\n - 1\u00a0\\leq D \\leq 10^{15}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX K D\n\n-----Output-----\nPrint the minimum possible absolute value of the coordinate of the destination.\n\n-----Sample Input-----\n6 2 4\n\n-----Sample Output-----\n2\n\nTakahashi is now at coordinate 6. It is optimal to make the following moves:\n - Move from coordinate 6 to (6 - 4 =) 2.\n - Move from coordinate 2 to (2 - 4 =) -2.\nHere, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller."} +{"id": "03105", "text": "Programmers' kids solve this riddle in 5-10 minutes. How fast can you do it?\n\n\n-----Input-----\n\nThe input contains a single integer n (0 \u2264 n \u2264 2000000000).\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Examples-----\nInput\n11\n\nOutput\n2\n\nInput\n14\n\nOutput\n0\n\nInput\n61441\n\nOutput\n2\n\nInput\n571576\n\nOutput\n10\n\nInput\n2128506\n\nOutput\n3"} +{"id": "03106", "text": "-----Input-----\n\nThe input contains a single integer a (1 \u2264 a \u2264 30).\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Example-----\nInput\n3\n\nOutput\n27"} +{"id": "03107", "text": "-----Input-----\n\nThe input contains a single integer a (10 \u2264 a \u2264 999).\n\n\n-----Output-----\n\nOutput 0 or 1.\n\n\n-----Examples-----\nInput\n13\n\nOutput\n1\n\nInput\n927\n\nOutput\n1\n\nInput\n48\n\nOutput\n0"} +{"id": "03108", "text": "DO YOU EXPECT ME TO FIND THIS OUT?\n\nWHAT BASE AND/XOR LANGUAGE INCLUDES string?\n\nDON'T BYTE OF MORE THAN YOU CAN CHEW\n\nYOU CAN ONLY DISTORT THE LARGEST OF MATHEMATICS SO FAR\n\nSAYING \"ABRACADABRA\" WITHOUT A MAGIC AND WON'T DO YOU ANY GOOD\n\nTHE LAST STACK RUPTURES. ALL DIE. OH, THE EMBARRASSMENT!\n\nI HAVE NO ARRAY AND I MUST SCREAM\n\nELEMENTS MAY NOT BE STORED IN WEST HYPERSPACE\n\n\n-----Input-----\n\nThe first line of input data contains a single integer n (1 \u2264 n \u2264 10).\n\nThe second line of input data contains n space-separated integers a_{i} (1 \u2264 a_{i} \u2264 11).\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Example-----\nInput\n4\n2 5 3 1\n\nOutput\n4"} +{"id": "03109", "text": "Polycarp wants to buy exactly $n$ shovels. The shop sells packages with shovels. The store has $k$ types of packages: the package of the $i$-th type consists of exactly $i$ shovels ($1 \\le i \\le k$). The store has an infinite number of packages of each type.\n\nPolycarp wants to choose one type of packages and then buy several (one or more) packages of this type. What is the smallest number of packages Polycarp will have to buy to get exactly $n$ shovels?\n\nFor example, if $n=8$ and $k=7$, then Polycarp will buy $2$ packages of $4$ shovels.\n\nHelp Polycarp find the minimum number of packages that he needs to buy, given that he: will buy exactly $n$ shovels in total; the sizes of all packages he will buy are all the same and the number of shovels in each package is an integer from $1$ to $k$, inclusive. \n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\le t \\le 100$)\u00a0\u2014 the number of test cases in the input. Then, $t$ test cases follow, one per line.\n\nEach test case consists of two positive integers $n$ ($1 \\le n \\le 10^9$) and $k$ ($1 \\le k \\le 10^9$)\u00a0\u2014 the number of shovels and the number of types of packages.\n\n\n-----Output-----\n\nPrint $t$ answers to the test cases. Each answer is a positive integer\u00a0\u2014 the minimum number of packages.\n\n\n-----Example-----\nInput\n5\n8 7\n8 1\n6 10\n999999733 999999732\n999999733 999999733\n\nOutput\n2\n8\n1\n999999733\n1\n\n\n\n-----Note-----\n\nThe answer to the first test case was explained in the statement.\n\nIn the second test case, there is only one way to buy $8$ shovels\u00a0\u2014 $8$ packages of one shovel.\n\nIn the third test case, you need to buy a $1$ package of $6$ shovels."} +{"id": "03110", "text": "We have N switches with \"on\" and \"off\" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.\nBulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are \"on\" among these switches is congruent to p_i modulo 2.\nHow many combinations of \"on\" and \"off\" states of the switches light all the bulbs?\n\n-----Constraints-----\n - 1 \\leq N, M \\leq 10\n - 1 \\leq k_i \\leq N\n - 1 \\leq s_{ij} \\leq N\n - s_{ia} \\neq s_{ib} (a \\neq b)\n - p_i is 0 or 1.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nk_1 s_{11} s_{12} ... s_{1k_1}\n:\nk_M s_{M1} s_{M2} ... s_{Mk_M}\np_1 p_2 ... p_M\n\n-----Output-----\nPrint the number of combinations of \"on\" and \"off\" states of the switches that light all the bulbs.\n\n-----Sample Input-----\n2 2\n2 1 2\n1 2\n0 1\n\n-----Sample Output-----\n1\n\n - Bulb 1 is lighted when there is an even number of switches that are \"on\" among the following: Switch 1 and 2.\n - Bulb 2 is lighted when there is an odd number of switches that are \"on\" among the following: Switch 2.\nThere are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1."} +{"id": "03111", "text": "Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.\nAn endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.\nAt 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq A, B \\leq 1000\n - 0 \\leq H \\leq 11\n - 0 \\leq M \\leq 59\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B H M\n\n-----Output-----\nPrint the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.\n\n-----Sample Input-----\n3 4 9 0\n\n-----Sample Output-----\n5.00000000000000000000\n\nThe two hands will be in the positions shown in the figure below, so the answer is 5 centimeters."} +{"id": "03112", "text": "You are given an integer sequence $a_1, a_2, \\dots, a_n$.\n\nFind the number of pairs of indices $(l, r)$ ($1 \\le l \\le r \\le n$) such that the value of median of $a_l, a_{l+1}, \\dots, a_r$ is exactly the given number $m$.\n\nThe median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.\n\nFor example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence.\n\nWrite a program to find the number of pairs of indices $(l, r)$ ($1 \\le l \\le r \\le n$) such that the value of median of $a_l, a_{l+1}, \\dots, a_r$ is exactly the given number $m$.\n\n\n-----Input-----\n\nThe first line contains integers $n$ and $m$ ($1 \\le n,m \\le 2\\cdot10^5$) \u2014 the length of the given sequence and the required value of the median.\n\nThe second line contains an integer sequence $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2\\cdot10^5$).\n\n\n-----Output-----\n\nPrint the required number.\n\n\n-----Examples-----\nInput\n5 4\n1 4 5 60 4\n\nOutput\n8\n\nInput\n3 1\n1 1 1\n\nOutput\n6\n\nInput\n15 2\n1 2 3 1 2 3 1 2 3 1 2 3 1 2 3\n\nOutput\n97\n\n\n\n-----Note-----\n\nIn the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$."} +{"id": "03113", "text": "The only difference between easy and hard versions is a number of elements in the array.\n\nYou are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$.\n\nYou are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \\le l_j \\le r_j \\le n$.\n\nYou can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$.\n\nYou have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\\max\\limits_{i=1}^{n}b_i - \\min\\limits_{i=1}^{n}b_i$ will be maximum possible.\n\nNote that you can choose the empty set.\n\nIf there are multiple answers, you can print any.\n\nIf you are Python programmer, consider using PyPy instead of Python when you submit your code.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n \\le 300, 0 \\le m \\le 300$) \u2014 the length of the array $a$ and the number of segments, respectively.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^6 \\le a_i \\le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$.\n\nThe next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \\le l_j \\le r_j \\le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment.\n\n\n-----Output-----\n\nIn the first line of the output print one integer $d$ \u2014 the maximum possible value $\\max\\limits_{i=1}^{n}b_i - \\min\\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$.\n\nIn the second line of the output print one integer $q$ ($0 \\le q \\le m$) \u2014 the number of segments you apply.\n\nIn the third line print $q$ distinct integers $c_1, c_2, \\dots, c_q$ in any order ($1 \\le c_k \\le m$) \u2014 indices of segments you apply to the array $a$ in such a way that the value $\\max\\limits_{i=1}^{n}b_i - \\min\\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible.\n\nIf there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n5 4\n2 -2 3 1 2\n1 3\n4 5\n2 5\n1 3\n\nOutput\n6\n2\n1 4 \n\nInput\n5 4\n2 -2 3 1 4\n3 5\n3 4\n2 4\n2 5\n\nOutput\n7\n2\n3 2 \n\nInput\n1 0\n1000000\n\nOutput\n0\n0\n\n\n\n\n-----Note-----\n\nIn the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$.\n\nIn the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$.\n\nIn the third example you cannot do anything so the answer is $0$."} +{"id": "03114", "text": "A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: \"fced\", \"xyz\", \"r\" and \"dabcef\". The following string are not diverse: \"az\", \"aa\", \"bad\" and \"babc\". Note that the letters 'a' and 'z' are not adjacent.\n\nFormally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).\n\nYou are given a sequence of strings. For each string, if it is diverse, print \"Yes\". Otherwise, print \"No\".\n\n\n-----Input-----\n\nThe first line contains integer $n$ ($1 \\le n \\le 100$), denoting the number of strings to process. The following $n$ lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between $1$ and $100$, inclusive.\n\n\n-----Output-----\n\nPrint $n$ lines, one line per a string in the input. The line should contain \"Yes\" if the corresponding string is diverse and \"No\" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, \"YeS\", \"no\" and \"yES\" are all acceptable.\n\n\n-----Example-----\nInput\n8\nfced\nxyz\nr\ndabcef\naz\naa\nbad\nbabc\n\nOutput\nYes\nYes\nYes\nYes\nNo\nNo\nNo\nNo"} +{"id": "03115", "text": "The only difference between easy and hard versions is a number of elements in the array.\n\nYou are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$.\n\nYou are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \\le l_j \\le r_j \\le n$.\n\nYou can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$.\n\nYou have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\\max\\limits_{i=1}^{n}b_i - \\min\\limits_{i=1}^{n}b_i$ will be maximum possible.\n\nNote that you can choose the empty set.\n\nIf there are multiple answers, you can print any.\n\nIf you are Python programmer, consider using PyPy instead of Python when you submit your code.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n \\le 10^5, 0 \\le m \\le 300$) \u2014 the length of the array $a$ and the number of segments, respectively.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^6 \\le a_i \\le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$.\n\nThe next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \\le l_j \\le r_j \\le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment.\n\n\n-----Output-----\n\nIn the first line of the output print one integer $d$ \u2014 the maximum possible value $\\max\\limits_{i=1}^{n}b_i - \\min\\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$.\n\nIn the second line of the output print one integer $q$ ($0 \\le q \\le m$) \u2014 the number of segments you apply.\n\nIn the third line print $q$ distinct integers $c_1, c_2, \\dots, c_q$ in any order ($1 \\le c_k \\le m$) \u2014 indices of segments you apply to the array $a$ in such a way that the value $\\max\\limits_{i=1}^{n}b_i - \\min\\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible.\n\nIf there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n5 4\n2 -2 3 1 2\n1 3\n4 5\n2 5\n1 3\n\nOutput\n6\n2\n4 1 \n\nInput\n5 4\n2 -2 3 1 4\n3 5\n3 4\n2 4\n2 5\n\nOutput\n7\n2\n3 2 \n\nInput\n1 0\n1000000\n\nOutput\n0\n0\n\n\n\n\n-----Note-----\n\nIn the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$.\n\nIn the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$.\n\nIn the third example you cannot do anything so the answer is $0$."} +{"id": "03116", "text": "The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).\n\nYou are given a sequence $a$ consisting of $n$ integers. All these integers are distinct, each value from $1$ to $n$ appears in the sequence exactly once.\n\nYou are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).\n\nFor example, for the sequence $[2, 1, 5, 4, 3]$ the answer is $4$ (you take $2$ and the sequence becomes $[1, 5, 4, 3]$, then you take the rightmost element $3$ and the sequence becomes $[1, 5, 4]$, then you take $4$ and the sequence becomes $[1, 5]$ and then you take $5$ and the sequence becomes $[1]$, the obtained increasing sequence is $[2, 3, 4, 5]$).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$), where $a_i$ is the $i$-th element of $a$. All these integers are pairwise distinct.\n\n\n-----Output-----\n\nIn the first line of the output print $k$ \u2014 the maximum number of elements in a strictly increasing sequence you can obtain.\n\nIn the second line print a string $s$ of length $k$, where the $j$-th character of this string $s_j$ should be 'L' if you take the leftmost element during the $j$-th move and 'R' otherwise. If there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n5\n2 1 5 4 3\n\nOutput\n4\nLRRR\n\nInput\n7\n1 3 5 6 7 4 2\n\nOutput\n7\nLRLRLLL\n\nInput\n3\n1 2 3\n\nOutput\n3\nLLL\n\nInput\n4\n1 2 4 3\n\nOutput\n4\nLLRL\n\n\n\n-----Note-----\n\nThe first example is described in the problem statement."} +{"id": "03117", "text": "You are given an array $a$ consisting of $n$ integers.\n\nYou can remove at most one element from this array. Thus, the final length of the array is $n-1$ or $n$.\n\nYour task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.\n\nRecall that the contiguous subarray $a$ with indices from $l$ to $r$ is $a[l \\dots r] = a_l, a_{l + 1}, \\dots, a_r$. The subarray $a[l \\dots r]$ is called strictly increasing if $a_l < a_{l+1} < \\dots < a_r$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible length of the strictly increasing contiguous subarray of the array $a$ after removing at most one element.\n\n\n-----Examples-----\nInput\n5\n1 2 5 3 4\n\nOutput\n4\n\nInput\n2\n1 2\n\nOutput\n2\n\nInput\n7\n6 5 4 3 2 4 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example, you can delete $a_3=5$. Then the resulting array will be equal to $[1, 2, 3, 4]$ and the length of its largest increasing subarray will be equal to $4$."} +{"id": "03118", "text": "The only difference between easy and hard versions is the number of elements in the array.\n\nYou are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \\lfloor\\frac{a_i}{2}\\rfloor$).\n\nYou can perform such an operation any (possibly, zero) number of times with any $a_i$.\n\nYour task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array.\n\nDon't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 50$) \u2014 the number of elements in the array and the number of equal numbers required.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible number of operations required to obtain at least $k$ equal numbers in the array.\n\n\n-----Examples-----\nInput\n5 3\n1 2 2 4 5\n\nOutput\n1\n\nInput\n5 3\n1 2 3 4 5\n\nOutput\n2\n\nInput\n5 3\n1 2 3 3 3\n\nOutput\n0"} +{"id": "03119", "text": "Takahashi has many red balls and blue balls. Now, he will place them in a row.\nInitially, there is no ball placed.\nTakahashi, who is very patient, will do the following operation 10^{100} times:\n - Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.\nHow many blue balls will be there among the first N balls in the row of balls made this way?\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^{18}\n - A, B \\geq 0\n - 0 < A + B \\leq 10^{18}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN A B\n\n-----Output-----\nPrint the number of blue balls that will be there among the first N balls in the row of balls.\n\n-----Sample Input-----\n8 3 4\n\n-----Sample Output-----\n4\n\nLet b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls."} +{"id": "03120", "text": "We guessed some integer number $x$. You are given a list of almost all its divisors. Almost all means that there are all divisors except $1$ and $x$ in the list.\n\nYour task is to find the minimum possible integer $x$ that can be the guessed number, or say that the input data is contradictory and it is impossible to find such number.\n\nYou have to answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 25$) \u2014 the number of queries. Then $t$ queries follow.\n\nThe first line of the query contains one integer $n$ ($1 \\le n \\le 300$) \u2014 the number of divisors in the list.\n\nThe second line of the query contains $n$ integers $d_1, d_2, \\dots, d_n$ ($2 \\le d_i \\le 10^6$), where $d_i$ is the $i$-th divisor of the guessed number. It is guaranteed that all values $d_i$ are distinct.\n\n\n-----Output-----\n\nFor each query print the answer to it.\n\nIf the input data in the query is contradictory and it is impossible to find such number $x$ that the given list of divisors is the list of almost all its divisors, print -1. Otherwise print the minimum possible $x$.\n\n\n-----Example-----\nInput\n2\n8\n8 2 12 6 4 24 16 3\n1\n2\n\nOutput\n48\n4"} +{"id": "03121", "text": "Petya has an array $a$ consisting of $n$ integers. He wants to remove duplicate (equal) elements.\n\nPetya wants to leave only the rightmost entry (occurrence) for each element of the array. The relative order of the remaining unique elements should not be changed.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 50$) \u2014 the number of elements in Petya's array.\n\nThe following line contains a sequence $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 1\\,000$) \u2014 the Petya's array.\n\n\n-----Output-----\n\nIn the first line print integer $x$ \u2014 the number of elements which will be left in Petya's array after he removed the duplicates.\n\nIn the second line print $x$ integers separated with a space \u2014 Petya's array after he removed the duplicates. For each unique element only the rightmost entry should be left.\n\n\n-----Examples-----\nInput\n6\n1 5 5 1 6 1\n\nOutput\n3\n5 6 1 \n\nInput\n5\n2 4 2 4 4\n\nOutput\n2\n2 4 \n\nInput\n5\n6 6 6 6 6\n\nOutput\n1\n6 \n\n\n\n-----Note-----\n\nIn the first example you should remove two integers $1$, which are in the positions $1$ and $4$. Also you should remove the integer $5$, which is in the position $2$.\n\nIn the second example you should remove integer $2$, which is in the position $1$, and two integers $4$, which are in the positions $2$ and $4$.\n\nIn the third example you should remove four integers $6$, which are in the positions $1$, $2$, $3$ and $4$."} +{"id": "03122", "text": "Polycarp knows that if the sum of the digits of a number is divisible by $3$, then the number itself is divisible by $3$. He assumes that the numbers, the sum of the digits of which is divisible by $4$, are also somewhat interesting. Thus, he considers a positive integer $n$ interesting if its sum of digits is divisible by $4$.\n\nHelp Polycarp find the nearest larger or equal interesting number for the given number $a$. That is, find the interesting number $n$ such that $n \\ge a$ and $n$ is minimal.\n\n\n-----Input-----\n\nThe only line in the input contains an integer $a$ ($1 \\le a \\le 1000$).\n\n\n-----Output-----\n\nPrint the nearest greater or equal interesting number for the given number $a$. In other words, print the interesting number $n$ such that $n \\ge a$ and $n$ is minimal.\n\n\n-----Examples-----\nInput\n432\n\nOutput\n435\n\nInput\n99\n\nOutput\n103\n\nInput\n237\n\nOutput\n237\n\nInput\n42\n\nOutput\n44"} +{"id": "03123", "text": "Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input.\n\nAfter that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information.\n\nSuppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \\ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$.\n\nFor example, if $t$ = \"abzb\", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. \n\nThus, if $t$ = \"abzb\", then $b=[6,1,0,1]$.\n\nGiven the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. \n\n\n-----Input-----\n\nThe first line contains an integer $q$ ($1 \\le q \\le 100$)\u00a0\u2014 the number of test cases in the test. Then $q$ test cases follow.\n\nEach test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \\le m \\le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \\dots, b_m$ ($0 \\le b_i \\le 1225$). \n\nIt is guaranteed that in each test case an answer exists.\n\n\n-----Output-----\n\nOutput $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any.\n\n\n-----Example-----\nInput\n4\nabac\n3\n2 1 0\nabc\n1\n0\nabba\n3\n1 0 1\necoosdcefr\n10\n38 13 24 14 11 5 3 24 17 0\n\nOutput\naac\nb\naba\ncodeforces\n\n\n\n-----Note-----\n\nIn the first test case, such strings $t$ are suitable: \"aac', \"aab\".\n\nIn the second test case, such trings $t$ are suitable: \"a\", \"b\", \"c\".\n\nIn the third test case, only the string $t$ equals to \"aba\" is suitable, but the character 'b' can be from the second or third position."} +{"id": "03124", "text": "1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:\n - the dogs numbered 1,2,\\cdots,26 were respectively given the names a, b, ..., z;\n - the dogs numbered 27,28,29,\\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;\n - the dogs numbered 703,704,705,\\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;\n - the dogs numbered 18279,18280,18281,\\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;\n - the dogs numbered 475255,475256,\\cdots were respectively given the names aaaaa, aaaab, ...;\n - and so on.\nTo sum it up, the dogs numbered 1, 2, \\cdots were respectively given the following names:\na, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...\nNow, Roger asks you:\n\"What is the name for the dog numbered N?\"\n\n-----Constraints-----\n - N is an integer.\n - 1 \\leq N \\leq 1000000000000001\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the answer to Roger's question as a string consisting of lowercase English letters.\n\n-----Sample Input-----\n2\n\n-----Sample Output-----\nb\n"} +{"id": "03125", "text": "You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words consist only of lowercase Latin letters.\n\nLet's denote a segment of words $w[i..j]$ as a sequence of words $w_i, w_{i + 1}, \\dots, w_j$. Two segments of words $w[i_1 .. j_1]$ and $w[i_2 .. j_2]$ are considered equal if $j_1 - i_1 = j_2 - i_2$, $j_1 \\ge i_1$, $j_2 \\ge i_2$, and for every $t \\in [0, j_1 - i_1]$ $w_{i_1 + t} = w_{i_2 + t}$. For example, for the text \"to be or not to be\" the segments $w[1..2]$ and $w[5..6]$ are equal, they correspond to the words \"to be\".\n\nAn abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text \"a ab a a b ab a a b c\" you can replace segments of words $w[2..4]$ and $w[6..8]$ with an abbreviation \"AAA\" and obtain the text \"a AAA b AAA b c\", or you can replace segments of words $w[2..5]$ and $w[6..9]$ with an abbreviation \"AAAB\" and obtain the text \"a AAAB AAAB c\".\n\nWhat is the minimum length of the text after at most one abbreviation?\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 300$) \u2014 the number of words in the text.\n\nThe next line contains $n$ space-separated words of the text $w_1, w_2, \\dots, w_n$. Each word consists only of lowercase Latin letters.\n\nIt is guaranteed that the length of text does not exceed $10^5$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum length of the text after at most one abbreviation.\n\n\n-----Examples-----\nInput\n6\nto be or not to be\n\nOutput\n12\n\nInput\n10\na ab a a b ab a a b c\n\nOutput\n13\n\nInput\n6\naa bb aa aa bb bb\n\nOutput\n11\n\n\n\n-----Note-----\n\nIn the first example you can obtain the text \"TB or not TB\".\n\nIn the second example you can obtain the text \"a AAAB AAAB c\".\n\nIn the third example you can obtain the text \"AB aa AB bb\"."} +{"id": "03126", "text": "Polycarp is practicing his problem solving skill. He has a list of $n$ problems with difficulties $a_1, a_2, \\dots, a_n$, respectively. His plan is to practice for exactly $k$ days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all $n$ problems in exactly $k$ days.\n\nThus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in $k$ days he will solve all the $n$ problems.\n\nThe profit of the $j$-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the $j$-th day (i.e. if he solves problems with indices from $l$ to $r$ during a day, then the profit of the day is $\\max\\limits_{l \\le i \\le r}a_i$). The total profit of his practice is the sum of the profits over all $k$ days of his practice.\n\nYou want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all $n$ problems between $k$ days satisfying the conditions above in such a way, that the total profit is maximum.\n\nFor example, if $n = 8, k = 3$ and $a = [5, 4, 2, 6, 5, 1, 9, 2]$, one of the possible distributions with maximum total profit is: $[5, 4, 2], [6, 5], [1, 9, 2]$. Here the total profit equals $5 + 6 + 9 = 20$.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2000$) \u2014 the number of problems and the number of days, respectively.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2000$) \u2014 difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).\n\n\n-----Output-----\n\nIn the first line of the output print the maximum possible total profit.\n\nIn the second line print exactly $k$ positive integers $t_1, t_2, \\dots, t_k$ ($t_1 + t_2 + \\dots + t_k$ must equal $n$), where $t_j$ means the number of problems Polycarp will solve during the $j$-th day in order to achieve the maximum possible total profit of his practice.\n\nIf there are many possible answers, you may print any of them.\n\n\n-----Examples-----\nInput\n8 3\n5 4 2 6 5 1 9 2\n\nOutput\n20\n3 2 3\nInput\n5 1\n1 1 1 1 1\n\nOutput\n1\n5\n\nInput\n4 2\n1 2000 2000 2\n\nOutput\n4000\n2 2\n\n\n\n-----Note-----\n\nThe first example is described in the problem statement.\n\nIn the second example there is only one possible distribution.\n\nIn the third example the best answer is to distribute problems in the following way: $[1, 2000], [2000, 2]$. The total profit of this distribution is $2000 + 2000 = 4000$."} +{"id": "03127", "text": "Kolya got an integer array $a_1, a_2, \\dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros.\n\nKolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array. \n\nYou have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).\n\nYour task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 200\\,000$) \u2014 the number of elements in Kolya's array.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^{9} \\le a_i \\le 10^{9}, a_i \\neq 0$) \u2014 the description of Kolya's array.\n\n\n-----Output-----\n\nPrint the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$.\n\n\n-----Examples-----\nInput\n4\n1 -5 3 2\n\nOutput\n1\n\nInput\n5\n4 -2 3 -9 2\n\nOutput\n0\n\nInput\n9\n-1 1 -1 1 -1 1 1 -1 -1\n\nOutput\n6\n\nInput\n8\n16 -5 -11 -15 10 5 4 -4\n\nOutput\n3\n\n\n\n-----Note-----\n\nConsider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.\n\nThere are no subsegments having sum $0$ in the second example so you don't need to do anything."} +{"id": "03128", "text": "You are given two integers $n$ and $m$. You have to construct the array $a$ of length $n$ consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly $m$ and the value $\\sum\\limits_{i=1}^{n-1} |a_i - a_{i+1}|$ is the maximum possible. Recall that $|x|$ is the absolute value of $x$.\n\nIn other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array $a=[1, 3, 2, 5, 5, 0]$ then the value above for this array is $|1-3| + |3-2| + |2-5| + |5-5| + |5-0| = 2 + 1 + 3 + 0 + 5 = 11$. Note that this example doesn't show the optimal answer but it shows how the required value for some array is calculated.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains two integers $n$ and $m$ ($1 \\le n, m \\le 10^9$) \u2014 the length of the array and its sum correspondingly.\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 the maximum possible value of $\\sum\\limits_{i=1}^{n-1} |a_i - a_{i+1}|$ for the array $a$ consisting of $n$ non-negative integers with the sum $m$.\n\n\n-----Example-----\nInput\n5\n1 100\n2 2\n5 5\n2 1000000000\n1000000000 1000000000\n\nOutput\n0\n2\n10\n1000000000\n2000000000\n\n\n\n-----Note-----\n\nIn the first test case of the example, the only possible array is $[100]$ and the answer is obviously $0$.\n\nIn the second test case of the example, one of the possible arrays is $[2, 0]$ and the answer is $|2-0| = 2$.\n\nIn the third test case of the example, one of the possible arrays is $[0, 2, 0, 3, 0]$ and the answer is $|0-2| + |2-0| + |0-3| + |3-0| = 10$."} +{"id": "03129", "text": "Takahashi loves the number 7 and multiples of K.\nWhere is the first occurrence of a multiple of K in the sequence 7,77,777,\\ldots? (Also see Output and Sample Input/Output below.)\nIf the sequence contains no multiples of K, print -1 instead.\n\n-----Constraints-----\n - 1 \\leq K \\leq 10^6\n - K is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK\n\n-----Output-----\nPrint an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)\n\n-----Sample Input-----\n101\n\n-----Sample Output-----\n4\n\nNone of 7, 77, and 777 is a multiple of 101, but 7777 is."} +{"id": "03130", "text": "You are given a permutation $p_1, p_2, \\dots, p_n$. A permutation of length $n$ is a sequence such that each integer between $1$ and $n$ occurs exactly once in the sequence.\n\nFind the number of pairs of indices $(l, r)$ ($1 \\le l \\le r \\le n$) such that the value of the median of $p_l, p_{l+1}, \\dots, p_r$ is exactly the given number $m$.\n\nThe median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.\n\nFor example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence.\n\nWrite a program to find the number of pairs of indices $(l, r)$ ($1 \\le l \\le r \\le n$) such that the value of the median of $p_l, p_{l+1}, \\dots, p_r$ is exactly the given number $m$.\n\n\n-----Input-----\n\nThe first line contains integers $n$ and $m$ ($1 \\le n \\le 2\\cdot10^5$, $1 \\le m \\le n$) \u2014 the length of the given sequence and the required value of the median.\n\nThe second line contains a permutation $p_1, p_2, \\dots, p_n$ ($1 \\le p_i \\le n$). Each integer between $1$ and $n$ occurs in $p$ exactly once.\n\n\n-----Output-----\n\nPrint the required number.\n\n\n-----Examples-----\nInput\n5 4\n2 4 5 3 1\n\nOutput\n4\n\nInput\n5 5\n1 2 3 4 5\n\nOutput\n1\n\nInput\n15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9\n\nOutput\n48\n\n\n\n-----Note-----\n\nIn the first example, the suitable pairs of indices are: $(1, 3)$, $(2, 2)$, $(2, 3)$ and $(2, 4)$."} +{"id": "03131", "text": "The only difference between easy and hard versions is the constraints.\n\nPolycarp has to write a coursework. The coursework consists of $m$ pages.\n\nPolycarp also has $n$ cups of coffee. The coffee in the $i$-th cup has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days).\n\nSurely, courseworks are not usually being written in a single day (in a perfect world of Berland, at least). Some of them require multiple days of hard work.\n\nLet's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \\dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages.\n\nIf Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day.\n\nPolycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n \\le 100$, $1 \\le m \\le 10^4$) \u2014 the number of cups of coffee and the number of pages in the coursework.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup.\n\n\n-----Output-----\n\nIf it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it.\n\n\n-----Examples-----\nInput\n5 8\n2 3 1 1 2\n\nOutput\n4\n\nInput\n7 10\n1 3 4 2 1 4 2\n\nOutput\n2\n\nInput\n5 15\n5 5 5 5 5\n\nOutput\n1\n\nInput\n5 16\n5 5 5 5 5\n\nOutput\n2\n\nInput\n5 26\n5 5 5 5 5\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days in this test.\n\nIn the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test.\n\nIn the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework.\n\nIn the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it.\n\nIn the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1."} +{"id": "03132", "text": "Polycarp likes arithmetic progressions. A sequence $[a_1, a_2, \\dots, a_n]$ is called an arithmetic progression if for each $i$ ($1 \\le i < n$) the value $a_{i+1} - a_i$ is the same. For example, the sequences $[42]$, $[5, 5, 5]$, $[2, 11, 20, 29]$ and $[3, 2, 1, 0]$ are arithmetic progressions, but $[1, 0, 1]$, $[1, 3, 9]$ and $[2, 3, 1]$ are not.\n\nIt follows from the definition that any sequence of length one or two is an arithmetic progression.\n\nPolycarp found some sequence of positive integers $[b_1, b_2, \\dots, b_n]$. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by $1$, an element can be increased by $1$, an element can be left unchanged.\n\nDetermine a minimum possible number of elements in $b$ which can be changed (by exactly one), so that the sequence $b$ becomes an arithmetic progression, or report that it is impossible.\n\nIt is possible that the resulting sequence contains element equals $0$.\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ $(1 \\le n \\le 100\\,000)$ \u2014 the number of elements in $b$.\n\nThe second line contains a sequence $b_1, b_2, \\dots, b_n$ $(1 \\le b_i \\le 10^{9})$.\n\n\n-----Output-----\n\nIf it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer \u2014 the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position).\n\n\n-----Examples-----\nInput\n4\n24 21 14 10\n\nOutput\n3\n\nInput\n2\n500 500\n\nOutput\n0\n\nInput\n3\n14 5 1\n\nOutput\n-1\n\nInput\n5\n1 3 6 9 12\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example Polycarp should increase the first number on $1$, decrease the second number on $1$, increase the third number on $1$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $[25, 20, 15, 10]$, which is an arithmetic progression.\n\nIn the second example Polycarp should not change anything, because his sequence is an arithmetic progression.\n\nIn the third example it is impossible to make an arithmetic progression.\n\nIn the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like $[0, 3, 6, 9, 12]$, which is an arithmetic progression."} +{"id": "03133", "text": "You are a coach at your local university. There are $n$ students under your supervision, the programming skill of the $i$-th student is $a_i$.\n\nYou have to form $k$ teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than $k$ (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than $5$. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).\n\nIt is possible that some students not be included in any team at all.\n\nYour task is to report the maximum possible total number of students in no more than $k$ (and at least one) non-empty balanced teams.\n\nIf you are Python programmer, consider using PyPy instead of Python when you submit your code.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 5000$) \u2014 the number of students and the maximum number of teams, correspondingly.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is a programming skill of the $i$-th student.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible total number of students in no more than $k$ (and at least one) non-empty balanced teams.\n\n\n-----Examples-----\nInput\n5 2\n1 2 15 15 15\n\nOutput\n5\n\nInput\n6 1\n36 4 1 25 9 16\n\nOutput\n2\n\nInput\n4 4\n1 10 100 1000\n\nOutput\n4"} +{"id": "03134", "text": "Takahashi is taking exams on N subjects. The score on each subject will be an integer between 0 and K (inclusive).\nHe has already taken exams on N-1 subjects and scored A_i points on the i-th subject.\nHis goal is to achieve the average score of M points or above on the N subjects.\nPrint the minimum number of points Takahashi needs on the final subject to achieve his goal.\nIf the goal is unachievable, print -1 instead.\n\n-----Constraints-----\n - 2 \\leq N \\leq 100\n - 1 \\leq K \\leq 100\n - 1 \\leq M \\leq K\n - 0 \\leq A_i \\leq K\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K M\nA_1 A_2 ... A_{N-1}\n\n-----Output-----\nPrint the minimum number of points required on the final subject, or -1.\n\n-----Sample Input-----\n5 10 7\n8 10 3 6\n\n-----Sample Output-----\n8\n\nIf he scores 8 points on the final subject, his average score will be (8+10+3+6+8)/5 = 7 points, which meets the goal."} +{"id": "03135", "text": "Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.\nA game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.\nWhen a player correctly answers a question, each of the other N-1 players receives minus one (-1) point. There is no other factor that affects the players' scores.\nAt the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.\nIn the last game, the players gave a total of Q correct answers, the i-th of which was given by Player A_i.\nFor Kizahashi, write a program that determines whether each of the N players survived this game.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 10^5\n - 1 \\leq K \\leq 10^9\n - 1 \\leq Q \\leq 10^5\n - 1 \\leq A_i \\leq N\\ (1 \\leq i \\leq Q)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K Q\nA_1\nA_2\n.\n.\n.\nA_Q\n\n-----Output-----\nPrint N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.\n\n-----Sample Input-----\n6 3 4\n3\n1\n3\n2\n\n-----Sample Output-----\nNo\nNo\nYes\nNo\nNo\nNo\n\nIn the beginning, the players' scores are (3, 3, 3, 3, 3, 3).\n - Player 3 correctly answers a question. The players' scores are now (2, 2, 3, 2, 2, 2).\n - Player 1 correctly answers a question. The players' scores are now (2, 1, 2, 1, 1, 1).\n - Player 3 correctly answers a question. The players' scores are now (1, 0, 2, 0, 0, 0).\n - Player 2 correctly answers a question. The players' scores are now (0, 0, 1, -1, -1, -1).\nPlayers 1, 2, 4, 5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game."} +{"id": "03136", "text": "You are given a binary matrix $a$ of size $n \\times m$. A binary matrix is a matrix where each element is either $0$ or $1$.\n\nYou may perform some (possibly zero) operations with this matrix. During each operation you can inverse the row of this matrix or a column of this matrix. Formally, inverting a row is changing all values in this row to the opposite ($0$ to $1$, $1$ to $0$). Inverting a column is changing all values in this column to the opposite.\n\nYour task is to sort the initial matrix by some sequence of such operations. The matrix is considered sorted if the array $[a_{1, 1}, a_{1, 2}, \\dots, a_{1, m}, a_{2, 1}, a_{2, 2}, \\dots, a_{2, m}, \\dots, a_{n, m - 1}, a_{n, m}]$ is sorted in non-descending order.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n, m \\le 200$) \u2014 the number of rows and the number of columns in the matrix.\n\nThe next $n$ lines contain $m$ integers each. The $j$-th element in the $i$-th line is $a_{i, j}$ ($0 \\le a_{i, j} \\le 1$) \u2014 the element of $a$ at position $(i, j)$.\n\n\n-----Output-----\n\nIf it is impossible to obtain a sorted matrix, print \"NO\" in the first line.\n\nOtherwise print \"YES\" in the first line. In the second line print a string $r$ of length $n$. The $i$-th character $r_i$ of this string should be '1' if the $i$-th row of the matrix is inverted and '0' otherwise. In the third line print a string $c$ of length $m$. The $j$-th character $c_j$ of this string should be '1' if the $j$-th column of the matrix is inverted and '0' otherwise. If there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n2 2\n1 1\n0 1\n\nOutput\nYES\n00\n10\n\nInput\n3 4\n0 0 0 1\n0 0 0 0\n1 1 1 1\n\nOutput\nYES\n010\n0000\n\nInput\n3 3\n0 0 0\n1 0 1\n1 1 0\n\nOutput\nNO"} +{"id": "03137", "text": "-----Input-----\n\nThe only line of the input contains a string of digits. The length of the string is between 1 and 10, inclusive.\n\n\n-----Output-----\n\nOutput \"Yes\" or \"No\".\n\n\n-----Examples-----\nInput\n373\n\nOutput\nYes\n\nInput\n121\n\nOutput\nNo\n\nInput\n436\n\nOutput\nYes"} +{"id": "03138", "text": "There is a robot staying at $X=0$ on the $Ox$ axis. He has to walk to $X=n$. You are controlling this robot and controlling how he goes. The robot has a battery and an accumulator with a solar panel.\n\nThe $i$-th segment of the path (from $X=i-1$ to $X=i$) can be exposed to sunlight or not. The array $s$ denotes which segments are exposed to sunlight: if segment $i$ is exposed, then $s_i = 1$, otherwise $s_i = 0$.\n\nThe robot has one battery of capacity $b$ and one accumulator of capacity $a$. For each segment, you should choose which type of energy storage robot will use to go to the next point (it can be either battery or accumulator). If the robot goes using the battery, the current charge of the battery is decreased by one (the robot can't use the battery if its charge is zero). And if the robot goes using the accumulator, the current charge of the accumulator is decreased by one (and the robot also can't use the accumulator if its charge is zero).\n\nIf the current segment is exposed to sunlight and the robot goes through it using the battery, the charge of the accumulator increases by one (of course, its charge can't become higher than it's maximum capacity).\n\nIf accumulator is used to pass some segment, its charge decreases by 1 no matter if the segment is exposed or not.\n\nYou understand that it is not always possible to walk to $X=n$. You want your robot to go as far as possible. Find the maximum number of segments of distance the robot can pass if you control him optimally.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n, b, a$ ($1 \\le n, b, a \\le 2 \\cdot 10^5$) \u2014 the robot's destination point, the battery capacity and the accumulator capacity, respectively.\n\nThe second line of the input contains $n$ integers $s_1, s_2, \\dots, s_n$ ($0 \\le s_i \\le 1$), where $s_i$ is $1$ if the $i$-th segment of distance is exposed to sunlight, and $0$ otherwise.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of segments the robot can pass if you control him optimally.\n\n\n-----Examples-----\nInput\n5 2 1\n0 1 0 1 0\n\nOutput\n5\n\nInput\n6 2 1\n1 0 0 1 0 1\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example the robot can go through the first segment using the accumulator, and charge levels become $b=2$ and $a=0$. The second segment can be passed using the battery, and charge levels become $b=1$ and $a=1$. The third segment can be passed using the accumulator, and charge levels become $b=1$ and $a=0$. The fourth segment can be passed using the battery, and charge levels become $b=0$ and $a=1$. And the fifth segment can be passed using the accumulator.\n\nIn the second example the robot can go through the maximum number of segments using battery two times and accumulator one time in any order."} +{"id": "03139", "text": "One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluating this expression. However, looks like there is a bug in the reference solution...\n\n\n-----Input-----\n\nThe only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive.\n\n\n-----Output-----\n\nReproduce the output of the reference solution, including the bug.\n\n\n-----Examples-----\nInput\n8-7+6-5+4-3+2-1-0\n\nOutput\n4\n\nInput\n2+2\n\nOutput\n-46\n\nInput\n112-37\n\nOutput\n375"} +{"id": "03140", "text": "The king of Berland organizes a ball! $n$ pair are invited to the ball, they are numbered from $1$ to $n$. Each pair consists of one man and one woman. Each dancer (either man or woman) has a monochrome costume. The color of each costume is represented by an integer from $1$ to $k$, inclusive.\n\nLet $b_i$ be the color of the man's costume and $g_i$ be the color of the woman's costume in the $i$-th pair. You have to choose a color for each dancer's costume (i.e. values $b_1, b_2, \\dots, b_n$ and $g_1, g_2, \\dots g_n$) in such a way that: for every $i$: $b_i$ and $g_i$ are integers between $1$ and $k$, inclusive; there are no two completely identical pairs, i.e. no two indices $i, j$ ($i \\ne j$) such that $b_i = b_j$ and $g_i = g_j$ at the same time; there is no pair such that the color of the man's costume is the same as the color of the woman's costume in this pair, i.e. $b_i \\ne g_i$ for every $i$; for each two consecutive (adjacent) pairs both man's costume colors and woman's costume colors differ, i.e. for every $i$ from $1$ to $n-1$ the conditions $b_i \\ne b_{i + 1}$ and $g_i \\ne g_{i + 1}$ hold. \n\nLet's take a look at the examples of bad and good color choosing (for $n=4$ and $k=3$, man is the first in a pair and woman is the second):\n\nBad color choosing: $(1, 2)$, $(2, 3)$, $(3, 2)$, $(1, 2)$ \u2014 contradiction with the second rule (there are equal pairs); $(2, 3)$, $(1, 1)$, $(3, 2)$, $(1, 3)$ \u2014 contradiction with the third rule (there is a pair with costumes of the same color); $(1, 2)$, $(2, 3)$, $(1, 3)$, $(2, 1)$ \u2014 contradiction with the fourth rule (there are two consecutive pairs such that colors of costumes of men/women are the same). \n\nGood color choosing: $(1, 2)$, $(2, 1)$, $(1, 3)$, $(3, 1)$; $(1, 2)$, $(3, 1)$, $(2, 3)$, $(3, 2)$; $(3, 1)$, $(1, 2)$, $(2, 3)$, $(3, 2)$. \n\nYou have to find any suitable color choosing or say that no suitable choosing exists.\n\n\n-----Input-----\n\nThe only line of the input contains two integers $n$ and $k$ ($2 \\le n, k \\le 2 \\cdot 10^5$) \u2014 the number of pairs and the number of colors.\n\n\n-----Output-----\n\nIf it is impossible to find any suitable colors choosing, print \"NO\".\n\nOtherwise print \"YES\" and then the colors of the costumes of pairs in the next $n$ lines. The $i$-th line should contain two integers $b_i$ and $g_i$ \u2014 colors of costumes of man and woman in the $i$-th pair, respectively.\n\nYou can print each letter in any case (upper or lower). For example, \"YeS\", \"no\" and \"yES\" are all acceptable.\n\n\n-----Examples-----\nInput\n4 3\n\nOutput\nYES\n3 1\n1 3\n3 2\n2 3\n\nInput\n10 4\n\nOutput\nYES\n2 1\n1 3\n4 2\n3 4\n4 3\n3 2\n2 4\n4 1\n1 4\n3 1\n\nInput\n13 4\n\nOutput\nNO"} +{"id": "03141", "text": "The only difference between easy and hard versions is the constraints.\n\nVova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$.\n\nVova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. \n\nFor example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.\n\nYour task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n, k$ and $x$ ($1 \\le k, x \\le n \\le 200$) \u2014 the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the beauty of the $i$-th picture.\n\n\n-----Output-----\n\nPrint -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement.\n\nOtherwise print one integer \u2014 the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.\n\n\n-----Examples-----\nInput\n5 2 3\n5 1 3 10 1\n\nOutput\n18\n\nInput\n6 1 5\n10 30 30 70 10 10\n\nOutput\n-1\n\nInput\n4 3 1\n1 100 1 1\n\nOutput\n100"} +{"id": "03142", "text": "You work as a system administrator in a dormitory, which has $n$ rooms one after another along a straight hallway. Rooms are numbered from $1$ to $n$.\n\nYou have to connect all $n$ rooms to the Internet.\n\nYou can connect each room to the Internet directly, the cost of such connection for the $i$-th room is $i$ coins. \n\nSome rooms also have a spot for a router. The cost of placing a router in the $i$-th room is also $i$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $i$, you connect all rooms with the numbers from $max(1,~i - k)$ to $min(n,~i + k)$ inclusive to the Internet, where $k$ is the range of router. The value of $k$ is the same for all routers. \n\nCalculate the minimum total cost of connecting all $n$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n, k \\le 2 \\cdot 10^5$) \u2014 the number of rooms and the range of each router.\n\nThe second line of the input contains one string $s$ of length $n$, consisting only of zeros and ones. If the $i$-th character of the string equals to '1' then there is a spot for a router in the $i$-th room. If the $i$-th character of the string equals to '0' then you cannot place a router in the $i$-th room.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum total cost of connecting all $n$ rooms to the Internet.\n\n\n-----Examples-----\nInput\n5 2\n00100\n\nOutput\n3\n\nInput\n6 1\n000000\n\nOutput\n21\n\nInput\n4 1\n0011\n\nOutput\n4\n\nInput\n12 6\n000010000100\n\nOutput\n15\n\n\n\n-----Note-----\n\nIn the first example it is enough to place the router in the room $3$, then all rooms will be connected to the Internet. The total cost of connection is $3$.\n\nIn the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $1 + 2 + 3 + 4 + 5 + 6 = 21$.\n\nIn the third example you need to connect the room $1$ directly and place the router in the room $3$. Thus, the total cost of connection of all rooms is $1 + 3 = 4$.\n\nIn the fourth example you need to place routers in rooms $5$ and $10$. Then all rooms will be connected to the Internet. The total cost of connection is $5 + 10 = 15$."} +{"id": "03143", "text": "You are given strings S and T consisting of lowercase English letters.\nYou can perform the following operation on S any number of times:\nOperation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.\nDetermine if S and T can be made equal by performing the operation zero or more times.\n\n-----Constraints-----\n - 1 \\leq |S| \\leq 2 \\times 10^5\n - |S| = |T|\n - S and T consists of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\nT\n\n-----Output-----\nIf S and T can be made equal, print Yes; otherwise, print No.\n\n-----Sample Input-----\nazzel\napple\n\n-----Sample Output-----\nYes\n\nazzel can be changed to apple, as follows:\n - Choose e as c_1 and l as c_2. azzel becomes azzle.\n - Choose z as c_1 and p as c_2. azzle becomes apple."} +{"id": "03144", "text": "Takahashi, who is a novice in competitive programming, wants to learn M algorithms.\nInitially, his understanding level of each of the M algorithms is 0.\nTakahashi is visiting a bookstore, where he finds N books on algorithms.\nThe i-th book (1\\leq i\\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\\leq j\\leq M).\nThere is no other way to increase the understanding levels of the algorithms.\nTakahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.\n\n"} +{"id": "03145", "text": "A programming competition site AtCode provides algorithmic problems.\nEach problem is allocated a score based on its difficulty.\nCurrently, for each integer i between 1 and D (inclusive), there are p_i problems with a score of 100i points.\nThese p_1 + \u2026 + p_D problems are all of the problems available on AtCode.\nA user of AtCode has a value called total score.\nThe total score of a user is the sum of the following two elements:\n - Base score: the sum of the scores of all problems solved by the user.\n - Perfect bonuses: when a user solves all problems with a score of 100i points, he/she earns the perfect bonus of c_i points, aside from the base score (1 \u2264 i \u2264 D).\nTakahashi, who is the new user of AtCode, has not solved any problem.\nHis objective is to have a total score of G or more points.\nAt least how many problems does he need to solve for this objective?\n\n-----Constraints-----\n - 1 \u2264 D \u2264 10\n - 1 \u2264 p_i \u2264 100\n - 100 \u2264 c_i \u2264 10^6\n - 100 \u2264 G\n - All values in input are integers.\n - c_i and G are all multiples of 100.\n - It is possible to have a total score of G or more points.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nD G\np_1 c_1\n:\np_D c_D\n\n-----Output-----\nPrint the minimum number of problems that needs to be solved in order to have a total score of G or more points. Note that this objective is always achievable (see Constraints).\n\n-----Sample Input-----\n2 700\n3 500\n5 800\n\n-----Sample Output-----\n3\n\nIn this case, there are three problems each with 100 points and five problems each with 200 points. The perfect bonus for solving all the 100-point problems is 500 points, and the perfect bonus for solving all the 200-point problems is 800 points. Takahashi's objective is to have a total score of 700 points or more.\nOne way to achieve this objective is to solve four 200-point problems and earn a base score of 800 points. However, if we solve three 100-point problems, we can earn the perfect bonus of 500 points in addition to the base score of 300 points, for a total score of 800 points, and we can achieve the objective with fewer problems."} +{"id": "03146", "text": "Tanya has $n$ candies numbered from $1$ to $n$. The $i$-th candy has the weight $a_i$.\n\nShe plans to eat exactly $n-1$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.\n\nYour task is to find the number of such candies $i$ (let's call these candies good) that if dad gets the $i$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.\n\nFor example, $n=4$ and weights are $[1, 4, 3, 3]$. Consider all possible cases to give a candy to dad: Tanya gives the $1$-st candy to dad ($a_1=1$), the remaining candies are $[4, 3, 3]$. She will eat $a_2=4$ in the first day, $a_3=3$ in the second day, $a_4=3$ in the third day. So in odd days she will eat $4+3=7$ and in even days she will eat $3$. Since $7 \\ne 3$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $2$-nd candy to dad ($a_2=4$), the remaining candies are $[1, 3, 3]$. She will eat $a_1=1$ in the first day, $a_3=3$ in the second day, $a_4=3$ in the third day. So in odd days she will eat $1+3=4$ and in even days she will eat $3$. Since $4 \\ne 3$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $3$-rd candy to dad ($a_3=3$), the remaining candies are $[1, 4, 3]$. She will eat $a_1=1$ in the first day, $a_2=4$ in the second day, $a_4=3$ in the third day. So in odd days she will eat $1+3=4$ and in even days she will eat $4$. Since $4 = 4$ this case should be counted to the answer (this candy is good). Tanya gives the $4$-th candy to dad ($a_4=3$), the remaining candies are $[1, 4, 3]$. She will eat $a_1=1$ in the first day, $a_2=4$ in the second day, $a_3=3$ in the third day. So in odd days she will eat $1+3=4$ and in even days she will eat $4$. Since $4 = 4$ this case should be counted to the answer (this candy is good). \n\nIn total there $2$ cases which should counted (these candies are good), so the answer is $2$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of candies.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^4$), where $a_i$ is the weight of the $i$-th candy.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of such candies $i$ (good candies) that if dad gets the $i$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days.\n\n\n-----Examples-----\nInput\n7\n5 5 4 5 5 5 6\n\nOutput\n2\n\nInput\n8\n4 8 8 7 8 4 4 5\n\nOutput\n2\n\nInput\n9\n2 3 4 2 2 3 2 2 4\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example indices of good candies are $[1, 2]$.\n\nIn the second example indices of good candies are $[2, 3]$.\n\nIn the third example indices of good candies are $[4, 5, 9]$."} +{"id": "03147", "text": "The only difference between easy and hard versions is the constraints.\n\nVova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$.\n\nVova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. \n\nFor example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.\n\nYour task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n, k$ and $x$ ($1 \\le k, x \\le n \\le 5000$) \u2014 the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the beauty of the $i$-th picture.\n\n\n-----Output-----\n\nPrint -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement.\n\nOtherwise print one integer \u2014 the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.\n\n\n-----Examples-----\nInput\n5 2 3\n5 1 3 10 1\n\nOutput\n18\n\nInput\n6 1 5\n10 30 30 70 10 10\n\nOutput\n-1\n\nInput\n4 3 1\n1 100 1 1\n\nOutput\n100"} +{"id": "03148", "text": "La Confiserie d'ABC sells cakes at 4 dollars each and doughnuts at 7 dollars each.\nDetermine if there is a way to buy some of them for exactly N dollars. You can buy two or more doughnuts and two or more cakes, and you can also choose to buy zero doughnuts or zero cakes.\n\n-----Constraints-----\n - N is an integer between 1 and 100, inclusive.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nIf there is a way to buy some cakes and some doughnuts for exactly N dollars, print Yes; otherwise, print No.\n\n-----Sample Input-----\n11\n\n-----Sample Output-----\nYes\n\nIf you buy one cake and one doughnut, the total will be 4 + 7 = 11 dollars."} +{"id": "03149", "text": "In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.\n\nThe pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0). \nAoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information: \n - C_X, C_Y was integers between 0 and 100 (inclusive), and H was an integer not less than 1. \n - Additionally, he obtained N pieces of information. The i-th of them is: \"the altitude of point (x_i, y_i) is h_i.\" \nThis was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above. \n\n-----Constraints-----\n - N is an integer between 1 and 100 (inclusive).\n - x_i and y_i are integers between 0 and 100 (inclusive).\n - h_i is an integer between 0 and 10^9 (inclusive).\n - The N coordinates (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_N, y_N) are all different.\n - The center coordinates and the height of the pyramid can be uniquely identified.\n\n-----Input-----\nInput is given from Standard Input in the following format: \nN\nx_1 y_1 h_1\nx_2 y_2 h_2\nx_3 y_3 h_3\n:\nx_N y_N h_N\n\n-----Output-----\nPrint values C_X, C_Y and H representing the center coordinates and the height of the pyramid in one line, with spaces in between. \n\n-----Sample Input-----\n4\n2 3 5\n2 1 5\n1 2 5\n3 2 5\n\n-----Sample Output-----\n2 2 6\n\nIn this case, the center coordinates and the height can be identified as (2, 2) and 6."} +{"id": "03150", "text": "Takahashi loves palindromes. Non-palindromic strings are unacceptable to him. Each time he hugs a string, he can change one of its characters to any character of his choice.\nGiven is a string S. Find the minimum number of hugs needed to make S palindromic.\n\n-----Constraints-----\n - S is a string consisting of lowercase English letters.\n - The length of S is between 1 and 100 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the minimum number of hugs needed to make S palindromic.\n\n-----Sample Input-----\nredcoder\n\n-----Sample Output-----\n1\n\nFor example, we can change the fourth character to o and get a palindrome redooder."} +{"id": "03151", "text": "Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together.\nGiven an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print Yes; if it cannot, print No.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nIf N can be represented as the product of two integers between 1 and 9 (inclusive), print Yes; if it cannot, print No.\n\n-----Sample Input-----\n10\n\n-----Sample Output-----\nYes\n\n10 can be represented as, for example, 2 \\times 5."} +{"id": "03152", "text": "We have sticks numbered 1, \\cdots, N. The length of Stick i (1 \\leq i \\leq N) is L_i.\nIn how many ways can we choose three of the sticks with different lengths that can form a triangle?\nThat is, find the number of triples of integers (i, j, k) (1 \\leq i < j < k \\leq N) that satisfy both of the following conditions:\n - L_i, L_j, and L_k are all different.\n - There exists a triangle whose sides have lengths L_i, L_j, and L_k.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 1 \\leq L_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nL_1 L_2 \\cdots L_N\n\n-----Output-----\nPrint the number of ways to choose three of the sticks with different lengths that can form a triangle.\n\n-----Sample Input-----\n5\n4 4 9 7 5\n\n-----Sample Output-----\n5\n\nThe following five triples (i, j, k) satisfy the conditions: (1, 3, 4), (1, 4, 5), (2, 3, 4), (2, 4, 5), and (3, 4, 5)."} +{"id": "03153", "text": "Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. He cannot do any other calculation.\nGiven are two integers A and B.\nIf Takahashi can calculate A \\times B, print the result; if he cannot, print -1 instead.\n\n-----Constraints-----\n - 1 \\leq A \\leq 20\n - 1 \\leq B \\leq 20\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nIf Takahashi can calculate A \\times B, print the result; if he cannot, print -1.\n\n-----Sample Input-----\n2 5\n\n-----Sample Output-----\n10\n\n2 \\times 5 = 10."} +{"id": "03154", "text": "We will play a one-player game using a number line and N pieces.\nFirst, we place each of these pieces at some integer coordinate.\nHere, multiple pieces can be placed at the same coordinate.\nOur objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:\nMove: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.\nNote that the coordinates where we initially place the pieces are already regarded as visited.\nFind the minimum number of moves required to achieve the objective.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^5\n - -10^5 \\leq X_i \\leq 10^5\n - X_1, X_2, ..., X_M are all different.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nX_1 X_2 ... X_M\n\n-----Output-----\nFind the minimum number of moves required to achieve the objective.\n\n-----Sample Input-----\n2 5\n10 12 1 2 14\n\n-----Sample Output-----\n5\n\nThe objective can be achieved in five moves as follows, and this is the minimum number of moves required.\n - Initially, put the two pieces at coordinates 1 and 10.\n - Move the piece at coordinate 1 to 2.\n - Move the piece at coordinate 10 to 11.\n - Move the piece at coordinate 11 to 12.\n - Move the piece at coordinate 12 to 13.\n - Move the piece at coordinate 13 to 14."} +{"id": "03155", "text": "There are $n$ cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from $1$ to $n$.\n\nIt is known that, from the capital (the city with the number $1$), you can reach any other city by moving along the roads.\n\nThe President of Berland plans to improve the country's road network. The budget is enough to repair exactly $n-1$ roads. The President plans to choose a set of $n-1$ roads such that:\n\n it is possible to travel from the capital to any other city along the $n-1$ chosen roads, if $d_i$ is the number of roads needed to travel from the capital to city $i$, moving only along the $n-1$ chosen roads, then $d_1 + d_2 + \\dots + d_n$ is minimized (i.e. as minimal as possible). \n\nIn other words, the set of $n-1$ roads should preserve the connectivity of the country, and the sum of distances from city $1$ to all cities should be minimized (where you can only use the $n-1$ chosen roads).\n\nThe president instructed the ministry to prepare $k$ possible options to choose $n-1$ roads so that both conditions above are met.\n\nWrite a program that will find $k$ possible ways to choose roads for repair. If there are fewer than $k$ ways, then the program should output all possible valid ways to choose roads.\n\n\n-----Input-----\n\nThe first line of the input contains integers $n$, $m$ and $k$ ($2 \\le n \\le 2\\cdot10^5, n-1 \\le m \\le 2\\cdot10^5, 1 \\le k \\le 2\\cdot10^5$), where $n$ is the number of cities in the country, $m$ is the number of roads and $k$ is the number of options to choose a set of roads for repair. It is guaranteed that $m \\cdot k \\le 10^6$.\n\nThe following $m$ lines describe the roads, one road per line. Each line contains two integers $a_i$, $b_i$ ($1 \\le a_i, b_i \\le n$, $a_i \\ne b_i$) \u2014 the numbers of the cities that the $i$-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.\n\n\n-----Output-----\n\nPrint $t$ ($1 \\le t \\le k$) \u2014 the number of ways to choose a set of roads for repair. Recall that you need to find $k$ different options; if there are fewer than $k$ of them, then you need to find all possible different valid options.\n\nIn the following $t$ lines, print the options, one per line. Print an option as a string of $m$ characters where the $j$-th character is equal to '1' if the $j$-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the $t$ lines should be different.\n\nSince it is guaranteed that $m \\cdot k \\le 10^6$, the total length of all the $t$ lines will not exceed $10^6$.\n\nIf there are several answers, output any of them.\n\n\n-----Examples-----\nInput\n4 4 3\n1 2\n2 3\n1 4\n4 3\n\nOutput\n2\n1110\n1011\n\nInput\n4 6 3\n1 2\n2 3\n1 4\n4 3\n2 4\n1 3\n\nOutput\n1\n101001\n\nInput\n5 6 2\n1 2\n1 3\n2 4\n2 5\n3 4\n3 5\n\nOutput\n2\n111100\n110110"} +{"id": "03156", "text": "Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.\n\nThe current state of the wall can be respresented by a sequence $a$ of $n$ integers, with $a_i$ being the height of the $i$-th part of the wall.\n\nVova can only use $2 \\times 1$ bricks to put in the wall (he has infinite supply of them, however).\n\nVova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $i$ the current height of part $i$ is the same as for part $i + 1$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $1$ of the wall or to the right of part $n$ of it).\n\nThe next paragraph is specific to the version 1 of the problem.\n\nVova can also put bricks vertically. That means increasing height of any part of the wall by 2.\n\nVova is a perfectionist, so he considers the wall completed when:\n\n all parts of the wall has the same height; the wall has no empty spaces inside it. \n\nCan Vova complete the wall using any amount of bricks (possibly zero)?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of parts in the wall.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) \u2014 the initial heights of the parts of the wall.\n\n\n-----Output-----\n\nPrint \"YES\" if Vova can complete the wall using any amount of bricks (possibly zero).\n\nPrint \"NO\" otherwise.\n\n\n-----Examples-----\nInput\n5\n2 1 1 2 5\n\nOutput\nYES\n\nInput\n3\n4 5 3\n\nOutput\nYES\n\nInput\n2\n10 10\n\nOutput\nYES\n\nInput\n3\n1 2 3\n\nOutput\nNO\n\n\n\n-----Note-----\n\nIn the first example Vova can put a brick on parts 2 and 3 to make the wall $[2, 2, 2, 2, 5]$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $[5, 5, 5, 5, 5]$.\n\nIn the second example Vova can put a brick vertically on part 3 to make the wall $[4, 5, 5]$, then horizontally on parts 2 and 3 to make it $[4, 6, 6]$ and then vertically on part 1 to make it $[6, 6, 6]$.\n\nIn the third example the wall is already complete."} +{"id": "03157", "text": "A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly $n$ minutes. After a round ends, the next round starts immediately. This is repeated over and over again.\n\nEach round has the same scenario. It is described by a sequence of $n$ numbers: $d_1, d_2, \\dots, d_n$ ($-10^6 \\le d_i \\le 10^6$). The $i$-th element means that monster's hp (hit points) changes by the value $d_i$ during the $i$-th minute of each round. Formally, if before the $i$-th minute of a round the monster's hp is $h$, then after the $i$-th minute it changes to $h := h + d_i$.\n\nThe monster's initial hp is $H$. It means that before the battle the monster has $H$ hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to $0$. Print -1 if the battle continues infinitely.\n\n\n-----Input-----\n\nThe first line contains two integers $H$ and $n$ ($1 \\le H \\le 10^{12}$, $1 \\le n \\le 2\\cdot10^5$). The second line contains the sequence of integers $d_1, d_2, \\dots, d_n$ ($-10^6 \\le d_i \\le 10^6$), where $d_i$ is the value to change monster's hp in the $i$-th minute of a round.\n\n\n-----Output-----\n\nPrint -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer $k$ such that $k$ is the first minute after which the monster is dead.\n\n\n-----Examples-----\nInput\n1000 6\n-100 -200 -300 125 77 -4\n\nOutput\n9\n\nInput\n1000000000000 5\n-1 0 0 0 0\n\nOutput\n4999999999996\n\nInput\n10 4\n-3 -6 5 4\n\nOutput\n-1"} +{"id": "03158", "text": "Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, \"AZ\", \"AA\", \"ZA\" \u2014 three distinct two-grams.\n\nYou are given a string $s$ consisting of $n$ capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times. For example, for string $s$ = \"BBAABBBA\" the answer is two-gram \"BB\", which contained in $s$ three times. In other words, find any most frequent two-gram.\n\nNote that occurrences of the two-gram can overlap with each other.\n\n\n-----Input-----\n\nThe first line of the input contains integer number $n$ ($2 \\le n \\le 100$) \u2014 the length of string $s$. The second line of the input contains the string $s$ consisting of $n$ capital Latin letters.\n\n\n-----Output-----\n\nPrint the only line containing exactly two capital Latin letters \u2014 any two-gram contained in the given string $s$ as a substring (i.e. two consecutive characters of the string) maximal number of times.\n\n\n-----Examples-----\nInput\n7\nABACABA\n\nOutput\nAB\n\nInput\n5\nZZZAA\n\nOutput\nZZ\n\n\n\n-----Note-----\n\nIn the first example \"BA\" is also valid answer.\n\nIn the second example the only two-gram \"ZZ\" can be printed because it contained in the string \"ZZZAA\" two times."} +{"id": "03159", "text": "You are given two strings $s$ and $t$. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by $1$. You can't choose a string if it is empty.\n\nFor example: by applying a move to the string \"where\", the result is the string \"here\", by applying a move to the string \"a\", the result is an empty string \"\". \n\nYou are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.\n\nWrite a program that finds the minimum number of moves to make two given strings $s$ and $t$ equal.\n\n\n-----Input-----\n\nThe first line of the input contains $s$. In the second line of the input contains $t$. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and $2\\cdot10^5$, inclusive.\n\n\n-----Output-----\n\nOutput the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.\n\n\n-----Examples-----\nInput\ntest\nwest\n\nOutput\n2\n\nInput\ncodeforces\nyes\n\nOutput\n9\n\nInput\ntest\nyes\n\nOutput\n7\n\nInput\nb\nab\n\nOutput\n1\n\n\n\n-----Note-----\n\nIn the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to \"est\".\n\nIn the second example, the move should be applied to the string \"codeforces\" $8$ times. As a result, the string becomes \"codeforces\" $\\to$ \"es\". The move should be applied to the string \"yes\" once. The result is the same string \"yes\" $\\to$ \"es\".\n\nIn the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.\n\nIn the fourth example, the first character of the second string should be deleted."} +{"id": "03160", "text": "There are N cities on a number line. The i-th city is located at coordinate x_i.\nYour objective is to visit all these cities at least once.\nIn order to do so, you will first set a positive integer D.\nThen, you will depart from coordinate X and perform Move 1 and Move 2 below, as many times as you like:\n - Move 1: travel from coordinate y to coordinate y + D.\n - Move 2: travel from coordinate y to coordinate y - D.\nFind the maximum value of D that enables you to visit all the cities.\nHere, to visit a city is to travel to the coordinate where that city is located.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq X \\leq 10^9\n - 1 \\leq x_i \\leq 10^9\n - x_i are all different.\n - x_1, x_2, ..., x_N \\neq X\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN X\nx_1 x_2 ... x_N\n\n-----Output-----\nPrint the maximum value of D that enables you to visit all the cities.\n\n-----Sample Input-----\n3 3\n1 7 11\n\n-----Sample Output-----\n2\n\nSetting D = 2 enables you to visit all the cities as follows, and this is the maximum value of such D.\n - Perform Move 2 to travel to coordinate 1.\n - Perform Move 1 to travel to coordinate 3.\n - Perform Move 1 to travel to coordinate 5.\n - Perform Move 1 to travel to coordinate 7.\n - Perform Move 1 to travel to coordinate 9.\n - Perform Move 1 to travel to coordinate 11."} +{"id": "03161", "text": "A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:\n - S is a palindrome.\n - Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.\n - The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.\nDetermine whether S is a strong palindrome.\n\n-----Constraints-----\n - S consists of lowercase English letters.\n - The length of S is an odd number between 3 and 99 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nIf S is a strong palindrome, print Yes;\notherwise, print No.\n\n-----Sample Input-----\nakasaka\n\n-----Sample Output-----\nYes\n\n - S is akasaka.\n - The string formed by the 1-st through the 3-rd characters is aka.\n - The string formed by the 5-th through the 7-th characters is aka.\nAll of these are palindromes, so S is a strong palindrome."} +{"id": "03162", "text": "Compute A \\times B, truncate its fractional part, and print the result as an integer.\n\n-----Constraints-----\n - 0 \\leq A \\leq 10^{15}\n - 0 \\leq B < 10\n - A is an integer.\n - B is a number with two digits after the decimal point.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the answer as an integer.\n\n-----Sample Input-----\n198 1.10\n\n-----Sample Output-----\n217\n\nWe have 198 \\times 1.10 = 217.8. After truncating the fractional part, we have the answer: 217."} +{"id": "03163", "text": "There are two sisters Alice and Betty. You have $n$ candies. You want to distribute these $n$ candies between two sisters in such a way that: Alice will get $a$ ($a > 0$) candies; Betty will get $b$ ($b > 0$) candies; each sister will get some integer number of candies; Alice will get a greater amount of candies than Betty (i.e. $a > b$); all the candies will be given to one of two sisters (i.e. $a+b=n$). \n\nYour task is to calculate the number of ways to distribute exactly $n$ candies between sisters in a way described above. Candies are indistinguishable.\n\nFormally, find the number of ways to represent $n$ as the sum of $n=a+b$, where $a$ and $b$ are positive integers and $a>b$.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of a test case contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^9$) \u2014 the number of candies you have.\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 the number of ways to distribute exactly $n$ candies between two sisters in a way described in the problem statement. If there is no way to satisfy all the conditions, print $0$.\n\n\n-----Example-----\nInput\n6\n7\n1\n2\n3\n2000000000\n763243547\n\nOutput\n3\n0\n0\n1\n999999999\n381621773\n\n\n\n-----Note-----\n\nFor the test case of the example, the $3$ possible ways to distribute candies are: $a=6$, $b=1$; $a=5$, $b=2$; $a=4$, $b=3$."} +{"id": "03164", "text": "There are $n$ cities and $m$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.\n\nWhat is the minimum number of new roads that need to be built to make all the cities reachable from the capital?\n\nNew roads will also be one-way.\n\n\n-----Input-----\n\nThe first line of input consists of three integers $n$, $m$ and $s$ ($1 \\le n \\le 5000, 0 \\le m \\le 5000, 1 \\le s \\le n$) \u2014 the number of cities, the number of roads and the index of the capital. Cities are indexed from $1$ to $n$.\n\nThe following $m$ lines contain roads: road $i$ is given as a pair of cities $u_i$, $v_i$ ($1 \\le u_i, v_i \\le n$, $u_i \\ne v_i$). For each pair of cities $(u, v)$, there can be at most one road from $u$ to $v$. Roads in opposite directions between a pair of cities are allowed (i.e. from $u$ to $v$ and from $v$ to $u$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of extra roads needed to make all the cities reachable from city $s$. If all the cities are already reachable from $s$, print 0.\n\n\n-----Examples-----\nInput\n9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1\n\nOutput\n3\n\nInput\n5 4 5\n1 2\n2 3\n3 4\n4 1\n\nOutput\n1\n\n\n\n-----Note-----\n\nThe first example is illustrated by the following: [Image] \n\nFor example, you can add roads ($6, 4$), ($7, 9$), ($1, 7$) to make all the cities reachable from $s = 1$.\n\nThe second example is illustrated by the following: [Image] \n\nIn this example, you can add any one of the roads ($5, 1$), ($5, 2$), ($5, 3$), ($5, 4$) to make all the cities reachable from $s = 5$."} +{"id": "03165", "text": "There are $n$ boxers, the weight of the $i$-th boxer is $a_i$. Each of them can change the weight by no more than $1$ before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.\n\nIt is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique).\n\nWrite a program that for given current values \u200b$a_i$ will find the maximum possible number of boxers in a team.\n\nIt is possible that after some change the weight of some boxer is $150001$ (but no more).\n\n\n-----Input-----\n\nThe first line contains an integer $n$ ($1 \\le n \\le 150000$) \u2014 the number of boxers. The next line contains $n$ integers $a_1, a_2, \\dots, a_n$, where $a_i$ ($1 \\le a_i \\le 150000$) is the weight of the $i$-th boxer.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum possible number of people in a team.\n\n\n-----Examples-----\nInput\n4\n3 2 4 1\n\nOutput\n4\n\nInput\n6\n1 1 1 4 4 4\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example, boxers should not change their weights \u2014 you can just make a team out of all of them.\n\nIn the second example, one boxer with a weight of $1$ can be increased by one (get the weight of $2$), one boxer with a weight of $4$ can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of $3$ and $5$, respectively). Thus, you can get a team consisting of boxers with weights of $5, 4, 3, 2, 1$."} +{"id": "03166", "text": "In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.\nCity i is established in year Y_i and belongs to Prefecture P_i.\nYou can assume that there are no multiple cities that are established in the same year.\nIt is decided to allocate a 12-digit ID number to each city.\nIf City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.\nHere, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.\nFind the ID numbers for all the cities.\nNote that there can be a prefecture with no cities.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^5\n - 1 \\leq P_i \\leq N\n - 1 \\leq Y_i \\leq 10^9\n - Y_i are all different.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nP_1 Y_1\n:\nP_M Y_M\n\n-----Output-----\nPrint the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).\n\n-----Sample Input-----\n2 3\n1 32\n2 63\n1 12\n\n-----Sample Output-----\n000001000002\n000002000001\n000001000001\n\n - As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.\n - As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.\n - As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001."} +{"id": "03167", "text": "There are N monsters, numbered 1, 2, ..., N.\nInitially, the health of Monster i is A_i.\nBelow, a monster with at least 1 health is called alive.\nUntil there is only one alive monster, the following is repeated:\n - A random alive monster attacks another random alive monster.\n - As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.\nFind the minimum possible final health of the last monster alive.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 10^5\n - 1 \\leq A_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the minimum possible final health of the last monster alive.\n\n-----Sample Input-----\n4\n2 10 8 40\n\n-----Sample Output-----\n2\n\nWhen only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum."} +{"id": "03168", "text": "Golorps are mysterious creatures who feed on variables. Golorp's name is a program in some programming language. Some scientists believe that this language is Befunge; golorps are tantalizingly silent.\n\nVariables consumed by golorps can take values from 0 to 9, inclusive. For each golorp its daily diet is defined by its name. Some golorps are so picky that they can't be fed at all. Besides, all golorps are very health-conscious and try to eat as little as possible. Given a choice of several valid sequences of variable values, each golorp will choose lexicographically smallest one.\n\nFor the purposes of this problem you can assume that a golorp consists of jaws and a stomach. The number of variables necessary to feed a golorp is defined by the shape of its jaws. Variables can get to the stomach only via the jaws.\n\nA hungry golorp is visiting you. You know its name; feed it or figure out that it's impossible.\n\n\n-----Input-----\n\nThe input is a single string (between 13 and 1024 characters long) \u2014 the name of the visiting golorp. All names are similar and will resemble the ones given in the samples. The name is guaranteed to be valid.\n\n\n-----Output-----\n\nOutput lexicographically smallest sequence of variable values fit for feeding this golorp. Values should be listed in the order in which they get into the jaws. If the golorp is impossible to feed, output \"false\".\n\n\n-----Examples-----\nInput\n?(_-_/___*__):-___>__.\n\nOutput\n0010\n\nInput\n?(__-_+_/_____):-__>__,_____<__.\n\nOutput\nfalse\n\nInput\n?(______________________/____+_______*__-_____*______-___):-__<___,___<____,____<_____,_____<______,______<_______.\n\nOutput\n0250341\n\nInput\n?(__+___+__-___):-___>__.\n\nOutput\n0101"} +{"id": "03169", "text": "There is a rectangular grid of size $n \\times m$. Each cell has a number written on it; the number on the cell ($i, j$) is $a_{i, j}$. Your task is to calculate the number of paths from the upper-left cell ($1, 1$) to the bottom-right cell ($n, m$) meeting the following constraints:\n\n You can move to the right or to the bottom only. Formally, from the cell ($i, j$) you may move to the cell ($i, j + 1$) or to the cell ($i + 1, j$). The target cell can't be outside of the grid. The xor of all the numbers on the path from the cell ($1, 1$) to the cell ($n, m$) must be equal to $k$ (xor operation is the bitwise exclusive OR, it is represented as '^' in Java or C++ and \"xor\" in Pascal). \n\nFind the number of such paths in the given grid.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n$, $m$ and $k$ ($1 \\le n, m \\le 20$, $0 \\le k \\le 10^{18}$) \u2014 the height and the width of the grid, and the number $k$.\n\nThe next $n$ lines contain $m$ integers each, the $j$-th element in the $i$-th line is $a_{i, j}$ ($0 \\le a_{i, j} \\le 10^{18}$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of paths from ($1, 1$) to ($n, m$) with xor sum equal to $k$.\n\n\n-----Examples-----\nInput\n3 3 11\n2 1 5\n7 10 0\n12 6 4\n\nOutput\n3\n\nInput\n3 4 2\n1 3 3 3\n0 3 3 2\n3 0 1 1\n\nOutput\n5\n\nInput\n3 4 1000000000000000000\n1 3 3 3\n0 3 3 2\n3 0 1 1\n\nOutput\n0\n\n\n\n-----Note-----\n\nAll the paths from the first example: $(1, 1) \\rightarrow (2, 1) \\rightarrow (3, 1) \\rightarrow (3, 2) \\rightarrow (3, 3)$; $(1, 1) \\rightarrow (2, 1) \\rightarrow (2, 2) \\rightarrow (2, 3) \\rightarrow (3, 3)$; $(1, 1) \\rightarrow (1, 2) \\rightarrow (2, 2) \\rightarrow (3, 2) \\rightarrow (3, 3)$. \n\nAll the paths from the second example: $(1, 1) \\rightarrow (2, 1) \\rightarrow (3, 1) \\rightarrow (3, 2) \\rightarrow (3, 3) \\rightarrow (3, 4)$; $(1, 1) \\rightarrow (2, 1) \\rightarrow (2, 2) \\rightarrow (3, 2) \\rightarrow (3, 3) \\rightarrow (3, 4)$; $(1, 1) \\rightarrow (2, 1) \\rightarrow (2, 2) \\rightarrow (2, 3) \\rightarrow (2, 4) \\rightarrow (3, 4)$; $(1, 1) \\rightarrow (1, 2) \\rightarrow (2, 2) \\rightarrow (2, 3) \\rightarrow (3, 3) \\rightarrow (3, 4)$; $(1, 1) \\rightarrow (1, 2) \\rightarrow (1, 3) \\rightarrow (2, 3) \\rightarrow (3, 3) \\rightarrow (3, 4)$."} +{"id": "03170", "text": "A string $s$ of length $n$ can be encrypted by the following algorithm: iterate over all divisors of $n$ in decreasing order (i.e. from $n$ to $1$), for each divisor $d$, reverse the substring $s[1 \\dots d]$ (i.e. the substring which starts at position $1$ and ends at position $d$). \n\nFor example, the above algorithm applied to the string $s$=\"codeforces\" leads to the following changes: \"codeforces\" $\\to$ \"secrofedoc\" $\\to$ \"orcesfedoc\" $\\to$ \"rocesfedoc\" $\\to$ \"rocesfedoc\" (obviously, the last reverse operation doesn't change the string because $d=1$).\n\nYou are given the encrypted string $t$. Your task is to decrypt this string, i.e., to find a string $s$ such that the above algorithm results in string $t$. It can be proven that this string $s$ always exists and is unique.\n\n\n-----Input-----\n\nThe first line of input consists of a single integer $n$ ($1 \\le n \\le 100$) \u2014 the length of the string $t$. The second line of input consists of the string $t$. The length of $t$ is $n$, and it consists only of lowercase Latin letters.\n\n\n-----Output-----\n\nPrint a string $s$ such that the above algorithm results in $t$.\n\n\n-----Examples-----\nInput\n10\nrocesfedoc\n\nOutput\ncodeforces\n\nInput\n16\nplmaetwoxesisiht\n\nOutput\nthisisexampletwo\n\nInput\n1\nz\n\nOutput\nz\n\n\n\n-----Note-----\n\nThe first example is described in the problem statement."} +{"id": "03171", "text": "A + B is often used as an example of the easiest problem possible to show some contest platform. However, some scientists have observed that sometimes this problem is not so easy to get accepted. Want to try?\n\n\n-----Input-----\n\nThe input contains two integers a and b (0 \u2264 a, b \u2264 10^3), separated by a single space.\n\n\n-----Output-----\n\nOutput the sum of the given integers.\n\n\n-----Examples-----\nInput\n5 14\n\nOutput\n19\n\nInput\n381 492\n\nOutput\n873"} +{"id": "03172", "text": "In this problem you will write a simple generator of Brainfuck (https://en.wikipedia.org/wiki/Brainfuck) calculators.\n\nYou are given an arithmetic expression consisting of integers from 0 to 255 and addition/subtraction signs between them. Output a Brainfuck program which, when executed, will print the result of evaluating this expression.\n\nWe use a fairly standard Brainfuck interpreter for checking the programs:\n\n\n\n 30000 memory cells.\n\n memory cells store integers from 0 to 255 with unsigned 8-bit wraparound.\n\n console input (, command) is not supported, but it's not needed for this problem.\n\n\n-----Input-----\n\nThe only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive. The calculations result is guaranteed to be an integer between 0 and 255, inclusive (results of intermediary calculations might be outside of these boundaries).\n\n\n-----Output-----\n\nOutput a Brainfuck program which, when executed, will print the result of evaluating this expression. The program must be at most 5000000 characters long (including the non-command characters), and its execution must be complete in at most 50000000 steps.\n\n\n-----Examples-----\nInput\n2+3\n\nOutput\n++>\n+++>\n<[<+>-]<\n++++++++++++++++++++++++++++++++++++++++++++++++.\n\nInput\n9-7\n\nOutput\n+++++++++>\n+++++++>\n<[<->-]<\n++++++++++++++++++++++++++++++++++++++++++++++++.\n\n\n\n-----Note-----\n\nYou can download the source code of the Brainfuck interpreter by the link http://assets.codeforces.com/rounds/784/bf.cpp. We use this code to interpret outputs."} +{"id": "03173", "text": "The only difference between the easy and the hard versions is the maximum value of $k$.\n\nYou are given an infinite sequence of form \"112123123412345$\\dots$\" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one \u2014 from $1$ to $2$, the third one \u2014 from $1$ to $3$, $\\dots$, the $i$-th block consists of all numbers from $1$ to $i$. \n\nSo the first $56$ elements of the sequence are \"11212312341234512345612345671234567812345678912345678910\". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$.\n\nYour task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 500$) \u2014 the number of queries.\n\nThe $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \\le k_i \\le 10^{18})$ \u2014 the description of the corresponding query.\n\n\n-----Output-----\n\nPrint $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \\le x_i \\le 9)$ \u2014 the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence.\n\n\n-----Examples-----\nInput\n5\n1\n3\n20\n38\n56\n\nOutput\n1\n2\n5\n2\n0\n\nInput\n4\n2132\n506\n999999999999999999\n1000000000000000000\n\nOutput\n8\n2\n4\n1\n\n\n\n-----Note-----\n\nAnswers on queries from the first example are described in the problem statement."} +{"id": "03174", "text": "You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally \"Seven-Five-Three numbers\") are there?\nHere, a Shichi-Go-San number is a positive integer that satisfies the following condition:\n - When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.\n\n-----Constraints-----\n - 1 \\leq N < 10^9\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the number of the Shichi-Go-San numbers between 1 and N (inclusive).\n\n-----Sample Input-----\n575\n\n-----Sample Output-----\n4\n\nThere are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573."} +{"id": "03175", "text": "N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.\nThe i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.\nYou want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.\nAt least how many tiles need to be repainted to satisfy the condition?\n\n-----Constraints-----\n - 1 \\leq |S| \\leq 10^5\n - S_i is 0 or 1.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the minimum number of tiles that need to be repainted to satisfy the condition.\n\n-----Sample Input-----\n000\n\n-----Sample Output-----\n1\n\nThe condition can be satisfied by repainting the middle tile white."} +{"id": "03176", "text": "You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.\nAccording to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:\n - All even numbers written on the document are divisible by 3 or 5.\nIf the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.\n\n-----Notes-----\n - The condition in the statement can be rephrased as \"If x is an even number written on the document, x is divisible by 3 or 5\".\nHere \"if\" and \"or\" are logical terms.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 100\n - 1 \\leq A_i \\leq 1000\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 \\dots A_N\n\n-----Output-----\nIf the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.\n\n-----Sample Input-----\n5\n6 7 9 10 31\n\n-----Sample Output-----\nAPPROVED\n\nThe even numbers written on the document are 6 and 10.\nAll of them are divisible by 3 or 5, so the immigrant should be allowed entry."} +{"id": "03177", "text": "Takahashi will do a tap dance. The dance is described by a string S where each character is L, R, U, or D. These characters indicate the positions on which Takahashi should step. He will follow these instructions one by one in order, starting with the first character.\nS is said to be easily playable if and only if it satisfies both of the following conditions:\n - Every character in an odd position (1-st, 3-rd, 5-th, \\ldots) is R, U, or D.\n - Every character in an even position (2-nd, 4-th, 6-th, \\ldots) is L, U, or D.\nYour task is to print Yes if S is easily playable, and No otherwise.\n\n-----Constraints-----\n - S is a string of length between 1 and 100 (inclusive).\n - Each character of S is L, R, U, or D.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint Yes if S is easily playable, and No otherwise.\n\n-----Sample Input-----\nRUDLUDR\n\n-----Sample Output-----\nYes\n\nEvery character in an odd position (1-st, 3-rd, 5-th, 7-th) is R, U, or D.\nEvery character in an even position (2-nd, 4-th, 6-th) is L, U, or D.\nThus, S is easily playable."} +{"id": "03178", "text": "In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!\nThere are five means of transport in this empire:\n - Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.\n - Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.\n - Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.\n - Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.\n - Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.\nFor each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).\nThere is a group of N people at City 1, and they all want to go to City 6.\n\nAt least how long does it take for all of them to reach there? \nYou can ignore the time needed to transfer. \n\n-----Constraints-----\n - 1 \\leq N, A, B, C, D, E \\leq 10^{15}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA\nB\nC\nD\nE\n\n-----Output-----\nPrint the minimum time required for all of the people to reach City 6, in minutes.\n\n-----Sample Input-----\n5\n3\n2\n4\n3\n5\n\n-----Sample Output-----\n7\n\nOne possible way to travel is as follows.\nFirst, there are N = 5 people at City 1, as shown in the following image:\n\nIn the first minute, three people travels from City 1 to City 2 by train. Note that a train can only occupy at most three people.\n\nIn the second minute, the remaining two people travels from City 1 to City 2 by train, and two of the three people who were already at City 2 travels to City 3 by bus. Note that a bus can only occupy at most two people.\n\nIn the third minute, two people travels from City 2 to City 3 by train, and another two people travels from City 3 to City 4 by taxi.\n\nFrom then on, if they continue traveling without stopping until they reach City 6, all of them can reach there in seven minutes.\n\nThere is no way for them to reach City 6 in 6 minutes or less."} +{"id": "03179", "text": "How many integer sequences A_1,A_2,\\ldots,A_N of length N satisfy all of the following conditions?\n - 0 \\leq A_i \\leq 9\n - There exists some i such that A_i=0 holds.\n - There exists some i such that A_i=9 holds.\nThe answer can be very large, so output it modulo 10^9 + 7.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^6\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the answer modulo 10^9 + 7.\n\n-----Sample Input-----\n2\n\n-----Sample Output-----\n2\n\nTwo sequences \\{0,9\\} and \\{9,0\\} satisfy all conditions."} +{"id": "03180", "text": "Find the minimum prime number greater than or equal to X.\n\n-----Notes-----\nA prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.\nFor example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.\n\n-----Constraints-----\n - 2 \\le X \\le 10^5 \n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX\n\n-----Output-----\nPrint the minimum prime number greater than or equal to X.\n\n-----Sample Input-----\n20\n\n-----Sample Output-----\n23\n\nThe minimum prime number greater than or equal to 20 is 23."} +{"id": "03181", "text": "A sequence a_1,a_2,... ,a_n is said to be /\\/\\/\\/ when the following conditions are satisfied:\n - For each i = 1,2,..., n-2, a_i = a_{i+2}.\n - Exactly two different numbers appear in the sequence.\nYou are given a sequence v_1,v_2,...,v_n whose length is even.\nWe would like to make this sequence /\\/\\/\\/ by replacing some of its elements.\nFind the minimum number of elements that needs to be replaced.\n\n-----Constraints-----\n - 2 \\leq n \\leq 10^5\n - n is even.\n - 1 \\leq v_i \\leq 10^5\n - v_i is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn\nv_1 v_2 ... v_n\n\n-----Output-----\nPrint the minimum number of elements that needs to be replaced.\n\n-----Sample Input-----\n4\n3 1 3 2\n\n-----Sample Output-----\n1\n\nThe sequence 3,1,3,2 is not /\\/\\/\\/, but we can make it /\\/\\/\\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1."} +{"id": "03182", "text": "You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.\nYour objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:\n - Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.\n - Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.\n - Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)\nAt least how much MP is needed to achieve the objective?\n\n-----Constraints-----\n - 3 \\leq N \\leq 8\n - 1 \\leq C < B < A \\leq 1000\n - 1 \\leq l_i \\leq 1000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN A B C\nl_1\nl_2\n:\nl_N\n\n-----Output-----\nPrint the minimum amount of MP needed to achieve the objective.\n\n-----Sample Input-----\n5 100 90 80\n98\n40\n30\n21\n80\n\n-----Sample Output-----\n23\n\nWe are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.\n - Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)\n - Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)\n - Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)\n - Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)"} +{"id": "03183", "text": "We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.\nShift each character of S by N in alphabetical order (see below), and print the resulting string.\nWe assume that A follows Z. For example, shifting A by 2 results in C (A \\to B \\to C), and shifting Y by 3 results in B (Y \\to Z \\to A \\to B).\n\n-----Constraints-----\n - 0 \\leq N \\leq 26\n - 1 \\leq |S| \\leq 10^4\n - S consists of uppercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS\n\n-----Output-----\nPrint the string resulting from shifting each character of S by N in alphabetical order.\n\n-----Sample Input-----\n2\nABCXYZ\n\n-----Sample Output-----\nCDEZAB\n\nNote that A follows Z."} +{"id": "03184", "text": "Authors guessed an array $a$ consisting of $n$ integers; each integer is not less than $2$ and not greater than $2 \\cdot 10^5$. You don't know the array $a$, but you know the array $b$ which is formed from it with the following sequence of operations: Firstly, let the array $b$ be equal to the array $a$; Secondly, for each $i$ from $1$ to $n$: if $a_i$ is a prime number, then one integer $p_{a_i}$ is appended to array $b$, where $p$ is an infinite sequence of prime numbers ($2, 3, 5, \\dots$); otherwise (if $a_i$ is not a prime number), the greatest divisor of $a_i$ which is not equal to $a_i$ is appended to $b$; Then the obtained array of length $2n$ is shuffled and given to you in the input. \n\nHere $p_{a_i}$ means the $a_i$-th prime number. The first prime $p_1 = 2$, the second one is $p_2 = 3$, and so on.\n\nYour task is to recover any suitable array $a$ that forms the given array $b$. It is guaranteed that the answer exists (so the array $b$ is obtained from some suitable array $a$). If there are multiple answers, you can print any.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $2n$ integers $b_1, b_2, \\dots, b_{2n}$ ($2 \\le b_i \\le 2750131$), where $b_i$ is the $i$-th element of $b$. $2750131$ is the $199999$-th prime number.\n\n\n-----Output-----\n\nIn the only line of the output print $n$ integers $a_1, a_2, \\dots, a_n$ ($2 \\le a_i \\le 2 \\cdot 10^5$) in any order \u2014 the array $a$ from which the array $b$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any.\n\n\n-----Examples-----\nInput\n3\n3 5 2 3 2 4\n\nOutput\n3 4 2 \nInput\n1\n2750131 199999\n\nOutput\n199999 \nInput\n1\n3 6\n\nOutput\n6"} +{"id": "03185", "text": "There are $n$ students standing in a row. Two coaches are forming two teams \u2014 the first coach chooses the first team and the second coach chooses the second team.\n\nThe $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.\n\nFirstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).\n\nYour problem is to determine which students will be taken into the first team and which students will be taken into the second team.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$) \u2014 the number of students and the value determining the range of chosen students during each move, respectively.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.\n\n\n-----Output-----\n\nPrint a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.\n\n\n-----Examples-----\nInput\n5 2\n2 4 5 3 1\n\nOutput\n11111\n\nInput\n5 1\n2 1 3 5 4\n\nOutput\n22111\n\nInput\n7 1\n7 2 1 3 5 4 6\n\nOutput\n1121122\n\nInput\n5 1\n2 4 5 3 1\n\nOutput\n21112\n\n\n\n-----Note-----\n\nIn the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).\n\nIn the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).\n\nIn the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).\n\nIn the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team)."} +{"id": "03186", "text": "You are given an array $a$ consisting of $n$ integers. Let's denote monotonic renumeration of array $a$ as an array $b$ consisting of $n$ integers such that all of the following conditions are met:\n\n $b_1 = 0$; for every pair of indices $i$ and $j$ such that $1 \\le i, j \\le n$, if $a_i = a_j$, then $b_i = b_j$ (note that if $a_i \\ne a_j$, it is still possible that $b_i = b_j$); for every index $i \\in [1, n - 1]$ either $b_i = b_{i + 1}$ or $b_i + 1 = b_{i + 1}$. \n\nFor example, if $a = [1, 2, 1, 2, 3]$, then two possible monotonic renumerations of $a$ are $b = [0, 0, 0, 0, 0]$ and $b = [0, 0, 0, 0, 1]$.\n\nYour task is to calculate the number of different monotonic renumerations of $a$. The answer may be large, so print it modulo $998244353$.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of different monotonic renumerations of $a$, taken modulo $998244353$.\n\n\n-----Examples-----\nInput\n5\n1 2 1 2 3\n\nOutput\n2\n\nInput\n2\n100 1\n\nOutput\n2\n\nInput\n4\n1 3 3 7\n\nOutput\n4"} +{"id": "03187", "text": "A sequence $a_1, a_2, \\dots, a_n$ is called good if, for each element $a_i$, there exists an element $a_j$ ($i \\ne j$) such that $a_i+a_j$ is a power of two (that is, $2^d$ for some non-negative integer $d$).\n\nFor example, the following sequences are good: $[5, 3, 11]$ (for example, for $a_1=5$ we can choose $a_2=3$. Note that their sum is a power of two. Similarly, such an element can be found for $a_2$ and $a_3$), $[1, 1, 1, 1023]$, $[7, 39, 89, 25, 89]$, $[]$. \n\nNote that, by definition, an empty sequence (with a length of $0$) is good.\n\nFor example, the following sequences are not good: $[16]$ (for $a_1=16$, it is impossible to find another element $a_j$ such that their sum is a power of two), $[4, 16]$ (for $a_1=4$, it is impossible to find another element $a_j$ such that their sum is a power of two), $[1, 3, 2, 8, 8, 8]$ (for $a_3=2$, it is impossible to find another element $a_j$ such that their sum is a power of two). \n\nYou are given a sequence $a_1, a_2, \\dots, a_n$. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.\n\n\n-----Input-----\n\nThe first line contains the integer $n$ ($1 \\le n \\le 120000$) \u2014 the length of the given sequence.\n\nThe second line contains the sequence of integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all $n$ elements, make it empty, and thus get a good sequence.\n\n\n-----Examples-----\nInput\n6\n4 7 1 5 4 9\n\nOutput\n1\n\nInput\n5\n1 2 3 4 5\n\nOutput\n2\n\nInput\n1\n16\n\nOutput\n1\n\nInput\n4\n1 1 1 1023\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, it is enough to delete one element $a_4=5$. The remaining elements form the sequence $[4, 7, 1, 4, 9]$, which is good."} +{"id": "03188", "text": "There are N cubes stacked vertically on a desk.\nYou are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.\nYou can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.\nAt most how many cubes can be removed?\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - |S| = N\n - Each character in S is 0 or 1.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the maximum number of cubes that can be removed.\n\n-----Sample Input-----\n0011\n\n-----Sample Output-----\n4\n\nAll four cubes can be removed, by performing the operation as follows:\n - Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.\n - Remove the first and second cubes from the bottom."} +{"id": "03189", "text": "We have N ID cards, and there are M gates.\nWe can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.\nHow many of the ID cards allow us to pass all the gates alone?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq M \\leq 10^5\n - 1 \\leq L_i \\leq R_i \\leq N\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nL_1 R_1\nL_2 R_2\n\\vdots\nL_M R_M\n\n-----Output-----\nPrint the number of ID cards that allow us to pass all the gates alone.\n\n-----Sample Input-----\n4 2\n1 3\n2 4\n\n-----Sample Output-----\n2\n\nTwo ID cards allow us to pass all the gates alone, as follows:\n - The first ID card does not allow us to pass the second gate.\n - The second ID card allows us to pass all the gates.\n - The third ID card allows us to pass all the gates.\n - The fourth ID card does not allow us to pass the first gate."} +{"id": "03190", "text": "In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.\nYou are given a sequence h=\\{h_1,h_2,h_3,......\\} as input. You would like to change the height of Flower k to h_k for all k (1 \\leq k \\leq N), by repeating the following \"watering\" operation:\n - Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \\leq x \\leq r.\nFind the minimum number of watering operations required to satisfy the condition.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 0 \\leq h_i \\leq 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nh_1 h_2 h_3 ...... h_N\n\n-----Output-----\nPrint the minimum number of watering operations required to satisfy the condition.\n\n-----Sample Input-----\n4\n1 2 2 1\n\n-----Sample Output-----\n2\n\nThe minimum number of watering operations required is 2.\nOne way to achieve it is:\n - Perform the operation with (l,r)=(1,3).\n - Perform the operation with (l,r)=(2,4)."} +{"id": "03191", "text": "The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.\n\nIf $x$ is the number of passengers in a bus just before the current bus stop and $y$ is the number of passengers in the bus just after current bus stop, the system records the number $y-x$. So the system records show how number of passengers changed.\n\nThe test run was made for single bus and $n$ bus stops. Thus, the system recorded the sequence of integers $a_1, a_2, \\dots, a_n$ (exactly one number for each bus stop), where $a_i$ is the record for the bus stop $i$. The bus stops are numbered from $1$ to $n$ in chronological order.\n\nDetermine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$ (that is, at any time in the bus there should be from $0$ to $w$ passengers inclusive).\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $w$ $(1 \\le n \\le 1\\,000, 1 \\le w \\le 10^{9})$ \u2014 the number of bus stops and the capacity of the bus.\n\nThe second line contains a sequence $a_1, a_2, \\dots, a_n$ $(-10^{6} \\le a_i \\le 10^{6})$, where $a_i$ equals to the number, which has been recorded by the video system after the $i$-th bus stop.\n\n\n-----Output-----\n\nPrint the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.\n\n\n-----Examples-----\nInput\n3 5\n2 1 -3\n\nOutput\n3\n\nInput\n2 4\n-1 1\n\nOutput\n4\n\nInput\n4 10\n2 4 1 2\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example initially in the bus could be $0$, $1$ or $2$ passengers.\n\nIn the second example initially in the bus could be $1$, $2$, $3$ or $4$ passengers.\n\nIn the third example initially in the bus could be $0$ or $1$ passenger."} +{"id": "03192", "text": "Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$. \n\nAfter each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all.\n\nYou are given a sequence of length $n$ \u2014 the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.\n\nYour problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.\n\nIt is guaranteed that the answer exists.\n\n\n-----Input-----\n\nThe first line of the input contatins an integer number $n$ ($2 \\le n \\le 100$) \u2014 the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 3 \\cdot 10^{18}$) \u2014 rearranged (reordered) sequence that Polycarp can wrote down on the board.\n\n\n-----Output-----\n\nPrint $n$ integer numbers \u2014 rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.\n\nIt is guaranteed that the answer exists.\n\n\n-----Examples-----\nInput\n6\n4 8 6 3 12 9\n\nOutput\n9 3 6 12 4 8 \n\nInput\n4\n42 28 84 126\n\nOutput\n126 42 84 28 \n\nInput\n2\n1000000000000000000 3000000000000000000\n\nOutput\n3000000000000000000 1000000000000000000 \n\n\n\n-----Note-----\n\nIn the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$."} +{"id": "03193", "text": "There are $n$ distinct points on a coordinate line, the coordinate of $i$-th point equals to $x_i$. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size.\n\nIn other words, you have to choose the maximum possible number of points $x_{i_1}, x_{i_2}, \\dots, x_{i_m}$ such that for each pair $x_{i_j}$, $x_{i_k}$ it is true that $|x_{i_j} - x_{i_k}| = 2^d$ where $d$ is some non-negative integer number (not necessarily the same for each pair of points).\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of points.\n\nThe second line contains $n$ pairwise distinct integers $x_1, x_2, \\dots, x_n$ ($-10^9 \\le x_i \\le 10^9$) \u2014 the coordinates of points.\n\n\n-----Output-----\n\nIn the first line print $m$ \u2014 the maximum possible number of points in a subset that satisfies the conditions described above.\n\nIn the second line print $m$ integers \u2014 the coordinates of points in the subset you have chosen.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n6\n3 5 4 7 10 12\n\nOutput\n3\n7 3 5\nInput\n5\n-1 2 5 8 11\n\nOutput\n1\n8\n\n\n\n-----Note-----\n\nIn the first example the answer is $[7, 3, 5]$. Note, that $|7-3|=4=2^2$, $|7-5|=2=2^1$ and $|3-5|=2=2^1$. You can't find a subset having more points satisfying the required property."} +{"id": "03194", "text": "Takahashi has A cookies, and Aoki has B cookies.\nTakahashi will do the following action K times:\n - If Takahashi has one or more cookies, eat one of his cookies.\n - Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.\n - If they both have no cookies, do nothing.\nIn the end, how many cookies will Takahashi and Aoki have, respectively?\n\n-----Constraints-----\n - 0 \\leq A \\leq 10^{12}\n - 0 \\leq B \\leq 10^{12}\n - 0 \\leq K \\leq 10^{12}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B K\n\n-----Output-----\nPrint the numbers of Takahashi's and Aoki's cookies after K actions.\n\n-----Sample Input-----\n2 3 3\n\n-----Sample Output-----\n0 2\n\nTakahashi will do the following:\n - He has two cookies, so he eats one of them.\n - Now he has one cookie left, and he eats it.\n - Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.\nThus, in the end, Takahashi will have 0 cookies, and Aoki will have 2."} +{"id": "03195", "text": "Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.\nThe bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)\nAssuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?\n\n-----Constraints-----\n - 101 \\le X \\le 10^{18} \n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX\n\n-----Output-----\nPrint the number of years it takes for Takahashi's balance to reach X yen or above for the first time.\n\n-----Sample Input-----\n103\n\n-----Sample Output-----\n3\n\n - The balance after one year is 101 yen.\n - The balance after two years is 102 yen.\n - The balance after three years is 103 yen.\nThus, it takes three years for the balance to reach 103 yen or above."} +{"id": "03196", "text": "Find \\displaystyle{\\sum_{a=1}^{K}\\sum_{b=1}^{K}\\sum_{c=1}^{K} \\gcd(a,b,c)}.\nHere \\gcd(a,b,c) denotes the greatest common divisor of a, b, and c.\n\n-----Constraints-----\n - 1 \\leq K \\leq 200\n - K is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK\n\n-----Output-----\nPrint the value of \\displaystyle{\\sum_{a=1}^{K}\\sum_{b=1}^{K}\\sum_{c=1}^{K} \\gcd(a,b,c)}.\n\n-----Sample Input-----\n2\n\n-----Sample Output-----\n9\n\n\\gcd(1,1,1)+\\gcd(1,1,2)+\\gcd(1,2,1)+\\gcd(1,2,2)+\\gcd(2,1,1)+\\gcd(2,1,2)+\\gcd(2,2,1)+\\gcd(2,2,2)=1+1+1+1+1+1+1+2=9\nThus, the answer is 9."} +{"id": "03197", "text": "You are given N positive integers a_1, a_2, ..., a_N.\nFor a non-negative integer m, let f(m) = (m\\ mod\\ a_1) + (m\\ mod\\ a_2) + ... + (m\\ mod\\ a_N).\nHere, X\\ mod\\ Y denotes the remainder of the division of X by Y.\nFind the maximum value of f.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 3000\n - 2 \\leq a_i \\leq 10^5\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the maximum value of f.\n\n-----Sample Input-----\n3\n3 4 6\n\n-----Sample Output-----\n10\n\nf(11) = (11\\ mod\\ 3) + (11\\ mod\\ 4) + (11\\ mod\\ 6) = 10 is the maximum value of f."} +{"id": "03198", "text": "Tak performed the following action N times: rolling two dice.\nThe result of the i-th roll is D_{i,1} and D_{i,2}.\nCheck if doublets occurred at least three times in a row.\nSpecifically, check if there exists at lease one i such that D_{i,1}=D_{i,2}, D_{i+1,1}=D_{i+1,2} and D_{i+2,1}=D_{i+2,2} hold.\n\n-----Constraints-----\n - 3 \\leq N \\leq 100\n - 1\\leq D_{i,j} \\leq 6\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nD_{1,1} D_{1,2}\n\\vdots\nD_{N,1} D_{N,2}\n\n-----Output-----\nPrint Yes if doublets occurred at least three times in a row. Print No otherwise.\n\n-----Sample Input-----\n5\n1 2\n6 6\n4 4\n3 3\n3 2\n\n-----Sample Output-----\nYes\n\nFrom the second roll to the fourth roll, three doublets occurred in a row."} +{"id": "03199", "text": "Given is an integer r.\nHow many times is the area of a circle of radius r larger than the area of a circle of radius 1?\nIt can be proved that the answer is always an integer under the constraints given.\n\n-----Constraints-----\n - 1 \\leq r \\leq 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nr\n\n-----Output-----\nPrint the area of a circle of radius r, divided by the area of a circle of radius 1, as an integer.\n\n-----Sample Input-----\n2\n\n-----Sample Output-----\n4\n\nThe area of a circle of radius 2 is 4 times larger than the area of a circle of radius 1.\nNote that output must be an integer - for example, 4.0 will not be accepted."} +{"id": "03200", "text": "Determine if an N-sided polygon (not necessarily convex) with sides of length L_1, L_2, ..., L_N can be drawn in a two-dimensional plane.\nYou can use the following theorem:\nTheorem: an N-sided polygon satisfying the condition can be drawn if and only if the longest side is strictly shorter than the sum of the lengths of the other N-1 sides.\n\n-----Constraints-----\n - All values in input are integers.\n - 3 \\leq N \\leq 10\n - 1 \\leq L_i \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nL_1 L_2 ... L_N\n\n-----Output-----\nIf an N-sided polygon satisfying the condition can be drawn, print Yes; otherwise, print No.\n\n-----Sample Input-----\n4\n3 8 5 1\n\n-----Sample Output-----\nYes\n\nSince 8 < 9 = 3 + 5 + 1, it follows from the theorem that such a polygon can be drawn on a plane."} +{"id": "03201", "text": "If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.\n - The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)\n - The s_i-th digit from the left is c_i. \\left(i = 1, 2, \\cdots, M\\right)\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 3\n - 0 \\leq M \\leq 5\n - 1 \\leq s_i \\leq N\n - 0 \\leq c_i \\leq 9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\ns_1 c_1\n\\vdots\ns_M c_M\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n3 3\n1 7\n3 2\n1 7\n\n-----Sample Output-----\n702\n\n702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them."} +{"id": "03202", "text": "You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.\nThe order of a,b,c does matter, and some of them can be the same.\n\n-----Constraints-----\n - 1 \\leq N,K \\leq 2\\times 10^5\n - N and K are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\n\n-----Output-----\nPrint the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.\n\n-----Sample Input-----\n3 2\n\n-----Sample Output-----\n9\n\n(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1) and (3,3,3) satisfy the condition."} +{"id": "03203", "text": "Given an integer N, find the base -2 representation of N.\nHere, S is the base -2 representation of N when the following are all satisfied:\n - S is a string consisting of 0 and 1.\n - Unless S = 0, the initial character of S is 1.\n - Let S = S_k S_{k-1} ... S_0, then S_0 \\times (-2)^0 + S_1 \\times (-2)^1 + ... + S_k \\times (-2)^k = N.\nIt can be proved that, for any integer M, the base -2 representation of M is uniquely determined.\n\n-----Constraints-----\n - Every value in input is integer.\n - -10^9 \\leq N \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the base -2 representation of N.\n\n-----Sample Input-----\n-9\n\n-----Sample Output-----\n1011\n\nAs (-2)^0 + (-2)^1 + (-2)^3 = 1 + (-2) + (-8) = -9, 1011 is the base -2 representation of -9."} +{"id": "03204", "text": "Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.\nThere are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.\nWhat is the minimum amount of money with which he can buy M cans of energy drinks?\nIt is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N, M \\leq 10^5\n - 1 \\leq A_i \\leq 10^9\n - 1 \\leq B_i \\leq 10^5\n - B_1 + ... + B_N \\geq M\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 B_1\nA_2 B_2\n\\vdots\nA_N B_N\n\n-----Output-----\nPrint the minimum amount of money with which Takahashi can buy M cans of energy drinks.\n\n-----Sample Input-----\n2 5\n4 9\n2 4\n\n-----Sample Output-----\n12\n\nWith 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less."} +{"id": "03205", "text": "There are N squares arranged in a row from left to right.\nThe height of the i-th square from the left is H_i.\nYou will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.\nFind the maximum number of times you can move.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq H_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nH_1 H_2 ... H_N\n\n-----Output-----\nPrint the maximum number of times you can move.\n\n-----Sample Input-----\n5\n10 4 8 7 3\n\n-----Sample Output-----\n2\n\nBy landing on the third square from the left, you can move to the right twice."} +{"id": "03206", "text": "The only difference between easy and hard versions is the number of elements in the array.\n\nYou are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \\lfloor\\frac{a_i}{2}\\rfloor$).\n\nYou can perform such an operation any (possibly, zero) number of times with any $a_i$.\n\nYour task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array.\n\nDon't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in the array and the number of equal numbers required.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible number of operations required to obtain at least $k$ equal numbers in the array.\n\n\n-----Examples-----\nInput\n5 3\n1 2 2 4 5\n\nOutput\n1\n\nInput\n5 3\n1 2 3 4 5\n\nOutput\n2\n\nInput\n5 3\n1 2 3 3 3\n\nOutput\n0"} +{"id": "03207", "text": "You are given the array $a$ consisting of $n$ elements and the integer $k \\le n$.\n\nYou want to obtain at least $k$ equal elements in the array $a$. In one move, you can make one of the following two operations:\n\n Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of $a$ is $mn$ then you choose such index $i$ that $a_i = mn$ and set $a_i := a_i + 1$); take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of $a$ is $mx$ then you choose such index $i$ that $a_i = mx$ and set $a_i := a_i - 1$). \n\nYour task is to calculate the minimum number of moves required to obtain at least $k$ equal elements in the array.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$ and the required number of equal elements.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of moves required to obtain at least $k$ equal elements in the array.\n\n\n-----Examples-----\nInput\n6 5\n1 2 2 4 2 3\n\nOutput\n3\n\nInput\n7 5\n3 3 2 1 1 1 3\n\nOutput\n4"} +{"id": "03208", "text": "Polycarp wants to cook a soup. To do it, he needs to buy exactly $n$ liters of water.\n\nThere are only two types of water bottles in the nearby shop \u2014 $1$-liter bottles and $2$-liter bottles. There are infinitely many bottles of these two types in the shop.\n\nThe bottle of the first type costs $a$ burles and the bottle of the second type costs $b$ burles correspondingly.\n\nPolycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $n$ liters of water in the nearby shop if the bottle of the first type costs $a$ burles and the bottle of the second type costs $b$ burles. \n\nYou also have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 500$) \u2014 the number of queries.\n\nThe next $n$ lines contain queries. The $i$-th query is given as three space-separated integers $n_i$, $a_i$ and $b_i$ ($1 \\le n_i \\le 10^{12}, 1 \\le a_i, b_i \\le 1000$) \u2014 how many liters Polycarp needs in the $i$-th query, the cost (in burles) of the bottle of the first type in the $i$-th query and the cost (in burles) of the bottle of the second type in the $i$-th query, respectively.\n\n\n-----Output-----\n\nPrint $q$ integers. The $i$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $n_i$ liters of water in the nearby shop if the bottle of the first type costs $a_i$ burles and the bottle of the second type costs $b_i$ burles.\n\n\n-----Example-----\nInput\n4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88\n\nOutput\n10\n9\n1000\n42000000000000"} +{"id": "03209", "text": "A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \\leq i \\leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.\nHow many times will the ball make a bounce where the coordinate is at most X?\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 1 \\leq L_i \\leq 100\n - 1 \\leq X \\leq 10000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN X\nL_1 L_2 ... L_{N-1} L_N\n\n-----Output-----\nPrint the number of times the ball will make a bounce where the coordinate is at most X.\n\n-----Sample Input-----\n3 6\n3 4 5\n\n-----Sample Output-----\n2\n\nThe ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6."} +{"id": "03210", "text": "Takahashi is practicing shiritori alone again today.\nShiritori is a game as follows:\n - In the first turn, a player announces any one word.\n - In the subsequent turns, a player announces a word that satisfies the following conditions:\n - That word is not announced before.\n - The first character of that word is the same as the last character of the last word announced.\nIn this game, he is practicing to announce as many words as possible in ten seconds.\nYou are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.\n\n-----Constraints-----\n - N is an integer satisfying 2 \\leq N \\leq 100.\n - W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nW_1\nW_2\n:\nW_N\n\n-----Output-----\nIf every word announced by Takahashi satisfied the conditions, print Yes; otherwise, print No.\n\n-----Sample Input-----\n4\nhoge\nenglish\nhoge\nenigma\n\n-----Sample Output-----\nNo\n\nAs hoge is announced multiple times, the rules of shiritori was not observed."} +{"id": "03211", "text": "Takahashi is organizing a party.\nAt the party, each guest will receive one or more snack pieces.\nTakahashi predicts that the number of guests at this party will be A or B.\nFind the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.\nWe assume that a piece cannot be divided and distributed to multiple guests.\n\n-----Constraints-----\n - 1 \\leq A, B \\leq 10^5\n - A \\neq B\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.\n\n-----Sample Input-----\n2 3\n\n-----Sample Output-----\n6\n\nWhen we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests."} +{"id": "03212", "text": "Given is a string S. Replace every character in S with x and print the result.\n\n-----Constraints-----\n - S is a string consisting of lowercase English letters.\n - The length of S is between 1 and 100 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nReplace every character in S with x and print the result.\n\n-----Sample Input-----\nsardine\n\n-----Sample Output-----\nxxxxxxx\n\nReplacing every character in S with x results in xxxxxxx."} +{"id": "03213", "text": "There are N squares arranged in a row from left to right. The height of the i-th square from the left is H_i.\nFor each square, you will perform either of the following operations once:\n - Decrease the height of the square by 1.\n - Do nothing.\nDetermine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq H_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nH_1 H_2 ... H_N\n\n-----Output-----\nIf it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right, print Yes; otherwise, print No.\n\n-----Sample Input-----\n5\n1 2 1 1 3\n\n-----Sample Output-----\nYes\n\nYou can achieve the objective by decreasing the height of only the second square from the left by 1."} +{"id": "03214", "text": "There are N pieces of source code. The characteristics of the i-th code is represented by M integers A_{i1}, A_{i2}, ..., A_{iM}.\nAdditionally, you are given integers B_1, B_2, ..., B_M and C.\nThe i-th code correctly solves this problem if and only if A_{i1} B_1 + A_{i2} B_2 + ... + A_{iM} B_M + C > 0.\nAmong the N codes, find the number of codes that correctly solve this problem.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N, M \\leq 20\n - -100 \\leq A_{ij} \\leq 100\n - -100 \\leq B_i \\leq 100\n - -100 \\leq C \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M C\nB_1 B_2 ... B_M\nA_{11} A_{12} ... A_{1M}\nA_{21} A_{22} ... A_{2M}\n\\vdots\nA_{N1} A_{N2} ... A_{NM}\n\n-----Output-----\nPrint the number of codes among the given N codes that correctly solve this problem.\n\n-----Sample Input-----\n2 3 -10\n1 2 3\n3 2 1\n1 2 2\n\n-----Sample Output-----\n1\n\nOnly the second code correctly solves this problem, as follows:\n - Since 3 \\times 1 + 2 \\times 2 + 1 \\times 3 + (-10) = 0 \\leq 0, the first code does not solve this problem.\n - 1 \\times 1 + 2 \\times 2 + 2 \\times 3 + (-10) = 1 > 0, the second code solves this problem."} +{"id": "03215", "text": "We will buy a product for N yen (the currency of Japan) at a shop.\nIf we use only 1000-yen bills to pay the price, how much change will we receive?\nAssume we use the minimum number of bills required.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10000\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the amount of change as an integer.\n\n-----Sample Input-----\n1900\n\n-----Sample Output-----\n100\n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change."} +{"id": "03216", "text": "There are N+1 towns. The i-th town is being attacked by A_i monsters.\nWe have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.\nWhat is the maximum total number of monsters the heroes can cooperate to defeat?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^5\n - 1 \\leq A_i \\leq 10^9\n - 1 \\leq B_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_{N+1}\nB_1 B_2 ... B_N\n\n-----Output-----\nPrint the maximum total number of monsters the heroes can defeat.\n\n-----Sample Input-----\n2\n3 5 2\n4 5\n\n-----Sample Output-----\n9\n\nIf the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.\n - The first hero defeats two monsters attacking the first town and two monsters attacking the second town.\n - The second hero defeats three monsters attacking the second town and two monsters attacking the third town."} +{"id": "03217", "text": "Our world is one-dimensional, and ruled by two empires called Empire A and Empire B.\nThe capital of Empire A is located at coordinate X, and that of Empire B is located at coordinate Y.\nOne day, Empire A becomes inclined to put the cities at coordinates x_1, x_2, ..., x_N under its control, and Empire B becomes inclined to put the cities at coordinates y_1, y_2, ..., y_M under its control.\nIf there exists an integer Z that satisfies all of the following three conditions, they will come to an agreement, but otherwise war will break out.\n - X < Z \\leq Y\n - x_1, x_2, ..., x_N < Z\n - y_1, y_2, ..., y_M \\geq Z\nDetermine if war will break out.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N, M \\leq 100\n - -100 \\leq X < Y \\leq 100\n - -100 \\leq x_i, y_i \\leq 100\n - x_1, x_2, ..., x_N \\neq X\n - x_i are all different.\n - y_1, y_2, ..., y_M \\neq Y\n - y_i are all different.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M X Y\nx_1 x_2 ... x_N\ny_1 y_2 ... y_M\n\n-----Output-----\nIf war will break out, print War; otherwise, print No War.\n\n-----Sample Input-----\n3 2 10 20\n8 15 13\n16 22\n\n-----Sample Output-----\nNo War\n\nThe choice Z = 16 satisfies all of the three conditions as follows, thus they will come to an agreement.\n - X = 10 < 16 \\leq 20 = Y\n - 8, 15, 13 < 16\n - 16, 22 \\geq 16"} +{"id": "03218", "text": "We have N clocks. The hand of the i-th clock (1\u2264i\u2264N) rotates through 360\u00b0 in exactly T_i seconds.\n\nInitially, the hand of every clock stands still, pointing directly upward.\n\nNow, Dolphin starts all the clocks simultaneously.\n\nIn how many seconds will the hand of every clock point directly upward again?\n\n-----Constraints-----\n - 1\u2264N\u2264100 \n - 1\u2264T_i\u226410^{18} \n - All input values are integers. \n - The correct answer is at most 10^{18} seconds.\n\n-----Input-----\nInput is given from Standard Input in the following format: \nN\nT_1\n: \nT_N\n\n-----Output-----\nPrint the number of seconds after which the hand of every clock point directly upward again.\n\n-----Sample Input-----\n2\n2\n3\n\n-----Sample Output-----\n6\n\nWe have two clocks. The time when the hand of each clock points upward is as follows:\n - Clock 1: 2, 4, 6, ... seconds after the beginning\n - Clock 2: 3, 6, 9, ... seconds after the beginning\nTherefore, it takes 6 seconds until the hands of both clocks point directly upward."} +{"id": "03219", "text": "We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.\nWe will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \\leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.\nConsider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.\n\n-----Constraints-----\n - 2 \\leq N \\leq 100\n - 1 \\leq W_i \\leq 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nW_1 W_2 ... W_{N-1} W_N\n\n-----Output-----\nPrint the minimum possible absolute difference of S_1 and S_2.\n\n-----Sample Input-----\n3\n1 2 3\n\n-----Sample Output-----\n0\n\nIf T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0."} +{"id": "03220", "text": "You are given a rectangular matrix of size $n \\times m$ consisting of integers from $1$ to $2 \\cdot 10^5$.\n\nIn one move, you can: choose any element of the matrix and change its value to any integer between $1$ and $n \\cdot m$, inclusive; take any column and shift it one cell up cyclically (see the example of such cyclic shift below). \n\nA cyclic shift is an operation such that you choose some $j$ ($1 \\le j \\le m$) and set $a_{1, j} := a_{2, j}, a_{2, j} := a_{3, j}, \\dots, a_{n, j} := a_{1, j}$ simultaneously. [Image] Example of cyclic shift of the first column \n\nYou want to perform the minimum number of moves to make this matrix look like this: $\\left. \\begin{array}{|c c c c|} \\hline 1 & {2} & {\\ldots} & {m} \\\\{m + 1} & {m + 2} & {\\ldots} & {2m} \\\\{\\vdots} & {\\vdots} & {\\ddots} & {\\vdots} \\\\{(n - 1) m + 1} & {(n - 1) m + 2} & {\\ldots} & {nm} \\\\ \\hline \\end{array} \\right.$ \n\nIn other words, the goal is to obtain the matrix, where $a_{1, 1} = 1, a_{1, 2} = 2, \\dots, a_{1, m} = m, a_{2, 1} = m + 1, a_{2, 2} = m + 2, \\dots, a_{n, m} = n \\cdot m$ (i.e. $a_{i, j} = (i - 1) \\cdot m + j$) with the minimum number of moves performed.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5, n \\cdot m \\le 2 \\cdot 10^5$) \u2014 the size of the matrix.\n\nThe next $n$ lines contain $m$ integers each. The number at the line $i$ and position $j$ is $a_{i, j}$ ($1 \\le a_{i, j} \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of moves required to obtain the matrix, where $a_{1, 1} = 1, a_{1, 2} = 2, \\dots, a_{1, m} = m, a_{2, 1} = m + 1, a_{2, 2} = m + 2, \\dots, a_{n, m} = n \\cdot m$ ($a_{i, j} = (i - 1)m + j$).\n\n\n-----Examples-----\nInput\n3 3\n3 2 1\n1 2 3\n4 5 6\n\nOutput\n6\n\nInput\n4 3\n1 2 3\n4 5 6\n7 8 9\n10 11 12\n\nOutput\n0\n\nInput\n3 4\n1 6 3 4\n5 10 7 8\n9 2 11 12\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example, you can set $a_{1, 1} := 7, a_{1, 2} := 8$ and $a_{1, 3} := 9$ then shift the first, the second and the third columns cyclically, so the answer is $6$. It can be shown that you cannot achieve a better answer.\n\nIn the second example, the matrix is already good so the answer is $0$.\n\nIn the third example, it is enough to shift the second column cyclically twice to obtain a good matrix, so the answer is $2$."} +{"id": "03221", "text": "There are $n$ students in a university. The number of students is even. The $i$-th student has programming skill equal to $a_i$. \n\nThe coach wants to form $\\frac{n}{2}$ teams. Each team should consist of exactly two students, and each student should belong to exactly one team. Two students can form a team only if their skills are equal (otherwise they cannot understand each other and cannot form a team).\n\nStudents can solve problems to increase their skill. One solved problem increases the skill by one.\n\nThe coach wants to know the minimum total number of problems students should solve to form exactly $\\frac{n}{2}$ teams (i.e. each pair of students should form a team). Your task is to find this number.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 100$) \u2014 the number of students. It is guaranteed that $n$ is even.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$), where $a_i$ is the skill of the $i$-th student.\n\n\n-----Output-----\n\nPrint one number \u2014 the minimum total number of problems students should solve to form exactly $\\frac{n}{2}$ teams.\n\n\n-----Examples-----\nInput\n6\n5 10 2 3 14 5\n\nOutput\n5\n\nInput\n2\n1 100\n\nOutput\n99\n\n\n\n-----Note-----\n\nIn the first example the optimal teams will be: $(3, 4)$, $(1, 6)$ and $(2, 5)$, where numbers in brackets are indices of students. Then, to form the first team the third student should solve $1$ problem, to form the second team nobody needs to solve problems and to form the third team the second student should solve $4$ problems so the answer is $1 + 4 = 5$.\n\nIn the second example the first student should solve $99$ problems to form a team with the second one."} +{"id": "03222", "text": "Each day in Berland consists of $n$ hours. Polycarp likes time management. That's why he has a fixed schedule for each day \u2014 it is a sequence $a_1, a_2, \\dots, a_n$ (each $a_i$ is either $0$ or $1$), where $a_i=0$ if Polycarp works during the $i$-th hour of the day and $a_i=1$ if Polycarp rests during the $i$-th hour of the day.\n\nDays go one after another endlessly and Polycarp uses the same schedule for each day.\n\nWhat is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day.\n\n\n-----Input-----\n\nThe first line contains $n$ ($1 \\le n \\le 2\\cdot10^5$) \u2014 number of hours per day.\n\nThe second line contains $n$ integer numbers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 1$), where $a_i=0$ if the $i$-th hour in a day is working and $a_i=1$ if the $i$-th hour is resting. It is guaranteed that $a_i=0$ for at least one $i$.\n\n\n-----Output-----\n\nPrint the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day.\n\n\n-----Examples-----\nInput\n5\n1 0 1 0 1\n\nOutput\n2\n\nInput\n6\n0 1 0 1 1 0\n\nOutput\n2\n\nInput\n7\n1 0 1 1 1 0 1\n\nOutput\n3\n\nInput\n3\n0 0 0\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, the maximal rest starts in last hour and goes to the first hour of the next day.\n\nIn the second example, Polycarp has maximal rest from the $4$-th to the $5$-th hour.\n\nIn the third example, Polycarp has maximal rest from the $3$-rd to the $5$-th hour.\n\nIn the fourth example, Polycarp has no rest at all."} +{"id": "03223", "text": "Little Petya wanted to give an April Fools Day present to some scientists. After some hesitation he decided to give them the array that he got as a present in Codeforces Round #153 (Div.2). The scientists rejoiced at the gift and decided to put some important facts to this array. Here are the first few of the facts: The highest mountain above sea level in the world is Mount Everest. Its peak rises to 8848 m. The largest board game tournament consisted of 958 participants playing chapaev. The largest online maths competition consisted of 12766 participants. The Nile is credited as the longest river in the world. From its farthest stream in Burundi, it extends 6695 km in length. While not in flood, the main stretches of the Amazon river in South America can reach widths of up to 1100 km at its widest points. Angel Falls is the highest waterfall. Its greatest single drop measures 807 m. The Hotel Everest View above Namche, Nepal \u2014 the village closest to Everest base camp \u2013 is at a record height of 31962 m Uranium is the heaviest of all the naturally occurring elements. Its most common isotope has a nucleus containing 146 neutrons. The coldest permanently inhabited place is the Siberian village of Oymyakon, where the temperature of -68\u00b0C was registered in the twentieth century. The longest snake held in captivity is over 25 feet long. Its name is Medusa. Colonel Meow holds the world record for longest fur on a cat \u2014 almost 134 centimeters. Sea otters can have up to 10000 hairs per square inch. This is the most dense fur in the animal kingdom. The largest state of USA is Alaska; its area is 663268 square miles Alaska has a longer coastline than all of the other 49 U.S. States put together: it is 154103 miles long. Lake Baikal is the largest freshwater lake in the world. It reaches 1642\u00a0meters in depth and contains around one-fifth of the world\u2019s unfrozen fresh water. The most colorful national flag is the one of Turkmenistan, with 106 colors. \n\n\n-----Input-----\n\nThe input will contain a single integer between 1 and 16.\n\n\n-----Output-----\n\nOutput a single integer.\n\n\n-----Examples-----\nInput\n1\n\nOutput\n1\n\nInput\n7\n\nOutput\n0"} +{"id": "03224", "text": "Not to be confused with chessboard.\n\n [Image] \n\n\n-----Input-----\n\nThe first line of input contains a single integer N (1 \u2264 N \u2264 100) \u2014 the number of cheeses you have.\n\nThe next N lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. The type is either \"soft\" or \"hard. All cheese names are distinct.\n\n\n-----Output-----\n\nOutput a single number.\n\n\n-----Examples-----\nInput\n9\nbrie soft\ncamembert soft\nfeta soft\ngoat soft\nmuenster soft\nasiago hard\ncheddar hard\ngouda hard\nswiss hard\n\nOutput\n3\n\nInput\n6\nparmesan hard\nemmental hard\nedam hard\ncolby hard\ngruyere hard\nasiago hard\n\nOutput\n4"} +{"id": "03225", "text": "You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$.\n\nYou can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \\% n$, where $x \\% y$ is $x$ modulo $y$.\n\nYour task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$.\n\nArray $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \\le i \\le n$), that $x_i < y_i$, and for any $j$ ($1 \\le j < i$) $x_j = y_j$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$, $b$ and $c$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i < n$), where $a_i$ is the $i$-th element of $a$.\n\nThe third line of the input contains $n$ integers $b_1, b_2, \\dots, b_n$ ($0 \\le b_i < n$), where $b_i$ is the $i$-th element of $b$.\n\n\n-----Output-----\n\nPrint the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \\% n$.\n\n\n-----Examples-----\nInput\n4\n0 1 2 1\n3 2 1 1\n\nOutput\n1 0 0 2 \n\nInput\n7\n2 5 1 5 3 4 3\n2 4 3 5 6 5 1\n\nOutput\n0 0 0 1 0 2 4"} +{"id": "03226", "text": "[Image] \n\n\n-----Input-----\n\nThe input consists of four lines, each line containing a single digit 0 or 1.\n\n\n-----Output-----\n\nOutput a single digit, 0 or 1.\n\n\n-----Example-----\nInput\n0\n1\n1\n0\n\nOutput\n0"} +{"id": "03227", "text": "Takahashi is meeting up with Aoki.\nThey have planned to meet at a place that is D meters away from Takahashi's house in T minutes from now.\nTakahashi will leave his house now and go straight to the place at a speed of S meters per minute.\nWill he arrive in time?\n\n-----Constraints-----\n - 1 \\leq D \\leq 10000\n - 1 \\leq T \\leq 10000\n - 1 \\leq S \\leq 10000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nD T S\n\n-----Output-----\nIf Takahashi will reach the place in time, print Yes; otherwise, print No.\n\n-----Sample Input-----\n1000 15 80\n\n-----Sample Output-----\nYes\n\nIt takes 12.5 minutes to go 1000 meters to the place at a speed of 80 meters per minute. They have planned to meet in 15 minutes so he will arrive in time."} +{"id": "03228", "text": "We have a bingo card with a 3\\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.\nThe MC will choose N numbers, b_1, b_2, \\cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.\nDetermine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq A_{i, j} \\leq 100\n - A_{i_1, j_1} \\neq A_{i_2, j_2} ((i_1, j_1) \\neq (i_2, j_2))\n - 1 \\leq N \\leq 10\n - 1 \\leq b_i \\leq 100\n - b_i \\neq b_j (i \\neq j)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA_{1, 1} A_{1, 2} A_{1, 3}\nA_{2, 1} A_{2, 2} A_{2, 3}\nA_{3, 1} A_{3, 2} A_{3, 3}\nN\nb_1\n\\vdots\nb_N\n\n-----Output-----\nIf we will have a bingo, print Yes; otherwise, print No.\n\n-----Sample Input-----\n84 97 66\n79 89 11\n61 59 7\n7\n89\n7\n87\n79\n24\n84\n30\n\n-----Sample Output-----\nYes\n\nWe will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right."} +{"id": "03229", "text": "Takahashi has N days of summer vacation.\nHis teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.\nHe cannot do multiple assignments on the same day, or hang out on a day he does an assignment.\nWhat is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?\nIf Takahashi cannot finish all the assignments during the vacation, print -1 instead.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^6\n - 1 \\leq M \\leq 10^4\n - 1 \\leq A_i \\leq 10^4\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 ... A_M\n\n-----Output-----\nPrint the maximum number of days Takahashi can hang out during the vacation, or -1.\n\n-----Sample Input-----\n41 2\n5 6\n\n-----Sample Output-----\n30\n\nFor example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out."} +{"id": "03230", "text": "Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo.\n\nAs the name of the contest is AtCoder Beginner Contest 100, Ringo would be happy if he is given a positive integer that can be divided by 100 exactly D times.\nFind the N-th smallest integer that would make Ringo happy.\n\n-----Constraints-----\n - D is 0, 1 or 2.\n - N is an integer between 1 and 100 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nD N\n\n-----Output-----\nPrint the N-th smallest integer that can be divided by 100 exactly D times.\n\n-----Sample Input-----\n0 5\n\n-----Sample Output-----\n5\n\nThe integers that can be divided by 100 exactly 0 times (that is, not divisible by 100) are as follows: 1, 2, 3, 4, 5, 6, 7, ...\n\nThus, the 5-th smallest integer that would make Ringo happy is 5."} +{"id": "03231", "text": "There are N integers, A_1, A_2, ..., A_N, written on the blackboard.\nYou will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.\nFind the maximum possible greatest common divisor of the N integers on the blackboard after your move.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 10^5\n - 1 \\leq A_i \\leq 10^9\n\n-----Output-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the maximum possible greatest common divisor of the N integers on the blackboard after your move.\n\n-----Sample Input-----\n3\n7 6 8\n\n-----Sample Output-----\n2\n\nIf we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value."} +{"id": "03232", "text": "Takahashi is a teacher responsible for a class of N students.\nThe students are given distinct student numbers from 1 to N.\nToday, all the students entered the classroom at different times.\nAccording to Takahashi's record, there were A_i students in the classroom when student number i entered the classroom (including student number i).\nFrom these records, reconstruct the order in which the students entered the classroom.\n\n-----Constraints-----\n - 1 \\le N \\le 10^5 \n - 1 \\le A_i \\le N \n - A_i \\neq A_j (i \\neq j)\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 \\ldots A_N\n\n-----Output-----\nPrint the student numbers of the students in the order the students entered the classroom.\n\n-----Sample Input-----\n3\n2 3 1\n\n-----Sample Output-----\n3 1 2\n\nFirst, student number 3 entered the classroom.\nThen, student number 1 entered the classroom.\nFinally, student number 2 entered the classroom."} +{"id": "03233", "text": "Takahashi has come to an integer shop to buy an integer.\nThe shop sells the integers from 1 through 10^9. The integer N is sold for A \\times N + B \\times d(N) yen (the currency of Japan), where d(N) is the number of digits in the decimal notation of N.\nFind the largest integer that Takahashi can buy when he has X yen. If no integer can be bought, print 0.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq A \\leq 10^9\n - 1 \\leq B \\leq 10^9\n - 1 \\leq X \\leq 10^{18}\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B X\n\n-----Output-----\nPrint the greatest integer that Takahashi can buy. If no integer can be bought, print 0.\n\n-----Sample Input-----\n10 7 100\n\n-----Sample Output-----\n9\n\nThe integer 9 is sold for 10 \\times 9 + 7 \\times 1 = 97 yen, and this is the greatest integer that can be bought.\nSome of the other integers are sold for the following prices:\n - 10: 10 \\times 10 + 7 \\times 2 = 114 yen\n - 100: 10 \\times 100 + 7 \\times 3 = 1021 yen\n - 12345: 10 \\times 12345 + 7 \\times 5 = 123485 yen"} +{"id": "03234", "text": "N friends of Takahashi has come to a theme park.\nTo ride the most popular roller coaster in the park, you must be at least K centimeters tall.\nThe i-th friend is h_i centimeters tall.\nHow many of the Takahashi's friends can ride the roller coaster?\n\n-----Constraints-----\n - 1 \\le N \\le 10^5 \n - 1 \\le K \\le 500 \n - 1 \\le h_i \\le 500\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nh_1 h_2 \\ldots h_N\n\n-----Output-----\nPrint the number of people among the Takahashi's friends who can ride the roller coaster.\n\n-----Sample Input-----\n4 150\n150 140 100 200\n\n-----Sample Output-----\n2\n\nTwo of them can ride the roller coaster: the first and fourth friends."} +{"id": "03235", "text": "We have held a popularity poll for N items on sale. Item i received A_i votes.\nFrom these N items, we will select M as popular items. However, we cannot select an item with less than \\dfrac{1}{4M} of the total number of votes.\nIf M popular items can be selected, print Yes; otherwise, print No.\n\n-----Constraints-----\n - 1 \\leq M \\leq N \\leq 100\n - 1 \\leq A_i \\leq 1000\n - A_i are distinct.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\nA_1 ... A_N\n\n-----Output-----\nIf M popular items can be selected, print Yes; otherwise, print No.\n\n-----Sample Input-----\n4 1\n5 4 2 1\n\n-----Sample Output-----\nYes\n\nThere were 12 votes in total. The most popular item received 5 votes, and we can select it."} +{"id": "03236", "text": "We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \\leq i \\leq H, 1 \\leq j \\leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #.\nConsider doing the following operation:\n - Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.\nYou are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.\n\n-----Constraints-----\n - 1 \\leq H, W \\leq 6\n - 1 \\leq K \\leq HW\n - c_{i,j} is . or #.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W K\nc_{1,1}c_{1,2}...c_{1,W}\nc_{2,1}c_{2,2}...c_{2,W}\n:\nc_{H,1}c_{H,2}...c_{H,W}\n\n-----Output-----\nPrint an integer representing the number of choices of rows and columns satisfying the condition.\n\n-----Sample Input-----\n2 3 2\n..#\n###\n\n-----Sample Output-----\n5\n\nFive choices below satisfy the condition.\n - The 1-st row and 1-st column\n - The 1-st row and 2-nd column\n - The 1-st row and 3-rd column\n - The 1-st and 2-nd column\n - The 3-rd column"} +{"id": "03237", "text": "You are given two non-negative integers L and R.\nWe will choose two integers i and j such that L \\leq i < j \\leq R.\nFind the minimum possible value of (i \\times j) \\mbox{ mod } 2019.\n\n-----Constraints-----\n - All values in input are integers.\n - 0 \\leq L < R \\leq 2 \\times 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nL R\n\n-----Output-----\nPrint the minimum possible value of (i \\times j) \\mbox{ mod } 2019 when i and j are chosen under the given condition.\n\n-----Sample Input-----\n2020 2040\n\n-----Sample Output-----\n2\n\nWhen (i, j) = (2020, 2021), (i \\times j) \\mbox{ mod } 2019 = 2."} +{"id": "03238", "text": "You are given a string S. Each character of S is uppercase or lowercase English letter.\nDetermine if S satisfies all of the following conditions:\n - The initial character of S is an uppercase A.\n - There is exactly one occurrence of C between the third character from the beginning and the second to last character (inclusive).\n - All letters except the A and C mentioned above are lowercase.\n\n-----Constraints-----\n - 4 \u2264 |S| \u2264 10 (|S| is the length of the string S.)\n - Each character of S is uppercase or lowercase English letter.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nIf S satisfies all of the conditions in the problem statement, print AC; otherwise, print WA.\n\n-----Sample Input-----\nAtCoder\n\n-----Sample Output-----\nAC\n\nThe first letter is A, the third letter is C and the remaining letters are all lowercase, so all the conditions are satisfied."} +{"id": "03239", "text": "Mr. Infinity has a string S consisting of digits from 1 to 9. Each time the date changes, this string changes as follows:\n - Each occurrence of 2 in S is replaced with 22. Similarly, each 3 becomes 333, 4 becomes 4444, 5 becomes 55555, 6 becomes 666666, 7 becomes 7777777, 8 becomes 88888888 and 9 becomes 999999999. 1 remains as 1.\nFor example, if S is 1324, it becomes 1333224444 the next day, and it becomes 133333333322224444444444444444 the day after next.\nYou are interested in what the string looks like after 5 \\times 10^{15} days. What is the K-th character from the left in the string after 5 \\times 10^{15} days?\n\n-----Constraints-----\n - S is a string of length between 1 and 100 (inclusive).\n - K is an integer between 1 and 10^{18} (inclusive).\n - The length of the string after 5 \\times 10^{15} days is at least K.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\nK\n\n-----Output-----\nPrint the K-th character from the left in Mr. Infinity's string after 5 \\times 10^{15} days.\n\n-----Sample Input-----\n1214\n4\n\n-----Sample Output-----\n2\n\nThe string S changes as follows: \n - Now: 1214\n - After one day: 12214444\n - After two days: 1222214444444444444444\n - After three days: 12222222214444444444444444444444444444444444444444444444444444444444444444\nThe first five characters in the string after 5 \\times 10^{15} days is 12222. As K=4, we should print the fourth character, 2."} +{"id": "03240", "text": "We have a sequence p = {p_1,\\ p_2,\\ ...,\\ p_N} which is a permutation of {1,\\ 2,\\ ...,\\ N}.\nYou can perform the following operation at most once: choose integers i and j (1 \\leq i < j \\leq N), and swap p_i and p_j. Note that you can also choose not to perform it.\nPrint YES if you can sort p in ascending order in this way, and NO otherwise.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 50\n - p is a permutation of {1,\\ 2,\\ ...,\\ N}.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\np_1 p_2 ... p_N\n\n-----Output-----\nPrint YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.\n\n-----Sample Input-----\n5\n5 2 3 4 1\n\n-----Sample Output-----\nYES\n\nYou can sort p in ascending order by swapping p_1 and p_5."} +{"id": "03241", "text": "Polycarp likes numbers that are divisible by 3.\n\nHe has a huge number $s$. Polycarp wants to cut from it the maximum number of numbers that are divisible by $3$. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after $m$ such cuts, there will be $m+1$ parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by $3$.\n\nFor example, if the original number is $s=3121$, then Polycarp can cut it into three parts with two cuts: $3|1|21$. As a result, he will get two numbers that are divisible by $3$.\n\nPolycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.\n\nWhat is the maximum number of numbers divisible by $3$ that Polycarp can obtain?\n\n\n-----Input-----\n\nThe first line of the input contains a positive integer $s$. The number of digits of the number $s$ is between $1$ and $2\\cdot10^5$, inclusive. The first (leftmost) digit is not equal to 0.\n\n\n-----Output-----\n\nPrint the maximum number of numbers divisible by $3$ that Polycarp can get by making vertical cuts in the given number $s$.\n\n\n-----Examples-----\nInput\n3121\n\nOutput\n2\n\nInput\n6\n\nOutput\n1\n\nInput\n1000000000000000000000000000000000\n\nOutput\n33\n\nInput\n201920181\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example, an example set of optimal cuts on the number is 3|1|21.\n\nIn the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by $3$.\n\nIn the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and $33$ digits 0. Each of the $33$ digits 0 forms a number that is divisible by $3$.\n\nIn the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers $0$, $9$, $201$ and $81$ are divisible by $3$."} +{"id": "03242", "text": "You are given two arrays $a$ and $b$, each contains $n$ integers.\n\nYou want to create a new array $c$ as follows: choose some real (i.e. not necessarily integer) number $d$, and then for every $i \\in [1, n]$ let $c_i := d \\cdot a_i + b_i$.\n\nYour goal is to maximize the number of zeroes in array $c$. What is the largest possible answer, if you choose $d$ optimally?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in both arrays.\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($-10^9 \\le a_i \\le 10^9$).\n\nThe third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($-10^9 \\le b_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of zeroes in array $c$, if you choose $d$ optimally.\n\n\n-----Examples-----\nInput\n5\n1 2 3 4 5\n2 4 7 11 3\n\nOutput\n2\n\nInput\n3\n13 37 39\n1 2 3\n\nOutput\n2\n\nInput\n4\n0 0 0 0\n1 2 3 4\n\nOutput\n0\n\nInput\n3\n1 2 -1\n-6 -12 6\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example, we may choose $d = -2$.\n\nIn the second example, we may choose $d = -\\frac{1}{13}$.\n\nIn the third example, we cannot obtain any zero in array $c$, no matter which $d$ we choose.\n\nIn the fourth example, we may choose $d = 6$."} +{"id": "03243", "text": "There are $n$ left boots and $n$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $l$ and $r$, both of length $n$. The character $l_i$ stands for the color of the $i$-th left boot and the character $r_i$ stands for the color of the $i$-th right boot.\n\nA lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.\n\nFor example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').\n\nCompute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.\n\nPrint the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair.\n\n\n-----Input-----\n\nThe first line contains $n$ ($1 \\le n \\le 150000$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots).\n\nThe second line contains the string $l$ of length $n$. It contains only lowercase Latin letters or question marks. The $i$-th character stands for the color of the $i$-th left boot.\n\nThe third line contains the string $r$ of length $n$. It contains only lowercase Latin letters or question marks. The $i$-th character stands for the color of the $i$-th right boot.\n\n\n-----Output-----\n\nPrint $k$ \u2014 the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors.\n\nThe following $k$ lines should contain pairs $a_j, b_j$ ($1 \\le a_j, b_j \\le n$). The $j$-th of these lines should contain the index $a_j$ of the left boot in the $j$-th pair and index $b_j$ of the right boot in the $j$-th pair. All the numbers $a_j$ should be distinct (unique), all the numbers $b_j$ should be distinct (unique).\n\nIf there are many optimal answers, print any of them.\n\n\n-----Examples-----\nInput\n10\ncodeforces\ndodivthree\n\nOutput\n5\n7 8\n4 9\n2 2\n9 10\n3 1\n\nInput\n7\nabaca?b\nzabbbcc\n\nOutput\n5\n6 5\n2 3\n4 6\n7 4\n1 2\n\nInput\n9\nbambarbia\nhellocode\n\nOutput\n0\n\nInput\n10\ncode??????\n??????test\n\nOutput\n10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10"} +{"id": "03244", "text": "This problem is given in two editions, which differ exclusively in the constraints on the number $n$.\n\nYou are given an array of integers $a[1], a[2], \\dots, a[n].$ A block is a sequence of contiguous (consecutive) elements $a[l], a[l+1], \\dots, a[r]$ ($1 \\le l \\le r \\le n$). Thus, a block is defined by a pair of indices $(l, r)$.\n\nFind a set of blocks $(l_1, r_1), (l_2, r_2), \\dots, (l_k, r_k)$ such that: They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks $(l_i, r_i)$ and $(l_j, r_j$) where $i \\neq j$ either $r_i < l_j$ or $r_j < l_i$. For each block the sum of its elements is the same. Formally, $$a[l_1]+a[l_1+1]+\\dots+a[r_1]=a[l_2]+a[l_2+1]+\\dots+a[r_2]=$$ $$\\dots =$$ $$a[l_k]+a[l_k+1]+\\dots+a[r_k].$$ The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks $(l_1', r_1'), (l_2', r_2'), \\dots, (l_{k'}', r_{k'}')$ satisfying the above two requirements with $k' > k$. $\\left. \\begin{array}{|l|l|l|l|l|l|} \\hline 4 & {1} & {2} & {2} & {1} & {5} & {3} \\\\ \\hline \\end{array} \\right.$ The picture corresponds to the first example. Blue boxes illustrate blocks. \n\nWrite a program to find such a set of blocks.\n\n\n-----Input-----\n\nThe first line contains integer $n$ ($1 \\le n \\le 1500$) \u2014 the length of the given array. The second line contains the sequence of elements $a[1], a[2], \\dots, a[n]$ ($-10^5 \\le a_i \\le 10^5$).\n\n\n-----Output-----\n\nIn the first line print the integer $k$ ($1 \\le k \\le n$). The following $k$ lines should contain blocks, one per line. In each line print a pair of indices $l_i, r_i$ ($1 \\le l_i \\le r_i \\le n$) \u2014 the bounds of the $i$-th block. You can print blocks in any order. If there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n7\n4 1 2 2 1 5 3\n\nOutput\n3\n7 7\n2 3\n4 5\n\nInput\n11\n-5 -4 -3 -2 -1 0 1 2 3 4 5\n\nOutput\n2\n3 4\n1 1\n\nInput\n4\n1 1 1 1\n\nOutput\n4\n4 4\n1 1\n2 2\n3 3"} +{"id": "03245", "text": "You are given an array $a$, consisting of $n$ positive integers.\n\nLet's call a concatenation of numbers $x$ and $y$ the number that is obtained by writing down numbers $x$ and $y$ one right after another without changing the order. For example, a concatenation of numbers $12$ and $3456$ is a number $123456$.\n\nCount the number of ordered pairs of positions $(i, j)$ ($i \\neq j$) in array $a$ such that the concatenation of $a_i$ and $a_j$ is divisible by $k$.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le n \\le 2 \\cdot 10^5$, $2 \\le k \\le 10^9$).\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$).\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of ordered pairs of positions $(i, j)$ ($i \\neq j$) in array $a$ such that the concatenation of $a_i$ and $a_j$ is divisible by $k$.\n\n\n-----Examples-----\nInput\n6 11\n45 1 10 12 11 7\n\nOutput\n7\n\nInput\n4 2\n2 78 4 10\n\nOutput\n12\n\nInput\n5 2\n3 7 19 3 3\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example pairs $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 1)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ suffice. They produce numbers $451$, $4510$, $110$, $1045$, $1012$, $121$, $1210$, respectively, each of them is divisible by $11$.\n\nIn the second example all $n(n - 1)$ pairs suffice.\n\nIn the third example no pair is sufficient."} +{"id": "03246", "text": "There is an integer sequence A of length N whose values are unknown.\nGiven is an integer sequence B of length N-1 which is known to satisfy the following:\n B_i \\geq \\max(A_i, A_{i+1}) \nFind the maximum possible sum of the elements of A.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \\leq N \\leq 100\n - 0 \\leq B_i \\leq 10^5\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nB_1 B_2 ... B_{N-1}\n\n-----Output-----\nPrint the maximum possible sum of the elements of A.\n\n-----Sample Input-----\n3\n2 5\n\n-----Sample Output-----\n9\n\nA can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum."} +{"id": "03247", "text": "Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).\nConsider a sequence A satisfying the following conditions:\n - A is a sequence of N positive integers.\n - 1 \\leq A_1 \\leq A_2 \\le \\cdots \\leq A_N \\leq M.\nLet us define a score of this sequence as follows:\n - The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)\nFind the maximum possible score of A.\n\n-----Constraints-----\n - All values in input are integers.\n - 2 \u2264 N \u2264 10\n - 1 \\leq M \\leq 10\n - 1 \\leq Q \\leq 50\n - 1 \\leq a_i < b_i \\leq N ( i = 1, 2, ..., Q )\n - 0 \\leq c_i \\leq M - 1 ( i = 1, 2, ..., Q )\n - (a_i, b_i, c_i) \\neq (a_j, b_j, c_j) (where i \\neq j)\n - 1 \\leq d_i \\leq 10^5 ( i = 1, 2, ..., Q )\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M Q\na_1 b_1 c_1 d_1\n:\na_Q b_Q c_Q d_Q\n\n-----Output-----\nPrint the maximum possible score of A.\n\n-----Sample Input-----\n3 4 3\n1 3 3 100\n1 2 2 10\n2 3 2 10\n\n-----Sample Output-----\n110\n\nWhen A = \\{1, 3, 4\\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110."} +{"id": "03248", "text": "You are given an integer sequence A of length N.\nFind the maximum absolute difference of two elements (with different indices) in A.\n\n-----Constraints-----\n - 2 \\leq N \\leq 100\n - 1 \\leq A_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the maximum absolute difference of two elements (with different indices) in A.\n\n-----Sample Input-----\n4\n1 4 6 3\n\n-----Sample Output-----\n5\n\nThe maximum absolute difference of two elements is A_3-A_1=6-1=5."} +{"id": "03249", "text": "There are N towns in a coordinate plane. Town i is located at coordinates (x_i, y_i). The distance between Town i and Town j is \\sqrt{\\left(x_i-x_j\\right)^2+\\left(y_i-y_j\\right)^2}.\nThere are N! possible paths to visit all of these towns once. Let the length of a path be the distance covered when we start at the first town in the path, visit the second, third, \\dots, towns, and arrive at the last town (assume that we travel in a straight line from a town to another). Compute the average length of these N! paths.\n\n-----Constraints-----\n - 2 \\leq N \\leq 8\n - -1000 \\leq x_i \\leq 1000\n - -1000 \\leq y_i \\leq 1000\n - \\left(x_i, y_i\\right) \\neq \\left(x_j, y_j\\right) (if i \\neq j)\n - (Added 21:12 JST) All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nx_1 y_1\n:\nx_N y_N\n\n-----Output-----\nPrint the average length of the paths.\nYour output will be judges as correct when the absolute difference from the judge's output is at most 10^{-6}.\n\n-----Sample Input-----\n3\n0 0\n1 0\n0 1\n\n-----Sample Output-----\n2.2761423749\n\nThere are six paths to visit the towns: 1 \u2192 2 \u2192 3, 1 \u2192 3 \u2192 2, 2 \u2192 1 \u2192 3, 2 \u2192 3 \u2192 1, 3 \u2192 1 \u2192 2, and 3 \u2192 2 \u2192 1.\nThe length of the path 1 \u2192 2 \u2192 3 is \\sqrt{\\left(0-1\\right)^2+\\left(0-0\\right)^2} + \\sqrt{\\left(1-0\\right)^2+\\left(0-1\\right)^2} = 1+\\sqrt{2}.\nBy calculating the lengths of the other paths in this way, we see that the average length of all routes is:\n\\frac{\\left(1+\\sqrt{2}\\right)+\\left(1+\\sqrt{2}\\right)+\\left(2\\right)+\\left(1+\\sqrt{2}\\right)+\\left(2\\right)+\\left(1+\\sqrt{2}\\right)}{6} = 2.276142..."} +{"id": "03250", "text": "The window of Takahashi's room has a width of A. There are two curtains hung over the window, each of which has a horizontal length of B. (Vertically, the curtains are long enough to cover the whole window.)\nWe will close the window so as to minimize the total horizontal length of the uncovered part of the window.\nFind the total horizontal length of the uncovered parts of the window then.\n\n-----Constraints-----\n - 1 \\leq A \\leq 100\n - 1 \\leq B \\leq 100\n - A and B are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the total horizontal length of the uncovered parts of the window.\n\n-----Sample Input-----\n12 4\n\n-----Sample Output-----\n4\n\nWe have a window with a horizontal length of 12, and two curtains, each of length 4, that cover both ends of the window, for example. The uncovered part has a horizontal length of 4."} +{"id": "03251", "text": "You are given an integer N.\n\nFor two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B.\n\nFor example, F(3,11) = 2 since 3 has one digit and 11 has two digits.\n\nFind the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \\times B.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^{10}\n - N is an integer.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \\times B.\n\n-----Sample Input-----\n10000\n\n-----Sample Output-----\n3\n\nF(A,B) has a minimum value of 3 at (A,B)=(100,100)."} +{"id": "03252", "text": "Katsusando loves omelette rice.\nBesides, he loves cr\u00e8me br\u00fbl\u00e9e, tenderloin steak and so on, and believes that these foods are all loved by everyone.\nTo prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not.\nThe i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food.\nFind the number of the foods liked by all the N people.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N, M \\leq 30\n - 1 \\leq K_i \\leq M\n - 1 \\leq A_{ij} \\leq M\n - For each i (1 \\leq i \\leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct.\n\n-----Constraints-----\nInput is given from Standard Input in the following format:\nN M\nK_1 A_{11} A_{12} ... A_{1K_1}\nK_2 A_{21} A_{22} ... A_{2K_2}\n:\nK_N A_{N1} A_{N2} ... A_{NK_N}\n\n-----Output-----\nPrint the number of the foods liked by all the N people.\n\n-----Sample Input-----\n3 4\n2 1 3\n3 1 2 3\n2 3 2\n\n-----Sample Output-----\n1\n\nAs only the third food is liked by all the three people, 1 should be printed."} +{"id": "03253", "text": "Given is an integer N.\nTakahashi chooses an integer a from the positive integers not greater than N with equal probability.\nFind the probability that a is odd.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the probability that a is odd.\nYour output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-6}.\n\n-----Sample Input-----\n4\n\n-----Sample Output-----\n0.5000000000\n\nThere are four positive integers not greater than 4: 1, 2, 3, and 4. Among them, we have two odd numbers: 1 and 3. Thus, the answer is \\frac{2}{4} = 0.5."} +{"id": "03254", "text": "There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not.\nPerson i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind.\nHow many honest persons can be among those N people at most?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 15\n - 0 \\leq A_i \\leq N - 1\n - 1 \\leq x_{ij} \\leq N\n - x_{ij} \\neq i\n - x_{ij_1} \\neq x_{ij_2} (j_1 \\neq j_2)\n - y_{ij} = 0, 1\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1\nx_{11} y_{11}\nx_{12} y_{12}\n:\nx_{1A_1} y_{1A_1}\nA_2\nx_{21} y_{21}\nx_{22} y_{22}\n:\nx_{2A_2} y_{2A_2}\n:\nA_N\nx_{N1} y_{N1}\nx_{N2} y_{N2}\n:\nx_{NA_N} y_{NA_N}\n\n-----Output-----\nPrint the maximum possible number of honest persons among the N people.\n\n-----Sample Input-----\n3\n1\n2 1\n1\n1 1\n1\n2 0\n\n-----Sample Output-----\n2\n\nIf Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons."} +{"id": "03255", "text": "We have a string S consisting of lowercase English letters.\nIf the length of S is at most K, print S without change.\nIf the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.\n\n-----Constraints-----\n - K is an integer between 1 and 100 (inclusive).\n - S is a string consisting of lowercase English letters.\n - The length of S is between 1 and 100 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK\nS\n\n-----Output-----\nPrint a string as stated in Problem Statement.\n\n-----Sample Input-----\n7\nnikoandsolstice\n\n-----Sample Output-----\nnikoand...\n\nnikoandsolstice has a length of 15, which exceeds K=7.\nWe should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand...."} +{"id": "03256", "text": "In the Kingdom of AtCoder, people use a language called Taknese, which uses lowercase English letters.\nIn Taknese, the plural form of a noun is spelled based on the following rules:\n - If a noun's singular form does not end with s, append s to the end of the singular form.\n - If a noun's singular form ends with s, append es to the end of the singular form.\nYou are given the singular form S of a Taknese noun. Output its plural form.\n\n-----Constraints-----\n - S is a string of length 1 between 1000, inclusive.\n - S contains only lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the plural form of the given Taknese word.\n\n-----Sample Input-----\napple\n\n-----Sample Output-----\napples\n\napple ends with e, so its plural form is apples."} +{"id": "03257", "text": "N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.\nThere are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \\cdots, A_{i, {d_i}}.\nTakahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 100\n - 1 \\leq K \\leq 100\n - 1 \\leq d_i \\leq N\n - 1 \\leq A_{i, 1} < \\cdots < A_{i, d_i} \\leq N\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nd_1\nA_{1, 1} \\cdots A_{1, d_1}\n\\vdots\nd_K\nA_{K, 1} \\cdots A_{K, d_K}\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n3 2\n2\n1 3\n1\n3\n\n-----Sample Output-----\n1\n\n - Snuke 1 has Snack 1.\n - Snuke 2 has no snacks.\n - Snuke 3 has Snack 1 and 2.\nThus, there will be one victim: Snuke 2."} +{"id": "03258", "text": "There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.\nAdjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.\nUltimately, how many slimes will be there?\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - |S| = N\n - S consists of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS\n\n-----Output-----\nPrint the final number of slimes.\n\n-----Sample Input-----\n10\naabbbbaaca\n\n-----Sample Output-----\n5\n\nUltimately, these slimes will fuse into abaca."} +{"id": "03259", "text": "As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}.\n\nSnuke, an employee, would like to play with this sequence.\nSpecifically, he would like to repeat the following operation as many times as possible:\nFor every i satisfying 1 \\leq i \\leq N, perform one of the following: \"divide a_i by 2\" and \"multiply a_i by 3\". \nHere, choosing \"multiply a_i by 3\" for every i is not allowed, and the value of a_i after the operation must be an integer.\n\nAt most how many operations can be performed?\n\n-----Constraints-----\n - N is an integer between 1 and 10 \\ 000 (inclusive).\n - a_i is an integer between 1 and 1 \\ 000 \\ 000 \\ 000 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 a_3 ... a_N\n\n-----Output-----\nPrint the maximum number of operations that Snuke can perform.\n\n-----Sample Input-----\n3\n5 2 4\n\n-----Sample Output-----\n3\n\nThe sequence is initially {5, 2, 4}. Three operations can be performed as follows:\n - First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}.\n - Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}.\n - Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 3}."} +{"id": "03260", "text": "We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.\nWe will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?\n\n-----Constraints-----\n - All values in input are integers.\n - 0 \\leq A, B, C\n - 1 \\leq K \\leq A + B + C \\leq 2 \\times 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B C K\n\n-----Output-----\nPrint the maximum possible sum of the numbers written on the cards chosen.\n\n-----Sample Input-----\n2 1 1 3\n\n-----Sample Output-----\n2\n\nConsider picking up two cards with 1s and one card with a 0.\nIn this case, the sum of the numbers written on the cards is 2, which is the maximum possible value."} +{"id": "03261", "text": "There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.\nTakahashi says: \"there are X animals in total in the garden, and they have Y legs in total.\" Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.\n\n-----Constraints-----\n - 1 \\leq X \\leq 100\n - 1 \\leq Y \\leq 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y\n\n-----Output-----\nIf there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.\n\n-----Sample Input-----\n3 8\n\n-----Sample Output-----\nYes\n\nThe statement \"there are 3 animals in total in the garden, and they have 8 legs in total\" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct."} +{"id": "03262", "text": "You are given an undirected unweighted graph with N vertices and M edges that contains neither self-loops nor double edges.\n\nHere, a self-loop is an edge where a_i = b_i (1\u2264i\u2264M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1\u2264i 1.\nFind the minimum integer m that satisfies the following condition:\n - There exists an integer n such that a_m = a_n (m > n).\n\n-----Constraints-----\n - 1 \\leq s \\leq 100\n - All values in input are integers.\n - It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.\n\n-----Input-----\nInput is given from Standard Input in the following format:\ns\n\n-----Output-----\nPrint the minimum integer m that satisfies the condition.\n\n-----Sample Input-----\n8\n\n-----Sample Output-----\n5\n\na=\\{8,4,2,1,4,2,1,4,2,1,......\\}. As a_5=a_2, the answer is 5."} +{"id": "03347", "text": "Takahashi and Aoki will have a battle using their monsters.\nThe health and strength of Takahashi's monster are A and B, respectively, and those of Aoki's monster are C and D, respectively.\nThe two monsters will take turns attacking, in the order Takahashi's, Aoki's, Takahashi's, Aoki's, ...\nHere, an attack decreases the opponent's health by the value equal to the attacker's strength.\nThe monsters keep attacking until the health of one monster becomes 0 or below. The person with the monster whose health becomes 0 or below loses, and the other person wins.\nIf Takahashi will win, print Yes; if he will lose, print No.\n\n-----Constraints-----\n - 1 \\leq A,B,C,D \\leq 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B C D\n\n-----Output-----\nIf Takahashi will win, print Yes; if he will lose, print No.\n\n-----Sample Input-----\n10 9 10 10\n\n-----Sample Output-----\nNo\n\nFirst, Takahashi's monster attacks Aoki's monster, whose health is now 10-9=1.\nNext, Aoki's monster attacks Takahashi's monster, whose health is now 10-10=0.\nTakahashi's monster is the first to have 0 or less health, so Takahashi loses."} +{"id": "03348", "text": "There are N gems. The value of the i-th gem is V_i.\nYou will choose some of these gems, possibly all or none, and get them.\nHowever, you need to pay a cost of C_i to get the i-th gem.\nLet X be the sum of the values of the gems obtained, and Y be the sum of the costs paid.\nFind the maximum possible value of X-Y.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 20\n - 1 \\leq C_i, V_i \\leq 50\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nV_1 V_2 ... V_N\nC_1 C_2 ... C_N\n\n-----Output-----\nPrint the maximum possible value of X-Y.\n\n-----Sample Input-----\n3\n10 2 5\n6 3 4\n\n-----Sample Output-----\n5\n\nIf we choose the first and third gems, X = 10 + 5 = 15 and Y = 6 + 4 = 10.\nWe have X-Y = 5 here, which is the maximum possible value."} +{"id": "03349", "text": "There is a grid of squares with H horizontal rows and W vertical columns.\nThe square at the i-th row from the top and the j-th column from the left is represented as (i, j).\nEach square is black or white.\nThe color of the square is given as an H-by-W matrix (a_{i, j}).\nIf a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.\nSnuke is compressing this grid.\nHe will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:\n - Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.\nIt can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.\nFind the final state of the grid.\n\n-----Constraints-----\n - 1 \\leq H, W \\leq 100\n - a_{i, j} is . or #.\n - There is at least one black square in the whole grid.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\na_{1, 1}...a_{1, W}\n:\na_{H, 1}...a_{H, W}\n\n-----Output-----\nPrint the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.\n\n-----Sample Input-----\n4 4\n##.#\n....\n##.#\n.#.#\n\n-----Sample Output-----\n###\n###\n.##\n\nThe second row and the third column in the original grid will be removed."} +{"id": "03350", "text": "Print the circumference of a circle of radius R.\n\n-----Constraints-----\n - 1 \\leq R \\leq 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nR\n\n-----Output-----\nPrint the circumference of the circle.\nYour output is considered correct if and only if its absolute or relative error from our answer is at most 10^{-2}.\n\n-----Sample Input-----\n1\n\n-----Sample Output-----\n6.28318530717958623200\n\nSince we accept an absolute or relative error of at most 10^{-2}, 6.28 is also an acceptable output, but 6 is not."} +{"id": "03351", "text": "You are given a 4-character string S consisting of uppercase English letters.\nDetermine if S consists of exactly two kinds of characters which both appear twice in S.\n\n-----Constraints-----\n - The length of S is 4.\n - S consists of uppercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nIf S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.\n\n-----Sample Input-----\nASSA\n\n-----Sample Output-----\nYes\n\nS consists of A and S which both appear twice in S."} +{"id": "03352", "text": "We have two integers: A and B.\nPrint the largest number among A + B, A - B, and A \\times B.\n\n-----Constraints-----\n - All values in input are integers.\n - -100 \\leq A,\\ B \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the largest number among A + B, A - B, and A \\times B.\n\n-----Sample Input-----\n-13 3\n\n-----Sample Output-----\n-10\n\nThe largest number among A + B = -10, A - B = -16, and A \\times B = -39 is -10."} +{"id": "03353", "text": "There are N mountains ranging from east to west, and an ocean to the west.\nAt the top of each mountain, there is an inn. You have decided to choose where to stay from these inns.\nThe height of the i-th mountain from the west is H_i.\nYou can certainly see the ocean from the inn at the top of the westmost mountain.\nFor the inn at the top of the i-th mountain from the west (i = 2, 3, ..., N), you can see the ocean if and only if H_1 \\leq H_i, H_2 \\leq H_i, ..., and H_{i-1} \\leq H_i.\nFrom how many of these N inns can you see the ocean?\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 20\n - 1 \\leq H_i \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nH_1 H_2 ... H_N\n\n-----Output-----\nPrint the number of inns from which you can see the ocean.\n\n-----Sample Input-----\n4\n6 5 6 8\n\n-----Sample Output-----\n3\n\nYou can see the ocean from the first, third and fourth inns from the west."} +{"id": "03354", "text": "Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $1$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $3$ steps, and the second contains $4$ steps, she will pronounce the numbers $1, 2, 3, 1, 2, 3, 4$.\n\nYou are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.\n\nThe given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.\n\n\n-----Input-----\n\nThe first line contains $n$ ($1 \\le n \\le 1000$) \u2014 the total number of numbers pronounced by Tanya.\n\nThe second line contains integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 1000$) \u2014 all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with $x$ steps, she will pronounce the numbers $1, 2, \\dots, x$ in that order.\n\nThe given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.\n\n\n-----Output-----\n\nIn the first line, output $t$ \u2014 the number of stairways that Tanya climbed. In the second line, output $t$ numbers \u2014 the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.\n\n\n-----Examples-----\nInput\n7\n1 2 3 1 2 3 4\n\nOutput\n2\n3 4 \nInput\n4\n1 1 1 1\n\nOutput\n4\n1 1 1 1 \nInput\n5\n1 2 3 4 5\n\nOutput\n1\n5 \nInput\n5\n1 2 1 2 1\n\nOutput\n3\n2 2 1"} +{"id": "03355", "text": "Recently Vova found $n$ candy wrappers. He remembers that he bought $x$ candies during the first day, $2x$ candies during the second day, $4x$ candies during the third day, $\\dots$, $2^{k-1} x$ candies during the $k$-th day. But there is an issue: Vova remembers neither $x$ nor $k$ but he is sure that $x$ and $k$ are positive integers and $k > 1$.\n\nVova will be satisfied if you tell him any positive integer $x$ so there is an integer $k>1$ that $x + 2x + 4x + \\dots + 2^{k-1} x = n$. It is guaranteed that at least one solution exists. Note that $k > 1$.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains one integer $n$ ($3 \\le n \\le 10^9$) \u2014 the number of candy wrappers Vova found. It is guaranteed that there is some positive integer $x$ and integer $k>1$ that $x + 2x + 4x + \\dots + 2^{k-1} x = n$.\n\n\n-----Output-----\n\nPrint one integer \u2014 any positive integer value of $x$ so there is an integer $k>1$ that $x + 2x + 4x + \\dots + 2^{k-1} x = n$.\n\n\n-----Example-----\nInput\n7\n3\n6\n7\n21\n28\n999999999\n999999984\n\nOutput\n1\n2\n1\n7\n4\n333333333\n333333328\n\n\n\n-----Note-----\n\nIn the first test case of the example, one of the possible answers is $x=1, k=2$. Then $1 \\cdot 1 + 2 \\cdot 1$ equals $n=3$.\n\nIn the second test case of the example, one of the possible answers is $x=2, k=2$. Then $1 \\cdot 2 + 2 \\cdot 2$ equals $n=6$.\n\nIn the third test case of the example, one of the possible answers is $x=1, k=3$. Then $1 \\cdot 1 + 2 \\cdot 1 + 4 \\cdot 1$ equals $n=7$.\n\nIn the fourth test case of the example, one of the possible answers is $x=7, k=2$. Then $1 \\cdot 7 + 2 \\cdot 7$ equals $n=21$.\n\nIn the fifth test case of the example, one of the possible answers is $x=4, k=3$. Then $1 \\cdot 4 + 2 \\cdot 4 + 4 \\cdot 4$ equals $n=28$."} +{"id": "03356", "text": "Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm: if the last digit of the number is non-zero, she decreases the number by one; if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit). \n\nYou are given an integer number $n$. Tanya will subtract one from it $k$ times. Your task is to print the result after all $k$ subtractions.\n\nIt is guaranteed that the result will be positive integer number.\n\n\n-----Input-----\n\nThe first line of the input contains two integer numbers $n$ and $k$ ($2 \\le n \\le 10^9$, $1 \\le k \\le 50$) \u2014 the number from which Tanya will subtract and the number of subtractions correspondingly.\n\n\n-----Output-----\n\nPrint one integer number \u2014 the result of the decreasing $n$ by one $k$ times.\n\nIt is guaranteed that the result will be positive integer number. \n\n\n-----Examples-----\nInput\n512 4\n\nOutput\n50\n\nInput\n1000000000 9\n\nOutput\n1\n\n\n\n-----Note-----\n\nThe first example corresponds to the following sequence: $512 \\rightarrow 511 \\rightarrow 510 \\rightarrow 51 \\rightarrow 50$."} +{"id": "03357", "text": "There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.\n\nA balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \\dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \\le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \\le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.\n\nYour task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of people.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the height of the $i$-th person.\n\n\n-----Output-----\n\nIn the first line of the output print $k$ \u2014 the number of people in the maximum balanced circle.\n\nIn the second line print $k$ integers $res_1, res_2, \\dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \\le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \\le 1$ should be also satisfied.\n\n\n-----Examples-----\nInput\n7\n4 3 5 1 2 2 1\n\nOutput\n5\n2 1 1 2 3\n\nInput\n5\n3 7 5 1 5\n\nOutput\n2\n5 5 \n\nInput\n3\n5 1 4\n\nOutput\n2\n4 5 \n\nInput\n7\n2 2 3 2 1 2 2\n\nOutput\n7\n1 2 2 2 2 3 2"} +{"id": "03358", "text": "Ivan has $n$ songs on his phone. The size of the $i$-th song is $a_i$ bytes. Ivan also has a flash drive which can hold at most $m$ bytes in total. Initially, his flash drive is empty.\n\nIvan wants to copy all $n$ songs to the flash drive. He can compress the songs. If he compresses the $i$-th song, the size of the $i$-th song reduces from $a_i$ to $b_i$ bytes ($b_i < a_i$).\n\nIvan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $m$. He can compress any subset of the songs (not necessarily contiguous).\n\nIvan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $m$).\n\nIf it is impossible to copy all the songs (even if Ivan compresses all the songs), print \"-1\". Otherwise print the minimum number of songs Ivan needs to compress.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n \\le 10^5, 1 \\le m \\le 10^9$) \u2014 the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.\n\nThe next $n$ lines contain two integers each: the $i$-th line contains two integers $a_i$ and $b_i$ ($1 \\le a_i, b_i \\le 10^9$, $a_i > b_i$) \u2014 the initial size of the $i$-th song and the size of the $i$-th song after compression.\n\n\n-----Output-----\n\nIf it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print \"-1\". Otherwise print the minimum number of the songs to compress.\n\n\n-----Examples-----\nInput\n4 21\n10 8\n7 4\n3 1\n5 4\n\nOutput\n2\n\nInput\n4 16\n10 8\n7 4\n3 1\n5 4\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $8 + 7 + 1 + 5 = 21 \\le 21$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $8 + 4 + 3 + 5 = 20 \\le 21$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $10 + 4 + 3 + 5 = 22 > 21$).\n\nIn the second example even if Ivan compresses all the songs the sum of sizes will be equal $8 + 4 + 1 + 4 = 17 > 16$."} +{"id": "03359", "text": "You are given three positive integers $n$, $a$ and $b$. You have to construct a string $s$ of length $n$ consisting of lowercase Latin letters such that each substring of length $a$ has exactly $b$ distinct letters. It is guaranteed that the answer exists.\n\nYou have to answer $t$ independent test cases.\n\nRecall that the substring $s[l \\dots r]$ is the string $s_l, s_{l+1}, \\dots, s_{r}$ and its length is $r - l + 1$. In this problem you are only interested in substrings of length $a$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2000$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of a test case contains three space-separated integers $n$, $a$ and $b$ ($1 \\le a \\le n \\le 2000, 1 \\le b \\le \\min(26, a)$), where $n$ is the length of the required string, $a$ is the length of a substring and $b$ is the required number of distinct letters in each substring of length $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\\sum n \\le 2000$).\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 such a string $s$ of length $n$ consisting of lowercase Latin letters that each substring of length $a$ has exactly $b$ distinct letters. If there are multiple valid answers, print any of them. It is guaranteed that the answer exists.\n\n\n-----Example-----\nInput\n4\n7 5 3\n6 1 1\n6 6 1\n5 2 2\n\nOutput\ntleelte\nqwerty\nvvvvvv\nabcde\n\n\n\n-----Note-----\n\nIn the first test case of the example, consider all the substrings of length $5$: \"tleel\": it contains $3$ distinct (unique) letters, \"leelt\": it contains $3$ distinct (unique) letters, \"eelte\": it contains $3$ distinct (unique) letters."} +{"id": "03360", "text": "Takahashi loves takoyaki - a ball-shaped snack.\nWith a takoyaki machine, he can make at most X pieces of takoyaki at a time, taking T minutes regardless of the number of pieces to make.\nHow long does it take to make N takoyaki?\n\n-----Constraints-----\n - 1 \\leq N,X,T \\leq 1000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN X T\n\n-----Output-----\nPrint an integer representing the minimum number of minutes needed to make N pieces of takoyaki.\n\n-----Sample Input-----\n20 12 6\n\n-----Sample Output-----\n12\n\nHe can make 12 pieces of takoyaki in the first 6 minutes and 8 more in the next 6 minutes, so he can make 20 in a total of 12 minutes.\nNote that being able to make 12 in 6 minutes does not mean he can make 2 in 1 minute."} +{"id": "03361", "text": "There are N students in a school.\nWe will divide these students into some groups, and in each group they will discuss some themes.\nYou think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.\nDivide the students so that the number of groups consisting of three or more students is maximized.\n\n-----Constraints-----\n - 1 \\leq N \\leq 1000\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nIf you can form at most x groups consisting of three or more students, print x.\n\n-----Sample Input-----\n8\n\n-----Sample Output-----\n2\n\nFor example, you can form a group of three students and another of five students."} +{"id": "03362", "text": "We have A apples and P pieces of apple.\nWe can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.\nFind the maximum number of apple pies we can make with what we have now.\n\n-----Constraints-----\n - All values in input are integers.\n - 0 \\leq A, P \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA P\n\n-----Output-----\nPrint the maximum number of apple pies we can make with what we have.\n\n-----Sample Input-----\n1 3\n\n-----Sample Output-----\n3\n\nWe can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple."} +{"id": "03363", "text": "You are given positive integers A and B.\nIf A is a divisor of B, print A + B; otherwise, print B - A.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq A \\leq B \\leq 20\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nIf A is a divisor of B, print A + B; otherwise, print B - A.\n\n-----Sample Input-----\n4 12\n\n-----Sample Output-----\n16\n\nAs 4 is a divisor of 12, 4 + 12 = 16 should be printed."} +{"id": "03364", "text": "Takahashi wants to be a member of some web service.\nHe tried to register himself with the ID S, which turned out to be already used by another user.\nThus, he decides to register using a string obtained by appending one character at the end of S as his ID.\nHe is now trying to register with the ID T. Determine whether this string satisfies the property above.\n\n-----Constraints-----\n - S and T are strings consisting of lowercase English letters.\n - 1 \\leq |S| \\leq 10\n - |T| = |S| + 1\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\nT\n\n-----Output-----\nIf T satisfies the property in Problem Statement, print Yes; otherwise, print No.\n\n-----Sample Input-----\nchokudai\nchokudaiz\n\n-----Sample Output-----\nYes\n\nchokudaiz can be obtained by appending z at the end of chokudai."} +{"id": "03365", "text": "We have two distinct integers A and B.\nPrint the integer K such that |A - K| = |B - K|.\nIf such an integer does not exist, print IMPOSSIBLE instead.\n\n-----Constraints-----\n - All values in input are integers.\n - 0 \\leq A,\\ B \\leq 10^9\n - A and B are distinct.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the integer K satisfying the condition.\nIf such an integer does not exist, print IMPOSSIBLE instead.\n\n-----Sample Input-----\n2 16\n\n-----Sample Output-----\n9\n\n|2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition."} +{"id": "03366", "text": "Given is a three-digit integer N. Does N contain the digit 7?\nIf so, print Yes; otherwise, print No.\n\n-----Constraints-----\n - 100 \\leq N \\leq 999\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nIf N contains the digit 7, print Yes; otherwise, print No.\n\n-----Sample Input-----\n117\n\n-----Sample Output-----\nYes\n\n117 contains 7 as its last digit."} +{"id": "03367", "text": "Let S(n) denote the sum of the digits in the decimal notation of n.\nFor example, S(101) = 1 + 0 + 1 = 2.\nGiven an integer N, determine if S(N) divides N.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nIf S(N) divides N, print Yes; if it does not, print No.\n\n-----Sample Input-----\n12\n\n-----Sample Output-----\nYes\n\nIn this input, N=12.\nAs S(12) = 1 + 2 = 3, S(N) divides N."} +{"id": "03368", "text": "There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.\n(Assume that the positive x-axis points right, and the positive y-axis points up.)\nTakahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).\nGiven x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.\n\n-----Constraints-----\n - |x_1|,|y_1|,|x_2|,|y_2| \\leq 100\n - (x_1,y_1) \u2260 (x_2,y_2)\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nx_1 y_1 x_2 y_2\n\n-----Output-----\nPrint x_3,y_3,x_4 and y_4 as integers, in this order.\n\n-----Sample Input-----\n0 0 0 1\n\n-----Sample Output-----\n-1 1 -1 0\n\n(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.\nNote that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order."} +{"id": "03369", "text": "We have A balls with the string S written on each of them and B balls with the string T written on each of them.\n\nFrom these balls, Takahashi chooses one with the string U written on it and throws it away.\n\nFind the number of balls with the string S and balls with the string T that we have now.\n\n-----Constraints-----\n - S, T, and U are strings consisting of lowercase English letters.\n - The lengths of S and T are each between 1 and 10 (inclusive).\n - S \\not= T\n - S=U or T=U.\n - 1 \\leq A,B \\leq 10\n - A and B are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS T\nA B\nU\n\n-----Output-----\nPrint the answer, with space in between.\n\n-----Sample Input-----\nred blue\n3 4\nred\n\n-----Sample Output-----\n2 4\n\nTakahashi chose a ball with red written on it and threw it away.\nNow we have two balls with the string S and four balls with the string T."} +{"id": "03370", "text": "Given are a positive integer N and a string S of length N consisting of lowercase English letters.\nDetermine whether the string is a concatenation of two copies of some string.\nThat is, determine whether there is a string T such that S = T + T.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - S consists of lowercase English letters.\n - |S| = N\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS\n\n-----Output-----\nIf S is a concatenation of two copies of some string, print Yes; otherwise, print No.\n\n-----Sample Input-----\n6\nabcabc\n\n-----Sample Output-----\nYes\n\nLet T = abc, and S = T + T."} +{"id": "03371", "text": "There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).\nYou are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.\n\n-----Constraints-----\n - 1 \\leq W,H \\leq 10^9\n - 0\\leq x\\leq W\n - 0\\leq y\\leq H\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nW H x y\n\n-----Output-----\nPrint the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.\nThe area printed will be judged correct when its absolute or relative error is at most 10^{-9}.\n\n-----Sample Input-----\n2 3 1 2\n\n-----Sample Output-----\n3.000000 0\n\nThe line x=1 gives the optimal cut, and no other line does."} +{"id": "03372", "text": "In Japan, people make offerings called hina arare, colorful crackers, on March 3.\nWe have a bag that contains N hina arare. (From here, we call them arare.)\nIt is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.\nWe have taken out the arare in the bag one by one, and the color of the i-th arare was S_i, where colors are represented as follows - pink: P, white: W, green: G, yellow: Y.\nIf the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - S_i is P, W, G or Y.\n - There always exist i, j and k such that S_i=P, S_j=W and S_k=G.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS_1 S_2 ... S_N\n\n-----Output-----\nIf the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.\n\n-----Sample Input-----\n6\nG W Y P Y W\n\n-----Sample Output-----\nFour\n\nThe bag contained arare in four colors, so you should print Four."} +{"id": "03373", "text": "You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consists of two phases. During the first phase you make your move and during the second phase Slavik makes his move.\n\nThere are $n$ doors, the $i$-th door initially has durability equal to $a_i$.\n\nDuring your move you can try to break one of the doors. If you choose door $i$ and its current durability is $b_i$ then you reduce its durability to $max(0, b_i - x)$ (the value $x$ is given).\n\nDuring Slavik's move he tries to repair one of the doors. If he chooses door $i$ and its current durability is $b_i$ then he increases its durability to $b_i + y$ (the value $y$ is given). Slavik cannot repair doors with current durability equal to $0$.\n\nThe game lasts $10^{100}$ turns. If some player cannot make his move then he has to skip it.\n\nYour goal is to maximize the number of doors with durability equal to $0$ at the end of the game. You can assume that Slavik wants to minimize the number of such doors. What is the number of such doors in the end if you both play optimally?\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n$, $x$ and $y$ ($1 \\le n \\le 100$, $1 \\le x, y \\le 10^5$) \u2014 the number of doors, value $x$ and value $y$, respectively.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^5$), where $a_i$ is the initial durability of the $i$-th door.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of doors with durability equal to $0$ at the end of the game, if you and Slavik both play optimally.\n\n\n-----Examples-----\nInput\n6 3 2\n2 3 1 3 4 2\n\nOutput\n6\n\nInput\n5 3 3\n1 2 4 2 3\n\nOutput\n2\n\nInput\n5 5 6\n1 2 6 10 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nClarifications about the optimal strategy will be ignored."} +{"id": "03374", "text": "The next lecture in a high school requires two topics to be discussed. The $i$-th topic is interesting by $a_i$ units for the teacher and by $b_i$ units for the students.\n\nThe pair of topics $i$ and $j$ ($i < j$) is called good if $a_i + a_j > b_i + b_j$ (i.e. it is more interesting for the teacher).\n\nYour task is to find the number of good pairs of topics.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of topics.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the interestingness of the $i$-th topic for the teacher.\n\nThe third line of the input contains $n$ integers $b_1, b_2, \\dots, b_n$ ($1 \\le b_i \\le 10^9$), where $b_i$ is the interestingness of the $i$-th topic for the students.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of good pairs of topic.\n\n\n-----Examples-----\nInput\n5\n4 8 2 6 2\n4 5 4 1 3\n\nOutput\n7\n\nInput\n4\n1 3 2 4\n1 3 2 4\n\nOutput\n0"} +{"id": "03375", "text": "Mishka got an integer array $a$ of length $n$ as a birthday present (what a surprise!).\n\nMishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it \"Mishka's Adjacent Replacements Algorithm\". This algorithm can be represented as a sequence of steps: Replace each occurrence of $1$ in the array $a$ with $2$; Replace each occurrence of $2$ in the array $a$ with $1$; Replace each occurrence of $3$ in the array $a$ with $4$; Replace each occurrence of $4$ in the array $a$ with $3$; Replace each occurrence of $5$ in the array $a$ with $6$; Replace each occurrence of $6$ in the array $a$ with $5$; $\\dots$ Replace each occurrence of $10^9 - 1$ in the array $a$ with $10^9$; Replace each occurrence of $10^9$ in the array $a$ with $10^9 - 1$. \n\nNote that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($2i - 1, 2i$) for each $i \\in\\{1, 2, \\ldots, 5 \\cdot 10^8\\}$ as described above.\n\nFor example, for the array $a = [1, 2, 4, 5, 10]$, the following sequence of arrays represents the algorithm: \n\n$[1, 2, 4, 5, 10]$ $\\rightarrow$ (replace all occurrences of $1$ with $2$) $\\rightarrow$ $[2, 2, 4, 5, 10]$ $\\rightarrow$ (replace all occurrences of $2$ with $1$) $\\rightarrow$ $[1, 1, 4, 5, 10]$ $\\rightarrow$ (replace all occurrences of $3$ with $4$) $\\rightarrow$ $[1, 1, 4, 5, 10]$ $\\rightarrow$ (replace all occurrences of $4$ with $3$) $\\rightarrow$ $[1, 1, 3, 5, 10]$ $\\rightarrow$ (replace all occurrences of $5$ with $6$) $\\rightarrow$ $[1, 1, 3, 6, 10]$ $\\rightarrow$ (replace all occurrences of $6$ with $5$) $\\rightarrow$ $[1, 1, 3, 5, 10]$ $\\rightarrow$ $\\dots$ $\\rightarrow$ $[1, 1, 3, 5, 10]$ $\\rightarrow$ (replace all occurrences of $10$ with $9$) $\\rightarrow$ $[1, 1, 3, 5, 9]$. The later steps of the algorithm do not change the array.\n\nMishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.\n\n\n-----Input-----\n\nThe first line of the input contains one integer number $n$ ($1 \\le n \\le 1000$) \u2014 the number of elements in Mishka's birthday present (surprisingly, an array).\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) \u2014 the elements of the array.\n\n\n-----Output-----\n\nPrint $n$ integers \u2014 $b_1, b_2, \\dots, b_n$, where $b_i$ is the final value of the $i$-th element of the array after applying \"Mishka's Adjacent Replacements Algorithm\" to the array $a$. Note that you cannot change the order of elements in the array.\n\n\n-----Examples-----\nInput\n5\n1 2 4 5 10\n\nOutput\n1 1 3 5 9\n\nInput\n10\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000\n\nOutput\n9999 9 50605065 1 5 89 5 999999999 60506055 999999999\n\n\n\n-----Note-----\n\nThe first example is described in the problem statement."} +{"id": "03376", "text": "You are given an undirected graph consisting of $n$ vertices and $m$ edges. Your task is to find the number of connected components which are cycles.\n\nHere are some definitions of graph theory.\n\nAn undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex $a$ is connected with a vertex $b$, a vertex $b$ is also connected with a vertex $a$). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.\n\nTwo vertices $u$ and $v$ belong to the same connected component if and only if there is at least one path along edges connecting $u$ and $v$.\n\nA connected component is a cycle if and only if its vertices can be reordered in such a way that: the first vertex is connected with the second vertex by an edge, the second vertex is connected with the third vertex by an edge, ... the last vertex is connected with the first vertex by an edge, all the described edges of a cycle are distinct. \n\nA cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices. [Image] There are $6$ connected components, $2$ of them are cycles: $[7, 10, 16]$ and $[5, 11, 9, 15]$. \n\n\n-----Input-----\n\nThe first line contains two integer numbers $n$ and $m$ ($1 \\le n \\le 2 \\cdot 10^5$, $0 \\le m \\le 2 \\cdot 10^5$) \u2014 number of vertices and edges.\n\nThe following $m$ lines contains edges: edge $i$ is given as a pair of vertices $v_i$, $u_i$ ($1 \\le v_i, u_i \\le n$, $u_i \\ne v_i$). There is no multiple edges in the given graph, i.e. for each pair ($v_i, u_i$) there no other pairs ($v_i, u_i$) and ($u_i, v_i$) in the list of edges.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of connected components which are also cycles.\n\n\n-----Examples-----\nInput\n5 4\n1 2\n3 4\n5 4\n3 5\n\nOutput\n1\n\nInput\n17 15\n1 8\n1 12\n5 11\n11 9\n9 15\n15 5\n4 13\n3 13\n4 3\n10 16\n7 10\n16 7\n14 3\n14 4\n17 6\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example only component $[3, 4, 5]$ is also a cycle.\n\nThe illustration above corresponds to the second example."} +{"id": "03377", "text": "You are given an undirected tree of $n$ vertices. \n\nSome vertices are colored blue, some are colored red and some are uncolored. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.\n\nYou choose an edge and remove it from the tree. Tree falls apart into two connected components. Let's call an edge nice if neither of the resulting components contain vertices of both red and blue colors.\n\nHow many nice edges are there in the given tree?\n\n\n-----Input-----\n\nThe first line contains a single integer $n$ ($2 \\le n \\le 3 \\cdot 10^5$) \u2014 the number of vertices in the tree.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 2$) \u2014 the colors of the vertices. $a_i = 1$ means that vertex $i$ is colored red, $a_i = 2$ means that vertex $i$ is colored blue and $a_i = 0$ means that vertex $i$ is uncolored.\n\nThe $i$-th of the next $n - 1$ lines contains two integers $v_i$ and $u_i$ ($1 \\le v_i, u_i \\le n$, $v_i \\ne u_i$) \u2014 the edges of the tree. It is guaranteed that the given edges form a tree. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the number of nice edges in the given tree.\n\n\n-----Examples-----\nInput\n5\n2 0 0 1 2\n1 2\n2 3\n2 4\n2 5\n\nOutput\n1\n\nInput\n5\n1 0 0 0 2\n1 2\n2 3\n3 4\n4 5\n\nOutput\n4\n\nInput\n3\n1 1 2\n2 3\n1 3\n\nOutput\n0\n\n\n\n-----Note-----\n\nHere is the tree from the first example: [Image] \n\nThe only nice edge is edge $(2, 4)$. Removing it makes the tree fall apart into components $\\{4\\}$ and $\\{1, 2, 3, 5\\}$. The first component only includes a red vertex and the second component includes blue vertices and uncolored vertices.\n\nHere is the tree from the second example: [Image] \n\nEvery edge is nice in it.\n\nHere is the tree from the third example: [Image] \n\nEdge $(1, 3)$ splits the into components $\\{1\\}$ and $\\{3, 2\\}$, the latter one includes both red and blue vertex, thus the edge isn't nice. Edge $(2, 3)$ splits the into components $\\{1, 3\\}$ and $\\{2\\}$, the former one includes both red and blue vertex, thus the edge also isn't nice. So the answer is 0."} +{"id": "03378", "text": "You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$.\n\nLet's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$\"az\" and $t=$\"bf\" the list will be [\"az\", \"ba\", \"bb\", \"bc\", \"bd\", \"be\", \"bf\"].\n\nYour task is to print the median (the middle element) of this list. For the example above this will be \"bc\".\n\nIt is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $k$ ($1 \\le k \\le 2 \\cdot 10^5$) \u2014 the length of strings.\n\nThe second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters.\n\nThe third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters.\n\nIt is guaranteed that $s$ is lexicographically less than $t$.\n\nIt is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$.\n\n\n-----Output-----\n\nPrint one string consisting exactly of $k$ lowercase Latin letters \u2014 the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$.\n\n\n-----Examples-----\nInput\n2\naz\nbf\n\nOutput\nbc\n\nInput\n5\nafogk\nasdji\n\nOutput\nalvuw\n\nInput\n6\nnijfvj\ntvqhwp\n\nOutput\nqoztvz"} +{"id": "03379", "text": "There are $n$ students in a school class, the rating of the $i$-th student on Codehorses is $a_i$. You have to form a team consisting of $k$ students ($1 \\le k \\le n$) such that the ratings of all team members are distinct.\n\nIf it is impossible to form a suitable team, print \"NO\" (without quotes). Otherwise print \"YES\", and then print $k$ distinct numbers which should be the indices of students in the team you form. If there are multiple answers, print any of them.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 100$) \u2014 the number of students and the size of the team you have to form.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$), where $a_i$ is the rating of $i$-th student.\n\n\n-----Output-----\n\nIf it is impossible to form a suitable team, print \"NO\" (without quotes). Otherwise print \"YES\", and then print $k$ distinct integers from $1$ to $n$ which should be the indices of students in the team you form. All the ratings of the students in the team should be distinct. You may print the indices in any order. If there are multiple answers, print any of them.\n\nAssume that the students are numbered from $1$ to $n$.\n\n\n-----Examples-----\nInput\n5 3\n15 13 15 15 12\n\nOutput\nYES\n1 2 5 \n\nInput\n5 4\n15 13 15 15 12\n\nOutput\nNO\n\nInput\n4 4\n20 10 40 30\n\nOutput\nYES\n1 2 3 4 \n\n\n\n-----Note-----\n\nAll possible answers for the first example: {1 2 5} {2 3 5} {2 4 5} \n\nNote that the order does not matter."} +{"id": "03380", "text": "Two integer sequences existed initially, one of them was strictly increasing, and another one \u2014 strictly decreasing.\n\nStrictly increasing sequence is a sequence of integers $[x_1 < x_2 < \\dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \\dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.\n\nElements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $[1, 3, 4]$ and $[10, 4, 2]$ can produce the following resulting sequences: $[10, \\textbf{1}, \\textbf{3}, 4, 2, \\textbf{4}]$, $[\\textbf{1}, \\textbf{3}, \\textbf{4}, 10, 4, 2]$. The following sequence cannot be the result of these insertions: $[\\textbf{1}, 10, \\textbf{4}, 4, \\textbf{3}, 2]$ because the order of elements in the increasing sequence was changed.\n\nLet the obtained sequence be $a$. This sequence $a$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one \u2014 strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.\n\nIf there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print \"NO\".\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nIf there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print \"NO\" in the first line.\n\nOtherwise print \"YES\" in the first line. In the second line, print a sequence of $n$ integers $res_1, res_2, \\dots, res_n$, where $res_i$ should be either $0$ or $1$ for each $i$ from $1$ to $n$. The $i$-th element of this sequence should be $0$ if the $i$-th element of $a$ belongs to the increasing sequence, and $1$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.\n\n\n-----Examples-----\nInput\n9\n5 1 3 6 8 2 9 0 10\n\nOutput\nYES\n1 0 0 0 0 1 0 1 0 \n\nInput\n5\n1 2 4 0 2\n\nOutput\nNO"} +{"id": "03381", "text": "Vova plans to go to the conference by train. Initially, the train is at the point $1$ and the destination point of the path is the point $L$. The speed of the train is $1$ length unit per minute (i.e. at the first minute the train is at the point $1$, at the second minute \u2014 at the point $2$ and so on).\n\nThere are lanterns on the path. They are placed at the points with coordinates divisible by $v$ (i.e. the first lantern is at the point $v$, the second is at the point $2v$ and so on).\n\nThere is also exactly one standing train which occupies all the points from $l$ to $r$ inclusive.\n\nVova can see the lantern at the point $p$ if $p$ is divisible by $v$ and there is no standing train at this position ($p \\not\\in [l; r]$). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.\n\nYour problem is to say the number of lanterns Vova will see during the path. Vova plans to go to $t$ different conferences, so you should answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of queries.\n\nThen $t$ lines follow. The $i$-th line contains four integers $L_i, v_i, l_i, r_i$ ($1 \\le L, v \\le 10^9$, $1 \\le l \\le r \\le L$) \u2014 destination point of the $i$-th path, the period of the lantern appearance and the segment occupied by the standing train.\n\n\n-----Output-----\n\nPrint $t$ lines. The $i$-th line should contain one integer \u2014 the answer for the $i$-th query.\n\n\n-----Example-----\nInput\n4\n10 2 3 7\n100 51 51 51\n1234 1 100 199\n1000000000 1 1 1000000000\n\nOutput\n3\n0\n1134\n0\n\n\n\n-----Note-----\n\nFor the first example query, the answer is $3$. There are lanterns at positions $2$, $4$, $6$, $8$ and $10$, but Vova didn't see the lanterns at positions $4$ and $6$ because of the standing train.\n\nFor the second example query, the answer is $0$ because the only lantern is at the point $51$ and there is also a standing train at this point.\n\nFor the third example query, the answer is $1134$ because there are $1234$ lanterns, but Vova didn't see the lanterns from the position $100$ to the position $199$ inclusive.\n\nFor the fourth example query, the answer is $0$ because the standing train covers the whole path."} +{"id": "03382", "text": "One day, $n$ people ($n$ is an even number) met on a plaza and made two round dances, each round dance consists of exactly $\\frac{n}{2}$ people. Your task is to find the number of ways $n$ people can make two round dances if each round dance consists of exactly $\\frac{n}{2}$ people. Each person should belong to exactly one of these two round dances.\n\nRound dance is a dance circle consisting of $1$ or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances $[1, 3, 4, 2]$, $[4, 2, 1, 3]$ and $[2, 1, 3, 4]$ are indistinguishable.\n\nFor example, if $n=2$ then the number of ways is $1$: one round dance consists of the first person and the second one of the second person.\n\nFor example, if $n=4$ then the number of ways is $3$. Possible options: one round dance \u2014 $[1,2]$, another \u2014 $[3,4]$; one round dance \u2014 $[2,4]$, another \u2014 $[3,1]$; one round dance \u2014 $[4,1]$, another \u2014 $[3,2]$. \n\nYour task is to find the number of ways $n$ people can make two round dances if each round dance consists of exactly $\\frac{n}{2}$ people.\n\n\n-----Input-----\n\nThe input contains one integer $n$ ($2 \\le n \\le 20$), $n$ is an even number.\n\n\n-----Output-----\n\nPrint one integer \u2014 the number of ways to make two round dances. It is guaranteed that the answer fits in the $64$-bit integer data type.\n\n\n-----Examples-----\nInput\n2\n\nOutput\n1\n\nInput\n4\n\nOutput\n3\n\nInput\n8\n\nOutput\n1260\n\nInput\n20\n\nOutput\n12164510040883200"} +{"id": "03383", "text": "You are given a string $s$ consisting of $n$ lowercase Latin letters. Polycarp wants to remove exactly $k$ characters ($k \\le n$) from the string $s$. Polycarp uses the following algorithm $k$ times:\n\n if there is at least one letter 'a', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; if there is at least one letter 'b', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; ... remove the leftmost occurrence of the letter 'z' and stop the algorithm. \n\nThis algorithm removes a single letter from the string. Polycarp performs this algorithm exactly $k$ times, thus removing exactly $k$ characters.\n\nHelp Polycarp find the resulting string.\n\n\n-----Input-----\n\nThe first line of input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 4 \\cdot 10^5$) \u2014 the length of the string and the number of letters Polycarp will remove.\n\nThe second line contains the string $s$ consisting of $n$ lowercase Latin letters.\n\n\n-----Output-----\n\nPrint the string that will be obtained from $s$ after Polycarp removes exactly $k$ letters using the above algorithm $k$ times.\n\nIf the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break).\n\n\n-----Examples-----\nInput\n15 3\ncccaabababaccbc\n\nOutput\ncccbbabaccbc\n\nInput\n15 9\ncccaabababaccbc\n\nOutput\ncccccc\n\nInput\n1 1\nu\n\nOutput"} +{"id": "03384", "text": "The only difference between easy and hard versions is constraints.\n\nIvan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions \u2014 and he won't start playing until he gets all of them.\n\nEach day (during the morning) Ivan earns exactly one burle.\n\nThere are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening).\n\nIvan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles.\n\nThere are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day.\n\nIvan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$) \u2014 the number of types of microtransactions and the number of special offers in the game shop.\n\nThe second line of the input contains $n$ integers $k_1, k_2, \\dots, k_n$ ($0 \\le k_i \\le 2 \\cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \\cdot 10^5$.\n\nThe next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \\le d_j \\le 2 \\cdot 10^5, 1 \\le t_j \\le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum day when Ivan can order all microtransactions he wants and actually start playing.\n\n\n-----Examples-----\nInput\n5 6\n1 2 0 2 0\n2 4\n3 3\n1 5\n1 2\n1 5\n2 3\n\nOutput\n8\n\nInput\n5 3\n4 2 1 3 2\n3 5\n4 2\n2 5\n\nOutput\n20"} +{"id": "03385", "text": "A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed).\n\nLet's consider empty cells are denoted by '.', then the following figures are stars:\n\n [Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$. \n\nYou are given a rectangular grid of size $n \\times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \\cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.\n\nIn this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \\cdot m$ stars.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($3 \\le n, m \\le 1000$) \u2014 the sizes of the given grid.\n\nThe next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.\n\n\n-----Output-----\n\nIf it is impossible to draw the given grid using stars only, print \"-1\".\n\nOtherwise in the first line print one integer $k$ ($0 \\le k \\le n \\cdot m$) \u2014 the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each \u2014 $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid.\n\n\n-----Examples-----\nInput\n6 8\n....*...\n...**...\n..*****.\n...**...\n....*...\n........\n\nOutput\n3\n3 4 1\n3 5 2\n3 5 1\n\nInput\n5 5\n.*...\n****.\n.****\n..**.\n.....\n\nOutput\n3\n2 2 1\n3 3 1\n3 4 1\n\nInput\n5 5\n.*...\n***..\n.*...\n.*...\n.....\n\nOutput\n-1\n\nInput\n3 3\n*.*\n.*.\n*.*\n\nOutput\n-1\n\n\n\n-----Note-----\n\nIn the first example the output 2\n\n3 4 1\n\n3 5 2\n\n\n\nis also correct."} +{"id": "03386", "text": "You are given a three-digit positive integer N.\n\nDetermine whether N is a palindromic number.\n\nHere, a palindromic number is an integer that reads the same backward as forward in decimal notation.\n\n-----Constraints-----\n - 100\u2264N\u2264999\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format: \nN\n\n-----Output-----\nIf N is a palindromic number, print Yes; otherwise, print No.\n\n-----Sample Input-----\n575\n\n-----Sample Output-----\nYes\n\nN=575 is also 575 when read backward, so it is a palindromic number. You should print Yes."} +{"id": "03387", "text": "Alice and Bob are playing One Card Poker.\n\nOne Card Poker is a two-player game using playing cards. \nEach card in this game shows an integer between 1 and 13, inclusive.\n\nThe strength of a card is determined by the number written on it, as follows: \nWeak 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < 1 Strong \nOne Card Poker is played as follows: \n - Each player picks one card from the deck. The chosen card becomes the player's hand.\n - The players reveal their hands to each other. The player with the stronger card wins the game.\nIf their cards are equally strong, the game is drawn. \nYou are watching Alice and Bob playing the game, and can see their hands.\n\nThe number written on Alice's card is A, and the number written on Bob's card is B.\n\nWrite a program to determine the outcome of the game. \n\n-----Constraints-----\n - 1\u2266A\u226613 \n - 1\u2266B\u226613 \n - A and B are integers.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint Alice if Alice will win. Print Bob if Bob will win. Print Draw if the game will be drawn.\n\n-----Sample Input-----\n8 6\n\n-----Sample Output-----\nAlice\n\n8 is written on Alice's card, and 6 is written on Bob's card.\nAlice has the stronger card, and thus the output should be Alice."} +{"id": "03388", "text": "As a New Year's gift, Dolphin received a string s of length 19.\n\nThe string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters].\n\nDolphin wants to convert the comma-separated string s into a space-separated string.\n\nWrite a program to perform the conversion for him. \n\n-----Constraints-----\n - The length of s is 19.\n - The sixth and fourteenth characters in s are ,.\n - The other characters in s are lowercase English letters.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\ns\n\n-----Output-----\nPrint the string after the conversion.\n\n-----Sample Input-----\nhappy,newyear,enjoy\n\n-----Sample Output-----\nhappy newyear enjoy\n\nReplace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy."} +{"id": "03389", "text": "There are N students and M checkpoints on the xy-plane.\n\nThe coordinates of the i-th student (1 \\leq i \\leq N) is (a_i,b_i), and the coordinates of the checkpoint numbered j (1 \\leq j \\leq M) is (c_j,d_j).\n\nWhen the teacher gives a signal, each student has to go to the nearest checkpoint measured in Manhattan distance. \n\nThe Manhattan distance between two points (x_1,y_1) and (x_2,y_2) is |x_1-x_2|+|y_1-y_2|.\n\nHere, |x| denotes the absolute value of x.\n\nIf there are multiple nearest checkpoints for a student, he/she will select the checkpoint with the smallest index.\n\nWhich checkpoint will each student go to?\n\n-----Constraints-----\n - 1 \\leq N,M \\leq 50\n - -10^8 \\leq a_i,b_i,c_j,d_j \\leq 10^8\n - All input values are integers.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN M\na_1 b_1\n: \na_N b_N\nc_1 d_1\n: \nc_M d_M\n\n-----Output-----\nPrint N lines.\n\nThe i-th line (1 \\leq i \\leq N) should contain the index of the checkpoint for the i-th student to go.\n\n-----Sample Input-----\n2 2\n2 0\n0 0\n-1 0\n1 0\n\n-----Sample Output-----\n2\n1\n\nThe Manhattan distance between the first student and each checkpoint is:\n - For checkpoint 1: |2-(-1)|+|0-0|=3\n - For checkpoint 2: |2-1|+|0-0|=1\nThe nearest checkpoint is checkpoint 2. Thus, the first line in the output should contain 2.\nThe Manhattan distance between the second student and each checkpoint is:\n - For checkpoint 1: |0-(-1)|+|0-0|=1\n - For checkpoint 2: |0-1|+|0-0|=1\nWhen there are multiple nearest checkpoints, the student will go to the checkpoint with the smallest index. Thus, the second line in the output should contain 1."} +{"id": "03390", "text": "Given an integer a as input, print the value a + a^2 + a^3.\n\n-----Constraints-----\n - 1 \\leq a \\leq 10\n - a is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\na\n\n-----Output-----\nPrint the value a + a^2 + a^3 as an integer.\n\n-----Sample Input-----\n2\n\n-----Sample Output-----\n14\n\nWhen a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.\nPrint the answer as an input. Outputs such as 14.0 will be judged as incorrect."} +{"id": "03391", "text": "You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.\n\nA pixel is the smallest element of an image, and in this problem it is a square of size 1\u00d71.\n\nAlso, the given images are binary images, and the color of each pixel is either white or black. \nIn the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.\n\nThe image A is given as N strings A_1,...,A_N.\n\nThe j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1\u2266i,j\u2266N).\n\nSimilarly, the template image B is given as M strings B_1,...,B_M.\n\nThe j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1\u2266i,j\u2266M). \nDetermine whether the template image B is contained in the image A when only parallel shifts can be applied to the images. \n\n-----Constraints-----\n - 1\u2266M\u2266N\u226650 \n - A_i is a string of length N consisting of # and ..\n - B_i is a string of length M consisting of # and ..\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN M\nA_1\nA_2\n: \nA_N\nB_1\nB_2\n: \nB_M\n\n-----Output-----\nPrint Yes if the template image B is contained in the image A. Print No otherwise.\n\n-----Sample Input-----\n3 2\n#.#\n.#.\n#.#\n#.\n.#\n\n-----Sample Output-----\nYes\n\nThe template image B is identical to the upper-left 2 \u00d7 2 subimage and the lower-right 2 \u00d7 2 subimage of A. Thus, the output should be Yes."} +{"id": "03392", "text": "You have decided to give an allowance to your child depending on the outcome of the game that he will play now.\nThe game is played as follows:\n - There are three \"integer panels\", each with a digit between 1 and 9 (inclusive) printed on it, and one \"operator panel\" with a + printed on it.\n - The player should construct a formula of the form X + Y, by arranging the four panels from left to right. (The operator panel should not be placed at either end of the formula.)\n - Then, the amount of the allowance will be equal to the resulting value of the formula.\nGiven the values A, B and C printed on the integer panels used in the game, find the maximum possible amount of the allowance.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq A, B, C \\leq 9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B C\n\n-----Output-----\nPrint the maximum possible amount of the allowance.\n\n-----Sample Input-----\n1 5 2\n\n-----Sample Output-----\n53\n\nThe amount of the allowance will be 53 when the panels are arranged as 52+1, and this is the maximum possible amount."} +{"id": "03393", "text": "In some other world, today is the day before Christmas Eve.\nMr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \\leq i \\leq N) is p_i yen (the currency of Japan).\nHe has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?\n\n-----Constraints-----\n - 2 \\leq N \\leq 10\n - 100 \\leq p_i \\leq 10000\n - p_i is an even number.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\np_1\np_2\n:\np_N\n\n-----Output-----\nPrint the total amount Mr. Takaha will pay.\n\n-----Sample Input-----\n3\n4980\n7980\n6980\n\n-----Sample Output-----\n15950\n\nThe 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.\nNote that outputs such as 15950.0 will be judged as Wrong Answer."} +{"id": "03394", "text": "The restaurant AtCoder serves the following five dishes:\n - ABC Don (rice bowl): takes A minutes to serve.\n - ARC Curry: takes B minutes to serve.\n - AGC Pasta: takes C minutes to serve.\n - APC Ramen: takes D minutes to serve.\n - ATC Hanbagu (hamburger patty): takes E minutes to serve.\nHere, the time to serve a dish is the time between when an order is placed and when the dish is delivered.\nThis restaurant has the following rules on orders:\n - An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).\n - Only one dish can be ordered at a time.\n - No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.\nE869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.\n\nHere, he can order the dishes in any order he likes, and he can place an order already at time 0.\n\n-----Constraints-----\n - A, B, C, D and E are integers between 1 and 123 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA\nB\nC\nD\nE\n\n-----Output-----\nPrint the earliest possible time for the last dish to be delivered, as an integer.\n\n-----Sample Input-----\n29\n20\n7\n35\n120\n\n-----Sample Output-----\n215\n\nIf we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:\n - Order ABC Don at time 0, which will be delivered at time 29.\n - Order ARC Curry at time 30, which will be delivered at time 50.\n - Order AGC Pasta at time 50, which will be delivered at time 57.\n - Order ATC Hanbagu at time 60, which will be delivered at time 180.\n - Order APC Ramen at time 180, which will be delivered at time 215.\nThere is no way to order the dishes in which the last dish will be delivered earlier than this."} +{"id": "03395", "text": "Given is a sequence of N integers A_1, \\ldots, A_N.\nFind the (multiplicative) inverse of the sum of the inverses of these numbers, \\frac{1}{\\frac{1}{A_1} + \\ldots + \\frac{1}{A_N}}.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 1 \\leq A_i \\leq 1000\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 \\ldots A_N\n\n-----Output-----\nPrint a decimal number (or an integer) representing the value of \\frac{1}{\\frac{1}{A_1} + \\ldots + \\frac{1}{A_N}}.\nYour output will be judged correct when its absolute or relative error from the judge's output is at most 10^{-5}.\n\n-----Sample Input-----\n2\n10 30\n\n-----Sample Output-----\n7.5\n\n\\frac{1}{\\frac{1}{10} + \\frac{1}{30}} = \\frac{1}{\\frac{4}{30}} = \\frac{30}{4} = 7.5.\nPrinting 7.50001, 7.49999, and so on will also be accepted."} +{"id": "03396", "text": "In some other world, today is Christmas Eve.\nThere are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \\leq i \\leq N) is h_i meters.\nHe decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.\nMore specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?\n\n-----Constraints-----\n - 2 \\leq K < N \\leq 10^5\n - 1 \\leq h_i \\leq 10^9\n - h_i is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nh_1\nh_2\n:\nh_N\n\n-----Output-----\nPrint the minimum possible value of h_{max} - h_{min}.\n\n-----Sample Input-----\n5 3\n10\n15\n11\n14\n12\n\n-----Sample Output-----\n2\n\nIf we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal."} +{"id": "03397", "text": "An uppercase or lowercase English letter \\alpha will be given as input.\nIf \\alpha is uppercase, print A; if it is lowercase, print a.\n\n-----Constraints-----\n - \\alpha is an uppercase (A - Z) or lowercase (a - z) English letter.\n\n-----Input-----\nInput is given from Standard Input in the following format:\n\u03b1\n\n-----Output-----\nIf \\alpha is uppercase, print A; if it is lowercase, print a.\n\n-----Sample Input-----\nB\n\n-----Sample Output-----\nA\n\nB is uppercase, so we should print A."} +{"id": "03398", "text": "You are given two integers K and S.\n\nThree variable X, Y and Z takes integer values satisfying 0\u2264X,Y,Z\u2264K.\n\nHow many different assignments of values to X, Y and Z are there such that X + Y + Z = S? \n\n-----Constraints-----\n - 2\u2264K\u22642500 \n - 0\u2264S\u22643K \n - K and S are integers. \n\n-----Input-----\nThe input is given from Standard Input in the following format:\nK S\n\n-----Output-----\nPrint the number of the triples of X, Y and Z that satisfy the condition.\n\n-----Sample Input-----\n2 2\n\n-----Sample Output-----\n6\n\nThere are six triples of X, Y and Z that satisfy the condition:\n - X = 0, Y = 0, Z = 2 \n - X = 0, Y = 2, Z = 0 \n - X = 2, Y = 0, Z = 0 \n - X = 0, Y = 1, Z = 1 \n - X = 1, Y = 0, Z = 1 \n - X = 1, Y = 1, Z = 0"} +{"id": "03399", "text": "You have a digit sequence S of length 4. You are wondering which of the following formats S is in:\n - YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order\n - MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order\nIf S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.\n\n-----Constraints-----\n - S is a digit sequence of length 4.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the specified string: YYMM, MMYY, AMBIGUOUS or NA.\n\n-----Sample Input-----\n1905\n\n-----Sample Output-----\nYYMM\n\nMay XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format."} +{"id": "03400", "text": "Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.\n\n-----Constraints-----\n - 2\\leq K\\leq 100\n - K is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK\n\n-----Output-----\nPrint the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).\n\n-----Sample Input-----\n3\n\n-----Sample Output-----\n2\n\nTwo pairs can be chosen: (2,1) and (2,3)."} +{"id": "03401", "text": "Dolphin loves programming contests. Today, he will take part in a contest in AtCoder.\n\nIn this country, 24-hour clock is used. For example, 9:00 p.m. is referred to as \"21 o'clock\".\n\nThe current time is A o'clock, and a contest will begin in exactly B hours.\nWhen will the contest begin? Answer in 24-hour time.\n\n-----Constraints-----\n - 0 \\leq A,B \\leq 23\n - A and B are integers.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the hour of the starting time of the contest in 24-hour time.\n\n-----Sample Input-----\n9 12\n\n-----Sample Output-----\n21\n\nIn this input, the current time is 9 o'clock, and 12 hours later it will be 21 o'clock in 24-hour time."} +{"id": "03402", "text": "Takahashi is a member of a programming competition site, ButCoder.\nEach member of ButCoder is assigned two values: Inner Rating and Displayed Rating.\nThe Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or more contests. Otherwise, the Displayed Rating will be their Inner Rating minus 100 \\times (10 - K) when the member has participated in K contests.\nTakahashi has participated in N contests, and his Displayed Rating is R. Find his Inner Rating.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 100\n - 0 \\leq R \\leq 4111\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN R\n\n-----Output-----\nPrint his Inner Rating.\n\n-----Sample Input-----\n2 2919\n\n-----Sample Output-----\n3719\n\nTakahashi has participated in 2 contests, which is less than 10, so his Displayed Rating is his Inner Rating minus 100 \\times (10 - 2) = 800.\nThus, Takahashi's Inner Rating is 2919 + 800 = 3719."} +{"id": "03403", "text": "Given is an integer N. Find the number of digits that N has in base K.\n\n-----Notes-----\nFor information on base-K representation, see Positional notation - Wikipedia.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq N \\leq 10^9\n - 2 \\leq K \\leq 10\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\n\n-----Output-----\nPrint the number of digits that N has in base K.\n\n-----Sample Input-----\n11 2\n\n-----Sample Output-----\n4\n\nIn binary, 11 is represented as 1011."} +{"id": "03404", "text": "Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.\nAt least how many sheets of paper does he need?\n\n-----Constraints-----\n - N is an integer.\n - 1 \\leq N \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n5\n\n-----Sample Output-----\n3\n\nBy printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper."} +{"id": "03405", "text": "E869120's and square1001's 16-th birthday is coming soon.\n\nTakahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.\nE869120 and square1001 were just about to eat A and B of those pieces, respectively,\n\nwhen they found a note attached to the cake saying that \"the same person should not take two adjacent pieces of cake\".\nCan both of them obey the instruction in the note and take desired numbers of pieces of cake?\n\n-----Constraints-----\n - A and B are integers between 1 and 16 (inclusive).\n - A+B is at most 16.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nIf both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.\n\n-----Sample Input-----\n5 4\n\n-----Sample Output-----\nYay!\n\nBoth of them can take desired number of pieces as follows:\n"} +{"id": "03406", "text": "There is a string S consisting of digits 1, 2, ..., 9.\nLunlun, the Dachshund, will take out three consecutive digits from S, treat them as a single integer X and bring it to her master. (She cannot rearrange the digits.)\nThe master's favorite number is 753. The closer to this number, the better.\nWhat is the minimum possible (absolute) difference between X and 753?\n\n-----Constraints-----\n - S is a string of length between 4 and 10 (inclusive).\n - Each character in S is 1, 2, ..., or 9.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the minimum possible difference between X and 753.\n\n-----Sample Input-----\n1234567876\n\n-----Sample Output-----\n34\n\nTaking out the seventh to ninth characters results in X = 787, and the difference between this and 753 is 787 - 753 = 34. The difference cannot be made smaller, no matter where X is taken from.\nNote that the digits cannot be rearranged. For example, taking out 567 and rearranging it to 765 is not allowed.\nWe cannot take out three digits that are not consecutive from S, either. For example, taking out the seventh digit 7, the ninth digit 7 and the tenth digit 6 to obtain 776 is not allowed."} +{"id": "03407", "text": "There were $n$ types of swords in the theater basement which had been used during the plays. Moreover there were exactly $x$ swords of each type. $y$ people have broken into the theater basement and each of them has taken exactly $z$ swords of some single type. Note that different people might have taken different types of swords. Note that the values $x, y$ and $z$ are unknown for you.\n\nThe next morning the director of the theater discovers the loss. He counts all swords \u2014 exactly $a_i$ swords of the $i$-th type are left untouched.\n\nThe director has no clue about the initial number of swords of each type in the basement, the number of people who have broken into the basement and how many swords each of them have taken.\n\nFor example, if $n=3$, $a = [3, 12, 6]$ then one of the possible situations is $x=12$, $y=5$ and $z=3$. Then the first three people took swords of the first type and the other two people took swords of the third type. Note that you don't know values $x, y$ and $z$ beforehand but know values of $n$ and $a$.\n\nThus he seeks for your help. Determine the minimum number of people $y$, which could have broken into the theater basement, and the number of swords $z$ each of them has taken.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ $(2 \\le n \\le 2 \\cdot 10^{5})$ \u2014 the number of types of swords.\n\nThe second line of the input contains the sequence $a_1, a_2, \\dots, a_n$ $(0 \\le a_i \\le 10^{9})$, where $a_i$ equals to the number of swords of the $i$-th type, which have remained in the basement after the theft. It is guaranteed that there exists at least one such pair of indices $(j, k)$ that $a_j \\neq a_k$.\n\n\n-----Output-----\n\nPrint two integers $y$ and $z$ \u2014 the minimum number of people which could have broken into the basement and the number of swords each of them has taken.\n\n\n-----Examples-----\nInput\n3\n3 12 6\n\nOutput\n5 3\n\nInput\n2\n2 9\n\nOutput\n1 7\n\nInput\n7\n2 1000000000 4 6 8 4 2\n\nOutput\n2999999987 2\n\nInput\n6\n13 52 0 13 26 52\n\nOutput\n12 13\n\n\n\n-----Note-----\n\nIn the first example the minimum value of $y$ equals to $5$, i.e. the minimum number of people who could have broken into the basement, is $5$. Each of them has taken $3$ swords: three of them have taken $3$ swords of the first type, and two others have taken $3$ swords of the third type.\n\nIn the second example the minimum value of $y$ is $1$, i.e. the minimum number of people who could have broken into the basement, equals to $1$. He has taken $7$ swords of the first type."} +{"id": "03408", "text": "Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly $1$ problem, during the second day \u2014 exactly $2$ problems, during the third day \u2014 exactly $3$ problems, and so on. During the $k$-th day he should solve $k$ problems.\n\nPolycarp has a list of $n$ contests, the $i$-th contest consists of $a_i$ problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly $k$ problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least $k$ problems that Polycarp didn't solve yet during the $k$-th day, then Polycarp stops his training.\n\nHow many days Polycarp can train if he chooses the contests optimally?\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of contests.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2 \\cdot 10^5$) \u2014 the number of problems in the $i$-th contest.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of days Polycarp can train if he chooses the contests optimally.\n\n\n-----Examples-----\nInput\n4\n3 1 4 1\n\nOutput\n3\n\nInput\n3\n1 1 1\n\nOutput\n1\n\nInput\n5\n1 1 1 2 2\n\nOutput\n2"} +{"id": "03409", "text": "You are given a forest \u2014 an undirected graph with $n$ vertices such that each its connected component is a tree.\n\nThe 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.\n\nYou task is to add some edges (possibly zero) to the graph so that it becomes a tree and the diameter of the tree is minimal possible.\n\nIf there are multiple correct answers, print any of them.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($1 \\le n \\le 1000$, $0 \\le m \\le n - 1$) \u2014 the number of vertices of the graph and the number of edges, respectively.\n\nEach of the next $m$ lines contains two integers $v$ and $u$ ($1 \\le v, u \\le n$, $v \\ne u$) \u2014 the descriptions of the edges.\n\nIt is guaranteed that the given graph is a forest.\n\n\n-----Output-----\n\nIn the first line print the diameter of the resulting tree.\n\nEach of the next $(n - 1) - m$ lines should contain two integers $v$ and $u$ ($1 \\le v, u \\le n$, $v \\ne u$) \u2014 the descriptions of the added edges.\n\nThe resulting graph should be a tree and its diameter should be minimal possible.\n\nFor $m = n - 1$ no edges are added, thus the output consists of a single integer \u2014 diameter of the given tree.\n\nIf there are multiple correct answers, print any of them.\n\n\n-----Examples-----\nInput\n4 2\n1 2\n2 3\n\nOutput\n2\n4 2\n\nInput\n2 0\n\nOutput\n1\n1 2\n\nInput\n3 2\n1 3\n2 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nIn the first example adding edges (1, 4) or (3, 4) will lead to a total diameter of 3. Adding edge (2, 4), however, will make it 2.\n\nEdge (1, 2) is the only option you have for the second example. The diameter is 1.\n\nYou can't add any edges in the third example. The diameter is already 2."} +{"id": "03410", "text": "You are given a tree, which consists of $n$ vertices. Recall that a tree is a connected undirected graph without cycles. [Image] Example of a tree. \n\nVertices are numbered from $1$ to $n$. All vertices have weights, the weight of the vertex $v$ is $a_v$.\n\nRecall that the distance between two vertices in the tree is the number of edges on a simple path between them.\n\nYour task is to find the subset of vertices with the maximum total weight (the weight of the subset is the sum of weights of all vertices in it) such that there is no pair of vertices with the distance $k$ or less between them in this subset.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n, k \\le 200$) \u2014 the number of vertices in the tree and the distance restriction, respectively.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^5$), where $a_i$ is the weight of the vertex $i$.\n\nThe next $n - 1$ lines contain edges of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$ \u2014 the labels of vertices it connects ($1 \\le u_i, v_i \\le n$, $u_i \\ne v_i$).\n\nIt is guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum total weight of the subset in which all pairs of vertices have distance more than $k$.\n\n\n-----Examples-----\nInput\n5 1\n1 2 3 4 5\n1 2\n2 3\n3 4\n3 5\n\nOutput\n11\n\nInput\n7 2\n2 1 2 1 2 1 1\n6 4\n1 5\n3 1\n2 3\n7 5\n7 4\n\nOutput\n4"} +{"id": "03411", "text": "There are $n$ dormitories in Berland State University, they are numbered with integers from $1$ to $n$. Each dormitory consists of rooms, there are $a_i$ rooms in $i$-th dormitory. The rooms in $i$-th dormitory are numbered from $1$ to $a_i$.\n\nA postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all $n$ dormitories is written on an envelope. In this case, assume that all the rooms are numbered from $1$ to $a_1 + a_2 + \\dots + a_n$ and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.\n\nFor example, in case $n=2$, $a_1=3$ and $a_2=5$ an envelope can have any integer from $1$ to $8$ written on it. If the number $7$ is written on an envelope, it means that the letter should be delivered to the room number $4$ of the second dormitory.\n\nFor each of $m$ letters by the room number among all $n$ dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ $(1 \\le n, m \\le 2 \\cdot 10^{5})$ \u2014 the number of dormitories and the number of letters.\n\nThe second line contains a sequence $a_1, a_2, \\dots, a_n$ $(1 \\le a_i \\le 10^{10})$, where $a_i$ equals to the number of rooms in the $i$-th dormitory. The third line contains a sequence $b_1, b_2, \\dots, b_m$ $(1 \\le b_j \\le a_1 + a_2 + \\dots + a_n)$, where $b_j$ equals to the room number (among all rooms of all dormitories) for the $j$-th letter. All $b_j$ are given in increasing order.\n\n\n-----Output-----\n\nPrint $m$ lines. For each letter print two integers $f$ and $k$ \u2014 the dormitory number $f$ $(1 \\le f \\le n)$ and the room number $k$ in this dormitory $(1 \\le k \\le a_f)$ to deliver the letter.\n\n\n-----Examples-----\nInput\n3 6\n10 15 12\n1 9 12 23 26 37\n\nOutput\n1 1\n1 9\n2 2\n2 13\n3 1\n3 12\n\nInput\n2 3\n5 10000000000\n5 6 9999999999\n\nOutput\n1 5\n2 1\n2 9999999994\n\n\n\n-----Note-----\n\nIn the first example letters should be delivered in the following order: the first letter in room $1$ of the first dormitory the second letter in room $9$ of the first dormitory the third letter in room $2$ of the second dormitory the fourth letter in room $13$ of the second dormitory the fifth letter in room $1$ of the third dormitory the sixth letter in room $12$ of the third dormitory"} +{"id": "03412", "text": "Polycarp has guessed three positive integers $a$, $b$ and $c$. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order \u2014 their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: $a+b$, $a+c$, $b+c$ and $a+b+c$.\n\nYou have to guess three numbers $a$, $b$ and $c$ using given numbers. Print three guessed integers in any order.\n\nPay attention that some given numbers $a$, $b$ and $c$ can be equal (it is also possible that $a=b=c$).\n\n\n-----Input-----\n\nThe only line of the input contains four positive integers $x_1, x_2, x_3, x_4$ ($2 \\le x_i \\le 10^9$) \u2014 numbers written on a board in random order. It is guaranteed that the answer exists for the given number $x_1, x_2, x_3, x_4$.\n\n\n-----Output-----\n\nPrint such positive integers $a$, $b$ and $c$ that four numbers written on a board are values $a+b$, $a+c$, $b+c$ and $a+b+c$ written in some order. Print $a$, $b$ and $c$ in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.\n\n\n-----Examples-----\nInput\n3 6 5 4\n\nOutput\n2 1 3\n\nInput\n40 40 40 60\n\nOutput\n20 20 20\n\nInput\n201 101 101 200\n\nOutput\n1 100 100"} +{"id": "03413", "text": "You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' \u2014 colors of lamps in the garland).\n\nYou have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.\n\nA garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors.\n\nIn other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \\ne t_{i + 1}$ should be satisfied.\n\nAmong all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of lamps.\n\nThe second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' \u2014 colors of lamps in the garland.\n\n\n-----Output-----\n\nIn the first line of the output print one integer $r$ \u2014 the minimum number of recolors needed to obtain a diverse garland from the given one.\n\nIn the second line of the output print one string $t$ of length $n$ \u2014 a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.\n\n\n-----Examples-----\nInput\n9\nRBGRRBRGG\n\nOutput\n2\nRBGRGBRGR\n\nInput\n8\nBBBGBRRR\n\nOutput\n2\nBRBGBRGR\n\nInput\n13\nBBRRRRGGGGGRR\n\nOutput\n6\nBGRBRBGBGBGRG"} +{"id": "03414", "text": "You are given an integer array of length $n$.\n\nYou have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to $[x, x + 1, \\dots, x + k - 1]$ for some value $x$ and length $k$.\n\nSubsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array $[5, 3, 1, 2, 4]$ the following arrays are subsequences: $[3]$, $[5, 3, 1, 2, 4]$, $[5, 1, 4]$, but the array $[1, 3]$ is not.\n\n\n-----Input-----\n\nThe first line of the input containing integer number $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of the array. The second line of the input containing $n$ integer numbers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) \u2014 the array itself.\n\n\n-----Output-----\n\nOn the first line print $k$ \u2014 the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.\n\nOn the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.\n\n\n-----Examples-----\nInput\n7\n3 3 4 7 5 6 8\n\nOutput\n4\n2 3 5 6 \n\nInput\n6\n1 3 5 2 4 6\n\nOutput\n2\n1 4 \n\nInput\n4\n10 9 8 7\n\nOutput\n1\n1 \n\nInput\n9\n6 7 8 3 4 5 9 10 11\n\nOutput\n6\n1 2 3 7 8 9 \n\n\n\n-----Note-----\n\nAll valid answers for the first example (as sequences of indices): $[1, 3, 5, 6]$ $[2, 3, 5, 6]$ \n\nAll valid answers for the second example: $[1, 4]$ $[2, 5]$ $[3, 6]$ \n\nAll valid answers for the third example: $[1]$ $[2]$ $[3]$ $[4]$ \n\nAll valid answers for the fourth example: $[1, 2, 3, 7, 8, 9]$"} +{"id": "03415", "text": "You are given integers A and B, each between 1 and 3 (inclusive).\nDetermine if there is an integer C between 1 and 3 (inclusive) such that A \\times B \\times C is an odd number.\n\n-----Constraints-----\n - All values in input are integers.\n - 1 \\leq A, B \\leq 3\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nIf there is an integer C between 1 and 3 that satisfies the condition, print Yes; otherwise, print No.\n\n-----Sample Input-----\n3 1\n\n-----Sample Output-----\nYes\n\nLet C = 3. Then, A \\times B \\times C = 3 \\times 1 \\times 3 = 9, which is an odd number."} +{"id": "03416", "text": "There is a train going from Station A to Station B that costs X yen (the currency of Japan).\nAlso, there is a bus going from Station B to Station C that costs Y yen.\nJoisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.\nHow much does it cost to travel from Station A to Station C if she uses this ticket?\n\n-----Constraints-----\n - 1 \\leq X,Y \\leq 100\n - Y is an even number.\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y\n\n-----Output-----\nIf it costs x yen to travel from Station A to Station C, print x.\n\n-----Sample Input-----\n81 58\n\n-----Sample Output-----\n110\n\n - The train fare is 81 yen.\n - The train fare is 58 \u2044 2=29 yen with the 50% discount.\nThus, it costs 110 yen to travel from Station A to Station C."} +{"id": "03417", "text": "Takahashi is going to set a 3-character password.\nHow many possible passwords are there if each of its characters must be a digit between 1 and N (inclusive)?\n\n-----Constraints-----\n - 1 \\leq N \\leq 9\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the number of possible passwords.\n\n-----Sample Input-----\n2\n\n-----Sample Output-----\n8\n\nThere are eight possible passwords: 111, 112, 121, 122, 211, 212, 221, and 222."} +{"id": "03418", "text": "Shichi-Go-San (literally \"Seven-Five-Three\") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.\nTakahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?\n\n-----Constraints-----\n - 1 \u2264 X \u2264 9\n - X is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX\n\n-----Output-----\nIf Takahashi's growth will be celebrated, print YES; if it will not, print NO.\n\n-----Sample Input-----\n5\n\n-----Sample Output-----\nYES\n\nThe growth of a five-year-old child will be celebrated."} +{"id": "03419", "text": "Decades have passed since the beginning of AtCoder Beginner Contest.\nThe contests are labeled as ABC001, ABC002, ... from the first round, but after the 999-th round ABC999, a problem occurred: how the future rounds should be labeled?\nIn the end, the labels for the rounds from the 1000-th to the 1998-th are decided: ABD001, ABD002, ..., ABD999.\nYou are given an integer N between 1 and 1998 (inclusive). Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.\n\n-----Constraints-----\n - 1 \\leq N \\leq 1998\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the first three characters of the label of the N-th round of AtCoder Beginner Contest.\n\n-----Sample Input-----\n999\n\n-----Sample Output-----\nABC\n\nThe 999-th round of AtCoder Beginner Contest is labeled as ABC999."} +{"id": "03420", "text": "In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.\n\nTwo antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.\n\nDetermine if there exists a pair of antennas that cannot communicate directly.\n\nHere, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. \n\n-----Constraints-----\n - a, b, c, d, e and k are integers between 0 and 123 (inclusive).\n - a < b < c < d < e\n\n-----Input-----\nInput is given from Standard Input in the following format:\na\nb\nc\nd\ne\nk\n\n-----Output-----\nPrint :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.\n\n-----Sample Input-----\n1\n2\n4\n8\n9\n15\n\n-----Sample Output-----\nYay!\n\nIn this case, there is no pair of antennas that cannot communicate directly, because:\n - the distance between A and B is 2 - 1 = 1\n - the distance between A and C is 4 - 1 = 3\n - the distance between A and D is 8 - 1 = 7\n - the distance between A and E is 9 - 1 = 8\n - the distance between B and C is 4 - 2 = 2\n - the distance between B and D is 8 - 2 = 6\n - the distance between B and E is 9 - 2 = 7\n - the distance between C and D is 8 - 4 = 4\n - the distance between C and E is 9 - 4 = 5\n - the distance between D and E is 9 - 8 = 1\nand none of them is greater than 15. Thus, the correct output is Yay!."} +{"id": "03421", "text": "You will be given an integer a and a string s consisting of lowercase English letters as input.\nWrite a program that prints s if a is not less than 3200 and prints red if a is less than 3200.\n\n-----Constraints-----\n - 2800 \\leq a < 5000\n - s is a string of length between 1 and 10 (inclusive).\n - Each character of s is a lowercase English letter.\n\n-----Input-----\nInput is given from Standard Input in the following format:\na\ns\n\n-----Output-----\nIf a is not less than 3200, print s; if a is less than 3200, print red.\n\n-----Sample Input-----\n3200\npink\n\n-----Sample Output-----\npink\n\na = 3200 is not less than 3200, so we print s = pink."} +{"id": "03422", "text": "A programming competition site AtCode regularly holds programming contests.\nThe next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200.\nThe contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800.\nThe contest after the ARC is called AGC, which is rated for all contestants.\nTakahashi's rating on AtCode is R. What is the next contest rated for him?\n\n-----Constraints-----\n - 0 \u2264 R \u2264 4208\n - R is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nR\n\n-----Output-----\nPrint the name of the next contest rated for Takahashi (ABC, ARC or AGC).\n\n-----Sample Input-----\n1199\n\n-----Sample Output-----\nABC\n\n1199 is less than 1200, so ABC will be rated."} +{"id": "03423", "text": "Cat Snuke is learning to write characters.\nToday, he practiced writing digits 1 and 9, but he did it the other way around.\nYou are given a three-digit integer n written by Snuke.\nPrint the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.\n\n-----Constraints-----\n - 111 \\leq n \\leq 999\n - n is an integer consisting of digits 1 and 9.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn\n\n-----Output-----\nPrint the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.\n\n-----Sample Input-----\n119\n\n-----Sample Output-----\n991\n\nReplace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991."} +{"id": "03424", "text": "Alice guesses the strings that Bob made for her.\n\nAt first, Bob came up with the secret string $a$ consisting of lowercase English letters. The string $a$ has a length of $2$ or more characters. Then, from string $a$ he builds a new string $b$ and offers Alice the string $b$ so that she can guess the string $a$.\n\nBob builds $b$ from $a$ as follows: he writes all the substrings of length $2$ of the string $a$ in the order from left to right, and then joins them in the same order into the string $b$.\n\nFor example, if Bob came up with the string $a$=\"abac\", then all the substrings of length $2$ of the string $a$ are: \"ab\", \"ba\", \"ac\". Therefore, the string $b$=\"abbaac\".\n\nYou are given the string $b$. Help Alice to guess the string $a$ that Bob came up with. It is guaranteed that $b$ was built according to the algorithm given above. It can be proved that the answer to the problem is unique.\n\n\n-----Input-----\n\nThe first line contains a single positive integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases in the test. Then $t$ test cases follow.\n\nEach test case consists of one line in which the string $b$ is written, consisting of lowercase English letters ($2 \\le |b| \\le 100$)\u00a0\u2014 the string Bob came up with, where $|b|$ is the length of the string $b$. It is guaranteed that $b$ was built according to the algorithm given above.\n\n\n-----Output-----\n\nOutput $t$ answers to test cases. Each answer is the secret string $a$, consisting of lowercase English letters, that Bob came up with.\n\n\n-----Example-----\nInput\n4\nabbaac\nac\nbccddaaf\nzzzzzzzzzz\n\nOutput\nabac\nac\nbcdaf\nzzzzzz\n\n\n\n-----Note-----\n\nThe first test case is explained in the statement.\n\nIn the second test case, Bob came up with the string $a$=\"ac\", the string $a$ has a length $2$, so the string $b$ is equal to the string $a$.\n\nIn the third test case, Bob came up with the string $a$=\"bcdaf\", substrings of length $2$ of string $a$ are: \"bc\", \"cd\", \"da\", \"af\", so the string $b$=\"bccddaaf\"."} +{"id": "03425", "text": "You are given two positive integers $a$ and $b$. In one move you can increase $a$ by $1$ (replace $a$ with $a+1$). Your task is to find the minimum number of moves you need to do in order to make $a$ divisible by $b$. It is possible, that you have to make $0$ moves, as $a$ is already divisible by $b$. You have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains two integers $a$ and $b$ ($1 \\le a, b \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case print the answer \u2014 the minimum number of moves you need to do in order to make $a$ divisible by $b$.\n\n\n-----Example-----\nInput\n5\n10 4\n13 9\n100 13\n123 456\n92 46\n\nOutput\n2\n5\n4\n333\n0"} +{"id": "03426", "text": "The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are.\n\nMathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows:\n\nSuppose we want to analyze the segment of $n$ consecutive days. We have measured the temperatures during these $n$ days; the temperature during $i$-th day equals $a_i$.\n\nWe denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $x$ to day $y$, we calculate it as $\\frac{\\sum \\limits_{i = x}^{y} a_i}{y - x + 1}$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $k$ consecutive days. For example, if analyzing the measures $[3, 4, 1, 2]$ and $k = 3$, we are interested in segments $[3, 4, 1]$, $[4, 1, 2]$ and $[3, 4, 1, 2]$ (we want to find the maximum value of average temperature over these segments).\n\nYou have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 5000$) \u2014 the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively.\n\nThe second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \\le a_i \\le 5000$) \u2014 the temperature measures during given $n$ days.\n\n\n-----Output-----\n\nPrint one real number \u2014 the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $k$ consecutive days.\n\nYour answer will be considered correct if the following condition holds: $|res - res_0| < 10^{-6}$, where $res$ is your answer, and $res_0$ is the answer given by the jury's solution.\n\n\n-----Example-----\nInput\n4 3\n3 4 1 2\n\nOutput\n2.666666666666667"} +{"id": "03427", "text": "You are given an array $a$ of length $n$.\n\nYou are also given a set of distinct positions $p_1, p_2, \\dots, p_m$, where $1 \\le p_i < n$. The position $p_i$ means that you can swap elements $a[p_i]$ and $a[p_i + 1]$. You can apply this operation any number of times for each of the given positions.\n\nYour task is to determine if it is possible to sort the initial array in non-decreasing order ($a_1 \\le a_2 \\le \\dots \\le a_n$) using only allowed swaps.\n\nFor example, if $a = [3, 2, 1]$ and $p = [1, 2]$, then we can first swap elements $a[2]$ and $a[3]$ (because position $2$ is contained in the given set $p$). We get the array $a = [3, 1, 2]$. Then we swap $a[1]$ and $a[2]$ (position $1$ is also contained in $p$). We get the array $a = [1, 3, 2]$. Finally, we swap $a[2]$ and $a[3]$ again and get the array $a = [1, 2, 3]$, sorted in non-decreasing order.\n\nYou can see that if $a = [4, 1, 2, 3]$ and $p = [3, 2]$ then you cannot sort the array.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThen $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \\le m < n \\le 100$) \u2014 the number of elements in $a$ and the number of elements in $p$. The second line of the test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$). The third line of the test case contains $m$ integers $p_1, p_2, \\dots, p_m$ ($1 \\le p_i < n$, all $p_i$ are distinct) \u2014 the set of positions described in the problem statement.\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 \"YES\" (without quotes) if you can sort the initial array in non-decreasing order ($a_1 \\le a_2 \\le \\dots \\le a_n$) using only allowed swaps. Otherwise, print \"NO\".\n\n\n-----Example-----\nInput\n6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4\n\nOutput\nYES\nNO\nYES\nYES\nNO\nYES"} +{"id": "03428", "text": "Polycarp loves ciphers. He has invented his own cipher called repeating.\n\nRepeating cipher is used for strings. To encrypt the string $s=s_{1}s_{2} \\dots s_{m}$ ($1 \\le m \\le 10$), Polycarp uses the following algorithm:\n\n he writes down $s_1$ ones, he writes down $s_2$ twice, he writes down $s_3$ three times, ... he writes down $s_m$ $m$ times. \n\nFor example, if $s$=\"bab\" the process is: \"b\" $\\to$ \"baa\" $\\to$ \"baabbb\". So the encrypted $s$=\"bab\" is \"baabbb\".\n\nGiven string $t$ \u2014 the result of encryption of some string $s$. Your task is to decrypt it, i. e. find the string $s$.\n\n\n-----Input-----\n\nThe first line contains integer $n$ ($1 \\le n \\le 55$) \u2014 the length of the encrypted string. The second line of the input contains $t$ \u2014 the result of encryption of some string $s$. It contains only lowercase Latin letters. The length of $t$ is exactly $n$.\n\nIt is guaranteed that the answer to the test exists.\n\n\n-----Output-----\n\nPrint such string $s$ that after encryption it equals $t$.\n\n\n-----Examples-----\nInput\n6\nbaabbb\n\nOutput\nbab\nInput\n10\nooopppssss\n\nOutput\noops\nInput\n1\nz\n\nOutput\nz"} +{"id": "03429", "text": "You are given an undirected weighted connected graph with $n$ vertices and $m$ edges without loops and multiple edges.\n\nThe $i$-th edge is $e_i = (u_i, v_i, w_i)$; the distance between vertices $u_i$ and $v_i$ along the edge $e_i$ is $w_i$ ($1 \\le w_i$). The graph is connected, i. e. for any pair of vertices, there is at least one path between them consisting only of edges of the given graph.\n\nA minimum spanning tree (MST) in case of positive weights is a subset of the edges of a connected weighted undirected graph that connects all the vertices together and has minimum total cost among all such subsets (total cost is the sum of costs of chosen edges).\n\nYou can modify the given graph. The only operation you can perform is the following: increase the weight of some edge by $1$. You can increase the weight of each edge multiple (possibly, zero) times.\n\nSuppose that the initial MST cost is $k$. Your problem is to increase weights of some edges with minimum possible number of operations in such a way that the cost of MST in the obtained graph remains $k$, but MST is unique (it means that there is only one way to choose MST in the obtained graph).\n\nYour problem is to calculate the minimum number of operations required to do it.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n \\le 2 \\cdot 10^5, n - 1 \\le m \\le 2 \\cdot 10^5$) \u2014 the number of vertices and the number of edges in the initial graph.\n\nThe next $m$ lines contain three integers each. The $i$-th line contains the description of the $i$-th edge $e_i$. It is denoted by three integers $u_i, v_i$ and $w_i$ ($1 \\le u_i, v_i \\le n, u_i \\ne v_i, 1 \\le w \\le 10^9$), where $u_i$ and $v_i$ are vertices connected by the $i$-th edge and $w_i$ is the weight of this edge.\n\nIt is guaranteed that the given graph doesn't contain loops and multiple edges (i.e. for each $i$ from $1$ to $m$ $u_i \\ne v_i$ and for each unordered pair of vertices $(u, v)$ there is at most one edge connecting this pair of vertices). It is also guaranteed that the given graph is connected.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of operations to unify MST of the initial graph without changing the cost of MST.\n\n\n-----Examples-----\nInput\n8 10\n1 2 1\n2 3 2\n2 4 5\n1 4 2\n6 3 3\n6 1 3\n3 5 2\n3 7 1\n4 8 1\n6 2 4\n\nOutput\n1\n\nInput\n4 3\n2 1 3\n4 3 4\n2 4 1\n\nOutput\n0\n\nInput\n3 3\n1 2 1\n2 3 2\n1 3 3\n\nOutput\n0\n\nInput\n3 3\n1 2 1\n2 3 3\n1 3 3\n\nOutput\n1\n\nInput\n1 0\n\nOutput\n0\n\nInput\n5 6\n1 2 2\n2 3 1\n4 5 3\n2 4 2\n1 4 2\n1 5 3\n\nOutput\n2\n\n\n\n-----Note-----\n\nThe picture corresponding to the first example: [Image]\n\nYou can, for example, increase weight of the edge $(1, 6)$ or $(6, 3)$ by $1$ to unify MST.\n\nThe picture corresponding to the last example: $\\$ 8$\n\nYou can, for example, increase weights of edges $(1, 5)$ and $(2, 4)$ by $1$ to unify MST."} +{"id": "03430", "text": "You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' \u2014 colors of lamps in the garland).\n\nYou have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.\n\nA garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is $t$, then for each $i, j$ such that $t_i = t_j$ should be satisfied $|i-j|~ mod~ 3 = 0$. The value $|x|$ means absolute value of $x$, the operation $x~ mod~ y$ means remainder of $x$ when divided by $y$.\n\nFor example, the following garlands are nice: \"RGBRGBRG\", \"GB\", \"R\", \"GRBGRBG\", \"BRGBRGB\". The following garlands are not nice: \"RR\", \"RGBG\".\n\nAmong all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of lamps.\n\nThe second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' \u2014 colors of lamps in the garland.\n\n\n-----Output-----\n\nIn the first line of the output print one integer $r$ \u2014 the minimum number of recolors needed to obtain a nice garland from the given one.\n\nIn the second line of the output print one string $t$ of length $n$ \u2014 a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.\n\n\n-----Examples-----\nInput\n3\nBRB\n\nOutput\n1\nGRB\n\nInput\n7\nRGBGRBB\n\nOutput\n3\nRGBRGBR"} +{"id": "03431", "text": "Takahashi received otoshidama (New Year's money gifts) from N of his relatives.\nYou are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.\nFor example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.\nIf we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?\n\n-----Constraints-----\n - 2 \\leq N \\leq 10\n - u_i = JPY or BTC.\n - If u_i = JPY, x_i is an integer such that 1 \\leq x_i \\leq 10^8.\n - If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \\leq x_i \\leq 100.00000000.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nx_1 u_1\nx_2 u_2\n:\nx_N u_N\n\n-----Output-----\nIf the gifts are worth Y yen in total, print the value Y (not necessarily an integer).\nOutput will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.\n\n-----Sample Input-----\n2\n10000 JPY\n0.10000000 BTC\n\n-----Sample Output-----\n48000.0\n\nThe otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.\nOutputs such as 48000 and 48000.1 will also be judged correct."} +{"id": "03432", "text": "There is an N-car train.\nYou are given an integer i. Find the value of j such that the following statement is true: \"the i-th car from the front of the train is the j-th car from the back.\"\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 1 \\leq i \\leq N\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN i\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n4 2\n\n-----Sample Output-----\n3\n\nThe second car from the front of a 4-car train is the third car from the back."} +{"id": "03433", "text": "Given are strings s and t of length N each, both consisting of lowercase English letters.\nLet us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - |S| = |T| = N\n - S and T are strings consisting of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS T\n\n-----Output-----\nPrint the string formed.\n\n-----Sample Input-----\n2\nip cc\n\n-----Sample Output-----\nicpc\n"} +{"id": "03434", "text": "In AtCoder City, there are three stations numbered 1, 2, and 3.\nEach of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.\nTo improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.\nDetermine if there is a pair of stations that will be connected by a bus service.\n\n-----Constraints-----\n - Each character of S is A or B.\n - |S| = 3\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nIf there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.\n\n-----Sample Input-----\nABA\n\n-----Sample Output-----\nYes\n\nCompany A operates Station 1 and 3, while Company B operates Station 2.\nThere will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes."} +{"id": "03435", "text": "We have weather records at AtCoder Town for some consecutive three days. A string of length 3, S, represents the records - if the i-th character is S, it means it was sunny on the i-th day; if that character is R, it means it was rainy on that day.\nFind the maximum number of consecutive rainy days in this period.\n\n-----Constraints-----\n - |S| = 3\n - Each character of S is S or R.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the maximum number of consecutive rainy days in the period.\n\n-----Sample Input-----\nRRS\n\n-----Sample Output-----\n2\n\nWe had rain on the 1-st and 2-nd days in the period. Here, the maximum number of consecutive rainy days is 2, so we should print 2."} +{"id": "03436", "text": "We have three boxes A, B, and C, each of which contains an integer.\n\nCurrently, the boxes A, B, and C contain the integers X, Y, and Z, respectively.\n\nWe will now do the operations below in order. Find the content of each box afterward. \n - Swap the contents of the boxes A and B\n - Swap the contents of the boxes A and C\n\n-----Constraints-----\n - 1 \\leq X,Y,Z \\leq 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y Z\n\n-----Output-----\nPrint the integers contained in the boxes A, B, and C, in this order, with space in between.\n\n-----Sample Input-----\n1 2 3\n\n-----Sample Output-----\n3 1 2\n\nAfter the contents of the boxes A and B are swapped, A, B, and C contain 2, 1, and 3, respectively.\n\nThen, after the contents of A and C are swapped, A, B, and C contain 3, 1, and 2, respectively."} +{"id": "03437", "text": "Takahashi, who is A years old, is riding a Ferris wheel.\nIt costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)\nFind the cost of the Ferris wheel for Takahashi.\n\n-----Constraints-----\n - 0 \u2264 A \u2264 100\n - 2 \u2264 B \u2264 1000\n - B is an even number.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the cost of the Ferris wheel for Takahashi.\n\n-----Sample Input-----\n30 100\n\n-----Sample Output-----\n100\n\nTakahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen."} +{"id": "03438", "text": "There is always an integer in Takahashi's mind.\nInitially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is + or -. When he eats +, the integer in his mind increases by 1; when he eats -, the integer in his mind decreases by 1.\nThe symbols Takahashi is going to eat are given to you as a string S. The i-th character in S is the i-th symbol for him to eat.\nFind the integer in Takahashi's mind after he eats all the symbols.\n\n-----Constraints-----\n - The length of S is 4.\n - Each character in S is + or -.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the integer in Takahashi's mind after he eats all the symbols.\n\n-----Sample Input-----\n+-++\n\n-----Sample Output-----\n2\n\n - Initially, the integer in Takahashi's mind is 0.\n - The first integer for him to eat is +. After eating it, the integer in his mind becomes 1.\n - The second integer to eat is -. After eating it, the integer in his mind becomes 0.\n - The third integer to eat is +. After eating it, the integer in his mind becomes 1.\n - The fourth integer to eat is +. After eating it, the integer in his mind becomes 2.\nThus, the integer in Takahashi's mind after he eats all the symbols is 2."} +{"id": "03439", "text": "You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)\nWrite a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.\n\n-----Constraints-----\n - S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.\n\n-----Sample Input-----\n2019/04/30\n\n-----Sample Output-----\nHeisei\n"} +{"id": "03440", "text": "Polycarp has prepared $n$ competitive programming problems. The topic of the $i$-th problem is $a_i$, and some problems' topics may coincide.\n\nPolycarp has to host several thematic contests. All problems in each contest should have the same topic, and all contests should have pairwise distinct topics. He may not use all the problems. It is possible that there are no contests for some topics.\n\nPolycarp wants to host competitions on consecutive days, one contest per day. Polycarp wants to host a set of contests in such a way that: number of problems in each contest is exactly twice as much as in the previous contest (one day ago), the first contest can contain arbitrary number of problems; the total number of problems in all the contests should be maximized. \n\nYour task is to calculate the maximum number of problems in the set of thematic contests. Note, that you should not maximize the number of contests.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of problems Polycarp has prepared.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) where $a_i$ is the topic of the $i$-th problem.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of problems in the set of thematic contests.\n\n\n-----Examples-----\nInput\n18\n2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10\n\nOutput\n14\n\nInput\n10\n6 6 6 3 6 1000000000 3 3 6 6\n\nOutput\n9\n\nInput\n3\n1337 1337 1337\n\nOutput\n3\n\n\n\n-----Note-----\n\nIn the first example the optimal sequence of contests is: $2$ problems of the topic $1$, $4$ problems of the topic $2$, $8$ problems of the topic $10$.\n\nIn the second example the optimal sequence of contests is: $3$ problems of the topic $3$, $6$ problems of the topic $6$.\n\nIn the third example you can take all the problems with the topic $1337$ (the number of such problems is $3$ so the answer is $3$) and host a single contest."} +{"id": "03441", "text": "The only difference between easy and hard versions are constraints on $n$ and $k$.\n\nYou are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $k$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $0$).\n\nEach conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.\n\nYou (suddenly!) have the ability to see the future. You know that during the day you will receive $n$ messages, the $i$-th message will be received from the friend with ID $id_i$ ($1 \\le id_i \\le 10^9$).\n\nIf you receive a message from $id_i$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.\n\nOtherwise (i.e. if there is no conversation with $id_i$ on the screen): Firstly, if the number of conversations displayed on the screen is $k$, the last conversation (which has the position $k$) is removed from the screen. Now the number of conversations on the screen is guaranteed to be less than $k$ and the conversation with the friend $id_i$ is not displayed on the screen. The conversation with the friend $id_i$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. \n\nYour task is to find the list of conversations (in the order they are displayed on the screen) after processing all $n$ messages.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n, k \\le 200)$ \u2014 the number of messages and the number of conversations your smartphone can show.\n\nThe second line of the input contains $n$ integers $id_1, id_2, \\dots, id_n$ ($1 \\le id_i \\le 10^9$), where $id_i$ is the ID of the friend which sends you the $i$-th message.\n\n\n-----Output-----\n\nIn the first line of the output print one integer $m$ ($1 \\le m \\le min(n, k)$) \u2014 the number of conversations shown after receiving all $n$ messages.\n\nIn the second line print $m$ integers $ids_1, ids_2, \\dots, ids_m$, where $ids_i$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $i$ after receiving all $n$ messages.\n\n\n-----Examples-----\nInput\n7 2\n1 2 3 2 1 3 2\n\nOutput\n2\n2 1 \n\nInput\n10 4\n2 3 3 1 1 2 1 2 3 3\n\nOutput\n3\n1 3 2 \n\n\n\n-----Note-----\n\nIn the first example the list of conversations will change in the following way (in order from the first to last message): $[]$; $[1]$; $[2, 1]$; $[3, 2]$; $[3, 2]$; $[1, 3]$; $[1, 3]$; $[2, 1]$. \n\nIn the second example the list of conversations will change in the following way: $[]$; $[2]$; $[3, 2]$; $[3, 2]$; $[1, 3, 2]$; and then the list will not change till the end."} +{"id": "03442", "text": "Polycarp has $n$ coins, the value of the $i$-th coin is $a_i$. It is guaranteed that all the values are integer powers of $2$ (i.e. $a_i = 2^d$ for some non-negative integer number $d$).\n\nPolycarp wants to know answers on $q$ queries. The $j$-th query is described as integer number $b_j$. The answer to the query is the minimum number of coins that is necessary to obtain the value $b_j$ using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value $b_j$, the answer to the $j$-th query is -1.\n\nThe queries are independent (the answer on the query doesn't affect Polycarp's coins).\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $q$ ($1 \\le n, q \\le 2 \\cdot 10^5$) \u2014 the number of coins and the number of queries.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ \u2014 values of coins ($1 \\le a_i \\le 2 \\cdot 10^9$). It is guaranteed that all $a_i$ are integer powers of $2$ (i.e. $a_i = 2^d$ for some non-negative integer number $d$).\n\nThe next $q$ lines contain one integer each. The $j$-th line contains one integer $b_j$ \u2014 the value of the $j$-th query ($1 \\le b_j \\le 10^9$).\n\n\n-----Output-----\n\nPrint $q$ integers $ans_j$. The $j$-th integer must be equal to the answer on the $j$-th query. If Polycarp can't obtain the value $b_j$ the answer to the $j$-th query is -1.\n\n\n-----Example-----\nInput\n5 4\n2 4 8 2 4\n8\n5\n14\n10\n\nOutput\n1\n-1\n3\n2"} +{"id": "03443", "text": "There are $n$ players sitting at the card table. Each player has a favorite number. The favorite number of the $j$-th player is $f_j$.\n\nThere are $k \\cdot n$ cards on the table. Each card contains a single integer: the $i$-th card contains number $c_i$. Also, you are given a sequence $h_1, h_2, \\dots, h_k$. Its meaning will be explained below.\n\nThe players have to distribute all the cards in such a way that each of them will hold exactly $k$ cards. After all the cards are distributed, each player counts the number of cards he has that contains his favorite number. The joy level of a player equals $h_t$ if the player holds $t$ cards containing his favorite number. If a player gets no cards with his favorite number (i.e., $t=0$), his joy level is $0$.\n\nPrint the maximum possible total joy levels of the players after the cards are distributed. Note that the sequence $h_1, \\dots, h_k$ is the same for all the players.\n\n\n-----Input-----\n\nThe first line of input contains two integers $n$ and $k$ ($1 \\le n \\le 500, 1 \\le k \\le 10$) \u2014 the number of players and the number of cards each player will get.\n\nThe second line contains $k \\cdot n$ integers $c_1, c_2, \\dots, c_{k \\cdot n}$ ($1 \\le c_i \\le 10^5$) \u2014 the numbers written on the cards.\n\nThe third line contains $n$ integers $f_1, f_2, \\dots, f_n$ ($1 \\le f_j \\le 10^5$) \u2014 the favorite numbers of the players.\n\nThe fourth line contains $k$ integers $h_1, h_2, \\dots, h_k$ ($1 \\le h_t \\le 10^5$), where $h_t$ is the joy level of a player if he gets exactly $t$ cards with his favorite number written on them. It is guaranteed that the condition $h_{t - 1} < h_t$ holds for each $t \\in [2..k]$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible total joy levels of the players among all possible card distributions.\n\n\n-----Examples-----\nInput\n4 3\n1 3 2 8 5 5 8 2 2 8 5 2\n1 2 2 5\n2 6 7\n\nOutput\n21\n\nInput\n3 3\n9 9 9 9 9 9 9 9 9\n1 2 3\n1 2 3\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example, one possible optimal card distribution is the following: Player $1$ gets cards with numbers $[1, 3, 8]$; Player $2$ gets cards with numbers $[2, 2, 8]$; Player $3$ gets cards with numbers $[2, 2, 8]$; Player $4$ gets cards with numbers $[5, 5, 5]$. \n\nThus, the answer is $2 + 6 + 6 + 7 = 21$.\n\nIn the second example, no player can get a card with his favorite number. Thus, the answer is $0$."} +{"id": "03444", "text": "You are given an array $a$ consisting of $n$ integers. You can perform the following operations arbitrary number of times (possibly, zero):\n\n Choose a pair of indices $(i, j)$ such that $|i-j|=1$ (indices $i$ and $j$ are adjacent) and set $a_i := a_i + |a_i - a_j|$; Choose a pair of indices $(i, j)$ such that $|i-j|=1$ (indices $i$ and $j$ are adjacent) and set $a_i := a_i - |a_i - a_j|$. \n\nThe value $|x|$ means the absolute value of $x$. For example, $|4| = 4$, $|-3| = 3$.\n\nYour task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.\n\nIt is guaranteed that you always can obtain the array of equal elements using such operations.\n\nNote that after each operation each element of the current array should not exceed $10^{18}$ by absolute value.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nIn the first line print one integer $k$ \u2014 the minimum number of operations required to obtain the array of equal elements.\n\nIn the next $k$ lines print operations itself. The $p$-th operation should be printed as a triple of integers $(t_p, i_p, j_p)$, where $t_p$ is either $1$ or $2$ ($1$ means that you perform the operation of the first type, and $2$ means that you perform the operation of the second type), and $i_p$ and $j_p$ are indices of adjacent elements of the array such that $1 \\le i_p, j_p \\le n$, $|i_p - j_p| = 1$. See the examples for better understanding.\n\nNote that after each operation each element of the current array should not exceed $10^{18}$ by absolute value.\n\nIf there are many possible answers, you can print any.\n\n\n-----Examples-----\nInput\n5\n2 4 6 6 6\n\nOutput\n2\n1 2 3 \n1 1 2 \n\nInput\n3\n2 8 10\n\nOutput\n2\n2 2 1 \n2 3 2 \n\nInput\n4\n1 1 1 1\n\nOutput\n0"} +{"id": "03445", "text": "Polycarp and his friends want to visit a new restaurant. The restaurant has $n$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $1$ to $n$ in the order from left to right. The state of the restaurant is described by a string of length $n$ which contains characters \"1\" (the table is occupied) and \"0\" (the table is empty).\n\nRestaurant rules prohibit people to sit at a distance of $k$ or less from each other. That is, if a person sits at the table number $i$, then all tables with numbers from $i-k$ to $i+k$ (except for the $i$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $k$.\n\nFor example, if $n=8$ and $k=2$, then: strings \"10010001\", \"10000010\", \"00000000\", \"00100000\" satisfy the rules of the restaurant; strings \"10100100\", \"10011001\", \"11111111\" do not satisfy to the rules of the restaurant, since each of them has a pair of \"1\" with a distance less than or equal to $k=2$. \n\nIn particular, if the state of the restaurant is described by a string without \"1\" or a string with one \"1\", then the requirement of the restaurant is satisfied.\n\nYou are given a binary string $s$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $s$.\n\nFind the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of \"0\" that can be replaced by \"1\" such that the requirement will still be satisfied?\n\nFor example, if $n=6$, $k=1$, $s=$\u00a0\"100010\", then the answer to the problem will be $1$, since only the table at position $3$ can be occupied such that the rules are still satisfied.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 10^4$)\u00a0\u2014 the number of test cases in the test. Then $t$ test cases follow.\n\nEach test case starts with a line containing two integers $n$ and $k$ ($1 \\le k \\le n \\le 2\\cdot 10^5$)\u00a0\u2014 the number of tables in the restaurant and the minimum allowed distance between two people.\n\nThe second line of each test case contains a binary string $s$ of length $n$ consisting of \"0\" and \"1\"\u00a0\u2014 a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant\u00a0\u2014 the difference between indices of any two \"1\" is more than $k$.\n\nThe sum of $n$ for all test cases in one test does not exceed $2\\cdot 10^5$.\n\n\n-----Output-----\n\nFor each test case output one integer\u00a0\u2014 the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $0$.\n\n\n-----Example-----\nInput\n6\n6 1\n100010\n6 2\n000000\n5 1\n10101\n3 1\n001\n2 2\n00\n1 1\n0\n\nOutput\n1\n2\n0\n1\n1\n1\n\n\n\n-----Note-----\n\nThe first test case is explained in the statement.\n\nIn the second test case, the answer is $2$, since you can choose the first and the sixth table.\n\nIn the third test case, you cannot take any free table without violating the rules of the restaurant."} +{"id": "03446", "text": "The only difference between easy and hard versions is constraints.\n\nYou are given $n$ segments on the coordinate axis $OX$. Segments can intersect, lie inside each other and even coincide. The $i$-th segment is $[l_i; r_i]$ ($l_i \\le r_i$) and it covers all integer points $j$ such that $l_i \\le j \\le r_i$.\n\nThe integer point is called bad if it is covered by strictly more than $k$ segments.\n\nYour task is to remove the minimum number of segments so that there are no bad points at all.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$) \u2014 the number of segments and the maximum number of segments by which each integer point can be covered.\n\nThe next $n$ lines contain segments. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le 2 \\cdot 10^5$) \u2014 the endpoints of the $i$-th segment.\n\n\n-----Output-----\n\nIn the first line print one integer $m$ ($0 \\le m \\le n$) \u2014 the minimum number of segments you need to remove so that there are no bad points.\n\nIn the second line print $m$ distinct integers $p_1, p_2, \\dots, p_m$ ($1 \\le p_i \\le n$) \u2014 indices of segments you remove in any order. If there are multiple answers, you can print any of them.\n\n\n-----Examples-----\nInput\n7 2\n11 11\n9 11\n7 8\n8 9\n7 8\n9 11\n7 9\n\nOutput\n3\n4 6 7 \n\nInput\n5 1\n29 30\n30 30\n29 29\n28 30\n30 30\n\nOutput\n3\n1 4 5 \n\nInput\n6 1\n2 3\n3 3\n2 3\n2 2\n2 3\n2 3\n\nOutput\n4\n1 3 5 6"} +{"id": "03447", "text": "One important contest will take place on the most famous programming platform (Topforces) very soon!\n\nThe authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).\n\nBut there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good.\n\nYour task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.\n\nYou have to answer $q$ independent queries.\n\nIf you are Python programmer, consider using PyPy instead of Python when you submit your code.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 2 \\cdot 10^5$) \u2014 the number of queries.\n\nThe first line of the query contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of problems.\n\nThe second line of the query contains $n$ integers $a_1, a_2, \\dots, a_n$ ($2 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem.\n\nIt is guaranteed that the sum of $n$ over all queries does not exceed $2 \\cdot 10^5$.\n\n\n-----Output-----\n\nFor each query print one integer \u2014 the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.\n\n\n-----Example-----\nInput\n3\n4\n5 6 15 30\n4\n10 6 30 15\n3\n3 4 6\n\nOutput\n30\n31\n10"} +{"id": "03448", "text": "You are a coach of a group consisting of $n$ students. The $i$-th student has programming skill $a_i$. All students have distinct programming skills. You want to divide them into teams in such a way that: No two students $i$ and $j$ such that $|a_i - a_j| = 1$ belong to the same team (i.e. skills of each pair of students in the same team have the difference strictly greater than $1$); the number of teams is the minimum possible. \n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 100$) \u2014 the number of queries. Then $q$ queries follow.\n\nThe first line of the query contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the number of students in the query. The second line of the query contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$, all $a_i$ are distinct), where $a_i$ is the programming skill of the $i$-th student.\n\n\n-----Output-----\n\nFor each query, print the answer on it \u2014 the minimum number of teams you can form if no two students $i$ and $j$ such that $|a_i - a_j| = 1$ may belong to the same team (i.e. skills of each pair of students in the same team has the difference strictly greater than $1$)\n\n\n-----Example-----\nInput\n4\n4\n2 10 1 20\n2\n3 6\n5\n2 3 4 99 100\n1\n42\n\nOutput\n2\n1\n2\n1\n\n\n\n-----Note-----\n\nIn the first query of the example, there are $n=4$ students with the skills $a=[2, 10, 1, 20]$. There is only one restriction here: the $1$-st and the $3$-th students can't be in the same team (because of $|a_1 - a_3|=|2-1|=1$). It is possible to divide them into $2$ teams: for example, students $1$, $2$ and $4$ are in the first team and the student $3$ in the second team.\n\nIn the second query of the example, there are $n=2$ students with the skills $a=[3, 6]$. It is possible to compose just a single team containing both students."} +{"id": "03449", "text": "You have $a$ coins of value $n$ and $b$ coins of value $1$. You always pay in exact change, so you want to know if there exist such $x$ and $y$ that if you take $x$ ($0 \\le x \\le a$) coins of value $n$ and $y$ ($0 \\le y \\le b$) coins of value $1$, then the total value of taken coins will be $S$.\n\nYou have to answer $q$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 10^4$) \u2014 the number of test cases. Then $q$ test cases follow.\n\nThe only line of the test case contains four integers $a$, $b$, $n$ and $S$ ($1 \\le a, b, n, S \\le 10^9$) \u2014 the number of coins of value $n$, the number of coins of value $1$, the value $n$ and the required total value.\n\n\n-----Output-----\n\nFor the $i$-th test case print the answer on it \u2014 YES (without quotes) if there exist such $x$ and $y$ that if you take $x$ coins of value $n$ and $y$ coins of value $1$, then the total value of taken coins will be $S$, and NO otherwise.\n\nYou may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).\n\n\n-----Example-----\nInput\n4\n1 2 3 4\n1 2 3 6\n5 2 6 27\n3 3 5 18\n\nOutput\nYES\nNO\nNO\nYES"} +{"id": "03450", "text": "Two integer sequences existed initially \u2014 one of them was strictly increasing, and the other one \u2014 strictly decreasing.\n\nStrictly increasing sequence is a sequence of integers $[x_1 < x_2 < \\dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \\dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.\n\nThey were merged into one sequence $a$. After that sequence $a$ got shuffled. For example, some of the possible resulting sequences $a$ for an increasing sequence $[1, 3, 4]$ and a decreasing sequence $[10, 4, 2]$ are sequences $[1, 2, 3, 4, 4, 10]$ or $[4, 2, 1, 10, 4, 3]$.\n\nThis shuffled sequence $a$ is given in the input.\n\nYour task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one \u2014 strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.\n\nIf there is a contradiction in the input and it is impossible to split the given sequence $a$ to increasing and decreasing sequences, print \"NO\".\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nIf there is a contradiction in the input and it is impossible to split the given sequence $a$ to increasing and decreasing sequences, print \"NO\" in the first line.\n\nOtherwise print \"YES\" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.\n\nIn the second line print $n_i$ \u2014 the number of elements in the strictly increasing sequence. $n_i$ can be zero, in this case the increasing sequence is empty.\n\nIn the third line print $n_i$ integers $inc_1, inc_2, \\dots, inc_{n_i}$ in the increasing order of its values ($inc_1 < inc_2 < \\dots < inc_{n_i}$) \u2014 the strictly increasing sequence itself. You can keep this line empty if $n_i = 0$ (or just print the empty line).\n\nIn the fourth line print $n_d$ \u2014 the number of elements in the strictly decreasing sequence. $n_d$ can be zero, in this case the decreasing sequence is empty.\n\nIn the fifth line print $n_d$ integers $dec_1, dec_2, \\dots, dec_{n_d}$ in the decreasing order of its values ($dec_1 > dec_2 > \\dots > dec_{n_d}$) \u2014 the strictly decreasing sequence itself. You can keep this line empty if $n_d = 0$ (or just print the empty line).\n\n$n_i + n_d$ should be equal to $n$ and the union of printed sequences should be a permutation of the given sequence (in case of \"YES\" answer).\n\n\n-----Examples-----\nInput\n7\n7 2 7 3 3 1 4\n\nOutput\nYES\n2\n3 7 \n5\n7 4 3 2 1 \n\nInput\n5\n4 3 1 5 3\n\nOutput\nYES\n1\n3 \n4\n5 4 3 1 \n\nInput\n5\n1 1 2 1 2\n\nOutput\nNO\n\nInput\n5\n0 1 2 3 4\n\nOutput\nYES\n0\n\n5\n4 3 2 1 0"} +{"id": "03451", "text": "Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.\n\nSummer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.\n\nThere are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ \u2014 the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not).\n\nSo they need to choose some books from the given $n$ books in such a way that:\n\n Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). \n\nThe set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set.\n\nYour task is to help them and find any suitable set of books or determine that it is impossible to find such a set.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$).\n\nThe next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \\le t_i \\le 10^4$, $0 \\le a_i, b_i \\le 1$), where:\n\n $t_i$ \u2014 the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. \n\n\n-----Output-----\n\nIf there is no solution, print only one integer -1. Otherwise print one integer $T$ \u2014 the minimum total reading time of the suitable set of books.\n\n\n-----Examples-----\nInput\n8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n\nOutput\n18\n\nInput\n5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n\nOutput\n8\n\nInput\n5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n\nOutput\n-1"} +{"id": "03452", "text": "There are $n$ products in the shop. The price of the $i$-th product is $a_i$. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.\n\nIn fact, the owner of the shop can change the price of some product $i$ in such a way that the difference between the old price of this product $a_i$ and the new price $b_i$ is at most $k$. In other words, the condition $|a_i - b_i| \\le k$ should be satisfied ($|x|$ is the absolute value of $x$).\n\nHe can change the price for each product not more than once. Note that he can leave the old prices for some products. The new price $b_i$ of each product $i$ should be positive (i.e. $b_i > 0$ should be satisfied for all $i$ from $1$ to $n$).\n\nYour task is to find out the maximum possible equal price $B$ of all productts with the restriction that for all products the condiion $|a_i - B| \\le k$ should be satisfied (where $a_i$ is the old price of the product and $B$ is the same new price of all products) or report that it is impossible to find such price $B$.\n\nNote that the chosen price $B$ should be integer.\n\nYou should answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 100$) \u2014 the number of queries. Each query is presented by two lines.\n\nThe first line of the query contains two integers $n$ and $k$ ($1 \\le n \\le 100, 1 \\le k \\le 10^8$) \u2014 the number of products and the value $k$. The second line of the query contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^8$), where $a_i$ is the price of the $i$-th product.\n\n\n-----Output-----\n\nPrint $q$ integers, where the $i$-th integer is the answer $B$ on the $i$-th query.\n\nIf it is impossible to equalize prices of all given products with restriction that for all products the condition $|a_i - B| \\le k$ should be satisfied (where $a_i$ is the old price of the product and $B$ is the new equal price of all products), print -1. Otherwise print the maximum possible equal price of all products.\n\n\n-----Example-----\nInput\n4\n5 1\n1 1 2 3 1\n4 2\n6 4 8 5\n2 2\n1 6\n3 5\n5 2 5\n\nOutput\n2\n6\n-1\n7\n\n\n\n-----Note-----\n\nIn the first example query you can choose the price $B=2$. It is easy to see that the difference between each old price and each new price $B=2$ is no more than $1$.\n\nIn the second example query you can choose the price $B=6$ and then all the differences between old and new price $B=6$ will be no more than $2$.\n\nIn the third example query you cannot choose any suitable price $B$. For any value $B$ at least one condition out of two will be violated: $|1-B| \\le 2$, $|6-B| \\le 2$.\n\nIn the fourth example query all values $B$ between $1$ and $7$ are valid. But the maximum is $7$, so it's the answer."} +{"id": "03453", "text": "You are given an array $a$ consisting of $n$ integers. Each $a_i$ is one of the six following numbers: $4, 8, 15, 16, 23, 42$.\n\nYour task is to remove the minimum number of elements to make this array good.\n\nAn array of length $k$ is called good if $k$ is divisible by $6$ and it is possible to split it into $\\frac{k}{6}$ subsequences $4, 8, 15, 16, 23, 42$.\n\nExamples of good arrays: $[4, 8, 15, 16, 23, 42]$ (the whole array is a required sequence); $[4, 8, 4, 15, 16, 8, 23, 15, 16, 42, 23, 42]$ (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements); $[]$ (the empty array is good). \n\nExamples of bad arrays: $[4, 8, 15, 16, 42, 23]$ (the order of elements should be exactly $4, 8, 15, 16, 23, 42$); $[4, 8, 15, 16, 23, 42, 4]$ (the length of the array is not divisible by $6$); $[4, 8, 15, 16, 23, 42, 4, 8, 15, 16, 23, 23]$ (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence). \n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 5 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ (each $a_i$ is one of the following numbers: $4, 8, 15, 16, 23, 42$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum number of elements you have to remove to obtain a good array.\n\n\n-----Examples-----\nInput\n5\n4 8 15 16 23\n\nOutput\n5\n\nInput\n12\n4 8 4 15 16 8 23 15 16 42 23 42\n\nOutput\n0\n\nInput\n15\n4 8 4 8 15 16 8 16 23 15 16 4 42 23 42\n\nOutput\n3"} +{"id": "03454", "text": "You are given two integers $a$ and $b$.\n\nIn one move, you can choose some integer $k$ from $1$ to $10$ and add it to $a$ or subtract it from $a$. In other words, you choose an integer $k \\in [1; 10]$ and perform $a := a + k$ or $a := a - k$. You may use different values of $k$ in different moves.\n\nYour task is to find the minimum number of moves required to obtain $b$ from $a$.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2 \\cdot 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains two integers $a$ and $b$ ($1 \\le a, b \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case, print the answer: the minimum number of moves required to obtain $b$ from $a$.\n\n\n-----Example-----\nInput\n6\n5 5\n13 42\n18 4\n1337 420\n123456789 1000000000\n100500 9000\n\nOutput\n0\n3\n2\n92\n87654322\n9150\n\n\n\n-----Note-----\n\nIn the first test case of the example, you don't need to do anything.\n\nIn the second test case of the example, the following sequence of moves can be applied: $13 \\rightarrow 23 \\rightarrow 32 \\rightarrow 42$ (add $10$, add $9$, add $10$).\n\nIn the third test case of the example, the following sequence of moves can be applied: $18 \\rightarrow 10 \\rightarrow 4$ (subtract $8$, subtract $6$)."} +{"id": "03455", "text": "You are given three integers $x, y$ and $n$. Your task is to find the maximum integer $k$ such that $0 \\le k \\le n$ that $k \\bmod x = y$, where $\\bmod$ is modulo operation. Many programming languages use percent operator % to implement it.\n\nIn other words, with given $x, y$ and $n$ you need to find the maximum possible integer from $0$ to $n$ that has the remainder $y$ modulo $x$.\n\nYou have to answer $t$ independent test cases. It is guaranteed that such $k$ exists for each test case.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 5 \\cdot 10^4$) \u2014 the number of test cases. The next $t$ lines contain test cases.\n\nThe only line of the test case contains three integers $x, y$ and $n$ ($2 \\le x \\le 10^9;~ 0 \\le y < x;~ y \\le n \\le 10^9$).\n\nIt can be shown that such $k$ always exists under the given constraints.\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 maximum non-negative integer $k$ such that $0 \\le k \\le n$ and $k \\bmod x = y$. It is guaranteed that the answer always exists.\n\n\n-----Example-----\nInput\n7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999\n\nOutput\n12339\n0\n15\n54306\n999999995\n185\n999999998\n\n\n\n-----Note-----\n\nIn the first test case of the example, the answer is $12339 = 7 \\cdot 1762 + 5$ (thus, $12339 \\bmod 7 = 5$). It is obvious that there is no greater integer not exceeding $12345$ which has the remainder $5$ modulo $7$."} +{"id": "03456", "text": "International Women's Day is coming soon! Polycarp is preparing for the holiday.\n\nThere are $n$ candy boxes in the shop for sale. The $i$-th box contains $d_i$ candies.\n\nPolycarp wants to prepare the maximum number of gifts for $k$ girls. Each gift will consist of exactly two boxes. The girls should be able to share each gift equally, so the total amount of candies in a gift (in a pair of boxes) should be divisible by $k$. In other words, two boxes $i$ and $j$ ($i \\ne j$) can be combined as a gift if $d_i + d_j$ is divisible by $k$.\n\nHow many boxes will Polycarp be able to give? Of course, each box can be a part of no more than one gift. Polycarp cannot use boxes \"partially\" or redistribute candies between them. \n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n \\le 2 \\cdot 10^5, 1 \\le k \\le 100$) \u2014 the number the boxes and the number the girls.\n\nThe second line of the input contains $n$ integers $d_1, d_2, \\dots, d_n$ ($1 \\le d_i \\le 10^9$), where $d_i$ is the number of candies in the $i$-th box.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of the boxes Polycarp can give as gifts.\n\n\n-----Examples-----\nInput\n7 2\n1 2 2 3 2 4 10\n\nOutput\n6\n\nInput\n8 2\n1 2 2 3 2 4 6 10\n\nOutput\n8\n\nInput\n7 3\n1 2 2 3 2 4 5\n\nOutput\n4\n\n\n\n-----Note-----\n\nIn the first example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $(2, 3)$; $(5, 6)$; $(1, 4)$. \n\nSo the answer is $6$.\n\nIn the second example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $(6, 8)$; $(2, 3)$; $(1, 4)$; $(5, 7)$. \n\nSo the answer is $8$.\n\nIn the third example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $(1, 2)$; $(6, 7)$. \n\nSo the answer is $4$."} +{"id": "03457", "text": "You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).\nPrint the string S after lowercasing the K-th character in it.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 50\n - 1 \u2264 K \u2264 N\n - S is a string of length N consisting of A, B and C.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nS\n\n-----Output-----\nPrint the string S after lowercasing the K-th character in it.\n\n-----Sample Input-----\n3 1\nABC\n\n-----Sample Output-----\naBC\n"} +{"id": "03458", "text": "You have decided to write a book introducing good restaurants.\nThere are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.\nNo two restaurants have the same score.\nYou want to introduce the restaurants in the following order:\n - The restaurants are arranged in lexicographical order of the names of their cities.\n - If there are multiple restaurants in the same city, they are arranged in descending order of score.\nPrint the identification numbers of the restaurants in the order they are introduced in the book.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 100\n - S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.\n - 0 \u2264 P_i \u2264 100\n - P_i is an integer.\n - P_i \u2260 P_j (1 \u2264 i < j \u2264 N)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS_1 P_1\n:\nS_N P_N\n\n-----Output-----\nPrint N lines. The i-th line (1 \u2264 i \u2264 N) should contain the identification number of the restaurant that is introduced i-th in the book.\n\n-----Sample Input-----\n6\nkhabarovsk 20\nmoscow 10\nkazan 50\nkazan 35\nmoscow 60\nkhabarovsk 40\n\n-----Sample Output-----\n3\n4\n6\n1\n5\n2\n\nThe lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2."} +{"id": "03459", "text": "Takahashi has K 500-yen coins. (Yen is the currency of Japan.)\nIf these coins add up to X yen or more, print Yes; otherwise, print No.\n\n-----Constraints-----\n - 1 \\leq K \\leq 100\n - 1 \\leq X \\leq 10^5\n\n-----Input-----\nInput is given from Standard Input in the following format:\nK X\n\n-----Output-----\nIf the coins add up to X yen or more, print Yes; otherwise, print No.\n\n-----Sample Input-----\n2 900\n\n-----Sample Output-----\nYes\n\nTwo 500-yen coins add up to 1000 yen, which is not less than X = 900 yen."} +{"id": "03460", "text": "Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:\n - Throw the die. The current score is the result of the die.\n - As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.\n - The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.\nYou are given N and K. Find the probability that Snuke wins the game.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 10^5\n - 1 \u2264 K \u2264 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\n\n-----Output-----\nPrint the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.\n\n-----Sample Input-----\n3 10\n\n-----Sample Output-----\n0.145833333333\n\n - If the die shows 1, Snuke needs to get four consecutive heads from four coin flips to obtain a score of 10 or above. The probability of this happening is \\frac{1}{3} \\times (\\frac{1}{2})^4 = \\frac{1}{48}.\n - If the die shows 2, Snuke needs to get three consecutive heads from three coin flips to obtain a score of 10 or above. The probability of this happening is \\frac{1}{3} \\times (\\frac{1}{2})^3 = \\frac{1}{24}.\n - If the die shows 3, Snuke needs to get two consecutive heads from two coin flips to obtain a score of 10 or above. The probability of this happening is \\frac{1}{3} \\times (\\frac{1}{2})^2 = \\frac{1}{12}.\nThus, the probability that Snuke wins is \\frac{1}{48} + \\frac{1}{24} + \\frac{1}{12} = \\frac{7}{48} \\simeq 0.1458333333."} +{"id": "03461", "text": "Given is a string S representing the day of the week today.\nS is SUN, MON, TUE, WED, THU, FRI, or SAT, for Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, respectively.\nAfter how many days is the next Sunday (tomorrow or later)?\n\n-----Constraints-----\n - S is SUN, MON, TUE, WED, THU, FRI, or SAT.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the number of days before the next Sunday.\n\n-----Sample Input-----\nSAT\n\n-----Sample Output-----\n1\n\nIt is Saturday today, and tomorrow will be Sunday."} +{"id": "03462", "text": "The development of algae in a pond is as follows.\nLet the total weight of the algae at the beginning of the year i be x_i gram. For i\u22652000, the following formula holds:\n - x_{i+1} = rx_i - D\nYou are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.\n\n-----Constraints-----\n - 2 \u2264 r \u2264 5\n - 1 \u2264 D \u2264 100\n - D < x_{2000} \u2264 200\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nr D x_{2000}\n\n-----Output-----\nPrint 10 lines. The i-th line (1 \u2264 i \u2264 10) should contain x_{2000+i} as an integer.\n\n-----Sample Input-----\n2 10 20\n\n-----Sample Output-----\n30\n50\n90\n170\n330\n650\n1290\n2570\n5130\n10250\n\nFor example, x_{2001} = rx_{2000} - D = 2 \\times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \\times 30 - 10 = 50."} +{"id": "03463", "text": "You are given an array $d_1, d_2, \\dots, d_n$ consisting of $n$ integer numbers.\n\nYour task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. \n\nLet the sum of elements of the first part be $sum_1$, the sum of elements of the second part be $sum_2$ and the sum of elements of the third part be $sum_3$. Among all possible ways to split the array you have to choose a way such that $sum_1 = sum_3$ and $sum_1$ is maximum possible.\n\nMore formally, if the first part of the array contains $a$ elements, the second part of the array contains $b$ elements and the third part contains $c$ elements, then:\n\n$$sum_1 = \\sum\\limits_{1 \\le i \\le a}d_i,$$ $$sum_2 = \\sum\\limits_{a + 1 \\le i \\le a + b}d_i,$$ $$sum_3 = \\sum\\limits_{a + b + 1 \\le i \\le a + b + c}d_i.$$\n\nThe sum of an empty array is $0$.\n\nYour task is to find a way to split the array such that $sum_1 = sum_3$ and $sum_1$ is maximum possible.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in the array $d$.\n\nThe second line of the input contains $n$ integers $d_1, d_2, \\dots, d_n$ ($1 \\le d_i \\le 10^9$) \u2014 the elements of the array $d$.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the maximum possible value of $sum_1$, considering that the condition $sum_1 = sum_3$ must be met.\n\nObviously, at least one valid way to split the array exists (use $a=c=0$ and $b=n$).\n\n\n-----Examples-----\nInput\n5\n1 3 1 1 4\n\nOutput\n5\n\nInput\n5\n1 3 2 1 4\n\nOutput\n4\n\nInput\n3\n4 1 2\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example there is only one possible splitting which maximizes $sum_1$: $[1, 3, 1], [~], [1, 4]$.\n\nIn the second example the only way to have $sum_1=4$ is: $[1, 3], [2, 1], [4]$.\n\nIn the third example there is only one way to split the array: $[~], [4, 1, 2], [~]$."} +{"id": "03464", "text": "You are given three positive (i.e. strictly greater than zero) integers $x$, $y$ and $z$.\n\nYour task is to find positive integers $a$, $b$ and $c$ such that $x = \\max(a, b)$, $y = \\max(a, c)$ and $z = \\max(b, c)$, or determine that it is impossible to find such $a$, $b$ and $c$.\n\nYou have to answer $t$ independent test cases. Print required $a$, $b$ and $c$ in any (arbitrary) order.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2 \\cdot 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains three integers $x$, $y$, and $z$ ($1 \\le x, y, z \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case, print the answer: \"NO\" in the only line of the output if a solution doesn't exist; or \"YES\" in the first line and any valid triple of positive integers $a$, $b$ and $c$ ($1 \\le a, b, c \\le 10^9$) in the second line. You can print $a$, $b$ and $c$ in any order. \n\n\n-----Example-----\nInput\n5\n3 2 3\n100 100 100\n50 49 49\n10 30 20\n1 1000000000 1000000000\n\nOutput\nYES\n3 2 1\nYES\n100 100 100\nNO\nNO\nYES\n1 1 1000000000"} +{"id": "03465", "text": "Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.\n\nMaksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.\n\nMaksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.\n\nEach time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n$, $m$, $k$ ($1 \\le n, m \\le 2 \\cdot 10^5$, $1 \\le k \\le 10^9$) \u2014 the number of objects, the number of boxes and the size of each box.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le k$), where $a_i$ is the size of the $i$-th object.\n\n\n-----Output-----\n\nPrint the maximum number of objects Maksim can pack using the algorithm described in the problem statement.\n\n\n-----Examples-----\nInput\n5 2 6\n5 2 1 4 2\n\nOutput\n4\n\nInput\n5 1 4\n4 2 3 4 1\n\nOutput\n1\n\nInput\n5 3 3\n1 2 3 1 1\n\nOutput\n5\n\n\n\n-----Note-----\n\nIn the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.\n\nIn the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).\n\nIn the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$."} +{"id": "03466", "text": "Recently, Norge found a string $s = s_1 s_2 \\ldots s_n$ consisting of $n$ lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string $s$. Yes, all $\\frac{n (n + 1)}{2}$ of them!\n\nA substring of $s$ is a non-empty string $x = s[a \\ldots b] = s_{a} s_{a + 1} \\ldots s_{b}$ ($1 \\leq a \\leq b \\leq n$). For example, \"auto\" and \"ton\" are substrings of \"automaton\".\n\nShortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only $k$ Latin letters $c_1, c_2, \\ldots, c_k$ out of $26$.\n\nAfter that, Norge became interested in how many substrings of the string $s$ he could still type using his broken keyboard. Help him to find this number.\n\n\n-----Input-----\n\nThe first line contains two space-separated integers $n$ and $k$ ($1 \\leq n \\leq 2 \\cdot 10^5$, $1 \\leq k \\leq 26$) \u2014 the length of the string $s$ and the number of Latin letters still available on the keyboard.\n\nThe second line contains the string $s$ consisting of exactly $n$ lowercase Latin letters.\n\nThe third line contains $k$ space-separated distinct lowercase Latin letters $c_1, c_2, \\ldots, c_k$ \u2014 the letters still available on the keyboard.\n\n\n-----Output-----\n\nPrint a single number \u2014 the number of substrings of $s$ that can be typed using only available letters $c_1, c_2, \\ldots, c_k$.\n\n\n-----Examples-----\nInput\n7 2\nabacaba\na b\n\nOutput\n12\n\nInput\n10 3\nsadfaasdda\nf a d\n\nOutput\n21\n\nInput\n7 1\naaaaaaa\nb\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example Norge can print substrings $s[1\\ldots2]$, $s[2\\ldots3]$, $s[1\\ldots3]$, $s[1\\ldots1]$, $s[2\\ldots2]$, $s[3\\ldots3]$, $s[5\\ldots6]$, $s[6\\ldots7]$, $s[5\\ldots7]$, $s[5\\ldots5]$, $s[6\\ldots6]$, $s[7\\ldots7]$."} +{"id": "03467", "text": "Recall that the sequence $b$ is a a subsequence of the sequence $a$ if $b$ can be derived from $a$ by removing zero or more elements without changing the order of the remaining elements. For example, if $a=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.\n\nYou are given a sequence $a$ consisting of $n$ positive and negative elements (there is no zeros in the sequence).\n\nYour task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.\n\nIn other words, if the maximum length of alternating subsequence is $k$ then your task is to find the maximum sum of elements of some alternating subsequence of length $k$.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$. The second line of the test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($-10^9 \\le a_i \\le 10^9, a_i \\ne 0$), where $a_i$ is the $i$-th element of $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$ ($\\sum n \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 the maximum sum of the maximum by size (length) alternating subsequence of $a$.\n\n\n-----Example-----\nInput\n4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000\n\nOutput\n2\n-1\n6\n-2999999997\n\n\n\n-----Note-----\n\nIn the first test case of the example, one of the possible answers is $[1, 2, \\underline{3}, \\underline{-1}, -2]$.\n\nIn the second test case of the example, one of the possible answers is $[-1, -2, \\underline{-1}, -3]$.\n\nIn the third test case of the example, one of the possible answers is $[\\underline{-2}, 8, 3, \\underline{8}, \\underline{-4}, -15, \\underline{5}, \\underline{-2}, -3, \\underline{1}]$.\n\nIn the fourth test case of the example, one of the possible answers is $[\\underline{1}, \\underline{-1000000000}, \\underline{1}, \\underline{-1000000000}, \\underline{1}, \\underline{-1000000000}]$."} +{"id": "03468", "text": "You are given an undirected unweighted connected graph consisting of $n$ vertices and $m$ edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.\n\nYour task is to find any spanning tree of this graph such that the maximum degree over all vertices is maximum possible. Recall that the degree of a vertex is the number of edges incident to it.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $m$ ($2 \\le n \\le 2 \\cdot 10^5$, $n - 1 \\le m \\le min(2 \\cdot 10^5, \\frac{n(n-1)}{2})$) \u2014 the number of vertices and edges, respectively.\n\nThe following $m$ lines denote edges: edge $i$ is represented by a pair of integers $v_i$, $u_i$ ($1 \\le v_i, u_i \\le n$, $u_i \\ne v_i$), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair ($v_i, u_i$) there are no other pairs ($v_i, u_i$) or ($u_i, v_i$) in the list of edges, and for each pair $(v_i, u_i)$ the condition $v_i \\ne u_i$ is satisfied.\n\n\n-----Output-----\n\nPrint $n-1$ lines describing the edges of a spanning tree such that the maximum degree over all vertices is maximum possible. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge $(v, u)$ is considered the same as the edge $(u, v)$).\n\nIf there are multiple possible answers, print any of them.\n\n\n-----Examples-----\nInput\n5 5\n1 2\n2 3\n3 5\n4 3\n1 5\n\nOutput\n3 5\n2 1\n3 2\n3 4\n\nInput\n4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n\nOutput\n4 1\n1 2\n1 3\n\nInput\n8 9\n1 2\n2 3\n2 5\n1 6\n3 4\n6 5\n4 5\n2 7\n5 8\n\nOutput\n3 2\n2 5\n8 5\n6 1\n2 7\n1 2\n3 4\n\n\n\n-----Note-----\n\nPicture corresponding to the first example: [Image]\n\nIn this example the number of edges of spanning tree incident to the vertex $3$ is $3$. It is the maximum degree over all vertices of the spanning tree. It is easy to see that we cannot obtain a better answer.\n\nPicture corresponding to the second example: [Image]\n\nIn this example the number of edges of spanning tree incident to the vertex $1$ is $3$. It is the maximum degree over all vertices of the spanning tree. It is easy to see that we cannot obtain a better answer.\n\nPicture corresponding to the third example: [Image]\n\nIn this example the number of edges of spanning tree incident to the vertex $2$ is $4$. It is the maximum degree over all vertices of the spanning tree. It is easy to see that we cannot obtain a better answer. But because this example is symmetric, we can choose almost the same spanning tree but with vertex $5$ instead of $2$."} +{"id": "03469", "text": "You are given a board of size $n \\times n$, where $n$ is odd (not divisible by $2$). Initially, each cell of the board contains one figure.\n\nIn one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell $(i, j)$ you can move the figure to cells: $(i - 1, j - 1)$; $(i - 1, j)$; $(i - 1, j + 1)$; $(i, j - 1)$; $(i, j + 1)$; $(i + 1, j - 1)$; $(i + 1, j)$; $(i + 1, j + 1)$; \n\nOf course, you can not move figures to cells out of the board. It is allowed that after a move there will be several figures in one cell.\n\nYour task is to find the minimum number of moves needed to get all the figures into one cell (i.e. $n^2-1$ cells should contain $0$ figures and one cell should contain $n^2$ figures).\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 200$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains one integer $n$ ($1 \\le n < 5 \\cdot 10^5$) \u2014 the size of the board. It is guaranteed that $n$ is odd (not divisible by $2$).\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $5 \\cdot 10^5$ ($\\sum n \\le 5 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each test case print the answer \u2014 the minimum number of moves needed to get all the figures into one cell.\n\n\n-----Example-----\nInput\n3\n1\n5\n499993\n\nOutput\n0\n40\n41664916690999888"} +{"id": "03470", "text": "You are given an array $a$ consisting of $n$ integers. In one move, you can jump from the position $i$ to the position $i - a_i$ (if $1 \\le i - a_i$) or to the position $i + a_i$ (if $i + a_i \\le n$).\n\nFor each position $i$ from $1$ to $n$ you want to know the minimum the number of moves required to reach any position $j$ such that $a_j$ has the opposite parity from $a_i$ (i.e. if $a_i$ is odd then $a_j$ has to be even and vice versa).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nPrint $n$ integers $d_1, d_2, \\dots, d_n$, where $d_i$ is the minimum the number of moves required to reach any position $j$ such that $a_j$ has the opposite parity from $a_i$ (i.e. if $a_i$ is odd then $a_j$ has to be even and vice versa) or -1 if it is impossible to reach such a position.\n\n\n-----Example-----\nInput\n10\n4 5 7 6 7 5 4 4 6 4\n\nOutput\n1 1 1 2 -1 1 1 3 1 1"} +{"id": "03471", "text": "You are given one integer number $n$. Find three distinct integers $a, b, c$ such that $2 \\le a, b, c$ and $a \\cdot b \\cdot c = n$ or say that it is impossible to do it.\n\nIf there are several answers, you can print any.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThe next $n$ lines describe test cases. The $i$-th test case is given on a new line as one integer $n$ ($2 \\le n \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case, print the answer on it. Print \"NO\" if it is impossible to represent $n$ as $a \\cdot b \\cdot c$ for some distinct integers $a, b, c$ such that $2 \\le a, b, c$.\n\nOtherwise, print \"YES\" and any possible such representation.\n\n\n-----Example-----\nInput\n5\n64\n32\n97\n2\n12345\n\nOutput\nYES\n2 4 8 \nNO\nNO\nNO\nYES\n3 5 823"} +{"id": "03472", "text": "Nikolay got a string $s$ of even length $n$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $1$ to $n$.\n\nHe wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.\n\nThe prefix of string $s$ of length $l$ ($1 \\le l \\le n$) is a string $s[1..l]$.\n\nFor example, for the string $s=$\"abba\" there are two prefixes of the even length. The first is $s[1\\dots2]=$\"ab\" and the second $s[1\\dots4]=$\"abba\". Both of them have the same number of 'a' and 'b'.\n\nYour task is to calculate the minimum number of operations Nikolay has to perform with the string $s$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.\n\n\n-----Input-----\n\nThe first line of the input contains one even integer $n$ $(2 \\le n \\le 2\\cdot10^{5})$ \u2014 the length of string $s$.\n\nThe second line of the input contains the string $s$ of length $n$, which consists only of lowercase Latin letters 'a' and 'b'.\n\n\n-----Output-----\n\nIn the first line print the minimum number of operations Nikolay has to perform with the string $s$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.\n\nIn the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.\n\n\n-----Examples-----\nInput\n4\nbbbb\n\nOutput\n2\nabba\n\nInput\n6\nababab\n\nOutput\n0\nababab\n\nInput\n2\naa\n\nOutput\n1\nba\n\n\n\n-----Note-----\n\nIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. \n\nIn the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'."} +{"id": "03473", "text": "Maksim walks on a Cartesian plane. Initially, he stands at the point $(0, 0)$ and in one move he can go to any of four adjacent points (left, right, up, down). For example, if Maksim is currently at the point $(0, 0)$, he can go to any of the following points in one move: $(1, 0)$; $(0, 1)$; $(-1, 0)$; $(0, -1)$. \n\nThere are also $n$ distinct key points at this plane. The $i$-th point is $p_i = (x_i, y_i)$. It is guaranteed that $0 \\le x_i$ and $0 \\le y_i$ and there is no key point $(0, 0)$.\n\nLet the first level points be such points that $max(x_i, y_i) = 1$, the second level points be such points that $max(x_i, y_i) = 2$ and so on. Maksim wants to visit all the key points. But he shouldn't visit points of level $i + 1$ if he does not visit all the points of level $i$. He starts visiting the points from the minimum level of point from the given set.\n\nThe distance between two points $(x_1, y_1)$ and $(x_2, y_2)$ is $|x_1 - x_2| + |y_1 - y_2|$ where $|v|$ is the absolute value of $v$.\n\nMaksim wants to visit all the key points in such a way that the total distance he walks will be minimum possible. Your task is to find this distance.\n\nIf you are Python programmer, consider using PyPy instead of Python when you submit your code.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of key points.\n\nEach of the next $n$ lines contains two integers $x_i$, $y_i$ ($0 \\le x_i, y_i \\le 10^9$) \u2014 $x$-coordinate of the key point $p_i$ and $y$-coordinate of the key point $p_i$. It is guaranteed that all the points are distinct and the point $(0, 0)$ is not in this set.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible total distance Maksim has to travel if he needs to visit all key points in a way described above.\n\n\n-----Examples-----\nInput\n8\n2 2\n1 4\n2 3\n3 1\n3 4\n1 1\n4 3\n1 2\n\nOutput\n15\n\nInput\n5\n2 1\n1 0\n2 0\n3 2\n0 3\n\nOutput\n9\n\n\n\n-----Note-----\n\nThe picture corresponding to the first example: [Image]\n\nThere is one of the possible answers of length $15$.\n\nThe picture corresponding to the second example: [Image]\n\nThere is one of the possible answers of length $9$."} +{"id": "03474", "text": "Takahashi is solving quizzes. He has easily solved all but the last one.\nThe last quiz has three choices: 1, 2, and 3.\nWith his supernatural power, Takahashi has found out that the choices A and B are both wrong.\nPrint the correct choice for this problem.\n\n-----Constraints-----\n - Each of the numbers A and B is 1, 2, or 3.\n - A and B are different.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA\nB\n\n-----Output-----\nPrint the correct choice.\n\n-----Sample Input-----\n3\n1\n\n-----Sample Output-----\n2\n\nWhen we know 3 and 1 are both wrong, the correct choice is 2."} +{"id": "03475", "text": "Given is a positive integer L.\nFind the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.\n\n-----Constraints-----\n - 1 \u2264 L \u2264 1000\n - L is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nL\n\n-----Output-----\nPrint the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.\nYour output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.\n\n-----Sample Input-----\n3\n\n-----Sample Output-----\n1.000000000000\n\nFor example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.\nOn the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater."} +{"id": "03476", "text": "In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the currency of Japan) has started a business in programming education.\n\nOne day, there was an exam where a one-year-old child must write a program that prints Hello World, and a two-year-old child must write a program that receives integers A, B and prints A+B.\n\nTakahashi, who is taking this exam, suddenly forgets his age.\n\nHe decides to write a program that first receives his age N (1 or 2) as input, then prints Hello World if N=1, and additionally receives integers A, B and prints A+B if N=2.\n\nWrite this program for him. \n\n-----Constraints-----\n - N is 1 or 2.\n - A is an integer between 1 and 9 (inclusive).\n - B is an integer between 1 and 9 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in one of the following formats: \n1\n\n2\nA\nB\n\n-----Output-----\nIf N=1, print Hello World; if N=2, print A+B. \n\n-----Sample Input-----\n1\n\n-----Sample Output-----\nHello World\n\nAs N=1, Takahashi is one year old. Thus, we should print Hello World."} +{"id": "03477", "text": "Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller?\n\n-----Constraints-----\n - 1 \\leq a \\leq 9\n - 1 \\leq b \\leq 9\n - a and b are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\na b\n\n-----Output-----\nPrint the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.)\n\n-----Sample Input-----\n4 3\n\n-----Sample Output-----\n3333\n\nWe have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller."} +{"id": "03478", "text": "Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.\n\n-----Constraints-----\n - C is a lowercase English letter that is not z.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nC\n\n-----Output-----\nPrint the letter that follows C in alphabetical order.\n\n-----Sample Input-----\na\n\n-----Sample Output-----\nb\n\na is followed by b."} +{"id": "03479", "text": "Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.\n\n-----Constraints-----\n - S and T are strings consisting of lowercase English letters.\n - The lengths of S and T are between 1 and 100 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS T\n\n-----Output-----\nPrint the resulting string.\n\n-----Sample Input-----\noder atc\n\n-----Sample Output-----\natcoder\n\nWhen S = oder and T = atc, concatenating T and S in this order results in atcoder."} +{"id": "03480", "text": "Polycarp has an array $a$ consisting of $n$ integers.\n\nHe wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains $n-1$ elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.\n\nFormally: If it is the first move, he chooses any element and deletes it; If it is the second or any next move: if the last deleted element was odd, Polycarp chooses any even element and deletes it; if the last deleted element was even, Polycarp chooses any odd element and deletes it. If after some move Polycarp cannot make a move, the game ends. \n\nPolycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.\n\nHelp Polycarp find this value.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2000$) \u2014 the number of elements of $a$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 10^6$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible sum of non-deleted elements of the array after end of the game.\n\n\n-----Examples-----\nInput\n5\n1 5 7 8 2\n\nOutput\n0\n\nInput\n6\n5 1 2 4 6 3\n\nOutput\n0\n\nInput\n2\n1000000 1000000\n\nOutput\n1000000"} +{"id": "03481", "text": "There are $n$ monsters standing in a row numbered from $1$ to $n$. The $i$-th monster has $h_i$ health points (hp). You have your attack power equal to $a$ hp and your opponent has his attack power equal to $b$ hp.\n\nYou and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to $0$.\n\nThe fight with a monster happens in turns. You hit the monster by $a$ hp. If it is dead after your hit, you gain one point and you both proceed to the next monster. Your opponent hits the monster by $b$ hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster. \n\nYou have some secret technique to force your opponent to skip his turn. You can use this technique at most $k$ times in total (for example, if there are two monsters and $k=4$, then you can use the technique $2$ times on the first monster and $1$ time on the second monster, but not $2$ times on the first monster and $3$ times on the second monster).\n\nYour task is to determine the maximum number of points you can gain if you use the secret technique optimally.\n\n\n-----Input-----\n\nThe first line of the input contains four integers $n, a, b$ and $k$ ($1 \\le n \\le 2 \\cdot 10^5, 1 \\le a, b, k \\le 10^9$) \u2014 the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.\n\nThe second line of the input contains $n$ integers $h_1, h_2, \\dots, h_n$ ($1 \\le h_i \\le 10^9$), where $h_i$ is the health points of the $i$-th monster.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum number of points you can gain if you use the secret technique optimally.\n\n\n-----Examples-----\nInput\n6 2 3 3\n7 10 50 12 1 8\n\nOutput\n5\n\nInput\n1 1 100 99\n100\n\nOutput\n1\n\nInput\n7 4 2 1\n1 3 5 4 2 7 6\n\nOutput\n6"} +{"id": "03482", "text": "You are given an array consisting of $n$ integers $a_1, a_2, \\dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.\n\nIn a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.\n\nLet's calculate $c_r$ ($0 \\le r \\le m-1)$ \u2014 the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.\n\nYour task is to change the array in such a way that $c_0 = c_1 = \\dots = c_{m-1} = \\frac{n}{m}$.\n\nFind the minimum number of moves to satisfy the above requirement.\n\n\n-----Input-----\n\nThe first line of input contains two integers $n$ and $m$ ($1 \\le n \\le 2 \\cdot 10^5, 1 \\le m \\le n$). It is guaranteed that $m$ is a divisor of $n$.\n\nThe second line of input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 10^9$), the elements of the array.\n\n\n-----Output-----\n\nIn the first line, print a single integer \u2014 the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\\frac{n}{m}$.\n\nIn the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.\n\n\n-----Examples-----\nInput\n6 3\n3 2 0 6 10 12\n\nOutput\n3\n3 2 0 7 10 14 \n\nInput\n4 2\n0 1 2 3\n\nOutput\n0\n0 1 2 3"} +{"id": "03483", "text": "The only difference between easy and hard versions is constraints.\n\nIvan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions \u2014 and he won't start playing until he gets all of them.\n\nEach day (during the morning) Ivan earns exactly one burle.\n\nThere are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening).\n\nIvan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles.\n\nThere are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day.\n\nIvan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n, m \\le 1000$) \u2014 the number of types of microtransactions and the number of special offers in the game shop.\n\nThe second line of the input contains $n$ integers $k_1, k_2, \\dots, k_n$ ($0 \\le k_i \\le 1000$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $1000$.\n\nThe next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \\le d_j \\le 1000, 1 \\le t_j \\le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum day when Ivan can order all microtransactions he wants and actually start playing.\n\n\n-----Examples-----\nInput\n5 6\n1 2 0 2 0\n2 4\n3 3\n1 5\n1 2\n1 5\n2 3\n\nOutput\n8\n\nInput\n5 3\n4 2 1 3 2\n3 5\n4 2\n2 5\n\nOutput\n20"} +{"id": "03484", "text": "You are given $4n$ sticks, the length of the $i$-th stick is $a_i$.\n\nYou have to create $n$ rectangles, each rectangle will consist of exactly $4$ sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length.\n\nYou want to all rectangles to have equal area. The area of the rectangle with sides $a$ and $b$ is $a \\cdot b$.\n\nYour task is to say if it is possible to create exactly $n$ rectangles of equal area or not.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 500$) \u2014 the number of queries. Then $q$ queries follow.\n\nThe first line of the query contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the number of rectangles.\n\nThe second line of the query contains $4n$ integers $a_1, a_2, \\dots, a_{4n}$ ($1 \\le a_i \\le 10^4$), where $a_i$ is the length of the $i$-th stick.\n\n\n-----Output-----\n\nFor each query print the answer to it. If it is impossible to create exactly $n$ rectangles of equal area using given sticks, print \"NO\". Otherwise print \"YES\".\n\n\n-----Example-----\nInput\n5\n1\n1 1 10 10\n2\n10 5 2 10 1 1 2 5\n2\n10 5 1 10 5 1 1 1\n2\n1 1 1 1 1 1 1 1\n1\n10000 10000 10000 10000\n\nOutput\nYES\nYES\nNO\nYES\nYES"} +{"id": "03485", "text": "You are given a connected undirected weighted graph consisting of $n$ vertices and $m$ edges.\n\nYou need to print the $k$-th smallest shortest path in this graph (paths from the vertex to itself are not counted, paths from $i$ to $j$ and from $j$ to $i$ are counted as one).\n\nMore formally, if $d$ is the matrix of shortest paths, where $d_{i, j}$ is the length of the shortest path between vertices $i$ and $j$ ($1 \\le i < j \\le n$), then you need to print the $k$-th element in the sorted array consisting of all $d_{i, j}$, where $1 \\le i < j \\le n$.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n, m$ and $k$ ($2 \\le n \\le 2 \\cdot 10^5$, $n - 1 \\le m \\le \\min\\Big(\\frac{n(n-1)}{2}, 2 \\cdot 10^5\\Big)$, $1 \\le k \\le \\min\\Big(\\frac{n(n-1)}{2}, 400\\Big)$\u00a0\u2014 the number of vertices in the graph, the number of edges in the graph and the value of $k$, correspondingly.\n\nThen $m$ lines follow, each containing three integers $x$, $y$ and $w$ ($1 \\le x, y \\le n$, $1 \\le w \\le 10^9$, $x \\ne y$) denoting an edge between vertices $x$ and $y$ of weight $w$.\n\nIt is guaranteed that the given graph is connected (there is a path between any pair of vertices), there are no self-loops (edges connecting the vertex with itself) and multiple edges (for each pair of vertices $x$ and $y$, there is at most one edge between this pair of vertices in the graph).\n\n\n-----Output-----\n\nPrint one integer\u00a0\u2014 the length of the $k$-th smallest shortest path in the given graph (paths from the vertex to itself are not counted, paths from $i$ to $j$ and from $j$ to $i$ are counted as one).\n\n\n-----Examples-----\nInput\n6 10 5\n2 5 1\n5 3 9\n6 2 2\n1 3 1\n5 1 8\n6 5 10\n1 6 5\n6 4 6\n3 6 2\n3 4 5\n\nOutput\n3\n\nInput\n7 15 18\n2 6 3\n5 7 4\n6 5 4\n3 6 9\n6 7 7\n1 6 4\n7 1 6\n7 2 1\n4 3 2\n3 2 8\n5 3 6\n2 5 5\n3 7 9\n4 1 8\n2 1 1\n\nOutput\n9"} +{"id": "03486", "text": "There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \\cdot 10^5$ students ready for the finals!\n\nEach team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \\dots, a[i_k]$, then the diversity of this team is $\\max\\limits_{j=1}^{k} a[i_j] - \\min\\limits_{j=1}^{k} a[i_j]$).\n\nThe total diversity is the sum of diversities of all teams formed.\n\nYour task is to minimize the total diversity of the division of students and find the optimal way to divide the students.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($3 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of students.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the programming skill of the $i$-th student.\n\n\n-----Output-----\n\nIn the first line print two integers $res$ and $k$ \u2014 the minimum total diversity of the division of students and the number of teams in your division, correspondingly.\n\nIn the second line print $n$ integers $t_1, t_2, \\dots, t_n$ ($1 \\le t_i \\le k$), where $t_i$ is the number of team to which the $i$-th student belong.\n\nIf there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.\n\n\n-----Examples-----\nInput\n5\n1 1 3 4 2\n\nOutput\n3 1\n1 1 1 1 1 \n\nInput\n6\n1 5 12 13 2 15\n\nOutput\n7 2\n2 2 1 1 2 1 \n\nInput\n10\n1 2 5 129 185 581 1041 1909 1580 8150\n\nOutput\n7486 3\n3 3 3 2 2 2 2 1 1 1 \n\n\n\n-----Note-----\n\nIn the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.\n\nIn the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.\n\nIn the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$."} +{"id": "03487", "text": "A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from $1$ to $9$ (inclusive) are round.\n\nFor example, the following numbers are round: $4000$, $1$, $9$, $800$, $90$. The following numbers are not round: $110$, $707$, $222$, $1001$.\n\nYou are given a positive integer $n$ ($1 \\le n \\le 10^4$). Represent the number $n$ as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number $n$ as a sum of the least number of terms, each of which is a round number.\n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases in the input. Then $t$ test cases follow.\n\nEach test case is a line containing an integer $n$ ($1 \\le n \\le 10^4$).\n\n\n-----Output-----\n\nPrint $t$ answers to the test cases. Each answer must begin with an integer $k$ \u2014 the minimum number of summands. Next, $k$ terms must follow, each of which is a round number, and their sum is $n$. The terms can be printed in any order. If there are several answers, print any of them.\n\n\n-----Example-----\nInput\n5\n5009\n7\n9876\n10000\n10\n\nOutput\n2\n5000 9\n1\n7 \n4\n800 70 6 9000 \n1\n10000 \n1\n10"} +{"id": "03488", "text": "The only difference between easy and hard versions is constraints.\n\nThere are $n$ kids, each of them is reading a unique book. At the end of any day, the $i$-th kid will give his book to the $p_i$-th kid (in case of $i = p_i$ the kid will give his book to himself). It is guaranteed that all values of $p_i$ are distinct integers from $1$ to $n$ (i.e. $p$ is a permutation). The sequence $p$ doesn't change from day to day, it is fixed.\n\nFor example, if $n=6$ and $p=[4, 6, 1, 3, 5, 2]$ then at the end of the first day the book of the $1$-st kid will belong to the $4$-th kid, the $2$-nd kid will belong to the $6$-th kid and so on. At the end of the second day the book of the $1$-st kid will belong to the $3$-th kid, the $2$-nd kid will belong to the $2$-th kid and so on.\n\nYour task is to determine the number of the day the book of the $i$-th child is returned back to him for the first time for every $i$ from $1$ to $n$.\n\nConsider the following example: $p = [5, 1, 2, 4, 3]$. The book of the $1$-st kid will be passed to the following kids: after the $1$-st day it will belong to the $5$-th kid, after the $2$-nd day it will belong to the $3$-rd kid, after the $3$-rd day it will belong to the $2$-nd kid, after the $4$-th day it will belong to the $1$-st kid. \n\nSo after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 200$) \u2014 the number of queries. Then $q$ queries follow.\n\nThe first line of the query contains one integer $n$ ($1 \\le n \\le 200$) \u2014 the number of kids in the query. The second line of the query contains $n$ integers $p_1, p_2, \\dots, p_n$ ($1 \\le p_i \\le n$, all $p_i$ are distinct, i.e. $p$ is a permutation), where $p_i$ is the kid which will get the book of the $i$-th kid.\n\n\n-----Output-----\n\nFor each query, print the answer on it: $n$ integers $a_1, a_2, \\dots, a_n$, where $a_i$ is the number of the day the book of the $i$-th child is returned back to him for the first time in this query.\n\n\n-----Example-----\nInput\n6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3\n\nOutput\n1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"} +{"id": "03489", "text": "You are both a shop keeper and a shop assistant at a small nearby shop. You have $n$ goods, the $i$-th good costs $a_i$ coins.\n\nYou got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all $n$ goods you have.\n\nHowever, you don't want to lose any money so you want to choose the price in such a way that the sum of new prices is not less than the sum of the initial prices. It means that if you sell all $n$ goods for the new price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.\n\nOn the other hand, you don't want to lose customers because of big prices so among all prices you can choose you need to choose the minimum one.\n\nSo you need to find the minimum possible equal price of all $n$ goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 100$) \u2014 the number of queries. Then $q$ queries follow.\n\nThe first line of the query contains one integer $n$ ($1 \\le n \\le 100)$ \u2014 the number of goods. The second line of the query contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^7$), where $a_i$ is the price of the $i$-th good.\n\n\n-----Output-----\n\nFor each query, print the answer for it \u2014 the minimum possible equal price of all $n$ goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.\n\n\n-----Example-----\nInput\n3\n5\n1 2 3 4 5\n3\n1 2 2\n4\n1 1 1 1\n\nOutput\n3\n2\n1"} +{"id": "03490", "text": "In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$.\n\nA programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel.\n\nYou are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor.\n\n\n-----Input-----\n\nThe first line contains two integers $n$ and $k$ $(2 \\le n \\le 2 \\cdot 10^5$, $0 \\le k \\le \\min(2 \\cdot 10^5, \\frac{n \\cdot (n - 1)}{2}))$ \u2014 total number of programmers and number of pairs of programmers which are in a quarrel.\n\nThe second line contains a sequence of integers $r_1, r_2, \\dots, r_n$ $(1 \\le r_i \\le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer.\n\nEach of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \\le x, y \\le n$, $x \\ne y)$ \u2014 pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input.\n\n\n-----Output-----\n\nPrint $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.\n\n\n-----Examples-----\nInput\n4 2\n10 4 10 15\n1 2\n4 3\n\nOutput\n0 0 1 2 \n\nInput\n10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5\n\nOutput\n5 4 0 5 3 3 9 0 2 5 \n\n\n\n-----Note-----\n\nIn the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel."} +{"id": "03491", "text": "Authors have come up with the string $s$ consisting of $n$ lowercase Latin letters.\n\nYou are given two permutations of its indices (not necessary equal) $p$ and $q$ (both of length $n$). Recall that the permutation is the array of length $n$ which contains each integer from $1$ to $n$ exactly once.\n\nFor all $i$ from $1$ to $n-1$ the following properties hold: $s[p_i] \\le s[p_{i + 1}]$ and $s[q_i] \\le s[q_{i + 1}]$. It means that if you will write down all characters of $s$ in order of permutation indices, the resulting string will be sorted in the non-decreasing order.\n\nYour task is to restore any such string $s$ of length $n$ consisting of at least $k$ distinct lowercase Latin letters which suits the given permutations.\n\nIf there are multiple answers, you can print any of them.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n \\le 2 \\cdot 10^5, 1 \\le k \\le 26$) \u2014 the length of the string and the number of distinct characters required.\n\nThe second line of the input contains $n$ integers $p_1, p_2, \\dots, p_n$ ($1 \\le p_i \\le n$, all $p_i$ are distinct integers from $1$ to $n$) \u2014 the permutation $p$.\n\nThe third line of the input contains $n$ integers $q_1, q_2, \\dots, q_n$ ($1 \\le q_i \\le n$, all $q_i$ are distinct integers from $1$ to $n$) \u2014 the permutation $q$.\n\n\n-----Output-----\n\nIf it is impossible to find the suitable string, print \"NO\" on the first line.\n\nOtherwise print \"YES\" on the first line and string $s$ on the second line. It should consist of $n$ lowercase Latin letters, contain at least $k$ distinct characters and suit the given permutations.\n\nIf there are multiple answers, you can print any of them.\n\n\n-----Example-----\nInput\n3 2\n1 2 3\n1 3 2\n\nOutput\nYES\nabb"} +{"id": "03492", "text": "Recently Vasya decided to improve his pistol shooting skills. Today his coach offered him the following exercise. He placed $n$ cans in a row on a table. Cans are numbered from left to right from $1$ to $n$. Vasya has to knock down each can exactly once to finish the exercise. He is allowed to choose the order in which he will knock the cans down.\n\nVasya knows that the durability of the $i$-th can is $a_i$. It means that if Vasya has already knocked $x$ cans down and is now about to start shooting the $i$-th one, he will need $(a_i \\cdot x + 1)$ shots to knock it down. You can assume that if Vasya starts shooting the $i$-th can, he will be shooting it until he knocks it down.\n\nYour task is to choose such an order of shooting so that the number of shots required to knock each of the $n$ given cans down exactly once is minimum possible.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ $(2 \\le n \\le 1\\,000)$ \u2014 the number of cans.\n\nThe second line of the input contains the sequence $a_1, a_2, \\dots, a_n$ $(1 \\le a_i \\le 1\\,000)$, where $a_i$ is the durability of the $i$-th can.\n\n\n-----Output-----\n\nIn the first line print the minimum number of shots required to knock each of the $n$ given cans down exactly once.\n\nIn the second line print the sequence consisting of $n$ distinct integers from $1$ to $n$ \u2014 the order of indices of cans that minimizes the number of shots required. If there are several answers, you can print any of them.\n\n\n-----Examples-----\nInput\n3\n20 10 20\n\nOutput\n43\n1 3 2 \n\nInput\n4\n10 10 10 10\n\nOutput\n64\n2 1 4 3 \n\nInput\n6\n5 4 5 4 4 5\n\nOutput\n69\n6 1 3 5 2 4 \n\nInput\n2\n1 4\n\nOutput\n3\n2 1 \n\n\n\n-----Note-----\n\nIn the first example Vasya can start shooting from the first can. He knocks it down with the first shot because he haven't knocked any other cans down before. After that he has to shoot the third can. To knock it down he shoots $20 \\cdot 1 + 1 = 21$ times. After that only second can remains. To knock it down Vasya shoots $10 \\cdot 2 + 1 = 21$ times. So the total number of shots is $1 + 21 + 21 = 43$.\n\nIn the second example the order of shooting does not matter because all cans have the same durability."} +{"id": "03493", "text": "Given is a permutation P_1, \\ldots, P_N of 1, \\ldots, N.\nFind the number of integers i (1 \\leq i \\leq N) that satisfy the following condition: \n - For any integer j (1 \\leq j \\leq i), P_i \\leq P_j.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - P_1, \\ldots, P_N is a permutation of 1, \\ldots, N. \n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nP_1 ... P_N\n\n-----Output-----\nPrint the number of integers i that satisfy the condition.\n\n-----Sample Input-----\n5\n4 2 5 1 3\n\n-----Sample Output-----\n3\n\ni=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.\n\nSimilarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition."} +{"id": "03494", "text": "You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).\nYour objective is to remove some of the elements in a so that a will be a good sequence.\nHere, an sequence b is a good sequence when the following condition holds true:\n - For each element x in b, the value x occurs exactly x times in b.\nFor example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.\nFind the minimum number of elements that needs to be removed so that a will be a good sequence.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - a_i is an integer.\n - 1 \\leq a_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the minimum number of elements that needs to be removed so that a will be a good sequence.\n\n-----Sample Input-----\n4\n3 3 3 3\n\n-----Sample Output-----\n1\n\nWe can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence."} +{"id": "03495", "text": "We have five variables x_1, x_2, x_3, x_4, and x_5.\nThe variable x_i was initially assigned a value of i.\nSnuke chose one of these variables and assigned it 0.\nYou are given the values of the five variables after this assignment.\nFind out which variable Snuke assigned 0.\n\n-----Constraints-----\n - The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nx_1 x_2 x_3 x_4 x_5\n\n-----Output-----\nIf the variable Snuke assigned 0 was x_i, print the integer i.\n\n-----Sample Input-----\n0 2 3 4 5\n\n-----Sample Output-----\n1\n\nIn this case, Snuke assigned 0 to x_1, so we should print 1."} +{"id": "03496", "text": "There is a bar of chocolate with a height of H blocks and a width of W blocks.\nSnuke is dividing this bar into exactly three pieces.\nHe can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.\nSnuke is trying to divide the bar as evenly as possible.\nMore specifically, he is trying to minimize S_{max} - S_{min}, where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.\nFind the minimum possible value of S_{max} - S_{min}.\n\n-----Constraints-----\n - 2 \u2264 H, W \u2264 10^5\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\n\n-----Output-----\nPrint the minimum possible value of S_{max} - S_{min}.\n\n-----Sample Input-----\n3 5\n\n-----Sample Output-----\n0\n\nIn the division below, S_{max} - S_{min} = 5 - 5 = 0."} +{"id": "03497", "text": "We have a sequence of length N, a = (a_1, a_2, ..., a_N).\nEach a_i is a positive integer.\nSnuke's objective is to permute the element in a so that the following condition is satisfied:\n - For each 1 \u2264 i \u2264 N - 1, the product of a_i and a_{i + 1} is a multiple of 4.\nDetermine whether Snuke can achieve his objective.\n\n-----Constraints-----\n - 2 \u2264 N \u2264 10^5\n - a_i is an integer.\n - 1 \u2264 a_i \u2264 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\nIf Snuke can achieve his objective, print Yes; otherwise, print No.\n\n-----Sample Input-----\n3\n1 10 100\n\n-----Sample Output-----\nYes\n\nOne solution is (1, 100, 10)."} +{"id": "03498", "text": "You are given strings s and t, consisting of lowercase English letters.\nYou will create a string s' by freely rearranging the characters in s.\nYou will also create a string t' by freely rearranging the characters in t.\nDetermine whether it is possible to satisfy s' < t' for the lexicographic order.\n\n-----Notes-----\nFor a string a = a_1 a_2 ... a_N of length N and a string b = b_1 b_2 ... b_M of length M, we say a < b for the lexicographic order if either one of the following two conditions holds true:\n - N < M and a_1 = b_1, a_2 = b_2, ..., a_N = b_N.\n - There exists i (1 \\leq i \\leq N, M) such that a_1 = b_1, a_2 = b_2, ..., a_{i - 1} = b_{i - 1} and a_i < b_i. Here, letters are compared using alphabetical order.\nFor example, xy < xya and atcoder < atlas.\n\n-----Constraints-----\n - The lengths of s and t are between 1 and 100 (inclusive).\n - s and t consists of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\ns\nt\n\n-----Output-----\nIf it is possible to satisfy s' < t', print Yes; if it is not, print No.\n\n-----Sample Input-----\nyx\naxy\n\n-----Sample Output-----\nYes\n\nWe can, for example, rearrange yx into xy and axy into yxa. Then, xy < yxa."} +{"id": "03499", "text": "We ask you to select some number of positive integers, and calculate the sum of them.\nIt is allowed to select as many integers as you like, and as large integers as you wish.\nYou have to follow these, however: each selected integer needs to be a multiple of A, and you need to select at least one integer.\nYour objective is to make the sum congruent to C modulo B.\nDetermine whether this is possible.\nIf the objective is achievable, print YES. Otherwise, print NO.\n\n-----Constraints-----\n - 1 \u2264 A \u2264 100\n - 1 \u2264 B \u2264 100\n - 0 \u2264 C < B\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B C\n\n-----Output-----\nPrint YES or NO.\n\n-----Sample Input-----\n7 5 1\n\n-----Sample Output-----\nYES\n\nFor example, if you select 7 and 14, the sum 21 is congruent to 1 modulo 5."} +{"id": "03500", "text": "There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)\n\nWhat is the area of this yard excluding the roads? Find it.\n\n-----Note-----\nIt can be proved that the positions of the roads do not affect the area.\n\n-----Constraints-----\n - A is an integer between 2 and 100 (inclusive).\n - B is an integer between 2 and 100 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the area of this yard excluding the roads (in square yards).\n\n-----Sample Input-----\n2 2\n\n-----Sample Output-----\n1\n\nIn this case, the area is 1 square yard."} +{"id": "03501", "text": "We have a long seat of width X centimeters.\nThere are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length Y centimeters.\nWe would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least Z centimeters between two people, and between the end of the seat and a person.\nAt most how many people can sit on the seat?\n\n-----Constraints-----\n - All input values are integers.\n - 1 \\leq X, Y, Z \\leq 10^5\n - Y+2Z \\leq X\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y Z\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n13 3 1\n\n-----Sample Output-----\n3\n\nThere is just enough room for three, as shown below:\nFigure"} +{"id": "03502", "text": "On a two-dimensional plane, there are N red points and N blue points.\nThe coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i).\nA red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.\nAt most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.\n\n-----Constraints-----\n - All input values are integers.\n - 1 \\leq N \\leq 100\n - 0 \\leq a_i, b_i, c_i, d_i < 2N\n - a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different.\n - b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 b_1\na_2 b_2\n:\na_N b_N\nc_1 d_1\nc_2 d_2\n:\nc_N d_N\n\n-----Output-----\nPrint the maximum number of friendly pairs.\n\n-----Sample Input-----\n3\n2 0\n3 1\n1 3\n4 2\n0 4\n5 5\n\n-----Sample Output-----\n2\n\nFor example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5)."} +{"id": "03503", "text": "In a public bath, there is a shower which emits water for T seconds when the switch is pushed.\nIf the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for T seconds.\nNote that it does not mean that the shower emits water for T additional seconds.\nN people will push the switch while passing by the shower.\nThe i-th person will push the switch t_i seconds after the first person pushes it.\nHow long will the shower emit water in total?\n\n-----Constraints-----\n - 1 \u2264 N \u2264 200,000\n - 1 \u2264 T \u2264 10^9\n - 0 = t_1 < t_2 < t_3 < , ..., < t_{N-1} < t_N \u2264 10^9\n - T and each t_i are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN T\nt_1 t_2 ... t_N\n\n-----Output-----\nAssume that the shower will emit water for a total of X seconds. Print X.\n\n-----Sample Input-----\n2 4\n0 3\n\n-----Sample Output-----\n7\n\nThree seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds."} +{"id": "03504", "text": "You have got a shelf and want to put some books on it.\n\nYou are given $q$ queries of three types: L $id$ \u2014 put a book having index $id$ on the shelf to the left from the leftmost existing book; R $id$ \u2014 put a book having index $id$ on the shelf to the right from the rightmost existing book; ? $id$ \u2014 calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index $id$ will be leftmost or rightmost. \n\nYou can assume that the first book you will put can have any position (it does not matter) and queries of type $3$ are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so $id$s don't repeat in queries of first two types.\n\nYour problem is to answer all the queries of type $3$ in order they appear in the input.\n\nNote that after answering the query of type $3$ all the books remain on the shelf and the relative order of books does not change.\n\nIf you are Python programmer, consider using PyPy instead of Python when you submit your code.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 2 \\cdot 10^5$) \u2014 the number of queries.\n\nThen $q$ lines follow. The $i$-th line contains the $i$-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type $3$, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).\n\nIt is guaranteed that there is at least one query of type $3$ in the input.\n\nIn each query the constraint $1 \\le id \\le 2 \\cdot 10^5$ is met.\n\n\n-----Output-----\n\nPrint answers to queries of the type $3$ in order they appear in the input.\n\n\n-----Examples-----\nInput\n8\nL 1\nR 2\nR 3\n? 2\nL 4\n? 1\nL 5\n? 1\n\nOutput\n1\n1\n2\n\nInput\n10\nL 100\nR 100000\nR 123\nL 101\n? 123\nL 10\nR 115\n? 100\nR 110\n? 115\n\nOutput\n0\n2\n1\n\n\n\n-----Note-----\n\nLet's take a look at the first example and let's consider queries: The shelf will look like $[1]$; The shelf will look like $[1, 2]$; The shelf will look like $[1, 2, 3]$; The shelf looks like $[1, \\textbf{2}, 3]$ so the answer is $1$; The shelf will look like $[4, 1, 2, 3]$; The shelf looks like $[4, \\textbf{1}, 2, 3]$ so the answer is $1$; The shelf will look like $[5, 4, 1, 2, 3]$; The shelf looks like $[5, 4, \\textbf{1}, 2, 3]$ so the answer is $2$. \n\nLet's take a look at the second example and let's consider queries: The shelf will look like $[100]$; The shelf will look like $[100, 100000]$; The shelf will look like $[100, 100000, 123]$; The shelf will look like $[101, 100, 100000, 123]$; The shelf looks like $[101, 100, 100000, \\textbf{123}]$ so the answer is $0$; The shelf will look like $[10, 101, 100, 100000, 123]$; The shelf will look like $[10, 101, 100, 100000, 123, 115]$; The shelf looks like $[10, 101, \\textbf{100}, 100000, 123, 115]$ so the answer is $2$; The shelf will look like $[10, 101, 100, 100000, 123, 115, 110]$; The shelf looks like $[10, 101, 100, 100000, 123, \\textbf{115}, 110]$ so the answer is $1$."} +{"id": "03505", "text": "You are given an integer $n$.\n\nYou can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: Replace $n$ with $\\frac{n}{2}$ if $n$ is divisible by $2$; Replace $n$ with $\\frac{2n}{3}$ if $n$ is divisible by $3$; Replace $n$ with $\\frac{4n}{5}$ if $n$ is divisible by $5$. \n\nFor example, you can replace $30$ with $15$ using the first operation, with $20$ using the second operation or with $24$ using the third operation.\n\nYour task is to find the minimum number of moves required to obtain $1$ from $n$ or say that it is impossible to do it.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 1000$) \u2014 the number of queries.\n\nThe next $q$ lines contain the queries. For each query you are given the integer number $n$ ($1 \\le n \\le 10^{18}$).\n\n\n-----Output-----\n\nPrint the answer for each query on a new line. If it is impossible to obtain $1$ from $n$, print -1. Otherwise, print the minimum number of moves required to do it.\n\n\n-----Example-----\nInput\n7\n1\n10\n25\n30\n14\n27\n1000000000000000000\n\nOutput\n0\n4\n6\n6\n-1\n6\n72"} +{"id": "03506", "text": "You are given some Tetris field consisting of $n$ columns. The initial height of the $i$-th column of the field is $a_i$ blocks. On top of these columns you can place only figures of size $2 \\times 1$ (i.e. the height of this figure is $2$ blocks and the width of this figure is $1$ block). Note that you cannot rotate these figures.\n\nYour task is to say if you can clear the whole field by placing such figures.\n\nMore formally, the problem can be described like this:\n\nThe following process occurs while at least one $a_i$ is greater than $0$: You place one figure $2 \\times 1$ (choose some $i$ from $1$ to $n$ and replace $a_i$ with $a_i + 2$); then, while all $a_i$ are greater than zero, replace each $a_i$ with $a_i - 1$. \n\nAnd your task is to determine if it is possible to clear the whole field (i.e. finish the described process), choosing the places for new figures properly.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases.\n\nThe next $2t$ lines describe test cases. The first line of the test case contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the number of columns in the Tetris field. The second line of the test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$), where $a_i$ is the initial height of the $i$-th column of the Tetris field.\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 \"YES\" (without quotes) if you can clear the whole Tetris field and \"NO\" otherwise.\n\n\n-----Example-----\nInput\n4\n3\n1 1 3\n4\n1 1 2 1\n2\n11 11\n1\n100\n\nOutput\nYES\nNO\nYES\nYES\n\n\n\n-----Note-----\n\nThe first test case of the example field is shown below:\n\n[Image]\n\nGray lines are bounds of the Tetris field. Note that the field has no upper bound.\n\nOne of the correct answers is to first place the figure in the first column. Then after the second step of the process, the field becomes $[2, 0, 2]$. Then place the figure in the second column and after the second step of the process, the field becomes $[0, 0, 0]$.\n\nAnd the second test case of the example field is shown below:\n\n$\\square$\n\nIt can be shown that you cannot do anything to end the process.\n\nIn the third test case of the example, you first place the figure in the second column after the second step of the process, the field becomes $[0, 2]$. Then place the figure in the first column and after the second step of the process, the field becomes $[0, 0]$.\n\nIn the fourth test case of the example, place the figure in the first column, then the field becomes $[102]$ after the first step of the process, and then the field becomes $[0]$ after the second step of the process."} +{"id": "03507", "text": "You are given two strings $a$ and $b$ consisting of lowercase English letters, both of length $n$. The characters of both strings have indices from $1$ to $n$, inclusive. \n\nYou are allowed to do the following changes: Choose any index $i$ ($1 \\le i \\le n$) and swap characters $a_i$ and $b_i$; Choose any index $i$ ($1 \\le i \\le n$) and swap characters $a_i$ and $a_{n - i + 1}$; Choose any index $i$ ($1 \\le i \\le n$) and swap characters $b_i$ and $b_{n - i + 1}$. \n\nNote that if $n$ is odd, you are formally allowed to swap $a_{\\lceil\\frac{n}{2}\\rceil}$ with $a_{\\lceil\\frac{n}{2}\\rceil}$ (and the same with the string $b$) but this move is useless. Also you can swap two equal characters but this operation is useless as well.\n\nYou have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps.\n\nIn one preprocess move you can replace a character in $a$ with another character. In other words, in a single preprocess move you can choose any index $i$ ($1 \\le i \\le n$), any character $c$ and set $a_i := c$.\n\nYour task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $a$ and $b$ equal by applying some number of changes described in the list above.\n\nNote that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $b$ or make any preprocess moves after the first change is made.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 10^5$) \u2014 the length of strings $a$ and $b$.\n\nThe second line contains the string $a$ consisting of exactly $n$ lowercase English letters.\n\nThe third line contains the string $b$ consisting of exactly $n$ lowercase English letters.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $a$ equal to string $b$ with a sequence of changes from the list above.\n\n\n-----Examples-----\nInput\n7\nabacaba\nbacabaa\n\nOutput\n4\n\nInput\n5\nzcabd\ndbacz\n\nOutput\n0\n\n\n\n-----Note-----\n\nIn the first example preprocess moves are as follows: $a_1 := $'b', $a_3 := $'c', $a_4 := $'a' and $a_5:=$'b'. Afterwards, $a = $\"bbcabba\". Then we can obtain equal strings by the following sequence of changes: $swap(a_2, b_2)$ and $swap(a_2, a_6)$. There is no way to use fewer than $4$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $4$.\n\nIn the second example no preprocess moves are required. We can use the following sequence of changes to make $a$ and $b$ equal: $swap(b_1, b_5)$, $swap(a_2, a_4)$."} +{"id": "03508", "text": "A frog is currently at the point $0$ on a coordinate axis $Ox$. It jumps by the following algorithm: the first jump is $a$ units to the right, the second jump is $b$ units to the left, the third jump is $a$ units to the right, the fourth jump is $b$ units to the left, and so on.\n\nFormally: if the frog has jumped an even number of times (before the current jump), it jumps from its current position $x$ to position $x+a$; otherwise it jumps from its current position $x$ to position $x-b$. \n\nYour task is to calculate the position of the frog after $k$ jumps.\n\nBut... One more thing. You are watching $t$ different frogs so you have to answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of queries.\n\nEach of the next $t$ lines contain queries (one query per line).\n\nThe query is described as three space-separated integers $a, b, k$ ($1 \\le a, b, k \\le 10^9$) \u2014 the lengths of two types of jumps and the number of jumps, respectively.\n\n\n-----Output-----\n\nPrint $t$ integers. The $i$-th integer should be the answer for the $i$-th query.\n\n\n-----Example-----\nInput\n6\n5 2 3\n100 1 4\n1 10 5\n1000000000 1 6\n1 1 1000000000\n1 1 999999999\n\nOutput\n8\n198\n-17\n2999999997\n0\n1\n\n\n\n-----Note-----\n\nIn the first query frog jumps $5$ to the right, $2$ to the left and $5$ to the right so the answer is $5 - 2 + 5 = 8$.\n\nIn the second query frog jumps $100$ to the right, $1$ to the left, $100$ to the right and $1$ to the left so the answer is $100 - 1 + 100 - 1 = 198$.\n\nIn the third query the answer is $1 - 10 + 1 - 10 + 1 = -17$.\n\nIn the fourth query the answer is $10^9 - 1 + 10^9 - 1 + 10^9 - 1 = 2999999997$.\n\nIn the fifth query all frog's jumps are neutralized by each other so the answer is $0$.\n\nThe sixth query is the same as the fifth but without the last jump so the answer is $1$."} +{"id": "03509", "text": "The only difference between easy and hard versions is the maximum value of $n$.\n\nYou are given a positive integer number $n$. You really love good numbers so you want to find the smallest good number greater than or equal to $n$.\n\nThe positive integer is called good if it can be represented as a sum of distinct powers of $3$ (i.e. no duplicates of powers of $3$ are allowed).\n\nFor example:\n\n $30$ is a good number: $30 = 3^3 + 3^1$, $1$ is a good number: $1 = 3^0$, $12$ is a good number: $12 = 3^2 + 3^1$, but $2$ is not a good number: you can't represent it as a sum of distinct powers of $3$ ($2 = 3^0 + 3^0$), $19$ is not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representations $19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0$ are invalid), $20$ is also not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representation $20 = 3^2 + 3^2 + 3^0 + 3^0$ is invalid). \n\nNote, that there exist other representations of $19$ and $20$ as sums of powers of $3$ but none of them consists of distinct powers of $3$.\n\nFor the given positive integer $n$ find such smallest $m$ ($n \\le m$) that $m$ is a good number.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 500$) \u2014 the number of queries. Then $q$ queries follow.\n\nThe only line of the query contains one integer $n$ ($1 \\le n \\le 10^{18}$).\n\n\n-----Output-----\n\nFor each query, print such smallest integer $m$ (where $n \\le m$) that $m$ is a good number.\n\n\n-----Example-----\nInput\n8\n1\n2\n6\n13\n14\n3620\n10000\n1000000000000000000\n\nOutput\n1\n3\n9\n13\n27\n6561\n19683\n1350851717672992089"} +{"id": "03510", "text": "You are given four integers $a$, $b$, $x$ and $y$. Initially, $a \\ge x$ and $b \\ge y$. You can do the following operation no more than $n$ times:\n\n Choose either $a$ or $b$ and decrease it by one. However, as a result of this operation, value of $a$ cannot become less than $x$, and value of $b$ cannot become less than $y$. \n\nYour task is to find the minimum possible product of $a$ and $b$ ($a \\cdot b$) you can achieve by applying the given operation no more than $n$ times.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2 \\cdot 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains five integers $a$, $b$, $x$, $y$ and $n$ ($1 \\le a, b, x, y, n \\le 10^9$). Additional constraint on the input: $a \\ge x$ and $b \\ge y$ always holds.\n\n\n-----Output-----\n\nFor each test case, print one integer: the minimum possible product of $a$ and $b$ ($a \\cdot b$) you can achieve by applying the given operation no more than $n$ times.\n\n\n-----Example-----\nInput\n7\n10 10 8 5 3\n12 8 8 7 2\n12343 43 4543 39 123212\n1000000000 1000000000 1 1 1\n1000000000 1000000000 1 1 1000000000\n10 11 2 1 5\n10 11 9 1 10\n\nOutput\n70\n77\n177177\n999999999000000000\n999999999\n55\n10\n\n\n\n-----Note-----\n\nIn the first test case of the example, you need to decrease $b$ three times and obtain $10 \\cdot 7 = 70$.\n\nIn the second test case of the example, you need to decrease $a$ one time, $b$ one time and obtain $11 \\cdot 7 = 77$.\n\nIn the sixth test case of the example, you need to decrease $a$ five times and obtain $5 \\cdot 11 = 55$.\n\nIn the seventh test case of the example, you need to decrease $b$ ten times and obtain $10 \\cdot 1 = 10$."} +{"id": "03511", "text": "You are given two positive integers $a$ and $b$.\n\nIn one move, you can change $a$ in the following way:\n\n Choose any positive odd integer $x$ ($x > 0$) and replace $a$ with $a+x$; choose any positive even integer $y$ ($y > 0$) and replace $a$ with $a-y$. \n\nYou can perform as many such operations as you want. You can choose the same numbers $x$ and $y$ in different moves.\n\nYour task is to find the minimum number of moves required to obtain $b$ from $a$. It is guaranteed that you can always obtain $b$ from $a$.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases.\n\nThen $t$ test cases follow. Each test case is given as two space-separated integers $a$ and $b$ ($1 \\le a, b \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 the minimum number of moves required to obtain $b$ from $a$ if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain $b$ from $a$.\n\n\n-----Example-----\nInput\n5\n2 3\n10 10\n2 4\n7 4\n9 3\n\nOutput\n1\n0\n2\n2\n1\n\n\n\n-----Note-----\n\nIn the first test case, you can just add $1$.\n\nIn the second test case, you don't need to do anything.\n\nIn the third test case, you can add $1$ two times.\n\nIn the fourth test case, you can subtract $4$ and add $1$.\n\nIn the fifth test case, you can just subtract $6$."} +{"id": "03512", "text": "There is a building consisting of $10~000$ apartments numbered from $1$ to $10~000$, inclusive.\n\nCall an apartment boring, if its number consists of the same digit. Examples of boring apartments are $11, 2, 777, 9999$ and so on.\n\nOur character is a troublemaker, and he calls the intercoms of all boring apartments, till someone answers the call, in the following order:\n\n First he calls all apartments consisting of digit $1$, in increasing order ($1, 11, 111, 1111$). Next he calls all apartments consisting of digit $2$, in increasing order ($2, 22, 222, 2222$) And so on. \n\nThe resident of the boring apartment $x$ answers the call, and our character stops calling anyone further.\n\nOur character wants to know how many digits he pressed in total and your task is to help him to count the total number of keypresses.\n\nFor example, if the resident of boring apartment $22$ answered, then our character called apartments with numbers $1, 11, 111, 1111, 2, 22$ and the total number of digits he pressed is $1 + 2 + 3 + 4 + 1 + 2 = 13$.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 36$) \u2014 the number of test cases.\n\nThe only line of the test case contains one integer $x$ ($1 \\le x \\le 9999$) \u2014 the apartment number of the resident who answered the call. It is guaranteed that $x$ consists of the same digit.\n\n\n-----Output-----\n\nFor each test case, print the answer: how many digits our character pressed in total.\n\n\n-----Example-----\nInput\n4\n22\n9999\n1\n777\n\nOutput\n13\n90\n1\n66"} +{"id": "03513", "text": "You are given $k$ sequences of integers. The length of the $i$-th sequence equals to $n_i$.\n\nYou have to choose exactly two sequences $i$ and $j$ ($i \\ne j$) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence $i$ (its length will be equal to $n_i - 1$) equals to the sum of the changed sequence $j$ (its length will be equal to $n_j - 1$).\n\nNote that it's required to remove exactly one element in each of the two chosen sequences.\n\nAssume that the sum of the empty (of the length equals $0$) sequence is $0$.\n\n\n-----Input-----\n\nThe first line contains an integer $k$ ($2 \\le k \\le 2 \\cdot 10^5$) \u2014 the number of sequences.\n\nThen $k$ pairs of lines follow, each pair containing a sequence.\n\nThe first line in the $i$-th pair contains one integer $n_i$ ($1 \\le n_i < 2 \\cdot 10^5$) \u2014 the length of the $i$-th sequence. The second line of the $i$-th pair contains a sequence of $n_i$ integers $a_{i, 1}, a_{i, 2}, \\dots, a_{i, n_i}$.\n\nThe elements of sequences are integer numbers from $-10^4$ to $10^4$.\n\nThe sum of lengths of all given sequences don't exceed $2 \\cdot 10^5$, i.e. $n_1 + n_2 + \\dots + n_k \\le 2 \\cdot 10^5$.\n\n\n-----Output-----\n\nIf it is impossible to choose two sequences such that they satisfy given conditions, print \"NO\" (without quotes). Otherwise in the first line print \"YES\" (without quotes), in the second line \u2014 two integers $i$, $x$ ($1 \\le i \\le k, 1 \\le x \\le n_i$), in the third line \u2014 two integers $j$, $y$ ($1 \\le j \\le k, 1 \\le y \\le n_j$). It means that the sum of the elements of the $i$-th sequence without the element with index $x$ equals to the sum of the elements of the $j$-th sequence without the element with index $y$.\n\nTwo chosen sequences must be distinct, i.e. $i \\ne j$. You can print them in any order.\n\nIf there are multiple possible answers, print any of them.\n\n\n-----Examples-----\nInput\n2\n5\n2 3 1 3 2\n6\n1 1 2 2 2 1\n\nOutput\nYES\n2 6\n1 2\n\nInput\n3\n1\n5\n5\n1 1 1 1 1\n2\n2 3\n\nOutput\nNO\n\nInput\n4\n6\n2 2 2 2 2 2\n5\n2 2 2 2 2\n3\n2 2 2\n5\n2 2 2 2 2\n\nOutput\nYES\n2 2\n4 1\n\n\n\n-----Note-----\n\nIn the first example there are two sequences $[2, 3, 1, 3, 2]$ and $[1, 1, 2, 2, 2, 1]$. You can remove the second element from the first sequence to get $[2, 1, 3, 2]$ and you can remove the sixth element from the second sequence to get $[1, 1, 2, 2, 2]$. The sums of the both resulting sequences equal to $8$, i.e. the sums are equal."} +{"id": "03514", "text": "Given an array A of integers, we must\u00a0modify the array in the following way: we choose an i\u00a0and replace\u00a0A[i] with -A[i], and we repeat this process K times in total.\u00a0 (We may choose the same index i multiple times.)\nReturn the largest possible sum of the array after modifying it in this way.\n\u00a0\nExample 1:\nInput: A = [4,2,3], K = 1\nOutput: 5\nExplanation: Choose indices (1,) and A becomes [4,-2,3].\n\n\nExample 2:\nInput: A = [3,-1,0,2], K = 3\nOutput: 6\nExplanation: Choose indices (1, 2, 2) and A becomes [3,1,0,2].\n\n\nExample 3:\nInput: A = [2,-3,-1,5,-4], K = 2\nOutput: 13\nExplanation: Choose indices (1, 4) and A becomes [2,3,-1,5,4].\n\n\n\n\u00a0\nNote:\n\n1 <= A.length <= 10000\n1 <= K <= 10000\n-100 <= A[i] <= 100"} +{"id": "03515", "text": "Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.\nFormally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])\n\u00a0\nExample 1:\nInput: A = [0,2,1,-6,6,-7,9,1,2,0,1]\nOutput: true\nExplanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1\n\nExample 2:\nInput: A = [0,2,1,-6,6,7,9,-1,2,0,1]\nOutput: false\n\nExample 3:\nInput: A = [3,3,6,5,-2,2,5,1,-9,4]\nOutput: true\nExplanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4\n\n\u00a0\nConstraints:\n\n3 <= A.length <= 50000\n-10^4\u00a0<= A[i] <= 10^4"} +{"id": "03516", "text": "We have N voting papers. The i-th vote (1 \\leq i \\leq N) has the string S_i written on it.\nPrint all strings that are written on the most number of votes, in lexicographical order.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - S_i (1 \\leq i \\leq N) are strings consisting of lowercase English letters.\n - The length of S_i (1 \\leq i \\leq N) is between 1 and 10 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS_1\n:\nS_N\n\n-----Output-----\nPrint all strings in question in lexicographical order.\n\n-----Sample Input-----\n7\nbeat\nvet\nbeet\nbed\nvet\nbet\nbeet\n\n-----Sample Output-----\nbeet\nvet\n\nbeet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet."} +{"id": "03517", "text": "Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them.\nHe may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if a_i=a_j (i\u2260j), he has to pay the cost separately for transforming each of them (See Sample 2).\nFind the minimum total cost to achieve his objective.\n\n-----Constraints-----\n - 1\u2266N\u2266100\n - -100\u2266a_i\u2266100\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the minimum total cost to achieve Evi's objective.\n\n-----Sample Input-----\n2\n4 8\n\n-----Sample Output-----\n8\n\nTransforming the both into 6s will cost (4-6)^2+(8-6)^2=8 dollars, which is the minimum."} +{"id": "03518", "text": "You went shopping to buy cakes and donuts with X yen (the currency of Japan).\nFirst, you bought one cake for A yen at a cake shop.\nThen, you bought as many donuts as possible for B yen each, at a donut shop.\nHow much do you have left after shopping?\n\n-----Constraints-----\n - 1 \\leq A, B \\leq 1 000\n - A + B \\leq X \\leq 10 000\n - X, A and B are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX\nA\nB\n\n-----Output-----\nPrint the amount you have left after shopping.\n\n-----Sample Input-----\n1234\n150\n100\n\n-----Sample Output-----\n84\n\nYou have 1234 - 150 = 1084 yen left after buying a cake.\nWith this amount, you can buy 10 donuts, after which you have 84 yen left."} +{"id": "03519", "text": "Snuke has N dogs and M monkeys. He wants them to line up in a row.\nAs a Japanese saying goes, these dogs and monkeys are on bad terms. (\"ken'en no naka\", literally \"the relationship of dogs and monkeys\", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.\nHow many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).\nHere, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.\n\n-----Constraints-----\n - 1 \u2264 N,M \u2264 10^5\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\n\n-----Output-----\nPrint the number of possible arrangements, modulo 10^9+7.\n\n-----Sample Input-----\n2 2\n\n-----Sample Output-----\n8\n\nWe will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA."} +{"id": "03520", "text": "In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands.\nFor convenience, we will call them Island 1, Island 2, ..., Island N.\nThere are M kinds of regular boat services between these islands.\nEach service connects two islands. The i-th service connects Island a_i and Island b_i.\nCat Snuke is on Island 1 now, and wants to go to Island N.\nHowever, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.\nHelp him.\n\n-----Constraints-----\n - 3 \u2264 N \u2264 200 000\n - 1 \u2264 M \u2264 200 000\n - 1 \u2264 a_i < b_i \u2264 N\n - (a_i, b_i) \\neq (1, N)\n - If i \\neq j, (a_i, b_i) \\neq (a_j, b_j).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\na_1 b_1\na_2 b_2\n:\na_M b_M\n\n-----Output-----\nIf it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.\n\n-----Sample Input-----\n3 2\n1 2\n2 3\n\n-----Sample Output-----\nPOSSIBLE\n"} +{"id": "03521", "text": "You are given a string s consisting of lowercase English letters. Extract all the characters in the odd-indexed positions and print the string obtained by concatenating them. Here, the leftmost character is assigned the index 1.\n\n-----Constraints-----\n - Each character in s is a lowercase English letter.\n - 1\u2264|s|\u226410^5\n\n-----Input-----\nThe input is given from Standard Input in the following format:\ns\n\n-----Output-----\nPrint the string obtained by concatenating all the characters in the odd-numbered positions.\n\n-----Sample Input-----\natcoder\n\n-----Sample Output-----\nacdr\n\nExtract the first character a, the third character c, the fifth character d and the seventh character r to obtain acdr."} +{"id": "03522", "text": "You are given three strings A, B and C. Check whether they form a word chain.\nMore formally, determine whether both of the following are true:\n - The last character in A and the initial character in B are the same.\n - The last character in B and the initial character in C are the same.\nIf both are true, print YES. Otherwise, print NO.\n\n-----Constraints-----\n - A, B and C are all composed of lowercase English letters (a - z).\n - 1 \u2264 |A|, |B|, |C| \u2264 10, where |A|, |B| and |C| are the lengths of A, B and C, respectively.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B C\n\n-----Output-----\nPrint YES or NO.\n\n-----Sample Input-----\nrng gorilla apple\n\n-----Sample Output-----\nYES\n\nThey form a word chain."} +{"id": "03523", "text": "You are given two positive integers A and B. Compare the magnitudes of these numbers.\n\n-----Constraints-----\n - 1 \u2264 A, B \u2264 10^{100}\n - Neither A nor B begins with a 0.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA\nB\n\n-----Output-----\nPrint GREATER if A>B, LESS if A24, print GREATER."} +{"id": "03524", "text": "Takahashi has N blue cards and M red cards.\nA string is written on each card. The string written on the i-th blue card is s_i, and the string written on the i-th red card is t_i.\nTakahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn 1 yen (the currency of Japan); each time he finds a red card with that string, he will lose 1 yen.\nHere, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces atcoder, he will not earn money even if there are blue cards with atcoderr, atcode, btcoder, and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.)\nAt most how much can he earn on balance?\nNote that the same string may be written on multiple cards.\n\n-----Constraints-----\n - N and M are integers.\n - 1 \\leq N, M \\leq 100\n - s_1, s_2, ..., s_N, t_1, t_2, ..., t_M are all strings of lengths between 1 and 10 (inclusive) consisting of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\ns_1\ns_2\n:\ns_N\nM\nt_1\nt_2\n:\nt_M\n\n-----Output-----\nIf Takahashi can earn at most X yen on balance, print X.\n\n-----Sample Input-----\n3\napple\norange\napple\n1\ngrape\n\n-----Sample Output-----\n2\n\nHe can earn 2 yen by announcing apple."} +{"id": "03525", "text": "On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.\nYou are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.\n\n-----Constraints-----\n - b is one of the letters A, C, G and T.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nb\n\n-----Output-----\nPrint the letter representing the base that bonds with the base b.\n\n-----Sample Input-----\nA\n\n-----Sample Output-----\nT\n"} +{"id": "03526", "text": "We have a 2 \\times N grid. We will denote the square at the i-th row and j-th column (1 \\leq i \\leq 2, 1 \\leq j \\leq N) as (i, j).\nYou are initially in the top-left square, (1, 1).\nYou will travel to the bottom-right square, (2, N), by repeatedly moving right or down.\nThe square (i, j) contains A_{i, j} candies.\nYou will collect all the candies you visit during the travel.\nThe top-left and bottom-right squares also contain candies, and you will also collect them.\nAt most how many candies can you collect when you choose the best way to travel?\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 1 \\leq A_{i, j} \\leq 100 (1 \\leq i \\leq 2, 1 \\leq j \\leq N)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_{1, 1} A_{1, 2} ... A_{1, N}\nA_{2, 1} A_{2, 2} ... A_{2, N}\n\n-----Output-----\nPrint the maximum number of candies that can be collected.\n\n-----Sample Input-----\n5\n3 2 2 4 1\n1 2 2 2 1\n\n-----Sample Output-----\n14\n\nThe number of collected candies will be maximized when you:\n - move right three times, then move down once, then move right once."} +{"id": "03527", "text": "There are N boxes arranged in a row.\nInitially, the i-th box from the left contains a_i candies.\nSnuke can perform the following operation any number of times:\n - Choose a box containing at least one candy, and eat one of the candies in the chosen box.\nHis objective is as follows:\n - Any two neighboring boxes contain at most x candies in total.\nFind the minimum number of operations required to achieve the objective.\n\n-----Constraints-----\n - 2 \u2264 N \u2264 10^5\n - 0 \u2264 a_i \u2264 10^9\n - 0 \u2264 x \u2264 10^9\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN x\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the minimum number of operations required to achieve the objective.\n\n-----Sample Input-----\n3 3\n2 2 2\n\n-----Sample Output-----\n1\n\nEat one candy in the second box.\nThen, the number of candies in each box becomes (2, 1, 2)."} +{"id": "03528", "text": "We have a 3 \\times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.\n\nAccording to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.\n\nDetermine if he is correct. \n\n-----Constraints-----\n - c_{i, j} \\ (1 \\leq i \\leq 3, 1 \\leq j \\leq 3) is an integer between 0 and 100 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nc_{1,1} c_{1,2} c_{1,3}\nc_{2,1} c_{2,2} c_{2,3}\nc_{3,1} c_{3,2} c_{3,3}\n\n-----Output-----\nIf Takahashi's statement is correct, print Yes; otherwise, print No.\n\n-----Sample Input-----\n1 0 1\n2 1 2\n1 0 1\n\n-----Sample Output-----\nYes\n\nTakahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1."} +{"id": "03529", "text": "AtCoder Inc. holds a contest every Saturday.\nThere are two types of contests called ABC and ARC, and just one of them is held at a time.\nThe company holds these two types of contests alternately: an ARC follows an ABC and vice versa.\nGiven a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.\n\n-----Constraints-----\n - S is ABC or ARC.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the string representing the type of the contest held this week.\n\n-----Sample Input-----\nABC\n\n-----Sample Output-----\nARC\n\nThey held an ABC last week, so they will hold an ARC this week."} +{"id": "03530", "text": "You are given nonnegative integers a and b (a \u2264 b), and a positive integer x.\nAmong the integers between a and b, inclusive, how many are divisible by x?\n\n-----Constraints-----\n - 0 \u2264 a \u2264 b \u2264 10^{18}\n - 1 \u2264 x \u2264 10^{18}\n\n-----Input-----\nThe input is given from Standard Input in the following format:\na b x\n\n-----Output-----\nPrint the number of the integers between a and b, inclusive, that are divisible by x.\n\n-----Sample Input-----\n4 8 2\n\n-----Sample Output-----\n3\n\nThere are three integers between 4 and 8, inclusive, that are divisible by 2: 4, 6 and 8."} +{"id": "03531", "text": "In some other world, today is December D-th.\nWrite a program that prints Christmas if D = 25, Christmas Eve if D = 24, Christmas Eve Eve if D = 23 and Christmas Eve Eve Eve if D = 22.\n\n-----Constraints-----\n - 22 \\leq D \\leq 25\n - D is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nD\n\n-----Output-----\nPrint the specified string (case-sensitive).\n\n-----Sample Input-----\n25\n\n-----Sample Output-----\nChristmas\n"} +{"id": "03532", "text": "Takahashi loves numbers divisible by 2.\nYou are given a positive integer N. Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique.\nHere, the number of times an integer can be divisible by 2, is how many times the integer can be divided by 2 without remainder.\nFor example,\n - 6 can be divided by 2 once: 6 -> 3.\n - 8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1.\n - 3 can be divided by 2 zero times.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\n7\n\n-----Sample Output-----\n4\n\n4 can be divided by 2 twice, which is the most number of times among 1, 2, ..., 7."} +{"id": "03533", "text": "Three people, A, B and C, are trying to communicate using transceivers.\nThey are standing along a number line, and the coordinates of A, B and C are a, b and c (in meters), respectively.\nTwo people can directly communicate when the distance between them is at most d meters.\nDetermine if A and C can communicate, either directly or indirectly.\nHere, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate.\n\n-----Constraints-----\n - 1 \u2264 a,b,c \u2264 100\n - 1 \u2264 d \u2264 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\na b c d\n\n-----Output-----\nIf A and C can communicate, print Yes; if they cannot, print No.\n\n-----Sample Input-----\n4 7 9 3\n\n-----Sample Output-----\nYes\n\nA and B can directly communicate, and also B and C can directly communicate, so we should print Yes."} +{"id": "03534", "text": "You are given three words s_1, s_2 and s_3, each composed of lowercase English letters, with spaces in between.\nPrint the acronym formed from the uppercased initial letters of the words.\n\n-----Constraints-----\n - s_1, s_2 and s_3 are composed of lowercase English letters.\n - 1 \u2264 |s_i| \u2264 10 (1\u2264i\u22643)\n\n-----Input-----\nInput is given from Standard Input in the following format:\ns_1 s_2 s_3\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\natcoder beginner contest\n\n-----Sample Output-----\nABC\n\nThe initial letters of atcoder, beginner and contest are a, b and c. Uppercase and concatenate them to obtain ABC."} +{"id": "03535", "text": "An elementary school student Takahashi has come to a variety store.\nHe has two coins, A-yen and B-yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it?\nNote that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan.\n\n-----Constraints-----\n - All input values are integers.\n - 1 \\leq A, B \\leq 500\n - 1 \\leq C \\leq 1000\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B C\n\n-----Output-----\nIf Takahashi can buy the toy, print Yes; if he cannot, print No.\n\n-----Sample Input-----\n50 100 120\n\n-----Sample Output-----\nYes\n\nHe has 50 + 100 = 150 yen, so he can buy the 120-yen toy."} +{"id": "03536", "text": "Tak has N cards. On the i-th (1 \\leq i \\leq N) card is written an integer x_i.\nHe is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A.\nIn how many ways can he make his selection?\n\n-----Constraints-----\n - 1 \\leq N \\leq 50\n - 1 \\leq A \\leq 50\n - 1 \\leq x_i \\leq 50\n - N,\\,A,\\,x_i are integers.\n\n-----Partial Score-----\n - 200 points will be awarded for passing the test set satisfying 1 \\leq N \\leq 16.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN A\nx_1 x_2 ... x_N\n\n-----Output-----\nPrint the number of ways to select cards such that the average of the written integers is exactly A.\n\n-----Sample Input-----\n4 8\n7 9 8 9\n\n-----Sample Output-----\n5\n\n - The following are the 5 ways to select cards such that the average is 8:\n - Select the 3-rd card.\n - Select the 1-st and 2-nd cards.\n - Select the 1-st and 4-th cards.\n - Select the 1-st, 2-nd and 3-rd cards.\n - Select the 1-st, 3-rd and 4-th cards."} +{"id": "03537", "text": "You are given an integer sequence of length n, a_1, ..., a_n.\nLet us consider performing the following n operations on an empty sequence b.\nThe i-th operation is as follows:\n - Append a_i to the end of b.\n - Reverse the order of the elements in b.\nFind the sequence b obtained after these n operations.\n\n-----Constraints-----\n - 1 \\leq n \\leq 2\\times 10^5\n - 0 \\leq a_i \\leq 10^9\n - n and a_i are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nn\na_1 a_2 ... a_n\n\n-----Output-----\nPrint n integers in a line with spaces in between.\nThe i-th integer should be b_i.\n\n-----Sample Input-----\n4\n1 2 3 4\n\n-----Sample Output-----\n4 2 1 3\n\n - After step 1 of the first operation, b becomes: 1.\n - After step 2 of the first operation, b becomes: 1.\n - After step 1 of the second operation, b becomes: 1, 2.\n - After step 2 of the second operation, b becomes: 2, 1.\n - After step 1 of the third operation, b becomes: 2, 1, 3.\n - After step 2 of the third operation, b becomes: 3, 1, 2.\n - After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.\n - After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.\nThus, the answer is 4 2 1 3."} +{"id": "03538", "text": "Raccoon is fighting with a monster.\nThe health of the monster is H.\nRaccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.\nThere is no other way to decrease the monster's health.\nRaccoon wins when the monster's health becomes 0 or below.\nIf Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.\n\n-----Constraints-----\n - 1 \\leq H \\leq 10^9\n - 1 \\leq N \\leq 10^5\n - 1 \\leq A_i \\leq 10^4\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH N\nA_1 A_2 ... A_N\n\n-----Output-----\nIf Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.\n\n-----Sample Input-----\n10 3\n4 5 6\n\n-----Sample Output-----\nYes\n\nThe monster's health will become 0 or below after, for example, using the second and third moves."} +{"id": "03539", "text": "We will call a string that can be obtained by concatenating two equal strings an even string.\nFor example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not.\nYou are given an even string S consisting of lowercase English letters.\nFind the length of the longest even string that can be obtained by deleting one or more characters from the end of S.\nIt is guaranteed that such a non-empty string exists for a given input.\n\n-----Constraints-----\n - 2 \\leq |S| \\leq 200\n - S is an even string consisting of lowercase English letters.\n - There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the length of the longest even string that can be obtained.\n\n-----Sample Input-----\nabaababaab\n\n-----Sample Output-----\n6\n\n - abaababaab itself is even, but we need to delete at least one character.\n - abaababaa is not even.\n - abaababa is not even.\n - abaabab is not even.\n - abaaba is even. Thus, we should print its length, 6."} +{"id": "03540", "text": "You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.\n\n-----Constraints-----\n - |S|=3\n - S consists of a, b and c.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nIf S can be obtained by permuting abc, print Yes; otherwise, print No.\n\n-----Sample Input-----\nbac\n\n-----Sample Output-----\nYes\n\nSwapping the first and second characters in bac results in abc."} +{"id": "03541", "text": "You are given two arrays $a$ and $b$, both of length $n$.\n\nLet's define a function $f(l, r) = \\sum\\limits_{l \\le i \\le r} a_i \\cdot b_i$.\n\nYour task is to reorder the elements (choose an arbitrary order of elements) of the array $b$ to minimize the value of $\\sum\\limits_{1 \\le l \\le r \\le n} f(l, r)$. Since the answer can be very large, you have to print it modulo $998244353$. Note that you should minimize the answer but not its remainder.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in $a$ and $b$.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^6$), where $a_i$ is the $i$-th element of $a$.\n\nThe third line of the input contains $n$ integers $b_1, b_2, \\dots, b_n$ ($1 \\le b_j \\le 10^6$), where $b_j$ is the $j$-th element of $b$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum possible value of $\\sum\\limits_{1 \\le l \\le r \\le n} f(l, r)$ after rearranging elements of $b$, taken modulo $998244353$. Note that you should minimize the answer but not its remainder.\n\n\n-----Examples-----\nInput\n5\n1 8 7 2 4\n9 7 2 9 3\n\nOutput\n646\n\nInput\n1\n1000000\n1000000\n\nOutput\n757402647\n\nInput\n2\n1 3\n4 2\n\nOutput\n20"} +{"id": "03542", "text": "There are $n$ shovels in the nearby shop. The $i$-th shovel costs $a_i$ bourles.\n\nMisha has to buy exactly $k$ shovels. Each shovel can be bought no more than once.\n\nMisha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset.\n\nThere are also $m$ special offers in the shop. The $j$-th of them is given as a pair $(x_j, y_j)$, and it means that if Misha buys exactly $x_j$ shovels during one purchase then $y_j$ most cheapest of them are for free (i.e. he will not pay for $y_j$ most cheapest shovels during the current purchase).\n\nMisha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers).\n\nYour task is to calculate the minimum cost of buying $k$ shovels, if Misha buys them optimally.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n, m$ and $k$ ($1 \\le n, m \\le 2 \\cdot 10^5, 1 \\le k \\le min(n, 2000)$) \u2014 the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the cost of the $i$-th shovel.\n\nThe next $m$ lines contain special offers. The $j$-th of them is given as a pair of integers $(x_i, y_i)$ ($1 \\le y_i \\le x_i \\le n$) and means that if Misha buys exactly $x_i$ shovels during some purchase, then he can take $y_i$ most cheapest of them for free.\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum cost of buying $k$ shovels if Misha buys them optimally.\n\n\n-----Examples-----\nInput\n7 4 5\n2 5 4 2 6 3 1\n2 1\n6 5\n2 1\n3 1\n\nOutput\n7\n\nInput\n9 4 8\n6 8 5 1 8 1 1 2 1\n9 2\n8 4\n5 3\n9 7\n\nOutput\n17\n\nInput\n5 1 4\n2 5 7 4 6\n5 4\n\nOutput\n17\n\n\n\n-----Note-----\n\nIn the first example Misha can buy shovels on positions $1$ and $4$ (both with costs $2$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $3$ and $6$ (with costs $4$ and $3$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $7$ with cost $1$. So the total cost is $4 + 2 + 1 = 7$.\n\nIn the second example Misha can buy shovels on positions $1$, $2$, $3$, $4$ and $8$ (costs are $6$, $8$, $5$, $1$ and $2$) and get three cheapest (with costs $5$, $1$ and $2$) for free. And then he can buy shovels on positions $6$, $7$ and $9$ (all with costs $1$) without using any special offers. So the total cost is $6 + 8 + 1 + 1 + 1 = 17$.\n\nIn the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $17$."} +{"id": "03543", "text": "You are given an undirected tree consisting of $n$ vertices. An undirected tree is a connected undirected graph with $n - 1$ edges.\n\nYour task is to add the minimum number of edges in such a way that the length of the shortest path from the vertex $1$ to any other vertex is at most $2$. Note that you are not allowed to add loops and multiple edges.\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of vertices in the tree.\n\nThe following $n - 1$ lines contain edges: edge $i$ is given as a pair of vertices $u_i, v_i$ ($1 \\le u_i, v_i \\le n$). It is guaranteed that the given edges form a tree. It is guaranteed that there are no loops and multiple edges in the given edges.\n\n\n-----Output-----\n\nPrint a single integer \u2014 the minimum number of edges you have to add in order to make the shortest distance from the vertex $1$ to any other vertex at most $2$. Note that you are not allowed to add loops and multiple edges.\n\n\n-----Examples-----\nInput\n7\n1 2\n2 3\n2 4\n4 5\n4 6\n5 7\n\nOutput\n2\n\nInput\n7\n1 2\n1 3\n2 4\n2 5\n3 6\n1 7\n\nOutput\n0\n\nInput\n7\n1 2\n2 3\n3 4\n3 5\n3 6\n3 7\n\nOutput\n1\n\n\n\n-----Note-----\n\nThe tree corresponding to the first example: [Image] The answer is $2$, some of the possible answers are the following: $[(1, 5), (1, 6)]$, $[(1, 4), (1, 7)]$, $[(1, 6), (1, 7)]$.\n\nThe tree corresponding to the second example: [Image] The answer is $0$.\n\nThe tree corresponding to the third example: [Image] The answer is $1$, only one possible way to reach it is to add the edge $(1, 3)$."} +{"id": "03544", "text": "You are given two positive integers $n$ and $k$. Print the $k$-th positive integer that is not divisible by $n$.\n\nFor example, if $n=3$, and $k=7$, then all numbers that are not divisible by $3$ are: $1, 2, 4, 5, 7, 8, 10, 11, 13 \\dots$. The $7$-th number among them is $10$.\n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases in the input. Next, $t$ test cases are given, one per line.\n\nEach test case is two positive integers $n$ ($2 \\le n \\le 10^9$) and $k$ ($1 \\le k \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case print the $k$-th positive integer that is not divisible by $n$.\n\n\n-----Example-----\nInput\n6\n3 7\n4 12\n2 1000000000\n7 97\n1000000000 1000000000\n2 1\n\nOutput\n10\n15\n1999999999\n113\n1000000001\n1"} +{"id": "03545", "text": "The only difference between easy and hard versions are constraints on $n$ and $k$.\n\nYou are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $k$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $0$).\n\nEach conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.\n\nYou (suddenly!) have the ability to see the future. You know that during the day you will receive $n$ messages, the $i$-th message will be received from the friend with ID $id_i$ ($1 \\le id_i \\le 10^9$).\n\nIf you receive a message from $id_i$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.\n\nOtherwise (i.e. if there is no conversation with $id_i$ on the screen):\n\n Firstly, if the number of conversations displayed on the screen is $k$, the last conversation (which has the position $k$) is removed from the screen. Now the number of conversations on the screen is guaranteed to be less than $k$ and the conversation with the friend $id_i$ is not displayed on the screen. The conversation with the friend $id_i$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. \n\nYour task is to find the list of conversations (in the order they are displayed on the screen) after processing all $n$ messages.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le n, k \\le 2 \\cdot 10^5)$ \u2014 the number of messages and the number of conversations your smartphone can show.\n\nThe second line of the input contains $n$ integers $id_1, id_2, \\dots, id_n$ ($1 \\le id_i \\le 10^9$), where $id_i$ is the ID of the friend which sends you the $i$-th message.\n\n\n-----Output-----\n\nIn the first line of the output print one integer $m$ ($1 \\le m \\le min(n, k)$) \u2014 the number of conversations shown after receiving all $n$ messages.\n\nIn the second line print $m$ integers $ids_1, ids_2, \\dots, ids_m$, where $ids_i$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $i$ after receiving all $n$ messages.\n\n\n-----Examples-----\nInput\n7 2\n1 2 3 2 1 3 2\n\nOutput\n2\n2 1 \n\nInput\n10 4\n2 3 3 1 1 2 1 2 3 3\n\nOutput\n3\n1 3 2 \n\n\n\n-----Note-----\n\nIn the first example the list of conversations will change in the following way (in order from the first to last message):\n\n $[]$; $[1]$; $[2, 1]$; $[3, 2]$; $[3, 2]$; $[1, 3]$; $[1, 3]$; $[2, 1]$. \n\nIn the second example the list of conversations will change in the following way:\n\n $[]$; $[2]$; $[3, 2]$; $[3, 2]$; $[1, 3, 2]$; and then the list will not change till the end."} +{"id": "03546", "text": "You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game.\n\nThe boss battle consists of $n$ turns. During each turn, you will get several cards. Each card has two parameters: its cost $c_i$ and damage $d_i$. You may play some of your cards during each turn in some sequence (you choose the cards and the exact order they are played), as long as the total cost of the cards you play during the turn does not exceed $3$. After playing some (possibly zero) cards, you end your turn, and all cards you didn't play are discarded. Note that you can use each card at most once.\n\nYour character has also found an artifact that boosts the damage of some of your actions: every $10$-th card you play deals double damage.\n\nWhat is the maximum possible damage you can deal during $n$ turns?\n\n\n-----Input-----\n\nThe first line contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of turns.\n\nThen $n$ blocks of input follow, the $i$-th block representing the cards you get during the $i$-th turn.\n\nEach block begins with a line containing one integer $k_i$ ($1 \\le k_i \\le 2 \\cdot 10^5$) \u2014 the number of cards you get during $i$-th turn. Then $k_i$ lines follow, each containing two integers $c_j$ and $d_j$ ($1 \\le c_j \\le 3$, $1 \\le d_j \\le 10^9$) \u2014 the parameters of the corresponding card.\n\nIt is guaranteed that $\\sum \\limits_{i = 1}^{n} k_i \\le 2 \\cdot 10^5$.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum damage you may deal.\n\n\n-----Example-----\nInput\n5\n3\n1 6\n1 7\n1 5\n2\n1 4\n1 3\n3\n1 10\n3 5\n2 3\n3\n1 15\n2 4\n1 10\n1\n1 100\n\nOutput\n263\n\n\n\n-----Note-----\n\nIn the example test the best course of action is as follows:\n\nDuring the first turn, play all three cards in any order and deal $18$ damage.\n\nDuring the second turn, play both cards and deal $7$ damage.\n\nDuring the third turn, play the first and the third card and deal $13$ damage.\n\nDuring the fourth turn, play the first and the third card and deal $25$ damage.\n\nDuring the fifth turn, play the only card, which will deal double damage ($200$)."} +{"id": "03547", "text": "You are given a string $s$ consisting of lowercase Latin letters and $q$ queries for this string.\n\nRecall that the substring $s[l; r]$ of the string $s$ is the string $s_l s_{l + 1} \\dots s_r$. For example, the substrings of \"codeforces\" are \"code\", \"force\", \"f\", \"for\", but not \"coder\" and \"top\".\n\nThere are two types of queries: $1~ pos~ c$ ($1 \\le pos \\le |s|$, $c$ is lowercase Latin letter): replace $s_{pos}$ with $c$ (set $s_{pos} := c$); $2~ l~ r$ ($1 \\le l \\le r \\le |s|$): calculate the number of distinct characters in the substring $s[l; r]$. \n\n\n-----Input-----\n\nThe first line of the input contains one string $s$ consisting of no more than $10^5$ lowercase Latin letters.\n\nThe second line of the input contains one integer $q$ ($1 \\le q \\le 10^5$) \u2014 the number of queries.\n\nThe next $q$ lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type.\n\n\n-----Output-----\n\nFor each query of the second type print the answer for it \u2014 the number of distinct characters in the required substring in this query.\n\n\n-----Examples-----\nInput\nabacaba\n5\n2 1 4\n1 4 b\n1 5 b\n2 4 6\n2 1 7\n\nOutput\n3\n1\n2\n\nInput\ndfcbbcfeeedbaea\n15\n1 6 e\n1 4 b\n2 6 14\n1 7 b\n1 12 c\n2 6 8\n2 1 6\n1 7 c\n1 2 f\n1 10 a\n2 7 9\n1 10 a\n1 14 b\n1 1 f\n2 1 11\n\nOutput\n5\n2\n5\n2\n6"} +{"id": "03548", "text": "You are given a correct solution of the sudoku puzzle. If you don't know what is the sudoku, you can read about it here.\n\nThe picture showing the correct sudoku solution:\n\n[Image]\n\nBlocks are bordered with bold black color.\n\nYour task is to change at most $9$ elements of this field (i.e. choose some $1 \\le i, j \\le 9$ and change the number at the position $(i, j)$ to any other number in range $[1; 9]$) to make it anti-sudoku. The anti-sudoku is the $9 \\times 9$ field, in which: Any number in this field is in range $[1; 9]$; each row contains at least two equal elements; each column contains at least two equal elements; each $3 \\times 3$ block (you can read what is the block in the link above) contains at least two equal elements. \n\nIt is guaranteed that the answer exists.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nEach test case consists of $9$ lines, each line consists of $9$ characters from $1$ to $9$ without any whitespaces \u2014 the correct solution of the sudoku puzzle.\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 the initial field with at most $9$ changed elements so that the obtained field is anti-sudoku. If there are several solutions, you can print any. It is guaranteed that the answer exists.\n\n\n-----Example-----\nInput\n1\n154873296\n386592714\n729641835\n863725149\n975314628\n412968357\n631457982\n598236471\n247189563\n\nOutput\n154873396\n336592714\n729645835\n863725145\n979314628\n412958357\n631457992\n998236471\n247789563"} +{"id": "03549", "text": "In this problem you will have to help Berland army with organizing their command delivery system.\n\nThere are $n$ officers in Berland army. The first officer is the commander of the army, and he does not have any superiors. Every other officer has exactly one direct superior. If officer $a$ is the direct superior of officer $b$, then we also can say that officer $b$ is a direct subordinate of officer $a$.\n\nOfficer $x$ is considered to be a subordinate (direct or indirect) of officer $y$ if one of the following conditions holds: officer $y$ is the direct superior of officer $x$; the direct superior of officer $x$ is a subordinate of officer $y$. \n\nFor example, on the picture below the subordinates of the officer $3$ are: $5, 6, 7, 8, 9$.\n\nThe structure of Berland army is organized in such a way that every officer, except for the commander, is a subordinate of the commander of the army.\n\nFormally, let's represent Berland army as a tree consisting of $n$ vertices, in which vertex $u$ corresponds to officer $u$. The parent of vertex $u$ corresponds to the direct superior of officer $u$. The root (which has index $1$) corresponds to the commander of the army.\n\nBerland War Ministry has ordered you to give answers on $q$ queries, the $i$-th query is given as $(u_i, k_i)$, where $u_i$ is some officer, and $k_i$ is a positive integer.\n\nTo process the $i$-th query imagine how a command from $u_i$ spreads to the subordinates of $u_i$. Typical DFS (depth first search) algorithm is used here.\n\nSuppose the current officer is $a$ and he spreads a command. Officer $a$ chooses $b$ \u2014 one of his direct subordinates (i.e. a child in the tree) who has not received this command yet. If there are many such direct subordinates, then $a$ chooses the one having minimal index. Officer $a$ gives a command to officer $b$. Afterwards, $b$ uses exactly the same algorithm to spread the command to its subtree. After $b$ finishes spreading the command, officer $a$ chooses the next direct subordinate again (using the same strategy). When officer $a$ cannot choose any direct subordinate who still hasn't received this command, officer $a$ finishes spreading the command.\n\nLet's look at the following example: [Image] \n\nIf officer $1$ spreads a command, officers receive it in the following order: $[1, 2, 3, 5 ,6, 8, 7, 9, 4]$.\n\nIf officer $3$ spreads a command, officers receive it in the following order: $[3, 5, 6, 8, 7, 9]$.\n\nIf officer $7$ spreads a command, officers receive it in the following order: $[7, 9]$.\n\nIf officer $9$ spreads a command, officers receive it in the following order: $[9]$.\n\nTo answer the $i$-th query $(u_i, k_i)$, construct a sequence which describes the order in which officers will receive the command if the $u_i$-th officer spreads it. Return the $k_i$-th element of the constructed list or -1 if there are fewer than $k_i$ elements in it.\n\nYou should process queries independently. A query doesn't affect the following queries.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $q$ ($2 \\le n \\le 2 \\cdot 10^5, 1 \\le q \\le 2 \\cdot 10^5$) \u2014 the number of officers in Berland army and the number of queries.\n\nThe second line of the input contains $n - 1$ integers $p_2, p_3, \\dots, p_n$ ($1 \\le p_i < i$), where $p_i$ is the index of the direct superior of the officer having the index $i$. The commander has index $1$ and doesn't have any superiors.\n\nThe next $q$ lines describe the queries. The $i$-th query is given as a pair ($u_i, k_i$) ($1 \\le u_i, k_i \\le n$), where $u_i$ is the index of the officer which starts spreading a command, and $k_i$ is the index of the required officer in the command spreading sequence.\n\n\n-----Output-----\n\nPrint $q$ numbers, where the $i$-th number is the officer at the position $k_i$ in the list which describes the order in which officers will receive the command if it starts spreading from officer $u_i$. Print \"-1\" if the number of officers which receive the command is less than $k_i$.\n\nYou should process queries independently. They do not affect each other.\n\n\n-----Example-----\nInput\n9 6\n1 1 1 3 5 3 5 7\n3 1\n1 5\n3 4\n7 3\n1 8\n1 9\n\nOutput\n3\n6\n8\n-1\n9\n4"} +{"id": "03550", "text": "Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $a$ coins, Barbara has $b$ coins and Cerene has $c$ coins. Recently Polycarp has returned from the trip around the world and brought $n$ coins.\n\nHe wants to distribute all these $n$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $A$ coins to Alice, $B$ coins to Barbara and $C$ coins to Cerene ($A+B+C=n$), then $a + A = b + B = c + C$.\n\nNote that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.\n\nYour task is to find out if it is possible to distribute all $n$ coins between sisters in a way described above.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases.\n\nThe next $t$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $a, b, c$ and $n$ ($1 \\le a, b, c, n \\le 10^8$) \u2014 the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.\n\n\n-----Output-----\n\nFor each test case, print \"YES\" if Polycarp can distribute all $n$ coins between his sisters and \"NO\" otherwise.\n\n\n-----Example-----\nInput\n5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3\n\nOutput\nYES\nYES\nNO\nNO\nYES"} +{"id": "03551", "text": "Let's define $p_i(n)$ as the following permutation: $[i, 1, 2, \\dots, i - 1, i + 1, \\dots, n]$. This means that the $i$-th permutation is almost identity (i.e. which maps every element to itself) permutation but the element $i$ is on the first position. Examples: $p_1(4) = [1, 2, 3, 4]$; $p_2(4) = [2, 1, 3, 4]$; $p_3(4) = [3, 1, 2, 4]$; $p_4(4) = [4, 1, 2, 3]$. \n\nYou are given an array $x_1, x_2, \\dots, x_m$ ($1 \\le x_i \\le n$).\n\nLet $pos(p, val)$ be the position of the element $val$ in $p$. So, $pos(p_1(4), 3) = 3, pos(p_2(4), 2) = 1, pos(p_4(4), 4) = 1$.\n\nLet's define a function $f(p) = \\sum\\limits_{i=1}^{m - 1} |pos(p, x_i) - pos(p, x_{i + 1})|$, where $|val|$ is the absolute value of $val$. This function means the sum of distances between adjacent elements of $x$ in $p$.\n\nYour task is to calculate $f(p_1(n)), f(p_2(n)), \\dots, f(p_n(n))$.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($2 \\le n, m \\le 2 \\cdot 10^5$) \u2014 the number of elements in each permutation and the number of elements in $x$.\n\nThe second line of the input contains $m$ integers ($m$, not $n$) $x_1, x_2, \\dots, x_m$ ($1 \\le x_i \\le n$), where $x_i$ is the $i$-th element of $x$. Elements of $x$ can repeat and appear in arbitrary order.\n\n\n-----Output-----\n\nPrint $n$ integers: $f(p_1(n)), f(p_2(n)), \\dots, f(p_n(n))$.\n\n\n-----Examples-----\nInput\n4 4\n1 2 3 4\n\nOutput\n3 4 6 5 \n\nInput\n5 5\n2 1 5 3 5\n\nOutput\n9 8 12 6 8 \n\nInput\n2 10\n1 2 1 1 2 2 2 2 2 2\n\nOutput\n3 3 \n\n\n\n-----Note-----\n\nConsider the first example:\n\n$x = [1, 2, 3, 4]$, so for the permutation $p_1(4) = [1, 2, 3, 4]$ the answer is $|1 - 2| + |2 - 3| + |3 - 4| = 3$; for the permutation $p_2(4) = [2, 1, 3, 4]$ the answer is $|2 - 1| + |1 - 3| + |3 - 4| = 4$; for the permutation $p_3(4) = [3, 1, 2, 4]$ the answer is $|2 - 3| + |3 - 1| + |1 - 4| = 6$; for the permutation $p_4(4) = [4, 1, 2, 3]$ the answer is $|2 - 3| + |3 - 4| + |4 - 1| = 5$. \n\nConsider the second example:\n\n$x = [2, 1, 5, 3, 5]$, so for the permutation $p_1(5) = [1, 2, 3, 4, 5]$ the answer is $|2 - 1| + |1 - 5| + |5 - 3| + |3 - 5| = 9$; for the permutation $p_2(5) = [2, 1, 3, 4, 5]$ the answer is $|1 - 2| + |2 - 5| + |5 - 3| + |3 - 5| = 8$; for the permutation $p_3(5) = [3, 1, 2, 4, 5]$ the answer is $|3 - 2| + |2 - 5| + |5 - 1| + |1 - 5| = 12$; for the permutation $p_4(5) = [4, 1, 2, 3, 5]$ the answer is $|3 - 2| + |2 - 5| + |5 - 4| + |4 - 5| = 6$; for the permutation $p_5(5) = [5, 1, 2, 3, 4]$ the answer is $|3 - 2| + |2 - 1| + |1 - 4| + |4 - 1| = 8$."} +{"id": "03552", "text": "You are given a rooted tree consisting of $n$ vertices numbered from $1$ to $n$. The root of the tree is a vertex number $1$.\n\nA tree is a connected undirected graph with $n-1$ edges.\n\nYou are given $m$ queries. The $i$-th query consists of the set of $k_i$ distinct vertices $v_i[1], v_i[2], \\dots, v_i[k_i]$. Your task is to say if there is a path from the root to some vertex $u$ such that each of the given $k$ vertices is either belongs to this path or has the distance $1$ to some vertex of this path.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($2 \\le n \\le 2 \\cdot 10^5$, $1 \\le m \\le 2 \\cdot 10^5$) \u2014 the number of vertices in the tree and the number of queries.\n\nEach of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \\le u_i, v_i \\le n, u_i \\ne v_i$).\n\nIt is guaranteed that the given edges form a tree.\n\nThe next $m$ lines describe queries. The $i$-th line describes the $i$-th query and starts with the integer $k_i$ ($1 \\le k_i \\le n$) \u2014 the number of vertices in the current query. Then $k_i$ integers follow: $v_i[1], v_i[2], \\dots, v_i[k_i]$ ($1 \\le v_i[j] \\le n$), where $v_i[j]$ is the $j$-th vertex of the $i$-th query.\n\nIt is guaranteed that all vertices in a single query are distinct.\n\nIt is guaranteed that the sum of $k_i$ does not exceed $2 \\cdot 10^5$ ($\\sum\\limits_{i=1}^{m} k_i \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each query, print the answer \u2014 \"YES\", if there is a path from the root to some vertex $u$ such that each of the given $k$ vertices is either belongs to this path or has the distance $1$ to some vertex of this path and \"NO\" otherwise.\n\n\n-----Example-----\nInput\n10 6\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n7 8\n7 9\n9 10\n4 3 8 9 10\n3 2 4 6\n3 2 1 5\n3 4 8 2\n2 6 10\n3 5 4 7\n\nOutput\nYES\nYES\nYES\nYES\nNO\nNO\n\n\n\n-----Note-----\n\nThe picture corresponding to the example:\n\n[Image]\n\nConsider the queries.\n\nThe first query is $[3, 8, 9, 10]$. The answer is \"YES\" as you can choose the path from the root $1$ to the vertex $u=10$. Then vertices $[3, 9, 10]$ belong to the path from $1$ to $10$ and the vertex $8$ has distance $1$ to the vertex $7$ which also belongs to this path.\n\nThe second query is $[2, 4, 6]$. The answer is \"YES\" as you can choose the path to the vertex $u=2$. Then the vertex $4$ has distance $1$ to the vertex $1$ which belongs to this path and the vertex $6$ has distance $1$ to the vertex $2$ which belongs to this path.\n\nThe third query is $[2, 1, 5]$. The answer is \"YES\" as you can choose the path to the vertex $u=5$ and all vertices of the query belong to this path.\n\nThe fourth query is $[4, 8, 2]$. The answer is \"YES\" as you can choose the path to the vertex $u=9$ so vertices $2$ and $4$ both have distance $1$ to the vertex $1$ which belongs to this path and the vertex $8$ has distance $1$ to the vertex $7$ which belongs to this path.\n\nThe fifth and the sixth queries both have answer \"NO\" because you cannot choose suitable vertex $u$."} +{"id": "03553", "text": "There are $n$ districts in the town, the $i$-th district belongs to the $a_i$-th bandit gang. Initially, no districts are connected to each other.\n\nYou are the mayor of the city and want to build $n-1$ two-way roads to connect all districts (two districts can be connected directly or through other connected districts).\n\nIf two districts belonging to the same gang are connected directly with a road, this gang will revolt.\n\nYou don't want this so your task is to build $n-1$ two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build $n-1$ roads to satisfy all the conditions.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 500$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains one integer $n$ ($2 \\le n \\le 5000$) \u2014 the number of districts. The second line of the test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the gang the $i$-th district belongs to.\n\nIt is guaranteed that the sum of $n$ does not exceed $5000$ ($\\sum n \\le 5000$).\n\n\n-----Output-----\n\nFor each test case, print:\n\n NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement. YES on the first line and $n-1$ roads on the next $n-1$ lines. Each road should be presented as a pair of integers $x_i$ and $y_i$ ($1 \\le x_i, y_i \\le n; x_i \\ne y_i$), where $x_i$ and $y_i$ are two districts the $i$-th road connects. \n\nFor each road $i$, the condition $a[x_i] \\ne a[y_i]$ should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).\n\n\n-----Example-----\nInput\n4\n5\n1 2 2 1 3\n3\n1 1 1\n4\n1 1000 101 1000\n4\n1 2 3 4\n\nOutput\nYES\n1 3\n3 5\n5 4\n1 2\nNO\nYES\n1 2\n2 3\n3 4\nYES\n1 2\n1 3\n1 4"} +{"id": "03554", "text": "You are given a binary string of length $n$ (i. e. a string consisting of $n$ characters '0' and '1').\n\nIn one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than $k$ moves? It is possible that you do not perform any moves at all.\n\nNote that you can swap the same pair of adjacent characters with indices $i$ and $i+1$ arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.\n\nYou have to answer $q$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 10^4$) \u2014 the number of test cases.\n\nThe first line of the test case contains two integers $n$ and $k$ ($1 \\le n \\le 10^6, 1 \\le k \\le n^2$) \u2014 the length of the string and the number of moves you can perform.\n\nThe second line of the test case contains one string consisting of $n$ characters '0' and '1'.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\\sum n \\le 10^6$).\n\n\n-----Output-----\n\nFor each test case, print the answer on it: the lexicographically minimum possible string of length $n$ you can obtain from the given one if you can perform no more than $k$ moves.\n\n\n-----Example-----\nInput\n3\n8 5\n11011010\n7 9\n1111100\n7 11\n1111100\n\nOutput\n01011110\n0101111\n0011111\n\n\n\n-----Note-----\n\nIn the first example, you can change the string as follows: $1\\underline{10}11010 \\rightarrow \\underline{10}111010 \\rightarrow 0111\\underline{10}10 \\rightarrow 011\\underline{10}110 \\rightarrow 01\\underline{10}1110 \\rightarrow 01011110$. \n\nIn the third example, there are enough operations to make the string sorted."} +{"id": "03555", "text": "The only difference between easy and hard versions is constraints.\n\nYou are given $n$ segments on the coordinate axis $OX$. Segments can intersect, lie inside each other and even coincide. The $i$-th segment is $[l_i; r_i]$ ($l_i \\le r_i$) and it covers all integer points $j$ such that $l_i \\le j \\le r_i$.\n\nThe integer point is called bad if it is covered by strictly more than $k$ segments.\n\nYour task is to remove the minimum number of segments so that there are no bad points at all.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 200$) \u2014 the number of segments and the maximum number of segments by which each integer point can be covered.\n\nThe next $n$ lines contain segments. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le 200$) \u2014 the endpoints of the $i$-th segment.\n\n\n-----Output-----\n\nIn the first line print one integer $m$ ($0 \\le m \\le n$) \u2014 the minimum number of segments you need to remove so that there are no bad points.\n\nIn the second line print $m$ distinct integers $p_1, p_2, \\dots, p_m$ ($1 \\le p_i \\le n$) \u2014 indices of segments you remove in any order. If there are multiple answers, you can print any of them.\n\n\n-----Examples-----\nInput\n7 2\n11 11\n9 11\n7 8\n8 9\n7 8\n9 11\n7 9\n\nOutput\n3\n1 4 7 \n\nInput\n5 1\n29 30\n30 30\n29 29\n28 30\n30 30\n\nOutput\n3\n1 2 4 \n\nInput\n6 1\n2 3\n3 3\n2 3\n2 2\n2 3\n2 3\n\nOutput\n4\n1 3 5 6"} +{"id": "03556", "text": "There are $n$ points on a coordinate axis $OX$. The $i$-th point is located at the integer point $x_i$ and has a speed $v_i$. It is guaranteed that no two points occupy the same coordinate. All $n$ points move with the constant speed, the coordinate of the $i$-th point at the moment $t$ ($t$ can be non-integer) is calculated as $x_i + t \\cdot v_i$.\n\nConsider two points $i$ and $j$. Let $d(i, j)$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $i$ and $j$ coincide at some moment, the value $d(i, j)$ will be $0$.\n\nYour task is to calculate the value $\\sum\\limits_{1 \\le i < j \\le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of points.\n\nThe second line of the input contains $n$ integers $x_1, x_2, \\dots, x_n$ ($1 \\le x_i \\le 10^8$), where $x_i$ is the initial coordinate of the $i$-th point. It is guaranteed that all $x_i$ are distinct.\n\nThe third line of the input contains $n$ integers $v_1, v_2, \\dots, v_n$ ($-10^8 \\le v_i \\le 10^8$), where $v_i$ is the speed of the $i$-th point.\n\n\n-----Output-----\n\nPrint one integer \u2014 the value $\\sum\\limits_{1 \\le i < j \\le n}$ $d(i, j)$ (the sum of minimum distances over all pairs of points).\n\n\n-----Examples-----\nInput\n3\n1 3 2\n-100 2 3\n\nOutput\n3\n\nInput\n5\n2 1 4 3 5\n2 2 2 3 4\n\nOutput\n19\n\nInput\n2\n2 1\n-3 0\n\nOutput\n0"} +{"id": "03557", "text": "You are given a weighted tree consisting of $n$ vertices. Recall that a tree is a connected graph without cycles. Vertices $u_i$ and $v_i$ are connected by an edge with weight $w_i$.\n\nYou are given $m$ queries. The $i$-th query is given as an integer $q_i$. In this query you need to calculate the number of pairs of vertices $(u, v)$ ($u < v$) such that the maximum weight of an edge on a simple path between $u$ and $v$ doesn't exceed $q_i$.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$) \u2014 the number of vertices in the tree and the number of queries.\n\nEach of the next $n - 1$ lines describes an edge of the tree. Edge $i$ is denoted by three integers $u_i$, $v_i$ and $w_i$ \u2014 the labels of vertices it connects ($1 \\le u_i, v_i \\le n$, $u_i \\ne v_i$) and the weight of the edge ($1 \\le w_i \\le 2 \\cdot 10^5$). It is guaranteed that the given edges form a tree.\n\nThe last line of the input contains $m$ integers $q_1, q_2, \\dots, q_m$ ($1 \\le q_i \\le 2 \\cdot 10^5$), where $q_i$ is the maximum weight of an edge in the $i$-th query.\n\n\n-----Output-----\n\nPrint $m$ integers \u2014 the answers to the queries. The $i$-th value should be equal to the number of pairs of vertices $(u, v)$ ($u < v$) such that the maximum weight of an edge on a simple path between $u$ and $v$ doesn't exceed $q_i$.\n\nQueries are numbered from $1$ to $m$ in the order of the input.\n\n\n-----Examples-----\nInput\n7 5\n1 2 1\n3 2 3\n2 4 1\n4 5 2\n5 7 4\n3 6 2\n5 2 3 4 1\n\nOutput\n21 7 15 21 3 \n\nInput\n1 2\n1 2\n\nOutput\n0 0 \n\nInput\n3 3\n1 2 1\n2 3 2\n1 3 2\n\nOutput\n1 3 3 \n\n\n\n-----Note-----\n\nThe picture shows the tree from the first example: [Image]"} +{"id": "03558", "text": "You are given the array $a$ consisting of $n$ positive (greater than zero) integers.\n\nIn one move, you can choose two indices $i$ and $j$ ($i \\ne j$) such that the absolute difference between $a_i$ and $a_j$ is no more than one ($|a_i - a_j| \\le 1$) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).\n\nYour task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains one integer $n$ ($1 \\le n \\le 50$) \u2014 the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$), where $a_i$ is the $i$-th element of $a$.\n\n\n-----Output-----\n\nFor each test case, print the answer: \"YES\" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or \"NO\" otherwise.\n\n\n-----Example-----\nInput\n5\n3\n1 2 2\n4\n5 5 5 5\n3\n1 2 4\n4\n1 3 4 4\n1\n100\n\nOutput\nYES\nYES\nNO\nNO\nYES\n\n\n\n-----Note-----\n\nIn the first test case of the example, we can perform the following sequence of moves: choose $i=1$ and $j=3$ and remove $a_i$ (so $a$ becomes $[2; 2]$); choose $i=1$ and $j=2$ and remove $a_j$ (so $a$ becomes $[2]$). \n\nIn the second test case of the example, we can choose any possible $i$ and $j$ any move and it doesn't matter which element we remove.\n\nIn the third test case of the example, there is no way to get rid of $2$ and $4$."} +{"id": "03559", "text": "You are given two huge binary integer numbers $a$ and $b$ of lengths $n$ and $m$ respectively. You will repeat the following process: if $b > 0$, then add to the answer the value $a~ \\&~ b$ and divide $b$ by $2$ rounding down (i.e. remove the last digit of $b$), and repeat the process again, otherwise stop the process.\n\nThe value $a~ \\&~ b$ means bitwise AND of $a$ and $b$. Your task is to calculate the answer modulo $998244353$.\n\nNote that you should add the value $a~ \\&~ b$ to the answer in decimal notation, not in binary. So your task is to calculate the answer in decimal notation. For example, if $a = 1010_2~ (10_{10})$ and $b = 1000_2~ (8_{10})$, then the value $a~ \\&~ b$ will be equal to $8$, not to $1000$.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$) \u2014 the length of $a$ and the length of $b$ correspondingly.\n\nThe second line of the input contains one huge integer $a$. It is guaranteed that this number consists of exactly $n$ zeroes and ones and the first digit is always $1$.\n\nThe third line of the input contains one huge integer $b$. It is guaranteed that this number consists of exactly $m$ zeroes and ones and the first digit is always $1$.\n\n\n-----Output-----\n\nPrint the answer to this problem in decimal notation modulo $998244353$.\n\n\n-----Examples-----\nInput\n4 4\n1010\n1101\n\nOutput\n12\n\nInput\n4 5\n1001\n10101\n\nOutput\n11\n\n\n\n-----Note-----\n\nThe algorithm for the first example: add to the answer $1010_2~ \\&~ 1101_2 = 1000_2 = 8_{10}$ and set $b := 110$; add to the answer $1010_2~ \\&~ 110_2 = 10_2 = 2_{10}$ and set $b := 11$; add to the answer $1010_2~ \\&~ 11_2 = 10_2 = 2_{10}$ and set $b := 1$; add to the answer $1010_2~ \\&~ 1_2 = 0_2 = 0_{10}$ and set $b := 0$. \n\nSo the answer is $8 + 2 + 2 + 0 = 12$.\n\nThe algorithm for the second example: add to the answer $1001_2~ \\&~ 10101_2 = 1_2 = 1_{10}$ and set $b := 1010$; add to the answer $1001_2~ \\&~ 1010_2 = 1000_2 = 8_{10}$ and set $b := 101$; add to the answer $1001_2~ \\&~ 101_2 = 1_2 = 1_{10}$ and set $b := 10$; add to the answer $1001_2~ \\&~ 10_2 = 0_2 = 0_{10}$ and set $b := 1$; add to the answer $1001_2~ \\&~ 1_2 = 1_2 = 1_{10}$ and set $b := 0$. \n\nSo the answer is $1 + 8 + 1 + 0 + 1 = 11$."} +{"id": "03560", "text": "You are given a positive integer $n$, it is guaranteed that $n$ is even (i.e. divisible by $2$).\n\nYou want to construct the array $a$ of length $n$ such that: The first $\\frac{n}{2}$ elements of $a$ are even (divisible by $2$); the second $\\frac{n}{2}$ elements of $a$ are odd (not divisible by $2$); all elements of $a$ are distinct and positive; the sum of the first half equals to the sum of the second half ($\\sum\\limits_{i=1}^{\\frac{n}{2}} a_i = \\sum\\limits_{i=\\frac{n}{2} + 1}^{n} a_i$). \n\nIf there are multiple answers, you can print any. It is not guaranteed that the answer exists.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of the array. It is guaranteed that that $n$ is even (i.e. divisible by $2$).\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$ ($\\sum n \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 \"NO\" (without quotes), if there is no suitable answer for the given test case or \"YES\" in the first line and any suitable array $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) satisfying conditions from the problem statement on the second line.\n\n\n-----Example-----\nInput\n5\n2\n4\n6\n8\n10\n\nOutput\nNO\nYES\n2 4 1 5\nNO\nYES\n2 4 6 8 1 3 5 11\nNO"} +{"id": "03561", "text": "Pay attention to the non-standard memory limit in this problem.\n\nIn order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution.\n\nThe array $a=[a_1, a_2, \\ldots, a_n]$ ($1 \\le a_i \\le n$) is given. Its element $a_i$ is called special if there exists a pair of indices $l$ and $r$ ($1 \\le l < r \\le n$) such that $a_i = a_l + a_{l+1} + \\ldots + a_r$. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not).\n\nPrint the number of special elements of the given array $a$.\n\nFor example, if $n=9$ and $a=[3,1,4,1,5,9,2,6,5]$, then the answer is $5$: $a_3=4$ is a special element, since $a_3=4=a_1+a_2=3+1$; $a_5=5$ is a special element, since $a_5=5=a_2+a_3=1+4$; $a_6=9$ is a special element, since $a_6=9=a_1+a_2+a_3+a_4=3+1+4+1$; $a_8=6$ is a special element, since $a_8=6=a_2+a_3+a_4=1+4+1$; $a_9=5$ is a special element, since $a_9=5=a_2+a_3=1+4$. \n\nPlease note that some of the elements of the array $a$ may be equal \u2014 if several elements are equal and special, then all of them should be counted in the answer.\n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases in the input. Then $t$ test cases follow.\n\nEach test case is given in two lines. The first line contains an integer $n$ ($1 \\le n \\le 8000$) \u2014 the length of the array $a$. The second line contains integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$).\n\nIt is guaranteed that the sum of the values of $n$ for all test cases in the input does not exceed $8000$.\n\n\n-----Output-----\n\nPrint $t$ numbers \u2014 the number of special elements for each of the given arrays.\n\n\n-----Example-----\nInput\n5\n9\n3 1 4 1 5 9 2 6 5\n3\n1 1 2\n5\n1 1 1 1 1\n8\n8 7 6 5 4 3 2 1\n1\n1\n\nOutput\n5\n1\n0\n4\n0"} +{"id": "03562", "text": "You are given $n$ segments on a coordinate axis $OX$. The $i$-th segment has borders $[l_i; r_i]$. All points $x$, for which $l_i \\le x \\le r_i$ holds, belong to the $i$-th segment.\n\nYour task is to choose the maximum by size (the number of segments) subset of the given set of segments such that each pair of segments in this subset either non-intersecting or one of them lies inside the other one.\n\nTwo segments $[l_i; r_i]$ and $[l_j; r_j]$ are non-intersecting if they have no common points. For example, segments $[1; 2]$ and $[3; 4]$, $[1; 3]$ and $[5; 5]$ are non-intersecting, while segments $[1; 2]$ and $[2; 3]$, $[1; 2]$ and $[2; 2]$ are intersecting.\n\nThe segment $[l_i; r_i]$ lies inside the segment $[l_j; r_j]$ if $l_j \\le l_i$ and $r_i \\le r_j$. For example, segments $[2; 2]$, $[2, 3]$, $[3; 4]$ and $[2; 4]$ lie inside the segment $[2; 4]$, while $[2; 5]$ and $[1; 4]$ are not.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains one integer $n$ ($1 \\le n \\le 3000$) \u2014 the number of segments. The next $n$ lines describe segments. The $i$-th segment is given as two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le 2 \\cdot 10^5$), where $l_i$ is the left border of the $i$-th segment and $r_i$ is the right border of the $i$-th segment.\n\nAdditional constraint on the input: there are no duplicates in the list of segments.\n\nIt is guaranteed that the sum of $n$ does not exceed $3000$ ($\\sum n \\le 3000$).\n\n\n-----Output-----\n\nFor each test case, print the answer: the maximum possible size of the subset of the given set of segments such that each pair of segments in this subset either non-intersecting or one of them lies inside the other one.\n\n\n-----Example-----\nInput\n4\n4\n1 5\n2 4\n2 3\n3 4\n5\n1 5\n2 3\n2 5\n3 5\n2 2\n3\n1 3\n2 4\n2 3\n7\n1 10\n2 8\n2 5\n3 4\n4 4\n6 8\n7 7\n\nOutput\n3\n4\n2\n7"} +{"id": "03563", "text": "New Year is coming and you are excited to know how many minutes remain before the New Year. You know that currently the clock shows $h$ hours and $m$ minutes, where $0 \\le hh < 24$ and $0 \\le mm < 60$. We use 24-hour time format!\n\nYour task is to find the number of minutes before the New Year. You know that New Year comes when the clock shows $0$ hours and $0$ minutes.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 1439$) \u2014 the number of test cases.\n\nThe following $t$ lines describe test cases. The $i$-th line contains the time as two integers $h$ and $m$ ($0 \\le h < 24$, $0 \\le m < 60$). It is guaranteed that this time is not a midnight, i.e. the following two conditions can't be met at the same time: $h=0$ and $m=0$. It is guaranteed that both $h$ and $m$ are given without leading zeros.\n\n\n-----Output-----\n\nFor each test case, print the answer on it \u2014 the number of minutes before the New Year.\n\n\n-----Example-----\nInput\n5\n23 55\n23 0\n0 1\n4 20\n23 59\n\nOutput\n5\n60\n1439\n1180\n1"} +{"id": "03564", "text": "There is a robot on a coordinate plane. Initially, the robot is located at the point $(0, 0)$. Its path is described as a string $s$ of length $n$ consisting of characters 'L', 'R', 'U', 'D'.\n\nEach of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $(x, y)$ to the point $(x - 1, y)$; 'R' (right): means that the robot moves from the point $(x, y)$ to the point $(x + 1, y)$; 'U' (up): means that the robot moves from the point $(x, y)$ to the point $(x, y + 1)$; 'D' (down): means that the robot moves from the point $(x, y)$ to the point $(x, y - 1)$. \n\nThe company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $(x_e, y_e)$, then after optimization (i.e. removing some single substring from $s$) the robot also ends its path at the point $(x_e, y_e)$.\n\nThis optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $s$).\n\nRecall that the substring of $s$ is such string that can be obtained from $s$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of \"LURLLR\" are \"LU\", \"LR\", \"LURLLR\", \"URL\", but not \"RR\" and \"UL\".\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases.\n\nThe next $2t$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the length of the robot's path. The second line of the test case contains one string $s$ consisting of $n$ characters 'L', 'R', 'U', 'D' \u2014 the robot's path.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$ ($\\sum n \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $l$ and $r$ such that $1 \\le l \\le r \\le n$ \u2014 endpoints of the substring you remove. The value $r-l+1$ should be minimum possible. If there are several answers, print any of them.\n\n\n-----Example-----\nInput\n4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR\n\nOutput\n1 2\n1 4\n3 4\n-1"} +{"id": "03565", "text": "You have $n$ students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the $i$-th student skill is denoted by an integer $a_i$ (different students can have the same skills).\n\nSo, about the teams. Firstly, these two teams should have the same size. Two more constraints: The first team should consist of students with distinct skills (i.e. all skills in the first team are unique). The second team should consist of students with the same skills (i.e. all skills in the second team are equal). \n\nNote that it is permissible that some student of the first team has the same skill as a student of the second team.\n\nConsider some examples (skills are given): $[1, 2, 3]$, $[4, 4]$ is not a good pair of teams because sizes should be the same; $[1, 1, 2]$, $[3, 3, 3]$ is not a good pair of teams because the first team should not contain students with the same skills; $[1, 2, 3]$, $[3, 4, 4]$ is not a good pair of teams because the second team should contain students with the same skills; $[1, 2, 3]$, $[3, 3, 3]$ is a good pair of teams; $[5]$, $[6]$ is a good pair of teams. \n\nYour task is to find the maximum possible size $x$ for which it is possible to compose a valid pair of teams, where each team size is $x$ (skills in the first team needed to be unique, skills in the second team should be the same between them). A student cannot be part of more than one team.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of students. The second line of the test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le n$), where $a_i$ is the skill of the $i$-th student. Different students can have the same skills.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$ ($\\sum n \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 the maximum possible size $x$ for which it is possible to compose a valid pair of teams, where each team size is $x$.\n\n\n-----Example-----\nInput\n4\n7\n4 2 4 1 4 3 4\n5\n2 1 5 4 3\n1\n1\n4\n1 1 1 3\n\nOutput\n3\n1\n0\n2\n\n\n\n-----Note-----\n\nIn the first test case of the example, it is possible to construct two teams of size $3$: the first team is $[1, 2, 4]$ and the second team is $[4, 4, 4]$. Note, that there are some other ways to construct two valid teams of size $3$."} +{"id": "03566", "text": "You are given a tree consisting exactly of $n$ vertices. Tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a value $a_v$ assigned to it.\n\nLet $dist(x, y)$ be the distance between the vertices $x$ and $y$. The distance between the vertices is the number of edges on the simple path between them.\n\nLet's define the cost of the tree as the following value: firstly, let's fix some vertex of the tree. Let it be $v$. Then the cost of the tree is $\\sum\\limits_{i = 1}^{n} dist(i, v) \\cdot a_i$.\n\nYour task is to calculate the maximum possible cost of the tree if you can choose $v$ arbitrarily.\n\n\n-----Input-----\n\nThe first line contains one integer $n$, the number of vertices in the tree ($1 \\le n \\le 2 \\cdot 10^5$).\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2 \\cdot 10^5$), where $a_i$ is the value of the vertex $i$.\n\nEach of the next $n - 1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects ($1 \\le u_i, v_i \\le n$, $u_i \\ne v_i$).\n\nIt is guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nPrint one integer \u2014 the maximum possible cost of the tree if you can choose any vertex as $v$.\n\n\n-----Examples-----\nInput\n8\n9 4 1 7 10 1 6 5\n1 2\n2 3\n1 4\n1 5\n5 6\n5 7\n5 8\n\nOutput\n121\n\nInput\n1\n1337\n\nOutput\n0\n\n\n\n-----Note-----\n\nPicture corresponding to the first example: [Image]\n\nYou can choose the vertex $3$ as a root, then the answer will be $2 \\cdot 9 + 1 \\cdot 4 + 0 \\cdot 1 + 3 \\cdot 7 + 3 \\cdot 10 + 4 \\cdot 1 + 4 \\cdot 6 + 4 \\cdot 5 = 18 + 4 + 0 + 21 + 30 + 4 + 24 + 20 = 121$.\n\nIn the second example tree consists only of one vertex so the answer is always $0$."} +{"id": "03567", "text": "You are given an array $a$ consisting of $n$ positive integers.\n\nInitially, you have an integer $x = 0$. During one move, you can do one of the following two operations: Choose exactly one $i$ from $1$ to $n$ and increase $a_i$ by $x$ ($a_i := a_i + x$), then increase $x$ by $1$ ($x := x + 1$). Just increase $x$ by $1$ ($x := x + 1$). \n\nThe first operation can be applied no more than once to each $i$ from $1$ to $n$.\n\nYour task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $k$ (the value $k$ is given).\n\nYou have to answer $t$ independent test cases. \n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2 \\cdot 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains two integers $n$ and $k$ ($1 \\le n \\le 2 \\cdot 10^5; 1 \\le k \\le 10^9$) \u2014 the length of $a$ and the required divisior. The second line of the test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the $i$-th element of $a$.\n\nIt is guaranteed that the sum of $n$ does not exceed $2 \\cdot 10^5$ ($\\sum n \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 the minimum number of moves required to obtain such an array that each its element is divisible by $k$.\n\n\n-----Example-----\nInput\n5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8\n\nOutput\n6\n18\n0\n227\n8\n\n\n\n-----Note-----\n\nConsider the first test case of the example: $x=0$, $a = [1, 2, 1, 3]$. Just increase $x$; $x=1$, $a = [1, 2, 1, 3]$. Add $x$ to the second element and increase $x$; $x=2$, $a = [1, 3, 1, 3]$. Add $x$ to the third element and increase $x$; $x=3$, $a = [1, 3, 3, 3]$. Add $x$ to the fourth element and increase $x$; $x=4$, $a = [1, 3, 3, 6]$. Just increase $x$; $x=5$, $a = [1, 3, 3, 6]$. Add $x$ to the first element and increase $x$; $x=6$, $a = [6, 3, 3, 6]$. We obtained the required array. \n\nNote that you can't add $x$ to the same element more than once."} +{"id": "03568", "text": "Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.\n\n\nNote:\nYou may assume the greed factor is always positive. \nYou cannot assign more than one cookie to one child.\n\n\nExample 1:\n\nInput: [1,2,3], [1,1]\n\nOutput: 1\n\nExplanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. \nAnd even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.\nYou need to output 1.\n\n\n\nExample 2:\n\nInput: [1,2], [1,2,3]\n\nOutput: 2\n\nExplanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. \nYou have 3 cookies and their sizes are big enough to gratify all of the children, \nYou need to output 2."} +{"id": "03569", "text": "Given a non-negative\u00a0index k\u00a0where k \u2264\u00a033, return the kth\u00a0index row of the Pascal's triangle.\n\nNote that the row index starts from\u00a00.\n\n\nIn Pascal's triangle, each number is the sum of the two numbers directly above it.\n\nExample:\n\n\nInput: 3\nOutput: [1,3,3,1]\n\n\nFollow up:\n\nCould you optimize your algorithm to use only O(k) extra space?"} +{"id": "03570", "text": "Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).\n\nNote:\n\nNote that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.\nIn Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer -3.\n \nExample 1:\nInput: n = 00000000000000000000000000001011\nOutput: 3\nExplanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.\n\nExample 2:\nInput: n = 00000000000000000000000010000000\nOutput: 1\nExplanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.\n\nExample 3:\nInput: n = 11111111111111111111111111111101\nOutput: 31\nExplanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.\n \n\nConstraints:\n\nThe input must be a binary string of length 32.\n \nFollow up: If this function is called many times, how would you optimize it?"} +{"id": "03571", "text": "Given a non-empty array of digits\u00a0representing a non-negative integer, plus one to the integer.\n\nThe digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.\n\nYou may assume the integer does not contain any leading zero, except the number 0 itself.\n\nExample 1:\n\n\nInput: [1,2,3]\nOutput: [1,2,4]\nExplanation: The array represents the integer 123.\n\n\nExample 2:\n\n\nInput: [4,3,2,1]\nOutput: [4,3,2,2]\nExplanation: The array represents the integer 4321."} +{"id": "03572", "text": "You are given two integers A and B.\nFind the largest value among A+B, A-B and A \\times B.\n\n-----Constraints-----\n - -1000 \\leq A,B \\leq 1000\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\n\n-----Output-----\nPrint the largest value among A+B, A-B and A \\times B.\n\n-----Sample Input-----\n3 1\n\n-----Sample Output-----\n4\n\n3+1=4, 3-1=2 and 3 \\times 1=3. The largest among them is 4."} +{"id": "03573", "text": "We have N points in the two-dimensional plane. The coordinates of the i-th point are (X_i,Y_i).\nAmong them, we are looking for the points such that the distance from the origin is at most D. How many such points are there?\nWe remind you that the distance between the origin and the point (p, q) can be represented as \\sqrt{p^2+q^2}.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2\\times 10^5\n - 0 \\leq D \\leq 2\\times 10^5\n - |X_i|,|Y_i| \\leq 2\\times 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN D\nX_1 Y_1\n\\vdots\nX_N Y_N\n\n-----Output-----\nPrint an integer representing the number of points such that the distance from the origin is at most D.\n\n-----Sample Input-----\n4 5\n0 5\n-2 4\n3 4\n4 -4\n\n-----Sample Output-----\n3\n\nThe distance between the origin and each of the given points is as follows:\n - \\sqrt{0^2+5^2}=5\n - \\sqrt{(-2)^2+4^2}=4.472\\ldots\n - \\sqrt{3^2+4^2}=5\n - \\sqrt{4^2+(-4)^2}=5.656\\ldots\nThus, we have three points such that the distance from the origin is at most 5."} +{"id": "03574", "text": "An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.\nGiven an integer N, determine whether it is a Harshad number.\n\n-----Constraints-----\n - 1?N?10^8\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint Yes if N is a Harshad number; print No otherwise.\n\n-----Sample Input-----\n12\n\n-----Sample Output-----\nYes\n\nf(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number."} +{"id": "03575", "text": "There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N.\nSpot i is at the point with coordinate A_i.\nIt costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.\nYou planned a trip along the axis.\nIn this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.\nHowever, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i.\nYou will visit the remaining spots as planned in the order they are numbered.\nYou will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.\nFor each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.\n\n-----Constraints-----\n - 2 \\leq N \\leq 10^5\n - -5000 \\leq A_i \\leq 5000 (1 \\leq i \\leq N)\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint N lines.\nIn the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.\n\n-----Sample Input-----\n3\n3 5 -1\n\n-----Sample Output-----\n12\n8\n10\n\nSpot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively.\nFor each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:\n - For i = 1, the course of the trip is 0 \\rightarrow 5 \\rightarrow -1 \\rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.\n - For i = 2, the course of the trip is 0 \\rightarrow 3 \\rightarrow -1 \\rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.\n - For i = 3, the course of the trip is 0 \\rightarrow 3 \\rightarrow 5 \\rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen."} +{"id": "03576", "text": "Given a lowercase English letter c, determine whether it is a vowel. Here, there are five vowels in the English alphabet: a, e, i, o and u.\n\n-----Constraints-----\n - c is a lowercase English letter.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nc\n\n-----Output-----\nIf c is a vowel, print vowel. Otherwise, print consonant.\n\n-----Sample Input-----\na\n\n-----Sample Output-----\nvowel\n\nSince a is a vowel, print vowel."} +{"id": "03577", "text": "Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.\nIn the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 \u2266 i \u2266 |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.\nJiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.\n\n-----Constraints-----\n - 1 \u2266 |S| \u2266 10^5\n - Each character in S is B or W.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the minimum number of new stones that Jiro needs to place for his purpose.\n\n-----Sample Input-----\nBBBWW\n\n-----Sample Output-----\n1\n\nBy placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.\nIn either way, Jiro's purpose can be achieved by placing one stone."} +{"id": "03578", "text": "AtCoDeer the deer has found two positive integers, a and b.\nDetermine whether the concatenation of a and b in this order is a square number.\n\n-----Constraints-----\n - 1 \u2264 a,b \u2264 100\n - a and b are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\na b\n\n-----Output-----\nIf the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.\n\n-----Sample Input-----\n1 21\n\n-----Sample Output-----\nYes\n\nAs 121 = 11 \u00d7 11, it is a square number."} +{"id": "03579", "text": "You are given an integer sequence of length N, a_1,a_2,...,a_N.\nFor each 1\u2264i\u2264N, you have three choices: add 1 to a_i, subtract 1 from a_i or do nothing.\nAfter these operations, you select an integer X and count the number of i such that a_i=X.\nMaximize this count by making optimal choices.\n\n-----Constraints-----\n - 1\u2264N\u226410^5\n - 0\u2264a_i<10^5 (1\u2264i\u2264N)\n - a_i is an integer.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\na_1 a_2 .. a_N\n\n-----Output-----\nPrint the maximum possible number of i such that a_i=X.\n\n-----Sample Input-----\n7\n3 1 4 1 5 9 2\n\n-----Sample Output-----\n4\n\nFor example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count."} +{"id": "03580", "text": "We have an N \\times N square grid.\nWe will paint each square in the grid either black or white.\nIf we paint exactly A squares white, how many squares will be painted black?\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 0 \\leq A \\leq N^2\n\n-----Inputs-----\nInput is given from Standard Input in the following format:\nN\nA\n\n-----Outputs-----\nPrint the number of squares that will be painted black.\n\n-----Sample Input-----\n3\n4\n\n-----Sample Output-----\n5\n\nThere are nine squares in a 3 \\times 3 square grid.\nFour of them will be painted white, so the remaining five squares will be painted black."} +{"id": "03581", "text": "Three poles stand evenly spaced along a line. Their heights are a, b and c meters, from left to right.\nWe will call the arrangement of the poles beautiful if the tops of the poles lie on the same line, that is, b-a = c-b.\nDetermine whether the arrangement of the poles is beautiful.\n\n-----Constraints-----\n - 1 \\leq a,b,c \\leq 100\n - a, b and c are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\na b c\n\n-----Output-----\nPrint YES if the arrangement of the poles is beautiful; print NO otherwise.\n\n-----Sample Input-----\n2 4 6\n\n-----Sample Output-----\nYES\n\nSince 4-2 = 6-4, this arrangement of poles is beautiful."} +{"id": "03582", "text": "It is September 9 in Japan now.\nYou are given a two-digit integer N. Answer the question: Is 9 contained in the decimal notation of N?\n\n-----Constraints-----\n - 10\u2264N\u226499\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nIf 9 is contained in the decimal notation of N, print Yes; if not, print No.\n\n-----Sample Input-----\n29\n\n-----Sample Output-----\nYes\n\nThe one's digit of 29 is 9."} +{"id": "03583", "text": "There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.\nInitially, you are in Square X.\nYou can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.\nHowever, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.\nIt is guaranteed that there is no toll gate in Square 0, Square X and Square N.\nFind the minimum cost incurred before reaching the goal.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 1 \\leq M \\leq 100\n - 1 \\leq X \\leq N - 1\n - 1 \\leq A_1 < A_2 < ... < A_M \\leq N\n - A_i \\neq X\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M X\nA_1 A_2 ... A_M\n\n-----Output-----\nPrint the minimum cost incurred before reaching the goal.\n\n-----Sample Input-----\n5 3 3\n1 2 4\n\n-----Sample Output-----\n1\n\nThe optimal solution is as follows:\n - First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.\n - Then, travel from Square 4 to Square 5. This time, no cost is incurred.\n - Now, we are in Square 5 and we have reached the goal.\nIn this case, the total cost incurred is 1."} +{"id": "03584", "text": "We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).\n\nInitially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..\n\nHowever, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.\n\nDetermine if square1001 can achieve his objective. \n\n-----Constraints-----\n - H is an integer between 1 and 50 (inclusive).\n - W is an integer between 1 and 50 (inclusive).\n - For every (i, j) (1 \\leq i \\leq H, 1 \\leq j \\leq W), s_{i, j} is # or ..\n\n-----Input-----\nInput is given from Standard Input in the following format:\nH W\ns_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}\ns_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}\n : :\ns_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}\n\n-----Output-----\nIf square1001 can achieve his objective, print Yes; if he cannot, print No.\n\n-----Sample Input-----\n3 3\n.#.\n###\n.#.\n\n-----Sample Output-----\nYes\n\nOne possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars."} +{"id": "03585", "text": "Two students of AtCoder Kindergarten are fighting over candy packs.\nThere are three candy packs, each of which contains a, b, and c candies, respectively.\nTeacher Evi is trying to distribute the packs between the two students so that each student gets the same number of candies. Determine whether it is possible.\nNote that Evi cannot take candies out of the packs, and the whole contents of each pack must be given to one of the students.\n\n-----Constraints-----\n - 1 \u2266 a, b, c \u2266 100\n\n-----Input-----\nThe input is given from Standard Input in the following format:\na b c\n\n-----Output-----\nIf it is possible to distribute the packs so that each student gets the same number of candies, print Yes. Otherwise, print No.\n\n-----Sample Input-----\n10 30 20\n\n-----Sample Output-----\nYes\n\nGive the pack with 30 candies to one student, and give the two packs with 10 and 20 candies to the other. Then, each gets 30 candies."} +{"id": "03586", "text": "A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L8, we should print Left."} +{"id": "03587", "text": "Joisino is planning to open a shop in a shopping street.\nEach of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods.\nThere are already N stores in the street, numbered 1 through N.\nYou are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2.\nLet c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}.\nFind the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.\n\n-----Constraints-----\n - 1\u2264N\u2264100\n - 0\u2264F_{i,j,k}\u22641\n - For every integer i such that 1\u2264i\u2264N, there exists at least one pair (j,k) such that F_{i,j,k}=1.\n - -10^7\u2264P_{i,j}\u226410^7\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nF_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2}\n:\nF_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2}\nP_{1,0} ... P_{1,10}\n:\nP_{N,0} ... P_{N,10}\n\n-----Output-----\nPrint the maximum possible profit of Joisino's shop.\n\n-----Sample Input-----\n1\n1 1 0 1 0 0 0 1 0 1\n3 4 5 6 7 8 9 -2 -3 4 -2\n\n-----Sample Output-----\n8\n\nIf her shop is open only during the periods when Shop 1 is opened, the profit will be 8, which is the maximum possible profit."} +{"id": "03588", "text": "The postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9.\nYou are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom.\n\n-----Constraints-----\n - 1\u2264A,B\u22645\n - |S|=A+B+1\n - S consists of - and digits from 0 through 9.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B\nS\n\n-----Output-----\nPrint Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise.\n\n-----Sample Input-----\n3 4\n269-6650\n\n-----Sample Output-----\nYes\n\nThe (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format."} +{"id": "03589", "text": "AtCoDeer the deer found two rectangles lying on the table, each with height 1 and width W.\nIf we consider the surface of the desk as a two-dimensional plane, the first rectangle covers the vertical range of [0,1] and the horizontal range of [a,a+W], and the second rectangle covers the vertical range of [1,2] and the horizontal range of [b,b+W], as shown in the following figure:\n\nAtCoDeer will move the second rectangle horizontally so that it connects with the first rectangle.\nFind the minimum distance it needs to be moved.\n\n-----Constraints-----\n - All input values are integers.\n - 1\u2264W\u226410^5\n - 1\u2264a,b\u226410^5\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nW a b\n\n-----Output-----\nPrint the minimum distance the second rectangle needs to be moved.\n\n-----Sample Input-----\n3 2 6\n\n-----Sample Output-----\n1\n\nThis input corresponds to the figure in the statement. In this case, the second rectangle should be moved to the left by a distance of 1."} +{"id": "03590", "text": "Print all the integers that satisfies the following in ascending order:\n - Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.\n\n-----Constraints-----\n - 1 \\leq A \\leq B \\leq 10^9\n - 1 \\leq K \\leq 100\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B K\n\n-----Output-----\nPrint all the integers that satisfies the condition above in ascending order.\n\n-----Sample Input-----\n3 8 2\n\n-----Sample Output-----\n3\n4\n7\n8\n\n - 3 is the first smallest integer among the integers between 3 and 8.\n - 4 is the second smallest integer among the integers between 3 and 8.\n - 7 is the second largest integer among the integers between 3 and 8.\n - 8 is the first largest integer among the integers between 3 and 8."} +{"id": "03591", "text": "Snuke is going to open a contest named \"AtCoder s Contest\".\nHere, s is a string of length 1 or greater, where the first character is an uppercase English letter, and the second and subsequent characters are lowercase English letters.\nSnuke has decided to abbreviate the name of the contest as \"AxC\".\nHere, x is the uppercase English letter at the beginning of s.\nGiven the name of the contest, print the abbreviation of the name.\n\n-----Constraints-----\n - The length of s is between 1 and 100, inclusive.\n - The first character in s is an uppercase English letter.\n - The second and subsequent characters in s are lowercase English letters.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nAtCoder s Contest\n\n-----Output-----\nPrint the abbreviation of the name of the contest.\n\n-----Sample Input-----\nAtCoder Beginner Contest\n\n-----Sample Output-----\nABC\n\nThe contest in which you are participating now."} +{"id": "03592", "text": "There are a total of A + B cats and dogs.\nAmong them, A are known to be cats, but the remaining B are not known to be either cats or dogs.\nDetermine if it is possible that there are exactly X cats among these A + B animals.\n\n-----Constraints-----\n - 1 \\leq A \\leq 100\n - 1 \\leq B \\leq 100\n - 1 \\leq X \\leq 200\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B X\n\n-----Output-----\nIf it is possible that there are exactly X cats, print YES; if it is impossible, print NO.\n\n-----Sample Input-----\n3 5 4\n\n-----Sample Output-----\nYES\n\nIf there are one cat and four dogs among the B = 5 animals, there are X = 4 cats in total."} +{"id": "03593", "text": "We have a sandglass that runs for X seconds. The sand drops from the upper bulb at a rate of 1 gram per second. That is, the upper bulb initially contains X grams of sand.\nHow many grams of sand will the upper bulb contains after t seconds?\n\n-----Constraints-----\n - 1\u2264X\u226410^9\n - 1\u2264t\u226410^9\n - X and t are integers.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nX t\n\n-----Output-----\nPrint the number of sand in the upper bulb after t second.\n\n-----Sample Input-----\n100 17\n\n-----Sample Output-----\n83\n\n17 out of the initial 100 grams of sand will be consumed, resulting in 83 grams."} +{"id": "03594", "text": "Given N integers A_1, ..., A_N, compute A_1 \\times ... \\times A_N.\nHowever, if the result exceeds 10^{18}, print -1 instead.\n\n-----Constraints-----\n - 2 \\leq N \\leq 10^5\n - 0 \\leq A_i \\leq 10^{18}\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 ... A_N\n\n-----Output-----\nPrint the value A_1 \\times ... \\times A_N as an integer, or -1 if the value exceeds 10^{18}.\n\n-----Sample Input-----\n2\n1000000000 1000000000\n\n-----Sample Output-----\n1000000000000000000\n\nWe have 1000000000 \\times 1000000000 = 1000000000000000000."} +{"id": "03595", "text": "E869120 has A 1-yen coins and infinitely many 500-yen coins.\n\nDetermine if he can pay exactly N yen using only these coins.\n\n-----Constraints-----\n - N is an integer between 1 and 10000 (inclusive).\n - A is an integer between 0 and 1000 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA\n\n-----Output-----\nIf E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.\n\n-----Sample Input-----\n2018\n218\n\n-----Sample Output-----\nYes\n\nWe can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes."} +{"id": "03596", "text": "Takahashi has a strong stomach. He never gets a stomachache from eating something whose \"best-by\" date is at most X days earlier.\nHe gets a stomachache if the \"best-by\" date of the food is X+1 or more days earlier, though.\nOther than that, he finds the food delicious if he eats it not later than the \"best-by\" date. Otherwise, he does not find it delicious.\nTakahashi bought some food A days before the \"best-by\" date, and ate it B days after he bought it.\nWrite a program that outputs delicious if he found it delicious, safe if he did not found it delicious but did not get a stomachache either, and dangerous if he got a stomachache.\n\n-----Constraints-----\n - 1 \u2264 X,A,B \u2264 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX A B\n\n-----Output-----\nPrint delicious if Takahashi found the food delicious; print safe if he neither found it delicious nor got a stomachache; print dangerous if he got a stomachache.\n\n-----Sample Input-----\n4 3 6\n\n-----Sample Output-----\nsafe\n\nHe ate the food three days after the \"best-by\" date. It was not delicious or harmful for him."} +{"id": "03597", "text": "Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^9\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the largest square number not exceeding N.\n\n-----Sample Input-----\n10\n\n-----Sample Output-----\n9\n\n10 is not square, but 9 = 3 \u00d7 3 is. Thus, we print 9."} +{"id": "03598", "text": "AtCoDeer the deer is seeing a quick report of election results on TV.\nTwo candidates are standing for the election: Takahashi and Aoki.\nThe report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes.\nAtCoDeer has checked the report N times, and when he checked it for the i-th (1\u2266i\u2266N) time, the ratio was T_i:A_i.\nIt is known that each candidate had at least one vote when he checked the report for the first time.\nFind the minimum possible total number of votes obtained by the two candidates when he checked the report for the N-th time.\nIt can be assumed that the number of votes obtained by each candidate never decreases.\n\n-----Constraints-----\n - 1\u2266N\u22661000\n - 1\u2266T_i,A_i\u22661000 (1\u2266i\u2266N)\n - T_i and A_i (1\u2266i\u2266N) are coprime.\n - It is guaranteed that the correct answer is at most 10^{18}.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\nT_1 A_1\nT_2 A_2\n:\nT_N A_N\n\n-----Output-----\nPrint the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.\n\n-----Sample Input-----\n3\n2 3\n1 1\n3 2\n\n-----Sample Output-----\n10\n\nWhen the numbers of votes obtained by the two candidates change as 2,3 \u2192 3,3 \u2192 6,4, the total number of votes at the end is 10, which is the minimum possible number."} +{"id": "03599", "text": "You are given a string S consisting of lowercase English letters. Determine whether all the characters in S are different.\n\n-----Constraints-----\n - 2 \u2264 |S| \u2264 26, where |S| denotes the length of S.\n - S consists of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nIf all the characters in S are different, print yes (case-sensitive); otherwise, print no.\n\n-----Sample Input-----\nuncopyrightable\n\n-----Sample Output-----\nyes\n"} +{"id": "03600", "text": "There are N people standing in a row from west to east.\nEach person is facing east or west.\nThe directions of the people is given as a string S of length N.\nThe i-th person from the west is facing east if S_i = E, and west if S_i = W.\nYou will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.\nHere, we do not care which direction the leader is facing.\nThe people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.\nFind the minimum number of people who have to change their directions.\n\n-----Constraints-----\n - 2 \\leq N \\leq 3 \\times 10^5\n - |S| = N\n - S_i is E or W.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS\n\n-----Output-----\nPrint the minimum number of people who have to change their directions.\n\n-----Sample Input-----\n5\nWEEWW\n\n-----Sample Output-----\n1\n\nAssume that we appoint the third person from the west as the leader.\nThen, the first person from the west needs to face east and has to turn around.\nThe other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.\nIt is not possible to have 0 people who have to change their directions, so the answer is 1."} +{"id": "03601", "text": "There are N cities and M roads.\nThe i-th road (1\u2264i\u2264M) connects two cities a_i and b_i (1\u2264a_i,b_i\u2264N) bidirectionally.\nThere may be more than one road that connects the same pair of two cities.\nFor each city, how many roads are connected to the city?\n\n-----Constraints-----\n - 2\u2264N,M\u226450\n - 1\u2264a_i,b_i\u2264N\n - a_i \u2260 b_i\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format: \nN M\na_1 b_1\n: \na_M b_M\n\n-----Output-----\nPrint the answer in N lines.\nIn the i-th line (1\u2264i\u2264N), print the number of roads connected to city i.\n\n-----Sample Input-----\n4 3\n1 2\n2 3\n1 4\n\n-----Sample Output-----\n2\n2\n1\n1\n\n - City 1 is connected to the 1-st and 3-rd roads.\n - City 2 is connected to the 1-st and 2-nd roads.\n - City 3 is connected to the 2-nd road.\n - City 4 is connected to the 3-rd road."} +{"id": "03602", "text": "You are taking a computer-based examination. The examination consists of N questions, and the score allocated to the i-th question is s_i. Your answer to each question will be judged as either \"correct\" or \"incorrect\", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.\nHowever, the examination system is actually flawed, and if your grade is a multiple of 10, the system displays 0 as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?\n\n-----Constraints-----\n - All input values are integers.\n - 1 \u2264 N \u2264 100\n - 1 \u2264 s_i \u2264 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\ns_1\ns_2\n:\ns_N\n\n-----Output-----\nPrint the maximum value that can be displayed as your grade.\n\n-----Sample Input-----\n3\n5\n10\n15\n\n-----Sample Output-----\n25\n\nYour grade will be 25 if the 10-point and 15-point questions are answered correctly and the 5-point question is not, and this grade will be displayed correctly. Your grade will become 30 if the 5-point question is also answered correctly, but this grade will be incorrectly displayed as 0."} +{"id": "03603", "text": "You are given a string S of length N consisting of lowercase English letters.\nWe will cut this string at one position into two strings X and Y.\nHere, we would like to maximize the number of different letters contained in both X and Y.\nFind the largest possible number of different letters contained in both X and Y when we cut the string at the optimal position.\n\n-----Constraints-----\n - 2 \\leq N \\leq 100\n - |S| = N\n - S consists of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS\n\n-----Output-----\nPrint the largest possible number of different letters contained in both X and Y.\n\n-----Sample Input-----\n6\naabbca\n\n-----Sample Output-----\n2\n\nIf we cut the string between the third and fourth letters into X = aab and Y = bca, the letters contained in both X and Y are a and b.\nThere will never be three or more different letters contained in both X and Y, so the answer is 2."} +{"id": "03604", "text": "The weather in Takahashi's town changes day by day, in the following cycle: Sunny, Cloudy, Rainy, Sunny, Cloudy, Rainy, ...\nGiven is a string S representing the weather in the town today. Predict the weather tomorrow.\n\n-----Constraints-----\n - S is Sunny, Cloudy, or Rainy.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint a string representing the expected weather tomorrow, in the same format in which input is given.\n\n-----Sample Input-----\nSunny\n\n-----Sample Output-----\nCloudy\n\nIn Takahashi's town, a sunny day is followed by a cloudy day."} +{"id": "03605", "text": "You are parking at a parking lot. You can choose from the following two fee plans:\n - Plan 1: The fee will be A\u00d7T yen (the currency of Japan) when you park for T hours.\n - Plan 2: The fee will be B yen, regardless of the duration.\nFind the minimum fee when you park for N hours.\n\n-----Constraints-----\n - 1\u2264N\u226420\n - 1\u2264A\u2264100\n - 1\u2264B\u22642000\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN A B\n\n-----Output-----\nWhen the minimum fee is x yen, print the value of x.\n\n-----Sample Input-----\n7 17 120\n\n-----Sample Output-----\n119\n\n - If you choose Plan 1, the fee will be 7\u00d717=119 yen.\n - If you choose Plan 2, the fee will be 120 yen.\nThus, the minimum fee is 119 yen."} +{"id": "03606", "text": "Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.\nWhen he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.\nThen, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.\nNow, he goes through the following process:\n - Submit the code.\n - Wait until the code finishes execution on all the cases.\n - If the code fails to correctly solve some of the M cases, submit it again.\n - Repeat until the code correctly solve all the cases in one submission.\nLet the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).\n\n-----Constraints-----\n - All input values are integers.\n - 1 \\leq N \\leq 100\n - 1 \\leq M \\leq {\\rm min}(N, 5)\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\n\n-----Output-----\nPrint X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.\n\n-----Sample Input-----\n1 1\n\n-----Sample Output-----\n3800\n\nIn this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.\nThe code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.\nThus, the answer is 1900 \\times 1/2 + (2 \\times 1900) \\times 1/4 + (3 \\times 1900) \\times 1/8 + ... = 3800."} +{"id": "03607", "text": "You are given a string S consisting of lowercase English letters.\nFind the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.\nIf every lowercase English letter occurs in S, print None instead.\n\n-----Constraints-----\n - 1 \\leq |S| \\leq 10^5 (|S| is the length of string S.)\n - S consists of lowercase English letters.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the lexicographically smallest lowercase English letter that does not occur in S.\nIf every lowercase English letter occurs in S, print None instead.\n\n-----Sample Input-----\natcoderregularcontest\n\n-----Sample Output-----\nb\n\nThe string atcoderregularcontest contains a, but does not contain b."} +{"id": "03608", "text": "When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.\nYou are given N numbers X_1, X_2, ..., X_N, where N is an even number.\nFor each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i, that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.\nFind B_i for each i = 1, 2, ..., N.\n\n-----Constraints-----\n - 2 \\leq N \\leq 200000\n - N is even.\n - 1 \\leq X_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nX_1 X_2 ... X_N\n\n-----Output-----\nPrint N lines.\nThe i-th line should contain B_i.\n\n-----Sample Input-----\n4\n2 4 4 3\n\n-----Sample Output-----\n4\n3\n3\n4\n\n - Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n - Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n - Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n - Since the median of X_1, X_2, X_3 is 4, B_4 = 4."} +{"id": "03609", "text": "We have N sticks with negligible thickness.\nThe length of the i-th stick is A_i.\nSnuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides.\nFind the maximum possible area of the rectangle.\n\n-----Constraints-----\n - 4 \\leq N \\leq 10^5\n - 1 \\leq A_i \\leq 10^9\n - A_i is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the maximum possible area of the rectangle.\nIf no rectangle can be formed, print 0.\n\n-----Sample Input-----\n6\n3 1 2 4 2 1\n\n-----Sample Output-----\n2\n\n1 \\times 2 rectangle can be formed."} +{"id": "03610", "text": "Some number of chocolate pieces were prepared for a training camp.\nThe camp had N participants and lasted for D days.\nThe i-th participant (1 \\leq i \\leq N) ate one chocolate piece on each of the following days in the camp: the 1-st day, the (A_i + 1)-th day, the (2A_i + 1)-th day, and so on.\nAs a result, there were X chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.\nFind the number of chocolate pieces prepared at the beginning of the camp.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 1 \\leq D \\leq 100\n - 1 \\leq X \\leq 100\n - 1 \\leq A_i \\leq 100 (1 \\leq i \\leq N)\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nD X\nA_1\nA_2\n:\nA_N\n\n-----Output-----\nFind the number of chocolate pieces prepared at the beginning of the camp.\n\n-----Sample Input-----\n3\n7 1\n2\n5\n10\n\n-----Sample Output-----\n8\n\nThe camp has 3 participants and lasts for 7 days.\nEach participant eats chocolate pieces as follows:\n - The first participant eats one chocolate piece on Day 1, 3, 5 and 7, for a total of four.\n - The second participant eats one chocolate piece on Day 1 and 6, for a total of two.\n - The third participant eats one chocolate piece only on Day 1, for a total of one.\nSince the number of pieces remaining at the end of the camp is one, the number of pieces prepared at the beginning of the camp is 1 + 4 + 2 + 1 = 8."} +{"id": "03611", "text": "You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan).\nIn how many ways can we select some of these coins so that they are X yen in total?\nCoins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that coin are different.\n\n-----Constraints-----\n - 0 \\leq A, B, C \\leq 50\n - A + B + C \\geq 1\n - 50 \\leq X \\leq 20 000\n - A, B and C are integers.\n - X is a multiple of 50.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA\nB\nC\nX\n\n-----Output-----\nPrint the number of ways to select coins.\n\n-----Sample Input-----\n2\n2\n2\n100\n\n-----Sample Output-----\n2\n\nThere are two ways to satisfy the condition:\n - Select zero 500-yen coins, one 100-yen coin and zero 50-yen coins.\n - Select zero 500-yen coins, zero 100-yen coins and two 50-yen coins."} +{"id": "03612", "text": "You are given three integers A, B and C.\nDetermine whether C is not less than A and not greater than B.\n\n-----Constraints-----\n - -100\u2264A,B,C\u2264100 \n - A, B and C are all integers.\n\n-----Input-----\nInput is given from Standard Input in the following format: \nA B C\n\n-----Output-----\nIf the condition is satisfied, print Yes; otherwise, print No.\n\n-----Sample Input-----\n1 3 2\n\n-----Sample Output-----\nYes\n\nC=2 is not less than A=1 and not greater than B=3, and thus the output should be Yes."} +{"id": "03613", "text": "Akaki, a patissier, can make N kinds of doughnut using only a certain powder called \"Okashi no Moto\" (literally \"material of pastry\", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 \u2264 i \u2264 N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.\nNow, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:\n - For each of the N kinds of doughnuts, make at least one doughnut of that kind.\nAt most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.\n\n-----Constraints-----\n - 2 \u2264 N \u2264 100\n - 1 \u2264 m_i \u2264 1000\n - m_1 + m_2 + ... + m_N \u2264 X \u2264 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN X\nm_1\nm_2\n:\nm_N\n\n-----Output-----\nPrint the maximum number of doughnuts that can be made under the condition.\n\n-----Sample Input-----\n3 1000\n120\n100\n140\n\n-----Sample Output-----\n9\n\nShe has 1000 grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes 120 + 100 + 140 = 360 grams of Moto. From the 640 grams of Moto that remains here, she can make additional six Doughnuts 2. This is how she can made a total of nine doughnuts, which is the maximum."} +{"id": "03614", "text": "You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.\nHow many kinds of items did you get?\n\n-----Constraints-----\n - 1 \\leq N \\leq 2\\times 10^5\n - S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nS_1\n:\nS_N\n\n-----Output-----\nPrint the number of kinds of items you got.\n\n-----Sample Input-----\n3\napple\norange\napple\n\n-----Sample Output-----\n2\n\nYou got two kinds of items: apple and orange."} +{"id": "03615", "text": "In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows: \n - Rating 1-399 : gray\n - Rating 400-799 : brown\n - Rating 800-1199 : green\n - Rating 1200-1599 : cyan\n - Rating 1600-1999 : blue\n - Rating 2000-2399 : yellow\n - Rating 2400-2799 : orange\n - Rating 2800-3199 : red\nOther than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not.\n\nCurrently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i.\n\nFind the minimum and maximum possible numbers of different colors of the users. \n\n-----Constraints-----\n - 1 \u2264 N \u2264 100\n - 1 \u2264 a_i \u2264 4800\n - a_i is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1 a_2 ... a_N\n\n-----Output-----\nPrint the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. \n\n-----Sample Input-----\n4\n2100 2500 2700 2700\n\n-----Sample Output-----\n2 2\n\nThe user with rating 2100 is \"yellow\", and the others are \"orange\". There are two different colors."} +{"id": "03616", "text": "In \"Takahashi-ya\", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions).\nA customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen.\nWrite a program that, when S is given, prints the price of the corresponding bowl of ramen.\n\n-----Constraints-----\n - S is a string of length 3.\n - Each character in S is o or x.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nS\n\n-----Output-----\nPrint the price of the bowl of ramen corresponding to S.\n\n-----Sample Input-----\noxo\n\n-----Sample Output-----\n900\n\nThe price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \\times 2 = 900 yen."} +{"id": "03617", "text": "Two deer, AtCoDeer and TopCoDeer, are playing a game called Honest or Dishonest.\nIn this game, an honest player always tells the truth, and an dishonest player always tell lies.\nYou are given two characters a and b as the input. Each of them is either H or D, and carries the following information:\nIf a=H, AtCoDeer is honest; if a=D, AtCoDeer is dishonest.\nIf b=H, AtCoDeer is saying that TopCoDeer is honest; if b=D, AtCoDeer is saying that TopCoDeer is dishonest.\nGiven this information, determine whether TopCoDeer is honest.\n\n-----Constraints-----\n - a=H or a=D.\n - b=H or b=D.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\na b\n\n-----Output-----\nIf TopCoDeer is honest, print H. If he is dishonest, print D.\n\n-----Sample Input-----\nH H\n\n-----Sample Output-----\nH\n\nIn this input, AtCoDeer is honest. Hence, as he says, TopCoDeer is honest."} +{"id": "03618", "text": "Sitting in a station waiting room, Joisino is gazing at her train ticket.\nThe ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).\nIn the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.\nThe given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.\n\n-----Constraints-----\n - 0\u2264A,B,C,D\u22649\n - All input values are integers.\n - It is guaranteed that there is a solution.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nABCD\n\n-----Output-----\nPrint the formula you made, including the part =7.\nUse the signs + and -.\nDo not print a space between a digit and a sign.\n\n-----Sample Input-----\n1222\n\n-----Sample Output-----\n1+2+2+2=7\n\nThis is the only valid solution."} +{"id": "03619", "text": "A company has N members, who are assigned ID numbers 1, ..., N.\nEvery member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.\nWhen a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.\nYou are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.\n\n-----Constraints-----\n - 2 \\leq N \\leq 2 \\times 10^5\n - 1 \\leq A_i < i\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_2 ... A_N\n\n-----Output-----\nFor each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.\n\n-----Sample Input-----\n5\n1 1 2 2\n\n-----Sample Output-----\n2\n2\n0\n0\n0\n\nThe member numbered 1 has two immediate subordinates: the members numbered 2 and 3.\nThe member numbered 2 has two immediate subordinates: the members numbered 4 and 5.\nThe members numbered 3, 4, and 5 do not have immediate subordinates."} +{"id": "03620", "text": "There is a kangaroo at coordinate 0 on an infinite number line that runs from left to right, at time 0.\nDuring the period between time i-1 and time i, the kangaroo can either stay at his position, or perform a jump of length exactly i to the left or to the right.\nThat is, if his coordinate at time i-1 is x, he can be at coordinate x-i, x or x+i at time i.\nThe kangaroo's nest is at coordinate X, and he wants to travel to coordinate X as fast as possible.\nFind the earliest possible time to reach coordinate X.\n\n-----Constraints-----\n - X is an integer.\n - 1\u2264X\u226410^9\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nX\n\n-----Output-----\nPrint the earliest possible time for the kangaroo to reach coordinate X.\n\n-----Sample Input-----\n6\n\n-----Sample Output-----\n3\n\nThe kangaroo can reach his nest at time 3 by jumping to the right three times, which is the earliest possible time."} +{"id": "03621", "text": "We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.\nYou are given a 4-digit integer N. Answer the question: Is N good?\n\n-----Constraints-----\n - 1000 \u2264 N \u2264 9999\n - N is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nIf N is good, print Yes; otherwise, print No.\n\n-----Sample Input-----\n1118\n\n-----Sample Output-----\nYes\n\nN is good, since it contains three consecutive 1."} +{"id": "03622", "text": "The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.\nHe has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.\nTo build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.\nHow many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^5\n - 1 \\leq A_i \\leq 10^9(1\\leq i\\leq N)\n - 1 \\leq B_i \\leq 10^9(1\\leq i\\leq N)\n - 1 \\leq C_i \\leq 10^9(1\\leq i\\leq N)\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 ... A_N\nB_1 ... B_N\nC_1 ... C_N\n\n-----Output-----\nPrint the number of different altars that Ringo can build.\n\n-----Sample Input-----\n2\n1 5\n2 4\n3 6\n\n-----Sample Output-----\n3\n\nThe following three altars can be built:\n - Upper: 1-st part, Middle: 1-st part, Lower: 1-st part\n - Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part\n - Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part"} +{"id": "03623", "text": "In programming, hexadecimal notation is often used.\nIn hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.\nIn this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.\nWhen X and Y are seen as hexadecimal numbers, which is larger?\n\n-----Constraints-----\n - Each X and Y is A, B, C, D, E or F.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX Y\n\n-----Output-----\nIf X is smaller, print <; if Y is smaller, print >; if they are equal, print =.\n\n-----Sample Input-----\nA B\n\n-----Sample Output-----\n<\n\n10 < 11."} +{"id": "03624", "text": "You are given an H \u00d7 W grid.\n\nThe squares in the grid are described by H strings, S_1,...,S_H.\n\nThe j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \\leq i \\leq H,1 \\leq j \\leq W).\n. stands for an empty square, and # stands for a square containing a bomb. \nDolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.\n\n(Below, we will simply say \"adjacent\" for this meaning. For each square, there are at most eight adjacent squares.)\n\nHe decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square. \nPrint the strings after the process. \n\n-----Constraints-----\n - 1 \\leq H,W \\leq 50\n - S_i is a string of length W consisting of # and ..\n\n-----Input-----\nInput is given from Standard Input in the following format: \nH W\nS_1\n:\nS_H\n\n-----Output-----\nPrint the H strings after the process.\n\nThe i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \\leq i \\leq H, 1 \\leq j \\leq W). \n\n-----Sample Input-----\n3 5\n.....\n.#.#.\n.....\n\n-----Sample Output-----\n11211\n1#2#1\n11211\n\nFor example, let us observe the empty square at the first row from the top and first column from the left.\n\nThere is one bomb square adjacent to this empty square: the square at the second row and second column.\n\nThus, the . corresponding to this empty square is replaced with 1."} +{"id": "03625", "text": "We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.\nIt takes us A_i minutes to read the i-th book from the top on Desk A (1 \\leq i \\leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \\leq i \\leq M).\nConsider the following action:\n - Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.\nHow many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.\n\n-----Constraints-----\n - 1 \\leq N, M \\leq 200000\n - 1 \\leq K \\leq 10^9\n - 1 \\leq A_i, B_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M K\nA_1 A_2 \\ldots A_N\nB_1 B_2 \\ldots B_M\n\n-----Output-----\nPrint an integer representing the maximum number of books that can be read.\n\n-----Sample Input-----\n3 4 240\n60 90 120\n80 150 80 150\n\n-----Sample Output-----\n3\n\nIn this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.\nWe can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.\n - Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.\n - Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.\n - Read the topmost book on Desk A in 90 minutes, and remove that book from the desk."} +{"id": "03626", "text": "\"Pizza At\", a fast food chain, offers three kinds of pizza: \"A-pizza\", \"B-pizza\" and \"AB-pizza\". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.\nNakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.\n\n-----Constraints-----\n - 1 \u2264 A, B, C \u2264 5000\n - 1 \u2264 X, Y \u2264 10^5\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA B C X Y\n\n-----Output-----\nPrint the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.\n\n-----Sample Input-----\n1500 2000 1600 3 2\n\n-----Sample Output-----\n7900\n\nIt is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza."} +{"id": "03627", "text": "You are given an integer N.\nFind the number of the positive divisors of N!, modulo 10^9+7.\n\n-----Constraints-----\n - 1\u2264N\u226410^3\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the number of the positive divisors of N!, modulo 10^9+7.\n\n-----Sample Input-----\n3\n\n-----Sample Output-----\n4\n\nThere are four divisors of 3! =6: 1, 2, 3 and 6. Thus, the output should be 4."} +{"id": "03628", "text": "You are given a positive integer X.\nFind the largest perfect power that is at most X.\nHere, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.\n\n-----Constraints-----\n - 1 \u2264 X \u2264 1000\n - X is an integer.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nX\n\n-----Output-----\nPrint the largest perfect power that is at most X.\n\n-----Sample Input-----\n10\n\n-----Sample Output-----\n9\n\nThere are four perfect powers that are at most 10: 1, 4, 8 and 9.\nWe should print the largest among them, 9."} +{"id": "03629", "text": "An X-layered kagami mochi (X \u2265 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.\nLunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?\n\n-----Constraints-----\n - 1 \u2264 N \u2264 100\n - 1 \u2264 d_i \u2264 100\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nd_1\n:\nd_N\n\n-----Output-----\nPrint the maximum number of layers in a kagami mochi that can be made.\n\n-----Sample Input-----\n4\n10\n8\n8\n6\n\n-----Sample Output-----\n3\n\nIf we stack the mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, we have a 3-layered kagami mochi, which is the maximum number of layers."} +{"id": "03630", "text": "Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).\nFind the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.\n\n-----Constraints-----\n - 1 \u2266 |s| \u2266 200{,}000\n - s consists of uppercase English letters.\n - There exists a substring of s that starts with A and ends with Z.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\ns\n\n-----Output-----\nPrint the answer.\n\n-----Sample Input-----\nQWERTYASDFZXCV\n\n-----Sample Output-----\n5\n\nBy taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z."} +{"id": "03631", "text": "There are N positive integers written on a blackboard: A_1, ..., A_N.\nSnuke can perform the following operation when all integers on the blackboard are even:\n - Replace each integer X on the blackboard by X divided by 2.\nFind the maximum possible number of operations that Snuke can perform.\n\n-----Constraints-----\n - 1 \\leq N \\leq 200\n - 1 \\leq A_i \\leq 10^9\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the maximum possible number of operations that Snuke can perform.\n\n-----Sample Input-----\n3\n8 12 40\n\n-----Sample Output-----\n2\n\nInitially, [8, 12, 40] are written on the blackboard.\nSince all those integers are even, Snuke can perform the operation.\nAfter the operation is performed once, [4, 6, 20] are written on the blackboard.\nSince all those integers are again even, he can perform the operation.\nAfter the operation is performed twice, [2, 3, 10] are written on the blackboard.\nNow, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.\nThus, Snuke can perform the operation at most twice."} +{"id": "03632", "text": "Snuke loves working out. He is now exercising N times.\nBefore he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.\nFind Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 10^{5}\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the answer modulo 10^{9}+7.\n\n-----Sample Input-----\n3\n\n-----Sample Output-----\n6\n\n - After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.\n - After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.\n - After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6."} +{"id": "03633", "text": "There are N children in AtCoder Kindergarten. Mr. Evi will arrange the children in a line, then give 1 candy to the first child in the line, 2 candies to the second child, ..., N candies to the N-th child. How many candies will be necessary in total?\n\n-----Constraints-----\n - 1\u2266N\u2266100\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the necessary number of candies in total.\n\n-----Sample Input-----\n3\n\n-----Sample Output-----\n6\n\nThe answer is 1+2+3=6."} +{"id": "03634", "text": "We have N cards. A number a_i is written on the i-th card.\n\nAlice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.\n\nThe game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.\n\n-----Constraints-----\n - N is an integer between 1 and 100 (inclusive).\n - a_i \\ (1 \\leq i \\leq N) is an integer between 1 and 100 (inclusive).\n\n-----Input-----\nInput is given from Standard Input in the following format: \nN\na_1 a_2 a_3 ... a_N\n\n-----Output-----\nPrint Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.\n\n-----Sample Input-----\n2\n3 1\n\n-----Sample Output-----\n2\n\nFirst, Alice will take the card with 3. Then, Bob will take the card with 1.\nThe difference of their scores will be 3 - 1 = 2."} +{"id": "03635", "text": "Takahashi participated in a contest on AtCoder.\nThe contest had N problems.\nTakahashi made M submissions during the contest.\nThe i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).\nThe number of Takahashi's correct answers is the number of problems on which he received an AC once or more.\nThe number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.\nFind the numbers of Takahashi's correct answers and penalties.\n\n-----Constraints-----\n - N, M, and p_i are integers.\n - 1 \\leq N \\leq 10^5\n - 0 \\leq M \\leq 10^5\n - 1 \\leq p_i \\leq N\n - S_i is AC or WA.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN M\np_1 S_1\n:\np_M S_M\n\n-----Output-----\nPrint the number of Takahashi's correct answers and the number of Takahashi's penalties.\n\n-----Sample Input-----\n2 5\n1 WA\n1 AC\n2 WA\n2 AC\n2 WA\n\n-----Sample Output-----\n2 2\n\nIn his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.\nIn his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.\nThus, he has two correct answers and two penalties."} +{"id": "03636", "text": "Fennec is fighting with N monsters.\nThe health of the i-th monster is H_i.\nFennec can do the following two actions:\n - Attack: Fennec chooses one monster. That monster's health will decrease by 1.\n - Special Move: Fennec chooses one monster. That monster's health will become 0.\nThere is no way other than Attack and Special Move to decrease the monsters' health.\nFennec wins when all the monsters' healths become 0 or below.\nFind the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.\n\n-----Constraints-----\n - 1 \\leq N \\leq 2 \\times 10^5\n - 0 \\leq K \\leq 2 \\times 10^5\n - 1 \\leq H_i \\leq 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nH_1 ... H_N\n\n-----Output-----\nPrint the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.\n\n-----Sample Input-----\n3 1\n4 1 5\n\n-----Sample Output-----\n5\n\nBy using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks."} +{"id": "03637", "text": "There are N balls in the xy-plane. The coordinates of the i-th of them is (x_i, i).\nThus, we have one ball on each of the N lines y = 1, y = 2, ..., y = N.\nIn order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.\nThen, he placed the i-th type-A robot at coordinates (0, i), and the i-th type-B robot at coordinates (K, i).\nThus, now we have one type-A robot and one type-B robot on each of the N lines y = 1, y = 2, ..., y = N.\nWhen activated, each type of robot will operate as follows.\n - When a type-A robot is activated at coordinates (0, a), it will move to the position of the ball on the line y = a, collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.\n - When a type-B robot is activated at coordinates (K, b), it will move to the position of the ball on the line y = b, collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.\nSnuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.\n\n-----Constraints-----\n - 1 \\leq N \\leq 100\n - 1 \\leq K \\leq 100\n - 0 < x_i < K\n - All input values are integers.\n\n-----Inputs-----\nInput is given from Standard Input in the following format:\nN\nK\nx_1 x_2 ... x_N\n\n-----Outputs-----\nPrint the minimum possible total distance covered by robots.\n\n-----Sample Input-----\n1\n10\n2\n\n-----Sample Output-----\n4\n\nThere are just one ball, one type-A robot and one type-B robot.\nIf the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4.\nSimilarly, if the type-B robot is used, the total distance covered will be 16.\nThus, the total distance covered will be minimized when the type-A robot is used. The output should be 4."} +{"id": "03638", "text": "You planned a trip using trains and buses.\nThe train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket.\nSimilarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.\nFind the minimum total fare when the optimal choices are made for trains and buses.\n\n-----Constraints-----\n - 1 \\leq A \\leq 1 000\n - 1 \\leq B \\leq 1 000\n - 1 \\leq C \\leq 1 000\n - 1 \\leq D \\leq 1 000\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nA\nB\nC\nD\n\n-----Output-----\nPrint the minimum total fare.\n\n-----Sample Input-----\n600\n300\n220\n420\n\n-----Sample Output-----\n520\n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket.\nThus, the optimal choice for trains is to buy an unlimited ticket for 300 yen.\nOn the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.\nTherefore, the minimum total fare is 300 + 220 = 520 yen."} +{"id": "03639", "text": "There are N people, conveniently numbered 1 through N.\nThey were standing in a row yesterday, but now they are unsure of the order in which they were standing.\nHowever, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person.\nAccording to their reports, the difference above for person i is A_i.\nBased on these reports, find the number of the possible orders in which they were standing.\nSince it can be extremely large, print the answer modulo 10^9+7.\nNote that the reports may be incorrect and thus there may be no consistent order.\nIn such a case, print 0.\n\n-----Constraints-----\n - 1\u2266N\u226610^5\n - 0\u2266A_i\u2266N-1\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nN\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the number of the possible orders in which they were standing, modulo 10^9+7.\n\n-----Sample Input-----\n5\n2 4 4 0 2\n\n-----Sample Output-----\n4\n\nThere are four possible orders, as follows:\n - 2,1,4,5,3\n - 2,5,4,1,3\n - 3,1,4,5,2\n - 3,5,4,1,2"} +{"id": "03640", "text": "Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).\n\n-----Constraints-----\n - 1 \\leq N \\leq 10^4\n - 1 \\leq A \\leq B \\leq 36\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN A B\n\n-----Output-----\nPrint the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).\n\n-----Sample Input-----\n20 2 5\n\n-----Sample Output-----\n84\n\nAmong the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84."} +{"id": "03641", "text": "This contest, AtCoder Beginner Contest, is abbreviated as ABC.\nWhen we refer to a specific round of ABC, a three-digit number is appended after ABC. For example, ABC680 is the 680th round of ABC.\nWhat is the abbreviation for the N-th round of ABC? Write a program to output the answer.\n\n-----Constraints-----\n - 100 \u2264 N \u2264 999\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\n\n-----Output-----\nPrint the abbreviation for the N-th round of ABC.\n\n-----Sample Input-----\n100\n\n-----Sample Output-----\nABC100\n\nThe 100th round of ABC is ABC100."} +{"id": "03642", "text": "In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the \"year-month-day\" order, or the \"month-day\" order without the year.\n\nFor example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year. \nIn this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.\n\nHow many days from 2018-1-1 through 2018-a-b are Takahashi?\n\n-----Constraints-----\n - a is an integer between 1 and 12 (inclusive).\n - b is an integer between 1 and 31 (inclusive).\n - 2018-a-b is a valid date in Gregorian calendar.\n\n-----Input-----\nInput is given from Standard Input in the following format:\na b\n\n-----Output-----\nPrint the number of days from 2018-1-1 through 2018-a-b that are Takahashi.\n\n-----Sample Input-----\n5 5\n\n-----Sample Output-----\n5\n\nThere are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5."} +{"id": "03643", "text": "Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.\nThe exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.\nThese buttons are numbered 1 through N.\nWhen Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.\nWhen Button i is not lighten up, nothing will happen by pressing it.\nInitially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.\nDetermine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.\n\n-----Constraints-----\n - 2 \u2264 N \u2264 10^5\n - 1 \u2264 a_i \u2264 N\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\na_1\na_2\n:\na_N\n\n-----Output-----\nPrint -1 if it is impossible to lighten up Button 2.\nOtherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.\n\n-----Sample Input-----\n3\n3\n1\n2\n\n-----Sample Output-----\n2\n\nPress Button 1, then Button 3."} +{"id": "03644", "text": "You are playing the following game with Joisino.\n - Initially, you have a blank sheet of paper.\n - Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.\n - Then, you are asked a question: How many numbers are written on the sheet now?\nThe numbers announced by Joisino are given as A_1, ... ,A_N in the order she announces them. How many numbers will be written on the sheet at the end of the game?\n\n-----Constraints-----\n - 1\u2264N\u2264100000\n - 1\u2264A_i\u22641000000000(=10^9)\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1\n:\nA_N\n\n-----Output-----\nPrint how many numbers will be written on the sheet at the end of the game.\n\n-----Sample Input-----\n3\n6\n2\n6\n\n-----Sample Output-----\n1\n\nThe game proceeds as follows:\n - 6 is not written on the sheet, so write 6.\n - 2 is not written on the sheet, so write 2.\n - 6 is written on the sheet, so erase 6.\nThus, the sheet contains only 2 in the end. The answer is 1."} +{"id": "03645", "text": "Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.\nHe would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.\nFind the minimum number of balls that Takahashi needs to rewrite the integers on them.\n\n-----Constraints-----\n - 1 \\leq K \\leq N \\leq 200000\n - 1 \\leq A_i \\leq N\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN K\nA_1 A_2 ... A_N\n\n-----Output-----\nPrint the minimum number of balls that Takahashi needs to rewrite the integers on them.\n\n-----Sample Input-----\n5 2\n1 1 2 2 5\n\n-----Sample Output-----\n1\n\nFor example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.\nOn the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1."} +{"id": "03646", "text": "AtCoDeer the deer is going on a trip in a two-dimensional plane.\nIn his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.\nIf AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).\nNote that he cannot stay at his place.\nDetermine whether he can carry out his plan.\n\n-----Constraints-----\n - 1 \u2264 N \u2264 10^5\n - 0 \u2264 x_i \u2264 10^5\n - 0 \u2264 y_i \u2264 10^5\n - 1 \u2264 t_i \u2264 10^5\n - t_i < t_{i+1} (1 \u2264 i \u2264 N-1)\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nt_1 x_1 y_1\nt_2 x_2 y_2\n:\nt_N x_N y_N\n\n-----Output-----\nIf AtCoDeer can carry out his plan, print Yes; if he cannot, print No.\n\n-----Sample Input-----\n2\n3 1 2\n6 1 1\n\n-----Sample Output-----\nYes\n\nFor example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1)."} +{"id": "03647", "text": "You are given two positive integers a and b.\nLet x be the average of a and b.\nPrint x rounded up to the nearest integer.\n\n-----Constraints-----\n - a and b are integers.\n - 1 \\leq a, b \\leq 100\n\n-----Input-----\nInput is given from Standard Input in the following format:\na b\n\n-----Output-----\nPrint x rounded up to the nearest integer.\n\n-----Sample Input-----\n1 3\n\n-----Sample Output-----\n2\n\nThe average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2."} +{"id": "03648", "text": "You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.\n\nThe i-th edge (1 \\leq i \\leq M) connects Vertex a_i and Vertex b_i. \nAn edge whose removal disconnects the graph is called a bridge.\n\nFind the number of the edges that are bridges among the M edges. \n\n-----Notes-----\n - A self-loop is an edge i such that a_i=b_i (1 \\leq i \\leq M).\n - Double edges are a pair of edges i,j such that a_i=a_j and b_i=b_j (1 \\leq i y_{j} where j is the smallest integer such that x_{j} \\neq y_{j}.\n\n-----Constraints-----\n - 1 \u2264 |s| \u2264 5000\n - s consists of lowercase English letters.\n - 1 \u2264 K \u2264 5\n - s has at least K different substrings.\n\n-----Partial Score-----\n - 200 points will be awarded as a partial score for passing the test set satisfying |s| \u2264 50.\n\n-----Input-----\nInput is given from Standard Input in the following format:\ns\nK\n\n-----Output-----\nPrint the K-th lexicographically smallest substring of K.\n\n-----Sample Input-----\naba\n4\n\n-----Sample Output-----\nb\n\ns has five substrings: a, b, ab, ba and aba.\nAmong them, we should print the fourth smallest one, b.\nNote that we do not count a twice."} +{"id": "03654", "text": "There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.\nSnuke plotted N points into the rectangle. The coordinate of the i-th (1 \u2266 i \u2266 N) point was (x_i, y_i).\nThen, he created an integer sequence a of length N, and for each 1 \u2266 i \u2266 N, he painted some region within the rectangle black, as follows:\n - If a_i = 1, he painted the region satisfying x < x_i within the rectangle.\n - If a_i = 2, he painted the region satisfying x > x_i within the rectangle.\n - If a_i = 3, he painted the region satisfying y < y_i within the rectangle.\n - If a_i = 4, he painted the region satisfying y > y_i within the rectangle.\nFind the area of the white region within the rectangle after he finished painting.\n\n-----Constraints-----\n - 1 \u2266 W, H \u2266 100\n - 1 \u2266 N \u2266 100\n - 0 \u2266 x_i \u2266 W (1 \u2266 i \u2266 N)\n - 0 \u2266 y_i \u2266 H (1 \u2266 i \u2266 N)\n - W, H (21:32, added), x_i and y_i are integers.\n - a_i (1 \u2266 i \u2266 N) is 1, 2, 3 or 4.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nW H N\nx_1 y_1 a_1\nx_2 y_2 a_2\n:\nx_N y_N a_N\n\n-----Output-----\nPrint the area of the white region within the rectangle after Snuke finished painting.\n\n-----Sample Input-----\n5 4 2\n2 1 1\n3 3 4\n\n-----Sample Output-----\n9\n\nThe figure below shows the rectangle before Snuke starts painting.\nFirst, as (x_1, y_1) = (2, 1) and a_1 = 1, he paints the region satisfying x < 2 within the rectangle:\nThen, as (x_2, y_2) = (3, 3) and a_2 = 4, he paints the region satisfying y > 3 within the rectangle:\nNow, the area of the white region within the rectangle is 9."} +{"id": "03655", "text": "A railroad running from west to east in Atcoder Kingdom is now complete.\nThere are N stations on the railroad, numbered 1 through N from west to east.\nTomorrow, the opening ceremony of the railroad will take place.\nOn this railroad, for each integer i such that 1\u2264i\u2264N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.\nThe first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.\nHere, it is guaranteed that F_i divides S_i.\nThat is, for each Time t satisfying S_i\u2264t and t\uff05F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A\uff05B denotes A modulo B, and there will be no other trains.\nFor each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.\n\n-----Constraints-----\n - 1\u2264N\u2264500\n - 1\u2264C_i\u2264100\n - 1\u2264S_i\u226410^5\n - 1\u2264F_i\u226410\n - S_i\uff05F_i=0\n - All input values are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nC_1 S_1 F_1\n:\nC_{N-1} S_{N-1} F_{N-1}\n\n-----Output-----\nPrint N lines. Assuming that we are at Station i (1\u2264i\u2264N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.\n\n-----Sample Input-----\n3\n6 5 1\n1 10 1\n\n-----Sample Output-----\n12\n11\n0\n\nWe will travel from Station 1 as follows:\n - 5 seconds after the beginning: take the train to Station 2.\n - 11 seconds: arrive at Station 2.\n - 11 seconds: take the train to Station 3.\n - 12 seconds: arrive at Station 3.\nWe will travel from Station 2 as follows:\n - 10 seconds: take the train to Station 3.\n - 11 seconds: arrive at Station 3.\nNote that we should print 0 for Station 3."} +{"id": "03656", "text": "There is an image with a height of H pixels and a width of W pixels. Each of the pixels is represented by either . or *. The character representing the pixel at the i-th row from the top and the j-th column from the left, is denoted by C_{i,j}.\nExtend this image vertically so that its height is doubled. That is, print a image with a height of 2H pixels and a width of W pixels where the pixel at the i-th row and j-th column is equal to C_{(i+1)/2,j} (the result of division is rounded down).\n\n-----Constraints-----\n - 1\u2266H, W\u2266100\n - C_{i,j} is either . or *.\n\n-----Input-----\nThe input is given from Standard Input in the following format:\nH W\nC_{1,1}...C_{1,W}\n:\nC_{H,1}...C_{H,W}\n\n-----Output-----\nPrint the extended image.\n\n-----Sample Input-----\n2 2\n*.\n.*\n\n-----Sample Output-----\n*.\n*.\n.*\n.*\n"} +{"id": "03657", "text": "Given is a sequence of integers A_1, A_2, ..., A_N.\nIf its elements are pairwise distinct, print YES; otherwise, print NO.\n\n-----Constraints-----\n - 2 \u2264 N \u2264 200000\n - 1 \u2264 A_i \u2264 10^9\n - All values in input are integers.\n\n-----Input-----\nInput is given from Standard Input in the following format:\nN\nA_1 ... A_N\n\n-----Output-----\nIf the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.\n\n-----Sample Input-----\n5\n2 6 1 4 5\n\n-----Sample Output-----\nYES\n\nThe elements are pairwise distinct."} +{"id": "03658", "text": "There are $n$ people who want to participate in a boat competition. The weight of the $i$-th participant is $w_i$. Only teams consisting of two people can participate in this competition. As an organizer, you think that it's fair to allow only teams with the same total weight.\n\nSo, if there are $k$ teams $(a_1, b_1)$, $(a_2, b_2)$, $\\dots$, $(a_k, b_k)$, where $a_i$ is the weight of the first participant of the $i$-th team and $b_i$ is the weight of the second participant of the $i$-th team, then the condition $a_1 + b_1 = a_2 + b_2 = \\dots = a_k + b_k = s$, where $s$ is the total weight of each team, should be satisfied.\n\nYour task is to choose such $s$ that the number of teams people can create is the maximum possible. Note that each participant can be in no more than one team.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains one integer $n$ ($1 \\le n \\le 50$) \u2014 the number of participants. The second line of the test case contains $n$ integers $w_1, w_2, \\dots, w_n$ ($1 \\le w_i \\le n$), where $w_i$ is the weight of the $i$-th participant.\n\n\n-----Output-----\n\nFor each test case, print one integer $k$: the maximum number of teams people can compose with the total weight $s$, if you choose $s$ optimally.\n\n\n-----Example-----\nInput\n5\n5\n1 2 3 4 5\n8\n6 6 6 6 6 6 8 8\n8\n1 2 2 1 2 1 1 2\n3\n1 3 3\n6\n1 1 3 4 2 2\n\nOutput\n2\n3\n4\n1\n2\n\n\n\n-----Note-----\n\nIn the first test case of the example, we can reach the optimal answer for $s=6$. Then the first boat is used by participants $1$ and $5$ and the second boat is used by participants $2$ and $4$ (indices are the same as weights).\n\nIn the second test case of the example, we can reach the optimal answer for $s=12$. Then first $6$ participants can form $3$ pairs.\n\nIn the third test case of the example, we can reach the optimal answer for $s=3$. The answer is $4$ because we have $4$ participants with weight $1$ and $4$ participants with weight $2$.\n\nIn the fourth test case of the example, we can reach the optimal answer for $s=4$ or $s=6$.\n\nIn the fifth test case of the example, we can reach the optimal answer for $s=3$. Note that participant with weight $3$ can't use the boat because there is no suitable pair for him in the list."} +{"id": "03659", "text": "Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is $n$. \n\nThere is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains $2$ apartments, every other floor contains $x$ apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers $1$ and $2$, apartments on the second floor have numbers from $3$ to $(x + 2)$, apartments on the third floor have numbers from $(x + 3)$ to $(2 \\cdot x + 2)$, and so on.\n\nYour task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least $n$ apartments.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains two integers $n$ and $x$ ($1 \\le n, x \\le 1000$) \u2014 the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).\n\n\n-----Output-----\n\nFor each test case, print the answer: the number of floor on which Petya lives.\n\n\n-----Example-----\nInput\n4\n7 3\n1 5\n22 5\n987 13\n\nOutput\n3\n1\n5\n77\n\n\n\n-----Note-----\n\nConsider the first test case of the example: the first floor contains apartments with numbers $1$ and $2$, the second one contains apartments with numbers $3$, $4$ and $5$, the third one contains apartments with numbers $6$, $7$ and $8$. Therefore, Petya lives on the third floor.\n\nIn the second test case of the example, Petya lives in the apartment $1$ which is on the first floor."} +{"id": "03660", "text": "You want to perform the combo on your opponent in one popular fighting game. The combo is the string $s$ consisting of $n$ lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in $s$. I.e. if $s=$\"abca\" then you have to press 'a', then 'b', 'c' and 'a' again.\n\nYou know that you will spend $m$ wrong tries to perform the combo and during the $i$-th try you will make a mistake right after $p_i$-th button ($1 \\le p_i < n$) (i.e. you will press first $p_i$ buttons right and start performing the combo from the beginning). It is guaranteed that during the $m+1$-th try you press all buttons right and finally perform the combo.\n\nI.e. if $s=$\"abca\", $m=2$ and $p = [1, 3]$ then the sequence of pressed buttons will be 'a' (here you're making a mistake and start performing the combo from the beginning), 'a', 'b', 'c', (here you're making a mistake and start performing the combo from the beginning), 'a' (note that at this point you will not perform the combo because of the mistake), 'b', 'c', 'a'.\n\nYour task is to calculate for each button (letter) the number of times you'll press it.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases.\n\nThen $t$ test cases follow.\n\nThe first line of each test case contains two integers $n$ and $m$ ($2 \\le n \\le 2 \\cdot 10^5$, $1 \\le m \\le 2 \\cdot 10^5$) \u2014 the length of $s$ and the number of tries correspondingly.\n\nThe second line of each test case contains the string $s$ consisting of $n$ lowercase Latin letters.\n\nThe third line of each test case contains $m$ integers $p_1, p_2, \\dots, p_m$ ($1 \\le p_i < n$) \u2014 the number of characters pressed right during the $i$-th try.\n\nIt is guaranteed that the sum of $n$ and the sum of $m$ both does not exceed $2 \\cdot 10^5$ ($\\sum n \\le 2 \\cdot 10^5$, $\\sum m \\le 2 \\cdot 10^5$).\n\nIt is guaranteed that the answer for each letter does not exceed $2 \\cdot 10^9$.\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 $26$ integers: the number of times you press the button 'a', the number of times you press the button 'b', $\\dots$, the number of times you press the button 'z'.\n\n\n-----Example-----\nInput\n3\n4 2\nabca\n1 3\n10 5\ncodeforces\n2 8 3 2 9\n26 10\nqwertyuioplkjhgfdsazxcvbnm\n20 10 1 2 3 5 10 5 9 4\n\nOutput\n4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0 \n2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2 \n\n\n\n-----Note-----\n\nThe first test case is described in the problem statement. Wrong tries are \"a\", \"abc\" and the final try is \"abca\". The number of times you press 'a' is $4$, 'b' is $2$ and 'c' is $2$.\n\nIn the second test case, there are five wrong tries: \"co\", \"codeforc\", \"cod\", \"co\", \"codeforce\" and the final try is \"codeforces\". The number of times you press 'c' is $9$, 'd' is $4$, 'e' is $5$, 'f' is $3$, 'o' is $9$, 'r' is $3$ and 's' is $1$."} +{"id": "03661", "text": "Three friends are going to meet each other. Initially, the first friend stays at the position $x = a$, the second friend stays at the position $x = b$ and the third friend stays at the position $x = c$ on the coordinate axis $Ox$.\n\nIn one minute each friend independently from other friends can change the position $x$ by $1$ to the left or by $1$ to the right (i.e. set $x := x - 1$ or $x := x + 1$) or even don't change it.\n\nLet's introduce the total pairwise distance \u2014 the sum of distances between each pair of friends. Let $a'$, $b'$ and $c'$ be the final positions of the first, the second and the third friend, correspondingly. Then the total pairwise distance is $|a' - b'| + |a' - c'| + |b' - c'|$, where $|x|$ is the absolute value of $x$.\n\nFriends are interested in the minimum total pairwise distance they can reach if they will move optimally. Each friend will move no more than once. So, more formally, they want to know the minimum total pairwise distance they can reach after one minute.\n\nYou have to answer $q$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 1000$) \u2014 the number of test cases.\n\nThe next $q$ lines describe test cases. The $i$-th test case is given as three integers $a, b$ and $c$ ($1 \\le a, b, c \\le 10^9$) \u2014 initial positions of the first, second and third friend correspondingly. The positions of friends can be equal.\n\n\n-----Output-----\n\nFor each test case print the answer on it \u2014 the minimum total pairwise distance (the minimum sum of distances between each pair of friends) if friends change their positions optimally. Each friend will move no more than once. So, more formally, you have to find the minimum total pairwise distance they can reach after one minute.\n\n\n-----Example-----\nInput\n8\n3 3 4\n10 20 30\n5 5 5\n2 4 3\n1 1000000000 1000000000\n1 1000000000 999999999\n3 2 5\n3 2 6\n\nOutput\n0\n36\n0\n0\n1999999994\n1999999994\n2\n4"} +{"id": "03662", "text": "We call two numbers $x$ and $y$ similar if they have the same parity (the same remainder when divided by $2$), or if $|x-y|=1$. For example, in each of the pairs $(2, 6)$, $(4, 3)$, $(11, 7)$, the numbers are similar to each other, and in the pairs $(1, 4)$, $(3, 12)$, they are not.\n\nYou are given an array $a$ of $n$ ($n$ is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other.\n\nFor example, for the array $a = [11, 14, 16, 12]$, there is a partition into pairs $(11, 12)$ and $(14, 16)$. The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases. Then $t$ test cases follow.\n\nEach test case consists of two lines.\n\nThe first line contains an even positive integer $n$ ($2 \\le n \\le 50$)\u00a0\u2014 length of array $a$.\n\nThe second line contains $n$ positive integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 100$).\n\n\n-----Output-----\n\nFor each test case print: YES if the such a partition exists, NO otherwise. \n\nThe letters in the words YES and NO can be displayed in any case.\n\n\n-----Example-----\nInput\n7\n4\n11 14 16 12\n2\n1 8\n4\n1 1 1 1\n4\n1 2 5 6\n2\n12 13\n6\n1 6 3 10 5 8\n6\n1 12 3 10 5 8\n\nOutput\nYES\nNO\nYES\nYES\nYES\nYES\nNO\n\n\n\n-----Note-----\n\nThe first test case was explained in the statement.\n\nIn the second test case, the two given numbers are not similar.\n\nIn the third test case, any partition is suitable."} +{"id": "03663", "text": "You are a mayor of Berlyatov. There are $n$ districts and $m$ two-way roads between them. The $i$-th road connects districts $x_i$ and $y_i$. The cost of travelling along this road is $w_i$. There is some path between each pair of districts, so the city is connected.\n\nThere are $k$ delivery routes in Berlyatov. The $i$-th route is going from the district $a_i$ to the district $b_i$. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district $a_i$ to the district $b_i$ to deliver products.\n\nThe route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).\n\nYou can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with $0$).\n\nLet $d(x, y)$ be the cheapest cost of travel between districts $x$ and $y$.\n\nYour task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with $0$. In other words, you have to find the minimum possible value of $\\sum\\limits_{i = 1}^{k} d(a_i, b_i)$ after applying the operation described above optimally.\n\n\n-----Input-----\n\nThe first line of the input contains three integers $n$, $m$ and $k$ ($2 \\le n \\le 1000$; $n - 1 \\le m \\le min(1000, \\frac{n(n-1)}{2})$; $1 \\le k \\le 1000$) \u2014 the number of districts, the number of roads and the number of courier routes.\n\nThe next $m$ lines describe roads. The $i$-th road is given as three integers $x_i$, $y_i$ and $w_i$ ($1 \\le x_i, y_i \\le n$; $x_i \\ne y_i$; $1 \\le w_i \\le 1000$), where $x_i$ and $y_i$ are districts the $i$-th road connects and $w_i$ is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.\n\nThe next $k$ lines describe courier routes. The $i$-th route is given as two integers $a_i$ and $b_i$ ($1 \\le a_i, b_i \\le n$) \u2014 the districts of the $i$-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).\n\n\n-----Output-----\n\nPrint one integer \u2014 the minimum total courier routes cost you can achieve (i.e. the minimum value $\\sum\\limits_{i=1}^{k} d(a_i, b_i)$, where $d(x, y)$ is the cheapest cost of travel between districts $x$ and $y$) if you can make some (at most one) road cost zero.\n\n\n-----Examples-----\nInput\n6 5 2\n1 2 5\n2 3 7\n2 4 4\n4 5 2\n4 6 8\n1 6\n5 3\n\nOutput\n22\n\nInput\n5 5 4\n1 2 5\n2 3 4\n1 4 3\n4 3 7\n3 5 2\n1 5\n1 3\n3 3\n1 5\n\nOutput\n13\n\n\n\n-----Note-----\n\nThe picture corresponding to the first example:\n\n[Image]\n\nThere, you can choose either the road $(2, 4)$ or the road $(4, 6)$. Both options lead to the total cost $22$.\n\nThe picture corresponding to the second example:\n\n$A$\n\nThere, you can choose the road $(3, 4)$. This leads to the total cost $13$."} +{"id": "03664", "text": "The only difference between easy and hard versions is the maximum value of $n$.\n\nYou are given a positive integer number $n$. You really love good numbers so you want to find the smallest good number greater than or equal to $n$.\n\nThe positive integer is called good if it can be represented as a sum of distinct powers of $3$ (i.e. no duplicates of powers of $3$ are allowed).\n\nFor example: $30$ is a good number: $30 = 3^3 + 3^1$, $1$ is a good number: $1 = 3^0$, $12$ is a good number: $12 = 3^2 + 3^1$, but $2$ is not a good number: you can't represent it as a sum of distinct powers of $3$ ($2 = 3^0 + 3^0$), $19$ is not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representations $19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0$ are invalid), $20$ is also not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representation $20 = 3^2 + 3^2 + 3^0 + 3^0$ is invalid). \n\nNote, that there exist other representations of $19$ and $20$ as sums of powers of $3$ but none of them consists of distinct powers of $3$.\n\nFor the given positive integer $n$ find such smallest $m$ ($n \\le m$) that $m$ is a good number.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 500$) \u2014 the number of queries. Then $q$ queries follow.\n\nThe only line of the query contains one integer $n$ ($1 \\le n \\le 10^4$).\n\n\n-----Output-----\n\nFor each query, print such smallest integer $m$ (where $n \\le m$) that $m$ is a good number.\n\n\n-----Example-----\nInput\n7\n1\n2\n6\n13\n14\n3620\n10000\n\nOutput\n1\n3\n9\n13\n27\n6561\n19683"} +{"id": "03665", "text": "The only difference between easy and hard versions is constraints.\n\nThere are $n$ kids, each of them is reading a unique book. At the end of any day, the $i$-th kid will give his book to the $p_i$-th kid (in case of $i = p_i$ the kid will give his book to himself). It is guaranteed that all values of $p_i$ are distinct integers from $1$ to $n$ (i.e. $p$ is a permutation). The sequence $p$ doesn't change from day to day, it is fixed.\n\nFor example, if $n=6$ and $p=[4, 6, 1, 3, 5, 2]$ then at the end of the first day the book of the $1$-st kid will belong to the $4$-th kid, the $2$-nd kid will belong to the $6$-th kid and so on. At the end of the second day the book of the $1$-st kid will belong to the $3$-th kid, the $2$-nd kid will belong to the $2$-th kid and so on.\n\nYour task is to determine the number of the day the book of the $i$-th child is returned back to him for the first time for every $i$ from $1$ to $n$.\n\nConsider the following example: $p = [5, 1, 2, 4, 3]$. The book of the $1$-st kid will be passed to the following kids: after the $1$-st day it will belong to the $5$-th kid, after the $2$-nd day it will belong to the $3$-rd kid, after the $3$-rd day it will belong to the $2$-nd kid, after the $4$-th day it will belong to the $1$-st kid. \n\nSo after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 1000$) \u2014 the number of queries. Then $q$ queries follow.\n\nThe first line of the query contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of kids in the query. The second line of the query contains $n$ integers $p_1, p_2, \\dots, p_n$ ($1 \\le p_i \\le n$, all $p_i$ are distinct, i.e. $p$ is a permutation), where $p_i$ is the kid which will get the book of the $i$-th kid.\n\nIt is guaranteed that $\\sum n \\le 2 \\cdot 10^5$ (sum of $n$ over all queries does not exceed $2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each query, print the answer on it: $n$ integers $a_1, a_2, \\dots, a_n$, where $a_i$ is the number of the day the book of the $i$-th child is returned back to him for the first time in this query.\n\n\n-----Example-----\nInput\n6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3\n\nOutput\n1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"} +{"id": "03666", "text": "There are $n$ Christmas trees on an infinite number line. The $i$-th tree grows at the position $x_i$. All $x_i$ are guaranteed to be distinct.\n\nEach integer point can be either occupied by the Christmas tree, by the human or not occupied at all. Non-integer points cannot be occupied by anything.\n\nThere are $m$ people who want to celebrate Christmas. Let $y_1, y_2, \\dots, y_m$ be the positions of people (note that all values $x_1, x_2, \\dots, x_n, y_1, y_2, \\dots, y_m$ should be distinct and all $y_j$ should be integer). You want to find such an arrangement of people that the value $\\sum\\limits_{j=1}^{m}\\min\\limits_{i=1}^{n}|x_i - y_j|$ is the minimum possible (in other words, the sum of distances to the nearest Christmas tree for all people should be minimized).\n\nIn other words, let $d_j$ be the distance from the $j$-th human to the nearest Christmas tree ($d_j = \\min\\limits_{i=1}^{n} |y_j - x_i|$). Then you need to choose such positions $y_1, y_2, \\dots, y_m$ that $\\sum\\limits_{j=1}^{m} d_j$ is the minimum possible.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$) \u2014 the number of Christmas trees and the number of people.\n\nThe second line of the input contains $n$ integers $x_1, x_2, \\dots, x_n$ ($-10^9 \\le x_i \\le 10^9$), where $x_i$ is the position of the $i$-th Christmas tree. It is guaranteed that all $x_i$ are distinct.\n\n\n-----Output-----\n\nIn the first line print one integer $res$ \u2014 the minimum possible value of $\\sum\\limits_{j=1}^{m}\\min\\limits_{i=1}^{n}|x_i - y_j|$ (in other words, the sum of distances to the nearest Christmas tree for all people).\n\nIn the second line print $m$ integers $y_1, y_2, \\dots, y_m$ ($-2 \\cdot 10^9 \\le y_j \\le 2 \\cdot 10^9$), where $y_j$ is the position of the $j$-th human. All $y_j$ should be distinct and all values $x_1, x_2, \\dots, x_n, y_1, y_2, \\dots, y_m$ should be distinct.\n\nIf there are multiple answers, print any of them.\n\n\n-----Examples-----\nInput\n2 6\n1 5\n\nOutput\n8\n-1 2 6 4 0 3 \n\nInput\n3 5\n0 3 1\n\nOutput\n7\n5 -2 4 -1 2"} +{"id": "03667", "text": "There is a robot in a warehouse and $n$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $(0, 0)$. The $i$-th package is at the point $(x_i, y_i)$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $(0, 0)$ doesn't contain a package.\n\nThe robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $(x, y)$ to the point ($x + 1, y$) or to the point $(x, y + 1)$.\n\nAs we say above, the robot wants to collect all $n$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.\n\nThe string $s$ of length $n$ is lexicographically less than the string $t$ of length $n$ if there is some index $1 \\le j \\le n$ that for all $i$ from $1$ to $j-1$ $s_i = t_i$ and $s_j < t_j$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way.\n\n\n-----Input-----\n\nThe first line of the input contains an integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases. Then test cases follow.\n\nThe first line of a test case contains one integer $n$ ($1 \\le n \\le 1000$) \u2014 the number of packages.\n\nThe next $n$ lines contain descriptions of packages. The $i$-th package is given as two integers $x_i$ and $y_i$ ($0 \\le x_i, y_i \\le 1000$) \u2014 the $x$-coordinate of the package and the $y$-coordinate of the package.\n\nIt is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $(0, 0)$ doesn't contain a package.\n\nThe sum of all values $n$ over test cases in the test doesn't exceed $1000$.\n\n\n-----Output-----\n\nPrint the answer for each test case.\n\nIf it is impossible to collect all $n$ packages in some order starting from ($0,0$), print \"NO\" on the first line.\n\nOtherwise, print \"YES\" in the first line. Then print the shortest path \u2014 a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path.\n\nNote that in this problem \"YES\" and \"NO\" can be only uppercase words, i.e. \"Yes\", \"no\" and \"YeS\" are not acceptable.\n\n\n-----Example-----\nInput\n3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3\n\nOutput\nYES\nRUUURRRRUU\nNO\nYES\nRRRRUUU\n\n\n\n-----Note-----\n\nFor the first test case in the example the optimal path RUUURRRRUU is shown below: [Image]"} +{"id": "03668", "text": "You are given a positive integer $n$. In one move, you can increase $n$ by one (i.e. make $n := n + 1$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $n$ be less than or equal to $s$.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2 \\cdot 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains two integers $n$ and $s$ ($1 \\le n \\le 10^{18}$; $1 \\le s \\le 162$).\n\n\n-----Output-----\n\nFor each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $n$ be less than or equal to $s$.\n\n\n-----Example-----\nInput\n5\n2 1\n1 1\n500 4\n217871987498122 10\n100000000000000001 1\n\nOutput\n8\n0\n500\n2128012501878\n899999999999999999"} +{"id": "03669", "text": "There is a bookshelf which can fit $n$ books. The $i$-th position of bookshelf is $a_i = 1$ if there is a book on this position and $a_i = 0$ otherwise. It is guaranteed that there is at least one book on the bookshelf.\n\nIn one move, you can choose some contiguous segment $[l; r]$ consisting of books (i.e. for each $i$ from $l$ to $r$ the condition $a_i = 1$ holds) and: Shift it to the right by $1$: move the book at index $i$ to $i + 1$ for all $l \\le i \\le r$. This move can be done only if $r+1 \\le n$ and there is no book at the position $r+1$. Shift it to the left by $1$: move the book at index $i$ to $i-1$ for all $l \\le i \\le r$. This move can be done only if $l-1 \\ge 1$ and there is no book at the position $l-1$. \n\nYour task is to find the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without any gaps).\n\nFor example, for $a = [0, 0, 1, 0, 1]$ there is a gap between books ($a_4 = 0$ when $a_3 = 1$ and $a_5 = 1$), for $a = [1, 1, 0]$ there are no gaps between books and for $a = [0, 0,0]$ there are also no gaps between books.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 200$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains one integer $n$ ($1 \\le n \\le 50$) \u2014 the number of places on a bookshelf. The second line of the test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 1$), where $a_i$ is $1$ if there is a book at this position and $0$ otherwise. It is guaranteed that there is at least one book on the bookshelf.\n\n\n-----Output-----\n\nFor each test case, print one integer: the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without gaps).\n\n\n-----Example-----\nInput\n5\n7\n0 0 1 0 1 0 1\n3\n1 0 0\n5\n1 1 0 0 1\n6\n1 0 0 0 0 1\n5\n1 1 0 1 1\n\nOutput\n2\n0\n2\n4\n1\n\n\n\n-----Note-----\n\nIn the first test case of the example, you can shift the segment $[3; 3]$ to the right and the segment $[4; 5]$ to the right. After all moves, the books form the contiguous segment $[5; 7]$. So the answer is $2$.\n\nIn the second test case of the example, you have nothing to do, all the books on the bookshelf form the contiguous segment already.\n\nIn the third test case of the example, you can shift the segment $[5; 5]$ to the left and then the segment $[4; 4]$ to the left again. After all moves, the books form the contiguous segment $[1; 3]$. So the answer is $2$.\n\nIn the fourth test case of the example, you can shift the segment $[1; 1]$ to the right, the segment $[2; 2]$ to the right, the segment $[6; 6]$ to the left and then the segment $[5; 5]$ to the left. After all moves, the books form the contiguous segment $[3; 4]$. So the answer is $4$.\n\nIn the fifth test case of the example, you can shift the segment $[1; 2]$ to the right. After all moves, the books form the contiguous segment $[2; 5]$. So the answer is $1$."} +{"id": "03670", "text": "You are given two integers $n$ and $k$.\n\nYour task is to construct such a string $s$ of length $n$ that for each $i$ from $1$ to $k$ there is at least one $i$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.\n\nYou have to answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of queries.\n\nThe next $t$ lines are contain queries, one per line. The $i$-th line contains two integers $n_i$ and $k_i$ ($1 \\le n_i \\le 100, 1 \\le k_i \\le min(n_i, 26)$) \u2014 the length of the string in the $i$-th query and the number of characters in the $i$-th query.\n\n\n-----Output-----\n\nPrint $t$ lines. In the $i$-th line print the answer to the $i$-th query: any string $s_i$ satisfying the conditions in the problem statement with constraints from the $i$-th query.\n\n\n-----Example-----\nInput\n3\n7 3\n4 4\n6 2\n\nOutput\ncbcacab\nabcd\nbaabab\n\n\n\n-----Note-----\n\nIn the first example query the maximum possible minimal frequency is $2$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: \"cbcabba\", \"ccbbaaa\" (any permutation of given answers is also correct).\n\nIn the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $1$).\n\nIn the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $3$)."} +{"id": "03671", "text": "There are $n$ candies in a row, they are numbered from left to right from $1$ to $n$. The size of the $i$-th candy is $a_i$.\n\nAlice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob \u2014 from right to left. The game ends if all the candies are eaten.\n\nThe process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob \u2014 from the right).\n\nAlice makes the first move. During the first move, she will eat $1$ candy (its size is $a_1$). Then, each successive move the players alternate \u2014 that is, Bob makes the second move, then Alice, then again Bob and so on.\n\nOn each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.\n\nFor example, if $n=11$ and $a=[3,1,4,1,5,9,2,6,5,3,5]$, then: move 1: Alice eats one candy of size $3$ and the sequence of candies becomes $[1,4,1,5,9,2,6,5,3,5]$. move 2: Alice ate $3$ on the previous move, which means Bob must eat $4$ or more. Bob eats one candy of size $5$ and the sequence of candies becomes $[1,4,1,5,9,2,6,5,3]$. move 3: Bob ate $5$ on the previous move, which means Alice must eat $6$ or more. Alice eats three candies with the total size of $1+4+1=6$ and the sequence of candies becomes $[5,9,2,6,5,3]$. move 4: Alice ate $6$ on the previous move, which means Bob must eat $7$ or more. Bob eats two candies with the total size of $3+5=8$ and the sequence of candies becomes $[5,9,2,6]$. move 5: Bob ate $8$ on the previous move, which means Alice must eat $9$ or more. Alice eats two candies with the total size of $5+9=14$ and the sequence of candies becomes $[2,6]$. move 6 (the last): Alice ate $14$ on the previous move, which means Bob must eat $15$ or more. It is impossible, so Bob eats the two remaining candies and the game ends. \n\nPrint the number of moves in the game and two numbers: $a$ \u2014 the total size of all sweets eaten by Alice during the game; $b$ \u2014 the total size of all sweets eaten by Bob during the game. \n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\le t \\le 5000$) \u2014 the number of test cases in the input. The following are descriptions of the $t$ test cases.\n\nEach test case consists of two lines. The first line contains an integer $n$ ($1 \\le n \\le 1000$) \u2014 the number of candies. The second line contains a sequence of integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 1000$) \u2014 the sizes of candies in the order they are arranged from left to right.\n\nIt is guaranteed that the sum of the values of $n$ for all sets of input data in a test does not exceed $2\\cdot10^5$.\n\n\n-----Output-----\n\nFor each set of input data print three integers \u2014 the number of moves in the game and the required values $a$ and $b$.\n\n\n-----Example-----\nInput\n7\n11\n3 1 4 1 5 9 2 6 5 3 5\n1\n1000\n3\n1 1 1\n13\n1 2 3 4 5 6 7 8 9 10 11 12 13\n2\n2 1\n6\n1 1 1 1 1 1\n7\n1 1 1 1 1 1 1\n\nOutput\n6 23 21\n1 1000 0\n2 1 2\n6 45 46\n2 2 1\n3 4 2\n4 4 3"} +{"id": "03672", "text": "You are given two arrays $a$ and $b$ both consisting of $n$ positive (greater than zero) integers. You are also given an integer $k$.\n\nIn one move, you can choose two indices $i$ and $j$ ($1 \\le i, j \\le n$) and swap $a_i$ and $b_j$ (i.e. $a_i$ becomes $b_j$ and vice versa). Note that $i$ and $j$ can be equal or different (in particular, swap $a_2$ with $b_2$ or swap $a_3$ and $b_9$ both are acceptable moves).\n\nYour task is to find the maximum possible sum you can obtain in the array $a$ if you can do no more than (i.e. at most) $k$ such moves (swaps).\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 200$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains two integers $n$ and $k$ ($1 \\le n \\le 30; 0 \\le k \\le n$) \u2014 the number of elements in $a$ and $b$ and the maximum number of moves you can do. The second line of the test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 30$), where $a_i$ is the $i$-th element of $a$. The third line of the test case contains $n$ integers $b_1, b_2, \\dots, b_n$ ($1 \\le b_i \\le 30$), where $b_i$ is the $i$-th element of $b$.\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 the maximum possible sum you can obtain in the array $a$ if you can do no more than (i.e. at most) $k$ swaps.\n\n\n-----Example-----\nInput\n5\n2 1\n1 2\n3 4\n5 5\n5 5 6 6 5\n1 2 5 4 3\n5 3\n1 2 3 4 5\n10 9 10 10 9\n4 0\n2 2 4 3\n2 4 2 3\n4 4\n1 2 2 1\n4 4 5 4\n\nOutput\n6\n27\n39\n11\n17\n\n\n\n-----Note-----\n\nIn the first test case of the example, you can swap $a_1 = 1$ and $b_2 = 4$, so $a=[4, 2]$ and $b=[3, 1]$.\n\nIn the second test case of the example, you don't need to swap anything.\n\nIn the third test case of the example, you can swap $a_1 = 1$ and $b_1 = 10$, $a_3 = 3$ and $b_3 = 10$ and $a_2 = 2$ and $b_4 = 10$, so $a=[10, 10, 10, 4, 5]$ and $b=[1, 9, 3, 2, 9]$.\n\nIn the fourth test case of the example, you cannot swap anything.\n\nIn the fifth test case of the example, you can swap arrays $a$ and $b$, so $a=[4, 4, 5, 4]$ and $b=[1, 2, 2, 1]$."} +{"id": "03673", "text": "You are planning to buy an apartment in a $n$-floor building. The floors are numbered from $1$ to $n$ from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.\n\nLet: $a_i$ for all $i$ from $1$ to $n-1$ be the time required to go from the $i$-th floor to the $(i+1)$-th one (and from the $(i+1)$-th to the $i$-th as well) using the stairs; $b_i$ for all $i$ from $1$ to $n-1$ be the time required to go from the $i$-th floor to the $(i+1)$-th one (and from the $(i+1)$-th to the $i$-th as well) using the elevator, also there is a value $c$ \u2014 time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). \n\nIn one move, you can go from the floor you are staying at $x$ to any floor $y$ ($x \\ne y$) in two different ways: If you are using the stairs, just sum up the corresponding values of $a_i$. Formally, it will take $\\sum\\limits_{i=min(x, y)}^{max(x, y) - 1} a_i$ time units. If you are using the elevator, just sum up $c$ and the corresponding values of $b_i$. Formally, it will take $c + \\sum\\limits_{i=min(x, y)}^{max(x, y) - 1} b_i$ time units. \n\nYou can perform as many moves as you want (possibly zero).\n\nSo your task is for each $i$ to determine the minimum total time it takes to reach the $i$-th floor from the $1$-st (bottom) floor.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $n$ and $c$ ($2 \\le n \\le 2 \\cdot 10^5, 1 \\le c \\le 1000$) \u2014 the number of floors in the building and the time overhead for the elevator rides.\n\nThe second line of the input contains $n - 1$ integers $a_1, a_2, \\dots, a_{n-1}$ ($1 \\le a_i \\le 1000$), where $a_i$ is the time required to go from the $i$-th floor to the $(i+1)$-th one (and from the $(i+1)$-th to the $i$-th as well) using the stairs.\n\nThe third line of the input contains $n - 1$ integers $b_1, b_2, \\dots, b_{n-1}$ ($1 \\le b_i \\le 1000$), where $b_i$ is the time required to go from the $i$-th floor to the $(i+1)$-th one (and from the $(i+1)$-th to the $i$-th as well) using the elevator.\n\n\n-----Output-----\n\nPrint $n$ integers $t_1, t_2, \\dots, t_n$, where $t_i$ is the minimum total time to reach the $i$-th floor from the first floor if you can perform as many moves as you want.\n\n\n-----Examples-----\nInput\n10 2\n7 6 18 6 16 18 1 17 17\n6 9 3 10 9 1 10 1 5\n\nOutput\n0 7 13 18 24 35 36 37 40 45 \n\nInput\n10 1\n3 2 3 1 3 3 1 4 1\n1 2 3 4 4 1 2 1 3\n\nOutput\n0 2 4 7 8 11 13 14 16 17"} +{"id": "03674", "text": "For the given integer $n$ ($n > 2$) let's write down all the strings of length $n$ which contain $n-2$ letters 'a' and two letters 'b' in lexicographical (alphabetical) order.\n\nRecall that the string $s$ of length $n$ is lexicographically less than string $t$ of length $n$, if there exists such $i$ ($1 \\le i \\le n$), that $s_i < t_i$, and for any $j$ ($1 \\le j < i$) $s_j = t_j$. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.\n\nFor example, if $n=5$ the strings are (the order does matter): aaabb aabab aabba abaab ababa abbaa baaab baaba babaa bbaaa \n\nIt is easy to show that such a list of strings will contain exactly $\\frac{n \\cdot (n-1)}{2}$ strings.\n\nYou are given $n$ ($n > 2$) and $k$ ($1 \\le k \\le \\frac{n \\cdot (n-1)}{2}$). Print the $k$-th string from the list.\n\n\n-----Input-----\n\nThe input contains one or more test cases.\n\nThe first line contains one integer $t$ ($1 \\le t \\le 10^4$) \u2014 the number of test cases in the test. Then $t$ test cases follow.\n\nEach test case is written on the the separate line containing two integers $n$ and $k$ ($3 \\le n \\le 10^5, 1 \\le k \\le \\min(2\\cdot10^9, \\frac{n \\cdot (n-1)}{2})$.\n\nThe sum of values $n$ over all test cases in the test doesn't exceed $10^5$.\n\n\n-----Output-----\n\nFor each test case print the $k$-th string from the list of all described above strings of length $n$. Strings in the list are sorted lexicographically (alphabetically).\n\n\n-----Example-----\nInput\n7\n5 1\n5 2\n5 8\n5 10\n3 1\n3 2\n20 100\n\nOutput\naaabb\naabab\nbaaba\nbbaaa\nabb\nbab\naaaaabaaaaabaaaaaaaa"} +{"id": "03675", "text": "There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform.\n\nNote that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate.\n\nWhen you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost.\n\nYour task is to find the maximum number of points you can save if you place both platforms optimally.\n\nYou have to answer $t$ independent test cases.\n\nFor better understanding, please read the Note section below to see a picture for the first test case.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2 \\cdot 10^4$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe first line of the test case contains two integers $n$ and $k$ ($1 \\le n \\le 2 \\cdot 10^5$; $1 \\le k \\le 10^9$) \u2014 the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \\dots, x_n$ ($1 \\le x_i \\le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \\dots, y_n$ ($1 \\le y_i \\le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \\le i < j \\le n$ such that $x_i = x_j$ and $y_i = y_j$).\n\nIt is guaranteed that the sum of $n$ does not exceed $2 \\cdot 10^5$ ($\\sum n \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each test case, print the answer: the maximum number of points you can save if you place both platforms optimally.\n\n\n-----Example-----\nInput\n4\n7 1\n1 5 2 3 1 5 4\n1 3 6 7 2 5 4\n1 1\n1000000000\n1000000000\n5 10\n10 7 5 15 8\n20 199 192 219 1904\n10 10\n15 19 8 17 20 10 9 2 10 19\n12 13 6 17 1 14 7 9 19 3\n\nOutput\n6\n1\n5\n10\n\n\n\n-----Note-----\n\nThe picture corresponding to the first test case of the example:\n\n[Image]\n\nBlue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform."} +{"id": "03676", "text": "Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array. \n\nYou are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$.\n\nYou are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query.\n\nIn one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query.\n\nYou have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).\n\nYou have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$).\n\nOperations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \\dots, y_j]$.\n\n\n-----Input-----\n\nThe first line of the input contains two integers $q, x$ ($1 \\le q, x \\le 4 \\cdot 10^5$) \u2014 the number of queries and the value of $x$.\n\nThe next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \\le y_j \\le 10^9$) and means that you have to append one element $y_j$ to the array.\n\n\n-----Output-----\n\nPrint the answer to the initial problem after each query \u2014 for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.\n\n\n-----Examples-----\nInput\n7 3\n0\n1\n2\n2\n0\n0\n10\n\nOutput\n1\n2\n3\n3\n4\n4\n7\n\nInput\n4 3\n1\n2\n1\n2\n\nOutput\n0\n0\n0\n0\n\n\n\n-----Note-----\n\nIn the first example: After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: $a[3] := a[3] + 3 = 2 + 3 = 5$, $a[4] := a[4] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 0 + 3 = 3$, $a[5] := a[5] + 3 = 3 + 3 = 6$, $a[6] := a[6] - 3 = 10 - 3 = 7$, $a[6] := a[6] - 3 = 7 - 3 = 4$. The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$."} +{"id": "03677", "text": "We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:\n\n The array consists of $n$ distinct positive (greater than $0$) integers. The array contains two elements $x$ and $y$ (these elements are known for you) such that $x < y$. If you sort the array in increasing order (such that $a_1 < a_2 < \\ldots < a_n$), differences between all adjacent (consecutive) elements are equal (i.e. $a_2 - a_1 = a_3 - a_2 = \\ldots = a_n - a_{n-1})$. \n\nIt can be proven that such an array always exists under the constraints given below.\n\nAmong all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $\\max(a_1, a_2, \\dots, a_n)$.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases. Then $t$ test cases follow.\n\nThe only line of the test case contains three integers $n$, $x$ and $y$ ($2 \\le n \\le 50$; $1 \\le x < y \\le 50$) \u2014 the length of the array and two elements that are present in the array, respectively.\n\n\n-----Output-----\n\nFor each test case, print the answer: $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the $i$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter).\n\nIt can be proven that such an array always exists under the given constraints.\n\n\n-----Example-----\nInput\n5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22\n\nOutput\n1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"} +{"id": "03678", "text": "You are developing a new feature for the website which sells airline tickets: being able to sort tickets by price! You have already extracted the tickets' prices, so there's just the last step to be done...\n\nYou are given an array of integers. Sort it in non-descending order.\n\n\n-----Input-----\n\nThe input consists of a single line of space-separated integers. The first number is n (1 \u2264 n \u2264 10) \u2014 the size of the array. The following n numbers are the elements of the array (1 \u2264 a_{i} \u2264 100).\n\n\n-----Output-----\n\nOutput space-separated elements of the sorted array.\n\n\n-----Example-----\nInput\n3 3 1 2\n\nOutput\n1 2 3 \n\n\n\n-----Note-----\n\nRemember, this is a very important feature, and you have to make sure the customers appreciate it!"} +{"id": "03679", "text": "You are given an array $a$ consisting of $n$ integers.\n\nIn one move, you can choose two indices $1 \\le i, j \\le n$ such that $i \\ne j$ and set $a_i := a_j$. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose $i$ and $j$ and replace $a_i$ with $a_j$).\n\nYour task is to say if it is possible to obtain an array with an odd (not divisible by $2$) sum of elements.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2000$) \u2014 the number of test cases.\n\nThe next $2t$ lines describe test cases. The first line of the test case contains one integer $n$ ($1 \\le n \\le 2000$) \u2014 the number of elements in $a$. The second line of the test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2000$), where $a_i$ is the $i$-th element of $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\\sum n \\le 2000$).\n\n\n-----Output-----\n\nFor each test case, print the answer on it \u2014 \"YES\" (without quotes) if it is possible to obtain the array with an odd sum of elements, and \"NO\" otherwise.\n\n\n-----Example-----\nInput\n5\n2\n2 3\n4\n2 2 8 8\n3\n3 3 3\n4\n5 5 5 5\n4\n1 1 1 1\n\nOutput\nYES\nNO\nYES\nNO\nNO"} +{"id": "03680", "text": "A permutation of length $n$ is an array $p=[p_1,p_2,\\dots,p_n]$, which contains every integer from $1$ to $n$ (inclusive) and, moreover, each number appears exactly once. For example, $p=[3,1,4,2,5]$ is a permutation of length $5$.\n\nFor a given number $n$ ($n \\ge 2$), find a permutation $p$ in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between $2$ and $4$, inclusive. Formally, find such permutation $p$ that $2 \\le |p_i - p_{i+1}| \\le 4$ for each $i$ ($1 \\le i < n$).\n\nPrint any such permutation for the given integer $n$ or determine that it does not exist.\n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\le t \\le 100$) \u2014 the number of test cases in the input. Then $t$ test cases follow.\n\nEach test case is described by a single line containing an integer $n$ ($2 \\le n \\le 1000$).\n\n\n-----Output-----\n\nPrint $t$ lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.\n\n\n-----Example-----\nInput\n6\n10\n2\n4\n6\n7\n13\n\nOutput\n9 6 10 8 4 7 3 1 5 2 \n-1\n3 1 4 2 \n5 3 6 2 4 1 \n5 1 3 6 2 4 7 \n13 9 7 11 8 4 1 3 5 2 6 10 12"} +{"id": "03681", "text": "You are given an array $a[0 \\ldots n-1]$ of length $n$ which consists of non-negative integers. Note that array indices start from zero.\n\nAn array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all $i$ ($0 \\le i \\le n - 1$) the equality $i \\bmod 2 = a[i] \\bmod 2$ holds, where $x \\bmod 2$ is the remainder of dividing $x$ by 2.\n\nFor example, the arrays [$0, 5, 2, 1$] and [$0, 17, 0, 3$] are good, and the array [$2, 4, 6, 7$] is bad, because for $i=1$, the parities of $i$ and $a[i]$ are different: $i \\bmod 2 = 1 \\bmod 2 = 1$, but $a[i] \\bmod 2 = 4 \\bmod 2 = 0$.\n\nIn one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).\n\nFind the minimum number of moves in which you can make the array $a$ good, or say that this is not possible.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 1000$)\u00a0\u2014 the number of test cases in the test. Then $t$ test cases follow.\n\nEach test case starts with a line containing an integer $n$ ($1 \\le n \\le 40$)\u00a0\u2014 the length of the array $a$.\n\nThe next line contains $n$ integers $a_0, a_1, \\ldots, a_{n-1}$ ($0 \\le a_i \\le 1000$)\u00a0\u2014 the initial array.\n\n\n-----Output-----\n\nFor each test case, output a single integer\u00a0\u2014 the minimum number of moves to make the given array $a$ good, or -1 if this is not possible.\n\n\n-----Example-----\nInput\n4\n4\n3 2 7 6\n3\n3 2 6\n1\n7\n7\n4 9 2 1 18 3 0\n\nOutput\n2\n1\n-1\n0\n\n\n\n-----Note-----\n\nIn the first test case, in the first move, you can swap the elements with indices $0$ and $1$, and in the second move, you can swap the elements with indices $2$ and $3$.\n\nIn the second test case, in the first move, you need to swap the elements with indices $0$ and $1$.\n\nIn the third test case, you cannot make the array good."} +{"id": "03682", "text": "You are given a tree consisting of $n$ vertices. A tree is a connected undirected graph with $n-1$ edges. Each vertex $v$ of this tree has a color assigned to it ($a_v = 1$ if the vertex $v$ is white and $0$ if the vertex $v$ is black).\n\nYou have to solve the following problem for each vertex $v$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $v$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $cnt_w$ white vertices and $cnt_b$ black vertices, you have to maximize $cnt_w - cnt_b$.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $n$ ($2 \\le n \\le 2 \\cdot 10^5$) \u2014 the number of vertices in the tree.\n\nThe second line of the input contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 1$), where $a_i$ is the color of the $i$-th vertex.\n\nEach of the next $n-1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$, the labels of vertices it connects $(1 \\le u_i, v_i \\le n, u_i \\ne v_i$).\n\nIt is guaranteed that the given edges form a tree.\n\n\n-----Output-----\n\nPrint $n$ integers $res_1, res_2, \\dots, res_n$, where $res_i$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $i$.\n\n\n-----Examples-----\nInput\n9\n0 1 1 1 0 0 0 0 1\n1 2\n1 3\n3 4\n3 5\n2 6\n4 7\n6 8\n5 9\n\nOutput\n2 2 2 2 2 1 1 0 2 \n\nInput\n4\n0 0 1 0\n1 2\n1 3\n1 4\n\nOutput\n0 -1 1 -1 \n\n\n\n-----Note-----\n\nThe first example is shown below:\n\n[Image]\n\nThe black vertices have bold borders.\n\nIn the second example, the best subtree for vertices $2, 3$ and $4$ are vertices $2, 3$ and $4$ correspondingly. And the best subtree for the vertex $1$ is the subtree consisting of vertices $1$ and $3$."} +{"id": "03683", "text": "You are given an integer $n$. In one move, you can either multiply $n$ by two or divide $n$ by $6$ (if it is divisible by $6$ without the remainder).\n\nYour task is to find the minimum number of moves needed to obtain $1$ from $n$ or determine if it's impossible to do that.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 2 \\cdot 10^4$) \u2014 the number of test cases. Then $t$ test cases follow. \n\nThe only line of the test case contains one integer $n$ ($1 \\le n \\le 10^9$).\n\n\n-----Output-----\n\nFor each test case, print the answer \u2014 the minimum number of moves needed to obtain $1$ from $n$ if it's possible to do that or -1 if it's impossible to obtain $1$ from $n$.\n\n\n-----Example-----\nInput\n7\n1\n2\n3\n12\n12345\n15116544\n387420489\n\nOutput\n0\n-1\n2\n-1\n-1\n12\n36\n\n\n\n-----Note-----\n\nConsider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer $15116544$:\n\n Divide by $6$ and get $2519424$; divide by $6$ and get $419904$; divide by $6$ and get $69984$; divide by $6$ and get $11664$; multiply by $2$ and get $23328$; divide by $6$ and get $3888$; divide by $6$ and get $648$; divide by $6$ and get $108$; multiply by $2$ and get $216$; divide by $6$ and get $36$; divide by $6$ and get $6$; divide by $6$ and get $1$."} +{"id": "03684", "text": "The only difference between easy and hard versions is the size of the input.\n\nYou are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'.\n\nYou are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string \"RGBRGBRGB ...\".\n\nA string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings \"GBRG\", \"B\", \"BR\" are substrings of the infinite string \"RGBRGBRGB ...\" while \"GR\", \"RGR\" and \"GGG\" are not.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 2000$)\u00a0\u2014 the number of queries. Then $q$ queries follow.\n\nThe first line of the query contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2000$)\u00a0\u2014 the length of the string $s$ and the length of the substring.\n\nThe second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'.\n\nIt is guaranteed that the sum of $n$ over all queries does not exceed $2000$ ($\\sum n \\le 2000$).\n\n\n-----Output-----\n\nFor each query print one integer\u00a0\u2014 the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string \"RGBRGBRGB ...\".\n\n\n-----Example-----\nInput\n3\n5 2\nBGGGG\n5 3\nRBRGR\n5 5\nBBBRR\n\nOutput\n1\n0\n3\n\n\n\n-----Note-----\n\nIn the first example, you can change the first character to 'R' and obtain the substring \"RG\", or change the second character to 'R' and obtain \"BR\", or change the third, fourth or fifth character to 'B' and obtain \"GB\".\n\nIn the second example, the substring is \"BRG\"."} +{"id": "03685", "text": "You are given an array $a$ consisting of $n$ integers $a_1, a_2, \\dots , a_n$.\n\nIn one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array $[2, 1, 4]$ you can obtain the following arrays: $[3, 4]$, $[1, 6]$ and $[2, 5]$.\n\nYour task is to find the maximum possible number of elements divisible by $3$ that are in the array after performing this operation an arbitrary (possibly, zero) number of times.\n\nYou have to answer $t$ independent queries.\n\n\n-----Input-----\n\nThe first line contains one integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of queries.\n\nThe first line of each query contains one integer $n$ ($1 \\le n \\le 100$).\n\nThe second line of each query contains $n$ integers $a_1, a_2, \\dots , a_n$ ($1 \\le a_i \\le 10^9$). \n\n\n-----Output-----\n\nFor each query print one integer in a single line \u2014 the maximum possible number of elements divisible by $3$ that are in the array after performing described operation an arbitrary (possibly, zero) number of times.\n\n\n-----Example-----\nInput\n2\n5\n3 1 2 3 1\n7\n1 1 1 1 1 2 2\n\nOutput\n3\n3\n\n\n\n-----Note-----\n\nIn the first query of the example you can apply the following sequence of operations to obtain $3$ elements divisible by $3$: $[3, 1, 2, 3, 1] \\rightarrow [3, 3, 3, 1]$.\n\nIn the second query you can obtain $3$ elements divisible by $3$ with the following sequence of operations: $[1, 1, 1, 1, 1, 2, 2] \\rightarrow [1, 1, 1, 1, 2, 3] \\rightarrow [1, 1, 1, 3, 3] \\rightarrow [2, 1, 3, 3] \\rightarrow [3, 3, 3]$."} +{"id": "03686", "text": "You are given a permutation of length $n$. Recall that the permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2, 3, 1, 5, 4]$ is a permutation, but $[1, 2, 2]$ is not a permutation ($2$ appears twice in the array) and $[1, 3, 4]$ is also not a permutation ($n=3$ but there is $4$ in the array).\n\nYou can perform at most $n-1$ operations with the given permutation (it is possible that you don't perform any operations at all). The $i$-th operation allows you to swap elements of the given permutation on positions $i$ and $i+1$. Each operation can be performed at most once. The operations can be performed in arbitrary order.\n\nYour task is to find the lexicographically minimum possible permutation obtained by performing some of the given operations in some order.\n\nYou can see the definition of the lexicographical order in the notes section.\n\nYou have to answer $q$ independent test cases.\n\nFor example, let's consider the permutation $[5, 4, 1, 3, 2]$. The minimum possible permutation we can obtain is $[1, 5, 2, 4, 3]$ and we can do it in the following way:\n\n perform the second operation (swap the second and the third elements) and obtain the permutation $[5, 1, 4, 3, 2]$; perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation $[5, 1, 4, 2, 3]$; perform the third operation (swap the third and the fourth elements) and obtain the permutation $[5, 1, 2, 4, 3]$. perform the first operation (swap the first and the second elements) and obtain the permutation $[1, 5, 2, 4, 3]$; \n\nAnother example is $[1, 2, 4, 3]$. The minimum possible permutation we can obtain is $[1, 2, 3, 4]$ by performing the third operation (swap the third and the fourth elements).\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 100$) \u2014 the number of test cases. Then $q$ test cases follow.\n\nThe first line of the test case contains one integer $n$ ($1 \\le n \\le 100$) \u2014 the number of elements in the permutation.\n\nThe second line of the test case contains $n$ distinct integers from $1$ to $n$ \u2014 the given permutation.\n\n\n-----Output-----\n\nFor each test case, print the answer on it \u2014 the lexicograhically minimum possible permutation obtained by performing some of the given operations in some order.\n\n\n-----Example-----\nInput\n4\n5\n5 4 1 3 2\n4\n1 2 4 3\n1\n1\n4\n4 3 2 1\n\nOutput\n1 5 2 4 3 \n1 2 3 4 \n1 \n1 4 3 2 \n\n\n\n-----Note-----\n\nRecall that the permutation $p$ of length $n$ is lexicographically less than the permutation $q$ of length $n$ if there is such index $i \\le n$ that for all $j$ from $1$ to $i - 1$ the condition $p_j = q_j$ is satisfied, and $p_i < q_i$. For example:\n\n $p = [1, 3, 5, 2, 4]$ is less than $q = [1, 3, 5, 4, 2]$ (such $i=4$ exists, that $p_i < q_i$ and for each $j < i$ holds $p_j = q_j$), $p = [1, 2]$ is less than $q = [2, 1]$ (such $i=1$ exists, that $p_i < q_i$ and for each $j < i$ holds $p_j = q_j$)."} +{"id": "03687", "text": "There are $n$ students standing in a circle in some order. The index of the $i$-th student is $p_i$. It is guaranteed that all indices of students are distinct integers from $1$ to $n$ (i. e. they form a permutation).\n\nStudents want to start a round dance. A clockwise round dance can be started if the student $2$ comes right after the student $1$ in clockwise order (there are no students between them), the student $3$ comes right after the student $2$ in clockwise order, and so on, and the student $n$ comes right after the student $n - 1$ in clockwise order. A counterclockwise round dance is almost the same thing \u2014 the only difference is that the student $i$ should be right after the student $i - 1$ in counterclockwise order (this condition should be met for every $i$ from $2$ to $n$). \n\nFor example, if the indices of students listed in clockwise order are $[2, 3, 4, 5, 1]$, then they can start a clockwise round dance. If the students have indices $[3, 2, 1, 4]$ in clockwise order, then they can start a counterclockwise round dance.\n\nYour task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle. \n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 200$) \u2014 the number of queries. Then $q$ queries follow.\n\nThe first line of the query contains one integer $n$ ($1 \\le n \\le 200$) \u2014 the number of students.\n\nThe second line of the query contains a permutation of indices $p_1, p_2, \\dots, p_n$ ($1 \\le p_i \\le n$), where $p_i$ is the index of the $i$-th student (in clockwise order). It is guaranteed that all $p_i$ are distinct integers from $1$ to $n$ (i. e. they form a permutation).\n\n\n-----Output-----\n\nFor each query, print the answer on it. If a round dance can be started with the given order of students, print \"YES\". Otherwise print \"NO\".\n\n\n-----Example-----\nInput\n5\n4\n1 2 3 4\n3\n1 3 2\n5\n1 2 3 5 4\n1\n1\n5\n3 2 1 5 4\n\nOutput\nYES\nYES\nNO\nYES\nYES"} +{"id": "03688", "text": "Santa has $n$ candies and he wants to gift them to $k$ kids. He wants to divide as many candies as possible between all $k$ kids. Santa can't divide one candy into parts but he is allowed to not use some candies at all.\n\nSuppose the kid who recieves the minimum number of candies has $a$ candies and the kid who recieves the maximum number of candies has $b$ candies. Then Santa will be satisfied, if the both conditions are met at the same time:\n\n $b - a \\le 1$ (it means $b = a$ or $b = a + 1$); the number of kids who has $a+1$ candies (note that $a+1$ not necessarily equals $b$) does not exceed $\\lfloor\\frac{k}{2}\\rfloor$ (less than or equal to $\\lfloor\\frac{k}{2}\\rfloor$). \n\n$\\lfloor\\frac{k}{2}\\rfloor$ is $k$ divided by $2$ and rounded down to the nearest integer. For example, if $k=5$ then $\\lfloor\\frac{k}{2}\\rfloor=\\lfloor\\frac{5}{2}\\rfloor=2$.\n\nYour task is to find the maximum number of candies Santa can give to kids so that he will be satisfied.\n\nYou have to answer $t$ independent test cases.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $t$ ($1 \\le t \\le 5 \\cdot 10^4$) \u2014 the number of test cases.\n\nThe next $t$ lines describe test cases. The $i$-th test case contains two integers $n$ and $k$ ($1 \\le n, k \\le 10^9$) \u2014 the number of candies and the number of kids.\n\n\n-----Output-----\n\nFor each test case print the answer on it \u2014 the maximum number of candies Santa can give to kids so that he will be satisfied.\n\n\n-----Example-----\nInput\n5\n5 2\n19 4\n12 7\n6 2\n100000 50010\n\nOutput\n5\n18\n10\n6\n75015\n\n\n\n-----Note-----\n\nIn the first test case, Santa can give $3$ and $2$ candies to kids. There $a=2, b=3,a+1=3$.\n\nIn the second test case, Santa can give $5, 5, 4$ and $4$ candies. There $a=4,b=5,a+1=5$. The answer cannot be greater because then the number of kids with $5$ candies will be $3$.\n\nIn the third test case, Santa can distribute candies in the following way: $[1, 2, 2, 1, 1, 2, 1]$. There $a=1,b=2,a+1=2$. He cannot distribute two remaining candies in a way to be satisfied.\n\nIn the fourth test case, Santa can distribute candies in the following way: $[3, 3]$. There $a=3, b=3, a+1=4$. Santa distributed all $6$ candies."} +{"id": "03689", "text": "You are given two positive integers $n$ ($1 \\le n \\le 10^9$) and $k$ ($1 \\le k \\le 100$). Represent the number $n$ as the sum of $k$ positive integers of the same parity (have the same remainder when divided by $2$).\n\nIn other words, find $a_1, a_2, \\ldots, a_k$ such that all $a_i>0$, $n = a_1 + a_2 + \\ldots + a_k$ and either all $a_i$ are even or all $a_i$ are odd at the same time.\n\nIf such a representation does not exist, then report it.\n\n\n-----Input-----\n\nThe first line contains an integer $t$ ($1 \\le t \\le 1000$) \u2014 the number of test cases in the input. Next, $t$ test cases are given, one per line.\n\nEach test case is two positive integers $n$ ($1 \\le n \\le 10^9$) and $k$ ($1 \\le k \\le 100$).\n\n\n-----Output-----\n\nFor each test case print:\n\n YES and the required values $a_i$, if the answer exists (if there are several answers, print any of them); NO if the answer does not exist. \n\nThe letters in the words YES and NO can be printed in any case.\n\n\n-----Example-----\nInput\n8\n10 3\n100 4\n8 7\n97 2\n8 8\n3 10\n5 3\n1000000000 9\n\nOutput\nYES\n4 2 4\nYES\n55 5 5 35\nNO\nNO\nYES\n1 1 1 1 1 1 1 1\nNO\nYES\n3 1 1\nYES\n111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120"} +{"id": "03690", "text": "Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it.\n\nAfter taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies.\n\nAlice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies).\n\nYou have to answer $q$ independent queries.\n\nLet's see the following example: $[1, 3, 4]$. Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob\u00a0\u2014 then Alice has $4$ candies, and Bob has $4$ candies.\n\nAnother example is $[1, 10, 100]$. Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes $54$ candies, and Alice takes $46$ candies. Now Bob has $55$ candies, and Alice has $56$ candies, so she has to discard one candy\u00a0\u2014 and after that, she has $55$ candies too.\n\n\n-----Input-----\n\nThe first line of the input contains one integer $q$ ($1 \\le q \\le 1000$)\u00a0\u2014 the number of queries. Then $q$ queries follow.\n\nThe only line of the query contains three integers $a, b$ and $c$ ($1 \\le a, b, c \\le 10^{16}$)\u00a0\u2014 the number of candies in the first, second and third piles correspondingly.\n\n\n-----Output-----\n\nPrint $q$ lines. The $i$-th line should contain the answer for the $i$-th query\u00a0\u2014 the maximum number of candies Alice can have after the division, if both Alice and Bob act optimally (of course, Bob will have the same number of candies).\n\n\n-----Example-----\nInput\n4\n1 3 4\n1 10 100\n10000000000000000 10000000000000000 10000000000000000\n23 34 45\n\nOutput\n4\n55\n15000000000000000\n51"} +{"id": "03691", "text": "The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet (\"a\"\u2013\"z\"). You want to buy some beads to assemble a necklace from them.\n\nA necklace is a set of beads connected in a circle.\n\nFor example, if the store sells beads \"a\", \"b\", \"c\", \"a\", \"c\", \"c\", then you can assemble the following necklaces (these are not all possible options): [Image] \n\nAnd the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads \"a\" (of the two available). The second necklace cannot be assembled because it contains a bead \"d\", which is not sold in the store. \n\nWe call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful.\n\nIn particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$.\n\nYou are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabet\u00a0\u2014 each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble.\n\n\n-----Input-----\n\nThe first line contains a single integer $t$ ($1 \\le t \\le 100$)\u00a0\u2014 the number of test cases in the test. Then $t$ test cases follow.\n\nThe first line of each test case contains two integers $n$ and $k$ ($1 \\le n, k \\le 2000$).\n\nThe second line of each test case contains the string $s$ containing $n$ lowercase English letters\u00a0\u2014 the beads in the store.\n\nIt is guaranteed that the sum of $n$ for all test cases does not exceed $2000$.\n\n\n-----Output-----\n\nOutput $t$ answers to the test cases. Each answer is a positive integer\u00a0\u2014 the maximum length of the $k$-beautiful necklace you can assemble.\n\n\n-----Example-----\nInput\n6\n6 3\nabcbac\n3 6\naaa\n7 1000\nabczgyo\n5 4\nababa\n20 10\naaebdbabdbbddaadaadc\n20 5\necbedececacbcbccbdec\n\nOutput\n6\n3\n5\n4\n15\n10\n\n\n\n-----Note-----\n\nThe first test case is explained in the statement.\n\nIn the second test case, a $6$-beautiful necklace can be assembled from all the letters.\n\nIn the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads \"abzyo\"."} +{"id": "03692", "text": "You are given an array $a$ consisting of $n$ integers $a_1, a_2, \\dots, a_n$. You want to split it into exactly $k$ non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the $n$ elements of the array $a$ must belong to exactly one of the $k$ subsegments.\n\nLet's see some examples of dividing the array of length $5$ into $3$ subsegments (not necessarily with odd sums): $[1, 2, 3, 4, 5]$ is the initial array, then all possible ways to divide it into $3$ non-empty non-intersecting subsegments are described below: $[1], [2], [3, 4, 5]$; $[1], [2, 3], [4, 5]$; $[1], [2, 3, 4], [5]$; $[1, 2], [3], [4, 5]$; $[1, 2], [3, 4], [5]$; $[1, 2, 3], [4], [5]$. \n\nOf course, it can be impossible to divide the initial array into exactly $k$ subsegments in such a way that each of them will have odd sum of elements. In this case print \"NO\". Otherwise, print \"YES\" and any possible division of the array. See the output format for the detailed explanation.\n\nYou have to answer $q$ independent queries.\n\n\n-----Input-----\n\nThe first line contains one integer $q$ ($1 \\le q \\le 2 \\cdot 10^5$) \u2014 the number of queries. Then $q$ queries follow.\n\nThe first line of the query contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$) \u2014 the number of elements in the array and the number of subsegments, respectively.\n\nThe second line of the query contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$), where $a_i$ is the $i$-th element of $a$.\n\nIt is guaranteed that the sum of $n$ over all queries does not exceed $2 \\cdot 10^5$ ($\\sum n \\le 2 \\cdot 10^5$).\n\n\n-----Output-----\n\nFor each query, print the answer to it. If it is impossible to divide the initial array into exactly $k$ subsegments in such a way that each of them will have odd sum of elements, print \"NO\" in the first line. Otherwise, print \"YES\" in the first line and any possible division of the array in the second line. The division can be represented as $k$ integers $r_1$, $r_2$, ..., $r_k$ such that $1 \\le r_1 < r_2 < \\dots < r_k = n$, where $r_j$ is the right border of the $j$-th segment (the index of the last element that belongs to the $j$-th segment), so the array is divided into subsegments $[1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], \\dots, [r_{k - 1} + 1, n]$. Note that $r_k$ is always $n$ but you should print it anyway. \n\n\n-----Example-----\nInput\n3\n5 3\n7 18 3 14 1\n5 4\n1 2 3 4 5\n6 2\n1 2 8 4 10 2\n\nOutput\nYES\n1 3 5\nNO\nNO"} +{"id": "03693", "text": "Reverse bits of a given 32 bits unsigned integer.\n\nNote:\n\nNote that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.\nIn Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.\nFollow up:\n\nIf this function is called many times, how would you optimize it?\n\nExample 1:\n\nInput: n = 00000010100101000001111010011100\nOutput: 964176192 (00111001011110000010100101000000)\nExplanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.\n\nExample 2:\n\nInput: n = 11111111111111111111111111111101\nOutput: 3221225471 (10111111111111111111111111111111)\nExplanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111. \n\nConstraints:\n\nThe input must be a binary string of length 32"} +{"id": "03694", "text": "Given a non-negative integer\u00a0numRows, generate the first numRows of Pascal's triangle.\n\n\nIn Pascal's triangle, each number is the sum of the two numbers directly above it.\n\nExample:\n\n\nInput: 5\nOutput:\n[\n [1],\n [1,1],\n [1,2,1],\n [1,3,3,1],\n [1,4,6,4,1]\n]"} +{"id": "03695", "text": "=====Problem Statement=====\nYou are given an integer N followed by N email addresses. Your task is to print a list containing only valid email addresses in lexicographical order.\nValid email addresses must follow these rules:\nIt must have the username@websitename.extension format type.\nThe username can only contain letters, digits, dashes and underscores.\nThe website name can only have letters and digits.\nThe maximum length of the extension is 3. \n\nConcept\n\nA filter takes a function returning True or False and applies it to a sequence, returning a list of only those members of the sequence where the function returned True. A Lambda function can be used with filters.\n\nLet's say you have to make a list of the squares of integers from 0 to 9 (both included).\n\n>> l = list(range(10))\n>> l = list(map(lambda x:x*x, l))\n\nNow, you only require those elements that are greater than 10 but less than 80.\n\n>> l = list(filter(lambda x: x > 10 and x < 80, l))\n\nEasy, isn't it?\n\n=====Input Format=====\nThe first line of input is the integer N, the number of email addresses.\nN lines follow, each containing a string.\n\n\n=====Constraints=====\nEach line is a non-empty string.\n\n=====Output Format=====\nOutput a list containing the valid email addresses in lexicographical order. If the list is empty, just output an empty list, []."} +{"id": "03696", "text": "=====Problem Statement=====\nLet's learn some new Python concepts! You have to generate a list of the first N fibonacci numbers, 0 being the first number. Then, apply the map function and a lambda expression to cube each fibonacci number and print the list.\n\nConcept\n\nThe map() function applies a function to every member of an iterable and returns the result. It takes two parameters: first, the function that is to be applied and secondly, the iterables.\nLet's say you are given a list of names, and you have to print a list that contains the length of each name.\n\n>> print (list(map(len, ['Tina', 'Raj', 'Tom']))) \n[4, 3, 3] \n\nLambda is a single expression anonymous function often used as an inline function. In simple words, it is a function that has only one line in its body. It proves very handy in functional and GUI programming.\n\n>> sum = lambda a, b, c: a + b + c\n>> sum(1, 2, 3)\n6\n\nNote:\n\nLambda functions cannot use the return statement and can only have a single expression. Unlike def, which creates a function and assigns it a name, lambda creates a function and returns the function itself. Lambda can be used inside lists and dictionaries. \n\n=====Input Format=====\nOne line of input: an integer N.\n\n=====Output Format=====\nA list on a single line containing the cubes of the first N fibonacci numbers."} +{"id": "03697", "text": "=====Problem Statement=====\nYou are given a valid XML document, and you have to print the maximum level of nesting in it. Take the depth of the root as 0.\n\n=====Input Format=====\nThe first line contains N, the number of lines in the XML document.\nThe next N lines follow containing the XML document.\n\n=====Output Format=====\nOutput a single line, the integer value of the maximum level of nesting in the XML document."} +{"id": "03698", "text": "=====Problem Statement=====\nLet's dive into the interesting topic of regular expressions! You are given some input, and you are required to check whether they are valid mobile numbers.\n\nConcept\nA valid mobile number is a ten digit number starting with a 7, 8, or 9.\n\n=====Input Format=====\nThe first line contains an integer N, the number of inputs.\nN lines follow, each containing some string.\n\n=====Constraints=====\n1\u2264N\u226410\n2\u2264len(Number)\u226415\n\n=====Output Format=====\nFor every string listed, print \"YES\" if it is a valid mobile number and \"NO\" if it is not on separate lines. Do not print the quotes."} +{"id": "03699", "text": "=====Function Descriptions=====\nObjective\nToday, we're learning about a new data type: sets.\n\nConcept\n\nIf the inputs are given on one line separated by a space character, use split() to get the separate values in the form of a list:\n\n>> a = raw_input()\n5 4 3 2\n>> lis = a.split()\n>> print (lis)\n['5', '4', '3', '2']\n\nIf the list values are all integer types, use the map() method to convert all the strings to integers.\n\n>> newlis = list(map(int, lis))\n>> print (newlis)\n[5, 4, 3, 2]\n\nSets are an unordered bag of unique values. A single set contains values of any immutable data type.\n\nCREATING SETS\n\n>> myset = {1, 2} # Directly assigning values to a set\n>> myset = set() # Initializing a set\n>> myset = set(['a', 'b']) # Creating a set from a list\n>> myset\n{'a', 'b'}\n\n\nMODIFYING SETS\n\nUsing the add() function:\n\n>> myset.add('c')\n>> myset\n{'a', 'c', 'b'}\n>> myset.add('a') # As 'a' already exists in the set, nothing happens\n>> myset.add((5, 4))\n>> myset\n{'a', 'c', 'b', (5, 4)}\n\n\nUsing the update() function:\n\n>> myset.update([1, 2, 3, 4]) # update() only works for iterable objects\n>> myset\n{'a', 1, 'c', 'b', 4, 2, (5, 4), 3}\n>> myset.update({1, 7, 8})\n>> myset\n{'a', 1, 'c', 'b', 4, 7, 8, 2, (5, 4), 3}\n>> myset.update({1, 6}, [5, 13])\n>> myset\n{'a', 1, 'c', 'b', 4, 5, 6, 7, 8, 2, (5, 4), 13, 3}\n\n\nREMOVING ITEMS\n\nBoth the discard() and remove() functions take a single value as an argument and removes that value from the set. If that value is not present, discard() does nothing, but remove() will raise a KeyError exception.\n\n>> myset.discard(10)\n>> myset\n{'a', 1, 'c', 'b', 4, 5, 7, 8, 2, 12, (5, 4), 13, 11, 3}\n>> myset.remove(13)\n>> myset\n{'a', 1, 'c', 'b', 4, 5, 7, 8, 2, 12, (5, 4), 11, 3}\n\n\nCOMMON SET OPERATIONS Using union(), intersection() and difference() functions.\n\n>> a = {2, 4, 5, 9}\n>> b = {2, 4, 11, 12}\n>> a.union(b) # Values which exist in a or b\n{2, 4, 5, 9, 11, 12}\n>> a.intersection(b) # Values which exist in a and b\n{2, 4}\n>> a.difference(b) # Values which exist in a but not in b\n{9, 5}\n\n\nThe union() and intersection() functions are symmetric methods:\n\n>> a.union(b) == b.union(a)\nTrue\n>> a.intersection(b) == b.intersection(a)\nTrue\n>> a.difference(b) == b.difference(a)\nFalse\n\nThese other built-in data structures in Python are also useful.\n\n=====Problem Statement=====\nGiven 2 sets of integers, M and N, print their symmetric difference in ascending order. The term symmetric difference indicates those values that exist in either M or N but do not exist in both.\n\n=====Input Format=====\nThe first line of input contains an integer, M.\nThe second line contains M space-separated integers.\nThe third line contains an integer, N.\nThe fourth line contains N space-separated integers. \n\n=====Output Format=====\nOutput the symmetric difference integers in ascending order, one per line."} +{"id": "03700", "text": "=====Function Descriptions=====\ncollections.namedtuple()\n\nBasically, namedtuples are easy to create, lightweight object types.\nThey turn tuples into convenient containers for simple tasks.\nWith namedtuples, you don\u2019t have to use integer indices for accessing members of a tuple.\n\nExample\nCode 01\n>>> from collections import namedtuple\n>>> Point = namedtuple('Point','x,y')\n>>> pt1 = Point(1,2)\n>>> pt2 = Point(3,4)\n>>> dot_product = ( pt1.x * pt2.x ) +( pt1.y * pt2.y )\n>>> print dot_product\n11\n\nCode 02\n>>> from collections import namedtuple\n>>> Car = namedtuple('Car','Price Mileage Colour Class')\n>>> xyz = Car(Price = 100000, Mileage = 30, Colour = 'Cyan', Class = 'Y')\n>>> print xyz\nCar(Price=100000, Mileage=30, Colour='Cyan', Class='Y')\n>>> print xyz.Class\nY\n\n=====Problem Statement=====\nDr. John Wesley has a spreadsheet containing a list of student's IDs, marks, class and name.\nYour task is to help Dr. Wesley calculate the average marks of the students.\nAverage = Sum of all marks / Total students\n\nNote:\n1. Columns can be in any order. IDs, marks, class and name can be written in any order in the spreadsheet.\n2. Column names are ID, MARKS, CLASS and NAME. (The spelling and case type of these names won't change.)\n\n=====Input Format=====\nThe first line contains an integer N, the total number of students.\nThe second line contains the names of the columns in any order.\nThe next N lines contains the marks, IDs, name and class, under their respective column names.\n\n=====Constraints=====\n0