contestId
int64
0
1.01k
name
stringlengths
2
58
tags
listlengths
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
listlengths
0
7
demo-output
listlengths
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
451
Predict Outcome of the Game
[ "brute force", "implementation", "math" ]
null
null
There are *n* games in a football tournament. Three teams are participating in it. Currently *k* games had already been played. You are an avid football fan, but recently you missed the whole *k* games. Fortunately, you remember a guess of your friend for these *k* games. Your friend did not tell exact number of wins...
The first line of the input contains a single integer corresponding to number of test cases *t* (1<=≀<=*t*<=≀<=105). Each of the next *t* lines will contain four space-separated integers *n*,<=*k*,<=*d*1,<=*d*2 (1<=≀<=*n*<=≀<=1012;Β 0<=≀<=*k*<=≀<=*n*;Β 0<=≀<=*d*1,<=*d*2<=≀<=*k*) β€” data for the current test case.
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
[ "5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 3 3 0\n3 3 3 2\n" ]
[ "yes\nyes\nyes\nno\nno\n" ]
Sample 1. There has not been any match up to now (*k* = 0, *d*<sub class="lower-index">1</sub> = 0, *d*<sub class="lower-index">2</sub> = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win. Sample 2. You missed all the games (*k* = 3). As *d*<sub class...
[ { "input": "5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 3 3 0\n3 3 3 2", "output": "yes\nyes\nyes\nno\nno" } ]
61
2,867,200
-1
7,605
0
none
[ "none" ]
null
null
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: 1. a tick...
The first line of input contains integer number *n* (1<=≀<=*n*<=≀<=105)Β β€” the number of trips made by passenger. Each of the following *n* lines contains the time of trip *t**i* (0<=≀<=*t**i*<=≀<=109), measured in minutes from the time of starting the system. All *t**i* are different, given in ascending order, i.Β e. *...
Output *n* integers. For each trip, print the sum the passenger is charged after it.
[ "3\n10\n20\n30\n", "10\n13\n45\n46\n60\n103\n115\n126\n150\n256\n516\n" ]
[ "20\n20\n10\n", "20\n20\n10\n0\n20\n0\n0\n20\n20\n10\n" ]
In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had alre...
[ { "input": "3\n10\n20\n30", "output": "20\n20\n10" }, { "input": "10\n13\n45\n46\n60\n103\n115\n126\n150\n256\n516", "output": "20\n20\n10\n0\n20\n0\n0\n20\n20\n10" }, { "input": "7\n100\n138\n279\n308\n396\n412\n821", "output": "20\n20\n20\n20\n20\n20\n0" }, { "input": "8\n0...
2,000
9,523,200
0
7,612
609
Load Balancing
[ "implementation", "math" ]
null
null
In the school computer room there are *n* servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are *m**i* tasks assigned to the *i*-th server. In order to balance the load for each server, you want to reassign some tasks to make the differe...
The first line contains positive number *n* (1<=≀<=*n*<=≀<=105) β€” the number of the servers. The second line contains the sequence of non-negative integers *m*1,<=*m*2,<=...,<=*m**n* (0<=≀<=*m**i*<=≀<=2Β·104), where *m**i* is the number of tasks assigned to the *i*-th server.
Print the minimum number of seconds required to balance the load.
[ "2\n1 6\n", "7\n10 11 10 11 10 11 11\n", "5\n1 2 3 4 5\n" ]
[ "2\n", "0\n", "3\n" ]
In the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2. In the second example the load is already balanced. A possible sequence of task movements for the third example is: ...
[ { "input": "2\n1 6", "output": "2" }, { "input": "7\n10 11 10 11 10 11 11", "output": "0" }, { "input": "5\n1 2 3 4 5", "output": "3" }, { "input": "10\n0 0 0 0 0 0 0 0 0 0", "output": "0" }, { "input": "1\n0", "output": "0" }, { "input": "1\n20000", ...
109
0
0
7,626
252
Unsorting Array
[ "brute force", "sortings" ]
null
null
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of *n* elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct ...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105). The second line contains *n* non-negative space-separated integers *a*1,<=*a*2,<=...,<=*a**n* β€” the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to *n*.
[ "1\n1\n", "2\n1 2\n", "4\n1 2 3 4\n", "3\n1 1 1\n" ]
[ "-1\n", "-1\n", "1 2\n", "-1\n" ]
In the first two samples the required pairs obviously don't exist. In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
[ { "input": "1\n1", "output": "-1" }, { "input": "2\n1 2", "output": "-1" }, { "input": "4\n1 2 3 4", "output": "1 2" }, { "input": "3\n1 1 1", "output": "-1" }, { "input": "3\n1 2 2", "output": "1 2" }, { "input": "5\n1 1 1 1 2", "output": "2 5" ...
342
8,396,800
3
7,635
486
OR in Matrix
[ "greedy", "hashing", "implementation" ]
null
null
Let's define logical *OR* as an operation on two logical values (i. e. values that belong to the set {0,<=1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical *OR* of three or more logical values in the same manner: where is equal to 1 if some *a**i*<==...
The first line contains two integer *m* and *n* (1<=≀<=*m*,<=*n*<=≀<=100), number of rows and number of columns of matrices respectively. The next *m* lines each contain *n* integers separated by spaces describing rows of matrix *B* (each element of *B* is either 0 or 1).
In the first line, print "NO" if Nam has made a mistake when calculating *B*, otherwise print "YES". If the first line is "YES", then also print *m* rows consisting of *n* integers representing matrix *A* that can produce given matrix *B*. If there are several solutions print any one.
[ "2 2\n1 0\n0 0\n", "2 3\n1 1 1\n1 1 1\n", "2 3\n0 1 0\n1 1 1\n" ]
[ "NO\n", "YES\n1 1 1\n1 1 1\n", "YES\n0 0 0\n0 1 0\n" ]
none
[ { "input": "2 2\n1 0\n0 0", "output": "NO" }, { "input": "2 3\n1 1 1\n1 1 1", "output": "YES\n1 1 1\n1 1 1" }, { "input": "2 3\n0 1 0\n1 1 1", "output": "YES\n0 0 0\n0 1 0" }, { "input": "5 5\n1 1 1 1 1\n1 0 0 0 0\n1 0 0 0 0\n1 0 0 0 0\n1 0 0 0 0", "output": "YES\n1 0 0 0...
46
307,200
0
7,646
34
Page Numbers
[ "expression parsing", "implementation", "sortings", "strings" ]
C. Page Numbers
2
256
Β«BersoftΒ» company is working on a new version of its most popular text editor β€” Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates them with a comma, without spaces). Your task is to w...
The only line contains the sequence, keyed by the user. The sequence contains at least one and at most 100 positive integer numbers. It's guaranteed, that this sequence consists of positive integer numbers, not exceeding 1000, separated with a comma, doesn't contain any other characters, apart from digits and commas, c...
Output the sequence in the required format.
[ "1,2,3,1,1,2,6,6,2\n", "3,2,1\n", "30,20,10\n" ]
[ "1-3,6\n", "1-3\n", "10,20,30\n" ]
none
[ { "input": "1,2,3,1,1,2,6,6,2", "output": "1-3,6" }, { "input": "3,2,1", "output": "1-3" }, { "input": "30,20,10", "output": "10,20,30" }, { "input": "826,747,849,687,437", "output": "437,687,747,826,849" }, { "input": "999,999,993,969,999", "output": "969,993...
92
4,505,600
3.968608
7,650
37
Old Berland Language
[ "data structures", "greedy", "trees" ]
C. Old Berland Language
2
256
Berland scientists know that the Old Berland language had exactly *n* words. Those words had lengths of *l*1,<=*l*2,<=...,<=*l**n* letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each ...
The first line contains one integer *N* (1<=≀<=*N*<=≀<=1000) β€” the number of words in Old Berland language. The second line contains *N* space-separated integers β€” the lengths of these words. All the lengths are natural numbers not exceeding 1000.
If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next *N* lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any.
[ "3\n1 2 3\n", "3\n1 1 1\n" ]
[ "YES\n0\n10\n110\n", "NO\n" ]
none
[ { "input": "3\n1 2 3", "output": "YES\n0\n10\n110" }, { "input": "3\n1 1 1", "output": "NO" }, { "input": "10\n4 4 4 4 4 4 4 4 4 4", "output": "YES\n0000\n0001\n0010\n0011\n0100\n0101\n0110\n0111\n1000\n1001" }, { "input": "20\n6 7 7 7 7 6 7 7 7 7 7 7 7 7 7 7 7 7 6 7", "o...
436
1,843,200
-1
7,651
493
Vasya and Football
[ "implementation" ]
null
null
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card. Vasya is watching a recorded football match now and makes notes of all the fouls tha...
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct. Next follows number *n* (1<=≀<=*n*<=≀<=9...
For each event when a player received his first red card in a chronological order print a string containing the following information: - The name of the team to which the player belongs; - the player's number in his team; - the minute when he received the card. If no player received a card, then you do not need to...
[ "MC\nCSKA\n9\n28 a 3 y\n62 h 25 y\n66 h 42 y\n70 h 25 y\n77 a 4 y\n79 a 25 y\n82 h 42 r\n89 h 16 y\n90 a 13 r\n" ]
[ "MC 25 70\nMC 42 82\nCSKA 13 90\n" ]
none
[ { "input": "MC\nCSKA\n9\n28 a 3 y\n62 h 25 y\n66 h 42 y\n70 h 25 y\n77 a 4 y\n79 a 25 y\n82 h 42 r\n89 h 16 y\n90 a 13 r", "output": "MC 25 70\nMC 42 82\nCSKA 13 90" }, { "input": "REAL\nBARCA\n3\n27 h 7 y\n44 a 10 y\n87 h 3 r", "output": "REAL 3 87" }, { "input": "MASFF\nSAFBDSRG\n5\n1 ...
62
0
3
7,662
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 ...
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 ...
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":...
61
307,200
0
7,663
26
Regular Bracket Sequence
[ "greedy" ]
B. Regular Bracket Sequence
5
256
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters Β«+Β» and Β«1Β» into this sequence. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. One day Johnny got bracket sequence. He decided to remove some...
Input consists of a single line with non-empty string of Β«(Β» and Β«)Β» characters. Its length does not exceed 106.
Output the maximum possible length of a regular bracket sequence.
[ "(()))(\n", "((()())\n" ]
[ "4\n", "6\n" ]
none
[ { "input": "(()))(", "output": "4" }, { "input": "((()())", "output": "6" }, { "input": "(", "output": "0" }, { "input": ")", "output": "0" }, { "input": ")(()(", "output": "2" }, { "input": "))))))(", "output": "0" }, { "input": "()()(()((...
872
15,052,800
3.884762
7,716
731
Socks
[ "dfs and similar", "dsu", "graphs", "greedy" ]
null
null
Arseniy is already grown-up and independent. His mother decided to leave him alone for *m* days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes. Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular c...
The first line of input contains three integers *n*, *m* and *k* (2<=≀<=*n*<=≀<=200<=000, 0<=≀<=*m*<=≀<=200<=000, 1<=≀<=*k*<=≀<=200<=000)Β β€” the number of socks, the number of days and the number of available colors respectively. The second line contain *n* integers *c*1, *c*2, ..., *c**n* (1<=≀<=*c**i*<=≀<=*k*)Β β€” curr...
Print one integerΒ β€” the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.
[ "3 2 3\n1 2 3\n1 2\n2 3\n", "3 2 2\n1 1 2\n1 2\n2 1\n" ]
[ "2\n", "0\n" ]
In the first sample, Arseniy can repaint the first and the third socks to the second color. In the second sample, there is no need to change any colors.
[ { "input": "3 2 3\n1 2 3\n1 2\n2 3", "output": "2" }, { "input": "3 2 2\n1 1 2\n1 2\n2 1", "output": "0" }, { "input": "3 3 3\n1 2 3\n1 2\n2 3\n3 1", "output": "2" }, { "input": "4 2 4\n1 2 3 4\n1 2\n3 4", "output": "2" }, { "input": "10 3 2\n2 1 1 2 1 1 2 1 2 2\n...
77
20,172,800
0
7,736
678
Another Sith Tournament
[ "bitmasks", "dp", "math", "probabilities" ]
null
null
The rules of Sith Tournament are well known to everyone. *n* Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=18)Β β€” the number of participants of the Sith Tournament. Each of the next *n* lines contains *n* real numbers, which form a matrix *p**ij* (0<=≀<=*p**ij*<=≀<=1). Each its element *p**ij* is the probability that the *i*-th participant defeats the *j*-th in a d...
Output a real numberΒ β€” the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10<=-<=6.
[ "3\n0.0 0.5 0.8\n0.5 0.0 0.4\n0.2 0.6 0.0\n" ]
[ "0.680000000000000\n" ]
none
[ { "input": "3\n0.0 0.5 0.8\n0.5 0.0 0.4\n0.2 0.6 0.0", "output": "0.680000000000000" }, { "input": "1\n0.0", "output": "1.000000000000000" }, { "input": "2\n0.00 0.75\n0.25 0.00", "output": "0.750000000000000" }, { "input": "4\n0.0 0.6 0.5 0.4\n0.4 0.0 0.3 0.8\n0.5 0.7 0.0 0....
0
0
-1
7,741
0
none
[ "none" ]
null
null
Pavel cooks barbecue. There are *n* skewers, they lay on a brazier in a row, each on one of *n* positions. Pavel wants each skewer to be cooked some time in every of *n* positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation *p* and a sequence ...
The first line contain the integer *n* (1<=≀<=*n*<=≀<=2Β·105)Β β€” the number of skewers. The second line contains a sequence of integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=*n*)Β β€” the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence *b*1,<=*b*2,<=...,<=*b**n* c...
Print single integerΒ β€” the minimum total number of elements in the given permutation *p* and the given sequence *b* he needs to change so that every skewer will visit each of 2*n* placements.
[ "4\n4 3 2 1\n0 1 1 1\n", "3\n2 3 1\n0 0 0\n" ]
[ "2\n", "1\n" ]
In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of *b* to 1.
[ { "input": "4\n4 3 2 1\n0 1 1 1", "output": "2" }, { "input": "3\n2 3 1\n0 0 0", "output": "1" }, { "input": "1\n1\n0", "output": "1" }, { "input": "2\n1 2\n0 0", "output": "3" }, { "input": "2\n2 1\n0 0", "output": "1" }, { "input": "2\n1 2\n0 1", ...
296
18,534,400
3
7,755
704
Thor
[ "brute force", "data structures", "implementation" ]
null
null
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are *n* applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). *q* events are ...
The first line of input contains two integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=300<=000)Β β€” the number of applications and the number of events to happen. The next *q* lines contain the events. The *i*-th of these lines starts with an integer *type**i*Β β€” type of the *i*-th event. If *type**i*<==<=1 or *type**i*<==<=2 t...
Print the number of unread notifications after each event.
[ "3 4\n1 3\n1 1\n1 2\n2 3\n", "4 6\n1 2\n1 4\n1 2\n3 3\n1 3\n1 3\n" ]
[ "1\n2\n3\n2\n", "1\n2\n3\n0\n1\n2\n" ]
In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 1. Application 1 generates a notification (there are 2 unread notifications). 1. Application 2 generates a notification (there are 3 unread notifications). 1. Thor reads the notification generated by application 3, the...
[ { "input": "3 4\n1 3\n1 1\n1 2\n2 3", "output": "1\n2\n3\n2" }, { "input": "4 6\n1 2\n1 4\n1 2\n3 3\n1 3\n1 3", "output": "1\n2\n3\n0\n1\n2" }, { "input": "10 85\n2 2\n1 10\n1 1\n2 6\n1 2\n1 4\n1 7\n2 1\n1 1\n3 3\n1 9\n1 6\n1 8\n1 10\n3 8\n2 8\n1 6\n1 3\n1 9\n1 6\n1 3\n1 8\n1 1\n1 6\n1 1...
77
4,915,200
-1
7,760
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* fr...
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 ...
30
0
0
7,769
833
The Meaningless Game
[ "math", "number theory" ]
null
null
Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting. The game consists of multiple rounds. Its rules are very simple: in each round, a natural number *k* is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is...
In the first string, the number of games *n* (1<=≀<=*n*<=≀<=350000) is given. Each game is represented by a pair of scores *a*, *b* (1<=≀<=*a*,<=*b*<=≀<=109) – the results of Slastyona and Pushok, correspondingly.
For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise. You can output each letter in arbitrary case (upper or lower).
[ "6\n2 4\n75 45\n8 8\n16 16\n247 994\n1000000000 1000000\n" ]
[ "Yes\nYes\nYes\nNo\nNo\nYes\n" ]
First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won. The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3.
[ { "input": "6\n2 4\n75 45\n8 8\n16 16\n247 994\n1000000000 1000000", "output": "Yes\nYes\nYes\nNo\nNo\nYes" }, { "input": "3\n1 1\n8 27\n1000 1331", "output": "Yes\nNo\nNo" }, { "input": "1\n12004 18012002", "output": "Yes" }, { "input": "1\n3331 11095561", "output": "Yes...
1,000
9,830,400
0
7,782
225
Snake
[ "bitmasks", "dfs and similar", "graphs", "implementation" ]
null
null
Let us remind you the rules of a very popular game called "Snake" (or sometimes "Boa", "Python" or "Worm"). The game field is represented by an *n*<=Γ—<=*m* rectangular table. Some squares of the field are considered impassable (walls), all other squares of the fields are passable. You control a snake, the snake consi...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=15) β€” the number of rows and columns of the game field. Next *n* lines describe the game field. Each of these lines contains *m* characters. Character "#" represents a wall, "." is a passable square, "@" is an apple. The snake's firs...
Print a single integer to the output β€” the minimum number of moves needed to reach the apple. If the snake can't reach the apple, print -1.
[ "4 5\n##...\n..1#@\n432#.\n...#.\n", "4 4\n#78#\n.612\n.543\n..@.\n", "3 2\n3@\n2#\n1#\n" ]
[ "4\n", "6\n", "-1\n" ]
none
[]
92
0
0
7,784
605
Lazy Student
[ "constructive algorithms", "data structures", "graphs" ]
null
null
Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graphΒ β€” something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following defin...
The first line of the input contains two integers *n* and *m* ()Β β€” the number of vertices and the number of edges in the graph. Each of the next *m* lines describes an edge of the graph and consists of two integers *a**j* and *b**j* (1<=≀<=*a**j*<=≀<=109,<=*b**j*<==<={0,<=1}). The first of these numbers is the weight ...
If Vladislav has made a mistake and such graph doesn't exist, print <=-<=1. Otherwise print *m* lines. On the *j*-th line print a pair of vertices (*u**j*,<=*v**j*) (1<=≀<=*u**j*,<=*v**j*<=≀<=*n*,<=*u**j*<=β‰ <=*v**j*), that should be connected by the *j*-th edge. The edges are numbered in the same order as in the input...
[ "4 5\n2 1\n3 1\n4 0\n1 1\n5 0\n", "3 3\n1 0\n2 1\n3 1\n" ]
[ "2 4\n1 4\n3 4\n3 1\n3 2\n", "-1\n" ]
none
[ { "input": "4 5\n2 1\n3 1\n4 0\n1 1\n5 0", "output": "2 4\n1 4\n3 4\n3 1\n3 2" }, { "input": "3 3\n1 0\n2 1\n3 1", "output": "-1" }, { "input": "2 1\n7 1", "output": "1 2" }, { "input": "3 2\n8 1\n9 1", "output": "1 2\n1 3" }, { "input": "3 3\n4 1\n5 0\n7 1", ...
0
0
-1
7,799
744
Hongcow's Game
[ "bitmasks", "divide and conquer", "interactive" ]
null
null
This is an interactive problem. In the interaction section below you will see the information about flushing the output. In this problem, you will be playing a game with Hongcow. How lucky of you! Hongcow has a hidden *n* by *n* matrix *M*. Let *M**i*,<=*j* denote the entry *i*-th row and *j*-th column of the matrix....
The first line of input will contain a single integer *n* (2<=≀<=*n*<=≀<=1,<=000).
To print the final answer, print out the string -1 on its own line. Then, the next line should contain *n* integers. The *i*-th integer should be the minimum value of the *i*-th row of the matrix, excluding elements on the diagonal. Do not forget to flush your answer!
[ "3\n0 0 0\n2 7 0\n0 0 4\n3 0 8\n0 5 4", "2\n0 0\n0 0" ]
[ "3\n1 2 3\n1\n3\n2\n1 2\n1\n2\n1\n1\n-1\n2 5 4\n", "1\n2\n1\n1\n-1\n0 0" ]
In the first sample, Hongcow has the hidden matrix Here is a more readable version demonstrating the interaction. The column on the left represents Hongcow, while the column on the right represents the contestant. For the second sample, it is possible for off-diagonal elements of the matrix to be zero.
[]
155
5,632,000
3
7,805
444
DZY Loves FFT
[ "probabilities" ]
null
null
DZY loves Fast Fourier Transformation, and he enjoys using it. Fast Fourier Transformation is an algorithm used to calculate convolution. Specifically, if *a*, *b* and *c* are sequences with length *n*, which are indexed from 0 to *n*<=-<=1, and We can calculate *c* fast using Fast Fourier Transformation. DZY made a...
The only line of input contains three space-separated integers *n*,<=*d*,<=*x*Β (1<=≀<=*d*<=≀<=*n*<=≀<=100000;Β 0<=≀<=*x*<=≀<=1000000006). Because DZY is naughty, *x* can't be equal to 27777500.
Output *n* lines, the *i*-th line should contain an integer *c**i*<=-<=1.
[ "3 1 1\n", "5 4 2\n", "5 4 3\n" ]
[ "1\n3\n2\n", "2\n2\n4\n5\n5\n", "5\n5\n5\n5\n4\n" ]
In the first sample, *a* is [1 3 2], *b* is [1 0 0], so *c*<sub class="lower-index">0</sub> = *max*(1Β·1) = 1, *c*<sub class="lower-index">1</sub> = *max*(1Β·0, 3Β·1) = 3, *c*<sub class="lower-index">2</sub> = *max*(1Β·0, 3Β·0, 2Β·1) = 2. In the second sample, *a* is [2 1 4 5 3], *b* is [1 1 1 0 1]. In the third sample, *a...
[]
46
0
0
7,822
982
Cut 'em all!
[ "dfs and similar", "dp", "graphs", "greedy", "trees" ]
null
null
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.
The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree.
Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property.
[ "4\n2 4\n4 1\n3 1\n", "3\n1 2\n1 3\n", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5\n", "2\n1 2\n" ]
[ "1", "-1", "4", "0" ]
In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
[ { "input": "4\n2 4\n4 1\n3 1", "output": "1" }, { "input": "3\n1 2\n1 3", "output": "-1" }, { "input": "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "output": "4" }, { "input": "2\n1 2", "output": "0" }, { "input": "1", "output": "-1" }, { "inpu...
327
22,732,800
-1
7,829
955
Sad powers
[ "binary search", "math", "number theory" ]
null
null
You're given *Q* queries of the form (*L*,<=*R*). For each query you have to find the number of such *x* that *L*<=≀<=*x*<=≀<=*R* and there exist integer numbers *a*<=&gt;<=0, *p*<=&gt;<=1 such that *x*<==<=*a**p*.
The first line contains the number of queries *Q* (1<=≀<=*Q*<=≀<=105). The next *Q* lines contains two integers *L*, *R* each (1<=≀<=*L*<=≀<=*R*<=≀<=1018).
Output *Q* lines β€” the answers to the queries.
[ "6\n1 4\n9 9\n5 7\n12 29\n137 591\n1 1000000\n" ]
[ "2\n1\n0\n3\n17\n1111\n" ]
In query one the suitable numbers are 1 and 4.
[ { "input": "6\n1 4\n9 9\n5 7\n12 29\n137 591\n1 1000000", "output": "2\n1\n0\n3\n17\n1111" }, { "input": "20\n862 928\n758 964\n541 789\n622 943\n328 900\n14 764\n217 972\n461 847\n442 468\n900 986\n518 529\n938 993\n549 851\n690 944\n484 601\n320 910\n98 868\n816 915\n765 880\n551 770", "output...
218
12,185,600
0
7,897
914
Bash and a Tough Math Puzzle
[ "data structures", "number theory" ]
null
null
Bash likes playing with arrays. He has an array *a*1,<=*a*2,<=... *a**n* of *n* integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, sometimes the guess is not correct. However, Bash will be satisfied if his guess is almost correct. Suppose he guesses that the gc...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=5Β·105) Β β€” the size of the array. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) Β β€” the elements of the array. The third line contains an integer *q* (1<=≀<=*q*<=≀<=4Β·105) Β β€” the number of queries. The next *q* lines descri...
For each query of first type, output "YES" (without quotes) if Bash's guess is almost correct and "NO" (without quotes) otherwise.
[ "3\n2 6 3\n4\n1 1 2 2\n1 1 3 3\n2 1 9\n1 1 3 2\n", "5\n1 2 3 4 5\n6\n1 1 4 2\n2 3 6\n1 1 4 2\n1 1 5 2\n2 5 10\n1 1 5 2\n" ]
[ "YES\nYES\nNO\n", "NO\nYES\nNO\nYES\n" ]
In the first sample, the array initially is {2, 6, 3}. For query 1, the first two numbers already have their gcd as 2. For query 2, we can achieve a gcd of 3 by changing the first element of the array to 3. Note that the changes made during queries of type 1 are temporary and do not get reflected in the array. Aft...
[ { "input": "3\n2 6 3\n4\n1 1 2 2\n1 1 3 3\n2 1 9\n1 1 3 2", "output": "YES\nYES\nNO" }, { "input": "5\n1 2 3 4 5\n6\n1 1 4 2\n2 3 6\n1 1 4 2\n1 1 5 2\n2 5 10\n1 1 5 2", "output": "NO\nYES\nNO\nYES" }, { "input": "1\n1000000000\n1\n1 1 1 1000000000", "output": "YES" }, { "inpu...
124
0
0
7,903
543
Writing Code
[ "dp" ]
null
null
Programmers working on a large project have just received a task to write exactly *m* lines of code. There are *n* programmers working on a project, the *i*-th of them makes exactly *a**i* bugs in every line of code that he writes. Let's call a sequence of non-negative integers *v*1,<=*v*2,<=...,<=*v**n* a plan, if *...
The first line contains four integers *n*, *m*, *b*, *mod* (1<=≀<=*n*,<=*m*<=≀<=500, 0<=≀<=*b*<=≀<=500; 1<=≀<=*mod*<=≀<=109<=+<=7)Β β€” the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer. The next line co...
Print a single integer β€” the answer to the problem modulo *mod*.
[ "3 3 3 100\n1 1 1\n", "3 6 5 1000000007\n1 2 3\n", "3 5 6 11\n1 2 1\n" ]
[ "10\n", "0\n", "0\n" ]
none
[ { "input": "3 3 3 100\n1 1 1", "output": "10" }, { "input": "3 6 5 1000000007\n1 2 3", "output": "0" }, { "input": "3 5 6 11\n1 2 1", "output": "0" }, { "input": "2 3 3 1000\n1 2", "output": "1" }, { "input": "3 10 10 150691913\n8 7 10", "output": "0" }, {...
0
0
-1
7,921
724
Checking the Calendar
[ "implementation" ]
null
null
You are given names of two days of the week. Please, determine whether it is possible that during some non-leap year the first day of some month was equal to the first day of the week you are given, while the first day of the next month was equal to the second day of the week you are given. Both months should belong t...
The input consists of two lines, each of them containing the name of exactly one day of the week. It's guaranteed that each string in the input is from the set "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday".
Print "YES" (without quotes) if such situation is possible during some non-leap year. Otherwise, print "NO" (without quotes).
[ "monday\ntuesday\n", "sunday\nsunday\n", "saturday\ntuesday\n" ]
[ "NO\n", "YES\n", "YES\n" ]
In the second sample, one can consider February 1 and March 1 of year 2015. Both these days were Sundays. In the third sample, one can consider July 1 and August 1 of year 2017. First of these two days is Saturday, while the second one is Tuesday.
[ { "input": "monday\ntuesday", "output": "NO" }, { "input": "sunday\nsunday", "output": "YES" }, { "input": "saturday\ntuesday", "output": "YES" }, { "input": "tuesday\nthursday", "output": "YES" }, { "input": "friday\nwednesday", "output": "NO" }, { "i...
0
0
-1
7,935
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 suppos...
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", ...
280
0
0
7,936
937
Vile Grasshoppers
[ "brute force", "math", "number theory" ]
null
null
The weather is fine today and hence it's high time to climb the nearby pine and enjoy the landscape. The pine's trunk includes several branches, located one above another and numbered from 2 to *y*. Some of them (more precise, from 2 to *p*) are occupied by tiny vile grasshoppers which you're at war with. These grassh...
The only line contains two integers *p* and *y* (2<=≀<=*p*<=≀<=*y*<=≀<=109).
Output the number of the highest suitable branch. If there are none, print -1 instead.
[ "3 6\n", "3 4\n" ]
[ "5\n", "-1\n" ]
In the first sample case grasshopper from branch 2 reaches branches 2, 4 and 6 while branch 3 is initially settled by another grasshopper. Therefore the answer is 5. It immediately follows that there are no valid branches in second sample case.
[ { "input": "3 6", "output": "5" }, { "input": "3 4", "output": "-1" }, { "input": "2 2", "output": "-1" }, { "input": "5 50", "output": "49" }, { "input": "944192806 944193066", "output": "944192807" }, { "input": "1000000000 1000000000", "output":...
93
0
0
7,947
135
Replacement
[ "greedy", "implementation", "sortings" ]
null
null
Little Petya very much likes arrays consisting of *n* integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that a...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105), which represents how many numbers the array has. The next line contains *n* space-separated integers β€” the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Print *n* space-separated integers β€” the minimum possible values of each array element after one replacement and the sorting are performed.
[ "5\n1 2 3 4 5\n", "5\n2 3 4 5 6\n", "3\n2 2 2\n" ]
[ "1 1 2 3 4\n", "1 2 3 4 5\n", "1 2 2\n" ]
none
[ { "input": "5\n1 2 3 4 5", "output": "1 1 2 3 4" }, { "input": "5\n2 3 4 5 6", "output": "1 2 3 4 5" }, { "input": "3\n2 2 2", "output": "1 2 2" }, { "input": "4\n1 1 2 3", "output": "1 1 1 2" }, { "input": "3\n1 1 1", "output": "1 1 2" }, { "input": "...
404
9,011,200
3
7,954
98
Help Victoria the Wise
[ "brute force", "implementation" ]
A. Help Victoria the Wise
1
256
Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know yet what the surprise actually is because she cannot open the box. She hopes that you can help her in that. The box's lock is constructed...
The first line contains exactly 6 characters without spaces from the set {R, O, Y, G, B, V} β€” they are the colors of gems with which the box should be decorated.
Print the required number of different ways to decorate the box.
[ "YYYYYY\n", "BOOOOB\n", "ROYGBV\n" ]
[ "1\n", "2\n", "30\n" ]
none
[ { "input": "YYYYYY", "output": "1" }, { "input": "BOOOOB", "output": "2" }, { "input": "ROYGBV", "output": "30" }, { "input": "RRRRRR", "output": "1" }, { "input": "BOOOOO", "output": "1" }, { "input": "GOGGVG", "output": "2" }, { "input": ...
139
0
0
7,958
27
Number With The Given Amount Of Divisors
[ "brute force", "dp", "number theory" ]
E. Number With The Given Amount Of Divisors
2
256
Given the number *n*, find the smallest positive integer which has exactly *n* divisors. It is guaranteed that for the given *n* the answer will not exceed 1018.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=1000).
Output the smallest positive integer with exactly *n* divisors.
[ "4\n", "6\n" ]
[ "6\n", "12\n" ]
none
[ { "input": "1", "output": "1" }, { "input": "7", "output": "64" }, { "input": "8", "output": "24" }, { "input": "9", "output": "36" }, { "input": "10", "output": "48" }, { "input": "15", "output": "144" }, { "input": "20", "output": "24...
2,000
204,800
0
7,972
248
Chilly Willy
[ "math", "number theory" ]
null
null
Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up with a few games that were connected with them. Chilly Willy wants to find the minimum number of length *n*, such that it is simultaneous...
A single input line contains a single integer *n* (1<=≀<=*n*<=≀<=105).
Print a single integer β€” the answer to the problem without leading zeroes, or "-1" (without the quotes), if the number that meet the problem condition does not exist.
[ "1\n", "5\n" ]
[ "-1\n", "10080" ]
none
[ { "input": "1", "output": "-1" }, { "input": "5", "output": "10080" }, { "input": "6", "output": "100170" }, { "input": "4", "output": "1050" }, { "input": "15", "output": "100000000000110" }, { "input": "16", "output": "1000000000000050" }, { ...
1,622
7,065,600
3
7,980
13
Holes
[ "data structures", "dsu" ]
E. Holes
1
64
Little Petya likes to play a lot. Most of all he likes to play a game Β«HolesΒ». This is a game for one person with following rules: There are *N* holes located in a single row and numbered from left to right with numbers from 1 to *N*. Each hole has it's own power (hole number *i* has the power *a**i*). If you throw a ...
The first line contains two integers *N* and *M* (1<=≀<=*N*<=≀<=105, 1<=≀<=*M*<=≀<=105) β€” the number of holes in a row and the number of moves. The second line contains *N* positive integers not exceeding *N* β€” initial values of holes power. The following *M* lines describe moves made by Petya. Each of these line can b...
For each move of the type 1 output two space-separated numbers on a separate line β€” the number of the last hole the ball visited before leaving the row and the number of jumps it made.
[ "8 5\n1 1 1 1 1 2 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n" ]
[ "8 7\n8 5\n7 3\n" ]
none
[ { "input": "8 5\n1 1 1 1 1 2 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2", "output": "8 7\n8 5\n7 3" }, { "input": "10 10\n5 1 2 4 1 7 3 8 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 6 1\n1 9\n1 1", "output": "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n10 4" } ]
0
0
-1
8,015
645
Enduring Exodus
[ "binary search", "two pointers" ]
null
null
In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his *k* cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of *n* rooms located in a row, some of which are occupie...
The first line of the input contains two integers *n* and *k* (1<=≀<=*k*<=&lt;<=*n*<=≀<=100<=000)Β β€” the number of rooms in the hotel and the number of cows travelling with Farmer John. The second line contains a string of length *n* describing the rooms. The *i*-th character of the string will be '0' if the *i*-th roo...
Print the minimum possible distance between Farmer John's room and his farthest cow.
[ "7 2\n0100100\n", "5 1\n01010\n", "3 2\n000\n" ]
[ "2\n", "2\n", "1\n" ]
In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms. In the second sample, Farmer John can book room 1 for himself and room 3 f...
[ { "input": "7 2\n0100100", "output": "2" }, { "input": "5 1\n01010", "output": "2" }, { "input": "3 2\n000", "output": "1" }, { "input": "10 1\n1101111101", "output": "6" }, { "input": "2 1\n00", "output": "1" }, { "input": "3 1\n010", "output": "2...
93
8,192,000
0
8,021
641
Little Artem and Random Variable
[ "dp", "implementation", "math", "probabilities" ]
null
null
Little Artyom decided to study probability theory. He found a book with a lot of nice exercises and now wants you to help him with one of them. Consider two dices. When thrown each dice shows some integer from 1 to *n* inclusive. For each dice the probability of each outcome is given (of course, their sum is 1), and d...
First line contains the integer *n* (1<=≀<=*n*<=≀<=100<=000)Β β€” the number of different values for both dices. Second line contains an array consisting of *n* real values with up to 8 digits after the decimal point Β β€” probability distribution for *max*(*a*,<=*b*), the *i*-th of these values equals to the probability th...
Output two descriptions of the probability distribution for *a* on the first line and for *b* on the second line. The answer will be considered correct if each value of max(*a*,<=*b*) and min(*a*,<=*b*) probability distribution values does not differ by more than 10<=-<=6 from ones given in input. Also, probabilities...
[ "2\n0.25 0.75\n0.75 0.25\n", "3\n0.125 0.25 0.625\n0.625 0.25 0.125\n" ]
[ "0.5 0.5 \n0.5 0.5 \n", "0.25 0.25 0.5 \n0.5 0.25 0.25 \n" ]
none
[ { "input": "2\n0.25 0.75\n0.75 0.25", "output": "0.5 0.5 \n0.5 0.5 " }, { "input": "3\n0.125 0.25 0.625\n0.625 0.25 0.125", "output": "0.25 0.25 0.5 \n0.5 0.25 0.25 " }, { "input": "10\n0.01 0.01 0.01 0.01 0.01 0.1 0.2 0.2 0.4 0.05\n1.0 0 0 0 0 0 0 0 0 0", "output": "0.01000000000000...
46
4,608,000
0
8,040
79
Beaver
[ "data structures", "dp", "greedy", "hashing", "strings", "two pointers" ]
C. Beaver
2
256
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string *s*, and she tried to remember the string *s* correctly. However, Ciel...
In the first line there is a string *s*. The length of *s* will be between 1 and 105, inclusive. In the second line there is a single integer *n* (1<=≀<=*n*<=≀<=10). Next *n* lines, there is a string *b**i* (1<=≀<=*i*<=≀<=*n*). Each length of *b**i* will be between 1 and 10, inclusive. Each character of the given st...
Output in the first line two space-separated integers *len* and *pos*: the length of the longest contiguous substring of *s* that does not contain any *b**i*, and the first position of the substring (0-indexed). The position *pos* must be between 0 and |*s*|<=-<=*len* inclusive, where |*s*| is the length of string *s*....
[ "Go_straight_along_this_street\n5\nstr\nlong\ntree\nbiginteger\nellipse\n", "IhaveNoIdea\n9\nI\nh\na\nv\ne\nN\no\nI\nd\n", "unagioisii\n2\nioi\nunagi\n" ]
[ "12 4\n", "0 0\n", "5 5\n" ]
In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be Β«0 0Β», Β«0 1Β», Β«0 2Β», and so on. In the third sample, the solution is either nagio or oisii.
[ { "input": "Go_straight_along_this_street\n5\nstr\nlong\ntree\nbiginteger\nellipse", "output": "12 4" }, { "input": "IhaveNoIdea\n9\nI\nh\na\nv\ne\nN\no\nI\nd", "output": "0 0" }, { "input": "unagioisii\n2\nioi\nunagi", "output": "5 5" }, { "input": "abcabcabcabc\n3\nabcabca\...
436
716,800
3.889665
8,051
721
Maxim and Array
[ "constructive algorithms", "data structures", "greedy", "math" ]
null
null
Recently Maxim has found an array of *n* integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer *x* and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer *i* (1<=≀<=*i*<=≀<=*n*) and replaces the *i...
The first line of the input contains three integers *n*,<=*k* and *x* (1<=≀<=*n*,<=*k*<=≀<=200<=000,<=1<=≀<=*x*<=≀<=109)Β β€” the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* ()Β β€” the elem...
Print *n* integers *b*1,<=*b*2,<=...,<=*b**n* in the only lineΒ β€” the array elements after applying no more than *k* operations to the array. In particular, should stay true for every 1<=≀<=*i*<=≀<=*n*, but the product of all array elements should be minimum possible. If there are multiple answers, print any of them.
[ "5 3 1\n5 4 3 5 2\n", "5 3 1\n5 4 3 5 5\n", "5 3 1\n5 4 4 5 5\n", "3 2 7\n5 4 2\n" ]
[ "5 4 3 5 -1 \n", "5 4 0 5 5 \n", "5 1 4 5 5 \n", "5 11 -5 \n" ]
none
[ { "input": "5 3 1\n5 4 3 5 2", "output": "5 4 3 5 -1 " }, { "input": "5 3 1\n5 4 3 5 5", "output": "5 4 0 5 5 " }, { "input": "5 3 1\n5 4 4 5 5", "output": "5 1 4 5 5 " }, { "input": "3 2 7\n5 4 2", "output": "5 11 -5 " }, { "input": "100 1 1\n-1 -1 -2 0 -2 -1 0 0...
295
26,931,200
0
8,056
19
World Football Cup
[ "implementation" ]
A. World Football Cup
2
64
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations: - the final tournament features *n* teams (*n* is always even) - the first *n*<=/<=2 t...
The first input line contains the only integer *n* (1<=≀<=*n*<=≀<=50) β€” amount of the teams, taking part in the final tournament of World Cup. The following *n* lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following *...
Output *n*<=/<=2 lines β€” names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.
[ "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3\n", "2\na\nA\na-A 2:1\n" ]
[ "A\nD\n", "a\n" ]
none
[ { "input": "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3", "output": "A\nD" }, { "input": "2\na\nA\na-A 2:1", "output": "a" }, { "input": "2\nEULEUbCmfrmqxtzvg\nuHGRmKUhDcxcfqyruwzen\nuHGRmKUhDcxcfqyruwzen-EULEUbCmfrmqxtzvg 13:92", "output": "EULEUbCmfrmqxtzvg"...
248
20,172,800
0
8,065
383
Milking cows
[ "data structures", "greedy" ]
null
null
Iahub helps his grandfather at the farm. Today he must milk the cows. There are *n* cows sitting in a row, numbered from 1 to *n* from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity o...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=200000). The second line contains *n* integers *a*1, *a*2, ..., *a**n*, where *a**i* is 0 if the cow number *i* is facing left, and 1 if it is facing right.
Print a single integer, the minimum amount of lost milk. 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.
[ "4\n0 0 1 0\n", "5\n1 0 1 0 1\n" ]
[ "1", "3" ]
In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
[ { "input": "4\n0 0 1 0", "output": "1" }, { "input": "5\n1 0 1 0 1", "output": "3" }, { "input": "50\n1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0", "output": "416" }, { "input": "100\n1 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1 0 0 ...
155
14,848,000
3
8,073
70
Text Messaging
[ "expression parsing", "greedy", "strings" ]
B. Text Messaging
1
256
Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing *n* characters (which is the size of one text message). Thus, whole sentences and words get split! Fangy did not lik...
The first line contains an integer *n*, which is the size of one message (2<=≀<=*n*<=≀<=255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty.
On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print "Impossible" without the quotes.
[ "25\nHello. I am a little walrus.\n", "2\nHow are you?\n", "19\nHello! Do you like fish? Why?\n" ]
[ "2\n", "Impossible\n", "3\n" ]
Let's take a look at the third sample. The text will be split into three messages: "Hello!", "Do you like fish?" and "Why?".
[ { "input": "25\nHello. I am a little walrus.", "output": "2" }, { "input": "2\nHow are you?", "output": "Impossible" }, { "input": "19\nHello! Do you like fish? Why?", "output": "3" }, { "input": "4\na. A.", "output": "2" }, { "input": "191\nEvx vnxZtUSgtzH yDNXCs...
93
0
0
8,076
835
Star sky
[ "dp", "implementation" ]
null
null
The Cartesian coordinate system is set in the sky. There you can see *n* stars, the *i*-th has coordinates (*x**i*, *y**i*), a maximum brightness *c*, equal for all stars, and an initial brightness *s**i* (0<=≀<=*s**i*<=≀<=*c*). Over time the stars twinkle. At moment 0 the *i*-th star has brightness *s**i*. Let at mom...
The first line contains three integers *n*, *q*, *c* (1<=≀<=*n*,<=*q*<=≀<=105, 1<=≀<=*c*<=≀<=10)Β β€” the number of the stars, the number of the views and the maximum brightness of the stars. The next *n* lines contain the stars description. The *i*-th from these lines contains three integers *x**i*, *y**i*, *s**i* (1<=≀...
For each view print the total brightness of the viewed stars.
[ "2 3 3\n1 1 1\n3 2 0\n2 1 1 2 2\n0 2 1 4 5\n5 1 1 5 5\n", "3 4 5\n1 1 2\n2 3 0\n3 3 1\n0 1 1 100 100\n1 2 2 4 4\n2 2 1 4 7\n1 50 50 51 51\n" ]
[ "3\n0\n3\n", "3\n3\n5\n0\n" ]
Let's consider the first example. At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3. At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0. At the third view, you can see both stars. At moment 5 brightness of th...
[ { "input": "2 3 3\n1 1 1\n3 2 0\n2 1 1 2 2\n0 2 1 4 5\n5 1 1 5 5", "output": "3\n0\n3" }, { "input": "3 4 5\n1 1 2\n2 3 0\n3 3 1\n0 1 1 100 100\n1 2 2 4 4\n2 2 1 4 7\n1 50 50 51 51", "output": "3\n3\n5\n0" } ]
686
11,059,200
3
8,108
0
none
[ "none" ]
null
null
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of *n* questions; the questions are to be answered strictly in the order in which they are given, from question 1 t...
The first line contains a positive integer *n* (1<=≀<=*n*<=≀<=100). It is the number of questions in the test. The second line contains space-separated *n* positive integers *a**i* (1<=≀<=*a**i*<=≀<=109), the number of answer variants to question *i*.
Print a single number β€” the minimal number of clicks needed to pass the test it the worst-case scenario. 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.
[ "2\n1 1\n", "2\n2 2\n", "1\n10\n" ]
[ "2", "5", "10" ]
Note to the second sample. In the worst-case scenario you will need five clicks: - the first click selects the first variant to the first question, this answer turns out to be wrong. - the second click selects the second variant to the first question, it proves correct and we move on to the second question; - the ...
[ { "input": "2\n1 1", "output": "2" }, { "input": "2\n2 2", "output": "5" }, { "input": "1\n10", "output": "10" }, { "input": "3\n2 4 1", "output": "10" }, { "input": "4\n5 5 3 1", "output": "22" }, { "input": "2\n1000000000 1000000000", "output": "...
62
0
3
8,109
847
Union of Doubly Linked Lists
[ "implementation" ]
null
null
Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each eleme...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of memory cells where the doubly linked lists are located. Each of the following *n* lines contains two integers *l**i*, *r**i* (0<=≀<=*l**i*,<=*r**i*<=≀<=*n*) β€” the cells of the previous and the next element of list for cell *i*. Value *l**...
Print *n* lines, the *i*-th line must contain two integers *l**i* and *r**i* β€” the cells of the previous and the next element of list for cell *i* after all lists from the input are united in a single list. If there are many solutions print any of them.
[ "7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0\n" ]
[ "4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0\n" ]
none
[ { "input": "7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0", "output": "4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0" }, { "input": "2\n2 0\n0 1", "output": "2 0\n0 1" }, { "input": "1\n0 0", "output": "0 0" }, { "input": "4\n0 2\n1 0\n0 4\n3 0", "output": "0 2\n1 3\n2 4\n3 0" }, { "i...
46
204,800
-1
8,141
398
Sorting Permutations
[]
null
null
We are given a permutation sequence *a*1,<=*a*2,<=...,<=*a**n* of numbers from 1 to *n*. Let's assume that in one second, we can choose some disjoint pairs (*u*1,<=*v*1),<=(*u*2,<=*v*2),<=...,<=(*u**k*,<=*v**k*) and swap all *a**u**i* and *a**v**i* for every *i* at the same time (1<=≀<=*u**i*<=&lt;<=*v**i*<=≀<=*n*). Th...
The first line contains two integers *n* (1<=≀<=*n*<=≀<=105) and *k* (0<=≀<=*k*<=≀<=12). The second line contains the permutation sequence *a*1,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=*n*). If a number is not yet determined, it is denoted as 0. There are exactly *k* zeroes. All the numbers *a**i* that aren't equal to zero are...
Print the total sum of the number of ways modulo 1000000007 (109<=+<=7).
[ "5 0\n1 5 2 4 3\n", "5 2\n1 0 2 4 0\n" ]
[ "6\n", "7\n" ]
none
[]
46
0
0
8,159
305
Ivan and Powers of Two
[ "greedy", "implementation" ]
null
null
Ivan has got an array of *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n*. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2*a*1,<=2*a*2,<=...,<=2*a**n* on a piece of paper. Now he wonders, what minimum number of integers of form 2*b* (*b*<=β‰₯<=0) need to be added to the pi...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105). The second input line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=2Β·109). It is guaranteed that *a*1<=≀<=*a*2<=≀<=...<=≀<=*a**n*.
Print a single integer β€” the answer to the problem.
[ "4\n0 1 1 1\n", "1\n3\n" ]
[ "0\n", "3\n" ]
In the first sample you do not need to add anything, the sum of numbers already equals 2<sup class="upper-index">3</sup> - 1 = 7. In the second sample you need to add numbers 2<sup class="upper-index">0</sup>, 2<sup class="upper-index">1</sup>, 2<sup class="upper-index">2</sup>.
[ { "input": "4\n0 1 1 1", "output": "0" }, { "input": "1\n3", "output": "3" }, { "input": "1\n0", "output": "0" }, { "input": "1\n2000000000", "output": "2000000000" }, { "input": "1\n1", "output": "1" }, { "input": "26\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
500
19,251,200
0
8,170
910
Door Frames
[ "greedy", "implementation" ]
null
null
Petya has equal wooden bars of length *n*. He wants to make a frame for two equal doors. Each frame has two vertical (left and right) sides of length *a* and one top side of length *b*. A solid (i.e. continuous without breaks) piece of bar is needed for each side. Determine a minimal number of wooden bars which are ne...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1<=000) β€” the length of each wooden bar. The second line contains a single integer *a* (1<=≀<=*a*<=≀<=*n*) β€” the length of the vertical (left and right) sides of a door frame. The third line contains a single integer *b* (1<=≀<=*b*<=≀<=*n*) β€” the length of t...
Print the minimal number of wooden bars with length *n* which are needed to make the frames for two doors.
[ "8\n1\n2\n", "5\n3\n4\n", "6\n4\n2\n", "20\n5\n6\n" ]
[ "1\n", "6\n", "4\n", "2\n" ]
In the first example one wooden bar is enough, since the total length of all six sides of the frames for two doors is 8. In the second example 6 wooden bars is enough, because for each side of the frames the new wooden bar is needed.
[ { "input": "8\n1\n2", "output": "1" }, { "input": "5\n3\n4", "output": "6" }, { "input": "6\n4\n2", "output": "4" }, { "input": "20\n5\n6", "output": "2" }, { "input": "1\n1\n1", "output": "6" }, { "input": "3\n1\n2", "output": "3" }, { "in...
62
5,632,000
3
8,237
626
Cards
[ "constructive algorithms", "dp", "math" ]
null
null
Catherine has a deck of *n* cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions: - take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color; - take any two (not necessarily adja...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=200)Β β€” the total number of cards. The next line contains a string *s* of length *n* β€” the colors of the cards. *s* contains only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively.
Print a single string of up to three charactersΒ β€” the possible colors of the final card (using the same symbols as the input) in alphabetical order.
[ "2\nRB\n", "3\nGRG\n", "5\nBBBBB\n" ]
[ "G\n", "BR\n", "B\n" ]
In the first sample, Catherine has one red card and one blue card, which she must exchange for a green card. In the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue car...
[ { "input": "2\nRB", "output": "G" }, { "input": "3\nGRG", "output": "BR" }, { "input": "5\nBBBBB", "output": "B" }, { "input": "1\nR", "output": "R" }, { "input": "200\nBBRGRRBBRGGGBGBGBGRRGRGRGRBGRGRRBBGRGBGRRGRRRGGBBRGBGBGBRBBBBBBBGGBRGGRRRGGRGBGBGGBRRRRBRRRBRBB...
46
409,600
0
8,247
727
Bill Total Value
[ "expression parsing", "implementation", "strings" ]
null
null
Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format "*name*1*price*1*name*2*price*2...*name**n**price**n*", where *name**i* (name of th...
The only line of the input contains a non-empty string *s* with length not greater than 1000Β β€” the content of the bill. It is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 106 dollars.
Print the total price exactly in the same format as prices given in the input.
[ "chipsy48.32televizor12.390\n", "a1b2c3.38\n", "aa0.01t0.03\n" ]
[ "12.438.32\n", "6.38\n", "0.04\n" ]
none
[ { "input": "chipsy48.32televizor12.390", "output": "12.438.32" }, { "input": "a1b2c3.38", "output": "6.38" }, { "input": "aa0.01t0.03", "output": "0.04" }, { "input": "test0.50test0.50", "output": "1" }, { "input": "a500b500", "output": "1.000" }, { "i...
93
307,200
0
8,252
656
You're a Professional
[ "*special" ]
null
null
A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system. You are given user's friends' opinions about a list of items. You are also given a threshold *T* β€” the minimal number of "likes" necessary for an item to be...
The first line of the input will contain three space-separated integers: the number of friends *F* (1<=≀<=*F*<=≀<=10), the number of items *I* (1<=≀<=*I*<=≀<=10) and the threshold *T* (1<=≀<=*T*<=≀<=*F*). The following *F* lines of input contain user's friends' opinions. *j*-th character of *i*-th line is 'Y' if *i*-t...
Output an integer β€” the number of items liked by at least *T* of user's friends.
[ "3 3 2\nYYY\nNNN\nYNY\n", "4 4 1\nNNNY\nNNYN\nNYNN\nYNNN\n" ]
[ "2\n", "4\n" ]
none
[ { "input": "3 3 2\nYYY\nNNN\nYNY", "output": "2" }, { "input": "4 4 1\nNNNY\nNNYN\nNYNN\nYNNN", "output": "4" }, { "input": "3 5 2\nNYNNY\nYNNNN\nNNYYN", "output": "0" }, { "input": "1 10 1\nYYYNYNNYNN", "output": "5" }, { "input": "10 1 5\nY\nN\nN\nN\nY\nN\nN\nY\...
0
0
0
8,299
873
Merge Sort
[ "constructive algorithms", "divide and conquer" ]
null
null
Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array *a* with indices from [*l*,<=*r*) can be implemented as follows: 1. If the segment [*l*,<=*r*) is already sorted in non-descending order (that is, for any *i* such that *l*<=≀<=*i*<=&lt;<=*r*<=-<=1 *a*[*i*]<=≀<=*a*[*i*<=+...
The first line contains two numbers *n* and *k* (1<=≀<=*n*<=≀<=100000, 1<=≀<=*k*<=≀<=200000) β€” the size of a desired permutation and the number of *mergesort* calls required to sort it.
If a permutation of size *n* such that there will be exactly *k* calls of *mergesort* while sorting it doesn't exist, output <=-<=1. Otherwise output *n* integer numbers *a*[0],<=*a*[1],<=...,<=*a*[*n*<=-<=1] β€” the elements of a permutation that would meet the required conditions. If there are multiple answers, print a...
[ "3 3\n", "4 1\n", "5 6\n" ]
[ "2 1 3 ", "1 2 3 4 ", "-1\n" ]
none
[ { "input": "3 3", "output": "2 1 3 " }, { "input": "4 1", "output": "1 2 3 4 " }, { "input": "5 6", "output": "-1" }, { "input": "100 100", "output": "-1" }, { "input": "10000 10001", "output": "3 1 5 2 7 4 8 10 6 12 9 13 15 11 17 14 18 20 16 22 19 23 25 21 27...
171
4,710,400
0
8,308
5
Bindian Signalizing
[ "data structures" ]
E. Bindian Signalizing
4
256
Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by *n* hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night. In case of any danger the watchman could make a fire on the hill. One watchman co...
The first line of the input data contains an integer number *n* (3<=≀<=*n*<=≀<=106), *n* β€” the amount of hills around the capital. The second line contains *n* numbers β€” heights of the hills in clockwise order. All height numbers are integer and lie between 1 and 109.
Print the required amount of pairs.
[ "5\n1 2 4 5 3\n" ]
[ "7\n" ]
none
[ { "input": "5\n1 2 4 5 3", "output": "7" }, { "input": "3\n2118 2118 2118", "output": "3" }, { "input": "3\n2221 1976 2221", "output": "3" }, { "input": "3\n140 989 2895", "output": "3" }, { "input": "4\n2440 2440 2440 2440", "output": "6" }, { "input"...
0
0
-1
8,317
676
Pyramid of Glasses
[ "implementation", "math", "math" ]
null
null
Mary has just graduated from one well-known University and is now attending celebration party. Students like to dream of a beautiful life, so they used champagne glasses to construct a small pyramid. The height of the pyramid is *n*. The top level consists of only 1 glass, that stands on 2 glasses on the second level (...
The only line of the input contains two integers *n* and *t* (1<=≀<=*n*<=≀<=10,<=0<=≀<=*t*<=≀<=10<=000)Β β€” the height of the pyramid and the number of seconds Vlad will be pouring champagne from the bottle.
Print the single integerΒ β€” the number of completely full glasses after *t* seconds.
[ "3 5\n", "4 8\n" ]
[ "4\n", "6\n" ]
In the first sample, the glasses full after 5 seconds are: the top glass, both glasses on the second level and the middle glass at the bottom level. Left and right glasses of the bottom level will be half-empty.
[ { "input": "3 5", "output": "4" }, { "input": "4 8", "output": "6" }, { "input": "1 1", "output": "1" }, { "input": "10 10000", "output": "55" }, { "input": "1 10000", "output": "1" }, { "input": "10 1", "output": "1" }, { "input": "1 0", ...
77
20,172,800
0
8,327
784
Kids' Riddle
[ "*special" ]
null
null
Programmers' kids solve this riddle in 5-10 minutes. How fast can you do it?
The input contains a single integer *n* (0<=≀<=*n*<=≀<=2000000000).
Output a single integer.
[ "11\n", "14\n", "61441\n", "571576\n", "2128506\n" ]
[ "2\n", "0\n", "2\n", "10\n", "3\n" ]
none
[ { "input": "11", "output": "2" }, { "input": "14", "output": "0" }, { "input": "61441", "output": "2" }, { "input": "571576", "output": "10" }, { "input": "2128506", "output": "3" }, { "input": "0", "output": "1" }, { "input": "2000000000",...
124
0
0
8,379
363
Renting Bikes
[ "binary search", "greedy" ]
null
null
A group of *n* schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them *m* bikes. The renting price is different for different bikes, renting the *j*-th bike costs *p**j* rubles. In total, the boys' shared budget is *a* rubles. Besides, each of them ha...
The first line of the input contains three integers *n*, *m* and *a* (1<=≀<=*n*,<=*m*<=≀<=105; 0<=≀<=*a*<=≀<=109). The second line contains the sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=104), where *b**i* is the amount of the *i*-th boy's personal money. The third line contains the sequence of i...
Print two integers *r* and *s*, where *r* is the maximum number of schoolboys that can rent a bike and *s* is the minimum total personal money needed to rent *r* bikes. If the schoolchildren cannot rent any bikes, then *r*<==<=*s*<==<=0.
[ "2 2 10\n5 5\n7 6\n", "4 5 2\n8 1 1 2\n6 3 7 5 2\n" ]
[ "2 3\n", "3 8\n" ]
In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal ...
[ { "input": "2 2 10\n5 5\n7 6", "output": "2 3" }, { "input": "4 5 2\n8 1 1 2\n6 3 7 5 2", "output": "3 8" }, { "input": "1 1 2\n1\n2", "output": "1 0" }, { "input": "4 1 1\n3 2 3 2\n3", "output": "1 2" }, { "input": "1 4 1\n3\n2 4 5 5", "output": "1 1" }, ...
31
0
0
8,387
416
Booking System
[ "binary search", "dp", "greedy", "implementation" ]
null
null
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are *n* booking requests received by now. Each request is characterized by two numbers: *c**i*...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=1000) β€” the number of requests from visitors. Then *n* lines follow. Each line contains two integers: *c**i*,<=*p**i* (1<=≀<=*c**i*,<=*p**i*<=≀<=1000) β€” the size of the group of visitors who will come by the *i*-th request and the total sum of money they w...
In the first line print two integers: *m*,<=*s* β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print *m* lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this...
[ "3\n10 50\n2 100\n5 30\n3\n4 6 9\n" ]
[ "2 130\n2 1\n3 2\n" ]
none
[ { "input": "3\n10 50\n2 100\n5 30\n3\n4 6 9", "output": "2 130\n2 1\n3 2" }, { "input": "1\n1 1\n1\n1", "output": "1 1\n1 1" }, { "input": "1\n2 1\n1\n1", "output": "0 0" }, { "input": "2\n10 10\n5 5\n1\n5", "output": "1 5\n2 1" }, { "input": "2\n10 10\n5 5\n1\n10...
623
0
0
8,403
784
INTERCALC
[ "*special", "implementation" ]
null
null
DO YOU EXPECT ME TO FIND THIS OUT? WHAT BASE AND/XOR LANGUAGE INCLUDES string? DON'T BYTE OF MORE THAN YOU CAN CHEW YOU CAN ONLY DISTORT THE LARGEST OF MATHEMATICS SO FAR SAYING "ABRACADABRA" WITHOUT A MAGIC AND WON'T DO YOU ANY GOOD THE LAST STACK RUPTURES. ALL DIE. OH, THE EMBARRASSMENT! I HAVE NO ARRAY AND I M...
The first line of input data contains a single integer *n* (1<=≀<=*n*<=≀<=10). The second line of input data contains *n* space-separated integers *a**i* (1<=≀<=*a**i*<=≀<=11).
Output a single integer.
[ "4\n2 5 3 1\n" ]
[ "4\n" ]
none
[ { "input": "4\n2 5 3 1", "output": "4" }, { "input": "2\n1 5", "output": "0" }, { "input": "1\n8", "output": "0" }, { "input": "6\n1 1 1 3 2 9", "output": "0" }, { "input": "5\n8 9 3 1 9", "output": "0" }, { "input": "6\n1 5 2 1 7 11", "output": "0...
92
0
3
8,409
384
Multitasking
[ "greedy", "implementation", "sortings", "two pointers" ]
null
null
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort *n* arrays simultaneously, each array consisting of *m* integers. Iahub can choose a pair of distinct indices *i* and *j* (1<=≀<=*i*,<=*j*<=≀<=*m*,<=*i*<=β‰ <=*j*). Then in each array the values at positions *i* and *j* are swapped ...
The first line contains three integers *n* (1<=≀<=<=*n*<=≀<=1000), *m* (1<=≀<=*m*<=≀<=<=100) and *k*. Integer *k* is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line *i* of the next *n* lines contains *m* integers separated by a space, representing the...
On the first line of the output print an integer *p*, the size of the array (*p* can be at most ). Each of the next *p* lines must contain two distinct integers *i* and *j* (1<=≀<=*i*,<=*j*<=≀<=*m*,<=*i*<=β‰ <=*j*), representing the chosen indices. If there are multiple correct answers, you can print any.
[ "2 5 0\n1 3 2 5 4\n1 4 3 2 5\n", "3 2 1\n1 2\n2 3\n3 4\n" ]
[ "3\n2 4\n2 3\n4 5\n", "1\n2 1\n" ]
Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5].
[ { "input": "2 5 0\n1 3 2 5 4\n1 4 3 2 5", "output": "3\n2 4\n2 3\n4 5" }, { "input": "3 2 1\n1 2\n2 3\n3 4", "output": "1\n2 1" }, { "input": "2 5 0\n836096 600367 472071 200387 79763\n714679 505282 233544 157810 152591", "output": "10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 ...
46
0
0
8,436
630
Hexagons!
[ "math" ]
null
null
After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might &amp; Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every...
The only line of the input contains one integer *n* (0<=≀<=*n*<=≀<=109).
Output one integer β€” the number of hexagons situated not farther than *n* cells away from a given cell.
[ "2\n" ]
[ "19" ]
none
[ { "input": "2", "output": "19" }, { "input": "0", "output": "1" }, { "input": "1", "output": "7" }, { "input": "3", "output": "37" }, { "input": "749431", "output": "1684942719577" }, { "input": "748629743", "output": "1681339478558627377" }, {...
500
1,331,200
0
8,441
797
k-Factorization
[ "implementation", "math", "number theory" ]
null
null
Given a positive integer *n*, find *k* integers (not necessary distinct) such that all these integers are strictly greater than 1, and their product is equal to *n*.
The first line contains two integers *n* and *k* (2<=≀<=*n*<=≀<=100000, 1<=≀<=*k*<=≀<=20).
If it's impossible to find the representation of *n* as a product of *k* numbers, print -1. Otherwise, print *k* integers in any order. Their product must be equal to *n*. If there are multiple answers, print any of them.
[ "100000 2\n", "100000 20\n", "1024 5\n" ]
[ "2 50000 \n", "-1\n", "2 64 2 2 2 \n" ]
none
[ { "input": "100000 2", "output": "2 50000 " }, { "input": "100000 20", "output": "-1" }, { "input": "1024 5", "output": "2 64 2 2 2 " }, { "input": "100000 10", "output": "2 2 2 2 2 5 5 5 5 5 " }, { "input": "99999 3", "output": "3 813 41 " }, { "input...
77
0
0
8,444
496
Secret Combination
[ "brute force", "constructive algorithms", "implementation" ]
null
null
You got a box with a combination lock. The lock has a display showing *n* digits. There are two buttons on the box, each button changes digits on the display. You have quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the dis...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1000)Β β€” the number of digits on the display. The second line contains *n* digitsΒ β€” the initial state of the display.
Print a single line containing *n* digitsΒ β€” the desired state of the display containing the smallest possible number.
[ "3\n579\n", "4\n2014\n" ]
[ "024\n", "0142\n" ]
none
[ { "input": "3\n579", "output": "024" }, { "input": "4\n2014", "output": "0142" }, { "input": "1\n1", "output": "0" }, { "input": "3\n039", "output": "014" }, { "input": "4\n4444", "output": "0000" }, { "input": "5\n46802", "output": "02468" }, ...
15
0
0
8,449
817
Choosing The Commander
[ "bitmasks", "data structures", "trees" ]
null
null
As you might remember from the previous round, Vova is currently playing a strategic game known as Rage of Empires. Vova managed to build a large army, but forgot about the main person in the army - the commander. So he tries to hire a commander, and he wants to choose the person who will be respected by warriors. Ea...
The first line contains one integer *q* (1<=≀<=*q*<=≀<=100000) β€” the number of events. Then *q* lines follow. Each line describes the event: - 1Β *p**i* (1<=≀<=*p**i*<=≀<=108) β€” one warrior with personality *p**i* joins Vova's army; - 2Β *p**i* (1<=≀<=*p**i*<=≀<=108) β€” one warrior with personality *p**i* leaves Vova'...
For each event of the third type print one integer β€” the number of warriors who respect the commander Vova tries to hire in the event.
[ "5\n1 3\n1 4\n3 6 3\n2 4\n3 6 3\n" ]
[ "1\n0\n" ]
In the example the army consists of two warriors with personalities 3 and 4 after first two events. Then Vova tries to hire a commander with personality 6 and leadership 3, and only one warrior respects him (<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3d8e4cd0a5c3c0cdc8b35097f3dc7317604...
[ { "input": "5\n1 3\n1 4\n3 6 3\n2 4\n3 6 3", "output": "1\n0" } ]
2,000
5,222,400
0
8,464
665
Buses Between Cities
[ "implementation" ]
null
null
Buses run between the cities *A* and *B*, the first one is at 05:00 AM and the last one departs not later than at 11:59 PM. A bus from the city *A* departs every *a* minutes and arrives to the city *B* in a *t**a* minutes, and a bus from the city *B* departs every *b* minutes and arrives to the city *A* in a *t**b* min...
The first line contains two integers *a*,<=*t**a* (1<=≀<=*a*,<=*t**a*<=≀<=120) β€” the frequency of the buses from the city *A* to the city *B* and the travel time. Both values are given in minutes. The second line contains two integers *b*,<=*t**b* (1<=≀<=*b*,<=*t**b*<=≀<=120) β€” the frequency of the buses from the city...
Print the only integer *z* β€” the number of buses Simion will meet on the way. Note that you should not count the encounters in cities *A* and *B*.
[ "10 30\n10 35\n05:20\n", "60 120\n24 100\n13:00\n" ]
[ "5\n", "9\n" ]
In the first example Simion departs form the city *A* at 05:20 AM and arrives to the city *B* at 05:50 AM. He will meet the first 5 buses from the city *B* that departed in the period [05:00 AM - 05:40 AM]. Also Simion will meet a bus in the city *B* at 05:50 AM, but he will not count it. Also note that the first enco...
[ { "input": "10 30\n10 35\n05:20", "output": "5" }, { "input": "60 120\n24 100\n13:00", "output": "9" }, { "input": "30 60\n60 60\n22:30", "output": "2" }, { "input": "30 60\n10 60\n23:30", "output": "8" }, { "input": "5 45\n4 60\n21:00", "output": "26" }, ...
77
4,608,000
3
8,467
962
Merge Equals
[ "data structures", "implementation" ]
null
null
You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value $x$ that occurs in the array $2$ or more times. Take the first two occurrences of $x$ in this array (the two leftmost occurrences). Remove the left of these two...
The first line contains a single integer $n$ ($2 \le n \le 150\,000$) β€” the number of elements in the array. The second line contains a sequence from $n$ elements $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$) β€” the elements of the array.
In the first line print an integer $k$ β€” the number of elements in the array after all the performed operations. In the second line print $k$ integers β€” the elements of the array after all the performed operations.
[ "7\n3 4 1 2 2 1 1\n", "5\n1 1 3 1 1\n", "5\n10 40 20 50 30\n" ]
[ "4\n3 8 2 1 \n", "2\n3 4 \n", "5\n10 40 20 50 30 \n" ]
The first two examples were considered in the statement. In the third example all integers in the given array are distinct, so it will not change.
[ { "input": "7\n3 4 1 2 2 1 1", "output": "4\n3 8 2 1 " }, { "input": "5\n1 1 3 1 1", "output": "2\n3 4 " }, { "input": "5\n10 40 20 50 30", "output": "5\n10 40 20 50 30 " }, { "input": "100\n10 10 15 12 15 13 15 12 10 10 15 11 13 14 13 14 10 13 12 10 14 12 13 11 14 15 12 11 1...
966
39,116,800
3
8,484
991
Candies
[ "binary search", "implementation" ]
null
null
After passing a test, Vasya got himself a box of $n$ candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself. This means the process of eating candies is the following: in the beginning Vasya cho...
The first line contains a single integer $n$ ($1 \leq n \leq 10^{18}$)Β β€” the initial amount of candies in the box.
Output a single integerΒ β€” the minimal amount of $k$ that would allow Vasya to eat at least half of candies he got.
[ "68\n" ]
[ "3\n" ]
In the sample, the amount of candies, with $k=3$, would change in the following way (Vasya eats first): $68 \to 65 \to 59 \to 56 \to 51 \to 48 \to 44 \to 41 \\ \to 37 \to 34 \to 31 \to 28 \to 26 \to 23 \to 21 \to 18 \to 17 \to 14 \\ \to 13 \to 10 \to 9 \to 6 \to 6 \to 3 \to 3 \to 0$. In total, Vasya would eat $39$ ca...
[ { "input": "68", "output": "3" }, { "input": "1", "output": "1" }, { "input": "2", "output": "1" }, { "input": "42", "output": "1" }, { "input": "43", "output": "2" }, { "input": "756", "output": "29" }, { "input": "999999972", "output"...
124
0
3
8,492
632
Longest Subsequence
[ "brute force", "math", "number theory" ]
null
null
You are given array *a* with *n* elements and the number *m*. Consider some subsequence of *a* and the value of least common multiple (LCM) of its elements. Denote LCM as *l*. Find any longest subsequence of *a* with the value *l*<=≀<=*m*. A subsequence of *a* is an array we can get by erasing some elements of *a*. It...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=106) β€” the size of the array *a* and the parameter from the problem statement. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=109) β€” the elements of *a*.
In the first line print two integers *l* and *k**max* (1<=≀<=*l*<=≀<=*m*,<=0<=≀<=*k**max*<=≀<=*n*) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print *k**max* integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can fin...
[ "7 8\n6 2 9 2 7 2 3\n", "6 4\n2 2 2 3 3 3\n" ]
[ "6 5\n1 2 4 6 7\n", "2 3\n1 2 3\n" ]
none
[ { "input": "7 8\n6 2 9 2 7 2 3", "output": "6 5\n1 2 4 6 7" }, { "input": "6 4\n2 2 2 3 3 3", "output": "2 3\n1 2 3" }, { "input": "10 50\n39 22 60 88 11 65 41 85 65 100", "output": "22 2\n2 5" }, { "input": "100 343\n999 284 486 785 176 742 856 415 992 601 600 122 460 214 33...
2,000
77,824,000
0
8,503
653
Delivery Bears
[ "binary search", "flows", "graphs" ]
null
null
Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city. In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with *n* nodes and *m* edges. Each e...
The first line contains three integers *n*, *m* and *x* (2<=≀<=*n*<=≀<=50, 1<=≀<=*m*<=≀<=500, 1<=≀<=*x*<=≀<=100<=000)Β β€” the number of nodes, the number of directed edges and the number of bears, respectively. Each of the following *m* lines contains three integers *a**i*, *b**i* and *c**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*...
Print one real value on a single lineΒ β€” the maximum amount of weight Niwel can deliver if he uses exactly *x* bears. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program wi...
[ "4 4 3\n1 2 2\n2 4 1\n1 3 1\n3 4 2\n", "5 11 23\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 3 4\n2 4 5\n3 5 6\n1 4 2\n2 5 3\n1 5 2\n3 2 30\n" ]
[ "1.5000000000\n", "10.2222222222\n" ]
In the first sample, Niwel has three bears. Two bears can choose the path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7c0aa60a06309ef607b7159fd7f3687ea0d943ce.png" style="max-width: 100.0%;max-height: 100.0%;"/>, while one bear can choose the path <img align="middle" class="tex-formula"...
[ { "input": "4 4 3\n1 2 2\n2 4 1\n1 3 1\n3 4 2", "output": "1.5000000000" }, { "input": "5 11 23\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 3 4\n2 4 5\n3 5 6\n1 4 2\n2 5 3\n1 5 2\n3 2 30", "output": "10.2222222222" }, { "input": "10 16 63\n1 2 1\n2 10 1\n1 3 1\n3 10 1\n1 4 1\n4 10 1\n1 5 1\n5 10 1\n1...
31
0
0
8,534
814
An overnight dance in discotheque
[ "dfs and similar", "dp", "geometry", "greedy", "trees" ]
null
null
The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it? The discotheque can be seen as an infinite *xy*-plane, in which there are a total of *n* dancers. Once someone starts moving around, they will move only inside their own movement range, whi...
The first line of input contains a positive integer *n* (1<=≀<=*n*<=≀<=1<=000) β€” the number of dancers. The following *n* lines each describes a dancer: the *i*-th line among them contains three space-separated integers *x**i*, *y**i* and *r**i* (<=-<=106<=≀<=*x**i*,<=*y**i*<=≀<=106, 1<=≀<=*r**i*<=≀<=106), describing ...
Output one decimal number β€” the largest achievable sum of spaciousness over two halves of the night. The output is considered correct if it has a relative or absolute error of at most 10<=-<=9. Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if .
[ "5\n2 1 6\n0 4 1\n2 -1 3\n1 -2 1\n4 -1 1\n", "8\n0 0 1\n0 0 2\n0 0 3\n0 0 4\n0 0 5\n0 0 6\n0 0 7\n0 0 8\n" ]
[ "138.23007676\n", "289.02652413\n" ]
The first sample corresponds to the illustrations in the legend.
[ { "input": "5\n2 1 6\n0 4 1\n2 -1 3\n1 -2 1\n4 -1 1", "output": "138.23007676" }, { "input": "8\n0 0 1\n0 0 2\n0 0 3\n0 0 4\n0 0 5\n0 0 6\n0 0 7\n0 0 8", "output": "289.02652413" }, { "input": "4\n1000000 -1000000 2\n1000000 -1000000 3\n-1000000 1000000 2\n-1000000 1000000 1000000", ...
779
25,600,000
3
8,574
292
Connected Components
[ "data structures", "dfs and similar", "dp", "dsu" ]
null
null
We already know of the large corporation where Polycarpus works as a system administrator. The computer network there consists of *n* computers and *m* cables that connect some pairs of computers. In other words, the computer network can be represented as some non-directed graph with *n* nodes and *m* edges. Let's inde...
The first line contains two space-separated integers *n*, *m* (2<=≀<=*n*<=≀<=500;Β 1<=≀<=*m*<=≀<=104) β€” the number of computers and the number of cables, correspondingly. The following *m* lines contain the cables' description. The *i*-th line contains space-separated pair of integers *x**i*, *y**i* (1<=≀<=*x**i*,<=*y*...
Print *k* numbers, the *i*-th number represents the number of connected components of the graph that defines the computer network during the *i*-th experiment.
[ "6 5\n1 2\n5 4\n2 3\n3 1\n3 6\n6\n1 3\n2 5\n1 5\n5 5\n2 4\n3 3\n" ]
[ "4\n5\n6\n3\n4\n2\n" ]
none
[ { "input": "6 5\n1 2\n5 4\n2 3\n3 1\n3 6\n6\n1 3\n2 5\n1 5\n5 5\n2 4\n3 3", "output": "4\n5\n6\n3\n4\n2" }, { "input": "2 1\n2 1\n2\n1 1\n1 1", "output": "2\n2" }, { "input": "3 2\n3 2\n3 1\n4\n1 1\n1 2\n2 2\n2 2", "output": "2\n3\n2\n2" }, { "input": "3 3\n2 3\n3 1\n2 1\n5\n...
186
7,168,000
0
8,590
17
Notepad
[ "number theory" ]
D. Notepad
2
64
Nick is attracted by everything unconventional. He doesn't like decimal number system any more, and he decided to study other number systems. A number system with base *b* caught his attention. Before he starts studying it, he wants to write in his notepad all the numbers of length *n* without leading zeros in this num...
The only input line contains three space-separated integers *b*, *n* and *c* (2<=≀<=*b*<=&lt;<=10106, 1<=≀<=*n*<=&lt;<=10106, 1<=≀<=*c*<=≀<=109). You may consider that Nick has infinite patience, endless amount of paper and representations of digits as characters. The numbers doesn't contain leading zeros.
In the only line output the amount of numbers written on the same page as the last number.
[ "2 3 3\n", "2 3 4\n" ]
[ "1", "4" ]
In both samples there are exactly 4 numbers of length 3 in binary number system. In the first sample Nick writes 3 numbers on the first page and 1 on the second page. In the second sample all the 4 numbers can be written on the first page.
[ { "input": "2 3 3", "output": "1" }, { "input": "2 3 4", "output": "4" }, { "input": "9 1 79", "output": "8" }, { "input": "9 1 345", "output": "8" }, { "input": "9 9 999982045", "output": "344373768" }, { "input": "4 42 44", "output": "12" }, ...
248
20,480,000
0
8,595
178
Educational Game
[]
null
null
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of *n* non-negative integers *a**i* numbered from 1 to *n*. The goal of the game is to make numbers *a*1,<=*a*2,<=...,<=*a**k* (i.e. some pr...
The first input line contains a single integer *n*. The second line contains *n* integers *a**i* (0<=≀<=*a**i*<=≀<=104), separated by single spaces. The input limitations for getting 20 points are: - 1<=≀<=*n*<=≀<=300 The input limitations for getting 50 points are: - 1<=≀<=*n*<=≀<=2000 The input limitations f...
Print exactly *n*<=-<=1 lines: the *k*-th output line must contain the minimum number of moves needed to make the first *k* elements of the original sequence *a**i* equal to zero. 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 sp...
[ "4\n1 0 1 2\n", "8\n1 2 3 4 5 6 7 8\n" ]
[ "1\n1\n3\n", "1\n3\n6\n10\n16\n24\n40\n" ]
none
[ { "input": "4\n1 0 1 2", "output": "1\n1\n3" }, { "input": "8\n1 2 3 4 5 6 7 8", "output": "1\n3\n6\n10\n16\n24\n40" }, { "input": "5\n4 1 4 7 6", "output": "4\n5\n9\n17" }, { "input": "9\n13 13 7 11 3 9 3 5 5", "output": "13\n26\n33\n44\n47\n69\n79\n117" }, { "in...
186
1,331,200
3
8,597
770
Online Courses In BSU
[ "*special", "dfs and similar", "graphs", "implementation" ]
null
null
Now you can take online courses in the Berland State University! Polycarp needs to pass *k* main online courses of his specialty to get a diploma. In total *n* courses are availiable for the passage. The situation is complicated by the dependence of online courses, for each course there is a list of those that must be...
The first line contains *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=105) β€” the number of online-courses and the number of main courses of Polycarp's specialty. The second line contains *k* distinct integers from 1 to *n* β€” numbers of main online-courses of Polycarp's specialty. Then *n* lines follow, each of them describes ...
Print -1, if there is no the way to get a specialty. Otherwise, in the first line print the integer *m* β€” the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print *m* distinct integers β€” numbers of courses which it is necessary to pass in the chronological order ...
[ "6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5\n", "9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2\n", "3 3\n1 2 3\n1 2\n1 3\n1 1\n" ]
[ "5\n1 2 3 4 5 \n", "6\n1 2 9 4 5 3 \n", "-1\n" ]
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
[ { "input": "6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5", "output": "5\n1 2 3 4 5 " }, { "input": "9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2", "output": "6\n1 2 9 4 5 3 " }, { "input": "3 3\n1 2 3\n1 2\n1 3\n1 1", "output": "-1" }, { "input": "5 3\n2 1 4\n0\n0\n1 5\n0\n0",...
951
40,243,200
3
8,605
787
Not Afraid
[ "greedy", "implementation", "math" ]
null
null
Since the giant heads have appeared in the sky all humanity is in danger, so all Ricks and Mortys from all parallel universes are gathering in groups to find a solution to get rid of them. There are *n* parallel universes participating in this event (*n* Ricks and *n* Mortys). I. e. each of *n* universes has one Rick...
The first line of input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=104) β€” number of universes and number of groups respectively. The next *m* lines contain the information about the groups. *i*-th of them first contains an integer *k* (number of times someone joined *i*-th group, *k*<=&gt;<=0) followed by ...
In a single line print the answer to Summer's question. Print "YES" if she should cancel the event and "NO" otherwise.
[ "4 2\n1 -3\n4 -2 3 2 -3\n", "5 2\n5 3 -2 1 -1 5\n3 -5 2 5\n", "7 2\n3 -1 6 7\n7 -5 4 2 4 7 -3 4\n" ]
[ "YES\n", "NO\n", "YES\n" ]
In the first sample testcase, 1st group only contains the Rick from universe number 3, so in case he's a traitor, then all members of this group are traitors and so Summer should cancel the event.
[ { "input": "4 2\n1 -3\n4 -2 3 2 -3", "output": "YES" }, { "input": "5 2\n5 3 -2 1 -1 5\n3 -5 2 5", "output": "NO" }, { "input": "7 2\n3 -1 6 7\n7 -5 4 2 4 7 -3 4", "output": "YES" }, { "input": "2 1\n2 -2 2", "output": "NO" }, { "input": "7 7\n1 -2\n1 6\n2 7 -6\n2...
62
4,608,000
0
8,609
527
Error Correct System
[ "greedy" ]
null
null
Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings *S* and *T* of equal length to be "similar". After a brief search on th...
The first line contains integer *n* (1<=≀<=*n*<=≀<=200<=000) β€” the length of strings *S* and *T*. The second line contains string *S*. The third line contains string *T*. Each of the lines only contains lowercase Latin letters.
In the first line, print number *x* β€” the minimum possible Hamming distance between strings *S* and *T* if you swap at most one pair of letters in *S*. In the second line, either print the indexes *i* and *j* (1<=≀<=*i*,<=*j*<=≀<=*n*, *i*<=β‰ <=*j*), if reaching the minimum possible distance is possible by swapping lett...
[ "9\npergament\npermanent\n", "6\nwookie\ncookie\n", "4\npetr\negor\n", "6\ndouble\nbundle\n" ]
[ "1\n4 6\n", "1\n-1 -1\n", "2\n1 2\n", "2\n4 1\n" ]
In the second test it is acceptable to print *i* = 2, *j* = 3.
[ { "input": "9\npergament\npermanent", "output": "1\n4 6" }, { "input": "6\nwookie\ncookie", "output": "1\n-1 -1" }, { "input": "4\npetr\negor", "output": "2\n1 2" }, { "input": "6\ndouble\nbundle", "output": "2\n4 1" }, { "input": "1\na\na", "output": "0\n-1 -...
109
20,172,800
0
8,641
437
The Child and Zoo
[ "dsu", "sortings" ]
null
null
Of course our child likes walking in a zoo. The zoo has *n* areas, that are numbered from 1 to *n*. The *i*-th area contains *a**i* animals in it. Also there are *m* roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo from any other area usin...
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=105; 0<=≀<=*m*<=≀<=105). The second line contains *n* integers: *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=105). Then follow *m* lines, each line contains two integers *x**i* and *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*; *x**i*<=β‰ <=*y**i*), denoting the roa...
Output a real number β€” the value of . The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=4.
[ "4 3\n10 20 30 40\n1 3\n2 3\n4 3\n", "3 3\n10 20 30\n1 2\n2 3\n3 1\n", "7 8\n40 20 10 30 20 50 40\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n1 4\n5 7\n" ]
[ "16.666667\n", "13.333333\n", "18.571429\n" ]
Consider the first sample. There are 12 possible situations: - *p* = 1, *q* = 3, *f*(*p*, *q*) = 10. - *p* = 2, *q* = 3, *f*(*p*, *q*) = 20. - *p* = 4, *q* = 3, *f*(*p*, *q*) = 30. - *p* = 1, *q* = 2, *f*(*p*, *q*) = 10. - *p* = 2, *q* = 4, *f*(*p*, *q*) = 20. - *p* = 4, *q* = 1, *f*(*p*, *q*) = 10. Another 6 c...
[ { "input": "4 3\n10 20 30 40\n1 3\n2 3\n4 3", "output": "16.666667" }, { "input": "3 3\n10 20 30\n1 2\n2 3\n3 1", "output": "13.333333" }, { "input": "7 8\n40 20 10 30 20 50 40\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n1 4\n5 7", "output": "18.571429" }, { "input": "10 14\n594 965 90 32...
2,000
8,908,800
0
8,642
818
Level Generation
[ "binary search", "math", "ternary search" ]
null
null
Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level. Ivan decided that there should be exactly *n**i* vertices in the graph representing level *i*, and the edges have to be bidirectional. ...
The first line of input file contains a positive integer *q* (1<=≀<=*q*<=≀<=100<=000) β€” the number of graphs Ivan needs to construct. Then *q* lines follow, *i*-th line contains one positive integer *n**i* (1<=≀<=*n**i*<=≀<=2Β·109) β€” the number of vertices in *i*-th graph. Note that in hacks you have to use *q*<==<=1.
Output *q* numbers, *i*-th of them must be equal to the maximum number of edges in *i*-th graph.
[ "3\n3\n4\n6\n" ]
[ "2\n3\n6\n" ]
In the first example it is possible to construct these graphs: 1. 1 - 2, 1 - 3; 1. 1 - 2, 1 - 3, 2 - 4; 1. 1 - 2, 1 - 3, 2 - 3, 1 - 4, 2 - 5, 3 - 6.
[ { "input": "3\n3\n4\n6", "output": "2\n3\n6" }, { "input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10", "output": "0\n1\n2\n3\n4\n6\n7\n8\n10\n12" }, { "input": "1\n212055293", "output": "424069398" }, { "input": "1\n508427854", "output": "1016791932" }, { "input": "1\n39...
171
10,444,800
3
8,671
550
Divisibility by Eight
[ "brute force", "dp", "math" ]
null
null
You are given a non-negative integer *n*, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes. Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a no...
The single line of the input contains a non-negative integer *n*. The representation of number *n* doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
Print "NO" (without quotes), if there is no such way to remove some digits from number *n*. Otherwise, print "YES" in the first line and the resulting number after removing digits from number *n* in the second line. The printed number must be divisible by 8. If there are multiple possible answers, you may print any ...
[ "3454\n", "10\n", "111111\n" ]
[ "YES\n344\n", "YES\n0\n", "NO\n" ]
none
[ { "input": "3454", "output": "YES\n344" }, { "input": "10", "output": "YES\n0" }, { "input": "111111", "output": "NO" }, { "input": "8996988892", "output": "YES\n8" }, { "input": "5555555555", "output": "NO" }, { "input": "1", "output": "NO" }, ...
1,669
268,390,400
0
8,672
652
Foe Pairs
[ "combinatorics", "sortings", "two pointers" ]
null
null
You are given a permutation *p* of length *n*. Also you are given *m* foe pairs (*a**i*,<=*b**i*) (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*,<=*a**i*<=β‰ <=*b**i*). Your task is to count the number of different intervals (*x*,<=*y*) (1<=≀<=*x*<=≀<=*y*<=≀<=*n*) that do not contain any foe pairs. So you shouldn't count intervals (*x...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=3Β·105) β€” the length of the permutation *p* and the number of foe pairs. The second line contains *n* distinct integers *p**i* (1<=≀<=*p**i*<=≀<=*n*) β€” the elements of the permutation *p*. Each of the next *m* lines contains two integers (*a**i*,<=*...
Print the only integer *c* β€” the number of different intervals (*x*,<=*y*) that does not contain any foe pairs. Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
[ "4 2\n1 3 2 4\n3 2\n2 4\n", "9 5\n9 7 2 3 1 4 6 5 8\n1 6\n4 5\n2 7\n7 2\n2 7\n" ]
[ "5\n", "20\n" ]
In the first example the intervals from the answer are (1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).
[ { "input": "4 2\n1 3 2 4\n3 2\n2 4", "output": "5" }, { "input": "9 5\n9 7 2 3 1 4 6 5 8\n1 6\n4 5\n2 7\n7 2\n2 7", "output": "20" }, { "input": "2 1\n1 2\n1 2", "output": "2" }, { "input": "10 3\n4 10 5 1 6 8 9 2 3 7\n10 5\n2 10\n4 1", "output": "39" }, { "input"...
1,000
13,824,000
0
8,678
939
Love Rescue
[ "dfs and similar", "dsu", "graphs", "greedy", "strings" ]
null
null
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long. This story c...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the length of the letterings. The second line contains a string with length *n*, consisting of lowercase English lettersΒ β€” the lettering on Valya's pullover. The third line contains the lettering on Tolya's t-shirt in the same format.
In the first line output a single integerΒ β€” the minimum amount of mana *t* required for rescuing love of Valya and Tolya. In the next *t* lines output pairs of space-separated lowercase English lettersΒ β€” spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order. If there are...
[ "3\nabb\ndad\n", "8\ndrpepper\ncocacola\n" ]
[ "2\na d\nb a", "7\nl e\ne d\nd c\nc p\np o\no r\nr a\n" ]
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
[ { "input": "3\nabb\ndad", "output": "2\nb d\nd a" }, { "input": "8\ndrpepper\ncocacola", "output": "7\nl e\ne d\nd c\nc p\np o\no r\nr a" }, { "input": "1\nh\np", "output": "1\np h" }, { "input": "2\nxc\nda", "output": "2\nc a\nx d" }, { "input": "3\nbab\naab", ...
202
7,577,600
3
8,713
827
High Load
[ "constructive algorithms", "greedy", "implementation", "trees" ]
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...
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 ...
[ "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" }, {...
62
3,072,000
0
8,729
195
Try and Catch
[ "expression parsing", "implementation" ]
null
null
Vasya is developing his own programming language VPL (Vasya Programming Language). Right now he is busy making the system of exceptions. He thinks that the system of exceptions must function like that. The exceptions are processed by try-catch-blocks. There are two operators that work with the blocks: 1. The try oper...
The first line contains a single integer: *n* (1<=≀<=*n*<=≀<=105) the number of lines in the program. Next *n* lines contain the program in language VPL. Each line contains no more than one operator. It means that input file can contain empty lines and lines, consisting only of spaces. The program contains only operat...
Print the message the screen will show after the given program is executed.
[ "8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")\n", "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE, \"BE in line 5\") \ncatch(AE,\"AE somewhere\")\n", "8\ntry\...
[ "AE somewhere\n", "AE in line 3\n", "Unhandled Exception\n" ]
In the first sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(BE,"BE in line 3") and try-catch(AE,"AE somewhere"). Exception type is AE, so the second block will be activated, because operator catch(A...
[ { "input": "8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")", "output": "AE somewhere" }, { "input": "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE,...
92
307,200
0
8,737
43
Journey
[ "brute force", "constructive algorithms", "implementation" ]
D. Journey
2
256
The territory of Berland is represented by a rectangular field *n*<=Γ—<=*m* in size. The king of Berland lives in the capital, located on the upper left square (1,<=1). The lower right square has coordinates (*n*,<=*m*). One day the king decided to travel through the whole country and return back to the capital, having ...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100,<=2<=≀<= *n* Β· *m*) β€” the field size. The upper left square has coordinates (1,<=1), and the lower right square has coordinates of (*n*,<=*m*).
On the first line output integer *k* β€” the minimum number of teleporters. Then output *k* lines each containing 4 integers *x*1 *y*1 *x*2 *y*2 (1<=≀<=*x*1,<=*x*2<=≀<=*n*,<=1<=≀<=*y*1,<=*y*2<=≀<=*m*) β€” the coordinates of the square where the teleporter is installed (*x*1,<=*y*1), and the coordinates of the square where ...
[ "2 2\n", "3 3\n" ]
[ "0\n1 1\n1 2\n2 2\n2 1\n1 1\n", "1\n3 3 1 1\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n1 1\n" ]
none
[ { "input": "2 2", "output": "0\n1 1\n1 2\n2 2\n2 1\n1 1" }, { "input": "3 3", "output": "1\n3 3 1 1\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n1 1" }, { "input": "3 2", "output": "0\n1 1\n2 1\n3 1\n3 2\n2 2\n1 2\n1 1" }, { "input": "2 3", "output": "0\n1 1\n1 2\n1 3\n2...
0
0
-1
8,742
593
Anton and Lines
[ "geometry", "sortings" ]
null
null
The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of *n* lines defined by the equations *y*<==<=*k**i*Β·*x*<=+<=*b**i*. It was necessary to determine whether there is at least one point of intersection of t...
The first line of the input contains an integer *n* (2<=≀<=*n*<=≀<=100<=000)Β β€” the number of lines in the task given to Anton. The second line contains integers *x*1 and *x*2 (<=-<=1<=000<=000<=≀<=*x*1<=&lt;<=*x*2<=≀<=1<=000<=000) defining the strip inside which you need to find a point of intersection of at least two ...
Print "Yes" (without quotes), if there is at least one intersection of two distinct lines, located strictly inside the strip. Otherwise print "No" (without quotes).
[ "4\n1 2\n1 2\n1 0\n0 1\n0 2\n", "2\n1 3\n1 0\n-1 3\n", "2\n1 3\n1 0\n0 2\n", "2\n1 3\n1 0\n0 3\n" ]
[ "NO", "YES", "YES", "NO" ]
In the first sample there are intersections located on the border of the strip, but there are no intersections located strictly inside it.
[ { "input": "4\n1 2\n1 2\n1 0\n0 1\n0 2", "output": "NO" }, { "input": "2\n1 3\n1 0\n-1 3", "output": "YES" }, { "input": "2\n1 3\n1 0\n0 2", "output": "YES" }, { "input": "2\n1 3\n1 0\n0 3", "output": "NO" }, { "input": "2\n0 1\n-1000000 1000000\n1000000 -1000000"...
0
0
-1
8,750
75
Big Maximum Sum
[ "data structures", "dp", "greedy", "implementation", "math", "trees" ]
D. Big Maximum Sum
2
256
Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't. This problem is similar to a standard problem but it has a different format and constraints. In the stand...
The first line contains two integers *n* and *m*, *n* is the number of the small arrays (1<=≀<=*n*<=≀<=50), and *m* is the number of indexes in the big array (1<=≀<=*m*<=≀<=250000). Then follow *n* lines, the *i*-th line starts with one integer *l* which is the size of the *i*-th array (1<=≀<=*l*<=≀<=5000), followed by...
Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).
[ "3 4\n3 1 6 -2\n2 3 3\n2 -5 1\n2 3 1 3\n", "6 1\n4 0 8 -3 -10\n8 3 -2 -5 10 8 -9 -5 -4\n1 0\n1 -3\n3 -8 5 6\n2 9 6\n1\n" ]
[ "9\n", "8\n" ]
none
[ { "input": "3 4\n3 1 6 -2\n2 3 3\n2 -5 1\n2 3 1 3", "output": "9" }, { "input": "6 1\n4 0 8 -3 -10\n8 3 -2 -5 10 8 -9 -5 -4\n1 0\n1 -3\n3 -8 5 6\n2 9 6\n1", "output": "8" }, { "input": "4 3\n6 6 8 -5 4 10 -2\n1 -2\n1 -10\n5 -10 10 8 -7 -10\n2 4 1", "output": "24" }, { "input"...
2,000
23,859,200
0
8,758
484
Maximum Value
[ "binary search", "math", "sortings", "two pointers" ]
null
null
You are given a sequence *a* consisting of *n* integers. Find the maximum possible value of (integer remainder of *a**i* divided by *a**j*), where 1<=≀<=*i*,<=*j*<=≀<=*n* and *a**i*<=β‰₯<=*a**j*.
The first line contains integer *n*Β β€” the length of the sequence (1<=≀<=*n*<=≀<=2Β·105). The second line contains *n* space-separated integers *a**i* (1<=≀<=*a**i*<=≀<=106).
Print the answer to the problem.
[ "3\n3 4 5\n" ]
[ "2\n" ]
none
[ { "input": "3\n3 4 5", "output": "2" }, { "input": "3\n1 2 4", "output": "0" }, { "input": "1\n1", "output": "0" }, { "input": "1\n1000000", "output": "0" }, { "input": "2\n1000000 999999", "output": "1" }, { "input": "12\n4 4 10 13 28 30 41 43 58 61 7...
1,000
20,992,000
0
8,798
543
Listening to Music
[ "constructive algorithms", "data structures" ]
null
null
Please note that the memory limit differs from the standard. You really love to listen to music. During the each of next *s* days you will listen to exactly *m* songs from the playlist that consists of exactly *n* songs. Let's number the songs from the playlist with numbers from 1 to *n*, inclusive. The quality of son...
The first line contains two positive integers *n*, *m* (1<=≀<=*m*<=≀<=*n*<=≀<=2Β·105). The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=&lt;<=230) β€” the description of songs from the playlist. The next line contains a single number *s* (1<=≀<=*s*<=≀<=2Β·105) β€” the number of days ...
Print exactly *s* integers *ans*1,<=*ans*2,<=...,<=*ans**s*, where *ans**i* is the minimum displeasure that you can get on day *i*.
[ "5 3\n1 2 1 2 3\n5\n1 1 2\n1 3 2\n1 3 3\n1 3 5\n1 3 1\n" ]
[ "2\n0\n2\n3\n1\n" ]
none
[]
7,000
23,244,800
0
8,807
117
Cycle
[ "dfs and similar", "graphs" ]
null
null
A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes *u* and *v* (*u*<=β‰ <=*v*) exists either an edge going from *u* to *v*, or an edge from *v* to *u*. You are given a tournament consisting of *n* vertexes. Your tas...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=5000). Next *n* lines contain the adjacency matrix *A* of the graph (without spaces). *A**i*,<=*j*<==<=1 if the graph has an edge going from vertex *i* to vertex *j*, otherwise *A**i*,<=*j*<==<=0. *A**i*,<=*j* stands for the *j*-th character in the *i*-th line. It ...
Print three distinct vertexes of the graph *a*1, *a*2, *a*3 (1<=≀<=*a**i*<=≀<=*n*), such that *A**a*1,<=*a*2<==<=*A**a*2,<=*a*3<==<=*A**a*3,<=*a*1<==<=1, or "-1", if a cycle whose length equals three does not exist. If there are several solutions, print any of them.
[ "5\n00100\n10000\n01001\n11101\n11000\n", "5\n01111\n00000\n01000\n01100\n01110\n" ]
[ "1 3 2 ", "-1\n" ]
none
[ { "input": "5\n00100\n10000\n01001\n11101\n11000", "output": "1 3 2 " }, { "input": "5\n01111\n00000\n01000\n01100\n01110", "output": "-1" }, { "input": "5\n01000\n00101\n10010\n11001\n10100", "output": "1 2 3 " }, { "input": "5\n00110\n10110\n00011\n00000\n11010", "outpu...
92
0
0
8,808
76
Plus and xor
[ "dp", "greedy", "math" ]
D. Plus and xor
0
256
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions ...
The first line contains integer number *A* and the second line contains integer number *B* (0<=≀<=*A*,<=*B*<=≀<=264<=-<=1).
The only output line should contain two integer non-negative numbers *X* and *Y*. Print the only number -1 if there is no answer.
[ "142\n76\n" ]
[ "33 109\n" ]
none
[ { "input": "142\n76", "output": "33 109" }, { "input": "638\n126", "output": "256 382" }, { "input": "1639\n1176", "output": "-1" }, { "input": "12608\n0", "output": "6304 6304" }, { "input": "104066\n104066", "output": "0 104066" }, { "input": "102499...
93
0
0
8,821
524
Π€ΠΎΡ‚ΠΎ Π½Π° ΠΏΠ°ΠΌΡΡ‚ΡŒ - 2 (round version)
[ "dp", "greedy" ]
null
null
ΠŸΡ€ΠΎΡˆΠ»ΠΎ ΠΌΠ½ΠΎΠ³ΠΎ Π»Π΅Ρ‚, ΠΈ Π½Π° Π²Π΅Ρ‡Π΅Ρ€ΠΈΠ½ΠΊΠ΅ снова Π²ΡΡ‚Ρ€Π΅Ρ‚ΠΈΠ»ΠΈΡΡŒ *n* Π΄Ρ€ΡƒΠ·Π΅ΠΉ. Π‘ ΠΌΠΎΠΌΠ΅Π½Ρ‚Π° послСднСй встрСчи Ρ‚Π΅Ρ…Π½ΠΈΠΊΠ° ΡˆΠ°Π³Π½ΡƒΠ»Π° Π΄Π°Π»Π΅ΠΊΠΎ Π²ΠΏΠ΅Ρ€Ρ‘Π΄, появились Ρ„ΠΎΡ‚ΠΎΠ°ΠΏΠΏΠ°Ρ€Π°Ρ‚Ρ‹ с автоспуском, ΠΈ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π½Π΅ трСбуСтся, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΠ΄ΠΈΠ½ ΠΈΠ· Π΄Ρ€ΡƒΠ·Π΅ΠΉ стоял с Ρ„ΠΎΡ‚ΠΎΠ°ΠΏΠΏΠ°Ρ€Π°Ρ‚ΠΎΠΌ, ΠΈ, Ρ‚Π΅ΠΌ самым, оказывался Π½Π΅ Π·Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π»Ρ‘Π½Π½Ρ‹ΠΌ Π½Π° снимкС. Π£ΠΏΡ€ΠΎΡ‰Π΅Π½Π½ΠΎ процСсс фотографирования ΠΌΠΎΠΆΠ½...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС слСдуСт Ρ†Π΅Π»ΠΎΠ΅ число *n* (1<=≀<=*n*<=≀<=1000) β€” количСство Π΄Ρ€ΡƒΠ·Π΅ΠΉ. Π’ ΠΏΠΎΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΡ… *n* строках ΡΠ»Π΅Π΄ΡƒΡŽΡ‚ ΠΏΠΎ Π΄Π²Π° Ρ†Π΅Π»Ρ‹Ρ… числа *w**i*,<=*h**i* (1<=≀<=*w**i*,<=*h**i*<=≀<=1000), ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°ΡŽΡ‰ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ°, ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰Π΅Π³ΠΎ *i*-ΠΌΡƒ ΠΈΠ· Π΄Ρ€ΡƒΠ·Π΅ΠΉ.
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ СдинствСнноС Ρ†Π΅Π»ΠΎΠ΅ число, Ρ€Π°Π²Π½ΠΎΠ΅ минимальной Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠΉ ΠΏΠ»ΠΎΡ‰Π°Π΄ΠΈ Ρ„ΠΎΡ‚ΠΎΠ³Ρ€Π°Ρ„ΠΈΠΈ, Π²ΠΌΠ΅Ρ‰Π°ΡŽΡ‰Π΅ΠΉ всСх Π΄Ρ€ΡƒΠ·Π΅ΠΉ.
[ "3\n10 1\n20 2\n30 3\n", "3\n3 1\n2 2\n4 3\n", "1\n5 10\n" ]
[ "180\n", "21\n", "50\n" ]
none
[ { "input": "3\n10 1\n20 2\n30 3", "output": "180" }, { "input": "3\n3 1\n2 2\n4 3", "output": "21" }, { "input": "1\n5 10", "output": "50" }, { "input": "10\n168 538\n836 439\n190 873\n206 47\n891 591\n939 481\n399 898\n859 466\n701 777\n629 222", "output": "3478056" },...
62
0
0
8,829
0
none
[ "none" ]
null
null
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has *n* mandatory quests. To perform each of them, you need to take exactly two companions. The attitude of each of the companions to the hero is an integer. Initially, the attitude of each...
The first line contains positive integer *n* (1<=≀<=*n*<=≀<=25) β€” the number of important tasks. Next *n* lines contain the descriptions of the tasks β€” the *i*-th line contains three integers *l**i*,<=*m**i*,<=*w**i* β€” the values by which the attitude of Lynn, Meliana and Worrigan respectively will change towards the...
If there is no solution, print in the first line "Impossible". Otherwise, print *n* lines, two characters is each line β€” in the *i*-th line print the first letters of the companions' names that hero should take to complete the *i*-th task ('L' for Lynn, 'M' for Meliana, 'W' for Worrigan). Print the letters in any orde...
[ "3\n1 0 0\n0 1 0\n0 0 1\n", "7\n0 8 9\n5 9 -2\n6 -8 -7\n9 4 5\n-4 -9 9\n-4 5 2\n-6 8 -7\n", "2\n1 0 0\n1 1 0\n" ]
[ "LM\nMW\nMW\n", "LM\nMW\nLM\nLW\nMW\nLM\nLW\n", "Impossible\n" ]
none
[ { "input": "3\n1 0 0\n0 1 0\n0 0 1", "output": "LW\nMW\nMW" }, { "input": "7\n0 8 9\n5 9 -2\n6 -8 -7\n9 4 5\n-4 -9 9\n-4 5 2\n-6 8 -7", "output": "LM\nMW\nLM\nLW\nMW\nLM\nLW" }, { "input": "2\n1 0 0\n1 1 0", "output": "Impossible" }, { "input": "25\n26668 10412 12658\n25216 1...
1,949
103,219,200
3
8,859
848
From Y to Y
[ "constructive algorithms" ]
null
null
From beginning till end, this message has been waiting to be conveyed. For a given unordered multiset of *n* lowercase English letters ("multi" means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation *n*<=-<=1 times: - Remove any two elements *s...
The first and only line of input contains a non-negative integer *k* (0<=≀<=*k*<=≀<=100<=000) β€” the required minimum cost.
Output a non-empty string of no more than 100<=000 lowercase English letters β€” any multiset satisfying the requirements, concatenated to be a string. Note that the printed string doesn't need to be the final concatenated string. It only needs to represent an unordered multiset of letters.
[ "12\n", "3\n" ]
[ "abababab\n", "codeforces\n" ]
For the multiset {'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'}, one of the ways to complete the process is as follows: - {"ab", "a", "b", "a", "b", "a", "b"}, with a cost of 0; - {"aba", "b", "a", "b", "a", "b"}, with a cost of 1; - {"abab", "a", "b", "a", "b"}, with a cost of 1; - {"abab", "ab", "a", "b"}, with a cost...
[ { "input": "12", "output": "abababab" }, { "input": "3", "output": "codeforces" }, { "input": "0", "output": "o" }, { "input": "2", "output": "aabb" }, { "input": "5", "output": "aaabbcc" }, { "input": "10", "output": "aaaaa" }, { "input": ...
30
0
0
8,916
464
No to Palindromes!
[ "greedy", "strings" ]
null
null
Paul hates palindromes. He assumes that string *s* is tolerable if each its character is one of the first *p* letters of the English alphabet and *s* doesn't contain any palindrome contiguous substring of length 2 or more. Paul has found a tolerable string *s* of length *n*. Help him find the lexicographically next to...
The first line contains two space-separated integers: *n* and *p* (1<=≀<=*n*<=≀<=1000; 1<=≀<=*p*<=≀<=26). The second line contains string *s*, consisting of *n* small English letters. It is guaranteed that the string is tolerable (according to the above definition).
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
[ "3 3\ncba\n", "3 4\ncba\n", "4 4\nabcd\n" ]
[ "NO\n", "cbd\n", "abda\n" ]
String *s* is lexicographically larger (or simply larger) than string *t* with the same length, if there is number *i*, such that *s*<sub class="lower-index">1</sub> = *t*<sub class="lower-index">1</sub>, ..., *s*<sub class="lower-index">*i*</sub> = *t*<sub class="lower-index">*i*</sub>, *s*<sub class="lower-index">*i*...
[ { "input": "3 3\ncba", "output": "NO" }, { "input": "3 4\ncba", "output": "cbd" }, { "input": "4 4\nabcd", "output": "abda" }, { "input": "2 2\nab", "output": "ba" }, { "input": "2 2\nba", "output": "NO" }, { "input": "1 2\na", "output": "b" }, ...
62
0
0
8,932
42
Game of chess unfinished
[ "implementation" ]
B. Game of chess unfinished
2
256
Once Volodya was at the museum and saw a regular chessboard as a museum piece. And there were only four chess pieces on it: two white rooks, a white king and a black king. "Aha, blacks certainly didn't win!", β€” Volodya said and was right for sure. And your task is to say whether whites had won or not. Pieces on the ch...
The input contains 4 space-separated piece positions: positions of the two rooks, the white king and the black king. Each position on 8<=Γ—<=8 chessboard is denoted by two symbols β€” ('a' - 'h') and ('1' - '8') β€” which stand for horizontal and vertical coordinates of the cell occupied by the piece. It is guaranteed, that...
Output should contain one word: "CHECKMATE" if whites mate blacks, and "OTHER" otherwise.
[ "a6 b4 c8 a8\n", "a6 c4 b6 b8\n", "a2 b1 a3 a1\n" ]
[ "CHECKMATE\n", "OTHER\n", "OTHER\n" ]
none
[ { "input": "a6 b4 c8 a8", "output": "CHECKMATE" }, { "input": "a6 c4 b6 b8", "output": "OTHER" }, { "input": "a2 b1 a3 a1", "output": "OTHER" }, { "input": "a5 c5 c2 a1", "output": "CHECKMATE" }, { "input": "a5 c5 c3 a1", "output": "OTHER" }, { "input"...
154
512,000
0
8,945
785
Anton and Fairy Tale
[ "binary search", "math" ]
null
null
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale: "Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that ba...
The only line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=1018)Β β€” the capacity of the barn and the number of grains that are brought every day.
Output one integerΒ β€” the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
[ "5 2\n", "8 1\n" ]
[ "4\n", "5\n" ]
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens: - At the beginning of the first day grain is brought to the barn. It's full, so nothing happens. - At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain. - ...
[ { "input": "5 2", "output": "4" }, { "input": "8 1", "output": "5" }, { "input": "32 5", "output": "12" }, { "input": "1024 1024", "output": "1024" }, { "input": "58044 52909", "output": "53010" }, { "input": "996478063 658866858", "output": "65889...
1,000
1,331,200
0
8,967
311
Cats Transport
[ "data structures", "dp" ]
null
null
Zxr960115 is owner of a large farm. He feeds *m* cute cats and employs *p* feeders. There's a straight road across the farm and *n* hills along the road, numbered from 1 to *n* from left to right. The distance between hill *i* and (*i*<=-<=1) is *d**i* meters. The feeders live in hill 1. One day, the cats went out to ...
The first line of the input contains three integers *n*,<=*m*,<=*p* (2<=≀<=*n*<=≀<=105,<=1<=≀<=*m*<=≀<=105,<=1<=≀<=*p*<=≀<=100). The second line contains *n*<=-<=1 positive integers *d*2,<=*d*3,<=...,<=*d**n* (1<=≀<=*d**i*<=&lt;<=104). Each of the next *m* lines contains two integers *h**i* and *t**i* (1<=≀<=*h**i*<=...
Output an integer, the minimum sum of waiting time of all cats. 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.
[ "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12\n" ]
[ "3\n" ]
none
[ { "input": "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12", "output": "3" } ]
46
0
0
8,971
207
The Beaver's Problem - 3
[]
null
null
The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem: You've got some training set of documents. For each document you know its su...
The first line contains integer *id* (0<=≀<=*id*<=≀<=106) β€” the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes. The tests for this problem are ...
Print an integer from 1 to 3, inclusive β€” the number of the subject the given document corresponds to.
[]
[]
none
[ { "input": "2000\nJAPAN FEBRUARY MONEY SUPPLY RISES 8.8 PCT\nTOKYO, March 17 - Japan's broadly defined money supply\naverage of M-2 plus certificate of deposits (CDs) rose a\npreliminary 8.8 pct in February from a year earlier, compared\nwith an 8.6 pct rise in January, the Bank of Japan said.\nThe seasonally a...
30
0
0
8,994
29
Mail Stamps
[ "data structures", "dfs and similar", "graphs", "implementation" ]
C. Mail Stamps
2
256
One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city Β«AΒ» to city Β«BΒ», they stamp it with Β«A BΒ», or Β«B AΒ». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that's why the letter is sen...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” amount of mail stamps on the envelope. Then there follow *n* lines with two integers each β€” description of the stamps. Each stamp is described with indexes of the cities between which a letter is sent. The indexes of cities are integers from 1 to 109. Indexes of...
Output *n*<=+<=1 numbers β€” indexes of cities in one of the two possible routes of the letter.
[ "2\n1 100\n100 2\n", "3\n3 1\n100 2\n3 2\n" ]
[ "2 100 1 ", "100 2 3 1 " ]
none
[ { "input": "2\n1 100\n100 2", "output": "2 100 1 " }, { "input": "3\n3 1\n100 2\n3 2", "output": "100 2 3 1 " }, { "input": "3\n458744979 589655889\n248228386 824699605\n458744979 824699605", "output": "589655889 458744979 824699605 248228386 " }, { "input": "4\n90104473 2210...
1,558
14,336,000
3.583797
9,005
578
Weakness and Poorness
[ "ternary search" ]
null
null
You are given a sequence of n integers *a*1,<=*a*2,<=...,<=*a**n*. Determine a real number *x* such that the weakness of the sequence *a*1<=-<=*x*,<=*a*2<=-<=*x*,<=...,<=*a**n*<=-<=*x* is as small as possible. The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous su...
The first line contains one integer *n* (1<=≀<=*n*<=≀<=200<=000), the length of a sequence. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≀<=10<=000).
Output a real number denoting the minimum possible weakness of *a*1<=-<=*x*,<=*a*2<=-<=*x*,<=...,<=*a**n*<=-<=*x*. Your answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6.
[ "3\n1 2 3\n", "4\n1 2 3 4\n", "10\n1 10 2 9 3 8 4 7 5 6\n" ]
[ "1.000000000000000\n", "2.000000000000000\n", "4.500000000000000\n" ]
For the first case, the optimal value of *x* is 2 so the sequence becomes  - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case. For the second sample the optimal value of *x* is 2.5 so the sequence becomes  - 1.5,  - 0.5, 0.5, 1.5 and the max ...
[ { "input": "3\n1 2 3", "output": "1.000000000000000" }, { "input": "4\n1 2 3 4", "output": "2.000000000000000" }, { "input": "10\n1 10 2 9 3 8 4 7 5 6", "output": "4.500000000000000" }, { "input": "1\n-10000", "output": "0.000000000000000" }, { "input": "3\n10000 ...
46
0
-1
9,020
498
Name That Tune
[ "dp", "probabilities", "two pointers" ]
null
null
It turns out that you are a great fan of rock band AC/PE. Peter learned that and started the following game: he plays the first song of the list of *n* songs of the group, and you have to find out the name of the song. After you tell the song name, Peter immediately plays the following song in order, and so on. The *i...
The first line of the input contains numbers *n* and *T* (1<=≀<=*n*<=≀<=5000, 1<=≀<=*T*<=≀<=5000), separated by a space. Next *n* lines contain pairs of numbers *p**i* and *t**i* (0<=≀<=*p**i*<=≀<=100, 1<=≀<=*t**i*<=≀<=*T*). The songs are given in the same order as in Petya's list.
Output a single number β€” the expected number of the number of songs you will recognize in *T* seconds. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
[ "2 2\n50 2\n10 1\n", "2 2\n0 2\n100 2\n", "3 3\n50 3\n50 2\n25 2\n", "2 2\n0 2\n0 2\n" ]
[ "1.500000000\n", "1.000000000\n", "1.687500000\n", "1.000000000\n" ]
none
[]
31
0
0
9,029
140
New Year Snowmen
[ "binary search", "data structures", "greedy" ]
null
null
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made *n* snowballs with radii equal to *r*1, *r*2, ..., *r**n*. To make a snowman, one nee...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of snowballs. The next line contains *n* integers β€” the balls' radii *r*1, *r*2, ..., *r**n* (1<=≀<=*r**i*<=≀<=109). The balls' radii can coincide.
Print on the first line a single number *k* β€” the maximum number of the snowmen. Next *k* lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers β€” the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print...
[ "7\n1 2 3 4 5 6 7\n", "3\n2 2 3\n" ]
[ "2\n3 2 1\n6 5 4\n", "0\n" ]
none
[ { "input": "7\n1 2 3 4 5 6 7", "output": "2\n7 5 3\n6 4 2" }, { "input": "3\n2 2 3", "output": "0" }, { "input": "1\n255317", "output": "0" }, { "input": "6\n1 1 2 2 3 3", "output": "2\n3 2 1\n3 2 1" }, { "input": "6\n1 2 2 2 3 3", "output": "1\n3 2 1" }, ...
1,152
37,376,000
3
9,058
603
Lieges of Legendre
[ "games", "math" ]
null
null
Kevin and Nicky Sun have invented a new game called Lieges of Legendre. In this game, two players take turns modifying the game state with Kevin moving first. Initially, the game is set up so that there are *n* piles of cows, with the *i*-th pile containing *a**i* cows. During each player's turn, that player calls upon...
The first line of the input contains two space-separated integers *n* and *k* (1<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*k*<=≀<=109). The second line contains *n* integers, *a*1,<=*a*2,<=... *a**n* (1<=≀<=*a**i*<=≀<=109) describing the initial state of the game.
Output the name of the winning player, either "Kevin" or "Nicky" (without quotes).
[ "2 1\n3 4\n", "1 2\n3\n" ]
[ "Kevin\n", "Nicky\n" ]
In the second sample, Nicky can win in the following way: Kevin moves first and is forced to remove a cow, so the pile contains two cows after his move. Next, Nicky replaces this pile of size 2 with two piles of size 1. So the game state is now two piles of size 1. Kevin then removes one of the remaining cows and Nicky...
[ { "input": "2 1\n3 4", "output": "Kevin" }, { "input": "1 2\n3", "output": "Nicky" }, { "input": "4 5\n20 21 22 25", "output": "Kevin" }, { "input": "5 1\n1 7 7 6 6", "output": "Kevin" }, { "input": "7 1\n8 6 10 10 1 5 8", "output": "Kevin" }, { "input...
77
9,318,400
0
9,064
11
A Simple Task
[ "bitmasks", "dp", "graphs" ]
D. A Simple Task
2
256
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no repeated vertices or edges.
The first line of input contains two integers *n* and *m* (1<=≀<=*n*<=≀<=19, 0<=≀<=*m*) – respectively the number of vertices and edges of the graph. Each of the subsequent *m* lines contains two integers *a* and *b*, (1<=≀<=*a*,<=*b*<=≀<=*n*, *a*<=β‰ <=*b*) indicating that vertices *a* and *b* are connected by an undire...
Output the number of cycles in the given graph.
[ "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n" ]
[ "7\n" ]
The example graph is a clique and contains four cycles of length 3 and three cycles of length 4.
[ { "input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4", "output": "7" }, { "input": "10 3\n4 8\n9 4\n8 9", "output": "1" }, { "input": "8 28\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n3 4\n3 5\n3 6\n3 7\n3 8\n4 5\n4 6\n4 7\n4 8\n5 6\n5 7\n5 8\n6 7\n6 8\n7 8", "output":...
0
0
-1
9,071