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
815
Karen and Supermarket
[ "brute force", "dp", "trees" ]
null
null
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to *b* dollars. The supermarket sells *n* goods. The *i*-th good can be bought for *c**i* dollars. Of course, each good can only be bought once. Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given *n* coupons. If Karen purchases the *i*-th good, she can use the *i*-th coupon to decrease its price by *d**i*. Of course, a coupon cannot be used without buying the corresponding good. There is, however, a constraint with the coupons. For all *i*<=β‰₯<=2, in order to use the *i*-th coupon, Karen must also use the *x**i*-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon). Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget *b*?
The first line of input contains two integers *n* and *b* (1<=≀<=*n*<=≀<=5000, 1<=≀<=*b*<=≀<=109), the number of goods in the store and the amount of money Karen has, respectively. The next *n* lines describe the items. Specifically: - The *i*-th line among these starts with two integers, *c**i* and *d**i* (1<=≀<=*d**i*<=&lt;<=*c**i*<=≀<=109), the price of the *i*-th good and the discount when using the coupon for the *i*-th good, respectively. - If *i*<=β‰₯<=2, this is followed by another integer, *x**i* (1<=≀<=*x**i*<=&lt;<=*i*), denoting that the *x**i*-th coupon must also be used before this coupon can be used.
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
[ "6 16\n10 9\n10 5 1\n12 2 1\n20 18 3\n10 2 3\n2 1 5\n", "5 10\n3 1\n3 1 1\n3 1 2\n3 1 3\n3 1 4\n" ]
[ "4\n", "5\n" ]
In the first test case, Karen can purchase the following 4 items: - Use the first coupon to buy the first item for 10 - 9 = 1 dollar. - Use the third coupon to buy the third item for 12 - 2 = 10 dollars. - Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars. - Buy the sixth item for 2 dollars. The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here. In the second test case, Karen has enough money to use all the coupons and purchase everything.
[ { "input": "6 16\n10 9\n10 5 1\n12 2 1\n20 18 3\n10 2 3\n2 1 5", "output": "4" }, { "input": "5 10\n3 1\n3 1 1\n3 1 2\n3 1 3\n3 1 4", "output": "5" }, { "input": "13 30\n6 4\n25 5 1\n7 1 2\n9 4 2\n10 2 1\n12 3 1\n5 2 3\n10 9 6\n2 1 1\n5 3 9\n10 2 10\n10 9 6\n3 2 11", "output": "9" ...
140
1,843,200
0
12,634
500
New Year Book Reading
[ "constructive algorithms", "greedy", "implementation", "math" ]
null
null
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has *n* books numbered by integers from 1 to *n*. The weight of the *i*-th (1<=≀<=*i*<=≀<=*n*) book is *w**i*. As Jaehyun's house is not large enough to have a bookshelf, he keeps the *n* books by stacking them vertically. When he wants to read a certain book *x*, he follows the steps described below. 1. He lifts all the books above book *x*. 1. He pushes book *x* out of the stack. 1. He puts down the lifted books without changing their order. 1. After reading book *x*, he puts book *x* on the top of the stack. He decided to read books for *m* days. In the *j*-th (1<=≀<=*j*<=≀<=*m*) day, he will read the book that is numbered with integer *b**j* (1<=≀<=*b**j*<=≀<=*n*). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times. After making this plan, he realized that the total weight of books he should lift during *m* days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
The first line contains two space-separated integers *n* (2<=≀<=*n*<=≀<=500) and *m* (1<=≀<=*m*<=≀<=1000) β€” the number of books, and the number of days for which Jaehyun would read books. The second line contains *n* space-separated integers *w*1,<=*w*2,<=...,<=*w**n* (1<=≀<=*w**i*<=≀<=100) β€” the weight of each book. The third line contains *m* space separated integers *b*1,<=*b*2,<=...,<=*b**m* (1<=≀<=*b**j*<=≀<=*n*) β€” the order of books that he would read. Note that he can read the same book more than once.
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
[ "3 5\n1 2 3\n1 3 2 3 1\n" ]
[ "12\n" ]
Here's a picture depicting the example. Each vertical column presents the stacked books.
[ { "input": "3 5\n1 2 3\n1 3 2 3 1", "output": "12" }, { "input": "3 3\n10 20 30\n1 2 3", "output": "40" }, { "input": "2 2\n10 12\n2 1", "output": "12" }, { "input": "10 10\n61 59 97 16 2 94 57 48 91 93\n2 8 6 5 3 1 3 4 9 10", "output": "2137" }, { "input": "50 50...
62
0
0
12,672
603
Moodular Arithmetic
[ "combinatorics", "dfs and similar", "dsu", "math", "number theory" ]
null
null
As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers *k* and *p*, where *p* is an odd prime number, the functional equation states that for some function . (This equation should hold for any integer *x* in the range 0 to *p*<=-<=1, inclusive.) It turns out that *f* can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions *f* that satisfy this equation. Since the answer may be very large, you should print your result modulo 109<=+<=7.
The input consists of two space-separated integers *p* and *k* (3<=≀<=*p*<=≀<=1<=000<=000, 0<=≀<=*k*<=≀<=*p*<=-<=1) on a single line. It is guaranteed that *p* is an odd prime number.
Print a single integer, the number of distinct functions *f* modulo 109<=+<=7.
[ "3 2\n", "5 4\n" ]
[ "3\n", "25\n" ]
In the first sample, *p* = 3 and *k* = 2. The following functions work: 1. *f*(0) = 0, *f*(1) = 1, *f*(2) = 2. 1. *f*(0) = 0, *f*(1) = 2, *f*(2) = 1. 1. *f*(0) = *f*(1) = *f*(2) = 0.
[ { "input": "3 2", "output": "3" }, { "input": "5 4", "output": "25" }, { "input": "7 2", "output": "49" }, { "input": "7 6", "output": "343" }, { "input": "10007 25", "output": "100140049" }, { "input": "40037 4", "output": "602961362" }, { ...
46
0
-1
12,683
355
Vasya and Public Transport
[ "greedy", "implementation" ]
null
null
Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has *n* buses and *m* trolleys, the buses are numbered by integers from 1 to *n*, the trolleys are numbered by integers from 1 to *m*. Public transport is not free. There are 4 types of tickets: 1. A ticket for one ride on some bus or trolley. It costs *c*1 burles; 1. A ticket for an unlimited number of rides on some bus or on some trolley. It costs *c*2 burles; 1. A ticket for an unlimited number of rides on all buses or all trolleys. It costs *c*3 burles; 1. A ticket for an unlimited number of rides on all buses and trolleys. It costs *c*4 burles. Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets.
The first line contains four integers *c*1,<=*c*2,<=*c*3,<=*c*4 (1<=≀<=*c*1,<=*c*2,<=*c*3,<=*c*4<=≀<=1000) β€” the costs of the tickets. The second line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=1000) β€” the number of buses and trolleys Vasya is going to use. The third line contains *n* integers *a**i* (0<=≀<=*a**i*<=≀<=1000) β€” the number of times Vasya is going to use the bus number *i*. The fourth line contains *m* integers *b**i* (0<=≀<=*b**i*<=≀<=1000) β€” the number of times Vasya is going to use the trolley number *i*.
Print a single number β€” the minimum sum of burles Vasya will have to spend on the tickets.
[ "1 3 7 19\n2 3\n2 5\n4 4 4\n", "4 3 2 1\n1 3\n798\n1 2 3\n", "100 100 8 100\n3 5\n7 94 12\n100 1 47 0 42\n" ]
[ "12\n", "1\n", "16\n" ]
In the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2Β·1) + 3 + 7 = 12 burles. In the second sample the profitable strategy is to buy one ticket of the fourth type. In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys.
[ { "input": "1 3 7 19\n2 3\n2 5\n4 4 4", "output": "12" }, { "input": "4 3 2 1\n1 3\n798\n1 2 3", "output": "1" }, { "input": "100 100 8 100\n3 5\n7 94 12\n100 1 47 0 42", "output": "16" }, { "input": "3 103 945 1000\n7 9\n34 35 34 35 34 35 34\n0 0 0 0 0 0 0 0 0", "output"...
46
0
3
12,708
817
Choosing The Commander
[ "bitmasks", "data structures", "trees" ]
null
null
As you might remember from the previous round, Vova is currently playing a strategic game known as Rage of Empires. Vova managed to build a large army, but forgot about the main person in the army - the commander. So he tries to hire a commander, and he wants to choose the person who will be respected by warriors. Each warrior is represented by his personality β€” an integer number *p**i*. Each commander has two characteristics β€” his personality *p**j* and leadership *l**j* (both are integer numbers). Warrior *i* respects commander *j* only if ( is the bitwise excluding OR of *x* and *y*). Initially Vova's army is empty. There are three different types of events that can happen with the army: - 1Β *p**i* β€” one warrior with personality *p**i* joins Vova's army; - 2Β *p**i* β€” one warrior with personality *p**i* leaves Vova's army; - 3Β *p**i*Β *l**i* β€” Vova tries to hire a commander with personality *p**i* and leadership *l**i*. For each event of the third type Vova wants to know how many warriors (counting only those who joined the army and haven't left yet) respect the commander he tries to hire.
The first line contains one integer *q* (1<=≀<=*q*<=≀<=100000) β€” the number of events. Then *q* lines follow. Each line describes the event: - 1Β *p**i* (1<=≀<=*p**i*<=≀<=108) β€” one warrior with personality *p**i* joins Vova's army; - 2Β *p**i* (1<=≀<=*p**i*<=≀<=108) β€” one warrior with personality *p**i* leaves Vova's army (it is guaranteed that there is at least one such warrior in Vova's army by this moment); - 3Β *p**i*Β *l**i* (1<=≀<=*p**i*,<=*l**i*<=≀<=108) β€” Vova tries to hire a commander with personality *p**i* and leadership *l**i*. There is at least one event of this type.
For each event of the third type print one integer β€” the number of warriors who respect the commander Vova tries to hire in the event.
[ "5\n1 3\n1 4\n3 6 3\n2 4\n3 6 3\n" ]
[ "1\n0\n" ]
In the example the army consists of two warriors with personalities 3 and 4 after first two events. Then Vova tries to hire a commander with personality 6 and leadership 3, and only one warrior respects him (<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3d8e4cd0a5c3c0cdc8b35097f3dc7317604240a6.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and 2 &lt; 3, but <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4e3eeab99fa495ecdcd103c68de47dd72943016f.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and 5 β‰₯ 3). Then warrior with personality 4 leaves, and when Vova tries to hire that commander again, there are no warriors who respect him.
[ { "input": "5\n1 3\n1 4\n3 6 3\n2 4\n3 6 3", "output": "1\n0" } ]
30
0
0
12,726
958
Encryption (medium)
[ "dp" ]
null
null
Heidi has now broken the first level of encryption of the Death Star plans, and is staring at the screen presenting her with the description of the next code she has to enter. It looks surprisingly similar to the first one – seems like the Empire engineers were quite lazy... Heidi is once again given a sequence *A*, but now she is also given two integers *k* and *p*. She needs to find out what the encryption key *S* is. Let *X* be a sequence of integers, and *p* a positive integer. We define the score of *X* to be the sum of the elements of *X* modulo *p*. Heidi is given a sequence *A* that consists of *N* integers, and also given integers *k* and *p*. Her goal is to split *A* into *k* part such that: - Each part contains at least 1 element of *A*, and each part consists of contiguous elements of *A*. - No two parts overlap. - The total sum *S* of the scores of those parts is maximized. Output the sum *S* – the encryption code.
The first line of the input contains three space-separated integer *N*, *k* and *p* (*k*<=≀<=*N*<=≀<=20<=000, 2<=≀<=*k*<=≀<=50, 2<=≀<=*p*<=≀<=100) – the number of elements in *A*, the number of parts *A* should be split into, and the modulo for computing scores, respectively. The second line contains *N* space-separated integers that are the elements of *A*. Each integer is from the interval [1,<=1<=000<=000].
Output the number *S* as described in the problem statement.
[ "4 3 10\n3 4 7 2\n", "10 5 12\n16 3 24 13 9 8 7 5 12 12\n" ]
[ "16\n", "37\n" ]
In the first example, if the input sequence is split as (3, 4), (7), (2), the total score would be <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5a7944578a08885d55a30192c8f0b817cad2ac96.png" style="max-width: 100.0%;max-height: 100.0%;"/>. It is easy to see that this score is maximum. In the second example, one possible way to obtain score 37 is to make the following split: (16, 3, 24), (13, 9), (8), (7), (5, 12, 12).
[]
0
0
-1
12,729
68
Energy exchange
[ "binary search" ]
B. Energy exchange
2
256
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the *i*-th accumulator has *a**i* units of energy. Energy can be transferred from one accumulator to the other. Every time *x* units of energy are transferred (*x* is not necessarily an integer) *k* percent of it is lost. That is, if *x* units were transferred from one accumulator to the other, amount of energy in the first one decreased by *x* units and in other increased by units. Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
First line of the input contains two integers *n* and *k* (1<=≀<=*n*<=≀<=10000,<=0<=≀<=*k*<=≀<=99) β€” number of accumulators and the percent of energy that is lost during transfers. Next line contains *n* integers *a*1,<=*a*2,<=... ,<=*a**n* β€” amounts of energy in the first, second, .., *n*-th accumulator respectively (0<=≀<=*a**i*<=≀<=1000,<=1<=≀<=*i*<=≀<=*n*).
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy. The absolute or relative error in the answer should not exceed 10<=-<=6.
[ "3 50\n4 2 1\n", "2 90\n1 11\n" ]
[ "2.000000000\n", "1.909090909\n" ]
none
[ { "input": "3 50\n4 2 1", "output": "2.000000000" }, { "input": "2 90\n1 11", "output": "1.909090909" }, { "input": "5 26\n42 65 23 43 64", "output": "45.415178571" }, { "input": "5 45\n964 515 454 623 594", "output": "594.109756098" }, { "input": "1 20\n784", ...
374
716,800
3.905165
12,785
590
Chip 'n Dale Rescue Rangers
[ "binary search", "geometry", "math" ]
null
null
A team of furry rescue rangers was sitting idle in their hollow tree when suddenly they received a signal of distress. In a few moments they were ready, and the dirigible of the rescue chipmunks hit the road. We assume that the action takes place on a Cartesian plane. The headquarters of the rescuers is located at point (*x*1,<=*y*1), and the distress signal came from the point (*x*2,<=*y*2). Due to Gadget's engineering talent, the rescuers' dirigible can instantly change its current velocity and direction of movement at any moment and as many times as needed. The only limitation is: the speed of the aircraft relative to the air can not exceed meters per second. Of course, Gadget is a true rescuer and wants to reach the destination as soon as possible. The matter is complicated by the fact that the wind is blowing in the air and it affects the movement of the dirigible. According to the weather forecast, the wind will be defined by the vector (*v**x*,<=*v**y*) for the nearest *t* seconds, and then will change to (*w**x*,<=*w**y*). These vectors give both the direction and velocity of the wind. Formally, if a dirigible is located at the point (*x*,<=*y*), while its own velocity relative to the air is equal to zero and the wind (*u**x*,<=*u**y*) is blowing, then after seconds the new position of the dirigible will be . Gadget is busy piloting the aircraft, so she asked Chip to calculate how long will it take them to reach the destination if they fly optimally. He coped with the task easily, but Dale is convinced that Chip has given the random value, aiming only not to lose the face in front of Gadget. Dale has asked you to find the right answer. It is guaranteed that the speed of the wind at any moment of time is strictly less than the maximum possible speed of the airship relative to the air.
The first line of the input contains four integers *x*1, *y*1, *x*2, *y*2 (|*x*1|,<=<=|*y*1|,<=<=|*x*2|,<=<=|*y*2|<=≀<=10<=000)Β β€” the coordinates of the rescuers' headquarters and the point, where signal of the distress came from, respectively. The second line contains two integers and *t* (0<=&lt;<=*v*,<=*t*<=≀<=1000), which are denoting the maximum speed of the chipmunk dirigible relative to the air and the moment of time when the wind changes according to the weather forecast, respectively. Next follow one per line two pairs of integer (*v**x*,<=*v**y*) and (*w**x*,<=*w**y*), describing the wind for the first *t* seconds and the wind that will blow at all the remaining time, respectively. It is guaranteed that and .
Print a single real valueΒ β€” the minimum time the rescuers need to get to point (*x*2,<=*y*2). You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "0 0 5 5\n3 2\n-1 -1\n-1 0\n", "0 0 0 1000\n100 1000\n-50 0\n50 0\n" ]
[ "3.729935587093555327\n", "11.547005383792516398\n" ]
none
[ { "input": "0 0 5 5\n3 2\n-1 -1\n-1 0", "output": "3.729935587093555327" }, { "input": "0 0 0 1000\n100 1000\n-50 0\n50 0", "output": "11.547005383792516398" }, { "input": "0 0 0 1000\n100 5\n0 -50\n0 50", "output": "10" }, { "input": "0 1000 0 0\n50 10\n-49 0\n49 0", "ou...
31
0
0
12,797
385
Bear in the Field
[ "math", "matrices" ]
null
null
Our bear's forest has a checkered field. The checkered field is an *n*<=Γ—<=*n* table, the rows are numbered from 1 to *n* from top to bottom, the columns are numbered from 1 to *n* from left to right. Let's denote a cell of the field on the intersection of row *x* and column *y* by record (*x*,<=*y*). Each cell of the field contains growing raspberry, at that, the cell (*x*,<=*y*) of the field contains *x*<=+<=*y* raspberry bushes. The bear came out to walk across the field. At the beginning of the walk his speed is (*dx*,<=*dy*). Then the bear spends exactly *t* seconds on the field. Each second the following takes place: - Let's suppose that at the current moment the bear is in cell (*x*,<=*y*). - First the bear eats the raspberry from all the bushes he has in the current cell. After the bear eats the raspberry from *k* bushes, he increases each component of his speed by *k*. In other words, if before eating the *k* bushes of raspberry his speed was (*dx*,<=*dy*), then after eating the berry his speed equals (*dx*<=+<=*k*,<=*dy*<=+<=*k*). - Let's denote the current speed of the bear (*dx*,<=*dy*) (it was increased after the previous step). Then the bear moves from cell (*x*,<=*y*) to cell (((*x*<=+<=*dx*<=-<=1)Β *mod*Β *n*)<=+<=1,<=((*y*<=+<=*dy*<=-<=1)Β *mod*Β *n*)<=+<=1). - Then one additional raspberry bush grows in each cell of the field. You task is to predict the bear's actions. Find the cell he ends up in if he starts from cell (*sx*,<=*sy*). Assume that each bush has infinitely much raspberry and the bear will never eat all of it.
The first line of the input contains six space-separated integers: *n*, *sx*, *sy*, *dx*, *dy*, *t* (1<=≀<=*n*<=≀<=109;Β 1<=≀<=*sx*,<=*sy*<=≀<=*n*;Β <=-<=100<=≀<=*dx*,<=*dy*<=≀<=100;Β 0<=≀<=*t*<=≀<=1018).
Print two integers β€” the coordinates of the cell the bear will end up in after *t* seconds.
[ "5 1 2 0 1 2\n", "1 1 1 -1 -1 2\n" ]
[ "3 1", "1 1" ]
Operation *a*Β *mod*Β *b* means taking the remainder after dividing *a* by *b*. Note that the result of the operation is always non-negative. For example, ( - 1)Β *mod*Β 3 = 2. In the first sample before the first move the speed vector will equal (3,4) and the bear will get to cell (4,1). Before the second move the speed vector will equal (9,10) and he bear will get to cell (3,1). Don't forget that at the second move, the number of berry bushes increased by 1. In the second sample before the first move the speed vector will equal (1,1) and the bear will get to cell (1,1). Before the second move, the speed vector will equal (4,4) and the bear will get to cell (1,1). Don't forget that at the second move, the number of berry bushes increased by 1.
[ { "input": "5 1 2 0 1 2", "output": "3 1" }, { "input": "1 1 1 -1 -1 2", "output": "1 1" }, { "input": "1 1 1 1 1 0", "output": "1 1" }, { "input": "2 2 1 -2 -2 5", "output": "1 2" }, { "input": "1000000000 1 1 1 1 1000000000000000000", "output": "168318977 16...
218
5,734,400
3
12,859
0
none
[ "none" ]
null
null
Firecrackers scare Nian the monster, but they're wayyyyy too noisy! Maybe fireworks make a nice complement. Little Tommy is watching a firework show. As circular shapes spread across the sky, a splendid view unfolds on the night of Lunar New Year's eve. A wonder strikes Tommy. How many regions are formed by the circles on the sky? We consider the sky as a flat plane. A region is a connected part of the plane with positive area, whose bound consists of parts of bounds of the circles and is a curve or several curves without self-intersections, and that does not contain any curve other than its boundaries. Note that exactly one of the regions extends infinitely.
The first line of input contains one integer *n* (1<=≀<=*n*<=≀<=3), denoting the number of circles. The following *n* lines each contains three space-separated integers *x*, *y* and *r* (<=-<=10<=≀<=*x*,<=*y*<=≀<=10, 1<=≀<=*r*<=≀<=10), describing a circle whose center is (*x*,<=*y*) and the radius is *r*. No two circles have the same *x*, *y* and *r* at the same time.
Print a single integerΒ β€” the number of regions on the plane.
[ "3\n0 0 1\n2 0 1\n4 0 1\n", "3\n0 0 2\n3 0 2\n6 0 2\n", "3\n0 0 2\n2 0 2\n1 1 2\n" ]
[ "4\n", "6\n", "8\n" ]
For the first example, For the second example, For the third example,
[ { "input": "3\n0 0 1\n2 0 1\n4 0 1", "output": "4" }, { "input": "3\n0 0 2\n3 0 2\n6 0 2", "output": "6" }, { "input": "3\n0 0 2\n2 0 2\n1 1 2", "output": "8" }, { "input": "1\n0 0 10", "output": "2" }, { "input": "2\n-10 10 1\n10 -10 1", "output": "3" }, ...
202
22,528,000
-1
12,866
156
Suspects
[ "constructive algorithms", "data structures", "implementation" ]
null
null
As Sherlock Holmes was investigating a crime, he identified *n* suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to *n*. After that, he asked each one: "Which one committed the crime?". Suspect number *i* answered either "The crime was committed by suspect number *a**i*", or "Suspect number *a**i* didn't commit the crime". Also, the suspect could say so about himself (*a**i*<==<=*i*). Sherlock Holmes understood for sure that exactly *m* answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth?
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=105,<=0<=≀<=*m*<=≀<=*n*) β€” the total number of suspects and the number of suspects who told the truth. Next *n* lines contain the suspects' answers. The *i*-th line contains either "+*a**i*" (without the quotes), if the suspect number *i* says that the crime was committed by suspect number *a**i*, or "-*a**i*" (without the quotes), if the suspect number *i* says that the suspect number *a**i* didn't commit the crime (*a**i* is an integer, 1<=≀<=*a**i*<=≀<=*n*). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly *m* people told the truth.
Print *n* lines. Line number *i* should contain "Truth" if suspect number *i* has told the truth for sure. Print "Lie" if the suspect number *i* lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime.
[ "1 1\n+1\n", "3 2\n-1\n-2\n-3\n", "4 1\n+2\n-3\n+4\n-1\n" ]
[ "Truth\n", "Not defined\nNot defined\nNot defined\n", "Lie\nNot defined\nLie\nNot defined\n" ]
The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
[ { "input": "1 1\n+1", "output": "Truth" }, { "input": "3 2\n-1\n-2\n-3", "output": "Not defined\nNot defined\nNot defined" }, { "input": "4 1\n+2\n-3\n+4\n-1", "output": "Lie\nNot defined\nLie\nNot defined" }, { "input": "1 0\n-1", "output": "Lie" }, { "input": "2...
186
0
0
12,883
843
Dynamic Shortest Path
[ "graphs", "shortest paths" ]
null
null
You are given a weighted directed graph, consisting of *n* vertices and *m* edges. You should answer *q* queries of two types: - 1 vΒ β€” find the length of shortest path from vertex 1 to vertex *v*. - 2 c *l*1 *l*2 ... *l**c*Β β€” add 1 to weights of edges with indices *l*1,<=*l*2,<=...,<=*l**c*.
The first line of input data contains integers *n*, *m*, *q* (1<=≀<=*n*,<=*m*<=≀<=105, 1<=≀<=*q*<=≀<=2000)Β β€” the number of vertices and edges in the graph, and the number of requests correspondingly. Next *m* lines of input data contain the descriptions of edges: *i*-th of them contains description of edge with index *i*Β β€” three integers *a**i*, *b**i*, *c**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, 0<=≀<=*c**i*<=≀<=109)Β β€” the beginning and the end of edge, and its initial weight correspondingly. Next *q* lines of input data contain the description of edges in the format described above (1<=≀<=*v*<=≀<=*n*, 1<=≀<=*l**j*<=≀<=*m*). It's guaranteed that inside single query all *l**j* are distinct. Also, it's guaranteed that a total number of edges in all requests of the second type does not exceed 106.
For each query of first type print the length of the shortest path from 1 to *v* in a separate line. Print -1, if such path does not exists.
[ "3 2 9\n1 2 0\n2 3 0\n2 1 2\n1 3\n1 2\n2 1 1\n1 3\n1 2\n2 2 1 2\n1 3\n1 2\n", "5 4 9\n2 3 1\n2 4 1\n3 4 1\n1 2 0\n1 5\n1 4\n2 1 2\n2 1 2\n1 4\n2 2 1 3\n1 4\n2 1 4\n1 4\n" ]
[ "1\n0\n2\n1\n4\n2\n", "-1\n1\n2\n3\n4\n" ]
The description of changes of the graph in the first sample case: <img class="tex-graphics" src="https://espresso.codeforces.com/aeb5751e557f6f6158f15919da64eee550146483.png" style="max-width: 100.0%;max-height: 100.0%;"/> The description of changes of the graph in the second sample case: <img class="tex-graphics" src="https://espresso.codeforces.com/5d4325fe06b5b55945d91d26c757ff5c9bdfbca1.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[]
46
0
0
12,885
279
Point on Spiral
[ "brute force", "geometry", "implementation" ]
null
null
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0,<=0),<=(1,<=0)], [(1,<=0),<=(1,<=1)], [(1,<=1),<=(<=-<=1,<=1)], [(<=-<=1,<=1),<=(<=-<=1,<=<=-<=1)], [(<=-<=1,<=<=-<=1),<=(2,<=<=-<=1)], [(2,<=<=-<=1),<=(2,<=2)] and so on. Thus, this infinite spiral passes through each integer point of the plane. Valera the horse lives on the plane at coordinates (0,<=0). He wants to walk along the spiral to point (*x*,<=*y*). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0,<=0) to point (*x*,<=*y*).
The first line contains two space-separated integers *x* and *y* (|*x*|,<=|*y*|<=≀<=100).
Print a single integer, showing how many times Valera has to turn.
[ "0 0\n", "1 0\n", "0 1\n", "-1 -1\n" ]
[ "0\n", "0\n", "2\n", "3\n" ]
none
[ { "input": "0 0", "output": "0" }, { "input": "1 0", "output": "0" }, { "input": "0 1", "output": "2" }, { "input": "-1 -1", "output": "3" }, { "input": "10 10", "output": "37" }, { "input": "0 6", "output": "22" }, { "input": "-7 -13", ...
62
0
0
12,886
710
Generate a String
[ "dfs and similar", "dp" ]
null
null
zscoder wants to generate an input file for some programming competition problem. His input is a string consisting of *n* letters 'a'. He is too lazy to write a generator so he will manually generate the input in a text editor. Initially, the text editor is empty. It takes him *x* seconds to insert or delete a letter 'a' from the text file and *y* seconds to copy the contents of the entire text file, and duplicate it. zscoder wants to find the minimum amount of time needed for him to create the input file of exactly *n* letters 'a'. Help him to determine the amount of time needed to generate the input.
The only line contains three integers *n*, *x* and *y* (1<=≀<=*n*<=≀<=107, 1<=≀<=*x*,<=*y*<=≀<=109) β€” the number of letters 'a' in the input file and the parameters from the problem statement.
Print the only integer *t* β€” the minimum amount of time needed to generate the input file.
[ "8 1 1\n", "8 1 10\n" ]
[ "4\n", "8\n" ]
none
[ { "input": "8 1 1", "output": "4" }, { "input": "8 1 10", "output": "8" }, { "input": "10 62 99", "output": "384" }, { "input": "88 417 591", "output": "4623" }, { "input": "57 5289 8444", "output": "60221" }, { "input": "382 81437847 324871127", "...
62
4,608,000
0
12,903
394
Physical Education and Buns
[ "brute force", "implementation", "math" ]
null
null
The Physical education teacher at SESC is a sort of mathematician too. His most favorite topic in mathematics is progressions. That is why the teacher wants the students lined up in non-decreasing height form an arithmetic progression. To achieve the goal, the gym teacher ordered a lot of magical buns from the dining room. The magic buns come in two types: when a student eats one magic bun of the first type, his height increases by one, when the student eats one magical bun of the second type, his height decreases by one. The physical education teacher, as expected, cares about the health of his students, so he does not want them to eat a lot of buns. More precisely, he wants the maximum number of buns eaten by some student to be minimum. Help the teacher, get the maximum number of buns that some pupils will have to eat to achieve the goal of the teacher. Also, get one of the possible ways for achieving the objective, namely, the height of the lowest student in the end and the step of the resulting progression.
The single line contains integer *n* (2<=≀<=*n*<=≀<=103) β€” the number of students. The second line contains *n* space-separated integers β€” the heights of all students. The height of one student is an integer which absolute value doesn't exceed 104.
In the first line print the maximum number of buns eaten by some student to achieve the teacher's aim. In the second line, print two space-separated integers β€” the height of the lowest student in the end and the step of the progression. Please, pay attention that the step should be non-negative. If there are multiple possible answers, you can print any of them.
[ "5\n-3 -4 -2 -3 3\n", "5\n2 -3 -1 -4 3\n" ]
[ "2\n-3 1\n", "1\n-4 2\n" ]
Lets look at the first sample. We can proceed in the following manner: - don't feed the 1-st student, his height will stay equal to -3; - give two buns of the first type to the 2-nd student, his height become equal to -2; - give two buns of the first type to the 3-rd student, his height become equal to 0; - give two buns of the first type to the 4-th student, his height become equal to -1; - give two buns of the second type to the 5-th student, his height become equal to 1. To sum it up, when the students line up in non-decreasing height it will be an arithmetic progression: -3, -2, -1, 0, 1. The height of the lowest student is equal to -3, the step of the progression is equal to 1. The maximum number of buns eaten by one student is equal to 2.
[ { "input": "5\n-3 -4 -2 -3 3", "output": "2\n-3 1" }, { "input": "5\n2 -3 -1 -4 3", "output": "1\n-4 2" }, { "input": "6\n94 65 -33 -43 60 -24", "output": "25\n-67 34" }, { "input": "3\n-10000 10000 -10000", "output": "5000\n-15000 10000" }, { "input": "2\n0 0", ...
139
5,632,000
3
12,945
175
Geometry Horse
[ "greedy", "implementation", "sortings", "two pointers" ]
null
null
Vasya plays the Geometry Horse. The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value. There are *n* types of geometric figures. The number of figures of type *k**i* and figure cost *c**i* is known for each figure type. A player gets *c**i*Β·*f* points for destroying one figure of type *i*, where *f* is the current factor. The factor value can be an integer number from 1 to *t*<=+<=1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to *i*<=+<=1 after destruction of *p**i* (1<=≀<=*i*<=≀<=*t*) figures, so the (*p**i*<=+<=1)-th figure to be destroyed is considered with factor equal to *i*<=+<=1. Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.
The first line contains the only integer number *n* (1<=≀<=*n*<=≀<=100) β€” the number of figure types. Each of the following *n* lines contains two integer numbers *k**i* and *c**i* (1<=≀<=*k**i*<=≀<=109,<=0<=≀<=*c**i*<=≀<=1000), separated with space β€” the number of figures of the *i*-th type and the cost of one *i*-type figure, correspondingly. The next line contains the only integer number *t* (1<=≀<=*t*<=≀<=100) β€” the number that describe the factor's changes. The next line contains *t* integer numbers *p**i* (1<=≀<=*p*1<=&lt;<=*p*2<=&lt;<=...<=&lt;<=*p**t*<=≀<=1012), separated with spaces. Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
Print the only number β€” the maximum number of points Vasya can get.
[ "1\n5 10\n2\n3 6\n", "2\n3 8\n5 10\n1\n20\n" ]
[ "70", "74" ]
In the first example Vasya destroys three figures first and gets 3Β·1Β·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2Β·2Β·10 = 40 points. As a result Vasya will get 70 points. In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3Β·8 + 5Β·10)Β·1 = 74 points.
[ { "input": "1\n5 10\n2\n3 6", "output": "70" }, { "input": "2\n3 8\n5 10\n1\n20", "output": "74" }, { "input": "3\n10 3\n20 2\n30 1\n3\n30 50 60", "output": "200" }, { "input": "1\n100 1000\n1\n1", "output": "199000" }, { "input": "1\n1 1000\n1\n1", "output": ...
280
0
0
12,964
926
Is This a Zebra?
[ "implementation" ]
null
null
A camera you have accidentally left in a desert has taken an interesting photo. The photo has a resolution of *n* pixels width, and each column of this photo is all white or all black. Thus, we can represent the photo as a sequence of *n* zeros and ones, where 0 means that the corresponding column is all white, and 1 means that the corresponding column is black. You think that this photo can contain a zebra. In this case the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width. For example, the photo [0,<=0,<=0,<=1,<=1,<=1,<=0,<=0,<=0] can be a photo of zebra, while the photo [0,<=0,<=0,<=1,<=1,<=1,<=1] can not, because the width of the black stripe is 3, while the width of the white stripe is 4. Can the given photo be a photo of zebra or not?
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=100<=000) β€” the width of the photo. The second line contains a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=1) β€” the description of the photo. If *a**i* is zero, the *i*-th column is all black. If *a**i* is one, then the *i*-th column is all white.
If the photo can be a photo of zebra, print "YES" (without quotes). Otherwise, print "NO". You can print each letter in any case (upper or lower).
[ "9\n0 0 0 1 1 1 0 0 0\n", "7\n0 0 0 1 1 1 1\n", "5\n1 1 1 1 1\n", "8\n1 1 1 0 0 0 1 1\n", "9\n1 1 0 1 1 0 1 1 0\n" ]
[ "YES\n", "NO\n", "YES\n", "NO\n", "NO\n" ]
The first two examples are described in the statements. In the third example all pixels are white, so the photo can be a photo of zebra. In the fourth example the width of the first stripe is equal to three (white color), the width of the second stripe is equal to three (black), and the width of the third stripe is equal to two (white). Thus, not all stripes have equal length, so this photo is not a photo of zebra.
[ { "input": "9\n0 0 0 1 1 1 0 0 0", "output": "YES" }, { "input": "7\n0 0 0 1 1 1 1", "output": "NO" }, { "input": "5\n1 1 1 1 1", "output": "YES" }, { "input": "8\n1 1 1 0 0 0 1 1", "output": "NO" }, { "input": "9\n1 1 0 1 1 0 1 1 0", "output": "NO" }, { ...
140
27,545,600
3
12,968
825
Suitable Replacement
[ "binary search", "greedy", "implementation" ]
null
null
You are given two strings *s* and *t* consisting of small Latin letters, string *s* can also contain '?' characters. Suitability of string *s* is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings *s*, you choose the one with the largest number of non-intersecting occurrences of string *t*. Suitability is this number of occurrences. You should replace all '?' characters with small Latin letters in such a way that the suitability of string *s* is maximal.
The first line contains string *s* (1<=≀<=|*s*|<=≀<=106). The second line contains string *t* (1<=≀<=|*t*|<=≀<=106).
Print string *s* with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them.
[ "?aa?\nab\n", "??b?\nza\n", "abcd\nabacaba\n" ]
[ "baab\n", "azbz\n", "abcd\n" ]
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2. In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them. In the third example there are no '?' characters and the suitability of the string is 0.
[ { "input": "?aa?\nab", "output": "baab" }, { "input": "??b?\nza", "output": "azbz" }, { "input": "abcd\nabacaba", "output": "abcd" }, { "input": "mqwstphetbfrsyxuzdww\nrutseqtsbh", "output": "mqwstphetbfrsyxuzdww" }, { "input": "????????????????????\nxwkxsxlrre", ...
15
0
-1
12,986
466
Increase Sequence
[ "combinatorics", "dp" ]
null
null
Peter has a sequence of integers *a*1,<=*a*2,<=...,<=*a**n*. Peter wants all numbers in the sequence to equal *h*. He can perform the operation of "adding one on the segment [*l*,<=*r*]": add one to all elements of the sequence with indices from *l* to *r* (inclusive). At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments [*l*1,<=*r*1] and [*l*2,<=*r*2], where Peter added one, the following inequalities hold: *l*1<=β‰ <=*l*2 and *r*1<=β‰ <=*r*2. How many distinct ways are there to make all numbers in the sequence equal *h*? Print this number of ways modulo 1000000007Β (109<=+<=7). Two ways are considered distinct if one of them has a segment that isn't in the other way.
The first line contains two integers *n*,<=*h* (1<=≀<=*n*,<=*h*<=≀<=2000). The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=2000).
Print a single integer β€” the answer to the problem modulo 1000000007Β (109<=+<=7).
[ "3 2\n1 1 1\n", "5 1\n1 1 1 1 1\n", "4 3\n3 2 1 1\n" ]
[ "4\n", "1\n", "0\n" ]
none
[ { "input": "3 2\n1 1 1", "output": "4" }, { "input": "5 1\n1 1 1 1 1", "output": "1" }, { "input": "4 3\n3 2 1 1", "output": "0" }, { "input": "1 2000\n2000", "output": "1" }, { "input": "3 2\n2 1 1", "output": "2" }, { "input": "3 4\n4 3 2", "outp...
46
0
0
12,988
398
Cards
[ "constructive algorithms", "implementation" ]
null
null
User ainta loves to play with cards. He has *a* cards containing letter "o" and *b* cards containing letter "x". He arranges the cards in a row, and calculates the score of the deck by the formula below. 1. At first, the score is 0. 1. For each block of contiguous "o"s with length *x* the score increases by *x*2. 1. For each block of contiguous "x"s with length *y* the score decreases by *y*2. For example, if *a*<==<=6,<=*b*<==<=3 and ainta have arranged the cards in the order, that is described by string "ooxoooxxo", the score of the deck equals 22<=-<=12<=+<=32<=-<=22<=+<=12<==<=9. That is because the deck has 5 blocks in total: "oo", "x", "ooo", "xx", "o". User ainta likes big numbers, so he wants to maximize the score with the given cards. Help ainta make the score as big as possible. Note, that he has to arrange all his cards.
The first line contains two space-separated integers *a* and *b* (0<=≀<=*a*,<=*b*<=≀<=105;Β *a*<=+<=*b*<=β‰₯<=1) β€” the number of "o" cards and the number of "x" cards.
In the first line print a single integer *v* β€” the maximum score that ainta can obtain. In the second line print *a*<=+<=*b* characters describing the deck. If the *k*-th card of the deck contains "o", the *k*-th character must be "o". If the *k*-th card of the deck contains "x", the *k*-th character must be "x". The number of "o" characters must be equal to *a*, and the number of "x " characters must be equal to *b*. If there are many ways to maximize *v*, print any. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "2 3\n", "4 0\n", "0 4\n" ]
[ "-1\nxoxox\n", "16\noooo", "-16\nxxxx" ]
none
[ { "input": "2 3", "output": "-1\nxoxox" }, { "input": "4 0", "output": "16\noooo" }, { "input": "0 4", "output": "-16\nxxxx" }, { "input": "8 6", "output": "46\nxxxooooooooxxx" }, { "input": "28691 28312", "output": "809737773\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
108
0
0
12,998
840
In a Trap
[ "trees" ]
null
null
Lech got into a tree consisting of *n* vertices with a root in vertex number 1. At each vertex *i* written integer *a**i*. He will not get out until he answers *q* queries of the form *u* *v*. Answer for the query is maximal value among all vertices *i* on path from *u* to *v* including *u* and *v*, where *dist*(*i*,<=*v*) is number of edges on path from *i* to *v*. Also guaranteed that vertex *u* is ancestor of vertex *v*. Leha's tastes are very singular: he believes that vertex is ancestor of itself. Help Leha to get out. The expression means the bitwise exclusive OR to the numbers *x* and *y*. Note that vertex *u* is ancestor of vertex *v* if vertex *u* lies on the path from root to the vertex *v*.
First line of input data contains two integers *n* and *q* (1<=≀<=*n*<=≀<=5Β·104, 1<=≀<=*q*<=≀<=150<=000) β€” number of vertices in the tree and number of queries respectively. Next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=*n*) β€” numbers on vertices. Each of next *n*<=-<=1 lines contains two integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*n*) β€” description of the edges in tree. Guaranteed that given graph is a tree. Each of next *q* lines contains two integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*n*) β€” description of queries. Guaranteed that vertex *u* is ancestor of vertex *v*.
Output *q* lines β€” answers for a queries.
[ "5 3\n0 3 2 1 4\n1 2\n2 3\n3 4\n3 5\n1 4\n1 5\n2 4\n", "5 4\n1 2 3 4 5\n1 2\n2 3\n3 4\n4 5\n1 5\n2 5\n1 4\n3 3\n" ]
[ "3\n4\n3\n", "5\n5\n4\n3\n" ]
none
[]
46
0
0
13,079
213
Game
[ "dfs and similar", "greedy" ]
null
null
Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists of *n* parts and to complete each part a player may probably need to complete some other ones. We know that the game can be fully completed, that is, its parts do not form cyclic dependencies. Rubik has 3 computers, on which he can play this game. All computers are located in different houses. Besides, it has turned out that each part of the game can be completed only on one of these computers. Let's number the computers with integers from 1 to 3. Rubik can perform the following actions: - Complete some part of the game on some computer. Rubik spends exactly 1 hour on completing any part on any computer. - Move from the 1-st computer to the 2-nd one. Rubik spends exactly 1 hour on that. - Move from the 1-st computer to the 3-rd one. Rubik spends exactly 2 hours on that. - Move from the 2-nd computer to the 1-st one. Rubik spends exactly 2 hours on that. - Move from the 2-nd computer to the 3-rd one. Rubik spends exactly 1 hour on that. - Move from the 3-rd computer to the 1-st one. Rubik spends exactly 1 hour on that. - Move from the 3-rd computer to the 2-nd one. Rubik spends exactly 2 hours on that. Help Rubik to find the minimum number of hours he will need to complete all parts of the game. Initially Rubik can be located at the computer he considers necessary.
The first line contains integer *n* (1<=≀<=*n*<=≀<=200) β€” the number of game parts. The next line contains *n* integers, the *i*-th integer β€” *c**i* (1<=≀<=*c**i*<=≀<=3) represents the number of the computer, on which you can complete the game part number *i*. Next *n* lines contain descriptions of game parts. The *i*-th line first contains integer *k**i* (0<=≀<=*k**i*<=≀<=*n*<=-<=1), then *k**i* distinct integers *a**i*,<=*j* (1<=≀<=*a**i*,<=*j*<=≀<=*n*;Β *a**i*,<=*j*<=β‰ <=*i*) β€” the numbers of parts to complete before part *i*. Numbers on all lines are separated by single spaces. You can assume that the parts of the game are numbered from 1 to *n* in some way. It is guaranteed that there are no cyclic dependencies between the parts of the game.
On a single line print the answer to the problem.
[ "1\n1\n0\n", "5\n2 2 1 1 3\n1 5\n2 5 1\n2 5 4\n1 5\n0\n" ]
[ "1\n", "7\n" ]
Note to the second sample: before the beginning of the game the best strategy is to stand by the third computer. First we complete part 5. Then we go to the 1-st computer and complete parts 3 and 4. Then we go to the 2-nd computer and complete parts 1 and 2. In total we get 1+1+2+1+2, which equals 7 hours.
[ { "input": "1\n1\n0", "output": "1" }, { "input": "5\n2 2 1 1 3\n1 5\n2 5 1\n2 5 4\n1 5\n0", "output": "7" }, { "input": "7\n1 3 3 1 2 1 1\n0\n1 1\n1 1\n2 1 6\n3 1 2 7\n1 1\n1 1", "output": "11" }, { "input": "2\n2 1\n0\n1 1", "output": "4" }, { "input": "3\n2 1 2...
186
0
0
13,088
45
Road Problem
[ "graphs" ]
H. Road Problem
3
256
The Berland capital (as you very well know) contains *n* junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions. The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform.
The first input line contains a pair of integers *n*, *m* (2<=≀<=*n*<=≀<=900,<=1<=≀<=*m*<=≀<=100000), where *n* is the number of junctions and *m* is the number of roads. Each of the following *m* lines contains a description of a road that is given by the numbers of the connected junctions *a**i*,<=*b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*,<=*a**i*<=β‰ <=*b**i*). The junctions are numbered from 1 to *n*. It is possible to reach any junction of the city from any other one moving along roads.
On the first line print *t* β€” the number of added roads. Then on *t* lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them. If the capital doesn't need the reform, print the single number 0. If there's no solution, print the single number -1.
[ "4 3\n1 2\n2 3\n3 4\n", "4 4\n1 2\n2 3\n2 4\n3 4\n" ]
[ "1\n1 4\n", "1\n1 3\n" ]
none
[ { "input": "4 3\n1 2\n2 3\n3 4", "output": "1\n1 4" }, { "input": "4 4\n1 2\n2 3\n2 4\n3 4", "output": "1\n1 4" }, { "input": "10 18\n6 4\n3 7\n4 9\n8 4\n3 4\n3 6\n7 5\n3 9\n10 9\n10 5\n1 2\n1 8\n8 2\n5 6\n6 9\n5 9\n3 10\n7 10", "output": "1\n1 3" }, { "input": "10 13\n2 9\n9...
92
0
0
13,133
799
Fountains
[ "binary search", "data structures", "implementation" ]
null
null
Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are *n* available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allowed. Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time.
The first line contains three integers *n*, *c* and *d* (2<=≀<=*n*<=≀<=100<=000, 0<=≀<=*c*,<=*d*<=≀<=100<=000)Β β€” the number of fountains, the number of coins and diamonds Arkady has. The next *n* lines describe fountains. Each of these lines contain two integers *b**i* and *p**i* (1<=≀<=*b**i*,<=*p**i*<=≀<=100<=000)Β β€” the beauty and the cost of the *i*-th fountain, and then a letter "C" or "D", describing in which type of money is the cost of fountain *i*: in coins or in diamonds, respectively.
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.
[ "3 7 6\n10 8 C\n4 3 C\n5 6 D\n", "2 4 5\n2 5 C\n2 1 D\n", "3 10 10\n5 5 C\n5 5 C\n10 11 D\n" ]
[ "9\n", "0\n", "10\n" ]
In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can't build because he don't have enough coins. Also Arkady should build the third fountain with beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9. In the second example there are two fountains, but Arkady can't build both of them, because he needs 5 coins for the first fountain, and Arkady has only 4 coins.
[ { "input": "3 7 6\n10 8 C\n4 3 C\n5 6 D", "output": "9" }, { "input": "2 4 5\n2 5 C\n2 1 D", "output": "0" }, { "input": "3 10 10\n5 5 C\n5 5 C\n10 11 D", "output": "10" }, { "input": "6 68 40\n1 18 D\n6 16 D\n11 16 D\n7 23 D\n16 30 D\n2 20 D", "output": "18" }, { ...
124
22,016,000
0
13,150
958
Lightsabers (easy)
[ "implementation" ]
null
null
There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. Heidi has *n* Jedi Knights standing in front of her, each one with a lightsaber of one of *m* possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly *k*1 knights with lightsabers of the first color, *k*2 knights with lightsabers of the second color, ..., *k**m* knights with lightsabers of the *m*-th color. Help her find out if this is possible.
The first line of the input contains *n* (1<=≀<=*n*<=≀<=100) and *m* (1<=≀<=*m*<=≀<=*n*). The second line contains *n* integers in the range {1,<=2,<=...,<=*m*} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains *m* integers *k*1,<=*k*2,<=...,<=*k**m* (with ) – the desired counts of lightsabers of each color from 1 to *m*.
Output YES if an interval with prescribed color counts exists, or output NO if there is none.
[ "5 2\n1 1 2 2 1\n1 2\n" ]
[ "YES\n" ]
none
[ { "input": "5 2\n1 1 2 2 1\n1 2", "output": "YES" }, { "input": "1 1\n1\n1", "output": "YES" }, { "input": "2 1\n1 1\n1", "output": "YES" }, { "input": "2 1\n1 1\n2", "output": "YES" }, { "input": "2 2\n1 2\n1 1", "output": "YES" }, { "input": "3 3\n1 ...
124
0
0
13,156
375
Divisible by Seven
[ "math", "number theory" ]
null
null
You have number *a*, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7. Number *a* doesn't contain any leading zeroes and contains digits 1, 6, 8, 9 (it also can contain another digits). The resulting number also mustn't contain any leading zeroes.
The first line contains positive integer *a* in the decimal record. It is guaranteed that the record of number *a* contains digits: 1, 6, 8, 9. Number *a* doesn't contain any leading zeroes. The decimal representation of number *a* contains at least 4 and at most 106 characters.
Print a number in the decimal notation without leading zeroes β€” the result of the permutation. If it is impossible to rearrange the digits of the number *a* in the required manner, print 0.
[ "1689\n", "18906\n" ]
[ "1869\n", "18690\n" ]
none
[ { "input": "1689", "output": "1869" }, { "input": "18906", "output": "18690" }, { "input": "2419323689", "output": "2432391689" }, { "input": "8589157262", "output": "5857221986" }, { "input": "2717172350336955863014903670481525170997949309274087058935108848979319...
77
0
0
13,187
294
Shaass and Lights
[ "combinatorics", "number theory" ]
null
null
There are *n* lights aligned in a row. These lights are numbered 1 to *n* from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007Β (109<=+<=7).
The first line of the input contains two integers *n* and *m* where *n* is the number of lights in the sequence and *m* is the number of lights which are initially switched on, (1<=≀<=*n*<=≀<=1000,<=1<=≀<=*m*<=≀<=*n*). The second line contains *m* distinct integers, each between 1 to *n* inclusive, denoting the indices of lights which are initially switched on.
In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007Β (109<=+<=7).
[ "3 1\n1\n", "4 2\n1 4\n", "11 2\n4 8\n" ]
[ "1\n", "2\n", "6720\n" ]
none
[ { "input": "3 1\n1", "output": "1" }, { "input": "4 2\n1 4", "output": "2" }, { "input": "11 2\n4 8", "output": "6720" }, { "input": "4 2\n1 3", "output": "2" }, { "input": "4 4\n1 2 3 4", "output": "1" }, { "input": "4 2\n1 3", "output": "2" }, ...
109
307,200
0
13,204
112
Petya and Square
[ "implementation", "math" ]
B. Petya and Square
2
256
Little Petya loves playing with squares. Mum bought him a square 2*n*<=Γ—<=2*n* in size. Petya marked a cell inside the square and now he is solving the following task. The task is to draw a broken line that would go along the grid lines and that would cut the square into two equal parts. The cutting line should not have any common points with the marked cell and the resulting two parts should be equal up to rotation. Petya wants to determine whether it is possible to cut the square in the required manner given the sizes of the square side and the coordinates of the marked cell. Help him.
The first line contains three space-separated integers 2*n*, *x* and *y* (2<=≀<=2*n*<=≀<=100,<=1<=≀<=*x*,<=*y*<=≀<=2*n*), representing the length of a square's side and the coordinates of the marked cell. It is guaranteed that 2*n* is even. The coordinates of the marked cell are represented by a pair of numbers *x* *y*, where *x* represents the number of the row and *y* represents the number of the column. The rows and columns are numbered by consecutive integers from 1 to 2*n*. The rows are numbered from top to bottom and the columns are numbered from the left to the right.
If the square is possible to cut, print "YES", otherwise print "NO" (without the quotes).
[ "4 1 1\n", "2 2 2\n" ]
[ "YES\n", "NO\n" ]
A sample test from the statement and one of the possible ways of cutting the square are shown in the picture:
[ { "input": "4 1 1", "output": "YES" }, { "input": "2 2 2", "output": "NO" }, { "input": "8 2 5", "output": "YES" }, { "input": "6 1 6", "output": "YES" }, { "input": "2 1 1", "output": "NO" }, { "input": "2 1 2", "output": "NO" }, { "input"...
124
0
0
13,219
633
Fibonacci-ish
[ "brute force", "dp", "hashing", "implementation", "math" ]
null
null
Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if 1. the sequence consists of at least two elements 1. *f*0 and *f*1 are arbitrary 1. *f**n*<=+<=2<==<=*f**n*<=+<=1<=+<=*f**n* for all *n*<=β‰₯<=0. You are given some sequence of integers *a*1,<=*a*2,<=...,<=*a**n*. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence.
The first line of the input contains a single integer *n* (2<=≀<=*n*<=≀<=1000)Β β€” the length of the sequence *a**i*. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≀<=109).
Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement.
[ "3\n1 2 -1\n", "5\n28 35 7 14 21\n" ]
[ "3\n", "4\n" ]
In the first sample, if we rearrange elements of the sequence as  - 1, 2, 1, the whole sequence *a*<sub class="lower-index">*i*</sub> would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/16f1f7e35511b29cb1396890ca2fb7dfa4d428de.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4003973f16750522e492d7d79318d7e2f0ff99cd.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/87b18fd9524b11e12faf154302fb14c1b55556fb.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b8950ea952476baea26e03281fa2f7640b6241ef.png" style="max-width: 100.0%;max-height: 100.0%;"/>, 28.
[ { "input": "3\n1 2 -1", "output": "3" }, { "input": "5\n28 35 7 14 21", "output": "4" }, { "input": "11\n-9 -1 -10 9 7 -4 0 -8 -3 3 5", "output": "5" }, { "input": "10\n-4 -8 -8 8 -9 0 -7 9 1 0", "output": "4" }, { "input": "2\n2 2", "output": "2" }, { ...
311
0
0
13,222
103
Cthulhu
[ "dfs and similar", "dsu", "graphs" ]
B. Cthulhu
2
256
...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu... Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super weapon. Due to high seismic activity and poor weather conditions the satellites haven't yet been able to make clear shots of the monster. The analysis of the first shot resulted in an undirected graph with *n* vertices and *m* edges. Now the world's best minds are about to determine whether this graph can be regarded as Cthulhu or not. To add simplicity, let's suppose that Cthulhu looks from the space like some spherical body with tentacles attached to it. Formally, we shall regard as Cthulhu such an undirected graph that can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle. It is guaranteed that the graph contains no multiple edges and self-loops.
The first line contains two integers β€” the number of vertices *n* and the number of edges *m* of the graph (1<=≀<=*n*<=≀<=100, 0<=≀<=*m*<=≀<=). Each of the following *m* lines contains a pair of integers *x* and *y*, that show that an edge exists between vertices *x* and *y* (1<=≀<=*x*,<=*y*<=≀<=*n*,<=*x*<=β‰ <=*y*). For each pair of vertices there will be at most one edge between them, no edge connects a vertex to itself.
Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.
[ "6 6\n6 3\n6 4\n5 1\n2 5\n1 4\n5 4\n", "6 5\n5 6\n4 6\n3 1\n5 1\n1 2\n" ]
[ "FHTAGN!", "NO" ]
Let us denote as a simple cycle a set of *v* vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., *v* - 1 and *v*, *v* and 1. A tree is a connected undirected graph consisting of *n* vertices and *n* - 1 edges (*n* &gt; 0). A rooted tree is a tree where one vertex is selected to be the root.
[ { "input": "6 6\n6 3\n6 4\n5 1\n2 5\n1 4\n5 4", "output": "FHTAGN!" }, { "input": "6 5\n5 6\n4 6\n3 1\n5 1\n1 2", "output": "NO" }, { "input": "10 10\n4 10\n8 5\n2 8\n4 9\n9 3\n2 7\n10 6\n10 2\n9 8\n1 8", "output": "FHTAGN!" }, { "input": "5 4\n1 5\n1 3\n1 4\n3 2", "outpu...
186
0
0
13,223
758
Blown Garland
[ "brute force", "implementation", "number theory" ]
null
null
Nothing is eternal in the world, Kostya understood it on the 7-th of January when he saw partially dead four-color garland. Now he has a goal to replace dead light bulbs, however he doesn't know how many light bulbs for each color are required. It is guaranteed that for each of four colors at least one light is working. It is known that the garland contains light bulbs of four colors: red, blue, yellow and green. The garland is made as follows: if you take any four consecutive light bulbs then there will not be light bulbs with the same color among them. For example, the garland can look like "RYBGRYBGRY", "YBGRYBGRYBG", "BGRYB", but can not look like "BGRYG", "YBGRYBYGR" or "BGYBGY". Letters denote colors: 'R'Β β€” red, 'B'Β β€” blue, 'Y'Β β€” yellow, 'G'Β β€” green. Using the information that for each color at least one light bulb still works count the number of dead light bulbs of each four colors.
The first and the only line contains the string *s* (4<=≀<=|*s*|<=≀<=100), which describes the garland, the *i*-th symbol of which describes the color of the *i*-th light bulb in the order from the beginning of garland: - 'R'Β β€” the light bulb is red, - 'B'Β β€” the light bulb is blue, - 'Y'Β β€” the light bulb is yellow, - 'G'Β β€” the light bulb is green, - '!'Β β€” the light bulb is dead. The string *s* can not contain other symbols except those five which were described. It is guaranteed that in the given string at least once there is each of four letters 'R', 'B', 'Y' and 'G'. It is guaranteed that the string *s* is correct garland with some blown light bulbs, it means that for example the line "GRBY!!!B" can not be in the input data.
In the only line print four integers *k**r*,<=*k**b*,<=*k**y*,<=*k**g*Β β€” the number of dead light bulbs of red, blue, yellow and green colors accordingly.
[ "RYBGRYBGR\n", "!RGYB\n", "!!!!YGRB\n", "!GB!RG!Y!\n" ]
[ "0 0 0 0", "0 1 0 0", "1 1 1 1", "2 1 1 0" ]
In the first example there are no dead light bulbs. In the second example it is obvious that one blue bulb is blown, because it could not be light bulbs of other colors on its place according to the statements.
[ { "input": "RYBGRYBGR", "output": "0 0 0 0" }, { "input": "!RGYB", "output": "0 1 0 0" }, { "input": "!!!!YGRB", "output": "1 1 1 1" }, { "input": "!GB!RG!Y!", "output": "2 1 1 0" }, { "input": "RYBG", "output": "0 0 0 0" }, { "input": "!Y!!!Y!!G!!!G!!...
62
0
3
13,252
339
Xenia and Bit Operations
[ "data structures", "trees" ]
null
null
Xenia the beginner programmer has a sequence *a*, consisting of 2*n* non-negative integers: *a*1,<=*a*2,<=...,<=*a*2*n*. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value *v* for *a*. Namely, it takes several iterations to calculate value *v*. At the first iteration, Xenia writes a new sequence *a*1Β *or*Β *a*2,<=*a*3Β *or*Β *a*4,<=...,<=*a*2*n*<=-<=1Β *or*Β *a*2*n*, consisting of 2*n*<=-<=1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence *a*. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is *v*. Let's consider an example. Suppose that sequence *a*<==<=(1,<=2,<=3,<=4). Then let's write down all the transformations (1,<=2,<=3,<=4) <=β†’<= (1Β *or*Β 2<==<=3,<=3Β *or*Β 4<==<=7) <=β†’<= (3Β *xor*Β 7<==<=4). The result is *v*<==<=4. You are given Xenia's initial sequence. But to calculate value *v* for a given sequence would be too easy, so you are given additional *m* queries. Each query is a pair of integers *p*,<=*b*. Query *p*,<=*b* means that you need to perform the assignment *a**p*<==<=*b*. After each query, you need to print the new value *v* for the new sequence *a*.
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=17,<=1<=≀<=*m*<=≀<=105). The next line contains 2*n* integers *a*1,<=*a*2,<=...,<=*a*2*n* (0<=≀<=*a**i*<=&lt;<=230). Each of the next *m* lines contains queries. The *i*-th line contains integers *p**i*,<=*b**i* (1<=≀<=*p**i*<=≀<=2*n*,<=0<=≀<=*b**i*<=&lt;<=230) β€” the *i*-th query.
Print *m* integers β€” the *i*-th integer denotes value *v* for sequence *a* after the *i*-th query.
[ "2 4\n1 6 3 5\n1 4\n3 4\n1 2\n1 2\n" ]
[ "1\n3\n3\n3\n" ]
For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation
[ { "input": "2 4\n1 6 3 5\n1 4\n3 4\n1 2\n1 2", "output": "1\n3\n3\n3" }, { "input": "1 1\n1 1\n1 1", "output": "1" }, { "input": "1 10\n6 26\n1 11\n1 9\n1 31\n1 10\n2 12\n1 8\n2 10\n2 4\n2 18\n1 31", "output": "27\n27\n31\n26\n14\n12\n10\n12\n26\n31" }, { "input": "1 10\n22 1...
2,000
19,251,200
0
13,253
159
Palindrome pairs
[ "*special", "brute force", "dp", "strings" ]
null
null
You are given a non-empty string *s* consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (*a*,<=*b*,<=*x*,<=*y*) such that 1<=≀<=*a*<=≀<=*b*<=&lt;<=*x*<=≀<=*y*<=≀<=|*s*| and substrings *s*[*a*... *b*], *s*[*x*... *y*] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring *s*[*i*... *j*] (1<=≀<=*i*<=≀<=*j*<=≀<=|*s*|) of string *s* = *s*1*s*2... *s*|*s*| is a string *s**i**s**i*<=+<=1... *s**j*. For example, substring *s*[2...4] of string *s* = "abacaba" equals "bac".
The first line of input contains a non-empty string *s* which consists of lowercase letters ('a'...'z'), *s* contains at most 2000 characters.
Output a single number β€” the quantity of pairs of non-overlapping palindromic substrings of *s*. Please do not use the %lld format specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d format specifier.
[ "aa\n", "aaa\n", "abacaba\n" ]
[ "1\n", "5\n", "36\n" ]
none
[ { "input": "aa", "output": "1" }, { "input": "aaa", "output": "5" }, { "input": "abacaba", "output": "36" }, { "input": "aaaaaaaaaa", "output": "495" }, { "input": "aabbb", "output": "24" }, { "input": "abbaa", "output": "18" }, { "input": ...
92
0
0
13,296
366
Dima and Salad
[ "dp" ]
null
null
Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. Dima and Seryozha have *n* fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal *k*. In other words, , where *a**j* is the taste of the *j*-th chosen fruit and *b**j* is its calories. Inna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem β€” now the happiness of a young couple is in your hands! Inna loves Dima very much so she wants to make the salad from at least one fruit.
The first line of the input contains two integers *n*, *k* (1<=≀<=*n*<=≀<=100,<=1<=≀<=*k*<=≀<=10). The second line of the input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=100) β€” the fruits' tastes. The third line of the input contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=100) β€” the fruits' calories. Fruit number *i* has taste *a**i* and calories *b**i*.
If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer β€” the maximum possible sum of the taste values of the chosen fruits.
[ "3 2\n10 8 1\n2 7 1\n", "5 3\n4 4 4 4 4\n2 2 2 2 2\n" ]
[ "18\n", "-1\n" ]
In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f7429bb0084a26268b364ce919a5231a4d9e38a9.png" style="max-width: 100.0%;max-height: 100.0%;"/> fulfills, that's exactly what Inna wants. In the second test sample we cannot choose the fruits so as to follow Inna's principle.
[ { "input": "3 2\n10 8 1\n2 7 1", "output": "18" }, { "input": "5 3\n4 4 4 4 4\n2 2 2 2 2", "output": "-1" }, { "input": "1 1\n1\n1", "output": "1" }, { "input": "1 1\n1\n2", "output": "-1" }, { "input": "2 1\n75 65\n16 60", "output": "-1" }, { "input":...
77
204,800
0
13,303
317
Ants
[ "brute force", "implementation" ]
null
null
It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (*x*, *y*) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (*x*<=+<=1, *y*), (*x*<=-<=1, *y*), (*x*, *y*<=+<=1), (*x*, *y*<=-<=1) β€” one ant in each direction. No other ant movements will happen. Ants never interfere with each other. Scientists have put a colony of *n* ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops.
First input line contains integers *n* (0<=≀<=*n*<=≀<=30000) and *t* (1<=≀<=*t*<=≀<=50000), where *n* is the number of ants in the colony and *t* is the number of queries. Each of the next *t* lines contains coordinates of a query junction: integers *x**i*, *y**i* (<=-<=109<=≀<=*x**i*,<=*y**i*<=≀<=109). Queries may coincide. It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end).
Print *t* integers, one per line β€” the number of ants at the corresponding junctions when the movement of the ants stops.
[ "1 3\n0 1\n0 0\n0 -1\n", "6 5\n0 -2\n0 -1\n0 0\n0 1\n0 2\n" ]
[ "0\n1\n0\n", "0\n1\n2\n1\n0\n" ]
In the first sample the colony consists of the one ant, so nothing happens at all. In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops.
[]
170
5,529,600
-1
13,321
402
Nuts
[ "greedy", "math" ]
null
null
You have *a* nuts and lots of boxes. The boxes have a wonderful feature: if you put *x* (*x*<=β‰₯<=0) divisors (the spacial bars that can divide a box) to it, you get a box, divided into *x*<=+<=1 sections. You are minimalist. Therefore, on the one hand, you are against dividing some box into more than *k* sections. On the other hand, you are against putting more than *v* nuts into some section of the box. What is the minimum number of boxes you have to use if you want to put all the nuts in boxes, and you have *b* divisors? Please note that you need to minimize the number of used boxes, not sections. You do not have to minimize the number of used divisors.
The first line contains four space-separated integers *k*, *a*, *b*, *v* (2<=≀<=*k*<=≀<=1000; 1<=≀<=*a*,<=*b*,<=*v*<=≀<=1000) β€” the maximum number of sections in the box, the number of nuts, the number of divisors and the capacity of each section of the box.
Print a single integer β€” the answer to the problem.
[ "3 10 3 3\n", "3 10 1 3\n", "100 100 1 1000\n" ]
[ "2\n", "3\n", "1\n" ]
In the first sample you can act like this: - Put two divisors to the first box. Now the first box has three sections and we can put three nuts into each section. Overall, the first box will have nine nuts. - Do not put any divisors into the second box. Thus, the second box has one section for the last nut. In the end we've put all the ten nuts into boxes. The second sample is different as we have exactly one divisor and we put it to the first box. The next two boxes will have one section each.
[ { "input": "3 10 3 3", "output": "2" }, { "input": "3 10 1 3", "output": "3" }, { "input": "100 100 1 1000", "output": "1" }, { "input": "5 347 20 1", "output": "327" }, { "input": "6 978 10 5", "output": "186" }, { "input": "6 856 50 35", "output"...
46
4,608,000
0
13,328
0
none
[ "none" ]
null
null
Little Petya likes permutations a lot. Recently his mom has presented him permutation *q*1,<=*q*2,<=...,<=*q**n* of length *n*. A permutation *a* of length *n* is a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*), all integers there are distinct. There is only one thing Petya likes more than permutations: playing with little Masha. As it turns out, Masha also has a permutation of length *n*. Petya decided to get the same permutation, whatever the cost may be. For that, he devised a game with the following rules: - Before the beginning of the game Petya writes permutation 1,<=2,<=...,<=*n* on the blackboard. After that Petya makes exactly *k* moves, which are described below. - During a move Petya tosses a coin. If the coin shows heads, he performs point 1, if the coin shows tails, he performs point 2. Let's assume that the board contains permutation *p*1,<=*p*2,<=...,<=*p**n* at the given moment. Then Petya removes the written permutation *p* from the board and writes another one instead: *p**q*1,<=*p**q*2,<=...,<=*p**q**n*. In other words, Petya applies permutation *q* (which he has got from his mother) to permutation *p*. - All actions are similar to point 1, except that Petya writes permutation *t* on the board, such that: *t**q**i*<==<=*p**i* for all *i* from 1 to *n*. In other words, Petya applies a permutation that is inverse to *q* to permutation *p*. We know that after the *k*-th move the board contained Masha's permutation *s*1,<=*s*2,<=...,<=*s**n*. Besides, we know that throughout the game process Masha's permutation never occurred on the board before the *k*-th move. Note that the game has exactly *k* moves, that is, throughout the game the coin was tossed exactly *k* times. Your task is to determine whether the described situation is possible or else state that Petya was mistaken somewhere. See samples and notes to them for a better understanding.
The first line contains two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=100). The second line contains *n* space-separated integers *q*1,<=*q*2,<=...,<=*q**n* (1<=≀<=*q**i*<=≀<=*n*) β€” the permutation that Petya's got as a present. The third line contains Masha's permutation *s*, in the similar format. It is guaranteed that the given sequences *q* and *s* are correct permutations.
If the situation that is described in the statement is possible, print "YES" (without the quotes), otherwise print "NO" (without the quotes).
[ "4 1\n2 3 4 1\n1 2 3 4\n", "4 1\n4 3 1 2\n3 4 2 1\n", "4 3\n4 3 1 2\n3 4 2 1\n", "4 2\n4 3 1 2\n2 1 4 3\n", "4 1\n4 3 1 2\n2 1 4 3\n" ]
[ "NO\n", "YES\n", "YES\n", "YES\n", "NO\n" ]
In the first sample Masha's permutation coincides with the permutation that was written on the board before the beginning of the game. Consequently, that violates the condition that Masha's permutation never occurred on the board before *k* moves were performed. In the second sample the described situation is possible, in case if after we toss a coin, we get tails. In the third sample the possible coin tossing sequence is: heads-tails-tails. In the fourth sample the possible coin tossing sequence is: heads-heads.
[ { "input": "4 1\n2 3 4 1\n1 2 3 4", "output": "NO" }, { "input": "4 1\n4 3 1 2\n3 4 2 1", "output": "YES" }, { "input": "4 3\n4 3 1 2\n3 4 2 1", "output": "YES" }, { "input": "4 2\n4 3 1 2\n2 1 4 3", "output": "YES" }, { "input": "4 1\n4 3 1 2\n2 1 4 3", "outp...
30
0
-1
13,329
802
Heidi and Library (medium)
[ "data structures", "greedy" ]
null
null
Whereas humans nowadays read fewer and fewer books on paper, book readership among marmots has surged. Heidi has expanded the library and is now serving longer request sequences.
Same as the easy version, but the limits have changed: 1<=≀<=*n*,<=*k*<=≀<=400<=000.
Same as the easy version.
[ "4 100\n1 2 2 1\n", "4 1\n1 2 2 1\n", "4 2\n1 2 3 1\n" ]
[ "2\n", "3\n", "3\n" ]
none
[ { "input": "4 100\n1 2 2 1", "output": "2" }, { "input": "4 1\n1 2 2 1", "output": "3" }, { "input": "4 2\n1 2 3 1", "output": "3" }, { "input": "11 1\n1 2 3 5 1 10 10 1 1 3 5", "output": "9" }, { "input": "5 2\n1 2 3 1 2", "output": "4" }, { "input": ...
30
0
0
13,357
223
Planar Graph
[ "flows", "geometry", "graphs" ]
null
null
A graph is called planar, if it can be drawn in such a way that its edges intersect only at their vertexes. An articulation point is such a vertex of an undirected graph, that when removed increases the number of connected components of the graph. A bridge is such an edge of an undirected graph, that when removed increases the number of connected components of the graph. You've got a connected undirected planar graph consisting of *n* vertexes, numbered from 1 to *n*, drawn on the plane. The graph has no bridges, articulation points, loops and multiple edges. You are also given *q* queries. Each query is a cycle in the graph. The query response is the number of graph vertexes, which (if you draw a graph and the cycle on the plane) are located either inside the cycle, or on it. Write a program that, given the graph and the queries, will answer each query.
The first line contains two space-separated integers *n* and *m* (3<=≀<=*n*,<=*m*<=≀<=105) β€” the number of vertexes and edges of the graph. Next *m* lines contain the edges of the graph: the *i*-th line contains two space-separated integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*) β€” the numbers of vertexes, connecting the *i*-th edge. The next *n* lines contain the positions of the planar graph vertexes on the plane: the *i*-th line contains a pair of space-separated integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=≀<=109) β€” the coordinates of the *i*-th vertex of the graph on the plane. The next line contains integer *q* (1<=≀<=*q*<=≀<=105)Β β€” the number of queries. Then follow *q* lines that describe the queries: the *i*-th line contains the sequence of space-separated integers *k**i*, *a*1, *a*2, ..., *a**k**i* (1<=≀<=*a**j*<=≀<=*n*;Β *k**i*<=&gt;<=2), where *k**i* is the cycle length in the *i*-th query, *a**j* are numbers of the vertexes that form a cycle. The numbers of vertexes in the cycle are given in the clockwise or counterclockwise order. The given cycles are simple, that is they cannot go through a graph vertex more than once. The total length of all cycles in all queries does not exceed 105. It is guaranteed that the given graph contains no bridges, articulation points, loops and multiple edges. It is guaranteed that the edge segments can have common points only at the graph's vertexes.
For each query print a single integer β€” the number of vertexes inside the cycle or on it. Print the answers in the order, in which the queries follow in the input. Separate the numbers by spaces.
[ "3 3\n1 2\n2 3\n3 1\n0 0\n1 0\n0 1\n1\n3 1 2 3\n", "5 8\n1 2\n2 3\n3 4\n4 1\n1 5\n2 5\n3 5\n4 5\n0 0\n2 0\n2 2\n0 2\n1 1\n1\n4 1 2 3 4\n", "4 5\n1 2\n2 3\n3 4\n4 1\n2 4\n0 0\n1 0\n1 1\n0 1\n3\n3 1 2 4\n3 4 2 3\n4 1 2 3 4\n" ]
[ "3\n", "5\n", "3\n3\n4\n" ]
none
[]
61
204,800
0
13,369
793
Mice problem
[ "geometry", "implementation", "math", "sortings" ]
null
null
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them. The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (*x*1,<=*y*1) and (*x*2,<=*y*2). Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the *i*-th mouse is equal to (*v**i**x*,<=*v**i**y*), that means that the *x* coordinate of the mouse increases by *v**i**x* units per second, while the *y* coordinates increases by *v**i**y* units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap. Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
The first line contains single integer *n* (1<=≀<=*n*<=≀<=100<=000)Β β€” the number of computer mice on the desk. The second line contains four integers *x*1, *y*1, *x*2 and *y*2 (0<=≀<=*x*1<=≀<=*x*2<=≀<=100<=000), (0<=≀<=*y*1<=≀<=*y*2<=≀<=100<=000)Β β€” the coordinates of the opposite corners of the mousetrap. The next *n* lines contain the information about mice. The *i*-th of these lines contains four integers *r**i**x*, *r**i**y*, *v**i**x* and *v**i**y*, (0<=≀<=*r**i**x*,<=*r**i**y*<=≀<=100<=000, <=-<=100<=000<=≀<=*v**i**x*,<=*v**i**y*<=≀<=100<=000), where (*r**i**x*,<=*r**i**y*) is the initial position of the mouse, and (*v**i**x*,<=*v**i**y*) is its speed.
In the only line print minimum possible non-negative number *t* such that if Igor closes the mousetrap at *t* seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such *t*, print -1. Your answer is considered correct if its absolute or relative error doesn't exceed 10<=-<=6. Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if .
[ "4\n7 7 9 8\n3 5 7 5\n7 5 2 4\n3 3 7 8\n6 6 3 2\n", "4\n7 7 9 8\n0 3 -5 4\n5 0 5 4\n9 9 -1 -6\n10 5 -7 -10\n" ]
[ "0.57142857142857139685\n", "-1\n" ]
Here is a picture of the first sample Points A, B, C, D - start mice positions, segments are their paths. <img class="tex-graphics" src="https://espresso.codeforces.com/9b2a39ff850b63eb3f41de7ce9efc61a192e99b5.png" style="max-width: 100.0%;max-height: 100.0%;"/> Then, at first time when all mice will be in rectangle it will be looks like this: <img class="tex-graphics" src="https://espresso.codeforces.com/bfdaed392636d2b1790e7986ca711c1c3ebe298c.png" style="max-width: 100.0%;max-height: 100.0%;"/> Here is a picture of the second sample <img class="tex-graphics" src="https://espresso.codeforces.com/a49c381e9f3e453fe5be91a972128def69042e45.png" style="max-width: 100.0%;max-height: 100.0%;"/> Points A, D, B will never enter rectangle.
[ { "input": "4\n7 7 9 8\n3 5 7 5\n7 5 2 4\n3 3 7 8\n6 6 3 2", "output": "0.57142857142857139685" }, { "input": "4\n7 7 9 8\n0 3 -5 4\n5 0 5 4\n9 9 -1 -6\n10 5 -7 -10", "output": "-1" }, { "input": "4\n8 42 60 54\n9 54 -58 -62\n46 47 52 -76\n15 50 -37 -40\n54 51 78 64", "output": "0.00...
61
5,529,600
0
13,390
417
Football
[ "constructive algorithms", "graphs", "implementation" ]
null
null
One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into *n* teams and played several matches, two teams could not play against each other more than once. The appointed Judge was the most experienced member β€” Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches. Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly *k* times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table.
The first line contains two integers β€” *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=1000).
In the first line print an integer *m* β€” number of the played games. The following *m* lines should contain the information about all the matches, one match per line. The *i*-th line should contain two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*; *a**i*<=β‰ <=*b**i*). The numbers *a**i* and *b**i* mean, that in the *i*-th match the team with number *a**i* won against the team with number *b**i*. You can assume, that the teams are numbered from 1 to *n*. If a tournir that meets the conditions of the problem does not exist, then print -1.
[ "3 1\n" ]
[ "3\n1 2\n2 3\n3 1\n" ]
none
[ { "input": "3 1", "output": "3\n1 2\n2 3\n3 1" }, { "input": "7 3", "output": "21\n1 2\n1 3\n1 4\n2 3\n2 4\n2 5\n3 4\n3 5\n3 6\n4 5\n4 6\n4 7\n5 6\n5 7\n5 1\n6 7\n6 1\n6 2\n7 1\n7 2\n7 3" }, { "input": "4 1", "output": "4\n1 2\n2 3\n3 4\n4 1" }, { "input": "5 2", "output"...
77
307,200
0
13,459
896
Nephren Runs a Cinema
[ "chinese remainder theorem", "combinatorics", "math", "number theory" ]
null
null
Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema. However, one day, the No. 68 Cinema runs out of changes (they don't have 50-yuan notes currently), but Nephren still wants to start their business. (Assume that yuan is a kind of currency in Regulu Ere.) There are three types of customers: some of them bring exactly a 50-yuan note; some of them bring a 100-yuan note and Nephren needs to give a 50-yuan note back to him/her; some of them bring VIP cards so that they don't need to pay for the ticket. Now *n* customers are waiting outside in queue. Nephren wants to know how many possible queues are there that they are able to run smoothly (i.e. every customer can receive his/her change), and that the number of 50-yuan notes they have after selling tickets to all these customers is between *l* and *r*, inclusive. Two queues are considered different if there exists a customer whose type is different in two queues. As the number can be large, please output the answer modulo *p*.
One line containing four integers *n* (1<=≀<=*n*<=≀<=105), *p* (1<=≀<=*p*<=≀<=2Β·109), *l* and *r* (0<=≀<=*l*<=≀<=*r*<=≀<=*n*).
One line indicating the answer modulo *p*.
[ "4 97 2 3\n", "4 100 0 4\n" ]
[ "13\n", "35\n" ]
We use A, B and C to indicate customers with 50-yuan notes, customers with 100-yuan notes and customers with VIP cards respectively. For the first sample, the different possible queues that there are 2 50-yuan notes left are AAAB, AABA, ABAA, AACC, ACAC, ACCA, CAAC, CACA and CCAA, and the different possible queues that there are 3 50-yuan notes left are AAAC, AACA, ACAA and CAAA. So there are 13 different queues satisfying the first sample. Similarly, there are 35 different queues satisfying the second sample.
[ { "input": "4 97 2 3", "output": "13" }, { "input": "4 100 0 4", "output": "35" }, { "input": "13 143 6 11", "output": "129" }, { "input": "999 998244353 666 777", "output": "974283165" }, { "input": "23333 1000000007 0 23333", "output": "192355111" }, { ...
1,996
268,390,400
0
13,484
869
The Intriguing Obsession
[ "combinatorics", "dp", "math" ]
null
null
β€” This is not playing but duty as allies of justice, Nii-chan! β€” Not allies but justice itself, Onii-chan! With hands joined, go everywhere at a speed faster than our thoughts! This time, the Fire SistersΒ β€” Karen and TsukihiΒ β€” is heading for somewhere they've never reachedΒ β€” water-surrounded islands! There are three clusters of islands, conveniently coloured red, blue and purple. The clusters consist of *a*, *b* and *c* distinct islands respectively. Bridges have been built between some (possibly all or none) of the islands. A bridge bidirectionally connects two different islands and has length 1. For any two islands of the same colour, either they shouldn't be reached from each other through bridges, or the shortest distance between them is at least 3, apparently in order to prevent oddities from spreading quickly inside a cluster. The Fire Sisters are ready for the unknown, but they'd also like to test your courage. And you're here to figure out the number of different ways to build all bridges under the constraints, and give the answer modulo 998<=244<=353. Two ways are considered different if a pair of islands exist, such that there's a bridge between them in one of them, but not in the other.
The first and only line of input contains three space-separated integers *a*, *b* and *c* (1<=≀<=*a*,<=*b*,<=*c*<=≀<=5<=000)Β β€” the number of islands in the red, blue and purple clusters, respectively.
Output one line containing an integerΒ β€” the number of different ways to build bridges, modulo 998<=244<=353.
[ "1 1 1\n", "1 2 2\n", "1 3 5\n", "6 2 9\n" ]
[ "8\n", "63\n", "3264\n", "813023575\n" ]
In the first example, there are 3 bridges that can possibly be built, and no setup of bridges violates the restrictions. Thus the answer is 2<sup class="upper-index">3</sup> = 8. In the second example, the upper two structures in the figure below are instances of valid ones, while the lower two are invalid due to the blue and purple clusters, respectively.
[ { "input": "1 1 1", "output": "8" }, { "input": "1 2 2", "output": "63" }, { "input": "1 3 5", "output": "3264" }, { "input": "6 2 9", "output": "813023575" }, { "input": "7 3 7", "output": "807577560" }, { "input": "135 14 39", "output": "41484950...
1,000
30,208,000
0
13,533
0
none
[ "none" ]
null
null
Johnny drives a truck and must deliver a package from his hometown to the district center. His hometown is located at point 0 on a number line, and the district center is located at the point *d*. Johnny's truck has a gas tank that holds exactly *n* liters, and his tank is initially full. As he drives, the truck consumes exactly one liter per unit distance traveled. Moreover, there are *m* gas stations located at various points along the way to the district center. The *i*-th station is located at the point *x**i* on the number line and sells an unlimited amount of fuel at a price of *p**i* dollars per liter. Find the minimum cost Johnny must pay for fuel to successfully complete the delivery.
The first line of input contains three space separated integers *d*, *n*, and *m* (1<=≀<=*n*<=≀<=*d*<=≀<=109, 1<=≀<=*m*<=≀<=200 000)Β β€” the total distance to the district center, the volume of the gas tank, and the number of gas stations, respectively. Each of the next *m* lines contains two integers *x**i*, *p**i* (1<=≀<=*x**i*<=≀<=*d*<=-<=1, 1<=≀<=*p**i*<=≀<=106)Β β€” the position and cost of gas at the *i*-th gas station. It is guaranteed that the positions of the gas stations are distinct.
Print a single integerΒ β€” the minimum cost to complete the delivery. If there is no way to complete the delivery, print -1.
[ "10 4 4\n3 5\n5 8\n6 3\n8 4\n", "16 5 2\n8 2\n5 1\n" ]
[ "22\n", "-1\n" ]
In the first sample, Johnny's truck holds 4 liters. He can drive 3 units to the first gas station, buy 2 liters of gas there (bringing the tank to 3 liters total), drive 3 more units to the third gas station, buy 4 liters there to fill up his tank, and then drive straight to the district center. His total cost is 2Β·5 + 4Β·3 = 22 dollars. In the second sample, there is no way for Johnny to make it to the district center, as his tank cannot hold enough gas to take him from the latest gas station to the district center.
[ { "input": "10 4 4\n3 5\n5 8\n6 3\n8 4", "output": "22" }, { "input": "16 5 2\n8 2\n5 1", "output": "-1" }, { "input": "400000000 400000000 3\n1 139613\n19426 13509\n246298622 343529", "output": "0" }, { "input": "229 123 2\n170 270968\n76 734741", "output": "50519939" ...
62
0
0
13,569
45
TCMCF+++
[ "greedy" ]
I. TCMCF+++
2
256
Vasya has gotten interested in programming contests in TCMCF+++ rules. On the contest *n* problems were suggested and every problem had a cost β€” a certain integral number of points (perhaps, negative or even equal to zero). According to TCMCF+++ rules, only accepted problems can earn points and the overall number of points of a contestant was equal to the product of the costs of all the problems he/she had completed. If a person didn't solve anything, then he/she didn't even appear in final standings and wasn't considered as participant. Vasya understood that to get the maximal number of points it is not always useful to solve all the problems. Unfortunately, he understood it only after the contest was finished. Now he asks you to help him: find out what problems he had to solve to earn the maximal number of points.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of the suggested problems. The next line contains *n* space-separated integers *c**i* (<=-<=100<=≀<=*c**i*<=≀<=100) β€” the cost of the *i*-th task. The tasks' costs may coinсide.
Print space-separated the costs of the problems that needed to be solved to get the maximal possible number of points. Do not forget, please, that it was necessary to solve at least one problem. If there are several solutions to that problem, print any of them.
[ "5\n1 2 -3 3 3\n", "13\n100 100 100 100 100 100 100 100 100 100 100 100 100\n", "4\n-2 -2 -2 -2\n" ]
[ "3 1 2 3 \n", "100 100 100 100 100 100 100 100 100 100 100 100 100 \n", "-2 -2 -2 -2 \n" ]
none
[ { "input": "5\n1 2 -3 3 3", "output": "3 1 2 3 " }, { "input": "13\n100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "100 100 100 100 100 100 100 100 100 100 100 100 100 " }, { "input": "4\n-2 -2 -2 -2", "output": "-2 -2 -2 -2 " }, { "input": "1\n1", "outpu...
124
0
0
13,573
929
ΠšΡ€Π°ΡΠΈΠ²Π°Ρ ΠΊΠΎΠΌΠ°Π½Π΄Π°
[ "*special", "combinatorics", "math" ]
null
null
Π—Π°Π²Ρ‚Ρ€Π° Ρƒ Ρ…ΠΎΠΊΠΊΠ΅ΠΉΠ½ΠΎΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Ρ‹, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Ρ€ΡƒΠΊΠΎΠ²ΠΎΠ΄ΠΈΡ‚ Π•Π²Π³Π΅Π½ΠΈΠΉ, Π²Π°ΠΆΠ½Ρ‹ΠΉ ΠΌΠ°Ρ‚Ρ‡. Π•Π²Π³Π΅Π½ΠΈΡŽ Π½ΡƒΠΆΠ½ΠΎ Π²Ρ‹Π±Ρ€Π°Ρ‚ΡŒ ΡˆΠ΅ΡΡ‚ΡŒ ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ², ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π²Ρ‹ΠΉΠ΄ΡƒΡ‚ Π½Π° Π»Π΅Π΄ Π² стартовом составС: ΠΎΠ΄ΠΈΠ½ Π²Ρ€Π°Ρ‚Π°Ρ€ΡŒ, Π΄Π²Π° Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊΠ° ΠΈ Ρ‚Ρ€ΠΈ Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΡ…. Π’Π°ΠΊ ΠΊΠ°ΠΊ это стартовый состав, ЕвгСния большС Π²ΠΎΠ»Π½ΡƒΠ΅Ρ‚, насколько красива Π±ΡƒΠ΄Π΅Ρ‚ ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π° Π»ΡŒΠ΄Ρƒ, Ρ‡Π΅ΠΌ способности ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ². А ΠΈΠΌΠ΅Π½Π½ΠΎ, Π•Π²Π³Π΅Π½ΠΈΠΉ Ρ…ΠΎΡ‡Π΅Ρ‚ Π²Ρ‹Π±Ρ€Π°Ρ‚ΡŒ Ρ‚Π°ΠΊΠΎΠΉ стартовый состав, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π½ΠΎΠΌΠ΅Ρ€Π° Π»ΡŽΠ±Ρ‹Ρ… Π΄Π²ΡƒΡ… ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² ΠΈΠ· стартового состава ΠΎΡ‚Π»ΠΈΡ‡Π°Π»ΠΈΡΡŒ Π½Π΅ Π±ΠΎΠ»Π΅Π΅, Ρ‡Π΅ΠΌ Π² Π΄Π²Π° Ρ€Π°Π·Π°. НапримСр, ΠΈΠ³Ρ€ΠΎΠΊΠΈ с Π½ΠΎΠΌΠ΅Ρ€Π°ΠΌΠΈ 13, 14, 10, 18, 15 ΠΈ 20 устроят ЕвгСния, Π° Ссли, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, Π½Π° Π»Π΅Π΄ Π²Ρ‹ΠΉΠ΄ΡƒΡ‚ ΠΈΠ³Ρ€ΠΎΠΊΠΈ с Π½ΠΎΠΌΠ΅Ρ€Π°ΠΌΠΈ 8 ΠΈ 17, Ρ‚ΠΎ это Π½Π΅ устроит ЕвгСния. ΠŸΡ€ΠΎ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΈΠ· ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² Π²Π°ΠΌ извСстно, Π½Π° ΠΊΠ°ΠΊΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ ΠΎΠ½ ΠΈΠ³Ρ€Π°Π΅Ρ‚ (Π²Ρ€Π°Ρ‚Π°Ρ€ΡŒ, Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊ ΠΈΠ»ΠΈ Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΠΉ), Π° Ρ‚Π°ΠΊΠΆΠ΅ Π΅Π³ΠΎ Π½ΠΎΠΌΠ΅Ρ€. Π’ Ρ…ΠΎΠΊΠΊΠ΅Π΅ Π½ΠΎΠΌΠ΅Ρ€Π° ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² Π½Π΅ ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ ΠΈΠ΄ΡƒΡ‚ подряд. ΠŸΠΎΡΡ‡ΠΈΡ‚Π°ΠΉΡ‚Π΅ число Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹Ρ… стартовых составов ΠΈΠ· ΠΎΠ΄Π½ΠΎΠ³ΠΎ вратаря, Π΄Π²ΡƒΡ… Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊΠΎΠ² ΠΈ Ρ‚Ρ€Π΅Ρ… Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΡ…, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π²Ρ‹Π±Ρ€Π°Ρ‚ΡŒ Π•Π²Π³Π΅Π½ΠΈΠΉ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹ΠΏΠΎΠ»Π½ΡΠ»ΠΎΡΡŒ Π΅Π³ΠΎ условиС красоты.
ΠŸΠ΅Ρ€Π²Π°Ρ строка содСрТит Ρ‚Ρ€ΠΈ Ρ†Π΅Π»Ρ‹Ρ… числа *g*, *d* ΠΈ *f* (1<=≀<=*g*<=≀<=1<=000, 1<=≀<=*d*<=≀<=1<=000, 1<=≀<=*f*<=≀<=1<=000)Β β€” число Π²Ρ€Π°Ρ‚Π°Ρ€Π΅ΠΉ, Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊΠΎΠ² ΠΈ Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΡ… Π² ΠΊΠΎΠΌΠ°Π½Π΄Π΅ ЕвгСния. Вторая строка содСрТит *g* Ρ†Π΅Π»Ρ‹Ρ… чисСл, ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ Π² ΠΏΡ€Π΅Π΄Π΅Π»Π°Ρ… ΠΎΡ‚ 1 Π΄ΠΎ 100<=000Β β€” Π½ΠΎΠΌΠ΅Ρ€Π° Π²Ρ€Π°Ρ‚Π°Ρ€Π΅ΠΉ. Π’Ρ€Π΅Ρ‚ΡŒΡ строка содСрТит *d* Ρ†Π΅Π»Ρ‹Ρ… чисСл, ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ Π² ΠΏΡ€Π΅Π΄Π΅Π»Π°Ρ… ΠΎΡ‚ 1 Π΄ΠΎ 100<=000Β β€” Π½ΠΎΠΌΠ΅Ρ€Π° Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊΠΎΠ². ЧСтвСртая строка содСрТит *f* Ρ†Π΅Π»Ρ‹Ρ… чисСл, ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ Π² ΠΏΡ€Π΅Π΄Π΅Π»Π°Ρ… ΠΎΡ‚ 1 Π΄ΠΎ 100<=000Β β€” Π½ΠΎΠΌΠ΅Ρ€Π° Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΡ…. ГарантируСтся, Ρ‡Ρ‚ΠΎ ΠΎΠ±Ρ‰Π΅Π΅ количСство ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² Π½Π΅ прСвосходит 1<=000, Ρ‚.Β Π΅. *g*<=+<=*d*<=+<=*f*<=≀<=1<=000. ВсС *g*<=+<=*d*<=+<=*f* Π½ΠΎΠΌΠ΅Ρ€ΠΎΠ² ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹.
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число — количСство Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Ρ… стартовых составов.
[ "1 2 3\n15\n10 19\n20 11 13\n", "2 3 4\n16 40\n20 12 19\n13 21 11 10\n" ]
[ "1\n", "6\n" ]
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ всСго ΠΎΠ΄ΠΈΠ½ Π²Π°Ρ€ΠΈΠ°Π½Ρ‚ для Π²Ρ‹Π±ΠΎΡ€Π° состава, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ удовлСтворяСт описанным условиям, поэтому ΠΎΡ‚Π²Π΅Ρ‚ 1. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ подходят ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ ΠΈΠ³Ρ€ΠΎΠ²Ρ‹Π΅ сочСтания (Π² порядкС Π²Ρ€Π°Ρ‚Π°Ρ€ΡŒ-Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊ-Π·Π°Ρ‰ΠΈΡ‚Π½ΠΈΠΊ-Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΠΉ-Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΠΉ-Π½Π°ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΠΉ): - 16 20 12 13 21 11 - 16 20 12 13 11 10 - 16 20 19 13 21 11 - 16 20 19 13 11 10 - 16 12 19 13 21 11 - 16 12 19 13 11 10 Π’Π°ΠΊΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ, ΠΎΡ‚Π²Π΅Ρ‚ Π½Π° этот ΠΏΡ€ΠΈΠΌΠ΅Ρ€ β€” 6.
[ { "input": "1 2 3\n15\n10 19\n20 11 13", "output": "1" }, { "input": "2 3 4\n16 40\n20 12 19\n13 21 11 10", "output": "6" }, { "input": "4 4 5\n15 16 19 6\n8 11 9 18\n5 3 1 12 14", "output": "0" }, { "input": "6 7 7\n32 35 26 33 16 23\n4 40 36 12 28 24 3\n39 11 31 37 1 25 6",...
1,000
5,632,000
0
13,580
946
Timetable
[ "dp" ]
null
null
Ivan is a student at Berland State University (BSU). There are *n* days in Berland week, and each of these days Ivan might have some classes at the university. There are *m* working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during *i*-th hour, and last lesson is during *j*-th hour, then he spends *j*<=-<=*i*<=+<=1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university. Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than *k* lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all. Given *n*, *m*, *k* and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than *k* lessons?
The first line contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=500, 0<=≀<=*k*<=≀<=500) β€” the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively. Then *n* lines follow, *i*-th line containing a binary string of *m* characters. If *j*-th character in *i*-th line is 1, then Ivan has a lesson on *i*-th day during *j*-th hour (if it is 0, there is no such lesson).
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than *k* lessons.
[ "2 5 1\n01001\n10110\n", "2 5 0\n01001\n10110\n" ]
[ "5\n", "8\n" ]
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day. In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
[ { "input": "2 5 1\n01001\n10110", "output": "5" }, { "input": "2 5 0\n01001\n10110", "output": "8" }, { "input": "3 4 0\n0000\n0000\n0000", "output": "0" }, { "input": "3 4 12\n1111\n1111\n1111", "output": "0" }, { "input": "3 4 6\n1111\n1111\n1111", "output":...
2,000
1,740,800
0
13,605
232
Table
[ "bitmasks", "combinatorics", "dp", "math" ]
null
null
John Doe has an *n*<=Γ—<=*m* table. John Doe can paint points in some table cells, not more than one point in one table cell. John Doe wants to use such operations to make each square subtable of size *n*<=Γ—<=*n* have exactly *k* points. John Doe wondered, how many distinct ways to fill the table with points are there, provided that the condition must hold. As this number can be rather large, John Doe asks to find its remainder after dividing by 1000000007 (109<=+<=7). You should assume that John always paints a point exactly in the center of some cell. Two ways to fill a table are considered distinct, if there exists a table cell, that has a point in one way and doesn't have it in the other.
A single line contains space-separated integers *n*, *m*, *k* (1<=≀<=*n*<=≀<=100;Β *n*<=≀<=*m*<=≀<=1018;Β 0<=≀<=*k*<=≀<=*n*2) β€” the number of rows of the table, the number of columns of the table and the number of points each square must contain. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
In a single line print a single integer β€” the remainder from dividing the described number of ways by 1000000007 (109<=+<=7).
[ "5 6 1\n" ]
[ "45" ]
Let's consider the first test case:
[ { "input": "5 6 1", "output": "45" }, { "input": "1 1000000000000000000 0", "output": "1" }, { "input": "100 1000000 5000", "output": "13662512" }, { "input": "100 1000000000000000000 10000", "output": "1" }, { "input": "2 1791938441 1", "output": "216278738" ...
4,000
9,523,200
0
13,638
690
The Wall (medium)
[ "combinatorics" ]
null
null
Heidi the Cow is aghast: cracks in the northern Wall? Zombies gathering outside, forming groups, preparing their assault? This must not happen! Quickly, she fetches her HC2 (Handbook of Crazy Constructions) and looks for the right chapter: How to build a wall: 1. Take a set of bricks. 1. Select one of the possible wall designs. Computing the number of possible designs is left as an exercise to the reader. 1. Place bricks on top of each other, according to the chosen design. This seems easy enough. But Heidi is a Coding Cow, not a Constructing Cow. Her mind keeps coming back to point 2b. Despite the imminent danger of a zombie onslaught, she wonders just how many possible walls she could build with up to *n* bricks. A wall is a set of wall segments as defined in the easy version. How many different walls can be constructed such that the wall consists of at least 1 and at most *n* bricks? Two walls are different if there exist a column *c* and a row *r* such that one wall has a brick in this spot, and the other does not. Along with *n*, you will be given *C*, the width of the wall (as defined in the easy version). Return the number of different walls modulo 106<=+<=3.
The first line contains two space-separated integers *n* and *C*, 1<=≀<=*n*<=≀<=500000, 1<=≀<=*C*<=≀<=200000.
Print the number of different walls that Heidi could build, modulo 106<=+<=3.
[ "5 1\n", "2 2\n", "3 2\n", "11 5\n", "37 63\n" ]
[ "5\n", "5\n", "9\n", "4367\n", "230574\n" ]
The number 10<sup class="upper-index">6</sup> + 3 is prime. In the second sample case, the five walls are: In the third sample case, the nine walls are the five as in the second sample case and in addition the following four:
[ { "input": "5 1", "output": "5" }, { "input": "2 2", "output": "5" }, { "input": "3 2", "output": "9" }, { "input": "11 5", "output": "4367" }, { "input": "37 63", "output": "230574" }, { "input": "1 1", "output": "1" }, { "input": "350000 ...
935
18,124,800
3
13,648
761
Dasha and Very Difficult Problem
[ "binary search", "brute force", "constructive algorithms", "greedy", "sortings" ]
null
null
Dasha logged into the system and began to solve problems. One of them is as follows: Given two sequences *a* and *b* of length *n* each you need to write a sequence *c* of length *n*, the *i*-th element of which is calculated as follows: *c**i*<==<=*b**i*<=-<=*a**i*. About sequences *a* and *b* we know that their elements are in the range from *l* to *r*. More formally, elements satisfy the following conditions: *l*<=≀<=*a**i*<=≀<=*r* and *l*<=≀<=*b**i*<=≀<=*r*. About sequence *c* we know that all its elements are distinct. Dasha wrote a solution to that problem quickly, but checking her work on the standard test was not so easy. Due to an error in the test system only the sequence *a* and the compressed sequence of the sequence *c* were known from that test. Let's give the definition to a compressed sequence. A compressed sequence of sequence *c* of length *n* is a sequence *p* of length *n*, so that *p**i* equals to the number of integers which are less than or equal to *c**i* in the sequence *c*. For example, for the sequence *c*<==<=[250,<=200,<=300,<=100,<=50] the compressed sequence will be *p*<==<=[4,<=3,<=5,<=2,<=1]. Pay attention that in *c* all integers are distinct. Consequently, the compressed sequence contains all integers from 1 to *n* inclusively. Help Dasha to find any sequence *b* for which the calculated compressed sequence of sequence *c* is correct.
The first line contains three integers *n*, *l*, *r* (1<=≀<=*n*<=≀<=105,<=1<=≀<=*l*<=≀<=*r*<=≀<=109) β€” the length of the sequence and boundaries of the segment where the elements of sequences *a* and *b* are. The next line contains *n* integers *a*1,<=<=*a*2,<=<=...,<=<=*a**n* (*l*<=≀<=*a**i*<=≀<=*r*) β€” the elements of the sequence *a*. The next line contains *n* distinct integers *p*1,<=<=*p*2,<=<=...,<=<=*p**n* (1<=≀<=*p**i*<=≀<=*n*) β€” the compressed sequence of the sequence *c*.
If there is no the suitable sequence *b*, then in the only line print "-1". Otherwise, in the only line print *n* integers β€” the elements of any suitable sequence *b*.
[ "5 1 5\n1 1 1 1 1\n3 1 5 4 2\n", "4 2 9\n3 4 8 9\n3 2 1 4\n", "6 1 5\n1 1 1 1 1 1\n2 3 5 4 1 6\n" ]
[ "3 1 5 4 2 ", "2 2 2 9 ", "-1\n" ]
Sequence *b* which was found in the second sample is suitable, because calculated sequence *c* = [2 - 3, 2 - 4, 2 - 8, 9 - 9] = [ - 1,  - 2,  - 6, 0] (note that *c*<sub class="lower-index">*i*</sub> = *b*<sub class="lower-index">*i*</sub> - *a*<sub class="lower-index">*i*</sub>) has compressed sequence equals to *p* = [3, 2, 1, 4].
[ { "input": "5 1 5\n1 1 1 1 1\n3 1 5 4 2", "output": "3 1 5 4 2 " }, { "input": "4 2 9\n3 4 8 9\n3 2 1 4", "output": "2 2 2 9 " }, { "input": "6 1 5\n1 1 1 1 1 1\n2 3 5 4 1 6", "output": "-1" }, { "input": "5 1 7\n1 4 4 6 5\n5 2 1 4 3", "output": "2 2 1 6 4 " }, { ...
46
0
0
13,649
362
Two Semiknights Meet
[ "greedy", "math" ]
null
null
A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard. Petya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count. Petya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board. Please see the test case analysis.
The first line contains number *t* (1<=≀<=*t*<=≀<=50) β€” the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters ".", "#", "K", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.
For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO" otherwise.
[ "2\n........\n........\n......#.\nK..##..#\n.......#\n...##..#\n......#.\nK.......\n\n........\n........\n..#.....\n..#..#..\n..####..\n...##...\n........\n....K#K#\n" ]
[ "YES\nNO\n" ]
Consider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7). On the second board the semiknights will never meet.
[ { "input": "2\n........\n........\n......#.\nK..##..#\n.......#\n...##..#\n......#.\nK.......\n\n........\n........\n..#.....\n..#..#..\n..####..\n...##...\n........\n....K#K#", "output": "YES\nNO" }, { "input": "3\n........\n........\n..#.....\n..#..#..\n..####..\n...##...\n........\n####K#K#\n\n.....
62
3,379,200
-1
13,656
547
Mike and Frog
[ "brute force", "greedy", "implementation", "math" ]
null
null
Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is *h*1 and height of Abol is *h*2. Each second, Mike waters Abol and Xaniar. So, if height of Xaniar is *h*1 and height of Abol is *h*2, after one second height of Xaniar will become and height of Abol will become where *x*1,<=*y*1,<=*x*2 and *y*2 are some integer numbers and denotes the remainder of *a* modulo *b*. Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is *a*1 and height of Abol is *a*2. Mike has asked you for your help. Calculate the minimum time or say it will never happen.
The first line of input contains integer *m* (2<=≀<=*m*<=≀<=106). The second line of input contains integers *h*1 and *a*1 (0<=≀<=*h*1,<=*a*1<=&lt;<=*m*). The third line of input contains integers *x*1 and *y*1 (0<=≀<=*x*1,<=*y*1<=&lt;<=*m*). The fourth line of input contains integers *h*2 and *a*2 (0<=≀<=*h*2,<=*a*2<=&lt;<=*m*). The fifth line of input contains integers *x*2 and *y*2 (0<=≀<=*x*2,<=*y*2<=&lt;<=*m*). It is guaranteed that *h*1<=β‰ <=*a*1 and *h*2<=β‰ <=*a*2.
Print the minimum number of seconds until Xaniar reaches height *a*1 and Abol reaches height *a*2 or print -1 otherwise.
[ "5\n4 2\n1 1\n0 1\n2 3\n", "1023\n1 2\n1 0\n1 2\n1 1\n" ]
[ "3\n", "-1\n" ]
In the first sample, heights sequences are following: Xaniar: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/85da629b05969e7a8a6636d995b8fe7a0494e8f4.png" style="max-width: 100.0%;max-height: 100.0%;"/> Abol: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ea95da14490864ae8b8bfcd4a8b7c02ad3a666b3.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "5\n4 2\n1 1\n0 1\n2 3", "output": "3" }, { "input": "1023\n1 2\n1 0\n1 2\n1 1", "output": "-1" }, { "input": "1023\n1 2\n1 2\n1 2\n1 2", "output": "512" }, { "input": "2\n0 1\n1 0\n1 0\n0 1", "output": "-1" }, { "input": "17\n15 12\n15 12\n12 14\n1 11"...
1,000
5,529,600
0
13,661
547
Mike and Foam
[ "bitmasks", "combinatorics", "dp", "math", "number theory" ]
null
null
Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are *n* kinds of beer at Rico's numbered from 1 to *n*. *i*-th kind of beer has *a**i* milliliters of foam on it. Maxim is Mike's boss. Today he told Mike to perform *q* queries. Initially the shelf is empty. In each request, Maxim gives him a number *x*. If beer number *x* is already in the shelf, then Mike should remove it from the shelf, otherwise he should put it in the shelf. After each query, Mike should tell him the score of the shelf. Bears are geeks. So they think that the score of a shelf is the number of pairs (*i*,<=*j*) of glasses in the shelf such that *i*<=&lt;<=*j* and where is the greatest common divisor of numbers *a* and *b*. Mike is tired. So he asked you to help him in performing these requests.
The first line of input contains numbers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=2<=Γ—<=105), the number of different kinds of beer and number of queries. The next line contains *n* space separated integers, *a*1,<=*a*2,<=... ,<=*a**n* (1<=≀<=*a**i*<=≀<=5<=Γ—<=105), the height of foam in top of each kind of beer. The next *q* lines contain the queries. Each query consists of a single integer integer *x* (1<=≀<=*x*<=≀<=*n*), the index of a beer that should be added or removed from the shelf.
For each query, print the answer for that query in one line.
[ "5 6\n1 2 3 4 6\n1\n2\n3\n4\n5\n1\n" ]
[ "0\n1\n3\n5\n6\n2\n" ]
none
[ { "input": "5 6\n1 2 3 4 6\n1\n2\n3\n4\n5\n1", "output": "0\n1\n3\n5\n6\n2" }, { "input": "3 3\n151790 360570 1\n2\n3\n3", "output": "0\n1\n0" }, { "input": "1 1\n1\n1", "output": "0" }, { "input": "5 10\n1 1 1 1 1\n1\n2\n3\n4\n5\n5\n4\n3\n2\n1", "output": "0\n1\n3\n6\n10...
2,000
1,945,600
0
13,690
177
Fibonacci Strings
[ "matrices", "strings" ]
null
null
Fibonacci strings are defined as follows: - *f*1 = Β«aΒ» - *f*2 = Β«bΒ» - *f**n* = *f**n*<=-<=1Β *f**n*<=-<=2, *n*<=&gt;<=2 Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba". You are given a Fibonacci string and *m* strings *s**i*. For each string *s**i*, find the number of times it occurs in the given Fibonacci string as a substring.
The first line contains two space-separated integers *k* and *m* β€” the number of a Fibonacci string and the number of queries, correspondingly. Next *m* lines contain strings *s**i* that correspond to the queries. It is guaranteed that strings *s**i* aren't empty and consist only of characters "a" and "b". The input limitations for getting 30 points are: - 1<=≀<=*k*<=≀<=3000 - 1<=≀<=*m*<=≀<=3000 - The total length of strings *s**i* doesn't exceed 3000 The input limitations for getting 100 points are: - 1<=≀<=*k*<=≀<=1018 - 1<=≀<=*m*<=≀<=104 - The total length of strings *s**i* doesn't exceed 105 Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
For each string *s**i* print the number of times it occurs in the given Fibonacci string as a substring. Since the numbers can be large enough, print them modulo 1000000007 (109<=+<=7). Print the answers for the strings in the order in which they are given in the input.
[ "6 5\na\nb\nab\nba\naba\n" ]
[ "3\n5\n3\n3\n1\n" ]
none
[ { "input": "6 5\na\nb\nab\nba\naba", "output": "3\n5\n3\n3\n1" }, { "input": "10 10\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab", "output": "12\n21\n21\n0\n12\n21\n21\n0\n12\n21" }, { "input": "10 10\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb", "output": "0\n12\n21\n0\n12\n8\n0\n0...
218
0
0
13,712
774
Bars
[ "*special", "binary search" ]
null
null
Polycarp's workday lasts exactly $n$ minutes. He loves chocolate bars and can eat one bar in one minute. Today Polycarp has $k$ bars at the beginning of the workday. In some minutes of the workday Polycarp has important things to do and in such minutes he is not able to eat a chocolate bar. In other minutes he can either eat or not eat one chocolate bar. It is guaranteed, that in the first and in the last minutes of the workday Polycarp has no important things to do and he will always eat bars in this minutes to gladden himself at the begining and at the end of the workday. Also it is guaranteed, that $k$ is strictly greater than $1$. Your task is to determine such an order of eating chocolate bars that the maximum break time between eating bars is as minimum as possible. Consider that Polycarp eats a bar in the minute $x$ and the next bar in the minute $y$ ($x &lt; y$). Then the break time is equal to $y - x - 1$ minutes. It is not necessary for Polycarp to eat all bars he has.
The first line contains two integers $n$ and $k$ ($2 \le n \le 200\,000$, $2 \le k \le n$) β€” the length of the workday in minutes and the number of chocolate bars, which Polycarp has in the beginning of the workday. The second line contains the string with length $n$ consisting of zeros and ones. If the $i$-th symbol in the string equals to zero, Polycarp has no important things to do in the minute $i$ and he can eat a chocolate bar. In the other case, Polycarp is busy in the minute $i$ and can not eat a chocolate bar. It is guaranteed, that the first and the last characters of the string are equal to zero, and Polycarp always eats chocolate bars in these minutes.
Print the minimum possible break in minutes between eating chocolate bars.
[ "3 3\n010\n", "8 3\n01010110\n" ]
[ "1\n", "3\n" ]
In the first example Polycarp can not eat the chocolate bar in the second minute, so the time of the break equals to one minute. In the second example Polycarp will eat bars in the minutes $1$ and $8$ anyway, also he needs to eat the chocolate bar in the minute $5$, so that the time of the maximum break will be equal to $3$ minutes.
[ { "input": "3 3\n010", "output": "1" }, { "input": "8 3\n01010110", "output": "3" }, { "input": "9 5\n001100110", "output": "2" }, { "input": "2 2\n00", "output": "0" }, { "input": "3 2\n010", "output": "1" }, { "input": "3 2\n000", "output": "1" ...
139
20,172,800
0
13,724
665
Beautiful Subarrays
[ "data structures", "divide and conquer", "strings", "trees" ]
null
null
One day, ZS the Coder wrote down an array of integers *a*<=with elements *a*1,<=<=*a*2,<=<=...,<=<=*a**n*. A subarray of the array *a* is a sequence *a**l*,<=<=*a**l*<=<=+<=<=1,<=<=...,<=<=*a**r* for some integers (*l*,<=<=*r*) such that 1<=<=≀<=<=*l*<=<=≀<=<=*r*<=<=≀<=<=*n*. ZS the Coder thinks that a subarray of *a* is beautiful if the bitwise xor of all the elements in the subarray is at least *k*. Help ZS the Coder find the number of beautiful subarrays of *a*!
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=106,<=1<=≀<=*k*<=≀<=109) β€” the number of elements in the array *a* and the value of the parameter *k*. The second line contains *n* integers *a**i* (0<=≀<=*a**i*<=≀<=109) β€” the elements of the array *a*.
Print the only integer *c* β€” the number of beautiful subarrays of the array *a*.
[ "3 1\n1 2 3\n", "3 2\n1 2 3\n", "3 3\n1 2 3\n" ]
[ "5\n", "3\n", "2\n" ]
none
[ { "input": "3 1\n1 2 3", "output": "5" }, { "input": "3 2\n1 2 3", "output": "3" }, { "input": "3 3\n1 2 3", "output": "2" }, { "input": "1 1\n1", "output": "1" }, { "input": "10 1\n1 1 0 1 0 1 1 0 0 0", "output": "28" }, { "input": "100 80\n85 16 22 8...
46
0
0
13,725
260
Black and White Tree
[ "constructive algorithms", "dsu", "graphs", "greedy", "trees" ]
null
null
The board has got a painted tree graph, consisting of *n* nodes. Let us remind you that a non-directed graph is called a tree if it is connected and doesn't contain any cycles. Each node of the graph is painted black or white in such a manner that there aren't two nodes of the same color, connected by an edge. Each edge contains its value written on it as a non-negative integer. A bad boy Vasya came up to the board and wrote number *s**v* near each node *v* β€” the sum of values of all edges that are incident to this node. Then Vasya removed the edges and their values from the board. Your task is to restore the original tree by the node colors and numbers *s**v*.
The first line of the input contains a single integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of nodes in the tree. Next *n* lines contain pairs of space-separated integers *c**i*, *s**i* (0<=≀<=*c**i*<=≀<=1, 0<=≀<=*s**i*<=≀<=109), where *c**i* stands for the color of the *i*-th vertex (0 is for white, 1 is for black), and *s**i* represents the sum of values of the edges that are incident to the *i*-th vertex of the tree that is painted on the board.
Print the description of *n*<=-<=1 edges of the tree graph. Each description is a group of three integers *v**i*, *u**i*, *w**i* (1<=≀<=*v**i*,<=*u**i*<=≀<=*n*, *v**i*<=β‰ <=*u**i*, 0<=≀<=*w**i*<=≀<=109), where *v**i* and *u**i* β€” are the numbers of the nodes that are connected by the *i*-th edge, and *w**i* is its value. Note that the following condition must fulfill *c**v**i*<=β‰ <=*c**u**i*. It is guaranteed that for any input data there exists at least one graph that meets these data. If there are multiple solutions, print any of them. You are allowed to print the edges in any order. As you print the numbers, separate them with spaces.
[ "3\n1 3\n1 2\n0 5\n", "6\n1 0\n0 3\n1 8\n0 2\n0 3\n0 0\n" ]
[ "3 1 3\n3 2 2\n", "2 3 3\n5 3 3\n4 3 2\n1 6 0\n2 1 0\n" ]
none
[ { "input": "3\n1 3\n1 2\n0 5", "output": "3 1 3\n3 2 2" }, { "input": "6\n1 0\n0 3\n1 8\n0 2\n0 3\n0 0", "output": "2 3 3\n5 3 3\n4 3 2\n1 6 0\n2 1 0" }, { "input": "2\n0 0\n1 0", "output": "1 2 0" }, { "input": "5\n1 11\n0 9\n1 4\n0 4\n0 2", "output": "2 1 9\n4 3 4\n5 1 ...
46
0
0
13,746
691
Swaps in Permutation
[ "dfs and similar", "dsu", "math" ]
null
null
You are given a permutation of the numbers 1,<=2,<=...,<=*n* and *m* pairs of positions (*a**j*,<=*b**j*). At each step you can choose a pair from the given positions and swap the numbers in that positions. What is the lexicographically maximal permutation one can get? Let *p* and *q* be two permutations of the numbers 1,<=2,<=...,<=*n*. *p* is lexicographically smaller than the *q* if a number 1<=≀<=*i*<=≀<=*n* exists, so *p**k*<==<=*q**k* for 1<=≀<=*k*<=&lt;<=*i* and *p**i*<=&lt;<=*q**i*.
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=106) β€” the length of the permutation *p* and the number of pairs of positions. The second line contains *n* distinct integers *p**i* (1<=≀<=*p**i*<=≀<=*n*) β€” the elements of the permutation *p*. Each of the last *m* lines contains two integers (*a**j*,<=*b**j*) (1<=≀<=*a**j*,<=*b**j*<=≀<=*n*) β€” the pairs of positions to swap. Note that you are given a positions, not the values to swap.
Print the only line with *n* distinct integers *p*'*i* (1<=≀<=*p*'*i*<=≀<=*n*) β€” the lexicographically maximal permutation one can get.
[ "9 6\n1 2 3 4 5 6 7 8 9\n1 4\n4 7\n2 5\n5 8\n3 6\n6 9\n" ]
[ "7 8 9 4 5 6 1 2 3\n" ]
none
[ { "input": "9 6\n1 2 3 4 5 6 7 8 9\n1 4\n4 7\n2 5\n5 8\n3 6\n6 9", "output": "7 8 9 4 5 6 1 2 3" }, { "input": "1 1\n1\n1 1", "output": "1" }, { "input": "2 10\n2 1\n2 1\n1 2\n1 1\n2 1\n1 1\n2 1\n1 1\n1 1\n2 1\n2 1", "output": "2 1" }, { "input": "3 10\n1 2 3\n2 2\n1 1\n2 2\n...
93
819,200
-1
13,770
381
Sereja and Stairs
[ "greedy", "implementation", "sortings" ]
null
null
Sereja loves integer sequences very much. He especially likes stairs. Sequence *a*1,<=*a*2,<=...,<=*a*|*a*| (|*a*| is the length of the sequence) is stairs if there is such index *i* (1<=≀<=*i*<=≀<=|*a*|), that the following condition is met: For example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't. Sereja has *m* cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the table?
The first line contains integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of Sereja's cards. The second line contains *m* integers *b**i* (1<=≀<=*b**i*<=≀<=5000) β€” the numbers on the Sereja's cards.
In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.
[ "5\n1 2 3 4 5\n", "6\n1 1 2 2 3 3\n" ]
[ "5\n5 4 3 2 1\n", "5\n1 2 3 2 1\n" ]
none
[ { "input": "5\n1 2 3 4 5", "output": "5\n5 4 3 2 1" }, { "input": "6\n1 1 2 2 3 3", "output": "5\n1 2 3 2 1" }, { "input": "47\n3 4 5 3 1 4 4 3 4 6 1 5 1 3 5 3 6 5 1 4 3 2 6 5 3 1 4 6 4 6 2 1 1 1 4 3 6 1 6 6 3 5 1 4 6 4 4", "output": "11\n1 2 3 4 5 6 5 4 3 2 1" }, { "input": ...
31
0
0
13,772
670
Magic Powder - 2
[ "binary search", "implementation" ]
null
null
The term of this problem is the same as the previous one, the only exception β€” increased restrictions.
The first line contains two positive integers *n* and *k* (1<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*k*<=≀<=109) β€” the number of ingredients and the number of grams of the magic powder. The second line contains the sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109), where the *i*-th number is equal to the number of grams of the *i*-th ingredient, needed to bake one cookie. The third line contains the sequence *b*1,<=*b*2,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=109), where the *i*-th number is equal to the number of grams of the *i*-th ingredient, which Apollinaria has.
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
[ "1 1000000000\n1\n1000000000\n", "10 1\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n1 1 1 1 1 1 1 1 1 1\n", "3 1\n2 1 4\n11 3 16\n", "4 3\n4 3 5 6\n11 12 14 20\n" ]
[ "2000000000\n", "0\n", "4\n", "3\n" ]
none
[ { "input": "1 1000000000\n1\n1000000000", "output": "2000000000" }, { "input": "10 1\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n1 1 1 1 1 1 1 1 1 1", "output": "0" }, { "input": "3 1\n2 1 4\n11 3 16", "output": "4" ...
873
11,673,600
3
13,809
514
Watto and Mechanism
[ "binary search", "data structures", "hashing", "string suffix structures", "strings" ]
null
null
Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with *n* strings. Then the mechanism should be able to process queries of the following type: "Given string *s*, determine if the memory of the mechanism contains string *t* that consists of the same number of characters as *s* and differs from *s* in exactly one position". Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of *n* initial lines and *m* queries. He decided to entrust this job to you.
The first line contains two non-negative numbers *n* and *m* (0<=≀<=*n*<=≀<=3Β·105, 0<=≀<=*m*<=≀<=3Β·105) β€” the number of the initial strings and the number of queries, respectively. Next follow *n* non-empty strings that are uploaded to the memory of the mechanism. Next follow *m* non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6Β·105. Each line consists only of letters 'a', 'b', 'c'.
For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).
[ "2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac\n" ]
[ "YES\nNO\nNO\n" ]
none
[ { "input": "2 3\naaaaa\nacacaca\naabaa\nccacacc\ncaaac", "output": "YES\nNO\nNO" }, { "input": "1 5\nacbacbacb\ncbacbacb\nacbacbac\naacbacbacb\nacbacbacbb\nacbaabacb", "output": "NO\nNO\nNO\nNO\nYES" }, { "input": "5 4\nab\ncacab\ncbabc\nacc\ncacab\nabc\naa\nacbca\ncb", "output": "YE...
358
18,944,000
0
13,813
793
Presents in Bankopolis
[ "dp", "graphs", "shortest paths" ]
null
null
Bankopolis is an incredible city in which all the *n* crossroads are located on a straight line and numbered from 1 to *n* along it. On each crossroad there is a bank office. The crossroads are connected with *m* oriented bicycle lanes (the *i*-th lane goes from crossroad *u**i* to crossroad *v**i*), the difficulty of each of the lanes is known. Oleg the bank client wants to gift happiness and joy to the bank employees. He wants to visit exactly *k* offices, in each of them he wants to gift presents to the employees. The problem is that Oleg don't want to see the reaction on his gifts, so he can't use a bicycle lane which passes near the office in which he has already presented his gifts (formally, the *i*-th lane passes near the office on the *x*-th crossroad if and only if *min*(*u**i*,<=*v**i*)<=&lt;<=*x*<=&lt;<=*max*(*u**i*,<=*v**i*))). Of course, in each of the offices Oleg can present gifts exactly once. Oleg is going to use exactly *k*<=-<=1 bicycle lane to move between offices. Oleg can start his path from any office and finish it in any office. Oleg wants to choose such a path among possible ones that the total difficulty of the lanes he will use is minimum possible. Find this minimum possible total difficulty.
The first line contains two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=80)Β β€” the number of crossroads (and offices) and the number of offices Oleg wants to visit. The second line contains single integer *m* (0<=≀<=*m*<=≀<=2000)Β β€” the number of bicycle lanes in Bankopolis. The next *m* lines contain information about the lanes. The *i*-th of these lines contains three integers *u**i*, *v**i* and *c**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, 1<=≀<=*c**i*<=≀<=1000), denoting the crossroads connected by the *i*-th road and its difficulty.
In the only line print the minimum possible total difficulty of the lanes in a valid path, or -1 if there are no valid paths.
[ "7 4\n4\n1 6 2\n6 2 2\n2 4 2\n2 7 1\n", "4 3\n4\n2 1 2\n1 3 2\n3 4 2\n4 1 1\n" ]
[ "6\n", "3\n" ]
In the first example Oleg visiting banks by path 1 → 6 → 2 → 4. Path 1 → 6 → 2 → 7 with smaller difficulity is incorrect because crossroad 2 → 7 passes near already visited office on the crossroad 6. In the second example Oleg can visit banks by path 4 → 1 → 3.
[ { "input": "7 4\n4\n1 6 2\n6 2 2\n2 4 2\n2 7 1", "output": "6" }, { "input": "4 3\n4\n2 1 2\n1 3 2\n3 4 2\n4 1 1", "output": "3" }, { "input": "3 2\n10\n2 3 290\n3 1 859\n3 1 852\n1 2 232\n1 2 358\n2 1 123\n1 3 909\n2 1 296\n1 3 119\n1 2 584", "output": "119" }, { "input": "3...
77
819,200
0
13,853
717
Cowboy Beblop at his computer
[ "geometry" ]
null
null
Cowboy Beblop is a funny little boy who likes sitting at his computer. He somehow obtained two elastic hoops in the shape of 2D polygons, which are not necessarily convex. Since there's no gravity on his spaceship, the hoops are standing still in the air. Since the hoops are very elastic, Cowboy Beblop can stretch, rotate, translate or shorten their edges as much as he wants. For both hoops, you are given the number of their vertices, as well as the position of each vertex, defined by the X , Y and Z coordinates. The vertices are given in the order they're connected: the 1stβ€―vertex is connected to the 2nd, which is connected to the 3rd, etc., and the last vertex is connected to the first one. Two hoops are connected if it's impossible to pull them to infinity in different directions by manipulating their edges, without having their edges or vertices intersect at any point – just like when two links of a chain are connected. The polygons' edges do not intersect or overlap. To make things easier, we say that two polygons are well-connected, if the edges of one polygon cross the area of the other polygon in two different directions (from the upper and lower sides of the plane defined by that polygon) a different number of times. Cowboy Beblop is fascinated with the hoops he has obtained and he would like to know whether they are well-connected or not. Since he’s busy playing with his dog, Zwei, he’d like you to figure it out for him. He promised you some sweets if you help him!
The first line of input contains an integer *n* (3<=≀<=*n*<=≀<=100<=000), which denotes the number of edges of the first polygon. The next N lines each contain the integers *x*, *y* and *z* (<=-<=1<=000<=000<=≀<=*x*,<=*y*,<=*z*<=≀<=1<=000<=000)Β β€” coordinates of the vertices, in the manner mentioned above. The next line contains an integer *m* (3<=≀<=*m*<=≀<=100<=000) , denoting the number of edges of the second polygon, followed by *m* lines containing the coordinates of the second polygon’s vertices. It is guaranteed that both polygons are simple (no self-intersections), and in general that the obtained polygonal lines do not intersect each other. Also, you can assume that no 3 consecutive points of a polygon lie on the same line.
Your output should contain only one line, with the words "YES" or "NO", depending on whether the two given polygons are well-connected.
[ "4\n0 0 0\n2 0 0\n2 2 0\n0 2 0\n4\n1 1 -1\n1 1 1\n1 3 1\n1 3 -1\n" ]
[ "YES\n" ]
On the picture below, the two polygons are well-connected, as the edges of the vertical polygon cross the area of the horizontal one exactly once in one direction (for example, from above to below), and zero times in the other (in this case, from below to above). Note that the polygons do not have to be parallel to any of the xy-,xz-,yz- planes in general. <img class="tex-graphics" src="https://espresso.codeforces.com/4b5198028f3c57ef65791f641cca363e82b1c219.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "4\n0 0 0\n2 0 0\n2 2 0\n0 2 0\n4\n1 1 -1\n1 1 1\n1 3 1\n1 3 -1", "output": "YES" }, { "input": "4\n4 -2 0\n4 3 0\n-3 3 0\n-3 -2 0\n4\n6 -2 0\n3 2 2\n-3 7 0\n3 4 6", "output": "NO" }, { "input": "4\n-6 6 0\n13 9 0\n15 -7 0\n-5 -5 0\n4\n2 0 4\n2 6 8\n2 12 1\n2 4 -4", "outpu...
124
512,000
0
13,868
822
Hacker, pack your bags!
[ "binary search", "greedy", "implementation", "sortings" ]
null
null
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha. So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly *x* days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that *n* vouchers left. *i*-th voucher is characterized by three integers *l**i*, *r**i*, *cost**i* β€” day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the *i*-th voucher is a value *r**i*<=-<=*l**i*<=+<=1. At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers *i* and *j* (*i*<=β‰ <=*j*) so that they don't intersect, sum of their durations is exactly *x* and their total cost is as minimal as possible. Two vouchers *i* and *j* don't intersect if only at least one of the following conditions is fulfilled: *r**i*<=&lt;<=*l**j* or *r**j*<=&lt;<=*l**i*. Help Leha to choose the necessary vouchers!
The first line contains two integers *n* and *x* (2<=≀<=*n*,<=*x*<=≀<=2Β·105) β€” the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly. Each of the next *n* lines contains three integers *l**i*, *r**i* and *cost**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=2Β·105,<=1<=≀<=*cost**i*<=≀<=109) β€” description of the voucher.
Print a single integer β€” a minimal amount of money that Leha will spend, or print <=-<=1 if it's impossible to choose two disjoint vouchers with the total duration exactly *x*.
[ "4 5\n1 3 4\n1 2 5\n5 6 1\n1 2 4\n", "3 2\n4 6 3\n2 4 1\n3 5 4\n" ]
[ "5\n", "-1\n" ]
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5. In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.
[ { "input": "4 5\n1 3 4\n1 2 5\n5 6 1\n1 2 4", "output": "5" }, { "input": "3 2\n4 6 3\n2 4 1\n3 5 4", "output": "-1" }, { "input": "2 1855\n159106 161198 437057705\n149039 158409 889963913", "output": "-1" }, { "input": "15 17\n1 10 8\n5 19 1\n12 14 6\n9 19 8\n6 7 3\n5 11 9\n...
93
0
0
13,880
45
Dancing Lessons
[ "data structures" ]
C. Dancing Lessons
2
256
There are *n* people taking dancing lessons. Every person is characterized by his/her dancing skill *a**i*. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of *a**i* variable. Your task is to find out what pairs and in what order will start dancing.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of people. The next line contains *n* symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains *n* space-separated integers *a**i* (1<=≀<=*a**i*<=≀<=107) β€” the dancing skill. People are specified from left to right in the order in which they lined up.
Print the resulting number of couples *k*. Then print *k* lines containing two numerals each β€” the numbers of people forming the couple. The people are numbered with integers from 1 to *n* from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
[ "4\nBGBG\n4 2 4 3\n", "4\nBBGG\n4 6 1 5\n", "4\nBGBB\n1 1 2 3\n" ]
[ "2\n3 4\n1 2\n", "2\n2 3\n1 4\n", "1\n1 2\n" ]
none
[ { "input": "4\nBGBG\n4 2 4 3", "output": "2\n3 4\n1 2" }, { "input": "4\nBBGG\n4 6 1 5", "output": "2\n2 3\n1 4" }, { "input": "4\nBGBB\n1 1 2 3", "output": "1\n1 2" }, { "input": "1\nB\n490297", "output": "0" }, { "input": "2\nBB\n2518190 6313112", "output": ...
2,000
34,713,600
0
13,897
629
Far Relative’s Problem
[ "brute force" ]
null
null
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has *n* friends and each of them can come to the party in a specific range of days of the year from *a**i* to *b**i*. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=5000)Β β€” then number of Famil Door's friends. Then follow *n* lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers *a**i* and *b**i* (1<=≀<=*a**i*<=≀<=*b**i*<=≀<=366), providing that the *i*-th friend can come to the party from day *a**i* to day *b**i* inclusive.
Print the maximum number of people that may come to Famil Door's party.
[ "4\nM 151 307\nF 343 352\nF 117 145\nM 24 128\n", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200\n" ]
[ "2\n", "4\n" ]
In the first sample, friends 3 and 4 can come on any day in range [117, 128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
[ { "input": "4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "output": "2" }, { "input": "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200", "output": "4" }, { "input": "1\nF 68 307", "output": "0" }, { "input": "40\nM 55 363\nF 117 252\nM 157 282\nF 322 345\...
202
5,120,000
3
13,937
321
Ciel the Commander
[ "constructive algorithms", "dfs and similar", "divide and conquer", "greedy", "trees" ]
null
null
Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has *n* cities connected by *n*<=-<=1 undirected roads, and for any two cities there always exists a path between them. Fox Ciel needs to assign an officer to each city. Each officer has a rank β€” a letter from 'A' to 'Z'. So there will be 26 different ranks, and 'A' is the topmost, so 'Z' is the bottommost. There are enough officers of each rank. But there is a special rule must obey: if *x* and *y* are two distinct cities and their officers have the same rank, then on the simple path between *x* and *y* there must be a city *z* that has an officer with higher rank. The rule guarantee that a communications between same rank officers will be monitored by higher rank officer. Help Ciel to make a valid plan, and if it's impossible, output "Impossible!".
The first line contains an integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of cities in Tree Land. Each of the following *n*<=-<=1 lines contains two integers *a* and *b* (1<=≀<=*a*,<=*b*<=≀<=*n*,<=*a*<=β‰ <=*b*) β€” they mean that there will be an undirected road between *a* and *b*. Consider all the cities are numbered from 1 to *n*. It guaranteed that the given graph will be a tree.
If there is a valid plane, output *n* space-separated characters in a line β€” *i*-th character is the rank of officer in the city with number *i*. Otherwise output "Impossible!".
[ "4\n1 2\n1 3\n1 4\n", "10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n" ]
[ "A B B B\n", "D C B A D C B D C D\n" ]
In the first example, for any two officers of rank 'B', an officer with rank 'A' will be on the path between them. So it is a valid solution.
[ { "input": "4\n1 2\n1 3\n1 4", "output": "A B B B" }, { "input": "10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10", "output": "D C B A D C B D C D" }, { "input": "6\n1 2\n2 4\n4 5\n6 4\n3 2", "output": "B A B B C C" }, { "input": "2\n2 1", "output": "A B" }, { "i...
278
307,200
0
13,943
622
Optimal Number Permutation
[ "constructive algorithms" ]
null
null
You have array *a* that contains all integers from 1 to *n* twice. You can arbitrary permute any numbers in *a*. Let number *i* be in positions *x**i*,<=*y**i* (*x**i*<=&lt;<=*y**i*) in the permuted array *a*. Let's define the value *d**i*<==<=*y**i*<=-<=*x**i* β€” the distance between the positions of the number *i*. Permute the numbers in array *a* to minimize the value of the sum .
The only line contains integer *n* (1<=≀<=*n*<=≀<=5Β·105).
Print 2*n* integers β€” the permuted array *a* that minimizes the value of the sum *s*.
[ "2\n", "1\n" ]
[ "1 1 2 2\n", "1 1\n" ]
none
[ { "input": "2", "output": "1 1 2 2" }, { "input": "1", "output": "1 1" }, { "input": "3", "output": "1 3 1 2 2 3" }, { "input": "4", "output": "1 3 3 1 2 4 2 4" }, { "input": "10", "output": "1 3 5 7 9 9 7 5 3 1 2 4 6 8 10 8 6 4 2 10" }, { "input": "10...
826
75,264,000
3
13,954
89
Robbery
[ "greedy" ]
A. Robbery
1
256
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has *n* cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to *n* from the left to the right. Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works. Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an *n*<=-<=1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered. Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than *m* times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check. In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only *k* minutes left before morning, and on each of these *k* minutes he can perform no more than *m* operations. All that remains in Joe's pocket, is considered his loot. Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
The first line contains integers *n*, *m* and *k* (1<=≀<=*n*<=≀<=104, 1<=≀<=*m*,<=*k*<=≀<=109). The next line contains *n* numbers. The *i*-th number is equal to the amount of diamonds in the *i*-th cell β€” it is an integer from 0 to 105.
Print a single number β€” the maximum number of diamonds Joe can steal.
[ "2 3 1\n2 3\n", "3 2 2\n4 1 3\n" ]
[ "0", "2" ]
In the second sample Joe can act like this: The diamonds' initial positions are 4 1 3. During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket. By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off. During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket. By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off. Now Joe leaves with 2 diamonds in his pocket.
[ { "input": "2 3 1\n2 3", "output": "0" }, { "input": "3 2 2\n4 1 3", "output": "2" }, { "input": "5 10 10\n7 0 7 0 7", "output": "7" }, { "input": "6 10 4\n1 2 3 4 5 6", "output": "0" }, { "input": "7 5 2\n1 2 3 4 5 6 7", "output": "1" }, { "input": "1...
46
0
0
13,956
128
Numbers
[ "constructive algorithms", "implementation" ]
null
null
One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?
The first line contains an integer *n* β€” how many numbers Anna had (3<=≀<=*n*<=≀<=105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.
Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).
[ "4\n1 2 3 2\n", "6\n1 1 2 2 2 3\n", "6\n2 4 1 1 2 2\n" ]
[ "YES\n", "YES\n", "NO\n" ]
none
[ { "input": "4\n1 2 3 2", "output": "YES" }, { "input": "6\n1 1 2 2 2 3", "output": "YES" }, { "input": "6\n2 4 1 1 2 2", "output": "NO" }, { "input": "4\n999999998 1000000000 999999999 999999999", "output": "YES" }, { "input": "5\n6 7 6 7 6", "output": "NO" ...
122
0
0
13,990
416
Booking System
[ "binary search", "dp", "greedy", "implementation" ]
null
null
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are *n* booking requests received by now. Each request is characterized by two numbers: *c**i* and *p**i* β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all *c**i* people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are *k* tables in the restaurant. For each table, we know *r**i* β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=1000) β€” the number of requests from visitors. Then *n* lines follow. Each line contains two integers: *c**i*,<=*p**i* (1<=≀<=*c**i*,<=*p**i*<=≀<=1000) β€” the size of the group of visitors who will come by the *i*-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer *k* (1<=≀<=*k*<=≀<=1000) β€” the number of tables in the restaurant. The last line contains *k* space-separated integers: *r*1,<=*r*2,<=...,<=*r**k* (1<=≀<=*r**i*<=≀<=1000) β€” the maximum number of people that can sit at each table.
In the first line print two integers: *m*,<=*s* β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print *m* lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them.
[ "3\n10 50\n2 100\n5 30\n3\n4 6 9\n" ]
[ "2 130\n2 1\n3 2\n" ]
none
[ { "input": "3\n10 50\n2 100\n5 30\n3\n4 6 9", "output": "2 130\n2 1\n3 2" }, { "input": "1\n1 1\n1\n1", "output": "1 1\n1 1" }, { "input": "1\n2 1\n1\n1", "output": "0 0" }, { "input": "2\n10 10\n5 5\n1\n5", "output": "1 5\n2 1" }, { "input": "2\n10 10\n5 5\n1\n10...
124
0
0
14,032
0
none
[ "none" ]
null
null
Let's consider one interesting word game. In this game you should transform one word into another through special operations. Let's say we have word *w*, let's split this word into two non-empty parts *x* and *y* so, that *w*<==<=*xy*. A split operation is transforming word *w*<==<=*xy* into word *u*<==<=*yx*. For example, a split operation can transform word "wordcut" into word "cutword". You are given two words *start* and *end*. Count in how many ways we can transform word *start* into word *end*, if we apply exactly *k* split operations consecutively to word *start*. Two ways are considered different if the sequences of applied operations differ. Two operation sequences are different if exists such number *i* (1<=≀<=*i*<=≀<=*k*), that in the *i*-th operation of the first sequence the word splits into parts *x* and *y*, in the *i*-th operation of the second sequence the word splits into parts *a* and *b*, and additionally *x*<=β‰ <=*a* holds.
The first line contains a non-empty word *start*, the second line contains a non-empty word *end*. The words consist of lowercase Latin letters. The number of letters in word *start* equals the number of letters in word *end* and is at least 2 and doesn't exceed 1000 letters. The third line contains integer *k* (0<=≀<=*k*<=≀<=105) β€” the required number of operations.
Print a single number β€” the answer to the problem. As this number can be rather large, print it modulo 1000000007 (109<=+<=7).
[ "ab\nab\n2\n", "ababab\nababab\n1\n", "ab\nba\n2\n" ]
[ "1\n", "2\n", "0\n" ]
The sought way in the first sample is: ab  →  a|b  →  ba  →  b|a  →  ab In the second sample the two sought ways are: - ababab  →  abab|ab  →  ababab - ababab  →  ab|abab  →  ababab
[ { "input": "ab\nab\n2", "output": "1" }, { "input": "ababab\nababab\n1", "output": "2" }, { "input": "ab\nba\n2", "output": "0" }, { "input": "aaa\naaa\n0", "output": "1" }, { "input": "hi\nhi\n1", "output": "0" }, { "input": "abcd\ncbad\n5", "outp...
124
0
0
14,074
1,005
Summarize to the Power of Two
[ "brute force", "greedy", "implementation" ]
null
null
A sequence $a_1, a_2, \dots, a_n$ is called good if, for each element $a_i$, there exists an element $a_j$ ($i \ne j$) such that $a_i+a_j$ is a power of two (that is, $2^d$ for some non-negative integer $d$). For example, the following sequences are good: - $[5, 3, 11]$ (for example, for $a_1=5$ we can choose $a_2=3$. Note that their sum is a power of two. Similarly, such an element can be found for $a_2$ and $a_3$), - $[1, 1, 1, 1023]$, - $[7, 39, 89, 25, 89]$, - $[]$. Note that, by definition, an empty sequence (with a length of $0$) is good. For example, the following sequences are not good: - $[16]$ (for $a_1=16$, it is impossible to find another element $a_j$ such that their sum is a power of two), - $[4, 16]$ (for $a_1=4$, it is impossible to find another element $a_j$ such that their sum is a power of two), - $[1, 3, 2, 8, 8, 8]$ (for $a_3=2$, it is impossible to find another element $a_j$ such that their sum is a power of two). You are given a sequence $a_1, a_2, \dots, a_n$. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
The first line contains the integer $n$ ($1 \le n \le 120000$) β€” the length of the given sequence. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all $n$ elements, make it empty, and thus get a good sequence.
[ "6\n4 7 1 5 4 9\n", "5\n1 2 3 4 5\n", "1\n16\n", "4\n1 1 1 1023\n" ]
[ "1\n", "2\n", "1\n", "0\n" ]
In the first example, it is enough to delete one element $a_4=5$. The remaining elements form the sequence $[4, 7, 1, 4, 9]$, which is good.
[ { "input": "6\n4 7 1 5 4 9", "output": "1" }, { "input": "5\n1 2 3 4 5", "output": "2" }, { "input": "1\n16", "output": "1" }, { "input": "4\n1 1 1 1023", "output": "0" }, { "input": "10\n2 10 9 1 10 4 7 8 5 4", "output": "5" }, { "input": "2\n1 1", ...
3,000
8,089,600
0
14,091
76
Tourist
[ "binary search", "data structures", "dp" ]
F. Tourist
0
256
Tourist walks along the *X* axis. He can choose either of two directions and any speed not exceeding *V*. He can also stand without moving anywhere. He knows from newspapers that at time *t*1 in the point with coordinate *x*1 an interesting event will occur, at time *t*2 in the point with coordinate *x*2 β€” another one, and so on up to (*x**n*,<=*t**n*). Interesting events are short so we can assume they are immediate. Event *i* counts visited if at time *t**i* tourist was at point with coordinate *x**i*. Write program tourist that will find maximum number of events tourist if: - at the beginning (when time is equal to 0) tourist appears at point 0, - tourist can choose initial point for himself. Yes, you should answer on two similar but different questions.
The first line of input contains single integer number *N* (1<=≀<=*N*<=≀<=100000) β€” number of interesting events. The following *N* lines contain two integers *x**i* and *t**i* β€” coordinate and time of the *i*-th event. The last line of the input contains integer *V* β€” maximum speed of the tourist. All *x**i* will be within range <=-<=2Β·108<=≀<=*x**i*<=≀<=2Β·108, all *t**i* will be between 1 and 2Β·106 inclusive. *V* will be positive and will not exceed 1000. The input may contain events that happen at the same time or in the same place but not in the same place at the same time.
The only line of the output should contain two space-sepatated integers β€” maximum number of events tourist can visit in he starts moving from point 0 at time 0, and maximum number of events tourist can visit if he chooses the initial point for himself.
[ "3\n-1 1\n42 7\n40 8\n2\n" ]
[ "1 2\n" ]
none
[ { "input": "3\n-1 1\n42 7\n40 8\n2", "output": "1 2" }, { "input": "5\n1 5\n6 7\n17127 17\n17072 42\n17042 77\n3", "output": "2 3" }, { "input": "7\n-976754 20479\n79929143 911181\n9598220 82517\n-51609349 810257\n67416551 266544\n-14170975 307690\n-15476178 491195\n352", "output": "...
468
31,948,800
0
14,154
992
Nastya Studies Informatics
[ "math", "number theory" ]
null
null
Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well. We define a pair of integers (*a*,<=*b*) good, if *GCD*(*a*,<=*b*)<==<=*x* and *LCM*(*a*,<=*b*)<==<=*y*, where *GCD*(*a*,<=*b*) denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of *a* and *b*, and *LCM*(*a*,<=*b*) denotes the [least common multiple](https://en.wikipedia.org/wiki/Least_common_multiple) of *a* and *b*. You are given two integers *x* and *y*. You are to find the number of good pairs of integers (*a*,<=*b*) such that *l*<=≀<=*a*,<=*b*<=≀<=*r*. Note that pairs (*a*,<=*b*) and (*b*,<=*a*) are considered different if *a*<=β‰ <=*b*.
The only line contains four integers *l*,<=*r*,<=*x*,<=*y* (1<=≀<=*l*<=≀<=*r*<=≀<=109, 1<=≀<=*x*<=≀<=*y*<=≀<=109).
In the only line print the only integerΒ β€” the answer for the problem.
[ "1 2 1 2\n", "1 12 1 12\n", "50 100 3 30\n" ]
[ "2\n", "4\n", "0\n" ]
In the first example there are two suitable good pairs of integers (*a*, *b*): (1, 2) and (2, 1). In the second example there are four suitable good pairs of integers (*a*, *b*): (1, 12), (12, 1), (3, 4) and (4, 3). In the third example there are good pairs of integers, for example, (3, 30), but none of them fits the condition *l* ≀ *a*, *b* ≀ *r*.
[ { "input": "1 2 1 2", "output": "2" }, { "input": "1 12 1 12", "output": "4" }, { "input": "50 100 3 30", "output": "0" }, { "input": "1 1000000000 1 1000000000", "output": "4" }, { "input": "1 1000000000 158260522 200224287", "output": "0" }, { "input...
1,000
1,433,600
0
14,165
295
Greg and Caves
[ "combinatorics", "dp" ]
null
null
Greg has a pad. The pad's screen is an *n*<=Γ—<=*m* rectangle, each cell can be either black or white. We'll consider the pad rows to be numbered with integers from 1 to *n* from top to bottom. Similarly, the pad's columns are numbered with integers from 1 to *m* from left to right. Greg thinks that the pad's screen displays a cave if the following conditions hold: - There is a segment [*l*,<=*r*] (1<=≀<=*l*<=≀<=*r*<=≀<=*n*), such that each of the rows *l*,<=*l*<=+<=1,<=...,<=*r* has exactly two black cells and all other rows have only white cells. - There is a row number *t* (*l*<=≀<=*t*<=≀<=*r*), such that for all pairs of rows with numbers *i* and *j* (*l*<=≀<=*i*<=≀<=*j*<=≀<=*t*) the set of columns between the black cells in row *i* (with the columns where is these black cells) is the subset of the set of columns between the black cells in row *j* (with the columns where is these black cells). Similarly, for all pairs of rows with numbers *i* and *j* (*t*<=≀<=*i*<=≀<=*j*<=≀<=*r*) the set of columns between the black cells in row *j* (with the columns where is these black cells) is the subset of the set of columns between the black cells in row *i* (with the columns where is these black cells). Greg wondered, how many ways there are to paint a cave on his pad. Two ways can be considered distinct if there is a cell that has distinct colors on the two pictures. Help Greg.
The first line contains two integers *n*, *m* β€” the pad's screen size (1<=≀<=*n*,<=*m*<=≀<=2000).
In the single line print the remainder after dividing the answer to the problem by 1000000007 (109<=+<=7).
[ "1 1\n", "4 4\n", "3 5\n" ]
[ "0\n", "485\n", "451\n" ]
none
[ { "input": "1 1", "output": "0" }, { "input": "4 4", "output": "485" }, { "input": "3 5", "output": "451" }, { "input": "5 3", "output": "185" }, { "input": "5 5", "output": "6751" }, { "input": "7 8", "output": "5898445" }, { "input": "9 8...
60
0
0
14,177
446
DZY Loves Modification
[ "brute force", "data structures", "greedy" ]
null
null
As we know, DZY loves playing games. One day DZY decided to play with a *n*<=Γ—<=*m* matrix. To be more precise, he decided to modify the matrix with exactly *k* operations. Each modification is one of the following: 1. Pick some row of the matrix and decrease each element of the row by *p*. This operation brings to DZY the value of pleasure equal to the sum of elements of the row before the decreasing. 1. Pick some column of the matrix and decrease each element of the column by *p*. This operation brings to DZY the value of pleasure equal to the sum of elements of the column before the decreasing. DZY wants to know: what is the largest total value of pleasure he could get after performing exactly *k* modifications? Please, help him to calculate this value.
The first line contains four space-separated integers *n*,<=*m*,<=*k* and *p* (1<=≀<=*n*,<=*m*<=≀<=103;Β 1<=≀<=*k*<=≀<=106;Β 1<=≀<=*p*<=≀<=100). Then *n* lines follow. Each of them contains *m* integers representing *a**ij*Β (1<=≀<=*a**ij*<=≀<=103) β€” the elements of the current row of the matrix.
Output a single integer β€” the maximum possible total pleasure value DZY could get.
[ "2 2 2 2\n1 3\n2 4\n", "2 2 5 2\n1 3\n2 4\n" ]
[ "11\n", "11\n" ]
For the first sample test, we can modify: column 2, row 2. After that the matrix becomes: For the second sample test, we can modify: column 2, row 2, row 1, column 1, column 2. After that the matrix becomes:
[ { "input": "2 2 2 2\n1 3\n2 4", "output": "11" }, { "input": "2 2 5 2\n1 3\n2 4", "output": "11" }, { "input": "5 5 20 100\n464 757 53 708 262\n753 769 189 38 796\n394 60 381 384 935\n882 877 501 615 464\n433 798 504 301 301", "output": "38013" }, { "input": "10 10 50 80\n529...
77
11,980,800
-1
14,181
488
Candy Boxes
[ "brute force", "constructive algorithms", "math" ]
null
null
There is an old tradition of keeping 4 boxes of candies in the house in Cyberland. The numbers of candies are special if their arithmetic mean, their median and their range are all equal. By definition, for a set {*x*1,<=*x*2,<=*x*3,<=*x*4} (*x*1<=≀<=*x*2<=≀<=*x*3<=≀<=*x*4) arithmetic mean is , median is and range is *x*4<=-<=*x*1. The arithmetic mean and median are not necessary integer. It is well-known that if those three numbers are same, boxes will create a "debugging field" and codes in the field will have no bugs. For example, 1,<=1,<=3,<=3 is the example of 4 numbers meeting the condition because their mean, median and range are all equal to 2. Jeff has 4 special boxes of candies. However, something bad has happened! Some of the boxes could have been lost and now there are only *n* (0<=≀<=*n*<=≀<=4) boxes remaining. The *i*-th remaining box contains *a**i* candies. Now Jeff wants to know: is there a possible way to find the number of candies of the 4<=-<=*n* missing boxes, meeting the condition above (the mean, median and range are equal)?
The first line of input contains an only integer *n* (0<=≀<=*n*<=≀<=4). The next *n* lines contain integers *a**i*, denoting the number of candies in the *i*-th box (1<=≀<=*a**i*<=≀<=500).
In the first output line, print "YES" if a solution exists, or print "NO" if there is no solution. If a solution exists, you should output 4<=-<=*n* more lines, each line containing an integer *b*, denoting the number of candies in a missing box. All your numbers *b* must satisfy inequality 1<=≀<=*b*<=≀<=106. It is guaranteed that if there exists a positive integer solution, you can always find such *b*'s meeting the condition. If there are multiple answers, you are allowed to print any of them. Given numbers *a**i* may follow in any order in the input, not necessary in non-decreasing. *a**i* may have stood at any positions in the original set, not necessary on lowest *n* first positions.
[ "2\n1\n1\n", "3\n1\n1\n1\n", "4\n1\n2\n2\n3\n" ]
[ "YES\n3\n3\n", "NO\n", "YES\n" ]
For the first sample, the numbers of candies in 4 boxes can be 1, 1, 3, 3. The arithmetic mean, the median and the range of them are all 2. For the second sample, it's impossible to find the missing number of candies. In the third example no box has been lost and numbers satisfy the condition. You may output *b* in any order.
[ { "input": "2\n1\n1", "output": "YES\n3\n3" }, { "input": "3\n1\n1\n1", "output": "NO" }, { "input": "4\n1\n2\n2\n3", "output": "YES" }, { "input": "0", "output": "YES\n1\n1\n3\n3" }, { "input": "1\n125", "output": "YES\n125\n375\n375" }, { "input": "2...
77
0
3
14,191
864
Cities Excursions
[ "dfs and similar", "graphs", "trees" ]
null
null
There are *n* cities in Berland. Some pairs of them are connected with *m* directed roads. One can use only these roads to move from one city to another. There are no roads that connect a city to itself. For each pair of cities (*x*,<=*y*) there is at most one road from *x* to *y*. A path from city *s* to city *t* is a sequence of cities *p*1, *p*2, ... , *p**k*, where *p*1<==<=*s*, *p**k*<==<=*t*, and there is a road from city *p**i* to city *p**i*<=+<=1 for each *i* from 1 to *k*<=-<=1. The path can pass multiple times through each city except *t*. It can't pass through *t* more than once. A path *p* from *s* to *t* is ideal if it is the lexicographically minimal such path. In other words, *p* is ideal path from *s* to *t* if for any other path *q* from *s* to *t* *p**i*<=&lt;<=*q**i*, where *i* is the minimum integer such that *p**i*<=β‰ <=*q**i*. There is a tourist agency in the country that offers *q* unusual excursions: the *j*-th excursion starts at city *s**j* and ends in city *t**j*. For each pair *s**j*, *t**j* help the agency to study the ideal path from *s**j* to *t**j*. Note that it is possible that there is no ideal path from *s**j* to *t**j*. This is possible due to two reasons: - there is no path from *s**j* to *t**j*; - there are paths from *s**j* to *t**j*, but for every such path *p* there is another path *q* from *s**j* to *t**j*, such that *p**i*<=&gt;<=*q**i*, where *i* is the minimum integer for which *p**i*<=β‰ <=*q**i*. The agency would like to know for the ideal path from *s**j* to *t**j* the *k**j*-th city in that path (on the way from *s**j* to *t**j*). For each triple *s**j*, *t**j*, *k**j* (1<=≀<=*j*<=≀<=*q*) find if there is an ideal path from *s**j* to *t**j* and print the *k**j*-th city in that path, if there is any.
The first line contains three integers *n*, *m* and *q* (2<=≀<=*n*<=≀<=3000,0<=≀<=*m*<=≀<=3000, 1<=≀<=*q*<=≀<=4Β·105) β€” the number of cities, the number of roads and the number of excursions. Each of the next *m* lines contains two integers *x**i* and *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*, *x**i*<=β‰ <=*y**i*), denoting that the *i*-th road goes from city *x**i* to city *y**i*. All roads are one-directional. There can't be more than one road in each direction between two cities. Each of the next *q* lines contains three integers *s**j*, *t**j* and *k**j* (1<=≀<=*s**j*,<=*t**j*<=≀<=*n*, *s**j*<=β‰ <=*t**j*, 1<=≀<=*k**j*<=≀<=3000).
In the *j*-th line print the city that is the *k**j*-th in the ideal path from *s**j* to *t**j*. If there is no ideal path from *s**j* to *t**j*, or the integer *k**j* is greater than the length of this path, print the string '-1' (without quotes) in the *j*-th line.
[ "7 7 5\n1 2\n2 3\n1 3\n3 4\n4 5\n5 3\n4 6\n1 4 2\n2 6 1\n1 7 3\n1 3 2\n1 3 5\n" ]
[ "2\n-1\n-1\n2\n-1\n" ]
none
[ { "input": "7 7 5\n1 2\n2 3\n1 3\n3 4\n4 5\n5 3\n4 6\n1 4 2\n2 6 1\n1 7 3\n1 3 2\n1 3 5", "output": "2\n-1\n-1\n2\n-1" }, { "input": "3 4 5\n1 3\n2 1\n3 1\n3 2\n1 2 1\n2 3 2\n2 3 3\n1 3 1\n3 2 1", "output": "-1\n1\n3\n1\n-1" }, { "input": "2 0 2\n2 1 2\n2 1 1", "output": "-1\n-1" }...
77
819,200
0
14,232
0
none
[ "none" ]
null
null
ΠŸΠΎΠ»ΠΈΠΊΠ°Ρ€ΠΏ ΠΌΠ΅Ρ‡Ρ‚Π°Π΅Ρ‚ ΡΡ‚Π°Ρ‚ΡŒ программистом ΠΈ Ρ„Π°Π½Π°Ρ‚Π΅Π΅Ρ‚ ΠΎΡ‚ стСпСнСй Π΄Π²ΠΎΠΉΠΊΠΈ. Π‘Ρ€Π΅Π΄ΠΈ Π΄Π²ΡƒΡ… чисСл Π΅ΠΌΡƒ большС нравится Ρ‚ΠΎ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ дСлится Π½Π° Π±ΠΎΠ»ΡŒΡˆΡƒΡŽ ΡΡ‚Π΅ΠΏΠ΅Π½ΡŒ числа 2. По Π·Π°Π΄Π°Π½Π½ΠΎΠΉ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ Ρ†Π΅Π»Ρ‹Ρ… ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… чисСл *a*1,<=*a*2,<=...,<=*a**n* трСбуСтся Π½Π°ΠΉΡ‚ΠΈ *r*Β β€” ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½ΡƒΡŽ ΡΡ‚Π΅ΠΏΠ΅Π½ΡŒ числа 2, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ дСлится хотя Π±Ρ‹ ΠΎΠ΄Π½ΠΎ ΠΈΠ· чисСл ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ. ΠšΡ€ΠΎΠΌΠ΅ Ρ‚ΠΎΠ³ΠΎ, трСбуСтся вывСсти количСство чисСл *a**i*, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ дСлятся Π½Π° *r*.
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС записано Ρ†Π΅Π»ΠΎΠ΅ число *n* (1<=≀<=*n*<=≀<=100)Β β€” Π΄Π»ΠΈΠ½Π° ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ *a*. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС записана ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ Ρ†Π΅Π»Ρ‹Ρ… чисСл *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109).
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ Π΄Π²Π° числа: - *r*Β β€” ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½ΡƒΡŽ ΡΡ‚Π΅ΠΏΠ΅Π½ΡŒ Π΄Π²ΠΎΠΉΠΊΠΈ, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ дСлится хотя Π±Ρ‹ ΠΎΠ΄Π½ΠΎ ΠΈΠ· чисСл Π·Π°Π΄Π°Π½Π½ΠΎΠΉ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ, - количСство элСмСнтов ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ дСлятся Π½Π° *r*.
[ "5\n80 7 16 4 48\n", "4\n21 5 3 33\n" ]
[ "16 3\n", "1 4\n" ]
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ тСстовом ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ максимальная ΡΡ‚Π΅ΠΏΠ΅Π½ΡŒ Π΄Π²ΠΎΠΉΠΊΠΈ, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ дСлится хотя Π±Ρ‹ ΠΎΠ΄Π½ΠΎ число, Ρ€Π°Π²Π½Π° 16 = 2<sup class="upper-index">4</sup>, Π½Π° Π½Π΅Ρ‘ дСлятся числа 80, 16 ΠΈ 48. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ тСстовом ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ всС Ρ‡Π΅Ρ‚Ρ‹Ρ€Π΅ числа Π½Π΅Ρ‡Ρ‘Ρ‚Π½Ρ‹Π΅, поэтому дСлятся Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π½Π° 1 = 2<sup class="upper-index">0</sup>. Π­Ρ‚ΠΎ ΠΈ Π±ΡƒΠ΄Π΅Ρ‚ максимальной ΡΡ‚Π΅ΠΏΠ΅Π½ΡŒΡŽ Π΄Π²ΠΎΠΉΠΊΠΈ для Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°.
[ { "input": "5\n80 7 16 4 48", "output": "16 3" }, { "input": "4\n21 5 3 33", "output": "1 4" }, { "input": "10\n8 112 52 86 93 102 24 24 100 826791168", "output": "256 1" }, { "input": "3\n458297759 18 104", "output": "8 1" }, { "input": "7\n12 14 40 8 74 104 11",...
62
4,608,000
0
14,299
393
Three matrices
[]
null
null
Chubby Yang is studying linear equations right now. He came up with a nice problem. In the problem you are given an *n*<=Γ—<=*n* matrix *W*, consisting of integers, and you should find two *n*<=Γ—<=*n* matrices *A* and *B*, all the following conditions must hold: - *A**ij*<==<=*A**ji*, for all *i*,<=*j* (1<=≀<=*i*,<=*j*<=≀<=*n*); - *B**ij*<==<=<=-<=*B**ji*, for all *i*,<=*j* (1<=≀<=*i*,<=*j*<=≀<=*n*); - *W**ij*<==<=*A**ij*<=+<=*B**ij*, for all *i*,<=*j* (1<=≀<=*i*,<=*j*<=≀<=*n*). Can you solve the problem?
The first line contains an integer *n* (1<=≀<=*n*<=≀<=170). Each of the following *n* lines contains *n* integers. The *j*-th integer in the *i*-th line is *W**ij* (0<=≀<=|*W**ij*|<=&lt;<=1717).
The first *n* lines must contain matrix *A*. The next *n* lines must contain matrix *B*. Print the matrices in the format equal to format of matrix *W* in input. It is guaranteed that the answer exists. If there are multiple answers, you are allowed to print any of them. The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=4.
[ "2\n1 4\n3 2\n", "3\n1 2 3\n4 5 6\n7 8 9\n" ]
[ "1.00000000 3.50000000\n3.50000000 2.00000000\n0.00000000 0.50000000\n-0.50000000 0.00000000\n", "1.00000000 3.00000000 5.00000000\n3.00000000 5.00000000 7.00000000\n5.00000000 7.00000000 9.00000000\n0.00000000 -1.00000000 -2.00000000\n1.00000000 0.00000000 -1.00000000\n2.00000000 1.00000000 0.00000000\n" ]
none
[ { "input": "2\n1 4\n3 2", "output": "1.00000000 3.50000000\n3.50000000 2.00000000\n0.00000000 0.50000000\n-0.50000000 0.00000000" }, { "input": "3\n1 2 3\n4 5 6\n7 8 9", "output": "1.00000000 3.00000000 5.00000000\n3.00000000 5.00000000 7.00000000\n5.00000000 7.00000000 9.00000000\n0.00000000 -1...
233
2,457,600
3
14,309
242
Heads or Tails
[ "brute force", "implementation" ]
null
null
Petya and Vasya are tossing a coin. Their friend Valera is appointed as a judge. The game is very simple. First Vasya tosses a coin *x* times, then Petya tosses a coin *y* times. If the tossing player gets head, he scores one point. If he gets tail, nobody gets any points. The winner is the player with most points by the end of the game. If boys have the same number of points, the game finishes with a draw. At some point, Valera lost his count, and so he can not say exactly what the score is at the end of the game. But there are things he remembers for sure. He remembers that the entire game Vasya got heads at least *a* times, and Petya got heads at least *b* times. Moreover, he knows that the winner of the game was Vasya. Valera wants to use this information to know every possible outcome of the game, which do not contradict his memories.
The single line contains four integers *x*,<=*y*,<=*a*,<=*b* (1<=≀<=*a*<=≀<=*x*<=≀<=100,<=1<=≀<=*b*<=≀<=*y*<=≀<=100). The numbers on the line are separated by a space.
In the first line print integer *n* β€” the number of possible outcomes of the game. Then on *n* lines print the outcomes. On the *i*-th line print a space-separated pair of integers *c**i*, *d**i* β€” the number of heads Vasya and Petya got in the *i*-th outcome of the game, correspondingly. Print pairs of integers (*c**i*,<=*d**i*) in the strictly increasing order. Let us remind you that the pair of numbers (*p*1,<=*q*1) is less than the pair of numbers (*p*2,<=*q*2), if *p*1<=&lt;<=*p*2, or *p*1<==<=*p*2 and also *q*1<=&lt;<=*q*2.
[ "3 2 1 1\n", "2 4 2 2\n" ]
[ "3\n2 1\n3 1\n3 2\n", "0\n" ]
none
[ { "input": "3 2 1 1", "output": "3\n2 1\n3 1\n3 2" }, { "input": "2 4 2 2", "output": "0" }, { "input": "1 1 1 1", "output": "0" }, { "input": "4 5 2 3", "output": "1\n4 3" }, { "input": "10 6 3 4", "output": "15\n5 4\n6 4\n6 5\n7 4\n7 5\n7 6\n8 4\n8 5\n8 6\n9...
122
0
3
14,330
479
Towers
[ "brute force", "constructive algorithms", "greedy", "implementation", "sortings" ]
null
null
As you know, all the kids in Berland love playing with cubes. Little Petya has *n* towers consisting of cubes of the same size. Tower with number *i* consists of *a**i* cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than *k* such operations. Petya does not want to be late for class, so you have to help him accomplish this task.
The first line contains two space-separated positive integers *n* and *k* (1<=≀<=*n*<=≀<=100, 1<=≀<=*k*<=≀<=1000) β€” the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains *n* space-separated positive integers *a**i* (1<=≀<=*a**i*<=≀<=104) β€” the towers' initial heights.
In the first line print two space-separated non-negative integers *s* and *m* (*m*<=≀<=*k*). The first number is the value of the minimum possible instability that can be obtained after performing at most *k* operations, the second number is the number of operations needed for that. In the next *m* lines print the description of each operation as two positive integers *i* and *j*, each of them lies within limits from 1 to *n*. They represent that Petya took the top cube from the *i*-th tower and put in on the *j*-th one (*i*<=β‰ <=*j*). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.
[ "3 2\n5 8 5\n", "3 4\n2 2 4\n", "5 3\n8 3 2 6 3\n" ]
[ "0 2\n2 1\n2 3\n", "1 1\n3 2\n", "3 3\n1 3\n1 2\n1 3\n" ]
In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.
[ { "input": "3 2\n5 8 5", "output": "0 2\n2 1\n2 3" }, { "input": "3 4\n2 2 4", "output": "1 4\n3 1\n1 2\n2 1\n1 2" }, { "input": "5 3\n8 3 2 6 3", "output": "3 3\n1 3\n1 2\n1 3" }, { "input": "4 6\n1 10 8 2", "output": "2 6\n2 1\n2 1\n2 4\n3 1\n2 4\n3 1" }, { "inp...
1,000
2,457,600
0
14,350
0
none
[ "none" ]
null
null
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane *n* rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length
The first line contains single integer *n* (1<=≀<=*n*<=≀<=5Β·105)Β β€” the number of rectangles. *n* lines follow. The *i*-th of these lines contains four integers *x*1, *y*1, *x*2 and *y*2 (<=-<=109<=≀<=*x*1<=&lt;<=*x*2<=≀<=109, <=-<=109<=≀<=*y*1<=&lt;<=*y*2<=≀<=109), that means that points (*x*1,<=*y*1) and (*x*2,<=*y*2) are the coordinates of two opposite corners of the *i*-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.
Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print *n* lines, in the *i*-th of them print single integer *c**i* (1<=≀<=*c**i*<=≀<=4)Β β€” the color of *i*-th rectangle.
[ "8\n0 0 5 3\n2 -1 5 0\n-3 -4 2 -1\n-1 -1 2 0\n-3 0 0 5\n5 2 10 3\n7 -3 10 2\n4 -2 7 -1\n" ]
[ "YES\n1\n2\n2\n3\n2\n2\n4\n1\n" ]
none
[ { "input": "8\n0 0 5 3\n2 -1 5 0\n-3 -4 2 -1\n-1 -1 2 0\n-3 0 0 5\n5 2 10 3\n7 -3 10 2\n4 -2 7 -1", "output": "YES\n1\n4\n3\n2\n3\n3\n2\n1" }, { "input": "1\n0 0 1 1", "output": "YES\n1" }, { "input": "4\n0 0 1 1\n1 0 2 1\n1 1 2 2\n0 1 1 2", "output": "YES\n1\n3\n4\n2" }, { "...
1,513
4,608,000
3
14,361
16
Logging
[ "implementation", "strings" ]
D. Logging
1
64
The main server of Gomble company received a log of one top-secret process, the name of which can't be revealed. The log was written in the following format: Β«[date:time]: messageΒ», where for each Β«[date:time]Β» value existed not more than 10 lines. All the files were encoded in a very complicated manner, and only one programmer β€” Alex β€” managed to decode them. The code was so complicated that Alex needed four weeks to decode it. Right after the decoding process was finished, all the files were deleted. But after the files deletion, Alex noticed that he saved the recordings in format Β«[time]: messageΒ». So, information about the dates was lost. However, as the lines were added into the log in chronological order, it's not difficult to say if the recordings could appear during one day or not. It is possible also to find the minimum amount of days during which the log was written. So, to make up for his mistake Alex has to find the minimum amount of days covered by the log. Note that Alex doesn't have to find the minimum amount of days between the beginning and the end of the logging, he has to find the minimum amount of dates in which records could be done. (See Sample test 2 for further clarifications). We should remind you that the process made not more than 10 recordings in a minute. Consider that a midnight belongs to coming day.
The first input line contains number *n* (1<=≀<=*n*<=≀<=100). The following *n* lines contain recordings in format Β«[time]: messageΒ», where time is given in format Β«hh:mm x.m.Β». For hh two-digit numbers from 01 to 12 are used, for mm two-digit numbers from 00 to 59 are used, and x is either character Β«aΒ» or character Β«pΒ». A message is a non-empty sequence of Latin letters and/or spaces, it doesn't start or end with a space. The length of each message doesn't exceed 20.
Output one number β€” the minimum amount of days covered by the log.
[ "5\n[05:00 a.m.]: Server is started\n[05:00 a.m.]: Rescan initialized\n[01:13 p.m.]: Request processed\n[01:10 p.m.]: Request processed\n[11:40 p.m.]: Rescan completed\n", "3\n[09:00 a.m.]: User logged in\n[08:00 a.m.]: User logged in\n[07:00 a.m.]: User logged in\n" ]
[ "2\n", "3\n" ]
Formally the 12-hour time format is described at: - http://en.wikipedia.org/wiki/12-hour_clock.
[ { "input": "5\n[05:00 a.m.]: Server is started\n[05:00 a.m.]: Rescan initialized\n[01:13 p.m.]: Request processed\n[01:10 p.m.]: Request processed\n[11:40 p.m.]: Rescan completed", "output": "2" }, { "input": "3\n[09:00 a.m.]: User logged in\n[08:00 a.m.]: User logged in\n[07:00 a.m.]: User logged i...
109
307,200
3.943211
14,370
997
Roman Digits
[ "brute force", "combinatorics", "dp", "greedy", "math" ]
null
null
Let's introduce a number system which is based on a roman digits. There are digits I, V, X, L which correspond to the numbers $1$, $5$, $10$ and $50$ respectively. The use of other roman digits is not allowed. Numbers in this system are written as a sequence of one or more digits. We define the value of the sequence simply as the sum of digits in it. For example, the number XXXV evaluates to $35$ and the number IXIΒ β€” to $12$. Pay attention to the difference to the traditional roman systemΒ β€” in our system any sequence of digits is valid, moreover the order of digits doesn't matter, for example IX means $11$, not $9$. One can notice that this system is ambiguous, and some numbers can be written in many different ways. Your goal is to determine how many distinct integers can be represented by exactly $n$ roman digits I, V, X, L.
The only line of the input file contains a single integer $n$ ($1 \le n \le 10^9$)Β β€” the number of roman digits to use.
Output a single integerΒ β€” the number of distinct integers which can be represented using $n$ roman digits exactly.
[ "1\n", "2\n", "10\n" ]
[ "4\n", "10\n", "244\n" ]
In the first sample there are exactly $4$ integers which can be representedΒ β€” I, V, X and L. In the second sample it is possible to represent integers $2$ (II), $6$ (VI), $10$ (VV), $11$ (XI), $15$ (XV), $20$ (XX), $51$ (IL), $55$ (VL), $60$ (XL) and $100$ (LL).
[ { "input": "1", "output": "4" }, { "input": "2", "output": "10" }, { "input": "10", "output": "244" }, { "input": "1000", "output": "48753" }, { "input": "2000", "output": "97753" }, { "input": "5000", "output": "244753" }, { "input": "1000...
93
0
-1
14,389
555
Case of Fugitive
[ "data structures", "greedy", "sortings" ]
null
null
Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an archipelago of *n* narrow islands located in a row. For more comfort let's represent them as non-intersecting segments on a straight line: island *i* has coordinates [*l**i*,<=*r**i*], besides, *r**i*<=&lt;<=*l**i*<=+<=1 for 1<=≀<=*i*<=≀<=*n*<=-<=1. To reach the goal, Andrewid needs to place a bridge between each pair of adjacent islands. A bridge of length *a* can be placed between the *i*-th and the (*i*<=+<=1)-th islads, if there are such coordinates of *x* and *y*, that *l**i*<=≀<=*x*<=≀<=*r**i*, *l**i*<=+<=1<=≀<=*y*<=≀<=*r**i*<=+<=1 and *y*<=-<=*x*<==<=*a*. The detective was supplied with *m* bridges, each bridge can be used at most once. Help him determine whether the bridges he got are enough to connect each pair of adjacent islands.
The first line contains integers *n* (2<=≀<=*n*<=≀<=2Β·105) and *m* (1<=≀<=*m*<=≀<=2Β·105) β€” the number of islands and bridges. Next *n* lines each contain two integers *l**i* and *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=1018) β€” the coordinates of the island endpoints. The last line contains *m* integer numbers *a*1,<=*a*2,<=...,<=*a**m* (1<=≀<=*a**i*<=≀<=1018) β€” the lengths of the bridges that Andrewid got.
If it is impossible to place a bridge between each pair of adjacent islands in the required manner, print on a single line "No" (without the quotes), otherwise print in the first line "Yes" (without the quotes), and in the second line print *n*<=-<=1 numbers *b*1,<=*b*2,<=...,<=*b**n*<=-<=1, which mean that between islands *i* and *i*<=+<=1 there must be used a bridge number *b**i*. If there are multiple correct answers, print any of them. Note that in this problem it is necessary to print "Yes" and "No" in correct case.
[ "4 4\n1 4\n7 8\n9 10\n12 14\n4 5 3 8\n", "2 2\n11 14\n17 18\n2 9\n", "2 1\n1 1\n1000000000000000000 1000000000000000000\n999999999999999999\n" ]
[ "Yes\n2 3 1 \n", "No\n", "Yes\n1 \n" ]
In the first sample test you can, for example, place the second bridge between points 3 and 8, place the third bridge between points 7 and 10 and place the first bridge between points 10 and 14. In the second sample test the first bridge is too short and the second bridge is too long, so the solution doesn't exist.
[ { "input": "4 4\n1 4\n7 8\n9 10\n12 14\n4 5 3 8", "output": "Yes\n2 3 1 " }, { "input": "2 2\n11 14\n17 18\n2 9", "output": "No" }, { "input": "2 1\n1 1\n1000000000000000000 1000000000000000000\n999999999999999999", "output": "Yes\n1 " }, { "input": "5 10\n1 2\n3 3\n5 7\n11 1...
3,000
58,777,600
0
14,405
980
Perfect Groups
[ "dp", "math", "number theory" ]
null
null
SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be divided into, such that the product of any pair of integers in the same group is a perfect square. Each integer must be in exactly one group. However, integers in a group do not necessarily have to be contiguous in the array. SaMer wishes to create more cases from the test case he already has. His test case has an array $A$ of $n$ integers, and he needs to find the number of contiguous subarrays of $A$ that have an answer to the problem equal to $k$ for each integer $k$ between $1$ and $n$ (inclusive).
The first line of input contains a single integer $n$ ($1 \leq n \leq 5000$), the size of the array. The second line contains $n$ integers $a_1$,$a_2$,$\dots$,$a_n$ ($-10^8 \leq a_i \leq 10^8$), the values of the array.
Output $n$ space-separated integers, the $k$-th integer should be the number of contiguous subarrays of $A$ that have an answer to the problem equal to $k$.
[ "2\n5 5\n", "5\n5 -4 2 1 8\n", "1\n0\n" ]
[ "3 0\n", "5 5 3 2 0\n", "1\n" ]
none
[ { "input": "2\n5 5", "output": "3 0" }, { "input": "5\n5 -4 2 1 8", "output": "5 5 3 2 0" }, { "input": "1\n0", "output": "1" }, { "input": "3\n-10 -5 7", "output": "3 2 1" }, { "input": "5\n-6 -7 -2 -3 -10", "output": "5 4 3 2 1" }, { "input": "8\n-5 ...
46
0
0
14,412
1,009
Relatively Prime Graph
[ "brute force", "constructive algorithms", "graphs", "greedy", "math" ]
null
null
Let's call an undirected graph $G = (V, E)$ relatively prime if and only if for each edge $(v, u) \in E$ Β $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$. Construct a relatively prime graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges. If there exists no valid graph with the given number of vertices and edges then output "Impossible". If there are multiple answers then print any of them.
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) β€” the number of vertices and the number of edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible". Otherwise print the answer in the following format: The first line should contain the word "Possible". The $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$. If there are multiple answers then print any of them.
[ "5 6\n", "6 12\n" ]
[ "Possible\n2 5\n3 2\n5 1\n3 4\n4 1\n5 4\n", "Impossible\n" ]
Here is the representation of the graph from the first example: <img class="tex-graphics" src="https://espresso.codeforces.com/7a1353a992545456c007e3071fa0a06fe46fc64e.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "5 6", "output": "Possible\n2 1\n3 1\n4 1\n5 1\n3 2\n5 2" }, { "input": "6 12", "output": "Impossible" }, { "input": "572 99643", "output": "Possible\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1...
30
0
0
14,434
101
Homework
[ "greedy" ]
A. Homework
2
256
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...
216
0
-1
14,449
0
none
[ "none" ]
null
null
Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string *s*. Each character of *s* is one move operation. There are four move operations at all: - 'U': go up, (x, y) <=β†’<= (x, y+1); - 'D': go down, (x, y) <=β†’<= (x, y-1); - 'L': go left, (x, y) <=β†’<= (x-1, y); - 'R': go right, (x, y) <=β†’<= (x+1, y). The robot will do the operations in *s* from left to right, and repeat it infinite times. Help Fox Ciel to determine if after some steps the robot will located in (*a*,<=*b*).
The first line contains two integers *a* and *b*, (<=-<=109<=≀<=*a*,<=*b*<=≀<=109). The second line contains a string *s* (1<=≀<=|*s*|<=≀<=100, *s* only contains characters 'U', 'D', 'L', 'R') β€” the command.
Print "Yes" if the robot will be located at (*a*,<=*b*), and "No" otherwise.
[ "2 2\nRU\n", "1 2\nRU\n", "-1 1000000000\nLRRLU\n", "0 0\nD\n" ]
[ "Yes\n", "No\n", "Yes\n", "Yes\n" ]
In the first and second test case, command string is "RU", so the robot will go right, then go up, then right, and then up and so on. The locations of its moves are (0, 0)  →  (1, 0)  →  (1, 1)  →  (2, 1)  →  (2, 2)  →  ... So it can reach (2, 2) but not (1, 2).
[ { "input": "2 2\nRU", "output": "Yes" }, { "input": "1 2\nRU", "output": "No" }, { "input": "-1 1000000000\nLRRLU", "output": "Yes" }, { "input": "0 0\nD", "output": "Yes" }, { "input": "0 0\nUURRDL", "output": "Yes" }, { "input": "987654321 987654321\...
62
102,400
0
14,455
301
Yaroslav and Arrangements
[ "dp" ]
null
null
Yaroslav calls an array of *r* integers *a*1,<=*a*2,<=...,<=*a**r* good, if it meets the following conditions: |*a*1<=-<=*a*2|<==<=1,<=|*a*2<=-<=*a*3|<==<=1,<=...,<=|*a**r*<=-<=1<=-<=*a**r*|<==<=1,<=|*a**r*<=-<=*a*1|<==<=1, at that . An array of integers *b*1,<=*b*2,<=...,<=*b**r* is called great, if it meets the following conditions: 1. The elements in it do not decrease (*b**i*<=≀<=*b**i*<=+<=1). 1. If the inequalities 1<=≀<=*r*<=≀<=*n* and 1<=≀<=*b**i*<=≀<=*m* hold. 1. If we can rearrange its elements and get at least one and at most *k* distinct good arrays. Yaroslav has three integers *n*,<=*m*,<=*k*. He needs to count the number of distinct great arrays. Help Yaroslav! As the answer may be rather large, print the remainder after dividing it by 1000000007 (109<=+<=7). Two arrays are considered distinct if there is a position in which they have distinct numbers.
The single line contains three integers *n*, *m*, *k* (1<=≀<=*n*,<=*m*,<=*k*<=≀<=100).
In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109<=+<=7).
[ "1 1 1\n", "3 3 3\n" ]
[ "0\n", "2\n" ]
none
[]
374
19,148,800
0
14,456
271
Good Substrings
[ "data structures", "strings" ]
null
null
You've got string *s*, consisting of small English letters. Some of the English letters are good, the rest are bad. A substring *s*[*l*...*r*] (1<=≀<=*l*<=≀<=*r*<=≀<=|*s*|) of string *s*<=<==<=<=*s*1*s*2...*s*|*s*| (where |*s*| is the length of string *s*) is string <=*s**l**s**l*<=+<=1...*s**r*. The substring *s*[*l*...*r*] is good, if among the letters <=*s**l*,<=*s**l*<=+<=1,<=...,<=*s**r* there are at most *k* bad ones (look at the sample's explanation to understand it more clear). Your task is to find the number of distinct good substrings of the given string *s*. Two substrings *s*[*x*...*y*] and *s*[*p*...*q*] are considered distinct if their content is different, i.e. *s*[*x*...*y*]<=β‰ <=*s*[*p*...*q*].
The first line of the input is the non-empty string *s*, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the *i*-th character of this string equals "1", then the *i*-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer *k* (0<=≀<=*k*<=≀<=|*s*|) β€” the maximum acceptable number of bad characters in a good substring.
Print a single integer β€” the number of distinct good substrings of string *s*.
[ "ababab\n01000000000000000000000000\n1\n", "acbacbacaa\n00000000000000000000000000\n2\n" ]
[ "5\n", "8\n" ]
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab". In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
[ { "input": "ababab\n01000000000000000000000000\n1", "output": "5" }, { "input": "acbacbacaa\n00000000000000000000000000\n2", "output": "8" }, { "input": "a\n00000000000000000000000000\n0", "output": "0" }, { "input": "aaaa\n00000000000000000000000000\n0", "output": "0" ...
154
4,608,000
3
14,468
803
Magazine Ad
[ "binary search", "greedy" ]
null
null
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this: There are space-separated non-empty words of lowercase and uppercase Latin letters. There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen. It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word. When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding. The ad can occupy no more that *k* lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it. You should write a program that will find minimal width of the ad.
The first line contains number *k* (1<=≀<=*k*<=≀<=105). The second line contains the text of the ad β€” non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters.
Output minimal width of the ad.
[ "4\ngarage for sa-le\n", "4\nEdu-ca-tion-al Ro-unds are so fun\n" ]
[ "7\n", "10\n" ]
Here all spaces are replaced with dots. In the first example one of possible results after all word wraps looks like this: The second example:
[ { "input": "4\ngarage for sa-le", "output": "7" }, { "input": "4\nEdu-ca-tion-al Ro-unds are so fun", "output": "10" }, { "input": "1\nj", "output": "1" }, { "input": "10\nb", "output": "1" }, { "input": "1\nQGVsfZevMD", "output": "10" }, { "input": "1...
1,000
13,004,800
0
14,506
958
Lightsabers (medium)
[ "binary search", "two pointers" ]
null
null
There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. Heidi has *n* Jedi Knights standing in front of her, each one with a lightsaber of one of *m* possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly *k*1 knights with lightsabers of the first color, *k*2 knights with lightsabers of the second color, ..., *k**m* knights with lightsabers of the *m*-th color. However, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights.
The first line of the input contains *n* (1<=≀<=*n*<=≀<=2Β·105) and *m* (1<=≀<=*m*<=≀<=*n*). The second line contains *n* integers in the range {1,<=2,<=...,<=*m*} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains *m* integers *k*1,<=*k*2,<=...,<=*k**m* (with ) – the desired counts of Jedi Knights with lightsabers of each color from 1 to *m*.
Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output <=-<=1.
[ "8 3\n3 3 1 2 2 1 1 3\n3 1 1\n" ]
[ "1\n" ]
none
[ { "input": "8 3\n3 3 1 2 2 1 1 3\n3 1 1", "output": "1" }, { "input": "6 5\n1 2 4 2 4 3\n0 0 1 0 0", "output": "0" }, { "input": "1 1\n1\n1", "output": "0" }, { "input": "2 1\n1 1\n1", "output": "0" }, { "input": "2 1\n1 1\n2", "output": "0" }, { "inpu...
46
614,400
0
14,533
286
Main Sequence
[ "greedy", "implementation" ]
null
null
As you know, Vova has recently become a new shaman in the city of Ultima Thule. So, he has received the shaman knowledge about the correct bracket sequences. The shamans of Ultima Thule have been using lots of different types of brackets since prehistoric times. A bracket type is a positive integer. The shamans define a correct bracket sequence as follows: - An empty sequence is a correct bracket sequence. - If {*a*1,<=*a*2,<=...,<=*a**l*} and {*b*1,<=*b*2,<=...,<=*b**k*} are correct bracket sequences, then sequence {*a*1,<=*a*2,<=...,<=*a**l*,<=*b*1,<=*b*2,<=...,<=*b**k*} (their concatenation) also is a correct bracket sequence. - If {*a*1,<=*a*2,<=...,<=*a**l*} β€” is a correct bracket sequence, then sequence also is a correct bracket sequence, where *v* (*v*<=&gt;<=0) is an integer. For example, sequences {1,<=1,<=<=-<=1,<=2,<=<=-<=2,<=<=-<=1} and {3,<=<=-<=3} are correct bracket sequences, and {2,<=<=-<=3} is not. Moreover, after Vova became a shaman, he learned the most important correct bracket sequence {*x*1,<=*x*2,<=...,<=*x**n*}, consisting of *n* integers. As sequence *x* is the most important, Vova decided to encrypt it just in case. Encrypting consists of two sequences. The first sequence {*p*1,<=*p*2,<=...,<=*p**n*} contains types of brackets, that is, *p**i*<==<=|*x**i*| (1<=≀<=*i*<=≀<=*n*). The second sequence {*q*1,<=*q*2,<=...,<=*q**t*} contains *t* integers β€” some positions (possibly, not all of them), which had negative numbers in sequence {*x*1,<=*x*2,<=...,<=*x**n*}. Unfortunately, Vova forgot the main sequence. But he was lucky enough to keep the encryption: sequences {*p*1,<=*p*2,<=...,<=*p**n*} and {*q*1,<=*q*2,<=...,<=*q**t*}. Help Vova restore sequence *x* by the encryption. If there are multiple sequences that correspond to the encryption, restore any of them. If there are no such sequences, you should tell so.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=106). The second line contains *n* integers: *p*1,<=*p*2,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=109). The third line contains integer *t* (0<=≀<=*t*<=≀<=*n*), followed by *t* distinct integers *q*1,<=*q*2,<=...,<=*q**t* (1<=≀<=*q**i*<=≀<=*n*). The numbers in each line are separated by spaces.
Print a single string "NO" (without the quotes) if Vova is mistaken and a suitable sequence {*x*1,<=*x*2,<=...,<=*x**n*} doesn't exist. Otherwise, in the first line print "YES" (without the quotes) and in the second line print *n* integers *x*1,<=*x*2,<=...,<=*x**n* (|*x**i*|<==<=*p**i*;Β *x**q**j*<=&lt;<=0). If there are multiple sequences that correspond to the encrypting, you are allowed to print any of them.
[ "2\n1 1\n0\n", "4\n1 1 1 1\n1 3\n", "3\n1 1 1\n0\n", "4\n1 2 2 1\n2 3 4\n" ]
[ "YES\n1 -1\n", "YES\n1 1 -1 -1\n", "NO\n", "YES\n1 2 -2 -1\n" ]
none
[]
2,000
60,620,800
0
14,548
233
Non-square Equation
[ "binary search", "brute force", "math" ]
null
null
Let's consider equation: where *x*,<=*n* are positive integers, *s*(*x*) is the function, equal to the sum of digits of number *x* in the decimal number system. You are given an integer *n*, find the smallest positive integer root of equation *x*, or else determine that there are no such roots.
A single line contains integer *n* (1<=≀<=*n*<=≀<=1018) β€” the equation parameter. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Print -1, if the equation doesn't have integer positive roots. Otherwise print such smallest integer *x* (*x*<=&gt;<=0), that the equation given in the statement holds.
[ "2\n", "110\n", "4\n" ]
[ "1\n", "10\n", "-1\n" ]
In the first test case *x* = 1 is the minimum root. As *s*(1) = 1 and 1<sup class="upper-index">2</sup> + 1Β·1 - 2 = 0. In the second test case *x* = 10 is the minimum root. As *s*(10) = 1 + 0 = 1 and 10<sup class="upper-index">2</sup> + 1Β·10 - 110 = 0. In the third test case the equation has no roots.
[ { "input": "2", "output": "1" }, { "input": "110", "output": "10" }, { "input": "4", "output": "-1" }, { "input": "8", "output": "2" }, { "input": "10000000100000000", "output": "100000000" }, { "input": "10000006999999929", "output": "99999999" ...
186
1,433,600
3
14,553
549
Looksery Party
[ "constructive algorithms", "dfs and similar", "graphs", "greedy" ]
null
null
The Looksery company, consisting of *n* staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself. Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates *n* numbers, the *i*-th of which indicates how many messages, in his view, the *i*-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins. You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of employees of company Looksery. Next *n* lines contain the description of the contact lists of the employees. The *i*-th of these lines contains a string of length *n*, consisting of digits zero and one, specifying the contact list of the *i*-th employee. If the *j*-th character of the *i*-th string equals 1, then the *j*-th employee is in the *i*-th employee's contact list, otherwise he isn't. It is guaranteed that the *i*-th character of the *i*-th line is always equal to 1. The last line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=*n*), where *a**i* represents the number of messages that the *i*-th employee should get according to Igor.
In the first line print a single integer *m* β€” the number of employees who should come to the party so that Igor loses the dispute. In the second line print *m* space-separated integers β€” the numbers of these employees in an arbitrary order. If Igor wins the dispute in any case, print -1. If there are multiple possible solutions, print any of them.
[ "3\n101\n010\n001\n0 1 2\n", "1\n1\n1\n", "4\n1111\n0101\n1110\n0001\n1 0 1 0\n" ]
[ "1\n1 \n", "0\n\n", "4\n1 2 3 4 \n" ]
In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number. In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it. In the third sample the first employee will receive 2 messages, the second β€” 3, the third β€” 2, the fourth β€” 3.
[ { "input": "3\n101\n010\n001\n0 1 2", "output": "1\n1 " }, { "input": "1\n1\n1", "output": "0" }, { "input": "4\n1111\n0101\n1110\n0001\n1 0 1 0", "output": "4\n1 2 3 4 " }, { "input": "2\n11\n01\n0 2", "output": "1\n1 " }, { "input": "5\n10110\n01110\n00101\n0001...
0
0
-1
14,567
241
Flights
[ "graphs", "shortest paths" ]
null
null
LiLand is a country, consisting of *n* cities. The cities are numbered from 1 to *n*. The country is well known because it has a very strange transportation system. There are many one-way flights that make it possible to travel between the cities, but the flights are arranged in a way that once you leave a city you will never be able to return to that city again. Previously each flight took exactly one hour, but recently Lily has become the new manager of transportation system and she wants to change the duration of some flights. Specifically, she wants to change the duration of some flights to exactly 2 hours in such a way that all trips from city 1 to city *n* take the same time regardless of their path. Your task is to help Lily to change the duration of flights.
First line of the input contains two integer numbers *n* and *m* (2<=≀<=*n*<=≀<=1000;Β 1<=≀<=*m*<=≀<=5000) specifying the number of cities and the number of flights. Each of the next *m* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*<=&lt;<=*b**i*<=≀<=*n*) specifying a one-directional flight from city *a**i* to city *b**i*. It is guaranteed that there exists a way to travel from city number 1 to city number *n* using the given flights. It is guaranteed that there is no sequence of flights that forms a cyclical path and no two flights are between the same pair of cities.
If it is impossible for Lily to do her task, print "No" (without quotes) on the only line of the output. Otherwise print "Yes" (without quotes) on the first line of output, then print an integer *ans**i* (1<=≀<=*ans**i*<=≀<=2) to each of the next *m* lines being the duration of flights in new transportation system. You should print these numbers in the order that flights are given in the input. If there are multiple solutions for the input, output any of them.
[ "3 3\n1 2\n2 3\n1 3\n", "4 4\n1 2\n2 3\n3 4\n1 4\n", "5 6\n1 2\n2 3\n3 5\n1 4\n4 5\n1 3\n" ]
[ "Yes\n1\n1\n2\n", "No\n", "Yes\n1\n1\n1\n2\n1\n2\n" ]
none
[ { "input": "3 3\n1 2\n2 3\n1 3", "output": "Yes\n1\n1\n2" }, { "input": "4 4\n1 2\n2 3\n3 4\n1 4", "output": "No" }, { "input": "5 6\n1 2\n2 3\n3 5\n1 4\n4 5\n1 3", "output": "Yes\n1\n1\n1\n2\n1\n2" } ]
2,000
1,126,400
0
14,718
0
none
[ "none" ]
null
null
KleofΓ‘Ε‘ is participating in an *n*-thlon - a tournament consisting of *n* different competitions in *n* different disciplines (numbered 1 through *n*). There are *m* participants in the *n*-thlon and each of them participates in all competitions. In each of these *n* competitions, the participants are given ranks from 1 to *m* in such a way that no two participants are given the same rank - in other words, the ranks in each competition form a permutation of numbers from 1 to *m*. The score of a participant in a competition is equal to his/her rank in it. The overall score of each participant is computed as the sum of that participant's scores in all competitions. The overall rank of each participant is equal to 1<=+<=*k*, where *k* is the number of participants with strictly smaller overall score. The *n*-thlon is over now, but the results haven't been published yet. KleofΓ‘Ε‘ still remembers his ranks in each particular competition; however, he doesn't remember anything about how well the other participants did. Therefore, KleofΓ‘Ε‘ would like to know his expected overall rank. All competitors are equally good at each discipline, so all rankings (permutations of ranks of everyone except KleofΓ‘Ε‘) in each competition are equiprobable.
The first line of the input contains two space-separated integers *n* (1<=≀<=*n*<=≀<=100) and *m* (1<=≀<=*m*<=≀<=1000)Β β€” the number of competitions and the number of participants respectively. Then, *n* lines follow. The *i*-th of them contains one integer *x**i* (1<=≀<=*x**i*<=≀<=*m*)Β β€” the rank of KleofΓ‘Ε‘ in the *i*-th competition.
Output a single real number – the expected overall rank of KleofΓ‘Ε‘. Your answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=9. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "4 10\n2\n1\n2\n1\n", "5 5\n1\n2\n3\n4\n5\n", "3 6\n2\n4\n2\n" ]
[ "1.0000000000000000\n", "2.7500000000000000\n", "1.6799999999999999\n" ]
In the first sample, KleofΓ‘Ε‘ has overall score 6. Nobody else can have overall score less than 6 (but it's possible for one other person to have overall score 6 as well), so his overall rank must be 1.
[]
46
0
0
14,721