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
12
Start of the session
[ "constructive algorithms" ]
E. Start of the season
2
256
Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size *n*<=×<=*n* (*n* is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to *n*<=-<=1, main diagonal should...
The first line contains one integer *n* (2<=≤<=*n*<=≤<=1000), *n* is even.
Output *n* lines with *n* numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any.
[ "2\n", "4\n" ]
[ "0 1\n1 0\n", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0\n" ]
none
[ { "input": "2", "output": "0 1\n1 0" }, { "input": "4", "output": "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0" }, { "input": "6", "output": "0 1 4 2 5 3\n1 0 2 5 3 4\n4 2 0 3 1 5\n2 5 3 0 4 1\n5 3 1 4 0 2\n3 4 5 1 2 0" }, { "input": "8", "output": "0 1 5 2 6 3 7 4\n1 0 2 6 3 7 4 ...
2,000
22,118,400
0
20,163
235
Let's Play Osu!
[ "dp", "math", "probabilities" ]
null
null
You're playing a game called Osu! Here's a simplified version of it. There are *n* clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of *n* characters "O" and "X". Using the play sequence you can calculate...
The first line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of clicks. The second line contains *n* space-separated real numbers *p*1,<=*p*2,<=...,<=*p**n* (0<=≤<=*p**i*<=≤<=1). There will be at most six digits after the decimal point in the given *p**i*.
Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
[ "3\n0.5 0.5 0.5\n", "4\n0.7 0.2 0.1 0.9\n", "5\n1 1 1 1 1\n" ]
[ "2.750000000000000\n", "2.489200000000000\n", "25.000000000000000\n" ]
For the first example. There are 8 possible outcomes. Each has a probability of 0.125. - "OOO"  →  3<sup class="upper-index">2</sup> = 9; - "OOX"  →  2<sup class="upper-index">2</sup> = 4; - "OXO"  →  1<sup class="upper-index">2</sup> + 1<sup class="upper-index">2</sup> = 2; - "OXX"  →  1<sup class="upper-index">2...
[ { "input": "3\n0.5 0.5 0.5", "output": "2.750000000000000" }, { "input": "4\n0.7 0.2 0.1 0.9", "output": "2.489200000000000" }, { "input": "5\n1 1 1 1 1", "output": "25.000000000000000" }, { "input": "10\n0.684846 0.156794 0.153696 0.714526 0.281868 0.628256 0.745339 0.123854...
374
9,318,400
3
20,164
960
Full Binary Tree Queries
[ "brute force", "implementation", "trees" ]
null
null
You have a full binary tree having infinite levels. Each node has an initial value. If a node has value *x*, then its left child has value 2·*x* and its right child has value 2·*x*<=+<=1. The value of the root is 1. You need to answer *Q* queries. There are 3 types of queries: 1. Cyclically shift the values of...
The first line contains a single integer *Q* (1<=≤<=*Q*<=≤<=105). Then *Q* queries follow, one per line: - Queries of type 1 and 2 have the following format: *T* *X* *K* (1<=≤<=*T*<=≤<=2; 1<=≤<=*X*<=≤<=1018; 0<=≤<=|*K*|<=≤<=1018), where *T* is type of the query.- Queries of type 3 have the following format: 3 *X* (...
For each query of type 3, print the values of all nodes encountered in descending order.
[ "5\n3 12\n1 2 1\n3 12\n2 4 -1\n3 8\n", "5\n3 14\n1 5 -3\n3 14\n1 3 1\n3 14\n" ]
[ "12 6 3 1 \n12 6 2 1 \n8 4 2 1 \n", "14 7 3 1 \n14 6 3 1 \n14 6 2 1 \n" ]
Following are the images of the first 4 levels of the tree in the first test case: Original: After query 1 2 1: After query 2 4 -1:
[ { "input": "5\n3 12\n1 2 1\n3 12\n2 4 -1\n3 8", "output": "12 6 3 1 \n12 6 2 1 \n8 4 2 1 " }, { "input": "5\n3 14\n1 5 -3\n3 14\n1 3 1\n3 14", "output": "14 7 3 1 \n14 6 3 1 \n14 6 2 1 " }, { "input": "6\n3 1\n2 1 0\n3 10\n2 1 -4\n3 10\n2 10 -5", "output": "1 \n10 5 2 1 \n10 5 2 1 " ...
0
0
-1
20,204
0
none
[ "none" ]
null
null
You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library. Some long and uninteresting story was removed... The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large ve...
The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=100<=000, 1<=≤<=*m*<=≤<=100<=000) — the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to *m*. Each of the next *n* lines contains a description of one word...
In the first line print "Yes" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print "No" (without quotes). If the required is possible, in the second line print *k* — the number of letters Denis has to capitali...
[ "4 3\n1 2\n1 1\n3 1 3 2\n2 1 1\n", "6 5\n2 1 2\n2 1 2\n3 1 2 3\n2 1 5\n2 4 4\n2 4 4\n", "4 3\n4 3 2 2 1\n3 1 1 3\n3 2 3 3\n2 3 1\n" ]
[ "Yes\n2\n2 3 ", "Yes\n0\n", "No\n" ]
In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following: - 2' - 1 - 1 3' 2' - 1 1 The condition 2' &lt; 1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order....
[ { "input": "4 3\n1 2\n1 1\n3 1 3 2\n2 1 1", "output": "Yes\n2\n2 3 " }, { "input": "6 5\n2 1 2\n2 1 2\n3 1 2 3\n2 1 5\n2 4 4\n2 4 4", "output": "Yes\n0" }, { "input": "4 3\n4 3 2 2 1\n3 1 1 3\n3 2 3 3\n2 3 1", "output": "No" }, { "input": "4 4\n3 3 4 1\n4 3 4 2 2\n4 2 1 2 3\n...
30
0
0
20,232
113
Grammar Lessons
[ "implementation", "strings" ]
A. Grammar Lessons
5
256
Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules: - There are three parts of spee...
The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105. It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible tha...
If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes).
[ "petr\n", "etis atis animatis etis atis amatis\n", "nataliala kataliala vetra feinites\n" ]
[ "YES\n", "NO\n", "YES\n" ]
none
[ { "input": "petr", "output": "YES" }, { "input": "etis atis animatis etis atis amatis", "output": "NO" }, { "input": "nataliala kataliala vetra feinites", "output": "YES" }, { "input": "qweasbvflios", "output": "YES" }, { "input": "lios lios petr initis qwe", ...
60
0
-1
20,260
404
Restore Graph
[ "dfs and similar", "graphs", "sortings" ]
null
null
Valera had an undirected connected graph without self-loops and multiple edges consisting of *n* vertices. The graph had an interesting property: there were at most *k* edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to *n*. One day Valera...
The first line contains two space-separated integers *n* and *k* (1<=≤<=*k*<=&lt;<=*n*<=≤<=105). Number *n* shows the number of vertices in the original graph. Number *k* shows that at most *k* edges were adjacent to each vertex in the original graph. The second line contains space-separated integers *d*[1],<=*d*[2],<...
If Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer *m* (0<=≤<=*m*<=≤<=106) — the number of edges in the found graph. In each of the next *m* lines print two space-separated integers *a**i* and *b**i* (1<=≤<=*a**i*,<=...
[ "3 2\n0 1 1\n", "4 2\n2 0 1 3\n", "3 1\n0 0 0\n" ]
[ "3\n1 2\n1 3\n3 2\n", "3\n1 3\n1 4\n2 3\n", "-1\n" ]
none
[ { "input": "3 2\n0 1 1", "output": "2\n1 2\n1 3" }, { "input": "4 2\n2 0 1 3", "output": "3\n1 3\n1 4\n2 3" }, { "input": "3 1\n0 0 0", "output": "-1" }, { "input": "5 3\n0 2 1 2 1", "output": "4\n1 3\n1 5\n2 5\n4 5" }, { "input": "7 3\n2 2 0 1 3 2 1", "output...
483
36,044,800
3
20,266
576
Points on Plane
[ "constructive algorithms", "divide and conquer", "geometry", "greedy", "sortings" ]
null
null
On a plane are *n* points (*x**i*, *y**i*) with integer coordinates between 0 and 106. The distance between the two points with numbers *a* and *b* is said to be the following value: (the distance calculated by such formula is called Manhattan distance). We call a hamiltonian path to be some permutation *p**i* of num...
The first line contains integer *n* (1<=≤<=*n*<=≤<=106). The *i*<=+<=1-th line contains the coordinates of the *i*-th point: *x**i* and *y**i* (0<=≤<=*x**i*,<=*y**i*<=≤<=106). It is guaranteed that no two points coincide.
Print the permutation of numbers *p**i* from 1 to *n* — the sought Hamiltonian path. The permutation must meet the inequality . If there are multiple possible answers, print any of them. It is guaranteed that the answer exists.
[ "5\n0 7\n8 10\n3 4\n5 0\n9 12\n" ]
[ "4 3 1 2 5 \n" ]
In the sample test the total distance is: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c772e61c616e1c27114e3facb9e6db6c5cf93b82.png" style="max-width: 100.0%;max-height: 100.0%;"/> (|5 - 3| + |0 - 4|) + (|3 - 0| + |4 - 7|) + (|0 - 8| + |7 - 10|) + (|8 - 9| + |10 - 12|) = 2 + 4 + 3 + 3 ...
[]
2,000
116,531,200
0
20,344
936
World of Tank
[ "dp", "greedy" ]
null
null
Vitya loves programming and problem solving, but sometimes, to distract himself a little, he plays computer games. Once he found a new interesting game about tanks, and he liked it so much that he went through almost all levels in one day. Remained only the last level, which was too tricky. Then Vitya remembered that h...
The first line contains four integers *n*, *m*1, *m*2 and *t*, the length of the field, the number of obstacles in the first lane, the number of obstacles in the second lane and the number of tank steps before reloading, respectively (1<=≤<=*n*<=≤<=109; 0<=≤<=*m*1,<=*m*2<=≤<=*n*; 0<=≤<=*m*1<=+<=*m*2<=≤<=106; 1<=≤<=*t*<...
In the first line print «Yes», if it is possible to pass the level, or «No», otherwise. If it is possible, then in the second line print the number of times the tank moves from one lane to another, and in the next line print the coordinates of the transitions, one number per transition: the coordinate *x* (0<=≤<=*x*<=...
[ "6 2 3 2\n2 6\n3 5 6\n", "1 1 1 1\n1\n1\n", "9 5 2 5\n1 2 7 8 9\n4 6\n" ]
[ "Yes\n2\n0 3 \n2\n2 2\n4 1\n", "No\n", "Yes\n4\n0 3 5 10 \n1\n5 2\n" ]
Picture for the first sample test.
[]
77
0
0
20,350
710
Generate a String
[ "dfs and similar", "dp" ]
null
null
zscoder wants to generate an input file for some programming competition problem. His input is a string consisting of *n* letters 'a'. He is too lazy to write a generator so he will manually generate the input in a text editor. Initially, the text editor is empty. It takes him *x* seconds to insert or delete a letter...
The only line contains three integers *n*, *x* and *y* (1<=≤<=*n*<=≤<=107, 1<=≤<=*x*,<=*y*<=≤<=109) — the number of letters 'a' in the input file and the parameters from the problem statement.
Print the only integer *t* — the minimum amount of time needed to generate the input file.
[ "8 1 1\n", "8 1 10\n" ]
[ "4\n", "8\n" ]
none
[ { "input": "8 1 1", "output": "4" }, { "input": "8 1 10", "output": "8" }, { "input": "10 62 99", "output": "384" }, { "input": "88 417 591", "output": "4623" }, { "input": "57 5289 8444", "output": "60221" }, { "input": "382 81437847 324871127", "...
0
0
-1
20,352
491
Up the hill
[ "constructive algorithms", "implementation" ]
null
null
Hiking club "Up the hill" just returned from a walk. Now they are trying to remember which hills they've just walked through. It is known that there were *N* stops, all on different integer heights between 1 and *N* kilometers (inclusive) above the sea level. On the first day they've traveled from the first stop to th...
In the first line there is an integer non-negative number *A* denoting the number of days of climbing up the hill. Second line contains an integer non-negative number *B* — the number of days of walking down the hill (*A*<=+<=*B*<=+<=1<==<=*N*, 1<=≤<=*N*<=≤<=100<=000).
Output *N* space-separated distinct integers from 1 to *N* inclusive, denoting possible heights of the stops in order of visiting.
[ "0\n1\n", "2\n1" ]
[ "2 1 \n", "1 3 4 2" ]
none
[ { "input": "0\n1", "output": "2 1 " }, { "input": "2\n1", "output": "2 3 4 1 " }, { "input": "0\n3", "output": "4 3 2 1 " }, { "input": "1\n1", "output": "2 3 1 " }, { "input": "3\n7", "output": "8 9 10 11 7 6 5 4 3 2 1 " }, { "input": "700\n300", ...
124
9,011,200
3
20,375
452
4-point polyline
[ "brute force", "constructive algorithms", "geometry", "trees" ]
null
null
You are given a rectangular grid of lattice points from (0,<=0) to (*n*,<=*m*) inclusive. You have to choose exactly 4 different points to build a polyline possibly with self-intersections and self-touching. This polyline should be as long as possible. A polyline defined by points *p*1,<=*p*2,<=*p*3,<=*p*4 consists of...
The only line of the input contains two integers *n* and *m* (0<=≤<=*n*,<=*m*<=≤<=1000). It is guaranteed that grid contains at least 4 different points.
Print 4 lines with two integers per line separated by space — coordinates of points *p*1,<=*p*2,<=*p*3,<=*p*4 in order which represent the longest possible polyline. Judge program compares your answer and jury's answer with 10<=-<=6 precision.
[ "1 1\n", "0 10\n" ]
[ "1 1\n0 0\n1 0\n0 1\n", "0 1\n0 10\n0 0\n0 9\n" ]
none
[ { "input": "1 1", "output": "0 0\n1 1\n0 1\n1 0" }, { "input": "0 10", "output": "0 1\n0 10\n0 0\n0 9" }, { "input": "10 10", "output": "10 9\n0 0\n10 10\n1 0" }, { "input": "100 100", "output": "0 1\n100 100\n0 0\n99 100" }, { "input": "10 100", "output": "9 ...
61
0
0
20,401
10
Digital Root
[ "number theory" ]
C. Digital Root
2
256
Not long ago Billy came across such a problem, where there were given three natural numbers *A*, *B* and *C* from the range [1,<=*N*], and it was asked to check whether the equation *AB*<==<=*C* is correct. Recently Billy studied the concept of a digital root of a number. We should remind you that a digital root *d*(*x...
The first line contains the only number *N* (1<=≤<=*N*<=≤<=106).
Output one number — the amount of required *A*, *B* and *C* from the range [1,<=*N*].
[ "4\n", "5\n" ]
[ "2\n", "6\n" ]
For the first sample the required triples are (3, 4, 3) and (4, 3, 3).
[ { "input": "4", "output": "2" }, { "input": "5", "output": "6" }, { "input": "6", "output": "14" }, { "input": "7", "output": "25" }, { "input": "1", "output": "0" }, { "input": "8", "output": "40" }, { "input": "10", "output": "82" }...
60
0
-1
20,403
63
Settlers' Training
[ "implementation" ]
B. Settlers' Training
2
256
In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly *n* soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either i...
The first line contains two integers *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains *n* numbers in the non-decreasing order. The *i*-th of them, *a**i*, represents the rank of the *i*-th soldier in the defense buil...
Print a single integer — the number of golden coins needed to raise all the soldiers to the maximal rank.
[ "4 4\n1 2 2 3\n", "4 3\n1 1 1 1\n" ]
[ "4", "5" ]
In the first example the ranks will be raised in the following manner: 1 2 2 3  →  2 2 3 4  →  2 3 4 4  →  3 4 4 4  →  4 4 4 4 Thus totals to 4 training sessions that require 4 golden coins.
[ { "input": "4 4\n1 2 2 3", "output": "4" }, { "input": "4 3\n1 1 1 1", "output": "5" }, { "input": "3 3\n1 2 3", "output": "2" }, { "input": "1 1\n1", "output": "0" }, { "input": "1 5\n1", "output": "4" }, { "input": "1 5\n4", "output": "1" }, ...
310
21,606,400
3.882255
20,416
513
Second price auction
[ "bitmasks", "probabilities" ]
null
null
Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receive...
The first line of input contains an integer number *n* (2<=≤<=*n*<=≤<=5). *n* lines follow, the *i*-th of them containing two numbers *L**i* and *R**i* (1<=≤<=*L**i*<=≤<=*R**i*<=≤<=10000) describing the *i*-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submi...
Output the answer with absolute or relative error no more than 1*e*<=-<=9.
[ "3\n4 7\n8 10\n5 5\n", "3\n2 5\n3 4\n1 6\n" ]
[ "5.7500000000\n", "3.5000000000\n" ]
Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first ...
[ { "input": "3\n4 7\n8 10\n5 5", "output": "5.7500000000" }, { "input": "3\n2 5\n3 4\n1 6", "output": "3.5000000000" }, { "input": "5\n1 10000\n1 10000\n1 10000\n1 10000\n1 10000", "output": "6667.1666666646" }, { "input": "2\n1 2\n1 2", "output": "1.2500000000" }, { ...
2,000
28,672,000
0
20,423
747
Comments
[ "dfs and similar", "expression parsing", "implementation", "strings" ]
null
null
A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment ex...
The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (...
Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.
[ "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0\n", "a,5,A,0,a,0,A,0,a,0,A,0\n", "A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0\n" ]
[ "3\nhello test one \nok bye two \na b \n", "2\na \nA a A a A \n", "4\nA K M \nB F H L N O \nC D G I P \nE J \n" ]
The first example is explained in the statements.
[ { "input": "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0", "output": "3\nhello test one \nok bye two \na b " }, { "input": "a,5,A,0,a,0,A,0,a,0,A,0", "output": "2\na \nA a A a A " }, { "input": "A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0", "output": "4\nA K M \n...
592
98,099,200
-1
20,437
809
Glad to see you!
[ "binary search", "interactive" ]
null
null
This is an interactive problem. In the output section below you will see the information about flushing the output. On Sunday Leha the hacker took Nura from the house where she lives and went with her to one of the most luxurious restaurants in Vičkopolis. Upon arrival, they left the car in a huge parking lot near the...
There are two numbers *n* and *k* (2<=≤<=*k*<=≤<=*n*<=≤<=105) in the single line of input denoting the number of dishes in the menu and the number of dishes Noora ordered.
If you want to provide an answer, output a string of the form 2 *x* *y* (1<=≤<=*x*,<=*y*<=≤<=*n*,<=*x*<=≠<=*y*), if you think the dishes *x* and *y* was among dishes ordered by Noora. After that, flush the output and terminate your program.
[ "3 2\nNIE\nTAK\nNIE\nTAK\nTAK\nTAK\n" ]
[ "1 1 2\n1 2 1\n1 1 3\n1 3 1\n1 2 3\n1 3 2\n2 2 3\n" ]
There are three dishes in sample. Noora ordered dished numberes 2 and 3, which Leha should guess. If Noora receive requests for the first dish (*x* = 1), then she'll choose the second dish (*a* = 2) as the dish with the minimum value <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5a4a1705...
[]
46
5,632,000
0
20,442
350
Wrong Floyd
[ "brute force", "constructive algorithms", "dfs and similar", "graphs" ]
null
null
Valera conducts experiments with algorithms that search for shortest paths. He has recently studied the Floyd's algorithm, so it's time to work with it. Valera's already written the code that counts the shortest distance between any pair of vertexes in a non-directed connected graph from *n* vertexes and *m* edges, co...
The first line of the input contains three integers *n*,<=*m*,<=*k* (3<=≤<=*n*<=≤<=300, 2<=≤<=*k*<=≤<=*n* , ) — the number of vertexes, the number of edges and the number of marked vertexes. The second line of the input contains *k* space-separated integers *a*1,<=*a*2,<=... *a**k* (1<=≤<=*a**i*<=≤<=*n*) — the number...
If the graph doesn't exist, print -1 on a single line. Otherwise, print *m* lines, each containing two integers *u*,<=*v* — the description of the edges of the graph Valera's been looking for.
[ "3 2 2\n1 2\n", "3 3 2\n1 2\n" ]
[ "1 3\n2 3\n", "-1\n" ]
none
[ { "input": "3 2 2\n1 2", "output": "1 3\n2 3" }, { "input": "3 3 2\n1 2", "output": "-1" }, { "input": "300 43056 2\n5 6", "output": "1 2\n2 3\n3 4\n4 7\n5 300\n6 300\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24...
592
102,400
0
20,457
961
Pair Of Lines
[ "geometry" ]
null
null
You are given *n* points on Cartesian plane. Every point is a lattice point (i.<=e. both of its coordinates are integers), and all points are distinct. You may draw two straight lines (not necessarily distinct). Is it possible to do this in such a way that every point lies on at least one of these lines?
The first line contains one integer *n* (1<=≤<=*n*<=≤<=105) — the number of points you are given. Then *n* lines follow, each line containing two integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=≤<=109)— coordinates of *i*-th point. All *n* points are distinct.
If it is possible to draw two straight lines in such a way that each of given points belongs to at least one of these lines, print YES. Otherwise, print NO.
[ "5\n0 0\n0 1\n1 1\n1 -1\n2 2\n", "5\n0 0\n1 0\n2 1\n1 1\n2 3\n" ]
[ "YES\n", "NO\n" ]
In the first example it is possible to draw two lines, the one containing the points 1, 3 and 5, and another one containing two remaining points.
[ { "input": "5\n0 0\n0 1\n1 1\n1 -1\n2 2", "output": "YES" }, { "input": "5\n0 0\n1 0\n2 1\n1 1\n2 3", "output": "NO" }, { "input": "1\n-1000000000 1000000000", "output": "YES" }, { "input": "5\n2 -1\n-4 1\n0 -9\n5 -9\n9 -10", "output": "NO" }, { "input": "5\n6 1\n...
514
71,680,000
3
20,468
920
SUM and REPLACE
[ "brute force", "data structures", "dsu", "number theory" ]
null
null
Let *D*(*x*) be the number of positive divisors of a positive integer *x*. For example, *D*(2)<==<=2 (2 is divisible by 1 and 2), *D*(6)<==<=4 (6 is divisible by 1, 2, 3 and 6). You are given an array *a* of *n* integers. You have to process two types of queries: 1. REPLACE *l* *r* — for every replace *a**i* with *...
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=3·105) — the number of elements in the array and the number of queries to process, respectively. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=106) — the elements of the array. Then *m* lines follow, each containin...
For each SUM query print the answer to it.
[ "7 6\n6 4 1 10 3 2 4\n2 1 7\n2 4 5\n1 3 5\n2 4 4\n1 5 7\n2 1 7\n" ]
[ "30\n13\n4\n22\n" ]
none
[ { "input": "7 6\n6 4 1 10 3 2 4\n2 1 7\n2 4 5\n1 3 5\n2 4 4\n1 5 7\n2 1 7", "output": "30\n13\n4\n22" }, { "input": "4 2\n1 1 1 3\n1 1 4\n2 1 4", "output": "5" }, { "input": "10 2\n1 1 1 1 1 1 1 1 1 9\n1 1 10\n2 1 10", "output": "12" }, { "input": "4 2\n1 1 3 1\n1 1 4\n2 1 4"...
2,000
15,462,400
0
20,495
976
Degree Set
[ "constructive algorithms", "graphs", "implementation" ]
null
null
You are given a sequence of *n* positive integers *d*1,<=*d*2,<=...,<=*d**n* (*d*1<=&lt;<=*d*2<=&lt;<=...<=&lt;<=*d**n*). Your task is to construct an undirected graph such that: - there are exactly *d**n*<=+<=1 vertices; - there are no self-loops; - there are no multiple edges; - there are no more than 106 edges;...
The first line contains one integer *n* (1<=≤<=*n*<=≤<=300) — the size of the degree set. The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (1<=≤<=*d**i*<=≤<=1000, *d*1<=&lt;<=*d*2<=&lt;<=...<=&lt;<=*d**n*) — the degree set.
In the first line print one integer *m* (1<=≤<=*m*<=≤<=106) — the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges. Each of the next *m* lines should contain two integers *v**i* and *u**i* (1<=≤<=*v**i*,<=*u**i*<...
[ "3\n2 3 4\n", "3\n1 2 3\n" ]
[ "8\n3 1\n4 2\n4 5\n2 5\n5 1\n3 2\n2 1\n5 3\n", "4\n1 2\n1 3\n1 4\n2 3\n" ]
none
[ { "input": "3\n2 3 4", "output": "8\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4" }, { "input": "3\n1 2 3", "output": "4\n1 2\n1 3\n1 4\n2 3" }, { "input": "4\n1 3 4 6", "output": "11\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n2 3\n2 4\n2 5\n3 4\n3 5" }, { "input": "1\n1", "output": "1\n1...
780
58,470,400
0
20,498
466
Information Graph
[ "dfs and similar", "dsu", "graphs", "trees" ]
null
null
There are *n* employees working in company "X" (let's number them from 1 to *n* for convenience). Initially the employees didn't have any relationships among each other. On each of *m* next days one of the following events took place: - either employee *y* became the boss of employee *x* (at that, employee *x* didn't...
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105) — the number of employees and the number of events. Each of the next *m* lines contains the description of one event (the events are given in the chronological order). The first number of the line determines the type of event *t* (1<=≤<=*t*<=≤...
For each query of the third type print "YES" if the employee signed the document package and "NO" otherwise. Print all the words without the quotes.
[ "4 9\n1 4 3\n2 4\n3 3 1\n1 2 3\n2 2\n3 1 2\n1 3 1\n2 2\n3 1 3\n" ]
[ "YES\nNO\nYES\n" ]
none
[]
30
0
0
20,532
929
Пограничные врата
[]
null
null
Герой Аркадий находится на узкой полоске земли, разделенной на *n* зон, пронумерованных от 1 до *n*. Из *i*-й зоны можно пройти лишь в (*i*<=-<=1)-ю зону и в (*i*<=+<=1)-ю зону, если они существуют. При этом между каждой парой соседних зон находятся пограничные врата, которые могут быть разных цветов, цвет врат между *...
Первая строка содержит три целых числа *n*, *a*, *b* (2<=≤<=*n*<=≤<=100<=000, 1<=≤<=*a*,<=*b*<=≤<=*n*, *a*<=≠<=*b*) — число зон, номер начальной зоны и номер конечной зоны, соответственно. Вторая строка содержит *n*<=-<=1 целое число *g*1,<=*g*2,<=...,<=*g**n*<=-<=1 (1<=≤<=*g**i*<=≤<=100<=000), где *g**i* означает цве...
Если Аркадий не может попасть из зоны *a* в зону *b*, не имея изначально ключей, выведите -1. Иначе выведите минимальное количество ходов, которое потребуется Аркадию.
[ "5 4 1\n3 1 1 2\n7 1 2 1 3\n", "5 1 5\n4 3 2 1\n4 3 2 5 5\n" ]
[ "7\n", "-1\n" ]
В первом примере, чтобы попасть из зоны 4 в зону 1, Аркадию нужно сначала взять ключ цвета 1, пройти в зону 3, там взять ключ цвета 2 и пройти обратно в зону 4 и затем в зону 5, взять там ключ цвета 3 и дойти до зоны 1 за четыре хода. Во втором примере Аркадий может дойти лишь до четвертой зоны, так как шатров храните...
[]
46
5,632,000
-1
20,534
758
Blown Garland
[ "brute force", "implementation", "number theory" ]
null
null
Nothing is eternal in the world, Kostya understood it on the 7-th of January when he saw partially dead four-color garland. Now he has a goal to replace dead light bulbs, however he doesn't know how many light bulbs for each color are required. It is guaranteed that for each of four colors at least one light is workin...
The first and the only line contains the string *s* (4<=≤<=|*s*|<=≤<=100), which describes the garland, the *i*-th symbol of which describes the color of the *i*-th light bulb in the order from the beginning of garland: - 'R' — the light bulb is red, - 'B' — the light bulb is blue, - 'Y' — the light bulb is yellow...
In the only line print four integers *k**r*,<=*k**b*,<=*k**y*,<=*k**g* — the number of dead light bulbs of red, blue, yellow and green colors accordingly.
[ "RYBGRYBGR\n", "!RGYB\n", "!!!!YGRB\n", "!GB!RG!Y!\n" ]
[ "0 0 0 0", "0 1 0 0", "1 1 1 1", "2 1 1 0" ]
In the first example there are no dead light bulbs. In the second example it is obvious that one blue bulb is blown, because it could not be light bulbs of other colors on its place according to the statements.
[ { "input": "RYBGRYBGR", "output": "0 0 0 0" }, { "input": "!RGYB", "output": "0 1 0 0" }, { "input": "!!!!YGRB", "output": "1 1 1 1" }, { "input": "!GB!RG!Y!", "output": "2 1 1 0" }, { "input": "RYBG", "output": "0 0 0 0" }, { "input": "!Y!!!Y!!G!!!G!!...
46
4,505,600
3
20,541
144
Meeting
[ "implementation" ]
null
null
The Super Duper Secret Meeting of the Super Duper Secret Military Squad takes place in a Super Duper Secret Place. The place is an infinite plane with introduced Cartesian coordinate system. The meeting table is represented as a rectangle whose sides are parallel to the coordinate axes and whose vertexes are located at...
The first input line contains coordinates of two opposite table corners *x**a*, *y**a*, *x**b*, *y**b* (*x**a*<=≠<=*x**b*,<=*y**a*<=≠<=*y**b*). The second line contains integer *n* — the number of radiators (1<=≤<=*n*<=≤<=103). Then *n* lines contain the heaters' coordinates as "*x**i* *y**i* *r**i*", the numbers are s...
Print the only number — the number of blankets you should bring.
[ "2 5 4 2\n3\n3 1 2\n5 3 1\n1 3 2\n", "5 2 6 3\n2\n6 2 2\n6 5 3\n" ]
[ "4\n", "0\n" ]
In the first sample the generals are sitting at points: (2, 2), (2, 3), (2, 4), (2, 5), (3, 2), (3, 5), (4, 2), (4, 3), (4, 4), (4, 5). Among them, 4 generals are located outside the heating range. They are the generals at points: (2, 5), (3, 5), (4, 4), (4, 5). In the second sample the generals are sitting at points:...
[ { "input": "2 5 4 2\n3\n3 1 2\n5 3 1\n1 3 2", "output": "4" }, { "input": "5 2 6 3\n2\n6 2 2\n6 5 3", "output": "0" }, { "input": "-705 595 -702 600\n1\n-589 365 261", "output": "4" }, { "input": "-555 674 -553 774\n5\n-656 128 631\n597 -220 999\n-399 793 155\n-293 -363 1000\...
2,000
204,800
0
20,574
623
Electric Charges
[ "binary search", "dp" ]
null
null
Programmer Sasha is a student at MIPT (Moscow Institute of Physics and Technology) and he needs to make a laboratory work to pass his finals. A laboratory unit is a plane with standard coordinate axes marked on it. Physicists from Moscow Institute of Physics and Technology charged the axes by large electric charges: a...
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of points marked on the plane. Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=108<=≤<=*x**i*,<=*y**i*<=≤<=108) — the coordinates of the *i*-th point. It is guaranteed that no two points coincide.
Print a single integer — the square of the minimum possible diameter of the set.
[ "3\n1 10\n1 20\n1 30\n", "2\n1 10\n10 1\n" ]
[ "0\n", "2\n" ]
In the first sample Sasha puts electrons at all points, all particles eventually fall at a single point (1, 0). In the second sample Sasha puts an electron at point (1, 10), and a proton at point (10, 1). The result is a set of two points (1, 0) and (0, 1), which has a diameter of <img align="middle" class="tex-formul...
[ { "input": "3\n1 10\n1 20\n1 30", "output": "0" }, { "input": "2\n1 10\n10 1", "output": "2" }, { "input": "10\n1 6\n2 2\n-1 9\n-8 8\n-4 10\n-10 -6\n5 -1\n-3 -7\n-4 3\n9 4", "output": "100" }, { "input": "18\n-14 -745\n87 -4611\n89 -3748\n-77 273\n-21 -4654\n-86 -5108\n-70 32...
530
20,684,800
0
20,579
375
Tree and Queries
[ "data structures", "dfs and similar", "trees" ]
null
null
You have a rooted tree consisting of *n* vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to *n*. Then we represent the color of vertex *v* as *c**v*. The tree root is a vertex with number 1. In this problem you need to answer to *m* queries. Each ...
The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=105; 1<=≤<=*m*<=≤<=105). The next line contains a sequence of integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=105). The next *n*<=-<=1 lines contain the edges of the tree. The *i*-th line contains the numbers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤...
Print *m* integers — the answers to the queries in the order the queries appear in the input.
[ "8 5\n1 2 2 3 3 2 3 3\n1 2\n1 5\n2 3\n2 4\n5 6\n5 7\n5 8\n1 2\n1 3\n1 4\n2 3\n5 3\n", "4 1\n1 2 3 4\n1 2\n2 3\n3 4\n1 1\n" ]
[ "2\n2\n1\n0\n1\n", "4\n" ]
A subtree of vertex *v* in a rooted tree with root *r* is a set of vertices {*u* : *dist*(*r*, *v*) + *dist*(*v*, *u*) = *dist*(*r*, *u*)}. Where *dist*(*x*, *y*) is the length (in edges) of the shortest path between vertices *x* and *y*.
[ { "input": "8 5\n1 2 2 3 3 2 3 3\n1 2\n1 5\n2 3\n2 4\n5 6\n5 7\n5 8\n1 2\n1 3\n1 4\n2 3\n5 3", "output": "2\n2\n1\n0\n1" }, { "input": "4 1\n1 2 3 4\n1 2\n2 3\n3 4\n1 1", "output": "4" } ]
1,000
5,120,000
0
20,632
518
Anya and Smartphone
[ "constructive algorithms", "data structures", "implementation" ]
null
null
Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly *n* applications, each application has its own icon. The icons are located on different screens, one screen contains *k* icons. The icons from the first to the *k*-th one are located on the first screen, from the (*k*<...
The first line of the input contains three numbers *n*,<=*m*,<=*k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=105) — the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen. The next line contains *n* integers, permutation...
Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order.
[ "8 3 3\n1 2 3 4 5 6 7 8\n7 8 1\n", "5 4 2\n3 1 5 2 4\n4 4 4 4\n" ]
[ "7\n", "8\n" ]
In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8. After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch ...
[ { "input": "8 3 3\n1 2 3 4 5 6 7 8\n7 8 1", "output": "7" }, { "input": "5 4 2\n3 1 5 2 4\n4 4 4 4", "output": "8" }, { "input": "10 10 3\n1 2 3 4 5 6 7 8 9 10\n2 3 4 5 6 7 8 9 10 1", "output": "25" }, { "input": "10 12 3\n6 1 2 9 3 10 8 5 7 4\n3 9 9 4 8 2 3 8 10 8 3 4", ...
358
15,769,600
3
20,639
374
Inna and Dima
[ "dfs and similar", "dp", "graphs", "implementation" ]
null
null
Inna and Dima bought a table of size *n*<=×<=*m* in the shop. Each cell of the table contains a single letter: "D", "I", "M", "A". Inna loves Dima, so she wants to go through his name as many times as possible as she moves through the table. For that, Inna acts as follows: 1. initially, Inna chooses some cell of the...
The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=103). Then follow *n* lines that describe Inna and Dima's table. Each line contains *m* characters. Each character is one of the following four characters: "D", "I", "M", "A". Note that it is not guaranteed that the table contains at...
If Inna cannot go through name DIMA once, print on a single line "Poor Dima!" without the quotes. If there is the infinite number of names DIMA Inna can go through, print "Poor Inna!" without the quotes. Otherwise print a single integer — the maximum number of times Inna can go through name DIMA.
[ "1 2\nDI\n", "2 2\nMA\nID\n", "5 5\nDIMAD\nDIMAI\nDIMAM\nDDMAA\nAAMID\n" ]
[ "Poor Dima!\n", "Poor Inna!\n", "4\n" ]
Notes to the samples: In the first test sample, Inna cannot go through name DIMA a single time. In the second test sample, Inna can go through the infinite number of words DIMA. For that, she should move in the clockwise direction starting from the lower right corner. In the third test sample the best strategy is to...
[ { "input": "1 2\nDI", "output": "Poor Dima!" }, { "input": "2 2\nMA\nID", "output": "Poor Inna!" }, { "input": "5 5\nDIMAD\nDIMAI\nDIMAM\nDDMAA\nAAMID", "output": "4" }, { "input": "1 1\nI", "output": "Poor Dima!" }, { "input": "5 5\nDIMAD\nADDDI\nMDDDM\nIDDDA\nDA...
0
0
-1
20,657
757
Felicity is Coming!
[ "data structures", "hashing", "sortings", "strings" ]
null
null
It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has *n* gyms. The *i*-th gym has *g**i* Pokemon in it. There are *m* distinct Pokemon types in the Himalayan region numbered from 1 to *m*. There is a special evolution camp...
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=105, 1<=≤<=*m*<=≤<=106) — the number of gyms and the number of Pokemon types. The next *n* lines contain the description of Pokemons in the gyms. The *i*-th of these lines begins with the integer *g**i* (1<=≤<=*g**i*<=≤<=105) — the number of Pokemon in th...
Output the number of valid evolution plans modulo 109<=+<=7.
[ "2 3\n2 1 2\n2 2 3\n", "1 3\n3 1 2 3\n", "2 4\n2 1 2\n3 2 3 4\n", "2 2\n3 2 2 1\n2 1 2\n", "3 7\n2 1 2\n2 3 4\n3 5 6 7\n" ]
[ "1\n", "6\n", "2\n", "1\n", "24\n" ]
In the first case, the only possible evolution plan is: In the second case, any permutation of (1,  2,  3) is valid. In the third case, there are two possible plans: In the fourth case, the only possible evolution plan is:
[ { "input": "2 3\n2 1 2\n2 2 3", "output": "1" }, { "input": "1 3\n3 1 2 3", "output": "6" }, { "input": "2 4\n2 1 2\n3 2 3 4", "output": "2" }, { "input": "2 2\n3 2 2 1\n2 1 2", "output": "1" }, { "input": "3 7\n2 1 2\n2 3 4\n3 5 6 7", "output": "24" }, { ...
1,372
111,001,600
-1
20,682
392
Three Arrays
[ "data structures" ]
null
null
There are three arrays *a*, *b* and *c*. Each of them consists of *n* integers. SmallY wants to find three integers *u*, *v*, *w* (0<=≤<=*u*,<=*v*,<=*w*<=≤<=*n*) such that the following condition holds: each number that appears in the union of *a*, *b* and *c*, appears either in the first *u* elements of *a*, or in the...
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* — array *a*. The third line contains the description of array *b* in the same format. The fourth line contains the description of array *c* in the same format. The following...
Print a single integer — the minimum possible sum of *u*<=+<=*v*<=+<=*w*.
[ "3\n1 1 101\n1 2 1\n3 2 1\n", "5\n1 1 2 2 3\n2 2 4 3 3\n3 3 1 1 1\n" ]
[ "5", "5" ]
In the first example you should choose *u* = 3, *v* = 0, *w* = 2. In the second example you should choose *u* = 1, *v* = 3, *w* = 1.
[]
109
0
0
20,760
916
Jamie and Interesting Graph
[ "constructive algorithms", "graphs", "shortest paths" ]
null
null
Jamie has recently found undirected weighted graphs with the following properties very interesting: - The graph is connected and contains exactly *n* vertices and *m* edges. - All edge weights are integers and are in range [1,<=109] inclusive. - The length of shortest path from 1 to *n* is a prime number. - The su...
First line of input contains 2 integers *n*, *m*  — the required number of vertices and edges.
In the first line output 2 integers *sp*, *mstw* (1<=≤<=*sp*,<=*mstw*<=≤<=1014) — the length of the shortest path and the sum of edges' weights in the minimum spanning tree. In the next *m* lines output the edges of the graph. In each line output 3 integers *u*, *v*, *w* (1<=≤<=*u*,<=*v*<=≤<=*n*,<=1<=≤<=*w*<=≤<=109) d...
[ "4 4\n", "5 4\n" ]
[ "7 7\n1 2 3\n2 3 2\n3 4 2\n2 4 4\n", "7 13\n1 2 2\n1 3 4\n1 4 3\n4 5 4\n" ]
The graph of sample 1: <img class="tex-graphics" src="https://espresso.codeforces.com/42f9750de41b0d9a6b21e8615170113cfe19b0f2.png" style="max-width: 100.0%;max-height: 100.0%;"/> Shortest path sequence: {1, 2, 3, 4}. MST edges are marked with an asterisk (*). Definition of terms used in the problem statement: A shor...
[ { "input": "4 4", "output": "100003 100003\n1 2 100001\n2 3 1\n3 4 1\n1 3 1000000000" }, { "input": "5 4", "output": "100003 100003\n1 2 100000\n2 3 1\n3 4 1\n4 5 1" }, { "input": "2 1", "output": "100003 100003\n1 2 100003" }, { "input": "10 19", "output": "100003 100003...
30
5,632,000
0
20,802
116
Little Pigs and Wolves
[ "greedy", "implementation" ]
null
null
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size *n*<=×<=*m*. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afra...
The first line contains integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow *n* lines containing *m* characters each — that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little...
Print a single number — the maximal number of little pigs that may be eaten by the wolves.
[ "2 3\nPPW\nW.P\n", "3 3\nP.W\n.P.\nW.P\n" ]
[ "2\n", "0\n" ]
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
[ { "input": "2 3\nPPW\nW.P", "output": "2" }, { "input": "3 3\nP.W\n.P.\nW.P", "output": "0" }, { "input": "1 1\nP", "output": "0" }, { "input": "2 6\nWW..WW\n.PPPP.", "output": "2" }, { "input": "6 2\n.W\n.W\n.P\nWP\n.P\nPW", "output": "3" }, { "input"...
124
0
3
20,827
74
Train
[ "dp", "games", "greedy" ]
B. Train
2
256
A stowaway and a controller play the following game. The train is represented by *n* wagons which are numbered with positive integers from 1 to *n* from the head to the tail. The stowaway and the controller are initially in some two different wagons. Every minute the train can be in one of two conditions — moving or ...
The first line contains three integers *n*, *m* and *k*. They represent the number of wagons in the train, the stowaway's and the controller's initial positions correspondingly (2<=≤<=*n*<=≤<=50, 1<=≤<=*m*,<=*k*<=≤<=*n*, *m*<=≠<=*k*). The second line contains the direction in which a controller moves. "to head" means ...
If the stowaway wins, print "Stowaway" without quotes. Otherwise, print "Controller" again without quotes, then, separated by a space, print the number of a minute, at which the stowaway will be caught.
[ "5 3 2\nto head\n0001001\n", "3 2 1\nto tail\n0001\n" ]
[ "Stowaway", "Controller 2" ]
none
[ { "input": "5 3 2\nto head\n0001001", "output": "Stowaway" }, { "input": "3 2 1\nto tail\n0001", "output": "Controller 2" }, { "input": "4 2 1\nto tail\n1000001", "output": "Controller 6" }, { "input": "2 1 2\nto head\n111111", "output": "Stowaway" }, { "input": "...
92
0
3.977
20,903
906
Reverses
[ "dp", "string suffix structures", "strings" ]
null
null
Hurricane came to Berland and to suburbs Stringsvill. You are going to it to check if it's all right with you favorite string. Hurrinace broke it a bit by reversing some of its non-intersecting substrings. You have a photo of this string before hurricane and you want to restore it to original state using reversing mini...
First line of input contains string *s* and second line contains string *t*. Both strings have same length and consist of lowercase English letters. 1<=≤<=|*s*|<==<=|*t*|<=≤<=5·105
In first line print *k* — minimum number of substrings you should reverse. Next output *k* lines. Each line should contain two integers *l**i*, *r**i* meaning that you should reverse substring from symbol number *l**i* to symbol *r**i* (strings are 1-indexed). These substrings shouldn't intersect. If there are multiple...
[ "abcxxxdef\ncbaxxxfed\n" ]
[ "2\n7 9\n1 3\n" ]
none
[]
30
0
0
20,974
986
Perfect Encoding
[ "fft", "math" ]
null
null
You are working as an analyst in a company working on a new system for big data storage. This system will store $n$ different objects. Each object should have a unique ID. To create the system, you choose the parameters of the system — integers $m \ge 1$ and $b_{1}, b_{2}, \ldots, b_{m}$. With these parameters an ID o...
In the only line of input there is one positive integer $n$. The length of the decimal representation of $n$ is no greater than $1.5 \cdot 10^{6}$. The integer does not contain leading zeros.
Print one number — minimal value of $\sum_{i=1}^{m} b_{i}$.
[ "36\n", "37\n", "12345678901234567890123456789\n" ]
[ "10\n", "11\n", "177\n" ]
none
[ { "input": "36", "output": "10" }, { "input": "37", "output": "11" }, { "input": "12345678901234567890123456789", "output": "177" }, { "input": "1", "output": "1" }, { "input": "2", "output": "2" }, { "input": "3", "output": "3" }, { "input...
2,000
21,401,600
0
21,046
626
Group Projects
[ "dp" ]
null
null
There are *n* students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the *i*-th student *a**i* minutes to finish his/her independent piece. If students work at differen...
The first line contains two space-separated integers *n* and *k* (1<=≤<=*n*<=≤<=200, 0<=≤<=*k*<=≤<=1000) — the number of students and the maximum total imbalance allowed, respectively. The second line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=500) — the time it takes the *i*-th student to complete...
Print a single integer, the number of ways the students can form groups. As the answer may be large, print its value modulo 109<=+<=7.
[ "3 2\n2 4 5\n", "4 3\n7 8 9 10\n", "4 0\n5 10 20 21\n" ]
[ "3\n", "13\n", "1\n" ]
In the first sample, we have three options: - The first and second students form a group, and the third student forms a group. Total imbalance is 2 + 0 = 2. - The first student forms a group, and the second and third students form a group. Total imbalance is 0 + 1 = 1. - All three students form their own groups. T...
[ { "input": "3 2\n2 4 5", "output": "3" }, { "input": "4 3\n7 8 9 10", "output": "13" }, { "input": "4 0\n5 10 20 21", "output": "1" }, { "input": "20 1000\n50 50 100 100 150 150 200 200 250 250 300 300 350 350 400 400 450 450 500 500", "output": "97456952" }, { "i...
1,762
25,190,400
3
21,065
786
Till I Collapse
[ "data structures", "divide and conquer" ]
null
null
Rick and Morty want to find MR. PBH and they can't do it alone. So they need of Mr. Meeseeks. They Have generated *n* Mr. Meeseeks, standing in a line numbered from 1 to *n*. Each of them has his own color. *i*-th Mr. Meeseeks' color is *a**i*. Rick and Morty are gathering their army and they want to divide Mr. Meese...
The first line of input contains a single integer *n* (1<=≤<=*n*<=≤<=105) — number of Mr. Meeseeks. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* separated by spaces (1<=≤<=*a**i*<=≤<=*n*) — colors of Mr. Meeseeks in order they standing in a line.
In the first and only line of input print *n* integers separated by spaces. *i*-th integer should be the minimum number of presidios needed if the value of *k* is *i*.
[ "5\n1 3 4 3 3\n", "8\n1 5 7 8 1 7 6 1\n" ]
[ "4 2 1 1 1 \n", "8 4 3 2 1 1 1 1 \n" ]
For the first sample testcase, some optimal ways of dividing army into squads for each *k* are: 1. [1], [3], [4], [3, 3] 1. [1], [3, 4, 3, 3] 1. [1, 3, 4, 3, 3] 1. [1, 3, 4, 3, 3] 1. [1, 3, 4, 3, 3] For the second testcase, some optimal ways of dividing army into squads for each *k* are: 1. [1], [5], [7], [8],...
[ { "input": "5\n1 3 4 3 3", "output": "4 2 1 1 1 " }, { "input": "8\n1 5 7 8 1 7 6 1", "output": "8 4 3 2 1 1 1 1 " }, { "input": "10\n4 1 2 6 8 5 3 9 3 9", "output": "10 4 3 2 2 2 2 1 1 1 " }, { "input": "85\n23 11 69 1 49 10 7 13 66 35 81 4 51 2 62 55 31 18 85 34 59 44 20 28...
31
0
-1
21,088
543
Road Improvement
[ "dp", "trees" ]
null
null
The country has *n* cities and *n*<=-<=1 bidirectional roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from 1 to *n* inclusive. All the roads are initially bad, but the government wants to improve the state of some roads. We will ass...
The first line of the input contains a single integer *n* (2<=≤<=*n*<=≤<=2·105) — the number of cities in the country. Next line contains *n*<=-<=1 positive integers *p*2,<=*p*3,<=*p*4,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*i*<=-<=1) — the description of the roads in the country. Number *p**i* means that the country has a r...
Print *n* integers *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is the sought number of ways to improve the quality of the roads modulo 1<=000<=000<=007 (109<=+<=7), if the capital of the country is at city number *i*.
[ "3\n1 1\n", "5\n1 2 3 4\n" ]
[ "4 3 3", "5 8 9 8 5" ]
none
[ { "input": "3\n1 1", "output": "4 3 3" }, { "input": "5\n1 2 3 4", "output": "5 8 9 8 5" }, { "input": "31\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "73741817 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 5368...
514
72,294,400
3
21,108
515
Drazil and His Happy Friends
[ "brute force", "dsu", "meet-in-the-middle", "number theory" ]
null
null
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are *n* boys and *m* girls among his friends. Let's number them from 0 to *n*<=-<=1 and 0 to *m*<=-<=1 separately. In *i*-th day, Drazil invites -th ...
The first line contains two integer *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100). The second line contains integer *b* (0<=≤<=*b*<=≤<=*n*), denoting the number of happy boys among friends of Drazil, and then follow *b* distinct integers *x*1,<=*x*2,<=...,<=*x**b* (0<=≤<=*x**i*<=&lt;<=*n*), denoting the list of indices of hap...
If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".
[ "2 3\n0\n1 0\n", "2 4\n1 0\n1 2\n", "2 3\n1 0\n1 1\n" ]
[ "Yes\n", "No\n", "Yes\n" ]
By <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/169ade208e6eb4f9263c57aaff716529d59c3288.png" style="max-width: 100.0%;max-height: 100.0%;"/> we define the remainder of integer division of *i* by *k*. In first sample case: - On the 0-th day, Drazil invites 0-th boy and 0-th girl. Bec...
[ { "input": "2 3\n0\n1 0", "output": "Yes" }, { "input": "2 4\n1 0\n1 2", "output": "No" }, { "input": "2 3\n1 0\n1 1", "output": "Yes" }, { "input": "16 88\n6 5 14 2 0 12 7\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82", "outpu...
46
2,867,200
-1
21,206
0
none
[ "none" ]
null
null
In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest. V...
The first line on the input contains a single integer *n* (2<=≤<=*n*<=≤<=200<=000, *n* is even) — the number of positions available initially. The second line contains *n* distinct integers *x*1,<=*x*2,<=...,<=*x**n* (0<=≤<=*x**i*<=≤<=109), giving the coordinates of the corresponding positions.
Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.
[ "6\n0 1 3 7 15 31\n", "2\n73 37\n" ]
[ "7\n", "36\n" ]
In the first sample one of the optimum behavior of the players looks like that: 1. Vova bans the position at coordinate 15; 1. Lesha bans the position at coordinate 3; 1. Vova bans the position at coordinate 31; 1. Lesha bans the position at coordinate 1. After these actions only positions 0 and 7 will remain, an...
[ { "input": "6\n0 1 3 7 15 31", "output": "7" }, { "input": "2\n73 37", "output": "36" }, { "input": "2\n0 1000000000", "output": "1000000000" }, { "input": "8\n729541013 135019377 88372488 319157478 682081360 558614617 258129110 790518782", "output": "470242129" }, { ...
46
102,400
0
21,235
860
Arkady and a Nobody-men
[ "data structures", "dfs and similar", "trees" ]
null
null
Arkady words in a large company. There are *n* employees working in a system of a strict hierarchy. Namely, each employee, with an exception of the CEO, has exactly one immediate manager. The CEO is a manager (through a chain of immediate managers) of all employees. Each employee has an integer rank. The CEO has rank ...
The first line contains single integer *n* (1<=≤<=*n*<=≤<=5·105) — the number of employees in the company. The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=≤<=*p**i*<=≤<=*n*), where *p**i*<==<=0 if the *i*-th employee is the CEO, otherwise *p**i* equals the id of the immediate manager of the employ...
Print *n* integers — the negligibilities of all employees in the order of their ids: *z*1,<=*z*2,<=...,<=*z**n*.
[ "4\n0 1 2 1\n", "5\n2 3 4 5 0\n", "5\n0 1 1 1 3\n" ]
[ "0 2 4 2 \n", "10 6 3 1 0 \n", "0 3 3 3 5 \n" ]
Consider the first example: - The CEO has no managers, thus *z*<sub class="lower-index">1</sub> = 0. - *r*(2, 1) = 2 (employees 2 and 4 suit the conditions, employee 3 has too large rank). Thus *z*<sub class="lower-index">2</sub> = *r*(2, 1) = 2. - Similarly, *z*<sub class="lower-index">4</sub> = *r*(4, 1) = 2. - ...
[]
46
0
0
21,237
15
Triangles
[ "combinatorics", "dp" ]
E. Triangles
1
64
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has *n* level...
The input data contain the only even integer *n* (2<=≤<=*n*<=≤<=106).
Output the only number — the amount of Peter's routes modulo 1000000009.
[ "2\n", "4\n" ]
[ "10\n", "74\n" ]
none
[ { "input": "2", "output": "10" }, { "input": "4", "output": "74" }, { "input": "6", "output": "1354" }, { "input": "8", "output": "163594" }, { "input": "10", "output": "122492554" }, { "input": "966", "output": "154440215" }, { "input": "9...
92
0
-1
21,260
0
none
[ "none" ]
null
null
Поликарп мечтает стать программистом и фанатеет от степеней двойки. Среди двух чисел ему больше нравится то, которое делится на большую степень числа 2. По заданной последовательности целых положительных чисел *a*1,<=*a*2,<=...,<=*a**n* требуется найти *r* — максимальную степень числа 2, на которую делится хотя бы од...
В первой строке записано целое число *n* (1<=≤<=*n*<=≤<=100) — длина последовательности *a*. Во второй строке записана последовательность целых чисел *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
Выведите два числа: - *r* — максимальную степень двойки, на которую делится хотя бы одно из чисел заданной последовательности, - количество элементов последовательности, которые делятся на *r*.
[ "5\n80 7 16 4 48\n", "4\n21 5 3 33\n" ]
[ "16 3\n", "1 4\n" ]
В первом тестовом примере максимальная степень двойки, на которую делится хотя бы одно число, равна 16 = 2<sup class="upper-index">4</sup>, на неё делятся числа 80, 16 и 48. Во втором тестовом примере все четыре числа нечётные, поэтому делятся только на 1 = 2<sup class="upper-index">0</sup>. Это и будет максимальной с...
[ { "input": "5\n80 7 16 4 48", "output": "16 3" }, { "input": "4\n21 5 3 33", "output": "1 4" }, { "input": "10\n8 112 52 86 93 102 24 24 100 826791168", "output": "256 1" }, { "input": "3\n458297759 18 104", "output": "8 1" }, { "input": "7\n12 14 40 8 74 104 11",...
62
4,608,000
3
21,263
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...
389
307,200
3
21,264
926
Choose Place
[]
null
null
A classroom in a school has six rows with 3 desks in each row. Two people can use the same desk: one sitting on the left and one sitting on the right. Some places are already occupied, and some places are vacant. Petya has just entered the class and wants to occupy the most convenient place. The conveniences of the p...
The input consists of 6 lines. Each line describes one row of desks, starting from the closest to the blackboard. Each line is given in the following format: two characters, each is '*' or '.' — the description of the left desk in the current row; a character '-' — the aisle; two characters, each is '*' or '.' — the de...
Print the plan of the classroom after Petya takes one of the most convenient for him places. Mark this place with the letter 'P'. There should be exactly one letter 'P' in the plan. Petya can only take a vacant place. In all other places the output should coincide with the input. If there are multiple answers, print a...
[ "..-**-..\n..-**-..\n..-..-..\n..-..-..\n..-..-..\n..-..-..\n", "**-**-**\n**-**-**\n..-**-.*\n**-**-**\n..-..-..\n..-**-..\n", "**-**-*.\n*.-*.-**\n**-**-**\n**-**-**\n..-..-..\n..-**-..\n" ]
[ "..-**-..\n..-**-..\n..-..-..\n..-P.-..\n..-..-..\n..-..-..\n", "**-**-**\n**-**-**\n..-**-.*\n**-**-**\n..-P.-..\n..-**-..\n", "**-**-*.\n*.-*P-**\n**-**-**\n**-**-**\n..-..-..\n..-**-..\n" ]
In the first example the maximum convenience is 3. In the second example the maximum convenience is 2. In the third example the maximum convenience is 4.
[ { "input": "..-**-..\n..-**-..\n..-..-..\n..-..-..\n..-..-..\n..-..-..", "output": "..-**-..\n..-**-..\n..-..-..\n..-P.-..\n..-..-..\n..-..-.." }, { "input": "**-**-**\n**-**-**\n..-**-.*\n**-**-**\n..-..-..\n..-**-..", "output": "**-**-**\n**-**-**\n..-**-.*\n**-**-**\n..-P.-..\n..-**-.." }, ...
93
7,065,600
3
21,291
835
Palindromic characteristics
[ "brute force", "dp", "hashing", "strings" ]
null
null
Palindromic characteristics of string *s* with length |*s*| is a sequence of |*s*| integers, where *k*-th number is the total number of non-empty substrings of *s* which are *k*-palindromes. A string is 1-palindrome if and only if it reads the same backward as forward. A string is *k*-palindrome (*k*<=&gt;<=1) if and...
The first line contains the string *s* (1<=≤<=|*s*|<=≤<=5000) consisting of lowercase English letters.
Print |*s*| integers — palindromic characteristics of string *s*.
[ "abba\n", "abacaba\n" ]
[ "6 1 0 0 \n", "12 4 1 0 0 0 0 \n" ]
In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here.
[ { "input": "abba", "output": "6 1 0 0 " }, { "input": "abacaba", "output": "12 4 1 0 0 0 0 " }, { "input": "qqqpvmgd", "output": "11 3 0 0 0 0 0 0 " }, { "input": "wyemcafatp", "output": "11 1 0 0 0 0 0 0 0 0 " } ]
15
0
0
21,299
670
Magic Powder - 2
[ "binary search", "implementation" ]
null
null
The term of this problem is the same as the previous one, the only exception — increased restrictions.
The first line contains two positive integers *n* and *k* (1<=≤<=*n*<=≤<=100<=000,<=1<=≤<=*k*<=≤<=109) — the number of ingredients and the number of grams of the magic powder. The second line contains the sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109), where the *i*-th number is equal to the number of gram...
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
[ "1 1000000000\n1\n1000000000\n", "10 1\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n1 1 1 1 1 1 1 1 1 1\n", "3 1\n2 1 4\n11 3 16\n", "4 3\n4 3 5 6\n11 12 14 20\n" ]
[ "2000000000\n", "0\n", "4\n", "3\n" ]
none
[ { "input": "1 1000000000\n1\n1000000000", "output": "2000000000" }, { "input": "10 1\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n1 1 1 1 1 1 1 1 1 1", "output": "0" }, { "input": "3 1\n2 1 4\n11 3 16", "output": "4" ...
171
17,612,800
3
21,309
815
Karen and Test
[ "brute force", "combinatorics", "constructive algorithms", "math" ]
null
null
Karen has just arrived at school, and she has a math test today! The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points. There are *...
The first line of input contains a single integer *n* (1<=≤<=*n*<=≤<=200000), the number of numbers written on the first row. The next line contains *n* integers. Specifically, the *i*-th one among these is *a**i* (1<=≤<=*a**i*<=≤<=109), the *i*-th number on the first row.
Output a single integer on a line by itself, the number on the final row after performing the process above. Since this number can be quite large, print only the non-negative remainder after dividing it by 109<=+<=7.
[ "5\n3 6 9 12 15\n", "4\n3 7 5 2\n" ]
[ "36\n", "1000000006\n" ]
In the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15. Karen performs the operations as follows: The non-negative remainder after dividing the final number by 10<sup class="upper-index">9</sup> + 7 is still 36, so this is the correct output. In the second test case, the numbers written ...
[ { "input": "5\n3 6 9 12 15", "output": "36" }, { "input": "4\n3 7 5 2", "output": "1000000006" }, { "input": "1\n1", "output": "1" }, { "input": "16\n985629174 189232688 48695377 692426437 952164554 243460498 173956955 210310239 237322183 96515847 678847559 682240199 49879255...
249
12,902,400
-1
21,322
958
Death Stars (medium)
[ "hashing", "strings" ]
null
null
The stardate is 1983, and Princess Heidi is getting better at detecting the Death Stars. This time, two Rebel spies have yet again given Heidi two maps with the possible locations of the Death Star. Since she got rid of all double agents last time, she knows that both maps are correct, and indeed show the map of the so...
The first line of the input contains two space-separated integers *N* and *M* (1<=≤<=*N*<=≤<=2000, 1<=≤<=*M*<=≤<=200, *M*<=≤<=*N*). The next *N* lines each contain *M* lower-case Latin characters (a-z), denoting the first map. Different characters correspond to different cosmic object types. The next *M* lines each con...
The only line of the output should contain two space-separated integers *i* and *j*, denoting that the section of size *M*<=×<=*M* in the first map that starts at the *i*-th row is equal to the section of the second map that starts at the *j*-th column. Rows and columns are numbered starting from 1. If there are sever...
[ "10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nnoise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo\n" ]
[ "4 6\n" ]
The 5-by-5 grid for the first test case looks like this:
[ { "input": "10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nnoise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo", "output": "4 6" }, { "input": "1 1\ng\ng", "output": "1 1" } ]
93
8,294,400
-1
21,359
97
Leaders
[ "dfs and similar", "dsu", "graphs", "trees" ]
E. Leaders
2
256
After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly orde...
The first line contains integers *n* and *m* (1<=≤<=*n*<=≤<=105,<=0<=≤<=*m*<=≤<=105) — the number of vertices and edges in the relationship graph correspondingly. Next *m* lines contain pairs of integers *a* and *b* which mean that there is an edge between the *a*-th and the *b*-th vertices (the vertices are numbered s...
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
[ "7 7\n1 3\n1 4\n2 3\n2 4\n5 6\n6 7\n7 5\n8\n1 2\n1 3\n1 4\n2 4\n1 5\n5 6\n5 7\n6 7\n" ]
[ "No\nYes\nYes\nYes\nNo\nYes\nYes\nYes\n" ]
Notes to the samples: 1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges. 2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3. 5) Vertices 1 and 5 are located in different connected componen...
[]
218
0
-1
21,399
0
none
[ "none" ]
null
null
Vasya has *n* items lying in a line. The items are consecutively numbered by numbers from 1 to *n* in such a way that the leftmost item has number 1, the rightmost item has number *n*. Each item has a weight, the *i*-th item weights *w**i* kilograms. Vasya needs to collect all these items, however he won't do it by hi...
The first line contains five integers *n*,<=*l*,<=*r*,<=*Q**l*,<=*Q**r* (1<=≤<=*n*<=≤<=105;<=1<=≤<=*l*,<=*r*<=≤<=100;<=1<=≤<=*Q**l*,<=*Q**r*<=≤<=104). The second line contains *n* integers *w*1,<=*w*2,<=...,<=*w**n* (1<=≤<=*w**i*<=≤<=100).
In the single line print a single number — the answer to the problem.
[ "3 4 4 19 1\n42 3 99\n", "4 7 2 3 9\n1 2 3 4\n" ]
[ "576\n", "34\n" ]
Consider the first sample. As *l* = *r*, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units. The second sample. The optimal solution is to take one item from the right, then one item from the left a...
[ { "input": "3 4 4 19 1\n42 3 99", "output": "576" }, { "input": "4 7 2 3 9\n1 2 3 4", "output": "34" }, { "input": "2 100 100 10000 10000\n100 100", "output": "20000" }, { "input": "2 3 4 5 6\n1 2", "output": "11" }, { "input": "1 78 94 369 10000\n93", "output...
0
0
-1
21,419
756
Long number
[ "expression parsing", "math", "number theory" ]
null
null
Consider the following grammar: - &lt;expression&gt; ::= &lt;term&gt; | &lt;expression&gt; '+' &lt;term&gt;- &lt;term&gt; ::= &lt;number&gt; | &lt;number&gt; '-' &lt;number&gt; | &lt;number&gt; '(' &lt;expression&gt; ')'- &lt;number&gt; ::= &lt;pos_digit&gt; | &lt;number&gt; &lt;digit&gt;- &lt;digit&gt; ::= '0' | &...
The only line contains a non-empty string at most 105 characters long which is valid according to the given grammar. In particular, it means that in terms l-r *l*<=≤<=*r* holds.
Print single integer — the number described by the expression modulo 109<=+<=7.
[ "8-11\n", "2(2-4+1)+2(2(17))\n", "1234-5678\n", "1+2+3+4-5+6+7-9\n" ]
[ "891011\n", "100783079\n", "745428774\n", "123456789\n" ]
none
[ { "input": "8-11", "output": "891011" }, { "input": "2(2-4+1)+2(2(17))", "output": "100783079" }, { "input": "1234-5678", "output": "745428774" }, { "input": "1+2+3+4-5+6+7-9", "output": "123456789" }, { "input": "598777", "output": "598777" }, { "inpu...
30
0
-1
21,429
370
Berland Bingo
[ "implementation" ]
null
null
Lately, a national version of a bingo game has become very popular in Berland. There are *n* players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the *i*-th player contains *m**i* numbers. During the game the host t...
The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=100) — the number of the players. Then follow *n* lines, each line describes a player's card. The line that describes a card starts from integer *m**i* (1<=≤<=*m**i*<=≤<=100) that shows how many numbers the *i*-th player's card has. Then follows a sequence...
Print *n* lines, the *i*-th line must contain word "YES" (without the quotes), if the *i*-th player can win, and "NO" (without the quotes) otherwise.
[ "3\n1 1\n3 2 4 1\n2 10 11\n", "2\n1 1\n1 1\n" ]
[ "YES\nNO\nYES\n", "NO\nNO\n" ]
none
[ { "input": "3\n1 1\n3 2 4 1\n2 10 11", "output": "YES\nNO\nYES" }, { "input": "2\n1 1\n1 1", "output": "NO\nNO" }, { "input": "1\n1 1", "output": "YES" }, { "input": "2\n1 2\n1 3", "output": "YES\nYES" }, { "input": "2\n1 1\n2 1 2", "output": "YES\nNO" }, ...
93
0
0
21,528
0
none
[ "none" ]
null
null
You are given a sequence of positive integers *x*1,<=*x*2,<=...,<=*x**n* and two non-negative integers *a* and *b*. Your task is to transform *a* into *b*. To do that, you can perform the following moves: - subtract 1 from the current *a*; - subtract *a* mod *x**i* (1<=≤<=*i*<=≤<=*n*) from the current *a*. Operatio...
The first line contains a single integer *n* (1<=≤<=<=*n*<=≤<=105). The second line contains *n* space-separated integers *x*1,<=*x*2,<=...,<=*x**n* (2<=≤<=<=*x**i*<=≤<=109). The third line contains two integers *a* and *b* (0<=<=≤<=*b*<=≤<=<=*a*<=≤<=109, *a*<=-<=*b*<=≤<=106).
Print a single integer — the required minimum number of moves needed to transform number *a* into number *b*.
[ "3\n3 4 5\n30 17\n", "3\n5 6 7\n1000 200\n" ]
[ "6\n", "206\n" ]
none
[]
92
0
0
21,580
792
Mages and Monsters
[ "data structures", "geometry" ]
null
null
Vova plays a computer game known as Mages and Monsters. Vova's character is a mage. Though as he has just started, his character knows no spells. Vova's character can learn new spells during the game. Every spell is characterized by two values *x**i* and *y**i* — damage per second and mana cost per second, respectivel...
The first line contains two integer numbers *q* and *m* (2<=≤<=*q*<=≤<=105,<=1<=≤<=*m*<=≤<=1012) — the number of queries and the amount of mana at the beginning of every fight. *i*-th of each next *q* lines contains three numbers *k**i*, *a**i* and *b**i* (1<=≤<=*k**i*<=≤<=2,<=1<=≤<=*a**i*,<=*b**i*<=≤<=106). Using t...
For every query of second type print YES if Vova is able to win the fight with corresponding monster and NO otherwise.
[ "3 100\n1 4 9\n2 19 49\n2 19 49\n" ]
[ "YES\nNO\n" ]
In first example Vova's character at first learns the spell with 5 damage and 10 mana cost per second. Next query is a fight with monster which can kill character in 20 seconds and has 50 health points. Vova kills it in 10 seconds (spending 100 mana). Next monster has 52 health, so Vova can't deal that much damage with...
[ { "input": "3 100\n1 4 9\n2 19 49\n2 19 49", "output": "YES\nNO" }, { "input": "10 442006988299\n2 10 47\n1 9 83\n1 15 24\n2 19 47\n2 75 99\n2 85 23\n2 8 33\n2 9 82\n1 86 49\n2 71 49", "output": "NO\nYES\nYES\nYES\nYES\nYES\nYES" }, { "input": "2 424978864039\n2 7 3\n2 10 8", "output...
873
15,564,800
3
21,586
0
none
[ "none" ]
null
null
...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu... Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super...
The first line contains two integers — the number of vertices *n* and the number of edges *m* of the graph (1<=≤<=*n*<=≤<=100, 0<=≤<=*m*<=≤<=). Each of the following *m* lines contains a pair of integers *x* and *y*, that show that an edge exists between vertices *x* and *y* (1<=≤<=*x*,<=*y*<=≤<=*n*,<=*x*<=≠<=*y*). Fo...
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
[ "6 6\n6 3\n6 4\n5 1\n2 5\n1 4\n5 4\n", "6 5\n5 6\n4 6\n3 1\n5 1\n1 2\n" ]
[ "FHTAGN!", "NO" ]
Let us denote as a simple cycle a set of *v* vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., *v* - 1 and *v*, *v* and 1. A tree is a connected undirected graph consisting of *n* vertices and *n* - 1 edges (*n* &gt; 0). A rooted tree is a tree where one ve...
[ { "input": "6 6\n6 3\n6 4\n5 1\n2 5\n1 4\n5 4", "output": "FHTAGN!" }, { "input": "6 5\n5 6\n4 6\n3 1\n5 1\n1 2", "output": "NO" }, { "input": "10 10\n4 10\n8 5\n2 8\n4 9\n9 3\n2 7\n10 6\n10 2\n9 8\n1 8", "output": "FHTAGN!" }, { "input": "5 4\n1 5\n1 3\n1 4\n3 2", "outpu...
248
2,048,000
3
21,614
387
George and Number
[ "greedy", "implementation" ]
null
null
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers *b*. During the game, George modifies the array by using special changes. Let's mark George's current array as *b*1,<=*b*2,<=...,<=*b*|*b*| (record |*b*| denotes the current length of the array). Then one chang...
The first line of the input contains a single integer *p* (1<=≤<=*p*<=&lt;<=10100000). It is guaranteed that number *p* doesn't contain any leading zeroes.
Print an integer — the maximum number of elements array *b* could contain originally.
[ "9555\n", "10000000005\n", "800101\n", "45\n", "1000000000000001223300003342220044555\n", "19992000\n", "310200\n" ]
[ "4", "2", "3", "1", "17", "1", "2" ]
Let's consider the test examples: - Originally array *b* can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}. - Originally array *b* could be equal to {1000000000, 5}. Please note that the array *b* cannot contain zeros. - Originally array *...
[ { "input": "9555", "output": "4" }, { "input": "10000000005", "output": "2" }, { "input": "800101", "output": "3" }, { "input": "45", "output": "1" }, { "input": "1000000000000001223300003342220044555", "output": "17" }, { "input": "19992000", "out...
124
0
0
21,642
80
Depression
[ "geometry", "math" ]
B. Depression
1
256
Do you remember a kind cartoon "Beauty and the Beast"? No, no, there was no firing from machine guns or radiation mutants time-travels! There was a beauty named Belle. Once she had violated the Beast's order and visited the West Wing. After that she was banished from the castle... Everybody was upset. The beautiful ...
The only line of input contains current time according to the digital clock, formatted as HH:MM (00<=≤<=HH<=≤<=23, 00<=≤<=MM<=≤<=59). The mantel clock initially shows 12:00. Pretests contain times of the beginning of some morning TV programs of the Channel One Russia.
Print two numbers *x* and *y* — the angles of turning the hour and minute hands, respectively (0<=≤<=*x*,<=*y*<=&lt;<=360). The absolute or relative error in the answer should not exceed 10<=-<=9.
[ "12:00\n", "04:30\n", "08:17\n" ]
[ "0 0", "135 180", "248.5 102" ]
A note to the second example: the hour hand will be positioned exactly in the middle, between 4 and 5.
[ { "input": "12:00", "output": "0 0" }, { "input": "04:30", "output": "135 180" }, { "input": "08:17", "output": "248.5 102" }, { "input": "07:20", "output": "220 120" }, { "input": "09:55", "output": "297.5 330" }, { "input": "11:59", "output": "35...
46
0
3.977
21,656
258
Little Elephant and LCM
[ "binary search", "combinatorics", "dp", "math" ]
null
null
The Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The result of the LCM operation of *k* positive integers *x*1,<=*x*2,<=...,<=*x**k* is the minimum positive integer that is divisible by each of numbers *x**i*. Let's assume that there is a sequence of integers...
The first line contains a single positive integer *n* (1<=≤<=*n*<=≤<=105) — the number of integers in the sequence *a*. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=105) — sequence *a*.
In the single line print a single integer — the answer to the problem modulo 1000000007 (109<=+<=7).
[ "4\n1 4 3 2\n", "2\n6 3\n" ]
[ "15\n", "13\n" ]
none
[ { "input": "4\n1 4 3 2", "output": "15" }, { "input": "2\n6 3", "output": "13" }, { "input": "7\n1 2 1 2 3 4 1", "output": "27" }, { "input": "4\n6 7 2 3", "output": "89" }, { "input": "7\n2 1 1 1 2 2 2", "output": "16" }, { "input": "7\n7 1 9 9 10 4 4...
4,000
19,251,200
0
21,675
617
Chocolate
[ "combinatorics" ]
null
null
Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would contain exactly one nut and any break line goes between two adjacent pieces. You are asked to calculate the number of ways he can...
The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=100) — the number of pieces in the chocolate bar. The second line contains *n* integers *a**i* (0<=≤<=*a**i*<=≤<=1), where 0 represents a piece without the nut and 1 stands for a piece with the nut.
Print the number of ways to break the chocolate into multiple parts so that each part would contain exactly one nut.
[ "3\n0 1 0\n", "5\n1 0 1 0 1\n" ]
[ "1\n", "4\n" ]
In the first sample there is exactly one nut, so the number of ways equals 1 — Bob shouldn't make any breaks. In the second sample you can break the bar in four ways: 10|10|1 1|010|1 10|1|01 1|01|01
[ { "input": "3\n0 1 0", "output": "1" }, { "input": "5\n1 0 1 0 1", "output": "4" }, { "input": "10\n0 0 1 0 0 0 1 1 0 1", "output": "8" }, { "input": "20\n0 0 0 0 1 1 1 0 1 0 0 1 0 1 1 0 1 1 1 0", "output": "24" }, { "input": "50\n0 1 1 1 1 1 1 0 1 1 0 1 1 1 0 0 1...
124
1,638,400
3
21,716
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" }, {...
124
0
0
21,744
710
Two Arithmetic Progressions
[ "math", "number theory" ]
null
null
You are given two arithmetic progressions: *a*1*k*<=+<=*b*1 and *a*2*l*<=+<=*b*2. Find the number of integers *x* such that *L*<=≤<=*x*<=≤<=*R* and *x*<==<=*a*1*k*'<=+<=*b*1<==<=*a*2*l*'<=+<=*b*2, for some integers *k*',<=*l*'<=≥<=0.
The only line contains six integers *a*1,<=*b*1,<=*a*2,<=*b*2,<=*L*,<=*R* (0<=&lt;<=*a*1,<=*a*2<=≤<=2·109,<=<=-<=2·109<=≤<=*b*1,<=*b*2,<=*L*,<=*R*<=≤<=2·109,<=*L*<=≤<=*R*).
Print the desired number of integers *x*.
[ "2 0 3 3 5 21\n", "2 4 3 0 6 17\n" ]
[ "3\n", "2\n" ]
none
[ { "input": "2 0 3 3 5 21", "output": "3" }, { "input": "2 4 3 0 6 17", "output": "2" }, { "input": "2 0 4 2 -39 -37", "output": "0" }, { "input": "1 9 3 11 49 109", "output": "20" }, { "input": "3 81 5 72 -1761 501", "output": "28" }, { "input": "8 -89...
62
0
0
21,745
161
Discounts
[ "constructive algorithms", "greedy", "sortings" ]
null
null
One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheape...
The first input line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=103) — the number of items in the supermarket and the number of carts, correspondingly. Next *n* lines describe the items as "*c**i* *t**i*" (without the quotes), where *c**i* (1<=≤<=*c**i*<=≤<=109) is an integer denoting the price of the *i*...
In the first line print a single real number with exactly one decimal place — the minimum total price of the items, including the discounts. In the following *k* lines print the descriptions of the items in the carts. In the *i*-th line print the description of the *i*-th cart as "*t* *b*1 *b*2 ... *b**t*" (without t...
[ "3 2\n2 1\n3 2\n3 1\n", "4 3\n4 1\n1 2\n2 2\n3 2\n" ]
[ "5.5\n2 1 2\n1 3\n", "8.0\n1 1\n2 4 2\n1 3\n" ]
In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2·0.5 + (3 + 3·0.5) = 1 + 4.5 = 5.5.
[ { "input": "3 2\n2 1\n3 2\n3 1", "output": "5.5\n2 1 2\n1 3" }, { "input": "4 3\n4 1\n1 2\n2 2\n3 2", "output": "8.0\n1 1\n1 2\n2 3 4" }, { "input": "1 1\n1 1", "output": "0.5\n1 1" }, { "input": "1 1\n1 2", "output": "1.0\n1 1" }, { "input": "10 1\n1 1\n2 2\n1 1\...
248
20,172,800
0
21,788
518
Arthur and Questions
[ "greedy", "implementation", "math", "ternary search" ]
null
null
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length *n* (*a*1,<=*a*2,<=...,<=*a**n*), consisting of integers and integer *k*, not exceeding *n*. This sequence had the following property: if you write out the sums of all its segments consisting of *k* consecutive elements ...
The first line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively. The next line contains *n* space-separated elements *a**i* (1<=≤<=*i*<=≤<=*n*). If *a**i* <==<= ?, then the *i*-th element of Arthur's sequence was ...
If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes). Otherwise, print *n* integers — Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum |*a**i*|, where |*a*...
[ "3 2\n? 1 2\n", "5 1\n-10 -9 ? -7 -6\n", "5 3\n4 6 7 2 9\n" ]
[ "0 1 2 \n", "-10 -9 -8 -7 -6 \n", "Incorrect sequence\n" ]
none
[ { "input": "3 2\n? 1 2", "output": "0 1 2 " }, { "input": "5 1\n-10 -9 ? -7 -6", "output": "-10 -9 -8 -7 -6 " }, { "input": "5 3\n4 6 7 2 9", "output": "Incorrect sequence" }, { "input": "9 3\n? ? ? ? ? ? ? ? ?", "output": "-1 -1 -1 0 0 0 1 1 1 " }, { "input": "5 ...
217
12,595,200
3
21,852
453
Little Pony and Harmony Chest
[ "bitmasks", "brute force", "dp" ]
null
null
Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony. A sequence of positive integers *b**i* is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony se...
The first line contains an integer *n* (1<=≤<=*n*<=≤<=100) — the number of elements of the sequences *a* and *b*. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=30).
Output the key — sequence *b**i* that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.
[ "5\n1 1 1 1 1\n", "5\n1 6 4 2 8\n" ]
[ "1 1 1 1 1 ", "1 5 3 1 8 " ]
none
[ { "input": "5\n1 1 1 1 1", "output": "1 1 1 1 1 " }, { "input": "5\n1 6 4 2 8", "output": "1 5 3 1 8 " }, { "input": "10\n16 3 16 10 12 5 14 14 15 27", "output": "19 1 17 7 11 1 16 13 15 29 " }, { "input": "10\n8 7 11 5 17 24 28 18 7 8", "output": "9 7 11 5 17 23 29 19 1 ...
0
0
-1
21,878
556
Case of Fake Numbers
[ "brute force", "implementation" ]
null
null
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was. Its most important components are a button and a line of *n* similar gears. Each gear has *n* teeth containing all numbe...
The first line contains integer *n* (1<=≤<=*n*<=≤<=1000) — the number of gears. The second line contains *n* digits *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=*n*<=-<=1) — the sequence of active teeth: the active tooth of the *i*-th gear contains number *a**i*.
In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise.
[ "3\n1 0 0\n", "5\n4 2 1 4 3\n", "4\n0 2 3 1\n" ]
[ "Yes\n", "Yes\n", "No\n" ]
In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.
[ { "input": "3\n1 0 0", "output": "Yes" }, { "input": "5\n4 2 1 4 3", "output": "Yes" }, { "input": "4\n0 2 3 1", "output": "No" }, { "input": "1\n0", "output": "Yes" }, { "input": "2\n1 0", "output": "Yes" }, { "input": "5\n2 4 4 1 1", "output": "Y...
15
0
0
21,943
149
Division into Teams
[ "greedy", "math", "sortings" ]
null
null
Petya loves football very much, especially when his parents aren't home. Each morning he comes to the yard, gathers his friends and they play all day. From time to time they have a break to have some food or do some chores (for example, water the flowers). The key in football is to divide into teams fairly before the ...
The first line contains the only integer *n* (2<=≤<=*n*<=≤<=105) which represents the number of guys in the yard. The next line contains *n* positive space-separated integers, *a**i* (1<=≤<=*a**i*<=≤<=104), the *i*-th number represents the *i*-th boy's playing skills.
On the first line print an integer *x* — the number of boys playing for the first team. On the second line print *x* integers — the individual numbers of boys playing for the first team. On the third line print an integer *y* — the number of boys playing for the second team, on the fourth line print *y* integers — the ...
[ "3\n1 2 1\n", "5\n2 3 3 1 1\n" ]
[ "2\n1 2 \n1\n3 \n", "3\n4 1 3 \n2\n5 2 \n" ]
Let's consider the first sample test. There we send the first and the second boy to the first team and the third boy to the second team. Let's check all three conditions of a fair division. The first limitation is fulfilled (all boys play), the second limitation on the sizes of groups (|2 - 1| = 1 ≤ 1) is fulfilled, th...
[ { "input": "3\n1 2 1", "output": "2\n1 2 \n1\n3 " }, { "input": "5\n2 3 3 1 1", "output": "3\n4 1 3 \n2\n5 2 " }, { "input": "10\n2 2 2 2 2 2 2 1 2 2", "output": "5\n8 2 4 6 9 \n5\n1 3 5 7 10 " }, { "input": "10\n2 3 3 1 3 1 1 1 2 2", "output": "5\n4 7 1 10 3 \n5\n6 8 9 2...
0
0
-1
21,948
630
Area of a Star
[ "geometry" ]
null
null
It was decided in IT City to distinguish successes of local IT companies by awards in the form of stars covered with gold from one side. To order the stars it is necessary to estimate order cost that depends on the area of gold-plating. Write a program that can calculate the area of a star. A "star" figure having *n*<...
The only line of the input contains two integers *n* (5<=≤<=*n*<=&lt;<=109, *n* is prime) and *r* (1<=≤<=*r*<=≤<=109) — the number of the star corners and the radius of the circumcircle correspondingly.
Output one number — the star area. The relative error of your answer should not be greater than 10<=-<=7.
[ "7 10\n" ]
[ "108.395919545675" ]
none
[ { "input": "7 10", "output": "108.395919545675" }, { "input": "5 1", "output": "1.122569941449" }, { "input": "7 1000", "output": "1083959.195456745256" }, { "input": "11 1000000000", "output": "1061689977712182980.125000000000" }, { "input": "999999937 1", "o...
61
0
0
22,035
0
none
[ "none" ]
null
null
You are an assistant director in a new musical play. The play consists of *n* musical parts, each part must be performed by exactly one actor. After the casting the director chose *m* actors who can take part in the play. Your task is to assign the parts to actors. However, there are several limitations. First, each a...
The first line contains a single integer *n* — the number of parts in the play (1<=≤<=*n*<=≤<=105). Next *n* lines contain two space-separated integers each, *a**j* and *b**j* — the range of notes for the *j*-th part (1<=≤<=*a**j*<=≤<=*b**j*<=≤<=109). The next line contains a single integer *m* — the number of actors...
If there is an assignment that meets all the criteria aboce, print a single word "YES" (without the quotes) in the first line. In the next line print *n* space-separated integers. The *i*-th integer should be the number of the actor who should perform the *i*-th part. If there are multiple correct assignments, print a...
[ "3\n1 3\n2 4\n3 5\n2\n1 4 2\n2 5 1\n", "3\n1 3\n2 4\n3 5\n2\n1 3 2\n2 5 1\n" ]
[ "YES\n1 1 2\n", "NO\n" ]
none
[]
124
307,200
0
22,054
436
Feed with Candy
[ "greedy" ]
null
null
The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is the hero of today's problem. One day, Om Nom visited his friend Evan. Evan has *n* candies of two types (fruit drops and caramel drops), the *i*-th candy hangs at the height of *h**i* centimeters ab...
The first line contains two integers, *n* and *x* (1<=≤<=*n*,<=*x*<=≤<=2000) — the number of sweets Evan has and the initial height of Om Nom's jump. Each of the following *n* lines contains three integers *t**i*,<=*h**i*,<=*m**i* (0<=≤<=*t**i*<=≤<=1; 1<=≤<=*h**i*,<=*m**i*<=≤<=2000) — the type, height and the mass of...
Print a single integer — the maximum number of candies Om Nom can eat.
[ "5 3\n0 2 4\n1 3 1\n0 8 3\n0 20 10\n1 5 5\n" ]
[ "4\n" ]
One of the possible ways to eat 4 candies is to eat them in the order: 1, 5, 3, 2. Let's assume the following scenario: 1. Initially, the height of Om Nom's jump equals 3. He can reach candies 1 and 2. Let's assume that he eats candy 1. As the mass of this candy equals 4, the height of his jump will rise to 3 + 4 = 7...
[ { "input": "5 3\n0 2 4\n1 3 1\n0 8 3\n0 20 10\n1 5 5", "output": "4" }, { "input": "5 2\n1 15 2\n1 11 2\n0 17 2\n0 16 1\n1 18 2", "output": "0" }, { "input": "6 2\n1 17 3\n1 6 1\n0 4 2\n1 10 1\n1 7 3\n1 5 1", "output": "0" }, { "input": "7 2\n1 14 1\n1 9 2\n0 6 3\n0 20 2\n0 4...
77
0
0
22,056
748
Santa Claus and Tangerines
[ "binary search", "data structures", "greedy", "two pointers" ]
null
null
Santa Claus has *n* tangerines, and the *i*-th of them consists of exactly *a**i* slices. Santa Claus came to a school which has *k* pupils. Santa decided to treat them with tangerines. However, there can be too few tangerines to present at least one tangerine to each pupil. So Santa decided to divide tangerines into ...
The first line contains two positive integers *n* and *k* (1<=≤<=*n*<=≤<=106, 1<=≤<=*k*<=≤<=2·109) denoting the number of tangerines and the number of pupils, respectively. The second line consists of *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=107), where *a**i* stands for the number of slices ...
If there's no way to present a tangerine or a part of tangerine to everyone, print -1. Otherwise, print the maximum possible joy that Santa can have.
[ "3 2\n5 9 3\n", "2 4\n12 14\n", "2 3\n1 1\n" ]
[ "5\n", "6\n", "-1\n" ]
In the first example Santa should divide the second tangerine into two parts with 5 and 4 slices. After that he can present the part with 5 slices to the first pupil and the whole first tangerine (with 5 slices, too) to the second pupil. In the second example Santa should divide both tangerines, so that he'll be able ...
[]
46
4,608,000
0
22,071
45
Event Dates
[ "greedy", "meet-in-the-middle", "sortings" ]
D. Event Dates
2
256
On a history lesson the teacher asked Vasya to name the dates when *n* famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [*l**i*,<=*r**i*] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help...
The first line contains one integer *n* (1<=≤<=*n*<=≤<=100) — the number of known events. Then follow *n* lines containing two integers *l**i* and *r**i* each (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=107) — the earliest acceptable date and the latest acceptable date of the *i*-th event.
Print *n* numbers — the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists.
[ "3\n1 2\n2 3\n3 4\n", "2\n1 3\n1 3\n" ]
[ "1 2 3 \n", "1 2 \n" ]
none
[ { "input": "3\n1 2\n2 3\n3 4", "output": "1 2 3 " }, { "input": "2\n1 3\n1 3", "output": "1 2 " }, { "input": "10\n1 10\n3 4\n8 10\n3 4\n5 9\n1 4\n7 10\n1 9\n1 8\n4 10", "output": "7 3 10 4 6 1 9 5 2 8 " }, { "input": "10\n1 1\n8 10\n1 7\n6 8\n5 7\n1 9\n8 8\n6 10\n1 4\n3 4", ...
92
0
0
22,072
89
Fire and Ice
[ "greedy" ]
E. Fire and Ice
0
256
The Fire Lord attacked the Frost Kingdom. He has already got to the Ice Fortress, where the Snow Queen dwells. He arranged his army on a segment *n* in length not far from the city walls. And only the frost magician Solomon can save the Frost Kingdom. The *n*-long segment is located at a distance equal exactly to 1 fr...
The first line contains an integer *n* (1<=≤<=*n*<=≤<=1000). The next line contains *n* numbers, the *i*-th of them represents the strength of the fire demon standing of the *i*-th position, an integer from 1 to 100. If there's no demon on the *i*-th position, then the *i*-th number equals to 0. It is guaranteed that t...
Print a string of minimum length, containing characters "L", "R" and "A" — the succession of actions leading to the required result. If there are several possible answers, print any of them.
[ "3\n1 0 1\n", "3\n0 2 0\n" ]
[ "ARARARALLLA", "ARARALAARALA" ]
none
[ { "input": "3\n1 0 1", "output": "ARARARALLLA" }, { "input": "3\n0 2 0", "output": "ARARALAARALA" }, { "input": "5\n3 1 2 2 4", "output": "ARALAARALAARARARARARALLLAARARARALAARALAARALLLLLA" }, { "input": "4\n2 2 2 2", "output": "ARARARARALLLLAARARARARALLLLA" }, { "...
139
20,172,800
0
22,076
949
Curfew
[ "binary search", "brute force", "greedy", "sortings" ]
null
null
Instructors of Some Informatics School make students go to bed. The house contains *n* rooms, in each room exactly *b* students were supposed to sleep. However, at the time of curfew it happened that many students are not located in their assigned rooms. The rooms are arranged in a row and numbered from 1 to *n*. Init...
The first line contains three integers *n*, *d* and *b* (2<=≤<=*n*<=≤<=100<=000, 1<=≤<=*d*<=≤<=*n*<=-<=1, 1<=≤<=*b*<=≤<=10<=000), number of rooms in the house, running distance of a student, official number of students in a room. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=109), ...
Output one integer, the minimal possible value of the maximum of *x**i*.
[ "5 1 1\n1 0 0 0 4\n", "6 1 2\n3 8 0 1 0 0\n" ]
[ "1\n", "2\n" ]
In the first sample the first three rooms are processed by the first instructor, and the last two are processed by the second instructor. One of the optimal strategies is the following: firstly three students run from room 5 to room 4, on the next stage two of them run to room 3, and one of those two hides under a bed....
[ { "input": "5 1 1\n1 0 0 0 4", "output": "1" }, { "input": "6 1 2\n3 8 0 1 0 0", "output": "2" }, { "input": "5 1 1\n1 1 0 3 0", "output": "0" }, { "input": "5 1 1\n4 0 0 1 0", "output": "1" }, { "input": "2 1 1\n0 2", "output": "0" }, { "input": "100 ...
109
13,516,800
3
22,092
0
none
[ "none" ]
null
null
Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has *n* cities connected by *n*<=-<=1 undirected roads, and for any two cities there always exists a path between them. Fox Ciel needs to assign an officer to each city. Each officer has a rank — a letter from 'A' to 'Z'. So there will be 26...
The first line contains an integer *n* (2<=≤<=*n*<=≤<=105) — the number of cities in Tree Land. Each of the following *n*<=-<=1 lines contains two integers *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=*n*,<=*a*<=≠<=*b*) — they mean that there will be an undirected road between *a* and *b*. Consider all the cities are numbered fro...
If there is a valid plane, output *n* space-separated characters in a line — *i*-th character is the rank of officer in the city with number *i*. Otherwise output "Impossible!".
[ "4\n1 2\n1 3\n1 4\n", "10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n" ]
[ "A B B B\n", "D C B A D C B D C D\n" ]
In the first example, for any two officers of rank 'B', an officer with rank 'A' will be on the path between them. So it is a valid solution.
[]
1,000
27,136,000
0
22,166
469
Chat Online
[ "implementation" ]
null
null
Little X and Little Z are good friends. They always chat online. But both of them have schedules. Little Z has fixed schedule. He always online at any moment of time between *a*1 and *b*1, between *a*2 and *b*2, ..., between *a**p* and *b**p* (all borders inclusive). But the schedule of Little X is quite strange, it d...
The first line contains four space-separated integers *p*,<=*q*,<=*l*,<=*r* (1<=≤<=<=*p*,<=*q*<=≤<=50; 0<=≤<=*l*<=≤<=*r*<=≤<=1000). Each of the next *p* lines contains two space-separated integers *a**i*,<=*b**i* (0<=≤<=*a**i*<=&lt;<=*b**i*<=≤<=1000). Each of the next *q* lines contains two space-separated integers *c...
Output a single integer — the number of moments of time from the segment [*l*,<=*r*] which suit for online conversation.
[ "1 1 0 4\n2 3\n0 1\n", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17\n" ]
[ "3\n", "20\n" ]
none
[ { "input": "1 1 0 4\n2 3\n0 1", "output": "3" }, { "input": "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17", "output": "20" }, { "input": "5 2 27 452\n148 154\n421 427\n462 470\n777 786\n969 978\n245 247\n313 322", "output": "54" }, { "input": "3 6 25 785\n273 275\n391 397\n775 78...
31
0
0
22,196
443
Kolya and Tandem Repeat
[ "brute force", "implementation", "strings" ]
null
null
Kolya got string *s* for his birthday, the string consists of small English letters. He immediately added *k* more characters to the right of the string. Then Borya came and said that the new string contained a tandem repeat of length *l* as a substring. How large could *l* be? See notes for definition of a tandem re...
The first line contains *s* (1<=≤<=|*s*|<=≤<=200). This string contains only small English letters. The second line contains number *k* (1<=≤<=*k*<=≤<=200) — the number of the added characters.
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
[ "aaba\n2\n", "aaabbbb\n2\n", "abracadabra\n10\n" ]
[ "6\n", "6\n", "20\n" ]
A tandem repeat of length 2*n* is string *s*, where for any position *i* (1 ≤ *i* ≤ *n*) the following condition fulfills: *s*<sub class="lower-index">*i*</sub> = *s*<sub class="lower-index">*i* + *n*</sub>. In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabr...
[ { "input": "aaba\n2", "output": "6" }, { "input": "aaabbbb\n2", "output": "6" }, { "input": "abracadabra\n10", "output": "20" }, { "input": "zwvrx\n3", "output": "6" }, { "input": "ayi\n10", "output": "12" }, { "input": "eexlzphiay\n1", "output": "...
78
3,584,000
0
22,200
0
none
[ "none" ]
null
null
Вася купил стол, у которого *n* ножек. Каждая ножка состоит из двух частей, которые соединяются друг с другом. Каждая часть может быть произвольной положительной длины, но гарантируется, что из всех 2*n* частей возможно составить *n* ножек одинаковой длины. При составлении ножки любые две части могут быть соединены дру...
В первой строке задано число *n* (1<=≤<=*n*<=≤<=1000) — количество ножек у стола, купленного Васей. Во второй строке следует последовательность из 2*n* целых положительных чисел *a*1,<=*a*2,<=...,<=*a*2*n* (1<=≤<=*a**i*<=≤<=100<=000) — длины частей ножек стола в произвольном порядке.
Выведите *n* строк по два целых числа в каждой — длины частей ножек, которые надо соединить друг с другом. Гарантируется, что всегда возможно собрать *n* ножек одинаковой длины. Если ответов несколько, разрешается вывести любой из них.
[ "3\n1 3 2 4 5 3\n", "3\n1 1 1 2 2 2\n" ]
[ "1 5\n2 4\n3 3\n", "1 2\n2 1\n1 2\n" ]
none
[ { "input": "3\n1 3 2 4 5 3", "output": "1 5\n2 4\n3 3" }, { "input": "3\n1 1 1 2 2 2", "output": "1 2\n1 2\n1 2" }, { "input": "1\n3 7", "output": "3 7" }, { "input": "10\n9 13 18 7 18 13 2 2 5 16 3 17 5 4 18 2 15 11 7 15", "output": "2 18\n2 18\n2 18\n3 17\n4 16\n5 15\n5...
62
4,608,000
0
22,207
733
Kostya the Sculptor
[ "data structures", "hashing" ]
null
null
Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya's idea and wants to present him a rectangular parallelepiped of marble from which he can carve the sphere. Zahar has *n* stones which are rectang...
The first line contains the integer *n* (1<=≤<=*n*<=≤<=105). *n* lines follow, in the *i*-th of which there are three integers *a**i*,<=*b**i* and *c**i* (1<=≤<=*a**i*,<=*b**i*,<=*c**i*<=≤<=109) — the lengths of edges of the *i*-th stone. Note, that two stones may have exactly the same sizes, but they still will be co...
In the first line print *k* (1<=≤<=*k*<=≤<=2) the number of stones which Zahar has chosen. In the second line print *k* distinct integers from 1 to *n* — the numbers of stones which Zahar needs to choose. Consider that stones are numbered from 1 to *n* in the order as they are given in the input data. You can print th...
[ "6\n5 5 5\n3 2 4\n1 4 1\n2 1 3\n3 2 4\n3 3 4\n", "7\n10 7 8\n5 10 3\n4 2 6\n5 5 5\n10 2 8\n4 2 1\n7 7 7\n" ]
[ "1\n1\n", "2\n1 5\n" ]
In the first example we can connect the pairs of stones: - 2 and 4, the size of the parallelepiped: 3 × 2 × 5, the radius of the inscribed sphere 1 - 2 and 5, the size of the parallelepiped: 3 × 2 × 8 or 6 × 2 × 4 or 3 × 4 × 4, the radius of the inscribed sphere 1, or 1, or 1.5 respectively. - 2 and 6, the size of ...
[ { "input": "6\n5 5 5\n3 2 4\n1 4 1\n2 1 3\n3 2 4\n3 3 4", "output": "1\n1" }, { "input": "7\n10 7 8\n5 10 3\n4 2 6\n5 5 5\n10 2 8\n4 2 1\n7 7 7", "output": "2\n1 5" }, { "input": "1\n1 1 1", "output": "1\n1" }, { "input": "2\n2 3 1\n2 2 3", "output": "2\n2 1" }, { ...
124
0
0
22,229
899
Letters Removing
[ "data structures", "strings" ]
null
null
Petya has a string of length *n* consisting of small and large English letters and digits. He performs *m* operations. Each operation is described with two integers *l* and *r* and a character *c*: Petya removes from the string all characters *c* on positions between *l* and *r*, inclusive. It's obvious that the lengt...
The first string contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=2·105) — the length of the string and the number of operations. The second line contains the string of length *n*, consisting of small and large English letters and digits. Positions in the string are enumerated from 1. Each of the next *m* lines...
Print the string Petya will obtain after performing all *m* operations. If the strings becomes empty after all operations, print an empty line.
[ "4 2\nabac\n1 3 a\n2 2 c\n", "3 2\nA0z\n1 3 0\n1 1 z\n", "10 4\nagtFrgF4aF\n2 5 g\n4 9 F\n1 5 4\n1 7 a\n", "9 5\naAAaBBccD\n1 4 a\n5 6 c\n2 3 B\n4 4 D\n2 3 A\n" ]
[ "b\n", "Az\n", "tFrg4\n", "AB\n" ]
In the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b". In the second example during the first operation Petya removes '0' from the second position. After that t...
[ { "input": "4 2\nabac\n1 3 a\n2 2 c", "output": "b" }, { "input": "3 2\nA0z\n1 3 0\n1 1 z", "output": "Az" }, { "input": "10 4\nagtFrgF4aF\n2 5 g\n4 9 F\n1 5 4\n1 7 a", "output": "tFrg4" }, { "input": "9 5\naAAaBBccD\n1 4 a\n5 6 c\n2 3 B\n4 4 D\n2 3 A", "output": "AB" }...
2,000
7,680,000
0
22,298
438
The Child and Binary Tree
[ "combinatorics", "divide and conquer", "fft", "number theory" ]
null
null
Our child likes computer science very much, especially he likes binary trees. Consider the sequence of *n* distinct positive integers: *c*1,<=*c*2,<=...,<=*c**n*. The child calls a vertex-weighted rooted binary tree good if and only if for every vertex *v*, the weight of *v* is in the set {*c*1,<=*c*2,<=...,<=*c**n*}....
The first line contains two integers *n*,<=*m* (1<=≤<=*n*<=≤<=105; 1<=≤<=*m*<=≤<=105). The second line contains *n* space-separated pairwise distinct integers *c*1,<=*c*2,<=...,<=*c**n*. (1<=≤<=*c**i*<=≤<=105).
Print *m* lines, each line containing a single integer. The *i*-th line must contain the number of good vertex-weighted rooted binary trees whose weight exactly equal to *i*. Print the answers modulo 998244353 (7<=×<=17<=×<=223<=+<=1, a prime number).
[ "2 3\n1 2\n", "3 10\n9 4 3\n", "5 10\n13 10 6 4 15\n" ]
[ "1\n3\n9\n", "0\n0\n1\n1\n0\n2\n4\n2\n6\n15\n", "0\n0\n0\n1\n0\n1\n0\n2\n0\n5\n" ]
In the first example, there are 9 good vertex-weighted rooted binary trees whose weight exactly equal to 3:
[ { "input": "2 3\n1 2", "output": "1\n3\n9" }, { "input": "3 10\n9 4 3", "output": "0\n0\n1\n1\n0\n2\n4\n2\n6\n15" }, { "input": "5 10\n13 10 6 4 15", "output": "0\n0\n0\n1\n0\n1\n0\n2\n0\n5" }, { "input": "1 1\n1", "output": "1" }, { "input": "10 100\n9 95 37 56 3...
0
0
-1
22,313
939
Maximize!
[ "binary search", "greedy", "ternary search", "two pointers" ]
null
null
You are given a multiset *S* consisting of positive integers (initially empty). There are two kind of queries: 1. Add a positive integer to *S*, the newly added integer is not less than any number in it. 1. Find a subset *s* of the set *S* such that the value is maximum possible. Here *max*(*s*) means maximum valu...
The first line contains a single integer *Q* (1<=≤<=*Q*<=≤<=5·105) — the number of queries. Each of the next *Q* lines contains a description of query. For queries of type 1 two integers 1 and *x* are given, where *x* (1<=≤<=*x*<=≤<=109) is a number that you should add to *S*. It's guaranteed that *x* is not less than...
Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line. Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10<=-<=6. Formally, let your answer be *a*, and the jury's answe...
[ "6\n1 3\n2\n1 4\n2\n1 8\n2\n", "4\n1 1\n1 4\n1 5\n2\n" ]
[ "0.0000000000\n0.5000000000\n3.0000000000\n", "2.0000000000\n" ]
none
[ { "input": "6\n1 3\n2\n1 4\n2\n1 8\n2", "output": "0.0000000000\n0.5000000000\n3.0000000000" }, { "input": "4\n1 1\n1 4\n1 5\n2", "output": "2.0000000000" }, { "input": "8\n1 7\n1 26\n1 40\n1 45\n1 64\n2\n1 88\n1 94", "output": "31.6666666667" }, { "input": "9\n1 35\n2\n2\n1 ...
77
102,400
0
22,323
958
Hyperspace Jump (hard)
[]
null
null
It is now 125 years later, but humanity is still on the run from a humanoid-cyborg race determined to destroy it. Or perhaps we are getting some stories mixed up here... In any case, the fleet is now smaller. However, in a recent upgrade, all the navigation systems have been outfitted with higher-dimensional, linear-al...
The first line of the input contains two space-separated integers *m* and *d* (2<=≤<=*m*<=≤<=30<=000, 1<=≤<=*d*<=≤<=5) – the number of ships and the dimension of the full underlying vector space, respectively. Next, the *m* subspaces are described, one after another. The *i*-th subspace, which corresponds to the *i*-th...
Output *m* space-separated integers *g*1,<=...,<=*g**m*, where denotes the group number assigned to the *i*-th ship. That is, for any 1<=≤<=*i*<=&lt;<=*j*<=≤<=*m*, the following should hold: *g**i*<==<=*g**j* if and only if the *i*-th and the *j*-th subspaces are equal. In addition, the sequence (*g*1,<=*g*2,<=...,<=*...
[ "8 2\n1\n5 0\n1\n0 1\n1\n0 1\n2\n0 6\n0 1\n2\n0 1\n1 0\n2\n-5 -5\n4 3\n2\n1 1\n0 1\n2\n1 0\n1 0\n" ]
[ "1 2 2 2 3 3 3 1 " ]
In the sample testcase, the first and the last subspace are equal, subspaces 2 to 4 are equal, and subspaces 5 to 7 are equal. Recall that two subspaces, one given as the span of vectors <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e8dc56705315ce4b334dd46dbc9c8c51fffb0036.png" style="ma...
[]
3,000
39,833,600
0
22,350
186
Growing Mushrooms
[ "greedy", "sortings" ]
null
null
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules ...
The first input line contains four integer numbers *n*, *t*1, *t*2, *k* (1<=≤<=*n*,<=*t*1,<=*t*2<=≤<=1000; 1<=≤<=*k*<=≤<=100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following...
Print the final results' table: *n* lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
[ "2 3 3 50\n2 4\n4 2\n", "4 1 1 1\n544 397\n280 101\n280 101\n693 970\n" ]
[ "1 15.00\n2 15.00\n", "4 1656.07\n1 937.03\n2 379.99\n3 379.99\n" ]
- First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 &gt; 4·3·0.5 + 2·3.
[ { "input": "2 3 3 50\n2 4\n4 2", "output": "1 15.00\n2 15.00" }, { "input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970", "output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99" }, { "input": "10 1 1 25\n981 1\n352 276\n164 691\n203 853\n599 97\n901 688\n934 579\n910 959\n317 624\n440 737",...
62
0
0
22,353
0
none
[ "none" ]
null
null
Students love to celebrate their holidays. Especially if the holiday is the day of the end of exams! Despite the fact that Igor K., unlike his groupmates, failed to pass a programming test, he decided to invite them to go to a cafe so that each of them could drink a bottle of... fresh cow milk. Having entered the cafe...
The only input data file contains three integers *n*, *w* and *m* (1<=≤<=*n*<=≤<=50, 100<=≤<=*w*<=≤<=1000, 2<=≤<=*m*<=≤<=50), where *n* stands for the number of ordered bottles, *w* stands for the volume of each of them and *m* stands for the number of friends in the company.
Print on the first line "YES" if it is possible to pour the milk so that the milk from each bottle was in no more than two different cups. If there's no solution, print "NO". If there is a solution, then print *m* more lines, where the *i*-th of them describes the content of the *i*-th student's cup. The line should c...
[ "2 500 3\n", "4 100 5\n", "4 100 7\n", "5 500 2\n" ]
[ "YES\n1 333.333333\n2 333.333333\n2 166.666667 1 166.666667\n", "YES\n3 20.000000 4 60.000000\n1 80.000000\n4 40.000000 2 40.000000\n3 80.000000\n2 60.000000 1 20.000000\n", "NO\n", "YES\n4 250.000000 5 500.000000 2 500.000000\n3 500.000000 1 500.000000 4 250.000000\n" ]
none
[ { "input": "2 500 3", "output": "YES\n1 333.333333\n2 333.333333\n2 166.666667 1 166.666667" }, { "input": "4 100 5", "output": "YES\n3 20.000000 4 60.000000\n1 80.000000\n4 40.000000 2 40.000000\n3 80.000000\n2 60.000000 1 20.000000" }, { "input": "4 100 7", "output": "NO" }, { ...
93
17,715,200
3
22,366
401
Sereja and Contests
[ "greedy", "implementation", "math" ]
null
null
Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds. Codesorfes has rounds of two types: *Div*1 (for advanced coders) and *Div*2 (for beginner coders). Two rounds, *Div*1 and *Div*2, can go simultaneously, (*Div*1 ro...
The first line contains two integers: *x* (1<=≤<=*x*<=≤<=4000) — the round Sereja is taking part in today, and *k* (0<=≤<=*k*<=&lt;<=4000) — the number of rounds he took part in. Next *k* lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds...
Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed.
[ "3 2\n2 1\n2 2\n", "9 3\n1 2 3\n2 8\n1 4 5\n", "10 0\n" ]
[ "0 0", "2 3", "5 9" ]
In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual *Div*2 round and the round with identifier 6 will be synchronous with the *Div*1 round. The maximum number of rounds equal...
[ { "input": "3 2\n2 1\n2 2", "output": "0 0" }, { "input": "9 3\n1 2 3\n2 8\n1 4 5", "output": "2 3" }, { "input": "10 0", "output": "5 9" }, { "input": "10 2\n1 1 2\n1 8 9", "output": "3 5" }, { "input": "9 3\n1 4 5\n1 1 2\n1 6 7", "output": "2 2" }, { ...
187
3,481,600
3
22,392
56
Corporation Mail
[ "data structures", "expression parsing", "implementation" ]
C. Corporation Mail
2
256
The Beroil corporation structure is hierarchical, that is it can be represented as a tree. Let's examine the presentation of this structure as follows: - *employee* ::= *name*. | *name*:*employee*1,*employee*2, ... ,*employee**k*. - *name* ::= name of an employee That is, the description of each employee consists o...
The first and single line contains the corporation structure which is a string of length from 1 to 1000 characters. It is guaranteed that the description is correct. Every name is a string consisting of capital Latin letters from 1 to 10 symbols in length.
Print a single number — the number of uncomfortable situations in the company.
[ "MIKE:MAX.,ARTEM:MIKE..,DMITRY:DMITRY.,DMITRY...\n", "A:A..\n", "A:C:C:C:C.....\n" ]
[ "3\n", "1\n", "6\n" ]
none
[ { "input": "A:A..", "output": "1" }, { "input": "CK:CK.,CK.,CK..", "output": "3" }, { "input": "RHLGWEVBJ:KAWUINWEI:KAWUINWEI..,ZQATMW.,KAWUINWEI.,RSWN..", "output": "1" }, { "input": "GIRRY.", "output": "0" }, { "input": "XGB:QJNGARRAZV:DWGDCCU:ARDKJV:P:MXBLZKLPY...
498
30,515,200
3.818661
22,412
962
Simple Cycles Edges
[ "dfs and similar", "graphs", "trees" ]
null
null
You are given an undirected graph, consisting of $n$ vertices and $m$ edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself). A cycle in a graph is called a simple, if it conta...
The first line contain two integers $n$ and $m$ $(1 \le n \le 100\,000$, $0 \le m \le \min(n \cdot (n - 1) / 2, 100\,000))$ — the number of vertices and the number of edges. Each of the following $m$ lines contain two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) — the description of the edges.
In the first line print the number of edges, which belong to exactly one simple cycle. In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.
[ "3 3\n1 2\n2 3\n3 1\n", "6 7\n2 3\n3 4\n4 2\n1 2\n1 5\n5 6\n6 1\n", "5 6\n1 2\n2 3\n2 4\n4 3\n2 5\n5 3\n" ]
[ "3\n1 2 3 \n", "6\n1 2 3 5 6 7 \n", "0\n\n" ]
none
[ { "input": "3 3\n1 2\n2 3\n3 1", "output": "3\n1 2 3 " }, { "input": "6 7\n2 3\n3 4\n4 2\n1 2\n1 5\n5 6\n6 1", "output": "6\n1 2 3 5 6 7 " }, { "input": "5 6\n1 2\n2 3\n2 4\n4 3\n2 5\n5 3", "output": "0" }, { "input": "4 5\n1 2\n2 3\n3 4\n4 1\n1 3", "output": "0" }, {...
46
0
0
22,509
425
Sereja and Two Sequences
[ "data structures", "dp" ]
null
null
Sereja has two sequences *a*1,<=*a*2,<=...,<=*a**n* and *b*1,<=*b*2,<=...,<=*b**m*, consisting of integers. One day Sereja got bored and he decided two play with them. The rules of the game was very simple. Sereja makes several moves, in one move he can perform one of the following actions: 1. Choose several (at leas...
The first line contains integers *n*, *m*, *s*, *e* (1<=≤<=*n*,<=*m*<=≤<=105; 1<=≤<=*s*<=≤<=3·105; 103<=≤<=*e*<=≤<=104). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=105). The third line contains *m* integers *b*1, *b*2, ..., *b**m* (1<=≤<=*b**i*<=≤<=105).
Print a single integer — maximum number of money in dollars that Sereja can get.
[ "5 5 100000 1000\n1 2 3 4 5\n3 2 4 5 1\n", "3 4 3006 1000\n1 2 3\n1 2 4 3\n" ]
[ "3\n", "2\n" ]
none
[]
46
7,987,200
0
22,527
311
Biologist
[ "flows" ]
null
null
SmallR is a biologist. Her latest research finding is how to change the sex of dogs. In other words, she can change female dogs into male dogs and vice versa. She is going to demonstrate this technique. Now SmallR has *n* dogs, the costs of each dog's change may be different. The dogs are numbered from 1 to *n*. The c...
The first line contains three integers *n*, *m*, *g* (1<=≤<=*n*<=≤<=104,<=0<=≤<=*m*<=≤<=2000,<=0<=≤<=*g*<=≤<=104). The second line contains *n* integers, each is 0 or 1, the sex of each dog, 0 represent the female and 1 represent the male. The third line contains *n* integers *v*1,<=*v*2,<=...,<=*v**n* (0<=≤<=*v**i*<=≤...
Print a single integer, the maximum money SmallR can gain. Note that the integer is negative if SmallR will lose money.
[ "5 5 9\n0 1 1 1 0\n1 8 6 2 3\n0 7 3 3 2 1 1\n1 8 1 5 1\n1 0 3 2 1 4 1\n0 8 3 4 2 1 0\n1 7 2 4 1 1\n", "5 5 8\n1 0 1 1 1\n6 5 4 2 8\n0 6 3 2 3 4 0\n0 8 3 3 2 4 0\n0 0 3 3 4 1 1\n0 10 3 4 3 1 1\n0 4 3 3 4 1 1\n" ]
[ "2\n", "16\n" ]
none
[ { "input": "5 5 9\n0 1 1 1 0\n1 8 6 2 3\n0 7 3 3 2 1 1\n1 8 1 5 1\n1 0 3 2 1 4 1\n0 8 3 4 2 1 0\n1 7 2 4 1 1", "output": "2" }, { "input": "5 5 8\n1 0 1 1 1\n6 5 4 2 8\n0 6 3 2 3 4 0\n0 8 3 3 2 4 0\n0 0 3 3 4 1 1\n0 10 3 4 3 1 1\n0 4 3 3 4 1 1", "output": "16" }, { "input": "5 5 8\n0 1 0...
30
0
0
22,544
487
Conveyor Belts
[ "data structures" ]
null
null
Automatic Bakery of Cyberland (ABC) recently bought an *n*<=×<=*m* rectangle table. To serve the diners, ABC placed seats around the table. The size of each seat is equal to a unit square, so there are 2(*n*<=+<=*m*) seats in total. ABC placed conveyor belts on each unit square on the table. There are three types of c...
The first line of input contains three integers *n*, *m* and *q* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=10,<=1<=≤<=*q*<=≤<=105), separated by a space. Next *n* lines, each line contains *m* characters, describing the table. The characters can only be one of "&lt;^&gt;". Next *q* lines, each line describes an event. The f...
For each event of type "A", output two integers *tx*, *ty* in a line, separated by a space, denoting the destination of (*x*,<=*y*) is (*tx*,<=*ty*). If there is an infinite loop, you should output *tx*<==<=*ty*<==<=<=-<=1.
[ "2 2 3\n&gt;&gt;\n^^\nA 2 1\nC 1 2 &lt;\nA 2 1\n", "4 5 7\n&gt;&lt;&lt;^&lt;\n^&lt;^^&gt;\n&gt;&gt;&gt;^&gt;\n&gt;^&gt;&gt;^\nA 3 1\nA 2 2\nC 1 4 &lt;\nA 3 1\nC 1 2 ^\nA 3 1\nA 2 2" ]
[ "1 3\n-1 -1\n", "0 4\n-1 -1\n-1 -1\n0 2\n0 2\n" ]
For the first sample: If the bread goes from (2, 1), it will go out of the table at (1, 3). After changing the conveyor belt of (1, 2) to "&lt;", when the bread goes from (2, 1) again, it will get stuck at "&gt;&lt;", so output is ( - 1,  - 1).
[]
31
0
0
22,558
436
Cardboard Box
[ "data structures", "greedy" ]
null
null
Everyone who has played Cut the Rope knows full well how the gameplay is organized. All levels in the game are divided into boxes. Initially only one box with some levels is available. Player should complete levels to earn stars, collecting stars opens new box with levels. Imagine that you are playing Cut the Rope for...
The first line contains two integers *n* and *w* (1<=≤<=*n*<=≤<=3·105; 1<=≤<=*w*<=≤<=2*n*) — the number of levels in the first box and the number of stars you need to open another box. Each of the following *n* lines contains two integers *a**i* and *b**i* (1<=≤<=*a**i*<=&lt;<=*b**i*<=≤<=109) — the attributes of the *i...
In the first line print integer *t* — the minimum time you need to open the next box. In the next line, print *n* digits without spaces — the description of the optimal scenario: - if you need to pass the *i*-th level for one star, the *i*-th digit should equal 1; - if you need to pass the *i*-th level for two st...
[ "2 3\n1 2\n1 2\n", "5 3\n10 20\n5 10\n10 20\n6 9\n25 30\n" ]
[ "3\n12\n", "14\n01020\n" ]
In the first test sample, answer 21 is also assumed correct.
[]
30
0
0
22,569
883
Berland Army
[ "constructive algorithms", "graphs", "greedy" ]
null
null
There are *n* military men in the Berland army. Some of them have given orders to other military men by now. Given *m* pairs (*x**i*, *y**i*), meaning that the military man *x**i* gave the *i*-th order to another military man *y**i*. It is time for reform! The Berland Ministry of Defence plans to introduce ranks in th...
The first line contains three integers *n*, *m* and *k* (1<=≤<=*n*<=≤<=2·105, 0<=≤<=*m*<=≤<=2·105, 1<=≤<=*k*<=≤<=2·105) — number of military men in the Berland army, number of orders and number of ranks. The second line contains *n* integers *r*1,<=*r*2,<=...,<=*r**n*, where *r**i*<=&gt;<=0 (in this case 1<=≤<=*r**i*<...
Print *n* integers, where the *i*-th number is the rank of the *i*-th military man. If there are many solutions, print any of them. If there is no solution, print the only number -1.
[ "5 3 3\n0 3 0 0 2\n2 4\n3 4\n3 5\n", "7 6 5\n0 4 5 4 1 0 0\n6 1\n3 6\n3 1\n7 5\n7 1\n7 4\n", "2 2 2\n2 1\n1 2\n2 1\n" ]
[ "1 3 3 2 2 \n", "2 4 5 4 1 3 5 \n", "-1\n" ]
none
[]
46
0
0
22,581
990
Flow Control
[ "dfs and similar", "dp", "greedy", "trees" ]
null
null
You have to handle a very complex water distribution system. The system consists of $n$ junctions and $m$ pipes, $i$-th pipe connects junctions $x_i$ and $y_i$. The only thing you can do is adjusting the pipes. You have to choose $m$ integer numbers $f_1$, $f_2$, ..., $f_m$ and use them as pipe settings. $i$-th pipe w...
The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of junctions. The second line contains $n$ integers $s_1, s_2, \dots, s_n$ ($-10^4 \le s_i \le 10^4$) — constraints for the junctions. The third line contains an integer $m$ ($0 \le m \le 2 \cdot 10^5$) — the number of pipes. $i$-th of ...
If you can choose such integer numbers $f_1, f_2, \dots, f_m$ in such a way that all requirements on incoming and outcoming flows are satisfied, then output "Possible" in the first line. Then output $m$ lines, $i$-th line should contain $f_i$ — the chosen setting numbers for the pipes. Pipes are numbered in order they ...
[ "4\n3 -10 6 1\n5\n1 2\n3 2\n2 4\n3 4\n3 1\n", "4\n3 -10 6 4\n5\n1 2\n3 2\n2 4\n3 4\n3 1\n" ]
[ "Possible\n4\n-6\n8\n-7\n7\n", "Impossible\n" ]
none
[ { "input": "4\n3 -10 6 1\n5\n1 2\n3 2\n2 4\n3 4\n3 1", "output": "Possible\n-3\n-6\n1\n0\n0" }, { "input": "4\n3 -10 6 4\n5\n1 2\n3 2\n2 4\n3 4\n3 1", "output": "Impossible" }, { "input": "1\n0\n0", "output": "Possible" }, { "input": "1\n123\n0", "output": "Impossible" ...
31
0
0
22,603
154
Double Profiles
[ "graphs", "hashing", "sortings" ]
null
null
You have been offered a job in a company developing a large social network. Your first task is connected with searching profiles that most probably belong to the same user. The social network contains *n* registered profiles, numbered from 1 to *n*. Some pairs there are friends (the "friendship" relationship is mutual...
The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*<=≤<=106, 0<=≤<=*m*<=≤<=106), — the number of profiles and the number of pairs of friends, correspondingly. Next *m* lines contains descriptions of pairs of friends in the format "*v* *u*", where *v* and *u* (1<=≤<=*v*,<=*u*<=≤<=*n*,<=*v*<=≠<=...
Print the single integer — the number of unordered pairs of profiles that are doubles. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the %I64d specificator.
[ "3 3\n1 2\n2 3\n1 3\n", "3 0\n", "4 1\n1 3\n" ]
[ "3\n", "3\n", "2\n" ]
In the first and second sample any two profiles are doubles. In the third sample the doubles are pairs of profiles (1, 3) and (2, 4).
[ { "input": "3 3\n1 2\n2 3\n1 3", "output": "3" }, { "input": "3 0", "output": "3" }, { "input": "4 1\n1 3", "output": "2" }, { "input": "1 0", "output": "0" }, { "input": "2 0", "output": "1" }, { "input": "2 1\n1 2", "output": "1" }, { "in...
46
0
0
22,620
132
Turing Tape
[ "implementation" ]
null
null
INTERCAL is the oldest of esoteric programming languages. One of its many weird features is the method of character-based output, known as Turing Tape method. It converts an array of unsigned 8-bit integers into a sequence of characters to print, using the following method. The integers of the array are processed one ...
The input will consist of a single line *text* which contains the message printed using the described method. String *text* will contain between 1 and 100 characters, inclusive. ASCII-code of each character of *text* will be between 32 (space) and 126 (tilde), inclusive.
Output the initial array, which was used to produce *text*, one integer per line.
[ "Hello, World!\n" ]
[ "238\n108\n112\n0\n64\n194\n48\n26\n244\n168\n24\n16\n162\n" ]
Let's have a closer look at the beginning of the example. The first character is "H" with ASCII-code 72 = 01001000<sub class="lower-index">2</sub>. Its reverse is 00010010<sub class="lower-index">2</sub> = 18, and this number should become the result of the second step of processing. The result of the first step is con...
[ { "input": "Hello, World!", "output": "238\n108\n112\n0\n64\n194\n48\n26\n244\n168\n24\n16\n162" }, { "input": "N", "output": "142" }, { "input": "!{|aPUBKs[k\"HE;>O&(Nf}N4,*#g*<3sQXFJ'?Z/H9L[xx Rc5\"8~v}84+wv]w[oO0e':MaNy&6*]jRkYomz[o?=13Y?!fzA3eC\\", "output": "124\n166\n160\n184\n...
154
307,200
-1
22,658
67
Partial Teacher
[ "dp", "graphs", "greedy", "implementation" ]
A. Partial Teacher
1
256
A teacher decides to give toffees to his students. He asks *n* students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees. He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same mar...
The first line of input contains the number of students *n* (2<=≤<=*n*<=≤<=1000). The second line gives (*n*<=-<=1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both ha...
Output consists of *n* integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
[ "5\nLRLR\n", "5\n=RRR\n" ]
[ "2 1 2 1 2\n", "1 1 2 3 4\n" ]
none
[ { "input": "5\nLRLR", "output": "2 1 2 1 2" }, { "input": "5\n=RRR", "output": "1 1 2 3 4" }, { "input": "6\nRLRL=", "output": "1 2 1 2 1 1" }, { "input": "3\nR=", "output": "1 2 2" }, { "input": "7\nRR==RR", "output": "1 2 3 3 3 4 5" }, { "input": "16...
310
1,945,600
3.841376
22,671
954
Water Taps
[ "binary search", "greedy", "sortings" ]
null
null
Consider a system of *n* water taps all pouring water into the same container. The *i*-th water tap can be set to deliver any amount of water from 0 to *a**i* ml per second (this amount may be a real number). The water delivered by *i*-th tap has temperature *t**i*. If for every you set *i*-th tap to deliver exactly ...
The first line contains two integers *n* and *T* (1<=≤<=*n*<=≤<=200000, 1<=≤<=*T*<=≤<=106) — the number of water taps and the desired temperature of water, respectively. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=106) where *a**i* is the maximum amount of water *i*-th tap can deliv...
Print the maximum possible amount of water with temperature exactly *T* you can get per second (if it is impossible to obtain water with such temperature, then the answer is considered to be 0). Your answer is considered correct if its absolute or relative error doesn't exceed 10<=-<=6.
[ "2 100\n3 10\n50 150\n", "3 9\n5 5 30\n6 6 10\n", "2 12\n1 3\n10 15\n" ]
[ "6.000000000000000\n", "40.000000000000000\n", "1.666666666666667\n" ]
none
[ { "input": "2 100\n3 10\n50 150", "output": "6.000000000000000" }, { "input": "3 9\n5 5 30\n6 6 10", "output": "40.000000000000000" }, { "input": "2 12\n1 3\n10 15", "output": "1.666666666666667" }, { "input": "20 30\n70 97 14 31 83 22 83 56 19 87 59 7 7 89 24 82 34 40 6 24\n...
0
0
-1
22,684
482
Game with Strings
[ "bitmasks", "dp", "probabilities" ]
null
null
You play the game with your friend. The description of this game is listed below. Your friend creates *n* distinct strings of the same length *m* and tells you all the strings. Then he randomly chooses one of them. He chooses strings equiprobably, i.e. the probability of choosing each of the *n* strings equals . You ...
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=50) — the number of strings your friend came up with. The next *n* lines contain the strings that your friend has created. It is guaranteed that all the strings are distinct and only consist of large and small English letters. Besides, the lengths of all stri...
Print the single number — the expected value. Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=9.
[ "2\naab\naac\n", "3\naaA\naBa\nCaa\n", "3\naca\nvac\nwqq\n" ]
[ "2.000000000000000\n", "1.666666666666667\n", "1.000000000000000\n" ]
In the first sample the strings only differ in the character in the third position. So only the following situations are possible: - you guess the string in one question. The event's probability is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/adaad783a304f9e72218e6e79114c6551a112aad.p...
[]
1,000
183,398,400
0
22,751
294
Shaass and Bookshelf
[ "dp", "greedy" ]
null
null
Shaass has *n* books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the *i*-th book is *t**i* and its pages' width is equal to *w**i*. The thickness of each book is either 1 or 2. All books have the same page heights. Shaass puts the bo...
The first line of the input contains an integer *n*, (1<=≤<=*n*<=≤<=100). Each of the next *n* lines contains two integers *t**i* and *w**i* denoting the thickness and width of the *i*-th book correspondingly, (1<=≤<=*t**i*<=≤<=2,<=1<=≤<=*w**i*<=≤<=100).
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
[ "5\n1 12\n1 3\n2 15\n2 5\n2 1\n", "3\n1 10\n2 1\n2 4\n" ]
[ "5\n", "3\n" ]
none
[ { "input": "5\n1 12\n1 3\n2 15\n2 5\n2 1", "output": "5" }, { "input": "3\n1 10\n2 1\n2 4", "output": "3" }, { "input": "10\n2 10\n2 4\n2 8\n2 3\n2 5\n2 6\n1 2\n1 10\n1 10\n2 5", "output": "12" }, { "input": "1\n2 7", "output": "2" }, { "input": "50\n1 24\n1 16\n1...
139
0
0
22,755