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
0
none
[ "none" ]
null
null
One must train much to do well on wizardry contests. So, there are numerous wizardry schools and magic fees. One of such magic schools consists of *n* tours. A winner of each tour gets a huge prize. The school is organised quite far away, so one will have to take all the prizes home in one go. And the bags that you've...
The first line contains three integers *n*, *l*, *k* (1<=≀<=*n*<=≀<=200,<=0<=≀<=*l*,<=*k*<=≀<=200) β€” the number of tours, the minimum number of tours to win, and the number of prizes that you can fit in the bags brought from home, correspondingly. The second line contains *n* space-separated integers, *p**i* (0<=≀<=*p...
Print a single real number β€” the answer to the problem. The answer will be accepted if the absolute or relative error does not exceed 10<=-<=6.
[ "3 1 0\n10 20 30\n-1 -1 2\n", "1 1 1\n100\n123\n" ]
[ "0.300000000000\n", "1.000000000000\n" ]
In the first sample we need either win no tour or win the third one. If we win nothing we wouldn't perform well. So, we must to win the third tour. Other conditions will be satisfied in this case. Probability of wining the third tour is 0.3. In the second sample we win the only tour with probability 1.0, and go back h...
[ { "input": "3 1 0\n10 20 30\n-1 -1 2", "output": "0.300000000000" }, { "input": "1 1 1\n100\n123", "output": "1.000000000000" }, { "input": "5 1 2\n36 44 13 83 63\n-1 2 -1 2 1", "output": "0.980387276800" }, { "input": "9 9 2\n91 96 99 60 42 67 46 39 62\n5 -1 2 -1 -1 -1 7 -1 ...
186
102,400
0
28,134
802
April Fools' Problem (hard)
[ "binary search", "data structures", "flows" ]
null
null
The plans for HC2 are rather far-fetched: we are just over 500 000 days away from HC2 3387, for example, and accordingly we are planning to have a couple hundred thousand problems in that edition (we hope that programming contests will become wildly more popular). The marmots need to get to work, and they could use a g...
Same as the medium version, but the limits have changed: 1<=≀<=*k*<=≀<=*n*<=≀<=500<=000.
Same as the medium version.
[ "8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 1\n" ]
[ "32" ]
none
[]
0
0
-1
28,223
113
Petr#
[ "brute force", "data structures", "hashing", "strings" ]
B. Petr#
2
256
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the *s**begin* and ending with the *s**end* (it is possible *s**begin*<==<=*s**end*), the given string...
The input file consists of three lines. The first line contains string *t*. The second and the third lines contain the *s**begin* and *s**end* identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output the only number β€” the amount of different substrings of *t* that start with *s**begin* and end with *s**end*.
[ "round\nro\nou\n", "codeforces\ncode\nforca\n", "abababab\na\nb\n", "aba\nab\nba\n" ]
[ "1\n", "0\n", "4\n", "1\n" ]
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab. In the fourth sample identificators intersect.
[ { "input": "round\nro\nou", "output": "1" }, { "input": "codeforces\ncode\nforca", "output": "0" }, { "input": "abababab\na\nb", "output": "4" }, { "input": "aba\nab\nba", "output": "1" }, { "input": "abcdefghijklmnopqrstuvwxyz\nabc\nxyz", "output": "1" }, ...
154
6,041,600
0
28,242
792
Colored Balls
[ "greedy", "math", "number theory" ]
null
null
There are *n* boxes with colored balls on the table. Colors are numbered from 1 to *n*. *i*-th box contains *a**i* balls, all of which have color *i*. You have to write a program that will divide all balls into sets such that: - each ball belongs to exactly one of the sets, - there are no empty sets, - there is no ...
The first line contains one integer number *n* (1<=≀<=*n*<=≀<=500). The second line contains *n* integer numbers *a*1,<=*a*2,<=... ,<=*a**n* (1<=≀<=*a**i*<=≀<=109).
Print one integer number β€” the minimum possible number of sets.
[ "3\n4 7 8\n", "2\n2 7\n" ]
[ "5\n", "4\n" ]
In the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color.
[ { "input": "3\n4 7 8", "output": "5" }, { "input": "2\n2 7", "output": "4" }, { "input": "1\n1", "output": "1" }, { "input": "1\n1000000000", "output": "1" }, { "input": "2\n1000000000 1", "output": "500000001" }, { "input": "2\n9 6", "output": "5"...
1,000
7,065,600
0
28,269
557
Vitaly and Cycle
[ "combinatorics", "dfs and similar", "graphs", "math" ]
null
null
After Vitaly was expelled from the university, he became interested in the graph theory. Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once. Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of *n* vertices and *m* edges, no...
The first line of the input contains two integers *n* and *m* (Β β€”Β the number of vertices in the graph and the number of edges in the graph. Next *m* lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers *a**i*, *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*)Β β€”Β the...
Print in the first line of the output two space-separated integers *t* and *w*Β β€”Β the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.
[ "4 4\n1 2\n1 3\n4 2\n4 3\n", "3 3\n1 2\n2 3\n3 1\n", "3 0\n" ]
[ "1 2\n", "0 1\n", "3 1\n" ]
The simple cycle is a cycle that doesn't contain any vertex twice.
[ { "input": "4 4\n1 2\n1 3\n4 2\n4 3", "output": "1 2" }, { "input": "3 3\n1 2\n2 3\n3 1", "output": "0 1" }, { "input": "3 0", "output": "3 1" }, { "input": "6 3\n1 2\n4 3\n6 5", "output": "2 12" }, { "input": "100000 0", "output": "3 166661666700000" }, {...
514
10,649,600
3
28,346
811
Vladik and Memorable Trip
[ "dp", "implementation" ]
null
null
Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips: Vladik is at initial train station, and now *n* people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code *a**i* i...
First line contains single integer *n* (1<=≀<=*n*<=≀<=5000)Β β€” number of people. Second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=5000), where *a**i* denotes code of the city to which *i*-th person is going.
The output should contain a single integerΒ β€” maximal possible total comfort.
[ "6\n4 4 2 5 2 3\n", "9\n5 1 3 1 5 2 4 2 5\n" ]
[ "14\n", "9\n" ]
In the first test case best partition into segments is: [4, 4] [2, 5, 2] [3], answer is calculated as follows: 4 + (2 *xor* 5) + 3 = 4 + 7 + 3 = 14 In the second test case best partition into segments is: 5 1 [3] 1 5 [2, 4, 2] 5, answer calculated as follows: 3 + (2 *xor* 4) = 3 + 6 = 9.
[ { "input": "6\n4 4 2 5 2 3", "output": "14" }, { "input": "9\n5 1 3 1 5 2 4 2 5", "output": "9" }, { "input": "5\n1558 4081 3591 1700 3232", "output": "14162" }, { "input": "10\n3838 1368 4825 2068 4755 2048 1342 4909 2837 4854", "output": "32844" }, { "input": "1...
93
0
0
28,409
620
New Year Tree
[ "bitmasks", "data structures", "trees" ]
null
null
The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited his best friends Kerim and Gural to help him to redecorate the New Year tree. The New Year tree is an undirected tree with *n* vertices and root in the vertex 1. You should process the queries of the two types: 1. Cha...
The first line contains two integers *n*,<=*m* (1<=≀<=*n*,<=*m*<=≀<=4Β·105) β€” the number of vertices in the tree and the number of the queries. The second line contains *n* integers *c**i* (1<=≀<=*c**i*<=≀<=60) β€” the colour of the *i*-th vertex. Each of the next *n*<=-<=1 lines contains two integers *x**j*,<=*y**j* (1...
For each query of the second type print the integer *a* β€” the number of different colours in the subtree of the vertex given in the query. Each of the numbers should be printed on a separate line in order of query appearing in the input.
[ "7 10\n1 1 1 1 1 1 1\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n1 3 2\n2 1\n1 4 3\n2 1\n1 2 5\n2 1\n1 6 4\n2 1\n2 2\n2 3\n", "23 30\n1 2 2 6 5 3 2 1 1 1 2 4 5 3 4 4 3 3 3 3 3 4 6\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n3 8\n4 9\n4 10\n4 11\n6 12\n6 13\n7 14\n7 15\n7 16\n8 17\n8 18\n10 19\n10 20\n10 21\n11 22\n11 23\n2 1\n2 5\n2 6\n2 ...
[ "2\n3\n4\n5\n1\n2\n", "6\n1\n3\n3\n2\n1\n2\n3\n5\n5\n1\n2\n2\n1\n1\n1\n2\n3\n" ]
none
[ { "input": "7 10\n1 1 1 1 1 1 1\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n1 3 2\n2 1\n1 4 3\n2 1\n1 2 5\n2 1\n1 6 4\n2 1\n2 2\n2 3", "output": "2\n3\n4\n5\n1\n2" }, { "input": "23 30\n1 2 2 6 5 3 2 1 1 1 2 4 5 3 4 4 3 3 3 3 3 4 6\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n3 8\n4 9\n4 10\n4 11\n6 12\n6 13\n7 14\n7 15\n7 16...
3,000
36,352,000
0
28,443
160
Edges in MST
[ "dfs and similar", "dsu", "graphs", "sortings" ]
null
null
You are given a connected weighted undirected graph without any loops and multiple edges. Let us remind you that a graph's spanning tree is defined as an acyclic connected subgraph of the given graph that includes all of the graph's vertexes. The weight of a tree is defined as the sum of weights of the edges that the...
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=105, ) β€” the number of the graph's vertexes and edges, correspondingly. Then follow *m* lines, each of them contains three integers β€” the description of the graph's edges as "*a**i* *b**i* *w**i*" (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*,<=1<=≀<=*w**i*<=≀<=106,<=*a*...
Print *m* lines β€” the answers for all edges. If the *i*-th edge is included in any MST, print "any"; if the *i*-th edge is included at least in one MST, print "at least one"; if the *i*-th edge isn't included in any MST, print "none". Print the answers for the edges in the order in which the edges are specified in the ...
[ "4 5\n1 2 101\n1 3 100\n2 3 2\n2 4 2\n3 4 1\n", "3 3\n1 2 1\n2 3 1\n1 3 2\n", "3 3\n1 2 1\n2 3 1\n1 3 1\n" ]
[ "none\nany\nat least one\nat least one\nany\n", "any\nany\nnone\n", "at least one\nat least one\nat least one\n" ]
In the second sample the MST is unique for the given graph: it contains two first edges. In the third sample any two edges form the MST for the given graph. That means that each edge is included at least in one MST.
[]
30
102,400
0
28,446
722
Research Rover
[ "combinatorics", "dp" ]
null
null
Unfortunately, the formal description of the task turned out to be too long, so here is the legend. Research rover finally reached the surface of Mars and is ready to complete its mission. Unfortunately, due to the mistake in the navigation system design, the rover is located in the wrong place. The rover will operat...
The first line of the input contains four integers *n*, *m*, *k* and *s* (1<=≀<=*n*,<=*m*<=≀<=100<=000, 0<=≀<=*k*<=≀<=2000, 1<=≀<=*s*<=≀<=1<=000<=000)Β β€” the number of rows and columns of the field, the number of cells with anomaly and the initial charge of the battery respectively. The follow *k* lines containing two ...
The answer can always be represented as an irreducible fraction . Print the only integer *P*Β·*Q*<=-<=1 modulo 109<=+<=7.
[ "3 3 2 11\n2 1\n2 3\n", "4 5 3 17\n1 2\n3 3\n4 1\n", "1 6 2 15\n1 1\n1 5\n" ]
[ "333333342\n", "514285727\n", "4\n" ]
In the first sample, the rover picks one of the following six routes: 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c9ec158c30775d6289140a3854e05168b09af399.png" style="max-width: 100.0%;max-height: 100.0%;"/>, after passing cell (2, 3) charge is equal to 6. 1. <img align="middle" c...
[]
30
0
0
28,467
558
A Simple Task
[ "data structures", "sortings", "strings" ]
null
null
This task is very simple. Given a string *S* of length *n* and *q* queries each query is on the format *i* *j* *k* which means sort the substring consisting of the characters from *i* to *j* in non-decreasing order if *k*<==<=1 or in non-increasing order if *k*<==<=0. Output the final string after applying the queries...
The first line will contain two integers *n*,<=*q* (1<=≀<=*n*<=≀<=105, 0<=≀<=*q*<=≀<=50<=000), the length of the string and the number of queries respectively. Next line contains a string *S* itself. It contains only lowercase English letters. Next *q* lines will contain three integers each *i*,<=*j*,<=*k* (1<=≀<=*i...
Output one line, the string *S* after applying the queries.
[ "10 5\nabacdabcda\n7 10 0\n5 8 1\n1 4 0\n3 6 0\n7 10 1\n", "10 1\nagjucbvdfk\n1 10 1\n" ]
[ "cbcaaaabdd", "abcdfgjkuv" ]
First sample test explanation: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3ac4e8cc7e335675a4a2b7b4758bfb3865377cea.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a90b5b03cf59288d8861f0142ecbdf6b12f69e5...
[ { "input": "10 5\nabacdabcda\n7 10 0\n5 8 1\n1 4 0\n3 6 0\n7 10 1", "output": "cbcaaaabdd" }, { "input": "10 1\nagjucbvdfk\n1 10 1", "output": "abcdfgjkuv" }, { "input": "10 6\nrmaahmdmuo\n1 3 1\n4 6 0\n5 6 1\n7 8 0\n8 10 0\n8 9 1", "output": "amrmahmoud" }, { "input": "10 5\...
5,000
8,499,200
0
28,473
508
Anya and Ghosts
[ "constructive algorithms", "greedy" ]
null
null
Anya loves to watch horror movies. In the best traditions of horror, she will be visited by *m* ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly *t* seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one...
The first line contains three integers *m*, *t*, *r* (1<=≀<=*m*,<=*t*,<=*r*<=≀<=300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit. The next line contains *m* space-separated numbers *w**i* (1<=≀<=*i*<=≀<=*m*, ...
If it is possible to make at least *r* candles burn during each visit, then print the minimum number of candles that Anya needs to light for that. If that is impossible, print <=-<=1.
[ "1 8 3\n10\n", "2 10 1\n5 8\n", "1 1 3\n10\n" ]
[ "3\n", "1\n", "-1\n" ]
Anya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit. It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from se...
[ { "input": "1 8 3\n10", "output": "3" }, { "input": "2 10 1\n5 8", "output": "1" }, { "input": "1 1 3\n10", "output": "-1" }, { "input": "21 79 1\n13 42 51 60 69 77 94 103 144 189 196 203 210 215 217 222 224 234 240 260 282", "output": "4" }, { "input": "125 92 2\...
30
0
-1
28,482
893
Counting Arrays
[ "combinatorics", "dp", "math", "number theory" ]
null
null
You are given two positive integer numbers *x* and *y*. An array *F* is called an *y*-factorization of *x* iff the following conditions are met: - There are *y* elements in *F*, and all of them are integer numbers; - . You have to count the number of pairwise distinct arrays that are *y*-factorizations of *x*. Two ...
The first line contains one integer *q* (1<=≀<=*q*<=≀<=105) β€” the number of testcases to solve. Then *q* lines follow, each containing two integers *x**i* and *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=106). Each of these lines represents a testcase.
Print *q* integers. *i*-th integer has to be equal to the number of *y**i*-factorizations of *x**i* modulo 109<=+<=7.
[ "2\n6 3\n4 2\n" ]
[ "36\n6\n" ]
In the second testcase of the example there are six *y*-factorizations: - { - 4,  - 1}; - { - 2,  - 2}; - { - 1,  - 4}; - {1, 4}; - {2, 2}; - {4, 1}.
[ { "input": "2\n6 3\n4 2", "output": "36\n6" }, { "input": "1\n524288 1000000", "output": "645043186" }, { "input": "1\n65536 1000000", "output": "928522471" }, { "input": "1\n5612 11399", "output": "215664246" } ]
1,497
181,555,200
3
28,483
653
Delivery Bears
[ "binary search", "flows", "graphs" ]
null
null
Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city. In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with *n* nodes and *m* edges. Each e...
The first line contains three integers *n*, *m* and *x* (2<=≀<=*n*<=≀<=50, 1<=≀<=*m*<=≀<=500, 1<=≀<=*x*<=≀<=100<=000)Β β€” the number of nodes, the number of directed edges and the number of bears, respectively. Each of the following *m* lines contains three integers *a**i*, *b**i* and *c**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*...
Print one real value on a single lineΒ β€” the maximum amount of weight Niwel can deliver if he uses exactly *x* bears. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program wi...
[ "4 4 3\n1 2 2\n2 4 1\n1 3 1\n3 4 2\n", "5 11 23\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 3 4\n2 4 5\n3 5 6\n1 4 2\n2 5 3\n1 5 2\n3 2 30\n" ]
[ "1.5000000000\n", "10.2222222222\n" ]
In the first sample, Niwel has three bears. Two bears can choose the path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7c0aa60a06309ef607b7159fd7f3687ea0d943ce.png" style="max-width: 100.0%;max-height: 100.0%;"/>, while one bear can choose the path <img align="middle" class="tex-formula"...
[ { "input": "4 4 3\n1 2 2\n2 4 1\n1 3 1\n3 4 2", "output": "1.5000000000" }, { "input": "5 11 23\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 3 4\n2 4 5\n3 5 6\n1 4 2\n2 5 3\n1 5 2\n3 2 30", "output": "10.2222222222" }, { "input": "10 16 63\n1 2 1\n2 10 1\n1 3 1\n3 10 1\n1 4 1\n4 10 1\n1 5 1\n5 10 1\n1...
108
307,200
0
28,535
260
Dividing Kingdom
[ "binary search", "brute force", "data structures" ]
null
null
A country called Flatland is an infinite two-dimensional plane. Flatland has *n* cities, each of them is a point on the plane. Flatland is ruled by king Circle IV. Circle IV has 9 sons. He wants to give each of his sons part of Flatland to rule. For that, he wants to draw four distinct straight lines, such that two of...
The first line contains integer *n* (9<=≀<=*n*<=≀<=105) β€” the number of cities in Flatland. Next *n* lines each contain two space-separated integers: *x**i*,<=*y**i* (<=-<=109<=≀<=*x**i*,<=*y**i*<=≀<=109) β€” the coordinates of the *i*-th city. No two cities are located at the same point. The last line contains nine spac...
If there is no solution, print a single integer -1. Otherwise, print in the first line two distinct real space-separated numbers: *x*1,<=*x*2 β€” the abscissas of the straight lines that are parallel to the *Oy* axis. And in the second line print two distinct real space-separated numbers: *y*1,<=*y*2 β€” the ordinates of ...
[ "9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3\n1 1 1 1 1 1 1 1 1\n", "15\n4 4\n-1 -3\n1 5\n3 -4\n-4 4\n-1 1\n3 -3\n-4 -5\n-3 3\n3 2\n4 1\n-4 2\n-2 -5\n-3 4\n-1 4\n2 1 2 1 2 1 3 2 1\n", "10\n-2 10\n6 0\n-16 -6\n-4 13\n-4 -2\n-17 -10\n9 15\n18 16\n-5 2\n10 -5\n2 1 1 1 1 1 1 1 1\n" ]
[ "1.5000000000 2.5000000000\n1.5000000000 2.5000000000\n", "-3.5000000000 2.0000000000\n3.5000000000 -1.0000000000\n", "-1\n" ]
The solution for the first sample test is shown below: The solution for the second sample test is shown below: There is no solution for the third sample test.
[]
124
20,070,400
0
28,608
175
Plane of Tanks: Pro
[ "implementation" ]
null
null
Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to divide the participants into several categories depending on their results. A player is given a non-negative integer number of points in each round of the Plane of Tanks. Vasya wrote results for each round of the last year. He ha...
The first line contains the only integer number *n* (1<=≀<=*n*<=≀<=1000) β€” a number of records with the players' results. Each of the next *n* lines contains a player's name and the amount of points, obtained by the player for the round, separated with a space. The name contains not less than 1 and no more than 10 cha...
Print on the first line the number *m* β€” the number of players, who participated in one round at least. Each one of the next *m* lines should contain a player name and a category he belongs to, separated with space. Category can be one of the following: "noob", "random", "average", "hardcore" or "pro" (without quotes)...
[ "5\nvasya 100\nvasya 200\nartem 100\nkolya 200\nigor 250\n", "3\nvasya 200\nkolya 1000\nvasya 1000\n" ]
[ "4\nartem noob\nigor pro\nkolya random\nvasya random\n", "2\nkolya pro\nvasya pro\n" ]
In the first example the best result, obtained by artem is not worse than the result that 25% of players have (his own result), so he belongs to category "noob". vasya and kolya have best results not worse than the results that 75% players have (both of them and artem), so they belong to category "random". igor has bes...
[ { "input": "5\nvasya 100\nvasya 200\nartem 100\nkolya 200\nigor 250", "output": "4\nartem noob\nigor pro\nkolya random\nvasya random" }, { "input": "3\nvasya 200\nkolya 1000\nvasya 1000", "output": "2\nkolya pro\nvasya pro" }, { "input": "1\nvasya 1000", "output": "1\nvasya pro" },...
434
204,800
3
28,663
856
To Play or not to Play
[ "greedy" ]
null
null
Vasya and Petya are playing an online game. As most online games, it has hero progress system that allows players to gain experience that make their heroes stronger. Of course, Vasya would like to get as many experience points as possible. After careful study of experience points allocation, he found out that if he pla...
The first line of input data contains integers *n*, *m* and *C* β€” the number of intervals when Vasya can play, the number of intervals when Petya can play, and the maximal difference in experience level when playing together still gives a progress boost (1<=≀<=*n*,<=*m*<=≀<=2Β·105, 0<=≀<=*C*<=≀<=1018). The following *...
Output one integerΒ β€” the maximal experience that Vasya can have in the end, if both players try to maximize this value.
[ "2 1 5\n1 7\n10 20\n10 20\n", "1 2 5\n0 100\n20 60\n85 90\n" ]
[ "25\n", "125\n" ]
none
[]
62
204,800
0
28,674
518
Pasha and Pipe
[ "binary search", "brute force", "combinatorics", "dp", "implementation" ]
null
null
On a certain meeting of a ruling party "A" minister Pavel suggested to improve the sewer system and to create a new pipe in the city. The city is an *n*<=Γ—<=*m* rectangular squared field. Each square of the field is either empty (then the pipe can go in it), or occupied (the pipe cannot go in such square). Empty squar...
The first line of the input contains two integers *n*,<=*m* (2<=≀<=*n*,<=*m*<=≀<=2000)Β β€”Β the height and width of Berland map. Each of the next *n* lines contains *m* characters β€” the map of the city. If the square of the map is marked by character '.', then the square is empty and the pipe can through it. If the s...
In the first line of the output print a single integer β€” the number of distinct ways to create a pipe.
[ "3 3\n...\n..#\n...\n", "4 2\n..\n..\n..\n..\n", "4 5\n#...#\n#...#\n###.#\n###.#\n" ]
[ "3", "2\n", "4" ]
In the first sample there are 3 ways to make a pipe (the squares of the pipe are marked by characters ' * '):
[]
46
0
0
28,767
873
Strange Game On Matrix
[ "greedy", "two pointers" ]
null
null
Ivan is playing a strange game. He has a matrix *a* with *n* rows and *m* columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: 1. Initially Ivan's...
The first line contains three integer numbers *n*, *m* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=100, 1<=≀<=*m*<=≀<=100). Then *n* lines follow, *i*-th of them contains *m* integer numbers β€” the elements of *i*-th row of matrix *a*. Each number is either 0 or 1.
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
[ "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1\n", "3 2 1\n1 0\n0 1\n0 0\n" ]
[ "4 1\n", "2 0\n" ]
In the first example Ivan will replace the element *a*<sub class="lower-index">1, 2</sub>.
[ { "input": "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1", "output": "4 1" }, { "input": "3 2 1\n1 0\n0 1\n0 0", "output": "2 0" }, { "input": "3 4 2\n0 1 1 1\n1 0 1 1\n1 0 0 1", "output": "7 0" }, { "input": "3 57 3\n1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1...
62
0
0
28,771
303
Random Ranking
[ "dp", "math", "probabilities" ]
null
null
Imagine a real contest or exam of *n* participants. Every participant will get a particular score. We can predict the standings board more or less, if we do some statistics on their previous performance. Let's say the score of the participants will be uniformly distributed in interval [*l**i*,<=*r**i*] (the score can ...
The first line contains integer *n* (1<=<=≀<=*n*<=<=≀<=80), showing how many participants we have. Each of the next *n* lines contains our predictions, the *i*-th line contains a pair of integers *l**i*,<=*r**i* (0<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=109) as the distributed interval for participant *i*. Consider the particip...
Output a distributed matrix *a* of order *n*. The element *a**ij* of the matrix is the probability that participant *i* has rank *j*. Your answer will considered correct if it has at most 10<=-<=6 absolute or relative error.
[ "2\n1 6\n4 9\n", "8\n0 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n" ]
[ "0.9200000000 0.080 \n0.080 0.9200000000 \n", "0.875 0.125 0 0 0 0 0 0 \n0.125 0.750 0.125 0 0 0 0 0 \n0 0.125 0.750 0.125 0 0 0 0 \n0 0 0.125 0.750 0.125 0 0 0 \n0 0 0 0.125 0.750 0.125 0 0 \n0 0 0 0 0.125 0.750 0.125 0 \n0 0 0 0 0 0.125 0.750 0.125 \n0 0 0 0 0 0 0.125 0.875 \n" ]
The score probability distribution is continuous, which means, there is no possibility for a draw.
[]
92
0
0
28,793
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 ...
248
0
0
28,876
28
DravDe saves the world
[ "geometry", "math" ]
E. DravDe saves the world
1
256
How horrible! The empire of galactic chickens tries to conquer a beautiful city "Z", they have built a huge incubator that produces millions of chicken soldiers a day, and fenced it around. The huge incubator looks like a polygon on the plane *Oxy* with *n* vertices. Naturally, DravDe can't keep still, he wants to dest...
The first line contains the number *n* (3<=≀<=*n*<=≀<=104) β€” the amount of vertices of the fence. Then there follow *n* lines containing the coordinates of these vertices (two integer numbers *x**i*,<=*y**i*) in clockwise or counter-clockwise order. It's guaranteed, that the fence does not contain self-intersections. ...
In the first line output two numbers *t*1,<=*t*2 such, that if DravDe air drops at time *t*1 (counting from the beginning of the flight), he lands on the incubator's territory (landing on the border is regarder as landing on the territory). If DravDe doesn't open his parachute, the second number should be equal to the ...
[ "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 0 1\n-1\n0 1 -1\n", "4\n0 0\n0 1\n1 1\n1 0\n0 -1\n-1 -1 1\n-1\n0 1 -1\n", "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 1 1\n-1\n1 1 -1\n" ]
[ "1.00000000 0.00000000\n", "-1.00000000 -1.00000000\n", "0.50000000 0.00000000\n" ]
none
[ { "input": "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 0 1\n-1\n0 1 -1", "output": "1.00000000 0.00000000" }, { "input": "4\n0 0\n0 1\n1 1\n1 0\n0 -1\n-1 -1 1\n-1\n0 1 -1", "output": "-1.00000000 -1.00000000" }, { "input": "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 1 1\n-1\n1 1 -1", "output": "0.50000000 0...
30
0
0
28,899
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", ...
248
8,806,400
0
28,901
498
Traffic Jams in the Land
[ "data structures", "dp", "number theory" ]
null
null
Some country consists of (*n*<=+<=1) cities, located along a straight highway. Let's number the cities with consecutive integers from 1 to *n*<=+<=1 in the order they occur along the highway. Thus, the cities are connected by *n* segments of the highway, the *i*-th segment connects cities number *i* and *i*<=+<=1. Ever...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of highway segments that connect the *n*<=+<=1 cities. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (2<=≀<=*a**i*<=≀<=6) β€” the periods of traffic jams appearance on segments of the highway. The next line contains a singl...
For each query of the first type output a single integer β€” the final value of time *t* after driving from city *x* to city *y*. Process the queries in the order in which they are given in the input.
[ "10\n2 5 3 2 3 5 3 4 2 4\n10\nC 10 6\nA 2 6\nA 1 3\nC 3 4\nA 3 11\nA 4 9\nA 5 6\nC 7 3\nA 8 10\nA 2 5\n" ]
[ "5\n3\n14\n6\n2\n4\n4\n" ]
none
[]
30
0
0
28,909
765
Math, math everywhere
[ "brute force", "dp", "math", "meet-in-the-middle", "number theory" ]
null
null
If you have gone that far, you'll probably skip unnecessary legends anyway... You are given a binary string and an integer . Find the number of integers *k*, 0<=≀<=*k*<=&lt;<=*N*, such that for all *i*<==<=0, 1, ..., *m*<=-<=1
In the first line of input there is a string *s* consisting of 0's and 1's (1<=≀<=|*s*|<=≀<=40). In the next line of input there is an integer *n* (1<=≀<=*n*<=≀<=5Β·105). Each of the next *n* lines contains two space-separated integers *p**i*, Ξ±*i* (1<=≀<=*p**i*,<=Ξ±*i*<=≀<=109, *p**i* is prime). All *p**i* are distinc...
A single integerΒ β€” the answer to the problem.
[ "1\n2\n2 1\n3 1\n", "01\n2\n3 2\n5 1\n", "1011\n1\n3 1000000000\n" ]
[ "2\n", "15\n", "411979884\n" ]
none
[]
124
0
0
28,984
571
Geometric Progressions
[ "math" ]
null
null
Geometric progression with the first element *a* and common ratio *b* is a sequence of numbers *a*,<=*ab*,<=*ab*2,<=*ab*3,<=.... You are given *n* integer geometric progressions. Your task is to find the smallest integer *x*, that is the element of all the given progressions, or else state that such integer does not e...
The first line contains integer (1<=≀<=*n*<=≀<=100) β€” the number of geometric progressions. Next *n* lines contain pairs of integers *a*,<=*b* (1<=≀<=*a*,<=*b*<=≀<=109), that are the first element and the common ratio of the corresponding geometric progression.
If the intersection of all progressions is empty, then print <=-<=1, otherwise print the remainder of the minimal positive integer number belonging to all progressions modulo 1000000007 (109<=+<=7).
[ "2\n2 2\n4 1\n", "2\n2 2\n3 3\n" ]
[ "4\n", "-1\n" ]
In the second sample test one of the progressions contains only powers of two, the other one contains only powers of three.
[ { "input": "2\n2 2\n4 1", "output": "4" }, { "input": "2\n2 2\n3 3", "output": "-1" }, { "input": "3\n1 4\n2 5\n4 2", "output": "-1" }, { "input": "2\n1 4\n2 6", "output": "-1" }, { "input": "2\n1 6\n2 6", "output": "-1" }, { "input": "2\n1 6\n1 12", ...
62
307,200
0
28,999
566
Clique in the Divisibility Graph
[ "dp", "math", "number theory" ]
null
null
As you must know, the maximum clique problem in an arbitrary graph is *NP*-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively. Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are conn...
The first line contains integer *n* (1<=≀<=*n*<=≀<=106), that sets the size of set *A*. The second line contains *n* distinct positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=106) β€” elements of subset *A*. The numbers in the line follow in the ascending order.
Print a single number β€” the maximum size of a clique in a divisibility graph for set *A*.
[ "8\n3 4 6 8 10 18 21 24\n" ]
[ "3\n" ]
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.
[ { "input": "8\n3 4 6 8 10 18 21 24", "output": "3" }, { "input": "5\n2 3 4 8 16", "output": "4" }, { "input": "2\n10 20", "output": "2" }, { "input": "2\n10 21", "output": "1" }, { "input": "5\n250000 333333 500000 666666 1000000", "output": "3" }, { "...
46
0
0
29,008
593
Beautiful Function
[ "constructive algorithms", "math" ]
null
null
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the i...
The first line of the input contains number *n* (1<=≀<=*n*<=≀<=50)Β β€” the number of circles Ruslan thinks of. Next follow *n* lines, each of them containing three integers *x**i*, *y**i* and *r**i* (0<=≀<=*x**i*,<=*y**i*<=≀<=50, 2<=≀<=*r**i*<=≀<=50)Β β€” the coordinates of the center and the raduis of the *i*-th circle.
In the first line print a correct function *f*(*t*). In the second line print a correct function *g*(*t*). The set of the points (*x**t*<==<=*f*(*t*),<=*y**t*<==<=*g*(*t*)) (0<=≀<=*t*<=≀<=50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at...
[ "3\n0 10 4\n10 0 4\n20 10 4\n" ]
[ "t \nabs((t-10))" ]
Correct functions: 1. 101. (1+2)1. ((*t*-3)+(*t**4))1. *abs*((*t*-10))1. (*abs*((((23-*t*)*(*t***t*))+((45+12)*(*t***t*))))*((5**t*)+((12**t*)-13)))1. *abs*((*t*-(*abs*((*t**31))+14)))) Incorrect functions: 1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7))) 1. *abs*(*t*-3) (not enough brackets,...
[ { "input": "3\n0 10 4\n10 0 4\n20 10 4", "output": "(((0*((1-abs((t-0)))+abs((abs((t-0))-1))))+(5*((1-abs((t-1)))+abs((abs((t-1))-1)))))+(10*((1-abs((t-2)))+abs((abs((t-2))-1)))))\n(((5*((1-abs((t-0)))+abs((abs((t-0))-1))))+(0*((1-abs((t-1)))+abs((abs((t-1))-1)))))+(5*((1-abs((t-2)))+abs((abs((t-2))-1)))))"...
156
20,172,800
0
29,025
398
Painting The Wall
[ "dp", "probabilities" ]
null
null
User ainta decided to paint a wall. The wall consists of *n*2 tiles, that are arranged in an *n*<=Γ—<=*n* table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below. 1. Firstly user ainta looks at the wall. If there is at least one painted cell on each ro...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=2Β·103; 0<=≀<=*m*<=≀<=*min*(*n*2,<=2Β·104)) β€” the size of the wall and the number of painted cells. Next *m* lines goes, each contains two integers *r**i* and *c**i* (1<=≀<=*r**i*,<=*c**i*<=≀<=*n*) β€” the position of the painted cell. It is guaranteed that t...
In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10<=-<=4 absolute or relative error.
[ "5 2\n2 3\n4 1\n", "2 2\n1 1\n1 2\n", "1 1\n1 1\n" ]
[ "11.7669491886\n", "2.0000000000\n", "0.0000000000\n" ]
none
[]
623
512,000
0
29,063
0
none
[ "none" ]
null
null
BigData Inc. is a corporation that has *n* data centers indexed from 1 to *n* that are located all over the world. These data centers provide storage for client data (you can figure out that client data is really big!). Main feature of services offered by BigData Inc. is the access availability guarantee even under th...
The first line of input contains three integers *n*, *m* and *h* (2<=≀<=*n*<=≀<=100<=000, 1<=≀<=*m*<=≀<=100<=000, 2<=≀<=*h*<=≀<=100<=000), the number of company data centers, number of clients and the day length of day measured in hours. The second line of input contains *n* integers *u*1,<=*u*2,<=...,<=*u**n* (0<=≀<...
In the first line print the minimum possible number of data centers *k* (1<=≀<=*k*<=≀<=*n*) that have to be included in an experiment in order to keep the data available for any client. In the second line print *k* distinct integers *x*1,<=*x*2,<=...,<=*x**k* (1<=≀<=*x**i*<=≀<=*n*), the indices of data centers whose m...
[ "3 3 5\n4 4 0\n1 3\n3 2\n3 1\n", "4 5 4\n2 1 0 3\n4 3\n3 2\n1 2\n1 4\n1 3\n" ]
[ "1\n3 ", "4\n1 2 3 4 " ]
Consider the first sample test. The given answer is the only way to conduct an experiment involving the only data center. In such a scenario the third data center has a maintenance during the hour 1, and no two data centers storing the information of the same client have maintenance at the same hour. On the other hand...
[ { "input": "3 3 5\n4 4 0\n1 3\n3 2\n3 1", "output": "1\n3 " }, { "input": "4 5 4\n2 1 0 3\n4 3\n3 2\n1 2\n1 4\n1 3", "output": "4\n1 2 3 4 " }, { "input": "5 5 4\n0 1 2 3 3\n1 2\n2 3\n3 4\n4 1\n3 5", "output": "1\n5 " }, { "input": "2 1 2\n1 0\n1 2", "output": "2\n1 2 " ...
155
512,000
0
29,083
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" ...
1,000
9,216,000
0
29,098
689
Mike and Geometry Problem
[ "combinatorics", "data structures", "dp", "geometry", "implementation" ]
null
null
Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define *f*([*l*,<=*r*])<==<=*r*<=-<=*l*<=+<=1 to be the number of integer points in the segment [*l*,<=*r*] with *l*<=≀<=*r* (say that ). You are given two integers *n* and *k* and *n* closed inter...
The first line contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=200<=000)Β β€” the number of segments and the number of segments in intersection groups respectively. Then *n* lines follow, the *i*-th line contains two integers *l**i*,<=*r**i* (<=-<=109<=≀<=*l**i*<=≀<=*r**i*<=≀<=109), describing *i*-th segment bou...
Print one integer numberΒ β€” the answer to Mike's problem modulo 1000000007 (109<=+<=7) in the only line.
[ "3 2\n1 2\n1 3\n2 3\n", "3 3\n1 3\n1 3\n1 3\n", "3 1\n1 2\n2 3\n3 4\n" ]
[ "5\n", "3\n", "6\n" ]
In the first example: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/099f833590960ffc5dafcbc207172a93605c44a8.png" style="max-width: 100.0%;max-height: 100.0%;"/>; <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6f56e6e99db45efff1c9be404aeb569c7e2bbb1d.png" ...
[ { "input": "3 2\n1 2\n1 3\n2 3", "output": "5" }, { "input": "3 3\n1 3\n1 3\n1 3", "output": "3" }, { "input": "3 1\n1 2\n2 3\n3 4", "output": "6" }, { "input": "1 1\n45 70", "output": "26" }, { "input": "1 1\n-35 -8", "output": "28" }, { "input": "1 1...
1,513
48,128,000
3
29,125
359
Permutation
[ "constructive algorithms", "dp", "math" ]
null
null
A permutation *p* is an ordered group of numbers *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*, consisting of *n* distinct positive integers, each is no more than *n*. We'll define number *n* as the length of permutation *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*. Simon has a positive integer *n* and a non-negative integer *k*, ...
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=50000, 0<=≀<=2*k*<=≀<=*n*).
Print 2*n* integers *a*1,<=*a*2,<=...,<=*a*2*n* β€” the required permutation *a*. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them.
[ "1 0\n", "2 1\n", "4 0\n" ]
[ "1 2", "3 2 1 4\n", "2 7 4 6 1 3 5 8\n" ]
Record |*x*| represents the absolute value of number *x*. In the first sample |1 - 2| - |1 - 2| = 0. In the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2. In the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0.
[ { "input": "1 0", "output": "1 2" }, { "input": "2 1", "output": "3 2 1 4" }, { "input": "4 0", "output": "2 7 4 6 1 3 5 8" }, { "input": "50000 0", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 4...
109
5,836,800
3
29,200
0
none
[ "none" ]
null
null
Given an *n*<=Γ—<=*n* table *T* consisting of lowercase English letters. We'll consider some string *s* good if the table contains a correct path corresponding to the given string. In other words, good strings are all strings we can obtain by moving from the left upper cell of the table only to the right and down. Here'...
The first line contains a single number *n* (1<=≀<=*n*<=≀<=20). Next *n* lines contain *n* lowercase English letters each β€” table *T*.
In a single line print string "FIRST", if the first player wins, "SECOND", if the second player wins and "DRAW", if the game ends with a draw.
[ "2\nab\ncd\n", "2\nxa\nay\n", "3\naab\nbcb\nbac\n" ]
[ "DRAW\n", "FIRST\n", "DRAW\n" ]
Consider the first sample: Good strings are strings: a, ab, ac, abd, acd. The first player moves first and adds letter a to the string, as there is only one good string of length 1. Then the second player can add b or c and the game will end with strings abd or acd, correspondingly. In the first case it will be a dra...
[]
46
307,200
0
29,306
715
Digit Tree
[ "dfs and similar", "divide and conquer", "dsu", "trees" ]
null
null
ZS the Coder has a large tree. It can be represented as an undirected connected graph of *n* vertices numbered from 0 to *n*<=-<=1 and *n*<=-<=1 edges between them. There is a single nonzero digit written on each edge. One day, ZS the Coder was bored and decided to investigate some properties of the tree. He chose a p...
The first line of the input contains two integers, *n* and *M* (2<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*M*<=≀<=109, )Β β€” the number of vertices and the number ZS has chosen respectively. The next *n*<=-<=1 lines contain three integers each. *i*-th of them contains *u**i*,<=*v**i* and *w**i*, denoting an edge between vertices *...
Print a single integerΒ β€” the number of interesting (by ZS the Coder's consideration) pairs.
[ "6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7\n", "5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3\n" ]
[ "7\n", "8\n" ]
In the first sample case, the interesting pairs are (0, 4), (1, 2), (1, 5), (3, 2), (2, 5), (5, 2), (3, 5). The numbers that are formed by these pairs are 14, 21, 217, 91, 7, 7, 917 respectively, which are all multiples of 7. Note that (2, 5) and (5, 2) are considered different. In the second sample case, the interes...
[ { "input": "6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7", "output": "7" }, { "input": "5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3", "output": "8" }, { "input": "4 3\n0 1 4\n1 2 4\n2 3 4", "output": "2" }, { "input": "2 7\n1 0 9", "output": "0" }, { "input": "2 7\n1 0 7", "ou...
46
0
0
29,434
12
Ball
[ "data structures", "sortings" ]
D. Ball
2
256
*N* ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, she can ...
The first line contains one integer *N* (1<=≀<=*N*<=≀<=500000). The second line contains *N* integer numbers *B**i*, separated by single spaces. The third and the fourth lines contain sequences *I**i* and *R**i* in the same format. It is guaranteed that 0<=≀<=*B**i*,<=*I**i*,<=*R**i*<=≀<=109.
Output the answer to the problem.
[ "3\n1 4 2\n4 3 2\n2 5 3\n" ]
[ "1\n" ]
none
[ { "input": "3\n1 4 2\n4 3 2\n2 5 3", "output": "1" }, { "input": "5\n2 8 10 0 7\n7 7 3 0 10\n2 8 3 2 2", "output": "1" }, { "input": "5\n3 0 0 2 0\n7 10 7 4 0\n9 1 6 1 9", "output": "1" }, { "input": "5\n5 4 0 2 5\n8 3 1 0 10\n4 5 0 0 5", "output": "2" }, { "input...
31
0
0
29,501
788
Weird journey
[ "combinatorics", "constructive algorithms", "dfs and similar", "dsu", "graphs" ]
null
null
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherlandΒ β€” Uzhlyandia. It is widely known that Uzhlyandia has *n* cities connected with *m* bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and en...
The first line contains two integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=106)Β β€” the number of cities and roads in Uzhlyandia, respectively. Each of the next *m* lines contains two integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*n*) that mean that there is road between cities *u* and *v*. It is guaranteed that no road will be gi...
Print out the only integerΒ β€” the number of good paths in Uzhlyandia.
[ "5 4\n1 2\n1 3\n1 4\n1 5\n", "5 3\n1 2\n2 3\n4 5\n", "2 2\n1 1\n1 2\n" ]
[ "6", "0", "1" ]
In first sample test case the good paths are: - 2 → 1 → 3 → 1 → 4 → 1 → 5, - 2 → 1 → 3 → 1 → 5 → 1 → 4, - 2 → 1 → 4 → 1 → 5 → 1 → 3, - 3 → 1 → 2 → 1 → 4 → 1 → 5, - 3 → 1 → 2 → 1 → 5 → 1 → 4, - 4 → 1 → 2 → 1 → 3 → 1 → 5. There are good paths that are same with displayed above, because the sets of roads they pas...
[ { "input": "5 4\n1 2\n1 3\n1 4\n1 5", "output": "6" }, { "input": "5 3\n1 2\n2 3\n4 5", "output": "0" }, { "input": "2 2\n1 1\n1 2", "output": "1" }, { "input": "4 5\n1 4\n3 4\n1 2\n2 3\n2 4", "output": "8" }, { "input": "7 13\n6 7\n2 7\n3 7\n4 3\n5 2\n1 4\n7 7\n3...
31
4,608,000
0
29,580
939
Love Rescue
[ "dfs and similar", "dsu", "graphs", "greedy", "strings" ]
null
null
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long. This story c...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the length of the letterings. The second line contains a string with length *n*, consisting of lowercase English lettersΒ β€” the lettering on Valya's pullover. The third line contains the lettering on Tolya's t-shirt in the same format.
In the first line output a single integerΒ β€” the minimum amount of mana *t* required for rescuing love of Valya and Tolya. In the next *t* lines output pairs of space-separated lowercase English lettersΒ β€” spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order. If there are...
[ "3\nabb\ndad\n", "8\ndrpepper\ncocacola\n" ]
[ "2\na d\nb a", "7\nl e\ne d\nd c\nc p\np o\no r\nr a\n" ]
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
[ { "input": "3\nabb\ndad", "output": "2\nb d\nd a" }, { "input": "8\ndrpepper\ncocacola", "output": "7\nl e\ne d\nd c\nc p\np o\no r\nr a" }, { "input": "1\nh\np", "output": "1\np h" }, { "input": "2\nxc\nda", "output": "2\nc a\nx d" }, { "input": "3\nbab\naab", ...
124
29,081,600
3
29,598
962
Byteland, Berland and Disputed Cities
[ "constructive algorithms", "greedy" ]
null
null
The cities of Byteland and Berland are located on the axis $Ox$. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line $Ox$ there are three types of cities: - the cities of Byteland, - the cities of Berland, - disputed cities. Recently, ...
The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^{5}$) β€” the number of cities. The following $n$ lines contains an integer $x_i$ and the letter $c_i$ ($-10^{9} \le x_i \le 10^{9}$) β€” the coordinate of the city and its type. If the city belongs to Byteland, $c_i$ equals to 'B'. If the city belongs ...
Print the minimal total length of such set of cables, that if we delete all Berland cities ($c_i$='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities ($c_i$='B'), it will be possible to find a way from any remain...
[ "4\n-5 R\n0 P\n3 P\n7 B\n", "5\n10 R\n14 B\n16 B\n21 R\n32 R\n" ]
[ "12\n", "24\n" ]
In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be $5 + 3 + 4 = 12$. In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neigh...
[ { "input": "4\n-5 R\n0 P\n3 P\n7 B", "output": "12" }, { "input": "5\n10 R\n14 B\n16 B\n21 R\n32 R", "output": "24" }, { "input": "10\n66 R\n67 R\n72 R\n73 R\n76 R\n78 B\n79 B\n83 B\n84 B\n85 P", "output": "26" }, { "input": "10\n61 R\n64 R\n68 R\n71 R\n72 R\n73 R\n74 P\n86 P...
108
0
0
29,669
114
PFAST Inc.
[ "bitmasks", "brute force", "graphs" ]
null
null
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members eff...
The first line contains two integer numbers *n* (1<=≀<=*n*<=≀<=16) β€” the number of volunteers, and *m* () β€” the number of pairs that do not get on. Next *n* lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next *m* lines contai...
The first output line should contain the single number *k* β€” the number of people in the sought team. Next *k* lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought t...
[ "3 1\nPetya\nVasya\nMasha\nPetya Vasya\n", "3 0\nPasha\nLesha\nVanya\n" ]
[ "2\nMasha\nPetya\n", "3\nLesha\nPasha\nVanya\n" ]
none
[ { "input": "3 1\nPetya\nVasya\nMasha\nPetya Vasya", "output": "2\nMasha\nPetya" }, { "input": "3 0\nPasha\nLesha\nVanya", "output": "3\nLesha\nPasha\nVanya" }, { "input": "7 12\nPasha\nLesha\nVanya\nTaras\nNikita\nSergey\nAndrey\nPasha Taras\nPasha Nikita\nPasha Andrey\nPasha Sergey\nLes...
92
4,812,800
0
29,691
0
none
[ "none" ]
null
null
You are given an integer *m*, and a list of *n* distinct integers between 0 and *m*<=-<=1. You would like to construct a sequence satisfying the properties: - Each element is an integer between 0 and *m*<=-<=1, inclusive. - All prefix products of the sequence modulo *m* are distinct. - No prefix product modulo *m*...
The first line of input contains two integers *n* and *m* (0<=≀<=*n*<=&lt;<=*m*<=≀<=200<=000)Β β€” the number of forbidden prefix products and the modulus. If *n* is non-zero, the next line of input contains *n* distinct integers between 0 and *m*<=-<=1, the forbidden prefix products. If *n* is zero, this line doesn't ex...
On the first line, print the number *k*, denoting the length of your sequence. On the second line, print *k* space separated integers, denoting your sequence.
[ "0 5\n", "3 10\n2 9 1\n" ]
[ "5\n1 2 4 3 0\n", "6\n3 9 2 9 8 0\n" ]
For the first case, the prefix products of this sequence modulo *m* are [1, 2, 3, 4, 0]. For the second case, the prefix products of this sequence modulo *m* are [3, 7, 4, 6, 8, 0].
[ { "input": "0 5", "output": "5\n1 2 4 3 0" }, { "input": "3 10\n2 9 1", "output": "6\n3 9 2 9 8 0" }, { "input": "0 1", "output": "1\n0" }, { "input": "0 720", "output": "397\n1 7 413 263 389 467 77 283 299 187 293 563 269 47 677 463 599 367 173 143 149 347 557 643 179 54...
31
0
0
29,709
44
Cola
[ "implementation" ]
B. Cola
2
256
To celebrate the opening of the Winter Computer School the organizers decided to buy in *n* liters of cola. However, an unexpected difficulty occurred in the shop: it turned out that cola is sold in bottles 0.5, 1 and 2 liters in volume. At that, there are exactly *a* bottles 0.5 in volume, *b* one-liter bottles and *c...
The first line contains four integers β€” *n*, *a*, *b*, *c* (1<=≀<=*n*<=≀<=10000, 0<=≀<=*a*,<=*b*,<=*c*<=≀<=5000).
Print the unique number β€” the solution to the problem. If it is impossible to buy exactly *n* liters of cola, print 0.
[ "10 5 5 5\n", "3 0 0 2\n" ]
[ "9\n", "0\n" ]
none
[ { "input": "10 5 5 5", "output": "9" }, { "input": "3 0 0 2", "output": "0" }, { "input": "1 0 0 0", "output": "0" }, { "input": "1 1 0 0", "output": "0" }, { "input": "1 2 0 0", "output": "1" }, { "input": "1 0 1 0", "output": "1" }, { "in...
2,000
22,118,400
0
29,724
0
none
[ "none" ]
null
null
Welcome to another task about breaking the code lock! Explorers Whitfield and Martin came across an unusual safe, inside of which, according to rumors, there are untold riches, among which one can find the solution of the problem of discrete logarithm! Of course, there is a code lock is installed on the safe. The lock...
The first line contains an integer *n*, the length of the strings *s* and *t* (1<=≀<=*n*<=≀<=2<=000). After that, there are two strings *s* and *t*, consisting of *n* lowercase Latin letters each.
If it is impossible to get string *t* from string *s* using no more than 6100 operations Β«shiftΒ», print a single number <=-<=1. Otherwise, in the first line output the number of operations *k* (0<=≀<=*k*<=≀<=6100). In the next line output *k* numbers *x**i* corresponding to the operations Β«shift *x**i*Β» (0<=≀<=*x**i*<...
[ "6\nabacbb\nbabcba\n", "3\naba\nbba\n" ]
[ "4\n6 3 2 3\n", "-1\n" ]
none
[ { "input": "6\nabacbb\nbabcba", "output": "13\n2 6 1 4 0 3 6 3 1 1 1 5 6 " }, { "input": "3\naba\nbba", "output": "-1" }, { "input": "1\nw\nw", "output": "2\n0 1 " }, { "input": "2\nvb\nvb", "output": "2\n1 1 " }, { "input": "7\nvhypflg\nvprhfly", "output": "-...
93
6,246,400
3
29,787
590
Birthday
[ "graph matchings", "strings" ]
null
null
Today is birthday of a Little DashaΒ β€” she is now 8 years old! On this occasion, each of her *n* friends and relatives gave her a ribbon with a greeting written on it, and, as it turned out, all the greetings are different. Dasha gathered all the ribbons and decided to throw away some of them in order to make the remain...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=750)Β β€” the number of Dasha's relatives and friends. Each of the next *n* lines contains exactly one greeting. Each greeting consists of characters 'a' and 'b' only. The total length of all greetings won't exceed 10<=000<=000 characters.
In the first line print the maximum size of the stylish set. In the second line print the numbers of ribbons involved in it, assuming that they are numbered from 1 to *n* in the order they appear in the input. If there are several stylish sets of the maximum size, print any of them.
[ "5\nabab\naba\naabab\nababb\nbab\n" ]
[ "2\n2 5\n" ]
In the sample, the answer that keeps ribbons 3 and 4 is also considered correct.
[]
46
0
0
29,788
39
Moon Craters
[ "dp", "sortings" ]
C. Moon Craters
1
256
There are lots of theories concerning the origin of moon craters. Most scientists stick to the meteorite theory, which says that the craters were formed as a result of celestial bodies colliding with the Moon. The other version is that the craters were parts of volcanoes. An extraterrestrial intelligence research spec...
The first line has an integer *n* (1<=≀<=*n*<=≀<=2000) β€” the number of discovered craters. The next *n* lines contain crater descriptions in the "*c**i* *r**i*" format, where *c**i* is the coordinate of the center of the crater on the moon robot’s path, *r**i* is the radius of the crater. All the numbers *c**i* and *r*...
In the first line output the number of craters in the required largest set. In the next line output space-separated numbers of craters that this set consists of. The craters are numbered from 1 to *n* in the order in which they were given in the input data. The numbers may be output in any order. If the result is not u...
[ "4\n1 1\n2 2\n4 1\n5 1\n" ]
[ "3\n1 2 4\n" ]
none
[]
62
0
0
29,801
279
Beautiful Decomposition
[ "dp", "games", "greedy", "number theory" ]
null
null
Valera considers a number beautiful, if it equals 2*k* or -2*k* for some integer *k* (*k*<=β‰₯<=0). Recently, the math teacher asked Valera to represent number *n* as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible. Help Valera and find,...
The first line contains string *s* (1<=≀<=|*s*|<=≀<=106), that is the binary representation of number *n* without leading zeroes (*n*<=&gt;<=0).
Print a single integer β€” the minimum amount of beautiful numbers that give a total of *n*.
[ "10\n", "111\n", "1101101\n" ]
[ "1\n", "2\n", "4\n" ]
In the first sample *n* = 2 is a beautiful number. In the second sample *n* = 7 and Valera can decompose it into sum 2<sup class="upper-index">3</sup> + ( - 2<sup class="upper-index">0</sup>). In the third sample *n* = 109 can be decomposed into the sum of four summands as follows: 2<sup class="upper-index">7</sup> +...
[]
30
0
0
29,840
496
Tennis Game
[ "binary search" ]
null
null
Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores *t* points, he wins the set; then the ...
The first line contains a single integer *n*Β β€” the length of the sequence of games (1<=≀<=*n*<=≀<=105). The second line contains *n* space-separated integers *a**i*. If *a**i*<==<=1, then the *i*-th serve was won by Petya, if *a**i*<==<=2, then the *i*-th serve was won by Gena. It is not guaranteed that at least one ...
In the first line print a single number *k*Β β€” the number of options for numbers *s* and *t*. In each of the following *k* lines print two integers *s**i* and *t**i*Β β€” the option for numbers *s* and *t*. Print the options in the order of increasing *s**i*, and for equal *s**i*Β β€” in the order of increasing *t**i*.
[ "5\n1 2 1 2 1\n", "4\n1 1 1 1\n", "4\n1 2 1 2\n", "8\n2 1 2 1 1 1 1 1\n" ]
[ "2\n1 3\n3 1\n", "3\n1 4\n2 2\n4 1\n", "0\n", "3\n1 6\n2 3\n6 1\n" ]
none
[ { "input": "5\n1 2 1 2 1", "output": "2\n1 3\n3 1" }, { "input": "4\n1 1 1 1", "output": "3\n1 4\n2 2\n4 1" }, { "input": "4\n1 2 1 2", "output": "0" }, { "input": "8\n2 1 2 1 1 1 1 1", "output": "3\n1 6\n2 3\n6 1" }, { "input": "14\n2 1 2 1 1 1 1 2 1 1 2 1 2 1", ...
77
6,963,200
0
29,893
571
Lengthening Sticks
[ "combinatorics", "implementation", "math" ]
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...
701
4,608,000
3
29,975
702
Road to Post Office
[ "math" ]
null
null
Vasiliy has a car and he wants to get from home to the post office. The distance which he needs to pass equals to *d* kilometers. Vasiliy's car is not new β€” it breaks after driven every *k* kilometers and Vasiliy needs *t* seconds to repair it. After repairing his car Vasiliy can drive again (but after *k* kilometers ...
The first line contains 5 positive integers *d*,<=*k*,<=*a*,<=*b*,<=*t* (1<=≀<=*d*<=≀<=1012; 1<=≀<=*k*,<=*a*,<=*b*,<=*t*<=≀<=106; *a*<=&lt;<=*b*), where: - *d* β€” the distance from home to the post office; - *k* β€” the distance, which car is able to drive before breaking; - *a* β€” the time, which Vasiliy spends to dri...
Print the minimal time after which Vasiliy will be able to reach the post office.
[ "5 2 1 4 10\n", "5 2 1 4 5\n" ]
[ "14\n", "13\n" ]
In the first example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds) and then to walk on foot 3 kilometers (in 12 seconds). So the answer equals to 14 seconds. In the second example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds), then repair his car (in 5 seconds) and d...
[ { "input": "5 2 1 4 10", "output": "14" }, { "input": "5 2 1 4 5", "output": "13" }, { "input": "1 1 1 2 1", "output": "1" }, { "input": "1000000000000 1000000 999999 1000000 1000000", "output": "999999999999000000" }, { "input": "997167959139 199252 232602 952690...
77
0
3
30,038
985
Pencils and Boxes
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
null
null
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence *a*1,<=*a*2,<=...,<=*a**n* of *n* integer numbers β€” saturation of the color of each pencil. Now Mis...
The first line contains three integer numbers *n*, *k* and *d* (1<=≀<=*k*<=≀<=*n*<=≀<=5Β·105, 0<=≀<=*d*<=≀<=109) β€” the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively. The second line contains *n* integer numbers *a*1,<=...
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
[ "6 3 10\n7 2 7 7 4 2\n", "6 2 3\n4 5 3 13 4 10\n", "3 2 5\n10 16 22\n" ]
[ "YES\n", "YES\n", "NO\n" ]
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10. In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and p...
[ { "input": "6 3 10\n7 2 7 7 4 2", "output": "YES" }, { "input": "6 2 3\n4 5 3 13 4 10", "output": "YES" }, { "input": "3 2 5\n10 16 22", "output": "NO" }, { "input": "8 7 13\n52 85 14 52 92 33 80 85", "output": "NO" }, { "input": "6 4 0\n1 3 2 4 2 1", "output"...
1,107
48,640,000
-1
30,042
585
Digits of Number Pi
[ "dp", "implementation", "strings" ]
null
null
Vasily has recently learned about the amazing properties of number Ο€. In one of the articles it has been hypothesized that, whatever the sequence of numbers we have, in some position, this sequence is found among the digits of number Ο€. Thus, if you take, for example, the epic novel "War and Peace" of famous Russian au...
The first line contains string *s* consisting of decimal digits (1<=≀<=|*s*|<=≀<=1000) that Vasily will use to search substrings in. According to hypothesis, this sequence of digis indeed occurs in the decimal representation of Ο€, although we can't guarantee that. The second and third lines contain two positive intege...
Print how many numbers in the segment from *x* to *y* that are half-occurrences in *s* modulo 109<=+<=7.
[ "02\n10\n19\n", "023456789\n10\n19\n", "31415926535\n10\n29\n" ]
[ "2\n", "9\n", "20\n" ]
none
[]
62
8,806,400
-1
30,131
727
T-shirts Distribution
[ "constructive algorithms", "flows", "greedy" ]
null
null
The organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXL you are given the number of t-shirts of this si...
The first line of the input contains six non-negative integersΒ β€” the number of t-shirts of each size. The numbers are given for the sizes S, M, L, XL, XXL, XXXL, respectively. The total number of t-shirts doesn't exceed 100<=000. The second line contains positive integer *n* (1<=≀<=*n*<=≀<=100<=000)Β β€” the number of pa...
If it is not possible to present a t-shirt to each participant, print Β«NOΒ» (without quotes). Otherwise, print *n*<=+<=1 lines. In the first line print Β«YESΒ» (without quotes). In the following *n* lines print the t-shirt sizes the orginizers should give to participants, one per line. The order of the participants shoul...
[ "0 1 0 1 1 0\n3\nXL\nS,M\nXL,XXL\n", "1 1 2 0 1 1\n5\nS\nM\nS,M\nXXL,XXXL\nXL,XXL\n" ]
[ "YES\nXL\nM\nXXL\n", "NO\n" ]
none
[ { "input": "0 1 0 1 1 0\n3\nXL\nS,M\nXL,XXL", "output": "YES\nXL\nM\nXXL" }, { "input": "1 1 2 0 1 1\n5\nS\nM\nS,M\nXXL,XXXL\nXL,XXL", "output": "NO" }, { "input": "1 2 4 4 1 1\n10\nXL\nXL\nS,M\nL\nM,L\nL\nS,M\nM\nXL,XXL\nXL", "output": "YES\nXL\nXL\nS\nL\nL\nL\nM\nM\nXL\nXL" }, ...
888
10,240,000
0
30,163
131
The World is a Theatre
[ "combinatorics", "math" ]
null
null
There are *n* boys and *m* girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly *t* actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of th...
The only line of the input data contains three integers *n*, *m*, *t* (4<=≀<=*n*<=≀<=30,<=1<=≀<=*m*<=≀<=30,<=5<=≀<=*t*<=≀<=*n*<=+<=*m*).
Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
[ "5 2 5\n", "4 3 5\n" ]
[ "10\n", "3\n" ]
none
[ { "input": "5 2 5", "output": "10" }, { "input": "4 3 5", "output": "3" }, { "input": "4 1 5", "output": "1" }, { "input": "7 3 6", "output": "168" }, { "input": "30 30 30", "output": "118264581548187697" }, { "input": "10 10 8", "output": "84990" ...
2,000
13,209,600
0
30,174
137
Last Chance
[ "data structures", "implementation", "strings" ]
null
null
Having read half of the book called "Storm and Calm" on the IT lesson, Innocentius was absolutely determined to finish the book on the maths lessons. All was fine until the math teacher Ms. Watkins saw Innocentius reading fiction books instead of solving equations of the fifth degree. As during the last maths class Inn...
The only input line contains a non-empty string *s* consisting of no more than 2Β·105 uppercase and lowercase Latin letters. We shall regard letters "a", "e", "i", "o", "u" and their uppercase variants as vowels.
Print on a single line two numbers without a space: the maximum length of a good substring and the number of good substrings with this length. If no good substring exists, print "No solution" without the quotes. Two substrings are considered different if their positions of occurrence are different. So if some string o...
[ "Abo\n", "OEIS\n", "auBAAbeelii\n", "AaaBRAaaCAaaDAaaBRAaa\n", "EA\n" ]
[ "3 1\n", "3 1\n", "9 3\n", "18 4\n", "No solution\n" ]
In the first sample there is only one longest good substring: "Abo" itself. The other good substrings are "b", "Ab", "bo", but these substrings have shorter length. In the second sample there is only one longest good substring: "EIS". The other good substrings are: "S", "IS".
[ { "input": "Abo", "output": "3 1" }, { "input": "OEIS", "output": "3 1" }, { "input": "auBAAbeelii", "output": "9 3" }, { "input": "AaaBRAaaCAaaDAaaBRAaa", "output": "18 4" }, { "input": "EA", "output": "No solution" }, { "input": "BBBAABAABAABBBB", ...
60
0
0
30,175
0
none
[ "none" ]
null
null
Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well. This time Igor K. got disappointed in ...
The only line contains four integers *n*, *m*, *a*, *b* (1<=≀<=*n*,<=*m*<=≀<=109, 1<=≀<=*a*<=≀<=*b*<=≀<=*n*). They are the number of folders in Igor K.'s computer, the width of a window and the numbers of the first and the last folders that need to be deleted.
Print a single number: the least possible number of times Igor K. will have to select the folders using frames to select only the folders with numbers from *a* to *b*.
[ "11 4 3 9\n", "20 5 2 20\n" ]
[ "3\n", "2\n" ]
The images below illustrate statement tests. The first test: <img class="tex-graphics" src="https://espresso.codeforces.com/a0e4ba690dd16e3c68210a28afd82020b23fb605.png" style="max-width: 100.0%;max-height: 100.0%;"/> In this test we can select folders 3 and 4 with out first selection, folders 5, 6, 7, 8 with our se...
[ { "input": "11 4 3 9", "output": "3" }, { "input": "20 5 2 20", "output": "2" }, { "input": "1 1 1 1", "output": "1" }, { "input": "26 5 2 18", "output": "3" }, { "input": "21 5 1 15", "output": "1" }, { "input": "21 5 1 21", "output": "1" }, {...
340
0
0
30,204
776
The Holmes Children
[ "math", "number theory" ]
null
null
The Holmes children are fighting over who amongst them is the cleverest. Mycroft asked Sherlock and Eurus to find value of *f*(*n*), where *f*(1)<==<=1 and for *n*<=β‰₯<=2, *f*(*n*) is the number of distinct ordered positive integer pairs (*x*,<=*y*) that satisfy *x*<=+<=*y*<==<=*n* and *gcd*(*x*,<=*y*)<==<=1. The integ...
A single line of input contains two space separated integers *n* (1<=≀<=*n*<=≀<=1012) and *k* (1<=≀<=*k*<=≀<=1012) indicating that Eurus asks Sherlock and Mycroft to find the value of *F**k*(*n*) modulo 1000000007.
Output a single integerΒ β€” the value of *F**k*(*n*) modulo 1000000007.
[ "7 1\n", "10 2\n" ]
[ "6", "4" ]
In the first case, there are 6 distinct ordered pairs (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1) satisfying *x* + *y* = 7 and *gcd*(*x*, *y*) = 1. Hence, *f*(7) = 6. So, *F*<sub class="lower-index">1</sub>(7) = *f*(*g*(7)) = *f*(*f*(7) + *f*(1)) = *f*(6 + 1) = *f*(7) = 6.
[ { "input": "7 1", "output": "6" }, { "input": "10 2", "output": "4" }, { "input": "640 15", "output": "2" }, { "input": "641 17", "output": "2" }, { "input": "641 2000", "output": "1" }, { "input": "961 2", "output": "930" }, { "input": "52...
2,000
307,200
0
30,219
906
Power Tower
[ "chinese remainder theorem", "math", "number theory" ]
null
null
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from *k*<=-<=1 rocks, possesses power *p* and we w...
First line of input contains two integers *n* (1<=≀<=*n*<=≀<=105) and *m* (1<=≀<=*m*<=≀<=109). Second line of input contains *n* integers *w**k* (1<=≀<=*w**k*<=≀<=109) which is the power of rocks that priests have. Third line of input contains single integer *q* (1<=≀<=*q*<=≀<=105) which is amount of queries from pri...
Output *q* integers. *k*-th of them must be the amount of cumulative power the tower will have if is built from rocks *l**k*,<=*l**k*<=+<=1,<=...,<=*r**k*.
[ "6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6\n" ]
[ "1\n1\n2\n4\n256\n3\n27\n597484987\n" ]
3<sup class="upper-index">27</sup> = 7625597484987
[ { "input": "6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6", "output": "1\n1\n2\n4\n256\n3\n27\n597484987" }, { "input": "10 20\n792708224 4633945 600798790 384332600 283309209 762285205 750900274 160512987 390669628 205259431\n10\n5 9\n10 10\n8 10\n7 10\n7 10\n10 10\n4 4\n10 1...
4,500
9,113,600
0
30,275
0
none
[ "none" ]
null
null
Treeland is a country in which there are *n* towns connected by *n*<=-<=1 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2*k* universities which are located in different towns. Recently, the president signed the decree to connect universities by high-speed network...
The first line of the input contains two integers *n* and *k* (2<=≀<=*n*<=≀<=200<=000, 1<=≀<=*k*<=≀<=*n*<=/<=2)Β β€” the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to *n*. The second line contains 2*k* distinct integers *u*1,<=*u*2,<=...,<=*u*2*k* (1<=≀<=*u**i...
Print the maximum possible sum of distances in the division of universities into *k* pairs.
[ "7 2\n1 5 6 2\n1 3\n3 2\n4 5\n3 7\n4 3\n4 6\n", "9 3\n3 2 1 6 5 9\n8 9\n3 2\n2 7\n3 4\n7 6\n4 5\n2 1\n2 8\n" ]
[ "6\n", "9\n" ]
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
[ { "input": "7 2\n1 5 6 2\n1 3\n3 2\n4 5\n3 7\n4 3\n4 6", "output": "6" }, { "input": "9 3\n3 2 1 6 5 9\n8 9\n3 2\n2 7\n3 4\n7 6\n4 5\n2 1\n2 8", "output": "9" }, { "input": "41 3\n11 10 15 35 34 6\n28 2\n25 3\n9 4\n11 5\n7 6\n24 7\n19 8\n1 9\n34 10\n23 11\n17 12\n32 13\n32 14\n32 15\n33 ...
77
0
0
30,278
925
Big Secret
[ "constructive algorithms", "math" ]
null
null
Vitya has learned that the answer for The Ultimate Question of Life, the Universe, and Everything is not the integer 54 42, but an increasing integer sequence $a_1, \ldots, a_n$. In order to not reveal the secret earlier than needed, Vitya encrypted the answer and obtained the sequence $b_1, \ldots, b_n$ using the foll...
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $b_1, \ldots, b_n$ ($1 \leq b_i &lt; 2^{60}$).
If there are no valid permutations, print a single line containing "No". Otherwise in the first line print the word "Yes", and in the second line print integers $b'_1, \ldots, b'_n$Β β€” a valid permutation of integers $b_i$. The unordered multisets $\{b_1, \ldots, b_n\}$ and $\{b'_1, \ldots, b'_n\}$ should be equal, i.Β ...
[ "3\n1 2 3\n", "6\n4 7 7 12 31 61\n" ]
[ "No\n", "Yes\n4 12 7 31 7 61 \n" ]
In the first example no permutation is valid. In the second example the given answer lead to the sequence $a_1 = 4$, $a_2 = 8$, $a_3 = 15$, $a_4 = 16$, $a_5 = 23$, $a_6 = 42$.
[ { "input": "3\n1 2 3", "output": "No" }, { "input": "6\n4 7 7 12 31 61", "output": "Yes\n4 12 7 31 7 61 " }, { "input": "1\n4", "output": "Yes\n4 " }, { "input": "2\n531 108", "output": "Yes\n108 531 " }, { "input": "5\n3 1 1 7 1", "output": "Yes\n1 3 1 7 1 " ...
61
0
0
30,281
171
Mysterious numbers - 2
[ "*special" ]
null
null
The only line of input contains three integers *a*1,<=*a*2,<=*a*3 (1<=≀<=*a*1,<=*a*2,<=*a*3<=≀<=20), separated by spaces. Output a single integer.
The only line of input contains three integers *a*1,<=*a*2,<=*a*3 (1<=≀<=*a*1,<=*a*2,<=*a*3<=≀<=20), separated by spaces.
Output a single integer.
[ "2 3 2\n", "13 14 1\n", "14 5 9\n", "17 18 3\n" ]
[ "5\n", "14\n", "464\n", "53\n" ]
none
[ { "input": "2 3 2", "output": "5" }, { "input": "13 14 1", "output": "14" }, { "input": "14 5 9", "output": "464" }, { "input": "17 18 3", "output": "53" }, { "input": "1 1 1", "output": "1" }, { "input": "4 6 7", "output": "110" }, { "inpu...
92
0
3
30,317
846
Math Show
[ "brute force", "greedy" ]
null
null
Polycarp takes part in a math show. He is given *n* tasks, each consists of *k* subtasks, numbered 1 through *k*. It takes him *t**j* minutes to solve the *j*-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order. ...
The first line contains three integer numbers *n*, *k* and *M* (1<=≀<=*n*<=≀<=45, 1<=≀<=*k*<=≀<=45, 0<=≀<=*M*<=≀<=2Β·109). The second line contains *k* integer numbers, values *t**j* (1<=≀<=*t**j*<=≀<=1000000), where *t**j* is the time in minutes required to solve *j*-th subtask of any task.
Print the maximum amount of points Polycarp can earn in *M* minutes.
[ "3 4 11\n1 2 3 4\n", "5 5 10\n1 2 4 8 16\n" ]
[ "6\n", "7\n" ]
In the first example Polycarp can complete the first task and spend 1 + 2 + 3 + 4 = 10 minutes. He also has the time to solve one subtask of the second task in one minute. In the second example Polycarp can solve the first subtask of all five tasks and spend 5Β·1 = 5 minutes. Also he can solve the second subtasks of tw...
[ { "input": "3 4 11\n1 2 3 4", "output": "6" }, { "input": "5 5 10\n1 2 4 8 16", "output": "7" }, { "input": "1 1 0\n2", "output": "0" }, { "input": "1 1 1\n1", "output": "2" }, { "input": "2 1 0\n2", "output": "0" }, { "input": "2 2 2\n2 3", "outpu...
62
0
0
30,324
590
Top Secret Task
[ "dp" ]
null
null
A top-secret military base under the command of Colonel Zuev is expecting an inspection from the Ministry of Defence. According to the charter, each top-secret military base must include a top-secret troop that should... well, we cannot tell you exactly what it should do, it is a top secret troop at the end. The proble...
The first line of the input contains three positive integers *n*, *k*, *s* (1<=≀<=*k*<=≀<=*n*<=≀<=150, 1<=≀<=*s*<=≀<=109)Β β€” the number of soldiers in the line, the size of the top-secret troop to be formed and the maximum possible number of swap operations of the consecutive pair of soldiers, respectively. The second ...
Print a single integer β€” the minimum possible total loquacity of the top-secret troop.
[ "3 2 2\n2 4 1\n", "5 4 2\n10 1 6 2 5\n", "5 2 3\n3 1 4 2 5\n" ]
[ "3\n", "18\n", "3\n" ]
In the first sample Colonel has to swap second and third soldiers, he doesn't really need the remaining swap. The resulting soldiers order is: (2, 1, 4). Minimum possible summary loquacity of the secret troop is 3. In the second sample Colonel will perform swaps in the following order: 1. (10, 1, 6 β€” 2, 5) 1. (10, 1...
[]
30
0
0
30,359
0
none
[ "none" ]
null
null
Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out *n* numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, n...
The first line contains two integers, *n* and *k* (0<=≀<=*k*<=&lt;<=*n*<=≀<=105). The second line contains a string consisting of *n* digits.
Print the answer to the problem modulo 109<=+<=7.
[ "3 1\n108\n", "3 2\n108\n" ]
[ "27", "9" ]
In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9.
[ { "input": "3 1\n108", "output": "27" }, { "input": "3 2\n108", "output": "9" }, { "input": "1 0\n5", "output": "5" }, { "input": "5 2\n39923", "output": "2667" }, { "input": "6 3\n967181", "output": "3506" }, { "input": "7 1\n2178766", "output": "...
1,684
7,065,600
3
30,367
0
none
[ "none" ]
null
null
After the Search Ultimate program that searched for strings in a text failed, Igor K. got to think: "Why on Earth does my program work so slowly?" As he double-checked his code, he said: "My code contains no errors, yet I know how we will improve Search Ultimate!" and took a large book from the shelves. The book read "...
The input data contain the only integer *n* (1<=≀<=*n*<=≀<=255), which Igor K. is about to multiply.
On the first line print number *p*, which represents the minimum number of lea operations, needed to do that. Then print the program consisting of *p* commands, performing the operations. It is guaranteed that such program exists for any *n* from 1 to 255. Use precisely the following format of commands (here *k* is eq...
[ "41\n", "2\n", "4\n" ]
[ "2\nlea ebx, [eax + 4*eax]\nlea ecx, [eax + 8*ebx]\n", "1\nlea ebx, [eax + eax]\n", "1\nlea ebx, [4*eax]\n" ]
none
[]
280
1,228,800
0
30,493
678
Another Sith Tournament
[ "bitmasks", "dp", "math", "probabilities" ]
null
null
The rules of Sith Tournament are well known to everyone. *n* Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=18)Β β€” the number of participants of the Sith Tournament. Each of the next *n* lines contains *n* real numbers, which form a matrix *p**ij* (0<=≀<=*p**ij*<=≀<=1). Each its element *p**ij* is the probability that the *i*-th participant defeats the *j*-th in a d...
Output a real numberΒ β€” the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10<=-<=6.
[ "3\n0.0 0.5 0.8\n0.5 0.0 0.4\n0.2 0.6 0.0\n" ]
[ "0.680000000000000\n" ]
none
[ { "input": "3\n0.0 0.5 0.8\n0.5 0.0 0.4\n0.2 0.6 0.0", "output": "0.680000000000000" }, { "input": "1\n0.0", "output": "1.000000000000000" }, { "input": "2\n0.00 0.75\n0.25 0.00", "output": "0.750000000000000" }, { "input": "4\n0.0 0.6 0.5 0.4\n0.4 0.0 0.3 0.8\n0.5 0.7 0.0 0....
2,136
98,406,400
0
30,594
95
Horse Races
[ "dp", "math" ]
D. Horse Races
2
256
Petya likes horse racing very much. Horses numbered from *l* to *r* take part in the races. Petya wants to evaluate the probability of victory; for some reason, to do that he needs to know the amount of nearly lucky horses' numbers. A nearly lucky number is an integer number that has at least two lucky digits the dista...
The first line contains two integers *t* and *k* (1<=≀<=*t*,<=*k*<=≀<=1000) β€” the number of segments and the distance between the numbers correspondingly. Next *t* lines contain pairs of integers *l**i* and *r**i* (1<=≀<=*l*<=≀<=*r*<=≀<=101000). All numbers are given without the leading zeroes. Numbers in each line are...
Output *t* lines. In each line print one integer β€” the answer for the corresponding segment modulo 1000000007 (109<=+<=7).
[ "1 2\n1 100\n", "1 2\n70 77\n", "2 1\n1 20\n80 100\n" ]
[ "4\n", "2\n", "0\n0\n" ]
In the first sample, the four nearly lucky numbers are 44, 47, 74, 77. In the second sample, only 74 and 77 are in the given segment.
[]
92
0
0
30,595
708
Student's Camp
[ "dp", "math" ]
null
null
Alex studied well and won the trip to student camp Alushta, located on the seashore. Unfortunately, it's the period of the strong winds now and there is a chance the camp will be destroyed! Camp building can be represented as the rectangle of *n*<=+<=2 concrete blocks height and *m* blocks width. Every day there is ...
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=1500) that define the size of the destructible part of building. The second line of the input contains two integers *a* and *b* (1<=≀<=*a*<=≀<=*b*<=≀<=109) that define the probability *p*. It's guaranteed that integers *a* and *b* are c...
Consider the answer as an irreducible fraction is equal to . Print one integer equal to . It's guaranteed that within the given constraints .
[ "2 2\n1 2\n1\n", "5 1\n3 10\n1\n", "3 3\n1 10\n5\n" ]
[ "937500007\n", "95964640\n", "927188454\n" ]
In the first sample, each of the four blocks is destroyed with the probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/eb946338365d9781f7d2e9ec692c26702d0ae3a7.png" style="max-width: 100.0%;max-height: 100.0%;"/>. There are 7 scenarios that result in building not collapsing, and the...
[]
46
0
0
30,662
329
Graph Reconstruction
[ "constructive algorithms" ]
null
null
I have an undirected graph consisting of *n* nodes, numbered 1 through *n*. Each node has at most two incident edges. For each pair of nodes, there is at most an edge connecting them. No edge connects a node to itself. I would like to create a new graph in such a way that: - The new graph consists of the same numbe...
The first line consists of two space-separated integers: *n* and *m* (1<=≀<=*m*<=≀<=*n*<=≀<=105), denoting the number of nodes and edges, respectively. Then *m* lines follow. Each of the *m* lines consists of two space-separated integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*n*;Β *u*<=β‰ <=*v*), denoting an edge between nodes...
If it is not possible to construct a new graph with the mentioned properties, output a single line consisting of -1. Otherwise, output exactly *m* lines. Each line should contain a description of edge in the same way as used in the input format.
[ "8 7\n1 2\n2 3\n4 5\n5 6\n6 8\n8 7\n7 4\n", "3 2\n1 2\n2 3\n", "5 4\n1 2\n2 3\n3 4\n4 1\n" ]
[ "1 4\n4 6\n1 6\n2 7\n7 5\n8 5\n2 8\n", "-1\n", "1 3\n3 5\n5 2\n2 4\n" ]
The old graph of the first example: <img class="tex-graphics" src="https://espresso.codeforces.com/1a5d4ab85ef86541ea9bea88ee537f6852ca2194.png" style="max-width: 100.0%;max-height: 100.0%;"/> A possible new graph for the first example: <img class="tex-graphics" src="https://espresso.codeforces.com/8a2d63a60d5196790...
[]
3,000
12,902,400
0
30,749
29
Quarrel
[ "graphs", "shortest paths" ]
E. Quarrel
1
256
Friends Alex and Bob live in Bertown. In this town there are *n* crossroads, some of them are connected by bidirectional roads of equal length. Bob lives in a house at the crossroads number 1, Alex β€” in a house at the crossroads number *n*. One day Alex and Bob had a big quarrel, and they refused to see each other. It...
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=500,<=1<=≀<=*m*<=≀<=10000) β€” the amount of crossroads and the amount of roads. Each of the following *m* lines contains two integers β€” the numbers of crossroads connected by the road. It is guaranteed that no road connects a crossroads with itself and no t...
If the required routes don't exist, output -1. Otherwise, the first line should contain integer *k* β€” the length of shortest routes (the length of the route is the amount of roads in it). The next line should contain *k*<=+<=1 integers β€” Bob's route, i.e. the numbers of *k*<=+<=1 crossroads passed by Bob. The last line...
[ "2 1\n1 2\n", "7 5\n1 2\n2 7\n7 6\n2 3\n3 4\n", "7 6\n1 2\n2 7\n7 6\n2 3\n3 4\n1 5\n" ]
[ "1\n1 2 \n2 1 \n", "-1\n", "6\n1 2 3 4 3 2 7 \n7 6 7 2 1 5 1 \n" ]
none
[ { "input": "2 1\n1 2", "output": "1\n1 2 \n2 1 " }, { "input": "7 5\n1 2\n2 7\n7 6\n2 3\n3 4", "output": "-1" }, { "input": "7 6\n1 2\n2 7\n7 6\n2 3\n3 4\n1 5", "output": "6\n1 2 3 4 3 2 7 \n7 6 7 2 1 5 1 " }, { "input": "6 10\n3 6\n3 5\n1 3\n2 6\n5 4\n6 4\n6 5\n5 1\n2 3\n1 2...
92
0
0
30,802
8
Looking for Order
[ "bitmasks", "dp" ]
C. Looking for Order
4
512
Girl Lena likes it when everything is in order, and looks for order everywhere. Once she was getting ready for the University and noticed that the room was in a mess β€” all the objects from her handbag were thrown about the room. Of course, she wanted to put them back into her handbag. The problem is that the girl canno...
The first line of the input file contains the handbag's coordinates *x**s*,<=*y**s*. The second line contains number *n* (1<=≀<=*n*<=≀<=24) β€” the amount of objects the girl has. The following *n* lines contain the objects' coordinates. All the coordinates do not exceed 100 in absolute value. All the given positions are...
In the first line output the only number β€” the minimum time the girl needs to put the objects into her handbag. In the second line output the possible optimum way for Lena. Each object in the input is described by its index number (from 1 to *n*), the handbag's point is described by number 0. The path should start an...
[ "0 0\n2\n1 1\n-1 1\n", "1 1\n3\n4 3\n3 4\n0 0\n" ]
[ "8\n0 1 2 0 \n", "32\n0 1 2 0 3 0 \n" ]
none
[ { "input": "0 0\n2\n1 1\n-1 1", "output": "8\n0 1 2 0 " }, { "input": "1 1\n3\n4 3\n3 4\n0 0", "output": "32\n0 1 2 0 3 0 " }, { "input": "-3 4\n1\n2 2", "output": "58\n0 1 0 " }, { "input": "7 -7\n2\n3 1\n-3 8", "output": "490\n0 1 2 0 " }, { "input": "3 -9\n3\n0...
61
0
0
30,803
353
Find Maximum
[ "implementation", "math", "number theory" ]
null
null
Valera has array *a*, consisting of *n* integers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1, and function *f*(*x*), taking an integer from 0 to 2*n*<=-<=1 as its single argument. Value *f*(*x*) is calculated by formula , where value *bit*(*i*) equals one if the binary representation of number *x* contains a 1 on the *i*-th posit...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of array elements. The next line contains *n* space-separated integers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (0<=≀<=*a**i*<=≀<=104) β€” elements of array *a*. The third line contains a sequence of digits zero and one without spaces *s*0*s*1... *s**n*<=-<=1 ...
Print a single integer β€” the maximum value of function *f*(*x*) for all .
[ "2\n3 8\n10\n", "5\n17 0 10 2 1\n11010\n" ]
[ "3\n", "27\n" ]
In the first test case *m* = 2<sup class="upper-index">0</sup> = 1, *f*(0) = 0, *f*(1) = *a*<sub class="lower-index">0</sub> = 3. In the second sample *m* = 2<sup class="upper-index">0</sup> + 2<sup class="upper-index">1</sup> + 2<sup class="upper-index">3</sup> = 11, the maximum value of function equals *f*(5) = *a*<...
[ { "input": "2\n3 8\n10", "output": "3" }, { "input": "5\n17 0 10 2 1\n11010", "output": "27" }, { "input": "18\n4382 3975 9055 7554 8395 204 5313 5739 1555 2306 5423 828 8108 9736 2683 7940 1249 5495\n110001100101110111", "output": "88691" }, { "input": "43\n475 2165 8771 714...
434
32,768,000
-1
30,840
883
Palindromic Cut
[ "brute force", "implementation", "strings" ]
null
null
Kolya has a string *s* of length *n* consisting of lowercase and uppercase Latin letters and digits. He wants to rearrange the symbols in *s* and cut it into the minimum number of parts so that each part is a palindrome and all parts have the same lengths. A palindrome is a string which reads the same backward as forw...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=4Β·105) β€” the length of string *s*. The second line contains a string *s* of length *n* consisting of lowercase and uppercase Latin letters and digits.
Print to the first line an integer *k* β€” minimum number of palindromes into which you can cut a given string. Print to the second line *k* strings β€” the palindromes themselves. Separate them by a space. You are allowed to print palindromes in arbitrary order. All of them should have the same length.
[ "6\naabaac\n", "8\n0rTrT022\n", "2\naA\n" ]
[ "2\naba aca ", "1\n02TrrT20 ", "2\na A \n" ]
none
[ { "input": "6\naabaac", "output": "2\naba aca " }, { "input": "8\n0rTrT022", "output": "1\n02TrrT20 " }, { "input": "2\naA", "output": "2\na A " }, { "input": "1\ns", "output": "1\ns " }, { "input": "10\n6IIC6CCIIC", "output": "1\n6CCIIIICC6 " }, { "in...
61
5,529,600
0
30,899
372
Watching Fireworks is Fun
[ "data structures", "dp", "math" ]
null
null
A festival will be held in a town's main street. There are *n* sections in the main street. The sections are numbered 1 through *n* from left to right. The distance between each adjacent sections is 1. In the festival *m* fireworks will be launched. The *i*-th (1<=≀<=*i*<=≀<=*m*) launching is on time *t**i* at section...
The first line contains three integers *n*, *m*, *d* (1<=≀<=*n*<=≀<=150000;Β 1<=≀<=*m*<=≀<=300;Β 1<=≀<=*d*<=≀<=*n*). Each of the next *m* lines contains integers *a**i*, *b**i*, *t**i* (1<=≀<=*a**i*<=≀<=*n*;Β 1<=≀<=*b**i*<=≀<=109;Β 1<=≀<=*t**i*<=≀<=109). The *i*-th line contains description of the *i*-th launching. It is...
Print a single integer β€” the maximum sum of happiness that you can gain from watching all the fireworks. Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "50 3 1\n49 1 1\n26 1 4\n6 1 10\n", "10 2 1\n1 1000 4\n9 1000 4\n" ]
[ "-31\n", "1992\n" ]
none
[ { "input": "50 3 1\n49 1 1\n26 1 4\n6 1 10", "output": "-31" }, { "input": "10 2 1\n1 1000 4\n9 1000 4", "output": "1992" }, { "input": "30 8 2\n15 97 3\n18 64 10\n20 14 20\n16 18 36\n10 23 45\n12 60 53\n17 93 71\n11 49 85", "output": "418" }, { "input": "100 20 5\n47 93 3\n6...
109
9,420,800
-1
30,975
306
Polygon
[ "constructive algorithms", "geometry" ]
null
null
Polycarpus loves convex polygons, especially if all their angles are the same and all their sides are different. Draw for him any such polygon with the given number of vertexes.
The input contains a single integer *n* (3<=≀<=*n*<=≀<=100) β€” the number of the polygon vertexes.
Print *n* lines, containing the coordinates of the vertexes of the *n*-gon "*x**i* *y**i*" in the counter clockwise order. The coordinates of the vertexes shouldn't exceed 106 in their absolute value. The side lengths should fit within limits [1,<=1000] (not necessarily integer). Mutual comparing sides and angles of yo...
[ "8\n" ]
[ "1.000 0.000\n7.000 0.000\n9.000 2.000\n9.000 3.000\n5.000 7.000\n3.000 7.000\n0.000 4.000\n0.000 1.000\n" ]
none
[]
92
0
0
31,055
164
Minimum Diameter
[ "binary search", "brute force" ]
null
null
You are given *n* points on the plane. You need to delete exactly *k* of them (*k*<=&lt;<=*n*) so that the diameter of the set of the remaining *n*<=-<=*k* points were as small as possible. The diameter of a set of points is the maximum pairwise distance between the points of the set. The diameter of a one point set eq...
The first input line contains a pair of integers *n*,<=*k* (2<=≀<=*n*<=≀<=1000, 1<=≀<=*k*<=≀<=30, *k*<=&lt;<=*n*) β€” the numbers of points on the plane and the number of points to delete, correspondingly. Next *n* lines describe the points, one per line. Each description consists of a pair of integers *x**i*,<=*y**i* (...
Print *k* different space-separated integers from 1 to *n* β€” the numbers of points to delete. The points are numbered in the order, in which they are given in the input from 1 to *n*. You can print the numbers in any order. If there are multiple solutions, print any of them.
[ "5 2\n1 2\n0 0\n2 2\n1 1\n3 3\n", "4 1\n0 0\n0 0\n1 1\n1 1\n" ]
[ "5 2", "3" ]
none
[]
30
0
0
31,076
471
MUH and House of Cards
[ "binary search", "brute force", "greedy", "math" ]
null
null
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that they've already found a hefty deck of *n* playing cards. Let's describe the house they want to make: 1. The house consists of some non-zero number of floors. 1. Each f...
The single line contains integer *n* (1<=≀<=*n*<=≀<=1012) β€” the number of cards.
Print the number of distinct heights that the houses made of exactly *n* cards can have.
[ "13\n", "6\n" ]
[ "1", "0" ]
In the first sample you can build only these two houses (remember, you must use all the cards): Thus, 13 cards are enough only for two floor houses, so the answer is 1. The six cards in the second sample are not enough to build any house.
[ { "input": "13", "output": "1" }, { "input": "6", "output": "0" }, { "input": "26", "output": "2" }, { "input": "1000000000000", "output": "272165" }, { "input": "571684826707", "output": "205784" }, { "input": "178573947413", "output": "115012" ...
935
4,608,000
3
31,100
908
New Year and Rainbow Roads
[ "graphs", "greedy", "implementation" ]
null
null
Roy and Biv have a set of *n* points on the infinite number line. Each point has one of 3 colors: red, green, or blue. Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it...
The first line will contain an integer *n* (1<=≀<=*n*<=≀<=300<=000), the number of points. The next *n* lines will contain two tokens *p**i* and *c**i* (*p**i* is an integer, 1<=≀<=*p**i*<=≀<=109, *c**i* is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the *i*-th point and the color of the *i*-...
Print a single integer, the minimum cost way to solve the problem.
[ "4\n1 G\n5 R\n10 B\n15 G\n", "4\n1 G\n2 R\n3 B\n10 G\n" ]
[ "23\n", "12\n" ]
In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively.
[ { "input": "4\n1 G\n5 R\n10 B\n15 G", "output": "23" }, { "input": "4\n1 G\n2 R\n3 B\n10 G", "output": "12" }, { "input": "4\n1 G\n123123 R\n987987987 B\n1000000000 G", "output": "1012135134" }, { "input": "1\n3 R", "output": "0" } ]
46
5,529,600
0
31,101
853
Michael and Charging Stations
[ "binary search", "dp", "greedy" ]
null
null
Michael has just bought a new electric car for moving across city. Michael does not like to overwork, so each day he drives to only one of two his jobs. Michael's day starts from charging his electric car for getting to the work and back. He spends 1000 burles on charge if he goes to the first job, and 2000 burles if ...
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=300<=000), the number of days Michael has planned. Next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (*a**i*<==<=1000 or *a**i*<==<=2000) with *a**i* denoting the charging cost at the day *i*.
Output the minimum amount of burles Michael has to spend.
[ "3\n1000 2000 1000\n", "6\n2000 2000 2000 2000 2000 1000\n" ]
[ "3700\n", "10000\n" ]
In the first sample case the most optimal way for Michael is to pay for the first two days spending 3000 burles and get 300 bonus burles as return. After that he is able to pay only 700 burles for the third days, covering the rest of the price with bonus burles. In the second sample case the most optimal way for Micha...
[]
202
25,190,400
0
31,111
797
Array Queries
[ "brute force", "data structures", "dp" ]
null
null
*a* is an array of *n* positive integers, all of which are not greater than *n*. You have to process *q* queries to this array. Each query is represented by two numbers *p* and *k*. Several operations are performed in each query; each operation changes *p* to *p*<=+<=*a**p*<=+<=*k*. There operations are applied until ...
The first line contains one integer *n* (1<=≀<=*n*<=≀<=100000). The second line contains *n* integers β€” elements of *a* (1<=≀<=*a**i*<=≀<=*n* for each *i* from 1 to *n*). The third line containts one integer *q* (1<=≀<=*q*<=≀<=100000). Then *q* lines follow. Each line contains the values of *p* and *k* for correspon...
Print *q* integers, *i*th integer must be equal to the answer to *i*th query.
[ "3\n1 1 1\n3\n1 1\n2 1\n3 1\n" ]
[ "2\n1\n1\n" ]
Consider first example: In first query after first operation *p* = 3, after second operation *p* = 5. In next two queries *p* is greater than *n* after the first operation.
[ { "input": "3\n1 1 1\n3\n1 1\n2 1\n3 1", "output": "2\n1\n1" }, { "input": "10\n3 5 4 3 7 10 6 7 2 3\n10\n4 5\n2 10\n4 6\n9 9\n9 2\n5 1\n6 4\n1 1\n5 6\n6 4", "output": "1\n1\n1\n1\n1\n1\n1\n2\n1\n1" }, { "input": "50\n6 2 5 6 10 2 5 8 9 2 9 5 10 4 3 6 10 6 1 1 3 7 2 1 7 8 5 9 6 2 7 6 1 7...
77
0
0
31,349
769
Cycle In Maze
[ "*special", "dfs and similar", "graphs", "greedy", "shortest paths" ]
null
null
The Robot is in a rectangular maze of size *n*<=Γ—<=*m*. Each cell of the maze is either empty or occupied by an obstacle. The Robot can move between neighboring cells on the side left (the symbol "L"), right (the symbol "R"), up (the symbol "U") or down (the symbol "D"). The Robot can move to the cell only if it is emp...
The first line contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=1000, 1<=≀<=*k*<=≀<=106) β€” the size of the maze and the length of the cycle. Each of the following *n* lines contains *m* symbols β€” the description of the maze. If the symbol equals to "." the current cell is empty. If the symbol equals to "...
Print the lexicographically minimum Robot's way with the length exactly *k*, which starts and ends in the cell where initially Robot is. If there is no such way, print "IMPOSSIBLE"(without quotes).
[ "2 3 2\n.**\nX..\n", "5 6 14\n..***.\n*...X.\n..*...\n..*.**\n....*.\n", "3 3 4\n***\n*X*\n***\n" ]
[ "RL\n", "DLDDLLLRRRUURU\n", "IMPOSSIBLE\n" ]
In the first sample two cyclic ways for the Robot with the length 2 exist β€” "UD" and "RL". The second cycle is lexicographically less. In the second sample the Robot should move in the following way: down, left, down, down, left, left, left, right, right, right, up, up, right, up. In the third sample the Robot can'...
[ { "input": "2 3 2\n.**\nX..", "output": "RL" }, { "input": "5 6 14\n..***.\n*...X.\n..*...\n..*.**\n....*.", "output": "DLDDLLLRRRUURU" }, { "input": "3 3 4\n***\n*X*\n***", "output": "IMPOSSIBLE" }, { "input": "1 1 1\nX", "output": "IMPOSSIBLE" }, { "input": "1 2...
46
0
-1
31,504
86
Tetris revisited
[ "constructive algorithms", "graph matchings", "greedy", "math" ]
B. Tetris revisited
1
256
Physicist Woll likes to play one relaxing game in between his search of the theory of everything. Game interface consists of a rectangular *n*<=Γ—<=*m* playing field and a dashboard. Initially some cells of the playing field are filled while others are empty. Dashboard contains images of all various connected (we mean ...
First line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=1000) β€” the height and the width of the field correspondingly. Next *n* lines contain *m* symbols each. They represent the field in a natural way: *j*-th character of the *i*-th line is "#" if the corresponding cell is filled, and "." if it is empty.
If there is no chance to win the game output the only number "-1" (without the quotes). Otherwise output any filling of the field by the figures in the following format: each figure should be represented by some digit and figures that touch each other by side should be represented by distinct digits. Every initially fi...
[ "2 3\n...\n#.#\n", "3 3\n.#.\n...\n..#\n", "3 3\n...\n.##\n.#.\n", "1 2\n##\n" ]
[ "000\n#0#\n", "5#1\n511\n55#\n", "-1\n", "##\n" ]
In the third sample, there is no way to fill a cell with no empty neighbours. In the forth sample, Woll does not have to fill anything, so we should output the field from the input.
[]
31
0
0
31,558
413
Maze 2D
[ "data structures", "divide and conquer" ]
null
null
The last product of the R2 company in the 2D games' field is a new revolutionary algorithm of searching for the shortest path in a 2<=Γ—<=*n* maze. Imagine a maze that looks like a 2<=Γ—<=*n* rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can ...
The first line contains two integers, *n* and *m* (1<=≀<=*n*<=≀<=2Β·105;Β 1<=≀<=*m*<=≀<=2Β·105) β€” the width of the maze and the number of queries, correspondingly. Next two lines contain the maze. Each line contains *n* characters, each character equals either '.' (empty cell), or 'X' (obstacle). Each of the next *m* lin...
Print *m* lines. In the *i*-th line print the answer to the *i*-th request β€” either the size of the shortest path or -1, if we can't reach the second cell from the first one.
[ "4 7\n.X..\n...X\n5 1\n1 3\n7 7\n1 4\n6 1\n4 7\n5 7\n", "10 3\nX...X..X..\n..X...X..X\n11 7\n7 18\n18 10\n" ]
[ "1\n4\n0\n5\n2\n2\n2\n", "9\n-1\n3\n" ]
none
[ { "input": "4 7\n.X..\n...X\n5 1\n1 3\n7 7\n1 4\n6 1\n4 7\n5 7", "output": "1\n4\n0\n5\n2\n2\n2" }, { "input": "10 3\nX...X..X..\n..X...X..X\n11 7\n7 18\n18 10", "output": "9\n-1\n3" }, { "input": "1 1\n.\n.\n1 2", "output": "1" }, { "input": "2 1\n..\n.X\n1 2", "output":...
2,000
0
0
31,613
896
Willem, Chtholly and Seniorious
[ "data structures", "probabilities" ]
null
null
β€” Willem... β€” What's the matter? β€” It seems that there's something wrong with Seniorious... β€” I'll have a look... Seniorious is made by linking special talismans in particular order. After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly. Seniorious has *n* pieces...
The only line contains four integers *n*,<=*m*,<=*seed*,<=*v**max* (1<=≀<=*n*,<=*m*<=≀<=105,<=0<=≀<=*seed*<=&lt;<=109<=+<=7,<=1<=≀<=*vmax*<=≀<=109). The initial values and operations are generated using following pseudo code: Here *op* is the type of the operation mentioned in the legend.
For each operation of types 3 or 4, output a line containing the answer.
[ "10 10 7 9\n", "10 10 9 9\n" ]
[ "2\n1\n0\n3\n", "1\n1\n3\n3\n" ]
In the first example, the initial array is {8, 9, 7, 2, 3, 1, 5, 6, 4, 8}. The operations are: - 2 6 7 9 - 1 3 10 8 - 4 4 6 2 4 - 1 4 5 8 - 2 1 7 1 - 4 7 9 4 4 - 1 2 7 9 - 4 5 8 1 1 - 2 5 7 5 - 4 3 10 8 5
[ { "input": "10 10 7 9", "output": "2\n1\n0\n3" }, { "input": "10 10 9 9", "output": "1\n1\n3\n3" }, { "input": "1000 1000 658073485 946088556", "output": "375432604\n52885108\n732131239\n335583873\n375432604\n582199284\n235058938\n682619432\n62026709\n631048460\n51394660\n25596188\n2...
0
0
-1
31,657
913
Logical Expression
[ "bitmasks", "dp", "shortest paths" ]
null
null
You are given a boolean function of three variables which is defined by its truth table. You need to find an expression of minimum length that equals to this function. The expression may consist of: - Operation AND ('&amp;', ASCII code 38) - Operation OR ('|', ASCII code 124) - Operation NOT ('!', ASCII code 33) -...
The first line contains one integer *n*Β β€” the number of functions in the input (1<=≀<=*n*<=≀<=10<=000). The following *n* lines contain descriptions of functions, the *i*-th of them contains a string of length 8 that consists of digits 0 and 1Β β€” the truth table of the *i*-th function. The digit on position *j* (0<=≀<=...
You should output *n* lines, the *i*-th line should contain the expression of minimum length which equals to the *i*-th function. If there is more than one such expression, output the lexicographically smallest of them. Expressions should satisfy the given grammar and shouldn't contain white spaces.
[ "4\n00110011\n00000111\n11110000\n00011111\n" ]
[ "y\n(y|z)&amp;x\n!x\nx|y&amp;z\n" ]
The truth table for the second function: <img class="tex-graphics" src="https://espresso.codeforces.com/2b70451f45cd74ee2be475affd7c407d7ed6d5fd.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "4\n00110011\n00000111\n11110000\n00011111", "output": "y\n(y|z)&x\n!x\nx|y&z" }, { "input": "1\n11001110", "output": "!y|!z&x" }, { "input": "2\n11001110\n01001001", "output": "!y|!z&x\n!(!x&!z|x&z|y)|x&y&z" }, { "input": "3\n10001001\n10111011\n10111101", "ou...
748
10,956,800
-1
31,658
935
Fifa and Fafa
[ "geometry" ]
null
null
Fifa and Fafa are sharing a flat. Fifa loves video games and wants to download a new soccer game. Unfortunately, Fafa heavily uses the internet which consumes the quota. Fifa can access the internet through his Wi-Fi access point. This access point can be accessed within a range of *r* meters (this range can be chosen ...
The single line of the input contains 5 space-separated integers *R*,<=*x*1,<=*y*1,<=*x*2,<=*y*2 (1<=≀<=*R*<=≀<=105, |*x*1|,<=|*y*1|,<=|*x*2|,<=|*y*2|<=≀<=105).
Print three space-separated numbers *x**ap*,<=*y**ap*,<=*r* where (*x**ap*,<=*y**ap*) is the position which Fifa chose for the access point and *r* is the radius of its range. Your answer will be considered correct if the radius does not differ from optimal more than 10<=-<=6 absolutely or relatively, and also the ra...
[ "5 3 3 1 1\n", "10 5 5 5 15\n" ]
[ "3.7677669529663684 3.7677669529663684 3.914213562373095\n", "5.0 5.0 10.0\n" ]
none
[ { "input": "5 3 3 1 1", "output": "3.7677669529663684 3.7677669529663684 3.914213562373095" }, { "input": "10 5 5 5 15", "output": "5.0 5.0 10.0" }, { "input": "5 0 0 0 7", "output": "0 0 5" }, { "input": "10 0 0 0 0", "output": "5.0 0.0 5.0" }, { "input": "100000...
31
0
0
31,680
772
Verifying Kingdom
[ "binary search", "divide and conquer", "interactive", "trees" ]
null
null
This is an interactive problem. The judge has a hidden rooted full binary tree with *n* leaves. A full binary tree is one where every node has either 0 or 2 children. The nodes with 0 children are called the leaves of the tree. Since this is a full binary tree, there are exactly 2*n*<=-<=1 nodes in the tree. The leave...
The first line of input will contain a single integer *n* (3<=≀<=*n*<=≀<=1<=000)Β β€” the number of leaves in the tree.
To print the final answer, print out the string "-1" on its own line. Then, the next line should contain 2*n*<=-<=1 integers. The *i*-th integer should be the parent of the *i*-th node, or -1, if it is the root. Your answer will be judged correct if your output is isomorphic to the judge's tree. In particular, the lab...
[ "5\nX\nZ\nY\nY\nX\n" ]
[ "1 4 2\n1 2 4\n2 4 1\n2 3 5\n2 4 3\n-1\n-1 1 1 2 2 3 3 6 6\n" ]
For the first sample, the judge has the hidden tree: <img class="tex-graphics" src="https://espresso.codeforces.com/2c9cae7de194cc1bc508ea7243ff4d0d509d34bd.png" style="max-width: 100.0%;max-height: 100.0%;"/> Here is a more readable format of the interaction:
[]
77
2,867,200
0
31,704
643
Bear and Two Paths
[ "constructive algorithms", "graphs" ]
null
null
Bearland has *n* cities, numbered 1 through *n*. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities. Bear Limak was once in a city *a* and he wanted to go to a city *b*. There was no direct connection so he decided to take a long walk, vis...
The first line of the input contains two integers *n* and *k* (4<=≀<=*n*<=≀<=1000, *n*<=-<=1<=≀<=*k*<=≀<=2*n*<=-<=2)Β β€” the number of cities and the maximum allowed number of roads, respectively. The second line contains four distinct integers *a*, *b*, *c* and *d* (1<=≀<=*a*,<=*b*,<=*c*,<=*d*<=≀<=*n*).
Print -1 if it's impossible to satisfy all the given conditions. Otherwise, print two lines with paths descriptions. The first of these two lines should contain *n* distinct integers *v*1,<=*v*2,<=...,<=*v**n* where *v*1<==<=*a* and *v**n*<==<=*b*. The second line should contain *n* distinct integers *u*1,<=*u*2,<=...,...
[ "7 11\n2 4 7 3\n", "1000 999\n10 20 30 40\n" ]
[ "2 7 1 3 6 5 4\n7 1 5 4 6 2 3\n", "-1\n" ]
In the first sample test, there should be 7 cities and at most 11 roads. The provided sample solution generates 10 roads, as in the drawing. You can also see a simple path of length *n* between 2 and 4, and a path between 7 and 3.
[ { "input": "7 11\n2 4 7 3", "output": "2 7 1 3 6 5 4\n7 1 5 4 6 2 3" }, { "input": "1000 999\n10 20 30 40", "output": "-1" }, { "input": "4 4\n1 2 3 4", "output": "-1" }, { "input": "5 6\n5 2 4 1", "output": "5 4 3 1 2\n4 5 3 2 1" }, { "input": "57 88\n54 30 5 43"...
93
2,048,000
0
31,764
0
none
[ "none" ]
null
null
Little Artem is given a graph, constructed as follows: start with some *k*-clique, then add new vertices one by one, connecting them to *k* already existing vertices that form a *k*-clique. Artem wants to count the number of spanning trees in this graph modulo 109<=+<=7.
First line of the input contains two integers *n* and *k* (1<=≀<=*n*<=≀<=10<=000, 1<=≀<=*k*<=≀<=*min*(*n*,<=5))Β β€” the total size of the graph and the size of the initial clique, respectively. Next *n*<=-<=*k* lines describe *k*<=+<=1-th, *k*<=+<=2-th, ..., *i*-th, ..., *n*-th vertices by listing *k* distinct vertex in...
Output a single integerΒ β€” the number of spanning trees in the given graph modulo 109<=+<=7.
[ "3 2\n1 2\n", "4 3\n1 2 3\n" ]
[ "3\n", "16\n" ]
none
[]
46
0
0
31,789
377
Captains Mode
[ "bitmasks", "dp", "games" ]
null
null
Kostya is a progamer specializing in the discipline of Dota 2. Valve Corporation, the developer of this game, has recently released a new patch which turned the balance of the game upside down. Kostya, as the captain of the team, realizes that the greatest responsibility lies on him, so he wants to resort to the analys...
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=100)Β β€” the number of heroes in Dota 2. The second line contains *n* integers *s*1, *s*2, ..., *s**n* (1<=≀<=*s**i*<=≀<=106)Β β€” the strengths of all the heroes. The third line contains a single integer *m* (2<=≀<=*m*<=≀<=*min*(*n*,<=20))Β β€” the number of action...
Print a single integerΒ β€” the difference between the strength of the first team and the strength of the second team if the captains of both teams will act optimally well.
[ "2\n2 1\n2\np 1\np 2\n", "6\n6 4 5 4 5 5\n4\nb 2\np 1\nb 1\np 2\n", "4\n1 2 3 4\n4\np 2\nb 2\np 1\nb 1\n" ]
[ "1\n", "0\n", "-2\n" ]
none
[ { "input": "2\n2 1\n2\np 1\np 2", "output": "1" }, { "input": "6\n6 4 5 4 5 5\n4\nb 2\np 1\nb 1\np 2", "output": "0" }, { "input": "4\n1 2 3 4\n4\np 2\nb 2\np 1\nb 1", "output": "-2" }, { "input": "4\n1 2 3 5\n4\nb 2\np 1\np 2\nb 1", "output": "1" }, { "input": "6...
61
0
0
31,916
0
none
[ "none" ]
null
null
Polycarpus has a chessboard of size *n*<=Γ—<=*m*, where *k* rooks are placed. Polycarpus hasn't yet invented the rules of the game he will play. However, he has already allocated *q* rectangular areas of special strategic importance on the board, they must be protected well. According to Polycarpus, a rectangular area o...
The first line contains four integers *n*, *m*, *k* and *q* (1<=≀<=*n*,<=*m*<=≀<=100<=000, 1<=≀<=*k*,<=*q*<=≀<=200<=000) β€” the sizes of the board, the number of rooks and the number of strategically important sites. We will consider that the cells of the board are numbered by integers from 1 to *n* horizontally and fro...
Print *q* lines. For each strategically important site print "YES" if it is well defended and "NO" otherwise.
[ "4 3 3 3\n1 1\n3 2\n2 3\n2 3 2 3\n2 1 3 3\n1 2 2 3\n" ]
[ "YES\nYES\nNO\n" ]
Picture to the sample: <img class="tex-graphics" src="https://espresso.codeforces.com/4b760b396c0058262fe776c85e60c5effd77ec1a.png" style="max-width: 100.0%;max-height: 100.0%;"/> For the last area the answer is "NO", because cell (1, 2) cannot be hit by a rook.
[]
30
0
0
31,979
625
Finals in arithmetic
[ "constructive algorithms", "implementation", "math" ]
null
null
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=10100<=000).
If there is no such positive integer *a* without leading zeroes that *a*<=+<=*a**r*<==<=*n* then print 0. Otherwise, print any valid *a*. If there are many possible answers, you are allowed to pick any.
[ "4\n", "11\n", "5\n", "33\n" ]
[ "2\n", "10\n", "0\n", "21\n" ]
In the first sample 4 = 2 + 2, *a* = 2 is the only possibility. In the second sample 11 = 10 + 1, *a* = 10Β β€” the only valid solution. Note, that *a* = 01 is incorrect, because *a* can't have leading zeroes. It's easy to check that there is no suitable *a* in the third sample. In the fourth sample 33 = 30 + 3 = 12 + ...
[ { "input": "4", "output": "2" }, { "input": "11", "output": "10" }, { "input": "5", "output": "0" }, { "input": "33", "output": "21" }, { "input": "1", "output": "0" }, { "input": "99", "output": "54" }, { "input": "100", "output": "0" ...
62
307,200
0
32,041
234
Cinema
[ "implementation" ]
null
null
Overall there are *m* actors in Berland. Each actor has a personal identifier β€” an integer from 1 to *m* (distinct actors have distinct identifiers). Vasya likes to watch Berland movies with Berland actors, and he has *k* favorite actors. He watched the movie trailers for the next month and wrote the following informat...
The first line of the input contains two integers *m* and *k* (1<=≀<=*m*<=≀<=100,<=1<=≀<=*k*<=≀<=*m*) β€” the number of actors in Berland and the number of Vasya's favourite actors. The second line contains *k* distinct integers *a**i* (1<=≀<=*a**i*<=≀<=*m*) β€” the identifiers of Vasya's favourite actors. The third lin...
Print *n* lines in the output. In the *i*-th line print: - 0, if the *i*-th movie will surely be the favourite; - 1, if the *i*-th movie won't surely be the favourite; - 2, if the *i*-th movie can either be favourite, or not favourite.
[ "5 3\n1 2 3\n6\nfirstfilm\n3\n0 0 0\nsecondfilm\n4\n0 0 4 5\nthirdfilm\n1\n2\nfourthfilm\n1\n5\nfifthfilm\n1\n4\nsixthfilm\n2\n1 0\n", "5 3\n1 3 5\n4\njumanji\n3\n0 0 0\ntheeagle\n5\n1 2 3 4 0\nmatrix\n3\n2 4 0\nsourcecode\n2\n2 4\n" ]
[ "2\n2\n1\n1\n1\n2\n", "2\n0\n1\n1\n" ]
Note to the second sample: - Movie jumanji can theoretically have from 1 to 3 Vasya's favourite actors. - Movie theeagle has all three favourite actors, as the actor Vasya failed to remember, can only have identifier 5. - Movie matrix can have exactly one favourite actor. - Movie sourcecode doesn't have any favou...
[ { "input": "5 3\n1 2 3\n6\nfirstfilm\n3\n0 0 0\nsecondfilm\n4\n0 0 4 5\nthirdfilm\n1\n2\nfourthfilm\n1\n5\nfifthfilm\n1\n4\nsixthfilm\n2\n1 0", "output": "2\n2\n1\n1\n1\n2" }, { "input": "5 3\n1 3 5\n4\njumanji\n3\n0 0 0\ntheeagle\n5\n1 2 3 4 0\nmatrix\n3\n2 4 0\nsourcecode\n2\n2 4", "output": "...
92
6,963,200
-1
32,114
62
Inquisition
[ "geometry", "implementation", "sortings" ]
C. Inquisition
3
256
In Medieval times existed the tradition of burning witches at steaks together with their pets, black cats. By the end of the 15-th century the population of black cats ceased to exist. The difficulty of the situation led to creating the EIC - the Emergency Inquisitory Commission. The resolution #666 says that a white ...
The first input line contains a single integer *n* (0<=≀<=*n*<=≀<=100). It is the number of spots on the cat's fur. The *i*-th of the last *n* lines contains 6 integers: *x*1*i*, *y*1*i*, *x*2*i*, *y*2*i*, *x*3*i*, *y*3*i*. They are the coordinates of the *i*-th triangular spot (0<=&lt;<=*x**ji*,<=*y**ji*<=&lt;<=105).
Print a single number, the answer to the problem, perimeter of the union of triangles. Your answer should differ from the correct one in no more than 10<=-<=6.
[ "1\n1 1 2 1 1 2\n", "3\n3 3 10 3 3 10\n1 1 9 4 5 6\n2 2 11 7 6 11\n" ]
[ "3.4142135624\n", "37.7044021497\n" ]
none
[]
92
0
0
32,133
383
Antimatter
[ "dp" ]
null
null
Iahub accidentally discovered a secret lab. He found there *n* devices ordered in a line, numbered from 1 to *n* from left to right. Each device *i* (1<=≀<=*i*<=≀<=*n*) can create either *a**i* units of matter or *a**i* units of antimatter. Iahub wants to choose some contiguous subarray of devices in the lab, specify...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=1000). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=1000). The sum *a*1<=+<=*a*2<=+<=...<=+<=*a**n* will be less than or equal to 10000.
Output a single integer, the number of ways Iahub can take a photo, modulo 1000000007 (109<=+<=7).
[ "4\n1 1 1 1\n" ]
[ "12\n" ]
The possible photos are [1+, 2-], [1-, 2+], [2+, 3-], [2-, 3+], [3+, 4-], [3-, 4+], [1+, 2+, 3-, 4-], [1+, 2-, 3+, 4-], [1+, 2-, 3-, 4+], [1-, 2+, 3+, 4-], [1-, 2+, 3-, 4+] and [1-, 2-, 3+, 4+], where "*i*+" means that the *i*-th element produces matter, and "*i*-" means that the *i*-th element produces antimatter.
[ { "input": "4\n1 1 1 1", "output": "12" }, { "input": "10\n16 9 9 11 10 12 9 6 10 8", "output": "86" }, { "input": "50\n2 1 5 2 1 3 1 2 3 2 1 1 5 2 2 2 3 2 1 2 2 2 3 3 1 3 1 1 2 2 2 2 1 2 3 1 2 4 1 1 1 3 2 1 1 1 3 2 1 3", "output": "115119382" }, { "input": "100\n8 3 3 7 3 6 ...
1,000
13,619,200
0
32,204
109
Lucky Probability
[ "brute force", "probabilities" ]
B. Lucky Probability
2
256
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer *p* from the ...
The single line contains five integers *p**l*, *p**r*, *v**l*, *v**r* and *k* (1<=≀<=*p**l*<=≀<=*p**r*<=≀<=109,<=1<=≀<=*v**l*<=≀<=*v**r*<=≀<=109,<=1<=≀<=*k*<=≀<=1000).
On the single line print the result with an absolute error of no more than 10<=-<=9.
[ "1 10 1 10 2\n", "5 6 8 10 1\n" ]
[ "0.320000000000\n", "1.000000000000\n" ]
Consider that [*a*, *b*] denotes an interval of integers; this interval includes the boundaries. That is, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/18b4a6012d95ad18891561410f0314497a578d63.png" style="max-width: 100.0%;max-height: 100.0%;"/> In first case there are 32 suitable pairs:...
[ { "input": "1 10 1 10 2", "output": "0.320000000000" }, { "input": "5 6 8 10 1", "output": "1.000000000000" }, { "input": "1 20 100 120 5", "output": "0.150000000000" }, { "input": "1 10 1 10 3", "output": "0.000000000000" }, { "input": "1 100 1 100 2", "outpu...
92
0
0
32,240
621
Rat Kwesh and Cheese
[ "brute force", "constructive algorithms", "math" ]
null
null
Wet Shark asked Rat Kwesh to generate three positive real numbers *x*, *y* and *z*, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point. Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportuni...
The only line of the input contains three space-separated real numbers *x*, *y* and *z* (0.1<=≀<=*x*,<=*y*,<=*z*<=≀<=200.0). Each of *x*, *y* and *z* is given with exactly one digit after the decimal point.
Find the maximum value of expression among *x**y**z*, *x**z**y*, (*x**y*)*z*, (*x**z*)*y*, *y**x**z*, *y**z**x*, (*y**x*)*z*, (*y**z*)*x*, *z**x**y*, *z**y**x*, (*z**x*)*y*, (*z**y*)*x* and print the corresponding expression. If there are many maximums, print the one that comes first in the list. *x**y**z* should be ...
[ "1.1 3.4 2.5\n", "2.0 2.0 2.0\n", "1.9 1.8 1.7\n" ]
[ "z^y^x\n", "x^y^z\n", "(x^y)^z\n" ]
none
[ { "input": "1.1 3.4 2.5", "output": "z^y^x" }, { "input": "2.0 2.0 2.0", "output": "x^y^z" }, { "input": "1.9 1.8 1.7", "output": "(x^y)^z" }, { "input": "2.0 2.1 2.2", "output": "x^z^y" }, { "input": "1.5 1.7 2.5", "output": "(z^x)^y" }, { "input": "1...
0
0
-1
32,253
452
Three strings
[ "data structures", "dsu", "string suffix structures", "strings" ]
null
null
You are given three strings (*s*1,<=*s*2,<=*s*3). For each integer *l* (1<=≀<=*l*<=≀<=*min*(|*s*1|,<=|*s*2|,<=|*s*3|) you need to find how many triples (*i*1,<=*i*2,<=*i*3) exist such that three strings *s**k*[*i**k*... *i**k*<=+<=*l*<=-<=1] (*k*<==<=1,<=2,<=3) are pairwise equal. Print all found numbers modulo 1000000...
First three lines contain three non-empty input strings. The sum of lengths of all strings is no more than 3Β·105. All strings consist only of lowercase English letters.
You need to output *min*(|*s*1|,<=|*s*2|,<=|*s*3|) numbers separated by spaces β€” answers for the problem modulo 1000000007Β (109<=+<=7).
[ "abc\nbc\ncbc\n", "abacaba\nabac\nabcd\n" ]
[ "3 1 \n", "11 2 0 0 \n" ]
Consider a string *t* = *t*<sub class="lower-index">1</sub>*t*<sub class="lower-index">2</sub>... *t*<sub class="lower-index">|*t*|</sub>, where *t*<sub class="lower-index">*i*</sub> denotes the *i*-th character of the string, and |*t*| denotes the length of the string. Then *t*[*i*... *j*] (1 ≀ *i* ≀ *j* ≀ |*t*|) rep...
[]
46
0
0
32,256
1,000
Yet Another Problem On a Subsequence
[ "combinatorics", "dp" ]
null
null
The sequence of integers $a_1, a_2, \dots, a_k$ is called a good array if $a_1 = k - 1$ and $a_1 &gt; 0$. For example, the sequences $[3, -1, 44, 0], [1, -99]$ are good arrays, and the sequences $[3, 7, 8], [2, 5, 4, 1], [0]$ β€” are not. A sequence of integers is called good if it can be divided into a positive number ...
The first line contains the number $n~(1 \le n \le 10^3)$ β€” the length of the initial sequence. The following line contains $n$ integers $a_1, a_2, \dots, a_n~(-10^9 \le a_i \le 10^9)$ β€” the sequence itself.
In the single line output one integer β€” the number of subsequences of the original sequence that are good sequences, taken modulo 998244353.
[ "3\n2 1 1\n", "4\n1 1 1 1\n" ]
[ "2\n", "7\n" ]
In the first test case, two good subsequences β€” $[a_1, a_2, a_3]$ and $[a_2, a_3]$. In the second test case, seven good subsequences β€” $[a_1, a_2, a_3, a_4], [a_1, a_2], [a_1, a_3], [a_1, a_4], [a_2, a_3], [a_2, a_4]$ and $[a_3, a_4]$.
[ { "input": "3\n2 1 1", "output": "2" }, { "input": "4\n1 1 1 1", "output": "7" }, { "input": "1\n0", "output": "0" }, { "input": "1\n1", "output": "0" } ]
93
3,584,000
0
32,272
741
Arpa's loud Owf and Mehrdad's evil plan
[ "dfs and similar", "math" ]
null
null
As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to *n*. Everyone has exactly one crush, *i*-th person's crush is person with the number *crush**i*. Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as...
The first line of input contains integer *n* (1<=≀<=*n*<=≀<=100)Β β€” the number of people in Arpa's land. The second line contains *n* integers, *i*-th of them is *crush**i* (1<=≀<=*crush**i*<=≀<=*n*)Β β€” the number of *i*-th person's crush.
If there is no *t* satisfying the condition, print -1. Otherwise print such smallest *t*.
[ "4\n2 3 1 4\n", "4\n4 4 4 4\n", "4\n2 1 4 3\n" ]
[ "3\n", "-1\n", "1\n" ]
In the first sample suppose *t* = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the...
[ { "input": "4\n2 3 1 4", "output": "3" }, { "input": "4\n4 4 4 4", "output": "-1" }, { "input": "4\n2 1 4 3", "output": "1" }, { "input": "5\n2 4 3 1 2", "output": "-1" }, { "input": "5\n2 2 4 4 5", "output": "-1" }, { "input": "5\n2 4 5 4 2", "out...
93
0
3
32,308
120
Brevity is Soul of Wit
[ "graph matchings" ]
null
null
As we communicate, we learn much new information. However, the process of communication takes too much time. It becomes clear if we look at the words we use in our everyday speech. We can list many simple words consisting of many letters: "information", "technologies", "university", "construction", "conservatoire", "r...
The first line of the input file contains the only integer *n* (1<=≀<=*n*<=≀<=200). Then *n* lines contain a set of different non-empty words that consist of lowercase Latin letters. The length of each word does not exceed 10 characters.
If the solution exists, print in the output file exactly *n* lines, where the *i*-th line represents the shortened variant of the *i*-th word from the initial set. If there are several variants to solve the problem, print any of them. If there is no solution, print -1.
[ "6\nprivet\nspasibo\ncodeforces\njava\nmarmelad\nnormalno\n", "5\naaa\naa\na\naaaa\naaaaa\n" ]
[ "pret\nsps\ncdfs\njava\nmama\nnorm\n", "-1\n" ]
none
[ { "input": "6\nprivet\nspasibo\ncodeforces\njava\nmarmelad\nnormalno", "output": "pr\np\ne\nv\nre\nr" }, { "input": "5\naaa\naa\na\naaaa\naaaaa", "output": "-1" }, { "input": "26\naaaaa\naaaab\naaaac\naaaad\naaaae\naaaaf\naaaag\naaaah\naaaai\naaaaj\naaaak\naaaal\naaaam\naaaan\naaaao\naaa...
62
0
0
32,349
335
Banana
[ "binary search", "constructive algorithms", "greedy" ]
null
null
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly *n* stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length *n*. Piegirl wants to create a string *s* using stickers. She may buy as many s...
The first line contains string *s* (1<=≀<=|*s*|<=≀<=1000), consisting of lowercase English characters only. The second line contains an integer *n* (1<=≀<=*n*<=≀<=1000).
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of *n* lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string *s*, pr...
[ "banana\n4\n", "banana\n3\n", "banana\n2\n" ]
[ "2\nbaan\n", "3\nnab\n", "-1\n" ]
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
[ { "input": "banana\n4", "output": "2\nbaan" }, { "input": "banana\n3", "output": "3\nnab" }, { "input": "banana\n2", "output": "-1" }, { "input": "p\n1000", "output": "1\npaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
92
0
0
32,424