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
0
none
[ "none" ]
null
null
A subsequence of length |*x*| of string *s*<==<=*s*1*s*2... *s*|*s*| (where |*s*| is the length of string *s*) is a string *x*<==<=*s**k*1*s**k*2... *s**k*|*x*| (1<=≀<=*k*1<=&lt;<=*k*2<=&lt;<=...<=&lt;<=*k*|*x*|<=≀<=|*s*|). You've got two stringsΒ β€” *s* and *t*. Let's consider all subsequences of string *s*, coinciding with string *t*. Is it true that each character of string *s* occurs in at least one of these subsequences? In other words, is it true that for all *i* (1<=≀<=*i*<=≀<=|*s*|), there is such subsequence *x*<==<=*s**k*1*s**k*2... *s**k*|*x*| of string *s*, that *x*<==<=*t* and for some *j* (1<=≀<=*j*<=≀<=|*x*|) *k**j*<==<=*i*.
The first line contains string *s*, the second line contains string *t*. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105.
Print "Yes" (without the quotes), if each character of the string *s* occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
[ "abab\nab\n", "abacaba\naba\n", "abc\nba\n" ]
[ "Yes\n", "No\n", "No\n" ]
In the first sample string *t* can occur in the string *s* as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string *s* occurs at least once. In the second sample the 4-th character of the string *s* doesn't occur in any occurrence of string *t*. In the third sample there is no occurrence of string *t* in string *s*.
[ { "input": "abab\nab", "output": "Yes" }, { "input": "abacaba\naba", "output": "No" }, { "input": "abc\nba", "output": "No" }, { "input": "babbbbbaba\nab", "output": "No" }, { "input": "accbacabaa\nbada", "output": "No" }, { "input": "aaaaaaaaaaaaaaaaa...
30
0
0
52,058
594
Max and Bike
[ "binary search", "geometry" ]
null
null
For months Maxim has been coming to work on his favorite bicycle. And quite recently he decided that he is ready to take part in a cyclists' competitions. He knows that this year *n* competitions will take place. During the *i*-th competition the participant must as quickly as possible complete a ride along a straight line from point *s**i* to point *f**i* (*s**i*<=&lt;<=*f**i*). Measuring time is a complex process related to usage of a special sensor and a time counter. Think of the front wheel of a bicycle as a circle of radius *r*. Let's neglect the thickness of a tire, the size of the sensor, and all physical effects. The sensor is placed on the rim of the wheel, that is, on some fixed point on a circle of radius *r*. After that the counter moves just like the chosen point of the circle, i.e. moves forward and rotates around the center of the circle. At the beginning each participant can choose any point *b**i*, such that his bike is fully behind the starting line, that is, *b**i*<=&lt;<=*s**i*<=-<=*r*. After that, he starts the movement, instantly accelerates to his maximum speed and at time *ts**i*, when the coordinate of the sensor is equal to the coordinate of the start, the time counter starts. The cyclist makes a complete ride, moving with his maximum speed and at the moment the sensor's coordinate is equal to the coordinate of the finish (moment of time *tf**i*), the time counter deactivates and records the final time. Thus, the counter records that the participant made a complete ride in time *tf**i*<=-<=*ts**i*. Maxim is good at math and he suspects that the total result doesn't only depend on his maximum speed *v*, but also on his choice of the initial point *b**i*. Now Maxim is asking you to calculate for each of *n* competitions the minimum possible time that can be measured by the time counter. The radius of the wheel of his bike is equal to *r*.
The first line contains three integers *n*, *r* and *v* (1<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*r*,<=*v*<=≀<=109)Β β€” the number of competitions, the radius of the front wheel of Max's bike and his maximum speed, respectively. Next *n* lines contain the descriptions of the contests. The *i*-th line contains two integers *s**i* and *f**i* (1<=≀<=*s**i*<=&lt;<=*f**i*<=≀<=109)Β β€” the coordinate of the start and the coordinate of the finish on the *i*-th competition.
Print *n* real numbers, the *i*-th number should be equal to the minimum possible time measured by the time counter. Your answer will be considered correct if its absolute or relative error will not exceed 10<=-<=6. Namely: let's assume that your answer equals *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if .
[ "2 1 2\n1 10\n5 9\n" ]
[ "3.849644710502\n1.106060157705\n" ]
none
[ { "input": "2 1 2\n1 10\n5 9", "output": "3.849644710502\n1.106060157705" }, { "input": "1 9 2\n8 11", "output": "0.750434631667" }, { "input": "5 9 2\n15 18\n13 15\n9 16\n5 21\n10 11", "output": "0.750434631667\n0.500128680269\n1.755555861769\n4.068584337764\n0.250016077584" }, ...
2,000
16,486,400
0
52,113
69
Bets
[ "greedy", "implementation" ]
B. Bets
2
256
In Chelyabinsk lives a much respected businessman Nikita with a strange nickname "Boss". Once Nikita decided to go with his friend Alex to the Summer Biathlon World Cup. Nikita, as a very important person, received a token which allows to place bets on each section no more than on one competitor. To begin with friends learned the rules: in the race there are *n* sections of equal length and *m* participants. The participants numbered from 1 to *m*. About each participant the following is known: - *l**i* β€” the number of the starting section, - *r**i* β€” the number of the finishing section (*l**i*<=≀<=*r**i*),- *t**i* β€” the time a biathlete needs to complete an section of the path,- *c**i* β€” the profit in roubles. If the *i*-th sportsman wins on one of the sections, the profit will be given to the man who had placed a bet on that sportsman. The *i*-th biathlete passes the sections from *l**i* to *r**i* inclusive. The competitor runs the whole way in (*r**i*<=-<=*l**i*<=+<=1)Β·*t**i* time units. It takes him exactly *t**i* time units to pass each section. In case of the athlete's victory on *k* sections the man who has betted on him receives *k*Β·*c**i* roubles. In each section the winner is determined independently as follows: if there is at least one biathlete running this in this section, then among all of them the winner is the one who has ran this section in minimum time (spent minimum time passing this section). In case of equality of times the athlete with the smaller index number wins. If there are no participants in this section, then the winner in this section in not determined. We have to say that in the summer biathlon all the participants are moving at a constant speed. We should also add that Nikita can bet on each section and on any contestant running in this section. Help the friends find the maximum possible profit.
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100). Then follow *m* lines, each containing 4 integers *l**i*, *r**i*, *t**i*, *c**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*, 1<=≀<=*t**i*,<=*c**i*<=≀<=1000).
Print a single integer, the maximal profit in roubles that the friends can get. In each of *n* sections it is not allowed to place bets on more than one sportsman.
[ "4 4\n1 4 20 5\n1 3 21 10\n3 3 4 30\n3 4 4 20\n", "8 4\n1 5 24 10\n2 4 6 15\n4 6 30 50\n6 7 4 20\n" ]
[ "60", "105" ]
In the first test the optimal bet is: in the 1-2 sections on biathlete 1, in section 3 on biathlete 3, in section 4 on biathlete 4. Total: profit of 5 rubles for 1 section, the profit of 5 rubles for 2 section, profit of 30 rubles for a 3 section, profit of 20 rubles for 4 section. Total profit 60 rubles. In the second test the optimal bet is: on 1 and 5 sections on biathlete 1, in the 2-4 sections on biathlete 2, in the 6-7 sections on athlete 4. There is no winner in the 8 section. Total: profit of 10 rubles for 1 section, the profit of 15 rubles for 2,3,4 section, profit of 10 rubles for a 5 section, profit of 20 rubles for 6, 7 section. Total profit 105 rubles.
[ { "input": "4 4\n1 4 20 5\n1 3 21 10\n3 3 4 30\n3 4 4 20", "output": "60" }, { "input": "8 4\n1 5 24 10\n2 4 6 15\n4 6 30 50\n6 7 4 20", "output": "105" }, { "input": "2 2\n1 2 3 1\n2 2 3 10", "output": "2" }, { "input": "20 30\n15 17 54 46\n4 18 26 18\n18 20 49 94\n12 12 83 ...
154
5,632,000
3.95101
52,168
103
Russian Roulette
[ "constructive algorithms", "greedy" ]
C. Russian Roulette
2
256
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 &lt; 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> &lt; *b*<sub class="lower-index">*i*</sub>, and for any *j* (1 ≀ *j* &lt; *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", ...
186
307,200
0
52,289
723
Lakes in Berland
[ "dfs and similar", "dsu", "graphs", "greedy", "implementation" ]
null
null
The map of Berland is a rectangle of the size *n*<=Γ—<=*m*, which consists of cells of size 1<=Γ—<=1. Each cell is either land or water. The map is surrounded by the ocean. Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell. You task is to fill up with the earth the minimum number of water cells so that there will be exactly *k* lakes in Berland. Note that the initial number of lakes on the map is not less than *k*.
The first line of the input contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=50, 0<=≀<=*k*<=≀<=50)Β β€” the sizes of the map and the number of lakes which should be left on the map. The next *n* lines contain *m* characters each β€” the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land). It is guaranteed that the map contain at least *k* lakes.
In the first line print the minimum number of cells which should be transformed from water to land. In the next *n* lines print *m* symbols β€” the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them. It is guaranteed that the answer exists on the given data.
[ "5 4 1\n****\n*..*\n****\n**.*\n..**\n", "3 3 0\n***\n*.*\n***\n" ]
[ "1\n****\n*..*\n****\n****\n..**\n", "1\n***\n***\n***\n" ]
In the first example there are only two lakes β€” the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.
[ { "input": "5 4 1\n****\n*..*\n****\n**.*\n..**", "output": "1\n****\n*..*\n****\n****\n..**" }, { "input": "3 3 0\n***\n*.*\n***", "output": "1\n***\n***\n***" }, { "input": "3 5 1\n.**.*\n*.*.*\n***..", "output": "0\n.**.*\n*.*.*\n***.." }, { "input": "3 5 0\n.**.*\n*.*.*\n...
31
0
0
52,471
0
none
[ "none" ]
null
null
Polycarpus got an internship in one well-known social network. His test task is to count the number of unique users who have visited a social network during the day. Polycarpus was provided with information on all user requests for this time period. For each query, we know its time... and nothing else, because Polycarpus has already accidentally removed the user IDs corresponding to the requests from the database. Thus, it is now impossible to determine whether any two requests are made by the same person or by different people. But wait, something is still known, because that day a record was achieved β€” *M* simultaneous users online! In addition, Polycarpus believes that if a user made a request at second *s*, then he was online for *T* seconds after that, that is, at seconds *s*, *s*<=+<=1, *s*<=+<=2, ..., *s*<=+<=*T*<=-<=1. So, the user's time online can be calculated as the union of time intervals of the form [*s*,<=*s*<=+<=*T*<=-<=1] over all times *s* of requests from him. Guided by these thoughts, Polycarpus wants to assign a user ID to each request so that: - the number of different users online did not exceed *M* at any moment, - at some second the number of distinct users online reached value *M*, - the total number of users (the number of distinct identifiers) was as much as possible. Help Polycarpus cope with the test.
The first line contains three integers *n*, *M* and *T* (1<=≀<=*n*,<=*M*<=≀<=20<=000, 1<=≀<=*T*<=≀<=86400) β€” the number of queries, the record number of online users and the time when the user was online after a query was sent. Next *n* lines contain the times of the queries in the format "hh:mm:ss", where hh are hours, mm are minutes, ss are seconds. The times of the queries follow in the non-decreasing order, some of them can coincide. It is guaranteed that all the times and even all the segments of type [*s*,<=*s*<=+<=*T*<=-<=1] are within one 24-hour range (from 00:00:00 to 23:59:59).
In the first line print number *R* β€” the largest possible number of distinct users. The following *n* lines should contain the user IDs for requests in the same order in which the requests are given in the input. User IDs must be integers from 1 to *R*. The requests of the same user must correspond to the same identifiers, the requests of distinct users must correspond to distinct identifiers. If there are multiple solutions, print any of them. If there is no solution, print "No solution" (without the quotes).
[ "4 2 10\n17:05:53\n17:05:58\n17:06:01\n22:39:47\n", "1 2 86400\n00:00:00\n" ]
[ "3\n1\n2\n2\n3\n", "No solution\n" ]
Consider the first sample. The user who sent the first request was online from 17:05:53 to 17:06:02, the user who sent the second request was online from 17:05:58 to 17:06:07, the user who sent the third request, was online from 17:06:01 to 17:06:10. Thus, these IDs cannot belong to three distinct users, because in that case all these users would be online, for example, at 17:06:01. That is impossible, because *M* = 2. That means that some two of these queries belonged to the same user. One of the correct variants is given in the answer to the sample. For it user 1 was online from 17:05:53 to 17:06:02, user 2 β€” from 17:05:58 to 17:06:10 (he sent the second and third queries), user 3 β€” from 22:39:47 to 22:39:56. In the second sample there is only one query. So, only one user visited the network within the 24-hour period and there couldn't be two users online on the network simultaneously. (The time the user spent online is the union of time intervals for requests, so users who didn't send requests could not be online in the network.)
[]
31
0
0
52,505
439
Devu and Partitioning of the Array
[ "brute force", "constructive algorithms", "implementation", "number theory" ]
null
null
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into *k* disjoint non-empty parts such that *p* of the parts have even sum (each of them must have even sum) and remaining *k* - *p* have odd sum? (note that parts need not to be continuous). If it is possible to partition the array, also give any possible way of valid partitioning.
The first line will contain three space separated integers *n*, *k*, *p* (1<=≀<=*k*<=≀<=*n*<=≀<=105;Β 0<=≀<=*p*<=≀<=*k*). The next line will contain *n* space-separated distinct integers representing the content of array *a*: *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109).
In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print *k* lines after the first line. The *i**th* of them should contain the content of the *i**th* part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly *p* parts with even sum, each of the remaining *k* - *p* parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition.
[ "5 5 3\n2 6 10 5 9\n", "5 5 3\n7 14 2 9 5\n", "5 3 1\n1 2 3 7 5\n" ]
[ "YES\n1 9\n1 5\n1 10\n1 6\n1 2\n", "NO\n", "YES\n3 5 1 3\n1 7\n1 2\n" ]
none
[ { "input": "5 5 3\n2 6 10 5 9", "output": "YES\n1 9\n1 5\n1 10\n1 6\n1 2" }, { "input": "5 5 3\n7 14 2 9 5", "output": "NO" }, { "input": "5 3 1\n1 2 3 7 5", "output": "YES\n3 5 1 3\n1 7\n1 2" }, { "input": "10 5 3\n194757070 828985446 11164 80016 84729 117765558 111730436 16...
482
16,076,800
3
52,519
756
Byteland coins
[ "combinatorics", "dp", "math" ]
null
null
There are *n* types of coins in Byteland. Conveniently, the denomination of the coin type *k* divides the denomination of the coin type *k*<=+<=1, the denomination of the coin type 1 equals 1 tugrick. The ratio of the denominations of coin types *k*<=+<=1 and *k* equals *a**k*. It is known that for each *x* there are at most 20 coin types of denomination *x*. Byteasar has *b**k* coins of type *k* with him, and he needs to pay exactly *m* tugricks. It is known that Byteasar never has more than 3Β·105 coins with him. Byteasar want to know how many ways there are to pay exactly *m* tugricks. Two ways are different if there is an integer *k* such that the amount of coins of type *k* differs in these two ways. As all Byteland citizens, Byteasar wants to know the number of ways modulo 109<=+<=7.
The first line contains single integer *n* (1<=≀<=*n*<=≀<=3Β·105)Β β€” the number of coin types. The second line contains *n*<=-<=1 integers *a*1, *a*2, ..., *a**n*<=-<=1 (1<=≀<=*a**k*<=≀<=109)Β β€” the ratios between the coin types denominations. It is guaranteed that for each *x* there are at most 20 coin types of denomination *x*. The third line contains *n* non-negative integers *b*1, *b*2, ..., *b**n*Β β€” the number of coins of each type Byteasar has. It is guaranteed that the sum of these integers doesn't exceed 3Β·105. The fourth line contains single integer *m* (0<=≀<=*m*<=&lt;<=1010000)Β β€” the amount in tugricks Byteasar needs to pay.
Print single integerΒ β€” the number of ways to pay exactly *m* tugricks modulo 109<=+<=7.
[ "1\n\n4\n2\n", "2\n1\n4 4\n2\n", "3\n3 3\n10 10 10\n17\n" ]
[ "1\n", "3\n", "6\n" ]
In the first example Byteasar has 4 coins of denomination 1, and he has to pay 2 tugricks. There is only one way. In the second example Byteasar has 4 coins of each of two different types of denomination 1, he has to pay 2 tugricks. There are 3 ways: pay one coin of the first type and one coin of the other, pay two coins of the first type, and pay two coins of the second type. In the third example the denominations are equal to 1, 3, 9.
[]
46
4,608,000
0
52,647
758
Geometrical Progression
[ "brute force", "math", "number theory" ]
null
null
For given *n*, *l* and *r* find the number of distinct geometrical progression, each of which contains *n* distinct integers not less than *l* and not greater than *r*. In other words, for each progression the following must hold: *l*<=≀<=*a**i*<=≀<=*r* and *a**i*<=β‰ <=*a**j* , where *a*1,<=*a*2,<=...,<=*a**n* is the geometrical progression, 1<=≀<=*i*,<=*j*<=≀<=*n* and *i*<=β‰ <=*j*. Geometrical progression is a sequence of numbers *a*1,<=*a*2,<=...,<=*a**n* where each term after first is found by multiplying the previous one by a fixed non-zero number *d* called the common ratio. Note that in our task *d* may be non-integer. For example in progression 4,<=6,<=9, common ratio is . Two progressions *a*1,<=*a*2,<=...,<=*a**n* and *b*1,<=*b*2,<=...,<=*b**n* are considered different, if there is such *i* (1<=≀<=*i*<=≀<=*n*) that *a**i*<=β‰ <=*b**i*.
The first and the only line cotains three integers *n*, *l* and *r* (1<=≀<=*n*<=≀<=107,<=1<=≀<=*l*<=≀<=*r*<=≀<=107).
Print the integer *K*Β β€” is the answer to the problem.
[ "1 1 10\n", "2 6 9\n", "3 1 10\n", "3 3 10\n" ]
[ "10", "12", "8", "2" ]
These are possible progressions for the first test of examples: - 1; - 2; - 3; - 4; - 5; - 6; - 7; - 8; - 9; - 10. These are possible progressions for the second test of examples: - 6, 7; - 6, 8; - 6, 9; - 7, 6; - 7, 8; - 7, 9; - 8, 6; - 8, 7; - 8, 9; - 9, 6; - 9, 7; - 9, 8. These are possible progressions for the third test of examples: - 1, 2, 4; - 1, 3, 9; - 2, 4, 8; - 4, 2, 1; - 4, 6, 9; - 8, 4, 2; - 9, 3, 1; - 9, 6, 4. These are possible progressions for the fourth test of examples: - 4, 6, 9; - 9, 6, 4.
[ { "input": "1 1 10", "output": "10" }, { "input": "2 6 9", "output": "12" }, { "input": "3 1 10", "output": "8" }, { "input": "3 3 10", "output": "2" }, { "input": "1 25 845", "output": "821" }, { "input": "2 25 845", "output": "673220" }, { ...
30
0
0
52,712
116
Little Pigs and Wolves
[ "greedy", "implementation" ]
null
null
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size *n*<=Γ—<=*m*. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig. But each wolf may be adjacent to any number of little pigs. They have been living peacefully for several years. But today the wolves got hungry. One by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor little pig. This process is not repeated. That is, each wolf will get to eat at most one little pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf. What is the maximum number of little pigs that may be eaten by the wolves?
The first line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow *n* lines containing *m* characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf. It is guaranteed that there will be at most one wolf adjacent to any little pig.
Print a single number β€” the maximal number of little pigs that may be eaten by the wolves.
[ "2 3\nPPW\nW.P\n", "3 3\nP.W\n.P.\nW.P\n" ]
[ "2\n", "0\n" ]
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
[ { "input": "2 3\nPPW\nW.P", "output": "2" }, { "input": "3 3\nP.W\n.P.\nW.P", "output": "0" }, { "input": "1 1\nP", "output": "0" }, { "input": "2 6\nWW..WW\n.PPPP.", "output": "2" }, { "input": "6 2\n.W\n.W\n.P\nWP\n.P\nPW", "output": "3" }, { "input"...
0
0
-1
52,741
721
Maxim and Array
[ "constructive algorithms", "data structures", "greedy", "math" ]
null
null
Recently Maxim has found an array of *n* integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer *x* and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer *i* (1<=≀<=*i*<=≀<=*n*) and replaces the *i*-th element of array *a**i* either with *a**i*<=+<=*x* or with *a**i*<=-<=*x*. Please note that the operation may be applied more than once to the same position. Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. ) can reach, if Maxim would apply no more than *k* operations to it. Please help him in that.
The first line of the input contains three integers *n*,<=*k* and *x* (1<=≀<=*n*,<=*k*<=≀<=200<=000,<=1<=≀<=*x*<=≀<=109)Β β€” the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* ()Β β€” the elements of the array found by Maxim.
Print *n* integers *b*1,<=*b*2,<=...,<=*b**n* in the only lineΒ β€” the array elements after applying no more than *k* operations to the array. In particular, should stay true for every 1<=≀<=*i*<=≀<=*n*, but the product of all array elements should be minimum possible. If there are multiple answers, print any of them.
[ "5 3 1\n5 4 3 5 2\n", "5 3 1\n5 4 3 5 5\n", "5 3 1\n5 4 4 5 5\n", "3 2 7\n5 4 2\n" ]
[ "5 4 3 5 -1 \n", "5 4 0 5 5 \n", "5 1 4 5 5 \n", "5 11 -5 \n" ]
none
[ { "input": "5 3 1\n5 4 3 5 2", "output": "5 4 3 5 -1 " }, { "input": "5 3 1\n5 4 3 5 5", "output": "5 4 0 5 5 " }, { "input": "5 3 1\n5 4 4 5 5", "output": "5 1 4 5 5 " }, { "input": "3 2 7\n5 4 2", "output": "5 11 -5 " }, { "input": "100 1 1\n-1 -1 -2 0 -2 -1 0 0...
826
24,576,000
3
52,830
370
Berland Bingo
[ "implementation" ]
null
null
Lately, a national version of a bingo game has become very popular in Berland. There are *n* players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the *i*-th player contains *m**i* numbers. During the game the host takes numbered balls one by one from a bag. He reads the number aloud in a high and clear voice and then puts the ball away. All participants cross out the number if it occurs on their cards. The person who crosses out all numbers from his card first, wins. If multiple people cross out all numbers from their cards at the same time, there are no winners in the game. At the beginning of the game the bag contains 100 balls numbered 1 through 100, the numbers of all balls are distinct. You are given the cards for each player. Write a program that determines whether a player can win the game at the most favorable for him scenario or not.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of the players. Then follow *n* lines, each line describes a player's card. The line that describes a card starts from integer *m**i* (1<=≀<=*m**i*<=≀<=100) that shows how many numbers the *i*-th player's card has. Then follows a sequence of integers *a**i*,<=1,<=*a**i*,<=2,<=...,<=*a**i*,<=*m**i* (1<=≀<=*a**i*,<=*k*<=≀<=100) β€” the numbers on the *i*-th player's card. The numbers in the lines are separated by single spaces. It is guaranteed that all the numbers on each card are distinct.
Print *n* lines, the *i*-th line must contain word "YES" (without the quotes), if the *i*-th player can win, and "NO" (without the quotes) otherwise.
[ "3\n1 1\n3 2 4 1\n2 10 11\n", "2\n1 1\n1 1\n" ]
[ "YES\nNO\nYES\n", "NO\nNO\n" ]
none
[ { "input": "3\n1 1\n3 2 4 1\n2 10 11", "output": "YES\nNO\nYES" }, { "input": "2\n1 1\n1 1", "output": "NO\nNO" }, { "input": "1\n1 1", "output": "YES" }, { "input": "2\n1 2\n1 3", "output": "YES\nYES" }, { "input": "2\n1 1\n2 1 2", "output": "YES\nNO" }, ...
46
0
0
52,929
601
Lipshitz Sequence
[ "data structures", "math" ]
null
null
A function is called Lipschitz continuous if there is a real constant *K* such that the inequality |*f*(*x*)<=-<=*f*(*y*)|<=≀<=*K*Β·|*x*<=-<=*y*| holds for all . We'll deal with a more... discrete version of this term. For an array , we define it's Lipschitz constant as follows: - if *n*<=&lt;<=2, - if *n*<=β‰₯<=2, over all 1<=≀<=*i*<=&lt;<=*j*<=≀<=*n* In other words, is the smallest non-negative integer such that |*h*[*i*]<=-<=*h*[*j*]|<=≀<=*L*Β·|*i*<=-<=*j*| holds for all 1<=≀<=*i*,<=*j*<=≀<=*n*. You are given an array of size *n* and *q* queries of the form [*l*,<=*r*]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .
The first line of the input contains two space-separated integers *n* and *q* (2<=≀<=*n*<=≀<=100<=000 and 1<=≀<=*q*<=≀<=100)Β β€” the number of elements in array and the number of queries respectively. The second line contains *n* space-separated integers (). The following *q* lines describe queries. The *i*-th of those lines contains two space-separated integers *l**i* and *r**i* (1<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=*n*).
Print the answers to all queries in the order in which they are given in the input. For the *i*-th query, print one line containing a single integerΒ β€” the sum of Lipschitz constants of all subarrays of .
[ "10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9\n", "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5\n" ]
[ "17\n82\n23\n210\n", "2\n0\n22\n59\n16\n8\n" ]
In the first query of the first sample, the Lipschitz constants of subarrays of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ddadffaf9e222576b1abb771dcd83dbad588d7fe.png" style="max-width: 100.0%;max-height: 100.0%;"/> with length at least 2 are: - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/887651f76ac57b8f2b2dbd46c6ac2335ac2a270a.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e9eed4a49028e9bd234162d9da74fd4f86481378.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/16eff35d7bd22d3e34c55fc95cc6c59212d5b1c8.png" style="max-width: 100.0%;max-height: 100.0%;"/> The answer to the query is their sum.
[ { "input": "10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9", "output": "17\n82\n23\n210" }, { "input": "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5", "output": "2\n0\n22\n59\n16\n8" }, { "input": "2 2\n0 0\n1 2\n1 2", "output": "0\n0" }, { "input": "2 2\n0 100000000\n1 2...
46
0
0
53,040
0
none
[ "none" ]
null
null
Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Let's assume that strings *s* and *t* have the same length *n*, then the function *h*(*s*,<=*t*) is defined as the number of positions in which the respective symbols of *s* and *t* are the same. Function *h*(*s*,<=*t*) can be used to define the function of Vasya distance ρ(*s*,<=*t*): Vasya found a string *s* of length *n* on the Internet. Now he wants to count how many strings *t* there are such that the Vasya distance from the string *s* attains maximum possible value. Formally speaking, *t* must satisfy the equation: . Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109<=+<=7.
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=105). The second line of the input contains a single string of length *n*, consisting of characters "ACGT".
Print a single numberΒ β€” the answer modulo 109<=+<=7.
[ "1\nC\n", "2\nAG\n", "3\nTTT\n" ]
[ "1\n", "4\n", "1\n" ]
Please note that if for two distinct strings *t*<sub class="lower-index">1</sub> and *t*<sub class="lower-index">2</sub> values ρ(*s*, *t*<sub class="lower-index">1</sub>) ΠΈ ρ(*s*, *t*<sub class="lower-index">2</sub>) are maximum among all possible *t*, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one. In the first sample, there is ρ("*C*", "*C*") = 1, for the remaining strings *t* of length 1 the value of ρ(*s*, *t*) is 0. In the second sample, ρ("*AG*", "*AG*") = ρ("*AG*", "*GA*") = ρ("*AG*", "*AA*") = ρ("*AG*", "*GG*") = 4. In the third sample, ρ("*TTT*", "*TTT*") = 27
[ { "input": "1\nC", "output": "1" }, { "input": "2\nAG", "output": "4" }, { "input": "3\nTTT", "output": "1" }, { "input": "4\nGACT", "output": "256" }, { "input": "1\nT", "output": "1" }, { "input": "2\nAG", "output": "4" }, { "input": "3\n...
62
204,800
3
53,043
932
Palindrome Partition
[ "dp", "string suffix structures", "strings" ]
null
null
Given a string *s*, find the number of ways to split *s* to substrings such that if there are *k* substrings (*p*1,<=*p*2,<=*p*3,<=...,<=*p**k*) in partition, then *p**i*<==<=*p**k*<=-<=*i*<=+<=1 for all *i* (1<=≀<=*i*<=≀<=*k*) and *k* is even. Since the number of ways can be large, print it modulo 109<=+<=7.
The only line of input contains a string *s* (2<=≀<=|*s*|<=≀<=106) of even length consisting of lowercase Latin letters.
Print one integer, the number of ways of partitioning the string modulo 109<=+<=7.
[ "abcdcdab\n", "abbababababbab\n" ]
[ "1", "3" ]
In the first case, the only way to partition the string is *ab*|*cd*|*cd*|*ab*. In the second case, the string can be partitioned as *ab*|*b*|*ab*|*ab*|*ab*|*ab*|*b*|*ab* or *ab*|*b*|*abab*|*abab*|*b*|*ab* or *abbab*|*ab*|*ab*|*abbab*.
[]
46
0
0
53,053
1,005
Berland and the Shortest Paths
[ "brute force", "dfs and similar", "graphs", "shortest paths" ]
null
null
There are $n$ cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from $1$ to $n$. It is known that, from the capital (the city with the number $1$), you can reach any other city by moving along the roads. The President of Berland plans to improve the country's road network. The budget is enough to repair exactly $n-1$ roads. The President plans to choose a set of $n-1$ roads such that: - it is possible to travel from the capital to any other city along the $n-1$ chosen roads, - if $d_i$ is the number of roads needed to travel from the capital to city $i$, moving only along the $n-1$ chosen roads, then $d_1 + d_2 + \dots + d_n$ is minimized (i.e. as minimal as possible). In other words, the set of $n-1$ roads should preserve the connectivity of the country, and the sum of distances from city $1$ to all cities should be minimized (where you can only use the $n-1$ chosen roads). The president instructed the ministry to prepare $k$ possible options to choose $n-1$ roads so that both conditions above are met. Write a program that will find $k$ possible ways to choose roads for repair. If there are fewer than $k$ ways, then the program should output all possible valid ways to choose roads.
The first line of the input contains integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5, n-1 \le m \le 2\cdot10^5, 1 \le k \le 2\cdot10^5$), where $n$ is the number of cities in the country, $m$ is the number of roads and $k$ is the number of options to choose a set of roads for repair. It is guaranteed that $m \cdot k \le 10^6$. The following $m$ lines describe the roads, one road per line. Each line contains two integers $a_i$, $b_i$ ($1 \le a_i, b_i \le n$, $a_i \ne b_i$) β€” the numbers of the cities that the $i$-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Print $t$ ($1 \le t \le k$) β€” the number of ways to choose a set of roads for repair. Recall that you need to find $k$ different options; if there are fewer than $k$ of them, then you need to find all possible different valid options. In the following $t$ lines, print the options, one per line. Print an option as a string of $m$ characters where the $j$-th character is equal to '1' if the $j$-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the $t$ lines should be different. Since it is guaranteed that $m \cdot k \le 10^6$, the total length of all the $t$ lines will not exceed $10^6$. If there are several answers, output any of them.
[ "4 4 3\n1 2\n2 3\n1 4\n4 3\n", "4 6 3\n1 2\n2 3\n1 4\n4 3\n2 4\n1 3\n", "5 6 2\n1 2\n1 3\n2 4\n2 5\n3 4\n3 5\n" ]
[ "2\n1110\n1011\n", "1\n101001\n", "2\n111100\n110110\n" ]
none
[ { "input": "4 4 3\n1 2\n2 3\n1 4\n4 3", "output": "2\n1110\n1011" }, { "input": "4 6 3\n1 2\n2 3\n1 4\n4 3\n2 4\n1 3", "output": "1\n101001" }, { "input": "5 6 2\n1 2\n1 3\n2 4\n2 5\n3 4\n3 5", "output": "2\n111100\n110110" }, { "input": "2 1 200000\n2 1", "output": "1\n1...
0
0
-1
53,157
855
Salazar Slytherin's Locket
[ "bitmasks", "dp" ]
null
null
Harry came to know from Dumbledore that Salazar Slytherin's locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black's mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry's former Defense Against the Dark Arts teacher. Harry, Ron and Hermione are infiltrating the Ministry. Upon reaching Umbridge's office, they observed a code lock with a puzzle asking them to calculate count of magic numbers between two integers *l* and *r* (both inclusive). Harry remembered from his detention time with Umbridge that she defined a magic number as a number which when converted to a given base *b*, all the digits from 0 to *b*<=-<=1 appear even number of times in its representation without any leading zeros. You have to answer *q* queries to unlock the office. Each query has three integers *b**i*, *l**i* and *r**i*, the base and the range for which you have to find the count of magic numbers.
First line of input contains *q* (1<=≀<=*q*<=≀<=105)Β β€” number of queries. Each of the next *q* lines contain three space separated integers *b**i*, *l**i*, *r**i* (2<=≀<=*b**i*<=≀<=10, 1<=≀<=*l**i*<=≀<=*r**i*<=≀<=1018).
You have to output *q* lines, each containing a single integer, the answer to the corresponding query.
[ "2\n2 4 9\n3 1 10\n", "2\n2 1 100\n5 1 100\n" ]
[ "1\n2\n", "21\n4\n" ]
In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get: - 4 = 100<sub class="lower-index">2</sub>, - 5 = 101<sub class="lower-index">2</sub>, - 6 = 110<sub class="lower-index">2</sub>, - 7 = 111<sub class="lower-index">2</sub>, - 8 = 1000<sub class="lower-index">2</sub>, - 9 = 1001<sub class="lower-index">2</sub>. Out of these, only base 2 representation of 9 has even number of 1 and 0. Thus, the answer is 1.
[ { "input": "2\n2 4 9\n3 1 10", "output": "1\n2" }, { "input": "2\n2 1 100\n5 1 100", "output": "21\n4" }, { "input": "10\n4 108 114\n5 30 155\n8 193 197\n9 71 169\n2 163 166\n8 120 144\n8 22 151\n4 21 166\n2 46 127\n8 38 51", "output": "0\n3\n0\n1\n3\n0\n5\n16\n9\n1" }, { "in...
46
307,200
0
53,562
191
Thwarting Demonstrations
[ "binary search", "data structures", "trees" ]
null
null
It is dark times in Berland. Berlyand opposition, funded from a neighboring state, has organized a demonstration in Berland capital Bertown. Through the work of intelligence we know that the demonstrations are planned to last for *k* days. Fortunately, Berland has a special police unit, which can save the country. It has exactly *n* soldiers numbered from 1 to *n*. Berland general, the commander of the detachment, must schedule the detachment's work in these difficult *k* days. In each of these days, the general must send a certain number of police officers to disperse riots. Since the detachment is large and the general is not very smart, he can only select a set of all soldiers numbered from *l* to *r*, inclusive, where *l* and *r* are selected arbitrarily. Now the general has exactly two problems. First, he cannot send the same group twice β€” then soldiers get bored and they rebel. Second, not all soldiers are equally reliable. Every soldier has a reliability of *a**i*. The reliability of the detachment is counted as the sum of reliabilities of soldiers in it. The reliability of a single soldier can be negative, then when you include him in the detachment, he will only spoil things. The general is distinguished by his great greed and shortsightedness, so each day he sends to the dissolution the most reliable group of soldiers possible (that is, of all the groups that have not been sent yet). The Berland Government has decided to know what would be the minimum reliability of the detachment, sent to disperse the demonstrations during these *k* days. The general himself can not cope with such a difficult task. Help him to not embarrass himself in front of his superiors!
The first line contains two integers *n* and *k* β€” the number of soldiers in the detachment and the number of times somebody goes on duty. The second line contains *n* space-separated integers *a**i*, their absolute value doesn't exceed 109 β€” the soldiers' reliabilities. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++, it is preferred to use cin, cout streams of the %I64d specifier.
Print a single number β€” the sought minimum reliability of the groups that go on duty during these *k* days.
[ "3 4\n1 4 2\n", "4 6\n2 -1 2 -1\n", "8 10\n1 -2 3 -4 5 -6 7 -8\n" ]
[ "4\n", "1\n", "2\n" ]
none
[ { "input": "3 4\n1 4 2", "output": "4" }, { "input": "4 6\n2 -1 2 -1", "output": "1" }, { "input": "8 10\n1 -2 3 -4 5 -6 7 -8", "output": "2" }, { "input": "10 13\n11 73 57 -34 61 38 -83 10 -88 -32", "output": "99" }, { "input": "20 31\n19 38 -67 83 -83 79 98 -8 8...
6,000
334,438,400
0
53,652
325
The Red Button
[ "combinatorics", "dfs and similar", "dsu", "graphs", "greedy" ]
null
null
Piegirl found the red button. You have one last chance to change the inevitable end. The circuit under the button consists of *n* nodes, numbered from 0 to *n* - 1. In order to deactivate the button, the *n* nodes must be disarmed in a particular order. Node 0 must be disarmed first. After disarming node *i*, the next node to be disarmed must be either node (2Β·*i*) modulo *n* or node (2Β·*i*)<=+<=1 modulo *n*. The last node to be disarmed must be node 0. Node 0 must be disarmed twice, but all other nodes must be disarmed exactly once. Your task is to find any such order and print it. If there is no such order, print -1.
Input consists of a single integer *n* (2<=≀<=*n*<=≀<=105).
Print an order in which you can to disarm all nodes. If it is impossible, print -1 instead. If there are multiple orders, print any one of them.
[ "2\n", "3\n", "4\n", "16\n" ]
[ "0 1 0\n", "-1", "0 1 3 2 0\n", "0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0\n" ]
none
[ { "input": "2", "output": "0 1 0" }, { "input": "3", "output": "-1" }, { "input": "4", "output": "0 1 3 2 0" }, { "input": "16", "output": "0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0" }, { "input": "5", "output": "-1" }, { "input": "7", "output": "-1"...
92
0
0
53,667
982
Shark
[ "brute force", "data structures", "dsu", "trees" ]
null
null
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $n$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $k$ that if the shark in some day traveled the distance strictly less than $k$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $k$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $k$. The shark never returned to the same location after it has moved from it. Thus, in the sequence of $n$ days we can find consecutive nonempty segments when the shark traveled the distance less than $k$ in each of the days: each such segment corresponds to one location. Max wants to choose such $k$ that the lengths of all such segments are equal. Find such integer $k$, that the number of locations is as large as possible. If there are several such $k$, print the smallest one.
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) β€” the number of days. The second line contains $n$ distinct positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β€” the distance traveled in each of the day.
Print a single integer $k$, such that 1. the shark was in each location the same number of days, 1. the number of locations is maximum possible satisfying the first condition, 1. $k$ is smallest possible satisfying the first and second conditions.
[ "8\n1 2 7 3 4 8 5 6\n", "6\n25 1 2 3 14 36\n" ]
[ "7", "2" ]
In the first example the shark travels inside a location on days $1$ and $2$ (first location), then on $4$-th and $5$-th days (second location), then on $7$-th and $8$-th days (third location). There are three locations in total. In the second example the shark only moves inside a location on the $2$-nd day, so there is only one location.
[ { "input": "8\n1 2 7 3 4 8 5 6", "output": "7" }, { "input": "6\n25 1 2 3 14 36", "output": "2" }, { "input": "20\n1 20 2 19 3 18 4 17 5 16 6 15 7 14 8 13 9 12 10 11", "output": "11" }, { "input": "7\n1 2 5 7 3 4 6", "output": "5" }, { "input": "1\n1000000000", ...
389
22,016,000
3
53,694
518
Ilya and Escalator
[ "combinatorics", "dp", "math", "probabilities" ]
null
null
Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. Let's assume that *n* people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue enters the escalator with probability *p*, or the first person in the queue doesn't move with probability (1<=-<=*p*), paralyzed by his fear of escalators and making the whole queue wait behind him. Formally speaking, the *i*-th person in the queue cannot enter the escalator until people with indices from 1 to *i*<=-<=1 inclusive enter it. In one second only one person can enter the escalator. The escalator is infinite, so if a person enters it, he never leaves it, that is he will be standing on the escalator at any following second. Ilya needs to count the expected value of the number of people standing on the escalator after *t* seconds. Your task is to help him solve this complicated task.
The first line of the input contains three numbers *n*,<=*p*,<=*t* (1<=≀<=*n*,<=*t*<=≀<=2000, 0<=≀<=*p*<=≀<=1). Numbers *n* and *t* are integers, number *p* is real, given with exactly two digits after the decimal point.
Print a single real number β€” the expected number of people who will be standing on the escalator after *t* seconds. The absolute or relative error mustn't exceed 10<=-<=6.
[ "1 0.50 1\n", "1 0.50 4\n", "4 0.20 2\n" ]
[ "0.5\n", "0.9375\n", "0.4\n" ]
none
[ { "input": "1 0.50 1", "output": "0.500000000000000" }, { "input": "1 0.50 4", "output": "0.937500000000000" }, { "input": "4 0.20 2", "output": "0.400000000000000" }, { "input": "2000 0.61 2000", "output": "1219.999999999999545" }, { "input": "100 1.00 200", ...
124
10,649,600
-1
53,711
126
Fibonacci Sums
[ "dp", "math" ]
null
null
Fibonacci numbers have the following form: Let's consider some non-empty set *S*<==<={*s*1,<=*s*2,<=...,<=*s**k*}, consisting of different Fibonacci numbers. Let's find the sum of values of this set's elements: Let's call the set *S* a number *n*'s decomposition into Fibonacci sum. It's easy to see that several numbers have several decompositions into Fibonacci sum. For example, for 13 we have 13,<=5<=+<=8,<=2<=+<=3<=+<=8 β€” three decompositions, and for 16: 3<=+<=13,<=1<=+<=2<=+<=13,<=3<=+<=5<=+<=8,<=1<=+<=2<=+<=5<=+<=8 β€” four decompositions. By the given number *n* determine the number of its possible different decompositions into Fibonacci sum.
The first line contains an integer *t* β€” the number of tests (1<=≀<=*t*<=≀<=105). Each of the following *t* lines contains one test. Each test is an integer *n* (1<=≀<=*n*<=≀<=1018). 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 or the %I64d specificator.
For each input data test print a single number on a single line β€” the answer to the problem.
[ "2\n13\n16\n" ]
[ "3\n4\n" ]
Two decompositions are different if there exists a number that is contained in the first decomposition, but is not contained in the second one. Decompositions that differ only in the order of summands are considered equal.
[ { "input": "2\n13\n16", "output": "3\n4" }, { "input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10", "output": "1\n1\n2\n1\n2\n2\n1\n3\n2\n2" }, { "input": "10\n24\n37\n42\n58\n60\n73\n79\n84\n92\n99", "output": "5\n6\n6\n7\n6\n6\n8\n7\n8\n6" }, { "input": "5\n484775665757\n9686857765...
30
0
0
53,998
137
Palindromes
[ "dp", "strings" ]
null
null
Friday is Polycarpus' favourite day of the week. Not because it is followed by the weekend, but because the lessons on Friday are 2 IT lessons, 2 math lessons and 2 literature lessons. Of course, Polycarpus has prepared to all of them, unlike his buddy Innocentius. Innocentius spent all evening playing his favourite game Fur2 and didn't have enough time to do the literature task. As Innocentius didn't want to get an F, he decided to do the task and read the book called "Storm and Calm" during the IT and Math lessons (he never used to have problems with these subjects). When the IT teacher Mr. Watkins saw this, he decided to give Innocentius another task so that the boy concentrated more on the lesson and less β€” on the staff that has nothing to do with IT. Mr. Watkins said that a palindrome is a string that can be read the same way in either direction, from the left to the right and from the right to the left. A concatenation of strings *a*, *b* is a string *ab* that results from consecutive adding of string *b* to string *a*. Of course, Innocentius knew it all but the task was much harder than he could have imagined. Mr. Watkins asked change in the "Storm and Calm" the minimum number of characters so that the text of the book would also be a concatenation of no more than *k* palindromes. Innocentius can't complete the task and therefore asks you to help him.
The first input line contains a non-empty string *s* which is the text of "Storm and Calm" (without spaces). The length of the string *s* does not exceed 500 characters. String *s* consists of uppercase and lowercase Latin letters. The second line contains a single number *k* (1<=≀<=*k*<=≀<=|*s*|, where |*s*| represents the length of the string *s*).
Print on the first line the minimum number of changes that Innocentius will have to make. Print on the second line the string consisting of no more than *k* palindromes. Each palindrome should be non-empty and consist of uppercase and lowercase Latin letters. Use the character "+" (ASCII-code 43) to separate consecutive palindromes. If there exist several solutions, print any of them. The letters' case does matter, that is an uppercase letter is not considered equivalent to the corresponding lowercase letter.
[ "abacaba\n1\n", "abdcaba\n2\n", "abdcaba\n5\n", "abacababababbcbabcd\n3\n" ]
[ "0\nabacaba\n", "1\nabdcdba\n", "0\na+b+d+c+aba\n", "1\nabacaba+babab+bcbabcb\n" ]
none
[ { "input": "abacaba\n1", "output": "0\nabacaba" }, { "input": "abdcaba\n2", "output": "1\nabdcdba" }, { "input": "abdcaba\n5", "output": "0\na+b+d+c+aba" }, { "input": "abacababababbcbabcd\n3", "output": "1\nabacaba+babab+bcbabcb" }, { "input": "abcdefghijklmnopqr...
122
0
-1
54,062
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\...
108
1,638,400
3
54,091
835
Roads in the Kingdom
[ "dfs and similar", "dp", "graphs", "trees" ]
null
null
In the Kingdom K., there are *n* towns numbered with integers from 1 to *n*. The towns are connected by *n* bi-directional roads numbered with integers from 1 to *n*. The *i*-th road connects the towns *u**i* and *v**i* and its length is *l**i*. There is no more than one road between two towns. Also, there are no roads that connect the towns with itself. Let's call the inconvenience of the roads the maximum of the shortest distances between all pairs of towns. Because of lack of money, it was decided to close down one of the roads so that after its removal it is still possible to reach any town from any other. You have to find the minimum possible inconvenience of the roads after closing down one of the roads.
The first line contains the integer *n* (3<=≀<=*n*<=≀<=2Β·105)Β β€” the number of towns and roads. The next *n* lines contain the roads description. The *i*-th from these lines contains three integers *u**i*, *v**i*, *l**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, 1<=≀<=*l**i*<=≀<=109)Β β€” the numbers of towns connected by the *i*-th road and the length of the *i*-th road. No road connects a town to itself, no two roads connect the same towns. It's guaranteed that it's always possible to close down one of the roads so that all the towns are still reachable from each other.
Print a single integerΒ β€” the minimum possible inconvenience of the roads after the refusal from one of the roads.
[ "3\n1 2 4\n2 3 5\n1 3 1\n", "5\n2 3 7\n3 1 9\n4 1 8\n3 5 4\n4 5 5\n" ]
[ "5\n", "18\n" ]
none
[]
31
0
0
54,280
989
A Trance of Nightfall
[ "dp", "geometry", "matrices", "probabilities" ]
null
null
"Flowing and passing like this, the water isn't gone ultimately; Waxing and waning like that, the moon doesn't shrink or grow eventually." "Everything is transient in a way and perennial in another." Kanno doesn't seem to make much sense out of Mino's isolated words, but maybe it's time that they enjoy the gentle breeze and the night skyΒ β€” the inexhaustible gifts from nature. Gazing into the sky of stars, Kanno indulges in a night's tranquil dreams. There is a set $S$ of $n$ points on a coordinate plane. Kanno starts from a point $P$ that can be chosen on the plane. $P$ is not added to $S$ if it doesn't belong to $S$. Then the following sequence of operations (altogether called a move) is repeated several times, in the given order: 1. Choose a line $l$ such that it passes through at least two elements in $S$ and passes through Kanno's current position. If there are multiple such lines, one is chosen equiprobably. 1. Move to one of the points that belong to $S$ and lie on $l$. The destination is chosen equiprobably among all possible ones, including Kanno's current position (if it does belong to $S$). There are $q$ queries each consisting of two integers $(t_i, m_i)$. For each query, you're to help Kanno maximize the probability of the stopping position being the $t_i$-th element in $S$ after $m_i$ moves with a proper selection of $P$, and output this maximum probability. Note that according to rule 1, $P$ should belong to at least one line that passes through at least two points from $S$.
The first line contains a positive integer $n$ ($2 \leq n \leq 200$)Β β€” the number of points in $S$. The $i$-th of the following $n$ lines contains two space-separated integers $x_i$ and $y_i$ ($-10^4 \leq x_i, y_i \leq 10^4$)Β β€” the coordinates of the $i$-th point in $S$. The input guarantees that for all $1 \leq i \lt j \leq n$, $(x_i, y_i) \neq (x_j, y_j)$ holds. The next line contains a positive integer $q$ ($1 \leq q \leq 200$)Β β€” the number of queries. Each of the following $q$ lines contains two space-separated integers $t$ and $m$ ($1 \leq t_i \leq n$, $1 \leq m_i \leq 10^4$)Β β€” the index of the target point and the number of moves, respectively.
Output $q$ lines each containing a decimal numberΒ β€” the $i$-th among them denotes the maximum probability of staying on the $t_i$-th point after $m_i$ steps, with a proper choice of starting position $P$. Your answer will be considered correct if each number in your output differs from the corresponding one in jury's answer by at most $10^{-6}$. Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is considered correct if $|a - b| \le 10^{-6}$.
[ "5\n0 0\n1 3\n2 2\n3 1\n4 4\n10\n1 1\n2 1\n3 1\n4 1\n5 1\n3 2\n3 3\n3 4\n3 5\n3 6\n" ]
[ "0.50000000000000000000\n0.50000000000000000000\n0.33333333333333331483\n0.50000000000000000000\n0.50000000000000000000\n0.18518518518518517491\n0.15226337448559670862\n0.14494741655235482414\n0.14332164812274550414\n0.14296036624949901017\n" ]
The points in $S$ and possible candidates for line $l$ are depicted in the following figure. For the first query, when $P = (-1, -3)$, $l$ is uniquely determined to be $3x = y$, and thus Kanno will move to $(0, 0)$ with a probability of $\frac 1 2$. For the third query, when $P = (2, 2)$, $l$ is chosen equiprobably between $x + y = 4$ and $x = y$. Kanno will then move to the other four points with a probability of $\frac 1 2 \cdot \frac 1 3 = \frac 1 6$ each, or stay at $(2, 2)$ with a probability of $\frac 1 3$.
[]
46
0
0
54,285
741
Arpa’s overnight party and Mehrdad’s silent entering
[ "constructive algorithms", "dfs and similar", "graphs" ]
null
null
Note that girls in Arpa’s land are really attractive. Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw *n* pairs of friends sitting around a table. *i*-th pair consisted of a boy, sitting on the *a**i*-th chair, and his girlfriend, sitting on the *b**i*-th chair. The chairs were numbered 1 through 2*n* in clockwise direction. There was exactly one person sitting on each chair. There were two types of food: Kooft and Zahre-mar. Now Mehrdad wonders, was there any way to serve food for the guests such that: - Each person had exactly one type of food, - No boy had the same type of food as his girlfriend, - Among any three guests sitting on consecutive chairs, there was two of them who had different type of food. Note that chairs 2*n* and 1 are considered consecutive. Find the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions.
The first line contains an integer *n* (1<=<=≀<=<=*n*<=<=≀<=<=105)Β β€” the number of pairs of guests. The *i*-th of the next *n* lines contains a pair of integers *a**i* and *b**i* (1<=<=≀<=*a**i*,<=*b**i*<=≀<=<=2*n*)Β β€” the number of chair on which the boy in the *i*-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed that there was exactly one person sitting on each chair.
If there is no solution, print -1. Otherwise print *n* lines, the *i*-th of them should contain two integers which represent the type of food for the *i*-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, otherwise print 2. If there are multiple solutions, print any of them.
[ "3\n1 4\n2 5\n3 6\n" ]
[ "1 2\n2 1\n1 2\n" ]
none
[ { "input": "3\n1 4\n2 5\n3 6", "output": "1 2\n2 1\n1 2" }, { "input": "6\n3 2\n5 11\n7 12\n6 9\n8 4\n1 10", "output": "1 2\n1 2\n2 1\n2 1\n1 2\n1 2" }, { "input": "19\n30 27\n6 38\n10 28\n20 5\n14 18\n32 2\n36 29\n12 1\n31 24\n15 4\n35 11\n3 7\n21 17\n25 19\n16 8\n23 22\n37 33\n13 9\n34...
264
22,118,400
-1
54,394
164
Machine Programming
[ "flows", "graphs" ]
null
null
One remarkable day company "X" received *k* machines. And they were not simple machines, they were mechanical programmers! This was the last unsuccessful step before switching to android programmers, but that's another story. The company has now *n* tasks, for each of them we know the start time of its execution *s**i*, the duration of its execution *t**i*, and the company profit from its completion *c**i*. Any machine can perform any task, exactly one at a time. If a machine has started to perform the task, it is busy at all moments of time from *s**i* to *s**i*<=+<=*t**i*<=-<=1, inclusive, and it cannot switch to another task. You are required to select a set of tasks which can be done with these *k* machines, and which will bring the maximum total profit.
The first line contains two integer numbers *n* and *k* (1<=≀<=*n*<=≀<=1000, 1<=≀<=*k*<=≀<=50) β€” the numbers of tasks and machines, correspondingly. The next *n* lines contain space-separated groups of three integers *s**i*,<=*t**i*,<=*c**i* (1<=≀<=*s**i*,<=*t**i*<=≀<=109, 1<=≀<=*c**i*<=≀<=106), *s**i* is the time where they start executing the *i*-th task, *t**i* is the duration of the *i*-th task and *c**i* is the profit of its execution.
Print *n* integers *x*1,<=*x*2,<=...,<=*x**n*. Number *x**i* should equal 1, if task *i* should be completed and otherwise it should equal 0. If there are several optimal solutions, print any of them.
[ "3 1\n2 7 5\n1 3 3\n4 1 3\n", "5 2\n1 5 4\n1 4 5\n1 3 2\n4 1 2\n5 6 1\n" ]
[ "0 1 1\n", "1 1 0 0 1\n" ]
In the first sample the tasks need to be executed at moments of time 2 ... 8, 1 ... 3 and 4 ... 4, correspondingly. The first task overlaps with the second and the third ones, so we can execute either task one (profit 5) or tasks two and three (profit 6).
[]
92
0
0
54,407
493
Vasya and Polynomial
[ "math" ]
null
null
Vasya is studying in the last class of school and soon he will take exams. He decided to study polynomials. Polynomial is a function *P*(*x*)<==<=*a*0<=+<=*a*1*x*1<=+<=...<=+<=*a**n**x**n*. Numbers *a**i* are called coefficients of a polynomial, non-negative integer *n* is called a degree of a polynomial. Vasya has made a bet with his friends that he can solve any problem with polynomials. They suggested him the problem: "Determine how many polynomials *P*(*x*) exist with integer non-negative coefficients so that , and , where and *b* are given positive integers"? Vasya does not like losing bets, but he has no idea how to solve this task, so please help him to solve the problem.
The input contains three integer positive numbers no greater than 1018.
If there is an infinite number of such polynomials, then print "inf" without quotes, otherwise print the reminder of an answer modulo 109<=+<=7.
[ "2 2 2\n", "2 3 3\n" ]
[ "2\n", "1\n" ]
none
[ { "input": "2 2 2", "output": "2" }, { "input": "2 3 3", "output": "1" }, { "input": "1 1 1", "output": "inf" }, { "input": "3 5 10", "output": "0" }, { "input": "2 3 1000000000000000000", "output": "0" }, { "input": "7 8 9", "output": "1" }, {...
93
0
-1
54,521
700
Cool Slogans
[ "string suffix structures", "strings" ]
null
null
Bomboslav set up a branding agency and now helps companies to create new logos and advertising slogans. In term of this problems, slogan of the company should be a non-empty substring of its name. For example, if the company name is "hornsandhoofs", then substrings "sand" and "hor" could be its slogans, while strings "e" and "hornss" can not. Sometimes the company performs rebranding and changes its slogan. Slogan *A* is considered to be cooler than slogan *B* if *B* appears in *A* as a substring at least twice (this occurrences are allowed to overlap). For example, slogan *A*<==<= "abacaba" is cooler than slogan *B*<==<= "ba", slogan *A*<==<= "abcbcbe" is cooler than slogan *B*<==<= "bcb", but slogan *A*<==<= "aaaaaa" is not cooler than slogan *B*<==<= "aba". You are given the company name *w* and your task is to help Bomboslav determine the length of the longest sequence of slogans *s*1,<=*s*2,<=...,<=*s**k*, such that any slogan in the sequence is cooler than the previous one.
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the length of the company name that asks Bomboslav to help. The second line contains the string *w* of length *n*, that consists of lowercase English letters.
Print a single integerΒ β€” the maximum possible length of the sequence of slogans of the company named *w*, such that any slogan in the sequence (except the first one) is cooler than the previous
[ "3\nabc\n", "5\nddddd\n", "11\nabracadabra\n" ]
[ "1\n", "5\n", "3\n" ]
none
[]
61
0
-1
54,546
549
Haar Features
[ "greedy", "implementation" ]
null
null
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept. Let's consider a rectangular image that is represented with a table of size *n*<=Γ—<=*m*. The table elements are integers that specify the brightness of each pixel in the image. A feature also is a rectangular table of size *n*<=Γ—<=*m*. Each cell of a feature is painted black or white. To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of *W*<=-<=*B*, where *W* is the total brightness of the pixels in the image, covered with white feature cells, and *B* is the total brightness of the pixels covered with black feature cells. Some examples of the most popular Haar features are given below. Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles. A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image. You have a variable *value*, whose value is initially zero. In one operation you can count the sum of pixel values ​​at any prefix rectangle, multiply it by any integer and add to variable *value*. You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100) β€” the number of rows and columns in the feature. Next *n* lines contain the description of the feature. Each line consists of *m* characters, the *j*-th character of the *i*-th line equals to "W", if this element of the feature is white and "B" if it is black.
Print a single number β€” the minimum number of operations that you need to make to calculate the value of the feature.
[ "6 8\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW\n", "3 3\nWBW\nBWW\nWWW\n", "3 6\nWWBBWW\nWWBBWW\nWWBBWW\n", "4 4\nBBBB\nBBBB\nBBBB\nBBBW\n" ]
[ "2\n", "4\n", "3\n", "4\n" ]
The first sample corresponds to feature *B*, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations: 1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable *value* (the rectangle is indicated by a red frame); <img class="tex-graphics" src="https://espresso.codeforces.com/59e6d843dfb74d53c1bdfa004d277d661dbfb8fc.png" style="max-width: 100.0%;max-height: 100.0%;"/>1. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient  - 2 and variable *value*. <img class="tex-graphics" src="https://espresso.codeforces.com/91d79402e81fce528454fd33ea193676082cf259.png" style="max-width: 100.0%;max-height: 100.0%;"/> Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 =  - 1, as required.
[ { "input": "6 8\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW", "output": "2" }, { "input": "3 3\nWBW\nBWW\nWWW", "output": "4" }, { "input": "3 6\nWWBBWW\nWWBBWW\nWWBBWW", "output": "3" }, { "input": "4 4\nBBBB\nBBBB\nBBBB\nBBBW", "output": "4" }, { ...
155
1,331,200
3
54,581
982
The Meeting Place Cannot Be Changed
[ "dfs and similar", "graphs" ]
null
null
Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping. Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two intersections. Petr wants to select one intersection such that if the robbers continue to drive the roads indefinitely, they will sooner or later come to that intersection. The initial position of the robbers is unknown. Find such an intersection that fits the requirements.
The first line of the input contains two integers $n$ and $m$ ($2 \leq n \le 10^5$, $2 \leq m \leq 5 \cdot 10^5$)Β β€” the number of intersections and the number of directed roads in Braginsk, respectively. Each of the next $m$ lines contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$)Β β€” the start and finish of the $i$-th directed road. It is guaranteed that the robbers can move along the roads indefinitely.
Print a single integer $k$Β β€” the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print $-1$.
[ "5 6\n1 2\n2 3\n3 1\n3 4\n4 5\n5 3\n", "3 3\n1 2\n2 3\n3 1\n" ]
[ "3", "1" ]
In the first example the robbers can move, for example, along the following routes: $(1-2-3-1)$, $(3-4-5-3)$, $(1-2-3-4-5-3-1)$. We can show that if Petr chooses the $3$-rd intersection, he will eventually meet the robbers independently of their route.
[]
124
102,400
0
54,799
847
Students Initiation
[ "binary search", "flows", "graphs" ]
null
null
Soon the first year students will be initiated into students at the University of Berland. The organizers of the initiation come up with a program for this holiday. In their opinion, it would be good if the first-year students presented small souvenirs to each other. When they voiced this idea to the first-year students, they found out the following: - some pairs of the new students already know each other; - each new student agrees to give souvenirs only to those with whom they are already familiar; - each new student does not want to present too many souvenirs. The organizers have written down all the pairs of first-year friends who are familiar with each other and now want to determine for each new student, whom they should give souvenirs to. In their opinion, in each pair of familiar students exactly one student must present a souvenir to another student. First year students already decided to call the unluckiest the one who will have to present the greatest number of souvenirs. The organizers in return promised that the unluckiest will be unlucky to the minimum possible degree: of course, they will have to present the greatest number of souvenirs compared to the other students, but this number will be as small as possible. Organizers are very busy, and they asked you to determine for each pair of first-year friends who and to whom should present a souvenir.
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=5000, 0<=≀<=*m*<=≀<=*min*(5000,<=*n*Β·(*n*<=-<=1)<=/<=2)) β€” the number of the first year students and the number of pairs of the students that know each other. The students are numbered from 1 to *n*. Each of the following *m* lines contains two integers *x**i*,<=*y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*, *x**i*<=β‰ <=*y**i*)Β β€” the students in each pair. It is guaranteed that each pair is present in the list exactly once. It is also guaranteed that if there is a pair (*x**i*,<=*y**i*) in the list, then there is no pair (*y**i*,<=*x**i*).
Print a single integer into the first line β€” the smallest number of souvenirs that the unluckiest student will have to present. Following should be *m* lines, each containing two integers β€” the students which are familiar with each other. The first number in the pair must be the student that will present the souvenir to the second student in the pair. Pairs can be printed in any order. If there are many solutions, print any of them.
[ "5 4\n2 1\n1 3\n2 3\n2 5\n", "4 3\n1 2\n1 3\n1 4\n", "4 6\n1 2\n4 1\n4 2\n3 2\n4 3\n1 3\n" ]
[ "1\n1 2\n2 3\n3 1\n5 2\n", "1\n1 4\n2 1\n3 1\n", "2\n1 3\n2 1\n2 4\n3 2\n4 1\n4 3\n" ]
none
[]
46
0
0
54,882
899
Segments Removal
[ "data structures", "dsu", "flows", "implementation", "two pointers" ]
null
null
Vasya has an array of integers of length *n*. Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13,<=13,<=7,<=7,<=7,<=2,<=2,<=2], then after one operation it becomes [13,<=13,<=2,<=2,<=2]. Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000) β€” the length of the array. The second line contains a sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” Vasya's array.
Print the number of operations Vasya should make to remove all elements from the array.
[ "4\n2 5 5 2\n", "5\n6 3 4 1 5\n", "8\n4 4 4 2 2 100 100 100\n", "6\n10 10 50 10 50 50\n" ]
[ "2\n", "5\n", "3\n", "4\n" ]
In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty. In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array. In the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second β€” all integers 100, in the third β€” all integers 2. In the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50, 10]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that.
[ { "input": "4\n2 5 5 2", "output": "2" }, { "input": "5\n6 3 4 1 5", "output": "5" }, { "input": "8\n4 4 4 2 2 100 100 100", "output": "3" }, { "input": "6\n10 10 50 10 50 50", "output": "4" }, { "input": "1\n1", "output": "1" }, { "input": "100\n45 45...
30
0
0
55,027
666
World Tour
[ "graphs", "shortest paths" ]
null
null
A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will entirely hold the world tour in his native country β€” Berland. Cicasso is very devoted to his work and he wants to be distracted as little as possible. Therefore he will visit only four cities. These cities will be different, so no one could think that he has "favourites". Of course, to save money, he will chose the shortest paths between these cities. But as you have probably guessed, Cicasso is a weird person. Although he doesn't like to organize exhibitions, he likes to travel around the country and enjoy its scenery. So he wants the total distance which he will travel to be as large as possible. However, the sculptor is bad in planning, so he asks you for help. There are *n* cities and *m* one-way roads in Berland. You have to choose four different cities, which Cicasso will visit and also determine the order in which he will visit them. So that the total distance he will travel, if he visits cities in your order, starting from the first city in your list, and ending in the last, choosing each time the shortest route between a pair of cities β€” will be the largest. Note that intermediate routes may pass through the cities, which are assigned to the tour, as well as pass twice through the same city. For example, the tour can look like that: . Four cities in the order of visiting marked as overlines: [1,<=5,<=2,<=4]. Note that Berland is a high-tech country. So using nanotechnologies all roads were altered so that they have the same length. For the same reason moving using regular cars is not very popular in the country, and it can happen that there are such pairs of cities, one of which generally can not be reached by car from the other one. However, Cicasso is very conservative and cannot travel without the car. Choose cities so that the sculptor can make the tour using only the automobile. It is guaranteed that it is always possible to do.
In the first line there is a pair of integers *n* and *m* (4<=≀<=*n*<=≀<=3000,<=3<=≀<=*m*<=≀<=5000) β€” a number of cities and one-way roads in Berland. Each of the next *m* lines contains a pair of integers *u**i*,<=*v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*) β€” a one-way road from the city *u**i* to the city *v**i*. Note that *u**i* and *v**i* are not required to be distinct. Moreover, it can be several one-way roads between the same pair of cities.
Print four integers β€” numbers of cities which Cicasso will visit according to optimal choice of the route. Numbers of cities should be printed in the order that Cicasso will visit them. If there are multiple solutions, print any of them.
[ "8 9\n1 2\n2 3\n3 4\n4 1\n4 5\n5 6\n6 7\n7 8\n8 5\n" ]
[ "2 1 8 7\n" ]
Let *d*(*x*, *y*) be the shortest distance between cities *x* and *y*. Then in the example *d*(2, 1) = 3, *d*(1, 8) = 7, *d*(8, 7) = 3. The total distance equals 13.
[]
31
0
0
55,074
677
Vanya and Food Processor
[ "implementation", "math" ]
null
null
Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed *h* and the processor smashes *k* centimeters of potato each second. If there are less than *k* centimeters remaining, than during this second processor smashes all the remaining potato. Vanya has *n* pieces of potato, the height of the *i*-th piece is equal to *a**i*. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number *n*. Formally, each second the following happens: 1. If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. 1. Processor smashes *k* centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed.
The first line of the input contains integers *n*, *h* and *k* (1<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*k*<=≀<=*h*<=≀<=109)Β β€” the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=*h*)Β β€” the heights of the pieces.
Print a single integerΒ β€” the number of seconds required to smash all the potatoes following the process described in the problem statement.
[ "5 6 3\n5 4 3 2 1\n", "5 6 3\n5 5 5 5 5\n", "5 6 3\n1 2 1 1 1\n" ]
[ "5\n", "10\n", "2\n" ]
Consider the first sample. 1. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. 1. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. 1. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. 1. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. 1. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2Β·5 = 10 seconds. In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds.
[ { "input": "5 6 3\n5 4 3 2 1", "output": "5" }, { "input": "5 6 3\n5 5 5 5 5", "output": "10" }, { "input": "5 6 3\n1 2 1 1 1", "output": "2" }, { "input": "10 100 80\n76 75 73 71 76 74 73 70 78 75", "output": "10" }, { "input": "10 100 88\n11 23 69 6 71 15 25 1 4...
139
13,619,200
3
55,096
37
Lesson Timetable
[ "combinatorics", "dp", "math" ]
D. Lesson Timetable
1
256
When Petya has free from computer games time, he attends university classes. Every day the lessons on Petya’s faculty consist of two double classes. The floor where the lessons take place is a long corridor with *M* classrooms numbered from 1 to *M*, situated along it. All the students of Petya’s year are divided into *N* groups. Petya has noticed recently that these groups’ timetable has the following peculiarity: the number of the classroom where the first lesson of a group takes place does not exceed the number of the classroom where the second lesson of this group takes place. Once Petya decided to count the number of ways in which one can make a lesson timetable for all these groups. The timetable is a set of 2*N* numbers: for each group the number of the rooms where the first and the second lessons take place. Unfortunately, he quickly lost the track of his calculations and decided to count only the timetables that satisfy the following conditions: 1) On the first lesson in classroom *i* exactly *X**i* groups must be present. 2) In classroom *i* no more than *Y**i* groups may be placed. Help Petya count the number of timetables satisfying all those conditionsю As there can be a lot of such timetables, output modulo 109<=+<=7.
The first line contains one integer *M* (1<=≀<=*M*<=≀<=100) β€” the number of classrooms. The second line contains *M* space-separated integers β€” *X**i* (0<=≀<=*X**i*<=≀<=100) the amount of groups present in classroom *i* during the first lesson. The third line contains *M* space-separated integers β€” *Y**i* (0<=≀<=*Y**i*<=≀<=100) the maximal amount of groups that can be present in classroom *i* at the same time. It is guaranteed that all the *X**i*<=≀<=*Y**i*, and that the sum of all the *X**i* is positive and does not exceed 1000.
In the single line output the answer to the problem modulo 109<=+<=7.
[ "3\n1 1 1\n1 2 3\n", "3\n1 1 1\n1 1 1\n" ]
[ "36\n", "6\n" ]
In the second sample test the first and the second lessons of each group must take place in the same classroom, that’s why the timetables will only be different in the rearrangement of the classrooms’ numbers for each group, e.g. 3! = 6.
[ { "input": "3\n1 1 1\n1 2 3", "output": "36" }, { "input": "3\n1 1 1\n1 1 1", "output": "6" }, { "input": "3\n2 1 1\n5 1 2", "output": "72" }, { "input": "5\n2 1 1 1 1\n5 3 1 1 3", "output": "49320" }, { "input": "5\n1 3 15 3 18\n2 6 18 5 19", "output": "92148...
1,000
31,744,000
0
55,115
183
Headquarters
[ "constructive algorithms", "math" ]
null
null
Sensation, sensation in the two-dimensional kingdom! The police have caught a highly dangerous outlaw, member of the notorious "Pihters" gang. The law department states that the outlaw was driving from the gang's headquarters in his car when he crashed into an ice cream stall. The stall, the car, and the headquarters each occupies exactly one point on the two-dimensional kingdom. The outlaw's car was equipped with a GPS transmitter. The transmitter showed that the car made exactly *n* movements on its way from the headquarters to the stall. A movement can move the car from point (*x*,<=*y*) to one of these four points: to point (*x*<=-<=1,<=*y*) which we will mark by letter "L", to point (*x*<=+<=1,<=*y*) β€” "R", to point (*x*,<=*y*<=-<=1) β€” "D", to point (*x*,<=*y*<=+<=1) β€” "U". The GPS transmitter is very inaccurate and it doesn't preserve the exact sequence of the car's movements. Instead, it keeps records of the car's possible movements. Each record is a string of one of these types: "UL", "UR", "DL", "DR" or "ULDR". Each such string means that the car made a single movement corresponding to one of the characters of the string. For example, string "UL" means that the car moved either "U", or "L". You've received the journal with the outlaw's possible movements from the headquarters to the stall. The journal records are given in a chronological order. Given that the ice-cream stall is located at point (0,<=0), your task is to print the number of different points that can contain the gang headquarters (that is, the number of different possible locations of the car's origin).
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of the car's movements from the headquarters to the stall. Each of the following *n* lines describes the car's possible movements. It is guaranteed that each possible movement is one of the following strings: "UL", "UR", "DL", "DR" or "ULDR". All movements are given in chronological order. Please do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin and cout stream or the %I64d specifier.
Print a single integer β€” the number of different possible locations of the gang's headquarters.
[ "3\nUR\nUL\nULDR\n", "2\nDR\nDL\n" ]
[ "9\n", "4\n" ]
The figure below shows the nine possible positions of the gang headquarters from the first sample: For example, the following movements can get the car from point (1, 0) to point (0, 0):
[ { "input": "3\nUR\nUL\nULDR", "output": "9" }, { "input": "2\nDR\nDL", "output": "4" }, { "input": "4\nUL\nUR\nDR\nDL", "output": "9" }, { "input": "10\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL", "output": "11" }, { "input": "6\nUL\nDL\nDL\nUL\nUL\nDL", "output"...
778
0
3
55,169
1,005
Median on Segments (Permutations Edition)
[ "sortings" ]
null
null
You are given a permutation $p_1, p_2, \dots, p_n$. A permutation of length $n$ is a sequence such that each integer between $1$ and $n$ occurs exactly once in the sequence. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of the median of $p_l, p_{l+1}, \dots, p_r$ is exactly the given number $m$. The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of the median of $p_l, p_{l+1}, \dots, p_r$ is exactly the given number $m$.
The first line contains integers $n$ and $m$ ($1 \le n \le 2\cdot10^5$, $1 \le m \le n$) β€” the length of the given sequence and the required value of the median. The second line contains a permutation $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). Each integer between $1$ and $n$ occurs in $p$ exactly once.
Print the required number.
[ "5 4\n2 4 5 3 1\n", "5 5\n1 2 3 4 5\n", "15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9\n" ]
[ "4\n", "1\n", "48\n" ]
In the first example, the suitable pairs of indices are: $(1, 3)$, $(2, 2)$, $(2, 3)$ and $(2, 4)$.
[ { "input": "5 4\n2 4 5 3 1", "output": "4" }, { "input": "5 5\n1 2 3 4 5", "output": "1" }, { "input": "15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9", "output": "48" }, { "input": "1 1\n1", "output": "1" }, { "input": "2 1\n1 2", "output": "2" }, { "input...
3,000
13,107,200
0
55,226
528
Fuzzy Search
[ "bitmasks", "brute force", "fft" ]
null
null
Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'. Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a string *S*. To analyze the fragment, you need to find all occurrences of string *T* in a string *S*. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem. Let's write down integer *k*<=β‰₯<=0 β€” the error threshold. We will say that string *T* occurs in string *S* on position *i* (1<=≀<=*i*<=≀<=|*S*|<=-<=|*T*|<=+<=1), if after putting string *T* along with this position, each character of string *T* corresponds to the some character of the same value in string *S* at the distance of at most *k*. More formally, for any *j* (1<=≀<=*j*<=≀<=|*T*|) there must exist such *p* (1<=≀<=*p*<=≀<=|*S*|), that |(*i*<=+<=*j*<=-<=1)<=-<=*p*|<=≀<=*k* and *S*[*p*]<==<=*T*[*j*]. For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6. Note that at *k*<==<=0 the given definition transforms to a simple definition of the occurrence of a string in a string. Help Leonid by calculating in how many positions the given string *T* occurs in the given string *S* with the given error threshold.
The first line contains three integers |*S*|,<=|*T*|,<=*k* (1<=≀<=|*T*|<=≀<=|*S*|<=≀<=200<=000, 0<=≀<=*k*<=≀<=200<=000) β€” the lengths of strings *S* and *T* and the error threshold. The second line contains string *S*. The third line contains string *T*. Both strings consist only of uppercase letters 'A', 'T', 'G' and 'C'.
Print a single number β€” the number of occurrences of *T* in *S* with the error threshold *k* by the given definition.
[ "10 4 1\nAGCAATTCAT\nACAT\n" ]
[ "3\n" ]
If you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously.
[ { "input": "10 4 1\nAGCAATTCAT\nACAT", "output": "3" }, { "input": "1 1 0\nA\nA", "output": "1" }, { "input": "1 1 0\nA\nT", "output": "0" }, { "input": "1 1 1\nA\nT", "output": "0" }, { "input": "5 3 0\nATATA\nATA", "output": "2" }, { "input": "5 3 1\...
31
0
0
55,233
767
Change-free
[ "greedy" ]
null
null
Student Arseny likes to plan his life for *n* days ahead. He visits a canteen every day and he has already decided what he will order in each of the following *n* days. Prices in the canteen do not change and that means Arseny will spend *c**i* rubles during the *i*-th day. There are 1-ruble coins and 100-ruble notes in circulation. At this moment, Arseny has *m* coins and a sufficiently large amount of notes (you can assume that he has an infinite amount of them). Arseny loves modern technologies, so he uses his credit card everywhere except the canteen, but he has to pay in cash in the canteen because it does not accept cards. Cashier always asks the student to pay change-free. However, it's not always possible, but Arseny tries to minimize the dissatisfaction of the cashier. Cashier's dissatisfaction for each of the days is determined by the total amount of notes and coins in the change. To be precise, if the cashier gives Arseny *x* notes and coins on the *i*-th day, his dissatisfaction for this day equals *x*Β·*w**i*. Cashier always gives change using as little coins and notes as possible, he always has enough of them to be able to do this. Arseny wants to pay in such a way that the total dissatisfaction of the cashier for *n* days would be as small as possible. Help him to find out how he needs to pay in each of the *n* days! Note that Arseny always has enough money to pay, because he has an infinite amount of notes. Arseny can use notes and coins he received in change during any of the following days.
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=105, 0<=≀<=*m*<=≀<=109)Β β€” the amount of days Arseny planned his actions for and the amount of coins he currently has. The second line contains a sequence of integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≀<=*c**i*<=≀<=105)Β β€” the amounts of money in rubles which Arseny is going to spend for each of the following days. The third line contains a sequence of integers *w*1,<=*w*2,<=...,<=*w**n* (1<=≀<=*w**i*<=≀<=105)Β β€” the cashier's dissatisfaction coefficients for each of the following days.
In the first line print one integerΒ β€” minimum possible total dissatisfaction of the cashier. Then print *n* lines, the *i*-th of then should contain two numbersΒ β€” the amount of notes and the amount of coins which Arseny should use to pay in the canteen on the *i*-th day. Of course, the total amount of money Arseny gives to the casher in any of the days should be no less than the amount of money he has planned to spend. It also shouldn't exceed 106 rubles: Arseny never carries large sums of money with him. If there are multiple answers, print any of them.
[ "5 42\n117 71 150 243 200\n1 1 1 1 1\n", "3 0\n100 50 50\n1 3 2\n", "5 42\n117 71 150 243 200\n5 4 3 2 1\n" ]
[ "79\n1 17\n1 0\n2 0\n2 43\n2 0\n", "150\n1 0\n1 0\n0 50\n", "230\n1 17\n1 0\n1 50\n3 0\n2 0\n" ]
none
[ { "input": "5 42\n117 71 150 243 200\n1 1 1 1 1", "output": "79\n1 17\n1 0\n2 0\n2 43\n2 0" }, { "input": "3 0\n100 50 50\n1 3 2", "output": "150\n1 0\n1 0\n0 50" }, { "input": "5 42\n117 71 150 243 200\n5 4 3 2 1", "output": "230\n1 17\n1 0\n1 50\n3 0\n2 0" }, { "input": "5 ...
30
0
0
55,325
0
none
[ "none" ]
null
null
There are two decks of cards lying on the table in front of you, some cards in these decks lay face up, some of them lay face down. You want to merge them into one deck in which each card is face down. You're going to do it in two stages. The first stage is to merge the two decks in such a way that the relative order of the cards from the same deck doesn't change. That is, for any two different cards *i* and *j* in one deck, if card *i* lies above card *j*, then after the merge card *i* must also be above card *j*. The second stage is performed on the deck that resulted from the first stage. At this stage, the executed operation is the turning operation. In one turn you can take a few of the top cards, turn all of them, and put them back. Thus, each of the taken cards gets turned and the order of these cards is reversed. That is, the card that was on the bottom before the turn, will be on top after it. Your task is to make sure that all the cards are lying face down. Find such an order of merging cards in the first stage and the sequence of turning operations in the second stage, that make all the cards lie face down, and the number of turns is minimum.
The first input line contains a single integer *n* β€” the number of cards in the first deck (1<=≀<=*n*<=≀<=105). The second input line contains *n* integers, separated by single spaces *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=1). Value *a**i* equals 0, if the *i*-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost one to the bottommost one. The third input line contains integer *m* β€” the number of cards in the second deck (1<=≀<=*m*<=≀<=105). The fourth input line contains *m* integers, separated by single spaces *b*1,<=*b*2,<=...,<=*b**m* (0<=≀<=*b**i*<=≀<=1). Value *b**i* equals 0, if the *i*-th card is lying face down, and 1, if the card is lying face up. The cards are given in the order from the topmost to the bottommost.
In the first line print *n*<=+<=*m* space-separated integers β€” the numbers of the cards in the order, in which they will lie after the first stage. List the cards from top to bottom. The cards from the first deck should match their indexes from 1 to *n* in the order from top to bottom. The cards from the second deck should match their indexes, increased by *n*, that is, numbers from *n*<=+<=1 to *n*<=+<=*m* in the order from top to bottom. In the second line print a single integer *x* β€” the minimum number of turn operations you need to make all cards in the deck lie face down. In the third line print *x* integers: *c*1,<=*c*2,<=...,<=*c**x* (1<=≀<=*c**i*<=≀<=*n*<=+<=*m*), each of them represents the number of cards to take from the top of the deck to perform a turn operation. Print the operations in the order, in which they should be performed. If there are multiple optimal solutions, print any of them. It is guaranteed that the minimum number of operations doesn't exceed 6Β·105.
[ "3\n1 0 1\n4\n1 1 1 1\n", "5\n1 1 1 1 1\n5\n0 1 0 1 0\n" ]
[ "1 4 5 6 7 2 3\n3\n5 6 7\n", "6 1 2 3 4 5 7 8 9 10\n4\n1 7 8 9\n" ]
none
[]
92
0
0
55,380
115
Linear Kingdom Races
[ "data structures", "dp" ]
null
null
You are a car race organizer and would like to arrange some races in Linear Kingdom. Linear Kingdom has *n* consecutive roads spanning from left to right. The roads are numbered from 1 to *n* from left to right, thus the roads follow in the order of their numbers' increasing. There will be several races that may be held on these roads. Each race will use a consecutive subset of these roads. Also, each race will pay some amount of money to you if this race is held. No races overlap in time, so some roads can be used in several races. Unfortunately, some of the roads are in a bad condition and they need repair. Each road has repair costs associated with it, you are required to pay this cost to repair the road. A race can only take place if all the roads used in the race are renovated. Your task is to repair such roads (possibly all or none) that will maximize your profit. Your profit is defined as the total money you get from the races that are held minus the total money you spent to repair the roads. Note that you may decide not to repair any road and gain zero profit. Print the maximum profit you can gain.
The first line contains two single-space separated integers, *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=2Β·105), denoting the number of roads and the number of races, respectively. Then *n* lines follow, each line will contain a single non-negative integer not exceeding 109 denoting the cost to repair a road. The costs are given in order from road 1 to road *n*. Finally, *m* lines follow. Each line is single-space-separated triplets of integers. Each triplet will be given as *lb*, *ub*, and *p* (1<=≀<=*lb*<=≀<=*ub*<=≀<=*n*,<=1<=≀<=*p*<=≀<=109), which means that the race these three integers describe will use all the roads from *lb* to *ub*, inclusive, and if it's held you get *p*.
Print a single integer denoting the maximum possible profit you can gain. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is recommended to use cin, cout stream (also you may use %I64d specificator).
[ "7 4\n3\n2\n3\n2\n1\n2\n3\n1 2 5\n2 3 5\n3 5 3\n7 7 5\n", "2 1\n0\n3\n1 2 5\n", "3 1\n10\n10\n10\n1 3 10\n" ]
[ "4\n", "2\n", "0\n" ]
In the first sample the optimal solution is to repair roads 1, 2, 3, and 7. Three races will take place which nets you 15. The road repair costs 11, hence your profit is 4.
[ { "input": "7 4\n3\n2\n3\n2\n1\n2\n3\n1 2 5\n2 3 5\n3 5 3\n7 7 5", "output": "4" }, { "input": "2 1\n0\n3\n1 2 5", "output": "2" }, { "input": "3 1\n10\n10\n10\n1 3 10", "output": "0" }, { "input": "3 2\n10\n10\n12\n1 2 25\n2 3 5", "output": "5" }, { "input": "7 5...
31
0
0
55,508
894
Ralph and Mushrooms
[ "dp", "graphs" ]
null
null
Ralph is going to collect mushrooms in the Mushroom Forest. There are *m* directed paths connecting *n* trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the *i*-th time, there regrow *i* mushrooms less than there was before this pass. That is, if there is initially *x* mushrooms on a path, then Ralph will collect *x* mushrooms for the first time, *x*<=-<=1 mushrooms the second time, *x*<=-<=1<=-<=2 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0. For example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 9, 8, 6 and 3 when Ralph passes by from first to fourth time. From the fifth time and later Ralph can't collect any mushrooms from the path (but still can pass it). Ralph decided to start from the tree *s*. How many mushrooms can he collect using only described paths?
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=106, 0<=≀<=*m*<=≀<=106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively. Each of the following *m* lines contains three integers *x*, *y* and *w* (1<=≀<=*x*,<=*y*<=≀<=*n*, 0<=≀<=*w*<=≀<=108), denoting a path that leads from tree *x* to tree *y* with *w* mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees. The last line contains a single integer *s* (1<=≀<=*s*<=≀<=*n*)Β β€” the starting position of Ralph.
Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.
[ "2 2\n1 2 4\n2 1 4\n1\n", "3 3\n1 2 4\n2 3 3\n1 3 8\n1\n" ]
[ "16", "8" ]
In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect. In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.
[ { "input": "2 2\n1 2 4\n2 1 4\n1", "output": "16" }, { "input": "3 3\n1 2 4\n2 3 3\n1 3 8\n1", "output": "8" }, { "input": "1 0\n1", "output": "0" } ]
732
26,009,600
-1
55,535
492
Vanya and Computer Game
[ "binary search", "implementation", "math", "sortings" ]
null
null
Vanya and his friend Vova play a computer game where they need to destroy *n* monsters to pass a level. Vanya's character performs attack with frequency *x* hits per second and Vova's character performs attack with frequency *y* hits per second. Each character spends fixed time to raise a weapon and then he hits (the time to raise the weapon is 1<=/<=*x* seconds for the first character and 1<=/<=*y* seconds for the second one). The *i*-th monster dies after he receives *a**i* hits. Vanya and Vova wonder who makes the last hit on each monster. If Vanya and Vova make the last hit at the same time, we assume that both of them have made the last hit.
The first line contains three integers *n*,*x*,*y* (1<=≀<=*n*<=≀<=105, 1<=≀<=*x*,<=*y*<=≀<=106) β€” the number of monsters, the frequency of Vanya's and Vova's attack, correspondingly. Next *n* lines contain integers *a**i* (1<=≀<=*a**i*<=≀<=109)Β β€” the number of hits needed do destroy the *i*-th monster.
Print *n* lines. In the *i*-th line print word "Vanya", if the last hit on the *i*-th monster was performed by Vanya, "Vova", if Vova performed the last hit, or "Both", if both boys performed it at the same time.
[ "4 3 2\n1\n2\n3\n4\n", "2 1 1\n1\n2\n" ]
[ "Vanya\nVova\nVanya\nBoth\n", "Both\nBoth\n" ]
In the first sample Vanya makes the first hit at time 1 / 3, Vova makes the second hit at time 1 / 2, Vanya makes the third hit at time 2 / 3, and both boys make the fourth and fifth hit simultaneously at the time 1. In the second sample Vanya and Vova make the first and second hit simultaneously at time 1.
[ { "input": "4 3 2\n1\n2\n3\n4", "output": "Vanya\nVova\nVanya\nBoth" }, { "input": "2 1 1\n1\n2", "output": "Both\nBoth" }, { "input": "7 5 20\n26\n27\n28\n29\n30\n31\n32", "output": "Vova\nVova\nVova\nBoth\nBoth\nVova\nVova" }, { "input": "10 10 1\n1\n2\n3\n4\n5\n6\n7\n8\n9\...
31
0
0
55,628
0
none
[ "none" ]
null
null
It's well-known that blog posts are an important part of Codeforces platform. Every blog post has a global characteristic changing over timeΒ β€” its community rating. A newly created blog post's community rating is 0. Codeforces users may visit the blog post page and rate it, changing its community rating by +1 or -1. Consider the following model of Codeforces users' behavior. The *i*-th user has his own estimated blog post rating denoted by an integer *a**i*. When a user visits a blog post page, he compares his estimated blog post rating to its community rating. If his estimated rating is higher, he rates the blog post with +1 (thus, the blog post's community rating increases by 1). If his estimated rating is lower, he rates the blog post with -1 (decreasing its community rating by 1). If the estimated rating and the community rating are equal, user doesn't rate the blog post at all (in this case we'll say that user rates the blog post for 0). In any case, after this procedure user closes the blog post page and never opens it again. Consider a newly created blog post with the initial community rating of 0. For each of *n* Codeforces users, numbered from 1 to *n*, his estimated blog post rating *a**i* is known. For each *k* from 1 to *n*, inclusive, the following question is asked. Let users with indices from 1 to *k*, in some order, visit the blog post page, rate the blog post and close the page. Each user opens the blog post only after the previous user closes it. What could be the maximum possible community rating of the blog post after these *k* visits?
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=5Β·105)Β β€” the number of Codeforces users. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=5Β·105<=≀<=*a**i*<=≀<=5Β·105)Β β€” estimated blog post ratings for users in order from 1 to *n*.
For each *k* from 1 to *n*, output a single integer equal to the maximum possible community rating of the blog post after users with indices from 1 to *k*, in some order, visit the blog post page, rate the blog post, and close the page.
[ "4\n2 0 2 2\n", "7\n2 -3 -2 5 0 -3 1\n" ]
[ "1\n1\n2\n2\n", "1\n0\n-1\n0\n1\n1\n2\n" ]
none
[]
30
0
0
55,735
489
SwapSort
[ "greedy", "implementation", "sortings" ]
null
null
In this problem your goal is to sort an array consisting of *n* integers in at most *n* swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another. Note that in this problem you do not have to minimize the number of swaps β€” your task is to find any sequence that is no longer than *n*.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=3000) β€” the number of array elements. The second line contains elements of array: *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (<=-<=109<=≀<=*a**i*<=≀<=109), where *a**i* is the *i*-th element of the array. The elements are numerated from 0 to *n*<=-<=1 from left to right. Some integers may appear in the array more than once.
In the first line print *k* (0<=≀<=*k*<=≀<=*n*) β€” the number of swaps. Next *k* lines must contain the descriptions of the *k* swaps, one per line. Each swap should be printed as a pair of integers *i*, *j* (0<=≀<=*i*,<=*j*<=≀<=*n*<=-<=1), representing the swap of elements *a**i* and *a**j*. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print *i*<==<=*j* and swap the same pair of elements multiple times. If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
[ "5\n5 2 5 1 4\n", "6\n10 20 20 40 60 60\n", "2\n101 100\n" ]
[ "2\n0 3\n4 2\n", "0\n", "1\n0 1\n" ]
none
[ { "input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2" }, { "input": "6\n10 20 20 40 60 60", "output": "0" }, { "input": "2\n101 100", "output": "1\n0 1" }, { "input": "1\n1000", "output": "0" }, { "input": "2\n1000000000 -1000000000", "output": "1\n0 1" }, { ...
857
204,800
3
55,816
478
Red-Green Towers
[ "dp" ]
null
null
There are *r* red and *g* green blocks for construction of the red-green tower. Red-green tower can be built following next rules: - Red-green tower is consisting of some number of levels; - Let the red-green tower consist of *n* levels, then the first level of this tower should consist of *n* blocks, second level β€” of *n*<=-<=1 blocks, the third one β€” of *n*<=-<=2 blocks, and so on β€” the last level of such tower should consist of the one block. In other words, each successive level should contain one block less than the previous one; - Each level of the red-green tower should contain blocks of the same color. Let *h* be the maximum possible number of levels of red-green tower, that can be built out of *r* red and *g* green blocks meeting the rules above. The task is to determine how many different red-green towers having *h* levels can be built out of the available blocks. Two red-green towers are considered different if there exists some level, that consists of red blocks in the one tower and consists of green blocks in the other tower. You are to write a program that will find the number of different red-green towers of height *h* moduloΒ 109<=+<=7.
The only line of input contains two integers *r* and *g*, separated by a single space β€” the number of available red and green blocks respectively (0<=≀<=*r*,<=*g*<=≀<=2Β·105, *r*<=+<=*g*<=β‰₯<=1).
Output the only integer β€” the number of different possible red-green towers of height *h* moduloΒ 109<=+<=7.
[ "4 6\n", "9 7\n", "1 1\n" ]
[ "2\n", "6\n", "2\n" ]
The image in the problem statement shows all possible red-green towers for the first sample.
[ { "input": "4 6", "output": "2" }, { "input": "9 7", "output": "6" }, { "input": "1 1", "output": "2" }, { "input": "3 3", "output": "2" }, { "input": "2 19", "output": "1" }, { "input": "18 3", "output": "2" }, { "input": "100000 1", "...
46
0
0
55,833
0
none
[ "none" ]
null
null
There are *n* animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number *i* in the queue will have to visit his office exactly *a**i* times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving *k* animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
The first line of input data contains two space-separated integers *n* and *k* (1<=≀<=*n*<=≀<=105, 0<=≀<=*k*<=≀<=1014). In the second line are given space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
If the doctor will overall carry out less than *k* examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers β€” number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
[ "3 3\n1 2 1\n", "4 10\n3 3 2 1\n", "7 10\n1 3 3 1 2 3 1\n" ]
[ "2 ", "-1\n", "6 2 3 " ]
In the first sample test: - Before examination: {1, 2, 3} - After the first examination: {2, 3} - After the second examination: {3, 2} - After the third examination: {2} In the second sample test: - Before examination: {1, 2, 3, 4, 5, 6, 7} - After the first examination: {2, 3, 4, 5, 6, 7} - After the second examination: {3, 4, 5, 6, 7, 2} - After the third examination: {4, 5, 6, 7, 2, 3} - After the fourth examination: {5, 6, 7, 2, 3} - After the fifth examination: {6, 7, 2, 3, 5} - After the sixth examination: {7, 2, 3, 5, 6} - After the seventh examination: {2, 3, 5, 6} - After the eighth examination: {3, 5, 6, 2} - After the ninth examination: {5, 6, 2, 3} - After the tenth examination: {6, 2, 3}
[ { "input": "3 3\n1 2 1", "output": "2 " }, { "input": "4 10\n3 3 2 1", "output": "-1" }, { "input": "7 10\n1 3 3 1 2 3 1", "output": "6 2 3 " }, { "input": "1 0\n1", "output": "1 " }, { "input": "6 101\n9 78 54 62 2 91", "output": "4 6 2 3 " }, { "inpu...
60
0
0
55,898
474
Captain Marmot
[ "brute force", "geometry" ]
null
null
Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has *n* regiments, each consisting of 4 moles. Initially, each mole *i* (1<=≀<=*i*<=≀<=4*n*) is placed at some position (*x**i*,<=*y**i*) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole *i* has a home placed at the position (*a**i*,<=*b**i*). Moving this mole one time means rotating his position point (*x**i*,<=*y**i*) 90 degrees counter-clockwise around it's home point (*a**i*,<=*b**i*). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible.
The first line contains one integer *n* (1<=≀<=*n*<=≀<=100), the number of regiments. The next 4*n* lines contain 4 integers *x**i*, *y**i*, *a**i*, *b**i* (<=-<=104<=≀<=*x**i*,<=*y**i*,<=*a**i*,<=*b**i*<=≀<=104).
Print *n* lines to the standard output. If the regiment *i* can be made compact, the *i*-th line should contain one integer, the minimal number of required moves. Otherwise, on the *i*-th line print "-1" (without quotes).
[ "4\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-2 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n-1 1 0 0\n2 2 0 1\n-1 0 0 -2\n3 0 0 -2\n-1 1 -2 0\n" ]
[ "1\n-1\n3\n3\n" ]
In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
[ { "input": "4\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-2 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n-1 1 0 0\n2 2 0 1\n-1 0 0 -2\n3 0 0 -2\n-1 1 -2 0", "output": "1\n-1\n3\n3" }, { "input": "3\n-2248 6528 -2144 6181\n-2245 6663 -2100 7054\n-4378 7068 -4061 7516\n-4274 6026 -391...
187
4,198,400
3
56,162
212
Privatization
[ "flows", "graphs" ]
null
null
There is a developed network of flights between Berland and Beerland. All of them belong to the Berland state company BerAvia. Each flight connects some Berland city with some Beerland city. For each flight airplanes fly in both directions. Changes are coming to Berland β€” the state decided to privatize BerAvia, namely, to sell out all flights to *t* private companies. Each of these companies wants to get the maximal number of flights, so if the Berland flights are sold unevenly, Berland can be accused of partiality. Berland Government decided to sell the flights as evenly as possible between the *t* companies. The unevenness of the distribution of flights between companies is calculated as follows. For each city *i* (both Berland and Beerland) we'll calculate the value of Help the Berland government come up with the most even distribution plan of selling flights.
The first input line contains four integers *n*,<=*m*,<=*k* and *t* (1<=≀<=*n*,<=*m*,<=*t*<=≀<=200;1<=≀<=*k*<=≀<=5000), where *n*,<=*m* are the numbers of cities in Berland and Beerland, correspondingly, *k* is the number of flights between them, and *t* is the number of private companies. Next *k* lines describe the flights, one per line, as pairs of positive integers *x**i*,<=*y**i* (1<=≀<=*x**i*<=≀<=*n*;1<=≀<=*y**i*<=≀<=*m*), where *x**i* and *y**i* are the indexes of cities in Berland and Beerland, correspondingly, connected by the *i*-th flight. There is at most one flight between any pair of cities, each flight connects cities of different countries. The cities in Berland are indexed from 1 to *n*, and in Beerland β€” from 1 to *m*.
Print the unevenness of the sought plan on the first line. On the second line print a sequence of *k* integers *c*1,<=*c*2,<=...,<=*c**k* (1<=≀<=*c**i*<=≀<=*t*), where *c**i* is the index of the company that should buy the *i*-th flight. Assume that the flights are indexed from 1 to *k* in the order they appear in the input. If there are multiple solutions, print any of them.
[ "3 5 8 2\n1 4\n1 3\n3 3\n1 2\n1 1\n2 1\n1 5\n2 2\n" ]
[ "4\n2 1 2 1 2 1 2 2 " ]
none
[]
92
0
0
56,170
605
Freelancer's Dreams
[ "geometry" ]
null
null
Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least *p* experience points, and a desired flat in Moscow costs *q* dollars. Mikhail is determined to follow his dreams and registered at a freelance site. He has suggestions to work on *n* distinct projects. Mikhail has already evaluated that the participation in the *i*-th project will increase his experience by *a**i* per day and bring *b**i* dollars per day. As freelance work implies flexible working hours, Mikhail is free to stop working on one project at any time and start working on another project. Doing so, he receives the respective share of experience and money. Mikhail is only trying to become a cool programmer, so he is able to work only on one project at any moment of time. Find the real value, equal to the minimum number of days Mikhail needs to make his dream come true. For example, suppose Mikhail is suggested to work on three projects and *a*1<==<=6, *b*1<==<=2, *a*2<==<=1, *b*2<==<=3, *a*3<==<=2, *b*3<==<=6. Also, *p*<==<=20 and *q*<==<=20. In order to achieve his aims Mikhail has to work for 2.5 days on both first and third projects. Indeed, *a*1Β·2.5<=+<=*a*2Β·0<=+<=*a*3Β·2.5<==<=6Β·2.5<=+<=1Β·0<=+<=2Β·2.5<==<=20 and *b*1Β·2.5<=+<=*b*2Β·0<=+<=*b*3Β·2.5<==<=2Β·2.5<=+<=3Β·0<=+<=6Β·2.5<==<=20.
The first line of the input contains three integers *n*, *p* and *q* (1<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*p*,<=*q*<=≀<=1<=000<=000)Β β€” the number of projects and the required number of experience and money. Each of the next *n* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=1<=000<=000)Β β€” the daily increase in experience and daily income for working on the *i*-th project.
Print a real valueΒ β€” the minimum number of days Mikhail needs to get the required amount of experience and money. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "3 20 20\n6 2\n1 3\n2 6\n", "4 1 1\n2 3\n3 2\n2 3\n3 2\n" ]
[ "5.000000000000000\n", "0.400000000000000\n" ]
First sample corresponds to the example in the problem statement.
[ { "input": "3 20 20\n6 2\n1 3\n2 6", "output": "5.000000000000000" }, { "input": "4 1 1\n2 3\n3 2\n2 3\n3 2", "output": "0.400000000000000" }, { "input": "3 12 12\n5 1\n2 2\n1 5", "output": "4.000000000000000" }, { "input": "3 12 12\n5 1\n4 4\n1 5", "output": "3.000000000...
0
0
-1
56,264
818
Four Melodies
[ "flows", "graphs" ]
null
null
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult! Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks. This time Alice wants to form four melodies for her tracks. Alice has a sheet with *n* notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal. Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7. You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
The first line contains one integer number *n* (4<=≀<=*n*<=≀<=3000). The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=105) β€” notes written on a sheet.
Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
[ "5\n1 3 5 7 9\n", "5\n1 3 5 7 2\n" ]
[ "4\n", "5\n" ]
In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note). In the second example it is possible to compose one melody with 2 notes β€” {1, 2}. Remaining notes are used in other three melodies (one note per each melody).
[ { "input": "5\n1 3 5 7 9", "output": "4" }, { "input": "5\n1 3 5 7 2", "output": "5" }, { "input": "4\n1 3 5 7", "output": "4" }, { "input": "4\n1 1 1 1", "output": "4" }, { "input": "4\n1 1 2 1", "output": "4" }, { "input": "4\n3 2 3 1", "output":...
31
409,600
-1
56,328
101
Vectors
[ "implementation", "math" ]
C. Vectors
1
256
At a geometry lesson Gerald was given a task: to get vector *B* out of vector *A*. Besides, the teacher permitted him to perform the following operations with vector *А*: - Turn the vector by 90 degrees clockwise.- Add to the vector a certain vector *C*. Operations could be performed in any order any number of times. Can Gerald cope with the task?
The first line contains integers *x*1 ΠΈ *y*1 β€” the coordinates of the vector *A* (<=-<=108<=≀<=*x*1,<=*y*1<=≀<=108). The second and the third line contain in the similar manner vectors *B* and *C* (their coordinates are integers; their absolute value does not exceed 108).
Print "YES" (without the quotes) if it is possible to get vector *B* using the given operations. Otherwise print "NO" (without the quotes).
[ "0 0\n1 1\n0 1\n", "0 0\n1 1\n1 1\n", "0 0\n1 1\n2 2\n" ]
[ "YES\n", "YES\n", "NO\n" ]
none
[ { "input": "0 0\n1 1\n0 1", "output": "YES" }, { "input": "0 0\n1 1\n1 1", "output": "YES" }, { "input": "0 0\n1 1\n2 2", "output": "NO" }, { "input": "2 3\n2 3\n0 0", "output": "YES" }, { "input": "-4 -2\n0 0\n-2 -1", "output": "YES" }, { "input": "-1...
62
0
0
56,442
360
Levko and Strings
[ "combinatorics", "dp" ]
null
null
Levko loves strings of length *n*, consisting of lowercase English letters, very much. He has one such string *s*. For each string *t* of length *n*, Levko defines its beauty relative to *s* as the number of pairs of indexes *i*, *j* (1<=≀<=*i*<=≀<=*j*<=≀<=*n*), such that substring *t*[*i*..*j*] is lexicographically larger than substring *s*[*i*..*j*]. The boy wondered how many strings *t* are there, such that their beauty relative to *s* equals exactly *k*. Help him, find the remainder after division this number by 1000000007 (109<=+<=7). A substring *s*[*i*..*j*] of string *s*<==<=*s*1*s*2... *s**n* is string *s**i**s**i*<=<=+<=<=1... *s**j*. String *x*<=<==<=<=*x*1*x*2... *x**p* is lexicographically larger than string *y*<=<==<=<=*y*1*y*2... *y**p*, if there is such number *r* (*r*<=&lt;<=*p*), that *x*1<=<==<=<=*y*1,<=<=*x*2<=<==<=<=*y*2,<=<=... ,<=<=*x**r*<=<==<=<=*y**r* and *x**r*<=<=+<=<=1<=&gt;<=*y**r*<=<=+<=<=1. The string characters are compared by their ASCII codes.
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=2000, 0<=≀<=*k*<=≀<=2000). The second line contains a non-empty string *s* of length *n*. String *s* consists only of lowercase English letters.
Print a single number β€” the answer to the problem modulo 1000000007 (109<=+<=7).
[ "2 2\nyz\n", "2 3\nyx\n", "4 7\nabcd\n" ]
[ "26\n", "2\n", "21962\n" ]
none
[]
31
0
0
56,521
895
Eyes Closed
[ "data structures", "probabilities" ]
null
null
Vasya and Petya were tired of studying so they decided to play a game. Before the game begins Vasya looks at array *a* consisting of *n* integers. As soon as he remembers all elements of *a* the game begins. Vasya closes his eyes and Petya does *q* actions of one of two types: 1) Petya says 4 integers *l*1,<=*r*1,<=*l*2,<=*r*2Β β€” boundaries of two non-intersecting segments. After that he swaps one random element from the [*l*1,<=*r*1] segment with another random element from the [*l*2,<=*r*2] segment. 2) Petya asks Vasya the sum of the elements of *a* in the [*l*,<=*r*] segment. Vasya is a mathematician so he answers Petya the mathematical expectation of the sum of the elements in the segment. Your task is to write a program which will answer the second type questions as Vasya would do it. In other words your program should print the mathematical expectation of the sum of the elements of *a* in the [*l*,<=*r*] segment for every second type query.
The first line contains two integers *n*,<=*q* (2<=≀<=*n*<=≀<=105,<=1<=≀<=*q*<=≀<=105) Β β€” the number of elements in the array and the number of queries you need to handle. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=109) Β β€” elements of the array. The next *q* lines contain Petya's actions of type 1 or 2. If it is a type 1 action then the line contains 5 integers 1,<=*l*1,<=*r*1,<=*l*2,<=*r*2 (1<=≀<=*l*1<=≀<=*r*1<=≀<=*n*,<=1<=≀<=*l*2<=≀<=*r*2<=≀<=*n*). If it is a type 2 query then the line contains 3 integers 2,<=*l*,<=*r* (1<=≀<=*l*<=≀<=*r*<=≀<=*n*). It is guaranteed that there is at least one type 2 query and segments [*l*1,<=*r*1],<=[*l*2,<=*r*2] don't have common elements.
For each type 2 query print one real numberΒ β€” the mathematical expectation of the sum of elements in the segment. Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=4 Β β€” formally, the answer is correct if where *x* is jury's answer and *y* is yours.
[ "4 4\n1 1 2 2\n1 2 2 3 3\n2 1 2\n1 1 2 3 4\n2 1 2\n", "10 5\n1 1 1 1 1 2 2 2 2 2\n1 1 5 6 10\n2 1 5\n1 1 5 6 10\n1 1 5 6 10\n2 6 10\n", "10 10\n1 2 3 4 5 6 7 8 9 10\n1 1 5 6 10\n1 1 5 6 10\n2 1 5\n1 1 3 6 9\n2 1 3\n1 5 7 8 10\n1 1 1 10 10\n2 1 5\n2 7 10\n2 1 10\n" ]
[ "3.0000000\n3.0000000\n", "6.0000000\n8.0400000\n", "23.0000000\n14.0000000\n28.0133333\n21.5733333\n55.0000000\n" ]
none
[]
2,500
49,561,600
0
56,548
519
A and B and Interesting Substrings
[ "data structures", "dp", "two pointers" ]
null
null
A and B are preparing themselves for programming contests. After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes. A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes). B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one). Also, A and B have a string *s*. Now they are trying to find out how many substrings *t* of a string *s* are interesting to B (that is, *t* starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero. Naturally, A and B have quickly found the number of substrings *t* that are interesting to them. Can you do it?
The first line contains 26 integers *x**a*,<=*x**b*,<=...,<=*x**z* (<=-<=105<=≀<=*x**i*<=≀<=105) β€” the value assigned to letters *a*,<=*b*,<=*c*,<=...,<=*z* respectively. The second line contains string *s* of length between 1 and 105 characters, consisting of Lating lowercase lettersβ€” the string for which you need to calculate the answer.
Print the answer to the problem.
[ "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab\n", "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa\n" ]
[ "2\n", "2\n" ]
In the first sample test strings satisfying the condition above are *abca* and *bcab*. In the second sample test strings satisfying the condition above are two occurences of *aa*.
[ { "input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab", "output": "2" }, { "input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa", "output": "2" }, { "input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naabbccdd", "output": "4" }, { ...
62
0
0
56,797
455
Serega and Fun
[ "data structures" ]
null
null
Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem. You are given an array *a* consisting of *n* positive integers and queries to it. The queries can be of two types: 1. Make a unit cyclic shift to the right on the segment from *l* to *r* (both borders inclusive). That is rearrange elements of the array in the following manner:*a*[*l*],<=*a*[*l*<=+<=1],<=...,<=*a*[*r*<=-<=1],<=*a*[*r*]<=β†’<=*a*[*r*],<=*a*[*l*],<=*a*[*l*<=+<=1],<=...,<=*a*[*r*<=-<=1].1. Count how many numbers equal to *k* are on the segment from *l* to *r* (both borders inclusive). Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let's see, can you solve it?
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of elements of the array. The second line contains *n* integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (1<=≀<=*a*[*i*]<=≀<=*n*). The third line contains a single integer *q* (1<=≀<=*q*<=≀<=105) β€” the number of queries. The next *q* lines contain the queries. As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: 1 *l*'*i* *r*'*i*. A query of the second type will be given in format: 2 *l*'*i* *r*'*i* *k*'*i*. All the number in input are integer. They satisfy the constraints: 1<=≀<=*l*'*i*,<=*r*'*i*,<=*k*'*i*<=≀<=*n*. To decode the queries from the data given in input, you need to perform the following transformations: Where *lastans* is the last reply to the query of the 2-nd type (initially, *lastans*<==<=0). If after transformation *l**i* is greater than *r**i*, you must swap these values.
For each query of the 2-nd type print the answer on a single line.
[ "7\n6 6 2 7 4 2 5\n7\n1 3 6\n2 2 4 2\n2 2 4 7\n2 2 2 5\n1 2 6\n1 1 4\n2 1 7 3\n", "8\n8 4 2 2 7 7 8 8\n8\n1 8 8\n2 8 1 7\n1 8 1\n1 7 3\n2 8 8 3\n1 1 4\n1 2 7\n1 4 5\n" ]
[ "2\n1\n0\n0\n", "2\n0\n" ]
none
[ { "input": "7\n6 6 2 7 4 2 5\n7\n1 3 6\n2 2 4 2\n2 2 4 7\n2 2 2 5\n1 2 6\n1 1 4\n2 1 7 3", "output": "2\n1\n0\n0" }, { "input": "8\n8 4 2 2 7 7 8 8\n8\n1 8 8\n2 8 1 7\n1 8 1\n1 7 3\n2 8 8 3\n1 1 4\n1 2 7\n1 4 5", "output": "2\n0" }, { "input": "10\n7 2 3 4 3 2 4 4 9 1\n10\n1 4 5\n1 1 6\n...
77
5,836,800
-1
56,898
448
Divisors
[ "brute force", "dfs and similar", "implementation", "number theory" ]
null
null
Bizon the Champion isn't just friendly, he also is a rigorous coder. Let's define function *f*(*a*), where *a* is a sequence of integers. Function *f*(*a*) returns the following sequence: first all divisors of *a*1 go in the increasing order, then all divisors of *a*2 go in the increasing order, and so on till the last element of sequence *a*. For example, *f*([2,<=9,<=1])<==<=[1,<=2,<=1,<=3,<=9,<=1]. Let's determine the sequence *X**i*, for integer *i* (*i*<=β‰₯<=0): *X*0<==<=[*X*] ([*X*] is a sequence consisting of a single number *X*), *X**i*<==<=*f*(*X**i*<=-<=1) (*i*<=&gt;<=0). For example, at *X*<==<=6 we get *X*0<==<=[6], *X*1<==<=[1,<=2,<=3,<=6], *X*2<==<=[1,<=1,<=2,<=1,<=3,<=1,<=2,<=3,<=6]. Given the numbers *X* and *k*, find the sequence *X**k*. As the answer can be rather large, find only the first 105 elements of this sequence.
A single line contains two space-separated integers β€” *X* (1<=≀<=*X*<=≀<=1012) and *k* (0<=≀<=*k*<=≀<=1018).
Print the elements of the sequence *X**k* in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements.
[ "6 1\n", "4 2\n", "10 3\n" ]
[ "1 2 3 6 \n", "1 1 2 1 2 4 \n", "1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10 \n" ]
none
[ { "input": "6 1", "output": "1 2 3 6 " }, { "input": "4 2", "output": "1 1 2 1 2 4 " }, { "input": "10 3", "output": "1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10 " }, { "input": "10 0", "output": "10 " }, { "input": "1 1", "output": "1 " }, { "input": "1 10000000...
155
7,168,000
-1
56,998
89
Space mines
[ "geometry" ]
D. Space mines
2
256
Once upon a time in the galaxy of far, far away... Darth Wader found out the location of a rebels' base. Now he is going to destroy the base (and the whole planet that the base is located at), using the Death Star. When the rebels learnt that the Death Star was coming, they decided to use their new secret weapon β€” space mines. Let's describe a space mine's build. Each space mine is shaped like a ball (we'll call it the mine body) of a certain radius *r* with the center in the point *O*. Several spikes protrude from the center. Each spike can be represented as a segment, connecting the center of the mine with some point *P*, such that (transporting long-spiked mines is problematic), where |*OP*| is the length of the segment connecting *O* and *P*. It is convenient to describe the point *P* by a vector *p* such that *P*<==<=*O*<=+<=*p*. The Death Star is shaped like a ball with the radius of *R* (*R* exceeds any mine's radius). It moves at a constant speed along the *v* vector at the speed equal to |*v*|. At the moment the rebels noticed the Star of Death, it was located in the point *A*. The rebels located *n* space mines along the Death Star's way. You may regard the mines as being idle. The Death Star does not know about the mines' existence and cannot notice them, which is why it doesn't change the direction of its movement. As soon as the Star of Death touched the mine (its body or one of the spikes), the mine bursts and destroys the Star of Death. A touching is the situation when there is a point in space which belongs both to the mine and to the Death Star. It is considered that Death Star will not be destroyed if it can move infinitely long time without touching the mines. Help the rebels determine whether they will succeed in destroying the Death Star using space mines or not. If they will succeed, determine the moment of time when it will happen (starting from the moment the Death Star was noticed).
The first input data line contains 7 integers *A**x*,<=*A**y*,<=*A**z*,<=*v**x*,<=*v**y*,<=*v**z*,<=*R*. They are the Death Star's initial position, the direction of its movement, and its radius (<=-<=10<=≀<=*v**x*,<=*v**y*,<=*v**z*<=≀<=10, |*v*|<=&gt;<=0, 0<=&lt;<=*R*<=≀<=100). The second line contains an integer *n*, which is the number of mines (1<=≀<=*n*<=≀<=100). Then follow *n* data blocks, the *i*-th of them describes the *i*-th mine. The first line of each block contains 5 integers *O**ix*,<=*O**iy*,<=*O**iz*,<=*r**i*,<=*m**i*, which are the coordinates of the mine centre, the radius of its body and the number of spikes (0<=&lt;<=*r**i*<=&lt;<=100,<=0<=≀<=*m**i*<=≀<=10). Then follow *m**i* lines, describing the spikes of the *i*-th mine, where the *j*-th of them describes the *i*-th spike and contains 3 integers *p**ijx*,<=*p**ijy*,<=*p**ijz* β€” the coordinates of the vector where the given spike is directed (). The coordinates of the mines' centers and the center of the Death Star are integers, their absolute value does not exceed 10000. It is guaranteed that *R*<=&gt;<=*r**i* for any 1<=≀<=*i*<=≀<=*n*. For any mines *i*<=β‰ <=*j* the following inequality if fulfilled: . Initially the Death Star and the mines do not have common points.
If the rebels will succeed in stopping the Death Star using space mines, print the time from the moment the Death Star was noticed to the blast. If the Death Star will not touch a mine, print "-1" (without quotes). For the answer the absolute or relative error of 10<=-<=6 is acceptable.
[ "0 0 0 1 0 0 5\n2\n10 8 0 2 2\n0 -3 0\n2 2 0\n20 0 0 4 3\n2 4 0\n-4 3 0\n1 -5 0\n", "8 8 4 4 4 2 6\n1\n-2 -2 -1 3 0\n", "30 30 2 1 2 1 20\n3\n0 0 40 5 1\n1 4 4\n-10 -40 -5 7 0\n100 200 95 8 1\n-10 0 0\n" ]
[ "10.0000000000", "-1", "74.6757620881" ]
none
[ { "input": "0 0 0 1 0 0 5\n2\n10 8 0 2 2\n0 -3 0\n2 2 0\n20 0 0 4 3\n2 4 0\n-4 3 0\n1 -5 0", "output": "10.0000000000" }, { "input": "8 8 4 4 4 2 6\n1\n-2 -2 -1 3 0", "output": "-1" }, { "input": "30 30 2 1 2 1 20\n3\n0 0 40 5 1\n1 4 4\n-10 -40 -5 7 0\n100 200 95 8 1\n-10 0 0", "outp...
92
0
0
57,049
317
Princess and Her Shadow
[ "constructive algorithms", "shortest paths" ]
null
null
Princess Vlada enjoys springing in the meadows and walking in the forest. One day β€” wonderful, sunny day β€” during her walk Princess found out with astonishment that her shadow was missing! "Blimey!", β€” she thought and started her search of the shadow in the forest. Normally the Shadow is too lazy and simply sleeps under the Princess. But at this terrifically hot summer day she got bored of such a dull life, so she decided to play with Vlada. The forest, where our characters entertain themselves, may be represented as a set of integer cells in the plane, where the Shadow and the Princess can move only up, down, left and right by 1. Some cells (as it happens in decent forests) are occupied by trees. The Shadow and the Princess are not allowed to enter a cell occupied by a tree. Unfortunately, these are the hard times for the forest, so there are very few trees growing here... At first the Princess was walking within the cell (*v**x*,Β *v**y*), while the Shadow hid from the Princess in the cell (*s**x*,Β *s**y*). The Princess, The Shadow and the trees are located in the different cells. The Shadow is playing with the Princess. As soon as the Princess moves by 1 in some direction, the Shadow simultaneously flies by 1 in the same direction, if it is possible (if the cell to fly to is not occupied by some tree); otherwise, the Shadow doesn't move. The Shadow is very shadowy, so our characters do not interfere with each other. We say that the Shadow is caught by the Princess if after some move both of them are located in the same cell. Vlada managed to catch her Shadow! Can you?
First line of the input contains the coordinates of the characters *v**x*, *v**y*, *s**x*, *s**y* and the number of trees *m* (0<=≀<=*m*<=≀<=400). The following *m* lines contain the coordinates of the trees. All the coordinates are integers between -100 and 100, inclusive. The Princess, The Shadow and the trees are located in the different cells.
If it is impossible for the Princess to catch the Shadow, print "-1" (without quotes). Otherwise print a sequence of characters "L", "R", "D", "U", corresponding to the Princess's moves, following which she will be able to catch the Shadow at some turn (L β€” move to the left, R β€” to the right, U β€” up, D β€” down; axis *x* is directed to the right, *y* β€” up). The number of characters (that is, the number of moves) must not exceed 106. All the Princess's moves should be correct, that is must not lead to the cell where a tree grows. It is allowed for the Princess and the Shadow to occupy the same cell before the last turn.
[ "0 0 1 0 1\n0 1\n", "5 0 3 0 8\n2 -1\n2 0\n2 1\n3 -1\n4 1\n4 0\n3 1\n4 -1\n", "3 2 1 1 3\n0 1\n1 0\n0 0\n" ]
[ "LLUR\n", "-1\n", "DLL" ]
Below the pictures for the samples are given (Princess, Shadow and the trees are colored in pink, gray and black correspondingly; the blue dot marks the lattice center). In the first case the Princess may make two left steps, one step upwards and one right step: <img class="tex-graphics" src="https://espresso.codeforces.com/34c3019570fb4cf80b391e93dbddf22fef48f245.png" style="max-width: 100.0%;max-height: 100.0%;"/> In the following case the Princess cannot catch the Shadow: <img class="tex-graphics" src="https://espresso.codeforces.com/063241343ce7958dbc94911c730502450d2396d0.png" style="max-width: 100.0%;max-height: 100.0%;"/> In the last sample the Princess may make two left steps and one down step (in any order): <img class="tex-graphics" src="https://espresso.codeforces.com/9772707754e3581e7b3ff8557e1617678d4c421b.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[]
92
0
0
57,092
730
Running Over The Bridges
[ "greedy", "implementation", "math" ]
null
null
Polycarp is playing a game called "Running Over The Bridges". In this game he has to run over *n* bridges from the left to the right. Bridges are arranged one after the other, so the *i*-th bridge begins where the (*i*<=-<=1)-th bridge ends. You have the following data about bridges: *l**i* and *t**i* β€” the length of the *i*-th bridge and the maximum allowed time which Polycarp can spend running over the *i*-th bridge. Thus, if Polycarp is in the beginning of the bridge *i* at the time *T* then he has to leave it at the time *T*<=+<=*t**i* or earlier. It is allowed to reach the right end of a bridge exactly at the time *T*<=+<=*t**i*. Polycarp can run from the left side to the right one with speed 0.5, so he will run over a bridge with length *s* in time 2Β·*s*. Besides, he has several magical drinks. If he uses one drink, his speed increases twice (i.e. to value 1) for *r* seconds. All magical drinks are identical. Please note that Polycarp can use a drink only at integer moments of time, and he drinks it instantly and completely. Additionally, if Polycarp uses a drink at the moment *T* he can use the next drink not earlier than at the moment *T*<=+<=*r*. What is the minimal number of drinks Polycarp has to use to run over all *n* bridges? If this number is not greater than 105, then you have to find out the moments of time when Polycarp has to use each magical drink.
The first line contains two integers *n* and *r* (1<=≀<=*n*<=≀<=2Β·105, 1<=≀<=*r*<=≀<=1012) β€” the number of bridges and the duration of the effect of a magical drink. The second line contains a sequence of integers *l*1,<=*l*2,<=...,<=*l**n* (1<=≀<=*l**i*<=≀<=5Β·106), where *l**i* is equal to the length of the *i*-th bridge. The third line contains a sequence of integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≀<=*t**i*<=≀<=107), where *t**i* is equal to the maximum allowed time which Polycarp can spend running over the *i*-th bridge.
The first line of the output should contain *k* β€” the minimal number of drinks which Polycarp has to use, or -1 if there is no solution. If the solution exists and the value of *k* is not greater than 105 then output *k* integers on the next line β€” moments of time from beginning of the game when Polycarp has to use drinks. Print the moments of time in chronological order. If there are several solutions, you can output any of them.
[ "1 3\n7\n10\n", "3 3\n3 3 3\n3 3 2\n", "3 100000\n5 5 5\n5 7 8\n", "4 1000\n1 2 3 4\n10 9 10 9\n" ]
[ "2\n0 3\n", "-1\n", "1\n0 \n", "0\n\n" ]
In the first case, there is only one bridge and it is clear that Polycarp cannot run over it without magical drinks. So, if he will use one magical drink on start (moment of time 0), and the second one β€” three seconds later (moment of time 3), he will be able to reach the end of the bridge in time. Please note, in this case there are several possible answers to the problem. For example, Polycarp can use the first drink at the moment of time 4 and the second one β€” at the moment of time 7. In the second case, Polycarp cannot run over all bridges even if he will use magical drinks. So, answer in this case is -1. In the fourth case, Polycarp can run over all bridges without magical drinks.
[]
46
0
0
57,291
449
Jzzhu and Apples
[ "constructive algorithms", "number theory" ]
null
null
Jzzhu has picked *n* apples from his big apple tree. All the apples are numbered from 1 to *n*. Now he wants to sell them to an apple store. Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group. Jzzhu wonders how to get the maximum possible number of groups. Can you help him?
A single integer *n* (1<=≀<=*n*<=≀<=105), the number of the apples.
The first line must contain a single integer *m*, representing the maximum number of groups he can get. Each of the next *m* lines must contain two integers β€” the numbers of apples in the current group. If there are several optimal answers you can print any of them.
[ "6\n", "9\n", "2\n" ]
[ "2\n6 3\n2 4\n", "3\n9 3\n2 4\n6 8\n", "0\n" ]
none
[ { "input": "6", "output": "2\n6 3\n2 4" }, { "input": "9", "output": "3\n9 3\n2 4\n6 8" }, { "input": "2", "output": "0" }, { "input": "10", "output": "4\n2 4\n6 8\n10 5\n9 3" }, { "input": "100", "output": "44\n33 27\n22 11\n25 5\n64 66\n42 44\n31 62\n58 29\n...
546
3,379,200
3
57,451
509
Restoring Numbers
[ "constructive algorithms", "math" ]
null
null
Vasya had two arrays consisting of non-negative integers: *a* of size *n* and *b* of size *m*. Vasya chose a positive integer *k* and created an *n*<=Γ—<=*m* matrix *v* using the following formula: Vasya wrote down matrix *v* on a piece of paper and put it in the table. A year later Vasya was cleaning his table when he found a piece of paper containing an *n*<=Γ—<=*m* matrix *w*. He remembered making a matrix one day by the rules given above but he was not sure if he had found the paper with the matrix *v* from those days. Your task is to find out if the matrix *w* that you've found could have been obtained by following these rules and if it could, then for what numbers *k*,<=*a*1,<=*a*2,<=...,<=*a**n*,<=*b*1,<=*b*2,<=...,<=*b**m* it is possible.
The first line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100), separated by a space β€” the number of rows and columns in the found matrix, respectively. The *i*-th of the following lines contains numbers *w**i*,<=1,<=*w**i*,<=2,<=...,<=*w**i*,<=*m* (0<=≀<=*w**i*,<=*j*<=≀<=109), separated by spaces β€” the elements of the *i*-th row of matrix *w*.
If the matrix *w* could not have been obtained in the manner described above, print "NO" (without quotes) in the single line of output. Otherwise, print four lines. In the first line print "YES" (without quotes). In the second line print an integer *k* (1<=≀<=*k*<=≀<=1018). Note that each element of table *w* should be in range between 0 and *k*<=-<=1 inclusively. In the third line print *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=1018), separated by spaces. In the fourth line print *m* integers *b*1,<=*b*2,<=...,<=*b**m* (0<=≀<=*b**i*<=≀<=1018), separated by spaces.
[ "2 3\n1 2 3\n2 3 4\n", "2 2\n1 2\n2 0\n", "2 2\n1 2\n2 1\n" ]
[ "YES\n1000000007\n0 1 \n1 2 3 ", "YES\n3\n0 1 \n1 2 ", "NO\n" ]
By <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e4ee2bc16f1508a982cfc739e1c7ddc442223116.png" style="max-width: 100.0%;max-height: 100.0%;"/> we denote the remainder of integer division of *b* by *c*. It is guaranteed that if there exists some set of numbers *k*, *a*<sub class="lower-index">1</sub>, ..., *a*<sub class="lower-index">*n*</sub>, *b*<sub class="lower-index">1</sub>, ..., *b*<sub class="lower-index">*m*</sub>, that you could use to make matrix *w*, then there also exists a set of numbers that meets the limits 1 ≀ *k* ≀ 10<sup class="upper-index">18</sup>, 1 ≀ *a*<sub class="lower-index">*i*</sub> ≀ 10<sup class="upper-index">18</sup>, 1 ≀ *b*<sub class="lower-index">*i*</sub> ≀ 10<sup class="upper-index">18</sup> in the output format. In other words, these upper bounds are introduced only for checking convenience purposes.
[ { "input": "2 3\n1 2 3\n2 3 4", "output": "YES\n1000000007\n0 1 \n1 2 3 " }, { "input": "2 2\n1 2\n2 0", "output": "YES\n3\n0 1 \n1 2 " }, { "input": "2 2\n1 2\n2 1", "output": "NO" }, { "input": "2 2\n2 3\n1 2", "output": "YES\n1000000007\n0 1000000006 \n2 3 " }, { ...
46
614,400
3
57,454
17
Balance
[ "dp" ]
C. Balance
3
128
Nick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on a piece of paper and began to perform the following operations: - to take two adjacent characters and replace the second character with the first one, - to take two adjacent characters and replace the first character with the second one To understand these actions better, let's take a look at a string Β«abcΒ». All of the following strings can be obtained by performing one of the described operations on Β«abcΒ»: Β«bbcΒ», Β«abbΒ», Β«accΒ». Let's denote the frequency of a character for each of the characters a, b and c as the number of occurrences of this character in the string. For example, for string Β«abcΒ»: |*a*| = 1, |*b*| = 1, |*c*| = 1, and for string Β«bbcΒ»: |*a*| = 0, |*b*| = 2, |*c*| = 1. While performing the described operations, Nick sometimes got balanced strings. Let's say that a string is balanced, if the frequencies of each character differ by at most 1. That is <=-<=1<=≀<=|*a*|<=-<=|*b*|<=≀<=1, <=-<=1<=≀<=|*a*|<=-<=|*c*|<=≀<=1 ΠΈ <=-<=1<=≀<=|*b*|<=-<=|*c*|<=≀<=1. Would you help Nick find the number of different balanced strings that can be obtained by performing the operations described above, perhaps multiple times, on the given string *s*. This number should be calculated modulo 51123987.
The first line contains integer *n* (1<=≀<=*n*<=≀<=150) β€” the length of the given string *s*. Next line contains the given string *s*. The initial string can be balanced as well, in this case it should be counted too. The given string *s* consists only of characters a, b and c.
Output the only number β€” the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string *s*, modulo 51123987.
[ "4\nabca\n", "4\nabbc\n", "2\nab\n" ]
[ "7\n", "3\n", "1\n" ]
In the first sample it is possible to get 51 different strings through the described operations, but only 7 of them are balanced: Β«abcaΒ», Β«bbcaΒ», Β«bccaΒ», Β«bcaaΒ», Β«abccΒ», Β«abbcΒ», Β«aabcΒ». In the second sample: Β«abbcΒ», Β«aabcΒ», Β«abccΒ». In the third sample there is only one balanced string β€” Β«abΒ» itself.
[]
92
0
0
57,638
314
Sereja and Periods
[ "binary search", "dfs and similar", "strings" ]
null
null
Let's introduce the designation , where *x* is a string, *n* is a positive integer and operation "<=+<=" is the string concatenation operation. For example, [*abc*,<=2]<==<=*abcabc*. We'll say that string *s* can be obtained from string *t*, if we can remove some characters from string *t* and obtain string *s*. For example, strings *ab* and *aсba* can be obtained from string *xacbac*, and strings *bx* and *aaa* cannot be obtained from it. Sereja has two strings, *w*<==<=[*a*,<=*b*] and *q*<==<=[*c*,<=*d*]. He wants to find such maximum integer *p* (*p*<=&gt;<=0), that [*q*,<=*p*] can be obtained from string *w*.
The first line contains two integers *b*, *d* (1<=≀<=*b*,<=*d*<=≀<=107). The second line contains string *a*. The third line contains string *c*. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100.
In a single line print an integer β€” the largest number *p*. If the required value of *p* doesn't exist, print 0.
[ "10 3\nabab\nbab\n" ]
[ "3\n" ]
none
[ { "input": "10 3\nabab\nbab", "output": "3" }, { "input": "841 7\nqjqhrksmvedtqldrqgchhsofokfcovut\nqhtmothoulodshrfejterjlguvooccsvqrrdfqfvkqhtecuhhuqhshthrkusrc", "output": "5" }, { "input": "901 8\nkikjbkgkkjeeficiigjjidfhkfdckjdkkbkfhkhdcjidjbfdfkbhbfjeiffkfgcaigck\ngcjifhjjfedbfbdhi...
124
2,252,800
0
57,774
711
Directed Roads
[ "combinatorics", "dfs and similar", "graphs", "math" ]
null
null
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of *n* towns numbered from 1 to *n*. There are *n* directed roads in the Udayland. *i*-th of them goes from town *i* to some other town *a**i* (*a**i*<=β‰ <=*i*). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town *A* to town *B* before the flip, it will go from town *B* to town *A* after. ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns *A*1,<=*A*2,<=...,<=*A**k* (*k*<=&gt;<=1) such that for every 1<=≀<=*i*<=&lt;<=*k* there is a road from town *A**i* to town *A**i*<=+<=1 and another road from town *A**k* to town *A*1. In other words, the roads are confusing if some of them form a directed cycle of some towns. Now ZS the Coder wonders how many sets of roads (there are 2*n* variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing. Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.
The first line of the input contains single integer *n* (2<=≀<=*n*<=≀<=2Β·105)Β β€” the number of towns in Udayland. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*,<=*a**i*<=β‰ <=*i*), *a**i* denotes a road going from town *i* to town *a**i*.
Print a single integerΒ β€” the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109<=+<=7.
[ "3\n2 3 1\n", "4\n2 1 1 1\n", "5\n2 4 2 5 3\n" ]
[ "6\n", "8\n", "28\n" ]
Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/030fc9181b578c2d906254d38dc56da5554323eb.png" style="max-width: 100.0%;max-height: 100.0%;"/> initially. Number the roads 1 to 3 in this order. The sets of roads that ZS the Coder can flip (to make them not confusing) are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. Note that the empty set is invalid because if no roads are flipped, then towns 1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip. The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing. <img class="tex-graphics" src="https://espresso.codeforces.com/2e275877797bea4817665ce9cfb5274b837194bc.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "3\n2 3 1", "output": "6" }, { "input": "4\n2 1 1 1", "output": "8" }, { "input": "5\n2 4 2 5 3", "output": "28" }, { "input": "4\n2 1 4 3", "output": "4" }, { "input": "7\n2 3 4 1 6 5 4", "output": "56" }, { "input": "20\n2 3 4 5 6 7 8 9 10...
62
7,168,000
0
57,847
311
Interval Cubing
[ "data structures", "math" ]
null
null
While learning Computational Geometry, Tiny is simultaneously learning a useful data structure called segment tree or interval tree. He has scarcely grasped it when comes out a strange problem: Given an integer sequence *a*1,<=*a*2,<=...,<=*a**n*. You should run *q* queries of two types: 1. Given two integers *l* and *r* (1<=≀<=*l*<=≀<=*r*<=≀<=*n*), ask the sum of all elements in the sequence *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r*. 1. Given two integers *l* and *r* (1<=≀<=*l*<=≀<=*r*<=≀<=*n*), let each element *x* in the sequence *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r* becomes *x*3. In other words, apply an assignments *a**l*<==<=*a**l*3,<=*a**l*<=+<=1<==<=*a**l*<=+<=13,<=...,<=*a**r*<==<=*a**r*3. For every query of type 1, output the answer to it. Tiny himself surely cannot work it out, so he asks you for help. In addition, Tiny is a prime lover. He tells you that because the answer may be too huge, you should only output it modulo 95542721 (this number is a prime number).
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105), representing the length of the sequence. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109). The third line contains an integer *q* (1<=≀<=*q*<=≀<=105), representing the number of queries. Then follow *q* lines. Each line contains three integers *t**i* (1<=≀<=*t**i*<=≀<=2), *l**i*, *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*), where *t**i* stands for the type of the query while *l**i* and *r**i* is the parameters of the query, correspondingly.
For each 1-type query, print the answer to it per line. You should notice that each printed number should be non-negative and less than 95542721.
[ "8\n1 2 3 4 5 6 7 8\n5\n1 2 5\n2 2 5\n1 2 5\n2 3 6\n1 4 7\n" ]
[ "14\n224\n2215492\n" ]
none
[]
6,000
14,131,200
0
57,854
333
Characteristics of Rectangles
[ "binary search", "bitmasks", "brute force", "implementation", "sortings" ]
null
null
Gerald found a table consisting of *n* rows and *m* columns. As a prominent expert on rectangular tables, he immediately counted the table's properties, that is, the minimum of the numbers in the corners of the table (minimum of four numbers). However, he did not like the final value β€” it seemed to be too small. And to make this value larger, he decided to crop the table a little: delete some columns on the left and some on the right, as well as some rows from the top and some from the bottom. Find what the maximum property of the table can be after such cropping. Note that the table should have at least two rows and at least two columns left in the end. The number of cropped rows or columns from each of the four sides can be zero.
The first line contains two space-separated integers *n* and *m* (2<=≀<=*n*,<=*m*<=≀<=1000). The following *n* lines describe the table. The *i*-th of these lines lists the space-separated integers *a**i*,<=1,<=*a**i*,<=2,<=...,<=*a**i*,<=*m* (0<=≀<=*a**i*,<=*j*<=≀<=109) β€” the *m* numbers standing in the *i*-th row of the table.
Print the answer to the problem.
[ "2 2\n1 2\n3 4\n", "3 3\n1 0 0\n0 1 1\n1 0 0\n" ]
[ "1\n", "0\n" ]
In the first test case Gerald cannot crop the table β€” table contains only two rows and only two columns. In the second test case if we'll crop the table, the table will contain zero in some corner cell. Also initially it contains two zeros in the corner cells, so the answer is 0.
[ { "input": "2 2\n1 2\n3 4", "output": "1" }, { "input": "3 3\n1 0 0\n0 1 1\n1 0 0", "output": "0" }, { "input": "2 2\n0 0\n0 0", "output": "0" }, { "input": "2 2\n1000000000 1000000000\n1000000000 1000000000", "output": "1000000000" }, { "input": "10 2\n1 20\n19 2...
60
0
0
57,876
630
Game
[ "games", "math" ]
null
null
There is a legend in the IT City college. A student that failed to answer all questions on the game theory exam is given one more chance by his professor. The student has to play a game with the professor. The game is played on a square field consisting of *n*<=Γ—<=*n* cells. Initially all cells are empty. On each turn a player chooses and paint an empty cell that has no common sides with previously painted cells. Adjacent corner of painted cells is allowed. On the next turn another player does the same, then the first one and so on. The player with no cells to paint on his turn loses. The professor have chosen the field size *n* and allowed the student to choose to be the first or the second player in the game. What should the student choose to win the game? Both players play optimally.
The only line of the input contains one integer *n* (1<=≀<=*n*<=≀<=1018) β€” the size of the field.
Output number 1, if the player making the first turn wins when both players play optimally, otherwise print number 2.
[ "1\n", "2\n" ]
[ "1", "2" ]
none
[ { "input": "1", "output": "1" }, { "input": "2", "output": "2" }, { "input": "3", "output": "1" }, { "input": "4", "output": "2" }, { "input": "5", "output": "1" }, { "input": "1000000000000000000", "output": "2" }, { "input": "999999999999...
0
0
-1
57,885
295
Greg and Friends
[ "combinatorics", "dp", "graphs", "shortest paths" ]
null
null
One day Greg and his friends were walking in the forest. Overall there were *n* people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold people with the total weight of at most *k* kilograms. Greg immediately took a piece of paper and listed there the weights of all people in his group (including himself). It turned out that each person weights either 50 or 100 kilograms. Now Greg wants to know what minimum number of times the boat needs to cross the river to transport the whole group to the other bank. The boat needs at least one person to navigate it from one bank to the other. As the boat crosses the river, it can have any non-zero number of passengers as long as their total weight doesn't exceed *k*. Also Greg is wondering, how many ways there are to transport everybody to the other side in the minimum number of boat rides. Two ways are considered distinct if during some ride they have distinct sets of people on the boat. Help Greg with this problem.
The first line contains two integers *n*, *k* (1<=≀<=*n*<=≀<=50,<=1<=≀<=*k*<=≀<=5000) β€” the number of people, including Greg, and the boat's weight limit. The next line contains *n* integers β€” the people's weights. A person's weight is either 50 kilos or 100 kilos. You can consider Greg and his friends indexed in some way.
In the first line print an integer β€” the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer -1. In the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (109<=+<=7). If transporting everyone to the other bank is impossible, print integer 0.
[ "1 50\n50\n", "3 100\n50 50 100\n", "2 50\n50 50\n" ]
[ "1\n1\n", "5\n2\n", "-1\n0\n" ]
In the first test Greg walks alone and consequently, he needs only one ride across the river. In the second test you should follow the plan: 1. transport two 50 kg. people; 1. transport one 50 kg. person back; 1. transport one 100 kg. person; 1. transport one 50 kg. person back; 1. transport two 50 kg. people. That totals to 5 rides. Depending on which person to choose at step 2, we can get two distinct ways.
[ { "input": "1 50\n50", "output": "1\n1" }, { "input": "3 100\n50 50 100", "output": "5\n2" }, { "input": "2 50\n50 50", "output": "-1\n0" }, { "input": "5 258\n100 100 50 50 50", "output": "3\n72" }, { "input": "8 191\n50 100 50 100 50 100 100 50", "output": "...
0
0
-1
57,911
535
Tavas and Malekas
[ "greedy", "hashing", "string suffix structures", "strings" ]
null
null
Tavas is a strange creature. Usually "zzz" comes out of people's mouth while sleeping, but string *s* of length *n* comes out from Tavas' mouth instead. Today Tavas fell asleep in Malekas' place. While he was sleeping, Malekas did a little process on *s*. Malekas has a favorite string *p*. He determined all positions *x*1<=&lt;<=*x*2<=&lt;<=...<=&lt;<=*x**k* where *p* matches *s*. More formally, for each *x**i* (1<=≀<=*i*<=≀<=*k*) he condition *s**x**i**s**x**i*<=+<=1... *s**x**i*<=+<=|*p*|<=-<=1<==<=*p* is fullfilled. Then Malekas wrote down one of subsequences of *x*1,<=*x*2,<=... *x**k* (possibly, he didn't write anything) on a piece of paper. Here a sequence *b* is a subsequence of sequence *a* if and only if we can turn *a* into *b* by removing some of its elements (maybe no one of them or all). After Tavas woke up, Malekas told him everything. He couldn't remember string *s*, but he knew that both *p* and *s* only contains lowercase English letters and also he had the subsequence he had written on that piece of paper. Tavas wonders, what is the number of possible values of *s*? He asked SaDDas, but he wasn't smart enough to solve this. So, Tavas asked you to calculate this number for him. Answer can be very large, so Tavas wants you to print the answer modulo 109<=+<=7.
The first line contains two integers *n* and *m*, the length of *s* and the length of the subsequence Malekas wrote down (1<=≀<=*n*<=≀<=106 and 0<=≀<=*m*<=≀<=*n*<=-<=|*p*|<=+<=1). The second line contains string *p* (1<=≀<=|*p*|<=≀<=*n*). The next line contains *m* space separated integers *y*1,<=*y*2,<=...,<=*y**m*, Malekas' subsequence (1<=≀<=*y*1<=&lt;<=*y*2<=&lt;<=...<=&lt;<=*y**m*<=≀<=*n*<=-<=|*p*|<=+<=1).
In a single line print the answer modulo 1000<=000<=007.
[ "6 2\nioi\n1 3\n", "5 2\nioi\n1 2\n" ]
[ "26\n", "0\n" ]
In the first sample test all strings of form "ioioi?" where the question mark replaces arbitrary English letter satisfy. Here |*x*| denotes the length of string x. Please note that it's possible that there is no such string (answer is 0).
[ { "input": "6 2\nioi\n1 3", "output": "26" }, { "input": "5 2\nioi\n1 2", "output": "0" }, { "input": "173700 6\nbcabcbcbcbaaacaccaacaccaabacabaacbcacbbccaccbcacbabcaccccccaacacabbbbbacabbaaacbcbbaccaccabbbbaabbacacbabccaabcabbbcacaaccbabbcaaaaaabccbbcabcacbcbcabcbcbbaabacaaccccabacaaacc...
2,000
268,390,400
0
57,956
1,000
One Occurrence
[ "data structures", "divide and conquer" ]
null
null
You are given an array $a$ consisting of $n$ integers, and $q$ queries to it. $i$-th query is denoted by two integers $l_i$ and $r_i$. For each query, you have to find any integer that occurs exactly once in the subarray of $a$ from index $l_i$ to index $r_i$ (a subarray is a contiguous subsegment of an array). For example, if $a = [1, 1, 2, 3, 2, 4]$, then for query $(l_i = 2, r_i = 6)$ the subarray we are interested in is $[1, 2, 3, 2, 4]$, and possible answers are $1$, $3$ and $4$; for query $(l_i = 1, r_i = 2)$ the subarray we are interested in is $[1, 1]$, and there is no such element that occurs exactly once. Can you answer all of the queries?
The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 5 \cdot 10^5$). The third line contains one integer $q$ ($1 \le q \le 5 \cdot 10^5$). Then $q$ lines follow, $i$-th line containing two integers $l_i$ and $r_i$ representing $i$-th query ($1 \le l_i \le r_i \le n$).
Answer the queries as follows: If there is no integer such that it occurs in the subarray from index $l_i$ to index $r_i$ exactly once, print $0$. Otherwise print any such integer.
[ "6\n1 1 2 3 2 4\n2\n2 6\n1 2\n" ]
[ "4\n0\n" ]
none
[ { "input": "6\n1 1 2 3 2 4\n2\n2 6\n1 2", "output": "4\n0" }, { "input": "10\n5 9 6 4 8 7 4 9 7 6\n10\n4 9\n4 7\n4 7\n4 8\n9 10\n4 9\n4 8\n1 5\n4 4\n2 5", "output": "8\n8\n8\n8\n6\n8\n8\n8\n4\n9" } ]
3,000
41,062,400
0
58,017
379
New Year Letter
[ "bitmasks", "brute force", "dp" ]
null
null
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas). Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, *s*1 anf *s*2, consisting of uppercase English letters. Then the boy makes string *s**k*, using a recurrent equation *s**n*<==<=*s**n*<=-<=2<=+<=*s**n*<=-<=1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string *s**k* on a piece of paper, puts it in the envelope and sends in to Santa. Vasya is absolutely sure that Santa will bring him the best present if the resulting string *s**k* has exactly *x* occurrences of substring AC (the short-cut reminds him ΠΎf accepted problems). Besides, Vasya decided that string *s*1 should have length *n*, and string *s*2 should have length *m*. Vasya hasn't decided anything else. At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, *s*1 and *s*2 in the required manner. Help Vasya.
The first line contains four integers *k*,<=*x*,<=*n*,<=*m* (3<=≀<=*k*<=≀<=50;Β 0<=≀<=*x*<=≀<=109;Β 1<=≀<=*n*,<=*m*<=≀<=100).
In the first line print string *s*1, consisting of *n* uppercase English letters. In the second line print string *s*2, consisting of *m* uppercase English letters. If there are multiple valid strings, print any of them. If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
[ "3 2 2 2\n", "3 3 2 2\n", "3 0 2 2\n", "4 3 2 1\n", "4 2 2 1\n" ]
[ "AC\nAC\n", "Happy new year!\n", "AA\nAA\n", "Happy new year!\n", "Happy new year!\n" ]
none
[ { "input": "3 2 2 2", "output": "AC\nAC" }, { "input": "3 3 2 2", "output": "Happy new year!" }, { "input": "3 0 2 2", "output": "AA\nAA" }, { "input": "4 3 2 1", "output": "Happy new year!" }, { "input": "4 2 2 1", "output": "Happy new year!" }, { "in...
46
0
0
58,246
101
Castle
[ "dp", "greedy", "probabilities", "sortings", "trees" ]
D. Castle
2
256
Gerald is positioned in an old castle which consists of *n* halls connected with *n*<=-<=1 corridors. It is exactly one way to go from any hall to any other one. Thus, the graph is a tree. Initially, at the moment of time 0, Gerald is positioned in hall 1. Besides, some other hall of the castle contains the treasure Gerald is looking for. The treasure's position is not known; it can equiprobably be in any of other *n*<=-<=1 halls. Gerald can only find out where the treasure is when he enters the hall with the treasure. That very moment Gerald sees the treasure and the moment is regarded is the moment of achieving his goal. The corridors have different lengths. At that, the corridors are considered long and the halls are considered small and well lit. Thus, it is possible not to take the time Gerald spends in the halls into consideration. The castle is very old, that's why a corridor collapses at the moment when somebody visits it two times, no matter in which direction. Gerald can move around the castle using the corridors; he will go until he finds the treasure. Naturally, Gerald wants to find it as quickly as possible. In other words, he wants to act in a manner that would make the average time of finding the treasure as small as possible. Each corridor can be used no more than two times. That's why Gerald chooses the strategy in such a way, so he can visit every hall for sure. More formally, if the treasure is located in the second hall, then Gerald will find it the moment he enters the second hall for the first time β€” let it be moment *t*2. If the treasure is in the third hall, then Gerald will find it the moment he enters the third hall for the first time. Let it be the moment of time *t*3. And so on. Thus, the average time of finding the treasure will be equal to .
The first line contains the only integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of halls in the castle. Next *n*<=-<=1 lines each contain three integers. The *i*-th line contains numbers *a**i*, *b**i* and *t**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*, 1<=≀<=*t**i*<=≀<=1000) β€” the numbers of halls connected with the *i*-th corridor and the time needed to go along the corridor. Initially Gerald is in the hall number 1. It is guaranteed that one can get from any hall to any other one using corridors.
Print the only real number: the sought expectation of time needed to find the treasure. The answer should differ from the right one in no less than 10<=-<=6.
[ "2\n1 2 1\n", "4\n1 3 2\n4 2 1\n3 2 3\n", "5\n1 2 1\n1 3 1\n1 4 1\n1 5 1\n" ]
[ "1.0\n", "4.333333333333334\n", "4.0\n" ]
In the first test the castle only has two halls which means that the treasure is located in the second hall. Gerald will only need one minute to go to the second hall from the first one. In the second test Gerald can only go from the first hall to the third one. He can get from the third room to the first one or to the second one, but he has already visited the first hall and can get nowhere from there. Thus, he needs to go to the second hall. He should go to hall 4 from there, because all other halls have already been visited. If the treasure is located in the third hall, Gerald will find it in a minute, if the treasure is located in the second hall, Gerald finds it in two minutes, if the treasure is in the fourth hall, Gerald will find it in three minutes. The average time makes 2 minutes. In the third test Gerald needs to visit 4 halls: the second, third, fourth and fifth ones. All of them are only reachable from the first hall. Thus, he needs to go to those 4 halls one by one and return. Gerald will enter the first of those halls in a minute, in the second one β€” in three minutes, in the third one - in 5 minutes, in the fourth one - in 7 minutes. The average time is 4 minutes.
[]
124
0
0
58,304
986
Petr and Permutations
[ "combinatorics", "math" ]
null
null
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from $1$ to $n$ and then $3n$ times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements $7n+1$ times instead of $3n$ times. Because it is more random, OK?! You somehow get a test from one of these problems and now you want to know from which one.
In the first line of input there is one integer $n$ ($10^{3} \le n \le 10^{6}$). In the second line there are $n$ distinct integers between $1$ and $n$Β β€” the permutation of size $n$ from the test. It is guaranteed that all tests except for sample are generated this way: First we choose $n$Β β€” the size of the permutation. Then we randomly choose a method to generate a permutationΒ β€” the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
[ "5\n2 4 5 1 3\n" ]
[ "Petr\n" ]
Please note that the sample is not a valid test (because of limitations for $n$) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC. Due to randomness of input hacks in this problem are forbidden.
[ { "input": "5\n2 4 5 1 3", "output": "Petr" } ]
217
22,323,200
-1
58,499
778
Peterson Polyglot
[ "brute force", "dfs and similar", "dsu", "hashing", "strings", "trees" ]
null
null
Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set of words, and word is a sequence of lowercase Latin letters. Peterson makes new language every morning. It is difficult task to store the whole language, so Peterson have invented new data structure for storing his languages which is called broom. Broom is rooted tree with edges marked with letters. Initially broom is represented by the only vertexΒ β€” the root of the broom. When Peterson wants to add new word to the language he stands at the root and processes the letters of new word one by one. Consider that Peterson stands at the vertex *u*. If there is an edge from *u* marked with current letter, Peterson goes through this edge. Otherwise Peterson adds new edge from *u* to the new vertex *v*, marks it with the current letter and goes through the new edge. Size of broom is the number of vertices in it. In the evening after working day Peterson can't understand the language he made this morning. It is too difficult for bored Peterson and he tries to make it simpler. Simplification of the language is the process of erasing some letters from some words of this language. Formally, Peterson takes some positive integer *p* and erases *p*-th letter from all the words of this language having length at least *p*. Letters in words are indexed starting by 1. Peterson considers that simplification should change at least one word, i.e. there has to be at least one word of length at least *p*. Peterson tries to make his language as simple as possible, so he wants to choose *p* such that the size of the broom for his simplified language is as small as possible. Peterson is pretty annoyed with this task so he asks you for help. Write a program to find the smallest possible size of the broom and integer *p*.
The first line of input contains integer *n* (2<=≀<=*n*<=≀<=3Β·105)Β β€” the size of the broom. Next *n*<=-<=1 lines describe the broom: *i*-th of them contains integers *u**i*, *v**i* and letter *x**i*Β β€” describing the edge from *u**i* to *v**i* marked with letter *x**i*. Vertices are numbered from 1 to *n*. All *x**i* are lowercase latin letters. Vertex 1 is the root of the broom. Edges describe correct broom which is made from Peterson's language.
The first line of output should contain the minimum possible size of the broom after its simplification. The second line of output should contain integer *p* to choose. If there are several suitable *p* values, print the smallest one.
[ "5\n1 2 c\n2 3 a\n3 4 t\n2 5 t\n", "16\n1 2 o\n2 3 f\n1 4 p\n4 5 i\n5 6 e\n6 7 c\n7 8 e\n4 9 r\n9 10 e\n10 11 t\n11 12 t\n12 13 y\n10 14 f\n14 15 i\n15 16 x\n" ]
[ "3\n2\n", "12\n2\n" ]
<img class="tex-graphics" src="https://espresso.codeforces.com/4b46644a485274790bd64830c23320ae20be3097.png" style="max-width: 100.0%;max-height: 100.0%;"/> Broom from the second sample test can be built using language "piece", "of", "pie", "pretty", "prefix". Its simplification with *p* = 2 obtains the language of words "pece", "o", "pe", "petty", "pefix". This language gives us the broom with minimum possible size.
[ { "input": "5\n1 2 c\n2 3 a\n3 4 t\n2 5 t", "output": "3\n2" }, { "input": "16\n1 2 o\n2 3 f\n1 4 p\n4 5 i\n5 6 e\n6 7 c\n7 8 e\n4 9 r\n9 10 e\n10 11 t\n11 12 t\n12 13 y\n10 14 f\n14 15 i\n15 16 x", "output": "12\n2" }, { "input": "2\n1 2 o", "output": "1\n1" }, { "input": "3...
4,000
24,780,800
0
58,552
0
none
[ "none" ]
null
null
Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of *n* small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than *k* characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than *k* characters are deleted. You also have to find any possible way to delete the characters.
The first input data line contains a string whose length is equal to *n* (1<=≀<=*n*<=≀<=105). The string consists of lowercase Latin letters. The second line contains the number *k* (0<=≀<=*k*<=≀<=105).
Print on the first line the only number *m* β€” the least possible number of different characters that could remain in the given string after it loses no more than *k* characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly *m* distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly *m* distinct characters, print any of them.
[ "aaaaa\n4\n", "abacaba\n4\n", "abcdefgh\n10\n" ]
[ "1\naaaaa\n", "1\naaaa\n", "0\n\n" ]
In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and *k* = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string.
[ { "input": "aaaaa\n4", "output": "1\naaaaa" }, { "input": "abacaba\n4", "output": "1\naaaa" }, { "input": "abcdefgh\n10", "output": "0" }, { "input": "aaaaaaaaaaaaaaaaaaaa\n19", "output": "1\naaaaaaaaaaaaaaaaaaaa" }, { "input": "abcdefghijjihgedcba\n0", "outpu...
404
512,000
3
58,553
149
Coloring Brackets
[ "dp" ]
null
null
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it. You are given string *s*. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. For example, such sequences as "(())()" and "()" are correct bracket sequences and such sequences as ")()" and "(()" are not. In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa). For example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the fifth bracket corresponds to the fourth one. You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled: - Each bracket is either not colored any color, or is colored red, or is colored blue. - For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored. - No two neighboring colored brackets have the same color. Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007 (109<=+<=7).
The first line contains the single string *s* (2<=≀<=|*s*|<=≀<=700) which represents a correct bracket sequence.
Print the only number β€” the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109<=+<=7).
[ "(())\n", "(()())\n", "()\n" ]
[ "12\n", "40\n", "4\n" ]
Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below. The two ways of coloring shown below are incorrect.
[ { "input": "(())", "output": "12" }, { "input": "(()())", "output": "40" }, { "input": "()", "output": "4" }, { "input": "((()))", "output": "36" }, { "input": "()(())", "output": "42" }, { "input": "()()()", "output": "48" }, { "input": "(...
310
4,300,800
3
58,728
420
Bug in Code
[ "data structures", "graphs", "implementation", "two pointers" ]
null
null
Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the *n* coders on the meeting said: 'I know for sure that either *x* or *y* did it!' The head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders' opinions. That's why the head wants to make such a choice that at least *p* of *n* coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects? Note that even if some coder was chosen as a suspect, he can agree with the head's choice if he named the other chosen coder at the meeting.
The first line contains integers *n* and *p* (3<=≀<=*n*<=≀<=3Β·105;Β 0<=≀<=*p*<=≀<=*n*) β€” the number of coders in the F company and the minimum number of agreed people. Each of the next *n* lines contains two integers *x**i*, *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*) β€” the numbers of coders named by the *i*-th coder. It is guaranteed that *x**i*<=β‰ <=*i*,<=Β *y**i*<=β‰ <=*i*,<=Β *x**i*<=β‰ <=*y**i*.
Print a single integer –– the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets (1,<=2) ΠΈ (2,<=1) are considered identical.
[ "4 2\n2 3\n1 4\n1 4\n2 1\n", "8 6\n5 6\n5 7\n5 8\n6 2\n2 1\n7 3\n1 3\n1 4\n" ]
[ "6\n", "1\n" ]
none
[ { "input": "4 2\n2 3\n1 4\n1 4\n2 1", "output": "6" }, { "input": "8 6\n5 6\n5 7\n5 8\n6 2\n2 1\n7 3\n1 3\n1 4", "output": "1" }, { "input": "3 2\n2 3\n3 1\n2 1", "output": "3" }, { "input": "4 1\n3 2\n4 1\n4 2\n1 2", "output": "6" }, { "input": "4 2\n3 4\n4 3\n4 ...
1,000
54,681,600
0
58,891
961
Partitions
[ "combinatorics", "math", "number theory" ]
null
null
You are given a set of *n* elements indexed from 1 to *n*. The weight of *i*-th element is *w**i*. The weight of some subset of a given set is denoted as . The weight of some partition *R* of a given set into *k* subsets is (recall that a partition of a given set is a set of its subsets such that every element of the given set belongs to exactly one subset in partition). Calculate the sum of weights of all partitions of a given set into exactly *k* non-empty subsets, and print it modulo 109<=+<=7. Two partitions are considered different iff there exist two elements *x* and *y* such that they belong to the same set in one of the partitions, and to different sets in another partition.
The first line contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=2Β·105) β€” the number of elements and the number of subsets in each partition, respectively. The second line contains *n* integers *w**i* (1<=≀<=*w**i*<=≀<=109)β€” weights of elements of the set.
Print one integer β€” the sum of weights of all partitions of a given set into *k* non-empty subsets, taken modulo 109<=+<=7.
[ "4 2\n2 3 2 3\n", "5 2\n1 2 3 4 5\n" ]
[ "160\n", "645\n" ]
Possible partitions in the first sample: 1. {{1, 2, 3}, {4}}, *W*(*R*) = 3Β·(*w*<sub class="lower-index">1</sub> + *w*<sub class="lower-index">2</sub> + *w*<sub class="lower-index">3</sub>) + 1Β·*w*<sub class="lower-index">4</sub> = 24; 1. {{1, 2, 4}, {3}}, *W*(*R*) = 26; 1. {{1, 3, 4}, {2}}, *W*(*R*) = 24; 1. {{1, 2}, {3, 4}}, *W*(*R*) = 2Β·(*w*<sub class="lower-index">1</sub> + *w*<sub class="lower-index">2</sub>) + 2Β·(*w*<sub class="lower-index">3</sub> + *w*<sub class="lower-index">4</sub>) = 20; 1. {{1, 3}, {2, 4}}, *W*(*R*) = 20; 1. {{1, 4}, {2, 3}}, *W*(*R*) = 20; 1. {{1}, {2, 3, 4}}, *W*(*R*) = 26; Possible partitions in the second sample: 1. {{1, 2, 3, 4}, {5}}, *W*(*R*) = 45; 1. {{1, 2, 3, 5}, {4}}, *W*(*R*) = 48; 1. {{1, 2, 4, 5}, {3}}, *W*(*R*) = 51; 1. {{1, 3, 4, 5}, {2}}, *W*(*R*) = 54; 1. {{2, 3, 4, 5}, {1}}, *W*(*R*) = 57; 1. {{1, 2, 3}, {4, 5}}, *W*(*R*) = 36; 1. {{1, 2, 4}, {3, 5}}, *W*(*R*) = 37; 1. {{1, 2, 5}, {3, 4}}, *W*(*R*) = 38; 1. {{1, 3, 4}, {2, 5}}, *W*(*R*) = 38; 1. {{1, 3, 5}, {2, 4}}, *W*(*R*) = 39; 1. {{1, 4, 5}, {2, 3}}, *W*(*R*) = 40; 1. {{2, 3, 4}, {1, 5}}, *W*(*R*) = 39; 1. {{2, 3, 5}, {1, 4}}, *W*(*R*) = 40; 1. {{2, 4, 5}, {1, 3}}, *W*(*R*) = 41; 1. {{3, 4, 5}, {1, 2}}, *W*(*R*) = 42.
[ { "input": "4 2\n2 3 2 3", "output": "160" }, { "input": "5 2\n1 2 3 4 5", "output": "645" }, { "input": "1 1\n1", "output": "1" }, { "input": "1 1\n1000000000", "output": "1000000000" }, { "input": "2 1\n6042 8885", "output": "29854" }, { "input": "2 ...
280
36,761,600
0
59,146
870
Paths
[ "data structures", "number theory" ]
null
null
You are given a positive integer *n*. Let's build a graph on vertices 1,<=2,<=...,<=*n* in such a way that there is an edge between vertices *u* and *v* if and only if . Let *d*(*u*,<=*v*) be the shortest distance between *u* and *v*, or 0 if there is no path between them. Compute the sum of values *d*(*u*,<=*v*) over all 1<=≀<=*u*<=&lt;<=*v*<=≀<=*n*. The *gcd* (greatest common divisor) of two positive integers is the maximum positive integer that divides both of the integers.
Single integer *n* (1<=≀<=*n*<=≀<=107).
Print the sum of *d*(*u*,<=*v*) over all 1<=≀<=*u*<=&lt;<=*v*<=≀<=*n*.
[ "6\n", "10\n" ]
[ "8\n", "44\n" ]
All shortest paths in the first example: - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7b7a110945393c2b47718b811aadf12cbe125f3f.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e9af98cb64a31b6dae850dbfb30429b9a091c123.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d43a3ea9a040a07b650cc4d74a5f6a69395d0005.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1725357f3b9d9284226b8c2848aab916720da24f.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9e05d0e132bba9db531dc61149062e6f9f5ff34a.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/043fdd1108540c9fbac49c9177d6ec0021cbd896.png" style="max-width: 100.0%;max-height: 100.0%;"/> There are no paths between other pairs of vertices. The total distance is 2 + 1 + 1 + 2 + 1 + 1 = 8.
[]
31
0
0
59,219
1,010
Store
[ "data structures" ]
null
null
Natasha was already going to fly back to Earth when she remembered that she needs to go to the Martian store to buy Martian souvenirs for her friends. It is known, that the Martian year lasts $x_{max}$ months, month lasts $y_{max}$ days, day lasts $z_{max}$ seconds. Natasha also knows that this store works according to the following schedule: 2 months in a year were selected: $x_l$ and $x_r$ ($1\le x_l\le x_r\le x_{max}$), 2 days in a month: $y_l$ and $y_r$ ($1\le y_l\le y_r\le y_{max}$) and 2 seconds in a day: $z_l$ and $z_r$ ($1\le z_l\le z_r\le z_{max}$). The store works at all such moments (month $x$, day $y$, second $z$), when simultaneously $x_l\le x\le x_r$, $y_l\le y\le y_r$ and $z_l\le z\le z_r$. Unfortunately, Natasha does not know the numbers $x_l,x_r,y_l,y_r,z_l,z_r$. One Martian told Natasha: "I went to this store $(n+m)$ times. $n$ times of them it was opened, and $m$ timesΒ β€” closed." He also described his every trip to the store: the month, day, second of the trip and whether the store was open or closed at that moment. Natasha can go to the store $k$ times. For each of them, determine whether the store at the time of the trip is open, closed, or this information is unknown.
The first line contains $6$ integers $x_{max}$, $y_{max}$, $z_{max}$, $n$, $m$, $k$ ($1\le x_{max},y_{max},z_{max}\le 10^5$, $1\le n\le 10^5$, $0\le m\le 10^5$, $1\le k\le 10^5$)Β β€” number of months in a year, days in a month, seconds in a day, times when the store (according to a Martian) was opened, when it was closed and Natasha's queries. The $i$-th of the next $n$ lines contains $3$ integers $x_i$, $y_i$, $z_i$ ($1\le x_i\le x_{max}$, $1\le y_i\le y_{max}$, $1\le z_i\le z_{max}$)Β β€” month, day and second of $i$-th time, when the store, according to the Martian, was opened. The $i$-th of the next $m$ lines contains $3$ integers $x_i$, $y_i$, $z_i$ ($1\le x_i\le x_{max}$, $1\le y_i\le y_{max}$, $1\le z_i\le z_{max}$)Β β€” month, day and second of $i$-th time, when the store, according to the Martian, was closed. The $i$-th of the next $k$ lines contains $3$ integers $x_i$, $y_i$, $z_i$ ($1\le x_i\le x_{max}$, $1\le y_i\le y_{max}$, $1\le z_i\le z_{max}$)Β β€” month, day and second of $i$-th Natasha's query.
If the Martian was mistaken and his information about when the store is open and when it is closed is inconsistent, print a single line "INCORRECT" (without quotes). Otherwise, print the first line "CORRECT" (without quotes). Next output $k$ lines: in $i$-th of them, output an answer to $i$-th Natasha's query: "OPEN" (without quotes), if the store was opened at the moment of this query, "CLOSED" (without quotes), if it was closed, or "UNKNOWN" (without quotes), if this information can not be determined on the basis of available data.
[ "10 10 10 3 1 3\n2 6 2\n4 2 4\n6 4 6\n9 9 9\n3 3 3\n10 10 10\n8 8 8\n", "10 10 10 1 1 1\n2 5 7\n2 5 7\n8 9 10\n" ]
[ "CORRECT\nOPEN\nCLOSED\nUNKNOWN\n", "INCORRECT\n" ]
Consider the first test case. There are $10$ months in a year, $10$ days in a month, and $10$ seconds in a day. The store was opened in $3$ moments: - month $2$, day $6$, second $2$;- month $4$, day $2$, second $4$;- month $6$, day $4$, second $6$. The store was closed at the time: month $9$, day $9$, second $9$. Queries: - month $3$, day $3$, second $3$Β β€” open ("OPEN") (since the store opens no later than month $2$, day $2$, second $2$ and closes no earlier than in month $6$, day $6$, second $6$); - month $10$, day $10$, second $10$Β β€” closed ("CLOSED") (since it is closed even in the month $9$, day $9$, second $9$);- month $8$, day $8$, second $8$Β β€” unknown ("UNKNOWN") (because the schedule in which the store is open at this moment exists, and the schedule in which the store is closed at this moment exists as well). In the second test case, the store was closed and opened at the same timeΒ β€” contradiction ("INCORRECT").
[]
108
0
0
59,302
630
A rectangle
[ "math" ]
null
null
Developing tools for creation of locations maps for turn-based fights in a new game, Petya faced the following problem. A field map consists of hexagonal cells. Since locations sizes are going to be big, a game designer wants to have a tool for quick filling of a field part with identical enemy units. This action will look like following: a game designer will select a rectangular area on the map, and each cell whose center belongs to the selected rectangle will be filled with the enemy unit. More formally, if a game designer selected cells having coordinates (*x*1,<=*y*1) and (*x*2,<=*y*2), where *x*1<=≀<=*x*2 and *y*1<=≀<=*y*2, then all cells having center coordinates (*x*,<=*y*) such that *x*1<=≀<=*x*<=≀<=*x*2 and *y*1<=≀<=*y*<=≀<=*y*2 will be filled. Orthogonal coordinates system is set up so that one of cell sides is parallel to *OX* axis, all hexagon centers have integer coordinates and for each integer *x* there are cells having center with such *x* coordinate and for each integer *y* there are cells having center with such *y* coordinate. It is guaranteed that difference *x*2<=-<=*x*1 is divisible by 2. Working on the problem Petya decided that before painting selected units he wants to output number of units that will be painted on the map. Help him implement counting of these units before painting.
The only line of input contains four integers *x*1,<=*y*1,<=*x*2,<=*y*2 (<=-<=109<=≀<=*x*1<=≀<=*x*2<=≀<=109,<=<=-<=109<=≀<=*y*1<=≀<=*y*2<=≀<=109) β€” the coordinates of the centers of two cells.
Output one integer β€” the number of cells to be filled.
[ "1 1 5 5\n" ]
[ "13" ]
none
[ { "input": "1 1 5 5", "output": "13" }, { "input": "-1 -3 1 3", "output": "11" }, { "input": "-2 -2 2 2", "output": "13" }, { "input": "0 0 2 2", "output": "5" }, { "input": "0 0 2 0", "output": "2" }, { "input": "0 0 0 0", "output": "1" }, { ...
31
0
0
59,308
659
Fence Divercity
[ "combinatorics", "dp", "number theory" ]
null
null
Long ago, Vasily built a good fence at his country house. Vasily calls a fence good, if it is a series of *n* consecutively fastened vertical boards of centimeter width, the height of each in centimeters is a positive integer. The house owner remembers that the height of the *i*-th board to the left is *h**i*. Today Vasily decided to change the design of the fence he had built, by cutting his top connected part so that the fence remained good. The cut part should consist of only the upper parts of the boards, while the adjacent parts must be interconnected (share a non-zero length before cutting out of the fence). You, as Vasily's curious neighbor, will count the number of possible ways to cut exactly one part as is described above. Two ways to cut a part are called distinct, if for the remaining fences there is such *i*, that the height of the *i*-th boards vary. As Vasily's fence can be very high and long, get the remainder after dividing the required number of ways by 1<=000<=000<=007 (109<=+<=7).
The first line contains integer *n* (1<=≀<=*n*<=≀<=1<=000<=000)Β β€” the number of boards in Vasily's fence. The second line contains *n* space-separated numbers *h*1,<=*h*2,<=...,<=*h**n* (1<=≀<=*h**i*<=≀<=109), where *h**i* equals the height of the *i*-th board to the left.
Print the remainder after dividing *r* by 1<=000<=000<=007, where *r* is the number of ways to cut exactly one connected part so that the part consisted of the upper parts of the boards and the remaining fence was good.
[ "2\n1 1\n", "3\n3 4 2\n" ]
[ "0\n", "13\n" ]
From the fence from the first example it is impossible to cut exactly one piece so as the remaining fence was good. All the possible variants of the resulting fence from the second sample look as follows (the grey shows the cut out part):
[ { "input": "2\n1 1", "output": "0" }, { "input": "3\n3 4 2", "output": "13" }, { "input": "1\n1", "output": "0" }, { "input": "1\n1000000000", "output": "999999999" }, { "input": "3\n1 2 3", "output": "4" }, { "input": "2\n2 3", "output": "4" }, ...
2,000
137,318,400
0
59,374
268
Wall Bars
[ "dp" ]
null
null
Manao is working for a construction company. Recently, an order came to build wall bars in a children's park. Manao was commissioned to develop a plan of construction, which will enable the company to save the most money. After reviewing the formal specifications for the wall bars, Manao discovered a number of controversial requirements and decided to treat them to the company's advantage. His resulting design can be described as follows: - Let's introduce some unit of length. The construction center is a pole of height *n*. - At heights 1,<=2,<=...,<=*n* exactly one horizontal bar sticks out from the pole. Each bar sticks in one of four pre-fixed directions. - A child can move from one bar to another if the distance between them does not exceed *h* and they stick in the same direction. If a child is on the ground, he can climb onto any of the bars at height between 1 and *h*. In Manao's construction a child should be able to reach at least one of the bars at heights *n*<=-<=*h*<=+<=1,<=*n*<=-<=*h*<=+<=2,<=...,<=*n* if he begins at the ground. Manao is wondering how many distinct construction designs that satisfy his requirements exist. As this number can be rather large, print the remainder after dividing it by 1000000009Β (109<=+<=9). Two designs are considered distinct if there is such height *i*, that the bars on the height *i* in these designs don't stick out in the same direction.
A single line contains two space-separated integers, *n* and *h* (1<=≀<=*n*<=≀<=1000, 1<=≀<=*h*<=≀<=*min*(*n*,<=30)).
In a single line print the remainder after dividing the number of designs by 1000000009Β (109<=+<=9).
[ "5 1\n", "4 2\n", "4 3\n", "5 2\n" ]
[ "4\n", "148\n", "256\n", "376\n" ]
Consider several designs for *h* = 2. A design with the first bar sticked out in direction *d*<sub class="lower-index">1</sub>, the second β€” in direction *d*<sub class="lower-index">2</sub> and so on (1 ≀ *d*<sub class="lower-index">*i*</sub> ≀ 4) is denoted as string *d*<sub class="lower-index">1</sub>*d*<sub class="lower-index">2</sub>...*d*<sub class="lower-index">*n*</sub>. Design "1231" (the first three bars are sticked out in different directions, the last one β€” in the same as first). A child can reach neither the bar at height 3 nor the bar at height 4. Design "414141". A child can reach the bar at height 5. To do this, he should first climb at the first bar, then at the third and then at the fifth one. He can also reach bar at height 6 by the route second  →  fourth  →  sixth bars. Design "123333". The child can't reach the upper two bars. Design "323323". The bar at height 6 can be reached by the following route: first  →  third  →  fourth  →  sixth bars.
[]
60
0
0
59,411
460
Little Victor and Set
[ "brute force", "constructive algorithms", "math" ]
null
null
Little Victor adores the sets theory. Let us remind you that a set is a group of numbers where all numbers are pairwise distinct. Today Victor wants to find a set of integers *S* that has the following properties: - for all *x* the following inequality holds *l*<=≀<=*x*<=≀<=*r*; - 1<=≀<=|*S*|<=≀<=*k*; - lets denote the *i*-th element of the set *S* as *s**i*; value must be as small as possible. Help Victor find the described set.
The first line contains three space-separated integers *l*,<=*r*,<=*k* (1<=≀<=*l*<=≀<=*r*<=≀<=1012;Β 1<=≀<=*k*<=≀<=*min*(106,<=*r*<=-<=*l*<=+<=1)).
Print the minimum possible value of *f*(*S*). Then print the cardinality of set |*S*|. Then print the elements of the set in any order. If there are multiple optimal sets, you can print any of them.
[ "8 15 3\n", "8 30 7\n" ]
[ "1\n2\n10 11\n", "0\n5\n14 9 28 11 16\n" ]
Operation <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b364f2e04c665b78b924ec10666327a4ef4635bc.png" style="max-width: 100.0%;max-height: 100.0%;"/> represents the operation of bitwise exclusive OR. In other words, it is the XOR operation.
[ { "input": "8 15 3", "output": "1\n2\n10 11" }, { "input": "8 30 7", "output": "0\n5\n14 9 28 11 16" }, { "input": "1 1 1", "output": "1\n1\n1 " }, { "input": "3 987654321502 1", "output": "3\n1\n3" }, { "input": "15603259690 63210239992 2", "output": "1\n2\n1...
61
2,867,200
-1
59,497
314
Sereja and Contest
[ "implementation" ]
null
null
During the last Sereja's Codesecrof round the server crashed many times, so the round was decided to be made unrated for some participants. Let's assume that *n* people took part in the contest. Let's assume that the participant who got the first place has rating *a*1, the second place participant has rating *a*2, ..., the *n*-th place participant has rating *a**n*. Then changing the rating on the Codesecrof site is calculated by the formula . After the round was over, the Codesecrof management published the participants' results table. They decided that if for a participant *d**i*<=&lt;<=*k*, then the round can be considered unrated for him. But imagine the management's surprise when they found out that the participants' rating table is dynamic. In other words, when some participant is removed from the rating, he is removed from the results' table and the rating is recalculated according to the new table. And of course, all applications for exclusion from the rating are considered in view of the current table. We know that among all the applications for exclusion from the rating the first application to consider is from the participant with the best rank (the rank with the minimum number), for who *d**i*<=&lt;<=*k*. We also know that the applications for exclusion from rating were submitted by all participants. Now Sereja wonders, what is the number of participants to be excluded from the contest rating, and the numbers of the participants in the original table in the order of their exclusion from the rating. Pay attention to the analysis of the first test case for a better understanding of the statement.
The first line contains two integers *n*, *k* (1<=≀<=*n*<=≀<=2Β·105,<=<=-<=109<=≀<=*k*<=≀<=0). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” ratings of the participants in the initial table.
Print the numbers of participants in the order in which they were removed from the table. Print the initial numbers of the participants, that is, the numbers that the participants had in the initial table.
[ "5 0\n5 3 4 1 2\n", "10 -10\n5 5 1 7 5 1 2 4 9 2\n" ]
[ "2\n3\n4\n", "2\n4\n5\n7\n8\n9\n" ]
Consider the first test sample. 1. Initially the sequence of the contest participants' ratings equals [5, 3, 4, 1, 2]. You can use this sequence to calculate the sequence of rating changes: [0, -9, -13, 8, 14]. According to the problem statement, the application of the participant who won the second place will be considered first.1. As soon as the second place winner is out from the ratings, the participants' rating sequence will equal [5, 4, 1, 2]. By this sequence you can count the new sequence of rating changes: [0, -8, 2, 6]. According to the problem statement, the application of the participant who won the second place will be considered. Initially this participant won third place.1. The new rating sequence equals [5, 1, 2], the new sequence of rating changes equals [0, -1, 1]. The second place participant's application is taken into consideration, initially this participant won the fourth place.1. The new rating sequence equals [5, 2], the new sequence of rating changes equals [0, 0]. No more applications will be considered. Thus, you should print 2, 3, 4.
[ { "input": "5 0\n5 3 4 1 2", "output": "2\n3\n4" }, { "input": "10 -10\n5 5 1 7 5 1 2 4 9 2", "output": "2\n4\n5\n7\n8\n9" } ]
2,000
17,715,200
0
59,507
363
Renting Bikes
[ "binary search", "greedy" ]
null
null
A group of *n* schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them *m* bikes. The renting price is different for different bikes, renting the *j*-th bike costs *p**j* rubles. In total, the boys' shared budget is *a* rubles. Besides, each of them has his own personal money, the *i*-th boy has *b**i* personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible?
The first line of the input contains three integers *n*, *m* and *a* (1<=≀<=*n*,<=*m*<=≀<=105; 0<=≀<=*a*<=≀<=109). The second line contains the sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=104), where *b**i* is the amount of the *i*-th boy's personal money. The third line contains the sequence of integers *p*1,<=*p*2,<=...,<=*p**m* (1<=≀<=*p**j*<=≀<=109), where *p**j* is the price for renting the *j*-th bike.
Print two integers *r* and *s*, where *r* is the maximum number of schoolboys that can rent a bike and *s* is the minimum total personal money needed to rent *r* bikes. If the schoolchildren cannot rent any bikes, then *r*<==<=*s*<==<=0.
[ "2 2 10\n5 5\n7 6\n", "4 5 2\n8 1 1 2\n6 3 7 5 2\n" ]
[ "2 3\n", "3 8\n" ]
In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
[ { "input": "2 2 10\n5 5\n7 6", "output": "2 3" }, { "input": "4 5 2\n8 1 1 2\n6 3 7 5 2", "output": "3 8" }, { "input": "1 1 2\n1\n2", "output": "1 0" }, { "input": "4 1 1\n3 2 3 2\n3", "output": "1 2" }, { "input": "1 4 1\n3\n2 4 5 5", "output": "1 1" }, ...
30
0
0
59,560
733
Drivers Dissatisfaction
[ "data structures", "dsu", "graphs", "trees" ]
null
null
In one kingdom there are *n* cities and *m* two-way roads. Each road connects a pair of cities, and for each road we know the level of drivers dissatisfactionΒ β€” the value *w**i*. For each road we know the value *c**i*Β β€” how many lamziks we should spend to reduce the level of dissatisfaction with this road by one. Thus, to reduce the dissatisfaction with the *i*-th road by *k*, we should spend *k*Β·*c**i* lamziks. And it is allowed for the dissatisfaction to become zero or even negative. In accordance with the king's order, we need to choose *n*<=-<=1 roads and make them the main roads. An important condition must hold: it should be possible to travel from any city to any other by the main roads. The road ministry has a budget of *S* lamziks for the reform. The ministry is going to spend this budget for repair of some roads (to reduce the dissatisfaction with them), and then to choose the *n*<=-<=1 main roads. Help to spend the budget in such a way and then to choose the main roads so that the total dissatisfaction with the main roads will be as small as possible. The dissatisfaction with some roads can become negative. It is not necessary to spend whole budget *S*. It is guaranteed that it is possible to travel from any city to any other using existing roads. Each road in the kingdom is a two-way road.
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=2Β·105, *n*<=-<=1<=≀<=*m*<=≀<=2Β·105)Β β€” the number of cities and the number of roads in the kingdom, respectively. The second line contains *m* integers *w*1,<=*w*2,<=...,<=*w**m* (1<=≀<=*w**i*<=≀<=109), where *w**i* is the drivers dissatisfaction with the *i*-th road. The third line contains *m* integers *c*1,<=*c*2,<=...,<=*c**m* (1<=≀<=*c**i*<=≀<=109), where *c**i* is the cost (in lamziks) of reducing the dissatisfaction with the *i*-th road by one. The next *m* lines contain the description of the roads. The *i*-th of this lines contain a pair of integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*) which mean that the *i*-th road connects cities *a**i* and *b**i*. All roads are two-way oriented so it is possible to move by the *i*-th road from *a**i* to *b**i*, and vice versa. It is allowed that a pair of cities is connected by more than one road. The last line contains one integer *S* (0<=≀<=*S*<=≀<=109)Β β€” the number of lamziks which we can spend for reforms.
In the first line print *K*Β β€” the minimum possible total dissatisfaction with main roads. In each of the next *n*<=-<=1 lines print two integers *x*,<=*v**x*, which mean that the road *x* is among main roads and the road *x*, after the reform, has the level of dissatisfaction *v**x*. Consider that roads are numbered from 1 to *m* in the order as they are given in the input data. The edges can be printed in arbitrary order. If there are several answers, print any of them.
[ "6 9\n1 3 1 1 3 1 2 2 2\n4 1 4 2 2 5 3 1 6\n1 2\n1 3\n2 3\n2 4\n2 5\n3 5\n3 6\n4 5\n5 6\n7\n", "3 3\n9 5 1\n7 7 2\n2 1\n3 1\n3 2\n2\n" ]
[ "0\n1 1\n3 1\n6 1\n7 2\n8 -5\n", "5\n3 0\n2 5\n" ]
none
[ { "input": "6 9\n1 3 1 1 3 1 2 2 2\n4 1 4 2 2 5 3 1 6\n1 2\n1 3\n2 3\n2 4\n2 5\n3 5\n3 6\n4 5\n5 6\n7", "output": "0\n1 1\n3 1\n6 1\n7 2\n8 -5" }, { "input": "3 3\n9 5 1\n7 7 2\n2 1\n3 1\n3 2\n2", "output": "5\n3 0\n2 5" }, { "input": "7 6\n8 10 4 8 4 4\n45 51 13 13 37 26\n2 7\n7 1\n6 3\...
78
0
0
59,604
35
Warehouse
[ "implementation" ]
B. Warehouse
2
64
Once upon a time, when the world was more beautiful, the sun shone brighter, the grass was greener and the sausages tasted better Arlandia was the most powerful country. And its capital was the place where our hero DravDe worked. He couldn’t program or make up problems (in fact, few people saw a computer those days) but he was nevertheless happy. He worked in a warehouse where a magical but non-alcoholic drink Ogudar-Olok was kept. We won’t describe his work in detail and take a better look at a simplified version of the warehouse. The warehouse has one set of shelving. It has *n* shelves, each of which is divided into *m* sections. The shelves are numbered from top to bottom starting from 1 and the sections of each shelf are numbered from left to right also starting from 1. Each section can contain exactly one box of the drink, and try as he might, DravDe can never put a box in a section that already has one. In the course of his work DravDe frequently notices that he has to put a box in a filled section. In that case his solution is simple. DravDe ignores that section and looks at the next one to the right. If it is empty, he puts the box there. Otherwise he keeps looking for the first empty section to the right. If no empty section is found by the end of the shelf, he looks at the shelf which is under it, then the next one, etc. Also each time he looks at a new shelf he starts from the shelf’s beginning. If DravDe still can’t find an empty section for the box, he immediately drinks it all up and throws the empty bottles away not to be caught. After one great party with a lot of Ogudar-Olok drunk DravDe asked you to help him. Unlike him, you can program and therefore modeling the process of counting the boxes in the warehouse will be easy work for you. The process of counting contains two types of query messages: - Β«+1 x y idΒ» (where *x*, *y* are integers, 1<=≀<=*x*<=≀<=*n*, 1<=≀<=*y*<=≀<=*m*, and *id* is a string of lower case Latin letters β€” from 1 to 10 characters long). That query means that the warehouse got a box identified as *id*, which should be put in the section *y* on the shelf *x*. If the section is full, use the rules described above. It is guaranteed that every moment of the process the identifiers of all the boxes in the warehouse are different. You don’t have to answer this query. - Β«-1 idΒ» (where *id* is a string of lower case Latin letters β€” from 1 to 10 characters long). That query means that a box identified as *id* is removed from the warehouse. You have to answer this query (see output format).
The first input line contains integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=30, 1<=≀<=*k*<=≀<=2000) β€” the height, the width of shelving and the amount of the operations in the warehouse that you need to analyze. In the following *k* lines the queries are given in the order of appearance in the format described above.
For each query of the Β«-1 idΒ» type output two numbers in a separate line β€” index of the shelf and index of the section where the box with this identifier lay. If there was no such box in the warehouse when the query was made, output Β«-1 -1Β» without quotes.
[ "2 2 9\n+1 1 1 cola\n+1 1 1 fanta\n+1 1 1 sevenup\n+1 1 1 whitekey\n-1 cola\n-1 fanta\n-1 sevenup\n-1 whitekey\n-1 cola\n", "2 2 8\n+1 1 1 cola\n-1 cola\n+1 1 1 fanta\n-1 fanta\n+1 1 1 sevenup\n-1 sevenup\n+1 1 1 whitekey\n-1 whitekey\n" ]
[ "1 1\n1 2\n2 1\n2 2\n-1 -1\n", "1 1\n1 1\n1 1\n1 1\n" ]
none
[ { "input": "2 2 9\n+1 1 1 cola\n+1 1 1 fanta\n+1 1 1 sevenup\n+1 1 1 whitekey\n-1 cola\n-1 fanta\n-1 sevenup\n-1 whitekey\n-1 cola", "output": "1 1\n1 2\n2 1\n2 2\n-1 -1" }, { "input": "2 2 8\n+1 1 1 cola\n-1 cola\n+1 1 1 fanta\n-1 fanta\n+1 1 1 sevenup\n-1 sevenup\n+1 1 1 whitekey\n-1 whitekey", ...
530
20,787,200
3.712623
59,634
196
Infinite Maze
[ "dfs and similar", "graphs" ]
null
null
We've got a rectangular *n*<=Γ—<=*m*-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze. Now on this plane cell (*x*,<=*y*) is a wall if and only if cell is a wall. In this problem is a remainder of dividing number *a* by number *b*. The little boy stood at some cell on the plane and he wondered whether he can walk infinitely far away from his starting position. From cell (*x*,<=*y*) he can go to one of the following cells: (*x*,<=*y*<=-<=1), (*x*,<=*y*<=+<=1), (*x*<=-<=1,<=*y*) and (*x*<=+<=1,<=*y*), provided that the cell he goes to is not a wall.
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=1500) β€” the height and the width of the maze that the boy used to cyclically tile the plane. Each of the next *n* lines contains *m* characters β€” the description of the labyrinth. Each character is either a "#", that marks a wall, a ".", that marks a passable cell, or an "S", that marks the little boy's starting point. The starting point is a passable cell. It is guaranteed that character "S" occurs exactly once in the input.
Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).
[ "5 4\n##.#\n##S#\n#..#\n#.##\n#..#\n", "5 4\n##.#\n##S#\n#..#\n..#.\n#.##\n" ]
[ "Yes\n", "No\n" ]
In the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up. In the second sample the vertical path is blocked. The path to the left doesn't work, too β€” the next "copy" of the maze traps the boy.
[]
716
18,124,800
-1
59,969
466
Information Graph
[ "dfs and similar", "dsu", "graphs", "trees" ]
null
null
There are *n* employees working in company "X" (let's number them from 1 to *n* for convenience). Initially the employees didn't have any relationships among each other. On each of *m* next days one of the following events took place: - either employee *y* became the boss of employee *x* (at that, employee *x* didn't have a boss before); - or employee *x* gets a packet of documents and signs them; then he gives the packet to his boss. The boss signs the documents and gives them to his boss and so on (the last person to sign the documents sends them to the archive); - or comes a request of type "determine whether employee *x* signs certain documents". Your task is to write a program that will, given the events, answer the queries of the described type. At that, it is guaranteed that throughout the whole working time the company didn't have cyclic dependencies.
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of employees and the number of events. Each of the next *m* lines contains the description of one event (the events are given in the chronological order). The first number of the line determines the type of event *t* (1<=≀<=*t*<=≀<=3). - If *t*<==<=1, then next follow two integers *x* and *y* (1<=≀<=*x*,<=*y*<=≀<=*n*) β€” numbers of the company employees. It is guaranteed that employee *x* doesn't have the boss currently. - If *t*<==<=2, then next follow integer *x* (1<=≀<=*x*<=≀<=*n*) β€” the number of the employee who got a document packet. - If *t*<==<=3, then next follow two integers *x* and *i* (1<=≀<=*x*<=≀<=*n*;Β 1<=≀<=*i*<=≀<=[number of packets that have already been given]) β€” the employee and the number of the document packet for which you need to find out information. The document packets are numbered started from 1 in the chronological order. It is guaranteed that the input has at least one query of the third type.
For each query of the third type print "YES" if the employee signed the document package and "NO" otherwise. Print all the words without the quotes.
[ "4 9\n1 4 3\n2 4\n3 3 1\n1 2 3\n2 2\n3 1 2\n1 3 1\n2 2\n3 1 3\n" ]
[ "YES\nNO\nYES\n" ]
none
[]
31
0
0
60,021
467
Fedor and Essay
[ "dfs and similar", "dp", "graphs", "hashing", "strings" ]
null
null
After you had helped Fedor to find friends in the Β«Call of Soldiers 3Β» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all. Now Fedor is going to change the essay using the synonym dictionary of the English language. Fedor does not want to change the meaning of the essay. So the only change he would do: change a word from essay to one of its synonyms, basing on a replacement rule from the dictionary. Fedor may perform this operation any number of times. As a result, Fedor wants to get an essay which contains as little letters Β«RΒ» (the case doesn't matter) as possible. If there are multiple essays with minimum number of Β«RΒ»s he wants to get the one with minimum length (length of essay is the sum of the lengths of all the words in it). Help Fedor get the required essay. Please note that in this problem the case of letters doesn't matter. For example, if the synonym dictionary says that word cat can be replaced with word DOG, then it is allowed to replace the word Cat with the word doG.
The first line contains a single integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 105 characters. The next line contains a single integer *n* (0<=≀<=*n*<=≀<=105) β€” the number of pairs of words in synonym dictionary. The *i*-th of the next *n* lines contains two space-separated non-empty words *x**i* and *y**i*. They mean that word *x**i* can be replaced with word *y**i* (but not vise versa). It is guaranteed that the total length of all pairs of synonyms doesn't exceed 5Β·105 characters. All the words at input can only consist of uppercase and lowercase letters of the English alphabet.
Print two integers β€” the minimum number of letters Β«RΒ» in an optimal essay and the minimum length of an optimal essay.
[ "3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y\n", "2\nRuruRu fedya\n1\nruruRU fedor\n" ]
[ "2 6\n", "1 10\n" ]
none
[ { "input": "3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y", "output": "2 6" }, { "input": "2\nRuruRu fedya\n1\nruruRU fedor", "output": "1 10" }, { "input": "1\nffff\n1\nffff r", "output": "0 4" }, { "input": "2\nYURA YUrA\n1\nyura fedya", "output": "0 10" }, { "inp...
30
0
0
60,081
679
Bear and Bad Powers of 42
[ "data structures" ]
null
null
Limak, a bear, isn't good at handling queries. So, he asks you to do it. We say that powers of 42 (numbers 1,<=42,<=1764,<=...) are bad. Other numbers are good. You are given a sequence of *n* good integers *t*1,<=*t*2,<=...,<=*t**n*. Your task is to handle *q* queries of three types: 1. 1 iΒ β€” print *t**i* in a separate line. 1. 2 a b xΒ β€” for set *t**i* to *x*. It's guaranteed that *x* is a good number. 1. 3 a b xΒ β€” for increase *t**i* by *x*. After this repeat the process while at least one *t**i* is bad. You can note that after each query all *t**i* are good.
The first line of the input contains two integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=100<=000)Β β€” the size of Limak's sequence and the number of queries, respectively. The second line of the input contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (2<=≀<=*t**i*<=≀<=109)Β β€” initial elements of Limak's sequence. All *t**i* are good. Then, *q* lines follow. The *i*-th of them describes the *i*-th query. The first number in the line is an integer *type**i* (1<=≀<=*type**i*<=≀<=3)Β β€” the type of the query. There is at least one query of the first type, so the output won't be empty. In queries of the second and the third type there is 1<=≀<=*a*<=≀<=*b*<=≀<=*n*. In queries of the second type an integer *x* (2<=≀<=*x*<=≀<=109) is guaranteed to be good. In queries of the third type an integer *x* (1<=≀<=*x*<=≀<=109) may be bad.
For each query of the first type, print the answer in a separate line.
[ "6 12\n40 1700 7 1672 4 1722\n3 2 4 42\n1 2\n1 3\n3 2 6 50\n1 2\n1 4\n1 6\n2 3 4 41\n3 1 5 1\n1 1\n1 3\n1 5\n" ]
[ "1742\n49\n1842\n1814\n1822\n43\n44\n107\n" ]
After a query 3 2 4 42 the sequence is 40, 1742, 49, 1714, 4, 1722. After a query 3 2 6 50 the sequence is 40, 1842, 149, 1814, 104, 1822. After a query 2 3 4 41 the sequence is 40, 1842, 41, 41, 104, 1822. After a query 3 1 5 1 the sequence is 43, 1845, 44, 44, 107, 1822.
[]
0
0
-1
60,134
883
Downloading B++
[ "binary search", "implementation" ]
null
null
Only *T* milliseconds left before the start of well-known online programming contest Codehorses Round 2017. Polycarp needs to download B++ compiler to take part in the contest. The size of the file is *f* bytes. Polycarp's internet tariff allows to download data at the rate of one byte per *t*0 milliseconds. This tariff is already prepaid, and its use does not incur any expense for Polycarp. In addition, the Internet service provider offers two additional packages: - download *a*1 bytes at the rate of one byte per *t*1 milliseconds, paying *p*1 burles for the package; - download *a*2 bytes at the rate of one byte per *t*2 milliseconds, paying *p*2 burles for the package. Polycarp can buy any package many times. When buying a package, its price (*p*1 or *p*2) is prepaid before usage. Once a package is bought it replaces the regular tariff until package data limit is completely used. After a package is consumed Polycarp can immediately buy a new package or switch to the regular tariff without loosing any time. While a package is in use Polycarp can't buy another package or switch back to the regular internet tariff. Find the minimum amount of money Polycarp has to spend to download an *f* bytes file no more than in *T* milliseconds. Note that because of technical reasons Polycarp can download only integer number of bytes using regular tariff and both packages. I.e. in each of three downloading modes the number of downloaded bytes will be integer. It means that Polycarp can't download a byte partially using the regular tariff or/and both packages.
The first line contains three integer numbers *f*, *T* and *t*0 (1<=≀<=*f*,<=*T*,<=*t*0<=≀<=107) β€” size of the file to download (in bytes), maximal time to download the file (in milliseconds) and number of milliseconds to download one byte using the regular internet tariff. The second line contains a description of the first additional package. The line contains three integer numbers *a*1, *t*1 and *p*1 (1<=≀<=*a*1,<=*t*1,<=*p*1<=≀<=107), where *a*1 is maximal sizes of downloaded data (in bytes), *t*1 is time to download one byte (in milliseconds), *p*1 is price of the package (in burles). The third line contains a description of the second additional package. The line contains three integer numbers *a*2, *t*2 and *p*2 (1<=≀<=*a*2,<=*t*2,<=*p*2<=≀<=107), where *a*2 is maximal sizes of downloaded data (in bytes), *t*2 is time to download one byte (in milliseconds), *p*2 is price of the package (in burles). Polycarp can buy any package many times. Once package is bought it replaces the regular tariff until package data limit is completely used. While a package is in use Polycarp can't buy another package or switch back to the regular internet tariff.
Print the minimum amount of money that Polycarp needs to pay to download B++ compiler no more than in *T* milliseconds. If there is no solution, print the only integer -1.
[ "120 964 20\n26 8 8\n13 10 4\n", "10 200 20\n1 1 1\n2 2 3\n", "8 81 11\n4 10 16\n3 10 12\n", "8 79 11\n4 10 16\n3 10 12\n" ]
[ "40\n", "0\n", "28\n", "-1\n" ]
In the first example Polycarp has to buy the first additional package 5 times and do not buy the second additional package. He downloads 120 bytes (of total 26Β·5 = 130 bytes) in 120Β·8 = 960 milliseconds (960 ≀ 964). He spends 8Β·5 = 40 burles on it. In the second example Polycarp has enough time to download 10 bytes. It takes 10Β·20 = 200 milliseconds which equals to upper constraint on download time. In the third example Polycarp has to buy one first additional package and one second additional package. In the fourth example Polycarp has no way to download the file on time.
[ { "input": "120 964 20\n26 8 8\n13 10 4", "output": "40" }, { "input": "10 200 20\n1 1 1\n2 2 3", "output": "0" }, { "input": "8 81 11\n4 10 16\n3 10 12", "output": "28" }, { "input": "8 79 11\n4 10 16\n3 10 12", "output": "-1" }, { "input": "62 10000 209\n95 106 ...
46
0
0
60,153
723
Text Document Analysis
[ "expression parsing", "implementation", "strings" ]
null
null
Modern text editors usually show some information regarding the document being edited. For example, the number of words, the number of pages, or the number of characters. In this problem you should implement the similar functionality. You are given a string which only consists of: - uppercase and lowercase English letters, - underscore symbols (they are used as separators), - parentheses (both opening and closing). It is guaranteed that each opening parenthesis has a succeeding closing parenthesis. Similarly, each closing parentheses has a preceding opening parentheses matching it. For each pair of matching parentheses there are no other parenthesis between them. In other words, each parenthesis in the string belongs to a matching "opening-closing" pair, and such pairs can't be nested. For example, the following string is valid: "_Hello_Vasya(and_Petya)__bye_(and_OK)". Word is a maximal sequence of consecutive letters, i.e. such sequence that the first character to the left and the first character to the right of it is an underscore, a parenthesis, or it just does not exist. For example, the string above consists of seven words: "Hello", "Vasya", "and", "Petya", "bye", "and" and "OK". Write a program that finds: - the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses), - the number of words inside the parentheses (print 0, if there is no word inside the parentheses).
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=255)Β β€” the length of the given string. The second line contains the string consisting of only lowercase and uppercase English letters, parentheses and underscore symbols.
Print two space-separated integers: - the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses), - the number of words inside the parentheses (print 0, if there is no word inside the parentheses).
[ "37\n_Hello_Vasya(and_Petya)__bye_(and_OK)\n", "37\n_a_(_b___c)__de_f(g_)__h__i(j_k_l)m__\n", "27\n(LoooonG)__shOrt__(LoooonG)\n", "5\n(___)\n" ]
[ "5 4", "2 6", "5 2", "0 0\n" ]
In the first sample, the words "Hello", "Vasya" and "bye" are outside any of the parentheses, and the words "and", "Petya", "and" and "OK" are inside. Note, that the word "and" is given twice and you should count it twice in the answer.
[ { "input": "37\n_Hello_Vasya(and_Petya)__bye_(and_OK)", "output": "5 4" }, { "input": "37\n_a_(_b___c)__de_f(g_)__h__i(j_k_l)m__", "output": "2 6" }, { "input": "27\n(LoooonG)__shOrt__(LoooonG)", "output": "5 2" }, { "input": "5\n(___)", "output": "0 0" }, { "inpu...
46
0
3
60,163
567
President and Roads
[ "dfs and similar", "graphs", "hashing", "shortest paths" ]
null
null
Berland has *n* cities, the capital is located in city *s*, and the historic home town of the President is in city *t* (*s*<=β‰ <=*t*). The cities are connected by one-way roads, the travel time for each of the road is a positive integer. Once a year the President visited his historic home town *t*, for which his motorcade passes along some path from *s* to *t* (he always returns on a personal plane). Since the president is a very busy man, he always chooses the path from *s* to *t*, along which he will travel the fastest. The ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer.
The first lines contain four integers *n*, *m*, *s* and *t* (2<=≀<=*n*<=≀<=105;Β 1<=≀<=*m*<=≀<=105;Β 1<=≀<=*s*,<=*t*<=≀<=*n*) β€” the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town (*s*<=β‰ <=*t*). Next *m* lines contain the roads. Each road is given as a group of three integers *a**i*,<=*b**i*,<=*l**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*;Β *a**i*<=β‰ <=*b**i*;Β 1<=≀<=*l**i*<=≀<=106) β€” the cities that are connected by the *i*-th road and the time needed to ride along it. The road is directed from city *a**i* to city *b**i*. The cities are numbered from 1 to *n*. Each pair of cities can have multiple roads between them. It is guaranteed that there is a path from *s* to *t* along the roads.
Print *m* lines. The *i*-th line should contain information about the *i*-th road (the roads are numbered in the order of appearance in the input). If the president will definitely ride along it during his travels, the line must contain a single word "YES" (without the quotes). Otherwise, if the *i*-th road can be repaired so that the travel time on it remains positive and then president will definitely ride along it, print space-separated word "CAN" (without the quotes), and the minimum cost of repairing. If we can't make the road be such that president will definitely ride along it, print "NO" (without the quotes).
[ "6 7 1 6\n1 2 2\n1 3 10\n2 3 7\n2 4 8\n3 5 3\n4 5 2\n5 6 1\n", "3 3 1 3\n1 2 10\n2 3 10\n1 3 100\n", "2 2 1 2\n1 2 1\n1 2 2\n" ]
[ "YES\nCAN 2\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nYES\n", "YES\nYES\nCAN 81\n", "YES\nNO\n" ]
The cost of repairing the road is the difference between the time needed to ride along it before and after the repairing. In the first sample president initially may choose one of the two following ways for a ride: 1 → 2 → 4 → 5 → 6 or 1 → 2 → 3 → 5 → 6.
[ { "input": "6 7 1 6\n1 2 2\n1 3 10\n2 3 7\n2 4 8\n3 5 3\n4 5 2\n5 6 1", "output": "YES\nCAN 2\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nYES" }, { "input": "3 3 1 3\n1 2 10\n2 3 10\n1 3 100", "output": "YES\nYES\nCAN 81" }, { "input": "2 2 1 2\n1 2 1\n1 2 2", "output": "YES\nNO" }, { "inpu...
46
0
0
60,189
959
Mahmoud and Ehab and the wrong algorithm
[ "constructive algorithms", "trees" ]
null
null
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is: Given an undirected tree consisting of *n* nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (*u*,<=*v*) that belongs to the tree, either *u* is in the set, or *v* is in the set, or both are in the set. Mahmoud has found the following algorithm: - Root the tree at node 1. - Count the number of nodes at an even depth. Let it be *evenCnt*. - Count the number of nodes at an odd depth. Let it be *oddCnt*. - The answer is the minimum between *evenCnt* and *oddCnt*. The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0. Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of *n* nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
The only line contains an integer *n* (2<=≀<=*n*<=≀<=105), the number of nodes in the desired trees.
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only. If the answer for a section exists, it should contain *n*<=-<=1 lines, each containing 2 space-separated integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*n*), which means that there's an undirected edge between node *u* and node *v*. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict. If there are multiple answers, you can print any of them.
[ "2\n", "8\n" ]
[ "-1\n1 2\n", "1 2\n1 3\n2 4\n2 5\n3 6\n4 7\n4 8\n1 2\n1 3\n2 4\n2 5\n2 6\n3 7\n6 8" ]
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed  - 1 in the first section, but notice that we printed this tree in the second section. In the second sample: In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <img class="tex-graphics" src="https://espresso.codeforces.com/fb4efe76f0a74395fe77f22da2dbd2a3340f8b2a.png" style="max-width: 100.0%;max-height: 100.0%;"/> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <img class="tex-graphics" src="https://espresso.codeforces.com/700db299c05ae94849f44b93e8240d375a0fe05f.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "2", "output": "-1\n1 2" }, { "input": "8", "output": "1 2\n1 3\n1 4\n2 5\n2 6\n1 7\n1 8\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8" }, { "input": "99", "output": "1 2\n1 3\n1 4\n2 5\n2 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\...
46
0
0
60,216
97
Winning Strategy
[ "binary search", "graphs", "math", "shortest paths" ]
C. Winning Strategy
2
256
One university has just found out about a sport programming contest called ACM ICPC v2.0. This contest doesn't differ much from the well-known ACM ICPC, for example, the participants are not allowed to take part in the finals more than two times. However, there is one notable difference: the teams in the contest should consist of exactly *n* participants. Having taken part in several ACM ICPC v2.0 finals and having not won any medals, the students and the university governors realized that it's high time they changed something about the preparation process. Specifically, as the first innovation it was decided to change the teams' formation process. Having spent considerable amount of time on studying the statistics of other universities' performance, they managed to receive some interesting information: the dependence between the probability of winning a medal and the number of team members that participated in the finals in the past. More formally, we know *n*<=+<=1 real numbers *p*0<=≀<=*p*1<=≀<=...<=≀<=*p**n*, where *p**i* is the probability of getting a medal on the finals if the team has *i* participants of previous finals, and other *n*<=-<=*i* participants arrived to the finals for the first time. Despite such useful data, the university governors are unable to determine such team forming tactics that would provide the maximum probability of winning a medal at ACM ICPC v2.0 finals on average (we are supposed to want to provide such result to the far future and we are also supposed to have an endless supply of students). And how about you, can you offer such optimal tactic? At the first stage the university governors want to know the value of maximum average probability. More formally, suppose that the university sends a team to the *k*-th world finals. The team has *a**k* participants of previous finals (0<=≀<=*a**k*<=≀<=*n*). Since each person can participate in the finals no more than twice, the following condition must be true: . Your task is to choose sequence so that the limit Ξ¨ exists and it's value is maximal: As is an infinite sequence, you should only print the maximum value of the Ξ¨ limit.
The first line contains an integer *n* (3<=≀<=*n*<=≀<=100), *n* is the number of team participants. The second line contains *n*<=+<=1 real numbers with no more than 6 digits after decimal point *p**i* (0<=≀<=*i*<=≀<=*n*,<=0<=≀<=*p**i*<=≀<=1) β€” the probability of that the team will win a medal if it contains *i* participants who has already been on the finals. Also the condition *p**i*<=≀<=*p**i*<=+<=1 should be fulfilled for all 0<=≀<=*i*<=≀<=*n*<=-<=1.
Print the only real number β€” the expected average number of medals won per year if the optimal strategy is used. The result may have absolute or relative error 10<=-<=6.
[ "3\n0.115590 0.384031 0.443128 0.562356\n", "3\n1 1 1 1\n" ]
[ "0.4286122500\n", "0.9999999999\n" ]
In the second test, no matter what participants the team contains, it is doomed to be successful.
[ { "input": "3\n0.115590 0.384031 0.443128 0.562356", "output": "0.4286122500" }, { "input": "3\n1 1 1 1", "output": "0.9999999999" }, { "input": "10\n0.054228 0.284367 0.307914 0.319911 0.325274 0.336089 0.549308 0.554288 0.814348 0.817238 0.861607", "output": "0.5872132857" }, {...
92
0
0
60,802