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
938
Buy a Ticket
[ "data structures", "graphs", "shortest paths" ]
null
null
Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well. There are *n* cities in Berland. People can travel between cities using two-directional train routes; there are exactly *m* routes, *i*-th route can be used to go from city *v**i* to city *u**i* (and from *u**i* to *v**i*), and it costs *w**i* coins to use this route. Each city will be visited by "Flayer", and the cost of the concert ticket in *i*-th city is *a**i* coins. You have friends in every city of Berland, and they, knowing about your programming skills, asked you to calculate the minimum possible number of coins they have to pay to visit the concert. For every city *i* you have to compute the minimum number of coins a person from city *i* has to spend to travel to some city *j* (or possibly stay in city *i*), attend a concert there, and return to city *i* (if *j*<=β‰ <=*i*). Formally, for every you have to calculate , where *d*(*i*,<=*j*) is the minimum number of coins you have to spend to travel from city *i* to city *j*. If there is no way to reach city *j* from city *i*, then we consider *d*(*i*,<=*j*) to be infinitely large.
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=2Β·105, 1<=≀<=*m*<=≀<=2Β·105). Then *m* lines follow, *i*-th contains three integers *v**i*, *u**i* and *w**i* (1<=≀<=*v**i*,<=*u**i*<=≀<=*n*,<=*v**i*<=β‰ <=*u**i*, 1<=≀<=*w**i*<=≀<=1012) denoting *i*-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (*v*,<=*u*) neither extra (*v*,<=*u*) nor (*u*,<=*v*) present in input. The next line contains *n* integers *a*1,<=*a*2,<=... *a**k* (1<=≀<=*a**i*<=≀<=1012) β€” price to attend the concert in *i*-th city.
Print *n* integers. *i*-th of them must be equal to the minimum number of coins a person from city *i* has to spend to travel to some city *j* (or possibly stay in city *i*), attend a concert there, and return to city *i* (if *j*<=β‰ <=*i*).
[ "4 2\n1 2 4\n2 3 7\n6 20 1 25\n", "3 3\n1 2 1\n2 3 1\n1 3 1\n30 10 20\n" ]
[ "6 14 1 25 \n", "12 10 12 \n" ]
none
[ { "input": "4 2\n1 2 4\n2 3 7\n6 20 1 25", "output": "6 14 1 25 " }, { "input": "3 3\n1 2 1\n2 3 1\n1 3 1\n30 10 20", "output": "12 10 12 " }, { "input": "7 7\n1 6 745325\n2 3 3581176\n2 4 19\n3 6 71263060078\n5 4 141198\n7 4 163953\n5 6 15994\n1 297404206755 82096176217 14663411 187389745 21385 704393", "output": "1 335807 7498159 335769 53373 21385 663675 " } ]
93
307,200
-1
1,989
510
Fox And Snake
[ "implementation" ]
null
null
Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead. A snake is a pattern on a *n* by *m* table. Denote *c*-th cell of *r*-th row as (*r*,<=*c*). The tail of the snake is located at (1,<=1), then it's body extends to (1,<=*m*), then goes down 2 rows to (3,<=*m*), then goes left to (3,<=1) and so on. Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and the snake cells should be filled with number signs ('#'). Consider sample tests in order to understand the snake pattern.
The only line contains two integers: *n* and *m* (3<=≀<=*n*,<=*m*<=≀<=50). *n* is an odd number.
Output *n* lines. Each line should contain a string consisting of *m* characters. Do not output spaces.
[ "3 3\n", "3 4\n", "5 3\n", "9 9\n" ]
[ "###\n..#\n###\n", "####\n...#\n####\n", "###\n..#\n###\n#..\n###\n", "#########\n........#\n#########\n#........\n#########\n........#\n#########\n#........\n#########\n" ]
none
[ { "input": "3 3", "output": "###\n..#\n###" }, { "input": "3 4", "output": "####\n...#\n####" }, { "input": "5 3", "output": "###\n..#\n###\n#..\n###" }, { "input": "9 9", "output": "#########\n........#\n#########\n#........\n#########\n........#\n#########\n#........\n#########" }, { "input": "3 5", "output": "#####\n....#\n#####" }, { "input": "3 6", "output": "######\n.....#\n######" }, { "input": "7 3", "output": "###\n..#\n###\n#..\n###\n..#\n###" }, { "input": "7 4", "output": "####\n...#\n####\n#...\n####\n...#\n####" }, { "input": "49 50", "output": "##################################################\n.................................................#\n##################################################\n#.................................................\n##################################################\n.................................................#\n##################################################\n#.................................................\n##################################################\n.............................................." }, { "input": "43 50", "output": "##################################################\n.................................................#\n##################################################\n#.................................................\n##################################################\n.................................................#\n##################################################\n#.................................................\n##################################################\n.............................................." }, { "input": "43 27", "output": "###########################\n..........................#\n###########################\n#..........................\n###########################\n..........................#\n###########################\n#..........................\n###########################\n..........................#\n###########################\n#..........................\n###########################\n..........................#\n###########################\n#..........................\n###########################\n....................." }, { "input": "11 15", "output": "###############\n..............#\n###############\n#..............\n###############\n..............#\n###############\n#..............\n###############\n..............#\n###############" }, { "input": "11 3", "output": "###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###" }, { "input": "19 3", "output": "###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###" }, { "input": "23 50", "output": "##################################################\n.................................................#\n##################################################\n#.................................................\n##################################################\n.................................................#\n##################################################\n#.................................................\n##################################################\n.............................................." }, { "input": "49 49", "output": "#################################################\n................................................#\n#################################################\n#................................................\n#################################################\n................................................#\n#################################################\n#................................................\n#################################################\n................................................#\n#..." }, { "input": "33 43", "output": "###########################################\n..........................................#\n###########################################\n#..........................................\n###########################################\n..........................................#\n###########################################\n#..........................................\n###########################################\n..........................................#\n###########################################\n#.................." }, { "input": "33 44", "output": "############################################\n...........................................#\n############################################\n#...........................................\n############################################\n...........................................#\n############################################\n#...........................................\n############################################\n...........................................#\n############################################\n#......." }, { "input": "45 45", "output": "#############################################\n............................................#\n#############################################\n#............................................\n#############################################\n............................................#\n#############################################\n#............................................\n#############################################\n............................................#\n#########################################..." }, { "input": "45 49", "output": "#################################################\n................................................#\n#################################################\n#................................................\n#################################################\n................................................#\n#################################################\n#................................................\n#################################################\n................................................#\n#..." }, { "input": "45 3", "output": "###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###\n..#\n###\n#..\n###" }, { "input": "45 4", "output": "####\n...#\n####\n#...\n####\n...#\n####\n#...\n####\n...#\n####\n#...\n####\n...#\n####\n#...\n####\n...#\n####\n#...\n####\n...#\n####\n#...\n####\n...#\n####\n#...\n####\n...#\n####\n#...\n####\n...#\n####\n#...\n####\n...#\n####\n#...\n####\n...#\n####\n#...\n####" }, { "input": "5 4", "output": "####\n...#\n####\n#...\n####" }, { "input": "5 5", "output": "#####\n....#\n#####\n#....\n#####" }, { "input": "5 6", "output": "######\n.....#\n######\n#.....\n######" }, { "input": "7 5", "output": "#####\n....#\n#####\n#....\n#####\n....#\n#####" }, { "input": "7 6", "output": "######\n.....#\n######\n#.....\n######\n.....#\n######" }, { "input": "7 7", "output": "#######\n......#\n#######\n#......\n#######\n......#\n#######" } ]
46
0
3
1,996
538
Cutting Banner
[ "brute force", "implementation" ]
null
null
A large banner with word CODEFORCES was ordered for the 1000-th onsite round of Codeforcesω that takes place on the Miami beach. Unfortunately, the company that made the banner mixed up two orders and delivered somebody else's banner that contains someone else's word. The word on the banner consists only of upper-case English letters. There is very little time to correct the mistake. All that we can manage to do is to cut out some substring from the banner, i.e. several consecutive letters. After that all the resulting parts of the banner will be glued into a single piece (if the beginning or the end of the original banner was cut out, only one part remains); it is not allowed change the relative order of parts of the banner (i.e. after a substring is cut, several first and last letters are left, it is allowed only to glue the last letters to the right of the first letters). Thus, for example, for example, you can cut a substring out from string 'TEMPLATE' and get string 'TEMPLE' (if you cut out string AT), 'PLATE' (if you cut out TEM), 'T' (if you cut out EMPLATE), etc. Help the organizers of the round determine whether it is possible to cut out of the banner some substring in such a way that the remaining parts formed word CODEFORCES.
The single line of the input contains the word written on the banner. The word only consists of upper-case English letters. The word is non-empty and its length doesn't exceed 100 characters. It is guaranteed that the word isn't word CODEFORCES.
Print 'YES', if there exists a way to cut out the substring, and 'NO' otherwise (without the quotes).
[ "CODEWAITFORITFORCES\n", "BOTTOMCODER\n", "DECODEFORCES\n", "DOGEFORCES\n" ]
[ "YES\n", "NO\n", "YES\n", "NO\n" ]
none
[ { "input": "CODEWAITFORITFORCES", "output": "YES" }, { "input": "BOTTOMCODER", "output": "NO" }, { "input": "DECODEFORCES", "output": "YES" }, { "input": "DOGEFORCES", "output": "NO" }, { "input": "ABACABA", "output": "NO" }, { "input": "CODEFORCE", "output": "NO" }, { "input": "C", "output": "NO" }, { "input": "NQTSMZEBLY", "output": "NO" }, { "input": "CODEFZORCES", "output": "YES" }, { "input": "EDYKHVZCNTLJUUOQGHPTIOETQNFLLWEKZOHIUAXELGECABVSBIBGQODQXVYFKBYJWTGBYHVSSNTINKWSINWSMALUSIWNJMTCOOVF", "output": "NO" }, { "input": "OCECFDSRDE", "output": "NO" }, { "input": "MDBUWCZFFZKFMJTTJFXRHTGRPREORKDVUXOEMFYSOMSQGHUKGYCRCVJTNDLFDEWFS", "output": "NO" }, { "input": "CODEFYTORCHES", "output": "NO" }, { "input": "BCODEFORCES", "output": "YES" }, { "input": "CVODEFORCES", "output": "YES" }, { "input": "COAKDEFORCES", "output": "YES" }, { "input": "CODFMWEFORCES", "output": "YES" }, { "input": "CODEVCSYRFORCES", "output": "YES" }, { "input": "CODEFXHHPWCVQORCES", "output": "YES" }, { "input": "CODEFORQWUFJLOFFXTXRCES", "output": "YES" }, { "input": "CODEFORBWFURYIDURNRKRDLHCLXZCES", "output": "YES" }, { "input": "CODEFORCQSYSLYKCDFFUPSAZCJIAENCKZUFJZEINQIES", "output": "YES" }, { "input": "CODEFORCEVENMDBQLSVPQIIBGSHBVOPYZXNWVSTVWDRONUREYJJIJIPMEBPQDCPFS", "output": "YES" }, { "input": "CODEFORCESCFNNPAHNHDIPPBAUSPKJYAQDBVZNLSTSDCREZACVLMRFGVKGVHHZLXOHCTJDBQKIDWBUXDUJARLWGFGFCTTXUCAZB", "output": "YES" }, { "input": "CODJRDPDEFOROES", "output": "NO" }, { "input": "CODEFOGSIUZMZCMWAVQHNYFEKIEZQMAZOVEMDRMOEDBHAXPLBLDYYXCVTOOSJZVSQAKFXTBTZFWAYRZEMDEMVDJTDRXXAQBURCES", "output": "YES" }, { "input": "CODEMKUYHAZSGJBQLXTHUCZZRJJJXUSEBOCNZASOKDZHMSGWZSDFBGHXFLABVPDQBJYXSHHAZAKHSTRGOKJYHRVSSUGDCMFOGCES", "output": "NO" }, { "input": "CODEFORCESCODEFORCESCODEFORCESCODEFORCESCODEFORCESCODEFORCESCODEFORCESCODEFORCESCODEFORCES", "output": "YES" }, { "input": "CCODEFORCESODECODEFORCCODEFORCESODCODEFORCESEFCODEFORCESORCODEFORCESCESCESFORCODEFORCESCES", "output": "NO" }, { "input": "CCODEFORCESC", "output": "NO" }, { "input": "CODEAFORBCES", "output": "NO" }, { "input": "CODERRRRRFORCRRRRES", "output": "NO" }, { "input": "CODELFORCELS", "output": "NO" }, { "input": "CPOPDPEPFPOPRPCPEPS", "output": "NO" }, { "input": "COXDEXFORXCEXS", "output": "NO" }, { "input": "CODAAAAAFORCES", "output": "NO" }, { "input": "CAOADEFORCES", "output": "NO" }, { "input": "FORCESXCODE", "output": "NO" }, { "input": "FORCESACODE", "output": "NO" }, { "input": "ACAOADAEFORCES", "output": "NO" }, { "input": "CCODEFORCESS", "output": "NO" }, { "input": "ZCODEFORCEZ", "output": "NO" }, { "input": "CODXEFORXCES", "output": "NO" }, { "input": "CODEFORCEZ", "output": "NO" }, { "input": "CODEFORCEST", "output": "YES" }, { "input": "AXODEFORCES", "output": "NO" }, { "input": "RCODEFORCESR", "output": "NO" }, { "input": "CODECODEFORCESFORCES", "output": "YES" }, { "input": "TTTWWWCODEFORCES", "output": "YES" }, { "input": "CODEFORRCEST", "output": "NO" }, { "input": "UJYTYUCODEFORCES", "output": "YES" }, { "input": "CODEXXXXXXXXXXXXXXXXXXCODEFORCESXXXXXXXXXXXXXXXXXXXXXFORCES", "output": "YES" }, { "input": "COXEDYFORCES", "output": "NO" }, { "input": "UJYTYCODEFORCES", "output": "YES" }, { "input": "UJYTCODEFORCES", "output": "YES" } ]
140
0
0
2,002
322
Ciel and Dancing
[ "greedy" ]
null
null
Fox Ciel and her friends are in a dancing room. There are *n* boys and *m* girls here, and they never danced before. There will be some songs, during each song, there must be exactly one boy and one girl are dancing. Besides, there is a special rule: - either the boy in the dancing pair must dance for the first time (so, he didn't dance with anyone before); - or the girl in the dancing pair must dance for the first time. Help Fox Ciel to make a schedule that they can dance as many songs as possible.
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100) β€” the number of boys and girls in the dancing room.
In the first line print *k* β€” the number of songs during which they can dance. Then in the following *k* lines, print the indexes of boys and girls dancing during songs chronologically. You can assume that the boys are indexed from 1 to *n*, and the girls are indexed from 1 to *m*.
[ "2 1\n", "2 2\n" ]
[ "2\n1 1\n2 1\n", "3\n1 1\n1 2\n2 2\n" ]
In test case 1, there are 2 boys and 1 girl. We can have 2 dances: the 1st boy and 1st girl (during the first song), the 2nd boy and 1st girl (during the second song). And in test case 2, we have 2 boys with 2 girls, the answer is 3.
[ { "input": "2 1", "output": "2\n1 1\n2 1" }, { "input": "2 2", "output": "3\n1 1\n1 2\n2 2" }, { "input": "1 1", "output": "1\n1 1" }, { "input": "2 3", "output": "4\n1 1\n1 2\n1 3\n2 3" }, { "input": "4 4", "output": "7\n1 1\n1 2\n1 3\n1 4\n4 4\n3 4\n2 4" }, { "input": "1 12", "output": "12\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12" }, { "input": "12 1", "output": "12\n1 1\n12 1\n11 1\n10 1\n9 1\n8 1\n7 1\n6 1\n5 1\n4 1\n3 1\n2 1" }, { "input": "100 100", "output": "199\n1 1\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\n1 85\n1 86\n..." }, { "input": "24 6", "output": "29\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n24 6\n23 6\n22 6\n21 6\n20 6\n19 6\n18 6\n17 6\n16 6\n15 6\n14 6\n13 6\n12 6\n11 6\n10 6\n9 6\n8 6\n7 6\n6 6\n5 6\n4 6\n3 6\n2 6" }, { "input": "7 59", "output": "65\n1 1\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\n7 59\n6 59\n5 59\n4 59\n3 59\n2 59" }, { "input": "26 75", "output": "100\n1 1\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\n26 75\n25 75\n24 75\n23 75\n22 75\n21 75\n20 75\n19 75\n18 75\n17..." }, { "input": "32 87", "output": "118\n1 1\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\n1 85\n1 86\n..." }, { "input": "42 51", "output": "92\n1 1\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\n42 51\n41 51\n40 51\n39 51\n38 51\n37 51\n36 51\n35 51\n34 51\n33 51\n32 51\n31 51\n30 51\n29 51\n28 51\n27 51\n26 51\n25 51\n24 51\n23 51\n22 51\n21 51\n20 51\n19 51\n18 51\n17 51\n16 51\n15 51\n14 51\n13 51\n..." }, { "input": "4 63", "output": "66\n1 1\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\n4 63\n3 63\n2 63" }, { "input": "10 79", "output": "88\n1 1\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\n10 79\n9 79\n8 79\n7 79\n6 79\n5 79\n4 79\n..." }, { "input": "20 95", "output": "114\n1 1\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\n1 85\n1 86\n..." }, { "input": "35 55", "output": "89\n1 1\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\n35 55\n34 55\n33 55\n32 55\n31 55\n30 55\n29 55\n28 55\n27 55\n26 55\n25 55\n24 55\n23 55\n22 55\n21 55\n20 55\n19 55\n18 55\n17 55\n16 55\n15 55\n14 55\n13 55\n12 55\n11 55\n10 55\n9 55..." }, { "input": "45 71", "output": "115\n1 1\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\n45 71\n44 71\n43 71\n42 71\n41 71\n40 71\n39 71\n38 71\n37 71\n36 71\n35 71\n34 71\n33 71..." }, { "input": "7 83", "output": "89\n1 1\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\n7 83\n6 83\n5 83\n..." }, { "input": "32 100", "output": "131\n1 1\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\n1 85\n1 86\n..." }, { "input": "42 17", "output": "58\n1 1\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\n42 17\n41 17\n40 17\n39 17\n38 17\n37 17\n36 17\n35 17\n34 17\n33 17\n32 17\n31 17\n30 17\n29 17\n28 17\n27 17\n26 17\n25 17\n24 17\n23 17\n22 17\n21 17\n20 17\n19 17\n18 17\n17 17\n16 17\n15 17\n14 17\n13 17\n12 17\n11 17\n10 17\n9 17\n8 17\n7 17\n6 17\n5 17\n4 17\n3 17\n2 17" }, { "input": "1 77", "output": "77\n1 1\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" }, { "input": "19 93", "output": "111\n1 1\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\n1 85\n1 86\n..." }, { "input": "25 5", "output": "29\n1 1\n1 2\n1 3\n1 4\n1 5\n25 5\n24 5\n23 5\n22 5\n21 5\n20 5\n19 5\n18 5\n17 5\n16 5\n15 5\n14 5\n13 5\n12 5\n11 5\n10 5\n9 5\n8 5\n7 5\n6 5\n5 5\n4 5\n3 5\n2 5" }, { "input": "35 21", "output": "55\n1 1\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\n35 21\n34 21\n33 21\n32 21\n31 21\n30 21\n29 21\n28 21\n27 21\n26 21\n25 21\n24 21\n23 21\n22 21\n21 21\n20 21\n19 21\n18 21\n17 21\n16 21\n15 21\n14 21\n13 21\n12 21\n11 21\n10 21\n9 21\n8 21\n7 21\n6 21\n5 21\n4 21\n3 21\n2 21" }, { "input": "99 99", "output": "197\n1 1\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\n1 85\n1 86\n..." }, { "input": "99 100", "output": "198\n1 1\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\n1 85\n1 86\n..." }, { "input": "100 99", "output": "198\n1 1\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\n1 85\n1 86\n..." } ]
216
512,000
3
2,004
0
none
[ "none" ]
null
null
This is an interactive problem. Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet. Let's define $x$ as the distance to Mars. Unfortunately, Natasha does not know $x$. But it is known that $1 \le x \le m$, where Natasha knows the number $m$. Besides, $x$ and $m$ are positive integers. Natasha can ask the rocket questions. Every question is an integer $y$ ($1 \le y \le m$). The correct answer to the question is $-1$, if $x&lt;y$, $0$, if $x=y$, and $1$, if $x&gt;y$. But the rocket is brokenΒ β€” it does not always answer correctly. Precisely: let the correct answer to the current question be equal to $t$, then, if the rocket answers this question correctly, then it will answer $t$, otherwise it will answer $-t$. In addition, the rocket has a sequence $p$ of length $n$. Each element of the sequence is either $0$ or $1$. The rocket processes this sequence in the cyclic order, that is $1$-st element, $2$-nd, $3$-rd, $\ldots$, $(n-1)$-th, $n$-th, $1$-st, $2$-nd, $3$-rd, $\ldots$, $(n-1)$-th, $n$-th, $\ldots$. If the current element is $1$, the rocket answers correctly, if $0$Β β€” lies. Natasha doesn't know the sequence $p$, but she knows its lengthΒ β€” $n$. You can ask the rocket no more than $60$ questions. Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions. Your solution will not be accepted, if it does not receive an answer $0$ from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).
The first line contains two integers $m$ and $n$ ($1 \le m \le 10^9$, $1 \le n \le 30$)Β β€” the maximum distance to Mars and the number of elements in the sequence $p$.
none
[ "5 2\n1\n-1\n-1\n1\n0\n" ]
[ "1\n2\n4\n5\n3\n" ]
In the example, hacking would look like this: 5 2 3 1 0 This means that the current distance to Mars is equal to $3$, Natasha knows that it does not exceed $5$, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ... Really: on the first query ($1$) the correct answer is $1$, the rocket answered correctly: $1$; on the second query ($2$) the correct answer is $1$, the rocket answered incorrectly: $-1$; on the third query ($4$) the correct answer is $-1$, the rocket answered correctly: $-1$; on the fourth query ($5$) the correct answer is $-1$, the rocket answered incorrectly: $1$; on the fifth query ($3$) the correct and incorrect answer is $0$.
[ { "input": "5 2 3\n1 0", "output": "3 queries, x=3" }, { "input": "1 1 1\n1", "output": "1 queries, x=1" }, { "input": "3 2 3\n1 0", "output": "4 queries, x=3" }, { "input": "6 3 5\n1 1 1", "output": "5 queries, x=5" }, { "input": "10 4 3\n0 0 1 0", "output": "6 queries, x=3" }, { "input": "30 5 16\n0 1 1 1 0", "output": "6 queries, x=16" }, { "input": "60 6 21\n1 0 0 1 0 1", "output": "11 queries, x=21" }, { "input": "100 7 73\n0 0 0 1 0 1 1", "output": "14 queries, x=73" }, { "input": "1000000000 29 958572235\n1 1 0 1 1 1 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0", "output": "58 queries, x=958572235" }, { "input": "738009704 30 116044407\n0 0 1 1 0 0 1 0 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 1 1", "output": "59 queries, x=116044407" }, { "input": "300 8 165\n1 1 1 0 0 1 1 0", "output": "16 queries, x=165" }, { "input": "600 9 150\n0 0 1 0 1 0 1 0 1", "output": "19 queries, x=150" }, { "input": "1000 10 140\n0 0 0 0 1 0 0 0 0 0", "output": "20 queries, x=140" }, { "input": "3000 11 1896\n1 0 1 1 0 0 0 0 1 1 1", "output": "21 queries, x=1896" }, { "input": "6000 12 4679\n1 0 1 1 1 1 1 0 0 0 0 1", "output": "23 queries, x=4679" }, { "input": "10000 13 4977\n1 0 1 1 0 0 0 1 0 0 1 1 0", "output": "26 queries, x=4977" }, { "input": "30000 14 60\n1 1 1 0 0 1 0 1 0 0 1 0 0 0", "output": "28 queries, x=60" }, { "input": "60000 15 58813\n0 1 1 0 1 1 0 0 0 1 1 1 1 0 1", "output": "27 queries, x=58813" }, { "input": "100000 16 79154\n1 1 1 0 0 0 0 0 1 1 0 1 0 1 0 1", "output": "32 queries, x=79154" }, { "input": "300000 17 11107\n1 0 0 0 1 0 0 0 1 1 1 0 0 1 1 1 0", "output": "34 queries, x=11107" }, { "input": "600000 18 146716\n0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 1 1", "output": "37 queries, x=146716" }, { "input": "1000000 19 418016\n1 0 0 1 0 1 0 0 1 1 0 0 0 1 1 1 1 0 0", "output": "38 queries, x=418016" }, { "input": "3000000 20 642518\n1 0 0 1 0 1 1 1 1 1 0 0 0 1 0 1 0 1 0 1", "output": "41 queries, x=642518" }, { "input": "6000000 21 3516807\n0 0 0 1 0 1 0 1 1 0 0 0 0 1 0 1 1 1 0 0 0", "output": "43 queries, x=3516807" }, { "input": "10000000 22 8115129\n1 0 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 0 0 1", "output": "42 queries, x=8115129" }, { "input": "30000000 23 10362635\n0 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 1 1 1 0 0 0", "output": "48 queries, x=10362635" }, { "input": "60000000 24 52208533\n1 1 1 0 1 0 0 0 0 0 1 0 0 1 0 1 0 1 1 0 1 1 1 0", "output": "46 queries, x=52208533" }, { "input": "100000000 25 51744320\n0 1 1 1 1 0 1 1 1 1 1 1 0 1 0 1 1 0 0 1 1 0 1 0 1", "output": "50 queries, x=51744320" }, { "input": "300000000 26 264009490\n1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 0 1 1 1 0 1 1", "output": "54 queries, x=264009490" }, { "input": "600000000 27 415720732\n1 1 1 1 1 1 0 0 1 1 1 0 1 0 1 1 0 0 1 1 1 1 0 1 0 1 0", "output": "56 queries, x=415720732" }, { "input": "1000000000 28 946835863\n0 0 1 0 1 1 1 0 1 0 1 1 0 1 0 1 1 0 0 0 1 0 1 0 1 1 0 0", "output": "58 queries, x=946835863" }, { "input": "1000000000 29 124919287\n0 0 1 0 0 0 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 1 1 0 1 0 0", "output": "59 queries, x=124919287" }, { "input": "1000000000 30 202669473\n1 1 0 1 1 1 0 0 1 0 1 0 1 0 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0 0", "output": "58 queries, x=202669473" }, { "input": "1000000000 13 532121080\n1 1 1 0 1 1 0 0 0 0 1 0 1", "output": "42 queries, x=532121080" }, { "input": "1000000000 27 105669924\n0 1 1 1 0 1 0 1 0 0 0 1 0 0 1 1 0 1 1 0 0 1 0 1 1 1 1", "output": "57 queries, x=105669924" }, { "input": "1000000000 11 533994576\n0 0 1 0 1 1 1 1 0 1 0", "output": "38 queries, x=533994576" }, { "input": "1000000000 9 107543421\n1 0 0 1 1 1 1 1 1", "output": "39 queries, x=107543421" }, { "input": "1000000000 23 976059561\n1 0 0 0 0 1 0 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1", "output": "53 queries, x=976059561" }, { "input": "1000000000 7 549608406\n1 1 1 0 1 1 1", "output": "36 queries, x=549608406" }, { "input": "1000000000 21 123157250\n0 1 1 1 1 1 1 0 0 0 1 1 1 1 0 0 0 1 0 0 1", "output": "49 queries, x=123157250" }, { "input": "1000000000 19 696706094\n0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0", "output": "47 queries, x=696706094" }, { "input": "1000000000 3 125030747\n0 0 0", "output": "33 queries, x=125030747" }, { "input": "1000000000 17 993546887\n1 0 1 0 1 1 1 1 1 1 0 1 0 1 1 1 1", "output": "46 queries, x=993546887" }, { "input": "1000000000 15 567095731\n1 1 1 0 0 1 1 1 0 1 0 0 1 0 0", "output": "45 queries, x=567095731" }, { "input": "1000000000 29 140644576\n1 1 1 1 1 1 0 1 0 0 0 1 0 1 0 1 1 1 0 1 1 1 0 1 1 0 1 0 0", "output": "58 queries, x=140644576" }, { "input": "1000000000 13 714193420\n0 1 0 0 0 1 0 0 0 0 1 1 1", "output": "43 queries, x=714193420" }, { "input": "1000000000 27 142518072\n0 0 0 1 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 0 1 0 0 1 0 0", "output": "52 queries, x=142518072" }, { "input": "1000000000 25 11034213\n0 0 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 1 0 1 0 0", "output": "54 queries, x=11034213" }, { "input": "1000000000 9 584583057\n1 1 1 0 0 1 0 0 0", "output": "35 queries, x=584583057" }, { "input": "1000000000 23 863164606\n1 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0 1 1 1 0 0 1 1", "output": "53 queries, x=863164606" }, { "input": "1000000000 21 731680746\n1 1 0 0 1 1 1 1 1 0 0 1 0 1 1 1 1 0 1 0 1", "output": "51 queries, x=731680746" }, { "input": "1000000000 5 305229590\n0 0 1 1 0", "output": "35 queries, x=305229590" }, { "input": "1000000000 3 28521539\n0 0 1", "output": "31 queries, x=28521539" }, { "input": "1000000000 3 602070383\n0 1 1", "output": "32 queries, x=602070383" }, { "input": "1000000000 2 880651931\n1 1", "output": "30 queries, x=880651931" }, { "input": "1000000000 16 749168072\n1 1 0 0 0 1 0 0 1 1 1 1 1 1 1 0", "output": "46 queries, x=749168072" }, { "input": "1000000000 30 322716916\n1 0 1 1 1 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0 1 1 1 1 0 0", "output": "58 queries, x=322716916" }, { "input": "1000000000 14 191233057\n0 0 1 0 0 1 1 1 1 0 0 0 1 1", "output": "43 queries, x=191233057" }, { "input": "1000000000 30 1\n1 1 0 1 1 0 0 1 0 1 1 0 0 1 1 1 0 0 1 1 0 0 0 0 0 1 0 0 0 0", "output": "1 queries, x=1" }, { "input": "1000000000 30 1\n1 0 1 1 1 1 1 1 0 1 0 0 1 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1", "output": "1 queries, x=1" }, { "input": "1000000000 30 1\n1 0 1 0 0 0 0 1 1 0 0 1 1 0 1 1 1 0 1 0 1 1 1 0 0 0 1 0 1 1", "output": "1 queries, x=1" }, { "input": "1000000000 30 1\n1 0 1 0 0 0 1 1 1 0 1 1 1 1 0 0 0 0 0 1 0 1 0 1 1 0 0 1 1 1", "output": "1 queries, x=1" }, { "input": "1000000000 30 1\n1 1 0 1 1 1 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0", "output": "1 queries, x=1" }, { "input": "1000000000 30 1000000000\n1 1 1 0 0 0 1 1 1 1 0 1 0 0 0 1 1 0 1 1 0 0 0 1 0 0 0 0 1 0", "output": "60 queries, x=1000000000" }, { "input": "1000000000 30 1000000000\n1 1 1 0 0 1 1 1 0 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 1 0 1 1 1 0", "output": "60 queries, x=1000000000" }, { "input": "1000000000 30 1000000000\n0 0 1 1 1 0 0 1 0 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 0 0 1 0 1 1", "output": "60 queries, x=1000000000" }, { "input": "1000000000 30 1000000000\n0 0 0 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 1 0 0 1 0 0 1 0 0 1 0 1", "output": "60 queries, x=1000000000" }, { "input": "1000000000 30 1000000000\n0 0 0 1 1 1 0 1 1 0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 0 1 1 0 0 1", "output": "60 queries, x=1000000000" }, { "input": "1 30 1\n1 1 1 0 1 0 0 0 0 1 1 0 1 0 0 1 0 1 0 1 0 1 0 1 1 0 0 0 1 1", "output": "1 queries, x=1" }, { "input": "1 30 1\n1 1 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 1 0 0 1 0 1 0 0 0 1 1 0 0", "output": "1 queries, x=1" }, { "input": "1 30 1\n1 0 0 0 0 1 0 0 1 0 0 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 0 0 0", "output": "1 queries, x=1" }, { "input": "1 30 1\n1 0 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 0 0 0 0 1 0 1 0 0", "output": "1 queries, x=1" }, { "input": "1 30 1\n1 0 1 1 1 1 1 0 0 0 1 1 0 1 0 1 1 0 0 0 0 0 1 1 1 1 1 0 1 1", "output": "1 queries, x=1" }, { "input": "2 1 2\n1", "output": "2 queries, x=2" }, { "input": "1000000000 1 1000000000\n1", "output": "31 queries, x=1000000000" }, { "input": "10000 1 10000\n1", "output": "15 queries, x=10000" }, { "input": "1000000000 1 999999999\n1", "output": "30 queries, x=999999999" }, { "input": "100000 2 15\n1 0", "output": "19 queries, x=15" }, { "input": "200000 1 110000\n1", "output": "17 queries, x=110000" }, { "input": "123456789 1 42\n1", "output": "27 queries, x=42" }, { "input": "1000000000 1 9\n1", "output": "30 queries, x=9" }, { "input": "200000 2 100002\n1 0", "output": "19 queries, x=100002" }, { "input": "1000000000 3 234567890\n0 1 0", "output": "31 queries, x=234567890" }, { "input": "1000000000 5 321732193\n1 1 0 1 0", "output": "35 queries, x=321732193" }, { "input": "1000000000 1 804289384\n1", "output": "27 queries, x=804289384" }, { "input": "1000000000 2 999999998\n1 0", "output": "32 queries, x=999999998" }, { "input": "1000000000 5 384618761\n0 1 1 0 1", "output": "33 queries, x=384618761" }, { "input": "100000000 1 100\n0", "output": "28 queries, x=100" }, { "input": "1000000000 1 804289384\n0", "output": "27 queries, x=804289384" }, { "input": "100000000 1 100000000\n1", "output": "28 queries, x=100000000" }, { "input": "40 1 4\n0", "output": "6 queries, x=4" }, { "input": "1000000000 2 999999998\n0 1", "output": "32 queries, x=999999998" }, { "input": "1000000000 1 1000000000\n0", "output": "31 queries, x=1000000000" }, { "input": "1000000000 2 255555555\n1 0", "output": "31 queries, x=255555555" }, { "input": "1000000000 2 1000000000\n0 1", "output": "32 queries, x=1000000000" }, { "input": "1000000000 1 999999999\n0", "output": "30 queries, x=999999999" }, { "input": "1000000000 2 888888888\n0 1", "output": "31 queries, x=888888888" }, { "input": "1000000000 1 77000000\n1", "output": "31 queries, x=77000000" }, { "input": "1000000000 1 123456789\n1", "output": "27 queries, x=123456789" }, { "input": "10000 1 228\n0", "output": "14 queries, x=228" }, { "input": "1000000000 1 12345\n1", "output": "31 queries, x=12345" }, { "input": "1000000000 1 77000000\n0", "output": "31 queries, x=77000000" }, { "input": "1000000000 1 23333\n0", "output": "31 queries, x=23333" }, { "input": "1000000000 4 100\n0 1 0 1", "output": "34 queries, x=100" }, { "input": "100000000 1 200\n1", "output": "27 queries, x=200" }, { "input": "1000000000 3 5\n0 1 0", "output": "33 queries, x=5" }, { "input": "1000000000 12 2\n1 1 1 1 1 1 0 0 1 1 1 1", "output": "41 queries, x=2" }, { "input": "1000000000 1 5\n0", "output": "31 queries, x=5" }, { "input": "100000 2 99999\n0 0", "output": "18 queries, x=99999" }, { "input": "100000 2 2\n0 1", "output": "18 queries, x=2" }, { "input": "1000000 1 91923\n0", "output": "21 queries, x=91923" }, { "input": "1000000 2 1235\n0 1", "output": "22 queries, x=1235" }, { "input": "1000000000 1 5\n1", "output": "31 queries, x=5" }, { "input": "100000000 2 1234567\n0 1", "output": "28 queries, x=1234567" }, { "input": "1000000000 1 1\n1", "output": "1 queries, x=1" }, { "input": "1000000000 4 999999999\n1 0 0 1", "output": "33 queries, x=999999999" }, { "input": "1000000000 4 1000000000\n1 0 1 0", "output": "34 queries, x=1000000000" }, { "input": "1000000000 4 1\n1 0 1 0", "output": "1 queries, x=1" }, { "input": "1000000000 5 500\n1 1 0 0 1", "output": "34 queries, x=500" }, { "input": "1000 1 13\n1", "output": "11 queries, x=13" }, { "input": "999999999 2 123456789\n1 0", "output": "31 queries, x=123456789" }, { "input": "1000000000 5 1000000000\n1 1 1 1 1", "output": "35 queries, x=1000000000" }, { "input": "1000000000 3 123456789\n1 0 1", "output": "29 queries, x=123456789" }, { "input": "1000000000 1 123\n1", "output": "29 queries, x=123" }, { "input": "10 1 9\n1", "output": "4 queries, x=9" }, { "input": "1000000000 1 1\n0", "output": "1 queries, x=1" }, { "input": "100000000 2 12345678\n0 1", "output": "28 queries, x=12345678" }, { "input": "1000000000 30 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", "output": "60 queries, x=1000000000" }, { "input": "805306369 30 805306369\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", "output": "60 queries, x=805306369" }, { "input": "1000000000 2 123\n0 1", "output": "30 queries, x=123" }, { "input": "1000000000 1 9\n0", "output": "30 queries, x=9" }, { "input": "100000 2 90000\n1 0", "output": "18 queries, x=90000" }, { "input": "1000000000 2 123456789\n1 0", "output": "28 queries, x=123456789" }, { "input": "1000000000 2 804289384\n1 0", "output": "28 queries, x=804289384" }, { "input": "1000000000 30 999999970\n1 0 0 1 1 1 1 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 1 0 0 0 1", "output": "60 queries, x=999999970" } ]
171
102,400
3
2,007
893
Rumor
[ "dfs and similar", "graphs", "greedy" ]
null
null
Vova promised himself that he would never play computer games... But recently Firestorm β€” a well-known game developing company β€” published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it. Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it. Vova knows that there are *n* characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; *i*-th character wants *c**i* gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on. The quest is finished when all *n* characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest? Take a look at the notes if you think you haven't understood the problem completely.
The first line contains two integer numbers *n* and *m* (1<=≀<=*n*<=≀<=105,<=0<=≀<=*m*<=≀<=105) β€” the number of characters in Overcity and the number of pairs of friends. The second line contains *n* integer numbers *c**i* (0<=≀<=*c**i*<=≀<=109) β€” the amount of gold *i*-th character asks to start spreading the rumor. Then *m* lines follow, each containing a pair of numbers (*x**i*,<=*y**i*) which represent that characters *x**i* and *y**i* are friends (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*, *x**i*<=β‰ <=*y**i*). It is guaranteed that each pair is listed at most once.
Print one number β€” the minimum amount of gold Vova has to spend in order to finish the quest.
[ "5 2\n2 5 3 4 8\n1 4\n4 5\n", "10 0\n1 2 3 4 5 6 7 8 9 10\n", "10 5\n1 6 2 7 3 8 4 9 5 10\n1 2\n3 4\n5 6\n7 8\n9 10\n" ]
[ "10\n", "55\n", "15\n" ]
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor. In the second example Vova has to bribe everyone. In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.
[ { "input": "5 2\n2 5 3 4 8\n1 4\n4 5", "output": "10" }, { "input": "10 0\n1 2 3 4 5 6 7 8 9 10", "output": "55" }, { "input": "10 5\n1 6 2 7 3 8 4 9 5 10\n1 2\n3 4\n5 6\n7 8\n9 10", "output": "15" }, { "input": "1 0\n0", "output": "0" }, { "input": "1 0\n1000000000", "output": "1000000000" }, { "input": "2 0\n0 0", "output": "0" }, { "input": "2 0\n1000000000 0", "output": "1000000000" }, { "input": "2 0\n0 1000000000", "output": "1000000000" }, { "input": "2 0\n1000000000 1000000000", "output": "2000000000" }, { "input": "2 1\n0 0\n1 2", "output": "0" } ]
2,000
10,956,800
0
2,009
285
Find Marble
[ "implementation" ]
null
null
Petya and Vasya are playing a game. Petya's got *n* non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to *n* from left to right. Note that the positions are indexed but the glasses are not. First Petya puts a marble under the glass in position *s*. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position *p*1, the glass from the second position to position *p*2 and so on. That is, a glass goes from position *i* to position *p**i*. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in. After all shuffling operations Petya shows Vasya that the ball has moved to position *t*. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position *s* to position *t*.
The first line contains three integers: *n*,<=*s*,<=*t* (1<=≀<=*n*<=≀<=105;Β 1<=≀<=*s*,<=*t*<=≀<=*n*) β€” the number of glasses, the ball's initial and final position. The second line contains *n* space-separated integers: *p*1,<=*p*2,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=*n*) β€” the shuffling operation parameters. It is guaranteed that all *p**i*'s are distinct. Note that *s* can equal *t*.
If the marble can move from position *s* to position *t*, then print on a single line a non-negative integer β€” the minimum number of shuffling operations, needed to get the marble to position *t*. If it is impossible, print number -1.
[ "4 2 1\n2 3 4 1\n", "4 3 3\n4 1 3 2\n", "4 3 4\n1 2 3 4\n", "3 1 3\n2 1 3\n" ]
[ "3\n", "0\n", "-1\n", "-1\n" ]
none
[ { "input": "4 2 1\n2 3 4 1", "output": "3" }, { "input": "4 3 3\n4 1 3 2", "output": "0" }, { "input": "4 3 4\n1 2 3 4", "output": "-1" }, { "input": "3 1 3\n2 1 3", "output": "-1" }, { "input": "1 1 1\n1", "output": "0" }, { "input": "10 6 7\n10 7 8 1 5 6 2 9 4 3", "output": "-1" }, { "input": "10 3 6\n5 6 7 3 8 4 2 1 10 9", "output": "3" }, { "input": "10 10 4\n4 2 6 9 5 3 8 1 10 7", "output": "4" }, { "input": "100 90 57\n19 55 91 50 31 23 60 84 38 1 22 51 27 76 28 98 11 44 61 63 15 93 52 3 66 16 53 36 18 62 35 85 78 37 73 64 87 74 46 26 82 69 49 33 83 89 56 67 71 25 39 94 96 17 21 6 47 68 34 42 57 81 13 10 54 2 48 80 20 77 4 5 59 30 90 95 45 75 8 88 24 41 40 14 97 32 7 9 65 70 100 99 72 58 92 29 79 12 86 43", "output": "-1" }, { "input": "100 11 20\n80 25 49 55 22 98 35 59 88 14 91 20 68 66 53 50 77 45 82 63 96 93 85 46 37 74 84 9 7 95 41 86 23 36 33 27 81 39 18 13 12 92 24 71 3 48 83 61 31 87 28 79 75 38 11 21 29 69 44 100 72 62 32 43 30 16 47 56 89 60 42 17 26 70 94 99 4 6 2 73 8 52 65 1 15 90 67 51 78 10 5 76 57 54 34 58 19 64 40 97", "output": "26" }, { "input": "100 84 83\n30 67 53 89 94 54 92 17 26 57 15 5 74 85 10 61 18 70 91 75 14 11 93 41 25 78 88 81 20 51 35 4 62 1 97 39 68 52 47 77 64 3 2 72 60 80 8 83 65 98 21 22 45 7 58 31 43 38 90 99 49 87 55 36 29 6 37 23 66 76 59 79 40 86 63 44 82 32 48 16 50 100 28 96 46 12 27 13 24 9 19 84 73 69 71 42 56 33 34 95", "output": "71" }, { "input": "100 6 93\n74 62 67 81 40 85 35 42 59 72 80 28 79 41 16 19 33 63 13 10 69 76 70 93 49 84 89 94 8 37 11 90 26 52 47 7 36 95 86 75 56 15 61 99 88 12 83 21 20 3 100 17 32 82 6 5 43 25 66 68 73 78 18 77 92 27 23 2 4 39 60 48 22 24 14 97 29 34 54 64 71 57 87 38 9 50 30 53 51 45 44 31 58 91 98 65 55 1 46 96", "output": "-1" }, { "input": "100 27 56\n58 18 50 41 33 37 14 87 77 73 61 53 15 8 70 68 45 96 54 78 39 67 51 60 80 12 93 99 20 92 17 79 4 13 62 91 69 29 49 36 98 34 90 35 84 64 38 83 28 89 97 94 9 16 26 48 10 57 23 75 27 88 44 21 72 76 30 43 32 2 71 24 100 1 31 81 42 40 47 55 86 85 66 5 52 22 95 74 11 19 7 82 6 25 56 63 65 59 46 3", "output": "20" }, { "input": "87 42 49\n45 55 24 44 56 72 74 23 4 7 37 67 22 6 58 76 40 36 3 20 26 87 64 75 49 70 62 42 31 1 80 33 25 59 78 27 32 2 41 61 66 28 19 85 15 69 52 77 50 14 16 34 18 43 73 83 11 39 29 9 35 13 81 54 79 21 60 46 71 57 12 17 5 47 38 30 10 84 53 63 68 8 51 65 48 86 82", "output": "-1" }, { "input": "2 1 2\n1 2", "output": "-1" }, { "input": "2 1 2\n2 1", "output": "1" }, { "input": "2 2 2\n1 2", "output": "0" }, { "input": "2 2 2\n2 1", "output": "0" }, { "input": "2 1 1\n2 1", "output": "0" } ]
434
14,028,800
3
2,010
508
Anton and currency you all know
[ "greedy", "math", "strings" ]
null
null
Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that to simplify the calculations, its fractional part was neglected and the exchange rate is now assumed to be an integer. Reliable sources have informed the financier Anton of some information about the exchange rate of currency you all know against the burle for tomorrow. Now Anton knows that tomorrow the exchange rate will be an even number, which can be obtained from the present rate by swapping exactly two distinct digits in it. Of all the possible values that meet these conditions, the exchange rate for tomorrow will be the maximum possible. It is guaranteed that today the exchange rate is an odd positive integer *n*. Help Anton to determine the exchange rate of currency you all know for tomorrow!
The first line contains an odd positive integer *n*Β β€” the exchange rate of currency you all know for today. The length of number *n*'s representation is within range from 2 to 105, inclusive. The representation of *n* doesn't contain any leading zeroes.
If the information about tomorrow's exchange rate is inconsistent, that is, there is no integer that meets the condition, print <=-<=1. Otherwise, print the exchange rate of currency you all know against the burle for tomorrow. This should be the maximum possible number of those that are even and that are obtained from today's exchange rate by swapping exactly two digits. Exchange rate representation should not contain leading zeroes.
[ "527\n", "4573\n", "1357997531\n" ]
[ "572\n", "3574\n", "-1\n" ]
none
[ { "input": "527", "output": "572" }, { "input": "4573", "output": "3574" }, { "input": "1357997531", "output": "-1" }, { "input": "444443", "output": "444434" }, { "input": "22227", "output": "72222" }, { "input": "24683", "output": "34682" }, { "input": "11", "output": "-1" }, { "input": "1435678543", "output": "1435678534" }, { "input": "4250769", "output": "9250764" }, { "input": "4052769", "output": "9052764" }, { "input": "5685341", "output": "5685314" }, { "input": "1111111111111111231", "output": "1111111111111111132" }, { "input": "333333332379", "output": "333333339372" }, { "input": "85", "output": "58" }, { "input": "7700016673", "output": "7730016670" }, { "input": "35451519805848712272404365322858764249299938505103", "output": "35451519835848712272404365322858764249299938505100" }, { "input": "4314752277691991627730686134692292422155753465948025897701703862445837045929984759093775762579123919", "output": "9314752277691991627730686134692292422155753465948025897701703862445837045929984759093775762579123914" }, { "input": "21", "output": "12" }, { "input": "101", "output": "110" }, { "input": "503147", "output": "573140" }, { "input": "333333333333333333333", "output": "-1" }, { "input": "55555555555555555555555", "output": "-1" }, { "input": "99", "output": "-1" }, { "input": "23759", "output": "93752" }, { "input": "235749", "output": "935742" }, { "input": "435729", "output": "935724" }, { "input": "8623", "output": "8632" }, { "input": "109", "output": "190" }, { "input": "20000000000000000000001", "output": "21000000000000000000000" }, { "input": "1001", "output": "1100" } ]
78
0
0
2,011
0
none
[ "none" ]
null
null
A tree is a connected undirected graph consisting of *n* vertices and *n*<=<=-<=<=1 edges. Vertices are numbered 1 through *n*. Limak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the treeΒ β€” he can tell you only three values *n*, *d* and *h*: - The tree had exactly *n* vertices. - The tree had diameter *d*. In other words, *d* was the biggest distance between two vertices. - Limak also remembers that he once rooted the tree in vertex 1 and after that its height was *h*. In other words, *h* was the biggest distance between vertex 1 and some other vertex. The distance between two vertices of the tree is the number of edges on the simple path between them. Help Limak to restore his tree. Check whether there exists a tree satisfying the given conditions. Find any such tree and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree – in this case print "-1".
The first line contains three integers *n*, *d* and *h* (2<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*h*<=≀<=*d*<=≀<=*n*<=-<=1)Β β€” the number of vertices, diameter, and height after rooting in vertex 1, respectively.
If there is no tree matching what Limak remembers, print the only line with "-1" (without the quotes). Otherwise, describe any tree matching Limak's description. Print *n*<=-<=1 lines, each with two space-separated integers – indices of vertices connected by an edge. If there are many valid trees, print any of them. You can print edges in any order.
[ "5 3 2\n", "8 5 2\n", "8 4 2\n" ]
[ "1 2\n1 3\n3 4\n3 5", "-1\n", "4 8\n5 7\n2 3\n8 1\n2 1\n5 6\n1 5\n" ]
Below you can see trees printed to the output in the first sample and the third sample.
[ { "input": "5 3 2", "output": "1 2\n2 3\n1 4\n5 1" }, { "input": "8 5 2", "output": "-1" }, { "input": "8 4 2", "output": "4 8\n5 7\n2 3\n8 1\n2 1\n5 6\n1 5" }, { "input": "2 1 1", "output": "1 2" }, { "input": "10 3 3", "output": "1 2\n2 3\n3 4\n5 2\n6 2\n7 2\n8 2\n9 2\n10 2" }, { "input": "15 6 4", "output": "1 2\n2 3\n3 4\n4 5\n1 6\n6 7\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1" }, { "input": "16 15 14", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n1 16" }, { "input": "1000 51 25", "output": "-1" }, { "input": "100000 10 7", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n1 9\n9 10\n10 11\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n87 1\n88..." }, { "input": "3 1 1", "output": "-1" }, { "input": "3 2 1", "output": "1 2\n1 3" }, { "input": "3 2 2", "output": "1 2\n2 3" }, { "input": "4 1 1", "output": "-1" }, { "input": "4 2 1", "output": "1 2\n1 3\n4 1" }, { "input": "4 2 2", "output": "1 2\n2 3\n4 2" }, { "input": "4 3 1", "output": "-1" }, { "input": "4 3 2", "output": "1 2\n2 3\n1 4" }, { "input": "4 3 3", "output": "1 2\n2 3\n3 4" }, { "input": "8 5 3", "output": "1 2\n2 3\n3 4\n1 5\n5 6\n7 1\n8 1" }, { "input": "20 19 19", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20" }, { "input": "30 14 14", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n16 2\n17 2\n18 2\n19 2\n20 2\n21 2\n22 2\n23 2\n24 2\n25 2\n26 2\n27 2\n28 2\n29 2\n30 2" }, { "input": "33 5 3", "output": "1 2\n2 3\n3 4\n1 5\n5 6\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1" }, { "input": "5432 200 100", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "5433 200 99", "output": "-1" }, { "input": "99999 1 1", "output": "-1" }, { "input": "99999 2 1", "output": "1 2\n1 3\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n87 1\n88 ..." }, { "input": "99999 7 4", "output": "1 2\n2 3\n3 4\n4 5\n1 6\n6 7\n7 8\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n87 1\n88 ..." }, { "input": "9999 7 3", "output": "-1" }, { "input": "100000 1 1", "output": "-1" }, { "input": "100000 2 1", "output": "1 2\n1 3\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n87 1\n88 ..." }, { "input": "100000 2 2", "output": "1 2\n2 3\n4 2\n5 2\n6 2\n7 2\n8 2\n9 2\n10 2\n11 2\n12 2\n13 2\n14 2\n15 2\n16 2\n17 2\n18 2\n19 2\n20 2\n21 2\n22 2\n23 2\n24 2\n25 2\n26 2\n27 2\n28 2\n29 2\n30 2\n31 2\n32 2\n33 2\n34 2\n35 2\n36 2\n37 2\n38 2\n39 2\n40 2\n41 2\n42 2\n43 2\n44 2\n45 2\n46 2\n47 2\n48 2\n49 2\n50 2\n51 2\n52 2\n53 2\n54 2\n55 2\n56 2\n57 2\n58 2\n59 2\n60 2\n61 2\n62 2\n63 2\n64 2\n65 2\n66 2\n67 2\n68 2\n69 2\n70 2\n71 2\n72 2\n73 2\n74 2\n75 2\n76 2\n77 2\n78 2\n79 2\n80 2\n81 2\n82 2\n83 2\n84 2\n85 2\n86 2\n87 2\n88 ..." }, { "input": "100000 3 1", "output": "-1" }, { "input": "100000 10 5", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n1 7\n7 8\n8 9\n9 10\n10 11\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n87 1\n88..." }, { "input": "100000 10 6", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n1 8\n8 9\n9 10\n10 11\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n87 1\n88..." }, { "input": "100000 10 9", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n1 11\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n87 1\n88 ..." }, { "input": "100000 10 10", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n12 2\n13 2\n14 2\n15 2\n16 2\n17 2\n18 2\n19 2\n20 2\n21 2\n22 2\n23 2\n24 2\n25 2\n26 2\n27 2\n28 2\n29 2\n30 2\n31 2\n32 2\n33 2\n34 2\n35 2\n36 2\n37 2\n38 2\n39 2\n40 2\n41 2\n42 2\n43 2\n44 2\n45 2\n46 2\n47 2\n48 2\n49 2\n50 2\n51 2\n52 2\n53 2\n54 2\n55 2\n56 2\n57 2\n58 2\n59 2\n60 2\n61 2\n62 2\n63 2\n64 2\n65 2\n66 2\n67 2\n68 2\n69 2\n70 2\n71 2\n72 2\n73 2\n74 2\n75 2\n76 2\n77 2\n78 2\n79 2\n80 2\n81 2\n82 2\n83 2\n84 2\n85 2\n86 2\n87 2\n88..." }, { "input": "100000 99900 78900", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99998 1", "output": "-1" }, { "input": "100000 99998 49999", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99998 50000", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99998 69001", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99998 99055", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99998 99998", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99999 1", "output": "-1" }, { "input": "100000 99999 49999", "output": "-1" }, { "input": "100000 99999 50000", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99999 50001", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99999 77777", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99999 99998", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "100000 99999 99999", "output": "1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 ..." }, { "input": "3 1 1", "output": "-1" }, { "input": "5 1 1", "output": "-1" }, { "input": "10 1 1", "output": "-1" }, { "input": "3 2 1", "output": "1 2\n1 3" }, { "input": "8 1 1", "output": "-1" }, { "input": "4 1 1", "output": "-1" }, { "input": "6 1 1", "output": "-1" }, { "input": "20 1 1", "output": "-1" }, { "input": "5 2 1", "output": "1 2\n1 3\n4 1\n5 1" }, { "input": "100 1 1", "output": "-1" }, { "input": "10 2 1", "output": "1 2\n1 3\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1" }, { "input": "100 2 1", "output": "1 2\n1 3\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n87 1\n88 ..." }, { "input": "47 1 1", "output": "-1" }, { "input": "7 1 1", "output": "-1" }, { "input": "4 2 1", "output": "1 2\n1 3\n4 1" }, { "input": "5 2 2", "output": "1 2\n2 3\n4 2\n5 2" }, { "input": "8 2 1", "output": "1 2\n1 3\n4 1\n5 1\n6 1\n7 1\n8 1" }, { "input": "1000 1 1", "output": "-1" }, { "input": "11 1 1", "output": "-1" }, { "input": "15 2 1", "output": "1 2\n1 3\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1" }, { "input": "3 2 2", "output": "1 2\n2 3" }, { "input": "8 2 2", "output": "1 2\n2 3\n4 2\n5 2\n6 2\n7 2\n8 2" } ]
61
4,608,000
0
2,016
892
Greed
[ "greedy", "implementation" ]
null
null
Jafar has *n* cans of cola. Each can is described by two integers: remaining volume of cola *a**i* and can's capacity *b**i* (*a**i* <=≀<= *b**i*). Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
The first line of the input contains one integer *n* (2<=≀<=*n*<=≀<=100<=000)Β β€” number of cola cans. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109) β€” volume of remaining cola in cans. The third line contains *n* space-separated integers that *b*1,<=*b*2,<=...,<=*b**n* (*a**i*<=≀<=*b**i*<=≀<=109) β€” capacities of the cans.
Print "YES" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print "NO" (without quotes). You can print each letter in any case (upper or lower).
[ "2\n3 5\n3 6\n", "3\n6 8 9\n6 10 12\n", "5\n0 0 5 0 0\n1 1 8 10 5\n", "4\n4 1 0 3\n5 2 2 3\n" ]
[ "YES\n", "NO\n", "YES\n", "YES\n" ]
In the first sample, there are already 2 cans, so the answer is "YES".
[ { "input": "2\n3 5\n3 6", "output": "YES" }, { "input": "3\n6 8 9\n6 10 12", "output": "NO" }, { "input": "5\n0 0 5 0 0\n1 1 8 10 5", "output": "YES" }, { "input": "4\n4 1 0 3\n5 2 2 3", "output": "YES" }, { "input": "10\n9 10 24 11 1 7 8 3 28 14\n86 20 34 11 22 94 8 16 73 85", "output": "YES" }, { "input": "4\n25 35 7 31\n70 37 43 35", "output": "YES" }, { "input": "10\n15 26 15 14 14 39 40 4 25 39\n27 72 16 44 69 48 53 17 63 42", "output": "NO" }, { "input": "5\n22 5 19 16 32\n26 10 43 38 37", "output": "NO" }, { "input": "5\n32 4 22 40 26\n39 20 36 98 44", "output": "YES" }, { "input": "6\n18 25 3 10 13 37\n38 73 19 35 24 37", "output": "YES" }, { "input": "2\n2 2\n2 2", "output": "YES" }, { "input": "2\n2 5\n2 5", "output": "YES" }, { "input": "2\n1000 1008\n10000 2352", "output": "YES" }, { "input": "5\n1 2 3 4 5\n1 2 3 4 11", "output": "YES" }, { "input": "4\n1 0 0 0\n2 0 0 0", "output": "YES" }, { "input": "2\n0 0\n1 2", "output": "YES" }, { "input": "3\n9 13 4\n10 14 5", "output": "NO" }, { "input": "2\n0 0\n1 1", "output": "YES" }, { "input": "5\n1 1 2 3 1\n1 1 2 3 4", "output": "NO" }, { "input": "2\n0 0\n0 0", "output": "YES" }, { "input": "3\n5 1 1\n5 5 5", "output": "YES" } ]
156
11,366,400
-1
2,017
621
Wet Shark and Odd and Even
[ "implementation" ]
null
null
Today, Wet Shark is given *n* integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark. Note, that if Wet Shark uses no integers from the *n* integers, the sum is an even integer 0.
The first line of the input contains one integer, *n* (1<=≀<=*n*<=≀<=100<=000). The next line contains *n* space separated integers given to Wet Shark. Each of these integers is in range from 1 to 109, inclusive.
Print the maximum possible even sum that can be obtained if we use some of the given integers.
[ "3\n1 2 3\n", "5\n999999999 999999999 999999999 999999999 999999999\n" ]
[ "6", "3999999996" ]
In the first sample, we can simply take all three integers for a total sum of 6. In the second sample Wet Shark should take any four out of five integers 999 999 999.
[ { "input": "3\n1 2 3", "output": "6" }, { "input": "5\n999999999 999999999 999999999 999999999 999999999", "output": "3999999996" }, { "input": "1\n1", "output": "0" }, { "input": "15\n39 52 88 78 46 95 84 98 55 3 68 42 6 18 98", "output": "870" }, { "input": "15\n59 96 34 48 8 72 67 90 15 85 7 90 97 47 25", "output": "840" }, { "input": "15\n87 37 91 29 58 45 51 74 70 71 47 38 91 89 44", "output": "922" }, { "input": "15\n11 81 49 7 11 14 30 67 29 50 90 81 77 18 59", "output": "674" }, { "input": "15\n39 21 95 89 73 90 9 55 85 32 30 21 68 59 82", "output": "848" }, { "input": "15\n59 70 48 54 26 67 84 39 40 18 77 69 70 88 93", "output": "902" }, { "input": "15\n87 22 98 32 88 36 72 31 100 97 17 16 60 22 20", "output": "798" }, { "input": "15\n15 63 51 13 37 9 43 19 55 79 57 60 50 59 31", "output": "632" }, { "input": "1\n4", "output": "4" }, { "input": "2\n1 4", "output": "4" }, { "input": "3\n1 2 4", "output": "6" }, { "input": "2\n9 3", "output": "12" }, { "input": "2\n1000000000 1001", "output": "1000000000" }, { "input": "3\n1 8 4", "output": "12" }, { "input": "3\n7 4 4", "output": "8" }, { "input": "5\n2 3 4 5 3", "output": "14" }, { "input": "2\n4 5", "output": "4" }, { "input": "3\n2 4 5", "output": "6" }, { "input": "3\n2 2 3", "output": "4" }, { "input": "2\n2 3", "output": "2" }, { "input": "4\n2 3 7 7", "output": "16" }, { "input": "2\n999999999 2", "output": "2" }, { "input": "2\n2 5", "output": "2" }, { "input": "3\n5 3 1", "output": "8" }, { "input": "4\n3 2 5 7", "output": "14" } ]
217
8,499,200
3
2,021
472
Design Tutorial: Learn from Life
[]
null
null
One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task. Let's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following way. We have *n* people standing on the first floor, the *i*-th person wants to go to the *f**i*-th floor. Unfortunately, there is only one elevator and its capacity equal to *k* (that is at most *k* people can use it simultaneously). Initially the elevator is located on the first floor. The elevator needs |*a*<=-<=*b*| seconds to move from the *a*-th floor to the *b*-th floor (we don't count the time the people need to get on and off the elevator). What is the minimal number of seconds that is needed to transport all the people to the corresponding floors and then return the elevator to the first floor?
The first line contains two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=2000) β€” the number of people and the maximal capacity of the elevator. The next line contains *n* integers: *f*1,<=*f*2,<=...,<=*f**n* (2<=≀<=*f**i*<=≀<=2000), where *f**i* denotes the target floor of the *i*-th person.
Output a single integer β€” the minimal time needed to achieve the goal.
[ "3 2\n2 3 4\n", "4 2\n50 100 50 100\n", "10 3\n2 2 2 2 2 2 2 2 2 2\n" ]
[ "8\n", "296\n", "8\n" ]
In first sample, an optimal solution is: 1. The elevator takes up person #1 and person #2. 1. It goes to the 2nd floor. 1. Both people go out of the elevator. 1. The elevator goes back to the 1st floor. 1. Then the elevator takes up person #3. 1. And it goes to the 2nd floor. 1. It picks up person #2. 1. Then it goes to the 3rd floor. 1. Person #2 goes out. 1. Then it goes to the 4th floor, where person #3 goes out. 1. The elevator goes back to the 1st floor.
[ { "input": "3 2\n2 3 4", "output": "8" }, { "input": "4 2\n50 100 50 100", "output": "296" }, { "input": "10 3\n2 2 2 2 2 2 2 2 2 2", "output": "8" }, { "input": "1 1\n2", "output": "2" }, { "input": "2 1\n2 2", "output": "4" }, { "input": "2 2\n2 2", "output": "2" }, { "input": "11 4\n2 3 4 5 6 7 8 9 10 11 12", "output": "42" }, { "input": "70 1311\n1146 769 1530 1132 877 1352 1295 1282 1086 914 852 1351 1432 847 1105 776 1278 716 1435 749 1320 869 812 1179 1239 1426 1403 1296 1489 1018 848 1215 1278 1203 1386 1113 812 896 1287 733 1328 1517 964 1317 1487 1322 1281 843 863 1220 790 1121 1442 726 957 1465 974 1314 993 1286 1163 938 902 1197 859 807 718 916 783 1450", "output": "3058" }, { "input": "22 1481\n1555 1499 1615 535 399 795 432 1830 1645 452 278 1173 1501 1779 317 796 1855 1631 651 495 236 1122", "output": "3708" }, { "input": "100 1\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "19800" }, { "input": "100 100\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "198" }, { "input": "2 1\n2000 2000", "output": "7996" }, { "input": "5 3\n2000 2000 2000 2000 1999", "output": "7996" } ]
46
0
3
2,022
888
Buggy Robot
[ "greedy" ]
null
null
Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0,<=0). The robot can process commands. There are four types of commands it can perform: - U β€” move from the cell (*x*,<=*y*) to (*x*,<=*y*<=+<=1); - D β€” move from (*x*,<=*y*) to (*x*,<=*y*<=-<=1); - L β€” move from (*x*,<=*y*) to (*x*<=-<=1,<=*y*); - R β€” move from (*x*,<=*y*) to (*x*<=+<=1,<=*y*). Ivan entered a sequence of *n* commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0,<=0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!
The first line contains one number *n* β€” the length of sequence of commands entered by Ivan (1<=≀<=*n*<=≀<=100). The second line contains the sequence itself β€” a string consisting of *n* characters. Each character can be U, D, L or R.
Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.
[ "4\nLDUR\n", "5\nRRRUU\n", "6\nLLRRRR\n" ]
[ "4\n", "0\n", "4\n" ]
none
[ { "input": "4\nLDUR", "output": "4" }, { "input": "5\nRRRUU", "output": "0" }, { "input": "6\nLLRRRR", "output": "4" }, { "input": "88\nLLUUULRDRRURDDLURRLRDRLLRULRUUDDLLLLRRDDURDURRLDURRLDRRRUULDDLRRRDDRRLUULLURDURUDDDDDLDR", "output": "76" }, { "input": "89\nLDLLLDRDUDURRRRRUDULDDDLLUDLRLRLRLDLDUULRDUDLRRDLUDLURRDDRRDLDUDUUURUUUDRLUDUDLURDLDLLDDU", "output": "80" }, { "input": "90\nRRRDUULLLRDUUDDRLDLRLUDURDRDUUURUURDDRRRURLDDDUUDRLLLULURDRDRURLDRRRRUULDULDDLLLRRLRDLLLLR", "output": "84" }, { "input": "91\nRLDRLRRLLDLULULLURULLRRULUDUULLUDULDUULURUDRUDUURDULDUDDUUUDRRUUDLLRULRULURLDRDLDRURLLLRDDD", "output": "76" }, { "input": "92\nRLRDDLULRLLUURRDDDLDDDLDDUURRRULLRDULDULLLUUULDUDLRLRRDRDRDDULDRLUDRDULDRURUDUULLRDRRLLDRLRR", "output": "86" }, { "input": "93\nRLLURLULRURDDLUURLUDDRDLUURLRDLRRRDUULLRDRRLRLDURRDLLRDDLLLDDDLDRRURLLDRUDULDDRRULRRULRLDRDLR", "output": "84" }, { "input": "94\nRDULDDDLULRDRUDRUUDUUDRRRULDRRUDURUULRDUUDLULLLUDURRDRDLUDRULRRRULUURUDDDDDUDLLRDLDRLLRUUURLUL", "output": "86" }, { "input": "95\nRDLUUULLUURDDRLDLLRRRULRLRDULULRULRUDURLULDDDRLURLDRULDUDUUULLRDDURUULULLDDLDRDRLLLURLRDLLDDDDU", "output": "86" }, { "input": "96\nRDDRLRLLDDULRLRURUDLRLDUDRURLLUUDLLURDLRRUURDRRUDRURLLDLLRDURDURLRLUDURULLLRDUURULUUULRRURRDLURL", "output": "84" }, { "input": "97\nRURDDLRLLRULUDURDLRLLUUDURRLLUDLLLDUDRUULDRUUURURULRDLDRRLLUUUDLLLDDLLLLRLLDUDRRDLLUDLRURUDULRLUR", "output": "82" }, { "input": "98\nRUDURLULLDDLLRDLLRDDLLLLRLDDDDRRRDDRRURLDRLLRUUUDLUUUDDDUDRUURLURUUDUUDRULRRULLRRLRULLULDLUURLULRD", "output": "92" }, { "input": "99\nRRULLDULRRDRULLDUDRUDDDRLLUUDRDDUDURLDDRUUDRRUUURRRURDDLDUURDLRLURRDDLUDDLUDURDRUDDURLURURLRUDRURLD", "output": "86" }, { "input": "100\nUDRLRRLLRRLRRRDDLLDDDLULLDDLURUURUULUDDDRDDLLRDLLUURLRDRLRRLRLLLULDUDDUURRLRDULDRDURRRRRRULDRRDLDRRL", "output": "88" }, { "input": "1\nU", "output": "0" }, { "input": "5\nUUULD", "output": "2" }, { "input": "1\nD", "output": "0" }, { "input": "5\nURLUL", "output": "2" }, { "input": "5\nDDDRU", "output": "2" }, { "input": "2\nLR", "output": "2" }, { "input": "8\nDDRDLDUR", "output": "4" }, { "input": "6\nLLLLUD", "output": "2" }, { "input": "13\nRRRLLLLLLLLLL", "output": "6" } ]
124
0
3
2,025
105
Item World
[ "brute force", "implementation", "sortings" ]
C. Item World
2
256
Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def) and resistance to different types of impact (res). Each item belongs to one class. In this problem we will only consider three of such classes: weapon, armor, orb. Besides, there's a whole new world hidden inside each item. We can increase an item's level travelling to its world. We can also capture the so-called residents in the Item World Residents are the creatures that live inside items. Each resident gives some bonus to the item in which it is currently located. We will only consider residents of types: gladiator (who improves the item's atk), sentry (who improves def) and physician (who improves res). Each item has the size parameter. The parameter limits the maximum number of residents that can live inside an item. We can move residents between items. Within one moment of time we can take some resident from an item and move it to some other item if it has a free place for a new resident. We cannot remove a resident from the items and leave outside β€” any of them should be inside of some item at any moment of time. Laharl has a certain number of items. He wants to move the residents between items so as to equip himself with weapon, armor and a defensive orb. The weapon's atk should be largest possible in the end. Among all equipping patterns containing weapon's maximum atk parameter we should choose the ones where the armor’s def parameter is the largest possible. Among all such equipment patterns we should choose the one where the defensive orb would have the largest possible res parameter. Values of the parameters def and res of weapon, atk and res of armor and atk and def of orb are indifferent for Laharl. Find the optimal equipment pattern Laharl can get.
The first line contains number *n* (3<=≀<=*n*<=≀<=100) β€” representing how many items Laharl has. Then follow *n* lines. Each line contains description of an item. The description has the following form: "*name* *class* *atk* *def* *res* *size*" β€” the item's name, class, basic attack, defense and resistance parameters and its size correspondingly. - *name* and *class* are strings and *atk*, *def*, *res* and *size* are integers. - *name* consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. - *class* can be "weapon", "armor" or "orb". - 0<=≀<=*atk*,<=*def*,<=*res*<=≀<=1000. - 1<=≀<=*size*<=≀<=10. It is guaranteed that Laharl has at least one item of each class. The next line contains an integer *k* (1<=≀<=*k*<=≀<=1000) β€” the number of residents. Then *k* lines follow. Each of them describes a resident. A resident description looks like: "*name* *type* *bonus* *home*" β€” the resident's name, his type, the number of points the resident adds to the item's corresponding parameter and the name of the item which currently contains the resident. - *name*, *type* and *home* are strings and *bonus* is an integer. - *name* consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. - *type* may be "gladiator", "sentry" or "physician". - 1<=≀<=*bonus*<=≀<=100. It is guaranteed that the number of residents in each item does not exceed the item's size. The names of all items and residents are pairwise different. All words and numbers in the input are separated by single spaces.
Print on the first line the name of the weapon in the optimal equipping pattern; then print the number of residents the weapon contains; then print the residents' names. Print on the second and third lines in the same form the names of the armor and defensive orb as well as the residents they contain. Use single spaces for separation. If there are several possible solutions, print any of them.
[ "4\nsword weapon 10 2 3 2\npagstarmor armor 0 15 3 1\niceorb orb 3 2 13 2\nlongbow weapon 9 1 2 1\n5\nmike gladiator 5 longbow\nbobby sentry 6 pagstarmor\npetr gladiator 7 iceorb\nteddy physician 6 sword\nblackjack sentry 8 sword\n", "4\nsword weapon 10 2 3 2\npagstarmor armor 0 15 3 1\niceorb orb 3 2 13 2\nlongbow weapon 9 1 2 1\n6\nmike gladiator 5 longbow\nbobby sentry 6 pagstarmor\npetr gladiator 7 iceorb\nteddy physician 6 sword\nblackjack sentry 8 sword\njoe physician 6 iceorb\n" ]
[ "sword 2 petr mike \npagstarmor 1 blackjack \niceorb 2 teddy bobby \n", "longbow 1 mike \npagstarmor 1 bobby \niceorb 2 petr joe \n" ]
In the second sample we have no free space inside the items, therefore we cannot move the residents between them.
[ { "input": "4\nsword weapon 10 2 3 2\npagstarmor armor 0 15 3 1\niceorb orb 3 2 13 2\nlongbow weapon 9 1 2 1\n5\nmike gladiator 5 longbow\nbobby sentry 6 pagstarmor\npetr gladiator 7 iceorb\nteddy physician 6 sword\nblackjack sentry 8 sword", "output": "sword 2 petr mike \npagstarmor 1 blackjack \niceorb 2 teddy bobby " }, { "input": "4\nsword weapon 10 2 3 2\npagstarmor armor 0 15 3 1\niceorb orb 3 2 13 2\nlongbow weapon 9 1 2 1\n6\nmike gladiator 5 longbow\nbobby sentry 6 pagstarmor\npetr gladiator 7 iceorb\nteddy physician 6 sword\nblackjack sentry 8 sword\njoe physician 6 iceorb", "output": "longbow 1 mike \npagstarmor 1 bobby \niceorb 2 petr joe " }, { "input": "3\nweapon weapon 10 5 2 4\narmor armor 0 20 0 6\norb orb 3 4 25 3\n3\nx gladiator 12 armor\ny sentry 13 orb\nz physician 5 weapon", "output": "weapon 1 x \narmor 1 y \norb 1 z " }, { "input": "6\nc armor 0 13 0 3\na weapon 23 0 0 3\nb weapon 20 0 0 4\ne orb 0 0 13 3\nd armor 0 15 0 4\nf orb 0 0 17 5\n5\nj gladiator 7 a\nh gladiator 3 f\ng gladiator 4 e\ni gladiator 7 a\nk gladiator 1 b", "output": "a 3 i j g \nd 2 h k \nf 0 " }, { "input": "6\nc armor 0 13 0 3\na weapon 23 0 0 3\nb weapon 10 0 0 4\ne orb 0 0 19 3\nd armor 0 15 0 4\nf orb 0 0 17 5\n5\nj gladiator 7 e\nh gladiator 5 f\ng gladiator 4 c\ni gladiator 7 b\nk gladiator 1 d", "output": "a 3 i j h \nd 2 g k \ne 0 " }, { "input": "6\nc armor 0 14 0 3\na weapon 23 0 0 3\nb weapon 21 0 0 4\ne orb 0 0 13 3\nd armor 0 5 0 4\nf orb 0 0 17 5\n5\nj gladiator 7 f\nh gladiator 5 a\ng gladiator 6 c\ni gladiator 7 d\nk gladiator 1 d", "output": "b 4 i j g h \nc 1 k \nf 0 " }, { "input": "5\nxx weapon 15 0 0 2\nyy armor 0 14 0 2\nzz orb 0 0 16 2\npp weapon 1 0 0 5\nqq armor 0 1 0 4\n9\na gladiator 2 pp\nb gladiator 3 pp\nc gladiator 4 pp\nd sentry 1 pp\ne sentry 2 pp\nf sentry 3 qq\ng physician 2 qq\nh physician 3 qq\ni physician 3 qq", "output": "xx 2 c b \nyy 2 f e \nzz 2 i h " }, { "input": "5\npixiebow weapon 10 0 7 2\nlance weapon 12 4 2 1\nbushido armor 0 14 1 4\nstarorb orb 2 3 16 3\nmoonorb orb 3 4 8 1\n8\nste gladiator 10 moonorb\nphi gladiator 8 starorb\nhjk gladiator 5 starorb\npoi gladiator 7 starorb\njor gladiator 4 lance\npui gladiator 6 bushido\nzea gladiator 1 bushido\nqwe gladiator 2 pixiebow", "output": "pixiebow 2 ste phi \nbushido 4 poi pui hjk jor \nstarorb 2 qwe zea " }, { "input": "5\npixiebow weapon 10 0 7 2\nlance weapon 12 4 2 1\nbushido armor 0 14 1 4\nstarorb orb 2 3 16 3\nmoonorb orb 3 4 8 1\n11\nste gladiator 10 moonorb\nphi gladiator 8 starorb\nhjk gladiator 5 starorb\npoi gladiator 7 starorb\njor gladiator 4 lance\npui gladiator 6 bushido\nzea gladiator 1 bushido\nqwe gladiator 2 pixiebow\nkkk physician 20 bushido\nlkh sentry 4 pixiebow\noop sentry 8 bushido", "output": "lance 1 jor \nbushido 4 pui zea kkk oop \nstarorb 3 phi hjk poi " }, { "input": "3\nhcyc weapon 646 755 45 5\nhfh armor 556 875 434 6\njkob orb 654 0 65 7\n1\njhcytccc sentry 76 jkob", "output": "hcyc 0 \nhfh 1 jhcytccc \njkob 0 " }, { "input": "5\naxgovq orb 75 830 793 3\nzeckskde weapon 316 351 917 2\nnrtbk armor 540 178 332 2\nnhjodogdd armor 880 453 186 2\ndxrgvjhvhg weapon 961 616 561 3\n7\nzvi gladiator 16 axgovq\nrq gladiator 52 axgovq\njlr physician 69 zeckskde\njackbeadx sentry 90 zeckskde\nvuhpq gladiator 23 nrtbk\nvfhyjtps physician 88 nhjodogdd\nrb gladiator 90 nhjodogdd", "output": "dxrgvjhvhg 3 rb rq vuhpq \nnhjodogdd 2 jackbeadx zvi \naxgovq 2 vfhyjtps jlr " }, { "input": "5\nhs orb 830 875 879 3\nfudflb weapon 13 854 317 1\nwwvhixixe armor 500 285 382 2\nh orb 58 57 409 2\ny weapon 734 408 297 4\n12\nwvxwgjoera physician 55 hs\nusukedr sentry 41 hs\niu physician 100 hs\ngixlx gladiator 42 fudflb\nrd sentry 95 wwvhixixe\nbaff sentry 6 wwvhixixe\nwkhxoubhy sentry 73 h\niat physician 3 h\nc sentry 24 y\noveuaziss gladiator 54 y\nbyfhpjezzv sentry 18 y\njxnpuofle gladiator 65 y", "output": "y 4 c oveuaziss byfhpjezzv jxnpuofle \nwwvhixixe 2 rd baff \nhs 3 wvxwgjoera usukedr iu " }, { "input": "4\nsword weapon 0 0 0 2\npagstarmor armor 0 0 0 1\niceorb orb 0 0 0 2\nlongbow weapon 0 0 0 1\n1\nteddy physician 1 iceorb", "output": "sword 0 \npagstarmor 0 \niceorb 1 teddy " } ]
122
0
-1
2,028
616
Dinner with Emma
[ "games", "greedy" ]
null
null
Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places. Munhattan consists of *n* streets and *m* avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to *n* and the avenues are numbered with integers from 1 to *m*. The cost of dinner in the restaurant at the intersection of the *i*-th street and the *j*-th avenue is *c**ij*. Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.
The first line contains two integers *n*,<=*m* (1<=≀<=*n*,<=*m*<=≀<=100) β€” the number of streets and avenues in Munhattan. Each of the next *n* lines contains *m* integers *c**ij* (1<=≀<=*c**ij*<=≀<=109) β€” the cost of the dinner in the restaurant on the intersection of the *i*-th street and the *j*-th avenue.
Print the only integer *a* β€” the cost of the dinner for Jack and Emma.
[ "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1\n", "3 3\n1 2 3\n2 3 1\n3 1 2\n" ]
[ "2\n", "1\n" ]
In the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2. In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.
[ { "input": "3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "output": "2" }, { "input": "3 3\n1 2 3\n2 3 1\n3 1 2", "output": "1" }, { "input": "1 1\n1", "output": "1" }, { "input": "1 10\n74 35 82 39 1 84 29 41 70 12", "output": "1" }, { "input": "10 1\n44\n23\n65\n17\n48\n29\n49\n88\n91\n85", "output": "91" }, { "input": "10 10\n256 72 455 45 912 506 235 68 951 92\n246 305 45 212 788 621 449 876 459 899\n732 107 230 357 370 610 997 669 61 192\n131 93 481 527 983 920 825 540 435 54\n777 682 984 20 337 480 264 137 249 502\n51 467 479 228 923 752 714 436 199 973\n3 91 612 571 631 212 751 84 886 948\n252 130 583 23 194 985 234 978 709 16\n636 991 203 469 719 540 184 902 503 652\n826 680 150 284 37 987 360 183 447 51", "output": "184" }, { "input": "1 1\n1000000000", "output": "1000000000" }, { "input": "2 1\n999999999\n1000000000", "output": "1000000000" } ]
108
512,000
3
2,030
235
LCM Challenge
[ "number theory" ]
null
null
Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it. But I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater than *n*. Can you help me to find the maximum possible least common multiple of these three integers?
The first line contains an integer *n* (1<=≀<=*n*<=≀<=106) β€” the *n* mentioned in the statement.
Print a single integer β€” the maximum possible LCM of three not necessarily distinct positive integers that are not greater than *n*.
[ "9\n", "7\n" ]
[ "504\n", "210\n" ]
The least common multiple of some positive integers is the least positive integer which is multiple for each of them. The result may become very large, 32-bit integer won't be enough. So using 64-bit integers is recommended. For the last example, we can chose numbers 7, 6, 5 and the LCM of them is 7Β·6Β·5 = 210. It is the maximum value we can get.
[ { "input": "9", "output": "504" }, { "input": "7", "output": "210" }, { "input": "1", "output": "1" }, { "input": "5", "output": "60" }, { "input": "6", "output": "60" }, { "input": "33", "output": "32736" }, { "input": "21", "output": "7980" }, { "input": "2", "output": "2" }, { "input": "41", "output": "63960" }, { "input": "29", "output": "21924" }, { "input": "117", "output": "1560780" }, { "input": "149", "output": "3241644" }, { "input": "733", "output": "392222436" }, { "input": "925", "output": "788888100" }, { "input": "509", "output": "131096004" }, { "input": "829", "output": "567662724" }, { "input": "117", "output": "1560780" }, { "input": "605", "output": "220348260" }, { "input": "245", "output": "14526540" }, { "input": "925", "output": "788888100" }, { "input": "213", "output": "9527916" }, { "input": "53", "output": "140556" }, { "input": "341", "output": "39303660" }, { "input": "21", "output": "7980" }, { "input": "605", "output": "220348260" }, { "input": "149", "output": "3241644" }, { "input": "733", "output": "392222436" }, { "input": "117", "output": "1560780" }, { "input": "53", "output": "140556" }, { "input": "245", "output": "14526540" }, { "input": "829", "output": "567662724" }, { "input": "924", "output": "783776526" }, { "input": "508", "output": "130065780" }, { "input": "700", "output": "341042100" }, { "input": "636", "output": "254839470" }, { "input": "20", "output": "6460" }, { "input": "604", "output": "218891412" }, { "input": "796", "output": "501826260" }, { "input": "732", "output": "389016270" }, { "input": "412", "output": "69256788" }, { "input": "700", "output": "341042100" }, { "input": "244", "output": "14289372" }, { "input": "828", "output": "563559150" }, { "input": "508", "output": "130065780" }, { "input": "796", "output": "501826260" }, { "input": "636", "output": "254839470" }, { "input": "924", "output": "783776526" }, { "input": "245", "output": "14526540" }, { "input": "828", "output": "563559150" }, { "input": "21", "output": "7980" }, { "input": "605", "output": "220348260" }, { "input": "636", "output": "254839470" }, { "input": "924", "output": "783776526" }, { "input": "116", "output": "1507420" }, { "input": "700", "output": "341042100" }, { "input": "732", "output": "389016270" }, { "input": "20", "output": "6460" }, { "input": "508", "output": "130065780" }, { "input": "148", "output": "3154620" }, { "input": "828", "output": "563559150" }, { "input": "763116", "output": "444394078546562430" }, { "input": "756604", "output": "433115377058855412" }, { "input": "447244", "output": "89460162932862372" }, { "input": "372636", "output": "51742503205363470" }, { "input": "546924", "output": "163597318076822526" }, { "input": "540412", "output": "157823524476316788" }, { "input": "714700", "output": "365063922340784100" }, { "input": "520731", "output": "141201007712496270" }, { "input": "695019", "output": "335728459024850814" }, { "input": "688507", "output": "326379736779169710" }, { "input": "862795", "output": "642275489615199390" }, { "input": "668827", "output": "299184742915995150" }, { "input": "810411", "output": "532248411551110590" }, { "input": "836603", "output": "585540171302562606" }, { "input": "978187", "output": "935975171582120670" }, { "input": "816923", "output": "545182335484592526" }, { "input": "958507", "output": "880611813728059710" }, { "input": "984699", "output": "954792870629291694" }, { "input": "642635", "output": "265393998349453470" }, { "input": "296604", "output": "26092892528622606" }, { "input": "1000000", "output": "999996000003000000" }, { "input": "8", "output": "280" }, { "input": "3", "output": "6" }, { "input": "4", "output": "12" }, { "input": "30", "output": "21924" }, { "input": "18", "output": "4080" }, { "input": "12", "output": "990" } ]
218
0
3
2,032
490
Queue
[ "dsu", "implementation" ]
null
null
During the lunch break all *n* Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working. Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1). After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task. Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
The first line contains integer *n* (2<=≀<=*n*<=≀<=2Β·105) β€” the number of students in the queue. Then *n* lines follow, *i*-th line contains the pair of integers *a**i*,<=*b**i* (0<=≀<=*a**i*,<=*b**i*<=≀<=106), where *a**i* is the ID number of a person in front of a student and *b**i* is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
Print a sequence of *n* integers *x*1,<=*x*2,<=...,<=*x**n* β€” the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
[ "4\n92 31\n0 7\n31 0\n7 141\n" ]
[ "92 7 31 141 \n" ]
The picture illustrates the queue for the first sample.
[ { "input": "4\n92 31\n0 7\n31 0\n7 141", "output": "92 7 31 141 " }, { "input": "2\n0 1\n2 0", "output": "2 1 " }, { "input": "3\n0 2\n1 3\n2 0", "output": "1 2 3 " }, { "input": "4\n101 0\n0 102\n102 100\n103 101", "output": "103 102 101 100 " }, { "input": "5\n0 1\n1 4\n4 0\n3 2\n5 3", "output": "5 1 3 4 2 " }, { "input": "6\n10001 0\n0 10005\n10003 10001\n10002 10000\n10005 10002\n10004 10003", "output": "10004 10005 10003 10002 10001 10000 " }, { "input": "3\n0 743259\n72866 70294\n743259 0", "output": "72866 743259 70294 " }, { "input": "4\n263750 0\n513707 263750\n0 718595\n718595 148112", "output": "513707 718595 263750 148112 " }, { "input": "5\n645873 145459\n638930 82975\n0 645873\n82975 389665\n145459 0", "output": "638930 645873 82975 145459 389665 " }, { "input": "6\n341637 51795\n0 809471\n51795 0\n244669 341637\n852537 508622\n809471 852537", "output": "244669 809471 341637 852537 51795 508622 " }, { "input": "7\n111283 0\n496010 510417\n423431 921854\n510417 111283\n0 496010\n758535 423431\n921854 59208", "output": "758535 496010 423431 510417 921854 111283 59208 " }, { "input": "8\n611412 115521\n114290 712424\n115521 242491\n242491 0\n0 114290\n712424 282922\n282922 589147\n359823 611412", "output": "359823 114290 611412 712424 115521 282922 242491 589147 " }, { "input": "9\n308992 348750\n0 6496\n487447 676506\n874677 985199\n260782 487447\n985199 260782\n348750 0\n570981 308992\n6496 570981", "output": "874677 6496 985199 570981 260782 308992 487447 348750 676506 " }, { "input": "10\n419946 201769\n245945 0\n842799 113073\n836998 245945\n0 794376\n692107 836998\n113073 904403\n904403 987165\n201769 692107\n794376 842799", "output": "419946 794376 201769 842799 692107 113073 836998 904403 245945 987165 " }, { "input": "10\n189071 852255\n227133 652124\n329720 4848\n652124 329720\n0 72517\n943168 0\n72517 544697\n4848 943168\n538963 189071\n544697 538963", "output": "227133 72517 652124 544697 329720 538963 4848 189071 943168 852255 " }, { "input": "2\n0 300000\n1000000 0", "output": "1000000 300000 " } ]
61
0
0
2,033
910
Minimum Sum
[ "constructive algorithms", "greedy", "math" ]
null
null
Petya has *n* positive integers *a*1,<=*a*2,<=...,<=*a**n*. His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to 'j' and replaced all digits 0 with one letter, all digits 1 with another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'. Your task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1<=000) β€” the number of Petya's numbers. Each of the following lines contains non-empty string *s**i* consisting of lowercase Latin letters from 'a' to 'j' β€” the Petya's numbers after Vasya's joke. The length of each string does not exceed six characters.
Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests.
[ "3\nab\nde\naj\n", "5\nabcdef\nghij\nbdef\naccbd\ng\n", "3\naa\njj\naa\n" ]
[ "47\n", "136542\n", "44\n" ]
In the first example, you need to replace the letter 'a' with the digit 1, the letter 'b' with the digit 0, the letter 'd' with the digit 2, the letter 'e' with the digit 3, and the letter 'j' with the digit 4. So after the restoration numbers will look like [10, 23, 14]. The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration. In the second example the numbers after the restoration can look like: [120468, 3579, 2468, 10024, 3]. In the second example the numbers after the restoration can look like: [11, 22, 11].
[ { "input": "3\nab\nde\naj", "output": "47" }, { "input": "5\nabcdef\nghij\nbdef\naccbd\ng", "output": "136542" }, { "input": "3\naa\njj\naa", "output": "44" }, { "input": "9\na\nb\nc\nd\nf\ng\nh\ni\nj", "output": "45" }, { "input": "5\nbdgbh\nadi\naa\ngjh\ngh", "output": "10824" }, { "input": "6\nchafj\nabhj\nfhe\nhfbd\njifgg\ng", "output": "42773" }, { "input": "1\nh", "output": "1" }, { "input": "7\nffh\nfhec\nfbchc\ng\ndfbhi\ncdbdi\ni", "output": "64995" }, { "input": "8\ne\nbhbib\nj\ndgb\njjbgb\nei\ndggbdh\nhfbbfj", "output": "429631" }, { "input": "10\ncf\ncha\nceiab\ng\naajac\ndj\nhe\ni\nhjfg\nhdcgcb", "output": "198795" }, { "input": "50\ng\nha\nhd\ndi\nac\nfdhhb\ng\nhgeag\nafafb\nb\nb\najjj\ncaiadi\nhciifa\nhb\ncaih\ncdbbi\ngjff\nbfe\neddci\ndijfie\nacjj\nef\ng\njdc\nahg\ne\nhbbh\ncdc\njifdc\ne\nffaehj\nhjhi\ng\neag\nfbbc\nchg\njhahfg\nbb\njd\njchh\nbefifj\nejac\ne\nh\njfhb\nedhe\nf\nag\nca", "output": "2673136" }, { "input": "31\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\nbc", "output": "50" }, { "input": "9\nb\nc\nd\ne\nf\ng\nh\ni\nj", "output": "45" }, { "input": "8\nb\nc\nd\nf\ng\nh\ni\nj", "output": "36" }, { "input": "8\nb\nce\necc\nf\ng\nh\ni\nj", "output": "176" }, { "input": "2\nababa\nbabaa", "output": "33332" }, { "input": "3\nabcbbc\nababab\nbcbbaa", "output": "443643" }, { "input": "3\nbb\nj\nc", "output": "16" }, { "input": "3\nj\ng\ng", "output": "4" }, { "input": "3\nbef\ncjff\nhi", "output": "1332" }, { "input": "3\nfi\nfej\nei", "output": "153" }, { "input": "4\nc\nb\nhh\ng", "output": "20" }, { "input": "4\nfjj\nba\nbc\neie", "output": "412" }, { "input": "4\nh\nchf\ngj\ndifd", "output": "1334" }, { "input": "4\ng\njicdh\nj\nfh", "output": "10287" }, { "input": "5\nfj\nbj\nja\nfd\ni", "output": "83" }, { "input": "5\ngij\nf\nj\nfd\niij", "output": "365" }, { "input": "5\nfhdh\ndaih\nff\nca\ncc", "output": "3468" }, { "input": "5\ni\ncghf\nh\ng\nbc", "output": "1281" }, { "input": "6\nb\ngc\na\nhj\nfg\nb", "output": "80" }, { "input": "6\nfj\ngd\nch\ni\ng\nh", "output": "80" }, { "input": "6\nedi\nfa\nad\nh\ngjf\njaa", "output": "766" }, { "input": "6\njafef\nihbb\njc\njc\ng\nfihji", "output": "37101" }, { "input": "7\nhg\ng\nag\nj\ng\na\nfe", "output": "82" }, { "input": "7\ncb\nfi\ndia\nada\nag\ng\nba", "output": "468" }, { "input": "7\nba\nac\nag\nfcj\ng\naa\ncgb", "output": "510" }, { "input": "7\niaiac\nc\naicic\nhfbfc\nggje\necgg\nhd", "output": "74622" }, { "input": "8\ngc\nf\nca\neh\nc\ni\nae\ng", "output": "122" }, { "input": "8\nc\nc\nh\nefe\nd\ne\nhjc\ngae", "output": "720" }, { "input": "8\nfhij\nbc\na\ngeh\nee\naeac\najb\njj", "output": "4136" }, { "input": "8\njaei\naidd\nciai\nfefdf\ngfahh\nh\nh\njagjg", "output": "78727" }, { "input": "9\ni\nh\ne\na\nb\nh\ni\nea\ni", "output": "36" }, { "input": "9\nhd\nca\nc\ncii\nii\nd\ne\nf\ngde", "output": "494" }, { "input": "9\njbc\nc\nfae\nce\nfgi\nigfg\nfeh\nied\nfe", "output": "2340" }, { "input": "9\nehdc\ng\ngdgj\naacg\nfgg\njhb\ng\nie\ndabfa", "output": "23429" }, { "input": "10\nc\naj\neh\nhc\nib\nd\nfc\nf\nfh\nc", "output": "204" }, { "input": "10\nji\nid\ni\na\nhhb\ndi\njd\ngdi\na\na", "output": "544" }, { "input": "10\necj\ni\nbadj\neai\naie\nfgj\nah\ngdaj\nai\nhdhd", "output": "8803" }, { "input": "10\nad\ngbha\nabh\ngbgc\nfa\njfde\neb\na\nfg\ndd", "output": "5084" }, { "input": "3\na\nb\nc", "output": "6" }, { "input": "1\na", "output": "1" }, { "input": "2\na\na", "output": "2" } ]
62
5,632,000
0
2,034
967
Mind the Gap
[ "implementation" ]
null
null
These days Arkady works as an air traffic controller at a large airport. He controls a runway which is usually used for landings only. Thus, he has a schedule of planes that are landing in the nearest future, each landing lasts $1$ minute. He was asked to insert one takeoff in the schedule. The takeoff takes $1$ minute itself, but for safety reasons there should be a time space between the takeoff and any landing of at least $s$ minutes from both sides. Find the earliest time when Arkady can insert the takeoff.
The first line of input contains two integers $n$ and $s$ ($1 \le n \le 100$, $1 \le s \le 60$)Β β€” the number of landings on the schedule and the minimum allowed time (in minutes) between a landing and a takeoff. Each of next $n$ lines contains two integers $h$ and $m$ ($0 \le h \le 23$, $0 \le m \le 59$)Β β€” the time, in hours and minutes, when a plane will land, starting from current moment (i.Β e. the current time is $0$ $0$). These times are given in increasing order.
Print two integers $h$ and $m$Β β€” the hour and the minute from the current moment of the earliest time Arkady can insert the takeoff.
[ "6 60\n0 0\n1 20\n3 21\n5 0\n19 30\n23 40\n", "16 50\n0 30\n1 20\n3 0\n4 30\n6 10\n7 50\n9 30\n11 10\n12 50\n14 30\n16 10\n17 50\n19 30\n21 10\n22 50\n23 59\n", "3 17\n0 30\n1 0\n12 0\n" ]
[ "6 1\n", "24 50\n", "0 0\n" ]
In the first example note that there is not enough time between 1:20 and 3:21, because each landing and the takeoff take one minute. In the second example there is no gaps in the schedule, so Arkady can only add takeoff after all landings. Note that it is possible that one should wait more than $24$ hours to insert the takeoff. In the third example Arkady can insert the takeoff even between the first landing.
[ { "input": "6 60\n0 0\n1 20\n3 21\n5 0\n19 30\n23 40", "output": "6 1" }, { "input": "16 50\n0 30\n1 20\n3 0\n4 30\n6 10\n7 50\n9 30\n11 10\n12 50\n14 30\n16 10\n17 50\n19 30\n21 10\n22 50\n23 59", "output": "24 50" }, { "input": "3 17\n0 30\n1 0\n12 0", "output": "0 0" }, { "input": "24 60\n0 21\n2 21\n2 46\n3 17\n4 15\n5 43\n6 41\n7 50\n8 21\n9 8\n10 31\n10 45\n12 30\n14 8\n14 29\n14 32\n14 52\n15 16\n16 7\n16 52\n18 44\n20 25\n21 13\n22 7", "output": "23 8" }, { "input": "20 60\n0 9\n0 19\n0 57\n2 42\n3 46\n3 47\n5 46\n8 1\n9 28\n9 41\n10 54\n12 52\n13 0\n14 49\n17 28\n17 39\n19 34\n20 52\n21 35\n23 22", "output": "6 47" }, { "input": "57 20\n0 2\n0 31\n1 9\n1 42\n1 58\n2 4\n2 35\n2 49\n3 20\n3 46\n4 23\n4 52\n5 5\n5 39\n6 7\n6 48\n6 59\n7 8\n7 35\n8 10\n8 46\n8 53\n9 19\n9 33\n9 43\n10 18\n10 42\n11 0\n11 26\n12 3\n12 5\n12 30\n13 1\n13 38\n14 13\n14 54\n15 31\n16 5\n16 44\n17 18\n17 30\n17 58\n18 10\n18 34\n19 13\n19 49\n19 50\n19 59\n20 17\n20 23\n20 40\n21 18\n21 57\n22 31\n22 42\n22 56\n23 37", "output": "23 58" }, { "input": "66 20\n0 16\n0 45\n0 58\n1 6\n1 19\n2 7\n2 9\n3 9\n3 25\n3 57\n4 38\n4 58\n5 21\n5 40\n6 16\n6 19\n6 58\n7 6\n7 26\n7 51\n8 13\n8 36\n8 55\n9 1\n9 15\n9 33\n10 12\n10 37\n11 15\n11 34\n12 8\n12 37\n12 55\n13 26\n14 0\n14 34\n14 36\n14 48\n15 23\n15 29\n15 43\n16 8\n16 41\n16 45\n17 5\n17 7\n17 15\n17 29\n17 46\n18 12\n18 19\n18 38\n18 57\n19 32\n19 58\n20 5\n20 40\n20 44\n20 50\n21 18\n21 49\n22 18\n22 47\n23 1\n23 38\n23 50", "output": "1 40" }, { "input": "1 1\n0 0", "output": "0 2" }, { "input": "10 1\n0 2\n0 4\n0 5\n0 8\n0 9\n0 11\n0 13\n0 16\n0 19\n0 21", "output": "0 0" }, { "input": "10 1\n0 2\n0 5\n0 8\n0 11\n0 15\n0 17\n0 25\n0 28\n0 29\n0 32", "output": "0 0" }, { "input": "15 20\n0 47\n2 24\n4 19\n4 34\n5 46\n8 15\n9 8\n10 28\n17 47\n17 52\n18 32\n19 50\n20 46\n20 50\n23 21", "output": "0 0" }, { "input": "1 5\n1 0", "output": "0 0" }, { "input": "24 60\n1 0\n2 0\n3 0\n4 0\n5 0\n6 0\n7 0\n8 0\n9 0\n10 0\n11 0\n12 0\n13 0\n14 0\n15 0\n16 0\n17 0\n18 0\n19 0\n20 0\n21 0\n22 0\n23 0\n23 59", "output": "25 0" }, { "input": "1 30\n0 29", "output": "1 0" }, { "input": "1 2\n3 0", "output": "0 0" }, { "input": "16 60\n0 30\n1 20\n3 0\n4 30\n6 10\n7 50\n9 30\n11 10\n12 50\n14 30\n16 10\n17 50\n19 30\n21 10\n22 50\n23 59", "output": "25 0" }, { "input": "1 5\n0 6", "output": "0 0" }, { "input": "2 60\n0 59\n23 59", "output": "2 0" }, { "input": "1 58\n0 1", "output": "1 0" }, { "input": "25 60\n0 0\n1 0\n2 0\n3 0\n4 0\n5 0\n6 0\n7 0\n8 0\n9 0\n10 0\n11 0\n12 0\n13 0\n14 0\n15 0\n16 0\n17 0\n18 0\n19 0\n20 0\n21 0\n22 0\n23 0\n23 59", "output": "25 0" }, { "input": "2 3\n0 3\n0 30", "output": "0 7" }, { "input": "16 50\n0 30\n1 20\n3 0\n4 30\n6 10\n7 50\n9 30\n11 10\n12 50\n14 30\n16 10\n17 50\n19 30\n21 10\n22 50\n23 9", "output": "24 0" }, { "input": "1 60\n2 0", "output": "0 0" }, { "input": "2 60\n0 0\n5 0", "output": "1 1" }, { "input": "1 30\n0 31", "output": "0 0" }, { "input": "2 60\n0 59\n3 1", "output": "2 0" }, { "input": "2 60\n0 59\n5 0", "output": "2 0" }, { "input": "1 59\n0 0", "output": "1 0" }, { "input": "3 25\n0 0\n1 0\n2 0", "output": "0 26" }, { "input": "1 2\n2 3", "output": "0 0" } ]
61
0
0
2,042
618
Hamiltonian Spanning Tree
[ "dfs and similar", "dp", "graph matchings", "greedy", "trees" ]
null
null
A group of *n* cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are roads in total. It takes exactly *y* seconds to traverse any single road. A spanning tree is a set of roads containing exactly *n*<=-<=1 roads such that it's possible to travel between any two cities using only these roads. Some spanning tree of the initial network was chosen. For every road in this tree the time one needs to traverse this road was changed from *y* to *x* seconds. Note that it's not guaranteed that *x* is smaller than *y*. You would like to travel through all the cities using the shortest path possible. Given *n*, *x*, *y* and a description of the spanning tree that was chosen, find the cost of the shortest path that starts in any city, ends in any city and visits all cities exactly once.
The first line of the input contains three integers *n*, *x* and *y* (2<=≀<=*n*<=≀<=200<=000,<=1<=≀<=*x*,<=*y*<=≀<=109). Each of the next *n*<=-<=1 lines contains a description of a road in the spanning tree. The *i*-th of these lines contains two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*)Β β€” indices of the cities connected by the *i*-th road. It is guaranteed that these roads form a spanning tree.
Print a single integerΒ β€” the minimum number of seconds one needs to spend in order to visit all the cities exactly once.
[ "5 2 3\n1 2\n1 3\n3 4\n5 3\n", "5 3 2\n1 2\n1 3\n3 4\n5 3\n" ]
[ "9\n", "8\n" ]
In the first sample, roads of the spanning tree have cost 2, while other roads have cost 3. One example of an optimal path is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3a11f64ac0349d4ecd3a2b4c3443aeb7ac3b28b9.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the second sample, we have the same spanning tree, but roads in the spanning tree cost 3, while other roads cost 2. One example of an optimal path is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3fdb844c44665567f5addf82820eb6f96a060920.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "5 2 3\n1 2\n1 3\n3 4\n5 3", "output": "9" }, { "input": "5 3 2\n1 2\n1 3\n3 4\n5 3", "output": "8" }, { "input": "50 23129 410924\n18 28\n17 23\n21 15\n18 50\n50 11\n32 3\n44 41\n50 31\n50 34\n5 14\n36 13\n22 40\n20 9\n9 43\n19 47\n48 40\n20 22\n33 45\n35 22\n33 24\n9 6\n13 1\n13 24\n49 20\n1 20\n29 38\n10 35\n25 23\n49 30\n42 8\n20 18\n32 15\n32 1\n27 10\n20 47\n41 7\n20 14\n18 26\n4 20\n20 2\n46 37\n41 16\n46 41\n12 20\n8 40\n18 37\n29 3\n32 39\n23 37", "output": "8113631" }, { "input": "2 3 4\n1 2", "output": "3" }, { "input": "50 491238 12059\n42 3\n5 9\n11 9\n41 15\n42 34\n11 6\n40 16\n23 8\n41 7\n22 6\n24 29\n7 17\n31 2\n17 33\n39 42\n42 6\n41 50\n21 45\n19 41\n1 21\n42 1\n2 25\n17 28\n49 42\n30 13\n4 12\n10 32\n48 35\n21 2\n14 6\n49 29\n18 20\n38 22\n19 37\n20 47\n3 36\n1 44\n20 7\n4 11\n39 26\n30 40\n6 7\n25 46\n2 27\n30 42\n10 11\n8 21\n42 43\n35 8", "output": "590891" }, { "input": "2 4 1\n1 2", "output": "4" }, { "input": "5 2 2\n1 2\n1 3\n1 4\n1 5", "output": "8" }, { "input": "4 100 1\n1 2\n1 3\n1 4", "output": "102" }, { "input": "3 2 1\n1 2\n1 3", "output": "3" }, { "input": "5 6 1\n1 2\n1 3\n1 4\n1 5", "output": "9" }, { "input": "3 100 1\n1 2\n2 3", "output": "101" }, { "input": "2 2 1\n1 2", "output": "2" }, { "input": "5 3 2\n1 2\n1 3\n1 4\n1 5", "output": "9" }, { "input": "4 1000 1\n1 2\n1 3\n1 4", "output": "1002" }, { "input": "4 100 1\n1 2\n2 3\n3 4", "output": "3" }, { "input": "2 3 1\n1 2", "output": "3" }, { "input": "5 4 3\n1 2\n1 3\n1 4\n1 5", "output": "13" } ]
1,123
20,992,000
0
2,043
478
Table Decorations
[ "greedy" ]
null
null
You have *r* red, *g* green and *b* blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number *t* of tables can be decorated if we know number of balloons of each color? Your task is to write a program that for given values *r*, *g* and *b* will find the maximum number *t* of tables, that can be decorated in the required manner.
The single line contains three integers *r*, *g* and *b* (0<=≀<=*r*,<=*g*,<=*b*<=≀<=2Β·109) β€” the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
Print a single integer *t* β€” the maximum number of tables that can be decorated in the required manner.
[ "5 4 3\n", "1 1 1\n", "2 3 3\n" ]
[ "4\n", "1\n", "2\n" ]
In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively.
[ { "input": "5 4 3", "output": "4" }, { "input": "1 1 1", "output": "1" }, { "input": "2 3 3", "output": "2" }, { "input": "0 1 0", "output": "0" }, { "input": "0 3 3", "output": "2" }, { "input": "4 0 4", "output": "2" }, { "input": "1000000000 1000000000 1000000000", "output": "1000000000" }, { "input": "100 99 56", "output": "85" }, { "input": "1000 1000 1002", "output": "1000" }, { "input": "0 1 1000000000", "output": "1" }, { "input": "500000000 1000000000 500000000", "output": "666666666" }, { "input": "1000000000 2000000000 1000000000", "output": "1333333333" }, { "input": "2000000000 2000000000 2000000000", "output": "2000000000" }, { "input": "0 0 0", "output": "0" }, { "input": "1 2000000000 1000000000", "output": "1000000000" }, { "input": "1585222789 1889821127 2000000000", "output": "1825014638" }, { "input": "10000 7500 7500", "output": "8333" }, { "input": "150000 75000 75000", "output": "100000" }, { "input": "999288131 55884921 109298382", "output": "165183303" }, { "input": "100500 100500 3", "output": "67001" }, { "input": "1463615122 1988383731 837331500", "output": "1429776784" }, { "input": "1938 8999 1882", "output": "3820" }, { "input": "45 33 76", "output": "51" }, { "input": "100000 1 2", "output": "3" }, { "input": "198488 50 18", "output": "68" }, { "input": "82728372 939848 100139442", "output": "61269220" }, { "input": "99 5747 5298", "output": "3714" }, { "input": "3 5 2", "output": "3" }, { "input": "7511 7512 7513", "output": "7512" }, { "input": "1234567890 123456789 987654321", "output": "781893000" }, { "input": "500000000 2000000000 500000000", "output": "1000000000" }, { "input": "500000002 2000000000 500000001", "output": "1000000001" }, { "input": "520000000 1000000033 501000000", "output": "673666677" }, { "input": "10000 1000 100000", "output": "11000" }, { "input": "2000000000 500000000 499999999", "output": "999999999" }, { "input": "1999999999 500000000 500000000", "output": "999999999" }, { "input": "1 1 9", "output": "2" }, { "input": "3 0 0", "output": "0" }, { "input": "6 1 1", "output": "2" }, { "input": "2000000000 1999999999 1999999999", "output": "1999999999" }, { "input": "3 4 9", "output": "5" }, { "input": "3 3 6", "output": "4" } ]
61
0
0
2,044
744
Hongcow Builds A Nation
[ "dfs and similar", "graphs" ]
null
null
Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries. The world can be modeled as an undirected graph with *n* nodes and *m* edges. *k* of the nodes are home to the governments of the *k* countries that make up the world. There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable. Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.
The first line of input will contain three integers *n*, *m* and *k* (1<=≀<=*n*<=≀<=1<=000, 0<=≀<=*m*<=≀<=100<=000, 1<=≀<=*k*<=≀<=*n*)Β β€” the number of vertices and edges in the graph, and the number of vertices that are homes of the government. The next line of input will contain *k* integers *c*1,<=*c*2,<=...,<=*c**k* (1<=≀<=*c**i*<=≀<=*n*). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world. The following *m* lines of input will contain two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*). This denotes an undirected edge between nodes *u**i* and *v**i*. It is guaranteed that the graph described by the input is stable.
Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.
[ "4 1 2\n1 3\n1 2\n", "3 3 1\n2\n1 2\n1 3\n2 3\n" ]
[ "2\n", "0\n" ]
For the first sample test, the graph looks like this: For the second sample test, the graph looks like this:
[ { "input": "4 1 2\n1 3\n1 2", "output": "2" }, { "input": "3 3 1\n2\n1 2\n1 3\n2 3", "output": "0" }, { "input": "10 3 2\n1 10\n1 2\n1 3\n4 5", "output": "33" }, { "input": "1 0 1\n1", "output": "0" }, { "input": "1000 0 1\n72", "output": "499500" }, { "input": "24 38 2\n4 13\n7 1\n24 1\n2 8\n17 2\n2 18\n22 2\n23 3\n5 9\n21 5\n6 7\n6 19\n6 20\n11 7\n7 20\n13 8\n16 8\n9 10\n14 9\n21 9\n12 10\n10 22\n23 10\n17 11\n11 24\n20 12\n13 16\n13 23\n15 14\n17 14\n14 20\n19 16\n17 20\n17 23\n18 22\n18 23\n22 19\n21 20\n23 24", "output": "215" }, { "input": "10 30 1\n4\n1 2\n3 1\n4 1\n1 6\n1 8\n10 1\n2 4\n2 7\n3 4\n3 5\n7 3\n3 9\n10 3\n5 4\n6 4\n7 4\n9 4\n10 4\n6 5\n5 8\n9 5\n10 5\n6 7\n9 6\n10 6\n7 8\n9 7\n10 7\n9 8\n10 8", "output": "15" }, { "input": "10 13 2\n5 10\n2 1\n1 4\n2 3\n2 8\n3 4\n7 3\n4 6\n8 4\n4 9\n6 7\n6 9\n10 6\n7 8", "output": "23" }, { "input": "10 10 3\n2 5 6\n1 3\n4 1\n4 3\n5 3\n3 9\n8 4\n9 4\n5 10\n8 7\n10 8", "output": "18" }, { "input": "10 5 3\n1 5 9\n1 3\n1 8\n2 3\n8 4\n5 7", "output": "17" }, { "input": "6 4 2\n1 4\n1 2\n2 3\n4 5\n5 6", "output": "2" }, { "input": "7 8 2\n1 4\n1 2\n2 3\n4 5\n4 6\n4 7\n5 6\n5 7\n6 7", "output": "1" }, { "input": "5 2 3\n1 3 4\n1 5\n2 4", "output": "0" }, { "input": "5 3 2\n1 2\n2 3\n2 4\n1 5", "output": "1" }, { "input": "9 5 2\n1 5\n1 2\n2 3\n3 4\n5 6\n6 7", "output": "13" }, { "input": "6 4 1\n1\n2 3\n3 4\n4 5\n5 6", "output": "11" }, { "input": "6 4 2\n1 5\n1 2\n2 3\n3 4\n5 6", "output": "3" }, { "input": "7 3 3\n1 5 6\n1 2\n1 3\n6 7", "output": "4" }, { "input": "5 2 2\n1 2\n1 3\n2 4", "output": "2" }, { "input": "11 7 2\n1 4\n1 2\n1 3\n4 5\n4 6\n5 6\n9 10\n1 11", "output": "24" }, { "input": "20 4 5\n1 3 9 10 20\n5 6\n1 2\n7 9\n4 10", "output": "89" } ]
61
0
0
2,045
687
NP-Hard Problem
[ "dfs and similar", "graphs" ]
null
null
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting. Suppose the graph *G* is given. Subset *A* of its vertices is called a vertex cover of this graph, if for each edge *uv* there is at least one endpoint of it in this set, i.e. or (or both). Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover. They have agreed to give you their graph and you need to find two disjoint subsets of its vertices *A* and *B*, such that both *A* and *B* are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
The first line of the input contains two integers *n* and *m* (2<=≀<=*n*<=≀<=100<=000, 1<=≀<=*m*<=≀<=100<=000)Β β€” the number of vertices and the number of edges in the prize graph, respectively. Each of the next *m* lines contains a pair of integers *u**i* and *v**i* (1<=<=≀<=<=*u**i*,<=<=*v**i*<=<=≀<=<=*n*), denoting an undirected edge between *u**i* and *v**i*. It's guaranteed the graph won't contain any self-loops or multiple edges.
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes). If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer *k* denoting the number of vertices in that vertex cover, and the second line contains *k* integersΒ β€” the indices of vertices. Note that because of *m*<=β‰₯<=1, vertex cover cannot be empty.
[ "4 2\n1 2\n2 3\n", "3 3\n1 2\n2 3\n1 3\n" ]
[ "1\n2 \n2\n1 3 \n", "-1\n" ]
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish). In the second sample, there is no way to satisfy both Pari and Arya.
[ { "input": "4 2\n1 2\n2 3", "output": "1\n2 \n2\n1 3 " }, { "input": "3 3\n1 2\n2 3\n1 3", "output": "-1" }, { "input": "5 7\n3 2\n5 4\n3 4\n1 3\n1 5\n1 4\n2 5", "output": "-1" }, { "input": "10 11\n4 10\n8 10\n2 3\n2 4\n7 1\n8 5\n2 8\n7 2\n1 2\n2 9\n6 8", "output": "-1" }, { "input": "10 9\n2 5\n2 4\n2 7\n2 9\n2 3\n2 8\n2 6\n2 10\n2 1", "output": "1\n2 \n9\n1 5 4 7 9 3 8 6 10 " }, { "input": "10 16\n6 10\n5 2\n6 4\n6 8\n5 3\n5 4\n6 2\n5 9\n5 7\n5 1\n6 9\n5 8\n5 10\n6 1\n6 7\n6 3", "output": "2\n5 6 \n8\n1 2 10 4 8 9 7 3 " }, { "input": "10 17\n5 1\n8 1\n2 1\n2 6\n3 1\n5 7\n3 7\n8 6\n4 7\n2 7\n9 7\n10 7\n3 6\n4 1\n9 1\n8 7\n10 1", "output": "7\n5 3 2 8 4 9 10 \n3\n1 7 6 " }, { "input": "10 15\n5 9\n7 8\n2 9\n1 9\n3 8\n3 9\n5 8\n1 8\n6 9\n7 9\n4 8\n4 9\n10 9\n10 8\n6 8", "output": "2\n9 8 \n8\n1 5 7 3 4 10 6 2 " }, { "input": "10 9\n4 9\n1 9\n10 9\n2 9\n3 9\n6 9\n5 9\n7 9\n8 9", "output": "1\n9 \n9\n1 4 10 2 3 6 5 7 8 " }, { "input": "2 1\n1 2", "output": "1\n2 \n1\n1 " }, { "input": "10 10\n6 4\n9 1\n3 6\n6 7\n4 2\n9 6\n8 6\n5 7\n1 4\n6 10", "output": "6\n9 4 3 7 8 10 \n4\n1 6 2 5 " }, { "input": "20 22\n20 8\n1 3\n3 18\n14 7\n19 6\n7 20\n14 8\n8 10\n2 5\n11 2\n4 19\n14 2\n7 11\n15 1\n12 15\n7 6\n11 13\n1 16\n9 12\n1 19\n17 3\n11 20", "output": "-1" }, { "input": "20 22\n3 18\n9 19\n6 15\n7 1\n16 8\n18 7\n12 3\n18 4\n9 15\n20 1\n4 2\n6 7\n14 2\n7 15\n7 10\n8 1\n13 6\n9 7\n11 8\n2 6\n18 5\n17 15", "output": "-1" }, { "input": "1000 1\n839 771", "output": "1\n839 \n1\n771 " }, { "input": "1000 1\n195 788", "output": "1\n788 \n1\n195 " }, { "input": "100000 1\n42833 64396", "output": "1\n64396 \n1\n42833 " }, { "input": "100000 1\n26257 21752", "output": "1\n26257 \n1\n21752 " }, { "input": "5 5\n1 2\n2 3\n3 4\n4 5\n5 1", "output": "-1" } ]
764
13,209,600
3
2,050
554
Kyoya and Photobooks
[ "brute force", "math", "strings" ]
null
null
Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He has 26 photos, labeled "a" to "z", and he has compiled them into a photo booklet with some photos in some order (possibly with some photos being duplicated). A photo booklet can be described as a string of lowercase letters, consisting of the photos in the booklet in order. He now wants to sell some "special edition" photobooks, each with one extra photo inserted anywhere in the book. He wants to make as many distinct photobooks as possible, so he can make more money. He asks Haruhi, how many distinct photobooks can he make by inserting one extra photo into the photobook he already has? Please help Haruhi solve this problem.
The first line of input will be a single string *s* (1<=≀<=|*s*|<=≀<=20). String *s* consists only of lowercase English letters.
Output a single integer equal to the number of distinct photobooks Kyoya Ootori can make.
[ "a\n", "hi\n" ]
[ "51\n", "76\n" ]
In the first case, we can make 'ab','ac',...,'az','ba','ca',...,'za', and 'aa', producing a total of 51 distinct photo booklets.
[ { "input": "a", "output": "51" }, { "input": "hi", "output": "76" }, { "input": "y", "output": "51" }, { "input": "kgan", "output": "126" }, { "input": "zoabkyuvus", "output": "276" }, { "input": "spyemhyznjieyhhbk", "output": "451" }, { "input": "xulsyfkuizjauadjjopu", "output": "526" }, { "input": "e", "output": "51" }, { "input": "zv", "output": "76" }, { "input": "jgv", "output": "101" }, { "input": "zsfo", "output": "126" }, { "input": "jselr", "output": "151" }, { "input": "dwemig", "output": "176" }, { "input": "mddoxsf", "output": "201" }, { "input": "jgirkrmi", "output": "226" }, { "input": "spkxurcum", "output": "251" }, { "input": "fykkiubdkt", "output": "276" }, { "input": "fznbcxsxygs", "output": "301" }, { "input": "qcrvrdqcbtou", "output": "326" }, { "input": "qktrbjzrqgmlr", "output": "351" }, { "input": "foamodbvptlxxg", "output": "376" }, { "input": "ydzpjhsidipricw", "output": "401" }, { "input": "lpfpndmjfvqejdgf", "output": "426" }, { "input": "ofkvparuvjtggnmab", "output": "451" }, { "input": "xxncfutrtxcwdzwbgs", "output": "476" }, { "input": "zovhffccflkgqncsdte", "output": "501" }, { "input": "cskgsxywlvfeicoueglr", "output": "526" }, { "input": "gggggggggggggggggggg", "output": "526" }, { "input": "qdqdddqddqqddqddqdqd", "output": "526" }, { "input": "takttttaakaaktakttkt", "output": "526" }, { "input": "coccoooogogcgocccmcg", "output": "526" }, { "input": "kskkskkkssksssk", "output": "401" }, { "input": "lllllllllllllll", "output": "401" } ]
62
0
3
2,055
505
Mr. Kitayuta, the Treasure Hunter
[ "dfs and similar", "dp", "two pointers" ]
null
null
The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from 0 to 30000 from the west to the east. These islands are known to contain many treasures. There are *n* gems in the Shuseki Islands in total, and the *i*-th gem is located on island *p**i*. Mr. Kitayuta has just arrived at island 0. With his great jumping ability, he will repeatedly perform jumps between islands to the east according to the following process: - First, he will jump from island 0 to island *d*. - After that, he will continue jumping according to the following rule. Let *l* be the length of the previous jump, that is, if his previous jump was from island *prev* to island *cur*, let *l*<==<=*cur*<=-<=*prev*. He will perform a jump of length *l*<=-<=1, *l* or *l*<=+<=1 to the east. That is, he will jump to island (*cur*<=+<=*l*<=-<=1), (*cur*<=+<=*l*) or (*cur*<=+<=*l*<=+<=1) (if they exist). The length of a jump must be positive, that is, he cannot perform a jump of length 0 when *l*<==<=1. If there is no valid destination, he will stop jumping. Mr. Kitayuta will collect the gems on the islands visited during the process. Find the maximum number of gems that he can collect.
The first line of the input contains two space-separated integers *n* and *d* (1<=≀<=*n*,<=*d*<=≀<=30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively. The next *n* lines describe the location of the gems. The *i*-th of them (1<=≀<=*i*<=≀<=*n*) contains a integer *p**i* (*d*<=≀<=*p*1<=≀<=*p*2<=≀<=...<=≀<=*p**n*<=≀<=30000), denoting the number of the island that contains the *i*-th gem.
Print the maximum number of gems that Mr. Kitayuta can collect.
[ "4 10\n10\n21\n27\n27\n", "8 8\n9\n19\n28\n36\n45\n55\n66\n78\n", "13 7\n8\n8\n9\n16\n17\n17\n18\n21\n23\n24\n24\n26\n30\n" ]
[ "3\n", "6\n", "4\n" ]
In the first sample, the optimal route is 0  →  10 (+1 gem)  →  19  →  27 (+2 gems)  → ... In the second sample, the optimal route is 0  →  8  →  15  →  21 →  28 (+1 gem)  →  36 (+1 gem)  →  45 (+1 gem)  →  55 (+1 gem)  →  66 (+1 gem)  →  78 (+1 gem)  → ... In the third sample, the optimal route is 0  →  7  →  13  →  18 (+1 gem)  →  24 (+2 gems)  →  30 (+1 gem)  → ...
[ { "input": "4 10\n10\n21\n27\n27", "output": "3" }, { "input": "8 8\n9\n19\n28\n36\n45\n55\n66\n78", "output": "6" }, { "input": "13 7\n8\n8\n9\n16\n17\n17\n18\n21\n23\n24\n24\n26\n30", "output": "4" }, { "input": "8 4\n9\n15\n15\n16\n22\n25\n25\n28", "output": "8" }, { "input": "1 30000\n30000", "output": "1" }, { "input": "1 12345\n23456", "output": "0" }, { "input": "1 1\n30000", "output": "1" }, { "input": "5 4\n4\n5\n9\n21\n25", "output": "4" }, { "input": "8 7\n8\n15\n18\n19\n23\n24\n25\n27", "output": "3" }, { "input": "11 15\n15\n18\n19\n19\n21\n23\n24\n26\n26\n29\n30", "output": "2" }, { "input": "1 1\n1", "output": "1" }, { "input": "12 244\n448\n29889\n29890\n29891\n29892\n29893\n29894\n29895\n29896\n29897\n29898\n29899", "output": "11" }, { "input": "1 1500\n1500", "output": "1" }, { "input": "1 410\n30000", "output": "1" }, { "input": "10 220\n29991\n29992\n29993\n29994\n29995\n29996\n29997\n29998\n29999\n30000", "output": "10" }, { "input": "5 203\n29996\n29997\n29998\n29999\n30000", "output": "5" } ]
389
62,976,000
0
2,061
534
Polycarpus' Dice
[ "math" ]
null
null
Polycarp has *n* dice *d*1,<=*d*2,<=...,<=*d**n*. The *i*-th dice shows numbers from 1 to *d**i*. Polycarp rolled all the dice and the sum of numbers they showed is *A*. Agrippina didn't see which dice showed what number, she knows only the sum *A* and the values *d*1,<=*d*2,<=...,<=*d**n*. However, she finds it enough to make a series of statements of the following type: dice *i* couldn't show number *r*. For example, if Polycarp had two six-faced dice and the total sum is *A*<==<=11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible). For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is *A*.
The first line contains two integers *n*,<=*A* (1<=≀<=*n*<=≀<=2Β·105,<=*n*<=≀<=*A*<=≀<=*s*) β€” the number of dice and the sum of shown values where *s*<==<=*d*1<=+<=*d*2<=+<=...<=+<=*d**n*. The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (1<=≀<=*d**i*<=≀<=106), where *d**i* is the maximum value that the *i*-th dice can show.
Print *n* integers *b*1,<=*b*2,<=...,<=*b**n*, where *b**i* is the number of values for which it is guaranteed that the *i*-th dice couldn't show them.
[ "2 8\n4 4\n", "1 3\n5\n", "2 3\n2 3\n" ]
[ "3 3 ", "4 ", "0 1 " ]
In the first sample from the statement *A* equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3. In the second sample from the statement *A* equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5. In the third sample from the statement *A* equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3.
[ { "input": "2 8\n4 4", "output": "3 3 " }, { "input": "1 3\n5", "output": "4 " }, { "input": "2 3\n2 3", "output": "0 1 " }, { "input": "1 1\n3", "output": "2 " }, { "input": "1 2\n3", "output": "2 " }, { "input": "2 2\n2 3", "output": "1 2 " }, { "input": "2 4\n2 3", "output": "0 1 " }, { "input": "3 3\n5 1 5", "output": "4 0 4 " }, { "input": "3 4\n5 1 5", "output": "3 0 3 " }, { "input": "3 5\n5 1 5", "output": "2 0 2 " }, { "input": "3 6\n5 1 5", "output": "1 0 1 " }, { "input": "3 7\n5 1 5", "output": "0 0 0 " }, { "input": "3 8\n5 1 5", "output": "1 0 1 " }, { "input": "3 5\n1 2 100", "output": "0 0 98 " }, { "input": "10 20\n1 1 1 1 5 100 1 1 1 1", "output": "0 0 0 0 0 95 0 0 0 0 " }, { "input": "5 50\n1 1 1 1 1000000", "output": "0 0 0 0 999999 " }, { "input": "5 50\n2 2 2 2 1000000", "output": "0 0 0 0 999995 " }, { "input": "5 50\n10 10 10 10 1000000", "output": "0 0 0 0 999963 " }, { "input": "10 19\n1 5 6 1 6 4 1 2 9 5", "output": "0 0 0 0 0 0 0 0 0 0 " }, { "input": "10 40\n1 5 6 1 6 4 1 2 9 5", "output": "0 4 5 0 5 3 0 1 8 4 " }, { "input": "10 16\n5 7 7 5 9 3 8 5 7 2", "output": "0 0 0 0 2 0 1 0 0 0 " }, { "input": "10 58\n5 7 7 5 9 3 8 5 7 2", "output": "4 6 6 4 8 2 7 4 6 1 " }, { "input": "10 13\n9 9 6 9 10 4 5 10 8 9", "output": "5 5 2 5 6 0 1 6 4 5 " }, { "input": "10 79\n9 9 6 9 10 4 5 10 8 9", "output": "8 8 5 8 9 3 4 9 7 8 " }, { "input": "10 16\n4 1 8 3 3 3 4 3 6 6", "output": "0 0 1 0 0 0 0 0 0 0 " }, { "input": "10 41\n4 1 8 3 3 3 4 3 6 6", "output": "3 0 7 2 2 2 3 2 5 5 " }, { "input": "10 18\n8 1 9 8 4 1 1 8 6 2", "output": "0 0 0 0 0 0 0 0 0 0 " }, { "input": "10 48\n8 1 9 8 4 1 1 8 6 2", "output": "7 0 8 7 3 0 0 7 5 1 " }, { "input": "1 5\n5", "output": "4 " } ]
77
102,400
0
2,065
982
Row
[ "brute force", "constructive algorithms" ]
null
null
You're given a row with $n$ chairs. We call a seating of people "maximal" if the two following conditions hold: 1. There are no neighbors adjacent to anyone seated. 1. It's impossible to seat one more person without violating the first rule. The seating is given as a string consisting of zeros and ones ($0$ means that the corresponding seat is empty, $1$ β€” occupied). The goal is to determine whether this seating is "maximal". Note that the first and last seats are not adjacent (if $n \ne 2$).
The first line contains a single integer $n$ ($1 \leq n \leq 1000$)Β β€” the number of chairs. The next line contains a string of $n$ characters, each of them is either zero or one, describing the seating.
Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No". You are allowed to print letters in whatever case you'd like (uppercase or lowercase).
[ "3\n101\n", "4\n1011\n", "5\n10001\n" ]
[ "Yes\n", "No\n", "No\n" ]
In sample case one the given seating is maximal. In sample case two the person at chair three has a neighbour to the right. In sample case three it is possible to seat yet another person into chair three.
[ { "input": "3\n101", "output": "Yes" }, { "input": "4\n1011", "output": "No" }, { "input": "5\n10001", "output": "No" }, { "input": "1\n0", "output": "No" }, { "input": "1\n1", "output": "Yes" }, { "input": "100\n0101001010101001010010010101001010100101001001001010010101010010101001001010101001001001010100101010", "output": "Yes" }, { "input": "4\n0100", "output": "No" }, { "input": "42\n011000100101001001101011011010100010011010", "output": "No" }, { "input": "3\n001", "output": "No" }, { "input": "64\n1001001010010010100101010010010100100101001001001001010100101001", "output": "Yes" }, { "input": "3\n111", "output": "No" }, { "input": "4\n0000", "output": "No" }, { "input": "4\n0001", "output": "No" }, { "input": "4\n0010", "output": "No" }, { "input": "4\n0011", "output": "No" }, { "input": "4\n0101", "output": "Yes" }, { "input": "4\n0110", "output": "No" }, { "input": "4\n0111", "output": "No" }, { "input": "4\n1000", "output": "No" }, { "input": "4\n1001", "output": "Yes" }, { "input": "4\n1010", "output": "Yes" }, { "input": "4\n1100", "output": "No" }, { "input": "4\n1101", "output": "No" }, { "input": "4\n1110", "output": "No" }, { "input": "4\n1111", "output": "No" }, { "input": "2\n00", "output": "No" }, { "input": "2\n01", "output": "Yes" }, { "input": "2\n10", "output": "Yes" }, { "input": "2\n11", "output": "No" }, { "input": "3\n000", "output": "No" }, { "input": "3\n010", "output": "Yes" }, { "input": "3\n011", "output": "No" }, { "input": "3\n100", "output": "No" }, { "input": "3\n110", "output": "No" }, { "input": "100\n0111001010101110001100000010011000100101110010001100111110101110001110101010111000010010011000000110", "output": "No" }, { "input": "357\n100101010010010010010100101001001010101010100100100100101001010101001010010100101001010100101001010010100100101001010101010101001001010100101010010100101001010100100100101010010010010100101010010010101001010010010101001001010010010101010100100101010010100100101001010100101001010100101001010010010010100101001010100100100100100100100101010101010010010100101", "output": "Yes" }, { "input": "459\n000111000101010000100001001010111110011011010001100101111010111011101110111101111101100101100011011001100110001001111001101000111001011100110100011111011111000010000110010011100110011011111110011100001101001111000100111011001000001011111100110100001001001100101011100001110110100101011011110100100111101011000101110000100110100100010000000100001001111111000011101010010011001111010111001100000100111001010111011010000011000011100101101011101000011011000110011", "output": "No" }, { "input": "3\n001", "output": "No" }, { "input": "4\n0010", "output": "No" }, { "input": "3\n111", "output": "No" }, { "input": "4\n0110", "output": "No" }, { "input": "2\n11", "output": "No" }, { "input": "2\n10", "output": "Yes" }, { "input": "2\n01", "output": "Yes" }, { "input": "4\n1101", "output": "No" }, { "input": "2\n00", "output": "No" }, { "input": "4\n1111", "output": "No" }, { "input": "15\n000010101010000", "output": "No" }, { "input": "8\n10010100", "output": "No" }, { "input": "5\n00100", "output": "No" }, { "input": "6\n100100", "output": "No" }, { "input": "7\n1000001", "output": "No" }, { "input": "98\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010", "output": "Yes" } ]
77
0
0
2,066
448
Suffix Structures
[ "implementation", "strings" ]
null
null
Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team. At a competition the "Bizons" got the following problem: "You are given two distinct words (strings of English letters), *s* and *t*. You need to transform word *s* into word *t*". The task looked simple to the guys because they know the suffix data structures well. Bizon Senior loves suffix automaton. By applying it once to a string, he can remove from this string any single character. Bizon Middle knows suffix array well. By applying it once to a string, he can swap any two characters of this string. The guys do not know anything about the suffix tree, but it can help them do much more. Bizon the Champion wonders whether the "Bizons" can solve the problem. Perhaps, the solution do not require both data structures. Find out whether the guys can solve the problem and if they can, how do they do it? Can they solve it either only with use of suffix automaton or only with use of suffix array or they need both structures? Note that any structure may be used an unlimited number of times, the structures may be used in any order.
The first line contains a non-empty word *s*. The second line contains a non-empty word *t*. Words *s* and *t* are different. Each word consists only of lowercase English letters. Each word contains at most 100 letters.
In the single line print the answer to the problem. Print "need tree" (without the quotes) if word *s* cannot be transformed into word *t* even with use of both suffix array and suffix automaton. Print "automaton" (without the quotes) if you need only the suffix automaton to solve the problem. Print "array" (without the quotes) if you need only the suffix array to solve the problem. Print "both" (without the quotes), if you need both data structures to solve the problem. It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton.
[ "automaton\ntomat\n", "array\narary\n", "both\nhot\n", "need\ntree\n" ]
[ "automaton\n", "array\n", "both\n", "need tree\n" ]
In the third sample you can act like that: first transform "both" into "oth" by removing the first character using the suffix automaton and then make two swaps of the string using the suffix array and get "hot".
[ { "input": "automaton\ntomat", "output": "automaton" }, { "input": "array\narary", "output": "array" }, { "input": "both\nhot", "output": "both" }, { "input": "need\ntree", "output": "need tree" }, { "input": "abacaba\naaaa", "output": "automaton" }, { "input": "z\nzz", "output": "need tree" }, { "input": "itwtyhhsdjjffmmoqkkhxjouypznewstyorotxhozlytndehmaxogrohccnqcgkrjrdmnuaogiwmnmsbdaizqkxnkqxxiihbwepc\nsnixfywvcntitcefsgqxjcodwtumurcglfmnamnowzbjzmfzspbfuldraiepeeiyasmrsneekydsbvazoqszyjxkjiotushsddet", "output": "need tree" }, { "input": "y\nu", "output": "need tree" }, { "input": "nbjigpsbammkuuqrxfnmhtimwpflrflehffykbylmnxgadldchdbqklqbremcmzlpxieozgpfgrhegmdcxxfyehzzelcwgkierrj\nbjbakuqrnhimwhffykylmngadhbqkqbrcziefredxxezcgkerj", "output": "automaton" }, { "input": "gzvvawianfysfuxhruarhverinqsbrfxvkcsermuzowahevgskmpvfdljtcztnbkzftfhvnarvkfkqjgrzbrcfthqmspvpqcva\nwnm", "output": "automaton" }, { "input": "dvzohfzgzdjavqwhjcrdphpdqjwtqijabbrhformstqaonlhbglmxugkwviigqaohwvqfhdwwcvdkjrcgxblhvtashhcxssbvpo\nzgvqhpjhforlugkwfwrchvhp", "output": "automaton" }, { "input": "wkfoyetcjivofxaktmauapzeuhcpzjloszzxwydgavebgniiuzrscytsokjkjfkpylvxtlqlquzduywbhqdzmtwprfdohmwgmysy\ny", "output": "automaton" }, { "input": "npeidcoiulxdxzjozsonkdwnoazsbntfclnpubgweaynuhfmrtybqtkuihxxfhwlnquslnhzvqznyofzcbdewnrisqzdhsiyhkxf\nnpeidcoiulxdxzjozsonkdwnoazsbntfclnpubgeaynuhfmrtybqtkuihxxfhwlnquslnhzvqznyofzcbdewnrisqzdhsiyhkxf", "output": "automaton" }, { "input": "gahcqpgmypeahjcwkzahnhmsmxosnikucqwyzklbfwtujjlzvwklqzxakcrcqalhsvsgvknpxsoqkjnyjkypfsiogbcaxjyugeet\ngahcqpgmypeahjwwkzahnhmsmxopnikucacyzklbfwtujjlzvwkoqzxakcrcqqlhsvsgvknpxslgkjnyjkysfoisqbcaxjyuteeg", "output": "array" }, { "input": "vwesbxsifsjqapwridrenumrukgemlldpbtdhxivsrmzbgprtkqgaryniudkjgpjndluwxuohwwysmyuxyrulwsodgunzirudgtx\nugeabdszfshqsksddireguvsukieqlluhngdpxjvwwnzdrtrtrdjiuxgadtgjpxrmlynspyyryngxuiibrmurwpmoxwwuklbwumo", "output": "array" }, { "input": "kjnohlseyntrslfssrshjxclzlsbkfzfwwwgyxsysvmfkxugdwjodfyxhdsveruoioutwmtcbaljomaorvzjsbmglqckmsyieeiu\netihhycsjgdysowuljmaoksoecxawsgsljofkrjftuweidrkwtymyswdlilsozsxevfbformnbsumlxzqzykjvsnrlxufvgbmshc", "output": "array" }, { "input": "ezbpsylkfztypqrefinexshtgglmkoinrktkloitqhfkivoabrfrivvqrcxkjckzvcozpchhiodrbbxuhnwcjigftnrjfiqyxakh\niacxghqffzdbsiqunhxbiooqvfohzticjpvrzykcrlrxklgknyrkrhjxcetmfocierekatfvkbslkkrbhftwngoijpipvqyznthi", "output": "array" }, { "input": "smywwqeolrsytkthfgacnbufzaulgszikbhluzcdbafjclkqueepxbhoamrwswxherzhhuqqcttokbljfbppdinzqgdupkfevmke\nsmywwqeolrsytkthfgacnbufzaulgszikbhluzcdbafjclkqueepxbhoamrwswxherzhhufqcttokbljfbppdinzqgdupkqevmke", "output": "array" }, { "input": "hxsvvydmzhxrswvhkvrbjrfqkazbkjabnrdghposgyfeslzumaovfkallszzumztftgpcilwfrzpvhhbgdzdvnmseqywlzmhhoxh\ndbelhtzgkssyfrqgzuurdjhwvmdbhylhmvphjgxpzhxbb", "output": "both" }, { "input": "nppjzscfgcvdcnsjtiaudvutmgswqbewejlzibczzowgkdrjgxrpirfdaekvngcsonroheepdoeoeevaullbfwprcnhlxextbxpd\nifilrvacohnwcgzuleicucebrfxphosrgwnglxxkqrcorsxegjoppbb", "output": "both" }, { "input": "ggzmtrhkpdswwqgcbtviahqrgzhyhzddtdekchrpjgngupitzyyuipwstgzewktcqpwezidwvvxgjixnflpjhfznokmpbyzczrzk\ngpgwhtzrcytstezmhettkppgmvxlxqnkjzibiqdtceczkbfhdziuajwjqzgwnhnkdzizprgzwud", "output": "both" }, { "input": "iypjqiiqxhtinlmywpetgqqsdopxhghthjopgbodkwrdxzaaxmtaqcfuiarhrvasusanklzcqaytdyzndakcpljqupowompjjved\nhxeatriypptbhnokarhgqdrkqkypqzdttixphngmpqjodzjqlmcztyjfgoswjelwwdaqdjayavsdocuhqsluxaaopniviaumxip", "output": "both" }, { "input": "ypyhyabmljukejpltkgunwuanhxblhiouyltdiczttndrhdprqtlpfanmzlyzbqanfwfyurxhepuzspdvehxnblhajczqcxlqebx\nlladxuucky", "output": "both" }, { "input": "ddmgoarkuhknbtjggnomyxvvavobmylixwuxnnsdrrbibitoteaiydptnvtfblathihflefuggfnyayniragbtkommycpdyhft\ntejwybmyrhmalraptqwhghsckvnnaagtmzhnpwbhzzgfgritqwqqamgssllnicjqdkivrwaqyxngsqopwieljfxcdywjaal", "output": "need tree" }, { "input": "kipjuscf\nkbwfqfwuvkyhmvnaznzsgdgdnpipikbicmlcwehjirmhgwpxwpgfztqjwfqfaapmsgskr", "output": "need tree" }, { "input": "kobhhrqgwbgqkzcoacrhpkegyepzfds\nhlwcgbvvlegoyrcrjhsjywpdnccxtzgmeujxciuwjlnefllwldidlnjswmetkarxqjigokfvmpxpzfxarhkpdcia", "output": "need tree" }, { "input": "lllolloloolllloolollololololollllooololoooloooolololloolloollllolloolloooooooololllolllolllloolllool\nlollollololololooooloooooooooolloolllololooollllol", "output": "automaton" }, { "input": "lloloooolooollololloooloololooollooloollolllloolllllllloollollllolooloollloololollllooloooololooolol\nlooooollooolllololloollooooololollollloloollollolo", "output": "both" }, { "input": "yyyyxxxxyxyyxxxyxxyxxxyyxxxxxyyxxxyxxyxxyyyxxxyxxxyxyxyyxyyxyxxyyyxyxxyxxyxxyyxyyyyxyyyyxxxyyxyxxyyx\nyyyyxxxxyxyyxxxyxxyxxxyyxxxxxyyxxxyxxyxxyyyxxxyxxxxxyxyyxyyxyxxyyyxyxxyxxyxxyyxyyyyxyyyyxxxyyxyxxyyx", "output": "need tree" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "automaton" }, { "input": "abracadabra\nlol", "output": "need tree" }, { "input": "abc\naa", "output": "need tree" }, { "input": "ba\naa", "output": "need tree" }, { "input": "abbb\naaab", "output": "need tree" }, { "input": "baaa\nbb", "output": "need tree" }, { "input": "boosss\nosos", "output": "both" } ]
77
0
3
2,069
930
Peculiar apple-tree
[ "dfs and similar", "graphs", "trees" ]
null
null
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are *n* inflorescences, numbered from 1 to *n*. Inflorescence number 1 is situated near base of tree and any other inflorescence with number *i* (*i*<=&gt;<=1) is situated at the top of branch, which bottom is *p**i*-th inflorescence and *p**i*<=&lt;<=*i*. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in *a*-th inflorescence gets to *p**a*-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
First line of input contains single integer number *n* (2<=≀<=*n*<=≀<=100<=000) Β β€” number of inflorescences. Second line of input contains sequence of *n*<=-<=1 integer numbers *p*2,<=*p*3,<=...,<=*p**n* (1<=≀<=*p**i*<=&lt;<=*i*), where *p**i* is number of inflorescence into which the apple from *i*-th inflorescence rolls down.
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
[ "3\n1 1\n", "5\n1 2 2 2\n", "18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4\n" ]
[ "1\n", "3\n", "4\n" ]
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
[ { "input": "3\n1 1", "output": "1" }, { "input": "5\n1 2 2 2", "output": "3" }, { "input": "18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4", "output": "4" }, { "input": "2\n1", "output": "2" }, { "input": "3\n1 2", "output": "3" }, { "input": "20\n1 1 1 1 1 4 1 2 4 1 2 1 7 1 2 2 9 7 1", "output": "2" }, { "input": "20\n1 2 1 2 2 1 2 4 1 6 2 2 4 3 2 6 2 5 9", "output": "2" }, { "input": "20\n1 1 1 4 2 4 3 1 2 8 3 2 11 13 15 1 12 13 12", "output": "4" }, { "input": "20\n1 2 2 4 3 5 5 6 6 9 11 9 9 12 13 10 15 13 15", "output": "4" }, { "input": "20\n1 2 3 4 5 6 7 8 9 6 11 12 12 7 13 15 16 11 13", "output": "8" }, { "input": "10\n1 1 1 2 1 3 4 2 1", "output": "2" }, { "input": "30\n1 1 1 2 1 2 1 1 2 1 1 1 2 2 4 3 6 2 3 5 3 4 11 5 3 3 4 7 6", "output": "4" }, { "input": "40\n1 1 1 1 1 1 1 1 1 3 4 3 3 1 3 6 7 4 5 2 4 3 9 1 4 2 5 3 5 9 5 9 10 12 3 7 2 11 1", "output": "2" }, { "input": "50\n1 1 1 1 1 2 3 3 2 1 1 2 3 1 3 1 5 6 4 1 1 2 1 2 1 10 17 2 2 4 12 9 6 6 5 13 1 3 2 8 25 3 22 1 10 13 6 3 2", "output": "4" }, { "input": "10\n1 1 1 1 2 1 3 4 3", "output": "2" }, { "input": "30\n1 2 1 1 1 2 1 4 2 3 9 2 3 2 1 1 4 3 12 4 8 8 3 7 9 1 9 19 1", "output": "2" }, { "input": "40\n1 1 1 2 3 1 2 1 3 7 1 3 4 3 2 3 4 1 2 2 4 1 7 4 1 3 2 1 4 5 3 10 14 11 10 13 8 7 4", "output": "2" }, { "input": "50\n1 2 1 1 1 3 1 3 1 5 3 2 7 3 6 6 3 1 4 2 3 10 8 9 1 4 5 2 8 6 12 9 7 5 7 19 3 15 10 4 12 4 19 5 16 5 3 13 5", "output": "2" }, { "input": "10\n1 1 1 2 3 2 1 2 3", "output": "2" }, { "input": "30\n1 1 1 1 2 1 4 4 2 3 2 1 1 1 1 3 1 1 3 2 3 5 1 2 9 16 2 4 3", "output": "2" }, { "input": "40\n1 1 1 2 1 2 1 2 4 8 1 7 1 6 2 8 2 12 4 11 5 5 15 3 12 11 22 11 13 13 24 6 10 15 3 6 7 1 2", "output": "2" }, { "input": "50\n1 1 1 1 3 4 1 2 3 5 1 2 1 5 1 10 4 11 1 8 8 4 4 12 5 3 4 1 1 2 5 13 13 2 2 10 12 3 19 14 1 1 15 3 23 21 12 3 14", "output": "4" }, { "input": "10\n1 1 1 1 2 4 1 1 3", "output": "2" }, { "input": "30\n1 1 1 1 3 3 2 3 7 4 1 2 4 6 2 8 1 2 13 7 5 15 3 3 8 4 4 18 3", "output": "2" }, { "input": "40\n1 1 1 2 2 1 1 4 6 4 7 7 7 4 4 8 10 7 5 1 5 13 7 8 2 11 18 2 1 20 7 3 12 16 2 22 4 22 14", "output": "4" }, { "input": "50\n1 1 1 2 2 1 3 5 3 1 9 4 4 2 12 15 3 13 8 8 4 13 20 17 19 2 4 3 9 5 17 9 17 1 5 7 6 5 20 11 31 33 32 20 6 25 1 2 6", "output": "4" }, { "input": "10\n1 1 1 3 3 5 6 8 3", "output": "4" }, { "input": "30\n1 2 2 1 5 5 5 1 7 4 10 2 4 11 2 3 10 10 7 13 12 4 10 3 22 25 8 1 1", "output": "6" }, { "input": "40\n1 2 2 2 2 4 2 2 6 9 3 9 9 9 3 5 7 7 2 17 4 4 8 8 25 18 12 27 8 19 26 15 33 26 33 9 24 4 27", "output": "4" }, { "input": "50\n1 1 3 3 4 5 5 2 4 3 9 9 1 5 5 7 5 5 16 1 18 3 6 5 6 13 26 12 23 20 17 21 9 17 19 34 12 24 11 9 32 10 40 42 7 40 11 25 3", "output": "6" }, { "input": "10\n1 2 1 2 5 5 6 6 6", "output": "2" }, { "input": "30\n1 1 3 3 5 6 7 5 7 6 5 4 8 6 10 12 14 9 15 20 6 21 14 24 17 23 23 18 8", "output": "2" }, { "input": "40\n1 2 2 3 1 2 5 6 4 8 11 12 9 5 12 7 4 16 16 15 6 22 17 24 10 8 22 4 27 9 19 23 16 18 28 22 5 35 19", "output": "4" }, { "input": "50\n1 2 3 4 5 5 5 7 1 2 11 5 7 11 11 11 15 3 17 10 6 18 14 14 24 11 10 7 17 18 8 7 19 18 31 27 21 30 34 32 27 39 38 22 32 23 31 48 25", "output": "2" }, { "input": "10\n1 2 2 4 5 5 6 4 7", "output": "2" }, { "input": "30\n1 2 3 3 5 6 3 8 9 10 10 10 11 7 8 8 15 16 13 13 19 12 15 18 18 24 27 25 10", "output": "6" }, { "input": "40\n1 2 3 4 5 6 6 8 7 10 11 3 12 11 15 12 17 15 10 20 16 20 12 20 15 21 20 26 29 23 29 30 23 24 35 33 25 32 36", "output": "8" }, { "input": "50\n1 2 2 2 5 6 7 7 9 10 7 4 5 4 15 15 16 17 10 19 18 16 15 24 20 8 27 16 19 24 23 32 17 23 29 18 35 35 38 35 39 41 42 38 19 46 38 28 29", "output": "6" }, { "input": "10\n1 2 3 4 5 5 5 7 9", "output": "8" }, { "input": "30\n1 2 3 4 5 6 5 3 6 7 8 11 12 13 15 15 13 13 19 10 14 10 15 23 21 9 27 22 28", "output": "4" }, { "input": "40\n1 2 2 3 3 6 5 5 9 7 8 11 13 7 10 10 16 14 18 20 11 19 23 18 20 21 25 16 29 25 27 31 26 34 33 23 36 33 32", "output": "6" }, { "input": "50\n1 2 2 4 5 5 7 6 9 10 11 12 13 7 14 15 14 17 10 14 9 21 23 23 19 26 19 25 11 24 22 27 26 34 35 30 37 31 38 32 40 32 42 44 37 21 40 40 48", "output": "10" }, { "input": "10\n1 2 3 4 3 6 6 6 7", "output": "4" }, { "input": "30\n1 2 2 4 5 6 5 7 9 6 4 12 7 14 12 12 15 17 13 12 8 20 21 15 17 24 21 19 16", "output": "4" }, { "input": "40\n1 2 3 4 4 6 6 4 9 9 10 12 10 12 12 16 8 13 18 14 17 20 21 23 25 22 25 26 29 26 27 27 33 31 33 34 36 29 34", "output": "10" }, { "input": "50\n1 2 3 3 4 3 6 7 8 10 11 10 12 11 11 14 13 8 17 20 21 19 15 18 21 18 17 23 25 28 25 27 29 32 32 34 37 29 30 39 41 35 24 41 37 36 41 35 43", "output": "10" }, { "input": "99\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", "output": "1" }, { "input": "99\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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", "output": "99" }, { "input": "100\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", "output": "2" }, { "input": "100\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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", "output": "100" } ]
140
25,292,800
3
2,071
558
Amr and The Large Array
[ "implementation" ]
null
null
Amr has got a large array of size *n*. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible.
The first line contains one number *n* (1<=≀<=*n*<=≀<=105), the size of the array. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=106), representing elements of the array.
Output two integers *l*,<=*r* (1<=≀<=*l*<=≀<=*r*<=≀<=*n*), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them.
[ "5\n1 1 2 2 1\n", "5\n1 2 2 3 1\n", "6\n1 2 2 1 1 2\n" ]
[ "1 5", "2 3", "1 5" ]
A subsegment *B* of an array *A* from *l* to *r* is an array of size *r* - *l* + 1 where *B*<sub class="lower-index">*i*</sub> = *A*<sub class="lower-index">*l* + *i* - 1</sub> for all 1 ≀ *i* ≀ *r* - *l* + 1
[ { "input": "5\n1 1 2 2 1", "output": "1 5" }, { "input": "5\n1 2 2 3 1", "output": "2 3" }, { "input": "6\n1 2 2 1 1 2", "output": "1 5" }, { "input": "10\n1 1000000 2 1000000 3 2 1000000 1 2 1", "output": "2 7" }, { "input": "10\n1 2 3 4 5 5 1 2 3 4", "output": "5 6" }, { "input": "10\n1 1 2 1 1 2 2 1 2 3", "output": "1 8" }, { "input": "10\n5 4 3 2 1 1 2 3 4 5", "output": "5 6" }, { "input": "10\n1 10 100 1000 10000 1 10 100 1000 10000", "output": "1 6" }, { "input": "10\n9 9 9 9 8 9 8 8 8 8", "output": "1 6" }, { "input": "10\n1 11 111 1111 1 11 11 1 1111 1111", "output": "2 7" } ]
295
16,384,000
3
2,073
411
Kicker
[ "implementation" ]
null
null
Kicker (table football) is a board game based on football, in which players control the footballers' figures mounted on rods by using bars to get the ball into the opponent's goal. When playing two on two, one player of each team controls the goalkeeper and the full-backs (plays defence), the other player controls the half-backs and forwards (plays attack). Two teams of company Q decided to battle each other. Let's enumerate players from both teams by integers from 1 to 4. The first and second player play in the first team, the third and the fourth one play in the second team. For each of the four players we know their game skills in defence and attack. The defence skill of the *i*-th player is *a**i*, the attack skill is *b**i*. Before the game, the teams determine how they will play. First the players of the first team decide who will play in the attack, and who will play in the defence. Then the second team players do the same, based on the choice of their opponents. We will define a team's defence as the defence skill of player of the team who plays defence. Similarly, a team's attack is the attack skill of the player of the team who plays attack. We assume that one team is guaranteed to beat the other one, if its defence is strictly greater than the opponent's attack and its attack is strictly greater than the opponent's defence. The teams of company Q know each other's strengths and therefore arrange their teams optimally. Identify the team that is guaranteed to win (if both teams act optimally) or tell that there is no such team.
The input contain the players' description in four lines. The *i*-th line contains two space-separated integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=100) β€” the defence and the attack skill of the *i*-th player, correspondingly.
If the first team can win, print phrase "Team 1" (without the quotes), if the second team can win, print phrase "Team 2" (without the quotes). If no of the teams can definitely win, print "Draw" (without the quotes).
[ "1 100\n100 1\n99 99\n99 99\n", "1 1\n2 2\n3 3\n2 2\n", "3 3\n2 2\n1 1\n2 2\n" ]
[ "Team 1\n", "Team 2\n", "Draw\n" ]
Let consider the first test sample. The first team can definitely win if it will choose the following arrangement: the first player plays attack, the second player plays defence. Consider the second sample. The order of the choosing roles for players makes sense in this sample. As the members of the first team choose first, the members of the second team can beat them (because they know the exact defence value and attack value of the first team).
[ { "input": "1 100\n100 1\n99 99\n99 99", "output": "Team 1" }, { "input": "1 1\n2 2\n3 3\n2 2", "output": "Team 2" }, { "input": "3 3\n2 2\n1 1\n2 2", "output": "Draw" }, { "input": "80 79\n79 30\n80 81\n40 80", "output": "Team 2" }, { "input": "10 10\n4 9\n8 9\n7 6", "output": "Team 1" }, { "input": "10 2\n9 3\n3 1\n9 4", "output": "Draw" }, { "input": "6 3\n6 10\n2 5\n4 4", "output": "Team 1" }, { "input": "8 7\n1 5\n7 4\n8 8", "output": "Draw" }, { "input": "2 7\n8 4\n4 6\n10 8", "output": "Draw" }, { "input": "8 3\n4 9\n6 1\n5 6", "output": "Team 1" }, { "input": "10 5\n3 1\n1 9\n1 2", "output": "Draw" }, { "input": "6 5\n10 6\n8 1\n3 2", "output": "Draw" }, { "input": "6 2\n7 5\n5 4\n8 6", "output": "Draw" }, { "input": "1 10\n1 10\n1 1\n7 8", "output": "Draw" }, { "input": "16 7\n9 3\n11 2\n11 4", "output": "Draw" }, { "input": "20 17\n14 10\n10 7\n19 18", "output": "Draw" }, { "input": "12 7\n3 17\n4 15\n2 8", "output": "Draw" }, { "input": "8 14\n8 12\n7 20\n14 6", "output": "Draw" }, { "input": "4 4\n4 15\n2 4\n10 12", "output": "Draw" }, { "input": "4 10\n9 9\n9 12\n13 10", "output": "Team 2" }, { "input": "20 20\n18 8\n15 5\n17 20", "output": "Draw" }, { "input": "12 10\n7 3\n10 5\n1 14", "output": "Draw" }, { "input": "8 16\n12 10\n13 18\n8 4", "output": "Draw" }, { "input": "16 15\n19 1\n16 16\n20 9", "output": "Draw" }, { "input": "12 29\n44 8\n18 27\n43 19", "output": "Draw" }, { "input": "28 46\n50 27\n23 50\n21 45", "output": "Draw" }, { "input": "40 6\n9 1\n16 18\n4 23", "output": "Draw" }, { "input": "4 16\n6 28\n12 32\n28 3", "output": "Draw" }, { "input": "16 22\n11 3\n17 5\n12 27", "output": "Draw" }, { "input": "32 32\n10 28\n14 23\n39 5", "output": "Draw" }, { "input": "48 41\n15 47\n11 38\n19 31", "output": "Team 1" }, { "input": "8 9\n11 17\n11 6\n5 9", "output": "Draw" }, { "input": "24 19\n18 44\n8 29\n30 39", "output": "Draw" }, { "input": "22 4\n29 38\n31 43\n47 21", "output": "Team 2" }, { "input": "51 54\n95 28\n42 28\n17 48", "output": "Team 1" }, { "input": "11 64\n92 47\n88 93\n41 26", "output": "Draw" }, { "input": "27 74\n97 22\n87 65\n24 52", "output": "Draw" }, { "input": "43 32\n49 48\n42 33\n60 30", "output": "Draw" }, { "input": "55 50\n54 23\n85 6\n32 60", "output": "Team 2" }, { "input": "19 56\n59 46\n40 70\n67 34", "output": "Team 2" }, { "input": "31 67\n8 13\n86 91\n43 12", "output": "Team 2" }, { "input": "47 77\n13 88\n33 63\n75 38", "output": "Draw" }, { "input": "59 35\n10 14\n88 23\n58 16", "output": "Draw" }, { "input": "63 4\n18 60\n58 76\n44 93", "output": "Draw" }, { "input": "14 47\n47 42\n21 39\n40 7", "output": "Team 1" }, { "input": "67 90\n63 36\n79 56\n25 56", "output": "Team 1" }, { "input": "64 73\n59 46\n8 19\n57 18", "output": "Team 1" }, { "input": "23 80\n62 56\n56 31\n9 50", "output": "Team 1" }, { "input": "86 95\n86 38\n59 66\n44 78", "output": "Team 1" }, { "input": "10 3\n2 5\n1 10\n2 10", "output": "Draw" }, { "input": "62 11\n79 14\n46 36\n91 52", "output": "Draw" }, { "input": "8 4\n9 10\n7 3\n6 5", "output": "Team 1" }, { "input": "21 12\n29 28\n16 4\n10 1", "output": "Team 1" }, { "input": "91 71\n87 45\n28 73\n9 48", "output": "Team 1" }, { "input": "4 1\n4 3\n6 4\n2 8", "output": "Team 2" }, { "input": "11 7\n12 8\n15 14\n14 14", "output": "Team 2" }, { "input": "12 7\n3 15\n20 18\n20 8", "output": "Team 2" }, { "input": "4 7\n24 11\n17 30\n21 4", "output": "Team 2" }, { "input": "21 22\n21 16\n32 14\n39 35", "output": "Team 2" }, { "input": "16 48\n16 49\n10 68\n60 64", "output": "Team 2" }, { "input": "46 33\n12 3\n11 67\n98 77", "output": "Team 2" }, { "input": "19 9\n47 28\n83 41\n76 14", "output": "Draw" }, { "input": "36 68\n65 82\n37 6\n21 60", "output": "Team 1" }, { "input": "70 98\n62 5\n30 50\n66 96", "output": "Draw" }, { "input": "45 69\n91 96\n72 67\n24 30", "output": "Draw" }, { "input": "34 38\n91 17\n2 12\n83 90", "output": "Draw" }, { "input": "30 31\n98 15\n40 62\n10 22", "output": "Draw" } ]
140
0
3
2,080
319
Kalila and Dimna in the Logging Industry
[ "dp", "geometry" ]
null
null
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut *n* trees with heights *a*1,<=*a*2,<=...,<=*a**n*. They bought a chain saw from a shop. Each time they use the chain saw on the tree number *i*, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is *i* (the tree that have height *a**i* in the beginning), then the cost of charging the chain saw would be *b**i*. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each *i* &lt; *j*, *a**i*<=&lt;<=*a**j* and *b**i*<=&gt;<=*b**j* and also *b**n*<==<=0 and *a*1<==<=1. Kalila and Dimna want to cut all the trees completely, with minimum cost. They want you to help them! Will you?
The first line of input contains an integer *n* (1<=≀<=*n*<=≀<=105). The second line of input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109). The third line of input contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (0<=≀<=*b**i*<=≀<=109). It's guaranteed that *a*1<==<=1, *b**n*<==<=0, *a*1<=&lt;<=*a*2<=&lt;<=...<=&lt;<=*a**n* and *b*1<=&gt;<=*b*2<=&gt;<=...<=&gt;<=*b**n*.
The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "5\n1 2 3 4 5\n5 4 3 2 0\n", "6\n1 2 3 10 20 30\n6 5 4 3 2 0\n" ]
[ "25\n", "138\n" ]
none
[ { "input": "5\n1 2 3 4 5\n5 4 3 2 0", "output": "25" }, { "input": "6\n1 2 3 10 20 30\n6 5 4 3 2 0", "output": "138" } ]
156
2,048,000
-1
2,087
688
Opponents
[ "implementation" ]
null
null
Arya has *n* opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya will beat all present opponents. Otherwise, if all opponents are present, then they will beat Arya. For each opponent Arya knows his scheduleΒ β€” whether or not he is going to present on each particular day. Tell him the maximum number of consecutive days that he will beat all present opponents. Note, that if some day there are no opponents present, Arya still considers he beats all the present opponents.
The first line of the input contains two integers *n* and *d* (1<=≀<=*n*,<=*d*<=≀<=100)Β β€” the number of opponents and the number of days, respectively. The *i*-th of the following *d* lines contains a string of length *n* consisting of characters '0' and '1'. The *j*-th character of this string is '0' if the *j*-th opponent is going to be absent on the *i*-th day.
Print the only integerΒ β€” the maximum number of consecutive days that Arya will beat all present opponents.
[ "2 2\n10\n00\n", "4 1\n0100\n", "4 5\n1101\n1111\n0110\n1011\n1111\n" ]
[ "2\n", "1\n", "2\n" ]
In the first and the second samples, Arya will beat all present opponents each of the *d* days. In the third sample, Arya will beat his opponents on days 1, 3 and 4 and his opponents will beat him on days 2 and 5. Thus, the maximum number of consecutive winning days is 2, which happens on days 3 and 4.
[ { "input": "2 2\n10\n00", "output": "2" }, { "input": "4 1\n0100", "output": "1" }, { "input": "4 5\n1101\n1111\n0110\n1011\n1111", "output": "2" }, { "input": "3 2\n110\n110", "output": "2" }, { "input": "10 6\n1111111111\n0100110101\n1111111111\n0000011010\n1111111111\n1111111111", "output": "1" }, { "input": "10 10\n1111111111\n0001001000\n1111111111\n1111111111\n1111111111\n1000000100\n1111111111\n0000011100\n1111111111\n1111111111", "output": "1" }, { "input": "10 10\n0000100011\n0100001111\n1111111111\n1100011111\n1111111111\n1000111000\n1111000010\n0111001001\n1101010110\n1111111111", "output": "4" }, { "input": "10 10\n1100110010\n0000000001\n1011100111\n1111111111\n1111111111\n1111111111\n1100010110\n1111111111\n1001001010\n1111111111", "output": "3" }, { "input": "10 7\n0000111001\n1111111111\n0110110001\n1111111111\n1111111111\n1000111100\n0110000111", "output": "2" }, { "input": "5 10\n00110\n11000\n10010\n00010\n11110\n01101\n11111\n10001\n11111\n01001", "output": "6" }, { "input": "5 9\n11111\n11101\n11111\n11111\n01010\n01010\n00000\n11111\n00111", "output": "3" }, { "input": "5 10\n11111\n00010\n11010\n11111\n11111\n00100\n11111\n11111\n01000\n11111", "output": "2" }, { "input": "5 9\n11111\n11111\n11111\n11111\n11100\n11111\n11111\n11111\n00000", "output": "1" }, { "input": "5 8\n11111\n10110\n01001\n11111\n01100\n10010\n11111\n11111", "output": "2" }, { "input": "1 1\n1", "output": "0" }, { "input": "100 1\n0011001100100010000011001100000001011101110110010001110001101100110011111101001011011001000010001111", "output": "1" }, { "input": "100 1\n1011011100000101000111110000110111010101110010010011110010001110100011001110110101111100100110000000", "output": "1" }, { "input": "100 1\n1110000011110101010111111100011001100000101101010110100101110000011100110110110101011100110110010011", "output": "1" }, { "input": "100 1\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", "output": "0" }, { "input": "1 100\n1\n0\n0\n0\n1\n1\n0\n0\n0\n0\n1\n1\n0\n1\n1\n0\n0\n1\n1\n1\n0\n0\n1\n1\n1\n1\n1\n0\n1\n0\n0\n0\n1\n1\n0\n1\n0\n1\n0\n0\n0\n1\n0\n1\n0\n0\n0\n1\n1\n1\n0\n1\n1\n1\n0\n1\n0\n1\n1\n1\n1\n0\n0\n0\n0\n0\n0\n1\n1\n0\n1\n1\n1\n1\n1\n0\n1\n1\n1\n1\n1\n0\n1\n0\n0\n1\n0\n0\n1\n0\n0\n1\n0\n1\n1\n1\n0\n1\n0\n0", "output": "6" }, { "input": "1 100\n0\n0\n0\n0\n1\n0\n0\n0\n0\n1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0", "output": "49" }, { "input": "1 100\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\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", "output": "0" }, { "input": "1 100\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0", "output": "100" }, { "input": "2 2\n11\n10", "output": "1" }, { "input": "1 1\n0", "output": "1" } ]
109
0
3
2,093
1,004
Sonya and Robots
[ "constructive algorithms", "implementation" ]
null
null
Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers. Sonya has drawn $n$ numbers in a row, $a_i$ is located in the $i$-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sonya will give a number to each robot (they can be either same or different) and run them. When a robot is running, it is moving toward to another robot, reading numbers in the row. When a robot is reading a number that is equal to the number that was given to that robot, it will turn off and stay in the same position. Sonya does not want robots to break, so she will give such numbers that robots will stop before they meet. That is, the girl wants them to stop at different positions so that the first robot is to the left of the second one. For example, if the numbers $[1, 5, 4, 1, 3]$ are written, and Sonya gives the number $1$ to the first robot and the number $4$ to the second one, the first robot will stop in the $1$-st position while the second one in the $3$-rd position. In that case, robots will not meet each other. As a result, robots will not be broken. But if Sonya gives the number $4$ to the first robot and the number $5$ to the second one, they will meet since the first robot will stop in the $3$-rd position while the second one is in the $2$-nd position. Sonya understands that it does not make sense to give a number that is not written in the row because a robot will not find this number and will meet the other robot. Sonya is now interested in finding the number of different pairs that she can give to robots so that they will not meet. In other words, she wants to know the number of pairs ($p$, $q$), where she will give $p$ to the first robot and $q$ to the second one. Pairs ($p_i$, $q_i$) and ($p_j$, $q_j$) are different if $p_i\neq p_j$ or $q_i\neq q_j$. Unfortunately, Sonya is busy fixing robots that broke after a failed launch. That is why she is asking you to find the number of pairs that she can give to robots so that they will not meet.
The first line contains a single integer $n$ ($1\leq n\leq 10^5$)Β β€” the number of numbers in a row. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1\leq a_i\leq 10^5$)Β β€” the numbers in a row.
Print one numberΒ β€” the number of possible pairs that Sonya can give to robots so that they will not meet.
[ "5\n1 5 4 1 3\n", "7\n1 2 1 1 1 3 2\n" ]
[ "9\n", "7\n" ]
In the first example, Sonya can give pairs ($1$, $1$), ($1$, $3$), ($1$, $4$), ($1$, $5$), ($4$, $1$), ($4$, $3$), ($5$, $1$), ($5$, $3$), and ($5$, $4$). In the second example, Sonya can give pairs ($1$, $1$), ($1$, $2$), ($1$, $3$), ($2$, $1$), ($2$, $2$), ($2$, $3$), and ($3$, $2$).
[ { "input": "5\n1 5 4 1 3", "output": "9" }, { "input": "7\n1 2 1 1 1 3 2", "output": "7" }, { "input": "10\n2 2 4 4 3 1 1 2 3 2", "output": "14" }, { "input": "15\n1 2 2 1 2 4 2 1 1 6 6 4 2 5 4", "output": "20" }, { "input": "1\n1", "output": "0" } ]
233
12,595,200
3
2,099
965
Battleship
[ "implementation" ]
null
null
Arkady is playing Battleship. The rules of this game aren't really important. There is a field of $n \times n$ cells. There should be exactly one $k$-decker on the field, i.Β e. a ship that is $k$ cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship. Consider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship.
The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 100$)Β β€” the size of the field and the size of the ship. The next $n$ lines contain the field. Each line contains $n$ characters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship).
Output two integersΒ β€” the row and the column of a cell that belongs to the maximum possible number of different locations of the ship. If there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell.
[ "4 3\n#..#\n#.#.\n....\n.###\n", "10 4\n#....##...\n.#...#....\n..#..#..#.\n...#.#....\n.#..##.#..\n.....#...#\n...#.##...\n.#...#.#..\n.....#..#.\n...#.#...#\n", "19 6\n##..............###\n#......#####.....##\n.....#########.....\n....###########....\n...#############...\n..###############..\n.#################.\n.#################.\n.#################.\n.#################.\n#####....##....####\n####............###\n####............###\n#####...####...####\n.#####..####..#####\n...###........###..\n....###########....\n.........##........\n#.................#\n" ]
[ "3 2\n", "6 1\n", "1 8\n" ]
The picture below shows the three possible locations of the ship that contain the cell $(3, 2)$ in the first sample.
[ { "input": "4 3\n#..#\n#.#.\n....\n.###", "output": "3 2" }, { "input": "10 4\n#....##...\n.#...#....\n..#..#..#.\n...#.#....\n.#..##.#..\n.....#...#\n...#.##...\n.#...#.#..\n.....#..#.\n...#.#...#", "output": "6 1" }, { "input": "19 6\n##..............###\n#......#####.....##\n.....#########.....\n....###########....\n...#############...\n..###############..\n.#################.\n.#################.\n.#################.\n.#################.\n#####....##....####\n####............###\n####............###\n#####...####...####\n.#####..####..#####\n...###........###..\n....###########....\n.........##........\n#.................#", "output": "1 8" }, { "input": "10 4\n##..######\n#...######\n#...######\n#......###\n#.......##\n.##.######\n.##.######\n.##.######\n.#....####\n....######", "output": "4 4" }, { "input": "1 1\n.", "output": "1 1" }, { "input": "1 1\n#", "output": "1 1" }, { "input": "5 2\n..##.\n.###.\n#####\n#####\n..#..", "output": "1 1" }, { "input": "5 2\n..##.\n####.\n#####\n.####\n..#..", "output": "5 1" }, { "input": "5 2\n..##.\n####.\n#####\n####.\n..#..", "output": "5 5" }, { "input": "5 2\n.##..\n.###.\n#####\n#####\n..#..", "output": "1 5" }, { "input": "2 2\n##\n##", "output": "1 1" }, { "input": "4 1\n####\n####\n####\n###.", "output": "4 4" }, { "input": "2 2\n#.\n.#", "output": "1 1" }, { "input": "3 3\n###\n##.\n###", "output": "1 1" }, { "input": "4 4\n####\n####\n####\n####", "output": "1 1" }, { "input": "4 3\n####\n####\n####\n####", "output": "1 1" }, { "input": "3 1\n###\n###\n###", "output": "1 1" }, { "input": "3 2\n###\n###\n###", "output": "1 1" }, { "input": "3 3\n.#.\n#.#\n.#.", "output": "1 1" } ]
233
24,576,000
3
2,106
152
Steps
[ "binary search", "implementation" ]
null
null
One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular *n*<=Γ—<=*m* field. The squares have coordinates (*x*,<=*y*) (1<=≀<=*x*<=≀<=*n*,<=1<=≀<=*y*<=≀<=*m*), where *x* is the index of the row and *y* is the index of the column. Initially Vasya stands in the square with coordinates (*x**c*,<=*y**c*). To play, he has got a list of *k* vectors (*dx**i*,<=*dy**i*) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to *k*, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (*x*,<=*y*), and the current vector is (*dx*,<=*dy*), one step moves Vasya to square (*x*<=+<=*dx*,<=*y*<=+<=*dy*). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made.
The first input line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=109) β€” the yard's sizes. The second line contains integers *x**c* and *y**c* β€” the initial square's coordinates (1<=≀<=*x**c*<=≀<=*n*,<=1<=≀<=*y**c*<=≀<=*m*). The third line contains an integer *k* (1<=≀<=*k*<=≀<=104) β€” the number of vectors. Then follow *k* lines, each of them contains two integers *dx**i* and *dy**i* (|*dx**i*|,<=|*dy**i*|<=≀<=109,<=|*dx*|<=+<=|*dy*|<=β‰₯<=1).
Print the single number β€” the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
[ "4 5\n1 1\n3\n1 1\n1 1\n0 -2\n", "10 10\n1 2\n1\n-1 0\n" ]
[ "4\n", "0\n" ]
In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0,  - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
[ { "input": "4 5\n1 1\n3\n1 1\n1 1\n0 -2", "output": "4" }, { "input": "10 10\n1 2\n1\n-1 0", "output": "0" }, { "input": "10 20\n10 3\n10\n-2 -6\n-1 0\n-8 0\n0 5\n-1 3\n16 -16\n-1 9\n0 -18\n9 -1\n-9 5", "output": "13" }, { "input": "20 10\n14 4\n10\n6 0\n-7 -7\n12 -2\n-4 9\n20 3\n-1 -16\n0 2\n-1 1\n20 0\n-1 1", "output": "4" }, { "input": "1000 2000\n226 566\n20\n0 -100\n-100 100\n100 0\n42 0\n-100 -79\n-66 -16\n0 -7\n-1 0\n0 100\n100 91\n99 0\n1 0\n-100 0\n70 -100\n-100 100\n100 1\n66 0\n-100 1\n-47 -100\n-42 0", "output": "708" }, { "input": "1 1\n1 1\n1\n1 1", "output": "0" }, { "input": "3 3\n2 2\n1\n2 0", "output": "0" } ]
60
0
0
2,107
952
Quirky Quantifiers
[ "math" ]
null
null
The input contains a single integer *a* (10<=≀<=*a*<=≀<=999). Output 0 or 1.
The input contains a single integer *a* (10<=≀<=*a*<=≀<=999).
Output 0 or 1.
[ "13\n", "927\n", "48\n" ]
[ "1\n", "1\n", "0\n" ]
none
[ { "input": "13", "output": "1" }, { "input": "927", "output": "1" }, { "input": "48", "output": "0" }, { "input": "10", "output": "0" }, { "input": "999", "output": "1" }, { "input": "142", "output": "0" }, { "input": "309", "output": "1" }, { "input": "572", "output": "0" }, { "input": "835", "output": "1" }, { "input": "990", "output": "0" }, { "input": "168", "output": "0" }, { "input": "431", "output": "1" }, { "input": "694", "output": "0" }, { "input": "957", "output": "1" }, { "input": "932", "output": "0" } ]
77
0
0
2,108
525
Pasha and String
[ "constructive algorithms", "greedy", "math", "strings" ]
null
null
Pasha got a very beautiful string *s* for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |*s*| from left to right, where |*s*| is the length of the given string. Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent *m* days performing the following transformations on his stringΒ β€”Β each day he chose integer *a**i* and reversed a piece of string (a segment) from position *a**i* to position |*s*|<=-<=*a**i*<=+<=1. It is guaranteed that 2Β·*a**i*<=≀<=|*s*|. You face the following task: determine what Pasha's string will look like after *m* days.
The first line of the input contains Pasha's string *s* of length from 2 to 2Β·105 characters, consisting of lowercase Latin letters. The second line contains a single integer *m* (1<=≀<=*m*<=≀<=105)Β β€”Β  the number of days when Pasha changed his string. The third line contains *m* space-separated elements *a**i* (1<=≀<=*a**i*; 2Β·*a**i*<=≀<=|*s*|)Β β€”Β the position from which Pasha started transforming the string on the *i*-th day.
In the first line of the output print what Pasha's string *s* will look like after *m* days.
[ "abcdef\n1\n2\n", "vwxyz\n2\n2 2\n", "abcdef\n3\n1 2 3\n" ]
[ "aedcbf\n", "vwxyz\n", "fbdcea\n" ]
none
[ { "input": "abcdef\n1\n2", "output": "aedcbf" }, { "input": "vwxyz\n2\n2 2", "output": "vwxyz" }, { "input": "abcdef\n3\n1 2 3", "output": "fbdcea" }, { "input": "jc\n5\n1 1 1 1 1", "output": "cj" }, { "input": "wljqgdlxyc\n13\n3 4 3 3 5 4 4 2 4 4 5 3 3", "output": "wyjldgqxlc" }, { "input": "keicnqmuqinhsmtudqcilocxkbqgzhbkitmqwttdyoyvcbxincwjryzknubpacsngorexaldfurondbednowemnnlphhboycfavs\n2\n5 12", "output": "keiccyobhhphsmtudqcilocxkbqgzhbkitmqwttdyoyvcbxincwjryzknubpacsngorexaldfurondbednowemnnlniqumqnfavs" }, { "input": "xwcxggxvfqbdklewbxkjzibmufnaywuxsqvwakefxbbkfandvigasbhbatsxyqxicrosatfsfybedklsaztyyiuurfbrzmwumujy\n100\n14 43 30 13 8 19 33 7 8 14 15 35 5 18 44 1 35 1 18 7 50 47 9 49 28 29 39 37 27 17 19 12 5 24 37 42 37 23 35 31 10 26 5 38 40 34 42 47 2 40 43 34 16 25 14 45 35 38 46 48 49 27 49 38 10 49 5 7 3 3 41 25 24 34 37 33 17 50 48 11 40 43 48 10 9 50 18 39 32 13 26 40 37 16 45 50 27 3 7 31", "output": "xjcxggxvfbbruliyyxkjzikdebnfyftxsorcaxqyxbtkfhbdvigasnababsxfekiwvqsauwsayfumblsaztbweukdfqrzmwumuwy" } ]
233
16,384,000
3
2,109
852
Bathroom terminal
[ "implementation" ]
null
null
Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to pipes. Next to him is tape-player with a hand-written message "Play Me". He finds a tape in his own back pocket. After putting the tape in the tape-player, he sees a key hanging from a ceiling, chained to some kind of a machine, which is connected to the terminal next to him. After pressing a Play button a rough voice starts playing from the tape: "Listen up Smith. As you can see, you are in pretty tough situation and in order to escape, you have to solve a puzzle. You are given *N* strings which represent words. Each word is of the maximum length *L* and consists of characters 'a'-'e'. You are also given *M* strings which represent patterns. Pattern is a string of length <=≀<= *L* and consists of characters 'a'-'e' as well as the maximum 3 characters '?'. Character '?' is an unknown character, meaning it can be equal to any character 'a'-'e', or even an empty character. For each pattern find the number of words that matches with the given pattern. After solving it and typing the result in the terminal, the key will drop from the ceiling and you may escape. Let the game begin." Help Smith escape.
The first line of input contains two integers *N* and *M* (1<=≀<=*N*<=≀<= 100 000, 1<=≀<=*M*<=≀<= 5000), representing the number of words and patterns respectively. The next *N* lines represent each word, and after those *N* lines, following *M* lines represent each pattern. Each word and each pattern has a maximum length *L* (1<=≀<=*L*<=≀<=50). Each pattern has no more that three characters '?'. All other characters in words and patters are lowercase English letters from 'a' to 'e'.
Output contains *M* lines and each line consists of one integer, representing the number of words that match the corresponding pattern.
[ "3 1\nabc\naec\nac\na?c\n" ]
[ "3\n" ]
If we switch '?' with 'b', 'e' and with empty character, we get 'abc', 'aec' and 'ac' respectively.
[ { "input": "3 1\nabc\naec\nac\na?c", "output": "3" }, { "input": "22 2\naaaab\naaabb\naabab\naabbb\nabaab\nababb\nabbab\nabbbb\naaab\naabb\nabab\nabbb\naab\nabb\nab\ncccd\nccdd\ncdcd\ncddd\nccd\ncdd\ncd\na???b\nc??d", "output": "15\n7" }, { "input": "15 6\naaa\naaabbb\naaabb\naaaaa\naaaaaa\naaaa\naaabbbb\naaaaa\naaaaaa\naaaa\naaabbbb\naabbbb\naa\naa\naab\na\n?a?\n??\n?aa?bb?\n?aa?aa?\n??aaa?", "output": "0\n4\n2\n5\n6\n7" } ]
46
0
0
2,112
313
Ilya and Queries
[ "dp", "implementation" ]
null
null
Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam. You've got string *s*<==<=*s*1*s*2... *s**n* (*n* is the length of the string), consisting only of characters "." and "#" and *m* queries. Each query is described by a pair of integers *l**i*,<=*r**i* (1<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=*n*). The answer to the query *l**i*,<=*r**i* is the number of such integers *i* (*l**i*<=≀<=*i*<=&lt;<=*r**i*), that *s**i*<==<=*s**i*<=+<=1. Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.
The first line contains string *s* of length *n* (2<=≀<=*n*<=≀<=105). It is guaranteed that the given string only consists of characters "." and "#". The next line contains integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of queries. Each of the next *m* lines contains the description of the corresponding query. The *i*-th line contains integers *l**i*,<=*r**i* (1<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=*n*).
Print *m* integers β€” the answers to the queries in the order in which they are given in the input.
[ "......\n4\n3 4\n2 3\n1 6\n2 6\n", "#..###\n5\n1 3\n5 6\n1 5\n3 6\n3 4\n" ]
[ "1\n1\n5\n4\n", "1\n1\n2\n2\n0\n" ]
none
[ { "input": "......\n4\n3 4\n2 3\n1 6\n2 6", "output": "1\n1\n5\n4" }, { "input": "#..###\n5\n1 3\n5 6\n1 5\n3 6\n3 4", "output": "1\n1\n2\n2\n0" }, { "input": ".#...#..\n6\n1 5\n2 3\n6 7\n2 4\n2 5\n1 3", "output": "2\n0\n0\n1\n2\n0" }, { "input": "#.#.#..\n5\n3 4\n4 5\n5 7\n5 7\n1 3", "output": "0\n0\n1\n1\n0" }, { "input": "#.##.##.\n7\n1 8\n2 6\n2 6\n6 8\n3 5\n2 4\n2 5", "output": "2\n1\n1\n1\n1\n1\n1" }, { "input": "#..#\n1\n1 4", "output": "1" }, { "input": "#..##...#.\n7\n5 9\n6 10\n1 7\n5 8\n3 5\n2 10\n3 4", "output": "2\n2\n3\n2\n1\n4\n0" }, { "input": "#.#.#\n7\n1 2\n3 4\n3 5\n2 3\n3 5\n1 5\n1 3", "output": "0\n0\n0\n0\n0\n0\n0" }, { "input": "###..#...#\n2\n2 4\n1 2", "output": "1\n1" }, { "input": "..\n1\n1 2", "output": "1" }, { "input": "##\n1\n1 2", "output": "1" }, { "input": ".#\n1\n1 2", "output": "0" }, { "input": "#.\n1\n1 2", "output": "0" }, { "input": "...\n2\n1 2\n1 2", "output": "1\n1" } ]
2,000
1,740,800
0
2,113
447
DZY Loves Hash
[ "implementation" ]
null
null
DZY has a hash table with *p* buckets, numbered from 0 to *p*<=-<=1. He wants to insert *n* numbers, in the order they are given, into the hash table. For the *i*-th number *x**i*, DZY will put it into the bucket numbered *h*(*x**i*), where *h*(*x*) is the hash function. In this problem we will assume, that *h*(*x*)<==<=*x*Β *mod*Β *p*. Operation *a*Β *mod*Β *b* denotes taking a remainder after division *a* by *b*. However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after the *i*-th insertion, you should output *i*. If no conflict happens, just output -1.
The first line contains two integers, *p* and *n* (2<=≀<=*p*,<=*n*<=≀<=300). Then *n* lines follow. The *i*-th of them contains an integer *x**i* (0<=≀<=*x**i*<=≀<=109).
Output a single integer β€” the answer to the problem.
[ "10 5\n0\n21\n53\n41\n53\n", "5 5\n0\n1\n2\n3\n4\n" ]
[ "4\n", "-1\n" ]
none
[ { "input": "10 5\n0\n21\n53\n41\n53", "output": "4" }, { "input": "5 5\n0\n1\n2\n3\n4", "output": "-1" }, { "input": "10 6\n811966798\n734823552\n790326404\n929189974\n414343256\n560346537", "output": "4" }, { "input": "2 2\n788371161\n801743052", "output": "-1" }, { "input": "10 6\n812796223\n122860157\n199259103\n597650585\n447742024\n521549402", "output": "3" }, { "input": "300 2\n822454942\n119374431", "output": "-1" }, { "input": "300 2\n823284367\n507345500", "output": "-1" }, { "input": "2 5\n791579811\n35613889\n997079893\n878677665\n693717467", "output": "2" }, { "input": "20 5\n793926268\n28931770\n842870287\n974950617\n859404206", "output": "-1" }, { "input": "100 15\n805069249\n778178198\n633897389\n844316223\n146759898\n870527016\n82668754\n42060733\n943602929\n979451110\n746979598\n47406033\n607284062\n850870259\n229415316", "output": "5" }, { "input": "100 15\n806204335\n189490323\n718805086\n716787474\n262315718\n822030596\n894644222\n724054623\n141384399\n579354205\n192622443\n672556242\n97417563\n243354557\n208957882", "output": "8" }, { "input": "100 15\n807033760\n577461392\n275221433\n532633429\n295714486\n783298996\n255799943\n99107143\n729119412\n59302896\n37640015\n313610861\n630550567\n534283052\n681062462", "output": "8" }, { "input": "100 15\n808103310\n136224397\n360129131\n405104681\n263786657\n734802577\n67808179\n928584682\n926900882\n511722343\n483348395\n938695534\n120684068\n74152694\n808088675", "output": "9" }, { "input": "2 2\n2\n2", "output": "2" }, { "input": "300 2\n0\n300", "output": "2" }, { "input": "2 2\n0\n0", "output": "2" } ]
124
0
3
2,130
812
Sagheer and Crossroads
[ "implementation" ]
null
null
Sagheer is walking in the street when he comes to an intersection of two roads. Each road can be represented as two parts where each part has 3 lanes getting into the intersection (one for each direction) and 3 lanes getting out of the intersection, so we have 4 parts in total. Each part has 4 lights, one for each lane getting into the intersection (*l* β€” left, *s* β€” straight, *r* β€” right) and a light *p* for a pedestrian crossing. An accident is possible if a car can hit a pedestrian. This can happen if the light of a pedestrian crossing of some part and the light of a lane that can get to or from that same part are green at the same time. Now, Sagheer is monitoring the configuration of the traffic lights. Your task is to help him detect whether an accident is possible.
The input consists of four lines with each line describing a road part given in a counter-clockwise order. Each line contains four integers *l*, *s*, *r*, *p* β€” for the left, straight, right and pedestrian lights, respectively. The possible values are 0 for red light and 1 for green light.
On a single line, print "YES" if an accident is possible, and "NO" otherwise.
[ "1 0 0 1\n0 1 0 0\n0 0 1 0\n0 0 0 1\n", "0 1 1 0\n1 0 1 0\n1 1 0 0\n0 0 0 1\n", "1 0 0 0\n0 0 0 1\n0 0 0 0\n1 0 1 0\n" ]
[ "YES\n", "NO\n", "NO\n" ]
In the first example, some accidents are possible because cars of part 1 can hit pedestrians of parts 1 and 4. Also, cars of parts 2 and 3 can hit pedestrians of part 4. In the second example, no car can pass the pedestrian crossing of part 4 which is the only green pedestrian light. So, no accident can occur.
[ { "input": "1 0 0 1\n0 1 0 0\n0 0 1 0\n0 0 0 1", "output": "YES" }, { "input": "0 1 1 0\n1 0 1 0\n1 1 0 0\n0 0 0 1", "output": "NO" }, { "input": "1 0 0 0\n0 0 0 1\n0 0 0 0\n1 0 1 0", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 1\n0 0 0 1\n0 0 0 1", "output": "NO" }, { "input": "1 1 1 0\n0 1 0 1\n1 1 1 0\n1 1 1 1", "output": "YES" }, { "input": "0 1 1 0\n0 1 0 0\n1 0 0 1\n1 0 0 0", "output": "YES" }, { "input": "1 0 0 0\n0 1 0 0\n1 1 0 0\n0 1 1 0", "output": "NO" }, { "input": "0 0 0 0\n0 1 0 1\n1 0 1 1\n1 1 1 0", "output": "YES" }, { "input": "1 1 0 0\n0 1 0 1\n1 1 1 0\n0 0 1 1", "output": "YES" }, { "input": "0 1 0 0\n0 0 0 0\n1 0 0 0\n0 0 0 1", "output": "NO" }, { "input": "0 0 1 0\n0 0 0 0\n1 1 0 0\n0 0 0 1", "output": "NO" }, { "input": "0 0 1 0\n0 1 0 1\n1 0 1 0\n0 0 1 0", "output": "YES" }, { "input": "1 1 1 0\n0 1 0 1\n1 1 1 1\n0 0 0 1", "output": "YES" }, { "input": "0 0 1 0\n0 0 0 0\n0 0 0 1\n0 0 0 1", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 1\n0 0 0 1\n0 0 0 1", "output": "NO" }, { "input": "0 0 0 0\n0 1 0 1\n1 0 1 1\n0 0 0 1", "output": "YES" }, { "input": "1 1 0 0\n0 1 0 0\n1 1 1 0\n1 0 1 0", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 0 1\n0 0 0 1", "output": "NO" }, { "input": "1 0 1 0\n1 1 0 0\n1 1 0 0\n0 0 0 0", "output": "NO" }, { "input": "0 0 1 0\n1 1 0 0\n1 0 1 0\n1 0 0 0", "output": "NO" }, { "input": "0 0 1 0\n1 0 0 0\n0 0 0 1\n0 0 0 1", "output": "NO" }, { "input": "0 1 1 0\n1 1 0 1\n1 0 0 1\n1 1 1 0", "output": "YES" }, { "input": "1 0 0 0\n1 1 0 0\n1 1 0 1\n0 0 1 0", "output": "YES" }, { "input": "0 0 0 0\n1 1 0 0\n0 0 0 1\n0 0 1 0", "output": "NO" }, { "input": "0 1 0 0\n0 0 0 1\n0 1 0 0\n0 0 0 1", "output": "NO" }, { "input": "0 1 0 0\n1 1 0 1\n1 0 0 1\n1 1 0 1", "output": "YES" }, { "input": "1 0 0 1\n0 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 1 0 1\n0 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 1 1\n0 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 1\n1 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 1\n0 1 0 0\n0 0 0 0\n0 0 0 0", "output": "NO" }, { "input": "0 0 0 1\n0 0 1 0\n0 0 0 0\n0 0 0 0", "output": "NO" }, { "input": "0 0 0 1\n0 0 0 0\n1 0 0 0\n0 0 0 0", "output": "NO" }, { "input": "0 0 0 1\n0 0 0 0\n0 1 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 1\n0 0 0 0\n0 0 1 0\n0 0 0 0", "output": "NO" }, { "input": "0 0 0 1\n0 0 0 0\n0 0 0 0\n1 0 0 0", "output": "NO" }, { "input": "0 0 0 1\n0 0 0 0\n0 0 0 0\n0 1 0 0", "output": "NO" }, { "input": "0 0 0 1\n0 0 0 0\n0 0 0 0\n0 0 1 0", "output": "YES" }, { "input": "1 0 0 0\n0 0 0 1\n0 0 0 0\n0 0 0 0", "output": "NO" }, { "input": "0 1 0 0\n0 0 0 1\n0 0 0 0\n0 0 0 0", "output": "NO" }, { "input": "0 0 1 0\n0 0 0 1\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n1 0 0 1\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 1 0 1\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 0 1 1\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 1\n1 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 1\n0 1 0 0\n0 0 0 0", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 1\n0 0 1 0\n0 0 0 0", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 1\n0 0 0 0\n1 0 0 0", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 1\n0 0 0 0\n0 1 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 1\n0 0 0 0\n0 0 1 0", "output": "NO" }, { "input": "1 0 0 0\n0 0 0 0\n0 0 0 1\n0 0 0 0", "output": "NO" }, { "input": "0 1 0 0\n0 0 0 0\n0 0 0 1\n0 0 0 0", "output": "YES" }, { "input": "0 0 1 0\n0 0 0 0\n0 0 0 1\n0 0 0 0", "output": "NO" }, { "input": "0 0 0 0\n1 0 0 0\n0 0 0 1\n0 0 0 0", "output": "NO" }, { "input": "0 0 0 0\n0 1 0 0\n0 0 0 1\n0 0 0 0", "output": "NO" }, { "input": "0 0 0 0\n0 0 1 0\n0 0 0 1\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 0\n1 0 0 1\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 0\n0 1 0 1\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 1 1\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 0 1\n1 0 0 0", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 0 1\n0 1 0 0", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 0 1\n0 0 1 0", "output": "NO" }, { "input": "1 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 1", "output": "YES" }, { "input": "0 1 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 1", "output": "NO" }, { "input": "0 0 1 0\n0 0 0 0\n0 0 0 0\n0 0 0 1", "output": "NO" }, { "input": "0 0 0 0\n1 0 0 0\n0 0 0 0\n0 0 0 1", "output": "NO" }, { "input": "0 0 0 0\n0 1 0 0\n0 0 0 0\n0 0 0 1", "output": "YES" }, { "input": "0 0 0 0\n0 0 1 0\n0 0 0 0\n0 0 0 1", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 0\n1 0 0 0\n0 0 0 1", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 0\n0 1 0 0\n0 0 0 1", "output": "NO" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 1 0\n0 0 0 1", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 0 0\n1 0 0 1", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 0 0\n0 1 0 1", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 1 1", "output": "YES" }, { "input": "0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0", "output": "NO" }, { "input": "1 1 1 1\n1 1 1 1\n1 1 1 1\n1 1 1 1", "output": "YES" }, { "input": "1 0 0 0\n0 1 0 0\n0 0 1 0\n0 0 0 1", "output": "YES" }, { "input": "1 1 1 1\n0 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "1 0 0 1\n0 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 1 1\n0 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 1 0 1\n0 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 1\n1 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 1 0 0\n0 0 0 0\n0 0 0 1\n0 0 0 0", "output": "YES" }, { "input": "0 1 1 0\n1 0 1 0\n1 1 1 0\n0 0 0 1", "output": "YES" }, { "input": "1 1 0 1\n0 0 0 0\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "1 1 1 0\n1 1 1 0\n1 1 1 0\n0 0 0 1", "output": "YES" }, { "input": "1 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 1", "output": "YES" }, { "input": "0 0 0 1\n0 0 0 0\n0 1 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 1\n0 0 1 1\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 1\n0 1 1 1\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 1\n0 1 0 1\n0 0 0 0\n0 0 0 0", "output": "YES" }, { "input": "0 0 0 1\n0 0 0 1\n0 0 0 0\n0 1 0 0", "output": "YES" }, { "input": "0 0 0 1\n0 0 0 1\n1 0 0 0\n0 0 0 0", "output": "YES" } ]
62
0
0
2,131
803
Maximal GCD
[ "constructive algorithms", "greedy", "math" ]
null
null
You are given positive integer number *n*. You should create such strictly increasing sequence of *k* positive numbers *a*1,<=*a*2,<=...,<=*a**k*, that their sum is equal to *n* and greatest common divisor is maximal. Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them. If there is no possible sequence then output -1.
The first line consists of two numbers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=1010).
If the answer exists then output *k* numbers β€” resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
[ "6 3\n", "8 2\n", "5 3\n" ]
[ "1 2 3\n", "2 6\n", "-1\n" ]
none
[ { "input": "6 3", "output": "1 2 3" }, { "input": "8 2", "output": "2 6" }, { "input": "5 3", "output": "-1" }, { "input": "1 1", "output": "1" }, { "input": "1 2", "output": "-1" }, { "input": "2 1", "output": "2" }, { "input": "2 10000000000", "output": "-1" }, { "input": "5 1", "output": "5" }, { "input": "6 2", "output": "2 4" }, { "input": "24 2", "output": "8 16" }, { "input": "24 3", "output": "4 8 12" }, { "input": "24 4", "output": "2 4 6 12" }, { "input": "24 5", "output": "1 2 3 4 14" }, { "input": "479001600 2", "output": "159667200 319334400" }, { "input": "479001600 3", "output": "79833600 159667200 239500800" }, { "input": "479001600 4", "output": "47900160 95800320 143700480 191600640" }, { "input": "479001600 5", "output": "31933440 63866880 95800320 127733760 159667200" }, { "input": "479001600 6", "output": "22809600 45619200 68428800 91238400 114048000 136857600" }, { "input": "3000000021 1", "output": "3000000021" }, { "input": "3000000021 2", "output": "1000000007 2000000014" }, { "input": "3000000021 3", "output": "3 6 3000000012" }, { "input": "3000000021 4", "output": "3 6 9 3000000003" }, { "input": "3000000021 50000", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..." }, { "input": "3000000021 100000", "output": "-1" }, { "input": "10000000000 100", "output": "1953125 3906250 5859375 7812500 9765625 11718750 13671875 15625000 17578125 19531250 21484375 23437500 25390625 27343750 29296875 31250000 33203125 35156250 37109375 39062500 41015625 42968750 44921875 46875000 48828125 50781250 52734375 54687500 56640625 58593750 60546875 62500000 64453125 66406250 68359375 70312500 72265625 74218750 76171875 78125000 80078125 82031250 83984375 85937500 87890625 89843750 91796875 93750000 95703125 97656250 99609375 101562500 103515625 105468750 107421875 109375000 1113281..." }, { "input": "10000000000 2000", "output": "4000 8000 12000 16000 20000 24000 28000 32000 36000 40000 44000 48000 52000 56000 60000 64000 68000 72000 76000 80000 84000 88000 92000 96000 100000 104000 108000 112000 116000 120000 124000 128000 132000 136000 140000 144000 148000 152000 156000 160000 164000 168000 172000 176000 180000 184000 188000 192000 196000 200000 204000 208000 212000 216000 220000 224000 228000 232000 236000 240000 244000 248000 252000 256000 260000 264000 268000 272000 276000 280000 284000 288000 292000 296000 300000 304000 30800..." }, { "input": "10000000000 5000", "output": "640 1280 1920 2560 3200 3840 4480 5120 5760 6400 7040 7680 8320 8960 9600 10240 10880 11520 12160 12800 13440 14080 14720 15360 16000 16640 17280 17920 18560 19200 19840 20480 21120 21760 22400 23040 23680 24320 24960 25600 26240 26880 27520 28160 28800 29440 30080 30720 31360 32000 32640 33280 33920 34560 35200 35840 36480 37120 37760 38400 39040 39680 40320 40960 41600 42240 42880 43520 44160 44800 45440 46080 46720 47360 48000 48640 49280 49920 50560 51200 51840 52480 53120 53760 54400 55040 55680 56320..." }, { "input": "10000000000 100000", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..." }, { "input": "10000000000 100000000", "output": "-1" }, { "input": "10000000000 10000000000", "output": "-1" }, { "input": "10000000000 100001", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..." }, { "input": "1 4000000000", "output": "-1" }, { "input": "4294967296 4294967296", "output": "-1" }, { "input": "71227122 9603838834", "output": "-1" }, { "input": "10000000000 9603838835", "output": "-1" }, { "input": "5 5999999999", "output": "-1" }, { "input": "2 9324327498", "output": "-1" }, { "input": "9 2", "output": "3 6" }, { "input": "10000000000 4294967296", "output": "-1" }, { "input": "1 3500000000", "output": "-1" }, { "input": "10000000000 4000000000", "output": "-1" }, { "input": "2000 9324327498", "output": "-1" }, { "input": "10000000000 8589934592", "output": "-1" }, { "input": "5000150001 100001", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..." }, { "input": "10000000000 3037000500", "output": "-1" }, { "input": "9400000000 9324327498", "output": "-1" }, { "input": "10000000000 3307000500", "output": "-1" }, { "input": "2 4000000000", "output": "-1" }, { "input": "1000 4294967295", "output": "-1" }, { "input": "36 3", "output": "6 12 18" }, { "input": "2147483648 4294967296", "output": "-1" }, { "input": "999 4294967295", "output": "-1" }, { "input": "10000000000 130000", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..." }, { "input": "10000000000 140000", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..." }, { "input": "10000000000 6074001000", "output": "-1" }, { "input": "12344321 1", "output": "12344321" }, { "input": "2 2", "output": "-1" }, { "input": "28 7", "output": "1 2 3 4 5 6 7" }, { "input": "1 1", "output": "1" }, { "input": "1 2", "output": "-1" }, { "input": "1 3", "output": "-1" }, { "input": "1 4", "output": "-1" }, { "input": "1 5", "output": "-1" }, { "input": "1 6", "output": "-1" }, { "input": "1 7", "output": "-1" }, { "input": "1 8", "output": "-1" }, { "input": "1 9", "output": "-1" }, { "input": "1 10", "output": "-1" }, { "input": "2 1", "output": "2" }, { "input": "2 2", "output": "-1" }, { "input": "2 3", "output": "-1" }, { "input": "2 4", "output": "-1" }, { "input": "2 5", "output": "-1" }, { "input": "2 6", "output": "-1" }, { "input": "2 7", "output": "-1" }, { "input": "2 8", "output": "-1" }, { "input": "2 9", "output": "-1" }, { "input": "2 10", "output": "-1" }, { "input": "3 1", "output": "3" }, { "input": "3 2", "output": "1 2" }, { "input": "3 3", "output": "-1" }, { "input": "3 4", "output": "-1" }, { "input": "3 5", "output": "-1" }, { "input": "3 6", "output": "-1" }, { "input": "3 7", "output": "-1" }, { "input": "3 8", "output": "-1" }, { "input": "3 9", "output": "-1" }, { "input": "3 10", "output": "-1" }, { "input": "4 1", "output": "4" }, { "input": "4 2", "output": "1 3" }, { "input": "4 3", "output": "-1" }, { "input": "4 4", "output": "-1" }, { "input": "4 5", "output": "-1" }, { "input": "4 6", "output": "-1" }, { "input": "4 7", "output": "-1" }, { "input": "4 8", "output": "-1" }, { "input": "4 9", "output": "-1" }, { "input": "4 10", "output": "-1" }, { "input": "5 1", "output": "5" }, { "input": "5 2", "output": "1 4" }, { "input": "5 3", "output": "-1" }, { "input": "5 4", "output": "-1" }, { "input": "5 5", "output": "-1" }, { "input": "5 6", "output": "-1" }, { "input": "5 7", "output": "-1" }, { "input": "5 8", "output": "-1" }, { "input": "5 9", "output": "-1" }, { "input": "5 10", "output": "-1" }, { "input": "6 1", "output": "6" }, { "input": "6 2", "output": "2 4" }, { "input": "6 3", "output": "1 2 3" }, { "input": "6 4", "output": "-1" }, { "input": "6 5", "output": "-1" }, { "input": "6 6", "output": "-1" }, { "input": "6 7", "output": "-1" }, { "input": "6 8", "output": "-1" }, { "input": "6 9", "output": "-1" }, { "input": "6 10", "output": "-1" }, { "input": "7 1", "output": "7" }, { "input": "7 2", "output": "1 6" }, { "input": "7 3", "output": "1 2 4" }, { "input": "7 4", "output": "-1" }, { "input": "7 5", "output": "-1" }, { "input": "7 6", "output": "-1" }, { "input": "7 7", "output": "-1" }, { "input": "7 8", "output": "-1" }, { "input": "7 9", "output": "-1" }, { "input": "7 10", "output": "-1" }, { "input": "8 1", "output": "8" }, { "input": "8 2", "output": "2 6" }, { "input": "8 3", "output": "1 2 5" }, { "input": "8 4", "output": "-1" }, { "input": "8 5", "output": "-1" }, { "input": "8 6", "output": "-1" }, { "input": "8 7", "output": "-1" }, { "input": "8 8", "output": "-1" }, { "input": "8 9", "output": "-1" }, { "input": "8 10", "output": "-1" } ]
218
9,523,200
3
2,136
877
Nikita and string
[ "brute force", "dp" ]
null
null
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?
The first line contains a non-empty string of length not greater than 5<=000 containing only lowercase English letters "a" and "b".
Print a single integerΒ β€” the maximum possible size of beautiful string Nikita can get.
[ "abba\n", "bab\n" ]
[ "4", "2" ]
It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
[ { "input": "abba", "output": "4" }, { "input": "bab", "output": "2" }, { "input": "bbabbbaabbbb", "output": "9" }, { "input": "bbabbbbbaaba", "output": "10" }, { "input": "bbabbbababaa", "output": "9" }, { "input": "aabbaababbab", "output": "8" }, { "input": "a", "output": "1" }, { "input": "b", "output": "1" }, { "input": "ab", "output": "2" }, { "input": "ba", "output": "2" }, { "input": "bb", "output": "2" }, { "input": "aa", "output": "2" }, { "input": "babbbaab", "output": "6" }, { "input": "abaaaa", "output": "6" }, { "input": "aaa", "output": "3" } ]
46
0
3
2,141
0
none
[ "none" ]
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.
[]
1,216
26,214,400
0
2,147
93
Frames
[ "implementation" ]
A. Frames
2
256
Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well. This time Igor K. got disappointed in one of his hobbies: editing and voicing videos. Moreover, he got disappointed in it so much, that he decided to destroy his secret archive for good. Igor K. use Pindows XR operation system which represents files and folders by small icons. At that, *m* icons can fit in a horizontal row in any window. Igor K.'s computer contains *n* folders in the D: disk's root catalog. The folders are numbered from 1 to *n* in the order from the left to the right and from top to bottom (see the images). At that the folders with secret videos have numbers from *a* to *b* inclusive. Igor K. wants to delete them forever, at that making as few frame selections as possible, and then pressing Shift+Delete exactly once. What is the minimum number of times Igor K. will have to select the folder in order to select folders from *a* to *b* and only them? Let us note that if some selected folder is selected repeatedly, then it is deselected. Each selection possesses the shape of some rectangle with sides parallel to the screen's borders.
The only line contains four integers *n*, *m*, *a*, *b* (1<=≀<=*n*,<=*m*<=≀<=109, 1<=≀<=*a*<=≀<=*b*<=≀<=*n*). They are the number of folders in Igor K.'s computer, the width of a window and the numbers of the first and the last folders that need to be deleted.
Print a single number: the least possible number of times Igor K. will have to select the folders using frames to select only the folders with numbers from *a* to *b*.
[ "11 4 3 9\n", "20 5 2 20\n" ]
[ "3\n", "2\n" ]
The images below illustrate statement tests. The first test: <img class="tex-graphics" src="https://espresso.codeforces.com/a0e4ba690dd16e3c68210a28afd82020b23fb605.png" style="max-width: 100.0%;max-height: 100.0%;"/> In this test we can select folders 3 and 4 with out first selection, folders 5, 6, 7, 8 with our second selection and folder 9 with our third, last selection. The second test: <img class="tex-graphics" src="https://espresso.codeforces.com/289e2666a3d8b3dfe5b22ff3d88976df711640f7.png" style="max-width: 100.0%;max-height: 100.0%;"/> In this test we can first select all folders in the first row (2, 3, 4, 5), then β€” all other ones.
[ { "input": "11 4 3 9", "output": "3" }, { "input": "20 5 2 20", "output": "2" }, { "input": "1 1 1 1", "output": "1" }, { "input": "26 5 2 18", "output": "3" }, { "input": "21 5 1 15", "output": "1" }, { "input": "21 5 1 21", "output": "1" }, { "input": "21 5 8 14", "output": "2" }, { "input": "20 4 1 20", "output": "1" }, { "input": "21 5 1 13", "output": "2" }, { "input": "21 5 4 15", "output": "2" }, { "input": "17 3 1 16", "output": "2" }, { "input": "19 5 7 19", "output": "2" }, { "input": "18 2 1 13", "output": "2" }, { "input": "21 3 6 11", "output": "2" }, { "input": "21 5 3 12", "output": "2" }, { "input": "21 3 6 10", "output": "3" }, { "input": "28 5 4 26", "output": "3" }, { "input": "21 5 6 18", "output": "2" }, { "input": "21 5 4 21", "output": "2" }, { "input": "17 5 6 17", "output": "1" }, { "input": "21 5 9 12", "output": "2" }, { "input": "21 3 6 7", "output": "2" }, { "input": "21 5 7 9", "output": "1" }, { "input": "12 4 5 8", "output": "1" }, { "input": "21 3 6 8", "output": "2" }, { "input": "21 1 5 17", "output": "1" }, { "input": "5 5 2 4", "output": "1" }, { "input": "18 4 6 17", "output": "2" }, { "input": "18 4 6 18", "output": "2" }, { "input": "16 4 1 16", "output": "1" }, { "input": "20 4 7 14", "output": "2" }, { "input": "17 3 12 16", "output": "3" }, { "input": "12 4 8 9", "output": "2" }, { "input": "11 8 2 7", "output": "1" }, { "input": "27 5 4 24", "output": "3" }, { "input": "29 5 12 27", "output": "3" }, { "input": "30 5 5 29", "output": "2" }, { "input": "91 2 15 72", "output": "1" }, { "input": "41 1 8 27", "output": "1" }, { "input": "26 5 7 21", "output": "2" }, { "input": "70 5 31 33", "output": "1" }, { "input": "84 9 6 80", "output": "3" }, { "input": "79 8 41 64", "output": "1" }, { "input": "63 11 23 48", "output": "2" }, { "input": "97 9 18 54", "output": "2" }, { "input": "75 18 20 23", "output": "1" }, { "input": "66 42 43 44", "output": "1" }, { "input": "92 54 20 53", "output": "1" }, { "input": "32 90 31 32", "output": "1" }, { "input": "18 100 6 6", "output": "1" }, { "input": "458 12 203 310", "output": "2" }, { "input": "149 49 92 129", "output": "2" }, { "input": "264 2 9 63", "output": "2" }, { "input": "908 6 407 531", "output": "3" }, { "input": "410 36 109 191", "output": "2" }, { "input": "301 38 97 171", "output": "3" }, { "input": "691 27 313 499", "output": "3" }, { "input": "939 42 86 827", "output": "3" }, { "input": "280 32 64 277", "output": "3" }, { "input": "244 25 94 199", "output": "3" }, { "input": "134 110 11 52", "output": "1" }, { "input": "886 251 61 672", "output": "3" }, { "input": "261 686 243 254", "output": "1" }, { "input": "162 309 68 98", "output": "1" }, { "input": "476 398 77 256", "output": "1" }, { "input": "258 224 84 174", "output": "1" }, { "input": "357 182 73 247", "output": "2" }, { "input": "488 655 290 457", "output": "1" }, { "input": "149 334 78 105", "output": "1" }, { "input": "488 519 203 211", "output": "1" }, { "input": "192293793 2864 5278163 190776899", "output": "3" }, { "input": "38644205 2729 9325777 31658388", "output": "3" }, { "input": "268836959 6117 166683294 249843000", "output": "3" }, { "input": "831447817 8377 549549158 577671489", "output": "3" }, { "input": "444819690 3519 48280371 117052060", "output": "3" }, { "input": "729584406 8367 456501516 557088265", "output": "3" }, { "input": "629207296 3735 112288653 309364482", "output": "3" }, { "input": "775589210 6930 266348458 604992807", "output": "3" }, { "input": "249414894 1999 34827655 127026562", "output": "3" }, { "input": "566377385 227 424126063 478693454", "output": "3" }, { "input": "960442940 572344654 77422042 406189391", "output": "1" }, { "input": "291071313 592207814 6792338 181083636", "output": "1" }, { "input": "191971162 306112722 18212391 188328807", "output": "1" }, { "input": "609162932 300548167 21640850 411089609", "output": "2" }, { "input": "645010014 34698301 217620581 416292490", "output": "3" }, { "input": "51474721 867363452 12231088 43489285", "output": "1" }, { "input": "484381636 927869638 57278216 175514226", "output": "1" }, { "input": "491259590 529594367 305425951 326414536", "output": "1" }, { "input": "733405771 830380469 19971607 389270995", "output": "1" }, { "input": "446237720 920085248 296916273 439113596", "output": "1" }, { "input": "12 6 3 10", "output": "2" }, { "input": "25 2 8 11", "output": "2" }, { "input": "17 8 3 15", "output": "2" }, { "input": "9 2 4 7", "output": "2" }, { "input": "6 7 5 6", "output": "1" }, { "input": "13 2 1 6", "output": "1" }, { "input": "15 8 10 14", "output": "1" }, { "input": "27 2 5 13", "output": "2" }, { "input": "14 8 2 12", "output": "2" }, { "input": "61 1 10 38", "output": "1" }, { "input": "15 6 7 15", "output": "1" }, { "input": "100 1 2 15", "output": "1" }, { "input": "10 1 4 5", "output": "1" }, { "input": "6 3 1 6", "output": "1" }, { "input": "4 3 3 4", "output": "2" }, { "input": "5 2 1 5", "output": "1" }, { "input": "7 3 1 1", "output": "1" }, { "input": "7 3 1 2", "output": "1" }, { "input": "7 3 1 3", "output": "1" }, { "input": "7 3 1 4", "output": "2" }, { "input": "7 3 1 5", "output": "2" }, { "input": "7 3 1 6", "output": "1" }, { "input": "7 3 1 7", "output": "1" }, { "input": "7 3 2 2", "output": "1" }, { "input": "7 3 2 3", "output": "1" }, { "input": "7 3 2 4", "output": "2" }, { "input": "7 3 2 5", "output": "2" }, { "input": "7 3 2 6", "output": "2" }, { "input": "7 3 2 7", "output": "2" }, { "input": "7 3 3 3", "output": "1" }, { "input": "7 3 3 4", "output": "2" }, { "input": "7 3 3 5", "output": "2" }, { "input": "7 3 3 6", "output": "2" }, { "input": "7 3 3 7", "output": "2" }, { "input": "7 3 4 4", "output": "1" }, { "input": "7 3 4 5", "output": "1" }, { "input": "7 3 4 6", "output": "1" }, { "input": "7 3 4 7", "output": "1" }, { "input": "7 3 5 5", "output": "1" }, { "input": "7 3 5 6", "output": "1" }, { "input": "7 3 5 7", "output": "2" }, { "input": "7 3 6 6", "output": "1" }, { "input": "7 3 6 7", "output": "2" }, { "input": "7 3 7 7", "output": "1" }, { "input": "8 3 1 1", "output": "1" }, { "input": "8 3 1 2", "output": "1" }, { "input": "8 3 1 3", "output": "1" }, { "input": "8 3 1 4", "output": "2" }, { "input": "8 3 1 5", "output": "2" }, { "input": "8 3 1 6", "output": "1" }, { "input": "8 3 1 7", "output": "2" }, { "input": "8 3 1 8", "output": "1" }, { "input": "8 3 2 2", "output": "1" }, { "input": "8 3 2 3", "output": "1" }, { "input": "8 3 2 4", "output": "2" }, { "input": "8 3 2 5", "output": "2" }, { "input": "8 3 2 6", "output": "2" }, { "input": "8 3 2 7", "output": "2" }, { "input": "8 3 2 8", "output": "2" }, { "input": "8 3 3 3", "output": "1" }, { "input": "8 3 3 4", "output": "2" }, { "input": "8 3 3 5", "output": "2" }, { "input": "8 3 3 6", "output": "2" }, { "input": "8 3 3 7", "output": "3" }, { "input": "8 3 3 8", "output": "2" }, { "input": "8 3 4 4", "output": "1" }, { "input": "8 3 4 5", "output": "1" }, { "input": "8 3 4 6", "output": "1" }, { "input": "8 3 4 7", "output": "2" }, { "input": "8 3 4 8", "output": "1" }, { "input": "8 3 5 5", "output": "1" }, { "input": "8 3 5 6", "output": "1" }, { "input": "8 3 5 7", "output": "2" }, { "input": "8 3 5 8", "output": "2" }, { "input": "8 3 6 6", "output": "1" }, { "input": "8 3 6 7", "output": "2" }, { "input": "8 3 6 8", "output": "2" }, { "input": "8 3 7 7", "output": "1" }, { "input": "8 3 7 8", "output": "1" }, { "input": "8 3 8 8", "output": "1" } ]
92
0
0
2,152
382
Number Busters
[ "binary search", "math" ]
null
null
Arthur and Alexander are number busters. Today they've got a competition. Arthur took a group of four integers *a*,<=*b*,<=*w*,<=*x* (0<=≀<=*b*<=&lt;<=*w*,<=0<=&lt;<=*x*<=&lt;<=*w*) and Alexander took integer *с*. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: *c*<==<=*c*<=-<=1. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if *b*<=β‰₯<=*x*, perform the assignment *b*<==<=*b*<=-<=*x*, if *b*<=&lt;<=*x*, then perform two consecutive assignments *a*<==<=*a*<=-<=1;Β *b*<==<=*w*<=-<=(*x*<=-<=*b*). You've got numbers *a*,<=*b*,<=*w*,<=*x*,<=*c*. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if *c*<=≀<=*a*.
The first line contains integers *a*,<=*b*,<=*w*,<=*x*,<=*c* (1<=≀<=*a*<=≀<=2Β·109,<=1<=≀<=*w*<=≀<=1000,<=0<=≀<=*b*<=&lt;<=*w*,<=0<=&lt;<=*x*<=&lt;<=*w*,<=1<=≀<=*c*<=≀<=2Β·109).
Print a single integer β€” the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem's limits.
[ "4 2 3 1 6\n", "4 2 3 1 7\n", "1 2 3 2 6\n", "1 1 2 1 1\n" ]
[ "2\n", "4\n", "13\n", "0\n" ]
none
[ { "input": "4 2 3 1 6", "output": "2" }, { "input": "4 2 3 1 7", "output": "4" }, { "input": "1 2 3 2 6", "output": "13" }, { "input": "1 1 2 1 1", "output": "0" }, { "input": "1 0 1000 999 2000000000", "output": "1999999999000" }, { "input": "10 1 6 4 20", "output": "30" }, { "input": "10 3 6 5 30", "output": "117" }, { "input": "10 3 5 1 30", "output": "25" }, { "input": "10 32 312 72 1000", "output": "1287" }, { "input": "1 102 123 27 2321", "output": "2972" }, { "input": "2000000000 159 1000 870 2000000000", "output": "0" }, { "input": "200000000 794 1000 117 2000000000", "output": "2038505096" }, { "input": "20000000 280 1000 25 2000000000", "output": "2030769231" }, { "input": "1999999999 47 1000 527 2000000000", "output": "3" }, { "input": "19999 346 1000 141 2000000000", "output": "2328265426" }, { "input": "1 142 1000 673 2000000000", "output": "6116207948" }, { "input": "1 851 999 721 2000000000", "output": "7187050354" }, { "input": "1 504 998 900 2000000000", "output": "20367346924" }, { "input": "1 250 997 55 2000000000", "output": "2116772823" }, { "input": "1 2 3 2 2000000000", "output": "5999999995" }, { "input": "1 0 2 1 1232132", "output": "2464262" }, { "input": "1 999 1000 1 2000000000", "output": "2002002001" }, { "input": "1 999 1000 2 2000000000", "output": "2004008015" } ]
61
409,600
0
2,161
472
Design Tutorial: Inverse the Problem
[ "dfs and similar", "dsu", "shortest paths", "trees" ]
null
null
There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example. Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an *n*<=Γ—<=*n* distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).
The first line contains an integer *n* (1<=≀<=*n*<=≀<=2000) β€” the number of nodes in that graph. Then next *n* lines each contains *n* integers *d**i*,<=*j* (0<=≀<=*d**i*,<=*j*<=≀<=109) β€” the distance between node *i* and node *j*.
If there exists such a tree, output "YES", otherwise output "NO".
[ "3\n0 2 7\n2 0 9\n7 9 0\n", "3\n1 2 7\n2 0 9\n7 9 0\n", "3\n0 2 2\n7 0 9\n7 9 0\n", "3\n0 1 1\n1 0 1\n1 1 0\n", "2\n0 0\n0 0\n" ]
[ "YES\n", "NO\n", "NO\n", "NO\n", "NO\n" ]
In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7. In the second example, it is impossible because *d*<sub class="lower-index">1, 1</sub> should be 0, but it is 1. In the third example, it is impossible because *d*<sub class="lower-index">1, 2</sub> should equal *d*<sub class="lower-index">2, 1</sub>.
[ { "input": "3\n0 2 7\n2 0 9\n7 9 0", "output": "YES" }, { "input": "3\n1 2 7\n2 0 9\n7 9 0", "output": "NO" }, { "input": "3\n0 2 2\n7 0 9\n7 9 0", "output": "NO" }, { "input": "3\n0 1 1\n1 0 1\n1 1 0", "output": "NO" }, { "input": "2\n0 0\n0 0", "output": "NO" }, { "input": "1\n0", "output": "YES" }, { "input": "1\n1", "output": "NO" }, { "input": "2\n0 1000000000\n1000000000 0", "output": "YES" }, { "input": "5\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0", "output": "NO" }, { "input": "2\n0 1\n1 0", "output": "YES" }, { "input": "4\n0 3 7 6\n3 0 4 9\n7 4 0 2\n6 9 2 0", "output": "NO" }, { "input": "3\n0 1 2\n1 0 2\n2 2 0", "output": "NO" }, { "input": "3\n0 2 7\n2 0 10\n7 10 0", "output": "NO" } ]
61
0
0
2,172
71
Progress Bar
[ "implementation", "math" ]
B. Progress Bar
1
256
A progress bar is an element of graphical interface that displays the progress of a process for this very moment before it is completed. Let's take a look at the following form of such a bar. A bar is represented as *n* squares, located in line. To add clarity, let's number them with positive integers from 1 to *n* from the left to the right. Each square has saturation (*a**i* for the *i*-th square), which is measured by an integer from 0 to *k*. When the bar for some *i* (1<=≀<=*i*<=≀<=*n*) is displayed, squares 1,<=2,<=... ,<=*i*<=-<=1 has the saturation *k*, squares *i*<=+<=1,<=*i*<=+<=2,<=... ,<=*n* has the saturation 0, and the saturation of the square *i* can have any value from 0 to *k*. So some first squares of the progress bar always have the saturation *k*. Some last squares always have the saturation 0. And there is no more than one square that has the saturation different from 0 and *k*. The degree of the process's completion is measured in percents. Let the process be *t*% completed. Then the following inequation is fulfilled: An example of such a bar can be seen on the picture. For the given *n*, *k*, *t* determine the measures of saturation for all the squares *a**i* of the progress bar.
We are given 3 space-separated integers *n*, *k*, *t* (1<=≀<=*n*,<=*k*<=≀<=100, 0<=≀<=*t*<=≀<=100).
Print *n* numbers. The *i*-th of them should be equal to *a**i*.
[ "10 10 54\n", "11 13 37\n" ]
[ "10 10 10 10 10 4 0 0 0 0 ", "13 13 13 13 0 0 0 0 0 0 0 " ]
none
[ { "input": "10 10 54", "output": "10 10 10 10 10 4 0 0 0 0 " }, { "input": "11 13 37", "output": "13 13 13 13 0 0 0 0 0 0 0 " }, { "input": "9 25 50", "output": "25 25 25 25 12 0 0 0 0 " }, { "input": "43 47 77", "output": "47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 5 0 0 0 0 0 0 0 0 0 " }, { "input": "20 1 43", "output": "1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 " }, { "input": "17 4 61", "output": "4 4 4 4 4 4 4 4 4 4 1 0 0 0 0 0 0 " }, { "input": "10 16 0", "output": "0 0 0 0 0 0 0 0 0 0 " }, { "input": "17 13 100", "output": "13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 " }, { "input": "11 9 1", "output": "0 0 0 0 0 0 0 0 0 0 0 " }, { "input": "9 11 99", "output": "11 11 11 11 11 11 11 11 10 " }, { "input": "6 17 1", "output": "1 0 0 0 0 0 " }, { "input": "6 17 99", "output": "17 17 17 17 17 15 " }, { "input": "17 6 1", "output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " }, { "input": "17 6 99", "output": "6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 4 " }, { "input": "99 1 1", "output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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": "99 1 99", "output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 " }, { "input": "1 1 0", "output": "0 " }, { "input": "1 1 37", "output": "0 " }, { "input": "1 1 50", "output": "0 " }, { "input": "1 1 51", "output": "0 " }, { "input": "1 1 99", "output": "0 " }, { "input": "1 1 100", "output": "1 " }, { "input": "1 17 35", "output": "5 " }, { "input": "1 31 88", "output": "27 " }, { "input": "1 100 0", "output": "0 " }, { "input": "1 100 38", "output": "38 " }, { "input": "1 100 99", "output": "99 " }, { "input": "1 100 100", "output": "100 " }, { "input": "1 99 99", "output": "98 " }, { "input": "100 100 73", "output": "100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 0 0 0 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": "100 100 100", "output": "100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 " }, { "input": "100 13 100", "output": "13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 " }, { "input": "100 1 100", "output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 " }, { "input": "100 1 0", "output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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": "100 13 0", "output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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": "100 63 0", "output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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": "100 100 0", "output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 99 51", "output": "99 1 " }, { "input": "2 1 49", "output": "0 0 " }, { "input": "2 1 100", "output": "1 1 " }, { "input": "2 13 0", "output": "0 0 " }, { "input": "99 1 51", "output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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": "15 25 93", "output": "25 25 25 25 25 25 25 25 25 25 25 25 25 23 0 " }, { "input": "60 51 85", "output": "51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 0 0 0 0 0 0 0 0 0 " }, { "input": "4 78 78", "output": "78 78 78 9 " }, { "input": "49 4 4", "output": "4 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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": "42 83 98", "output": "83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 13 " }, { "input": "87 9 90", "output": "9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 2 0 0 0 0 0 0 0 0 " }, { "input": "31 36 83", "output": "36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 26 0 0 0 0 0 " }, { "input": "24 14 76", "output": "14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 3 0 0 0 0 0 " } ]
31
0
-1
2,175
180
Letter
[ "dp" ]
null
null
Patrick has just finished writing a message to his sweetheart Stacey when he noticed that the message didn't look fancy. Patrick was nervous while writing the message, so some of the letters there were lowercase and some of them were uppercase. Patrick believes that a message is fancy if any uppercase letter stands to the left of any lowercase one. In other words, this rule describes the strings where first go zero or more uppercase letters, and then β€” zero or more lowercase letters. To make the message fancy, Patrick can erase some letter and add the same letter in the same place in the opposite case (that is, he can replace an uppercase letter with the lowercase one and vice versa). Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions.
The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105.
Print a single number β€” the least number of actions needed to make the message fancy.
[ "PRuvetSTAaYA\n", "OYPROSTIYAOPECHATALSYAPRIVETSTASYA\n", "helloworld\n" ]
[ "5\n", "0\n", "0\n" ]
none
[ { "input": "PRuvetSTAaYA", "output": "5" }, { "input": "OYPROSTIYAOPECHATALSYAPRIVETSTASYA", "output": "0" }, { "input": "helloworld", "output": "0" }, { "input": "P", "output": "0" }, { "input": "t", "output": "0" }, { "input": "XdJ", "output": "1" }, { "input": "FSFlNEelYY", "output": "3" }, { "input": "lgtyasficu", "output": "0" }, { "input": "WYKUDTDDBT", "output": "0" }, { "input": "yysxwlyqboatikfnpxczmpijziiojbvadlfozjqldssffcxdegyxfrvohoxvgsrvlzjlkcuffoeisrpvagxtbkapkpzcafadzzjd", "output": "0" }, { "input": "mnAkOBuKxaiJwXhKnlcCvjxYXGXDoIqfUYkiLrdSYWhMemgWFzsgpoKOtHqooxbLYFuABWQSXuHdbyPVWyrkeEfqOsnEBikiqhfu", "output": "43" }, { "input": "MMVESdOCALHJCTBTUWWQRGUUVTTTABKKAAdIINAdKLRLLVLODHDXDPMcQfUhPNHFBJSDRGsHZNORSCPNvKOOIuZnZAmTPUCoPNlR", "output": "13" }, { "input": "MMbJIBhgFXPVpdQHLkWJkAHFIfJSpITTCRzRCzvRPRYECCheOknfINZWuKATDBOrEVKTcWXiYPjtzQMGUSGPNTMCUrvYCSWQHqAi", "output": "27" }, { "input": "ZnqXEBOABXVbHRFFqDLWpWBBLqZIagmbRAYMDKJAYyFQXnPYwvDyRXKHLQnMLHGEOnRKmXtGqyQkoBCSmgKPcEIEugwJyxDOtTVn", "output": "31" } ]
0
0
-1
2,179
916
Jamie and Alarm Snooze
[ "brute force", "implementation", "math" ]
null
null
Jamie loves sleeping. One day, he decides that he needs to wake up at exactly *hh*:<=*mm*. However, he hates waking up, so he wants to make waking up less painful by setting the alarm at a lucky time. He will then press the snooze button every *x* minutes until *hh*:<=*mm* is reached, and only then he will wake up. He wants to know what is the smallest number of times he needs to press the snooze button. A time is considered lucky if it contains a digit '7'. For example, 13:<=07 and 17:<=27 are lucky, while 00:<=48 and 21:<=34 are not lucky. Note that it is not necessary that the time set for the alarm and the wake-up time are on the same day. It is guaranteed that there is a lucky time Jamie can set so that he can wake at *hh*:<=*mm*. Formally, find the smallest possible non-negative integer *y* such that the time representation of the time *x*Β·*y* minutes before *hh*:<=*mm* contains the digit '7'. Jamie uses 24-hours clock, so after 23:<=59 comes 00:<=00.
The first line contains a single integer *x* (1<=≀<=*x*<=≀<=60). The second line contains two two-digit integers, *hh* and *mm* (00<=≀<=*hh*<=≀<=23,<=00<=≀<=*mm*<=≀<=59).
Print the minimum number of times he needs to press the button.
[ "3\n11 23\n", "5\n01 07\n" ]
[ "2\n", "0\n" ]
In the first sample, Jamie needs to wake up at 11:23. So, he can set his alarm at 11:17. He would press the snooze button when the alarm rings at 11:17 and at 11:20. In the second sample, Jamie can set his alarm at exactly at 01:07 which is lucky.
[ { "input": "3\n11 23", "output": "2" }, { "input": "5\n01 07", "output": "0" }, { "input": "34\n09 24", "output": "3" }, { "input": "2\n14 37", "output": "0" }, { "input": "14\n19 54", "output": "9" }, { "input": "42\n15 44", "output": "12" }, { "input": "46\n02 43", "output": "1" }, { "input": "14\n06 41", "output": "1" }, { "input": "26\n04 58", "output": "26" }, { "input": "54\n16 47", "output": "0" }, { "input": "38\n20 01", "output": "3" }, { "input": "11\n02 05", "output": "8" }, { "input": "55\n22 10", "output": "5" }, { "input": "23\n10 08", "output": "6" }, { "input": "23\n23 14", "output": "9" }, { "input": "51\n03 27", "output": "0" }, { "input": "35\n15 25", "output": "13" }, { "input": "3\n12 15", "output": "6" }, { "input": "47\n00 28", "output": "3" }, { "input": "31\n13 34", "output": "7" }, { "input": "59\n17 32", "output": "0" }, { "input": "25\n11 03", "output": "8" }, { "input": "9\n16 53", "output": "4" }, { "input": "53\n04 06", "output": "3" }, { "input": "37\n00 12", "output": "5" }, { "input": "5\n13 10", "output": "63" }, { "input": "50\n01 59", "output": "10" }, { "input": "34\n06 13", "output": "4" }, { "input": "2\n18 19", "output": "1" }, { "input": "46\n06 16", "output": "17" }, { "input": "14\n03 30", "output": "41" }, { "input": "40\n13 37", "output": "0" }, { "input": "24\n17 51", "output": "0" }, { "input": "8\n14 57", "output": "0" }, { "input": "52\n18 54", "output": "2" }, { "input": "20\n15 52", "output": "24" }, { "input": "20\n03 58", "output": "30" }, { "input": "48\n07 11", "output": "0" }, { "input": "32\n04 01", "output": "2" }, { "input": "60\n08 15", "output": "1" }, { "input": "44\n20 20", "output": "4" }, { "input": "55\n15 35", "output": "9" }, { "input": "55\n03 49", "output": "11" }, { "input": "23\n16 39", "output": "4" }, { "input": "7\n20 36", "output": "7" }, { "input": "35\n16 42", "output": "1" }, { "input": "35\n05 56", "output": "21" }, { "input": "3\n17 45", "output": "0" }, { "input": "47\n05 59", "output": "6" }, { "input": "15\n10 13", "output": "9" }, { "input": "59\n06 18", "output": "9" }, { "input": "34\n17 18", "output": "0" }, { "input": "18\n05 23", "output": "2" }, { "input": "46\n17 21", "output": "0" }, { "input": "30\n06 27", "output": "0" }, { "input": "14\n18 40", "output": "3" }, { "input": "58\n22 54", "output": "6" }, { "input": "26\n19 44", "output": "5" }, { "input": "10\n15 57", "output": "0" }, { "input": "54\n20 47", "output": "0" }, { "input": "22\n08 45", "output": "3" }, { "input": "48\n18 08", "output": "1" }, { "input": "32\n07 06", "output": "0" }, { "input": "60\n19 19", "output": "2" }, { "input": "45\n07 25", "output": "0" }, { "input": "29\n12 39", "output": "8" }, { "input": "13\n08 28", "output": "3" }, { "input": "41\n21 42", "output": "5" }, { "input": "41\n09 32", "output": "3" }, { "input": "9\n21 45", "output": "2" }, { "input": "37\n10 43", "output": "5" }, { "input": "3\n20 50", "output": "1" }, { "input": "47\n00 04", "output": "1" }, { "input": "15\n13 10", "output": "21" }, { "input": "15\n17 23", "output": "0" }, { "input": "43\n22 13", "output": "2" }, { "input": "27\n10 26", "output": "6" }, { "input": "55\n22 24", "output": "5" }, { "input": "55\n03 30", "output": "11" }, { "input": "24\n23 27", "output": "0" }, { "input": "52\n11 33", "output": "3" }, { "input": "18\n22 48", "output": "17" }, { "input": "1\n12 55", "output": "8" }, { "input": "1\n04 27", "output": "0" }, { "input": "1\n12 52", "output": "5" }, { "input": "1\n20 16", "output": "9" }, { "input": "1\n04 41", "output": "4" }, { "input": "1\n20 21", "output": "4" }, { "input": "1\n04 45", "output": "8" }, { "input": "1\n12 18", "output": "1" }, { "input": "1\n04 42", "output": "5" }, { "input": "1\n02 59", "output": "2" }, { "input": "1\n18 24", "output": "7" }, { "input": "1\n02 04", "output": "7" }, { "input": "1\n18 28", "output": "1" }, { "input": "1\n18 01", "output": "2" }, { "input": "1\n10 25", "output": "8" }, { "input": "1\n02 49", "output": "2" }, { "input": "1\n02 30", "output": "3" }, { "input": "1\n18 54", "output": "7" }, { "input": "1\n02 19", "output": "2" }, { "input": "1\n05 25", "output": "8" }, { "input": "60\n23 55", "output": "6" }, { "input": "60\n08 19", "output": "1" }, { "input": "60\n00 00", "output": "7" }, { "input": "60\n08 24", "output": "1" }, { "input": "60\n16 13", "output": "9" }, { "input": "60\n08 21", "output": "1" }, { "input": "60\n16 45", "output": "9" }, { "input": "60\n08 26", "output": "1" }, { "input": "60\n08 50", "output": "1" }, { "input": "60\n05 21", "output": "12" }, { "input": "60\n13 29", "output": "6" }, { "input": "60\n05 18", "output": "12" }, { "input": "60\n13 42", "output": "6" }, { "input": "60\n05 07", "output": "0" }, { "input": "60\n05 47", "output": "0" }, { "input": "60\n21 55", "output": "4" }, { "input": "60\n05 36", "output": "12" }, { "input": "60\n21 08", "output": "4" }, { "input": "60\n21 32", "output": "4" }, { "input": "60\n16 31", "output": "9" }, { "input": "5\n00 00", "output": "73" }, { "input": "2\n06 58", "output": "390" }, { "input": "60\n00 00", "output": "7" }, { "input": "2\n00 00", "output": "181" }, { "input": "10\n00 00", "output": "37" }, { "input": "60\n01 00", "output": "8" }, { "input": "12\n00 06", "output": "31" }, { "input": "1\n00 01", "output": "4" }, { "input": "5\n00 05", "output": "74" }, { "input": "60\n01 01", "output": "8" }, { "input": "11\n18 11", "output": "2" }, { "input": "60\n01 15", "output": "8" }, { "input": "10\n00 16", "output": "38" }, { "input": "60\n00 59", "output": "7" }, { "input": "30\n00 00", "output": "13" }, { "input": "60\n01 05", "output": "8" }, { "input": "4\n00 03", "output": "4" }, { "input": "4\n00 00", "output": "91" }, { "input": "60\n00 01", "output": "7" }, { "input": "6\n00 03", "output": "1" }, { "input": "13\n00 00", "output": "1" }, { "input": "1\n18 01", "output": "2" }, { "input": "5\n06 00", "output": "145" }, { "input": "60\n04 08", "output": "11" }, { "input": "5\n01 55", "output": "96" }, { "input": "8\n00 08", "output": "47" }, { "input": "23\n18 23", "output": "2" }, { "input": "6\n00 06", "output": "62" }, { "input": "59\n18 59", "output": "2" }, { "input": "11\n00 10", "output": "3" }, { "input": "10\n00 01", "output": "37" }, { "input": "59\n00 00", "output": "7" }, { "input": "10\n18 10", "output": "2" }, { "input": "5\n00 01", "output": "73" }, { "input": "1\n00 00", "output": "3" }, { "input": "8\n00 14", "output": "47" }, { "input": "60\n03 00", "output": "10" }, { "input": "60\n00 10", "output": "7" }, { "input": "5\n01 13", "output": "87" }, { "input": "30\n02 43", "output": "18" }, { "input": "17\n00 08", "output": "3" }, { "input": "3\n00 00", "output": "1" }, { "input": "60\n00 05", "output": "7" }, { "input": "5\n18 05", "output": "2" }, { "input": "30\n00 30", "output": "14" }, { "input": "1\n00 06", "output": "9" }, { "input": "55\n00 00", "output": "7" }, { "input": "8\n02 08", "output": "62" }, { "input": "7\n00 00", "output": "9" }, { "input": "6\n08 06", "output": "2" }, { "input": "48\n06 24", "output": "16" }, { "input": "8\n06 58", "output": "98" }, { "input": "3\n12 00", "output": "1" }, { "input": "5\n01 06", "output": "86" }, { "input": "2\n00 08", "output": "185" }, { "input": "3\n18 03", "output": "2" }, { "input": "1\n17 00", "output": "0" }, { "input": "59\n00 48", "output": "7" }, { "input": "5\n12 01", "output": "49" }, { "input": "55\n01 25", "output": "9" }, { "input": "2\n07 23", "output": "0" }, { "input": "10\n01 10", "output": "44" }, { "input": "2\n00 01", "output": "2" }, { "input": "59\n00 01", "output": "6" }, { "input": "5\n00 02", "output": "1" }, { "input": "4\n01 02", "output": "106" }, { "input": "5\n00 06", "output": "74" }, { "input": "42\n00 08", "output": "9" }, { "input": "60\n01 20", "output": "8" }, { "input": "3\n06 00", "output": "1" }, { "input": "4\n00 01", "output": "1" }, { "input": "2\n00 06", "output": "184" }, { "input": "1\n00 57", "output": "0" }, { "input": "6\n00 00", "output": "61" }, { "input": "5\n08 40", "output": "9" }, { "input": "58\n00 55", "output": "1" }, { "input": "2\n00 02", "output": "182" }, { "input": "1\n08 01", "output": "2" }, { "input": "10\n10 10", "output": "14" }, { "input": "60\n01 11", "output": "8" }, { "input": "2\n07 00", "output": "0" }, { "input": "15\n00 03", "output": "25" }, { "input": "6\n04 34", "output": "106" }, { "input": "16\n00 16", "output": "24" }, { "input": "2\n00 59", "output": "1" }, { "input": "59\n00 08", "output": "7" }, { "input": "10\n03 10", "output": "56" }, { "input": "3\n08 03", "output": "2" }, { "input": "20\n06 11", "output": "37" }, { "input": "4\n01 00", "output": "106" }, { "input": "38\n01 08", "output": "12" }, { "input": "60\n00 06", "output": "7" }, { "input": "5\n12 00", "output": "49" }, { "input": "6\n01 42", "output": "78" }, { "input": "4\n00 04", "output": "92" }, { "input": "60\n04 05", "output": "11" }, { "input": "1\n00 53", "output": "6" }, { "input": "5\n08 05", "output": "2" }, { "input": "60\n18 45", "output": "1" }, { "input": "60\n06 23", "output": "13" }, { "input": "6\n00 15", "output": "3" }, { "input": "58\n00 06", "output": "7" }, { "input": "2\n06 44", "output": "383" }, { "input": "1\n08 00", "output": "1" }, { "input": "10\n06 58", "output": "78" }, { "input": "59\n00 58", "output": "8" }, { "input": "1\n18 00", "output": "1" }, { "input": "50\n00 42", "output": "9" }, { "input": "30\n18 30", "output": "2" }, { "input": "60\n21 59", "output": "4" }, { "input": "2\n10 52", "output": "87" }, { "input": "56\n00 00", "output": "7" }, { "input": "16\n18 16", "output": "2" }, { "input": "5\n01 05", "output": "86" }, { "input": "5\n05 00", "output": "133" }, { "input": "5\n23 59", "output": "72" }, { "input": "7\n17 13", "output": "0" }, { "input": "58\n00 00", "output": "7" }, { "input": "15\n00 07", "output": "0" }, { "input": "59\n08 00", "output": "1" }, { "input": "46\n00 00", "output": "8" }, { "input": "59\n01 05", "output": "2" }, { "input": "2\n01 00", "output": "211" }, { "input": "60\n00 24", "output": "7" }, { "input": "10\n00 08", "output": "37" }, { "input": "10\n00 06", "output": "37" }, { "input": "60\n01 24", "output": "8" }, { "input": "50\n00 10", "output": "8" }, { "input": "2\n03 00", "output": "271" }, { "input": "4\n19 04", "output": "17" }, { "input": "25\n00 23", "output": "16" }, { "input": "10\n01 01", "output": "43" } ]
156
4,710,400
3
2,181
821
Okabe and Future Gadget Laboratory
[ "implementation" ]
null
null
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an *n* by *n* square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every *x*,<=*y* such that 1<=≀<=*x*,<=*y*<=≀<=*n* and *a**x*,<=*y*<=β‰ <=1, there should exist two indices *s* and *t* so that *a**x*,<=*y*<==<=*a**x*,<=*s*<=+<=*a**t*,<=*y*, where *a**i*,<=*j* denotes the integer in *i*-th row and *j*-th column. Help Okabe determine whether a given lab is good!
The first line of input contains the integer *n* (1<=≀<=*n*<=≀<=50)Β β€” the size of the lab. The next *n* lines contain *n* space-separated integers denoting a row of the grid. The *j*-th integer in the *i*-th row is *a**i*,<=*j* (1<=≀<=*a**i*,<=*j*<=≀<=105).
Print "Yes" if the given lab is good and "No" otherwise. You can output each letter in upper or lower case.
[ "3\n1 1 2\n2 3 1\n6 4 1\n", "3\n1 5 2\n1 1 1\n1 2 3\n" ]
[ "Yes\n", "No\n" ]
In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes". In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No".
[ { "input": "3\n1 1 2\n2 3 1\n6 4 1", "output": "Yes" }, { "input": "3\n1 5 2\n1 1 1\n1 2 3", "output": "No" }, { "input": "1\n1", "output": "Yes" }, { "input": "4\n1 1 1 1\n1 11 1 2\n2 5 1 4\n3 9 4 1", "output": "Yes" }, { "input": "4\n1 1 1 1\n1 7 1 1\n1 3 1 2\n2 6 3 1", "output": "Yes" }, { "input": "4\n1 1 1 1\n1 12 1 2\n4 4 1 3\n5 10 6 1", "output": "Yes" }, { "input": "4\n1 1 1 1\n1 13 1 2\n4 5 1 3\n5 11 6 1", "output": "Yes" }, { "input": "4\n1 1 1 1\n1 13 1 2\n4 5 1 3\n7 11 6 1", "output": "No" }, { "input": "4\n1 1 1 1\n1 14 1 6\n4 5 1 5\n3 8 2 1", "output": "Yes" }, { "input": "10\n3 2 6 5 1 2 1 1 2 1\n1 2 2 3 1 4 1 1 1 1\n1 1 3 1 2 2 2 1 1 1\n1 3 1 2 1 2 1 1 1 1\n12 1 4 3 3 1 1 1 2 1\n8 1 1 1 1 3 6 5 1 3\n1 1 1 2 4 1 3 2 1 1\n1 1 1 1 1 1 1 2 1 1\n1 2 1 1 1 1 1 1 1 1\n2 1 1 1 1 1 1 1 1 2", "output": "Yes" }, { "input": "4\n1 2 3 5\n1 1 1 1\n1 1 1 1\n1 1 1 1", "output": "No" }, { "input": "1\n2", "output": "No" }, { "input": "3\n1 1 4\n1 1 2\n1 1 2", "output": "No" }, { "input": "1\n5", "output": "No" }, { "input": "3\n1 2 1\n2 2 3\n1 3 1", "output": "No" }, { "input": "3\n2 1 1\n2 1 1\n2 1 1", "output": "No" }, { "input": "1\n60000", "output": "No" } ]
109
307,200
3
2,183
571
CNF 2
[ "constructive algorithms", "dfs and similar", "graphs", "greedy" ]
null
null
'In Boolean logic, a formula is in conjunctive normal form (CNF) or clausal normal form if it is a conjunction of clauses, where a clause is a disjunction of literals' (cited from https://en.wikipedia.org/wiki/Conjunctive_normal_form) In the other words, CNF is a formula of type , where &amp; represents a logical "AND" (conjunction), represents a logical "OR" (disjunction), and *v**ij* are some boolean variables or their negations. Each statement in brackets is called a clause, and *v**ij* are called literals. You are given a CNF containing variables *x*1,<=...,<=*x**m* and their negations. We know that each variable occurs in at most two clauses (with negation and without negation in total). Your task is to determine whether this CNF is satisfiable, that is, whether there are such values of variables where the CNF value is true. If CNF is satisfiable, then you also need to determine the values of the variables at which the CNF is true. It is guaranteed that each variable occurs at most once in each clause.
The first line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=2Β·105) β€” the number of clauses and the number variables, correspondingly. Next *n* lines contain the descriptions of each clause. The *i*-th line first contains first number *k**i* (*k**i*<=β‰₯<=1) β€” the number of literals in the *i*-th clauses. Then follow space-separated literals *v**ij* (1<=≀<=|*v**ij*|<=≀<=*m*). A literal that corresponds to *v**ij* is *x*|*v**ij*| either with negation, if *v**ij* is negative, or without negation otherwise.
If CNF is not satisfiable, print a single line "NO" (without the quotes), otherwise print two strings: string "YES" (without the quotes), and then a string of *m* numbers zero or one β€” the values of variables in satisfying assignment in the order from *x*1 to *x**m*.
[ "2 2\n2 1 -2\n2 2 -1\n", "4 3\n1 1\n1 2\n3 -1 -2 3\n1 -3\n", "5 6\n2 1 2\n3 1 -2 3\n4 -3 5 4 6\n2 -6 -4\n1 5\n" ]
[ "YES\n11\n", "NO\n", "YES\n100010\n" ]
In the first sample test formula is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5a8654a57efa13b47a585b7998c9defb42712ded.png" style="max-width: 100.0%;max-height: 100.0%;"/>. One of possible answer is *x*<sub class="lower-index">1</sub> = *TRUE*, *x*<sub class="lower-index">2</sub> = *TRUE*.
[]
1,000
38,092,800
0
2,187
342
Cupboard and Balloons
[ "geometry" ]
null
null
A girl named Xenia has a cupboard that looks like an arc from ahead. The arc is made of a semicircle with radius *r* (the cupboard's top) and two walls of height *h* (the cupboard's sides). The cupboard's depth is *r*, that is, it looks like a rectangle with base *r* and height *h*<=+<=*r* from the sides. The figure below shows what the cupboard looks like (the front view is on the left, the side view is on the right). Xenia got lots of balloons for her birthday. The girl hates the mess, so she wants to store the balloons in the cupboard. Luckily, each balloon is a sphere with radius . Help Xenia calculate the maximum number of balloons she can put in her cupboard. You can say that a balloon is in the cupboard if you can't see any part of the balloon on the left or right view. The balloons in the cupboard can touch each other. It is not allowed to squeeze the balloons or deform them in any way. You can assume that the cupboard's walls are negligibly thin.
The single line contains two integers *r*,<=*h* (1<=≀<=*r*,<=*h*<=≀<=107).
Print a single integer β€” the maximum number of balloons Xenia can put in the cupboard.
[ "1 1\n", "1 2\n", "2 1\n" ]
[ "3\n", "5\n", "2\n" ]
none
[ { "input": "1 1", "output": "3" }, { "input": "1 2", "output": "5" }, { "input": "2 1", "output": "2" }, { "input": "2 2", "output": "3" }, { "input": "2 3", "output": "4" }, { "input": "4 1", "output": "1" }, { "input": "5 1", "output": "1" }, { "input": "5 2", "output": "1" }, { "input": "5 3", "output": "2" }, { "input": "5 4", "output": "2" }, { "input": "5 5", "output": "3" }, { "input": "5 6", "output": "3" }, { "input": "5 9", "output": "4" }, { "input": "5 10", "output": "5" }, { "input": "5 11", "output": "5" }, { "input": "674098 1358794", "output": "5" }, { "input": "3983458 7761504", "output": "5" }, { "input": "4841874 9131511", "output": "5" }, { "input": "667586 5534221", "output": "17" }, { "input": "1526002 6904227", "output": "10" }, { "input": "4835362 5823289", "output": "3" }, { "input": "5693778 7001807", "output": "3" }, { "input": "6552194 8371814", "output": "3" }, { "input": "2377906 4774524", "output": "5" }, { "input": "4365659 4738707", "output": "3" }, { "input": "98 1358794", "output": "27731" }, { "input": "458 7761504", "output": "33894" }, { "input": "874 9131511", "output": "20897" }, { "input": "586 5534221", "output": "18889" }, { "input": "2 6904227", "output": "6904228" }, { "input": "1 10000000", "output": "20000001" }, { "input": "2 10000000", "output": "10000001" }, { "input": "3 10000000", "output": "6666667" }, { "input": "4 10000000", "output": "5000001" }, { "input": "3 9999999", "output": "6666667" }, { "input": "10000000 866254", "output": "1" }, { "input": "10000000 8660255", "output": "3" }, { "input": "100 50", "output": "2" }, { "input": "100 49", "output": "1" }, { "input": "100 199", "output": "5" }, { "input": "8 7", "output": "3" }, { "input": "10000 9999", "output": "3" }, { "input": "1000000 1999999", "output": "5" }, { "input": "2000000 1999999", "output": "3" }, { "input": "18 16", "output": "3" }, { "input": "100 87", "output": "3" }, { "input": "10 19", "output": "5" }, { "input": "10000 38661", "output": "9" } ]
124
0
3
2,188
887
Div. 64
[ "implementation" ]
null
null
Top-model Izabella participates in the competition. She wants to impress judges and show her mathematical skills. Her problem is following: for given string, consisting of only 0 and 1, tell if it's possible to remove some digits in such a way, that remaining number is a representation of some positive integer, divisible by 64, in the binary numerical system.
In the only line given a non-empty binary string *s* with length up to 100.
Print Β«yesΒ» (without quotes) if it's possible to remove digits required way and Β«noΒ» otherwise.
[ "100010001\n", "100\n" ]
[ "yes", "no" ]
In the first test case, you can get string 1 000 000 after removing two ones which is a representation of number 64 in the binary numerical system. You can read more about binary numeral system representation here: [https://en.wikipedia.org/wiki/Binary_system](https://en.wikipedia.org/wiki/Binary_system)
[ { "input": "100010001", "output": "yes" }, { "input": "100", "output": "no" }, { "input": "0000001000000", "output": "yes" }, { "input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", "output": "no" }, { "input": "1111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111", "output": "no" }, { "input": "0111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", "output": "no" }, { "input": "1111011111111111111111111111110111110111111111111111111111011111111111111110111111111111111111111111", "output": "no" }, { "input": "1111111111101111111111111111111111111011111111111111111111111101111011111101111111111101111111111111", "output": "yes" }, { "input": "0110111111111111111111011111111110110111110111111111111111111111111111111111111110111111111111111111", "output": "yes" }, { "input": "1100110001111011001101101000001110111110011110111110010100011000100101000010010111100000010001001101", "output": "yes" }, { "input": "000000", "output": "no" }, { "input": "0001000", "output": "no" }, { "input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "no" }, { "input": "1000000", "output": "yes" }, { "input": "0", "output": "no" }, { "input": "1", "output": "no" }, { "input": "10000000000", "output": "yes" }, { "input": "0000000000", "output": "no" }, { "input": "0010000", "output": "no" }, { "input": "000000011", "output": "no" }, { "input": "000000000", "output": "no" }, { "input": "00000000", "output": "no" }, { "input": "000000000011", "output": "no" }, { "input": "0000000", "output": "no" }, { "input": "00000000011", "output": "no" }, { "input": "000000001", "output": "no" }, { "input": "000000000000000000000000000", "output": "no" }, { "input": "0000001", "output": "no" }, { "input": "00000001", "output": "no" }, { "input": "00000000100", "output": "no" }, { "input": "00000000000000000000", "output": "no" }, { "input": "0000000000000000000", "output": "no" }, { "input": "00001000", "output": "no" }, { "input": "0000000000010", "output": "no" }, { "input": "000000000010", "output": "no" }, { "input": "000000000000010", "output": "no" }, { "input": "0100000", "output": "no" }, { "input": "00010000", "output": "no" }, { "input": "00000000000000000", "output": "no" }, { "input": "00000000000", "output": "no" }, { "input": "000001000", "output": "no" }, { "input": "000000000000", "output": "no" }, { "input": "100000000000000", "output": "yes" }, { "input": "000010000", "output": "no" }, { "input": "00000100", "output": "no" }, { "input": "0001100000", "output": "no" }, { "input": "000000000000000000000000001", "output": "no" }, { "input": "000000100", "output": "no" }, { "input": "0000000000001111111111", "output": "no" }, { "input": "00000010", "output": "no" }, { "input": "0001110000", "output": "no" }, { "input": "0000000000000000000000", "output": "no" }, { "input": "000000010010", "output": "no" }, { "input": "0000100", "output": "no" }, { "input": "0000000001", "output": "no" }, { "input": "000000111", "output": "no" }, { "input": "0000000000000", "output": "no" }, { "input": "000000000000000000", "output": "no" }, { "input": "0000000000000000000000000", "output": "no" }, { "input": "000000000000000", "output": "no" }, { "input": "0010000000000100", "output": "yes" }, { "input": "0000001000", "output": "no" }, { "input": "00000000000000000001", "output": "no" }, { "input": "100000000", "output": "yes" }, { "input": "000000000001", "output": "no" }, { "input": "0000011001", "output": "no" }, { "input": "000", "output": "no" }, { "input": "000000000000000000000", "output": "no" }, { "input": "0000000000011", "output": "no" }, { "input": "0000000000000000", "output": "no" }, { "input": "00000000000000001", "output": "no" }, { "input": "00000000000000", "output": "no" }, { "input": "0000000000000000010", "output": "no" }, { "input": "00000000000000000000000000000000000000000000000000000000", "output": "no" }, { "input": "000011000", "output": "no" }, { "input": "00000011", "output": "no" }, { "input": "0000000000001100", "output": "no" }, { "input": "00000", "output": "no" }, { "input": "000000000000000000000000000111111111111111", "output": "no" }, { "input": "000000010", "output": "no" }, { "input": "00000000111", "output": "no" }, { "input": "000000000000001", "output": "no" }, { "input": "0000000000000011111111111111111", "output": "no" }, { "input": "0000000010", "output": "no" }, { "input": "0000000000000000000000000000000000000000000000000", "output": "no" }, { "input": "00000000010", "output": "no" }, { "input": "101000000000", "output": "yes" }, { "input": "00100000", "output": "no" }, { "input": "00000000000001", "output": "no" }, { "input": "0000000000100", "output": "no" }, { "input": "0000", "output": "no" }, { "input": "00000000000111", "output": "no" }, { "input": "0000000000000011", "output": "no" }, { "input": "0000000000000000000000000000000000000000", "output": "no" }, { "input": "0000000000000010", "output": "no" }, { "input": "0010101010", "output": "no" }, { "input": "0000000000000001", "output": "no" }, { "input": "1010101", "output": "no" } ]
93
0
3
2,193
157
Game Outcome
[ "brute force" ]
null
null
Sherlock Holmes and Dr. Watson played some game on a checkered board *n*<=Γ—<=*n* in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to count the number of winning squares. To determine if the particular square is winning you should do the following. Calculate the sum of all numbers on the squares that share this column (including the given square) and separately calculate the sum of all numbers on the squares that share this row (including the given square). A square is considered winning if the sum of the column numbers is strictly greater than the sum of the row numbers. For instance, lets game was ended like is shown in the picture. Then the purple cell is winning, because the sum of its column numbers equals 8<=+<=3<=+<=6<=+<=7<==<=24, sum of its row numbers equals 9<=+<=5<=+<=3<=+<=2<==<=19, and 24<=&gt;<=19.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=30). Each of the following *n* lines contain *n* space-separated integers. The *j*-th number on the *i*-th line represents the number on the square that belongs to the *j*-th column and the *i*-th row on the board. All number on the board are integers from 1 to 100.
Print the single number β€” the number of the winning squares.
[ "1\n1\n", "2\n1 2\n3 4\n", "4\n5 7 8 4\n9 5 3 2\n1 6 6 4\n9 5 7 3\n" ]
[ "0\n", "2\n", "6\n" ]
In the first example two upper squares are winning. In the third example three left squares in the both middle rows are winning:
[ { "input": "1\n1", "output": "0" }, { "input": "2\n1 2\n3 4", "output": "2" }, { "input": "4\n5 7 8 4\n9 5 3 2\n1 6 6 4\n9 5 7 3", "output": "6" }, { "input": "2\n1 1\n1 1", "output": "0" }, { "input": "3\n1 2 3\n4 5 6\n7 8 9", "output": "4" }, { "input": "3\n1 2 3\n3 1 2\n2 3 1", "output": "0" }, { "input": "4\n1 2 3 4\n8 7 6 5\n9 10 11 12\n16 15 14 13", "output": "8" }, { "input": "1\n53", "output": "0" }, { "input": "5\n1 98 22 9 39\n10 9 44 49 66\n79 17 23 8 47\n59 69 72 47 14\n94 91 98 19 54", "output": "13" }, { "input": "1\n31", "output": "0" }, { "input": "1\n92", "output": "0" }, { "input": "5\n61 45 70 19 48\n52 29 98 21 74\n21 66 12 6 55\n62 75 66 62 57\n94 74 9 86 24", "output": "13" }, { "input": "2\n73 99\n13 100", "output": "2" }, { "input": "4\n89 79 14 89\n73 24 58 89\n62 88 69 65\n58 92 18 83", "output": "10" }, { "input": "5\n99 77 32 20 49\n93 81 63 7 58\n37 1 17 35 53\n18 94 38 80 23\n91 50 42 61 63", "output": "12" }, { "input": "4\n81 100 38 54\n8 64 39 59\n6 12 53 65\n79 50 99 71", "output": "8" }, { "input": "5\n42 74 45 85 14\n68 94 11 3 89\n68 67 97 62 66\n65 76 96 18 84\n61 98 28 94 74", "output": "12" }, { "input": "9\n53 80 94 41 58 49 88 24 42\n85 11 32 64 40 56 63 95 73\n17 85 60 41 13 71 54 67 87\n38 14 21 81 66 59 52 33 86\n29 34 46 18 19 80 10 44 51\n4 27 65 75 77 21 15 49 50\n35 68 86 98 98 62 69 52 71\n43 28 56 91 89 21 14 57 79\n27 27 29 26 15 76 21 70 78", "output": "40" }, { "input": "7\n80 81 45 81 72 19 65\n31 24 15 52 47 1 14\n81 35 42 24 96 59 46\n16 2 59 56 60 98 76\n20 95 10 68 68 56 93\n60 16 68 77 89 52 43\n11 22 43 36 99 2 11", "output": "21" }, { "input": "9\n33 80 34 56 56 33 27 74 57\n14 69 78 44 56 70 26 73 47\n13 42 17 33 78 83 94 70 37\n96 78 92 6 16 68 8 31 46\n67 97 21 10 44 64 15 77 28\n34 44 83 96 63 52 29 27 79\n23 23 57 54 35 16 5 64 36\n29 71 36 78 47 81 72 97 36\n24 83 70 58 36 82 42 44 26", "output": "41" }, { "input": "9\n57 70 94 69 77 59 88 63 83\n6 79 46 5 9 43 20 39 48\n46 35 58 22 17 3 81 82 34\n77 10 40 53 71 84 14 58 56\n6 92 77 81 13 20 77 29 40\n59 53 3 97 21 97 22 11 64\n52 91 82 20 6 3 99 17 44\n79 25 43 69 85 55 95 61 31\n89 24 50 84 54 93 54 60 87", "output": "46" }, { "input": "5\n77 44 22 21 20\n84 3 35 86 35\n97 50 1 44 92\n4 88 56 20 3\n32 56 26 17 80", "output": "13" }, { "input": "7\n62 73 50 63 66 92 2\n27 13 83 84 88 81 47\n60 41 25 2 68 32 60\n7 94 18 98 41 25 72\n69 37 4 10 82 49 91\n76 26 67 27 30 49 18\n44 78 6 1 41 94 80", "output": "26" }, { "input": "9\n40 70 98 28 44 78 15 73 20\n25 74 46 3 27 59 33 96 19\n100 47 99 68 68 67 66 87 31\n26 39 8 91 58 20 91 69 81\n77 43 90 60 17 91 78 85 68\n41 46 47 50 96 18 69 81 26\n10 58 2 36 54 64 69 10 65\n6 86 26 7 88 20 43 92 59\n61 76 13 23 49 28 22 79 8", "output": "44" }, { "input": "8\n44 74 25 81 32 33 55 58\n36 13 28 28 20 65 87 58\n8 35 52 59 34 15 33 16\n2 22 42 29 11 66 30 72\n33 47 8 61 31 64 59 63\n79 36 38 42 12 21 92 36\n56 47 44 6 6 1 37 2\n79 88 79 53 50 69 94 39", "output": "31" }, { "input": "5\n4 91 100 8 48\n78 56 61 49 83\n12 21 95 77 78\n40 20 91 79 25\n32 88 94 28 55", "output": "10" }, { "input": "5\n23 70 5 36 69\n83 18 19 98 40\n84 91 18 51 35\n17 18 35 47 59\n29 72 35 87 27", "output": "13" }, { "input": "12\n8 42 23 20 39 5 23 86 26 65 93 82\n48 35 12 4 59 19 19 28 38 81 97 99\n93 24 31 44 97 50 44 99 50 7 10 64\n79 43 65 29 84 43 46 41 89 16 6 1\n34 90 33 1 7 12 46 84 67 30 1 58\n58 21 100 66 56 22 7 24 72 73 86 37\n2 17 85 6 2 73 85 44 43 79 34 65\n3 53 29 76 87 2 27 19 11 42 71 38\n69 82 73 52 44 23 92 10 13 72 59 16\n73 32 37 93 21 94 43 39 27 53 14 15\n86 16 90 91 14 50 73 61 77 36 93 90\n22 56 30 52 81 70 12 92 75 27 38 12", "output": "77" }, { "input": "3\n41 94 58\n73 61 8\n34 88 89", "output": "5" }, { "input": "3\n1 2 3\n1 1 1\n1 1 1", "output": "4" }, { "input": "2\n7 3\n9 5", "output": "2" }, { "input": "3\n4 3 2\n2 2 2\n2 2 2", "output": "4" } ]
218
307,200
3
2,196
996
World Cup
[ "binary search", "math" ]
null
null
Allen wants to enter a fan zone that occupies a round square and has $n$ entrances. There already is a queue of $a_i$ people in front of the $i$-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute. Allen uses the following strategy to enter the fan zone: - Initially he stands in the end of the queue in front of the first entrance. - Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance). Determine the entrance through which Allen will finally enter the fan zone.
The first line contains a single integer $n$ ($2 \le n \le 10^5$)Β β€” the number of entrances. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β€” the number of people in queues. These numbers do not include Allen.
Print a single integerΒ β€” the number of entrance that Allen will use.
[ "4\n2 3 2 0\n", "2\n10 10\n", "6\n5 2 6 5 7 4\n" ]
[ "3\n", "1\n", "6\n" ]
In the first example the number of people (not including Allen) changes as follows: $[\textbf{2}, 3, 2, 0] \to [1, \textbf{2}, 1, 0] \to [0, 1, \textbf{0}, 0]$. The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance. In the second example the number of people (not including Allen) changes as follows: $[\textbf{10}, 10] \to [9, \textbf{9}] \to [\textbf{8}, 8] \to [7, \textbf{7}] \to [\textbf{6}, 6] \to \\ [5, \textbf{5}] \to [\textbf{4}, 4] \to [3, \textbf{3}] \to [\textbf{2}, 2] \to [1, \textbf{1}] \to [\textbf{0}, 0]$. In the third example the number of people (not including Allen) changes as follows: $[\textbf{5}, 2, 6, 5, 7, 4] \to [4, \textbf{1}, 5, 4, 6, 3] \to [3, 0, \textbf{4}, 3, 5, 2] \to \\ [2, 0, 3, \textbf{2}, 4, 1] \to [1, 0, 2, 1, \textbf{3}, 0] \to [0, 0, 1, 0, 2, \textbf{0}]$.
[ { "input": "4\n2 3 2 0", "output": "3" }, { "input": "2\n10 10", "output": "1" }, { "input": "6\n5 2 6 5 7 4", "output": "6" }, { "input": "2\n483544186 940350702", "output": "1" }, { "input": "10\n3 3 3 5 6 9 3 1 7 3", "output": "7" }, { "input": "10\n0 8 45 88 48 68 28 55 17 24", "output": "1" }, { "input": "100\n8 8 9 10 6 8 2 4 2 2 10 6 6 10 10 2 3 5 1 2 10 4 2 0 9 4 9 3 0 6 3 2 3 10 10 6 4 6 4 4 2 5 1 4 1 1 9 8 9 5 3 5 5 4 5 5 6 5 3 3 7 2 0 10 9 7 7 3 5 1 0 9 6 3 1 3 4 4 3 6 3 2 1 4 10 2 3 4 4 3 6 7 6 2 1 7 0 6 8 10", "output": "7" }, { "input": "10\n5 6 7 8 9 10 11 12 13 14", "output": "1" }, { "input": "10\n15 14 13 12 11 10 9 8 7 6", "output": "9" }, { "input": "2\n1000000000 1000000000", "output": "1" }, { "input": "3\n3 3 1", "output": "3" }, { "input": "10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "1" }, { "input": "3\n15 8 9", "output": "2" }, { "input": "3\n5 5 5", "output": "3" }, { "input": "3\n41 5 6", "output": "2" }, { "input": "2\n999999999 1000000000", "output": "1" }, { "input": "2\n0 1", "output": "1" }, { "input": "3\n3 2 3", "output": "1" }, { "input": "3\n8 5 8", "output": "2" }, { "input": "4\n5 2 3 4", "output": "2" }, { "input": "2\n0 0", "output": "1" }, { "input": "2\n3 3", "output": "2" }, { "input": "4\n9 2 4 7", "output": "2" }, { "input": "30\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "11" }, { "input": "4\n11 10 12 12", "output": "1" }, { "input": "2\n1 0", "output": "2" }, { "input": "6\n7 2 6 5 7 9", "output": "2" }, { "input": "3\n1000000000 1000000000 1000000000", "output": "2" }, { "input": "2\n999999999 999999699", "output": "2" }, { "input": "5\n5 5 5 5 5", "output": "1" }, { "input": "2\n1 1", "output": "2" } ]
61
0
0
2,199
883
Quadcopter Competition
[ "greedy", "math" ]
null
null
Polycarp takes part in a quadcopter competition. According to the rules a flying robot should: - start the race from some point of a field, - go around the flag, - close cycle returning back to the starting point. Polycarp knows the coordinates of the starting point (*x*1,<=*y*1) and the coordinates of the point where the flag is situated (*x*2,<=*y*2). Polycarp’s quadcopter can fly only parallel to the sides of the field each tick changing exactly one coordinate by 1. It means that in one tick the quadcopter can fly from the point (*x*,<=*y*) to any of four points: (*x*<=-<=1,<=*y*), (*x*<=+<=1,<=*y*), (*x*,<=*y*<=-<=1) or (*x*,<=*y*<=+<=1). Thus the quadcopter path is a closed cycle starting and finishing in (*x*1,<=*y*1) and containing the point (*x*2,<=*y*2) strictly inside. What is the minimal length of the quadcopter path?
The first line contains two integer numbers *x*1 and *y*1 (<=-<=100<=≀<=*x*1,<=*y*1<=≀<=100) β€” coordinates of the quadcopter starting (and finishing) point. The second line contains two integer numbers *x*2 and *y*2 (<=-<=100<=≀<=*x*2,<=*y*2<=≀<=100) β€” coordinates of the flag. It is guaranteed that the quadcopter starting point and the flag do not coincide.
Print the length of minimal path of the quadcopter to surround the flag and return back.
[ "1 5\n5 2\n", "0 1\n0 0\n" ]
[ "18\n", "8\n" ]
none
[ { "input": "1 5\n5 2", "output": "18" }, { "input": "0 1\n0 0", "output": "8" }, { "input": "-100 -100\n100 100", "output": "804" }, { "input": "-100 -100\n-100 100", "output": "406" }, { "input": "-100 -100\n100 -100", "output": "406" }, { "input": "100 -100\n-100 -100", "output": "406" }, { "input": "100 -100\n-100 100", "output": "804" }, { "input": "100 -100\n100 100", "output": "406" }, { "input": "-100 100\n-100 -100", "output": "406" }, { "input": "-100 100\n100 -100", "output": "804" }, { "input": "-100 100\n100 100", "output": "406" }, { "input": "100 100\n-100 -100", "output": "804" }, { "input": "100 100\n-100 100", "output": "406" }, { "input": "100 100\n100 -100", "output": "406" }, { "input": "45 -43\n45 -44", "output": "8" }, { "input": "76 76\n75 75", "output": "8" }, { "input": "-34 -56\n-35 -56", "output": "8" }, { "input": "56 -7\n55 -6", "output": "8" }, { "input": "43 -11\n43 -10", "output": "8" }, { "input": "1 -3\n2 -2", "output": "8" }, { "input": "55 71\n56 71", "output": "8" }, { "input": "54 -87\n55 -88", "output": "8" }, { "input": "22 98\n100 33", "output": "290" }, { "input": "37 84\n-83 5", "output": "402" }, { "input": "52 74\n-73 -39", "output": "480" }, { "input": "66 51\n51 -71", "output": "278" }, { "input": "-31 44\n73 86", "output": "296" }, { "input": "-20 34\n-9 55", "output": "68" }, { "input": "-5 19\n-91 -86", "output": "386" }, { "input": "-82 5\n28 -17", "output": "268" }, { "input": "-90 -100\n55 48", "output": "590" }, { "input": "-75 -14\n-32 8", "output": "134" }, { "input": "-53 -28\n-13 -28", "output": "86" }, { "input": "-42 -46\n10 -64", "output": "144" }, { "input": "55 -42\n25 2", "output": "152" }, { "input": "70 -64\n-54 70", "output": "520" }, { "input": "93 -78\n-32 -75", "output": "260" }, { "input": "8 -93\n79 -6", "output": "320" }, { "input": "50 43\n54 10", "output": "78" }, { "input": "65 32\n-37 71", "output": "286" }, { "input": "80 18\n-15 -58", "output": "346" }, { "input": "94 92\n4 -1", "output": "370" }, { "input": "-10 96\n27 64", "output": "142" }, { "input": "-96 78\n-56 32", "output": "176" }, { "input": "-81 64\n-37 -8", "output": "236" }, { "input": "-58 49\n74 -40", "output": "446" }, { "input": "-62 -55\n1 18", "output": "276" }, { "input": "-51 -69\n-78 86", "output": "368" }, { "input": "-29 -80\n-56 -47", "output": "124" }, { "input": "-14 -94\n55 -90", "output": "150" }, { "input": "83 -2\n82 83", "output": "176" }, { "input": "98 -16\n-96 40", "output": "504" }, { "input": "17 -34\n-86 -93", "output": "328" }, { "input": "32 -48\n33 -37", "output": "28" }, { "input": "74 87\n3 92", "output": "156" }, { "input": "89 73\n-80 49", "output": "390" }, { "input": "4 58\n-61 -80", "output": "410" }, { "input": "15 48\n50 -20", "output": "210" }, { "input": "-82 45\n81 46", "output": "332" }, { "input": "-68 26\n-2 6", "output": "176" }, { "input": "-53 4\n-92 -31", "output": "152" }, { "input": "-30 94\n31 -58", "output": "430" }, { "input": "-38 -11\n58 99", "output": "416" }, { "input": "-27 -25\n-28 68", "output": "192" }, { "input": "-5 -39\n-10 -77", "output": "90" }, { "input": "-90 -54\n9 -9", "output": "292" }, { "input": "7 -57\n28 61", "output": "282" }, { "input": "18 -67\n-51 21", "output": "318" }, { "input": "41 -82\n-33 -15", "output": "286" }, { "input": "56 -8\n91 -55", "output": "168" }, { "input": "-23 -13\n-24 -12", "output": "8" }, { "input": "1 32\n1 33", "output": "8" }, { "input": "25 76\n24 76", "output": "8" }, { "input": "-29 -78\n-28 -79", "output": "8" }, { "input": "-77 19\n-76 19", "output": "8" }, { "input": "-53 63\n-53 62", "output": "8" }, { "input": "86 12\n86 11", "output": "8" }, { "input": "14 56\n13 56", "output": "8" }, { "input": "63 41\n62 40", "output": "8" }, { "input": "0 -4\n1 -4", "output": "8" }, { "input": "24 41\n24 42", "output": "8" }, { "input": "48 85\n49 86", "output": "8" }, { "input": "0 0\n0 1", "output": "8" }, { "input": "0 0\n1 0", "output": "8" }, { "input": "0 0\n1 1", "output": "8" }, { "input": "0 1\n0 0", "output": "8" }, { "input": "0 1\n1 0", "output": "8" }, { "input": "0 1\n1 1", "output": "8" }, { "input": "1 0\n0 0", "output": "8" }, { "input": "1 0\n0 1", "output": "8" }, { "input": "1 0\n1 1", "output": "8" }, { "input": "1 1\n0 0", "output": "8" }, { "input": "1 1\n0 1", "output": "8" }, { "input": "1 1\n1 0", "output": "8" }, { "input": "100 100\n99 -100", "output": "406" }, { "input": "100 100\n-100 99", "output": "406" }, { "input": "-100 -100\n-99 100", "output": "406" }, { "input": "-100 -100\n100 -99", "output": "406" }, { "input": "0 0\n1 2", "output": "10" }, { "input": "0 0\n2 1", "output": "10" } ]
62
4,608,000
0
2,202
975
Aramic script
[ "implementation", "strings" ]
null
null
In Aramic language words can only represent objects. Words in Aramic have special properties: - A word is a root if it does not contain the same letter more than once. - A root and all its permutations represent the same object. - The root $x$ of a word $y$ is the word that contains all letters that appear in $y$ in a way that each letter appears once. For example, the root of "aaaa", "aa", "aaa" is "a", the root of "aabb", "bab", "baabb", "ab" is "ab". - Any word in Aramic represents the same object as its root. You have an ancient script in Aramic. What is the number of different objects mentioned in the script?
The first line contains one integer $n$ ($1 \leq n \leq 10^3$)Β β€” the number of words in the script. The second line contains $n$ words $s_1, s_2, \ldots, s_n$Β β€” the script itself. The length of each string does not exceed $10^3$. It is guaranteed that all characters of the strings are small latin letters.
Output one integerΒ β€” the number of different objects mentioned in the given ancient Aramic script.
[ "5\na aa aaa ab abb\n", "3\namer arem mrea\n" ]
[ "2", "1" ]
In the first test, there are two objects mentioned. The roots that represent them are "a","ab". In the second test, there is only one object, its root is "amer", the other strings are just permutations of "amer".
[ { "input": "5\na aa aaa ab abb", "output": "2" }, { "input": "3\namer arem mrea", "output": "1" }, { "input": "10\nbda bbb cda dca dda dcb bcd dcb ada ddd", "output": "6" }, { "input": "2\nfhjlqs aceginpr", "output": "2" }, { "input": "2\nbcdfghimn efghijlmo", "output": "2" } ]
1,000
10,035,200
0
2,205
569
Inventory
[ "greedy", "math" ]
null
null
Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep the track of everything. During an audit, you were surprised to find out that the items are not numbered sequentially, and some items even share the same inventory number! There is an urgent need to fix it. You have chosen to make the numbers of the items sequential, starting with 1. Changing a number is quite a time-consuming process, and you would like to make maximum use of the current numbering. You have been given information on current inventory numbers for *n* items in the company. Renumber items so that their inventory numbers form a permutation of numbers from 1 to *n* by changing the number of as few items as possible. Let us remind you that a set of *n* numbers forms a permutation if all the numbers are in the range from 1 to *n*, and no two numbers are equal.
The first line contains a single integer *n*Β β€” the number of items (1<=≀<=*n*<=≀<=105). The second line contains *n* numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=105)Β β€” the initial inventory numbers of the items.
Print *n* numbersΒ β€” the final inventory numbers of the items in the order they occur in the input. If there are multiple possible answers, you may print any of them.
[ "3\n1 3 2\n", "4\n2 2 3 3\n", "1\n2\n" ]
[ "1 3 2 \n", "2 1 3 4 \n", "1 \n" ]
In the first test the numeration is already a permutation, so there is no need to change anything. In the second test there are two pairs of equal numbers, in each pair you need to replace one number. In the third test you need to replace 2 by 1, as the numbering should start from one.
[ { "input": "3\n1 3 2", "output": "1 3 2 " }, { "input": "4\n2 2 3 3", "output": "2 1 3 4 " }, { "input": "1\n2", "output": "1 " }, { "input": "3\n3 3 1", "output": "3 2 1 " }, { "input": "5\n1 1 1 1 1", "output": "1 2 3 4 5 " }, { "input": "5\n5 3 4 4 2", "output": "5 3 4 1 2 " }, { "input": "5\n19 11 8 8 10", "output": "1 2 3 4 5 " }, { "input": "15\n2 2 1 2 1 2 3 3 1 3 2 1 2 3 2", "output": "2 4 1 5 6 7 3 8 9 10 11 12 13 14 15 " }, { "input": "18\n3 11 5 9 5 4 6 4 5 7 5 1 8 11 11 2 1 9", "output": "3 11 5 9 10 4 6 12 13 7 14 1 8 15 16 2 17 18 " }, { "input": "42\n999 863 440 1036 1186 908 330 265 382 417 858 286 834 922 42 569 79 158 312 1175 1069 188 21 1207 985 375 59 417 256 595 732 742 629 737 25 699 484 517 37 1134 472 720", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 42 15 16 17 18 19 20 22 21 23 24 26 27 28 29 30 31 32 33 34 25 35 36 38 37 39 40 41 " }, { "input": "111\n15 45 14 65 49 25 102 86 14 80 54 73 43 78 42 32 47 60 55 66 84 69 49 22 26 72 89 52 26 80 71 35 56 2 88 23 23 53 65 92 46 73 29 65 88 99 19 99 87 10 47 96 109 20 60 89 63 105 29 92 109 20 95 65 31 89 107 3 3 50 58 9 28 39 104 42 41 36 70 49 59 96 16 9 3 108 38 42 2 67 32 86 20 6 101 70 101 91 38 10 74 3 27 15 103 63 51 60 62 10 70", "output": "15 45 14 65 49 25 102 86 1 80 54 73 43 78 42 32 47 60 55 66 84 69 4 22 26 72 89 52 5 7 71 35 56 2 88 23 8 53 11 92 46 12 29 13 17 99 19 18 87 10 21 96 109 20 24 30 63 105 33 34 37 40 95 44 31 48 107 3 57 50 58 9 28 39 104 61 41 36 70 64 59 68 16 75 76 108 38 77 79 67 81 82 83 6 101 85 90 91 93 94 74 97 27 98 103 100 51 106 62 110 111 " }, { "input": "7\n45301 14370 61599 42695 46301 24556 26812", "output": "1 2 3 4 5 6 7 " }, { "input": "22\n70150 17718 11731 6488 72633 41249 12141 71465 88562 6167 71659 34151 60508 24942 77343 35882 80424 67225 92746 55412 79 53642", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 " }, { "input": "2\n1 4", "output": "1 2 " } ]
280
18,739,200
3
2,209
915
Browser
[ "implementation" ]
null
null
Luba is surfing the Internet. She currently has *n* opened tabs in her browser, indexed from 1 to *n* from left to right. The mouse cursor is currently located at the *pos*-th tab. Luba needs to use the tabs with indices from *l* to *r* (inclusive) for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible. Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab *i*, then she can move it to the tab *max*(*i*<=-<=1,<=*a*) or to the tab *min*(*i*<=+<=1,<=*b*)) or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab *i*, she can close all the tabs with indices from segment [*a*,<=*i*<=-<=1] or from segment [*i*<=+<=1,<=*b*]). In the aforementioned expressions *a* and *b* denote the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7 are closed, then *a*<==<=3, *b*<==<=6. What is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from *l* to *r* inclusive opened?
The only line of input contains four integer numbers *n*, *pos*, *l*, *r* (1<=≀<=*n*<=≀<=100, 1<=≀<=*pos*<=≀<=*n*, 1<=≀<=*l*<=≀<=*r*<=≀<=*n*) β€” the number of the tabs, the cursor position and the segment which Luba needs to leave opened.
Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment [*l*,<=*r*].
[ "6 3 2 4\n", "6 3 1 3\n", "5 2 1 5\n" ]
[ "5\n", "1\n", "0\n" ]
In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the mouse cursor to the tab 3, then to the tab 4, and then close all the tabs to the right of it. In the second test she only needs to close all the tabs to the right of the current position of the cursor. In the third test Luba doesn't need to do anything.
[ { "input": "6 3 2 4", "output": "5" }, { "input": "6 3 1 3", "output": "1" }, { "input": "5 2 1 5", "output": "0" }, { "input": "100 1 1 99", "output": "99" }, { "input": "100 50 1 99", "output": "50" }, { "input": "100 99 1 99", "output": "1" }, { "input": "100 100 1 99", "output": "2" }, { "input": "100 50 2 100", "output": "49" }, { "input": "100 1 100 100", "output": "100" }, { "input": "100 50 50 50", "output": "2" }, { "input": "6 4 2 5", "output": "6" }, { "input": "100 5 2 50", "output": "53" }, { "input": "10 7 3 9", "output": "10" }, { "input": "7 4 2 5", "output": "6" }, { "input": "43 16 2 18", "output": "20" }, { "input": "100 50 2 51", "output": "52" }, { "input": "6 5 2 4", "output": "5" }, { "input": "10 5 2 7", "output": "9" }, { "input": "10 10 2 9", "output": "10" }, { "input": "10 7 3 7", "output": "6" }, { "input": "64 64 8 44", "output": "58" }, { "input": "5 4 2 4", "output": "4" }, { "input": "6 6 3 5", "output": "5" }, { "input": "10 6 2 7", "output": "8" }, { "input": "8 6 2 7", "output": "8" }, { "input": "7 5 2 4", "output": "5" }, { "input": "7 5 2 6", "output": "7" }, { "input": "100 50 49 99", "output": "53" }, { "input": "100 50 2 99", "output": "147" }, { "input": "10 9 2 9", "output": "9" }, { "input": "10 10 7 9", "output": "5" }, { "input": "8 4 2 7", "output": "9" }, { "input": "100 50 2 2", "output": "50" }, { "input": "10 4 3 7", "output": "7" }, { "input": "6 3 2 5", "output": "6" }, { "input": "53 17 13 18", "output": "8" }, { "input": "10 6 3 6", "output": "5" }, { "input": "9 8 2 5", "output": "8" }, { "input": "100 50 2 3", "output": "50" }, { "input": "10 7 2 9", "output": "11" }, { "input": "6 1 2 5", "output": "6" }, { "input": "7 6 2 4", "output": "6" }, { "input": "26 12 2 4", "output": "12" }, { "input": "10 8 3 7", "output": "7" }, { "input": "100 97 3 98", "output": "98" }, { "input": "6 2 2 4", "output": "4" }, { "input": "9 2 4 6", "output": "6" }, { "input": "6 6 2 4", "output": "6" }, { "input": "50 2 25 49", "output": "49" }, { "input": "5 5 2 3", "output": "5" }, { "input": "49 11 2 17", "output": "23" }, { "input": "10 3 2 9", "output": "10" }, { "input": "10 6 3 7", "output": "7" }, { "input": "6 1 5 5", "output": "6" }, { "input": "5 5 3 4", "output": "4" }, { "input": "10 2 5 6", "output": "6" }, { "input": "7 7 3 4", "output": "6" }, { "input": "7 3 2 3", "output": "3" }, { "input": "5 1 2 4", "output": "5" }, { "input": "100 53 2 99", "output": "145" }, { "input": "10 2 4 7", "output": "7" }, { "input": "5 2 1 4", "output": "3" }, { "input": "100 65 41 84", "output": "64" }, { "input": "33 20 7 17", "output": "15" }, { "input": "7 2 3 6", "output": "6" }, { "input": "77 64 10 65", "output": "58" }, { "input": "6 1 3 4", "output": "5" }, { "input": "6 4 2 4", "output": "4" }, { "input": "11 8 2 10", "output": "12" }, { "input": "7 1 3 6", "output": "7" }, { "input": "100 50 2 50", "output": "50" }, { "input": "50 49 5 8", "output": "46" }, { "input": "15 1 10 13", "output": "14" }, { "input": "13 9 5 11", "output": "10" }, { "input": "20 3 5 8", "output": "7" }, { "input": "10 5 2 3", "output": "5" }, { "input": "7 1 3 5", "output": "6" }, { "input": "7 2 3 4", "output": "4" }, { "input": "10 5 2 5", "output": "5" }, { "input": "8 5 2 6", "output": "7" }, { "input": "8 5 3 6", "output": "6" }, { "input": "9 6 3 7", "output": "7" }, { "input": "50 46 34 37", "output": "14" }, { "input": "10 7 2 8", "output": "9" }, { "input": "8 3 1 4", "output": "2" }, { "input": "100 3 10 20", "output": "19" }, { "input": "6 2 1 5", "output": "4" }, { "input": "12 11 5 10", "output": "8" }, { "input": "98 97 72 83", "output": "27" }, { "input": "100 5 3 98", "output": "99" }, { "input": "8 5 2 7", "output": "9" }, { "input": "10 10 4 6", "output": "8" }, { "input": "10 4 2 5", "output": "6" }, { "input": "3 3 2 3", "output": "2" }, { "input": "75 30 6 33", "output": "32" }, { "input": "4 3 2 3", "output": "3" }, { "input": "2 2 1 1", "output": "2" }, { "input": "2 2 1 2", "output": "0" }, { "input": "1 1 1 1", "output": "0" }, { "input": "20 9 7 17", "output": "14" }, { "input": "10 2 3 7", "output": "7" }, { "input": "100 40 30 80", "output": "62" }, { "input": "10 6 2 3", "output": "6" }, { "input": "7 3 2 5", "output": "6" }, { "input": "10 6 2 9", "output": "12" }, { "input": "23 20 19 22", "output": "6" }, { "input": "100 100 1 1", "output": "100" }, { "input": "10 2 5 9", "output": "9" }, { "input": "9 7 2 8", "output": "9" }, { "input": "100 50 50 100", "output": "1" }, { "input": "3 1 2 2", "output": "3" }, { "input": "16 13 2 15", "output": "17" }, { "input": "9 8 2 6", "output": "8" }, { "input": "43 22 9 24", "output": "19" }, { "input": "5 4 2 3", "output": "4" }, { "input": "82 72 66 75", "output": "14" }, { "input": "7 4 5 6", "output": "4" }, { "input": "100 50 51 51", "output": "3" }, { "input": "6 5 2 6", "output": "4" }, { "input": "4 4 2 2", "output": "4" }, { "input": "4 3 2 4", "output": "2" }, { "input": "2 2 2 2", "output": "1" }, { "input": "6 1 2 4", "output": "5" }, { "input": "2 1 1 1", "output": "1" }, { "input": "4 2 2 3", "output": "3" }, { "input": "2 1 1 2", "output": "0" }, { "input": "5 4 1 2", "output": "3" }, { "input": "100 100 2 99", "output": "100" }, { "input": "10 6 3 4", "output": "5" }, { "input": "100 74 30 60", "output": "46" }, { "input": "4 1 2 3", "output": "4" }, { "input": "100 50 3 79", "output": "107" }, { "input": "10 6 2 8", "output": "10" }, { "input": "100 51 23 33", "output": "30" }, { "input": "3 1 2 3", "output": "2" }, { "input": "29 13 14 23", "output": "12" }, { "input": "6 5 2 5", "output": "5" }, { "input": "10 2 3 5", "output": "5" }, { "input": "9 3 1 6", "output": "4" }, { "input": "45 33 23 37", "output": "20" }, { "input": "100 99 1 98", "output": "2" }, { "input": "100 79 29 68", "output": "52" }, { "input": "7 7 6 6", "output": "3" }, { "input": "100 4 30 60", "output": "58" }, { "input": "100 33 50 50", "output": "19" }, { "input": "50 2 34 37", "output": "37" }, { "input": "100 70 2 99", "output": "128" }, { "input": "6 6 4 4", "output": "4" }, { "input": "41 24 14 19", "output": "12" }, { "input": "100 54 52 55", "output": "6" }, { "input": "10 5 3 6", "output": "6" }, { "input": "6 5 4 6", "output": "2" }, { "input": "10 9 2 3", "output": "9" }, { "input": "6 4 2 3", "output": "4" }, { "input": "100 68 5 49", "output": "65" }, { "input": "8 4 3 6", "output": "6" }, { "input": "9 3 2 8", "output": "9" }, { "input": "100 50 1 1", "output": "50" }, { "input": "10 9 5 9", "output": "6" }, { "input": "62 54 2 54", "output": "54" }, { "input": "100 54 30 60", "output": "38" }, { "input": "6 6 6 6", "output": "1" }, { "input": "10 2 2 9", "output": "9" }, { "input": "50 3 23 25", "output": "24" }, { "input": "24 1 5 18", "output": "19" }, { "input": "43 35 23 34", "output": "14" }, { "input": "50 46 23 26", "output": "25" }, { "input": "10 8 5 9", "output": "7" }, { "input": "6 2 2 5", "output": "5" }, { "input": "43 1 13 41", "output": "42" }, { "input": "13 2 1 5", "output": "4" }, { "input": "6 3 3 5", "output": "4" }, { "input": "14 10 4 12", "output": "12" }, { "input": "5 1 4 4", "output": "5" }, { "input": "3 3 1 1", "output": "3" }, { "input": "17 17 12 14", "output": "7" }, { "input": "20 15 6 7", "output": "11" }, { "input": "86 36 8 70", "output": "92" }, { "input": "100 69 39 58", "output": "32" }, { "input": "3 3 2 2", "output": "3" }, { "input": "3 2 1 1", "output": "2" }, { "input": "9 7 3 8", "output": "8" }, { "input": "4 4 2 3", "output": "4" }, { "input": "100 4 2 5", "output": "6" }, { "input": "100 65 5 13", "output": "62" }, { "input": "3 2 2 3", "output": "1" }, { "input": "44 38 20 28", "output": "20" }, { "input": "100 65 58 60", "output": "9" }, { "input": "16 12 8 13", "output": "8" }, { "input": "11 8 4 9", "output": "8" }, { "input": "20 9 2 10", "output": "11" }, { "input": "5 5 4 5", "output": "2" }, { "input": "100 99 1 50", "output": "50" }, { "input": "6 5 3 5", "output": "4" }, { "input": "50 29 7 48", "output": "62" }, { "input": "26 11 1 24", "output": "14" }, { "input": "5 2 3 4", "output": "4" }, { "input": "100 1 2 3", "output": "4" }, { "input": "100 60 27 56", "output": "35" }, { "input": "6 4 2 6", "output": "3" }, { "input": "8 7 3 5", "output": "6" }, { "input": "4 1 3 3", "output": "4" }, { "input": "12 9 2 10", "output": "11" }, { "input": "100 25 9 19", "output": "18" }, { "input": "10 7 3 8", "output": "8" }, { "input": "7 3 2 6", "output": "7" }, { "input": "100 39 4 40", "output": "39" }, { "input": "100 51 2 99", "output": "147" }, { "input": "15 6 4 10", "output": "10" }, { "input": "10 4 4 9", "output": "7" }, { "input": "6 4 3 4", "output": "3" }, { "input": "14 7 4 12", "output": "13" }, { "input": "4 4 1 2", "output": "3" }, { "input": "6 5 2 3", "output": "5" }, { "input": "12 12 5 5", "output": "9" }, { "input": "10 5 3 5", "output": "4" }, { "input": "8 6 2 2", "output": "6" }, { "input": "8 7 2 7", "output": "7" }, { "input": "100 33 5 60", "output": "84" }, { "input": "100 32 5 60", "output": "84" }, { "input": "79 5 3 5", "output": "4" }, { "input": "85 85 85 85", "output": "1" }, { "input": "69 69 69 69", "output": "1" }, { "input": "7 5 3 6", "output": "6" }, { "input": "7 4 2 6", "output": "8" }, { "input": "2 1 2 2", "output": "2" }, { "input": "100 2 1 90", "output": "89" }, { "input": "100 89 11 90", "output": "82" }, { "input": "10 1 2 8", "output": "9" } ]
46
0
0
2,215
408
Garland
[ "implementation" ]
null
null
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought *n* colored sheets of paper, the area of each sheet is 1 square meter. The garland must consist of exactly *m* pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland. Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ​​*m* pieces of paper in the garland. Calculate what the maximum total area of ​​the pieces of paper in the garland Vasya can get.
The first line contains a non-empty sequence of *n* (1<=≀<=*n*<=≀<=1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of *m* (1<=≀<=*m*<=≀<=1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
[ "aaabbac\naabbccac\n", "a\nz\n" ]
[ "6\n", "-1" ]
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color *b*, three (but not four) sheets of color *a* and cut a single sheet of color *c* in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6. In the second test sample Vasya cannot make a garland at all β€” he doesn't have a sheet of color *z*.
[ { "input": "aaabbac\naabbccac", "output": "6" }, { "input": "a\nz", "output": "-1" }, { "input": "r\nr", "output": "1" }, { "input": "stnsdn\nndnndsn", "output": "4" }, { "input": "yqfqfp\ntttwtqq", "output": "-1" }, { "input": "zzbbrrtrtzr\ntbbtrrrzr", "output": "9" }, { "input": "ivvfisvsvii\npaihjinno", "output": "-1" }, { "input": "zbvwnlgkshqerxptyod\nz", "output": "1" }, { "input": "xlktwjymocqrahnbesf\nfoo", "output": "2" }, { "input": "bbzmzqazmbambnmzaabznmbabzqnaabmabmnnabbmnzaanzzezebzabqaabzqaemeqqammmbazmmz\naznnbbmeebmanbeemzmemqbaeebnqenqzzbanebmnzqqebqmmnmqqzmmeqqqaaezemmazqqmqaqnnqqzbzeeazammmenbbamzbmnaenemenaaaebnmanebqmqnznqbenmqqnnnaeaebqmamennmqqeaaqqbammnzqmnmqnqbbezmemznqmanzmmqzzzzembqnzqbanamezqaqbazenenqqznqaebzaeezbqqbmeeaqnmmbnqbbnmaqqemaeaezaabmbnbzzaae", "output": "77" }, { "input": "lccfdfnfflncddlksndcwnfcllnnaswcdnldafcalssfcdnkkaklwnnacsncfwanwnwfadawcsdcfwflnnlfsfclkfknlaldna\nuaaldlllhedgugugueahddhedbuddaedhaaeugdubaealbgbagedldbl", "output": "-1" }, { "input": "hvewdvwdwudrwarwmddwnazmwauzatrmwptwwevavpmwrtruwnpwantewrnwmepdwvtmnveanunrvrtwpvhhnuhnmpptdttzmmndtvudmzhhannmmnahdpzamuvhzaavnhtnumwrwvttdetvuewaaennddwuvzvaptdzrzhtetwwzmzedrwuvrwznprhdvnavrruvvhzuwpdtmpwmzrwvermrhdamv\nuvzhwtpuputnahwwarduzddhpnwwvettprwavdmnztdnrddmarmvuevtdezndnezvarhmppwwnmvnrtddzhhnzrwuhvpwmezuurundarwdazwptrpeurrnwautddnhdmhtwhwvvtavdzezumetzezpazndhuentmrhamutrtttpevtuutemdnvwnwnmnvmznatneweuaahdavmaddhnrdenwwztrwh", "output": "199" }, { "input": "aaccddff\nabcdf", "output": "-1" } ]
30
0
-1
2,218
598
Nearest vectors
[ "geometry", "sortings" ]
null
null
You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them. Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and Ο€. For example, opposite directions vectors have angle equals to Ο€.
First line of the input contains a single integer *n* (2<=≀<=*n*<=≀<=100<=000)Β β€” the number of vectors. The *i*-th of the following *n* lines contains two integers *x**i* and *y**i* (|*x*|,<=|*y*|<=≀<=10<=000,<=*x*2<=+<=*y*2<=&gt;<=0)Β β€” the coordinates of the *i*-th vector. Vectors are numbered from 1 to *n* in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).
Print two integer numbers *a* and *b* (*a*<=β‰ <=*b*)Β β€” a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.
[ "4\n-1 0\n0 -1\n1 0\n1 1\n", "6\n-1 0\n0 -1\n1 0\n1 1\n-4 -5\n-4 -6\n" ]
[ "3 4\n", "6 5" ]
none
[ { "input": "4\n-1 0\n0 -1\n1 0\n1 1", "output": "3 4" }, { "input": "6\n-1 0\n0 -1\n1 0\n1 1\n-4 -5\n-4 -6", "output": "5 6" }, { "input": "10\n8 6\n-7 -3\n9 8\n7 10\n-3 -8\n3 7\n6 -8\n-9 8\n9 2\n6 7", "output": "1 3" }, { "input": "20\n-9 8\n-7 3\n0 10\n3 7\n6 -9\n6 8\n7 -6\n-6 10\n-10 3\n-8 -10\n10 -2\n1 -8\n-8 10\n10 10\n10 6\n-5 6\n5 -8\n5 -9\n-9 -1\n9 2", "output": "13 16" }, { "input": "2\n351 -4175\n-328 -657", "output": "2 1" }, { "input": "3\n620 -1189\n8101 -2770\n3347 3473", "output": "1 2" }, { "input": "4\n-7061 -5800\n-3471 -9470\n-7639 2529\n5657 -6522", "output": "1 2" }, { "input": "5\n-7519 -3395\n-32 -257\n-4827 -1889\n9545 -7037\n2767 583", "output": "3 1" }, { "input": "6\n-5120 -3251\n8269 -7984\n841 3396\n3136 -7551\n-1280 -3013\n-3263 -3278", "output": "1 6" }, { "input": "7\n-2722 6597\n-3303 200\n6508 -1021\n-1107 -1042\n6875 7616\n-3047 6749\n662 -1979", "output": "1 6" }, { "input": "8\n-36 749\n5126 943\n1165 533\n-1647 -5725\n5031 6532\n5956 8447\n2297 -2284\n1986 6937", "output": "5 6" }, { "input": "9\n-391 -1706\n995 -5756\n-5013 -154\n1121 3160\n-7111 8303\n-7303 -2414\n-7791 -935\n7576 -9361\n1072 203", "output": "3 7" }, { "input": "10\n-9920 -5477\n9691 -3200\n754 885\n-1895 1768\n-941 1588\n6293 -2631\n-2288 9129\n4067 696\n-6754 9869\n-5747 701", "output": "5 9" }, { "input": "2\n1 0\n-1 0", "output": "1 2" }, { "input": "2\n0 1\n0 -1", "output": "1 2" }, { "input": "2\n2131 -3249\n-2131 3249", "output": "2 1" }, { "input": "3\n-5 1\n-5 -1\n5 0", "output": "1 2" }, { "input": "3\n-100 1\n-100 -1\n0 100", "output": "1 2" }, { "input": "3\n1 10\n10 1\n10 -1", "output": "3 2" }, { "input": "3\n3 0\n0 3\n1 -3", "output": "3 1" }, { "input": "3\n1 1\n-1 0\n1 -1", "output": "3 1" }, { "input": "3\n-1 0\n10 -1\n1 0", "output": "2 3" }, { "input": "4\n1 10\n10 1\n-2 -2\n10 -1", "output": "4 2" }, { "input": "3\n-6 0\n6 1\n6 -1", "output": "3 2" }, { "input": "3\n114 1\n-514 0\n114 -1", "output": "3 1" }, { "input": "4\n-1 0\n0 -1\n-1 1\n1 0", "output": "3 1" }, { "input": "4\n2 1\n2 -1\n-1 1\n-1 -1", "output": "2 1" }, { "input": "3\n3 1\n3 -1\n0 3", "output": "2 1" }, { "input": "3\n1 1\n9000 1\n9000 -1", "output": "3 2" }, { "input": "3\n1 0\n-1 1\n-1 -1", "output": "2 3" }, { "input": "6\n1 1\n-1 -1\n0 20\n100 1\n-100 0\n100 -1", "output": "6 4" }, { "input": "4\n1 0\n0 1\n-1 0\n-13 -1", "output": "3 4" }, { "input": "3\n1 0\n-1 0\n1 -1", "output": "3 1" }, { "input": "3\n100 1\n-100 0\n100 -1", "output": "3 1" }, { "input": "3\n-100 1\n100 0\n-100 -1", "output": "1 3" }, { "input": "3\n1 100\n0 -100\n-1 100", "output": "1 3" }, { "input": "11\n-7945 386\n7504 -576\n-6020 -8277\n930 9737\n1682 474\n-8279 1197\n2790 2607\n-5514 -9601\n-3159 5939\n-1806 4207\n-9073 -2138", "output": "10 9" }, { "input": "3\n1 0\n10000 -1\n1 1", "output": "2 1" }, { "input": "4\n-7125 -1643\n-1235 4071\n-75 -8717\n2553 9278", "output": "4 2" }, { "input": "5\n-6 0\n6 1\n6 -1\n0 6\n0 -6", "output": "3 2" }, { "input": "4\n5 5\n5 -5\n-555 1\n-555 -1", "output": "3 4" }, { "input": "4\n1 1\n-1 1\n-1 -1\n2 -1", "output": "4 1" }, { "input": "4\n-1 -100\n1 -100\n-100 -100\n100 -100", "output": "1 2" }, { "input": "3\n1 0\n1 -1\n-4 -6", "output": "2 1" }, { "input": "4\n-1 -100\n1 -100\n100 -100\n-100 -100", "output": "1 2" }, { "input": "4\n-1 0\n0 -2\n-3 3\n4 0", "output": "3 1" }, { "input": "4\n-2 0\n0 -3\n-5 5\n4 0", "output": "3 1" }, { "input": "3\n1 -100\n0 100\n-1 -100", "output": "3 1" }, { "input": "5\n10000 2\n10000 -1\n10000 -5\n10000 -9\n10000 -13", "output": "2 1" }, { "input": "8\n-9580 8545\n-9379 -1139\n5824 -391\n-8722 2765\n-1357 -5547\n-7700 217\n9323 -7008\n957 -8356", "output": "6 2" }, { "input": "4\n5 5\n5 -5\n-500 1\n-500 -1", "output": "3 4" }, { "input": "3\n30 1\n30 -1\n0 30", "output": "2 1" }, { "input": "4\n3966 -1107\n8007 -5457\n-7753 4945\n-2209 -4221", "output": "2 1" }, { "input": "4\n1 9999\n0 1\n10000 0\n10000 -1", "output": "4 3" }, { "input": "3\n10000 1\n10000 -1\n-10000 0", "output": "2 1" }, { "input": "13\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n13 14\n12 13", "output": "12 13" }, { "input": "4\n2 1\n2 -1\n0 1\n-1 0", "output": "2 1" }, { "input": "4\n10 3\n10 -3\n-500 1\n-500 -1", "output": "3 4" }, { "input": "4\n1 10000\n-1 1\n10000 0\n10000 -1", "output": "4 3" }, { "input": "3\n0 1\n1 0\n1 -1", "output": "3 2" }, { "input": "3\n1 0\n0 1\n1 -1", "output": "3 1" }, { "input": "4\n1 1\n-1 1\n1 -2\n-1 -2", "output": "4 3" }, { "input": "4\n0 -1\n-1 0\n-1 1\n1 0", "output": "3 2" }, { "input": "3\n-100 1\n-100 -1\n1 1", "output": "1 2" }, { "input": "3\n-3 1\n-3 -1\n2 -3", "output": "1 2" }, { "input": "3\n1 -1\n1 0\n0 1", "output": "1 2" }, { "input": "5\n-5 1\n0 5\n4 1\n0 -4\n-5 -1", "output": "1 5" }, { "input": "4\n1 10000\n0 1\n10000 0\n9999 -1", "output": "1 2" }, { "input": "4\n2 3\n2 -3\n-3 2\n-3 -2", "output": "3 4" }, { "input": "3\n1 -3\n1 0\n0 1", "output": "1 2" }, { "input": "3\n1 0\n-1 0\n-1 -1", "output": "2 3" }, { "input": "4\n-2 1\n-2 -1\n1 1\n1 -1", "output": "1 2" }, { "input": "3\n1 -1\n-1 1\n-1 -2", "output": "3 1" }, { "input": "3\n1 0\n-1 -1\n1 -1", "output": "3 1" }, { "input": "3\n5 5\n-5 0\n5 -5", "output": "3 1" }, { "input": "4\n1 -2\n1 0\n-1 0\n10 -1", "output": "4 2" }, { "input": "3\n-1000 1\n-1000 -1\n1000 0", "output": "1 2" }, { "input": "6\n1 1\n1 -1\n-1 1\n-1 -1\n1 -10000\n-1 -10000", "output": "6 5" }, { "input": "3\n1 1\n-1 0\n0 -1", "output": "2 3" }, { "input": "4\n5000 1\n5000 -1\n-2 -1\n2 -1", "output": "2 1" }, { "input": "3\n1 0\n-1 1\n-1 -5", "output": "3 1" }, { "input": "3\n-5374 1323\n-4463 -8462\n6118 -7918", "output": "2 3" }, { "input": "4\n-6427 -6285\n-5386 -5267\n-3898 7239\n-3905 7252", "output": "4 3" }, { "input": "10\n-7 -3\n-2 8\n9 -9\n0 1\n4 5\n5 3\n-3 0\n10 2\n4 -1\n2 -10", "output": "4 2" }, { "input": "4\n9999 1\n9999 -1\n-9998 1\n-10000 -1", "output": "2 1" }, { "input": "4\n10000 9999\n9999 9998\n9998 9997\n9997 9996", "output": "2 1" }, { "input": "4\n-6285 -6427\n-5267 -5386\n7239 -3898\n7252 -3905", "output": "3 4" }, { "input": "4\n-6427 6285\n-5386 5267\n3898 -7239\n3905 -7252", "output": "4 3" }, { "input": "4\n-6427 -6285\n-5386 -5267\n-3898 -7239\n-3905 -7252", "output": "3 4" }, { "input": "3\n0 1\n-1 -1\n1 -1", "output": "2 3" }, { "input": "4\n10000 1\n9998 -1\n-9999 1\n-9999 -1", "output": "3 4" }, { "input": "3\n100 0\n100 2\n100 -1", "output": "3 1" }, { "input": "3\n-1 1\n-1 -1\n1 0", "output": "1 2" }, { "input": "4\n9844 9986\n181 9967\n-9812 -9925\n-194 -9900", "output": "1 2" }, { "input": "4\n9800 9981\n61 9899\n-9926 -9932\n-149 -9926", "output": "3 4" }, { "input": "4\n-9901 9900\n-10000 9899\n9899 9801\n9899 9900", "output": "3 4" }, { "input": "4\n9934 9989\n199 9949\n-9917 -9974\n-197 -9901", "output": "3 4" }, { "input": "3\n-1 1\n1 0\n-1 -1", "output": "1 3" }, { "input": "3\n1 1\n-10 -10\n-10 -9", "output": "3 2" }, { "input": "3\n1 0\n10000 -1\n-1 0", "output": "2 1" }, { "input": "4\n9999 1\n9999 -1\n-10000 1\n-10000 -1", "output": "3 4" }, { "input": "3\n-5 1\n-5 -1\n1 0", "output": "1 2" }, { "input": "3\n1 0\n10000 1\n-1 0", "output": "1 2" }, { "input": "4\n-9990 9995\n9994 -9991\n-9999 -9992\n9993 9992", "output": "2 4" }, { "input": "8\n1 0\n1 1\n0 1\n-1 1\n-1 0\n-1 -1\n0 -1\n1 -2", "output": "7 8" }, { "input": "3\n-9930 9932\n9909 -9909\n-9932 -9931", "output": "3 2" }, { "input": "4\n9876 9977\n127 9938\n-9820 -9934\n-120 -9921", "output": "3 4" }, { "input": "3\n10000 -1\n-1 0\n0 -1", "output": "3 1" }, { "input": "4\n6427 -6285\n5386 -5267\n3898 7239\n3905 7252", "output": "3 4" }, { "input": "4\n9811 9970\n155 9994\n-9826 -9977\n-159 -9986", "output": "1 2" }, { "input": "4\n9851 9917\n74 9921\n-9855 -9916\n-77 -9984", "output": "1 2" }, { "input": "4\n9826 9977\n159 9986\n-9811 -9970\n-155 -9994", "output": "3 4" }, { "input": "4\n9849 9986\n148 9980\n-9800 -9999\n-116 -9927", "output": "3 4" }, { "input": "4\n9822 9967\n111 9905\n-9943 -9986\n-163 -9953", "output": "1 2" }, { "input": "4\n9959 9995\n113 9940\n-9965 -9931\n-148 -9945", "output": "1 2" }, { "input": "4\n9851 9972\n153 9983\n-9866 -9926\n-183 -9946", "output": "1 2" }, { "input": "4\n9816 -9979\n127 -9940\n-9876 9915\n-190 9978", "output": "2 1" }, { "input": "4\n9887 -9917\n138 -9977\n-9826 9995\n-68 9971", "output": "2 1" }, { "input": "4\n9936 -9965\n135 -9949\n-9928 9980\n-123 9908", "output": "2 1" }, { "input": "4\n9981 -9985\n191 -9956\n-9893 9937\n-171 9962", "output": "2 1" }, { "input": "4\n-9811 9970\n-155 9994\n9826 -9977\n159 -9986", "output": "2 1" }, { "input": "4\n9808 9899\n179 9966\n-9870 -9961\n-179 -9950", "output": "3 4" }, { "input": "4\n9815 -9936\n168 -9937\n-9896 9995\n-180 9969", "output": "2 1" }, { "input": "4\n1 1\n1 -1\n-100 1\n-100 -1", "output": "3 4" }, { "input": "4\n9965 114\n87 9916\n-9957 -106\n-95 -9929", "output": "3 4" }, { "input": "4\n9895 -9949\n188 -9978\n-9810 9935\n-151 9914", "output": "2 1" }, { "input": "4\n-9957 106\n-95 9929\n9965 -114\n87 -9916", "output": "2 1" }, { "input": "4\n-9862 9980\n-174 9917\n9845 -9967\n173 -9980", "output": "2 1" }, { "input": "4\n9944 9926\n9927 9935\n-9961 -9929\n-9997 -9991", "output": "3 4" }, { "input": "4\n9917 9909\n196 9925\n-9971 -9991\n-183 -9977", "output": "3 4" } ]
483
0
-1
2,219
250
Movie Critics
[ "greedy" ]
null
null
A film festival is coming up in the city N. The festival will last for exactly *n* days and each day will have a premiere of exactly one film. Each film has a genre β€” an integer from 1 to *k*. On the *i*-th day the festival will show a movie of genre *a**i*. We know that a movie of each of *k* genres occurs in the festival programme at least once. In other words, each integer from 1 to *k* occurs in the sequence *a*1,<=*a*2,<=...,<=*a**n* at least once. Valentine is a movie critic. He wants to watch some movies of the festival and then describe his impressions on his site. As any creative person, Valentine is very susceptive. After he watched the movie of a certain genre, Valentine forms the mood he preserves until he watches the next movie. If the genre of the next movie is the same, it does not change Valentine's mood. If the genres are different, Valentine's mood changes according to the new genre and Valentine has a stress. Valentine can't watch all *n* movies, so he decided to exclude from his to-watch list movies of one of the genres. In other words, Valentine is going to choose exactly one of the *k* genres and will skip all the movies of this genre. He is sure to visit other movies. Valentine wants to choose such genre *x* (1<=≀<=*x*<=≀<=*k*), that the total number of after-movie stresses (after all movies of genre *x* are excluded) were minimum.
The first line of the input contains two integers *n* and *k* (2<=≀<=*k*<=≀<=*n*<=≀<=105), where *n* is the number of movies and *k* is the number of genres. The second line of the input contains a sequence of *n* positive integers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=*k*), where *a**i* is the genre of the *i*-th movie. It is guaranteed that each number from 1 to *k* occurs at least once in this sequence.
Print a single number β€” the number of the genre (from 1 to *k*) of the excluded films. If there are multiple answers, print the genre with the minimum number.
[ "10 3\n1 1 2 3 2 3 3 1 1 3\n", "7 3\n3 1 3 2 3 1 2\n" ]
[ "3", "1" ]
In the first sample if we exclude the movies of the 1st genre, the genres 2, 3, 2, 3, 3, 3 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1, 1, 3, 3, 3, 1, 1, 3 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1, 1, 2, 2, 1, 1 remain, that is 2 stresses. In the second sample whatever genre Valentine excludes, he will have exactly 3 stresses.
[ { "input": "10 3\n1 1 2 3 2 3 3 1 1 3", "output": "3" }, { "input": "7 3\n3 1 3 2 3 1 2", "output": "1" }, { "input": "2 2\n1 2", "output": "1" }, { "input": "10 2\n1 2 2 1 1 2 1 1 2 2", "output": "1" }, { "input": "10 10\n5 7 8 2 4 10 1 3 9 6", "output": "1" }, { "input": "100 10\n6 2 8 1 7 1 2 9 2 6 10 4 2 8 7 5 2 9 5 2 3 2 8 3 7 2 4 3 1 8 8 5 7 10 2 1 8 4 1 4 9 4 2 1 9 3 7 2 4 8 4 3 10 3 9 5 7 7 1 2 10 7 7 8 9 7 1 7 4 8 9 4 1 10 2 4 2 10 9 6 10 5 1 4 2 1 3 1 6 9 10 1 8 9 1 9 1 1 7 6", "output": "1" }, { "input": "74 10\n10 5 4 7 1 9 3 5 10 7 1 4 8 8 4 1 3 9 3 3 10 6 10 4 2 8 9 7 3 2 5 3 6 7 10 4 4 7 8 2 3 10 5 10 5 10 7 9 9 6 1 10 8 9 7 8 9 10 3 6 10 9 9 5 10 6 4 3 5 3 6 8 9 3", "output": "10" }, { "input": "113 3\n1 3 2 2 1 3 1 2 2 2 3 1 1 3 1 3 3 1 2 2 1 3 2 3 3 1 3 1 1 3 3 1 2 3 3 1 3 3 2 3 3 1 1 1 1 2 3 2 2 3 3 2 3 1 3 2 1 3 2 1 1 2 2 2 2 2 1 1 3 3 2 1 1 3 2 2 1 3 1 1 1 3 3 2 1 2 2 3 3 1 3 1 2 2 1 2 2 3 3 2 3 1 3 1 1 2 3 2 3 2 3 1 3", "output": "3" }, { "input": "129 37\n15 22 28 15 30 16 11 20 4 26 14 37 29 17 15 15 14 18 1 9 16 22 13 32 5 18 25 11 6 33 33 22 17 9 1 5 27 27 37 4 6 1 3 18 11 33 6 17 2 24 23 1 1 37 35 21 21 17 18 6 31 17 29 18 10 10 30 31 8 3 25 20 34 11 12 12 24 13 5 27 20 31 28 3 36 2 4 13 14 9 33 25 4 26 36 6 2 7 15 9 20 33 24 33 28 12 23 1 28 12 29 3 35 33 26 15 3 19 1 23 23 32 2 27 11 7 36 34 36", "output": "1" }, { "input": "100 13\n1 1 9 10 6 1 12 13 9 5 3 7 3 5 2 2 10 1 3 8 9 4 4 4 2 10 12 11 1 5 7 13 4 12 5 9 3 13 5 10 7 2 1 7 2 2 4 10 3 10 6 11 13 1 4 3 8 8 9 8 13 4 4 3 7 12 5 5 8 13 1 9 8 12 12 10 4 7 7 12 1 4 3 4 9 6 4 13 10 12 10 9 8 13 13 5 6 9 7 13", "output": "3" }, { "input": "100 12\n9 12 3 3 1 3 12 12 7 9 6 5 8 12 10 7 8 3 4 8 5 9 9 10 9 7 4 5 10 7 4 1 11 6 5 9 1 2 9 9 1 10 6 8 9 10 7 9 10 3 6 4 9 12 11 10 4 4 2 12 11 8 4 9 12 6 4 7 5 1 5 2 7 4 10 2 5 6 4 2 5 8 6 9 6 4 8 6 2 11 4 12 3 1 1 11 1 6 1 10", "output": "9" } ]
248
14,233,600
3
2,230
0
none
[ "none" ]
null
null
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β€” some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β€” it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word *s*. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word *abacabaca* is given. This word can be obtained in the following ways: , where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {*aca*,<=*ba*,<=*ca*}.
The only line contains a string *s* (5<=≀<=|*s*|<=≀<=104) consisting of lowercase English letters.
On the first line print integer *k* β€” a number of distinct possible suffixes. On the next *k* lines print suffixes. Print suffixes in lexicographical (alphabetical) order.
[ "abacabaca\n", "abaca\n" ]
[ "3\naca\nba\nca\n", "0\n" ]
The first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
[ { "input": "abacabaca", "output": "3\naca\nba\nca" }, { "input": "abaca", "output": "0" }, { "input": "gzqgchv", "output": "1\nhv" }, { "input": "iosdwvzerqfi", "output": "9\ner\nerq\nfi\nqfi\nrq\nvz\nvze\nze\nzer" }, { "input": "oawtxikrpvfuzugjweki", "output": "25\neki\nfu\nfuz\ngj\ngjw\nik\nikr\njw\njwe\nki\nkr\nkrp\npv\npvf\nrp\nrpv\nug\nugj\nuz\nuzu\nvf\nvfu\nwe\nzu\nzug" }, { "input": "abcdexyzzzz", "output": "5\nxyz\nyz\nyzz\nzz\nzzz" }, { "input": "affviytdmexpwfqplpyrlniprbdphrcwlboacoqec", "output": "67\nac\naco\nbd\nbdp\nbo\nboa\nco\ncoq\ncw\ncwl\ndm\ndme\ndp\ndph\nec\nex\nexp\nfq\nfqp\nhr\nhrc\nip\nipr\nlb\nlbo\nln\nlni\nlp\nlpy\nme\nmex\nni\nnip\noa\noac\noq\nph\nphr\npl\nplp\npr\nprb\npw\npwf\npy\npyr\nqec\nqp\nqpl\nrb\nrbd\nrc\nrcw\nrl\nrln\ntd\ntdm\nwf\nwfq\nwl\nwlb\nxp\nxpw\nyr\nyrl\nyt\nytd" }, { "input": "lmnxtobrknqjvnzwadpccrlvisxyqbxxmghvl", "output": "59\nad\nadp\nbr\nbrk\nbx\nbxx\ncc\nccr\ncr\ncrl\ndp\ndpc\ngh\nhvl\nis\nisx\njv\njvn\nkn\nknq\nlv\nlvi\nmg\nmgh\nnq\nnqj\nnz\nnzw\nob\nobr\npc\npcc\nqb\nqbx\nqj\nqjv\nrk\nrkn\nrl\nrlv\nsx\nsxy\nvi\nvis\nvl\nvn\nvnz\nwa\nwad\nxm\nxmg\nxx\nxxm\nxy\nxyq\nyq\nyqb\nzw\nzwa" }, { "input": "tbdbdpkluawodlrwldjgplbiylrhuywkhafbkiuoppzsjxwbaqqiwagprqtoauowtaexrhbmctcxwpmplkyjnpwukzwqrqpv", "output": "170\nae\naex\naf\nafb\nag\nagp\naq\naqq\nau\nauo\naw\nawo\nba\nbaq\nbi\nbiy\nbk\nbki\nbm\nbmc\nct\nctc\ncx\ncxw\ndj\ndjg\ndl\ndlr\nex\nexr\nfb\nfbk\ngp\ngpl\ngpr\nha\nhaf\nhb\nhbm\nhu\nhuy\niu\niuo\niw\niwa\niy\niyl\njg\njgp\njn\njnp\njx\njxw\nkh\nkha\nki\nkiu\nkl\nklu\nky\nkyj\nkz\nkzw\nlb\nlbi\nld\nldj\nlk\nlky\nlr\nlrh\nlrw\nlu\nlua\nmc\nmct\nmp\nmpl\nnp\nnpw\noa\noau\nod\nodl\nop\nopp\now\nowt\npk\npkl\npl\nplb\nplk\npm\npmp\npp\nppz\npr\nprq\npv\npw\npwu\npz\npzs\nqi\nqiw\nqpv\nqq\nqqi\nqr\nqrq\nqt\nq..." }, { "input": "caqmjjtwmqxytcsawfufvlofqcqdwnyvywvbbhmpzqwqqxieptiaguwvqdrdftccsglgfezrzhstjcxdknftpyslyqdmkwdolwbusyrgyndqllgesktvgarpfkiglxgtcfepclqhgfbfmkymsszrtynlxbosmrvntsqwccdtahkpnelwiqn", "output": "323\nag\nagu\nah\nahk\nar\narp\naw\nawf\nbb\nbbh\nbf\nbfm\nbh\nbhm\nbo\nbos\nbu\nbus\ncc\nccd\nccs\ncd\ncdt\ncf\ncfe\ncl\nclq\ncq\ncqd\ncs\ncsa\ncsg\ncx\ncxd\ndf\ndft\ndk\ndkn\ndm\ndmk\ndo\ndol\ndq\ndql\ndr\ndrd\ndt\ndta\ndw\ndwn\nel\nelw\nep\nepc\nept\nes\nesk\nez\nezr\nfb\nfbf\nfe\nfep\nfez\nfk\nfki\nfm\nfmk\nfq\nfqc\nft\nftc\nftp\nfu\nfuf\nfv\nfvl\nga\ngar\nge\nges\ngf\ngfb\ngfe\ngl\nglg\nglx\ngt\ngtc\ngu\nguw\ngy\ngyn\nhg\nhgf\nhk\nhkp\nhm\nhmp\nhs\nhst\nia\niag\nie\niep\nig\nigl\niqn\njc\njcx\njt\njtw..." }, { "input": "prntaxhysjfcfmrjngdsitlguahtpnwgbaxptubgpwcfxqehrulbxfcjssgocqncscduvyvarvwxzvmjoatnqfsvsilubexmwugedtzavyamqjqtkxzuslielibjnvkpvyrbndehsqcaqzcrmomqqwskwcypgqoawxdutnxmeivnfpzwvxiyscbfnloqjhjacsfnkfmbhgzpujrqdbaemjsqphokkiplblbflvadcyykcqrdohfasstobwrobslaofbasylwiizrpozvhtwyxtzl", "output": "505\nac\nacs\nad\nadc\nae\naem\nah\naht\nam\namq\nao\naof\naq\naqz\nar\narv\nas\nass\nasy\nat\natn\nav\navy\naw\nawx\nax\naxp\nba\nbae\nbas\nbax\nbe\nbex\nbf\nbfl\nbfn\nbg\nbgp\nbh\nbhg\nbj\nbjn\nbl\nblb\nbn\nbnd\nbs\nbsl\nbw\nbwr\nbx\nbxf\nca\ncaq\ncb\ncbf\ncd\ncdu\ncf\ncfm\ncfx\ncj\ncjs\ncq\ncqn\ncqr\ncr\ncrm\ncs\ncsc\ncsf\ncy\ncyp\ncyy\ndb\ndba\ndc\ndcy\nde\ndeh\ndo\ndoh\nds\ndsi\ndt\ndtz\ndu\ndut\nduv\ned\nedt\neh\nehr\nehs\nei\neiv\nel\neli\nem\nemj\nex\nexm\nfa\nfas\nfb\nfba\nfc\nfcf\nfcj\nfl\nflv\nf..." }, { "input": "gvtgnjyfvnuhagulgmjlqzpvxsygmikofsnvkuplnkxeibnicygpvfvtebppadpdnrxjodxdhxqceaulbfxogwrigstsjudhkgwkhseuwngbppisuzvhzzxxbaggfngmevksbrntpprxvcczlalutdzhwmzbalkqmykmodacjrmwhwugyhwlrbnqxsznldmaxpndwmovcolowxhj", "output": "375\nac\nacj\nad\nadp\nag\nagg\nagu\nal\nalk\nalu\nau\naul\nax\naxp\nba\nbag\nbal\nbf\nbfx\nbn\nbni\nbnq\nbp\nbpp\nbr\nbrn\ncc\nccz\nce\ncea\ncj\ncjr\nco\ncol\ncy\ncyg\ncz\nczl\nda\ndac\ndh\ndhk\ndhx\ndm\ndma\ndn\ndnr\ndp\ndpd\ndw\ndwm\ndx\ndxd\ndz\ndzh\nea\neau\neb\nebp\nei\neib\neu\neuw\nev\nevk\nfn\nfng\nfs\nfsn\nfv\nfvn\nfvt\nfx\nfxo\ngb\ngbp\ngf\ngfn\ngg\nggf\ngm\ngme\ngmi\ngmj\ngp\ngpv\ngs\ngst\ngu\ngul\ngw\ngwk\ngwr\ngy\ngyh\nha\nhag\nhj\nhk\nhkg\nhs\nhse\nhw\nhwl\nhwm\nhwu\nhx\nhxq\nhz\nhzz\nib\nib..." }, { "input": "topqexoicgzjmssuxnswdhpwbsqwfhhziwqibjgeepcvouhjezlomobgireaxaceppoxfxvkwlvgwtjoiplihbpsdhczddwfvcbxqqmqtveaunshmobdlkmmfyajjlkhxnvfmibtbbqswrhcfwytrccgtnlztkddrevkfovunuxtzhhhnorecyfgmlqcwjfjtqegxagfiuqtpjpqlwiefofpatxuqxvikyynncsueynmigieototnbcwxavlbgeqao", "output": "462\nac\nace\nag\nagf\naj\najj\nao\nat\natx\nau\naun\nav\navl\nax\naxa\nbb\nbbq\nbc\nbcw\nbd\nbdl\nbg\nbge\nbgi\nbj\nbjg\nbp\nbps\nbq\nbqs\nbs\nbsq\nbt\nbtb\nbx\nbxq\ncb\ncbx\ncc\nccg\nce\ncep\ncf\ncfw\ncg\ncgt\ncgz\ncs\ncsu\ncv\ncvo\ncw\ncwj\ncwx\ncy\ncyf\ncz\nczd\ndd\nddr\nddw\ndh\ndhc\ndhp\ndl\ndlk\ndr\ndre\ndw\ndwf\nea\neau\neax\nec\necy\nee\neep\nef\nefo\neg\negx\neo\neot\nep\nepc\nepp\neq\nev\nevk\ney\neyn\nez\nezl\nfg\nfgm\nfh\nfhh\nfi\nfiu\nfj\nfjt\nfm\nfmi\nfo\nfof\nfov\nfp\nfpa\nfv\nfvc\nfw\nfwy\n..." }, { "input": "lcrjhbybgamwetyrppxmvvxiyufdkcotwhmptefkqxjhrknjdponulsynpkgszhbkeinpnjdonjfwzbsaweqwlsvuijauwezfydktfljxgclpxpknhygdqyiapvzudyyqomgnsrdhhxhsrdfrwnxdolkmwmw", "output": "276\nam\namw\nap\napv\nau\nauw\naw\nawe\nbg\nbga\nbk\nbke\nbs\nbsa\nby\nbyb\ncl\nclp\nco\ncot\ndf\ndfr\ndh\ndhh\ndk\ndkc\ndkt\ndo\ndol\ndon\ndp\ndpo\ndq\ndqy\ndy\ndyy\nef\nefk\nei\nein\neq\neqw\net\nety\nez\nezf\nfd\nfdk\nfk\nfkq\nfl\nflj\nfr\nfrw\nfw\nfwz\nfy\nfyd\nga\ngam\ngc\ngcl\ngd\ngdq\ngn\ngns\ngs\ngsz\nhb\nhbk\nhh\nhhx\nhm\nhmp\nhr\nhrk\nhs\nhsr\nhx\nhxh\nhy\nhyg\nia\niap\nij\nija\nin\ninp\niy\niyu\nja\njau\njd\njdo\njdp\njf\njfw\njh\njhr\njx\njxg\nkc\nkco\nke\nkei\nkg\nkgs\nkm\nkmw\nkn\nknh\nknj\n..." }, { "input": "hzobjysjhbebobkoror", "output": "20\nbe\nbeb\nbko\nbo\nbob\neb\nebo\nhb\nhbe\njh\njhb\nko\nkor\nob\nor\nror\nsj\nsjh\nys\nysj" }, { "input": "safgmgpzljarfswowdxqhuhypxcmiddyvehjtnlflzknznrukdsbatxoytzxkqngopeipbythhbhfkvlcdxwqrxumbtbgiosjnbeorkzsrfarqofsrcwsfpyheaszjpkjysrcxbzebkxzovdchhososo", "output": "274\nar\narf\narq\nas\nasz\nat\natx\nba\nbat\nbe\nbeo\nbg\nbgi\nbh\nbhf\nbk\nbkx\nbt\nbtb\nby\nbyt\nbz\nbze\ncd\ncdx\nch\nchh\ncm\ncmi\ncw\ncws\ncx\ncxb\ndc\ndch\ndd\nddy\nds\ndsb\ndx\ndxq\ndxw\ndy\ndyv\nea\neas\neb\nebk\neh\nehj\nei\neip\neo\neor\nfa\nfar\nfk\nfkv\nfl\nflz\nfp\nfpy\nfs\nfsr\nfsw\ngi\ngio\ngo\ngop\ngp\ngpz\nhb\nhbh\nhe\nhea\nhf\nhfk\nhh\nhhb\nhj\nhjt\nhos\nhu\nhuh\nhy\nhyp\nid\nidd\nio\nios\nip\nipb\nja\njar\njn\njnb\njp\njpk\njt\njtn\njy\njys\nkd\nkds\nkj\nkjy\nkn\nknz\nkq\nkqn\nkv\nkvl\n..." }, { "input": "glaoyryxrgsysy", "output": "10\ngs\ngsy\nrgs\nry\nryx\nsy\nxr\nysy\nyx\nyxr" }, { "input": "aaaaaxyxxxx", "output": "5\nxx\nxxx\nxyx\nyx\nyxx" }, { "input": "aaaaax", "output": "0" }, { "input": "aaaaaxx", "output": "1\nxx" }, { "input": "aaaaaaa", "output": "1\naa" }, { "input": "aaaaaxxx", "output": "2\nxx\nxxx" }, { "input": "aaaaayxx", "output": "2\nxx\nyxx" }, { "input": "aaaaaxyz", "output": "2\nxyz\nyz" }, { "input": "aaaaaxyxy", "output": "2\nxy\nyxy" }, { "input": "aaaxyyxyy", "output": "3\nxyy\nyx\nyy" }, { "input": "aaaaaxxxxxx", "output": "2\nxx\nxxx" }, { "input": "aaaaaxxxxx", "output": "2\nxx\nxxx" }, { "input": "aaaaaxyzxyxy", "output": "5\nxy\nyxy\nyzx\nzx\nzxy" }, { "input": "aaaaadddgggg", "output": "6\ndd\nddg\ndg\ndgg\ngg\nggg" }, { "input": "abcdeabzzzzzzzz", "output": "5\nab\nabz\nbz\nzz\nzzz" }, { "input": "bbbbbccaaaaaa", "output": "4\naa\naaa\nca\ncca" }, { "input": "xxxxxababc", "output": "5\nab\naba\nabc\nba\nbc" }, { "input": "dddddaabbbbbb", "output": "4\naab\nab\nbb\nbbb" }, { "input": "xxxxxababe", "output": "5\nab\naba\nabe\nba\nbe" }, { "input": "aaaaababaaaaaaaaaaaa", "output": "6\naa\naaa\nab\nba\nbaa\nbab" } ]
93
8,806,400
0
2,233
977
Two-gram
[ "implementation", "strings" ]
null
null
Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" β€” three distinct two-grams. You are given a string $s$ consisting of $n$ capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times. For example, for string $s$ = "BBAABBBA" the answer is two-gram "BB", which contained in $s$ three times. In other words, find any most frequent two-gram. Note that occurrences of the two-gram can overlap with each other.
The first line of the input contains integer number $n$ ($2 \le n \le 100$) β€” the length of string $s$. The second line of the input contains the string $s$ consisting of $n$ capital Latin letters.
Print the only line containing exactly two capital Latin letters β€” any two-gram contained in the given string $s$ as a substring (i.e. two consecutive characters of the string) maximal number of times.
[ "7\nABACABA\n", "5\nZZZAA\n" ]
[ "AB\n", "ZZ\n" ]
In the first example "BA" is also valid answer. In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times.
[ { "input": "7\nABACABA", "output": "AB" }, { "input": "5\nZZZAA", "output": "ZZ" }, { "input": "26\nQWERTYUIOPASDFGHJKLZXCVBNM", "output": "AS" }, { "input": "2\nQA", "output": "QA" }, { "input": "2\nWW", "output": "WW" }, { "input": "11\nGGRRAATTZZZ", "output": "ZZ" }, { "input": "50\nNYQAHBYYOXLTRYQDMVENEMAQNBAKGLGQOLXNAIFNQTOCLNNQIA", "output": "NQ" }, { "input": "100\nURXCAIZFIBNJTPCZHBQIBCILLPXZCFGMKKZMNPLCYGAVJVIBMCZEBSJWPSCPQDYCTTKPOKIJRSKIZPDGCHVOUTMPNECYORSFZFNC", "output": "IB" }, { "input": "100\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "output": "AA" }, { "input": "10\nSQSQSQSQTG", "output": "SQ" }, { "input": "5\nAZAZA", "output": "AZ" }, { "input": "15\nMIRZOYANOVECLOX", "output": "AN" }, { "input": "9\nEGORLETOV", "output": "EG" }, { "input": "8\nPUTINVOR", "output": "IN" }, { "input": "7\nKADUROV", "output": "AD" }, { "input": "6\nAZAZAZ", "output": "AZ" }, { "input": "3\nLOL", "output": "LO" }, { "input": "3\nKEK", "output": "EK" }, { "input": "5\nFUFEL", "output": "EL" }, { "input": "9\nMIKEPIDOR", "output": "DO" }, { "input": "9\nAAAAAAAAA", "output": "AA" }, { "input": "23\nAABBBAAACCCCCAAADDDDDDD", "output": "DD" } ]
62
0
0
2,235
630
Benches
[ "combinatorics", "math" ]
null
null
The city park of IT City contains *n* east to west paths and *n* north to south paths. Each east to west path crosses each north to south path, so there are *n*2 intersections. The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench. Help the park administration count the number of ways to place the benches.
The only line of the input contains one integer *n* (5<=≀<=*n*<=≀<=100) β€” the number of east to west paths and north to south paths.
Output one integer β€” the number of ways to place the benches.
[ "5\n" ]
[ "120" ]
none
[ { "input": "5", "output": "120" }, { "input": "6", "output": "4320" }, { "input": "7", "output": "52920" }, { "input": "15", "output": "1082161080" }, { "input": "17", "output": "4594961280" }, { "input": "72", "output": "23491596420472320" }, { "input": "83", "output": "101159538130177920" }, { "input": "95", "output": "402852449038723320" }, { "input": "99", "output": "613867215317368320" }, { "input": "100", "output": "680185280130048000" } ]
171
13,312,000
0
2,238
629
Far Relative’s Birthday Cake
[ "brute force", "combinatorics", "constructive algorithms", "implementation" ]
null
null
Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird! The cake is a *n*<=Γ—<=*n* square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be? Please, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column.
In the first line of the input, you are given a single integer *n* (1<=≀<=*n*<=≀<=100)Β β€” the length of the side of the cake. Then follow *n* lines, each containing *n* characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'.
Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.
[ "3\n.CC\nC..\nC.C\n", "4\nCC..\nC..C\n.CC.\n.CC.\n" ]
[ "4\n", "9\n" ]
If we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are: 1. (1, 2) and (1, 3) 1. (3, 1) and (3, 3) 1. (2, 1) and (3, 1) 1. (1, 3) and (3, 3)
[ { "input": "3\n.CC\nC..\nC.C", "output": "4" }, { "input": "4\nCC..\nC..C\n.CC.\n.CC.", "output": "9" }, { "input": "5\n.CCCC\nCCCCC\n.CCC.\nCC...\n.CC.C", "output": "46" }, { "input": "7\n.CC..CC\nCC.C..C\nC.C..C.\nC...C.C\nCCC.CCC\n.CC...C\n.C.CCC.", "output": "84" }, { "input": "8\n..C....C\nC.CCC.CC\n.C..C.CC\nCC......\nC..C..CC\nC.C...C.\nC.C..C..\nC...C.C.", "output": "80" }, { "input": "9\n.C...CCCC\nC.CCCC...\n....C..CC\n.CC.CCC..\n.C.C..CC.\nC...C.CCC\nCCC.C...C\nCCCC....C\n..C..C..C", "output": "144" }, { "input": "10\n..C..C.C..\n..CC..C.CC\n.C.C...C.C\n..C.CC..CC\n....C..C.C\n...C..C..C\nCC.CC....C\n..CCCC.C.C\n..CC.CCC..\nCCCC..C.CC", "output": "190" }, { "input": "11\nC.CC...C.CC\nCC.C....C.C\n.....C..CCC\n....C.CC.CC\nC..C..CC...\nC...C...C..\nCC..CCC.C.C\n..C.CC.C..C\nC...C.C..CC\n.C.C..CC..C\n.C.C.CC.C..", "output": "228" }, { "input": "21\n...CCC.....CC..C..C.C\n..CCC...CC...CC.CCC.C\n....C.C.C..CCC..C.C.C\n....CCC..C..C.CC.CCC.\n...CCC.C..C.C.....CCC\n.CCC.....CCC..C...C.C\nCCCC.C...CCC.C...C.CC\nC..C...C.CCC..CC..C..\nC...CC..C.C.CC..C.CC.\nCC..CCCCCCCCC..C....C\n.C..CCCC.CCCC.CCC...C\nCCC...CCC...CCC.C..C.\n.CCCCCCCC.CCCC.CC.C..\n.C.C..C....C.CCCCCC.C\n...C...C.CCC.C.CC..C.\nCCC...CC..CC...C..C.C\n.CCCCC...C.C..C.CC.C.\n..CCC.C.C..CCC.CCC...\n..C..C.C.C.....CC.C..\n.CC.C...C.CCC.C....CC\n...C..CCCC.CCC....C..", "output": "2103" }, { "input": "20\nC.C.CCC.C....C.CCCCC\nC.CC.C..CCC....CCCC.\n.CCC.CC...CC.CCCCCC.\n.C...CCCC..C....CCC.\n.C..CCCCCCC.C.C.....\nC....C.C..CCC.C..CCC\n...C.C.CC..CC..CC...\nC...CC.C.CCCCC....CC\n.CC.C.CCC....C.CCC.C\nCC...CC...CC..CC...C\nC.C..CC.C.CCCC.C.CC.\n..CCCCC.C.CCC..CCCC.\n....C..C..C.CC...C.C\nC..CCC..CC..C.CC..CC\n...CC......C.C..C.C.\nCC.CCCCC.CC.CC...C.C\n.C.CC..CC..CCC.C.CCC\nC..C.CC....C....C...\n..CCC..CCC...CC..C.C\n.C.CCC.CCCCCCCCC..CC", "output": "2071" }, { "input": "17\nCCC..C.C....C.C.C\n.C.CC.CC...CC..C.\n.CCCC.CC.C..CCC.C\n...CCC.CC.CCC.C.C\nCCCCCCCC..C.CC.CC\n...C..C....C.CC.C\nCC....CCC...C.CC.\n.CC.C.CC..C......\n.CCCCC.C.CC.CCCCC\n..CCCC...C..CC..C\nC.CC.C.CC..C.C.C.\nC..C..C..CCC.C...\n.C..CCCC..C......\n.CC.C...C..CC.CC.\nC..C....CC...CC..\nC.CC.CC..C.C..C..\nCCCC...C.C..CCCC.", "output": "1160" }, { "input": "15\nCCCC.C..CCC....\nCCCCCC.CC.....C\n...C.CC.C.C.CC.\nCCCCCCC..C..C..\nC..CCC..C.CCCC.\n.CC..C.C.C.CC.C\n.C.C..C..C.C..C\n...C...C..CCCC.\n.....C.C..CC...\nCC.C.C..CC.C..C\n..CCCCC..CCC...\nCC.CC.C..CC.CCC\n..CCC...CC.C..C\nCC..C.C..CCC..C\n.C.C....CCC...C", "output": "789" }, { "input": "1\n.", "output": "0" }, { "input": "3\n.CC\nC..\nC.C", "output": "4" }, { "input": "13\nC.C...C.C.C..\nCC.CCCC.CC..C\n.C.CCCCC.CC..\nCCCC..C...C..\n...CC.C.C...C\n.CC.CCC...CC.\nCC.CCCCCC....\n.C...C..CC..C\nCCCC.CC...C..\n.C.CCC..C.CC.\n..C...CC..C.C\n..C.CCC..CC.C\n.C...CCC.CC.C", "output": "529" }, { "input": "16\n.C.C.C.C.C...C.C\n..C..C.CCCCCC...\n..C.C.C.C..C..C.\n.CC....C.CCC..C.\n.C.CCC..C....CCC\nCC..C.CC..C.C.CC\n...C..C..CC..CC.\n.CCC..C.CC.C.C..\n.CC.C..........C\nC...C....CC..C..\nC.CCC.C..C..C...\n.CCCCCCCCCCCC..C\n..C.C.CC.CC.CCC.\nCC..C.C....C..CC\nC.CCC..C..C.C.CC\n.C.CCC.CC..CCC.C", "output": "874" }, { "input": "2\nCC\nCC", "output": "4" }, { "input": "3\nC..\nC..\nC..", "output": "3" } ]
62
0
3
2,244
757
Bash's Big Day
[ "greedy", "math", "number theory" ]
null
null
Bash has set out on a journey to become the greatest Pokemon master. To get his first Pokemon, he went to Professor Zulu's Lab. Since Bash is Professor Zulu's favourite student, Zulu allows him to take as many Pokemon from his lab as he pleases. But Zulu warns him that a group of *k*<=&gt;<=1 Pokemon with strengths {*s*1,<=*s*2,<=*s*3,<=...,<=*s**k*} tend to fight among each other if *gcd*(*s*1,<=*s*2,<=*s*3,<=...,<=*s**k*)<==<=1 (see notes for *gcd* definition). Bash, being smart, does not want his Pokemon to fight among each other. However, he also wants to maximize the number of Pokemon he takes from the lab. Can you help Bash find out the maximum number of Pokemon he can take? Note: A Pokemon cannot fight with itself.
The input consists of two lines. The first line contains an integer *n* (1<=≀<=*n*<=≀<=105), the number of Pokemon in the lab. The next line contains *n* space separated integers, where the *i*-th of them denotes *s**i* (1<=≀<=*s**i*<=≀<=105), the strength of the *i*-th Pokemon.
Print single integerΒ β€” the maximum number of Pokemons Bash can take.
[ "3\n2 3 4\n", "5\n2 3 4 6 7\n" ]
[ "2\n", "3\n" ]
*gcd* (greatest common divisor) of positive integers set {*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*n*</sub>} is the maximum positive integer that divides all the integers {*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*n*</sub>}. In the first sample, we can take Pokemons with strengths {2, 4} since *gcd*(2, 4) = 2. In the second sample, we can take Pokemons with strengths {2, 4, 6}, and there is no larger group with *gcd* ≠ 1.
[ { "input": "3\n2 3 4", "output": "2" }, { "input": "5\n2 3 4 6 7", "output": "3" }, { "input": "3\n5 6 4", "output": "2" }, { "input": "8\n41 74 4 27 85 39 100 36", "output": "4" }, { "input": "6\n89 20 86 81 62 23", "output": "3" }, { "input": "71\n23 84 98 8 14 4 42 56 83 87 28 22 32 50 5 96 90 1 59 74 77 88 71 38 62 36 85 97 99 6 81 20 49 57 66 9 45 41 29 68 35 19 27 76 78 72 55 25 46 48 26 53 39 31 94 34 63 37 64 16 79 24 82 17 12 3 89 61 80 30 10", "output": "38" }, { "input": "95\n72 38 75 62 87 30 11 65 35 16 73 23 18 48 19 4 22 42 14 60 49 83 59 15 51 27 80 97 37 100 64 81 54 71 52 20 5 98 78 86 26 55 25 57 36 3 8 74 82 21 29 1 76 2 79 61 39 9 89 77 70 63 56 28 92 53 31 45 93 47 67 99 58 12 84 44 32 34 69 40 13 7 66 68 17 85 6 90 33 91 94 24 46 10 50", "output": "48" }, { "input": "44\n39706 21317 26213 55086 10799 31825 29024 6565 96535 11412 14642 91901 41932 24538 81351 53861 63403 34199 82286 32594 29684 42753 16857 73821 71085 36306 70080 11233 21023 8551 85406 95390 92375 52675 77938 46265 74855 5229 5856 66713 65730 24525 84078 20684", "output": "19" }, { "input": "35\n45633 86983 46174 48399 33926 51395 76300 6387 48852 82808 28694 79864 4482 35982 21956 76522 19656 74518 28480 71481 25700 46815 14170 95705 8535 96993 29029 8898 97637 62710 14615 22864 69849 27068 68557", "output": "20" }, { "input": "1\n1", "output": "1" }, { "input": "10\n10 7 9 8 3 3 10 7 3 3", "output": "5" }, { "input": "9\n10 10 6 10 9 1 8 3 5", "output": "5" }, { "input": "7\n9 4 2 3 3 9 8", "output": "4" }, { "input": "1\n4", "output": "1" }, { "input": "6\n1623 45906 37856 34727 27156 12598", "output": "4" }, { "input": "30\n83172 59163 67334 83980 5932 8773 77649 41428 62789 28159 17183 10199 41496 59500 14614 10468 54886 64679 42382 57021 50499 95643 77239 61434 16181 30505 59152 55972 18265 70566", "output": "15" }, { "input": "23\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 22 16 2 13 16", "output": "22" }, { "input": "46\n12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 12553 15 1 18 28 20 6 31 16 5 23 21 38 3 11 18 11 3 25 33", "output": "27" }, { "input": "43\n8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8831 8 23 40 33 11 5 21 16 19 15 41 30 28 31 5 32 16 5 38 11 21 34", "output": "21" }, { "input": "25\n58427 26687 48857 46477 7039 25423 58757 48119 38113 40637 22391 48337 4157 10597 8167 19031 64613 70913 69313 18047 17159 77491 13499 70949 24107", "output": "1" }, { "input": "10\n1 1 1 1 1 1 1 1 1 1", "output": "1" }, { "input": "2\n3 6", "output": "2" }, { "input": "5\n1 1 1 1 1", "output": "1" }, { "input": "5\n3 3 3 3 3", "output": "5" }, { "input": "3\n1 1 1", "output": "1" }, { "input": "2\n541 541", "output": "2" }, { "input": "2\n1 1", "output": "1" }, { "input": "2\n99989 99989", "output": "2" }, { "input": "3\n3 9 27", "output": "3" }, { "input": "2\n1009 1009", "output": "2" }, { "input": "4\n1 1 1 1", "output": "1" }, { "input": "6\n2 10 20 5 15 25", "output": "5" }, { "input": "3\n3 3 6", "output": "3" }, { "input": "3\n457 457 457", "output": "3" }, { "input": "2\n34 17", "output": "2" }, { "input": "3\n12 24 3", "output": "3" }, { "input": "10\n99991 99991 99991 99991 99991 99991 99991 99991 99991 99991", "output": "10" }, { "input": "2\n1009 2018", "output": "2" }, { "input": "3\n3 3 3", "output": "3" }, { "input": "7\n6 9 12 15 21 27 33", "output": "7" }, { "input": "3\n2 1 1", "output": "1" }, { "input": "2\n557 557", "output": "2" }, { "input": "3\n1 1 2", "output": "1" }, { "input": "5\n2 2 101 101 101", "output": "3" }, { "input": "2\n122 3721", "output": "2" }, { "input": "2\n49201 98402", "output": "2" }, { "input": "2\n88258 44129", "output": "2" }, { "input": "2\n7919 47514", "output": "2" }, { "input": "5\n1 2 1 1 1", "output": "1" }, { "input": "2\n2 2", "output": "2" }, { "input": "5\n1 1 1 1 2", "output": "1" }, { "input": "3\n10007 20014 30021", "output": "3" }, { "input": "4\n10 10 5 5", "output": "4" }, { "input": "2\n6 9", "output": "2" }, { "input": "3\n6 12 9", "output": "3" }, { "input": "2\n14 7", "output": "2" }, { "input": "2\n199 199", "output": "2" }, { "input": "3\n49999 49999 99998", "output": "3" }, { "input": "5\n1009 1009 1009 1009 1009", "output": "5" }, { "input": "3\n17 17 17", "output": "3" }, { "input": "2\n503 1509", "output": "2" }, { "input": "3\n49999 99998 99998", "output": "3" }, { "input": "2\n28657 28657", "output": "2" }, { "input": "3\n121 22 33", "output": "3" }, { "input": "2\n10079 20158", "output": "2" }, { "input": "2\n6 15", "output": "2" }, { "input": "4\n6 6 6 3", "output": "4" }, { "input": "2\n991 1982", "output": "2" }, { "input": "3\n20362 30543 50905", "output": "3" }, { "input": "2\n443 886", "output": "2" }, { "input": "4\n3 7 7 21", "output": "3" }, { "input": "3\n3 6 9", "output": "3" }, { "input": "2\n217 31", "output": "2" }, { "input": "6\n10 10 10 5 5 5", "output": "6" }, { "input": "4\n49441 25225 9081 4036", "output": "4" }, { "input": "2\n4 9", "output": "1" }, { "input": "2\n18 27", "output": "2" }, { "input": "2\n13 26", "output": "2" }, { "input": "4\n6 6 6 15", "output": "4" } ]
30
0
0
2,247
925
Aztec Catacombs
[ "constructive algorithms" ]
null
null
Indiana Jones found ancient Aztec catacombs containing a golden idol. The catacombs consists of $n$ caves. Each pair of caves is connected with a two-way corridor that can be opened or closed. The entrance to the catacombs is in the cave $1$, the idol and the exit are in the cave $n$. When Indiana goes from a cave $x$ to a cave $y$ using an open corridor, all corridors connected to the cave $x$ change their state: all open corridors become closed, all closed corridors become open. Indiana wants to go from cave $1$ to cave $n$ going through as small number of corridors as possible. Help him find the optimal path, or determine that it is impossible to get out of catacombs.
The first line contains two integers $n$ and $m$ ($2 \leq n \leq 3\cdot 10^5$, $0 \leq m \leq 3 \cdot 10^5$)Β β€” the number of caves and the number of open corridors at the initial moment. The next $m$ lines describe the open corridors. The $i$-th of these lines contains two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$, $u_i \neq v_i$)Β β€” the caves connected by the $i$-th open corridor. It is guaranteed that each unordered pair of caves is presented at most once.
If there is a path to exit, in the first line print a single integer $k$Β β€” the minimum number of corridors Indians should pass through ($1 \leq k \leq 10^6$). In the second line print $k+1$ integers $x_0, \ldots, x_k$Β β€” the number of caves in the order Indiana should visit them. The sequence $x_0, \ldots, x_k$ should satisfy the following: - $x_0 = 1$, $x_k = n$;- for each $i$ from $1$ to $k$ the corridor from $x_{i - 1}$ to $x_i$ should be open at the moment Indiana walks along this corridor. If there is no path, print a single integer $-1$. We can show that if there is a path, there is a path consisting of no more than $10^6$ corridors.
[ "4 4\n1 2\n2 3\n1 3\n3 4\n", "4 2\n1 2\n2 3\n" ]
[ "2\n1 3 4 \n", "4\n1 2 3 1 4 \n" ]
none
[]
30
0
0
2,256
859
Lazy Security Guard
[ "brute force", "geometry", "math" ]
null
null
Your security guard friend recently got a new job at a new security company. The company requires him to patrol an area of the city encompassing exactly *N* city blocks, but they let him choose which blocks. That is, your friend must walk the perimeter of a region whose area is exactly *N* blocks. Your friend is quite lazy and would like your help to find the shortest possible route that meets the requirements. The city is laid out in a square grid pattern, and is large enough that for the sake of the problem it can be considered infinite.
Input will consist of a single integer *N* (1<=≀<=*N*<=≀<=106), the number of city blocks that must be enclosed by the route.
Print the minimum perimeter that can be achieved.
[ "4\n", "11\n", "22\n" ]
[ "8\n", "14\n", "20\n" ]
Here are some possible shapes for the examples: <img class="tex-graphics" src="https://espresso.codeforces.com/e11bef2cf82b55dd583cfc97d12b5aee5e483a65.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "4", "output": "8" }, { "input": "11", "output": "14" }, { "input": "22", "output": "20" }, { "input": "3", "output": "8" }, { "input": "1024", "output": "128" }, { "input": "101", "output": "42" }, { "input": "30", "output": "22" }, { "input": "1000000", "output": "4000" }, { "input": "1", "output": "4" }, { "input": "2", "output": "6" }, { "input": "5", "output": "10" }, { "input": "6", "output": "10" }, { "input": "7", "output": "12" }, { "input": "8", "output": "12" }, { "input": "9", "output": "12" }, { "input": "10", "output": "14" }, { "input": "999000", "output": "3998" }, { "input": "999001", "output": "4000" }, { "input": "999999", "output": "4000" }, { "input": "933206", "output": "3866" }, { "input": "718351", "output": "3392" }, { "input": "607443", "output": "3118" }, { "input": "347887", "output": "2360" }, { "input": "246206", "output": "1986" }, { "input": "151375", "output": "1558" }, { "input": "12639", "output": "450" }, { "input": "3751", "output": "246" }, { "input": "3607", "output": "242" }, { "input": "124", "output": "46" }, { "input": "64", "output": "32" }, { "input": "31", "output": "24" }, { "input": "23", "output": "20" }, { "input": "15", "output": "16" }, { "input": "19", "output": "18" }, { "input": "59637", "output": "978" } ]
15
0
0
2,257
194
Square
[ "math" ]
null
null
There is a square painted on a piece of paper, the square's side equals *n* meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (*n*<=+<=1) meters, he draws a cross (see picture for clarifications). John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
The first line contains integer *t* (1<=≀<=*t*<=≀<=104) β€” the number of test cases. The second line contains *t* space-separated integers *n**i* (1<=≀<=*n**i*<=≀<=109) β€” the sides of the square for each test sample.
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input. 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\n4 8 100\n" ]
[ "17\n33\n401\n" ]
none
[ { "input": "3\n4 8 100", "output": "17\n33\n401" }, { "input": "8\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 13", "output": "4000000001\n4000000001\n4000000001\n4000000001\n4000000001\n4000000001\n4000000001\n27" }, { "input": "3\n13 17 21", "output": "27\n35\n43" } ]
92
0
0
2,260
219
Choosing Capital for Treeland
[ "dfs and similar", "dp", "graphs", "trees" ]
null
null
The country Treeland consists of *n* cities, some pairs of them are connected with unidirectional roads. Overall there are *n*<=-<=1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one. The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city *a* is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city *a* to any other city. For that some roads may have to be inversed. Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.
The first input line contains integer *n* (2<=≀<=*n*<=≀<=2Β·105) β€” the number of cities in Treeland. Next *n*<=-<=1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers *s**i*,<=*t**i* (1<=≀<=*s**i*,<=*t**i*<=≀<=*n*;Β *s**i*<=β‰ <=*t**i*) β€” the numbers of cities, connected by that road. The *i*-th road is oriented from city *s**i* to city *t**i*. You can consider cities in Treeland indexed from 1 to *n*.
In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital β€” a sequence of indexes of cities in the increasing order.
[ "3\n2 1\n2 3\n", "4\n1 4\n2 4\n3 4\n" ]
[ "0\n2 \n", "2\n1 2 3 \n" ]
none
[ { "input": "3\n2 1\n2 3", "output": "0\n2 " }, { "input": "4\n1 4\n2 4\n3 4", "output": "2\n1 2 3 " }, { "input": "2\n1 2", "output": "0\n1 " }, { "input": "8\n1 2\n3 2\n4 3\n4 5\n6 5\n6 7\n8 7", "output": "3\n4 6 8 " }, { "input": "10\n2 3\n1 8\n9 5\n5 4\n6 10\n4 8\n5 6\n7 6\n5 3", "output": "3\n9 " }, { "input": "10\n9 3\n3 8\n4 3\n3 5\n7 8\n10 3\n2 3\n6 2\n3 1", "output": "4\n6 " }, { "input": "10\n5 4\n6 5\n10 1\n2 5\n9 8\n1 3\n7 10\n2 3\n5 9", "output": "2\n7 " }, { "input": "10\n2 3\n1 8\n9 5\n5 4\n6 10\n4 8\n5 6\n7 6\n5 3", "output": "3\n9 " }, { "input": "11\n3 10\n4 7\n11 7\n8 2\n6 9\n9 3\n5 9\n1 3\n3 8\n7 3", "output": "5\n4 5 6 11 " }, { "input": "12\n10 7\n6 10\n8 10\n4 12\n10 4\n3 12\n1 2\n2 7\n4 5\n11 5\n11 9", "output": "5\n1 6 8 " }, { "input": "13\n7 13\n5 4\n5 1\n11 12\n8 3\n13 4\n10 7\n11 5\n8 13\n2 8\n10 6\n4 9", "output": "4\n2 10 " }, { "input": "14\n13 10\n5 14\n9 4\n8 12\n10 1\n5 2\n3 6\n11 2\n13 2\n13 7\n8 2\n4 5\n1 3", "output": "3\n9 " }, { "input": "15\n15 6\n2 15\n14 15\n15 12\n1 2\n10 15\n7 4\n13 7\n11 2\n5 2\n7 2\n3 2\n7 8\n9 15", "output": "7\n13 " } ]
60
0
0
2,261
0
none
[ "none" ]
null
null
Π’Ρ€ΠΈ Π±Ρ€Π°Ρ‚Π° Π΄ΠΎΠ³ΠΎΠ²ΠΎΡ€ΠΈΠ»ΠΈΡΡŒ ΠΎ встрСчС. ΠŸΡ€ΠΎΠ½ΡƒΠΌΠ΅Ρ€ΡƒΠ΅ΠΌ Π±Ρ€Π°Ρ‚ΡŒΠ΅Π² ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ: ΠΏΡƒΡΡ‚ΡŒ ΡΡ‚Π°Ρ€ΡˆΠΈΠΉ Π±Ρ€Π°Ρ‚ ΠΈΠΌΠ΅Π΅Ρ‚ Π½ΠΎΠΌΠ΅Ρ€ 1, срСдний Π±Ρ€Π°Ρ‚ ΠΈΠΌΠ΅Π΅Ρ‚ Π½ΠΎΠΌΠ΅Ρ€ 2, Π° младший Π±Ρ€Π°Ρ‚Β β€” Π½ΠΎΠΌΠ΅Ρ€ 3. Когда ΠΏΡ€ΠΈΡˆΠ»ΠΎ врСмя встрСчи, ΠΎΠ΄ΠΈΠ½ ΠΈΠ· Π±Ρ€Π°Ρ‚ΡŒΠ΅Π² ΠΎΠΏΠΎΠ·Π΄Π°Π». По Π·Π°Π΄Π°Π½Π½Ρ‹ΠΌ Π½ΠΎΠΌΠ΅Ρ€Π°ΠΌ Π΄Π²ΡƒΡ… Π±Ρ€Π°Ρ‚ΡŒΠ΅Π², ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΏΡ€ΠΈΡˆΠ»ΠΈ воврСмя, Π²Π°ΠΌ прСдстоит ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ Π½ΠΎΠΌΠ΅Ρ€ опоздавшСго Π±Ρ€Π°Ρ‚Π°.
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… ΡΠ»Π΅Π΄ΡƒΡŽΡ‚ Π΄Π²Π° Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹Ρ… Ρ†Π΅Π»Ρ‹Ρ… числа *a* ΠΈ *b* (1<=≀<=*a*,<=*b*<=≀<=3, *a*<=β‰ <=*b*)Β β€” Π½ΠΎΠΌΠ΅Ρ€Π° Π±Ρ€Π°Ρ‚ΡŒΠ΅Π², ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΏΡ€ΠΈΡˆΠ»ΠΈ Π½Π° встрСчу воврСмя. НомСра Π΄Π°Π½Ρ‹ Π² ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½ΠΎΠΌ порядкС.
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ СдинствСнноС Ρ†Π΅Π»ΠΎΠ΅ число — Π½ΠΎΠΌΠ΅Ρ€ Π±Ρ€Π°Ρ‚Π°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΎΠΏΠΎΠ·Π΄Π°Π» Π½Π° встрСчу.
[ "3 1\n" ]
[ "2\n" ]
none
[ { "input": "3 1", "output": "2" }, { "input": "2 1", "output": "3" }, { "input": "2 3", "output": "1" }, { "input": "1 2", "output": "3" }, { "input": "1 3", "output": "2" }, { "input": "3 2", "output": "1" } ]
62
4,812,800
3
2,267
501
Contest
[ "implementation" ]
null
null
Misha and Vasya participated in a Codeforces contest. Unfortunately, each of them solved only one problem, though successfully submitted it at the first attempt. Misha solved the problem that costs *a* points and Vasya solved the problem that costs *b* points. Besides, Misha submitted the problem *c* minutes after the contest started and Vasya submitted the problem *d* minutes after the contest started. As you know, on Codeforces the cost of a problem reduces as a round continues. That is, if you submit a problem that costs *p* points *t* minutes after the contest started, you get points. Misha and Vasya are having an argument trying to find out who got more points. Help them to find out the truth.
The first line contains four integers *a*, *b*, *c*, *d* (250<=≀<=*a*,<=*b*<=≀<=3500, 0<=≀<=*c*,<=*d*<=≀<=180). It is guaranteed that numbers *a* and *b* are divisible by 250 (just like on any real Codeforces round).
Output on a single line: "Misha" (without the quotes), if Misha got more points than Vasya. "Vasya" (without the quotes), if Vasya got more points than Misha. "Tie" (without the quotes), if both of them got the same number of points.
[ "500 1000 20 30\n", "1000 1000 1 1\n", "1500 1000 176 177\n" ]
[ "Vasya\n", "Tie\n", "Misha\n" ]
none
[ { "input": "500 1000 20 30", "output": "Vasya" }, { "input": "1000 1000 1 1", "output": "Tie" }, { "input": "1500 1000 176 177", "output": "Misha" }, { "input": "1500 1000 74 177", "output": "Misha" }, { "input": "750 2500 175 178", "output": "Vasya" }, { "input": "750 1000 54 103", "output": "Tie" }, { "input": "2000 1250 176 130", "output": "Tie" }, { "input": "1250 1750 145 179", "output": "Tie" }, { "input": "2000 2000 176 179", "output": "Tie" }, { "input": "1500 1500 148 148", "output": "Tie" }, { "input": "2750 1750 134 147", "output": "Misha" }, { "input": "3250 250 175 173", "output": "Misha" }, { "input": "500 500 170 176", "output": "Misha" }, { "input": "250 1000 179 178", "output": "Vasya" }, { "input": "3250 1000 160 138", "output": "Misha" }, { "input": "3000 2000 162 118", "output": "Tie" }, { "input": "1500 1250 180 160", "output": "Tie" }, { "input": "1250 2500 100 176", "output": "Tie" }, { "input": "3500 3500 177 178", "output": "Tie" }, { "input": "3000 3250 16 34", "output": "Tie" }, { "input": "1750 3000 137 49", "output": "Vasya" }, { "input": "500 1500 179 71", "output": "Vasya" }, { "input": "1250 2000 101 180", "output": "Misha" }, { "input": "250 750 180 176", "output": "Vasya" }, { "input": "2250 2250 163 145", "output": "Vasya" }, { "input": "3000 3000 176 78", "output": "Vasya" }, { "input": "250 3500 8 178", "output": "Vasya" }, { "input": "1750 1250 179 180", "output": "Misha" }, { "input": "2750 1750 13 164", "output": "Misha" }, { "input": "1750 2250 178 53", "output": "Vasya" }, { "input": "2500 2750 73 179", "output": "Misha" }, { "input": "1000 3500 178 175", "output": "Vasya" }, { "input": "1000 500 7 162", "output": "Misha" }, { "input": "1000 250 175 48", "output": "Misha" }, { "input": "1750 500 166 177", "output": "Misha" }, { "input": "250 250 0 0", "output": "Tie" }, { "input": "250 3500 0 0", "output": "Vasya" }, { "input": "250 3500 0 180", "output": "Vasya" }, { "input": "3500 3500 180 180", "output": "Tie" }, { "input": "3500 250 0 180", "output": "Misha" } ]
62
5,632,000
3
2,268
0
none
[ "none" ]
null
null
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of *n* nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly *k* of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability. Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes. Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.
The first line contains two integers *n* and *k* (3<=≀<=*n*<=≀<=2Β·105, 2<=≀<=*k*<=≀<=*n*<=-<=1)Β β€” the total number of nodes and the number of exit-nodes. Note that it is always possible to build at least one network with *n* nodes and *k* exit-nodes within the given constraints.
In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next *n*<=-<=1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to *n*. Exit-nodes can have any ids. If there are multiple answers, print any of them.
[ "3 2\n", "5 3\n" ]
[ "2\n1 2\n2 3\n", "3\n1 2\n2 3\n3 4\n3 5\n" ]
In the first example the only network is shown on the left picture. In the second example one of optimal networks is shown on the right picture. Exit-nodes are highlighted.
[ { "input": "3 2", "output": "2\n1 2\n2 3" }, { "input": "5 3", "output": "3\n1 2\n2 3\n3 4\n3 5" }, { "input": "4 2", "output": "3\n1 2\n2 3\n3 4" }, { "input": "4 3", "output": "2\n1 2\n2 3\n2 4" }, { "input": "5 2", "output": "4\n1 2\n2 3\n3 4\n4 5" }, { "input": "5 4", "output": "2\n1 2\n2 3\n2 4\n2 5" }, { "input": "6 2", "output": "5\n1 2\n2 3\n3 4\n4 5\n5 6" }, { "input": "6 3", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6" }, { "input": "6 4", "output": "3\n1 2\n2 3\n3 4\n3 5\n3 6" }, { "input": "6 5", "output": "2\n1 2\n2 3\n2 4\n2 5\n2 6" }, { "input": "1000 245", "output": "10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n6 12\n12 13\n13 14\n14 15\n15 16\n6 17\n17 18\n18 19\n19 20\n20 21\n6 22\n22 23\n23 24\n24 25\n25 26\n6 27\n27 28\n28 29\n29 30\n30 31\n6 32\n32 33\n33 34\n34 35\n35 36\n6 37\n37 38\n38 39\n39 40\n40 41\n6 42\n42 43\n43 44\n44 45\n45 46\n6 47\n47 48\n48 49\n49 50\n50 51\n6 52\n52 53\n53 54\n54 55\n55 56\n6 57\n57 58\n58 59\n59 60\n60 61\n6 62\n62 63\n63 64\n64 65\n65 66\n6 67\n67 68\n68 69\n69 70\n70 71\n6 72\n72 73\n73 74\n74 75\n75 76\n6 77\n77 78\n..." }, { "input": "1000 999", "output": "2\n1 2\n2 3\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\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 85\n2 86\n2 87\n..." }, { "input": "1024 23", "output": "90\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n..." }, { "input": "200000 1014", "output": "396\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "100003 16", "output": "12502\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 ..." }, { "input": "7 2", "output": "6\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7" }, { "input": "7 3", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7" }, { "input": "7 4", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n3 7" }, { "input": "7 5", "output": "3\n1 2\n2 3\n3 4\n3 5\n3 6\n3 7" }, { "input": "7 6", "output": "2\n1 2\n2 3\n2 4\n2 5\n2 6\n2 7" }, { "input": "100 2", "output": "99\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n..." }, { "input": "100 5", "output": "40\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n21 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n21 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n..." }, { "input": "100 59", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "100 98", "output": "3\n1 2\n2 3\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\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 54\n3 55\n3 56\n3 57\n3 58\n3 59\n3 60\n3 61\n3 62\n3 63\n3 64\n3 65\n3 66\n3 67\n3 68\n3 69\n3 70\n3 71\n3 72\n3 73\n3 74\n3 75\n3 76\n3 77\n3 78\n3 79\n3 80\n3 81\n3 82\n3 83\n3 84\n3 85\n3 86\n3 87\n..." }, { "input": "100 99", "output": "2\n1 2\n2 3\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\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 85\n2 86\n2 87\n..." }, { "input": "1000 2", "output": "999\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "1000 5", "output": "400\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "1000 670", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "1000 998", "output": "3\n1 2\n2 3\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\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 54\n3 55\n3 56\n3 57\n3 58\n3 59\n3 60\n3 61\n3 62\n3 63\n3 64\n3 65\n3 66\n3 67\n3 68\n3 69\n3 70\n3 71\n3 72\n3 73\n3 74\n3 75\n3 76\n3 77\n3 78\n3 79\n3 80\n3 81\n3 82\n3 83\n3 84\n3 85\n3 86\n3 87\n..." }, { "input": "100000 2", "output": "99999\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 ..." }, { "input": "100000 4", "output": "50000\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 ..." }, { "input": "100000 101", "output": "1982\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 7..." }, { "input": "100000 30005", "output": "8\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n5 10\n10 11\n11 12\n12 13\n5 14\n14 15\n15 16\n16 17\n5 18\n18 19\n19 20\n20 21\n5 22\n22 23\n23 24\n24 25\n5 26\n26 27\n27 28\n28 29\n5 30\n30 31\n31 32\n32 33\n5 34\n34 35\n35 36\n36 37\n5 38\n38 39\n39 40\n40 41\n5 42\n42 43\n43 44\n44 45\n5 46\n46 47\n47 48\n48 49\n5 50\n50 51\n51 52\n52 53\n5 54\n54 55\n55 56\n56 57\n5 58\n58 59\n59 60\n60 61\n5 62\n62 63\n63 64\n64 65\n5 66\n66 67\n67 68\n68 69\n5 70\n70 71\n71 72\n72 73\n5 74\n74 75\n75 76\n76 77\n5 78\n78 ..." }, { "input": "100000 76541", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "100000 99998", "output": "3\n1 2\n2 3\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\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 54\n3 55\n3 56\n3 57\n3 58\n3 59\n3 60\n3 61\n3 62\n3 63\n3 64\n3 65\n3 66\n3 67\n3 68\n3 69\n3 70\n3 71\n3 72\n3 73\n3 74\n3 75\n3 76\n3 77\n3 78\n3 79\n3 80\n3 81\n3 82\n3 83\n3 84\n3 85\n3 86\n3 87\n..." }, { "input": "100000 99999", "output": "2\n1 2\n2 3\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\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 85\n2 86\n2 87\n..." }, { "input": "200000 2", "output": "199999\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75..." }, { "input": "200000 5", "output": "80000\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 ..." }, { "input": "200000 211", "output": "1896\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 7..." }, { "input": "200000 100002", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "200000 145321", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "200000 199998", "output": "3\n1 2\n2 3\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\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 54\n3 55\n3 56\n3 57\n3 58\n3 59\n3 60\n3 61\n3 62\n3 63\n3 64\n3 65\n3 66\n3 67\n3 68\n3 69\n3 70\n3 71\n3 72\n3 73\n3 74\n3 75\n3 76\n3 77\n3 78\n3 79\n3 80\n3 81\n3 82\n3 83\n3 84\n3 85\n3 86\n3 87\n..." }, { "input": "200000 199999", "output": "2\n1 2\n2 3\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\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 85\n2 86\n2 87\n..." }, { "input": "1024 2", "output": "1023\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 7..." }, { "input": "1024 16", "output": "128\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "1024 512", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "1024 511", "output": "5\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n7 8\n4 9\n9 10\n4 11\n11 12\n4 13\n13 14\n4 15\n15 16\n4 17\n17 18\n4 19\n19 20\n4 21\n21 22\n4 23\n23 24\n4 25\n25 26\n4 27\n27 28\n4 29\n29 30\n4 31\n31 32\n4 33\n33 34\n4 35\n35 36\n4 37\n37 38\n4 39\n39 40\n4 41\n41 42\n4 43\n43 44\n4 45\n45 46\n4 47\n47 48\n4 49\n49 50\n4 51\n51 52\n4 53\n53 54\n4 55\n55 56\n4 57\n57 58\n4 59\n59 60\n4 61\n61 62\n4 63\n63 64\n4 65\n65 66\n4 67\n67 68\n4 69\n69 70\n4 71\n71 72\n4 73\n73 74\n4 75\n75 76\n4 77\n77 78\n4 79\n79 80\n4 81\n8..." }, { "input": "1024 513", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "1024 1023", "output": "2\n1 2\n2 3\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\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 85\n2 86\n2 87\n..." }, { "input": "1013 2", "output": "1012\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 7..." }, { "input": "1013 16", "output": "128\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "1013 23", "output": "88\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n..." }, { "input": "1013 507", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "1013 508", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "1013 1012", "output": "2\n1 2\n2 3\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\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 85\n2 86\n2 87\n..." }, { "input": "100003 2", "output": "100002\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75..." }, { "input": "100003 23", "output": "8696\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 7..." }, { "input": "100003 19683", "output": "12\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n7 14\n14 15\n15 16\n16 17\n17 18\n18 19\n7 20\n20 21\n21 22\n22 23\n23 24\n24 25\n7 26\n26 27\n27 28\n28 29\n29 30\n30 31\n7 32\n32 33\n33 34\n34 35\n35 36\n36 37\n7 38\n38 39\n39 40\n40 41\n41 42\n42 43\n7 44\n44 45\n45 46\n46 47\n47 48\n48 49\n7 50\n50 51\n51 52\n52 53\n53 54\n54 55\n7 56\n56 57\n57 58\n58 59\n59 60\n60 61\n7 62\n62 63\n63 64\n64 65\n65 66\n66 67\n7 68\n68 69\n69 70\n70 71\n71 72\n72 73\n7 74\n74 75\n75 76\n76 77\n77 ..." }, { "input": "100003 100002", "output": "2\n1 2\n2 3\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\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 85\n2 86\n2 87\n..." }, { "input": "100001 2", "output": "100000\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75..." }, { "input": "100001 16", "output": "12500\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 ..." }, { "input": "100001 23", "output": "8696\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 7..." }, { "input": "100001 9091", "output": "22\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n12 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n12 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n12 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n12 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n12 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n..." }, { "input": "100001 19683", "output": "12\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n7 14\n14 15\n15 16\n16 17\n17 18\n18 19\n7 20\n20 21\n21 22\n22 23\n23 24\n24 25\n7 26\n26 27\n27 28\n28 29\n29 30\n30 31\n7 32\n32 33\n33 34\n34 35\n35 36\n36 37\n7 38\n38 39\n39 40\n40 41\n41 42\n42 43\n7 44\n44 45\n45 46\n46 47\n47 48\n48 49\n7 50\n50 51\n51 52\n52 53\n53 54\n54 55\n7 56\n56 57\n57 58\n58 59\n59 60\n60 61\n7 62\n62 63\n63 64\n64 65\n65 66\n66 67\n7 68\n68 69\n69 70\n70 71\n71 72\n72 73\n7 74\n74 75\n75 76\n76 77\n77 ..." }, { "input": "100001 50000", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "100001 50001", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n8 9\n3 10\n10 11\n3 12\n12 13\n3 14\n14 15\n3 16\n16 17\n3 18\n18 19\n3 20\n20 21\n3 22\n22 23\n3 24\n24 25\n3 26\n26 27\n3 28\n28 29\n3 30\n30 31\n3 32\n32 33\n3 34\n34 35\n3 36\n36 37\n3 38\n38 39\n3 40\n40 41\n3 42\n42 43\n3 44\n44 45\n3 46\n46 47\n3 48\n48 49\n3 50\n50 51\n3 52\n52 53\n3 54\n54 55\n3 56\n56 57\n3 58\n58 59\n3 60\n60 61\n3 62\n62 63\n3 64\n64 65\n3 66\n66 67\n3 68\n68 69\n3 70\n70 71\n3 72\n72 73\n3 74\n74 75\n3 76\n76 77\n3 78\n78 79\n3 80\n80 81\n..." }, { "input": "100001 100000", "output": "2\n1 2\n2 3\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\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 85\n2 86\n2 87\n..." }, { "input": "10 6", "output": "4\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n3 8\n3 9\n3 10" } ]
1,013
33,280,000
-1
2,271
39
Inverse Function
[ "implementation" ]
G. Inverse Function
5
64
Petya wrote a programme on C++ that calculated a very interesting function *f*(*n*). Petya ran the program with a certain value of *n* and went to the kitchen to have some tea. The history has no records concerning how long the program had been working. By the time Petya returned, it had completed the calculations and had the result. However while Petya was drinking tea, a sly virus managed to destroy the input file so that Petya can't figure out for which value of *n* the program was run. Help Petya, carry out the inverse function! Mostly, the program consists of a function in C++ with the following simplified syntax: - *function* ::= int f(int n) {*operatorSequence*}- *operatorSequence* ::= *operator*Β |Β *operator*Β *operatorSequence*- *operator* ::= return *arithmExpr*; | if (*logicalExpr*) return *arithmExpr*;- *logicalExpr* ::= *arithmExpr*<=&gt;<=*arithmExpr* | *arithmExpr*<=&lt;<=*arithmExpr* | *arithmExpr* == *arithmExpr*- *arithmExpr* ::= *sum*- *sum* ::= *product* | *sum*<=+<=*product* | *sum*<=-<=*product*- *product* ::= *multiplier* | *product*<=*<=*multiplier* | *product*<=/<=*multiplier*- *multiplier* ::= n | *number* | f(*arithmExpr*)- *number* ::= 0|1|2|... |32767 The whitespaces in a *operatorSequence* are optional. Thus, we have a function, in which body there are two kinds of operators. There is the operator "return *arithmExpr*;" that returns the value of the expression as the value of the function, and there is the conditional operator "if (*logicalExpr*) return *arithmExpr*;" that returns the value of the arithmetical expression when and only when the logical expression is true. Guaranteed that no other constructions of C++ language β€” cycles, assignment operators, nested conditional operators etc, and other variables except the *n* parameter are used in the function. All the constants are integers in the interval [0..32767]. The operators are performed sequentially. After the function has returned a value other operators in the sequence are not performed. Arithmetical expressions are performed taking into consideration the standard priority of the operations. It means that first all the products that are part of the sum are calculated. During the calculation of the products the operations of multiplying and division are performed from the left to the right. Then the summands are summed, and the addition and the subtraction are also performed from the left to the right. Operations "&gt;" (more), "&lt;" (less) and "==" (equals) also have standard meanings. Now you've got to pay close attention! The program is compiled with the help of 15-bit Berland C++ compiler invented by a Berland company BerSoft, that's why arithmetical operations are performed in a non-standard way. Addition, subtraction and multiplication are performed modulo 32768 (if the result of subtraction is negative, then 32768 is added to it until the number belongs to the interval [0..32767]). Division "/" is a usual integer division where the remainder is omitted. Examples of arithmetical operations: Guaranteed that for all values of *n* from 0 to 32767 the given function is performed correctly. That means that: 1. Division by 0 never occures. 2. When performing a function for the value *n*<==<=*N* recursive calls of the function *f* may occur only for the parameter value of 0,<=1,<=...,<=*N*<=-<=1. Consequently, the program never has an infinite recursion. 3. As the result of the sequence of the operators, the function always returns a value. We have to mention that due to all the limitations the value returned by the function *f* is independent from either global variables or the order of performing the calculations of arithmetical expressions as part of the logical one, or from anything else except the value of *n* parameter. That's why the *f* function can be regarded as a function in its mathematical sense, i.e. as a unique correspondence between any value of *n* from the interval [0..32767] and a value of *f*(*n*) from the same interval. Given the value of *f*(*n*), and you should find *n*. If the suitable *n* value is not unique, you should find the maximal one (from the interval [0..32767]).
The first line has an integer *f*(*n*) from the interval [0..32767]. The next lines have the description of the function *f*. In the description can be found extra spaces and line breaks (see the examples) which, of course, can’t break key words int, if, return and numbers. The size of input data can’t exceed 100 bytes.
Output a single number β€” the answer to the problem. If there’s no answer, output "-1" (without quotes).
[ "17\nint f(int n)\n{\nif (n &lt; 100) return 17;\nif (n &gt; 99) return 27;\n}\n", "13\nint f(int n)\n{\nif (n == 0) return 0;\nreturn f(n - 1) + 1;\n}\n", "144\nint f(int n)\n{\nif (n == 0) return 0;\nif (n == 1) return n;\nreturn f(n - 1) + f(n - 2);\n}" ]
[ "99\n", "13", "24588\n" ]
none
[ { "input": "17\nint f(int n)\n{\nif (n < 100) return 17;\nif (n > 99) return 27;\n}", "output": "99" }, { "input": "13\nint f(int n)\n{\nif (n == 0) return 0;\nreturn f(n - 1) + 1;\n}", "output": "13" }, { "input": "144\nint f(int n)\n{\nif (n == 0) return 0;\nif (n == 1) return n;\nreturn f(n - 1) + f(n - 2);\n}", "output": "24588" }, { "input": "32767\nint f(int n)\n{\nreturn n;\n}", "output": "32767" }, { "input": "27926\nint f(int n)\n{\nreturn n*n + 5*n/3 + 19 * 1423;\n}", "output": "29" }, { "input": "1\nint f(int n)\n{\nif (n == 0) return 153/12+5;\nreturn 32767/n;\n}", "output": "32767" }, { "input": "32\nint f(int n)\n{\nif (n == 0) return 1;\nreturn f(n - 1) * 7;\n}", "output": "-1" }, { "input": "1\nint f(int n)\n{\nif (n == 0) return 1;\nreturn f(n - 1) * 7;\n}", "output": "28672" }, { "input": "0\nint f(int n)\n{\nif (n < 2) return 1;\nreturn f(n - 1) + f(n - 2);\n}", "output": "24575" }, { "input": "32\nint f(int n)\n{\nif (n < 2) return 1;\nreturn f(n - 1) + f(n - 2);\n}", "output": "15959" }, { "input": "12351\nint f(int n)\n{\nif (n < 3) return n;\nreturn f(n - 1) * n + f(n - 2) + f(n - 3) / 5;\n}", "output": "26464" }, { "input": "0\nint f(int n){if (n < 1) return 0;return f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(0)))))))))))))))));}", "output": "32767" }, { "input": "0\nint f(int n){if (n < 1) return 1;if (n < 2) return 0;return f(f(f(f(f(f(f(f(f(f(n-1))))))))));}", "output": "32767" }, { "input": "1\nint f(int n){if (n < 1) return 1;if (n < 2) return 0;return f(f(f(f(f(f(f(f(f(f(n-1))))))))));}", "output": "32766" }, { "input": "14\nint f(int n)\n{\nif (n < 1) return 0;\nreturn f(n / 2) + 1;\n}", "output": "16383" }, { "input": "12\nint f(int n)\n{\nif (n < 1) return 0;\nreturn f(n / 2) + 1;\n}", "output": "4095" }, { "input": "100\nint f(int n){if (n < 3) return 1;return f(n-1)+f(n-2)+f(n-3);}", "output": "-1" }, { "input": "7113\nint f(int n)\n{\nif (n < 3) return n;\nreturn f(n - 1) * n + f(n - 2) + f(n - 3) / 5;\n}", "output": "31277" }, { "input": "7169\nint f(int n){if (n < 7) return 1; return f(n-1)+f(n-2)+f(n-3)+f(n-4)+f(n-5)-f(n-6)-f(n-7);}", "output": "32258" }, { "input": "32657\nint f(int n){if (n < 7) return 1; return f(n-1)+f(n-2)+f(n-3)+f(n-4)+f(n-5)-f(n-6)-f(n-7);}", "output": "32308" }, { "input": "5\nint f(int n){if (n < 2) return 1;if (n < 8) return 2;if (n > 8) return 7;if (n > 2) return 5;}", "output": "8" }, { "input": "2\nint f(int n){if (n < 2) return 1;if (n < 8) return 2;if (n > 8) return 7;if (n > 2) return 5;}", "output": "7" }, { "input": "1\nint f(int n){if (n < 2) return 1;if (n < 8) return 2;if (n > 8) return 7;if (n > 2) return 5;}", "output": "1" }, { "input": "7\nint f(int n){if (n < 2) return 1;if (n < 8) return 2;if (n > 8) return 7;if (n > 2) return 5;}", "output": "32767" }, { "input": "6\nint f(int n){if (n < 1025) return n*5+12/6-n/3*15-7;return f(n/2)-f(n-7)*f(n-7)-1024;}", "output": "-1" }, { "input": "32763\nint f(int n){if (n < 1025) return n*5+12/6-n/3*15-7;return f(n/2)-f(n-7)*f(n-7)-1024;}", "output": "1023" }, { "input": "280\nint f(int n){if (n < 1025) return n*5+12/6-n/3*15-7;return f(n/2)-f(n-7)*f(n-7)-1024;}", "output": "32746" }, { "input": "1\nint f(int n){return 1;return 2;return 3;return 4;return 5;return 6;return 7;return 8;return 9;}", "output": "32767" }, { "input": "2\nint f(int n){return 1;return 2;return 3;return 4;return 5;return 6;return 7;return 8;return 9;}", "output": "-1" }, { "input": "3647\nint f(int n){return 1*2*3+2*3*4-3*4*5-4*5*6+5*6*7+n*n*n-n/2/4/5+n+1/2+3*9*6-12+17/7*6+1-1;}", "output": "23117" }, { "input": "17463\nint f(int n){return 1*2*3+2*3*4-3*4*5-4*5*6+5*6*7+n*n*n-n/2/4/5+n+1/2+3*9*6-12+17/7*6+1-1;}", "output": "9167" }, { "input": "17\nint f(int n){if (n > 1000) return n/n;if (n > 100) return n/2;if (n > 10) return n; return 7;}", "output": "17" }, { "input": "55\nint f(int n)\n{\nif (n < 2) return 0;if (f(n-1) == f(n-2)) return f(n-2)+1;return f(n-1);\n}", "output": "111" }, { "input": "3\nint f(int n)\n{\nif (n > 5) return 8;\nif (n < 3) return 1;\nreturn f(12/n/n)+1;\n}", "output": "-1" }, { "input": "2\nint f(int n)\n{\nif (n > 5) return 8;\nif (n < 3) return 1;\nreturn f(12/n/n)+1;\n}", "output": "5" }, { "input": "354\nint f(int n)\n{\nif (n < 4) return 1;\nreturn f(n-1)*f(n-2)*f(n-3)*f(n-4)+n;\n}", "output": "19794" }, { "input": "9182\nint f(int n)\n{\nif (n < 4) return 1;\nreturn f(n-1)*f(n-2)*f(n-3)*f(n-4)+n;\n}", "output": "27142" }, { "input": "32767\nint f(int n)\n{\nif (n < 5) return 0-1;if (f(n/2)*f(n-1) < 0) return f(n+28);return 10;\n}", "output": "4" }, { "input": "57\nint f(int n)\n{\nif (n/2*2 == n) return 1;\nif (n/2*2 == n-1) return 0;\nreturn 57;\n}", "output": "-1" }, { "input": "1\nint f(int n)\n{\nif (n/2*2 == n) return 1;\nif (n/2*2 == n-1) return 0;\nreturn 57;\n}", "output": "32766" }, { "input": "15\nint f(int n){if (n > 1) return f(n-2)/f(n-1)+15;if (n == 0) return 0;if (n == 1) return 1;}", "output": "32767" } ]
840
9,523,200
-1
2,276
496
Removing Columns
[ "brute force", "constructive algorithms", "implementation" ]
null
null
You are given an *n*<=Γ—<=*m* rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table Β  we obtain the table: Β  A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
The first line contains two integers Β β€” *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100). Next *n* lines contain *m* small English letters eachΒ β€” the characters of the table.
Print a single numberΒ β€” the minimum number of columns that you need to remove in order to make the table good.
[ "1 10\ncodeforces\n", "4 4\ncase\ncare\ntest\ncode\n", "5 4\ncode\nforc\nesco\ndefo\nrces\n" ]
[ "0\n", "2\n", "4\n" ]
In the first sample the table is already good. In the second sample you may remove the first and third column. In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition). Let strings *s* and *t* have equal length. Then, *s* is lexicographically larger than *t* if they are not equal and the character following the largest common prefix of *s* and *t* (the prefix may be empty) in *s* is alphabetically larger than the corresponding character of *t*.
[ { "input": "1 10\ncodeforces", "output": "0" }, { "input": "4 4\ncase\ncare\ntest\ncode", "output": "2" }, { "input": "5 4\ncode\nforc\nesco\ndefo\nrces", "output": "4" }, { "input": "2 2\nfb\nye", "output": "0" }, { "input": "5 5\nrzrzh\nrzrzh\nrzrzh\nrzrzh\nrzrzh", "output": "0" }, { "input": "10 10\nddorannorz\nmdrnzqvqgo\ngdtdjmlsuf\neoxbrntqdp\nhribwlslgo\newlqrontvk\nnxibmnawnh\nvxiwdjvdom\nhyhhewmzmp\niysgvzayst", "output": "1" }, { "input": "9 7\nygqartj\nlgwxlqv\nancjjpr\nwnnhkpx\ncnnhvty\nxsfrbqp\nxsolyne\nbsoojiq\nxstetjb", "output": "1" }, { "input": "4 50\nulkteempxafxafcvfwmwhsixwzgbmubcqqceevbbwijeerqbsj\neyqxsievaratndjoekltlqwppfgcukjwxdxexhejbfhzklppkk\npskatxpbjdbmjpwhussetytneohgzxgirluwnbraxtxmaupuid\neappatavdzktqlrjqttmwwroathnulubpjgsjazcycecwmxwvn", "output": "20" }, { "input": "5 50\nvlrkwhvbigkhihwqjpvmohdsszvndheqlmdsspkkxxiedobizr\nmhnzwdefqmttclfxocdmvvtdjtvqhmdllrtrrlnewuqowmtrmp\nrihlhxrqfhpcddslxepesvjqmlqgwyehvxjcsytevujfegeewh\nqrdyiymanvbdjomyruspreihahjhgkcixwowfzczundxqydldq\nkgnrbjlrmkuoiuzeiqwhnyjpuzfnsinqiamlnuzksrdnlvaxjd", "output": "50" }, { "input": "100 1\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\ni\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nv\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx", "output": "0" }, { "input": "1 100\nteloaetuldspjqdlcktjlishwynmjjhlomvemhoyyplbltfwmrlnazbbjvyvwvoxjvvoadkznvxqubgwesoxrznvbdizjdzixecb", "output": "0" }, { "input": "4 100\ngdgmmejiigzsmlarrnfsypvlbutvoxazcigpcospgztqkowfhhbnnbxxrbmwbxwkvxlxzabjjjdtbebedukdelooqlxnadjwjpnp\ndmtsnsbsfdzqvydxcdcnkkfcbseicxhvclsligbhqlkccwujpirymoshkqcosbtlxdypsdqkqaolrqtiibymavcwmbfryttdckhw\njtdmpdljcpciuuoznvqqmafvoqychzfslmwqouuarxctunlzegxsucrwontjplkkxhgixgdbvnewphnatxnwqxqshcexpxlyjuwq\nvhdtvexkyhjmahejbhyfeaompjkdvxmbtdbmvrxrixhnkkjgyvfbxlviatbikfejnqhkrtafftmsgyznpsfxsnzaqdzhxridzilo", "output": "4" }, { "input": "5 2\nab\ndc\ngd\ngc\nhx", "output": "1" }, { "input": "2 1\nb\na", "output": "1" }, { "input": "4 3\nabd\nacc\nbdx\nbcy", "output": "2" }, { "input": "4 2\nad\nac\nbd\nbc", "output": "1" }, { "input": "3 3\naac\nabb\nbba", "output": "0" }, { "input": "3 3\nadc\nbdb\ncda", "output": "0" }, { "input": "4 3\nabz\ndby\ngbx\nhbw", "output": "0" }, { "input": "3 3\naac\nbab\ncaa", "output": "0" }, { "input": "5 6\naaaeaa\nababab\nacacac\nadadad\naeaeae", "output": "0" }, { "input": "3 2\naa\nbc\nba", "output": "1" }, { "input": "3 3\naaz\ndaa\nbaa", "output": "2" }, { "input": "3 3\nabc\nbbb\ncba", "output": "0" }, { "input": "2 3\naxc\nbxa", "output": "0" } ]
109
307,200
0
2,277
994
Fingerprints
[ "implementation" ]
null
null
You are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits. Some keys on the keypad have fingerprints. You believe the correct code is the longest not necessarily contiguous subsequence of the sequence you have that only contains digits with fingerprints on the corresponding keys. Find such code.
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 10$) representing the number of digits in the sequence you have and the number of keys on the keypad that have fingerprints. The next line contains $n$ distinct space-separated integers $x_1, x_2, \ldots, x_n$ ($0 \le x_i \le 9$) representing the sequence. The next line contains $m$ distinct space-separated integers $y_1, y_2, \ldots, y_m$ ($0 \le y_i \le 9$) β€” the keys with fingerprints.
In a single line print a space-separated sequence of integers representing the code. If the resulting sequence is empty, both printing nothing and printing a single line break is acceptable.
[ "7 3\n3 5 7 1 6 2 8\n1 2 7\n", "4 4\n3 4 1 0\n0 1 7 9\n" ]
[ "7 1 2\n", "1 0\n" ]
In the first example, the only digits with fingerprints are $1$, $2$ and $7$. All three of them appear in the sequence you know, $7$ first, then $1$ and then $2$. Therefore the output is 7 1 2. Note that the order is important, and shall be the same as the order in the original sequence. In the second example digits $0$, $1$, $7$ and $9$ have fingerprints, however only $0$ and $1$ appear in the original sequence. $1$ appears earlier, so the output is 1 0. Again, the order is important.
[ { "input": "7 3\n3 5 7 1 6 2 8\n1 2 7", "output": "7 1 2" }, { "input": "4 4\n3 4 1 0\n0 1 7 9", "output": "1 0" }, { "input": "9 4\n9 8 7 6 5 4 3 2 1\n2 4 6 8", "output": "8 6 4 2" }, { "input": "10 5\n3 7 1 2 4 6 9 0 5 8\n4 3 0 7 9", "output": "3 7 4 9 0" }, { "input": "10 10\n1 2 3 4 5 6 7 8 9 0\n4 5 6 7 1 2 3 0 9 8", "output": "1 2 3 4 5 6 7 8 9 0" }, { "input": "1 1\n4\n4", "output": "4" }, { "input": "3 7\n6 3 4\n4 9 0 1 7 8 6", "output": "6 4" }, { "input": "10 1\n9 0 8 1 7 4 6 5 2 3\n0", "output": "0" }, { "input": "5 10\n6 0 3 8 1\n3 1 0 5 4 7 2 8 9 6", "output": "6 0 3 8 1" }, { "input": "8 2\n7 2 9 6 1 0 3 4\n6 3", "output": "6 3" }, { "input": "5 4\n7 0 1 4 9\n0 9 5 3", "output": "0 9" }, { "input": "10 1\n9 6 2 0 1 8 3 4 7 5\n6", "output": "6" }, { "input": "10 2\n7 1 0 2 4 6 5 9 3 8\n3 2", "output": "2 3" }, { "input": "5 9\n3 7 9 2 4\n3 8 4 5 9 6 1 0 2", "output": "3 9 2 4" }, { "input": "10 6\n7 1 2 3 8 0 6 4 5 9\n1 5 8 2 3 6", "output": "1 2 3 8 6 5" }, { "input": "8 2\n7 4 8 9 2 5 6 1\n6 4", "output": "4 6" }, { "input": "10 2\n1 0 3 5 8 9 4 7 6 2\n0 3", "output": "0 3" }, { "input": "7 6\n9 2 8 6 1 3 7\n4 2 0 3 1 8", "output": "2 8 1 3" }, { "input": "1 6\n3\n6 8 2 4 5 3", "output": "3" }, { "input": "1 8\n0\n9 2 4 8 1 5 0 7", "output": "0" }, { "input": "6 9\n7 3 9 4 1 0\n9 1 5 8 0 6 2 7 4", "output": "7 9 4 1 0" }, { "input": "10 2\n4 9 6 8 3 0 1 5 7 2\n0 1", "output": "0 1" }, { "input": "10 5\n5 2 8 0 9 7 6 1 4 3\n9 6 4 1 2", "output": "2 9 6 1 4" }, { "input": "6 3\n8 3 9 2 7 6\n5 4 3", "output": "3" }, { "input": "4 10\n8 3 9 6\n4 9 6 2 7 0 8 1 3 5", "output": "8 3 9 6" }, { "input": "1 2\n1\n1 0", "output": "1" }, { "input": "3 6\n1 2 3\n4 5 6 1 2 3", "output": "1 2 3" }, { "input": "1 2\n2\n1 2", "output": "2" }, { "input": "1 10\n9\n0 1 2 3 4 5 6 7 8 9", "output": "9" } ]
92
0
3
2,279
117
Very Interesting Game
[ "brute force", "number theory" ]
null
null
In a very ancient country the following game was popular. Two people play the game. Initially first player writes a string *s*1, consisting of exactly nine digits and representing a number that does not exceed *a*. After that second player looks at *s*1 and writes a string *s*2, consisting of exactly nine digits and representing a number that does not exceed *b*. Here *a* and *b* are some given constants, *s*1 and *s*2 are chosen by the players. The strings are allowed to contain leading zeroes. If a number obtained by the concatenation (joining together) of strings *s*1 and *s*2 is divisible by *mod*, then the second player wins. Otherwise the first player wins. You are given numbers *a*, *b*, *mod*. Your task is to determine who wins if both players play in the optimal manner. If the first player wins, you are also required to find the lexicographically minimum winning move.
The first line contains three integers *a*, *b*, *mod* (0<=≀<=*a*,<=*b*<=≀<=109, 1<=≀<=*mod*<=≀<=107).
If the first player wins, print "1" and the lexicographically minimum string *s*1 he has to write to win. If the second player wins, print the single number "2".
[ "1 10 7\n", "4 0 9\n" ]
[ "2\n", "1 000000001\n" ]
The lexical comparison of strings is performed by the &lt; operator in modern programming languages. String *x* is lexicographically less than string *y* if exists such *i* (1 ≀ *i* ≀ 9), that *x*<sub class="lower-index">*i*</sub> &lt; *y*<sub class="lower-index">*i*</sub>, and for any *j* (1 ≀ *j* &lt; *i*) *x*<sub class="lower-index">*j*</sub> = *y*<sub class="lower-index">*j*</sub>. These strings always have length 9.
[ { "input": "1 10 7", "output": "2" }, { "input": "4 0 9", "output": "1 000000001" }, { "input": "10 7 8", "output": "2" }, { "input": "6 4 10", "output": "2" }, { "input": "4 1 4", "output": "2" }, { "input": "4 7 9", "output": "1 000000001" }, { "input": "13 4 51", "output": "1 000000001" }, { "input": "0 0 1", "output": "2" }, { "input": "1 0 1", "output": "2" }, { "input": "2 1 3", "output": "1 000000001" }, { "input": "0 2 2", "output": "2" }, { "input": "2 3 1", "output": "2" }, { "input": "3 0 3", "output": "1 000000001" }, { "input": "1 1 2", "output": "2" }, { "input": "3 2 1", "output": "2" }, { "input": "0 3 3", "output": "2" }, { "input": "4 0 13", "output": "1 000000001" }, { "input": "1 2 13", "output": "2" }, { "input": "4 3 12", "output": "1 000000001" }, { "input": "1 2 11", "output": "2" }, { "input": "4 3 12", "output": "1 000000001" }, { "input": "815 216 182", "output": "2" }, { "input": "218 550 593", "output": "1 000000011" }, { "input": "116482865 344094604 3271060", "output": "2" }, { "input": "19749161 751031022 646204", "output": "2" }, { "input": "70499104 10483793 5504995", "output": "2" }, { "input": "1960930 562910 606828", "output": "1 000000011" }, { "input": "8270979 4785512 9669629", "output": "1 000000001" }, { "input": "9323791 4748006 5840080", "output": "1 000000005" }, { "input": "972037745 4602117 5090186", "output": "1 000000011" }, { "input": "585173560 4799128 5611727", "output": "1 000000036" }, { "input": "22033548 813958 4874712", "output": "1 000000001" }, { "input": "702034015 6007275 9777625", "output": "1 000000001" }, { "input": "218556 828183 7799410", "output": "1 000000001" }, { "input": "1167900 2709798 6800151", "output": "1 000000001" }, { "input": "7004769 3114686 4659684", "output": "1 000000002" }, { "input": "1000000000 1000000000 10000000", "output": "2" }, { "input": "3631 1628 367377", "output": "1 000000009" }, { "input": "3966 5002 273075", "output": "1 000000008" }, { "input": "2388 2896 73888", "output": "1 000000016" }, { "input": "0 0 1", "output": "2" }, { "input": "1 0 1", "output": "2" }, { "input": "0 1 1", "output": "2" }, { "input": "1 1 1", "output": "2" }, { "input": "1000000000 0 1", "output": "2" }, { "input": "0 1000000000 1", "output": "2" }, { "input": "1000000000 1000000000 1", "output": "2" }, { "input": "1000000000 0 10000000", "output": "2" }, { "input": "0 1000000000 10000000", "output": "2" }, { "input": "0 0 10000000", "output": "2" }, { "input": "0 999999999 10000000", "output": "2" }, { "input": "999999999 0 10000000", "output": "2" }, { "input": "999999999 999999999 10000000", "output": "2" }, { "input": "999999999 1000000000 10000000", "output": "2" }, { "input": "1000000000 999999999 10000000", "output": "2" }, { "input": "1000000000 10000 10000000", "output": "2" }, { "input": "1 1 1337", "output": "1 000000001" }, { "input": "576694 1234562 1234567", "output": "2" }, { "input": "12350 12000 12345", "output": "1 000000011" }, { "input": "576695 1234562 1234567", "output": "1 000576695" }, { "input": "0 0 11", "output": "2" }, { "input": "999999999 999999999 9009009", "output": "2" }, { "input": "1 0 7", "output": "1 000000001" }, { "input": "1 1 7", "output": "2" }, { "input": "1000000000 9999991 10000000", "output": "2" }, { "input": "9902593 9902584 9902593", "output": "1 002490619" }, { "input": "10000000 9999977 9999979", "output": "1 009909503" }, { "input": "1000000000 1000000000 9999999", "output": "2" }, { "input": "11 9 11", "output": "1 000000010" }, { "input": "0 7 13", "output": "2" }, { "input": "1 0 3", "output": "1 000000001" }, { "input": "100 2 3", "output": "2" }, { "input": "2 7 13", "output": "2" }, { "input": "1 0 9", "output": "1 000000001" }, { "input": "1000000000 9999995 10000000", "output": "2" }, { "input": "1000000000 25 30", "output": "2" }, { "input": "243 1001 1003", "output": "2" }, { "input": "9 9 11", "output": "2" }, { "input": "0 1 11", "output": "2" }, { "input": "4 4 7", "output": "2" }, { "input": "1000000000 1 10", "output": "2" }, { "input": "1 0 11", "output": "1 000000001" }, { "input": "0 0 11", "output": "2" }, { "input": "0 0 3", "output": "2" }, { "input": "10 12000 12345", "output": "2" }, { "input": "1000000000 0 2", "output": "2" }, { "input": "0 1 3", "output": "2" }, { "input": "3 1 7", "output": "1 000000002" }, { "input": "1000000000 2 1000000", "output": "2" }, { "input": "23 0 23", "output": "1 000000001" }, { "input": "123456789 1234561 1234567", "output": "1 000549636" }, { "input": "11 10 13", "output": "1 000000011" }, { "input": "138 11711 11829", "output": "2" }, { "input": "1000000000 100050 1000001", "output": "1 000000101" } ]
0
0
-1
2,282
887
Cubes for Masha
[ "brute force", "implementation" ]
null
null
Absent-minded Masha got set of *n* cubes for her birthday. At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural *x* such she can make using her new cubes all integers from 1 to *x*. To make a number Masha can rotate her cubes and put them in a row. After that, she looks at upper faces of cubes from left to right and reads the number. The number can't contain leading zeros. It's not required to use all cubes to build a number. Pay attention: Masha can't make digit 6 from digit 9 and vice-versa using cube rotations.
In first line integer *n* is given (1<=≀<=*n*<=≀<=3)Β β€” the number of cubes, Masha got for her birthday. Each of next *n* lines contains 6 integers *a**i**j* (0<=≀<=*a**i**j*<=≀<=9)Β β€” number on *j*-th face of *i*-th cube.
Print single integerΒ β€” maximum number *x* such Masha can make any integers from 1 to *x* using her cubes or 0 if Masha can't make even 1.
[ "3\n0 1 2 3 4 5\n6 7 8 9 0 1\n2 3 4 5 6 7\n", "3\n0 1 3 5 6 8\n1 2 4 5 7 8\n2 3 4 6 7 9\n" ]
[ "87", "98" ]
In the first test case, Masha can build all numbers from 1 to 87, but she can't make 88 because there are no two cubes with digit 8.
[ { "input": "3\n0 1 2 3 4 5\n6 7 8 9 0 1\n2 3 4 5 6 7", "output": "87" }, { "input": "3\n0 1 3 5 6 8\n1 2 4 5 7 8\n2 3 4 6 7 9", "output": "98" }, { "input": "3\n0 1 2 3 4 5\n0 1 2 3 4 5\n0 1 2 3 4 5", "output": "5" }, { "input": "3\n1 2 3 7 8 9\n9 8 7 1 2 3\n7 9 2 3 1 8", "output": "3" }, { "input": "1\n5 2 2 5 6 7", "output": "0" }, { "input": "1\n7 6 5 8 9 0", "output": "0" }, { "input": "1\n2 5 9 6 7 9", "output": "0" }, { "input": "1\n6 3 1 9 4 9", "output": "1" }, { "input": "1\n1 9 8 3 7 8", "output": "1" }, { "input": "2\n1 7 2 0 4 3\n5 2 3 6 1 0", "output": "7" }, { "input": "2\n6 0 1 7 2 9\n1 3 4 6 7 0", "output": "4" }, { "input": "2\n8 6 4 1 2 0\n7 8 5 3 2 1", "output": "8" }, { "input": "2\n0 8 6 2 1 3\n5 2 7 1 0 9", "output": "3" }, { "input": "2\n0 9 5 7 6 2\n8 6 2 7 1 4", "output": "2" }, { "input": "3\n5 0 7 6 2 1\n2 7 4 6 1 9\n0 2 6 1 7 5", "output": "2" }, { "input": "3\n0 6 2 9 5 4\n3 8 0 1 6 9\n6 9 0 1 5 2", "output": "6" }, { "input": "3\n5 6 2 9 3 5\n5 4 1 5 9 8\n4 4 2 0 3 5", "output": "6" }, { "input": "3\n0 1 9 1 0 8\n9 9 3 5 6 2\n9 3 9 9 7 3", "output": "3" }, { "input": "3\n2 5 7 4 2 7\n1 5 5 9 0 3\n8 2 0 1 5 1", "output": "5" }, { "input": "1\n4 6 9 8 2 7", "output": "0" }, { "input": "1\n5 3 8 0 2 6", "output": "0" }, { "input": "1\n7 9 5 0 4 6", "output": "0" }, { "input": "1\n4 0 9 6 3 1", "output": "1" }, { "input": "1\n7 9 2 5 0 4", "output": "0" }, { "input": "1\n0 7 6 3 2 4", "output": "0" }, { "input": "1\n9 8 1 6 5 7", "output": "1" }, { "input": "1\n7 3 6 9 8 1", "output": "1" }, { "input": "1\n3 9 1 7 4 5", "output": "1" }, { "input": "1\n8 6 0 9 4 2", "output": "0" }, { "input": "1\n8 2 7 4 1 0", "output": "2" }, { "input": "1\n8 3 5 4 2 9", "output": "0" }, { "input": "1\n0 8 7 1 3 2", "output": "3" }, { "input": "1\n6 2 8 5 1 3", "output": "3" }, { "input": "1\n6 0 7 5 4 8", "output": "0" }, { "input": "1\n6 2 8 4 5 1", "output": "2" }, { "input": "1\n4 3 8 9 2 3", "output": "0" }, { "input": "1\n8 1 9 2 9 7", "output": "2" }, { "input": "1\n3 7 7 6 4 2", "output": "0" }, { "input": "1\n1 4 5 7 0 5", "output": "1" }, { "input": "2\n6 6 4 7 9 0\n2 1 2 8 6 4", "output": "2" }, { "input": "2\n5 3 2 9 8 2\n0 7 4 8 1 8", "output": "5" }, { "input": "2\n5 7 4 2 1 9\n2 2 7 1 1 8", "output": "2" }, { "input": "2\n9 3 3 6 7 2\n6 2 9 1 5 9", "output": "3" }, { "input": "2\n2 0 5 7 0 8\n4 5 1 5 4 9", "output": "2" }, { "input": "2\n2 6 8 1 3 1\n2 1 3 8 6 7", "output": "3" }, { "input": "2\n4 3 8 6 0 1\n4 7 1 8 9 0", "output": "1" }, { "input": "2\n0 2 9 1 8 5\n0 7 4 3 2 5", "output": "5" }, { "input": "2\n1 7 6 9 2 5\n1 6 7 0 9 2", "output": "2" }, { "input": "2\n0 2 9 8 1 7\n6 7 4 3 2 5", "output": "9" }, { "input": "2\n3 6 8 9 5 0\n6 7 0 8 2 3", "output": "0" }, { "input": "2\n5 1 2 3 0 8\n3 6 7 4 9 2", "output": "9" }, { "input": "2\n7 8 6 1 4 5\n8 6 4 3 2 5", "output": "8" }, { "input": "2\n2 3 5 1 9 6\n1 6 8 7 3 9", "output": "3" }, { "input": "2\n1 7 8 6 0 9\n3 2 1 7 4 9", "output": "4" }, { "input": "2\n2 4 0 3 7 6\n3 2 8 7 1 5", "output": "8" }, { "input": "2\n6 5 2 7 1 3\n3 7 8 1 0 9", "output": "3" }, { "input": "2\n5 8 4 7 1 2\n0 8 6 2 4 9", "output": "2" }, { "input": "2\n8 0 6 5 1 4\n7 1 0 8 3 4", "output": "1" }, { "input": "2\n2 3 9 1 6 7\n2 5 4 3 0 6", "output": "7" }, { "input": "3\n9 4 3 0 2 6\n7 0 5 3 3 9\n1 0 7 4 6 7", "output": "7" }, { "input": "3\n3 8 5 1 5 5\n1 5 7 2 6 9\n4 3 4 8 8 9", "output": "9" }, { "input": "3\n7 7 2 5 3 2\n3 0 0 6 4 4\n1 2 1 1 9 1", "output": "7" }, { "input": "3\n8 1 6 8 6 8\n7 0 2 5 8 4\n5 2 0 3 1 9", "output": "32" }, { "input": "3\n2 7 4 0 7 1\n5 5 4 9 1 4\n2 1 7 5 1 7", "output": "2" }, { "input": "3\n4 4 5 0 6 6\n7 1 6 9 5 4\n5 0 4 0 3 9", "output": "1" }, { "input": "3\n9 4 3 3 9 3\n1 0 3 4 5 3\n2 9 6 2 4 1", "output": "6" }, { "input": "3\n3 8 3 5 5 5\n3 0 1 6 6 3\n0 4 3 7 2 4", "output": "8" }, { "input": "3\n4 1 0 8 0 2\n1 5 3 5 0 7\n7 7 2 7 2 2", "output": "5" }, { "input": "3\n8 1 8 2 7 1\n9 1 9 9 4 7\n0 0 9 0 4 0", "output": "2" }, { "input": "3\n4 6 0 3 9 2\n8 6 9 0 7 2\n6 9 3 2 5 7", "output": "0" }, { "input": "3\n5 1 2 9 6 4\n9 0 6 4 2 8\n4 6 2 8 3 7", "output": "10" }, { "input": "3\n9 3 1 8 4 6\n6 9 1 2 0 7\n8 9 1 5 0 3", "output": "21" }, { "input": "3\n7 1 3 0 2 4\n2 4 3 0 9 5\n1 9 8 0 6 5", "output": "65" }, { "input": "3\n9 4 6 2 7 0\n3 7 1 9 6 4\n6 1 0 8 7 2", "output": "4" }, { "input": "3\n2 7 3 6 4 5\n0 2 1 9 4 8\n8 6 9 5 4 0", "output": "10" }, { "input": "3\n2 6 3 7 1 0\n9 1 2 4 7 6\n1 4 8 7 6 2", "output": "4" }, { "input": "3\n5 4 8 1 6 7\n0 9 3 5 8 6\n2 4 7 8 1 3", "output": "21" }, { "input": "3\n7 2 1 3 6 9\n0 3 8 4 7 6\n1 4 5 8 7 0", "output": "21" }, { "input": "3\n8 6 0 5 4 9\n1 8 5 3 9 7\n7 4 5 1 6 8", "output": "1" }, { "input": "1\n0 1 2 3 4 5", "output": "5" }, { "input": "3\n0 1 1 2 2 3\n4 5 6 7 8 9\n3 4 5 6 7 8", "output": "9" }, { "input": "2\n0 1 2 3 4 5\n6 7 8 9 1 2", "output": "29" }, { "input": "3\n0 1 2 3 4 5\n6 7 8 9 1 2\n3 4 5 6 7 8", "output": "98" }, { "input": "3\n0 1 1 2 2 3\n4 5 6 7 8 9\n3 4 5 6 7 1", "output": "19" }, { "input": "2\n0 1 2 3 4 5\n6 7 8 9 6 6", "output": "9" }, { "input": "2\n0 1 2 3 4 5\n4 5 6 7 8 9", "output": "9" }, { "input": "2\n1 8 9 1 1 0\n2 3 4 5 6 7", "output": "9" }, { "input": "2\n0 1 2 3 4 5\n9 8 7 6 5 4", "output": "9" }, { "input": "3\n2 3 4 5 6 7\n3 4 5 6 7 8\n9 1 2 3 4 5", "output": "9" }, { "input": "3\n1 1 2 3 4 5\n6 7 8 9 0 2\n3 4 5 6 7 8", "output": "10" }, { "input": "3\n1 1 1 0 2 3\n4 5 6 7 8 9\n0 0 0 0 0 0", "output": "10" }, { "input": "3\n0 1 2 2 4 5\n6 7 8 9 0 1\n3 3 4 5 6 7", "output": "21" }, { "input": "3\n2 0 1 3 4 5\n6 7 8 9 1 1\n3 4 5 6 6 7", "output": "19" }, { "input": "3\n1 1 1 1 1 1\n0 2 3 4 5 6\n7 8 9 2 3 4", "output": "10" }, { "input": "2\n0 1 2 3 4 5\n6 6 6 7 8 9", "output": "9" }, { "input": "3\n3 4 5 6 8 9\n1 1 1 1 1 1\n1 2 4 5 7 0", "output": "19" } ]
62
0
3
2,293
820
Mister B and Book Reading
[ "implementation" ]
null
null
Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had *c* pages. At first day Mister B read *v*0 pages, but after that he started to speed up. Every day, starting from the second, he read *a* pages more than on the previous day (at first day he read *v*0 pages, at secondΒ β€” *v*0<=+<=*a* pages, at thirdΒ β€” *v*0<=+<=2*a* pages, and so on). But Mister B is just a human, so he physically wasn't able to read more than *v*1 pages per day. Also, to refresh his memory, every day, starting from the second, Mister B had to reread last *l* pages he read on the previous day. Mister B finished the book when he read the last page for the first time. Help Mister B to calculate how many days he needed to finish the book.
First and only line contains five space-separated integers: *c*, *v*0, *v*1, *a* and *l* (1<=≀<=*c*<=≀<=1000, 0<=≀<=*l*<=&lt;<=*v*0<=≀<=*v*1<=≀<=1000, 0<=≀<=*a*<=≀<=1000) β€” the length of the book in pages, the initial reading speed, the maximum reading speed, the acceleration in reading speed and the number of pages for rereading.
Print one integer β€” the number of days Mister B needed to finish the book.
[ "5 5 10 5 4\n", "12 4 12 4 1\n", "15 1 100 0 0\n" ]
[ "1\n", "3\n", "15\n" ]
In the first sample test the book contains 5 pages, so Mister B read it right at the first day. In the second sample test at first day Mister B read pages number 1 - 4, at second dayΒ β€” 4 - 11, at third dayΒ β€” 11 - 12 and finished the book. In third sample test every day Mister B read 1 page of the book, so he finished in 15 days.
[ { "input": "5 5 10 5 4", "output": "1" }, { "input": "12 4 12 4 1", "output": "3" }, { "input": "15 1 100 0 0", "output": "15" }, { "input": "1 1 1 0 0", "output": "1" }, { "input": "1000 999 1000 1000 998", "output": "2" }, { "input": "1000 2 2 5 1", "output": "999" }, { "input": "1000 1 1 1000 0", "output": "1000" }, { "input": "737 41 74 12 11", "output": "13" }, { "input": "1000 1000 1000 0 999", "output": "1" }, { "input": "765 12 105 5 7", "output": "17" }, { "input": "15 2 2 1000 0", "output": "8" }, { "input": "1000 1 1000 1000 0", "output": "2" }, { "input": "20 3 7 1 2", "output": "6" }, { "input": "1000 500 500 1000 499", "output": "501" }, { "input": "1 1000 1000 1000 0", "output": "1" }, { "input": "1000 2 1000 56 0", "output": "7" }, { "input": "1000 2 1000 802 0", "output": "3" }, { "input": "16 1 8 2 0", "output": "4" }, { "input": "20 6 10 2 2", "output": "3" }, { "input": "8 2 12 4 1", "output": "3" }, { "input": "8 6 13 2 5", "output": "2" }, { "input": "70 4 20 87 0", "output": "5" }, { "input": "97 8 13 234 5", "output": "13" }, { "input": "16 4 23 8 3", "output": "3" }, { "input": "65 7 22 7 4", "output": "5" }, { "input": "93 10 18 11 7", "output": "9" }, { "input": "86 13 19 15 9", "output": "9" }, { "input": "333 17 50 10 16", "output": "12" }, { "input": "881 16 55 10 12", "output": "23" }, { "input": "528 11 84 3 9", "output": "19" }, { "input": "896 2 184 8 1", "output": "16" }, { "input": "236 10 930 9 8", "output": "8" }, { "input": "784 1 550 14 0", "output": "12" }, { "input": "506 1 10 4 0", "output": "53" }, { "input": "460 1 3 2 0", "output": "154" }, { "input": "701 1 3 1 0", "output": "235" }, { "input": "100 49 50 1000 2", "output": "3" }, { "input": "100 1 100 100 0", "output": "2" }, { "input": "12 1 4 2 0", "output": "4" }, { "input": "22 10 12 0 0", "output": "3" }, { "input": "20 10 15 1 4", "output": "3" }, { "input": "1000 5 10 1 4", "output": "169" }, { "input": "1000 1 1000 1 0", "output": "45" }, { "input": "4 1 2 2 0", "output": "3" }, { "input": "1 5 5 1 1", "output": "1" }, { "input": "19 10 11 0 2", "output": "3" }, { "input": "1 2 3 0 0", "output": "1" }, { "input": "10 1 4 10 0", "output": "4" }, { "input": "20 3 100 1 1", "output": "5" }, { "input": "1000 5 9 5 0", "output": "112" }, { "input": "1 11 12 0 10", "output": "1" }, { "input": "1 1 1 1 0", "output": "1" }, { "input": "1000 1 20 1 0", "output": "60" }, { "input": "9 1 4 2 0", "output": "4" }, { "input": "129 2 3 4 0", "output": "44" }, { "input": "4 2 2 0 1", "output": "3" }, { "input": "1000 1 10 100 0", "output": "101" }, { "input": "100 1 100 1 0", "output": "14" }, { "input": "8 3 4 2 0", "output": "3" }, { "input": "20 1 6 4 0", "output": "5" }, { "input": "8 2 4 2 0", "output": "3" }, { "input": "11 5 6 7 2", "output": "3" }, { "input": "100 120 130 120 0", "output": "1" }, { "input": "7 1 4 1 0", "output": "4" }, { "input": "5 3 10 0 2", "output": "3" }, { "input": "5 2 2 0 0", "output": "3" }, { "input": "1000 10 1000 10 0", "output": "14" }, { "input": "25 3 50 4 2", "output": "4" }, { "input": "9 10 10 10 9", "output": "1" }, { "input": "17 10 12 6 5", "output": "2" }, { "input": "15 5 10 3 0", "output": "3" }, { "input": "8 3 5 1 0", "output": "3" }, { "input": "19 1 12 5 0", "output": "4" }, { "input": "1000 10 1000 1 0", "output": "37" }, { "input": "100 1 2 1000 0", "output": "51" }, { "input": "20 10 11 1000 9", "output": "6" }, { "input": "16 2 100 1 1", "output": "5" }, { "input": "18 10 13 2 5", "output": "3" }, { "input": "12 3 5 3 1", "output": "4" }, { "input": "17 3 11 2 0", "output": "4" }, { "input": "4 2 100 1 1", "output": "2" }, { "input": "7 4 5 2 3", "output": "3" }, { "input": "100 1 2 2 0", "output": "51" }, { "input": "50 4 5 5 0", "output": "11" }, { "input": "1 2 2 0 1", "output": "1" }, { "input": "1000 2 3 10 1", "output": "500" }, { "input": "500 10 500 1000 0", "output": "2" }, { "input": "1000 4 12 1 0", "output": "87" }, { "input": "18 10 13 1 5", "output": "3" }, { "input": "7 3 6 2 2", "output": "3" }, { "input": "15 5 100 1 2", "output": "4" }, { "input": "100 1 10 1 0", "output": "15" }, { "input": "8 2 7 5 1", "output": "2" }, { "input": "11 2 4 1 1", "output": "5" }, { "input": "1000 500 900 100 300", "output": "3" }, { "input": "7 1 2 5 0", "output": "4" }, { "input": "7 3 5 3 2", "output": "3" }, { "input": "7 3 10 2 1", "output": "2" }, { "input": "1000 501 510 1 499", "output": "50" }, { "input": "1000 1 1000 2 0", "output": "32" }, { "input": "1 5 5 0 0", "output": "1" }, { "input": "18 10 15 1 5", "output": "3" }, { "input": "100 4 1000 1 2", "output": "13" }, { "input": "20 2 40 1 1", "output": "6" }, { "input": "1 11 1000 100 1", "output": "1" }, { "input": "6 4 4 1 2", "output": "2" }, { "input": "8 3 5 3 1", "output": "3" }, { "input": "10 5 7 1 2", "output": "3" }, { "input": "400 100 198 1 99", "output": "25" }, { "input": "3 1 2 5 0", "output": "2" } ]
77
0
-1
2,295
25
Phone numbers
[ "implementation" ]
B. Phone numbers
2
256
Phone number in Berland is a sequence of *n* digits. Often, to make it easier to memorize the number, it is divided into groups of two or three digits. For example, the phone number 1198733 is easier to remember as 11-987-33. Your task is to find for a given phone number any of its divisions into groups of two or three digits.
The first line contains integer *n* (2<=≀<=*n*<=≀<=100) β€” amount of digits in the phone number. The second line contains *n* digits β€” the phone number to divide into groups.
Output any of divisions of the given phone number into groups of two or three digits. Separate groups by single character -. If the answer is not unique, output any.
[ "6\n549871\n", "7\n1198733\n" ]
[ "54-98-71", "11-987-33\n" ]
none
[ { "input": "6\n549871", "output": "54-98-71" }, { "input": "7\n1198733", "output": "119-87-33" }, { "input": "2\n74", "output": "74" }, { "input": "2\n33", "output": "33" }, { "input": "3\n074", "output": "074" }, { "input": "3\n081", "output": "081" }, { "input": "4\n3811", "output": "38-11" }, { "input": "5\n21583", "output": "215-83" }, { "input": "8\n33408349", "output": "33-40-83-49" }, { "input": "9\n988808426", "output": "988-80-84-26" }, { "input": "10\n0180990956", "output": "01-80-99-09-56" }, { "input": "15\n433488906230138", "output": "433-48-89-06-23-01-38" }, { "input": "22\n7135498415686025907059", "output": "71-35-49-84-15-68-60-25-90-70-59" }, { "input": "49\n2429965524999668169991253653390090510755018570235", "output": "242-99-65-52-49-99-66-81-69-99-12-53-65-33-90-09-05-10-75-50-18-57-02-35" }, { "input": "72\n491925337784111770500147619881727525570039735507439360627744863794794290", "output": "49-19-25-33-77-84-11-17-70-50-01-47-61-98-81-72-75-25-57-00-39-73-55-07-43-93-60-62-77-44-86-37-94-79-42-90" }, { "input": "95\n32543414456047900690980198395035321172843693417425457554204776648220562494524275489599199209210", "output": "325-43-41-44-56-04-79-00-69-09-80-19-83-95-03-53-21-17-28-43-69-34-17-42-54-57-55-42-04-77-66-48-22-05-62-49-45-24-27-54-89-59-91-99-20-92-10" }, { "input": "97\n9362344595153688016434451101547661156123505108492010669557671355055642365998461003851354321478898", "output": "936-23-44-59-51-53-68-80-16-43-44-51-10-15-47-66-11-56-12-35-05-10-84-92-01-06-69-55-76-71-35-50-55-64-23-65-99-84-61-00-38-51-35-43-21-47-88-98" }, { "input": "98\n65521815795893886057122984634320900545031770769333931308009346017867969790810907868670369236928568", "output": "65-52-18-15-79-58-93-88-60-57-12-29-84-63-43-20-90-05-45-03-17-70-76-93-33-93-13-08-00-93-46-01-78-67-96-97-90-81-09-07-86-86-70-36-92-36-92-85-68" }, { "input": "99\n455213856470326729480192345541970106407563996625458559297407682539801838244443866898560852503660390", "output": "455-21-38-56-47-03-26-72-94-80-19-23-45-54-19-70-10-64-07-56-39-96-62-54-58-55-92-97-40-76-82-53-98-01-83-82-44-44-38-66-89-85-60-85-25-03-66-03-90" }, { "input": "100\n4004223124942730640235383244438257614581534320356060987241659784249551110165034719443327659510644224", "output": "40-04-22-31-24-94-27-30-64-02-35-38-32-44-43-82-57-61-45-81-53-43-20-35-60-60-98-72-41-65-97-84-24-95-51-11-01-65-03-47-19-44-33-27-65-95-10-64-42-24" } ]
92
0
3.977
2,310
596
Wilbur and Array
[ "greedy", "implementation" ]
null
null
Wilbur the pig is tinkering with arrays again. He has the array *a*1,<=*a*2,<=...,<=*a**n* initially consisting of *n* zeros. At one step, he can choose any index *i* and either add 1 to all elements *a**i*,<=*a**i*<=+<=1,<=... ,<=*a**n* or subtract 1 from all elements *a**i*,<=*a**i*<=+<=1,<=...,<=*a**n*. His goal is to end up with the array *b*1,<=*b*2,<=...,<=*b**n*. Of course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the length of the array *a**i*. Initially *a**i*<==<=0 for every position *i*, so this array is not given in the input. The second line of the input contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (<=-<=109<=≀<=*b**i*<=≀<=109).
Print the minimum number of steps that Wilbur needs to make in order to achieve *a**i*<==<=*b**i* for all *i*.
[ "5\n1 2 3 4 5\n", "4\n1 2 2 1\n" ]
[ "5", "3" ]
In the first sample, Wilbur may successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes. In the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract 1.
[ { "input": "5\n1 2 3 4 5", "output": "5" }, { "input": "4\n1 2 2 1", "output": "3" }, { "input": "3\n1 2 4", "output": "4" }, { "input": "6\n1 2 3 6 5 4", "output": "8" }, { "input": "10\n2 1 4 3 6 5 8 7 10 9", "output": "19" }, { "input": "7\n12 6 12 13 4 3 2", "output": "36" }, { "input": "15\n15 14 13 1 2 3 12 11 10 4 5 6 9 8 7", "output": "55" }, { "input": "16\n1 2 3 4 13 14 15 16 9 10 11 12 5 6 7 8", "output": "36" }, { "input": "6\n1000 1 2000 1 3000 1", "output": "11995" }, { "input": "1\n0", "output": "0" }, { "input": "5\n1000000000 1 1000000000 1 1000000000", "output": "4999999996" }, { "input": "5\n1000000000 0 1000000000 0 1000000000", "output": "5000000000" }, { "input": "10\n1000000000 0 1000000000 0 1000000000 0 1000000000 0 1000000000 0", "output": "10000000000" }, { "input": "10\n1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000", "output": "19000000000" }, { "input": "7\n0 1000000000 0 1000000000 0 1000000000 0", "output": "6000000000" }, { "input": "4\n1000000000 -1000000000 1000000000 -1000000000", "output": "7000000000" }, { "input": "20\n1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000", "output": "39000000000" }, { "input": "11\n1000000000 0 1000000000 0 1000000000 0 1000000000 0 1000000000 0 1000000000", "output": "11000000000" }, { "input": "5\n1000000000 -1000000000 1000000000 -1000000000 1000000000", "output": "9000000000" }, { "input": "22\n1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000", "output": "43000000000" } ]
217
22,016,000
3
2,317
984
Game
[ "sortings" ]
null
null
Two players play a game. Initially there are $n$ integers $a_1, a_2, \ldots, a_n$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i.Β e. $n - 1$ turns are made. The first player makes the first move, then players alternate turns. The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it. You want to know what number will be left on the board after $n - 1$ turns if both players make optimal moves.
The first line contains one integer $n$ ($1 \le n \le 1000$)Β β€” the number of numbers on the board. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$).
Print one number that will be left on the board.
[ "3\n2 1 3\n", "3\n2 2 2\n" ]
[ "2", "2" ]
In the first sample, the first player erases $3$ and the second erases $1$. $2$ is left on the board. In the second sample, $2$ is left on the board regardless of the actions of the players.
[ { "input": "3\n2 1 3", "output": "2" }, { "input": "3\n2 2 2", "output": "2" }, { "input": "9\n44 53 51 80 5 27 74 79 94", "output": "53" }, { "input": "10\n38 82 23 37 96 4 81 60 67 86", "output": "60" }, { "input": "10\n58 26 77 15 53 81 68 48 22 65", "output": "53" }, { "input": "1\n124", "output": "124" }, { "input": "2\n2 1", "output": "1" }, { "input": "3\n1 1 1000", "output": "1" }, { "input": "2\n322 322", "output": "322" }, { "input": "3\n9 92 12", "output": "12" }, { "input": "3\n1 2 2", "output": "2" } ]
109
0
3
2,321
717
R3D3’s Summer Adventure
[ "dp", "greedy" ]
null
null
R3D3 spent some time on an internship in MDCS. After earning enough money, he decided to go on a holiday somewhere far, far away. He enjoyed suntanning, drinking alcohol-free cocktails and going to concerts of popular local bands. While listening to "The White Buttons" and their hit song "Dacan the Baker", he met another robot for whom he was sure is the love of his life. Well, his summer, at least. Anyway, R3D3 was too shy to approach his potential soulmate, so he decided to write her a love letter. However, he stumbled upon a problem. Due to a terrorist threat, the Intergalactic Space Police was monitoring all letters sent in the area. Thus, R3D3 decided to invent his own alphabet, for which he was sure his love would be able to decipher. There are *n* letters in R3D3’s alphabet, and he wants to represent each letter as a sequence of '0' and '1', so that no letter’s sequence is a prefix of another letter's sequence. Since the Intergalactic Space Communications Service has lately introduced a tax for invented alphabets, R3D3 must pay a certain amount of money for each bit in his alphabet’s code (check the sample test for clarifications). He is too lovestruck to think clearly, so he asked you for help. Given the costs *c*0 and *c*1 for each '0' and '1' in R3D3’s alphabet, respectively, you should come up with a coding for the alphabet (with properties as above) with minimum total cost.
The first line of input contains three integers *n* (2<=≀<=*n*<=≀<=108), *c*0 and *c*1 (0<=≀<=*c*0,<=*c*1<=≀<=108)Β β€” the number of letters in the alphabet, and costs of '0' and '1', respectively.
Output a single integerΒ β€” minimum possible total a cost of the whole alphabet.
[ "4 1 2\n" ]
[ "12\n" ]
There are 4 letters in the alphabet. The optimal encoding is "00", "01", "10", "11". There are 4 zeroes and 4 ones used, so the total cost is 4Β·1 + 4Β·2 = 12.
[ { "input": "4 1 2", "output": "12" }, { "input": "2 1 5", "output": "6" }, { "input": "3 1 1", "output": "5" }, { "input": "5 5 5", "output": "60" }, { "input": "4 0 0", "output": "0" }, { "input": "6 0 6", "output": "30" }, { "input": "6 6 0", "output": "30" }, { "input": "2 1 2", "output": "3" }, { "input": "100000000 1 0", "output": "99999999" }, { "input": "2 0 0", "output": "0" }, { "input": "2 100000000 100000000", "output": "200000000" }, { "input": "2 100000000 0", "output": "100000000" }, { "input": "2 0 100000000", "output": "100000000" }, { "input": "100000000 0 0", "output": "0" }, { "input": "100000000 100000000 100000000", "output": "266578227200000000" }, { "input": "100000000 100000000 0", "output": "9999999900000000" }, { "input": "100000000 0 100000000", "output": "9999999900000000" }, { "input": "2 50000000 0", "output": "50000000" }, { "input": "2 50000000 100000000", "output": "150000000" }, { "input": "2 50000000 0", "output": "50000000" }, { "input": "2 50000000 100000000", "output": "150000000" }, { "input": "100000000 50000000 0", "output": "4999999950000000" }, { "input": "100000000 50000000 100000000", "output": "191720992950000000" }, { "input": "100000000 50000000 0", "output": "4999999950000000" }, { "input": "100000000 50000000 100000000", "output": "191720992950000000" }, { "input": "96212915 66569231 66289469", "output": "170023209909758400" }, { "input": "39969092 91869601 91924349", "output": "93003696194821620" }, { "input": "26854436 29462638 67336233", "output": "30373819153055635" }, { "input": "39201451 80233602 30662934", "output": "50953283386656312" }, { "input": "92820995 96034432 40568102", "output": "158135215198065044" }, { "input": "81913246 61174868 31286889", "output": "96084588586645841" }, { "input": "74790405 66932852 48171076", "output": "111690840882243696" }, { "input": "88265295 26984472 18821097", "output": "52835608063500861" }, { "input": "39858798 77741429 44017779", "output": "59709461677488470" }, { "input": "70931513 41663344 29095671", "output": "64816798089350400" }, { "input": "68251617 52232534 34187120", "output": "75694251898945158" }, { "input": "44440915 82093126 57268128", "output": "77907273273831800" }, { "input": "61988457 90532323 72913492", "output": "130757350538583270" }, { "input": "13756397 41019327 86510346", "output": "19895886795999000" }, { "input": "84963589 37799442 20818727", "output": "63754887412974663" }, { "input": "99338896 62289589 49020203", "output": "146320678028775569" }, { "input": "1505663 3257962 1039115", "output": "60023256524142" }, { "input": "80587587 25402325 8120971", "output": "32044560697691212" }, { "input": "64302230 83635846 22670768", "output": "77790985833197594" }, { "input": "6508457 32226669 8706339", "output": "2645634460061466" }, { "input": "1389928 84918086 54850899", "output": "1953921305304795" }, { "input": "37142108 10188690 35774598", "output": "19009588918065432" }, { "input": "86813943 11824369 38451380", "output": "51645349299460766" }, { "input": "14913475 61391038 9257618", "output": "9761450207212562" }, { "input": "25721978 63666459 14214946", "output": "20847031763747988" }, { "input": "73363656 63565575 76409698", "output": "133919836504944416" }, { "input": "34291060 92893503 64680754", "output": "66960630525688676" }, { "input": "85779772 26434899 86820336", "output": "114681463889615136" }, { "input": "7347370 2098650 66077918", "output": "3070602135161752" }, { "input": "28258585 6194848 49146833", "output": "14441957862691571" }, { "input": "9678 133 5955", "output": "196970292" }, { "input": "9251 4756 2763", "output": "448302621" }, { "input": "1736 5628 2595", "output": "73441521" }, { "input": "5195 1354 2885", "output": "130236572" }, { "input": "1312 5090 9909", "output": "98808420" }, { "input": "8619 6736 9365", "output": "900966230" }, { "input": "151 7023 3093", "output": "5267919" }, { "input": "5992 2773 6869", "output": "340564941" }, { "input": "3894 9921 3871", "output": "299508763" }, { "input": "1006 9237 1123", "output": "38974261" }, { "input": "9708 3254 2830", "output": "391502526" }, { "input": "1504 1123 626", "output": "13538132" }, { "input": "8642 5709 51", "output": "135655830" }, { "input": "8954 4025 7157", "output": "641304164" }, { "input": "4730 8020 8722", "output": "484587068" }, { "input": "2500 5736 4002", "output": "136264140" }, { "input": "6699 4249 1068", "output": "196812772" }, { "input": "4755 6759 4899", "output": "336456318" }, { "input": "8447 1494 4432", "output": "298387478" }, { "input": "6995 4636 8251", "output": "561476311" }, { "input": "4295 9730 4322", "output": "346320888" }, { "input": "8584 4286 9528", "output": "738058224" }, { "input": "174 6826 355", "output": "2889605" }, { "input": "5656 7968 3400", "output": "379249528" }, { "input": "2793 175 3594", "output": "36405762" }, { "input": "2888 9056 3931", "output": "204521173" }, { "input": "6222 7124 6784", "output": "547839384" }, { "input": "8415 8714 2475", "output": "545452719" }, { "input": "2179 7307 8608", "output": "192281235" }, { "input": "1189 1829 6875", "output": "46521099" } ]
46
0
0
2,322
825
Binary Protocol
[ "implementation" ]
null
null
Polycarp has just invented a new binary protocol for data transmission. He is encoding positive integer decimal number to binary string using following algorithm: - Each digit is represented with number of '1' characters equal to the value of that digit (for 0 it is zero ones). - Digits are written one by one in order corresponding to number and separated by single '0' character. Though Polycarp learnt how to encode the numbers, he has no idea how to decode them back. Help him calculate the decoded number.
The first line contains one integer number *n* (1<=≀<=*n*<=≀<=89) β€” length of the string *s*. The second line contains string *s* β€” sequence of '0' and '1' characters, number in its encoded format. It is guaranteed that the number corresponding to the string is positive and doesn't exceed 109. The string always starts with '1'.
Print the decoded number.
[ "3\n111\n", "9\n110011101\n" ]
[ "3\n", "2031\n" ]
none
[ { "input": "3\n111", "output": "3" }, { "input": "9\n110011101", "output": "2031" }, { "input": "1\n1", "output": "1" }, { "input": "3\n100", "output": "100" }, { "input": "5\n10001", "output": "1001" }, { "input": "14\n11001100011000", "output": "202002000" }, { "input": "31\n1000011111111100011110111111111", "output": "100090049" }, { "input": "53\n10110111011110111110111111011111110111111110111111111", "output": "123456789" }, { "input": "89\n11111111101111111110111111111011111111101111111110111111111011111111101111111110111111111", "output": "999999999" }, { "input": "10\n1000000000", "output": "1000000000" }, { "input": "2\n10", "output": "10" }, { "input": "4\n1110", "output": "30" }, { "input": "8\n10101010", "output": "11110" } ]
78
23,142,400
-1
2,323
389
Fox and Cross
[ "greedy", "implementation" ]
null
null
Fox Ciel has a board with *n* rows and *n* columns. So, the board consists of *n*<=Γ—<=*n* cells. Each cell contains either a symbol '.', or a symbol '#'. A cross on the board is a connected set of exactly five cells of the board that looks like a cross. The picture below shows how it looks. Ciel wants to draw several (may be zero) crosses on the board. Each cross must cover exactly five cells with symbols '#', and any cell with symbol '#' must belong to some cross. No two crosses can share a cell. Please, tell Ciel if she can draw the crosses in the described way.
The first line contains an integer *n* (3<=≀<=*n*<=≀<=100) β€” the size of the board. Each of the next *n* lines describes one row of the board. The *i*-th line describes the *i*-th row of the board and consists of *n* characters. Each character is either a symbol '.', or a symbol '#'.
Output a single line with "YES" if Ciel can draw the crosses in the described way. Otherwise output a single line with "NO".
[ "5\n.#...\n####.\n.####\n...#.\n.....\n", "4\n####\n####\n####\n####\n", "6\n.#....\n####..\n.####.\n.#.##.\n######\n.#..#.\n", "6\n.#..#.\n######\n.####.\n.####.\n######\n.#..#.\n", "3\n...\n...\n...\n" ]
[ "YES\n", "NO\n", "YES\n", "NO\n", "YES\n" ]
In example 1, you can draw two crosses. The picture below shows what they look like. In example 2, the board contains 16 cells with '#', but each cross contains 5. Since 16 is not a multiple of 5, so it's impossible to cover all.
[ { "input": "4\n####\n####\n####\n####", "output": "NO" }, { "input": "6\n.#....\n####..\n.####.\n.#.##.\n######\n.#..#.", "output": "YES" }, { "input": "6\n.#..#.\n######\n.####.\n.####.\n######\n.#..#.", "output": "NO" }, { "input": "5\n.....\n.#...\n####.\n.####\n...#.", "output": "YES" }, { "input": "5\n#....\n###..\n.###.\n..##.\n..##.", "output": "NO" }, { "input": "10\n....#...#.\n.#.###.###\n#####.#.#.\n.#######..\n.#.##.#.#.\n######.###\n.#..#.#.#.\n.#..#####.\n##########\n.#..#...#.", "output": "YES" }, { "input": "8\n.#...##.\n##.##.#.\n#....##.\n#.###.##\n...##..#\n#..#.##.\n#..#.#..\n#....###", "output": "NO" }, { "input": "15\n.#...##.##.....\n.###.#.#.##..#.\n##.#####......#\n#....###.#..#..\n#.##.#.###.###.\n.###....#.###.#\n##.....##..#...\n.#.########...#\n##..#..##..#.##\n#...#....##....\n.......#.......\n.#..#.#.####...\n.#..#...#..###.\n#..#.#..#......\n###.....##....#", "output": "NO" }, { "input": "7\n.......\n.......\n.......\n.......\n.....#.\n....###\n.....#.", "output": "YES" }, { "input": "9\n.#.#....#\n#.#.....#\n.#..#..#.\n#.#....##\n...#...#.\n#..###.#.\n....#..##\n.#.###...\n....##..#", "output": "NO" }, { "input": "5\n#...#\n.....\n..#..\n.....\n#...#", "output": "NO" }, { "input": "3\n.#.\n#.#\n.#.", "output": "NO" }, { "input": "3\n...\n...\n..#", "output": "NO" } ]
77
2,252,800
3
2,325
290
Mysterious strings
[ "*special", "implementation" ]
null
null
The input contains a single integer *a* (1<=≀<=*a*<=≀<=40). Output a single string.
The input contains a single integer *a* (1<=≀<=*a*<=≀<=40).
Output a single string.
[ "2\n", "8\n", "29\n" ]
[ "Adams\n", "Van Buren\n", "Harding\n" ]
none
[ { "input": "2", "output": "Adams" }, { "input": "8", "output": "Van Buren" }, { "input": "29", "output": "Harding" }, { "input": "1", "output": "Washington" }, { "input": "3", "output": "Jefferson" }, { "input": "4", "output": "Madison" }, { "input": "5", "output": "Monroe" }, { "input": "6", "output": "Adams" }, { "input": "7", "output": "Jackson" }, { "input": "9", "output": "Harrison" }, { "input": "10", "output": "Tyler" }, { "input": "11", "output": "Polk" }, { "input": "12", "output": "Taylor" }, { "input": "13", "output": "Fillmore" }, { "input": "14", "output": "Pierce" }, { "input": "15", "output": "Buchanan" }, { "input": "16", "output": "Lincoln" }, { "input": "17", "output": "Johnson" }, { "input": "18", "output": "Grant" }, { "input": "19", "output": "Hayes" }, { "input": "20", "output": "Garfield" }, { "input": "21", "output": "Arthur" }, { "input": "22", "output": "Cleveland" }, { "input": "23", "output": "Harrison" }, { "input": "24", "output": "Cleveland" }, { "input": "25", "output": "McKinley" }, { "input": "26", "output": "Roosevelt" }, { "input": "27", "output": "Taft" }, { "input": "28", "output": "Wilson" }, { "input": "30", "output": "Coolidge" }, { "input": "31", "output": "Hoover" }, { "input": "32", "output": "Roosevelt" }, { "input": "33", "output": "Truman" }, { "input": "34", "output": "Eisenhower" }, { "input": "35", "output": "Kennedy" }, { "input": "36", "output": "Johnson" }, { "input": "37", "output": "Nixon" }, { "input": "38", "output": "Ford" }, { "input": "39", "output": "Carter" }, { "input": "40", "output": "Reagan" } ]
0
0
-1
2,330
631
Interview
[ "brute force", "implementation" ]
null
null
Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem. We define function *f*(*x*,<=*l*,<=*r*) as a bitwise OR of integers *x**l*,<=*x**l*<=+<=1,<=...,<=*x**r*, where *x**i* is the *i*-th element of the array *x*. You are given two arrays *a* and *b* of length *n*. You need to determine the maximum value of sum *f*(*a*,<=*l*,<=*r*)<=+<=*f*(*b*,<=*l*,<=*r*) among all possible 1<=≀<=*l*<=≀<=*r*<=≀<=*n*.
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=1000)Β β€” the length of the arrays. The second line contains *n* integers *a**i* (0<=≀<=*a**i*<=≀<=109). The third line contains *n* integers *b**i* (0<=≀<=*b**i*<=≀<=109).
Print a single integerΒ β€” the maximum value of sum *f*(*a*,<=*l*,<=*r*)<=+<=*f*(*b*,<=*l*,<=*r*) among all possible 1<=≀<=*l*<=≀<=*r*<=≀<=*n*.
[ "5\n1 2 4 3 2\n2 3 3 12 1\n", "10\n13 2 7 11 8 4 9 8 5 1\n5 7 18 9 2 3 0 11 8 6\n" ]
[ "22", "46" ]
Bitwise OR of two non-negative integers *a* and *b* is the number *c* = *a* *OR* *b*, such that each of its digits in binary notation is 1 if and only if at least one of *a* or *b* have 1 in the corresponding position in binary notation. In the first sample, one of the optimal answers is *l* = 2 and *r* = 4, because *f*(*a*, 2, 4) + *f*(*b*, 2, 4) = (2 *OR* 4 *OR* 3) + (3 *OR* 3 *OR* 12) = 7 + 15 = 22. Other ways to get maximum value is to choose *l* = 1 and *r* = 4, *l* = 1 and *r* = 5, *l* = 2 and *r* = 4, *l* = 2 and *r* = 5, *l* = 3 and *r* = 4, or *l* = 3 and *r* = 5. In the second sample, the maximum value is obtained for *l* = 1 and *r* = 9.
[ { "input": "5\n1 2 4 3 2\n2 3 3 12 1", "output": "22" }, { "input": "10\n13 2 7 11 8 4 9 8 5 1\n5 7 18 9 2 3 0 11 8 6", "output": "46" }, { "input": "25\n12 30 38 109 81 124 80 33 38 48 29 78 96 48 96 27 80 77 102 65 80 113 31 118 35\n25 64 95 13 12 6 111 80 85 16 61 119 23 65 73 65 20 95 124 18 28 79 125 106 116", "output": "254" }, { "input": "20\n64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64\n64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64", "output": "128" }, { "input": "1\n1000000000\n1000000000", "output": "2000000000" }, { "input": "1\n0\n0", "output": "0" }, { "input": "2\n7 16\n16 7", "output": "46" }, { "input": "4\n6 0 0 0\n0 0 0 1", "output": "7" }, { "input": "8\n1 2 4 8 16 32 64 128\n1 2 4 8 16 32 64 128", "output": "510" }, { "input": "1\n2\n3", "output": "5" }, { "input": "1\n4\n3", "output": "7" }, { "input": "1\n1\n1", "output": "2" } ]
124
6,963,200
3
2,349
6
President's Office
[ "implementation" ]
B. President's Office
2
64
President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say, the two desks (President's and each deputy's) have a common side of a positive length. The office-room plan can be viewed as a matrix with *n* rows and *m* columns. Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The Β«periodΒ» character (Β«.Β») stands for an empty cell.
The first line contains two separated by a space integer numbers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=100) β€” the length and the width of the office-room, and *c* character β€” the President's desk colour. The following *n* lines contain *m* characters each β€” the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters.
Print the only number β€” the amount of President's deputies.
[ "3 4 R\nG.B.\n.RR.\nTTT.\n", "3 3 Z\n...\n.H.\n..Z\n" ]
[ "2\n", "0\n" ]
none
[ { "input": "3 4 R\nG.B.\n.RR.\nTTT.", "output": "2" }, { "input": "3 3 Z\n...\n.H.\n..Z", "output": "0" }, { "input": "1 1 C\nC", "output": "0" }, { "input": "2 2 W\nKW\nKW", "output": "1" }, { "input": "1 10 H\n....DDHHHH", "output": "1" }, { "input": "3 2 W\nOO\nWW\nWW", "output": "1" }, { "input": "3 3 U\nUOO\nUVV\nUVV", "output": "2" }, { "input": "4 5 Z\n...ZZ\nUU.ZZ\nUUTT.\n..TT.", "output": "1" }, { "input": "4 4 X\nT..R\nTJJJ\nDJJJ\nXJJJ", "output": "2" }, { "input": "5 5 O\nCQGAV\nIHTUD\nRFPZO\nMYSKX\nJEWBN", "output": "3" }, { "input": "5 4 O\n.O.J\nWOBJ\nWOBJ\nDDBJ\nDD.J", "output": "3" }, { "input": "7 7 Q\n....RRR\nUUUURRR\nUUUUSS.\n....SSB\nPPP.OIB\n.MMTTIB\nQQQTTIB", "output": "2" }, { "input": "8 10 B\n..BBBBEEEE\n..BBBBEEEE\n..BBBBEEEE\n..BBBBEEEE\nJJJJYYYY..\nJJJJYYYY..\nJJJJYYYY..\nJJJJYYYY..", "output": "3" }, { "input": "7 13 G\n....GGGGGGXXX\nSSSSGGGGGGXXX\nSSSSJJFFFFFFF\nRRR.JJFFFFFFF\nRRRYYYYYYYAAA\nRRRYYYYYYYAAA\nRRR.......AAA", "output": "4" }, { "input": "10 10 T\nCCEEEKKKHJ\nCCRRRRRRHJ\nCC..XFFOOO\nZZZZZFFOOO\n..PTTFFOOO\nAAATTFFOOO\nAAATTYYYYY\nAAATTYYYYY\nAAAMMYYYYY\nAAA..YYYYY", "output": "6" }, { "input": "15 12 M\n............\n.....L..QQQQ\nNNN..L..QQQQ\nNNN..LJJJJJ.\nNNNEEEEEEEE.\nNNNEEEEEEEE.\nNNNMMMMM.AAA\nNNNMMMMM.AAA\n.RRMMMMM.AAA\n.RRMMMMMYAAA\n.RRMMMMMYAAA\n.RRMMMMMYAAA\nZRRMMMMMYAAA\nZRRMMMMM.AAA\nZRRMMMMM.AAA", "output": "4" } ]
0
0
-1
2,355
459
Pashmak and Flowers
[ "combinatorics", "implementation", "sortings" ]
null
null
Pashmak decided to give Parmida a pair of flowers from the garden. There are *n* flowers in the garden and the *i*-th of them has a beauty number *b**i*. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible! Your task is to write a program which calculates two things: 1. The maximum beauty difference of flowers that Pashmak can give to Parmida. 1. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.
The first line of the input contains *n* (2<=≀<=*n*<=≀<=2Β·105). In the next line there are *n* space-separated integers *b*1, *b*2, ..., *b**n* (1<=≀<=*b**i*<=≀<=109).
The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.
[ "2\n1 2\n", "3\n1 4 5\n", "5\n3 1 2 3 1\n" ]
[ "1 1", "4 1", "2 4" ]
In the third sample the maximum beauty difference is 2 and there are 4 ways to do this: 1. choosing the first and the second flowers; 1. choosing the first and the fifth flowers; 1. choosing the fourth and the second flowers; 1. choosing the fourth and the fifth flowers.
[ { "input": "2\n1 2", "output": "1 1" }, { "input": "3\n1 4 5", "output": "4 1" }, { "input": "5\n3 1 2 3 1", "output": "2 4" }, { "input": "2\n1 1", "output": "0 1" }, { "input": "3\n1 1 1", "output": "0 3" }, { "input": "4\n1 1 1 1", "output": "0 6" }, { "input": "5\n1 1 1 1 1", "output": "0 10" }, { "input": "5\n2 2 2 2 2", "output": "0 10" }, { "input": "10\n2 2 2 2 2 2 2 2 2 2", "output": "0 45" }, { "input": "3\n2 2 2", "output": "0 3" }, { "input": "3\n3 3 3", "output": "0 3" }, { "input": "2\n10000000 100000000", "output": "90000000 1" }, { "input": "5\n5 5 5 5 5", "output": "0 10" }, { "input": "5\n3 3 3 3 3", "output": "0 10" }, { "input": "6\n1 1 1 1 1 1", "output": "0 15" }, { "input": "2\n5 6", "output": "1 1" }, { "input": "10\n1 1 1 1 1 1 1 1 1 1", "output": "0 45" }, { "input": "10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "0 45" }, { "input": "4\n4 4 4 4", "output": "0 6" }, { "input": "7\n1 1 1 1 1 1 1", "output": "0 21" }, { "input": "11\n1 1 1 1 1 1 1 1 1 1 1", "output": "0 55" }, { "input": "8\n8 8 8 8 8 8 8 8", "output": "0 28" }, { "input": "2\n3 2", "output": "1 1" } ]
171
16,588,800
0
2,366
873
Chores
[ "implementation" ]
null
null
Luba has to do *n* chores today. *i*-th chore takes *a**i* units of time to complete. It is guaranteed that for every the condition *a**i*<=β‰₯<=*a**i*<=-<=1 is met, so the sequence is sorted. Also Luba can work really hard on some chores. She can choose not more than *k* any chores and do each of them in *x* units of time instead of *a**i* (). Luba is very responsible, so she has to do all *n* chores, and now she wants to know the minimum time she needs to do everything. Luba cannot do two chores simultaneously.
The first line contains three integers *n*,<=*k*,<=*x*Β (1<=≀<=*k*<=≀<=*n*<=≀<=100,<=1<=≀<=*x*<=≀<=99) β€” the number of chores Luba has to do, the number of chores she can do in *x* units of time, and the number *x* itself. The second line contains *n* integer numbers *a**i*Β (2<=≀<=*a**i*<=≀<=100) β€” the time Luba has to spend to do *i*-th chore. It is guaranteed that , and for each *a**i*<=β‰₯<=*a**i*<=-<=1.
Print one number β€” minimum time Luba needs to do all *n* chores.
[ "4 2 2\n3 6 7 10\n", "5 2 1\n100 100 100 100 100\n" ]
[ "13\n", "302\n" ]
In the first example the best option would be to do the third and the fourth chore, spending *x* = 2 time on each instead of *a*<sub class="lower-index">3</sub> and *a*<sub class="lower-index">4</sub>, respectively. Then the answer is 3 + 6 + 2 + 2 = 13. In the second example Luba can choose any two chores to spend *x* time on them instead of *a*<sub class="lower-index">*i*</sub>. So the answer is 100Β·3 + 2Β·1 = 302.
[ { "input": "4 2 2\n3 6 7 10", "output": "13" }, { "input": "5 2 1\n100 100 100 100 100", "output": "302" }, { "input": "1 1 1\n100", "output": "1" }, { "input": "100 1 99\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "9999" }, { "input": "100 100 1\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "100" }, { "input": "100 50 50\n51 51 52 53 55 55 55 55 56 56 56 57 57 58 58 59 59 59 60 60 61 61 62 62 63 64 64 64 64 65 65 65 65 66 66 66 67 68 68 68 69 69 70 70 70 70 71 71 71 71 71 71 72 72 76 76 76 76 77 79 79 81 81 81 81 82 82 82 82 83 84 85 86 87 87 88 88 88 89 89 89 90 90 90 91 91 91 92 92 93 95 95 96 96 96 97 97 98 99 100", "output": "5618" }, { "input": "100 100 1\n2 4 4 4 5 5 5 6 10 10 11 11 12 12 13 13 13 14 17 18 20 20 21 21 22 22 23 24 24 25 26 29 29 32 32 34 34 35 38 39 39 40 40 42 42 43 45 47 48 49 51 52 52 54 57 59 59 60 61 61 62 63 63 64 65 65 68 70 70 72 74 75 75 76 76 77 77 78 78 78 79 80 81 82 82 83 83 83 84 89 90 92 92 93 94 96 96 97 98 99", "output": "100" }, { "input": "100 1 1\n3 3 5 7 8 8 8 9 9 9 11 13 14 15 18 18 19 20 21 22 22 25 27 27 29 31 32 33 33 34 36 37 37 38 40 42 44 44 46 47 47 48 48 48 50 50 51 51 54 54 54 55 55 56 56 56 60 61 62 62 63 64 65 65 68 70 70 71 71 71 71 75 75 76 76 79 79 79 79 81 81 82 82 86 86 86 86 88 90 90 92 96 97 97 98 98 98 98 100 100", "output": "5202" }, { "input": "100 50 49\n50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51", "output": "4950" }, { "input": "100 50 1\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3", "output": "151" }, { "input": "100 1 1\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2", "output": "199" } ]
93
0
3
2,376
551
ZgukistringZ
[ "brute force", "constructive algorithms", "implementation", "strings" ]
null
null
Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one. GukiZ has strings *a*, *b*, and *c*. He wants to obtain string *k* by swapping some letters in *a*, so that *k* should contain as many non-overlapping substrings equal either to *b* or *c* as possible. Substring of string *x* is a string formed by consecutive segment of characters from *x*. Two substrings of string *x* overlap if there is position *i* in string *x* occupied by both of them. GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings *k*?
The first line contains string *a*, the second line contains string *b*, and the third line contains string *c* (1<=≀<=|*a*|,<=|*b*|,<=|*c*|<=≀<=105, where |*s*| denotes the length of string *s*). All three strings consist only of lowercase English letters. It is possible that *b* and *c* coincide.
Find one of possible strings *k*, as described in the problem statement. If there are multiple possible answers, print any of them.
[ "aaa\na\nb\n", "pozdravstaklenidodiri\nniste\ndobri\n", "abbbaaccca\nab\naca\n" ]
[ "aaa", "nisteaadddiiklooprrvz", "ababacabcc" ]
In the third sample, this optimal solutions has three non-overlaping substrings equal to either *b* or *c* on positions 1 – 2 (*ab*), 3 – 4 (*ab*), 5 – 7 (*aca*). In this sample, there exist many other optimal solutions, one of them would be *acaababbcc*.
[ { "input": "aaa\na\nb", "output": "aaa" }, { "input": "pozdravstaklenidodiri\nniste\ndobri", "output": "nisteaadddiiklooprrvz" }, { "input": "abbbaaccca\nab\naca", "output": "ababacabcc" }, { "input": "lemigazalemiolemilicomzalemljenje\nlemi\nzlo", "output": "lemilemilemilemizlozloaaaceegjjmn" }, { "input": "xxxbbbcccoca\nca\ncb", "output": "cacbcbcboxxx" }, { "input": "aleksandrehteosidatedodam\nevo\nsi", "output": "siaaaaddddeeeehklmnoorstt" }, { "input": "cumurcumur\num\ncur", "output": "umumcurcur" }, { "input": "saljivdzijasamjaneki\nneki\nja", "output": "nekijajajaadiilmssvz" }, { "input": "lebronnojameslebronprogrammers\nje\nbro", "output": "jebrobroaaeeegllmmmnnnooprrrss" }, { "input": "lukavpastaakojelukav\na\nu", "output": "aaaaauuejkkkllopstvv" }, { "input": "navijamzaradnickiastabidrugo\ndruzina\ndjavola", "output": "druzinaaaaaabcdgiiijkmnorstv" }, { "input": "zlobobermyfriendandthanksforhelp\nde\nfor", "output": "dedeforforaabbehhikllmnnnoprstyz" }, { "input": "randomusername\numno\numno", "output": "umnoaadeemnrrs" }, { "input": "aaaaaabababaaa\naa\na", "output": "aaaaaaaaaaabbb" }, { "input": "balsabratepozdravimajudevojku\noj\nzdrav", "output": "ojojzdravaaaabbdeeiklmprstuuv" }, { "input": "milenicnikolaitisideotakmicenja\nelem\nnik", "output": "elemniknikaaaccdeiiiiijlmnoostt" }, { "input": "touristyouaregreatguy\ntourist\nguy", "output": "touristguyguyaaeeorrt" }, { "input": "oduleodule\nxgrizx\nivanstosicprvi", "output": "ddeelloouu" }, { "input": "damandicnenapravicheckerzeznulibise\nman\nker", "output": "mankeraaabcccddeeeehiiiilnnnprsuvzz" }, { "input": "jankosustersicneceovoraditi\ncosovic\noce", "output": "oceoceaadeiiijknnorrsssttuv" }, { "input": "princeofpersiayouhavegreatcontestbutinwrongtime\nop\npera", "output": "peraperaabcceeeefgghiiiimnnnnoooorrsstttttuuvwy" }, { "input": "gukimikazedauradimseminarskidodatnohumorhumor\ndp\nmrzime", "output": "mrzimeaaaaaddddeghhiiiikkkmmmnnoooorrrsstuuuu" }, { "input": "duxidimkeetoivas\ndd\nodi", "output": "odiadeeiikmstuvx" }, { "input": "svetislavgajicpoznatijikaosvetaxxx\nslavi\nslavu", "output": "slaviaaaaceegiiijjknoopsstttvvxxxz" }, { "input": "djeneralmilomirstefanovic\nradi\nnesto", "output": "radinestoaceefiijllmmnorv" }, { "input": "pozdravizazenskudecunecuvasodvajatidaseneprotumacipogresno\ncao\ndeco", "output": "decodecodecoaaaaaaadeeegiiijkmnnnnooppprrrssssttuuuuvvvzzz" }, { "input": "thisiscornercase\nyouhavetwolongerstrings\nibelivethatyoudontmissit", "output": "acceehiinorrssst" }, { "input": "petryouaregoodandyouhavegoodblogs\nblog\nrega", "output": "blogregaregaadddehnoooooopstuuvyy" }, { "input": "ikatanictisinajboljiuhrvatskojakoprictasovojaviseakotijedosadno\njavise\nsine", "output": "sinesineaaaaaaaaabccddhiiiiiijjjjjkkkklnooooooooprrssstttttuvvv" }, { "input": "ikbalturkeybelieveinyou\nbal\nkan", "output": "kanbbeeeeiiikllortuuvyy" }, { "input": "egoryouaregoodbutcantsolveeverythinginonehour\neat\nyour", "output": "eateatyouryourbcdeeeeggghhiilnnnnooooorrstuvv" }, { "input": "pozdravzamojeodeljenjeiprofesoreocudabudempetnula\nbojan\ncao", "output": "bojancaoaaddddeeeeeeeefijjllmmnooooppprrrstuuuvzz" }, { "input": "pozdravizamarkamatovicaaleksandracveticainenadaslagalicustanisica\nvas\nrad", "output": "vasvasvasradradradaaaaaaaaaaccccceeegiiiiiiikklllmmnnnnoopstttuzz" }, { "input": "hellodeninobrdo\nod\nhel", "output": "ododhelbeilnnor" }, { "input": "iwanttothanktomygrandmaheisveryimportantpersoninmylife\nthanks\nstanka", "output": "stankaaaadeeeefghhiiiiilmmmmnnnnnoooopprrrrstttttvwyyy" }, { "input": "molimprofesorkuengleskogdamidapetjasamdobarcovekitrudimseiztogaiakosamoperisan\nhvala\nunapred", "output": "unapredunapredaaaaaaabcddeeeeefgggiiiiiiijkkkkllmmmmmmoooooooooprrrsssssstttvz" }, { "input": "razredninjegosgrebovicdobarcoveklosbasketas\nne\ngo", "output": "nenegogoaaaabbbccddeeeiijkklooorrrrsssstvvz" }, { "input": "goodbyeihopecontestisntsohar\noh\ngod", "output": "ohohgodabceeeiinnooprsssttty" }, { "input": "zdule\ndidins\nmeinkraft", "output": "deluz" }, { "input": "dreamoonhasonedream\nno\nno", "output": "nonoaaaddeeehmmorrs" }, { "input": "brtakoktrosttttttttttosafasfkalsfkodfdasiofhadfhasdsajfdsafoasodsafahaihfdisoadspapsapiosapdsajdipsahdhasuirhaeuifhhfkjgosooooooooodafdfioottttafdsafaddfuiasdjfjasdo\nokat\ntako", "output": "takotakotakotakotakoaaaaaaaaaaaaaaaaaaaaaabddddddddddddddddddeffffffffffffffffffghhhhhhhhhiiiiiiiiijjjjjloooooooooooooooooppppprrrssssssssssssssssssssstttttttttttuuu" }, { "input": "bumbumdzejsikerol\nbumbum\nbum", "output": "bumbumdeeijklorsz" }, { "input": "mztskopjetisisampiosrcenaterenostaviajdezanaspobedi\nmzt\noptee", "output": "mztopteeopteeopteeaaaaaabcddiiiiijjkmnnnorrssssssvz" } ]
1,247
268,390,400
0
2,377
177
Script Generation
[]
null
null
The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are *n* single men and *n* single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as *k* triples of numbers (*h*,<=*w*,<=*r*), where *h* is the index of the man, *w* is the index of the woman, and *r* is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot. The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the *k* marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set. Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value β€” it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the *t*-th set from the sorted list. Thus, *t*<==<=1 corresponds to a plot without marriages, *t*<==<=2 β€” to a single marriage resulting in minimal delight for the audience, and so on. Help the Beaver to implement the algorithm for selecting the desired set.
The first input line contains integers *n*, *k* and *t* (1<=≀<=*k*<=≀<=*min*(100,<=*n*2), 1<=≀<=*t*<=≀<=2Β·105), separated by single spaces. Next *k* lines contain triples of integers (*h*,<=*w*,<=*r*) (1<=≀<=*h*,<=*w*<=≀<=*n*;Β 1<=≀<=*r*<=≀<=1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: *t* doesn't exceed the total number of acceptable sets, and each pair (*h*,<=*w*) is present in at most one triple. The input limitations for getting 30 points are: - 1<=≀<=*n*<=≀<=5 The input limitations for getting 100 points are: - 1<=≀<=*n*<=≀<=20
Print a single number β€” the value of the *t*-th acceptable variant.
[ "2 4 3\n1 1 1\n1 2 2\n2 1 3\n2 2 7\n", "2 4 7\n1 1 1\n1 2 2\n2 1 3\n2 2 7\n" ]
[ "2\n", "8\n" ]
The figure shows 7 acceptable sets of marriages that exist in the first sample.
[ { "input": "2 4 3\n1 1 1\n1 2 2\n2 1 3\n2 2 7", "output": "2" }, { "input": "2 4 7\n1 1 1\n1 2 2\n2 1 3\n2 2 7", "output": "8" }, { "input": "2 2 1\n1 2 8\n2 2 1", "output": "0" }, { "input": "5 25 140\n3 5 40\n3 3 42\n4 5 62\n2 4 7\n4 2 57\n1 5 69\n3 2 37\n2 5 43\n2 3 14\n1 3 67\n5 2 62\n3 1 13\n5 5 55\n1 2 71\n4 1 69\n1 4 32\n4 4 58\n5 3 2\n2 2 31\n5 1 20\n2 1 38\n1 1 69\n5 4 66\n3 4 27\n4 3 90", "output": "80" }, { "input": "3 7 8\n1 1 4\n2 2 14\n2 1 26\n3 2 12\n2 3 1\n1 3 6\n3 3 16", "output": "14" }, { "input": "3 9 21\n3 2 40\n1 3 39\n3 1 18\n1 2 34\n2 1 27\n1 1 12\n2 2 4\n3 3 7\n2 3 16", "output": "50" }, { "input": "3 9 34\n3 2 37\n3 1 16\n1 2 1\n1 3 2\n2 2 23\n2 3 34\n2 1 2\n3 3 1\n1 1 23", "output": "94" }, { "input": "4 11 61\n3 1 39\n4 1 14\n2 3 38\n2 2 24\n2 1 4\n3 4 18\n3 2 16\n4 3 40\n4 2 10\n2 4 24\n1 1 3", "output": "69" }, { "input": "4 14 110\n3 2 27\n4 1 49\n3 1 36\n1 3 39\n3 3 23\n1 2 8\n2 2 16\n4 4 7\n1 1 36\n2 3 5\n2 4 37\n2 1 29\n1 4 44\n3 4 14", "output": "85" }, { "input": "4 16 105\n2 4 15\n1 1 16\n2 2 57\n3 4 31\n1 2 47\n2 3 28\n1 3 70\n4 2 50\n3 1 10\n4 1 11\n4 4 27\n1 4 56\n3 3 28\n3 2 28\n2 1 33\n4 3 63", "output": "94" }, { "input": "5 15 90\n2 3 71\n5 1 72\n3 2 29\n2 5 35\n5 4 49\n2 1 5\n3 3 37\n5 2 3\n1 1 24\n1 3 50\n5 3 45\n2 2 31\n4 3 71\n3 1 30\n2 4 18", "output": "95" }, { "input": "5 20 110\n1 4 29\n2 3 87\n1 1 19\n5 1 56\n3 5 71\n4 5 60\n5 3 10\n1 3 35\n1 5 29\n1 2 28\n2 5 33\n5 2 21\n5 5 61\n3 1 26\n3 2 70\n2 4 10\n4 1 16\n3 3 78\n5 4 30\n3 4 83", "output": "78" } ]
124
0
3
2,379
455
Civilization
[ "dfs and similar", "dp", "dsu", "ternary search", "trees" ]
null
null
Andrew plays a game called "Civilization". Dima helps him. The game has *n* cities and *m* bidirectional roads. The cities are numbered from 1 to *n*. Between any pair of cities there either is a single (unique) path, or there is no path at all. A path is such a sequence of distinct cities *v*1,<=*v*2,<=...,<=*v**k*, that there is a road between any contiguous cities *v**i* and *v**i*<=+<=1 (1<=≀<=*i*<=&lt;<=*k*). The length of the described path equals to (*k*<=-<=1). We assume that two cities lie in the same region if and only if, there is a path connecting these two cities. During the game events of two types take place: 1. Andrew asks Dima about the length of the longest path in the region where city *x* lies. 1. Andrew asks Dima to merge the region where city *x* lies with the region where city *y* lies. If the cities lie in the same region, then no merging is needed. Otherwise, you need to merge the regions as follows: choose a city from the first region, a city from the second region and connect them by a road so as to minimize the length of the longest path in the resulting region. If there are multiple ways to do so, you are allowed to choose any of them. Dima finds it hard to execute Andrew's queries, so he asks you to help him. Help Dima.
The first line contains three integers *n*, *m*, *q* (1<=≀<=*n*<=≀<=3Β·105; 0<=≀<=*m*<=&lt;<=*n*; 1<=≀<=*q*<=≀<=3Β·105) β€” the number of cities, the number of the roads we already have and the number of queries, correspondingly. Each of the following *m* lines contains two integers, *a**i* and *b**i* (*a**i*<=β‰ <=*b**i*; 1<=≀<=*a**i*,<=*b**i*<=≀<=*n*). These numbers represent the road between cities *a**i* and *b**i*. There can be at most one road between two cities. Each of the following *q* lines contains one of the two events in the following format: - 1 *x**i*. It is the request Andrew gives to Dima to find the length of the maximum path in the region that contains city *x**i* (1<=≀<=*x**i*<=≀<=*n*). 2 *x**i* *y**i*. It is the request Andrew gives to Dima to merge the region that contains city *x**i* and the region that contains city *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*). Note, that *x**i* can be equal to *y**i*.
For each event of the first type print the answer on a separate line.
[ "6 0 6\n2 1 2\n2 3 4\n2 5 6\n2 3 2\n2 5 3\n1 1\n" ]
[ "4\n" ]
none
[]
982
268,390,400
0
2,389
796
Find The Bone
[ "implementation" ]
null
null
Zane the wizard is going to perform a magic show shuffling the cups. There are *n* cups, numbered from 1 to *n*, placed along the *x*-axis on a table that has *m* holes on it. More precisely, cup *i* is on the table at the position *x*<==<=*i*. The problematic bone is initially at the position *x*<==<=1. Zane will confuse the audience by swapping the cups *k* times, the *i*-th time of which involves the cups at the positions *x*<==<=*u**i* and *x*<==<=*v**i*. If the bone happens to be at the position where there is a hole at any time, it will fall into the hole onto the ground and will not be affected by future swapping operations. Do not forget that Zane is a wizard. When he swaps the cups, he does not move them ordinarily. Instead, he teleports the cups (along with the bone, if it is inside) to the intended positions. Therefore, for example, when he swaps the cup at *x*<==<=4 and the one at *x*<==<=6, they will not be at the position *x*<==<=5 at any moment during the operation. Zane’s puppy, Inzane, is in trouble. Zane is away on his vacation, and Inzane cannot find his beloved bone, as it would be too exhausting to try opening all the cups. Inzane knows that the Codeforces community has successfully helped Zane, so he wants to see if it could help him solve his problem too. Help Inzane determine the final position of the bone.
The first line contains three integers *n*, *m*, and *k* (2<=≀<=*n*<=≀<=106, 1<=≀<=*m*<=≀<=*n*, 1<=≀<=*k*<=≀<=3Β·105)Β β€” the number of cups, the number of holes on the table, and the number of swapping operations, respectively. The second line contains *m* distinct integers *h*1,<=*h*2,<=...,<=*h**m* (1<=≀<=*h**i*<=≀<=*n*)Β β€” the positions along the *x*-axis where there is a hole on the table. Each of the next *k* lines contains two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*)Β β€” the positions of the cups to be swapped.
Print one integerΒ β€” the final position along the *x*-axis of the bone.
[ "7 3 4\n3 4 6\n1 2\n2 5\n5 7\n7 1\n", "5 1 2\n2\n1 2\n2 4\n" ]
[ "1", "2" ]
In the first sample, after the operations, the bone becomes at *x* = 2, *x* = 5, *x* = 7, and *x* = 1, respectively. In the second sample, after the first operation, the bone becomes at *x* = 2, and falls into the hole onto the ground.
[ { "input": "7 3 4\n3 4 6\n1 2\n2 5\n5 7\n7 1", "output": "1" }, { "input": "5 1 2\n2\n1 2\n2 4", "output": "2" }, { "input": "10000 1 9\n55\n44 1\n2929 9292\n9999 9998\n44 55\n49 94\n55 53\n100 199\n55 50\n53 11", "output": "55" }, { "input": "100000 3 7\n2 3 4\n1 5\n5 1\n1 5\n5 1\n1 4\n4 3\n3 2", "output": "4" }, { "input": "1000000 9 11\n38 59 999999 199 283 4849 1000000 2 554\n39 94\n3 9\n1 39\n39 40\n40 292\n5399 5858\n292 49949\n49949 222\n222 38\n202 9494\n38 59", "output": "38" }, { "input": "1000000 11 9\n19 28 39 82 99 929384 8298 892849 202020 777777 123123\n19 28\n28 39\n1 123124\n39 28\n28 99\n99 8298\n123124 123122\n2300 3200\n8298 1000000", "output": "123122" }, { "input": "2 1 1\n1\n1 2", "output": "1" }, { "input": "7 3 6\n1 4 5\n1 2\n2 3\n3 5\n4 5\n4 5\n4 5", "output": "1" }, { "input": "10 3 8\n1 5 10\n1 2\n2 3\n3 4\n3 4\n3 4\n4 5\n5 6\n6 5", "output": "1" }, { "input": "5 2 9\n2 4\n1 3\n3 5\n3 5\n3 4\n4 2\n2 4\n1 4\n1 2\n1 4", "output": "4" }, { "input": "10 10 13\n1 2 3 4 5 6 7 8 9 10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n6 7\n6 10\n10 9\n9 1\n1 10\n1 10\n1 10", "output": "1" }, { "input": "3 3 3\n1 2 3\n1 2\n2 3\n3 2", "output": "1" }, { "input": "100 7 7\n17 27 37 47 57 67 77\n49 39\n55 1\n50 3\n89 1\n1 99\n100 55\n98 55", "output": "100" }, { "input": "9 1 9\n9\n1 2\n3 2\n4 3\n8 9\n4 5\n7 4\n8 5\n1 3\n3 2", "output": "8" }, { "input": "300000 1 1\n200000\n300000 1", "output": "300000" }, { "input": "203948 2 14\n203948 203946\n39 38\n4959 3030\n1 203947\n2929 9292\n203944 203948\n203947 203944\n203944 203922\n203922 203948\n2495 20495\n29419 5959\n12949 12\n49 29292\n1 94\n1 203", "output": "203948" }, { "input": "203948 2 14\n203948 203947\n39 38\n4959 3030\n1 203947\n2929 9292\n203944 203948\n203947 203944\n203944 203922\n203922 203948\n2495 20495\n29419 5959\n12949 12\n49 29292\n1 94\n1 203", "output": "203947" }, { "input": "100 2 5\n1 2\n2 39\n39 29\n99 100\n1 2\n2 39", "output": "1" }, { "input": "3 1 1\n1\n1 2", "output": "1" }, { "input": "5 2 2\n1 2\n1 2\n2 3", "output": "1" }, { "input": "2 2 1\n1 2\n2 1", "output": "1" }, { "input": "5 2 1\n1 2\n2 1", "output": "1" }, { "input": "5 1 1\n5\n3 4", "output": "1" }, { "input": "3 2 1\n1 2\n2 1", "output": "1" }, { "input": "5 1 2\n2\n2 1\n2 3", "output": "2" }, { "input": "3 1 2\n2\n2 1\n2 3", "output": "2" }, { "input": "3 2 2\n2 3\n2 1\n2 3", "output": "2" }, { "input": "4 2 1\n1 2\n2 1", "output": "1" }, { "input": "4 1 1\n2\n2 3", "output": "1" }, { "input": "3 2 1\n1 3\n3 1", "output": "1" }, { "input": "10 1 3\n2\n2 1\n2 4\n9 10", "output": "2" }, { "input": "5 2 4\n3 5\n1 2\n4 2\n3 4\n3 5", "output": "3" }, { "input": "4 3 1\n1 2 3\n2 1", "output": "1" } ]
2,000
20,377,600
0
2,390
591
Rebranding
[ "implementation", "strings" ]
null
null
The name of one small but proud corporation consists of *n* lowercase English letters. The Corporation has decided to try rebrandingΒ β€” an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name. For this purpose the corporation has consecutively hired *m* designers. Once a company hires the *i*-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters *x**i* by *y**i*, and all the letters *y**i* by *x**i*. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that *x**i* coincides with *y**i*. The version of the name received after the work of the last designer becomes the new name of the corporation. Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive. Satisfy Arkady's curiosity and tell him the final version of the name.
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=200<=000)Β β€” the length of the initial name and the number of designers hired, respectively. The second line consists of *n* lowercase English letters and represents the original name of the corporation. Next *m* lines contain the descriptions of the designers' actions: the *i*-th of them contains two space-separated lowercase English letters *x**i* and *y**i*.
Print the new name of the corporation.
[ "6 1\npolice\np m\n", "11 6\nabacabadaba\na b\nb c\na d\ne g\nf a\nb b\n" ]
[ "molice\n", "cdcbcdcfcdc\n" ]
In the second sample the name of the corporation consecutively changes as follows: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c7648432f7138ca53234357d7e08d1d119166055.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/de89ad7bc7f27c46ec34f5e66ce0dc23bd5bc90a.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/812e653c8d7ff496e6a0f04c676423806751531e.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/19c564fcefb8dde36256240a8b877bb6a4792bfe.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e1cafd93792430ad1a49e893e04715383bdae757.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "6 1\npolice\np m", "output": "molice" }, { "input": "11 6\nabacabadaba\na b\nb c\na d\ne g\nf a\nb b", "output": "cdcbcdcfcdc" }, { "input": "1 1\nf\nz h", "output": "f" }, { "input": "1 1\na\na b", "output": "b" }, { "input": "10 10\nlellelleel\ne l\ne l\ne l\ne l\ne l\ne e\nl l\nl e\nl l\ne e", "output": "lellelleel" } ]
1,029
93,798,400
-1
2,394
820
Mister B and Angle in Polygon
[ "constructive algorithms", "geometry", "math" ]
null
null
On one quiet day all of sudden Mister B decided to draw angle *a* on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex *n*-gon (regular convex polygon with *n* sides). That's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices *v*1, *v*2, *v*3 such that the angle (where *v*2 is the vertex of the angle, and *v*1 and *v*3 lie on its sides) is as close as possible to *a*. In other words, the value should be minimum possible. If there are many optimal solutions, Mister B should be satisfied with any of them.
First and only line contains two space-separated integers *n* and *a* (3<=≀<=*n*<=≀<=105, 1<=≀<=*a*<=≀<=180)Β β€” the number of vertices in the polygon and the needed angle, in degrees.
Print three space-separated integers: the vertices *v*1, *v*2, *v*3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to *n* in clockwise order.
[ "3 15\n", "4 67\n", "4 68\n" ]
[ "1 2 3\n", "2 1 3\n", "4 1 2\n" ]
In first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct. Vertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since |45 - 67| &lt; |90 - 67|. Other correct answers are: "3 1 2", "3 2 4", "4 2 3", "4 3 1", "1 3 4", "1 4 2", "2 4 1", "4 1 3", "3 1 4", "3 4 2", "2 4 3", "2 3 1", "1 3 2", "1 2 4", "4 2 1". In third sample test, on the contrary, the angle of 90 degrees was chosen, since |90 - 68| &lt; |45 - 68|. Other correct answers are: "2 1 4", "3 2 1", "1 2 3", "4 3 2", "2 3 4", "1 4 3", "3 4 1".
[ { "input": "3 15", "output": "2 1 3" }, { "input": "4 67", "output": "2 1 3" }, { "input": "4 68", "output": "2 1 4" }, { "input": "3 1", "output": "2 1 3" }, { "input": "3 180", "output": "2 1 3" }, { "input": "100000 1", "output": "2 1 558" }, { "input": "100000 180", "output": "2 1 100000" }, { "input": "100000 42", "output": "2 1 23335" }, { "input": "100000 123", "output": "2 1 68335" }, { "input": "5 1", "output": "2 1 3" }, { "input": "5 36", "output": "2 1 3" }, { "input": "5 54", "output": "2 1 3" }, { "input": "5 55", "output": "2 1 4" }, { "input": "5 70", "output": "2 1 4" }, { "input": "5 89", "output": "2 1 4" }, { "input": "5 90", "output": "2 1 4" }, { "input": "5 91", "output": "2 1 5" }, { "input": "5 111", "output": "2 1 5" }, { "input": "5 126", "output": "2 1 5" }, { "input": "5 127", "output": "2 1 5" }, { "input": "5 141", "output": "2 1 5" }, { "input": "5 162", "output": "2 1 5" }, { "input": "5 180", "output": "2 1 5" }, { "input": "6 46", "output": "2 1 4" }, { "input": "6 33", "output": "2 1 3" }, { "input": "13 4", "output": "2 1 3" }, { "input": "23 11", "output": "2 1 3" }, { "input": "11 119", "output": "2 1 9" }, { "input": "13 117", "output": "2 1 10" }, { "input": "18 174", "output": "2 1 18" }, { "input": "8509 139", "output": "2 1 6573" }, { "input": "29770 76", "output": "2 1 12572" }, { "input": "59115 40", "output": "2 1 13139" }, { "input": "68459 88", "output": "2 1 33471" }, { "input": "85100 129", "output": "2 1 60990" }, { "input": "100000 13", "output": "2 1 7224" }, { "input": "100000 35", "output": "2 1 19446" }, { "input": "100000 49", "output": "2 1 27224" }, { "input": "100000 71", "output": "2 1 39446" }, { "input": "100000 79", "output": "2 1 43891" }, { "input": "100000 101", "output": "2 1 56113" }, { "input": "100000 109", "output": "2 1 60558" }, { "input": "100000 143", "output": "2 1 79446" }, { "input": "100000 148", "output": "2 1 82224" }, { "input": "100000 176", "output": "2 1 97780" }, { "input": "4 16", "output": "2 1 3" } ]
93
23,142,400
0
2,397
44
Phone Number
[ "dp" ]
H. Phone Number
2
256
Alas, finding one's true love is not easy. Masha has been unsuccessful in that yet. Her friend Dasha told Masha about a way to determine the phone number of one's Prince Charming through arithmancy. The phone number is divined like that. First one needs to write down one's own phone numbers. For example, let's suppose that Masha's phone number is 12345. After that one should write her favorite digit from 0 to 9 under the first digit of her number. That will be the first digit of the needed number. For example, Masha's favorite digit is 9. The second digit is determined as a half sum of the second digit of Masha's number and the already written down first digit from her beloved one's number. In this case the arithmetic average equals to (2<=+<=9)<=/<=2<==<=5.5. Masha can round the number up or down, depending on her wishes. For example, she chooses the digit 5. Having written down the resulting digit under the second digit of her number, Masha moves to finding the third digit in the same way, i.e. finding the half sum the the third digit of her number and the second digit of the new number. The result is (5<=+<=3)<=/<=2<==<=4. In this case the answer is unique. Thus, every *i*-th digit is determined as an arithmetic average of the *i*-th digit of Masha's number and the *i*<=-<=1-th digit of her true love's number. If needed, the digit can be rounded up or down. For example, Masha can get:
The first line contains nonempty sequence consisting of digits from 0 to 9 β€” Masha's phone number. The sequence length does not exceed 50.
Output the single number β€” the number of phone numbers Masha will dial.
[ "12345\n", "09\n" ]
[ "48\n", "15\n" ]
none
[ { "input": "12345", "output": "48" }, { "input": "09", "output": "15" }, { "input": "3", "output": "9" }, { "input": "55", "output": "14" }, { "input": "737", "output": "23" }, { "input": "21583", "output": "55" }, { "input": "33408349", "output": "133" }, { "input": "0180990956", "output": "473" }, { "input": "433488906230138", "output": "1399" }, { "input": "00046142930690780976", "output": "35257" }, { "input": "317579445234107659439645596", "output": "145866" }, { "input": "95066916647678224147260013920", "output": "446529" }, { "input": "36460576924876475371008334246121610", "output": "31319157" }, { "input": "429622625617508557672595893160462042433748844995", "output": "284175107" }, { "input": "17601120900014764776764048700928872725171605903217", "output": "10428170619" }, { "input": "39884857105160870767160905699169880375621726152715", "output": "244663375" }, { "input": "52056884218028089650567882557609167736461846591193", "output": "1358962463" }, { "input": "74239501210975375541963549337949373386030687741681", "output": "3422420940" }, { "input": "96591550315931484452350406227169651758570705180260", "output": "6869183484" }, { "input": "10764487327809690332754482187409867297140746339768", "output": "3435387051" }, { "input": "44444444444444444444444444444444444444444444444444", "output": "631" }, { "input": "9876543210", "output": "157" }, { "input": "23321232101010000101232344554334", "output": "5316368" }, { "input": "3232345665654567888878887898999998788766654567878", "output": "2520209072" }, { "input": "78776656654555655544443212222101121000000000100000", "output": "164642009" }, { "input": "78767765544454334445445555455676565433343455432332", "output": "11031574582" }, { "input": "67676566654565654332111011212211111223433222110012", "output": "5882859948" } ]
122
0
3.9695
2,398