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
406
Hill Climbing
[ "dfs and similar", "geometry", "trees" ]
null
null
This problem has nothing to do with Little Chris. It is about hill climbers instead (and Chris definitely isn't one). There are *n* hills arranged on a line, each in the form of a vertical line segment with one endpoint on the ground. The hills are numbered with numbers from 1 to *n* from left to right. The *i*-th hil...
The first line of input contains a single integer *n* (1<=≤<=*n*<=≤<=105), the number of hills. The next *n* lines describe the hills. The *i*-th of them contains two space-separated integers *x**i*, *y**i* (1<=≤<=*x**i*<=≤<=107; 1<=≤<=*y**i*<=≤<=1011), the position and the height of the *i*-th hill. The hills are give...
In a single line output *m* space-separated integers, where the *i*-th integer is the number of the meeting hill for the members of the *i*-th team.
[ "6\n1 4\n2 1\n3 2\n4 3\n6 4\n7 4\n3\n3 1\n5 6\n2 3\n" ]
[ "5 6 3 \n" ]
none
[]
78
7,065,600
0
31,779
0
none
[ "none" ]
null
null
You are given a connected weighted graph with *n* vertices and *m* edges. The graph doesn't contain loops nor multiple edges. Consider some edge with id *i*. Let's determine for this edge the maximum integer weight we can give to it so that it is contained in all minimum spanning trees of the graph if we don't change t...
The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=2·105, *n*<=-<=1<=≤<=*m*<=≤<=2·105), where *n* and *m* are the number of vertices and the number of edges in the graph, respectively. Each of the next *m* lines contains three integers *u*, *v* and *c* (1<=≤<=*v*,<=*u*<=≤<=*n*, *v*<=≠<=*u*, 1<=≤<=*c*<=≤<=...
Print the answer for each edge in the order the edges are given in the input. If an edge is contained in every minimum spanning tree with any weight, print -1 as the answer.
[ "4 4\n1 2 2\n2 3 2\n3 4 2\n4 1 3\n", "4 3\n1 2 2\n2 3 2\n3 4 2\n" ]
[ "2 2 2 1 ", "-1 -1 -1 " ]
none
[]
46
4,812,800
-1
31,808
280
Game on Tree
[ "implementation", "math", "probabilities", "trees" ]
null
null
Momiji has got a rooted tree, consisting of *n* nodes. The tree nodes are numbered by integers from 1 to *n*. The root has number 1. Momiji decided to play a game on this tree. The game consists of several steps. On each step, Momiji chooses one of the remaining tree nodes (let's denote it by *v*) and removes all the ...
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of nodes in the tree. The next *n*<=-<=1 lines contain the tree edges. The *i*-th line contains integers *a**i*, *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*) — the numbers of the nodes that are connected by the *i*-th edge. It is guarant...
Print a single real number — the expectation of the number of steps in the described game. The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=6.
[ "2\n1 2\n", "3\n1 2\n1 3\n" ]
[ "1.50000000000000000000\n", "2.00000000000000000000\n" ]
In the first sample, there are two cases. One is directly remove the root and another is remove the root after one step. Thus the expected steps are: In the second sample, things get more complex. There are two cases that reduce to the first sample, and one case cleaned at once. Thus the expected steps are:
[ { "input": "2\n1 2", "output": "1.50000000000000000000" }, { "input": "3\n1 2\n1 3", "output": "2.00000000000000000000" }, { "input": "10\n1 2\n2 3\n3 4\n1 5\n2 6\n6 7\n4 8\n6 9\n9 10", "output": "3.81666666666666690000" }, { "input": "6\n1 3\n2 4\n5 6\n3 6\n5 4", "output...
716
19,558,400
3
31,854
81
Sequence Formatting
[ "implementation", "strings" ]
B. Sequence Formatting
2
256
Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does not look neat, he rushes to correct it. For example, number sequence written like "1,2 ,3,...,   10" will be corrected ...
The input data contains a single string *s*. Its length is from 1 to 255 characters. The string *s* does not begin and end with a space. Its content matches the description given above.
Print the string *s* after it is processed. Your program's output should be exactly the same as the expected answer. It is permissible to end output line with a line-break character, and without it.
[ "1,2 ,3,..., 10\n", "1,,,4...5......6\n", "...,1,2,3,...\n" ]
[ "1, 2, 3, ..., 10\n", "1, , , 4 ...5 ... ...6\n", "..., 1, 2, 3, ...\n" ]
none
[ { "input": "1,2 ,3,..., 10", "output": "1, 2, 3, ..., 10" }, { "input": "1,,,4...5......6", "output": "1, , , 4 ...5 ... ...6" }, { "input": ",,,,,,,,,,,,,", "output": ", , , , , , , , , , , , ," }, { "input": "123456789", "output": "123456789" }, { "input": "...
216
307,200
0
31,881
61
Capture Valerian
[ "math" ]
C. Capture Valerian
2
256
It's now 260 AD. Shapur, being extremely smart, became the King of Persia. He is now called Shapur, His majesty King of kings of Iran and Aniran. Recently the Romans declared war on Persia. They dreamed to occupy Armenia. In the recent war, the Romans were badly defeated. Now their senior army general, Philip is captu...
The first line contains two integers *a* and *b* (2<=≤<=*a*,<=*b*<=≤<=25). Only *b* may be replaced by an R which indicates Roman numbering system. The next line contains a single non-negative integer *c* in base *a* which may contain leading zeros but its length doesn't exceed 103. It is guaranteed that if we have ...
Write a single line that contains integer *c* in base *b*. You must omit leading zeros.
[ "10 2\n1\n", "16 R\n5\n", "5 R\n4\n", "2 2\n1111001\n", "12 13\nA\n" ]
[ "1\n", "V\n", "IV\n", "1111001\n", "A\n" ]
You can find more information about roman numerals here: http://en.wikipedia.org/wiki/Roman_numerals
[ { "input": "10 2\n1", "output": "1" }, { "input": "16 R\n5", "output": "V" }, { "input": "5 R\n4", "output": "IV" }, { "input": "2 2\n1111001", "output": "1111001" }, { "input": "12 13\nA", "output": "A" }, { "input": "6 7\n12345", "output": "5303"...
62
2,867,200
-1
31,913
552
Vanya and Triangles
[ "brute force", "combinatorics", "data structures", "geometry", "math", "sortings" ]
null
null
Vanya got bored and he painted *n* distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.
The first line contains integer *n* (1<=≤<=*n*<=≤<=2000) — the number of the points painted on the plane. Next *n* lines contain two integers each *x**i*,<=*y**i* (<=-<=100<=≤<=*x**i*,<=*y**i*<=≤<=100) — the coordinates of the *i*-th point. It is guaranteed that no two given points coincide.
In the first line print an integer — the number of triangles with the non-zero area among the painted points.
[ "4\n0 0\n1 1\n2 0\n2 2\n", "3\n0 0\n1 1\n2 0\n", "1\n1 1\n" ]
[ "3\n", "1\n", "0\n" ]
Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0). Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0). Note to the third sample test. A single point doesn't form a single triangle.
[ { "input": "4\n0 0\n1 1\n2 0\n2 2", "output": "3" }, { "input": "3\n0 0\n1 1\n2 0", "output": "1" }, { "input": "1\n1 1", "output": "0" }, { "input": "5\n0 0\n1 1\n2 2\n3 3\n4 4", "output": "0" }, { "input": "5\n0 0\n1 1\n2 3\n3 6\n4 10", "output": "10" }, ...
4,000
0
0
32,015
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
32,229
509
Restoring Numbers
[ "constructive algorithms", "math" ]
null
null
Vasya had two arrays consisting of non-negative integers: *a* of size *n* and *b* of size *m*. Vasya chose a positive integer *k* and created an *n*<=×<=*m* matrix *v* using the following formula: Vasya wrote down matrix *v* on a piece of paper and put it in the table. A year later Vasya was cleaning his table when h...
The first line contains integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100), separated by a space — the number of rows and columns in the found matrix, respectively. The *i*-th of the following lines contains numbers *w**i*,<=1,<=*w**i*,<=2,<=...,<=*w**i*,<=*m* (0<=≤<=*w**i*,<=*j*<=≤<=109), separated by spaces — the eleme...
If the matrix *w* could not have been obtained in the manner described above, print "NO" (without quotes) in the single line of output. Otherwise, print four lines. In the first line print "YES" (without quotes). In the second line print an integer *k* (1<=≤<=*k*<=≤<=1018). Note that each element of table *w* should...
[ "2 3\n1 2 3\n2 3 4\n", "2 2\n1 2\n2 0\n", "2 2\n1 2\n2 1\n" ]
[ "YES\n1000000007\n0 1 \n1 2 3 ", "YES\n3\n0 1 \n1 2 ", "NO\n" ]
By <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e4ee2bc16f1508a982cfc739e1c7ddc442223116.png" style="max-width: 100.0%;max-height: 100.0%;"/> we denote the remainder of integer division of *b* by *c*. It is guaranteed that if there exists some set of numbers *k*, *a*<sub class="lower-in...
[ { "input": "2 3\n1 2 3\n2 3 4", "output": "YES\n1000000007\n0 1 \n1 2 3 " }, { "input": "2 2\n1 2\n2 0", "output": "YES\n3\n0 1 \n1 2 " }, { "input": "2 2\n1 2\n2 1", "output": "NO" }, { "input": "2 2\n2 3\n1 2", "output": "YES\n1000000007\n0 1000000006 \n2 3 " }, { ...
124
2,867,200
3
32,233
792
Colored Balls
[ "greedy", "math", "number theory" ]
null
null
There are *n* boxes with colored balls on the table. Colors are numbered from 1 to *n*. *i*-th box contains *a**i* balls, all of which have color *i*. You have to write a program that will divide all balls into sets such that: - each ball belongs to exactly one of the sets, - there are no empty sets, - there is no ...
The first line contains one integer number *n* (1<=≤<=*n*<=≤<=500). The second line contains *n* integer numbers *a*1,<=*a*2,<=... ,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
Print one integer number — the minimum possible number of sets.
[ "3\n4 7 8\n", "2\n2 7\n" ]
[ "5\n", "4\n" ]
In the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color.
[ { "input": "3\n4 7 8", "output": "5" }, { "input": "2\n2 7", "output": "4" }, { "input": "1\n1", "output": "1" }, { "input": "1\n1000000000", "output": "1" }, { "input": "2\n1000000000 1", "output": "500000001" }, { "input": "2\n9 6", "output": "5"...
46
0
0
32,245
68
Half-decay tree
[ "data structures", "divide and conquer", "dp", "math", "probabilities" ]
D. Half-decay tree
3
256
Recently Petya has become keen on physics. Anna V., his teacher noticed Petya's interest and gave him a fascinating physical puzzle — a half-decay tree. A half-decay tree is a complete binary tree with the height *h*. The height of a tree is the length of the path (in edges) from the root to a leaf in the tree. While...
First line will contain two integers *h* and *q* (1<=≤<=*h*<=≤<=30,<=1<=≤<=*q*<=≤<=105). Next *q* lines will contain a query of one of two types: - add *v* *e*Petya adds *e* electrons to vertex number *v* (1<=≤<=*v*<=≤<=2*h*<=+<=1<=-<=1,<=0<=≤<=*e*<=≤<=104). *v* and *e* are integers.The vertices of the tree are numbe...
For each query decay solution you should output the mathematical expectation of potential of the tree after being desintegrated. The absolute or relative error in the answer should not exceed 10<=-<=4.
[ "1 4\nadd 1 3\nadd 2 10\nadd 3 11\ndecay\n" ]
[ "13.50000000\n" ]
none
[ { "input": "1 4\nadd 1 3\nadd 2 10\nadd 3 11\ndecay", "output": "13.50000000" }, { "input": "3 6\ndecay\ndecay\nadd 6 872\ndecay\nadd 13 813\nadd 8 531", "output": "0.00000000\n0.00000000\n872.00000000" }, { "input": "3 6\nadd 2 101\nadd 6 830\nadd 11 899\nadd 2 421\ndecay\ndecay", "...
92
819,200
-1
32,281
665
Beautiful Subarrays
[ "data structures", "divide and conquer", "strings", "trees" ]
null
null
One day, ZS the Coder wrote down an array of integers *a*<=with elements *a*1,<=<=*a*2,<=<=...,<=<=*a**n*. A subarray of the array *a* is a sequence *a**l*,<=<=*a**l*<=<=+<=<=1,<=<=...,<=<=*a**r* for some integers (*l*,<=<=*r*) such that 1<=<=≤<=<=*l*<=<=≤<=<=*r*<=<=≤<=<=*n*. ZS the Coder thinks that a subarray of *a*...
The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=106,<=1<=≤<=*k*<=≤<=109) — the number of elements in the array *a* and the value of the parameter *k*. The second line contains *n* integers *a**i* (0<=≤<=*a**i*<=≤<=109) — the elements of the array *a*.
Print the only integer *c* — the number of beautiful subarrays of the array *a*.
[ "3 1\n1 2 3\n", "3 2\n1 2 3\n", "3 3\n1 2 3\n" ]
[ "5\n", "3\n", "2\n" ]
none
[ { "input": "3 1\n1 2 3", "output": "5" }, { "input": "3 2\n1 2 3", "output": "3" }, { "input": "3 3\n1 2 3", "output": "2" }, { "input": "1 1\n1", "output": "1" }, { "input": "10 1\n1 1 0 1 0 1 1 0 0 0", "output": "28" }, { "input": "100 80\n85 16 22 8...
3,000
54,681,600
0
32,309
0
none
[ "none" ]
null
null
Recently, a wild Krakozyabra appeared at Jelly Castle. It is, truth to be said, always eager to have something for dinner. Its favorite meal is natural numbers (typically served with honey sauce), or, to be more precise, the zeros in their corresponding decimal representations. As for other digits, Krakozyabra dislike...
In the first and only string, the numbers *L* and *R* are given – the boundaries of the range (1<=≤<=*L*<=≤<=*R*<=≤<=1018).
Output the sole number – the answer for the problem.
[ "1 10\n", "40 57\n", "157 165\n" ]
[ "9\n", "17\n", "9\n" ]
In the first sample case, the inedible tails are the numbers from 1 to 9. Note that 10 and 1 have the same inedible tail – the number 1. In the second sample case, each number has a unique inedible tail, except for the pair 45, 54. The answer to this sample case is going to be (57 - 40 + 1) - 1 = 17.
[]
1,000
10,752,000
0
32,409
959
Mahmoud and Ehab and the wrong algorithm
[ "constructive algorithms", "trees" ]
null
null
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is: Given an undirected tree consisting of *n* nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (*u*,<=*v*) that belongs to the tree, either *u* i...
The only line contains an integer *n* (2<=≤<=*n*<=≤<=105), the number of nodes in the desired trees.
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only. If the answer for a...
[ "2\n", "8\n" ]
[ "-1\n1 2\n", "1 2\n1 3\n2 4\n2 5\n3 6\n4 7\n4 8\n1 2\n1 3\n2 4\n2 5\n2 6\n3 7\n6 8" ]
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed  - 1 in the first section, but notice that we printed this tree in the second section. In the second sample: In the first tree, the algorithm will find an answer with 4 ...
[ { "input": "2", "output": "-1\n1 2" }, { "input": "8", "output": "1 2\n1 3\n1 4\n2 5\n2 6\n1 7\n1 8\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8" }, { "input": "99", "output": "1 2\n1 3\n1 4\n2 5\n2 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\...
358
9,830,400
3
32,483
420
Cup Trick
[ "data structures" ]
null
null
The employees of the F company have lots of ways to entertain themselves. Today they invited a famous magician who shows a trick with plastic cups and a marble. The point is to trick the spectator's attention. Initially, the spectator stands in front of a line of *n* plastic cups. Then the magician places a small marb...
The first line contains integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=106). Each of the next *m* lines contains a couple of integers. The *i*-th line contains integers *x**i*, *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*) — the description of the *i*-th operation of the magician. Note that the operations are given in the order in...
If the described permutation doesn't exist (the programmer remembered wrong operations), print -1. Otherwise, print *n* distinct integers, each from 1 to *n*: the *i*-th number should represent the mark on the cup that initially is in the row in position *i*. If there are multiple correct answers, you should print the...
[ "2 1\n2 1\n", "3 2\n1 2\n1 1\n", "3 3\n1 3\n2 3\n1 3\n" ]
[ "2 1 \n", "2 1 3 \n", "-1\n" ]
none
[ { "input": "2 1\n2 1", "output": "2 1 " }, { "input": "3 2\n1 2\n1 1", "output": "2 1 3 " }, { "input": "3 3\n1 3\n2 3\n1 3", "output": "-1" }, { "input": "3 2\n1 1\n3 2", "output": "1 3 2 " }, { "input": "5 2\n3 3\n3 1", "output": "1 2 3 4 5 " }, { "i...
46
0
0
32,515
274
Mirror Room
[ "data structures", "implementation" ]
null
null
Imagine an *n*<=×<=*m* grid with some blocked cells. The top left cell in the grid has coordinates (1,<=1) and the bottom right cell has coordinates (*n*,<=*m*). There are *k* blocked cells in the grid and others are empty. You flash a laser beam from the center of an empty cell (*x**s*,<=*y**s*) in one of the diagonal...
The first line of the input contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=105,<=0<=≤<=*k*<=≤<=105). Each of the next *k* lines contains two integers *x**i* and *y**i* (1<=≤<=*x**i*<=≤<=*n*,<=1<=≤<=*y**i*<=≤<=*m*) indicating the position of the *i*-th blocked cell. The last line contains *x**s*, *y**s*...
In the only line of the output print the number of empty cells that the beam goes through at least once. 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.
[ "3 3 0\n1 2 SW\n", "7 5 3\n3 3\n4 3\n5 3\n2 1 SE\n" ]
[ "6\n", "14\n" ]
none
[]
60
0
0
32,523
364
Ghd
[ "brute force", "math", "probabilities" ]
null
null
John Doe offered his sister Jane Doe find the gcd of some set of numbers *a*. Gcd is a positive integer *g*, such that all number from the set are evenly divisible by *g* and there isn't such *g*' (*g*'<=&gt;<=*g*), that all numbers of the set are evenly divisible by *g*'. Unfortunately Jane couldn't cope with the ta...
The first line contains an integer *n* (1<=≤<=*n*<=≤<=106) showing how many numbers are in set *a*. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=1012). Please note, that given set can contain equal numbers. Please, do not write the %lld specifier to read or write 64-bi...
Print a single integer *g* — the Ghd of set *a*.
[ "6\n6 2 3 4 5 6\n", "5\n5 5 6 10 15\n" ]
[ "3\n", "5\n" ]
none
[ { "input": "6\n6 2 3 4 5 6", "output": "3" }, { "input": "5\n5 5 6 10 15", "output": "5" }, { "input": "100\n32 40 7 3 7560 21 7560 7560 10 12 3 7560 7560 7560 7560 5 7560 7560 6 7560 7560 7560 35 7560 18 7560 7560 7560 7560 7560 48 2 7 25 7560 2 2 49 7560 7560 15 16 7560 7560 2 7560 27 ...
218
3,788,800
-1
32,525
566
Matching Names
[ "dfs and similar", "strings", "trees" ]
null
null
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching ps...
The first line contains number *n* (1<=≤<=*n*<=≤<=100<=000) — the number of students in the summer school. Next *n* lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating. The last *n* lines contain the given pseudonyms. Each pseudony...
In the first line print the maximum possible quality of matching pseudonyms to students. In the next *n* lines describe the optimal matching. Each line must have the form *a* *b* (1<=≤<=*a*,<=*b*<=≤<=*n*), that means that the student who was number *a* in the input, must match to the pseudonym number *b* in the input....
[ "5\ngennady\ngalya\nboris\nbill\ntoshik\nbilbo\ntorin\ngendalf\nsmaug\ngaladriel\n" ]
[ "11\n4 1\n2 5\n1 3\n5 2\n3 4\n" ]
The first test from the statement the match looks as follows: - bill  →  bilbo (lcp = 3) - galya  →  galadriel (lcp = 3) - gennady  →  gendalf (lcp = 3) - toshik  →  torin (lcp = 2) - boris  →  smaug (lcp = 0)
[ { "input": "5\ngennady\ngalya\nboris\nbill\ntoshik\nbilbo\ntorin\ngendalf\nsmaug\ngaladriel", "output": "11\n4 1\n2 5\n1 3\n5 2\n3 4" }, { "input": "1\na\na", "output": "1\n1 1" }, { "input": "2\na\na\na\na", "output": "2\n1 1\n2 2" }, { "input": "2\na\nb\na\na", "output"...
109
307,200
0
32,658
0
none
[ "none" ]
null
null
Dreamoon likes to play with sets, integers and . is defined as the largest positive integer that divides both *a* and *b*. Let *S* be a set of exactly four distinct integers greater than 0. Define *S* to be of rank *k* if and only if for all pairs of distinct elements *s**i*, *s**j* from *S*, . Given *k* and *n*, Dr...
The single line of the input contains two space separated integers *n*, *k* (1<=≤<=*n*<=≤<=10<=000,<=1<=≤<=*k*<=≤<=100).
On the first line print a single integer — the minimal possible *m*. On each of the next *n* lines print four space separated integers representing the *i*-th set. Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal *m*, print any o...
[ "1 1\n", "2 2\n" ]
[ "5\n1 2 3 5\n", "22\n2 4 6 22\n14 18 10 16\n" ]
For the first example it's easy to see that set {1, 2, 3, 4} isn't a valid set of rank 1 since <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e2af04e5e60e1fe79a4d74bf22dfa575f0b0f7bb.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "1 1", "output": "5\n1 3 4 5" }, { "input": "2 2", "output": "22\n2 6 8 10\n14 18 20 22" }, { "input": "7 7", "output": "287\n7 21 28 35\n49 63 70 77\n91 105 112 119\n133 147 154 161\n175 189 196 203\n217 231 238 245\n259 273 280 287" }, { "input": "13 7", "out...
93
2,764,800
3
32,826
22
System Administrator
[ "graphs" ]
C. System Administrator
1
256
Bob got a job as a system administrator in X corporation. His first task was to connect *n* servers with the help of *m* two-way direct connection so that it becomes possible to transmit data from one server to any other server via these connections. Each direct connection has to link two different servers, each pair o...
The first input line contains 3 space-separated integer numbers *n*, *m*, *v* (3<=≤<=*n*<=≤<=105,<=0<=≤<=*m*<=≤<=105,<=1<=≤<=*v*<=≤<=*n*), *n* — amount of servers, *m* — amount of direct connections, *v* — index of the server that fails and leads to the failure of the whole system.
If it is impossible to connect the servers in the required way, output -1. Otherwise output *m* lines with 2 numbers each — description of all the direct connections in the system. Each direct connection is described by two numbers — indexes of two servers, linked by this direct connection. The servers are numbered fro...
[ "5 6 3\n", "6 100 1\n" ]
[ "1 2\n2 3\n3 4\n4 5\n1 3\n3 5\n", "-1\n" ]
none
[ { "input": "5 6 3", "output": "1 3\n2 3\n4 3\n5 3\n1 2\n1 4" }, { "input": "6 100 1", "output": "-1" }, { "input": "10 26 1", "output": "2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n4 5\n4 6\n4 7\n4 8" }, { "in...
217
15,052,800
3.863462
32,866
98
Help Shrek and Donkey
[ "dp", "games", "math", "probabilities" ]
E. Help Shrek and Donkey
2
256
Shrek and the Donkey (as you can guess, they also live in the far away kingdom) decided to play a card game called YAGame. The rules are very simple: initially Shrek holds *m* cards and the Donkey holds *n* cards (the players do not see each other's cards), and one more card lies on the table face down so that both pla...
The first line contains space-separated integers *m* and *n* (0<=≤<=*m*,<=*n*<=≤<=1000).
Print space-separated probabilities that Shrek wins and Donkey wins correspondingly; the absolute error should not exceed 10<=-<=9.
[ "0 3\n", "1 0\n", "1 1\n" ]
[ "0.25 0.75\n", "1 0\n", "0.5 0.5\n" ]
none
[ { "input": "0 3", "output": "0.2500000000000000 0.7500000000000000" }, { "input": "1 0", "output": "1.0000000000000000 0.0000000000000000" }, { "input": "1 1", "output": "0.5000000000000000 0.5000000000000000" }, { "input": "0 0", "output": "1.0000000000000000 0.000000000...
310
7,782,400
-1
33,031
128
String
[ "brute force", "constructive algorithms", "hashing", "implementation", "string suffix structures", "strings" ]
null
null
One day in the IT lesson Anna and Maria learned about the lexicographic order. String *x* is lexicographically less than string *y*, if either *x* is a prefix of *y* (and *x*<=≠<=*y*), or there exists such *i* (1<=≤<=*i*<=≤<=*min*(|*x*|,<=|*y*|)), that *x**i*<=&lt;<=*y**i*, and for any *j* (1<=≤<=*j*<=&lt;<=*i*) *x**j...
The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer *k* (1<=≤<=*k*<=≤<=105).
Print the string Anna and Maria need — the *k*-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than *k*, print a string saying "No such line." (without the quotes).
[ "aa\n2\n", "abc\n5\n", "abab\n7\n" ]
[ "a\n", "bc\n", "b\n" ]
In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
[ { "input": "aa\n2", "output": "a" }, { "input": "abc\n5", "output": "bc" }, { "input": "abab\n7", "output": "b" }, { "input": "codeforces\n1", "output": "c" }, { "input": "cccc\n8", "output": "ccc" }, { "input": "abcdefghijklmnopqrstuvwxyz\n27", "o...
530
268,390,400
0
33,144
251
Playing with Permutations
[ "implementation", "math" ]
null
null
Little Petya likes permutations a lot. Recently his mom has presented him permutation *q*1,<=*q*2,<=...,<=*q**n* of length *n*. A permutation *a* of length *n* is a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*), all integers there are distinct. There is only one thing Petya likes more than p...
The first line contains two integers *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=100). The second line contains *n* space-separated integers *q*1,<=*q*2,<=...,<=*q**n* (1<=≤<=*q**i*<=≤<=*n*) — the permutation that Petya's got as a present. The third line contains Masha's permutation *s*, in the similar format. It is guaranteed t...
If the situation that is described in the statement is possible, print "YES" (without the quotes), otherwise print "NO" (without the quotes).
[ "4 1\n2 3 4 1\n1 2 3 4\n", "4 1\n4 3 1 2\n3 4 2 1\n", "4 3\n4 3 1 2\n3 4 2 1\n", "4 2\n4 3 1 2\n2 1 4 3\n", "4 1\n4 3 1 2\n2 1 4 3\n" ]
[ "NO\n", "YES\n", "YES\n", "YES\n", "NO\n" ]
In the first sample Masha's permutation coincides with the permutation that was written on the board before the beginning of the game. Consequently, that violates the condition that Masha's permutation never occurred on the board before *k* moves were performed. In the second sample the described situation is possible...
[ { "input": "4 1\n2 3 4 1\n1 2 3 4", "output": "NO" }, { "input": "4 1\n4 3 1 2\n3 4 2 1", "output": "YES" }, { "input": "4 3\n4 3 1 2\n3 4 2 1", "output": "YES" }, { "input": "4 2\n4 3 1 2\n2 1 4 3", "output": "YES" }, { "input": "4 1\n4 3 1 2\n2 1 4 3", "outp...
154
0
0
33,162
254
Anagram
[ "greedy", "strings" ]
null
null
String *x* is an anagram of string *y*, if we can rearrange the letters in string *x* and get exact string *y*. For example, strings "DOG" and "GOD" are anagrams, so are strings "BABA" and "AABB", but strings "ABBAC" and "CAABA" are not. You are given two strings *s* and *t* of the same length, consisting of uppercase...
The input consists of two lines. The first line contains string *s*, the second line contains string *t*. The strings have the same length (from 1 to 105 characters) and consist of uppercase English letters.
In the first line print *z* — the minimum number of replacement operations, needed to get an anagram of string *t* from string *s*. In the second line print the lexicographically minimum anagram that could be obtained in *z* operations.
[ "ABA\nCBA\n", "CDBABC\nADCABD\n" ]
[ "1\nABC\n", "2\nADBADC\n" ]
The second sample has eight anagrams of string *t*, that can be obtained from string *s* by replacing exactly two letters: "ADBADC", "ADDABC", "CDAABD", "CDBAAD", "CDBADA", "CDDABA", "DDAABC", "DDBAAC". These anagrams are listed in the lexicographical order. The lexicographically minimum anagram is "ADBADC".
[ { "input": "ABA\nCBA", "output": "1\nABC" }, { "input": "CDBABC\nADCABD", "output": "2\nADBADC" }, { "input": "AABAA\nBBAAA", "output": "1\nAABAB" }, { "input": "OVGHK\nRPGUC", "output": "4\nCPGRU" }, { "input": "CCAACBA\nBBBAACC", "output": "2\nBCAACBB" }, ...
62
307,200
-1
33,345
0
none
[ "none" ]
null
null
In the country of Never, there are *n* cities and a well-developed road system. There is exactly one bidirectional road between every pair of cities, thus, there are as many as roads! No two roads intersect, and no road passes through intermediate cities. The art of building tunnels and bridges has been mastered by Ne...
The first line contains a single integer *n* (2<=≤<=*n*<=≤<=2000) — the number of cities in Never. The following *n*<=-<=1 lines contain the description of the road network. The *i*-th of these lines contains *n*<=-<=*i* integers. The *j*-th integer in the *i*-th line denotes the perishability of the road between citi...
For each city in order from 1 to *n*, output the minimum possible sum of perishabilities of the routes to this city from all the other cities of Never if the signposts are set in a way which minimizes this sum.
[ "3\n1 2\n3\n", "6\n2 9 9 6 6\n7 1 9 10\n9 2 5\n4 10\n8\n" ]
[ "2\n2\n3\n", "6\n5\n7\n5\n7\n11\n" ]
The first example is explained by the picture below. From left to right, there is the initial road network and the optimal directions of the signposts in case the museum is built in city 1, 2 and 3, respectively. The museum city is represented by a blue circle, the directions of the signposts are represented by green a...
[]
46
0
0
33,382
847
Travel Cards
[ "greedy", "implementation", "sortings" ]
null
null
In the evening Polycarp decided to analyze his today's travel expenses on public transport. The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop t...
The first line contains five integers *n*,<=*a*,<=*b*,<=*k*,<=*f* (1<=≤<=*n*<=≤<=300, 1<=≤<=*b*<=&lt;<=*a*<=≤<=100, 0<=≤<=*k*<=≤<=300, 1<=≤<=*f*<=≤<=1000) where: - *n* — the number of Polycarp trips, - *a* — the cost of a regualar single trip, - *b* — the cost of a trip after a transshipment, - *k* — the maximum n...
Print the smallest amount of money Polycarp could have spent today, if he can purchase no more than *k* travel cards.
[ "3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank\n", "4 2 1 300 1000\na A\nA aa\naa AA\nAA a\n" ]
[ "11\n", "5\n" ]
In the first example Polycarp can buy travel card for the route "BerBank <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ecc94b07e73defe233bfe831f3977337706a2d27.png" style="max-width: 100.0%;max-height: 100.0%;"/> University" and spend 8 burles. Note that his second trip "University" <img ...
[ { "input": "3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank", "output": "11" }, { "input": "4 2 1 300 1000\na A\nA aa\naa AA\nAA a", "output": "5" }, { "input": "2 2 1 0 1\naca BCBA\nBCBA aca", "output": "3" }, { "input": "2 2 1 2 1\nBDDB C\nC BDDB", ...
124
23,142,400
3
33,395
439
Devu and Birthday Celebration
[ "combinatorics", "dp", "math" ]
null
null
Today is Devu's birthday. For celebrating the occasion, he bought *n* sweets from the nearby market. He has invited his *f* friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to ea...
The first line contains an integer *q* representing the number of queries (1<=≤<=*q*<=≤<=105). Each of the next *q* lines contains two space space-separated integers *n*, *f* (1<=≤<=*f*<=≤<=*n*<=≤<=105).
For each query, output a single integer in a line corresponding to the answer of each query.
[ "5\n6 2\n7 2\n6 3\n6 4\n7 4\n" ]
[ "2\n6\n9\n10\n20\n" ]
For first query: *n* = 6, *f* = 2. Possible partitions are [1, 5] and [5, 1]. For second query: *n* = 7, *f* = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
[ { "input": "5\n6 2\n7 2\n6 3\n6 4\n7 4", "output": "2\n6\n9\n10\n20" }, { "input": "10\n1 1\n1 1\n1 1\n7 2\n6 3\n9 5\n4 1\n2 1\n3 1\n2 2", "output": "1\n1\n1\n6\n9\n70\n0\n0\n0\n1" }, { "input": "40\n37 15\n48 10\n16 5\n25 23\n32 20\n24 4\n46 19\n16 13\n1 1\n37 22\n44 29\n24 6\n27 10\n39...
46
0
0
33,497
111
Petya and Inequiations
[ "greedy" ]
A. Petya and Inequiations
2
256
Little Petya loves inequations. Help him find *n* positive integers *a*1,<=*a*2,<=...,<=*a**n*, such that the following two conditions are satisfied: - *a*12<=+<=*a*22<=+<=...<=+<=*a**n*2<=≥<=*x*- *a*1<=+<=*a*2<=+<=...<=+<=*a**n*<=≤<=*y*
The first line contains three space-separated integers *n*, *x* and *y* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*x*<=≤<=1012,<=1<=≤<=*y*<=≤<=106). Please do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use cin, cout streams or the %I64d specificator.
Print *n* positive integers that satisfy the conditions, one integer per line. If such numbers do not exist, print a single number "-1". If there are several solutions, print any of them.
[ "5 15 15\n", "2 3 2\n", "1 99 11\n" ]
[ "4\n4\n1\n1\n2\n", "-1\n", "11\n" ]
none
[ { "input": "5 15 15", "output": "11\n1\n1\n1\n1" }, { "input": "2 3 2", "output": "-1" }, { "input": "1 99 11", "output": "11" }, { "input": "100000 810000099998 1000000", "output": "900001\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n...
404
8,089,600
0
33,542
101
Vectors
[ "implementation", "math" ]
C. Vectors
1
256
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
[ { "input": "0 0\n1 1\n0 1", "output": "YES" }, { "input": "0 0\n1 1\n1 1", "output": "YES" }, { "input": "0 0\n1 1\n2 2", "output": "NO" }, { "input": "2 3\n2 3\n0 0", "output": "YES" }, { "input": "-4 -2\n0 0\n-2 -1", "output": "YES" }, { "input": "-1...
0
0
-1
33,593
821
Okabe and El Psy Kongroo
[ "dp", "matrices" ]
null
null
Okabe likes to take walks but knows that spies from the Organization could be anywhere; that's why he wants to know how many different walks he can take in his city safely. Okabe's city can be represented as all points (*x*,<=*y*) such that *x* and *y* are non-negative. Okabe starts at the origin (point (0,<=0)), and n...
The first line of input contains the integers *n* and *k* (1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=1018) — the number of segments and the destination *x* coordinate. The next *n* lines contain three space-separated integers *a**i*, *b**i*, and *c**i* (0<=≤<=*a**i*<=&lt;<=*b**i*<=≤<=1018, 0<=≤<=*c**i*<=≤<=15) — the left and r...
Print the number of walks satisfying the conditions, modulo 1000000007 (109<=+<=7).
[ "1 3\n0 3 3\n", "2 6\n0 3 0\n3 10 2\n" ]
[ "4\n", "4\n" ]
The graph above corresponds to sample 1. The possible walks are: - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7fcce410dbd2cf4e427a6b50e0f159b7ce538901.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/...
[ { "input": "1 3\n0 3 3", "output": "4" }, { "input": "2 6\n0 3 0\n3 10 2", "output": "4" }, { "input": "2 3\n0 2 13\n2 3 11", "output": "4" }, { "input": "2 9\n0 8 0\n8 10 10", "output": "1" }, { "input": "1 1\n0 3 9", "output": "1" }, { "input": "3 8\...
514
10,137,600
3
33,662
815
Karen and Cards
[ "binary search", "combinatorics", "data structures", "geometry" ]
null
null
Karen just got home from the supermarket, and is getting ready to go to sleep. After taking a shower and changing into her pajamas, she looked at her shelf and saw an album. Curious, she opened it and saw a trading card collection. She recalled that she used to play with those cards as a child, and, although she is n...
The first line of input contains four integers, *n*, *p*, *q* and *r* (1<=≤<=*n*,<=*p*,<=*q*,<=*r*<=≤<=500000), the number of cards in the collection, the maximum possible strength, the maximum possible defense, and the maximum possible speed, respectively. The next *n* lines each contain three integers. In particular...
Output a single integer on a line by itself, the number of different cards that can beat all the cards in her collection.
[ "3 4 4 5\n2 2 5\n1 3 4\n4 1 1\n", "5 10 10 10\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n" ]
[ "10\n", "972\n" ]
In the first test case, the maximum possible strength is 4, the maximum possible defense is 4 and the maximum possible speed is 5. Karen has three cards: - The first card has strength 2, defense 2 and speed 5. - The second card has strength 1, defense 3 and speed 4. - The third card has strength 4, defense 1 and sp...
[]
46
0
-1
33,755
264
Choosing Balls
[ "dp" ]
null
null
There are *n* balls. They are arranged in a row. Each ball has a color (for convenience an integer) and an integer value. The color of the *i*-th ball is *c**i* and the value of the *i*-th ball is *v**i*. Squirrel Liss chooses some balls and makes a new sequence without changing the relative order of the balls. She wa...
The first line contains two integers *n* and *q* (1<=≤<=*n*<=≤<=105; 1<=≤<=*q*<=≤<=500). The second line contains *n* integers: *v*1,<=*v*2,<=...,<=*v**n* (|*v**i*|<=≤<=105). The third line contains *n* integers: *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=*n*). The following *q* lines contain the values of the const...
For each query, output a line containing an integer — the answer to the query. The *i*-th line contains the answer to the *i*-th query in the input order. 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.
[ "6 3\n1 -2 3 4 0 -1\n1 2 1 2 1 1\n5 1\n-2 1\n1 0\n", "4 1\n-3 6 -1 2\n1 2 3 1\n1 -1\n" ]
[ "20\n9\n4\n", "5\n" ]
In the first example, to achieve the maximal value: - In the first query, you should select 1st, 3rd, and 4th ball. - In the second query, you should select 3rd, 4th, 5th and 6th ball. - In the third query, you should select 2nd and 4th ball. Note that there may be other ways to achieve the maximal value.
[]
124
0
0
33,762
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
[]
31
0
0
33,810
766
Mahmoud and a Dictionary
[ "data structures", "dfs and similar", "dp", "dsu", "graphs" ]
null
null
Mahmoud wants to write a new dictionary that contains *n* words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discovers a new relation between two words. He know that if two words hav...
The first line of input contains three integers *n*, *m* and *q* (2<=≤<=*n*<=≤<=105, 1<=≤<=*m*,<=*q*<=≤<=105) where *n* is the number of words in the dictionary, *m* is the number of relations Mahmoud figured out and *q* is the number of questions Mahmoud asked after telling all relations. The second line contains *n*...
First, print *m* lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print "NO" (without quotes) and ignore it, otherwise print "YES" (without quotes). After that print *q* lines, one per each question. If the two words have the same ...
[ "3 3 4\nhate love like\n1 love like\n2 love hate\n1 hate like\nlove like\nlove hate\nlike hate\nhate like\n", "8 6 5\nhi welcome hello ihateyou goaway dog cat rat\n1 hi welcome\n1 ihateyou goaway\n2 hello ihateyou\n2 hi goaway\n2 hi hello\n1 hi hello\ndog cat\ndog hi\nhi hello\nihateyou goaway\nwelcome ihateyou\n...
[ "YES\nYES\nNO\n1\n2\n2\n2\n", "YES\nYES\nYES\nYES\nNO\nYES\n3\n3\n1\n1\n2\n" ]
none
[ { "input": "3 3 4\nhate love like\n1 love like\n2 love hate\n1 hate like\nlove like\nlove hate\nlike hate\nhate like", "output": "YES\nYES\nNO\n1\n2\n2\n2" }, { "input": "8 6 5\nhi welcome hello ihateyou goaway dog cat rat\n1 hi welcome\n1 ihateyou goaway\n2 hello ihateyou\n2 hi goaway\n2 hi hello\n...
2,230
99,430,400
-1
33,877
493
Vasya and Polynomial
[ "math" ]
null
null
Vasya is studying in the last class of school and soon he will take exams. He decided to study polynomials. Polynomial is a function *P*(*x*)<==<=*a*0<=+<=*a*1*x*1<=+<=...<=+<=*a**n**x**n*. Numbers *a**i* are called coefficients of a polynomial, non-negative integer *n* is called a degree of a polynomial. Vasya has ma...
The input contains three integer positive numbers no greater than 1018.
If there is an infinite number of such polynomials, then print "inf" without quotes, otherwise print the reminder of an answer modulo 109<=+<=7.
[ "2 2 2\n", "2 3 3\n" ]
[ "2\n", "1\n" ]
none
[ { "input": "2 2 2", "output": "2" }, { "input": "2 3 3", "output": "1" }, { "input": "1 1 1", "output": "inf" }, { "input": "3 5 10", "output": "0" }, { "input": "2 3 1000000000000000000", "output": "0" }, { "input": "7 8 9", "output": "1" }, {...
77
0
0
33,928
682
Alyona and the Tree
[ "dfs and similar", "dp", "graphs", "trees" ]
null
null
Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on. The girl noticed that some of the tree's vertices are sad, so she decided to play with them. Let's call ver...
In the first line of the input integer *n* (1<=≤<=*n*<=≤<=105) is given — the number of vertices in the tree. In the second line the sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) is given, where *a**i* is the number written on vertex *i*. The next *n*<=-<=1 lines describe tree edges: *i**...
Print the only integer — the minimum number of leaves Alyona needs to remove such that there will be no any sad vertex left in the tree.
[ "9\n88 22 83 14 95 91 98 53 11\n3 24\n7 -8\n1 67\n1 64\n9 65\n5 12\n6 -80\n3 8\n" ]
[ "5\n" ]
The following image represents possible process of removing leaves from the tree:
[ { "input": "9\n88 22 83 14 95 91 98 53 11\n3 24\n7 -8\n1 67\n1 64\n9 65\n5 12\n6 -80\n3 8", "output": "5" }, { "input": "6\n53 82 15 77 71 23\n5 -77\n6 -73\n2 0\n1 26\n4 -92", "output": "0" }, { "input": "10\n99 60 68 46 51 11 96 41 48 99\n4 50\n6 -97\n3 -92\n7 1\n9 99\n2 79\n1 -15\n8 -6...
748
58,777,600
3
34,036
212
Polycarpus is Looking for Good Substrings
[ "bitmasks", "hashing", "implementation" ]
null
null
We'll call string *s*[*a*,<=*b*]<==<=*s**a**s**a*<=+<=1... *s**b* (1<=≤<=*a*<=≤<=*b*<=≤<=|*s*|) a substring of string *s*<==<=*s*1*s*2... *s*|*s*|, where |*s*| is the length of string *s*. The trace of a non-empty string *t* is a set of characters that the string consists of. For example, the trace of string "aab" equ...
The first line contains a non-empty string *s* (1<=≤<=|*s*|<=≤<=106). The second line contains a single integer *m* (1<=≤<=*m*<=≤<=104). Next *m* lines contain descriptions of sets *C**i*. The *i*-th line contains string *c**i* such that its trace equals *C**i*. It is guaranteed that all characters of each string *c**...
Print *m* integers — the *i*-th integer must equal *r*(*C**i*,<=*s*).
[ "aaaaa\n2\na\na\n", "abacaba\n3\nac\nba\na\n" ]
[ "1\n1\n", "1\n2\n4\n" ]
none
[]
92
0
0
34,073
1,007
Mini Metro
[ "dp" ]
null
null
In a simplified version of a "Mini Metro" game, there is only one subway line, and all the trains go in the same direction. There are $n$ stations on the line, $a_i$ people are waiting for the train at the $i$-th station at the beginning of the game. The game starts at the beginning of the $0$-th hour. At the end of ea...
The first line contains three integers $n$, $t$, and $k$ ($1 \leq n, t \leq 200, 1 \leq k \leq 10^9$) — the number of stations on the line, hours we want to survive, and capacity of each train respectively. Each of the next $n$ lines contains three integers $a_i$, $b_i$, and $c_i$ ($0 \leq a_i, b_i \leq c_i \leq 10^9$...
Output a single integer number — the answer to the problem.
[ "3 3 10\n2 4 10\n3 3 9\n4 2 8\n", "4 10 5\n1 1 1\n1 0 1\n0 5 8\n2 7 100\n" ]
[ "2\n", "12\n" ]
<img class="tex-graphics" src="https://espresso.codeforces.com/bfa11d535d9fc44e73f6f8280d06436e4e327753.png" style="max-width: 100.0%;max-height: 100.0%;"/> Let's look at the sample. There are three stations, on the first, there are initially 2 people, 3 people on the second, and 4 people on the third. Maximal capacit...
[]
109
0
-1
34,078
0
none
[ "none" ]
null
null
I won't feel lonely, nor will I be sorrowful... not before everything is buried. A string of *n* beads is left as the message of leaving. The beads are numbered from 1 to *n* from left to right, each having a shape numbered by integers between 1 and *n* inclusive. Some beads may have the same shapes. The memory of a ...
The first line of input contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100<=000) — the number of beads in the string, and the total number of changes and queries, respectively. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*) — the initial shapes of beads ...
For each query, print one line with an integer — the memory of the recalled subsegment.
[ "7 6\n1 2 3 1 3 2 1\n2 3 7\n2 1 3\n1 7 2\n1 3 2\n2 1 6\n2 5 7\n", "7 5\n1 3 2 1 4 2 3\n1 1 4\n2 2 3\n1 1 7\n2 4 5\n1 1 7\n" ]
[ "5\n0\n7\n1\n", "0\n0\n" ]
The initial string of beads has shapes (1, 2, 3, 1, 3, 2, 1). Consider the changes and queries in their order: 1. 2 3 7: the memory of the subsegment [3, 7] is (7 - 4) + (6 - 6) + (5 - 3) = 5; 1. 2 1 3: the memory of the subsegment [1, 3] is (1 - 1) + (2 - 2) + (3 - 3) = 0; 1. 1 7 2: the shape of the 7-th bead ch...
[]
46
0
0
34,117
149
Martian Clock
[ "implementation" ]
null
null
Having stayed home alone, Petya decided to watch forbidden films on the Net in secret. "What ungentlemanly behavior!" — you can say that, of course, but don't be too harsh on the kid. In his country films about the Martians and other extraterrestrial civilizations are forbidden. It was very unfair to Petya as he adored...
The first line contains a single string as "*a*:*b*" (without the quotes). There *a* is a non-empty string, consisting of numbers and uppercase Latin letters. String *a* shows the number of hours. String *b* is a non-empty string that consists of numbers and uppercase Latin letters. String *b* shows the number of minut...
Print the radixes of the numeral systems that can represent the time "*a*:*b*" in the increasing order. Separate the numbers with spaces or line breaks. If there is no numeral system that can represent time "*a*:*b*", print the single integer 0. If there are infinitely many numeral systems that can represent the time "...
[ "11:20\n", "2A:13\n", "000B:00001\n" ]
[ "3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22", "0\n", "-1\n" ]
Let's consider the first sample. String "11:20" can be perceived, for example, as time 4:6, represented in the ternary numeral system or as time 17:32 in hexadecimal system. Let's consider the second sample test. String "2A:13" can't be perceived as correct time in any notation. For example, let's take the base-11 nu...
[ { "input": "11:20", "output": "3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22" }, { "input": "2A:13", "output": "0" }, { "input": "000B:00001", "output": "-1" }, { "input": "00000:00000", "output": "-1" }, { "input": "70:00", "output": "0" }, { "...
154
0
0
34,139
291
Network Mask
[ "*special", "bitmasks", "brute force", "implementation" ]
null
null
The problem uses a simplified TCP/IP address model, please make sure you've read the statement attentively. Polycarpus has found a job, he is a system administrator. One day he came across *n* IP addresses. Each IP address is a 32 bit number, represented as a group of four 8-bit numbers (without leading zeroes), separ...
The first line contains two integers, *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=105) — the number of IP addresses and networks. The next *n* lines contain the IP addresses. It is guaranteed that all IP addresses are distinct.
In a single line print the IP address of the subnet mask in the format that is described in the statement, if the required subnet mask exists. Otherwise, print -1.
[ "5 3\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3\n", "5 2\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3\n", "2 1\n255.0.0.1\n0.0.0.2\n" ]
[ "255.255.254.0", "255.255.0.0", "-1\n" ]
none
[ { "input": "5 3\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3", "output": "255.255.254.0" }, { "input": "5 2\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3", "output": "255.255.0.0" }, { "input": "2 1\n255.0.0.1\n0.0.0.2", "output": "-1" }, { "input": "10 2\n57.11.146.42\n200.13...
2,000
138,342,400
0
34,268
858
Tests Renumeration
[ "greedy", "implementation" ]
null
null
The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website. Unfortunately, the archive with Olympiad's data is a mess. For example, the files with tests are named arbitrary without any logic. Vladimir wants to re...
The first line contains single integer *n* (1<=≤<=*n*<=≤<=105) — the number of files with tests. *n* lines follow, each describing a file with test. Each line has a form of "name_i type_i", where "name_i" is the filename, and "type_i" equals "1", if the *i*-th file contains an example test, and "0" if it contains a re...
In the first line print the minimum number of lines in Vladimir's script file. After that print the script file, each line should be "move file_1 file_2", where "file_1" is an existing at the moment of this line being run filename, and "file_2" — is a string of digits and small English letters with length from 1 to 6.
[ "5\n01 0\n2 1\n2extra 0\n3 1\n99 0\n", "2\n1 0\n2 1\n", "5\n1 0\n11 1\n111 0\n1111 1\n11111 0\n" ]
[ "4\nmove 3 1\nmove 01 5\nmove 2extra 4\nmove 99 3\n", "3\nmove 1 3\nmove 2 1\nmove 3 2", "5\nmove 1 5\nmove 11 1\nmove 1111 2\nmove 111 4\nmove 11111 3\n" ]
none
[ { "input": "5\n01 0\n2 1\n2extra 0\n3 1\n99 0", "output": "4\nmove 3 1\nmove 01 5\nmove 2extra 4\nmove 99 3" }, { "input": "2\n1 0\n2 1", "output": "3\nmove 1 odt0m5\nmove 2 1\nmove odt0m5 2" }, { "input": "5\n1 0\n11 1\n111 0\n1111 1\n11111 0", "output": "5\nmove 1 5\nmove 11 1\nmov...
77
819,200
0
34,329
0
none
[ "none" ]
null
null
You are given set of *n* points in 5-dimensional space. The points are labeled from 1 to *n*. No two points coincide. We will call point *a* bad if there are different points *b* and *c*, not equal to *a*, from the given set such that angle between vectors and is acute (i.e. strictly less than ). Otherwise, the poin...
The first line of input contains a single integer *n* (1<=≤<=*n*<=≤<=103) — the number of points. The next *n* lines of input contain five integers *a**i*,<=*b**i*,<=*c**i*,<=*d**i*,<=*e**i* (|*a**i*|,<=|*b**i*|,<=|*c**i*|,<=|*d**i*|,<=|*e**i*|<=≤<=103)  — the coordinates of the i-th point. All points are distinct.
First, print a single integer *k* — the number of good points. Then, print *k* integers, each on their own line — the indices of the good points in ascending order.
[ "6\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 0 1\n", "3\n0 0 1 2 0\n0 0 9 2 0\n0 0 5 9 0\n" ]
[ "1\n1\n", "0\n" ]
In the first sample, the first point forms exactly a <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/362ac8c7a7496dffc06cd0c843287cd822be63c3.png" style="max-width: 100.0%;max-height: 100.0%;"/> angle with all other pairs of points, so it is good. In the second sample, along the cd plane, ...
[ { "input": "6\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 0 1", "output": "1\n1" }, { "input": "3\n0 0 1 2 0\n0 0 9 2 0\n0 0 5 9 0", "output": "0" }, { "input": "1\n0 0 0 0 0", "output": "1\n1" }, { "input": "2\n0 1 2 3 4\n5 6 7 8 9", "output": "2\n1\n2"...
62
204,800
0
34,352
771
Bear and Company
[ "dp" ]
null
null
Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible. Limak has a string *s*...
The first line of the input contains an integer *n* (1<=≤<=*n*<=≤<=75) — the length of the string. The second line contains a string *s*, consisting of uppercase English letters. The length of the string is equal to *n*.
Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring "VK".
[ "4\nVKVK\n", "5\nBVVKV\n", "7\nVVKEVKK\n", "20\nVKVKVVVKVOVKVQKKKVVK\n", "5\nLIMAK\n" ]
[ "3\n", "2\n", "3\n", "8\n", "0\n" ]
In the first sample, the initial string is "VKVK". The minimum possible number of moves is 3. One optimal sequence of moves is: 1. Swap two last letters. The string becomes "VKKV".1. Swap first two letters. The string becomes "KVKV".1. Swap the second and the third letter. The string becomes "KKVV". Indeed, this str...
[ { "input": "4\nVKVK", "output": "3" }, { "input": "5\nBVVKV", "output": "2" }, { "input": "7\nVVKEVKK", "output": "3" }, { "input": "20\nVKVKVVVKVOVKVQKKKVVK", "output": "8" }, { "input": "5\nLIMAK", "output": "0" }, { "input": "1\nV", "output": "0...
31
4,812,800
0
34,363
936
Lock Puzzle
[ "constructive algorithms", "implementation", "strings" ]
null
null
Welcome to another task about breaking the code lock! Explorers Whitfield and Martin came across an unusual safe, inside of which, according to rumors, there are untold riches, among which one can find the solution of the problem of discrete logarithm! Of course, there is a code lock is installed on the safe. The lock...
The first line contains an integer *n*, the length of the strings *s* and *t* (1<=≤<=*n*<=≤<=2<=000). After that, there are two strings *s* and *t*, consisting of *n* lowercase Latin letters each.
If it is impossible to get string *t* from string *s* using no more than 6100 operations «shift», print a single number <=-<=1. Otherwise, in the first line output the number of operations *k* (0<=≤<=*k*<=≤<=6100). In the next line output *k* numbers *x**i* corresponding to the operations «shift *x**i*» (0<=≤<=*x**i*<...
[ "6\nabacbb\nbabcba\n", "3\naba\nbba\n" ]
[ "4\n6 3 2 3\n", "-1\n" ]
none
[ { "input": "6\nabacbb\nbabcba", "output": "13\n2 6 1 4 0 3 6 3 1 1 1 5 6 " }, { "input": "3\naba\nbba", "output": "-1" }, { "input": "1\nw\nw", "output": "2\n0 1 " }, { "input": "2\nvb\nvb", "output": "2\n1 1 " }, { "input": "7\nvhypflg\nvprhfly", "output": "-...
77
204,800
-1
34,385
328
Sheldon and Ice Pieces
[ "greedy" ]
null
null
Do you remember how Kai constructed the word "eternity" using pieces of ice as components? Little Sheldon plays with pieces of ice, each piece has exactly one digit between 0 and 9. He wants to construct his favourite number *t*. He realized that digits 6 and 9 are very similar, so he can rotate piece of ice with 6 to...
The first line contains integer *t* (1<=≤<=*t*<=≤<=10000). The second line contains the sequence of digits on the pieces. The length of line is equal to the number of pieces and between 1 and 200, inclusive. It contains digits between 0 and 9.
Print the required number of instances.
[ "42\n23454\n", "169\n12118999\n" ]
[ "2\n", "1\n" ]
This problem contains very weak pretests.
[ { "input": "42\n23454", "output": "2" }, { "input": "169\n12118999", "output": "1" }, { "input": "1\n1", "output": "1" }, { "input": "7\n777", "output": "3" }, { "input": "18\n8118", "output": "2" }, { "input": "33\n33333333", "output": "4" }, ...
122
0
3
34,487
119
Education Reform
[ "dp" ]
null
null
Yet another education system reform has been carried out in Berland recently. The innovations are as follows: An academic year now consists of *n* days. Each day pupils study exactly one of *m* subjects, besides, each subject is studied for no more than one day. After the lessons of the *i*-th subject pupils get the h...
The first line contains three integers *n*, *m*, *k* (1<=≤<=*n*<=≤<=*m*<=≤<=50, 1<=≤<=*k*<=≤<=100) which represent the number of days in an academic year, the number of subjects and the *k* parameter correspondingly. Each of the following *m* lines contains the description of a subject as three integers *a**i*, *b**i*,...
If no valid solution exists, print the single word "NO" (without the quotes). Otherwise, the first line should contain the word "YES" (without the quotes) and the next *n* lines should contain any timetable that satisfies all the conditions. The *i*<=+<=1-th line should contain two positive integers: the number of the ...
[ "4 5 2\n1 10 1\n1 10 2\n1 10 3\n1 20 4\n1 100 5\n", "3 4 3\n1 3 1\n2 4 4\n2 3 3\n2 2 2\n" ]
[ "YES\n2 8\n3 10\n4 20\n5 40\n", "NO" ]
none
[]
92
0
-1
34,509
873
Awards For Contestants
[ "brute force", "data structures", "dp" ]
null
null
Alexey recently held a programming contest for students from Berland. *n* students participated in a contest, *i*-th of them solved *a**i* problems. Now he wants to award some contestants. Alexey can award the students with diplomas of three different degrees. Each student either will receive one diploma of some degree...
The first line contains one integer number *n* (3<=≤<=*n*<=≤<=3000). The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=5000).
Output *n* numbers. *i*-th number must be equal to the degree of diploma *i*-th contestant will receive (or <=-<=1 if he doesn't receive any diploma). If there are multiple optimal solutions, print any of them. It is guaranteed that the answer always exists.
[ "4\n1 2 3 4\n", "6\n1 4 3 1 1 2\n" ]
[ "3 3 2 1 \n", "-1 1 2 -1 -1 3 \n" ]
none
[ { "input": "4\n1 2 3 4", "output": "3 3 2 1 " }, { "input": "6\n1 4 3 1 1 2", "output": "-1 1 2 -1 -1 3 " }, { "input": "100\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
109
3,584,000
-1
34,535
825
Minimal Labels
[ "data structures", "dfs and similar", "graphs", "greedy" ]
null
null
You are given a directed acyclic graph with *n* vertices and *m* edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected. You should assign labels to all vertices in such a way that: - Labels form a valid permutation of length *n* — an integer sequence such that each ...
The first line contains two integer numbers *n*, *m* (2<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=105). Next *m* lines contain two integer numbers *v* and *u* (1<=≤<=*v*,<=*u*<=≤<=*n*,<=*v*<=≠<=*u*) — edges of the graph. Edges are directed, graph doesn't contain loops or multiple edges.
Print *n* numbers — lexicographically smallest correct permutation of labels of vertices.
[ "3 3\n1 2\n1 3\n3 2\n", "4 5\n3 1\n4 1\n2 3\n3 4\n2 4\n", "5 4\n3 1\n2 1\n2 3\n4 5\n" ]
[ "1 3 2 \n", "4 1 2 3 \n", "3 1 2 4 5 \n" ]
none
[ { "input": "3 3\n1 2\n1 3\n3 2", "output": "1 3 2 " }, { "input": "4 5\n3 1\n4 1\n2 3\n3 4\n2 4", "output": "4 1 2 3 " }, { "input": "5 4\n3 1\n2 1\n2 3\n4 5", "output": "3 1 2 4 5 " }, { "input": "2 1\n2 1", "output": "2 1 " }, { "input": "5 10\n5 2\n4 1\n2 1\n3 ...
31
0
0
34,580
990
GCD Counting
[ "divide and conquer", "dp", "dsu", "number theory", "trees" ]
null
null
You are given a tree consisting of $n$ vertices. A number is written on each vertex; the number on vertex $i$ is equal to $a_i$. Let's denote the function $g(x, y)$ as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex $x$ to vertex $y$ (including these two vert...
The first line contains one integer $n$ — the number of vertices $(1 \le n \le 2 \cdot 10^5)$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 2 \cdot 10^5)$ — the numbers written on vertices. Then $n - 1$ lines follow, each containing two integers $x$ and $y$ $(1 \le x, y \le n, x \ne ...
For every integer $i$ from $1$ to $2 \cdot 10^5$ do the following: if there is no pair $(x, y)$ such that $x \le y$ and $g(x, y) = i$, don't output anything. Otherwise output two integers: $i$ and the number of aforementioned pairs. You have to consider the values of $i$ in ascending order. See the examples for better...
[ "3\n1 2 3\n1 2\n2 3\n", "6\n1 2 4 8 16 32\n1 6\n6 3\n3 4\n4 2\n6 5\n", "4\n9 16 144 6\n1 3\n2 3\n4 3\n" ]
[ "1 4\n2 1\n3 1\n", "1 6\n2 5\n4 6\n8 1\n16 2\n32 1\n", "1 1\n2 1\n3 1\n6 2\n9 2\n16 2\n144 1\n" ]
none
[ { "input": "3\n1 2 3\n1 2\n2 3", "output": "1 4\n2 1\n3 1" }, { "input": "6\n1 2 4 8 16 32\n1 6\n6 3\n3 4\n4 2\n6 5", "output": "1 6\n2 5\n4 6\n8 1\n16 2\n32 1" }, { "input": "4\n9 16 144 6\n1 3\n2 3\n4 3", "output": "1 1\n2 1\n3 1\n6 2\n9 2\n16 2\n144 1" }, { "input": "3\n1 ...
4,500
207,667,200
0
34,606
370
Broken Monitor
[ "brute force", "constructive algorithms", "greedy", "implementation" ]
null
null
Innocentius has a problem — his computer monitor has broken. Now some of the pixels are "dead", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus. Innocentius is touch-typing a program that pain...
The first line contains the resolution of the monitor as a pair of integers *n*, *m* (1<=≤<=*n*,<=*m*<=≤<=2000). The next *n* lines contain exactly *m* characters each — the state of the monitor pixels at the moment of the game. Character "." (period, ASCII code 46) corresponds to the black pixel, and character "w" (lo...
Print the monitor screen. Represent the sought frame by characters "+" (the "plus" character). The pixels that has become white during the game mustn't be changed. Print them as "w". If there are multiple possible ways to position the frame of the minimum size, print any of them. If the required frame doesn't exist, t...
[ "4 8\n..w..w..\n........\n........\n..w..w..\n", "5 6\n......\n.w....\n......\n..w...\n......\n", "2 4\n....\n.w..\n", "2 6\nw..w.w\n...w..\n" ]
[ "..w++w..\n..+..+..\n..+..+..\n..w++w..\n", "......\n+w+...\n+.+...\n++w...\n......\n", "....\n.w..\n", "-1\n" ]
In the first sample the required size of the optimal frame equals 4. In the second sample the size of the optimal frame equals 3. In the third sample, the size of the optimal frame is 1. In the fourth sample, the required frame doesn't exist.
[ { "input": "4 8\n..w..w..\n........\n........\n..w..w..", "output": "..w++w..\n..+..+..\n..+..+..\n..w++w.." }, { "input": "2 4\n....\n.w..", "output": "....\n.w.." }, { "input": "2 6\nw..w.w\n...w..", "output": "-1" }, { "input": "9 4\n....\n....\n....\n....\n....\n..w.\n......
46
0
0
34,664
343
Water Tree
[ "data structures", "dfs and similar", "graphs", "trees" ]
null
null
Mad scientist Mike has constructed a rooted tree, which consists of *n* vertices. Each vertex is a reservoir which can be either empty or filled with water. The vertices of the tree are numbered from 1 to *n* with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of ...
The first line of the input contains an integer *n* (1<=≤<=*n*<=≤<=500000) — the number of vertices in the tree. Each of the following *n*<=-<=1 lines contains two space-separated numbers *a**i*, *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*) — the edges of the tree. The next line contains a number *q* (1<=...
For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.
[ "5\n1 2\n5 1\n2 3\n4 2\n12\n1 1\n2 3\n3 1\n3 2\n3 3\n3 4\n1 2\n2 4\n3 1\n3 3\n3 4\n3 5\n" ]
[ "0\n0\n0\n1\n0\n1\n0\n1\n" ]
none
[ { "input": "5\n1 2\n5 1\n2 3\n4 2\n12\n1 1\n2 3\n3 1\n3 2\n3 3\n3 4\n1 2\n2 4\n3 1\n3 3\n3 4\n3 5", "output": "0\n0\n0\n1\n0\n1\n0\n1" }, { "input": "1\n1\n3 1", "output": "0" }, { "input": "2\n1 2\n13\n1 1\n3 1\n3 2\n2 1\n3 1\n3 2\n2 2\n3 1\n3 2\n1 1\n2 2\n3 1\n3 2", "output": "1\n1...
46
0
0
34,682
101
Castle
[ "dp", "greedy", "probabilities", "sortings", "trees" ]
D. Castle
2
256
Gerald is positioned in an old castle which consists of *n* halls connected with *n*<=-<=1 corridors. It is exactly one way to go from any hall to any other one. Thus, the graph is a tree. Initially, at the moment of time 0, Gerald is positioned in hall 1. Besides, some other hall of the castle contains the treasure Ge...
The first line contains the only integer *n* (2<=≤<=*n*<=≤<=105) — the number of halls in the castle. Next *n*<=-<=1 lines each contain three integers. The *i*-th line contains numbers *a**i*, *b**i* and *t**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*, 1<=≤<=*t**i*<=≤<=1000) — the numbers of halls connected wi...
Print the only real number: the sought expectation of time needed to find the treasure. The answer should differ from the right one in no less than 10<=-<=6.
[ "2\n1 2 1\n", "4\n1 3 2\n4 2 1\n3 2 3\n", "5\n1 2 1\n1 3 1\n1 4 1\n1 5 1\n" ]
[ "1.0\n", "4.333333333333334\n", "4.0\n" ]
In the first test the castle only has two halls which means that the treasure is located in the second hall. Gerald will only need one minute to go to the second hall from the first one. In the second test Gerald can only go from the first hall to the third one. He can get from the third room to the first one or to th...
[]
60
0
0
34,780
920
Tanks
[ "dp", "greedy", "implementation" ]
null
null
Petya sometimes has to water his field. To water the field, Petya needs a tank with exactly *V* ml of water. Petya has got *N* tanks, *i*-th of them initially containing *a**i* ml of water. The tanks are really large, any of them can contain any amount of water (no matter how large this amount is). Also Petya has got...
The first line contains 3 integers: *N* (2<=≤<=*N*<=≤<=5000), *K* (1<=≤<=*K*<=≤<=5000), and *V* (0<=≤<=*V*<=≤<=109) — the number of tanks, the maximum volume of water the scoop can contain, and the required amount of water in some tank, respectively. The second line contains *N* integers *a**i* (0<=≤<=*a**i*<=≤<=105),...
If it is impossible to obtain a tank with exactly *V* ml of water, print NO. Otherwise print YES in the first line, and beginning from the second line, print the sequence of operations in the following format: Each line has to contain 3 numbers denoting a compressed operation: "*cnt* *x* *y*" (1<=≤<=*cnt*<=≤<=109,<...
[ "2 3 5\n2 3\n", "2 3 4\n2 3\n", "5 2 0\n1 3 5 7 9\n" ]
[ "YES\n1 2 1\n", "NO\n", "YES\n2 2 1\n3 3 1\n4 4 1\n5 5 1\n" ]
none
[ { "input": "2 3 5\n2 3", "output": "YES\n1 2 1" }, { "input": "2 3 4\n2 3", "output": "NO" }, { "input": "5 2 0\n1 3 5 7 9", "output": "YES\n2 2 1\n3 3 1\n4 4 1\n5 5 1" }, { "input": "5 10 3\n3 4 5 6 7", "output": "YES\n1 3 2\n1 4 2\n1 5 2" }, { "input": "6 4 8\n5...
1,559
209,612,800
3
34,885
163
Substring and Subsequence
[ "dp" ]
null
null
One day Polycarpus got hold of two non-empty strings *s* and *t*, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "*x* *y*" are there, such that *x* is a substring of string *s*, *y* is a subsequence of string *t*, and the content of ...
The input consists of two lines. The first of them contains *s* (1<=≤<=|*s*|<=≤<=5000), and the second one contains *t* (1<=≤<=|*t*|<=≤<=5000). Both strings consist of lowercase Latin letters.
Print a single number — the number of different pairs "*x* *y*" such that *x* is a substring of string *s*, *y* is a subsequence of string *t*, and the content of *x* and *y* is the same. As the answer can be rather large, print it modulo 1000000007 (109<=+<=7).
[ "aa\naa\n", "codeforces\nforceofcode\n" ]
[ "5\n", "60\n" ]
Let's write down all pairs "*x* *y*" that form the answer in the first sample: "*s*[1...1] *t*[1]", "*s*[2...2] *t*[1]", "*s*[1...1] *t*[2]","*s*[2...2] *t*[2]", "*s*[1...2] *t*[1 2]".
[ { "input": "aa\naa", "output": "5" }, { "input": "codeforces\nforceofcode", "output": "60" }, { "input": "coderscontest\ncodeforces", "output": "39" }, { "input": "a\nb", "output": "0" }, { "input": "ab\nbbbba", "output": "5" }, { "input": "abbbccbba\n...
2,000
102,707,200
0
34,921
475
Meta-universe
[ "data structures" ]
null
null
Consider infinite grid of unit cells. Some of those cells are planets. Meta-universe *M*<==<={*p*1,<=*p*2,<=...,<=*p**k*} is a set of planets. Suppose there is an infinite row or column with following two properties: 1) it doesn't contain any planet *p**i* of meta-universe *M* on it; 2) there are planets of *M* locat...
The first line of input contains an integer *n*, (1<=≤<=*n*<=≤<=105), denoting the number of planets in the meta-universe. The next *n* lines each contain integers *x**i* and *y**i*, (<=-<=109<=≤<=*x**i*,<=*y**i*<=≤<=109), denoting the coordinates of the *i*-th planet. All planets are located in different cells.
Print the number of resulting universes.
[ "5\n0 0\n0 2\n2 0\n2 1\n2 2\n", "8\n0 0\n1 0\n0 2\n0 3\n3 0\n3 1\n2 3\n3 3\n" ]
[ "3\n", "1\n" ]
The following figure describes the first test case:
[]
1,404
14,745,600
-1
35,029
0
none
[ "none" ]
null
null
A team of furry rescue rangers was sitting idle in their hollow tree when suddenly they received a signal of distress. In a few moments they were ready, and the dirigible of the rescue chipmunks hit the road. We assume that the action takes place on a Cartesian plane. The headquarters of the rescuers is located at poi...
The first line of the input contains four integers *x*1, *y*1, *x*2, *y*2 (|*x*1|,<=<=|*y*1|,<=<=|*x*2|,<=<=|*y*2|<=≤<=10<=000) — the coordinates of the rescuers' headquarters and the point, where signal of the distress came from, respectively. The second line contains two integers and *t* (0<=&lt;<=*v*,<=*t*<=≤<=10...
Print a single real value — the minimum time the rescuers need to get to point (*x*2,<=*y*2). You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer...
[ "0 0 5 5\n3 2\n-1 -1\n-1 0\n", "0 0 0 1000\n100 1000\n-50 0\n50 0\n" ]
[ "3.729935587093555327\n", "11.547005383792516398\n" ]
none
[ { "input": "0 0 5 5\n3 2\n-1 -1\n-1 0", "output": "3.729935587093555327" }, { "input": "0 0 0 1000\n100 1000\n-50 0\n50 0", "output": "11.547005383792516398" }, { "input": "0 0 0 1000\n100 5\n0 -50\n0 50", "output": "10" }, { "input": "0 1000 0 0\n50 10\n-49 0\n49 0", "ou...
30
0
0
35,070
455
Serega and Fun
[ "data structures" ]
null
null
Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem. You are given an array *a* consisting of *n* positive integers and queries to it. The queries can be of two types: 1. Make a unit cyclic shift to the right on the seg...
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements of the array. The second line contains *n* integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (1<=≤<=*a*[*i*]<=≤<=*n*). The third line contains a single integer *q* (1<=≤<=*q*<=≤<=105) — the number of queries. The next *q* lines contain the querie...
For each query of the 2-nd type print the answer on a single line.
[ "7\n6 6 2 7 4 2 5\n7\n1 3 6\n2 2 4 2\n2 2 4 7\n2 2 2 5\n1 2 6\n1 1 4\n2 1 7 3\n", "8\n8 4 2 2 7 7 8 8\n8\n1 8 8\n2 8 1 7\n1 8 1\n1 7 3\n2 8 8 3\n1 1 4\n1 2 7\n1 4 5\n" ]
[ "2\n1\n0\n0\n", "2\n0\n" ]
none
[ { "input": "7\n6 6 2 7 4 2 5\n7\n1 3 6\n2 2 4 2\n2 2 4 7\n2 2 2 5\n1 2 6\n1 1 4\n2 1 7 3", "output": "2\n1\n0\n0" }, { "input": "8\n8 4 2 2 7 7 8 8\n8\n1 8 8\n2 8 1 7\n1 8 1\n1 7 3\n2 8 8 3\n1 1 4\n1 2 7\n1 4 5", "output": "2\n0" }, { "input": "10\n7 2 3 4 3 2 4 4 9 1\n10\n1 4 5\n1 1 6\n...
46
0
0
35,121
893
Credit Card
[ "data structures", "dp", "greedy", "implementation" ]
null
null
Recenlty Luba got a credit card and started to use it. Let's consider *n* consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of *i*-th day a transaction *a**i* occurs. If *a**i*<=&gt;<=0, then *a**i* bourles are deposited to Luba's account. If *a**i*<=&lt;<=0, then *a**i* bour...
The first line contains two integers *n*, *d* (1<=≤<=*n*<=≤<=105, 1<=≤<=*d*<=≤<=109) —the number of days and the money limitation. The second line contains *n* integer numbers *a*1,<=*a*2,<=... *a**n* (<=-<=104<=≤<=*a**i*<=≤<=104), where *a**i* represents the transaction in *i*-th day.
Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money.
[ "5 10\n-1 5 0 -5 3\n", "3 4\n-10 0 20\n", "5 10\n-5 0 10 -11 0\n" ]
[ "0\n", "-1\n", "2\n" ]
none
[ { "input": "5 10\n-1 5 0 -5 3", "output": "0" }, { "input": "3 4\n-10 0 20", "output": "-1" }, { "input": "5 10\n-5 0 10 -11 0", "output": "2" }, { "input": "5 13756\n-2 -9 -10 0 10", "output": "1" }, { "input": "20 23036\n-1 1 -1 -1 -1 -1 1 -1 -1 0 0 1 1 0 0 1 0 ...
62
0
0
35,135
677
Vanya and Treasure
[ "data structures", "dp", "graphs", "shortest paths" ]
null
null
Vanya is in the palace that can be represented as a grid *n*<=×<=*m*. Each room contains a single chest, an the room located in the *i*-th row and *j*-th columns contains the chest of type *a**ij*. Each chest of type *x*<=≤<=*p*<=-<=1 contains a key that can open any chest of type *x*<=+<=1, and all chests of type 1 ar...
The first line of the input contains three integers *n*, *m* and *p* (1<=≤<=*n*,<=*m*<=≤<=300,<=1<=≤<=*p*<=≤<=*n*·*m*) — the number of rows and columns in the table representing the palace and the number of different types of the chests, respectively. Each of the following *n* lines contains *m* integers *a**ij* (1<=≤...
Print one integer — the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of type *p*.
[ "3 4 3\n2 1 1 1\n1 1 1 1\n2 1 1 3\n", "3 3 9\n1 3 5\n8 9 7\n4 6 2\n", "3 4 12\n1 2 3 4\n8 7 6 5\n9 10 11 12\n" ]
[ "5\n", "22\n", "11\n" ]
none
[]
61
0
0
35,140
0
none
[ "none" ]
null
null
Natalia Romanova is trying to test something on the new gun S.H.I.E.L.D gave her. In order to determine the result of the test, she needs to find the number of answers to a certain equation. The equation is of form: Where represents logical OR and represents logical exclusive OR (XOR), and *v**i*,<=*j* are some bo...
The first line of input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100<=000) — the number of clauses and the number of variables respectively. The next *n* lines contain the formula. The *i*-th of them starts with an integer *k**i* — the number of literals in the *i*-th clause. It is followed by *k**i* non...
Print the answer modulo 1<=000<=000<=007 (109<=+<=7) in one line.
[ "6 7\n2 4 -2\n2 6 3\n2 -7 1\n2 -5 1\n2 3 6\n2 -2 -5\n", "8 10\n1 -5\n2 4 -6\n2 -2 -6\n2 -7 9\n2 10 -1\n2 3 -1\n2 -8 9\n2 5 8\n", "2 3\n2 1 1\n2 -3 3\n" ]
[ "48\n", "544\n", "4\n" ]
The equation in the first sample is: The equation in the second sample is: The equation in the third sample is:
[]
46
0
0
35,150
0
none
[ "none" ]
null
null
A sum of *p* rubles is charged from Arkady's mobile phone account every day in the morning. Among the following *m* days, there are *n* days when Arkady will top up the account: in the day *d**i* he will deposit *t**i* rubles on his mobile phone account. Arkady will always top up the account before the daily payment wi...
The first line contains three integers *n*, *p* and *m* (1<=≤<=*n*<=≤<=100<=000, 1<=≤<=*p*<=≤<=109, 1<=≤<=*m*<=≤<=109, *n*<=≤<=*m*) — the number of days Arkady will top up the account, the amount of the daily payment, and the number of days you should check. The *i*-th of the following *n* lines contains two integers ...
Print the number of days from the 1-st to the *m*-th such that the account will have a negative amount on it after the daily payment.
[ "3 6 7\n2 13\n4 20\n7 9\n", "5 4 100\n10 70\n15 76\n21 12\n30 100\n67 85\n" ]
[ "3\n", "26\n" ]
In the first example the balance will change as following (remember, initially the balance is zero): 1. in the first day 6 rubles will be charged, the balance in the evening will be equal to  - 6; 1. in the second day Arkady will deposit 13 rubles, then 6 rubles will be charged, the balance in the evening will be eq...
[]
61
0
-1
35,182
62
Wormhouse
[ "dfs and similar", "graphs" ]
D. Wormhouse
2
256
Arnie the Worm has finished eating an apple house yet again and decided to move. He made up his mind on the plan, the way the rooms are located and how they are joined by corridors. He numbered all the rooms from 1 to *n*. All the corridors are bidirectional. Arnie wants the new house to look just like the previous on...
The first line contains two integers *n* and *m* (3<=≤<=*n*<=≤<=100,<=3<=≤<=*m*<=≤<=2000). It is the number of rooms and corridors in Arnie's house correspondingly. The next line contains *m*<=+<=1 positive integers that do not exceed *n*. They are the description of Arnie's old path represented as a list of rooms he v...
Print *m*<=+<=1 positive integers that do not exceed *n*. Those numbers are the description of the new path, according to which Arnie should gnaw out his new house. If it is impossible to find new path you should print out No solution. The first number in your answer should be equal to the last one. Also it should be e...
[ "3 3\n1 2 3 1\n", "3 3\n1 3 2 1\n" ]
[ "1 3 2 1 ", "No solution" ]
none
[ { "input": "3 3\n1 2 3 1", "output": "1 3 2 1 " }, { "input": "3 3\n1 3 2 1", "output": "No solution" }, { "input": "4 4\n1 2 4 3 1", "output": "1 3 4 2 1 " }, { "input": "6 7\n3 2 4 1 6 5 1 3", "output": "No solution" }, { "input": "8 12\n4 6 5 1 4 3 1 8 3 7 8 5 ...
92
0
-1
35,202
165
Compatible Numbers
[ "bitmasks", "brute force", "dfs and similar", "dp" ]
null
null
Two integers *x* and *y* are compatible, if the result of their bitwise "AND" equals zero, that is, *a* &amp; *b*<==<=0. For example, numbers 90 (10110102) and 36 (1001002) are compatible, as 10110102 &amp; 1001002<==<=02, and numbers 3 (112) and 6 (1102) are not compatible, as 112 &amp; 1102<==<=102. You are given an...
The first line contains an integer *n* (1<=≤<=*n*<=≤<=106) — the number of elements in the given array. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=4·106) — the elements of the given array. The numbers in the array can coincide.
Print *n* integers *ans**i*. If *a**i* isn't compatible with any other element of the given array *a*1,<=*a*2,<=...,<=*a**n*, then *ans**i* should be equal to -1. Otherwise *ans**i* is any such number, that *a**i* &amp; *ans**i*<==<=0, and also *ans**i* occurs in the array *a*1,<=*a*2,<=...,<=*a**n*.
[ "2\n90 36\n", "4\n3 6 3 6\n", "5\n10 6 9 8 2\n" ]
[ "36 90", "-1 -1 -1 -1", "-1 8 2 2 8" ]
none
[ { "input": "2\n90 36", "output": "36 90" }, { "input": "4\n3 6 3 6", "output": "-1 -1 -1 -1" }, { "input": "5\n10 6 9 8 2", "output": "-1 8 2 2 8" }, { "input": "10\n4 9 8 3 2 6 8 2 9 7", "output": "8 4 4 8 8 8 4 8 4 8" }, { "input": "10\n3 5 18 12 4 20 11 19 15 6...
60
0
0
35,210
156
Suspects
[ "constructive algorithms", "data structures", "implementation" ]
null
null
As Sherlock Holmes was investigating a crime, he identified *n* suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to *n*. After that, he asked each one: "Which one committed the crime?". Suspect number *i*...
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=105,<=0<=≤<=*m*<=≤<=*n*) — the total number of suspects and the number of suspects who told the truth. Next *n* lines contain the suspects' answers. The *i*-th line contains either "+*a**i*" (without the quotes), if the suspect number *i* says that the cri...
Print *n* lines. Line number *i* should contain "Truth" if suspect number *i* has told the truth for sure. Print "Lie" if the suspect number *i* lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime.
[ "1 1\n+1\n", "3 2\n-1\n-2\n-3\n", "4 1\n+2\n-3\n+4\n-1\n" ]
[ "Truth\n", "Not defined\nNot defined\nNot defined\n", "Lie\nNot defined\nLie\nNot defined\n" ]
The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling th...
[ { "input": "1 1\n+1", "output": "Truth" }, { "input": "3 2\n-1\n-2\n-3", "output": "Not defined\nNot defined\nNot defined" }, { "input": "4 1\n+2\n-3\n+4\n-1", "output": "Lie\nNot defined\nLie\nNot defined" }, { "input": "1 0\n-1", "output": "Lie" }, { "input": "2...
2,000
50,483,200
0
35,238
837
Vasya's Function
[ "binary search", "implementation", "math" ]
null
null
Vasya is studying number theory. He has denoted a function *f*(*a*,<=*b*) such that: - *f*(*a*,<=0)<==<=0; - *f*(*a*,<=*b*)<==<=1<=+<=*f*(*a*,<=*b*<=-<=*gcd*(*a*,<=*b*)), where *gcd*(*a*,<=*b*) is the greatest common divisor of *a* and *b*. Vasya has two numbers *x* and *y*, and he wants to calculate *f*(*x*,<=*y*)...
The first line contains two integer numbers *x* and *y* (1<=≤<=*x*,<=*y*<=≤<=1012).
Print *f*(*x*,<=*y*).
[ "3 5\n", "6 3\n" ]
[ "3\n", "1\n" ]
none
[ { "input": "3 5", "output": "3" }, { "input": "6 3", "output": "1" }, { "input": "1000000009 1000000008", "output": "1000000008" }, { "input": "1000000007 1000000006", "output": "1000000006" }, { "input": "2000000018 2000000017", "output": "1000000009" }, ...
46
5,632,000
-1
35,302
117
Very Interesting Game
[ "brute force", "number theory" ]
null
null
In a very ancient country the following game was popular. Two people play the game. Initially first player writes a string *s*1, consisting of exactly nine digits and representing a number that does not exceed *a*. After that second player looks at *s*1 and writes a string *s*2, consisting of exactly nine digits and re...
The first line contains three integers *a*, *b*, *mod* (0<=≤<=*a*,<=*b*<=≤<=109, 1<=≤<=*mod*<=≤<=107).
If the first player wins, print "1" and the lexicographically minimum string *s*1 he has to write to win. If the second player wins, print the single number "2".
[ "1 10 7\n", "4 0 9\n" ]
[ "2\n", "1 000000001\n" ]
The lexical comparison of strings is performed by the &lt; operator in modern programming languages. String *x* is lexicographically less than string *y* if exists such *i* (1 ≤ *i* ≤ 9), that *x*<sub class="lower-index">*i*</sub> &lt; *y*<sub class="lower-index">*i*</sub>, and for any *j* (1 ≤ *j* &lt; *i*) *x*<sub cl...
[ { "input": "1 10 7", "output": "2" }, { "input": "4 0 9", "output": "1 000000001" }, { "input": "10 7 8", "output": "2" }, { "input": "6 4 10", "output": "2" }, { "input": "4 1 4", "output": "2" }, { "input": "4 7 9", "output": "1 000000001" }, ...
184
18,944,000
3
35,305
291
Command Line Arguments
[ "*special", "implementation", "strings" ]
null
null
The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document. In the Pindows operating system a strings ...
The single line contains a non-empty string *s*. String *s* consists of at most 105 characters. Each character is either an uppercase or a lowercase English letter, or a digit, or one of the ".,?!"" signs, or a space. It is guaranteed that the given string is some correct command line string of the OS Pindows. It is g...
In the first line print the first lexeme, in the second line print the second one and so on. To make the output clearer, print the "&lt;" (less) character to the left of your lexemes and the "&gt;" (more) character to the right. Print the lexemes in the order in which they occur in the command. Please, follow the give...
[ "\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"\n", "firstarg second \"\" \n" ]
[ "&lt;RUn.exe O&gt;\n&lt;&gt;\n&lt; 2ne, &gt;\n&lt;two!&gt;\n&lt;.&gt;\n&lt; &gt;\n", "&lt;firstarg&gt;\n&lt;second&gt;\n&lt;&gt;\n" ]
none
[ { "input": "\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"", "output": "<RUn.exe O>\n<>\n< 2ne, >\n<two!>\n<.>\n< >" }, { "input": " firstarg second \"\" ", "output": "<firstarg>\n<second>\n<>" }, { "input": " \" \" ", "output": "< >" }, { "input": " a \" \...
156
2,764,800
3
35,369
33
Wonderful Randomized Sum
[ "greedy" ]
C. Wonderful Randomized Sum
2
256
Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite th...
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — amount of elements in the sequence. The second line contains *n* integers *a**i* (<=-<=104<=≤<=*a**i*<=≤<=104) — the sequence itself.
The first and the only line of the output should contain the answer to the problem.
[ "3\n-1 -2 -3\n", "5\n-4 2 0 5 0\n", "5\n-1 10 -5 10 -2\n" ]
[ "6\n", "11\n", "18\n" ]
none
[ { "input": "3\n-1 -2 -3", "output": "6" }, { "input": "5\n-4 2 0 5 0", "output": "11" }, { "input": "5\n-1 10 -5 10 -2", "output": "18" }, { "input": "1\n-3", "output": "3" }, { "input": "4\n1 4 -5 -2", "output": "12" }, { "input": "7\n-17 6 5 0 1 4 -1...
92
0
0
35,522
720
Cactusophobia
[ "dfs and similar", "flows" ]
null
null
Tree is a connected undirected graph that has no cycles. Edge cactus is a connected undirected graph without loops and parallel edges, such that each edge belongs to at most one cycle. Vasya has an edge cactus, each edge of this graph has some color. Vasya would like to remove the minimal number of edges in such way ...
The first line contains two integers: *n*, *m* (2<=≤<=*n*<=≤<=10<=000) — the number of vertices and the number of edges in Vasya's graph, respectively. The following *m* lines contain three integers each: *u*, *v*, *c* (1<=≤<=*u*,<=*v*<=≤<=*n*, *u*<=≠<=*v*, 1<=≤<=*c*<=≤<=*m*) — the numbers of vertices connected by the...
Output one integer: the maximal number of different colors that the resulting tree can have.
[ "4 4\n1 2 4\n2 3 1\n3 4 2\n4 2 3\n", "7 9\n1 2 1\n2 3 4\n3 1 5\n1 4 5\n4 5 2\n5 1 6\n1 6 4\n6 7 6\n7 1 3\n" ]
[ "3\n", "6\n" ]
none
[ { "input": "4 4\n1 2 4\n2 3 1\n3 4 2\n4 2 3", "output": "3" }, { "input": "7 9\n1 2 1\n2 3 4\n3 1 5\n1 4 5\n4 5 2\n5 1 6\n1 6 4\n6 7 6\n7 1 3", "output": "6" }, { "input": "5 6\n4 1 2\n4 2 3\n5 1 5\n1 3 1\n1 2 1\n5 3 4", "output": "4" }, { "input": "9 10\n8 7 2\n8 2 10\n8 4 2...
46
7,065,600
-1
35,639
436
Om Nom and Spiders
[ "implementation", "math" ]
null
null
Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular *n*<=×<=*m* field. The park has *k* spiders, each spider at t...
The first line contains three integers *n*,<=*m*,<=*k* (2<=≤<=*n*,<=*m*<=≤<=2000; 0<=≤<=*k*<=≤<=*m*(*n*<=-<=1)). Each of the next *n* lines contains *m* characters — the description of the park. The characters in the *i*-th line describe the *i*-th row of the park field. If the character in the line equals ".", that ...
Print *m* integers: the *j*-th integer must show the number of spiders Om Nom will see if he starts his walk from the *j*-th cell of the first row. The cells in any row of the field are numbered from left to right.
[ "3 3 4\n...\nR.L\nR.U\n", "2 2 2\n..\nRL\n", "2 2 2\n..\nLR\n", "3 4 8\n....\nRRLL\nUUUU\n", "2 2 2\n..\nUU\n" ]
[ "0 2 2 ", "1 1 ", "0 0 ", "1 3 3 1 ", "0 0 " ]
Consider the first sample. The notes below show how the spider arrangement changes on the field over time: Character "*" represents a cell that contains two spiders at the same time. - If Om Nom starts from the first cell of the first row, he won't see any spiders. - If he starts from the second cell, he will see t...
[ { "input": "3 3 4\n...\nR.L\nR.U", "output": "0 2 2 " }, { "input": "2 2 2\n..\nRL", "output": "1 1 " }, { "input": "2 2 2\n..\nLR", "output": "0 0 " }, { "input": "3 4 8\n....\nRRLL\nUUUU", "output": "1 3 3 1 " }, { "input": "2 2 2\n..\nUU", "output": "0 0 " ...
3,000
18,944,000
0
35,718
400
Dima and Bacteria
[ "dsu", "graphs", "shortest paths" ]
null
null
Dima took up the biology of bacteria, as a result of his experiments, he invented *k* types of bacteria. Overall, there are *n* bacteria at his laboratory right now, and the number of bacteria of type *i* equals *c**i*. For convenience, we will assume that all the bacteria are numbered from 1 to *n*. The bacteria of ty...
The first line contains three integers *n*,<=*m*,<=*k* (1<=≤<=*n*<=≤<=105; 0<=≤<=*m*<=≤<=105; 1<=≤<=*k*<=≤<=500). The next line contains *k* integers *c*1,<=*c*2,<=...,<=*c**k* (1<=≤<=*c**i*<=≤<=*n*). Each of the next *m* lines contains three integers *u**i*,<=*v**i*,<=*x**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=105; 0<=≤<=*x**i...
If Dima's type-distribution is correct, print string «Yes», and then *k* lines: in the *i*-th line print integers *d*[*i*][1],<=*d*[*i*][2],<=...,<=*d*[*i*][*k*] (*d*[*i*][*i*]<==<=0). If there is no way to move energy from bacteria *i* to bacteria *j* appropriate *d*[*i*][*j*] must equal to -1. If the type-distributio...
[ "4 4 2\n1 3\n2 3 0\n3 4 0\n2 4 1\n2 1 2\n", "3 1 2\n2 1\n1 2 0\n", "3 2 2\n2 1\n1 2 0\n2 3 1\n", "3 0 2\n1 2\n" ]
[ "Yes\n0 2\n2 0\n", "Yes\n0 -1\n-1 0\n", "Yes\n0 1\n1 0\n", "No\n" ]
none
[ { "input": "4 4 2\n1 3\n2 3 0\n3 4 0\n2 4 1\n2 1 2", "output": "Yes\n0 2\n2 0" }, { "input": "3 1 2\n2 1\n1 2 0", "output": "Yes\n0 -1\n-1 0" }, { "input": "3 2 2\n2 1\n1 2 0\n2 3 1", "output": "Yes\n0 1\n1 0" }, { "input": "3 0 2\n1 2", "output": "No" }, { "input...
30
0
0
35,762
339
Xenia and Bit Operations
[ "data structures", "trees" ]
null
null
Xenia the beginner programmer has a sequence *a*, consisting of 2*n* non-negative integers: *a*1,<=*a*2,<=...,<=*a*2*n*. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value *v* for *a*. Namely, it takes several iterations to calculate value *v*. At the ...
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=17,<=1<=≤<=*m*<=≤<=105). The next line contains 2*n* integers *a*1,<=*a*2,<=...,<=*a*2*n* (0<=≤<=*a**i*<=&lt;<=230). Each of the next *m* lines contains queries. The *i*-th line contains integers *p**i*,<=*b**i* (1<=≤<=*p**i*<=≤<=2*n*,<=0<=≤<=*b**i*<=&lt;<...
Print *m* integers — the *i*-th integer denotes value *v* for sequence *a* after the *i*-th query.
[ "2 4\n1 6 3 5\n1 4\n3 4\n1 2\n1 2\n" ]
[ "1\n3\n3\n3\n" ]
For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation
[ { "input": "2 4\n1 6 3 5\n1 4\n3 4\n1 2\n1 2", "output": "1\n3\n3\n3" }, { "input": "1 1\n1 1\n1 1", "output": "1" }, { "input": "1 10\n6 26\n1 11\n1 9\n1 31\n1 10\n2 12\n1 8\n2 10\n2 4\n2 18\n1 31", "output": "27\n27\n31\n26\n14\n12\n10\n12\n26\n31" }, { "input": "1 10\n22 1...
2,000
23,961,600
0
35,831
786
Berzerk
[ "dfs and similar", "dp", "games" ]
null
null
Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer. In this game there are *n* objects numbered from 1 to *n* arranged in a circle (in clockwise order). Object number 1 is a black hole and the...
The first line of input contains a single integer *n* (2<=≤<=*n*<=≤<=7000) — number of objects in game. The second line contains integer *k*1 followed by *k*1 distinct integers *s*1,<=1,<=*s*1,<=2,<=...,<=*s*1,<=*k*1 — Rick's set. The third line contains integer *k*2 followed by *k*2 distinct integers *s*2,<=1,<=*s*2...
In the first line print *n*<=-<=1 words separated by spaces where *i*-th word is "Win" (without quotations) if in the scenario that Rick plays first and monster is initially in object number *i*<=+<=1 he wins, "Lose" if he loses and "Loop" if the game will never end. Similarly, in the second line print *n*<=-<=1 words...
[ "5\n2 3 2\n3 1 2 3\n", "8\n4 6 2 3 4\n2 3 6\n" ]
[ "Lose Win Win Loop\nLoop Win Win Win\n", "Win Win Win Win Win Win Win\nLose Win Lose Lose Win Lose Lose\n" ]
none
[ { "input": "5\n2 3 2\n3 1 2 3", "output": "Lose Win Win Loop\nLoop Win Win Win" }, { "input": "8\n4 6 2 3 4\n2 3 6", "output": "Win Win Win Win Win Win Win\nLose Win Lose Lose Win Lose Lose" }, { "input": "10\n3 4 7 5\n2 8 5", "output": "Win Win Win Win Win Win Win Loop Win\nLose Win...
733
4,300,800
3
35,837
644
Hostname Aliases
[ "*special", "binary search", "data structures", "implementation", "sortings", "strings" ]
null
null
There are some websites that are accessible through several different addresses. For example, for a long time Codeforces was accessible with two hostnames codeforces.com and codeforces.ru. You are given a list of page addresses being queried. For simplicity we consider all addresses to have the form http://&lt;hostnam...
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of page queries. Then follow *n* lines each containing exactly one address. Each address is of the form http://&lt;hostname&gt;[/&lt;path&gt;], where: - &lt;hostname&gt; consists of lowercase English letters and dots, ther...
First print *k* — the number of groups of server names that correspond to one website. You should count only groups of size greater than one. Next *k* lines should contain the description of groups, one group per line. For each group print all server names separated by a single space. You are allowed to print both gro...
[ "10\nhttp://abacaba.ru/test\nhttp://abacaba.ru/\nhttp://abacaba.com\nhttp://abacaba.com/test\nhttp://abacaba.de/\nhttp://abacaba.ru/test\nhttp://abacaba.de/test\nhttp://abacaba.com/\nhttp://abacaba.com/t\nhttp://abacaba.com/test\n", "14\nhttp://c\nhttp://ccc.bbbb/aba..b\nhttp://cba.com\nhttp://a.c/aba..b/a\nhttp:...
[ "1\nhttp://abacaba.de http://abacaba.ru \n", "2\nhttp://cba.com http://ccc.bbbb \nhttp://a.a.a http://a.c http://abc \n" ]
none
[ { "input": "10\nhttp://abacaba.ru/test\nhttp://abacaba.ru/\nhttp://abacaba.com\nhttp://abacaba.com/test\nhttp://abacaba.de/\nhttp://abacaba.ru/test\nhttp://abacaba.de/test\nhttp://abacaba.com/\nhttp://abacaba.com/t\nhttp://abacaba.com/test", "output": "1\nhttp://abacaba.de http://abacaba.ru " }, { "...
5,000
29,184,000
0
36,006
33
Knights
[ "geometry", "graphs", "shortest paths", "sortings" ]
D. Knights
2
256
Berland is facing dark times again. The army of evil lord Van de Mart is going to conquer the whole kingdom. To the council of war called by the Berland's king Valery the Severe came *n* knights. After long discussions it became clear that the kingdom has exactly *n* control points (if the enemy conquers at least one o...
The first input line contains three integers *n*, *m*, *k* (1<=≤<=*n*,<=*m*<=≤<=1000, 0<=≤<=*k*<=≤<=100000). Then follow *n* lines, each containing two integers *Kx**i*, *Ky**i* (<=-<=109<=≤<=*Kx**i*,<=*Ky**i*<=≤<=109) — coordinates of control point with index *i*. Control points can coincide. Each of the following *m...
Output exactly *k* lines, each containing one integer — the answer to the corresponding request.
[ "2 1 1\n0 0\n3 3\n2 0 0\n1 2\n", "2 3 1\n0 0\n4 4\n1 0 0\n2 0 0\n3 0 0\n1 2\n" ]
[ "1\n", "3\n" ]
none
[]
2,000
31,641,600
0
36,013
0
none
[ "none" ]
null
null
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis. Petya decided to compress tables. He is given a table *a* consisting of *n* rows and *...
The first line of the input contains two integers *n* and *m* (, the number of rows and the number of columns of the table respectively. Each of the following *n* rows contain *m* integers *a**i*,<=*j* (1<=≤<=*a**i*,<=*j*<=≤<=109) that are the values in the table.
Output the compressed table in form of *n* lines each containing *m* integers. If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.
[ "2 2\n1 2\n3 4\n", "4 3\n20 10 30\n50 40 30\n50 60 70\n90 80 70\n" ]
[ "1 2\n2 3\n", "2 1 3\n5 4 3\n5 6 7\n9 8 7\n" ]
In the first sample test, despite the fact *a*<sub class="lower-index">1, 2</sub> ≠ *a*<sub class="lower-index">21</sub>, they are not located in the same row or column so they may become equal after the compression.
[]
15
0
0
36,060
452
Three strings
[ "data structures", "dsu", "string suffix structures", "strings" ]
null
null
You are given three strings (*s*1,<=*s*2,<=*s*3). For each integer *l* (1<=≤<=*l*<=≤<=*min*(|*s*1|,<=|*s*2|,<=|*s*3|) you need to find how many triples (*i*1,<=*i*2,<=*i*3) exist such that three strings *s**k*[*i**k*... *i**k*<=+<=*l*<=-<=1] (*k*<==<=1,<=2,<=3) are pairwise equal. Print all found numbers modulo 1000000...
First three lines contain three non-empty input strings. The sum of lengths of all strings is no more than 3·105. All strings consist only of lowercase English letters.
You need to output *min*(|*s*1|,<=|*s*2|,<=|*s*3|) numbers separated by spaces — answers for the problem modulo 1000000007 (109<=+<=7).
[ "abc\nbc\ncbc\n", "abacaba\nabac\nabcd\n" ]
[ "3 1 \n", "11 2 0 0 \n" ]
Consider a string *t* = *t*<sub class="lower-index">1</sub>*t*<sub class="lower-index">2</sub>... *t*<sub class="lower-index">|*t*|</sub>, where *t*<sub class="lower-index">*i*</sub> denotes the *i*-th character of the string, and |*t*| denotes the length of the string. Then *t*[*i*... *j*] (1 ≤ *i* ≤ *j* ≤ |*t*|) rep...
[]
46
0
0
36,121
207
Military Trainings
[]
null
null
The Smart Beaver from ABBYY started cooperating with the Ministry of Defence. Now they train soldiers to move armoured columns. The training involves testing a new type of tanks that can transmit information. To test the new type of tanks, the training has a special exercise, its essence is as follows. Initially, the ...
The first line contains integer *n* — the number of tanks in the column. Each of the next *n* lines contains one integer *a**i* (1<=≤<=*a**i*<=≤<=250000, 1<=≤<=*i*<=≤<=*n*) — the message receiving radii of the tanks in the order from tank 1 to tank *n* (let us remind you that initially the tanks are located in the colu...
Print a single integer — the minimum possible total time of transmitting the messages. 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.
[ "3\n2\n1\n1\n", "5\n2\n2\n2\n2\n2\n" ]
[ "5\n", "10\n" ]
In the first sample the original order of tanks is 1, 2, 3. The first tank sends a message to the second one, then the second tank sends it to the third one — it takes two seconds. The third tank moves to the beginning of the column and the order of tanks now is 3, 1, 2. The third tank sends a message to the first one,...
[]
3,000
0
0
36,129
804
The same permutation
[ "constructive algorithms" ]
null
null
Seyyed and MoJaK are friends of Sajjad. Sajjad likes a permutation. Seyyed wants to change the permutation in a way that Sajjad won't like it. Seyyed thinks more swaps yield more probability to do that, so he makes MoJaK to perform a swap between every pair of positions (*i*,<=*j*), where *i*<=&lt;<=*j*, exactly once. ...
The first line contains single integer *n* (1<=≤<=*n*<=≤<=1000) — the size of the permutation. As the permutation is not important, you can consider *a**i*<==<=*i*, where the permutation is *a*1,<=*a*2,<=...,<=*a**n*.
If it is not possible to swap all pairs of positions so that the permutation stays the same, print "NO", Otherwise print "YES", then print lines: the *i*-th of these lines should contain two integers *a* and *b* (*a*<=&lt;<=*b*) — the positions where the *i*-th swap is performed.
[ "3\n", "1\n" ]
[ "NO\n", "YES\n" ]
none
[ { "input": "3", "output": "NO" }, { "input": "1", "output": "YES" }, { "input": "5", "output": "YES\n3 5\n3 4\n4 5\n1 3\n2 4\n2 3\n1 4\n1 5\n1 2\n2 5" }, { "input": "6", "output": "NO" }, { "input": "7", "output": "NO" }, { "input": "8", "output": ...
1,403
4,505,600
3
36,312
847
Load Testing
[ "greedy" ]
null
null
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last *n* minutes and in the *i*-th minute friends will send *a**i* requests. Polycarp plans to test Fakebook under a special...
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the duration of the load testing. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109), where *a**i* is the number of requests from friends in the *i*-th minute of the load testing.
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
[ "5\n1 4 3 2 5\n", "5\n1 2 2 2 1\n", "7\n10 20 40 50 70 90 30\n" ]
[ "6\n", "1\n", "0\n" ]
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests. In the second example it is enough to make one additional request in the thir...
[ { "input": "5\n1 4 3 2 5", "output": "6" }, { "input": "5\n1 2 2 2 1", "output": "1" }, { "input": "7\n10 20 40 50 70 90 30", "output": "0" }, { "input": "1\n1", "output": "0" }, { "input": "2\n1 15", "output": "0" }, { "input": "4\n36 54 55 9", "o...
31
0
-1
36,326
895
String Mark
[ "combinatorics", "math", "strings" ]
null
null
At the Byteland State University marks are strings of the same length. Mark *x* is considered better than *y* if string *y* is lexicographically smaller than *x*. Recently at the BSU was an important test work on which Vasya recived the mark *a*. It is very hard for the teacher to remember the exact mark of every stud...
First line contains string *a*, second line contains string *b*. Strings *a*,<=*b* consist of lowercase English letters. Their lengths are equal and don't exceed 106. It is guaranteed that *a* is lexicographically smaller than *b*.
Print one integer  — the number of different strings satisfying the condition of the problem modulo 109<=+<=7.
[ "abc\nddd\n", "abcdef\nabcdeg\n", "abacaba\nubuduba\n" ]
[ "5\n", "0\n", "64\n" ]
In first sample from string *abc* can be obtained strings *acb*, *bac*, *bca*, *cab*, *cba*, all of them are larger than *abc*, but smaller than *ddd*. So the answer is 5. In second sample any string obtained from *abcdef* is larger than *abcdeg*. So the answer is 0.
[ { "input": "abc\nddd", "output": "5" }, { "input": "abcdef\nabcdeg", "output": "0" }, { "input": "abacaba\nubuduba", "output": "64" }, { "input": "aac\nbbb", "output": "1" }, { "input": "aaaccc\nbbbbbb", "output": "9" }, { "input": "aaaaaa\nzzzzzz", ...
109
204,800
0
36,385
19
Fairy
[ "dfs and similar", "divide and conquer", "dsu" ]
E. Fairy
1
256
Once upon a time there lived a good fairy A. One day a fine young man B came to her and asked to predict his future. The fairy looked into her magic ball and said that soon the fine young man will meet the most beautiful princess ever and will marry her. Then she drew on a sheet of paper *n* points and joined some of t...
The first input line contains two integer numbers: *n* — amount of the drawn points and *m* — amount of the drawn segments (1<=≤<=*n*<=≤<=104,<=0<=≤<=*m*<=≤<=104). The following *m* lines contain the descriptions of the segments. Each description contains two different space-separated integer numbers *v*, *u* (1<=≤<=*v...
In the first line output number *k* — amount of the segments in the answer. In the second line output *k* space-separated numbers — indexes of these segments in ascending order. Each index should be output only once. Segments are numbered from 1 in the input order.
[ "4 4\n1 2\n1 3\n2 4\n3 4\n", "4 5\n1 2\n2 3\n3 4\n4 1\n1 3\n" ]
[ "4\n1 2 3 4 ", "1\n5 " ]
none
[]
62
0
0
36,443
610
Alphabet Permutations
[ "data structures", "strings" ]
null
null
You are given a string *s* of length *n*, consisting of first *k* lowercase English letters. We define a *c*-repeat of some string *q* as a string, consisting of *c* copies of the string *q*. For example, string "acbacbacbacb" is a 4-repeat of the string "acb". Let's say that string *a* contains string *b* as a subse...
The first line contains three positive integers *n*, *m* and *k* (1<=≤<=*n*<=≤<=200<=000,<=1<=≤<=*m*<=≤<=20000,<=1<=≤<=*k*<=≤<=10) — the length of the string *s*, the number of operations and the size of the alphabet respectively. The second line contains the string *s* itself. Each of the following lines *m* contains...
For each query of the second type the value of function *d*(*p*).
[ "7 4 3\nabacaba\n1 3 5 b\n2 abc\n1 4 4 c\n2 cba\n" ]
[ "6\n5\n" ]
After the first operation the string *s* will be abbbbba. In the second operation the answer is 6-repeat of abc: ABcaBcaBcaBcaBcAbc. After the third operation the string *s* will be abbcbba. In the fourth operation the answer is 5-repeat of cba: cbAcBacBaCBacBA. Uppercase letters means the occurrences of symbols fr...
[ { "input": "7 4 3\nabacaba\n1 3 5 b\n2 abc\n1 4 4 c\n2 cba", "output": "6\n5" }, { "input": "5 5 3\ncbcab\n1 2 4 b\n2 cab\n2 bca\n1 2 3 a\n1 2 2 c", "output": "4\n5" }, { "input": "10 10 3\ncababcacaa\n1 3 3 b\n2 acb\n2 abc\n1 8 10 c\n1 7 9 c\n1 4 7 b\n2 cba\n1 5 6 a\n1 9 9 c\n2 acb", ...
77
307,200
-1
36,491
691
Swaps in Permutation
[ "dfs and similar", "dsu", "math" ]
null
null
You are given a permutation of the numbers 1,<=2,<=...,<=*n* and *m* pairs of positions (*a**j*,<=*b**j*). At each step you can choose a pair from the given positions and swap the numbers in that positions. What is the lexicographically maximal permutation one can get? Let *p* and *q* be two permutations of the numbe...
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=106) — the length of the permutation *p* and the number of pairs of positions. The second line contains *n* distinct integers *p**i* (1<=≤<=*p**i*<=≤<=*n*) — the elements of the permutation *p*. Each of the last *m* lines contains two integers (*a*...
Print the only line with *n* distinct integers *p*'*i* (1<=≤<=*p*'*i*<=≤<=*n*) — the lexicographically maximal permutation one can get.
[ "9 6\n1 2 3 4 5 6 7 8 9\n1 4\n4 7\n2 5\n5 8\n3 6\n6 9\n" ]
[ "7 8 9 4 5 6 1 2 3\n" ]
none
[ { "input": "9 6\n1 2 3 4 5 6 7 8 9\n1 4\n4 7\n2 5\n5 8\n3 6\n6 9", "output": "7 8 9 4 5 6 1 2 3" }, { "input": "1 1\n1\n1 1", "output": "1" }, { "input": "2 10\n2 1\n2 1\n1 2\n1 1\n2 1\n1 1\n2 1\n1 1\n1 1\n2 1\n2 1", "output": "2 1" }, { "input": "3 10\n1 2 3\n2 2\n1 1\n2 2\n...
4,087
268,390,400
0
36,508
865
Egg Roulette
[ "bitmasks", "brute force", "divide and conquer", "math", "meet-in-the-middle" ]
null
null
The game of Egg Roulette is played between two players. Initially 2*R* raw eggs and 2*C* cooked eggs are placed randomly into a carton. The shells are left on so there is no way to distinguish a raw egg from a cooked egg. One at a time, a player will select an egg, and then smash the egg on his/her forehead. If the egg...
The first line of input will contain integers *R* and *C* (1<=≤<=*R*,<=*C*<=≤<=20,<=*R*<=+<=*C*<=≤<=30). The second line of input will contain the string *S* of length 2(*R*<=+<=*C*) consisting only of characters 'A', 'B', '?'.
Print the number of valid orderings that minimize unfairness and match *S*.
[ "1 1\n??BB\n", "2 4\n?BA??B??A???\n", "4 14\n????A??BB?????????????AB????????????\n" ]
[ "0\n", "1\n", "314\n" ]
In the first test case, the minimum unfairness is 0, and the orderings that achieve it are "ABBA" and "BAAB", neither of which match *S*. Note that an ordering such as "ABBB" would also have an unfairness of 0, but is invalid because it does not contain the same number of 'A's as 'B's. In the second example, the only ...
[]
30
0
0
36,536
928
Dependency management
[ "*special", "graphs", "implementation" ]
null
null
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity. A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with lengt...
The first line contains an only integer *n* (1<=≤<=*n*<=≤<=1<=000) — the number of projects in Vaja. The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to *n*<=-<=...
Output all Polycarp's project's dependencies in lexicographical order.
[ "4\na 3\n2\nb 1\nc 1\n \nb 2\n0\n \nb 1\n1\nb 2\n \nc 1\n1\nb 2\n", "9\ncodehorses 5\n3\nwebfrmk 6\nmashadb 1\nmashadb 2\n \ncommons 2\n0\n \nmashadb 3\n0\n \nwebfrmk 6\n2\nmashadb 3\ncommons 2\n \nextra 4\n1\nextra 3\n \nextra 3\n0\n \nextra 1\n0\n \nmashadb 1\n1\nextra 3\n \nmashadb 2\n1\nextra 1\n", "3\nabc ...
[ "2\nb 1\nc 1\n", "4\ncommons 2\nextra 1\nmashadb 2\nwebfrmk 6\n", "1\ncba 2\n" ]
The first sample is given in the pic below. Arrow from *A* to *B* means that *B* directly depends on *A*. Projects that Polycarp's project «a» (version 3) depends on are painted black. The second sample is again given in the pic below. Arrow from *A* to *B* means that *B* directly depends on *A*. Projects that Polycar...
[ { "input": "4\na 3\n2\nb 1\nc 1\n\nb 2\n0\n\nb 1\n1\nb 2\n\nc 1\n1\nb 2", "output": "2\nb 1\nc 1" }, { "input": "9\ncodehorses 5\n3\nwebfrmk 6\nmashadb 1\nmashadb 2\n\ncommons 2\n0\n\nmashadb 3\n0\n\nwebfrmk 6\n2\nmashadb 3\ncommons 2\n\nextra 4\n1\nextra 3\n\nextra 3\n0\n\nextra 1\n0\n\nmashadb 1\n...
30
5,632,000
0
36,556
137
Last Chance
[ "data structures", "implementation", "strings" ]
null
null
Having read half of the book called "Storm and Calm" on the IT lesson, Innocentius was absolutely determined to finish the book on the maths lessons. All was fine until the math teacher Ms. Watkins saw Innocentius reading fiction books instead of solving equations of the fifth degree. As during the last maths class Inn...
The only input line contains a non-empty string *s* consisting of no more than 2·105 uppercase and lowercase Latin letters. We shall regard letters "a", "e", "i", "o", "u" and their uppercase variants as vowels.
Print on a single line two numbers without a space: the maximum length of a good substring and the number of good substrings with this length. If no good substring exists, print "No solution" without the quotes. Two substrings are considered different if their positions of occurrence are different. So if some string o...
[ "Abo\n", "OEIS\n", "auBAAbeelii\n", "AaaBRAaaCAaaDAaaBRAaa\n", "EA\n" ]
[ "3 1\n", "3 1\n", "9 3\n", "18 4\n", "No solution\n" ]
In the first sample there is only one longest good substring: "Abo" itself. The other good substrings are "b", "Ab", "bo", but these substrings have shorter length. In the second sample there is only one longest good substring: "EIS". The other good substrings are: "S", "IS".
[ { "input": "Abo", "output": "3 1" }, { "input": "OEIS", "output": "3 1" }, { "input": "auBAAbeelii", "output": "9 3" }, { "input": "AaaBRAaaCAaaDAaaBRAaa", "output": "18 4" }, { "input": "EA", "output": "No solution" }, { "input": "BBBAABAABAABBBB", ...
2,000
8,089,600
0
36,592
1,005
Median on Segments (General Case Edition)
[ "sortings" ]
null
null
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after s...
The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) — the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$).
Print the required number.
[ "5 4\n1 4 5 60 4\n", "3 1\n1 1 1\n", "15 2\n1 2 3 1 2 3 1 2 3 1 2 3 1 2 3\n" ]
[ "8\n", "6\n", "97\n" ]
In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
[ { "input": "5 4\n1 4 5 60 4", "output": "8" }, { "input": "3 1\n1 1 1", "output": "6" }, { "input": "15 2\n1 2 3 1 2 3 1 2 3 1 2 3 1 2 3", "output": "97" }, { "input": "1 1\n1", "output": "1" }, { "input": "2 1\n1 2", "output": "2" }, { "input": "2 1\n...
46
7,168,000
0
36,596
809
Find a car
[ "combinatorics", "divide and conquer", "dp" ]
null
null
After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help. Form...
The first line contains one integer *q* (1<=≤<=*q*<=≤<=104) — the number of Leha's requests. The next *q* lines contain five integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=*k* (1<=≤<=*x*1<=≤<=*x*2<=≤<=109,<=1<=≤<=*y*1<=≤<=*y*2<=≤<=109,<=1<=≤<=*k*<=≤<=2·109) — parameters of Leha's requests.
Print exactly *q* lines — in the first line print the answer to the first request, in the second — the answer to the second request and so on.
[ "4\n1 1 1 1 1\n3 2 5 4 5\n1 1 5 5 10000\n1 4 2 5 2\n" ]
[ "1\n13\n93\n0\n" ]
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue. In the first request (*k* = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1. <img class="tex-graphics" src="https://espresso.codeforces.com/76839e22308c8...
[ { "input": "4\n1 1 1 1 1\n3 2 5 4 5\n1 1 5 5 10000\n1 4 2 5 2", "output": "1\n13\n93\n0" }, { "input": "10\n3 7 4 10 7\n6 1 7 10 18\n9 6 10 8 3\n1 8 3 10 3\n10 4 10 5 19\n8 9 9 10 10\n10 1 10 5 4\n8 1 9 4 18\n6 3 9 5 1\n6 6 9 6 16", "output": "22\n130\n0\n0\n25\n3\n0\n68\n0\n22" }, { "in...
1,044
8,908,800
3
36,600
272
Dima and Horses
[ "combinatorics", "constructive algorithms", "graphs" ]
null
null
Dima came to the horse land. There are *n* horses living in the land. Each horse in the horse land has several enemies (enmity is a symmetric relationship). The horse land isn't very hostile, so the number of enemies of each horse is at most 3. Right now the horse land is going through an election campaign. So the hor...
The first line contains two integers *n*,<=*m* — the number of horses in the horse land and the number of enemy pairs. Next *m* lines define the enemy pairs. The *i*-th line contains integers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*), which mean that horse *a**i* is the enemy of horse *b**i*....
Print a line, consisting of *n* characters: the *i*-th character of the line must equal "0", if the horse number *i* needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1.
[ "3 3\n1 2\n3 2\n3 1\n", "2 1\n2 1\n", "10 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n" ]
[ "100\n", "00\n", "0110000000\n" ]
none
[]
92
0
0
36,665
676
Theseus and labyrinth
[ "graphs", "implementation", "shortest paths" ]
null
null
Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size *n*<=×<=*m* and consists of blocks of size 1<=×<=1. Each block of the labyrinth has a button that rotates all blocks 90 degrees clockwise. Each block rotates around its center and doesn't change its...
The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — the number of rows and the number of columns in labyrinth, respectively. Each of the following *n* lines contains *m* characters, describing the blocks of the labyrinth. The possible characters are: - «+» means this block has ...
If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the block where Minotaur is hiding.
[ "2 2\n+*\n*U\n1 1\n2 2\n", "2 3\n&lt;&gt;&lt;\n&gt;&lt;&gt;\n1 1\n2 1\n" ]
[ "-1", "4" ]
Assume that Theseus starts at the block (*x*<sub class="lower-index">*T*</sub>, *y*<sub class="lower-index">*T*</sub>) at the moment 0.
[ { "input": "2 2\n+*\n*U\n1 1\n2 2", "output": "-1" }, { "input": "2 3\n<><\n><>\n1 1\n2 1", "output": "4" }, { "input": "3 3\n->v\n*+|\n+*^\n3 3\n1 1", "output": "6" }, { "input": "10 10\n><URRD>>+-\n>+vLLDL-v*\n*+R^v+UUR<\n<DDU>R||RR\nRL*v^UvD|R\nR>U<>DRv|R\n-D^+U^-|UD\nD^>L...
108
409,600
0
36,701
519
A and B and Lecture Rooms
[ "binary search", "data structures", "dfs and similar", "dp", "trees" ]
null
null
A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected by corridors. Overall, the University has *n* rooms connected by *n*<=-<=1 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 ...
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of rooms in the University. The next *n*<=-<=1 lines describe the corridors. The *i*-th of these lines (1<=≤<=*i*<=≤<=*n*<=-<=1) contains two integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*), showing that the *i*-th corridor connects rooms ...
In the *i*-th (1<=≤<=*i*<=≤<=*m*) line print the number of rooms that are equidistant from the rooms where A and B write contest on the *i*-th day.
[ "4\n1 2\n1 3\n2 4\n1\n2 3\n", "4\n1 2\n2 3\n2 4\n2\n1 2\n1 3\n" ]
[ "1\n", "0\n2\n" ]
in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.
[ { "input": "4\n1 2\n1 3\n2 4\n1\n2 3", "output": "1" }, { "input": "4\n1 2\n2 3\n2 4\n2\n1 2\n1 3", "output": "0\n2" }, { "input": "15\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n5 8\n6 9\n9 14\n14 15\n7 10\n4 13\n3 11\n3 12\n6\n10 15\n13 12\n2 15\n8 4\n15 12\n6 13", "output": "1\n10\n1\n7\n0\n4"...
108
20,377,600
0
36,729
0
none
[ "none" ]
null
null
Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has *n* junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads c...
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=1000,<=0<=≤<=*m*<=≤<=1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to *n*, inclusive. The next line contains two integers *x* and *y* (1<=≤<=*x*,<=*y*<=≤<=*n*). They are the numbers of the ...
If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
[ "4 4\n1 3\n1 2 3\n1 4 1\n2 4 1\n2 3 5\n2 7\n7 2\n1 2\n7 7\n" ]
[ "9\n" ]
An optimal way — ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.
[]
30
0
-1
36,762
621
Rat Kwesh and Cheese
[ "brute force", "constructive algorithms", "math" ]
null
null
Wet Shark asked Rat Kwesh to generate three positive real numbers *x*, *y* and *z*, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point. Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportuni...
The only line of the input contains three space-separated real numbers *x*, *y* and *z* (0.1<=≤<=*x*,<=*y*,<=*z*<=≤<=200.0). Each of *x*, *y* and *z* is given with exactly one digit after the decimal point.
Find the maximum value of expression among *x**y**z*, *x**z**y*, (*x**y*)*z*, (*x**z*)*y*, *y**x**z*, *y**z**x*, (*y**x*)*z*, (*y**z*)*x*, *z**x**y*, *z**y**x*, (*z**x*)*y*, (*z**y*)*x* and print the corresponding expression. If there are many maximums, print the one that comes first in the list. *x**y**z* should be ...
[ "1.1 3.4 2.5\n", "2.0 2.0 2.0\n", "1.9 1.8 1.7\n" ]
[ "z^y^x\n", "x^y^z\n", "(x^y)^z\n" ]
none
[ { "input": "1.1 3.4 2.5", "output": "z^y^x" }, { "input": "2.0 2.0 2.0", "output": "x^y^z" }, { "input": "1.9 1.8 1.7", "output": "(x^y)^z" }, { "input": "2.0 2.1 2.2", "output": "x^z^y" }, { "input": "1.5 1.7 2.5", "output": "(z^x)^y" }, { "input": "1...
77
0
0
36,772
689
Friends and Subsequences
[ "binary search", "data structures" ]
null
null
Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows? Every one of them has an integer sequences *a* and *b* of length *n*. Being given a query of the form of pair of integers...
The first line contains only integer *n* (1<=≤<=*n*<=≤<=200<=000). The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109) — the sequence *a*. The third line contains *n* integer numbers *b*1,<=*b*2,<=...,<=*b**n* (<=-<=109<=≤<=*b**i*<=≤<=109) — the sequence *b*.
Print the only integer number — the number of occasions the robot will count, thus for how many pairs is satisfied.
[ "6\n1 2 3 2 1 4\n6 7 1 2 3 2\n", "3\n3 3 3\n1 1 1\n" ]
[ "2\n", "0\n" ]
The occasions in the first sample case are: 1.*l* = 4,*r* = 4 since *max*{2} = *min*{2}. 2.*l* = 4,*r* = 5 since *max*{2, 1} = *min*{2, 3}. There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.
[ { "input": "6\n1 2 3 2 1 4\n6 7 1 2 3 2", "output": "2" }, { "input": "3\n3 3 3\n1 1 1", "output": "0" }, { "input": "17\n714413739 -959271262 714413739 -745891378 926207665 -404845105 -404845105 -959271262 -189641729 -670860364 714413739 -189641729 192457837 -745891378 -670860364 536388...
62
2,969,600
0
36,996
856
Satellites
[]
null
null
Real Cosmic Communications is the largest telecommunication company on a far far away planet, located at the very edge of the universe. RCC launches communication satellites. The planet is at the very edge of the universe, so its form is half of a circle. Its radius is *r*, the ends of its diameter are points *A* and ...
The first line of input data contains integers *r* and *n* — radius of the planet and the number of events (1<=≤<=*r*<=≤<=109, 1<=≤<=*n*<=≤<=5·105). Each of the following *n* lines describe events in the specified format. Satellite coordinates are integer, the satisfy the following constraints |*x*|<=≤<=109, 0<=&lt;<...
For each event of type 3 print «YES» on a separate line, if it is possible to create a communication channel, or «NO» if it is impossible.
[ "5 8\n1 -5 8\n1 -4 8\n1 -3 8\n1 2 7\n3 1 3\n2 2\n3 1 3\n3 3 4\n" ]
[ "NO\nYES\nYES\n" ]
none
[]
46
0
0
37,077
0
none
[ "none" ]
null
null
Vasya's telephone contains *n* photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo *n*. Similarly, by swiping right from the last photo you reach photo 1. It takes *...
The first line of the input contains 4 integers *n*,<=*a*,<=*b*,<=*T* (1<=≤<=*n*<=≤<=5·105, 1<=≤<=*a*,<=*b*<=≤<=1000, 1<=≤<=*T*<=≤<=109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a ...
Output the only integer, the maximum number of photos Vasya is able to watch during those *T* seconds.
[ "4 2 3 10\nwwhw\n", "5 2 4 13\nhhwhh\n", "5 2 4 1000\nhhwhh\n", "3 1 100 10\nwhw\n" ]
[ "2\n", "4\n", "5\n", "0\n" ]
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first phot...
[ { "input": "4 2 3 10\nwwhw", "output": "2" }, { "input": "5 2 4 13\nhhwhh", "output": "4" }, { "input": "5 2 4 1000\nhhwhh", "output": "5" }, { "input": "3 1 100 10\nwhw", "output": "0" }, { "input": "10 2 3 32\nhhwwhwhwwh", "output": "7" }, { "input":...
124
0
0
37,120
852
Neural Network country
[ "dp", "matrices" ]
null
null
Due to the recent popularity of the Deep learning new countries are starting to look like Neural Networks. That is, the countries are being built deep with many layers, each layer possibly having many cities. They also have one entry, and one exit point. There are exactly *L* layers, each having *N* cities. Let us loo...
The first line of input contains *N* (1<=≤<=*N*<=≤<=106), *L* (2<=≤<=*L*<=≤<=105) and *M* (2<=≤<=*M*<=≤<=100), the number of cities in each layer, the number of layers and the number that travelling cost should be divisible by, respectively. Second, third and fourth line contain *N* integers each denoting costs 0<=≤<=...
Output a single integer, the number of paths Doctor G. can take which have total cost divisible by *M*, modulo 109<=+<=7.
[ "2 3 13\n4 6\n2 1\n3 4\n" ]
[ "2" ]
<img class="tex-graphics" src="https://espresso.codeforces.com/959c8bea1eef9daad659ecab34d36a2f692c5e88.png" style="max-width: 100.0%;max-height: 100.0%;"/> This is a country with 3 layers, each layer having 2 cities. Paths <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ea33f7ca0560180dc0...
[ { "input": "2 3 13\n4 6\n2 1\n3 4", "output": "2" }, { "input": "2 4 5\n1 1\n1 1\n1 1", "output": "16" }, { "input": "1 1234 5\n1\n1\n1", "output": "1" }, { "input": "3 2 2\n0 1 0\n0 0 1\n1 1 0", "output": "3" }, { "input": "5 4 3\n2 1 0 1 2\n0 1 2 1 0\n1 2 1 0 2"...
62
0
0
37,429
484
Strange Sorting
[ "implementation", "math" ]
null
null
How many specific orders do you know? Ascending order, descending order, order of ascending length, order of ascending polar angle... Let's have a look at another specific order: *d*-sorting. This sorting is applied to the strings of length at least *d*, where *d* is some positive integer. The characters of the string ...
The first line of the input contains a non-empty string *S* of length *n*, consisting of lowercase and uppercase English letters and digits from 0 to 9. The second line of the input contains integer *m* – the number of shuffling operations (1<=≤<=*m*·*n*<=≤<=106). Following *m* lines contain the descriptions of the...
After each operation print the current state of string *S*.
[ "qwerty\n3\n4 2\n6 3\n5 2\n" ]
[ "qertwy\nqtewry\nqetyrw\n" ]
Here is detailed explanation of the sample. The first modification is executed with arguments *k* = 4, *d* = 2. That means that you need to apply 2-sorting for each substring of length 4 one by one moving from the left to the right. The string will transform in the following manner: qwerty  →  qewrty  →  qerwty  →  qe...
[]
2,000
0
0
37,469
894
Ralph and Mushrooms
[ "dp", "graphs" ]
null
null
Ralph is going to collect mushrooms in the Mushroom Forest. There are *m* directed paths connecting *n* trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantas...
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=106, 0<=≤<=*m*<=≤<=106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively. Each of the following *m* lines contains three integers *x*, *y* and *w* (1<=≤<=*x*,<=*y*<=≤<=*n*, 0<=≤<=*w*<=≤<=108), denotin...
Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.
[ "2 2\n1 2 4\n2 1 4\n1\n", "3 3\n1 2 4\n2 3 3\n1 3 8\n1\n" ]
[ "16", "8" ]
In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect. In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.
[ { "input": "2 2\n1 2 4\n2 1 4\n1", "output": "16" }, { "input": "3 3\n1 2 4\n2 3 3\n1 3 8\n1", "output": "8" }, { "input": "1 0\n1", "output": "0" } ]
62
4,608,000
0
37,487