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
630
Turn
[ "geometry", "math" ]
null
null
Vasya started working in a machine vision company of IT City. Vasya's team creates software and hardware for identification of people by their face. One of the project's know-how is a camera rotating around its optical axis on shooting. People see an eye-catching gadget β€” a rotating camera β€” come up to it to see it be...
The only line of the input contains one integer *x* (<=-<=1018<=≀<=*x*<=≀<=1018) β€” camera angle in degrees. Positive value denotes clockwise camera rotation, negative β€” counter-clockwise.
Output one integer β€” the minimum required number of 90 degrees clockwise turns.
[ "60\n", "-60\n" ]
[ "1\n", "3\n" ]
When the camera is rotated 60 degrees counter-clockwise (the second example), an image from it is rotated 60 degrees clockwise. One 90 degrees clockwise turn of the image result in 150 degrees clockwise total rotation and deviation from "true up" for one turn is 150 degrees. Two 90 degrees clockwise turns of the image ...
[ { "input": "60", "output": "1" }, { "input": "-60", "output": "3" }, { "input": "0", "output": "0" }, { "input": "44", "output": "0" }, { "input": "45", "output": "0" }, { "input": "46", "output": "1" }, { "input": "134", "output": "1" ...
62
0
0
22,775
319
Kalila and Dimna in the Logging Industry
[ "dp", "geometry" ]
null
null
Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money. The manager of logging factory wants them to go to the jungle and cut *n* trees with heights *a*1,<=*a*2,<=...,<=*a**n*. They bought a chain saw from a shop. Each time they use the chain sa...
The first line of input contains an integer *n* (1<=≀<=*n*<=≀<=105). The second line of input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109). The third line of input contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (0<=≀<=*b**i*<=≀<=109). It's guaranteed that *a*1<==<=1, *b**n*<==<=0, *a*1<=&l...
The only line of output must contain the minimum cost of cutting all the trees completely. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "5\n1 2 3 4 5\n5 4 3 2 0\n", "6\n1 2 3 10 20 30\n6 5 4 3 2 0\n" ]
[ "25\n", "138\n" ]
none
[ { "input": "5\n1 2 3 4 5\n5 4 3 2 0", "output": "25" }, { "input": "6\n1 2 3 10 20 30\n6 5 4 3 2 0", "output": "138" } ]
2,000
14,848,000
0
22,789
611
New Year and Ancient Prophecy
[ "dp", "hashing", "strings" ]
null
null
Limak is a little polar bear. In the snow he found a scroll with the ancient prophecy. Limak doesn't know any ancient languages and thus is unable to understand the prophecy. But he knows digits! One fragment of the prophecy is a sequence of *n* digits. The first digit isn't zero. Limak thinks that it's a list of some...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=5000)Β β€” the number of digits. The second line contains a string of digits and has length equal to *n*. It's guaranteed that the first digit is not '0'.
Print the number of ways to correctly split the given sequence modulo 109<=+<=7.
[ "6\n123434\n", "8\n20152016\n" ]
[ "8\n", "4\n" ]
In the first sample there are 8 ways to split the sequence: - "123434" = "123434" (maybe the given sequence is just one big number) - "123434" = "1" + "23434" - "123434" = "12" + "3434" - "123434" = "123" + "434" - "123434" = "1" + "23" + "434" - "123434" = "1" + "2" + "3434" - "123434" = "1" + "2" + "3" + "434...
[]
62
4,608,000
0
22,831
822
Liar
[ "binary search", "dp", "hashing", "string suffix structures" ]
null
null
The first semester ended. You know, after the end of the first semester the holidays begin. On holidays Noora decided to return to Vičkopolis. As a modest souvenir for Leha, she brought a sausage of length *m* from Pavlopolis. Everyone knows that any sausage can be represented as a string of lowercase English letters, ...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=105) β€” length of sausage bought by Leha, i.e. the length of the string *s*. The second line contains string *s* of the length *n* consisting of lowercase English letters. The third line contains single integer *m* (1<=≀<=*m*<=≀<=*n*) β€” length of sausage bought...
In the only line print "YES" (without quotes), if Leha is able to succeed in creating new sausage so that Noora doesn't notice anything. Otherwise print "NO" (without quotes).
[ "9\nhloyaygrt\n6\nloyyrt\n3\n", "9\nhloyaygrt\n6\nloyyrt\n2\n" ]
[ "YES\n", "NO\n" ]
Let's consider the first sample. In the optimal answer, Leha should cut the sausage he bought in the following way: hloyaygrt = h + loy + a + y + g + rt. Then he numbers received parts from 1 to 6: - h β€” number 1 - loy β€” number 2 - a β€” number 3 - y β€” number 4 - g β€” number 5 - rt β€” number 6 Hereupon the hacker ...
[]
31
0
0
22,900
830
Cards Sorting
[ "data structures", "implementation", "sortings" ]
null
null
Vasily has a deck of cards consisting of *n* cards. There is an integer on each of the cards, this integer is between 1 and 100<=000, inclusive. It is possible that some cards have the same integers on them. Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=100<=000) β€” the number of cards in the deck. The second line contains a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=100<=000), where *a**i* is the number written on the *i*-th from top card in the deck.
Print the total number of times Vasily takes the top card from the deck.
[ "4\n6 3 1 2\n", "1\n1000\n", "7\n3 3 3 3 3 3 3\n" ]
[ "7\n", "1\n", "7\n" ]
In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards ...
[ { "input": "4\n6 3 1 2", "output": "7" }, { "input": "1\n1000", "output": "1" }, { "input": "7\n3 3 3 3 3 3 3", "output": "7" }, { "input": "64\n826 142 89 337 897 891 1004 704 281 644 910 852 147 193 289 384 625 695 416 944 162 939 164 1047 359 114 499 99 713 300 268 316 256...
1,000
11,776,000
0
22,987
691
Xor-sequences
[ "matrices" ]
null
null
You are given *n* integers *a*1,<=<=*a*2,<=<=...,<=<=*a**n*. A sequence of integers *x*1,<=<=*x*2,<=<=...,<=<=*x**k* is called a "xor-sequence" if for every 1<=<=≀<=<=*i*<=<=≀<=<=*k*<=-<=1 the number of ones in the binary representation of the number *x**i* *x**i*<=<=+<=<=1's is a multiple of 3 and for all 1<=≀<=*i*...
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=100, 1<=≀<=*k*<=≀<=1018) β€” the number of given integers and the length of the "xor-sequences". The second line contains *n* integers *a**i* (0<=≀<=*a**i*<=≀<=1018).
Print the only integer *c* β€” the number of "xor-sequences" of length *k* modulo 109<=+<=7.
[ "5 2\n15 1 2 4 8\n", "5 1\n15 1 2 4 8\n" ]
[ "13\n", "5\n" ]
none
[ { "input": "5 2\n15 1 2 4 8", "output": "13" }, { "input": "5 1\n15 1 2 4 8", "output": "5" }, { "input": "10 1\n44 65 23 44 100 19 19 23 19 40", "output": "10" }, { "input": "10 2\n93 93 85 48 44 98 93 100 98 98", "output": "52" }, { "input": "10 100\n22 0 41 63 ...
795
9,830,400
3
23,075
0
none
[ "none" ]
null
null
You are given three sticks with positive integer lengths of *a*,<=*b*, and *c* centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most *l* centimeters. In particular, it is allowed not to incre...
The single line contains 4 integers *a*,<=*b*,<=*c*,<=*l* (1<=≀<=*a*,<=*b*,<=*c*<=≀<=3Β·105, 0<=≀<=*l*<=≀<=3Β·105).
Print a single integer β€” the number of ways to increase the sizes of the sticks by the total of at most *l* centimeters, so that you can make a non-degenerate triangle from it.
[ "1 1 1 2\n", "1 2 3 1\n", "10 2 1 7\n" ]
[ "4\n", "2\n", "0\n" ]
In the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter. In the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn't meet the conditions.
[ { "input": "1 1 1 2", "output": "4" }, { "input": "1 2 3 1", "output": "2" }, { "input": "10 2 1 7", "output": "0" }, { "input": "1 2 1 5", "output": "20" }, { "input": "10 15 17 10", "output": "281" }, { "input": "5 5 5 10000", "output": "41841675...
0
0
-1
23,139
660
Different Subsets For All Tuples
[ "combinatorics", "math" ]
null
null
For a sequence *a* of *n* integers between 1 and *m*, inclusive, denote *f*(*a*) as the number of distinct subsequences of *a* (including the empty subsequence). You are given two positive integers *n* and *m*. Let *S* be the set of all sequences of length *n* consisting of numbers from 1 to *m*. Compute the sum *f*(*...
The only line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=106) β€” the number of elements in arrays and the upper bound for elements.
Print the only integer *c* β€” the desired sum modulo 109<=+<=7.
[ "1 3\n", "2 2\n", "3 3\n" ]
[ "6\n", "14\n", "174\n" ]
none
[ { "input": "1 3", "output": "6" }, { "input": "2 2", "output": "14" }, { "input": "3 3", "output": "174" }, { "input": "1 1000000", "output": "2000000" }, { "input": "1000000 1", "output": "1000001" }, { "input": "500 500", "output": "383255233" ...
46
0
0
23,151
235
Graph Game
[ "graphs" ]
null
null
In computer science, there is a method called "Divide And Conquer By Node" to solve some hard problems about paths on a tree. Let's desribe how this method works by function: *solve*(*t*) (*t* is a tree): 1. Chose a node *x* (it's common to chose weight-center) in tree *t*. Let's call this step "Line A". 1. Deal w...
The first line contains an integer *n* (3<=≀<=*n*<=≀<=3000) β€” the number of nodes and edges in the graph. Each of the next *n* lines contains two space-separated integers *a**i*,<=*b**i* (0<=≀<=*a**i*,<=*b**i*<=≀<=*n*<=-<=1) indicating an edge between nodes *a**i* and *b**i*. Consider that the graph nodes are numbered...
Print a single real number β€” the expectation of *totalCost*. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
[ "5\n3 4\n2 3\n2 4\n0 4\n1 2\n", "3\n0 1\n1 2\n0 2\n", "5\n0 1\n1 2\n2 0\n3 0\n4 1\n" ]
[ "13.166666666666666\n", "6.000000000000000\n", "13.166666666666666\n" ]
Consider the second example. No matter what we choose first, the *totalCost* will always be 3 + 2 + 1 = 6.
[]
60
0
0
23,166
928
Autocompletion
[ "*special", "strings", "trees" ]
null
null
Arcady is a copywriter. His today's task is to type up an already well-designed story using his favorite text editor. Arcady types words, punctuation signs and spaces one after another. Each letter and each sign (including line feed) requires one keyboard click in order to be printed. Moreover, when Arcady has a non-e...
The only line contains Arcady's text, consisting only of lowercase latin letters, spaces, line feeds and the following punctuation signs: Β«.Β», Β«,Β», Β«?Β», Β«!Β», Β«'Β» and Β«-Β». The total amount of symbols doesn't exceed 3Β·105. It's guaranteed that all lines are non-empty.
Print a single integer β€” the minimum number of clicks.
[ "snow affects sports such as skiing, snowboarding, and snowmachine travel.\nsnowboarding is a recreational activity and olympic and paralympic sport.\n", "'co-co-co, codeforces?!'\n", "thun-thun-thunder, thunder, thunder\nthunder, thun-, thunder\nthun-thun-thunder, thunder\nthunder, feel the thunder\nlightning ...
[ "141\n", "25\n", "183\n" ]
In sample case one it's optimal to use autocompletion for the first instance of Β«snowboardingΒ» after typing up Β«snΒ» and for the second instance of Β«snowboardingΒ» after typing up Β«snowbΒ». This will save 7 clicks. In sample case two it doesn't matter whether to use autocompletion or not.
[ { "input": "snow affects sports such as skiing, snowboarding, and snowmachine travel.\nsnowboarding is a recreational activity and olympic and paralympic sport.", "output": "141" }, { "input": "'co-co-co, codeforces?!'", "output": "25" }, { "input": "thun-thun-thunder, thunder, thunder\n...
1,000
7,065,600
0
23,180
432
Prefixes and Suffixes
[ "dp", "string suffix structures", "strings", "two pointers" ]
null
null
You have a string *s*<==<=*s*1*s*2...*s*|*s*|, where |*s*| is the length of string *s*, and *s**i* its *i*-th character. Let's introduce several definitions: - A substring *s*[*i*..*j*] (1<=≀<=*i*<=≀<=*j*<=≀<=|*s*|) of string *s* is string *s**i**s**i*<=+<=1...*s**j*. - The prefix of string *s* of length *l* (1<=≀...
The single line contains a sequence of characters *s*1*s*2...*s*|*s*| (1<=≀<=|*s*|<=≀<=105) β€” string *s*. The string only consists of uppercase English letters.
In the first line, print integer *k* (0<=≀<=*k*<=≀<=|*s*|) β€” the number of prefixes that match a suffix of string *s*. Next print *k* lines, in each line print two integers *l**i* *c**i*. Numbers *l**i* *c**i* mean that the prefix of the length *l**i* matches the suffix of length *l**i* and occurs in string *s* as a su...
[ "ABACABA\n", "AAA\n" ]
[ "3\n1 4\n3 2\n7 1\n", "3\n1 3\n2 2\n3 1\n" ]
none
[ { "input": "ABACABA", "output": "3\n1 4\n3 2\n7 1" }, { "input": "AAA", "output": "3\n1 3\n2 2\n3 1" }, { "input": "A", "output": "1\n1 1" }, { "input": "AAAAAAAAAAAAAAAAXAAAAAAAAAAAAAAAAAAAAAAA", "output": "17\n1 39\n2 37\n3 35\n4 33\n5 31\n6 29\n7 27\n8 25\n9 23\n10 21\...
30
0
0
23,189
721
Journey
[ "dp", "graphs" ]
null
null
Recently Irina arrived to one of the most famous cities of BerlandΒ β€” the Berlatov city. There are *n* showplaces in the city, numbered from 1 to *n*, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces. Initially I...
The first line of the input contains three integers *n*,<=*m* and *T* (2<=≀<=*n*<=≀<=5000,<=<=1<=≀<=*m*<=≀<=5000,<=<=1<=≀<=*T*<=≀<=109)Β β€” the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively. The next *m* lines describes roads in Berlatov. *i*-th of them cont...
Print the single integer *k* (2<=≀<=*k*<=≀<=*n*)Β β€” the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace *n* within time not exceeding *T*, in the first line. Print *k* distinct integers in the second lineΒ β€” indices of showplaces that Irina will visit on her route, in t...
[ "4 3 13\n1 2 5\n2 3 7\n2 4 8\n", "6 6 7\n1 2 2\n1 3 3\n3 6 3\n2 4 2\n4 6 2\n6 5 1\n", "5 5 6\n1 3 3\n3 5 3\n1 2 2\n2 4 3\n4 5 2\n" ]
[ "3\n1 2 4 \n", "4\n1 2 4 6 \n", "3\n1 3 5 \n" ]
none
[ { "input": "4 3 13\n1 2 5\n2 3 7\n2 4 8", "output": "3\n1 2 4 " }, { "input": "6 6 7\n1 2 2\n1 3 3\n3 6 3\n2 4 2\n4 6 2\n6 5 1", "output": "4\n1 2 4 6 " }, { "input": "5 5 6\n1 3 3\n3 5 3\n1 2 2\n2 4 3\n4 5 2", "output": "3\n1 3 5 " }, { "input": "10 10 100\n1 4 1\n6 4 1\n9 3...
561
268,390,400
0
23,242
652
Pursuit For Artifacts
[ "dfs and similar", "dsu", "graphs", "trees" ]
null
null
Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience. In that country there are *n* islands and *m* bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerfu...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=3Β·105, 0<=≀<=*m*<=≀<=3Β·105) β€” the number of islands and bridges in the game. Each of the next *m* lines contains the description of the bridge β€” three integers *x**i*, *y**i*, *z**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*, *x**i*<=β‰ <=*y**i*, 0<=≀<=*z**i*<=≀<=1), ...
If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes).
[ "6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6\n", "5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4\n", "5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2\n" ]
[ "YES\n", "NO\n", "YES\n" ]
none
[ { "input": "6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6", "output": "YES" }, { "input": "5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4", "output": "NO" }, { "input": "5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2", "output": "YES" }, { "input": "1 0\n1 1", "outp...
0
0
-1
23,285
632
Thief in a Shop
[ "divide and conquer", "dp", "fft", "math" ]
null
null
A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contain *k* objects. There are *n* kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind *i* is *a**i*. The thief is greedy, so he will take exactly *k* products (it's...
The first line contains two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=1000) β€” the number of kinds of products and the number of products the thief will take. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=1000) β€” the costs of products for kinds from 1 to *n*.
Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.
[ "3 2\n1 2 3\n", "5 5\n1 1 1 1 1\n", "3 3\n3 5 11\n" ]
[ "2 3 4 5 6\n", "5\n", "9 11 13 15 17 19 21 25 27 33\n" ]
none
[ { "input": "3 2\n1 2 3", "output": "2 3 4 5 6" }, { "input": "5 5\n1 1 1 1 1", "output": "5" }, { "input": "3 3\n3 5 11", "output": "9 11 13 15 17 19 21 25 27 33" }, { "input": "10 3\n3 4 12 5 7 13 5 6 1 6", "output": "3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23...
5,000
126,873,600
0
23,330
827
DNA Evolution
[ "data structures", "strings" ]
null
null
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string *s* initially. Evolution of the species is described as a sequence of changes...
The first line contains the string *s* (1<=≀<=|*s*|<=≀<=105) that describes the initial DNA strand. It consists only of capital English letters "A", "T", "G" and "C". The next line contains single integer *q* (1<=≀<=*q*<=≀<=105)Β β€” the number of events. After that, *q* lines follow, each describes one event. Each of t...
For each scientists' query (second type query) print a single integer in a new lineΒ β€” the value of impact of the infection on the DNA.
[ "ATGCATGC\n4\n2 1 8 ATGC\n2 2 6 TTT\n1 4 T\n2 2 6 TA\n", "GAGTTGTTAA\n6\n2 3 4 TATGGTG\n1 1 T\n1 6 G\n2 5 9 AGTAATA\n1 10 G\n2 2 6 TTGT\n" ]
[ "8\n2\n4\n", "0\n3\n1\n" ]
Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string "TTTTT..." and the substring "TGCAT". There are two matches. In the third query, after the DNA change, we compare string "TATAT..."' with substring "TGTAT". There are 4 matche...
[ { "input": "ATGCATGC\n4\n2 1 8 ATGC\n2 2 6 TTT\n1 4 T\n2 2 6 TA", "output": "8\n2\n4" }, { "input": "GAGTTGTTAA\n6\n2 3 4 TATGGTG\n1 1 T\n1 6 G\n2 5 9 AGTAATA\n1 10 G\n2 2 6 TTGT", "output": "0\n3\n1" }, { "input": "TCAATCGGGCGGACTAACCC\n20\n2 4 17 CTCGGATGTT\n1 4 T\n1 1 C\n2 15 18 CA\n2...
2,000
5,324,800
0
23,392
690
Photographs (I)
[]
null
null
The Human-Cow Confederation (*HC*2), led by Heidi, has built a base where people and cows can hide, guarded from zombie attacks. The entrance to the base is protected by an automated gate which performs a kind of a Turing test: it shows the entering creature a photograph and asks them whether the top and bottom halves ...
The first line of the input contains the number *q* of questions (1<=≀<=*q*<=≀<=220). After that, *q* questions follow, each of which in the format described below. The first line of every question contains two space-separated integers *h* and *w* (1<=≀<=*h*,<=*w*<=≀<=600) – the height (number of rows) and width (numb...
Your program should print *q* lines. The *i*-th line should contain your answer for the *i*-th question: YES if the photograph has been rearranged and NO otherwise. Your answers will be accepted if they all conform to this format and if at least 75% of them are correct. Because the input is rather huge, feel free to p...
[]
[]
The link to download all necessary files is http://assets.codeforces.com/files/690/easy_contestant_package.zip
[]
0
0
-1
23,404
546
Soldier and Badges
[ "brute force", "greedy", "implementation", "sortings" ]
null
null
Colonel has *n* badges. He wants to give one badge to every of his *n* soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the s...
First line of input consists of one integer *n* (1<=≀<=*n*<=≀<=3000). Next line consists of *n* integers *a**i* (1<=≀<=*a**i*<=≀<=*n*), which stand for coolness factor of each badge.
Output single integer β€” minimum amount of coins the colonel has to pay.
[ "4\n1 3 1 4\n", "5\n1 2 3 2 5\n" ]
[ "1", "2" ]
In first sample test we can increase factor of first badge by 1. In second sample test we can increase factors of the second and the third badge by 1.
[ { "input": "4\n1 3 1 4", "output": "1" }, { "input": "5\n1 2 3 2 5", "output": "2" }, { "input": "5\n1 5 3 2 4", "output": "0" }, { "input": "10\n1 1 2 3 4 5 6 7 8 9", "output": "9" }, { "input": "11\n9 2 10 3 1 5 7 1 4 8 6", "output": "10" }, { "input...
109
1,945,600
3
23,468
144
Missile Silos
[ "data structures", "dfs and similar", "graphs", "shortest paths" ]
null
null
A country called Berland consists of *n* cities, numbered with integer numbers from 1 to *n*. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. According to some Super Duper Documents, Berland is protected by the Super Duper Mis...
The first line contains three integers *n*, *m* and *s* (2<=≀<=*n*<=≀<=105, , 1<=≀<=*s*<=≀<=*n*) β€” the number of cities, the number of roads in the country and the number of the capital, correspondingly. Capital is the city no. *s*. Then *m* lines contain the descriptions of roads. Each of them is described by three ...
Print the single number β€” the number of Super Duper Secret Missile Silos that are located in Berland.
[ "4 6 1\n1 2 1\n1 3 3\n2 3 1\n2 4 1\n3 4 1\n1 4 2\n2\n", "5 6 3\n3 1 1\n3 2 1\n3 4 1\n3 5 1\n1 2 6\n4 5 8\n4\n" ]
[ "3\n", "3\n" ]
In the first sample the silos are located in cities 3 and 4 and on road (1, 3) at a distance 2 from city 1 (correspondingly, at a distance 1 from city 3). In the second sample one missile silo is located right in the middle of the road (1, 2). Two more silos are on the road (4, 5) at a distance 3 from city 4 in the di...
[]
154
0
0
23,483
696
Puzzles
[ "dfs and similar", "math", "probabilities", "trees" ]
null
null
Barney lives in country USC (United States of Charzeh). USC has *n* cities numbered from 1 through *n* and *n*<=-<=1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit an...
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=105)Β β€” the number of cities in USC. The second line contains *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* (1<=≀<=*p**i*<=&lt;<=*i*), where *p**i* is the number of the parent city of city number *i* in the tree, meaning there is a road between cities...
In the first and only line of output print *n* numbers, where *i*-th number is the expected value of starting_time[i]. Your answer for each city will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
[ "7\n1 2 1 1 4 4\n", "12\n1 1 2 2 4 4 3 3 1 10 8\n" ]
[ "1.0 4.0 5.0 3.5 4.5 5.0 5.0 \n", "1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 \n" ]
none
[ { "input": "7\n1 2 1 1 4 4", "output": "1.0 4.0 5.0 3.5 4.5 5.0 5.0 " }, { "input": "12\n1 1 2 2 4 4 3 3 1 10 8", "output": "1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 " }, { "input": "3\n1 2", "output": "1.0 2.0 3.0 " }, { "input": "8\n1 1 2 2 3 6 1", "output": "1.0...
1,000
28,672,000
0
23,547
852
Product transformation
[ "combinatorics", "math", "number theory" ]
null
null
Consider an array *A* with *N* elements, all being the same integer *a*. Define the product transformation as a simultaneous update *A**i*<==<=*A**i*Β·*A**i*<=+<=1, that is multiplying each element to the element right to it for , with the last number *A**N* remaining the same. For example, if we start with an array *A...
The first and only line of input contains four integers *N*, *M*, *a*, *Q* (7<=≀<=*Q*<=≀<=109<=+<=123, 2<=≀<=*a*<=≀<=106<=+<=123, , is prime), where is the multiplicative order of the integer *a* modulo *Q*, see notes for definition.
You should output the array *A* from left to right.
[ "2 2 2 7\n" ]
[ "1 2 " ]
The multiplicative order of a number *a* modulo *Q* <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/38b13c1f6db75ae72784f8602e8230429b26cf2a.png" style="max-width: 100.0%;max-height: 100.0%;"/>, is the smallest natural number *x* such that *a*<sup class="upper-index">*x*</sup> *mod* *Q* = 1...
[]
46
0
-1
23,614
329
Evil
[ "math" ]
null
null
There are *n* cities on a two dimensional Cartesian plane. The distance between two cities is equal to the Manhattan distance between them (see the Notes for definition). A Hamiltonian cycle of the cities is defined as a permutation of all *n* cities. The length of this Hamiltonian cycle is defined as the sum of the di...
The first line contains an integer *n* (3<=≀<=*n*<=≀<=105). Then *n* lines follow, each consisting of two integers *x**i* and *y**i* (0<=≀<=*x**i*,<=*y**i*<=≀<=109), denoting the coordinates of a city. All given points will be distinct.
A single line denoting the longest possible length of a Hamiltonian cycle of the given cities. You should not output the cycle, only its length. 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\n1 1\n1 2\n2 1\n2 2\n" ]
[ "6\n" ]
In the example, one of the possible Hamiltonian cycles with length 6 is (1, 1) (1, 2) (2, 1) (2, 2). There does not exist any other Hamiltonian cycle with a length greater than 6. The Manhattan distance between two cities (*x*<sub class="lower-index">*i*</sub>, *y*<sub class="lower-index">*i*</sub>) and (*x*<sub class...
[]
122
0
0
23,678
118
Caesar's Legions
[ "dp" ]
null
null
Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had *n*1 footmen and *n*2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that *k*1 footmen standing successively one after another, or there are strictly more than *k*2 ...
The only line contains four space-separated integers *n*1, *n*2, *k*1, *k*2 (1<=≀<=*n*1,<=*n*2<=≀<=100,<=1<=≀<=*k*1,<=*k*2<=≀<=10) which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly.
Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than *k*1 footmen stand successively, and no more than *k*2 horsemen stand successively.
[ "2 1 1 10\n", "2 3 1 2\n", "2 4 1 1\n" ]
[ "1\n", "5\n", "0\n" ]
Let's mark a footman as 1, and a horseman as 2. In the first sample the only beautiful line-up is: 121 In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121
[ { "input": "2 1 1 10", "output": "1" }, { "input": "2 3 1 2", "output": "5" }, { "input": "2 4 1 1", "output": "0" }, { "input": "10 10 5 7", "output": "173349" }, { "input": "12 15 7 2", "output": "171106" }, { "input": "20 8 4 8", "output": "1625...
2,000
98,201,600
0
23,689
87
Interesting Game
[ "dp", "games", "math" ]
C. Interesting Game
2
256
Two best friends Serozha and Gena play a game. Initially there is one pile consisting of *n* stones on the table. During one move one pile should be taken and divided into an arbitrary number of piles consisting of *a*1<=&gt;<=*a*2<=&gt;<=...<=&gt;<=*a**k*<=&gt;<=0 stones. The piles should meet the condition *a*1<=-<=...
The single line contains a single integer *n* (1<=≀<=*n*<=≀<=105).
If Serozha wins, print *k*, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game. If Gena wins, print "-1" (without the quotes).
[ "3\n", "6\n", "100\n" ]
[ "2\n", "-1\n", "8\n" ]
none
[ { "input": "3", "output": "2" }, { "input": "6", "output": "-1" }, { "input": "100", "output": "8" }, { "input": "33", "output": "2" }, { "input": "23", "output": "-1" }, { "input": "35", "output": "-1" }, { "input": "15", "output": "2"...
1,682
2,867,200
3.574159
23,696
637
Running with Obstacles
[ "*special", "data structures", "dp", "greedy" ]
null
null
A sportsman starts from point *x**start*<==<=0 and runs to point with coordinate *x**finish*<==<=*m* (on a straight line). Also, the sportsman can jump β€” to jump, he should first take a run of length of not less than *s* meters (in this case for these *s* meters his path should have no obstacles), and after that he can...
The first line of the input containsd four integers *n*, *m*, *s* and *d* (1<=≀<=*n*<=≀<=200<=000, 2<=≀<=*m*<=≀<=109, 1<=≀<=*s*,<=*d*<=≀<=109)Β β€” the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly. ...
If the runner cannot reach the finishing point, print in the first line of the output "IMPOSSIBLE" (without the quotes). If the athlete can get from start to finish, print any way to do this in the following format: - print a line of form "RUN X&gt;" (where "X" should be a positive integer), if the athlete should ru...
[ "3 10 1 3\n3 4 7\n", "2 9 2 3\n6 4\n" ]
[ "RUN 2\nJUMP 3\nRUN 1\nJUMP 2\nRUN 2\n", "IMPOSSIBLE\n" ]
none
[ { "input": "3 10 1 3\n3 4 7", "output": "RUN 2\nJUMP 3\nRUN 1\nJUMP 2\nRUN 2" }, { "input": "2 9 2 3\n6 4", "output": "IMPOSSIBLE" }, { "input": "10 100 2 8\n93 35 24 87 39 46 86 37 73 33", "output": "RUN 23\nJUMP 2\nRUN 7\nJUMP 8\nRUN 5\nJUMP 2\nRUN 25\nJUMP 2\nRUN 11\nJUMP 3\nRUN 4...
2,000
16,076,800
0
23,701
219
Color Stripe
[ "brute force", "dp", "greedy" ]
null
null
A colored stripe is represented by a horizontal row of *n* square cells, each cell is pained one of *k* colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to *k* to repaint the cells.
The first input line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=5Β·105;Β 2<=≀<=*k*<=≀<=26). The second line contains *n* uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first *k* English letters may be used. Each letter represents the color of ...
Print a single integer β€” the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
[ "6 3\nABBACC\n", "3 2\nBBB\n" ]
[ "2\nABCACA\n", "1\nBAB\n" ]
none
[ { "input": "6 3\nABBACC", "output": "2\nABCACA" }, { "input": "3 2\nBBB", "output": "1\nBAB" }, { "input": "1 2\nA", "output": "0\nA" }, { "input": "1 26\nZ", "output": "0\nZ" }, { "input": "2 2\nAA", "output": "1\nAB" }, { "input": "2 2\nBA", "out...
124
0
0
23,719
353
Queue
[ "constructive algorithms", "dp" ]
null
null
There are *n* schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes. Each second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginni...
The first line contains a sequence of letters without spaces *s*1*s*2... *s**n* (1<=≀<=*n*<=≀<=106), consisting of capital English letters M and F. If letter *s**i* equals M, that means that initially, the line had a boy on the *i*-th position. If letter *s**i* equals F, then initially the line had a girl on the *i*-th...
Print a single integer β€” the number of seconds needed to move all the girls in the line in front of the boys. If the line has only boys or only girls, print 0.
[ "MFM\n", "MMFF\n", "FFMMM\n" ]
[ "1\n", "3\n", "0\n" ]
In the first test case the sequence of changes looks as follows: MFM  →  FMM. The second test sample corresponds to the sample from the statement. The sequence of changes is: MMFF  →  MFMF  →  FMFM  →  FFMM.
[ { "input": "MFM", "output": "1" }, { "input": "MMFF", "output": "3" }, { "input": "FFMMM", "output": "0" }, { "input": "MMFMMFFFFM", "output": "7" }, { "input": "MFFFMMFMFMFMFFFMMMFFMMMMMMFMMFFMMMFMMFMFFFMMFMMMFFMMFFFFFMFMFFFMMMFFFMFMFMFMFFFMMMMFMMFMMFFMMMMMMFFM",...
872
10,137,600
3
23,778
706
Hard problem
[ "dp", "strings" ]
null
null
Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help. Vasiliy is given *n* strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only o...
The first line of the input contains a single integer *n* (2<=≀<=*n*<=≀<=100<=000)Β β€” the number of strings. The second line contains *n* integers *c**i* (0<=≀<=*c**i*<=≀<=109), the *i*-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the *i*-th string. Then follow *n* lines, each ...
If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print <=-<=1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.
[ "2\n1 2\nba\nac\n", "3\n1 3 1\naa\nba\nac\n", "2\n5 5\nbbb\naaa\n", "2\n3 3\naaa\naa\n" ]
[ "1\n", "1\n", "-1\n", "-1\n" ]
In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller. In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is  - 1. In the fourth sample, both strings consists of characters 'a' only, but in...
[ { "input": "2\n1 2\nba\nac", "output": "1" }, { "input": "3\n1 3 1\naa\nba\nac", "output": "1" }, { "input": "2\n5 5\nbbb\naaa", "output": "-1" }, { "input": "2\n3 3\naaa\naa", "output": "-1" }, { "input": "4\n0 0 8 6\nbi\nqp\nbt\nya", "output": "8" }, { ...
717
16,896,000
0
23,836
0
none
[ "none" ]
null
null
Kyoya Ootori has a bag with *n* colored balls that are colored with *k* different colors. The colors are labeled from 1 to *k*. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color *i* before drawing the last ball of...
The first line of input will have one integer *k* (1<=≀<=*k*<=≀<=1000) the number of colors. Then, *k* lines will follow. The *i*-th line will contain *c**i*, the number of balls of the *i*-th color (1<=≀<=*c**i*<=≀<=1000). The total number of balls doesn't exceed 1000.
A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1<=000<=000<=007.
[ "3\n2\n2\n1\n", "4\n1\n2\n3\n4\n" ]
[ "3\n", "1680\n" ]
In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are:
[ { "input": "3\n2\n2\n1", "output": "3" }, { "input": "4\n1\n2\n3\n4", "output": "1680" }, { "input": "10\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100", "output": "12520708" }, { "input": "5\n10\n10\n10\n10\n10", "output": "425711769" }, { "input": "11\n291\n3...
139
102,400
3
23,843
0
none
[ "none" ]
null
null
There are *n* walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the *n*-th walrus stands at the beginning of the queue. The *i*-th walrus has the age equal to *a**i*. The *i*-th walrus becomes displeased if there's a younger...
The first line contains an integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of walruses in the queue. The second line contains integers *a**i* (1<=≀<=*a**i*<=≀<=109). Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly youn...
Print *n* numbers: if the *i*-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the *i*-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
[ "6\n10 8 5 3 50 45\n", "7\n10 4 6 3 2 8 15\n", "5\n10 3 1 10 11\n" ]
[ "2 1 0 -1 0 -1 ", "4 2 1 0 -1 -1 -1 ", "1 0 -1 -1 -1 " ]
none
[ { "input": "6\n10 8 5 3 50 45", "output": "2 1 0 -1 0 -1 " }, { "input": "7\n10 4 6 3 2 8 15", "output": "4 2 1 0 -1 -1 -1 " }, { "input": "5\n10 3 1 10 11", "output": "1 0 -1 -1 -1 " }, { "input": "13\n18 9 8 9 23 20 18 18 33 25 31 37 36", "output": "2 0 -1 -1 2 1 -1 -1 ...
1,466
17,920,000
3
23,958
0
none
[ "none" ]
null
null
Santa Claus likes palindromes very much. There was his birthday recently. *k* of his friends came to him to congratulate him, and each of them presented to him a string *s**i* having the same length *n*. We denote the beauty of the *i*-th string by *a**i*. It can happen that *a**i* is negativeΒ β€” that means that Santa d...
The first line contains two positive integers *k* and *n* divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1<=≀<=*k*,<=*n*<=≀<=100<=000; *n*Β·*k*Β <=≀<=100<=000). *k* lines follow. The *i*-th of them contains the string *s**i* and its beauty *a**i*...
In the only line print the required maximum possible beauty.
[ "7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4\n", "3 1\na 1\na 2\na 3\n", "2 5\nabcde 10000\nabcde 10000\n" ]
[ "12\n", "6\n", "0\n" ]
In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).
[ { "input": "7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4", "output": "12" }, { "input": "3 1\na 1\na 2\na 3", "output": "6" }, { "input": "2 5\nabcde 10000\nabcde 10000", "output": "0" }, { "input": "10 10\nnjxbzflaka -1\nfelbvvtkja 6\ngxiuztqkcw 5\naomvscmtti 6\nj...
389
7,475,200
0
23,967
252
Little Xor
[ "brute force", "implementation" ]
null
null
Little Petya likes arrays that consist of non-negative integers a lot. Recently his mom has presented him one such array consisting of *n* elements. Petya immediately decided to find there a segment of consecutive elements, such that the *xor* of all numbers from this segment was maximal possible. Help him with that. ...
The first line contains integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of elements in the array. The second line contains the space-separated integers from the array. All numbers are non-negative integers strictly less than 230.
Print a single integer β€” the required maximal *xor* of a segment of consecutive elements.
[ "5\n1 2 1 1 2\n", "3\n1 2 7\n", "4\n4 2 4 8\n" ]
[ "3\n", "7\n", "14\n" ]
In the first sample one of the optimal segments is the segment that consists of the first and the second array elements, if we consider the array elements indexed starting from one. The second sample contains only one optimal segment, which contains exactly one array element (element with index three).
[ { "input": "5\n1 2 1 1 2", "output": "3" }, { "input": "3\n1 2 7", "output": "7" }, { "input": "4\n4 2 4 8", "output": "14" }, { "input": "5\n1 1 1 1 1", "output": "1" }, { "input": "16\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15", "output": "15" }, { "inpu...
186
3,481,600
3
24,008
442
Gena and Second Distance
[ "geometry" ]
null
null
Gena doesn't like geometry, so he asks you to solve this problem for him. A rectangle with sides parallel to coordinate axes contains *n* dots. Let's consider some point of the plane. Let's count the distances from this point to the given *n* points. Let's sort these numbers in the non-decreasing order. We'll call the...
The first line contains three integers *w*,<=*h*,<=*n* (1<=≀<=*w*,<=*h*<=≀<=106,<=2<=≀<=*n*<=≀<=1000) β€” the lengths of the rectangle sides and the number of points. Next *n* lines contain two integers *x**i*,<=*y**i* (0<=≀<=*x**i*<=≀<=*w*,<=0<=≀<=*y**i*<=≀<=*h*) each β€” the coordinates of a point. It is possible that it...
Print a single number β€” the maximum beauty of a point with the absolute or relative error of at most 10<=-<=9.
[ "5 5 4\n0 0\n5 0\n0 5\n5 5\n", "5 5 3\n4 0\n2 5\n4 1\n" ]
[ "4.99999999941792340\n", "5.65685424744772010\n" ]
The point which beauty we need to find must have coordinates (*x*, *y*), where 0 ≀ *x* ≀ *w*, 0 ≀ *y* ≀ *h*. Some of the *n* points can coincide.
[]
30
0
0
24,013
314
Sereja and Contest
[ "implementation" ]
null
null
During the last Sereja's Codesecrof round the server crashed many times, so the round was decided to be made unrated for some participants. Let's assume that *n* people took part in the contest. Let's assume that the participant who got the first place has rating *a*1, the second place participant has rating *a*2, .....
The first line contains two integers *n*, *k* (1<=≀<=*n*<=≀<=2Β·105,<=<=-<=109<=≀<=*k*<=≀<=0). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” ratings of the participants in the initial table.
Print the numbers of participants in the order in which they were removed from the table. Print the initial numbers of the participants, that is, the numbers that the participants had in the initial table.
[ "5 0\n5 3 4 1 2\n", "10 -10\n5 5 1 7 5 1 2 4 9 2\n" ]
[ "2\n3\n4\n", "2\n4\n5\n7\n8\n9\n" ]
Consider the first test sample. 1. Initially the sequence of the contest participants' ratings equals [5, 3, 4, 1, 2]. You can use this sequence to calculate the sequence of rating changes: [0, -9, -13, 8, 14]. According to the problem statement, the application of the participant who won the second place will be con...
[ { "input": "5 0\n5 3 4 1 2", "output": "2\n3\n4" }, { "input": "10 -10\n5 5 1 7 5 1 2 4 9 2", "output": "2\n4\n5\n7\n8\n9" } ]
122
0
0
24,055
103
Russian Roulette
[ "constructive algorithms", "greedy" ]
C. Russian Roulette
2
256
After all the events in Orlando we all know, Sasha and Roma decided to find out who is still the team's biggest loser. Thankfully, Masha found somewhere a revolver with a rotating cylinder of *n* bullet slots able to contain exactly *k* bullets, now the boys have a chance to resolve the problem once and for all. Sash...
The first line contains three integers *n*, *k* and *p* (1<=≀<=*n*<=≀<=1018,<=0<=≀<=*k*<=≀<=*n*,<=1<=≀<=*p*<=≀<=1000) β€” the number of slots in the cylinder, the number of bullets and the number of queries. Then follow *p* lines; they are the queries. Each line contains one integer *x**i* (1<=≀<=*x**i*<=≀<=*n*) the numb...
For each query print "." if the slot should be empty and "X" if the slot should be charged.
[ "3 1 3\n1\n2\n3\n", "6 3 6\n1\n2\n3\n4\n5\n6\n", "5 2 5\n1\n2\n3\n4\n5\n" ]
[ "..X", ".X.X.X", "...XX" ]
The lexicographical comparison of is performed by the &lt; operator in modern programming languages. The *a* string is lexicographically less that the *b* string, if there exists such *i* (1 ≀ *i* ≀ *n*), that *a*<sub class="lower-index">*i*</sub> &lt; *b*<sub class="lower-index">*i*</sub>, and for any *j* (1 ≀ *j* &lt...
[ { "input": "3 1 3\n1\n2\n3", "output": "..X" }, { "input": "6 3 6\n1\n2\n3\n4\n5\n6", "output": ".X.X.X" }, { "input": "5 2 5\n1\n2\n3\n4\n5", "output": "...XX" }, { "input": "4 2 8\n1\n3\n4\n2\n3\n4\n1\n2", "output": "..XX.X.X" }, { "input": "4 0 4\n1\n2\n3\n4", ...
216
0
0
24,065
509
Sums of Digits
[ "dp", "greedy", "implementation" ]
null
null
Vasya had a strictly increasing sequence of positive integers *a*1, ..., *a**n*. Vasya used it to build a new sequence *b*1, ..., *b**n*, where *b**i* is the sum of digits of *a**i*'s decimal representation. Then sequence *a**i* got lost and all that remained is sequence *b**i*. Vasya wonders what the numbers *a**i* c...
The first line contains a single integer number *n* (1<=≀<=*n*<=≀<=300). Next *n* lines contain integer numbers *b*1, ..., *b**n* Β β€” the required sums of digits. All *b**i* belong to the range 1<=≀<=*b**i*<=≀<=300.
Print *n* integer numbers, one per lineΒ β€” the correct option for numbers *a**i*, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the *i*-th number should be equal to *b**i*. If there are multiple sequences with least possible number *a**n*, print any of them. Print ...
[ "3\n1\n2\n3\n", "3\n3\n2\n1\n" ]
[ "1\n2\n3\n", "3\n11\n100\n" ]
none
[ { "input": "3\n1\n2\n3", "output": "1\n2\n3" }, { "input": "3\n3\n2\n1", "output": "3\n11\n100" }, { "input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1", "output": "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" }, { "input": "10\n8\n8\n5\n1\n2\n7\n3\n8\n9\n4", "output": "8\n17\n23\n100\n101\n10...
233
2,560,000
3
24,155
50
Square Equation Roots
[ "math" ]
E. Square Equation Roots
5
256
A schoolboy Petya studies square equations. The equations that are included in the school curriculum, usually look simple: Petya noticed that some equations have two real roots, some of them have only one root and some equations don't have real roots at all. Moreover it turned out that several different square equati...
The single line contains two integers *n* and *m*. (1<=≀<=*n*,<=*m*<=≀<=5000000).
Print a single number which is the number of real roots of the described set of equations.
[ "3 3\n", "1 2\n" ]
[ "12\n", "1\n" ]
In the second test from the statement the following equations are analysed: *b* = 1, *c* = 1: *x*<sup class="upper-index">2</sup> + 2*x* + 1 = 0; The root is *x* =  - 1 *b* = 1, *c* = 2: *x*<sup class="upper-index">2</sup> + 2*x* + 2 = 0; No roots Overall there's one root In the second test the following equatio...
[]
154
31,744,000
-1
24,164
0
none
[ "none" ]
null
null
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics. Polycarp needed to write a code that could, given two words, check whether t...
The first line contains integer *n* (1<=≀<=*n*<=≀<=100<=000) β€” the length of words *S* and *T*. The second line contains word *S*. The third line contains word *T*. Words *S* and *T* consist of lowercase English letters. It is guaranteed that *S* and *T* are distinct words.
Print a single integer β€” the number of distinct words *W* that can be transformed to *S* and *T* due to a typo.
[ "7\nreading\ntrading\n", "5\nsweet\nsheep\n", "3\ntoy\ntry\n" ]
[ "1\n", "0\n", "2\n" ]
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold). In the second sample test the two given words couldn't be obtained from the same word by removing one letter. In the third sample test the two given words could be obtained from either wo...
[ { "input": "7\nreading\ntrading", "output": "1" }, { "input": "5\nsweet\nsheep", "output": "0" }, { "input": "3\ntoy\ntry", "output": "2" }, { "input": "5\nspare\nspars", "output": "2" }, { "input": "1\na\nb", "output": "2" }, { "input": "1\nz\ny", ...
124
1,843,200
3
24,175
417
Cunning Gena
[ "bitmasks", "dp", "greedy", "sortings" ]
null
null
A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his *n* friends that they will solve the problems for him. The participants are offered *m* problems on the contest. For each friend, Gena knows wha...
The first line contains three integers *n*, *m* and *b* (1<=≀<=*n*<=≀<=100; 1<=≀<=*m*<=≀<=20; 1<=≀<=*b*<=≀<=109)Β β€” the number of Gena's friends, the number of problems and the cost of a single monitor. The following 2*n* lines describe the friends. Lines number 2*i* and (2*i*<=+<=1) contain the information about the *...
Print the minimum amount of money Gena needs to spend to solve all the problems. Or print -1, if this cannot be achieved.
[ "2 2 1\n100 1 1\n2\n100 2 1\n1\n", "3 2 5\n100 1 1\n1\n100 1 1\n2\n200 1 2\n1 2\n", "1 2 1\n1 1 1\n1\n" ]
[ "202\n", "205\n", "-1\n" ]
none
[ { "input": "2 2 1\n100 1 1\n2\n100 2 1\n1", "output": "202" }, { "input": "3 2 5\n100 1 1\n1\n100 1 1\n2\n200 1 2\n1 2", "output": "205" }, { "input": "1 2 1\n1 1 1\n1", "output": "-1" }, { "input": "4 2 1\n62 91 1\n1\n2 18 1\n1\n33 76 1\n1\n23 58 1\n1", "output": "-1" ...
124
0
0
24,213
677
Vanya and Label
[ "bitmasks", "combinatorics", "implementation", "strings" ]
null
null
While walking down the street Vanya saw a label "Hide&amp;Seek". Because he is a programmer, he used &amp; as a bitwise AND for these two words represented as a integers in base 64 and got new word. Now Vanya thinks of some string *s* and wants to know the number of pairs of words of length |*s*| (length of *s*), such ...
The only line of the input contains a single word *s* (1<=≀<=|*s*|<=≀<=100<=000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'.
Print a single integerΒ β€” the number of possible pairs of words, such that their bitwise AND is equal to string *s* modulo 109<=+<=7.
[ "z\n", "V_V\n", "Codeforces\n" ]
[ "3\n", "9\n", "130653412\n" ]
For a detailed definition of bitwise AND we recommend to take a look in the corresponding article in Wikipedia. In the first sample, there are 3 possible solutions: 1. *z*&amp;_ = 61&amp;63 = 61 = *z* 1. _&amp;*z* = 63&amp;61 = 61 = *z* 1. *z*&amp;*z* = 61&amp;61 = 61 = *z*
[ { "input": "z", "output": "3" }, { "input": "V_V", "output": "9" }, { "input": "Codeforces", "output": "130653412" }, { "input": "zHsIINYjVtU71kmM9E", "output": "130312847" }, { "input": "fRRNAdMvLFTX21T0FG5gyn7NG0SaIvzGG_g_SO", "output": "547121709" }, { ...
108
7,987,200
3
24,281
546
Soldier and Traveling
[ "flows", "graphs", "math" ]
null
null
In the country there are *n* cities and *m* bidirectional roads between them. Each city has an army. Army of the *i*-th city consists of *a**i* soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road. Check if ...
First line of input consists of two integers *n* and *m* (1<=≀<=*n*<=≀<=100, 0<=≀<=*m*<=≀<=200). Next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=100). Next line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (0<=≀<=*b**i*<=≀<=100). Then *m* lines follow, each of them consists of two in...
If the conditions can not be met output single word "NO". Otherwise output word "YES" and then *n* lines, each of them consisting of *n* integers. Number in the *i*-th line in the *j*-th column should denote how many soldiers should road from city *i* to city *j* (if *i*<=β‰ <=*j*) or how many soldiers should stay in ci...
[ "4 4\n1 2 6 3\n3 5 3 1\n1 2\n2 3\n3 4\n4 2\n", "2 0\n1 2\n2 1\n" ]
[ "YES\n1 0 0 0 \n2 0 0 0 \n0 5 1 0 \n0 0 2 1 \n", "NO" ]
none
[ { "input": "4 4\n1 2 6 3\n3 5 3 1\n1 2\n2 3\n3 4\n4 2", "output": "YES\n1 0 0 0 \n2 0 0 0 \n0 5 1 0 \n0 0 2 1 " }, { "input": "2 0\n1 2\n2 1", "output": "NO" }, { "input": "10 20\n22 3 4 48 12 12 14 37 15 37\n57 29 35 88 6 54 100 32 91 59\n10 1\n4 3\n4 7\n2 4\n7 6\n1 3\n2 5\n5 10\n1 6\n4...
124
10,854,400
0
24,298
285
Permutation Sum
[ "bitmasks", "combinatorics", "dp", "implementation", "meet-in-the-middle" ]
null
null
Permutation *p* is an ordered set of integers *p*1,<=<=*p*2,<=<=...,<=<=*p**n*, consisting of *n* distinct positive integers, each of them doesn't exceed *n*. We'll denote the *i*-th element of permutation *p* as *p**i*. We'll call number *n* the size or the length of permutation *p*1,<=<=*p*2,<=<=...,<=<=*p**n*. Pety...
The single line contains integer *n* (1<=≀<=*n*<=≀<=16).
In the single line print a single non-negative integer β€” the number of such pairs of permutations *a* and *b*, that exists permutation *c* that is sum of *a* and *b*, modulo 1000000007 (109<=+<=7).
[ "3\n", "5\n" ]
[ "18\n", "1800\n" ]
none
[ { "input": "3", "output": "18" }, { "input": "5", "output": "1800" }, { "input": "13", "output": "695720788" }, { "input": "1", "output": "1" }, { "input": "2", "output": "0" }, { "input": "4", "output": "0" }, { "input": "6", "output":...
92
0
0
24,333
117
Elevator
[ "implementation", "math" ]
null
null
And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All *n* participants who have made it to the finals found themselves in a huge *m*-floored 108-star hotel. Of course the first thought to come in a place like this is "How about checking out the elev...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*<=≀<=105,<=2<=≀<=*m*<=≀<=108). Next *n* lines contain information about the participants in the form of three space-separated integers *s**i* *f**i* *t**i* (1<=≀<=*s**i*,<=*f**i*<=≀<=*m*,<=0<=≀<=*t**i*<=≀<=108), described in the problem statem...
Print *n* lines each containing one integer β€” the time of the arrival for each participant to the required floor.
[ "7 4\n2 4 3\n1 2 0\n2 2 0\n1 2 1\n4 3 5\n1 2 2\n4 2 0\n", "5 5\n1 5 4\n1 3 1\n1 3 4\n3 1 5\n4 2 5\n" ]
[ "9\n1\n0\n7\n10\n7\n5\n", "12\n10\n10\n8\n7\n" ]
Let's consider the first sample. The first participant starts at floor *s* = 2 by the time equal to *t* = 3. To get to the floor *f* = 4, he has to wait until the time equals 7, that's the time when the elevator will go upwards for the second time. Then the first participant should get on the elevator and go two floors...
[ { "input": "7 4\n2 4 3\n1 2 0\n2 2 0\n1 2 1\n4 3 5\n1 2 2\n4 2 0", "output": "9\n1\n0\n7\n10\n7\n5" }, { "input": "5 5\n1 5 4\n1 3 1\n1 3 4\n3 1 5\n4 2 5", "output": "12\n10\n10\n8\n7" }, { "input": "5 5\n1 3 4\n4 4 2\n3 2 1\n2 4 0\n1 5 3", "output": "10\n2\n7\n3\n12" }, { "i...
0
0
-1
24,352
576
Invariance of Tree
[ "constructive algorithms", "dfs and similar", "greedy", "trees" ]
null
null
A tree of size *n* is an undirected connected graph consisting of *n* vertices without cycles. Consider some tree with *n* vertices. We call a tree invariant relative to permutation *p*<==<=*p*1*p*2... *p**n*, if for any two vertices of the tree *u* and *v* the condition holds: "vertices *u* and *v* are connected by a...
The first line contains number *n* (1<=≀<=*n*<=≀<=105) β€” the size of the permutation (also equal to the size of the sought tree). The second line contains permutation *p**i* (1<=≀<=*p**i*<=≀<=*n*).
If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print *n*<=-<=1 lines, each of which contains two integers β€” the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices wi...
[ "4\n4 3 2 1\n", "3\n3 1 2\n" ]
[ "YES\n4 1\n4 2\n1 3\n", "NO\n" ]
In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition.
[ { "input": "4\n4 3 2 1", "output": "YES\n4 1\n4 2\n1 3" }, { "input": "3\n3 1 2", "output": "NO" }, { "input": "3\n3 2 1", "output": "YES\n2 1\n2 3" }, { "input": "4\n3 4 1 2", "output": "YES\n4 2\n4 1\n2 3" }, { "input": "5\n5 3 2 1 4", "output": "NO" }, ...
93
0
0
24,361
337
Routine Problem
[ "greedy", "math", "number theory" ]
null
null
Manao has a monitor. The screen of the monitor has horizontal to vertical length ratio *a*:*b*. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratio *c*:*d*. Manao adjusts the view in such a way that the movie preserves the original frame ratio, but also occupies as much space on ...
A single line contains four space-separated integers *a*, *b*, *c*, *d* (1<=≀<=*a*,<=*b*,<=*c*,<=*d*<=≀<=1000).
Print the answer to the problem as "p/q", where *p* is a non-negative integer, *q* is a positive integer and numbers *p* and *q* don't have a common divisor larger than 1.
[ "1 1 3 2\n", "4 3 2 2\n" ]
[ "1/3\n", "1/4\n" ]
Sample 1. Manao's monitor has a square screen. The movie has 3:2 horizontal to vertical length ratio. Obviously, the movie occupies most of the screen if the width of the picture coincides with the width of the screen. In this case, only 2/3 of the monitor will project the movie in the horizontal dimension: <img class=...
[ { "input": "1 1 3 2", "output": "1/3" }, { "input": "4 3 2 2", "output": "1/4" }, { "input": "3 4 2 3", "output": "1/9" }, { "input": "4 4 5 5", "output": "0/1" }, { "input": "1 1 1 1", "output": "0/1" }, { "input": "1000 1000 1000 1000", "output":...
30
0
0
24,369
177
Space Voyage
[ "binary search" ]
null
null
The Smart Beaver from ABBYY plans a space travel on an ultramodern spaceship. During the voyage he plans to visit *n* planets. For planet *i* *a**i* is the maximum number of suitcases that an alien tourist is allowed to bring to the planet, and *b**i* is the number of citizens on the planet. The Smart Beaver is going ...
The first input line contains space-separated integers *n* and *c* β€” the number of planets that the Beaver is going to visit and the number of days he is going to spend traveling, correspondingly. The next *n* lines contain pairs of space-separated integers *a**i*,<=*b**i* (1<=≀<=*i*<=≀<=*n*) β€” the number of suitcases...
Print a single number *k* β€” the number of ways to choose *x* so as to travel for exactly *c* days. If there are infinitely many possible values of *x*, print -1. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "2 5\n1 5\n2 4\n" ]
[ "1\n" ]
In the first example there is only one suitable value *x* = 5. Then the Beaver takes 1 suitcase with 5 presents to the first planet. Here he spends 2 days: he hangs around on the first day, and he gives away five presents on the second day. He takes 2 suitcases with 10 presents to the second planet. Here he spends 3 da...
[ { "input": "2 5\n1 5\n2 4", "output": "1" }, { "input": "1 97\n1 91", "output": "91" }, { "input": "2 79\n1 91\n1 77", "output": "42" }, { "input": "3 100\n8 46\n8 56\n77 98", "output": "1" }, { "input": "71 100\n1 92\n1 94\n1 97\n1 95\n1 100\n1 100\n1 98\n1 99\n1...
186
307,200
0
24,399
255
Code Parsing
[ "implementation" ]
null
null
Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string *s*, consisting of characters "x" and "y", and uses two following operations at runtime: 1. Find two consecutive characters in the string, such that the first of them equals "y", and the ...
The first line contains a non-empty string *s*. It is guaranteed that the string only consists of characters "x" and "y". It is guaranteed that the string consists of at most 106 characters. It is guaranteed that as the result of the algorithm's execution won't be an empty string.
In the only line print the string that is printed as the result of the algorithm's work, if the input of the algorithm input receives string *s*.
[ "x\n", "yxyxy\n", "xxxxxy\n" ]
[ "x\n", "y\n", "xxxx\n" ]
In the first test the algorithm will end after the first step of the algorithm, as it is impossible to apply any operation. Thus, the string won't change. In the second test the transformation will be like this: 1. string "yxyxy" transforms into string "xyyxy"; 1. string "xyyxy" transforms into string "xyxyy"; 1. ...
[ { "input": "x", "output": "x" }, { "input": "yxyxy", "output": "y" }, { "input": "xxxxxy", "output": "xxxx" }, { "input": "yxyyxyyx", "output": "yy" }, { "input": "yxxyxyx", "output": "x" }, { "input": "xxx", "output": "xxx" }, { "input": "...
2,000
11,878,400
0
24,414
0
none
[ "none" ]
null
null
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has *n* cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to *n* from the left to the right. Unfortunately, ...
The first line contains integers *n*, *m* and *k* (1<=≀<=*n*<=≀<=104, 1<=≀<=*m*,<=*k*<=≀<=109). The next line contains *n* numbers. The *i*-th number is equal to the amount of diamonds in the *i*-th cell β€” it is an integer from 0 to 105.
Print a single number β€” the maximum number of diamonds Joe can steal.
[ "2 3 1\n2 3\n", "3 2 2\n4 1 3\n" ]
[ "0", "2" ]
In the second sample Joe can act like this: The diamonds' initial positions are 4 1 3. During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket. By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference...
[ { "input": "2 3 1\n2 3", "output": "0" }, { "input": "3 2 2\n4 1 3", "output": "2" }, { "input": "5 10 10\n7 0 7 0 7", "output": "7" }, { "input": "6 10 4\n1 2 3 4 5 6", "output": "0" }, { "input": "7 5 2\n1 2 3 4 5 6 7", "output": "1" }, { "input": "1...
46
0
0
24,700
0
none
[ "none" ]
null
null
On one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game. It is well known that one month on this planet consists of $n^2$ days, so calendars, represented as square matrix $n$ by $n$ are extremely popular. Weather conditions are even more unusual. Due to the unique composi...
The first and only line of input contains a single integer $n$ ($1 \le n \le 1000\,000$)Β β€” the number of rows and columns in the calendar.
Print one numberΒ β€” number of lucky colorings of the calendar modulo $998244353$
[ "1\n", "2\n", "3\n" ]
[ "3\n", "63\n", "9933\n" ]
In the first sample any coloring is lucky, since the only column contains cells of only one color. In the second sample, there are a lot of lucky colorings, in particular, the following colorings are lucky: While these colorings are not lucky:
[]
46
0
0
24,726
321
Ciel and Duel
[ "dp", "flows", "greedy" ]
null
null
Fox Ciel is playing a card game with her friend Jiro. Jiro has *n* cards, each one has two attributes: *position* (Attack or Defense) and *strength*. Fox Ciel has *m* cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack. Now is Ciel's battle phase, Ciel can do the follo...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100) β€” the number of cards Jiro and Ciel have. Each of the next *n* lines contains a string *position* and an integer *strength* (0<=≀<=*strength*<=≀<=8000) β€” the position and strength of Jiro's current card. Position is the string "ATK" for attack,...
Output an integer: the maximal damage Jiro can get.
[ "2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500\n", "3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001\n", "2 4\nDEF 0\nATK 0\n0\n0\n1\n1\n" ]
[ "3000\n", "992\n", "1\n" ]
In the first test case, Ciel has 3 cards with same *strength*. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damag...
[ { "input": "2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500", "output": "3000" }, { "input": "3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001", "output": "992" }, { "input": "2 4\nDEF 0\nATK 0\n0\n0\n1\n1", "output": "1" }, { "input": "1 1\nATK 100\n99", "output": "0" }, {...
154
512,000
0
24,744
865
Ordering Pizza
[ "binary search", "sortings", "ternary search" ]
null
null
It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly *S* slices. It is known that the *i*-th contestant will eat *s**i* slices of pizza, and ...
The first line of input will contain integers *N* and *S* (1<=≀<=*N*<=≀<=105,<=1<=≀<=*S*<=≀<=105), the number of contestants and the number of slices per pizza, respectively. *N* lines follow. The *i*-th such line contains integers *s**i*, *a**i*, and *b**i* (1<=≀<=*s**i*<=≀<=105,<=1<=≀<=*a**i*<=≀<=105,<=1<=≀<=*b**i*<...
Print the maximum total happiness that can be achieved.
[ "3 12\n3 5 7\n4 6 7\n5 9 5\n", "6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6\n" ]
[ "84\n", "314\n" ]
In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3Β·5 + 4Β·6 + 5Β·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3Β·7 + 4Β·7 + 5Β·5 = 74.
[ { "input": "3 12\n3 5 7\n4 6 7\n5 9 5", "output": "84" }, { "input": "6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6", "output": "314" }, { "input": "1 100\n97065 97644 98402", "output": "9551390130" }, { "input": "1 100000\n1 82372 5587", "output": "82372" }, { ...
30
0
-1
24,758
0
none
[ "none" ]
null
null
Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but why would bears do it? Limak lives in the forest and he decides to draw a tree. Recall that tree is a connected graph consisting of *n* vertices and *n*<=-<=1 edges. Limak chose a tree with *n* vertices. He has infinit...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=105). Next *n*<=-<=1 lines contain description of a tree. *i*-th of them contains two space-separated integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*,<=*a**i*<=β‰ <=*b**i*) denoting an edge between vertices *a**i* and *b**i*. It's guaranteed that given ...
Print "Yes" (without the quotes) if Limak can draw chosen tree. Otherwise, print "No" (without the quotes).
[ "8\n1 2\n1 3\n1 6\n6 4\n6 7\n6 5\n7 8\n", "13\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\n3 10\n4 11\n4 12\n4 13\n" ]
[ "Yes\n", "No\n" ]
none
[ { "input": "8\n1 2\n1 3\n1 6\n6 4\n6 7\n6 5\n7 8", "output": "Yes" }, { "input": "13\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\n3 10\n4 11\n4 12\n4 13", "output": "No" }, { "input": "1", "output": "Yes" }, { "input": "20\n5 15\n20 4\n11 18\n1 14\n18 2\n14 17\n8 10\n13 1\n11 6\n...
202
22,323,200
-1
24,785
226
Anniversary
[ "data structures", "implementation", "math", "matrices", "number theory" ]
null
null
There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathematician Leonardo Fibonacci. Of course, such important anniversary needs much preparations. Dima is sure that it'll be great to learn to solve the following problem by the Big Day: You're given a set *A*, consisting of numb...
The first line contains four space-separated integers *m*, *l*, *r* and *k* (1<=≀<=*m*<=≀<=109;Β 1<=≀<=*l*<=&lt;<=*r*<=≀<=1012;Β 2<=≀<=*k*<=≀<=*r*<=-<=*l*<=+<=1). Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Print a single integer β€” the residue from dividing the sought greatest common divisor by *m*.
[ "10 1 8 2\n", "10 1 8 3\n" ]
[ "3\n", "1\n" ]
none
[ { "input": "10 1 8 2", "output": "3" }, { "input": "10 1 8 3", "output": "1" }, { "input": "10 1 20 2", "output": "5" }, { "input": "4 1 10 2", "output": "1" }, { "input": "1000000000 999000000000 1000000000000 1000000001", "output": "1" }, { "input": ...
92
0
0
24,869
203
Transportation
[ "greedy", "sortings", "two pointers" ]
null
null
Valera came to Japan and bought many robots for his research. He's already at the airport, the plane will fly very soon and Valera urgently needs to bring all robots to the luggage compartment. The robots are self-propelled (they can potentially move on their own), some of them even have compartments to carry other ro...
The first line contains three space-separated integers *n*,<=*d*,<=*S* (1<=≀<=*n*<=≀<=105,<=1<=≀<=*d*,<=*S*<=≀<=109). The first number represents the number of robots, the second one β€” the distance to the luggage compartment and the third one β€” the amount of available fuel. Next *n* lines specify the robots. The *i*-t...
Print two space-separated integers β€” the maximum number of robots Valera can transport to the luggage compartment and the minimum amount of fuel he will need for that. If Valera won't manage to get any robots to the luggage compartment, print two zeroes.
[ "3 10 10\n0 12 10\n1 6 10\n0 1 1\n", "2 7 10\n3 12 10\n5 16 8\n", "4 8 10\n0 12 3\n1 1 0\n0 3 11\n1 6 9\n" ]
[ "2 6\n", "0 0\n", "4 9\n" ]
none
[]
62
0
0
24,870
418
Tricky Password
[ "data structures" ]
null
null
In order to ensure confidentiality, the access to the "Russian Code Cup" problems is password protected during the problem development process. To select a password, the jury can generate a special table that contains *n* columns and the infinite number of rows. To construct a table, the first row is fixed, and all th...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=100000) β€” the number of columns. The second line contains the description of the first row of the table, that is, *n* integers, which are not less than 1 and do not exceed 109. The third line of the input contains an integer *m* (1<=≀<=*m*<=≀<=100000) β€” the number ...
Print an answer for each request of the second type in the order you receive them.
[ "6\n1 2 2 2 3 1\n3\n2 2 3\n1 3 3\n2 3 4\n" ]
[ "2\n1\n" ]
none
[]
30
0
0
25,010
438
The Child and Sequence
[ "data structures", "math" ]
null
null
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array *a*[1],<=*a*[2],<=...,<=*a*[*n...
The first line of input contains two integer: *n*,<=*m* (1<=≀<=*n*,<=*m*<=≀<=105). The second line contains *n* integers, separated by space: *a*[1],<=*a*[2],<=...,<=*a*[*n*]Β (1<=≀<=*a*[*i*]<=≀<=109) β€” initial value of array elements. Each of the next *m* lines begins with a number *type* . - If *type*<==<=1, there...
For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.
[ "5 5\n1 2 3 4 5\n2 3 5 4\n3 3 5\n1 2 5\n2 1 3 3\n1 1 3\n", "10 10\n6 9 6 7 6 1 10 10 9 5\n1 3 9\n2 7 10 9\n2 5 10 8\n1 4 7\n3 3 7\n2 7 9 9\n1 2 4\n1 6 6\n1 5 9\n3 1 10\n" ]
[ "8\n5\n", "49\n15\n23\n1\n9\n" ]
Consider the first testcase: - At first, *a* = {1, 2, 3, 4, 5}. - After operation 1, *a* = {1, 2, 3, 0, 1}. - After operation 2, *a* = {1, 2, 5, 0, 1}. - At operation 3, 2 + 5 + 0 + 1 = 8. - After operation 4, *a* = {1, 2, 2, 0, 1}. <li> At operation 5, 1 + 2 + 2 = 5. <ul>
[ { "input": "5 5\n1 2 3 4 5\n2 3 5 4\n3 3 5\n1 2 5\n2 1 3 3\n1 1 3", "output": "8\n5" }, { "input": "10 10\n6 9 6 7 6 1 10 10 9 5\n1 3 9\n2 7 10 9\n2 5 10 8\n1 4 7\n3 3 7\n2 7 9 9\n1 2 4\n1 6 6\n1 5 9\n3 1 10", "output": "49\n15\n23\n1\n9" }, { "input": "1 1\n1\n1 1 1", "output": "1" ...
1,107
0
0
25,067
727
Games on a CD
[ "data structures", "hashing", "string suffix structures", "strings" ]
null
null
Several years ago Tolya had *n* computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD in clockwise order. The names were distinct, the length of each name was equal to *k*. The names didn't overlap. Thus, there is...
The first line of the input contains two positive integers *n* and *k* (1<=≀<=*n*<=≀<=105, 1<=≀<=*k*<=≀<=105)Β β€” the amount of games Tolya burned to the CD, and the length of each of the names. The second line of the input contains one string consisting of lowercase English lettersΒ β€” the string Tolya wrote on the CD, s...
If there is no answer, print "NO" (without quotes). Otherwise, print two lines. In the first line print "YES" (without quotes). In the second line, print *n* integersΒ β€” the games which names were written on the CD. You should print games in the order they could have been written on the CD, it means, in clockwise order...
[ "3 1\nabc\n4\nb\na\nc\nd\n", "4 2\naabbccdd\n4\ndd\nab\nbc\ncd\n" ]
[ "YES\n2 1 3 \n", "NO\n" ]
none
[ { "input": "3 1\nabc\n4\nb\na\nc\nd", "output": "YES\n2 1 3 " }, { "input": "4 2\naabbccdd\n4\ndd\nab\nbc\ncd", "output": "NO" }, { "input": "2 2\nkdcj\n4\ncj\nkd\njk\ndc", "output": "YES\n3 4 " }, { "input": "10 10\nlgfrjgityzwtmfyygmpsmokiwphewhpoelsvnctwxmpimqvblgrisozncsi...
811
18,022,400
-1
25,074
372
Counting Rectangles is Fun
[ "brute force", "divide and conquer", "dp" ]
null
null
There is an *n*<=Γ—<=*m* rectangular grid, each cell of the grid contains a single integer: zero or one. Let's call the cell on the *i*-th row and the *j*-th column as (*i*,<=*j*). Let's define a "rectangle" as four integers *a*,<=*b*,<=*c*,<=*d* (1<=≀<=*a*<=≀<=*c*<=≀<=*n*;Β 1<=≀<=*b*<=≀<=*d*<=≀<=*m*). Rectangle denotes...
There are three integers in the first line: *n*, *m* and *q* (1<=≀<=*n*,<=*m*<=≀<=40,<=1<=≀<=*q*<=≀<=3Β·105). Each of the next *n* lines contains *m* characters β€” the grid. Consider grid rows are numbered from top to bottom, and grid columns are numbered from left to right. Both columns and rows are numbered starting fr...
For each query output an answer β€” a single integer in a separate line.
[ "5 5 5\n00101\n00000\n00001\n01000\n00001\n1 2 2 4\n4 5 4 5\n1 2 5 2\n2 2 4 5\n4 2 5 3\n", "4 7 5\n0000100\n0000010\n0011000\n0000000\n1 7 2 7\n3 1 3 1\n2 3 4 5\n1 2 2 7\n2 2 4 7\n" ]
[ "10\n1\n7\n34\n5\n", "3\n1\n16\n27\n52\n" ]
For the first example, there is a 5 × 5 rectangular grid, and the first, the second, and the third queries are represented in the following image. - For the first query, there are 10 good rectangles, five 1 × 1, two 2 × 1, two 1 × 2, and one 1 × 3. - For the second query, there is only one 1 × 1 good rectangle. - F...
[ { "input": "5 5 5\n00101\n00000\n00001\n01000\n00001\n1 2 2 4\n4 5 4 5\n1 2 5 2\n2 2 4 5\n4 2 5 3", "output": "10\n1\n7\n34\n5" }, { "input": "4 7 5\n0000100\n0000010\n0011000\n0000000\n1 7 2 7\n3 1 3 1\n2 3 4 5\n1 2 2 7\n2 2 4 7", "output": "3\n1\n16\n27\n52" }, { "input": "10 10 10\n00...
764
53,043,200
3
25,092
229
Towers
[ "dp", "greedy", "two pointers" ]
null
null
The city of D consists of *n* towers, built consecutively on a straight line. The height of the tower that goes *i*-th (from left to right) in the sequence equals *h**i*. The city mayor decided to rebuild the city to make it beautiful. In a beautiful city all towers are are arranged in non-descending order of their hei...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=5000)Β β€” the number of towers in the city. The next line contains *n* space-separated integers: the *i*-th number *h**i* (1<=≀<=*h**i*<=≀<=105) determines the height of the tower that is *i*-th (from left to right) in the initial tower sequence.
Print a single integer β€” the minimum number of operations needed to make the city beautiful.
[ "5\n8 2 7 3 1\n", "3\n5 2 1\n" ]
[ "3\n", "2\n" ]
none
[ { "input": "5\n8 2 7 3 1", "output": "3" }, { "input": "3\n5 2 1", "output": "2" }, { "input": "1\n1", "output": "0" }, { "input": "2\n1 2", "output": "0" }, { "input": "2\n2 1", "output": "1" }, { "input": "3\n1 3 2", "output": "1" }, { "i...
46
0
0
25,125
161
Abracadabra
[ "divide and conquer" ]
null
null
Polycarpus analyzes a string called abracadabra. This string is constructed using the following algorithm: - On the first step the string consists of a single character "a". - On the *k*-th step Polycarpus concatenates two copies of the string obtained on the (*k*<=-<=1)-th step, while inserting the *k*-th characte...
The input consists of a single line containing four integers *l*1, *r*1, *l*2, *r*2 (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=109, *i*<==<=1,<=2). The numbers are separated by single spaces. *l**i* and *r**i* give the indices of the first and the last characters of the *i*-th chosen substring, correspondingly (*i*<==<=1,<=2). The c...
Print a single number β€” the length of the longest common substring of the given strings. If there are no common substrings, print 0.
[ "3 6 1 4\n", "1 1 4 4\n" ]
[ "2\n", "0\n" ]
In the first sample the first substring is "acab", the second one is "abac". These two substrings have two longest common substrings "ac" and "ab", but we are only interested in their length β€” 2. In the second sample the first substring is "a", the second one is "c". These two substrings don't have any common characte...
[ { "input": "3 6 1 4", "output": "2" }, { "input": "1 1 4 4", "output": "0" }, { "input": "2 2 6 6", "output": "1" }, { "input": "73426655 594361930 343984155 989446962", "output": "379149396" }, { "input": "169720415 312105195 670978284 671296539", "output": "...
46
0
3
25,176
847
Dog Show
[ "constructive algorithms", "data structures", "greedy" ]
null
null
A new dog show on TV is starting next week. On the show dogs are required to demonstrate bottomless stomach, strategic thinking and self-preservation instinct. You and your dog are invited to compete with other participants and naturally you want to win! On the show a dog needs to eat as many bowls of dog food as poss...
Two integer numbers are given in the first line - *n* and *T* (1<=≀<=*n*<=≀<=200<=000, 1<=≀<=*T*<=≀<=2Β·109) β€” the number of bowls of food and the time when the dog is stopped. On the next line numbers *t*1,<=*t*2,<=...,<=*t**n* (1<=≀<=*t**i*<=≀<=109) are given, where *t**i* is the moment of time when the *i*-th bowl o...
Output a single integer β€” the maximum number of bowls of food the dog will be able to eat in *T* seconds.
[ "3 5\n1 5 3\n", "1 2\n1\n", "1 1\n1\n" ]
[ "2\n", "1\n", "0\n" ]
In the first example the dog should skip the second bowl to eat from the two bowls (the first and the third).
[ { "input": "3 5\n1 5 3", "output": "2" }, { "input": "1 2\n1", "output": "1" }, { "input": "1 1\n1", "output": "0" }, { "input": "1 1\n2", "output": "0" }, { "input": "2 2\n2 3", "output": "0" }, { "input": "2 3\n2 1", "output": "1" }, { "i...
358
20,684,800
3
25,189
279
Ladder
[ "dp", "implementation", "two pointers" ]
null
null
You've got an array, consisting of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Also, you've got *m* queries, the *i*-th query is described by two integers *l**i*,<=*r**i*. Numbers *l**i*,<=*r**i* define a subsegment of the original array, that is, the sequence of numbers *a**l**i*,<=*a**l**i*<=+<=1,<=*a**l**i*<=+<=2,<=......
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of array elements and the number of queries. The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109), where number *a**i* stands for the *i*-th array element. The following *m* lines cont...
Print *m* lines, in the *i*-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the *i*-th query is the ladder, or word "No" (without the quotes) otherwise.
[ "8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8\n" ]
[ "Yes\nYes\nNo\nYes\nNo\nYes\n" ]
none
[ { "input": "8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8", "output": "Yes\nYes\nNo\nYes\nNo\nYes" }, { "input": "1 1\n6\n1 1", "output": "Yes" }, { "input": "2 5\n1 1\n1 2\n2 2\n2 2\n1 2\n1 2", "output": "Yes\nYes\nYes\nYes\nYes" }, { "input": "10 10\n4 7 2 6 4 8 1 2 1 ...
92
0
0
25,225
504
Misha and XOR
[ "bitmasks" ]
null
null
After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number *x* to the basket, Misha should answer the que...
The first line contains number *m* (1<=≀<=*m*<=≀<=2000), showing how many numbers are scattered around the room. The next *m* lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10600 that doesn't contain leading zeroes.
For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer *k* showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the represent...
[ "7\n7\n6\n5\n4\n3\n2\n1\n", "2\n5\n5\n" ]
[ "0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1\n", "0\n1 0\n" ]
The XOR sum of numbers is the result of bitwise sum of numbers modulo 2.
[ { "input": "7\n7\n6\n5\n4\n3\n2\n1", "output": "0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1" }, { "input": "2\n5\n5", "output": "0\n1 0" }, { "input": "10\n81\n97\n12\n2\n16\n96\n80\n99\n6\n83", "output": "0\n0\n0\n0\n0\n0\n3 0 1 5\n2 1 3\n0\n2 0 3" }, { "input": "10\n15106\n13599\...
108
1,843,200
0
25,250
630
Benches
[ "combinatorics", "math" ]
null
null
The city park of IT City contains *n* east to west paths and *n* north to south paths. Each east to west path crosses each north to south path, so there are *n*2 intersections. The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possib...
The only line of the input contains one integer *n* (5<=≀<=*n*<=≀<=100) β€” the number of east to west paths and north to south paths.
Output one integer β€” the number of ways to place the benches.
[ "5\n" ]
[ "120" ]
none
[ { "input": "5", "output": "120" }, { "input": "6", "output": "4320" }, { "input": "7", "output": "52920" }, { "input": "15", "output": "1082161080" }, { "input": "17", "output": "4594961280" }, { "input": "72", "output": "23491596420472320" }, ...
0
0
-1
25,340
632
Alice, Bob, Two Teams
[ "brute force", "constructive algorithms" ]
null
null
Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are *n* pieces, and the *i*-th piece has a strength *p**i*. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups *A* and *B*. This can be seen as ...
The first line contains integer *n* (1<=≀<=*n*<=≀<=5Β·105) β€” the number of game pieces. The second line contains *n* integers *p**i* (1<=≀<=*p**i*<=≀<=109) β€” the strength of the *i*-th piece. The third line contains *n* characters *A* or *B* β€” the assignment of teams after the first step (after Alice's step).
Print the only integer *a* β€” the maximum strength Bob can achieve.
[ "5\n1 2 3 4 5\nABABA\n", "5\n1 2 3 4 5\nAAAAA\n", "1\n1\nB\n" ]
[ "11\n", "15\n", "1\n" ]
In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing.
[ { "input": "5\n1 2 3 4 5\nABABA", "output": "11" }, { "input": "5\n1 2 3 4 5\nAAAAA", "output": "15" }, { "input": "1\n1\nB", "output": "1" }, { "input": "10\n1 9 7 6 2 4 7 8 1 3\nABBABAABBB", "output": "33" }, { "input": "100\n591 417 888 251 792 847 685 3 182 46...
655
38,297,600
3
25,342
241
Challenging Balloons
[ "constructive algorithms" ]
null
null
Martha β€” as a professional problemsetter β€” proposed a problem for a world-class contest. This is the problem statement: Tomorrow is Nadia's birthday, and Bardia (her brother) is assigned to make the balloons ready! There are *n* balloons (initially empty) that are tied to a straight line on certain positions *x*1,<=*...
Please pay attention! No input will be given to your program for this problem. So you do not have to read from the input anything.
You should output the generated small testcase (which Artha's solution doesn't get it right). It should be in the following format: - First line must contain the only number *n* (1<=≀<=*n*<=≀<=500). - The *i*-th of the next *n* lines should contain the description of the *i*-th balloon β€” two space-separated integers...
[]
[]
The testcase depicted in the figure above (just showing how output should be formatted):
[]
31
0
0
25,397
660
Seating On Bus
[ "implementation" ]
null
null
Consider 2*n* rows of the seats in a bus. *n* rows of the seats on the left and *n* rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4*n*. Consider that *m* (*m*<=≀<=4*n*) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to...
The only line contains two integers, *n* and *m* (1<=≀<=*n*<=≀<=100,<=1<=≀<=*m*<=≀<=4*n*) β€” the number of pairs of rows and the number of passengers.
Print *m* distinct integers from 1 to *m* β€” the order in which the passengers will get off the bus.
[ "2 7\n", "9 36\n" ]
[ "5 1 6 2 7 3 4\n", "19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18\n" ]
none
[ { "input": "2 7", "output": "5 1 6 2 7 3 4" }, { "input": "9 36", "output": "19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18" }, { "input": "1 1", "output": "1" }, { "input": "1 4", "output": "3 1 4 2" }, { "input...
46
0
3
25,402
571
Minimization
[ "dp", "greedy", "sortings" ]
null
null
You've got array *A*, consisting of *n* integers and a positive integer *k*. Array *A* is indexed by integers from 1 to *n*. You need to permute the array elements so that value
The first line contains two integers *n*,<=*k* (2<=≀<=*n*<=≀<=3Β·105, 1<=≀<=*k*<=≀<=*min*(5000,<=*n*<=-<=1)). The second line contains *n* integers *A*[1],<=*A*[2],<=...,<=*A*[*n*] (<=-<=109<=≀<=*A*[*i*]<=≀<=109), separate by spaces β€” elements of the array *A*.
Print the minimum possible value of the sum described in the statement.
[ "3 2\n1 2 4\n", "5 2\n3 -5 3 -5 3\n", "6 3\n4 3 4 3 2 5\n" ]
[ "1\n", "0\n", "3\n" ]
In the first test one of the optimal permutations is 1Β 4Β 2. In the second test the initial order is optimal. In the third test one of the optimal permutations is 2Β 3Β 4Β 4Β 3Β 5.
[ { "input": "3 2\n1 2 4", "output": "1" }, { "input": "5 2\n3 -5 3 -5 3", "output": "0" }, { "input": "6 3\n4 3 4 3 2 5", "output": "3" }, { "input": "2 1\n1 100", "output": "99" }, { "input": "4 3\n1 2 4 8", "output": "1" }, { "input": "5 2\n1 2 8 8 16...
264
15,257,600
0
25,419
952
Ravioli Sort
[ "implementation" ]
null
null
Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with...
The first line of input contains a single number *n* (1<=≀<=*n*<=≀<=10) β€” the size of the array. The second line of input contains *n* space-separated integers *a**i* (1<=≀<=*a**i*<=≀<=100) β€” the elements of the array.
Output "YES" if the array can be sorted using the described procedure and "NO" if it can not.
[ "3\n1 2 3\n", "3\n3 1 2\n" ]
[ "YES\n", "NO\n" ]
In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.
[ { "input": "3\n1 2 3", "output": "YES" }, { "input": "3\n3 1 2", "output": "NO" }, { "input": "1\n13", "output": "YES" }, { "input": "10\n67 67 67 67 67 67 67 67 67 67", "output": "YES" }, { "input": "10\n16 17 16 15 14 15 16 17 16 15", "output": "YES" }, ...
0
0
-1
25,496
915
Almost Acyclic Graph
[ "dfs and similar", "graphs" ]
null
null
You are given a [directed graph](https://en.wikipedia.org/wiki/Directed_graph) consisting of *n* vertices and *m* edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it. Can you make this graph [acyclic](https://en.wikipedia.org/wiki/Directed_acy...
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=500, 1<=≀<=*m*<=≀<=*min*(*n*(*n*<=-<=1),<=100000)) β€” the number of vertices and the number of edges, respectively. Then *m* lines follow. Each line contains two integers *u* and *v* denoting a directed edge going from vertex *u* to vertex *v* (1<=≀<=*u*,<...
If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.
[ "3 4\n1 2\n2 3\n3 2\n3 1\n", "5 6\n1 2\n2 3\n3 2\n3 1\n2 1\n4 5\n" ]
[ "YES\n", "NO\n" ]
In the first example you can remove edge <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and the graph becomes acyclic. In the second example you have to remove at least two edges (for example, <...
[ { "input": "3 4\n1 2\n2 3\n3 2\n3 1", "output": "YES" }, { "input": "5 6\n1 2\n2 3\n3 2\n3 1\n2 1\n4 5", "output": "NO" }, { "input": "2 2\n1 2\n2 1", "output": "YES" }, { "input": "7 7\n1 3\n3 6\n3 7\n5 3\n6 2\n6 7\n7 2", "output": "YES" }, { "input": "500 50\n39...
77
20,172,800
0
25,541
122
Lucky Substring
[ "brute force", "implementation" ]
null
null
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya was delivered a string *s*, containing only digits. He needs to find a string that - ...
The single line contains a non-empty string *s* whose length can range from 1 to 50, inclusive. The string only contains digits. The string can contain leading zeroes.
In the only line print the answer to Petya's problem. If the sought string does not exist, print "-1" (without quotes).
[ "047\n", "16\n", "472747\n" ]
[ "4\n", "-1\n", "7\n" ]
The lexicographical comparison of strings is performed by the &lt; operator in the modern programming languages. String *x* is lexicographically less than string *y* either if *x* is a prefix of *y*, or exists such *i* (1 ≀ *i* ≀ *min*(|*x*|, |*y*|)), that *x*<sub class="lower-index">*i*</sub> &lt; *y*<sub class="lower...
[ { "input": "047", "output": "4" }, { "input": "16", "output": "-1" }, { "input": "472747", "output": "7" }, { "input": "1925", "output": "-1" }, { "input": "5486846414848445484", "output": "4" }, { "input": "516160414", "output": "4" }, { "...
124
0
3
25,788
703
Mishka and trip
[ "implementation", "math" ]
null
null
Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXXΒ β€” beautiful, but little-known northern country. Here are some interesting facts about XXX: 1. XXX consists of *n* cities, *k* of whose (just imagine!) are capital cities. 1. All of cities...
The first line of the input contains two integers *n* and *k* (3<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*k*<=≀<=*n*)Β β€” the number of cities in XXX and the number of capital cities among them. The second line of the input contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≀<=*c**i*<=≀<=10<=000)Β β€” beauty values of the cities. ...
Print the only integerΒ β€” summary price of passing each of the roads in XXX.
[ "4 1\n2 3 1 2\n3\n", "5 2\n3 5 2 2 4\n1 4\n" ]
[ "17", "71" ]
This image describes first sample case: <img class="tex-graphics" src="https://espresso.codeforces.com/9dcb0e62694349977e064303a74efd880af5eb03.png" style="max-width: 100.0%;max-height: 100.0%;"/> It is easy to see that summary price is equal to 17. This image describes second sample case: <img class="tex-graphics"...
[ { "input": "4 1\n2 3 1 2\n3", "output": "17" }, { "input": "5 2\n3 5 2 2 4\n1 4", "output": "71" }, { "input": "3 1\n1 1 1\n1", "output": "3" }, { "input": "3 3\n1 1 1\n1 2 3", "output": "3" }, { "input": "7 7\n6 9 2 7 4 8 7\n1 2 3 4 5 6 7", "output": "775" ...
187
11,468,800
3
25,792
958
Lightsabers (easy)
[ "implementation" ]
null
null
There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of th...
The first line of the input contains *n* (1<=≀<=*n*<=≀<=100) and *m* (1<=≀<=*m*<=≀<=*n*). The second line contains *n* integers in the range {1,<=2,<=...,<=*m*} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains *m* integers *k*1,<=*k*2,<=...,<=*k**m* (with ) – the desired co...
Output YES if an interval with prescribed color counts exists, or output NO if there is none.
[ "5 2\n1 1 2 2 1\n1 2\n" ]
[ "YES\n" ]
none
[ { "input": "5 2\n1 1 2 2 1\n1 2", "output": "YES" }, { "input": "1 1\n1\n1", "output": "YES" }, { "input": "2 1\n1 1\n1", "output": "YES" }, { "input": "2 1\n1 1\n2", "output": "YES" }, { "input": "2 2\n1 2\n1 1", "output": "YES" }, { "input": "3 3\n1 ...
93
7,372,800
0
25,869
656
Out of Controls
[ "*special" ]
null
null
You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them.
The first line of the input contains a single integer *N* (3<=≀<=*N*<=≀<=10). The following *N* lines each contain *N* space-separated integers. *j*th integer in *i*th line *a**ij* is the length of the edge that connects vertices *i* and *j*. *a**ij*<==<=*a**ji*, *a**ii*<==<=0, 1<=≀<=*a**ij*<=≀<=100 for *i*<=β‰ <=*j*.
Output the maximum length of the shortest path between any pair of vertices in the graph.
[ "3\n0 1 1\n1 0 4\n1 4 0\n", "4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0\n" ]
[ "2\n", "5\n" ]
You're running short of keywords, so you can't use some of them:
[ { "input": "3\n0 1 1\n1 0 4\n1 4 0", "output": "2" }, { "input": "4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0", "output": "5" }, { "input": "10\n0 16 67 7 82 44 25 13 25 42\n16 0 24 37 63 20 19 87 55 99\n67 24 0 81 19 71 35 6 20 91\n7 37 81 0 82 89 34 80 7 32\n82 63 19 82 0 42 66 96 42 99\n44 ...
46
0
3
26,055
666
Forensic Examination
[ "data structures", "string suffix structures" ]
null
null
The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word. B...
The first line contains the string *s* (1<=≀<=|*s*|<=≀<=5Β·105) β€” the Absolutely Inadmissible Swearword. The string *s* consists of only lowercase English letters. The second line contains the only integer *m* (1<=≀<=*m*<=≀<=5Β·104) β€” the number of texts of leaflets for expertise. Each of the next *m* lines contains th...
Print *q* lines. The *i*-th of them should contain two integers β€” the number of the text with the most occurences and the number of occurences of the substring [*p**l*,<=*p**r*] of the string *s*. If there are several text numbers print the smallest one.
[ "suffixtree\n3\nsuffixtreesareawesome\ncartesiantreeisworsethansegmenttree\nnyeeheeheee\n2\n1 2 1 10\n1 3 9 10\n" ]
[ "1 1\n3 4\n" ]
none
[]
451
6,656,000
0
26,065
31
TV Game
[ "dp" ]
E. TV Game
2
256
There is a new TV game on BerTV. In this game two players get a number *A* consisting of 2*n* digits. Before each turn players determine who will make the next move. Each player should make exactly *n* moves. On it's turn *i*-th player takes the leftmost digit of *A* and appends it to his or her number *S**i*. After th...
The first line contains integer *n* (1<=≀<=*n*<=≀<=18). The second line contains integer *A* consisting of exactly 2*n* digits. This number can have leading zeroes.
Output the line of 2*n* characters Β«HΒ» and Β«MΒ» β€” the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly *n* moves. If there are several solutions, output any of them.
[ "2\n1234\n", "2\n9911\n" ]
[ "HHMM", "HMHM" ]
none
[ { "input": "2\n1234", "output": "HHMM" }, { "input": "2\n9911", "output": "HMHM" }, { "input": "2\n0153", "output": "HHMM" }, { "input": "3\n614615", "output": "HHHMMM" }, { "input": "4\n21305374", "output": "HHHHMMMM" }, { "input": "4\n00013213", ...
216
2,252,800
-1
26,133
0
none
[ "none" ]
null
null
ΠŸΠ°ΠΌΡΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° состоит ΠΈΠ· *n* ячССк, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ выстроСны Π² ряд. ΠŸΡ€ΠΎΠ½ΡƒΠΌΠ΅Ρ€ΡƒΠ΅ΠΌ ячСйки ΠΎΡ‚ 1 Π΄ΠΎ *n* слСва Π½Π°ΠΏΡ€Π°Π²ΠΎ. ΠŸΡ€ΠΎ ΠΊΠ°ΠΆΠ΄ΡƒΡŽ ячСйку извСстно, свободна ΠΎΠ½Π° ΠΈΠ»ΠΈ ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ‚ ΠΊΠ°ΠΊΠΎΠΌΡƒ-Π»ΠΈΠ±ΠΎ процСссу (Π² Ρ‚Π°ΠΊΠΎΠΌ случаС извСстСн процСсс, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ ΠΎΠ½Π° ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ‚). Для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ процСсса извСстно, Ρ‡Ρ‚ΠΎ ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°Ρ‰ΠΈΠ΅ Π΅ΠΌΡƒ ячСйки Π·Π°Π½ΠΈΠΌΠ°ΡŽΡ‚ ...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… записано число *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” количСство ячССк Π² памяти ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π°. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… ΡΠ»Π΅Π΄ΡƒΡŽΡ‚ *n* Ρ†Π΅Π»Ρ‹Ρ… чисСл *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*), Π³Π΄Π΅ *a**i* Ρ€Π°Π²Π½ΠΎ Π»ΠΈΠ±ΠΎ 0 (это ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ *i*-я ячСйка памяти свободна), Π»ΠΈΠ±ΠΎ Π½ΠΎΠΌΠ΅Ρ€Ρƒ процСсса, ΠΊ...
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число — минимальноС количСство ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π½ΡƒΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ для Π΄Π΅Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚Π°Ρ†ΠΈΠΈ памяти.
[ "4\n0 2 2 1\n", "8\n0 8 8 8 0 4 4 2\n" ]
[ "2\n", "4\n" ]
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ тСстовом ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ достаточно Π΄Π²ΡƒΡ… ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ: 1. ΠŸΠ΅Ρ€Π΅ΠΏΠΈΡΠ°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· Ρ‚Ρ€Π΅Ρ‚ΡŒΠ΅ΠΉ ячСйки Π² ΠΏΠ΅Ρ€Π²ΡƒΡŽ. ПослС этого ΠΏΠ°ΠΌΡΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° ΠΏΡ€ΠΈΠΌΠ΅Ρ‚ Π²ΠΈΠ΄: 2Β 2Β 0Β 1. 1. ΠŸΠ΅Ρ€Π΅ΠΏΠΈΡΠ°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· Ρ‡Π΅Ρ‚Π²Π΅Ρ€Ρ‚ΠΎΠΉ ячСйки Π² Ρ‚Ρ€Π΅Ρ‚ΡŒΡŽ. ПослС этого ΠΏΠ°ΠΌΡΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° ΠΏΡ€ΠΈΠΌΠ΅Ρ‚ Π²ΠΈΠ΄: 2Β 2Β 1Β 0.
[ { "input": "4\n0 2 2 1", "output": "2" }, { "input": "8\n0 8 8 8 0 4 4 2", "output": "4" }, { "input": "5\n0 0 0 1 1", "output": "2" }, { "input": "6\n0 0 0 3 0 0", "output": "1" }, { "input": "10\n0 10 10 0 0 3 3 0 0 0", "output": "3" }, { "input": "1...
404
34,713,600
3
26,273
95
Hockey
[ "implementation", "strings" ]
A. Hockey
2
256
Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name *w* and the collection of forbidden substrings *s*1,<=*s*2,<=...,<=*s**n*. All those strings consist of uppercase and low...
The first line contains the only integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of forbidden substrings in the collection. Next *n* lines contain these substrings. The next line contains string *w*. All those *n*<=+<=1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not e...
Output the only line β€” Petya's resulting string with the maximum number of letters *letter*. If there are several answers then output the one that comes first lexicographically. The lexicographical comparison is performed by the standard &lt; operator in modern programming languages. The line *a* is lexicographically ...
[ "3\nbers\nucky\nelu\nPetrLoveLuckyNumbers\nt\n", "4\nhello\nparty\nabefglghjdhfgj\nIVan\npetrsmatchwin\na\n", "2\naCa\ncba\nabAcaba\nc\n" ]
[ "PetrLovtTttttNumtttt\n", "petrsmatchwin\n", "abCacba\n" ]
none
[ { "input": "3\nbers\nucky\nelu\nPetrLoveLuckyNumbers\nt", "output": "PetrLovtTttttNumtttt" }, { "input": "4\nhello\nparty\nabefglghjdhfgj\nIVan\npetrsmatchwin\na", "output": "petrsmatchwin" }, { "input": "2\naCa\ncba\nabAcaba\nc", "output": "abCacba" }, { "input": "3\nlol\nhe...
404
19,763,200
0
26,381
193
Cutting Figure
[ "constructive algorithms", "graphs", "trees" ]
null
null
You've gotten an *n*<=Γ—<=*m* sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as *A*. Set *A* is connected. Your task is to find the minimum number of squares that we can delete from set *A* to make it not connected. A set of painted squares is called connected, if for...
The first input line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=50) β€” the sizes of the sheet of paper. Each of the next *n* lines contains *m* characters β€” the description of the sheet of paper: the *j*-th character of the *i*-th line equals either "#", if the corresponding square is paint...
On the first line print the minimum number of squares that need to be deleted to make set *A* not connected. If it is impossible, print -1.
[ "5 4\n####\n#..#\n#..#\n#..#\n####\n", "5 5\n#####\n#...#\n#####\n#...#\n#####\n" ]
[ "2\n", "2\n" ]
In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore. The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The de...
[ { "input": "5 4\n####\n#..#\n#..#\n#..#\n####", "output": "2" }, { "input": "5 5\n#####\n#...#\n#####\n#...#\n#####", "output": "2" }, { "input": "1 10\n.########.", "output": "1" }, { "input": "1 1\n#", "output": "-1" }, { "input": "3 3\n.#.\n###\n.#.", "outp...
124
1,536,000
0
26,399
754
Vladik and chat
[ "brute force", "constructive algorithms", "dp", "implementation", "strings" ]
null
null
Recently Vladik discovered a new entertainmentΒ β€” coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them. At first, he need to download *t* chats. Vladik coded a script which should have downloaded the chats, however, something went wrong...
The first line contains single integer *t* (1<=≀<=*t*<=≀<=10) β€” the number of chats. The *t* chats follow. Each chat is given in the following format. The first line of each chat description contains single integer *n* (1<=≀<=*n*<=≀<=100)Β β€” the number of users in the chat. The next line contains *n* space-separated d...
Print the information about the *t* chats in the following format: If it is not possible to recover senders, print single line "Impossible" for this chat. Otherwise print *m* messages in the following format: &lt;username&gt;:&lt;text&gt; If there are multiple answers, print any of them.
[ "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi\n", "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine\n", "2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2...
[ "netman: Hello, Vladik!\nVladik: Hi\n", "Impossible\n", "Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course.\n" ]
none
[ { "input": "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi", "output": "netman: Hello, Vladik!\nVladik: Hi" }, { "input": "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine", "output": "Impossible" }, { "input": "2\n3\nnetman vladik Fedosik\n2\n?: users are n...
140
3,686,400
0
26,401
394
Lightbulb for Minister
[ "geometry" ]
null
null
The Minister for education is coming! Naturally, nobody wants to perform poorly in front of such a honored guest. However, two hours before the arrival it turned out that one of the classes has a malfunctioning lightbulb β€” for some reason it doesn't get enough energy. The solution was found quickly: all we've got to do...
The first line contains integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of generators. Each of the next *n* lines contains a pair of integers *x**i*, *y**i*, representing the coordinates of the *i*-th generator in the plane of the class ceiling. It's guaranteed that no two generators have the same location. The next line...
Print a single real number β€” the minimum value of the sum of squares of distances from the generators to the point of the lightbulb's optimal position. The answer will be considered valid if its absolute or relative error doesn't exceed 10<=-<=4.
[ "4\n3 2\n3 4\n5 4\n5 2\n4\n3 3\n4 4\n5 3\n4 2\n" ]
[ "8.00000000" ]
We'll define a strictly convex polygon as a convex polygon with the following property: no three vertices of the polygon lie on the same line.
[]
46
0
0
26,422
0
none
[ "none" ]
null
null
This is an interactive problem. Jury has hidden a permutation *p* of integers from 0 to *n*<=-<=1. You know only the length *n*. Remind that in permutation all integers are distinct. Let *b* be the inverse permutation for *p*, i.e. *p**b**i*<==<=*i* for all *i*. The only thing you can do is to ask xor of elements *p*...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=5000) β€” the length of the hidden permutation. You should read this integer first.
When your program is ready to print the answer, print three lines. In the first line print "!". In the second line print single integer *answers*_*cnt*Β β€” the number of permutations indistinguishable from the hidden one, including the hidden one. In the third line print *n* integers *p*0,<=*p*1,<=...,<=*p**n*<=-<=1 ...
[ "3\n0\n0\n3\n2\n3\n2", "4\n2\n3\n2\n0\n2\n3\n2\n0" ]
[ "? 0 0\n? 1 1\n? 1 2\n? 0 2\n? 2 1\n? 2 0\n!\n1\n0 1 2", "? 0 1\n? 1 2\n? 2 3\n? 3 3\n? 3 2\n? 2 1\n? 1 0\n? 0 0\n!\n2\n3 1 2 0" ]
xor operation, or bitwise exclusive OR, is an operation performed over two integers, in which the *i*-th digit in binary representation of the result is equal to 1 if and only if exactly one of the two integers has the *i*-th digit in binary representation equal to 1. For more information, see [here](https://en.wikiped...
[ { "input": "3\n0 1 2", "output": "1\n0 1 2 " }, { "input": "4\n3 1 2 0", "output": "2\n0 2 1 3 " }, { "input": "4\n3 2 1 0", "output": "4\n0 1 2 3 " }, { "input": "8\n2 3 0 1 4 5 6 7", "output": "4\n0 1 2 3 6 7 4 5 " }, { "input": "1\n0", "output": "1\n0 " }...
2,000
716,800
0
26,458
935
Fafa and Ancient Mathematics
[ "dfs and similar", "dp", "trees" ]
null
null
Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as Ahmes arithmetic expression. An Ahmes arithmetic expression can be defined as: - "*d*" is an Ahmes arithmetic...
The first line contains a string *E* (1<=≀<=|*E*|<=≀<=104) β€” a valid Ahmes arithmetic expression. All operators are erased and replaced with '?'. The second line contains two space-separated integers *P* and *M* (0<=≀<=*min*(*P*,<=*M*)<=≀<=100) β€” the number of plus and minus operators, respectively. It is guaranteed...
Print one line containing the answer to the problem.
[ "(1?1)\n1 0\n", "(2?(1?2))\n1 1\n", "((1?(5?7))?((6?2)?7))\n3 2\n", "((1?(5?7))?((6?2)?7))\n2 3\n" ]
[ "2\n", "1\n", "18\n", "16\n" ]
- The first sample will be (1 + 1)  =  2. - The second sample will be (2 + (1 - 2))  =  1. - The third sample will be ((1 - (5 - 7)) + ((6 + 2) + 7))  =  18. - The fourth sample will be ((1 + (5 + 7)) - ((6 - 2) - 7))  =  16.
[ { "input": "(1?1)\n1 0", "output": "2" }, { "input": "(2?(1?2))\n1 1", "output": "1" }, { "input": "((1?(5?7))?((6?2)?7))\n3 2", "output": "18" }, { "input": "((1?(5?7))?((6?2)?7))\n2 3", "output": "16" }, { "input": "((4?3)?(((2?(4?((4?((2?(3?(7?3)))?(((((3?(6?2)...
46
0
0
26,464
228
Zigzag
[ "data structures" ]
null
null
The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function. The Zigag's sequence with the...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” The number of elements in array *a*. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” the elements of the array. The third line contains integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of operations. Next ...
For each Zigzag operation print the calculated value of the Zigzag function on a single line. Print the values for Zigzag functions in the order, in which they are given in the input. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d sp...
[ "5\n2 3 1 5 5\n4\n2 2 3 2\n2 1 5 3\n1 3 5\n2 1 5 3\n" ]
[ "5\n26\n38\n" ]
Explanation of the sample test: - Result of the first operation is *Z*(2, 3, 2) = 3Β·1 + 1Β·2 = 5. - Result of the second operation is *Z*(1, 5, 3) = 2Β·1 + 3Β·2 + 1Β·3 + 5Β·2 + 5Β·1 = 26. - After the third operation array *a* is equal to 2, 3, 5, 5, 5. - Result of the forth operation is *Z*(1, 5, 3) = 2Β·1 + 3Β·2 + 5Β·3 +...
[]
92
0
0
26,509
317
Game with Powers
[ "dp", "games" ]
null
null
Vasya and Petya wrote down all integers from 1 to *n* to play the "powers" game (*n* can be quite large; however, Vasya and Petya are not confused by this fact). Players choose numbers in turn (Vasya chooses first). If some number *x* is chosen at the current turn, it is forbidden to choose *x* or all of its other pos...
Input contains single integer *n* (1<=≀<=*n*<=≀<=109).
Print the name of the winner β€” "Vasya" or "Petya" (without quotes).
[ "1\n", "2\n", "8\n" ]
[ "Vasya\n", "Petya\n", "Petya\n" ]
In the first sample Vasya will choose 1 and win immediately. In the second sample no matter which number Vasya chooses during his first turn, Petya can choose the remaining number and win.
[ { "input": "1", "output": "Vasya" }, { "input": "2", "output": "Petya" }, { "input": "8", "output": "Petya" }, { "input": "52", "output": "Petya" }, { "input": "53", "output": "Vasya" }, { "input": "3", "output": "Vasya" }, { "input": "4", ...
92
0
0
26,563
758
Broken Tree
[ "dfs and similar", "dp", "graphs", "greedy", "trees" ]
null
null
You are given a tree that has *n* vertices, which are numbered from 1 to *n*, where the vertex number one is the root. Each edge has weight *w**i* and strength *p**i*. Botanist Innokentiy, who is the only member of the jury of the Olympiad in Informatics, doesn't like broken trees. The tree is broken if there is suc...
The first line contains the integer *n* (1<=≀<=*n*<=≀<=2Β·105)Β β€” the number of vertices in the tree. The next *n*<=-<=1 lines contains the description of edges. Each line contains four integers *x*, *y*, *w*, *p* (1<=≀<=*x*,<=*y*<=≀<=*n*,<=1<=≀<=*w*<=≀<=109,<=0<=≀<=*p*<=≀<=109), where *x* and *y*Β β€” vertices which connec...
If it is impossible to get unbroken tree from the given tree, print -1 in the only line. Otherwise, the output data should contain *n* lines: In the first line print the number *n*Β β€” the number of vertices on the tree. In the next *n*<=-<=1 lines print the description of edges of the resulting tree. Each line shoul...
[ "3\n1 3 5 7\n3 2 4 3\n", "4\n1 3 2 3\n3 4 5 1\n3 2 3 3\n", "5\n1 2 2 4\n2 4 1 9\n4 5 5 6\n4 3 4 8\n", "7\n1 2 5 2\n2 3 4 3\n1 4 3 7\n4 5 4 1\n4 6 3 2\n6 7 1 6\n" ]
[ "3\n1 3 5 7\n3 2 4 3\n", "-1", "5\n1 2 2 4\n2 4 1 9\n4 5 1 2\n4 3 2 6\n", "7\n1 2 5 2\n2 3 2 1\n1 4 3 7\n4 5 3 0\n4 6 3 2\n6 7 1 6\n" ]
none
[]
46
0
0
26,571
534
Simplified Nonogram
[ "bitmasks", "dp", "hashing", "meet-in-the-middle" ]
null
null
In this task you have to write a program dealing with nonograms on fields no larger than 5<=Γ—<=20. Simplified nonogram is a task where you have to build such field (each cell is either white or black) that satisfies the given information about rows and columns. For each row and each column the number of contiguous bla...
In the first line there follow two integers *n*, *m* (1<=≀<=*n*<=≀<=5,<=1<=≀<=*m*<=≀<=20) β€” number of rows and number of columns respectively. Second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* where *a**i* is the number of contiguous black segments in *i*-th row of the field. Similarly, third line contain...
Output any possible solution. Output should consist of *n* lines each containing *m* characters. Denote white cell as "." and black cell as "*".
[ "3 5\n2 3 2\n1 0 1 2 1\n", "3 3\n2 1 2\n2 1 2\n", "3 3\n1 0 1\n2 2 2\n" ]
[ "*.**.\n*.*.*\n*..**", "*.*\n.*.\n*.*\n", "***\n...\n***\n" ]
none
[ { "input": "3 5\n2 3 2\n1 0 1 2 1", "output": "*..**\n*.*.*\n*..**" }, { "input": "3 3\n2 1 2\n2 1 2", "output": "*.*\n.*.\n*.*" }, { "input": "3 3\n1 0 1\n2 2 2", "output": "***\n...\n***" }, { "input": "1 1\n1\n1", "output": "*" }, { "input": "1 1\n0\n0", "o...
30
0
0
26,608
919
Congruence Equation
[ "chinese remainder theorem", "math", "number theory" ]
null
null
Given an integer $x$. Your task is to find out how many positive integers $n$ ($1 \leq n \leq x$) satisfy $$n \cdot a^n \equiv b \quad (\textrm{mod}\;p),$$ where $a, b, p$ are all known constants.
The only line contains four integers $a,b,p,x$ ($2 \leq p \leq 10^6+3$, $1 \leq a,b &lt; p$, $1 \leq x \leq 10^{12}$). It is guaranteed that $p$ is a prime.
Print a single integer: the number of possible answers $n$.
[ "2 3 5 8\n", "4 6 7 13\n", "233 233 10007 1\n" ]
[ "2\n", "1\n", "1\n" ]
In the first sample, we can see that $n=2$ and $n=8$ are possible answers.
[ { "input": "2 3 5 8", "output": "2" }, { "input": "4 6 7 13", "output": "1" }, { "input": "233 233 10007 1", "output": "1" }, { "input": "338792 190248 339821 152634074578", "output": "449263" }, { "input": "629260 663548 739463 321804928248", "output": "43481...
701
2,150,400
3
26,712
868
Huge Strings
[ "bitmasks", "brute force", "dp", "implementation", "strings" ]
null
null
You are given *n* strings *s*1,<=*s*2,<=...,<=*s**n* consisting of characters 0 and 1. *m* operations are performed, on each of them you concatenate two existing strings into a new one. On the *i*-th operation the concatenation *s**a**i**s**b**i* is saved into a new string *s**n*<=+<=*i* (the operations are numbered st...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=100)Β β€” the number of strings. The next *n* lines contain strings *s*1,<=*s*2,<=...,<=*s**n* (1<=≀<=|*s**i*|<=≀<=100), one per line. The total length of strings is not greater than 100. The next line contains single integer *m* (1<=≀<=*m*<=≀<=100)Β β€” the number o...
Print *m* lines, each should contain one integerΒ β€” the answer to the question after the corresponding operation.
[ "5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4\n" ]
[ "1\n2\n0\n" ]
On the first operation, a new string "0110" is created. For *k* = 1 the two possible binary strings of length *k* are "0" and "1", they are substrings of the new string. For *k* = 2 and greater there exist strings of length *k* that do not appear in this string (for *k* = 2 such string is "00"). So the answer is 1. On...
[ { "input": "5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4", "output": "1\n2\n0" }, { "input": "5\n01\n1\n0011\n0\n01\n6\n5 5\n3 2\n4 2\n6 7\n5 1\n9 7", "output": "1\n1\n1\n2\n1\n2" }, { "input": "5\n111101000111100011100110000100\n000111001\n01101000\n000011010010001001100100000001010010011...
2,000
18,534,400
0
26,779
615
Longtail Hedgehog
[ "dp", "graphs" ]
null
null
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of *n* points connected by *m* segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a...
First line of the input contains two integers *n* and *m*(2<=≀<=*n*<=≀<=100<=000, 1<=≀<=*m*<=≀<=200<=000)Β β€” the number of points and the number segments on the picture respectively. Then follow *m* lines, each containing two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*)Β β€” the numbers o...
Print the maximum possible value of the hedgehog's beauty.
[ "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7\n", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n" ]
[ "9\n", "12\n" ]
The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3Β·3 = 9. <img class="tex-graphics" src="...
[ { "input": "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7", "output": "9" }, { "input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4", "output": "12" }, { "input": "5 7\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2", "output": "9" }, { "input": "5 9\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2\n1 5\n2 5", "output"...
623
41,984,000
3
26,816
909
Coprocessor
[ "dfs and similar", "dp", "graphs", "greedy" ]
null
null
You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depen...
The first line contains two space-separated integers *N* (1<=≀<=*N*<=≀<=105) β€” the total number of tasks given, and *M* (0<=≀<=*M*<=≀<=105) β€” the total number of dependencies between tasks. The next line contains *N* space-separated integers . If *E**i*<==<=0, task *i* can only be executed on the main processor, other...
Output one line containing an integer β€” the minimal number of coprocessor calls necessary to execute the program.
[ "4 3\n0 1 0 1\n0 1\n1 2\n2 3\n", "4 3\n1 1 1 0\n0 1\n0 2\n3 0\n" ]
[ "2\n", "1\n" ]
In the first test, tasks 1 and 3 can only be executed on the coprocessor. The dependency graph is linear, so the tasks must be executed in order 3 -&gt; 2 -&gt; 1 -&gt; 0. You have to call coprocessor twice: first you call it for task 3, then you execute task 2 on the main processor, then you call it for for task 1, an...
[ { "input": "4 3\n0 1 0 1\n0 1\n1 2\n2 3", "output": "2" }, { "input": "4 3\n1 1 1 0\n0 1\n0 2\n3 0", "output": "1" }, { "input": "10 39\n0 1 0 1 0 1 1 0 1 1\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n3 4\n3 6\n3 7\n3 8\n3 9\n...
0
0
-1
26,885
570
Pig and Palindromes
[ "combinatorics", "dp" ]
null
null
Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of *n* rows and *m* columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to *n*, and the columns β€” from left to right with numbers from 1 to *m*. Let's ...
The first line contains two integers *n*,<=*m* (1<=≀<=*n*,<=*m*<=≀<=500) β€” the height and width of the field. Each of the following *n* lines contains *m* lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by dif...
Print a single integer β€” the number of beautiful paths modulo 109<=+<=7.
[ "3 4\naaab\nbaaa\nabba\n" ]
[ "3" ]
Picture illustrating possibilities for the sample test. <img class="tex-graphics" src="https://espresso.codeforces.com/bf73568d1cf80d89f66d4e472a91ae0339af83a2.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/1e870880d976e642be85498efab6bbd10bae8a84...
[ { "input": "3 4\naaab\nbaaa\nabba", "output": "3" }, { "input": "2 2\nab\naa", "output": "2" }, { "input": "3 5\nqqqrw\nwqqtw\newqqq", "output": "3" }, { "input": "1 5\nabbba", "output": "1" }, { "input": "1 5\nabbbb", "output": "0" }, { "input": "1 4\...
2,885
13,107,200
3
26,902
678
The Same Calendar
[ "implementation" ]
null
null
The girl Taylor has a beautiful calendar for the year *y*. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. The calendar is so beautiful that she wants to know what is the next year after *y* when the calendar will be exactly the same. Help ...
The only line contains integer *y* (1000<=≀<=*y*<=&lt;<=100'000) β€” the year of the calendar.
Print the only integer *y*' β€” the next year after *y* when the calendar will be the same. Note that you should find the first year after *y* with the same calendar.
[ "2016\n", "2000\n", "50501\n" ]
[ "2044\n", "2028\n", "50507\n" ]
Today is Monday, the 13th of June, 2016.
[ { "input": "2016", "output": "2044" }, { "input": "2000", "output": "2028" }, { "input": "50501", "output": "50507" }, { "input": "1000", "output": "1006" }, { "input": "1900", "output": "1906" }, { "input": "1899", "output": "1905" }, { "i...
155
0
3
26,911
911
Stack Sorting
[ "constructive algorithms", "data structures", "greedy", "implementation" ]
null
null
Let's suppose you have an array *a*, a stack *s* (initially empty) and an array *b* (also initially empty). You may perform the following operations until both *a* and *s* are empty: - Take the first element of *a*, push it into *s* and remove it from *a* (if *a* is not empty); - Take the top element from *s*, appe...
The first line contains two integers *n* and *k* (2<=≀<=*n*<=≀<=200000, 1<=≀<=*k*<=&lt;<=*n*) β€” the size of a desired permutation, and the number of elements you are given, respectively. The second line contains *k* integers *p*1, *p*2, ..., *p**k* (1<=≀<=*p**i*<=≀<=*n*) β€” the first *k* elements of *p*. These integers...
If it is possible to restore a stack-sortable permutation *p* of size *n* such that the first *k* elements of *p* are equal to elements given in the input, print lexicographically maximal such permutation. Otherwise print -1.
[ "5 3\n3 2 1\n", "5 3\n2 3 1\n", "5 1\n3\n", "5 2\n3 4\n" ]
[ "3 2 1 5 4 ", "-1\n", "3 2 1 5 4 ", "-1\n" ]
none
[ { "input": "5 3\n3 2 1", "output": "3 2 1 5 4 " }, { "input": "5 3\n2 3 1", "output": "-1" }, { "input": "5 1\n3", "output": "3 2 1 5 4 " }, { "input": "5 2\n3 4", "output": "-1" }, { "input": "100000 1\n98419", "output": "98419 98418 98417 98416 98415 98414 9...
576
23,040,000
0
26,946
0
none
[ "none" ]
null
null
There is a social website with *n* fanpages, numbered 1 through *n*. There are also *n* companies, and the *i*-th company owns the *i*-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where *i* fol...
The first line of the input contains two integers *n* and *q* (3<=≀<=*n*<=≀<=100<=000, 1<=≀<=*q*<=≀<=100<=000)Β β€” the number of fanpages and the number of queries, respectively. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≀<=*t**i*<=≀<=1012) where *t**i* denotes the number of people subscribing...
For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively.
[ "5 12\n10 20 30 40 50\n2 3 4 5 2\n2 1\n2 2\n2 3\n2 4\n2 5\n1 4 2\n2 1\n2 2\n2 3\n2 4\n2 5\n3\n" ]
[ "10\n36\n28\n40\n36\n9\n57\n27\n28\n29\n9 57\n" ]
In the sample test, there are 5 fanpages. The *i*-th of them has *i*Β·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from *A* to *B* means that *A* follows *B*. The left drawing shows the initial situation. The first company gets income <img align="middle" class="tex-formula" src=...
[]
46
0
0
26,953
191
Fools and Roads
[ "data structures", "dfs and similar", "trees" ]
null
null
They say that Berland has exactly two problems, fools and roads. Besides, Berland has *n* cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between eac...
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of cities. Each of the next *n*<=-<=1 lines contains two space-separated integers *u**i*,<=*v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*), that means that there is a road connecting cities *u**i* and *v**i*. The next line conta...
Print *n*<=-<=1 integer. The integers should be separated by spaces. The *i*-th number should equal the number of fools who can go on the *i*-th road. The roads are numbered starting from one in the order, in which they occur in the input.
[ "5\n1 2\n1 3\n2 4\n2 5\n2\n1 4\n3 5\n", "5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5\n" ]
[ "2 1 1 1 \n", "3 1 1 1 \n" ]
In the first sample the fool number one goes on the first and third road and the fool number 3 goes on the second, first and fourth ones. In the second sample, the fools number 1, 3 and 5 go on the first road, the fool number 5 will go on the second road, on the third road goes the fool number 3, and on the fourth one...
[ { "input": "5\n1 2\n1 3\n2 4\n2 5\n2\n1 4\n3 5", "output": "2 1 1 1 " }, { "input": "5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5", "output": "3 1 1 1 " } ]
966
49,561,600
-1
26,957
761
Dasha and Password
[ "brute force", "dp", "implementation" ]
null
null
After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length *n* which satisfies the following requirements: - There is at least one digit in the string, - There is at least one lowercase (small) letter of the Latin alphabet in the string,...
The first line contains two integers *n*, *m* (3<=≀<=*n*<=≀<=50,<=1<=≀<=*m*<=≀<=50) β€” the length of the password and the length of strings which are assigned to password symbols. Each of the next *n* lines contains the string which is assigned to the *i*-th symbol of the password string. Its length is *m*, it consist...
Print one integer β€” the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.
[ "3 4\n1**2\na3*0\nc4**\n", "5 5\n#*&amp;#*\n*a1c&amp;\n&amp;q2w*\n#a3c#\n*&amp;#*&amp;\n" ]
[ "1\n", "3\n" ]
In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer. In the second test one of possible algorithms will be: - to move the pointer of the second symbol once to the right. - to move the pointer of the third symbol twice to the right.
[ { "input": "3 4\n1**2\na3*0\nc4**", "output": "1" }, { "input": "5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&", "output": "3" }, { "input": "5 2\n&l\n*0\n*9\n*#\n#o", "output": "2" }, { "input": "25 16\nvza**ooxkmd#*ywa\ndip#*#&ef&z&&&pv\nwggob&&72#*&&nku\nrsb##*&jm&#u**te\nzif#lu#...
155
819,200
0
27,032
159
Friends or Not
[ "*special", "greedy", "implementation" ]
null
null
Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that ...
The first line of the input contains two integers *n* and *d* (1<=≀<=*n*,<=*d*<=≀<=1000). The next *n* lines contain the messages log. The *i*-th line contains one line of the log formatted as "*A**i* *B**i* *t**i*" (without the quotes), which means that user *A**i* sent a message to user *B**i* at time *t**i* (1<=≀<=*...
In the first line print integer *k* β€” the number of pairs of friends. In the next *k* lines print pairs of friends as "*A**i* *B**i*" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once.
[ "4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4\n", "1 1000\na b 0\n" ]
[ "1\npetya vasya\n", "0\n" ]
In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
[ { "input": "4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4", "output": "1\npetya vasya" }, { "input": "1 1000\na b 0", "output": "0" }, { "input": "2 1\na b 0\nb a 0", "output": "0" }, { "input": "3 1\na b 1\nb c 2\nc d 3", "output": "0" }, { "input":...
1,122
6,963,200
3
27,041
38
Smart Boy
[ "dp", "games", "strings" ]
F. Smart Boy
4
256
Once Petya and Vasya invented a new game and called it "Smart Boy". They located a certain set of words β€” the dictionary β€” for the game. It is admissible for the dictionary to contain similar words. The rules of the game are as follows: first the first player chooses any letter (a word as long as 1) from any word fro...
The first input line contains an integer *n* which is the number of words in the located dictionary (1<=≀<=*n*<=≀<=30). The *n* lines contain the words from the dictionary β€” one word is written on one line. Those lines are nonempty, consisting of Latin lower-case characters no longer than 30 characters. Equal words can...
On the first output line print a line "First" or "Second" which means who will win the game. On the second line output the number of points of the first player and the number of points of the second player after the game ends. Separate the numbers by a single space.
[ "2\naba\nabac\n", "3\nartem\nnik\nmax\n" ]
[ "Second\n29 35\n", "First\n2403 1882\n" ]
none
[ { "input": "2\naba\nabac", "output": "Second\n29 35" }, { "input": "3\nartem\nnik\nmax", "output": "First\n2403 1882" }, { "input": "1\njyi", "output": "First\n1727 876" }, { "input": "2\naz\nkagim", "output": "First\n1082 678" }, { "input": "3\nskz\nsauy\nrxu", ...
92
0
0
27,076
257
Greedy Elevator
[ "data structures", "implementation" ]
null
null
The *m*-floor (*m*<=&gt;<=1) office of international corporation CodeForces has the advanced elevator control system established. It works as follows. All office floors are sequentially numbered with integers from 1 to *m*. At time *t*<==<=0, the elevator is on the first floor, the elevator is empty and nobody is wait...
The first line contains two space-separated integers: *n*,<=*m* (1<=≀<=*n*<=≀<=105,<=2<=≀<=*m*<=≀<=105) β€” the number of people and floors in the building, correspondingly. Next *n* lines each contain three space-separated integers: *t**i*,<=*s**i*,<=*f**i* (1<=≀<=*t**i*<=≀<=109,<=1<=≀<=*s**i*,<=*f**i*<=≀<=*m*,<=*s**i*...
Print *n* lines. In the *i*-th line print a single number β€” the moment of time, when the *i*-th person gets to the floor he needs. The people are numbered in the order, in which they are given in the input. Please don't use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, co...
[ "3 10\n1 2 7\n3 6 5\n3 4 8\n", "2 10\n1 2 5\n7 4 5\n" ]
[ "7\n11\n8\n", "5\n9\n" ]
In the first sample the elevator worked as follows: - *t* = 1. The elevator is on the floor number 1. The elevator is empty. The floor number 2 has one person waiting. *p*<sub class="lower-index">*up*</sub> = 1 + 0 = 1, *p*<sub class="lower-index">*down*</sub> = 0 + 0 = 0, *p*<sub class="lower-index">*up*</sub> β‰₯ *p...
[]
60
0
0
27,136
76
Gift
[ "dsu", "graphs", "sortings", "trees" ]
A. Gift
2
256
The kingdom of Olympia consists of *N* cities and *M* bidirectional roads. Each road connects exactly two cities and two cities can be connected with more than one road. Also it possible that some roads connect city with itself making a loop. All roads are constantly plundered with bandits. After a while bandits becam...
The first line of the input contains two integers *N* and *M* (2<=≀<=*N*<=≀<=200, 1<=≀<=*M*<=≀<=50<=000) β€” the number of cities and the number of roads, respectively. The second line contains two integers *G* and *S* (1<=≀<=*G*,<=*S*<=≀<=109) β€” the prices of gold and silver coins in tugriks. The following *M* lines con...
The output should contain the minimal cost of the gift in Olympian tugriks. If there is no gift that satisfies the given requirements output .
[ "3 3\n2 1\n1 2 10 15\n1 2 4 20\n1 3 5 1\n" ]
[ "30\n" ]
none
[ { "input": "3 3\n2 1\n1 2 10 15\n1 2 4 20\n1 3 5 1", "output": "30" } ]
92
1,740,800
0
27,241