contestId int64 0 1.01k | name stringlengths 2 58 | tags listlengths 0 11 | title stringclasses 523
values | time-limit stringclasses 8
values | memory-limit stringclasses 8
values | problem-description stringlengths 0 7.15k | input-specification stringlengths 0 2.05k | output-specification stringlengths 0 1.5k | demo-input listlengths 0 7 | demo-output listlengths 0 7 | note stringlengths 0 5.24k | test_cases listlengths 0 402 | timeConsumedMillis int64 0 8k | memoryConsumedBytes int64 0 537M | score float64 -1 3.99 | __index_level_0__ int64 0 621k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | none | [
"none"
] | null | null | For months Maxim has been coming to work on his favorite bicycle. And quite recently he decided that he is ready to take part in a cyclists' competitions.
He knows that this year *n* competitions will take place. During the *i*-th competition the participant must as quickly as possible complete a ride along a straight line from point *s**i* to point *f**i* (*s**i*<=<<=*f**i*).
Measuring time is a complex process related to usage of a special sensor and a time counter. Think of the front wheel of a bicycle as a circle of radius *r*. Let's neglect the thickness of a tire, the size of the sensor, and all physical effects. The sensor is placed on the rim of the wheel, that is, on some fixed point on a circle of radius *r*. After that the counter moves just like the chosen point of the circle, i.e. moves forward and rotates around the center of the circle.
At the beginning each participant can choose any point *b**i*, such that his bike is fully behind the starting line, that is, *b**i*<=<<=*s**i*<=-<=*r*. After that, he starts the movement, instantly accelerates to his maximum speed and at time *ts**i*, when the coordinate of the sensor is equal to the coordinate of the start, the time counter starts. The cyclist makes a complete ride, moving with his maximum speed and at the moment the sensor's coordinate is equal to the coordinate of the finish (moment of time *tf**i*), the time counter deactivates and records the final time. Thus, the counter records that the participant made a complete ride in time *tf**i*<=-<=*ts**i*.
Maxim is good at math and he suspects that the total result doesn't only depend on his maximum speed *v*, but also on his choice of the initial point *b**i*. Now Maxim is asking you to calculate for each of *n* competitions the minimum possible time that can be measured by the time counter. The radius of the wheel of his bike is equal to *r*. | The first line contains three integers *n*, *r* and *v* (1<=β€<=*n*<=β€<=100<=000,<=1<=β€<=*r*,<=*v*<=β€<=109)Β β the number of competitions, the radius of the front wheel of Max's bike and his maximum speed, respectively.
Next *n* lines contain the descriptions of the contests. The *i*-th line contains two integers *s**i* and *f**i* (1<=β€<=*s**i*<=<<=*f**i*<=β€<=109)Β β the coordinate of the start and the coordinate of the finish on the *i*-th competition. | Print *n* real numbers, the *i*-th number should be equal to the minimum possible time measured by the time counter. Your answer will be considered correct if its absolute or relative error will not exceed 10<=-<=6.
Namely: let's assume that your answer equals *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if . | [
"2 1 2\n1 10\n5 9\n"
] | [
"3.849644710502\n1.106060157705\n"
] | none | [] | 2,000 | 921,600 | 0 | 15,075 | |
954 | Yet Another String Matching Problem | [
"fft",
"math"
] | null | null | Suppose you have two strings *s* and *t*, and their length is equal. You may perform the following operation any number of times: choose two different characters *c*1 and *c*2, and replace every occurence of *c*1 in both strings with *c*2. Let's denote the distance between strings *s* and *t* as the minimum number of operations required to make these strings equal. For example, if *s* is abcd and *t* is ddcb, the distance between them is 2 β we may replace every occurence of a with b, so *s* becomes bbcd, and then we may replace every occurence of b with d, so both strings become ddcd.
You are given two strings *S* and *T*. For every substring of *S* consisting of |*T*| characters you have to determine the distance between this substring and *T*. | The first line contains the string *S*, and the second β the string *T* (1<=β€<=|*T*|<=β€<=|*S*|<=β€<=125000). Both strings consist of lowercase Latin letters from a to f. | Print |*S*|<=-<=|*T*|<=+<=1 integers. The *i*-th of these integers must be equal to the distance between the substring of *S* beginning at *i*-th index with length |*T*| and the string *T*. | [
"abcdefa\nddcb\n"
] | [
"2 3 3 3 \n"
] | none | [
{
"input": "abcdefa\nddcb",
"output": "2 3 3 3 "
}
] | 30 | 0 | 0 | 15,084 | |
600 | Area of Two Circles' Intersection | [
"geometry"
] | null | null | You are given two circles. Find the area of their intersection. | The first line contains three integers *x*1,<=*y*1,<=*r*1 (<=-<=109<=β€<=*x*1,<=*y*1<=β€<=109,<=1<=β€<=*r*1<=β€<=109) β the position of the center and the radius of the first circle.
The second line contains three integers *x*2,<=*y*2,<=*r*2 (<=-<=109<=β€<=*x*2,<=*y*2<=β€<=109,<=1<=β€<=*r*2<=β€<=109) β the position of the center and the radius of the second circle. | Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=6. | [
"0 0 4\n6 0 4\n",
"0 0 5\n11 0 5\n"
] | [
"7.25298806364175601379\n",
"0.00000000000000000000\n"
] | none | [
{
"input": "0 0 4\n6 0 4",
"output": "7.25298806364175601379"
},
{
"input": "0 0 5\n11 0 5",
"output": "0.00000000000000000000"
},
{
"input": "0 0 10\n9 0 1",
"output": "3.14159265358979311600"
},
{
"input": "0 0 2\n2 2 2",
"output": "2.28318530717958647659"
},
{
... | 46 | 0 | -1 | 15,089 | |
214 | Hometask | [
"brute force",
"constructive algorithms",
"greedy",
"math"
] | null | null | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?
You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.
Each digit is allowed to occur in the number the same number of times it occurs in the set. | A single line contains a single integer *n* (1<=β€<=*n*<=β€<=100000) β the number of digits in the set. The second line contains *n* digits, the digits are separated by a single space. | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | [
"1\n0\n",
"11\n3 4 5 4 5 3 5 3 4 4 0\n",
"8\n3 2 5 1 5 2 2 3\n"
] | [
"0\n",
"5554443330\n",
"-1\n"
] | In the first sample there is only one number you can make β 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | [
{
"input": "1\n0",
"output": "0"
},
{
"input": "11\n3 4 5 4 5 3 5 3 4 4 0",
"output": "5554443330"
},
{
"input": "8\n3 2 5 1 5 2 2 3",
"output": "-1"
},
{
"input": "12\n5 3 3 3 2 5 5 1 2 1 4 1",
"output": "-1"
},
{
"input": "8\n5 5 4 1 5 5 5 3",
"output": "-1"... | 404 | 9,932,800 | 0 | 15,091 | |
935 | Fafa and Ancient Alphabet | [
"math",
"probabilities"
] | null | null | Ancient Egyptians are known to have used a large set of symbols to write on the walls of the temples. Fafa and Fifa went to one of the temples and found two non-empty words *S*1 and *S*2 of equal lengths on the wall of temple written one below the other. Since this temple is very ancient, some symbols from the words were erased. The symbols in the set have equal probability for being in the position of any erased symbol.
Fifa challenged Fafa to calculate the probability that *S*1 is lexicographically greater than *S*2. Can you help Fafa with this task?
You know that , i.Β e. there were *m* distinct characters in Egyptians' alphabet, in this problem these characters are denoted by integers from 1 to *m* in alphabet order. A word *x* is lexicographically greater than a word *y* of the same length, if the words are same up to some position, and then the word *x* has a larger character, than the word *y*.
We can prove that the probability equals to some fraction , where *P* and *Q* are coprime integers, and . Print as the answer the value , i.Β e. such a non-negative integer less than 109<=+<=7, such that , where means that *a* and *b* give the same remainders when divided by *m*. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=<=*m*<=β€<=105) β the length of each of the two words and the size of the alphabet , respectively.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=*m*) β the symbols of *S*1. If *a**i*<==<=0, then the symbol at position *i* was erased.
The third line contains *n* integers representing *S*2 with the same format as *S*1. | Print the value , where *P* and *Q* are coprime and is the answer to the problem. | [
"1 2\n0\n1\n",
"1 2\n1\n0\n",
"7 26\n0 15 12 9 13 0 14\n11 1 0 13 15 12 0\n"
] | [
"500000004\n",
"0\n",
"230769233\n"
] | In the first sample, the first word can be converted into (1) or (2). The second option is the only one that will make it lexicographically larger than the second word. So, the answer to the problem will be <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a762254bc6d3a2cc6ae07485c1de945962e7e524.png" style="max-width: 100.0%;max-height: 100.0%;"/>, that is 500000004, because <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/cbff288b4c38669aa9b8013ee25b8dde80420841.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second example, there is no replacement for the zero in the second word that will make the first one lexicographically larger. So, the answer to the problem is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a468ab19fec68399601f37993805846b7dd342ad.png" style="max-width: 100.0%;max-height: 100.0%;"/>, that is 0. | [
{
"input": "1 2\n0\n1",
"output": "500000004"
},
{
"input": "1 2\n1\n0",
"output": "0"
},
{
"input": "7 26\n0 15 12 9 13 0 14\n11 1 0 13 15 12 0",
"output": "230769233"
},
{
"input": "6 26\n14 5 19 18 9 14\n0 0 0 0 0 0",
"output": "182369325"
},
{
"input": "4 26\n... | 46 | 0 | 0 | 15,102 | |
849 | Tell Your World | [
"brute force",
"geometry"
] | null | null | Connect the countless points with lines, till we reach the faraway yonder.
There are *n* points on a coordinate plane, the *i*-th of which being (*i*,<=*y**i*).
Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set. | The first line of input contains a positive integer *n* (3<=β€<=*n*<=β€<=1<=000) β the number of points.
The second line contains *n* space-separated integers *y*1,<=*y*2,<=...,<=*y**n* (<=-<=109<=β€<=*y**i*<=β€<=109) β the vertical coordinates of each point. | Output "Yes" (without quotes) if it's possible to fulfill the requirements, and "No" otherwise.
You can print each letter in any case (upper or lower). | [
"5\n7 5 8 6 9\n",
"5\n-1 -2 0 0 -5\n",
"5\n5 4 3 2 1\n",
"5\n1000000000 0 0 0 0\n"
] | [
"Yes\n",
"No\n",
"No\n",
"Yes\n"
] | In the first example, there are five points: (1,β7), (2,β5), (3,β8), (4,β6) and (5,β9). It's possible to draw a line that passes through points 1,β3,β5, and another one that passes through points 2,β4 and is parallel to the first one.
In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel.
In the third example, it's impossible to satisfy both requirements at the same time. | [
{
"input": "5\n7 5 8 6 9",
"output": "Yes"
},
{
"input": "5\n-1 -2 0 0 -5",
"output": "No"
},
{
"input": "5\n5 4 3 2 1",
"output": "No"
},
{
"input": "5\n1000000000 0 0 0 0",
"output": "Yes"
},
{
"input": "5\n1000000000 1 0 -999999999 -1000000000",
"output": "... | 841 | 5,632,000 | 3 | 15,119 | |
608 | Saitama Destroys Hotel | [
"implementation",
"math"
] | null | null | Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special β it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to *s* and elevator initially starts on floor *s* at time 0.
The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0. | The first line of input contains two integers *n* and *s* (1<=β€<=*n*<=β€<=100, 1<=β€<=*s*<=β€<=1000)Β β the number of passengers and the number of the top floor respectively.
The next *n* lines each contain two space-separated integers *f**i* and *t**i* (1<=β€<=*f**i*<=β€<=*s*, 1<=β€<=*t**i*<=β€<=1000)Β β the floor and the time of arrival in seconds for the passenger number *i*. | Print a single integerΒ β the minimum amount of time in seconds needed to bring all the passengers to floor 0. | [
"3 7\n2 1\n3 8\n5 2\n",
"5 10\n2 77\n3 33\n8 21\n9 12\n10 64\n"
] | [
"11\n",
"79\n"
] | In the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:
1. Move to floor 5: takes 2 seconds.
2. Pick up passenger 3.
3. Move to floor 3: takes 2 seconds.
4. Wait for passenger 2 to arrive: takes 4 seconds.
5. Pick up passenger 2.
6. Go to floor 2: takes 1 second.
7. Pick up passenger 1.
8. Go to floor 0: takes 2 seconds.
This gives a total of 2β+β2β+β4β+β1β+β2β=β11 seconds. | [
{
"input": "3 7\n2 1\n3 8\n5 2",
"output": "11"
},
{
"input": "5 10\n2 77\n3 33\n8 21\n9 12\n10 64",
"output": "79"
},
{
"input": "1 1000\n1000 1000",
"output": "2000"
},
{
"input": "1 1\n1 1",
"output": "2"
},
{
"input": "1 1000\n1 1",
"output": "1000"
},
... | 62 | 4,608,000 | 0 | 15,131 | |
183 | Headquarters | [
"constructive algorithms",
"math"
] | null | null | Sensation, sensation in the two-dimensional kingdom! The police have caught a highly dangerous outlaw, member of the notorious "Pihters" gang. The law department states that the outlaw was driving from the gang's headquarters in his car when he crashed into an ice cream stall. The stall, the car, and the headquarters each occupies exactly one point on the two-dimensional kingdom.
The outlaw's car was equipped with a GPS transmitter. The transmitter showed that the car made exactly *n* movements on its way from the headquarters to the stall. A movement can move the car from point (*x*,<=*y*) to one of these four points: to point (*x*<=-<=1,<=*y*) which we will mark by letter "L", to point (*x*<=+<=1,<=*y*) β "R", to point (*x*,<=*y*<=-<=1) β "D", to point (*x*,<=*y*<=+<=1) β "U".
The GPS transmitter is very inaccurate and it doesn't preserve the exact sequence of the car's movements. Instead, it keeps records of the car's possible movements. Each record is a string of one of these types: "UL", "UR", "DL", "DR" or "ULDR". Each such string means that the car made a single movement corresponding to one of the characters of the string. For example, string "UL" means that the car moved either "U", or "L".
You've received the journal with the outlaw's possible movements from the headquarters to the stall. The journal records are given in a chronological order. Given that the ice-cream stall is located at point (0,<=0), your task is to print the number of different points that can contain the gang headquarters (that is, the number of different possible locations of the car's origin). | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the number of the car's movements from the headquarters to the stall.
Each of the following *n* lines describes the car's possible movements. It is guaranteed that each possible movement is one of the following strings: "UL", "UR", "DL", "DR" or "ULDR".
All movements are given in chronological order.
Please do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin and cout stream or the %I64d specifier. | Print a single integer β the number of different possible locations of the gang's headquarters. | [
"3\nUR\nUL\nULDR\n",
"2\nDR\nDL\n"
] | [
"9\n",
"4\n"
] | The figure below shows the nine possible positions of the gang headquarters from the first sample:
For example, the following movements can get the car from point (1,β0) to point (0,β0): | [
{
"input": "3\nUR\nUL\nULDR",
"output": "9"
},
{
"input": "2\nDR\nDL",
"output": "4"
},
{
"input": "4\nUL\nUR\nDR\nDL",
"output": "9"
},
{
"input": "10\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL",
"output": "11"
},
{
"input": "6\nUL\nDL\nDL\nUL\nUL\nDL",
"output"... | 436 | 5,120,000 | 3 | 15,153 | |
939 | Convenient For Everybody | [
"binary search",
"two pointers"
] | null | null | In distant future on Earth day lasts for *n* hours and that's why there are *n* timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to *n* are used, i.e. there is no time "0 hours", instead of it "*n* hours" is used. When local time in the 1-st timezone is 1 hour, local time in the *i*-th timezone is *i* hours.
Some online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are *a**i* people from *i*-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than *s* hours 00 minutes local time and ends not later than *f* hours 00 minutes local time. Values *s* and *f* are equal for all time zones. If the contest starts at *f* hours 00 minutes local time, the person won't participate in it.
Help platform select such an hour, that the number of people who will participate in the contest is maximum. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=100<=000)Β β the number of hours in day.
The second line contains *n* space-separated integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=10<=000), where *a**i* is the number of people in the *i*-th timezone who want to participate in the contest.
The third line contains two space-separated integers *s* and *f* (1<=β€<=*s*<=<<=*f*<=β€<=*n*). | Output a single integerΒ β the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them. | [
"3\n1 2 3\n1 3\n",
"5\n1 2 3 4 1\n1 3\n"
] | [
"3\n",
"4\n"
] | In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate.
In second example only people from the third and the fourth timezones will participate. | [
{
"input": "3\n1 2 3\n1 3",
"output": "3"
},
{
"input": "5\n1 2 3 4 1\n1 3",
"output": "4"
},
{
"input": "2\n5072 8422\n1 2",
"output": "2"
},
{
"input": "10\n7171 2280 6982 9126 9490 2598 569 6744 5754 1855\n7 9",
"output": "4"
},
{
"input": "10\n5827 8450 8288 5... | 77 | 512,000 | 0 | 15,189 | |
0 | none | [
"none"
] | null | null | You are given an undirected graph with *n* vertices. There are no edge-simple cycles with the even length in it. In other words, there are no cycles of even length that pass each edge at most once. Let's enumerate vertices from 1 to *n*.
You have to answer *q* queries. Each query is described by a segment of vertices [*l*;<=*r*], and you have to count the number of its subsegments [*x*;<=*y*] (*l*<=β€<=*x*<=β€<=*y*<=β€<=*r*), such that if we delete all vertices except the segment of vertices [*x*;<=*y*] (including *x* and *y*) and edges between them, the resulting graph is bipartite. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=3Β·105, 1<=β€<=*m*<=β€<=3Β·105)Β β the number of vertices and the number of edges in the graph.
The next *m* lines describe edges in the graph. The *i*-th of these lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*; *a**i*<=β <=*b**i*), denoting an edge between vertices *a**i* and *b**i*. It is guaranteed that this graph does not contain edge-simple cycles of even length.
The next line contains a single integer *q* (1<=β€<=*q*<=β€<=3Β·105)Β β the number of queries.
The next *q* lines contain queries. The *i*-th of these lines contains two integers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*)Β β the query parameters. | Print *q* numbers, each in new line: the *i*-th of them should be the number of subsegments [*x*;<=*y*] (*l**i*<=β€<=*x*<=β€<=*y*<=β€<=*r**i*), such that the graph that only includes vertices from segment [*x*;<=*y*] and edges between them is bipartite. | [
"6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n3\n1 3\n4 6\n1 6\n",
"8 9\n1 2\n2 3\n3 1\n4 5\n5 6\n6 7\n7 8\n8 4\n7 2\n3\n1 8\n1 4\n3 8\n"
] | [
"5\n5\n14\n",
"27\n8\n19\n"
] | The first example is shown on the picture below:
<img class="tex-graphics" src="https://espresso.codeforces.com/01e1d1999228f416613ff64b5d0e0cf984f150b1.png" style="max-width: 100.0%;max-height: 100.0%;"/>
For the first query, all subsegments of [1;β3], except this segment itself, are suitable.
For the first query, all subsegments of [4;β6], except this segment itself, are suitable.
For the third query, all subsegments of [1;β6] are suitable, except [1;β3], [1;β4], [1;β5], [1;β6], [2;β6], [3;β6], [4;β6].
The second example is shown on the picture below:
<img class="tex-graphics" src="https://espresso.codeforces.com/09b9227070585b8d5a7dff3cbc5f8535c260a595.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 62 | 0 | 0 | 15,196 | |
594 | Edo and Magnets | [
"brute force",
"greedy",
"implementation",
"two pointers"
] | null | null | Edo has got a collection of *n* refrigerator magnets!
He decided to buy a refrigerator and hang the magnets on the door. The shop can make the refrigerator with any size of the door that meets the following restrictions: the refrigerator door must be rectangle, and both the length and the width of the door must be positive integers.
Edo figured out how he wants to place the magnets on the refrigerator. He introduced a system of coordinates on the plane, where each magnet is represented as a rectangle with sides parallel to the coordinate axes.
Now he wants to remove no more than *k* magnets (he may choose to keep all of them) and attach all remaining magnets to the refrigerator door, and the area of ββthe door should be as small as possible. A magnet is considered to be attached to the refrigerator door if its center lies on the door or on its boundary. The relative positions of all the remaining magnets must correspond to the plan.
Let us explain the last two sentences. Let's suppose we want to hang two magnets on the refrigerator. If the magnet in the plan has coordinates of the lower left corner (*x*1, *y*1) and the upper right corner (*x*2, *y*2), then its center is located at (, ) (may not be integers). By saying the relative position should correspond to the plan we mean that the only available operation is translation, i.e. the vector connecting the centers of two magnets in the original plan, must be equal to the vector connecting the centers of these two magnets on the refrigerator.
The sides of the refrigerator door must also be parallel to coordinate axes. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=100<=000, 0<=β€<=*k*<=β€<=*min*(10,<=*n*<=-<=1))Β β the number of magnets that Edo has and the maximum number of magnets Edo may not place on the refrigerator.
Next *n* lines describe the initial plan of placing magnets. Each line contains four integers *x*1,<=*y*1,<=*x*2,<=*y*2 (1<=β€<=*x*1<=<<=*x*2<=β€<=109, 1<=β€<=*y*1<=<<=*y*2<=β€<=109)Β β the coordinates of the lower left and upper right corners of the current magnet. The magnets can partially overlap or even fully coincide. | Print a single integerΒ β the minimum area of the door of refrigerator, which can be used to place at least *n*<=-<=*k* magnets, preserving the relative positions. | [
"3 1\n1 1 2 2\n2 2 3 3\n3 3 4 4\n",
"4 1\n1 1 2 2\n1 9 2 10\n9 9 10 10\n9 1 10 2\n",
"3 0\n1 1 2 2\n1 1 1000000000 1000000000\n1 3 8 12\n"
] | [
"1\n",
"64\n",
"249999999000000001\n"
] | In the first test sample it is optimal to remove either the first or the third magnet. If we remove the first magnet, the centers of two others will lie at points (2.5, 2.5) and (3.5, 3.5). Thus, it is enough to buy a fridge with door width 1 and door height 1, the area of the door also equals one, correspondingly.
In the second test sample it doesn't matter which magnet to remove, the answer will not change β we need a fridge with door width 8 and door height 8.
In the third sample you cannot remove anything as *k*β=β0. | [
{
"input": "3 1\n1 1 2 2\n2 2 3 3\n3 3 4 4",
"output": "1"
},
{
"input": "4 1\n1 1 2 2\n1 9 2 10\n9 9 10 10\n9 1 10 2",
"output": "64"
},
{
"input": "3 0\n1 1 2 2\n1 1 1000000000 1000000000\n1 3 8 12",
"output": "249999999000000001"
},
{
"input": "11 8\n9 1 11 5\n2 2 8 12\n3 ... | 966 | 17,100,800 | 3 | 15,214 | |
813 | Two Melodies | [
"dp",
"flows"
] | null | null | Alice is a beginner composer and now she is ready to create another masterpiece. And not even the single one but two at the same time!
Alice has a sheet with *n* notes written on it. She wants to take two such non-empty non-intersecting subsequences that both of them form a melody and sum of their lengths is maximal.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Subsequence forms a melody when each two adjacent notes either differs by 1 or are congruent modulo 7.
You should write a program which will calculate maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody. | The first line contains one integer number *n* (2<=β€<=*n*<=β€<=5000).
The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=105) β notes written on a sheet. | Print maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody. | [
"4\n1 2 4 5\n",
"6\n62 22 60 61 48 49\n"
] | [
"4\n",
"5\n"
] | In the first example subsequences [1,β2] and [4,β5] give length 4 in total.
In the second example subsequences [62,β48,β49] and [60,β61] give length 5 in total. If you choose subsequence [62,β61] in the first place then the second melody will have maximum length 2, that gives the result of 4, which is not maximal. | [
{
"input": "4\n1 2 4 5",
"output": "4"
},
{
"input": "6\n62 22 60 61 48 49",
"output": "5"
},
{
"input": "2\n1 4",
"output": "2"
},
{
"input": "2\n5 4",
"output": "2"
},
{
"input": "10\n9 6 8 5 5 2 8 9 2 2",
"output": "9"
},
{
"input": "10\n7776 32915 ... | 0 | 0 | -1 | 15,236 | |
118 | Present from Lena | [
"constructive algorithms",
"implementation"
] | null | null | Vasya's birthday is approaching and Lena decided to sew a patterned handkerchief to him as a present. Lena chose digits from 0 to *n* as the pattern. The digits will form a rhombus. The largest digit *n* should be located in the centre. The digits should decrease as they approach the edges. For example, for *n*<==<=5 the handkerchief pattern should look like that:
Your task is to determine the way the handkerchief will look like by the given *n*. | The first line contains the single integer *n* (2<=β€<=*n*<=β€<=9). | Print a picture for the given *n*. You should strictly observe the number of spaces before the first digit on each line. Every two adjacent digits in the same line should be separated by exactly one space. There should be no spaces after the last digit at the end of each line. | [
"2\n",
"3\n"
] | [
"0\n 0 1 0\n0 1 2 1 0\n 0 1 0\n 0\n",
"0\n 0 1 0\n 0 1 2 1 0\n0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n"
] | none | [
{
"input": "2",
"output": " 0\n 0 1 0\n0 1 2 1 0\n 0 1 0\n 0"
},
{
"input": "3",
"output": " 0\n 0 1 0\n 0 1 2 1 0\n0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0"
},
{
"input": "4",
"output": " 0\n 0 1 0\n 0 1 2 1 0\n 0 1 2 3 2 1 0\n0 1 2 3 4 3 2 1 0... | 30 | 0 | 0 | 15,237 | |
376 | I.O.U. | [
"implementation"
] | null | null | Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has *n* people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=100;Β 0<=β€<=*m*<=β€<=104). The next *m* lines contain the debts. The *i*-th line contains three integers *a**i*,<=*b**i*,<=*c**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*;Β *a**i*<=β <=*b**i*;Β 1<=β€<=*c**i*<=β€<=100), which mean that person *a**i* owes person *b**i* *c**i* rubles.
Assume that the people are numbered by integers from 1 to *n*.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (*x*,<=*y*) and pair of people (*y*,<=*x*). | Print a single integer β the minimum sum of debts in the optimal rearrangement. | [
"5 3\n1 2 10\n2 3 1\n2 4 1\n",
"3 0\n",
"4 3\n1 2 1\n2 3 1\n3 1 1\n"
] | [
"10\n",
"0\n",
"0\n"
] | In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | [
{
"input": "5 3\n1 2 10\n2 3 1\n2 4 1",
"output": "10"
},
{
"input": "3 0",
"output": "0"
},
{
"input": "4 3\n1 2 1\n2 3 1\n3 1 1",
"output": "0"
},
{
"input": "20 28\n1 5 6\n1 12 7\n1 13 4\n1 15 7\n1 20 3\n2 4 1\n2 15 6\n3 5 3\n3 8 10\n3 13 8\n3 20 6\n4 6 10\n4 12 8\n4 19 5\... | 46 | 6,963,200 | -1 | 15,249 | |
526 | Om Nom and Necklace | [
"hashing",
"string suffix structures",
"strings"
] | null | null | One day Om Nom found a thread with *n* beads of different colors. He decided to cut the first several beads from this thread to make a bead necklace and present it to his girlfriend Om Nelly.
Om Nom knows that his girlfriend loves beautiful patterns. That's why he wants the beads on the necklace to form a regular pattern. A sequence of beads *S* is regular if it can be represented as *S*<==<=*A*<=+<=*B*<=+<=*A*<=+<=*B*<=+<=*A*<=+<=...<=+<=*A*<=+<=*B*<=+<=*A*, where *A* and *B* are some bead sequences, "<=+<=" is the concatenation of sequences, there are exactly 2*k*<=+<=1 summands in this sum, among which there are *k*<=+<=1 "*A*" summands and *k* "*B*" summands that follow in alternating order. Om Nelly knows that her friend is an eager mathematician, so she doesn't mind if *A* or *B* is an empty sequence.
Help Om Nom determine in which ways he can cut off the first several beads from the found thread (at least one; probably, all) so that they form a regular pattern. When Om Nom cuts off the beads, he doesn't change their order. | The first line contains two integers *n*, *k* (1<=β€<=*n*,<=*k*<=β€<=1<=000<=000) β the number of beads on the thread that Om Nom found and number *k* from the definition of the regular sequence above.
The second line contains the sequence of *n* lowercase Latin letters that represent the colors of the beads. Each color corresponds to a single letter. | Print a string consisting of *n* zeroes and ones. Position *i* (1<=β€<=*i*<=β€<=*n*) must contain either number one if the first *i* beads on the thread form a regular sequence, or a zero otherwise. | [
"7 2\nbcabcab\n",
"21 2\nababaababaababaababaa\n"
] | [
"0000011",
"000110000111111000011"
] | In the first sample test a regular sequence is both a sequence of the first 6 beads (we can take *A*β=β"", *B*β=β"bca"), and a sequence of the first 7 beads (we can take *A*β=β"b", *B*β=β"ca").
In the second sample test, for example, a sequence of the first 13 beads is regular, if we take *A*β=β"aba", *B*β=β"ba". | [
{
"input": "7 2\nbcabcab",
"output": "0000011"
},
{
"input": "21 2\nababaababaababaababaa",
"output": "000110000111111000011"
},
{
"input": "321 2\nabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaab... | 62 | 0 | 0 | 15,264 | |
449 | Jzzhu and Numbers | [
"bitmasks",
"combinatorics",
"dp"
] | null | null | Jzzhu have *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n*. We will call a sequence of indexes *i*1,<=*i*2,<=...,<=*i**k* (1<=β€<=*i*1<=<<=*i*2<=<<=...<=<<=*i**k*<=β€<=*n*) a group of size *k*.
Jzzhu wonders, how many groups exists such that *a**i*1 & *a**i*2 & ... & *a**i**k*<==<=0 (1<=β€<=*k*<=β€<=*n*)? Help him and print this number modulo 1000000007 (109<=+<=7). Operation *x* & *y* denotes bitwise AND operation of two numbers. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=106). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=106). | Output a single integer representing the number of required groups modulo 1000000007 (109<=+<=7). | [
"3\n2 3 3\n",
"4\n0 1 2 3\n",
"6\n5 2 0 5 2 1\n"
] | [
"0\n",
"10\n",
"53\n"
] | none | [
{
"input": "3\n2 3 3",
"output": "0"
},
{
"input": "4\n0 1 2 3",
"output": "10"
},
{
"input": "6\n5 2 0 5 2 1",
"output": "53"
},
{
"input": "16\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15",
"output": "64594"
},
{
"input": "10\n450661 128600 993228 725823 293549 33490 ... | 1,294 | 201,113,600 | 3 | 15,342 | |
115 | Plumber | [
"math"
] | null | null | Little John aspires to become a plumber! Today he has drawn a grid consisting of *n* rows and *m* columns, consisting of *n*<=Γ<=*m* square cells.
In each cell he will draw a pipe segment. He can only draw four types of segments numbered from 1 to 4, illustrated as follows:
Each pipe segment has two ends, illustrated by the arrows in the picture above. For example, segment 1 has ends at top and left side of it.
Little John considers the piping system to be leaking if there is at least one pipe segment inside the grid whose end is not connected to another pipe's end or to the border of the grid. The image below shows an example of leaking and non-leaking systems of size 1<=Γ<=2.
Now, you will be given the grid that has been partially filled by Little John. Each cell will either contain one of the four segments above, or be empty. Find the number of possible different non-leaking final systems after Little John finishes filling all of the empty cells with pipe segments. Print this number modulo 1000003 (106<=+<=3).
Note that rotations or flipping of the grid are not allowed and so two configurations that are identical only when one of them has been rotated or flipped either horizontally or vertically are considered two different configurations. | The first line will contain two single-space separated integers *n* and *m* (1<=β€<=*n*,<=*m*,<=*n*Β·*m*<=β€<=5Β·105) β the number of rows and columns respectively. Then *n* lines follow, each contains exactly *m* characters β the description of the grid. Each character describes a cell and is either one of these:
- "1" - "4" β a pipe segment of one of four types as described above - "." β an empty cell | Print a single integer denoting the number of possible final non-leaking pipe systems modulo 1000003 (106<=+<=3). If there are no such configurations, print 0. | [
"2 2\n13\n..\n",
"3 1\n1\n4\n.\n",
"2 2\n3.\n.1\n"
] | [
"2\n",
"0\n",
"1\n"
] | For the first example, the initial configuration of the grid is as follows.
The only two possible final non-leaking pipe configurations are as follows:
For the second example, the initial grid is already leaking, so there will be no final grid that is non-leaking.
For the final example, there's only one possible non-leaking final grid as follows. | [
{
"input": "2 2\n13\n..",
"output": "2"
},
{
"input": "3 1\n1\n4\n.",
"output": "0"
},
{
"input": "2 2\n3.\n.1",
"output": "1"
},
{
"input": "2 2\n..\n..",
"output": "16"
},
{
"input": "2 2\n32\n41",
"output": "1"
},
{
"input": "4 4\n....\n.3.3\n....\n... | 3,000 | 73,523,200 | 0 | 15,344 | |
81 | Average Score | [
"greedy",
"math",
"sortings"
] | C. Average Score | 2 | 256 | After the educational reform Polycarp studies only two subjects at school, Safety Studies and PE (Physical Education). During the long months of the fourth term, he received *n* marks in them. When teachers wrote a mark in the journal, they didn't write in what subject the mark was for, they just wrote the mark.
Now it's time to show the journal to his strict parents. Polycarp knows that recently at the Parent Meeting the parents were told that he received *a* Safety Studies marks and *b* PE marks (*a*<=+<=*b*<==<=*n*). Now Polycarp wants to write a subject's name in front of each mark so that:
- there are exactly *a* Safety Studies marks, - there are exactly *b* PE marks, - the total average score in both subjects is maximum.
An average subject grade is the sum of all marks in it, divided by the number of them. Of course, the division is performed in real numbers without rounding up or down. Polycarp aims to maximize the *x*1<=+<=*x*2, where *x*1 is the average score in the first subject (Safety Studies), and *x*2 is the average score in the second one (Physical Education). | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105), *n* is the number of marks in Polycarp's Journal. The second line contains two positive integers *a*,<=*b* (1<=β€<=*a*,<=*b*<=β€<=*n*<=-<=1,<=*a*<=+<=*b*<==<=*n*). The third line contains a sequence of integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=5), they are Polycarp's marks. | Print the sequence of integers *f*1,<=*f*2,<=...,<=*f**n*, where *f**i* (1<=β€<=*f**i*<=β€<=2) is the number of a subject to which the *i*-th mark should be attributed. If there are several possible solutions, then print such that the sequence *f*1,<=*f*2,<=...,<=*f**n* is the smallest lexicographically.
The sequence *p*1,<=*p*2,<=...,<=*p**n* is lexicographically less than *q*1,<=*q*2,<=...,<=*q**n* if there exists such *j* (1<=β€<=*j*<=β€<=*n*) that *p**i*<==<=*q**i* for all 1<=β€<=*i*<=<<=*j*, Π°nd *p**j*<=<<=*q**j*. | [
"5\n3 2\n4 4 5 4 4\n",
"4\n2 2\n3 5 4 5\n",
"6\n1 5\n4 4 4 5 4 4\n"
] | [
"1 1 2 1 2 ",
"1 1 2 2 ",
"2 2 2 1 2 2 "
] | In the first sample the average score in the first subject is equal to 4, and in the second one β to 4.5. The total average score is 8.5. | [
{
"input": "5\n3 2\n4 4 5 4 4",
"output": "1 1 2 1 2 "
},
{
"input": "4\n2 2\n3 5 4 5",
"output": "1 1 2 2 "
},
{
"input": "6\n1 5\n4 4 4 5 4 4",
"output": "2 2 2 1 2 2 "
},
{
"input": "4\n2 2\n2 1 3 3",
"output": "1 1 2 2 "
},
{
"input": "9\n3 6\n4 5 4 1 2 2 2 4 ... | 92 | 512,000 | 0 | 15,355 |
846 | Random Query | [
"data structures",
"math",
"probabilities",
"two pointers"
] | null | null | You are given an array *a* consisting of *n* positive integers. You pick two integer numbers *l* and *r* from 1 to *n*, inclusive (numbers are picked randomly, equiprobably and independently). If *l*<=><=*r*, then you swap values of *l* and *r*. You have to calculate the expected value of the number of unique elements in segment of the array from index *l* to index *r*, inclusive (1-indexed). | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=106). The second line contains *n* integer numbers *a*1, *a*2, ... *a**n* (1<=β€<=*a**i*<=β€<=106) β elements of the array. | Print one number β the expected number of unique elements in chosen segment.
Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=4 β formally, the answer is correct if , where *x* is jury's answer, and *y* is your answer. | [
"2\n1 2\n",
"2\n2 2\n"
] | [
"1.500000\n",
"1.000000\n"
] | none | [
{
"input": "2\n1 2",
"output": "1.500000"
},
{
"input": "2\n2 2",
"output": "1.000000"
},
{
"input": "10\n9 6 8 5 5 2 8 9 2 2",
"output": "3.100000"
},
{
"input": "20\n49 33 9 8 50 21 12 44 23 39 24 10 17 4 17 40 24 19 27 21",
"output": "7.010000"
},
{
"input": "1... | 108 | 0 | -1 | 15,416 | |
371 | Subway Innovation | [
"greedy",
"math",
"two pointers"
] | null | null | Berland is going through tough times β the dirt price has dropped and that is a blow to the country's economy. Everybody knows that Berland is the top world dirt exporter!
The President of Berland was forced to leave only *k* of the currently existing *n* subway stations.
The subway stations are located on a straight line one after another, the trains consecutively visit the stations as they move. You can assume that the stations are on the *Ox* axis, the *i*-th station is at point with coordinate *x**i*. In such case the distance between stations *i* and *j* is calculated by a simple formula |*x**i*<=-<=*x**j*|.
Currently, the Ministry of Transport is choosing which stations to close and which ones to leave. Obviously, the residents of the capital won't be too enthusiastic about the innovation, so it was decided to show the best side to the people. The Ministry of Transport wants to choose such *k* stations that minimize the average commute time in the subway!
Assuming that the train speed is constant (it is a fixed value), the average commute time in the subway is calculated as the sum of pairwise distances between stations, divided by the number of pairs (that is ) and divided by the speed of the train.
Help the Minister of Transport to solve this difficult problem. Write a program that, given the location of the stations selects such *k* stations that the average commute time in the subway is minimized. | The first line of the input contains integer *n* (3<=β€<=*n*<=β€<=3Β·105) β the number of the stations before the innovation. The second line contains the coordinates of the stations *x*1,<=*x*2,<=...,<=*x**n* (<=-<=108<=β€<=*x**i*<=β€<=108). The third line contains integer *k* (2<=β€<=*k*<=β€<=*n*<=-<=1) β the number of stations after the innovation.
The station coordinates are distinct and not necessarily sorted. | Print a sequence of *k* distinct integers *t*1,<=*t*2,<=...,<=*t**k* (1<=β€<=*t**j*<=β€<=*n*) β the numbers of the stations that should be left after the innovation in arbitrary order. Assume that the stations are numbered 1 through *n* in the order they are given in the input. The number of stations you print must have the minimum possible average commute time among all possible ways to choose *k* stations. If there are multiple such ways, you are allowed to print any of them. | [
"3\n1 100 101\n2\n"
] | [
"2 3 "
] | In the sample testcase the optimal answer is to destroy the first station (with *x*β=β1). The average commute time will be equal to 1 in this way. | [
{
"input": "3\n1 100 101\n2",
"output": "2 3 "
},
{
"input": "5\n11 21 30 40 50\n3",
"output": "1 2 3 "
},
{
"input": "3\n0 -4 -3\n2",
"output": "2 3 "
},
{
"input": "4\n5 -7 8 1\n2",
"output": "1 3 "
},
{
"input": "5\n-4 -2 10 -9 -10\n2",
"output": "5 4 "
}... | 2,000 | 22,732,800 | 0 | 15,418 | |
198 | Jumping on Walls | [
"shortest paths"
] | null | null | Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon.
The canyon consists of two vertical parallel walls, their height is *n* meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to *n* from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous.
Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions:
- climb one area up; - climb one area down; - jump to the opposite wall. That gets the ninja to the area that is exactly *k* meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area *x* of one wall, then after the jump he is located at area *x*<=+<=*k* of the other wall.
If at some point of time the ninja tries to get to an area with a number larger than *n*, then we can assume that the ninja got out of the canyon.
The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" β first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on.
The level is considered completed if the ninja manages to get out of the canyon.
After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question. | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=105) β the height of the canyon and the height of ninja's jump, correspondingly.
The second line contains the description of the left wall β a string with the length of *n* characters. The *i*-th character represents the state of the *i*-th wall area: character "X" represents a dangerous area and character "-" represents a safe area.
The third line describes the right wall in the same format.
It is guaranteed that the first area of the left wall is not dangerous. | Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes). | [
"7 3\n---X--X\n-X--XX-\n",
"6 2\n--X-X-\nX--XX-\n"
] | [
"YES\n",
"NO\n"
] | In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon.
In the second sample there's no way the ninja can get out of the canyon. | [
{
"input": "7 3\n---X--X\n-X--XX-",
"output": "YES"
},
{
"input": "6 2\n--X-X-\nX--XX-",
"output": "NO"
},
{
"input": "10 1\n-X-X-X-X-X\nX-X-X-X-X-",
"output": "YES"
},
{
"input": "5 4\n-X---\n----X",
"output": "NO"
},
{
"input": "6 2\n--X--X\nXX-X-X",
"output... | 560 | 31,744,000 | 3 | 15,495 | |
0 | none | [
"none"
] | null | null | Kostya is a progamer specializing in the discipline of Dota 2. Valve Corporation, the developer of this game, has recently released a new patch which turned the balance of the game upside down. Kostya, as the captain of the team, realizes that the greatest responsibility lies on him, so he wants to resort to the analysis of innovations patch from the mathematical point of view to choose the best heroes for his team in every game.
A Dota 2 match involves two teams, each of them must choose some heroes that the players of the team are going to play for, and it is forbidden to choose the same hero several times, even in different teams. In large electronic sports competitions where Kostya's team is going to participate, the matches are held in the Captains Mode. In this mode the captains select the heroes by making one of two possible actions in a certain, predetermined order: pick or ban.
- To pick a hero for the team. After the captain picks, the picked hero goes to his team (later one of a team members will play it) and can no longer be selected by any of the teams. - To ban a hero. After the ban the hero is not sent to any of the teams, but it still can no longer be selected by any of the teams.
The team captain may miss a pick or a ban. If he misses a pick, a random hero is added to his team from those that were available at that moment, and if he misses a ban, no hero is banned, as if there was no ban.
Kostya has already identified the strength of all the heroes based on the new patch fixes. Of course, Kostya knows the order of picks and bans. The strength of a team is the sum of the strengths of the team's heroes and both teams that participate in the match seek to maximize the difference in strengths in their favor. Help Kostya determine what team, the first one or the second one, has advantage in the match, and how large the advantage is. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=100)Β β the number of heroes in Dota 2.
The second line contains *n* integers *s*1, *s*2, ..., *s**n* (1<=β€<=*s**i*<=β€<=106)Β β the strengths of all the heroes.
The third line contains a single integer *m* (2<=β€<=*m*<=β€<=*min*(*n*,<=20))Β β the number of actions the captains of the team must perform.
Next *m* lines look like "*action*Β *team*", where *action* is the needed action: a pick (represented as a "p") or a ban (represented as a "b"), and *team* is the number of the team that needs to perform the action (number 1 or 2).
It is guaranteed that each team makes at least one pick. Besides, each team has the same number of picks and the same number of bans. | Print a single integerΒ β the difference between the strength of the first team and the strength of the second team if the captains of both teams will act optimally well. | [
"2\n2 1\n2\np 1\np 2\n",
"6\n6 4 5 4 5 5\n4\nb 2\np 1\nb 1\np 2\n",
"4\n1 2 3 4\n4\np 2\nb 2\np 1\nb 1\n"
] | [
"1\n",
"0\n",
"-2\n"
] | none | [] | 140 | 12,083,200 | 0 | 15,565 | |
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<=<<=*x*2<=β€<=109, <=-<=109<=β€<=*y*1<=<<=*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"
},
{
"... | 561 | 7,680,000 | 3 | 15,593 | |
753 | Interactive Bulls and Cows (Easy) | [
"brute force",
"constructive algorithms",
"implementation"
] | null | null | This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in C++ you've got to use the fflush(stdout) function, in Java β call System.out.flush(), and in Pascal β flush(output).
Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking paper and pencil game for two players, predating the similar commercially marketed board game Mastermind.
On a sheet of paper, the first player thinks a secret string. This string consists only of digits and has the length 4. The digits in the string must be all different, no two or more equal digits are allowed.
Then the second player tries to guess his opponent's string. For every guess the first player gives the number of matches. If the matching digits are on their right positions, they are "bulls", if on different positions, they are "cows". Thus a response is a pair of numbers β the number of "bulls" and the number of "cows". A try can contain equal digits.
More formally, let's the secret string is *s* and the second player are trying to guess it with a string *x*. The number of "bulls" is a number of such positions *i* (1<=β€<=*i*<=β€<=4) where *s*[*i*]<==<=*x*[*i*]. The number of "cows" is a number of such digits *c* that *s* contains *c* in the position *i* (i.e. *s*[*i*]<==<=*c*), *x* contains *c*, but *x*[*i*]<=β <=*c*.
For example, the secret string is "0427", the opponent's try is "0724", then the answer is 2 bulls and 2 cows (the bulls are "0" and "2", the cows are "4" and "7"). If the secret string is "0123", the opponent's try is "0330", then the answer is 1 bull and 1 cow.
In this problem you are to guess the string *s* that the system has chosen. You only know that the chosen string consists of 4 distinct digits.
You can make queries to the testing system, each query is the output of a single 4-digit string. The answer to the query is the number of bulls and number of cows. If the system's response equals "4 0", that means the interaction with your problem is over and the program must terminate. That is possible for two reasons β the program either guessed the number *x* or made an invalid action (for example, printed letters instead of digits).
Your program is allowed to do at most 50 queries.
You can hack solutions of other participants providing a 4-digit string containing distinct digits β the secret string. | To read answers to the queries, the program must use the standard input.
The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string *s* and the string *x**i* printed by your program. If the system response equals "4 0", then your solution should terminate.
The testing system will let your program read the *i*-th pair of integers from the input only after your program displays the corresponding system query in the output: prints value *x**i* in a single line and executes operation flush. | The program must use the standard output to print queries.
Your program must output requests β 4-digit strings *x*1,<=*x*2,<=..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.
Your program is allowed to do at most 50 queries. | [
"0 1\n2 0\n1 1\n0 4\n2 1\n4 0\n"
] | [
"8000\n0179\n3159\n3210\n0112\n0123"
] | The secret string *s* in the example is "0123". | [
{
"input": "0123",
"output": "20"
},
{
"input": "1234",
"output": "20"
},
{
"input": "9876",
"output": "20"
},
{
"input": "7158",
"output": "20"
},
{
"input": "7590",
"output": "20"
},
{
"input": "7325",
"output": "20"
},
{
"input": "7524",... | 93 | 0 | 0 | 15,605 | |
656 | Without Text | [
"*special"
] | null | null | You can preview the image in better quality by the link: [http://assets.codeforces.com/files/656/without-text.png](//assets.codeforces.com/files/656/without-text.png) | The only line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be an alphanumeric character or a full stop ".". | Output the required answer. | [
"Codeforces\n",
"APRIL.1st\n"
] | [
"-87\n",
"17\n"
] | none | [
{
"input": "Codeforces",
"output": "-87"
},
{
"input": "APRIL.1st",
"output": "17"
},
{
"input": ".0.1.2.",
"output": "0"
},
{
"input": "CODEcode",
"output": "0"
},
{
"input": "A",
"output": "1"
},
{
"input": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ... | 62 | 0 | 3 | 15,619 | |
708 | Recover the String | [
"constructive algorithms",
"greedy",
"implementation",
"math"
] | null | null | For each string *s* consisting of characters '0' and '1' one can define four integers *a*00, *a*01, *a*10 and *a*11, where *a**xy* is the number of subsequences of length 2 of the string *s* equal to the sequence {*x*,<=*y*}.
In these problem you are given four integers *a*00, *a*01, *a*10, *a*11 and have to find any non-empty string *s* that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1<=000<=000. | The only line of the input contains four non-negative integers *a*00, *a*01, *a*10 and *a*11. Each of them doesn't exceed 109. | If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1<=000<=000. | [
"1 2 3 4\n",
"1 2 2 1\n"
] | [
"Impossible\n",
"0110\n"
] | none | [
{
"input": "1 2 3 4",
"output": "Impossible"
},
{
"input": "1 2 2 1",
"output": "0110"
},
{
"input": "10 7 28 21",
"output": "011111110000"
},
{
"input": "0 0 0 0",
"output": "0"
},
{
"input": "499928010 820999488 178951395 499991253",
"output": "0000000000000... | 77 | 2,764,800 | 0 | 15,635 | |
468 | Two Sets | [
"2-sat",
"dfs and similar",
"dsu",
"graph matchings",
"greedy"
] | null | null | Little X has *n* distinct integers: *p*1,<=*p*2,<=...,<=*p**n*. He wants to divide all of them into two sets *A* and *B*. The following two conditions must be satisfied:
- If number *x* belongs to set *A*, then number *a*<=-<=*x* must also belong to set *A*. - If number *x* belongs to set *B*, then number *b*<=-<=*x* must also belong to set *B*.
Help Little X divide the numbers into two sets or determine that it's impossible. | The first line contains three space-separated integers *n*,<=*a*,<=*b* (1<=β€<=*n*<=β€<=105;Β 1<=β€<=*a*,<=*b*<=β€<=109). The next line contains *n* space-separated distinct integers *p*1,<=*p*2,<=...,<=*p**n*Β (1<=β€<=*p**i*<=β€<=109). | If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print *n* integers: *b*1,<=*b*2,<=...,<=*b**n* (*b**i* equals either 0, or 1), describing the division. If *b**i* equals to 0, then *p**i* belongs to set *A*, otherwise it belongs to set *B*.
If it's impossible, print "NO" (without the quotes). | [
"4 5 9\n2 3 4 5\n",
"3 3 4\n1 2 4\n"
] | [
"YES\n0 0 1 1\n",
"NO\n"
] | It's OK if all the numbers are in the same set, and the other one is empty. | [
{
"input": "4 5 9\n2 3 4 5",
"output": "YES\n0 0 1 1"
},
{
"input": "3 3 4\n1 2 4",
"output": "NO"
},
{
"input": "100 8883 915\n1599 4666 663 3646 754 2113 2200 3884 4082 1640 3795 2564 2711 2766 1122 4525 1779 2678 2816 2182 1028 2337 4918 1273 4141 217 2682 1756 309 4744 915 1351 3302 ... | 77 | 8,294,400 | -1 | 15,640 | |
831 | Jury Marks | [
"brute force",
"constructive algorithms"
] | null | null | Polycarp watched TV-show where *k* jury members one by one rated a participant by adding him a certain number of points (may be negative, i.Β e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the *i*-th jury member gave *a**i* points.
Polycarp does not remember how many points the participant had before this *k* marks were given, but he remembers that among the scores announced after each of the *k* judges rated the participant there were *n* (*n*<=β€<=*k*) values *b*1,<=*b*2,<=...,<=*b**n* (it is guaranteed that all values *b**j* are distinct). It is possible that Polycarp remembers not all of the scores announced, i.Β e. *n*<=<<=*k*. Note that the initial score wasn't announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant. | The first line contains two integers *k* and *n* (1<=β€<=*n*<=β€<=*k*<=β€<=2<=000) β the number of jury members and the number of scores Polycarp remembers.
The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (<=-<=2<=000<=β€<=*a**i*<=β€<=2<=000) β jury's marks in chronological order.
The third line contains *n* distinct integers *b*1,<=*b*2,<=...,<=*b**n* (<=-<=4<=000<=000<=β€<=*b**j*<=β€<=4<=000<=000) β the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order. | Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes). | [
"4 1\n-5 5 0 20\n10\n",
"2 2\n-2000 -2000\n3998000 4000000\n"
] | [
"3\n",
"1\n"
] | The answer for the first example is 3 because initially the participant could have β-β10, 10 or 15 points.
In the second example there is only one correct initial score equaling to 4β002β000. | [
{
"input": "4 1\n-5 5 0 20\n10",
"output": "3"
},
{
"input": "2 2\n-2000 -2000\n3998000 4000000",
"output": "1"
},
{
"input": "1 1\n-577\n1273042",
"output": "1"
},
{
"input": "2 1\n614 -1943\n3874445",
"output": "2"
},
{
"input": "3 1\n1416 -1483 1844\n3261895",
... | 2,000 | 10,854,400 | 0 | 15,690 | |
659 | Qualifying Contest | [
"constructive algorithms",
"sortings"
] | null | null | Very soon Berland will hold a School Team Programming Olympiad. From each of the *m* Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by *n* Berland students. There were at least two schoolboys participating from each of the *m* regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive.
The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest.
Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. | The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=100<=000, 1<=β€<=*m*<=β€<=10<=000, *n*<=β₯<=2*m*)Β β the number of participants of the qualifying contest and the number of regions in Berland.
Next *n* lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to *m*) and the number of points scored by the participant (integer from 0 to 800, inclusive).
It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the *m* regions. The surnames that only differ in letter cases, should be considered distinct. | Print *m* lines. On the *i*-th line print the team of the *i*-th regionΒ β the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. | [
"5 2\nIvanov 1 763\nAndreev 2 800\nPetrov 1 595\nSidorov 1 790\nSemenov 2 503\n",
"5 2\nIvanov 1 800\nAndreev 2 763\nPetrov 1 800\nSidorov 1 800\nSemenov 2 503\n"
] | [
"Sidorov Ivanov\nAndreev Semenov\n",
"?\nAndreev Semenov\n"
] | In the first sample region teams are uniquely determined.
In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. | [
{
"input": "5 2\nIvanov 1 763\nAndreev 2 800\nPetrov 1 595\nSidorov 1 790\nSemenov 2 503",
"output": "Sidorov Ivanov\nAndreev Semenov"
},
{
"input": "5 2\nIvanov 1 800\nAndreev 2 763\nPetrov 1 800\nSidorov 1 800\nSemenov 2 503",
"output": "?\nAndreev Semenov"
},
{
"input": "10 2\nSHiBIEz... | 374 | 17,817,600 | 3 | 15,760 | |
901 | GCD of Polynomials | [
"constructive algorithms",
"math"
] | null | null | Suppose you have two polynomials and . Then polynomial can be uniquely represented in the following way:
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, denotes the degree of polynomial *P*(*x*). is called the remainder of division of polynomial by polynomial , it is also denoted as .
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials . If the polynomial is zero, the result is , otherwise the result is the value the algorithm returns for pair . On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer *n*. You have to build two polynomials with degrees not greater than *n*, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of *x*) are equal to one, and the described Euclid's algorithm performs exactly *n* steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair to pair . | You are given a single integer *n* (1<=β€<=*n*<=β€<=150)Β β the number of steps of the algorithm you need to reach. | Print two polynomials in the following format.
In the first line print a single integer *m* (0<=β€<=*m*<=β€<=*n*)Β β the degree of the polynomial.
In the second line print *m*<=+<=1 integers between <=-<=1 and 1Β β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly *n* steps when called using these polynomials.
If there is no answer for the given *n*, print -1.
If there are multiple answer, print any of them. | [
"1\n",
"2\n"
] | [
"1\n0 1\n0\n1\n",
"2\n-1 0 1\n1\n0 1\n"
] | In the second example you can print polynomials *x*<sup class="upper-index">2</sup>β-β1 and *x*. The sequence of transitions is
There are two steps in it. | [
{
"input": "1",
"output": "1\n0 1\n0\n1"
},
{
"input": "2",
"output": "2\n-1 0 1\n1\n0 1"
},
{
"input": "3",
"output": "3\n0 0 0 1\n2\n-1 0 1"
},
{
"input": "4",
"output": "4\n1 0 -1 0 1\n3\n0 0 0 1"
},
{
"input": "5",
"output": "5\n0 1 0 0 0 1\n4\n1 0 -1 0 1"... | 46 | 0 | 3 | 15,763 | |
0 | none | [
"none"
] | null | null | Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.
Artem wants to create a basic multiset of integers. He wants these structure to support operations of three types:
1. Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer. 1. Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset. 1. Count the number of instances of the given integer that are stored in the multiset.
But what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example.
- First Artem adds integer 5 to the multiset at the 1-st moment of time. - Then Artem adds integer 3 to the multiset at the moment 5. - Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1. - Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0. - Then Artem goes back in time again and removes 5 from the multiset at moment 3. - Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment 3.
Note that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes.
Help Artem implement time travellers multiset. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of Artem's queries.
Then follow *n* lines with queries descriptions. Each of them contains three integers *a**i*, *t**i* and *x**i* (1<=β€<=*a**i*<=β€<=3, 1<=β€<=*t**i*,<=*x**i*<=β€<=109)Β β type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent. | For each ask operation output the number of instances of integer being queried at the given moment of time. | [
"6\n1 1 5\n3 5 5\n1 2 5\n3 6 5\n2 3 5\n3 7 5\n",
"3\n1 1 1\n2 2 1\n3 3 1\n"
] | [
"1\n2\n1\n",
"0\n"
] | none | [
{
"input": "6\n1 1 5\n3 5 5\n1 2 5\n3 6 5\n2 3 5\n3 7 5",
"output": "1\n2\n1"
},
{
"input": "3\n1 1 1\n2 2 1\n3 3 1",
"output": "0"
},
{
"input": "10\n1 1 1000000000\n1 4 1000000000\n2 2 1000000000\n1 5 1000000000\n1 8 1000000000\n2 15 1000000000\n3 3 1000000000\n3 10 1000000000\n3 6 100... | 46 | 5,324,800 | 0 | 15,765 | |
0 | none | [
"none"
] | null | null | Fox Ciel is participating in a party in Prime Kingdom. There are *n* foxes there (include Fox Ciel). The i-th fox is *a**i* years old.
They will have dinner around some round tables. You want to distribute foxes such that:
1. Each fox is sitting at some table. 1. Each table has at least 3 foxes sitting around it. 1. The sum of ages of any two adjacent foxes around each table should be a prime number.
If *k* foxes *f*1, *f*2, ..., *f**k* are sitting around table in clockwise order, then for 1<=β€<=*i*<=β€<=*k*<=-<=1: *f**i* and *f**i*<=+<=1 are adjacent, and *f*1 and *f**k* are also adjacent.
If it is possible to distribute the foxes in the desired manner, find out a way to do that. | The first line contains single integer *n* (3<=β€<=*n*<=β€<=200): the number of foxes in this party.
The second line contains *n* integers *a**i* (2<=β€<=*a**i*<=β€<=104). | If it is impossible to do this, output "Impossible".
Otherwise, in the first line output an integer *m* (): the number of tables.
Then output *m* lines, each line should start with an integer *k* -=β the number of foxes around that table, and then *k* numbers β indices of fox sitting around that table in clockwise order.
If there are several possible arrangements, output any of them. | [
"4\n3 4 8 9\n",
"5\n2 2 2 2 2\n",
"12\n2 3 4 5 6 7 8 9 10 11 12 13\n",
"24\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25\n"
] | [
"1\n4 1 2 4 3\n",
"Impossible\n",
"1\n12 1 2 3 6 5 12 9 8 7 10 11 4\n",
"3\n6 1 2 3 6 5 4\n10 7 8 9 12 15 14 13 16 11 10\n8 17 18 23 22 19 20 21 24\n"
] | In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.
In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number. | [
{
"input": "4\n3 4 8 9",
"output": "1\n4 1 2 4 3"
},
{
"input": "5\n2 2 2 2 2",
"output": "Impossible"
},
{
"input": "12\n2 3 4 5 6 7 8 9 10 11 12 13",
"output": "1\n12 1 2 3 6 5 12 9 8 7 10 11 4"
},
{
"input": "24\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24... | 62 | 0 | 0 | 15,778 | |
803 | Roma and Poker | [
"dp",
"graphs"
] | null | null | Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.
Last evening Roma started to play poker. He decided to spend no more than *k* virtual bourles β he will stop immediately if the number of his loses exceeds the number of his wins by *k*. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by *k*.
Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won *k* bourles or he lost.
The sequence written by Roma is a string *s* consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:
- In the end the absolute difference between the number of wins and loses is equal to *k*; - There is no hand such that the absolute difference before this hand was equal to *k*.
Help Roma to restore any such sequence. | The first line contains two numbers *n* (the length of Roma's sequence) and *k* (1<=β€<=*n*,<=*k*<=β€<=1000).
The second line contains the sequence *s* consisting of characters W, L, D and ?. There are exactly *n* characters in this sequence. | If there is no valid sequence that can be obtained from *s* by replacing all ? characters by W, L or D, print NO.
Otherwise print this sequence. If there are multiple answers, print any of them. | [
"3 2\nL??\n",
"3 1\nW??\n",
"20 5\n?LLLLLWWWWW?????????\n"
] | [
"LDL\n",
"NO\n",
"WLLLLLWWWWWWWWLWLWDW\n"
] | none | [
{
"input": "3 2\nL??",
"output": "LDL"
},
{
"input": "3 1\nW??",
"output": "NO"
},
{
"input": "20 5\n?LLLLLWWWWW?????????",
"output": "WLLLLLWWWWWWWWLWLWDW"
},
{
"input": "5 5\n?WDDD",
"output": "NO"
},
{
"input": "5 3\n??D??",
"output": "WWDDW"
},
{
"... | 233 | 43,315,200 | 3 | 15,800 | |
910 | Minimum Sum | [
"constructive algorithms",
"greedy",
"math"
] | null | null | Petya has *n* positive integers *a*1,<=*a*2,<=...,<=*a**n*.
His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to 'j' and replaced all digits 0 with one letter, all digits 1 with another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'.
Your task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=1<=000) β the number of Petya's numbers.
Each of the following lines contains non-empty string *s**i* consisting of lowercase Latin letters from 'a' to 'j' β the Petya's numbers after Vasya's joke. The length of each string does not exceed six characters. | Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests. | [
"3\nab\nde\naj\n",
"5\nabcdef\nghij\nbdef\naccbd\ng\n",
"3\naa\njj\naa\n"
] | [
"47\n",
"136542\n",
"44\n"
] | In the first example, you need to replace the letter 'a' with the digit 1, the letter 'b' with the digit 0, the letter 'd' with the digit 2, the letter 'e' with the digit 3, and the letter 'j' with the digit 4. So after the restoration numbers will look like [10,β23,β14]. The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration.
In the second example the numbers after the restoration can look like: [120468,β3579,β2468,β10024,β3].
In the second example the numbers after the restoration can look like: [11,β22,β11]. | [
{
"input": "3\nab\nde\naj",
"output": "47"
},
{
"input": "5\nabcdef\nghij\nbdef\naccbd\ng",
"output": "136542"
},
{
"input": "3\naa\njj\naa",
"output": "44"
},
{
"input": "9\na\nb\nc\nd\nf\ng\nh\ni\nj",
"output": "45"
},
{
"input": "5\nbdgbh\nadi\naa\ngjh\ngh",
... | 31 | 5,632,000 | 0 | 15,809 | |
42 | Strange town | [
"constructive algorithms",
"math"
] | D. Strange town | 2 | 256 | Volodya has recently visited a very odd town. There are *N* tourist attractions in the town and every two of them are connected by a bidirectional road. Each road has some travel price (natural number) assigned to it and all prices are distinct. But the most striking thing about this town is that each city sightseeing tour has the same total price! That is, if we choose any city sightseeing tour β a cycle which visits every attraction exactly once β the sum of the costs of the tour roads is independent of the tour. Volodya is curious if you can find such price system with all road prices not greater than 1000. | Input contains just one natural number (3<=β€<=*N*<=β€<=20) β the number of town attractions. | Output should contain *N* rows containing *N* positive integer numbers each β the adjacency matrix of the prices graph (thus, *j*-th number in *i*-th row should be equal to the price of the road between the *j*-th and the *i*-th attraction). Diagonal numbers should be equal to zero. All numbers should not be greater than 1000. All prices should be positive and pairwise distinct. If there are several solutions, output any of them. | [
"3\n"
] | [
"0 3 4 \n3 0 5 \n4 5 0 \n"
] | none | [
{
"input": "3",
"output": "0 3 4 \n3 0 5 \n4 5 0 "
},
{
"input": "4",
"output": "0 3 4 6 \n3 0 5 7 \n4 5 0 8 \n6 7 8 0 "
},
{
"input": "5",
"output": "0 3 4 6 9 \n3 0 5 7 10 \n4 5 0 8 11 \n6 7 8 0 13 \n9 10 11 13 0 "
},
{
"input": "6",
"output": "0 3 4 6 9 14 \n3 0 5 7 10... | 124 | 6,963,200 | 3.95603 | 15,810 |
638 | Three-dimensional Turtle Super Computer | [
"brute force",
"dfs and similar",
"graphs"
] | null | null | A super computer has been built in the Turtle Academy of Sciences. The computer consists of *n*Β·*m*Β·*k* CPUs. The architecture was the paralellepiped of size *n*<=Γ<=*m*<=Γ<=*k*, split into 1<=Γ<=1<=Γ<=1 cells, each cell contains exactly one CPU. Thus, each CPU can be simultaneously identified as a group of three numbers from the layer number from 1 to *n*, the line number from 1 to *m* and the column number from 1 to *k*.
In the process of the Super Computer's work the CPUs can send each other messages by the famous turtle scheme: CPU (*x*,<=*y*,<=*z*) can send messages to CPUs (*x*<=+<=1,<=*y*,<=*z*), (*x*,<=*y*<=+<=1,<=*z*) and (*x*,<=*y*,<=*z*<=+<=1) (of course, if they exist), there is no feedback, that is, CPUs (*x*<=+<=1,<=*y*,<=*z*), (*x*,<=*y*<=+<=1,<=*z*) and (*x*,<=*y*,<=*z*<=+<=1) cannot send messages to CPU (*x*,<=*y*,<=*z*).
Over time some CPUs broke down and stopped working. Such CPUs cannot send messages, receive messages or serve as intermediates in transmitting messages. We will say that CPU (*a*,<=*b*,<=*c*) controls CPU (*d*,<=*e*,<=*f*) , if there is a chain of CPUs (*x**i*,<=*y**i*,<=*z**i*), such that (*x*1<==<=*a*,<=*y*1<==<=*b*,<=*z*1<==<=*c*), (*x**p*<==<=*d*,<=*y**p*<==<=*e*,<=*z**p*<==<=*f*) (here and below *p* is the length of the chain) and the CPU in the chain with number *i* (*i*<=<<=*p*) can send messages to CPU *i*<=+<=1.
Turtles are quite concerned about the denial-proofness of the system of communication between the remaining CPUs. For that they want to know the number of critical CPUs. A CPU (*x*,<=*y*,<=*z*) is critical, if turning it off will disrupt some control, that is, if there are two distinctive from (*x*,<=*y*,<=*z*) CPUs: (*a*,<=*b*,<=*c*) and (*d*,<=*e*,<=*f*), such that (*a*,<=*b*,<=*c*) controls (*d*,<=*e*,<=*f*) before (*x*,<=*y*,<=*z*) is turned off and stopped controlling it after the turning off. | The first line contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*,<=*k*<=β€<=100)Β β the dimensions of the Super Computer.
Then *n* blocks follow, describing the current state of the processes. The blocks correspond to the layers of the Super Computer in the order from 1 to *n*. Each block consists of *m* lines, *k* characters in each β the description of a layer in the format of an *m*<=Γ<=*k* table. Thus, the state of the CPU (*x*,<=*y*,<=*z*) is corresponded to the *z*-th character of the *y*-th line of the block number *x*. Character "1" corresponds to a working CPU and character "0" corresponds to a malfunctioning one. The blocks are separated by exactly one empty line. | Print a single integer β the number of critical CPUs, that is, such that turning only this CPU off will disrupt some control. | [
"2 2 3\n000\n000\n\n111\n111\n",
"3 3 3\n111\n111\n111\n\n111\n111\n111\n\n111\n111\n111\n",
"1 1 10\n0101010101\n"
] | [
"2\n",
"19\n",
"0\n"
] | In the first sample the whole first layer of CPUs is malfunctional. In the second layer when CPU (2,β1,β2) turns off, it disrupts the control by CPU (2,β1,β3) over CPU (2,β1,β1), and when CPU (2,β2,β2) is turned off, it disrupts the control over CPU (2,β2,β3) by CPU (2,β2,β1).
In the second sample all processors except for the corner ones are critical.
In the third sample there is not a single processor controlling another processor, so the answer is 0. | [
{
"input": "2 2 3\n000\n000\n\n111\n111",
"output": "2"
},
{
"input": "3 3 3\n111\n111\n111\n\n111\n111\n111\n\n111\n111\n111",
"output": "19"
},
{
"input": "1 1 10\n0101010101",
"output": "0"
},
{
"input": "1 1 1\n0",
"output": "0"
},
{
"input": "1 1 1\n1",
"... | 30 | 0 | 0 | 15,818 | |
106 | Space Rescuers | [
"geometry",
"ternary search"
] | E. Space Rescuers | 2 | 256 | The Galaxy contains *n* planets, there are many different living creatures inhabiting each planet. And each creature can get into troubles! Space rescuers know it perfectly well and they are always ready to help anyone who really needs help. All you need to do is call for them.
Now the space rescuers plan to build the largest in the history of the Galaxy rescue station; however, the rescue station's location is yet to be determined. As some cases are real emergencies, the rescuers want to find such a point in the Galaxy from which it would be possible to get to the remotest planet in the minimum possible time. In other words, the rescuers need such point in the space that the distance between it and the planet remotest from it was minimal (if we compare this point with all other possible points in the space). Unfortunately, the rescuers can't sole this problem.
As the planets are quite remote from each other, they can be considered as points in Euclidean three-dimensional space. The distance between points (*x**i*,<=*y**i*,<=*z**i*) and (*x**j*,<=*y**j*,<=*z**j*) can be calculated by the formula . The rescue station can be positioned in any point in the space. It can also coincide with some planet.
Galaxy is in danger! Save the space rescuers and find the required point for them. | The first line of the input file contains integer *n* β the number of planets (1<=β€<=*N*<=β€<=100). Each of the following *n* lines contains information about the planets. The *i*-th line contains three integers *x**i*,<=*y**i*,<=*z**i* β the coordinates of the *i*-th planet (<=-<=104<=β€<=*x**i*,<=*y**i*,<=*z**i*<=β€<=104, 1<=β€<=*i*<=β€<=*n*). No two planets coincide. | Print on the first line of the output file three space-separated real numbers *x*0,<=*y*0,<=*z*0 β the coordinates for the future base. If there are several solutions, you are allowed to print any of them. The answer will be accepted if the distance from this point to the remotest planet will differ from the juries' variant in no more than 10<=-<=6 in absolute or relative value. | [
"5\n5 0 0\n-5 0 0\n0 3 4\n4 -3 0\n2 2 -2\n"
] | [
"0.000 0.000 0.000\n"
] | none | [
{
"input": "5\n5 0 0\n-5 0 0\n0 3 4\n4 -3 0\n2 2 -2",
"output": "-0.0000000017 -0.0000000319 0.0000000473"
},
{
"input": "4\n-2 -9 1\n10 4 0\n-1 1 0\n3 -10 -4",
"output": "4.0000068501 -2.5000015036 0.5000626514"
},
{
"input": "5\n6 0 -4\n8 1 5\n-8 5 -6\n-2 -4 -3\n8 -2 1",
"output": ... | 218 | 6,860,800 | -1 | 15,822 |
852 | Casinos and travel | [
"dp"
] | null | null | John has just bought a new car and is planning a journey around the country. Country has *N* cities, some of which are connected by bidirectional roads. There are *N*<=-<=1 roads and every city is reachable from any other city. Cities are labeled from 1 to *N*.
John first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules.
To select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey.
He also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey.
In how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 109<=+<=7. | In the first line, a positive integer *N* (1<=β€<=*N*<=β€<=100000), the number of cities.
In the next *N*<=-<=1 lines, two numbers *a*,<= *b* (1<=β€<=*a*,<=*b*<=β€<=*N*) separated by a single space meaning that cities *a* and *b* are connected by a bidirectional road. | Output one number, the answer to the problem modulo 109<=+<=7. | [
"2\n1 2\n",
"3\n1 2\n2 3\n"
] | [
"4\n",
"10\n"
] | Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4.
Example 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4β+β2β+β4β=β10 options. | [
{
"input": "2\n1 2",
"output": "4"
},
{
"input": "3\n1 2\n2 3",
"output": "10"
},
{
"input": "4\n1 2\n2 3\n3 4",
"output": "24"
}
] | 904 | 6,553,600 | 0 | 15,852 | |
819 | Mister B and PR Shifts | [
"data structures",
"implementation",
"math"
] | null | null | Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation *p* of length *n* or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation *p* as .
Find a cyclic shift of permutation *p* with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id *k* (0<=β€<=*k*<=<<=*n*) of a cyclic shift of permutation *p* as the number of right shifts needed to reach this shift, for example:
- *k*<==<=0: shift *p*1,<=*p*2,<=... *p**n*, - *k*<==<=1: shift *p**n*,<=*p*1,<=... *p**n*<=-<=1, - ..., - *k*<==<=*n*<=-<=1: shift *p*2,<=*p*3,<=... *p**n*,<=*p*1. | First line contains single integer *n* (2<=β€<=*n*<=β€<=106) β the length of the permutation.
The second line contains *n* space-separated integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*)Β β the elements of the permutation. It is guaranteed that all elements are distinct. | Print two integers: the minimum deviation of cyclic shifts of permutation *p* and the id of such shift. If there are multiple solutions, print any of them. | [
"3\n1 2 3\n",
"3\n2 3 1\n",
"3\n3 2 1\n"
] | [
"0 0\n",
"0 1\n",
"2 1\n"
] | In the first sample test the given permutation *p* is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of *p* equals to 4, the deviation of the 1-st cyclic shift (1,β2,β3) equals to 0, the deviation of the 2-nd cyclic shift (3,β1,β2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of *p* equals to 4, the deviation of the 1-st cyclic shift (1,β3,β2) equals to 2, the deviation of the 2-nd cyclic shift (2,β1,β3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. | [
{
"input": "3\n1 2 3",
"output": "0 0"
},
{
"input": "3\n2 3 1",
"output": "0 1"
},
{
"input": "3\n3 2 1",
"output": "2 1"
},
{
"input": "2\n1 2",
"output": "0 0"
},
{
"input": "2\n2 1",
"output": "0 1"
},
{
"input": "10\n10 1 9 2 8 3 7 4 6 5",
"ou... | 61 | 5,529,600 | 0 | 15,898 | |
936 | Sleepy Game | [
"dfs and similar",
"dp",
"games",
"graphs"
] | null | null | Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of *n* vertices and *m* edges. One of the vertices contains a chip. Initially the chip is located at vertex *s*. Players take turns moving the chip along some edge of the graph. Petya goes first. Player who can't move the chip loses. If the game lasts for 106 turns the draw is announced.
Vasya was performing big laboratory work in "Spelling and parts of speech" at night before the game, so he fell asleep at the very beginning of the game. Petya decided to take the advantage of this situation and make both Petya's and Vasya's moves.
Your task is to help Petya find out if he can win the game or at least draw a tie. | The first line of input contain two integers *n* and *m*Β β the number of vertices and the number of edges in the graph (2<=β€<=*n*<=β€<=105, 0<=β€<=*m*<=β€<=2Β·105).
The next *n* lines contain the information about edges of the graph. *i*-th line (1<=β€<=*i*<=β€<=*n*) contains nonnegative integer *c**i*Β β number of vertices such that there is an edge from *i* to these vertices and *c**i* distinct integers *a**i*,<=*j*Β β indices of these vertices (1<=β€<=*a**i*,<=*j*<=β€<=*n*, *a**i*,<=*j*<=β <=*i*).
It is guaranteed that the total sum of *c**i* equals to *m*.
The next line contains index of vertex *s*Β β the initial position of the chip (1<=β€<=*s*<=β€<=*n*). | If Petya can win print Β«WinΒ» in the first line. In the next line print numbers *v*1,<=*v*2,<=...,<=*v**k* (1<=β€<=*k*<=β€<=106)Β β the sequence of vertices Petya should visit for the winning. Vertex *v*1 should coincide with *s*. For *i*<==<=1... *k*<=-<=1 there should be an edge from *v**i* to *v**i*<=+<=1 in the graph. There must be no possible move from vertex *v**k*. The sequence should be such that Petya wins the game.
If Petya can't win but can draw a tie, print Β«DrawΒ» in the only line. Otherwise print Β«LoseΒ». | [
"5 6\n2 2 3\n2 4 5\n1 4\n1 5\n0\n1\n",
"3 2\n1 3\n1 1\n0\n2\n",
"2 2\n1 2\n1 1\n1\n"
] | [
"Win\n1 2 4 5 \n",
"Lose\n",
"Draw\n"
] | In the first example the graph is the following:
Initially the chip is located at vertex 1. In the first move Petya moves the chip to vertex 2, after that he moves it to vertex 4 for Vasya. After that he moves to vertex 5. Now it is Vasya's turn and there is no possible move, so Petya wins.
In the second example the graph is the following:
Initially the chip is located at vertex 2. The only possible Petya's move is to go to vertex 1. After that he has to go to 3 for Vasya. Now it's Petya's turn but he has no possible move, so Petya loses.
In the third example the graph is the following:
Petya can't win, but he can move along the cycle, so the players will draw a tie. | [
{
"input": "5 6\n2 2 3\n2 4 5\n1 4\n1 5\n0\n1",
"output": "Win\n1 2 4 5 "
},
{
"input": "3 2\n1 3\n1 1\n0\n2",
"output": "Lose"
},
{
"input": "2 2\n1 2\n1 1\n1",
"output": "Draw"
},
{
"input": "92 69\n1 76\n1 14\n1 9\n0\n1 46\n1 80\n0\n0\n1 77\n0\n1 53\n1 81\n1 61\n1 40\n0\n1... | 1,138 | 25,190,400 | -1 | 15,906 | |
899 | Segments Removal | [
"data structures",
"dsu",
"flows",
"implementation",
"two pointers"
] | null | null | Vasya has an array of integers of length *n*.
Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13,<=13,<=7,<=7,<=7,<=2,<=2,<=2], then after one operation it becomes [13,<=13,<=2,<=2,<=2].
Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=200<=000) β the length of the array.
The second line contains a sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β Vasya's array. | Print the number of operations Vasya should make to remove all elements from the array. | [
"4\n2 5 5 2\n",
"5\n6 3 4 1 5\n",
"8\n4 4 4 2 2 100 100 100\n",
"6\n10 10 50 10 50 50\n"
] | [
"2\n",
"5\n",
"3\n",
"4\n"
] | In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2,β2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty.
In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array.
In the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second β all integers 100, in the third β all integers 2.
In the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50,β10,β50,β50]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50,β10]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that. | [
{
"input": "4\n2 5 5 2",
"output": "2"
},
{
"input": "5\n6 3 4 1 5",
"output": "5"
},
{
"input": "8\n4 4 4 2 2 100 100 100",
"output": "3"
},
{
"input": "6\n10 10 50 10 50 50",
"output": "4"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "100\n45 45... | 108 | 0 | 0 | 15,910 | |
172 | BHTML+BCSS | [
"*special",
"dfs and similar",
"expression parsing"
] | null | null | This problem is about imaginary languages BHTML and BCSS, which slightly resemble HTML and CSS. Read the problem statement carefully as the resemblance is rather slight and the problem uses very simplified analogs.
You are given a BHTML document that resembles HTML but is much simpler. It is recorded as a sequence of opening and closing tags. A tag that looks like "<tagname>" is called an opening tag and a tag that looks like "</tagname>" is called a closing tag. Besides, there are self-closing tags that are written as "<tagname/>" and in this problem they are fully equivalent to "<tagname></tagname>". All tagnames in this problem are strings consisting of lowercase Latin letters with length from 1 to 10 characters. Tagnames of different tags may coincide.
The document tags form a correct bracket sequence, that is, we can obtain an empty sequence from the given one using the following operations:
- remove any self-closing tag "<tagname/>", - remove a pair of an opening and a closing tag that go consecutively (in this order) and have the same names. In other words, remove substring "<tagname></tagname>".
For example, you may be given such document: "<header><p><a/><b></b></p></header><footer></footer>" but you may not be given documents "<a>", "<a></b>", "</a><a>" or "<a><b></a></b>".
Obviously, for any opening tag there is the only matching closing one β each such pair is called an element. A self-closing tag also is an element. Let's consider that one element is nested inside another one, if tags of the first element are between tags of the second one. An element is not nested to itself. For instance, in the example above element "b" is nested in "header" and in "p", but it isn't nested in "a" and "footer", also it isn't nested to itself ("b"). Element "header" has three elements nested in it, and "footer" has zero.
We need the BCSS rules to apply styles when displaying elements of the BHTML documents. Each rule is recorded as a subsequence of words "*x*1 *x*2 ... *x**n*". This rule has effect over all such elements *t*, which satisfy both conditions from the list:
- there is a sequence of nested elements with tagnames "*x*1", "*x*2", ..., "*x**n*" (that is, the second element is nested in the first one, the third element is nested in the second one and so on), - this sequence ends with element *t* (i.e. tagname of element *t* equals "*x**n*").
For example, element "b" meets the conditions of the rule "a b" if for element "b" exists element "a" in which it is nested. Element "c" meets the conditions of the rule "a b b c", if three elements exist: "a", "b", "b", and in the chain "a"-"b"-"b"-"c" each following element is nested in the previous one.
Given a BHTML document and a set of BCSS rules, write a program that determines the number of elements that meet the conditions of each rule. | The first line of the input contains a BHTML-document. The document has length from 4 to 106 characters. The document has a correct structure, doesn't contain spaces or any other unnecessary characters. Tagnames consist of lowercase Latin letters, their lengths are from 1 to 10 characters.
The second line contains an integer *m* (1<=β€<=*m*<=β€<=200) β the number of queries. Then *m* lines contain the queries, one per line. Each query is a sequence *x*1,<=*x*2,<=...,<=*x**n*, where *x**i* is the *i*-th element of the query, and *n* (1<=β€<=*n*<=β€<=200) is the number of elements in the query. The elements are separated by single spaces. Each query doesn't begin with and doesn't end with a space. Each query element is a sequence of lowercase Latin letters with length from 1 to 10. | Print *m* lines, the *j*-th line should contain the number of elements of the document that correspond to the *j*-th BCSS-rule. If there are no such elements at all, print on the line 0. | [
"<a><b><b></b></b></a><a><b></b><b><v/></b></a><b></b>\n4\na\na b b\na b\nb a\n",
"<b><aa/></b><aa><b/><b/></aa>\n5\naa b\nb\naa\nb aa\na\n"
] | [
"2\n1\n4\n0\n",
"2\n3\n2\n1\n0\n"
] | none | [] | 46 | 0 | 0 | 15,934 | |
336 | Vasily the Bear and Sequence | [
"brute force",
"greedy",
"implementation",
"number theory"
] | null | null | Vasily the bear has got a sequence of positive integers *a*1,<=*a*2,<=...,<=*a**n*. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum.
The beauty of the written out numbers *b*1,<=*b*2,<=...,<=*b**k* is such maximum non-negative integer *v*, that number *b*1 *and* *b*2 *and* ... *and* *b**k* is divisible by number 2*v* without a remainder. If such number *v* doesn't exist (that is, for any non-negative integer *v*, number *b*1 *and* *b*2 *and* ... *and* *b**k* is divisible by 2*v* without a remainder), the beauty of the written out numbers equals -1.
Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible.
Here expression *x* *and* *y* means applying the bitwise AND operation to numbers *x* and *y*. In programming languages C++ and Java this operation is represented by "&", in Pascal β by "and". | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a*1<=<<=*a*2<=<<=...<=<<=*a**n*<=β€<=109). | In the first line print a single integer *k* (*k*<=><=0), showing how many numbers to write out. In the second line print *k* integers *b*1,<=*b*2,<=...,<=*b**k* β the numbers to write out. You are allowed to print numbers *b*1,<=*b*2,<=...,<=*b**k* in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them. | [
"5\n1 2 3 4 5\n",
"3\n1 2 4\n"
] | [
"2\n4 5\n",
"1\n4\n"
] | none | [
{
"input": "5\n1 2 3 4 5",
"output": "2\n4 5"
},
{
"input": "3\n1 2 4",
"output": "1\n4"
},
{
"input": "3\n1 20 22",
"output": "2\n20 22"
},
{
"input": "10\n109070199 215498062 361633800 406156967 452258663 530571268 670482660 704334662 841023955 967424642",
"output": "6\... | 608 | 137,318,400 | 3 | 15,941 | |
475 | CGCDSSQ | [
"brute force",
"data structures",
"math"
] | null | null | Given a sequence of integers *a*1,<=...,<=*a**n* and *q* queries *x*1,<=...,<=*x**q* on it. For each query *x**i* you have to count the number of pairs (*l*,<=*r*) such that 1<=β€<=*l*<=β€<=*r*<=β€<=*n* and *gcd*(*a**l*,<=*a**l*<=+<=1,<=...,<=*a**r*)<==<=*x**i*.
is a greatest common divisor of *v*1,<=*v*2,<=...,<=*v**n*, that is equal to a largest positive integer that divides all *v**i*. | The first line of the input contains integer *n*, (1<=β€<=*n*<=β€<=105), denoting the length of the sequence. The next line contains *n* space separated integers *a*1,<=...,<=*a**n*, (1<=β€<=*a**i*<=β€<=109).
The third line of the input contains integer *q*, (1<=β€<=*q*<=β€<=3<=Γ<=105), denoting the number of queries. Then follows *q* lines, each contain an integer *x**i*, (1<=β€<=*x**i*<=β€<=109). | For each query print the result in a separate line. | [
"3\n2 6 3\n5\n1\n2\n3\n4\n6\n",
"7\n10 20 3 15 1000 60 16\n10\n1\n2\n3\n4\n5\n6\n10\n20\n60\n1000\n"
] | [
"1\n2\n2\n0\n1\n",
"14\n0\n2\n2\n2\n0\n2\n2\n1\n1\n"
] | none | [
{
"input": "3\n2 6 3\n5\n1\n2\n3\n4\n6",
"output": "1\n2\n2\n0\n1"
},
{
"input": "7\n10 20 3 15 1000 60 16\n10\n1\n2\n3\n4\n5\n6\n10\n20\n60\n1000",
"output": "14\n0\n2\n2\n2\n0\n2\n2\n1\n1"
},
{
"input": "10\n2 2 4 3 2 4 4 2 4 2\n104\n3\n3\n1\n4\n1\n1\n4\n1\n1\n3\n1\n1\n4\n1\n1\n1\n4\n3... | 2,000 | 5,529,600 | 0 | 15,994 | |
6 | Exposition | [
"binary search",
"data structures",
"dsu",
"trees",
"two pointers"
] | E. Exposition | 1 | 64 | There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than *k* millimeters.
The library has *n* volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is *h**i*. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task. | The first line of the input data contains two integer numbers separated by a space *n* (1<=β€<=*n*<=β€<=105) and *k* (0<=β€<=*k*<=β€<=106) β the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains *n* integer numbers separated by a space. Each number *h**i* (1<=β€<=*h**i*<=β€<=106) is the height of the *i*-th book in millimeters. | In the first line of the output data print two numbers *a* and *b* (separate them by a space), where *a* is the maximum amount of books the organizers can include into the exposition, and *b* β the amount of the time periods, during which Berlbury published *a* books, and the height difference between the lowest and the highest among these books is not more than *k* milllimeters.
In each of the following *b* lines print two integer numbers separated by a space β indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work. | [
"3 3\n14 12 10\n",
"2 0\n10 10\n",
"4 5\n8 19 10 13\n"
] | [
"2 2\n1 2\n2 3\n",
"2 1\n1 2\n",
"2 1\n3 4\n"
] | none | [
{
"input": "3 3\n14 12 10",
"output": "2 2\n1 2\n2 3"
},
{
"input": "2 0\n10 10",
"output": "2 1\n1 2"
},
{
"input": "4 5\n8 19 10 13",
"output": "2 1\n3 4"
},
{
"input": "1 1\n1",
"output": "1 1\n1 1"
},
{
"input": "2 10\n35 45",
"output": "2 1\n1 2"
},
{... | 1,500 | 38,297,600 | 0 | 16,004 |
975 | Ghosts | [
"geometry",
"math"
] | null | null | Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are $n$ ghosts in the universe, they move in the $OXY$ plane, each one of them has its own velocity that does not change in time: $\overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j}$ where $V_{x}$ is its speed on the $x$-axis and $V_{y}$ is on the $y$-axis.
A ghost $i$ has experience value $EX_i$, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind $GX = \sum_{i=1}^{n} EX_i$ will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time $T$, and magically all the ghosts were aligned on a line of the form $y = a \cdot x + b$. You have to compute what will be the experience index of the ghost kind $GX$ in the indefinite future, this is your task for today.
Note that when Tameem took the picture, $GX$ may already be greater than $0$, because many ghosts may have scared one another at any moment between $[-\infty, T]$. | The first line contains three integers $n$, $a$ and $b$ ($1 \leq n \leq 200000$, $1 \leq |a| \leq 10^9$, $0 \le |b| \le 10^9$)Β β the number of ghosts in the universe and the parameters of the straight line.
Each of the next $n$ lines contains three integers $x_i$, $V_{xi}$, $V_{yi}$ ($-10^9 \leq x_i \leq 10^9$, $-10^9 \leq V_{x i}, V_{y i} \leq 10^9$), where $x_i$ is the current $x$-coordinate of the $i$-th ghost (and $y_i = a \cdot x_i + b$).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all $(i,j)$ $x_i \neq x_j$ for $i \ne j$. | Output one line: experience index of the ghost kind $GX$ in the indefinite future. | [
"4 1 1\n1 -1 -1\n2 1 1\n3 1 1\n4 -1 -1\n",
"3 1 0\n-1 1 0\n0 0 -1\n1 -1 -2\n",
"3 1 0\n0 0 0\n1 0 0\n2 0 0\n"
] | [
"8\n",
"6\n",
"0\n"
] | There are four collisions $(1,2,T-0.5)$, $(1,3,T-1)$, $(2,4,T+1)$, $(3,4,T+0.5)$, where $(u,v,t)$ means a collision happened between ghosts $u$ and $v$ at moment $t$. At each collision, each ghost gained one experience point, this means that $GX = 4 \cdot 2 = 8$.
In the second test, all points will collide when $t = T + 1$.
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | [
{
"input": "4 1 1\n1 -1 -1\n2 1 1\n3 1 1\n4 -1 -1",
"output": "8"
},
{
"input": "3 1 0\n-1 1 0\n0 0 -1\n1 -1 -2",
"output": "6"
},
{
"input": "3 1 0\n0 0 0\n1 0 0\n2 0 0",
"output": "0"
},
{
"input": "10 7 -626288749\n795312099 49439844 266151109\n-842143911 23740808 62497340... | 1,341 | 26,624,000 | 3 | 16,062 | |
710 | Magic Odd Square | [
"constructive algorithms",
"math"
] | null | null | Find an *n*<=Γ<=*n* matrix with different numbers from 1 to *n*2, so the sum in each row, column and both main diagonals are odd. | The only line contains odd integer *n* (1<=β€<=*n*<=β€<=49). | Print *n* lines with *n* integers. All the integers should be different and from 1 to *n*2. The sum in each row, column and both main diagonals should be odd. | [
"1\n",
"3\n"
] | [
"1\n",
"2 1 4\n3 5 7\n6 9 8\n"
] | none | [
{
"input": "1",
"output": "1"
},
{
"input": "3",
"output": "2 1 4\n3 5 7\n6 9 8"
},
{
"input": "5",
"output": "2 4 1 6 8\n10 3 5 7 12\n9 11 13 15 17\n14 19 21 23 16\n18 20 25 22 24"
},
{
"input": "7",
"output": "2 4 6 1 8 10 12\n14 16 3 5 7 18 20\n22 9 11 13 15 17 24\n19 ... | 77 | 6,963,200 | 3 | 16,089 | |
507 | Breaking Good | [
"dfs and similar",
"dp",
"graphs",
"shortest paths"
] | null | null | Breaking Good is a new video game which a lot of gamers want to have. There is a certain level in the game that is really difficult even for experienced gamers.
Walter William, the main character of the game, wants to join a gang called Los Hermanos (The Brothers). The gang controls the whole country which consists of *n* cities with *m* bidirectional roads connecting them. There is no road is connecting a city to itself and for any two cities there is at most one road between them. The country is connected, in the other words, it is possible to reach any city from any other city using the given roads.
The roads aren't all working. There are some roads which need some more work to be performed to be completely functioning.
The gang is going to rob a bank! The bank is located in city 1. As usual, the hardest part is to escape to their headquarters where the police can't get them. The gang's headquarters is in city *n*. To gain the gang's trust, Walter is in charge of this operation, so he came up with a smart plan.
First of all the path which they are going to use on their way back from city 1 to their headquarters *n* must be as short as possible, since it is important to finish operation as fast as possible.
Then, gang has to blow up all other roads in country that don't lay on this path, in order to prevent any police reinforcements. In case of non-working road, they don't have to blow up it as it is already malfunctional.
If the chosen path has some roads that doesn't work they'll have to repair those roads before the operation.
Walter discovered that there was a lot of paths that satisfied the condition of being shortest possible so he decided to choose among them a path that minimizes the total number of affected roads (both roads that have to be blown up and roads to be repaired).
Can you help Walter complete his task and gain the gang's trust? | The first line of input contains two integers *n*,<=*m* (2<=β€<=*n*<=β€<=105, ), the number of cities and number of roads respectively.
In following *m* lines there are descriptions of roads. Each description consists of three integers *x*,<=*y*,<=*z* (1<=β€<=*x*,<=*y*<=β€<=*n*, ) meaning that there is a road connecting cities number *x* and *y*. If *z*<==<=1, this road is working, otherwise it is not. | In the first line output one integer *k*, the minimum possible number of roads affected by gang.
In the following *k* lines output three integers describing roads that should be affected. Each line should contain three integers *x*,<=*y*,<=*z* (1<=β€<=*x*,<=*y*<=β€<=*n*, ), cities connected by a road and the new state of a road. *z*<==<=1 indicates that the road between cities *x* and *y* should be repaired and *z*<==<=0 means that road should be blown up.
You may output roads in any order. Each affected road should appear exactly once. You may output cities connected by a single road in any order. If you output a road, it's original state should be different from *z*.
After performing all operations accroding to your plan, there should remain working only roads lying on some certain shortest past between city 1 and *n*.
If there are multiple optimal answers output any. | [
"2 1\n1 2 0\n",
"4 4\n1 2 1\n1 3 0\n2 3 1\n3 4 1\n",
"8 9\n1 2 0\n8 3 0\n2 3 1\n1 4 1\n8 7 0\n1 5 1\n4 6 1\n5 7 0\n6 8 0\n"
] | [
"1\n1 2 1\n",
"3\n1 2 0\n1 3 1\n2 3 0\n",
"3\n2 3 0\n1 5 0\n6 8 1\n"
] | In the first test the only path is 1β-β2
In the second test the only shortest path is 1β-β3β-β4
In the third test there are multiple shortest paths but the optimal is 1β-β4β-β6β-β8 | [
{
"input": "2 1\n1 2 0",
"output": "1\n1 2 1"
},
{
"input": "4 4\n1 2 1\n1 3 0\n2 3 1\n3 4 1",
"output": "3\n1 2 0\n1 3 1\n2 3 0"
},
{
"input": "8 9\n1 2 0\n8 3 0\n2 3 1\n1 4 1\n8 7 0\n1 5 1\n4 6 1\n5 7 0\n6 8 0",
"output": "3\n2 3 0\n1 5 0\n6 8 1"
},
{
"input": "9 10\n1 2 0\... | 1,325 | 69,222,400 | -1 | 16,105 | |
142 | Help Farmer | [
"brute force",
"math"
] | null | null | Once upon a time in the Kingdom of Far Far Away lived Sam the Farmer. Sam had a cow named Dawn and he was deeply attached to her. Sam would spend the whole summer stocking hay to feed Dawn in winter. Sam scythed hay and put it into haystack. As Sam was a bright farmer, he tried to make the process of storing hay simpler and more convenient to use. He collected the hay into cubical hay blocks of the same size. Then he stored the blocks in his barn. After a summer spent in hard toil Sam stored *A*Β·*B*Β·*C* hay blocks and stored them in a barn as a rectangular parallelepiped *A* layers high. Each layer had *B* rows and each row had *C* blocks.
At the end of the autumn Sam came into the barn to admire one more time the hay he'd been stacking during this hard summer. Unfortunately, Sam was horrified to see that the hay blocks had been carelessly scattered around the barn. The place was a complete mess. As it turned out, thieves had sneaked into the barn. They completely dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides. As a result, the barn only had a parallelepiped containing (*A*<=-<=1)<=Γ<=(*B*<=-<=2)<=Γ<=(*C*<=-<=2) hay blocks. To hide the evidence of the crime, the thieves had dissembled the parallelepiped into single 1<=Γ<=1<=Γ<=1 blocks and scattered them around the barn. After the theft Sam counted *n* hay blocks in the barn but he forgot numbers *A*, *B* ΠΈ *C*.
Given number *n*, find the minimally possible and maximally possible number of stolen hay blocks. | The only line contains integer *n* from the problem's statement (1<=β€<=*n*<=β€<=109). | Print space-separated minimum and maximum number of hay blocks that could have been stolen by the thieves.
Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. | [
"4\n",
"7\n",
"12\n"
] | [
"28 41\n",
"47 65\n",
"48 105\n"
] | Let's consider the first sample test. If initially Sam has a parallelepiped consisting of 32β=β2βΓβ4βΓβ4 hay blocks in his barn, then after the theft the barn has 4β=β(2β-β1)βΓβ(4β-β2)βΓβ(4β-β2) hay blocks left. Thus, the thieves could have stolen 32β-β4β=β28 hay blocks. If Sam initially had a parallelepiped consisting of 45β=β5βΓβ3βΓβ3 hay blocks in his barn, then after the theft the barn has 4β=β(5β-β1)βΓβ(3β-β2)βΓβ(3β-β2) hay blocks left. Thus, the thieves could have stolen 45β-β4β=β41 hay blocks. No other variants of the blocks' initial arrangement (that leave Sam with exactly 4 blocks after the theft) can permit the thieves to steal less than 28 or more than 41 blocks. | [
{
"input": "4",
"output": "28 41"
},
{
"input": "7",
"output": "47 65"
},
{
"input": "12",
"output": "48 105"
},
{
"input": "1",
"output": "17 17"
},
{
"input": "6",
"output": "34 57"
},
{
"input": "8",
"output": "40 73"
},
{
"input": "9",
... | 1,000 | 2,969,600 | 0 | 16,110 | |
440 | Balancer | [
"greedy",
"implementation"
] | null | null | Petya has *k* matches, placed in *n* matchboxes lying in a line from left to right. We know that *k* is divisible by *n*. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? | The first line contains integer *n* (1<=β€<=*n*<=β€<=50000). The second line contains *n* non-negative numbers that do not exceed 109, the *i*-th written number is the number of matches in the *i*-th matchbox. It is guaranteed that the total number of matches is divisible by *n*. | Print the total minimum number of moves. | [
"6\n1 6 2 5 3 7\n"
] | [
"12\n"
] | none | [
{
"input": "6\n1 6 2 5 3 7",
"output": "12"
},
{
"input": "6\n6 6 6 0 0 0",
"output": "27"
},
{
"input": "6\n0 0 0 6 6 6",
"output": "27"
},
{
"input": "6\n6 6 0 0 6 6",
"output": "12"
},
{
"input": "5\n0 0 0 0 0",
"output": "0"
},
{
"input": "10\n0 10... | 108 | 0 | 0 | 16,151 | |
60 | Mushroom Gnomes | [
"math",
"matrices"
] | E. Mushroom Gnomes | 3 | 256 | Once upon a time in the thicket of the mushroom forest lived mushroom gnomes. They were famous among their neighbors for their magic mushrooms. Their magic nature made it possible that between every two neighboring mushrooms every minute grew another mushroom with the weight equal to the sum of weights of two neighboring ones.
The mushroom gnomes loved it when everything was in order, that's why they always planted the mushrooms in one line in the order of their weights' increasing. Well... The gnomes planted the mushrooms and went to eat. After *x* minutes they returned and saw that new mushrooms had grown up, so that the increasing order had been violated. The gnomes replanted all the mushrooms in the correct order, that is, they sorted the mushrooms in the order of the weights' increasing. And went to eat again (those gnomes were quite big eaters). What total weights modulo *p* will the mushrooms have in another *y* minutes? | The first line contains four integers *n*, *x*, *y*, *p* (1<=β€<=*n*<=β€<=106,<=0<=β€<=*x*,<=*y*<=β€<=1018,<=*x*<=+<=*y*<=><=0,<=2<=β€<=*p*<=β€<=109) which represent the number of mushrooms, the number of minutes after the first replanting, the number of minutes after the second replanting and the module. The next line contains *n* integers *a**i* which represent the mushrooms' weight in the non-decreasing order (0<=β€<=*a**i*<=β€<=109).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). | The answer should contain a single number which is the total weights of the mushrooms modulo *p* in the end after *x*<=+<=*y* minutes. | [
"2 1 0 657276545\n1 2\n",
"2 1 1 888450282\n1 2\n",
"4 5 0 10000\n1 2 3 4\n"
] | [
"6\n",
"14\n",
"1825\n"
] | none | [
{
"input": "2 1 0 657276545\n1 2",
"output": "6"
},
{
"input": "2 1 1 888450282\n1 2",
"output": "14"
},
{
"input": "4 5 0 10000\n1 2 3 4",
"output": "1825"
},
{
"input": "4 0 8 78731972\n1 52 76 81",
"output": "1108850"
},
{
"input": "4 0 8 414790855\n1 88 97 99"... | 780 | 120,320,000 | 3.645887 | 16,231 |
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 "
},
{
... | 0 | 0 | -1 | 16,232 | |
734 | Anton and Making Potions | [
"binary search",
"dp",
"greedy",
"two pointers"
] | null | null | Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare *n* potions.
Anton has a special kettle, that can prepare one potions in *x* seconds. Also, he knows spells of two types that can faster the process of preparing potions.
1. Spells of this type speed up the preparation time of one potion. There are *m* spells of this type, the *i*-th of them costs *b**i* manapoints and changes the preparation time of each potion to *a**i* instead of *x*. 1. Spells of this type immediately prepare some number of potions. There are *k* such spells, the *i*-th of them costs *d**i* manapoints and instantly create *c**i* potions.
Anton can use no more than one spell of the first type and no more than one spell of the second type, and the total number of manapoints spent should not exceed *s*. Consider that all spells are used instantly and right before Anton starts to prepare potions.
Anton wants to get to the next level as fast as possible, so he is interested in the minimum number of time he needs to spent in order to prepare at least *n* potions. | The first line of the input contains three integers *n*, *m*, *k* (1<=β€<=*n*<=β€<=2Β·109,<=1<=β€<=*m*,<=*k*<=β€<=2Β·105)Β β the number of potions, Anton has to make, the number of spells of the first type and the number of spells of the second type.
The second line of the input contains two integers *x* and *s* (2<=β€<=*x*<=β€<=2Β·109,<=1<=β€<=*s*<=β€<=2Β·109)Β β the initial number of seconds required to prepare one potion and the number of manapoints Anton can use.
The third line contains *m* integers *a**i* (1<=β€<=*a**i*<=<<=*x*)Β β the number of seconds it will take to prepare one potion if the *i*-th spell of the first type is used.
The fourth line contains *m* integers *b**i* (1<=β€<=*b**i*<=β€<=2Β·109)Β β the number of manapoints to use the *i*-th spell of the first type.
There are *k* integers *c**i* (1<=β€<=*c**i*<=β€<=*n*) in the fifth lineΒ β the number of potions that will be immediately created if the *i*-th spell of the second type is used. It's guaranteed that *c**i* are not decreasing, i.e. *c**i*<=β€<=*c**j* if *i*<=<<=*j*.
The sixth line contains *k* integers *d**i* (1<=β€<=*d**i*<=β€<=2Β·109)Β β the number of manapoints required to use the *i*-th spell of the second type. It's guaranteed that *d**i* are not decreasing, i.e. *d**i*<=β€<=*d**j* if *i*<=<<=*j*. | Print one integerΒ β the minimum time one has to spent in order to prepare *n* potions. | [
"20 3 2\n10 99\n2 4 3\n20 10 40\n4 15\n10 80\n",
"20 3 2\n10 99\n2 4 3\n200 100 400\n4 15\n100 800\n"
] | [
"20\n",
"200\n"
] | In the first sample, the optimum answer is to use the second spell of the first type that costs 10 manapoints. Thus, the preparation time of each potion changes to 4 seconds. Also, Anton should use the second spell of the second type to instantly prepare 15 potions spending 80 manapoints. The total number of manapoints used is 10β+β80β=β90, and the preparation time is 4Β·5β=β20 seconds (15 potions were prepared instantly, and the remaining 5 will take 4 seconds each).
In the second sample, Anton can't use any of the spells, so he just prepares 20 potions, spending 10 seconds on each of them and the answer is 20Β·10β=β200. | [
{
"input": "20 3 2\n10 99\n2 4 3\n20 10 40\n4 15\n10 80",
"output": "20"
},
{
"input": "20 3 2\n10 99\n2 4 3\n200 100 400\n4 15\n100 800",
"output": "200"
},
{
"input": "10 3 3\n10 33\n1 7 6\n17 25 68\n2 9 10\n78 89 125",
"output": "10"
},
{
"input": "94 1 1\n26 324\n7\n236\n... | 61 | 2,867,200 | -1 | 16,255 | |
527 | Clique Problem | [
"data structures",
"dp",
"greedy",
"implementation",
"sortings"
] | null | null | The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph *G*. It is required to find a subset of vertices *C* of the maximum size such that any two of them are connected by an edge in graph *G*. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider *n* distinct points on a line. Let the *i*-th point have the coordinate *x**i* and weight *w**i*. Let's form graph *G*, whose vertices are these points and edges connect exactly the pairs of points (*i*,<=*j*), such that the distance between them is not less than the sum of their weights, or more formally: |*x**i*<=-<=*x**j*|<=β₯<=*w**i*<=+<=*w**j*.
Find the size of the maximum clique in such graph. | The first line contains the integer *n* (1<=β€<=*n*<=β€<=200<=000) β the number of points.
Each of the next *n* lines contains two numbers *x**i*, *w**i* (0<=β€<=*x**i*<=β€<=109,<=1<=β€<=*w**i*<=β€<=109) β the coordinate and the weight of a point. All *x**i* are different. | Print a single number β the number of vertexes in the maximum clique of the given graph. | [
"4\n2 3\n3 1\n6 1\n0 2\n"
] | [
"3\n"
] | If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test. | [
{
"input": "4\n2 3\n3 1\n6 1\n0 2",
"output": "3"
},
{
"input": "1\n42 23",
"output": "1"
},
{
"input": "2\n1 5\n2 6",
"output": "1"
},
{
"input": "2\n1 5\n12 6",
"output": "2"
},
{
"input": "1\n0 1",
"output": "1"
},
{
"input": "1\n1000000000 10000000... | 31 | 0 | -1 | 16,279 | |
813 | Bipartite Checking | [
"data structures",
"dsu",
"graphs"
] | null | null | You are given an undirected graph consisting of *n* vertices. Initially there are no edges in the graph. Also you are given *q* queries, each query either adds one undirected edge to the graph or removes it. After each query you have to check if the resulting graph is bipartite (that is, you can paint all vertices of the graph into two colors so that there is no edge connecting two vertices of the same color). | The first line contains two integers *n* and *q* (2<=β€<=*n*,<=*q*<=β€<=100000).
Then *q* lines follow. *i*th line contains two numbers *x**i* and *y**i* (1<=β€<=*x**i*<=<<=*y**i*<=β€<=*n*). These numbers describe *i*th query: if there is an edge between vertices *x**i* and *y**i*, then remove it, otherwise add it. | Print *q* lines. *i*th line must contain YES if the graph is bipartite after *i*th query, and NO otherwise. | [
"3 5\n2 3\n1 3\n1 2\n1 2\n1 2\n"
] | [
"YES\nYES\nNO\nYES\nNO\n"
] | none | [
{
"input": "3 5\n2 3\n1 3\n1 2\n1 2\n1 2",
"output": "YES\nYES\nNO\nYES\nNO"
},
{
"input": "5 10\n1 5\n2 5\n2 4\n1 4\n4 5\n2 4\n2 5\n1 4\n2 3\n1 2",
"output": "YES\nYES\nYES\nYES\nNO\nNO\nNO\nYES\nYES\nYES"
},
{
"input": "10 20\n1 10\n5 7\n1 2\n3 5\n3 6\n4 9\n3 4\n6 9\n4 8\n6 9\n7 8\n3 8... | 62 | 1,228,800 | 0 | 16,316 | |
551 | ZgukistringZ | [
"brute force",
"constructive algorithms",
"implementation",
"strings"
] | null | null | Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.
GukiZ has strings *a*, *b*, and *c*. He wants to obtain string *k* by swapping some letters in *a*, so that *k* should contain as many non-overlapping substrings equal either to *b* or *c* as possible. Substring of string *x* is a string formed by consecutive segment of characters from *x*. Two substrings of string *x* overlap if there is position *i* in string *x* occupied by both of them.
GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings *k*? | The first line contains string *a*, the second line contains string *b*, and the third line contains string *c* (1<=β€<=|*a*|,<=|*b*|,<=|*c*|<=β€<=105, where |*s*| denotes the length of string *s*).
All three strings consist only of lowercase English letters.
It is possible that *b* and *c* coincide. | Find one of possible strings *k*, as described in the problem statement. If there are multiple possible answers, print any of them. | [
"aaa\na\nb\n",
"pozdravstaklenidodiri\nniste\ndobri\n",
"abbbaaccca\nab\naca\n"
] | [
"aaa",
"nisteaadddiiklooprrvz",
"ababacabcc"
] | In the third sample, this optimal solutions has three non-overlaping substrings equal to either *b* or *c* on positions 1βββ2 (*ab*), 3βββ4 (*ab*), 5βββ7 (*aca*). In this sample, there exist many other optimal solutions, one of them would be *acaababbcc*. | [
{
"input": "aaa\na\nb",
"output": "aaa"
},
{
"input": "pozdravstaklenidodiri\nniste\ndobri",
"output": "nisteaadddiiklooprrvz"
},
{
"input": "abbbaaccca\nab\naca",
"output": "ababacabcc"
},
{
"input": "lemigazalemiolemilicomzalemljenje\nlemi\nzlo",
"output": "lemilemilemi... | 78 | 819,200 | 0 | 16,358 | |
77 | Falling Anvils | [
"math",
"probabilities"
] | B. Falling Anvils | 2 | 256 | For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all.
Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who throws anvils from the sky? From what height? We are sure that such questions have never troubled you!
It turns out that throwing an anvil properly is not an easy task at all. Let's describe one of the most popular anvil throwing models.
Let the height *p* of the potential victim vary in the range [0;*a*] and the direction of the wind *q* vary in the range [<=-<=*b*;*b*]. *p* and *q* could be any real (floating) numbers. Then we can assume that the anvil will fit the toon's head perfectly only if the following equation has at least one real root:
Determine the probability with which an aim can be successfully hit by an anvil.
You can assume that the *p* and *q* coefficients are chosen equiprobably and independently in their ranges. | The first line contains integer *t* (1<=β€<=*t*<=β€<=10000) β amount of testcases.
Each of the following *t* lines contain two space-separated integers *a* and *b* (0<=β€<=*a*,<=*b*<=β€<=106).
Pretests contain all the tests with 0<=<<=*a*<=<<=10,<=0<=β€<=*b*<=<<=10. | Print *t* lines β the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10<=-<=6. | [
"2\n4 2\n1 2\n"
] | [
"0.6250000000\n0.5312500000\n"
] | none | [
{
"input": "2\n4 2\n1 2",
"output": "0.6250000000\n0.5312500000"
},
{
"input": "90\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n1 2\n2 2\n3 2\n4 2\n5 2\n6 2\n7 2\n8 2\n9 2\n1 3\n2 3\n3 3\n4 3\n5 3\n6 3\n7 3\n8 3\n9 3\n1 4\n2 4\n3 4\n4 4\n5 4\n6 4\n7 4\n8 4\n9 4\n1 5\n2 5\n3 5\n4 5\n5 5\n6 5\n7 5\n8... | 92 | 0 | 0 | 16,362 |
798 | Mike and distribution | [
"constructive algorithms",
"sortings"
] | null | null | Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers *A*<==<=[*a*1,<=*a*2,<=...,<=*a**n*] and *B*<==<=[*b*1,<=*b*2,<=...,<=*b**n*] of length *n* each which he uses to ask people some quite peculiar questions.
To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select *k* numbers *P*<==<=[*p*1,<=*p*2,<=...,<=*p**k*] such that 1<=β€<=*p**i*<=β€<=*n* for 1<=β€<=*i*<=β€<=*k* and elements in *P* are distinct. Sequence *P* will represent indices of elements that you'll select from both sequences. He calls such a subset *P* "unfair" if and only if the following conditions are satisfied: 2Β·(*a**p*1<=+<=...<=+<=*a**p**k*) is greater than the sum of all elements from sequence *A*, and 2Β·(*b**p*1<=+<=...<=+<=*b**p**k*) is greater than the sum of all elements from the sequence *B*. Also, *k* should be smaller or equal to because it will be to easy to find sequence *P* if he allowed you to select too many elements!
Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of elements in the sequences.
On the second line there are *n* space-separated integers *a*1,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β elements of sequence *A*.
On the third line there are also *n* space-separated integers *b*1,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=109) β elements of sequence *B*. | On the first line output an integer *k* which represents the size of the found subset. *k* should be less or equal to .
On the next line print *k* integers *p*1,<=*p*2,<=...,<=*p**k* (1<=β€<=*p**i*<=β€<=*n*) β the elements of sequence *P*. You can print the numbers in any order you want. Elements in sequence *P* should be distinct. | [
"5\n8 7 4 8 3\n4 2 5 3 7\n"
] | [
"3\n1 4 5\n"
] | none | [
{
"input": "5\n8 7 4 8 3\n4 2 5 3 7",
"output": "3\n1 4 5"
},
{
"input": "27\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864\n67108864 33554432 16777216 8388608 4194304 2097152 1048576 524288 262144 1... | 46 | 0 | 0 | 16,478 | |
0 | none | [
"none"
] | null | null | Π’ΡΠΈ Π±ΡΠ°ΡΠ° Π΄ΠΎΠ³ΠΎΠ²ΠΎΡΠΈΠ»ΠΈΡΡ ΠΎ Π²ΡΡΡΠ΅ΡΠ΅. ΠΡΠΎΠ½ΡΠΌΠ΅ΡΡΠ΅ΠΌ Π±ΡΠ°ΡΡΠ΅Π² ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ: ΠΏΡΡΡΡ ΡΡΠ°ΡΡΠΈΠΉ Π±ΡΠ°Ρ ΠΈΠΌΠ΅Π΅Ρ Π½ΠΎΠΌΠ΅Ρ 1, ΡΡΠ΅Π΄Π½ΠΈΠΉ Π±ΡΠ°Ρ ΠΈΠΌΠ΅Π΅Ρ Π½ΠΎΠΌΠ΅Ρ 2, Π° ΠΌΠ»Π°Π΄ΡΠΈΠΉ Π±ΡΠ°ΡΒ β Π½ΠΎΠΌΠ΅Ρ 3.
ΠΠΎΠ³Π΄Π° ΠΏΡΠΈΡΠ»ΠΎ Π²ΡΠ΅ΠΌΡ Π²ΡΡΡΠ΅ΡΠΈ, ΠΎΠ΄ΠΈΠ½ ΠΈΠ· Π±ΡΠ°ΡΡΠ΅Π² ΠΎΠΏΠΎΠ·Π΄Π°Π». ΠΠΎ Π·Π°Π΄Π°Π½Π½ΡΠΌ Π½ΠΎΠΌΠ΅ΡΠ°ΠΌ Π΄Π²ΡΡ
Π±ΡΠ°ΡΡΠ΅Π², ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΡΠΈΡΠ»ΠΈ Π²ΠΎΠ²ΡΠ΅ΠΌΡ, Π²Π°ΠΌ ΠΏΡΠ΅Π΄ΡΡΠΎΠΈΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ Π½ΠΎΠΌΠ΅Ρ ΠΎΠΏΠΎΠ·Π΄Π°Π²ΡΠ΅Π³ΠΎ Π±ΡΠ°ΡΠ°. | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΡΡ Π΄Π²Π° ΡΠ°Π·Π»ΠΈΡΠ½ΡΡ
ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° *a* ΠΈ *b* (1<=β€<=*a*,<=*b*<=β€<=3, *a*<=β <=*b*)Β β Π½ΠΎΠΌΠ΅ΡΠ° Π±ΡΠ°ΡΡΠ΅Π², ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΡΠΈΡΠ»ΠΈ Π½Π° Π²ΡΡΡΠ΅ΡΡ Π²ΠΎΠ²ΡΠ΅ΠΌΡ. ΠΠΎΠΌΠ΅ΡΠ° Π΄Π°Π½Ρ Π² ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΠΎΠ΅ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎΒ β Π½ΠΎΠΌΠ΅Ρ Π±ΡΠ°ΡΠ°, ΠΊΠΎΡΠΎΡΡΠΉ ΠΎΠΏΠΎΠ·Π΄Π°Π» Π½Π° Π²ΡΡΡΠ΅ΡΡ. | [
"3 1\n"
] | [
"2\n"
] | none | [
{
"input": "3 1",
"output": "2"
},
{
"input": "2 1",
"output": "3"
},
{
"input": "2 3",
"output": "1"
},
{
"input": "1 2",
"output": "3"
},
{
"input": "1 3",
"output": "2"
},
{
"input": "3 2",
"output": "1"
}
] | 93 | 23,040,000 | 3 | 16,502 | |
855 | Salazar Slytherin's Locket | [
"bitmasks",
"dp"
] | null | null | Harry came to know from Dumbledore that Salazar Slytherin's locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black's mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry's former Defense Against the Dark Arts teacher.
Harry, Ron and Hermione are infiltrating the Ministry. Upon reaching Umbridge's office, they observed a code lock with a puzzle asking them to calculate count of magic numbers between two integers *l* and *r* (both inclusive).
Harry remembered from his detention time with Umbridge that she defined a magic number as a number which when converted to a given base *b*, all the digits from 0 to *b*<=-<=1 appear even number of times in its representation without any leading zeros.
You have to answer *q* queries to unlock the office. Each query has three integers *b**i*, *l**i* and *r**i*, the base and the range for which you have to find the count of magic numbers. | First line of input contains *q* (1<=β€<=*q*<=β€<=105)Β β number of queries.
Each of the next *q* lines contain three space separated integers *b**i*, *l**i*, *r**i* (2<=β€<=*b**i*<=β€<=10, 1<=β€<=*l**i*<=β€<=*r**i*<=β€<=1018). | You have to output *q* lines, each containing a single integer, the answer to the corresponding query. | [
"2\n2 4 9\n3 1 10\n",
"2\n2 1 100\n5 1 100\n"
] | [
"1\n2\n",
"21\n4\n"
] | In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get:
- 4β=β100<sub class="lower-index">2</sub>, - 5β=β101<sub class="lower-index">2</sub>, - 6β=β110<sub class="lower-index">2</sub>, - 7β=β111<sub class="lower-index">2</sub>, - 8β=β1000<sub class="lower-index">2</sub>, - 9β=β1001<sub class="lower-index">2</sub>.
Out of these, only base 2 representation of 9 has even number of 1 and 0. Thus, the answer is 1. | [
{
"input": "2\n2 4 9\n3 1 10",
"output": "1\n2"
},
{
"input": "2\n2 1 100\n5 1 100",
"output": "21\n4"
},
{
"input": "10\n4 108 114\n5 30 155\n8 193 197\n9 71 169\n2 163 166\n8 120 144\n8 22 151\n4 21 166\n2 46 127\n8 38 51",
"output": "0\n3\n0\n1\n3\n0\n5\n16\n9\n1"
},
{
"in... | 46 | 0 | 0 | 16,518 | |
306 | White, Black and White Again | [
"combinatorics",
"number theory"
] | null | null | Polycarpus is sure that his life fits the description: "first there is a white stripe, then a black one, then a white one again". So, Polycarpus is sure that this rule is going to fulfill during the next *n* days. Polycarpus knows that he is in for *w* good events and *b* not-so-good events. At least one event is going to take place during each day. As each day is unequivocally characterizes as a part of a white or a black stripe, then each day is going to have events of the same type only (ether good or not-so-good).
What is the number of distinct ways this scenario can develop over the next *n* days if Polycarpus is in for a white stripe (a stripe that has good events only, the stripe's length is at least 1 day), the a black stripe (a stripe that has not-so-good events only, the stripe's length is at least 1 day) and a white stripe again (a stripe that has good events only, the stripe's length is at least 1 day). Each of *n* days will belong to one of the three stripes only.
Note that even the events of the same type are distinct from each other. Even if some events occur on the same day, they go in some order (there are no simultaneous events).
Write a code that prints the number of possible configurations to sort the events into days. See the samples for clarifications on which scenarios should be considered distinct. Print the answer modulo 1000000009 (109<=+<=9). | The single line of the input contains integers *n*, *w* and *b* (3<=β€<=*n*<=β€<=4000, 2<=β€<=*w*<=β€<=4000, 1<=β€<=*b*<=β€<=4000) β the number of days, the number of good events and the number of not-so-good events. It is guaranteed that *w*<=+<=*b*<=β₯<=*n*. | Print the required number of ways modulo 1000000009 (109<=+<=9). | [
"3 2 1\n",
"4 2 2\n",
"3 2 2\n"
] | [
"2\n",
"4\n",
"4\n"
] | We'll represent the good events by numbers starting from 1 and the not-so-good events β by letters starting from 'a'. Vertical lines separate days.
In the first sample the possible ways are: "1|a|2" and "2|a|1". In the second sample the possible ways are: "1|a|b|2", "2|a|b|1", "1|b|a|2" and "2|b|a|1". In the third sample the possible ways are: "1|ab|2", "2|ab|1", "1|ba|2" and "2|ba|1". | [
{
"input": "3 2 1",
"output": "2"
},
{
"input": "4 2 2",
"output": "4"
},
{
"input": "3 2 2",
"output": "4"
},
{
"input": "3 3 1",
"output": "12"
},
{
"input": "3 2 2",
"output": "4"
},
{
"input": "3 3 3",
"output": "72"
},
{
"input": "4 2 ... | 124 | 0 | 0 | 16,520 | |
103 | Time to Raid Cowavans | [
"brute force",
"data structures",
"sortings"
] | D. Time to Raid Cowavans | 4 | 70 | As you know, the most intelligent beings on the Earth are, of course, cows. This conclusion was reached long ago by the Martian aliens, as well as a number of other intelligent civilizations from outer space.
Sometimes cows gather into cowavans. This seems to be seasonal. But at this time the cows become passive and react poorly to external stimuli. A cowavan is a perfect target for the Martian scientific saucer, it's time for large-scale abductions, or, as the Martians say, raids. Simply put, a cowavan is a set of cows in a row.
If we number all cows in the cowavan with positive integers from 1 to *n*, then we can formalize the popular model of abduction, known as the (*a*,<=*b*)-Cowavan Raid: first they steal a cow number *a*, then number *a*<=+<=*b*, then β number *a*<=+<=2Β·*b*, and so on, until the number of an abducted cow exceeds *n*. During one raid the cows are not renumbered.
The aliens would be happy to place all the cows on board of their hospitable ship, but unfortunately, the amount of cargo space is very, very limited. The researchers, knowing the mass of each cow in the cowavan, made *p* scenarios of the (*a*,<=*b*)-raid. Now they want to identify the following thing for each scenario individually: what total mass of pure beef will get on board of the ship. All the scenarios are independent, in the process of performing the calculations the cows are not being stolen. | The first line contains the only positive integer *n* (1<=β€<=*n*<=β€<=3Β·105) β the number of cows in the cowavan.
The second number contains *n* positive integer *w**i*, separated by spaces, where the *i*-th number describes the mass of the *i*-th cow in the cowavan (1<=β€<=*w**i*<=β€<=109).
The third line contains the only positive integer *p* β the number of scenarios of (*a*,<=*b*)-raids (1<=β€<=*p*<=β€<=3Β·105).
Each following line contains integer parameters *a* and *b* of the corresponding scenario (1<=β€<=*a*,<=*b*<=β€<=*n*). | Print for each scenario of the (*a*,<=*b*)-raid the total mass of cows, that can be stolen using only this scenario.
Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is recommended to use the cin, cout streams of the %I64d specificator. | [
"3\n1 2 3\n2\n1 1\n1 2\n",
"4\n2 3 5 7\n3\n1 3\n2 3\n2 2\n"
] | [
"6\n4\n",
"9\n3\n10\n"
] | none | [
{
"input": "3\n1 2 3\n2\n1 1\n1 2",
"output": "6\n4"
},
{
"input": "4\n2 3 5 7\n3\n1 3\n2 3\n2 2",
"output": "9\n3\n10"
},
{
"input": "5\n3 2 4 5 6\n8\n4 2\n3 1\n3 5\n3 4\n3 5\n5 5\n4 4\n5 3",
"output": "5\n15\n4\n4\n4\n6\n5\n6"
},
{
"input": "10\n10 10 7 10 2 8 9 6 4 9\n10\n... | 3,648 | 73,420,800 | 0 | 16,556 |
35 | Animals | [
"dp",
"greedy"
] | D. Animals | 2 | 64 | Once upon a time DravDe, an outstanding person famous for his professional achievements (as you must remember, he works in a warehouse storing Ogudar-Olok, a magical but non-alcoholic drink) came home after a hard day. That day he had to drink 9875 boxes of the drink and, having come home, he went to bed at once.
DravDe dreamt about managing a successful farm. He dreamt that every day one animal came to him and asked him to let it settle there. However, DravDe, being unimaginably kind, could send the animal away and it went, rejected. There were exactly *n* days in DravDeβs dream and the animal that came on the *i*-th day, ate exactly *c**i* tons of food daily starting from day *i*. But if one day the animal could not get the food it needed, it got really sad. At the very beginning of the dream there were exactly *X* tons of food on the farm.
DravDe woke up terrified...
When he retold the dream to you, he couldnβt remember how many animals were on the farm by the end of the *n*-th day any more, but he did remember that nobody got sad (as it was a happy farm) and that there was the maximum possible amount of the animals. Thatβs the number he wants you to find out.
It should be noticed that the animals arrived in the morning and DravDe only started to feed them in the afternoon, so that if an animal willing to join them is rejected, it canβt eat any farm food. But if the animal does join the farm, it eats daily from that day to the *n*-th. | The first input line contains integers *n* and *X* (1<=β€<=*n*<=β€<=100,<=1<=β€<=*X*<=β€<=104) β amount of days in DravDeβs dream and the total amount of food (in tons) that was there initially. The second line contains integers *c**i* (1<=β€<=*c**i*<=β€<=300). Numbers in the second line are divided by a space. | Output the only number β the maximum possible amount of animals on the farm by the end of the *n*-th day given that the food was enough for everybody. | [
"3 4\n1 1 1\n",
"3 6\n1 1 1\n"
] | [
"2\n",
"3\n"
] | Note to the first example: DravDe leaves the second and the third animal on the farm. The second animal will eat one ton of food on the second day and one ton on the third day. The third animal will eat one ton of food on the third day. | [
{
"input": "3 4\n1 1 1",
"output": "2"
},
{
"input": "3 6\n1 1 1",
"output": "3"
},
{
"input": "1 12\n1",
"output": "1"
},
{
"input": "3 100\n1 1 1",
"output": "3"
},
{
"input": "5 75\n1 1 1 1 1",
"output": "5"
},
{
"input": "7 115\n1 1 1 1 1 1 1",
... | 62 | 0 | -1 | 16,575 |
632 | Magic Matrix | [
"brute force",
"divide and conquer",
"graphs",
"matrices",
"trees"
] | null | null | You're given a matrix *A* of size *n*<=Γ<=*n*.
Let's call the matrix with nonnegative elements magic if it is symmetric (so *a**ij*<==<=*a**ji*), *a**ii*<==<=0 and *a**ij*<=β€<=*max*(*a**ik*,<=*a**jk*) for all triples *i*,<=*j*,<=*k*. Note that *i*,<=*j*,<=*k* do not need to be distinct.
Determine if the matrix is magic.
As the input/output can reach very huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2500) β the size of the matrix *A*.
Each of the next *n* lines contains *n* integers *a**ij* (0<=β€<=*a**ij*<=<<=109) β the elements of the matrix *A*.
Note that the given matrix not necessarily is symmetric and can be arbitrary. | Print ''MAGIC" (without quotes) if the given matrix *A* is magic. Otherwise print ''NOT MAGIC". | [
"3\n0 1 2\n1 0 2\n2 2 0\n",
"2\n0 1\n2 3\n",
"4\n0 1 2 3\n1 0 3 4\n2 3 0 5\n3 4 5 0\n"
] | [
"MAGIC\n",
"NOT MAGIC\n",
"NOT MAGIC\n"
] | none | [
{
"input": "3\n0 1 2\n1 0 2\n2 2 0",
"output": "MAGIC"
},
{
"input": "2\n0 1\n2 3",
"output": "NOT MAGIC"
},
{
"input": "4\n0 1 2 3\n1 0 3 4\n2 3 0 5\n3 4 5 0",
"output": "NOT MAGIC"
},
{
"input": "5\n0 2 5 9 5\n2 0 5 9 5\n5 5 0 9 4\n9 9 9 0 9\n5 5 4 9 0",
"output": "MAGI... | 46 | 0 | 0 | 16,582 | |
25 | Test | [
"hashing",
"strings"
] | E. Test | 2 | 256 | Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings β input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring *s*1, the second enters an infinite loop if the input data contains the substring *s*2, and the third requires too much memory if the input data contains the substring *s*3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions? | There are exactly 3 lines in the input data. The *i*-th line contains string *s**i*. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105. | Output one number β what is minimal length of the string, containing *s*1, *s*2 and *s*3 as substrings. | [
"ab\nbc\ncd\n",
"abacaba\nabaaba\nx\n"
] | [
"4\n",
"11\n"
] | none | [
{
"input": "ab\nbc\ncd",
"output": "4"
},
{
"input": "abacaba\nabaaba\nx",
"output": "11"
},
{
"input": "syvncqmfhautvxudqdhggz\nhrpxzeghsocjpicuixskfuzupytsgjsdiyb\nybcmnmnbpndbxlxbzhbfnqvwcffvrdhtickyqhupmcehls",
"output": "100"
},
{
"input": "jwdezvgfm\nmdoqvylpuvyk\nqylld... | 92 | 0 | 0 | 16,610 |
254 | Jury Size | [
"brute force",
"implementation"
] | null | null | In 2013, the writers of Berland State University should prepare problems for *n* Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to *n*. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number *i* should be prepared by *p**i* people for *t**i* days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.
For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.
In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | The first line contains integer *n* β the number of Olympiads in 2013 (1<=β€<=*n*<=β€<=100). Each of the following *n* lines contains four integers *m**i*, *d**i*, *p**i* and *t**i* β the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the *i*-th Olympiad (1<=β€<=*m**i*<=β€<=12, *d**i*<=β₯<=1, 1<=β€<=*p**i*,<=*t**i*<=β€<=100), *d**i* doesn't exceed the number of days in month *m**i*. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day.
Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | Print a single number β the minimum jury size. | [
"2\n5 23 1 2\n3 13 2 3\n",
"3\n12 9 2 1\n12 8 1 3\n12 8 2 2\n",
"1\n1 10 1 13\n"
] | [
"2\n",
"3\n",
"1\n"
] | none | [
{
"input": "2\n5 23 1 2\n3 13 2 3",
"output": "2"
},
{
"input": "3\n12 9 2 1\n12 8 1 3\n12 8 2 2",
"output": "3"
},
{
"input": "1\n1 10 1 13",
"output": "1"
},
{
"input": "3\n3 16 25 1\n3 15 9 34\n3 14 90 87",
"output": "99"
},
{
"input": "4\n2 15 52 53\n2 15 35 8... | 248 | 5,836,800 | -1 | 16,614 | |
552 | Vanya and Brackets | [
"brute force",
"dp",
"expression parsing",
"greedy",
"implementation",
"strings"
] | null | null | Vanya is doing his maths homework. He has an expression of form , where *x*1,<=*x*2,<=...,<=*x**n* are digits from 1 to 9, and sign represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression. | The first line contains expression *s* (1<=β€<=|*s*|<=β€<=5001, |*s*| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs <=+<= and <=*<=.
The number of signs <=*<= doesn't exceed 15. | In the first line print the maximum possible value of an expression. | [
"3+5*7+8*4\n",
"2+3*5\n",
"3*4*5\n"
] | [
"303\n",
"25\n",
"60\n"
] | Note to the first sample test. 3β+β5β*β(7β+β8)β*β4β=β303.
Note to the second sample test. (2β+β3)β*β5β=β25.
Note to the third sample test. (3β*β4)β*β5β=β60 (also many other variants are valid, for instance, (3)β*β4β*β5β=β60). | [
{
"input": "3+5*7+8*4",
"output": "303"
},
{
"input": "2+3*5",
"output": "25"
},
{
"input": "3*4*5",
"output": "60"
},
{
"input": "5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5",
"output": "152587890625"
},
{
"input": "2*2+2*2",
"output": "16"
},
{
"input": "1+1+1+1... | 1,000 | 9,932,800 | 0 | 16,617 | |
0 | none | [
"none"
] | null | null | The evil Bumbershoot corporation produces clones for gruesome experiments in a vast underground lab. On one occasion, the corp cloned a boy Andryusha who was smarter than his comrades. Immediately Andryusha understood that something fishy was going on there. He rallied fellow clones to go on a feud against the evil corp, and they set out to find an exit from the lab. The corp had to reduce to destroy the lab complex.
The lab can be pictured as a connected graph with *n* vertices and *m* edges. *k* clones of Andryusha start looking for an exit in some of the vertices. Each clone can traverse any edge once per second. Any number of clones are allowed to be at any vertex simultaneously. Each clone is allowed to stop looking at any time moment, but he must look at his starting vertex at least. The exit can be located at any vertex of the lab, hence each vertex must be visited by at least one clone.
Each clone can visit at most vertices before the lab explodes.
Your task is to choose starting vertices and searching routes for the clones. Each route can have at most vertices. | The first line contains three integers *n*, *m*, and *k* (1<=β€<=*n*<=β€<=2Β·105, *n*<=-<=1<=β€<=*m*<=β€<=2Β·105, 1<=β€<=*k*<=β€<=*n*)Β β the number of vertices and edges in the lab, and the number of clones.
Each of the next *m* lines contains two integers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*)Β β indices of vertices connected by the respective edge. The graph is allowed to have self-loops and multiple edges.
The graph is guaranteed to be connected. | You should print *k* lines. *i*-th of these lines must start with an integer *c**i* ()Β β the number of vertices visited by *i*-th clone, followed by *c**i* integersΒ β indices of vertices visited by this clone in the order of visiting. You have to print each vertex every time it is visited, regardless if it was visited earlier or not.
It is guaranteed that a valid answer exists. | [
"3 2 1\n2 1\n3 1\n",
"5 4 2\n1 2\n1 3\n1 4\n1 5\n"
] | [
"3 2 1 3\n",
"3 2 1 3\n3 4 1 5"
] | In the first sample case there is only one clone who may visit vertices in order (2, 1, 3), which fits the constraint of 6 vertices per clone.
In the second sample case the two clones can visited vertices in order (2, 1, 3) and (4, 1, 5), which fits the constraint of 5 vertices per clone. | [
{
"input": "3 2 1\n2 1\n3 1",
"output": "5 1 2 1 3 1"
},
{
"input": "5 4 2\n1 2\n1 3\n1 4\n1 5",
"output": "5 1 2 1 3 1\n4 4 1 5 1"
},
{
"input": "3 3 2\n1 3\n2 1\n3 2",
"output": "3 1 3 1\n2 2 1"
},
{
"input": "10 30 3\n6 5\n3 2\n7 8\n2 6\n1 9\n3 6\n6 5\n4 7\n3 2\n1 9\n7 8\n... | 62 | 614,400 | 0 | 16,632 | |
630 | Divisibility | [
"math",
"number theory"
] | null | null | IT City company developing computer games invented a new way to reward its employees. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is divisible by all numbers from 2 to 10 every developer of this game gets a small bonus.
A game designer Petya knows that the company is just about to release a new game that was partly developed by him. On the basis of his experience he predicts that *n* people will buy the game during the first month. Now Petya wants to determine how many times he will get the bonus. Help him to know it. | The only line of the input contains one integer *n* (1<=β€<=*n*<=β€<=1018) β the prediction on the number of people who will buy the game. | Output one integer showing how many numbers from 1 to *n* are divisible by all numbers from 2 to 10. | [
"3000\n"
] | [
"1"
] | none | [
{
"input": "3000",
"output": "1"
},
{
"input": "2520",
"output": "1"
},
{
"input": "2519",
"output": "0"
},
{
"input": "2521",
"output": "1"
},
{
"input": "1",
"output": "0"
},
{
"input": "314159265",
"output": "124666"
},
{
"input": "71828... | 62 | 1,228,800 | 0 | 16,633 | |
747 | Winter Is Coming | [
"dp",
"greedy",
"sortings"
] | null | null | The winter in Berland lasts *n* days. For each day we know the forecast for the average air temperature that day.
Vasya has a new set of winter tires which allows him to drive safely no more than *k* days at any average air temperature. After *k* days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these *k* days form a continuous segment of days.
Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative.
Vasya can change summer tires to winter tires and vice versa at the beginning of any day.
Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. | The first line contains two positive integers *n* and *k* (1<=β€<=*n*<=β€<=2Β·105, 0<=β€<=*k*<=β€<=*n*)Β β the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than *k* days in total.
The second line contains a sequence of *n* integers *t*1,<=*t*2,<=...,<=*t**n* (<=-<=20<=β€<=*t**i*<=β€<=20)Β β the average air temperature in the *i*-th winter day. | Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. | [
"4 3\n-5 20 -3 0\n",
"4 2\n-5 20 -3 0\n",
"10 6\n2 -5 1 3 0 0 -4 -3 1 0\n"
] | [
"2\n",
"4\n",
"3\n"
] | In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two.
In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four. | [
{
"input": "4 3\n-5 20 -3 0",
"output": "2"
},
{
"input": "4 2\n-5 20 -3 0",
"output": "4"
},
{
"input": "10 6\n2 -5 1 3 0 0 -4 -3 1 0",
"output": "3"
},
{
"input": "4 4\n-5 20 -3 0",
"output": "1"
},
{
"input": "4 1\n-5 20 -3 0",
"output": "-1"
},
{
"... | 77 | 13,107,200 | -1 | 16,678 | |
325 | Monsters and Diamonds | [
"dfs and similar",
"graphs",
"shortest paths"
] | null | null | Piegirl has found a monster and a book about monsters and pies. When she is reading the book, she found out that there are *n* types of monsters, each with an ID between 1 and *n*. If you feed a pie to a monster, the monster will split into some number of monsters (possibly zero), and at least one colorful diamond. Monsters may be able to split in multiple ways.
At the begining Piegirl has exactly one monster. She begins by feeding the monster a pie. She continues feeding pies to monsters until no more monsters are left. Then she collects all the diamonds that were created.
You will be given a list of split rules describing the way in which the various monsters can split. Every monster can split in at least one way, and if a monster can split in multiple ways then each time when it splits Piegirl can choose the way it splits.
For each monster, determine the smallest and the largest number of diamonds Piegirl can possibly collect, if initially she has a single instance of that monster. Piegirl has an unlimited supply of pies. | The first line contains two integers: *m* and *n* (1<=β€<=*m*,<=*n*<=β€<=105), the number of possible splits and the number of different monster types. Each of the following *m* lines contains a split rule. Each split rule starts with an integer (a monster ID) *m**i* (1<=β€<=*m**i*<=β€<=*n*), and a positive integer *l**i* indicating the number of monsters and diamonds the current monster can split into. This is followed by *l**i* integers, with positive integers representing a monster ID and -1 representing a diamond.
Each monster will have at least one split rule. Each split rule will have at least one diamond. The sum of *l**i* across all split rules will be at most 105. | For each monster, in order of their IDs, print a line with two integers: the smallest and the largest number of diamonds that can possibly be collected by starting with that monster. If Piegirl cannot possibly end up in a state without monsters, print -1 for both smallest and the largest value. If she can collect an arbitrarily large number of diamonds, print -2 as the largest number of diamonds.
If any number in output exceeds 314000000 (but is finite), print 314000000 instead of that number. | [
"6 4\n1 3 -1 1 -1\n1 2 -1 -1\n2 3 -1 3 -1\n2 3 -1 -1 -1\n3 2 -1 -1\n4 2 4 -1\n",
"3 2\n1 2 1 -1\n2 2 -1 -1\n2 3 2 1 -1\n"
] | [
"2 -2\n3 4\n2 2\n-1 -1\n",
"-1 -1\n2 2\n"
] | none | [
{
"input": "6 4\n1 3 -1 1 -1\n1 2 -1 -1\n2 3 -1 3 -1\n2 3 -1 -1 -1\n3 2 -1 -1\n4 2 4 -1",
"output": "2 -2\n3 4\n2 2\n-1 -1"
},
{
"input": "3 2\n1 2 1 -1\n2 2 -1 -1\n2 3 2 1 -1",
"output": "-1 -1\n2 2"
},
{
"input": "2 1\n1 3 -1 1 -1\n1 5 -1 -1 -1 -1 -1",
"output": "5 -2"
},
{
... | 92 | 0 | 0 | 16,725 | |
246 | Beauty Pageant | [
"brute force",
"constructive algorithms",
"greedy"
] | null | null | General Payne has a battalion of *n* soldiers. The soldiers' beauty contest is coming up, it will last for *k* days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants.
All soldiers in the battalion have different beauty that is represented by a positive integer. The value *a**i* represents the beauty of the *i*-th soldier.
On each of *k* days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of *k* days the beauty of the sent detachment should be unique. In other words, all *k* beauties of the sent detachments must be distinct numbers.
Help Payne choose *k* detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. | The first line contains two integers *n*, *k* (1<=β€<=*n*<=β€<=50; 1<=β€<=*k*<=β€<= ) β the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=107) β the beauties of the battalion soldiers.
It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. | Print *k* lines: in the *i*-th line print the description of the detachment that will participate in the pageant on the *i*-th day. The description consists of integer *c**i* (1<=β€<=*c**i*<=β€<=*n*) β the number of soldiers in the detachment on the *i*-th day of the pageant and *c**i* distinct integers *p*1,<=*i*,<=*p*2,<=*i*,<=...,<=*p**c**i*,<=*i* β the beauties of the soldiers in the detachment on the *i*-th day of the pageant. The beauties of the soldiers are allowed to print in any order.
Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. | [
"3 3\n1 2 3\n",
"2 1\n7 12\n"
] | [
"1 1\n1 2\n2 3 2\n",
"1 12 \n"
] | none | [
{
"input": "3 3\n1 2 3",
"output": "1 1\n1 2\n2 3 2"
},
{
"input": "2 1\n7 12",
"output": "1 12 "
},
{
"input": "1 1\n1000",
"output": "1 1000 "
},
{
"input": "5 8\n10 3 8 31 20",
"output": "1 31 \n1 20 \n1 10 \n1 8 \n1 3 \n2 31 20 \n2 31 10 \n2 31 8 "
},
{
"input... | 2,000 | 2,457,600 | 0 | 16,829 | |
95 | Horse Races | [
"dp",
"math"
] | D. Horse Races | 2 | 256 | Petya likes horse racing very much. Horses numbered from *l* to *r* take part in the races. Petya wants to evaluate the probability of victory; for some reason, to do that he needs to know the amount of nearly lucky horses' numbers. A nearly lucky number is an integer number that has at least two lucky digits the distance between which does not exceed *k*. Petya learned from some of his mates from Lviv that lucky digits are digits 4 and 7. The distance between the digits is the absolute difference between their positions in the number of a horse. For example, if *k*<==<=2, then numbers 412395497, 404, 4070400000070004007 are nearly lucky and numbers 4, 4123954997, 4007000040070004007 are not.
Petya prepared *t* intervals [*l**i*,<=*r**i*] and invented number *k*, common for all of them. Your task is to find how many nearly happy numbers there are in each of these segments. Since the answers can be quite large, output them modulo 1000000007 (109<=+<=7). | The first line contains two integers *t* and *k* (1<=β€<=*t*,<=*k*<=β€<=1000) β the number of segments and the distance between the numbers correspondingly. Next *t* lines contain pairs of integers *l**i* and *r**i* (1<=β€<=*l*<=β€<=*r*<=β€<=101000). All numbers are given without the leading zeroes. Numbers in each line are separated by exactly one space character. | Output *t* lines. In each line print one integer β the answer for the corresponding segment modulo 1000000007 (109<=+<=7). | [
"1 2\n1 100\n",
"1 2\n70 77\n",
"2 1\n1 20\n80 100\n"
] | [
"4\n",
"2\n",
"0\n0\n"
] | In the first sample, the four nearly lucky numbers are 44, 47, 74, 77.
In the second sample, only 74 and 77 are in the given segment. | [] | 30 | 0 | 0 | 16,898 |
0 | none | [
"none"
] | null | null | Consider a sequence [*a*1,<=*a*2,<=... ,<=*a**n*]. Define its prefix product sequence .
Now given *n*, find a permutation of [1,<=2,<=...,<=*n*], such that its prefix product sequence is a permutation of [0,<=1,<=...,<=*n*<=-<=1]. | The only input line contains an integer *n* (1<=β€<=*n*<=β€<=105). | In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output *n* more lines. *i*-th line contains only an integer *a**i*. The elements of the sequence should be different positive integers no larger than *n*.
If there are multiple solutions, you are allowed to print any of them. | [
"7\n",
"6\n"
] | [
"YES\n1\n4\n3\n6\n5\n2\n7\n",
"NO\n"
] | For the second sample, there are no valid sequences. | [
{
"input": "7",
"output": "YES\n1\n2\n5\n6\n3\n4\n7"
},
{
"input": "6",
"output": "NO"
},
{
"input": "7137",
"output": "NO"
},
{
"input": "10529",
"output": "YES\n1\n2\n5266\n3511\n7898\n2107\n1756\n9026\n9214\n1171\n1054\n4787\n6143\n811\n9778\n703\n9872\n8672\n586\n3326... | 0 | 0 | -1 | 16,971 | |
0 | none | [
"none"
] | null | null | Furik loves writing all sorts of problems, especially such that he can't solve himself. You've got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it.
There is integer *n* and array *a*, consisting of ten integers, indexed by numbers from 0 to 9. Your task is to count the number of positive integers with the following properties:
- the number's length does not exceed *n*; - the number doesn't have leading zeroes; - digit *i* (0<=β€<=*i*<=β€<=9) occurs in the number at least *a*[*i*] times. | The first line contains integer *n* (1<=β€<=*n*<=β€<=100). The next line contains 10 integers *a*[0], *a*[1], ..., *a*[9] (0<=β€<=*a*[*i*]<=β€<=100) β elements of array *a*. The numbers are separated by spaces. | On a single line print the remainder of dividing the answer to the problem by 1000000007 (109<=+<=7). | [
"1\n0 0 0 0 0 0 0 0 0 1\n",
"2\n1 1 0 0 0 0 0 0 0 0\n",
"3\n1 1 0 0 0 0 0 0 0 0\n"
] | [
"1\n",
"1\n",
"36\n"
] | In the first sample number 9 meets the requirements.
In the second sample number 10 meets the requirements.
In the third sample numbers 10, 110, 210, 120, 103 meet the requirements. There are other suitable numbers, 36 in total. | [] | 92 | 0 | 0 | 16,991 | |
212 | Cowboys | [
"combinatorics",
"dp",
"math"
] | null | null | A very tense moment: *n* cowboys stand in a circle and each one points his colt at a neighbor. Each cowboy can point the colt to the person who follows or precedes him in clockwise direction. Human life is worthless, just like in any real western.
The picture changes each second! Every second the cowboys analyse the situation and, if a pair of cowboys realize that they aim at each other, they turn around. In a second all such pairs of neighboring cowboys aiming at each other turn around. All actions happen instantaneously and simultaneously in a second.
We'll use character "A" to denote a cowboy who aims at his neighbour in the clockwise direction, and character "B" for a cowboy who aims at his neighbour in the counter clockwise direction. Then a string of letters "A" and "B" will denote the circle of cowboys, the record is made from the first of them in a clockwise direction.
For example, a circle that looks like "ABBBABBBA" after a second transforms into "BABBBABBA" and a circle that looks like "BABBA" transforms into "ABABB".
A second passed and now the cowboys' position is described by string *s*. Your task is to determine the number of possible states that lead to *s* in a second. Two states are considered distinct if there is a cowboy who aims at his clockwise neighbor in one state and at his counter clockwise neighbor in the other state. | The input data consists of a single string *s*. Its length is from 3 to 100 characters, inclusive. Line *s* consists of letters "A" and "B". | Print the sought number of states. | [
"BABBBABBA\n",
"ABABB\n",
"ABABAB\n"
] | [
"2\n",
"2\n",
"4\n"
] | In the first sample the possible initial states are "ABBBABBAB" and "ABBBABBBA".
In the second sample the possible initial states are "AABBB" and "BABBA". | [
{
"input": "BABBBABBA",
"output": "2"
},
{
"input": "ABABB",
"output": "2"
},
{
"input": "ABABAB",
"output": "4"
},
{
"input": "ABA",
"output": "1"
},
{
"input": "AABB",
"output": "0"
},
{
"input": "ABABBABBAABAB",
"output": "4"
},
{
"input... | 60 | 0 | 0 | 16,995 | |
596 | Wilbur and Points | [
"combinatorics",
"greedy",
"sortings"
] | null | null | Wilbur is playing with a set of *n* points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (*x*, *y*) belongs to the set, then all points (*x*', *y*'), such that 0<=β€<=*x*'<=β€<=*x* and 0<=β€<=*y*'<=β€<=*y* also belong to this set.
Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to *n*. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (*x*, *y*) gets number *i*, then all (*x*',*y*') from the set, such that *x*'<=β₯<=*x* and *y*'<=β₯<=*y* must be assigned a number not less than *i*. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4.
Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as *s*(*x*,<=*y*)<==<=*y*<=-<=*x*. Now he gives Wilbur some *w*1, *w*2,..., *w**n*, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number *i* has it's special value equal to *w**i*, that is *s*(*x**i*,<=*y**i*)<==<=*y**i*<=-<=*x**i*<==<=*w**i*.
Now Wilbur asks you to help him with this challenge. | The first line of the input consists of a single integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of points in the set Wilbur is playing with.
Next follow *n* lines with points descriptions. Each line contains two integers *x* and *y* (0<=β€<=*x*,<=*y*<=β€<=100<=000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (*x*, *y*) is present in the input, then all points (*x*', *y*'), such that 0<=β€<=*x*'<=β€<=*x* and 0<=β€<=*y*'<=β€<=*y*, are also present in the input.
The last line of the input contains *n* integers. The *i*-th of them is *w**i* (<=-<=100<=000<=β€<=*w**i*<=β€<=100<=000)Β β the required special value of the point that gets number *i* in any aesthetically pleasing numbering. | If there exists an aesthetically pleasant numbering of points in the set, such that *s*(*x**i*,<=*y**i*)<==<=*y**i*<=-<=*x**i*<==<=*w**i*, then print "YES" on the first line of the output. Otherwise, print "NO".
If a solution exists, proceed output with *n* lines. On the *i*-th of these lines print the point of the set that gets number *i*. If there are multiple solutions, print any of them. | [
"5\n2 0\n0 0\n1 0\n1 1\n0 1\n0 -1 -2 1 0\n",
"3\n1 0\n0 0\n2 0\n0 1 2\n"
] | [
"YES\n0 0\n1 0\n2 0\n0 1\n1 1\n",
"NO\n"
] | In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and *y*<sub class="lower-index">*i*</sub>β-β*x*<sub class="lower-index">*i*</sub>β=β*w*<sub class="lower-index">*i*</sub>.
In the second sample, the special values of the points in the set are 0, β-β1, and β-β2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist. | [
{
"input": "5\n2 0\n0 0\n1 0\n1 1\n0 1\n0 -1 -2 1 0",
"output": "YES\n0 0\n1 0\n2 0\n0 1\n1 1"
},
{
"input": "3\n1 0\n0 0\n2 0\n0 1 2",
"output": "NO"
},
{
"input": "9\n0 0\n1 0\n2 0\n0 1\n1 1\n2 1\n1 2\n2 2\n0 2\n0 0 0 -1 -1 -2 1 1 2",
"output": "NO"
},
{
"input": "18\n0 0\n... | 0 | 0 | -1 | 17,004 | |
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... | 1,278 | 32,153,600 | 3 | 17,007 | |
770 | Draw Brackets! | [
"*special",
"implementation"
] | null | null | A sequence of square brackets is regular if by inserting symbols "+" and "1" into it, you can get a regular mathematical expression from it. For example, sequences "[[]][]", "[]" and "[[][[]]]" β are regular, at the same time "][", "[[]" and "[[]]][" β are irregular.
Draw the given sequence using a minimalistic pseudographics in the strip of the lowest possible height β use symbols '+', '-' and '|'. For example, the sequence "[[][]][]" should be represented as:
Each bracket should be represented with the hepl of one or more symbols '|' (the vertical part) and symbols '+' and '-' as on the example which is given above.
Brackets should be drawn without spaces one by one, only dividing pairs of consecutive pairwise brackets with a single-space bar (so that the two brackets do not visually merge into one symbol). The image should have the minimum possible height.
The enclosed bracket is always smaller than the surrounding bracket, but each bracket separately strives to maximize the height of the image. So the pair of final brackets in the example above occupies the entire height of the image.
Study carefully the examples below, they adequately explain the condition of the problem. Pay attention that in this problem the answer (the image) is unique. | The first line contains an even integer *n* (2<=β€<=*n*<=β€<=100) β the length of the sequence of brackets.
The second line contains the sequence of brackets β these are *n* symbols "[" and "]". It is guaranteed that the given sequence of brackets is regular. | Print the drawn bracket sequence in the format which is given in the condition. Don't print extra (unnecessary) spaces. | [
"8\n[[][]][]\n",
"6\n[[[]]]\n",
"6\n[[][]]\n",
"2\n[]\n",
"4\n[][]\n"
] | [
"+- -++- -+\n|+- -++- -+|| |\n|| || ||| |\n|+- -++- -+|| |\n+- -++- -+\n",
"+- -+\n|+- -+|\n||+- -+||\n||| |||\n||+- -+||\n|+- -+|\n+- -+\n",
"+- -+\n|+- -++- -+|\n|| || ||\n|+- -++- -+|\n+- -+\n",
"+- -+\n| |\n+- -+\n",
"+- -++- -+\n| || |\n+- -... | none | [
{
"input": "8\n[[][]][]",
"output": "+- -++- -+\n|+- -++- -+|| |\n|| || ||| |\n|+- -++- -+|| |\n+- -++- -+"
},
{
"input": "6\n[[[]]]",
"output": "+- -+\n|+- -+|\n||+- -+||\n||| |||\n||+- -+||\n|+- -+|\n+- -+"
},
{
"input": "6\n[[][]]",
"output": ... | 77 | 7,475,200 | 0 | 17,095 | |
39 | What Has Dirichlet Got to Do with That? | [
"dp",
"games"
] | E. What Has Dirichlet Got to Do with That? | 2 | 64 | You all know the Dirichlet principle, the point of which is that if *n* boxes have no less than *n*<=+<=1 items, that leads to the existence of a box in which there are at least two items.
Having heard of that principle, but having not mastered the technique of logical thinking, 8 year olds Stas and Masha invented a game. There are *a* different boxes and *b* different items, and each turn a player can either add a new box or a new item. The player, after whose turn the number of ways of putting *b* items into *a* boxes becomes no less then a certain given number *n*, loses. All the boxes and items are considered to be different. Boxes may remain empty.
Who loses if both players play optimally and Stas's turn is first? | The only input line has three integers *a*,<=*b*,<=*n* (1<=β€<=*a*<=β€<=10000, 1<=β€<=*b*<=β€<=30, 2<=β€<=*n*<=β€<=109) β the initial number of the boxes, the number of the items and the number which constrains the number of ways, respectively. Guaranteed that the initial number of ways is strictly less than *n*. | Output "Stas" if Masha wins. Output "Masha" if Stas wins. In case of a draw, output "Missing". | [
"2 2 10\n",
"5 5 16808\n",
"3 1 4\n",
"1 4 10\n"
] | [
"Masha\n",
"Masha\n",
"Stas\n",
"Missing\n"
] | In the second example the initial number of ways is equal to 3125.
- If Stas increases the number of boxes, he will lose, as Masha may increase the number of boxes once more during her turn. After that any Stas's move will lead to defeat. - But if Stas increases the number of items, then any Masha's move will be losing. | [
{
"input": "2 2 10",
"output": "Masha"
},
{
"input": "5 5 16808",
"output": "Masha"
},
{
"input": "3 1 4",
"output": "Stas"
},
{
"input": "1 4 10",
"output": "Missing"
},
{
"input": "1 1 2",
"output": "Missing"
},
{
"input": "1 1 3",
"output": "Mas... | 156 | 0 | 0 | 17,117 |
508 | Anya and Ghosts | [
"constructive algorithms",
"greedy"
] | null | null | Anya loves to watch horror movies. In the best traditions of horror, she will be visited by *m* ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly *t* seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly *t* seconds and then goes out and can no longer be used.
For each of the *m* ghosts Anya knows the time at which it comes: the *i*-th visit will happen *w**i* seconds after midnight, all *w**i*'s are distinct. Each visit lasts exactly one second.
What is the minimum number of candles Anya should use so that during each visit, at least *r* candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time. | The first line contains three integers *m*, *t*, *r* (1<=β€<=*m*,<=*t*,<=*r*<=β€<=300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.
The next line contains *m* space-separated numbers *w**i* (1<=β€<=*i*<=β€<=*m*, 1<=β€<=*w**i*<=β€<=300), the *i*-th of them repesents at what second after the midnight the *i*-th ghost will come. All *w**i*'s are distinct, they follow in the strictly increasing order. | If it is possible to make at least *r* candles burn during each visit, then print the minimum number of candles that Anya needs to light for that.
If that is impossible, print <=-<=1. | [
"1 8 3\n10\n",
"2 10 1\n5 8\n",
"1 1 3\n10\n"
] | [
"3\n",
"1\n",
"-1\n"
] | Anya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.
It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.
In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight.
In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.
In the third sample test the answer is β-β1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes. | [
{
"input": "1 8 3\n10",
"output": "3"
},
{
"input": "2 10 1\n5 8",
"output": "1"
},
{
"input": "1 1 3\n10",
"output": "-1"
},
{
"input": "21 79 1\n13 42 51 60 69 77 94 103 144 189 196 203 210 215 217 222 224 234 240 260 282",
"output": "4"
},
{
"input": "125 92 2\... | 46 | 0 | 3 | 17,126 | |
811 | Vladik and Entertaining Flags | [
"data structures",
"dsu",
"graphs"
] | null | null | In his spare time Vladik estimates beauty of the flags.
Every flag could be represented as the matrix *n*<=Γ<=*m* which consists of positive integers.
Let's define the beauty of the flag as number of components in its matrix. We call component a set of cells with same numbers and between any pair of cells from that set there exists a path through adjacent cells from same component. Here is the example of the partitioning some flag matrix into components:
But this time he decided to change something in the process. Now he wants to estimate not the entire flag, but some segment. Segment of flag can be described as a submatrix of the flag matrix with opposite corners at (1,<=*l*) and (*n*,<=*r*), where conditions 1<=β€<=*l*<=β€<=*r*<=β€<=*m* are satisfied.
Help Vladik to calculate the beauty for some segments of the given flag. | First line contains three space-separated integers *n*, *m*, *q* (1<=β€<=*n*<=β€<=10, 1<=β€<=*m*,<=*q*<=β€<=105)Β β dimensions of flag matrix and number of segments respectively.
Each of next *n* lines contains *m* space-separated integersΒ β description of flag matrix. All elements of flag matrix is positive integers not exceeding 106.
Each of next *q* lines contains two space-separated integers *l*, *r* (1<=β€<=*l*<=β€<=*r*<=β€<=*m*)Β β borders of segment which beauty Vladik wants to know. | For each segment print the result on the corresponding line. | [
"4 5 4\n1 1 1 1 1\n1 2 2 3 3\n1 1 1 2 5\n4 4 5 5 5\n1 5\n2 5\n1 2\n4 5\n"
] | [
"6\n7\n3\n4\n"
] | Partitioning on components for every segment from first test case:
<img class="tex-graphics" src="https://espresso.codeforces.com/5c89ff7036ddb39d2997c8f594d4a0729e524ab0.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "4 5 4\n1 1 1 1 1\n1 2 2 3 3\n1 1 1 2 5\n4 4 5 5 5\n1 5\n2 5\n1 2\n4 5",
"output": "6\n7\n3\n4"
},
{
"input": "5 2 9\n6 1\n6 6\n6 6\n6 6\n5 6\n1 2\n1 1\n1 2\n1 2\n1 2\n1 1\n1 1\n1 2\n1 1",
"output": "3\n2\n3\n3\n3\n2\n2\n3\n2"
},
{
"input": "5 4 10\n5 5 5 5\n5 5 5 5\n5 5 5 5\n... | 61 | 0 | 0 | 17,162 | |
272 | Dima and Sequence | [
"implementation",
"math"
] | null | null | Dima got into number sequences. Now he's got sequence *a*1,<=*a*2,<=...,<=*a**n*, consisting of *n* positive integers. Also, Dima has got a function *f*(*x*), which can be defined with the following recurrence:
- *f*(0)<==<=0; - *f*(2Β·*x*)<==<=*f*(*x*); - *f*(2Β·*x*<=+<=1)<==<=*f*(*x*)<=+<=1.
Dima wonders, how many pairs of indexes (*i*,<=*j*) (1<=β€<=*i*<=<<=*j*<=β€<=*n*) are there, such that *f*(*a**i*)<==<=*f*(*a**j*). Help him, count the number of such pairs. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109).
The numbers in the lines are separated by single spaces. | In a single line print the answer to the problem.
Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3\n1 2 4\n",
"3\n5 3 1\n"
] | [
"3\n",
"1\n"
] | In the first sample any pair (*i*,β*j*) will do, so the answer is 3.
In the second sample only pair (1,β2) will do. | [
{
"input": "3\n1 2 4",
"output": "3"
},
{
"input": "3\n5 3 1",
"output": "1"
},
{
"input": "2\n469264357 996569493",
"output": "0"
},
{
"input": "6\n396640239 62005863 473635171 329666981 510631133 207643327",
"output": "2"
},
{
"input": "8\n851991424 32517099 310... | 2,000 | 6,553,600 | 0 | 17,190 | |
599 | Spongebob and Joke | [
"implementation"
] | null | null | While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence *a*1,<=*a*2,<=...,<=*a**m* of length *m*, consisting of integers from 1 to *n*, not necessarily distinct. Then he picked some sequence *f*1,<=*f*2,<=...,<=*f**n* of length *n* and for each number *a**i* got number *b**i*<==<=*f**a**i*. To finish the prank he erased the initial sequence *a**i*.
It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100<=000)Β β the lengths of sequences *f**i* and *b**i* respectively.
The second line contains *n* integers, determining sequence *f*1,<=*f*2,<=...,<=*f**n* (1<=β€<=*f**i*<=β€<=*n*).
The last line contains *m* integers, determining sequence *b*1,<=*b*2,<=...,<=*b**m* (1<=β€<=*b**i*<=β€<=*n*). | Print "Possible" if there is exactly one sequence *a**i*, such that *b**i*<==<=*f**a**i* for all *i* from 1 to *m*. Then print *m* integers *a*1,<=*a*2,<=...,<=*a**m*.
If there are multiple suitable sequences *a**i*, print "Ambiguity".
If Spongebob has made a mistake in his calculations and no suitable sequence *a**i* exists, print "Impossible". | [
"3 3\n3 2 1\n1 2 3\n",
"3 3\n1 1 1\n1 1 1\n",
"3 3\n1 2 1\n3 3 3\n"
] | [
"Possible\n3 2 1 \n",
"Ambiguity\n",
"Impossible\n"
] | In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique.
In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.
In the third sample *f*<sub class="lower-index">*i*</sub>ββ β3 for all *i*, so no sequence *a*<sub class="lower-index">*i*</sub> transforms into such *b*<sub class="lower-index">*i*</sub> and we can say for sure that Spongebob has made a mistake. | [
{
"input": "3 3\n3 2 1\n1 2 3",
"output": "Possible\n3 2 1 "
},
{
"input": "3 3\n1 1 1\n1 1 1",
"output": "Ambiguity"
},
{
"input": "3 3\n1 2 1\n3 3 3",
"output": "Impossible"
},
{
"input": "2 100\n2 1\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1... | 217 | 2,150,400 | -1 | 17,194 | |
0 | none | [
"none"
] | null | null | Jeff got 2*n* real numbers *a*1,<=*a*2,<=...,<=*a*2*n* as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes *n* operations, each of them goes as follows:
- choose indexes *i* and *j* (*i*<=β <=*j*) that haven't been chosen yet; - round element *a**i* to the nearest integer that isn't more than *a**i* (assign to *a**i*: β *a**i*Β β); - round element *a**j* to the nearest integer that isn't less than *a**j* (assign to *a**j*: β *a**j*Β β).
Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2000). The next line contains 2*n* real numbers *a*1, *a*2, ..., *a*2*n* (0<=β€<=*a**i*<=β€<=10000), given with exactly three digits after the decimal point. The numbers are separated by spaces. | In a single line print a single real number β the required difference with exactly three digits after the decimal point. | [
"3\n0.000 0.500 0.750 1.000 2.000 3.000\n",
"3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896\n"
] | [
"0.250\n",
"0.279\n"
] | In the first test case you need to perform the operations as follows: (*i*β=β1,β*j*β=β4), (*i*β=β2,β*j*β=β3), (*i*β=β5,β*j*β=β6). In this case, the difference will equal |(0β+β0.5β+β0.75β+β1β+β2β+β3)β-β(0β+β0β+β1β+β1β+β2β+β3)|β=β0.25. | [
{
"input": "3\n0.000 0.500 0.750 1.000 2.000 3.000",
"output": "0.250"
},
{
"input": "3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896",
"output": "0.279"
},
{
"input": "3\n673.674 9263.142 6780.000 9801.000 4640.000 8244.000",
"output": "0.184"
},
{
"input": "3\n6470.... | 154 | 2,150,400 | 3 | 17,207 | |
409 | The Great Game | [
"*special"
] | null | null | Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. | The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. | Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. | [
"[]()[]8<\n8<[]()8<\n",
"8<8<()\n[]8<[]\n"
] | [
"TEAM 2 WINS\n",
"TIE\n"
] | none | [
{
"input": "[]()[]8<\n8<[]()8<",
"output": "TEAM 2 WINS"
},
{
"input": "8<8<()\n[]8<[]",
"output": "TIE"
},
{
"input": "()\n[]",
"output": "TEAM 2 WINS"
},
{
"input": "()\n8<",
"output": "TEAM 1 WINS"
},
{
"input": "8<\n[]",
"output": "TEAM 1 WINS"
},
{
... | 77 | 0 | 3 | 17,215 | |
413 | Data Recovery | [
"implementation"
] | null | null | Not so long ago company R2 bought company R1 and consequently, all its developments in the field of multicore processors. Now the R2 laboratory is testing one of the R1 processors.
The testing goes in *n* steps, at each step the processor gets some instructions, and then its temperature is measured. The head engineer in R2 is keeping a report record on the work of the processor: he writes down the minimum and the maximum measured temperature in his notebook. His assistant had to write down all temperatures into his notebook, but (for unknown reasons) he recorded only *m*.
The next day, the engineer's assistant filed in a report with all the *m* temperatures. However, the chief engineer doubts that the assistant wrote down everything correctly (naturally, the chief engineer doesn't doubt his notes). So he asked you to help him. Given numbers *n*, *m*, *min*, *max* and the list of *m* temperatures determine whether you can upgrade the set of *m* temperatures to the set of *n* temperatures (that is add *n*<=-<=*m* temperatures), so that the minimum temperature was *min* and the maximum one was *max*. | The first line contains four integers *n*,<=*m*,<=*min*,<=*max* (1<=β€<=*m*<=<<=*n*<=β€<=100;Β 1<=β€<=*min*<=<<=*max*<=β€<=100). The second line contains *m* space-separated integers *t**i* (1<=β€<=*t**i*<=β€<=100) β the temperatures reported by the assistant.
Note, that the reported temperatures, and the temperatures you want to add can contain equal temperatures. | If the data is consistent, print 'Correct' (without the quotes). Otherwise, print 'Incorrect' (without the quotes). | [
"2 1 1 2\n1\n",
"3 1 1 3\n2\n",
"2 1 1 3\n2\n"
] | [
"Correct\n",
"Correct\n",
"Incorrect\n"
] | In the first test sample one of the possible initial configurations of temperatures is [1, 2].
In the second test sample one of the possible initial configurations of temperatures is [2, 1, 3].
In the third test sample it is impossible to add one temperature to obtain the minimum equal to 1 and the maximum equal to 3. | [
{
"input": "2 1 1 2\n1",
"output": "Correct"
},
{
"input": "3 1 1 3\n2",
"output": "Correct"
},
{
"input": "2 1 1 3\n2",
"output": "Incorrect"
},
{
"input": "3 1 1 5\n3",
"output": "Correct"
},
{
"input": "3 2 1 5\n1 5",
"output": "Correct"
},
{
"input... | 62 | 0 | 0 | 17,221 | |
117 | Tree or not Tree | [
"data structures",
"divide and conquer",
"implementation",
"trees"
] | null | null | You are given an undirected connected graph *G* consisting of *n* vertexes and *n* edges. *G* contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off.
You are also given *m* queries represented as (*v*,<=*u*) β change the state of all edges on the shortest path from vertex *v* to vertex *u* in graph *G*. If there are several such paths, the lexicographically minimal one is chosen. More formally, let us consider all shortest paths from vertex *v* to vertex *u* as the sequences of vertexes *v*,<=*v*1,<=*v*2,<=...,<=*u*. Among such sequences we choose the lexicographically minimal one.
After each query you should tell how many connected components has the graph whose vertexes coincide with the vertexes of graph *G* and edges coincide with the switched on edges of graph *G*. | The first line contains two integers *n* and *m* (3<=β€<=*n*<=β€<=105, 1<=β€<=*m*<=β€<=105). Then *n* lines describe the graph edges as *a* *b* (1<=β€<=*a*,<=*b*<=β€<=*n*). Next *m* lines contain the queries as *v* *u* (1<=β€<=*v*,<=*u*<=β€<=*n*).
It is guaranteed that the graph is connected, does not have any self-loops or multiple edges. | Print *m* lines, each containing one integer β the query results. | [
"5 2\n2 1\n4 3\n2 4\n2 5\n4 1\n5 4\n1 5\n",
"6 2\n4 6\n4 3\n1 2\n6 5\n1 5\n1 4\n2 5\n2 6\n"
] | [
"3\n3\n",
"4\n3\n"
] | Let's consider the first sample. We'll highlight the switched on edges blue on the image.
- The graph before applying any operations. No graph edges are switched on, that's why there initially are 5 connected components. <center> <img class="tex-graphics" src="https://espresso.codeforces.com/2c7aa638136542ed6824b69dab748a209dff230e.png" style="max-width: 100.0%;max-height: 100.0%;" width="189px"/> </center>- The graph after query *v*β=β5,β*u*β=β4. We can see that the graph has three components if we only consider the switched on edges. <center> <img class="tex-graphics" src="https://espresso.codeforces.com/82154b187d99628a1ca850f392ca593733308b5f.png" style="max-width: 100.0%;max-height: 100.0%;" width="189px"/> </center>- The graph after query *v*β=β1,β*u*β=β5. We can see that the graph has three components if we only consider the switched on edges. <center> <img class="tex-graphics" src="https://espresso.codeforces.com/e763d608f728113f2041c6be20cfe56b34f8df63.png" style="max-width: 100.0%;max-height: 100.0%;" width="189px"/> </center>
Lexicographical comparison of two sequences of equal length of *k* numbers should be done as follows. Sequence *x* is lexicographically less than sequence *y* if exists such *i* (1ββ€β*i*ββ€β*k*), so that *x*<sub class="lower-index">*i*</sub>β<β*y*<sub class="lower-index">*i*</sub>, and for any *j* (1ββ€β*j*β<β*i*) *x*<sub class="lower-index">*j*</sub>β=β*y*<sub class="lower-index">*j*</sub>. | [] | 92 | 0 | 0 | 17,264 | |
463 | Caisa and Sugar | [
"brute force",
"implementation"
] | null | null | Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town.
Unfortunately, he has just *s* dollars for sugar. But that's not a reason to be sad, because there are *n* types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar).
Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change. | The first line contains two space-separated integers *n*,<=*s* (1<=β€<=*n*,<=*s*<=β€<=100).
The *i*-th of the next *n* lines contains two integers *x**i*, *y**i* (1<=β€<=*x**i*<=β€<=100;Β 0<=β€<=*y**i*<=<<=100), where *x**i* represents the number of dollars and *y**i* the number of cents needed in order to buy the *i*-th type of sugar. | Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar. | [
"5 10\n3 90\n12 0\n9 70\n5 50\n7 0\n",
"5 5\n10 10\n20 20\n30 30\n40 40\n50 50\n"
] | [
"50\n",
"-1\n"
] | In the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change. | [
{
"input": "5 10\n3 90\n12 0\n9 70\n5 50\n7 0",
"output": "50"
},
{
"input": "5 5\n10 10\n20 20\n30 30\n40 40\n50 50",
"output": "-1"
},
{
"input": "1 2\n1 0",
"output": "0"
},
{
"input": "2 10\n20 99\n30 99",
"output": "-1"
},
{
"input": "15 21\n16 51\n33 44\n32 ... | 62 | 0 | 3 | 17,267 | |
1,006 | Military Problem | [
"dfs and similar",
"graphs",
"trees"
] | null | null | In this problem you will have to help Berland army with organizing their command delivery system.
There are $n$ officers in Berland army. The first officer is the commander of the army, and he does not have any superiors. Every other officer has exactly one direct superior. If officer $a$ is the direct superior of officer $b$, then we also can say that officer $b$ is a direct subordinate of officer $a$.
Officer $x$ is considered to be a subordinate (direct or indirect) of officer $y$ if one of the following conditions holds:
- officer $y$ is the direct superior of officer $x$; - the direct superior of officer $x$ is a subordinate of officer $y$.
For example, on the picture below the subordinates of the officer $3$ are: $5, 6, 7, 8, 9$.
The structure of Berland army is organized in such a way that every officer, except for the commander, is a subordinate of the commander of the army.
Formally, let's represent Berland army as a tree consisting of $n$ vertices, in which vertex $u$ corresponds to officer $u$. The parent of vertex $u$ corresponds to the direct superior of officer $u$. The root (which has index $1$) corresponds to the commander of the army.
Berland War Ministry has ordered you to give answers on $q$ queries, the $i$-th query is given as $(u_i, k_i)$, where $u_i$ is some officer, and $k_i$ is a positive integer.
To process the $i$-th query imagine how a command from $u_i$ spreads to the subordinates of $u_i$. Typical DFS (depth first search) algorithm is used here.
Suppose the current officer is $a$ and he spreads a command. Officer $a$ chooses $b$ β one of his direct subordinates (i.e. a child in the tree) who has not received this command yet. If there are many such direct subordinates, then $a$ chooses the one having minimal index. Officer $a$ gives a command to officer $b$. Afterwards, $b$ uses exactly the same algorithm to spread the command to its subtree. After $b$ finishes spreading the command, officer $a$ chooses the next direct subordinate again (using the same strategy). When officer $a$ cannot choose any direct subordinate who still hasn't received this command, officer $a$ finishes spreading the command.
Let's look at the following example:
If officer $1$ spreads a command, officers receive it in the following order: $[1, 2, 3, 5 ,6, 8, 7, 9, 4]$.
If officer $3$ spreads a command, officers receive it in the following order: $[3, 5, 6, 8, 7, 9]$.
If officer $7$ spreads a command, officers receive it in the following order: $[7, 9]$.
If officer $9$ spreads a command, officers receive it in the following order: $[9]$.
To answer the $i$-th query $(u_i, k_i)$, construct a sequence which describes the order in which officers will receive the command if the $u_i$-th officer spreads it. Return the $k_i$-th element of the constructed list or -1 if there are fewer than $k_i$ elements in it.
You should process queries independently. A query doesn't affect the following queries. | The first line of the input contains two integers $n$ and $q$ ($2 \le n \le 2 \cdot 10^5, 1 \le q \le 2 \cdot 10^5$) β the number of officers in Berland army and the number of queries.
The second line of the input contains $n - 1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$), where $p_i$ is the index of the direct superior of the officer having the index $i$. The commander has index $1$ and doesn't have any superiors.
The next $q$ lines describe the queries. The $i$-th query is given as a pair ($u_i, k_i$) ($1 \le u_i, k_i \le n$), where $u_i$ is the index of the officer which starts spreading a command, and $k_i$ is the index of the required officer in the command spreading sequence. | Print $q$ numbers, where the $i$-th number is the officer at the position $k_i$ in the list which describes the order in which officers will receive the command if it starts spreading from officer $u_i$. Print "-1" if the number of officers which receive the command is less than $k_i$.
You should process queries independently. They do not affect each other. | [
"9 6\n1 1 1 3 5 3 5 7\n3 1\n1 5\n3 4\n7 3\n1 8\n1 9\n"
] | [
"3\n6\n8\n-1\n9\n4\n"
] | none | [
{
"input": "9 6\n1 1 1 3 5 3 5 7\n3 1\n1 5\n3 4\n7 3\n1 8\n1 9",
"output": "3\n6\n8\n-1\n9\n4"
},
{
"input": "2 1\n1\n1 1",
"output": "1"
},
{
"input": "13 12\n1 1 1 1 1 1 1 1 1 1 1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n... | 670 | 32,051,200 | -1 | 17,272 | |
171 | MYSTERIOUS LANGUAGE | [
"*special"
] | null | null | You are given a mysterious language (codenamed "Secret") available in "Custom Test" tab. Find out what this language is and write a program which outputs its name. Note that the program must be written in this language. | This program has only one test, and it's empty (it doesn't give your program anything to read). | Output the name of the mysterious language. | [] | [] | none | [
{
"input": "1",
"output": "INTERCAL"
}
] | 30 | 0 | 0 | 17,322 | |
690 | Brain Network (easy) | [] | null | null | One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect β it's the opposite! Independent researchers confirmed that the nervous system of a zombie is highly complicated β it consists of *n* brains (much like a cow has several stomachs). They are interconnected by brain connectors, which are veins capable of transmitting thoughts between brains. There are two important properties such a brain network should have to function properly:
1. It should be possible to exchange thoughts between any two pairs of brains (perhaps indirectly, through other brains). 1. There should be no redundant brain connectors, that is, removing any brain connector would make property 1 false.
If both properties are satisfied, we say that the nervous system is valid. Unfortunately (?), if the system is not valid, the zombie stops thinking and becomes (even more) dead. Your task is to analyze a given nervous system of a zombie and find out whether it is valid. | The first line of the input contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000) denoting the number of brains (which are conveniently numbered from 1 to *n*) and the number of brain connectors in the nervous system, respectively. In the next *m* lines, descriptions of brain connectors follow. Every connector is given as a pair of brains *a*β*b* it connects (1<=β€<=*a*,<=*b*<=β€<=*n*, *a*<=β <=*b*). | The output consists of one line, containing either yes or no depending on whether the nervous system is valid. | [
"4 4\n1 2\n2 3\n3 1\n4 1\n",
"6 5\n1 2\n2 3\n3 4\n4 5\n3 6\n"
] | [
"no\n",
"yes\n"
] | none | [
{
"input": "4 4\n1 2\n2 3\n3 1\n4 1",
"output": "no"
},
{
"input": "6 5\n1 2\n2 3\n3 4\n4 5\n3 6",
"output": "yes"
},
{
"input": "2 1\n1 2",
"output": "yes"
},
{
"input": "3 3\n2 1\n1 3\n3 2",
"output": "no"
},
{
"input": "3 2\n1 2\n2 3",
"output": "yes"
},
... | 62 | 0 | -1 | 17,416 | |
0 | none | [
"none"
] | null | null | Vasya and Kolya play a game with a string, using the following rules. Initially, Kolya creates a string *s*, consisting of small English letters, and uniformly at random chooses an integer *k* from a segment [0,<=*len*(*s*)<=-<=1]. He tells Vasya this string *s*, and then shifts it *k* letters to the left, i.Β e. creates a new string *t*<==<=*s**k*<=+<=1*s**k*<=+<=2... *s**n**s*1*s*2... *s**k*. Vasya does not know the integer *k* nor the string *t*, but he wants to guess the integer *k*. To do this, he asks Kolya to tell him the first letter of the new string, and then, after he sees it, open one more letter on some position, which Vasya can choose.
Vasya understands, that he can't guarantee that he will win, but he wants to know the probability of winning, if he plays optimally. He wants you to compute this probability.
Note that Vasya wants to know the value of *k* uniquely, it means, that if there are at least two cyclic shifts of *s* that fit the information Vasya knowns, Vasya loses. Of course, at any moment of the game Vasya wants to maximize the probability of his win. | The only string contains the string *s* of length *l* (3<=β€<=*l*<=β€<=5000), consisting of small English letters only. | Print the only numberΒ β the answer for the problem. You answer is considered correct, if its absolute or relative error does not exceed 10<=-<=6.
Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if | [
"technocup\n",
"tictictactac\n",
"bbaabaabbb\n"
] | [
"1.000000000000000\n",
"0.333333333333333\n",
"0.100000000000000\n"
] | In the first example Vasya can always open the second letter after opening the first letter, and the cyclic shift is always determined uniquely.
In the second example if the first opened letter of *t* is "t" or "c", then Vasya can't guess the shift by opening only one other letter. On the other hand, if the first letter is "i" or "a", then he can open the fourth letter and determine the shift uniquely. | [
{
"input": "technocup",
"output": "1.000000000000000"
},
{
"input": "tictictactac",
"output": "0.333333333333333"
},
{
"input": "bbaabaabbb",
"output": "0.100000000000000"
},
{
"input": "cbbbbcaaca",
"output": "0.800000000000000"
},
{
"input": "cadbcdddda",
"o... | 1,731 | 23,142,400 | 3 | 17,457 | |
637 | Voting for Photos | [
"*special",
"constructive algorithms",
"implementation"
] | null | null | After celebrating the midcourse the students of one of the faculties of the Berland State University decided to conduct a vote for the best photo. They published the photos in the social network and agreed on the rules to choose a winner: the photo which gets most likes wins. If multiple photoes get most likes, the winner is the photo that gets this number first.
Help guys determine the winner photo by the records of likes. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=1000) β the total likes to the published photoes.
The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1<=000<=000), where *a**i* is the identifier of the photo which got the *i*-th like. | Print the identifier of the photo which won the elections. | [
"5\n1 3 2 2 1\n",
"9\n100 200 300 200 100 300 300 100 200\n"
] | [
"2\n",
"300\n"
] | In the first test sample the photo with id 1 got two likes (first and fifth), photo with id 2 got two likes (third and fourth), and photo with id 3 got one like (second).
Thus, the winner is the photo with identifier 2, as it got:
- more likes than the photo with id 3; - as many likes as the photo with id 1, but the photo with the identifier 2 got its second like earlier. | [
{
"input": "5\n1 3 2 2 1",
"output": "2"
},
{
"input": "9\n100 200 300 200 100 300 300 100 200",
"output": "300"
},
{
"input": "1\n5",
"output": "5"
},
{
"input": "1\n1000000",
"output": "1000000"
},
{
"input": "5\n1 3 4 2 2",
"output": "2"
},
{
"input... | 140 | 0 | 3 | 17,471 | |
459 | Pashmak and Buses | [
"combinatorics",
"constructive algorithms",
"math"
] | null | null | Recently Pashmak has been employed in a transportation company. The company has *k* buses and has a contract with a school which has *n* students. The school planned to take the students to *d* different places for *d* days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all *d* days.
Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. | The first line of input contains three space-separated integers *n*,<=*k*,<=*d* (1<=β€<=*n*,<=*d*<=β€<=1000;Β 1<=β€<=*k*<=β€<=109). | If there is no valid arrangement just print -1. Otherwise print *d* lines, in each of them print *n* integers. The *j*-th integer of the *i*-th line shows which bus the *j*-th student has to take on the *i*-th day. You can assume that the buses are numbered from 1 to *k*. | [
"3 2 2\n",
"3 2 1\n"
] | [
"1 1 2 \n1 2 1 \n",
"-1\n"
] | Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. | [
{
"input": "3 2 2",
"output": "1 1 2 \n1 2 1 "
},
{
"input": "3 2 1",
"output": "-1"
},
{
"input": "7 2 3",
"output": "1 1 1 1 2 2 2 \n1 1 2 2 1 1 2 \n1 2 1 2 1 2 1 "
},
{
"input": "9 2 3",
"output": "-1"
},
{
"input": "2 1 1000",
"output": "-1"
},
{
"... | 31 | 0 | 0 | 17,494 | |
61 | Eternal Victory | [
"dfs and similar",
"graphs",
"greedy",
"shortest paths",
"trees"
] | D. Eternal Victory | 2 | 256 | Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all *n* cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these *n* cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to *n*. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel. | First line contains a single natural number *n* (1<=β€<=*n*<=β€<=105) β the amount of cities.
Next *n*<=-<=1 lines contain 3 integer numbers each *x**i*, *y**i* and *w**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*,<=0<=β€<=*w**i*<=β€<=2<=Γ<=104). *x**i* and *y**i* are two ends of a road and *w**i* is the length of that road. | A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | [
"3\n1 2 3\n2 3 4\n",
"3\n1 2 3\n1 3 3\n"
] | [
"7\n",
"9\n"
] | none | [
{
"input": "3\n1 2 3\n2 3 4",
"output": "7"
},
{
"input": "3\n1 2 3\n1 3 3",
"output": "9"
},
{
"input": "5\n5 3 60\n4 3 63\n2 1 97\n3 1 14",
"output": "371"
},
{
"input": "3\n2 1 63\n3 1 78",
"output": "204"
},
{
"input": "13\n8 2 58\n2 1 49\n13 10 41\n11 9 67\n6... | 1,216 | 85,196,800 | 0 | 17,497 |
19 | Points | [
"data structures"
] | D. Points | 2 | 256 | Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coordinate system on it as follows: point (0,<=0) is located in the bottom-left corner, *Ox* axis is directed right, *Oy* axis is directed up. Pete gives Bob requests of three types:
- add x y β on the sheet of paper Bob marks a point with coordinates (*x*,<=*y*). For each request of this type it's guaranteed that point (*x*,<=*y*) is not yet marked on Bob's sheet at the time of the request. - remove x y β on the sheet of paper Bob erases the previously marked point with coordinates (*x*,<=*y*). For each request of this type it's guaranteed that point (*x*,<=*y*) is already marked on Bob's sheet at the time of the request. - find x y β on the sheet of paper Bob finds all the marked points, lying strictly above and strictly to the right of point (*x*,<=*y*). Among these points Bob chooses the leftmost one, if it is not unique, he chooses the bottommost one, and gives its coordinates to Pete.
Bob managed to answer the requests, when they were 10, 100 or 1000, but when their amount grew up to 2Β·105, Bob failed to cope. Now he needs a program that will answer all Pete's requests. Help Bob, please! | The first input line contains number *n* (1<=β€<=*n*<=β€<=2Β·105) β amount of requests. Then there follow *n* lines β descriptions of the requests. add x y describes the request to add a point, remove x y β the request to erase a point, find x y β the request to find the bottom-left point. All the coordinates in the input file are non-negative and don't exceed 109. | For each request of type find x y output in a separate line the answer to it β coordinates of the bottommost among the leftmost marked points, lying strictly above and to the right of point (*x*,<=*y*). If there are no points strictly above and to the right of point (*x*,<=*y*), output -1. | [
"7\nadd 1 1\nadd 3 4\nfind 0 0\nremove 1 1\nfind 0 0\nadd 1 1\nfind 0 0\n",
"13\nadd 5 5\nadd 5 6\nadd 5 7\nadd 6 5\nadd 6 6\nadd 6 7\nadd 7 5\nadd 7 6\nadd 7 7\nfind 6 6\nremove 7 7\nfind 6 6\nfind 4 4\n"
] | [
"1 1\n3 4\n1 1\n",
"7 7\n-1\n5 5\n"
] | none | [] | 60 | 0 | 0 | 17,531 |
1,000 | Yet Another Problem On a Subsequence | [
"combinatorics",
"dp"
] | null | null | The sequence of integers $a_1, a_2, \dots, a_k$ is called a good array if $a_1 = k - 1$ and $a_1 > 0$. For example, the sequences $[3, -1, 44, 0], [1, -99]$ are good arrays, and the sequences $[3, 7, 8], [2, 5, 4, 1], [0]$ β are not.
A sequence of integers is called good if it can be divided into a positive number of good arrays. Each good array should be a subsegment of sequence and each element of the sequence should belong to exactly one array. For example, the sequences $[2, -3, 0, 1, 4]$, $[1, 2, 3, -3, -9, 4]$ are good, and the sequences $[2, -3, 0, 1]$, $[1, 2, 3, -3 -9, 4, 1]$ β are not.
For a given sequence of numbers, count the number of its subsequences that are good sequences, and print the number of such subsequences modulo 998244353. | The first line contains the number $n~(1 \le n \le 10^3)$ β the length of the initial sequence. The following line contains $n$ integers $a_1, a_2, \dots, a_n~(-10^9 \le a_i \le 10^9)$ β the sequence itself. | In the single line output one integer β the number of subsequences of the original sequence that are good sequences, taken modulo 998244353. | [
"3\n2 1 1\n",
"4\n1 1 1 1\n"
] | [
"2\n",
"7\n"
] | In the first test case, two good subsequences β $[a_1, a_2, a_3]$ and $[a_2, a_3]$.
In the second test case, seven good subsequences β $[a_1, a_2, a_3, a_4], [a_1, a_2], [a_1, a_3], [a_1, a_4], [a_2, a_3], [a_2, a_4]$ and $[a_3, a_4]$. | [
{
"input": "3\n2 1 1",
"output": "2"
},
{
"input": "4\n1 1 1 1",
"output": "7"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "1\n1",
"output": "0"
}
] | 46 | 4,710,400 | -1 | 17,538 | |
744 | Hongcow Buys a Deck of Cards | [
"bitmasks",
"brute force",
"dp"
] | null | null | One day, Hongcow goes to the store and sees a brand new deck of *n* special cards. Each individual card is either red or blue. He decides he wants to buy them immediately. To do this, he needs to play a game with the owner of the store.
This game takes some number of turns to complete. On a turn, Hongcow may do one of two things:
- Collect tokens. Hongcow collects 1 red token and 1 blue token by choosing this option (thus, 2 tokens in total per one operation). - Buy a card. Hongcow chooses some card and spends tokens to purchase it as specified below.
The *i*-th card requires *r**i* red resources and *b**i* blue resources. Suppose Hongcow currently has *A* red cards and *B* blue cards. Then, the *i*-th card will require Hongcow to spend *max*(*r**i*<=-<=*A*,<=0) red tokens, and *max*(*b**i*<=-<=*B*,<=0) blue tokens. Note, only tokens disappear, but the cards stay with Hongcow forever. Each card can be bought only once.
Given a description of the cards and their costs determine the minimum number of turns Hongcow needs to purchase all cards. | The first line of input will contain a single integer *n* (1<=β€<=*n*<=β€<=16).
The next *n* lines of input will contain three tokens *c**i*, *r**i* and *b**i*. *c**i* will be 'R' or 'B', denoting the color of the card as red or blue. *r**i* will be an integer denoting the amount of red resources required to obtain the card, and *b**i* will be an integer denoting the amount of blue resources required to obtain the card (0<=β€<=*r**i*,<=*b**i*<=β€<=107). | Output a single integer, denoting the minimum number of turns needed to acquire all the cards. | [
"3\nR 0 1\nB 1 0\nR 1 1\n",
"3\nR 3 0\nR 2 0\nR 1 0\n"
] | [
"4\n",
"6\n"
] | For the first sample, Hongcow's four moves are as follows:
1. Collect tokens 1. Buy card 1 1. Buy card 2 1. Buy card 3
For the second sample, one optimal strategy is as follows:
1. Collect tokens 1. Collect tokens 1. Buy card 2 1. Collect tokens 1. Buy card 3 1. Buy card 1 | [
{
"input": "3\nR 0 1\nB 1 0\nR 1 1",
"output": "4"
},
{
"input": "3\nR 3 0\nR 2 0\nR 1 0",
"output": "6"
},
{
"input": "8\nB 0 1\nR 2 3\nB 2 1\nR 4 2\nB 1 3\nB 1 10\nB 3 4\nR 3 4",
"output": "17"
},
{
"input": "16\nR 10000000 10000000\nR 10000000 10000000\nR 10000000 10000000... | 31 | 0 | 0 | 17,566 | |
142 | Help Caretaker | [
"brute force",
"dp"
] | null | null | Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.
He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space):
Simon faced a quite natural challenge: placing in the given *n*<=Γ<=*m* cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.
Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him? | The only line contains two space-separated integers *n* and *m* β the sizes of the warehouse (1<=β€<=*n*,<=*m*<=β€<=9). | In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next *n* lines print *m* characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them. | [
"3 3\n",
"5 6\n",
"2 2\n"
] | [
"1\nAAA\n.A.\n.A.\n",
"4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D\n",
"0\n..\n..\n"
] | none | [
{
"input": "3 3",
"output": "1\nAAA\n.A.\n.A."
},
{
"input": "5 6",
"output": "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D"
},
{
"input": "2 2",
"output": "0\n..\n.."
},
{
"input": "4 2",
"output": "0\n..\n..\n..\n.."
},
{
"input": "3 4",
"output": "1\nA...\nAAA... | 0 | 0 | -1 | 17,570 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.