contestId
int64
0
1.01k
name
stringlengths
2
58
tags
sequencelengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
sequencelengths
0
7
demo-output
sequencelengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
67
Restoration of the Permutation
[ "greedy" ]
B. Restoration of the Permutation
1
256
Let *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} be any permutation of the first *n* natural numbers {1,<=2,<=...,<=*n*}. You are given a positive integer *k* and another sequence *B*<==<={*b*1,<=*b*2,<=...,<=*b**n*}, where *b**i* is the number of elements *a**j* in *A* to the left of the element *a**t*<==<=*i* such that *a**j*<=β‰₯<=(*i*<=+<=*k*). For example, if *n*<==<=5, a possible *A* is {5,<=1,<=4,<=2,<=3}. For *k*<==<=2, *B* is given by {1,<=2,<=1,<=0,<=0}. But if *k*<==<=3, then *B*<==<={1,<=1,<=0,<=0,<=0}. For two sequences *X*<==<={*x*1,<=*x*2,<=...,<=*x**n*} and *Y*<==<={*y*1,<=*y*2,<=...,<=*y**n*}, let *i*-th elements be the first elements such that *x**i*<=β‰ <=*y**i*. If *x**i*<=&lt;<=*y**i*, then *X* is lexicographically smaller than *Y*, while if *x**i*<=&gt;<=*y**i*, then *X* is lexicographically greater than *Y*. Given *n*, *k* and *B*, you need to determine the lexicographically smallest *A*.
The first line contains two space separated integers *n* and *k* (1<=≀<=*n*<=≀<=1000, 1<=≀<=*k*<=≀<=*n*). On the second line are *n* integers specifying the values of *B*<==<={*b*1,<=*b*2,<=...,<=*b**n*}.
Print on a single line *n* integers of *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} such that *A* is lexicographically minimal. It is guaranteed that the solution exists.
[ "5 2\n1 2 1 0 0\n", "4 2\n1 0 0 0\n" ]
[ "4 1 5 2 3 ", "2 3 1 4 " ]
none
[ { "input": "5 2\n1 2 1 0 0", "output": "4 1 5 2 3 " }, { "input": "4 2\n1 0 0 0", "output": "2 3 1 4 " }, { "input": "10 3\n4 2 4 2 1 0 1 0 0 0", "output": "6 8 2 5 9 1 4 10 3 7 " }, { "input": "15 3\n4 2 7 5 1 1 1 0 0 0 0 0 0 0 0", "output": "8 5 2 9 6 1 10 7 11 4 12 3 13 14 15 " }, { "input": "5 1\n1 3 2 0 0", "output": "4 1 5 3 2 " }, { "input": "8 3\n2 0 2 2 1 0 0 0", "output": "2 6 7 1 3 8 4 5 " }, { "input": "1 1\n0", "output": "1 " }, { "input": "10 10\n0 0 0 0 0 0 0 0 0 0", "output": "1 2 3 4 5 6 7 8 9 10 " }, { "input": "10 9\n1 0 0 0 0 0 0 0 0 0", "output": "2 3 4 5 6 7 8 9 10 1 " }, { "input": "10 1\n9 8 7 6 5 4 3 2 1 0", "output": "10 9 8 7 6 5 4 3 2 1 " }, { "input": "10 1\n7 6 5 4 3 2 1 0 0 0", "output": "8 7 6 5 4 3 2 1 9 10 " }, { "input": "13 2\n1 2 3 4 5 4 3 2 1 0 0 0 0", "output": "10 1 11 2 8 3 9 4 6 7 5 12 13 " }, { "input": "2 2\n0 0", "output": "1 2 " }, { "input": "2 1\n1 0", "output": "2 1 " }, { "input": "2 1\n0 0", "output": "1 2 " }, { "input": "3 1\n2 1 0", "output": "3 2 1 " }, { "input": "5 1\n4 3 2 1 0", "output": "5 4 3 2 1 " }, { "input": "5 4\n1 0 0 0 0", "output": "2 3 4 5 1 " }, { "input": "20 4\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0", "output": "17 1 2 3 4 5 6 7 8 9 10 11 12 13 18 14 19 15 20 16 " }, { "input": "20 2\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0", "output": "19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 20 18 " }, { "input": "20 1\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0", "output": "20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 " }, { "input": "20 1\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0", "output": "18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 " }, { "input": "10 1\n1 1 1 1 1 1 1 1 1 0", "output": "10 1 2 3 4 5 6 7 8 9 " }, { "input": "130 32\n72 56 81 71 45 17 65 13 87 22 17 75 72 80 68 76 62 35 14 53 49 72 19 55 10 57 28 35 66 59 45 24 37 65 46 31 34 1 30 12 37 13 40 41 39 6 31 28 30 0 2 7 23 32 0 34 21 0 23 3 18 33 7 6 20 19 21 10 27 28 19 23 5 15 22 13 14 1 3 14 0 5 1 14 0 0 7 11 10 1 1 1 6 5 3 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "50 55 58 81 38 85 86 51 97 99 100 46 60 101 8 102 25 52 103 6 19 64 11 104 63 105 106 40 107 10 42 108 23 109 68 73 110 78 111 112 113 79 32 114 115 27 61 83 18 116 117 65 66 118 28 53 5 57 82 119 36 39 67 120 48 59 74 76 2 33 121 37 71 122 21 47 49 77 90 41 123 20 31 91 124 7 92 125 43 1 4 45 54 80 17 24 35 44 87 126 72 26 127 15 69 3 128 13 56 75 12 129 70 30 84 95 62 16 130 88 14 22 89 29 93 9 94 96 98 34 " } ]
590
409,600
3.704237
57,876
612
Simba on the Circle
[ "dp" ]
null
null
You are given a circular array with *n* elements. The elements are numbered from some element with values from 1 to *n* in clockwise order. The *i*-th cell contains the value *a**i*. The robot Simba is in cell *s*. Each moment of time the robot is in some of the *n* cells (at the begin he is in *s*). In one turn the robot can write out the number written in current cell or move to the adjacent cell in clockwise or counterclockwise direction. To write out the number from the cell Simba doesn't spend any time, but to move to adjacent cell Simba spends one unit of time. Simba wants to write the number from each cell one time, so the numbers will be written in a non decreasing order. Find the least number of time units to write out all numbers.
The first line contains two integers *n* and *s* (1<=≀<=*s*<=≀<=*n*<=≀<=2000) β€” the number of cells in the circular array and the starting position of Simba. The second line contains *n* integers *a**i* (<=-<=109<=≀<=*a**i*<=≀<=109) β€” the number written in the *i*-th cell. The numbers are given for cells in order from 1 to *n*. Some of numbers *a**i* can be equal.
In the first line print the number *t* β€” the least number of time units. Each of the next *n* lines should contain the direction of robot movement and the number of cells to move in that direction. After that movement the robot writes out the number from the cell in which it turns out. The direction and the number of cells should be printed in the form of +x in case of clockwise movement and -x in case of counterclockwise movement to *x* cells (0<=≀<=*x*<=≀<=*n*<=-<=1). Note that the sum of absolute values of *x* should be equal to *t*.
[ "9 1\n0 1 2 2 2 1 0 1 1\n", "8 1\n0 1 0 1 0 1 0 1\n", "8 1\n1 2 3 4 5 6 7 8\n", "8 1\n0 0 0 0 0 0 0 0\n" ]
[ "12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1\n", "13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2\n", "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1\n", "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1\n" ]
none
[ { "input": "9 1\n0 1 2 2 2 1 0 1 1", "output": "12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1" }, { "input": "8 1\n0 1 0 1 0 1 0 1", "output": "13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2" }, { "input": "8 1\n1 2 3 4 5 6 7 8", "output": "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1" }, { "input": "8 1\n0 0 0 0 0 0 0 0", "output": "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1" }, { "input": "8 1\n0 1 2 2 1 0 1 1", "output": "11\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1" }, { "input": "1 1\n4", "output": "0\n+0" }, { "input": "10 1\n-1 0 1 0 -1 1 0 0 1 -1", "output": "22\n+0\n-1\n-5\n-3\n+2\n+3\n+1\n+1\n-3\n-3" }, { "input": "20 7\n0 6 0 0 0 -7 -8 9 -7 4 7 2 -4 4 -5 2 6 8 -2 -7", "output": "83\n+0\n+2\n-3\n-6\n-5\n-2\n+6\n+2\n+2\n+1\n+1\n+7\n+4\n-2\n-4\n-8\n-5\n-6\n+7\n+10" }, { "input": "30 13\n68 50 99 23 84 23 24 -42 82 36 -10 -51 -96 96 19 -4 4 -41 74 92 13 58 26 79 -11 38 -80 -38 73 -21", "output": "238\n+0\n+14\n+15\n-4\n+10\n+10\n+2\n-5\n-14\n+5\n+1\n+4\n-6\n-11\n+2\n+1\n-14\n-13\n-14\n+6\n-10\n+9\n-2\n-10\n+5\n+15\n-4\n+15\n-6\n-11" }, { "input": "40 1\n886 -661 499 -14 -101 660 -259 -499 -766 155 -120 -112 -922 979 36 528 593 653 409 -476 -125 183 -817 59 353 16 525 -43 -388 989 306 -145 935 -712 -243 460 -861 339 347 -445", "output": "437\n+12\n-16\n-14\n-14\n-15\n+8\n+6\n+12\n+20\n-11\n+18\n-12\n-3\n-11\n-10\n+1\n-7\n-17\n+16\n-18\n-11\n+9\n-14\n+12\n+9\n+7\n+1\n-14\n-6\n+17\n+7\n-16\n-11\n+1\n+1\n-12\n-5\n-8\n-19\n+16" }, { "input": "50 32\n2624 -8355 -5993 -1 8197 382 -9197 -5078 -7 -1021 -4419 8918 -7114 5016 1912 -8436 -1217 2178 -6513 -9910 -1695 7501 7028 -6171 9063 9112 9063 -1886 9156 -7256 8871 -6855 7059 -5209 2308 5964 -4283 2248 1790 -6658 2906 -478 -5663 -9250 4355 1099 1468 -3051 -9353 -5717", "output": "601\n-12\n-21\n-5\n+13\n+9\n-14\n-22\n-17\n+19\n+8\n-21\n+5\n-21\n-3\n-7\n-9\n+24\n+3\n-24\n+11\n-20\n-7\n-4\n-7\n-18\n+17\n-5\n+2\n-10\n+1\n-8\n-24\n+3\n+20\n-3\n+16\n-10\n+4\n+19\n+22\n-13\n+10\n-11\n-17\n-24\n-19\n+13\n+2\n-1\n+3" }, { "input": "60 32\n58726 58267 -31806 44691 -52713 -11475 61179 83630 93772 48048 -64921 -16810 -16172 -30820 30109 -81876 -27921 -69676 -28393 -45495 6588 -30154 21312 50563 22336 -37995 -31034 -30980 -72408 -29962 -4891 24299 8648 -69415 -62580 95513 -13691 -92575 -10376 40008 2041 -24616 -6934 -42025 68949 -87961 -91709 -46669 -36624 -75601 -83110 43195 86628 53287 -14813 -7263 -20579 -51021 37654 -13428", "output": "884\n+6\n+9\n-1\n+5\n+25\n-26\n-21\n-11\n+16\n-23\n+24\n+30\n-7\n-10\n-28\n+24\n-18\n+23\n+14\n+24\n+1\n-14\n+8\n+8\n-11\n-2\n+25\n+15\n+15\n+1\n-18\n-18\n+23\n+6\n-27\n+17\n-13\n-12\n+10\n-20\n+12\n-10\n+2\n+7\n-17\n-16\n-19\n+12\n+12\n+6\n+14\n+30\n+8\n-1\n+6\n-22\n+23\n-15\n+16\n+27" } ]
61
409,600
0
58,003
794
Labelling Cities
[ "dfs and similar", "graphs", "hashing" ]
null
null
Oleg the bank client lives in Bankopolia. There are *n* cities in Bankopolia and some pair of cities are connected directly by bi-directional roads. The cities are numbered from 1 to *n*. There are a total of *m* roads in Bankopolia, the *i*-th road connects cities *u**i* and *v**i*. It is guaranteed that from each city it is possible to travel to any other city using some of the roads. Oleg wants to give a label to each city. Suppose the label of city *i* is equal to *x**i*. Then, it must hold that for all pairs of cities (*u*,<=*v*) the condition |*x**u*<=-<=*x**v*|<=≀<=1 holds if and only if there is a road connecting *u* and *v*. Oleg wonders if such a labeling is possible. Find an example of such labeling if the task is possible and state that it is impossible otherwise.
The first line of input contains two space-separated integers *n* and *m* (2<=≀<=*n*<=≀<=3Β·105, 1<=≀<=*m*<=≀<=3Β·105)Β β€” the number of cities and the number of roads. Next, *m* lines follow. The *i*-th line contains two space-separated integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*)Β β€” the cities connected by the *i*-th road. It is guaranteed that there is at most one road between each pair of cities and it is possible to travel from any city to any other city using some roads.
If the required labeling is not possible, output a single line containing the string "NO" (without quotes). Otherwise, output the string "YES" (without quotes) on the first line. On the next line, output *n* space-separated integers, *x*1,<=*x*2,<=...,<=*x**n*. The condition 1<=≀<=*x**i*<=≀<=109 must hold for all *i*, and for all pairs of cities (*u*,<=*v*) the condition |*x**u*<=-<=*x**v*|<=≀<=1 must hold if and only if there is a road connecting *u* and *v*.
[ "4 4\n1 2\n1 3\n1 4\n3 4\n", "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n5 4\n", "4 3\n1 2\n1 3\n1 4\n" ]
[ "YES\n2 3 1 1 \n", "YES\n1 1 1 1 1 \n", "NO\n" ]
For the first sample, *x*<sub class="lower-index">1</sub> = 2, *x*<sub class="lower-index">2</sub> = 3, *x*<sub class="lower-index">3</sub> = *x*<sub class="lower-index">4</sub> = 1 is a valid labeling. Indeed, (3, 4), (1, 2), (1, 3), (1, 4) are the only pairs of cities with difference of labels not greater than 1, and these are precisely the roads of Bankopolia. For the second sample, all pairs of cities have difference of labels not greater than 1 and all pairs of cities have a road connecting them. For the last sample, it is impossible to construct a labeling satisfying the given constraints.
[ { "input": "4 4\n1 2\n1 3\n1 4\n3 4", "output": "YES\n2 3 1 1 " }, { "input": "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n5 4", "output": "YES\n1 1 1 1 1 " }, { "input": "4 3\n1 2\n1 3\n1 4", "output": "NO" }, { "input": "8 12\n1 2\n1 3\n1 4\n2 3\n2 4\n4 5\n5 6\n5 7\n5 8\n6 7\n6 8\n7 8", "output": "YES\n4 4 5 3 2 1 1 1 " }, { "input": "10 9\n1 2\n2 5\n5 10\n10 3\n3 7\n7 8\n8 6\n6 4\n4 9", "output": "YES\n10 9 6 2 8 3 5 4 1 7 " }, { "input": "8 13\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n4 5\n5 6\n5 7\n5 8\n6 7\n6 8\n7 8", "output": "YES\n4 4 4 3 2 1 1 1 " }, { "input": "2 1\n1 2", "output": "YES\n1 1 " } ]
1,621
98,201,600
0
58,053
155
Combination
[ "greedy", "sortings" ]
null
null
Ilya plays a card game by the following rules. A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number *a**i*, and the bottom contains number *b**i*, then when the player is playing the card, he gets *a**i* points and also gets the opportunity to play additional *b**i* cards. After the playing the card is discarded. More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number *b**i*, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards. Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1000) β€” the number of cards Ilya has. Each of the next *n* lines contains two non-negative space-separated integers β€” *a**i* and *b**i* (0<=≀<=*a**i*,<=*b**i*<=≀<=104) β€” the numbers, written at the top and the bottom of the *i*-th card correspondingly.
Print the single number β€” the maximum number of points you can score in one round by the described rules.
[ "2\n1 0\n2 0\n", "3\n1 0\n2 0\n0 2\n" ]
[ "2\n", "3\n" ]
In the first sample none of two cards brings extra moves, so you should play the one that will bring more points. In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards.
[ { "input": "2\n1 0\n2 0", "output": "2" }, { "input": "3\n1 0\n2 0\n0 2", "output": "3" }, { "input": "5\n0 0\n2 0\n2 0\n3 0\n5 1", "output": "8" }, { "input": "7\n9 1\n8 1\n9 0\n9 1\n5 1\n1 1\n0 1", "output": "41" }, { "input": "7\n5 0\n4 0\n3 0\n5 2\n3 0\n4 2\n0 0", "output": "21" }, { "input": "1\n7 0", "output": "7" }, { "input": "1\n10 1", "output": "10" }, { "input": "10\n18 0\n4 0\n10 0\n5 0\n1 0\n18 0\n19 0\n11 1\n11 1\n0 1", "output": "41" }, { "input": "20\n33 0\n53 0\n91 0\n15 0\n35 0\n55 0\n23 0\n70 0\n98 0\n98 0\n56 0\n65 0\n20 0\n45 0\n71 0\n80 0\n39 0\n41 0\n47 0\n79 0", "output": "98" }, { "input": "20\n20 0\n36 0\n27 0\n25 0\n0 0\n24 0\n90 0\n94 0\n100 0\n72 0\n50 0\n6 0\n16 0\n85 0\n22 4\n60 0\n48 0\n13 0\n13 0\n7 0", "output": "391" }, { "input": "50\n35 0\n72 0\n28 0\n47 0\n20 0\n94 0\n82 0\n23 0\n71 0\n92 0\n79 0\n74 0\n19 4\n36 0\n59 0\n71 0\n53 0\n36 0\n11 4\n31 0\n77 0\n47 0\n71 0\n69 0\n53 0\n2 0\n56 0\n69 0\n13 0\n78 0\n84 0\n33 0\n77 0\n28 0\n14 2\n32 0\n86 0\n7 0\n6 0\n52 0\n89 0\n66 1\n0 0\n2 0\n41 0\n81 0\n5 0\n5 0\n58 4\n57 0", "output": "1087" }, { "input": "50\n137 0\n174 1\n10 0\n58 0\n85 3\n35 0\n125 0\n53 0\n185 0\n19 0\n192 0\n182 0\n70 0\n174 1\n86 0\n153 0\n9 0\n87 2\n158 0\n171 0\n45 0\n29 0\n27 0\n115 0\n106 1\n159 3\n13 0\n61 3\n106 0\n140 0\n18 0\n144 2\n176 0\n3 0\n112 0\n106 2\n6 0\n182 0\n128 0\n23 1\n127 0\n127 0\n50 0\n19 0\n119 0\n180 0\n29 0\n130 0\n127 0\n37 0", "output": "2838" }, { "input": "100\n0 0\n1 0\n1 0\n1 0\n1 0\n1 0\n0 0\n1 0\n0 0\n0 0\n1 0\n0 0\n0 0\n1 0\n1 0\n1 0\n0 0\n0 0\n1 0\n0 0\n1 0\n1 0\n1 0\n0 0\n0 0\n1 0\n0 0\n0 0\n0 0\n0 0\n1 0\n0 0\n0 0\n0 0\n1 0\n1 0\n0 0\n1 0\n0 0\n0 0\n0 0\n1 0\n0 0\n1 0\n1 0\n1 0\n1 0\n0 0\n1 0\n1 0\n1 0\n0 0\n1 0\n0 0\n0 0\n1 0\n1 0\n1 0\n1 0\n1 0\n0 0\n1 0\n1 0\n1 0\n1 0\n0 0\n1 0\n1 0\n0 0\n0 0\n0 0\n0 0\n0 0\n1 0\n1 0\n0 0\n1 0\n0 0\n1 0\n0 0\n1 0\n1 0\n0 0\n0 0\n0 0\n1 0\n1 0\n1 0\n0 0\n0 0\n1 0\n1 0\n0 0\n1 0\n1 0\n1 0\n0 0\n0 0\n0 0\n1 0", "output": "1" }, { "input": "100\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 0\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1", "output": "0" }, { "input": "1\n0 0", "output": "0" }, { "input": "1\n0 10000", "output": "0" }, { "input": "1\n2 9999", "output": "2" }, { "input": "2\n0 10000\n1 0", "output": "1" }, { "input": "7\n1 1000\n100 1000\n3 1000\n4 1000\n5 1000\n6 1000\n7 1000", "output": "126" } ]
92
0
0
58,088
575
Bribes
[ "dfs and similar", "graphs", "trees" ]
null
null
Ruritania is a country with a very badly maintained road network, which is not exactly good news for lorry drivers that constantly have to do deliveries. In fact, when roads are maintained, they become one-way. It turns out that it is sometimes impossible to get from one town to another in a legal way – however, we know that all towns are reachable, though illegally! Fortunately for us, the police tend to be very corrupt and they will allow a lorry driver to break the rules and drive in the wrong direction provided they receive β€˜a small gift’. There is one patrol car for every road and they will request 1000 Ruritanian dinars when a driver drives in the wrong direction. However, being greedy, every time a patrol car notices the same driver breaking the rule, they will charge double the amount of money they requested the previous time on that particular road. Borna is a lorry driver that managed to figure out this bribing pattern. As part of his job, he has to make *K* stops in some towns all over Ruritania and he has to make these stops in a certain order. There are *N* towns (enumerated from 1 to *N*) in Ruritania and Borna’s initial location is the capital city i.e. town 1. He happens to know which ones out of the *N*<=-<=1 roads in Ruritania are currently unidirectional, but he is unable to compute the least amount of money he needs to prepare for bribing the police. Help Borna by providing him with an answer and you will be richly rewarded.
The first line contains *N*, the number of towns in Ruritania. The following *N*<=-<=1 lines contain information regarding individual roads between towns. A road is represented by a tuple of integers (*a*,*b*,*x*), which are separated with a single whitespace character. The numbers *a* and *b* represent the cities connected by this particular road, and x is either 0 or 1: 0 means that the road is bidirectional, 1 means that only the *a*<=β†’<=*b* direction is legal. The next line contains *K*, the number of stops Borna has to make. The final line of input contains K positive integers *s*1,<=…,<=*s**K*: the towns Borna has to visit. - 1<=≀<=*N*<=≀<=105 - 1<=≀<=*K*<=≀<=106 - 1<=≀<=*a*,<=*b*<=≀<=*N* for all roads - for all roads - 1<=≀<=*s**i*<=≀<=*N* for all 1<=≀<=*i*<=≀<=*K*
The output should contain a single number: the least amount of thousands of Ruritanian dinars Borna should allocate for bribes, modulo 109<=+<=7.
[ "5\n1 2 0\n2 3 0\n5 1 1\n3 4 1\n5\n5 4 5 2 2\n" ]
[ "4\n" ]
Borna first takes the route 1 → 5 and has to pay 1000 dinars. After that, he takes the route 5 → 1 → 2 → 3 → 4 and pays nothing this time. However, when he has to return via 4 → 3 → 2 → 1 → 5, he needs to prepare 3000 (1000+2000) dinars. Afterwards, getting to 2 via 5 → 1 → 2 will cost him nothing. Finally, he doesn't even have to leave town 2 to get to 2, so there is no need to prepare any additional bribe money. Hence he has to prepare 4000 dinars in total.
[]
30
0
0
58,090
723
st-Spanning Tree
[ "dsu", "graphs", "greedy", "implementation" ]
null
null
You are given an undirected connected graph consisting of *n* vertices and *m* edges. There are no loops and no multiple edges in the graph. You are also given two distinct vertices *s* and *t*, and two values *d**s* and *d**t*. Your task is to build any spanning tree of the given graph (note that the graph is not weighted), such that the degree of the vertex *s* doesn't exceed *d**s*, and the degree of the vertex *t* doesn't exceed *d**t*, or determine, that there is no such spanning tree. The spanning tree of the graph *G* is a subgraph which is a tree and contains all vertices of the graph *G*. In other words, it is a connected graph which contains *n*<=-<=1 edges and can be obtained by removing some of the edges from *G*. The degree of a vertex is the number of edges incident to this vertex.
The first line of the input contains two integers *n* and *m* (2<=≀<=*n*<=≀<=200<=000, 1<=≀<=*m*<=≀<=*min*(400<=000,<=*n*Β·(*n*<=-<=1)<=/<=2))Β β€” the number of vertices and the number of edges in the graph. The next *m* lines contain the descriptions of the graph's edges. Each of the lines contains two integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*n*, *u*<=β‰ <=*v*)Β β€” the ends of the corresponding edge. It is guaranteed that the graph contains no loops and no multiple edges and that it is connected. The last line contains four integers *s*, *t*, *d**s*, *d**t* (1<=≀<=*s*,<=*t*<=≀<=*n*, *s*<=β‰ <=*t*, 1<=≀<=*d**s*,<=*d**t*<=≀<=*n*<=-<=1).
If the answer doesn't exist print "No" (without quotes) in the only line of the output. Otherwise, in the first line print "Yes" (without quotes). In the each of the next (*n*<=-<=1) lines print two integers β€” the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed exactly once. You can output edges in any order. You can output the ends of each edge in any order. If there are several solutions, print any of them.
[ "3 3\n1 2\n2 3\n3 1\n1 2 1 1\n", "7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4\n" ]
[ "Yes\n3 2\n1 3\n", "Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1\n" ]
none
[ { "input": "3 3\n1 2\n2 3\n3 1\n1 2 1 1", "output": "Yes\n3 2\n1 3" }, { "input": "7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4", "output": "Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1" }, { "input": "10 15\n4 1\n5 10\n2 1\n5 7\n9 2\n4 6\n6 7\n9 1\n6 9\n8 4\n8 3\n9 8\n3 9\n2 3\n7 10\n10 1 2 3", "output": "Yes\n5 7\n9 2\n4 6\n6 7\n6 9\n8 4\n8 3\n7 10\n1 2" }, { "input": "10 10\n1 3\n10 3\n6 1\n2 7\n1 7\n1 9\n9 5\n2 10\n10 8\n4 3\n9 5 2 4", "output": "Yes\n1 3\n10 3\n6 1\n2 7\n1 7\n10 8\n4 3\n1 9\n9 5" }, { "input": "4 4\n1 4\n4 2\n1 3\n4 3\n3 4 3 3", "output": "Yes\n2 4\n1 3\n3 4" }, { "input": "3 2\n3 1\n2 3\n1 2 1 1", "output": "Yes\n3 2\n1 3" }, { "input": "2 1\n1 2\n1 2 1 1", "output": "Yes\n1 2" }, { "input": "3 3\n2 3\n1 2\n1 3\n2 3 1 1", "output": "Yes\n1 3\n2 1" }, { "input": "2 1\n2 1\n1 2 1 1", "output": "Yes\n1 2" }, { "input": "3 3\n3 1\n3 2\n2 1\n1 3 1 1", "output": "Yes\n2 3\n1 2" }, { "input": "2 1\n2 1\n2 1 1 1", "output": "Yes\n2 1" }, { "input": "5 6\n4 1\n3 2\n1 2\n1 3\n5 1\n3 4\n5 4 1 1", "output": "Yes\n3 2\n1 2\n3 4\n5 1" }, { "input": "44 63\n19 38\n36 18\n3 11\n30 44\n24 31\n1 8\n28 38\n38 30\n14 24\n36 34\n32 38\n39 34\n17 38\n35 22\n5 43\n20 10\n22 10\n9 39\n42 22\n20 32\n14 6\n38 42\n40 38\n1 7\n36 15\n3 37\n36 10\n36 33\n12 38\n33 23\n10 43\n26 36\n35 44\n16 20\n9 36\n3 36\n1 2\n36 27\n21 36\n36 13\n25 37\n29 30\n36 31\n5 28\n31 20\n35 41\n16 36\n36 25\n8 14\n17 39\n42 28\n34 5\n41 38\n1 36\n15 24\n3 16\n14 1\n44 23\n6 26\n30 4\n30 23\n38 36\n5 11\n25 4 12 10", "output": "Yes\n19 38\n36 18\n3 11\n30 44\n24 31\n1 8\n28 38\n38 30\n14 24\n36 34\n32 38\n39 34\n17 38\n35 22\n5 43\n20 10\n22 10\n9 39\n42 22\n20 32\n14 6\n40 38\n1 7\n36 15\n3 37\n36 10\n36 33\n12 38\n33 23\n10 43\n26 36\n16 20\n3 36\n1 2\n36 27\n21 36\n36 13\n29 30\n36 31\n35 41\n8 14\n37 25\n4 30" }, { "input": "10 16\n6 1\n1 10\n6 5\n5 7\n5 2\n4 1\n1 3\n5 8\n1 8\n1 7\n4 5\n9 1\n10 5\n9 5\n3 5\n1 2\n1 5 4 5", "output": "Yes\n2 1\n3 1\n4 1\n6 5\n7 5\n8 5\n9 5\n10 5\n1 6" }, { "input": "2 1\n1 2\n1 2 1 1", "output": "Yes\n1 2" }, { "input": "2 1\n2 1\n1 2 1 1", "output": "Yes\n1 2" }, { "input": "3 2\n2 1\n3 1\n1 2 1 1", "output": "No" }, { "input": "3 2\n2 1\n3 1\n1 2 2 1", "output": "Yes\n3 1\n1 2" }, { "input": "3 2\n2 1\n3 1\n1 2 1 2", "output": "No" }, { "input": "3 2\n2 1\n3 1\n3 2 1 1", "output": "Yes\n1 2\n3 1" }, { "input": "10 15\n3 2\n9 3\n9 4\n7 3\n9 1\n5 9\n9 8\n7 5\n5 2\n4 3\n6 8\n7 9\n9 10\n9 6\n5 6\n5 9 1 3", "output": "No" }, { "input": "6 11\n1 5\n4 2\n5 2\n6 2\n4 6\n6 1\n3 5\n5 6\n1 2\n1 4\n1 3\n5 1 1 1", "output": "No" }, { "input": "5 6\n3 1\n5 1\n3 5\n4 5\n1 2\n3 2\n1 3 1 1", "output": "No" }, { "input": "15 15\n6 2\n7 5\n14 11\n8 10\n13 10\n10 9\n3 14\n9 6\n3 1\n5 6\n1 15\n2 12\n11 4\n2 3\n11 9\n9 2 2 2", "output": "No" }, { "input": "5 5\n1 3\n1 4\n1 5\n2 3\n2 4\n1 2 2 2", "output": "Yes\n5 1\n3 2\n4 2\n1 3" }, { "input": "23 32\n20 22\n17 21\n22 5\n11 13\n14 22\n1 16\n6 3\n8 5\n16 10\n10 22\n7 17\n21 1\n12 1\n14 19\n17 19\n12 17\n15 18\n9 19\n11 23\n12 2\n1 17\n14 4\n13 9\n23 1\n11 20\n20 16\n2 6\n7 23\n16 6\n18 14\n23 10\n5 3\n12 6 9 9", "output": "Yes\n20 22\n17 21\n22 5\n11 13\n14 22\n1 16\n8 5\n16 10\n10 22\n7 17\n21 1\n14 19\n15 18\n9 19\n11 23\n14 4\n13 9\n18 14\n5 3\n2 12\n17 12\n6 2" }, { "input": "14 18\n6 9\n9 7\n6 10\n14 9\n5 9\n9 3\n3 1\n2 13\n13 8\n1 8\n11 3\n10 5\n12 5\n5 8\n13 7\n4 11\n2 5\n9 11\n8 3 2 2", "output": "Yes\n6 9\n9 7\n6 10\n14 9\n5 9\n2 13\n12 5\n13 7\n4 11\n9 11\n1 8\n11 3\n8 5" }, { "input": "5 7\n1 3\n4 2\n5 1\n4 3\n2 3\n4 5\n1 2\n1 4 2 2", "output": "Yes\n2 3\n3 1\n5 4\n1 5" }, { "input": "24 34\n21 22\n10 6\n22 5\n16 9\n18 11\n11 6\n24 6\n6 1\n18 2\n19 1\n21 14\n16 24\n13 18\n6 20\n17 18\n3 2\n21 3\n3 10\n14 16\n3 23\n6 13\n8 20\n1 9\n16 22\n22 1\n13 17\n12 4\n6 4\n5 3\n9 13\n23 15\n21 6\n21 7\n6 19\n4 6 2 3", "output": "Yes\n21 22\n22 5\n16 9\n18 11\n18 2\n19 1\n21 14\n16 24\n13 18\n17 18\n3 2\n21 3\n3 10\n14 16\n3 23\n8 20\n1 9\n23 15\n21 7\n24 6\n12 4\n20 6\n4 6" }, { "input": "27 30\n17 27\n14 24\n1 17\n20 15\n15 21\n16 19\n6 13\n12 20\n3 16\n10 25\n7 26\n12 25\n17 26\n24 17\n2 6\n22 27\n22 9\n5 16\n3 25\n20 11\n23 24\n26 22\n11 13\n20 8\n17 11\n6 4\n22 25\n26 18\n9 6\n3 11\n19 16 5 20", "output": "Yes\n17 27\n14 24\n1 17\n20 15\n15 21\n6 13\n12 20\n10 25\n7 26\n12 25\n17 26\n24 17\n2 6\n22 27\n22 9\n3 25\n20 11\n23 24\n11 13\n20 8\n17 11\n6 4\n26 18\n5 16\n3 16\n19 16" }, { "input": "30 32\n17 28\n7 28\n8 3\n15 25\n30 18\n2 12\n7 23\n15 29\n8 14\n7 24\n13 2\n9 1\n26 29\n9 21\n18 13\n22 6\n9 15\n23 30\n17 26\n9 26\n17 16\n5 2\n7 8\n7 5\n17 20\n29 10\n21 4\n5 19\n5 11\n29 6\n24 1\n24 27\n7 27 4 9", "output": "Yes\n17 28\n8 3\n15 25\n30 18\n2 12\n15 29\n8 14\n13 2\n9 1\n26 29\n9 21\n18 13\n22 6\n9 15\n23 30\n17 26\n17 16\n5 2\n17 20\n29 10\n21 4\n5 19\n5 11\n29 6\n24 1\n23 7\n8 7\n28 7\n27 24" }, { "input": "17 27\n12 11\n5 12\n12 17\n7 14\n4 14\n17 8\n7 9\n15 13\n4 3\n13 17\n11 5\n15 8\n14 17\n8 13\n16 7\n8 3\n1 6\n8 7\n12 9\n7 11\n14 15\n3 7\n8 4\n7 2\n8 14\n14 10\n15 1\n15 6 2 3", "output": "Yes\n12 11\n5 12\n12 17\n7 14\n4 14\n17 8\n7 9\n4 3\n13 17\n14 17\n16 7\n7 2\n14 10\n14 15\n1 6\n15 1" } ]
30
0
0
58,109
67
Save the City!
[ "geometry" ]
E. Save the City!
1
256
In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away from the uncorrupted part, the crimes could be stopped. So, they are trying to set up a compound where they can keep the corrupted people. To ensure that the criminals don't escape the compound, a watchtower needs to be set up, so that they can be watched. Since the people of Aalam-Aara aren't very rich, they met up with a merchant from some rich town who agreed to sell them a land-plot which has already a straight line fence *AB* along which a few points are set up where they can put up a watchtower. Your task is to help them find out the number of points on that fence where the tower can be put up, so that all the criminals can be watched from there. Only one watchtower can be set up. A criminal is watchable from the watchtower if the line of visibility from the watchtower to him doesn't cross the plot-edges at any point between him and the tower i.e. as shown in figure 1 below, points *X*, *Y*, *C* and *A* are visible from point *B* but the points *E* and *D* are not. Assume that the land plot is in the shape of a polygon and coordinate axes have been setup such that the fence *AB* is parallel to *x*-axis and the points where the watchtower can be set up are the integer points on the line. For example, in given figure 2, watchtower can be setup on any of five integer points on *AB* i.e. (4,<=8), (5,<=8), (6,<=8), (7,<=8) or (8,<=8). You can assume that no three consecutive points are collinear and all the corner points other than *A* and *B*, lie towards same side of fence *AB*. The given polygon doesn't contain self-intersections.
The first line of the test case will consist of the number of vertices *n* (3<=≀<=*n*<=≀<=1000). Next *n* lines will contain the coordinates of the vertices in the clockwise order of the polygon. On the *i*-th line are integers *x**i* and *y**i* (0<=≀<=*x**i*,<=*y**i*<=≀<=106) separated by a space. The endpoints of the fence *AB* are the first two points, (*x*1,<=*y*1) and (*x*2,<=*y*2).
Output consists of a single line containing the number of points where the watchtower can be set up.
[ "5\n4 8\n8 8\n9 4\n4 0\n0 4\n", "5\n4 8\n5 8\n5 4\n7 4\n2 2\n" ]
[ "5\n", "0\n" ]
Figure 2 shows the first test case. All the points in the figure are watchable from any point on fence *AB*. Since, *AB* has 5 integer coordinates, so answer is 5. For case two, fence *CD* and *DE* are not completely visible, thus answer is 0.
[]
92
0
0
58,154
82
Two out of Three
[ "dp" ]
D. Two out of Three
2
256
Vasya has recently developed a new algorithm to optimize the reception of customer flow and he considered the following problem. Let the queue to the cashier contain *n* people, at that each of them is characterized by a positive integer *a**i* β€” that is the time needed to work with this customer. What is special about this very cashier is that it can serve two customers simultaneously. However, if two customers need *a**i* and *a**j* of time to be served, the time needed to work with both of them customers is equal to *max*(*a**i*,<=*a**j*). Please note that working with customers is an uninterruptable process, and therefore, if two people simultaneously come to the cashier, it means that they begin to be served simultaneously, and will both finish simultaneously (it is possible that one of them will have to wait). Vasya used in his algorithm an ingenious heuristic β€” as long as the queue has more than one person waiting, then some two people of the first three standing in front of the queue are sent simultaneously. If the queue has only one customer number *i*, then he goes to the cashier, and is served within *a**i* of time. Note that the total number of phases of serving a customer will always be equal to ⌈*n*<=/<=2βŒ‰. Vasya thinks that this method will help to cope with the queues we all hate. That's why he asked you to work out a program that will determine the minimum time during which the whole queue will be served using this algorithm.
The first line of the input file contains a single number *n* (1<=≀<=*n*<=≀<=1000), which is the number of people in the sequence. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=106). The people are numbered starting from the cashier to the end of the queue.
Print on the first line a single number β€” the minimum time needed to process all *n* people. Then on ⌈*n*<=/<=2βŒ‰ lines print the order in which customers will be served. Each line (probably, except for the last one) must contain two numbers separated by a space β€” the numbers of customers who will be served at the current stage of processing. If *n* is odd, then the last line must contain a single number β€” the number of the last served customer in the queue. The customers are numbered starting from 1.
[ "4\n1 2 3 4\n", "5\n2 4 3 1 4\n" ]
[ "6\n1 2\n3 4\n", "8\n1 3\n2 5\n4\n" ]
none
[ { "input": "4\n1 2 3 4", "output": "6\n1 2\n3 4" }, { "input": "5\n2 4 3 1 4", "output": "8\n1 3\n2 5\n4" }, { "input": "1\n10", "output": "10\n1" }, { "input": "2\n3 5", "output": "5\n1 2" }, { "input": "3\n1 10 1", "output": "11\n1 2\n3" }, { "input": "5\n4 5 4 5 5", "output": "14\n1 2\n4 5\n3" }, { "input": "7\n100 1 2 3 4 3 2", "output": "107\n2 3\n1 5\n4 6\n7" }, { "input": "8\n100 1 2 3 1000 3 2 1", "output": "1007\n2 3\n1 5\n4 6\n7 8" }, { "input": "10\n9 4 4 6 10 4 4 10 7 8", "output": "35\n2 3\n1 4\n6 7\n5 8\n9 10" }, { "input": "15\n6 6 6 6 6 6 6 5 5 6 6 6 6 5 6", "output": "46\n1 2\n3 4\n5 6\n8 9\n7 10\n11 12\n13 15\n14" }, { "input": "15\n9 9 4 2 4 6 5 7 1 7 4 3 8 4 1", "output": "41\n1 2\n3 5\n6 7\n4 9\n8 10\n11 12\n13 14\n15" }, { "input": "15\n8257 5015 8429 1425 8674 7051 7808 5562 9581 5867 5033 6170 5217 5283 3452", "output": "51300\n1 3\n2 4\n6 7\n5 9\n8 11\n10 12\n13 14\n15" }, { "input": "25\n45 59 47 19 54 29 31 36 55 58 47 37 18 7 23 47 59 20 53 6 37 55 10 54 24", "output": "518\n1 3\n2 5\n4 6\n7 8\n9 10\n11 12\n13 14\n16 17\n15 18\n19 21\n20 23\n22 24\n25" }, { "input": "25\n88 71 33 8 68 76 2 65 22 32 45 71 14 85 65 30 78 47 70 18 32 90 84 15 90", "output": "731\n1 2\n3 5\n4 7\n6 8\n10 11\n9 13\n12 15\n14 17\n16 18\n20 21\n19 23\n22 25\n24" }, { "input": "25\n8432 7540 8635 101 3810 1892 8633 1640 8440 2096 7974 5359 9912 6850 2898 5542 4535 344 1000 7041 7018 7573 6952 8521 6266", "output": "77477\n1 3\n2 5\n4 6\n7 9\n8 10\n11 13\n12 14\n16 17\n15 19\n20 21\n22 23\n24 25\n18" }, { "input": "25\n58 59 59 59 52 54 58 55 59 58 54 58 58 59 56 52 50 59 51 57 60 51 58 58 57", "output": "737\n1 2\n3 4\n5 6\n7 9\n8 11\n10 12\n13 14\n15 16\n17 19\n18 21\n20 23\n24 25\n22" }, { "input": "55\n28 35 54 56 32 31 26 36 46 27 42 48 59 24 58 25 56 41 36 57 51 43 59 44 43 23 51 30 37 57 41 40 27 51 56 23 28 41 51 24 25 29 24 47 32 49 21 22 36 28 34 29 39 56 23", "output": "1135\n1 2\n3 4\n5 6\n8 9\n7 10\n11 12\n13 15\n14 16\n18 19\n17 21\n20 23\n22 25\n24 27\n26 28\n29 31\n32 33\n30 35\n36 37\n34 39\n40 41\n42 43\n38 45\n44 46\n47 48\n49 51\n50 52\n53 54\n55" }, { "input": "55\n33 64 51 38 37 44 63 89 60 90 33 67 82 81 40 41 58 86 47 78 60 86 81 43 57 89 39 72 59 88 47 41 59 79 35 41 67 57 70 55 90 46 37 64 78 55 82 88 81 60 46 75 34 79 51", "output": "1814\n2 3\n1 5\n4 6\n7 9\n8 10\n11 12\n13 14\n15 16\n17 19\n18 20\n22 23\n21 25\n24 27\n28 29\n26 30\n31 32\n33 34\n35 36\n37 39\n38 40\n42 43\n44 45\n41 47\n48 49\n46 50\n51 53\n52 54\n55" }, { "input": "55\n9756 9901 6482 8013 6684 3583 1777 4593 7432 3389 3887 1139 7086 6908 3715 1810 8360 2029 7149 3548 5049 5650 6870 1247 6993 1527 7986 9839 3271 9201 8331 5640 7205 1179 7274 1136 8358 9420 5380 2415 1599 5295 5541 8297 3277 3817 6220 9655 3180 9467 1613 4500 5821 4902 2138", "output": "162951\n1 2\n3 5\n6 7\n4 9\n8 11\n10 12\n13 14\n15 16\n17 19\n18 20\n21 22\n23 25\n24 26\n27 28\n30 31\n29 32\n33 35\n34 36\n37 38\n40 41\n39 43\n42 45\n44 47\n46 49\n48 50\n52 53\n54 55\n51" }, { "input": "55\n56 5023 3523 1042 1540 1549 3830 4597 3736 181 4642 260 1695 1766 1567 1984 973 1035 687 1556 1569 1703 3869 3004 1766 2624 3145 4414 1876 4203 1491 899 3172 3843 2988 3858 2768 2967 998 2219 5364 4699 2869 2978 1297 5652 3992 5147 1995 1992 350 3597 2328 5679 371", "output": "78896\n2 3\n1 4\n5 6\n7 9\n8 11\n10 12\n13 15\n14 16\n17 19\n18 20\n21 22\n23 24\n26 27\n25 29\n28 30\n31 32\n33 35\n34 36\n37 38\n39 40\n41 42\n43 44\n46 47\n45 49\n50 51\n52 53\n48 54\n55" }, { "input": "100\n1525 7246 2782 4070 7539 5459 8162 8074 5197 4808 4599 8161 2110 2216 7434 9937 2350 9406 9497 8027 2100 1600 7508 3441 4356 3928 8448 3788 7189 9186 7923 5170 6940 6986 9909 8418 2963 9146 8411 6136 6753 3006 2726 4290 7310 8044 3558 6136 4781 1765 4242 5433 9937 8768 5103 4470 1000 5480 4834 3372 8877 2388 3601 8731 4873 3455 1121 2727 5053 8367 8677 2326 6646 8178 8722 7911 6188 8800 5693 1546 1690 5921 7368 3688 6250 7749 3073 2254 4794 4291 7590 2647 6187 5988 1729 6855 3936 5016 4874 5675", "output": "299505\n1 3\n2 5\n4 6\n7 8\n9 10\n11 13\n12 15\n14 17\n16 19\n18 20\n21 22\n24 25\n23 27\n26 28\n29 31\n32 33\n30 35\n34 36\n38 39\n40 41\n37 43\n42 44\n45 46\n48 49\n47 50\n51 52\n53 54\n55 56\n58 59\n57 60\n62 63\n61 64\n66 67\n65 69\n70 71\n68 72\n74 75\n73 77\n76 78\n80 81\n79 82\n84 85\n83 86\n87 88\n89 90\n91 93\n92 95\n94 96\n97 99\n98 100" }, { "input": "7\n10 1 1 10 1 1 10", "output": "22\n1 2\n3 5\n4 7\n6" } ]
62
0
0
58,428
283
Cow Program
[ "dfs and similar", "dp", "graphs" ]
null
null
Farmer John has just given the cows a program to play with! The program contains two integer variables, *x* and *y*, and performs the following operations on a sequence *a*1,<=*a*2,<=...,<=*a**n* of positive integers: 1. Initially, *x*<==<=1 and *y*<==<=0. If, after any step, *x*<=≀<=0 or *x*<=&gt;<=*n*, the program immediately terminates. 1. The program increases both *x* and *y* by a value equal to *a**x* simultaneously. 1. The program now increases *y* by *a**x* while decreasing *x* by *a**x*. 1. The program executes steps 2 and 3 (first step 2, then step 3) repeatedly until it terminates (it may never terminate). So, the sequence of executed steps may start with: step 2, step 3, step 2, step 3, step 2 and so on. The cows are not very good at arithmetic though, and they want to see how the program works. Please help them! You are given the sequence *a*2,<=*a*3,<=...,<=*a**n*. Suppose for each *i* (1<=≀<=*i*<=≀<=*n*<=-<=1) we run the program on the sequence *i*,<=*a*2,<=*a*3,<=...,<=*a**n*. For each such run output the final value of *y* if the program terminates or -1 if it does not terminate.
The first line contains a single integer, *n* (2<=≀<=*n*<=≀<=2Β·105). The next line contains *n*<=-<=1 space separated integers, *a*2,<=*a*3,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109).
Output *n*<=-<=1 lines. On the *i*-th line, print the requested value when the program is run on the sequence *i*,<=*a*2,<=*a*3,<=...*a**n*. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "4\n2 4 1\n", "3\n1 2\n" ]
[ "3\n6\n8\n", "-1\n-1\n" ]
In the first sample 1. For *i* = 1,  *x* becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d87035805e6f1c669693d86603da6d89c5730833.png" style="max-width: 100.0%;max-height: 100.0%;"/> and *y* becomes 1 + 2 = 3. 1. For *i* = 2,  *x* becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c59515a288e6cb9876590f520c031cb27e346525.png" style="max-width: 100.0%;max-height: 100.0%;"/> and *y* becomes 2 + 4 = 6. 1. For *i* = 3,  *x* becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/62b52a6cff2221e154a9c36f7bb277ad6475a1b6.png" style="max-width: 100.0%;max-height: 100.0%;"/> and *y* becomes 3 + 1 + 4 = 8.
[ { "input": "4\n2 4 1", "output": "3\n6\n8" }, { "input": "3\n1 2", "output": "-1\n-1" }, { "input": "5\n2 2 1 3", "output": "3\n-1\n-1\n-1" }, { "input": "2\n1", "output": "-1" }, { "input": "8\n7 6 2 6 2 6 6", "output": "8\n8\n12\n10\n-1\n-1\n20" }, { "input": "8\n4 5 3 2 3 3 3", "output": "5\n7\n-1\n-1\n-1\n-1\n-1" }, { "input": "3\n1 1", "output": "-1\n-1" }, { "input": "5\n3 2 4 2", "output": "4\n-1\n7\n-1" }, { "input": "92\n79 52 17 45 47 64 48 49 650617238 32 9 74 12 80 39 41 73 22 25 73 79 51 85 21 3 56 255371563 2 986959075 17 30 70 577324422 84 7 39 85 18 6 63 44 52 37 5 36 9 12 34 9 60 56 1 491072951 57 7 91 76 88 50 59 6 5 27 80 79279147 67 340148613 82 13 12520473 23 23 39 44 69 83 38 46 26 75 44 30 65 76 56 7 6 2 9 804681590 37", "output": "80\n54\n20\n49\n52\n70\n55\n57\n650617247\n42\n129\n86\n283\n94\n54\n57\n90\n40\n44\n93\n100\n73\n108\n135\n202\n82\n255371590\n491073068\n986959104\n236\n319\n102\n577324455\n118\n250\n75\n122\n129\n577324467\n103\n85\n94\n12520617\n650617329\n650617319\n140\n118\n162\n121\n110\n107\n109\n491073004\n111\n491073078\n147\n133\n146\n650617347\n377\n155\n143\n12520735\n144\n79279212\n133\n340148680\n150\n173\n12520543\n491073110\n192\n196\n206\n12520681\n159\n650617395\n194\n491073056\n259\n210\n491073064\n23..." }, { "input": "98\n94 24 17 92 275858941 58 91 57 13 468038892 42 195790073 494005784 8 468106970 518962936 33 27 61 72 42 206673418 10 82 23 34 29 77 90 39 9 67 34 71 29 95 49 48 60 69 86 64 94 77 48 74 19 96700186 5 67 881058074 663483223 64 64 78 23 8 60 7 17 96 71 70 20 5 63 35 34 63 30 86 76 32 86 11 6 96 10 4 37891677 63 58 74 36 20 48 44 93 97 568562143 850624643 55 48 63 59 55 46", "output": "95\n26\n20\n96\n275858946\n64\n98\n65\n22\n468038902\n53\n195790085\n494005797\n177\n468106985\n518962952\n50\n45\n80\n92\n63\n206673440\n494005817\n106\n106\n60\n56\n105\n119\n69\n206673458\n99\n67\n105\n219\n131\n86\n86\n99\n109\n127\n106\n137\n121\n93\n120\n143\n96700234\n131\n117\n881058125\n663483275\n117\n118\n133\n215\n134\n118\n663483289\n171\n157\n133\n133\n161\n183\n218\n169\n173\n287\n169\n157\n148\n191\n160\n96700328\n112\n173\n122\n-1\n37891757\n219\n222\n206673588\n96700306\n209\n-1\n225\n181..." }, { "input": "98\n19 32 32 78 52 65 57 90 865825369 956483278 1 44 77 14 72 31 3 92 62 9 20 70 6 73 92 94 47 444654052 31 21298850 68 86 65 23 86 11 72 96 16 61 44 17 83 2 32 90 21 59 95 84 69 35 85 46 82 81 73 49 5 12 73 2 90 87 57 70 21 35 75 13 18 7 28 960620421 31 95865681 36 95 77 26 49 78 36 42 9 65 37 78 904133698 88 55 65 968490755 672903800 47 7 21", "output": "20\n34\n35\n82\n57\n71\n64\n98\n865825378\n956483288\n956483290\n56\n90\n-1\n87\n47\n444654086\n110\n81\n74\n114\n92\n86\n97\n117\n120\n74\n444654080\n60\n21298880\n99\n118\n98\n102\n121\n139\n109\n134\n92\n101\n85\n151\n126\n114\n342\n136\n162\n107\n144\n134\n120\n144\n138\n190\n137\n137\n130\n865825476\n110\n131\n134\n140\n153\n151\n212\n136\n178\n168\n144\n156\n174\n136\n95865903\n960620495\n198\n95865757\n95865847\n173\n256\n152\n216\n-1\n240\n194\n95865775\n215\n208\n956483444\n904133787\n278\n272\n96..." }, { "input": "98\n54 88 79 67 72 6 44 71 40 1 76 14 74 8 12 88 36 72 94 97 65 19 95 81 19 22 60 1 20 438323030 97 27 166869403 230316676 482602003 72 47 52 87 48 2 50 28 55 47 25 22 44 40 22 53 41 92 47 1 56 76 82 39 74 85 61 80 52 91 95 55 90 72 27 11 69 59 66 681086671 33 798374266 33 84 768636470 31 68 47 83 14 81 337200269 49 40 8 91 44 48 97 18 26 9", "output": "55\n90\n82\n71\n77\n-1\n51\n79\n49\n-1\n87\n26\n87\n42\n-1\n104\n53\n90\n113\n117\n86\n-1\n118\n105\n64\n200\n87\n337200358\n-1\n438323060\n128\n798374397\n166869436\n230316710\n482602038\n108\n84\n90\n126\n88\n130\n92\n143\n99\n92\n798374555\n143\n244\n-1\n-1\n104\n337200438\n145\n198\n103\n-1\n133\n140\n195\n134\n146\n225\n143\n304\n156\n161\n310\n158\n141\n210\n156\n-1\n-1\n295\n681086746\n222\n798374343\n-1\n163\n768636550\n-1\n-1\n202\n269\n-1\n798374505\n337200356\n224\n-1\n166\n-1\n-1\n-1\n191\n7983..." }, { "input": "10\n6 7 5 3 1 5 2 4 6", "output": "7\n9\n8\n-1\n-1\n-1\n-1\n-1\n-1" }, { "input": "8\n6 311942309 3 1 3 2 2", "output": "7\n311942311\n-1\n311942323\n311942317\n311942321\n12" }, { "input": "8\n2 3 1 2 2 3 3", "output": "3\n5\n-1\n-1\n-1\n-1\n-1" }, { "input": "6\n2 1 2 2 3", "output": "3\n-1\n-1\n-1\n-1" }, { "input": "23\n20 1 3 3 13 11 9 7 5 3 1 7 2 4 6 8 10 12 14 16 12 5", "output": "21\n-1\n-1\n-1\n18\n17\n16\n-1\n26\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n48\n-1\n37" }, { "input": "71\n28 11 39 275858941 64 69 66 18 468038892 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 25 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 701366631 51 25 11 11 49 33 67 43 57", "output": "29\n13\n42\n275858945\n69\n75\n73\n26\n468038901\n59\n58\n57\n56\n55\n54\n53\n52\n51\n50\n49\n48\n47\n-1\n-1\n113\n468038935\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n701366692\n-1\n-1\n111\n114\n-1\n-1\n-1\n-1\n-1" }, { "input": "23\n11 6 21 9 13 11 9 7 5 3 1 8 2 4 6 8 10 12 14 935874687 21 1", "output": "12\n8\n24\n13\n18\n17\n16\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n935874707\n-1\n44" }, { "input": "71\n2 50 62 41 50 16 65 6 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 26 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 14 6 67 54 54 620768469 637608010 27 54 18 49", "output": "3\n52\n65\n45\n55\n22\n72\n801\n58\n57\n56\n55\n54\n53\n52\n51\n50\n49\n48\n47\n46\n45\n831\n1067\n87\n1147\n891\n671\n487\n339\n227\n151\n111\n105\n109\n117\n129\n145\n165\n189\n217\n249\n285\n325\n369\n417\n469\n525\n585\n649\n717\n789\n865\n945\n1029\n1117\n1209\n1305\n1405\n543\n109\n129\n1413\n1317\n620768534\n637608076\n843\n973\n121\n515" } ]
466
29,696,000
3
58,492
732
Sanatorium
[ "binary search", "constructive algorithms", "greedy", "implementation", "math" ]
null
null
Vasiliy spent his vacation in a sanatorium, came back and found that he completely forgot details of his vacation! Every 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. Vasiliy 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. According 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.
The only line contains three integers *b*, *d* and *s* (0<=≀<=*b*,<=*d*,<=*s*<=≀<=1018,<=<=*b*<=+<=*d*<=+<=*s*<=β‰₯<=1)Β β€” the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium.
Print single integerΒ β€” the minimum possible number of meals which Vasiliy could have missed during his vacation.
[ "3 2 1\n", "1 0 0\n", "1 1 1\n", "1000000000000000000 0 1000000000000000000\n" ]
[ "1\n", "0\n", "0\n", "999999999999999999\n" ]
In 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. In the second sample, Vasiliy could have arrived before breakfast, have had it, and immediately have left the sanatorium, not missing any meal. In the third sample, Vasiliy could have been in the sanatorium for one day, not missing any meal.
[ { "input": "3 2 1", "output": "1" }, { "input": "1 0 0", "output": "0" }, { "input": "1 1 1", "output": "0" }, { "input": "1000000000000000000 0 1000000000000000000", "output": "999999999999999999" }, { "input": "1000 0 0", "output": "1998" }, { "input": "0 1 0", "output": "0" }, { "input": "0 0 1", "output": "0" }, { "input": "1 1 0", "output": "0" }, { "input": "0 1 1", "output": "0" }, { "input": "1000000000000000000 999999999999999999 999999999999999999", "output": "0" }, { "input": "1000000000000000000 1000000000000000000 999999999999999999", "output": "0" }, { "input": "1000000000000000000 1000000000000000000 1000000000000000000", "output": "0" }, { "input": "999999999999999999 999999999999999999 999999999999999999", "output": "0" }, { "input": "999999999999999999 1000000000000000000 999999999999999999", "output": "0" }, { "input": "999999999999999999 1000000000000000000 1000000000000000000", "output": "0" }, { "input": "999999999999999999 999999999999999998 999999999999999999", "output": "0" }, { "input": "999999999999999999 999999999999999999 1000000000000000000", "output": "0" }, { "input": "10 10 10", "output": "0" }, { "input": "9 11 11", "output": "1" }, { "input": "10 8 9", "output": "1" }, { "input": "12 8 8", "output": "6" }, { "input": "13 18 14", "output": "7" }, { "input": "94 87 14", "output": "85" }, { "input": "35 91 108", "output": "88" }, { "input": "1671 24 419", "output": "2897" }, { "input": "7759 10296 4966", "output": "7865" }, { "input": "8912 10561 8205", "output": "4003" }, { "input": "1000000100 1000000083 1000000047", "output": "68" }, { "input": "999996782 1000007108 1000009860", "output": "15828" }, { "input": "999342691 1000075839 1000144842", "output": "871152" }, { "input": "951468821 1005782339 1063348170", "output": "169445178" }, { "input": "498556507 1820523034 1566999959", "output": "1575489600" }, { "input": "99999999999999938 99999999999999971 99999999999999944", "output": "58" }, { "input": "99999999999995757 100000000000000116 99999999999999158", "output": "5315" }, { "input": "100000000481729714 100000000353636209 99999999472937283", "output": "1136885934" }, { "input": "100000330161030627 99999005090484867 99999729994406004", "output": "1925237170381" }, { "input": "100040306518636197 100049788380618015 99377557930019414", "output": "681712312580417" }, { "input": "117864695669097197 53280311324979856 171825292362519668", "output": "172505577730962281" }, { "input": "64058874468711639 248004345115813505 265277488186534632", "output": "218491756788544118" }, { "input": "0 323940200031019351 1000000000000000000", "output": "1676059799968980647" }, { "input": "0 1000000000000000000 0", "output": "1999999999999999998" }, { "input": "4 3 5", "output": "1" }, { "input": "0 0 1000000000000000000", "output": "1999999999999999998" }, { "input": "1000000000000000000 0 0", "output": "1999999999999999998" }, { "input": "0 1000000000000000000 1000000000000000000", "output": "999999999999999999" }, { "input": "1000000000000000000 1000000000000000000 0", "output": "999999999999999999" }, { "input": "6436340231848 215 5328562123", "output": "12867351901356" }, { "input": "1 4352626266226 1", "output": "8705252532448" }, { "input": "647397084215 513125 15999899", "output": "1294777655404" }, { "input": "429 42 132", "output": "682" }, { "input": "1 3 1", "output": "2" }, { "input": "0 6 0", "output": "10" }, { "input": "100 100 99", "output": "0" }, { "input": "3 3 2", "output": "0" }, { "input": "10 10 9", "output": "0" }, { "input": "3 3 1", "output": "1" }, { "input": "100 99 100", "output": "0" }, { "input": "5 5 4", "output": "0" }, { "input": "50 70 80", "output": "38" }, { "input": "4 4 4", "output": "0" }, { "input": "4 4 3", "output": "0" }, { "input": "1 0 1", "output": "0" }, { "input": "0 0 2", "output": "2" }, { "input": "1 1 10", "output": "16" }, { "input": "5 2 3", "output": "3" }, { "input": "5 5 1", "output": "3" }, { "input": "10 200000 10", "output": "399978" }, { "input": "3 0 0", "output": "4" }, { "input": "100 100 100", "output": "0" }, { "input": "3 5 5", "output": "1" }, { "input": "3 4 3", "output": "0" }, { "input": "1000000000000000000 1 0", "output": "1999999999999999997" }, { "input": "2 0 0", "output": "2" }, { "input": "5 6 4", "output": "1" }, { "input": "0 2 2", "output": "1" }, { "input": "1000000000000000000 100000000000000000 0", "output": "1899999999999999998" }, { "input": "2 2 1", "output": "0" }, { "input": "1000000000000000000 1 1", "output": "1999999999999999996" }, { "input": "1 2 2", "output": "0" }, { "input": "1 1 5", "output": "6" }, { "input": "10 0 5", "output": "13" }, { "input": "10 10 0", "output": "9" }, { "input": "0 1000 0", "output": "1998" } ]
93
0
3
58,539
788
The Great Mixing
[ "dfs and similar", "graphs", "shortest paths" ]
null
null
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 . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . 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. Carbon 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. Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type.
The first line contains two integers *n*, *k* (0<=≀<=*n*<=≀<=1000, 1<=≀<=*k*<=≀<=106)Β β€” carbon dioxide concentration the friends want and the number of Coke types. The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (0<=≀<=*a**i*<=≀<=1000)Β β€” carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.
Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.
[ "400 4\n100 300 450 500\n", "50 2\n100 25\n" ]
[ "2\n", "3\n" ]
In the first sample case, we can achieve concentration <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bed0f5c3640139492194728ccc3ac55accf16a8e.png" style="max-width: 100.0%;max-height: 100.0%;"/> using one liter of Coke of types <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9b37ab6b0795f08ffcc699d9101a9efb89374478.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d82574f3d78c4bd9d8ab9bda103e05a51e1b3161.png" style="max-width: 100.0%;max-height: 100.0%;"/>: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b23f59a536403f9a2364e971aa0bfc9a3411b366.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the second case, we can achieve concentration <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/46aa9afb7ee4d932ca2c3f0d6535a9955fc8f0a8.png" style="max-width: 100.0%;max-height: 100.0%;"/> using two liters of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/69b8967d23533c2caada3910f564294509450a59.png" style="max-width: 100.0%;max-height: 100.0%;"/> type and one liter of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/abf5cbf9e8a81a0eff83ff53574dcabb097df44e.png" style="max-width: 100.0%;max-height: 100.0%;"/> type: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d2331fc733efc58d37745ff9a495a116ebd7e8a.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "400 4\n100 300 450 500", "output": "2" }, { "input": "50 2\n100 25", "output": "3" }, { "input": "500 3\n1000 5 5", "output": "199" }, { "input": "500 1\n1000", "output": "-1" }, { "input": "874 3\n873 974 875", "output": "2" }, { "input": "999 2\n1 1000", "output": "999" }, { "input": "326 18\n684 49 373 57 747 132 441 385 640 575 567 665 323 515 527 656 232 701", "output": "3" }, { "input": "314 15\n160 769 201 691 358 724 248 47 420 432 667 601 596 370 469", "output": "4" }, { "input": "0 1\n0", "output": "1" }, { "input": "0 1\n1000", "output": "-1" }, { "input": "345 5\n497 135 21 199 873", "output": "5" }, { "input": "641 8\n807 1000 98 794 536 845 407 331", "output": "7" }, { "input": "852 10\n668 1000 1000 1000 1000 1000 1000 639 213 1000", "output": "10" }, { "input": "710 7\n854 734 63 921 921 187 978", "output": "5" }, { "input": "134 6\n505 10 1 363 344 162", "output": "4" }, { "input": "951 15\n706 1000 987 974 974 706 792 792 974 1000 1000 987 974 953 953", "output": "6" }, { "input": "834 10\n921 995 1000 285 1000 166 1000 999 991 983", "output": "10" }, { "input": "917 21\n999 998 1000 997 1000 998 78 991 964 985 987 78 985 999 83 987 1000 999 999 78 83", "output": "12" }, { "input": "971 15\n692 1000 1000 997 1000 691 996 691 1000 1000 1000 692 1000 997 1000", "output": "11" }, { "input": "971 108\n706 706 991 706 988 997 996 997 991 996 706 706 996 706 996 984 1000 991 996 1000 724 724 997 991 997 984 997 1000 984 996 996 997 724 997 997 1000 997 724 984 997 996 988 997 706 706 997 1000 991 706 988 997 724 988 706 996 706 724 997 988 996 991 1000 1000 724 988 996 1000 988 984 996 991 724 706 988 991 724 1000 1000 991 984 984 706 724 706 988 724 984 984 991 988 991 706 997 984 984 1000 706 724 988 984 996 1000 988 997 984 724 991 991", "output": "10" }, { "input": "1000 16\n536 107 113 397 613 1 535 652 730 137 239 538 764 431 613 273", "output": "-1" }, { "input": "998 2\n1 1000", "output": "999" }, { "input": "998 3\n1 999 1000", "output": "500" }, { "input": "998 4\n1 2 999 1000", "output": "499" }, { "input": "500 2\n1000 2", "output": "499" }, { "input": "508 15\n0 998 997 1 1 2 997 1 997 1000 0 3 3 2 4", "output": "53" }, { "input": "492 2\n706 4", "output": "351" }, { "input": "672 5\n4 6 1000 995 997", "output": "46" }, { "input": "410 4\n998 8 990 990", "output": "54" }, { "input": "499 2\n1000 2", "output": "998" }, { "input": "995 5\n996 997 998 999 1000", "output": "-1" }, { "input": "500 3\n499 1000 300", "output": "7" }, { "input": "499 2\n0 1000", "output": "1000" }, { "input": "1000 10\n0 1 2 3 4 5 6 7 8 9", "output": "-1" }, { "input": "501 2\n1 1000", "output": "999" } ]
530
44,851,200
0
58,855
0
none
[ "none" ]
null
null
We'll call a set of positive integers *a* beautiful if the following condition fulfills: for any prime *p*, if , then . In other words, if one number from the set is divisible by prime *p*, then at least half of numbers from the set is divisible by *p*. Your task is to find any beautiful set, where the number of elements is equal to *k* and each element doesn't exceed 2*k*2.
The first line contains integer *k* (10<=≀<=*k*<=≀<=5000) that shows how many numbers the required beautiful set should have.
In the first line print *k* space-separated integers that are a beautiful set. If there are multiple such sets, you are allowed to print any of them.
[ "10\n" ]
[ "16 18 24 27 36 48 54 72 108 144 \n" ]
none
[]
46
0
0
59,010
41
3-cycles
[ "constructive algorithms", "graphs", "greedy" ]
E. 3-cycles
2
256
During a recent research Berland scientists found out that there were *n* cities in Ancient Berland, joined by two-way paths. Any two cities are joined by no more than one path. No path joins a city with itself. According to a well-known tradition, the road network was built so that it would be impossible to choose three cities from each of which one can get to any other one directly. That is, there was no cycle exactly as long as 3. Unfortunately, the road map has not been preserved till nowadays. Now the scientists are interested how much developed a country Ancient Berland was. Help them - find, what maximal number of roads could be in the country. You also have to restore any of the possible road maps.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of cities in Berland.
On the first line must be printed number *m* β€” the maximal number of roads in Berland. Then print *m* lines containing two numbers each β€” the numbers of cities that the given road joins. The cities are numbered with integers from 1 to *n*. If there are several variants of solving the problem, print any of them.
[ "3\n", "4\n" ]
[ "2\n1 2\n2 3\n", "4\n1 2\n2 3\n3 4\n4 1\n" ]
none
[ { "input": "3", "output": "2\n1 2\n1 3" }, { "input": "4", "output": "4\n1 3\n1 4\n2 3\n2 4" }, { "input": "5", "output": "6\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5" }, { "input": "6", "output": "9\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6" }, { "input": "7", "output": "12\n1 4\n1 5\n1 6\n1 7\n2 4\n2 5\n2 6\n2 7\n3 4\n3 5\n3 6\n3 7" }, { "input": "8", "output": "16\n1 5\n1 6\n1 7\n1 8\n2 5\n2 6\n2 7\n2 8\n3 5\n3 6\n3 7\n3 8\n4 5\n4 6\n4 7\n4 8" }, { "input": "9", "output": "20\n1 5\n1 6\n1 7\n1 8\n1 9\n2 5\n2 6\n2 7\n2 8\n2 9\n3 5\n3 6\n3 7\n3 8\n3 9\n4 5\n4 6\n4 7\n4 8\n4 9" }, { "input": "10", "output": "25\n1 6\n1 7\n1 8\n1 9\n1 10\n2 6\n2 7\n2 8\n2 9\n2 10\n3 6\n3 7\n3 8\n3 9\n3 10\n4 6\n4 7\n4 8\n4 9\n4 10\n5 6\n5 7\n5 8\n5 9\n5 10" }, { "input": "1", "output": "0" }, { "input": "2", "output": "1\n1 2" }, { "input": "11", "output": "30\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11" }, { "input": "13", "output": "42\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n4 13\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n5 13\n6 7\n6 8\n6 9\n6 10\n6 11\n6 12\n6 13" }, { "input": "16", "output": "64\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n4 9\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n4 16\n5 9\n5 10\n5 11\n5 12\n5 13\n5 14\n5 15\n5 16\n6 9\n6 10\n6 11\n6 12\n6 13\n6 14\n6 15\n6 16\n7 9\n7 10\n7 11\n7 12\n7 13\n7 14\n7 15\n7 16\n8 9\n8 10\n8 11\n8 12\n8 13\n8 14\n8 15\n8 16" }, { "input": "18", "output": "81\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n4 16\n4 17\n4 18\n5 10\n5 11\n5 12\n5 13\n5 14\n5 15\n5 16\n5 17\n5 18\n6 10\n6 11\n6 12\n6 13\n6 14\n6 15\n6 16\n6 17\n6 18\n7 10\n7 11\n7 12\n7 13\n7 14\n7 15\n7 16\n7 17\n7 18\n8 10\n8 11\n8 12\n8 13\n8 14\n8 15\n8 16\n8 17\n8 18\n9 10\n9 11\n9 12\n9 13\n9 14\n9 15\n9 16\n9 17\n9 18" }, { "input": "19", "output": "90\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n4 16\n4 17\n4 18\n4 19\n5 10\n5 11\n5 12\n5 13\n5 14\n5 15\n5 16\n5 17\n5 18\n5 19\n6 10\n6 11\n6 12\n6 13\n6 14\n6 15\n6 16\n6 17\n6 18\n6 19\n7 10\n7 11\n7 12\n7 13\n7 14\n7 15\n7 16\n7 17\n7 18\n7 19\n8 10\n8 11\n8 12\n8 13\n8 14\n8 15\n8 16\n8 17\n8 18\n8 19\n9 10\n9 11\n9 12\n9 13\n9 1..." }, { "input": "12", "output": "36\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n6 7\n6 8\n6 9\n6 10\n6 11\n6 12" }, { "input": "22", "output": "121\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n4 12\n4 13\n4 14\n4 15\n4 16\n4 17\n4 18\n4 19\n4 20\n4 21\n4 22\n5 12\n5 13\n5 14\n5 15\n5 16\n5 17\n5 18\n5 19\n5 20\n5 21\n5 22\n6 12\n6 13\n6 14\n6 15\n6 16\n6 17\n6 18\n6 19\n6 20\n6 21\n6 22\n7 12\n7 13\n7 14\n7 15\n7 16\n7 17\n7 18\n7 19\n7 20\n7 21\n7 22\n8 12\n8 13\n8 14\n8 15\n8 16\n8 17\n8 18\n8 ..." }, { "input": "23", "output": "132\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3 23\n4 12\n4 13\n4 14\n4 15\n4 16\n4 17\n4 18\n4 19\n4 20\n4 21\n4 22\n4 23\n5 12\n5 13\n5 14\n5 15\n5 16\n5 17\n5 18\n5 19\n5 20\n5 21\n5 22\n5 23\n6 12\n6 13\n6 14\n6 15\n6 16\n6 17\n6 18\n6 19\n6 20\n6 21\n6 22\n6 23\n7 12\n7 13\n7 14\n7 15\n7 16\n7 17\n7 18\n7 19\n7 20\n7 21\n7 22\n7 23\n8 ..." }, { "input": "15", "output": "56\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n4 8\n4 9\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n5 8\n5 9\n5 10\n5 11\n5 12\n5 13\n5 14\n5 15\n6 8\n6 9\n6 10\n6 11\n6 12\n6 13\n6 14\n6 15\n7 8\n7 9\n7 10\n7 11\n7 12\n7 13\n7 14\n7 15" }, { "input": "29", "output": "210\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3 23\n3 24\n3 25\n3 26\n3 27\n3 28\n3 29\n4 15\n4 16\n4 17\n4 18\n4 19\n4 20\n4 21\n4 22\n4 23\n4 24\n4 25\n4 26\n4 27\n4 28\n4 29\n5 15\n5 16\n5 17\n5 18\n5 19\n5 20\n5 21\n5 22\n5 23\n5 24\n5 25\n5 26\n5 27\n5 28\n5 29\n6 15\n6 16\n6 17\n6 18\n6 19\n6 20\n6 21\n6 22\n6 23\n6 ..." }, { "input": "31", "output": "240\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3 23\n3 24\n3 25\n3 26\n3 27\n3 28\n3 29\n3 30\n3 31\n4 16\n4 17\n4 18\n4 19\n4 20\n4 21\n4 22\n4 23\n4 24\n4 25\n4 26\n4 27\n4 28\n4 29\n4 30\n4 31\n5 16\n5 17\n5 18\n5 19\n5 20\n5 21\n5 22\n5 23\n5 24\n5 25\n5 26\n5 27\n5 28\n5 29\n5 30\n5 31\n6 16\n6 17\n6 18\n6 19\n6 ..." }, { "input": "33", "output": "272\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3 23\n3 24\n3 25\n3 26\n3 27\n3 28\n3 29\n3 30\n3 31\n3 32\n3 33\n4 17\n4 18\n4 19\n4 20\n4 21\n4 22\n4 23\n4 24\n4 25\n4 26\n4 27\n4 28\n4 29\n4 30\n4 31\n4 32\n4 33\n5 17\n5 18\n5 19\n5 20\n5 21\n5 22\n5 23\n5 24\n5 25\n5 26\n5 27\n5 28\n5 29\n5 30\n5 31\n5 32\n5 ..." }, { "input": "36", "output": "324\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n3 19\n3 20\n3 21\n3 22\n3 23\n3 24\n3 25\n3 26\n3 27\n3 28\n3 29\n3 30\n3 31\n3 32\n3 33\n3 34\n3 35\n3 36\n4 19\n4 20\n4 21\n4 22\n4 23\n4 24\n4 25\n4 26\n4 27\n4 28\n4 29\n4 30\n4 31\n4 32\n4 33\n4 34\n4 35\n4 36\n5 19\n5 20\n5 21\n5 22\n5 23\n5 24\n5 25\n5 26\n5 27\n5 28\n5 29\n5 30\n5 ..." }, { "input": "40", "output": "400\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n3 21\n3 22\n3 23\n3 24\n3 25\n3 26\n3 27\n3 28\n3 29\n3 30\n3 31\n3 32\n3 33\n3 34\n3 35\n3 36\n3 37\n3 38\n3 39\n3 40\n4 21\n4 22\n4 23\n4 24\n4 25\n4 26\n4 27\n4 28\n4 29\n4 30\n4 31\n4 32\n4 33\n4 34\n4 35\n4 36\n4 37\n4 38\n4 39\n4 40\n5 21\n5 22\n5 23\n5 24\n5 ..." }, { "input": "42", "output": "441\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n3 22\n3 23\n3 24\n3 25\n3 26\n3 27\n3 28\n3 29\n3 30\n3 31\n3 32\n3 33\n3 34\n3 35\n3 36\n3 37\n3 38\n3 39\n3 40\n3 41\n3 42\n4 22\n4 23\n4 24\n4 25\n4 26\n4 27\n4 28\n4 29\n4 30\n4 31\n4 32\n4 33\n4 34\n4 35\n4 36\n4 37\n4 38\n4 39\n4 40\n4 41\n4 42\n5 ..." }, { "input": "47", "output": "552\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n3 24\n3 25\n3 26\n3 27\n3 28\n3 29\n3 30\n3 31\n3 32\n3 33\n3 34\n3 35\n3 36\n3 37\n3 38\n3 39\n3 40\n3 41\n3 42\n3 43\n3 44\n3 45\n3 46\n3 47\n4 24\n4 25\n4 26\n4 27\n4 28\n4 29\n4 30\n4 31\n4 32\n4 33\n4 34\n4 35\n4 ..." }, { "input": "50", "output": "625\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n3 26\n3 27\n3 28\n3 29\n3 30\n3 31\n3 32\n3 33\n3 34\n3 35\n3 36\n3 37\n3 38\n3 39\n3 40\n3 41\n3 42\n3 43\n3 44\n3 45\n3 46\n3 47\n3 48\n3 49\n3 50\n4 26\n4 27\n4 28\n4 29\n4 30\n4 31\n4 32\n4 33\n4 34\n4 ..." }, { "input": "52", "output": "676\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n3 27\n3 28\n3 29\n3 30\n3 31\n3 32\n3 33\n3 34\n3 35\n3 36\n3 37\n3 38\n3 39\n3 40\n3 41\n3 42\n3 43\n3 44\n3 45\n3 46\n3 47\n3 48\n3 49\n3 50\n3 51\n3 52\n4 27\n4 28\n4 29\n4 30\n4 31\n4 32\n4 ..." }, { "input": "59", "output": "870\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n3 30\n3 31\n3 32\n3 33\n3 34\n3 35\n3 36\n3 37\n3 38\n3 39\n3 40\n3 41\n3 42\n3 43\n3 44\n3 45\n3 46\n3 47\n3 48\n3 49\n3 50\n3 51\n3 52\n3 53\n3 ..." }, { "input": "63", "output": "992\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n3 32\n3 33\n3 34\n3 35\n3 36\n3 37\n3 38\n3 39\n3 40\n3 41\n3 42\n3 43\n3 44\n3 45\n3 46\n3 47\n3 48\n3 49\n3 50\n3 51\n3 ..." }, { "input": "64", "output": "1024\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n3 33\n3 34\n3 35\n3 36\n3 37\n3 38\n3 39\n3 40\n3 41\n3 42\n3 43\n3 44\n3 45\n3 46\n3 47\n3 48\n3 49\n3 50\n3 51\n3 52\n3..." }, { "input": "68", "output": "1156\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n3 35\n3 36\n3 37\n3 38\n3 39\n3 40\n3 41\n3 42\n3 43\n3 44\n3 45\n3 46\n3 47\n3 48\n3 49\n3 50\n3..." }, { "input": "75", "output": "1406\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n3 38\n3 39\n3 40\n3 41\n3 42\n3 43\n3 44\n3 45\n3..." }, { "input": "77", "output": "1482\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n3 39\n3 40\n3 41\n3 42\n3 43\n3 44\n3..." }, { "input": "81", "output": "1640\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n3 41\n3 42\n3..." }, { "input": "86", "output": "1849\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2..." }, { "input": "87", "output": "1892\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2..." }, { "input": "88", "output": "1936\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2..." }, { "input": "89", "output": "1980\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2..." }, { "input": "90", "output": "2025\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2..." }, { "input": "91", "output": "2070\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2..." }, { "input": "92", "output": "2116\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n1 92\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2..." }, { "input": "93", "output": "2162\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n1 92\n1 93\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2..." }, { "input": "94", "output": "2209\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n1 92\n1 93\n1 94\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2..." }, { "input": "95", "output": "2256\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n1 92\n1 93\n1 94\n1 95\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2..." }, { "input": "96", "output": "2304\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n1 92\n1 93\n1 94\n1 95\n1 96\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2..." }, { "input": "97", "output": "2352\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n1 92\n1 93\n1 94\n1 95\n1 96\n1 97\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2..." }, { "input": "98", "output": "2401\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n1 92\n1 93\n1 94\n1 95\n1 96\n1 97\n1 98\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2..." }, { "input": "99", "output": "2450\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n1 92\n1 93\n1 94\n1 95\n1 96\n1 97\n1 98\n1 99\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2..." }, { "input": "100", "output": "2500\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n1 88\n1 89\n1 90\n1 91\n1 92\n1 93\n1 94\n1 95\n1 96\n1 97\n1 98\n1 99\n1 100\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n..." } ]
92
0
0
59,184
132
Constants in the language of Shakespeare
[ "constructive algorithms", "dp", "greedy" ]
null
null
Shakespeare is a widely known esoteric programming language in which programs look like plays by Shakespeare, and numbers are given by combinations of ornate epithets. In this problem we will have a closer look at the way the numbers are described in Shakespeare. Each constant in Shakespeare is created from non-negative powers of 2 using arithmetic operations. For simplicity we'll allow only addition and subtraction and will look for a representation of the given number which requires a minimal number of operations. You are given an integer *n*. You have to represent it as *n*<==<=*a*1<=+<=*a*2<=+<=...<=+<=*a**m*, where each of *a**i* is a non-negative power of 2, possibly multiplied by -1. Find a representation which minimizes the value of *m*.
The only line of input contains a positive integer *n*, written as its binary notation. The length of the notation is at most 106. The first digit of the notation is guaranteed to be 1.
Output the required minimal *m*. After it output *m* lines. Each line has to be formatted as "+2^x" or "-2^x", where *x* is the power coefficient of the corresponding term. The order of the lines doesn't matter.
[ "1111\n", "1010011\n" ]
[ "2\n+2^4\n-2^0\n", "4\n+2^0\n+2^1\n+2^4\n+2^6\n" ]
none
[ { "input": "1111", "output": "2\n+2^4\n-2^0" }, { "input": "1010011", "output": "4\n+2^0\n+2^1\n+2^4\n+2^6" }, { "input": "1", "output": "1\n+2^0" }, { "input": "10110111", "output": "4\n+2^8\n-2^6\n-2^3\n-2^0" }, { "input": "10101110", "output": "4\n+2^8\n-2^6\n-2^4\n-2^1" }, { "input": "1011001", "output": "4\n+2^0\n+2^3\n+2^4\n+2^6" }, { "input": "10001", "output": "2\n+2^0\n+2^4" }, { "input": "10", "output": "1\n+2^1" }, { "input": "11", "output": "2\n+2^0\n+2^1" }, { "input": "100", "output": "1\n+2^2" }, { "input": "100", "output": "1\n+2^2" }, { "input": "111", "output": "2\n+2^3\n-2^0" }, { "input": "1000000000", "output": "1\n+2^9" }, { "input": "1011000000", "output": "3\n+2^6\n+2^7\n+2^9" }, { "input": "1100010000", "output": "3\n+2^4\n+2^8\n+2^9" }, { "input": "1000111001", "output": "4\n+2^0\n+2^6\n-2^3\n+2^9" }, { "input": "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "1\n+2^99" }, { "input": "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "1\n+2^99" }, { "input": "1000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000", "output": "2\n+2^18\n+2^99" }, { "input": "1000000000000000000000000000010000000000000000100000000000100000000000000000000000000000000000000000", "output": "4\n+2^41\n+2^53\n+2^70\n+2^99" }, { "input": "1000000000000000000100000001000000000000000000100000101000000000000000000000100000000000000100000000", "output": "8\n+2^8\n+2^23\n+2^45\n+2^47\n+2^53\n+2^72\n+2^80\n+2^99" }, { "input": "1000100000101000000100100000000000000000100000000000000010000010000001010010100000000000000001001000", "output": "15\n+2^3\n+2^6\n+2^23\n+2^25\n+2^28\n+2^30\n+2^37\n+2^43\n+2^59\n+2^77\n+2^80\n+2^87\n+2^89\n+2^95\n+2^99" }, { "input": "1000001001000011000010001000100110010110100000000101010101000100100010000100000100000100001000000000", "output": "26\n+2^9\n+2^14\n+2^20\n+2^26\n+2^31\n+2^35\n+2^38\n+2^42\n+2^44\n+2^46\n+2^48\n+2^50\n+2^59\n+2^61\n+2^62\n+2^64\n+2^67\n+2^68\n+2^71\n+2^75\n+2^79\n+2^84\n+2^85\n+2^90\n+2^93\n+2^99" }, { "input": "1110000000001011100111001010000010101100010101001101001010101100011101100110000010000101110101000011", "output": "37\n+2^0\n+2^1\n+2^6\n+2^8\n+2^15\n-2^13\n-2^10\n+2^19\n+2^25\n+2^26\n+2^35\n-2^31\n-2^29\n+2^38\n+2^39\n+2^41\n+2^43\n+2^45\n+2^48\n+2^50\n+2^51\n+2^54\n+2^56\n+2^58\n+2^62\n+2^63\n+2^65\n+2^67\n+2^73\n+2^75\n+2^81\n-2^78\n+2^88\n-2^86\n-2^83\n+2^100\n-2^97" }, { "input": "1001110101111100101111011111111111101010111111010111011111111111011111111111111100011011111111111101", "output": "20\n+2^0\n+2^17\n-2^14\n-2^2\n+2^84\n-2^82\n-2^77\n-2^64\n-2^62\n-2^60\n-2^53\n-2^51\n-2^47\n-2^35\n-2^20\n+2^97\n-2^93\n-2^91\n-2^86\n+2^99" }, { "input": "1111111101111111101111111111101111111111111111111111101111111011111110011111101111111110111111111111", "output": "11\n+2^29\n-2^22\n-2^12\n-2^0\n+2^100\n-2^91\n-2^82\n-2^70\n-2^46\n-2^38\n-2^31" }, { "input": "100000000000000000000000000000000", "output": "1\n+2^32" }, { "input": "111111000111111000111111000111111", "output": "8\n+2^6\n-2^0\n+2^15\n-2^9\n+2^24\n-2^18\n+2^33\n-2^27" }, { "input": "10001100000000000011011011", "output": "7\n+2^8\n-2^5\n-2^2\n-2^0\n+2^20\n+2^21\n+2^25" }, { "input": "1101011", "output": "4\n+2^7\n-2^4\n-2^2\n-2^0" }, { "input": "11000101010000101101101101111000100100001101001111000011011100", "output": "21\n+2^8\n-2^5\n-2^2\n+2^16\n-2^12\n+2^18\n+2^20\n+2^21\n+2^26\n+2^29\n+2^48\n-2^46\n-2^43\n-2^40\n-2^37\n-2^33\n+2^52\n+2^54\n+2^56\n+2^60\n+2^61" }, { "input": "11101011", "output": "4\n+2^8\n-2^4\n-2^2\n-2^0" } ]
46
0
0
59,220
926
Segments
[ "data structures" ]
null
null
There is a straight line colored in white. *n* black segments are added on it one by one. After each segment is added, determine the number of connected components of black segments (i.Β e. the number of black segments in the union of the black segments). In particular, if one segment ends in a point *x*, and another segment starts in the point *x*, these two segments belong to the same connected component.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000) β€” the number of segments. The *i*-th of the next *n* lines contains two integers *l**i* and *r**i* (1<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=109) β€” the coordinates of the left and the right ends of the *i*-th segment. The segments are listed in the order they are added on the white line.
Print *n* integers β€” the number of connected components of black segments after each segment is added.
[ "3\n1 3\n4 5\n2 4\n", "9\n10 20\n50 60\n30 40\n70 80\n90 100\n60 70\n10 40\n40 50\n80 90\n" ]
[ "1 2 1 \n", "1 2 3 4 5 4 3 2 1 \n" ]
In the first example there are two components after the addition of the first two segments, because these segments do not intersect. The third added segment intersects the left segment and touches the right segment at the point 4 (these segments belong to the same component, according to the statements). Thus the number of connected components of black segments is equal to 1 after that.
[]
31
0
0
59,231
812
Sagheer and Apple Tree
[ "games", "trees" ]
null
null
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). Sagheer and Soliman will take turns to play. Soliman will make the first move. The player who can't make a move loses. In 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: 1. eat the apples, if the node is a leaf. 1. move the apples to one of the children, if the node is non-leaf. Before 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*. Can 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.
The first line will contain one integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of nodes in the apple tree. The second line will contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=107) β€” the number of apples on each node of the tree. The third line will contain *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=*n*) β€” the parent of each node of the tree. Node *i* has parent *p**i* (for 2<=≀<=*i*<=≀<=*n*). Node 1 is the root of the tree. It 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.
On a single line, print the number of different pairs of nodes (*u*,<=*v*), *u*<=β‰ <=*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.
[ "3\n2 2 3\n1 1\n", "3\n1 2 3\n1 1\n", "8\n7 2 2 5 4 3 1 1\n1 1 1 4 4 5 6\n" ]
[ "1\n", "0\n", "4\n" ]
In 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. In the second sample, There is no swap that will make Sagheer win the game. Note that Sagheer must make the swap even if he can win with the initial tree.
[ { "input": "3\n2 2 3\n1 1", "output": "1" }, { "input": "3\n1 2 3\n1 1", "output": "0" }, { "input": "8\n7 2 2 5 4 3 1 1\n1 1 1 4 4 5 6", "output": "4" }, { "input": "6\n7 7 7 7 7 7\n1 1 1 1 1", "output": "0" }, { "input": "6\n3 1 1 1 2 2\n1 1 1 1 1", "output": "2" }, { "input": "6\n1 2 3 4 5 6\n1 2 3 4 5", "output": "6" }, { "input": "7\n15 3 5 1 2 4 8\n1 1 2 2 3 3", "output": "11" }, { "input": "7\n15 3 5 1 2 4 9\n1 1 2 2 3 3", "output": "2" }, { "input": "7\n15 16 32 1 2 4 9\n1 1 2 2 3 3", "output": "0" }, { "input": "6\n49 70 74 18 64 63\n1 2 2 1 5", "output": "0" }, { "input": "9\n15 17 68 100 31 32 79 48 100\n1 2 3 4 3 6 6 2", "output": "0" }, { "input": "5\n87 100 12 93 86\n1 1 3 4", "output": "0" }, { "input": "3\n7751 9661 9437\n1 1", "output": "0" }, { "input": "8\n5201 769 1896 5497 1825 9718 7784 5952\n1 2 3 4 2 1 7", "output": "0" }, { "input": "2\n1848 2048\n1", "output": "0" }, { "input": "7\n588300 370437 481646 898447 78363 612652 998152\n1 2 3 2 5 1", "output": "0" }, { "input": "5\n753534 24400 461709 881954 452720\n1 2 3 1", "output": "0" }, { "input": "10\n191029 704946 159138 387479 61727 310778 534080 300097 442549 542174\n1 2 3 4 4 1 7 1 9", "output": "0" }, { "input": "4\n9849878 7525175 2569229 7972892\n1 2 2", "output": "0" }, { "input": "9\n734917 6649640 8476531 7374030 3139097 8258293 114238 8589112 7847119\n1 2 3 3 1 1 7 8", "output": "0" }, { "input": "10\n20 55 95 66 25 43 94 65 24 93\n1 2 3 2 5 5 1 8 9", "output": "0" }, { "input": "10\n9039 4789 3817 8625 516 4989 3436 1312 2989 3923\n1 2 3 3 1 6 7 6 9", "output": "0" }, { "input": "22\n324 4430 3495 8972 1547 9183 849 4663 2959 4715 8984 8016 2744 4451 8468 4549 9013 4124 9087 4823 4839 4635\n1 2 3 2 5 2 7 1 9 10 9 12 13 14 14 12 1 18 19 20 21", "output": "0" }, { "input": "21\n3735 1580 7599 9670 1414 8033 413 2852 5366 9196 4695 7629 7873 1731 9635 178 5637 3956 9520 8679 5006\n1 2 3 3 3 6 7 8 9 3 2 12 12 2 15 1 17 18 18 1", "output": "0" }, { "input": "23\n795895 158259 79726 699691 945099 38534 445699 515393 738257 857153 240818 675301 838661 323621 217120 707356 397865 725499 137739 272401 434551 135304 376364\n1 2 3 4 5 5 4 8 8 3 3 3 3 14 15 1 1 18 19 18 21 21", "output": "0" }, { "input": "22\n400941 84726 528265 945059 220341 935243 984080 215282 279808 757218 684733 72861 632695 371932 965754 849619 155281 780223 216197 591694 713921 293137\n1 2 3 4 5 6 3 8 8 1 11 12 13 13 11 16 17 11 1 20 20", "output": "0" }, { "input": "20\n889385 521616 271301 16205 522627 403737 958822 160624 675036 93618 352440 574828 756891 28294 239816 662464 835985 931516 576399 904671\n1 2 3 4 5 4 7 7 3 10 11 12 13 13 3 16 17 2 19", "output": "0" }, { "input": "19\n8746191 7960210 2540730 4331468 8492963 4996162 6655813 3805069 8827753 4274284 8410722 5213133 9813311 4714221 5980788 8244094 1518741 290394 4067514\n1 2 3 4 5 5 4 8 4 10 10 3 13 14 2 16 16 1", "output": "0" }, { "input": "21\n6194737 6429360 6047962 1014039 9196042 2483033 9232471 5444322 4437778 6614229 4791649 7417126 7679607 790939 3062370 174847 8404336 832859 2083127 9041108 3074902\n1 2 3 4 4 6 7 2 9 10 9 12 12 1 15 1 17 1 19 19", "output": "0" } ]
217
9,830,400
3
59,283
0
none
[ "none" ]
null
null
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherlandΒ β€” Uzhlyandia. It is widely known that Uzhlyandia has *n* cities connected with *m* bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is good if the path goes over *m*<=-<=2 roads twice, and over the other 2 exactly once. The good path can start and finish in any city of Uzhlyandia. Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help IgorΒ β€” calculate the number of good paths.
The first line contains two integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=106)Β β€” the number of cities and roads in Uzhlyandia, respectively. Each of the next *m* lines contains two integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*n*) that mean that there is road between cities *u* and *v*. It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself.
Print out the only integerΒ β€” the number of good paths in Uzhlyandia.
[ "5 4\n1 2\n1 3\n1 4\n1 5\n", "5 3\n1 2\n2 3\n4 5\n", "2 2\n1 1\n1 2\n" ]
[ "6", "0", "1" ]
In first sample test case the good paths are: - 2 → 1 → 3 → 1 → 4 → 1 → 5, - 2 → 1 → 3 → 1 → 5 → 1 → 4, - 2 → 1 → 4 → 1 → 5 → 1 → 3, - 3 → 1 → 2 → 1 → 4 → 1 → 5, - 3 → 1 → 2 → 1 → 5 → 1 → 4, - 4 → 1 → 2 → 1 → 3 → 1 → 5. There are good paths that are same with displayed above, because the sets of roads they pass over once are same: - 2 → 1 → 4 → 1 → 3 → 1 → 5, - 2 → 1 → 5 → 1 → 3 → 1 → 4, - 2 → 1 → 5 → 1 → 4 → 1 → 3, - 3 → 1 → 4 → 1 → 2 → 1 → 5, - 3 → 1 → 5 → 1 → 2 → 1 → 4, - 4 → 1 → 3 → 1 → 2 → 1 → 5, - and all the paths in the other direction. Thus, the answer is 6. In the second test case, Igor simply can not walk by all the roads. In the third case, Igor walks once over every road.
[]
77
5,632,000
0
59,428
174
Range Increments
[ "data structures", "greedy" ]
null
null
Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array *a* for all indexes in the segment [*l*,<=*r*]. In other words, this function does the following: Polycarpus knows the state of the array *a* after a series of function calls. He wants to determine the minimum number of function calls that lead to such state. In addition, he wants to find what function calls are needed in this case. It is guaranteed that the required number of calls does not exceed 105. Before calls of function rangeIncrement(l, r) all array elements equal zero.
The first input line contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the length of the array *a*[1... *n*]. The second line contains its integer space-separated elements, *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=≀<=*a*[*i*]<=≀<=105) after some series of function calls rangeIncrement(l, r). It is guaranteed that at least one element of the array is positive. It is guaranteed that the answer contains no more than 105 calls of function rangeIncrement(l, r).
Print on the first line *t* β€” the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105. Then print *t* lines β€” the descriptions of function calls, one per line. Each line should contain two integers *l**i*,<=*r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*) β€” the arguments of the *i*-th call rangeIncrement(l, r). Calls can be applied in any order. If there are multiple solutions, you are allowed to print any of them.
[ "6\n1 2 1 1 4 1\n", "5\n1 0 1 0 1\n" ]
[ "5\n2 2\n5 5\n5 5\n5 5\n1 6\n", "3\n1 1\n3 3\n5 5\n" ]
The first sample requires a call for the entire array, and four additional calls: - one for the segment [2,2] (i.e. the second element of the array), - three for the segment [5,5] (i.e. the fifth element of the array).
[ { "input": "6\n1 2 1 1 4 1", "output": "5\n2 2\n5 5\n5 5\n5 5\n1 6" }, { "input": "5\n1 0 1 0 1", "output": "3\n1 1\n3 3\n5 5" }, { "input": "1\n1", "output": "1\n1 1" }, { "input": "1\n100000", "output": "100000\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1..." }, { "input": "5\n1 2 3 4 5", "output": "5\n5 5\n4 5\n3 5\n2 5\n1 5" }, { "input": "12\n0 1 1 1 3 4 3 3 3 3 2 2", "output": "4\n6 6\n5 10\n5 12\n2 12" }, { "input": "2\n1 1", "output": "1\n1 2" }, { "input": "2\n2 1", "output": "2\n1 1\n1 2" }, { "input": "2\n1 3", "output": "3\n2 2\n2 2\n1 2" }, { "input": "2\n2 4", "output": "4\n2 2\n2 2\n1 2\n1 2" }, { "input": "3\n1 1 1", "output": "1\n1 3" }, { "input": "3\n0 2 1", "output": "2\n2 2\n2 3" }, { "input": "3\n2 2 1", "output": "2\n1 2\n1 3" }, { "input": "3\n2 4 2", "output": "4\n2 2\n2 2\n1 3\n1 3" }, { "input": "5\n1 1 0 0 0", "output": "1\n1 2" }, { "input": "5\n0 0 1 1 0", "output": "1\n3 4" }, { "input": "5\n1 0 2 1 0", "output": "3\n1 1\n3 3\n3 4" }, { "input": "5\n2 1 2 3 3", "output": "4\n1 1\n4 5\n3 5\n1 5" }, { "input": "20\n4 5 4 4 3 2 2 1 2 2 2 3 3 4 2 2 2 1 1 1", "output": "8\n2 2\n1 4\n1 5\n1 7\n14 14\n12 14\n9 17\n1 20" }, { "input": "20\n1 6 8 9 10 10 11 11 10 10 9 6 6 6 6 4 3 2 1 0", "output": "11\n7 8\n5 10\n4 11\n3 11\n3 11\n2 15\n2 15\n2 16\n2 17\n2 18\n1 19" }, { "input": "20\n4 6 7 8 8 8 9 9 10 12 12 11 12 12 11 9 8 8 5 2", "output": "13\n10 11\n13 14\n10 15\n9 15\n7 16\n4 18\n3 18\n2 18\n2 19\n1 19\n1 19\n1 20\n1 20" }, { "input": "20\n2 2 4 5 5 6 7 6 5 5 7 6 4 3 3 3 3 3 3 1", "output": "9\n7 7\n6 8\n11 11\n11 12\n4 12\n3 13\n3 19\n1 19\n1 20" }, { "input": "20\n5 9 11 12 13 13 13 13 13 13 13 13 13 13 12 11 11 8 6 4", "output": "13\n5 14\n4 15\n3 17\n3 17\n2 17\n2 18\n2 18\n2 19\n1 19\n1 20\n1 20\n1 20\n1 20" } ]
280
24,576,000
0
59,450
0
none
[ "none" ]
null
null
After all the events in Orlando we all know, Sasha and Roma decided to find out who is still the team's biggest loser. Thankfully, Masha found somewhere a revolver with a rotating cylinder of *n* bullet slots able to contain exactly *k* bullets, now the boys have a chance to resolve the problem once and for all. Sasha selects any *k* out of *n* slots he wishes and puts bullets there. Roma spins the cylinder so that every of *n* possible cylinder's shifts is equiprobable. Then the game starts, the players take turns, Sasha starts: he puts the gun to his head and shoots. If there was no bullet in front of the trigger, the cylinder shifts by one position and the weapon is given to Roma for make the same move. The game continues until someone is shot, the survivor is the winner. Sasha does not want to lose, so he must choose slots for bullets in such a way as to minimize the probability of its own loss. Of all the possible variant he wants to select the lexicographically minimal one, where an empty slot is lexicographically less than a charged one. More formally, the cylinder of *n* bullet slots able to contain *k* bullets can be represented as a string of *n* characters. Exactly *k* of them are "X" (charged slots) and the others are "." (uncharged slots). Let us describe the process of a shot. Suppose that the trigger is in front of the first character of the string (the first slot). If a shot doesn't kill anyone and the cylinder shifts, then the string shifts left. So the first character becomes the last one, the second character becomes the first one, and so on. But the trigger doesn't move. It will be in front of the first character of the resulting string. Among all the strings that give the minimal probability of loss, Sasha choose the lexicographically minimal one. According to this very string, he charges the gun. You have to help Sasha to charge the gun. For that, each *x**i* query must be answered: is there a bullet in the positions *x**i*?
The first line contains three integers *n*, *k* and *p* (1<=≀<=*n*<=≀<=1018,<=0<=≀<=*k*<=≀<=*n*,<=1<=≀<=*p*<=≀<=1000) β€” the number of slots in the cylinder, the number of bullets and the number of queries. Then follow *p* lines; they are the queries. Each line contains one integer *x**i* (1<=≀<=*x**i*<=≀<=*n*) the number of slot to describe. Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
For each query print "." if the slot should be empty and "X" if the slot should be charged.
[ "3 1 3\n1\n2\n3\n", "6 3 6\n1\n2\n3\n4\n5\n6\n", "5 2 5\n1\n2\n3\n4\n5\n" ]
[ "..X", ".X.X.X", "...XX" ]
The lexicographical comparison of is performed by the &lt; operator in modern programming languages. The *a* string is lexicographically less that the *b* string, if there exists such *i* (1 ≀ *i* ≀ *n*), that *a*<sub class="lower-index">*i*</sub> &lt; *b*<sub class="lower-index">*i*</sub>, and for any *j* (1 ≀ *j* &lt; *i*) *a*<sub class="lower-index">*j*</sub> = *b*<sub class="lower-index">*j*</sub>.
[ { "input": "3 1 3\n1\n2\n3", "output": "..X" }, { "input": "6 3 6\n1\n2\n3\n4\n5\n6", "output": ".X.X.X" }, { "input": "5 2 5\n1\n2\n3\n4\n5", "output": "...XX" }, { "input": "4 2 8\n1\n3\n4\n2\n3\n4\n1\n2", "output": "..XX.X.X" }, { "input": "4 0 4\n1\n2\n3\n4", "output": "...." }, { "input": "10 2 10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10", "output": ".......X.X" }, { "input": "12 2 12\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12", "output": ".........X.X" }, { "input": "9 4 9\n1\n2\n3\n4\n5\n6\n7\n8\n9", "output": "...X.X.XX" }, { "input": "15 10 15\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15", "output": ".X.X.X.X.XXXXXX" }, { "input": "7 3 7\n1\n2\n3\n4\n5\n6\n7", "output": "...X.XX" }, { "input": "7 4 7\n1\n2\n3\n4\n5\n6\n7", "output": ".X.X.XX" }, { "input": "7 5 7\n1\n2\n3\n4\n5\n6\n7", "output": ".X.XXXX" }, { "input": "7 7 7\n1\n2\n3\n4\n5\n6\n7", "output": "XXXXXXX" } ]
0
0
-1
59,518
39
Moon Craters
[ "dp", "sortings" ]
C. Moon Craters
1
256
There are lots of theories concerning the origin of moon craters. Most scientists stick to the meteorite theory, which says that the craters were formed as a result of celestial bodies colliding with the Moon. The other version is that the craters were parts of volcanoes. An extraterrestrial intelligence research specialist professor Okulov (the namesake of the Okulov, the author of famous textbooks on programming) put forward an alternate hypothesis. Guess what kind of a hypothesis it was –– sure, the one including extraterrestrial mind involvement. Now the professor is looking for proofs of his hypothesis. Professor has data from the moon robot that moves linearly in one direction along the Moon surface. The moon craters are circular in form with integer-valued radii. The moon robot records only the craters whose centers lay on his path and sends to the Earth the information on the distance from the centers of the craters to the initial point of its path and on the radii of the craters. According to the theory of professor Okulov two craters made by an extraterrestrial intelligence for the aims yet unknown either are fully enclosed one in the other or do not intersect at all. Internal or external tangency is acceptable. However the experimental data from the moon robot do not confirm this theory! Nevertheless, professor Okulov is hopeful. He perfectly understands that to create any logical theory one has to ignore some data that are wrong due to faulty measuring (or skillful disguise by the extraterrestrial intelligence that will be sooner or later found by professor Okulov!) That’s why Okulov wants to choose among the available crater descriptions the largest set that would satisfy his theory.
The first line has an integer *n* (1<=≀<=*n*<=≀<=2000) β€” the number of discovered craters. The next *n* lines contain crater descriptions in the "*c**i* *r**i*" format, where *c**i* is the coordinate of the center of the crater on the moon robot’s path, *r**i* is the radius of the crater. All the numbers *c**i* and *r**i* are positive integers not exceeding 109. No two craters coincide.
In the first line output the number of craters in the required largest set. In the next line output space-separated numbers of craters that this set consists of. The craters are numbered from 1 to *n* in the order in which they were given in the input data. The numbers may be output in any order. If the result is not unique, output any.
[ "4\n1 1\n2 2\n4 1\n5 1\n" ]
[ "3\n1 2 4\n" ]
none
[]
1,000
4,710,400
0
59,685
630
Pyramids
[ "geometry", "math" ]
null
null
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 β€” one triangular, one quadrangular and one pentagonal. The 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.
The only line of the input contains three integers *l*3,<=*l*4,<=*l*5 (1<=≀<=*l*3,<=*l*4,<=*l*5<=≀<=1000) β€” the edge lengths of triangular, quadrangular and pentagonal pyramids correspondingly.
Output one number β€” the total volume of the pyramids. Absolute or relative error should not be greater than 10<=-<=9.
[ "2 5 3\n" ]
[ "38.546168065709" ]
none
[ { "input": "2 5 3", "output": "38.546168065709" }, { "input": "3 4 5", "output": "55.954779230131" }, { "input": "1 1 1", "output": "0.655056222989" }, { "input": "1000 1000 1000", "output": "655056222.989098310000" }, { "input": "999 997 998", "output": "650782658.915145640000" }, { "input": "1 999 1000", "output": "536498692.999083940000" }, { "input": "709 328 709", "output": "157775455.683012100000" }, { "input": "982 948 879", "output": "517178698.528754120000" }, { "input": "99 23 530", "output": "45004055.801775165000" }, { "input": "1 2 998", "output": "299697433.026884910000" } ]
62
0
0
60,000
140
New Year Snowflake
[ "geometry", "sortings" ]
null
null
As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true. The New Year snowflake consists of tiny ice crystals, which can be approximately regarded as points on the plane. The beauty of the New Year snowflake is that it has a center of symmetry. This is a point such that for each crystal of the snowflake exists another crystal, symmetrical to it relative to that point. One of the crystals can be placed directly in the center of symmetry. While Constantine was choosing a snowflake among millions of other snowflakes, no less symmetrical and no less magical, then endured a difficult path through the drifts to the house of his mistress, while he was waiting with bated breath for a few long moments before the Beautiful Lady opens the door, some of the snowflake crystals melted and naturally disappeared. Constantine is sure that there were no more than *k* of such crystals, because he handled the snowflake very carefully. Now he is ready to demonstrate to the Beautiful Lady all the power of nanotechnology and restore the symmetry of snowflakes. You are given the coordinates of the surviving snowflake crystals, given in nanometers. Your task is to identify all possible positions of the original center of symmetry.
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=200<=000, 0<=≀<=*k*<=≀<=10) β€” the number of the surviving snowflake crystals and the maximum number of melted crystals, correspondingly. Next *n* lines contain the coordinates of the crystals that are left in the following form: "*x**i* *y**i*". The coordinates are integers and do not exceed 5Β·108 in absolute value. All given points are different.
The first line contains an integer *c* β€” the number of possible symmetry centers. Next *c* lines should contain the centers' descriptions. Each symmetry center is described by a couple of coordinates "*x* *y*", separated by a space. Print the coordinates with absolute error not exceeding 10<=-<=6. You are allowed to print the symmetry centers in any order. All printed points should be different. If there exist an infinite number of possible symmetry centers, print the single number "-1".
[ "4 0\n0 0\n0 1\n1 0\n1 1\n", "4 2\n0 0\n0 1\n1 0\n1 1\n", "4 4\n0 0\n0 1\n1 0\n1 1\n" ]
[ "1\n0.5 0.5\n", "5\n0.0 0.5\n0.5 0.0\n0.5 0.5\n0.5 1.0\n1.0 0.5\n", "-1\n" ]
none
[]
1,000
15,052,800
0
60,081
164
Variable, or There and Back Again
[ "dfs and similar", "graphs" ]
null
null
Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used! Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or ignored. Between some states are directed (oriented) transitions. A path is a sequence of states *v*1,<=*v*2,<=...,<=*v**x*, where for any 1<=≀<=*i*<=&lt;<=*x* exists a transition from *v**i* to *v**i*<=+<=1. Vasya's value in state *v* is interesting to the world, if exists path *p*1,<=*p*2,<=...,<=*p**k* such, that *p**i*<==<=*v* for some *i* (1<=≀<=*i*<=≀<=*k*), in state *p*1 Vasya gets assigned a value, in state *p**k* Vasya is used and there is no state *p**i* (except for *p*1) where Vasya gets assigned a value. Help Vasya, find the states in which Vasya's value is interesting to the world.
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the numbers of states and transitions, correspondingly. The second line contains space-separated *n* integers *f*1,<=*f*2,<=...,<=*f**n* (0<=≀<=*f**i*<=≀<=2), *f**i* described actions performed upon Vasya in state *i*: 0 represents ignoring, 1 β€” assigning a value, 2 β€” using. Next *m* lines contain space-separated pairs of integers *a**i*,<=*b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*), each pair represents the transition from the state number *a**i* to the state number *b**i*. Between two states can be any number of transitions.
Print *n* integers *r*1,<=*r*2,<=...,<=*r**n*, separated by spaces or new lines. Number *r**i* should equal 1, if Vasya's value in state *i* is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to *n* in the order, in which they are described in the input.
[ "4 3\n1 0 0 2\n1 2\n2 3\n3 4\n", "3 1\n1 0 2\n1 3\n", "3 1\n2 0 1\n1 3\n" ]
[ "1\n1\n1\n1\n", "1\n0\n1\n", "0\n0\n0\n" ]
In the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 3 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 4; it includes all the states, so in all of them Vasya's value is interesting to the world. The second sample the only path in which Vasya's value is interesting to the world is , β€” 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 3; state 2 is not included there. In the third sample we cannot make from the states any path in which the value of Vasya would be interesting to the world, so the value of Vasya is never interesting to the world.
[ { "input": "4 3\n1 0 0 2\n1 2\n2 3\n3 4", "output": "1\n1\n1\n1" }, { "input": "3 1\n1 0 2\n1 3", "output": "1\n0\n1" }, { "input": "3 1\n2 0 1\n1 3", "output": "0\n0\n0" }, { "input": "4 4\n1 0 2 0\n1 2\n2 3\n3 4\n4 1", "output": "1\n1\n1\n0" }, { "input": "2 1\n2 1\n2 1", "output": "1\n1" }, { "input": "3 2\n1 2 0\n1 2\n3 2", "output": "1\n1\n0" }, { "input": "8 8\n1 0 0 2 1 0 0 2\n1 2\n2 3\n3 2\n2 4\n6 8\n7 6\n6 7\n5 6", "output": "1\n1\n1\n1\n1\n1\n1\n1" }, { "input": "6 6\n1 0 0 0 0 2\n1 2\n2 3\n3 4\n4 5\n5 2\n3 6", "output": "1\n1\n1\n1\n1\n1" } ]
154
0
0
60,128
774
Pens And Days Of Week
[ "*special", "binary search", "number theory" ]
null
null
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 β€” he uses the pen number 2 and so on. On 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. Stepan 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.
The first line contains the integer *n* (1<=≀<=*n*<=≀<=50<=000) β€” the number of pens Stepan has. The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109), where *a**i* is equal to the number of milliliters of ink which the pen number *i* currently has.
Print 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. Pens are numbered in the order they are given in input data. The numeration begins from one. Note that the answer is always unambiguous, since several pens can not end at the same time.
[ "3\n3 3 3\n", "5\n5 4 5 4 4\n" ]
[ "2\n", "5\n" ]
In the first test Stepan uses ink of pens as follows: 1. on the day number 1 (Monday) Stepan will use the pen number 1, after that there will be 2 milliliters of ink in it; 1. on the day number 2 (Tuesday) Stepan will use the pen number 2, after that there will be 2 milliliters of ink in it; 1. on the day number 3 (Wednesday) Stepan will use the pen number 3, after that there will be 2 milliliters of ink in it; 1. on the day number 4 (Thursday) Stepan will use the pen number 1, after that there will be 1 milliliters of ink in it; 1. on the day number 5 (Friday) Stepan will use the pen number 2, after that there will be 1 milliliters of ink in it; 1. on the day number 6 (Saturday) Stepan will use the pen number 3, after that there will be 1 milliliters of ink in it; 1. 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; 1. on the day number 8 (Monday) Stepan will use the pen number 2, after that this pen will run out of ink. So, the first pen which will not have ink is the pen number 2.
[ { "input": "3\n3 3 3", "output": "2" }, { "input": "5\n5 4 5 4 4", "output": "5" }, { "input": "28\n2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033", "output": "1" }, { "input": "7\n10 10 10 10 10 10 10", "output": "1" }, { "input": "28\n1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000", "output": "1" }, { "input": "21\n996 995 996 996 996 996 995 996 996 995 996 996 995 996 995 995 995 995 996 996 996", "output": "2" }, { "input": "28\n2033 2033 2034 2033 2034 2034 2033 2034 2033 2034 2033 2034 2034 2033 2033 2034 2034 2033 2034 2034 2034 2033 2034 2033 2034 2034 2034 2034", "output": "1" }, { "input": "1\n1", "output": "1" }, { "input": "1\n2", "output": "1" }, { "input": "1\n1123", "output": "1" }, { "input": "1\n1000000000", "output": "1" }, { "input": "2\n1000000000 1000000000", "output": "2" }, { "input": "2\n999999999 999999999", "output": "1" }, { "input": "3\n1000000000 1000000000 1000000000", "output": "2" }, { "input": "3\n999999999 1000000000 1000000000", "output": "1" }, { "input": "4\n1000000000 1000000000 1000000000 1000000000", "output": "1" }, { "input": "4\n999999999 999999999 999999999 999999999", "output": "1" }, { "input": "5\n1000000000 1000000000 1000000000 1000000000 1000000000", "output": "1" }, { "input": "5\n999999999 1000000000 999999999 1000000000 999999999", "output": "1" }, { "input": "6\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "4" }, { "input": "6\n1000000000 999999999 999999999 999999999 1000000000 1000000000", "output": "3" }, { "input": "7\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "1" }, { "input": "7\n1000000000 1000000000 1000000000 1000000000 999999999 999999999 999999999", "output": "5" }, { "input": "8\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "1" }, { "input": "8\n1000000000 999999999 1000000000 999999999 1000000000 999999999 999999999 999999999", "output": "2" }, { "input": "7\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1", "output": "1" } ]
46
5,529,600
0
60,194
862
Mahmoud and Ehab and the final stage
[ "data structures", "strings" ]
null
null
Mahmoud and Ehab solved Dr. Evil's questions so he gave them the password of the door of the evil land. When they tried to open the door using it, the door gave them a final question to solve before they leave (yes, the door is digital, Dr. Evil is modern). If they don't solve it, all the work will be useless and they won't leave the evil land forever. Will you help them? Mahmoud and Ehab are given *n* strings *s*1,<=*s*2,<=... ,<=*s**n* numbered from 1 to *n* and *q* queries, Each query has one of the following forms: - 1 *a* *b* (1<=≀<=*a*<=≀<=*b*<=≀<=*n*), For all the intervals [*l*;*r*] where (*a*<=≀<=*l*<=≀<=*r*<=≀<=*b*) find the maximum value of this expression:(*r*<=-<=*l*<=+<=1)<=*<=*LCP*(*s**l*,<=*s**l*<=+<=1,<=... ,<=*s**r*<=-<=1,<=*s**r*) where *LCP*(*str*1,<=*str*2,<=*str*3,<=... ) is the length of the longest common prefix of the strings *str*1,<=*str*2,<=*str*3,<=... .- 2 *x* *y* (1<=≀<=*x*<=≀<=*n*) where *y* is a string, consisting of lowercase English letters. Change the string at position *x* to *y*.
The first line of input contains 2 integers *n* and *q* (1<=≀<=*n*<=≀<=105,<=1<=≀<=*q*<=≀<=105) – The number of strings and the number of queries, respectively. The second line contains *n* strings *str**i* consisting of lowercase English letters. The next *q* lines describe the queries and may have one of the 2 forms: - 1 *a* *b* (1<=≀<=*a*<=≀<=*b*<=≀<=*n*).- 2 *x* *y* (1<=≀<=*x*<=≀<=*n*), where *y* is a string consisting of lowercase English letters. the total length of all strings in input won't exceed 105
For each query of first type output its answer in a new line.
[ "5 9\nmahmoud mahmoudbadawy drmahmoud drevil mahmoud\n1 1 5\n1 1 2\n1 2 3\n2 3 mahmoud\n2 4 mahmoud\n2 2 mahmouu\n1 1 5\n1 2 3\n1 1 1\n" ]
[ "14\n14\n13\n30\n12\n7\n" ]
none
[]
77
2,764,800
-1
60,269
167
Wizards and Bets
[ "dfs and similar", "graphs", "math", "matrices" ]
null
null
In some country live wizards. They like to make weird bets. Two wizards draw an acyclic directed graph with *n* vertices and *m* edges (the graph's vertices are numbered from 1 to *n*). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink and the source simultaneously. In the wizards' graph the number of the sinks and the sources is the same. Wizards numbered the sources in the order of increasing numbers of the vertices from 1 to *k*. The sinks are numbered from 1 to *k* in the similar way. To make a bet, they, as are real wizards, cast a spell, which selects a set of *k* paths from all sources to the sinks in such a way that no two paths intersect at the vertices. In this case, each sink has exactly one path going to it from exactly one source. Let's suppose that the *i*-th sink has a path going to it from the *a**i*'s source. Then let's call pair (*i*,<=*j*) an inversion if *i*<=&lt;<=*j* and *a**i*<=&gt;<=*a**j*. If the number of inversions among all possible pairs (*i*,<=*j*), such that (1<=≀<=*i*<=&lt;<=*j*<=≀<=*k*), is even, then the first wizard wins (the second one gives him one magic coin). Otherwise, the second wizard wins (he gets one magic coin from the first one). Our wizards are captured with feverish excitement, so they kept choosing new paths again and again for so long that eventually they have chosen every possible set of paths for exactly once. The two sets of non-intersecting pathes are considered to be different, if and only if there is an edge, which lies at some path in one set and doesn't lie at any path of another set. To check their notes, they asked you to count the total winnings of the first player for all possible sets of paths modulo a prime number *p*.
The first line contains three space-separated integers *n*, *m*, *p* (1<=≀<=*n*<=≀<=600, 0<=≀<=*m*<=≀<=105, 2<=≀<=*p*<=≀<=109<=+<=7). It is guaranteed that *p* is prime number. Next *m* lines contain edges of the graph. Each line contains a pair of space-separated integers, *a**i*Β *b**i* β€” an edge from vertex *a**i* to vertex *b**i*. It is guaranteed that the graph is acyclic and that the graph contains the same number of sources and sinks. Please note that the graph can have multiple edges.
Print the answer to the problem β€” the total winnings of the first player modulo a prime number *p*. Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample).
[ "4 2 1000003\n1 3\n2 4\n", "4 2 1000003\n4 1\n3 2\n", "4 4 1000003\n2 1\n2 4\n3 1\n3 4\n", "6 5 1000003\n1 4\n1 5\n1 6\n2 6\n3 6\n", "5 2 1000003\n5 1\n3 4\n" ]
[ "1\n", "1000002\n", "0\n", "0\n", "1\n" ]
In the first sample, there is exactly one set of paths β€” <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4c100300071b70505c9eb8fd6f97456068e38f93.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The number of inversions is 0, which is an even number. Therefore, the first wizard gets 1 coin. In the second sample there is exactly one set of paths β€” <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/83cf8c3e6763396a8ead42b4762ff62e4b01f917.png" style="max-width: 100.0%;max-height: 100.0%;"/>. There is exactly one inversion. Therefore, the first wizard gets -1 coin. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fa579688f149cd6ab8f4c3c99c47c5a65e52e87c.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the third sample, there are two sets of paths, which are counted with opposite signs. In the fourth sample there are no set of paths at all. In the fifth sample, there are three sources β€” the vertices with the numbers (2, 3, 5) and three sinks β€” the vertices with numbers (1, 2, 4). For a single set of paths <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5ef1a2c8d476f3fea4b49124c0c23f371be35bb9.png" style="max-width: 100.0%;max-height: 100.0%;"/> are 2 inversions, that is, their number is even.
[]
92
0
0
60,299
436
Banners
[ "brute force", "data structures", "dp" ]
null
null
All modern mobile applications are divided into free and paid. Even a single application developers often release two versions: a paid version without ads and a free version with ads. Suppose that a paid version of the app costs *p* (*p* is an integer) rubles, and the free version of the application contains *c* ad banners. Each user can be described by two integers: *a**i* β€” the number of rubles this user is willing to pay for the paid version of the application, and *b**i* β€” the number of banners he is willing to tolerate in the free version. The behavior of each member shall be considered strictly deterministic: - if for user *i*, value *b**i* is at least *c*, then he uses the free version, - otherwise, if value *a**i* is at least *p*, then he buys the paid version without advertising, - otherwise the user simply does not use the application. Each user of the free version brings the profit of *c*<=Γ—<=*w* rubles. Each user of the paid version brings the profit of *p* rubles. Your task is to help the application developers to select the optimal parameters *p* and *c*. Namely, knowing all the characteristics of users, for each value of *c* from 0 to (*max*Β *b**i*)<=+<=1 you need to determine the maximum profit from the application and the corresponding parameter *p*.
The first line contains two integers *n* and *w* (1<=≀<=*n*<=≀<=105;Β 1<=≀<=*w*<=≀<=105) β€” the number of users and the profit from a single banner. Each of the next *n* lines contains two integers *a**i* and *b**i* (0<=≀<=*a**i*,<=*b**i*<=≀<=105) β€” the characteristics of the *i*-th user.
Print (*max*Β *b**i*)<=+<=2 lines, in the *i*-th line print two integers: *pay* β€” the maximum gained profit at *c*<==<=*i*<=-<=1, *p* (0<=≀<=*p*<=≀<=109) β€” the corresponding optimal app cost. If there are multiple optimal solutions, print any of them.
[ "2 1\n2 0\n0 2\n", "3 1\n3 1\n2 2\n1 3\n" ]
[ "0 3\n3 2\n4 2\n2 2\n", "0 4\n3 4\n7 3\n7 2\n4 2\n" ]
none
[]
5,000
0
0
60,536
490
Chocolate
[ "brute force", "dfs and similar", "math", "meet-in-the-middle", "number theory" ]
null
null
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<=Γ—<=*b*1 segments large and the second one is *a*2<=Γ—<=*b*2 segments large. Polycarpus 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. To 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. In 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. Both variants aren't always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16<=Γ—<=23, then Polycarpus can chip off a half, but not a third. If the bar is 20<=Γ—<=18, then Polycarpus can chip off both a half and a third. If the bar is 5<=Γ—<=7, then Polycarpus cannot chip off a half nor a third. What 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.
The first line of the input contains integers *a*1,<=*b*1 (1<=≀<=*a*1,<=*b*1<=≀<=109) β€” the initial sizes of the first chocolate bar. The second line of the input contains integers *a*2,<=*b*2 (1<=≀<=*a*2,<=*b*2<=≀<=109) β€” the initial sizes of the second bar. You can use the data of type int64 (in Pascal), long long (in Π‘++), long (in Java) to process large integers (exceeding 231<=-<=1).
In the first line print *m* β€” 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. If there is no solution, print a single line with integer -1.
[ "2 6\n2 3\n", "36 5\n10 16\n", "3 5\n2 1\n" ]
[ "1\n1 6\n2 3\n", "3\n16 5\n5 16\n", "-1\n" ]
none
[ { "input": "2 6\n2 3", "output": "1\n1 6\n2 3" }, { "input": "36 5\n10 16", "output": "3\n16 5\n5 16" }, { "input": "3 5\n2 1", "output": "-1" }, { "input": "36 5\n10 12", "output": "1\n24 5\n10 12" }, { "input": "1 1\n1 1", "output": "0\n1 1\n1 1" }, { "input": "2 1\n1 2", "output": "0\n2 1\n1 2" }, { "input": "3 6\n2 1", "output": "4\n1 2\n2 1" }, { "input": "1 27\n1 1", "output": "6\n1 1\n1 1" }, { "input": "2 5\n20 2", "output": "2\n2 5\n5 2" }, { "input": "40 5\n150 36", "output": "6\n40 5\n25 8" }, { "input": "60 1080\n60 45", "output": "5\n5 540\n60 45" }, { "input": "2160 3240\n7200 384", "output": "5\n640 2160\n3600 384" }, { "input": "51840 900\n48 27000", "output": "6\n1440 900\n48 27000" }, { "input": "100 200\n7200 25", "output": "4\n100 200\n800 25" }, { "input": "112500 96\n375 2400", "output": "4\n9375 96\n375 2400" }, { "input": "432000 3000\n4800 10000", "output": "6\n16000 3000\n4800 10000" }, { "input": "7 1\n1 7", "output": "0\n7 1\n1 7" }, { "input": "12 39\n13 3", "output": "4\n1 39\n13 3" }, { "input": "906992640 544195584\n906992640 725594112", "output": "2\n604661760 544195584\n453496320 725594112" }, { "input": "859963392 644972544\n725594112 967458816", "output": "6\n214990848 644972544\n143327232 967458816" }, { "input": "644972544 886837248\n725594112 886837248", "output": "3\n322486272 886837248\n322486272 886837248" }, { "input": "243 216\n6 1", "output": "16\n1 6\n6 1" }, { "input": "400 2500000\n1000000 1000", "output": "0\n400 2500000\n1000000 1000" }, { "input": "10000 100000\n2 1000000000", "output": "1\n10000 100000\n1 1000000000" }, { "input": "25000000 80\n128 23437500", "output": "1\n25000000 80\n128 15625000" }, { "input": "62500000 96\n256 7812500", "output": "2\n31250000 64\n256 7812500" }, { "input": "1280 2343750\n25600 312500", "output": "3\n1280 1562500\n6400 312500" }, { "input": "15625 1152000\n1562500 5760", "output": "1\n15625 576000\n1562500 5760" }, { "input": "9000000 12000\n6250 480000", "output": "6\n250000 12000\n6250 480000" }, { "input": "1920 50000000\n78125 25600", "output": "6\n40 50000000\n78125 25600" }, { "input": "5625000 19200\n1125000 96000", "output": "0\n5625000 19200\n1125000 96000" }, { "input": "45 800000000\n288000000 500", "output": "2\n45 800000000\n72000000 500" }, { "input": "750000000 725594112\n716636160 675000000", "output": "3\n500000000 483729408\n358318080 675000000" }, { "input": "10000079 1\n10000079 1", "output": "0\n10000079 1\n10000079 1" }, { "input": "1 30000237\n10000079 1", "output": "2\n1 10000079\n10000079 1" }, { "input": "10000079 1\n6 10000079", "output": "3\n10000079 1\n1 10000079" }, { "input": "3 540004266\n60000474 27", "output": "0\n3 540004266\n60000474 27" }, { "input": "720005688 725594112\n816293376 960007584", "output": "1\n720005688 725594112\n544195584 960007584" }, { "input": "859963392 816293376\n967458816 859963392", "output": "5\n254803968 816293376\n241864704 859963392" }, { "input": "644972544 816293376\n544195584 816293376", "output": "5\n161243136 816293376\n161243136 816293376" }, { "input": "99999989 1\n1 99999989", "output": "0\n99999989 1\n1 99999989" }, { "input": "99999989 9\n1 99999989", "output": "4\n99999989 1\n1 99999989" }, { "input": "199999978 2\n599999934 3", "output": "3\n199999978 2\n199999978 2" }, { "input": "544195584 899999901\n599999934 967458816", "output": "5\n161243136 899999901\n299999967 483729408" }, { "input": "8 8\n1 1", "output": "6\n1 1\n1 1" }, { "input": "31 15\n36 25", "output": "-1" }, { "input": "68 34\n84 78", "output": "-1" }, { "input": "894 197\n325 232", "output": "-1" }, { "input": "41764 97259\n54586 18013", "output": "-1" }, { "input": "333625 453145\n800800 907251", "output": "-1" }, { "input": "4394826 2233224\n609367 3364334", "output": "-1" }, { "input": "13350712 76770926\n61331309 8735000", "output": "-1" }, { "input": "844212449 863672439\n410956265 742052168", "output": "-1" }, { "input": "22295873 586964387\n4736819 472714349", "output": "-1" }, { "input": "905412001 865545936\n598517372 498343827", "output": "-1" }, { "input": "378462721 734062076\n42554822 374230201", "output": "-1" }, { "input": "261578849 307610920\n636335376 399859678", "output": "-1" }, { "input": "144694977 881159765\n80372825 425489156", "output": "-1" }, { "input": "35135676 3879\n841304242 18", "output": "4\n3903964 3879\n841304242 18" }, { "input": "57946752 619939008\n114816 331164", "output": "24\n92 413292672\n114816 331164" }, { "input": "171 162\n9 57", "output": "7\n19 27\n9 57" }, { "input": "2592 4950\n60 2970", "output": "7\n36 4950\n60 2970" }, { "input": "90315 96\n48 30105", "output": "3\n30105 48\n48 30105" }, { "input": "5832 45693720\n10154160 108", "output": "10\n24 45693720\n10154160 108" }, { "input": "5832 45693720\n10154160 108", "output": "10\n24 45693720\n10154160 108" }, { "input": "1 911953772\n39650164 23", "output": "0\n1 911953772\n39650164 23" }, { "input": "3 707552887\n6 707552887", "output": "1\n3 707552887\n3 707552887" }, { "input": "806410824 11\n2 369604961", "output": "4\n67200902 11\n2 369604961" }, { "input": "144 980783074\n24786 461544976", "output": "8\n144 980783074\n306 461544976" }, { "input": "614363206 2\n2 307181603", "output": "1\n307181603 2\n2 307181603" }, { "input": "1336608 1650\n18711 3182400", "output": "6\n1336608 1650\n693 3182400" }, { "input": "472586400 448\n1050192 8400", "output": "5\n19691100 448\n1050192 8400" }, { "input": "497664 367567200\n3304800 55351296", "output": "0\n497664 367567200\n3304800 55351296" }, { "input": "916090560 291133440\n628176384 424569600", "output": "0\n916090560 291133440\n628176384 424569600" }, { "input": "556792704 718502400\n640493568 832809600", "output": "2\n371195136 718502400\n320246784 832809600" }, { "input": "320 162162\n8736 1980", "output": "2\n160 108108\n8736 1980" }, { "input": "25740 6048\n38918880 81", "output": "6\n25740 6048\n1921920 81" }, { "input": "90720 35582976\n294840 9237888", "output": "5\n22680 35582976\n87360 9237888" }, { "input": "870912 1924560\n544195584 35925120", "output": "16\n870912 1924560\n46656 35925120" }, { "input": "846526464 537477120\n806215680 952342272", "output": "4\n423263232 537477120\n238878720 952342272" }, { "input": "862202880 967458816\n595213920 886837248", "output": "7\n107775360 967458816\n117573120 886837248" }, { "input": "564350976 623557440\n775982592 604661760", "output": "2\n376233984 623557440\n387991296 604661760" }, { "input": "775982592 716636160\n906992640 919683072", "output": "1\n775982592 716636160\n604661760 919683072" }, { "input": "806215680 940584960\n627056640 537477120", "output": "2\n358318080 940584960\n627056640 537477120" }, { "input": "537477120 560431872\n627056640 720555264", "output": "1\n537477120 560431872\n418037760 720555264" }, { "input": "564350976 906992640\n836075520 816293376", "output": "2\n376233984 906992640\n418037760 816293376" }, { "input": "591224832 529079040\n574801920 725594112", "output": "2\n394149888 529079040\n287400960 725594112" }, { "input": "816293376 881798400\n612220032 783820800", "output": "1\n544195584 881798400\n612220032 783820800" }, { "input": "862202880 764411904\n997691904 836075520", "output": "6\n215550720 764411904\n197074944 836075520" }, { "input": "766402560 725594112\n680244480 689762304", "output": "5\n191600640 725594112\n201553920 689762304" }, { "input": "766402560 816293376\n680244480 581986944", "output": "7\n95800320 816293376\n134369280 581986944" }, { "input": "952342272 554273280\n646652160 725594112", "output": "3\n423263232 554273280\n323326080 725594112" }, { "input": "739031040 564350976\n644972544 862202880", "output": "2\n492687360 564350976\n322486272 862202880" }, { "input": "831409920 564350976\n574801920 725594112", "output": "3\n369515520 564350976\n287400960 725594112" }, { "input": "1 1\n774840978 774840978", "output": "74\n1 1\n1 1" }, { "input": "725594112 725594112\n1 1", "output": "68\n1 1\n1 1" }, { "input": "1 1\n536870912 536870912", "output": "58\n1 1\n1 1" }, { "input": "573308928 573308928\n1 1", "output": "64\n1 1\n1 1" }, { "input": "1 1\n918330048 918330048", "output": "72\n1 1\n1 1" }, { "input": "1 1\n688747536 688747536", "output": "72\n1 1\n1 1" }, { "input": "536870912 536870912\n387420489 387420489", "output": "58\n128 536870912\n262144 262144" }, { "input": "967458816 967458816\n967458816 967458816", "output": "0\n967458816 967458816\n967458816 967458816" }, { "input": "1 1\n65536 65536", "output": "32\n1 1\n1 1" }, { "input": "387420489 387420489\n536870912 536870912", "output": "58\n262144 262144\n128 536870912" }, { "input": "999999937 999999937\n999999937 999999937", "output": "0\n999999937 999999937\n999999937 999999937" }, { "input": "387420489 774840978\n774840978 645700815", "output": "-1" } ]
92
0
3
60,748
843
Dynamic Shortest Path
[ "graphs", "shortest paths" ]
null
null
You are given a weighted directed graph, consisting of *n* vertices and *m* edges. You should answer *q* queries of two types: - 1 vΒ β€” find the length of shortest path from vertex 1 to vertex *v*. - 2 c *l*1 *l*2 ... *l**c*Β β€” add 1 to weights of edges with indices *l*1,<=*l*2,<=...,<=*l**c*.
The first line of input data contains integers *n*, *m*, *q* (1<=≀<=*n*,<=*m*<=≀<=105, 1<=≀<=*q*<=≀<=2000)Β β€” the number of vertices and edges in the graph, and the number of requests correspondingly. Next *m* lines of input data contain the descriptions of edges: *i*-th of them contains description of edge with index *i*Β β€” three integers *a**i*, *b**i*, *c**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, 0<=≀<=*c**i*<=≀<=109)Β β€” the beginning and the end of edge, and its initial weight correspondingly. Next *q* lines of input data contain the description of edges in the format described above (1<=≀<=*v*<=≀<=*n*, 1<=≀<=*l**j*<=≀<=*m*). It's guaranteed that inside single query all *l**j* are distinct. Also, it's guaranteed that a total number of edges in all requests of the second type does not exceed 106.
For each query of first type print the length of the shortest path from 1 to *v* in a separate line. Print -1, if such path does not exists.
[ "3 2 9\n1 2 0\n2 3 0\n2 1 2\n1 3\n1 2\n2 1 1\n1 3\n1 2\n2 2 1 2\n1 3\n1 2\n", "5 4 9\n2 3 1\n2 4 1\n3 4 1\n1 2 0\n1 5\n1 4\n2 1 2\n2 1 2\n1 4\n2 2 1 3\n1 4\n2 1 4\n1 4\n" ]
[ "1\n0\n2\n1\n4\n2\n", "-1\n1\n2\n3\n4\n" ]
The description of changes of the graph in the first sample case: <img class="tex-graphics" src="https://espresso.codeforces.com/aeb5751e557f6f6158f15919da64eee550146483.png" style="max-width: 100.0%;max-height: 100.0%;"/> The description of changes of the graph in the second sample case: <img class="tex-graphics" src="https://espresso.codeforces.com/5d4325fe06b5b55945d91d26c757ff5c9bdfbca1.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[]
46
0
0
61,080
852
Dating
[ "brute force", "dfs and similar", "graphs", "trees" ]
null
null
This story is happening in a town named BubbleLand. There are *n* houses in BubbleLand. In each of these *n* houses lives a boy or a girl. People there really love numbers and everyone has their favorite number *f*. That means that the boy or girl that lives in the *i*-th house has favorite number equal to *f**i*. The houses are numerated with numbers 1 to *n*. The houses are connected with *n*<=-<=1 bidirectional roads and you can travel from any house to any other house in the town. There is exactly one path between every pair of houses. A new dating had agency opened their offices in this mysterious town and the citizens were very excited. They immediately sent *q* questions to the agency and each question was of the following format: - *a* *b*Β β€” asking how many ways are there to choose a couple (boy and girl) that have the same favorite number and live in one of the houses on the unique path from house *a* to house *b*. Help the dating agency to answer the questions and grow their business.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105), the number of houses in the town. The second line contains *n* integers, where the *i*-th number is 1 if a boy lives in the *i*-th house or 0 if a girl lives in *i*-th house. The third line contains *n* integers, where the *i*-th number represents the favorite number *f**i* (1<=≀<=*f**i*<=≀<=109) of the girl or boy that lives in the *i*-th house. The next *n*<=-<=1 lines contain information about the roads and the *i*-th line contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*) which means that there exists road between those two houses. It is guaranteed that it's possible to reach any house from any other. The following line contains an integer *q* (1<=≀<=*q*<=≀<=105), the number of queries. Each of the following *q* lines represents a question and consists of two integers *a* and *b* (1<=≀<=*a*,<=*b*<=≀<=*n*).
For each of the *q* questions output a single number, the answer to the citizens question.
[ "7\n1 0 0 1 0 1 0\n9 2 9 2 2 9 9\n2 6\n1 2\n4 2\n6 5\n3 6\n7 4\n2\n1 3\n7 5\n" ]
[ "2\n3\n" ]
In the first question from house 1 to house 3, the potential couples are (1, 3) and (6, 3). In the second question from house 7 to house 5, the potential couples are (7, 6), (4, 2) and (4, 5).
[]
46
0
0
61,091
97
Superset
[ "constructive algorithms", "divide and conquer" ]
B. Superset
2
256
A set of points on a plane is called good, if for any two points at least one of the three conditions is true: - those two points lie on same horizontal line; - those two points lie on same vertical line; - the rectangle, with corners in these two points, contains inside or on its borders at least one point of the set, other than these two. We mean here a rectangle with sides parallel to coordinates' axes, the so-called bounding box of the two points. You are given a set consisting of *n* points on a plane. Find any good superset of the given set whose size would not exceed 2Β·105 points.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=104) β€” the number of points in the initial set. Next *n* lines describe the set's points. Each line contains two integers *x**i* and *y**i* (<=-<=109<=≀<=*x**i*,<=*y**i*<=≀<=109) β€” a corresponding point's coordinates. It is guaranteed that all the points are different.
Print on the first line the number of points *m* (*n*<=≀<=*m*<=≀<=2Β·105) in a good superset, print on next *m* lines the points. The absolute value of the points' coordinates should not exceed 109. Note that you should not minimize *m*, it is enough to find any good superset of the given set, whose size does not exceed 2Β·105. All points in the superset should have integer coordinates.
[ "2\n1 1\n2 2\n" ]
[ "3\n1 1\n2 2\n1 2\n" ]
none
[]
92
0
0
61,144
732
Tourist Reform
[ "dfs and similar", "graphs" ]
null
null
Berland is a tourist country! At least, it can become suchΒ β€” the government of Berland is confident about this. There are *n* cities in Berland, some pairs of which are connected by two-ways roads. Each road connects two different cities. In Berland there are no roads which connect the same pair of cities. It is possible to get from any city to any other city using given two-ways roads. According to the reform each road will become one-way. It will be oriented to one of two directions. To maximize the tourist attraction of Berland, after the reform for each city *i* the value *r**i* will be calculated. It will equal to the number of cities *x* for which there is an oriented path from the city *i* to the city *x*. In other words, *r**i* will equal the number of cities which can be reached from the city *i* by roads. The government is sure that tourist's attention will be focused on the minimum value of *r**i*. Help the government of Berland make the reform to maximize the minimum of *r**i*.
The first line contains two integers *n*,<=*m* (2<=≀<=*n*<=≀<=400<=000,<=1<=≀<=*m*<=≀<=400<=000)Β β€” the number of cities and the number of roads. The next *m* lines describe roads in Berland: the *j*-th of them contains two integers *u**j* and *v**j* (1<=≀<=*u**j*,<=*v**j*<=≀<=*n*, *u**j*<=β‰ <=*v**j*), where *u**j* and *v**j* are the numbers of cities which are connected by the *j*-th road. The cities are numbered from 1 to *n*. It is guaranteed that it is possible to get from any city to any other by following two-ways roads. In Berland there are no roads which connect the same pair of cities.
In the first line print single integerΒ β€” the maximum possible value *min*1<=≀<=*i*<=≀<=*n*{*r**i*} after the orientation of roads. The next *m* lines must contain the description of roads after the orientation: the *j*-th of them must contain two integers *u**j*,<=*v**j*, it means that the *j*-th road will be directed from the city *u**j* to the city *v**j*. Print roads in the same order as they are given in the input data.
[ "7 9\n4 3\n2 6\n7 1\n4 1\n7 3\n3 5\n7 4\n6 5\n2 5\n" ]
[ "4\n4 3\n6 2\n7 1\n1 4\n3 7\n5 3\n7 4\n5 6\n2 5\n" ]
none
[ { "input": "7 9\n4 3\n2 6\n7 1\n4 1\n7 3\n3 5\n7 4\n6 5\n2 5", "output": "4\n4 3\n6 2\n7 1\n1 4\n3 7\n5 3\n7 4\n5 6\n2 5" }, { "input": "2 1\n2 1", "output": "1\n2 1" }, { "input": "3 2\n1 3\n1 2", "output": "1\n3 1\n2 1" }, { "input": "3 3\n3 1\n3 2\n1 2", "output": "3\n3 1\n2 3\n1 2" }, { "input": "4 3\n3 4\n2 4\n1 4", "output": "1\n3 4\n2 4\n4 1" }, { "input": "12 16\n12 3\n10 12\n12 9\n4 10\n1 12\n10 6\n2 4\n7 10\n3 8\n9 8\n10 5\n1 11\n2 11\n11 10\n12 2\n3 10", "output": "9\n12 3\n10 12\n9 12\n10 4\n1 12\n6 10\n4 2\n7 10\n3 8\n8 9\n5 10\n11 1\n2 11\n10 11\n2 12\n3 10" }, { "input": "12 16\n12 5\n11 7\n1 8\n6 3\n3 4\n8 5\n3 10\n3 5\n2 9\n7 3\n6 2\n5 2\n1 10\n9 5\n4 7\n4 8", "output": "10\n12 5\n11 7\n8 1\n3 6\n4 3\n5 8\n10 3\n5 3\n2 9\n7 3\n6 2\n5 2\n1 10\n9 5\n4 7\n8 4" }, { "input": "12 16\n10 2\n9 6\n6 2\n12 1\n5 11\n11 12\n1 4\n9 10\n12 8\n4 8\n6 4\n2 12\n8 7\n3 10\n2 5\n7 12", "output": "11\n10 2\n6 9\n2 6\n12 1\n5 11\n11 12\n1 4\n9 10\n8 12\n4 8\n4 6\n2 12\n7 8\n3 10\n2 5\n12 7" }, { "input": "12 16\n2 8\n1 11\n7 12\n12 4\n6 12\n7 2\n2 9\n1 6\n5 7\n7 3\n8 10\n2 5\n8 6\n2 3\n2 1\n6 11", "output": "9\n2 8\n1 11\n7 12\n4 12\n12 6\n2 7\n9 2\n6 1\n5 7\n3 7\n10 8\n2 5\n8 6\n2 3\n1 2\n11 6" }, { "input": "12 16\n5 11\n9 11\n11 8\n2 5\n8 7\n12 11\n3 8\n3 1\n3 6\n12 10\n7 6\n5 1\n10 1\n7 4\n5 6\n11 6", "output": "9\n11 5\n9 11\n8 11\n2 5\n8 7\n12 11\n3 8\n1 3\n6 3\n10 12\n7 6\n5 1\n1 10\n4 7\n6 5\n6 11" }, { "input": "5 5\n1 2\n2 3\n3 4\n4 5\n4 2", "output": "3\n1 2\n3 2\n4 3\n5 4\n2 4" } ]
30
0
0
61,179
48
Black and White
[ "constructive algorithms" ]
H. Black and White
2
256
According to the legends the king of Berland Berl I was noted for his love of beauty and order. One day he ordered to tile the palace hall's floor where balls and receptions used to take place with black and white tiles according to a regular geometrical pattern invented by him. However, as is after the case, due to low financing there were only *a* black and *b* white tiles delivered to the palace. The other *c* tiles were black and white (see the picture). The initial plan failed! Having learned of that, the king gave a new command: tile the floor with the available tiles so that no black side of a tile touched a white one. The tiles are squares of one size 1<=Γ—<=1, every black and white tile can be rotated in one of the four ways. The court programmer was given the task to work out the plan of tiling and he coped with the task and didn't suffer the consequences of disobedience. And can you cope with it?
The first line contains given integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100) which represent the sizes of the rectangle that needs to be tiled. The next line contains non-negative numbers *a*, *b* and *c*, *a*<=+<=*b*<=+<=*c*<==<=*nm*, *c*<=β‰₯<=*m*.
Print 2*n* lines containing 2*m* characters each β€” the tiling scheme. Every tile is represented by a square 2<=Γ—<=2 in the following manner (the order corresponds to the order of the picture above):
[ "2 2\n0 0 4\n", "2 3\n1 2 3\n" ]
[ "\\../\n#\\/#\n\\##/\n.\\/.\n", "###/\\#\n##/..\\\n#/....\n/.....\n" ]
none
[]
60
0
-1
61,247
9
How many trees?
[ "combinatorics", "divide and conquer", "dp" ]
D. How many trees?
1
64
In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong β€” the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure β€” and then terrible Game Cubes fall down on the city, bringing death to those modules, who cannot win the game... For sure, as guard Bob appeared in Mainframe many modules stopped fearing Game Cubes. Because Bob (as he is alive yet) has never been defeated by User, and he always meddles with Game Cubes, because he is programmed to this. However, unpleasant situations can happen, when a Game Cube falls down on Lost Angles. Because there lives a nasty virus β€” Hexadecimal, who is... mmm... very strange. And she likes to play very much. So, willy-nilly, Bob has to play with her first, and then with User. This time Hexadecimal invented the following entertainment: Bob has to leap over binary search trees with *n* nodes. We should remind you that a binary search tree is a binary tree, each node has a distinct key, for each node the following is true: the left sub-tree of a node contains only nodes with keys less than the node's key, the right sub-tree of a node contains only nodes with keys greater than the node's key. All the keys are different positive integer numbers from 1 to *n*. Each node of such a tree can have up to two children, or have no children at all (in the case when a node is a leaf). In Hexadecimal's game all the trees are different, but the height of each is not lower than *h*. In this problem Β«heightΒ» stands for the maximum amount of nodes on the way from the root to the remotest leaf, the root node and the leaf itself included. When Bob leaps over a tree, it disappears. Bob gets the access to a Cube, when there are no trees left. He knows how many trees he will have to leap over in the worst case. And you?
The input data contains two space-separated positive integer numbers *n* and *h* (*n*<=≀<=35, *h*<=≀<=*n*).
Output one number β€” the answer to the problem. It is guaranteed that it does not exceed 9Β·1018.
[ "3 2\n", "3 3\n" ]
[ "5", "4" ]
none
[ { "input": "3 2", "output": "5" }, { "input": "3 3", "output": "4" }, { "input": "1 1", "output": "1" }, { "input": "2 1", "output": "2" }, { "input": "2 2", "output": "2" }, { "input": "27 11", "output": "61162698256896" }, { "input": "32 27", "output": "22643872890880" }, { "input": "4 1", "output": "14" }, { "input": "9 1", "output": "4862" }, { "input": "33 4", "output": "212336130412243110" }, { "input": "4 4", "output": "8" }, { "input": "8 5", "output": "1336" }, { "input": "12 8", "output": "127200" }, { "input": "15 5", "output": "9694844" }, { "input": "19 18", "output": "2424832" }, { "input": "23 17", "output": "19649347584" }, { "input": "27 15", "output": "25162319484928" }, { "input": "29 14", "output": "577801978306560" }, { "input": "33 18", "output": "54307238601375744" }, { "input": "7 7", "output": "64" }, { "input": "23 21", "output": "275251200" }, { "input": "7 3", "output": "429" }, { "input": "21 18", "output": "211156992" }, { "input": "4 1", "output": "14" }, { "input": "21 12", "output": "12153990144" }, { "input": "35 13", "output": "2690352397519398400" }, { "input": "19 2", "output": "1767263190" }, { "input": "33 26", "output": "434871797284864" }, { "input": "16 9", "output": "25607552" }, { "input": "16 14", "output": "1032192" }, { "input": "10 2", "output": "16796" }, { "input": "4 4", "output": "8" }, { "input": "33 17", "output": "75307983624118272" }, { "input": "27 25", "output": "6081740800" }, { "input": "20 14", "output": "1094473728" }, { "input": "16 11", "output": "11819008" }, { "input": "10 10", "output": "512" }, { "input": "4 3", "output": "14" }, { "input": "33 21", "output": "14830955929665536" }, { "input": "24 20", "output": "8171945984" }, { "input": "30 16", "output": "1375710400053248" }, { "input": "3 2", "output": "5" }, { "input": "9 4", "output": "4862" }, { "input": "16 5", "output": "35357670" }, { "input": "22 22", "output": "2097152" }, { "input": "28 23", "output": "739948625920" }, { "input": "34 1", "output": "812944042149730764" }, { "input": "7 4", "output": "428" }, { "input": "14 11", "output": "488448" }, { "input": "35 1", "output": "3116285494907301262" }, { "input": "35 35", "output": "17179869184" } ]
0
0
-1
61,449
0
none
[ "none" ]
null
null
Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters "a", ..., "z". The testtube is divided into *n* consecutive regions. Each region is occupied by a single colony of a certain bacteria type at any given moment. Hence, the population of the testtube at any moment can be described by a string of *n* Latin characters. Sometimes a colony can decide to conquer another colony in one of the adjacent regions. When that happens, the attacked colony is immediately eliminated and replaced by a colony of the same type as the attacking colony, while the attacking colony keeps its type. Note that a colony can only attack its neighbours within the boundaries of the testtube. At any moment, at most one attack can take place. For example, consider a testtube with population "babb". There are six options for an attack that may happen next: - the first colony attacks the second colony (1<=β†’<=2), the resulting population is "bbbb";- 2<=β†’<=1, the result is "aabb";- 2<=β†’<=3, the result is "baab";- 3<=β†’<=2, the result is "bbbb" (note that the result is the same as the first option);- 3<=β†’<=4 or 4<=β†’<=3, the population does not change. The pattern of attacks is rather unpredictable. Julia is now wondering how many different configurations of bacteria in the testtube she can obtain after a sequence of attacks takes place (it is possible that no attacks will happen at all). Since this number can be large, find it modulo 109<=+<=7.
The first line contains an integer *n*Β β€” the number of regions in the testtube (1<=≀<=*n*<=≀<=5<=000). The second line contains *n* small Latin letters that describe the initial population of the testtube.
Print one numberΒ β€” the answer to the problem modulo 109<=+<=7.
[ "3\naaa\n", "2\nab\n", "4\nbabb\n", "7\nabacaba\n" ]
[ "1\n", "3\n", "11\n", "589\n" ]
In the first sample the population can never change since all bacteria are of the same type. In the second sample three configurations are possible: "ab" (no attacks), "aa" (the first colony conquers the second colony), and "bb" (the second colony conquers the first colony). To get the answer for the third sample, note that more than one attack can happen.
[]
46
0
0
61,497
411
Multi-core Processor
[ "implementation" ]
null
null
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. At 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. The 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.
The first line contains three integers *n*, *m*, *k* (1<=≀<=*n*,<=*m*,<=*k*<=≀<=100). Then follow *n* lines describing instructions. The *i*-th line contains *m* integers: *x**i*1,<=*x**i*2,<=...,<=*x**im* (0<=≀<=*x**ij*<=≀<=*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 Β«do nothingΒ». But if *x**ij* is a number from 1 to *k*, then the corresponding instruction is Β«write information to the memory cell number *x**ij*Β». We 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*.
Print *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.
[ "4 3 5\n1 0 0\n1 0 2\n2 3 1\n3 2 0\n", "3 2 2\n1 2\n1 2\n2 2\n", "1 1 1\n0\n" ]
[ "1\n1\n3\n0\n", "1\n1\n0\n", "0\n" ]
none
[ { "input": "4 3 5\n1 0 0\n1 0 2\n2 3 1\n3 2 0", "output": "1\n1\n3\n0" }, { "input": "3 2 2\n1 2\n1 2\n2 2", "output": "1\n1\n0" }, { "input": "1 1 1\n0", "output": "0" }, { "input": "1 1 1\n1", "output": "0" }, { "input": "2 1 1\n1\n1", "output": "1\n1" }, { "input": "2 1 1\n1\n0", "output": "0\n0" }, { "input": "2 1 1\n0\n1", "output": "0\n0" }, { "input": "2 1 1\n0\n0", "output": "0\n0" }, { "input": "2 1 2\n1\n2", "output": "0\n0" }, { "input": "2 1 1\n1\n1", "output": "1\n1" }, { "input": "2 2 2\n2 1\n0 2", "output": "0\n0" }, { "input": "1 100 100\n32 97 28 73 22 27 27 21 25 26 21 95 45 60 47 64 44 88 24 10 82 55 84 69 86 70 99 99 34 59 71 83 53 90 29 100 98 68 24 82 5 67 49 70 23 85 5 90 57 0 99 26 32 11 81 92 6 45 32 72 54 32 20 37 40 33 55 55 33 61 13 31 67 51 74 96 67 13 28 3 23 99 26 6 91 95 67 29 46 78 85 17 47 83 26 51 88 31 37 15", "output": "0" }, { "input": "100 1 100\n59\n37\n53\n72\n37\n15\n8\n93\n92\n74\n11\n11\n68\n16\n92\n40\n76\n20\n10\n86\n76\n5\n9\n95\n5\n81\n44\n57\n10\n24\n22\n2\n57\n6\n26\n67\n48\n95\n34\n97\n55\n33\n70\n66\n51\n70\n74\n65\n35\n85\n37\n9\n27\n43\n65\n6\n5\n57\n54\n27\n22\n41\n8\n29\n10\n50\n9\n68\n78\n9\n92\n30\n88\n62\n30\n5\n80\n58\n19\n39\n22\n88\n81\n34\n36\n18\n28\n93\n64\n27\n47\n89\n30\n21\n24\n42\n34\n100\n27\n46", "output": "0\n1\n0\n0\n1\n0\n1\n1\n1\n1\n1\n1\n1\n0\n1\n0\n1\n0\n1\n0\n1\n1\n1\n1\n1\n1\n0\n1\n1\n1\n1\n0\n1\n1\n0\n0\n0\n1\n1\n0\n0\n0\n1\n0\n0\n1\n1\n1\n0\n0\n1\n1\n1\n0\n1\n1\n1\n1\n0\n1\n1\n0\n1\n0\n1\n0\n1\n1\n0\n1\n1\n1\n1\n0\n1\n1\n0\n0\n0\n0\n1\n1\n1\n1\n0\n0\n0\n1\n0\n1\n0\n0\n1\n0\n1\n0\n1\n0\n1\n0" }, { "input": "1 100 10\n7 2 8 3 0 10 0 3 0 5 3 6 4 1 2 2 5 1 7 10 7 9 10 6 2 8 6 10 0 10 4 4 4 9 7 0 0 8 6 2 2 4 10 10 5 9 4 6 1 1 9 7 2 7 4 7 2 2 3 3 10 3 8 1 0 4 3 10 9 8 6 2 10 7 5 10 0 3 6 2 3 6 6 2 5 9 10 0 10 4 10 3 4 2 2 10 4 5 7 8", "output": "0" }, { "input": "100 1 10\n10\n6\n8\n2\n4\n3\n3\n2\n0\n2\n10\n5\n10\n4\n10\n2\n6\n9\n1\n1\n1\n3\n7\n3\n9\n10\n6\n1\n4\n1\n4\n1\n4\n4\n5\n1\n9\n4\n10\n3\n3\n2\n8\n10\n1\n2\n10\n4\n8\n8\n4\n8\n6\n3\n8\n6\n8\n1\n2\n3\n2\n2\n9\n4\n1\n10\n10\n7\n8\n10\n8\n8\n10\n9\n2\n0\n5\n0\n9\n0\n2\n6\n7\n4\n5\n4\n2\n3\n1\n9\n7\n0\n10\n7\n2\n1\n1\n9\n6\n7", "output": "1\n1\n1\n1\n1\n1\n1\n1\n0\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n0\n1\n0\n1\n0\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n0\n1\n1\n1\n1\n1\n1\n1\n1" }, { "input": "7 2 98\n0 72\n71 26\n87 23\n26 37\n65 97\n81 30\n19 83", "output": "0\n0\n0\n0\n0\n0\n0" } ]
77
307,200
3
61,715
0
none
[ "none" ]
D. Falling Anvils
2
256
For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all. Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who throws anvils from the sky? From what height? We are sure that such questions have never troubled you! It turns out that throwing an anvil properly is not an easy task at all. Let's describe one of the most popular anvil throwing models. Let the height *p* of the potential victim vary in the range [0;*a*] and the direction of the wind *q* vary in the range [<=-<=*b*;*b*]. *p* and *q* could be any real (floating) numbers. Then we can assume that the anvil will fit the toon's head perfectly only if the following equation has at least one real root: Determine the probability with which an aim can be successfully hit by an anvil. You can assume that the *p* and *q* coefficients are chosen equiprobably and independently in their ranges.
The first line contains integer *t* (1<=≀<=*t*<=≀<=10000) β€” amount of testcases. Each of the following *t* lines contain two space-separated integers *a* and *b* (0<=≀<=*a*,<=*b*<=≀<=106). Pretests contain all the tests with 0<=&lt;<=*a*<=&lt;<=10,<=0<=≀<=*b*<=&lt;<=10.
Print *t* lines β€” the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10<=-<=6.
[ "2\n4 2\n1 2\n" ]
[ "0.6250000000\n0.5312500000\n" ]
none
[]
154
0
0
61,974
843
Maximum Flow
[ "flows", "graphs" ]
null
null
You are given a directed graph, consisting of *n* vertices and *m* edges. The vertices *s* and *t* are marked as source and sink correspondingly. Additionally, there are no edges ending at *s* and there are no edges beginning in *t*. The graph was constructed in a following way: initially each edge had capacity *c**i*<=&gt;<=0. A maximum flow with source at *s* and sink at *t* was constructed in this flow network. Let's denote *f**i* as the value of flow passing through edge with index *i*. Next, all capacities *c**i* and flow value *f**i* were erased. Instead, indicators *g**i* were written on edges β€” if flow value passing through edge *i* was positive, i.e. 1 if *f**i*<=&gt;<=0 and 0 otherwise. Using the graph and values *g**i*, find out what is the minimum possible number of edges in the initial flow network that could be saturated (the passing flow is equal to capacity, i.e. *f**i*<==<=*c**i*). Also construct the corresponding flow network with maximum flow in it. A flow in directed graph is described by flow values *f**i* on each of the edges so that the following conditions are satisfied: - for each vertex, except source and sink, total incoming flow and total outcoming flow are equal, - for each edge 0<=≀<=*f**i*<=≀<=*c**i* A flow is maximum if the difference between the sum of flow values on edges from the source, and the sum of flow values on edges to the source (there are no such in this problem), is maximum possible.
The first line of input data contains four positive integers *n*,<=*m*,<=*s*,<=*t* (2<=≀<=*n*<=≀<=100, 1<=≀<=*m*<=≀<=1000, 1<=≀<=*s*,<=*t*<=≀<=*n*, *s*<=β‰ <=*t*) β€” the number of vertices, the number of edges, index of source vertex and index of sink vertex correspondingly. Each of next *m* lines of input data contain non-negative integers *u**i*, *v**i*, *g**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, ) β€” the beginning of edge *i*, the end of edge *i* and indicator, which equals to 1 if flow value passing through edge *i* was positive and 0 if not. It's guaranteed that no edge connects vertex with itself. Also it's guaranteed that there are no more than one edge between each ordered pair of vertices and that there exists at least one network flow that satisfies all the constrains from input data.
In the first line print single non-negative integer *k* β€” minimum number of edges, which should be saturated in maximum flow. In each of next *m* lines print two integers *f**i*,<=*c**i* (1<=≀<=*c**i*<=≀<=109, 0<=≀<=*f**i*<=≀<=*c**i*) β€” the flow value passing through edge *i* and capacity of edge *i*. This data should form a correct maximum flow in flow network. Also there must be exactly *k* edges with statement *f**i*<==<=*c**i* satisfied. Also statement *f**i*<=&gt;<=0 must be true if and only if *g**i*<==<=1. If there are several possible answers, print any of them.
[ "5 6 1 5\n1 2 1\n2 3 1\n3 5 1\n1 4 1\n4 3 0\n4 5 1\n" ]
[ "2\n3 3\n3 8\n3 4\n4 4\n0 5\n4 9\n" ]
The illustration for second sample case. The saturated edges are marked dark, while edges with *g*<sub class="lower-index">*i*</sub> = 0 are marked with dotted line. The integer on edge is the index of this edge in input list. <img class="tex-graphics" src="https://espresso.codeforces.com/a80cb6067d5d58458863f416f6ac95ad991a308a.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[]
1,000
9,011,200
0
62,160
4
Mysterious Present
[ "dp", "sortings" ]
D. Mysterious Present
1
64
Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes *A*<==<={*a*1,<=<=*a*2,<=<=...,<=<=*a**n*}, where the width and the height of the *i*-th envelope is strictly higher than the width and the height of the (*i*<=<=-<=<=1)-th envelope respectively. Chain size is the number of envelopes in the chain. Peter wants to make the chain of the maximum size from the envelopes he has, the chain should be such, that he'll be able to put a card into it. The card fits into the chain if its width and height is lower than the width and the height of the smallest envelope in the chain respectively. It's forbidden to turn the card and the envelopes. Peter has very many envelopes and very little time, this hard task is entrusted to you.
The first line contains integers *n*, *w*, *h* (1<=<=≀<=*n*<=≀<=5000, 1<=≀<=*w*,<=<=*h*<=<=≀<=106) β€” amount of envelopes Peter has, the card width and height respectively. Then there follow *n* lines, each of them contains two integer numbers *w**i* and *h**i* β€” width and height of the *i*-th envelope (1<=≀<=*w**i*,<=<=*h**i*<=≀<=106).
In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, print any of the answers. If the card does not fit into any of the envelopes, print number 0 in the single line.
[ "2 1 1\n2 2\n2 2\n", "3 3 3\n5 4\n12 11\n9 8\n" ]
[ "1\n1 \n", "3\n1 3 2 \n" ]
none
[ { "input": "2 1 1\n2 2\n2 2", "output": "1\n1 " }, { "input": "3 3 3\n5 4\n12 11\n9 8", "output": "3\n1 3 2 " }, { "input": "5 10 10\n22 23\n17 19\n13 17\n8 12\n2 6", "output": "3\n3 2 1 " }, { "input": "5 13 13\n4 4\n10 10\n7 7\n1 1\n13 13", "output": "0" }, { "input": "4 12 140\n172 60\n71 95\n125 149\n53 82", "output": "1\n3 " }, { "input": "3 500 789\n56 32\n64 42\n74 55", "output": "0" }, { "input": "4 100 100\n332 350\n232 250\n32 50\n132 150", "output": "3\n4 2 1 " }, { "input": "2 10 10\n15 15\n16 16", "output": "2\n1 2 " }, { "input": "6 1 1\n900000 900000\n902400 902400\n901200 901200\n903600 903600\n906000 906000\n904800 904800", "output": "6\n1 3 2 4 6 5 " }, { "input": "5 1000 998\n5002 5005\n5003 5004\n5003 5002\n5002 5001\n5002 5002", "output": "2\n4 3 " }, { "input": "3 5 5\n6 2\n7 8\n10 2", "output": "1\n2 " }, { "input": "14 12 800\n166 847\n205 889\n223 907\n93 785\n110 803\n136 829\n189 871\n149 839\n40 740\n48 750\n180 857\n76 777\n125 820\n63 766", "output": "9\n5 13 6 8 1 11 7 2 3 " }, { "input": "15 600 875\n1200 451\n1664 852\n1763 1355\n1374 1724\n1374 1587\n1003 1513\n1636 1002\n431 367\n1632 690\n1257 778\n410 1632\n1045 1279\n1762 1763\n841 576\n1165 705", "output": "3\n6 5 13 " }, { "input": "30 900 15\n1396 562\n1265 475\n3329 2605\n1016 340\n2369 1595\n2085 1245\n2677 1934\n1953 1154\n3002 2199\n1688 855\n1147 407\n2762 2064\n2202 1359\n700 30\n3265 2488\n1884 1034\n2571 1836\n3067 2269\n882 212\n3505 2793\n1602 785\n2292 1460\n2442 1712\n2889 2127\n3187 2361\n1770 932\n3624 2891\n783 104\n3410 2704\n1521 696", "output": "27\n4 11 2 1 30 21 10 26 16 8 6 13 22 5 23 17 7 12 24 9 18 25 15 3 29 20 27 " } ]
77
0
0
62,436
350
Resort
[ "graphs" ]
null
null
Valera's finally decided to go on holiday! He packed up and headed for a ski resort. Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has *n* objects (we will consider the objects indexed in some way by integers from 1 to *n*), each object is either a hotel or a mountain. Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object *v*, the resort has at most one object *u*, such that there is a ski track built from object *u* to object *v*. We also know that no hotel has got a ski track leading from the hotel to some object. Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects *v*1,<=*v*2,<=...,<=*v**k* (*k*<=β‰₯<=1) and meet the following conditions: 1. Objects with numbers *v*1,<=*v*2,<=...,<=*v**k*<=-<=1 are mountains and the object with number *v**k* is the hotel. 1. For any integer *i* (1<=≀<=*i*<=&lt;<=*k*), there is exactly one ski track leading from object *v**i*. This track goes to object *v**i*<=+<=1. 1. The path contains as many objects as possible (*k* is maximal). Help Valera. Find such path that meets all the criteria of our hero!
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of objects. The second line contains *n* space-separated integers *type*1,<=*type*2,<=...,<=*type**n* β€” the types of the objects. If *type**i* equals zero, then the *i*-th object is the mountain. If *type**i* equals one, then the *i*-th object is the hotel. It is guaranteed that at least one object is a hotel. The third line of the input contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=*n*) β€” the description of the ski tracks. If number *a**i* equals zero, then there is no such object *v*, that has a ski track built from *v* to *i*. If number *a**i* doesn't equal zero, that means that there is a track built from object *a**i* to object *i*.
In the first line print *k* β€” the maximum possible path length for Valera. In the second line print *k* integers *v*1,<=*v*2,<=...,<=*v**k* β€” the path. If there are multiple solutions, you can print any of them.
[ "5\n0 0 0 0 1\n0 1 2 3 4\n", "5\n0 0 1 0 1\n0 1 2 2 4\n", "4\n1 0 0 0\n2 3 4 2\n" ]
[ "5\n1 2 3 4 5\n", "2\n4 5\n", "1\n1\n" ]
none
[ { "input": "5\n0 0 0 0 1\n0 1 2 3 4", "output": "5\n1 2 3 4 5" }, { "input": "5\n0 0 1 0 1\n0 1 2 2 4", "output": "2\n4 5" }, { "input": "4\n1 0 0 0\n2 3 4 2", "output": "1\n1" }, { "input": "10\n0 0 0 0 0 0 0 0 0 1\n4 0 8 4 7 8 5 5 7 2", "output": "2\n2 10" }, { "input": "50\n0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0\n28 4 33 22 4 35 36 31 42 25 50 33 25 36 18 23 23 28 43 3 18 31 1 2 15 22 40 43 29 32 28 35 18 27 48 40 14 36 27 50 40 5 48 14 36 24 32 33 26 50", "output": "2\n3 20" }, { "input": "100\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0\n86 12 47 46 45 31 20 47 58 79 23 70 35 72 37 20 16 64 46 87 57 7 84 72 70 3 14 40 17 42 30 99 12 20 38 98 14 40 4 83 10 15 47 30 83 58 12 7 97 46 17 6 41 13 87 37 36 12 7 25 26 35 69 13 18 5 9 53 72 28 13 51 5 57 14 64 28 25 91 96 57 69 9 12 97 7 56 42 31 15 88 16 41 88 86 13 89 81 3 42", "output": "1\n44" }, { "input": "10\n1 0 0 0 0 0 0 0 0 0\n6 2 7 8 2 9 0 5 4 2", "output": "6\n5 8 4 9 6 1" }, { "input": "100\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n38 2 49 55 6 42 12 100 25 69 85 76 13 22 78 73 37 64 5 21 0 23 61 87 4 16 44 3 98 54 1 91 18 26 82 24 18 50 95 21 75 97 51 9 67 73 51 19 63 92 27 82 8 7 20 84 2 93 40 11 39 80 58 85 74 48 72 78 34 33 31 65 46 71 32 36 33 88 47 4 66 84 16 27 16 14 90 16 79 41 99 30 57 73 28 89 45 81 86 29", "output": "52\n57 93 58 63 49 3 28 95 39 61 23 22 14 86 99 91 32 75 41 90 87 24 36 76 12 7 54 30 92 50 38 1 31 71 74 65 72 67 45 97 42 6 5 19 48 66 81 98 29 100 8 53" }, { "input": "2\n1 1\n0 0", "output": "1\n1" }, { "input": "1\n1\n0", "output": "1\n1" } ]
374
36,352,000
0
62,445
508
Tanya and Password
[ "dfs and similar", "graphs" ]
null
null
While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of *n*<=+<=2 characters. She has written all the possible *n* three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the password out. Each three-letter substring was written the number of times it occurred in the password. Thus, Tanya ended up with *n* pieces of paper. Then Tanya realized that dad will be upset to learn about her game and decided to restore the password or at least any string corresponding to the final set of three-letter strings. You have to help her in this difficult task. We know that dad's password consisted of lowercase and uppercase letters of the Latin alphabet and digits. Uppercase and lowercase letters of the Latin alphabet are considered distinct.
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·105), the number of three-letter substrings Tanya got. Next *n* lines contain three letters each, forming the substring of dad's password. Each character in the input is a lowercase or uppercase Latin letter or a digit.
If Tanya made a mistake somewhere during the game and the strings that correspond to the given set of substrings don't exist, print "NO". If it is possible to restore the string that corresponds to given set of substrings, print "YES", and then print any suitable password option.
[ "5\naca\naba\naba\ncab\nbac\n", "4\nabc\nbCb\ncb1\nb13\n", "7\naaa\naaa\naaa\naaa\naaa\naaa\naaa\n" ]
[ "YES\nabacaba\n", "NO\n", "YES\naaaaaaaaa\n" ]
none
[ { "input": "5\naca\naba\naba\ncab\nbac", "output": "YES\nabacaba" }, { "input": "4\nabc\nbCb\ncb1\nb13", "output": "NO" }, { "input": "7\naaa\naaa\naaa\naaa\naaa\naaa\naaa", "output": "YES\naaaaaaaaa" }, { "input": "1\nabc", "output": "YES\nabc" }, { "input": "2\nbba\nabb", "output": "YES\nabba" }, { "input": "3\naaa\naaa\naab", "output": "YES\naaaab" }, { "input": "2\naba\nbab", "output": "YES\nbaba" }, { "input": "2\nabc\nbga", "output": "NO" }, { "input": "4\naba\nbab\ncdc\ndcd", "output": "NO" }, { "input": "4\nbaa\ncaa\naax\naay", "output": "NO" }, { "input": "2\naba\naba", "output": "NO" }, { "input": "5\n123\n234\n345\n456\n567", "output": "YES\n1234567" }, { "input": "3\n42y\n2ya\nya6", "output": "YES\n42ya6" }, { "input": "8\nzHW\ncwx\nxmo\nWcw\nox1\nwxm\nmox\nHWc", "output": "YES\nzHWcwxmox1" }, { "input": "98\nuOK\nI0I\n7Ql\nqT6\nYux\nnGb\nXuh\nNZ4\nzrV\ntlm\nRMm\nWyO\nOCX\nq2q\nTpY\nukr\nRuO\njr7\nRjv\nxBW\nBWy\n1nG\n7Tp\n4NQ\nrI0\nepN\nZfB\nUzz\n6PR\nzFC\nIKN\nlR1\nQiq\nD2U\nV1h\niZf\nr7T\nuOt\nyOC\nNTe\njaN\nlmN\n2Uz\nZ4N\nuxB\nvuk\nAu1\nVQG\nT6P\nNQi\nfBy\nnr7\npYu\n0IK\nCXu\nZja\nhZj\nKNn\nu1n\neoj\nQGR\nmvu\njv3\npNi\nv3i\nmNT\nMmv\nr7Q\nFCu\n2qT\nkrI\nCuO\nKbR\nOtl\nR1z\nBxA\nGbV\n1zr\nojr\nPRu\nhD2\n3iZ\nNnr\niq2\nbRM\nOKb\n1hD\nNiB\nzzF\nuhZ\nbV1\nrVQ\niBx\nQlR\nxAu\nTeo\nGRj\naNZ", "output": "YES\nepNiBxAu1nGbV1hD2UzzFCuOKbRMmvukrI0IKNnr7TpYuxBWyOCXuhZjaNZ4NQiq2qT6PRuOtlmNTeojr7QlR1zrVQGRjv3iZfBy" }, { "input": "100\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa\naaa", "output": "YES\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }, { "input": "98\n229\nB2p\npBp\np9T\np9B\nB9T\nB9p\nB2T\n22T\nTBB\np2B\n29B\n9B9\nBTT\n929\n9pB\nT92\nppB\nB92\nBpT\nB9p\n29B\nT9B\n9B2\nTB9\np99\nT2T\n9TT\n9B2\nTp9\n2pB\npTB\nBp9\n92p\nBTB\n9Tp\nTBB\nTTp\n9B9\nTp2\nTBT\n9BT\n9BT\npT9\npBp\npB2\np9B\npBB\nBpB\n9pB\nBTB\n992\n2T9\n2TT\nT9p\nBTp\nBTB\n9pT\nT29\nTT9\n2B2\n2BT\nTT2\nTTT\n922\nB2B\np22\n9pT\nB2p\npBp\nB9B\nBTp\npT2\n9B9\nBBp\n2p2\nBpT\nTB2\n92B\nBB2\nTp9\n2BT\nB9B\nBB9\n2BT\n292\nTp9\np9p\nTpp\n292\nB2T\n2Tp\n2p9\nB2B\n2TB\n929\npT9\nBB9", "output": "YES\nBB9B9BTB2BTp9pT2922TBTBB9pT9pBB2Tp9BTB92p9B9pB2p229B2BTTT2TT9B2T929B9Tp2B2pBpBpTBBp9TTp99292BTppBpT9" }, { "input": "99\naaJ\nJx9\naLL\nrCx\nllJ\naja\nxxr\nLal\nv9C\njrL\nLvL\nJLl\nxlJ\nLja\nLLC\njvL\n9CJ\nvrJ\nlJJ\nlCC\nlxC\njxl\nLaJ\nLxJ\nrjr\nxvv\n9jC\nLxL\nvvr\nCCa\nJJr\nxJL\nxCj\nvv9\nLJJ\nx9J\nxx9\nrCx\nJLa\nrLv\nJrC\nvLx\njCv\nr9J\n9Cj\nv9C\naJL\nJrC\nCJx\nJJv\nJxx\nLCr\nLlx\nrJL\nx9L\naJx\nJxv\nxvv\nLLr\nLrC\nCv9\nCja\nxjv\n9Jj\nCjL\njvL\nC9j\nJLx\njaa\nxLJ\nlxv\n9LL\nJlC\nCxx\nJLa\njaj\nWRX\njJl\nljv\n9Jx\nall\nLlj\njaL\naJl\nvlx\nCr9\nCaJ\nCxj\nrC9\nJlJ\nlJL\njLl\nlJr\nvLj\njvl\nvjx\nJjv\nxrj\nJvj", "output": "NO" }, { "input": "5\ndip\nBQd\nipB\npBQ\nQdi", "output": "YES\nQdipBQd" } ]
31
0
0
62,519
109
Lucky Probability
[ "brute force", "probabilities" ]
B. Lucky Probability
2
256
Petya loves lucky numbers. We all know that 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. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer *p* from the interval [*p**l*,<=*p**r*] and Vasya chooses an integer *v* from the interval [*v**l*,<=*v**r*] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [*min*(*v*,<=*p*),<=*max*(*v*,<=*p*)] contains exactly *k* lucky numbers.
The single line contains five integers *p**l*, *p**r*, *v**l*, *v**r* and *k* (1<=≀<=*p**l*<=≀<=*p**r*<=≀<=109,<=1<=≀<=*v**l*<=≀<=*v**r*<=≀<=109,<=1<=≀<=*k*<=≀<=1000).
On the single line print the result with an absolute error of no more than 10<=-<=9.
[ "1 10 1 10 2\n", "5 6 8 10 1\n" ]
[ "0.320000000000\n", "1.000000000000\n" ]
Consider that [*a*, *b*] denotes an interval of integers; this interval includes the boundaries. That is, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/18b4a6012d95ad18891561410f0314497a578d63.png" style="max-width: 100.0%;max-height: 100.0%;"/> In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10Β·10 = 100, so answer is 32 / 100. In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number.
[ { "input": "1 10 1 10 2", "output": "0.320000000000" }, { "input": "5 6 8 10 1", "output": "1.000000000000" }, { "input": "1 20 100 120 5", "output": "0.150000000000" }, { "input": "1 10 1 10 3", "output": "0.000000000000" }, { "input": "1 100 1 100 2", "output": "0.362600000000" }, { "input": "47 95 18 147 4", "output": "0.080533751962" }, { "input": "1 1000000000 1 1000000000 47", "output": "0.000000010664" }, { "input": "1 2 3 4 12", "output": "0.000000000000" }, { "input": "1 50 64 80 4", "output": "0.231764705882" }, { "input": "1 128 45 99 2", "output": "0.432954545455" }, { "input": "45 855 69 854 7", "output": "0.005859319848" }, { "input": "1 1000 1 1000 2", "output": "0.082970000000" }, { "input": "999 999 1000 1000 1", "output": "0.000000000000" }, { "input": "789 5888 1 10 7", "output": "0.000000000000" }, { "input": "1 1000 1 1000 14", "output": "0.001792000000" }, { "input": "4 4 7 7 2", "output": "1.000000000000" }, { "input": "7 7 4 4 2", "output": "1.000000000000" }, { "input": "2588 3000 954 8555 4", "output": "0.035122336227" }, { "input": "1 10000 1 10000 2", "output": "0.009328580000" }, { "input": "1 10000 1 10000 6", "output": "0.009012260000" }, { "input": "69 98200 9999 88888 7", "output": "0.000104470975" }, { "input": "1 1000000000 1 1000000000 1000", "output": "0.000001185373" }, { "input": "1 1000000 1 1000000 19", "output": "0.000010456080" }, { "input": "4855 95555 485 95554750 7", "output": "0.000000239243" }, { "input": "2 999999999 3 999999998 999", "output": "0.000000001334" }, { "input": "45 8555 969 4000 3", "output": "0.000704970039" }, { "input": "369 852 741 963 2", "output": "0.134584738539" }, { "input": "8548 8554575 895 9954448 47", "output": "0.000001161081" }, { "input": "488 985544 8500 74844999 105", "output": "0.000000323831" }, { "input": "458995 855555 999999 84444444 245", "output": "0.000000065857" }, { "input": "8544 8855550 9874 8800000 360", "output": "0.000000000000" }, { "input": "1 1000000000 1 1000000000 584", "output": "0.000003345099" }, { "input": "1 1000000000 1 1000000000 48", "output": "0.000094672776" }, { "input": "1 1000000000 1 1000000000 470", "output": "0.000000073832" }, { "input": "1 1000000000 1 1000000000 49", "output": "0.000000010664" }, { "input": "1 1000000000 1 1000000000 998", "output": "0.000000012002" }, { "input": "4555 99878870 950000 400000000 458", "output": "0.000000218543" }, { "input": "99999999 989999999 1 1000000000 21", "output": "0.000000009517" }, { "input": "9887400 488085444 599 600000000 374", "output": "0.000000066330" }, { "input": "4 47777777 444444444 777777777 320", "output": "0.010618322184" }, { "input": "4 7 1 1000000000 395", "output": "0.000000021000" }, { "input": "123456789 987654321 4588 95470 512", "output": "0.000734548731" }, { "input": "1 1000000000 488 744444444 748", "output": "0.000000298888" }, { "input": "69 74444 47 744444 100", "output": "0.000000000000" }, { "input": "1 1000000000 100000000 1000000000 300", "output": "0.000000594125" }, { "input": "987654215 1000000000 9854874 854888120 270", "output": "0.000000031951" }, { "input": "85478 999999999 1 1000000000 1000", "output": "0.000000592737" }, { "input": "47 555555555 8596 584987999 894", "output": "0.000000000000" }, { "input": "74 182015585 98247 975000999 678", "output": "0.000000083341" }, { "input": "1 1000000000 7 1000000000 987", "output": "0.000000001335" }, { "input": "47 47 47 47 1", "output": "1.000000000000" }, { "input": "6 8 6 8 1", "output": "0.777777777778" }, { "input": "5 30 6 43 1", "output": "0.159919028340" }, { "input": "777777776 778777777 777777775 1000000000 1", "output": "0.000002013496" }, { "input": "28 46 8 45 1", "output": "0.199445983380" }, { "input": "444444 444445 444440 444446 1", "output": "0.857142857143" }, { "input": "1 6 2 4 1", "output": "0.666666666667" }, { "input": "1 10 1 10 1", "output": "0.460000000000" }, { "input": "4 4 4 4 1", "output": "1.000000000000" }, { "input": "4 7 4 7 2", "output": "0.125000000000" } ]
92
0
0
62,527
472
Design Tutorial: Change the Goal
[ "constructive algorithms", "math", "matrices" ]
null
null
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal. Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given *n* integers *x*1,<=*x*2,<=...,<=*x**n*. You are allowed to perform the assignments (as many as you want) of the following form *x**i* ^= *x**j* (in the original task *i* and *j* must be different, but in this task we allow *i* to equal *j*). The goal is to maximize the sum of all *x**i*. Now we just change the goal. You are also given *n* integers *y*1,<=*y*2,<=...,<=*y**n*. You should make *x*1,<=*x*2,<=...,<=*x**n* exactly equal to *y*1,<=*y*2,<=...,<=*y**n*. In other words, for each *i* number *x**i* should be equal to *y**i*.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=10000). The second line contains *n* integers: *x*1 to *x**n* (0<=≀<=*x**i*<=≀<=109). The third line contains *n* integers: *y*1 to *y**n* (0<=≀<=*y**i*<=≀<=109).
If there is no solution, output -1. If there is a solution, then in the first line output an integer *m* (0<=≀<=*m*<=≀<=1000000) – the number of assignments you need to perform. Then print *m* lines, each line should contain two integers *i* and *j* (1<=≀<=*i*,<=*j*<=≀<=*n*), which denote assignment *x**i* ^= *x**j*. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
[ "2\n3 5\n6 0\n", "5\n0 0 0 0 0\n1 2 3 4 5\n", "3\n4 5 6\n1 2 3\n", "3\n1 2 3\n4 5 6\n" ]
[ "2\n1 2\n2 2\n", "-1\n", "5\n3 1\n1 2\n2 2\n2 3\n3 1\n", "-1\n" ]
Assignment *a* ^= *b* denotes assignment *a* = *a* ^ *b*, where operation "^" is bitwise XOR of two integers.
[]
62
0
0
62,529
375
Maximum Submatrix 2
[ "data structures", "dp", "implementation", "sortings" ]
null
null
You are given a matrix consisting of digits zero and one, its size is *n*<=Γ—<=*m*. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations? Let's assume that the rows of matrix *a* are numbered from 1 to *n* from top to bottom and the columns are numbered from 1 to *m* from left to right. A matrix cell on the intersection of the *i*-th row and the *j*-th column can be represented as (*i*,<=*j*). Formally, a submatrix of matrix *a* is a group of four integers *d*,<=*u*,<=*l*,<=*r* (1<=≀<=*d*<=≀<=*u*<=≀<=*n*;Β 1<=≀<=*l*<=≀<=*r*<=≀<=*m*). We will assume that the submatrix contains cells (*i*,<=*j*) (*d*<=≀<=*i*<=≀<=*u*;Β *l*<=≀<=*j*<=≀<=*r*). The area of the submatrix is the number of cells it contains.
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=5000). Next *n* lines contain *m* characters each β€” matrix *a*. Matrix *a* only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines.
Print a single integer β€” the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0.
[ "1 1\n1\n", "2 2\n10\n11\n", "4 3\n100\n011\n000\n101\n" ]
[ "1\n", "2\n", "2\n" ]
none
[ { "input": "1 1\n1", "output": "1" }, { "input": "2 2\n10\n11", "output": "2" }, { "input": "4 3\n100\n011\n000\n101", "output": "2" }, { "input": "11 16\n0111110101100011\n1000101100010000\n0010110110010101\n0110110010110010\n0011101101110000\n1001100011010111\n0010011111111000\n0100100100111110\n1001000000100111\n0110000011001000\n1011111011010000", "output": "9" }, { "input": "19 12\n110001100110\n100100000000\n101011001111\n010111110001\n011000100100\n011111010000\n010011101100\n011010011110\n011001111110\n010111110001\n010000010111\n001111110100\n100100110001\n100110000000\n110000010010\n111101011101\n010111100000\n100000011010\n000100100101", "output": "16" }, { "input": "13 19\n0000111111111111011\n0111000001110001101\n1110100110111011101\n0001101011100001110\n1101100100010000101\n1010100011110011010\n1010011101010000001\n1011101000001111000\n1101110001101011110\n0110101010001111100\n0001011010100111001\n1111101000110001000\n0010010000011100010", "output": "14" }, { "input": "8 5\n00000\n00000\n00000\n00000\n00000\n00000\n00000\n00000", "output": "0" }, { "input": "15 18\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111\n111111111111111111", "output": "270" }, { "input": "1 1\n0", "output": "0" } ]
2,000
54,476,800
0
62,600
718
Sasha and Array
[ "data structures", "math", "matrices" ]
null
null
Sasha has an array of integers *a*1,<=*a*2,<=...,<=*a**n*. You have to perform *m* queries. There might be queries of two types: 1. 1 l r xΒ β€” increase all integers on the segment from *l* to *r* by values *x*; 1. 2 l rΒ β€” find , where *f*(*x*) is the *x*-th Fibonacci number. As this number may be large, you only have to find it modulo 109<=+<=7. In this problem we define Fibonacci numbers as follows: *f*(1)<==<=1, *f*(2)<==<=1, *f*(*x*)<==<=*f*(*x*<=-<=1)<=+<=*f*(*x*<=-<=2) for all *x*<=&gt;<=2. Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha?
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*<=≀<=100<=000, 1<=≀<=*m*<=≀<=100<=000)Β β€” the number of elements in the array and the number of queries respectively. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109). Then follow *m* lines with queries descriptions. Each of them contains integers *tp**i*, *l**i*, *r**i* and may be *x**i* (1<=≀<=*tp**i*<=≀<=2, 1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*, 1<=≀<=*x**i*<=≀<=109). Here *tp**i*<==<=1 corresponds to the queries of the first type and *tp**i* corresponds to the queries of the second type. It's guaranteed that the input will contains at least one query of the second type.
For each query of the second type print the answer modulo 109<=+<=7.
[ "5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5\n" ]
[ "5\n7\n9\n" ]
Initially, array *a* is equal to 1, 1, 2, 1, 1. The answer for the first query of the second type is *f*(1) + *f*(1) + *f*(2) + *f*(1) + *f*(1) = 1 + 1 + 1 + 1 + 1 = 5. After the query 1 2 4 2 array *a* is equal to 1, 3, 4, 3, 1. The answer for the second query of the second type is *f*(3) + *f*(4) + *f*(3) = 2 + 3 + 2 = 7. The answer for the third query of the second type is *f*(1) + *f*(3) + *f*(4) + *f*(3) + *f*(1) = 1 + 2 + 3 + 2 + 1 = 9.
[ { "input": "5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5", "output": "5\n7\n9" }, { "input": "2 3\n1 3\n2 1 1\n1 1 2 3\n1 1 2 2", "output": "1" }, { "input": "7 4\n2 2 1 1 3 3 2\n2 1 5\n2 6 7\n1 3 4 3\n2 6 6", "output": "6\n3\n2" }, { "input": "9 4\n2 1 2 3 3 3 2 1 3\n2 1 8\n1 7 7 3\n1 1 3 1\n1 3 5 2", "output": "11" }, { "input": "18 19\n2 2 2 2 1 1 1 1 1 2 1 2 2 1 2 1 2 2\n2 4 14\n2 16 16\n2 5 8\n1 1 6 2\n2 14 14\n2 1 3\n2 4 5\n2 11 13\n2 18 18\n1 8 15 2\n2 17 18\n2 14 16\n1 3 10 2\n2 1 9\n1 3 18 1\n1 17 18 1\n2 17 17\n1 15 15 1\n1 3 18 2", "output": "11\n1\n4\n1\n9\n5\n3\n1\n2\n6\n44\n3" }, { "input": "18 31\n1 1 2 2 2 1 2 1 2 2 2 2 1 1 1 2 2 1\n1 4 7 2\n1 9 10 1\n1 2 4 2\n1 15 18 2\n1 3 10 2\n1 7 8 1\n1 13 18 2\n1 15 17 2\n1 16 17 1\n1 16 16 2\n1 10 12 1\n1 4 13 2\n1 3 8 1\n1 7 9 1\n1 13 18 2\n1 3 14 2\n1 2 2 1\n1 14 17 1\n1 9 18 1\n1 7 17 1\n1 2 5 1\n1 8 13 2\n1 12 18 1\n2 13 14\n2 15 18\n1 14 14 2\n1 8 14 2\n1 3 11 2\n1 17 17 2\n1 7 10 2\n2 3 12", "output": "466\n2474\n22934" }, { "input": "17 30\n8 10 7 8 7 9 10 6 7 3 7 2 8 8 10 9 8\n1 16 16 5\n2 13 13\n1 16 17 4\n1 1 17 2\n2 17 17\n1 2 10 10\n1 4 8 3\n1 17 17 4\n1 13 15 6\n1 10 16 2\n2 13 14\n2 12 14\n1 6 11 5\n2 4 14\n1 12 16 7\n1 8 8 4\n1 1 16 9\n1 17 17 6\n1 7 8 7\n1 7 10 1\n1 11 14 6\n1 6 10 8\n1 9 14 8\n1 16 16 9\n1 6 13 10\n1 9 13 10\n2 8 8\n1 5 12 6\n1 8 11 10\n1 6 15 7", "output": "21\n377\n5168\n5176\n1584272\n680057396" }, { "input": "1 1\n1\n2 1 1", "output": "1" }, { "input": "1 2\n1000000000\n1 1 1 1000000000\n2 1 1", "output": "999999020" } ]
15
0
0
62,740
732
Exams
[ "binary search", "greedy", "sortings" ]
null
null
Vasiliy has an exam period which will continue for *n* days. He has to pass exams on *m* subjects. Subjects are numbered from 1 to *m*. About every day we know exam for which one of *m* subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day. On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest. About each subject Vasiliy know a number *a**i*Β β€” the number of days he should prepare to pass the exam number *i*. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during *a**i* days for the exam number *i*. He can mix the order of preparation for exams in any way. Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105)Β β€” the number of days in the exam period and the number of subjects. The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (0<=≀<=*d**i*<=≀<=*m*), where *d**i* is the number of subject, the exam of which can be passed on the day number *i*. If *d**i* equals 0, it is not allowed to pass any exams on the day number *i*. The third line contains *m* positive integers *a*1,<=*a*2,<=...,<=*a**m* (1<=≀<=*a**i*<=≀<=105), where *a**i* is the number of days that are needed to prepare before passing the exam on the subject *i*.
Print one integerΒ β€” the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
[ "7 2\n0 1 0 2 1 0 2\n2 1\n", "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4\n", "5 1\n1 1 1 1 1\n5\n" ]
[ "5\n", "9\n", "-1\n" ]
In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day. In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day. In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.
[ { "input": "7 2\n0 1 0 2 1 0 2\n2 1", "output": "5" }, { "input": "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4", "output": "9" }, { "input": "5 1\n1 1 1 1 1\n5", "output": "-1" }, { "input": "100 10\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 10 9 4 4 8 8 6 6 1 5 5 6 5 6 6 6 9 4 7 5 4 6 6 1 1 2 1 8 10 6 2 1 7 2 1 8 10 9 2 7 3 1 5 10 2 8 10 10 10 8 9 5 4 6 10 8 9 6 6\n2 4 10 11 5 2 6 7 2 15", "output": "74" }, { "input": "1 1\n1\n1", "output": "-1" }, { "input": "3 2\n0 0 0\n2 1", "output": "-1" }, { "input": "4 2\n0 1 0 2\n1 1", "output": "4" }, { "input": "10 1\n0 1 0 0 0 0 0 0 0 1\n1", "output": "2" }, { "input": "5 1\n0 0 0 0 1\n1", "output": "5" }, { "input": "7 2\n0 0 0 0 0 1 2\n1 1", "output": "7" }, { "input": "10 3\n0 0 1 2 2 0 2 0 1 3\n1 1 4", "output": "10" }, { "input": "6 2\n1 1 1 1 1 2\n1 1", "output": "6" }, { "input": "6 2\n1 0 0 0 0 2\n1 1", "output": "-1" } ]
139
12,492,800
3
62,800
756
Nikita and stack
[ "data structures" ]
null
null
Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operation push(x) puts an integer *x* on the top of the stack, and operation pop() deletes the top integer from the stack, i.Β e. the last added. If the stack is empty, then the operation pop() does nothing. Nikita made *m* operations with the stack but forgot them. Now Nikita wants to remember them. He remembers them one by one, on the *i*-th step he remembers an operation he made *p**i*-th. In other words, he remembers the operations in order of some permutation *p*1,<=*p*2,<=...,<=*p**m*. After each step Nikita wants to know what is the integer on the top of the stack after performing the operations he have already remembered, in the corresponding order. Help him!
The first line contains the integer *m* (1<=≀<=*m*<=≀<=105)Β β€” the number of operations Nikita made. The next *m* lines contain the operations Nikita remembers. The *i*-th line starts with two integers *p**i* and *t**i* (1<=≀<=*p**i*<=≀<=*m*, *t**i*<==<=0 or *t**i*<==<=1)Β β€” the index of operation he remembers on the step *i*, and the type of the operation. *t**i* equals 0, if the operation is pop(), and 1, is the operation is push(x). If the operation is push(x), the line also contains the integer *x**i* (1<=≀<=*x**i*<=≀<=106)Β β€” the integer added to the stack. It is guaranteed that each integer from 1 to *m* is present exactly once among integers *p**i*.
Print *m* integers. The integer *i* should equal the number on the top of the stack after performing all the operations Nikita remembered on the steps from 1 to *i*. If the stack is empty after performing all these operations, print -1.
[ "2\n2 1 2\n1 0\n", "3\n1 1 2\n2 1 3\n3 0\n", "5\n5 0\n4 0\n3 1 1\n2 1 1\n1 1 2\n" ]
[ "2\n2\n", "2\n3\n2\n", "-1\n-1\n-1\n-1\n2\n" ]
In the first example, after Nikita remembers the operation on the first step, the operation push(2) is the only operation, so the answer is 2. After he remembers the operation pop() which was done before push(2), answer stays the same. In the second example, the operations are push(2), push(3) and pop(). Nikita remembers them in the order they were performed. In the third example Nikita remembers the operations in the reversed order.
[]
31
0
0
62,945
360
Levko and Array
[ "binary search", "dp" ]
null
null
Levko has an array that consists of integers: *a*1,<=*a*2,<=... ,<=*a**n*. But he doesn’t like this array at all. Levko thinks that the beauty of the array *a* directly depends on value *c*(*a*), which can be calculated by the formula: It’s time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most *k* array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible. Help Levko and calculate what minimum number *c*(*a*) he can reach.
The first line contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=2000). The second line contains space-separated integers *a*1,<=*a*2,<=... ,<=*a**n* (<=-<=109<=≀<=*a**i*<=≀<=109).
A single number β€” the minimum value of *c*(*a*) Levko can get.
[ "5 2\n4 7 4 7 4\n", "3 1\n-100 0 100\n", "6 3\n1 2 3 7 8 9\n" ]
[ "0\n", "100\n", "1\n" ]
In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4. In the third sample he can get array: 1, 2, 3, 4, 5, 6.
[ { "input": "5 2\n4 7 4 7 4", "output": "0" }, { "input": "3 1\n-100 0 100", "output": "100" }, { "input": "6 3\n1 2 3 7 8 9", "output": "1" }, { "input": "4 1\n-1000000000 -1000000000 1000000000 1000000000", "output": "1000000000" }, { "input": "10 1\n-6 5 -7 -7 -7 -7 5 -9 4 7", "output": "12" }, { "input": "20 17\n-5 -9 11 -7 -17 -8 0 -14 -20 -15 7 -13 0 -3 -14 0 9 -10 6 -19", "output": "0" }, { "input": "5 1\n-1000000000 1000000000 -1000000000 1000000000 -1000000000", "output": "2000000000" }, { "input": "16 13\n441427217 689669074 -194425155 750543104 6572770 -417122148 483768301 -687067832 703108904 -4865435 -626501708 -222868350 692734658 667640778 259672206 418102760", "output": "2593562" }, { "input": "1 1\n3243243", "output": "0" }, { "input": "1 1\n-1000000000", "output": "0" }, { "input": "2 1\n1234234 -3434", "output": "0" }, { "input": "2 2\n1 5", "output": "0" }, { "input": "4 1\n100 0 7 0", "output": "7" } ]
2,000
4,608,000
0
62,992
993
Compute Power
[ "binary search", "dp", "greedy" ]
null
null
You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume. You 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. If 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. Due to the specifics of the task, you need to print the answer multiplied by 1000 and rounded up.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=50) β€” the number of tasks. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=108), where *a**i* represents the amount of power required for the *i*-th task. The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=100), where *b**i* is the number of processors that *i*-th task will utilize.
Print a single integer value β€” 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.
[ "6\n8 10 9 9 8 10\n1 1 1 1 1 1\n", "6\n8 10 9 9 8 10\n1 10 5 5 1 10\n" ]
[ "9000\n", "1160\n" ]
In 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. In 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.
[ { "input": "6\n8 10 9 9 8 10\n1 1 1 1 1 1", "output": "9000" }, { "input": "6\n8 10 9 9 8 10\n1 10 5 5 1 10", "output": "1160" }, { "input": "1\n1\n100", "output": "10" }, { "input": "50\n83 43 73 75 11 53 6 43 67 38 83 12 70 27 60 13 9 79 61 30 29 71 10 11 95 87 26 26 19 99 13 47 66 93 91 47 90 75 68 3 22 29 59 12 44 41 64 3 99 100\n31 36 69 25 18 33 15 70 12 91 41 44 1 96 80 74 12 80 16 82 88 25 87 17 53 63 3 42 81 6 50 78 34 68 65 78 94 14 53 14 41 97 63 44 21 62 95 37 36 31", "output": "705" }, { "input": "50\n95 86 10 54 82 42 64 88 14 62 2 31 10 80 18 47 73 81 42 98 30 86 65 77 45 28 39 9 88 58 19 70 41 6 33 7 50 34 22 69 37 65 98 89 46 48 9 76 57 64\n87 39 41 23 49 45 91 83 50 92 25 11 76 1 97 42 62 91 2 53 40 11 93 72 66 8 8 62 35 14 57 95 15 80 95 51 60 95 25 70 27 59 51 76 99 100 87 58 24 7", "output": "637" }, { "input": "50\n1 2 7 8 4 9 1 8 3 6 7 2 10 10 4 2 1 7 9 10 10 1 4 7 5 6 1 6 6 2 5 4 5 10 9 9 7 5 5 7 1 3 9 6 2 3 9 10 6 3\n29 37 98 68 71 45 20 38 88 34 85 33 55 80 99 29 28 53 79 100 76 53 18 32 39 29 54 18 56 95 94 60 80 3 24 69 52 91 51 7 36 37 67 28 99 10 99 66 92 48", "output": "78" }, { "input": "5\n99999948 99999931 99999946 99999958 99999965\n43 42 42 24 87", "output": "1744185140" }, { "input": "5\n61 56 77 33 13\n79 40 40 26 56", "output": "863" }, { "input": "5\n99999943 99999973 99999989 99999996 99999953\n2 6 5 2 1", "output": "23076919847" }, { "input": "5\n21581303 73312811 99923326 93114466 53291492\n32 75 75 33 5", "output": "1070425495" }, { "input": "5\n99999950 99999991 99999910 99999915 99999982\n99 55 71 54 100", "output": "1181102060" }, { "input": "5\n81372426 35955615 58387606 77143158 48265342\n9 8 1 6 3", "output": "8455269522" }, { "input": "5\n88535415 58317418 74164690 46139122 28946947\n3 9 3 1 4", "output": "10987486250" }, { "input": "5\n5 4 3 7 3\n7 7 14 57 94", "output": "89" }, { "input": "5\n99 65 93 94 17\n1 5 6 2 3", "output": "18267" }, { "input": "10\n99999917 99999940 99999907 99999901 99999933 99999930 99999964 99999929 99999967 99999947\n93 98 71 41 13 7 24 70 52 70", "output": "1305482246" }, { "input": "10\n7 9 8 9 4 8 5 2 10 5\n6 6 7 8 9 7 10 1 1 7", "output": "977" }, { "input": "10\n68 10 16 26 94 30 17 90 40 26\n36 3 5 9 60 92 55 10 25 27", "output": "871" }, { "input": "10\n4 6 4 4 6 7 2 7 7 8\n35 50 93 63 8 59 46 97 50 88", "output": "75" }, { "input": "10\n99999954 99999947 99999912 99999920 99999980 99999928 99999908 99999999 99999927 99999957\n15 97 18 8 82 21 73 15 28 75", "output": "1621620860" }, { "input": "10\n46 29 60 65 57 95 82 52 39 21\n35 24 8 69 63 27 69 29 94 64", "output": "918" }, { "input": "10\n9 5 1 4 7 6 10 10 3 8\n40 84 53 88 20 33 55 41 34 55", "output": "100" }, { "input": "10\n99999983 99999982 99999945 99999989 99999981 99999947 99999941 99999987 99999965 99999914\n65 14 84 48 71 14 86 65 61 76", "output": "1414140889" }, { "input": "10\n3 10 3 1 3 8 9 7 1 5\n11 18 35 41 47 38 51 68 85 58", "output": "96" }, { "input": "50\n2 10 10 6 8 1 5 10 3 4 3 5 5 8 4 5 8 2 3 3 3 8 8 5 5 5 5 8 2 5 1 5 4 8 3 7 10 8 6 1 4 9 4 9 1 9 2 7 9 9\n10 6 2 2 3 6 5 5 4 1 3 1 2 3 10 10 6 8 7 2 8 5 2 5 4 9 7 5 2 8 3 6 9 8 2 5 8 3 7 3 3 6 3 7 6 10 9 2 9 7", "output": "785" }, { "input": "50\n88 86 31 49 90 52 57 70 39 94 8 90 39 89 56 78 10 80 9 18 95 96 8 57 29 37 13 89 32 99 85 61 35 37 44 55 92 16 69 80 90 34 84 25 26 17 71 93 46 7\n83 95 7 23 34 68 100 89 8 82 36 84 52 42 44 2 25 6 40 72 19 2 75 70 83 3 92 58 51 88 77 75 75 52 15 20 77 63 6 32 39 86 16 22 8 83 53 66 39 13", "output": "751" }, { "input": "50\n84 98 70 31 72 99 83 73 24 28 100 87 3 12 84 85 28 16 53 29 77 64 38 85 44 60 12 58 3 61 88 42 14 83 1 11 57 63 77 37 99 97 50 94 55 3 12 50 27 68\n9 1 4 6 10 5 3 2 4 6 6 9 8 6 1 2 2 1 8 5 8 1 9 1 2 10 2 7 5 1 7 4 7 1 3 6 10 7 3 5 1 3 4 8 4 7 3 3 10 7", "output": "7265" }, { "input": "50\n5 6 10 7 3 8 5 1 5 3 10 7 9 3 9 5 5 4 8 1 6 10 6 7 8 2 2 3 1 4 10 1 2 9 6 6 10 10 2 7 1 6 1 1 7 9 1 8 5 4\n2 2 6 1 5 1 4 9 5 3 5 3 2 1 5 7 4 10 9 8 5 8 1 10 6 7 5 4 10 3 9 4 1 5 6 9 3 8 9 8 2 10 7 3 10 1 1 7 5 3", "output": "736" }, { "input": "1\n100000000\n1", "output": "100000000000" } ]
46
0
0
63,083
681
Gifts by the List
[ "constructive algorithms", "dfs and similar", "graphs", "trees" ]
null
null
Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are *n* men in Sasha's family, so let's number them with integers from 1 to *n*. Each man has at most one father but may have arbitrary number of sons. Man number *A* is considered to be the ancestor of the man number *B* if at least one of the following conditions is satisfied: - *A*<==<=*B*; - the man number *A* is the father of the man number *B*; - there is a man number *C*, such that the man number *A* is his ancestor and the man number *C* is the father of the man number *B*. Of course, if the man number *A* is an ancestor of the man number *B* and *A*<=β‰ <=*B*, then the man number *B* is not an ancestor of the man number *A*. The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens. 1. A list of candidates is prepared, containing some (possibly all) of the *n* men in some order. 1. Each of the *n* men decides to give a gift. 1. In order to choose a person to give a gift to, man *A* looks through the list and picks the first man *B* in the list, such that *B* is an ancestor of *A* and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 1. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the *n* men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above?
In the first line of the input two integers *n* and *m* (0<=≀<=*m*<=&lt;<=*n*<=≀<=100<=000) are givenΒ β€” the number of the men in the Sasha's family and the number of family relations in it respectively. The next *m* lines describe family relations: the (*i*<=+<=1)*th* line consists of pair of integers *p**i* and *q**i* (1<=≀<=*p**i*,<=*q**i*<=≀<=*n*, *p**i*<=β‰ <=*q**i*) meaning that the man numbered *p**i* is the father of the man numbered *q**i*. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*), *i**th* of which means that the man numbered *i* wants to give a gift to the man numbered *a**i*. It is guaranteed that for every 1<=≀<=*i*<=≀<=*n* the man numbered *a**i* is an ancestor of the man numbered *i*.
Print an integer *k* (1<=≀<=*k*<=≀<=*n*)Β β€” the number of the men in the list of candidates, in the first line. Print then *k* pairwise different positive integers not exceeding *n* β€” the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropriate lists, print any of them. If there is no appropriate list print <=-<=1 in the only line.
[ "3 2\n1 2\n2 3\n1 2 1\n", "4 2\n1 2\n3 4\n1 2 3 3\n" ]
[ "-1", "3\n2\n1\n3\n" ]
The first sample explanation: - if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (*a*<sub class="lower-index">1</sub> = *a*<sub class="lower-index">3</sub> = 1); - if there would be no 2 in the list then the second man wish would not be satisfied (*a*<sub class="lower-index">2</sub> = 2); - if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (*a*<sub class="lower-index">2</sub> = 2). - if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (*a*<sub class="lower-index">3</sub> = 1).
[ { "input": "3 2\n1 2\n2 3\n1 2 1", "output": "-1" }, { "input": "4 2\n1 2\n3 4\n1 2 3 3", "output": "3\n2\n1\n3" }, { "input": "1 0\n1", "output": "1\n1" }, { "input": "2 1\n2 1\n2 2", "output": "1\n2" }, { "input": "2 1\n2 1\n1 2", "output": "2\n1\n2" }, { "input": "4 3\n1 2\n2 3\n3 4\n1 1 3 2", "output": "-1" }, { "input": "4 3\n4 3\n3 2\n2 1\n3 4 4 4", "output": "-1" }, { "input": "4 3\n1 2\n2 3\n3 4\n1 1 1 2", "output": "-1" } ]
1,000
19,046,400
0
63,374
323
Tournament-graph
[ "constructive algorithms", "graphs" ]
null
null
In this problem you have to build tournament graph, consisting of *n* vertices, such, that for any oriented pair of vertices (*v*,<=*u*) (*v*<=β‰ <=*u*) there exists a path from vertex *v* to vertex *u* consisting of no more then two edges. A directed graph without self-loops is a tournament, if there is exactly one edge between any two distinct vertices (in one out of two possible directions).
The first line contains an integer *n* (3<=≀<=*n*<=≀<=1000), the number of the graph's vertices.
Print -1 if there is no graph, satisfying the described conditions. Otherwise, print *n* lines with *n* integers in each. The numbers should be separated with spaces. That is adjacency matrix *a* of the found tournament. Consider the graph vertices to be numbered with integers from 1 to *n*. Then *a**v*,<=*u*<==<=0, if there is no edge from *v* to *u*, and *a**v*,<=*u*<==<=1 if there is one. As the output graph has to be a tournament, following equalities must be satisfied: - *a**v*,<=*u*<=+<=*a**u*,<=*v*<==<=1 for each *v*,<=*u* (1<=≀<=*v*,<=*u*<=≀<=*n*;Β *v*<=β‰ <=*u*); - *a**v*,<=*v*<==<=0 for each *v* (1<=≀<=*v*<=≀<=*n*).
[ "3\n", "4\n" ]
[ "0 1 0\n0 0 1\n1 0 0\n", "-1\n" ]
none
[]
78
102,400
0
63,654
720
Slalom
[ "data structures", "dp", "sortings" ]
null
null
Little girl Masha likes winter sports, today she's planning to take part in slalom skiing. The track is represented as a grid composed of *n*<=Γ—<=*m* squares. There are rectangular obstacles at the track, composed of grid squares. Masha must get from the square (1,<=1) to the square (*n*,<=*m*). She can move from a square to adjacent square: either to the right, or upwards. If the square is occupied by an obstacle, it is not allowed to move to that square. One can see that each obstacle can actually be passed in two ways: either it is to the right of Masha's path, or to the left. Masha likes to try all ways to do things, so she would like to know how many ways are there to pass the track. Two ways are considered different if there is an obstacle such that it is to the right of the path in one way, and to the left of the path in the other way. Help Masha to find the number of ways to pass the track. The number of ways can be quite big, so Masha would like to know it modulo 109<=+<=7. The pictures below show different ways to pass the track in sample tests.
The first line of input data contains three positive integers: *n*, *m* and *k* (3<=≀<=*n*,<=*m*<=≀<=106, 0<=≀<=*k*<=≀<=105)Β β€” the size of the track and the number of obstacles. The following *k* lines contain four positive integers each: *x*1, *y*1, *x*2, *y*2 (1<=≀<=*x*1<=≀<=*x*2<=≀<=*n*, 1<=≀<=*y*1<=≀<=*y*2<=≀<=*m*)Β β€” coordinates of bottom left, and top right squares of the obstacle. It is guaranteed that there are no obstacles at squares (1,<=1) and (*n*,<=*m*), and no obstacles overlap (but some of them may touch).
Output one integerΒ β€” the number of ways to pass the track modulo 109<=+<=7.
[ "3 3 0\n", "4 5 1\n2 2 3 4\n", "5 5 3\n2 2 2 3\n4 2 5 2\n4 4 4 4\n" ]
[ "1\n", "2\n", "3\n" ]
none
[]
30
0
0
63,736
362
Insertion Sort
[ "data structures", "dp", "implementation", "math" ]
null
null
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. Petya 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. It 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.
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=5000) β€” the length of the permutation. The second line contains *n* different integers from 0 to *n*<=-<=1, inclusive β€” the actual permutation.
Print 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.
[ "5\n4 0 3 1 2\n", "5\n1 2 3 4 0\n" ]
[ "3 2\n", "3 4\n" ]
In the first sample the appropriate pairs are (0, 3) and (0, 4). In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4).
[ { "input": "5\n4 0 3 1 2", "output": "3 2" }, { "input": "5\n1 2 3 4 0", "output": "3 4" }, { "input": "5\n1 3 4 0 2", "output": "4 5" }, { "input": "10\n9 8 7 6 5 4 3 2 1 0", "output": "28 1" }, { "input": "5\n0 4 1 3 2", "output": "1 1" }, { "input": "6\n3 0 1 4 5 2", "output": "4 5" }, { "input": "3\n0 2 1", "output": "0 1" }, { "input": "3\n1 0 2", "output": "0 1" }, { "input": "3\n1 2 0", "output": "1 2" }, { "input": "3\n2 0 1", "output": "1 2" }, { "input": "3\n2 1 0", "output": "0 1" }, { "input": "7\n4 0 3 5 1 2 6", "output": "5 2" }, { "input": "8\n1 5 4 0 2 7 3 6", "output": "7 3" }, { "input": "9\n1 5 6 3 0 7 2 8 4", "output": "11 4" }, { "input": "10\n8 6 7 9 4 5 2 3 1 0", "output": "24 1" }, { "input": "11\n4 9 1 2 8 5 10 3 0 7 6", "output": "16 1" }, { "input": "12\n2 7 0 1 3 10 4 8 11 6 9 5", "output": "13 1" }, { "input": "13\n5 11 12 10 3 8 4 0 7 9 6 1 2", "output": "39 4" }, { "input": "100\n73 98 9 92 43 77 32 2 29 5 58 59 61 17 10 94 60 12 80 16 24 91 8 70 62 99 47 23 78 19 22 30 44 96 63 74 48 18 69 45 33 88 97 11 31 66 1 82 7 28 27 41 51 0 37 39 71 75 13 26 20 87 25 40 38 46 79 15 14 81 57 90 83 52 67 6 53 68 54 65 86 93 4 34 95 42 85 72 56 36 89 84 35 64 55 76 21 50 49 3", "output": "2137 1" }, { "input": "120\n60 100 55 8 106 57 43 85 103 0 6 20 88 102 53 2 116 31 119 59 86 71 99 81 50 22 74 5 80 13 95 118 49 67 17 63 10 27 61 45 101 76 87 72 113 93 92 47 42 41 35 83 97 51 77 114 69 30 91 44 1 84 107 105 16 70 108 65 64 78 25 39 89 23 40 62 117 4 98 24 104 75 58 3 79 112 11 28 109 38 21 19 37 115 9 54 32 111 46 68 90 48 34 12 96 82 29 73 110 18 26 52 36 94 66 15 14 33 7 56", "output": "3686 1" }, { "input": "150\n48 115 13 9 105 117 41 136 123 32 84 95 62 50 140 106 145 91 57 141 139 35 45 27 129 63 137 10 37 60 44 30 101 119 138 78 22 103 39 134 49 36 25 12 28 67 69 99 148 26 16 87 146 65 8 74 14 38 47 89 81 19 40 11 64 43 110 66 102 3 122 124 100 2 125 42 97 73 121 7 52 23 29 109 1 70 34 108 59 55 127 90 88 144 18 56 17 75 116 5 135 4 15 20 86 94 82 149 126 130 113 33 147 80 54 76 142 96 85 114 112 31 71 133 77 79 93 21 143 128 24 72 68 61 0 131 107 58 132 120 6 46 104 118 53 51 111 83 92 98", "output": "5113 4" } ]
31
307,200
0
63,846
958
Maximum Control (medium)
[ "data structures", "dfs and similar", "graphs", "greedy", "trees" ]
null
null
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. The 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. A 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. Heidi 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.
The first line of the input contains an integer *N* (1<=≀<=*N*<=≀<=105) – the number of planets in the galaxy. The 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<=≀<=*u*,<=*v*<=≀<=*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.
On 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.
[ "3\n1 2\n2 3\n", "4\n1 2\n3 2\n4 2\n" ]
[ "1 3 3 ", "1 3 4 4 " ]
Consider the first example. If *K* = 1, then Heidi can only send one ship to some planet and control it. However, for *K* β‰₯ 2, sending ships to planets 1 and 3 will allow the Resistance to control all planets.
[ { "input": "3\n1 2\n2 3", "output": "1 3 3 " }, { "input": "4\n1 2\n3 2\n4 2", "output": "1 3 4 4 " }, { "input": "19\n2 19\n7 15\n8 10\n16 1\n12 5\n11 5\n6 18\n12 14\n14 15\n2 6\n9 14\n4 17\n16 10\n4 2\n7 18\n3 2\n9 13\n11 10", "output": "1 14 16 17 18 19 19 19 19 19 19 19 19 19 19 19 19 19 19 " }, { "input": "20\n12 10\n5 19\n12 18\n4 2\n3 16\n11 8\n3 2\n17 4\n14 7\n6 5\n10 20\n14 9\n16 12\n9 13\n13 4\n1 15\n11 6\n15 19\n14 8", "output": "1 17 18 19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 " }, { "input": "21\n10 12\n12 9\n16 8\n18 11\n17 15\n4 11\n7 19\n10 14\n10 19\n8 5\n1 5\n20 3\n19 18\n2 20\n19 15\n9 16\n6 4\n4 13\n20 6\n13 21", "output": "1 14 16 18 19 20 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 " }, { "input": "23\n5 14\n6 7\n6 17\n2 4\n6 13\n9 17\n14 10\n23 9\n1 18\n3 12\n15 11\n21 15\n10 23\n4 3\n8 15\n22 18\n22 16\n15 5\n12 22\n4 6\n19 16\n20 10", "output": "1 15 17 18 19 20 21 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 " }, { "input": "24\n19 14\n8 15\n13 4\n18 16\n1 17\n10 3\n22 21\n10 14\n6 11\n9 12\n15 22\n11 3\n21 7\n2 12\n7 4\n4 19\n16 9\n24 17\n5 15\n8 2\n23 10\n20 6\n18 24", "output": "1 21 22 23 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 " } ]
77
7,372,800
0
64,084
0
none
[ "none" ]
null
null
One must train much to do well on wizardry contests. So, there are numerous wizardry schools and magic fees. One of such magic schools consists of *n* tours. A winner of each tour gets a huge prize. The school is organised quite far away, so one will have to take all the prizes home in one go. And the bags that you've brought with you have space for no more than *k* huge prizes. Besides the fact that you want to take all the prizes home, you also want to perform well. You will consider your performance good if you win at least *l* tours. In fact, years of organizing contests proved to the organizers that transporting huge prizes is an issue for the participants. Alas, no one has ever invented a spell that would shrink the prizes... So, here's the solution: for some tours the winner gets a bag instead of a huge prize. Each bag is characterized by number *a**i* β€” the number of huge prizes that will fit into it. You already know the subject of all tours, so you can estimate the probability *p**i* of winning the *i*-th tour. You cannot skip the tour under any circumstances. Find the probability that you will perform well on the contest and will be able to take all won prizes home (that is, that you will be able to fit all the huge prizes that you won into the bags that you either won or brought from home).
The first line contains three integers *n*, *l*, *k* (1<=≀<=*n*<=≀<=200,<=0<=≀<=*l*,<=*k*<=≀<=200) β€” the number of tours, the minimum number of tours to win, and the number of prizes that you can fit in the bags brought from home, correspondingly. The second line contains *n* space-separated integers, *p**i* (0<=≀<=*p**i*<=≀<=100) β€” the probability to win the *i*-th tour, in percents. The third line contains *n* space-separated integers, *a**i* (1<=≀<=*a**i*<=≀<=200) β€” the capacity of the bag that will be awarded to you for winning the *i*-th tour, or else -1, if the prize for the *i*-th tour is a huge prize and not a bag.
Print a single real number β€” the answer to the problem. The answer will be accepted if the absolute or relative error does not exceed 10<=-<=6.
[ "3 1 0\n10 20 30\n-1 -1 2\n", "1 1 1\n100\n123\n" ]
[ "0.300000000000\n", "1.000000000000\n" ]
In the first sample we need either win no tour or win the third one. If we win nothing we wouldn't perform well. So, we must to win the third tour. Other conditions will be satisfied in this case. Probability of wining the third tour is 0.3. In the second sample we win the only tour with probability 1.0, and go back home with bag for it.
[ { "input": "3 1 0\n10 20 30\n-1 -1 2", "output": "0.300000000000" }, { "input": "1 1 1\n100\n123", "output": "1.000000000000" }, { "input": "5 1 2\n36 44 13 83 63\n-1 2 -1 2 1", "output": "0.980387276800" }, { "input": "9 9 2\n91 96 99 60 42 67 46 39 62\n5 -1 2 -1 -1 -1 7 -1 3", "output": "0.016241917181" }, { "input": "1 0 0\n7\n-1", "output": "0.930000000000" }, { "input": "2 1 2\n80 35\n-1 -1", "output": "0.870000000000" }, { "input": "4 1 2\n38 15 28 15\n-1 1 -1 -1", "output": "0.663910000000" }, { "input": "1 0 0\n3\n-1", "output": "0.970000000000" }, { "input": "7 0 3\n58 29 75 56 47 28 27\n-1 -1 1 -1 1 2 -1", "output": "0.997573802464" }, { "input": "46 33 12\n3 26 81 86 20 98 99 59 98 80 43 28 21 91 63 86 75 82 85 36 88 27 48 29 44 25 43 45 54 42 44 66 6 64 74 90 82 10 55 63 100 3 4 86 40 39\n-1 -1 8 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 13 -1 -1 -1 -1 10 -1 5 -1 14 10 -1 -1 -1 2 -1 -1 -1 -1 -1 5 -1 -1 10 -1 -1 -1 -1 5 -1 -1 -1", "output": "0.003687974046" }, { "input": "79 31 70\n76 69 67 55 50 32 53 6 1 20 30 20 59 12 99 6 60 44 95 59 32 91 24 71 36 99 87 83 14 13 19 82 16 16 12 6 29 14 36 8 9 46 80 76 22 100 57 65 13 90 28 20 72 28 14 70 12 12 27 51 74 83 47 0 18 61 47 88 63 1 22 56 8 70 79 23 26 20 91\n12 -1 -1 -1 23 24 7 -1 -1 -1 4 6 10 -1 -1 -1 -1 4 25 -1 15 -1 -1 -1 12 2 17 -1 -1 -1 19 -1 4 23 6 -1 40 -1 17 -1 13 -1 3 11 2 -1 1 -1 -1 -1 -1 9 25 -1 -1 2 3 -1 -1 -1 -1 6 -1 -1 -1 -1 -1 11 26 2 12 -1 -1 -1 5 5 19 20 -1", "output": "0.883830429223" }, { "input": "26 25 5\n5 46 54 97 12 16 22 100 51 88 78 47 93 95 1 80 94 33 39 54 70 92 30 20 72 72\n-1 -1 4 5 6 -1 4 -1 3 -1 4 -1 -1 3 -1 6 10 14 5 5 2 8 10 1 -1 -1", "output": "0.000000011787" }, { "input": "47 38 17\n25 72 78 36 8 35 53 83 23 63 53 85 67 43 48 80 67 0 55 12 67 0 17 19 80 77 28 16 88 0 79 41 50 46 54 31 80 89 77 24 75 52 49 3 58 38 56\n4 -1 -1 7 -1 2 1 -1 -1 -1 -1 -1 -1 -1 17 -1 5 18 -1 -1 -1 -1 3 22 -1 1 -1 12 -1 7 -1 -1 -1 -1 -1 3 8 -1 1 22 -1 -1 5 -1 2 -1 23", "output": "0.000000043571" }, { "input": "57 22 40\n100 99 89 78 37 82 12 100 4 30 23 4 63 33 71 16 88 13 75 32 53 46 54 26 60 41 34 5 83 63 71 46 5 46 29 16 81 74 84 86 81 19 36 21 42 70 49 28 34 37 29 22 24 18 52 48 66\n46 19 4 30 20 4 -1 5 6 19 12 1 24 15 5 24 7 -1 15 9 13 2 -1 5 6 24 10 10 10 7 7 5 14 1 23 20 8 -1 10 28 3 11 24 20 3 10 3 8 1 7 6 1 2 -1 23 6 2", "output": "0.968076497396" }, { "input": "69 61 48\n55 30 81 52 50 99 58 15 6 98 95 56 97 71 38 87 28 88 22 73 51 21 78 7 73 28 47 36 74 48 49 8 69 83 63 72 53 36 19 48 91 47 2 74 64 40 14 50 41 57 45 97 9 84 50 57 91 24 24 67 18 63 77 96 38 10 17 55 43\n3 8 -1 -1 39 -1 3 -1 10 -1 -1 -1 26 12 38 8 14 24 2 11 6 9 27 32 20 6 -1 13 10 -1 20 13 13 -1 18 6 27 5 19 19 39 9 14 -1 35 -1 3 17 7 11 -1 -1 17 44 7 14 9 29 1 -1 24 1 16 4 14 3 2 -1 -1", "output": "0.000000000000" }, { "input": "12 6 12\n98 44 95 72 87 100 72 60 34 5 30 78\n6 1 3 1 3 1 1 1 1 3 1 5", "output": "0.957247046683" }, { "input": "66 30 30\n7 86 54 73 90 31 86 4 28 49 87 44 23 58 84 0 43 37 90 31 23 57 11 70 86 25 53 75 65 20 23 6 33 66 65 4 54 74 74 58 93 49 80 35 94 71 80 97 39 39 59 50 62 65 88 43 60 53 80 23 71 61 57 100 71 3\n-1 25 25 9 -1 7 23 3 23 8 37 14 33 -1 -1 11 -1 -1 5 40 21 -1 4 -1 19 -1 1 9 24 -1 -1 -1 -1 -1 5 2 24 -1 3 -1 2 3 -1 -1 -1 -1 -1 -1 8 28 2 -1 1 -1 -1 30 -1 10 42 17 22 -1 -1 -1 -1 -1", "output": "0.965398798999" }, { "input": "82 77 11\n100 56 83 61 74 15 44 60 25 4 78 16 85 93 4 10 40 16 74 89 73 20 75 20 57 48 19 46 44 43 48 40 95 60 97 63 48 50 38 23 23 23 16 75 18 72 63 31 18 52 78 80 51 34 62 5 18 60 21 36 96 45 74 69 29 49 22 91 21 78 87 70 78 57 75 18 17 75 2 53 45 97\n55 57 -1 -1 -1 -1 38 -1 -1 19 37 3 -1 -1 -1 -1 -1 -1 10 11 29 9 3 14 -1 -1 -1 35 -1 1 6 24 7 -1 -1 4 2 32 -1 -1 2 12 3 -1 39 5 -1 5 3 2 20 21 -1 -1 17 -1 7 35 24 2 22 -1 -1 -1 19 -1 -1 43 25 24 6 5 25 1 -1 7 13 10 -1 22 12 5", "output": "0.000000000000" }, { "input": "4 0 3\n45 54 15 33\n1 -1 -1 -1", "output": "1.000000000000" }, { "input": "17 5 17\n69 43 30 9 17 75 43 42 3 10 47 90 82 47 1 51 31\n-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1", "output": "0.924223127356" }, { "input": "38 35 36\n45 27 85 64 37 79 43 16 92 6 16 83 61 79 67 52 44 35 80 79 39 29 68 6 88 84 51 56 94 46 15 50 81 53 88 25 26 59\n2 3 -1 13 -1 7 -1 7 3 14 -1 -1 4 -1 2 1 10 -1 -1 -1 3 -1 -1 12 -1 9 -1 5 10 1 3 12 -1 -1 -1 -1 12 8", "output": "0.000000004443" }, { "input": "2 1 2\n92 42\n-1 -1", "output": "0.953600000000" }, { "input": "33 9 19\n32 7 0 39 72 86 95 87 33 6 65 79 85 36 87 80 63 56 62 20 20 96 28 63 38 26 76 10 16 16 99 60 49\n-1 -1 -1 -1 -1 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 9 -1 -1 -1 -1 11 -1 -1 -1 -1 -1 -1 -1", "output": "0.998227991691" }, { "input": "57 12 37\n27 40 10 0 81 52 8 79 61 9 90 26 24 22 8 10 0 93 63 74 65 46 64 23 27 37 6 21 5 9 40 53 66 78 65 10 53 1 36 90 5 0 25 60 76 62 36 79 71 29 7 72 45 43 34 35 72\n-1 10 13 -1 5 -1 -1 5 13 -1 16 9 3 15 -1 23 15 42 8 -1 14 28 -1 19 5 6 3 -1 5 -1 -1 -1 14 7 -1 -1 30 12 16 11 16 9 3 25 -1 -1 17 -1 39 29 10 2 18 24 7 -1 3", "output": "0.999960060813" }, { "input": "86 81 36\n84 44 92 12 39 24 70 73 17 43 50 59 9 89 87 67 80 35 7 49 6 23 1 19 2 70 40 84 4 28 18 60 13 97 3 76 69 5 13 26 55 27 21 62 17 3 6 40 55 69 16 56 13 55 20 72 35 13 38 24 14 73 73 92 75 46 92 39 22 86 3 70 12 95 48 40 37 69 4 83 42 9 4 63 66 56\n16 5 2 16 -1 21 11 -1 1 48 -1 17 -1 -1 2 12 20 34 41 12 30 3 -1 31 42 45 26 30 34 29 -1 3 18 16 19 24 2 7 -1 38 28 -1 18 24 3 41 16 1 46 18 8 12 6 34 8 -1 -1 3 -1 3 3 6 11 -1 13 -1 1 11 12 -1 2 4 55 17 -1 -1 -1 16 7 -1 15 -1 4 23 38 2", "output": "0.000000000000" }, { "input": "11 6 2\n54 64 95 25 45 65 97 14 0 19 20\n2 2 2 3 1 2 2 3 4 1 3", "output": "0.337088638195" }, { "input": "76 43 67\n20 91 34 79 34 62 50 99 35 22 92 32 77 48 2 90 27 56 65 85 88 58 63 99 88 89 45 82 78 5 70 7 100 72 75 1 59 32 30 89 81 28 99 27 95 67 89 65 63 63 63 77 80 32 1 81 25 64 29 20 7 62 60 51 58 95 68 78 98 78 97 68 68 96 95 74\n9 24 -1 -1 13 -1 11 23 -1 -1 3 9 -1 -1 8 47 -1 -1 16 -1 10 -1 34 -1 12 23 -1 4 26 -1 13 11 9 11 -1 -1 -1 18 -1 2 13 30 -1 5 -1 9 -1 -1 28 29 -1 -1 8 40 -1 -1 -1 -1 -1 -1 10 24 -1 36 18 -1 -1 -1 -1 29 -1 6 10 -1 -1 2", "output": "0.865190370143" }, { "input": "1 2 43\n18\n-1", "output": "0.000000000000" }, { "input": "5 3 200\n100 100 100 100 100\n200 200 200 200 200", "output": "1.000000000000" }, { "input": "2 2 0\n50 50\n1 -1", "output": "0.250000000000" }, { "input": "3 1 200\n20 30 40\n-1 -1 -1", "output": "0.664000000000" }, { "input": "2 1 200\n20 30\n-1 -1", "output": "0.440000000000" }, { "input": "1 0 200\n50\n-1", "output": "1.000000000000" }, { "input": "3 1 0\n20 20 20\n2 -1 -1", "output": "0.200000000000" }, { "input": "4 3 0\n100 100 100 100\n200 200 200 200", "output": "1.000000000000" } ]
60
0
0
64,275
959
Mahmoud and Ehab and yet another xor task
[ "bitmasks", "dp", "math", "matrices" ]
null
null
Ehab has an array *a* of *n* integers. He likes the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) and he likes to bother Mahmoud so he came up with a problem. He gave Mahmoud *q* queries. In each of them, he gave Mahmoud 2 integers *l* and *x*, and asked him to find the number of subsequences of the first *l* elements of the array such that their bitwise-xor sum is *x*. Can you help Mahmoud answer the queries? A subsequence can contain elements that are not neighboring.
The first line contains integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=105), the number of elements in the array and the number of queries. The next line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=≀<=*a**i*<=&lt;<=220), the elements of the array. The next *q* lines, each contains integers *l* and *x* (1<=≀<=*l*<=≀<=*n*, 0<=≀<=*x*<=&lt;<=220), representing the queries.
For each query, output its answer modulo 109<=+<=7 in a newline.
[ "5 5\n0 1 2 3 4\n4 3\n2 0\n3 7\n5 7\n5 8\n", "3 2\n1 1 1\n3 1\n2 0\n" ]
[ "4\n2\n0\n4\n0\n", "4\n2\n" ]
The bitwise-xor sum of the empty set is 0 and the bitwise-xor sum of a set containing one element is that element itself.
[ { "input": "5 5\n0 1 2 3 4\n4 3\n2 0\n3 7\n5 7\n5 8", "output": "4\n2\n0\n4\n0" }, { "input": "3 2\n1 1 1\n3 1\n2 0", "output": "4\n2" }, { "input": "20 20\n353123 353123 677328 353123 0 997043 677328 420884 968991 566539 420884 801220 1030642 937882 762558 599450 196420 299659 101896 600581\n14 764540\n10 140147\n20 631492\n15 403662\n14 730429\n11 566539\n2 353123\n9 353123\n18 463456\n15 714519\n16 418098\n8 623050\n10 369379\n9 801220\n11 705280\n3 156540\n4 507911\n10 338945\n11 496568\n1 360148", "output": "256\n0\n1024\n0\n256\n128\n2\n32\n256\n0\n0\n0\n0\n32\n0\n0\n0\n0\n128\n0" }, { "input": "20 20\n306892 1035619 634501 191090 883124 315497 52694 533023 671659 798312 656827 376229 777308 233774 1044523 889658 1001600 326577 1019534 723866\n19 454109\n12 293130\n11 1024526\n9 97284\n8 418790\n1 306892\n16 795066\n5 1020870\n20 526791\n18 338417\n17 990717\n11 101307\n12 792824\n18 108354\n1 158141\n20 471906\n14 180513\n5 100587\n1 752042\n15 704444", "output": "512\n4\n2\n1\n1\n1\n0\n1\n1024\n0\n128\n0\n4\n256\n0\n1024\n0\n0\n0\n0" }, { "input": "5 6\n4 4 0 14 14\n2 2\n4 10\n5 4\n3 0\n5 4\n2 4", "output": "0\n4\n8\n4\n8\n2" }, { "input": "11 3\n8 290 0 298 8 290 290 290 298 8 0\n2 290\n9 311\n10 487", "output": "1\n0\n0" }, { "input": "20 4\n3543 10760 559 23602 14947 30833 5084 17919 10471 10689 21846 11925 23128 11184 24426 19945 19340 4464 9112 21680\n2 4015\n2 18599\n7 31496\n10 11700", "output": "0\n0\n0\n0" }, { "input": "9 7\n0 0 2 2 3 2 3 0 1\n3 3\n9 1\n7 0\n4 1\n8 3\n4 1\n9 0", "output": "0\n128\n32\n0\n64\n0\n128" }, { "input": "13 18\n2 0 0 0 0 0 1 1 1 0 3 3 1\n5 2\n11 2\n1 4\n12 7\n11 4\n2 2\n6 1\n13 0\n2 5\n9 1\n5 5\n2 2\n7 4\n11 4\n8 5\n10 4\n3 3\n12 6", "output": "16\n512\n0\n0\n0\n2\n0\n2048\n0\n128\n0\n2\n0\n0\n0\n0\n0\n0" }, { "input": "2 2\n3 1\n2 3\n1 0", "output": "1\n1" }, { "input": "3 7\n2 3 3\n3 1\n2 0\n2 1\n1 0\n3 2\n2 3\n1 0", "output": "2\n1\n1\n1\n2\n1\n1" }, { "input": "3 10\n3 6 5\n2 4\n3 1\n2 4\n3 0\n2 5\n2 5\n2 6\n2 0\n1 3\n2 7", "output": "0\n0\n0\n2\n1\n1\n1\n1\n1\n0" }, { "input": "3 18\n0 1 2\n1 0\n3 3\n1 0\n2 1\n2 0\n2 1\n3 5\n3 3\n2 2\n3 1\n2 4\n2 1\n2 0\n2 2\n1 7\n1 0\n3 2\n3 4", "output": "2\n2\n2\n2\n2\n2\n0\n2\n0\n2\n0\n2\n2\n0\n0\n2\n2\n0" }, { "input": "4 3\n3 10 9 0\n1 3\n4 9\n2 9", "output": "1\n4\n1" }, { "input": "9 10\n10 16 16 10 0 0 26 0 16\n1 14\n1 10\n2 16\n1 21\n8 16\n8 26\n6 16\n2 26\n3 10\n7 22", "output": "0\n1\n1\n0\n64\n64\n16\n1\n2\n0" }, { "input": "5 8\n0 0 0 14 6\n2 12\n4 3\n4 5\n5 0\n1 13\n5 7\n4 12\n2 0", "output": "0\n0\n0\n8\n0\n0\n0\n4" }, { "input": "8 2\n0 0 0 11 2 36 61 2\n8 53\n7 10", "output": "0\n0" }, { "input": "2 4\n1 2\n1 3\n2 3\n1 2\n1 3", "output": "0\n1\n0\n0" }, { "input": "3 5\n3 1 7\n3 2\n3 1\n2 3\n3 0\n2 2", "output": "1\n1\n1\n1\n1" }, { "input": "6 7\n4 4 4 3 1 3\n4 4\n6 4\n1 3\n2 4\n3 0\n6 4\n1 3", "output": "4\n8\n0\n2\n4\n8\n0" }, { "input": "3 8\n3 2 4\n3 4\n2 7\n1 2\n3 1\n3 5\n3 1\n2 7\n2 4", "output": "1\n0\n0\n1\n1\n1\n0\n0" }, { "input": "3 15\n4 3 1\n3 2\n3 2\n3 7\n1 6\n3 7\n2 0\n1 0\n1 3\n1 4\n3 3\n3 1\n1 4\n1 4\n2 6\n1 0", "output": "1\n1\n1\n0\n1\n1\n1\n0\n1\n1\n1\n1\n1\n0\n1" }, { "input": "4 19\n2 5 6 4\n1 3\n4 4\n1 0\n1 7\n4 3\n4 5\n3 2\n4 1\n1 0\n2 2\n3 2\n1 0\n4 1\n4 1\n2 6\n1 2\n2 6\n2 4\n1 7", "output": "0\n2\n1\n0\n2\n2\n1\n2\n1\n1\n1\n1\n2\n2\n0\n1\n0\n0\n0" }, { "input": "5 9\n5 4 7 4 7\n2 4\n3 5\n2 3\n5 7\n3 2\n3 5\n2 5\n1 5\n5 7", "output": "1\n1\n0\n4\n1\n1\n1\n1\n4" }, { "input": "9 5\n0 0 30 30 0 0 52 43 53\n4 45\n4 23\n2 63\n2 0\n1 0", "output": "0\n0\n0\n4\n2" }, { "input": "10 10\n0 31 62 32 0 62 32 30 30 33\n2 34\n7 49\n10 31\n5 46\n8 12\n4 24\n6 15\n7 52\n9 30\n2 0", "output": "0\n0\n128\n0\n0\n0\n0\n0\n64\n2" }, { "input": "6 6\n4 3 11 2 2 4\n3 1\n5 13\n3 8\n3 4\n3 8\n1 8", "output": "0\n2\n1\n1\n1\n0" }, { "input": "8 5\n0 0 127 127 48 0 66 253\n6 79\n1 111\n4 0\n7 213\n1 126", "output": "16\n0\n8\n0\n0" }, { "input": "17 5\n234 55 212 253 148 148 212 163 96 55 0 163 157 131 94 119 189\n2 55\n11 172\n8 41\n7 135\n14 180", "output": "1\n0\n8\n0\n512" }, { "input": "10 9\n286 621 876 0 31 883 31 257 163 670\n8 257\n6 196\n3 883\n2 286\n1 286\n8 252\n2 572\n6 565\n6 626", "output": "32\n0\n1\n1\n1\n0\n0\n0\n8" }, { "input": "7 10\n3 2 1 0 2 3 3\n6 1\n5 1\n4 3\n3 0\n6 2\n3 3\n1 3\n4 0\n7 1\n7 3", "output": "16\n8\n4\n2\n16\n2\n1\n4\n32\n32" }, { "input": "13 16\n201 2016 1726 1340 2030 1712 2030 0 1650 983 1299 838 1670\n3 1995\n3 1412\n4 667\n10 78\n13 1377\n12 1388\n6 837\n4 2016\n6 1531\n1 0\n12 2012\n10 905\n12 1040\n10 284\n3 2016\n12 1819", "output": "0\n0\n0\n0\n8\n8\n2\n1\n2\n1\n0\n8\n8\n0\n1\n0" }, { "input": "7 10\n1 7 2 3 0 3 2\n5 0\n1 0\n6 2\n7 1\n3 2\n3 7\n1 2\n4 2\n5 3\n3 4", "output": "4\n1\n8\n16\n1\n1\n0\n2\n4\n1" }, { "input": "9 10\n0 0 72 177 1 167 95 72 23\n9 0\n1 149\n2 0\n6 94\n7 176\n6 177\n2 0\n1 198\n6 239\n9 221", "output": "32\n0\n4\n4\n8\n4\n4\n0\n4\n0" } ]
46
0
0
64,369
842
Nikita and game
[ "binary search", "dfs and similar", "divide and conquer", "graphs", "trees" ]
null
null
Nikita plays a new computer game. There are *m* levels in this game. In the beginning of each level a new class appears in the game; this class is a child-class of the class *y**i* (and *y**i* is called parent-class for this new class). Thus, the classes form a tree. Initially there is only one class with index 1. Changing the class to its neighbour (child-class or parent-class) in the tree costs 1 coin. You can not change the class back. The cost of changing the class *a* to the class *b* is equal to the total cost of class changes on the path from *a* to *b* in the class tree. Suppose that at *i*Β -th level the maximum cost of changing one class to another is *x*. For each level output the number of classes such that for each of these classes there exists some other class *y*, and the distance from this class to *y* is exactly *x*.
First line contains one integer number *m*Β β€” number of queries (1<=≀<=*m*<=≀<=3Β·105). Next *m* lines contain description of queries. *i*Β -th line (1<=≀<=*i*<=≀<=*m*) describes the *i*Β -th level and contains an integer *y**i*Β β€” the index of the parent-class of class with index *i*<=+<=1 (1<=≀<=*y**i*<=≀<=*i*).
Suppose that at *i*Β -th level the maximum cost of changing one class to another is *x*. For each level output the number of classes such that for each of these classes there exists some other class *y*, and the distance from this class to *y* is exactly *x*.
[ "4\n1\n1\n2\n1\n", "4\n1\n1\n2\n3\n" ]
[ "2\n2\n2\n3\n", "2\n2\n2\n2\n" ]
none
[]
30
0
0
64,481
708
Incorrect Flow
[ "flows" ]
null
null
At the entrance examination for the magistracy of the MSU Cyber-Mechanics Department Sasha got the question about Ford-Fulkerson algorithm. He knew the topic perfectly as he worked with it many times on programming competition. As the task for the question he was given a network with partially build flow that he had to use in order to demonstrate the workflow of the algorithm. He quickly finished to write the text and took a look at the problem only to understand that the given network is incorrect! Suppose you are given a directed graph *G*(*V*,<=*E*) with two special nodes *s* and *t* called source and sink. We denote as *n* the number of nodes in the graph, i.e. *n*<==<=|*V*| and *m* stands for the number of directed edges in the graph, i.e. *m*<==<=|*E*|. For the purpose of this problem we always consider node 1 to be the source and node *n* to be the sink. In addition, for each edge of the graph *e* we define the capacity function *c*(*e*) and flow function *f*(*e*). Function *f*(*e*) represents the correct flow if the following conditions are satisfied: 1. For each edge the flow is non-negative and does not exceed capacity *c*(*e*), i.e. 0<=≀<=*f*(*e*)<=≀<=*c*(*e*). 1. For each node , that is not source or sink (*v*<=β‰ <=*s* and *v*<=β‰ <=*t*) the sum of flows of all edges going in *v* is equal to the sum of the flows among all edges going out from *v*. In other words, there is no flow stuck in *v*. It was clear that as the exam was prepared last night and there are plenty of mistakes in the tasks. Sasha asked one of the professors to fix the network or give the correct task, but the reply was that the magistrate student should be able to fix the network himself. As the professor doesn't want the task to become easier, he asks Sasha to fix the network in a such way that the total number of changes is minimum possible. Sasha is not allowed to remove edges, add new ones or reverse the direction of existing edges. The only thing he is able to do is to change capacity function *c*(*e*) and flow function *f*(*e*). Moreover, all the values should remain non-negative integers. There is no requirement on the flow to be maximum in any sense. Find the minimum possible total change of the functions *f*(*e*) and *c*(*e*) that Sasha has to make in order to make the flow correct. The total change is defined as the sum of absolute differences, i.e. if new functions are *f*<=*<=(*e*) and *c*<=*<=(*e*), then the total change is .
The first line of the input contains two integers *n* and *m* (2<=≀<=*n*<=≀<=100, 0<=≀<=*m*<=≀<=100)Β β€” the number of nodes and edges in the graph respectively. Each of the following *m* lines contains the description of the edges, consisting of four integers *u**i*, *v**i*, *c**i* and *f**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*, 0<=≀<=*c**i*,<=*f**i*<=≀<=1<=000<=000)Β β€” index of the node the edges starts from, the index of the node the edge goes to, current capacity and flow value. Node number 1 is the source, and node number *n* is the sink. It's guaranteed that no edge goes to the source, and no edges starts in the sink. Given graph contains no self-loops but may contain multiple edges.
Print one integerΒ β€” the minimum total sum of changes that Sasha has to do in order to get the correct flow description.
[ "2 1\n1 2 2 1\n", "2 1\n1 2 1 2\n", "3 3\n1 2 1 1\n2 3 2 2\n1 3 3 3\n", "4 2\n2 3 1 1\n3 2 1 1\n" ]
[ "0\n", "1\n", "1\n", "0\n" ]
In the first sample, the flow is initially correct. Note, that the flow is not maximum, but this is not required. In the second sample, the flow value of the only edge is greater than its capacity. There are two ways to fix this: either increase the capacity up to 2 or reduce the flow down to 1. In the third sample, there is only 1 unit of flow coming to vertex 2, but there are 2 units going out of it. One of the possible solutions is to reduce the value of the flow on the second edge by 1. In the fourth sample, there is isolated circulation of flow, but this description is correct by definition.
[]
30
0
0
64,592
898
Restoring the Expression
[ "brute force", "hashing", "math" ]
null
null
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Β β€” b and the right partΒ β€” c), - all the three parts a, b and c do not contain leading zeros, - it is true that a+b=c. It is guaranteed that in given tests answer always exists.
The first line contains a non-empty string consisting of digits. The length of the string does not exceed 106.
Output the restored expression. If there are several solutions, you can print any of them. Note that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be. Do not separate numbers and operation signs with spaces. Strictly follow the output format given in the examples. If you remove symbol '+' and symbol '=' from answer string you should get a string, same as string from the input data.
[ "12345168\n", "099\n", "199100\n", "123123123456456456579579579\n" ]
[ "123+45=168\n", "0+9=9\n", "1+99=100\n", "123123123+456456456=579579579\n" ]
none
[ { "input": "12345168", "output": "123+45=168" }, { "input": "099", "output": "0+9=9" }, { "input": "199100", "output": "1+99=100" }, { "input": "123123123456456456579579579", "output": "123123123+456456456=579579579" }, { "input": "112", "output": "1+1=2" }, { "input": "101", "output": "1+0=1" }, { "input": "19999999999999999999991000000000000000000000", "output": "1+999999999999999999999=1000000000000000000000" }, { "input": "011", "output": "0+1=1" }, { "input": "178", "output": "1+7=8" }, { "input": "56772", "output": "5+67=72" }, { "input": "24823441936901", "output": "2482+34419=36901" }, { "input": "90001667868290010349", "output": "90001667+8682=90010349" }, { "input": "2099339395097728908986834555921320993394819322848302", "output": "20993393950977289089+868345559213=20993394819322848302" }, { "input": "000", "output": "0+0=0" }, { "input": "814752277691991477306861346922924221557534659480258977017038624458370459477306861346922924221557534659480258977017853376736062450", "output": "814752277691991+477306861346922924221557534659480258977017038624458370459=477306861346922924221557534659480258977017853376736062450" }, { "input": "10111110001111101", "output": "101+1111000=1111101" }, { "input": "1101111000111111", "output": "110111+1000=111111" }, { "input": "101011100101111", "output": "101011+100=101111" }, { "input": "10001100110110111", "output": "10001+100110=110111" }, { "input": "1011100011101", "output": "101+11000=11101" }, { "input": "10101110100111111", "output": "101011+10100=111111" }, { "input": "989121001", "output": "989+12=1001" }, { "input": "1999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999910000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "1+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" }, { "input": "999091008", "output": "99+909=1008" }, { "input": "100277221476489181970323375139380435110027722147648918197032337513938043511002772214764891819703233751393804351100277221476489181970323375139380435120055444295297836394064675027876087022005544429529783639406467502787608702", "output": "10027722147648918197032337513938043511002772214764891819703233751393804351+10027722147648918197032337513938043511002772214764891819703233751393804351=20055444295297836394064675027876087022005544429529783639406467502787608702" } ]
93
307,200
0
64,662
220
Little Elephant and Inversions
[ "data structures", "two pointers" ]
null
null
The Little Elephant has array *a*, consisting of *n* positive integers, indexed from 1 to *n*. Let's denote the number with index *i* as *a**i*. The Little Elephant wants to count, how many pairs of integers *l* and *r* are there, such that 1<=≀<=*l*<=&lt;<=*r*<=≀<=*n* and sequence *b*<==<=*a*1*a*2... *a**l**a**r**a**r*<=+<=1... *a**n* has no more than *k* inversions. An inversion in sequence *b* is a pair of elements of the sequence *b*, that change their relative order after a stable sorting of the sequence. In other words, an inversion is a pair of integers *i* and *j*, such that 1<=≀<=*i*<=&lt;<=*j*<=≀<=|*b*| and *b**i*<=&gt;<=*b**j*, where |*b*| is the length of sequence *b*, and *b**j* is its *j*-th element. Help the Little Elephant and count the number of the described pairs.
The first line contains two integers *n* and *k* (2<=≀<=*n*<=≀<=105,<=0<=≀<=*k*<=≀<=1018) β€” the size of array *a* and the maximum allowed number of inversions respectively. The next line contains *n* positive integers, separated by single spaces, *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” elements of array *a*. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
In a single line print a single number β€” the answer to the problem.
[ "3 1\n1 3 2\n", "5 2\n1 3 2 1 7\n" ]
[ "3\n", "6\n" ]
none
[ { "input": "3 1\n1 3 2", "output": "3" }, { "input": "5 2\n1 3 2 1 7", "output": "6" }, { "input": "7 3\n1 7 6 4 9 5 3", "output": "6" }, { "input": "5 0\n1 2 3 4 5", "output": "10" }, { "input": "2 1\n2 1", "output": "1" }, { "input": "3 1000000000000\n3 2 1", "output": "3" }, { "input": "10 5\n1 4 4 2 3 7 6 5 1 2", "output": "10" }, { "input": "10 10\n7 5 5 5 9 10 9 8 7 5", "output": "22" }, { "input": "7 1\n10 38 46 40 88 5 94", "output": "6" }, { "input": "20 7\n5 10 23 7 24 7 15 11 13 18 18 18 8 20 5 16 7 25 2 22", "output": "17" }, { "input": "40 1000000000000000000\n83 35 47 18 96 63 24 91 15 100 40 23 20 34 65 22 52 87 55 19 11 73 45 28 60 61 24 42 30 43 65 75 31 84 100 12 69 98 49 25", "output": "780" }, { "input": "74 9\n23 15 38 22 47 8 2 38 17 3 39 10 33 26 19 27 11 15 42 18 44 22 47 44 18 29 25 6 4 44 12 44 30 26 5 41 18 6 17 15 40 28 44 37 27 32 31 13 1 13 3 19 39 3 27 23 25 27 30 31 14 42 31 3 1 26 40 42 26 33 43 11 33 15", "output": "15" }, { "input": "100 25\n457 226 629 226 147 284 72 23 247 76 972 829 173 591 581 373 533 474 521 219 181 278 890 27 345 478 595 678 971 157 776 156 970 955 584 786 692 774 228 459 118 419 671 24 128 76 677 66 923 800 870 124 420 528 609 131 88 118 19 807 288 973 896 305 268 486 830 149 529 399 894 71 101 971 620 671 367 206 252 103 905 528 429 185 768 103 407 371 376 807 638 44 193 835 134 542 47 50 238 668", "output": "45" }, { "input": "128 1\n96 56 71 72 37 31 41 55 63 79 64 59 32 8 55 17 25 97 11 38 100 76 80 99 1 17 23 8 42 33 28 86 60 10 11 7 93 49 96 90 2 94 21 53 88 39 64 81 8 55 67 42 69 79 9 13 66 72 36 3 10 61 15 56 6 75 63 87 87 49 71 22 74 7 53 52 17 13 97 69 69 31 66 14 72 84 18 59 61 1 96 86 61 52 73 7 13 59 66 83 19 88 25 59 12 28 61 100 20 54 25 14 27 18 89 23 8 24 2 50 70 32 100 58 76 90 91 78", "output": "1" }, { "input": "50 1000000000\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "1225" }, { "input": "6 10\n4 3 2 4 1 1", "output": "10" }, { "input": "2 2\n1 2", "output": "1" }, { "input": "2 0\n2 1", "output": "0" } ]
310
0
0
64,675
630
Indivisibility
[ "math", "number theory" ]
null
null
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. A 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.
The only line of the input contains one integer *n* (1<=≀<=*n*<=≀<=1018) β€” the prediction on the number of people who will buy the game.
Output one integer showing how many numbers from 1 to *n* are not divisible by any number from 2 to 10.
[ "12\n" ]
[ "2" ]
none
[ { "input": "12", "output": "2" }, { "input": "2519", "output": "576" }, { "input": "2521", "output": "577" }, { "input": "1", "output": "1" }, { "input": "314159265", "output": "71807832" }, { "input": "718281828459045235", "output": "164178703647781768" }, { "input": "1000000000000000000", "output": "228571428571428571" }, { "input": "987654321234567890", "output": "225749559139329804" }, { "input": "3628800", "output": "829440" }, { "input": "504000000000000000", "output": "115200000000000000" } ]
46
0
0
64,880
409
On a plane
[ "*special", "geometry" ]
null
null
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1000) β€” the number of points on a plane. Each of the next *n* lines contains two real coordinates *x**i* and *y**i* of the point, specified with exactly 2 fractional digits. All coordinates are between <=-<=1000 and 1000, inclusive. Output a single real number ΞΈ β€” the answer to the problem statement. The absolute or relative error of your answer should be at most 10<=-<=2.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1000) β€” the number of points on a plane. Each of the next *n* lines contains two real coordinates *x**i* and *y**i* of the point, specified with exactly 2 fractional digits. All coordinates are between <=-<=1000 and 1000, inclusive.
Output a single real number ΞΈ β€” the answer to the problem statement. The absolute or relative error of your answer should be at most 10<=-<=2.
[ "8\n-2.14 2.06\n-1.14 2.04\n-2.16 1.46\n-2.14 0.70\n-1.42 0.40\n-0.94 -0.48\n-1.42 -1.28\n-2.16 -1.62\n", "5\n2.26 1.44\n2.28 0.64\n2.30 -0.30\n1.58 0.66\n3.24 0.66\n", "8\n6.98 2.06\n6.40 1.12\n5.98 0.24\n5.54 -0.60\n7.16 0.30\n7.82 1.24\n8.34 0.24\n8.74 -0.76\n", "5\n10.44 2.06\n10.90 0.80\n11.48 -0.48\n12.06 0.76\n12.54 2.06\n", "8\n16.94 2.42\n15.72 2.38\n14.82 1.58\n14.88 0.50\n15.76 -0.16\n16.86 -0.20\n17.00 0.88\n16.40 0.92\n", "7\n20.62 3.00\n21.06 2.28\n21.56 1.36\n21.66 0.56\n21.64 -0.52\n22.14 2.32\n22.62 3.04\n" ]
[ "5.410\n", "5.620\n", "5.480\n", "6.040\n", "6.040\n", "6.720\n" ]
none
[ { "input": "8\n-2.14 2.06\n-1.14 2.04\n-2.16 1.46\n-2.14 0.70\n-1.42 0.40\n-0.94 -0.48\n-1.42 -1.28\n-2.16 -1.62", "output": "5.410" }, { "input": "5\n2.26 1.44\n2.28 0.64\n2.30 -0.30\n1.58 0.66\n3.24 0.66", "output": "5.620" }, { "input": "8\n6.98 2.06\n6.40 1.12\n5.98 0.24\n5.54 -0.60\n7.16 0.30\n7.82 1.24\n8.34 0.24\n8.74 -0.76", "output": "5.480" }, { "input": "5\n10.44 2.06\n10.90 0.80\n11.48 -0.48\n12.06 0.76\n12.54 2.06", "output": "6.040" }, { "input": "8\n16.94 2.42\n15.72 2.38\n14.82 1.58\n14.88 0.50\n15.76 -0.16\n16.86 -0.20\n17.00 0.88\n16.40 0.92", "output": "6.040" }, { "input": "7\n20.62 3.00\n21.06 2.28\n21.56 1.36\n21.66 0.56\n21.64 -0.52\n22.14 2.32\n22.62 3.04", "output": "6.720" }, { "input": "14\n99.19 -882.27\n468.09 310.41\n-539.17 665.55\n-355.65 -90.01\n490.35 -966.88\n-102.77 252.03\n981.63 -976.33\n-363.05 -435.09\n-44.93 -37.28\n947.69 530.68\n49.38 -299.65\n503.33 684.17\n199.13 328.89\n31.24 65.36", "output": "-55.744" }, { "input": "1\n1.00 1.01", "output": "6.010" }, { "input": "1\n0.00 0.01", "output": "5.010" }, { "input": "1\n1000.00 999.99", "output": "1004.990" }, { "input": "1\n792.52 879.16", "output": "884.160" }, { "input": "2\n792.70 540.07\n-865.28 -699.23", "output": "-74.580" }, { "input": "3\n792.88 200.98\n-5.87 -263.79\n-134.68 900.15", "output": "284.113" } ]
109
307,200
3
65,294
37
Trial for Chief
[ "graphs", "greedy", "shortest paths" ]
E. Trial for Chief
2
256
Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chroniclers of that time. For example, they learned how the chief of the ancient Berland tribe was chosen. As soon as enough pretenders was picked, the following test took place among them: the chief of the tribe took a slab divided by horizontal and vertical stripes into identical squares (the slab consisted of *N* lines and *M* columns) and painted every square black or white. Then every pretender was given a slab of the same size but painted entirely white. Within a day a pretender could paint any side-linked set of the squares of the slab some color. The set is called linked if for any two squares belonging to the set there is a path belonging the set on which any two neighboring squares share a side. The aim of each pretender is to paint his slab in the exactly the same way as the chief’s slab is painted. The one who paints a slab like that first becomes the new chief. Scientists found the slab painted by the ancient Berland tribe chief. Help them to determine the minimal amount of days needed to find a new chief if he had to paint his slab in the given way.
The first line contains two integers *N* and *M* (1<=≀<=*N*,<=*M*<=≀<=50) β€” the number of lines and columns on the slab. The next *N* lines contain *M* symbols each β€” the final coloration of the slab. *W* stands for the square that should be painted white and *B* β€” for the square that should be painted black.
In the single line output the minimal number of repaintings of side-linked areas needed to get the required coloration of the slab.
[ "3 3\nWBW\nBWB\nWBW\n", "2 3\nBBB\nBWB\n" ]
[ "2\n", "1\n" ]
none
[ { "input": "3 3\nWBW\nBWB\nWBW", "output": "2" }, { "input": "2 3\nBBB\nBWB", "output": "1" }, { "input": "9 29\nBWBBBBBBBBBWBWWBBBWBWBBBWWBWW\nWBWBBWBBWBWWBWBBBWBWWWBWBBBBB\nBWBBBBWWBBBWBWBBWWBBWBBBBBBBB\nBBBWWBBWWBBBWBWBBBWWWWWWBBBBW\nBBWWWWBBBBBBBBBWBBBBBBBBBBWBW\nBBBWWBBBBWBBBWWBBBWBBBBWBBWBW\nBBBBBWBWBBBWWBBWBBBBBBBBBBBBW\nWWBBBWWBWBWBBBBWBBBBWWWBBBBBB\nBWWBWBBBBBWBBWBBBBBBBWBWBBBWW", "output": "4" }, { "input": "3 5\nBBBBB\nBWWBB\nWWBBB", "output": "1" }, { "input": "17 22\nBBWBBWWWBBWWWBBBBBBWWW\nWWBWBWWBBWBBBWWBBBBBWW\nBBBWBWBBWBWBWBWWBWWBBB\nBWBBBBWBBBWBWBBBBBWBWW\nBBWWWWWBBBBBWBWBBBBBBB\nBBBWBBWWWWBBWWBWBWWBBW\nBBBWWWWWWWBWWWBBBWWWWW\nBBBBWBBBWBWBBWBBBWWWBB\nBWWWWBBBWWWBBBBBBWBWWW\nBBBBWBWBWBWBBBWWBWBBBB\nBBWBWWBWWWBWBWWBWBBBBW\nWWBWWBBBBWBWBWBWWBBBBB\nWWWWBWBBBWBBBWWBBWWBBW\nBBBBBWWBBBBWWWWBWBBBWW\nBBBBBWBBWBWBBWBWWBBBBW\nWWBBBWBWBBWWWBBBWWBBBW\nWWBWBBBBWBBBWBBBWBBBBB", "output": "4" }, { "input": "23 12\nBWWWBBWWWWWW\nWWWWBWWWBWWW\nBWBWWWWBBWWB\nWBWWWWBWWBWW\nBWWWWWWWWWWW\nWWWWWWBWWWWW\nWWWWWBWBWWBB\nWBBWWWWWWWWW\nWWBWBBWWWBBW\nWWWWWWWWWWWW\nWWWWWWWWWWWW\nWWWWWWBWWWWW\nBWWWWBBWBWWB\nBWBWWWWWWBWW\nWWWWWWWBWWWW\nWWBBWWWBWWBW\nBBWBWBWWWWWW\nWWBWBBWWWBBW\nWBWWWWWBWWBW\nWWWWBBBWWWWW\nWWWWBWWBBWWW\nWWWBBWWWWWWW\nWWWBWWBWWWBW", "output": "2" }, { "input": "41 6\nWBBBWB\nWWBWBW\nBBBBWB\nBBWBBB\nBBBWWW\nBBBWBB\nBBWBBB\nBBWWBB\nWBBBBB\nWBBBBB\nBBBBBB\nBBBBBB\nBBBBBB\nBBBBBB\nBBBBBB\nBBBBBB\nWBBBBB\nBBBBBW\nBWBWBW\nBBBBBB\nWWWBWW\nBBBBBB\nWBBBWB\nBBBWBB\nBBWBBB\nBWBWBB\nBWBBBB\nWBWBBB\nBBWWBB\nBBBBBB\nBBBBBB\nBBBBWB\nBBBBBB\nBBBBBW\nBWBWBW\nBBBBWB\nWBBWBB\nWBWBBW\nBBWBWW\nBBBWWB\nBBWWBB", "output": "3" }, { "input": "2 7\nBBBBBBB\nBBBBBBB", "output": "1" }, { "input": "7 28\nWWWWWWWWWBBWWWBBWBWWBWWBWWWW\nWWBBBWWWWWWWBWBBBWWWWBBWBWBW\nBWWWBBWWBWBWWBWWWWWWBBWWWWWW\nWBWWWWWWWBBBBWBBWWWWWWWBWWBB\nWWWBWBWWWWWBBWBWWWWBWWBWWBWW\nWWBWWWBWWWBWWBBWBWWWWWWBWWWB\nBWWWWBWWWWWWWWWBWBWWBWWWWWWW", "output": "3" }, { "input": "25 6\nWWWWWB\nWWBBWW\nBWBWWB\nBBWWWW\nBWWWWW\nWWWWWW\nWWWWWW\nWWWBWW\nWBWWWW\nWWWWWW\nWWBWWW\nWWWWWW\nWWBWWW\nWWWWWW\nWWWBWW\nWWWBWW\nWWWBWB\nWWWWWW\nWWWWWW\nWBWWWB\nBWWWWW\nBWBWWB\nWBWWWB\nWWWBWB\nWWWWBW", "output": "2" }, { "input": "42 1\nW\nW\nW\nB\nW\nW\nB\nB\nB\nB\nB\nB\nW\nB\nB\nW\nB\nB\nB\nB\nW\nB\nB\nW\nB\nW\nB\nB\nB\nB\nW\nB\nW\nB\nW\nB\nB\nB\nB\nW\nW\nB", "output": "11" }, { "input": "42 8\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBWBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBWB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBWBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBWBBB\nBBBBBBBB\nBBWBBBBB\nBBBBBBBB\nBBBBBBBB\nWBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBWW\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nBWBBBBBB", "output": "1" }, { "input": "4 38\nWWWWWBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWW\nWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW\nWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW\nWWBWWWWWWWWWWWWWWBWWWWWWWWWWWWWWWWWWWW", "output": "2" }, { "input": "9 10\nWWWWWWBWWW\nWWWBWWWWWW\nWBWWWWBWWW\nWBWBWWWWWW\nWBWWWWWWWW\nWWWWWWWWWW\nWWWWWWWWWB\nWWWWWWWWBB\nWWBBWWBWWW", "output": "2" }, { "input": "7 47\nWBWBWWWWBBBBBBWWWWBBBWBWBWBWBBWWWWBBBBWWWWBBWWB\nWBBBWBWWBBWBWWBWWBWBBBBWWBWBWWBBBBBBBBBWBWBBBBW\nBBBBWBWBWWBBWWBBBBBWWBBBWWBWBWWWBWWWWBWBBWWBBBW\nBWBBBBWBBWWWWBWBBWBWBBBBBBWWWWWBBBBBBWWBBBWWBWB\nBWBBBBBBBBBWBBBBWBWWBWWBBWBBWBBBBBBBWBBBWBBBBWB\nBBWBBWWWBBWBBBBBWWBBBWWBWBBBWBBBBWWWWBWWBBBWBWB\nBBBWBBBBWWBWBBWBWBWWBBBBBBBWBBBBBWWBWBBBBBWBWBW", "output": "6" }, { "input": "42 1\nB\nB\nB\nW\nB\nB\nW\nB\nB\nB\nB\nB\nW\nB\nW\nB\nW\nB\nB\nW\nB\nB\nB\nB\nW\nB\nW\nB\nB\nB\nB\nB\nB\nB\nB\nB\nB\nB\nB\nW\nB\nB", "output": "10" }, { "input": "25 10\nWWWWBBBWWW\nBBBWWBWWBB\nWBWWBWWWWB\nBWBWWWBBWB\nWBWWBWWWBW\nWBWWBWBBWW\nWWWWBBWBWW\nBWBWWWWBBB\nBBBWWBWWWW\nBWWWBWBBBW\nBBWBBBWBBB\nWBBWWBWWBW\nBBBBWWWWBW\nWWWWWWWWWB\nBWBBWBWBBB\nBBWWWBBWWW\nWBWWBWWWWW\nBWWBBWWWWB\nWWWWBBBWWW\nWBBWWWWWWB\nWBBWWWBBWB\nWBWWWBBWBW\nWWBWWWWWBW\nBWWBWWWBBW\nBWWWBBBBBB", "output": "3" }, { "input": "4 35\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", "output": "1" }, { "input": "39 1\nB\nB\nW\nW\nB\nB\nB\nB\nW\nB\nW\nW\nB\nB\nW\nB\nB\nB\nW\nW\nB\nB\nB\nB\nW\nB\nB\nB\nB\nB\nB\nB\nW\nB\nW\nB\nW\nW\nB", "output": "10" }, { "input": "18 11\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBWBBBBBB\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBBBBBWBB\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBBBBWBBB\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBBBBBBBB\nBBBBBBBBBBB", "output": "1" }, { "input": "7 20\nBBBBBBBBBBBBBBBBBBBB\nBBBBBBBBBBBBBBBBBBBB\nBBBBBBBBBBBBBBBBBBBB\nBBBBBBBBBBBBBBBBBBBB\nBBBBBBBBBBBBBBBBBBWB\nBBBBBBBBBBBBBBBBBBBB\nBBBBBBBBBBBBBBBBBBBB", "output": "1" }, { "input": "45 9\nBBWBWBBBW\nWBBBBWBWB\nBWWBBWWWW\nBBBBWWBBB\nWBBBBWWWB\nWWBWWBBBW\nBBWBBWBBB\nWBBBBWBWW\nBBBWBWBWW\nBWBWBBWWB\nBWBWBBWBW\nBWBBBWBWB\nBBWBBWWBB\nWBBBBWBBB\nWWBWBBBWB\nBWWBWWBBB\nBBWBWBWWB\nWBWWWWBBW\nBBBWBWBBB\nWBWWWBBBB\nBBBWWBWBB\nWWBBBBBWB\nBWBBBWWBB\nBBBWWBWWW\nBBWBBBBBW\nWBBWWWBBW\nBWBWBWWBB\nBBWBWBBWB\nBWWBBBBBB\nBBBBBBBWB\nBBBBBWWBB\nWWWBBBBBB\nWWWBBBBBW\nWWBBBBBWW\nWBWBWBWBB\nBBBBBBBWB\nBBWBBWBBB\nBBBWBBBWW\nWBWWWWWBW\nBBBBBBWBB\nBBWBBBWBW\nBWBBBBWWW\nBWWBWBBBB\nBWBBBBBBB\nBWBWBBBWW", "output": "6" }, { "input": "19 9\nWWWWBWWBW\nWBWBWBBWB\nWWBBWWWBB\nWWBWWWBWW\nBWBWBWWWW\nBBBBWBWWB\nWWWBBBWBW\nBWWWBWWWB\nBBBBWWBWW\nWWWBBBBBB\nWBWBWBWWW\nWBBWWWWBB\nWWBBWBWWB\nBBWBBBBWB\nWBWBBWWWB\nBWBWWBWBW\nWWBBBWWBW\nWBBWBWBWB\nBBWBBWBBB", "output": "5" }, { "input": "2 23\nBBBBWBBBBBBBBBBBWBBBBBW\nWBBBBWBWBBBBBBWBWBBBWBB", "output": "3" }, { "input": "11 1\nW\nW\nW\nW\nW\nW\nW\nW\nW\nW\nW", "output": "0" }, { "input": "2 39\nBBBBBBWWBBWBBBBBBBBBBBBBBWBBBBBBBBBBBBB\nBBBBBWBBBBBBBBBBWBWWBBWBBBBBBBBBBWBBBBB", "output": "2" }, { "input": "36 3\nBBB\nBBB\nWWB\nBBB\nBBB\nBWW\nWBB\nBBW\nWBW\nBBB\nBBB\nBBW\nBWW\nWBB\nBWB\nWWB\nBBW\nWBB\nBBW\nWBB\nBBB\nWWB\nWBW\nBBB\nBWW\nBBW\nWBB\nWBW\nWWW\nBWW\nBWB\nWBW\nBWB\nBBB\nBBB\nWBB", "output": "7" }, { "input": "5 20\nWWBBBBWWWWWBWBWBWWBW\nWBWWWWWWWWWBWWBWWWBW\nWBWWBWWBWWWWWBBWWWBW\nWBWBWWWWWBBWWWWBWWWW\nWBBWBWWBBWBWBWBBWWWW", "output": "3" }, { "input": "19 11\nWWWBWWBBBBB\nBWBWBWBWWBW\nWBBWBBBWWBB\nWWBBBWBBWWW\nWWWBBWBWBWW\nBBWBWBWBBBB\nBWWBWWWBWBB\nBWBWWBBWBBW\nBWBWBWWBWWW\nWWBWBWWWBWB\nBBBWWWBWWWW\nBWBWWWWWBBB\nBWWWBBBWWBB\nWWWBWBWWBWB\nWWWWBWWBWWB\nBWWWBWBWBBB\nBBWWWWWWWBW\nWBWWWBWBBWW\nWBWWWWBWBWB", "output": "4" }, { "input": "6 33\nWBBWWWWWWWWWBBBWWWWWWWWWWWWWWWBWW\nWBWWBBWBBWWBWWWWWBBBBBBBWWWBWBWBW\nWBBBBBBWBBWBWWBBWBBBWBWWBBBWWWWWW\nWWBWBWBBWBWWBWBWBWBBWWWBWWWWWWWWW\nWWBWWWWBWBBBBWWWWWWWWWBBWWWBWBWWW\nBWBWWBBBWBWWBWBBBBWBWBBBWWWWBBWWW", "output": "4" }, { "input": "22 17\nBBBBBWBBWWBBBWBBB\nBBBBBBBBBBBBWBBBB\nWBBBBBBBBBBBBBBBB\nBBBBBBBBBBBBBBBBB\nBBBBWBWBBBBWBBBBW\nBBBBBWBBWBBBWWBBB\nBBBWBWBBBBBBBBBBB\nBBBWBBBBBBBBBBBBB\nBWBBWWBBBBBBBBBBB\nBWBBWBBWBBBBBBBBW\nBBBBBBWWWBWBBBWWW\nBWBBWBBBBBBWBBWBB\nBWWBBBBBBBBBBBBBB\nBBBBBBBBWBWBWBBBB\nBBBBBBBBWBBBBBBBB\nBBBBBBBBBBBBBBWBB\nBBBWWBBBBBBWWBBBB\nBBBBBBBWBBBBBBBBB\nBBBWBBBBBBWBBBBBB\nWBWBWBBBBBBBWBBBB\nBBBBBWBBBBBBBBBBB\nBBBWBBBBBBBBBBBBB", "output": "1" }, { "input": "36 6\nBWWWBW\nBBBBWB\nBWWBBB\nBWBWBB\nBBBBBB\nBWWBBW\nBWBWBW\nWBBBBB\nWBBBBB\nBBWBBB\nBWBBWB\nBBWWWB\nWBBBBB\nBWBWBB\nBBBBBW\nBWWWWB\nBBBBWB\nBBBBBB\nBWBBBB\nWBWBWB\nBBBBWW\nBWBBWB\nWBBBBW\nBBBWWW\nWBBBBB\nBBBBBW\nBBBBBW\nBBBBWW\nBBBWBW\nBWBBBB\nBWBBBW\nWWBBBW\nWBWBWB\nBBBWBW\nBBWBBB\nBBBBBB", "output": "3" }, { "input": "24 1\nW\nB\nW\nW\nB\nB\nB\nB\nB\nB\nB\nB\nB\nB\nB\nB\nW\nB\nB\nB\nB\nB\nB\nB", "output": "3" }, { "input": "7 7\nBBBBBBB\nBWWWWWB\nBWBBBWB\nBWBWBWB\nBWBBBWB\nBWWWWWB\nBBBBBBB", "output": "2" }, { "input": "4 4\nBWWW\nWBWB\nBWBW\nWBWB", "output": "3" }, { "input": "5 5\nWBWWW\nBWBWW\nWBWWB\nWWBBW\nWWBWB", "output": "3" }, { "input": "10 10\nBBWWBBBBBW\nBWWWWBWBWB\nWBWBWBBBBW\nWWBBWBBBBW\nBBBBBBBBBW\nBBBWWBBWWB\nWBBWBWWWWB\nWWWBBBBBWW\nWBBBBBWBBB\nWBWWBWWBBB", "output": "3" }, { "input": "7 7\nWBWBBBW\nBBBWBBW\nWWWWBWB\nBBWWBBW\nWWBWBWW\nBWWBBWW\nWWBWBWW", "output": "3" }, { "input": "6 6\nWWWWBB\nWBWBWW\nBBBWBW\nBWWWWB\nWBBBBW\nBBWWWB", "output": "3" } ]
62
0
0
65,381
58
Calendar
[ "greedy", "strings" ]
D. Calendar
2
256
BerOilGasDiamondBank has branches in *n* cities, at that *n* is an even number. The bank management wants to publish a calendar with the names of all those cities written in two columns: the calendar should consist of exactly *n*<=/<=2 lines of strictly equal length, each of which contains exactly two names and exactly one separator character between them. The name of every city should be used in the calendar exactly once. For historical reasons the symbol *d* is used as the separator of words in the calendar. The BerOilGasDiamondBank management wants to show that all its branches are equally important to it, that's why the order of their appearance in the calendar should be following: if we "glue"(concatinate) all the *n*<=/<=2 calendar lines (from top to bottom) to make a single line, then the lexicographically minimal line is obtained. No separator character will be used to separate calendar lines. For example, if the lines are "bertown!berville", "newberville!bera", then the resulting line is "bertown!bervillenewberville!bera". In some sense one has to find the lexicographically minimal calendar, where the comparison of calendars happens line by line. Help BerOilGasDiamondBank and construct the required calendar.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=104, *n* is even) which is the number of branches. Then follow *n* lines which are the names of the cities. All the names consist of lowercase Latin letters; their lengths are no less than 1 and no more than 10 symbols. The next line contains a single symbol *d* (*d* has an ASCII-code from 33 to 126 inclusively, excluding lowercase Latin letters) which is the separator between words in the calendar lines. It is guaranteed that the calendar is possible to be constructed and all the names are different.
Print *n*<=/<=2 lines of similar length which are the required calendar. Every line should contain exactly two words and exactly one separator between them. If there are several solutions, print the lexicographically minimal one. The lexicographical comparison of lines is realized by the "&lt;" operator in the modern programming languages.
[ "4\nb\naa\nhg\nc\n.\n", "2\naa\na\n!\n", "2\naa\na\n|\n" ]
[ "aa.b\nc.hg\n", "a!aa\n", "aa|a\n" ]
none
[ { "input": "4\nb\naa\nhg\nc\n.", "output": "aa.b\nc.hg" }, { "input": "2\naa\na\n!", "output": "a!aa" }, { "input": "2\naa\na\n|", "output": "aa|a" }, { "input": "4\nqhcivbxotj\nirgxzzxvw\npxdmcyszvk\nyyaevcdal\n~", "output": "irgxzzxvw~pxdmcyszvk\nqhcivbxotj~yyaevcdal" }, { "input": "8\nbad\nrnnpg\njvcjsxfob\nad\nairnnpg\nqury\njvcjsxfo\nquryai\n6", "output": "ad6jvcjsxfob\nairnnpg6qury\nbad6jvcjsxfo\nquryai6rnnpg" }, { "input": "6\neh\nehkhdp\ngque\nkhdptvgque\ntvgque\nehkhdptv\n}", "output": "ehkhdptv}gque\nehkhdp}tvgque\neh}khdptvgque" }, { "input": "10\ndoecgzo\ntjptpqp\noitegxzwlp\nmwsrwmeyeg\nsmapaqanak\nsmapaqa\nqghrydm\nnakqghrydm\nmedoraus\nnyvgozjdf\n|", "output": "doecgzo|mwsrwmeyeg\nmedoraus|nyvgozjdf\nnakqghrydm|qghrydm\noitegxzwlp|smapaqa\nsmapaqanak|tjptpqp" }, { "input": "30\nd\nahx\nr\nyd\np\nnhy\na\ntqt\nctp\ntp\nho\nry\nm\ng\ns\nn\nct\nsc\nqr\nrry\ny\nhxm\nqrr\nsct\ncwu\nq\ndk\nrf\nhyd\nnh\n$", "output": "a$ahx\nct$dk\nctp$d\ncwu$g\nho$nh\nhxm$m\nhyd$n\nnhy$p\nq$qrr\nqr$rf\nr$rry\nry$sc\ns$sct\ntp$yd\ntqt$y" }, { "input": "14\neskrrgzq\nxbmynhxfg\nwwffafny\nfaxcnrqkkb\nfaxcnrqk\nkbwwffafny\nmnborvqeae\nranfahuebj\neskrrgzqk\nfaxcnrqkk\ncznaycxe\nrnkgfgyq\nkxbmynhxfg\nbwwffafny\n}", "output": "bwwffafny}eskrrgzqk\ncznaycxe}faxcnrqkkb\neskrrgzq}kbwwffafny\nfaxcnrqkk}xbmynhxfg\nfaxcnrqk}kxbmynhxfg\nmnborvqeae}rnkgfgyq\nranfahuebj}wwffafny" }, { "input": "34\nobseknnnqk\ncvyvvbcgb\nxvmhfzfl\ngrtp\nhbcbhj\nknnnqk\ncyud\nkuaeui\naeui\nlhpdobsekn\ncxmigej\ncvyvvbcgbs\nuwuu\nnnqk\npzcftfrrqp\nnwsyrgqa\nxvmhfzflku\nndcis\nxhaznwqsgk\ncftfrrqp\nkakdggegew\njjzvokhh\nlhpdobse\nxjjzvokhh\nlhpd\nsuwuu\ntuatbwof\nvpsuday\nndcisx\nfggxici\nbfnipz\nknzjio\noirksxb\nbfni\n~", "output": "aeui~cvyvvbcgbs\nbfnipz~cftfrrqp\nbfni~kakdggegew\ncvyvvbcgb~ndcis\ncxmigej~fggxici\ncyud~lhpdobsekn\ngrtp~obseknnnqk\nhbcbhj~jjzvokhh\nknnnqk~lhpdobse\nknzjio~nwsyrgqa\nkuaeui~tuatbwof\nlhpd~pzcftfrrqp\nndcisx~xvmhfzfl\nnnqk~xhaznwqsgk\noirksxb~vpsuday\nsuwuu~xjjzvokhh\nuwuu~xvmhfzflku" }, { "input": "58\nesgdfmf\nxfkluadj\nqhvh\njwhuyhm\nmgi\nysgc\nvhhenku\npb\ntr\nu\njyrpjnpd\nkluadjo\nopb\ncopb\ngcyhceo\nr\ndjo\nxfklu\neo\nadjo\nfkluadjo\nybe\nwljwh\nqhvhh\nrhgotp\nyhceo\nuyhm\nvdd\nyhm\nysgcyhc\nvddrhg\nril\nwljwhu\nx\nqh\nhceo\ntfcopb\nmgitfc\nvddrh\nmgitfco\nxf\nmgitf\ncyoybe\notp\no\nljwhuyhm\nysgcy\nhhenku\nwl\ngotp\nqhv\nw\nhenku\nenku\nys\nrilcyo\nxfklua\nqhvhhenk\n|", "output": "adjo|henku\ncopb|mgitf\ncyoybe|djo\nenku|qhvhh\neo|esgdfmf\nfkluadjo|o\ngcyhceo|pb\ngotp|vddrh\nhceo|wljwh\nhhenku|mgi\njwhuyhm|qh\njyrpjnpd|r\nkluadjo|tr\nljwhuyhm|u\nmgitfco|wl\nmgitfc|opb\notp|rhgotp\nqhvhhenk|w\nqhvh|xfklu\nqhv|rilcyo\nril|tfcopb\nuyhm|yhceo\nvddrhg|vdd\nvhhenku|xf\nwljwhu|ybe\nxfkluadj|x\nxfklua|yhm\nysgcyhc|ys\nysgcy|ysgc" }, { "input": "76\nsd\nwhx\nk\nce\nthm\nbyfi\npju\nbn\ndz\non\nizr\niswh\nl\nwig\ns\nju\nsr\nie\nx\nbth\nzvi\nlxth\ndmzz\nbnqq\nan\ny\ng\nvlj\nc\nhdu\nlx\nwkyd\ndb\nrmr\nrv\nis\ngv\nu\nbyf\nm\nqqb\nwe\nb\ne\nnioo\niek\no\nymk\nifpw\nisw\nammm\ncgk\ncq\nhhv\nq\nat\nd\ney\nn\nrhq\ncecg\nqsh\nak\nhx\nrve\nlaly\ni\nbnsa\nioou\nsk\nkg\nqshs\nwzmn\nupt\nvwvr\nyjj\nN", "output": "akNbth\nammmNb\nanNbyf\natNcgk\nbnNhdu\nbnqqNc\nbnsaNd\nbyfiNe\nceNhhv\ncecgNg\ncqNiek\ndbNisw\ndmzzNi\ndzNizr\neyNpju\ngvNqqb\nhxNqsh\nieNrhq\nifpwNk\nioouNl\nisNrmr\niswhNm\njuNrve\nkgNthm\nlalyNn\nlxNupt\nlxthNo\nniooNq\nonNvlj\nqshsNs\nrvNwhx\nsdNwig\nskNyjj\nsrNymk\nuNvwvr\nweNzvi\nwkydNx\nwzmnNy" }, { "input": "10\npo\negf\ne\ngfuzaqsi\nsi\nhpo\nuldiig\negfuzaq\nuldiigh\nuzaqsi\n{", "output": "egfuzaq{po\negf{uldiig\ne{gfuzaqsi\nhpo{uzaqsi\nsi{uldiigh" }, { "input": "4\na\nf\nz\nh\n!", "output": "a!f\nh!z" } ]
186
0
0
65,410
611
New Year and Cake
[ "geometry", "two pointers" ]
null
null
Limak is a little polar bear. According to some old traditions, his bear family prepared a New Year cake. And Limak likes cakes. As you may know, a New Year cake is a strictly convex polygon with *n* vertices. Parents won't allow Limak to eat more than half of a cake because he would get sick. After some thinking they decided to cut a cake along one of *n*Β·(*n*<=-<=3)<=/<=2 diagonals. Then Limak will get a non-greater piece. Limak understands rules but he won't be happy if the second piece happens to be much bigger. Limak's disappointment will be equal to the difference between pieces' areas, multiplied by two. It can be proved that it will be integer for the given constraints. There are *n*Β·(*n*<=-<=3)<=/<=2 possible scenarios. Consider them all and find the sum of values of Limak's disappointment, modulo 109<=+<=7.
The first line of the input contains a single integer *n* (4<=≀<=*n*<=≀<=500<=000)Β β€” the number of vertices in the polygon denoting the cake. Each of the next *n* lines contains two integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=≀<=109)Β β€” coordinates of the *i*-th point. It's guaranteed that all points are distinct, polygon is strictly convex and points are given in the clockwise order.
Print the sum of values of Limak's disappointment over all possible scenarios modulo 109<=+<=7.
[ "5\n2 4\n2 7\n5 7\n5 4\n3 -2\n", "4\n-1000000000 -5000000\n0 1234567\n1 1\n-5 -100000000\n", "8\n-10 0\n-6 6\n0 10\n6 6\n10 0\n6 -6\n0 -10\n-6 -6\n" ]
[ "90\n", "525185196\n", "5216\n" ]
In the first sample possible values of Limak's disappointment are 0, 18, 18, 24, 30.
[]
2,000
43,622,400
0
65,765
216
Martian Luck
[ "math", "number theory" ]
null
null
You know that the Martians use a number system with base *k*. Digit *b* (0<=≀<=*b*<=&lt;<=*k*) is considered lucky, as the first contact between the Martians and the Earthlings occurred in year *b* (by Martian chronology). A digital root *d*(*x*) of number *x* is a number that consists of a single digit, resulting after cascading summing of all digits of number *x*. Word "cascading" means that if the first summing gives us a number that consists of several digits, then we sum up all digits again, and again, until we get a one digit number. For example, *d*(35047)<==<=*d*((3<=+<=5<=+<=0<=+<=4)7)<==<=*d*(157)<==<=*d*((1<=+<=5)7)<==<=*d*(67)<==<=67. In this sample the calculations are performed in the 7-base notation. If a number's digital root equals *b*, the Martians also call this number lucky. You have string *s*, which consists of *n* digits in the *k*-base notation system. Your task is to find, how many distinct substrings of the given string are lucky numbers. Leading zeroes are permitted in the numbers. Note that substring *s*[*i*... *j*] of the string *s*<==<=*a*1*a*2... *a**n* (1<=≀<=*i*<=≀<=*j*<=≀<=*n*) is the string *a**i**a**i*<=+<=1... *a**j*. Two substrings *s*[*i*1... *j*1] and *s*[*i*2... *j*2] of the string *s* are different if either *i*1<=β‰ <=*i*2 or *j*1<=β‰ <=*j*2.
The first line contains three integers *k*, *b* and *n* (2<=≀<=*k*<=≀<=109, 0<=≀<=*b*<=&lt;<=*k*, 1<=≀<=*n*<=≀<=105). The second line contains string *s* as a sequence of *n* integers, representing digits in the *k*-base notation: the *i*-th integer equals *a**i* (0<=≀<=*a**i*<=&lt;<=*k*) β€” the *i*-th digit of string *s*. The numbers in the lines are space-separated.
Print a single integer β€” the number of substrings that are lucky numbers. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "10 5 6\n3 2 0 5 6 1\n", "7 6 4\n3 5 0 4\n", "257 0 3\n0 0 256\n" ]
[ "5", "1", "3" ]
In the first sample the following substrings have the sought digital root: *s*[1... 2] = "3 2", *s*[1... 3] = "3 2 0", *s*[3... 4] = "0 5", *s*[4... 4] = "5" and *s*[2... 6] = "2 0 5 6 1".
[ { "input": "10 5 6\n3 2 0 5 6 1", "output": "5" }, { "input": "7 6 4\n3 5 0 4", "output": "1" }, { "input": "257 0 3\n0 0 256", "output": "3" }, { "input": "2 1 1\n0", "output": "0" }, { "input": "2 0 20\n1 1 1 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 1", "output": "22" }, { "input": "100 29 33\n28 89 23 14 97 97 66 56 55 60 47 29 9 79 26 80 63 78 83 60 41 29 52 10 82 26 47 88 99 75 52 1 31", "output": "10" }, { "input": "3 2 100\n2 0 0 0 0 2 1 1 1 2 0 1 1 1 1 2 0 0 1 0 1 1 2 0 2 0 1 0 1 0 0 2 0 0 0 1 2 0 2 2 0 2 0 2 1 0 1 1 1 1 2 0 0 0 1 0 2 0 2 0 2 1 2 2 1 1 0 1 1 2 1 1 0 0 1 1 2 2 1 2 2 0 1 2 2 1 2 2 0 2 0 2 2 0 2 2 1 2 0 0", "output": "2451" }, { "input": "4 2 201\n0 2 0 3 3 0 3 0 1 2 0 1 1 2 0 2 3 1 3 1 1 1 2 1 2 3 0 0 3 1 2 2 0 0 2 3 3 3 3 0 1 2 2 3 2 3 2 2 1 0 1 3 1 3 0 2 1 0 2 0 1 1 1 2 2 1 3 0 0 0 2 1 3 0 0 1 1 3 3 2 1 2 2 1 3 3 1 3 1 3 0 1 0 0 0 2 0 3 0 0 2 3 1 3 1 0 1 3 0 1 3 2 2 1 2 0 2 1 1 0 3 0 1 1 0 1 2 2 3 0 3 0 2 0 2 0 1 2 2 2 1 0 3 2 0 3 0 2 2 1 0 2 1 3 0 2 1 0 2 2 0 3 2 1 2 0 3 1 2 3 1 1 3 2 1 3 3 3 3 1 3 0 2 3 0 1 3 2 3 1 2 3 2 0 3 2 0 1 2 3 0", "output": "6692" }, { "input": "5 4 102\n3 2 2 3 3 2 2 0 3 1 2 4 0 1 3 4 3 2 3 0 4 1 0 0 0 0 4 4 1 2 3 3 4 0 1 2 2 3 3 1 3 1 0 0 3 0 4 0 2 4 2 3 0 1 4 3 0 2 3 3 2 2 1 0 1 3 0 3 4 4 4 1 0 1 2 1 4 2 4 4 4 4 4 2 3 3 0 3 0 0 0 4 1 3 0 4 2 1 2 0 3 0", "output": "1293" }, { "input": "6 2 203\n3 0 5 1 3 3 0 3 4 0 4 2 4 4 4 4 4 5 0 2 1 0 3 2 5 3 2 3 4 1 5 0 0 4 4 2 0 4 2 0 1 3 4 4 3 2 1 5 1 3 2 0 4 5 2 3 0 4 2 4 5 4 1 2 2 4 1 3 5 5 0 2 5 0 3 5 4 3 4 0 0 3 5 2 4 2 2 5 3 3 3 0 2 5 1 0 0 2 2 1 1 2 4 1 0 5 5 0 5 2 0 3 5 5 3 2 2 0 5 3 2 0 4 0 0 5 5 4 4 0 3 0 4 4 0 4 0 0 2 1 1 4 3 3 0 5 4 4 1 1 5 3 2 2 5 4 0 0 2 3 2 4 3 1 3 0 3 5 0 1 2 2 4 2 3 4 5 4 1 5 5 1 1 1 3 3 2 4 2 0 5 5 1 1 0 3 2 1 3 4 2 1 5", "output": "4130" }, { "input": "7 4 104\n4 3 0 6 6 5 3 4 4 5 0 1 2 5 5 1 3 4 1 5 3 5 4 4 2 4 3 5 4 2 2 3 1 1 0 5 4 3 2 5 2 1 3 1 6 4 1 3 0 2 5 2 5 3 3 6 1 2 2 2 4 5 6 0 5 4 5 3 5 3 4 3 1 0 2 4 5 5 5 5 3 3 6 1 6 1 3 6 6 5 3 3 1 3 2 0 4 4 3 3 4 0 5 6", "output": "938" }, { "input": "8 5 205\n5 6 5 0 2 1 1 3 7 2 1 7 7 7 7 6 5 5 0 2 0 7 4 3 0 4 3 6 6 4 1 5 0 3 2 5 2 5 6 3 5 7 6 6 3 4 4 0 0 5 6 4 2 7 4 1 2 1 5 5 1 2 5 2 3 7 2 3 6 2 1 7 3 3 5 5 4 3 0 6 4 4 0 6 2 2 3 3 5 4 2 4 0 2 1 2 7 0 5 1 0 2 4 0 3 6 0 4 2 4 4 1 1 0 5 0 6 6 1 1 1 5 3 3 5 5 4 6 5 0 3 0 7 3 6 4 4 6 6 3 5 7 7 7 7 3 4 3 7 5 7 4 7 4 6 2 7 1 7 7 3 6 4 6 3 7 7 5 6 4 7 3 5 5 0 2 6 4 7 5 6 6 3 2 5 2 5 1 6 7 0 3 3 2 1 4 4 5 7 1 1 1 3 2 3", "output": "2928" }, { "input": "9 5 197\n4 3 0 6 2 2 2 7 2 4 8 1 7 4 6 0 1 0 4 2 6 3 6 2 1 8 6 1 5 2 8 3 0 8 3 6 6 2 0 7 3 6 7 4 4 0 4 1 7 8 5 6 5 7 3 1 7 6 7 2 3 4 1 0 5 5 7 3 4 0 8 1 5 3 6 3 7 8 8 1 7 6 1 8 5 6 6 2 1 4 7 3 6 5 0 7 3 1 7 8 4 4 6 0 6 8 4 8 4 8 4 1 4 2 1 5 1 7 5 1 2 3 7 8 0 5 2 0 6 2 2 5 8 3 8 1 5 2 0 4 1 4 8 1 6 3 1 3 2 1 0 3 3 3 0 3 3 1 1 5 2 5 6 4 3 8 1 1 2 3 7 7 3 8 3 4 3 2 4 4 4 7 2 7 5 1 6 4 1 4 4 4 4 8 7 6 6", "output": "2451" }, { "input": "11 1 199\n10 1 2 1 6 2 0 1 9 9 0 5 5 4 0 1 8 7 7 5 2 5 6 10 7 1 6 8 0 8 10 9 1 8 1 6 1 3 6 1 9 0 5 3 6 8 7 5 10 1 1 10 1 6 10 8 10 10 9 1 3 10 2 3 8 10 6 1 7 7 10 5 2 2 7 2 0 2 1 8 6 5 9 6 7 10 2 9 1 4 2 4 5 4 1 5 6 1 1 1 5 2 6 8 8 3 8 6 5 4 2 1 6 5 0 6 5 4 3 7 4 7 6 8 6 6 6 4 10 4 6 5 5 7 10 1 9 2 4 8 4 1 7 1 8 3 10 3 4 1 7 9 5 10 7 0 8 5 4 5 2 4 6 6 10 7 1 3 2 3 10 2 6 6 4 0 5 7 7 8 9 7 10 4 2 10 8 7 8 7 3 8 1 4 9 1 9 2 2", "output": "2051" }, { "input": "20 15 1\n6", "output": "0" }, { "input": "20 19 2\n16 13", "output": "0" }, { "input": "30 24 30\n7 24 3 20 8 24 0 6 15 22 20 21 16 26 28 6 6 28 19 2 12 22 6 12 15 17 24 13 12 16", "output": "20" }, { "input": "2 0 100\n0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0", "output": "331" } ]
92
0
0
65,969
31
Chocolate
[ "dfs and similar", "implementation" ]
D. Chocolate
2
256
Bob has a rectangular chocolate bar of the size *W*<=Γ—<=*H*. He introduced a cartesian coordinate system so that the point (0,<=0) corresponds to the lower-left corner of the bar, and the point (*W*,<=*H*) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line *x*<==<=*x**c* or *y*<==<=*y**c*, where *x**c* and *y**c* are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made *n* breaks and wrote them down in his notebook in arbitrary order. At the end he got *n*<=+<=1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
The first line contains 3 integers *W*, *H* and *n* (1<=≀<=*W*,<=*H*,<=*n*<=≀<=100) β€” width of the bar, height of the bar and amount of breaks. Each of the following *n* lines contains four integers *x**i*,<=1,<=*y**i*,<=1,<=*x**i*,<=2,<=*y**i*,<=2 β€” coordinates of the endpoints of the *i*-th break (0<=≀<=*x**i*,<=1<=≀<=*x**i*,<=2<=≀<=*W*,<=0<=≀<=*y**i*,<=1<=≀<=*y**i*,<=2<=≀<=*H*, or *x**i*,<=1<==<=*x**i*,<=2, or *y**i*,<=1<==<=*y**i*,<=2). Breaks are given in arbitrary order. It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output *n*<=+<=1 numbers β€” areas of the resulting parts in the increasing order.
[ "2 2 2\n1 0 1 2\n0 1 1 1\n", "2 2 3\n1 0 1 2\n0 1 1 1\n1 1 2 1\n", "2 4 2\n0 1 2 1\n0 3 2 3\n" ]
[ "1 1 2 ", "1 1 1 1 ", "2 2 4 " ]
none
[ { "input": "2 2 2\n1 0 1 2\n0 1 1 1", "output": "1 1 2 " }, { "input": "2 2 3\n1 0 1 2\n0 1 1 1\n1 1 2 1", "output": "1 1 1 1 " }, { "input": "2 4 2\n0 1 2 1\n0 3 2 3", "output": "2 2 4 " }, { "input": "5 5 3\n2 1 2 5\n0 1 5 1\n4 0 4 1", "output": "1 4 8 12 " }, { "input": "10 10 4\n9 0 9 10\n4 0 4 10\n1 0 1 10\n1 4 4 4", "output": "10 10 12 18 50 " }, { "input": "9 8 5\n4 3 4 4\n0 4 9 4\n5 4 5 8\n0 3 9 3\n1 4 1 8", "output": "4 4 5 16 16 27 " }, { "input": "100 100 1\n0 14 100 14", "output": "1400 8600 " } ]
374
2,252,800
3.902304
66,034
494
Obsessive String
[ "dp", "strings" ]
null
null
Hamed has recently found a string *t* and suddenly became quite fond of it. He spent several days trying to find all occurrences of *t* in other strings he had. Finally he became tired and started thinking about the following problem. Given a string *s* how many ways are there to extract *k*<=β‰₯<=1 non-overlapping substrings from it such that each of them contains string *t* as a substring? More formally, you need to calculate the number of ways to choose two sequences *a*1,<=*a*2,<=...,<=*a**k* and *b*1,<=*b*2,<=...,<=*b**k* satisfying the following requirements: - *k*<=β‰₯<=1 - - - - Β Β *t* is a substring of string *s**a**i**s**a**i*<=+<=1... *s**b**i* (string *s* is considered as 1-indexed). As the number of ways can be rather large print it modulo 109<=+<=7.
Input consists of two lines containing strings *s* and *t* (1<=≀<=|*s*|,<=|*t*|<=≀<=105). Each string consists of lowercase Latin letters.
Print the answer in a single line.
[ "ababa\naba\n", "welcometoroundtwohundredandeightytwo\nd\n", "ddd\nd\n" ]
[ "5\n", "274201\n", "12\n" ]
none
[ { "input": "ababa\naba", "output": "5" }, { "input": "welcometoroundtwohundredandeightytwo\nd", "output": "274201" }, { "input": "ddd\nd", "output": "12" }, { "input": "vnssnssnssnssnssnssnssnssnssnssnssnssnssnssnssnssn\nnssnssns", "output": "943392" }, { "input": "kpjmawawawawawawawawawawawawawawawawawawawawawawaw\nwawawawa", "output": "834052" }, { "input": "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\nvvvvvvvv", "output": "2728075" }, { "input": "a\na", "output": "1" }, { "input": "a\naa", "output": "0" }, { "input": "a\nb", "output": "0" }, { "input": "ababababab\nabab", "output": "35" } ]
2,000
28,876,800
0
66,125
580
Kefa and Dishes
[ "bitmasks", "dp" ]
null
null
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were *n* dishes. Kefa knows that he needs exactly *m* dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible. Kefa knows that the *i*-th dish gives him *a**i* units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself *k* rules of eating food of the following type β€” if he eats dish *x* exactly before dish *y* (there should be no other dishes between *x* and *y*), then his satisfaction level raises by *c*. Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!
The first line of the input contains three space-separated numbers, *n*, *m* and *k* (1<=≀<=*m*<=≀<=*n*<=≀<=18, 0<=≀<=*k*<=≀<=*n*<=*<=(*n*<=-<=1)) β€” the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules. The second line contains *n* space-separated numbers *a**i*, (0<=≀<=*a**i*<=≀<=109) β€” the satisfaction he gets from the *i*-th dish. Next *k* lines contain the rules. The *i*-th rule is described by the three numbers *x**i*, *y**i* and *c**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*, 0<=≀<=*c**i*<=≀<=109). That means that if you eat dish *x**i* right before dish *y**i*, then the Kefa's satisfaction increases by *c**i*. It is guaranteed that there are no such pairs of indexes *i* and *j* (1<=≀<=*i*<=&lt;<=*j*<=≀<=*k*), that *x**i*<==<=*x**j* and *y**i*<==<=*y**j*.
In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.
[ "2 2 1\n1 1\n2 1 1\n", "4 3 2\n1 2 3 4\n2 1 5\n3 4 2\n" ]
[ "3\n", "12\n" ]
In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule. In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get an additional satisfaction 5.
[ { "input": "2 2 1\n1 1\n2 1 1", "output": "3" }, { "input": "4 3 2\n1 2 3 4\n2 1 5\n3 4 2", "output": "12" }, { "input": "1 1 0\n1000000000", "output": "1000000000" }, { "input": "4 2 1\n100 1 2 200\n2 3 400", "output": "403" }, { "input": "7 4 3\n2 6 13 5 7 1 9\n6 1 15\n2 5 3\n6 4 8", "output": "40" }, { "input": "5 5 5\n3 3 3 3 3\n3 4 6\n4 1 2\n1 5 7\n5 2 4\n2 3 5", "output": "37" }, { "input": "5 4 1\n1000000000 500000000 1 1000000000 1000000000\n3 5 700000000", "output": "3700000001" }, { "input": "6 3 6\n2 3 4 1 5 6\n1 5 3\n5 1 4\n2 6 1\n2 4 3\n3 5 3\n4 5 6", "output": "18" }, { "input": "10 5 5\n45 45 12 67 32 6 125 33 89 100\n6 3 78\n1 2 23\n5 7 17\n9 2 90\n4 8 39", "output": "516" }, { "input": "18 2 4\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0\n18 2 5\n7 8 3\n1 18 2\n6 4 1", "output": "6" }, { "input": "4 2 0\n1 2 3 2", "output": "5" }, { "input": "7 1 21\n15 11 13 13 15 8 5\n4 1 14\n7 2 5\n3 2 11\n6 4 19\n3 1 12\n1 3 9\n3 6 12\n2 5 10\n5 7 14\n2 6 5\n5 2 19\n7 5 9\n4 2 19\n5 1 6\n6 2 7\n1 7 6\n2 4 15\n7 4 10\n2 7 19\n4 5 14\n6 7 8", "output": "15" }, { "input": "8 4 56\n59 78 6 55 79 70 25 2\n1 2 47\n7 6 41\n5 1 28\n7 1 8\n5 8 7\n3 5 6\n3 6 5\n8 6 55\n2 4 44\n4 6 8\n3 7 95\n8 7 96\n6 2 11\n4 1 24\n6 5 86\n4 7 98\n2 6 24\n8 5 31\n5 3 35\n5 4 30\n4 8 52\n5 6 8\n8 1 71\n6 7 48\n1 4 39\n2 5 88\n2 3 18\n5 7 30\n2 1 32\n1 8 86\n6 1 30\n2 8 1\n3 4 16\n7 2 56\n5 2 28\n6 3 5\n1 6 24\n8 2 61\n4 5 58\n1 7 82\n4 2 58\n4 3 60\n3 8 26\n6 4 88\n7 4 32\n2 7 58\n8 4 22\n7 3 2\n7 8 61\n7 5 76\n1 3 17\n1 5 50\n3 2 4\n3 1 44\n6 8 89\n8 3 41", "output": "516" }, { "input": "8 8 7\n0 0 0 0 0 0 0 0\n1 5 97132661\n5 2 199172588\n2 7 815407164\n7 3 938757689\n3 8 843668614\n8 4 560487634\n4 6 244729651", "output": "3699356001" }, { "input": "14 14 13\n0 0 0 0 0 0 0 0 0 0 0 0 0 0\n6 4 4\n4 8 6\n8 3 6\n3 12 1\n12 14 5\n14 2 4\n2 10 5\n10 1 2\n1 9 4\n9 7 8\n7 13 1\n13 11 5\n11 5 1", "output": "52" }, { "input": "15 15 14\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n2 6 57661\n6 15 67617\n15 10 81652\n10 1 16278\n1 14 3009\n14 11 33687\n11 3 62989\n3 9 96410\n9 13 91302\n13 5 86368\n5 12 2182\n12 4 16546\n4 8 12851\n8 7 26224", "output": "654776" }, { "input": "15 15 14\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n7 11 6827\n11 5 7756\n5 14 6186\n14 8 1460\n8 13 1620\n13 9 1232\n9 15 7361\n15 3 2850\n3 4 4479\n4 2 6756\n2 6 3449\n6 10 8684\n10 1 6984\n1 12 2948", "output": "68592" }, { "input": "16 16 15\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n1 4 565659444\n4 11 593187381\n11 16 541913119\n16 14 657168751\n14 12 500399857\n12 6 662036115\n6 10 647286834\n10 3 579955363\n3 15 568540410\n15 5 581760329\n5 9 609894501\n9 13 537885589\n13 8 504299990\n8 2 656459498\n2 7 514164539", "output": "8720611720" }, { "input": "16 12 2\n215685056 606689499 786509392 322681480 170763622 255981931 402020260 580776290 525819654 50248606 830314959 223078821 851769718 76817680 251067040 491418559\n14 4 951819487\n4 2 770897556", "output": "7704583505" }, { "input": "17 17 16\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n11 7 231657830\n7 5 232292501\n5 8 231939378\n8 4 224957214\n4 12 229241817\n12 1 231562923\n1 3 226465387\n3 16 230627405\n16 10 229220153\n10 9 228646517\n9 2 230321133\n2 17 232190045\n17 6 229133406\n6 15 227156658\n15 13 228664795\n13 14 228903543", "output": "3672980705" }, { "input": "18 18 17\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n4 17 510748572\n17 10 696685757\n10 15 161105419\n15 1 126069679\n1 2 837682033\n2 12 436383858\n12 9 267569422\n9 16 881873714\n16 11 735761535\n11 6 259423529\n6 5 263222337\n5 18 697008445\n18 13 962557222\n13 7 790468460\n7 8 13582102\n8 14 510800814\n14 3 880706849", "output": "9031649747" }, { "input": "18 18 17\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n2 1 34349196\n1 8 40123769\n8 9 52139422\n9 15 10987402\n15 16 61786095\n16 12 77282765\n12 4 18495873\n4 11 85323692\n11 10 28529557\n10 5 53623688\n5 18 40093687\n18 3 67739085\n3 14 68785388\n14 6 65567931\n6 17 48915166\n17 7 27948918\n7 13 70519650", "output": "852211284" }, { "input": "18 17 16\n298794979 56088167 220017066 212013239 193074498 269869999 238524920 107690830 236214025 114513789 305713075 41451289 198089336 246759776 267311824 142373649 312495533 217782770\n9 4 70536531\n13 14 235114377\n18 4 38527037\n3 7 39141694\n1 17 15392162\n6 17 35451325\n17 14 2959732\n13 10 244941239\n12 4 249018275\n15 16 20714817\n1 2 37731505\n9 18 28754011\n4 17 56009536\n5 6 273584878\n6 4 291865229\n17 16 249459905", "output": "4858815472" }, { "input": "18 18 0\n115005244 38190165 126325576 840509301 765839937 656684780 596662787 415309843 853044850 645116094 328898380 458939277 646488701 699003763 666873084 991845119 532882957 385678203", "output": "9763298061" }, { "input": "18 18 3\n759507857 606098930 900220927 233055471 218518255 171579924 442155777 996213891 751779343 197968566 990617245 159772960 717293019 848554997 726485601 13147425 265922263 562203963\n13 4 408141884\n3 2 213428327\n10 17 126413702", "output": "10309080327" }, { "input": "18 18 17\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n7 2 554462987\n2 4 444921228\n4 6 157507820\n6 10 900032432\n10 9 888093209\n9 17 115896460\n17 5 904317029\n5 12 581655689\n12 3 743717743\n3 13 692735618\n13 11 607064833\n11 18 677893322\n18 1 909539256\n1 14 619680883\n14 15 933580266\n15 8 932409037\n8 16 555252931", "output": "11218760743" }, { "input": "18 18 17\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n18 1 1000000000\n1 5 1000000000\n5 13 1000000000\n13 16 1000000000\n16 17 1000000000\n17 7 1000000000\n7 9 1000000000\n9 6 1000000000\n6 15 1000000000\n15 14 1000000000\n14 12 1000000000\n12 4 1000000000\n4 8 1000000000\n8 10 1000000000\n10 3 1000000000\n3 11 1000000000\n11 2 1000000000", "output": "17000000000" } ]
0
0
-1
66,191
0
none
[ "none" ]
null
null
In the year 2500 the annual graduation ceremony in the German University in Cairo (GUC) has run smoothly for almost 500 years so far. The most important part of the ceremony is related to the arrangement of the professors in the ceremonial hall. Traditionally GUC has *n* professors. Each professor has his seniority level. All seniorities are different. Let's enumerate the professors from 1 to *n*, with 1 being the most senior professor and *n* being the most junior professor. The ceremonial hall has *n* seats, one seat for each professor. Some places in this hall are meant for more senior professors than the others. More specifically, *m* pairs of seats are in "senior-junior" relation, and the tradition requires that for all *m* pairs of seats (*a**i*,<=*b**i*) the professor seated in "senior" position *a**i* should be more senior than the professor seated in "junior" position *b**i*. GUC is very strict about its traditions, which have been carefully observed starting from year 2001. The tradition requires that: - The seating of the professors changes every year. - Year 2001 ceremony was using lexicographically first arrangement of professors in the ceremonial hall. - Each consecutive year lexicographically next arrangement of the professors is used. The arrangement of the professors is the list of *n* integers, where the first integer is the seniority of the professor seated in position number one, the second integer is the seniority of the professor seated in position number two, etc. Given *n*, the number of professors, *y*, the current year and *m* pairs of restrictions, output the arrangement of the professors for this year.
The first line contains three integers *n*, *y* and *m* (1<=≀<=*n*<=≀<=16,<=2001<=≀<=*y*<=≀<=1018,<=0<=≀<=*m*<=≀<=100) β€” the number of professors, the year for which the arrangement should be computed, and the number of pairs of seats for which the seniority relation should be kept, respectively. The next *m* lines contain one pair of integers each, "*a**i* *b**i*", indicating that professor on the *a**i*-th seat is more senior than professor on the *b**i*-th seat (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*,<=*a**i*<=β‰ <=*b**i*). Some pair may be listed more than once. Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin stream (you may also use the %I64d specificator).
Print the order in which the professors should be seated in the requested year. If by this year the GUC would have ran out of arrangements, or the given "senior-junior" relation are contradictory, print "The times have changed" (without quotes).
[ "3 2001 2\n1 2\n2 3\n", "7 2020 6\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n", "10 3630801 0\n", "3 2001 3\n1 2\n2 3\n3 1\n" ]
[ "1 2 3\n", "1 2 3 7 4 6 5\n", "The times have changed\n", "The times have changed\n" ]
In the first example the lexicographically first order of seating is 1 2 3. In the third example the GUC will run out of arrangements after the year 3630800. In the fourth example there are no valid arrangements for the seating. The lexicographical comparison of arrangements is performed by the &lt; operator in modern programming languages. The arrangement *a* is lexicographically less that the arrangement *b*, if there exists such *i* (1 ≀ *i* ≀ *n*), that *a*<sub class="lower-index">*i*</sub> &lt; *b*<sub class="lower-index">*i*</sub>, and for any *j* (1 ≀ *j* &lt; *i*) *a*<sub class="lower-index">*j*</sub> = *b*<sub class="lower-index">*j*</sub>.
[]
92
0
0
66,200
9
Interestring graph and Apples
[ "dfs and similar", "dsu", "graphs" ]
E. Interesting Graph and Apples
1
64
Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life Β«interesting graph and applesΒ». An undirected graph is called interesting, if each of its vertices belongs to one cycle only β€” a funny ring β€” and does not belong to any other cycles. A funny ring is a cycle that goes through all the vertices just once. Moreover, loops are funny rings too. She has already drawn the apples and some of the graph edges. But now it is not clear, how to connect the rest of the vertices to get an interesting graph as a result. The answer should contain the minimal amount of added edges. And furthermore, the answer should be the lexicographically smallest one. The set of edges (*x*1,<=*y*1),<=(*x*2,<=*y*2),<=...,<=(*x**n*,<=*y**n*), where *x**i*<=≀<=*y**i*, is lexicographically smaller than the set (*u*1,<=*v*1),<=(*u*2,<=*v*2),<=...,<=(*u**n*,<=*v**n*), where *u**i*<=≀<=*v**i*, provided that the sequence of integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=...,<=*x**n*,<=*y**n* is lexicographically smaller than the sequence *u*1,<=*v*1,<=*u*2,<=*v*2,<=...,<=*u**n*,<=*v**n*. If you do not cope, Hexadecimal will eat you. ...eat you alive.
The first line of the input data contains a pair of integers *n* and *m* (1<=≀<=*n*<=≀<=50, 0<=≀<=*m*<=≀<=2500) β€” the amount of vertices and edges respectively. The following lines contain pairs of numbers *x**i* and *y**i* (1<=≀<=*x**i*, *y**i*<=≀<=*n*) β€” the vertices that are already connected by edges. The initial graph may contain multiple edges and loops.
In the first line output Β«YESΒ» or Β«NOΒ»: if it is possible or not to construct an interesting graph. If the answer is Β«YESΒ», in the second line output *k* β€” the amount of edges that should be added to the initial graph. Finally, output *k* lines: pairs of vertices *x**j* and *y**j*, between which edges should be drawn. The result may contain multiple edges and loops. *k* can be equal to zero.
[ "3 2\n1 2\n2 3\n" ]
[ "YES\n1\n1 3\n" ]
none
[ { "input": "3 2\n1 2\n2 3", "output": "YES\n1\n1 3" }, { "input": "1 1\n1 1", "output": "YES\n0" }, { "input": "1 2\n1 1\n1 1", "output": "NO" }, { "input": "1 3\n1 1\n1 1\n1 1", "output": "NO" }, { "input": "2 0", "output": "YES\n2\n1 2\n1 2" }, { "input": "2 1\n1 1", "output": "NO" }, { "input": "2 1\n2 2", "output": "NO" }, { "input": "2 1\n2 1", "output": "YES\n1\n1 2" }, { "input": "2 2\n1 1\n2 2", "output": "NO" }, { "input": "2 2\n1 2\n2 1", "output": "YES\n0" }, { "input": "2 3\n1 1\n1 2\n2 1", "output": "NO" }, { "input": "3 2\n1 2\n1 2", "output": "NO" }, { "input": "1 0", "output": "YES\n1\n1 1" }, { "input": "4 3\n1 2\n1 3\n1 4", "output": "NO" }, { "input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 1", "output": "NO" }, { "input": "49 0", "output": "YES\n49\n1 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 49" }, { "input": "50 0", "output": "YES\n50\n1 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 50" }, { "input": "50 1\n2 3", "output": "YES\n49\n1 2\n1 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 50" }, { "input": "3 2\n1 2\n2 3", "output": "YES\n1\n1 3" }, { "input": "5 3\n3 5\n4 2\n5 1", "output": "YES\n2\n1 2\n3 4" }, { "input": "6 6\n4 3\n3 5\n6 4\n1 6\n2 1\n5 2", "output": "YES\n0" }, { "input": "7 4\n3 2\n2 6\n6 7\n1 5", "output": "YES\n3\n1 3\n4 5\n4 7" }, { "input": "8 5\n4 7\n3 6\n8 3\n6 5\n1 2", "output": "YES\n3\n1 4\n2 5\n7 8" }, { "input": "9 5\n5 2\n4 6\n8 4\n1 8\n2 1", "output": "YES\n4\n3 5\n3 7\n6 9\n7 9" }, { "input": "3 3\n1 3\n2 1\n3 2", "output": "YES\n0" }, { "input": "4 3\n1 2\n4 1\n2 3", "output": "YES\n1\n3 4" }, { "input": "5 2\n1 5\n5 4", "output": "YES\n3\n1 2\n2 3\n3 4" }, { "input": "6 3\n3 4\n1 3\n2 5", "output": "YES\n3\n1 2\n4 6\n5 6" }, { "input": "7 6\n5 6\n2 7\n7 3\n4 1\n1 5\n3 4", "output": "YES\n1\n2 6" }, { "input": "4 1\n3 1", "output": "YES\n3\n1 2\n2 4\n3 4" }, { "input": "8 3\n3 8\n2 6\n1 7", "output": "YES\n5\n1 2\n3 4\n4 5\n5 6\n7 8" }, { "input": "9 4\n7 6\n2 8\n3 5\n8 3", "output": "YES\n5\n1 2\n1 4\n4 6\n5 9\n7 9" }, { "input": "45 22\n15 23\n14 30\n5 44\n43 21\n24 17\n37 38\n40 9\n41 43\n7 4\n38 22\n26 18\n44 41\n42 11\n4 33\n35 24\n36 15\n19 1\n1 37\n9 35\n12 40\n31 29\n18 25", "output": "YES\n23\n2 3\n2 5\n3 6\n6 7\n8 10\n8 11\n10 12\n13 14\n13 16\n16 17\n19 20\n20 21\n22 23\n25 27\n26 28\n27 29\n28 30\n31 32\n32 33\n34 36\n34 39\n39 45\n42 45" }, { "input": "46 25\n44 40\n25 10\n28 44\n26 4\n38 7\n27 3\n46 8\n32 28\n22 20\n14 33\n30 14\n12 23\n13 30\n40 18\n37 35\n10 16\n23 22\n3 46\n36 24\n19 12\n18 42\n11 34\n34 36\n9 32\n24 19", "output": "YES\n21\n1 2\n1 4\n2 5\n5 6\n6 7\n8 9\n11 13\n15 16\n15 17\n17 20\n21 25\n21 26\n27 29\n29 31\n31 33\n35 38\n37 39\n39 41\n41 43\n42 45\n43 45" }, { "input": "47 26\n24 2\n13 24\n25 14\n35 6\n4 10\n11 18\n29 41\n37 13\n38 3\n2 31\n30 29\n6 42\n33 25\n41 45\n40 8\n28 47\n43 39\n39 38\n1 5\n45 22\n19 21\n18 37\n36 17\n27 28\n16 11\n12 30", "output": "YES\n21\n1 3\n4 5\n7 8\n7 9\n9 10\n12 14\n15 16\n15 17\n19 20\n20 22\n21 23\n23 26\n26 27\n31 32\n32 33\n34 35\n34 36\n40 42\n43 44\n44 46\n46 47" }, { "input": "48 26\n27 5\n13 21\n14 20\n41 31\n4 26\n21 39\n31 17\n18 4\n42 2\n28 43\n11 23\n35 22\n34 18\n23 15\n10 13\n7 48\n5 44\n19 25\n12 7\n15 27\n39 41\n33 10\n45 40\n20 42\n29 38\n17 28", "output": "YES\n22\n1 2\n1 3\n3 6\n6 8\n8 9\n9 11\n12 14\n16 19\n16 22\n24 25\n24 26\n29 30\n30 32\n32 33\n34 36\n35 37\n36 38\n37 40\n43 44\n45 46\n46 47\n47 48" }, { "input": "49 26\n33 34\n43 21\n26 27\n46 33\n32 47\n6 3\n44 14\n34 42\n4 8\n27 29\n12 4\n42 7\n22 16\n5 31\n35 24\n39 40\n20 12\n17 44\n8 18\n38 26\n48 39\n31 17\n9 19\n10 23\n1 30\n49 38", "output": "YES\n23\n1 2\n2 3\n5 6\n7 9\n10 11\n11 13\n13 14\n15 16\n15 18\n19 20\n21 22\n23 24\n25 28\n25 29\n28 30\n32 35\n36 37\n36 40\n37 41\n41 43\n45 46\n45 47\n48 49" }, { "input": "50 21\n27 16\n42 35\n15 28\n46 17\n30 39\n47 18\n35 25\n26 24\n24 30\n28 41\n40 38\n11 21\n33 20\n43 10\n37 14\n1 43\n32 49\n49 6\n10 45\n21 50\n39 3", "output": "YES\n29\n1 2\n2 3\n4 5\n4 6\n5 7\n7 8\n8 9\n9 11\n12 13\n12 14\n13 15\n16 17\n18 19\n19 20\n22 23\n22 25\n23 26\n27 29\n29 31\n31 32\n33 34\n34 36\n36 37\n38 41\n40 42\n44 45\n44 46\n47 48\n48 50" }, { "input": "30 21\n6 14\n19 17\n25 20\n28 10\n10 3\n24 23\n22 13\n1 7\n11 26\n12 1\n16 8\n14 9\n30 15\n4 27\n13 21\n20 12\n24 14\n19 10\n7 10\n16 8\n26 11", "output": "NO" }, { "input": "31 24\n6 25\n8 13\n29 20\n13 5\n26 8\n16 9\n31 2\n22 7\n24 21\n28 18\n9 12\n27 14\n20 24\n23 10\n10 27\n15 1\n21 28\n11 16\n12 29\n8 7\n10 28\n27 19\n17 3\n23 16", "output": "NO" }, { "input": "5 2\n1 3\n4 1", "output": "YES\n3\n2 3\n2 5\n4 5" }, { "input": "32 24\n9 15\n32 16\n26 7\n15 8\n30 21\n23 14\n22 17\n14 29\n19 1\n24 31\n3 22\n20 9\n5 23\n10 3\n27 24\n1 30\n8 18\n23 28\n14 4\n27 10\n11 9\n11 24\n11 18\n17 6", "output": "NO" }, { "input": "33 19\n27 23\n17 16\n20 33\n3 11\n1 31\n26 24\n25 10\n21 15\n14 9\n12 4\n29 2\n7 21\n32 13\n33 6\n5 26\n13 28\n6 22\n3 24\n27 19", "output": "YES\n14\n1 2\n4 5\n7 8\n8 9\n10 11\n12 14\n15 16\n17 18\n18 19\n20 23\n22 28\n25 29\n30 31\n30 32" }, { "input": "34 18\n9 14\n30 23\n19 3\n34 19\n26 2\n31 28\n7 21\n20 27\n16 15\n18 20\n5 34\n17 22\n10 12\n6 4\n8 32\n29 24\n24 10\n34 22", "output": "NO" }, { "input": "35 28\n6 24\n35 10\n14 19\n30 34\n29 23\n21 16\n34 5\n22 6\n7 35\n13 29\n27 3\n8 27\n5 15\n26 11\n19 1\n31 28\n17 31\n18 20\n12 32\n4 17\n10 4\n32 8\n35 18\n9 5\n33 30\n24 25\n12 12\n34 3", "output": "NO" }, { "input": "36 23\n27 31\n33 14\n17 24\n14 25\n3 8\n1 21\n24 27\n13 26\n23 6\n35 22\n34 33\n36 4\n19 16\n18 15\n32 36\n5 7\n20 30\n21 11\n11 27\n8 23\n6 10\n4 31\n15 31", "output": "NO" }, { "input": "37 22\n2 15\n37 11\n14 29\n9 37\n15 23\n24 35\n18 3\n23 12\n34 33\n4 19\n22 14\n21 26\n28 27\n12 36\n8 6\n26 28\n31 1\n29 5\n27 25\n17 10\n33 18\n35 20", "output": "YES\n15\n1 2\n3 4\n5 6\n7 8\n7 9\n10 11\n13 16\n13 17\n16 19\n20 21\n22 24\n25 30\n30 31\n32 34\n32 36" }, { "input": "38 30\n21 36\n20 21\n9 11\n27 10\n25 20\n33 16\n11 23\n31 4\n13 22\n36 27\n32 37\n12 6\n35 31\n5 34\n6 14\n7 38\n26 18\n4 24\n18 5\n23 17\n29 28\n38 13\n10 30\n18 3\n15 25\n1 24\n22 22\n17 22\n36 18\n23 13", "output": "NO" }, { "input": "39 25\n8 23\n27 38\n6 32\n20 33\n7 34\n22 26\n32 12\n23 2\n28 20\n33 35\n18 10\n1 21\n11 18\n39 28\n17 9\n36 8\n15 17\n14 1\n19 24\n37 30\n21 39\n38 13\n28 5\n36 30\n33 13", "output": "NO" }, { "input": "40 29\n23 2\n40 16\n35 31\n2 40\n39 35\n18 11\n21 7\n3 6\n15 5\n4 18\n17 19\n8 34\n16 17\n9 39\n37 21\n19 26\n26 36\n33 4\n10 9\n34 22\n13 20\n32 40\n35 11\n5 12\n14 5\n5 24\n40 6\n32 35\n21 21", "output": "NO" }, { "input": "41 28\n6 28\n1 38\n11 7\n12 26\n10 36\n9 21\n8 3\n2 20\n33 32\n21 40\n34 10\n22 15\n30 22\n5 12\n19 35\n13 6\n31 37\n25 4\n15 23\n37 33\n19 19\n20 6\n14 8\n9 12\n27 33\n28 27\n37 11\n36 20", "output": "NO" }, { "input": "6 1\n4 1", "output": "YES\n5\n1 2\n2 3\n3 5\n4 6\n5 6" }, { "input": "42 28\n7 19\n15 24\n3 42\n18 5\n32 27\n26 20\n40 30\n35 2\n14 8\n22 10\n36 4\n16 14\n21 29\n37 40\n2 12\n30 21\n19 17\n39 34\n31 28\n20 3\n4 33\n11 42\n26 21\n9 10\n4 32\n6 1\n1 14\n14 12", "output": "NO" }, { "input": "43 36\n3 24\n25 36\n36 11\n12 38\n11 32\n15 3\n8 9\n2 17\n5 40\n21 37\n39 20\n28 30\n16 22\n27 13\n31 6\n24 39\n34 19\n35 18\n43 21\n41 4\n7 31\n33 26\n6 5\n42 27\n29 2\n30 10\n40 1\n1 29\n20 14\n40 29\n29 6\n26 27\n37 21\n19 9\n31 4\n19 38", "output": "NO" }, { "input": "44 31\n28 26\n5 36\n9 37\n36 29\n26 5\n25 42\n30 22\n29 3\n35 10\n44 28\n18 13\n16 6\n3 33\n22 9\n4 15\n27 19\n17 11\n19 41\n11 25\n10 30\n2 34\n12 7\n37 31\n16 40\n25 24\n28 44\n41 37\n21 21\n12 28\n20 23\n20 17", "output": "NO" }, { "input": "45 20\n37 5\n41 6\n13 22\n28 24\n30 10\n39 35\n5 20\n38 32\n26 1\n23 37\n35 17\n21 12\n7 8\n1 7\n4 16\n8 40\n44 3\n27 23\n19 2\n33 27", "output": "YES\n25\n2 3\n4 6\n9 10\n9 11\n11 12\n13 14\n14 15\n15 16\n17 18\n18 19\n20 21\n22 24\n25 26\n25 28\n29 30\n29 31\n31 32\n33 34\n34 36\n36 39\n38 40\n41 42\n42 43\n43 45\n44 45" }, { "input": "46 24\n24 43\n38 20\n8 38\n22 13\n25 24\n40 35\n21 10\n7 39\n18 5\n33 19\n26 7\n1 27\n43 26\n9 17\n3 44\n44 14\n20 11\n5 2\n15 32\n23 8\n10 37\n27 23\n43 23\n33 25", "output": "NO" }, { "input": "47 36\n29 31\n25 45\n39 46\n12 19\n31 21\n4 41\n5 38\n33 3\n21 39\n40 1\n1 47\n35 12\n42 10\n2 4\n6 35\n17 16\n22 28\n14 22\n41 25\n10 14\n34 37\n27 20\n44 27\n20 2\n3 17\n45 13\n18 34\n47 15\n10 44\n25 15\n12 23\n27 17\n15 38\n17 32\n29 31\n3 39", "output": "NO" }, { "input": "48 32\n45 23\n17 3\n2 48\n47 20\n27 18\n13 28\n18 26\n26 21\n48 31\n21 9\n43 19\n34 43\n10 36\n14 17\n6 12\n3 11\n15 1\n23 37\n37 13\n42 40\n35 5\n16 7\n40 44\n4 29\n24 25\n5 16\n31 45\n39 22\n46 34\n22 30\n28 33\n33 41", "output": "YES\n16\n1 2\n4 6\n7 8\n8 9\n10 11\n12 14\n15 19\n20 24\n25 27\n29 30\n32 35\n32 36\n38 39\n38 41\n42 46\n44 47" }, { "input": "49 29\n43 18\n44 26\n49 31\n37 19\n20 16\n18 22\n30 5\n7 28\n12 2\n31 11\n27 43\n25 9\n19 4\n35 25\n4 30\n6 27\n46 41\n38 23\n17 37\n13 8\n11 38\n29 20\n40 10\n22 29\n36 7\n17 36\n35 48\n41 36\n39 27", "output": "NO" }, { "input": "50 27\n10 7\n32 9\n17 33\n25 34\n47 28\n23 16\n15 46\n41 50\n18 24\n27 19\n35 36\n19 38\n50 31\n31 40\n4 14\n1 11\n6 48\n33 35\n36 30\n39 12\n28 45\n2 1\n22 13\n3 49\n29 36\n7 34\n36 8", "output": "NO" }, { "input": "7 3\n7 4\n5 2\n1 3", "output": "YES\n4\n1 2\n3 4\n5 6\n6 7" }, { "input": "8 4\n1 7\n2 4\n6 2\n5 8", "output": "YES\n4\n1 3\n3 4\n5 6\n7 8" }, { "input": "9 2\n2 5\n1 6", "output": "YES\n7\n1 2\n3 4\n3 5\n4 7\n6 8\n7 9\n8 9" }, { "input": "3 2\n3 2\n2 1", "output": "YES\n1\n1 3" }, { "input": "4 2\n3 1\n4 2", "output": "YES\n2\n1 2\n3 4" } ]
186
512,000
-1
66,256
0
none
[ "none" ]
null
null
На ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π½ΠΎΠΉ прямой сидит *n* собачСк, *i*-я собачка находится Π² Ρ‚ΠΎΡ‡ΠΊΠ΅ *x**i*. ΠšΡ€ΠΎΠΌΠ΅ Ρ‚ΠΎΠ³ΠΎ, Π½Π° прямой Π΅ΡΡ‚ΡŒ *m* мисок с Π΅Π΄ΠΎΠΉ, для ΠΊΠ°ΠΆΠ΄ΠΎΠΉ извСстна Π΅Ρ‘ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° Π½Π° прямой *u**j* ΠΈ врСмя *t**j*, Ρ‡Π΅Ρ€Π΅Π· ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π΅Π΄Π° Π² мискС остынСт ΠΈ станСт нСвкусной. Π­Ρ‚ΠΎ Π·Π½Π°Ρ‡ΠΈΡ‚, Ρ‡Ρ‚ΠΎ Ссли собачка ΠΏΡ€ΠΈΠ±Π΅ΠΆΠΈΡ‚ ΠΊ мискС Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ, строго больший *t**j*, Ρ‚ΠΎ Π΅Π΄Π° ΡƒΠΆΠ΅ остынСт, ΠΈ собачка ΠΊΡƒΡˆΠ°Ρ‚ΡŒ Π΅Ρ‘ Π½Π΅ станСт. Бчитая, Ρ‡Ρ‚ΠΎ каТдая собачка Π±Π΅ΠΆΠΈΡ‚ со ΡΠΊΠΎΡ€ΠΎΡΡ‚ΡŒΡŽ 1, Π½Π°ΠΉΠ΄ΠΈΡ‚Π΅ максимальноС количСство собачСк, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ смогут ΠΏΠΎΠΊΡƒΡˆΠ°Ρ‚ΡŒ. Π‘Ρ‡ΠΈΡ‚Π°ΠΉΡ‚Π΅, Ρ‡Ρ‚ΠΎ собачки ΠΏΠΎΠ±Π΅Π³ΡƒΡ‚ ΠΊ Ρ‚Π΅ΠΌ мискам, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π²Ρ‹ ΠΈΠΌ ΡƒΠΊΠ°ΠΆΠ΅Ρ‚Π΅. Из ΠΎΠ΄Π½ΠΎΠΉ миски Π½Π΅ ΠΌΠΎΠ³ΡƒΡ‚ ΠΊΡƒΡˆΠ°Ρ‚ΡŒ Π΄Π²Π΅ ΠΈΠ»ΠΈ Π±ΠΎΠ»Π΅Π΅ собачки. Π‘ΠΎΠ±Π°Ρ‡ΠΊΠΈ ΠΌΠΎΠ³ΡƒΡ‚ ΠΎΠ±Π³ΠΎΠ½ΡΡ‚ΡŒ Π΄Ρ€ΡƒΠ³ Π΄Ρ€ΡƒΠ³Π°, Ρ‚ΠΎ Π΅ΡΡ‚ΡŒ, Ссли ΠΎΠ΄Π½Π° ΠΈΠ· Π½ΠΈΡ… остановится ΠΏΠΎΠΊΡƒΡˆΠ°Ρ‚ΡŒ, другая ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΡ€ΠΎΠΉΡ‚ΠΈ ΠΌΠΈΠΌΠΎ Π½Π΅Ρ‘, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠΏΠ°ΡΡ‚ΡŒ ΠΊ Π΄Ρ€ΡƒΠ³ΠΎΠΉ мискС.
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС находится ΠΏΠ°Ρ€Π° Ρ†Π΅Π»Ρ‹Ρ… чисСл *n* ΠΈ *m* (1<=≀<=*n*,<=*m*<=≀<=200<=000)Β β€” количСство собачСк ΠΈ мисок соотвСтствСнно. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС находятся *n* Ρ†Π΅Π»Ρ‹Ρ… чисСл *x**i* (<=-<=109<=≀<=*x**i*<=≀<=109)Β β€” ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° *i*-ΠΉ собачки. Π’ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΡ… *m* строках находятся ΠΏΠ°Ρ€Ρ‹ Ρ†Π΅Π»Ρ‹Ρ… чисСл *u**j* ΠΈ *t**j* (<=-<=109<=≀<=*u**j*<=≀<=109, 1<=≀<=*t**j*<=≀<=109)Β β€” ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° *j*-ΠΉ миски ΠΈ врСмя, ΠΊΠΎΠ³Π΄Π° остынСт Π΅Π΄Π° Π² Π½Π΅ΠΉ, соотвСтствСнно. ГарантируСтся, Ρ‡Ρ‚ΠΎ Π½ΠΈΠΊΠ°ΠΊΠΈΠ΅ Π΄Π²Π΅ собачки Π½Π΅ находятся Π² ΠΎΠ΄Π½ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠ΅. НикакиС Π΄Π²Π΅ миски Ρ‚Π°ΠΊΠΆΠ΅ Π½Π΅ ΠΌΠΎΠ³ΡƒΡ‚ Π½Π°Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒΡΡ Π² ΠΎΠ΄Π½ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠ΅.
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число *a*Β β€” максимальноС количСство собачСк, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ смогут ΠΏΠΎΠΊΡƒΡˆΠ°Ρ‚ΡŒ.
[ "5 4\n-2 0 4 8 13\n-1 1\n4 3\n6 3\n11 2\n", "3 3\n-1 3 7\n1 1\n4 1\n7 1\n", "4 4\n20 1 10 30\n1 1\n2 5\n22 2\n40 10\n" ]
[ "4\n", "2\n", "3\n" ]
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ пСрвая собачка ΠΏΠΎΠ±Π΅ΠΆΠΈΡ‚ Π½Π°ΠΏΡ€Π°Π²ΠΎ ΠΊ ΠΏΠ΅Ρ€Π²ΠΎΠΉ мискС, Ρ‚Ρ€Π΅Ρ‚ΡŒΡ собачка сразу Π½Π°Ρ‡Π½Ρ‘Ρ‚ Π΅ΡΡ‚ΡŒ ΠΈΠ· Π²Ρ‚ΠΎΡ€ΠΎΠΉ миски, чСтвёртая собачка ΠΏΠΎΠ±Π΅ΠΆΠΈΡ‚ Π²Π»Π΅Π²ΠΎ ΠΊ Ρ‚Ρ€Π΅Ρ‚ΡŒΠ΅ΠΉ мискС, Π° пятая собачка ΠΏΠΎΠ±Π΅ΠΆΠΈΡ‚ Π²Π»Π΅Π²ΠΎ ΠΊ Ρ‡Π΅Ρ‚Π²Ρ‘Ρ€Ρ‚ΠΎΠΉ мискС.
[]
0
0
-1
66,318
887
Ratings and Reality Shows
[ "data structures", "two pointers" ]
null
null
There are two main kinds of events in the life of top-model: fashion shows and photo shoots. Participating in any of these events affects the rating of appropriate top-model. After each photo shoot model's rating increases by *a* and after each fashion show decreases by *b* (designers do too many experiments nowadays). Moreover, sometimes top-models participates in talk shows. After participating in talk show model becomes more popular and increasing of her rating after photo shoots become *c* and decreasing of her rating after fashion show becomes *d*. Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will never become negative. Help her to find a suitable moment for participating in the talk show. Let's assume that model's career begins in moment 0. At that moment Izabella's rating was equal to *start*. If talk show happens in moment *t* if will affect all events in model's life in interval of time [*t*..*t*<=+<=*len*) (including *t* and not including *t*<=+<=*len*), where *len* is duration of influence. Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will not become become negative before talk show or during period of influence of talk show. Help her to find a suitable moment for participating in the talk show.
In first line there are 7 positive integers *n*, *a*, *b*, *c*, *d*, *start*, *len* (1<=≀<=*n*<=≀<=3Β·105, 0<=≀<=*start*<=≀<=109, 1<=≀<=*a*,<=*b*,<=*c*,<=*d*,<=*len*<=≀<=109), where *n* is a number of fashion shows and photo shoots, *a*, *b*, *c* and *d* are rating changes described above, *start* is an initial rating of model and *len* is a duration of influence of talk show. In next *n* lines descriptions of events are given. Each of those lines contains two integers *t**i* and *q**i* (1<=≀<=*t**i*<=≀<=109, 0<=≀<=*q*<=≀<=1)Β β€” moment, in which event happens and type of this event. Type 0 corresponds to the fashion show and type 1Β β€” to photo shoot. Events are given in order of increasing *t**i*, all *t**i* are different.
Print one non-negative integer *t*Β β€” the moment of time in which talk show should happen to make Izabella's rating non-negative before talk show and during period of influence of talk show. If there are multiple answers print smallest of them. If there are no such moments, print <=-<=1.
[ "5 1 1 1 4 0 5\n1 1\n2 1\n3 1\n4 0\n5 0\n", "1 1 2 1 2 1 2\n1 0\n" ]
[ "6", "-1" ]
none
[ { "input": "5 1 1 1 4 0 5\n1 1\n2 1\n3 1\n4 0\n5 0", "output": "6" }, { "input": "1 1 2 1 2 1 2\n1 0", "output": "-1" }, { "input": "10 1 1 1 2 0 10\n1 1\n2 1\n3 0\n4 0\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1", "output": "5" } ]
62
0
0
66,838
62
World Evil
[ "dp", "flows" ]
E. World Evil
5
256
As a result of Pinky and Brain's mysterious experiments in the Large Hadron Collider some portals or black holes opened to the parallel dimension. And the World Evil has crept to the veil between their world and ours. Brain quickly evaluated the situation and he understood that the more evil tentacles creep out and become free, the higher is the possibility that Brain will rule the world. The collider's constriction is a rectangular grid rolled into a cylinder and consisting of *n* rows and *m* columns such as is shown in the picture below: In this example *n*<==<=4, *m*<==<=5. Dotted lines are corridores that close each column to a ring, i. e. connect the *n*-th and the 1-th rows of the grid. In the leftmost column of the grid the portals are situated and the tentacles of the World Evil are ready to creep out from there. In the rightmost column the exit doors are located. The tentacles can only get out through those doors. The segments joining the nodes of the grid are corridors. Brain would be glad to let all the tentacles out but he faces a problem: the infinite number of tentacles can creep out of the portals, every tentacle possesses infinite length and some width and the volume of the corridors are, unfortunately, quite limited. Brain could approximately evaluate the maximal number of tentacles that will be able to crawl through every corridor. Now help the mice to determine the maximal number of tentacles of the World Evil that will crawl out of the Large Hadron Collider.
The first line of the input file contains two integers *n* and *m* (2<=≀<=*n*<=≀<=5, 2<=≀<=*m*<=≀<=105). They are the sizes of the Large Hadron Collider grid. The next *m*<=-<=1 lines contain *n* numbers each. They are the horizontal corridors' capacities. The next *m* lines contain *n* numbers each. They are the vertical corridors' capacities. Corridors are described from left to right and from top to bottom. Every *n*-th vertical corridor connects nodes of the *n*-th and 1-th rows. A corridor's capacity is a non-negative integer that does not exceed 109.
Print a single number, the number of the World Evil tentacles Pinky and Brain will command. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
[ "3 4\n4 4 4\n1 1 5\n5 5 3\n4 1 2\n1 3 1\n3 5 4\n1 4 3\n", "2 2\n9 2\n2 3\n6 1\n" ]
[ "7\n", "11\n" ]
none
[]
0
0
-1
66,875
0
none
[ "none" ]
null
null
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. Multiset is a set where equal elements are allowed. Multiset is called symmetric, if there is a point *P* on the plane such that the multiset is [centrally symmetric](https://en.wikipedia.org/wiki/Point_reflection) in respect of point *P*.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2000) β€” the number of points in the set. Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=106<=<=≀<=<=*x**i*,<=<=*y**i*<=<=≀<=<=106) β€” the coordinates of the points. It is guaranteed that no two points coincide.
If there are infinitely many good lines, print -1. Otherwise, print single integerΒ β€” the number of good lines.
[ "3\n1 2\n2 1\n3 3\n", "2\n4 3\n1 2\n" ]
[ "3\n", "-1\n" ]
Picture to the first sample test: <img class="tex-graphics" src="https://espresso.codeforces.com/eedc60313be8684bd6169b8b23f0f0afd92479a8.png" style="max-width: 100.0%;max-height: 100.0%;"/> In the second sample, any line containing the origin is good.
[]
77
512,000
0
66,929
85
Embassy Queue
[ "data structures", "greedy" ]
B. Embassy Queue
2
256
In an embassy of a well-known kingdom an electronic queue is organised. Every person who comes to the embassy, needs to make the following three actions: show the ID, pay money to the cashier and be fingerprinted. Besides, the actions should be performed in the given order. For each action several separate windows are singled out: *k*1 separate windows for the first action (the first type windows), *k*2 windows for the second one (the second type windows), and *k*3 for the third one (the third type windows). The service time for one person in any of the first type window equals to *t*1. Similarly, it takes *t*2 time to serve a person in any of the second type windows. And it takes *t*3 to serve one person in any of the third type windows. Thus, the service time depends only on the window type and is independent from the person who is applying for visa. At some moment *n* people come to the embassy, the *i*-th person comes at the moment of time *c**i*. The person is registered under some number. After that he sits in the hall and waits for his number to be shown on a special board. Besides the person's number the board shows the number of the window where one should go and the person goes there immediately. Let's consider that the time needed to approach the window is negligible. The table can show information for no more than one person at a time. The electronic queue works so as to immediately start working with the person who has approached the window, as there are no other people in front of the window. The Client Service Quality inspectors noticed that several people spend too much time in the embassy (this is particularly tiresome as the embassy has no mobile phone reception and 3G). It was decided to organise the system so that the largest time a person spends in the embassy were minimum. Help the inspectors organise the queue. Consider that all actions except for being served in at the window, happen instantly.
The first line contains three space-separated integers *k*1, *k*2, *k*3 (1<=≀<=*k**i*<=≀<=109), they are the number of windows of the first, second and third type correspondingly. The second line contains three space-separated integers *t*1, *t*2, *t*3 (1<=≀<=*t**i*<=≀<=105), they are the periods of time needed to serve one person in the window of the first, second and third type correspondingly. The third line contains an integer *n* (1<=≀<=*n*<=≀<=105), it is the number of people. The fourth line contains *n* space-separated integers *c**i* (1<=≀<=*c**i*<=≀<=109) in the non-decreasing order; *c**i* is the time when the person number *i* comes to the embassy.
Print the single number, the maximum time a person will spend in the embassy if the queue is organized optimally. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).
[ "1 1 1\n1 1 1\n5\n1 1 1 1 1\n", "2 1 1\n5 1 1\n5\n1 2 3 3 5\n" ]
[ "7\n", "13\n" ]
In the first test 5 people come simultaneously at the moment of time equal to 1. There is one window of every type, it takes 1 unit of time to be served at each window. That's why the maximal time a person spends in the embassy is the time needed to be served at the windows (3 units of time) plus the time the last person who comes to the first window waits (4 units of time). Windows in the second test work like this: The first window of the first type: [1, 6) β€” the first person, [6, 11) β€” third person, [11, 16) β€” fifth person The second window of the first type: [2, 7) β€” the second person, [7, 12) β€” the fourth person The only second type window: [6, 7) β€” first, [7, 8) β€” second, [11, 12) β€” third, [12, 13) β€” fourth, [16, 17) β€” fifth The only third type window: [7, 8) β€” first, [8, 9) β€” second, [12, 13) β€” third, [13, 14) β€” fourth, [17, 18) β€” fifth We can see that it takes most time to serve the fifth person.
[ { "input": "1 1 1\n1 1 1\n5\n1 1 1 1 1", "output": "7" }, { "input": "2 1 1\n5 1 1\n5\n1 2 3 3 5", "output": "13" }, { "input": "1 1 1\n1 1 1\n5\n1 2 3 4 5", "output": "3" }, { "input": "1 1 1\n1 1 1\n5\n1 2 3 3 4", "output": "4" }, { "input": "9 5 6\n8 2 2\n1\n5", "output": "12" }, { "input": "2 7 3\n8 10 5\n5\n5 6 7 8 9", "output": "35" }, { "input": "3 9 10\n3 10 4\n5\n2 2 2 2 2", "output": "20" }, { "input": "2 7 1\n10 3 10\n100\n3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102", "output": "914" }, { "input": "8 2 3\n7 7 10\n100\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "370" }, { "input": "2 2 7\n3 1 3\n5\n1 10001 20001 30001 40001", "output": "7" }, { "input": "3 4 5\n9053 90120 36494\n5\n60569225 70426298 122734833 143091149 631569476", "output": "135667" }, { "input": "2 2 2\n80223 94781 69753\n5\n221143148 236273686 612319555 780338715 825112104", "output": "244757" }, { "input": "8 9 10\n9053 90120 36494\n5\n60569225 70426298 122734833 143091149 631569476", "output": "135667" }, { "input": "1 1 1\n1 1 1\n10\n1 1 1 1 100000 100000 100000 100000 100000 100000", "output": "8" }, { "input": "1000000000 1000000000 1000000000\n1 1 1\n1\n1", "output": "3" }, { "input": "1000000000 1000000000 1000000000\n100000 100000 100000\n5\n1 1 1 1 1", "output": "300000" } ]
530
9,420,800
3.849952
67,097
731
Coupons and Discounts
[ "constructive algorithms", "greedy" ]
null
null
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. Teams 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. There 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). As 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. Sereja 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*.
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the number of training sessions. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=≀<=*a**i*<=≀<=10<=000)Β β€” the number of teams that will be present on each of the days.
If 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).
[ "4\n1 2 1 2\n", "3\n1 0 1\n" ]
[ "YES\n", "NO\n" ]
In 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. In 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.
[ { "input": "4\n1 2 1 2", "output": "YES" }, { "input": "3\n1 0 1", "output": "NO" }, { "input": "3\n1 3 1", "output": "NO" }, { "input": "3\n2 0 2", "output": "YES" }, { "input": "1\n179", "output": "NO" }, { "input": "10\n0 0 5 9 9 3 0 0 0 10", "output": "YES" }, { "input": "3\n3 2 3", "output": "YES" }, { "input": "1\n0", "output": "YES" }, { "input": "2\n0 0", "output": "YES" }, { "input": "10\n0 0 0 0 0 0 0 0 0 0", "output": "YES" }, { "input": "1\n1", "output": "NO" }, { "input": "1\n2", "output": "YES" }, { "input": "1\n3", "output": "NO" }, { "input": "1\n10000", "output": "YES" }, { "input": "2\n10000 10000", "output": "YES" }, { "input": "3\n2 2 2", "output": "YES" }, { "input": "10\n3 3 3 2 2 2 2 2 2 3", "output": "YES" }, { "input": "100\n2 3 2 3 3 3 3 3 3 2 2 2 2 2 2 3 2 3 3 2 3 2 3 2 2 3 3 3 3 3 2 2 2 2 3 2 3 3 2 2 3 2 3 3 3 3 2 2 3 3 3 3 3 2 3 3 3 2 2 2 2 3 2 2 2 2 3 2 2 3 2 2 2 3 2 2 3 2 2 2 3 3 3 2 2 2 2 3 2 2 3 3 3 2 2 2 2 2 3 3", "output": "NO" }, { "input": "3\n0 0 1", "output": "NO" }, { "input": "10\n1 0 1 1 0 1 1 0 1 0", "output": "NO" }, { "input": "100\n1 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 1 1 1 1 0 1 1 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1", "output": "NO" }, { "input": "10\n8 4 0 0 6 1 9 8 0 6", "output": "YES" }, { "input": "100\n44 0 0 0 16 0 0 0 0 77 9 0 94 0 78 0 0 50 55 35 0 35 88 27 0 0 86 0 0 56 0 0 17 23 0 22 54 36 0 0 94 36 0 22 0 0 0 0 0 0 0 82 0 0 50 0 6 0 0 44 80 0 0 0 98 0 0 0 0 92 0 56 0 16 0 14 0 37 89 0 62 3 83 0 0 0 80 0 92 58 92 0 0 0 57 79 0 0 0 42", "output": "YES" }, { "input": "100\n37 92 14 95 3 37 0 0 0 84 27 33 0 0 0 74 74 0 35 72 46 29 8 92 1 76 47 0 38 82 0 81 54 7 61 46 91 0 86 0 80 0 0 98 88 0 4 0 0 52 0 0 82 0 33 35 0 36 58 52 1 50 29 0 0 24 0 69 97 65 13 0 30 0 14 66 47 94 22 24 8 92 67 0 34 0 0 0 84 85 50 33 0 99 67 73 21 0 0 62", "output": "YES" }, { "input": "100\n56 22 13 79 28 73 16 55 34 0 97 19 22 36 22 80 30 19 36 92 9 38 24 10 61 43 19 12 18 34 21 36 1 17 0 97 72 37 74 70 51 34 33 87 27 33 45 97 38 56 2 32 88 92 64 51 74 94 86 98 57 62 83 3 87 61 9 65 57 13 64 10 50 35 7 75 41 3 70 66 6 55 69 42 91 75 14 22 68 93 2 53 22 98 45 2 78 58 18 13", "output": "YES" }, { "input": "2\n1 4", "output": "NO" }, { "input": "4\n2 1 1 2", "output": "YES" }, { "input": "5\n1 1 1 0 1", "output": "NO" }, { "input": "4\n1 0 2 2", "output": "NO" }, { "input": "3\n3 2 1", "output": "YES" }, { "input": "2\n1 0", "output": "NO" }, { "input": "2\n1 2", "output": "NO" }, { "input": "3\n2 1 1", "output": "YES" }, { "input": "3\n3 0 0", "output": "NO" }, { "input": "9\n6 3 5 9 0 3 1 9 6", "output": "NO" }, { "input": "4\n1 0 1 1", "output": "NO" }, { "input": "4\n1 1 1 0", "output": "NO" }, { "input": "2\n1 5", "output": "YES" }, { "input": "3\n1 0 2", "output": "NO" }, { "input": "3\n1 2 2", "output": "NO" }, { "input": "3\n1 2 1", "output": "YES" }, { "input": "3\n1 4 1", "output": "YES" }, { "input": "3\n3 2 2", "output": "NO" } ]
108
20,172,800
0
67,268
249
Donkey and Stars
[ "data structures", "dp", "geometry", "math", "sortings" ]
null
null
In the evenings Donkey would join Shrek to look at the stars. They would sit on a log, sipping tea and they would watch the starry sky. The sky hung above the roof, right behind the chimney. Shrek's stars were to the right of the chimney and the Donkey's stars were to the left. Most days the Donkey would just count the stars, so he knew that they are exactly *n*. This time he wanted a challenge. He imagined a coordinate system: he put the origin of the coordinates at the intersection of the roof and the chimney, directed the *OX* axis to the left along the roof and the *OY* axis β€” up along the chimney (see figure). The Donkey imagined two rays emanating from he origin of axes at angles Ξ±1 and Ξ±2 to the *OX* axis. Now he chooses any star that lies strictly between these rays. After that he imagines more rays that emanate from this star at the same angles Ξ±1 and Ξ±2 to the *OX* axis and chooses another star that lies strictly between the new rays. He repeats the operation as long as there still are stars he can choose between the rays that emanate from a star. As a result, the Donkey gets a chain of stars. He can consecutively get to each star if he acts by the given rules. Your task is to find the maximum number of stars *m* that the Donkey's chain can contain. Note that the chain must necessarily start in the point of the origin of the axes, that isn't taken into consideration while counting the number *m* of stars in the chain.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of stars. The second line contains simple fractions representing relationships "*a*/*b* *c*/*d*", such that and (0<=≀<=*a*,<=*b*,<=*c*,<=*d*<=≀<=105; ; ; ). The given numbers *a*, *b*, *c*, *d* are integers. Next *n* lines contain pairs of integers *x**i*, *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=105)β€” the stars' coordinates. It is guaranteed that all stars have distinct coordinates.
In a single line print number *m* β€” the answer to the problem.
[ "15\n1/3 2/1\n3 1\n6 2\n4 2\n2 5\n4 5\n6 6\n3 4\n1 6\n2 1\n7 4\n9 3\n5 3\n1 3\n15 5\n12 4\n" ]
[ "4\n" ]
In the sample the longest chain the Donkey can build consists of four stars. Note that the Donkey can't choose the stars that lie on the rays he imagines.
[ { "input": "15\n1/3 2/1\n3 1\n6 2\n4 2\n2 5\n4 5\n6 6\n3 4\n1 6\n2 1\n7 4\n9 3\n5 3\n1 3\n15 5\n12 4", "output": "4" }, { "input": "15\n2/1 2/0\n3 1\n6 2\n9 3\n12 4\n15 5\n2 1\n4 2\n5 3\n7 4\n1 3\n3 4\n2 5\n4 5\n1 6\n6 6", "output": "1" }, { "input": "15\n2/1 2/0\n3 1\n6 2\n9 3\n12 4\n15 5\n2 1\n4 2\n5 3\n7 4\n1 3\n3 4\n2 6\n4 5\n1 6\n6 6", "output": "2" }, { "input": "15\n1/4 2/1\n3 1\n6 2\n9 3\n12 4\n15 5\n2 1\n4 2\n5 3\n7 4\n1 3\n3 4\n2 5\n4 5\n1 6\n6 6", "output": "5" }, { "input": "5\n3/24 24/3\n31394 23366\n27990 71363\n33642 36903\n79731 10588\n10907 5058", "output": "3" }, { "input": "5\n3/18 18/17\n84697 26074\n16334 31084\n38824 37740\n1288 50582\n87807 48721", "output": "2" }, { "input": "5\n3/18 18/17\n5148 38615\n84759 63111\n16345 23100\n49727 20597\n43590 46573", "output": "1" }, { "input": "5\n3/18 18/17\n49797 95131\n5075 96918\n91898 7865\n91852 41070\n12076 45049", "output": "1" }, { "input": "5\n3/18 18/17\n43008 52460\n68903 46619\n16613 30280\n66639 17904\n83797 83401", "output": "1" }, { "input": "5\n3/18 18/17\n66980 84763\n69224 39\n62888 61748\n53474 234\n77487 94808", "output": "1" }, { "input": "5\n3/18 18/17\n35429 29897\n89928 67711\n29047 22691\n84838 6917\n32683 99009", "output": "2" }, { "input": "5\n3/18 18/17\n62344 72564\n31069 2824\n74485 34763\n61186 78544\n75470 51019", "output": "1" }, { "input": "5\n27/18 27/17\n27746 42830\n22071 47985\n44242 62799\n16038 48367\n85158 21622", "output": "1" }, { "input": "5\n27/18 27/17\n91659 76441\n96317 38081\n99805 94867\n79758 84753\n96445 53616", "output": "0" }, { "input": "5\n27/18 27/17\n85006 4046\n10811 30171\n97316 32923\n73899 71559\n76723 17949", "output": "0" }, { "input": "5\n0/17 74/0\n24922 93126\n75686 80827\n33683 91759\n10584 66980\n58159 52129", "output": "2" }, { "input": "5\n0/17 74/0\n69711 29703\n91677 56040\n26051 78244\n20816 40897\n70770 35908", "output": "3" }, { "input": "5\n0/17 74/0\n68877 18122\n96115 84747\n71027 43746\n31622 3444\n93281 34803", "output": "4" }, { "input": "5\n3/24 24/3\n31394 23366\n27990 71363\n33642 36903\n79731 10588\n10907 5058", "output": "3" } ]
2,000
8,704,000
0
67,466
0
none
[ "none" ]
null
null
This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive. Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, but not happily ever after: one day a vicious dragon attacked the kingdom and stole Victoria. The King was full of grief, yet he gathered his noble knights and promised half of his kingdom and Victoria's hand in marriage to the one who will save the girl from the infernal beast. Having travelled for some time, the knights found the dragon's lair and all of them rushed there to save Victoria. Each knight spat on the dragon once and, as the dragon had quite a fragile and frail heart, his heart broke and poor beast died. As for the noble knights, they got Victoria right to the King and started brawling as each one wanted the girl's hand in marriage. The problem was that all the noble knights were equally noble and equally handsome, and Victoria didn't want to marry any of them anyway. Then the King (and he was a very wise man and didn't want to hurt anybody's feelings) decided to find out who will get his daughter randomly, i.e. tossing a coin. However, there turned out to be *n* noble knights and the coin only has two sides. The good thing is that when a coin is tossed, the coin falls on each side with equal probability. The King got interested how to pick one noble knight using this coin so that all knights had equal probability of being chosen (the probability in that case should always be equal to 1<=/<=*n*). First the King wants to know the expected number of times he will need to toss a coin to determine the winner. Besides, while tossing the coin, the King should follow the optimal tossing strategy (i.e. the strategy that minimizes the expected number of tosses). Help the King in this challenging task.
The first line contains a single integer *n* from the problem's statement (1<=≀<=*n*<=≀<=10000).
Print the sought expected number of tosses as an irreducible fraction in the following form: "*a*/*b*" (without the quotes) without leading zeroes.
[ "2\n", "3\n", "4\n" ]
[ "1/1\n", "8/3\n", "2/1\n" ]
none
[ { "input": "2", "output": "1/1" }, { "input": "3", "output": "8/3" }, { "input": "4", "output": "2/1" }, { "input": "8", "output": "3/1" }, { "input": "7", "output": "24/7" }, { "input": "6", "output": "11/3" }, { "input": "1", "output": "0/1" }, { "input": "5", "output": "18/5" }, { "input": "96", "output": "23/3" }, { "input": "54", "output": "377/57" }, { "input": "49", "output": "1985714/299593" }, { "input": "57", "output": "1118/171" }, { "input": "21", "output": "38/7" }, { "input": "43", "output": "896/129" }, { "input": "56", "output": "45/7" }, { "input": "46", "output": "13719/2047" }, { "input": "91", "output": "704/91" }, { "input": "13", "output": "306/65" }, { "input": "82", "output": "7739/1025" }, { "input": "69", "output": "32740246/4194303" }, { "input": "77", "output": "8215881550/1073741823" }, { "input": "27", "output": "320/57" }, { "input": "63", "output": "128/21" }, { "input": "60", "output": "94/15" }, { "input": "42", "output": "45/7" }, { "input": "29", "output": "89074/16385" }, { "input": "99", "output": "82792/10923" }, { "input": "19", "output": "2936/513" }, { "input": "89", "output": "15942/2047" }, { "input": "356", "output": "20036/2047" }, { "input": "377", "output": "42877948701338/4398046511105" }, { "input": "376", "output": "81794781/8388607" }, { "input": "199", "output": "5416016912792671923933831206744/633825300114114700748351602687" }, { "input": "563", "output": "13880251801665520090148870069821814422429790384486504048582895486382118580803670864520/1295112594817152713946307937882345937761604559368093014167874939825936190136805665451" }, { "input": "768", "output": "32/3" }, { "input": "777", "output": "242912325346/22906492245" }, { "input": "721", "output": "24323770714557246/2251799813685247" }, { "input": "629", "output": "49843473149688266962934/4722366482869645213695" }, { "input": "589", "output": "13383001136884230836493585742/1237940039285380274899124223" }, { "input": "698", "output": "261084475895343697268604849722313704014143081638278003/23945242826029513411849172299223580994042798784118785" }, { "input": "897", "output": "6303910445469575479185224346195379698702/604946430081668379490443746545365709255" }, { "input": "100", "output": "1548/205" }, { "input": "898", "output": "54181807382553033285317903788494851/5192296858534827628530496329220097" }, { "input": "778", "output": "266081190240146347461776796003542832080357458393960689731819/25108406941546723055343157692830665664409421777856138051585" }, { "input": "408", "output": "481/51" }, { "input": "915", "output": "24489920882263416/2329134352741105" }, { "input": "659", "output": "11506864999305980525685467247724160971763964558240405270397991163704744844086671519176045618744739064/1093625362391505962186251113558810682676584715446606218212885303204976499599687961611756588511526913" }, { "input": "380", "output": "17089848754/1762037865" }, { "input": "826", "output": "28584112877549284603281069376719144007233968270323045/2660582536225501490205463588802620110449199864902087" }, { "input": "570", "output": "81780152549/7635497415" }, { "input": "8947", "output": "9221776965061394108063074537665193339719130234791259353035383369908276748232005329587397633526133839630977366288410166284245740877083371222474025826991630911939336371916388075479778450420492837649418370715068725438291562232309367997828789596177392059429297899551357796421577750411209045517378683903876507895356659650107620108241824167563644901510340265372019179673453724120802026261156279978925379653814720997930734349648232984115741566886698873835913569553263892609588155620638724704727598288959685621550249737..." }, { "input": "5379", "output": "369970662679510300707473720654925140999880897391589444623141436368705516892112173326991585567498714963884175295883677699016/27543998699593691622996471168032126659516861790178384136860366469766907968179639409382883768321470201982967167709306596011" }, { "input": "2614", "output": "467288820759363880397722656040592964020719930972683440703193351374700405080631777580381050558531644513848619882285655907104502715940077345333622944378242416293600447930921066507506568704500773773633/37375513539561023231108477793896786533525327931380202951304745106630862169773485150256437750311906506986637800026885384689161869077507588081685801531164378630160340372359290471078905382884178132993" }, { "input": "6212", "output": "718828780749497450032379379564/52818775009509558395695966891" }, { "input": "3586", "output": "1026714465140783036738474632330359205383783819874722347437634384119469704928190591460057095203615899196742515080053210107395/82631996098781074868989413504096379978550585370535152410581099409300723904538918228148651304964410605948901503127919788033" }, { "input": "6629", "output": "2887307659130237302642554273423838808127851411579740408603950850448329124209429578113205979182558694432261838498919298006073282090488747091133824076367223385537560048772408256051559991222428913480943619030448796454451464748405569577077070495809049315150455270612613741854545427076463694803465943631755282225609026765330663439059215627258266275926212560658364641003632351846514061002732653254081394744335331237509798266695411546509090369792904212760685221160746857281893822469878668200610823671894328098385881817..." }, { "input": "9861", "output": "48205401549715737511233606679265031152996036546952288925330285615396586708825956490117819026249283326464870369563186432330830706792989284262067110327971987394928549360770263191516012692848869076552182149927367930957872055698792311482405327082065553619653685393875355497796244989262571944371234203749880818792381890143341459002264984197001955297069409069640536538793687219440766877509028035223421720178766484630650779161811795215598122460003343408475412680746717692338/3290879900222849827608052539430658202162644..." }, { "input": "1649", "output": "1103258059369546/93824992236885" }, { "input": "1108", "output": "829108303866668/70368744177665" }, { "input": "6704", "output": "3747509385261755254210316751920800389821077334621726013635892116/274250759553534340359161530426225084163789310938929977224768171" }, { "input": "8771", "output": "3770311067804988086442280897982793233953991174834038671591744622664235928848771481024534828492198884087581040596792755307166862630362254070801530083149662062951269890316117808935983174156710048296936374305714276676638609402088826404322038255398708095256451326373150897864019805504212731596330281332804357336587199175247125958363190591680678576959242590696722761770737808874948948372052533640442104091203823298252400438183921494229420351844306480773904754871502489649028646657382266084070860349033005494965904934..." }, { "input": "4710", "output": "62214231544449127/4503599627370495" }, { "input": "2337", "output": "6429067313146769086191243390708972817399071295570593226/510831846955296286119449009050103061206246374061200725" }, { "input": "7708", "output": "39413297508974631726391031213171923271142554912679270458193728426423636026646334915727139650713682692479792411879633606353758285856351703126/2977131414714805823690030317109266572712515013375254774912983855843898524112477893944078543723575564536883288499266264815757728270805630975" }, { "input": "5484", "output": "211086977989859276474150/15111572745182864683827" }, { "input": "2050", "output": "2663/205" }, { "input": "5157", "output": "1244202699616577925257908854992502879105187462860652753500143979369064245723363816152901984642185318105227079924186855121298620779636669909099525176016720566980036738379840484201060840762226140891649195248344160034243456960969500434409559402400997785076262368243485151264623913446406300409714366263836544017408112602389622392168343772273329588989257854547075621647134412375571624421845467345018113608142941220069412256286427238705568845394057266549279844034564540504288723831871872613761385100219013571797574528..." }, { "input": "9556", "output": "61105610582596188064013632976018238169594847263124154642386689594807697097885447064570207703083797375229286491941164437689434529990291086697099939303795410031817348526076989232509267027565641811597218817018817025267674646870116016782259903322684685324891049798873702815116504349778081974046190242424181610803910534907237209386333892868457625884598750479036332/4139057561631190052420042715397268145091328063809781231185874234711716512317727312355164552220203407039993009382683073987640607521830928131007598529502..." }, { "input": "7901", "output": "1545248712235484231234951857976545065744617617020030658469109057466859986475148250693905886674893272489053581743278765525029576600572532534838775161015285299867097071351763023768221772211614326132946083723216283578235838641012739144142278748089419497887052400481010672846336748523045688232788476455926025596760805229671163941642019404158795532714920358340283273984860096000931574655035232114889550219531272686479161110317036826198314000803468166009351225032738229182153921483640756378743625620038865132469106047..." }, { "input": "8622", "output": "2346970612497798292051684863778787677688918498337895665035233783242368128286257851467636458012981817792946447606933694463959158768680516918810044760234529638520868683195790661167605124671831213269266007988150361676380026247303195939614144448935172977317349504257500667344413574778276538084533093523373859439640543406392557232735743996270882880554311714917225257533788072187384837827098726745502145040757646538864148126423442625937245/15845010358053163603682882776020046370855826407419789577061413023873354878758..." }, { "input": "16", "output": "4/1" }, { "input": "32", "output": "5/1" }, { "input": "64", "output": "6/1" }, { "input": "128", "output": "7/1" }, { "input": "256", "output": "8/1" }, { "input": "512", "output": "9/1" }, { "input": "1024", "output": "10/1" }, { "input": "2048", "output": "11/1" }, { "input": "4096", "output": "12/1" }, { "input": "8192", "output": "13/1" }, { "input": "9749", "output": "2442073850489536993602927585016307199165621395304075025177694875024679607796284569131767726963170591399402108675638309823508923120869023271471818205166195909414421298770225330343221707377312426015931814666620011704161259290684411521150904067521292013955185317667861253504565429204675752674949251324122812583751343895693341865871684651273036507250959013187746544944138162398891218537343348931977619498001506833868902121065893913936425349903046456157409756042496452120477911621545767772676861905004685673876448424..." }, { "input": "9803", "output": "3273210180788326072665779956874407303695209430301336271835983946088930098968971592446649088493870209185601637297946849124915345946101994703881396454817629317067995483358046696632270413949397783371061147815617923539634120806955265009854911188530306501324761896490486400736980978270851173985761450205468514371802366150508965142349402634775190541932571215772934087049601511151015580218962449832712507548850463067009640345032653747650848021793733486635063672276271184797891474270402353352104959589784879937226619631..." }, { "input": "9851", "output": "1826526826552054078590833159938859608660168689393622926697593614966325247523385486843148169222905802492343158938765988786790268008018346250425185590729454696317705467641275241528094330016670372495410419378141651879792828682641532533284092879418090040903735626785107378204659838740412715571880884453837366724444432895654221478996531206045405344762139424591887085118650734474741760565377558510969213435666224432175984910232854534478442066899535808519463012771143160247271088007881997468588868391395961360449311260..." }, { "input": "9859", "output": "9737556487585955271085725726347548524587201515015415940974212696477918276264308244608931996051014559947727871548525843984646030062506833242765111803441650144268637762679345636011358150115775908768896005580214321628703868435548404239676226435996040392670484414990942576376634145721680636769353606720643265107519869112758891642047680407647874665865802380346668006876206490814588938569751811311435939234659864088396761136789798899162439354805109166450795279173094212597156554710639295669120966978022955523520243885..." }, { "input": "9883", "output": "3585169013008320587733453934540655406968508071250655174069676351966247095891666666457951370861261022535907218197677774600785190516321049571099175528120462561383326438548822604717683501452327840790897918994708328294710567693091965543331903184071652577937298158191755916197924842106107578819028940776121678861464188820255086932973183740793372165945865009415954786595728774195980645745127346052687115724325004061229398519190748075580806663121266449693574278118081790297078495193488775980028423355059820522243653725..." }, { "input": "9901", "output": "1838099364825091485842998238489502638352163208605775246129613825541675258139803932850287467779059152113767067936585018625986257156889365449198856216156597215096982506298229063072584364817360767528951048690402306736891920567120457283597208730483707878597646111737127186752299112571526574444743928839110241560725998077538870670908652964981237795923920133705668072188172055098674658571105524472734987571439013958589827622877679807533101349696441175687163721291688130495577638100992957486490904022264860238167307326..." }, { "input": "9907", "output": "1633354865670482634892575473274177878269848372373759525765811325846788929611278968402771127221384695483915945198456285004476851582174958025985911899376499975337986661537173411180428490907671107896805361798951388912266345066652077866312114944286489782939084013056891167373799137114155515056010000276816186615290579398553645497061457349859874907706345855178899820840538136570385474191197090563464515778875629438803382571649568677198891486414476226932831825123576296441036124053704853315256369077062694497961110434..." }, { "input": "9923", "output": "3760299283807515305392881669921685380087483626665387583310740045779392618490518061864438198412014466565364228291295311284131760183238506480296641521584717001189575119537493891940454933801650905249443352246953782854872613017048635048443614120554701976326536039594670095564945312513189153593550500517921950184962024588992584781239533465993501341159180970745350678104403343694528834994019085606787711931776684194936418570665879951352474415566347678136222813280481902061127946763865268688300550988394467968839427831..." }, { "input": "9941", "output": "3848534499658219667049727329663074105337547166730018729906447543650907057565599099052599064685569838374730308744072576703962547115876952975258439787965733453851059665347937329225608515273601271650034928543860491219634012020498814661622840677950496926059282612940876829862296013101963794076610277533664472961283348371953697283523069103642217248197905842345904997732309684469494496541394358109215746512001993248263898375663673449385592381926788206401567212276907962968353724108187030299614264372186122509235461661..." }, { "input": "9949", "output": "3077194576897431282984928614971803243979867746742349396701033435828399498432602651550466130139454008014151230282733882472359320178948173983098259522962570058314958623895543103765377873930945767189012935127692368514283490255979891724625021542152152743761996609145171758487359833408232379642800156557718632212879833607371512214716299467380836645557808645986902660996771461355482482475905883289978390754379236092379144196172866264243566348794250784667125654370098361595124994920872496780086986738805458903185126030..." }, { "input": "9991", "output": "6382125632197575640171909108366323999672572433833222272392411654952806480364944179628596547334889065794512754663260535014179926913634410060297543083266944529654281991142775738499030710183320227853166673816892482763220210176769895648821886542105816/436994993873214129706097166956708350993678881411295357199729151951767944417616335439228580716318181998128654620651240845861768505204366709906692902245553277900892247131030458103436298545516643924637451297481464347472084863384057367177715867713535" }, { "input": "9992", "output": "882713207347291313029281/60446290980731458735309" }, { "input": "9993", "output": "12637244675354581352253260560857374/865382809755804604755082721536683" }, { "input": "9994", "output": "3281117414508879498426129146296635638706673857559146714758804687655977321336441892014774756310161571021964653093136031059973674988535235552194295743721563055893425769046817776696216427896857321525164709814889404834572227298884316348149960471145881924249349826195785903333523568245055998853462086046866400139120072781311401680748431799894049040989328677849586317964939112513229643421712863477114408475285877161526889717094721450283390765651073229738491488236723709404792790505935059388430226031866694290751309103..." }, { "input": "9995", "output": "1368770541403820619075110203708490210616145992745821521870208914365828115565556194877572535511077690510688277376757546565243584175363368143317667278940670502781186329534839008398699279841764334491329910860701074569229951248069967340109056226002539889667430100999595433067983400778886042165596127864919572486395941238704720403024794261441096255620000217687954366591408789194462597191661175824028310400352/93746319107175952683864071964918454730461746778024627464635174121600584812748548703282543376385452193806936..." }, { "input": "9996", "output": "2016420396858486097238844042485568452071214924046/138111634978483258420134114867245645268334710595" }, { "input": "9997", "output": "115045178372494165897872226686512107429178048300340407805913417043457084371526821355671616896548808082243735275331446/7880401239278895842455808020028722761015947854093089333589658680849144354299442122282853250976983128161325598061363" }, { "input": "9998", "output": "4285402091468445389426164244750423198106729816197961669529078982307315890678968433526215498100171962888335447/293567822846729153486185074598667128421960318613539983838411371441526128139326055432962374798096087878991871" }, { "input": "9999", "output": "396504919788033353440140876437127916065460257739670266843919813860579129943101204509709504/27160479684459814483579275845458375480686245248879150008481872658058417330177822749111965" }, { "input": "10000", "output": "211285126026764324876224334024814529251789998319439411297242149907456038558/14474011154664524427946373126085988481658748083205070504932198000989141205" } ]
60
0
0
67,530
0
none
[ "none" ]
null
null
Radewoosh is playing a computer game. There are *n* levels, numbered 1 through *n*. Levels are divided into *k* regions (groups). Each region contains some positive number of consecutive levels. The game repeats the the following process: 1. If all regions are beaten then the game ends immediately. Otherwise, the system finds the first region with at least one non-beaten level. Let *X* denote this region.1. The system creates an empty bag for tokens. Each token will represent one level and there may be many tokens representing the same level. For each already beaten level *i* in the region *X*, the system adds *t**i* tokens to the bag (tokens representing the *i*-th level). 1. Let *j* denote the first non-beaten level in the region *X*. The system adds *t**j* tokens to the bag. 1. Finally, the system takes a uniformly random token from the bag and a player starts the level represented by the token. A player spends one hour and beats the level, even if he has already beaten it in the past. Given *n*, *k* and values *t*1,<=*t*2,<=...,<=*t**n*, your task is to split levels into regions. Each level must belong to exactly one region, and each region must contain non-empty consecutive set of levels. What is the minimum possible expected number of hours required to finish the game?
The first line of the input contains two integers *n* and *k* (1<=≀<=*n*<=≀<=200<=000, 1<=≀<=*k*<=≀<=*min*(50,<=*n*))Β β€” the number of levels and the number of regions, respectively. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≀<=*t**i*<=≀<=100<=000).
Print one real numberΒ β€” the minimum possible expected value of the number of hours spent to finish the game if levels are distributed between regions in the optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=4. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if .
[ "4 2\n100 3 5 7\n", "6 2\n1 2 4 8 16 32\n" ]
[ "5.7428571429\n", "8.5000000000\n" ]
In the first sample, we are supposed to split 4 levels into 2 regions. It's optimal to create the first region with only one level (it must be the first level). Then, the second region must contain other three levels. In the second sample, it's optimal to split levels into two regions with 3 levels each.
[]
3,000
10,035,200
0
67,594
679
Bear and Chase
[ "brute force", "dfs and similar", "graphs", "implementation", "math", "probabilities" ]
null
null
Bearland has *n* cities, numbered 1 through *n*. There are *m* bidirectional roads. The *i*-th road connects two distinct cities *a**i* and *b**i*. No two roads connect the same pair of cities. It's possible to get from any city to any other city (using one or more roads). The distance between cities *a* and *b* is defined as the minimum number of roads used to travel between *a* and *b*. Limak is a grizzly bear. He is a criminal and your task is to catch him, or at least to try to catch him. You have only two days (today and tomorrow) and after that Limak is going to hide forever. Your main weapon is BCD (Bear Criminal Detector). Where you are in some city, you can use BCD and it tells you the distance between you and a city where Limak currently is. Unfortunately, BCD can be used only once a day. You don't know much about Limak's current location. You assume that he is in one of *n* cities, chosen uniformly at random (each city with probability ). You decided for the following plan: 1. Choose one city and use BCD there. After using BCD you can try to catch Limak (but maybe it isn't a good idea). In this case you choose one city and check it. You win if Limak is there. Otherwise, Limak becomes more careful and you will never catch him (you loose). 1. Wait 24 hours to use BCD again. You know that Limak will change his location during that time. In detail, he will choose uniformly at random one of roads from his initial city, and he will use the chosen road, going to some other city. 1. Tomorrow, you will again choose one city and use BCD there. 1. Finally, you will try to catch Limak. You will choose one city and check it. You will win if Limak is there, and loose otherwise. Each time when you choose one of cities, you can choose any of *n* cities. Let's say it isn't a problem for you to quickly get somewhere. What is the probability of finding Limak, if you behave optimally?
The first line of the input contains two integers *n* and *m* (2<=≀<=*n*<=≀<=400, )Β β€” the number of cities and the number of roads, respectively. Then, *m* lines follow. The *i*-th of them contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*)Β β€” cities connected by the *i*-th road. No two roads connect the same pair of cities. It's possible to get from any city to any other city.
Print one real numberΒ β€” the probability of finding Limak, if you behave optimally. Your answer will be considered correct if its absolute error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if |*a*<=-<=*b*|<=≀<=10<=-<=6.
[ "3 3\n1 2\n1 3\n2 3\n", "5 4\n1 2\n3 1\n5 1\n1 4\n", "4 4\n1 2\n1 3\n2 3\n1 4\n", "5 5\n1 2\n2 3\n3 4\n4 5\n1 5\n" ]
[ "0.833333333333\n", "1.000000000000\n", "0.916666666667\n", "0.900000000000\n" ]
In the first sample test, there are three cities and there is a road between every pair of cities. Let's analyze one of optimal scenarios. 1. Use BCD in city 1. <li> With probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/64c94d13eeb330b494061e86538db66574ad0f7d.png" style="max-width: 100.0%;max-height: 100.0%;"/> Limak is in this city and BCD tells you that the distance is 0. You should try to catch him now and you win for sure. 1. With probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/14b21b617fcd2e25700376368355f7bbf975d8de.png" style="max-width: 100.0%;max-height: 100.0%;"/> the distance is 1 because Limak is in city 2 or city 3. In this case you should wait for the second day. </li>1. You wait and Limak moves to some other city. <li> There is probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that Limak was in city 2 and then went to city 3. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 2 to 1. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 3 to 2. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 3 to 1. </li>1. Use BCD again in city 1 (though it's allowed to use it in some other city). <li> If the distance is 0 then you're sure Limak is in this city (you win). 1. If the distance is 1 then Limak is in city 2 or city 3. Then you should guess that he is in city 2 (guessing city 3 would be fine too). </li> - There is probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that Limak was in city 2 and then went to city 3. - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 2 to 1. - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 3 to 2. - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 3 to 1. You loose only if Limak was in city 2 first and then he moved to city 3. The probability of loosing is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/55bd5cf64609940bba840be4d13d882b3b49c511.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/56d7c14b8db6605aa95b947bca89a66828b49171.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[]
30
0
0
67,625
980
Perfect Groups
[ "dp", "math", "number theory" ]
null
null
SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be divided into, such that the product of any pair of integers in the same group is a perfect square. Each integer must be in exactly one group. However, integers in a group do not necessarily have to be contiguous in the array. SaMer wishes to create more cases from the test case he already has. His test case has an array $A$ of $n$ integers, and he needs to find the number of contiguous subarrays of $A$ that have an answer to the problem equal to $k$ for each integer $k$ between $1$ and $n$ (inclusive).
The first line of input contains a single integer $n$ ($1 \leq n \leq 5000$), the size of the array. The second line contains $n$ integers $a_1$,$a_2$,$\dots$,$a_n$ ($-10^8 \leq a_i \leq 10^8$), the values of the array.
Output $n$ space-separated integers, the $k$-th integer should be the number of contiguous subarrays of $A$ that have an answer to the problem equal to $k$.
[ "2\n5 5\n", "5\n5 -4 2 1 8\n", "1\n0\n" ]
[ "3 0\n", "5 5 3 2 0\n", "1\n" ]
none
[ { "input": "2\n5 5", "output": "3 0" }, { "input": "5\n5 -4 2 1 8", "output": "5 5 3 2 0" }, { "input": "1\n0", "output": "1" }, { "input": "3\n-10 -5 7", "output": "3 2 1" }, { "input": "5\n-6 -7 -2 -3 -10", "output": "5 4 3 2 1" }, { "input": "8\n-5 5 10 -5 9 -2 5 7", "output": "8 7 7 5 6 3 0 0" }, { "input": "19\n-51 -47 -57 57 -62 41 42 53 47 -7 -41 -37 -41 -46 -47 41 -39 -59 -50", "output": "19 19 18 17 16 15 14 13 12 11 10 11 7 4 3 1 0 0 0" }, { "input": "25\n94 -94 61 -98 89 -91 83 61 -3 53 91 61 -3 -3 95 -67 -73 -59 -73 37 -47 -3 94 -79 -73", "output": "26 26 25 29 27 26 27 27 23 20 14 16 10 10 7 8 4 0 0 0 0 0 0 0 0" }, { "input": "80\n8861 -8846 -3257 8263 -8045 4549 9626 -8599 5755 -3559 5813 -7411 9151 -1847 2441 4201 2381 4651 -6571 199 -6737 -6333 -9433 -4967 9041 -9319 6801 5813 -2731 -8599 7681 -1303 -7333 6067 -1847 -6333 6047 -7907 -7333 8677 -937 829 2441 -5702 -8045 -3037 -4882 -7703 9001 -5702 -2039 -7493 9173 -6047 6046 5802 -8609 9173 8083 8291 -1453 2381 7321 -859 8539 -2011 2374 8263 8111 -5897 -2017 -2311 -571 4993 -3389 -7789 1847 -4799 4651 -5702", "output": "80 79 78 77 77 78 77 76 75 74 73 72 72 71 70 71 69 68 67 66 68 66 69 64 66 65 63 60 58 57 55 56 52 53 48 48 46 45 44 41 42 42 39 39 36 35 33 33 31 29 28 27 25 26 23 22 25 22 15 16 12 13 9 12 5 4 3 0 0 0 0 0 0 0 0 0 0 0 0 0" }, { "input": "1\n-3622679", "output": "1" }, { "input": "2\n6649609 -8006683", "output": "2 1" }, { "input": "4\n0 -7955897 3123413 -7955897", "output": "5 5 0 0" }, { "input": "3\n1 2 0", "output": "4 2 0" }, { "input": "2\n-7 0", "output": "3 0" }, { "input": "5\n5 0 0 0 0", "output": "15 0 0 0 0" }, { "input": "3\n0 1 2", "output": "4 2 0" }, { "input": "30\n1 2 0 2 1 0 0 1 2 0 1 2 3 0 4 1 0 0 0 3 2 0 1 0 1 0 5 0 6 0", "output": "77 90 216 42 40 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" }, { "input": "2\n2 0", "output": "3 0" }, { "input": "10\n0 0 5 0 0 5 0 0 5 0", "output": "55 0 0 0 0 0 0 0 0 0" }, { "input": "2\n0 2", "output": "3 0" }, { "input": "4\n1 0 1 0", "output": "10 0 0 0" }, { "input": "2\n0 1", "output": "3 0" }, { "input": "6\n5 -4 0 2 1 8", "output": "8 8 3 2 0 0" }, { "input": "3\n1 0 1", "output": "6 0 0" }, { "input": "3\n1 0 -1", "output": "5 1 0" }, { "input": "5\n0 1 2 3 4", "output": "6 4 5 0 0" } ]
1,000
31,129,600
0
67,777
575
Fibonotci
[ "data structures", "math", "matrices" ]
null
null
Fibonotci sequence is an integer recursive sequence defined by the recurrence relation Sequence *s* is an infinite and almost cyclic sequence with a cycle of length *N*. A sequence *s* is called almost cyclic with a cycle of length *N* if , for *i*<=β‰₯<=*N*, except for a finite number of values *s**i*, for which (*i*<=β‰₯<=*N*). Following is an example of an almost cyclic sequence with a cycle of length 4: Notice that the only value of *s* for which the equality does not hold is *s*6 (*s*6<==<=7 and *s*2<==<=8). You are given *s*0,<=*s*1,<=...*s**N*<=-<=1 and all the values of sequence *s* for which (*i*<=β‰₯<=*N*). Find .
The first line contains two numbers *K* and *P*. The second line contains a single number *N*. The third line contains *N* numbers separated by spaces, that represent the first *N* numbers of the sequence *s*. The fourth line contains a single number *M*, the number of values of sequence *s* for which . Each of the following *M* lines contains two numbers *j* and *v*, indicating that and *s**j*<==<=*v*. All j-s are distinct. - 1<=≀<=*N*,<=*M*<=≀<=50000 - 0<=≀<=*K*<=≀<=1018 - 1<=≀<=*P*<=≀<=109 - 1<=≀<=*s**i*<=≀<=109, for all *i*<==<=0,<=1,<=...*N*<=-<=1 - *N*<=≀<=*j*<=≀<=1018 - 1<=≀<=*v*<=≀<=109 - All values are integers
Output should contain a single integer equal to .
[ "10 8\n3\n1 2 1\n2\n7 3\n5 4\n" ]
[ "4\n" ]
none
[]
2,000
17,715,200
0
67,854
660
Bear and Bowling 4
[ "binary search", "data structures", "divide and conquer", "geometry", "ternary search" ]
null
null
Limak is an old brown bear. He often goes bowling with his friends. Today he feels really good and tries to beat his own record! For rolling a ball one gets a score β€” an integer (maybe negative) number of points. Score for the *i*-th roll is multiplied by *i* and scores are summed up. So, for *k* rolls with scores *s*1,<=*s*2,<=...,<=*s**k*, the total score is . The total score is 0 if there were no rolls. Limak made *n* rolls and got score *a**i* for the *i*-th of them. He wants to maximize his total score and he came up with an interesting idea. He can say that some first rolls were only a warm-up, and that he wasn't focused during the last rolls. More formally, he can cancel any prefix and any suffix of the sequence *a*1,<=*a*2,<=...,<=*a**n*. It is allowed to cancel all rolls, or to cancel none of them. The total score is calculated as if there were only non-canceled rolls. So, the first non-canceled roll has score multiplied by 1, the second one has score multiplied by 2, and so on, till the last non-canceled roll. What maximum total score can Limak get?
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the total number of rolls made by Limak. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≀<=107) β€” scores for Limak's rolls.
Print the maximum possible total score after cancelling rolls.
[ "6\n5 -1000 1 -3 7 -8\n", "5\n1000 1000 1001 1000 1000\n", "3\n-60 -70 -80\n" ]
[ "16\n", "15003\n", "0\n" ]
In the first sample test, Limak should cancel the first two rolls, and one last roll. He will be left with rolls 1,  - 3, 7 what gives him the total score 1Β·1 + 2Β·( - 3) + 3Β·7 = 1 - 6 + 21 = 16.
[ { "input": "6\n5 -1000 1 -3 7 -8", "output": "16" }, { "input": "5\n1000 1000 1001 1000 1000", "output": "15003" }, { "input": "3\n-60 -70 -80", "output": "0" }, { "input": "1\n-4", "output": "0" }, { "input": "2\n-3 6", "output": "9" }, { "input": "3\n8 1 -6", "output": "10" }, { "input": "4\n9 2 -5 1", "output": "13" }, { "input": "5\n10 -3 -3 8 2", "output": "37" }, { "input": "6\n3 1 -9 1 2 -10", "output": "5" }, { "input": "7\n-3 -7 -7 -9 -3 7 -9", "output": "11" }, { "input": "8\n-2 1 -5 -2 1 -9 0 2", "output": "4" }, { "input": "9\n-1 10 -8 -9 -7 8 6 -6 7", "output": "38" }, { "input": "10\n-9 -10 -9 4 6 8 3 -8 0 10", "output": "100" }, { "input": "65\n349 -152 -35 -353 -647 -702 64 299 -431 -11 -185 437 237 -103 1 448 23 -308 -689 329 -409 309 424 -93 -192 0 257 -90 -394 -512 -148 376 -394 -528 212 -215 -255 -684 -321 503 -72 -227 -583 -537 -65 444 -332 465 -547 291 -663 -235 542 -89 -450 -212 438 12 139 -558 -87 433 -462 79 35", "output": "6676" }, { "input": "5\n7 -5 3 -9 8", "output": "10" }, { "input": "7\n-7 0 10 1 -1 -5 6", "output": "34" }, { "input": "7\n3 -10 -2 5 2 -7 7", "output": "21" }, { "input": "4\n0 -7 1 -9", "output": "1" }, { "input": "4\n4 -6 3 3", "output": "13" }, { "input": "10\n-9 8 0 -4 -4 -3 -5 9 -6 -9", "output": "14" }, { "input": "6\n3 -5 -5 1 -6 -2", "output": "3" }, { "input": "9\n8 -2 -8 4 -8 8 -3 -8 0", "output": "12" }, { "input": "6\n3 3 0 -7 6 -6", "output": "11" }, { "input": "7\n5 -6 -2 6 -2 -4 -3", "output": "11" } ]
46
0
0
67,952
848
Days of Floral Colours
[ "combinatorics", "divide and conquer", "dp", "fft", "math" ]
null
null
The Floral Clock has been standing by the side of Mirror Lake for years. Though unable to keep time, it reminds people of the passage of time and the good old days. On the rim of the Floral Clock are 2*n* flowers, numbered from 1 to 2*n* clockwise, each of which has a colour among all *n* possible ones. For each colour, there are exactly two flowers with it, the distance between which either is less than or equal to 2, or equals *n*. Additionally, if flowers *u* and *v* are of the same colour, then flowers opposite to *u* and opposite to *v* should be of the same colour as well β€” symmetry is beautiful! Formally, the distance between two flowers is 1 plus the number of flowers on the minor arc (or semicircle) between them. Below is a possible arrangement with *n*<==<=6 that cover all possibilities. The beauty of an arrangement is defined to be the product of the lengths of flower segments separated by all opposite flowers of the same colour. In other words, in order to compute the beauty, we remove from the circle all flowers that have the same colour as flowers opposite to them. Then, the beauty is the product of lengths of all remaining segments. Note that we include segments of length 0 in this product. If there are no flowers that have the same colour as flower opposite to them, the beauty equals 0. For instance, the beauty of the above arrangement equals 1<=Γ—<=3<=Γ—<=1<=Γ—<=3<==<=9 β€” the segments are {2}, {4,<=5,<=6}, {8} and {10,<=11,<=12}. While keeping the constraints satisfied, there may be lots of different arrangements. Find out the sum of beauty over all possible arrangements, modulo 998<=244<=353. Two arrangements are considered different, if a pair (*u*,<=*v*) (1<=≀<=*u*,<=*v*<=≀<=2*n*) exists such that flowers *u* and *v* are of the same colour in one of them, but not in the other.
The first and only line of input contains a lonely positive integer *n* (3<=≀<=*n*<=≀<=50<=000)Β β€” the number of colours present on the Floral Clock.
Output one integer β€” the sum of beauty over all possible arrangements of flowers, modulo 998<=244<=353.
[ "3\n", "4\n", "7\n", "15\n" ]
[ "24\n", "4\n", "1316\n", "3436404\n" ]
With *n* = 3, the following six arrangements each have a beauty of 2 × 2 = 4. While many others, such as the left one in the figure below, have a beauty of 0. The right one is invalid, since it's asymmetric.
[ { "input": "3", "output": "24" }, { "input": "4", "output": "4" }, { "input": "7", "output": "1316" }, { "input": "15", "output": "3436404" }, { "input": "10", "output": "26200" }, { "input": "99", "output": "620067986" }, { "input": "1317", "output": "414025" }, { "input": "50000", "output": "475800099" }, { "input": "5", "output": "240" }, { "input": "6", "output": "204" }, { "input": "8", "output": "2988" }, { "input": "9", "output": "6720" }, { "input": "11", "output": "50248" }, { "input": "12", "output": "174280" }, { "input": "13", "output": "436904" }, { "input": "14", "output": "1140888" }, { "input": "16", "output": "8348748" }, { "input": "17", "output": "24631232" }, { "input": "18", "output": "64575924" }, { "input": "19", "output": "174658944" }, { "input": "20", "output": "488230244" }, { "input": "33", "output": "823529776" }, { "input": "39", "output": "302870971" }, { "input": "89", "output": "530141864" }, { "input": "144", "output": "395837543" }, { "input": "233", "output": "422271260" }, { "input": "396", "output": "994574954" }, { "input": "418", "output": "57956054" }, { "input": "431", "output": "767293469" }, { "input": "831", "output": "418821250" }, { "input": "985", "output": "574051668" }, { "input": "998", "output": "452930999" }, { "input": "1000", "output": "945359814" }, { "input": "2017", "output": "222633425" }, { "input": "3939", "output": "582943734" }, { "input": "5000", "output": "148029988" }, { "input": "8081", "output": "473740780" }, { "input": "10000", "output": "938538566" }, { "input": "10001", "output": "552705744" }, { "input": "10492", "output": "914991759" }, { "input": "20178", "output": "207394683" }, { "input": "23333", "output": "259575428" }, { "input": "25252", "output": "306102706" }, { "input": "30000", "output": "583465411" }, { "input": "35000", "output": "520751787" }, { "input": "39393", "output": "929692433" }, { "input": "40404", "output": "618777849" }, { "input": "45000", "output": "672059275" }, { "input": "49997", "output": "645043850" }, { "input": "49999", "output": "791828238" } ]
46
0
0
68,015
733
Epidemic in Monstropolis
[ "constructive algorithms", "dp", "greedy", "two pointers" ]
null
null
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. Soon, monsters became hungry and began to eat each other. One 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*. For 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: 1. the first monster can't eat the second monster because *a*1<==<=1 is not greater than *a*2<==<=2; 1. the second monster can't eat the third monster because *a*2<==<=2 is not greater than *a*3<==<=2; 1. the second monster can't eat the fifth monster because they are not neighbors; 1. the second monster can eat the first monster, the queue will be transformed to [3,<=2,<=2,<=1,<=2]. After some time, someone said a good joke and all monsters recovered. At that moment there were *k* (*k*<=≀<=*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. You 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.
The first line contains single integer *n* (1<=≀<=*n*<=≀<=500)Β β€” the number of monsters in the initial queue. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=106)Β β€” the initial weights of the monsters. The third line contains single integer *k* (1<=≀<=*k*<=≀<=*n*)Β β€” the number of monsters in the queue after the joke. The fourth line contains *k* integers *b*1,<=*b*2,<=...,<=*b**k* (1<=≀<=*b**j*<=≀<=5Β·108)Β β€” the weights of the monsters after the joke. Monsters are listed in the order from the beginning of the queue to the end.
In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line. Otherwise 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*Β β€” 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. When one monster eats another the queue decreases. If there are several answers, print any of them.
[ "6\n1 2 2 2 1 2\n2\n5 5\n", "5\n1 2 3 4 5\n1\n15\n", "5\n1 1 1 3 3\n3\n2 1 6\n" ]
[ "YES\n2 L\n1 R\n4 L\n3 L\n", "YES\n5 L\n4 L\n3 L\n2 L\n", "NO" ]
In 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]. Note that for each step the output contains numbers of the monsters in their current order in the queue.
[ { "input": "6\n1 2 2 2 1 2\n2\n5 5", "output": "YES\n2 L\n1 R\n4 L\n3 L" }, { "input": "5\n1 2 3 4 5\n1\n15", "output": "YES\n5 L\n4 L\n3 L\n2 L" }, { "input": "5\n1 1 1 3 3\n3\n2 1 6", "output": "NO" }, { "input": "5\n1 1 1 1 2\n3\n1 1 4", "output": "YES\n5 L\n4 L" }, { "input": "5\n1 1 1 1 1\n4\n1 1 2 1", "output": "NO" }, { "input": "6\n2 1 2 2 1 2\n2\n5 5", "output": "YES\n3 L\n2 L\n4 L\n3 L" }, { "input": "8\n2 5 3 1 4 2 3 4\n3\n10 6 8", "output": "NO" }, { "input": "1\n959139\n1\n470888", "output": "NO" }, { "input": "3\n2 2 1\n1\n5", "output": "YES\n2 R\n2 L" }, { "input": "3\n1 2 2\n1\n5", "output": "YES\n2 L\n1 R" }, { "input": "5\n1 2 3 4 5\n1\n10", "output": "NO" }, { "input": "5\n325539 329221 106895 882089 718673\n5\n699009 489855 430685 939232 282330", "output": "NO" }, { "input": "10\n30518 196518 274071 359971 550121 204862 843967 173607 619138 690754\n3\n171337 183499 549873", "output": "NO" }, { "input": "3\n2 1 1\n1\n3", "output": "NO" }, { "input": "4\n2 2 2 1\n3\n2 2 2", "output": "NO" }, { "input": "3\n1 2 3\n1\n3", "output": "NO" }, { "input": "2\n1 2\n2\n3 1", "output": "NO" }, { "input": "5\n3 3 2 2 1\n2\n8 3", "output": "YES\n2 R\n2 L\n2 R" }, { "input": "3\n3 2 5\n1\n10", "output": "YES\n3 L\n2 L" }, { "input": "3\n1 5 1\n1\n6", "output": "NO" }, { "input": "5\n1 2 3 4 5\n3\n1 2 3", "output": "NO" }, { "input": "3\n5 2 3\n1\n10", "output": "YES\n1 R\n1 R" }, { "input": "3\n2 1 3\n1\n6", "output": "YES\n3 L\n2 L" }, { "input": "3\n3 2 1\n1\n6", "output": "YES\n1 R\n1 R" }, { "input": "2\n5 5\n1\n5", "output": "NO" }, { "input": "3\n1 2 3\n2\n1 2", "output": "NO" }, { "input": "4\n1 2 3 4\n3\n1 2 3", "output": "NO" }, { "input": "4\n4 3 2 1\n3\n3 2 1", "output": "NO" }, { "input": "2\n5 3\n1\n5", "output": "NO" }, { "input": "5\n1 1 1 1 1\n4\n1 1 1 1", "output": "NO" }, { "input": "3\n3 3 2\n1\n8", "output": "YES\n2 R\n2 L" }, { "input": "8\n2 2 1 2 2 1 2 4\n2\n9 8", "output": "NO" }, { "input": "4\n3 2 1 4\n3\n3 2 1", "output": "NO" }, { "input": "5\n3 3 2 3 1\n2\n11 1", "output": "YES\n4 L\n3 L\n2 L" }, { "input": "3\n2 1 3\n1\n3", "output": "NO" }, { "input": "4\n2 3 3 2\n2\n5 3", "output": "NO" }, { "input": "16\n2 2 2 1 2 2 2 1 1 2 2 2 1 2 2 2\n4\n7 7 7 7", "output": "YES\n3 R\n3 L\n2 L\n4 R\n4 L\n3 L\n4 L\n3 R\n3 R\n5 L\n4 R\n4 R" }, { "input": "2\n1 1\n1\n1", "output": "NO" }, { "input": "3\n1 2 1\n2\n3 2", "output": "NO" }, { "input": "3\n2 3 5\n1\n10", "output": "YES\n3 L\n2 L" }, { "input": "5\n1 2 3 4 5\n2\n3 7", "output": "NO" }, { "input": "4\n1 2 3 4\n2\n1 2", "output": "NO" }, { "input": "8\n1 2 2 2 1 2 1 1\n2\n5 5", "output": "NO" }, { "input": "3\n5 5 4\n1\n14", "output": "YES\n2 R\n2 L" }, { "input": "22\n3 2 3 3 3 1 1 2 1 2 1 1 1 2 2 3 1 2 3 3 3 3\n5\n5 16 5 5 15", "output": "YES\n1 R\n4 R\n4 R\n4 R\n4 R\n4 R\n4 L\n3 L\n6 L\n5 L\n4 L\n5 L\n7 L\n6 L\n5 R\n5 R\n5 R" }, { "input": "4\n2 2 1 2\n1\n7", "output": "YES\n4 L\n3 L\n2 L" }, { "input": "7\n2 2 2 1 2 2 2\n1\n13", "output": "YES\n5 L\n4 L\n3 L\n2 L\n1 R\n1 R" }, { "input": "2\n1 2\n1\n1", "output": "NO" }, { "input": "14\n5 5 5 5 4 4 4 3 3 3 4 4 4 4\n3\n32 21 4", "output": "YES\n4 R\n4 R\n4 R\n4 L\n3 L\n2 L\n5 L\n4 L\n3 L\n2 R\n2 R" }, { "input": "5\n2 2 1 2 2\n1\n9", "output": "YES\n4 L\n3 L\n2 L\n1 R" }, { "input": "1\n2\n1\n2", "output": "YES" }, { "input": "23\n3 2 1 3 3 3 1 1 2 1 2 1 1 1 2 2 3 1 2 3 3 3 3\n5\n6 16 5 5 15", "output": "YES\n1 R\n1 R\n4 R\n4 R\n4 R\n4 R\n4 R\n4 L\n3 L\n6 L\n5 L\n4 L\n5 L\n7 L\n6 L\n5 R\n5 R\n5 R" } ]
0
0
-1
68,253
553
Nudist Beach
[ "binary search", "graphs", "greedy" ]
null
null
Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Life Fibers. There are *n* cities, labeled from 1 to *n*, and *m* bidirectional roads between them. Currently, there are Life Fibers in every city. In addition, there are *k* cities that are fortresses of the Life Fibers that cannot be captured under any circumstances. So, the Nudist Beach can capture an arbitrary non-empty subset of cities with no fortresses. After the operation, Nudist Beach will have to defend the captured cities from counterattack. If they capture a city and it is connected to many Life Fiber controlled cities, it will be easily defeated. So, Nudist Beach would like to capture a set of cities such that for each captured city the ratio of Nudist Beach controlled neighbors among all neighbors of that city is as high as possible. More formally, they would like to capture a non-empty set of cities *S* with no fortresses of Life Fibers. The strength of a city is defined as (number of neighbors of *x* in *S*) / (total number of neighbors of *x*). Here, two cities are called neighbors if they are connnected with a road. The goal is to maximize the strength of the weakest city in *S*. Given a description of the graph, and the cities with fortresses, find a non-empty subset that maximizes the strength of the weakest city.
The first line of input contains three integers *n*,<=*m*,<=*k* (2<=<=≀<=<=*n*<=<=≀<=100<=000, 1<=≀<=*m*<=≀<=100<=000, 1<=≀<=*k*<=≀<=*n*<=-<=1). The second line of input contains *k* integers, representing the cities with fortresses. These cities will all be distinct. The next *m* lines contain the roads. The *i*-th of these lines will have 2 integers *a**i*,<=*b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*). Every city will have at least one road adjacent to it. There is no more than one road between each pair of the cities.
The first line should contain an integer *r*, denoting the size of an optimum set (1<=≀<=*r*<=≀<=*n*<=-<=*k*). The second line should contain *r* integers, denoting the cities in the set. Cities may follow in an arbitrary order. This line should not contain any of the cities with fortresses. If there are multiple possible answers, print any of them.
[ "9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9\n", "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4\n" ]
[ "3\n1 4 5\n", "8\n1 5 4 8 10 6 3 7\n" ]
The first example case achieves a strength of 1/2. No other subset is strictly better. The second example case achieves a strength of 1. Note that the subset doesn't necessarily have to be connected.
[ { "input": "9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9", "output": "3\n5 1 4" }, { "input": "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4", "output": "8\n3 10 6 1 5 8 7 4" }, { "input": "2 1 1\n1\n2 1", "output": "1\n2" } ]
452
30,617,600
-1
68,355
804
Expected diameter of a tree
[ "binary search", "brute force", "dfs and similar", "dp", "sortings", "trees" ]
null
null
Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem. We have a forest (acyclic undirected graph) with *n* vertices and *m* edges. There are *q* queries we should answer. In each query two vertices *v* and *u* are given. Let *V* be the set of vertices in the connected component of the graph that contains *v*, and *U* be the set of vertices in the connected component of the graph that contains *u*. Let's add an edge between some vertex and some vertex in and compute the value *d* of the resulting component. If the resulting component is a tree, the value *d* is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of *d*, if we choose vertices *a* and *b* from the sets uniformly at random? Can you help Pasha to solve this problem? The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices. Note that queries don't add edges to the initial forest.
The first line contains three integers *n*, *m* and *q*(1<=≀<=*n*,<=*m*,<=*q*<=≀<=105)Β β€” the number of vertices, the number of edges in the graph and the number of queries. Each of the next *m* lines contains two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*), that means there is an edge between vertices *u**i* and *v**i*. It is guaranteed that the given graph is a forest. Each of the next *q* lines contains two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*)Β β€” the vertices given in the *i*-th query.
For each query print the expected value of *d* as described in the problem statement. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Let's assume that your answer is *a*, and the jury's answer is *b*. The checker program will consider your answer correct, if .
[ "3 1 2\n1 3\n3 1\n2 3\n", "5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5\n" ]
[ "-1\n2.0000000000\n", "-1\n2.6666666667\n2.6666666667\n" ]
In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2. In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f12c59a7dfd20580ff1e8e5eeab9ecd19cb3c3f1.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "3 1 2\n1 3\n3 1\n2 3", "output": "-1\n2.0000000000" }, { "input": "5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5", "output": "-1\n2.6666666667\n2.6666666667" }, { "input": "17 15 13\n3 15\n3 1\n15 9\n16 6\n1 5\n1 8\n16 12\n15 7\n9 4\n6 11\n15 14\n9 10\n15 13\n1 17\n11 2\n7 3\n9 6\n9 7\n1 8\n14 13\n16 16\n14 6\n4 4\n3 4\n9 3\n8 13\n15 2\n14 4", "output": "-1\n8.4500000000\n-1\n-1\n-1\n-1\n8.4500000000\n-1\n-1\n-1\n-1\n8.4500000000\n-1" }, { "input": "7 4 31\n1 4\n1 7\n4 6\n5 2\n6 2\n1 1\n3 2\n6 4\n1 6\n3 2\n2 1\n7 3\n7 6\n3 3\n1 1\n4 3\n6 7\n4 4\n3 2\n1 6\n2 6\n1 4\n2 3\n4 2\n4 3\n5 3\n7 5\n3 3\n2 5\n6 1\n4 6\n6 3\n7 2\n1 7\n4 7", "output": "4.5000000000\n-1\n2.0000000000\n-1\n-1\n2.0000000000\n4.5000000000\n3.5000000000\n-1\n-1\n-1\n3.5000000000\n-1\n-1\n2.0000000000\n-1\n4.5000000000\n-1\n2.0000000000\n4.5000000000\n3.5000000000\n2.0000000000\n4.5000000000\n-1\n-1\n-1\n-1\n3.5000000000\n4.5000000000\n-1\n-1" }, { "input": "30 22 21\n1 21\n29 12\n23 7\n7 30\n11 10\n25 2\n10 8\n11 26\n29 13\n28 24\n4 5\n1 20\n20 9\n8 16\n26 14\n1 19\n25 27\n28 22\n5 6\n28 15\n29 18\n10 3\n17 15\n23 23\n5 29\n5 16\n18 8\n25 20\n22 18\n9 18\n6 20\n14 4\n21 23\n30 27\n12 15\n15 14\n26 4\n20 13\n14 17\n11 17\n21 4\n10 11\n21 21", "output": "2.7500000000\n-1\n4.4166666667\n6.6666666667\n6.7500000000\n5.2666666667\n4.5000000000\n5.3500000000\n5.2666666667\n6.6666666667\n5.2666666667\n4.3333333333\n4.5000000000\n6.7500000000\n6.6666666667\n5.3500000000\n5.2857142857\n5.2857142857\n5.2666666667\n-1\n-1" }, { "input": "33 23 15\n15 10\n2 24\n2 14\n19 25\n33 27\n14 21\n13 26\n19 30\n13 9\n33 17\n20 18\n27 22\n25 6\n15 7\n17 32\n14 8\n19 11\n22 16\n7 23\n8 12\n11 3\n30 4\n22 31\n11 10\n9 8\n11 16\n18 23\n14 4\n31 27\n20 32\n18 11\n18 14\n6 10\n3 17\n20 33\n26 9\n27 18\n22 13", "output": "6.7857142857\n5.8333333333\n8.4285714286\n4.5000000000\n7.4523809524\n-1\n6.1428571429\n5.2857142857\n5.1666666667\n6.7857142857\n8.4285714286\n6.1428571429\n-1\n6.1428571429\n6.8095238095" }, { "input": "44 34 9\n23 12\n31 24\n39 37\n16 20\n16 10\n23 28\n25 9\n20 13\n24 14\n5 26\n26 42\n8 17\n13 30\n9 35\n28 15\n39 38\n5 4\n20 6\n38 41\n24 40\n15 2\n20 18\n21 3\n31 34\n16 7\n26 22\n34 27\n41 11\n34 19\n28 43\n11 1\n2 29\n41 32\n16 33\n21 1\n30 15\n44 11\n30 27\n4 7\n8 31\n2 35\n18 7\n35 17", "output": "6.0000000000\n8.3333333333\n5.2857142857\n7.7619047619\n6.9333333333\n5.4285714286\n6.6666666667\n-1\n3.6666666667" }, { "input": "19 16 3\n1 4\n4 16\n16 5\n17 12\n4 7\n3 9\n12 14\n5 19\n17 18\n4 13\n1 6\n3 2\n7 8\n16 15\n15 11\n15 10\n2 16\n7 10\n18 8", "output": "6.9166666667\n-1\n7.7500000000" } ]
3,000
22,323,200
0
68,586
209
Pixels
[ "constructive algorithms", "math" ]
null
null
Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that is, the total number of pixels decreases by one). Besides, if pixels of colors *x* and *y* (*x*<=β‰ <=*y*) meet in a violent fight, then the pixel that survives the fight immediately changes its color to *z* (*z*<=β‰ <=*x*;Β *z*<=β‰ <=*y*). Pixels of the same color are friends, so they don't fight. The King of Flatland knows that his land will be peaceful and prosperous when the pixels are of the same color. For each of the three colors you know the number of pixels of this color that inhabit Flatland. Help the king and determine whether fights can bring peace and prosperity to the country and if it is possible, find the minimum number of fights needed to make the land peaceful and prosperous.
The first line contains three space-separated integers *a*, *b* and *c* (0<=≀<=*a*,<=*b*,<=*c*<=≀<=231;Β *a*<=+<=*b*<=+<=*c*<=&gt;<=0) β€” the number of red, green and blue pixels, correspondingly.
Print a single number β€” the minimum number of pixel fights before the country becomes peaceful and prosperous. If making the country peaceful and prosperous is impossible, print -1.
[ "1 1 1\n", "3 1 0\n" ]
[ "1\n", "3\n" ]
In the first test sample the country needs only one fight to achieve peace and prosperity. Besides, it can be any fight whatsoever. For example, let's assume that the green and the blue pixels fight, then the surviving pixel will be red. As a result, after the fight there are two red pixels. There won't be other pixels. In the second sample the following sequence of fights is possible: red and blue, green and red, red and blue. As a result, after all fights there is one green pixel left.
[ { "input": "1 1 1", "output": "1" }, { "input": "3 1 0", "output": "3" }, { "input": "1 4 4", "output": "4" }, { "input": "5 10 6", "output": "10" }, { "input": "6 8 10", "output": "8" }, { "input": "1 10 2", "output": "10" }, { "input": "10 6 8", "output": "8" }, { "input": "18 67 5", "output": "67" }, { "input": "67 81 1", "output": "67" }, { "input": "51 10 91", "output": "91" }, { "input": "48 6 7", "output": "48" }, { "input": "8 97 83", "output": "97" }, { "input": "2 7 95", "output": "95" }, { "input": "772486757 1747374885 377299255", "output": "772486757" }, { "input": "1358352906 27037371 1947040615", "output": "1947040615" }, { "input": "1944219055 454183506 1369298327", "output": "1944219055" }, { "input": "382601556 881329640 791556039", "output": "881329640" }, { "input": "246543403 71853598 1504509195", "output": "1504509195" }, { "input": "50606342 2 1134945035", "output": "50606342" }, { "input": "9 530792195 6", "output": "530792195" }, { "input": "1016450951 2 9", "output": "1016450951" }, { "input": "3 10 1007169359", "output": "1007169359" }, { "input": "0 1 0", "output": "0" }, { "input": "1 0 0", "output": "0" }, { "input": "0 0 1", "output": "0" }, { "input": "3 2 0", "output": "2" }, { "input": "0 3 2", "output": "2" }, { "input": "3 0 2", "output": "2" }, { "input": "10 10 0", "output": "10" }, { "input": "0 0 10", "output": "0" }, { "input": "2 2 0", "output": "2" }, { "input": "0 2 10", "output": "2" }, { "input": "5 0 5", "output": "5" }, { "input": "5 9 0", "output": "9" }, { "input": "2147483648 2147483648 2147483648", "output": "2147483648" }, { "input": "2147483648 2147483647 2147483648", "output": "2147483648" }, { "input": "2147483648 0 0", "output": "0" }, { "input": "2147483648 2147483648 0", "output": "2147483648" }, { "input": "2147483648 0 2147483647", "output": "2147483648" }, { "input": "2147483630 2147483642 2147483610", "output": "2147483630" }, { "input": "1 4 3", "output": "3" }, { "input": "1 2 3", "output": "3" }, { "input": "1 0 1", "output": "1" }, { "input": "92134834 23742837 92374737", "output": "92374737" }, { "input": "92134834 23742837 92374738", "output": "92374738" }, { "input": "92134834 23742837 92374739", "output": "92374739" }, { "input": "9214834 2742837 9234739", "output": "9234739" }, { "input": "914835 2742837 9234739", "output": "2742837" }, { "input": "1 2 2147483648", "output": "2147483648" }, { "input": "0 0 58", "output": "0" } ]
248
0
3
68,597
715
Digit Tree
[ "dfs and similar", "divide and conquer", "dsu", "trees" ]
null
null
ZS the Coder has a large tree. It can be represented as an undirected connected graph of *n* vertices numbered from 0 to *n*<=-<=1 and *n*<=-<=1 edges between them. There is a single nonzero digit written on each edge. One day, ZS the Coder was bored and decided to investigate some properties of the tree. He chose a positive integer *M*, which is coprime to 10, i.e. . ZS consider an ordered pair of distinct vertices (*u*,<=*v*) interesting when if he would follow the shortest path from vertex *u* to vertex *v* and write down all the digits he encounters on his path in the same order, he will get a decimal representaion of an integer divisible by *M*. Formally, ZS consider an ordered pair of distinct vertices (*u*,<=*v*) interesting if the following states true: - Let *a*1<==<=*u*,<=*a*2,<=...,<=*a**k*<==<=*v* be the sequence of vertices on the shortest path from *u* to *v* in the order of encountering them; - Let *d**i* (1<=≀<=*i*<=&lt;<=*k*) be the digit written on the edge between vertices *a**i* and *a**i*<=+<=1; - The integer is divisible by *M*. Help ZS the Coder find the number of interesting pairs!
The first line of the input contains two integers, *n* and *M* (2<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*M*<=≀<=109, )Β β€” the number of vertices and the number ZS has chosen respectively. The next *n*<=-<=1 lines contain three integers each. *i*-th of them contains *u**i*,<=*v**i* and *w**i*, denoting an edge between vertices *u**i* and *v**i* with digit *w**i* written on it (0<=≀<=*u**i*,<=*v**i*<=&lt;<=*n*,<=<=1<=≀<=*w**i*<=≀<=9).
Print a single integerΒ β€” the number of interesting (by ZS the Coder's consideration) pairs.
[ "6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7\n", "5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3\n" ]
[ "7\n", "8\n" ]
In the first sample case, the interesting pairs are (0, 4), (1, 2), (1, 5), (3, 2), (2, 5), (5, 2), (3, 5). The numbers that are formed by these pairs are 14, 21, 217, 91, 7, 7, 917 respectively, which are all multiples of 7. Note that (2, 5) and (5, 2) are considered different. In the second sample case, the interesting pairs are (4, 0), (0, 4), (3, 2), (2, 3), (0, 1), (1, 0), (4, 1), (1, 4), and 6 of these pairs give the number 33 while 2 of them give the number 3333, which are all multiples of 11.
[ { "input": "6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7", "output": "7" }, { "input": "5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3", "output": "8" }, { "input": "4 3\n0 1 4\n1 2 4\n2 3 4", "output": "2" }, { "input": "2 7\n1 0 9", "output": "0" }, { "input": "2 7\n1 0 7", "output": "2" }, { "input": "10 999999937\n1 0 9\n2 1 9\n3 2 9\n4 3 9\n5 4 9\n6 5 9\n7 6 9\n8 7 3\n9 8 7", "output": "1" }, { "input": "7 97\n0 1 9\n0 2 2\n1 3 8\n1 4 5\n2 5 7\n2 6 9", "output": "1" }, { "input": "7 3\n0 1 9\n0 2 2\n1 3 8\n1 4 5\n2 5 7\n2 6 9", "output": "8" }, { "input": "2 1\n0 1 1", "output": "2" }, { "input": "10 999999999\n1 0 9\n2 1 9\n3 2 9\n4 3 9\n5 4 9\n6 5 9\n7 6 9\n8 7 9\n9 8 9", "output": "2" }, { "input": "7 1\n0 1 9\n0 2 2\n1 3 8\n1 4 5\n2 5 7\n2 6 9", "output": "42" } ]
0
0
-1
68,701
358
Dima and Hares
[ "dp", "greedy" ]
null
null
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more. Dima 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? Inna 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. Help Inna maximize the total joy the hares radiate. :)
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=3000) β€” 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<=≀<=*a**i*,<=*b**i*,<=*c**i*<=≀<=105. Number *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 *с**i* in the third line shows the joy that hare number *i* radiates if both his adjacent hares are full.
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
[ "4\n1 2 3 4\n4 3 2 1\n0 1 1 0\n", "7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3\n", "3\n1 1 1\n1 2 1\n1 1 1\n" ]
[ "13\n", "44\n", "4\n" ]
none
[ { "input": "4\n1 2 3 4\n4 3 2 1\n0 1 1 0", "output": "13" }, { "input": "7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3", "output": "44" }, { "input": "3\n1 1 1\n1 2 1\n1 1 1", "output": "4" }, { "input": "7\n1 3 8 9 3 4 4\n6 0 6 6 1 8 4\n9 6 3 7 8 8 2", "output": "42" }, { "input": "2\n3 5\n9 8\n4 0", "output": "14" }, { "input": "7\n3 6 1 5 4 2 0\n9 7 3 7 2 6 0\n1 6 5 7 5 4 1", "output": "37" }, { "input": "1\n0\n1\n4", "output": "0" }, { "input": "1\n7\n1\n7", "output": "7" }, { "input": "8\n7 3 3 5 9 9 8 1\n8 2 6 6 0 3 8 0\n1 2 5 0 9 4 7 8", "output": "49" }, { "input": "6\n1 2 0 1 6 4\n0 6 1 8 9 8\n4 1 4 3 9 8", "output": "33" }, { "input": "1\n0\n0\n0", "output": "0" }, { "input": "1\n100000\n100000\n100000", "output": "100000" } ]
170
2,252,800
3
68,756
250
Mad Joe
[ "brute force" ]
null
null
Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path. Joe's house has *n* floors, each floor is a segment of *m* cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right. Now Joe is on the *n*-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right. Joe moves by a particular algorithm. Every second he makes one of the following actions: - If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved. - Otherwise consider the next cell in the current direction of the gaze. If the cell is empty, then Joe moves into it, the gaze direction is preserved. - If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite. - If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits). Joe calms down as soon as he reaches any cell of the first floor. The figure below shows an example Joe's movements around the house. Determine how many seconds Joe will need to calm down.
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=100, 1<=≀<=*m*<=≀<=104). Next *n* lines contain the description of Joe's house. The *i*-th of these lines contains the description of the (*n*<=-<=*i*<=+<=1)-th floor of the house β€” a line that consists of *m* characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall. It is guaranteed that the first cell of the *n*-th floor is empty.
Print a single number β€” the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "3 5\n..+.#\n#+..+\n+.#+.\n", "4 10\n...+.##+.+\n+#++..+++#\n++.#++++..\n.+##.++#.+\n", "2 2\n..\n++\n" ]
[ "14", "42\n", "Never" ]
none
[ { "input": "3 5\n..+.#\n#+..+\n+.#+.", "output": "14" }, { "input": "4 10\n...+.##+.+\n+#++..+++#\n++.#++++..\n.+##.++#.+", "output": "42" }, { "input": "2 2\n..\n++", "output": "Never" }, { "input": "5 1\n.\n.\n.\n.\n.", "output": "4" }, { "input": "20 20\n..+#+.+++.+++#+..#++\n..####+++#..++#+.+.+\n+.+..+++..#.++++++++\n+##++..+.##..#+++.++\n++.+.+.+.++++.+++.++\n.+++++.+#+++++...+#+\n.+++#+++++++.+.++.++\n...+.++++++.++#...++\n+++.+++.+....#....+.\n.++++++.+.+..++.++##\n++++++..+.#++..+..+.\n+..#+++++..+##+#++.+\n+.+#+#....+.#+++#+.+\n++.+.+++.++.+.#..#..\n+.+..+++.+.+.++.++++\n..#+++.++.++.#+.+++.\n++++.#.+.+#..+++.+.+\n+..+.+...+....+.....\n#.###++++.+.++.+.+++\n++..+.+.++.+..+.++++", "output": "155" }, { "input": "4 100\n.++++.+++++..+++.++++.+++++++++++.++++++++.+++.++++.+++++.+.+++.+++++++++++.+++++++++.+.++++++++++++\n++++++++.++++++.++.++++++++.++.++...+++++++++++++++++++++++++.+++++++.++++++++++++++++++++++++++++.+\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++..++.++++.++++++++.+++++++++++.+++++++++.\n++++++++.+++++++++++.+.+.+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++", "output": "4930" }, { "input": "100 1\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.", "output": "99" }, { "input": "100 3\n.++\n+.+\n++.\n+.+\n.++\n+.+\n+.+\n++.\n.++\n++.\n++.\n+.+\n+.+\n.++\n.++\n+.+\n+.+\n.++\n.++\n.++\n.++\n+.+\n++.\n++.\n+.+\n.++\n.++\n+.+\n+.+\n+.+\n++.\n++.\n++.\n+.+\n.++\n++.\n.++\n++.\n++.\n+.+\n++.\n.++\n+.+\n+.+\n+.+\n++.\n+.+\n.++\n.++\n++.\n++.\n++.\n++.\n+.+\n.++\n.++\n+.+\n++.\n+.+\n+.+\n++.\n+.+\n++.\n+.+\n+.+\n++.\n+.+\n+.+\n+.+\n+.+\n++.\n++.\n++.\n.++\n++.\n++.\n.++\n.++\n+.+\n++.\n+.+\n+.+\n+.+\n+.+\n++.\n++.\n++.\n+.+\n.++\n+.+\n++.\n.++\n.++\n+.+\n++.\n++.\n.++\n++.\n++.\n+.+", "output": "401" }, { "input": "10 10\n.+++++++++\n+++++++++.\n.+++++++++\n+++++++++.\n.+++++++++\n+++++++++.\n.+++++++++\n+++++++++.\n.+++++++++\n+++++++++.", "output": "908" }, { "input": "10 10\n.++++++++.\n#########.\n.++++++++.\n.#########\n.++++++++.\n#########.\n.++++++++.\n.#########\n.++++++++.\n#########.", "output": "418" } ]
46
0
0
68,770
550
Regular Bridge
[ "constructive algorithms", "graphs", "implementation" ]
null
null
An undirected graph is called *k*-regular, if the degrees of all its vertices are equal *k*. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components. Build a connected undirected *k*-regular graph containing at least one bridge, or else state that such graph doesn't exist.
The single line of the input contains integer *k* (1<=≀<=*k*<=≀<=100) β€” the required degree of the vertices of the regular graph.
Print "NO" (without quotes), if such graph doesn't exist. Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines. The description of the made graph must start with numbers *n* and *m* β€” the number of vertices and edges respectively. Each of the next *m* lines must contain two integers, *a* and *b* (1<=≀<=*a*,<=*b*<=≀<=*n*, *a*<=β‰ <=*b*), that mean that there is an edge connecting the vertices *a* and *b*. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equal *k*. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order. The constructed graph must contain at most 106 vertices and 106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most 106 vertices and at most 106 edges).
[ "1\n" ]
[ "YES\n2 1\n1 2\n" ]
In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge.
[ { "input": "1", "output": "YES\n2 1\n1 2" }, { "input": "3", "output": "YES\n10 15\n1 6\n1 2\n1 3\n2 4\n2 5\n3 4\n3 5\n4 5\n6 7\n6 8\n7 9\n7 10\n8 9\n8 10\n9 10" }, { "input": "11", "output": "YES\n26 143\n1 14\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n2 12\n2 13\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n3 12\n3 13\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n4 12\n4 13\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n5 12\n5 13\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n6 12\n6 13\n6 8\n6 9\n6 10\n6 11\n7 12\n7 13\n7 8\n7 9\n7 10\n7 11\n8 12\n8 13\n8 10\n8 11\n9 12\n9 13\n9 10\n9 11\n10 12\n10 13\n11 12\n11 13\n12 13\n14 15\n14 16\n14 17\n14 18\n14 19\n14 20\n14 21\n14 22\n14 23\n14 24\n15 25\n15 26\n15 17\n15..." }, { "input": "10", "output": "NO" }, { "input": "2", "output": "NO" }, { "input": "4", "output": "NO" }, { "input": "5", "output": "YES\n14 35\n1 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 4\n2 5\n3 6\n3 7\n3 4\n3 5\n4 6\n4 7\n5 6\n5 7\n6 7\n8 9\n8 10\n8 11\n8 12\n9 13\n9 14\n9 11\n9 12\n10 13\n10 14\n10 11\n10 12\n11 13\n11 14\n12 13\n12 14\n13 14" }, { "input": "6", "output": "NO" }, { "input": "7", "output": "YES\n18 63\n1 10\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n2 8\n2 9\n2 4\n2 5\n2 6\n2 7\n3 8\n3 9\n3 4\n3 5\n3 6\n3 7\n4 8\n4 9\n4 6\n4 7\n5 8\n5 9\n5 6\n5 7\n6 8\n6 9\n7 8\n7 9\n8 9\n10 11\n10 12\n10 13\n10 14\n10 15\n10 16\n11 17\n11 18\n11 13\n11 14\n11 15\n11 16\n12 17\n12 18\n12 13\n12 14\n12 15\n12 16\n13 17\n13 18\n13 15\n13 16\n14 17\n14 18\n14 15\n14 16\n15 17\n15 18\n16 17\n16 18\n17 18" }, { "input": "8", "output": "NO" }, { "input": "9", "output": "YES\n22 99\n1 12\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n2 10\n2 11\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n3 10\n3 11\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n4 10\n4 11\n4 6\n4 7\n4 8\n4 9\n5 10\n5 11\n5 6\n5 7\n5 8\n5 9\n6 10\n6 11\n6 8\n6 9\n7 10\n7 11\n7 8\n7 9\n8 10\n8 11\n9 10\n9 11\n10 11\n12 13\n12 14\n12 15\n12 16\n12 17\n12 18\n12 19\n12 20\n13 21\n13 22\n13 15\n13 16\n13 17\n13 18\n13 19\n13 20\n14 21\n14 22\n14 15\n14 16\n14 17\n14 18\n14 19\n14 20\n15 21\n15 22\n15 17\n15 18\n15 19\n15 20\n16 21\n16 22\n16 17\n..." }, { "input": "12", "output": "NO" }, { "input": "13", "output": "YES\n30 195\n1 16\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n2 14\n2 15\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n3 14\n3 15\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n4 14\n4 15\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n4 13\n5 14\n5 15\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n5 13\n6 14\n6 15\n6 8\n6 9\n6 10\n6 11\n6 12\n6 13\n7 14\n7 15\n7 8\n7 9\n7 10\n7 11\n7 12\n7 13\n8 14\n8 15\n8 10\n8 11\n8 12\n8 13\n9 14\n9 15\n9 10\n9 11\n9 12\n9 13\n10 14\n10 15\n10 12\n..." }, { "input": "14", "output": "NO" }, { "input": "15", "output": "YES\n34 255\n1 18\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n2 16\n2 17\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n3 16\n3 17\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n4 16\n4 17\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n5 16\n5 17\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n5 13\n5 14\n5 15\n6 16\n6 17\n6 8\n6 9\n6 10\n6 11\n6 12\n6 13\n6 14\n6 15\n7 16\n7 17\n7 8\n7 9\n7 10\n7 11\n7 12\n7 13\n7 14\n7 15\n8 16\n8 ..." }, { "input": "16", "output": "NO" }, { "input": "17", "output": "YES\n38 323\n1 20\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n2 18\n2 19\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n3 18\n3 19\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n4 18\n4 19\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n4 16\n4 17\n5 18\n5 19\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n5 13\n5 14\n5 15\n5 16\n5 17\n6 18\n6 19\n6 8\n6 9\n6 10\n6 11\n6 12\n6 13\n6 14\n6 15\n6 16\n..." }, { "input": "18", "output": "NO" }, { "input": "19", "output": "YES\n42 399\n1 22\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n2 20\n2 21\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n3 20\n3 21\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n4 20\n4 21\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n4 16\n4 17\n4 18\n4 19\n5 20\n5 21\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n5 13\n5 14\n5 15\n5 16\n5 17\n5 18\n5 19\n6 20..." }, { "input": "20", "output": "NO" }, { "input": "21", "output": "YES\n46 483\n1 24\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n2 22\n2 23\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n3 22\n3 23\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n4 22\n4 23\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n4 16\n4 17\n4 18\n4 19\n4 20\n4 21\n5 22\n5 23\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12..." }, { "input": "22", "output": "NO" }, { "input": "23", "output": "YES\n50 575\n1 26\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n2 24\n2 25\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n3 24\n3 25\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3 23\n4 24\n4 25\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n4 16\n4 17\n4 18\n4 19\n4 20\n4 21\n4 22\n4 23\n..." }, { "input": "24", "output": "NO" }, { "input": "25", "output": "YES\n54 675\n1 28\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n2 26\n2 27\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n3 26\n3 27\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3 23\n3 24\n3 25\n4 26\n4 27\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n4 13\n4 14\n4 15\n4 16\n4 17\n..." }, { "input": "26", "output": "NO" }, { "input": "27", "output": "YES\n58 783\n1 30\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n2 28\n2 29\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n3 28\n3 29\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3 23\n3 24\n3 25\n3 26\n3 27\n4 28\n4 29\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n..." }, { "input": "28", "output": "NO" }, { "input": "29", "output": "YES\n62 899\n1 32\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n2 30\n2 31\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n3 30\n3 31\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3 23\n3 24\n3 25\n3 26\n3 27\n3 28\n3 29\n4 30\n4 ..." }, { "input": "30", "output": "NO" }, { "input": "31", "output": "YES\n66 1023\n1 34\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n2 32\n2 33\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n3 32\n3 33\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3 23\n3 24\n3 25\n3 26\n3..." }, { "input": "32", "output": "NO" }, { "input": "33", "output": "YES\n70 1155\n1 36\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n2 34\n2 35\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n3 34\n3 35\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3 19\n3 20\n3 21\n3 22\n3..." }, { "input": "34", "output": "NO" }, { "input": "35", "output": "YES\n74 1295\n1 38\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n2 36\n2 37\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n3 36\n3 37\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n3 18\n3..." }, { "input": "36", "output": "NO" }, { "input": "37", "output": "YES\n78 1443\n1 40\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n2 38\n2 39\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n3 38\n3 39\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3..." }, { "input": "38", "output": "NO" }, { "input": "39", "output": "YES\n82 1599\n1 42\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n2 40\n2 41\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n3 40\n3 41\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3..." }, { "input": "40", "output": "NO" }, { "input": "41", "output": "YES\n86 1763\n1 44\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n2 42\n2 43\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n3 42\n3 43\n3 4\n3 5\n3 6..." }, { "input": "42", "output": "NO" }, { "input": "43", "output": "YES\n90 1935\n1 46\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n2 44\n2 45\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n3..." }, { "input": "44", "output": "NO" }, { "input": "45", "output": "YES\n94 2115\n1 48\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n2 46\n2 47\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2..." }, { "input": "46", "output": "NO" }, { "input": "47", "output": "YES\n98 2303\n1 50\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n2 48\n2 49\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2..." }, { "input": "48", "output": "NO" }, { "input": "49", "output": "YES\n102 2499\n1 52\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n2 50\n2 51\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n..." }, { "input": "50", "output": "NO" }, { "input": "51", "output": "YES\n106 2703\n1 54\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n2 52\n2 53\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n..." }, { "input": "52", "output": "NO" }, { "input": "53", "output": "YES\n110 2915\n1 56\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n2 54\n2 55\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n..." }, { "input": "54", "output": "NO" }, { "input": "55", "output": "YES\n114 3135\n1 58\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n2 56\n2 57\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n..." }, { "input": "56", "output": "NO" }, { "input": "57", "output": "YES\n118 3363\n1 60\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n2 58\n2 59\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n..." }, { "input": "58", "output": "NO" }, { "input": "59", "output": "YES\n122 3599\n1 62\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n2 60\n2 61\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n..." }, { "input": "60", "output": "NO" }, { "input": "61", "output": "YES\n126 3843\n1 64\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n2 62\n2 63\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n..." }, { "input": "62", "output": "NO" }, { "input": "63", "output": "YES\n130 4095\n1 66\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n2 64\n2 65\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n..." }, { "input": "64", "output": "NO" }, { "input": "65", "output": "YES\n134 4355\n1 68\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n2 66\n2 67\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n..." }, { "input": "66", "output": "NO" }, { "input": "67", "output": "YES\n138 4623\n1 70\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n2 68\n2 69\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n..." }, { "input": "68", "output": "NO" }, { "input": "69", "output": "YES\n142 4899\n1 72\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n2 70\n2 71\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n..." }, { "input": "70", "output": "NO" }, { "input": "71", "output": "YES\n146 5183\n1 74\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n2 72\n2 73\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n..." }, { "input": "72", "output": "NO" }, { "input": "73", "output": "YES\n150 5475\n1 76\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n2 74\n2 75\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n..." }, { "input": "74", "output": "NO" }, { "input": "75", "output": "YES\n154 5775\n1 78\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n2 76\n2 77\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n..." }, { "input": "76", "output": "NO" }, { "input": "77", "output": "YES\n158 6083\n1 80\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n2 78\n2 79\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n..." }, { "input": "78", "output": "NO" }, { "input": "79", "output": "YES\n162 6399\n1 82\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n2 80\n2 81\n2 4\n2 5\n2 6\n2 7..." }, { "input": "80", "output": "NO" }, { "input": "81", "output": "YES\n166 6723\n1 84\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n2 82\n2 83\n2 4\n2..." }, { "input": "82", "output": "NO" }, { "input": "83", "output": "YES\n170 7055\n1 86\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n2 84\n..." }, { "input": "84", "output": "NO" }, { "input": "85", "output": "YES\n174 7395\n1 88\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n..." }, { "input": "86", "output": "NO" }, { "input": "87", "output": "YES\n178 7743\n1 90\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n..." }, { "input": "88", "output": "NO" }, { "input": "89", "output": "YES\n182 8099\n1 92\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n..." }, { "input": "90", "output": "NO" }, { "input": "91", "output": "YES\n186 8463\n1 94\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n..." }, { "input": "92", "output": "NO" }, { "input": "93", "output": "YES\n190 8835\n1 96\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n..." }, { "input": "94", "output": "NO" }, { "input": "95", "output": "YES\n194 9215\n1 98\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n..." }, { "input": "96", "output": "NO" }, { "input": "97", "output": "YES\n198 9603\n1 100\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n..." }, { "input": "98", "output": "NO" }, { "input": "99", "output": "YES\n202 9999\n1 102\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n..." }, { "input": "100", "output": "NO" } ]
62
102,400
3
68,832
158
Ice Sculptures
[ "*special", "brute force", "number theory" ]
null
null
The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus *n* ice sculptures were erected. The sculptures are arranged in a circle at equal distances from each other, so they form a regular *n*-gon. They are numbered in clockwise order with numbers from 1 to *n*. The site of the University has already conducted a voting that estimated each sculpture's characteristic of *t**i* β€” the degree of the sculpture's attractiveness. The values of *t**i* can be positive, negative or zero. When the university rector came to evaluate the work, he said that this might be not the perfect arrangement. He suggested to melt some of the sculptures so that: - the remaining sculptures form a regular polygon (the number of vertices should be between 3 and *n*), - the sum of the *t**i* values of the remaining sculptures is maximized. Help the Vice Rector to analyze the criticism β€” find the maximum value of *t**i* sum which can be obtained in this way. It is allowed not to melt any sculptures at all. The sculptures can not be moved.
The first input line contains an integer *n* (3<=≀<=*n*<=≀<=20000) β€” the initial number of sculptures. The second line contains a sequence of integers *t*1,<=*t*2,<=...,<=*t**n*, *t**i* β€” the degree of the *i*-th sculpture's attractiveness (<=-<=1000<=≀<=*t**i*<=≀<=1000). The numbers on the line are separated by spaces.
Print the required maximum sum of the sculptures' attractiveness.
[ "8\n1 2 -3 4 -5 5 2 3\n", "6\n1 -2 3 -4 5 -6\n", "6\n1 2 3 4 5 6\n" ]
[ "14\n", "9\n", "21\n" ]
In the first sample it is best to leave every second sculpture, that is, leave sculptures with attractivenesses: 2, 4, 5 ΠΈ 3.
[ { "input": "8\n1 2 -3 4 -5 5 2 3", "output": "14" }, { "input": "6\n1 -2 3 -4 5 -6", "output": "9" }, { "input": "6\n1 2 3 4 5 6", "output": "21" }, { "input": "4\n1 -10 1 -10", "output": "-18" }, { "input": "6\n1 0 1 0 1 0", "output": "3" }, { "input": "12\n1 1 1 1 1 1 1 -1000 1 1 1 1", "output": "6" }, { "input": "3\n-1 -1 -1", "output": "-3" }, { "input": "97\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "0" }, { "input": "5\n0 -1 1 1 0", "output": "1" }, { "input": "6\n-8 -8 6 5 -5 1", "output": "-2" }, { "input": "7\n0 55 -5 50 53 34 84", "output": "271" }, { "input": "9\n-2 -1 2 0 1 1 -1 -2 -2", "output": "1" }, { "input": "10\n5 -5 -4 3 -1 -1 2 1 5 -2", "output": "7" }, { "input": "13\n6 7 8 2 5 1 -9 -6 9 10 8 9 -8", "output": "42" } ]
216
4,403,200
3
68,893
757
Can Bash Save the Day?
[ "data structures", "divide and conquer", "graphs", "trees" ]
null
null
Whoa! You did a great job helping Team Rocket who managed to capture all the Pokemons sent by Bash. Meowth, part of Team Rocket, having already mastered the human language, now wants to become a master in programming as well. He agrees to free the Pokemons if Bash can answer his questions. Initially, Meowth gives Bash a weighted tree containing *n* nodes and a sequence *a*1,<=*a*2...,<=*a**n* which is a permutation of 1,<=2,<=...,<=*n*. Now, Mewoth makes *q* queries of one of the following forms: - 1 l r v: meaning Bash should report , where *dist*(*a*,<=*b*) is the length of the shortest path from node *a* to node *b* in the given tree. - 2 x: meaning Bash should swap *a**x* and *a**x*<=+<=1 in the given sequence. This new sequence is used for later queries. Help Bash to answer the questions!
The first line contains two integers *n* and *q* (1<=≀<=*n*<=≀<=2Β·105, 1<=≀<=*q*<=≀<=2Β·105)Β β€” the number of nodes in the tree and the number of queries, respectively. The next line contains *n* space-separated integersΒ β€” the sequence *a*1,<=*a*2,<=...,<=*a**n* which is a permutation of 1,<=2,<=...,<=*n*. Each of the next *n*<=-<=1 lines contain three space-separated integers *u*, *v*, and *w* denoting that there exists an undirected edge between node *u* and node *v* of weight *w*, (1<=≀<=*u*,<=*v*<=≀<=*n*, *u*<=β‰ <=*v*, 1<=≀<=*w*<=≀<=106). It is guaranteed that the given graph is a tree. Each query consists of two lines. First line contains single integer *t*, indicating the type of the query. Next line contains the description of the query: - t = 1: Second line contains three integers *a*, *b* and *c* (1<=≀<=*a*,<=*b*,<=*c*<=&lt;<=230) using which *l*, *r* and *v* can be generated using the formula given below: , - , - . - . The *ans**i* is the answer for the *i*-th query, assume that *ans*0<==<=0. If the *i*-th query is of type 2 then *ans**i* = *ans**i*<=-<=1. It is guaranteed that: - for each query of type 1: 1<=≀<=*l*<=≀<=*r*<=≀<=*n*, 1<=≀<=*v*<=≀<=*n*, - for each query of type 2: 1<=≀<=*x*<=≀<=*n*<=-<=1. The operation means bitwise exclusive OR.
For each query of type 1, output a single integer in a separate line, denoting the answer to the query.
[ "5 5\n4 5 1 3 2\n4 2 4\n1 3 9\n4 1 4\n4 5 2\n1\n1 5 4\n1\n22 20 20\n2\n38\n2\n39\n1\n36 38 38\n" ]
[ "23\n37\n28\n" ]
In the sample, the actual queries are the following: - 1 1 5 4 - 1 1 3 3 - 2 3 - 2 2 - 1 1 3 3
[]
46
0
0
69,018
604
Uncowed Forces
[ "implementation" ]
null
null
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. Codeforces 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 . 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. All arithmetic operations are performed with absolute precision and no rounding. It is guaranteed that Kevin's final score is an integer.
The first line of the input contains five space-separated integers *m*1, *m*2, *m*3, *m*4, *m*5, where *m**i* (0<=≀<=*m**i*<=≀<=119) is the time of Kevin's last submission for problem *i*. His last submission is always correct and gets accepted. The second line contains five space-separated integers *w*1, *w*2, *w*3, *w*4, *w*5, where *w**i* (0<=≀<=*w**i*<=≀<=10) is Kevin's number of wrong submissions on problem *i*. The last line contains two space-separated integers *h**s* and *h**u* (0<=≀<=*h**s*,<=*h**u*<=≀<=20), denoting the Kevin's numbers of successful and unsuccessful hacks, respectively.
Print a single integer, the value of Kevin's final score.
[ "20 40 60 80 100\n0 1 2 3 4\n1 0\n", "119 119 119 119 119\n0 0 0 0 0\n10 0\n" ]
[ "4900\n", "4930\n" ]
In the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/42158dc2bc78cd21fa679530ae9ef8b9ea298d15.png" style="max-width: 100.0%;max-height: 100.0%;"/> of the points on each problem. So his score from solving problems is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fdf392d8508500b57f8057ac0c4c892ab5f925a2.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Adding in 10Β·100 = 1000 points from hacks, his total score becomes 3930 + 1000 = 4930.
[ { "input": "20 40 60 80 100\n0 1 2 3 4\n1 0", "output": "4900" }, { "input": "119 119 119 119 119\n0 0 0 0 0\n10 0", "output": "4930" }, { "input": "3 6 13 38 60\n6 10 10 3 8\n9 9", "output": "5088" }, { "input": "21 44 11 68 75\n6 2 4 8 4\n2 8", "output": "4522" }, { "input": "16 112 50 114 68\n1 4 8 4 9\n19 11", "output": "5178" }, { "input": "55 66 75 44 47\n6 0 6 6 10\n19 0", "output": "6414" }, { "input": "47 11 88 5 110\n6 10 4 2 3\n10 6", "output": "5188" }, { "input": "5 44 61 103 92\n9 0 10 4 8\n15 7", "output": "4914" }, { "input": "115 53 96 62 110\n7 8 1 7 9\n7 16", "output": "3416" }, { "input": "102 83 26 6 11\n3 4 1 8 3\n17 14", "output": "6704" }, { "input": "36 102 73 101 19\n5 9 2 2 6\n4 13", "output": "4292" }, { "input": "40 115 93 107 113\n5 7 2 6 8\n6 17", "output": "2876" }, { "input": "53 34 53 107 81\n4 3 1 10 8\n7 7", "output": "4324" }, { "input": "113 37 4 84 66\n2 0 10 3 0\n20 19", "output": "6070" }, { "input": "10 53 101 62 1\n8 0 9 7 9\n0 11", "output": "4032" }, { "input": "45 45 75 36 76\n6 2 2 0 0\n8 17", "output": "5222" }, { "input": "47 16 44 78 111\n7 9 8 0 2\n1 19", "output": "3288" }, { "input": "7 54 39 102 31\n6 0 2 10 1\n18 3", "output": "6610" }, { "input": "0 46 86 72 40\n1 5 5 5 9\n6 5", "output": "4924" }, { "input": "114 4 45 78 113\n0 4 8 10 2\n10 12", "output": "4432" }, { "input": "56 56 96 105 107\n4 9 10 4 8\n2 1", "output": "3104" }, { "input": "113 107 59 50 56\n3 7 10 6 3\n10 12", "output": "4586" }, { "input": "96 104 9 94 84\n6 10 7 8 3\n14 11", "output": "4754" }, { "input": "98 15 116 43 55\n4 3 0 9 3\n10 7", "output": "5400" }, { "input": "0 26 99 108 35\n0 4 3 0 10\n9 5", "output": "5388" }, { "input": "89 24 51 49 84\n5 6 2 2 9\n2 14", "output": "4066" }, { "input": "57 51 76 45 96\n1 0 4 3 6\n12 15", "output": "5156" }, { "input": "79 112 37 36 116\n2 8 4 7 5\n4 12", "output": "3872" }, { "input": "71 42 60 20 7\n7 1 1 10 6\n1 7", "output": "5242" }, { "input": "86 10 66 80 55\n0 2 5 10 5\n15 6", "output": "5802" }, { "input": "66 109 22 22 62\n3 1 5 4 5\n10 5", "output": "5854" }, { "input": "97 17 43 84 58\n2 8 3 8 6\n10 7", "output": "5028" }, { "input": "109 83 5 114 104\n6 0 3 9 5\n5 2", "output": "4386" }, { "input": "94 18 24 91 105\n2 0 7 10 3\n1 4", "output": "4118" }, { "input": "64 17 86 59 45\n8 0 10 2 2\n4 4", "output": "5144" }, { "input": "70 84 31 57 2\n7 0 0 2 7\n12 5", "output": "6652" }, { "input": "98 118 117 86 4\n2 10 9 7 5\n11 15", "output": "4476" }, { "input": "103 110 101 97 70\n4 2 1 0 5\n7 5", "output": "4678" }, { "input": "78 96 6 97 62\n7 7 9 2 9\n10 3", "output": "4868" }, { "input": "95 28 3 31 115\n1 9 0 7 3\n10 13", "output": "5132" }, { "input": "45 17 116 58 3\n8 8 7 6 4\n3 19", "output": "3992" }, { "input": "19 12 0 113 77\n3 0 10 9 2\n8 6", "output": "5040" }, { "input": "0 0 0 0 0\n0 0 0 0 0\n0 0", "output": "7500" }, { "input": "0 0 0 0 0\n0 0 0 0 0\n20 0", "output": "9500" }, { "input": "119 119 119 119 119\n10 10 10 10 10\n0 20", "output": "1310" }, { "input": "0 0 0 0 0\n10 10 10 10 10\n0 20", "output": "4150" }, { "input": "119 0 0 0 0\n10 0 0 0 0\n5 5", "output": "7400" }, { "input": "0 119 0 0 0\n0 10 0 0 0\n5 5", "output": "7050" }, { "input": "0 0 119 0 0\n0 0 10 0 0\n0 0", "output": "6450" }, { "input": "0 0 0 119 0\n0 0 0 10 0\n5 5", "output": "6350" }, { "input": "0 0 0 0 119\n0 0 0 0 10\n5 5", "output": "6060" }, { "input": "119 0 0 0 0\n2 0 0 0 0\n5 5", "output": "7412" }, { "input": "0 119 0 0 0\n0 2 0 0 0\n5 5", "output": "7174" }, { "input": "0 0 119 0 0\n0 0 2 0 0\n5 5", "output": "6936" }, { "input": "0 0 0 119 0\n0 0 0 2 0\n5 5", "output": "6698" }, { "input": "0 0 0 0 119\n0 0 0 0 2\n5 5", "output": "6460" }, { "input": "119 0 0 0 0\n0 0 0 0 0\n4 9", "output": "7212" } ]
77
6,963,200
3
0
281
Word Capitalization
[ "implementation", "strings" ]
null
null
Capitalization is writing a word with its first letter as a capital letter. Your task is to capitalize the given word. Note, that during capitalization all the letters except the first one remains unchanged.
A single line contains a non-empty word. This word consists of lowercase and uppercase English letters. The length of the word will not exceed 103.
Output the given word after capitalization.
[ "ApPLe\n", "konjac\n" ]
[ "ApPLe\n", "Konjac\n" ]
none
[ { "input": "ApPLe", "output": "ApPLe" }, { "input": "konjac", "output": "Konjac" }, { "input": "a", "output": "A" }, { "input": "A", "output": "A" }, { "input": "z", "output": "Z" }, { "input": "ABACABA", "output": "ABACABA" }, { "input": "xYaPxPxHxGePfGtQySlNrLxSjDtNnTaRaEpAhPaQpWnDzMqGgRgEwJxGiBdZnMtHxFbObCaGiCeZkUqIgBhHtNvAqAlHpMnQhNeQbMyZrCdElVwHtKrPpJjIaHuIlYwHaRkAkUpPlOhNlBtXwDsKzPyHrPiUwNlXtTaPuMwTqYtJySgFoXvLiHbQwMjSvXsQfKhVlOxGdQkWjBhEyQvBjPoFkThNeRhTuIzFjInJtEfPjOlOsJpJuLgLzFnZmKvFgFrNsOnVqFcNiMfCqTpKnVyLwNqFiTySpWeTdFnWuTwDkRjVxNyQvTrOoEiExYiFaIrLoFmJfZcDkHuWjYfCeEqCvEsZiWnJaEmFbMjDvYwEeJeGcKbVbChGsIzNlExHzHiTlHcSaKxLuZxX", "output": "XYaPxPxHxGePfGtQySlNrLxSjDtNnTaRaEpAhPaQpWnDzMqGgRgEwJxGiBdZnMtHxFbObCaGiCeZkUqIgBhHtNvAqAlHpMnQhNeQbMyZrCdElVwHtKrPpJjIaHuIlYwHaRkAkUpPlOhNlBtXwDsKzPyHrPiUwNlXtTaPuMwTqYtJySgFoXvLiHbQwMjSvXsQfKhVlOxGdQkWjBhEyQvBjPoFkThNeRhTuIzFjInJtEfPjOlOsJpJuLgLzFnZmKvFgFrNsOnVqFcNiMfCqTpKnVyLwNqFiTySpWeTdFnWuTwDkRjVxNyQvTrOoEiExYiFaIrLoFmJfZcDkHuWjYfCeEqCvEsZiWnJaEmFbMjDvYwEeJeGcKbVbChGsIzNlExHzHiTlHcSaKxLuZxX" }, { "input": "rZhIcQlXpNcPgXrOjTiOlMoTgXgIhCfMwZfWoFzGhEkQlOoMjIuShPlZfWkNnMyQfYdUhVgQuSmYoElEtZpDyHtOxXgCpWbZqSbYnPqBcNqRtPgCnJnAyIvNsAhRbNeVlMwZyRyJnFgIsCnSbOdLvUyIeOzQvRpMoMoHfNhHwKvTcHuYnYySfPmAiNwAiWdZnWlLvGfBbRbRrCrBqIgIdWkWiBsNyYkKdNxZdGaToSsDnXpRaGrKxBpQsCzBdQgZzBkGeHgGxNrIyQlSzWsTmSnZwOcHqQpNcQvJlPvKaPiQaMaYsQjUeCqQdCjPgUbDmWiJmNiXgExLqOcCtSwSePnUxIuZfIfBeWbEiVbXnUsPwWyAiXyRbZgKwOqFfCtQuKxEmVeRlAkOeXkO", "output": "RZhIcQlXpNcPgXrOjTiOlMoTgXgIhCfMwZfWoFzGhEkQlOoMjIuShPlZfWkNnMyQfYdUhVgQuSmYoElEtZpDyHtOxXgCpWbZqSbYnPqBcNqRtPgCnJnAyIvNsAhRbNeVlMwZyRyJnFgIsCnSbOdLvUyIeOzQvRpMoMoHfNhHwKvTcHuYnYySfPmAiNwAiWdZnWlLvGfBbRbRrCrBqIgIdWkWiBsNyYkKdNxZdGaToSsDnXpRaGrKxBpQsCzBdQgZzBkGeHgGxNrIyQlSzWsTmSnZwOcHqQpNcQvJlPvKaPiQaMaYsQjUeCqQdCjPgUbDmWiJmNiXgExLqOcCtSwSePnUxIuZfIfBeWbEiVbXnUsPwWyAiXyRbZgKwOqFfCtQuKxEmVeRlAkOeXkO" }, { "input": "hDgZlUmLhYbLkLcNcKeOwJwTePbOvLaRvNzQbSbLsPeHqLhUqWtUbNdQfQqFfXeJqJwWuOrFnDdZiPxIkDyVmHbHvXfIlFqSgAcSyWbOlSlRuPhWdEpEzEeLnXwCtWuVcHaUeRgCiYsIvOaIgDnFuDbRnMoCmPrZfLeFpSjQaTfHgZwZvAzDuSeNwSoWuJvLqKqAuUxFaCxFfRcEjEsJpOfCtDiVrBqNsNwPuGoRgPzRpLpYnNyQxKaNnDnYiJrCrVcHlOxPiPcDbEgKfLwBjLhKcNeMgJhJmOiJvPfOaPaEuGqWvRbErKrIpDkEoQnKwJnTlStLyNsHyOjZfKoIjXwUvRrWpSyYhRpQdLqGmErAiNcGqAqIrTeTiMuPmCrEkHdBrLyCxPtYpRqD", "output": "HDgZlUmLhYbLkLcNcKeOwJwTePbOvLaRvNzQbSbLsPeHqLhUqWtUbNdQfQqFfXeJqJwWuOrFnDdZiPxIkDyVmHbHvXfIlFqSgAcSyWbOlSlRuPhWdEpEzEeLnXwCtWuVcHaUeRgCiYsIvOaIgDnFuDbRnMoCmPrZfLeFpSjQaTfHgZwZvAzDuSeNwSoWuJvLqKqAuUxFaCxFfRcEjEsJpOfCtDiVrBqNsNwPuGoRgPzRpLpYnNyQxKaNnDnYiJrCrVcHlOxPiPcDbEgKfLwBjLhKcNeMgJhJmOiJvPfOaPaEuGqWvRbErKrIpDkEoQnKwJnTlStLyNsHyOjZfKoIjXwUvRrWpSyYhRpQdLqGmErAiNcGqAqIrTeTiMuPmCrEkHdBrLyCxPtYpRqD" }, { "input": "qUdLgGrJeGmIzIeZrCjUtBpYfRvNdXdRpGsThIsEmJjTiMqEwRxBeBaSxEuWrNvExKePjPnXhPzBpWnHiDhTvZhBuIjDnZpTcEkCvRkAcTmMuXhGgErWgFyGyToOyVwYlCuQpTfJkVdWmFyBqQhJjYtXrBbFdHzDlGsFbHmHbFgXgFhIyDhZyEqEiEwNxSeByBwLiVeSnCxIdHbGjOjJrZeVkOzGeMmQrJkVyGhDtCzOlPeAzGrBlWwEnAdUfVaIjNrRyJjCnHkUvFuKuKeKbLzSbEmUcXtVkZzXzKlOrPgQiDmCcCvIyAdBwOeUuLbRmScNcWxIkOkJuIsBxTrIqXhDzLcYdVtPgZdZfAxTmUtByGiTsJkSySjXdJvEwNmSmNoWsChPdAzJrBoW", "output": "QUdLgGrJeGmIzIeZrCjUtBpYfRvNdXdRpGsThIsEmJjTiMqEwRxBeBaSxEuWrNvExKePjPnXhPzBpWnHiDhTvZhBuIjDnZpTcEkCvRkAcTmMuXhGgErWgFyGyToOyVwYlCuQpTfJkVdWmFyBqQhJjYtXrBbFdHzDlGsFbHmHbFgXgFhIyDhZyEqEiEwNxSeByBwLiVeSnCxIdHbGjOjJrZeVkOzGeMmQrJkVyGhDtCzOlPeAzGrBlWwEnAdUfVaIjNrRyJjCnHkUvFuKuKeKbLzSbEmUcXtVkZzXzKlOrPgQiDmCcCvIyAdBwOeUuLbRmScNcWxIkOkJuIsBxTrIqXhDzLcYdVtPgZdZfAxTmUtByGiTsJkSySjXdJvEwNmSmNoWsChPdAzJrBoW" }, { "input": "kHbApGoBcLmIwUlXkVgUmWzYeLoDbGaOkWbIuXoRwMfKuOoMzAoXrBoTvYxGrMbRjDuRxAbGsTnErIiHnHoLeRnTbFiRfDdOkNlWiAcOsChLdLqFqXlDpDoDtPxXqAmSvYgPvOcCpOlWtOjYwFkGkHuCaHwZcFdOfHjBmIxTeSiHkWjXyFcCtOlSuJsZkDxUgPeZkJwMmNpErUlBcGuMlJwKkWnOzFeFiSiPsEvMmQiCsYeHlLuHoMgBjFoZkXlObDkSoQcVyReTmRsFzRhTuIvCeBqVsQdQyTyZjStGrTyDcEcAgTgMiIcVkLbZbGvWeHtXwEqWkXfTcPyHhHjYwIeVxLyVmHmMkUsGiHmNnQuMsXaFyPpVqNrBhOiWmNkBbQuHvQdOjPjKiZcL", "output": "KHbApGoBcLmIwUlXkVgUmWzYeLoDbGaOkWbIuXoRwMfKuOoMzAoXrBoTvYxGrMbRjDuRxAbGsTnErIiHnHoLeRnTbFiRfDdOkNlWiAcOsChLdLqFqXlDpDoDtPxXqAmSvYgPvOcCpOlWtOjYwFkGkHuCaHwZcFdOfHjBmIxTeSiHkWjXyFcCtOlSuJsZkDxUgPeZkJwMmNpErUlBcGuMlJwKkWnOzFeFiSiPsEvMmQiCsYeHlLuHoMgBjFoZkXlObDkSoQcVyReTmRsFzRhTuIvCeBqVsQdQyTyZjStGrTyDcEcAgTgMiIcVkLbZbGvWeHtXwEqWkXfTcPyHhHjYwIeVxLyVmHmMkUsGiHmNnQuMsXaFyPpVqNrBhOiWmNkBbQuHvQdOjPjKiZcL" }, { "input": "aHmRbLgNuWkLxLnWvUbYwTeZeYiOlLhTuOvKfLnVmCiPcMkSgVrYjZiLuRjCiXhAnVzVcTlVeJdBvPdDfFvHkTuIhCdBjEsXbVmGcLrPfNvRdFsZkSdNpYsJeIhIcNqSoLkOjUlYlDmXsOxPbQtIoUxFjGnRtBhFaJvBeEzHsAtVoQbAfYjJqReBiKeUwRqYrUjPjBoHkOkPzDwEwUgTxQxAvKzUpMhKyOhPmEhYhItQwPeKsKaKlUhGuMcTtSwFtXfJsDsFlTtOjVvVfGtBtFlQyIcBaMsPaJlPqUcUvLmReZiFbXxVtRhTzJkLkAjVqTyVuFeKlTyQgUzMsXjOxQnVfTaWmThEnEoIhZeZdStBkKeLpAhJnFoJvQyGwDiStLjEwGfZwBuWsEfC", "output": "AHmRbLgNuWkLxLnWvUbYwTeZeYiOlLhTuOvKfLnVmCiPcMkSgVrYjZiLuRjCiXhAnVzVcTlVeJdBvPdDfFvHkTuIhCdBjEsXbVmGcLrPfNvRdFsZkSdNpYsJeIhIcNqSoLkOjUlYlDmXsOxPbQtIoUxFjGnRtBhFaJvBeEzHsAtVoQbAfYjJqReBiKeUwRqYrUjPjBoHkOkPzDwEwUgTxQxAvKzUpMhKyOhPmEhYhItQwPeKsKaKlUhGuMcTtSwFtXfJsDsFlTtOjVvVfGtBtFlQyIcBaMsPaJlPqUcUvLmReZiFbXxVtRhTzJkLkAjVqTyVuFeKlTyQgUzMsXjOxQnVfTaWmThEnEoIhZeZdStBkKeLpAhJnFoJvQyGwDiStLjEwGfZwBuWsEfC" }, { "input": "sLlZkDiDmEdNaXuUuJwHqYvRtOdGfTiTpEpAoSqAbJaChOiCvHgSwZwEuPkMmXiLcKdXqSsEyViEbZpZsHeZpTuXoGcRmOiQfBfApPjDqSqElWeSeOhUyWjLyNoRuYeGfGwNqUsQoTyVvWeNgNdZfDxGwGfLsDjIdInSqDlMuNvFaHbScZkTlVwNcJpEjMaPaOtFgJjBjOcLlLmDnQrShIrJhOcUmPnZhTxNeClQsZaEaVaReLyQpLwEqJpUwYhLiRzCzKfOoFeTiXzPiNbOsZaZaLgCiNnMkBcFwGgAwPeNyTxJcCtBgXcToKlWaWcBaIvBpNxPeClQlWeQqRyEtAkJdBtSrFdDvAbUlKyLdCuTtXxFvRcKnYnWzVdYqDeCmOqPxUaFjQdTdCtN", "output": "SLlZkDiDmEdNaXuUuJwHqYvRtOdGfTiTpEpAoSqAbJaChOiCvHgSwZwEuPkMmXiLcKdXqSsEyViEbZpZsHeZpTuXoGcRmOiQfBfApPjDqSqElWeSeOhUyWjLyNoRuYeGfGwNqUsQoTyVvWeNgNdZfDxGwGfLsDjIdInSqDlMuNvFaHbScZkTlVwNcJpEjMaPaOtFgJjBjOcLlLmDnQrShIrJhOcUmPnZhTxNeClQsZaEaVaReLyQpLwEqJpUwYhLiRzCzKfOoFeTiXzPiNbOsZaZaLgCiNnMkBcFwGgAwPeNyTxJcCtBgXcToKlWaWcBaIvBpNxPeClQlWeQqRyEtAkJdBtSrFdDvAbUlKyLdCuTtXxFvRcKnYnWzVdYqDeCmOqPxUaFjQdTdCtN" }, { "input": "iRuStKvVhJdJbQwRoIuLiVdTpKaOqKfYlYwAzIpPtUwUtMeKyCaOlXmVrKwWeImYmVuXdLkRlHwFxKqZbZtTzNgOzDbGqTfZnKmUzAcIjDcEmQgYyFbEfWzRpKvCkDmAqDiIiRcLvMxWaJqCgYqXgIcLdNaZlBnXtJyKaMnEaWfXfXwTbDnAiYnWqKbAtDpYdUbZrCzWgRnHzYxFgCdDbOkAgTqBuLqMeStHcDxGnVhSgMzVeTaZoTfLjMxQfRuPcFqVlRyYdHyOdJsDoCeWrUuJyIiAqHwHyVpEeEoMaJwAoUfPtBeJqGhMaHiBjKwAlXoZpUsDhHgMxBkVbLcEvNtJbGnPsUwAvXrAkTlXwYvEnOpNeWyIkRnEnTrIyAcLkRgMyYcKrGiDaAyE", "output": "IRuStKvVhJdJbQwRoIuLiVdTpKaOqKfYlYwAzIpPtUwUtMeKyCaOlXmVrKwWeImYmVuXdLkRlHwFxKqZbZtTzNgOzDbGqTfZnKmUzAcIjDcEmQgYyFbEfWzRpKvCkDmAqDiIiRcLvMxWaJqCgYqXgIcLdNaZlBnXtJyKaMnEaWfXfXwTbDnAiYnWqKbAtDpYdUbZrCzWgRnHzYxFgCdDbOkAgTqBuLqMeStHcDxGnVhSgMzVeTaZoTfLjMxQfRuPcFqVlRyYdHyOdJsDoCeWrUuJyIiAqHwHyVpEeEoMaJwAoUfPtBeJqGhMaHiBjKwAlXoZpUsDhHgMxBkVbLcEvNtJbGnPsUwAvXrAkTlXwYvEnOpNeWyIkRnEnTrIyAcLkRgMyYcKrGiDaAyE" }, { "input": "cRtJkOxHzUbJcDdHzJtLbVmSoWuHoTkVrPqQaVmXeBrHxJbQfNrQbAaMrEhVdQnPxNyCjErKxPoEdWkVrBbDeNmEgBxYiBtWdAfHiLuSwIxJuHpSkAxPoYdNkGoLySsNhUmGoZhDzAfWhJdPlJzQkZbOnMtTkClIoCqOlIcJcMlGjUyOiEmHdYfIcPtTgQhLlLcPqQjAnQnUzHpCaQsCnYgQsBcJrQwBnWsIwFfSfGuYgTzQmShFpKqEeRlRkVfMuZbUsDoFoPrNuNwTtJqFkRiXxPvKyElDzLoUnIwAaBaOiNxMpEvPzSpGpFhMtGhGdJrFnZmNiMcUfMtBnDuUnXqDcMsNyGoLwLeNnLfRsIwRfBtXkHrFcPsLdXaAoYaDzYnZuQeVcZrElWmP", "output": "CRtJkOxHzUbJcDdHzJtLbVmSoWuHoTkVrPqQaVmXeBrHxJbQfNrQbAaMrEhVdQnPxNyCjErKxPoEdWkVrBbDeNmEgBxYiBtWdAfHiLuSwIxJuHpSkAxPoYdNkGoLySsNhUmGoZhDzAfWhJdPlJzQkZbOnMtTkClIoCqOlIcJcMlGjUyOiEmHdYfIcPtTgQhLlLcPqQjAnQnUzHpCaQsCnYgQsBcJrQwBnWsIwFfSfGuYgTzQmShFpKqEeRlRkVfMuZbUsDoFoPrNuNwTtJqFkRiXxPvKyElDzLoUnIwAaBaOiNxMpEvPzSpGpFhMtGhGdJrFnZmNiMcUfMtBnDuUnXqDcMsNyGoLwLeNnLfRsIwRfBtXkHrFcPsLdXaAoYaDzYnZuQeVcZrElWmP" }, { "input": "wVaCsGxZrBbFnTbKsCoYlAvUkIpBaYpYmJkMlPwCaFvUkDxAiJgIqWsFqZlFvTtAnGzEwXbYiBdFfFxRiDoUkLmRfAwOlKeOlKgXdUnVqLkTuXtNdQpBpXtLvZxWoBeNePyHcWmZyRiUkPlRqYiQdGeXwOhHbCqVjDcEvJmBkRwWnMqPjXpUsIyXqGjHsEsDwZiFpIbTkQaUlUeFxMwJzSaHdHnDhLaLdTuYgFuJsEcMmDvXyPjKsSeBaRwNtPuOuBtNeOhQdVgKzPzOdYtPjPfDzQzHoWcYjFbSvRgGdGsCmGnQsErToBkCwGeQaCbBpYkLhHxTbUvRnJpZtXjKrHdRiUmUbSlJyGaLnWsCrJbBnSjFaZrIzIrThCmGhQcMsTtOxCuUcRaEyPaG", "output": "WVaCsGxZrBbFnTbKsCoYlAvUkIpBaYpYmJkMlPwCaFvUkDxAiJgIqWsFqZlFvTtAnGzEwXbYiBdFfFxRiDoUkLmRfAwOlKeOlKgXdUnVqLkTuXtNdQpBpXtLvZxWoBeNePyHcWmZyRiUkPlRqYiQdGeXwOhHbCqVjDcEvJmBkRwWnMqPjXpUsIyXqGjHsEsDwZiFpIbTkQaUlUeFxMwJzSaHdHnDhLaLdTuYgFuJsEcMmDvXyPjKsSeBaRwNtPuOuBtNeOhQdVgKzPzOdYtPjPfDzQzHoWcYjFbSvRgGdGsCmGnQsErToBkCwGeQaCbBpYkLhHxTbUvRnJpZtXjKrHdRiUmUbSlJyGaLnWsCrJbBnSjFaZrIzIrThCmGhQcMsTtOxCuUcRaEyPaG" }, { "input": "kEiLxLmPjGzNoGkJdBlAfXhThYhMsHmZoZbGyCvNiUoLoZdAxUbGyQiEfXvPzZzJrPbEcMpHsMjIkRrVvDvQtHuKmXvGpQtXbPzJpFjJdUgWcPdFxLjLtXgVpEiFhImHnKkGiWnZbJqRjCyEwHsNbYfYfTyBaEuKlCtWnOqHmIgGrFmQiYrBnLiFcGuZxXlMfEuVoCxPkVrQvZoIpEhKsYtXrPxLcSfQqXsWaDgVlOnAzUvAhOhMrJfGtWcOwQfRjPmGhDyAeXrNqBvEiDfCiIvWxPjTwPlXpVsMjVjUnCkXgBuWnZaDyJpWkCfBrWnHxMhJgItHdRqNrQaEeRjAuUwRkUdRhEeGlSqVqGmOjNcUhFfXjCmWzBrGvIuZpRyWkWiLyUwFpYjNmNfV", "output": "KEiLxLmPjGzNoGkJdBlAfXhThYhMsHmZoZbGyCvNiUoLoZdAxUbGyQiEfXvPzZzJrPbEcMpHsMjIkRrVvDvQtHuKmXvGpQtXbPzJpFjJdUgWcPdFxLjLtXgVpEiFhImHnKkGiWnZbJqRjCyEwHsNbYfYfTyBaEuKlCtWnOqHmIgGrFmQiYrBnLiFcGuZxXlMfEuVoCxPkVrQvZoIpEhKsYtXrPxLcSfQqXsWaDgVlOnAzUvAhOhMrJfGtWcOwQfRjPmGhDyAeXrNqBvEiDfCiIvWxPjTwPlXpVsMjVjUnCkXgBuWnZaDyJpWkCfBrWnHxMhJgItHdRqNrQaEeRjAuUwRkUdRhEeGlSqVqGmOjNcUhFfXjCmWzBrGvIuZpRyWkWiLyUwFpYjNmNfV" }, { "input": "eIhDoLmDeReKqXsHcVgFxUqNfScAiQnFrTlCgSuTtXiYvBxKaPaGvUeYfSgHqEaWcHxKpFaSlCxGqAmNeFcIzFcZsBiVoZhUjXaDaIcKoBzYdIlEnKfScRqSkYpPtVsVhXsBwUsUfAqRoCkBxWbHgDiCkRtPvUwVgDjOzObYwNiQwXlGnAqEkHdSqLgUkOdZiWaHqQnOhUnDhIzCiQtVcJlGoRfLuVlFjWqSuMsLgLwOdZvKtWdRuRqDoBoInKqPbJdXpIqLtFlMlDaWgSiKbFpCxOnQeNeQzXeKsBzIjCyPxCmBnYuHzQoYxZgGzSgGtZiTeQmUeWlNzZeKiJbQmEjIiDhPeSyZlNdHpZnIkPdJzSeJpPiXxToKyBjJfPwNzZpWzIzGySqPxLtI", "output": "EIhDoLmDeReKqXsHcVgFxUqNfScAiQnFrTlCgSuTtXiYvBxKaPaGvUeYfSgHqEaWcHxKpFaSlCxGqAmNeFcIzFcZsBiVoZhUjXaDaIcKoBzYdIlEnKfScRqSkYpPtVsVhXsBwUsUfAqRoCkBxWbHgDiCkRtPvUwVgDjOzObYwNiQwXlGnAqEkHdSqLgUkOdZiWaHqQnOhUnDhIzCiQtVcJlGoRfLuVlFjWqSuMsLgLwOdZvKtWdRuRqDoBoInKqPbJdXpIqLtFlMlDaWgSiKbFpCxOnQeNeQzXeKsBzIjCyPxCmBnYuHzQoYxZgGzSgGtZiTeQmUeWlNzZeKiJbQmEjIiDhPeSyZlNdHpZnIkPdJzSeJpPiXxToKyBjJfPwNzZpWzIzGySqPxLtI" }, { "input": "uOoQzIeTwYeKpJtGoUdNiXbPgEwVsZkAnJcArHxIpEnEhZwQhZvAiOuLeMkVqLeDsAyKeYgFxGmRoLaRsZjAeXgNfYhBkHeDrHdPuTuYhKmDlAvYzYxCdYgYfVaYlGeVqTeSfBxQePbQrKsTaIkGzMjFrQlJuYaMxWpQkLdEcDsIiMnHnDtThRvAcKyGwBsHqKdXpJfIeTeZtYjFbMeUoXoXzGrShTwSwBpQlKeDrZdCjRqNtXoTsIzBkWbMsObTtDvYaPhUeLeHqHeMpZmTaCcIqXzAmGnPfNdDaFhOqWqDrWuFiBpRjZrQmAdViOuMbFfRyXyWfHgRkGpPnDrEqQcEmHcKpEvWlBrOtJbUaXbThJaSxCbVoGvTmHvZrHvXpCvLaYbRiHzYuQyX", "output": "UOoQzIeTwYeKpJtGoUdNiXbPgEwVsZkAnJcArHxIpEnEhZwQhZvAiOuLeMkVqLeDsAyKeYgFxGmRoLaRsZjAeXgNfYhBkHeDrHdPuTuYhKmDlAvYzYxCdYgYfVaYlGeVqTeSfBxQePbQrKsTaIkGzMjFrQlJuYaMxWpQkLdEcDsIiMnHnDtThRvAcKyGwBsHqKdXpJfIeTeZtYjFbMeUoXoXzGrShTwSwBpQlKeDrZdCjRqNtXoTsIzBkWbMsObTtDvYaPhUeLeHqHeMpZmTaCcIqXzAmGnPfNdDaFhOqWqDrWuFiBpRjZrQmAdViOuMbFfRyXyWfHgRkGpPnDrEqQcEmHcKpEvWlBrOtJbUaXbThJaSxCbVoGvTmHvZrHvXpCvLaYbRiHzYuQyX" }, { "input": "lZqBqKeGvNdSeYuWxRiVnFtYbKuJwQtUcKnVtQhAlOeUzMaAuTaEnDdPfDcNyHgEoBmYjZyFePeJrRiKyAzFnBfAuGiUyLrIeLrNhBeBdVcEeKgCcBrQzDsPwGcNnZvTsEaYmFfMeOmMdNuZbUtDoQoNcGwDqEkEjIdQaPwAxJbXeNxOgKgXoEbZiIsVkRrNpNyAkLeHkNfEpLuQvEcMbIoGaDzXbEtNsLgGfOkZaFiUsOvEjVeCaMcZqMzKeAdXxJsVeCrZaFpJtZxInQxFaSmGgSsVyGeLlFgFqTpIbAvPkIfJrVcJeBxSdEvPyVwIjHpYrLrKqLnAmCuGmPoZrSbOtGaLaTmBmSuUyAmAsRiMqOtRjJhPhAfXaJnTpLbFqPmJgFcBxImTqIiJ", "output": "LZqBqKeGvNdSeYuWxRiVnFtYbKuJwQtUcKnVtQhAlOeUzMaAuTaEnDdPfDcNyHgEoBmYjZyFePeJrRiKyAzFnBfAuGiUyLrIeLrNhBeBdVcEeKgCcBrQzDsPwGcNnZvTsEaYmFfMeOmMdNuZbUtDoQoNcGwDqEkEjIdQaPwAxJbXeNxOgKgXoEbZiIsVkRrNpNyAkLeHkNfEpLuQvEcMbIoGaDzXbEtNsLgGfOkZaFiUsOvEjVeCaMcZqMzKeAdXxJsVeCrZaFpJtZxInQxFaSmGgSsVyGeLlFgFqTpIbAvPkIfJrVcJeBxSdEvPyVwIjHpYrLrKqLnAmCuGmPoZrSbOtGaLaTmBmSuUyAmAsRiMqOtRjJhPhAfXaJnTpLbFqPmJgFcBxImTqIiJ" }, { "input": "P", "output": "P" }, { "input": "Xyzzy", "output": "Xyzzy" }, { "input": "Zzz", "output": "Zzz" }, { "input": "Zp", "output": "Zp" } ]
92
0
3
1
322
Ciel and Flowers
[ "combinatorics", "math" ]
null
null
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: - 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. Help Fox Ciel to find the maximal number of bouquets she can make.
The first line contains three integers *r*, *g* and *b* (0<=≀<=*r*,<=*g*,<=*b*<=≀<=109) β€” the number of red, green and blue flowers.
Print the maximal number of bouquets Fox Ciel can make.
[ "3 6 9\n", "4 4 4\n", "0 0 0\n" ]
[ "6\n", "4\n", "0\n" ]
In test case 1, we can make 1 red bouquet, 2 green bouquets and 3 blue bouquets. In test case 2, we can make 1 red, 1 green, 1 blue and 1 mixing bouquet.
[ { "input": "3 6 9", "output": "6" }, { "input": "4 4 4", "output": "4" }, { "input": "0 0 0", "output": "0" }, { "input": "0 3 6", "output": "3" }, { "input": "7 8 9", "output": "7" }, { "input": "8 8 9", "output": "8" }, { "input": "15 3 999", "output": "339" }, { "input": "32 62 92", "output": "62" }, { "input": "123456789 123456789 123456789", "output": "123456789" }, { "input": "3 5 5", "output": "4" }, { "input": "666806767 385540591 357848286", "output": "470065214" }, { "input": "80010646 727118126 817880463", "output": "541669744" }, { "input": "829651016 732259171 572879931", "output": "711596705" }, { "input": "242854896 442432924 180395753", "output": "288561190" }, { "input": "139978911 5123031 935395222", "output": "360165721" }, { "input": "553182792 10264076 395427398", "output": "319624755" }, { "input": "597790453 720437830 855459575", "output": "724562619" }, { "input": "494914467 356982656 757942689", "output": "536613270" }, { "input": "908118348 67156409 217974865", "output": "397749873" }, { "input": "952726009 629846517 972974334", "output": "851848953" }, { "input": "775140200 616574841 630329230", "output": "674014756" }, { "input": "524780569 326748594 90361407", "output": "313963523" }, { "input": "937984449 184405994 992844522", "output": "705078321" }, { "input": "835108464 525983528 452876698", "output": "604656229" }, { "input": "879716125 531124573 207876166", "output": "539572288" }, { "input": "292920005 241298326 667908343", "output": "400708891" }, { "input": "1000000000 1000000000 1000000000", "output": "1000000000" }, { "input": "1000000000 999999999 999999998", "output": "999999998" }, { "input": "999999998 999999998 999999999", "output": "999999998" }, { "input": "0 1 1", "output": "0" }, { "input": "0 1000000000 0", "output": "333333333" }, { "input": "0 1 0", "output": "0" }, { "input": "1 0 0", "output": "0" }, { "input": "0 2 2", "output": "0" }, { "input": "3 3 5", "output": "3" }, { "input": "2 2 0", "output": "0" }, { "input": "0 5 5", "output": "2" }, { "input": "2 0 11", "output": "3" }, { "input": "9 9 7", "output": "8" }, { "input": "65 30 74", "output": "56" } ]
78
102,400
3
2
298
Snow Footprints
[ "greedy", "implementation" ]
null
null
There is a straight snowy road, divided into *n* blocks. The blocks are numbered from 1 to *n* from left to right. If one moves from the *i*-th block to the (*i*<=+<=1)-th block, he will leave a right footprint on the *i*-th block. Similarly, if one moves from the *i*-th block to the (*i*<=-<=1)-th block, he will leave a left footprint on the *i*-th block. If there already is a footprint on the *i*-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the *s*-th block, makes a sequence of moves and ends in the *t*-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of *s*,<=*t* by looking at the footprints.
The first line of the input contains integer *n* (3<=≀<=*n*<=≀<=1000). The second line contains the description of the road β€” the string that consists of *n* characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Print two space-separated integers β€” the values of *s* and *t*. If there are several possible solutions you can print any of them.
[ "9\n..RRLL...\n", "11\n.RRRLLLLL..\n" ]
[ "3 4\n", "7 5\n" ]
The first test sample is the one in the picture.
[ { "input": "11\n.RRRLLLLL..", "output": "7 5" }, { "input": "4\n.RL.", "output": "3 2" }, { "input": "3\n.L.", "output": "2 1" }, { "input": "3\n.R.", "output": "2 3" } ]
0
0
-1
3
804
Minimum number of steps
[ "combinatorics", "greedy", "implementation", "math" ]
null
null
We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109<=+<=7. The string "ab" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string.
The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.
Print the minimum number of steps modulo 109<=+<=7.
[ "ab\n", "aab\n" ]
[ "1\n", "3\n" ]
The first example: "ab"  →  "bba". The second example: "aab"  →  "abba"  →  "bbaba"  →  "bbbbaa".
[ { "input": "ab", "output": "1" }, { "input": "aab", "output": "3" }, { "input": "aaaaabaabababaaaaaba", "output": "17307" }, { "input": "abaabaaabbabaabab", "output": "1795" }, { "input": "abbaa", "output": "2" }, { "input": "abbaaabaabaaaaabbbbaababaaaaabaabbaaaaabbaabbaaaabbbabbbabb", "output": "690283580" }, { "input": "aababbaaaabbaabbbbbbbbabbababbbaaabbaaabbabbba", "output": "2183418" }, { "input": "aabbaababbabbbaabbaababaaaabbaaaabaaaaaababbaaaabaababbabbbb", "output": "436420225" }, { "input": "aaabaaaabbababbaabbababbbbaaaaaaabbabbba", "output": "8431094" }, { "input": "abbbbababbabbbbbabaabbbaabbbbbbbaaab", "output": "8180" }, { "input": "bbababbbaabaaaaaaaabbabbbb", "output": "40979" }, { "input": "abbbaaabbbbbabaabbaaabbbababbbaabaabababababa", "output": "2065758" }, { "input": "abaaaaaabaaaabbabbaaabbbbabababaaaaabbaabbaaaaabbbaababaaaaaaabbbbbaaaaabaababbabababbabbbbaabbaabbabbbabaabbaabbaaaaaab", "output": "235606597" }, { "input": "abbbbbbbbbbbbbbbbbbbbbbbbbbaababaaaaaaabaabaaababaabaababaaabababaababab", "output": "7" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbaaaaaaaaabaabaaababaabaababaaabababaabbbbbbb", "output": "557763786" } ]
218
6,348,800
3
4