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
89
Widget Library
[ "dp", "expression parsing", "graphs", "implementation" ]
B. Widget Library
2
256
Vasya writes his own library for building graphical user interface. Vasya called his creation VTK (VasyaToolKit). One of the interesting aspects of this library is that widgets are packed in each other. A widget is some element of graphical interface. Each widget has width and height, and occupies some rectangle on t...
The first line contains an integer *n* β€” the number of instructions (1<=≀<=*n*<=≀<=100). Next *n* lines contain instructions in the language VasyaScript β€” one instruction per line. There is a list of possible instructions below. - "Widget [name]([x],[y])" β€” create a new widget [name] of the type Widget possessing the...
For each widget print on a single line its name, width and height, separated by spaces. The lines must be ordered lexicographically by a widget's name. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d specificator)
[ "12\nWidget me(50,40)\nVBox grandpa\nHBox father\ngrandpa.pack(father)\nfather.pack(me)\ngrandpa.set_border(10)\ngrandpa.set_spacing(20)\nWidget brother(30,60)\nfather.pack(brother)\nWidget friend(20,60)\nWidget uncle(100,20)\ngrandpa.pack(uncle)\n", "15\nWidget pack(10,10)\nHBox dummy\nHBox x\nVBox y\ny.pack(dum...
[ "brother 30 60\nfather 80 60\nfriend 20 60\ngrandpa 120 120\nme 50 40\nuncle 100 20\n", "dummy 0 0\npack 10 10\nx 40 10\ny 10 10\n" ]
In the first sample the widgets are arranged as follows:
[ { "input": "12\nWidget me(50,40)\nVBox grandpa\nHBox father\ngrandpa.pack(father)\nfather.pack(me)\ngrandpa.set_border(10)\ngrandpa.set_spacing(20)\nWidget brother(30,60)\nfather.pack(brother)\nWidget friend(20,60)\nWidget uncle(100,20)\ngrandpa.pack(uncle)", "output": "brother 30 60\nfather 80 60\nfriend 2...
124
1,126,400
3.966902
172,327
414
Mashmokh and Water Tanks
[ "binary search", "data structures", "greedy", "trees", "two pointers" ]
null
null
Mashmokh is playing a new game. In the beginning he has *k* liters of water and *p* coins. Additionally he has a rooted tree (an undirected connected acyclic graph) that consists of *m* vertices. Each vertex of the tree contains a water tank that is empty in the beginning. The game begins with the fact that Mashmokh c...
The first line of the input contains three space-separated integers *m*,<=*k*,<=*p*Β (2<=≀<=*m*<=≀<=105;Β 0<=≀<=*k*,<=*p*<=≀<=109). Each of the following *m*<=-<=1 lines contains two space-separated integers *a**i*,<=*b**i*Β (1<=≀<=*a**i*,<=*b**i*<=≀<=*m*;Β *a**i*<=β‰ <=*b**i*) β€” the edges of the tree. Consider that the v...
Output a single integer, the number Mashmokh asked you to find.
[ "10 2 1\n1 2\n1 3\n3 4\n3 5\n2 6\n6 8\n6 7\n9 8\n8 10\n", "5 1000 1000\n1 2\n1 3\n3 4\n3 5\n" ]
[ "2\n", "4\n" ]
The tree in the first sample is shown on the picture below. The black, red, blue colors correspond to vertices with 0, 1, 2 liters of water. One way to achieve the maximum amount of money is to put 1 liter of water in each of vertices 3 and 4. The beginning state is shown on the picture below. Then in the first move ...
[]
46
0
0
172,345
809
Surprise me!
[ "divide and conquer", "math", "number theory", "trees" ]
null
null
Tired of boring dates, Leha and Noora decided to play a game. Leha found a tree with *n* vertices numbered from 1 to *n*. We remind you that tree is an undirected graph without cycles. Each vertex *v* of a tree has a number *a**v* written on it. Quite by accident it turned out that all values written on vertices are d...
The first line of input contains one integer number *n* (2<=≀<=*n*<=≀<=2Β·105) Β β€” number of vertices in a tree. The second line contains *n* different numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*) separated by spaces, denoting the values written on a tree vertices. Each of the next *n*<=-<=1 lines contains...
In a single line print a number equal to *P*Β·*Q*<=-<=1 modulo 109<=+<=7.
[ "3\n1 2 3\n1 2\n2 3\n", "5\n5 4 3 1 2\n3 5\n1 2\n4 3\n2 5\n" ]
[ "333333338\n", "8\n" ]
Euler's totient function Ο†(*n*) is the number of such *i* that 1 ≀ *i* ≀ *n*,and *gcd*(*i*, *n*) = 1, where *gcd*(*x*, *y*) is the greatest common divisor of numbers *x* and *y*. There are 6 variants of choosing vertices by Leha and Noora in the first testcase: - *u* = 1, *v* = 2, *f*(1, 2) = φ(*a*<sub class="lower-...
[]
46
0
0
172,423
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...
186
8,806,400
3
172,723
813
Two Melodies
[ "dp", "flows" ]
null
null
Alice is a beginner composer and now she is ready to create another masterpiece. And not even the single one but two at the same time! Alice has a sheet with *n* notes written on it. She wants to take two such non-empty non-intersecting subsequences that both of them form a melody and sum of their lengths is maximal....
The first line contains one integer number *n* (2<=≀<=*n*<=≀<=5000). The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=105) β€” notes written on a sheet.
Print maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody.
[ "4\n1 2 4 5\n", "6\n62 22 60 61 48 49\n" ]
[ "4\n", "5\n" ]
In the first example subsequences [1, 2] and [4, 5] give length 4 in total. In the second example subsequences [62, 48, 49] and [60, 61] give length 5 in total. If you choose subsequence [62, 61] in the first place then the second melody will have maximum length 2, that gives the result of 4, which is not maximal.
[ { "input": "4\n1 2 4 5", "output": "4" }, { "input": "6\n62 22 60 61 48 49", "output": "5" }, { "input": "2\n1 4", "output": "2" }, { "input": "2\n5 4", "output": "2" }, { "input": "10\n9 6 8 5 5 2 8 9 2 2", "output": "9" }, { "input": "10\n7776 32915 ...
2,000
0
0
173,796
585
Present for Vitalik the Philatelist
[ "combinatorics", "math", "number theory" ]
null
null
Vitalik the philatelist has a birthday today! As he is a regular customer in a stamp store called 'Robin Bobin', the store management decided to make him a gift. Vitalik wants to buy one stamp and the store will give him a non-empty set of the remaining stamps, such that the greatest common divisor (GCD) of the price...
The first line of the input contains integer *n* (2<=≀<=*n*<=≀<=5Β·105) β€” the number of distinct stamps, available for sale in the 'Robin Bobin' store. The second line contains a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (2<=≀<=*a**i*<=≀<=107), where *a**i* is the price of the *i*-th stamp.
Print a single integer β€” the remainder of the sought number of situations modulo 109<=+<=7.
[ "3\n2 3 2\n", "2\n9 6\n" ]
[ "5\n", "0\n" ]
In the first sample the following situations are possible: - Vitalik buys the 1-st stamp, the store gives him the 2-nd stamp as a present; - Vitalik buys the 3-rd stamp, the store gives him the 2-nd stamp as a present; - Vitalik buys the 2-nd stamp, the store gives him the 1-st stamp as a present; - Vitalik buys ...
[]
31
0
0
173,868
766
Mahmoud and a xor trip
[ "bitmasks", "constructive algorithms", "data structures", "dfs and similar", "dp", "math", "trees" ]
null
null
Mahmoud and Ehab live in a country with *n* cities numbered from 1 to *n* and connected by *n*<=-<=1 undirected roads. It's guaranteed that you can reach any city from any other using these roads. Each city has a number *a**i* attached to it. We define the distance from city *x* to city *y* as the xor of numbers attac...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of cities in Mahmoud and Ehab's country. Then the second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=106) which represent the numbers attached to the cities. Integer *a**i* is attached to the city *i*. Each of the next *n...
Output one number denoting the total distance between all pairs of cities.
[ "3\n1 2 3\n1 2\n2 3\n", "5\n1 2 3 4 5\n1 2\n2 3\n3 4\n3 5\n", "5\n10 9 8 7 6\n1 2\n2 3\n3 4\n3 5\n" ]
[ "10\n", "52\n", "131\n" ]
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: [https:/...
[ { "input": "3\n1 2 3\n1 2\n2 3", "output": "10" }, { "input": "5\n1 2 3 4 5\n1 2\n2 3\n3 4\n3 5", "output": "52" }, { "input": "5\n10 9 8 7 6\n1 2\n2 3\n3 4\n3 5", "output": "131" }, { "input": "1\n1", "output": "1" }, { "input": "2\n1 2\n1 2", "output": "6" ...
46
0
0
174,308
0
none
[ "none" ]
null
null
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is *l* centimeters. The ruler already has *n* marks, with which he can make mea...
The first line contains four positive space-separated integers *n*, *l*, *x*, *y* (2<=≀<=*n*<=≀<=105, 2<=≀<=*l*<=≀<=109, 1<=≀<=*x*<=&lt;<=*y*<=≀<=*l*) β€” the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of *n* integers *a*1,<=*a*2,<...
In the first line print a single non-negative integer *v* β€” the minimum number of marks that you need to add on the ruler. In the second line print *v* space-separated integers *p*1,<=*p*2,<=...,<=*p**v* (0<=≀<=*p**i*<=≀<=*l*). Number *p**i* means that the *i*-th mark should be at the distance of *p**i* centimeters fr...
[ "3 250 185 230\n0 185 250\n", "4 250 185 230\n0 20 185 250\n", "2 300 185 230\n0 300\n" ]
[ "1\n230\n", "0\n", "2\n185 230\n" ]
In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the thir...
[ { "input": "3 250 185 230\n0 185 250", "output": "1\n230" }, { "input": "4 250 185 230\n0 20 185 250", "output": "0" }, { "input": "2 300 185 230\n0 300", "output": "2\n185 230" }, { "input": "4 300 4 5\n0 6 7 300", "output": "1\n11" }, { "input": "2 100 30 70\n0 ...
77
0
0
174,357
434
Furukawa Nagisa's Tree
[ "binary search", "divide and conquer", "sortings", "trees" ]
null
null
One day, Okazaki Tomoya has bought a tree for Furukawa Nagisa's birthday. The tree is so strange that every node of the tree has a value. The value of the *i*-th node is *v**i*. Now Furukawa Nagisa and Okazaki Tomoya want to play a game on the tree. Let (*s*,<=*e*) be the path from node *s* to node *e*, we can write d...
The first line contains four integers *n*, *y*, *k* and *x*Β (1<=≀<=*n*<=≀<=105;Β 2<=≀<=*y*<=≀<=109;Β 1<=≀<=*k*<=&lt;<=*y*;Β 0<=≀<=*x*<=&lt;<=*y*) β€” *n* being the number of nodes on the tree. It is guaranteed that *y* is a prime number. The second line contains *n* integers, the *i*-th integer is *v**i*Β (0<=≀<=*v**i*<=&lt...
Output a single integer β€” the number of triplets that are correct for Furukawa Nagisa's conclusion.
[ "1 2 1 0\n1\n", "3 5 2 1\n4 3 1\n1 2\n2 3\n", "8 13 8 12\n0 12 7 4 12 0 8 12\n1 8\n8 4\n4 6\n6 2\n2 3\n8 5\n2 7\n" ]
[ "1\n", "14\n", "341\n" ]
none
[]
30
0
0
174,379
173
Deputies
[ "constructive algorithms", "graphs", "greedy", "implementation" ]
null
null
The Trinitarian kingdom has exactly *n*<==<=3*k* cities. All of them are located on the shores of river Trissisipi, which flows through the whole kingdom. Some of the cities are located on one side of the river, and all the rest are on the other side. Some cities are connected by bridges built between them. Each bridg...
The first line contains two integers *n* and *m* β€” the number of cities and bridges (3<=≀<=*n*<=&lt;<=105, *n*<==<=3*k*, 0<=≀<=*m*<=≀<=105). Next *m* lines describe the bridges. The *i*-th line contains two integers *a**i* and *b**i* β€” the numbers of cities that are connected by the *i*-th bridge (1<=≀<=*a**i*,<=*b**i*...
If distributing the deputies in the required manner is impossible, print in a single line "NO" (without the quotes). Otherwise, in the first line print "YES" (without the quotes), and in the second line print which deputy should be put in charge of each city. The *i*-th number should represent the number of the deputy...
[ "6 6\n1 2\n4 1\n3 5\n6 5\n2 6\n4 6\n", "3 1\n1 2\n" ]
[ "YES\n1 2 1 2 2 1 ", "NO" ]
none
[]
92
0
0
174,509
185
Soap Time! - 2
[ "binary search", "data structures" ]
null
null
Imagine the Cartesian coordinate system. There are *k* different points containing subway stations. One can get from any subway station to any one instantly. That is, the duration of the transfer between any two subway stations can be considered equal to zero. You are allowed to travel only between subway stations, tha...
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=105;Β 0<=≀<=*k*<=≀<=105) β€” the number of dwarves and the number of subway stations, correspondingly. The next *n* lines contain the coordinates of the dwarves. The *i*-th line contains two space-separated integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=≀<...
Print a single number β€” the minimum time, in which all dwarves can gather together at one point to watch the soap.
[ "1 0\n2 -2\n", "2 2\n5 -3\n-4 -5\n-4 0\n-3 -2\n" ]
[ "0\n", "6\n" ]
none
[]
92
0
0
174,734
91
Grocer's Problem
[ "constructive algorithms", "graphs", "greedy" ]
D. Grocer's Problem
2
256
Yesterday was a fair in a supermarket's grocery section. There were *n* jars with spices on the fair. Before the event the jars were numbered from 1 to *n* from the left to the right. After the event the jars were moved and the grocer had to sort them by the increasing of the numbers. The grocer has a special machine ...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105). The second line contains *n* space-separated integers *a**i* (1<=≀<=*a**i*<=≀<=*n*) β€” the *i*-th number represents the number of a jar that occupies the *i*-th position. It is guaranteed that all the numbers are distinct.
Print on the first line the least number of operations needed to rearrange all the jars in the order of the numbers' increasing. Then print the description of all actions in the following format. On the first line of the description of one action indicate the number of jars that need to be taken (*k*), on the second l...
[ "6\n3 5 6 1 2 4\n", "14\n9 13 11 3 10 7 12 14 1 5 4 6 8 2\n" ]
[ "2\n4\n1 3 6 4 \n3 6 4 1 \n2\n2 5 \n5 2 \n", "3\n4\n2 13 8 14 \n13 8 14 2 \n5\n6 7 12 5 10 \n7 12 6 10 5 \n5\n3 11 4 1 9 \n11 4 3 9 1 \n" ]
Let's consider the first sample. The jars can be sorted within two actions. During the first action we take the jars from positions 1, 3, 6 and 4 and put them so that the jar that used to occupy the position 1 will occupy the position 3 after the operation is completed. The jar from position 3 will end up in position ...
[ { "input": "6\n3 5 6 1 2 4", "output": "2\n4\n1 3 6 4 \n3 6 4 1 \n2\n2 5 \n5 2 " }, { "input": "14\n9 13 11 3 10 7 12 14 1 5 4 6 8 2", "output": "3\n4\n2 13 8 14 \n13 8 14 2 \n5\n6 7 12 5 10 \n7 12 6 10 5 \n5\n3 11 4 1 9 \n11 4 3 9 1 " }, { "input": "14\n11 4 14 10 7 3 9 2 5 8 1 13 12 6"...
124
0
0
175,248
120
Luck is in Numbers
[ "greedy" ]
null
null
Vasya has been collecting transport tickets for quite a while now. His collection contains several thousands of tram, trolleybus and bus tickets. Vasya is already fed up with the traditional definition of what a lucky ticket is. Thus, he's looking for new perspectives on that. Besides, Vasya cannot understand why all t...
The first line contains the number of the ticket that consists of *k* characters (*k*<==<=2*n*,<=1<=≀<=*n*<=≀<=105).
Print the number of the sought ticket or "-1" (without the quotes) if no such ticket exists.
[ "13\n", "2345\n", "88\n" ]
[ "20\n", "2348\n", "-1\n" ]
none
[ { "input": "13", "output": "20" }, { "input": "2345", "output": "2348" }, { "input": "88", "output": "-1" }, { "input": "682444", "output": "682445" }, { "input": "685116", "output": "685120" }, { "input": "576079", "output": "576086" }, { ...
404
16,486,400
3
175,386
775
University Schedule
[ "*special" ]
null
null
In this problem your task is to come up with a week schedule of classes in university for professors and student groups. Consider that there are 6 educational days in week and maximum number of classes per educational day is 7 (classes numerated from 1 to 7 for each educational day). It is known that in university *n*...
The first line contains three integers *n*, *m* and *a* (1<=≀<=*n*,<=*m*,<=*a*<=≀<=60) β€” the number of groups, the number of professors and the number of classrooms. Each of the following *n* lines contains *m* integers from 0 to 24 β€” *j*-th number in *i*-th line equals to the number of classes with the professor *j* ...
In the first line print the minimized value of function *f*. After that print blank line. After that print the schedule for each student group in increasing order of group number. For each student group print 7 lines. Each line must contains 6 numbers. Let the number at *i*-th line and *j*-th column equals to *x*. If...
[ "3 3 1\n1 0 0\n0 1 0\n0 0 1\n", "3 1 1\n1\n1\n1\n", "5 7 10\n1 3 6 0 1 2 4\n0 3 0 6 5 1 4\n3 5 1 2 3 2 4\n2 3 1 1 4 1 2\n2 4 3 2 4 3 2\n" ]
[ "54\n\n1 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n\n0 0 0 0 0 0 \n2 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n\n0 0 0 0 0 0 \n0 0 0 0 0 0 \n3 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n0 0 0 0 0 0 \n", "52\n\...
During the main part of the competition (one week) you solution will be judged on 100 preliminary tests. The first 10 preliminary tests are available for download by a link [http://assets.codeforces.com/files/vk/vkcup-2017-wr2-materials-v1.tar.gz](//assets.codeforces.com/files/vk/vkcup-2017-wr2-materials-v1.tar.gz). A...
[]
46
0
0
175,400
313
Ilya and Matrix
[ "constructive algorithms", "greedy", "implementation", "sortings" ]
null
null
Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve. He's got a square 2*n*<=Γ—<=2*n*-sized matrix and 4*n* integers. You need to arrange all these numbers in the matrix (put each number in a single i...
The first line contains integer 4*n* (1<=≀<=4*n*<=≀<=2Β·106). The next line contains 4*n* integers *a**i* (1<=≀<=*a**i*<=≀<=109) β€” the numbers you need to arrange in the 2*n*<=Γ—<=2*n*-sized matrix.
On a single line print the maximum value of the beauty of the described matrix. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "1\n13\n", "4\n1 2 3 4\n" ]
[ "13\n", "14\n" ]
Consider the second sample. You need to arrange the numbers in the matrix as follows: Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14.
[ { "input": "1\n13", "output": "13" }, { "input": "4\n1 2 3 4", "output": "14" }, { "input": "16\n978618343 473608041 799158564 800910753 461479363 520477481 780529176 678879534 118274424 720632652 639921017 582019792 143353286 537373229 944668919 758615621", "output": "14440495117" ...
46
0
0
175,877
912
Perun, Ult!
[ "brute force", "greedy", "sortings" ]
null
null
A lot of students spend their winter holidays productively. Vlad has advanced very well in doing so! For three days already, fueled by salads and tangerinesΒ β€” the leftovers from New Year celebrationΒ β€” he has been calibrating his rating in his favorite MOBA game, playing as a hero named Perun. Perun has an ultimate abi...
In the first line, two integers are given (separated by spaces)Β β€” *n* and *m* (1<=≀<=*n*<=≀<=105, 0<=≀<=*m*<=≀<=105). In the second line, there are three integers: , and (, ). Each of the following *n* lines has three integersΒ β€” , , (, ). The next *m* lines contain three integers eachΒ β€” , , (, , ). It is guarant...
Output the single integerΒ β€” the maximum amount of gold Vlad can obtain if he applies "Thunderwrath" exactly once, or -1 if this amount can be infinitely large.
[ "3 2\n1000 10 50\n70 5 5\n90 70 1\n110 20 2\n20 2 10\n30 3 10\n", "1 1\n500 50 1000\n750 750 20\n10 1 300\n" ]
[ "3000\n", "-1\n" ]
On the pictures you can see health points of each enemy versus time in sample cases. Periods when Vlad can kill one enemy are marked with yellow color. Periods when Vlad can kill two enemies are marked with purple color. In the first sample case, Vlad can activate the ability at the 50-th second: the enemies 2 and 3...
[]
1,000
55,296,000
0
176,217
366
Dima and Magic Guitar
[ "brute force", "implementation", "math" ]
null
null
Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with *n* strings and *m* frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the *i*-th string holding it on the *j*...
The first line of the input contains four integers *n*, *m*, *k* and *s* (1<=≀<=*n*,<=*m*<=≀<=2000,<=1<=≀<=*k*<=≀<=9,<=2<=≀<=*s*<=≀<=105). Then follow *n* lines, each containing *m* integers *a**ij* (1<=≀<=*a**ij*<=≀<=*k*). The number in the *i*-th row and the *j*-th column (*a**ij*) means a note that the guitar prod...
In a single line print a single number β€” the maximum possible complexity of the song.
[ "4 6 5 7\n3 1 2 2 3 1\n3 2 2 2 5 5\n4 2 2 2 5 3\n3 2 2 1 4 3\n2 3 1 4 1 5 1\n", "4 4 9 5\n4 7 9 5\n1 2 1 7\n8 3 4 9\n5 7 7 2\n7 1 9 2 5\n" ]
[ "8\n", "4\n" ]
none
[ { "input": "4 6 5 7\n3 1 2 2 3 1\n3 2 2 2 5 5\n4 2 2 2 5 3\n3 2 2 1 4 3\n2 3 1 4 1 5 1", "output": "8" }, { "input": "4 4 9 5\n4 7 9 5\n1 2 1 7\n8 3 4 9\n5 7 7 2\n7 1 9 2 5", "output": "4" }, { "input": "5 5 2 2\n2 2 2 1 2\n2 1 2 2 2\n2 2 2 2 2\n1 2 2 2 2\n2 2 2 2 1\n1 1", "output": ...
61
0
0
176,748
398
Instant Messanger
[ "data structures" ]
null
null
User ainta decided to make a new instant messenger called "aintalk". With aintalk, each user can chat with other people. User ainta made the prototype of some functions to implement this thing. 1. login(*u*): User *u* logins into aintalk and becomes online. 1. logout(*u*): User *u* logouts and becomes offline. 1. a...
The first line contains three space-separated integers *n*, *m* and *q* (1<=≀<=*n*<=≀<=50000; 1<=≀<=*m*<=≀<=150000; 1<=≀<=*q*<=≀<=250000) β€” the number of users, the number of pairs of friends, and the number of queries. The second line contains an integer *o* (1<=≀<=*o*<=≀<=*n*) β€” the number of online users at the beg...
For each count_online_friends(*u*) query, print the required answer in a single line.
[ "5 2 9\n1\n4\n1 3\n3 4\nC 3\nA 2 5\nO 1\nD 1 3\nA 1 2\nA 4 2\nC 2\nF 4\nC 2\n" ]
[ "1\n2\n1\n" ]
none
[]
233
27,648,000
0
176,868
799
Field expansion
[ "brute force", "dp", "meet-in-the-middle" ]
null
null
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are *n* extensions, the *i*-th of them multiplies the width or the length (b...
The first line contains five integers *a*, *b*, *h*, *w* and *n* (1<=≀<=*a*,<=*b*,<=*h*,<=*w*,<=*n*<=≀<=100<=000)Β β€” the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (2<=≀<=*a**i*<=≀<=1...
Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0.
[ "3 3 2 4 4\n2 5 4 10\n", "3 3 3 3 5\n2 3 5 4 2\n", "5 5 1 2 3\n2 2 3\n", "3 4 1 1 3\n2 3 2\n" ]
[ "1\n", "0\n", "-1\n", "3\n" ]
In the first example it is enough to use any of the extensions available. For example, we can enlarge *h* in 5 times using the second extension. Then *h* becomes equal 10 and it is now possible to place the rectangle on the field.
[ { "input": "3 3 2 4 4\n2 5 4 10", "output": "1" }, { "input": "3 3 3 3 5\n2 3 5 4 2", "output": "0" }, { "input": "5 5 1 2 3\n2 2 3", "output": "-1" }, { "input": "3 4 1 1 3\n2 3 2", "output": "3" }, { "input": "572 540 6 2 12\n2 3 2 2 2 3 3 3 2 2 2 2", "outpu...
62
2,560,000
3
177,053
403
Beautiful Pairs of Numbers
[ "combinatorics", "dp" ]
null
null
The sequence of integer pairs (*a*1,<=*b*1),<=(*a*2,<=*b*2),<=...,<=(*a**k*,<=*b**k*) is beautiful, if the following statements are fulfilled: - 1<=≀<=*a*1<=≀<=*b*1<=&lt;<=*a*2<=≀<=*b*2<=&lt;<=...<=&lt;<=*a**k*<=≀<=*b**k*<=≀<=*n*, where *n* is a given positive integer; - all numbers *b*1<=-<=*a*1, *b*2<=-<=*a*2, .....
The first line contains integer *t* (1<=≀<=*t*<=≀<=<=2Β·105) β€” the number of the test data. Each of the next *t* lines contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=1000).
For each test from the input print the answer to the problem modulo 1000000007 (109<=+<=7). Print the answers to the tests in the order in which the tests are given in the input.
[ "6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n" ]
[ "1\n3\n0\n6\n2\n0\n" ]
In the first test sample there is exactly one beautiful sequence: (1, 1). In the second test sample, the following sequences are beautiful: - (1, 1); - (1, 2); - (2, 2). In the fourth test sample, the following sequences are beautiful: - (1, 1); - (1, 2); - (1, 3); - (2, 2); - (2, 3); - (3, 3). In the f...
[ { "input": "6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3", "output": "1\n3\n0\n6\n2\n0" } ]
31
0
0
177,200
475
Strongly Connected City 2
[ "dfs and similar" ]
null
null
Imagine a city with *n* junctions and *m* streets. Junctions are numbered from 1 to *n*. In order to increase the traffic flow, mayor of the city has decided to make each street one-way. This means in the street between junctions *u* and *v*, the traffic moves only from *u* to *v* or only from *v* to *u*. The proble...
The first line of input contains integers *n* and *m*, (), denoting the number of junctions and streets of the city. Each of the following *m* lines contains two integers *u* and *v*, (*u*<=β‰ <=*v*), denoting endpoints of a street in the city. Between every two junctions there will be at most one street. It is guarant...
Print the maximal number of pairs (*u*,<=*v*) such that that it is possible to reach junction *v* from *u* after directing the streets.
[ "5 4\n1 2\n1 3\n1 4\n1 5\n", "4 5\n1 2\n2 3\n3 4\n4 1\n1 3\n", "2 1\n1 2\n", "6 7\n1 2\n2 3\n1 3\n1 4\n4 5\n5 6\n6 4\n" ]
[ "13\n", "16\n", "3\n", "27\n" ]
In the first sample, if the mayor makes first and second streets one-way towards the junction 1 and third and fourth streets in opposite direction, there would be 13 pairs of reachable junctions: {(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (2, 1), (3, 1), (1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)}
[]
31
0
0
177,859
611
New Year and Cleaning
[ "binary search", "implementation" ]
null
null
Limak is a little polar bear. His parents told him to clean a house before the New Year's Eve. Their house is a rectangular grid with *h* rows and *w* columns. Each cell is an empty square. He is a little bear and thus he can't clean a house by himself. Instead, he is going to use a cleaning robot. A cleaning robot h...
The first line contains three integers *n*, *h* and *w* (1<=≀<=*n*,<=*h*,<=*w*<=≀<=500<=000)Β β€” the length of the pattern, the number of rows and the number of columns, respectively. The second line contains a string of length *n*Β β€” the pattern of *n* moves. Each character is one of uppercase letters 'U', 'D', 'L' or '...
Print one line with the answer. If a cleaning robot will never stop, print "-1" (without the quotes). Otherwise, print the number of minutes it will take to clean a house modulo 109<=+<=7.
[ "1 10 2\nR\n", "3 4 6\nRUL\n", "4 1 500000\nRLRL\n" ]
[ "30\n", "134\n", "-1\n" ]
In the first sample house is a grid with 10 rows and 2 columns. Starting a robot anywhere in the second column will result in only one move (thus, one minute of cleaning) in which robot will hit a wallΒ β€” he tried to go right but there is no third column. Starting a robot anywhere in the first column will result in two ...
[]
2,000
2,355,200
0
178,283
949
Binary Cards
[ "brute force" ]
null
null
It is never too late to play the fancy "Binary Cards" game! There is an infinite amount of cards of positive and negative ranks that are used in the game. The absolute value of any card rank is a power of two, i.e. each card has a rank of either 2*k* or <=-<=2*k* for some integer *k*<=β‰₯<=0. There is an infinite amount...
The first line of input contains an integer *n* (1<=≀<=*n*<=≀<=100<=000), the number of rounds in the game. The second line of input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=100<=000<=≀<=*a**i*<=≀<=100<=000), the numbers that jury is going to tell in each round.
In the first line print the integer *k* (0<=≀<=*k*<=≀<=100<=000), the minimum number of cards you have to pick in your deck in ordered to win the "Binary Cards". In the second line print *k* integers *b*1,<=*b*2,<=...,<=*b**k* (<=-<=220<=≀<=*b**i*<=≀<=220, |*b**i*| is a power of two), the ranks of the cards in your de...
[ "1\n9\n", "5\n-1 3 0 4 7\n", "4\n2 -2 14 18\n" ]
[ "2\n1 8\n", "3\n4 -1 4\n", "3\n-2 2 16" ]
In the first sample there is the only round in the game, in which you may simply draw both your cards. Note that this sample test is the only one satisfying the first test group constraints. In the second sample you may draw the only card  - 1 in the first round, cards 4 and  - 1 in the second round, nothing in the th...
[]
78
7,065,600
0
178,417
0
none
[ "none" ]
null
null
You work in a big office. It is a 9Β floor building with an elevator that can accommodate up to 4Β people. It is your responsibility to manage this elevator. Today you are late, so there are queues on some floors already. For each person you know the floor where he currently is and the floor he wants to reach. Also, you...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=2000)Β β€” the number of employees. The *i*-th of the next *n* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=9, *a**i*<=β‰ <=*b**i*)Β β€” the floor on which an employee initially is, and the floor he wants to reach. The employees are given in th...
Print a single integerΒ β€” the minimal possible time in seconds.
[ "2\n3 5\n5 3\n", "2\n5 3\n3 5\n" ]
[ "10", "12" ]
<img class="tex-graphics" src="https://espresso.codeforces.com/1b7e25450a0703db67675fea25386865f0290049.png" style="max-width: 100.0%;max-height: 100.0%;"/> *t* = 2 <img class="tex-graphics" src="https://espresso.codeforces.com/0f585328ada5689c39e5a7263a792979c00fb01a.png" style="max-width: 100.0%;max-height: 100.0%;"...
[]
46
0
0
178,804
474
Pillars
[ "binary search", "data structures", "dp", "sortings", "trees" ]
null
null
Marmot found a row with *n* pillars. The *i*-th pillar has the height of *h**i* meters. Starting from one pillar *i*1, Marmot wants to jump on the pillars *i*2, ..., *i**k*. (1<=≀<=*i*1<=&lt;<=*i*2<=&lt;<=...<=&lt;<=*i**k*<=≀<=*n*). From a pillar *i* Marmot can jump on a pillar *j* only if *i*<=&lt;<=*j* and |*h**i*<=-...
The first line contains two integers *n* and *d* (1<=≀<=*n*<=≀<=105, 0<=≀<=*d*<=≀<=109). The second line contains *n* numbers *h*1,<=*h*2,<=...,<=*h**n* (1<=≀<=*h**i*<=≀<=1015).
The first line should contain one integer *k*, the maximal length of a jump sequence. The second line should contain *k* integers *i*1,<=*i*2,<=...,<=*i**k* (1<=≀<=*i*1<=&lt;<=*i*2<=&lt;<=...<=&lt;<=*i**k*<=≀<=*n*), representing the pillars' indices from the maximal length jump sequence. If there is more than one max...
[ "5 2\n1 3 6 7 4\n", "10 3\n2 1 3 6 9 11 7 3 20 18\n" ]
[ "4\n1 2 3 5 \n", "6\n1 4 6 7 8 9 \n" ]
In the first example Marmot chooses the pillars 1, 2, 3, 5 with the heights 1, 3, 6, 4. Another jump sequence of length 4 is 1, 2, 4, 5.
[]
577
268,390,400
0
178,919
388
Fox and Meteor Shower
[ "geometry" ]
null
null
There is a meteor shower on the sky and there are *n* meteors. The sky can be viewed as a 2D Euclid Plane and the meteor is point on this plane. Fox Ciel looks at the sky. She finds out that the orbit of each meteor is a straight line, and each meteor has a constant velocity. Now Ciel wants to know: what is the maxim...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=1000). Each of the next *n* lines contains six integers: *t*1,<=*x*1,<=*y*1,<=*t*2,<=*x*2,<=*y*2 β€” the description of a meteor's orbit: at time *t*1, the current meteor is located at the point (*x*1, *y*1) and at time *t*2, the meteor is located at point (*x*2, *y*2...
Print a single integer β€” the maximum number of meteors such that any pair met at the same position at a certain time.
[ "2\n0 0 1 1 0 2\n0 1 0 1 2 0\n", "3\n-1 -1 0 3 3 0\n0 2 -1 -1 3 -2\n-2 0 -1 6 0 3\n", "4\n0 0 0 1 0 1\n0 0 1 1 1 1\n0 1 1 1 1 0\n0 1 0 1 0 0\n", "1\n0 0 0 1 0 0\n" ]
[ "2\n", "3\n", "1\n", "1\n" ]
In example 1, meteor 1 and 2 meet in *t*=-1 at (0, 0). In example 2, meteor 1 and 2 meet in *t*=1 at (1, 0), meteor 1 and 3 meet in *t*=0 at (0, 0) and meteor 2 and 3 meet in *t*=2 at (0, 1). In example 3, no two meteor meet. In example 4, there is only 1 meteor, and its velocity is zero. If your browser doesn't su...
[]
31
0
0
179,111
0
none
[ "none" ]
null
null
The country Treeland consists of *n* cities connected with *n*<=-<=1 bidirectional roads in such a way that it's possible to reach every city starting from any other city using these roads. There will be a soccer championship next year, and all participants are Santa Clauses. There are exactly 2*k* teams from 2*k* diff...
The first line of input contains two integers *n* and *k* (2<=≀<=*n*<=≀<=2Β·105,<=2<=≀<=2*k*<=≀<=*n*)Β β€” the number of cities in Treeland and the number of pairs of teams, respectively. The following *n*<=-<=1 lines describe roads in Treeland: each of these lines contains two integers *a* and *b* (1<=≀<=*a*,<=*b*<=≀<=*n...
The first line of output must contain the only positive integer *m* which should be equal to the minimum possible number of cities the teams can be settled in. The second line should contain *m* distinct numbers *d*1,<=*d*2,<=...,<=*d**m* (1<=≀<=*d**i*<=≀<=*n*) denoting the indices of the cities where the teams should...
[ "6 2\n1 2\n1 3\n2 4\n2 5\n3 6\n2 5 4 6\n" ]
[ "1\n2\n5 4 2\n6 2 2\n" ]
In the first test the orginizers can settle all the teams in the city number 2. The way to divide all teams into pairs is not important, since all requirements are satisfied anyway, because the city 2 lies on the shortest path between every two cities from {2, 4, 5, 6}.
[ { "input": "6 2\n1 2\n1 3\n2 4\n2 5\n3 6\n2 5 4 6", "output": "1\n2\n5 4 2\n6 2 2" }, { "input": "2 1\n1 2\n1 2", "output": "1\n1\n2 1 1" }, { "input": "6 2\n1 6\n6 2\n6 5\n5 3\n5 4\n1 3 4 2", "output": "1\n6\n4 2 6\n3 1 6" }, { "input": "10 1\n4 2\n9 2\n1 4\n4 10\n2 3\n7 10\...
30
0
0
179,133
702
T-Shirts
[ "data structures" ]
null
null
The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In all *n* types of t-shirts go on sale. The t-shirt of the *i*-th type has two integer parameters β€” *c**i* and *q**i*, where *c**i* β€” is the price of the *i*-th type t-shirt, *q**i* β€” is the quality of the *i*-th type t-shirt....
The first line contains the positive integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of t-shirt types. Each of the following *n* lines contains two integers *c**i* and *q**i* (1<=≀<=*c**i*,<=*q**i*<=≀<=109) β€” the price and the quality of the *i*-th type t-shirt. The next line contains the positive integer *k* (1<=≀<=...
The first line of the input data should contain the sequence of *k* integers, where the *i*-th number should be equal to the number of t-shirts, which the *i*-th customer will buy.
[ "3\n7 5\n3 5\n4 3\n2\n13 14\n", "2\n100 500\n50 499\n4\n50 200 150 100\n" ]
[ "2 3 \n", "1 2 2 1 \n" ]
In the first example the first customer will buy the t-shirt of the second type, then the t-shirt of the first type. He will spend 10 and will not be able to buy the t-shirt of the third type because it costs 4, and the customer will owe only 3. The second customer will buy all three t-shirts (at first, the t-shirt of ...
[]
15
0
0
179,613
757
Team Rocket Rises Again
[ "data structures", "graphs", "shortest paths" ]
null
null
It's the turn of the year, so Bash wants to send presents to his friends. There are *n* cities in the Himalayan region and they are connected by *m* bidirectional roads. Bash is living in city *s*. Bash has exactly one friend in each of the other cities. Since Bash wants to surprise his friends, he decides to send a Pi...
The first line contains three space separated integers *n*, *m* and *s* (2<=≀<=*n*<=≀<=2Β·105, , 1<=≀<=*s*<=≀<=*n*)Β β€” the number of cities and the number of roads in the Himalayan region and the city Bash lives in. Each of the next *m* lines contain three space-separated integers *u*, *v* and *w* (1<=≀<=*u*,<=*v*<=≀<=*...
Print a single integer, the answer to the problem.
[ "4 4 3\n1 2 1\n2 3 1\n2 4 1\n3 1 1\n", "7 11 2\n1 2 5\n1 3 5\n2 4 2\n2 5 2\n3 6 3\n3 7 3\n4 6 2\n3 4 2\n6 7 3\n4 5 7\n4 7 7\n" ]
[ "2\n", "4\n" ]
In the first sample, on destroying the city 2, the length of shortest distance between pairs of cities (3, 2) and (3, 4) will change. Hence the answer is 2.
[]
30
0
0
179,858
468
Tree
[ "graph matchings" ]
null
null
Little X has a tree consisting of *n* nodes (they are numbered from 1 to *n*). Each edge of the tree has a positive length. Let's define the distance between two nodes *v* and *u* (we'll denote it *d*(*v*,<=*u*)) as the sum of the lengths of edges in the shortest path between *v* and *u*. A permutation *p* is a seque...
The first line contains an integer *n*Β (1<=≀<=*n*<=≀<=105). Each of the next *n*<=-<=1 lines contains three space separated integers *u**i*,<=<=*v**i*,<=*w**i*Β (1<=≀<=<=*u**i*,<=<=*v**i*<=≀<=<=*n*;Β 1<=≀<=<=*w**i*<=≀<=<=105), denoting an edge between nodes *u**i* and *v**i* with length equal to *w**i*. It is guarantee...
In the first line print the maximum possible value of the described sum. In the second line print *n* integers, representing the lexicographically smallest permutation.
[ "2\n1 2 3\n", "5\n1 2 2\n1 3 3\n2 4 4\n2 5 5\n" ]
[ "6\n2 1\n", "32\n2 1 4 5 3\n" ]
none
[]
0
0
-1
179,993
436
Banners
[ "brute force", "data structures", "dp" ]
null
null
All modern mobile applications are divided into free and paid. Even a single application developers often release two versions: a paid version without ads and a free version with ads. Suppose that a paid version of the app costs *p* (*p* is an integer) rubles, and the free version of the application contains *c* ad ba...
The first line contains two integers *n* and *w* (1<=≀<=*n*<=≀<=105;Β 1<=≀<=*w*<=≀<=105) β€” the number of users and the profit from a single banner. Each of the next *n* lines contains two integers *a**i* and *b**i* (0<=≀<=*a**i*,<=*b**i*<=≀<=105) β€” the characteristics of the *i*-th user.
Print (*max*Β *b**i*)<=+<=2 lines, in the *i*-th line print two integers: *pay* β€” the maximum gained profit at *c*<==<=*i*<=-<=1, *p* (0<=≀<=*p*<=≀<=109) β€” the corresponding optimal app cost. If there are multiple optimal solutions, print any of them.
[ "2 1\n2 0\n0 2\n", "3 1\n3 1\n2 2\n1 3\n" ]
[ "0 3\n3 2\n4 2\n2 2\n", "0 4\n3 4\n7 3\n7 2\n4 2\n" ]
none
[]
30
0
0
181,063
750
New Year and Old Subsequence
[ "data structures", "divide and conquer", "dp", "matrices" ]
null
null
A string *t* is called nice if a string "2017" occurs in *t* as a subsequence but a string "2016" doesn't occur in *t* as a subsequence. For example, strings "203434107" and "9220617" are nice, while strings "20016", "1234" and "20167" aren't nice. The ugliness of a string is the minimum possible number of characters ...
The first line of the input contains two integers *n* and *q* (4<=≀<=*n*<=≀<=200<=000, 1<=≀<=*q*<=≀<=200<=000)Β β€” the length of the string *s* and the number of queries respectively. The second line contains a string *s* of length *n*. Every character is one of digits '0'–'9'. The *i*-th of next *q* lines contains two...
For each query print the ugliness of the given substring.
[ "8 3\n20166766\n1 8\n1 7\n2 8\n", "15 5\n012016662091670\n3 4\n1 14\n4 15\n1 13\n10 15\n", "4 2\n1234\n2 4\n1 2\n" ]
[ "4\n3\n-1\n", "-1\n2\n1\n-1\n-1\n", "-1\n-1\n" ]
In the first sample: - In the first query, *ugliness*("20166766") = 4 because all four sixes must be removed. - In the second query, *ugliness*("2016676") = 3 because all three sixes must be removed. - In the third query, *ugliness*("0166766") =  - 1 because it's impossible to remove some digits to get a nice strin...
[]
46
0
0
181,135
830
Singer House
[ "combinatorics", "dp", "graphs", "trees" ]
null
null
It is known that passages in Singer house are complex and intertwined. Let's define a Singer *k*-house as a graph built by the following process: take complete binary tree of height *k* and add edges from each vertex to all its successors, if they are not yet present. Count the number of non-empty paths in Singer *k*-...
The only line contains single integer *k* (1<=≀<=*k*<=≀<=400).
Print single integerΒ β€” the answer for the task modulo 109<=+<=7.
[ "2\n", "3\n", "20\n" ]
[ "9\n", "245\n", "550384565\n" ]
There are 9 paths in the first example (the vertices are numbered on the picture below): 1, 2, 3, 1-2, 2-1, 1-3, 3-1, 2-1-3, 3-1-2.
[ { "input": "2", "output": "9" }, { "input": "3", "output": "245" }, { "input": "20", "output": "550384565" }, { "input": "1", "output": "1" }, { "input": "4", "output": "126565" }, { "input": "5", "output": "54326037" }, { "input": "6", ...
30
0
0
182,327
0
none
[ "none" ]
null
null
Piet is one of the most known visual esoteric programming languages. The programs in Piet are constructed from colorful blocks of pixels and interpreted using pretty complicated rules. In this problem we will use a subset of Piet language with simplified rules. The program will be a rectangular image consisting of col...
The first line of the input contains two integer numbers *m* (1<=≀<=*m*<=≀<=50) and *n* (1<=≀<=*n*<=≀<=5Β·107). Next *m* lines contain the rows of the program. All the lines have the same length between 1 and 50 pixels, and consist of characters 0-9. The first character of the first line will not be equal to 0.
Output the color of the block which will be current after *n* steps of program interpretation.
[ "2 10\n12\n43\n", "3 12\n1423\n6624\n6625\n", "5 9\n10345\n23456\n34567\n45678\n56789\n" ]
[ "1\n", "6\n", "5\n" ]
In the first example IP changes in the following way. After step 1 block 2 becomes current one and stays it after two more steps. After step 4 BP moves to block 3, after step 7 β€” to block 4, and finally after step 10 BP returns to block 1. The sequence of states of IP is shown on the image: the arrows are traversed cl...
[]
62
0
0
184,029
509
Progress Monitoring
[ "dp", "trees" ]
null
null
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students: You are given a tree *T* with *n* vertices, specified by its adjacency matrix *a*[1... *n*,<=1... *n*]. What is the output of the following pseudocode? In order to simplify the test results checking procedur...
The first line contains the positive integer *n* (1<=≀<=*n*<=≀<=500) β€” the length of sequence *b*. The second line contains *n* positive integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=*n*). It is guaranteed that *b* is a permutation, or in other words, each of the numbers 1,<=2,<=...,<=*n* appears exactly once...
Output the number of trees satisfying the conditions above modulo 109<=+<=7.
[ "3\n1 2 3\n", "3\n1 3 2\n" ]
[ "2\n", "1\n" ]
none
[]
46
0
0
184,107
538
A Heap of Heaps
[ "brute force", "data structures", "math", "sortings" ]
null
null
Andrew skipped lessons on the subject 'Algorithms and Data Structures' for the entire term. When he came to the final test, the teacher decided to give him a difficult task as a punishment. The teacher gave Andrew an array of *n* numbers *a*1, ..., *a**n*. After that he asked Andrew for each *k* from 1 to *n*<=-<=1 to...
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=2Β·105). The second line contains *n* space-separated integers *a*1, ..., *a**n* (<=-<=109<=≀<=*a**i*<=≀<=109).
in a single line print *n*<=-<=1 integers, separate the consecutive numbers with a single space β€” the number of elements for which the property of the *k*-ary heap is violated, for *k*<==<=1, 2, ..., *n*<=-<=1.
[ "5\n1 5 4 3 2\n", "6\n2 2 2 2 2 2\n" ]
[ "3 2 1 0\n", "0 0 0 0 0\n" ]
Pictures with the heaps for the first sample are given below; elements for which the property of the heap is violated are marked with red. In the second sample all elements are equal, so the property holds for all pairs.
[ { "input": "5\n1 5 4 3 2", "output": "3 2 1 0" }, { "input": "6\n2 2 2 2 2 2", "output": "0 0 0 0 0" }, { "input": "2\n0 0", "output": "0" }, { "input": "2\n1 2", "output": "0" }, { "input": "2\n10 -1", "output": "1" }, { "input": "5\n934 235 171 111 1...
1,341
30,924,800
3
184,497
325
Reclamation
[ "dsu" ]
null
null
In a far away land, there exists a planet shaped like a cylinder. There are three regions in this planet: top, bottom, and side as shown in the following picture. Both the top and the bottom areas consist of big cities. The side area consists entirely of the sea. One day, a city decides that it has too little space a...
The first line consists of three integers *r*, *c*, and *n* (1<=≀<=*r*,<=*c*<=≀<=3000, 1<=≀<=*n*<=≀<=3Β·105). Then, *n* lines follow, describing the cells in the order you will reclamate them. Each line will consists of two integers: *r**i* and *c**i* (1<=≀<=*r**i*<=≀<=*r*, 1<=≀<=*c**i*<=≀<=*c*), which represents the ce...
You should output a single number representing the number of cells that were successfully turned to land.
[ "3 4 9\n2 2\n3 2\n2 3\n3 4\n3 1\n1 3\n2 1\n1 1\n1 4\n" ]
[ "6\n" ]
The pictures below show the sequence of reclamations that are performed in the example input. Blue cells represent the cells occupied by sea, while other colored cells represent land. The latest cell that are reclamated is colored either yellow or red, depending on whether the addition violates the condition in the sta...
[]
186
409,600
-1
184,895
601
Acyclic Organic Compounds
[ "data structures", "dfs and similar", "dsu", "hashing", "strings", "trees" ]
null
null
You are given a tree *T* with *n* vertices (numbered 1 through *n*) and a letter in each vertex. The tree is rooted at vertex 1. Let's look at the subtree *T**v* of some vertex *v*. It is possible to read a string along each simple path starting at *v* and ending at some vertex in *T**v* (possibly *v* itself). Let's d...
The first line of the input contains one integer *n* (1<=≀<=*n*<=≀<=300<=000)Β β€” the number of vertices of the tree. The second line contains *n* space-separated integers *c**i* (0<=≀<=*c**i*<=≀<=109). The third line contains a string *s* consisting of *n* lowercase English lettersΒ β€” the *i*-th character of this strin...
Print two lines. On the first line, print over all 1<=≀<=*i*<=≀<=*n*. On the second line, print the number of vertices *v* for which .
[ "10\n1 2 7 20 20 30 40 50 50 50\ncacabbcddd\n1 2\n6 8\n7 2\n6 2\n5 4\n5 9\n3 10\n2 5\n2 3\n", "6\n0 2 4 1 1 1\nraaaba\n1 2\n2 3\n2 4\n2 5\n3 6\n" ]
[ "51\n3\n", "6\n2\n" ]
In the first sample, the tree looks like this: The sets of strings that can be read from individual vertices are: Finally, the values of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/14691bbbb07694ceb84c7c73aa24a29ecfcb9e6b.png" style="max-width: 100.0%;max-height: 100.0%;"/> are: In t...
[ { "input": "10\n1 2 7 20 20 30 40 50 50 50\ncacabbcddd\n1 2\n6 8\n7 2\n6 2\n5 4\n5 9\n3 10\n2 5\n2 3", "output": "51\n3" }, { "input": "6\n0 2 4 1 1 1\nraaaba\n1 2\n2 3\n2 4\n2 5\n3 6", "output": "6\n2" }, { "input": "16\n186427765 186427335 186426882 186427151 186427355 186427547 186427...
46
0
0
185,657
212
Cowboys
[ "combinatorics", "dp", "math" ]
null
null
A very tense moment: *n* cowboys stand in a circle and each one points his colt at a neighbor. Each cowboy can point the colt to the person who follows or precedes him in clockwise direction. Human life is worthless, just like in any real western. The picture changes each second! Every second the cowboys analyse the s...
The input data consists of a single string *s*. Its length is from 3 to 100 characters, inclusive. Line *s* consists of letters "A" and "B".
Print the sought number of states.
[ "BABBBABBA\n", "ABABB\n", "ABABAB\n" ]
[ "2\n", "2\n", "4\n" ]
In the first sample the possible initial states are "ABBBABBAB" and "ABBBABBBA". In the second sample the possible initial states are "AABBB" and "BABBA".
[ { "input": "BABBBABBA", "output": "2" }, { "input": "ABABB", "output": "2" }, { "input": "ABABAB", "output": "4" }, { "input": "ABA", "output": "1" }, { "input": "AABB", "output": "0" }, { "input": "ABABBABBAABAB", "output": "4" }, { "input...
60
0
0
185,846
617
Polyline
[ "constructive algorithms", "implementation" ]
null
null
There are three points marked on the coordinate plane. The goal is to make a simple polyline, without self-intersections and self-touches, such that it passes through all these points. Also, the polyline must consist of only segments parallel to the coordinate axes. You are to find the minimum number of segments this p...
Each of the three lines of the input contains two integers. The *i*-th line contains integers *x**i* and *y**i* (<=-<=109<=≀<=*x**i*,<=*y**i*<=≀<=109)Β β€” the coordinates of the *i*-th point. It is guaranteed that all points are distinct.
Print a single numberΒ β€” the minimum possible number of segments of the polyline.
[ "1 -1\n1 1\n1 2\n", "-1 -1\n-1 3\n4 3\n", "1 1\n2 3\n3 2\n" ]
[ "1\n", "2\n", "3\n" ]
The variant of the polyline in the first sample: <img class="tex-graphics" src="https://espresso.codeforces.com/b41b4dad8437bd7a69f6ab01eaedf010b82ba7b8.png" style="max-width: 100.0%;max-height: 100.0%;"/> The variant of the polyline in the second sample: <img class="tex-graphics" src="https://espresso.codeforces.com/7...
[ { "input": "1 -1\n1 1\n1 2", "output": "1" }, { "input": "-1 -1\n-1 3\n4 3", "output": "2" }, { "input": "1 1\n2 3\n3 2", "output": "3" }, { "input": "1000000000 -1000000000\n1000000000 1000000000\n-1000000000 -1000000000", "output": "2" }, { "input": "-510073119 ...
46
0
0
185,873
248
Piglet's Birthday
[ "dp", "math", "probabilities" ]
null
null
Piglet has got a birthday today. His friend Winnie the Pooh wants to make the best present for him β€” a honey pot. Of course Winnie realizes that he won't manage to get the full pot to Piglet. In fact, he is likely to eat all the honey from the pot. And as soon as Winnie planned a snack on is way, the pot should initial...
The first line of the input contains a single number *n* (1<=≀<=*n*<=≀<=105) β€” the number of shelves at Winnie's place. The second line contains *n* integers *a**i* (1<=≀<=*i*<=≀<=*n*, 0<=≀<=*a**i*<=≀<=100) β€” the number of honey pots on a shelf number *i*. The next line contains integer *q* (1<=≀<=*q*<=≀<=105) β€” the ...
For each Winnie's action print the value of the mathematical expectation *m* by the moment when this action is performed. The relative or absolute error of each value mustn't exceed 10<=-<=9.
[ "3\n2 2 3\n5\n1 2 1\n2 1 2\n1 2 2\n3 1 1\n3 2 2\n" ]
[ "0.000000000000\n0.333333333333\n1.000000000000\n1.000000000000\n2.000000000000\n" ]
none
[]
2,000
77,209,600
0
186,982
232
Doe Graphs
[ "constructive algorithms", "divide and conquer", "dp", "graphs", "shortest paths" ]
null
null
John Doe decided that some mathematical object must be named after him. So he invented the Doe graphs. The Doe graphs are a family of undirected graphs, each of them is characterized by a single non-negative number β€” its order. We'll denote a graph of order *k* as *D*(*k*), and we'll denote the number of vertices in ...
The first line contains two integers *t* and *n* (1<=≀<=*t*<=≀<=105;Β 1<=≀<=*n*<=≀<=103) β€” the number of queries and the order of the given graph. The *i*-th of the next *t* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=1016, *a**i*<=β‰ <=*b**i*) β€” numbers of two vertices in the *i*-th query. It...
For each query print a single integer on a single line β€” the length of the shortest path between vertices *a**i* and *b**i*. Print the answers to the queries in the order, in which the queries are given in the input.
[ "10 5\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5\n" ]
[ "1\n1\n1\n2\n1\n2\n3\n1\n2\n1\n" ]
none
[]
3,000
10,854,400
0
187,002
723
Polycarp at the Radio
[ "greedy" ]
null
null
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is a band, which performs the *i*-th song. Polycarp likes bands with the numbers from 1 to *m*, but he doesn't really like others. We define as *b**j* th...
The first line of the input contains two integers *n* and *m* (1<=≀<=*m*<=≀<=*n*<=≀<=2000). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109), where *a**i* is the performer of the *i*-th song.
In the first line print two integers: the maximum possible value of the minimum among the *b**j* (1<=≀<=*j*<=≀<=*m*), where *b**j* is the number of songs in the changed playlist performed by the *j*-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed ...
[ "4 2\n1 2 3 2\n", "7 3\n1 3 2 2 2 2 1\n", "4 4\n1000000000 100 7 1000000000\n" ]
[ "2 1\n1 2 1 2 \n", "2 1\n1 3 3 2 2 2 1 \n", "1 4\n1 2 3 4 \n" ]
In the first sample, after Polycarp's changes the first band performs two songs (*b*<sub class="lower-index">1</sub> = 2), and the second band also performs two songs (*b*<sub class="lower-index">2</sub> = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any chang...
[ { "input": "4 2\n1 2 3 2", "output": "2 1\n1 2 1 2 " }, { "input": "7 3\n1 3 2 2 2 2 1", "output": "2 1\n1 3 3 2 2 2 1 " }, { "input": "4 4\n1000000000 100 7 1000000000", "output": "1 4\n1 2 3 4 " }, { "input": "1 1\n1", "output": "1 0\n1 " }, { "input": "1 1\n381...
77
4,198,400
3
187,043
55
Very simple problem
[ "geometry", "two pointers" ]
E. Very simple problem
3
256
You are given a convex polygon. Count, please, the number of triangles that contain a given point in the plane and their vertices are the vertices of the polygon. It is guaranteed, that the point doesn't lie on the sides and the diagonals of the polygon.
The first line contains integer *n* β€” the number of vertices of the polygon (3<=≀<=*n*<=≀<=100000). The polygon description is following: *n* lines containing coordinates of the vertices in clockwise order (integer *x* and *y* not greater than 109 by absolute value). It is guaranteed that the given polygon is nondegene...
The output should contain *t* integer numbers, each on a separate line, where *i*-th number is the answer for the *i*-th point. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
[ "4\n5 0\n0 0\n0 5\n5 5\n1\n1 3\n", "3\n0 0\n0 5\n5 0\n2\n1 1\n10 10\n", "5\n7 6\n6 3\n4 1\n1 2\n2 4\n4\n3 3\n2 3\n5 5\n4 2\n" ]
[ "2\n", "1\n0\n", "5\n3\n3\n4\n" ]
none
[ { "input": "4\n5 0\n0 0\n0 5\n5 5\n1\n1 3", "output": "2" }, { "input": "3\n0 0\n0 5\n5 0\n2\n1 1\n10 10", "output": "1\n0" }, { "input": "5\n7 6\n6 3\n4 1\n1 2\n2 4\n4\n3 3\n2 3\n5 5\n4 2", "output": "5\n3\n3\n4" }, { "input": "10\n3 2\n4 0\n3 -2\n1 -3\n-1 -3\n-3 -2\n-4 0\n-...
3,000
13,516,800
0
187,222
152
Garden
[ "bitmasks", "dp", "graphs", "trees" ]
null
null
Vasya has a very beautiful country garden that can be represented as an *n*<=Γ—<=*m* rectangular field divided into *n*Β·*m* squares. One beautiful day Vasya remembered that he needs to pave roads between *k* important squares that contain buildings. To pave a road, he can cover some squares of his garden with concrete. ...
The first input line contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=100, *n*Β·*m*<=≀<=200, 1<=≀<=*k*<=≀<=*min*(*n*Β·*m*,<=7)) β€” the garden's sizes and the number of the important squares. Each of the next *n* lines contains *m* numbers *a**i**j* (1<=≀<=*a**i**j*<=≀<=1000) β€” the numbers of flowers in the sq...
In the first line print the single integer β€” the minimum number of plants that die during the road construction. Then print *n* lines each containing *m* characters β€” the garden's plan. In this plan use character "X" (uppercase Latin letter X) to represent a concrete-covered square and use character "." (dot) for a squ...
[ "3 3 2\n1 2 3\n1 2 3\n1 2 3\n1 2\n3 3\n", "4 5 4\n1 4 5 1 2\n2 2 2 2 7\n2 4 1 4 5\n3 2 1 7 1\n1 1\n1 5\n4 1\n4 4\n" ]
[ "9\n.X.\n.X.\n.XX\n", "26\nX..XX\nXXXX.\nX.X..\nX.XX.\n" ]
none
[]
0
0
-1
187,581
98
Help Monks
[ "constructive algorithms" ]
D. Help Monks
2
256
In a far away kingdom is the famous Lio Shan monastery. Gods constructed three diamond pillars on the monastery's lawn long ago. Gods also placed on one pillar *n* golden disks of different diameters (in the order of the diameters' decreasing from the bottom to the top). Besides, gods commanded to carry all the disks f...
The first line contains an integer *n* β€” the number of disks (1<=≀<=*n*<=≀<=20). The second line contains *n* integers *d**i* β€” the disks' diameters after Ku Sean Sun cut their edges. The diameters are given from the bottom to the top (1<=≀<=*d**i*<=≀<=20, besides, *d**i*<=β‰₯<=*d**i*<=+<=1 for any 1<=≀<=*i*<=&lt;<=*n*).
Print on the first line number *m* β€” the smallest number of moves to solve the gods' problem. Print on the next *m* lines the description of moves: two space-separated positive integers *s**i* and *t**i* that determine the number of the pillar from which the disk is moved and the number of pillar where the disk is move...
[ "3\n3 2 1\n", "3\n3 1 1\n", "3\n3 3 3\n" ]
[ "7\n1 3\n1 2\n3 2\n1 3\n2 1\n2 3\n1 3\n", "5\n1 2\n1 2\n1 3\n2 3\n2 3\n", "5\n1 2\n1 2\n1 3\n2 3\n2 3\n" ]
Pay attention to the third test demonstrating that the order of disks should remain the same in the end, even despite the disks' same radius. If this condition was not necessary to fulfill, the gods' task could have been solved within a smaller number of moves (three β€” simply moving the three disks from the first pilla...
[]
62
0
0
187,856
573
Bear and Cavalry
[ "data structures", "divide and conquer", "dp" ]
null
null
Would you want to fight against bears riding horses? Me neither. Limak is a grizzly bear. He is general of the dreadful army of Bearland. The most important part of an army is cavalry of course. Cavalry of Bearland consists of *n* warriors and *n* horses. *i*-th warrior has strength *w**i* and *i*-th horse has streng...
The first line contains two space-separated integers, *n* and *q* (2<=≀<=*n*<=≀<=30 000, 1<=≀<=*q*<=≀<=10 000). The second line contains *n* space-separated integers, *w*1,<=*w*2,<=...,<=*w**n* (1<=≀<=*w**i*<=≀<=106) β€” strengths of warriors. The third line contains *n* space-separated integers, *h*1,<=*h*2,<=...,<=*h...
Print *q* lines with answers to queries. In *i*-th line print the maximum possible strength of cavalry after first *i* queries.
[ "4 2\n1 10 100 1000\n3 7 2 5\n2 4\n2 4\n", "3 3\n7 11 5\n3 2 1\n1 2\n1 3\n2 3\n", "7 4\n1 2 4 8 16 32 64\n87 40 77 29 50 11 18\n1 5\n2 7\n6 2\n5 6\n" ]
[ "5732\n7532\n", "44\n48\n52\n", "9315\n9308\n9315\n9315\n" ]
Clarification for the first sample: Horses:Β Β Β 3Β Β 7Β Β 2Β Β Β Β 5Β  After first query situation looks like the following: Horses:Β Β Β 3Β Β 5Β Β 2Β Β Β Β 7Β  We can get 1Β·2 + 10Β·3 + 100Β·7 + 1000Β·5 = 5732 (note that no hussar takes his own horse in this assignment). After second query we get back to initial situation and optimal ass...
[]
61
512,000
-1
188,771
435
Special Graph
[]
null
null
In this problem you will need to deal with an *n*<=Γ—<=*m* grid graph. The graph's vertices are the nodes of the *n*<=Γ—<=*m* grid. The graph's edges are all the sides and diagonals of the grid's unit squares. The figure below shows a 3<=Γ—<=5 graph. The black lines are the graph's edges, the colored circles are the grap...
The first line contains two integers *n* and *m* (2<=≀<=*n*,<=*m*<=≀<=1000). Each of the next *n* lines consists of *m* characters β€” the given graph. Each character is either Β«0Β», Β«1Β», Β«2Β», Β«3Β», Β«4Β». Character Β«0Β» means that the corresponding vertex is unpainted, otherwise the character means the color of the vertex. ...
If there is no way to get correct vertex coloring of the graph, print 0 in a single line. Otherwise print the colored *n*<=Γ—<=*m* graph. Print the graph in the same format as in the input. If multiple answers exist, print any of them.
[ "3 5\n10101\n00020\n01000\n", "2 2\n00\n00\n", "2 2\n11\n00\n" ]
[ "13131\n42424\n31313\n", "12\n34\n", "0\n" ]
The answer to the first sample is shown on the picture (1 β€” green color, 2 β€” blue, 3 β€” dark blue, 4 β€” pink). In the second sample there exists 4! answers, each of them is considered correct. In the third sample two vertices with equal colors are connected. So the correct vertex coloring couldn't be obtained.
[]
46
0
0
189,467
118
Bertown roads
[ "dfs and similar", "graphs" ]
null
null
Bertown has *n* junctions and *m* bidirectional roads. We know that one can get from any junction to any other one by the existing roads. As there were more and more cars in the city, traffic jams started to pose real problems. To deal with them the government decided to make the traffic one-directional on all the ro...
The first line contains two space-separated integers *n* and *m* (2<=≀<=*n*<=≀<=105,<=*n*<=-<=1<=≀<=*m*<=≀<=3Β·105) which represent the number of junctions and the roads in the town correspondingly. Then follow *m* lines, each containing two numbers which describe the roads in the city. Each road is determined by two in...
If there's no solution, print the single number 0. Otherwise, print *m* lines each containing two integers *p**i* and *q**i* β€” each road's orientation. That is the traffic flow will move along a one-directional road from junction *p**i* to junction *q**i*. You can print the roads in any order. If there are several solu...
[ "6 8\n1 2\n2 3\n1 3\n4 5\n4 6\n5 6\n2 4\n3 5\n", "6 7\n1 2\n2 3\n1 3\n4 5\n4 6\n5 6\n2 4\n" ]
[ "1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n4 2\n3 5\n", "0\n" ]
none
[ { "input": "6 8\n1 2\n2 3\n1 3\n4 5\n4 6\n5 6\n2 4\n3 5", "output": "6 4\n4 5\n5 6\n5 3\n3 2\n2 1\n1 3\n2 4" }, { "input": "6 7\n1 2\n2 3\n1 3\n4 5\n4 6\n5 6\n2 4", "output": "0" }, { "input": "10 19\n6 8\n5 8\n8 3\n1 9\n3 6\n4 8\n10 8\n8 7\n5 3\n10 1\n5 10\n4 10\n2 1\n3 2\n7 6\n8 2\n1 6...
92
0
0
189,764
0
none
[ "none" ]
null
null
A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains *n* ski junctions, numbered from 1 to *n*. Initially the junctions aren't connected in any way. In the constructing process *m* bidirectional...
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=105,<=1<=≀<=*m*<=≀<=105). They represent the number of junctions and the number of roads correspondingly. Then on *m* lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers *a**i* and ...
Print *m* lines: the *i*-th line should represent the number of ways to build a ski base after the end of construction of the road number *i*. The numbers should be printed modulo 1000000009 (109<=+<=9).
[ "3 4\n1 3\n2 3\n1 2\n1 2\n" ]
[ "0\n0\n1\n3\n" ]
Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this: The land lot for the construction will look in the following way: We can choose...
[ { "input": "3 4\n1 3\n2 3\n1 2\n1 2", "output": "0\n0\n1\n3" }, { "input": "15 29\n6 11\n14 3\n10 4\n14 7\n6 14\n7 15\n13 8\n10 13\n4 14\n15 8\n12 7\n3 5\n6 7\n8 1\n4 5\n11 5\n10 6\n11 3\n13 14\n7 10\n3 12\n7 14\n8 11\n7 15\n15 8\n12 7\n4 3\n9 4\n8 10", "output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n1\n1...
15
0
0
189,971
717
Heroes of Making Magic III
[ "data structures" ]
null
null
I’m strolling on sunshine, yeah-ah! And doesn’t it feel good! Well, it certainly feels good for our Heroes of Making Magic, who are casually walking on a one-directional road, fighting imps. Imps are weak and feeble creatures and they are not good at much. However, Heroes enjoy fighting them. For fun, if nothing else. ...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000), the length of the array *a*. The following line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=5<=000), the initial number of imps in each cell. The third line contains a single integer *q* (1<=≀<=*q*<=≀<=300<=000), the number of...
For each second type of query output 1 if it is possible to clear the segment, and 0 if it is not.
[ "3\n2 2 2\n3\n2 0 2\n1 1 1 1\n2 0 2\n" ]
[ "0\n1\n" ]
For the first query, one can easily check that it is indeed impossible to get from the first to the last cell while clearing everything. After we add 1 to the second position, we can clear the segment, for example by moving in the following way: <img align="middle" class="tex-formula" src="https://espresso.codeforces.c...
[]
3,000
204,800
0
190,161
862
Mahmoud and Ehab and the final stage
[ "data structures", "strings" ]
null
null
Mahmoud and Ehab solved Dr. Evil's questions so he gave them the password of the door of the evil land. When they tried to open the door using it, the door gave them a final question to solve before they leave (yes, the door is digital, Dr. Evil is modern). If they don't solve it, all the work will be useless and they ...
The first line of input contains 2 integers *n* and *q* (1<=≀<=*n*<=≀<=105,<=1<=≀<=*q*<=≀<=105) – The number of strings and the number of queries, respectively. The second line contains *n* strings *str**i* consisting of lowercase English letters. The next *q* lines describe the queries and may have one of the 2 form...
For each query of first type output its answer in a new line.
[ "5 9\nmahmoud mahmoudbadawy drmahmoud drevil mahmoud\n1 1 5\n1 1 2\n1 2 3\n2 3 mahmoud\n2 4 mahmoud\n2 2 mahmouu\n1 1 5\n1 2 3\n1 1 1\n" ]
[ "14\n14\n13\n30\n12\n7\n" ]
none
[]
46
0
0
190,308
363
Two Circles
[ "brute force", "data structures", "implementation" ]
null
null
Let's assume that we are given an *n*<=Γ—<=*m* table filled by integers. We'll mark a cell in the *i*-th row and *j*-th column as (*i*,<=*j*). Thus, (1,<=1) is the upper left cell of the table and (*n*,<=*m*) is the lower right cell. We'll assume that a circle of radius *r* with the center in cell (*i*0,<=*j*0) is a set...
The first line contains three integers *n*, *m* and *r* (2<=≀<=*n*,<=*m*<=≀<=500, *r*<=β‰₯<=0). Each of the following *n* lines contains *m* integers from 1 to 1000 each β€” the elements of the table. The rows of the table are listed from top to bottom at the elements in the rows are listed from left to right. It is guaran...
Print two integers β€” the maximum sum of numbers in the cells that are located into two non-intersecting circles and the number of pairs of non-intersecting circles with the maximum sum. If there isn't a single pair of non-intersecting circles, print 0 0.
[ "2 2 0\n1 2\n2 4\n", "5 6 1\n4 2 1 3 2 6\n2 3 2 4 7 2\n5 2 2 1 1 3\n1 4 3 3 6 4\n5 1 4 2 3 2\n", "3 3 1\n1 2 3\n4 5 6\n7 8 9\n" ]
[ "6 2\n", "34 3\n", "0 0\n" ]
none
[]
46
0
0
190,847
178
Representative Sampling
[]
null
null
The Smart Beaver from ABBYY has a long history of cooperating with the "Institute of Cytology and Genetics". Recently, the Institute staff challenged the Beaver with a new problem. The problem is as follows. There is a collection of *n* proteins (not necessarily distinct). Each protein is a string consisting of lowerc...
The first input line contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*), separated by a single space. The following *n* lines contain the descriptions of proteins, one per line. Each protein is a non-empty string of no more than 500 characters consisting of only lowercase Latin letters (a...z). Some of the strings m...
Print a single number denoting the largest possible value of representativity that a subcollection of size *k* of the given collection of proteins can have.
[ "3 2\naba\nbzd\nabq\n", "4 3\neee\nrrr\nttt\nqqq\n", "4 3\naaa\nabba\nabbc\nabbd\n" ]
[ "2\n", "0\n", "9\n" ]
none
[]
2,000
106,598,400
0
191,068
351
Jeff and Brackets
[ "dp", "matrices" ]
null
null
Jeff loves regular bracket sequences. Today Jeff is going to take a piece of paper and write out the regular bracket sequence, consisting of *nm* brackets. Let's number all brackets of this sequence from 0 to *nm* - 1 from left to right. Jeff knows that he is going to spend *a**i* *mod* *n* liters of ink on the *i*-th...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=20;Β 1<=≀<=*m*<=≀<=107; *m* is even). The next line contains *n* integers: *a*0, *a*1, ..., *a**n*<=-<=1 (1<=≀<=*a**i*<=≀<=10). The next line contains *n* integers: *b*0, *b*1, ..., *b**n*<=-<=1 (1<=≀<=*b**i*<=≀<=10). The numbers are separated by spaces.
In a single line print the answer to the problem β€” the minimum required amount of ink in liters.
[ "2 6\n1 2\n2 1\n", "1 10000000\n2\n3\n" ]
[ "12\n", "25000000\n" ]
In the first test the optimal sequence is: ()()()()()(), the required number of ink liters is 12.
[ { "input": "2 6\n1 2\n2 1", "output": "12" }, { "input": "1 10000000\n2\n3", "output": "25000000" }, { "input": "3 184\n3 2 8\n3 9 2", "output": "1288" }, { "input": "4 26\n10 2 5 9\n5 4 2 5", "output": "444" }, { "input": "3 76\n4 7 9\n10 1 1", "output": "684...
92
0
0
191,710
0
none
[ "none" ]
null
null
It's Piegirl's birthday soon, and Pieguy has decided to buy her a bouquet of flowers and a basket of chocolates. The flower shop has *F* different types of flowers available. The *i*-th type of flower always has exactly *p**i* petals. Pieguy has decided to buy a bouquet consisting of exactly *N* flowers. He may buy th...
The first line of input will contain integers *F*, *B*, and *N* (1<=≀<=*F*<=≀<=10,<=1<=≀<=*B*<=≀<=100,<=1<=≀<=*N*<=≀<=1018), the number of types of flowers, the number of types of boxes, and the number of flowers that must go into the bouquet, respectively. The second line of input will contain *F* integers *p*1,<=*p*...
Print the number of bouquet+basket combinations Pieguy can buy, modulo 1000000007<==<=109<=+<=7.
[ "2 3 3\n3 5\n10 3 7\n", "6 5 10\n9 3 3 4 9 9\n9 9 1 6 4\n" ]
[ "17\n", "31415926\n" ]
In the first example, there is 1 way to make a bouquet with 9 petals (3 + 3 + 3), and 1 way to make a basket with 9 pieces of chocolate (3 + 3 + 3), for 1 possible combination. There are 3 ways to make a bouquet with 13 petals (3 + 5 + 5, 5 + 3 + 5, 5 + 5 + 3), and 5 ways to make a basket with 13 pieces of chocolate (3...
[]
46
0
0
191,888
0
none
[ "none" ]
null
null
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya wonders eagerly what minimum lucky number has the sum of digits equal to *n*. Help him cope wi...
The single line contains an integer *n* (1<=≀<=*n*<=≀<=106) β€” the sum of digits of the required lucky number.
Print on the single line the result β€” the minimum lucky number, whose sum of digits equals *n*. If such number does not exist, print -1.
[ "11\n", "10\n" ]
[ "47\n", "-1\n" ]
none
[ { "input": "11", "output": "47" }, { "input": "10", "output": "-1" }, { "input": "64", "output": "4477777777" }, { "input": "1", "output": "-1" }, { "input": "4", "output": "4" }, { "input": "7", "output": "7" }, { "input": "12", "outpu...
92
4,812,800
0
192,111
273
Dima and Game
[ "dp", "games" ]
null
null
Dima and Anya love playing different games. Now Dima has imagined a new game that he wants to play with Anya. Dima writes *n* pairs of integers on a piece of paper (*l**i*,<=*r**i*) (1<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=*p*). Then players take turns. On his turn the player can do the following actions: 1. choose the numbe...
The first line contains two integers *n*, *p* (1<=≀<=*n*<=≀<=1000,<=1<=≀<=*p*<=≀<=109). The numbers are separated by a single space.
In a single line print the remainder after dividing the answer to the problem by number 1000000007Β (109<=+<=7).
[ "2 2\n", "4 4\n", "100 1000\n" ]
[ "0\n", "520\n", "269568947\n" ]
none
[]
92
0
0
192,315
48
The Race
[ "math" ]
C. The Race
2
256
Every year a race takes place on the motorway between cities A and B. This year Vanya decided to take part in the race and drive his own car that has been around and bears its own noble name β€” The Huff-puffer. So, Vasya leaves city A on the Huff-puffer, besides, at the very beginning he fills the petrol tank with Ξ± li...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=1000) which represents the number of petrol stations where Vanya has stopped. The next line has *n* space-separated integers which represent the numbers of the stations. The numbers are positive and do not exceed 106, they are given in the increasing order. No two n...
Print in the first line "unique" (without quotes) if the answer can be determined uniquely. In the second line print the number of the station where the next stop will take place. If the answer is not unique, print in the first line "not unique".
[ "3\n1 2 4\n", "2\n1 2\n" ]
[ "unique\n5\n", "not unique\n" ]
In the second example the answer is not unique. For example, if α = 10, we'll have such a sequence as 1, 2, 3, and if α = 14, the sequence will be 1, 2, 4.
[ { "input": "3\n1 2 4", "output": "unique\n5" }, { "input": "2\n1 2", "output": "not unique" }, { "input": "1\n5", "output": "not unique" }, { "input": "3\n1 3 4", "output": "unique\n6" }, { "input": "5\n1 2 3 5 6", "output": "unique\n7" }, { "input": "...
124
819,200
3.967474
192,856
0
none
[ "none" ]
null
null
Π”Π°Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ Π½Π΅ΠΎΡ‚Ρ€ΠΈΡ†Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ число *k* ΠΈ *n* Π½Π΅ΠΎΡ‚Ρ€ΠΈΡ†Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ†Π΅Π»Ρ‹Ρ… чисСл *a*1,<=*a*2,<=...,<=*a**n*. Записывая Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΈΠ· этих чисСл Π΄Ρ€ΡƒΠ³ Π·Π° Π΄Ρ€ΡƒΠ³ΠΎΠΌ Π² ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½ΠΎΠΌ порядкС ΠΈ, Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ ΠΊΠ°ΠΊΠΈΠ΅-Ρ‚ΠΎ ΠΈΠ· Π½ΠΈΡ… нСсколько Ρ€Π°Π· (Π° ΠΊΠ°ΠΊΠΈΠ΅-Ρ‚ΠΎ Π²ΠΎΠΎΠ±Ρ‰Π΅ Π½Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ), трСбуСтся ΡΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ ΠΊΡ€Π°Ρ‚Ρ‡Π°ΠΉΡˆΠ΅Π΅ (наимСньшСС ΠΏΠΎ количСству Ρ†ΠΈΡ„Ρ€)...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС содСрТится Π΄Π²Π° Ρ†Π΅Π»Ρ‹Ρ… числа *n* (1<=≀<=*n*<=≀<=1<=000<=000) ΠΈ *k* (1<=≀<=*k*<=≀<=1000)Β β€” количСство чисСл ΠΈ Ρ‚Ρ€Π΅Π±ΡƒΠ΅ΠΌΡ‹ΠΉ Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ соотвСтствСнно. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС содСрТится *n* Ρ†Π΅Π»Ρ‹Ρ… чисСл *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109).
Если ΠΎΡ‚Π²Π΅Ρ‚ сущСствуСт, Π² ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ Β«YESΒ» (Π±Π΅Π· ΠΊΠ°Π²Ρ‹Ρ‡Π΅ΠΊ), Π° Π²ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС — искомоС ΠΊΡ€Π°Ρ‚Ρ‡Π°ΠΉΡˆΠ΅Π΅ число Π±Π΅Π· Π²Π΅Π΄ΡƒΡ‰ΠΈΡ… Π½ΡƒΠ»Π΅ΠΉ. Π’ случаС Ссли ΠΎΡ‚Π²Π΅Ρ‚Π° Π½Π΅ сущСствуСт, Π² СдинствСнной строкС Π²Ρ‹Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ Β«NOΒ» (Π±Π΅Π· ΠΊΠ°Π²Ρ‹Ρ‡Π΅ΠΊ).
[ "2 3\n123 1\n", "1 10\n1\n", "3 4\n1 2 3\n", "3 777\n12 23 345\n" ]
[ "YES\n123", "NO\n", "YES\n12", "YES\n121212" ]
none
[]
2,000
4,915,200
0
193,234
877
Danil and a Part-time Job
[ "bitmasks", "data structures", "trees" ]
null
null
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher. Danil works in a rooted tree (undirected connected acyclic graph) with *n* vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each ...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000) β€” the number of vertices in the tree. The second line contains *n*<=-<=1 space-separated integers *p*2,<=*p*3,<=...,<=*p**n* (1<=≀<=*p**i*<=&lt;<=*i*), where *p**i* is the ancestor of vertex *i*. The third line contains *n* space-separated integers...
For each task get v print the number of rooms in the subtree of *v*, in which the light is turned on.
[ "4\n1 1 1\n1 0 0 1\n9\nget 1\nget 2\nget 3\nget 4\npow 1\nget 1\nget 2\nget 3\nget 4\n" ]
[ "2\n0\n0\n1\n2\n1\n1\n0\n" ]
<img class="tex-graphics" src="https://espresso.codeforces.com/839c4a0a06cc547ffb8d937bfe52730b51c842b4.png" style="max-width: 100.0%;max-height: 100.0%;"/> The tree after the task pow 1.
[ { "input": "4\n1 1 1\n1 0 0 1\n9\nget 1\nget 2\nget 3\nget 4\npow 1\nget 1\nget 2\nget 3\nget 4", "output": "2\n0\n0\n1\n2\n1\n1\n0" }, { "input": "1\n\n1\n4\npow 1\nget 1\npow 1\nget 1", "output": "0\n1" }, { "input": "10\n1 2 3 4 2 4 1 7 8\n1 1 0 1 1 0 0 0 1 1\n10\npow 1\nget 2\npow 2\...
2,000
4,096,000
0
194,442
232
Quick Tortoise
[ "bitmasks", "divide and conquer", "dp" ]
null
null
John Doe has a field, which is a rectangular table of size *n*<=Γ—<=*m*. We assume that the field rows are numbered from 1 to *n* from top to bottom, and the field columns are numbered from 1 to *m* from left to right. Then the cell of the field at the intersection of the *x*-th row and the *y*-th column has coordinates...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=500) β€” the field sizes. Each of the next *n* lines contains *m* characters "#" and ".": the *j*-th character of the *i*-th line equals "#", if the cell (*i*; *j*) is painted black and ".", if it is painted white. The next line conta...
For each of *q* queries print on a single line "Yes", if there is a way from cell (*x*1; *y*1) to cell (*x*2; *y*2), that meets the requirements, and "No" otherwise. Print the answers to the queries in the order, in which the queries are given in the input.
[ "3 3\n...\n.##\n.#.\n5\n1 1 3 3\n1 1 1 3\n1 1 3 1\n1 1 1 2\n1 1 2 1\n", "5 5\n.....\n.###.\n.....\n.###.\n.....\n5\n1 1 5 5\n1 1 1 5\n1 1 3 4\n2 1 2 5\n1 1 2 5\n" ]
[ "No\nYes\nYes\nYes\nYes\n", "Yes\nYes\nYes\nNo\nYes\n" ]
none
[]
92
0
0
194,518
431
Chemistry Experiment
[ "binary search", "data structures", "ternary search" ]
null
null
One day two students, Grisha and Diana, found themselves in the university chemistry lab. In the lab the students found *n* test tubes with mercury numbered from 1 to *n* and decided to conduct an experiment. The experiment consists of *q* steps. On each step, one of the following actions occurs: 1. Diana pours all ...
The first line contains two integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=105) β€” the number of tubes ans the number of experiment steps. The next line contains *n* space-separated integers: *h*1,<=*h*2,<=...,<=*h**n* (0<=≀<=*h**i*<=≀<=109), where *h**i* is the volume of mercury in the *Ρ–*-th tube at the beginning of the ex...
For each action of the second type print the calculated value. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=4.
[ "3 3\n1 2 0\n2 2\n1 2 1\n2 3\n", "4 5\n1 3 0 1\n2 3\n2 1\n1 3 2\n2 3\n2 4\n" ]
[ "1.50000\n1.66667\n", "1.66667\n1.00000\n2.33333\n2.66667\n" ]
none
[]
46
0
0
195,688
894
Ralph And His Tour in Binary Country
[ "brute force", "data structures", "trees" ]
null
null
Ralph is in the Binary Country. The Binary Country consists of *n* cities and (*n*<=-<=1) bidirectional roads connecting the cities. The roads are numbered from 1 to (*n*<=-<=1), the *i*-th road connects the city labeled (here ⌊ *x*βŒ‹ denotes the *x* rounded down to the nearest integer) and the city labeled (*i*<=+<=1)...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=106, 1<=≀<=*m*<=≀<=105). (*n*<=-<=1) lines follow, each line contains one integer *L**i* (1<=≀<=*L**i*<=≀<=105), which denotes the length of the *i*-th road. *m* lines follow, each line contains two integers *A**i* and *H**i* (1<=≀<=*A**i*<=≀<=*n*, 0<=≀<...
Print *m* lines, on the *i*-th line print one integerΒ β€” the answer for the *i*-th query.
[ "2 2\n5\n1 8\n2 4\n", "6 4\n2\n1\n1\n3\n2\n2 4\n1 3\n3 2\n1 7\n" ]
[ "11\n4\n", "11\n6\n3\n28\n" ]
Here is the explanation for the second sample. Ralph's first query is to start tours from city 2 and *H*<sub class="lower-index">*i*</sub> equals to 4. Here are the options: - He can choose city 5 as his terminal city. Since the distance between city 5 and city 2 is 3, he can gain happiness 4 - 3 = 1. - He can choo...
[ { "input": "2 2\n5\n1 8\n2 4", "output": "11\n4" }, { "input": "6 4\n2\n1\n1\n3\n2\n2 4\n1 3\n3 2\n1 7", "output": "11\n6\n3\n28" }, { "input": "8 1\n21725\n80273\n97276\n78838\n78474\n1896\n6570\n7 5267977", "output": "41283845" }, { "input": "4 2\n56025\n27554\n51024\n2 473...
2,500
9,113,600
0
195,768
819
Mister B and Flight to the Moon
[ "constructive algorithms", "graphs" ]
null
null
In order to fly to the Moon Mister B just needs to solve the following problem. There is a complete indirected graph with *n* vertices. You need to cover it with several simple cycles of length 3 and 4 so that each edge is in exactly 2 cycles. We are sure that Mister B will solve the problem soon and will fly to the ...
The only line contains single integer *n* (3<=≀<=*n*<=≀<=300).
If there is no answer, print -1. Otherwise, in the first line print *k* (1<=≀<=*k*<=≀<=*n*2)Β β€” the number of cycles in your solution. In each of the next *k* lines print description of one cycle in the following format: first print integer *m* (3<=≀<=*m*<=≀<=4)Β β€” the length of the cycle, then print *m* integers *v*1,...
[ "3\n", "5\n" ]
[ "2\n3 1 2 3\n3 1 2 3\n", "6\n3 5 4 2\n3 3 1 5\n4 4 5 2 3\n4 4 3 2 1\n3 4 2 1\n3 3 1 5\n" ]
none
[ { "input": "3", "output": "2\n3 1 2 3\n3 1 2 3" }, { "input": "5", "output": "6\n3 1 2 3\n3 2 3 4\n3 3 4 5\n3 4 5 1\n4 2 1 3 5\n4 5 1 4 2" }, { "input": "299", "output": "22350\n4 2 3 1 4\n4 1 4 299 5\n4 299 5 298 6\n4 298 6 297 7\n4 297 7 296 8\n4 296 8 295 9\n4 295 9 294 10\n4 294 ...
46
5,120,000
0
196,236
336
Vasily the Bear and Beautiful Strings
[ "combinatorics", "math", "number theory" ]
null
null
Vasily the Bear loves beautiful strings. String *s* is beautiful if it meets the following criteria: 1. String *s* only consists of characters 0 and 1, at that character 0 must occur in string *s* exactly *n* times, and character 1 must occur exactly *m* times. 1. We can obtain character *g* from string *s* with so...
The first line of the input contains three space-separated integers *n*,<=*m*,<=*g* (0<=≀<=*n*,<=*m*<=≀<=105,<=*n*<=+<=*m*<=β‰₯<=1,<=0<=≀<=*g*<=≀<=1).
Print a single integer β€” the answer to the problem modulo 1000000007 (109<=+<=7).
[ "1 1 0\n", "2 2 0\n", "1 1 1\n" ]
[ "2\n", "4\n", "0\n" ]
In the first sample the beautiful strings are: "01", "10". In the second sample the beautiful strings are: "0011", "1001", "1010", "1100". In the third sample there are no beautiful strings.
[ { "input": "1 1 0", "output": "2" }, { "input": "2 2 0", "output": "4" }, { "input": "1 1 1", "output": "0" }, { "input": "100000 0 1", "output": "1" }, { "input": "0 100000 1", "output": "0" }, { "input": "0 100000 0", "output": "1" }, { "...
280
2,867,200
0
196,485
93
Azembler
[ "brute force", "implementation" ]
C. Azembler
5
256
After the Search Ultimate program that searched for strings in a text failed, Igor K. got to think: "Why on Earth does my program work so slowly?" As he double-checked his code, he said: "My code contains no errors, yet I know how we will improve Search Ultimate!" and took a large book from the shelves. The book read "...
The input data contain the only integer *n* (1<=≀<=*n*<=≀<=255), which Igor K. is about to multiply.
On the first line print number *p*, which represents the minimum number of lea operations, needed to do that. Then print the program consisting of *p* commands, performing the operations. It is guaranteed that such program exists for any *n* from 1 to 255. Use precisely the following format of commands (here *k* is eq...
[ "41\n", "2\n", "4\n" ]
[ "2\nlea ebx, [eax + 4*eax]\nlea ecx, [eax + 8*ebx]\n", "1\nlea ebx, [eax + eax]\n", "1\nlea ebx, [4*eax]\n" ]
none
[ { "input": "41", "output": "2\nlea ebx, [eax + 4*eax]\nlea ecx, [eax + 8*ebx]" }, { "input": "2", "output": "1\nlea ebx, [eax + eax]" }, { "input": "4", "output": "1\nlea ebx, [4*eax]" }, { "input": "6", "output": "2\nlea ebx, [eax + 4*eax]\nlea ecx, [ebx + eax]" }, {...
92
0
0
197,081
696
...Wait for it...
[ "data structures", "dsu", "trees" ]
null
null
Barney is searching for his dream girl. He lives in NYC. NYC has *n* junctions numbered from 1 to *n* and *n*<=-<=1 roads connecting them. We will consider the NYC as a rooted tree with root being junction 1. *m* girls live in NYC, *i*-th of them lives along junction *c**i* and her weight initially equals *i* pounds. ...
The first line of input contains three integers *n*, *m* and *q* (1<=≀<=*n*,<=*m*,<=*q*<=≀<=105)Β β€” the number of junctions in NYC, the number of girls living in NYC and the number of events respectively. The next *n*<=-<=1 lines describes the roads. Each line contains two integers *v* and *u* (1<=≀<=*v*,<=*u*<=≀<=*n*,...
For each event of the first type, print number *t* and then *t* integers *g*1,<=*g*2,<=...,<=*g**t* in one line, meaning that in this event Barney will invite *t* girls whose indices are *g*1,<=...,<=*g**t* in the order from the best to the worst according to Barney's considerations.
[ "5 7 11\n3 5\n2 3\n4 3\n1 4\n4 1 4 5 4 1 4\n2 4 3\n1 2 1 2\n1 4 2 1\n2 2 10\n2 1 10\n1 2 4 1\n1 2 3 4\n2 5 2\n2 4 9\n1 3 5 2\n1 1 2 3\n" ]
[ "2 2 1 \n1 3 \n1 5 \n0 \n1 4 \n2 6 7 \n" ]
For the first sample case: Description of events: 1. Weights of girls in subtree of junction 4 increase by 3. These girls have IDs: 1, 3, 5, 4, 7. 1. Barney goes from junction 2 to 1. Girls on his way have IDs 1, 2, 3, 5, 6, 7 with weights 4, 2, 6, 8, 6, 10 respectively. So, he invites girls 2 and 1. 1. Barney goe...
[]
30
0
0
197,234
923
Public Service
[ "constructive algorithms", "graphs", "trees" ]
null
null
There are *N* cities in Bob's country connected by roads. Some pairs of cities are connected by public transport. There are two competing transport companiesΒ β€” Boblines operating buses and Bobrail running trains. When traveling from *A* to *B*, a passenger always first selects the mode of transport (either bus or train...
The first line contains an integer *N* (2<=≀<=*N*<=≀<=10000), the number of cities. *N*<=-<=1 lines follow, representing the network plan of Boblines. Each contains two integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*N*), meaning that there is a bus route between cities *u* and *v*. *N*<=-<=1 lines follow, representing th...
If there is no solution, output a single line with the word "No". If a solution exists, output two lines. On the first line, there should be the word "Yes". On the second line, there should be *N* integers *P*1,<=*P*2,<=...,<=*P**N* (*N*<=+<=1<=≀<=*P**i*<=≀<=2*N*)Β β€” the mapping between the two numbering schemes. More ...
[ "4\n1 2\n2 3\n3 4\n5 6\n6 7\n7 8\n", "4\n1 2\n2 3\n3 4\n5 6\n5 7\n5 8\n", "7\n1 2\n1 3\n1 4\n1 5\n5 6\n6 7\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n" ]
[ "Yes\n6 8 5 7\n", "No\n", "Yes\n9 14 11 12 13 10 8\n" ]
The first sample (bus lines in red and rail lines in blue): <img class="tex-graphics" src="https://espresso.codeforces.com/efe8f12cad3807c409243e2b6c9e13afe2fc9973.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[]
31
0
0
197,874
178
Greedy Merchants
[]
null
null
In ABBYY a wonderful Smart Beaver lives. This time, he began to study history. When he read about the Roman Empire, he became interested in the life of merchants. The Roman Empire consisted of *n* cities numbered from 1 to *n*. It also had *m* bidirectional roads numbered from 1 to *m*. Each road connected two differe...
The first input line contains two integers *n* and *m*, separated by a space, *n* is the number of cities, and *m* is the number of roads in the empire. The following *m* lines contain pairs of integers *a**i*, *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*,<=*a**i*<=β‰ <=*b**i*), separated by a space β€” the numbers of cities con...
Print exactly *k* lines, the *i*-th line should contain a single integer *d**i* β€” the number of dinars that the *i*-th merchant paid.
[ "7 8\n1 2\n2 3\n3 4\n4 5\n5 6\n5 7\n3 5\n4 7\n4\n1 5\n2 4\n2 6\n4 7\n" ]
[ "2\n1\n2\n0\n" ]
The given sample is illustrated in the figure below. Let's describe the result for the first merchant. The merchant's warehouse is located in city 1 and his shop is in city 5. Let us note that if either road, (1, 2) or (2, 3) is destroyed, there won't be any path between cities 1 and 5 anymore. If any other road is d...
[]
92
512,000
0
198,362
914
Sum the Fibonacci
[ "bitmasks", "divide and conquer", "dp", "fft", "math" ]
null
null
You are given an array *s* of *n* non-negative integers. A 5-tuple of integers (*a*,<=*b*,<=*c*,<=*d*,<=*e*) is said to be valid if it satisfies the following conditions: - 1<=≀<=*a*,<=*b*,<=*c*,<=*d*,<=*e*<=≀<=*n* - (*s**a* | *s**b*) &amp; *s**c* &amp; (*s**d* ^ *s**e*)<==<=2*i* for some integer *i* - *s**a* &am...
The first line of input contains an integer *n* (1<=≀<=*n*<=≀<=106). The second line of input contains *n* integers *s**i* (0<=≀<=*s**i*<=&lt;<=217).
Output the sum as described above, modulo 109<=+<=7
[ "2\n1 2\n", "3\n7 4 1\n", "10\n1 3 0 7 3 7 6 5 7 5\n", "10\n50 9 11 44 39 40 5 39 23 7\n" ]
[ "32\n", "3520\n", "1235424\n", "113860062\n" ]
none
[ { "input": "2\n1 2", "output": "32" }, { "input": "3\n7 4 1", "output": "3520" }, { "input": "10\n1 3 0 7 3 7 6 5 7 5", "output": "1235424" }, { "input": "10\n50 9 11 44 39 40 5 39 23 7", "output": "113860062" }, { "input": "10\n4 4 3 1 5 1 3 6 4 4", "output":...
46
0
0
198,538
773
Dynamic Problem Scoring
[ "brute force", "greedy" ]
null
null
Vasya and Petya take part in a Codeforces round. The round lasts for two hours and contains five problems. For this round the dynamic problem scoring is used. If you were lucky not to participate in any Codeforces round with dynamic problem scoring, here is what it means. The maximum point value of the problem depends...
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=120)Β β€” the number of round participants, including Vasya and Petya. Each of the next *n* lines contains five integers *a**i*,<=1,<=*a**i*,<=2...,<=*a**i*,<=5 (<=-<=1<=≀<=*a**i*,<=*j*<=≀<=119)Β β€” the number of minutes passed between the beginning of the round a...
Output a single integerΒ β€” the number of new accounts Vasya needs to beat Petya, or -1 if Vasya can't achieve his goal.
[ "2\n5 15 40 70 115\n50 45 40 30 15\n", "3\n55 80 10 -1 -1\n15 -1 79 60 -1\n42 -1 13 -1 -1\n", "5\n119 119 119 119 119\n0 0 0 0 -1\n20 65 12 73 77\n78 112 22 23 11\n1 78 60 111 62\n", "4\n-1 20 40 77 119\n30 10 73 50 107\n21 29 -1 64 98\n117 65 -1 -1 -1\n" ]
[ "2\n", "3\n", "27\n", "-1\n" ]
In the first example, Vasya's optimal strategy is to submit the solutions to the last three problems from two new accounts. In this case the first two problems will have the maximum point value of 1000, while the last three problems will have the maximum point value of 500. Vasya's score will be equal to 980 + 940 + 42...
[ { "input": "2\n5 15 40 70 115\n50 45 40 30 15", "output": "2" }, { "input": "3\n55 80 10 -1 -1\n15 -1 79 60 -1\n42 -1 13 -1 -1", "output": "3" }, { "input": "5\n119 119 119 119 119\n0 0 0 0 -1\n20 65 12 73 77\n78 112 22 23 11\n1 78 60 111 62", "output": "27" }, { "input": "4\...
124
0
3
199,223
71
Solitaire
[ "brute force", "implementation" ]
D. Solitaire
1
256
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out *nm* cards as a rectangle *n*<=Γ—<=*m*. If there are jokers among them, then Vasya should change them with some of the rest of 54<=-<=*nm* ca...
The first line contains integers *n* and *m* (3<=≀<=*n*,<=*m*<=≀<=17, *n*<=Γ—<=*m*<=≀<=52). Next *n* lines contain *m* words each. Each word consists of two letters. The jokers are defined as "J1" and "J2" correspondingly. For the rest of the cards, the first letter stands for the rank and the second one β€” for the suit....
If the Solitaire can be solved, print on the first line "Solution exists." without the quotes. On the second line print in what way the jokers can be replaced. Three variants are possible: - "There are no jokers.", if there are no jokers in the input data.- "Replace J*x* with *y*.", if there is one joker. *x* is its...
[ "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H 5S TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C\n", "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H J1 TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C\n", "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H QC TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C\n" ]
[ "No solution.", "Solution exists.\nReplace J1 with 2H.\nPut the first square to (1, 1).\nPut the second square to (2, 4).\n", "Solution exists.\nThere are no jokers.\nPut the first square to (1, 1).\nPut the second square to (2, 4).\n" ]
The pretests cover all the possible output formats.
[ { "input": "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H 5S TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C", "output": "No solution." }, { "input": "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H J1 TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C", "output": "Solution exists.\nReplace J1 with 2H.\nPut the first square to (1, 1)...
46
0
0
201,627
504
Misha and LCP on Tree
[ "binary search", "dfs and similar", "hashing", "string suffix structures", "trees" ]
null
null
Misha has a tree with characters written on the vertices. He can choose two vertices *s* and *t* of this tree and write down characters of vertices lying on a path from *s* to *t*. We'll say that such string corresponds to pair (*s*,<=*t*). Misha has *m* queries of type: you are given 4 vertices *a*, *b*, *c*, *d*; yo...
The first line contains integer *n* (1<=≀<=*n*<=≀<=300<=000) β€” the number of vertices in the tree. Next follows a line consisting of *n* small English letters. The *i*-th character of the string corresponds to the character written on the *i*-th vertex. Next *n*<=-<=1 lines contain information about edges. An edge i...
For each query print the length of the largest common prefix on a separate line.
[ "6\nbbbabb\n2 1\n3 2\n4 3\n5 2\n6 5\n6\n2 5 3 1\n1 5 2 3\n5 6 5 6\n6 3 4 1\n6 2 3 4\n2 2 4 5\n" ]
[ "2\n2\n2\n0\n1\n0\n" ]
none
[]
31
0
0
201,936
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...
62
0
0
202,457
431
Random Task
[ "binary search", "bitmasks", "combinatorics", "dp", "math" ]
null
null
One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: "Find such positive integer *n*, that among numbers *n*<=+<=1, *n*<=+<=2, ..., 2Β·*n* there are exactly *m* numbers which binary representation contains exactly *k* digits one". The girl got int...
The first line contains two space-separated integers, *m* and *k* (0<=≀<=*m*<=≀<=1018; 1<=≀<=*k*<=≀<=64).
Print the required number *n* (1<=≀<=*n*<=≀<=1018). If there are multiple answers, print any of them.
[ "1 1\n", "3 2\n" ]
[ "1\n", "5\n" ]
none
[ { "input": "1 1", "output": "1" }, { "input": "3 2", "output": "5" }, { "input": "3 3", "output": "7" }, { "input": "1 11", "output": "1024" }, { "input": "4 20", "output": "983040" }, { "input": "45902564 24", "output": "6406200698" }, { "...
61
0
0
202,850
200
Programming Language
[ "binary search", "brute force", "expression parsing", "implementation" ]
null
null
Recently, Valery have come across an entirely new programming language. Most of all the language attracted him with template functions and procedures. Let us remind you that templates are tools of a language, designed to encode generic algorithms, without reference to some parameters (e.g., data types, buffer sizes, de...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1000) β€” the number of template procedures. The next *n* lines contain the description of the procedures specified in the following format: "void procedureName (type_1, type_2, ..., type_t)" (1<=≀<=*t*<=≀<=5), where void is the keyword, procedureName is the pr...
On each of *k* lines print a single number, where the *i*-th number stands for the number of suitable template procedures for the *i*-th call.
[ "4\nvoid f(int,T)\nvoid f(T, T)\n void foo123 ( int, double, string,string ) \n void p(T,double)\n3\nint a\n string s\ndouble x123 \n5\nf(a, a)\n f(s,a )\nfoo (a,s,s)\n f ( s ,x123)\nproc(a)\n", "6\nvoid f(string,double,int)\nvoid f(int)\n void f ( T )\nvoid procedure(int,double)\nvoid f ...
[ "2\n1\n0\n1\n0\n", "1\n3\n0\n0\n2\n" ]
none
[ { "input": "4\nvoid f(int,T)\nvoid f(T, T)\n void foo123 ( int, double, string,string ) \n void p(T,double)\n3\nint a\n string s\ndouble x123 \n5\nf(a, a)\n f(s,a )\nfoo (a,s,s)\n f ( s ,x123)\nproc(a)", "output": "2\n1\n0\n1\n0" }, { "input": "6\nvoid f(string,double,int)\nvoid ...
30
102,400
0
203,181
81
Pairs
[ "dfs and similar", "dp", "dsu", "graphs", "implementation", "trees" ]
E. Pairs
1
256
There are *n* students in Polycarp's class (including himself). A few days ago all students wrote an essay "My best friend". Each student's essay was dedicated to one of the students of class, to his/her best friend. Note that student *b*'s best friend is not necessarily student *a*, if *a*'s best friend is *b*. And n...
The first line contains an integer *n* (2<=≀<=*n*<=≀<=105), *n* is the number of students per class. Next, *n* lines contain information about the students, one per line. Each line contains two integers *f**i*,<=*s**i* (1<=≀<=*f**i*<=≀<=*n*,<=*f**i*<=β‰ <=*i*,<=1<=≀<=*s**i*<=≀<=2), where *f**i* is the number of *i*-th st...
Print on the first line two numbers *t*, *e*, where *t* is the maximum number of formed pairs, and *e* is the maximum number of boy-girl type pairs among them. Then print *t* lines, each line must contain a pair *a**i*,<=*b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*), they are numbers of pupils in the *i*-th pair. Print the pa...
[ "5\n5 2\n3 2\n5 1\n2 1\n4 2\n", "6\n5 2\n3 2\n5 1\n2 1\n4 2\n3 1\n", "8\n2 2\n3 2\n5 1\n3 1\n6 1\n5 1\n8 2\n7 1\n" ]
[ "2 2\n5 3\n4 2\n", "3 1\n4 2\n5 1\n3 6\n", "4 1\n5 6\n3 4\n2 1\n7 8\n" ]
The picture corresponds to the first sample. On the picture rhomb stand for boys, squares stand for girls, arrows lead from a pupil to his/her best friend. Bold non-dashed arrows stand for pairs in the answer.
[]
30
102,400
0
205,217
587
Duff is Mad
[ "data structures", "strings" ]
null
null
Duff is mad at her friends. That's why she sometimes makes Malek to take candy from one of her friends for no reason! She has *n* friends. Her *i*-th friend's name is *s**i* (their names are not necessarily unique). *q* times, she asks Malek to take candy from her friends. She's angry, but also she acts with rules. Wh...
The first line of input contains two integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=105). The next *n* lines contain the names. *i*-th of them contains an string *s**i*, consisting of lowercase English letters (). The next *q* lines contain the requests. Each of them contains three integers, *l*,<=*r* and *k* (says that...
Print the answer to each request in one line.
[ "5 5\na\nab\nabab\nababab\nb\n1 5 4\n3 5 4\n1 5 2\n1 5 3\n1 4 1\n" ]
[ "12\n6\n3\n7\n1\n" ]
none
[]
31
0
0
205,520
109
Lucky Sorting
[ "constructive algorithms", "sortings" ]
D. Lucky Sorting
3
256
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya got an array consisting of *n* numbers, it is the gift for his birthday. Now he wants to sort ...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of elements in the array. The second line contains *n* positive integers, not exceeding 109 β€” the array that needs to be sorted in the non-decreasing order.
On the first line print number *k* (0<=≀<=*k*<=≀<=2*n*) β€” the number of the swaps in the sorting. On the following *k* lines print one pair of distinct numbers (a pair per line) β€” the indexes of elements to swap. The numbers in the array are numbered starting from 1. If it is impossible to sort the given sequence, prin...
[ "2\n4 7\n", "3\n4 2 1\n", "7\n77 66 55 44 33 22 11\n" ]
[ "0\n", "1\n1 3\n", "7\n1 7\n7 2\n2 6\n6 7\n3 4\n5 3\n4 5\n" ]
none
[ { "input": "2\n4 7", "output": "0" }, { "input": "3\n4 2 1", "output": "1\n1 3" }, { "input": "7\n77 66 55 44 33 22 11", "output": "9\n4 7\n1 7\n1 6\n2 6\n2 5\n3 5\n2 3\n1 2\n1 4" }, { "input": "7\n1 2 3 4 5 6 7", "output": "0" }, { "input": "4\n47 1 7 2", "ou...
216
307,200
0
206,132
516
Drazil and His Happy Friends
[ "math", "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*<=≀<=109). The second line contains integer *b* (0<=≀<=*b*<=≀<=*min*(*n*,<=105)), 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 i...
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
[ "2 3\n0\n1 0\n", "2 4\n1 0\n1 2\n", "2 3\n1 0\n1 1\n", "99999 100000\n2 514 415\n2 50216 61205\n" ]
[ "4\n", "-1\n", "2\n", "4970100515\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...
[]
4,000
4,608,000
0
206,288
633
Startup Funding
[ "binary search", "constructive algorithms", "data structures", "probabilities", "two pointers" ]
null
null
An e-commerce startup pitches to the investors to get funding. They have been functional for *n* weeks now and also have a website! For each week they know the number of unique visitors during this week *v**i* and the revenue *c**i*. To evaluate the potential of the startup at some range of weeks from *l* to *r* inclu...
The first line of the input contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=1<=000<=000). The second line contains *n* integers *v**i* (1<=≀<=*v**i*<=≀<=107)Β β€” the number of unique visitors during each week. The third line contains *n* integers *c**i* (1<=≀<=*c**i*<=≀<=107)Β β€”the revenue for each week.
Print a single real valueΒ β€” the expected grade of the startup. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "3 2\n3 2 1\n300 200 300\n" ]
[ "133.3333333\n" ]
Consider the first sample. If the investors ask for *l*<sub class="lower-index">*i*</sub> = 1 onwards, startup will choose *r*<sub class="lower-index">*i*</sub> = 1, such that max number of visitors is 3 and minimum revenue is 300. Thus, potential in this case is *min*(3Β·100, 300) = 300. If the investors ask for *l*<...
[]
0
0
-1
206,328
201
Brand New Problem
[ "bitmasks", "brute force", "dp" ]
null
null
A widely known among some people Belarusian sport programmer Lesha decided to make some money to buy a one square meter larger flat. To do this, he wants to make and carry out a Super Rated Match (SRM) on the site Torcoder.com. But there's a problem β€” a severe torcoder coordinator Ivan does not accept any Lesha's probl...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=15) β€” the number of words in Lesha's problem. The second line contains *n* space-separated words β€” the short description of the problem. The third line contains a single integer *m* (1<=≀<=*m*<=≀<=10) β€” the number of problems in the Torcoder.com archive. Next...
If Lesha's problem is brand new, print string "Brand new problem!" (without quotes). Otherwise, on the first line print the index of the archive problem which resembles Lesha's problem most. If there are multiple such problems, print the one with the smallest index. On the second line print a string consisting of cha...
[ "4\nfind the next palindrome\n1\n10 find the previous palindrome or print better luck next time\n", "3\nadd two numbers\n3\n1 add\n2 two two\n3 numbers numbers numbers\n", "4\nthese papers are formulas\n3\n6 what are these formulas and papers\n5 papers are driving me crazy\n4 crazy into the night\n", "3\nadd ...
[ "1\n[:||||||:]\n", "Brand new problem!\n", "1\n[:||||:]\n", "3\n[:|||:]\n" ]
Let us remind you that the number of inversions is the number of pairs of words that follow in the permutation not in their original order. Thus, for example, if the original problem is "add two numbers", then permutation "numbers add two" contains two inversions β€” pairs of words "numbers" and "add", "numbers" and "two...
[]
60
0
0
207,811
277
Google Code Jam
[ "dp", "probabilities" ]
null
null
Many of you must be familiar with the Google Code Jam round rules. Let us remind you of some key moments that are crucial to solving this problem. During the round, the participants are suggested to solve several problems, each divided into two subproblems: an easy one with small limits (Small input), and a hard one wi...
The first line contains two integers *n* and *t* (1<=≀<=*n*<=≀<=1000,<=1<=≀<=*t*<=≀<=1560). Then follow *n* lines, each containing 5 numbers: *scoreSmall**i*,<=*scoreLarge**i*,<=*timeSmall**i*,<=*timeLarge**i*,<=*probFail**i* (1<=≀<=*scoreSmall**i*,<=*scoreLarge**i*<=≀<=109,<=1<=≀<=*timeSmall**i*,<=*timeLarge**i*<=≀<=1...
Print two real numbers β€” the maximum expectation of the total points and the corresponding minimum possible time penalty expectation. The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=9.
[ "3 40\n10 20 15 4 0.5\n4 100 21 1 0.99\n1 4 1 1 0.25\n", "1 1\n100000000 200000000 1 1 0\n" ]
[ "24.0 18.875\n", "100000000 1\n" ]
In the first sample one of the optimal orders of solving problems is: 1. The Small input of the third problem. 1. The Small input of the first problem. 1. The Large input of the third problem. 1. The Large input of the first problem. Note that if you solve the Small input of the second problem instead of two inputs...
[]
92
0
0
208,569
698
Coprime Permutation
[ "combinatorics", "number theory" ]
null
null
Two positive integers are coprime if and only if they don't have a common divisor greater than 1. Some bear doesn't want to tell Radewoosh how to solve some algorithmic problem. So, Radewoosh is going to break into that bear's safe with solutions. To pass through the door, he must enter a permutation of numbers 1 thro...
The first line of the input contains one integer *n* (2<=≀<=*n*<=≀<=1<=000<=000). The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=≀<=*p**i*<=≀<=*n*) where *p**i*<==<=0 means a gap to fill, and *p**i*<=β‰₯<=1 means a fixed number. It's guaranteed that if *i*<=β‰ <=*j* and *p**i*,<=*p**j*<=β‰₯<=1 then *p...
Print the number of ways to fill the gaps modulo 109<=+<=7 (i.e. modulo 1000000007).
[ "4\n0 0 0 0\n", "5\n0 0 1 2 0\n", "6\n0 0 1 2 0 0\n", "5\n5 3 4 2 1\n" ]
[ "4\n", "2\n", "0\n", "0\n" ]
In the first sample test, none of four element is fixed. There are four permutations satisfying the given conditions: (1,2,3,4), (1,4,3,2), (3,2,1,4), (3,4,1,2). In the second sample test, there must be *p*<sub class="lower-index">3</sub> = 1 and *p*<sub class="lower-index">4</sub> = 2. The two permutations satisfying...
[ { "input": "4\n0 0 0 0", "output": "4" }, { "input": "5\n0 0 1 2 0", "output": "2" }, { "input": "6\n0 0 1 2 0 0", "output": "0" }, { "input": "5\n5 3 4 2 1", "output": "0" }, { "input": "2\n0 0", "output": "2" }, { "input": "2\n1 0", "output": "1"...
140
1,740,800
0
209,203
48
Snow sellers
[ "greedy", "sortings" ]
F. Snow sellers
10
256
The New Year celebrations in Berland last *n* days. Only this year the winter is snowless, that’s why the winter celebrations’ organizers should buy artificial snow. There are *m* snow selling companies in Berland. Every day the *i*-th company produces *w**i* cubic meters of snow. Next day the snow thaws and the compan...
The first line contains integers *n*, *m* and *W* (1<=≀<=*n*<=≀<=100, 1<=≀<=*m*<=≀<=500000, 1<=≀<=*W*<=≀<=109) which represent the number of days, the number of companies and the amount of snow that needs to be purchased on every one of the *n* days. The second line contains *m* integers *w**i*. The third line contains...
Print a single number β€” the answer to the given problem. Print the answer in the format with the decimal point (even if the answer is integer, it must contain the decimal point), without "e" and without leading zeroes. The answer should differ with the right one by no more than 10<=-<=9.
[ "2 3 10\n4 4 4\n5 5 8\n1 2 5\n", "100 2 1000000000\n999999998 999999999\n1000000000 1000000000\n1 1\n" ]
[ "22.000000000000000\n", "99999995149.999995249999991\n" ]
none
[]
92
0
0
209,675
0
none
[ "none" ]
null
null
Little Petya likes positive integers a lot. Recently his mom has presented him a positive integer *a*. There's only one thing Petya likes more than numbers: playing with little Masha. It turned out that Masha already has a positive integer *b*. Petya decided to turn his number *a* into the number *b* consecutively perf...
The only line contains three integers *a*, *b* (1<=≀<=*b*<=≀<=*a*<=≀<=1018) and *k* (2<=≀<=*k*<=≀<=15). Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print a single integer β€” the required minimum number of seconds needed to transform number *a* into number *b*.
[ "10 1 4\n", "6 3 10\n", "1000000000000000000 1 3\n" ]
[ "6\n", "2\n", "666666666666666667\n" ]
In the first sample the sequence of numbers that Petya gets as he tries to obtain number *b* is as follows: 10  →  8  →  6  →  4  →  3  →  2  →  1. In the second sample one of the possible sequences is as follows: 6  →  4  →  3.
[]
60
0
0
209,715
768
Jon Snow and his Favourite Number
[ "brute force", "dp", "implementation", "sortings" ]
null
null
Jon Snow now has to fight with White Walkers. He has *n* rangers, each of which has his own strength. Also Jon Snow has his favourite number *x*. Each ranger can fight with a white walker only if the strength of the white walker equals his strength. He however thinks that his rangers are weak and need to improve. Jon n...
First line consists of three integers *n*, *k*, *x* (1<=≀<=*n*<=≀<=105, 0<=≀<=*k*<=≀<=105, 0<=≀<=*x*<=≀<=103) β€” number of rangers Jon has, the number of times Jon will carry out the operation and Jon's favourite number respectively. Second line consists of *n* integers representing the strengths of the rangers *a*1,<=...
Output two integers, the maximum and the minimum strength of the rangers after performing the operation *k* times.
[ "5 1 2\n9 7 11 15 5\n", "2 100000 569\n605 986\n" ]
[ "13 7", "986 605" ]
none
[ { "input": "5 1 2\n9 7 11 15 5", "output": "13 7" }, { "input": "2 100000 569\n605 986", "output": "986 605" }, { "input": "10 10 98\n1 58 62 71 55 4 20 17 25 29", "output": "127 17" }, { "input": "100 100 96\n11 79 47 73 77 66 50 32 26 38 8 58 45 86 35 49 63 13 35 61 52 44 1...
4,000
614,400
0
211,411
219
Parking Lot
[ "data structures" ]
null
null
A parking lot in the City consists of *n* parking spaces, standing in a line. The parking spaces are numbered from 1 to *n* from left to right. When a car arrives at the lot, the operator determines an empty parking space for it. For the safety's sake the chosen place should be located as far from the already occupie...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=2Β·105) β€” the number of parking places and the number of records correspondingly. Next *m* lines contain the descriptions of the records, one per line. The *i*-th line contains numbers *t**i*, *id**i* (1<=≀<=*t**i*<=≀<=2;Β 1<=≀<=*id**...
For each entry of an arriving car print the number of its parking space. Print the numbers of the spaces in the order, in which the cars arrive to the parking lot.
[ "7 11\n1 15\n1 123123\n1 3\n1 5\n2 123123\n2 15\n1 21\n2 3\n1 6\n1 7\n1 8\n" ]
[ "1\n7\n4\n2\n7\n4\n1\n3\n" ]
none
[]
233
32,256,000
0
211,783
198
Gripping Story
[ "binary search", "data structures", "sortings" ]
null
null
One day Qwerty the Ranger witnessed two transport ships collide with each other. As a result, all contents of their cargo holds scattered around the space. And now Qwerty wants to pick as many lost items as possible to sell them later. The thing is, both ships had lots of new gravitational grippers, transported to sal...
The first line contains five integers *x*, *y*, *p*, *r* and *n* (<=-<=109<=≀<=*x*,<=*y*<=≀<=109, 1<=≀<=*p*,<=*r*<=≀<=109, 1<=≀<=*n*<=≀<=250000) β€” the ship's initial position, the initial gripper's features and the number of grippers that got into the space during the collision. Next *n* lines contain the grippers' de...
Print a single number β€” the maximum number of grippers Qwerty can draw to his ship. You do not need to count the initial old magnet gripper.
[ "0 0 5 10 5\n5 4 7 11 5\n-7 1 4 7 8\n0 2 13 5 6\n2 -3 9 3 4\n13 5 1 9 9\n" ]
[ "3\n" ]
In the first sample you should get the second gripper, then use the second gripper to get the first one, then use the first gripper to get the fourth one. You cannot get neither the third gripper as it is too heavy, nor the fifth one as it is too far away.
[]
30
0
0
211,916
267
Berland Traffic
[ "math", "matrices" ]
null
null
Berland traffic is very different from traffic in other countries. The capital of Berland consists of *n* junctions and *m* roads. Each road connects a pair of junctions. There can be multiple roads between a pair of junctions. For each road we know its capacity: value *c**i* is the maximum number of cars that can driv...
The first line contains a positive integer *n* β€” the number of junctions (2<=≀<=*n*<=≀<=100). The second line contains integer *m* (1<=≀<=*m*<=≀<=5000) β€” the number of roads. Next *m* lines contain the roads' descriptions. Each road contains a group of three numbers *a**i*, *b**i*, *c**i*, where *a**i*,<=*b**i* are the...
In the first line print the required largest traffic across the city. Then print *m* lines, on each line print the speed, at which the traffic moves along the corresponding road. If the direction doesn't match the order of the junctions, given in the input, then print the traffic with the minus sign. Print the numbers ...
[ "2\n3\n1 2 2\n1 2 4\n2 1 1000\n", "7\n11\n1 2 7\n1 2 7\n1 3 7\n1 4 7\n2 3 7\n2 5 7\n3 6 7\n4 7 7\n5 4 7\n5 6 7\n6 7 7\n" ]
[ "6.00000\n2.00000\n2.00000\n-2.00000\n", "13.00000\n2.00000\n2.00000\n3.00000\n6.00000\n1.00000\n3.00000\n4.00000\n7.00000\n1.00000\n2.00000\n6.00000\n" ]
none
[]
62
0
0
211,964
847
Noise Level
[ "dfs and similar", "implementation", "math" ]
null
null
The Berland's capital has the form of a rectangle with sizes *n*<=Γ—<=*m* quarters. All quarters are divided into three types: - regular (labeled with the character '.') β€” such quarters do not produce the noise but are not obstacles to the propagation of the noise; - sources of noise (labeled with an uppercase Latin ...
The first line contains four integers *n*, *m*, *q* and *p* (1<=≀<=*n*,<=*m*<=≀<=250, 1<=≀<=*q*,<=*p*<=≀<=106) β€” the sizes of Berland's capital, the number of noise units that a quarter 'A' produces, and the allowable noise level. Each of the following *n* lines contains *m* characters β€” the description of the capital...
Print the number of quarters, in which the noise level exceeds the allowed level *p*.
[ "3 3 100 140\n...\nA*.\n.B.\n", "3 3 2 8\nB*.\nBB*\nBBB\n", "3 4 5 4\n..*B\n..**\nD...\n" ]
[ "3\n", "4\n", "7\n" ]
The illustration to the first example is in the main part of the statement.
[]
5,000
409,600
0
213,192
115
Unambiguous Arithmetic Expression
[ "dp", "expression parsing" ]
null
null
Let's define an unambiguous arithmetic expression (UAE) as follows. - All non-negative integers are UAE's. Integers may have leading zeroes (for example, 0000 and 0010 are considered valid integers). - If *X* and *Y* are two UAE's, then "(*X*)<=+<=(*Y*)", "(*X*)<=-<=(*Y*)", "(*X*)<=*<=(*Y*)", and "(*X*)<=/<=(*Y*)" ...
The first line is a non-empty string consisting of digits ('0'-'9') and characters '-', '+', '*', and/or '/'. Its length will not exceed 2000. The line doesn't contain any spaces.
Print a single integer representing the number of different unambiguous arithmetic expressions modulo 1000003 (106<=+<=3) such that if all its brackets are removed, it becomes equal to the input string (character-by-character).
[ "1+2*3\n", "03+-30+40\n", "5//4\n", "5/0\n", "1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1\n" ]
[ "2\n", "3\n", "0\n", "1\n", "100728\n" ]
For the first example, the two possible unambiguous arithmetic expressions are: For the second example, the three possible unambiguous arithmetic expressions are:
[]
62
0
0
213,431
645
Armistice Area Apportionment
[ "binary search", "geometry" ]
null
null
After a drawn-out mooclear arms race, Farmer John and the Mischievous Mess Makers have finally agreed to establish peace. They plan to divide the territory of Bovinia with a line passing through at least two of the *n* outposts scattered throughout the land. These outposts, remnants of the conflict, are located at the ...
The first line of the input contains two integers *n* and *a* (2<=≀<=*n*<=≀<=100<=000, 1<=≀<=*a*<=≀<=10<=000)Β β€” the number of outposts and the coordinates of the farm and the base, respectively. The following *n* lines describe the locations of the outposts as pairs of integers (*x**i*,<=*y**i*) (|*x**i*|,<=|*y**i*|<=...
Print a single real numberβ€”the difference of the optimal dividing line. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "2 5\n1 0\n2 1\n", "3 6\n0 1\n2 5\n0 -3\n" ]
[ "7.2111025509\n", "0.0000000000\n" ]
In the first sample case, the only possible line <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c7c068229f25f741e2c902b657ba04a42feb0752.png" style="max-width: 100.0%;max-height: 100.0%;"/> is *y* = *x* - 1. It can be shown that the point *X* which maximizes |*PX* - *QX*| is (13, 12), with...
[]
30
0
0
213,444
401
Olympic Games
[ "math" ]
null
null
This problem was deleted from the contest, because it was used previously at another competition.
[ "1 1\n1 2 100\n" ]
[ "6\n" ]
none
[ { "input": "1 1\n1 2 100", "output": "6" }, { "input": "2 2\n1 1 100", "output": "12" }, { "input": "2 2\n1 3 100", "output": "28" }, { "input": "8 17\n1 20 1000000000", "output": "8055" }, { "input": "10 10\n1 5 10000000", "output": "1892" }, { "input...
46
0
0
213,699
200
Cinema
[ "brute force", "data structures" ]
null
null
The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into *n* rows, each row consists of *m* seats. There are *k* people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office start...
The first input line contains three integers *n*, *m*, *k* (1<=≀<=*n*,<=*m*<=≀<=2000, 1<=≀<=*k*<=≀<=*min*(*n*Β·*m*,<=105) β€” the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next *k* lines contains two integers *x**i*, *y**i* (1<=≀<=*x**i*<...
Print *k* lines, each containing a pair of integers. Print on the *i*-th line *x**i*,<=*y**i* β€” the coordinates of the seat, for which the person who stands *i*-th in the line will buy the ticket.
[ "3 4 6\n1 1\n1 1\n1 1\n1 2\n1 3\n1 3\n", "4 3 12\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n" ]
[ "1 1\n1 2\n2 1\n1 3\n1 4\n2 3\n", "2 2\n1 2\n2 1\n2 3\n3 2\n1 1\n1 3\n3 1\n3 3\n4 2\n4 1\n4 3\n" ]
none
[]
62
2,867,200
-1
213,744
0
none
[ "none" ]
null
null
In the Isle of Guernsey there are *n* different types of coins. For each *i* (1<=≀<=*i*<=≀<=*n*), coin of type *i* is worth *a**i* cents. It is possible that *a**i*<==<=*a**j* for some *i* and *j* (*i*<=β‰ <=*j*). Bessie has some set of these coins totaling *t* cents. She tells Jessie *q* pairs of integers. For each *i...
The first line contains three space-separated integers, *n*,<=*q* and *t* (1<=≀<=*n*<=≀<=300;Β 0<=≀<=*q*<=≀<=*n*;Β 1<=≀<=*t*<=≀<=105). The second line contains *n* space separated integers, *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=105). The next *q* lines each contain two distinct space-separated integers, *b**i* and...
A single integer, the number of valid coin combinations that Bessie could have, modulo 1000000007 (109<=+<=7).
[ "4 2 17\n3 1 2 5\n4 2\n3 4\n", "3 2 6\n3 1 1\n1 2\n2 3\n", "3 2 10\n1 2 3\n1 2\n2 1\n" ]
[ "3\n", "0\n", "0\n" ]
For the first sample, the following 3 combinations give a total of 17 cents and satisfy the given conditions: {0Β *of*Β *type*Β 1, 1Β *of*Β *type*Β 2, 3Β *of*Β *type*Β 3, 2Β *of*Β *type*Β 4}, {0, 0, 6, 1}, {2, 0, 3, 1}. No other combinations exist. Note that even though 4 occurs in both *b*<sub class="lower-index">*i*</sub> and *...
[]
92
0
0
214,086
0
none
[ "none" ]
null
null
There is a computer network consisting of *n* nodes numbered 1 through *n*. There are links in the network that connect pairs of nodes. A pair of nodes may have multiple links between them, but no node has a link to itself. Each link supports unlimited bandwidth (in either direction), however a link may only transmit ...
Input will begin with two integers *n* and *m* (2<=≀<=*n*<=≀<=200000; 0<=≀<=*m*<=≀<=200000), the number of nodes and number of known links in the network, respectively. Following this are *m* lines with four integers each: *f*, *t*, *w*, *b* (1<=≀<=*f*<=≀<=*n*;Β 1<=≀<=*t*<=≀<=*n*;Β *f*<=β‰ <=*t*;Β 1<=≀<=*w*<=≀<=100;Β 0<=≀<=*...
If the intern's solution is definitely not optimal, print "BAD *x*", where *x* is the first link in the input that violates the optimality of the solution. If the intern's solution may be optimal, print the efficiency of the solution if it can be determined rounded to the nearest integer, otherwise print "UNKNOWN".
[ "4 5\n1 2 1 2\n1 3 4 1\n2 3 2 1\n2 4 4 1\n3 4 1 2\n", "5 5\n2 3 1 1\n3 4 1 1\n4 2 1 1\n1 5 1 1\n1 5 100 100\n", "6 4\n1 3 31 41\n1 5 59 26\n2 6 53 58\n4 6 97 93\n", "7 5\n1 7 2 1\n2 3 1 1\n4 5 1 0\n6 1 10 0\n1 3 1 1\n" ]
[ "6\n", "BAD 3\n", "UNKNOWN\n", "BAD 4\n" ]
Although the known weights and bandwidths happen to always be integers, the weights and bandwidths of the remaining links are not restricted to integers.
[]
31
0
0
214,332
482
ELCA
[ "data structures", "trees" ]
null
null
You have a root tree containing *n* vertexes. Let's number the tree vertexes with integers from 1 to *n*. The tree root is in the vertex 1. Each vertex (except fot the tree root) *v* has a direct ancestor *p**v*. Also each vertex *v* has its integer value *s**v*. Your task is to perform following queries: - P *v* ...
The first line of the input contains integer *n* (2<=≀<=*n*<=≀<=5Β·104) β€” the number of the tree vertexes. The second line contains *n*<=-<=1 integer *p*2,<=*p*3,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=*n*) β€” the description of the tree edges. It is guaranteed that those numbers form a tree. The third line contains *n* inte...
Print *q*<=+<=1 number β€” the corresponding expected values. Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=9.
[ "5\n1 2 2 1\n1 2 3 4 5\n5\nP 3 4\nP 4 5\nV 2 3\nP 5 2\nP 1 4\n" ]
[ "1.640000000\n1.800000000\n2.280000000\n2.320000000\n2.800000000\n1.840000000\n" ]
Note that in the query P *v* *u* if *u* lies in subtree of *v* you must perform assignment *p*<sub class="lower-index">*u*</sub> = *v*. An example of such case is the last query in the sample.
[]
46
0
0
214,696
475
Meta-universe
[ "data structures" ]
null
null
Consider infinite grid of unit cells. Some of those cells are planets. Meta-universe *M*<==<={*p*1,<=*p*2,<=...,<=*p**k*} is a set of planets. Suppose there is an infinite row or column with following two properties: 1) it doesn't contain any planet *p**i* of meta-universe *M* on it; 2) there are planets of *M* locat...
The first line of input contains an integer *n*, (1<=≀<=*n*<=≀<=105), denoting the number of planets in the meta-universe. The next *n* lines each contain integers *x**i* and *y**i*, (<=-<=109<=≀<=*x**i*,<=*y**i*<=≀<=109), denoting the coordinates of the *i*-th planet. All planets are located in different cells.
Print the number of resulting universes.
[ "5\n0 0\n0 2\n2 0\n2 1\n2 2\n", "8\n0 0\n1 0\n0 2\n0 3\n3 0\n3 1\n2 3\n3 3\n" ]
[ "3\n", "1\n" ]
The following figure describes the first test case:
[]
795
16,486,400
0
214,855