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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
67 | Restoration of the Permutation | [
"greedy"
] | B. Restoration of the Permutation | 1 | 256 | Let *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} be any permutation of the first *n* natural numbers {1,<=2,<=...,<=*n*}. You are given a positive integer *k* and another sequence *B*<==<={*b*1,<=*b*2,<=...,<=*b**n*}, where *b**i* is the number of elements *a**j* in *A* to the left of the element *a**t*<==<=*i* such that *a**j*<=β₯<=(*i*<=+<=*k*).
For example, if *n*<==<=5, a possible *A* is {5,<=1,<=4,<=2,<=3}. For *k*<==<=2, *B* is given by {1,<=2,<=1,<=0,<=0}. But if *k*<==<=3, then *B*<==<={1,<=1,<=0,<=0,<=0}.
For two sequences *X*<==<={*x*1,<=*x*2,<=...,<=*x**n*} and *Y*<==<={*y*1,<=*y*2,<=...,<=*y**n*}, let *i*-th elements be the first elements such that *x**i*<=β <=*y**i*. If *x**i*<=<<=*y**i*, then *X* is lexicographically smaller than *Y*, while if *x**i*<=><=*y**i*, then *X* is lexicographically greater than *Y*.
Given *n*, *k* and *B*, you need to determine the lexicographically smallest *A*. | The first line contains two space separated integers *n* and *k* (1<=β€<=*n*<=β€<=1000, 1<=β€<=*k*<=β€<=*n*). On the second line are *n* integers specifying the values of *B*<==<={*b*1,<=*b*2,<=...,<=*b**n*}. | Print on a single line *n* integers of *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} such that *A* is lexicographically minimal. It is guaranteed that the solution exists. | [
"5 2\n1 2 1 0 0\n",
"4 2\n1 0 0 0\n"
] | [
"4 1 5 2 3 ",
"2 3 1 4 "
] | none | [
{
"input": "5 2\n1 2 1 0 0",
"output": "4 1 5 2 3 "
},
{
"input": "4 2\n1 0 0 0",
"output": "2 3 1 4 "
},
{
"input": "10 3\n4 2 4 2 1 0 1 0 0 0",
"output": "6 8 2 5 9 1 4 10 3 7 "
},
{
"input": "15 3\n4 2 7 5 1 1 1 0 0 0 0 0 0 0 0",
"output": "8 5 2 9 6 1 10 7 11 4 12 3 1... | 590 | 409,600 | 3.704237 | 57,876 |
612 | Simba on the Circle | [
"dp"
] | null | null | You are given a circular array with *n* elements. The elements are numbered from some element with values from 1 to *n* in clockwise order. The *i*-th cell contains the value *a**i*. The robot Simba is in cell *s*.
Each moment of time the robot is in some of the *n* cells (at the begin he is in *s*). In one turn the robot can write out the number written in current cell or move to the adjacent cell in clockwise or counterclockwise direction. To write out the number from the cell Simba doesn't spend any time, but to move to adjacent cell Simba spends one unit of time.
Simba wants to write the number from each cell one time, so the numbers will be written in a non decreasing order. Find the least number of time units to write out all numbers. | The first line contains two integers *n* and *s* (1<=β€<=*s*<=β€<=*n*<=β€<=2000) β the number of cells in the circular array and the starting position of Simba.
The second line contains *n* integers *a**i* (<=-<=109<=β€<=*a**i*<=β€<=109) β the number written in the *i*-th cell. The numbers are given for cells in order from 1 to *n*. Some of numbers *a**i* can be equal. | In the first line print the number *t* β the least number of time units.
Each of the next *n* lines should contain the direction of robot movement and the number of cells to move in that direction. After that movement the robot writes out the number from the cell in which it turns out. The direction and the number of cells should be printed in the form of +x in case of clockwise movement and -x in case of counterclockwise movement to *x* cells (0<=β€<=*x*<=β€<=*n*<=-<=1).
Note that the sum of absolute values of *x* should be equal to *t*. | [
"9 1\n0 1 2 2 2 1 0 1 1\n",
"8 1\n0 1 0 1 0 1 0 1\n",
"8 1\n1 2 3 4 5 6 7 8\n",
"8 1\n0 0 0 0 0 0 0 0\n"
] | [
"12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1\n",
"13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2\n",
"7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1\n",
"7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1\n"
] | none | [
{
"input": "9 1\n0 1 2 2 2 1 0 1 1",
"output": "12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1"
},
{
"input": "8 1\n0 1 0 1 0 1 0 1",
"output": "13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2"
},
{
"input": "8 1\n1 2 3 4 5 6 7 8",
"output": "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1"
},
{
"input": "8 1\... | 61 | 409,600 | 0 | 58,003 | |
794 | Labelling Cities | [
"dfs and similar",
"graphs",
"hashing"
] | null | null | Oleg the bank client lives in Bankopolia. There are *n* cities in Bankopolia and some pair of cities are connected directly by bi-directional roads. The cities are numbered from 1 to *n*. There are a total of *m* roads in Bankopolia, the *i*-th road connects cities *u**i* and *v**i*. It is guaranteed that from each city it is possible to travel to any other city using some of the roads.
Oleg wants to give a label to each city. Suppose the label of city *i* is equal to *x**i*. Then, it must hold that for all pairs of cities (*u*,<=*v*) the condition |*x**u*<=-<=*x**v*|<=β€<=1 holds if and only if there is a road connecting *u* and *v*.
Oleg wonders if such a labeling is possible. Find an example of such labeling if the task is possible and state that it is impossible otherwise. | The first line of input contains two space-separated integers *n* and *m* (2<=β€<=*n*<=β€<=3Β·105, 1<=β€<=*m*<=β€<=3Β·105)Β β the number of cities and the number of roads.
Next, *m* lines follow. The *i*-th line contains two space-separated integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*)Β β the cities connected by the *i*-th road. It is guaranteed that there is at most one road between each pair of cities and it is possible to travel from any city to any other city using some roads. | If the required labeling is not possible, output a single line containing the string "NO" (without quotes).
Otherwise, output the string "YES" (without quotes) on the first line. On the next line, output *n* space-separated integers, *x*1,<=*x*2,<=...,<=*x**n*. The condition 1<=β€<=*x**i*<=β€<=109 must hold for all *i*, and for all pairs of cities (*u*,<=*v*) the condition |*x**u*<=-<=*x**v*|<=β€<=1 must hold if and only if there is a road connecting *u* and *v*. | [
"4 4\n1 2\n1 3\n1 4\n3 4\n",
"5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n5 4\n",
"4 3\n1 2\n1 3\n1 4\n"
] | [
"YES\n2 3 1 1 \n",
"YES\n1 1 1 1 1 \n",
"NO\n"
] | For the first sample, *x*<sub class="lower-index">1</sub>β=β2, *x*<sub class="lower-index">2</sub>β=β3, *x*<sub class="lower-index">3</sub>β=β*x*<sub class="lower-index">4</sub>β=β1 is a valid labeling. Indeed, (3,β4), (1,β2), (1,β3), (1,β4) are the only pairs of cities with difference of labels not greater than 1, and these are precisely the roads of Bankopolia.
For the second sample, all pairs of cities have difference of labels not greater than 1 and all pairs of cities have a road connecting them.
For the last sample, it is impossible to construct a labeling satisfying the given constraints. | [
{
"input": "4 4\n1 2\n1 3\n1 4\n3 4",
"output": "YES\n2 3 1 1 "
},
{
"input": "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n5 4",
"output": "YES\n1 1 1 1 1 "
},
{
"input": "4 3\n1 2\n1 3\n1 4",
"output": "NO"
},
{
"input": "8 12\n1 2\n1 3\n1 4\n2 3\n2 4\n4 5\n5 6\n5 7\n... | 1,621 | 98,201,600 | 0 | 58,053 | |
155 | Combination | [
"greedy",
"sortings"
] | null | null | Ilya plays a card game by the following rules.
A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number *a**i*, and the bottom contains number *b**i*, then when the player is playing the card, he gets *a**i* points and also gets the opportunity to play additional *b**i* cards. After the playing the card is discarded.
More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number *b**i*, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.
Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards? | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=1000) β the number of cards Ilya has.
Each of the next *n* lines contains two non-negative space-separated integers β *a**i* and *b**i* (0<=β€<=*a**i*,<=*b**i*<=β€<=104) β the numbers, written at the top and the bottom of the *i*-th card correspondingly. | Print the single number β the maximum number of points you can score in one round by the described rules. | [
"2\n1 0\n2 0\n",
"3\n1 0\n2 0\n0 2\n"
] | [
"2\n",
"3\n"
] | In the first sample none of two cards brings extra moves, so you should play the one that will bring more points.
In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards. | [
{
"input": "2\n1 0\n2 0",
"output": "2"
},
{
"input": "3\n1 0\n2 0\n0 2",
"output": "3"
},
{
"input": "5\n0 0\n2 0\n2 0\n3 0\n5 1",
"output": "8"
},
{
"input": "7\n9 1\n8 1\n9 0\n9 1\n5 1\n1 1\n0 1",
"output": "41"
},
{
"input": "7\n5 0\n4 0\n3 0\n5 2\n3 0\n4 2\n0... | 92 | 0 | 0 | 58,088 | |
575 | Bribes | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Ruritania is a country with a very badly maintained road network, which is not exactly good news for lorry drivers that constantly have to do deliveries. In fact, when roads are maintained, they become one-way. It turns out that it is sometimes impossible to get from one town to another in a legal way β however, we know that all towns are reachable, though illegally!
Fortunately for us, the police tend to be very corrupt and they will allow a lorry driver to break the rules and drive in the wrong direction provided they receive βa small giftβ. There is one patrol car for every road and they will request 1000 Ruritanian dinars when a driver drives in the wrong direction. However, being greedy, every time a patrol car notices the same driver breaking the rule, they will charge double the amount of money they requested the previous time on that particular road.
Borna is a lorry driver that managed to figure out this bribing pattern. As part of his job, he has to make *K* stops in some towns all over Ruritania and he has to make these stops in a certain order. There are *N* towns (enumerated from 1 to *N*) in Ruritania and Bornaβs initial location is the capital city i.e. town 1. He happens to know which ones out of the *N*<=-<=1 roads in Ruritania are currently unidirectional, but he is unable to compute the least amount of money he needs to prepare for bribing the police. Help Borna by providing him with an answer and you will be richly rewarded. | The first line contains *N*, the number of towns in Ruritania. The following *N*<=-<=1 lines contain information regarding individual roads between towns. A road is represented by a tuple of integers (*a*,*b*,*x*), which are separated with a single whitespace character. The numbers *a* and *b* represent the cities connected by this particular road, and x is either 0 or 1: 0 means that the road is bidirectional, 1 means that only the *a*<=β<=*b* direction is legal. The next line contains *K*, the number of stops Borna has to make. The final line of input contains K positive integers *s*1,<=β¦,<=*s**K*: the towns Borna has to visit.
- 1<=β€<=*N*<=β€<=105 - 1<=β€<=*K*<=β€<=106 - 1<=β€<=*a*,<=*b*<=β€<=*N* for all roads - for all roads - 1<=β€<=*s**i*<=β€<=*N* for all 1<=β€<=*i*<=β€<=*K* | The output should contain a single number: the least amount of thousands of Ruritanian dinars Borna should allocate for bribes, modulo 109<=+<=7. | [
"5\n1 2 0\n2 3 0\n5 1 1\n3 4 1\n5\n5 4 5 2 2\n"
] | [
"4\n"
] | Borna first takes the route 1βββ5 and has to pay 1000 dinars. After that, he takes the route 5βββ1βββ2βββ3βββ4 and pays nothing this time. However, when he has to return via 4βββ3βββ2βββ1βββ5, he needs to prepare 3000 (1000+2000) dinars. Afterwards, getting to 2 via 5βββ1βββ2 will cost him nothing. Finally, he doesn't even have to leave town 2 to get to 2, so there is no need to prepare any additional bribe money. Hence he has to prepare 4000 dinars in total. | [] | 30 | 0 | 0 | 58,090 | |
723 | st-Spanning Tree | [
"dsu",
"graphs",
"greedy",
"implementation"
] | null | null | You are given an undirected connected graph consisting of *n* vertices and *m* edges. There are no loops and no multiple edges in the graph.
You are also given two distinct vertices *s* and *t*, and two values *d**s* and *d**t*. Your task is to build any spanning tree of the given graph (note that the graph is not weighted), such that the degree of the vertex *s* doesn't exceed *d**s*, and the degree of the vertex *t* doesn't exceed *d**t*, or determine, that there is no such spanning tree.
The spanning tree of the graph *G* is a subgraph which is a tree and contains all vertices of the graph *G*. In other words, it is a connected graph which contains *n*<=-<=1 edges and can be obtained by removing some of the edges from *G*.
The degree of a vertex is the number of edges incident to this vertex. | The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=200<=000, 1<=β€<=*m*<=β€<=*min*(400<=000,<=*n*Β·(*n*<=-<=1)<=/<=2))Β β the number of vertices and the number of edges in the graph.
The next *m* lines contain the descriptions of the graph's edges. Each of the lines contains two integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*, *u*<=β <=*v*)Β β the ends of the corresponding edge. It is guaranteed that the graph contains no loops and no multiple edges and that it is connected.
The last line contains four integers *s*, *t*, *d**s*, *d**t* (1<=β€<=*s*,<=*t*<=β€<=*n*, *s*<=β <=*t*, 1<=β€<=*d**s*,<=*d**t*<=β€<=*n*<=-<=1). | If the answer doesn't exist print "No" (without quotes) in the only line of the output.
Otherwise, in the first line print "Yes" (without quotes). In the each of the next (*n*<=-<=1) lines print two integers β the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed exactly once.
You can output edges in any order. You can output the ends of each edge in any order.
If there are several solutions, print any of them. | [
"3 3\n1 2\n2 3\n3 1\n1 2 1 1\n",
"7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4\n"
] | [
"Yes\n3 2\n1 3\n",
"Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1\n"
] | none | [
{
"input": "3 3\n1 2\n2 3\n3 1\n1 2 1 1",
"output": "Yes\n3 2\n1 3"
},
{
"input": "7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4",
"output": "Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1"
},
{
"input": "10 15\n4 1\n5 10\n2 1\n5 7\n9 2\n4 6\n6 7\n9 1\n6 9\n8 4\n8 3\n9 8\n3 9\n2 3\n7 10\n10 1 ... | 30 | 0 | 0 | 58,109 | |
67 | Save the City! | [
"geometry"
] | E. Save the City! | 1 | 256 | In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away from the uncorrupted part, the crimes could be stopped. So, they are trying to set up a compound where they can keep the corrupted people. To ensure that the criminals don't escape the compound, a watchtower needs to be set up, so that they can be watched.
Since the people of Aalam-Aara aren't very rich, they met up with a merchant from some rich town who agreed to sell them a land-plot which has already a straight line fence *AB* along which a few points are set up where they can put up a watchtower. Your task is to help them find out the number of points on that fence where the tower can be put up, so that all the criminals can be watched from there. Only one watchtower can be set up. A criminal is watchable from the watchtower if the line of visibility from the watchtower to him doesn't cross the plot-edges at any point between him and the tower i.e. as shown in figure 1 below, points *X*, *Y*, *C* and *A* are visible from point *B* but the points *E* and *D* are not.
Assume that the land plot is in the shape of a polygon and coordinate axes have been setup such that the fence *AB* is parallel to *x*-axis and the points where the watchtower can be set up are the integer points on the line. For example, in given figure 2, watchtower can be setup on any of five integer points on *AB* i.e. (4,<=8), (5,<=8), (6,<=8), (7,<=8) or (8,<=8). You can assume that no three consecutive points are collinear and all the corner points other than *A* and *B*, lie towards same side of fence *AB*. The given polygon doesn't contain self-intersections. | The first line of the test case will consist of the number of vertices *n* (3<=β€<=*n*<=β€<=1000).
Next *n* lines will contain the coordinates of the vertices in the clockwise order of the polygon. On the *i*-th line are integers *x**i* and *y**i* (0<=β€<=*x**i*,<=*y**i*<=β€<=106) separated by a space.
The endpoints of the fence *AB* are the first two points, (*x*1,<=*y*1) and (*x*2,<=*y*2). | Output consists of a single line containing the number of points where the watchtower can be set up. | [
"5\n4 8\n8 8\n9 4\n4 0\n0 4\n",
"5\n4 8\n5 8\n5 4\n7 4\n2 2\n"
] | [
"5\n",
"0\n"
] | Figure 2 shows the first test case. All the points in the figure are watchable from any point on fence *AB*. Since, *AB* has 5 integer coordinates, so answer is 5.
For case two, fence *CD* and *DE* are not completely visible, thus answer is 0. | [] | 92 | 0 | 0 | 58,154 |
82 | Two out of Three | [
"dp"
] | D. Two out of Three | 2 | 256 | Vasya has recently developed a new algorithm to optimize the reception of customer flow and he considered the following problem.
Let the queue to the cashier contain *n* people, at that each of them is characterized by a positive integer *a**i* β that is the time needed to work with this customer. What is special about this very cashier is that it can serve two customers simultaneously. However, if two customers need *a**i* and *a**j* of time to be served, the time needed to work with both of them customers is equal to *max*(*a**i*,<=*a**j*). Please note that working with customers is an uninterruptable process, and therefore, if two people simultaneously come to the cashier, it means that they begin to be served simultaneously, and will both finish simultaneously (it is possible that one of them will have to wait).
Vasya used in his algorithm an ingenious heuristic β as long as the queue has more than one person waiting, then some two people of the first three standing in front of the queue are sent simultaneously. If the queue has only one customer number *i*, then he goes to the cashier, and is served within *a**i* of time. Note that the total number of phases of serving a customer will always be equal to β*n*<=/<=2β.
Vasya thinks that this method will help to cope with the queues we all hate. That's why he asked you to work out a program that will determine the minimum time during which the whole queue will be served using this algorithm. | The first line of the input file contains a single number *n* (1<=β€<=*n*<=β€<=1000), which is the number of people in the sequence. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106). The people are numbered starting from the cashier to the end of the queue. | Print on the first line a single number β the minimum time needed to process all *n* people. Then on β*n*<=/<=2β lines print the order in which customers will be served. Each line (probably, except for the last one) must contain two numbers separated by a space β the numbers of customers who will be served at the current stage of processing. If *n* is odd, then the last line must contain a single number β the number of the last served customer in the queue. The customers are numbered starting from 1. | [
"4\n1 2 3 4\n",
"5\n2 4 3 1 4\n"
] | [
"6\n1 2\n3 4\n",
"8\n1 3\n2 5\n4\n"
] | none | [
{
"input": "4\n1 2 3 4",
"output": "6\n1 2\n3 4"
},
{
"input": "5\n2 4 3 1 4",
"output": "8\n1 3\n2 5\n4"
},
{
"input": "1\n10",
"output": "10\n1"
},
{
"input": "2\n3 5",
"output": "5\n1 2"
},
{
"input": "3\n1 10 1",
"output": "11\n1 2\n3"
},
{
"input"... | 62 | 0 | 0 | 58,428 |
283 | Cow Program | [
"dfs and similar",
"dp",
"graphs"
] | null | null | Farmer John has just given the cows a program to play with! The program contains two integer variables, *x* and *y*, and performs the following operations on a sequence *a*1,<=*a*2,<=...,<=*a**n* of positive integers:
1. Initially, *x*<==<=1 and *y*<==<=0. If, after any step, *x*<=β€<=0 or *x*<=><=*n*, the program immediately terminates. 1. The program increases both *x* and *y* by a value equal to *a**x* simultaneously. 1. The program now increases *y* by *a**x* while decreasing *x* by *a**x*. 1. The program executes steps 2 and 3 (first step 2, then step 3) repeatedly until it terminates (it may never terminate). So, the sequence of executed steps may start with: step 2, step 3, step 2, step 3, step 2 and so on.
The cows are not very good at arithmetic though, and they want to see how the program works. Please help them!
You are given the sequence *a*2,<=*a*3,<=...,<=*a**n*. Suppose for each *i* (1<=β€<=*i*<=β€<=*n*<=-<=1) we run the program on the sequence *i*,<=*a*2,<=*a*3,<=...,<=*a**n*. For each such run output the final value of *y* if the program terminates or -1 if it does not terminate. | The first line contains a single integer, *n* (2<=β€<=*n*<=β€<=2Β·105). The next line contains *n*<=-<=1 space separated integers, *a*2,<=*a*3,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109). | Output *n*<=-<=1 lines. On the *i*-th line, print the requested value when the program is run on the sequence *i*,<=*a*2,<=*a*3,<=...*a**n*.
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. | [
"4\n2 4 1\n",
"3\n1 2\n"
] | [
"3\n6\n8\n",
"-1\n-1\n"
] | In the first sample
1. For *i*β=β1,β *x* becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d87035805e6f1c669693d86603da6d89c5730833.png" style="max-width: 100.0%;max-height: 100.0%;"/> and *y* becomes 1β+β2β=β3. 1. For *i*β=β2,β *x* becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c59515a288e6cb9876590f520c031cb27e346525.png" style="max-width: 100.0%;max-height: 100.0%;"/> and *y* becomes 2β+β4β=β6. 1. For *i*β=β3,β *x* becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/62b52a6cff2221e154a9c36f7bb277ad6475a1b6.png" style="max-width: 100.0%;max-height: 100.0%;"/> and *y* becomes 3β+β1β+β4β=β8. | [
{
"input": "4\n2 4 1",
"output": "3\n6\n8"
},
{
"input": "3\n1 2",
"output": "-1\n-1"
},
{
"input": "5\n2 2 1 3",
"output": "3\n-1\n-1\n-1"
},
{
"input": "2\n1",
"output": "-1"
},
{
"input": "8\n7 6 2 6 2 6 6",
"output": "8\n8\n12\n10\n-1\n-1\n20"
},
{
... | 466 | 29,696,000 | 3 | 58,492 | |
732 | Sanatorium | [
"binary search",
"constructive algorithms",
"greedy",
"implementation",
"math"
] | null | null | Vasiliy spent his vacation in a sanatorium, came back and found that he completely forgot details of his vacation!
Every day there was a breakfast, a dinner and a supper in a dining room of the sanatorium (of course, in this order). The only thing that Vasiliy has now is a card from the dining room contaning notes how many times he had a breakfast, a dinner and a supper (thus, the card contains three integers). Vasiliy could sometimes have missed some meal, for example, he could have had a breakfast and a supper, but a dinner, or, probably, at some days he haven't been at the dining room at all.
Vasiliy doesn't remember what was the time of the day when he arrived to sanatorium (before breakfast, before dinner, before supper or after supper), and the time when he left it (before breakfast, before dinner, before supper or after supper). So he considers any of these options. After Vasiliy arrived to the sanatorium, he was there all the time until he left. Please note, that it's possible that Vasiliy left the sanatorium on the same day he arrived.
According to the notes in the card, help Vasiliy determine the minimum number of meals in the dining room that he could have missed. We shouldn't count as missed meals on the arrival day before Vasiliy's arrival and meals on the departure day after he left. | The only line contains three integers *b*, *d* and *s* (0<=β€<=*b*,<=*d*,<=*s*<=β€<=1018,<=<=*b*<=+<=*d*<=+<=*s*<=β₯<=1)Β β the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium. | Print single integerΒ β the minimum possible number of meals which Vasiliy could have missed during his vacation. | [
"3 2 1\n",
"1 0 0\n",
"1 1 1\n",
"1000000000000000000 0 1000000000000000000\n"
] | [
"1\n",
"0\n",
"0\n",
"999999999999999999\n"
] | In the first sample, Vasiliy could have missed one supper, for example, in case he have arrived before breakfast, have been in the sanatorium for two days (including the day of arrival) and then have left after breakfast on the third day.
In the second sample, Vasiliy could have arrived before breakfast, have had it, and immediately have left the sanatorium, not missing any meal.
In the third sample, Vasiliy could have been in the sanatorium for one day, not missing any meal. | [
{
"input": "3 2 1",
"output": "1"
},
{
"input": "1 0 0",
"output": "0"
},
{
"input": "1 1 1",
"output": "0"
},
{
"input": "1000000000000000000 0 1000000000000000000",
"output": "999999999999999999"
},
{
"input": "1000 0 0",
"output": "1998"
},
{
"input... | 93 | 0 | 3 | 58,539 | |
788 | The Great Mixing | [
"dfs and similar",
"graphs",
"shortest paths"
] | null | null | Sasha and Kolya decided to get drunk with Coke, again. This time they have *k* types of Coke. *i*-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.
Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.
Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type. | The first line contains two integers *n*, *k* (0<=β€<=*n*<=β€<=1000, 1<=β€<=*k*<=β€<=106)Β β carbon dioxide concentration the friends want and the number of Coke types.
The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (0<=β€<=*a**i*<=β€<=1000)Β β carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration. | Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible. | [
"400 4\n100 300 450 500\n",
"50 2\n100 25\n"
] | [
"2\n",
"3\n"
] | In the first sample case, we can achieve concentration <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bed0f5c3640139492194728ccc3ac55accf16a8e.png" style="max-width: 100.0%;max-height: 100.0%;"/> using one liter of Coke of types <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9b37ab6b0795f08ffcc699d9101a9efb89374478.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d82574f3d78c4bd9d8ab9bda103e05a51e1b3161.png" style="max-width: 100.0%;max-height: 100.0%;"/>: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b23f59a536403f9a2364e971aa0bfc9a3411b366.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second case, we can achieve concentration <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/46aa9afb7ee4d932ca2c3f0d6535a9955fc8f0a8.png" style="max-width: 100.0%;max-height: 100.0%;"/> using two liters of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/69b8967d23533c2caada3910f564294509450a59.png" style="max-width: 100.0%;max-height: 100.0%;"/> type and one liter of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/abf5cbf9e8a81a0eff83ff53574dcabb097df44e.png" style="max-width: 100.0%;max-height: 100.0%;"/> type: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d2331fc733efc58d37745ff9a495a116ebd7e8a.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "400 4\n100 300 450 500",
"output": "2"
},
{
"input": "50 2\n100 25",
"output": "3"
},
{
"input": "500 3\n1000 5 5",
"output": "199"
},
{
"input": "500 1\n1000",
"output": "-1"
},
{
"input": "874 3\n873 974 875",
"output": "2"
},
{
"input": ... | 530 | 44,851,200 | 0 | 58,855 | |
0 | none | [
"none"
] | null | null | We'll call a set of positive integers *a* beautiful if the following condition fulfills: for any prime *p*, if , then . In other words, if one number from the set is divisible by prime *p*, then at least half of numbers from the set is divisible by *p*.
Your task is to find any beautiful set, where the number of elements is equal to *k* and each element doesn't exceed 2*k*2. | The first line contains integer *k* (10<=β€<=*k*<=β€<=5000) that shows how many numbers the required beautiful set should have. | In the first line print *k* space-separated integers that are a beautiful set. If there are multiple such sets, you are allowed to print any of them. | [
"10\n"
] | [
"16 18 24 27 36 48 54 72 108 144 \n"
] | none | [] | 46 | 0 | 0 | 59,010 | |
41 | 3-cycles | [
"constructive algorithms",
"graphs",
"greedy"
] | E. 3-cycles | 2 | 256 | During a recent research Berland scientists found out that there were *n* cities in Ancient Berland, joined by two-way paths. Any two cities are joined by no more than one path. No path joins a city with itself. According to a well-known tradition, the road network was built so that it would be impossible to choose three cities from each of which one can get to any other one directly. That is, there was no cycle exactly as long as 3. Unfortunately, the road map has not been preserved till nowadays. Now the scientists are interested how much developed a country Ancient Berland was. Help them - find, what maximal number of roads could be in the country. You also have to restore any of the possible road maps. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=100) β the number of cities in Berland. | On the first line must be printed number *m* β the maximal number of roads in Berland. Then print *m* lines containing two numbers each β the numbers of cities that the given road joins. The cities are numbered with integers from 1 to *n*. If there are several variants of solving the problem, print any of them. | [
"3\n",
"4\n"
] | [
"2\n1 2\n2 3\n",
"4\n1 2\n2 3\n3 4\n4 1\n"
] | none | [
{
"input": "3",
"output": "2\n1 2\n1 3"
},
{
"input": "4",
"output": "4\n1 3\n1 4\n2 3\n2 4"
},
{
"input": "5",
"output": "6\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5"
},
{
"input": "6",
"output": "9\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6"
},
{
"input": "7",
"output... | 92 | 0 | 0 | 59,184 |
132 | Constants in the language of Shakespeare | [
"constructive algorithms",
"dp",
"greedy"
] | null | null | Shakespeare is a widely known esoteric programming language in which programs look like plays by Shakespeare, and numbers are given by combinations of ornate epithets. In this problem we will have a closer look at the way the numbers are described in Shakespeare.
Each constant in Shakespeare is created from non-negative powers of 2 using arithmetic operations. For simplicity we'll allow only addition and subtraction and will look for a representation of the given number which requires a minimal number of operations.
You are given an integer *n*. You have to represent it as *n*<==<=*a*1<=+<=*a*2<=+<=...<=+<=*a**m*, where each of *a**i* is a non-negative power of 2, possibly multiplied by -1. Find a representation which minimizes the value of *m*. | The only line of input contains a positive integer *n*, written as its binary notation. The length of the notation is at most 106. The first digit of the notation is guaranteed to be 1. | Output the required minimal *m*. After it output *m* lines. Each line has to be formatted as "+2^x" or "-2^x", where *x* is the power coefficient of the corresponding term. The order of the lines doesn't matter. | [
"1111\n",
"1010011\n"
] | [
"2\n+2^4\n-2^0\n",
"4\n+2^0\n+2^1\n+2^4\n+2^6\n"
] | none | [
{
"input": "1111",
"output": "2\n+2^4\n-2^0"
},
{
"input": "1010011",
"output": "4\n+2^0\n+2^1\n+2^4\n+2^6"
},
{
"input": "1",
"output": "1\n+2^0"
},
{
"input": "10110111",
"output": "4\n+2^8\n-2^6\n-2^3\n-2^0"
},
{
"input": "10101110",
"output": "4\n+2^8\n-2^... | 46 | 0 | 0 | 59,220 | |
926 | Segments | [
"data structures"
] | null | null | There is a straight line colored in white. *n* black segments are added on it one by one.
After each segment is added, determine the number of connected components of black segments (i.Β e. the number of black segments in the union of the black segments).
In particular, if one segment ends in a point *x*, and another segment starts in the point *x*, these two segments belong to the same connected component. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=200<=000) β the number of segments.
The *i*-th of the next *n* lines contains two integers *l**i* and *r**i* (1<=β€<=*l**i*<=<<=*r**i*<=β€<=109) β the coordinates of the left and the right ends of the *i*-th segment. The segments are listed in the order they are added on the white line. | Print *n* integers β the number of connected components of black segments after each segment is added. | [
"3\n1 3\n4 5\n2 4\n",
"9\n10 20\n50 60\n30 40\n70 80\n90 100\n60 70\n10 40\n40 50\n80 90\n"
] | [
"1 2 1 \n",
"1 2 3 4 5 4 3 2 1 \n"
] | In the first example there are two components after the addition of the first two segments, because these segments do not intersect. The third added segment intersects the left segment and touches the right segment at the point 4 (these segments belong to the same component, according to the statements). Thus the number of connected components of black segments is equal to 1 after that. | [] | 31 | 0 | 0 | 59,231 | |
812 | Sagheer and Apple Tree | [
"games",
"trees"
] | null | null | Sagheer is playing a game with his best friend Soliman. He brought a tree with *n* nodes numbered from 1 to *n* and rooted at node 1. The *i*-th node has *a**i* apples. This tree has a special property: the lengths of all paths from the root to any leaf have the same parity (i.e. all paths have even length or all paths have odd length).
Sagheer and Soliman will take turns to play. Soliman will make the first move. The player who can't make a move loses.
In each move, the current player will pick a single node, take a non-empty subset of apples from it and do one of the following two things:
1. eat the apples, if the node is a leaf. 1. move the apples to one of the children, if the node is non-leaf.
Before Soliman comes to start playing, Sagheer will make exactly one change to the tree. He will pick two different nodes *u* and *v* and swap the apples of *u* with the apples of *v*.
Can you help Sagheer count the number of ways to make the swap (i.e. to choose *u* and *v*) after which he will win the game if both players play optimally? (*u*,<=*v*) and (*v*,<=*u*) are considered to be the same pair. | The first line will contain one integer *n* (2<=β€<=*n*<=β€<=105) β the number of nodes in the apple tree.
The second line will contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=107) β the number of apples on each node of the tree.
The third line will contain *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*) β the parent of each node of the tree. Node *i* has parent *p**i* (for 2<=β€<=*i*<=β€<=*n*). Node 1 is the root of the tree.
It is guaranteed that the input describes a valid tree, and the lengths of all paths from the root to any leaf will have the same parity. | On a single line, print the number of different pairs of nodes (*u*,<=*v*), *u*<=β <=*v* such that if they start playing after swapping the apples of both nodes, Sagheer will win the game. (*u*,<=*v*) and (*v*,<=*u*) are considered to be the same pair. | [
"3\n2 2 3\n1 1\n",
"3\n1 2 3\n1 1\n",
"8\n7 2 2 5 4 3 1 1\n1 1 1 4 4 5 6\n"
] | [
"1\n",
"0\n",
"4\n"
] | In the first sample, Sagheer can only win if he swapped node 1 with node 3. In this case, both leaves will have 2 apples. If Soliman makes a move in a leaf node, Sagheer can make the same move in the other leaf. If Soliman moved some apples from a root to a leaf, Sagheer will eat those moved apples. Eventually, Soliman will not find a move.
In the second sample, There is no swap that will make Sagheer win the game.
Note that Sagheer must make the swap even if he can win with the initial tree. | [
{
"input": "3\n2 2 3\n1 1",
"output": "1"
},
{
"input": "3\n1 2 3\n1 1",
"output": "0"
},
{
"input": "8\n7 2 2 5 4 3 1 1\n1 1 1 4 4 5 6",
"output": "4"
},
{
"input": "6\n7 7 7 7 7 7\n1 1 1 1 1",
"output": "0"
},
{
"input": "6\n3 1 1 1 2 2\n1 1 1 1 1",
"output"... | 217 | 9,830,400 | 3 | 59,283 | |
0 | none | [
"none"
] | null | null | Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherlandΒ β Uzhlyandia.
It is widely known that Uzhlyandia has *n* cities connected with *m* bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is good if the path goes over *m*<=-<=2 roads twice, and over the other 2 exactly once. The good path can start and finish in any city of Uzhlyandia.
Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help IgorΒ β calculate the number of good paths. | The first line contains two integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=106)Β β the number of cities and roads in Uzhlyandia, respectively.
Each of the next *m* lines contains two integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*) that mean that there is road between cities *u* and *v*.
It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself. | Print out the only integerΒ β the number of good paths in Uzhlyandia. | [
"5 4\n1 2\n1 3\n1 4\n1 5\n",
"5 3\n1 2\n2 3\n4 5\n",
"2 2\n1 1\n1 2\n"
] | [
"6",
"0",
"1"
] | In first sample test case the good paths are:
- 2βββ1βββ3βββ1βββ4βββ1βββ5, - 2βββ1βββ3βββ1βββ5βββ1βββ4, - 2βββ1βββ4βββ1βββ5βββ1βββ3, - 3βββ1βββ2βββ1βββ4βββ1βββ5, - 3βββ1βββ2βββ1βββ5βββ1βββ4, - 4βββ1βββ2βββ1βββ3βββ1βββ5.
There are good paths that are same with displayed above, because the sets of roads they pass over once are same:
- 2βββ1βββ4βββ1βββ3βββ1βββ5, - 2βββ1βββ5βββ1βββ3βββ1βββ4, - 2βββ1βββ5βββ1βββ4βββ1βββ3, - 3βββ1βββ4βββ1βββ2βββ1βββ5, - 3βββ1βββ5βββ1βββ2βββ1βββ4, - 4βββ1βββ3βββ1βββ2βββ1βββ5, - and all the paths in the other direction.
Thus, the answer is 6.
In the second test case, Igor simply can not walk by all the roads.
In the third case, Igor walks once over every road. | [] | 77 | 5,632,000 | 0 | 59,428 | |
174 | Range Increments | [
"data structures",
"greedy"
] | null | null | Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array *a* for all indexes in the segment [*l*,<=*r*]. In other words, this function does the following:
Polycarpus knows the state of the array *a* after a series of function calls. He wants to determine the minimum number of function calls that lead to such state. In addition, he wants to find what function calls are needed in this case. It is guaranteed that the required number of calls does not exceed 105.
Before calls of function rangeIncrement(l, r) all array elements equal zero. | The first input line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the length of the array *a*[1... *n*].
The second line contains its integer space-separated elements, *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=β€<=*a*[*i*]<=β€<=105) after some series of function calls rangeIncrement(l, r).
It is guaranteed that at least one element of the array is positive. It is guaranteed that the answer contains no more than 105 calls of function rangeIncrement(l, r). | Print on the first line *t* β the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105.
Then print *t* lines β the descriptions of function calls, one per line. Each line should contain two integers *l**i*,<=*r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*) β the arguments of the *i*-th call rangeIncrement(l, r). Calls can be applied in any order.
If there are multiple solutions, you are allowed to print any of them. | [
"6\n1 2 1 1 4 1\n",
"5\n1 0 1 0 1\n"
] | [
"5\n2 2\n5 5\n5 5\n5 5\n1 6\n",
"3\n1 1\n3 3\n5 5\n"
] | The first sample requires a call for the entire array, and four additional calls:
- one for the segment [2,2] (i.e. the second element of the array), - three for the segment [5,5] (i.e. the fifth element of the array). | [
{
"input": "6\n1 2 1 1 4 1",
"output": "5\n2 2\n5 5\n5 5\n5 5\n1 6"
},
{
"input": "5\n1 0 1 0 1",
"output": "3\n1 1\n3 3\n5 5"
},
{
"input": "1\n1",
"output": "1\n1 1"
},
{
"input": "1\n100000",
"output": "100000\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1... | 280 | 24,576,000 | 0 | 59,450 | |
0 | none | [
"none"
] | null | null | After all the events in Orlando we all know, Sasha and Roma decided to find out who is still the team's biggest loser. Thankfully, Masha found somewhere a revolver with a rotating cylinder of *n* bullet slots able to contain exactly *k* bullets, now the boys have a chance to resolve the problem once and for all.
Sasha selects any *k* out of *n* slots he wishes and puts bullets there. Roma spins the cylinder so that every of *n* possible cylinder's shifts is equiprobable. Then the game starts, the players take turns, Sasha starts: he puts the gun to his head and shoots. If there was no bullet in front of the trigger, the cylinder shifts by one position and the weapon is given to Roma for make the same move. The game continues until someone is shot, the survivor is the winner.
Sasha does not want to lose, so he must choose slots for bullets in such a way as to minimize the probability of its own loss. Of all the possible variant he wants to select the lexicographically minimal one, where an empty slot is lexicographically less than a charged one.
More formally, the cylinder of *n* bullet slots able to contain *k* bullets can be represented as a string of *n* characters. Exactly *k* of them are "X" (charged slots) and the others are "." (uncharged slots).
Let us describe the process of a shot. Suppose that the trigger is in front of the first character of the string (the first slot). If a shot doesn't kill anyone and the cylinder shifts, then the string shifts left. So the first character becomes the last one, the second character becomes the first one, and so on. But the trigger doesn't move. It will be in front of the first character of the resulting string.
Among all the strings that give the minimal probability of loss, Sasha choose the lexicographically minimal one. According to this very string, he charges the gun. You have to help Sasha to charge the gun. For that, each *x**i* query must be answered: is there a bullet in the positions *x**i*? | The first line contains three integers *n*, *k* and *p* (1<=β€<=*n*<=β€<=1018,<=0<=β€<=*k*<=β€<=*n*,<=1<=β€<=*p*<=β€<=1000) β the number of slots in the cylinder, the number of bullets and the number of queries. Then follow *p* lines; they are the queries. Each line contains one integer *x**i* (1<=β€<=*x**i*<=β€<=*n*) the number of slot to describe.
Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. | For each query print "." if the slot should be empty and "X" if the slot should be charged. | [
"3 1 3\n1\n2\n3\n",
"6 3 6\n1\n2\n3\n4\n5\n6\n",
"5 2 5\n1\n2\n3\n4\n5\n"
] | [
"..X",
".X.X.X",
"...XX"
] | The lexicographical comparison of is performed by the < operator in modern programming languages. The *a* string is lexicographically less that the *b* string, if there exists such *i* (1ββ€β*i*ββ€β*n*), that *a*<sub class="lower-index">*i*</sub>β<β*b*<sub class="lower-index">*i*</sub>, and for any *j* (1ββ€β*j*β<β*i*) *a*<sub class="lower-index">*j*</sub>β=β*b*<sub class="lower-index">*j*</sub>. | [
{
"input": "3 1 3\n1\n2\n3",
"output": "..X"
},
{
"input": "6 3 6\n1\n2\n3\n4\n5\n6",
"output": ".X.X.X"
},
{
"input": "5 2 5\n1\n2\n3\n4\n5",
"output": "...XX"
},
{
"input": "4 2 8\n1\n3\n4\n2\n3\n4\n1\n2",
"output": "..XX.X.X"
},
{
"input": "4 0 4\n1\n2\n3\n4",
... | 0 | 0 | -1 | 59,518 | |
39 | Moon Craters | [
"dp",
"sortings"
] | C. Moon Craters | 1 | 256 | There are lots of theories concerning the origin of moon craters. Most scientists stick to the meteorite theory, which says that the craters were formed as a result of celestial bodies colliding with the Moon. The other version is that the craters were parts of volcanoes.
An extraterrestrial intelligence research specialist professor Okulov (the namesake of the Okulov, the author of famous textbooks on programming) put forward an alternate hypothesis. Guess what kind of a hypothesis it was ββ sure, the one including extraterrestrial mind involvement. Now the professor is looking for proofs of his hypothesis.
Professor has data from the moon robot that moves linearly in one direction along the Moon surface. The moon craters are circular in form with integer-valued radii. The moon robot records only the craters whose centers lay on his path and sends to the Earth the information on the distance from the centers of the craters to the initial point of its path and on the radii of the craters.
According to the theory of professor Okulov two craters made by an extraterrestrial intelligence for the aims yet unknown either are fully enclosed one in the other or do not intersect at all. Internal or external tangency is acceptable. However the experimental data from the moon robot do not confirm this theory! Nevertheless, professor Okulov is hopeful. He perfectly understands that to create any logical theory one has to ignore some data that are wrong due to faulty measuring (or skillful disguise by the extraterrestrial intelligence that will be sooner or later found by professor Okulov!) Thatβs why Okulov wants to choose among the available crater descriptions the largest set that would satisfy his theory. | The first line has an integer *n* (1<=β€<=*n*<=β€<=2000) β the number of discovered craters. The next *n* lines contain crater descriptions in the "*c**i* *r**i*" format, where *c**i* is the coordinate of the center of the crater on the moon robotβs path, *r**i* is the radius of the crater. All the numbers *c**i* and *r**i* are positive integers not exceeding 109. No two craters coincide. | In the first line output the number of craters in the required largest set. In the next line output space-separated numbers of craters that this set consists of. The craters are numbered from 1 to *n* in the order in which they were given in the input data. The numbers may be output in any order. If the result is not unique, output any. | [
"4\n1 1\n2 2\n4 1\n5 1\n"
] | [
"3\n1 2 4\n"
] | none | [] | 1,000 | 4,710,400 | 0 | 59,685 |
630 | Pyramids | [
"geometry",
"math"
] | null | null | IT City administration has no rest because of the fame of the Pyramids in Egypt. There is a project of construction of pyramid complex near the city in the place called Emerald Walley. The distinction of the complex is that its pyramids will be not only quadrangular as in Egypt but also triangular and pentagonal. Of course the amount of the city budget funds for the construction depends on the pyramids' volume. Your task is to calculate the volume of the pilot project consisting of three pyramids β one triangular, one quadrangular and one pentagonal.
The first pyramid has equilateral triangle as its base, and all 6 edges of the pyramid have equal length. The second pyramid has a square as its base and all 8 edges of the pyramid have equal length. The third pyramid has a regular pentagon as its base and all 10 edges of the pyramid have equal length. | The only line of the input contains three integers *l*3,<=*l*4,<=*l*5 (1<=β€<=*l*3,<=*l*4,<=*l*5<=β€<=1000) β the edge lengths of triangular, quadrangular and pentagonal pyramids correspondingly. | Output one number β the total volume of the pyramids. Absolute or relative error should not be greater than 10<=-<=9. | [
"2 5 3\n"
] | [
"38.546168065709"
] | none | [
{
"input": "2 5 3",
"output": "38.546168065709"
},
{
"input": "3 4 5",
"output": "55.954779230131"
},
{
"input": "1 1 1",
"output": "0.655056222989"
},
{
"input": "1000 1000 1000",
"output": "655056222.989098310000"
},
{
"input": "999 997 998",
"output": "6507... | 62 | 0 | 0 | 60,000 | |
140 | New Year Snowflake | [
"geometry",
"sortings"
] | null | null | As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true.
The New Year snowflake consists of tiny ice crystals, which can be approximately regarded as points on the plane. The beauty of the New Year snowflake is that it has a center of symmetry. This is a point such that for each crystal of the snowflake exists another crystal, symmetrical to it relative to that point. One of the crystals can be placed directly in the center of symmetry.
While Constantine was choosing a snowflake among millions of other snowflakes, no less symmetrical and no less magical, then endured a difficult path through the drifts to the house of his mistress, while he was waiting with bated breath for a few long moments before the Beautiful Lady opens the door, some of the snowflake crystals melted and naturally disappeared. Constantine is sure that there were no more than *k* of such crystals, because he handled the snowflake very carefully. Now he is ready to demonstrate to the Beautiful Lady all the power of nanotechnology and restore the symmetry of snowflakes.
You are given the coordinates of the surviving snowflake crystals, given in nanometers. Your task is to identify all possible positions of the original center of symmetry. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=200<=000, 0<=β€<=*k*<=β€<=10) β the number of the surviving snowflake crystals and the maximum number of melted crystals, correspondingly. Next *n* lines contain the coordinates of the crystals that are left in the following form: "*x**i* *y**i*". The coordinates are integers and do not exceed 5Β·108 in absolute value. All given points are different. | The first line contains an integer *c* β the number of possible symmetry centers. Next *c* lines should contain the centers' descriptions. Each symmetry center is described by a couple of coordinates "*x* *y*", separated by a space. Print the coordinates with absolute error not exceeding 10<=-<=6. You are allowed to print the symmetry centers in any order. All printed points should be different. If there exist an infinite number of possible symmetry centers, print the single number "-1". | [
"4 0\n0 0\n0 1\n1 0\n1 1\n",
"4 2\n0 0\n0 1\n1 0\n1 1\n",
"4 4\n0 0\n0 1\n1 0\n1 1\n"
] | [
"1\n0.5 0.5\n",
"5\n0.0 0.5\n0.5 0.0\n0.5 0.5\n0.5 1.0\n1.0 0.5\n",
"-1\n"
] | none | [] | 1,000 | 15,052,800 | 0 | 60,081 | |
164 | Variable, or There and Back Again | [
"dfs and similar",
"graphs"
] | null | null | Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used!
Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or ignored. Between some states are directed (oriented) transitions.
A path is a sequence of states *v*1,<=*v*2,<=...,<=*v**x*, where for any 1<=β€<=*i*<=<<=*x* exists a transition from *v**i* to *v**i*<=+<=1.
Vasya's value in state *v* is interesting to the world, if exists path *p*1,<=*p*2,<=...,<=*p**k* such, that *p**i*<==<=*v* for some *i* (1<=β€<=*i*<=β€<=*k*), in state *p*1 Vasya gets assigned a value, in state *p**k* Vasya is used and there is no state *p**i* (except for *p*1) where Vasya gets assigned a value.
Help Vasya, find the states in which Vasya's value is interesting to the world. | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the numbers of states and transitions, correspondingly.
The second line contains space-separated *n* integers *f*1,<=*f*2,<=...,<=*f**n* (0<=β€<=*f**i*<=β€<=2), *f**i* described actions performed upon Vasya in state *i*: 0 represents ignoring, 1 β assigning a value, 2 β using.
Next *m* lines contain space-separated pairs of integers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*), each pair represents the transition from the state number *a**i* to the state number *b**i*. Between two states can be any number of transitions. | Print *n* integers *r*1,<=*r*2,<=...,<=*r**n*, separated by spaces or new lines. Number *r**i* should equal 1, if Vasya's value in state *i* is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to *n* in the order, in which they are described in the input. | [
"4 3\n1 0 0 2\n1 2\n2 3\n3 4\n",
"3 1\n1 0 2\n1 3\n",
"3 1\n2 0 1\n1 3\n"
] | [
"1\n1\n1\n1\n",
"1\n0\n1\n",
"0\n0\n0\n"
] | In the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 3 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 4; it includes all the states, so in all of them Vasya's value is interesting to the world.
The second sample the only path in which Vasya's value is interesting to the world is , β 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 3; state 2 is not included there.
In the third sample we cannot make from the states any path in which the value of Vasya would be interesting to the world, so the value of Vasya is never interesting to the world. | [
{
"input": "4 3\n1 0 0 2\n1 2\n2 3\n3 4",
"output": "1\n1\n1\n1"
},
{
"input": "3 1\n1 0 2\n1 3",
"output": "1\n0\n1"
},
{
"input": "3 1\n2 0 1\n1 3",
"output": "0\n0\n0"
},
{
"input": "4 4\n1 0 2 0\n1 2\n2 3\n3 4\n4 1",
"output": "1\n1\n1\n0"
},
{
"input": "2 1\n... | 154 | 0 | 0 | 60,128 | |
774 | Pens And Days Of Week | [
"*special",
"binary search",
"number theory"
] | null | null | Stepan has *n* pens. Every day he uses them, and on the *i*-th day he uses the pen number *i*. On the (*n*<=+<=1)-th day again he uses the pen number 1, on the (*n*<=+<=2)-th β he uses the pen number 2 and so on.
On every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the pen he uses that day. On Sunday Stepan has a day of rest, he does not stend the ink of the pen he uses that day.
Stepan knows the current volume of ink in each of his pens. Now it's the Monday morning and Stepan is going to use the pen number 1 today. Your task is to determine which pen will run out of ink before all the rest (that is, there will be no ink left in it), if Stepan will use the pens according to the conditions described above. | The first line contains the integer *n* (1<=β€<=*n*<=β€<=50<=000) β the number of pens Stepan has.
The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109), where *a**i* is equal to the number of milliliters of ink which the pen number *i* currently has. | Print the index of the pen which will run out of ink before all (it means that there will be no ink left in it), if Stepan will use pens according to the conditions described above.
Pens are numbered in the order they are given in input data. The numeration begins from one.
Note that the answer is always unambiguous, since several pens can not end at the same time. | [
"3\n3 3 3\n",
"5\n5 4 5 4 4\n"
] | [
"2\n",
"5\n"
] | In the first test Stepan uses ink of pens as follows:
1. on the day number 1 (Monday) Stepan will use the pen number 1, after that there will be 2 milliliters of ink in it; 1. on the day number 2 (Tuesday) Stepan will use the pen number 2, after that there will be 2 milliliters of ink in it; 1. on the day number 3 (Wednesday) Stepan will use the pen number 3, after that there will be 2 milliliters of ink in it; 1. on the day number 4 (Thursday) Stepan will use the pen number 1, after that there will be 1 milliliters of ink in it; 1. on the day number 5 (Friday) Stepan will use the pen number 2, after that there will be 1 milliliters of ink in it; 1. on the day number 6 (Saturday) Stepan will use the pen number 3, after that there will be 1 milliliters of ink in it; 1. on the day number 7 (Sunday) Stepan will use the pen number 1, but it is a day of rest so he will not waste ink of this pen in it; 1. on the day number 8 (Monday) Stepan will use the pen number 2, after that this pen will run out of ink.
So, the first pen which will not have ink is the pen number 2. | [
{
"input": "3\n3 3 3",
"output": "2"
},
{
"input": "5\n5 4 5 4 4",
"output": "5"
},
{
"input": "28\n2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033",
"output": "1"
},
{
"input": "7\n10... | 46 | 5,529,600 | 0 | 60,194 | |
862 | Mahmoud and Ehab and the final stage | [
"data structures",
"strings"
] | null | null | Mahmoud and Ehab solved Dr. Evil's questions so he gave them the password of the door of the evil land. When they tried to open the door using it, the door gave them a final question to solve before they leave (yes, the door is digital, Dr. Evil is modern). If they don't solve it, all the work will be useless and they won't leave the evil land forever. Will you help them?
Mahmoud and Ehab are given *n* strings *s*1,<=*s*2,<=... ,<=*s**n* numbered from 1 to *n* and *q* queries, Each query has one of the following forms:
- 1 *a* *b* (1<=β€<=*a*<=β€<=*b*<=β€<=*n*), For all the intervals [*l*;*r*] where (*a*<=β€<=*l*<=β€<=*r*<=β€<=*b*) find the maximum value of this expression:(*r*<=-<=*l*<=+<=1)<=*<=*LCP*(*s**l*,<=*s**l*<=+<=1,<=... ,<=*s**r*<=-<=1,<=*s**r*) where *LCP*(*str*1,<=*str*2,<=*str*3,<=... ) is the length of the longest common prefix of the strings *str*1,<=*str*2,<=*str*3,<=... .- 2 *x* *y* (1<=β€<=*x*<=β€<=*n*) where *y* is a string, consisting of lowercase English letters. Change the string at position *x* to *y*. | The first line of input contains 2 integers *n* and *q* (1<=β€<=*n*<=β€<=105,<=1<=β€<=*q*<=β€<=105)Β β The number of strings and the number of queries, respectively.
The second line contains *n* strings *str**i* consisting of lowercase English letters.
The next *q* lines describe the queries and may have one of the 2 forms:
- 1 *a* *b* (1<=β€<=*a*<=β€<=*b*<=β€<=*n*).- 2 *x* *y* (1<=β€<=*x*<=β€<=*n*), where *y* is a string consisting of lowercase English letters.
the total length of all strings in input won't exceed 105 | For each query of first type output its answer in a new line. | [
"5 9\nmahmoud mahmoudbadawy drmahmoud drevil mahmoud\n1 1 5\n1 1 2\n1 2 3\n2 3 mahmoud\n2 4 mahmoud\n2 2 mahmouu\n1 1 5\n1 2 3\n1 1 1\n"
] | [
"14\n14\n13\n30\n12\n7\n"
] | none | [] | 77 | 2,764,800 | -1 | 60,269 | |
167 | Wizards and Bets | [
"dfs and similar",
"graphs",
"math",
"matrices"
] | null | null | In some country live wizards. They like to make weird bets.
Two wizards draw an acyclic directed graph with *n* vertices and *m* edges (the graph's vertices are numbered from 1 to *n*). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink and the source simultaneously. In the wizards' graph the number of the sinks and the sources is the same.
Wizards numbered the sources in the order of increasing numbers of the vertices from 1 to *k*. The sinks are numbered from 1 to *k* in the similar way.
To make a bet, they, as are real wizards, cast a spell, which selects a set of *k* paths from all sources to the sinks in such a way that no two paths intersect at the vertices. In this case, each sink has exactly one path going to it from exactly one source. Let's suppose that the *i*-th sink has a path going to it from the *a**i*'s source. Then let's call pair (*i*,<=*j*) an inversion if *i*<=<<=*j* and *a**i*<=><=*a**j*. If the number of inversions among all possible pairs (*i*,<=*j*), such that (1<=β€<=*i*<=<<=*j*<=β€<=*k*), is even, then the first wizard wins (the second one gives him one magic coin). Otherwise, the second wizard wins (he gets one magic coin from the first one).
Our wizards are captured with feverish excitement, so they kept choosing new paths again and again for so long that eventually they have chosen every possible set of paths for exactly once. The two sets of non-intersecting pathes are considered to be different, if and only if there is an edge, which lies at some path in one set and doesn't lie at any path of another set. To check their notes, they asked you to count the total winnings of the first player for all possible sets of paths modulo a prime number *p*. | The first line contains three space-separated integers *n*, *m*, *p* (1<=β€<=*n*<=β€<=600, 0<=β€<=*m*<=β€<=105, 2<=β€<=*p*<=β€<=109<=+<=7). It is guaranteed that *p* is prime number.
Next *m* lines contain edges of the graph. Each line contains a pair of space-separated integers, *a**i*Β *b**i* β an edge from vertex *a**i* to vertex *b**i*. It is guaranteed that the graph is acyclic and that the graph contains the same number of sources and sinks. Please note that the graph can have multiple edges. | Print the answer to the problem β the total winnings of the first player modulo a prime number *p*. Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample). | [
"4 2 1000003\n1 3\n2 4\n",
"4 2 1000003\n4 1\n3 2\n",
"4 4 1000003\n2 1\n2 4\n3 1\n3 4\n",
"6 5 1000003\n1 4\n1 5\n1 6\n2 6\n3 6\n",
"5 2 1000003\n5 1\n3 4\n"
] | [
"1\n",
"1000002\n",
"0\n",
"0\n",
"1\n"
] | In the first sample, there is exactly one set of paths β <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4c100300071b70505c9eb8fd6f97456068e38f93.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The number of inversions is 0, which is an even number. Therefore, the first wizard gets 1 coin.
In the second sample there is exactly one set of paths β <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/83cf8c3e6763396a8ead42b4762ff62e4b01f917.png" style="max-width: 100.0%;max-height: 100.0%;"/>. There is exactly one inversion. Therefore, the first wizard gets -1 coin. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fa579688f149cd6ab8f4c3c99c47c5a65e52e87c.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the third sample, there are two sets of paths, which are counted with opposite signs.
In the fourth sample there are no set of paths at all.
In the fifth sample, there are three sources β the vertices with the numbers (2, 3, 5) and three sinks β the vertices with numbers (1, 2, 4). For a single set of paths <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5ef1a2c8d476f3fea4b49124c0c23f371be35bb9.png" style="max-width: 100.0%;max-height: 100.0%;"/> are 2 inversions, that is, their number is even. | [] | 92 | 0 | 0 | 60,299 | |
436 | Banners | [
"brute force",
"data structures",
"dp"
] | null | null | All modern mobile applications are divided into free and paid. Even a single application developers often release two versions: a paid version without ads and a free version with ads.
Suppose that a paid version of the app costs *p* (*p* is an integer) rubles, and the free version of the application contains *c* ad banners. Each user can be described by two integers: *a**i* β the number of rubles this user is willing to pay for the paid version of the application, and *b**i* β the number of banners he is willing to tolerate in the free version.
The behavior of each member shall be considered strictly deterministic:
- if for user *i*, value *b**i* is at least *c*, then he uses the free version, - otherwise, if value *a**i* is at least *p*, then he buys the paid version without advertising, - otherwise the user simply does not use the application.
Each user of the free version brings the profit of *c*<=Γ<=*w* rubles. Each user of the paid version brings the profit of *p* rubles.
Your task is to help the application developers to select the optimal parameters *p* and *c*. Namely, knowing all the characteristics of users, for each value of *c* from 0 to (*max*Β *b**i*)<=+<=1 you need to determine the maximum profit from the application and the corresponding parameter *p*. | The first line contains two integers *n* and *w* (1<=β€<=*n*<=β€<=105;Β 1<=β€<=*w*<=β€<=105) β the number of users and the profit from a single banner. Each of the next *n* lines contains two integers *a**i* and *b**i* (0<=β€<=*a**i*,<=*b**i*<=β€<=105) β the characteristics of the *i*-th user. | Print (*max*Β *b**i*)<=+<=2 lines, in the *i*-th line print two integers: *pay* β the maximum gained profit at *c*<==<=*i*<=-<=1, *p* (0<=β€<=*p*<=β€<=109) β the corresponding optimal app cost. If there are multiple optimal solutions, print any of them. | [
"2 1\n2 0\n0 2\n",
"3 1\n3 1\n2 2\n1 3\n"
] | [
"0 3\n3 2\n4 2\n2 2\n",
"0 4\n3 4\n7 3\n7 2\n4 2\n"
] | none | [] | 5,000 | 0 | 0 | 60,536 | |
490 | Chocolate | [
"brute force",
"dfs and similar",
"math",
"meet-in-the-middle",
"number theory"
] | null | null | Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is *a*1<=Γ<=*b*1 segments large and the second one is *a*2<=Γ<=*b*2 segments large.
Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus's mind and Paraskevi's beauty are equally matched, so the two bars must have the same number of squares.
To make the bars have the same number of squares, Polycarpus eats a little piece of chocolate each minute. Each minute he does the following:
- he either breaks one bar exactly in half (vertically or horizontally) and eats exactly a half of the bar, - or he chips of exactly one third of a bar (vertically or horizontally) and eats exactly a third of the bar.
In the first case he is left with a half, of the bar and in the second case he is left with two thirds of the bar.
Both variants aren't always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16<=Γ<=23, then Polycarpus can chip off a half, but not a third. If the bar is 20<=Γ<=18, then Polycarpus can chip off both a half and a third. If the bar is 5<=Γ<=7, then Polycarpus cannot chip off a half nor a third.
What is the minimum number of minutes Polycarpus needs to make two bars consist of the same number of squares? Find not only the required minimum number of minutes, but also the possible sizes of the bars after the process. | The first line of the input contains integers *a*1,<=*b*1 (1<=β€<=*a*1,<=*b*1<=β€<=109) β the initial sizes of the first chocolate bar. The second line of the input contains integers *a*2,<=*b*2 (1<=β€<=*a*2,<=*b*2<=β€<=109) β the initial sizes of the second bar.
You can use the data of type int64 (in Pascal), long long (in Π‘++), long (in Java) to process large integers (exceeding 231<=-<=1). | In the first line print *m* β the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in *m* minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line must correspond to the first bar and the third line must correspond to the second bar. If there are multiple solutions, print any of them.
If there is no solution, print a single line with integer -1. | [
"2 6\n2 3\n",
"36 5\n10 16\n",
"3 5\n2 1\n"
] | [
"1\n1 6\n2 3\n",
"3\n16 5\n5 16\n",
"-1\n"
] | none | [
{
"input": "2 6\n2 3",
"output": "1\n1 6\n2 3"
},
{
"input": "36 5\n10 16",
"output": "3\n16 5\n5 16"
},
{
"input": "3 5\n2 1",
"output": "-1"
},
{
"input": "36 5\n10 12",
"output": "1\n24 5\n10 12"
},
{
"input": "1 1\n1 1",
"output": "0\n1 1\n1 1"
},
{
... | 92 | 0 | 3 | 60,748 | |
843 | Dynamic Shortest Path | [
"graphs",
"shortest paths"
] | null | null | You are given a weighted directed graph, consisting of *n* vertices and *m* edges. You should answer *q* queries of two types:
- 1 vΒ β find the length of shortest path from vertex 1 to vertex *v*. - 2 c *l*1 *l*2 ... *l**c*Β β add 1 to weights of edges with indices *l*1,<=*l*2,<=...,<=*l**c*. | The first line of input data contains integers *n*, *m*, *q* (1<=β€<=*n*,<=*m*<=β€<=105, 1<=β€<=*q*<=β€<=2000)Β β the number of vertices and edges in the graph, and the number of requests correspondingly.
Next *m* lines of input data contain the descriptions of edges: *i*-th of them contains description of edge with index *i*Β β three integers *a**i*, *b**i*, *c**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, 0<=β€<=*c**i*<=β€<=109)Β β the beginning and the end of edge, and its initial weight correspondingly.
Next *q* lines of input data contain the description of edges in the format described above (1<=β€<=*v*<=β€<=*n*, 1<=β€<=*l**j*<=β€<=*m*). It's guaranteed that inside single query all *l**j* are distinct. Also, it's guaranteed that a total number of edges in all requests of the second type does not exceed 106. | For each query of first type print the length of the shortest path from 1 to *v* in a separate line. Print -1, if such path does not exists. | [
"3 2 9\n1 2 0\n2 3 0\n2 1 2\n1 3\n1 2\n2 1 1\n1 3\n1 2\n2 2 1 2\n1 3\n1 2\n",
"5 4 9\n2 3 1\n2 4 1\n3 4 1\n1 2 0\n1 5\n1 4\n2 1 2\n2 1 2\n1 4\n2 2 1 3\n1 4\n2 1 4\n1 4\n"
] | [
"1\n0\n2\n1\n4\n2\n",
"-1\n1\n2\n3\n4\n"
] | The description of changes of the graph in the first sample case:
<img class="tex-graphics" src="https://espresso.codeforces.com/aeb5751e557f6f6158f15919da64eee550146483.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The description of changes of the graph in the second sample case:
<img class="tex-graphics" src="https://espresso.codeforces.com/5d4325fe06b5b55945d91d26c757ff5c9bdfbca1.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 46 | 0 | 0 | 61,080 | |
852 | Dating | [
"brute force",
"dfs and similar",
"graphs",
"trees"
] | null | null | This story is happening in a town named BubbleLand. There are *n* houses in BubbleLand. In each of these *n* houses lives a boy or a girl. People there really love numbers and everyone has their favorite number *f*. That means that the boy or girl that lives in the *i*-th house has favorite number equal to *f**i*.
The houses are numerated with numbers 1 to *n*.
The houses are connected with *n*<=-<=1 bidirectional roads and you can travel from any house to any other house in the town. There is exactly one path between every pair of houses.
A new dating had agency opened their offices in this mysterious town and the citizens were very excited. They immediately sent *q* questions to the agency and each question was of the following format:
- *a* *b*Β β asking how many ways are there to choose a couple (boy and girl) that have the same favorite number and live in one of the houses on the unique path from house *a* to house *b*.
Help the dating agency to answer the questions and grow their business. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105), the number of houses in the town.
The second line contains *n* integers, where the *i*-th number is 1 if a boy lives in the *i*-th house or 0 if a girl lives in *i*-th house.
The third line contains *n* integers, where the *i*-th number represents the favorite number *f**i* (1<=β€<=*f**i*<=β€<=109) of the girl or boy that lives in the *i*-th house.
The next *n*<=-<=1 lines contain information about the roads and the *i*-th line contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*) which means that there exists road between those two houses. It is guaranteed that it's possible to reach any house from any other.
The following line contains an integer *q* (1<=β€<=*q*<=β€<=105), the number of queries.
Each of the following *q* lines represents a question and consists of two integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=*n*). | For each of the *q* questions output a single number, the answer to the citizens question. | [
"7\n1 0 0 1 0 1 0\n9 2 9 2 2 9 9\n2 6\n1 2\n4 2\n6 5\n3 6\n7 4\n2\n1 3\n7 5\n"
] | [
"2\n3\n"
] | In the first question from house 1 to house 3, the potential couples are (1,β3) and (6,β3).
In the second question from house 7 to house 5, the potential couples are (7,β6), (4,β2) and (4,β5). | [] | 46 | 0 | 0 | 61,091 | |
97 | Superset | [
"constructive algorithms",
"divide and conquer"
] | B. Superset | 2 | 256 | A set of points on a plane is called good, if for any two points at least one of the three conditions is true:
- those two points lie on same horizontal line; - those two points lie on same vertical line; - the rectangle, with corners in these two points, contains inside or on its borders at least one point of the set, other than these two. We mean here a rectangle with sides parallel to coordinates' axes, the so-called bounding box of the two points.
You are given a set consisting of *n* points on a plane. Find any good superset of the given set whose size would not exceed 2Β·105 points. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=104) β the number of points in the initial set. Next *n* lines describe the set's points. Each line contains two integers *x**i* and *y**i* (<=-<=109<=β€<=*x**i*,<=*y**i*<=β€<=109) β a corresponding point's coordinates. It is guaranteed that all the points are different. | Print on the first line the number of points *m* (*n*<=β€<=*m*<=β€<=2Β·105) in a good superset, print on next *m* lines the points. The absolute value of the points' coordinates should not exceed 109. Note that you should not minimize *m*, it is enough to find any good superset of the given set, whose size does not exceed 2Β·105.
All points in the superset should have integer coordinates. | [
"2\n1 1\n2 2\n"
] | [
"3\n1 1\n2 2\n1 2\n"
] | none | [] | 92 | 0 | 0 | 61,144 |
732 | Tourist Reform | [
"dfs and similar",
"graphs"
] | null | null | Berland is a tourist country! At least, it can become suchΒ β the government of Berland is confident about this.
There are *n* cities in Berland, some pairs of which are connected by two-ways roads. Each road connects two different cities. In Berland there are no roads which connect the same pair of cities. It is possible to get from any city to any other city using given two-ways roads.
According to the reform each road will become one-way. It will be oriented to one of two directions.
To maximize the tourist attraction of Berland, after the reform for each city *i* the value *r**i* will be calculated. It will equal to the number of cities *x* for which there is an oriented path from the city *i* to the city *x*. In other words, *r**i* will equal the number of cities which can be reached from the city *i* by roads.
The government is sure that tourist's attention will be focused on the minimum value of *r**i*.
Help the government of Berland make the reform to maximize the minimum of *r**i*. | The first line contains two integers *n*,<=*m* (2<=β€<=*n*<=β€<=400<=000,<=1<=β€<=*m*<=β€<=400<=000)Β β the number of cities and the number of roads.
The next *m* lines describe roads in Berland: the *j*-th of them contains two integers *u**j* and *v**j* (1<=β€<=*u**j*,<=*v**j*<=β€<=*n*, *u**j*<=β <=*v**j*), where *u**j* and *v**j* are the numbers of cities which are connected by the *j*-th road.
The cities are numbered from 1 to *n*. It is guaranteed that it is possible to get from any city to any other by following two-ways roads. In Berland there are no roads which connect the same pair of cities. | In the first line print single integerΒ β the maximum possible value *min*1<=β€<=*i*<=β€<=*n*{*r**i*} after the orientation of roads.
The next *m* lines must contain the description of roads after the orientation: the *j*-th of them must contain two integers *u**j*,<=*v**j*, it means that the *j*-th road will be directed from the city *u**j* to the city *v**j*. Print roads in the same order as they are given in the input data. | [
"7 9\n4 3\n2 6\n7 1\n4 1\n7 3\n3 5\n7 4\n6 5\n2 5\n"
] | [
"4\n4 3\n6 2\n7 1\n1 4\n3 7\n5 3\n7 4\n5 6\n2 5\n"
] | none | [
{
"input": "7 9\n4 3\n2 6\n7 1\n4 1\n7 3\n3 5\n7 4\n6 5\n2 5",
"output": "4\n4 3\n6 2\n7 1\n1 4\n3 7\n5 3\n7 4\n5 6\n2 5"
},
{
"input": "2 1\n2 1",
"output": "1\n2 1"
},
{
"input": "3 2\n1 3\n1 2",
"output": "1\n3 1\n2 1"
},
{
"input": "3 3\n3 1\n3 2\n1 2",
"output": "3\n... | 30 | 0 | 0 | 61,179 | |
48 | Black and White | [
"constructive algorithms"
] | H. Black and White | 2 | 256 | According to the legends the king of Berland Berl I was noted for his love of beauty and order. One day he ordered to tile the palace hall's floor where balls and receptions used to take place with black and white tiles according to a regular geometrical pattern invented by him. However, as is after the case, due to low financing there were only *a* black and *b* white tiles delivered to the palace. The other *c* tiles were black and white (see the picture).
The initial plan failed! Having learned of that, the king gave a new command: tile the floor with the available tiles so that no black side of a tile touched a white one. The tiles are squares of one size 1<=Γ<=1, every black and white tile can be rotated in one of the four ways.
The court programmer was given the task to work out the plan of tiling and he coped with the task and didn't suffer the consequences of disobedience. And can you cope with it? | The first line contains given integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100) which represent the sizes of the rectangle that needs to be tiled. The next line contains non-negative numbers *a*, *b* and *c*, *a*<=+<=*b*<=+<=*c*<==<=*nm*, *c*<=β₯<=*m*. | Print 2*n* lines containing 2*m* characters each β the tiling scheme. Every tile is represented by a square 2<=Γ<=2 in the following manner (the order corresponds to the order of the picture above): | [
"2 2\n0 0 4\n",
"2 3\n1 2 3\n"
] | [
"\\../\n#\\/#\n\\##/\n.\\/.\n",
"###/\\#\n##/..\\\n#/....\n/.....\n"
] | none | [] | 60 | 0 | -1 | 61,247 |
9 | How many trees? | [
"combinatorics",
"divide and conquer",
"dp"
] | D. How many trees? | 1 | 64 | In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong β the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure β and then terrible Game Cubes fall down on the city, bringing death to those modules, who cannot win the game...
For sure, as guard Bob appeared in Mainframe many modules stopped fearing Game Cubes. Because Bob (as he is alive yet) has never been defeated by User, and he always meddles with Game Cubes, because he is programmed to this.
However, unpleasant situations can happen, when a Game Cube falls down on Lost Angles. Because there lives a nasty virus β Hexadecimal, who is... mmm... very strange. And she likes to play very much. So, willy-nilly, Bob has to play with her first, and then with User.
This time Hexadecimal invented the following entertainment: Bob has to leap over binary search trees with *n* nodes. We should remind you that a binary search tree is a binary tree, each node has a distinct key, for each node the following is true: the left sub-tree of a node contains only nodes with keys less than the node's key, the right sub-tree of a node contains only nodes with keys greater than the node's key. All the keys are different positive integer numbers from 1 to *n*. Each node of such a tree can have up to two children, or have no children at all (in the case when a node is a leaf).
In Hexadecimal's game all the trees are different, but the height of each is not lower than *h*. In this problem Β«heightΒ» stands for the maximum amount of nodes on the way from the root to the remotest leaf, the root node and the leaf itself included. When Bob leaps over a tree, it disappears. Bob gets the access to a Cube, when there are no trees left. He knows how many trees he will have to leap over in the worst case. And you? | The input data contains two space-separated positive integer numbers *n* and *h* (*n*<=β€<=35, *h*<=β€<=*n*). | Output one number β the answer to the problem. It is guaranteed that it does not exceed 9Β·1018. | [
"3 2\n",
"3 3\n"
] | [
"5",
"4"
] | none | [
{
"input": "3 2",
"output": "5"
},
{
"input": "3 3",
"output": "4"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "2 1",
"output": "2"
},
{
"input": "2 2",
"output": "2"
},
{
"input": "27 11",
"output": "61162698256896"
},
{
"input": "32 ... | 0 | 0 | -1 | 61,449 |
0 | none | [
"none"
] | null | null | Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters "a", ..., "z".
The testtube is divided into *n* consecutive regions. Each region is occupied by a single colony of a certain bacteria type at any given moment. Hence, the population of the testtube at any moment can be described by a string of *n* Latin characters.
Sometimes a colony can decide to conquer another colony in one of the adjacent regions. When that happens, the attacked colony is immediately eliminated and replaced by a colony of the same type as the attacking colony, while the attacking colony keeps its type. Note that a colony can only attack its neighbours within the boundaries of the testtube. At any moment, at most one attack can take place.
For example, consider a testtube with population "babb". There are six options for an attack that may happen next:
- the first colony attacks the second colony (1<=β<=2), the resulting population is "bbbb";- 2<=β<=1, the result is "aabb";- 2<=β<=3, the result is "baab";- 3<=β<=2, the result is "bbbb" (note that the result is the same as the first option);- 3<=β<=4 or 4<=β<=3, the population does not change.
The pattern of attacks is rather unpredictable. Julia is now wondering how many different configurations of bacteria in the testtube she can obtain after a sequence of attacks takes place (it is possible that no attacks will happen at all). Since this number can be large, find it modulo 109<=+<=7. | The first line contains an integer *n*Β β the number of regions in the testtube (1<=β€<=*n*<=β€<=5<=000).
The second line contains *n* small Latin letters that describe the initial population of the testtube. | Print one numberΒ β the answer to the problem modulo 109<=+<=7. | [
"3\naaa\n",
"2\nab\n",
"4\nbabb\n",
"7\nabacaba\n"
] | [
"1\n",
"3\n",
"11\n",
"589\n"
] | In the first sample the population can never change since all bacteria are of the same type.
In the second sample three configurations are possible: "ab" (no attacks), "aa" (the first colony conquers the second colony), and "bb" (the second colony conquers the first colony).
To get the answer for the third sample, note that more than one attack can happen. | [] | 46 | 0 | 0 | 61,497 | |
411 | Multi-core Processor | [
"implementation"
] | null | null | The research center Q has developed a new multi-core processor. The processor consists of *n* cores and has *k* cells of cache memory. Consider the work of this processor.
At each cycle each core of the processor gets one instruction: either do nothing, or the number of the memory cell (the core will write an information to the cell). After receiving the command, the core executes it immediately. Sometimes it happens that at one cycle, multiple cores try to write the information into a single cell. Unfortunately, the developers did not foresee the possibility of resolving conflicts between cores, so in this case there is a deadlock: all these cores and the corresponding memory cell are locked forever. Each of the locked cores ignores all further commands, and no core in the future will be able to record an information into the locked cell. If any of the cores tries to write an information into some locked cell, it is immediately locked.
The development team wants to explore the deadlock situation. Therefore, they need a program that will simulate the processor for a given set of instructions for each core within *m* cycles . You're lucky, this interesting work is entrusted to you. According to the instructions, during the *m* cycles define for each core the number of the cycle, during which it will become locked. It is believed that initially all cores and all memory cells are not locked. | The first line contains three integers *n*, *m*, *k* (1<=β€<=*n*,<=*m*,<=*k*<=β€<=100). Then follow *n* lines describing instructions. The *i*-th line contains *m* integers: *x**i*1,<=*x**i*2,<=...,<=*x**im* (0<=β€<=*x**ij*<=β€<=*k*), where *x**ij* is the instruction that must be executed by the *i*-th core at the *j*-th cycle. If *x**ij* equals 0, then the corresponding instruction is Β«do nothingΒ». But if *x**ij* is a number from 1 to *k*, then the corresponding instruction is Β«write information to the memory cell number *x**ij*Β».
We assume that the cores are numbered from 1 to *n*, the work cycles are numbered from 1 to *m* and the memory cells are numbered from 1 to *k*. | Print *n* lines. In the *i*-th line print integer *t**i*. This number should be equal to 0 if the *i*-th core won't be locked, or it should be equal to the number of the cycle when this core will be locked. | [
"4 3 5\n1 0 0\n1 0 2\n2 3 1\n3 2 0\n",
"3 2 2\n1 2\n1 2\n2 2\n",
"1 1 1\n0\n"
] | [
"1\n1\n3\n0\n",
"1\n1\n0\n",
"0\n"
] | none | [
{
"input": "4 3 5\n1 0 0\n1 0 2\n2 3 1\n3 2 0",
"output": "1\n1\n3\n0"
},
{
"input": "3 2 2\n1 2\n1 2\n2 2",
"output": "1\n1\n0"
},
{
"input": "1 1 1\n0",
"output": "0"
},
{
"input": "1 1 1\n1",
"output": "0"
},
{
"input": "2 1 1\n1\n1",
"output": "1\n1"
},
... | 77 | 307,200 | 3 | 61,715 | |
0 | none | [
"none"
] | D. Falling Anvils | 2 | 256 | For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all.
Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who throws anvils from the sky? From what height? We are sure that such questions have never troubled you!
It turns out that throwing an anvil properly is not an easy task at all. Let's describe one of the most popular anvil throwing models.
Let the height *p* of the potential victim vary in the range [0;*a*] and the direction of the wind *q* vary in the range [<=-<=*b*;*b*]. *p* and *q* could be any real (floating) numbers. Then we can assume that the anvil will fit the toon's head perfectly only if the following equation has at least one real root:
Determine the probability with which an aim can be successfully hit by an anvil.
You can assume that the *p* and *q* coefficients are chosen equiprobably and independently in their ranges. | The first line contains integer *t* (1<=β€<=*t*<=β€<=10000) β amount of testcases.
Each of the following *t* lines contain two space-separated integers *a* and *b* (0<=β€<=*a*,<=*b*<=β€<=106).
Pretests contain all the tests with 0<=<<=*a*<=<<=10,<=0<=β€<=*b*<=<<=10. | Print *t* lines β the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10<=-<=6. | [
"2\n4 2\n1 2\n"
] | [
"0.6250000000\n0.5312500000\n"
] | none | [] | 154 | 0 | 0 | 61,974 |
843 | Maximum Flow | [
"flows",
"graphs"
] | null | null | You are given a directed graph, consisting of *n* vertices and *m* edges. The vertices *s* and *t* are marked as source and sink correspondingly. Additionally, there are no edges ending at *s* and there are no edges beginning in *t*.
The graph was constructed in a following way: initially each edge had capacity *c**i*<=><=0. A maximum flow with source at *s* and sink at *t* was constructed in this flow network. Let's denote *f**i* as the value of flow passing through edge with index *i*. Next, all capacities *c**i* and flow value *f**i* were erased. Instead, indicators *g**i* were written on edges β if flow value passing through edge *i* was positive, i.e. 1 if *f**i*<=><=0 and 0 otherwise.
Using the graph and values *g**i*, find out what is the minimum possible number of edges in the initial flow network that could be saturated (the passing flow is equal to capacity, i.e. *f**i*<==<=*c**i*). Also construct the corresponding flow network with maximum flow in it.
A flow in directed graph is described by flow values *f**i* on each of the edges so that the following conditions are satisfied:
- for each vertex, except source and sink, total incoming flow and total outcoming flow are equal, - for each edge 0<=β€<=*f**i*<=β€<=*c**i*
A flow is maximum if the difference between the sum of flow values on edges from the source, and the sum of flow values on edges to the source (there are no such in this problem), is maximum possible. | The first line of input data contains four positive integers *n*,<=*m*,<=*s*,<=*t* (2<=β€<=*n*<=β€<=100, 1<=β€<=*m*<=β€<=1000, 1<=β€<=*s*,<=*t*<=β€<=*n*, *s*<=β <=*t*) β the number of vertices, the number of edges, index of source vertex and index of sink vertex correspondingly.
Each of next *m* lines of input data contain non-negative integers *u**i*, *v**i*, *g**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, ) β the beginning of edge *i*, the end of edge *i* and indicator, which equals to 1 if flow value passing through edge *i* was positive and 0 if not.
It's guaranteed that no edge connects vertex with itself. Also it's guaranteed that there are no more than one edge between each ordered pair of vertices and that there exists at least one network flow that satisfies all the constrains from input data. | In the first line print single non-negative integer *k* β minimum number of edges, which should be saturated in maximum flow.
In each of next *m* lines print two integers *f**i*,<=*c**i* (1<=β€<=*c**i*<=β€<=109, 0<=β€<=*f**i*<=β€<=*c**i*) β the flow value passing through edge *i* and capacity of edge *i*.
This data should form a correct maximum flow in flow network. Also there must be exactly *k* edges with statement *f**i*<==<=*c**i* satisfied. Also statement *f**i*<=><=0 must be true if and only if *g**i*<==<=1.
If there are several possible answers, print any of them. | [
"5 6 1 5\n1 2 1\n2 3 1\n3 5 1\n1 4 1\n4 3 0\n4 5 1\n"
] | [
"2\n3 3\n3 8\n3 4\n4 4\n0 5\n4 9\n"
] | The illustration for second sample case. The saturated edges are marked dark, while edges with *g*<sub class="lower-index">*i*</sub>β=β0 are marked with dotted line. The integer on edge is the index of this edge in input list. <img class="tex-graphics" src="https://espresso.codeforces.com/a80cb6067d5d58458863f416f6ac95ad991a308a.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 1,000 | 9,011,200 | 0 | 62,160 | |
4 | Mysterious Present | [
"dp",
"sortings"
] | D. Mysterious Present | 1 | 64 | Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes *A*<==<={*a*1,<=<=*a*2,<=<=...,<=<=*a**n*}, where the width and the height of the *i*-th envelope is strictly higher than the width and the height of the (*i*<=<=-<=<=1)-th envelope respectively. Chain size is the number of envelopes in the chain.
Peter wants to make the chain of the maximum size from the envelopes he has, the chain should be such, that he'll be able to put a card into it. The card fits into the chain if its width and height is lower than the width and the height of the smallest envelope in the chain respectively. It's forbidden to turn the card and the envelopes.
Peter has very many envelopes and very little time, this hard task is entrusted to you. | The first line contains integers *n*, *w*, *h* (1<=<=β€<=*n*<=β€<=5000, 1<=β€<=*w*,<=<=*h*<=<=β€<=106) β amount of envelopes Peter has, the card width and height respectively. Then there follow *n* lines, each of them contains two integer numbers *w**i* and *h**i* β width and height of the *i*-th envelope (1<=β€<=*w**i*,<=<=*h**i*<=β€<=106). | In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, print any of the answers.
If the card does not fit into any of the envelopes, print number 0 in the single line. | [
"2 1 1\n2 2\n2 2\n",
"3 3 3\n5 4\n12 11\n9 8\n"
] | [
"1\n1 \n",
"3\n1 3 2 \n"
] | none | [
{
"input": "2 1 1\n2 2\n2 2",
"output": "1\n1 "
},
{
"input": "3 3 3\n5 4\n12 11\n9 8",
"output": "3\n1 3 2 "
},
{
"input": "5 10 10\n22 23\n17 19\n13 17\n8 12\n2 6",
"output": "3\n3 2 1 "
},
{
"input": "5 13 13\n4 4\n10 10\n7 7\n1 1\n13 13",
"output": "0"
},
{
"i... | 77 | 0 | 0 | 62,436 |
350 | Resort | [
"graphs"
] | null | null | Valera's finally decided to go on holiday! He packed up and headed for a ski resort.
Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has *n* objects (we will consider the objects indexed in some way by integers from 1 to *n*), each object is either a hotel or a mountain.
Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object *v*, the resort has at most one object *u*, such that there is a ski track built from object *u* to object *v*. We also know that no hotel has got a ski track leading from the hotel to some object.
Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects *v*1,<=*v*2,<=...,<=*v**k* (*k*<=β₯<=1) and meet the following conditions:
1. Objects with numbers *v*1,<=*v*2,<=...,<=*v**k*<=-<=1 are mountains and the object with number *v**k* is the hotel. 1. For any integer *i* (1<=β€<=*i*<=<<=*k*), there is exactly one ski track leading from object *v**i*. This track goes to object *v**i*<=+<=1. 1. The path contains as many objects as possible (*k* is maximal).
Help Valera. Find such path that meets all the criteria of our hero! | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of objects.
The second line contains *n* space-separated integers *type*1,<=*type*2,<=...,<=*type**n* β the types of the objects. If *type**i* equals zero, then the *i*-th object is the mountain. If *type**i* equals one, then the *i*-th object is the hotel. It is guaranteed that at least one object is a hotel.
The third line of the input contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=*n*) β the description of the ski tracks. If number *a**i* equals zero, then there is no such object *v*, that has a ski track built from *v* to *i*. If number *a**i* doesn't equal zero, that means that there is a track built from object *a**i* to object *i*. | In the first line print *k* β the maximum possible path length for Valera. In the second line print *k* integers *v*1,<=*v*2,<=...,<=*v**k* β the path. If there are multiple solutions, you can print any of them. | [
"5\n0 0 0 0 1\n0 1 2 3 4\n",
"5\n0 0 1 0 1\n0 1 2 2 4\n",
"4\n1 0 0 0\n2 3 4 2\n"
] | [
"5\n1 2 3 4 5\n",
"2\n4 5\n",
"1\n1\n"
] | none | [
{
"input": "5\n0 0 0 0 1\n0 1 2 3 4",
"output": "5\n1 2 3 4 5"
},
{
"input": "5\n0 0 1 0 1\n0 1 2 2 4",
"output": "2\n4 5"
},
{
"input": "4\n1 0 0 0\n2 3 4 2",
"output": "1\n1"
},
{
"input": "10\n0 0 0 0 0 0 0 0 0 1\n4 0 8 4 7 8 5 5 7 2",
"output": "2\n2 10"
},
{
... | 374 | 36,352,000 | 0 | 62,445 | |
508 | Tanya and Password | [
"dfs and similar",
"graphs"
] | null | null | While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of *n*<=+<=2 characters. She has written all the possible *n* three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the password out. Each three-letter substring was written the number of times it occurred in the password. Thus, Tanya ended up with *n* pieces of paper.
Then Tanya realized that dad will be upset to learn about her game and decided to restore the password or at least any string corresponding to the final set of three-letter strings. You have to help her in this difficult task. We know that dad's password consisted of lowercase and uppercase letters of the Latin alphabet and digits. Uppercase and lowercase letters of the Latin alphabet are considered distinct. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2Β·105), the number of three-letter substrings Tanya got.
Next *n* lines contain three letters each, forming the substring of dad's password. Each character in the input is a lowercase or uppercase Latin letter or a digit. | If Tanya made a mistake somewhere during the game and the strings that correspond to the given set of substrings don't exist, print "NO".
If it is possible to restore the string that corresponds to given set of substrings, print "YES", and then print any suitable password option. | [
"5\naca\naba\naba\ncab\nbac\n",
"4\nabc\nbCb\ncb1\nb13\n",
"7\naaa\naaa\naaa\naaa\naaa\naaa\naaa\n"
] | [
"YES\nabacaba\n",
"NO\n",
"YES\naaaaaaaaa\n"
] | none | [
{
"input": "5\naca\naba\naba\ncab\nbac",
"output": "YES\nabacaba"
},
{
"input": "4\nabc\nbCb\ncb1\nb13",
"output": "NO"
},
{
"input": "7\naaa\naaa\naaa\naaa\naaa\naaa\naaa",
"output": "YES\naaaaaaaaa"
},
{
"input": "1\nabc",
"output": "YES\nabc"
},
{
"input": "2\n... | 31 | 0 | 0 | 62,519 | |
109 | Lucky Probability | [
"brute force",
"probabilities"
] | B. Lucky Probability | 2 | 256 | Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer *p* from the interval [*p**l*,<=*p**r*] and Vasya chooses an integer *v* from the interval [*v**l*,<=*v**r*] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [*min*(*v*,<=*p*),<=*max*(*v*,<=*p*)] contains exactly *k* lucky numbers. | The single line contains five integers *p**l*, *p**r*, *v**l*, *v**r* and *k* (1<=β€<=*p**l*<=β€<=*p**r*<=β€<=109,<=1<=β€<=*v**l*<=β€<=*v**r*<=β€<=109,<=1<=β€<=*k*<=β€<=1000). | On the single line print the result with an absolute error of no more than 10<=-<=9. | [
"1 10 1 10 2\n",
"5 6 8 10 1\n"
] | [
"0.320000000000\n",
"1.000000000000\n"
] | Consider that [*a*,β*b*] denotes an interval of integers; this interval includes the boundaries. That is, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/18b4a6012d95ad18891561410f0314497a578d63.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In first case there are 32 suitable pairs: (1,β7),β(1,β8),β(1,β9),β(1,β10),β(2,β7),β(2,β8),β(2,β9),β(2,β10),β(3,β7),β(3,β8),β(3,β9),β(3,β10),β(4,β7),β(4,β8),β(4,β9),β(4,β10),β(7,β1),β(7,β2),β(7,β3),β(7,β4),β(8,β1),β(8,β2),β(8,β3),β(8,β4),β(9,β1),β(9,β2),β(9,β3),β(9,β4),β(10,β1),β(10,β2),β(10,β3),β(10,β4). Total number of possible pairs is 10Β·10β=β100, so answer is 32β/β100.
In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. | [
{
"input": "1 10 1 10 2",
"output": "0.320000000000"
},
{
"input": "5 6 8 10 1",
"output": "1.000000000000"
},
{
"input": "1 20 100 120 5",
"output": "0.150000000000"
},
{
"input": "1 10 1 10 3",
"output": "0.000000000000"
},
{
"input": "1 100 1 100 2",
"outpu... | 92 | 0 | 0 | 62,527 |
472 | Design Tutorial: Change the Goal | [
"constructive algorithms",
"math",
"matrices"
] | null | null | There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.
Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given *n* integers *x*1,<=*x*2,<=...,<=*x**n*. You are allowed to perform the assignments (as many as you want) of the following form *x**i* ^= *x**j* (in the original task *i* and *j* must be different, but in this task we allow *i* to equal *j*). The goal is to maximize the sum of all *x**i*.
Now we just change the goal. You are also given *n* integers *y*1,<=*y*2,<=...,<=*y**n*. You should make *x*1,<=*x*2,<=...,<=*x**n* exactly equal to *y*1,<=*y*2,<=...,<=*y**n*. In other words, for each *i* number *x**i* should be equal to *y**i*. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=10000). The second line contains *n* integers: *x*1 to *x**n* (0<=β€<=*x**i*<=β€<=109). The third line contains *n* integers: *y*1 to *y**n* (0<=β€<=*y**i*<=β€<=109). | If there is no solution, output -1.
If there is a solution, then in the first line output an integer *m* (0<=β€<=*m*<=β€<=1000000) β the number of assignments you need to perform. Then print *m* lines, each line should contain two integers *i* and *j* (1<=β€<=*i*,<=*j*<=β€<=*n*), which denote assignment *x**i* ^= *x**j*.
If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations. | [
"2\n3 5\n6 0\n",
"5\n0 0 0 0 0\n1 2 3 4 5\n",
"3\n4 5 6\n1 2 3\n",
"3\n1 2 3\n4 5 6\n"
] | [
"2\n1 2\n2 2\n",
"-1\n",
"5\n3 1\n1 2\n2 2\n2 3\n3 1\n",
"-1\n"
] | Assignment *a* ^= *b* denotes assignment *a* = *a* ^ *b*, where operation "^" is bitwise XOR of two integers. | [] | 62 | 0 | 0 | 62,529 | |
375 | Maximum Submatrix 2 | [
"data structures",
"dp",
"implementation",
"sortings"
] | null | null | You are given a matrix consisting of digits zero and one, its size is *n*<=Γ<=*m*. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations?
Let's assume that the rows of matrix *a* are numbered from 1 to *n* from top to bottom and the columns are numbered from 1 to *m* from left to right. A matrix cell on the intersection of the *i*-th row and the *j*-th column can be represented as (*i*,<=*j*). Formally, a submatrix of matrix *a* is a group of four integers *d*,<=*u*,<=*l*,<=*r* (1<=β€<=*d*<=β€<=*u*<=β€<=*n*;Β 1<=β€<=*l*<=β€<=*r*<=β€<=*m*). We will assume that the submatrix contains cells (*i*,<=*j*) (*d*<=β€<=*i*<=β€<=*u*;Β *l*<=β€<=*j*<=β€<=*r*). The area of the submatrix is the number of cells it contains. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=5000). Next *n* lines contain *m* characters each β matrix *a*. Matrix *a* only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines. | Print a single integer β the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0. | [
"1 1\n1\n",
"2 2\n10\n11\n",
"4 3\n100\n011\n000\n101\n"
] | [
"1\n",
"2\n",
"2\n"
] | none | [
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "2 2\n10\n11",
"output": "2"
},
{
"input": "4 3\n100\n011\n000\n101",
"output": "2"
},
{
"input": "11 16\n0111110101100011\n1000101100010000\n0010110110010101\n0110110010110010\n0011101101110000\n1001100011010111\n0010011111111... | 2,000 | 54,476,800 | 0 | 62,600 | |
718 | Sasha and Array | [
"data structures",
"math",
"matrices"
] | null | null | Sasha has an array of integers *a*1,<=*a*2,<=...,<=*a**n*. You have to perform *m* queries. There might be queries of two types:
1. 1 l r xΒ β increase all integers on the segment from *l* to *r* by values *x*; 1. 2 l rΒ β find , where *f*(*x*) is the *x*-th Fibonacci number. As this number may be large, you only have to find it modulo 109<=+<=7.
In this problem we define Fibonacci numbers as follows: *f*(1)<==<=1, *f*(2)<==<=1, *f*(*x*)<==<=*f*(*x*<=-<=1)<=+<=*f*(*x*<=-<=2) for all *x*<=><=2.
Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha? | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*<=β€<=100<=000, 1<=β€<=*m*<=β€<=100<=000)Β β the number of elements in the array and the number of queries respectively.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109).
Then follow *m* lines with queries descriptions. Each of them contains integers *tp**i*, *l**i*, *r**i* and may be *x**i* (1<=β€<=*tp**i*<=β€<=2, 1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*x**i*<=β€<=109). Here *tp**i*<==<=1 corresponds to the queries of the first type and *tp**i* corresponds to the queries of the second type.
It's guaranteed that the input will contains at least one query of the second type. | For each query of the second type print the answer modulo 109<=+<=7. | [
"5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5\n"
] | [
"5\n7\n9\n"
] | Initially, array *a* is equal to 1, 1, 2, 1, 1.
The answer for the first query of the second type is *f*(1)β+β*f*(1)β+β*f*(2)β+β*f*(1)β+β*f*(1)β=β1β+β1β+β1β+β1β+β1β=β5.
After the query 1 2 4 2 array *a* is equal to 1, 3, 4, 3, 1.
The answer for the second query of the second type is *f*(3)β+β*f*(4)β+β*f*(3)β=β2β+β3β+β2β=β7.
The answer for the third query of the second type is *f*(1)β+β*f*(3)β+β*f*(4)β+β*f*(3)β+β*f*(1)β=β1β+β2β+β3β+β2β+β1β=β9. | [
{
"input": "5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5",
"output": "5\n7\n9"
},
{
"input": "2 3\n1 3\n2 1 1\n1 1 2 3\n1 1 2 2",
"output": "1"
},
{
"input": "7 4\n2 2 1 1 3 3 2\n2 1 5\n2 6 7\n1 3 4 3\n2 6 6",
"output": "6\n3\n2"
},
{
"input": "9 4\n2 1 2 3 3 3 2 1 3\n2 1 8\n... | 15 | 0 | 0 | 62,740 | |
732 | Exams | [
"binary search",
"greedy",
"sortings"
] | null | null | Vasiliy has an exam period which will continue for *n* days. He has to pass exams on *m* subjects. Subjects are numbered from 1 to *m*.
About every day we know exam for which one of *m* subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day.
On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest.
About each subject Vasiliy know a number *a**i*Β β the number of days he should prepare to pass the exam number *i*. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during *a**i* days for the exam number *i*. He can mix the order of preparation for exams in any way.
Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105)Β β the number of days in the exam period and the number of subjects.
The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (0<=β€<=*d**i*<=β€<=*m*), where *d**i* is the number of subject, the exam of which can be passed on the day number *i*. If *d**i* equals 0, it is not allowed to pass any exams on the day number *i*.
The third line contains *m* positive integers *a*1,<=*a*2,<=...,<=*a**m* (1<=β€<=*a**i*<=β€<=105), where *a**i* is the number of days that are needed to prepare before passing the exam on the subject *i*. | Print one integerΒ β the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1. | [
"7 2\n0 1 0 2 1 0 2\n2 1\n",
"10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4\n",
"5 1\n1 1 1 1 1\n5\n"
] | [
"5\n",
"9\n",
"-1\n"
] | In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day.
In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day.
In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it. | [
{
"input": "7 2\n0 1 0 2 1 0 2\n2 1",
"output": "5"
},
{
"input": "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4",
"output": "9"
},
{
"input": "5 1\n1 1 1 1 1\n5",
"output": "-1"
},
{
"input": "100 10\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 ... | 139 | 12,492,800 | 3 | 62,800 | |
756 | Nikita and stack | [
"data structures"
] | null | null | Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operation push(x) puts an integer *x* on the top of the stack, and operation pop() deletes the top integer from the stack, i.Β e. the last added. If the stack is empty, then the operation pop() does nothing.
Nikita made *m* operations with the stack but forgot them. Now Nikita wants to remember them. He remembers them one by one, on the *i*-th step he remembers an operation he made *p**i*-th. In other words, he remembers the operations in order of some permutation *p*1,<=*p*2,<=...,<=*p**m*. After each step Nikita wants to know what is the integer on the top of the stack after performing the operations he have already remembered, in the corresponding order. Help him! | The first line contains the integer *m* (1<=β€<=*m*<=β€<=105)Β β the number of operations Nikita made.
The next *m* lines contain the operations Nikita remembers. The *i*-th line starts with two integers *p**i* and *t**i* (1<=β€<=*p**i*<=β€<=*m*, *t**i*<==<=0 or *t**i*<==<=1)Β β the index of operation he remembers on the step *i*, and the type of the operation. *t**i* equals 0, if the operation is pop(), and 1, is the operation is push(x). If the operation is push(x), the line also contains the integer *x**i* (1<=β€<=*x**i*<=β€<=106)Β β the integer added to the stack.
It is guaranteed that each integer from 1 to *m* is present exactly once among integers *p**i*. | Print *m* integers. The integer *i* should equal the number on the top of the stack after performing all the operations Nikita remembered on the steps from 1 to *i*. If the stack is empty after performing all these operations, print -1. | [
"2\n2 1 2\n1 0\n",
"3\n1 1 2\n2 1 3\n3 0\n",
"5\n5 0\n4 0\n3 1 1\n2 1 1\n1 1 2\n"
] | [
"2\n2\n",
"2\n3\n2\n",
"-1\n-1\n-1\n-1\n2\n"
] | In the first example, after Nikita remembers the operation on the first step, the operation push(2) is the only operation, so the answer is 2. After he remembers the operation pop() which was done before push(2), answer stays the same.
In the second example, the operations are push(2), push(3) and pop(). Nikita remembers them in the order they were performed.
In the third example Nikita remembers the operations in the reversed order. | [] | 31 | 0 | 0 | 62,945 | |
360 | Levko and Array | [
"binary search",
"dp"
] | null | null | Levko has an array that consists of integers: *a*1,<=*a*2,<=... ,<=*a**n*. But he doesnβt like this array at all.
Levko thinks that the beauty of the array *a* directly depends on value *c*(*a*), which can be calculated by the formula:
Itβs time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most *k* array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.
Help Levko and calculate what minimum number *c*(*a*) he can reach. | The first line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=2000). The second line contains space-separated integers *a*1,<=*a*2,<=... ,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109). | A single number β the minimum value of *c*(*a*) Levko can get. | [
"5 2\n4 7 4 7 4\n",
"3 1\n-100 0 100\n",
"6 3\n1 2 3 7 8 9\n"
] | [
"0\n",
"100\n",
"1\n"
] | In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.
In the third sample he can get array: 1, 2, 3, 4, 5, 6. | [
{
"input": "5 2\n4 7 4 7 4",
"output": "0"
},
{
"input": "3 1\n-100 0 100",
"output": "100"
},
{
"input": "6 3\n1 2 3 7 8 9",
"output": "1"
},
{
"input": "4 1\n-1000000000 -1000000000 1000000000 1000000000",
"output": "1000000000"
},
{
"input": "10 1\n-6 5 -7 -7 -... | 2,000 | 4,608,000 | 0 | 62,992 | |
993 | Compute Power | [
"binary search",
"dp",
"greedy"
] | null | null | You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume.
You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task can be any, the second task on each computer must use strictly less power than the first. You will assign between 1 and 2 tasks to each computer. You will then first execute the first task on each computer, wait for all of them to complete, and then execute the second task on each computer that has two tasks assigned.
If the average compute power per utilized processor (the sum of all consumed powers for all tasks presently running divided by the number of utilized processors) across all computers exceeds some unknown threshold during the execution of the first tasks, the entire system will blow up. There is no restriction on the second tasks execution. Find the lowest threshold for which it is possible.
Due to the specifics of the task, you need to print the answer multiplied by 1000 and rounded up. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=50) β the number of tasks.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=108), where *a**i* represents the amount of power required for the *i*-th task.
The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=100), where *b**i* is the number of processors that *i*-th task will utilize. | Print a single integer value β the lowest threshold for which it is possible to assign all tasks in such a way that the system will not blow up after the first round of computation, multiplied by 1000 and rounded up. | [
"6\n8 10 9 9 8 10\n1 1 1 1 1 1\n",
"6\n8 10 9 9 8 10\n1 10 5 5 1 10\n"
] | [
"9000\n",
"1160\n"
] | In the first example the best strategy is to run each task on a separate computer, getting average compute per processor during the first round equal to 9.
In the second task it is best to run tasks with compute 10 and 9 on one computer, tasks with compute 10 and 8 on another, and tasks with compute 9 and 8 on the last, averaging (10 + 10 + 9) / (10 + 10 + 5) = 1.16 compute power per processor during the first round. | [
{
"input": "6\n8 10 9 9 8 10\n1 1 1 1 1 1",
"output": "9000"
},
{
"input": "6\n8 10 9 9 8 10\n1 10 5 5 1 10",
"output": "1160"
},
{
"input": "1\n1\n100",
"output": "10"
},
{
"input": "50\n83 43 73 75 11 53 6 43 67 38 83 12 70 27 60 13 9 79 61 30 29 71 10 11 95 87 26 26 19 99 ... | 46 | 0 | 0 | 63,083 | |
681 | Gifts by the List | [
"constructive algorithms",
"dfs and similar",
"graphs",
"trees"
] | null | null | Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are *n* men in Sasha's family, so let's number them with integers from 1 to *n*.
Each man has at most one father but may have arbitrary number of sons.
Man number *A* is considered to be the ancestor of the man number *B* if at least one of the following conditions is satisfied:
- *A*<==<=*B*; - the man number *A* is the father of the man number *B*; - there is a man number *C*, such that the man number *A* is his ancestor and the man number *C* is the father of the man number *B*.
Of course, if the man number *A* is an ancestor of the man number *B* and *A*<=β <=*B*, then the man number *B* is not an ancestor of the man number *A*.
The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens.
1. A list of candidates is prepared, containing some (possibly all) of the *n* men in some order. 1. Each of the *n* men decides to give a gift. 1. In order to choose a person to give a gift to, man *A* looks through the list and picks the first man *B* in the list, such that *B* is an ancestor of *A* and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 1. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone.
This year you have decided to help in organizing celebration and asked each of the *n* men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above? | In the first line of the input two integers *n* and *m* (0<=β€<=*m*<=<<=*n*<=β€<=100<=000) are givenΒ β the number of the men in the Sasha's family and the number of family relations in it respectively.
The next *m* lines describe family relations: the (*i*<=+<=1)*th* line consists of pair of integers *p**i* and *q**i* (1<=β€<=*p**i*,<=*q**i*<=β€<=*n*, *p**i*<=β <=*q**i*) meaning that the man numbered *p**i* is the father of the man numbered *q**i*. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*), *i**th* of which means that the man numbered *i* wants to give a gift to the man numbered *a**i*. It is guaranteed that for every 1<=β€<=*i*<=β€<=*n* the man numbered *a**i* is an ancestor of the man numbered *i*. | Print an integer *k* (1<=β€<=*k*<=β€<=*n*)Β β the number of the men in the list of candidates, in the first line.
Print then *k* pairwise different positive integers not exceeding *n* β the numbers of the men in the list in an order satisfying every of the men's wishes, one per line.
If there are more than one appropriate lists, print any of them. If there is no appropriate list print <=-<=1 in the only line. | [
"3 2\n1 2\n2 3\n1 2 1\n",
"4 2\n1 2\n3 4\n1 2 3 3\n"
] | [
"-1",
"3\n2\n1\n3\n"
] | The first sample explanation:
- if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (*a*<sub class="lower-index">1</sub>β=β*a*<sub class="lower-index">3</sub>β=β1); - if there would be no 2 in the list then the second man wish would not be satisfied (*a*<sub class="lower-index">2</sub>β=β2); - if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (*a*<sub class="lower-index">2</sub>β=β2). - if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (*a*<sub class="lower-index">3</sub>β=β1). | [
{
"input": "3 2\n1 2\n2 3\n1 2 1",
"output": "-1"
},
{
"input": "4 2\n1 2\n3 4\n1 2 3 3",
"output": "3\n2\n1\n3"
},
{
"input": "1 0\n1",
"output": "1\n1"
},
{
"input": "2 1\n2 1\n2 2",
"output": "1\n2"
},
{
"input": "2 1\n2 1\n1 2",
"output": "2\n1\n2"
},
... | 1,000 | 19,046,400 | 0 | 63,374 | |
323 | Tournament-graph | [
"constructive algorithms",
"graphs"
] | null | null | In this problem you have to build tournament graph, consisting of *n* vertices, such, that for any oriented pair of vertices (*v*,<=*u*) (*v*<=β <=*u*) there exists a path from vertex *v* to vertex *u* consisting of no more then two edges.
A directed graph without self-loops is a tournament, if there is exactly one edge between any two distinct vertices (in one out of two possible directions). | The first line contains an integer *n* (3<=β€<=*n*<=β€<=1000), the number of the graph's vertices. | Print -1 if there is no graph, satisfying the described conditions.
Otherwise, print *n* lines with *n* integers in each. The numbers should be separated with spaces. That is adjacency matrix *a* of the found tournament. Consider the graph vertices to be numbered with integers from 1 to *n*. Then *a**v*,<=*u*<==<=0, if there is no edge from *v* to *u*, and *a**v*,<=*u*<==<=1 if there is one.
As the output graph has to be a tournament, following equalities must be satisfied:
- *a**v*,<=*u*<=+<=*a**u*,<=*v*<==<=1 for each *v*,<=*u* (1<=β€<=*v*,<=*u*<=β€<=*n*;Β *v*<=β <=*u*); - *a**v*,<=*v*<==<=0 for each *v* (1<=β€<=*v*<=β€<=*n*). | [
"3\n",
"4\n"
] | [
"0 1 0\n0 0 1\n1 0 0\n",
"-1\n"
] | none | [] | 78 | 102,400 | 0 | 63,654 | |
720 | Slalom | [
"data structures",
"dp",
"sortings"
] | null | null | Little girl Masha likes winter sports, today she's planning to take part in slalom skiing.
The track is represented as a grid composed of *n*<=Γ<=*m* squares. There are rectangular obstacles at the track, composed of grid squares. Masha must get from the square (1,<=1) to the square (*n*,<=*m*). She can move from a square to adjacent square: either to the right, or upwards. If the square is occupied by an obstacle, it is not allowed to move to that square.
One can see that each obstacle can actually be passed in two ways: either it is to the right of Masha's path, or to the left. Masha likes to try all ways to do things, so she would like to know how many ways are there to pass the track. Two ways are considered different if there is an obstacle such that it is to the right of the path in one way, and to the left of the path in the other way.
Help Masha to find the number of ways to pass the track. The number of ways can be quite big, so Masha would like to know it modulo 109<=+<=7.
The pictures below show different ways to pass the track in sample tests. | The first line of input data contains three positive integers: *n*, *m* and *k* (3<=β€<=*n*,<=*m*<=β€<=106, 0<=β€<=*k*<=β€<=105)Β β the size of the track and the number of obstacles.
The following *k* lines contain four positive integers each: *x*1, *y*1, *x*2, *y*2 (1<=β€<=*x*1<=β€<=*x*2<=β€<=*n*, 1<=β€<=*y*1<=β€<=*y*2<=β€<=*m*)Β β coordinates of bottom left, and top right squares of the obstacle.
It is guaranteed that there are no obstacles at squares (1,<=1) and (*n*,<=*m*), and no obstacles overlap (but some of them may touch). | Output one integerΒ β the number of ways to pass the track modulo 109<=+<=7. | [
"3 3 0\n",
"4 5 1\n2 2 3 4\n",
"5 5 3\n2 2 2 3\n4 2 5 2\n4 4 4 4\n"
] | [
"1\n",
"2\n",
"3\n"
] | none | [] | 30 | 0 | 0 | 63,736 | |
362 | Insertion Sort | [
"data structures",
"dp",
"implementation",
"math"
] | null | null | Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array *a* of size *n* in the non-decreasing order.
Petya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to *n*<=-<=1. He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.
It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=5000) β the length of the permutation. The second line contains *n* different integers from 0 to *n*<=-<=1, inclusive β the actual permutation. | Print two integers: the minimum number of times the swap function is executed and the number of such pairs (*i*,<=*j*) that swapping the elements of the input permutation with indexes *i* and *j* leads to the minimum number of the executions. | [
"5\n4 0 3 1 2\n",
"5\n1 2 3 4 0\n"
] | [
"3 2\n",
"3 4\n"
] | In the first sample the appropriate pairs are (0,β3) and (0,β4).
In the second sample the appropriate pairs are (0,β4), (1,β4), (2,β4) and (3,β4). | [
{
"input": "5\n4 0 3 1 2",
"output": "3 2"
},
{
"input": "5\n1 2 3 4 0",
"output": "3 4"
},
{
"input": "5\n1 3 4 0 2",
"output": "4 5"
},
{
"input": "10\n9 8 7 6 5 4 3 2 1 0",
"output": "28 1"
},
{
"input": "5\n0 4 1 3 2",
"output": "1 1"
},
{
"input":... | 31 | 307,200 | 0 | 63,846 | |
958 | Maximum Control (medium) | [
"data structures",
"dfs and similar",
"graphs",
"greedy",
"trees"
] | null | null | The Resistance is trying to take control over as many planets of a particular solar system as possible. Princess Heidi is in charge of the fleet, and she must send ships to some planets in order to maximize the number of controlled planets.
The Galaxy contains *N* planets, connected by bidirectional hyperspace tunnels in such a way that there is a unique path between every pair of the planets.
A planet is controlled by the Resistance if there is a Resistance ship in its orbit, or if the planet lies on the shortest path between some two planets that have Resistance ships in their orbits.
Heidi has not yet made up her mind as to how many ships to use. Therefore, she is asking you to compute, for every *K*<==<=1,<=2,<=3,<=...,<=*N*, the maximum number of planets that can be controlled with a fleet consisting of *K* ships. | The first line of the input contains an integer *N* (1<=β€<=*N*<=β€<=105) β the number of planets in the galaxy.
The next *N*<=-<=1 lines describe the hyperspace tunnels between the planets. Each of the *N*<=-<=1 lines contains two space-separated integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*N*) indicating that there is a bidirectional hyperspace tunnel between the planets *u* and *v*. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets. | On a single line, print *N* space-separated integers. The *K*-th number should correspond to the maximum number of planets that can be controlled by the Resistance using a fleet of *K* ships. | [
"3\n1 2\n2 3\n",
"4\n1 2\n3 2\n4 2\n"
] | [
"1 3 3 ",
"1 3 4 4 "
] | Consider the first example. If *K*β=β1, then Heidi can only send one ship to some planet and control it. However, for *K*ββ₯β2, sending ships to planets 1 and 3 will allow the Resistance to control all planets. | [
{
"input": "3\n1 2\n2 3",
"output": "1 3 3 "
},
{
"input": "4\n1 2\n3 2\n4 2",
"output": "1 3 4 4 "
},
{
"input": "19\n2 19\n7 15\n8 10\n16 1\n12 5\n11 5\n6 18\n12 14\n14 15\n2 6\n9 14\n4 17\n16 10\n4 2\n7 18\n3 2\n9 13\n11 10",
"output": "1 14 16 17 18 19 19 19 19 19 19 19 19 19 19 ... | 77 | 7,372,800 | 0 | 64,084 | |
0 | none | [
"none"
] | null | null | One must train much to do well on wizardry contests. So, there are numerous wizardry schools and magic fees.
One of such magic schools consists of *n* tours. A winner of each tour gets a huge prize. The school is organised quite far away, so one will have to take all the prizes home in one go. And the bags that you've brought with you have space for no more than *k* huge prizes.
Besides the fact that you want to take all the prizes home, you also want to perform well. You will consider your performance good if you win at least *l* tours.
In fact, years of organizing contests proved to the organizers that transporting huge prizes is an issue for the participants. Alas, no one has ever invented a spell that would shrink the prizes... So, here's the solution: for some tours the winner gets a bag instead of a huge prize. Each bag is characterized by number *a**i* β the number of huge prizes that will fit into it.
You already know the subject of all tours, so you can estimate the probability *p**i* of winning the *i*-th tour. You cannot skip the tour under any circumstances.
Find the probability that you will perform well on the contest and will be able to take all won prizes home (that is, that you will be able to fit all the huge prizes that you won into the bags that you either won or brought from home). | The first line contains three integers *n*, *l*, *k* (1<=β€<=*n*<=β€<=200,<=0<=β€<=*l*,<=*k*<=β€<=200) β the number of tours, the minimum number of tours to win, and the number of prizes that you can fit in the bags brought from home, correspondingly.
The second line contains *n* space-separated integers, *p**i* (0<=β€<=*p**i*<=β€<=100) β the probability to win the *i*-th tour, in percents.
The third line contains *n* space-separated integers, *a**i* (1<=β€<=*a**i*<=β€<=200) β the capacity of the bag that will be awarded to you for winning the *i*-th tour, or else -1, if the prize for the *i*-th tour is a huge prize and not a bag. | Print a single real number β the answer to the problem. The answer will be accepted if the absolute or relative error does not exceed 10<=-<=6. | [
"3 1 0\n10 20 30\n-1 -1 2\n",
"1 1 1\n100\n123\n"
] | [
"0.300000000000\n",
"1.000000000000\n"
] | In the first sample we need either win no tour or win the third one. If we win nothing we wouldn't perform well. So, we must to win the third tour. Other conditions will be satisfied in this case. Probability of wining the third tour is 0.3.
In the second sample we win the only tour with probability 1.0, and go back home with bag for it. | [
{
"input": "3 1 0\n10 20 30\n-1 -1 2",
"output": "0.300000000000"
},
{
"input": "1 1 1\n100\n123",
"output": "1.000000000000"
},
{
"input": "5 1 2\n36 44 13 83 63\n-1 2 -1 2 1",
"output": "0.980387276800"
},
{
"input": "9 9 2\n91 96 99 60 42 67 46 39 62\n5 -1 2 -1 -1 -1 7 -1 ... | 60 | 0 | 0 | 64,275 | |
959 | Mahmoud and Ehab and yet another xor task | [
"bitmasks",
"dp",
"math",
"matrices"
] | null | null | Ehab has an array *a* of *n* integers. He likes the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) and he likes to bother Mahmoud so he came up with a problem. He gave Mahmoud *q* queries. In each of them, he gave Mahmoud 2 integers *l* and *x*, and asked him to find the number of subsequences of the first *l* elements of the array such that their bitwise-xor sum is *x*. Can you help Mahmoud answer the queries?
A subsequence can contain elements that are not neighboring. | The first line contains integers *n* and *q* (1<=β€<=*n*,<=*q*<=β€<=105), the number of elements in the array and the number of queries.
The next line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=β€<=*a**i*<=<<=220), the elements of the array.
The next *q* lines, each contains integers *l* and *x* (1<=β€<=*l*<=β€<=*n*, 0<=β€<=*x*<=<<=220), representing the queries. | For each query, output its answer modulo 109<=+<=7 in a newline. | [
"5 5\n0 1 2 3 4\n4 3\n2 0\n3 7\n5 7\n5 8\n",
"3 2\n1 1 1\n3 1\n2 0\n"
] | [
"4\n2\n0\n4\n0\n",
"4\n2\n"
] | The bitwise-xor sum of the empty set is 0 and the bitwise-xor sum of a set containing one element is that element itself. | [
{
"input": "5 5\n0 1 2 3 4\n4 3\n2 0\n3 7\n5 7\n5 8",
"output": "4\n2\n0\n4\n0"
},
{
"input": "3 2\n1 1 1\n3 1\n2 0",
"output": "4\n2"
},
{
"input": "20 20\n353123 353123 677328 353123 0 997043 677328 420884 968991 566539 420884 801220 1030642 937882 762558 599450 196420 299659 101896 60... | 46 | 0 | 0 | 64,369 | |
842 | Nikita and game | [
"binary search",
"dfs and similar",
"divide and conquer",
"graphs",
"trees"
] | null | null | Nikita plays a new computer game. There are *m* levels in this game. In the beginning of each level a new class appears in the game; this class is a child-class of the class *y**i* (and *y**i* is called parent-class for this new class). Thus, the classes form a tree. Initially there is only one class with index 1.
Changing the class to its neighbour (child-class or parent-class) in the tree costs 1 coin. You can not change the class back. The cost of changing the class *a* to the class *b* is equal to the total cost of class changes on the path from *a* to *b* in the class tree.
Suppose that at *i*Β -th level the maximum cost of changing one class to another is *x*. For each level output the number of classes such that for each of these classes there exists some other class *y*, and the distance from this class to *y* is exactly *x*. | First line contains one integer number *m*Β β number of queries (1<=β€<=*m*<=β€<=3Β·105).
Next *m* lines contain description of queries. *i*Β -th line (1<=β€<=*i*<=β€<=*m*) describes the *i*Β -th level and contains an integer *y**i*Β β the index of the parent-class of class with index *i*<=+<=1 (1<=β€<=*y**i*<=β€<=*i*). | Suppose that at *i*Β -th level the maximum cost of changing one class to another is *x*. For each level output the number of classes such that for each of these classes there exists some other class *y*, and the distance from this class to *y* is exactly *x*. | [
"4\n1\n1\n2\n1\n",
"4\n1\n1\n2\n3\n"
] | [
"2\n2\n2\n3\n",
"2\n2\n2\n2\n"
] | none | [] | 30 | 0 | 0 | 64,481 | |
708 | Incorrect Flow | [
"flows"
] | null | null | At the entrance examination for the magistracy of the MSU Cyber-Mechanics Department Sasha got the question about Ford-Fulkerson algorithm. He knew the topic perfectly as he worked with it many times on programming competition. As the task for the question he was given a network with partially build flow that he had to use in order to demonstrate the workflow of the algorithm. He quickly finished to write the text and took a look at the problem only to understand that the given network is incorrect!
Suppose you are given a directed graph *G*(*V*,<=*E*) with two special nodes *s* and *t* called source and sink. We denote as *n* the number of nodes in the graph, i.e. *n*<==<=|*V*| and *m* stands for the number of directed edges in the graph, i.e. *m*<==<=|*E*|. For the purpose of this problem we always consider node 1 to be the source and node *n* to be the sink. In addition, for each edge of the graph *e* we define the capacity function *c*(*e*) and flow function *f*(*e*). Function *f*(*e*) represents the correct flow if the following conditions are satisfied:
1. For each edge the flow is non-negative and does not exceed capacity *c*(*e*), i.e. 0<=β€<=*f*(*e*)<=β€<=*c*(*e*). 1. For each node , that is not source or sink (*v*<=β <=*s* and *v*<=β <=*t*) the sum of flows of all edges going in *v* is equal to the sum of the flows among all edges going out from *v*. In other words, there is no flow stuck in *v*.
It was clear that as the exam was prepared last night and there are plenty of mistakes in the tasks. Sasha asked one of the professors to fix the network or give the correct task, but the reply was that the magistrate student should be able to fix the network himself. As the professor doesn't want the task to become easier, he asks Sasha to fix the network in a such way that the total number of changes is minimum possible. Sasha is not allowed to remove edges, add new ones or reverse the direction of existing edges. The only thing he is able to do is to change capacity function *c*(*e*) and flow function *f*(*e*). Moreover, all the values should remain non-negative integers. There is no requirement on the flow to be maximum in any sense.
Find the minimum possible total change of the functions *f*(*e*) and *c*(*e*) that Sasha has to make in order to make the flow correct. The total change is defined as the sum of absolute differences, i.e. if new functions are *f*<=*<=(*e*) and *c*<=*<=(*e*), then the total change is . | The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=100, 0<=β€<=*m*<=β€<=100)Β β the number of nodes and edges in the graph respectively. Each of the following *m* lines contains the description of the edges, consisting of four integers *u**i*, *v**i*, *c**i* and *f**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*, 0<=β€<=*c**i*,<=*f**i*<=β€<=1<=000<=000)Β β index of the node the edges starts from, the index of the node the edge goes to, current capacity and flow value.
Node number 1 is the source, and node number *n* is the sink. It's guaranteed that no edge goes to the source, and no edges starts in the sink.
Given graph contains no self-loops but may contain multiple edges. | Print one integerΒ β the minimum total sum of changes that Sasha has to do in order to get the correct flow description. | [
"2 1\n1 2 2 1\n",
"2 1\n1 2 1 2\n",
"3 3\n1 2 1 1\n2 3 2 2\n1 3 3 3\n",
"4 2\n2 3 1 1\n3 2 1 1\n"
] | [
"0\n",
"1\n",
"1\n",
"0\n"
] | In the first sample, the flow is initially correct. Note, that the flow is not maximum, but this is not required.
In the second sample, the flow value of the only edge is greater than its capacity. There are two ways to fix this: either increase the capacity up to 2 or reduce the flow down to 1.
In the third sample, there is only 1 unit of flow coming to vertex 2, but there are 2 units going out of it. One of the possible solutions is to reduce the value of the flow on the second edge by 1.
In the fourth sample, there is isolated circulation of flow, but this description is correct by definition. | [] | 30 | 0 | 0 | 64,592 | |
898 | Restoring the Expression | [
"brute force",
"hashing",
"math"
] | null | null | A correct expression of the form a+b=c was written; *a*, *b* and *c* are non-negative integers without leading zeros. In this expression, the plus and equally signs were lost. The task is to restore the expression. In other words, one character '+' and one character '=' should be inserted into given sequence of digits so that:
- character'+' is placed on the left of character '=', - characters '+' and '=' split the sequence into three non-empty subsequences consisting of digits (let's call the left part a, the middle partΒ β b and the right partΒ β c), - all the three parts a, b and c do not contain leading zeros, - it is true that a+b=c.
It is guaranteed that in given tests answer always exists. | The first line contains a non-empty string consisting of digits. The length of the string does not exceed 106. | Output the restored expression. If there are several solutions, you can print any of them.
Note that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be.
Do not separate numbers and operation signs with spaces. Strictly follow the output format given in the examples.
If you remove symbol '+' and symbol '=' from answer string you should get a string, same as string from the input data. | [
"12345168\n",
"099\n",
"199100\n",
"123123123456456456579579579\n"
] | [
"123+45=168\n",
"0+9=9\n",
"1+99=100\n",
"123123123+456456456=579579579\n"
] | none | [
{
"input": "12345168",
"output": "123+45=168"
},
{
"input": "099",
"output": "0+9=9"
},
{
"input": "199100",
"output": "1+99=100"
},
{
"input": "123123123456456456579579579",
"output": "123123123+456456456=579579579"
},
{
"input": "112",
"output": "1+1=2"
},... | 93 | 307,200 | 0 | 64,662 | |
220 | Little Elephant and Inversions | [
"data structures",
"two pointers"
] | null | null | The Little Elephant has array *a*, consisting of *n* positive integers, indexed from 1 to *n*. Let's denote the number with index *i* as *a**i*.
The Little Elephant wants to count, how many pairs of integers *l* and *r* are there, such that 1<=β€<=*l*<=<<=*r*<=β€<=*n* and sequence *b*<==<=*a*1*a*2... *a**l**a**r**a**r*<=+<=1... *a**n* has no more than *k* inversions.
An inversion in sequence *b* is a pair of elements of the sequence *b*, that change their relative order after a stable sorting of the sequence. In other words, an inversion is a pair of integers *i* and *j*, such that 1<=β€<=*i*<=<<=*j*<=β€<=|*b*| and *b**i*<=><=*b**j*, where |*b*| is the length of sequence *b*, and *b**j* is its *j*-th element.
Help the Little Elephant and count the number of the described pairs. | The first line contains two integers *n* and *k* (2<=β€<=*n*<=β€<=105,<=0<=β€<=*k*<=β€<=1018) β the size of array *a* and the maximum allowed number of inversions respectively. The next line contains *n* positive integers, separated by single spaces, *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β elements of array *a*.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. | In a single line print a single number β the answer to the problem. | [
"3 1\n1 3 2\n",
"5 2\n1 3 2 1 7\n"
] | [
"3\n",
"6\n"
] | none | [
{
"input": "3 1\n1 3 2",
"output": "3"
},
{
"input": "5 2\n1 3 2 1 7",
"output": "6"
},
{
"input": "7 3\n1 7 6 4 9 5 3",
"output": "6"
},
{
"input": "5 0\n1 2 3 4 5",
"output": "10"
},
{
"input": "2 1\n2 1",
"output": "1"
},
{
"input": "3 1000000000000... | 310 | 0 | 0 | 64,675 | |
630 | Indivisibility | [
"math",
"number theory"
] | null | null | IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following way. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is not divisible by any number from 2 to 10 every developer of this game gets a small bonus.
A game designer Petya knows that the company is just about to release a new game that was partly developed by him. On the basis of his experience he predicts that *n* people will buy the game during the first month. Now Petya wants to determine how many times he will get the bonus. Help him to know it. | The only line of the input contains one integer *n* (1<=β€<=*n*<=β€<=1018) β the prediction on the number of people who will buy the game. | Output one integer showing how many numbers from 1 to *n* are not divisible by any number from 2 to 10. | [
"12\n"
] | [
"2"
] | none | [
{
"input": "12",
"output": "2"
},
{
"input": "2519",
"output": "576"
},
{
"input": "2521",
"output": "577"
},
{
"input": "1",
"output": "1"
},
{
"input": "314159265",
"output": "71807832"
},
{
"input": "718281828459045235",
"output": "1641787036477... | 46 | 0 | 0 | 64,880 | |
409 | On a plane | [
"*special",
"geometry"
] | null | null | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=1000) β the number of points on a plane.
Each of the next *n* lines contains two real coordinates *x**i* and *y**i* of the point, specified with exactly 2 fractional digits. All coordinates are between <=-<=1000 and 1000, inclusive.
Output a single real number ΞΈ β the answer to the problem statement. The absolute or relative error of your answer should be at most 10<=-<=2. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=1000) β the number of points on a plane.
Each of the next *n* lines contains two real coordinates *x**i* and *y**i* of the point, specified with exactly 2 fractional digits. All coordinates are between <=-<=1000 and 1000, inclusive. | Output a single real number ΞΈ β the answer to the problem statement. The absolute or relative error of your answer should be at most 10<=-<=2. | [
"8\n-2.14 2.06\n-1.14 2.04\n-2.16 1.46\n-2.14 0.70\n-1.42 0.40\n-0.94 -0.48\n-1.42 -1.28\n-2.16 -1.62\n",
"5\n2.26 1.44\n2.28 0.64\n2.30 -0.30\n1.58 0.66\n3.24 0.66\n",
"8\n6.98 2.06\n6.40 1.12\n5.98 0.24\n5.54 -0.60\n7.16 0.30\n7.82 1.24\n8.34 0.24\n8.74 -0.76\n",
"5\n10.44 2.06\n10.90 0.80\n11.48 -0.48\n12.... | [
"5.410\n",
"5.620\n",
"5.480\n",
"6.040\n",
"6.040\n",
"6.720\n"
] | none | [
{
"input": "8\n-2.14 2.06\n-1.14 2.04\n-2.16 1.46\n-2.14 0.70\n-1.42 0.40\n-0.94 -0.48\n-1.42 -1.28\n-2.16 -1.62",
"output": "5.410"
},
{
"input": "5\n2.26 1.44\n2.28 0.64\n2.30 -0.30\n1.58 0.66\n3.24 0.66",
"output": "5.620"
},
{
"input": "8\n6.98 2.06\n6.40 1.12\n5.98 0.24\n5.54 -0.60\... | 109 | 307,200 | 3 | 65,294 | |
37 | Trial for Chief | [
"graphs",
"greedy",
"shortest paths"
] | E. Trial for Chief | 2 | 256 | Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chroniclers of that time. For example, they learned how the chief of the ancient Berland tribe was chosen.
As soon as enough pretenders was picked, the following test took place among them: the chief of the tribe took a slab divided by horizontal and vertical stripes into identical squares (the slab consisted of *N* lines and *M* columns) and painted every square black or white. Then every pretender was given a slab of the same size but painted entirely white. Within a day a pretender could paint any side-linked set of the squares of the slab some color. The set is called linked if for any two squares belonging to the set there is a path belonging the set on which any two neighboring squares share a side. The aim of each pretender is to paint his slab in the exactly the same way as the chiefβs slab is painted. The one who paints a slab like that first becomes the new chief.
Scientists found the slab painted by the ancient Berland tribe chief. Help them to determine the minimal amount of days needed to find a new chief if he had to paint his slab in the given way. | The first line contains two integers *N* and *M* (1<=β€<=*N*,<=*M*<=β€<=50) β the number of lines and columns on the slab. The next *N* lines contain *M* symbols each β the final coloration of the slab. *W* stands for the square that should be painted white and *B* β for the square that should be painted black. | In the single line output the minimal number of repaintings of side-linked areas needed to get the required coloration of the slab. | [
"3 3\nWBW\nBWB\nWBW\n",
"2 3\nBBB\nBWB\n"
] | [
"2\n",
"1\n"
] | none | [
{
"input": "3 3\nWBW\nBWB\nWBW",
"output": "2"
},
{
"input": "2 3\nBBB\nBWB",
"output": "1"
},
{
"input": "9 29\nBWBBBBBBBBBWBWWBBBWBWBBBWWBWW\nWBWBBWBBWBWWBWBBBWBWWWBWBBBBB\nBWBBBBWWBBBWBWBBWWBBWBBBBBBBB\nBBBWWBBWWBBBWBWBBBWWWWWWBBBBW\nBBWWWWBBBBBBBBBWBBBBBBBBBBWBW\nBBBWWBBBBWBBBWWBBBWB... | 62 | 0 | 0 | 65,381 |
58 | Calendar | [
"greedy",
"strings"
] | D. Calendar | 2 | 256 | BerOilGasDiamondBank has branches in *n* cities, at that *n* is an even number. The bank management wants to publish a calendar with the names of all those cities written in two columns: the calendar should consist of exactly *n*<=/<=2 lines of strictly equal length, each of which contains exactly two names and exactly one separator character between them. The name of every city should be used in the calendar exactly once. For historical reasons the symbol *d* is used as the separator of words in the calendar.
The BerOilGasDiamondBank management wants to show that all its branches are equally important to it, that's why the order of their appearance in the calendar should be following: if we "glue"(concatinate) all the *n*<=/<=2 calendar lines (from top to bottom) to make a single line, then the lexicographically minimal line is obtained. No separator character will be used to separate calendar lines. For example, if the lines are "bertown!berville", "newberville!bera", then the resulting line is "bertown!bervillenewberville!bera". In some sense one has to find the lexicographically minimal calendar, where the comparison of calendars happens line by line.
Help BerOilGasDiamondBank and construct the required calendar. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=104, *n* is even) which is the number of branches. Then follow *n* lines which are the names of the cities. All the names consist of lowercase Latin letters; their lengths are no less than 1 and no more than 10 symbols. The next line contains a single symbol *d* (*d* has an ASCII-code from 33 to 126 inclusively, excluding lowercase Latin letters) which is the separator between words in the calendar lines. It is guaranteed that the calendar is possible to be constructed and all the names are different. | Print *n*<=/<=2 lines of similar length which are the required calendar. Every line should contain exactly two words and exactly one separator between them. If there are several solutions, print the lexicographically minimal one. The lexicographical comparison of lines is realized by the "<" operator in the modern programming languages. | [
"4\nb\naa\nhg\nc\n.\n",
"2\naa\na\n!\n",
"2\naa\na\n|\n"
] | [
"aa.b\nc.hg\n",
"a!aa\n",
"aa|a\n"
] | none | [
{
"input": "4\nb\naa\nhg\nc\n.",
"output": "aa.b\nc.hg"
},
{
"input": "2\naa\na\n!",
"output": "a!aa"
},
{
"input": "2\naa\na\n|",
"output": "aa|a"
},
{
"input": "4\nqhcivbxotj\nirgxzzxvw\npxdmcyszvk\nyyaevcdal\n~",
"output": "irgxzzxvw~pxdmcyszvk\nqhcivbxotj~yyaevcdal"
... | 186 | 0 | 0 | 65,410 |
611 | New Year and Cake | [
"geometry",
"two pointers"
] | null | null | Limak is a little polar bear. According to some old traditions, his bear family prepared a New Year cake. And Limak likes cakes.
As you may know, a New Year cake is a strictly convex polygon with *n* vertices.
Parents won't allow Limak to eat more than half of a cake because he would get sick. After some thinking they decided to cut a cake along one of *n*Β·(*n*<=-<=3)<=/<=2 diagonals. Then Limak will get a non-greater piece.
Limak understands rules but he won't be happy if the second piece happens to be much bigger. Limak's disappointment will be equal to the difference between pieces' areas, multiplied by two. It can be proved that it will be integer for the given constraints.
There are *n*Β·(*n*<=-<=3)<=/<=2 possible scenarios. Consider them all and find the sum of values of Limak's disappointment, modulo 109<=+<=7. | The first line of the input contains a single integer *n* (4<=β€<=*n*<=β€<=500<=000)Β β the number of vertices in the polygon denoting the cake.
Each of the next *n* lines contains two integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=β€<=109)Β β coordinates of the *i*-th point.
It's guaranteed that all points are distinct, polygon is strictly convex and points are given in the clockwise order. | Print the sum of values of Limak's disappointment over all possible scenarios modulo 109<=+<=7. | [
"5\n2 4\n2 7\n5 7\n5 4\n3 -2\n",
"4\n-1000000000 -5000000\n0 1234567\n1 1\n-5 -100000000\n",
"8\n-10 0\n-6 6\n0 10\n6 6\n10 0\n6 -6\n0 -10\n-6 -6\n"
] | [
"90\n",
"525185196\n",
"5216\n"
] | In the first sample possible values of Limak's disappointment are 0,β18,β18,β24,β30. | [] | 2,000 | 43,622,400 | 0 | 65,765 | |
216 | Martian Luck | [
"math",
"number theory"
] | null | null | You know that the Martians use a number system with base *k*. Digit *b* (0<=β€<=*b*<=<<=*k*) is considered lucky, as the first contact between the Martians and the Earthlings occurred in year *b* (by Martian chronology).
A digital root *d*(*x*) of number *x* is a number that consists of a single digit, resulting after cascading summing of all digits of number *x*. Word "cascading" means that if the first summing gives us a number that consists of several digits, then we sum up all digits again, and again, until we get a one digit number.
For example, *d*(35047)<==<=*d*((3<=+<=5<=+<=0<=+<=4)7)<==<=*d*(157)<==<=*d*((1<=+<=5)7)<==<=*d*(67)<==<=67. In this sample the calculations are performed in the 7-base notation.
If a number's digital root equals *b*, the Martians also call this number lucky.
You have string *s*, which consists of *n* digits in the *k*-base notation system. Your task is to find, how many distinct substrings of the given string are lucky numbers. Leading zeroes are permitted in the numbers.
Note that substring *s*[*i*... *j*] of the string *s*<==<=*a*1*a*2... *a**n* (1<=β€<=*i*<=β€<=*j*<=β€<=*n*) is the string *a**i**a**i*<=+<=1... *a**j*. Two substrings *s*[*i*1... *j*1] and *s*[*i*2... *j*2] of the string *s* are different if either *i*1<=β <=*i*2 or *j*1<=β <=*j*2. | The first line contains three integers *k*, *b* and *n* (2<=β€<=*k*<=β€<=109, 0<=β€<=*b*<=<<=*k*, 1<=β€<=*n*<=β€<=105).
The second line contains string *s* as a sequence of *n* integers, representing digits in the *k*-base notation: the *i*-th integer equals *a**i* (0<=β€<=*a**i*<=<<=*k*) β the *i*-th digit of string *s*. The numbers in the lines are space-separated. | Print a single integer β the number of substrings that are lucky numbers.
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. | [
"10 5 6\n3 2 0 5 6 1\n",
"7 6 4\n3 5 0 4\n",
"257 0 3\n0 0 256\n"
] | [
"5",
"1",
"3"
] | In the first sample the following substrings have the sought digital root: *s*[1... 2] = "3 2", *s*[1... 3] = "3 2 0", *s*[3... 4] = "0 5", *s*[4... 4] = "5" and *s*[2... 6] = "2 0 5 6 1". | [
{
"input": "10 5 6\n3 2 0 5 6 1",
"output": "5"
},
{
"input": "7 6 4\n3 5 0 4",
"output": "1"
},
{
"input": "257 0 3\n0 0 256",
"output": "3"
},
{
"input": "2 1 1\n0",
"output": "0"
},
{
"input": "2 0 20\n1 1 1 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 1",
"output": "22... | 92 | 0 | 0 | 65,969 | |
31 | Chocolate | [
"dfs and similar",
"implementation"
] | D. Chocolate | 2 | 256 | Bob has a rectangular chocolate bar of the size *W*<=Γ<=*H*. He introduced a cartesian coordinate system so that the point (0,<=0) corresponds to the lower-left corner of the bar, and the point (*W*,<=*H*) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line *x*<==<=*x**c* or *y*<==<=*y**c*, where *x**c* and *y**c* are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made *n* breaks and wrote them down in his notebook in arbitrary order. At the end he got *n*<=+<=1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task. | The first line contains 3 integers *W*, *H* and *n* (1<=β€<=*W*,<=*H*,<=*n*<=β€<=100) β width of the bar, height of the bar and amount of breaks. Each of the following *n* lines contains four integers *x**i*,<=1,<=*y**i*,<=1,<=*x**i*,<=2,<=*y**i*,<=2 β coordinates of the endpoints of the *i*-th break (0<=β€<=*x**i*,<=1<=β€<=*x**i*,<=2<=β€<=*W*,<=0<=β€<=*y**i*,<=1<=β€<=*y**i*,<=2<=β€<=*H*, or *x**i*,<=1<==<=*x**i*,<=2, or *y**i*,<=1<==<=*y**i*,<=2). Breaks are given in arbitrary order.
It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts. | Output *n*<=+<=1 numbers β areas of the resulting parts in the increasing order. | [
"2 2 2\n1 0 1 2\n0 1 1 1\n",
"2 2 3\n1 0 1 2\n0 1 1 1\n1 1 2 1\n",
"2 4 2\n0 1 2 1\n0 3 2 3\n"
] | [
"1 1 2 ",
"1 1 1 1 ",
"2 2 4 "
] | none | [
{
"input": "2 2 2\n1 0 1 2\n0 1 1 1",
"output": "1 1 2 "
},
{
"input": "2 2 3\n1 0 1 2\n0 1 1 1\n1 1 2 1",
"output": "1 1 1 1 "
},
{
"input": "2 4 2\n0 1 2 1\n0 3 2 3",
"output": "2 2 4 "
},
{
"input": "5 5 3\n2 1 2 5\n0 1 5 1\n4 0 4 1",
"output": "1 4 8 12 "
},
{
... | 374 | 2,252,800 | 3.902304 | 66,034 |
494 | Obsessive String | [
"dp",
"strings"
] | null | null | Hamed has recently found a string *t* and suddenly became quite fond of it. He spent several days trying to find all occurrences of *t* in other strings he had. Finally he became tired and started thinking about the following problem. Given a string *s* how many ways are there to extract *k*<=β₯<=1 non-overlapping substrings from it such that each of them contains string *t* as a substring? More formally, you need to calculate the number of ways to choose two sequences *a*1,<=*a*2,<=...,<=*a**k* and *b*1,<=*b*2,<=...,<=*b**k* satisfying the following requirements:
- *k*<=β₯<=1 - - - - Β Β *t* is a substring of string *s**a**i**s**a**i*<=+<=1... *s**b**i* (string *s* is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109<=+<=7. | Input consists of two lines containing strings *s* and *t* (1<=β€<=|*s*|,<=|*t*|<=β€<=105). Each string consists of lowercase Latin letters. | Print the answer in a single line. | [
"ababa\naba\n",
"welcometoroundtwohundredandeightytwo\nd\n",
"ddd\nd\n"
] | [
"5\n",
"274201\n",
"12\n"
] | none | [
{
"input": "ababa\naba",
"output": "5"
},
{
"input": "welcometoroundtwohundredandeightytwo\nd",
"output": "274201"
},
{
"input": "ddd\nd",
"output": "12"
},
{
"input": "vnssnssnssnssnssnssnssnssnssnssnssnssnssnssnssnssn\nnssnssns",
"output": "943392"
},
{
"input":... | 2,000 | 28,876,800 | 0 | 66,125 | |
580 | Kefa and Dishes | [
"bitmasks",
"dp"
] | null | null | When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were *n* dishes. Kefa knows that he needs exactly *m* dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible.
Kefa knows that the *i*-th dish gives him *a**i* units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself *k* rules of eating food of the following type β if he eats dish *x* exactly before dish *y* (there should be no other dishes between *x* and *y*), then his satisfaction level raises by *c*.
Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task! | The first line of the input contains three space-separated numbers, *n*, *m* and *k* (1<=β€<=*m*<=β€<=*n*<=β€<=18, 0<=β€<=*k*<=β€<=*n*<=*<=(*n*<=-<=1)) β the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.
The second line contains *n* space-separated numbers *a**i*, (0<=β€<=*a**i*<=β€<=109) β the satisfaction he gets from the *i*-th dish.
Next *k* lines contain the rules. The *i*-th rule is described by the three numbers *x**i*, *y**i* and *c**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*, 0<=β€<=*c**i*<=β€<=109). That means that if you eat dish *x**i* right before dish *y**i*, then the Kefa's satisfaction increases by *c**i*. It is guaranteed that there are no such pairs of indexes *i* and *j* (1<=β€<=*i*<=<<=*j*<=β€<=*k*), that *x**i*<==<=*x**j* and *y**i*<==<=*y**j*. | In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant. | [
"2 2 1\n1 1\n2 1 1\n",
"4 3 2\n1 2 3 4\n2 1 5\n3 4 2\n"
] | [
"3\n",
"12\n"
] | In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule.
In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get an additional satisfaction 5. | [
{
"input": "2 2 1\n1 1\n2 1 1",
"output": "3"
},
{
"input": "4 3 2\n1 2 3 4\n2 1 5\n3 4 2",
"output": "12"
},
{
"input": "1 1 0\n1000000000",
"output": "1000000000"
},
{
"input": "4 2 1\n100 1 2 200\n2 3 400",
"output": "403"
},
{
"input": "7 4 3\n2 6 13 5 7 1 9\n... | 0 | 0 | -1 | 66,191 | |
0 | none | [
"none"
] | null | null | In the year 2500 the annual graduation ceremony in the German University in Cairo (GUC) has run smoothly for almost 500 years so far.
The most important part of the ceremony is related to the arrangement of the professors in the ceremonial hall.
Traditionally GUC has *n* professors. Each professor has his seniority level. All seniorities are different. Let's enumerate the professors from 1 to *n*, with 1 being the most senior professor and *n* being the most junior professor.
The ceremonial hall has *n* seats, one seat for each professor. Some places in this hall are meant for more senior professors than the others. More specifically, *m* pairs of seats are in "senior-junior" relation, and the tradition requires that for all *m* pairs of seats (*a**i*,<=*b**i*) the professor seated in "senior" position *a**i* should be more senior than the professor seated in "junior" position *b**i*.
GUC is very strict about its traditions, which have been carefully observed starting from year 2001. The tradition requires that:
- The seating of the professors changes every year. - Year 2001 ceremony was using lexicographically first arrangement of professors in the ceremonial hall. - Each consecutive year lexicographically next arrangement of the professors is used.
The arrangement of the professors is the list of *n* integers, where the first integer is the seniority of the professor seated in position number one, the second integer is the seniority of the professor seated in position number two, etc.
Given *n*, the number of professors, *y*, the current year and *m* pairs of restrictions, output the arrangement of the professors for this year. | The first line contains three integers *n*, *y* and *m* (1<=β€<=*n*<=β€<=16,<=2001<=β€<=*y*<=β€<=1018,<=0<=β€<=*m*<=β€<=100) β the number of professors, the year for which the arrangement should be computed, and the number of pairs of seats for which the seniority relation should be kept, respectively.
The next *m* lines contain one pair of integers each, "*a**i* *b**i*", indicating that professor on the *a**i*-th seat is more senior than professor on the *b**i*-th seat (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*). Some pair may be listed more than once.
Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin stream (you may also use the %I64d specificator). | Print the order in which the professors should be seated in the requested year.
If by this year the GUC would have ran out of arrangements, or the given "senior-junior" relation are contradictory, print "The times have changed" (without quotes). | [
"3 2001 2\n1 2\n2 3\n",
"7 2020 6\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10 3630801 0\n",
"3 2001 3\n1 2\n2 3\n3 1\n"
] | [
"1 2 3\n",
"1 2 3 7 4 6 5\n",
"The times have changed\n",
"The times have changed\n"
] | In the first example the lexicographically first order of seating is 1 2 3.
In the third example the GUC will run out of arrangements after the year 3630800.
In the fourth example there are no valid arrangements for the seating.
The lexicographical comparison of arrangements is performed by the < operator in modern programming languages. The arrangement *a* is lexicographically less that the arrangement *b*, if there exists such *i* (1ββ€β*i*ββ€β*n*), that *a*<sub class="lower-index">*i*</sub>β<β*b*<sub class="lower-index">*i*</sub>, and for any *j* (1ββ€β*j*β<β*i*) *a*<sub class="lower-index">*j*</sub>β=β*b*<sub class="lower-index">*j*</sub>. | [] | 92 | 0 | 0 | 66,200 | |
9 | Interestring graph and Apples | [
"dfs and similar",
"dsu",
"graphs"
] | E. Interesting Graph and Apples | 1 | 64 | Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life Β«interesting graph and applesΒ». An undirected graph is called interesting, if each of its vertices belongs to one cycle only β a funny ring β and does not belong to any other cycles. A funny ring is a cycle that goes through all the vertices just once. Moreover, loops are funny rings too.
She has already drawn the apples and some of the graph edges. But now it is not clear, how to connect the rest of the vertices to get an interesting graph as a result. The answer should contain the minimal amount of added edges. And furthermore, the answer should be the lexicographically smallest one. The set of edges (*x*1,<=*y*1),<=(*x*2,<=*y*2),<=...,<=(*x**n*,<=*y**n*), where *x**i*<=β€<=*y**i*, is lexicographically smaller than the set (*u*1,<=*v*1),<=(*u*2,<=*v*2),<=...,<=(*u**n*,<=*v**n*), where *u**i*<=β€<=*v**i*, provided that the sequence of integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=...,<=*x**n*,<=*y**n* is lexicographically smaller than the sequence *u*1,<=*v*1,<=*u*2,<=*v*2,<=...,<=*u**n*,<=*v**n*. If you do not cope, Hexadecimal will eat you. ...eat you alive. | The first line of the input data contains a pair of integers *n* and *m* (1<=β€<=*n*<=β€<=50, 0<=β€<=*m*<=β€<=2500) β the amount of vertices and edges respectively. The following lines contain pairs of numbers *x**i* and *y**i* (1<=β€<=*x**i*, *y**i*<=β€<=*n*) β the vertices that are already connected by edges. The initial graph may contain multiple edges and loops. | In the first line output Β«YESΒ» or Β«NOΒ»: if it is possible or not to construct an interesting graph. If the answer is Β«YESΒ», in the second line output *k* β the amount of edges that should be added to the initial graph. Finally, output *k* lines: pairs of vertices *x**j* and *y**j*, between which edges should be drawn. The result may contain multiple edges and loops. *k* can be equal to zero. | [
"3 2\n1 2\n2 3\n"
] | [
"YES\n1\n1 3\n"
] | none | [
{
"input": "3 2\n1 2\n2 3",
"output": "YES\n1\n1 3"
},
{
"input": "1 1\n1 1",
"output": "YES\n0"
},
{
"input": "1 2\n1 1\n1 1",
"output": "NO"
},
{
"input": "1 3\n1 1\n1 1\n1 1",
"output": "NO"
},
{
"input": "2 0",
"output": "YES\n2\n1 2\n1 2"
},
{
"in... | 186 | 512,000 | -1 | 66,256 |
0 | none | [
"none"
] | null | null | ΠΠ° ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ½ΠΎΠΉ ΠΏΡΡΠΌΠΎΠΉ ΡΠΈΠ΄ΠΈΡ *n* ΡΠΎΠ±Π°ΡΠ΅ΠΊ, *i*-Ρ ΡΠΎΠ±Π°ΡΠΊΠ° Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΡΠΎΡΠΊΠ΅ *x**i*. ΠΡΠΎΠΌΠ΅ ΡΠΎΠ³ΠΎ, Π½Π° ΠΏΡΡΠΌΠΎΠΉ Π΅ΡΡΡ *m* ΠΌΠΈΡΠΎΠΊ Ρ Π΅Π΄ΠΎΠΉ, Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ·Π²Π΅ΡΡΠ½Π° Π΅Ρ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° Π½Π° ΠΏΡΡΠΌΠΎΠΉ *u**j* ΠΈ Π²ΡΠ΅ΠΌΡ *t**j*, ΡΠ΅ΡΠ΅Π· ΠΊΠΎΡΠΎΡΠΎΠ΅ Π΅Π΄Π° Π² ΠΌΠΈΡΠΊΠ΅ ΠΎΡΡΡΠ½Π΅Ρ ΠΈ ΡΡΠ°Π½Π΅Ρ Π½Π΅Π²ΠΊΡΡΠ½ΠΎΠΉ. ΠΡΠΎ Π·Π½Π°ΡΠΈΡ, ΡΡΠΎ Π΅ΡΠ»ΠΈ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΡΠΈΠ±Π΅ΠΆΠΈΡ ΠΊ ΠΌΠΈΡΠΊΠ΅ Π² ΠΌΠΎΠΌΠ΅Π½Ρ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ, ΡΡΡΠΎΠ³ΠΎ Π±ΠΎΠ»ΡΡΠΈΠΉ *t**j*, ΡΠΎ Π΅Π΄Π° ΡΠΆΠ΅ ΠΎΡΡΡΠ½Π΅Ρ, ΠΈ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΊΡΡΠ°ΡΡ Π΅Ρ Π½Π΅ ΡΡΠ°Π½Π΅Ρ.
Π‘ΡΠΈΡΠ°Ρ, ΡΡΠΎ ΠΊΠ°ΠΆΠ΄Π°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° Π±Π΅ΠΆΠΈΡ ΡΠΎ ΡΠΊΠΎΡΠΎΡΡΡΡ 1, Π½Π°ΠΉΠ΄ΠΈΡΠ΅ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠΌΠΎΠ³ΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ. Π‘ΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ ΡΠΎΠ±Π°ΡΠΊΠΈ ΠΏΠΎΠ±Π΅Π³ΡΡ ΠΊ ΡΠ΅ΠΌ ΠΌΠΈΡΠΊΠ°ΠΌ, Π½Π° ΠΊΠΎΡΠΎΡΡΠ΅ Π²Ρ ΠΈΠΌ ΡΠΊΠ°ΠΆΠ΅ΡΠ΅. ΠΠ· ΠΎΠ΄Π½ΠΎΠΉ ΠΌΠΈΡΠΊΠΈ Π½Π΅ ΠΌΠΎΠ³ΡΡ ΠΊΡΡΠ°ΡΡ Π΄Π²Π΅ ΠΈΠ»ΠΈ Π±ΠΎΠ»Π΅Π΅ ΡΠΎΠ±Π°ΡΠΊΠΈ.
Π‘ΠΎΠ±Π°ΡΠΊΠΈ ΠΌΠΎΠ³ΡΡ ΠΎΠ±Π³ΠΎΠ½ΡΡΡ Π΄ΡΡΠ³ Π΄ΡΡΠ³Π°, ΡΠΎ Π΅ΡΡΡ, Π΅ΡΠ»ΠΈ ΠΎΠ΄Π½Π° ΠΈΠ· Π½ΠΈΡ
ΠΎΡΡΠ°Π½ΠΎΠ²ΠΈΡΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ, Π΄ΡΡΠ³Π°Ρ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΎΠΉΡΠΈ ΠΌΠΈΠΌΠΎ Π½Π΅Ρ, ΡΡΠΎΠ±Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΊ Π΄ΡΡΠ³ΠΎΠΉ ΠΌΠΈΡΠΊΠ΅. | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ ΠΏΠ°ΡΠ° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» *n* ΠΈ *m* (1<=β€<=*n*,<=*m*<=β€<=200<=000)Β β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ ΠΈ ΠΌΠΈΡΠΎΠΊ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ.
ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ *n* ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» *x**i* (<=-<=109<=β€<=*x**i*<=β€<=109)Β β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° *i*-ΠΉ ΡΠΎΠ±Π°ΡΠΊΠΈ.
Π ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
*m* ΡΡΡΠΎΠΊΠ°Ρ
Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ ΠΏΠ°ΡΡ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» *u**j* ΠΈ *t**j* (<=-<=109<=β€<=*u**j*<=β€<=109, 1<=β€<=*t**j*<=β€<=109)Β β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° *j*-ΠΉ ΠΌΠΈΡΠΊΠΈ ΠΈ Π²ΡΠ΅ΠΌΡ, ΠΊΠΎΠ³Π΄Π° ΠΎΡΡΡΠ½Π΅Ρ Π΅Π΄Π° Π² Π½Π΅ΠΉ, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ.
ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ Π½ΠΈΠΊΠ°ΠΊΠΈΠ΅ Π΄Π²Π΅ ΡΠΎΠ±Π°ΡΠΊΠΈ Π½Π΅ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ Π² ΠΎΠ΄Π½ΠΎΠΉ ΡΠΎΡΠΊΠ΅. ΠΠΈΠΊΠ°ΠΊΠΈΠ΅ Π΄Π²Π΅ ΠΌΠΈΡΠΊΠΈ ΡΠ°ΠΊΠΆΠ΅ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡΡ Π² ΠΎΠ΄Π½ΠΎΠΉ ΡΠΎΡΠΊΠ΅. | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ *a*Β β ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠΌΠΎΠ³ΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ. | [
"5 4\n-2 0 4 8 13\n-1 1\n4 3\n6 3\n11 2\n",
"3 3\n-1 3 7\n1 1\n4 1\n7 1\n",
"4 4\n20 1 10 30\n1 1\n2 5\n22 2\n40 10\n"
] | [
"4\n",
"2\n",
"3\n"
] | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΏΠ΅ΡΠ²Π°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π½Π°ΠΏΡΠ°Π²ΠΎ ΠΊ ΠΏΠ΅ΡΠ²ΠΎΠΉ ΠΌΠΈΡΠΊΠ΅, ΡΡΠ΅ΡΡΡ ΡΠΎΠ±Π°ΡΠΊΠ° ΡΡΠ°Π·Ρ Π½Π°ΡΠ½ΡΡ Π΅ΡΡΡ ΠΈΠ· Π²ΡΠΎΡΠΎΠΉ ΠΌΠΈΡΠΊΠΈ, ΡΠ΅ΡΠ²ΡΡΡΠ°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π²Π»Π΅Π²ΠΎ ΠΊ ΡΡΠ΅ΡΡΠ΅ΠΉ ΠΌΠΈΡΠΊΠ΅, Π° ΠΏΡΡΠ°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π²Π»Π΅Π²ΠΎ ΠΊ ΡΠ΅ΡΠ²ΡΡΡΠΎΠΉ ΠΌΠΈΡΠΊΠ΅. | [] | 0 | 0 | -1 | 66,318 | |
887 | Ratings and Reality Shows | [
"data structures",
"two pointers"
] | null | null | There are two main kinds of events in the life of top-model: fashion shows and photo shoots. Participating in any of these events affects the rating of appropriate top-model. After each photo shoot model's rating increases by *a* and after each fashion show decreases by *b* (designers do too many experiments nowadays). Moreover, sometimes top-models participates in talk shows. After participating in talk show model becomes more popular and increasing of her rating after photo shoots become *c* and decreasing of her rating after fashion show becomes *d*.
Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will never become negative. Help her to find a suitable moment for participating in the talk show.
Let's assume that model's career begins in moment 0. At that moment Izabella's rating was equal to *start*. If talk show happens in moment *t* if will affect all events in model's life in interval of time [*t*..*t*<=+<=*len*) (including *t* and not including *t*<=+<=*len*), where *len* is duration of influence.
Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will not become become negative before talk show or during period of influence of talk show. Help her to find a suitable moment for participating in the talk show. | In first line there are 7 positive integers *n*, *a*, *b*, *c*, *d*, *start*, *len* (1<=β€<=*n*<=β€<=3Β·105, 0<=β€<=*start*<=β€<=109, 1<=β€<=*a*,<=*b*,<=*c*,<=*d*,<=*len*<=β€<=109), where *n* is a number of fashion shows and photo shoots, *a*, *b*, *c* and *d* are rating changes described above, *start* is an initial rating of model and *len* is a duration of influence of talk show.
In next *n* lines descriptions of events are given. Each of those lines contains two integers *t**i* and *q**i* (1<=β€<=*t**i*<=β€<=109, 0<=β€<=*q*<=β€<=1)Β β moment, in which event happens and type of this event. Type 0 corresponds to the fashion show and type 1Β β to photo shoot.
Events are given in order of increasing *t**i*, all *t**i* are different. | Print one non-negative integer *t*Β β the moment of time in which talk show should happen to make Izabella's rating non-negative before talk show and during period of influence of talk show. If there are multiple answers print smallest of them. If there are no such moments, print <=-<=1. | [
"5 1 1 1 4 0 5\n1 1\n2 1\n3 1\n4 0\n5 0\n",
"1 1 2 1 2 1 2\n1 0\n"
] | [
"6",
"-1"
] | none | [
{
"input": "5 1 1 1 4 0 5\n1 1\n2 1\n3 1\n4 0\n5 0",
"output": "6"
},
{
"input": "1 1 2 1 2 1 2\n1 0",
"output": "-1"
},
{
"input": "10 1 1 1 2 0 10\n1 1\n2 1\n3 0\n4 0\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1",
"output": "5"
}
] | 62 | 0 | 0 | 66,838 | |
62 | World Evil | [
"dp",
"flows"
] | E. World Evil | 5 | 256 | As a result of Pinky and Brain's mysterious experiments in the Large Hadron Collider some portals or black holes opened to the parallel dimension. And the World Evil has crept to the veil between their world and ours. Brain quickly evaluated the situation and he understood that the more evil tentacles creep out and become free, the higher is the possibility that Brain will rule the world.
The collider's constriction is a rectangular grid rolled into a cylinder and consisting of *n* rows and *m* columns such as is shown in the picture below:
In this example *n*<==<=4, *m*<==<=5. Dotted lines are corridores that close each column to a ring, i. e. connect the *n*-th and the 1-th rows of the grid.
In the leftmost column of the grid the portals are situated and the tentacles of the World Evil are ready to creep out from there. In the rightmost column the exit doors are located. The tentacles can only get out through those doors. The segments joining the nodes of the grid are corridors.
Brain would be glad to let all the tentacles out but he faces a problem: the infinite number of tentacles can creep out of the portals, every tentacle possesses infinite length and some width and the volume of the corridors are, unfortunately, quite limited. Brain could approximately evaluate the maximal number of tentacles that will be able to crawl through every corridor.
Now help the mice to determine the maximal number of tentacles of the World Evil that will crawl out of the Large Hadron Collider. | The first line of the input file contains two integers *n* and *m* (2<=β€<=*n*<=β€<=5, 2<=β€<=*m*<=β€<=105). They are the sizes of the Large Hadron Collider grid. The next *m*<=-<=1 lines contain *n* numbers each. They are the horizontal corridors' capacities. The next *m* lines contain *n* numbers each. They are the vertical corridors' capacities. Corridors are described from left to right and from top to bottom. Every *n*-th vertical corridor connects nodes of the *n*-th and 1-th rows. A corridor's capacity is a non-negative integer that does not exceed 109. | Print a single number, the number of the World Evil tentacles Pinky and Brain will command.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | [
"3 4\n4 4 4\n1 1 5\n5 5 3\n4 1 2\n1 3 1\n3 5 4\n1 4 3\n",
"2 2\n9 2\n2 3\n6 1\n"
] | [
"7\n",
"11\n"
] | none | [] | 0 | 0 | -1 | 66,875 |
0 | none | [
"none"
] | null | null | You are given a set of *n* points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines.
Multiset is a set where equal elements are allowed.
Multiset is called symmetric, if there is a point *P* on the plane such that the multiset is [centrally symmetric](https://en.wikipedia.org/wiki/Point_reflection) in respect of point *P*. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2000) β the number of points in the set.
Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=106<=<=β€<=<=*x**i*,<=<=*y**i*<=<=β€<=<=106) β the coordinates of the points. It is guaranteed that no two points coincide. | If there are infinitely many good lines, print -1.
Otherwise, print single integerΒ β the number of good lines. | [
"3\n1 2\n2 1\n3 3\n",
"2\n4 3\n1 2\n"
] | [
"3\n",
"-1\n"
] | Picture to the first sample test:
<img class="tex-graphics" src="https://espresso.codeforces.com/eedc60313be8684bd6169b8b23f0f0afd92479a8.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In the second sample, any line containing the origin is good. | [] | 77 | 512,000 | 0 | 66,929 | |
85 | Embassy Queue | [
"data structures",
"greedy"
] | B. Embassy Queue | 2 | 256 | In an embassy of a well-known kingdom an electronic queue is organised. Every person who comes to the embassy, needs to make the following three actions: show the ID, pay money to the cashier and be fingerprinted. Besides, the actions should be performed in the given order.
For each action several separate windows are singled out: *k*1 separate windows for the first action (the first type windows), *k*2 windows for the second one (the second type windows), and *k*3 for the third one (the third type windows). The service time for one person in any of the first type window equals to *t*1. Similarly, it takes *t*2 time to serve a person in any of the second type windows. And it takes *t*3 to serve one person in any of the third type windows. Thus, the service time depends only on the window type and is independent from the person who is applying for visa.
At some moment *n* people come to the embassy, the *i*-th person comes at the moment of time *c**i*. The person is registered under some number. After that he sits in the hall and waits for his number to be shown on a special board. Besides the person's number the board shows the number of the window where one should go and the person goes there immediately. Let's consider that the time needed to approach the window is negligible. The table can show information for no more than one person at a time. The electronic queue works so as to immediately start working with the person who has approached the window, as there are no other people in front of the window.
The Client Service Quality inspectors noticed that several people spend too much time in the embassy (this is particularly tiresome as the embassy has no mobile phone reception and 3G). It was decided to organise the system so that the largest time a person spends in the embassy were minimum. Help the inspectors organise the queue. Consider that all actions except for being served in at the window, happen instantly. | The first line contains three space-separated integers *k*1, *k*2, *k*3 (1<=β€<=*k**i*<=β€<=109), they are the number of windows of the first, second and third type correspondingly.
The second line contains three space-separated integers *t*1, *t*2, *t*3 (1<=β€<=*t**i*<=β€<=105), they are the periods of time needed to serve one person in the window of the first, second and third type correspondingly.
The third line contains an integer *n* (1<=β€<=*n*<=β€<=105), it is the number of people.
The fourth line contains *n* space-separated integers *c**i* (1<=β€<=*c**i*<=β€<=109) in the non-decreasing order; *c**i* is the time when the person number *i* comes to the embassy. | Print the single number, the maximum time a person will spend in the embassy if the queue is organized optimally.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator). | [
"1 1 1\n1 1 1\n5\n1 1 1 1 1\n",
"2 1 1\n5 1 1\n5\n1 2 3 3 5\n"
] | [
"7\n",
"13\n"
] | In the first test 5 people come simultaneously at the moment of time equal to 1. There is one window of every type, it takes 1 unit of time to be served at each window. That's why the maximal time a person spends in the embassy is the time needed to be served at the windows (3 units of time) plus the time the last person who comes to the first window waits (4 units of time).
Windows in the second test work like this:
The first window of the first type: [1,β6) β the first person, [6,β11) β third person, [11,β16) β fifth person
The second window of the first type: [2,β7) β the second person, [7,β12) β the fourth person
The only second type window: [6,β7) β first, [7,β8) β second, [11,β12) β third, [12,β13) β fourth, [16,β17) β fifth
The only third type window: [7,β8) β first, [8,β9) β second, [12,β13) β third, [13,β14) β fourth, [17,β18) β fifth
We can see that it takes most time to serve the fifth person. | [
{
"input": "1 1 1\n1 1 1\n5\n1 1 1 1 1",
"output": "7"
},
{
"input": "2 1 1\n5 1 1\n5\n1 2 3 3 5",
"output": "13"
},
{
"input": "1 1 1\n1 1 1\n5\n1 2 3 4 5",
"output": "3"
},
{
"input": "1 1 1\n1 1 1\n5\n1 2 3 3 4",
"output": "4"
},
{
"input": "9 5 6\n8 2 2\n1\n5"... | 530 | 9,420,800 | 3.849952 | 67,097 |
731 | Coupons and Discounts | [
"constructive algorithms",
"greedy"
] | null | null | The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.
Teams plan to train for *n* times during *n* consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be *a**i* teams on the *i*-th day.
There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).
As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.
Sereja wants to order exactly *a**i* pizzas on the *i*-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day *n*. | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of training sessions.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=β€<=*a**i*<=β€<=10<=000)Β β the number of teams that will be present on each of the days. | If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes). | [
"4\n1 2 1 2\n",
"3\n1 0 1\n"
] | [
"YES\n",
"NO\n"
] | In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.
In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days. | [
{
"input": "4\n1 2 1 2",
"output": "YES"
},
{
"input": "3\n1 0 1",
"output": "NO"
},
{
"input": "3\n1 3 1",
"output": "NO"
},
{
"input": "3\n2 0 2",
"output": "YES"
},
{
"input": "1\n179",
"output": "NO"
},
{
"input": "10\n0 0 5 9 9 3 0 0 0 10",
"o... | 108 | 20,172,800 | 0 | 67,268 | |
249 | Donkey and Stars | [
"data structures",
"dp",
"geometry",
"math",
"sortings"
] | null | null | In the evenings Donkey would join Shrek to look at the stars. They would sit on a log, sipping tea and they would watch the starry sky. The sky hung above the roof, right behind the chimney. Shrek's stars were to the right of the chimney and the Donkey's stars were to the left. Most days the Donkey would just count the stars, so he knew that they are exactly *n*. This time he wanted a challenge. He imagined a coordinate system: he put the origin of the coordinates at the intersection of the roof and the chimney, directed the *OX* axis to the left along the roof and the *OY* axis β up along the chimney (see figure). The Donkey imagined two rays emanating from he origin of axes at angles Ξ±1 and Ξ±2 to the *OX* axis.
Now he chooses any star that lies strictly between these rays. After that he imagines more rays that emanate from this star at the same angles Ξ±1 and Ξ±2 to the *OX* axis and chooses another star that lies strictly between the new rays. He repeats the operation as long as there still are stars he can choose between the rays that emanate from a star.
As a result, the Donkey gets a chain of stars. He can consecutively get to each star if he acts by the given rules.
Your task is to find the maximum number of stars *m* that the Donkey's chain can contain.
Note that the chain must necessarily start in the point of the origin of the axes, that isn't taken into consideration while counting the number *m* of stars in the chain. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105) β the number of stars. The second line contains simple fractions representing relationships "*a*/*b* *c*/*d*", such that and (0<=β€<=*a*,<=*b*,<=*c*,<=*d*<=β€<=105; ; ; ). The given numbers *a*, *b*, *c*, *d* are integers.
Next *n* lines contain pairs of integers *x**i*, *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=105)β the stars' coordinates.
It is guaranteed that all stars have distinct coordinates. | In a single line print number *m* β the answer to the problem. | [
"15\n1/3 2/1\n3 1\n6 2\n4 2\n2 5\n4 5\n6 6\n3 4\n1 6\n2 1\n7 4\n9 3\n5 3\n1 3\n15 5\n12 4\n"
] | [
"4\n"
] | In the sample the longest chain the Donkey can build consists of four stars. Note that the Donkey can't choose the stars that lie on the rays he imagines. | [
{
"input": "15\n1/3 2/1\n3 1\n6 2\n4 2\n2 5\n4 5\n6 6\n3 4\n1 6\n2 1\n7 4\n9 3\n5 3\n1 3\n15 5\n12 4",
"output": "4"
},
{
"input": "15\n2/1 2/0\n3 1\n6 2\n9 3\n12 4\n15 5\n2 1\n4 2\n5 3\n7 4\n1 3\n3 4\n2 5\n4 5\n1 6\n6 6",
"output": "1"
},
{
"input": "15\n2/1 2/0\n3 1\n6 2\n9 3\n12 4\n15... | 2,000 | 8,704,000 | 0 | 67,466 | |
0 | none | [
"none"
] | null | null | This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive.
Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, but not happily ever after: one day a vicious dragon attacked the kingdom and stole Victoria. The King was full of grief, yet he gathered his noble knights and promised half of his kingdom and Victoria's hand in marriage to the one who will save the girl from the infernal beast.
Having travelled for some time, the knights found the dragon's lair and all of them rushed there to save Victoria. Each knight spat on the dragon once and, as the dragon had quite a fragile and frail heart, his heart broke and poor beast died. As for the noble knights, they got Victoria right to the King and started brawling as each one wanted the girl's hand in marriage.
The problem was that all the noble knights were equally noble and equally handsome, and Victoria didn't want to marry any of them anyway. Then the King (and he was a very wise man and didn't want to hurt anybody's feelings) decided to find out who will get his daughter randomly, i.e. tossing a coin. However, there turned out to be *n* noble knights and the coin only has two sides. The good thing is that when a coin is tossed, the coin falls on each side with equal probability. The King got interested how to pick one noble knight using this coin so that all knights had equal probability of being chosen (the probability in that case should always be equal to 1<=/<=*n*). First the King wants to know the expected number of times he will need to toss a coin to determine the winner. Besides, while tossing the coin, the King should follow the optimal tossing strategy (i.e. the strategy that minimizes the expected number of tosses). Help the King in this challenging task. | The first line contains a single integer *n* from the problem's statement (1<=β€<=*n*<=β€<=10000). | Print the sought expected number of tosses as an irreducible fraction in the following form: "*a*/*b*" (without the quotes) without leading zeroes. | [
"2\n",
"3\n",
"4\n"
] | [
"1/1\n",
"8/3\n",
"2/1\n"
] | none | [
{
"input": "2",
"output": "1/1"
},
{
"input": "3",
"output": "8/3"
},
{
"input": "4",
"output": "2/1"
},
{
"input": "8",
"output": "3/1"
},
{
"input": "7",
"output": "24/7"
},
{
"input": "6",
"output": "11/3"
},
{
"input": "1",
"output"... | 60 | 0 | 0 | 67,530 | |
0 | none | [
"none"
] | null | null | Radewoosh is playing a computer game. There are *n* levels, numbered 1 through *n*. Levels are divided into *k* regions (groups). Each region contains some positive number of consecutive levels.
The game repeats the the following process:
1. If all regions are beaten then the game ends immediately. Otherwise, the system finds the first region with at least one non-beaten level. Let *X* denote this region.1. The system creates an empty bag for tokens. Each token will represent one level and there may be many tokens representing the same level. For each already beaten level *i* in the region *X*, the system adds *t**i* tokens to the bag (tokens representing the *i*-th level). 1. Let *j* denote the first non-beaten level in the region *X*. The system adds *t**j* tokens to the bag. 1. Finally, the system takes a uniformly random token from the bag and a player starts the level represented by the token. A player spends one hour and beats the level, even if he has already beaten it in the past.
Given *n*, *k* and values *t*1,<=*t*2,<=...,<=*t**n*, your task is to split levels into regions. Each level must belong to exactly one region, and each region must contain non-empty consecutive set of levels. What is the minimum possible expected number of hours required to finish the game? | The first line of the input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=200<=000, 1<=β€<=*k*<=β€<=*min*(50,<=*n*))Β β the number of levels and the number of regions, respectively.
The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=100<=000). | Print one real numberΒ β the minimum possible expected value of the number of hours spent to finish the game if levels are distributed between regions in the optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=4.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if . | [
"4 2\n100 3 5 7\n",
"6 2\n1 2 4 8 16 32\n"
] | [
"5.7428571429\n",
"8.5000000000\n"
] | In the first sample, we are supposed to split 4 levels into 2 regions. It's optimal to create the first region with only one level (it must be the first level). Then, the second region must contain other three levels.
In the second sample, it's optimal to split levels into two regions with 3 levels each. | [] | 3,000 | 10,035,200 | 0 | 67,594 | |
679 | Bear and Chase | [
"brute force",
"dfs and similar",
"graphs",
"implementation",
"math",
"probabilities"
] | null | null | Bearland has *n* cities, numbered 1 through *n*. There are *m* bidirectional roads. The *i*-th road connects two distinct cities *a**i* and *b**i*. No two roads connect the same pair of cities. It's possible to get from any city to any other city (using one or more roads).
The distance between cities *a* and *b* is defined as the minimum number of roads used to travel between *a* and *b*.
Limak is a grizzly bear. He is a criminal and your task is to catch him, or at least to try to catch him. You have only two days (today and tomorrow) and after that Limak is going to hide forever.
Your main weapon is BCD (Bear Criminal Detector). Where you are in some city, you can use BCD and it tells you the distance between you and a city where Limak currently is. Unfortunately, BCD can be used only once a day.
You don't know much about Limak's current location. You assume that he is in one of *n* cities, chosen uniformly at random (each city with probability ). You decided for the following plan:
1. Choose one city and use BCD there. After using BCD you can try to catch Limak (but maybe it isn't a good idea). In this case you choose one city and check it. You win if Limak is there. Otherwise, Limak becomes more careful and you will never catch him (you loose). 1. Wait 24 hours to use BCD again. You know that Limak will change his location during that time. In detail, he will choose uniformly at random one of roads from his initial city, and he will use the chosen road, going to some other city. 1. Tomorrow, you will again choose one city and use BCD there. 1. Finally, you will try to catch Limak. You will choose one city and check it. You will win if Limak is there, and loose otherwise.
Each time when you choose one of cities, you can choose any of *n* cities. Let's say it isn't a problem for you to quickly get somewhere.
What is the probability of finding Limak, if you behave optimally? | The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=400, )Β β the number of cities and the number of roads, respectively.
Then, *m* lines follow. The *i*-th of them contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*)Β β cities connected by the *i*-th road.
No two roads connect the same pair of cities. It's possible to get from any city to any other city. | Print one real numberΒ β the probability of finding Limak, if you behave optimally. Your answer will be considered correct if its absolute error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if |*a*<=-<=*b*|<=β€<=10<=-<=6. | [
"3 3\n1 2\n1 3\n2 3\n",
"5 4\n1 2\n3 1\n5 1\n1 4\n",
"4 4\n1 2\n1 3\n2 3\n1 4\n",
"5 5\n1 2\n2 3\n3 4\n4 5\n1 5\n"
] | [
"0.833333333333\n",
"1.000000000000\n",
"0.916666666667\n",
"0.900000000000\n"
] | In the first sample test, there are three cities and there is a road between every pair of cities. Let's analyze one of optimal scenarios.
1. Use BCD in city 1. <li> With probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/64c94d13eeb330b494061e86538db66574ad0f7d.png" style="max-width: 100.0%;max-height: 100.0%;"/> Limak is in this city and BCD tells you that the distance is 0. You should try to catch him now and you win for sure. 1. With probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/14b21b617fcd2e25700376368355f7bbf975d8de.png" style="max-width: 100.0%;max-height: 100.0%;"/> the distance is 1 because Limak is in city 2 or city 3. In this case you should wait for the second day. </li>1. You wait and Limak moves to some other city. <li> There is probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that Limak was in city 2 and then went to city 3. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 2 to 1. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 3 to 2. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 3 to 1. </li>1. Use BCD again in city 1 (though it's allowed to use it in some other city). <li> If the distance is 0 then you're sure Limak is in this city (you win). 1. If the distance is 1 then Limak is in city 2 or city 3. Then you should guess that he is in city 2 (guessing city 3 would be fine too). </li> - There is probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that Limak was in city 2 and then went to city 3. - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 2 to 1. - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 3 to 2. - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/45372faa18afd6691c4e5aa94c7b5edc43a23007.png" style="max-width: 100.0%;max-height: 100.0%;"/> that he went from 3 to 1.
You loose only if Limak was in city 2 first and then he moved to city 3. The probability of loosing is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/55bd5cf64609940bba840be4d13d882b3b49c511.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/56d7c14b8db6605aa95b947bca89a66828b49171.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [] | 30 | 0 | 0 | 67,625 | |
980 | Perfect Groups | [
"dp",
"math",
"number theory"
] | null | null | SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be divided into, such that the product of any pair of integers in the same group is a perfect square.
Each integer must be in exactly one group. However, integers in a group do not necessarily have to be contiguous in the array.
SaMer wishes to create more cases from the test case he already has. His test case has an array $A$ of $n$ integers, and he needs to find the number of contiguous subarrays of $A$ that have an answer to the problem equal to $k$ for each integer $k$ between $1$ and $n$ (inclusive). | The first line of input contains a single integer $n$ ($1 \leq n \leq 5000$), the size of the array.
The second line contains $n$ integers $a_1$,$a_2$,$\dots$,$a_n$ ($-10^8 \leq a_i \leq 10^8$), the values of the array. | Output $n$ space-separated integers, the $k$-th integer should be the number of contiguous subarrays of $A$ that have an answer to the problem equal to $k$. | [
"2\n5 5\n",
"5\n5 -4 2 1 8\n",
"1\n0\n"
] | [
"3 0\n",
"5 5 3 2 0\n",
"1\n"
] | none | [
{
"input": "2\n5 5",
"output": "3 0"
},
{
"input": "5\n5 -4 2 1 8",
"output": "5 5 3 2 0"
},
{
"input": "1\n0",
"output": "1"
},
{
"input": "3\n-10 -5 7",
"output": "3 2 1"
},
{
"input": "5\n-6 -7 -2 -3 -10",
"output": "5 4 3 2 1"
},
{
"input": "8\n-5 ... | 1,000 | 31,129,600 | 0 | 67,777 | |
575 | Fibonotci | [
"data structures",
"math",
"matrices"
] | null | null | Fibonotci sequence is an integer recursive sequence defined by the recurrence relation
Sequence *s* is an infinite and almost cyclic sequence with a cycle of length *N*. A sequence *s* is called almost cyclic with a cycle of length *N* if , for *i*<=β₯<=*N*, except for a finite number of values *s**i*, for which (*i*<=β₯<=*N*).
Following is an example of an almost cyclic sequence with a cycle of length 4:
Notice that the only value of *s* for which the equality does not hold is *s*6 (*s*6<==<=7 and *s*2<==<=8). You are given *s*0,<=*s*1,<=...*s**N*<=-<=1 and all the values of sequence *s* for which (*i*<=β₯<=*N*).
Find . | The first line contains two numbers *K* and *P*. The second line contains a single number *N*. The third line contains *N* numbers separated by spaces, that represent the first *N* numbers of the sequence *s*. The fourth line contains a single number *M*, the number of values of sequence *s* for which . Each of the following *M* lines contains two numbers *j* and *v*, indicating that and *s**j*<==<=*v*. All j-s are distinct.
- 1<=β€<=*N*,<=*M*<=β€<=50000 - 0<=β€<=*K*<=β€<=1018 - 1<=β€<=*P*<=β€<=109 - 1<=β€<=*s**i*<=β€<=109, for all *i*<==<=0,<=1,<=...*N*<=-<=1 - *N*<=β€<=*j*<=β€<=1018 - 1<=β€<=*v*<=β€<=109 - All values are integers | Output should contain a single integer equal to . | [
"10 8\n3\n1 2 1\n2\n7 3\n5 4\n"
] | [
"4\n"
] | none | [] | 2,000 | 17,715,200 | 0 | 67,854 | |
660 | Bear and Bowling 4 | [
"binary search",
"data structures",
"divide and conquer",
"geometry",
"ternary search"
] | null | null | Limak is an old brown bear. He often goes bowling with his friends. Today he feels really good and tries to beat his own record!
For rolling a ball one gets a score β an integer (maybe negative) number of points. Score for the *i*-th roll is multiplied by *i* and scores are summed up. So, for *k* rolls with scores *s*1,<=*s*2,<=...,<=*s**k*, the total score is . The total score is 0 if there were no rolls.
Limak made *n* rolls and got score *a**i* for the *i*-th of them. He wants to maximize his total score and he came up with an interesting idea. He can say that some first rolls were only a warm-up, and that he wasn't focused during the last rolls. More formally, he can cancel any prefix and any suffix of the sequence *a*1,<=*a*2,<=...,<=*a**n*. It is allowed to cancel all rolls, or to cancel none of them.
The total score is calculated as if there were only non-canceled rolls. So, the first non-canceled roll has score multiplied by 1, the second one has score multiplied by 2, and so on, till the last non-canceled roll.
What maximum total score can Limak get? | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the total number of rolls made by Limak.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=β€<=107) β scores for Limak's rolls. | Print the maximum possible total score after cancelling rolls. | [
"6\n5 -1000 1 -3 7 -8\n",
"5\n1000 1000 1001 1000 1000\n",
"3\n-60 -70 -80\n"
] | [
"16\n",
"15003\n",
"0\n"
] | In the first sample test, Limak should cancel the first two rolls, and one last roll. He will be left with rolls 1,ββ-β3,β7 what gives him the total score 1Β·1β+β2Β·(β-β3)β+β3Β·7β=β1β-β6β+β21β=β16. | [
{
"input": "6\n5 -1000 1 -3 7 -8",
"output": "16"
},
{
"input": "5\n1000 1000 1001 1000 1000",
"output": "15003"
},
{
"input": "3\n-60 -70 -80",
"output": "0"
},
{
"input": "1\n-4",
"output": "0"
},
{
"input": "2\n-3 6",
"output": "9"
},
{
"input": "3\... | 46 | 0 | 0 | 67,952 | |
848 | Days of Floral Colours | [
"combinatorics",
"divide and conquer",
"dp",
"fft",
"math"
] | null | null | The Floral Clock has been standing by the side of Mirror Lake for years. Though unable to keep time, it reminds people of the passage of time and the good old days.
On the rim of the Floral Clock are 2*n* flowers, numbered from 1 to 2*n* clockwise, each of which has a colour among all *n* possible ones. For each colour, there are exactly two flowers with it, the distance between which either is less than or equal to 2, or equals *n*. Additionally, if flowers *u* and *v* are of the same colour, then flowers opposite to *u* and opposite to *v* should be of the same colour as well β symmetry is beautiful!
Formally, the distance between two flowers is 1 plus the number of flowers on the minor arc (or semicircle) between them. Below is a possible arrangement with *n*<==<=6 that cover all possibilities.
The beauty of an arrangement is defined to be the product of the lengths of flower segments separated by all opposite flowers of the same colour. In other words, in order to compute the beauty, we remove from the circle all flowers that have the same colour as flowers opposite to them. Then, the beauty is the product of lengths of all remaining segments. Note that we include segments of length 0 in this product. If there are no flowers that have the same colour as flower opposite to them, the beauty equals 0. For instance, the beauty of the above arrangement equals 1<=Γ<=3<=Γ<=1<=Γ<=3<==<=9 β the segments are {2}, {4,<=5,<=6}, {8} and {10,<=11,<=12}.
While keeping the constraints satisfied, there may be lots of different arrangements. Find out the sum of beauty over all possible arrangements, modulo 998<=244<=353. Two arrangements are considered different, if a pair (*u*,<=*v*) (1<=β€<=*u*,<=*v*<=β€<=2*n*) exists such that flowers *u* and *v* are of the same colour in one of them, but not in the other. | The first and only line of input contains a lonely positive integer *n* (3<=β€<=*n*<=β€<=50<=000)Β β the number of colours present on the Floral Clock. | Output one integer β the sum of beauty over all possible arrangements of flowers, modulo 998<=244<=353. | [
"3\n",
"4\n",
"7\n",
"15\n"
] | [
"24\n",
"4\n",
"1316\n",
"3436404\n"
] | With *n*β=β3, the following six arrangements each have a beauty of 2βΓβ2β=β4.
While many others, such as the left one in the figure below, have a beauty of 0. The right one is invalid, since it's asymmetric. | [
{
"input": "3",
"output": "24"
},
{
"input": "4",
"output": "4"
},
{
"input": "7",
"output": "1316"
},
{
"input": "15",
"output": "3436404"
},
{
"input": "10",
"output": "26200"
},
{
"input": "99",
"output": "620067986"
},
{
"input": "1317"... | 46 | 0 | 0 | 68,015 | |
733 | Epidemic in Monstropolis | [
"constructive algorithms",
"dp",
"greedy",
"two pointers"
] | null | null | There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.
Soon, monsters became hungry and began to eat each other.
One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster *A* eats the monster *B*, the weight of the monster *A* increases by the weight of the eaten monster *B*. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were *n* monsters in the queue, the *i*-th of which had weight *a**i*.
For example, if weights are [1,<=2,<=2,<=2,<=1,<=2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are:
1. the first monster can't eat the second monster because *a*1<==<=1 is not greater than *a*2<==<=2; 1. the second monster can't eat the third monster because *a*2<==<=2 is not greater than *a*3<==<=2; 1. the second monster can't eat the fifth monster because they are not neighbors; 1. the second monster can eat the first monster, the queue will be transformed to [3,<=2,<=2,<=1,<=2].
After some time, someone said a good joke and all monsters recovered. At that moment there were *k* (*k*<=β€<=*n*) monsters in the queue, the *j*-th of which had weight *b**j*. Both sequences (*a* and *b*) contain the weights of the monsters in the order from the first to the last.
You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=500)Β β the number of monsters in the initial queue.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106)Β β the initial weights of the monsters.
The third line contains single integer *k* (1<=β€<=*k*<=β€<=*n*)Β β the number of monsters in the queue after the joke.
The fourth line contains *k* integers *b*1,<=*b*2,<=...,<=*b**k* (1<=β€<=*b**j*<=β€<=5Β·108)Β β the weights of the monsters after the joke.
Monsters are listed in the order from the beginning of the queue to the end. | In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line.
Otherwise print "YES" (without quotes) in the first line. In the next *n*<=-<=*k* lines print actions in the chronological order. In each line print *x*Β β the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the *x*-th in the queue eats the monster in front of him, or 'R' if the monster which stays the *x*-th in the queue eats the monster behind him. After each eating the queue is enumerated again.
When one monster eats another the queue decreases. If there are several answers, print any of them. | [
"6\n1 2 2 2 1 2\n2\n5 5\n",
"5\n1 2 3 4 5\n1\n15\n",
"5\n1 1 1 3 3\n3\n2 1 6\n"
] | [
"YES\n2 L\n1 R\n4 L\n3 L\n",
"YES\n5 L\n4 L\n3 L\n2 L\n",
"NO"
] | In the first example, initially there were *n*β=β6 monsters, their weights are [1,β2,β2,β2,β1,β2] (in order of queue from the first monster to the last monster). The final queue should be [5,β5]. The following sequence of eatings leads to the final queue:
- the second monster eats the monster to the left (i.e. the first monster), queue becomes [3,β2,β2,β1,β2]; - the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5,β2,β1,β2]; - the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5,β2,β3]; - the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5,β5].
Note that for each step the output contains numbers of the monsters in their current order in the queue. | [
{
"input": "6\n1 2 2 2 1 2\n2\n5 5",
"output": "YES\n2 L\n1 R\n4 L\n3 L"
},
{
"input": "5\n1 2 3 4 5\n1\n15",
"output": "YES\n5 L\n4 L\n3 L\n2 L"
},
{
"input": "5\n1 1 1 3 3\n3\n2 1 6",
"output": "NO"
},
{
"input": "5\n1 1 1 1 2\n3\n1 1 4",
"output": "YES\n5 L\n4 L"
},
... | 0 | 0 | -1 | 68,253 | |
553 | Nudist Beach | [
"binary search",
"graphs",
"greedy"
] | null | null | Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Life Fibers.
There are *n* cities, labeled from 1 to *n*, and *m* bidirectional roads between them. Currently, there are Life Fibers in every city. In addition, there are *k* cities that are fortresses of the Life Fibers that cannot be captured under any circumstances. So, the Nudist Beach can capture an arbitrary non-empty subset of cities with no fortresses.
After the operation, Nudist Beach will have to defend the captured cities from counterattack. If they capture a city and it is connected to many Life Fiber controlled cities, it will be easily defeated. So, Nudist Beach would like to capture a set of cities such that for each captured city the ratio of Nudist Beach controlled neighbors among all neighbors of that city is as high as possible.
More formally, they would like to capture a non-empty set of cities *S* with no fortresses of Life Fibers. The strength of a city is defined as (number of neighbors of *x* in *S*) / (total number of neighbors of *x*). Here, two cities are called neighbors if they are connnected with a road. The goal is to maximize the strength of the weakest city in *S*.
Given a description of the graph, and the cities with fortresses, find a non-empty subset that maximizes the strength of the weakest city. | The first line of input contains three integers *n*,<=*m*,<=*k* (2<=<=β€<=<=*n*<=<=β€<=100<=000, 1<=β€<=*m*<=β€<=100<=000, 1<=β€<=*k*<=β€<=*n*<=-<=1).
The second line of input contains *k* integers, representing the cities with fortresses. These cities will all be distinct.
The next *m* lines contain the roads. The *i*-th of these lines will have 2 integers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*). Every city will have at least one road adjacent to it.
There is no more than one road between each pair of the cities. | The first line should contain an integer *r*, denoting the size of an optimum set (1<=β€<=*r*<=β€<=*n*<=-<=*k*).
The second line should contain *r* integers, denoting the cities in the set. Cities may follow in an arbitrary order. This line should not contain any of the cities with fortresses.
If there are multiple possible answers, print any of them. | [
"9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9\n",
"10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4\n"
] | [
"3\n1 4 5\n",
"8\n1 5 4 8 10 6 3 7\n"
] | The first example case achieves a strength of 1/2. No other subset is strictly better.
The second example case achieves a strength of 1. Note that the subset doesn't necessarily have to be connected. | [
{
"input": "9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9",
"output": "3\n5 1 4"
},
{
"input": "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4",
"output": "8\n3 10 6 1 5 8 7 4"
},
{
"input": "2 1 1\n1\n2 1",
"output": "1\n2"
}
] | 452 | 30,617,600 | -1 | 68,355 | |
804 | Expected diameter of a tree | [
"binary search",
"brute force",
"dfs and similar",
"dp",
"sortings",
"trees"
] | null | null | Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem.
We have a forest (acyclic undirected graph) with *n* vertices and *m* edges. There are *q* queries we should answer. In each query two vertices *v* and *u* are given. Let *V* be the set of vertices in the connected component of the graph that contains *v*, and *U* be the set of vertices in the connected component of the graph that contains *u*. Let's add an edge between some vertex and some vertex in and compute the value *d* of the resulting component. If the resulting component is a tree, the value *d* is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of *d*, if we choose vertices *a* and *b* from the sets uniformly at random?
Can you help Pasha to solve this problem?
The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices.
Note that queries don't add edges to the initial forest. | The first line contains three integers *n*, *m* and *q*(1<=β€<=*n*,<=*m*,<=*q*<=β€<=105)Β β the number of vertices, the number of edges in the graph and the number of queries.
Each of the next *m* lines contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*), that means there is an edge between vertices *u**i* and *v**i*.
It is guaranteed that the given graph is a forest.
Each of the next *q* lines contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*)Β β the vertices given in the *i*-th query. | For each query print the expected value of *d* as described in the problem statement.
Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Let's assume that your answer is *a*, and the jury's answer is *b*. The checker program will consider your answer correct, if . | [
"3 1 2\n1 3\n3 1\n2 3\n",
"5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5\n"
] | [
"-1\n2.0000000000\n",
"-1\n2.6666666667\n2.6666666667\n"
] | In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1β-β2, the other one is 2β-β3. In both ways the resulting diameter is 2, so the answer is 2.
In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1β-β2 or 1β-β3 the diameter is 3, and for added edge 1β-β4 the diameter is 2. Thus, the answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f12c59a7dfd20580ff1e8e5eeab9ecd19cb3c3f1.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "3 1 2\n1 3\n3 1\n2 3",
"output": "-1\n2.0000000000"
},
{
"input": "5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5",
"output": "-1\n2.6666666667\n2.6666666667"
},
{
"input": "17 15 13\n3 15\n3 1\n15 9\n16 6\n1 5\n1 8\n16 12\n15 7\n9 4\n6 11\n15 14\n9 10\n15 13\n1 17\n11 2\n7 3\n9 6\n9 7\n1 8\... | 3,000 | 22,323,200 | 0 | 68,586 | |
209 | Pixels | [
"constructive algorithms",
"math"
] | null | null | Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that is, the total number of pixels decreases by one). Besides, if pixels of colors *x* and *y* (*x*<=β <=*y*) meet in a violent fight, then the pixel that survives the fight immediately changes its color to *z* (*z*<=β <=*x*;Β *z*<=β <=*y*). Pixels of the same color are friends, so they don't fight.
The King of Flatland knows that his land will be peaceful and prosperous when the pixels are of the same color. For each of the three colors you know the number of pixels of this color that inhabit Flatland. Help the king and determine whether fights can bring peace and prosperity to the country and if it is possible, find the minimum number of fights needed to make the land peaceful and prosperous. | The first line contains three space-separated integers *a*, *b* and *c* (0<=β€<=*a*,<=*b*,<=*c*<=β€<=231;Β *a*<=+<=*b*<=+<=*c*<=><=0) β the number of red, green and blue pixels, correspondingly. | Print a single number β the minimum number of pixel fights before the country becomes peaceful and prosperous. If making the country peaceful and prosperous is impossible, print -1. | [
"1 1 1\n",
"3 1 0\n"
] | [
"1\n",
"3\n"
] | In the first test sample the country needs only one fight to achieve peace and prosperity. Besides, it can be any fight whatsoever. For example, let's assume that the green and the blue pixels fight, then the surviving pixel will be red. As a result, after the fight there are two red pixels. There won't be other pixels.
In the second sample the following sequence of fights is possible: red and blue, green and red, red and blue. As a result, after all fights there is one green pixel left. | [
{
"input": "1 1 1",
"output": "1"
},
{
"input": "3 1 0",
"output": "3"
},
{
"input": "1 4 4",
"output": "4"
},
{
"input": "5 10 6",
"output": "10"
},
{
"input": "6 8 10",
"output": "8"
},
{
"input": "1 10 2",
"output": "10"
},
{
"input": "1... | 248 | 0 | 3 | 68,597 | |
715 | Digit Tree | [
"dfs and similar",
"divide and conquer",
"dsu",
"trees"
] | null | null | ZS the Coder has a large tree. It can be represented as an undirected connected graph of *n* vertices numbered from 0 to *n*<=-<=1 and *n*<=-<=1 edges between them. There is a single nonzero digit written on each edge.
One day, ZS the Coder was bored and decided to investigate some properties of the tree. He chose a positive integer *M*, which is coprime to 10, i.e. .
ZS consider an ordered pair of distinct vertices (*u*,<=*v*) interesting when if he would follow the shortest path from vertex *u* to vertex *v* and write down all the digits he encounters on his path in the same order, he will get a decimal representaion of an integer divisible by *M*.
Formally, ZS consider an ordered pair of distinct vertices (*u*,<=*v*) interesting if the following states true:
- Let *a*1<==<=*u*,<=*a*2,<=...,<=*a**k*<==<=*v* be the sequence of vertices on the shortest path from *u* to *v* in the order of encountering them; - Let *d**i* (1<=β€<=*i*<=<<=*k*) be the digit written on the edge between vertices *a**i* and *a**i*<=+<=1; - The integer is divisible by *M*.
Help ZS the Coder find the number of interesting pairs! | The first line of the input contains two integers, *n* and *M* (2<=β€<=*n*<=β€<=100<=000,<=1<=β€<=*M*<=β€<=109, )Β β the number of vertices and the number ZS has chosen respectively.
The next *n*<=-<=1 lines contain three integers each. *i*-th of them contains *u**i*,<=*v**i* and *w**i*, denoting an edge between vertices *u**i* and *v**i* with digit *w**i* written on it (0<=β€<=*u**i*,<=*v**i*<=<<=*n*,<=<=1<=β€<=*w**i*<=β€<=9). | Print a single integerΒ β the number of interesting (by ZS the Coder's consideration) pairs. | [
"6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7\n",
"5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3\n"
] | [
"7\n",
"8\n"
] | In the first sample case, the interesting pairs are (0,β4),β(1,β2),β(1,β5),β(3,β2),β(2,β5),β(5,β2),β(3,β5). The numbers that are formed by these pairs are 14,β21,β217,β91,β7,β7,β917 respectively, which are all multiples of 7. Note that (2,β5) and (5,β2) are considered different.
In the second sample case, the interesting pairs are (4,β0),β(0,β4),β(3,β2),β(2,β3),β(0,β1),β(1,β0),β(4,β1),β(1,β4), and 6 of these pairs give the number 33 while 2 of them give the number 3333, which are all multiples of 11. | [
{
"input": "6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7",
"output": "7"
},
{
"input": "5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3",
"output": "8"
},
{
"input": "4 3\n0 1 4\n1 2 4\n2 3 4",
"output": "2"
},
{
"input": "2 7\n1 0 9",
"output": "0"
},
{
"input": "2 7\n1 0 7",
"ou... | 0 | 0 | -1 | 68,701 | |
358 | Dima and Hares | [
"dp",
"greedy"
] | null | null | Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her *n* hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to *n* from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and *n* don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :) | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=3000) β the number of hares. Then three lines follow, each line has *n* integers. The first line contains integers *a*1 *a*2 ... *a**n*. The second line contains *b*1,<=*b*2,<=...,<=*b**n*. The third line contains *c*1,<=*c*2,<=...,<=*c**n*. The following limits are fulfilled: 0<=β€<=*a**i*,<=*b**i*,<=*c**i*<=β€<=105.
Number *a**i* in the first line shows the joy that hare number *i* gets if his adjacent hares are both hungry. Number *b**i* in the second line shows the joy that hare number *i* radiates if he has exactly one full adjacent hare. Number *Ρ**i* in the third line shows the joy that hare number *i* radiates if both his adjacent hares are full. | In a single line, print the maximum possible total joy of the hares Inna can get by feeding them. | [
"4\n1 2 3 4\n4 3 2 1\n0 1 1 0\n",
"7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3\n",
"3\n1 1 1\n1 2 1\n1 1 1\n"
] | [
"13\n",
"44\n",
"4\n"
] | none | [
{
"input": "4\n1 2 3 4\n4 3 2 1\n0 1 1 0",
"output": "13"
},
{
"input": "7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3",
"output": "44"
},
{
"input": "3\n1 1 1\n1 2 1\n1 1 1",
"output": "4"
},
{
"input": "7\n1 3 8 9 3 4 4\n6 0 6 6 1 8 4\n9 6 3 7 8 8 2",
"output": "42"
... | 170 | 2,252,800 | 3 | 68,756 | |
250 | Mad Joe | [
"brute force"
] | null | null | Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path.
Joe's house has *n* floors, each floor is a segment of *m* cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right.
Now Joe is on the *n*-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right.
Joe moves by a particular algorithm. Every second he makes one of the following actions:
- If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved. - Otherwise consider the next cell in the current direction of the gaze. If the cell is empty, then Joe moves into it, the gaze direction is preserved. - If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite. - If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits).
Joe calms down as soon as he reaches any cell of the first floor.
The figure below shows an example Joe's movements around the house.
Determine how many seconds Joe will need to calm down. | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=100, 1<=β€<=*m*<=β€<=104).
Next *n* lines contain the description of Joe's house. The *i*-th of these lines contains the description of the (*n*<=-<=*i*<=+<=1)-th floor of the house β a line that consists of *m* characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall.
It is guaranteed that the first cell of the *n*-th floor is empty. | Print a single number β the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen.
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 5\n..+.#\n#+..+\n+.#+.\n",
"4 10\n...+.##+.+\n+#++..+++#\n++.#++++..\n.+##.++#.+\n",
"2 2\n..\n++\n"
] | [
"14",
"42\n",
"Never"
] | none | [
{
"input": "3 5\n..+.#\n#+..+\n+.#+.",
"output": "14"
},
{
"input": "4 10\n...+.##+.+\n+#++..+++#\n++.#++++..\n.+##.++#.+",
"output": "42"
},
{
"input": "2 2\n..\n++",
"output": "Never"
},
{
"input": "5 1\n.\n.\n.\n.\n.",
"output": "4"
},
{
"input": "20 20\n..+#+.... | 46 | 0 | 0 | 68,770 | |
550 | Regular Bridge | [
"constructive algorithms",
"graphs",
"implementation"
] | null | null | An undirected graph is called *k*-regular, if the degrees of all its vertices are equal *k*. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components.
Build a connected undirected *k*-regular graph containing at least one bridge, or else state that such graph doesn't exist. | The single line of the input contains integer *k* (1<=β€<=*k*<=β€<=100) β the required degree of the vertices of the regular graph. | Print "NO" (without quotes), if such graph doesn't exist.
Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines.
The description of the made graph must start with numbers *n* and *m* β the number of vertices and edges respectively.
Each of the next *m* lines must contain two integers, *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=*n*, *a*<=β <=*b*), that mean that there is an edge connecting the vertices *a* and *b*. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equal *k*. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order.
The constructed graph must contain at most 106 vertices and 106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most 106 vertices and at most 106 edges). | [
"1\n"
] | [
"YES\n2 1\n1 2\n"
] | In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge. | [
{
"input": "1",
"output": "YES\n2 1\n1 2"
},
{
"input": "3",
"output": "YES\n10 15\n1 6\n1 2\n1 3\n2 4\n2 5\n3 4\n3 5\n4 5\n6 7\n6 8\n7 9\n7 10\n8 9\n8 10\n9 10"
},
{
"input": "11",
"output": "YES\n26 143\n1 14\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n2 12\n2 13\n2 4\n2 5... | 62 | 102,400 | 3 | 68,832 | |
158 | Ice Sculptures | [
"*special",
"brute force",
"number theory"
] | null | null | The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus *n* ice sculptures were erected. The sculptures are arranged in a circle at equal distances from each other, so they form a regular *n*-gon. They are numbered in clockwise order with numbers from 1 to *n*.
The site of the University has already conducted a voting that estimated each sculpture's characteristic of *t**i* β the degree of the sculpture's attractiveness. The values of *t**i* can be positive, negative or zero.
When the university rector came to evaluate the work, he said that this might be not the perfect arrangement. He suggested to melt some of the sculptures so that:
- the remaining sculptures form a regular polygon (the number of vertices should be between 3 and *n*), - the sum of the *t**i* values of the remaining sculptures is maximized.
Help the Vice Rector to analyze the criticism β find the maximum value of *t**i* sum which can be obtained in this way. It is allowed not to melt any sculptures at all. The sculptures can not be moved. | The first input line contains an integer *n* (3<=β€<=*n*<=β€<=20000) β the initial number of sculptures. The second line contains a sequence of integers *t*1,<=*t*2,<=...,<=*t**n*, *t**i* β the degree of the *i*-th sculpture's attractiveness (<=-<=1000<=β€<=*t**i*<=β€<=1000). The numbers on the line are separated by spaces. | Print the required maximum sum of the sculptures' attractiveness. | [
"8\n1 2 -3 4 -5 5 2 3\n",
"6\n1 -2 3 -4 5 -6\n",
"6\n1 2 3 4 5 6\n"
] | [
"14\n",
"9\n",
"21\n"
] | In the first sample it is best to leave every second sculpture, that is, leave sculptures with attractivenesses: 2, 4, 5 ΠΈ 3. | [
{
"input": "8\n1 2 -3 4 -5 5 2 3",
"output": "14"
},
{
"input": "6\n1 -2 3 -4 5 -6",
"output": "9"
},
{
"input": "6\n1 2 3 4 5 6",
"output": "21"
},
{
"input": "4\n1 -10 1 -10",
"output": "-18"
},
{
"input": "6\n1 0 1 0 1 0",
"output": "3"
},
{
"input"... | 216 | 4,403,200 | 3 | 68,893 | |
757 | Can Bash Save the Day? | [
"data structures",
"divide and conquer",
"graphs",
"trees"
] | null | null | Whoa! You did a great job helping Team Rocket who managed to capture all the Pokemons sent by Bash. Meowth, part of Team Rocket, having already mastered the human language, now wants to become a master in programming as well. He agrees to free the Pokemons if Bash can answer his questions.
Initially, Meowth gives Bash a weighted tree containing *n* nodes and a sequence *a*1,<=*a*2...,<=*a**n* which is a permutation of 1,<=2,<=...,<=*n*. Now, Mewoth makes *q* queries of one of the following forms:
- 1 l r v: meaning Bash should report , where *dist*(*a*,<=*b*) is the length of the shortest path from node *a* to node *b* in the given tree. - 2 x: meaning Bash should swap *a**x* and *a**x*<=+<=1 in the given sequence. This new sequence is used for later queries.
Help Bash to answer the questions! | The first line contains two integers *n* and *q* (1<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*q*<=β€<=2Β·105)Β β the number of nodes in the tree and the number of queries, respectively.
The next line contains *n* space-separated integersΒ β the sequence *a*1,<=*a*2,<=...,<=*a**n* which is a permutation of 1,<=2,<=...,<=*n*.
Each of the next *n*<=-<=1 lines contain three space-separated integers *u*, *v*, and *w* denoting that there exists an undirected edge between node *u* and node *v* of weight *w*, (1<=β€<=*u*,<=*v*<=β€<=*n*, *u*<=β <=*v*, 1<=β€<=*w*<=β€<=106). It is guaranteed that the given graph is a tree.
Each query consists of two lines. First line contains single integer *t*, indicating the type of the query. Next line contains the description of the query:
- t = 1: Second line contains three integers *a*, *b* and *c* (1<=β€<=*a*,<=*b*,<=*c*<=<<=230) using which *l*, *r* and *v* can be generated using the formula given below: , - , - . - .
The *ans**i* is the answer for the *i*-th query, assume that *ans*0<==<=0. If the *i*-th query is of type 2 then *ans**i* = *ans**i*<=-<=1. It is guaranteed that:
- for each query of type 1: 1<=β€<=*l*<=β€<=*r*<=β€<=*n*, 1<=β€<=*v*<=β€<=*n*, - for each query of type 2: 1<=β€<=*x*<=β€<=*n*<=-<=1.
The operation means bitwise exclusive OR. | For each query of type 1, output a single integer in a separate line, denoting the answer to the query. | [
"5 5\n4 5 1 3 2\n4 2 4\n1 3 9\n4 1 4\n4 5 2\n1\n1 5 4\n1\n22 20 20\n2\n38\n2\n39\n1\n36 38 38\n"
] | [
"23\n37\n28\n"
] | In the sample, the actual queries are the following:
- 1 1 5 4 - 1 1 3 3 - 2 3 - 2 2 - 1 1 3 3 | [] | 46 | 0 | 0 | 69,018 | |
604 | Uncowed Forces | [
"implementation"
] | null | null | Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himself from the herd as the best cowmputer scientist in all of Bovinia. Kevin knows his submission time for each problem, the number of wrong submissions that he made on each problem, and his total numbers of successful and unsuccessful hacks. Because Codeforces scoring is complicated, Kevin wants you to write a program to compute his final score.
Codeforces scores are computed as follows: If the maximum point value of a problem is *x*, and Kevin submitted correctly at minute *m* but made *w* wrong submissions, then his score on that problem is . His total score is equal to the sum of his scores for each problem. In addition, Kevin's total score gets increased by 100 points for each successful hack, but gets decreased by 50 points for each unsuccessful hack.
All arithmetic operations are performed with absolute precision and no rounding. It is guaranteed that Kevin's final score is an integer. | The first line of the input contains five space-separated integers *m*1, *m*2, *m*3, *m*4, *m*5, where *m**i* (0<=β€<=*m**i*<=β€<=119) is the time of Kevin's last submission for problem *i*. His last submission is always correct and gets accepted.
The second line contains five space-separated integers *w*1, *w*2, *w*3, *w*4, *w*5, where *w**i* (0<=β€<=*w**i*<=β€<=10) is Kevin's number of wrong submissions on problem *i*.
The last line contains two space-separated integers *h**s* and *h**u* (0<=β€<=*h**s*,<=*h**u*<=β€<=20), denoting the Kevin's numbers of successful and unsuccessful hacks, respectively. | Print a single integer, the value of Kevin's final score. | [
"20 40 60 80 100\n0 1 2 3 4\n1 0\n",
"119 119 119 119 119\n0 0 0 0 0\n10 0\n"
] | [
"4900\n",
"4930\n"
] | In the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/42158dc2bc78cd21fa679530ae9ef8b9ea298d15.png" style="max-width: 100.0%;max-height: 100.0%;"/> of the points on each problem. So his score from solving problems is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fdf392d8508500b57f8057ac0c4c892ab5f925a2.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Adding in 10Β·100β=β1000 points from hacks, his total score becomes 3930β+β1000β=β4930. | [
{
"input": "20 40 60 80 100\n0 1 2 3 4\n1 0",
"output": "4900"
},
{
"input": "119 119 119 119 119\n0 0 0 0 0\n10 0",
"output": "4930"
},
{
"input": "3 6 13 38 60\n6 10 10 3 8\n9 9",
"output": "5088"
},
{
"input": "21 44 11 68 75\n6 2 4 8 4\n2 8",
"output": "4522"
},
{... | 77 | 6,963,200 | 3 | 0 | |
281 | Word Capitalization | [
"implementation",
"strings"
] | null | null | Capitalization is writing a word with its first letter as a capital letter. Your task is to capitalize the given word.
Note, that during capitalization all the letters except the first one remains unchanged. | A single line contains a non-empty word. This word consists of lowercase and uppercase English letters. The length of the word will not exceed 103. | Output the given word after capitalization. | [
"ApPLe\n",
"konjac\n"
] | [
"ApPLe\n",
"Konjac\n"
] | none | [
{
"input": "ApPLe",
"output": "ApPLe"
},
{
"input": "konjac",
"output": "Konjac"
},
{
"input": "a",
"output": "A"
},
{
"input": "A",
"output": "A"
},
{
"input": "z",
"output": "Z"
},
{
"input": "ABACABA",
"output": "ABACABA"
},
{
"input": "... | 92 | 0 | 3 | 1 | |
322 | Ciel and Flowers | [
"combinatorics",
"math"
] | null | null | Fox Ciel has some flowers: *r* red flowers, *g* green flowers and *b* blue flowers. She wants to use these flowers to make several bouquets. There are 4 types of bouquets:
- To make a "red bouquet", it needs 3 red flowers. - To make a "green bouquet", it needs 3 green flowers. - To make a "blue bouquet", it needs 3 blue flowers. - To make a "mixing bouquet", it needs 1 red, 1 green and 1 blue flower.
Help Fox Ciel to find the maximal number of bouquets she can make. | The first line contains three integers *r*, *g* and *b* (0<=β€<=*r*,<=*g*,<=*b*<=β€<=109) β the number of red, green and blue flowers. | Print the maximal number of bouquets Fox Ciel can make. | [
"3 6 9\n",
"4 4 4\n",
"0 0 0\n"
] | [
"6\n",
"4\n",
"0\n"
] | In test case 1, we can make 1 red bouquet, 2 green bouquets and 3 blue bouquets.
In test case 2, we can make 1 red, 1 green, 1 blue and 1 mixing bouquet. | [
{
"input": "3 6 9",
"output": "6"
},
{
"input": "4 4 4",
"output": "4"
},
{
"input": "0 0 0",
"output": "0"
},
{
"input": "0 3 6",
"output": "3"
},
{
"input": "7 8 9",
"output": "7"
},
{
"input": "8 8 9",
"output": "8"
},
{
"input": "15 3 9... | 78 | 102,400 | 3 | 2 | |
298 | Snow Footprints | [
"greedy",
"implementation"
] | null | null | There is a straight snowy road, divided into *n* blocks. The blocks are numbered from 1 to *n* from left to right. If one moves from the *i*-th block to the (*i*<=+<=1)-th block, he will leave a right footprint on the *i*-th block. Similarly, if one moves from the *i*-th block to the (*i*<=-<=1)-th block, he will leave a left footprint on the *i*-th block. If there already is a footprint on the *i*-th block, the new footprint will cover the old one.
At the beginning, there were no footprints. Then polar bear Alice starts from the *s*-th block, makes a sequence of moves and ends in the *t*-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of *s*,<=*t* by looking at the footprints. | The first line of the input contains integer *n* (3<=β€<=*n*<=β€<=1000).
The second line contains the description of the road β the string that consists of *n* characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | Print two space-separated integers β the values of *s* and *t*. If there are several possible solutions you can print any of them. | [
"9\n..RRLL...\n",
"11\n.RRRLLLLL..\n"
] | [
"3 4\n",
"7 5\n"
] | The first test sample is the one in the picture. | [
{
"input": "11\n.RRRLLLLL..",
"output": "7 5"
},
{
"input": "4\n.RL.",
"output": "3 2"
},
{
"input": "3\n.L.",
"output": "2 1"
},
{
"input": "3\n.R.",
"output": "2 3"
}
] | 0 | 0 | -1 | 3 | |
804 | Minimum number of steps | [
"combinatorics",
"greedy",
"implementation",
"math"
] | null | null | We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109<=+<=7.
The string "ab" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string. | The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106. | Print the minimum number of steps modulo 109<=+<=7. | [
"ab\n",
"aab\n"
] | [
"1\n",
"3\n"
] | The first example: "ab" βββ "bba".
The second example: "aab" βββ "abba" βββ "bbaba" βββ "bbbbaa". | [
{
"input": "ab",
"output": "1"
},
{
"input": "aab",
"output": "3"
},
{
"input": "aaaaabaabababaaaaaba",
"output": "17307"
},
{
"input": "abaabaaabbabaabab",
"output": "1795"
},
{
"input": "abbaa",
"output": "2"
},
{
"input": "abbaaabaabaaaaabbbbaababaa... | 218 | 6,348,800 | 3 | 4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.