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
536
Tavas in Kansas
[ "dp", "games" ]
null
null
Tavas lives in Kansas. Kansas has *n* cities numbered from 1 to *n* connected with *m* bidirectional roads. We can travel from any city to any other city via these roads. Kansas is as strange as Tavas. So there may be a road between a city and itself or more than one road between two cities. Tavas invented a game and ...
The first line of input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=2000, *n*<=-<=1<=≤<=*m*<=≤<=105). The second line of input contains two integers *s* and *t* (1<=≤<=*s*,<=*t*<=≤<=*n*, *s*<=≠<=*t*). The next line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* separated by spaces (|*p**i*|<=≤<=109). The next...
If Tavas wins, print "Break a heart". If Nafas wins print "Cry" and if nobody wins (i. e. the game ended with draw) print "Flowers".
[ "4 4\n1 2\n3 2 5 -11\n1 4 2\n3 4 2\n3 1 5\n3 2 1\n", "5 4\n1 2\n2 2 -5 -4 6\n1 2 4\n2 3 5\n2 4 2\n4 5 2\n", "2 1\n1 2\n-5 -5\n1 2 10\n" ]
[ "Cry\n", "Break a heart\n", "Flowers\n" ]
none
[]
2,000
330,342,400
0
285,732
0
none
[ "none" ]
null
null
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...
2,000
11,366,400
0
285,836
855
Rowena Ravenclaw's Diadem
[ "trees" ]
null
null
Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her. Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry ...
First line of input contains the number *n* (1<=≤<=*n*<=≤<=105), the number of objects. Next *n* lines contain two integer *parent**i* and *type**i* (<=-<=1<=≤<=*parent**i*<=&lt;<=*i* *parent**i*<=≠<=0, <=-<=1<=≤<=*type**i*<=≤<=1), implying that the *i*-th object has the parent *parent**i*. (If *type**i*<==<=0, this ...
Output will contain *q* lines, each containing the answer for the corresponding query as "YES" (affirmative) or "NO" (without quotes). You can output each letter in any case (upper or lower).
[ "3\n-1 -1\n1 0\n2 0\n2\n1 1 3\n2 1 3\n", "3\n-1 -1\n1 0\n1 1\n2\n2 2 3\n2 3 2\n" ]
[ "YES\nNO\n", "YES\nNO\n" ]
In test case 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object 3 a special case of object 1. In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general...
[]
46
0
0
286,552
280
Sequence Transformation
[ "brute force", "data structures", "dp", "implementation", "math" ]
null
null
You've got a non-decreasing sequence *x*1,<=*x*2,<=...,<=*x**n* (1<=≤<=*x*1<=≤<=*x*2<=≤<=...<=≤<=*x**n*<=≤<=*q*). You've also got two integers *a* and *b* (*a*<=≤<=*b*; *a*·(*n*<=-<=1)<=&lt;<=*q*). Your task is to transform sequence *x*1,<=*x*2,<=...,<=*x**n* into some sequence *y*1,<=*y*2,<=...,<=*y**n* (1<=≤<=*y**i*...
The first line contains four integers *n*,<=*q*,<=*a*,<=*b* (2<=≤<=*n*<=≤<=6000; 1<=≤<=*q*,<=*a*,<=*b*<=≤<=109; *a*·(*n*<=-<=1)<=&lt;<=*q*; *a*<=≤<=*b*). The second line contains a non-decreasing integer sequence *x*1,<=*x*2,<=...,<=*x**n* (1<=≤<=*x*1<=≤<=*x*2<=≤<=...<=≤<=*x**n*<=≤<=*q*).
In the first line print *n* real numbers — the sought sequence *y*1,<=*y*2,<=...,<=*y**n* (1<=≤<=*y**i*<=≤<=*q*; *a*<=≤<=*y**i*<=+<=1<=-<=*y**i*<=≤<=*b*). In the second line print the minimum transformation price, that is, . If there are multiple optimal answers you can print any of them. The answer will be considere...
[ "3 6 2 2\n1 4 6\n", "10 100000 8714 9344\n3378 14705 17588 22672 32405 34309 37446 51327 81228 94982\n" ]
[ "1.666667 3.666667 5.666667 \n0.666667\n", "1.000000 8715.000000 17429.000000 26143.000000 34857.000000 43571.000000 52285.000000 61629.000000 70973.000000 80317.000000 \n797708674.000000\n" ]
none
[]
0
0
-1
287,245
43
Race
[ "brute force", "implementation", "two pointers" ]
E. Race
2
256
Today *s* kilometer long auto race takes place in Berland. The track is represented by a straight line as long as *s* kilometers. There are *n* cars taking part in the race, all of them start simultaneously at the very beginning of the track. For every car is known its behavior — the system of segments on each of which...
The first line contains two integers *n* and *s* (2<=≤<=*n*<=≤<=100,<=1<=≤<=*s*<=≤<=106) — the number of cars and the length of the track in kilometers. Then follow *n* lines — the description of the system of segments for each car. Every description starts with integer *k* (1<=≤<=*k*<=≤<=100) — the number of segments ...
Print the single number — the number of times some car managed to take the lead over another car during the race.
[ "2 33\n2 5 1 2 14\n1 3 11\n", "2 33\n2 1 3 10 3\n1 11 3\n", "5 33\n2 1 3 3 10\n1 11 3\n2 5 3 3 6\n2 3 1 10 3\n2 6 3 3 5\n" ]
[ "1\n", "0\n", "2\n" ]
none
[ { "input": "2 33\n2 5 1 2 14\n1 3 11", "output": "1" }, { "input": "2 33\n2 1 3 10 3\n1 11 3", "output": "0" }, { "input": "5 33\n2 1 3 3 10\n1 11 3\n2 5 3 3 6\n2 3 1 10 3\n2 6 3 3 5", "output": "2" }, { "input": "2 166755\n2 733 187 362 82\n3 813 147 565 57 557 27", "out...
684
5,734,400
3.818319
287,807
185
Clever Fat Rat
[ "dp" ]
null
null
The Fat Rat and his friend Сerealguy have had a bet whether at least a few oats are going to descend to them by some clever construction. The figure below shows the clever construction. A more formal description of the clever construction is as follows. The clever construction consists of *n* rows with scales. The fir...
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=50) — the number of rows with scales. The next line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=106) — the masses of the oats in kilograms. The next *n* lines contain descriptions of the scales: the *i*-th line contains (*n*<=-<=*i*<=+<=1)...
Print "Fat Rat" if the Fat Rat is right, otherwise print "Cerealguy".
[ "1\n1\n2\n", "2\n2 2\n1 2\n4\n", "2\n2 2\n1 2\n5\n" ]
[ "Fat Rat\n", "Cerealguy\n", "Fat Rat\n" ]
Notes to the examples: - The first example: the scale with weight capacity 2 gets 1. That means that the lower scale don't break. - The second sample: all scales in the top row obviously break. Then the oats fall on the lower row. Their total mass is 4,and that's exactly the weight that the lower scale can "nearly ...
[]
46
0
0
288,338
903
Yet Another Maxflow Problem
[ "data structures", "flows", "graphs" ]
null
null
In this problem you will have to deal with a very special network. The network consists of two parts: part *A* and part *B*. Each part consists of *n* vertices; *i*-th vertex of part *A* is denoted as *A**i*, and *i*-th vertex of part *B* is denoted as *B**i*. For each index *i* (1<=≤<=*i*<=&lt;<=*n*) there is a dire...
The first line contains three integer numbers *n*, *m* and *q* (2<=≤<=*n*,<=*m*<=≤<=2·105, 0<=≤<=*q*<=≤<=2·105) — the number of vertices in each part, the number of edges going from *A* to *B* and the number of changes, respectively. Then *n*<=-<=1 lines follow, *i*-th line contains two integers *x**i* and *y**i* deno...
Firstly, print the maximum flow value in the original network. Then print *q* integers, *i*-th of them must be equal to the maximum flow value after *i*-th change.
[ "4 3 2\n1 2\n3 4\n5 6\n2 2 7\n1 4 8\n4 3 9\n1 100\n2 100\n" ]
[ "9\n14\n14\n" ]
This is the original network in the example:
[ { "input": "4 3 2\n1 2\n3 4\n5 6\n2 2 7\n1 4 8\n4 3 9\n1 100\n2 100", "output": "9\n14\n14" }, { "input": "10 10 10\n291546518 199012865\n327731857 137263959\n145140225 631959974\n559674936 815057131\n677050070 949982094\n839693202 160045764\n967872826 489258292\n706535160 594950620\n230389718 27478...
62
0
0
288,764
167
Wizards and Roads
[ "data structures", "divide and conquer", "graph matchings", "graphs", "greedy" ]
null
null
In some country live wizards. They love to build cities and roads. The country used to have *k* cities, the *j*-th city (1<=≤<=*j*<=≤<=*k*) was located at a point (*x**j*, *y**j*). It was decided to create another *n*<=-<=*k* cities. And the *i*-th one (*k*<=&lt;<=*i*<=≤<=*n*) was created at a point with coordinates (...
The first line contains two space-separated integers *n*, *k* (1<=≤<=*k*<=≤<=*n*<=≤<=105, *k*<=≤<=30). Next *k* lines contain coordinates of the cities' location points from the first to the *k*-th one. The *j*-th line contains space-separated pair of integers *x**j*, *y**j* (0<=≤<=*x**j*,<=*y**j*<=&lt;<=109<=+<=9) — c...
For any pair of numbers *L**i*, *R**i* print the answer to the problem on a single line. Print the answers for the pairs in the order, in which the pairs are given in the input data.
[ "6 6\n0 0\n1 1\n2 2\n3 3\n4 4\n5 5\n2 3 3 2\n4\n0 5\n1 4\n2 3\n3 3\n", "6 1\n0 0\n3 5 23917 11\n4\n0 1000000008\n0 10\n100 150\n200 10000\n" ]
[ "3\n2\n1\n0\n", "2\n1\n0\n1\n" ]
In the first sample the roads connect the cities in a chain in the order of increasing of *x*. In the second sample the remaining 5 cities will be located at points (5,  11); (20,  263098); (65,  292514823); (200,  76958738); (605,  622120197).
[]
92
0
0
289,230
251
Tree and Table
[ "dfs and similar", "dp", "implementation", "trees" ]
null
null
Little Petya likes trees a lot. Recently his mother has presented him a tree with 2*n* nodes. Petya immediately decided to place this tree on a rectangular table consisting of 2 rows and *n* columns so as to fulfill the following conditions: 1. Each cell of the table corresponds to exactly one tree node and vice vers...
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105). Next (2*n*<=-<=1) lines contain two integers each *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=2*n*; *a**i*<=≠<=*b**i*) that determine the numbers of the vertices connected by the corresponding edge. Consider the tree vertexes numbered by integers from...
Print a single integer — the required number of ways to place the tree on the table modulo 1000000007 (109<=+<=7).
[ "3\n1 3\n2 3\n4 3\n5 1\n6 2\n", "4\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n", "2\n1 2\n3 2\n4 2\n" ]
[ "12\n", "28\n", "0\n" ]
Note to the first sample (all 12 variants to place the tree on the table are given below):
[]
62
0
0
289,732
274
The Last Hole!
[ "brute force", "geometry" ]
null
null
Luyi has *n* circles on the plane. The *i*-th circle is centered at (*x**i*,<=*y**i*). At the time zero circles start to grow simultaneously. In other words, the radius of each circle at time *t* (*t*<=&gt;<=0) is equal to *t*. The circles are drawn as black discs on an infinite white plane. So at each moment the plane...
The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=100). Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=104<=≤<=*x**i*,<=*y**i*<=≤<=104), indicating the location of *i*-th circle. It's guaranteed that no two circles are centered at the same point.
Print the moment where the last hole disappears. If there exists no moment in which we can find holes print -1. The answer will be considered correct if the absolute or relative error does not exceed 10<=-<=4.
[ "3\n0 0\n1 1\n2 2\n", "4\n0 0\n0 2\n2 2\n2 0\n", "4\n0 1\n0 -1\n-2 0\n4 0\n" ]
[ "-1\n", "1.414214\n", "2.125000\n" ]
none
[]
92
0
0
289,900
839
Mother of Dragons
[ "brute force", "graphs", "math", "meet-in-the-middle" ]
null
null
There are *n* castles in the Lannister's Kingdom and some walls connect two castles, no two castles are connected by more than one wall, no wall connects a castle to itself. Sir Jaime Lannister has discovered that Daenerys Targaryen is going to attack his kingdom soon. Therefore he wants to defend his kingdom. He has...
The first line of the input contains two integers *n* and *k* (1<=≤<=*n*<=≤<=40, 1<=≤<=*k*<=≤<=1000). Then *n* lines follows. The *i*-th of these lines contains *n* integers *a**i*,<=1,<=*a**i*,<=2,<=...,<=*a**i*,<=*n* (). If castles *i* and *j* are connected by a wall, then *a**i*,<=*j*<==<=1. Otherwise it is equal t...
Print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve. 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 an...
[ "3 1\n0 1 0\n1 0 0\n0 0 0\n", "4 4\n0 1 0 1\n1 0 1 0\n0 1 0 1\n1 0 1 0\n" ]
[ "0.250000000000\n", "4.000000000000\n" ]
In the first sample, we can assign 0.5, 0.5, 0 liters of liquid to castles 1, 2, 3, respectively, to get the maximum sum (0.25). In the second sample, we can assign 1.0, 1.0, 1.0, 1.0 liters of liquid to castles 1, 2, 3, 4, respectively, to get the maximum sum (4.0)
[ { "input": "3 1\n0 1 0\n1 0 0\n0 0 0", "output": "0.250000000000" }, { "input": "4 4\n0 1 0 1\n1 0 1 0\n0 1 0 1\n1 0 1 0", "output": "4.000000000000" }, { "input": "1 252\n0", "output": "0.000000000000" }, { "input": "5 21\n0 1 1 1 1\n1 0 1 1 1\n1 1 0 1 1\n1 1 1 0 1\n1 1 1 1 ...
31
0
0
290,472
0
none
[ "none" ]
null
null
Limak is a smart brown bear who loves chemistry, reactions and transforming elements. In Bearland (Limak's home) there are *n* elements, numbered 1 through *n*. There are also special machines, that can transform elements. Each machine is described by two integers *a**i*,<=*b**i* representing two elements, not necessa...
The first line contains three integers *n*, *m* and *q* (1<=≤<=*n*,<=*q*<=≤<=300<=000,<=0<=≤<=*m*<=≤<=300<=000) — the number of elements, the number of Limak's machines and the number of gossips, respectively. Each of the next *m* lines contains two integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*) describing...
You should print *q* lines. The *i*-th of them should contain "YES" (without quotes) if for the *i*-th gossip for each pair of elements *x* and *y* (in the set *x**i*,<=1,<=*x**i*,<=2,<=...,<=*x**i*,<=*n**i*) Limak is able to succeed. Otherwise you should print "NO" (without quotes).
[ "6 5 4\n1 2\n2 3\n3 4\n2 4\n5 6\n2 0\n4 2\n2 1\n6 2\n3 4\n3 2\n6 3 4\n2 5\n4 6\n2 1\n1 2\n1 2\n", "7 6 2\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n7 2\n1 2 3 4 5 6 7\n4 5\n6 7\n7 2\n1 2 3 4 5 6 7\n4 6\n5 7\n" ]
[ "YES\nNO\nYES\nYES\n", "NO\nYES\n" ]
Lets look at first sample: In first gossip Radewoosh's favorite set is {4, 2} and he has no machines. Limak can tranform element 4 into 2 (so half of a task is complete) and then 2 into 3, and 3 into 4. Answer is "YES", so *R* is increased by 1. In second gossip set in the input is denoted by {6, 2} and machine by (3...
[]
46
0
0
291,008
793
Problem of offices
[ "constructive algorithms", "dfs and similar", "dp", "trees" ]
null
null
Earlier, when there was no Internet, each bank had a lot of offices all around Bankopolis, and it caused a lot of problems. Namely, each day the bank had to collect cash from all the offices. Once Oleg the bank client heard a dialogue of two cash collectors. Each day they traveled through all the departments and offic...
The first line contains single integer *n* (5<=≤<=*n*<=≤<=5000) — the total number of offices and departments. The departments and offices are numbered from 1 to *n*, the central office has index 1. The second line contains four integers *a*, *b*, *c* and *d* (2<=≤<=*a*,<=*b*,<=*c*,<=*d*<=≤<=*n*) — the indexes of the ...
If the situation described by the cash collectors was possible, print "YES". Otherwise, print "NO".
[ "5\n2 3 4 5\n1 1 1 1\n", "10\n3 8 9 10\n1 2 2 2 2 2 1 1 1\n", "13\n13 12 9 7\n1 1 1 1 5 5 2 2 2 3 3 4\n" ]
[ "YES", "NO", "YES" ]
In the first example the following collector's route was possible: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/441a4e28c99fe8b120f6b0215c357b7da102c815.png" style="max-width: 100.0%;max-height: 100.0%;"/>. We can note that between their visits to offices *a* and *b* the collectors visit...
[ { "input": "5\n2 3 4 5\n1 1 1 1", "output": "YES" }, { "input": "10\n3 8 9 10\n1 2 2 2 2 2 1 1 1", "output": "NO" }, { "input": "13\n13 12 9 7\n1 1 1 1 5 5 2 2 2 3 3 4", "output": "YES" }, { "input": "11\n2 4 9 11\n1 1 3 3 3 3 3 1 1 10", "output": "NO" }, { "input...
46
0
0
291,021
980
Cactus to Tree
[ "dp", "graphs", "trees" ]
null
null
You are given a special connected undirected graph where each vertex belongs to at most one simple cycle. Your task is to remove as many edges as needed to convert this graph into a tree (connected graph with no cycles). For each node, independently, output the maximum distance between it and a leaf in the resulting...
The first line of input contains two integers $n$ and $m$ ($1 \leq n \leq 5\cdot 10^5$), the number of nodes and the number of edges, respectively. Each of the following $m$ lines contains two integers $u$ and $v$ ($1 \leq u,v \leq n$, $u \ne v$), and represents an edge connecting the two nodes $u$ and $v$. Each pair ...
Print $n$ space-separated integers, the $i$-th integer represents the maximum distance between node $i$ and a leaf if the removed edges were chosen in a way that minimizes this distance.
[ "9 10\n7 2\n9 2\n1 6\n3 1\n4 3\n4 7\n7 6\n9 8\n5 8\n5 9\n", "4 4\n1 2\n2 3\n3 4\n4 1\n" ]
[ "5 3 5 4 5 4 3 5 4\n", "2 2 2 2\n" ]
In the first sample, a possible way to minimize the maximum distance from vertex $1$ is by removing the marked edges in the following image: Note that to minimize the answer for different nodes, you can remove different edges.
[]
78
0
0
293,499
0
none
[ "none" ]
null
null
This problem has unusual memory constraint. At evening, Igor and Zhenya the financiers became boring, so they decided to play a game. They prepared *n* papers with the income of some company for some time periods. Note that the income can be positive, zero or negative. Igor and Zhenya placed the papers in a row and d...
The first line contains single positive integer *n* (1<=≤<=*n*<=≤<=4000) — the number of papers. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=105<=≤<=*a**i*<=≤<=105), where *a**i* is the income on the *i*-th paper from the left.
Print the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.
[ "3\n1 3 1\n", "5\n-1 -2 -1 -2 -1\n", "4\n-4 -2 4 5\n" ]
[ "4\n", "0\n", "-13\n" ]
In the first example it's profitable for Igor to take two papers from the left to have the sum of the incomes equal to 4. Then Zhenya wouldn't be able to make a move since there would be only one paper, and he would be able to take only 2 or 3..
[]
46
0
0
295,297
799
Aquarium decoration
[ "data structures", "greedy", "two pointers" ]
null
null
Arkady and Masha want to choose decorations for thier aquarium in Fishdom game. They have *n* decorations to choose from, each of them has some cost. To complete a task Arkady and Masha need to choose exactly *m* decorations from given, and they want to spend as little money as possible. There is one difficulty: Masha...
The first line contains three integers *n*, *m* and *k* (1<=≤<=*n*<=≤<=200000, 1<=≤<=*m*<=≤<=*n*, 1<=≤<=*k*<=≤<=*n*) — the number of decorations, how many decorations the friends should choose, how many decorations each of them should like among the chosen. The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c*...
Print single integer: the minimum sum of money the friends should spend to fulfill all constraints. If it is not possible, print -1.
[ "4 3 2\n3 2 2 1\n2\n1 2\n2\n1 3\n", "4 3 2\n3 2 2 1\n2\n1 2\n3\n4 1 3\n", "4 2 2\n3 2 2 1\n2\n1 2\n3\n4 1 3\n" ]
[ "7\n", "6\n", "-1\n" ]
In the first example the only possible variant to choose 3 decorations having all conditions satisfied is to choose decorations 1, 2, 3. In the second example friends can choose decoration 4 instead of decoration 3, because this one is one coin cheaper. In the third example it's not possible to choose 2 decorations i...
[ { "input": "4 3 2\n3 2 2 1\n2\n1 2\n2\n1 3", "output": "7" }, { "input": "4 3 2\n3 2 2 1\n2\n1 2\n3\n4 1 3", "output": "6" }, { "input": "4 2 2\n3 2 2 1\n2\n1 2\n3\n4 1 3", "output": "-1" }, { "input": "1 1 1\n1\n1\n1\n1\n1", "output": "1" }, { "input": "2 1 1\n1 ...
46
0
0
297,066
37
Trial for Chief
[ "graphs", "greedy", "shortest paths" ]
E. Trial for Chief
2
256
Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chroniclers of that time. For example, they learned how the chief of the ancient Berland tribe was chosen. As soon as enough pretenders was picked, the following test took place among them: the chief of the tribe took a slab divid...
The first line contains two integers *N* and *M* (1<=≤<=*N*,<=*M*<=≤<=50) — the number of lines and columns on the slab. The next *N* lines contain *M* symbols each — the final coloration of the slab. *W* stands for the square that should be painted white and *B* — for the square that should be painted black.
In the single line output the minimal number of repaintings of side-linked areas needed to get the required coloration of the slab.
[ "3 3\nWBW\nBWB\nWBW\n", "2 3\nBBB\nBWB\n" ]
[ "2\n", "1\n" ]
none
[ { "input": "3 3\nWBW\nBWB\nWBW", "output": "2" }, { "input": "2 3\nBBB\nBWB", "output": "1" }, { "input": "9 29\nBWBBBBBBBBBWBWWBBBWBWBBBWWBWW\nWBWBBWBBWBWWBWBBBWBWWWBWBBBBB\nBWBBBBWWBBBWBWBBWWBBWBBBBBBBB\nBBBWWBBWWBBBWBWBBBWWWWWWBBBBW\nBBWWWWBBBBBBBBBWBBBBBBBBBBWBW\nBBBWWBBBBWBBBWWBBBWB...
92
0
0
297,455
76
Mutation
[ "bitmasks", "dp", "math" ]
C. Mutation
1
256
Scientists of planet Olympia are conducting an experiment in mutation of primitive organisms. Genome of organism from this planet can be represented as a string of the first *K* capital English letters. For each pair of types of genes they assigned *a**i*,<=*j* — a risk of disease occurence in the organism provided tha...
The first line of the input contains three integer numbers *N* (1<=≤<=*N*<=≤<=200<=000) — length of the genome of base organism, *K* (1<=≤<=*K*<=≤<=22) — the maximal index of gene type in the genome and *T* (1<=≤<=*T*<=≤<=2·109) — maximal allowable risk of disease occurence. The second line contains the genome of the g...
Output the number of organisms that can be obtained from the base one and which have the total risk of disease occurence not greater than *T*.
[ "5 3 13\nBACAC\n4 1 2\n1 2 3\n2 3 4\n3 4 10\n" ]
[ "5\n" ]
Explanation: one can obtain the following organisms (risks are stated in brackets): BACAC (11), ACAC (10), BAA (5), B (6), AA (4).
[]
46
0
0
297,461
443
Kolya and Tandem Repeat
[ "brute force", "implementation", "strings" ]
null
null
Kolya got string *s* for his birthday, the string consists of small English letters. He immediately added *k* more characters to the right of the string. Then Borya came and said that the new string contained a tandem repeat of length *l* as a substring. How large could *l* be? See notes for definition of a tandem re...
The first line contains *s* (1<=≤<=|*s*|<=≤<=200). This string contains only small English letters. The second line contains number *k* (1<=≤<=*k*<=≤<=200) — the number of the added characters.
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
[ "aaba\n2\n", "aaabbbb\n2\n", "abracadabra\n10\n" ]
[ "6\n", "6\n", "20\n" ]
A tandem repeat of length 2*n* is string *s*, where for any position *i* (1 ≤ *i* ≤ *n*) the following condition fulfills: *s*<sub class="lower-index">*i*</sub> = *s*<sub class="lower-index">*i* + *n*</sub>. In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabr...
[ { "input": "aaba\n2", "output": "6" }, { "input": "aaabbbb\n2", "output": "6" }, { "input": "abracadabra\n10", "output": "20" }, { "input": "zwvrx\n3", "output": "6" }, { "input": "ayi\n10", "output": "12" }, { "input": "eexlzphiay\n1", "output": "...
30
0
0
298,372
575
Robots protection
[ "data structures" ]
null
null
Company "Robots industries" produces robots for territory protection. Robots protect triangle territories — right isosceles triangles with catheti parallel to North-South and East-West directions. Owner of some land buys and sets robots on his territory to protect it. From time to time, businessmen want to build offic...
The first line contains integer *N* — width and height of the land, and integer *Q* — number of queries to handle. Next *Q* lines contain queries you need to process. Two types of queries: 1. 1 *dir* *x* *y* *len* — add a robot to protect a triangle. Depending on the value of *dir*, the values of *x*, *y* and *len...
For each second type query output how many robots guard this point. Each answer should be in a separate line.
[ "17 10\n1 1 3 2 4\n1 3 10 3 7\n1 2 6 8 2\n1 3 9 4 2\n2 4 4\n1 4 15 10 6\n2 7 7\n2 9 4\n2 12 2\n2 13 8\n" ]
[ "2\n2\n2\n0\n1\n" ]
none
[]
1,500
100,659,200
0
299,183
79
Password
[ "bitmasks", "dp", "shortest paths" ]
D. Password
1
256
Finally Fox Ciel arrived in front of her castle! She have to type a password to enter her castle. An input device attached to her castle is a bit unusual. The input device is a 1<=×<=*n* rectangle divided into *n* square panels. They are numbered 1 to *n* from left to right. Each panel has a state either ON or OFF. I...
The first line contains three integers *n*, *k* and *l* (1<=≤<=*n*<=≤<=10000,<=1<=≤<=*k*<=≤<=10,<=1<=≤<=*l*<=≤<=100), separated by single spaces. The second line contains *k* integers *x*1, ..., *x**k* (1<=≤<=*x*1<=&lt;<=*x*2<=&lt;<=...<=&lt;<=*x**k*<=≤<=*n*), separated by single spaces. The third line contains *l* i...
Print the minimal number of moves required to type the password. If it's impossible, print -1.
[ "10 8 2\n1 2 3 5 6 7 8 9\n3 5\n", "3 2 1\n1 2\n3\n" ]
[ "2\n", "-1\n" ]
One possible way to type the password in the first example is following: In the first move, choose 1st, 2nd, 3rd panels and flip those panels. In the second move, choose 5th, 6th, 7th, 8th, 9th panels and flip those panels.
[]
155
102,400
0
299,480
392
Deleting Substrings
[]
null
null
SmallR likes a game called "Deleting Substrings". In the game you are given a sequence of integers *w*, you can modify the sequence and get points. The only type of modification you can perform is (unexpected, right?) deleting substrings. More formally, you can choose several contiguous elements of *w* and delete them ...
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=400) — the initial length of *w*. The second line contains *n* integers *v*1,<=*v*2,<=...,<=*v**n* (0<=≤<=|*v**i*|<=≤<=2000) — the costs of operations. The next line contains *n* integers *w*1,<=*w*2,<=...,<=*w**n* (1<=≤<=*w**i*<=≤<=109) — the initial *w*.
Print a single integer — the maximum total score you can get.
[ "3\n0 0 3\n1 2 1\n", "6\n1 4 5 6 7 1000\n2 1 1 2 2 3\n" ]
[ "3", "12" ]
none
[]
46
0
0
299,970
30
Tricky and Clever Password
[ "binary search", "constructive algorithms", "data structures", "greedy", "hashing", "strings" ]
E. Tricky and Clever Password
2
256
In his very young years the hero of our story, king Copa, decided that his private data was hidden not enough securely, what is unacceptable for the king. That's why he invented tricky and clever password (later he learned that his password is a palindrome of odd length), and coded all his data using it. Copa is afra...
The input contains single string of small Latin letters with length from 1 to 105 characters.
The first line should contain integer *k* — amount of nonempty parts of the password in your answer (). In each of the following *k* lines output two integers *x**i* and *l**i* — start and length of the corresponding part of the password. Output pairs in order of increasing *x**i*. Separate the numbers in pairs by a si...
[ "abacaba\n", "axbya\n", "xabyczba\n" ]
[ "1\n1 7\n", "3\n1 1\n2 1\n5 1\n", "3\n2 2\n4 1\n7 2\n" ]
none
[]
92
0
0
303,940
704
Captain America
[ "flows", "greedy" ]
null
null
Steve Rogers is fascinated with new vibranium shields S.H.I.E.L.D gave him. They're all uncolored. There are *n* shields in total, the *i*-th shield is located at point (*x**i*,<=*y**i*) of the coordinate plane. It's possible that two or more shields share the same location. Steve wants to paint all these shields. He ...
The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100<=000) — the number of shields and the number of constraints respectively. The second line contains two integers *r* and *b* (1<=≤<=*r*,<=*b*<=≤<=109). The next *n* lines contain the shields coordinates. The *i*-th of these lines co...
If satisfying all the constraints is impossible print -1 in first and only line of the output. Otherwise, print the minimum total cost in the first line of output. In the second line print a string of length *n* consisting of letters 'r' and 'b' only. The *i*-th character should be 'r' if the *i*-th shield should be p...
[ "5 6\n8 3\n2 10\n1 5\n9 10\n9 10\n2 8\n1 9 1\n1 2 1\n2 10 3\n2 10 2\n1 1 1\n2 5 2\n", "4 4\n7 3\n10 3\n9 8\n10 3\n2 8\n2 8 0\n2 8 0\n1 2 0\n1 9 0\n" ]
[ "25\nrbrbb\n", "-1\n" ]
none
[]
2,000
172,953,600
0
306,208
0
none
[ "none" ]
null
null
Two players play a game. The game is played on a rectangular board with *n*<=×<=*m* squares. At the beginning of the game two different squares of the board have two chips. The first player's goal is to shift the chips to the same square. The second player aims to stop the first one with a tube of superglue. We'll des...
The first line contains six integers *n*, *m*, *x*1, *y*1, *x*2, *y*2 — the board sizes and the coordinates of the first and second chips, correspondingly (1<=≤<=*n*,<=*m*<=≤<=100; 2<=≤<=*n*<=×<=*m*; 1<=≤<=*x*1,<=*x*2<=≤<=*n*; 1<=≤<=*y*1,<=*y*2<=≤<=*m*). The numbers in the line are separated by single spaces. It is gu...
If the first player wins, print "First" without the quotes. Otherwise, print "Second" without the quotes.
[ "1 6 1 2 1 6\n", "6 5 4 3 2 1\n", "10 10 1 1 10 10\n" ]
[ "First", "First", "Second" ]
none
[ { "input": "1 6 1 2 1 6", "output": "First" }, { "input": "6 5 4 3 2 1", "output": "First" }, { "input": "10 10 1 1 10 10", "output": "Second" }, { "input": "1 2 1 1 1 2", "output": "First" }, { "input": "4 4 1 4 4 1", "output": "First" }, { "input": "...
122
0
0
307,134
147
Smile House
[ "binary search", "graphs", "matrices" ]
null
null
A smile house is created to raise the mood. It has *n* rooms. Some of the rooms are connected by doors. For each two rooms (number *i* and *j*), which are connected by a door, Petya knows their value *c**ij* — the value which is being added to his mood when he moves from room *i* to room *j*. Petya wondered whether he...
The first line contains two positive integers *n* and *m* (), where *n* is the number of rooms, and *m* is the number of doors in the Smile House. Then follows the description of the doors: *m* lines each containing four integers *i*, *j*, *c**ij* и *c**ji* (1<=≤<=*i*,<=*j*<=≤<=*n*,<=*i*<=≠<=*j*,<=<=-<=104<=≤<=*c**ij*,...
Print the minimum number of rooms that one needs to visit during one traverse of the cycle that can raise mood infinitely. If such cycle does not exist, print number 0.
[ "4 4\n1 2 -10 3\n1 3 1 -10\n2 4 -10 -1\n3 4 0 -3\n" ]
[ "4\n" ]
Cycle is such a sequence of rooms *a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*k*</sub>, that *a*<sub class="lower-index">1</sub> is connected with *a*<sub class="lower-index">2</sub>, *a*<sub class="lower-index">2</sub> is connected with *a*<sub class="low...
[]
46
0
0
307,481
731
80-th Level Archeology
[ "brute force", "data structures", "greedy", "sortings" ]
null
null
Archeologists have found a secret pass in the dungeon of one of the pyramids of Cycleland. To enter the treasury they have to open an unusual lock on the door. The lock consists of *n* words, each consisting of some hieroglyphs. The wall near the lock has a round switch. Each rotation of this switch changes the hierogl...
The first line of the input contains two integers *n* and *c* (2<=≤<=*n*<=≤<=500<=000, 1<=≤<=*c*<=≤<=106) — the number of words, written on the lock, and the number of different hieroglyphs. Each of the following *n* lines contains the description of one word. The *i*-th of these lines starts with integer *l**i* (1<=≤...
If it is possible to open the door by rotating the round switch, print integer *x* (0<=≤<=*x*<=≤<=*c*<=-<=1) that defines the required number of clockwise rotations. If there are several valid *x*, print any of them. If it is impossible to open the door by this method, print <=-<=1.
[ "4 3\n2 3 2\n1 1\n3 2 3 1\n4 2 3 1 2\n", "2 5\n2 4 2\n2 4 2\n", "4 4\n1 2\n1 3\n1 4\n1 2\n" ]
[ "1\n", "0\n", "-1\n" ]
Word *a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*m*</sub> of length *m* is lexicographically not greater than word *b*<sub class="lower-index">1</sub>, *b*<sub class="lower-index">2</sub>, ..., *b*<sub class="lower-index">*k*</sub> of length *k*, if one of...
[ { "input": "4 3\n2 3 2\n1 1\n3 2 3 1\n4 2 3 1 2", "output": "1" }, { "input": "2 5\n2 4 2\n2 4 2", "output": "0" }, { "input": "4 4\n1 2\n1 3\n1 4\n1 2", "output": "-1" }, { "input": "3 1\n2 1 1\n2 1 1\n3 1 1 1", "output": "0" }, { "input": "2 10\n14 9 6 7 1 6 9 3...
46
0
0
308,057
690
The Wall (hard)
[ "dp" ]
null
null
So many wall designs to choose from! Even modulo 106<=+<=3, it's an enormous number. Given that recently Heidi acquired an unlimited supply of bricks, her choices are endless! She really needs to do something to narrow them down. Heidi is quick to come up with criteria for a useful wall: - In a useful wall, at least...
The first and the only line of the input contains three space-separated integers *C*, *W* and *H* (1<=≤<=*C*<=≤<=108, 1<=≤<=*W*,<=*H*<=≤<=100).
Output the number of different walls, modulo 106<=+<=3, which are useless according to Heidi's criteria.
[ "1 1 1\n", "1 2 2\n", "1 2 3\n", "3 2 2\n", "5 4 9\n", "40 37 65\n" ]
[ "2\n", "3\n", "4\n", "19\n", "40951\n", "933869\n" ]
If there is no brick in any of the columns, the structure is considered as a useless wall.
[ { "input": "1 1 1", "output": "2" }, { "input": "1 2 2", "output": "3" }, { "input": "1 2 3", "output": "4" }, { "input": "3 2 2", "output": "19" }, { "input": "5 4 9", "output": "40951" }, { "input": "40 37 65", "output": "933869" }, { "in...
7,409
5,529,600
3
309,406
392
Three Arrays
[ "data structures" ]
null
null
There are three arrays *a*, *b* and *c*. Each of them consists of *n* integers. SmallY wants to find three integers *u*, *v*, *w* (0<=≤<=*u*,<=*v*,<=*w*<=≤<=*n*) such that the following condition holds: each number that appears in the union of *a*, *b* and *c*, appears either in the first *u* elements of *a*, or in the...
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* — array *a*. The third line contains the description of array *b* in the same format. The fourth line contains the description of array *c* in the same format. The following...
Print a single integer — the minimum possible sum of *u*<=+<=*v*<=+<=*w*.
[ "3\n1 1 101\n1 2 1\n3 2 1\n", "5\n1 1 2 2 3\n2 2 4 3 3\n3 3 1 1 1\n" ]
[ "5", "5" ]
In the first example you should choose *u* = 3, *v* = 0, *w* = 2. In the second example you should choose *u* = 1, *v* = 3, *w* = 1.
[]
46
0
0
309,558
490
Treeland Tour
[ "data structures", "dfs and similar", "dp", "trees" ]
null
null
The "Road Accident" band is planning an unprecedented tour around Treeland. The RA fans are looking forward to the event and making bets on how many concerts their favorite group will have. Treeland consists of *n* cities, some pairs of cities are connected by bidirectional roads. Overall the country has *n*<=-<=1 roa...
The first line of the input contains integer *n* (2<=≤<=*n*<=≤<=6000) — the number of cities in Treeland. The next line contains *n* integers *r*1,<=*r*2,<=...,<=*r**n* (1<=≤<=*r**i*<=≤<=106), where *r**i* is the population of the *i*-th city. The next *n*<=-<=1 lines contain the descriptions of the roads, one road per...
Print the number of cities where the "Road Accident" band will have concerts.
[ "6\n1 2 3 4 5 1\n1 2\n2 3\n3 4\n3 5\n3 6\n", "5\n1 2 3 4 5\n1 2\n1 3\n2 4\n3 5\n" ]
[ "4\n", "3\n" ]
none
[]
31
0
0
309,559
715
Complete the Permutations
[ "combinatorics", "fft", "graphs", "math" ]
null
null
ZS the Coder is given two permutations *p* and *q* of {1,<=2,<=...,<=*n*}, but some of their elements are replaced with 0. The distance between two permutations *p* and *q* is defined as the minimum number of moves required to turn *p* into *q*. A move consists of swapping exactly 2 elements of *p*. ZS the Coder wants...
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=250) — the number of elements in the permutations. The second line contains *n* integers, *p*1,<=*p*2,<=...,<=*p**n* (0<=≤<=*p**i*<=≤<=*n*) — the permutation *p*. It is guaranteed that there is at least one way to replace zeros such that *p* is a...
Print *n* integers, *i*-th of them should denote the answer for *k*<==<=*i*<=-<=1. Since the answer may be quite large, and ZS the Coder loves weird primes, print them modulo 998244353<==<=223·7·17<=+<=1, which is a prime.
[ "3\n1 0 0\n0 2 0\n", "4\n1 0 0 3\n0 0 0 4\n", "6\n1 3 2 5 4 6\n6 4 5 1 0 0\n", "4\n1 2 3 4\n2 3 4 1\n" ]
[ "1 2 1 \n", "0 2 6 4 \n", "0 0 0 0 1 1 \n", "0 0 0 1 \n" ]
In the first sample case, there is the only way to replace zeros so that it takes 0 swaps to convert *p* into *q*, namely *p* = (1, 2, 3), *q* = (1, 2, 3). There are two ways to replace zeros so that it takes 1 swap to turn *p* into *q*. One of these ways is *p* = (1, 2, 3), *q* = (3, 2, 1), then swapping 1 and 3 from...
[]
46
0
0
310,200
802
Heidi and Library (hard)
[ "flows" ]
null
null
The good times at Heidi's library are over. Marmots finally got their internet connections and stopped coming to the library altogether. Not only that, but the bookstore has begun charging extortionate prices for some books. Namely, whereas in the previous versions each book could be bought for 1 CHF, now the price of ...
The first line of input will contain two integers *n* and *k* (). The second line will contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*) – the sequence of book requests. The third line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (0<=≤<=*c**i*<=≤<=106) – the costs of the books.
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
[ "4 80\n1 2 2 1\n1 1 1 1\n", "4 1\n1 2 2 1\n1 1 1 1\n", "4 2\n1 2 3 1\n1 1 1 1\n", "7 2\n1 2 3 1 1 1 2\n1 10 1 0 0 0 0\n" ]
[ "2", "3", "3", "13" ]
The first three sample cases are repeated, but the fourth one is new. In the fourth test case, when buying book 3, Heidi should discard either book 1 or 2. Even though book 2 will be requested later than book 1, she should keep it, because it is so expensive to buy again.
[ { "input": "4 80\n1 2 2 1\n1 1 1 1", "output": "2" }, { "input": "4 1\n1 2 2 1\n1 1 1 1", "output": "3" }, { "input": "4 2\n1 2 3 1\n1 1 1 1", "output": "3" }, { "input": "7 2\n1 2 3 1 1 1 2\n1 10 1 0 0 0 0", "output": "13" }, { "input": "11 1\n1 2 3 5 1 10 10 1 1...
124
4,505,600
3
310,310
568
Longest Increasing Subsequence
[ "data structures", "dp" ]
null
null
Note that the memory limit in this problem is less than usual. Let's consider an array consisting of positive integers, some positions of which contain gaps. We have a collection of numbers that can be used to fill the gaps. Each number from the given collection can be used at most once. Your task is to determine su...
The first line contains a single integer *n* — the length of the array (1<=≤<=*n*<=≤<=105). The second line contains *n* space-separated integers — the elements of the sequence. A gap is marked as "-1". The elements that are not gaps are positive integers not exceeding 109. It is guaranteed that the sequence contains ...
Print *n* space-separated numbers in a single line — the resulting sequence. If there are multiple possible answers, print any of them.
[ "3\n1 2 3\n1\n10\n", "3\n1 -1 3\n3\n1 2 3\n", "2\n-1 2\n2\n2 4\n", "3\n-1 -1 -1\n5\n1 1 1 1 2\n", "4\n-1 -1 -1 2\n4\n1 1 2 2\n" ]
[ "1 2 3 \n", "1 2 3 \n", "2 2 \n", "1 1 2 \n", "1 2 1 2 \n" ]
In the first sample there are no gaps, so the correct answer is the initial sequence. In the second sample there is only one way to get an increasing subsequence of length 3. In the third sample answer "4 2" would also be correct. Note that only strictly increasing subsequences are considered. In the fifth sample th...
[]
77
7,168,000
0
312,645
550
Brackets in Implications
[ "constructive algorithms", "greedy", "implementation", "math" ]
null
null
Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false. Implication is written by using character '', and the arguments and the result of the implication are written as '0' (*false*) and '1' (*true*). ...
The first line contains integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of arguments in a logical expression. The second line contains *n* numbers *a*1,<=*a*2,<=...,<=*a**n* (), which means the values of arguments in the expression in the order they occur.
Print "NO" (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0. Otherwise, print "YES" in the first line and the logical expression with the required arrangement of brackets in the second line. The expression should only contain characters '0', '1', '-' (char...
[ "4\n0 1 1 0\n", "2\n1 1\n", "1\n0\n" ]
[ "YES\n(((0)-&gt;1)-&gt;(1-&gt;0))\n", "NO\n", "YES\n0\n" ]
none
[ { "input": "4\n0 1 1 0", "output": "YES\n0->1->1->0" }, { "input": "2\n1 1", "output": "NO" }, { "input": "1\n0", "output": "YES\n0" }, { "input": "4\n0 0 0 0", "output": "YES\n0->(0->0)->0" }, { "input": "6\n0 0 0 0 0 1", "output": "NO" }, { "input": ...
31
0
0
312,817
317
Balance
[ "constructive algorithms", "dfs and similar", "graphs", "trees" ]
null
null
A system of *n* vessels with water is given. Several pairs of vessels are connected by tubes with transfusion mechanisms. One may transfer an integer amount of liters of water between two vessels connected by such tube (tube works in both directions). There might be multiple tubes between two vessels. Total number of t...
First line of the input contains integers *n*, *v*, *e* (1<=≤<=*n*<=≤<=300, 1<=≤<=*v*<=≤<=109, 0<=≤<=*e*<=≤<=50000). Next two lines contain *n* integers each: initial *a**i* and the desired amounts *b**i* of water in corresponding vessels (0<=≤<=*a**i*,<=*b**i*<=≤<=*v*). Next *e* lines describe one tube each in the f...
Print "NO" (without quotes), if such sequence of transfusions does not exist. Otherwise print any suitable sequence in the following format. On the first line print the total number of transfusions *k* (*k* should not exceed 2·*n*2). In the following *k* lines print transfusions in the format *x* *y* *d* (transfusion ...
[ "2 10 1\n1 9\n5 5\n1 2\n", "2 10 0\n5 2\n4 2\n", "2 10 0\n4 2\n4 2\n" ]
[ "1\n2 1 4\n", "NO\n", "0\n" ]
none
[]
62
0
0
313,027
193
Hamming Distance
[ "constructive algorithms", "greedy", "math", "matrices" ]
null
null
Hamming distance between strings *a* and *b* of equal length (denoted by *h*(*a*,<=*b*)) is equal to the number of distinct integers *i* (1<=≤<=*i*<=≤<=|*a*|), such that *a**i*<=≠<=*b**i*, where *a**i* is the *i*-th symbol of string *a*, *b**i* is the *i*-th symbol of string *b*. For example, the Hamming distance betwe...
The first line contains space-separated integers *h*(*s*1,<=*s*2), *h*(*s*1,<=*s*3), *h*(*s*1,<=*s*4). The second line contains space-separated integers *h*(*s*2,<=*s*3) and *h*(*s*2,<=*s*4). The third line contains the single integer *h*(*s*3,<=*s*4). All given integers *h*(*s**i*,<=*s**j*) are non-negative and do no...
Print -1 if there's no suitable set of strings. Otherwise print on the first line number *len* — the length of each string. On the *i*-th of the next four lines print string *s*'*i*. If there are multiple sets with the minimum length of the strings, print any of them.
[ "4 4 4\n4 4\n4\n" ]
[ "6\naaaabb\naabbaa\nbbaaaa\nbbbbbb\n" ]
none
[ { "input": "4 4 4\n4 4\n4", "output": "6\naaaabb\naabbaa\nbbaaaa\nbbbbbb" }, { "input": "10 10 8\n8 8\n10", "output": "14\naaaabaaaabbbbb\nbaaabbbbbaaaaa\nbbbbaaaaaaaaaa\nbbbbbbbbbbbbbb" }, { "input": "50109 50255 50102\n50104 50011\n50085", "output": "75234\naaaaaaaaaaaaaaaaaaaaaaaa...
46
204,800
0
313,179
300
Empire Strikes Back
[ "binary search", "math", "number theory" ]
null
null
In a far away galaxy there is war again. The treacherous Republic made *k* precision strikes of power *a**i* on the Empire possessions. To cope with the republican threat, the Supreme Council decided to deal a decisive blow to the enemy forces. To successfully complete the conflict, the confrontation balance after th...
The first line contains integer *k* (1<=≤<=*k*<=≤<=106). The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (1<=≤<=*a**i*<=≤<=107).
Print the minimum positive integer *n*, needed for the Empire to win. Please, do not use the %lld to read or write 64-but integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
[ "2\n1000 1000\n", "1\n2\n" ]
[ "2000", "2" ]
none
[]
92
1,638,400
0
314,848
533
Landmarks
[ "data structures", "dp" ]
null
null
We have an old building with *n*<=+<=2 columns in a row. These columns support the ceiling. These columns are located in points with coordinates 0<==<=*x*0<=&lt;<=*x*1<=&lt;<=...<=&lt;<=*x**n*<=&lt;<=*x**n*<=+<=1. The leftmost and the rightmost columns are special, we will call them bearing, the other columns are ordin...
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of ordinary columns. The second line contains *n*<=+<=2 integers *x*0,<=*x*1,<=...,<=*x**n*,<=*x**n*<=+<=1 (*x*0<==<=0, *x**i*<=&lt;<=*x**i*<=+<=1 for 0<=≤<=*i*<=≤<=*n*, *x**n*<=+<=1<=≤<=109) — the coordinates of the columns. The third line contains...
Print a single number — the minimum possible durability of the column that you need to add in order to make the building stay. If you do not have to add the column, please print 0. Your answer will be checked with the relative or absolute error 10<=-<=4.
[ "2\n0 20 40 100\n15 40\n", "3\n0 4 10 28 30\n9 13 5\n" ]
[ "10\n", "0\n" ]
none
[]
46
0
0
318,467
913
Strongly Connected Tournament
[ "dp", "graphs", "math", "probabilities" ]
null
null
There is a chess tournament in All-Right-City. *n* players were invited to take part in the competition. The tournament is held by the following rules: 1. Initially, each player plays one game with every other player. There are no ties; 1. After that, the organizers build a complete directed graph with players as ve...
The first line of input contains a single integer *n* (2<=≤<=*n*<=≤<=2000) — the number of players. The second line contains two integers *a* and *b* (1<=≤<=*a*<=&lt;<=*b*<=≤<=100) — the numerator and the denominator of fraction .
In the only line print the expected value of total number of games played by all the players. Print the answer using the format above.
[ "3\n1 2\n", "3\n4 6\n", "4\n1 2\n" ]
[ "4\n", "142606340\n", "598946623\n" ]
In the first example the expected value is 4. In the second example the expected value is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/24bad8c170d1a2f751a54ab5a1ba5ce8bea4e73a.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the third example the expected value is <img align="m...
[]
46
0
0
318,562
946
Almost Increasing Array
[ "data structures", "dp" ]
null
null
We call an array almost increasing if we can erase not more than one element from it so that the array becomes strictly increasing (that is, every element is striclty greater than every element before it). You are given an array *a* consisting of *n* elements. You are allowed to replace any element with any integer nu...
The first line contains one integer *n* (2<=≤<=*n*<=≤<=200000) — the number of elements in *a*. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=109) — the array *a*.
Print the minimum number of replaces you have to perform so that *a* is almost increasing.
[ "5\n5 4 3 2 1\n", "5\n1 2 8 9 5\n" ]
[ "3\n", "0\n" ]
none
[ { "input": "5\n5 4 3 2 1", "output": "3" }, { "input": "5\n1 2 8 9 5", "output": "0" }, { "input": "15\n1013 8003 4947 359 17360 21594 25180 25764 22357 22357 16649 12544 23382 10579 25937", "output": "7" }, { "input": "8\n1 1 1 1 1 1 1 1", "output": "6" }, { "inp...
30
0
0
321,327
0
none
[ "none" ]
null
null
Mister B has a house in the middle of a giant plain field, which attracted aliens life. For convenience, aliens specified the Cartesian coordinate system on the field in such a way that Mister B's house has coordinates (0,<=0). After that they sent three beacons to the field, but something went wrong. One beacon was co...
The first line contains one integer *t* (1<=≤<=*t*<=≤<=1000) — the number of test cases. The next 3·*t* lines describe *t* test cases. Every test case is described in three lines as follows. Note that each parameter is given as a product of three factors. The first line of a test case contains three space-separated ...
Print *t* integers one per line — the answers for each test.
[ "3\n2 1 1\n2 1 1\n1 1 3\n1 5 1\n2 2 1\n1 1 2\n10 6 18\n2 103 2\n13 1 13\n" ]
[ "4\n7\n171\n" ]
First test case contains the following beacon positions: (2, 0) and (0, 2), *s* = 3. The following packets could be sent: ((2, 0), (0, 2), ( - 1, 0)), ((1, 0), (0, 2), (4, 0)), ((0, 0), (0, 2), (3, 1)), ((0, 0), (0, 1), ( - 6, 0)), where (*b*<sub class="lower-index">1</sub>, *b*<sub class="lower-index">2</sub>, *p*) ha...
[]
30
0
0
321,369
704
Iron Man
[ "data structures", "geometry", "trees" ]
null
null
Tony Stark is playing a game with his suits (they have auto-pilot now). He lives in Malibu. Malibu has *n* junctions numbered from 1 to *n*, connected with *n*<=-<=1 roads. One can get from a junction to any other junction using these roads (graph of Malibu forms a tree). Tony has *m* suits. There's a special plan for...
The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100<=000) — the number of junctions and the number of suits respectively. The next *n*<=-<=1 lines contain the roads descriptions. Each line contains two integers *a**i* and *b**i* — endpoints of the *i*-th road (1<=≤<=*a**i*,<=*b**i*<=...
If there would be no explosions at all, print -1 in the first and only line of output. Otherwise print the moment of the first explosion. Your answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6.
[ "6 4\n2 5\n6 5\n3 6\n4 6\n4 1\n27 6 1 3\n9 5 1 6\n27 4 3 4\n11 29 2 6\n", "6 4\n3 1\n4 5\n6 4\n6 1\n2 6\n16 4 4 5\n13 20 6 2\n3 16 4 5\n28 5 3 5\n" ]
[ "27.3\n", "-1\n" ]
none
[]
124
2,150,400
-1
322,821
981
K Paths
[ "combinatorics", "data structures", "dp", "fft", "math" ]
null
null
You are given a tree of $n$ vertices. You are to select $k$ (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and ...
The first line contains two integers $n$ and $k$ ($1 \leq n, k \leq 10^{5}$) — the number of vertices in the tree and the desired number of paths. The next $n - 1$ lines describe edges of the tree. Each line contains two integers $a$ and $b$ ($1 \le a, b \le n$, $a \ne b$) — the endpoints of an edge. It is guaranteed ...
Print the number of ways to select $k$ enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all $k$ paths, and the intersection of all paths is non-empty. As the answer can be large, prin...
[ "3 2\n1 2\n2 3\n", "5 1\n4 1\n2 3\n4 5\n2 1\n", "29 29\n1 2\n1 3\n1 4\n1 5\n5 6\n5 7\n5 8\n8 9\n8 10\n8 11\n11 12\n11 13\n11 14\n14 15\n14 16\n14 17\n17 18\n17 19\n17 20\n20 21\n20 22\n20 23\n23 24\n23 25\n23 26\n26 27\n26 28\n26 29\n" ]
[ "7\n", "10\n", "125580756\n" ]
In the first example the following ways are valid: - $((1,2), (1,2))$, - $((1,2), (1,3))$, - $((1,3), (1,2))$, - $((1,3), (1,3))$, - $((1,3), (2,3))$, - $((2,3), (1,3))$, - $((2,3), (2,3))$. In the second example $k=1$, so all $n \cdot (n - 1) / 2 = 5 \cdot 4 / 2 = 10$ paths are valid. In the third example, t...
[]
46
0
0
323,630
917
Pollywog
[ "combinatorics", "dp", "matrices" ]
null
null
As we all know, Dart is some kind of creature from Upside Down world. For simplicity, we call their kind pollywogs. Dart and *x*<=-<=1 other pollywogs are playing a game. There are *n* stones in a row, numbered from 1 through *n* from left to right. At most 1 pollywog may be sitting on each stone at a time. Initially, ...
The first line of input contains four integers, *x*,<=*k*,<=*n* and *q* (1<=≤<=*x*<=≤<=*k*<=≤<=8, *k*<=≤<=*n*<=≤<=108, 0<=≤<=*q*<=≤<=*min*(25,<=*n*<=-<=*x*)) — the number of pollywogs, the maximum length of jump, the number of stones and the number of special stones. The next line contains *k* integers, *c*1,<=*c*2,<=...
Print the minimum amount of energy they need, in the first and only line of output.
[ "2 3 10 2\n1 2 3\n5 -10\n6 1000\n", "4 7 85 3\n17 5 28 4 52 46 6\n59 -76\n33 -69\n19 2018\n" ]
[ "6\n", "135\n" ]
none
[]
46
0
0
324,011
776
Sherlock's bet to Moriarty
[ "constructive algorithms", "data structures", "divide and conquer", "geometry", "graphs", "implementation", "trees" ]
null
null
Sherlock met Moriarty for a final battle of wits. He gave him a regular *n* sided convex polygon. In addition to it, he gave him certain diagonals to form regions on the polygon. It was guaranteed that the diagonals did not intersect in interior points. He took each of the region and calculated its importance value. I...
First line contains two integers *n* and *m* (3<=≤<=*n*<=≤<=100000, 0<=≤<=*m*<=≤<=*n*<=-<=3), the number of vertices in the polygon and the number of diagonals added. Each of the next *m* lines contains two integers *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=*n*), describing a diagonal between vertices *a* and *b*. It is guaran...
Let the number of regions be *k*. Output *k* space-separated integers, each between 1 and 20, representing the colors of the regions in the order of increasing importance. If there are multiple answers, print any of them. It can be shown that at least one answer exists.
[ "4 1\n1 3\n", "6 3\n1 3\n1 4\n1 5\n" ]
[ "1 2\n", "2 1 2 3\n" ]
In 2nd input, regions formed in order after sorting will be (1, 2, 3), (1, 3, 4), (1, 4, 5), (1, 5, 6), i.e, region (1, 2, 3) is first region followed by region (1, 3, 4) and so on. So, we can color regions 1 and 3 with same color, as region number 2 is on the path from 1 to 3 and it has color 1 which is less than col...
[]
46
0
0
324,560
833
Caramel Clouds
[ "data structures", "dp", "sortings" ]
null
null
It is well-known that the best decoration for a flower bed in Sweetland are vanilla muffins. Seedlings of this plant need sun to grow up. Slastyona has *m* seedlings, and the *j*-th seedling needs at least *k**j* minutes of sunlight to grow up. Most of the time it's sunny in Sweetland, but sometimes some caramel cloud...
The first line contains two integers *n* and *C* (0<=≤<=*n*<=≤<=3·105,<=0<=≤<=*C*<=≤<=109) – the number of caramel clouds and the number of candies Slastyona has. The next *n* lines contain three integers each: *l**i*,<=*r**i*,<=*c**i* (0<=≤<=*l**i*<=&lt;<=*r**i*<=≤<=109,<=0<=≤<=*c**i*<=≤<=109), describing one caramel...
For each seedling print one integer – the minimum minute Slastyona can grow it up.
[ "3 5\n1 7 1\n1 6 2\n1 7 1\n3\n7\n2\n5\n", "3 15\n1 4 17\n2 8 6\n4 8 9\n2\n5\n1\n", "2 10\n3 7 9\n10 90 10\n2\n10\n100\n" ]
[ "12\n7\n10\n", "8\n1\n", "10\n104\n" ]
Consider the first example. For each *k* it is optimal to dispel clouds 1 and 3. Then the remaining cloud will give shadow on time segment [1..6]. So, intervals [0..1] and [6..*inf*) are sunny. In the second example for *k* = 1 it is not necessary to dispel anything, and for *k* = 5 the best strategy is to dispel clou...
[]
62
0
0
325,171
690
Tree of Life (hard)
[ "trees" ]
null
null
To add insult to injury, the zombies have taken all but two drawings from Heidi! Please help her recover the Tree of Life from only these two drawings.
The input format is the same as in the medium version, except that now the bound on *n* is 2<=≤<=*n*<=≤<=1000 and that *k*<==<=2.
The same as in the medium version.
[ "1\n9 2\n6\n4 3\n5 4\n6 1\n8 6\n8 2\n7 1\n5\n8 6\n8 7\n8 1\n7 3\n5 1\n" ]
[ "YES\n2 1\n3 1\n5 4\n6 5\n7 6\n8 5\n9 8\n1 4\n" ]
none
[]
61
0
0
326,059
360
Levko and Sets
[ "number theory" ]
null
null
Levko loves all sorts of sets very much. Levko has two arrays of integers *a*1,<=*a*2,<=... ,<=*a**n* and *b*1,<=*b*2,<=... ,<=*b**m* and a prime number *p*. Today he generates *n* sets. Let's describe the generation process for the *i*-th set: 1. First it has a single number 1. 1. Let's take any element *c* from t...
The first line contains three integers *n*, *m* and *p* (1<=≤<=*n*<=≤<=104, 1<=≤<=*m*<=≤<=105, 2<=≤<=*p*<=≤<=109), *p* is prime. The second line contains space-separated integers *a*1,<=*a*2,<=... ,<=*a**n* (1<=≤<=*a**i*<=&lt;<=*p*). The third line contains space-separated integers *b*1,<=*b*2,<=... ,<=*b**m* (1<=≤<=...
The single number — the size of the union of the sets.
[ "1 1 7\n2\n5\n", "1 2 7\n2\n2 4\n", "2 1 7\n1 6\n2\n", "2 1 7\n1 6\n5\n" ]
[ "3\n", "3\n", "1\n", "2\n" ]
none
[]
31
0
0
331,105
313
Ilya and Two Numbers
[ "constructive algorithms", "data structures", "dsu", "greedy" ]
null
null
Ilya has recently taken up archaeology. He's recently found two numbers, written in the *m*-based notation. Each of the found numbers consisted of exactly *n* digits. Ilya immediately started looking for information about those numbers. He learned that the numbers are part of a cyphered code and the one who can decyphe...
The first line contains two integers *n*,<=*m* (1<=≤<=*n*,<=*m*<=≤<=105,<=*m*<=&gt;<=1). The second line contains the first found number, the third line contains the second found number. The numbers are recorded as a sequence of digits in the *m*-based notation. Each digit is an integer from 0 to *m*<=-<=1. The digit...
Print *n* *m*-base digits. The resulting third number written in the *m*-based notation. Print the digits in the order from the most significant digits to the least significant ones.
[ "4 7\n5 4 3 2\n5 6 5 4\n", "5 5\n2 4 4 1 3\n1 0 1 2 4\n" ]
[ "6 4 2 1 \n", "4 4 4 3 2 \n" ]
none
[]
92
0
0
332,057
0
none
[ "none" ]
null
null
Andryusha has found a perplexing arcade machine. The machine is a vertically adjusted board divided into square cells. The board has *w* columns numbered from 1 to *w* from left to right, and *h* rows numbered from 1 to *h* from the bottom to the top. Further, there are barriers in some of board rows. There are *n* ba...
The first line contains three integers *h*, *w*, and *n* (1<=≤<=*h*<=≤<=109, 2<=≤<=*w*<=≤<=105, 0<=≤<=*n*<=≤<=105) — the number of rows, columns, and barriers in the machine respectively. Next *n* lines describe barriers. *i*-th of these lines containts four integers *u**i*, *l**i*, *r**i*, and *s**i* (1<=≤<=*u**i*<=≤...
Print one integer — the answer to the problem modulo 109<=+<=7.
[ "10 5 1\n3 2 3 10\n", "10 5 2\n3 1 3 10\n5 3 5 10\n", "10 5 2\n3 1 3 7\n5 3 5 10\n", "10 15 4\n7 3 9 5\n6 4 10 1\n1 1 4 10\n4 11 11 20\n" ]
[ "7\n", "16\n", "14\n", "53\n" ]
In the first sample case, there is a single barrier: if one throws a marble in the second or the third column, two marbles come out, otherwise there is only one. The total answer is 7. In the second sample case, the numbers of resulting marbles are 2, 2, 4, 4, 4 in order of indexing columns with the initial marble. I...
[]
30
0
0
333,517
477
Dreamoon and Binary
[ "dp", "strings" ]
null
null
Dreamoon saw a large integer *x* written on the ground and wants to print its binary form out. Dreamoon has accomplished the part of turning *x* into its binary format. Now he is going to print it in the following manner. He has an integer *n*<==<=0 and can only perform the following two operations in any order for un...
The single line of the input contains a binary integer representing *x* (1<=≤<=*x*<=&lt;<=25000) without leading zeros.
The first line of the output should contain an integer representing the number of different ideal sequences modulo 1000000007 (109<=+<=7). The second line of the output contains an integer representing the minimal length of an ideal sequence modulo 1000000007 (109<=+<=7).
[ "101\n", "11010\n" ]
[ "1\n6\n", "3\n5\n" ]
For the first sample, the shortest and the only ideal sequence is «222221» of length 6. For the second sample, there are three ideal sequences «21211», «212222222221», «222222222222222222222222221». Among them the shortest one has length 5.
[]
46
0
0
335,352
241
Numbers
[]
null
null
You have a sequence of *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*). You want to remove some integers in such a way that the resulting sequence of integers satisfies the following three conditions: 1. the resulting sequence is not empty; 1. the exclusive or (*xor* operation) of all the int...
The first line of the input contains two integers *n* and *p* (1<=≤<=*n*,<=*p*<=≤<=50000). Next line contains *n* space-separated distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*). It is guaranteed that *p* is a prime number.
If there is no solution for the given input, print "No" (without quotes) in the only line of the output. Otherwise print "Yes" in the first line of output. The second line should contain an integer *k* (*k*<=&gt;<=0) specifying the number of remaining elements and the third line should contain *k* distinct integers *x...
[ "3 3\n1 2 3\n", "3 5\n1 2 3\n" ]
[ "Yes\n3\n1 2 3 \n", "No\n" ]
none
[]
0
0
-1
336,019
500
New Year Santa Network
[ "combinatorics", "dfs and similar", "graphs", "trees" ]
null
null
New Year is coming in Tree World! In this world, as the name implies, there are *n* cities connected by *n*<=-<=1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to *n*, and the roads are numbered by integers from 1 to *n*<=-<=1. Let's define *d...
The first line contains an integer *n* (3<=≤<=*n*<=≤<=105) — the number of cities in Tree World. Next *n*<=-<=1 lines describe the roads. The *i*-th line of them (1<=≤<=*i*<=≤<=*n*<=-<=1) contains three space-separated integers *a**i*, *b**i*, *l**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*, 1<=≤<=*l**i*<=≤<=...
Output *q* numbers. For each given change, print a line containing the expected cost needed to build the network in Tree World. The answer will be considered correct if its absolute and relative error doesn't exceed 10<=-<=6.
[ "3\n2 3 5\n1 3 3\n5\n1 4\n2 2\n1 2\n2 1\n1 1\n", "6\n1 5 3\n5 3 2\n6 1 7\n1 4 4\n5 2 3\n5\n1 2\n2 1\n3 5\n4 1\n5 2\n" ]
[ "14.0000000000\n12.0000000000\n8.0000000000\n6.0000000000\n4.0000000000\n", "19.6000000000\n18.6000000000\n16.6000000000\n13.6000000000\n12.6000000000\n" ]
Consider the first sample. There are 6 triples: (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1). Because *n* = 3, the cost needed to build the network is always *d*(1, 2) + *d*(2, 3) + *d*(3, 1) for all the triples. So, the expected cost equals to *d*(1, 2) + *d*(2, 3) + *d*(3, 1).
[]
30
0
0
336,061
391
Three Trees
[]
null
null
This problem consists of two subproblems: for solving subproblem E1 you will receive 11 points, and for solving subproblem E2 you will receive 13 points. A tree is an undirected connected graph containing no cycles. The distance between two nodes in an unweighted tree is the minimum number of edges that have to be tra...
The first line contains three space-separated integers *n*1, *n*2, *n*3 — the number of vertices in the first, second, and third trees, respectively. The following *n*1<=-<=1 lines describe the first tree. Each of these lines describes an edge in the first tree and contains a pair of integers separated by a single spac...
Print a single integer number — the maximum possible sum of distances between all pairs of nodes in the united tree.
[ "2 2 3\n1 2\n1 2\n1 2\n2 3\n", "5 1 4\n1 2\n2 5\n3 4\n4 2\n1 2\n1 3\n1 4\n" ]
[ "56\n", "151\n" ]
Consider the first test case. There are two trees composed of two nodes, and one tree with three nodes. The maximum possible answer is obtained if the trees are connected in a single chain of 7 vertices. In the second test case, a possible choice of new edges to obtain the maximum answer is the following: - Connect...
[]
93
0
-1
336,373
494
Sharti
[ "data structures", "games" ]
null
null
During the last 24 hours Hamed and Malek spent all their time playing "Sharti". Now they are too exhausted to finish the last round. So they asked you for help to determine the winner of this round. "Sharti" is played on a *n*<=×<=*n* board with some of cells colored white and others colored black. The rows of the bo...
In this problem the initial board is specified as a set of *m* rectangles. All cells that lie inside at least one of these rectangles are colored white and the rest are colored black. In the first line of input three space-spereated integers *n*,<=*m*,<=*k* (1<=≤<=*k*<=≤<=*n*<=≤<=109, 1<=≤<=*m*<=≤<=5·104) follow, deno...
If Hamed wins, print "Hamed", otherwise print "Malek" (without the quotes).
[ "5 2 1\n1 1 3 3\n2 2 4 4\n", "12 5 7\n3 4 5 6\n1 2 1 2\n4 5 9 9\n8 6 12 10\n12 4 12 4\n" ]
[ "Malek\n", "Hamed\n" ]
none
[]
46
0
0
337,791
542
Superhero's Job
[ "dfs and similar", "dp", "hashing", "math", "number theory" ]
null
null
It's tough to be a superhero. And it's twice as tough to resist the supervillain who is cool at math. Suppose that you're an ordinary Batman in an ordinary city of Gotham. Your enemy Joker mined the building of the city administration and you only have several minutes to neutralize the charge. To do that you should ent...
The single line of the input contains a single integer *A* (1<=≤<=*A*<=≤<=1012).
Print the number of solutions of the equation *J*(*x*)<==<=*A*.
[ "3\n", "24\n" ]
[ "1\n", "3\n" ]
Record *x*|*n* means that number *n* divides number *x*. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f651455cf24acab8e8b76da4abe0ec8f028ec17b.png" style="max-width: 100.0%;max-height: 100.0%;"/> is defined as the largest positive integer that divides both *a* and *b*. In the first sam...
[]
31
0
0
337,991
231
Magic Box
[ "brute force", "geometry" ]
null
null
One day Vasya was going home when he saw a box lying on the road. The box can be represented as a rectangular parallelepiped. Vasya needed no time to realize that the box is special, as all its edges are parallel to the coordinate axes, one of its vertices is at point (0,<=0,<=0), and the opposite one is at point (*x*1...
The fist input line contains three space-separated integers *x*, *y* and *z* (|*x*|,<=|*y*|,<=|*z*|<=≤<=106) — the coordinates of Vasya's position in space. The second line contains three space-separated integers *x*1, *y*1, *z*1 (1<=≤<=*x*1,<=*y*1,<=*z*1<=≤<=106) — the coordinates of the box's vertex that is opposite ...
Print a single integer — the sum of all numbers on the box faces that Vasya sees.
[ "2 2 2\n1 1 1\n1 2 3 4 5 6\n", "0 0 10\n3 2 3\n1 2 3 4 5 6\n" ]
[ "12\n", "4\n" ]
The first sample corresponds to perspective, depicted on the picture. Vasya sees numbers *a*<sub class="lower-index">2</sub> (on the top face that is the darkest), *a*<sub class="lower-index">6</sub> (on the right face that is the lightest) and *a*<sub class="lower-index">4</sub> (on the left visible face). In the sec...
[ { "input": "2 2 2\n1 1 1\n1 2 3 4 5 6", "output": "12" }, { "input": "0 0 10\n3 2 3\n1 2 3 4 5 6", "output": "4" }, { "input": "0 1 2\n1 1 1\n634728 627299 454463 927148 298618 186257", "output": "927148" }, { "input": "5 2 -4\n1 1 1\n279519 704273 181008 670653 198973 996401...
154
0
3
338,140
70
Professor's task
[ "data structures", "geometry" ]
D. Professor's task
1
256
Once a walrus professor Plato asked his programming students to perform the following practical task. The students had to implement such a data structure that would support a convex hull on some set of points *S*. The input to the program had *q* queries of two types: 1. Add a point with coordinates (*x*,<=*y*) int...
The first line contains an integer *q* (4<=≤<=*q*<=≤<=105). Then follow *q* lines in the following way: "*t* *x* *y*", where *t* is the query type (1 or 2), and (*x*,<=*y*) are the coordinates of the point (<=-<=106<=≤<=*x*,<=*y*<=≤<=106, *x* and *y* are integers). There is at least one query of type 2. It is guar...
For each query of the second type print one string containing "YES", if the point lies inside the convex hull or on its border. Otherwise, print "NO".
[ "8\n1 0 0\n1 2 0\n1 2 2\n2 1 0\n1 0 2\n2 1 1\n2 2 1\n2 20 -1\n" ]
[ "YES\nYES\nYES\nNO\n" ]
none
[]
0
0
-1
339,383
89
Chip Play
[ "brute force", "data structures", "implementation" ]
C. Chip Play
4
256
Let's consider the following game. We have a rectangular field *n*<=×<=*m* in size. Some squares of the field contain chips. Each chip has an arrow painted on it. Thus, each chip on the field points in one of the following directions: up, down, left or right. The player may choose a chip and make a move with it. The...
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*,<=*n*<=×<=*m*<=≤<=5000). Then follow *n* lines containing *m* characters each — that is the game field description. "." means that this square is empty. "L", "R", "U", "D" mean that this square contains a chip and an arrow on it says left, right, up or d...
Print two numbers — the maximal number of points a player can get after a move and the number of moves that allow receiving this maximum number of points.
[ "4 4\nDRLD\nU.UL\n.UUR\nRDDL\n", "3 5\n.D...\nRRRLL\n.U...\n" ]
[ "10 1", "6 2" ]
In the first sample the maximum number of points is earned by the chip in the position (3, 3). You can see its progress at the following picture: All other chips earn fewer points.
[ { "input": "4 4\nDRLD\nU.UL\n.UUR\nRDDL", "output": "10 1" }, { "input": "2 2\nRD\nUL", "output": "4 4" }, { "input": "5 5\nUD..L\nRRD..\n..RDD\nUUL..\nDDLRR", "output": "8 3" }, { "input": "10 10\n.L.....R..\n.........U\n..D....D..\n.R......L.\n....L.....\n.........D\n.........
92
0
0
339,910
73
Plane of Tanks
[ "brute force", "geometry" ]
F. Plane of Tanks
4
256
Vasya plays the Plane of Tanks. The tanks in this game keep trying to finish each other off. But your "Pedalny" is not like that... He just needs to drive in a straight line from point *A* to point B on the plane. Unfortunately, on the same plane are *n* enemy tanks. We shall regard all the tanks as points. At the init...
The first line contains 4 numbers – the coordinates of points *A* and *B* (in meters), the points do not coincide. On the second line number *n* is given (1<=≤<=*n*<=≤<=104). It is the number of enemy tanks. Each of the following *n* lines contain the coordinates of a corresponding tank *x**i*,<=*y**i* and its paramete...
Print a single number with absolute or relative error no more than 10<=-<=4 — the minimum required speed of Pedalny in meters per second.
[ "0 0 10 0\n1\n5 -5 4.71238 1\n0\n", "0 0 10 0\n1\n5 -5 4.71238 1\n1\n" ]
[ "4.2441\n", "0.0000\n" ]
none
[]
46
0
0
340,104
811
Vladik and Entertaining Flags
[ "data structures", "dsu", "graphs" ]
null
null
In his spare time Vladik estimates beauty of the flags. Every flag could be represented as the matrix *n*<=×<=*m* which consists of positive integers. Let's define the beauty of the flag as number of components in its matrix. We call component a set of cells with same numbers and between any pair of cells from that s...
First line contains three space-separated integers *n*, *m*, *q* (1<=≤<=*n*<=≤<=10, 1<=≤<=*m*,<=*q*<=≤<=105) — dimensions of flag matrix and number of segments respectively. Each of next *n* lines contains *m* space-separated integers — description of flag matrix. All elements of flag matrix is positive integers not e...
For each segment print the result on the corresponding line.
[ "4 5 4\n1 1 1 1 1\n1 2 2 3 3\n1 1 1 2 5\n4 4 5 5 5\n1 5\n2 5\n1 2\n4 5\n" ]
[ "6\n7\n3\n4\n" ]
Partitioning on components for every segment from first test case: <img class="tex-graphics" src="https://espresso.codeforces.com/5c89ff7036ddb39d2997c8f594d4a0729e524ab0.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "4 5 4\n1 1 1 1 1\n1 2 2 3 3\n1 1 1 2 5\n4 4 5 5 5\n1 5\n2 5\n1 2\n4 5", "output": "6\n7\n3\n4" }, { "input": "5 2 9\n6 1\n6 6\n6 6\n6 6\n5 6\n1 2\n1 1\n1 2\n1 2\n1 2\n1 1\n1 1\n1 2\n1 1", "output": "3\n2\n3\n3\n3\n2\n2\n3\n2" }, { "input": "5 4 10\n5 5 5 5\n5 5 5 5\n5 5 5 5\n...
46
5,529,600
0
340,188
500
New Year Running
[ "number theory", "trees" ]
null
null
New Year is coming in Tree Island! In this island, as the name implies, there are *n* cities connected by *n*<=-<=1 roads, and for any two distinct cities there always exists exactly one path between them. For every person in Tree Island, it takes exactly one minute to pass by exactly one road. There is a weird New Ye...
The first line contains a single positive integer *n* (5<=≤<=*n*<=≤<=2<=×<=105) — the number of cities in Tree Island. Next *n*<=-<=1 lines describe the roads of Tree Island. The *i*-th line (1<=≤<=*i*<=≤<=*n*<=-<=1) of them contains two space-separated integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*,<=*a**i...
For each test case, print an integer describing the amount of time they should run in minutes. If they have to run for an infinitely long time (in other words, if they never meet at the same city), print -1 instead. If they meet at the beginning of their run, print 0.
[ "7\n1 3\n3 6\n7 4\n3 7\n5 4\n7 2\n4\n6 5 5 3\n3 5 4 6\n1 5 1 3\n1 5 3 1\n" ]
[ "2\n1\n0\n-1\n" ]
The example looks like:
[]
30
0
0
345,746
271
Secret
[ "constructive algorithms", "implementation" ]
null
null
The Greatest Secret Ever consists of *n* words, indexed by positive integers from 1 to *n*. The secret needs dividing between *k* Keepers (let's index them by positive integers from 1 to *k*), the *i*-th Keeper gets a non-empty set of words with numbers from the set *U**i*<==<=(*u**i*,<=1,<=*u**i*,<=2,<=...,<=*u**i*,<=...
The input consists of a single line which contains two integers *n* and *k* (2<=≤<=*k*<=≤<=*n*<=≤<=106) — the number of words in the secret and the number of the Keepers. The numbers are separated by a single space.
If there is no way to keep the secret safe, print a single integer "-1" (without the quotes). Otherwise, print *n* integers, the *i*-th of them representing the number of the Keeper who's got the *i*-th word of the secret. If there are multiple solutions, print any of them.
[ "11 3\n", "5 2\n" ]
[ "3 1 2 1 1 2 3 2 2 3 1\n", "-1\n" ]
none
[ { "input": "11 3", "output": "3 1 2 1 1 2 3 2 2 3 1" }, { "input": "5 2", "output": "-1" }, { "input": "2 2", "output": "-1" }, { "input": "3 2", "output": "-1" }, { "input": "6 2", "output": "1 1 2 2 1 2" }, { "input": "1000000 333333", "output": ...
2,000
31,232,000
0
345,927
60
Savior
[ "brute force", "dsu", "math" ]
D. Savior
4
256
Misha decided to help Pasha and Akim be friends again. He had a cunning plan — to destroy all the laughy mushrooms. He knows that the laughy mushrooms can easily burst when they laugh. Mushrooms grow on the lawns. There are *a*[*t*] mushrooms on the *t*-th lawn. Misha knows that the lawns where the mushrooms grow have...
The first line contains one integer *n* (1<=≤<=*n*<=≤<=106) which is the number of lawns. The next line contains *n* integers *a**i* which are the number of mushrooms on the *i*-lawn (1<=≤<=*a**i*<=≤<=107). All the numbers are different.
Print a single number — the minimal number of lawns on which Misha should laugh for all the mushrooms to burst.
[ "1\n2\n", "2\n1 2\n", "2\n3 5\n" ]
[ "1\n", "2\n", "1\n" ]
none
[]
0
0
-1
346,779
101
Candies and Stones
[ "divide and conquer", "dp" ]
E. Candies and Stones
7
45
Little Gerald and his coach Mike play an interesting game. At the beginning of the game there is a pile consisting of *n* candies and a pile consisting of *m* stones. Gerald and Mike move in turns, Mike goes first. During his move Mike checks how many candies and stones Gerald has eaten. Let Gerald eat *a* candies and ...
The first line contains three integers *n*,<=*m*,<=*p* (1<=≤<=*n*,<=*m*<=≤<=20000, 1<=≤<=*p*<=≤<=109). The second line contains *n* integers *x*0, *x*1, ..., *x**n*<=-<=1 (0<=≤<=*x**i*<=≤<=20000). The third line contains *m* integers *y*0, *y*1, ..., *y**m*<=-<=1 (0<=≤<=*y**i*<=≤<=20000). The value of *f*(*a*,<=*b*) is...
Print on the first line the only number: the maximal number of points Gerald can earn. Print on the second line a sting consisting of *n*<=+<=*m*<=-<=2 characters, each of which is either a "C" or "S", the *i*-th character should be "C" if Gerald's *i*-th move should be eating a candy and "S" if he should eat a stone.
[ "2 2 10\n0 0\n0 1\n", "3 3 10\n0 2 0\n0 0 2\n", "3 3 2\n0 1 1\n1 1 0\n" ]
[ "2\nSC\n", "10\nCSSC\n", "4\nSCSC\n" ]
In the first test if Gerald's first move is eating a stone, he will receive a point for it and if he eats a candy, he will get zero pints. In any way Gerald will get 0 points before his first move, and 1 after his second one. This, the maximum number of points Gerald can get equals to 2, and for that he should first ea...
[]
7,500
12,595,200
0
348,261
390
Inna and Large Sweet Matrix
[]
null
null
Inna loves sweets very much. That's why she wants to play the "Sweet Matrix" game with Dima and Sereja. But Sereja is a large person, so the game proved small for him. Sereja suggested playing the "Large Sweet Matrix" game. The "Large Sweet Matrix" playing field is an *n*<=×<=*m* matrix. Let's number the rows of the m...
The first line of the input contains three integers *n*, *m* and *w* (3<=≤<=*n*,<=*m*<=≤<=4·106; 1<=≤<=*w*<=≤<=105). The next *w* lines describe the moves that were made in the game. - A line that describes an event of the first type contains 6 integers: 0, *x*1, *y*1, *x*2, *y*2 and *v* (1<=≤<=*x*1<=≤<=*x*2<=≤<=*n...
For each second type move print a single integer on a single line — the difference between Dima and Inna's numbers.
[ "4 5 5\n0 1 1 2 3 2\n0 2 2 3 3 3\n0 1 5 4 5 1\n1 2 3 3 4\n1 3 4 3 4\n" ]
[ "2\n-21\n" ]
Note to the sample. After the first query the matrix looks as: After the second one it is: After the third one it is: For the fourth query, Dima's sum equals 5 + 0 + 3 + 0 = 8 and Inna's sum equals 4 + 1 + 0 + 1 = 6. The answer to the query equals 8 - 6 = 2. For the fifth query, Dima's sum equals 0 and Inna's sum...
[]
31
0
0
348,343
400
Inna and Binary Logic
[ "binary search", "bitmasks", "data structures" ]
null
null
Inna is fed up with jokes about female logic. So she started using binary logic instead. Inna has an array of *n* elements *a*1[1],<=*a*1[2],<=...,<=*a*1[*n*]. Girl likes to train in her binary logic, so she does an exercise consisting of *n* stages: on the first stage Inna writes out all numbers from array *a*1, on t...
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105) — size of array *a*1 and number of Dima's questions. Next line contains *n* integers *a*1[1],<=*a*1[2],<=...,<=*a*1[*n*] (0<=≤<=*a**i*<=≤<=105) — initial array elements. Each of next *m* lines contains two integers — Dima's question description...
For each question print Inna's answer on a single line.
[ "3 4\n1 1 1\n1 1\n2 2\n3 2\n1 2\n" ]
[ "6\n4\n7\n12\n" ]
none
[ { "input": "3 4\n1 1 1\n1 1\n2 2\n3 2\n1 2", "output": "6\n4\n7\n12" }, { "input": "1 1\n0\n1 0", "output": "0" }, { "input": "1 10\n1\n1 9\n1 10\n1 2\n1 0\n1 8\n1 9\n1 9\n1 0\n1 6\n1 8", "output": "9\n10\n2\n0\n8\n9\n9\n0\n6\n8" } ]
46
0
0
349,909
175
Power Defence
[ "brute force", "dp", "geometry", "greedy" ]
null
null
Vasya plays the Power Defence. He must pass the last level of the game. In order to do this he must kill the Main Villain, who moves in a straight line at speed 1 meter per second from the point (<=-<=∞,<=0) to the point (<=+<=∞,<=0) of the game world. In the points (*x*,<=1) and (*x*,<=<=-<=1), where *x* is an integ...
The first line contains three integer numbers *nf*,<=*ne* and *ns* — the maximum number of fire-towers, electric-towers and freezing-towers that can be built (0<=≤<=*nf*,<=*ne*,<=*ns*<=≤<=20,<=1<=≤<=*nf*<=+<=*ne*<=+<=*ns*<=≤<=20). The numbers are separated with single spaces. The second line contains three integer num...
Print the only real number — the maximum possible damage to the Main Villain with absolute or relative error not more than 10<=-<=6.
[ "1 0 0\n10 10 10\n100 100\n", "1 0 1\n10 10 10\n100 100\n" ]
[ "1989.97487421", "3979.94974843" ]
In the first sample we've got one fire-tower that always inflicts the same damage, independently of its position. In the second sample we've got another freezing-tower of the same action radius. If we build the two towers opposite each other, then the Main Villain's speed will be two times lower, whenever he enters t...
[]
62
0
0
350,344
343
Pumping Stations
[ "brute force", "dfs and similar", "divide and conquer", "flows", "graphs", "greedy", "trees" ]
null
null
Mad scientist Mike has applied for a job. His task is to manage a system of water pumping stations. The system consists of *n* pumping stations, which are numbered by integers from 1 to *n*. Some pairs of stations are connected by bidirectional pipes through which water can flow in either direction (but only in one at...
The first line of the input contains two space-separated integers *n* and *m* (2<=≤<=*n*<=≤<=200, 1<=≤<=*m*<=≤<=1000) — the number of stations and pipes in the system, accordingly. The *i*-th of the next *m* lines contains three space-separated integers *a**i*, *b**i* and *c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=...
On the first line print a single integer — the maximum salary Mike can earn. On the second line print a space-separated permutation of *n* numbers from 1 to *n* — the numbers of stations in the sequence *v*1, *v*2, ..., *v**n*. If there are multiple answers, print any of them.
[ "6 11\n1 2 10\n1 6 8\n2 3 4\n2 5 2\n2 6 3\n3 4 5\n3 5 4\n3 6 2\n4 5 7\n4 6 2\n5 6 3\n" ]
[ "77\n6 2 1 5 3 4 \n" ]
none
[]
60
0
0
352,666
380
Sereja and Tree
[ "graphs", "implementation" ]
null
null
Sereja adores trees. Today he came up with a revolutionary new type of binary root trees. His new tree consists of *n* levels, each vertex is indexed by two integers: the number of the level and the number of the vertex on the current level. The tree root is at level 1, its index is (1,<=1). Here is a pseudo code of t...
The first line contains integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=7000). Next *m* lines contain the descriptions of the operations. The operation of the first type is given by five integers: 1 *t* *l* *r* *x* (1<=≤<=*t*<=≤<=*n*; 1<=≤<=*l*<=≤<=*r*<=≤<=*cnt*[*t*]; 1<=≤<=*x*<=≤<=106). The operation of the second type is...
For each operation of the second type, print the answer on a single line.
[ "4 5\n1 4 4 7 1\n1 3 1 2 2\n2 1 1\n2 4 1\n2 3 3\n" ]
[ "2\n0\n1\n" ]
You can find the definitions that are used while working with root trees by this link: http://en.wikipedia.org/wiki/Tree_(graph_theory) You can see an example of a constructed tree at *n* = 4 below.
[]
15
307,200
-1
356,274
472
Design Tutorial: Learn from a Game
[ "constructive algorithms", "implementation" ]
null
null
One way to create task is to learn from game. You should pick a game and focus on part of the mechanic of that game, then it might be a good task. Let's have a try. Puzzle and Dragon was a popular game in Japan, we focus on the puzzle part of that game, it is a tile-matching puzzle. There is an *n*<=×<=*m* board whic...
The first line contains two integers: *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=30). The next *n* lines each contains *m* integers — the description of the initial board. The *j*-th integer in the *i*-th line is *s**i*,<=*j* (1<=≤<=*s**i*,<=*j*<=≤<=900), where *s**i*,<=*j* denotes the type of the orb located in the *i*-th row ...
If there is no solution, then output: -1. If there is a solution, then in the first line output an integer *k* (1<=≤<=*k*<=≤<=106) — the number of finger moves. In the next line print two integers *x*0 and *y*0 (1<=≤<=*x*0<=≤<=*n*; 1<=≤<=*y*0<=≤<=*m*) — the position of the cell you touch at the beginning. In each of ...
[ "2 2\n1 3\n2 3\n1 3\n3 2\n", "2 2\n1 3\n2 3\n1 2\n2 3\n", "1 4\n1 2 3 4\n4 3 2 1\n", "4 1\n1\n2\n3\n4\n3\n1\n2\n4\n" ]
[ "3\n1 1\n2 2\n2 1\n1 1\n", "-1\n", "-1\n", "2\n3 1\n2 1\n1 1\n" ]
none
[]
46
0
0
356,291
391
Supercollider
[ "brute force" ]
null
null
This problem consists of two subproblems: for solving subproblem D1 you will receive 3 points, and for solving subproblem D2 you will receive 16 points. Manao is the chief architect involved in planning a new supercollider. He has to identify a plot of land where the largest possible supercollider can be built. The su...
The first line of the input will contain two single-space-separated integers *n*, the number of north-south tracts and *m*, the number of west-east tracts. Each of the *n* lines following the first describes a north-south tract. Each such tract is described by three single-space-separated integers *x**i*,<=*y**i*,<=*l...
Print one line containing a single integer, the size of the largest supercollider that can be built on one north-south tract and one west-east tract. The size of the supercollider is defined to be the length of one of the four accelerating chambers. In other words, the size of the resulting supercollider is defined to ...
[ "1 2\n4 0 9\n1 1 8\n1 2 7\n" ]
[ "2\n" ]
Consider the example. There is one vertical line segment from (4, 0) to (4, 9) and two horizontal line segments: from (1, 1) to (9, 1) and from (1, 2) to (8, 2). The largest plus shape that can be found among these segments is formed from the only vertical segment and the second of horizontal segments, and is centered ...
[]
108
0
0
358,731
737
Dirty plates
[ "constructive algorithms", "math" ]
null
null
After one of celebrations there is a stack of dirty plates in Nikita's kitchen. Nikita has to wash them and put into a dryer. In dryer, the plates should be also placed in a stack also, and the plates sizes should increase down up. The sizes of all plates are distinct. Nikita has no so much free space, specifically, h...
The first line contains three integers *n*, *a* and *b* (1<=≤<=*n*<=≤<=2000, 1<=≤<=*a*,<=*b*<=≤<=*n*). The second line contains integers *s*1,<=*s*2,<=...,<=*s**n* (1<=≤<=*s**i*<=≤<=*n*) — the sizes of the plates in down up order. All the sizes are distinct.
In the first line print "YES" if there is a solution. In this case, in the second line print integer *k* — the number of operations. Then in *k* lines print the operations, one per line. Each operation is described by two integers *t**j* and *c**j*, where *t**j*<==<=1, if the operation is to wash the top *c**j* places ...
[ "6 2 3\n2 3 6 4 1 5\n", "7 7 7\n1 2 3 4 5 6 7\n", "7 1 1\n1 2 3 4 5 6 7\n", "4 2 2\n3 2 1 4\n" ]
[ "YES\n8\n1 2\n1 1\n2 1\n1 2\n1 1\n2 1\n2 1\n2 3\n", "YES\n2\n1 7\n2 7\n", "YES\n14\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n2 1\n2 1\n2 1\n2 1\n2 1\n2 1\n2 1\n", "NO\n" ]
In the first example the initial order of plates was 2, 3, 6, 4, 1, 5. Here is how the stacks look like after each of the operations: - [1 2]: Dirty stack: 6, 4, 1, 5. Intermediary stack: 2, 3. The dryer is empty. - [1 1]: Dirty stack: 4, 1, 5. Intermediary stack: 6, 2, 3. The dryer is empty. - [2 1]: Dirty stack:...
[]
31
0
0
360,908
653
Move by Prime
[ "combinatorics", "math", "number theory" ]
null
null
Pussycat Sonya has an array consisting of *n* positive integers. There are 2*n* possible subsequences of the array. For each subsequence she counts the minimum number of operations to make all its elements equal. Each operation must be one of two: - Choose some element of the subsequence and multiply it by some prime...
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=300<=000) — the size of the array. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≤<=*t**i*<=≤<=300<=000) — elements of the array.
Print the sum of minimum number of operation for all possible subsequences of the given array modulo 109<=+<=7.
[ "3\n60 60 40\n", "4\n1 2 3 4\n" ]
[ "6\n", "24\n" ]
In the first sample, there are 8 possible subsequences: (60, 60, 40), (60, 60), (60, 40), (60, 40), (60), (60), (40) and () (empty subsequence). For a subsequence (60, 60, 40) we can make all elements equal by two operations — divide 40 by 2 to get 20, and then multiply 20 by 3 to get 60. It's impossible to achieve th...
[]
93
5,836,800
0
362,038
36
New Game with a Chess Piece
[ "games" ]
D. New Game with a Chess Piece
2
64
Petya and Vasya are inventing a new game that requires a rectangular board and one chess piece. At the beginning of the game the piece stands in the upper-left corner of the board. Two players move the piece in turns. Each turn the chess piece can be moved either one square to the right or one square down or jump *k* s...
The first input line contains two integers *t* and *k* (1<=≤<=*t*<=≤<=20, 1<=≤<=*k*<=≤<=109). Each of the following *t* lines contains two numbers *n*, *m* — the board’s length and width (1<=≤<=*n*,<=*m*<=≤<=109).
Output *t* lines that can determine the outcomes of the game on every board. Write «+» if the first player is a winner, and «-» otherwise.
[ "10 2\n1 1\n1 2\n2 1\n2 2\n1 3\n2 3\n3 1\n3 2\n3 3\n4 3\n" ]
[ "-\n+\n+\n-\n-\n+\n-\n+\n+\n+\n" ]
none
[ { "input": "10 2\n1 1\n1 2\n2 1\n2 2\n1 3\n2 3\n3 1\n3 2\n3 3\n4 3", "output": "-\n+\n+\n-\n-\n+\n-\n+\n+\n+" }, { "input": "20 2\n5 9\n6 7\n6 5\n9 5\n1 7\n7 5\n6 5\n2 10\n9 10\n5 5\n5 7\n3 3\n2 7\n6 1\n9 5\n1 1\n2 1\n5 8\n6 3\n2 9", "output": "+\n+\n-\n+\n-\n+\n-\n-\n+\n+\n+\n+\n+\n+\n+\n-\n+\n...
0
0
-1
362,994
264
Colorful Stones
[ "dp", "two pointers" ]
null
null
There are two sequences of colorful stones. The color of each stone is one of red, green, or blue. You are given two strings *s* and *t*. The *i*-th (1-based) character of *s* represents the color of the *i*-th stone of the first sequence. Similarly, the *i*-th (1-based) character of *t* represents the color of the *i*...
The input contains two lines. The first line contains the string *s* (1<=≤<=|*s*|<=≤<=106). The second line contains the string *t* (1<=≤<=|*t*|<=≤<=106). The characters of each string will be one of "R", "G", or "B".
Print the number of distinct reachable states in a single line. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "RBR\nRGG\n", "RGBB\nBRRBRR\n", "RRRRRRRRRR\nRRRRRRRR\n" ]
[ "5\n", "19\n", "8\n" ]
In the first example, there are five reachable states: (1, 1), (2, 2), (2, 3), (3, 2), and (3, 3). For example, the state (3, 3) is reachable because if you perform instructions "RED", "GREEN", and "BLUE" in this order from the initial state, the state will be (3, 3). The following picture shows how the instructions wo...
[]
60
0
0
363,066
338
Optimize!
[ "data structures" ]
null
null
Manao is solving a problem with the following statement: He came up with a solution that produces the correct answers but is too slow. You are given the pseudocode of his solution, where the function getAnswer calculates the answer to the problem: Your task is to help Manao optimize his algorithm.
The first line contains space-separated integers *n*, *len* and *h* (1<=≤<=*len*<=≤<=*n*<=≤<=150000; 1<=≤<=*h*<=≤<=109). The second line contains *len* space-separated integers *b*1,<=*b*2,<=...,<=*b**len* (1<=≤<=*b**i*<=≤<=109). The third line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a*...
Print a single number — the answer to Manao's problem.
[ "5 2 10\n5 3\n1 8 5 5 7\n" ]
[ "2\n" ]
none
[ { "input": "5 2 10\n5 3\n1 8 5 5 7", "output": "2" } ]
46
0
0
366,156
50
Square Equation Roots
[ "math" ]
E. Square Equation Roots
5
256
A schoolboy Petya studies square equations. The equations that are included in the school curriculum, usually look simple: Petya noticed that some equations have two real roots, some of them have only one root and some equations don't have real roots at all. Moreover it turned out that several different square equati...
The single line contains two integers *n* and *m*. (1<=≤<=*n*,<=*m*<=≤<=5000000).
Print a single number which is the number of real roots of the described set of equations.
[ "3 3\n", "1 2\n" ]
[ "12\n", "1\n" ]
In the second test from the statement the following equations are analysed: *b* = 1, *c* = 1: *x*<sup class="upper-index">2</sup> + 2*x* + 1 = 0; The root is *x* =  - 1 *b* = 1, *c* = 2: *x*<sup class="upper-index">2</sup> + 2*x* + 2 = 0; No roots Overall there's one root In the second test the following equatio...
[]
5,000
0
0
366,210
869
The Overdosing Ubiquity
[ "brute force", "dfs and similar", "graphs" ]
null
null
The fundamental prerequisite for justice is not to be correct, but to be strong. That's why justice is always the victor. The Cinderswarm Bee. Koyomi knows it. The bees, according to their nature, live in a tree. To be more specific, a complete binary tree with *n* nodes numbered from 1 to *n*. The node numbered 1 is...
The first line of input contains two space-separated integers *n* and *m* (1<=≤<=*n*<=≤<=109, 0<=≤<=*m*<=≤<=4) — the number of nodes in the tree and the number of extra edges respectively. The following *m* lines each contains two space-separated integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*n*, *u*<=≠<=*v*) — describing...
Output one integer — the number of simple paths in the resulting graph, modulo 109<=+<=7.
[ "3 0\n", "3 1\n2 3\n", "2 4\n1 2\n2 1\n1 2\n2 1\n" ]
[ "9\n", "15\n", "12\n" ]
In the first example, the paths are: (1); (2); (3); (1, 2); (2, 1); (1, 3); (3, 1); (2, 1, 3); (3, 1, 2). (For the sake of clarity, the edges between nodes are omitted since there are no multiple edges in this case.) In the second example, the paths are: (1); (1, 2); (1, 2, 3); (1, 3); (1, 3, 2); and similarly for pat...
[]
46
0
0
370,010
282
Sausage Maximization
[ "bitmasks", "data structures", "trees" ]
null
null
The Bitlandians are quite weird people. They have their own problems and their own solutions. They have their own thoughts and their own beliefs, they have their own values and their own merits. They have their own dishes and their own sausages! In Bitland a sausage is an array of integers! A sausage's deliciousness i...
The first line contains an integer *n* (1<=≤<=*n*<=≤<=105). The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=1012) — Mr. Bitkoch's sausage. 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 maximum pleasure BitHaval and BitAryo can get from the dinner.
[ "2\n1 2\n", "3\n1 2 3\n", "2\n1000 1000\n" ]
[ "3\n", "3\n", "1000\n" ]
none
[]
62
0
0
371,458
526
Transmitting Levels
[ "dp", "implementation" ]
null
null
Optimizing the amount of data transmitted via a network is an important and interesting part of developing any network application. In one secret game developed deep in the ZeptoLab company, the game universe consists of *n* levels, located in a circle. You can get from level *i* to levels *i*<=-<=1 and *i*<=+<=1, als...
The first line contains two integers *n*, *q* (2<=≤<=*n*<=≤<=106, 1<=≤<=*q*<=≤<=50) — the number of levels in the game universe and the number of distinct values of *b* that you need to process. The second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=109) — the sizes of the levels in bytes. The next *q* lines ...
For each value of *k**j* from the input print on a single line integer *m**j* (1<=≤<=*m**j*<=≤<=*n*), determining the minimum number of groups to divide game levels into for transmission via network observing the given conditions.
[ "6 3\n2 4 2 1 3 2\n7\n4\n6\n" ]
[ "2\n4\n3\n" ]
In the test from the statement you can do in the following manner. - at *b* = 7 you can divide into two segments: 2|421|32 (note that one of the segments contains the fifth, sixth and first levels); - at *b* = 4 you can divide into four segments: 2|4|21|3|2; - at *b* = 6 you can divide into three segments: 24|21|32...
[]
77
2,048,000
0
372,083
713
Sonya Partymaker
[ "binary search", "dp" ]
null
null
Owl Sonya decided to become a partymaker. To train for this role she gather all her owl friends in the country house. There are *m* chairs located in a circle and consequently numbered with integers from 1 to *m*. Thus, chairs *i* and *i*<=+<=1 are neighbouring for all *i* from 1 to *m*<=-<=1. Chairs 1 and *m* are also...
The first line of the input contains a single integer *m* (1<=≤<=*m*<=≤<=109) — the length of the circle. The second line contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of friends. Last line contains an increasing sequence of *n* integers *a**i* (1<=≤<=*a**i*<=≤<=*m*) — initial positions of all o...
Print the minimum number of move required to finish the game. Note, that 0 also may be an answer.
[ "6\n3\n1 3 5\n", "6\n2\n1 6\n", "406\n6\n1 2 3 204 205 206\n" ]
[ "1\n", "2\n", "100\n" ]
In the first sample, it's possible if all owls will move clockwise, i.e. in the direction of increasing indices. In the sample, first owl has to move clockwise, while the second — counterclockwise. In the third sample, the first and the fourth owls should move counterclockwise, while the third and the sixth — clockwi...
[]
155
12,800,000
0
372,122
498
Stairs and Lines
[ "dp", "matrices" ]
null
null
You are given a figure on a grid representing stairs consisting of 7 steps. The width of the stair on height *i* is *w**i* squares. Formally, the figure is created by consecutively joining rectangles of size *w**i*<=×<=*i* so that the *w**i* sides lie on one straight line. Thus, for example, if all *w**i*<==<=1, the fi...
The single line of the input contains 7 numbers *w*1,<=*w*2,<=...,<=*w*7 (0<=≤<=*w**i*<=≤<=105). It is guaranteed that at least one of the *w**i*'s isn't equal to zero.
In the single line of the output display a single number — the answer to the problem modulo 109<=+<=7.
[ "0 1 0 0 0 0 0\n", "0 2 0 0 0 0 0\n", "1 1 1 0 0 0 0\n", "5 1 0 3 0 0 1\n" ]
[ "1\n", "7\n", "9\n", "411199181\n" ]
All the possible ways of painting the third sample are given below:
[]
61
0
0
373,520
44
BerPaint
[ "geometry", "graphs" ]
F. BerPaint
5
256
Anfisa the monkey got disappointed in word processors as they aren't good enough at reflecting all the range of her emotions, that's why she decided to switch to graphics editors. Having opened the BerPaint, she saw a white rectangle *W*<=×<=*H* in size which can be painted on. First Anfisa learnt to navigate the drawi...
The first input line has two integers *W* and *H* (3<=≤<=*W*,<=*H*<=≤<=104) — the sizes of the initially white rectangular painting area. The second line contains integer *n* — the number of black segments (0<=≤<=*n*<=≤<=100). On the next *n* lines are described the segments themselves, each of which is given by coordi...
For every color present in the final picture print on the single line the name of the color and the total area of areas painted that color with an accuracy of 10<=-<=6. Print the colors in any order.
[ "4 5\n6\n1 1 1 3\n1 3 3 3\n3 3 3 1\n3 1 1 1\n1 3 3 1\n1 1 3 3\n2\n2 1 red\n2 2 blue\n", "5 5\n5\n1 1 2 2\n2 2 4 2\n4 2 4 4\n4 4 2 4\n2 4 2 2\n2\n3 3 black\n3 3 green\n", "7 4\n9\n1 2 2 3\n2 3 3 2\n3 2 2 1\n2 1 1 2\n3 2 4 2\n4 2 5 3\n5 3 6 2\n6 2 5 1\n5 1 4 2\n2\n2 2 black\n2 2 red\n" ]
[ "blue 0.00000000\nwhite 20.00000000\n", "green 4.00000000\nwhite 21.00000000\n", "red 2.00000000\nwhite 26.00000000\n" ]
Initially the black segments painted by Anfisa can also be painted a color if any of the chosen points lays on the segment. The segments have areas equal to 0. That is why if in the final picture only parts of segments is painted some color, then the area, painted the color is equal to 0.
[]
60
0
0
374,204
82
Corridor
[ "geometry" ]
E. Corridor
2
256
Consider a house plan. Let the house be represented by an infinite horizontal strip defined by the inequality <=-<=*h*<=≤<=*y*<=≤<=*h*. Strictly outside the house there are two light sources at the points (0,<=*f*) and (0,<=<=-<=*f*). Windows are located in the walls, the windows are represented by segments on the li...
The first line of the input file contains three integers *n*, *h* and *f* (1<=≤<=*n*<=≤<=500, 1<=≤<=*h*<=≤<=10, *h*<=&lt;<=*f*<=≤<=1000). Next, *n* lines contain two integers each *l**i*, *r**i* (<=-<=5000<=≤<=*l**i*<=&lt;<=*r**i*<=≤<=5000), each entry indicates two segments. Endpoints of the first segment are (*l**i*,...
Print the single real number — the area of the illuminated part of the floor with an absolute or relative error of no more than 10<=-<=4.
[ "1 1 2\n-1 1\n", "2 2 4\n-1 0\n1 2\n" ]
[ "10.0000000000\n", "23.3333333333\n" ]
The second sample test is shown on the figure. Green area is the desired area of the illuminated part of the floor. Violet segments indicate windows.
[]
46
0
0
376,037
103
Time to Raid Cowavans
[ "brute force", "data structures", "sortings" ]
D. Time to Raid Cowavans
4
70
As you know, the most intelligent beings on the Earth are, of course, cows. This conclusion was reached long ago by the Martian aliens, as well as a number of other intelligent civilizations from outer space. Sometimes cows gather into cowavans. This seems to be seasonal. But at this time the cows become passive and ...
The first line contains the only positive integer *n* (1<=≤<=*n*<=≤<=3·105) — the number of cows in the cowavan. The second number contains *n* positive integer *w**i*, separated by spaces, where the *i*-th number describes the mass of the *i*-th cow in the cowavan (1<=≤<=*w**i*<=≤<=109). The third line contains the ...
Print for each scenario of the (*a*,<=*b*)-raid the total mass of cows, that can be stolen using only this scenario. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams of the %I64d specificator.
[ "3\n1 2 3\n2\n1 1\n1 2\n", "4\n2 3 5 7\n3\n1 3\n2 3\n2 2\n" ]
[ "6\n4\n", "9\n3\n10\n" ]
none
[ { "input": "3\n1 2 3\n2\n1 1\n1 2", "output": "6\n4" }, { "input": "4\n2 3 5 7\n3\n1 3\n2 3\n2 2", "output": "9\n3\n10" }, { "input": "5\n3 2 4 5 6\n8\n4 2\n3 1\n3 5\n3 4\n3 5\n5 5\n4 4\n5 3", "output": "5\n15\n4\n4\n4\n6\n5\n6" }, { "input": "10\n10 10 7 10 2 8 9 6 4 9\n10\n...
186
409,600
0
376,058
107
Arrangement
[ "bitmasks", "dp" ]
C. Arrangement
2
256
In the year 2500 the annual graduation ceremony in the German University in Cairo (GUC) has run smoothly for almost 500 years so far. The most important part of the ceremony is related to the arrangement of the professors in the ceremonial hall. Traditionally GUC has *n* professors. Each professor has his seniority l...
The first line contains three integers *n*, *y* and *m* (1<=≤<=*n*<=≤<=16,<=2001<=≤<=*y*<=≤<=1018,<=0<=≤<=*m*<=≤<=100) — the number of professors, the year for which the arrangement should be computed, and the number of pairs of seats for which the seniority relation should be kept, respectively. The next *m* lines co...
Print the order in which the professors should be seated in the requested year. If by this year the GUC would have ran out of arrangements, or the given "senior-junior" relation are contradictory, print "The times have changed" (without quotes).
[ "3 2001 2\n1 2\n2 3\n", "7 2020 6\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n", "10 3630801 0\n", "3 2001 3\n1 2\n2 3\n3 1\n" ]
[ "1 2 3\n", "1 2 3 7 4 6 5\n", "The times have changed\n", "The times have changed\n" ]
In the first example the lexicographically first order of seating is 1 2 3. In the third example the GUC will run out of arrangements after the year 3630800. In the fourth example there are no valid arrangements for the seating. The lexicographical comparison of arrangements is performed by the &lt; operator in mode...
[]
92
0
0
376,823
954
Path Counting
[ "combinatorics", "dp" ]
null
null
You are given a rooted tree. Let's denote *d*(*x*) as depth of node *x*: depth of the root is 1, depth of any other node *x* is *d*(*y*)<=+<=1, where *y* is a parent of *x*. The tree has the following property: every node *x* with *d*(*x*)<==<=*i* has exactly *a**i* children. Maximum possible depth of a node is *n*, a...
The first line of input contains an integer *n* (2<=<=≤<=<=*n*<=<=≤<=<=5<=000) — the maximum depth of a node. The second line of input contains *n*<=-<=1 integers *a*1,<=<=*a*2,<=<=...,<=<=*a**n*<=-<=1 (2<=≤<=<=*a**i*<=<=≤<=109), where *a**i* is the number of children of every node *x* such that *d*(*x*)<==<=*i*. Sinc...
Print 2*n*<=-<=2 numbers. The *k*-th of these numbers must be equal to *f**k* modulo 109<=+<=7.
[ "4\n2 2 2\n", "3\n2 3\n" ]
[ "14 19 20 20 16 16 ", "8 13 6 9 " ]
This the tree from the first sample:
[ { "input": "4\n2 2 2", "output": "14 19 20 20 16 16 " }, { "input": "3\n2 3", "output": "8 13 6 9 " }, { "input": "2\n2", "output": "2 1 " }, { "input": "6\n2 2 4 2 4", "output": "182 415 420 980 1216 2432 1536 3328 2048 4096 " } ]
0
0
-1
380,809
720
Homework
[ "constructive algorithms" ]
null
null
Today Peter has got an additional homework for tomorrow. The teacher has given three integers to him: *n*, *m* and *k*, and asked him to mark one or more squares on a square grid of size *n*<=×<=*m*. The marked squares must form a connected figure, and there must be exactly *k* triples of marked squares that form an ...
Input data contains one or more test cases. The first line contains the number of test cases *t* (1<=≤<=*t*<=≤<=100). Each of the following *t* test cases is described by a line that contains three integers: *n*, *m* and *k* (3<=≤<=*n*,<=*m*, *n*<=×<=*m*<=≤<=105, 0<=≤<=*k*<=≤<=109). The sum of values of *n*<=×<=*m* f...
For each test case print the answer. If it is possible to create such figure, print *n* lines, *m* characters each, use asterisk '*' to denote the marked square, and dot '.' to denote the unmarked one. If there is no solution, print -1. Print empty line between test cases.
[ "3\n3 3 4\n3 3 5\n3 3 3\n" ]
[ ".*.\n***\n.*.\n\n**.\n**.\n*..\n\n.*.\n***\n*..\n" ]
none
[]
31
0
0
380,991
387
George and Interesting Graph
[ "graph matchings" ]
null
null
George loves graphs. Most of all, he loves interesting graphs. We will assume that a directed graph is interesting, if it meets the following criteria: - The graph doesn't contain any multiple arcs; - There is vertex *v* (we'll call her the center), such that for any vertex of graph *u*, the graph contains arcs (*u...
The first line contains two space-separated integers *n* and *m* (2<=≤<=*n*<=≤<=500,<=1<=≤<=*m*<=≤<=1000) — the number of vertices and arcs in the presented graph. Each of the next *m* lines contains two space-separated integers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*) — the descriptions of the graph's arcs. Pa...
Print a single integer — the answer to George's question.
[ "3 7\n1 1\n2 2\n3 1\n1 3\n3 2\n2 3\n3 3\n", "3 6\n1 1\n2 2\n3 1\n3 2\n2 3\n3 3\n", "3 1\n2 2\n" ]
[ "0\n", "1\n", "6\n" ]
For more information about directed graphs, please visit: http://en.wikipedia.org/wiki/Directed_graph In the first sample the graph already is interesting, its center is vertex 3.
[ { "input": "3 7\n1 1\n2 2\n3 1\n1 3\n3 2\n2 3\n3 3", "output": "0" }, { "input": "3 6\n1 1\n2 2\n3 1\n3 2\n2 3\n3 3", "output": "1" }, { "input": "3 1\n2 2", "output": "6" }, { "input": "5 25\n4 2\n1 4\n5 2\n2 2\n3 1\n4 5\n2 1\n2 5\n2 4\n1 2\n4 1\n4 4\n1 1\n1 5\n4 3\n5 1\n3 3...
342
9,932,800
3
381,378
54
Vacuum Сleaner
[ "geometry" ]
E. Vacuum Сleaner
1
256
One winter evening the Hedgehog was relaxing at home in his cozy armchair and clicking through the TV channels. Stumbled on an issue of «TopShop», the Hedgehog was about to change the channel when all of a sudden he was stopped by an advertisement of a new wondrous invention. Actually, a vacuum cleaner was advertised ...
The first line contains an integer *N* which represents the number of vertices of the vacuum cleaner's polygon (3<=≤<=*N*<=≤<=4·104). Then follow *N* lines each containing two numbers — the coordinates of a vertex of the polygon. All the coordinates are integer and their absolute values do not exceed 106. It is guaran...
Print the minimum possible uncovered area. The answer will be accepted if it is within 10<=-<=6 of absolute or relative error from the correct answer.
[ "4\n0 0\n1 0\n1 1\n0 1\n", "8\n1 2\n2 1\n2 -1\n1 -2\n-1 -2\n-2 -1\n-2 1\n-1 2\n" ]
[ "0.00000000000000000000", "0.50000000000000000000" ]
none
[]
46
0
0
385,626
0
none
[ "none" ]
null
null
At a geometry lesson Gerald was given a task: to get vector *B* out of vector *A*. Besides, the teacher permitted him to perform the following operations with vector *А*: - Turn the vector by 90 degrees clockwise.- Add to the vector a certain vector *C*. Operations could be performed in any order any number of times....
The first line contains integers *x*1 и *y*1 — the coordinates of the vector *A* (<=-<=108<=≤<=*x*1,<=*y*1<=≤<=108). The second and the third line contain in the similar manner vectors *B* and *C* (their coordinates are integers; their absolute value does not exceed 108).
Print "YES" (without the quotes) if it is possible to get vector *B* using the given operations. Otherwise print "NO" (without the quotes).
[ "0 0\n1 1\n0 1\n", "0 0\n1 1\n1 1\n", "0 0\n1 1\n2 2\n" ]
[ "YES\n", "YES\n", "NO\n" ]
none
[]
62
0
0
385,764
576
Painting Edges
[ "binary search", "data structures" ]
null
null
Note the unusual memory limit for this problem. You are given an undirected graph consisting of *n* vertices and *m* edges. The vertices are numbered with integers from 1 to *n*, the edges are numbered with integers from 1 to *m*. Each edge can be unpainted or be painted in one of the *k* colors, which are numbered wi...
The first line contains integers *n*, *m*, *k*, *q* (2<=≤<=*n*<=≤<=5·105, 1<=≤<=*m*,<=*q*<=≤<=5·105, 1<=≤<=*k*<=≤<=50) — the number of vertices, the number of edges, the number of colors and the number of queries. Then follow *m* edges of the graph in the form *a**i*, *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*). Then fo...
For each query print "YES" (without the quotes), if it is valid, or "NO" (without the quotes), if this query destroys the bipartivity of the graph formed by the edges of some color.
[ "3 3 2 5\n1 2\n2 3\n1 3\n1 1\n2 1\n3 2\n3 1\n2 2\n" ]
[ "YES\nYES\nYES\nNO\nYES\n" ]
none
[]
46
0
0
386,939
773
Test Data Generation
[ "combinatorics", "divide and conquer", "dp", "fft", "math", "number theory" ]
null
null
Test data generation is not an easy task! Often, generating big random test cases is not enough to ensure thorough testing of solutions for correctness. For example, consider a problem from an old Codeforces round. Its input format looks roughly as follows: The first line contains a single integer *n* (1<=≤<=*n*<=≤<=...
The only line contains three integers *max**n*, *max**a* and *q* (1<=≤<=*max**n*<=≤<=30<=000; *max**n*<=≤<=*max**a*<=≤<=109; 104<=≤<=*q*<=≤<=105<=+<=129).
Output a single integer — the number of test cases which satisfy the constraints and make both wrong solutions output an incorrect answer, modulo *q*.
[ "3 6 100000\n", "6 21 100129\n", "58 787788 50216\n" ]
[ "4\n", "154\n", "46009\n" ]
In the first example, interesting test cases look as follows:
[]
46
0
0
388,940
113
Museum
[ "math", "matrices", "probabilities" ]
D. Museum
2
256
One day as Petya and his friend Vasya were having one of their numerous trips, they decided to visit a museum castle. The museum has a specific shape: it consists of *n* rooms connected with *m* corridors so that one can access any room from any other one. After the two friends had a little walk around the museum, the...
The first line contains four integers: *n* (1<=≤<=*n*<=≤<=22), representing the numbers of rooms; *m* , representing the number of corridors; *a*,<=*b* (1<=≤<=*a*,<=*b*<=≤<=*n*), representing the numbers of Petya's and Vasya's starting rooms correspondingly. Next *m* lines contain pairs of numbers — the numbers of roo...
In the only line print *n* space-separated numbers, the *i*-th number should represent the probability that the friends meet in the *i*-th room with absolute or relative error of no more than 10<=-<=6.
[ "2 1 1 2\n1 2\n0.5\n0.5\n", "4 4 1 2\n1 2\n2 3\n3 4\n4 1\n0.5\n0.5\n0.5\n0.5\n" ]
[ "0.5000000000 0.5000000000 ", "0.3333333333 0.3333333333 0.1666666667 0.1666666667 " ]
In the first sample the museum is symmetric. That means the probabilities to meet in rooms 1 and 2 are equal. And their sum equals to one. So, each probability equals 0.5.
[]
60
0
0
389,968
630
Turn
[ "geometry", "math" ]
null
null
Vasya started working in a machine vision company of IT City. Vasya's team creates software and hardware for identification of people by their face. One of the project's know-how is a camera rotating around its optical axis on shooting. People see an eye-catching gadget — a rotating camera — come up to it to see it be...
The only line of the input contains one integer *x* (<=-<=1018<=≤<=*x*<=≤<=1018) — camera angle in degrees. Positive value denotes clockwise camera rotation, negative — counter-clockwise.
Output one integer — the minimum required number of 90 degrees clockwise turns.
[ "60\n", "-60\n" ]
[ "1\n", "3\n" ]
When the camera is rotated 60 degrees counter-clockwise (the second example), an image from it is rotated 60 degrees clockwise. One 90 degrees clockwise turn of the image result in 150 degrees clockwise total rotation and deviation from "true up" for one turn is 150 degrees. Two 90 degrees clockwise turns of the image ...
[ { "input": "60", "output": "1" }, { "input": "-60", "output": "3" }, { "input": "0", "output": "0" }, { "input": "44", "output": "0" }, { "input": "45", "output": "0" }, { "input": "46", "output": "1" }, { "input": "134", "output": "1" ...
46
0
0
392,541
623
Transforming Sequence
[ "combinatorics", "dp", "fft", "math" ]
null
null
Let's define the transformation *P* of a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* as *b*1,<=*b*2,<=...,<=*b**n*, where *b**i*<==<=*a*1 | *a*2 | ... | *a**i* for all *i*<==<=1,<=2,<=...,<=*n*, where | is the bitwise OR operation. Vasya consequently applies the transformation *P* to all sequences of length *n* co...
The only line of the input contains two integers *n* and *k* (1<=≤<=*n*<=≤<=1018,<=1<=≤<=*k*<=≤<=30<=000).
Print a single integer — the answer to the problem modulo 109<=+<=7.
[ "1 2\n", "2 3\n", "3 3\n" ]
[ "3\n", "30\n", "48\n" ]
none
[]
46
0
0
393,368
768
Barrels and boxes
[ "brute force", "combinatorics", "math", "number theory", "probabilities" ]
null
null
Tarly has two different type of items, food boxes and wine barrels. There are *f* food boxes and *w* wine barrels. Tarly stores them in various stacks and each stack can consist of either food boxes or wine barrels but not both. The stacks are placed in a line such that no two stacks of food boxes are together and no t...
The first line of input contains three integers *f*, *w*, *h* (0<=≤<=*f*,<=*w*,<=*h*<=≤<=105) — number of food boxes, number of wine barrels and *h* is as described above. It is guaranteed that he has at least one food box or at least one wine barrel.
Output the probability that Jon Snow will like the arrangement. The probability is of the form , then you need to output a single integer *p*·*q*<=-<=1 *mod* (109<=+<=7).
[ "1 1 1\n", "1 2 1\n" ]
[ "0\n", "666666672\n" ]
In the first example *f*  =  1, *w* = 1 and *h* = 1, there are only two possible arrangement of stacks and Jon Snow doesn't like any of them. In the second example *f* = 1, *w* = 2 and *h* = 1, there are three arrangements. Jon Snow likes the (1) and (3) arrangement. So the probabilty is <img align="middle" class="tex...
[ { "input": "1 1 1", "output": "0" }, { "input": "1 2 1", "output": "666666672" }, { "input": "6 5 7", "output": "0" }, { "input": "12 12 24", "output": "0" }, { "input": "20 12 32", "output": "0" }, { "input": "21 32 10", "output": "683999264" },...
30
0
0
393,984
360
Levko and Game
[ "graphs", "greedy", "shortest paths" ]
null
null
Levko loves sports pathfinding competitions in his city very much. In order to boost his performance, Levko spends his spare time practicing. The practice is a game. The city consists of *n* intersections connected by *m*<=+<=*k* directed roads. Two or more roads can connect the same pair of intersections. Besides, th...
The first line contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=104, 1<=≤<=*k*<=≤<=100). The second line contains three integers *s*1, *s*2 and *f* (1<=≤<=*s*1,<=*s*2,<=*f*<=≤<=*n*). The next *m* lines contains the descriptions of the roads that cannot be changed by Levko. Each line contains three integer...
In the first line print string "WIN" (without the quotes) if Levko can win this game, string "DRAW" (without the quotes) if Levko can end the game with a draw and "LOSE" (without the quotes) if he loses for sure. If the answer is "WIN" or "DRAW", then print on the second line *k* space-separated integers — the length ...
[ "4 1 3\n1 3 4\n3 2 2\n1 2 1 3\n2 4 1 3\n3 4 1 3\n", "4 1 3\n1 3 4\n3 2 2\n1 2 1 3\n2 4 1 3\n3 4 1 2\n", "5 4 2\n1 2 5\n1 3 3\n1 4 4\n2 3 2\n2 4 3\n3 5 1 5\n4 5 4 7\n" ]
[ "WIN\n1 1 3 ", "DRAW\n1 1 2 ", "LOSE\n" ]
none
[]
30
0
0
394,974
605
Intergalaxy Trips
[ "probabilities", "shortest paths" ]
null
null
The scientists have recently discovered wormholes — objects in space that allow to travel very long distances between galaxies and star systems. The scientists know that there are *n* galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number *n*. To get from galaxy *i* to galaxy *...
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=1000) — the number of galaxies within reach. Then follows a matrix of *n* rows and *n* columns. Each element *p**ij* represents the probability that there is a wormhole from galaxy *i* to galaxy *j*. All the probabilities are given in percents an...
Print a single real value — the expected value of the time needed to travel from galaxy 1 to galaxy *n* if one acts in an optimal way. 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...
[ "3\n100 50 50\n0 100 80\n0 0 100\n", "2\n100 30\n40 100\n" ]
[ "1.750000000000000\n", "3.333333333333333\n" ]
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/af1e8c68b44c3e2e8dc07919e5f90699f09c9dc1.png" style="max-widt...
[]
46
0
0
397,097