task stringlengths 0 154k | __index_level_0__ int64 0 39.2k |
|---|---|
Title: Dungeons and Candies
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
During the loading of the game "Dungeons and Candies" you are required to get descriptions of *k* levels from the server. Each description is a map of an *n*<=Γ<=*m* checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as "." on the map, but if a cell has a candy, it is denoted as a letter of the English alphabet. A level may contain identical candies, in this case the letters in the corresponding cells of the map will be the same.
When you transmit information via a network, you want to minimize traffic β the total size of the transferred data. The levels can be transmitted in any order. There are two ways to transmit the current level *A*:
1. You can transmit the whole level *A*. Then you need to transmit *n*Β·*m* bytes via the network. 1. You can transmit the difference between level *A* and some previously transmitted level *B* (if it exists); this operation requires to transmit *d**A*,<=*B*Β·*w* bytes, where *d**A*,<=*B* is the number of cells of the field that are different for *A* and *B*, and *w* is a constant. Note, that you should compare only the corresponding cells of levels *A* and *B* to calculate *d**A*,<=*B*. You cannot transform the maps of levels, i.e. rotate or shift them relatively to each other.
Your task is to find a way to transfer all the *k* levels and minimize the traffic.
Input Specification:
The first line contains four integers *n*,<=*m*,<=*k*,<=*w* (1<=β€<=*n*,<=*m*<=β€<=10;Β 1<=β€<=*k*,<=*w*<=β€<=1000). Then follows the description of *k* levels. Each level is described by *n* lines, each line contains *m* characters. Each character is either a letter of the English alphabet or a dot ("."). Please note that the case of the letters matters.
Output Specification:
In the first line print the required minimum number of transferred bytes.
Then print *k* pairs of integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=...,<=*x**k*,<=*y**k*, describing the way to transfer levels. Pair *x**i*, *y**i* means that level *x**i* needs to be transferred by way *y**i*. If *y**i* equals 0, that means that the level must be transferred using the first way, otherwise *y**i* must be equal to the number of a previously transferred level. It means that you will transfer the difference between levels *y**i* and *x**i* to transfer level *x**i*. Print the pairs in the order of transferring levels. The levels are numbered 1 through *k* in the order they follow in the input.
If there are multiple optimal solutions, you can print any of them.
Demo Input:
['2 3 3 2\nA.A\n...\nA.a\n..C\nX.Y\n...\n', '1 1 4 1\nA\n.\nB\n.\n', '1 3 5 2\nABA\nBBB\nBBA\nBAB\nABB\n']
Demo Output:
['14\n1 0\n2 1\n3 1\n', '3\n1 0\n2 0\n4 2\n3 0\n', '11\n1 0\n3 1\n2 3\n4 2\n5 1\n']
Note:
none | 600 |
Title: Bear and Paradox
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Limak is a big polar bear. He prepared *n* problems for an algorithmic contest. The *i*-th problem has initial score *p**i*. Also, testers said that it takes *t**i* minutes to solve the *i*-th problem. Problems aren't necessarily sorted by difficulty and maybe harder problems have smaller initial score but it's too late to change itΒ β Limak has already announced initial scores for problems. Though it's still possible to adjust the speed of losing points, denoted by *c* in this statement.
Let *T* denote the total number of minutes needed to solve all problems (so, *T*<==<=*t*1<=+<=*t*2<=+<=...<=+<=*t**n*). The contest will last exactly *T* minutes. So it's just enough to solve all problems.
Points given for solving a problem decrease linearly. Solving the *i*-th problem after *x* minutes gives exactly points, where is some real constant that Limak must choose.
Let's assume that *c* is fixed. During a contest a participant chooses some order in which he or she solves problems. There are *n*! possible orders and each of them gives some total number of points, not necessarily integer. We say that an order is optimal if it gives the maximum number of points. In other words, the total number of points given by this order is greater or equal than the number of points given by any other order. It's obvious that there is at least one optimal order. However, there may be more than one optimal order.
Limak assumes that every participant will properly estimate *t**i* at the very beginning and will choose some optimal order. He also assumes that testers correctly predicted time needed to solve each problem.
For two distinct problems *i* and *j* such that *p**i*<=<<=*p**j* Limak wouldn't be happy to see a participant with strictly more points for problem *i* than for problem *j*. He calls such a situation a paradox.
It's not hard to prove that there will be no paradox for *c*<==<=0. The situation may be worse for bigger *c*. What is the maximum real value *c* (remember that ) for which there is no paradox possible, that is, there will be no paradox for any optimal order of solving problems?
It can be proved that the answer (the maximum *c* as described) always exists.
Input Specification:
The first line contains one integer *n* (2<=β€<=*n*<=β€<=150<=000)Β β the number of problems.
The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=108)Β β initial scores.
The third line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=108) where *t**i* is the number of minutes needed to solve the *i*-th problem.
Output Specification:
Print one real value on a single lineΒ β the maximum value of *c* that and there is no optimal order with a paradox. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if .
Demo Input:
['3\n4 3 10\n1 1 8\n', '4\n7 20 15 10\n7 20 15 10\n', '2\n10 20\n10 1\n']
Demo Output:
['0.62500000000\n', '0.31901840491\n', '1.00000000000\n']
Note:
In the first sample, there are 3 problems. The first is (4,β1) (initial score is 4 and required time is 1 minute), the second problem is (3,β1) and the third one is (10,β8). The total time is *T*β=β1β+β1β+β8β=β10.
Let's show that there is a paradox for *c*β=β0.7. Solving problems in order 1, 2, 3 turns out to give the best total score, equal to the sum of:
1. solved 1 minute after the start: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e300b226dba2622235193889de3334147547cfcb.png" style="max-width: 100.0%;max-height: 100.0%;"/> 1. solved 2 minutes after the start: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/899352b92886f248a9c8e31247a7e4043f9f45e9.png" style="max-width: 100.0%;max-height: 100.0%;"/> 1. solved 10 minutes after the start: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ed13f9d5e6cc1ef056b90a9157397d02abab0b16.png" style="max-width: 100.0%;max-height: 100.0%;"/>
So, this order gives 3.72β+β2.58β+β3β=β9.3 points in total and this is the only optimal order (you can calculate total scores for other 5 possible orders too see that they are lower). You should check points for problems 1 and 3 to see a paradox. There is 4β<β10 but 3.72β>β3. It turns out that there is no paradox for *c*β=β0.625 but there is a paradox for any bigger *c*.
In the second sample, all 24 orders are optimal.
In the third sample, even for *c*β=β1 there is no paradox. | 601 |
Title: Soldier and Traveling
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
In the country there are *n* cities and *m* bidirectional roads between them. Each city has an army. Army of the *i*-th city consists of *a**i* soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.
Check if is it possible that after roaming there will be exactly *b**i* soldiers in the *i*-th city.
Input Specification:
First line of input consists of two integers *n* and *m* (1<=β€<=*n*<=β€<=100, 0<=β€<=*m*<=β€<=200).
Next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=100).
Next line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (0<=β€<=*b**i*<=β€<=100).
Then *m* lines follow, each of them consists of two integers *p* and *q* (1<=β€<=*p*,<=*q*<=β€<=*n*, *p*<=β <=*q*) denoting that there is an undirected road between cities *p* and *q*.
It is guaranteed that there is at most one road between each pair of cities.
Output Specification:
If the conditions can not be met output single word "NO".
Otherwise output word "YES" and then *n* lines, each of them consisting of *n* integers. Number in the *i*-th line in the *j*-th column should denote how many soldiers should road from city *i* to city *j* (if *i*<=β <=*j*) or how many soldiers should stay in city *i* (if *i*<==<=*j*).
If there are several possible answers you may output any of them.
Demo Input:
['4 4\n1 2 6 3\n3 5 3 1\n1 2\n2 3\n3 4\n4 2\n', '2 0\n1 2\n2 1\n']
Demo Output:
['YES\n1 0 0 0 \n2 0 0 0 \n0 5 1 0 \n0 0 2 1 \n', 'NO']
Note:
none | 602 |
Title: Present
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted *n* flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.
There are *m* days left to the birthday. The height of the *i*-th flower (assume that the flowers in the row are numbered from 1 to *n* from left to right) is equal to *a**i* at the moment. At each of the remaining *m* days the beaver can take a special watering and water *w* contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?
Input Specification:
The first line contains space-separated integers *n*, *m* and *w* (1<=β€<=*w*<=β€<=*n*<=β€<=105;Β 1<=β€<=*m*<=β€<=105). The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109).
Output Specification:
Print a single integer β the maximum final height of the smallest flower.
Demo Input:
['6 2 3\n2 2 2 2 1 1\n', '2 5 1\n5 8\n']
Demo Output:
['2\n', '9\n']
Note:
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test. | 603 |
Title: Directed Roads
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of *n* towns numbered from 1 to *n*.
There are *n* directed roads in the Udayland. *i*-th of them goes from town *i* to some other town *a**i* (*a**i*<=β <=*i*). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town *A* to town *B* before the flip, it will go from town *B* to town *A* after.
ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns *A*1,<=*A*2,<=...,<=*A**k* (*k*<=><=1) such that for every 1<=β€<=*i*<=<<=*k* there is a road from town *A**i* to town *A**i*<=+<=1 and another road from town *A**k* to town *A*1. In other words, the roads are confusing if some of them form a directed cycle of some towns.
Now ZS the Coder wonders how many sets of roads (there are 2*n* variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing.
Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.
Input Specification:
The first line of the input contains single integer *n* (2<=β€<=*n*<=β€<=2Β·105)Β β the number of towns in Udayland.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*,<=*a**i*<=β <=*i*), *a**i* denotes a road going from town *i* to town *a**i*.
Output Specification:
Print a single integerΒ β the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109<=+<=7.
Demo Input:
['3\n2 3 1\n', '4\n2 1 1 1\n', '5\n2 4 2 5 3\n']
Demo Output:
['6\n', '8\n', '28\n']
Note:
Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/030fc9181b578c2d906254d38dc56da5554323eb.png" style="max-width: 100.0%;max-height: 100.0%;"/> initially. Number the roads 1 to 3 in this order.
The sets of roads that ZS the Coder can flip (to make them not confusing) are {1},β{2},β{3},β{1,β2},β{1,β3},β{2,β3}. Note that the empty set is invalid because if no roads are flipped, then towns 1,β2,β3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip.
The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing.
<img class="tex-graphics" src="https://espresso.codeforces.com/2e275877797bea4817665ce9cfb5274b837194bc.png" style="max-width: 100.0%;max-height: 100.0%;"/> | 604 |
Title: Number Table
Time Limit: 2 seconds
Memory Limit: 216 megabytes
Problem Description:
As it has been found out recently, all the Berland's current economical state can be described using a simple table *n*<=Γ<=*m* in size. *n* β the number of days in each Berland month, *m* β the number of months. Thus, a table cell corresponds to a day and a month of the Berland's year. Each cell will contain either 1, or -1, which means the state's gains in a particular month, on a particular day. 1 corresponds to profits, -1 corresponds to losses. It turned out important for successful development to analyze the data on the state of the economy of the previous year, however when the treasurers referred to the archives to retrieve the data, it turned out that the table had been substantially damaged. In some table cells the number values had faded and were impossible to be deciphered. It is known that the number of cells in which the data had been preserved is strictly less than *max*(*n*,<=*m*). However, there is additional information β the product of the numbers in each line and column equaled -1. Your task is to find out how many different tables may conform to the preserved data. As the answer to the task can be quite large, you have to find it modulo *p*.
Input Specification:
The first line contains integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000). The second line contains the integer *k* (0<=β€<=*k*<=<<=*max*(*n*,<=*m*)) β the number of cells in which the data had been preserved. The next *k* lines contain the data on the state of the table in the preserved cells. Each line is of the form "*a* *b* *c*", where *a* (1<=β€<=*a*<=β€<=*n*) β the number of the table row, *b* (1<=β€<=*b*<=β€<=*m*) β the number of the column, *c* β the value containing in the cell (1 or -1). They are numbered starting from 1. It is guaranteed that no two lines with same *a* and *b* values exist. The last line contains an integer *p* (2<=β€<=*p*<=β€<=109<=+<=7).
Output Specification:
Print the number of different tables that could conform to the preserved data modulo *p*.
Demo Input:
['2 2\n0\n100\n', '2 2\n1\n1 1 -1\n100\n']
Demo Output:
['2\n', '1\n']
Note:
none | 605 |
Title: Whose sentence is it?
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
One day, liouzhou_101 got a chat record of Freda and Rainbow. Out of curiosity, he wanted to know which sentences were said by Freda, and which were said by Rainbow. According to his experience, he thought that Freda always said "lala." at the end of her sentences, while Rainbow always said "miao." at the beginning of his sentences. For each sentence in the chat record, help liouzhou_101 find whose sentence it is.
Input Specification:
The first line of the input contains an integer *n* (1<=β€<=*n*<=β€<=10), number of sentences in the chat record. Each of the next *n* lines contains a sentence. A sentence is a string that contains only Latin letters (A-Z, a-z), underline (_), comma (,), point (.) and space ( ). Its length doesnβt exceed 100.
Output Specification:
For each sentence, output "Freda's" if the sentence was said by Freda, "Rainbow's" if the sentence was said by Rainbow, or "OMG>.< I don't know!" if liouzhou_101 canβt recognize whose sentence it is. He canβt recognize a sentence if it begins with "miao." and ends with "lala.", or satisfies neither of the conditions.
Demo Input:
['5\nI will go to play with you lala.\nwow, welcome.\nmiao.lala.\nmiao.\nmiao .\n']
Demo Output:
["Freda's\nOMG>.< I don't know!\nOMG>.< I don't know!\nRainbow's\nOMG>.< I don't know!\n"]
Note:
none | 606 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other.
For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word β some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction β it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to).
Here is one exercise that you have found in your task list. You are given the word *s*. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language.
Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match.
Let's look at the example: the word *abacabaca* is given. This word can be obtained in the following ways: , where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {*aca*,<=*ba*,<=*ca*}.
Input Specification:
The only line contains a string *s* (5<=β€<=|*s*|<=β€<=104) consisting of lowercase English letters.
Output Specification:
On the first line print integer *k* β a number of distinct possible suffixes. On the next *k* lines print suffixes.
Print suffixes in lexicographical (alphabetical) order.
Demo Input:
['abacabaca\n', 'abaca\n']
Demo Output:
['3\naca\nba\nca\n', '0\n']
Note:
The first test was analysed in the problem statement.
In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix. | 607 |
Title: Sweets Game
Time Limit: 3 seconds
Memory Limit: 256 megabytes
Problem Description:
Karlsson has visited Lillebror again. They found a box of chocolates and a big whipped cream cake at Lillebror's place. Karlsson immediately suggested to divide the sweets fairly between Lillebror and himself. Specifically, to play together a game he has just invented with the chocolates. The winner will get the cake as a reward.
The box of chocolates has the form of a hexagon. It contains 19 cells for the chocolates, some of which contain a chocolate. The players move in turns. During one move it is allowed to eat one or several chocolates that lay in the neighboring cells on one line, parallel to one of the box's sides. The picture below shows the examples of allowed moves and of an unacceptable one. The player who cannot make a move loses.
Karlsson makes the first move as he is Lillebror's guest and not vice versa. The players play optimally. Determine who will get the cake.
Input Specification:
The input data contains 5 lines, containing 19 words consisting of one symbol. The word "O" means that the cell contains a chocolate and a "." stands for an empty cell. It is guaranteed that the box contains at least one chocolate. See the examples for better understanding.
Output Specification:
If Karlsson gets the cake, print "Karlsson" (without the quotes), otherwise print "Lillebror" (yet again without the quotes).
Demo Input:
['. . .\n . . O .\n. . O O .\n . . . .\n . . .\n', '. . .\n . . . O\n. . . O .\n O . O .\n . O .\n']
Demo Output:
['Lillebror', 'Karlsson']
Note:
none | 608 |
Title: Slime Combining
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Your friend recently gave you some slimes for your birthday. You have *n* slimes all initially with value 1.
You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other *n*<=-<=1 slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value *v*, you combine them together to create a slime with value *v*<=+<=1.
You would like to see what the final state of the row is after you've added all *n* slimes. Please print the values of the slimes in the row from left to right.
Input Specification:
The first line of the input will contain a single integer, *n* (1<=β€<=*n*<=β€<=100<=000).
Output Specification:
Output a single line with *k* integers, where *k* is the number of slimes in the row after you've finished the procedure described in the problem statement. The *i*-th of these numbers should be the value of the *i*-th slime from the left.
Demo Input:
['1\n', '2\n', '3\n', '8\n']
Demo Output:
['1\n', '2\n', '2 1\n', '4\n']
Note:
In the first sample, we only have a single slime with value 1. The final state of the board is just a single slime with value 1.
In the second sample, we perform the following steps:
Initially we place a single slime in a row by itself. Thus, row is initially 1.
Then, we will add another slime. The row is now 1 1. Since two rightmost slimes have the same values, we should replace these slimes with one with value 2. Thus, the final state of the board is 2.
In the third sample, after adding the first two slimes, our row is 2. After adding one more slime, the row becomes 2 1.
In the last sample, the steps look as follows:
1. 1 1. 2 1. 2 1 1. 3 1. 3 1 1. 3 2 1. 3 2 1 1. 4 | 609 |
Title: Okabe and Future Gadget Laboratory
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an *n* by *n* square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every *x*,<=*y* such that 1<=β€<=*x*,<=*y*<=β€<=*n* and *a**x*,<=*y*<=β <=1, there should exist two indices *s* and *t* so that *a**x*,<=*y*<==<=*a**x*,<=*s*<=+<=*a**t*,<=*y*, where *a**i*,<=*j* denotes the integer in *i*-th row and *j*-th column.
Help Okabe determine whether a given lab is good!
Input Specification:
The first line of input contains the integer *n* (1<=β€<=*n*<=β€<=50)Β β the size of the lab.
The next *n* lines contain *n* space-separated integers denoting a row of the grid. The *j*-th integer in the *i*-th row is *a**i*,<=*j* (1<=β€<=*a**i*,<=*j*<=β€<=105).
Output Specification:
Print "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case.
Demo Input:
['3\n1 1 2\n2 3 1\n6 4 1\n', '3\n1 5 2\n1 1 1\n1 2 3\n']
Demo Output:
['Yes\n', 'No\n']
Note:
In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".
In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No". | 610 |
Title: Pair Of Lines
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given *n* points on Cartesian plane. Every point is a lattice point (i.<=e. both of its coordinates are integers), and all points are distinct.
You may draw two straight lines (not necessarily distinct). Is it possible to do this in such a way that every point lies on at least one of these lines?
Input Specification:
The first line contains one integer *n* (1<=β€<=*n*<=β€<=105) β the number of points you are given.
Then *n* lines follow, each line containing two integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=β€<=109)β coordinates of *i*-th point. All *n* points are distinct.
Output Specification:
If it is possible to draw two straight lines in such a way that each of given points belongs to at least one of these lines, print YES. Otherwise, print NO.
Demo Input:
['5\n0 0\n0 1\n1 1\n1 -1\n2 2\n', '5\n0 0\n1 0\n2 1\n1 1\n2 3\n']
Demo Output:
['YES\n', 'NO\n']
Note:
In the first example it is possible to draw two lines, the one containing the points 1, 3 and 5, and another one containing two remaining points. | 611 |
Title: Fafa and Ancient Alphabet
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
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*.
Input Specification:
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.
Output Specification:
Print the value , where *P* and *Q* are coprime and is the answer to the problem.
Demo Input:
['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']
Demo Output:
['500000004\n', '0\n', '230769233\n']
Note:
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. | 612 |
Title: April Fools' Problem (medium)
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
The marmots need to prepare *k* problems for HC2 over *n* days. Each problem, once prepared, also has to be printed.
The preparation of a problem on day *i* (at most one per day) costs *a**i* CHF, and the printing of a problem on day *i* (also at most one per day) costs *b**i* CHF. Of course, a problem cannot be printed before it has been prepared (but doing both on the same day is fine).
What is the minimum cost of preparation and printing?
Input Specification:
The first line of input contains two space-separated integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=2200). The second line contains *n* space-separated integers *a*1,<=...,<=*a**n* () β the preparation costs. The third line contains *n* space-separated integers *b*1,<=...,<=*b**n* () β the printing costs.
Output Specification:
Output the minimum cost of preparation and printing *k* problems β that is, the minimum possible sum *a**i*1<=+<=*a**i*2<=+<=...<=+<=*a**i**k*<=+<=*b**j*1<=+<=*b**j*2<=+<=...<=+<=*b**j**k*, where 1<=β€<=*i*1<=<<=*i*2<=<<=...<=<<=*i**k*<=β€<=*n*, 1<=β€<=*j*1<=<<=*j*2<=<<=...<=<<=*j**k*<=β€<=*n* and *i*1<=β€<=*j*1, *i*2<=β€<=*j*2, ..., *i**k*<=β€<=*j**k*.
Demo Input:
['8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 1\n']
Demo Output:
['32']
Note:
In the sample testcase, one optimum solution is to prepare the first problem on day 1 and print it on day 1, prepare the second problem on day 2 and print it on day 4, prepare the third problem on day 3 and print it on day 5, and prepare the fourth problem on day 6 and print it on day 8. | 613 |
Title: Birds
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Apart from plush toys, Imp is a huge fan of little yellow birds!
To summon birds, Imp needs strong magic. There are *n* trees in a row on an alley in a park, there is a nest on each of the trees. In the *i*-th nest there are *c**i* birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him *cost**i* points of mana. However, for each bird summoned, Imp increases his mana capacity by *B* points. Imp summons birds one by one, he can summon any number from 0 to *c**i* birds from the *i*-th nest.
Initially Imp stands under the first tree and has *W* points of mana, and his mana capacity equals *W* as well. He can only go forward, and each time he moves from a tree to the next one, he restores *X* points of mana (but it can't exceed his current mana capacity). Moving only forward, what is the maximum number of birds Imp can summon?
Input Specification:
The first line contains four integers *n*, *W*, *B*, *X* (1<=β€<=*n*<=β€<=103,<=0<=β€<=*W*,<=*B*,<=*X*<=β€<=109)Β β the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one.
The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (0<=β€<=*c**i*<=β€<=104)Β β where *c**i* is the number of birds living in the *i*-th nest. It is guaranteed that .
The third line contains *n* integers *cost*1,<=*cost*2,<=...,<=*cost**n* (0<=β€<=*cost**i*<=β€<=109), where *cost**i* is the mana cost to summon a bird from the *i*-th nest.
Output Specification:
Print a single integerΒ β the maximum number of birds Imp can summon.
Demo Input:
['2 12 0 4\n3 4\n4 2\n', '4 1000 10 35\n1 2 4 5\n1000 500 250 200\n', '2 10 7 11\n2 10\n6 1\n']
Demo Output:
['6\n', '5\n', '11\n']
Note:
In the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since *B*β=β0). After this step his mana will be 4 of 12; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it's optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be β 6.
In the second sample the base amount of mana is equal to 1000. The right choice will be to simply pick all birds from the last nest. Note that Imp's mana doesn't restore while moving because it's initially full. | 614 |
Title: Limak and Shooting Points
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Bearland is a dangerous place. Limak canβt travel on foot. Instead, he has *k* magic teleportation stones. Each stone can be used at most once. The *i*-th stone allows to teleport to a point (*ax**i*,<=*ay**i*). Limak can use stones in any order.
There are *n* monsters in Bearland. The *i*-th of them stands at (*mx**i*,<=*my**i*).
The given *k*<=+<=*n* points are pairwise distinct.
After each teleportation, Limak can shoot an arrow in some direction. An arrow will hit the first monster in the chosen direction. Then, both an arrow and a monster disappear. Itβs dangerous to stay in one place for long, so Limak can shoot only one arrow from one place.
A monster should be afraid if itβs possible that Limak will hit it. How many monsters should be afraid of Limak?
Input Specification:
The first line of the input contains two integers *k* and *n* (1<=β€<=*k*<=β€<=7, 1<=β€<=*n*<=β€<=1000)Β β the number of stones and the number of monsters.
The *i*-th of following *k* lines contains two integers *ax**i* and *ay**i* (<=-<=109<=β€<=*ax**i*,<=*ay**i*<=β€<=109)Β β coordinates to which Limak can teleport using the *i*-th stone.
The *i*-th of last *n* lines contains two integers *mx**i* and *my**i* (<=-<=109<=β€<=*mx**i*,<=*my**i*<=β€<=109)Β β coordinates of the *i*-th monster.
The given *k*<=+<=*n* points are pairwise distinct.
Output Specification:
Print the number of monsters which should be afraid of Limak.
Demo Input:
['2 4\n-2 -1\n4 5\n4 2\n2 1\n4 -1\n1 -1\n', '3 8\n10 20\n0 0\n20 40\n300 600\n30 60\n170 340\n50 100\n28 56\n90 180\n-4 -8\n-1 -2\n']
Demo Output:
['3\n', '5\n']
Note:
In the first sample, there are two stones and four monsters. Stones allow to teleport to points (β-β2,ββ-β1) and (4,β5), marked blue in the drawing below. Monsters are at (4,β2), (2,β1), (4,ββ-β1) and (1,ββ-β1), marked red. A monster at (4,ββ-β1) shouldn't be afraid because it's impossible that Limak will hit it with an arrow. Other three monsters can be hit and thus the answer is 3.
In the second sample, five monsters should be afraid. Safe monsters are those at (300,β600), (170,β340) and (90,β180). | 615 |
Title: Mr. Kitayuta's Technology
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Shuseki Kingdom is the world's leading nation for innovation and technology. There are *n* cities in the kingdom, numbered from 1 to *n*.
Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city *x* to city *y* cannot be used to travel from city *y* to city *x*. The transportation within each city is extremely developed, therefore if a pipe from city *x* to city *y* and a pipe from city *y* to city *z* are both constructed, people will be able to travel from city *x* to city *z* instantly.
Mr. Kitayuta is also involved in national politics. He considers that the transportation between the *m* pairs of city (*a**i*,<=*b**i*) (1<=β€<=*i*<=β€<=*m*) is important. He is planning to construct teleportation pipes so that for each important pair (*a**i*,<=*b**i*), it will be possible to travel from city *a**i* to city *b**i* by using one or more teleportation pipes (but not necessarily from city *b**i* to city *a**i*). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities.
Input Specification:
The first line contains two space-separated integers *n* and *m* (2<=β€<=*n*<=β€<=105,<=1<=β€<=*m*<=β€<=105), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively.
The following *m* lines describe the important pairs. The *i*-th of them (1<=β€<=*i*<=β€<=*m*) contains two space-separated integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*), denoting that it must be possible to travel from city *a**i* to city *b**i* by using one or more teleportation pipes (but not necessarily from city *b**i* to city *a**i*). It is guaranteed that all pairs (*a**i*,<=*b**i*) are distinct.
Output Specification:
Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose.
Demo Input:
['4 5\n1 2\n1 3\n1 4\n2 3\n2 4\n', '4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4\n']
Demo Output:
['3\n', '4\n']
Note:
For the first sample, one of the optimal ways to construct pipes is shown in the image below:
For the second sample, one of the optimal ways is shown below: | 616 |
Title: Painting Eggs
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have *n* eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got *n* eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
Input Specification:
The first line contains integer *n* (1<=β€<=*n*<=β€<=106) β the number of eggs.
Next *n* lines contain two integers *a**i* and *g**i* each (0<=β€<=*a**i*,<=*g**i*<=β€<=1000;Β *a**i*<=+<=*g**i*<==<=1000): *a**i* is the price said by A. for the *i*-th egg and *g**i* is the price said by G. for the *i*-th egg.
Output Specification:
If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of *n* letters "G" and "A". The *i*-th letter of this string should represent the child who will get the *i*-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as *S**a*, and the money Uncle J. must pay G. for the painting as *S**g*, then this inequality must hold: |*S**a*<=<=-<=<=*S**g*|<=<=β€<=<=500.
If there are several solutions, you are allowed to print any of them.
Demo Input:
['2\n1 999\n999 1\n', '3\n400 600\n400 600\n400 600\n']
Demo Output:
['AG\n', 'AGA\n']
Note:
none | 617 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to *a*1,<=*a*2,<=...,<=*a**n*.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to *b*1,<=*b*2,<=...,<=*b**n*.
Stepan said that he swapped only cubes which where on the positions between *l* and *r*, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions *l* and *r*, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.
Input Specification:
The first line contains three integers *n*, *l*, *r* (1<=β€<=*n*<=β€<=105, 1<=β€<=*l*<=β€<=*r*<=β€<=*n*) β the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*) β the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=*n*) β the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.
Output Specification:
Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).
Demo Input:
['5 2 4\n3 4 2 3 1\n3 2 3 4 1\n', '3 1 2\n1 2 3\n3 1 2\n', '4 2 4\n1 1 1 1\n1 1 1 1\n']
Demo Output:
['TRUTH\n', 'LIE\n', 'TRUTH\n']
Note:
In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values *l* and *r* there is a situation when Stepan said the truth. | 618 |
Title: Anton and Classes
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.
Anton has *n* variants when he will attend chess classes, *i*-th variant is given by a period of time (*l*1,<=*i*,<=*r*1,<=*i*). Also he has *m* variants when he will attend programming classes, *i*-th variant is given by a period of time (*l*2,<=*i*,<=*r*2,<=*i*).
Anton needs to choose exactly one of *n* possible periods of time when he will attend chess classes and exactly one of *m* possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.
The distance between periods (*l*1,<=*r*1) and (*l*2,<=*r*2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |*i*<=-<=*j*|, where *l*1<=β€<=*i*<=β€<=*r*1 and *l*2<=β€<=*j*<=β€<=*r*2. In particular, when the periods intersect, the distance between them is 0.
Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!
Input Specification:
The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of time periods when Anton can attend chess classes.
Each of the following *n* lines of the input contains two integers *l*1,<=*i* and *r*1,<=*i* (1<=β€<=*l*1,<=*i*<=β€<=*r*1,<=*i*<=β€<=109)Β β the *i*-th variant of a period of time when Anton can attend chess classes.
The following line of the input contains a single integer *m* (1<=β€<=*m*<=β€<=200<=000)Β β the number of time periods when Anton can attend programming classes.
Each of the following *m* lines of the input contains two integers *l*2,<=*i* and *r*2,<=*i* (1<=β€<=*l*2,<=*i*<=β€<=*r*2,<=*i*<=β€<=109)Β β the *i*-th variant of a period of time when Anton can attend programming classes.
Output Specification:
Output one integerΒ β the maximal possible distance between time periods.
Demo Input:
['3\n1 5\n2 6\n2 3\n2\n2 4\n6 8\n', '3\n1 5\n2 6\n3 7\n2\n2 4\n1 4\n']
Demo Output:
['3\n', '0\n']
Note:
In the first sample Anton can attend chess classes in the period (2,β3) and attend programming classes in the period (6,β8). It's not hard to see that in this case the distance between the periods will be equal to 3.
In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0. | 619 |
Title: Mirror Room
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Imagine an *n*<=Γ<=*m* grid with some blocked cells. The top left cell in the grid has coordinates (1,<=1) and the bottom right cell has coordinates (*n*,<=*m*). There are *k* blocked cells in the grid and others are empty. You flash a laser beam from the center of an empty cell (*x**s*,<=*y**s*) in one of the diagonal directions (i.e. north-east, north-west, south-east or south-west). If the beam hits a blocked cell or the border of the grid it will reflect. The behavior of the beam reflection in different situations is depicted in the figure below.
After a while the beam enters an infinite cycle. Count the number of empty cells that the beam goes through at least once. We consider that the beam goes through cell if it goes through its center.
Input Specification:
The first line of the input contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=105,<=0<=β€<=*k*<=β€<=105). Each of the next *k* lines contains two integers *x**i* and *y**i* (1<=β€<=*x**i*<=β€<=*n*,<=1<=β€<=*y**i*<=β€<=*m*) indicating the position of the *i*-th blocked cell.
The last line contains *x**s*, *y**s* (1<=β€<=*x**s*<=β€<=*n*,<=1<=β€<=*y**s*<=β€<=*m*) and the flash direction which is equal to "NE", "NW", "SE" or "SW". These strings denote directions (<=-<=1,<=1), (<=-<=1,<=<=-<=1), (1,<=1), (1,<=<=-<=1).
It's guaranteed that no two blocked cells have the same coordinates.
Output Specification:
In the only line of the output print the number of empty cells that the beam goes through at least once.
Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Demo Input:
['3 3 0\n1 2 SW\n', '7 5 3\n3 3\n4 3\n5 3\n2 1 SE\n']
Demo Output:
['6\n', '14\n']
Note:
none | 620 |
Title: T-primes
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer *t* Π’-prime, if *t* has exactly three distinct positive divisors.
You are given an array of *n* positive integers. For each of them determine whether it is Π’-prime or not.
Input Specification:
The first line contains a single positive integer, *n* (1<=β€<=*n*<=β€<=105), showing how many numbers are in the array. The next line contains *n* space-separated integers *x**i* (1<=β€<=*x**i*<=β€<=1012).
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier.
Output Specification:
Print *n* lines: the *i*-th line should contain "YES" (without the quotes), if number *x**i* is Π’-prime, and "NO" (without the quotes), if it isn't.
Demo Input:
['3\n4 5 6\n']
Demo Output:
['YES\nNO\nNO\n']
Note:
The given test has three numbers. The first number 4 has exactly three divisors β 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | 621 |
Title: Parallelogram is Back
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Long time ago Alex created an interesting problem about parallelogram. The input data for this problem contained four integer points on the Cartesian plane, that defined the set of vertices of some non-degenerate (positive area) parallelogram. Points not necessary were given in the order of clockwise or counterclockwise traversal.
Alex had very nice test for this problem, but is somehow happened that the last line of the input was lost and now he has only three out of four points of the original parallelogram. He remembers that test was so good that he asks you to restore it given only these three points.
Input Specification:
The input consists of three lines, each containing a pair of integer coordinates *x**i* and *y**i* (<=-<=1000<=β€<=*x**i*,<=*y**i*<=β€<=1000). It's guaranteed that these three points do not lie on the same line and no two of them coincide.
Output Specification:
First print integer *k*Β β the number of ways to add one new integer point such that the obtained set defines some parallelogram of positive area. There is no requirement for the points to be arranged in any special order (like traversal), they just define the set of vertices.
Then print *k* lines, each containing a pair of integerΒ β possible coordinates of the fourth point.
Demo Input:
['0 0\n1 0\n0 1\n']
Demo Output:
['3\n1 -1\n-1 1\n1 1\n']
Note:
If you need clarification of what parallelogram is, please check Wikipedia page:
https://en.wikipedia.org/wiki/Parallelogram | 622 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge [1;*m*] segment. One day Teodor noticed that picture he just drawn has one interesting feature: there doesn't exist an integer point, that belongs each of segments in the picture. Having discovered this fact, Teodor decided to share it with Sasha.
Sasha knows that Teodor likes to show off so he never trusts him. Teodor wants to prove that he can be trusted sometimes, so he decided to convince Sasha that there is no such integer point in his picture, which belongs to each segment. However Teodor is lazy person and neither wills to tell Sasha all coordinates of segments' ends nor wills to tell him their amount, so he suggested Sasha to ask him series of questions 'Given the integer point *x**i*, how many segments in Fedya's picture contain that point?', promising to tell correct answers for this questions.
Both boys are very busy studying and don't have much time, so they ask you to find out how many questions can Sasha ask Teodor, that having only answers on his questions, Sasha can't be sure that Teodor isn't lying to him. Note that Sasha doesn't know amount of segments in Teodor's picture. Sure, Sasha is smart person and never asks about same point twice.
Input Specification:
First line of input contains two integer numbers: *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100<=000)Β β amount of segments of Teodor's picture and maximal coordinate of point that Sasha can ask about.
*i*th of next *n* lines contains two integer numbers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*m*)Β β left and right ends of *i*th segment in the picture. Note that that left and right ends of segment can be the same point.
It is guaranteed that there is no integer point, that belongs to all segments.
Output Specification:
Single line of output should contain one integer number *k* β size of largest set (*x**i*,<=*cnt*(*x**i*)) where all *x**i* are different, 1<=β€<=*x**i*<=β€<=*m*, and *cnt*(*x**i*) is amount of segments, containing point with coordinate *x**i*, such that one can't be sure that there doesn't exist point, belonging to all of segments in initial picture, if he knows only this set(and doesn't know *n*).
Demo Input:
['2 4\n1 2\n3 4\n', '4 6\n1 3\n2 3\n4 6\n5 6\n']
Demo Output:
['4\n', '5\n']
Note:
First example shows situation where Sasha can never be sure that Teodor isn't lying to him, because even if one knows *cnt*(*x*<sub class="lower-index">*i*</sub>) for each point in segment [1;4], he can't distinguish this case from situation Teodor has drawn whole [1;4] segment.
In second example Sasha can ask about 5 points e.g. 1,β2,β3,β5,β6, still not being sure if Teodor haven't lied to him. But once he knows information about all points in [1;6] segment, Sasha can be sure that Teodor haven't lied to him. | 623 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
It can be shown that any positive integer *x* can be uniquely represented as *x*<==<=1<=+<=2<=+<=4<=+<=...<=+<=2*k*<=-<=1<=+<=*r*, where *k* and *r* are integers, *k*<=β₯<=0, 0<=<<=*r*<=β€<=2*k*. Let's call that representation prairie partition of *x*.
For example, the prairie partitions of 12, 17, 7 and 1 are:
17<==<=1<=+<=2<=+<=4<=+<=8<=+<=2,
7<==<=1<=+<=2<=+<=4,
1<==<=1.
Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options!
Input Specification:
The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of numbers given from Alice to Borys.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1012; *a*1<=β€<=*a*2<=β€<=...<=β€<=*a**n*)Β β the numbers given from Alice to Borys.
Output Specification:
Output, in increasing order, all possible values of *m* such that there exists a sequence of positive integers of length *m* such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.
If there are no such values of *m*, output a single integer -1.
Demo Input:
['8\n1 1 2 2 3 4 5 8\n', '6\n1 1 1 2 2 2\n', '5\n1 2 4 4 4\n']
Demo Output:
['2 \n', '2 3 \n', '-1\n']
Note:
In the first example, Alice could get the input sequence from [6,β20] as the original sequence.
In the second example, Alice's original sequence could be either [4,β5] or [3,β3,β3]. | 624 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
A sequence of positive integers *b**i* is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence *b**i* which minimizes the following expression:
You are given sequence *a**i*, help Princess Twilight to find the key.
Input Specification:
The first line contains an integer *n* (1<=β€<=*n*<=β€<=100) β the number of elements of the sequences *a* and *b*. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=30).
Output Specification:
Output the key β sequence *b**i* that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.
Demo Input:
['5\n1 1 1 1 1\n', '5\n1 6 4 2 8\n']
Demo Output:
['1 1 1 1 1 ', '1 5 3 1 8 ']
Note:
none | 625 |
Title: Dima and Hares
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her *n* hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to *n* from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and *n* don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
Input Specification:
The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=3000) β the number of hares. Then three lines follow, each line has *n* integers. The first line contains integers *a*1 *a*2 ... *a**n*. The second line contains *b*1,<=*b*2,<=...,<=*b**n*. The third line contains *c*1,<=*c*2,<=...,<=*c**n*. The following limits are fulfilled: 0<=β€<=*a**i*,<=*b**i*,<=*c**i*<=β€<=105.
Number *a**i* in the first line shows the joy that hare number *i* gets if his adjacent hares are both hungry. Number *b**i* in the second line shows the joy that hare number *i* radiates if he has exactly one full adjacent hare. Number *Ρ**i* in the third line shows the joy that hare number *i* radiates if both his adjacent hares are full.
Output Specification:
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Demo Input:
['4\n1 2 3 4\n4 3 2 1\n0 1 1 0\n', '7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3\n', '3\n1 1 1\n1 2 1\n1 1 1\n']
Demo Output:
['13\n', '44\n', '4\n']
Note:
none | 626 |
Title: Bear and Raspberry
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the following *n* days. According to the bear's data, on the *i*-th (1<=β€<=*i*<=β€<=*n*) day, the price for one barrel of honey is going to is *x**i* kilos of raspberry.
Unfortunately, the bear has neither a honey barrel, nor the raspberry. At the same time, the bear's got a friend who is ready to lend him a barrel of honey for exactly one day for *c* kilograms of raspberry. That's why the bear came up with a smart plan. He wants to choose some day *d* (1<=β€<=*d*<=<<=*n*), lent a barrel of honey and immediately (on day *d*) sell it according to a daily exchange rate. The next day (*d*<=+<=1) the bear wants to buy a new barrel of honey according to a daily exchange rate (as he's got some raspberry left from selling the previous barrel) and immediately (on day *d*<=+<=1) give his friend the borrowed barrel of honey as well as *c* kilograms of raspberry for renting the barrel.
The bear wants to execute his plan at most once and then hibernate. What maximum number of kilograms of raspberry can he earn? Note that if at some point of the plan the bear runs out of the raspberry, then he won't execute such a plan.
Input Specification:
The first line contains two space-separated integers, *n* and *c* (2<=β€<=*n*<=β€<=100,<=0<=β€<=*c*<=β€<=100), β the number of days and the number of kilos of raspberry that the bear should give for borrowing the barrel.
The second line contains *n* space-separated integers *x*1,<=*x*2,<=...,<=*x**n* (0<=β€<=*x**i*<=β€<=100), the price of a honey barrel on day *i*.
Output Specification:
Print a single integer β the answer to the problem.
Demo Input:
['5 1\n5 10 7 3 20\n', '6 2\n100 1 10 40 10 40\n', '3 0\n1 2 3\n']
Demo Output:
['3\n', '97\n', '0\n']
Note:
In the first sample the bear will lend a honey barrel at day 3 and then sell it for 7. Then the bear will buy a barrel for 3 and return it to the friend. So, the profit is (7 - 3 - 1) = 3.
In the second sample bear will lend a honey barrel at day 1 and then sell it for 100. Then the bear buy the barrel for 1 at the day 2. So, the profit is (100 - 1 - 2) = 97. | 627 |
Title: Circling Round Treasures
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure with a certain price, or a bomb, or an empty cell. Your initial position is also given to you.
You can go from one cell of the map to a side-adjacent one. At that, you are not allowed to go beyond the borders of the map, enter the cells with treasures, obstacles and bombs. To pick the treasures, you need to build a closed path (starting and ending in the starting cell). The closed path mustn't contain any cells with bombs inside. Let's assume that the sum of the treasures' values that are located inside the closed path equals *v*, and besides, you've made *k* single moves (from one cell to another) while you were going through the path, then such path brings you the profit of *v*<=-<=*k* rubles.
Your task is to build a closed path that doesn't contain any bombs and brings maximum profit.
Note that the path can have self-intersections. In order to determine if a cell lies inside a path or not, use the following algorithm:
1. Assume that the table cells are points on the plane (the table cell on the intersection of the *i*-th column and the *j*-th row is point (*i*,<=*j*)). And the given path is a closed polyline that goes through these points. 1. You need to find out if the point *p* of the table that is not crossed by the polyline lies inside the polyline. 1. Let's draw a ray that starts from point *p* and does not intersect other points of the table (such ray must exist). 1. Let's count the number of segments of the polyline that intersect the painted ray. If this number is odd, we assume that point *p* (and consequently, the table cell) lie inside the polyline (path). Otherwise, we assume that it lies outside.
Input Specification:
The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=20) β the sizes of the table. Next *n* lines each contains *m* characters β the description of the table. The description means the following:
- character "B" is a cell with a bomb; - character "S" is the starting cell, you can assume that it's empty; - digit *c* (1-8) is treasure with index *c*; - character "." is an empty cell; - character "#" is an obstacle.
Assume that the map has *t* treasures. Next *t* lines contain the prices of the treasures. The *i*-th line contains the price of the treasure with index *i*, *v**i* (<=-<=200<=β€<=*v**i*<=β€<=200). It is guaranteed that the treasures are numbered from 1 to *t*. It is guaranteed that the map has not more than 8 objects in total. Objects are bombs and treasures. It is guaranteed that the map has exactly one character "S".
Output Specification:
Print a single integer β the maximum possible profit you can get.
Demo Input:
['4 4\n....\n.S1.\n....\n....\n10\n', '7 7\n.......\n.1###2.\n.#...#.\n.#.B.#.\n.3...4.\n..##...\n......S\n100\n100\n100\n100\n', '7 8\n........\n........\n....1B..\n.S......\n....2...\n3.......\n........\n100\n-100\n100\n', '1 1\nS\n']
Demo Output:
['2\n', '364\n', '0\n', '0\n']
Note:
In the first example the answer will look as follows.
In the second example the answer will look as follows.
In the third example you cannot get profit.
In the fourth example you cannot get profit as you cannot construct a closed path with more than one cell. | 628 |
Title: Making Genome in Berland
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has noting in common with the genome that we've used to: it can have 26 distinct nucleotide types, a nucleotide of each type can occur at most once. If we assign distinct English letters to all nucleotides, then the genome of a Berland dinosaur will represent a non-empty string consisting of small English letters, such that each letter occurs in it at most once.
Scientists have *n* genome fragments that are represented as substrings (non-empty sequences of consecutive nucleotides) of the sought genome.
You face the following problem: help scientists restore the dinosaur genome. It is guaranteed that the input is not contradictory and at least one suitable line always exists. When the scientists found out that you are a strong programmer, they asked you in addition to choose the one with the minimum length. If there are multiple such strings, choose any string.
Input Specification:
The first line of the input contains a positive integer *n* (1<=β€<=*n*<=β€<=100)Β β the number of genome fragments.
Each of the next lines contains one descriptions of a fragment. Each fragment is a non-empty string consisting of distinct small letters of the English alphabet. It is not guaranteed that the given fragments are distinct. Fragments could arbitrarily overlap and one fragment could be a substring of another one.
It is guaranteed that there is such string of distinct letters that contains all the given fragments as substrings.
Output Specification:
In the single line of the output print the genome of the minimum length that contains all the given parts. All the nucleotides in the genome must be distinct. If there are multiple suitable strings, print the string of the minimum length. If there also are multiple suitable strings, you can print any of them.
Demo Input:
['3\nbcd\nab\ncdef\n', '4\nx\ny\nz\nw\n']
Demo Output:
['abcdef\n', 'xyzw\n']
Note:
none | 629 |
Title: Edge coloring of bipartite graph
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given an undirected bipartite graph without multiple edges. You should paint the edges of graph to minimal number of colours, so that no two adjacent edges have the same colour.
Input Specification:
The first line contains three integers *a*,<=*b*,<=*m* (1<=β€<=*a*,<=*b*<=β€<=1000, 0<=β€<=*m*<=β€<=105), *a* is the size of the first part, *b* is the size of the second part, *m* is the number of edges in the graph.
Each of the next *m* lines contains two integers *x*,<=*y* (1<=β€<=*x*<=β€<=*a*,<=1<=β€<=*y*<=β€<=*b*), where *x* is the number of the vertex in the first part and *y* is the number of the vertex in the second part. It is guaranteed that there are no multiple edges.
Output Specification:
In the first line print integer *c* β the minimal number of colours. The second line should contain *m* integers from 1 to *c* β the colours of the edges (in the order they appear in the input).
If there are several solutions, you can print any one of them.
Demo Input:
['4 3 5\n1 2\n2 2\n3 2\n4 1\n4 3\n']
Demo Output:
['3\n1 2 3 1 2\n']
Note:
none | 630 |
Title: Bracket Sequences Concatenation Problem
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
A bracket sequence is a string containing only characters "(" and ")".
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.
You are given $n$ bracket sequences $s_1, s_2, \dots , s_n$. Calculate the number of pairs $i, j \, (1 \le i, j \le n)$ such that the bracket sequence $s_i + s_j$ is a regular bracket sequence. Operation $+$ means concatenation i.e. "()(" + ")()" = "()()()".
If $s_i + s_j$ and $s_j + s_i$ are regular bracket sequences and $i \ne j$, then both pairs $(i, j)$ and $(j, i)$ must be counted in the answer. Also, if $s_i + s_i$ is a regular bracket sequence, the pair $(i, i)$ must be counted in the answer.
Input Specification:
The first line contains one integer $n \, (1 \le n \le 3 \cdot 10^5)$ β the number of bracket sequences. The following $n$ lines contain bracket sequences β non-empty strings consisting only of characters "(" and ")". The sum of lengths of all bracket sequences does not exceed $3 \cdot 10^5$.
Output Specification:
In the single line print a single integer β the number of pairs $i, j \, (1 \le i, j \le n)$ such that the bracket sequence $s_i + s_j$ is a regular bracket sequence.
Demo Input:
['3\n)\n()\n(\n', '2\n()\n()\n']
Demo Output:
['2\n', '4\n']
Note:
In the first example, suitable pairs are $(3, 1)$ and $(2, 2)$.
In the second example, any pair is suitable, namely $(1, 1), (1, 2), (2, 1), (2, 2)$. | 631 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
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.
Input Specification:
The only string contains the string *s* of length *l* (3<=β€<=*l*<=β€<=5000), consisting of small English letters only.
Output Specification:
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
Demo Input:
['technocup\n', 'tictictactac\n', 'bbaabaabbb\n']
Demo Output:
['1.000000000000000\n', '0.333333333333333\n', '0.100000000000000\n']
Note:
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. | 632 |
Title: Knights of a Polygonal Table
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than $k$ other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins.
Now each knight ponders: how many coins he can have if only he kills other knights?
You should answer this question for each knight.
Input Specification:
The first line contains two integers $n$ and $k$ $(1 \le n \le 10^5, 0 \le k \le \min(n-1,10))$ β the number of knights and the number $k$ from the statement.
The second line contains $n$ integers $p_1, p_2 ,\ldots,p_n$ $(1 \le p_i \le 10^9)$ β powers of the knights. All $p_i$ are distinct.
The third line contains $n$ integers $c_1, c_2 ,\ldots,c_n$ $(0 \le c_i \le 10^9)$ β the number of coins each knight has.
Output Specification:
Print $n$ integers β the maximum number of coins each knight can have it only he kills other knights.
Demo Input:
['4 2\n4 5 9 7\n1 2 11 33\n', '5 1\n1 2 3 4 5\n1 2 3 4 5\n', '1 0\n2\n3\n']
Demo Output:
['1 3 46 36 ', '1 3 5 7 9 ', '3 ']
Note:
Consider the first example.
- The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has. - The second knight can kill the first knight and add his coin to his own two. - The third knight is the strongest, but he can't kill more than $k = 2$ other knights. It is optimal to kill the second and the fourth knights: $2+11+33 = 46$. - The fourth knight should kill the first and the second knights: $33+1+2 = 36$.
In the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own.
In the third example there is only one knight, so he can't kill anyone. | 633 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given an array *a* with *n* distinct integers. Construct an array *b* by permuting *a* such that for every non-empty subset of indices *S*<==<={*x*1,<=*x*2,<=...,<=*x**k*} (1<=β€<=*x**i*<=β€<=*n*, 0<=<<=*k*<=<<=*n*) the sums of elements on that positions in *a* and *b* are different, i.Β e.
Input Specification:
The first line contains one integer *n* (1<=β€<=*n*<=β€<=22)Β β the size of the array.
The second line contains *n* space-separated distinct integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109)Β β the elements of the array.
Output Specification:
If there is no such array *b*, print -1.
Otherwise in the only line print *n* space-separated integers *b*1,<=*b*2,<=...,<=*b**n*. Note that *b* must be a permutation of *a*.
If there are multiple answers, print any of them.
Demo Input:
['2\n1 2\n', '4\n1000 100 10 1\n']
Demo Output:
['2 1 \n', '100 1 1000 10\n']
Note:
An array *x* is a permutation of *y*, if we can shuffle elements of *y* such that it will coincide with *x*.
Note that the empty subset and the subset containing all indices are not counted. | 634 |
Title: Escape Through Leaf
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given a tree with *n* nodes (numbered from 1 to *n*) rooted at node 1. Also, each node has two values associated with it. The values for *i*-th node are *a**i* and *b**i*.
You can jump from a node to any node in its subtree. The cost of one jump from node *x* to node *y* is the product of *a**x* and *b**y*. The total cost of a path formed by one or more jumps is sum of costs of individual jumps. For every node, calculate the minimum total cost to reach any leaf from that node. Pay attention, that root can never be leaf, even if it has degree 1.
Note that you cannot jump from a node to itself.
Input Specification:
The first line of input contains an integer *n* (2<=β€<=*n*<=β€<=105)Β β the number of nodes in the tree.
The second line contains *n* space-separated integers *a*1,<=<=*a*2,<=<=...,<=<=*a**n*(<=-<=105<=<=β€<=<=*a**i*<=<=β€<=<=105).
The third line contains *n* space-separated integers *b*1,<=<=*b*2,<=<=...,<=<=*b**n*(<=-<=105<=<=β€<=<=*b**i*<=<=β€<=<=105).
Next *n*<=<=-<=<=1 lines contains two space-separated integers *u**i* and *v**i* (1<=β€<=*u**i*,<=<=*v**i*<=β€<=<=*n*) describing edge between nodes *u**i* and *v**i* in the tree.
Output Specification:
Output *n* space-separated integers, *i*-th of which denotes the minimum cost of a path from node *i* to reach any leaf.
Demo Input:
['3\n2 10 -1\n7 -7 5\n2 3\n2 1\n', '4\n5 -10 5 7\n-8 -80 -3 -10\n2 1\n2 4\n1 3\n']
Demo Output:
['10 50 0 ', '-300 100 0 0 ']
Note:
In the first example, node 3 is already a leaf, so the cost is 0. For node 2, jump to node 3 with cost *a*<sub class="lower-index">2</sub>βΓβ*b*<sub class="lower-index">3</sub>β=β50. For node 1, jump directly to node 3 with cost *a*<sub class="lower-index">1</sub>βΓβ*b*<sub class="lower-index">3</sub>β=β10.
In the second example, node 3 and node 4 are leaves, so the cost is 0. For node 2, jump to node 4 with cost *a*<sub class="lower-index">2</sub>βΓβ*b*<sub class="lower-index">4</sub>β=β100. For node 1, jump to node 2 with cost *a*<sub class="lower-index">1</sub>βΓβ*b*<sub class="lower-index">2</sub>β=ββ-β400 followed by a jump from 2 to 4 with cost *a*<sub class="lower-index">2</sub>βΓβ*b*<sub class="lower-index">4</sub>β=β100. | 635 |
Title: Nikita and Order Statistics
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Nikita likes tasks on order statistics, for example, he can easily find the $k$-th number in increasing order on a segment of an array. But now Nikita wonders how many segments of an array there are such that a given number $x$ is the $k$-th number in increasing order on this segment. In other words, you should find the number of segments of a given array such that there are exactly $k$ numbers of this segment which are less than $x$.
Nikita wants to get answer for this question for each $k$ from $0$ to $n$, where $n$ is the size of the array.
Input Specification:
The first line contains two integers $n$ and $x$ $(1 \le n \le 2 \cdot 10^5, -10^9 \le x \le 10^9)$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(-10^9 \le a_i \le 10^9)$Β β the given array.
Output Specification:
Print $n+1$ integers, where the $i$-th number is the answer for Nikita's question for $k=i-1$.
Demo Input:
['5 3\n1 2 3 4 5\n', '2 6\n-5 9\n', '6 99\n-1 -1 -1 -1 -1 -1\n']
Demo Output:
['6 5 4 0 0 0 ', '1 2 0 ', '0 6 5 4 3 2 1 ']
Note:
none | 636 |
Title: Break Up
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Again, there are hard times in Berland! Many towns have such tensions that even civil war is possible.
There are *n* towns in Reberland, some pairs of which connected by two-way roads. It is not guaranteed that it is possible to reach one town from any other town using these roads.
Towns *s* and *t* announce the final break of any relationship and intend to rule out the possibility of moving between them by the roads. Now possibly it is needed to close several roads so that moving from *s* to *t* using roads becomes impossible. Each town agrees to spend money on closing no more than one road, therefore, the total number of closed roads will be no more than two.
Help them find set of no more than two roads such that there will be no way between *s* and *t* after closing these roads. For each road the budget required for its closure was estimated. Among all sets find such that the total budget for the closure of a set of roads is minimum.
Input Specification:
The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=1000, 0<=β€<=*m*<=β€<=30<=000)Β β the number of towns in Berland and the number of roads.
The second line contains integers *s* and *t* (1<=β€<=*s*,<=*t*<=β€<=*n*, *s*<=β <=*t*)Β β indices of towns which break up the relationships.
Then follow *m* lines, each of them contains three integers *x**i*, *y**i* and *w**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*, 1<=β€<=*w**i*<=β€<=109)Β β indices of towns connected by the *i*-th road, and the budget on its closure.
All roads are bidirectional. It is allowed that the pair of towns is connected by more than one road. Roads that connect the city to itself are allowed.
Output Specification:
In the first line print the minimum budget required to break up the relations between *s* and *t*, if it is allowed to close no more than two roads.
In the second line print the value *c* (0<=β€<=*c*<=β€<=2)Β β the number of roads to be closed in the found solution.
In the third line print in any order *c* diverse integers from 1 to *m*Β β indices of closed roads. Consider that the roads are numbered from 1 to *m* in the order they appear in the input.
If it is impossible to make towns *s* and *t* disconnected by removing no more than 2 roads, the output should contain a single line -1.
If there are several possible answers, you may print any of them.
Demo Input:
['6 7\n1 6\n2 1 6\n2 3 5\n3 4 9\n4 6 4\n4 6 5\n4 5 1\n3 1 3\n', '6 7\n1 6\n2 3 1\n1 2 2\n1 3 3\n4 5 4\n3 6 5\n4 6 6\n1 5 7\n', '5 4\n1 5\n2 1 3\n3 2 1\n3 4 4\n4 5 2\n', '2 3\n1 2\n1 2 734458840\n1 2 817380027\n1 2 304764803\n']
Demo Output:
['8\n2\n2 7\n', '9\n2\n4 5\n', '1\n1\n2\n', '-1\n']
Note:
none | 637 |
Title: Given Length and Sum of Digits...
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You have a positive integer *m* and a non-negative integer *s*. Your task is to find the smallest and the largest of the numbers that have length *m* and sum of digits *s*. The required numbers should be non-negative integers written in the decimal base without leading zeroes.
Input Specification:
The single line of the input contains a pair of integers *m*, *s* (1<=β€<=*m*<=β€<=100,<=0<=β€<=*s*<=β€<=900) β the length and the sum of the digits of the required numbers.
Output Specification:
In the output print the pair of the required non-negative integer numbers β first the minimum possible number, then β the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1 -1" (without the quotes).
Demo Input:
['2 15\n', '3 0\n']
Demo Output:
['69 96\n', '-1 -1\n']
Note:
none | 638 |
Title: PLEASE
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
As we all know Barney's job is "PLEASE" and he has not much to do at work. That's why he started playing "cups and key". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's heart is under the middle cup.
Then at one turn Barney swaps the cup in the middle with any of other two cups randomly (he choses each with equal probability), so the chosen cup becomes the middle one. Game lasts *n* turns and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it was at the start.
After *n*-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney was distracted while making turns and doesn't know if the key is under the middle cup. That's why he asked you to tell him the probability that girl guessed right.
Number *n* of game turns can be extremely large, that's why Barney did not give it to you. Instead he gave you an array *a*1,<=*a*2,<=...,<=*a**k* such that
in other words, *n* is multiplication of all elements of the given array.
Because of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In other words you need to find it as a fraction *p*<=/<=*q* such that , where is the greatest common divisor. Since *p* and *q* can be extremely large, you only need to find the remainders of dividing each of them by 109<=+<=7.
Please note that we want of *p* and *q* to be 1, not of their remainders after dividing by 109<=+<=7.
Input Specification:
The first line of input contains a single integer *k* (1<=β€<=*k*<=β€<=105)Β β the number of elements in array Barney gave you.
The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (1<=β€<=*a**i*<=β€<=1018)Β β the elements of the array.
Output Specification:
In the only line of output print a single string *x*<=/<=*y* where *x* is the remainder of dividing *p* by 109<=+<=7 and *y* is the remainder of dividing *q* by 109<=+<=7.
Demo Input:
['1\n2\n', '3\n1 1 1\n']
Demo Output:
['1/2\n', '0/1\n']
Note:
none | 639 |
Title: Tricky Password
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
In order to ensure confidentiality, the access to the "Russian Code Cup" problems is password protected during the problem development process.
To select a password, the jury can generate a special table that contains *n* columns and the infinite number of rows. To construct a table, the first row is fixed, and all the others are obtained by the following rule:
In the row *i* at position *p* there is a number equal to the number of times *a*[*i*<=-<=1][*p*] occurs on the prefix *a*[*i*<=-<=1][1... *p*].
To ensure the required level of confidentiality, the jury must be able to perform the following operations:
- Replace number *a*[1][*p*] by *v* and rebuild the table. - Find the number *a*[*x*][*y*], which will be the new password.
Doing all these steps manually is very tedious, so the jury asks you to help him. Write a program that responds to the request of the jury.
Input Specification:
The first line contains an integer *n* (1<=β€<=*n*<=β€<=100000) β the number of columns. The second line contains the description of the first row of the table, that is, *n* integers, which are not less than 1 and do not exceed 109.
The third line of the input contains an integer *m* (1<=β€<=*m*<=β€<=100000) β the number of requests.
Next, each row contains a description of the request, which consists of three integers:
- If the first number is equal to 1, then the remaining two numbers are *v*, *p* (1<=β€<=*v*<=β€<=109; 1<=β€<=*p*<=β€<=*n*). So, you should put value *v* in the position *p* in the first row. - If the first number is equal to 2, then the remaining two numbers are *x*, *y* (1<=β€<=*x*<=β€<=105; 1<=β€<=*y*<=β€<=*n*) β the row and column of the table cell from which you want to get value.
Output Specification:
Print an answer for each request of the second type in the order you receive them.
Demo Input:
['6\n1 2 2 2 3 1\n3\n2 2 3\n1 3 3\n2 3 4\n']
Demo Output:
['2\n1\n']
Note:
none | 640 |
Title: Sail
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
The polar bears are going fishing. They plan to sail from (*s**x*,<=*s**y*) to (*e**x*,<=*e**y*). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (*x*,<=*y*).
- If the wind blows to the east, the boat will move to (*x*<=+<=1,<=*y*). - If the wind blows to the south, the boat will move to (*x*,<=*y*<=-<=1). - If the wind blows to the west, the boat will move to (*x*<=-<=1,<=*y*). - If the wind blows to the north, the boat will move to (*x*,<=*y*<=+<=1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (*x*,<=*y*). Given the wind direction for *t* seconds, what is the earliest time they sail to (*e**x*,<=*e**y*)?
Input Specification:
The first line contains five integers *t*,<=*s**x*,<=*s**y*,<=*e**x*,<=*e**y* (1<=β€<=*t*<=β€<=105,<=<=-<=109<=β€<=*s**x*,<=*s**y*,<=*e**x*,<=*e**y*<=β€<=109). The starting location and the ending location will be different.
The second line contains *t* characters, the *i*-th character is the wind blowing direction at the *i*-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).
Output Specification:
If they can reach (*e**x*,<=*e**y*) within *t* seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).
Demo Input:
['5 0 0 1 1\nSESNW\n', '10 5 3 3 6\nNENSWESNEE\n']
Demo Output:
['4\n', '-1\n']
Note:
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | 641 |
Title: Kingdom and its Cities
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marriage, reforms must be finished as soon as possible.
The kingdom currently consists of *n* cities. Cities are connected by *n*<=-<=1 bidirectional road, such that one can get from any city to any other city. As the King had to save a lot, there is only one path between any two cities.
What is the point of the reform? The key ministries of the state should be relocated to distinct cities (we call such cities important). However, due to the fact that there is a high risk of an attack by barbarians it must be done carefully. The King has made several plans, each of which is described by a set of important cities, and now wonders what is the best plan.
Barbarians can capture some of the cities that are not important (the important ones will have enough protection for sure), after that the captured city becomes impassable. In particular, an interesting feature of the plan is the minimum number of cities that the barbarians need to capture in order to make all the important cities isolated, that is, from all important cities it would be impossible to reach any other important city.
Help the King to calculate this characteristic for each of his plan.
Input Specification:
The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of cities in the kingdom.
Each of the next *n*<=-<=1 lines contains two distinct integers *u**i*, *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*)Β β the indices of the cities connected by the *i*-th road. It is guaranteed that you can get from any city to any other one moving only along the existing roads.
The next line contains a single integer *q* (1<=β€<=*q*<=β€<=100<=000)Β β the number of King's plans.
Each of the next *q* lines looks as follows: first goes number *k**i*Β β the number of important cities in the King's plan, (1<=β€<=*k**i*<=β€<=*n*), then follow exactly *k**i* space-separated pairwise distinct numbers from 1 to *n*Β β the numbers of important cities in this plan.
The sum of all *k**i*'s does't exceed 100<=000.
Output Specification:
For each plan print a single integer β the minimum number of cities that the barbarians need to capture, or print <=-<=1 if all the barbarians' attempts to isolate important cities will not be effective.
Demo Input:
['4\n1 3\n2 3\n4 3\n4\n2 1 2\n3 2 3 4\n3 1 2 4\n4 1 2 3 4\n', '7\n1 2\n2 3\n3 4\n1 5\n5 6\n5 7\n1\n4 2 4 6 7\n']
Demo Output:
['1\n-1\n1\n-1\n', '2\n']
Note:
In the first sample, in the first and the third King's plan barbarians can capture the city 3, and that will be enough. In the second and the fourth plans all their attempts will not be effective.
In the second sample the cities to capture are 3 and 5. | 642 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.
Each club's full name consist of two words: the team's name and the hometown's name, for example, "DINAMO BYTECITY". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that:
1. the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN", 1. or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "DIB".
Apart from this, there is a rule that if for some club *x* the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club *x*. For example, if the above mentioned club has short name "DIB", then no club for which the first option is chosen can have short name equal to "DIN". However, it is possible that some club have short name "DIN", where "DI" are the first two letters of the team's name, and "N" is the first letter of hometown's name. Of course, no two teams can have the same short name.
Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen.
Input Specification:
The first line contains a single integer *n* (1<=β€<=*n*<=β€<=1000)Β β the number of clubs in the league.
Each of the next *n* lines contains two wordsΒ β the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20.
Output Specification:
It it is not possible to choose short names and satisfy all constraints, print a single line "NO".
Otherwise, in the first line print "YES". Then print *n* lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input.
If there are multiple answers, print any of them.
Demo Input:
['2\nDINAMO BYTECITY\nFOOTBALL MOSCOW\n', '2\nDINAMO BYTECITY\nDINAMO BITECITY\n', '3\nPLAYFOOTBALL MOSCOW\nPLAYVOLLEYBALL SPB\nGOGO TECHNOCUP\n', '3\nABC DEF\nABC EFG\nABD OOO\n']
Demo Output:
['YES\nDIN\nFOO\n', 'NO\n', 'YES\nPLM\nPLS\nGOG\n', 'YES\nABD\nABE\nABO\n']
Note:
In the first sample Innokenty can choose first option for both clubs.
In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.
In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club.
In the fourth example note that it is possible that the chosen short name for some club *x* is the same as the first option of another club *y* if the first options of *x* and *y* are different. | 643 |
Title: Berland collider
Time Limit: 1 seconds
Memory Limit: 256 megabytes
Problem Description:
Recently the construction of Berland collider has been completed. Collider can be represented as a long narrow tunnel that contains *n* particles. We associate with collider 1-dimensional coordinate system, going from left to right. For each particle we know its coordinate and velocity at the moment of start of the collider. The velocities of the particles don't change after the launch of the collider. Berland scientists think that the big bang will happen at the first collision of particles, whose velocities differs in directions. Help them to determine how much time elapses after the launch of the collider before the big bang happens.
Input Specification:
The first line contains single integer *n* (1<=β€<=*n*<=β€<=5Β·105) β amount of particles in the collider. Next *n* lines contain description of particles. Each particle is described by two integers *x**i*, *v**i* (<=-<=109<=β€<=*x**i*,<=*v**i*<=β€<=109,<=*v**i*<=β <=0) β coordinate and velocity respectively. All the coordinates are distinct. The particles are listed in order of increasing of coordinates. All the coordinates are in meters, and all the velocities β in meters per second. The negative velocity means that after the start of collider the particle will move to the left, and the positive β that the particle will move to the right.
Output Specification:
If there will be no big bang, output -1. Otherwise output one number β how much time in seconds elapses after the launch of the collider before the big bang happens. Your answer must have a relative or absolute error less than 10<=-<=9.
Demo Input:
['3\n-5 9\n0 1\n5 -1\n', '6\n1 3\n2 3\n3 3\n4 -3\n5 -1\n6 -100\n']
Demo Output:
['1.00000000000000000000\n', '0.02912621359223301065\n']
Note:
none | 644 |
Title: Dense Subsequence
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given a string *s*, consisting of lowercase English letters, and the integer *m*.
One should choose some symbols from the given string so that any contiguous subsegment of length *m* has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1<=β€<=*i*1<=<<=*i*2<=<<=...<=<<=*i**t*<=β€<=|*s*|. The selected sequence must meet the following condition: for every *j* such that 1<=β€<=*j*<=β€<=|*s*|<=-<=*m*<=+<=1, there must be at least one selected index that belongs to the segment [*j*,<=Β *j*<=+<=*m*<=-<=1], i.e. there should exist a *k* from 1 to *t*, such that *j*<=β€<=*i**k*<=β€<=*j*<=+<=*m*<=-<=1.
Then we take any permutation *p* of the selected indices and form a new string *s**i**p*1*s**i**p*2... *s**i**p**t*.
Find the lexicographically smallest string, that can be obtained using this procedure.
Input Specification:
The first line of the input contains a single integer *m* (1<=β€<=*m*<=β€<=100<=000).
The second line contains the string *s* consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100<=000. It is also guaranteed that the number *m* doesn't exceed the length of the string *s*.
Output Specification:
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
Demo Input:
['3\ncbabc\n', '2\nabcab\n', '3\nbcabcbaccba\n']
Demo Output:
['a\n', 'aab\n', 'aaabb\n']
Note:
In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1,β2,β4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". | 645 |
Title: Alyona and Triangles
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given *n* points with integer coordinates on the plane. Points are given in a way such that there is no triangle, formed by any three of these *n* points, which area exceeds *S*.
Alyona tried to construct a triangle with integer coordinates, which contains all *n* points and which area doesn't exceed 4*S*, but, by obvious reason, had no success in that. Please help Alyona construct such triangle. Please note that vertices of resulting triangle are not necessarily chosen from *n* given points.
Input Specification:
In the first line of the input two integers *n* and *S* (3<=β€<=*n*<=β€<=5000, 1<=β€<=*S*<=β€<=1018) are givenΒ β the number of points given and the upper bound value of any triangle's area, formed by any three of given *n* points.
The next *n* lines describes given points: *i**th* of them consists of two integers *x**i* and *y**i* (<=-<=108<=β€<=*x**i*,<=*y**i*<=β€<=108)Β β coordinates of *i**th* point.
It is guaranteed that there is at least one triple of points not lying on the same line.
Output Specification:
Print the coordinates of three pointsΒ β vertices of a triangle which contains all *n* points and which area doesn't exceed 4*S*.
Coordinates of every triangle's vertex should be printed on a separate line, every coordinate pair should be separated by a single space. Coordinates should be an integers not exceeding 109 by absolute value.
It is guaranteed that there is at least one desired triangle. If there is more than one answer, print any of them.
Demo Input:
['4 1\n0 0\n1 0\n0 1\n1 1\n']
Demo Output:
['-1 0\n2 0\n0 2']
| 646 |
Title: Maximum Control (easy)
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
The Resistance is trying to take control over all planets in a particular solar system. This solar system is shaped like a tree. More precisely, some planets are connected by bidirectional hyperspace tunnels in such a way that there is a path between every pair of the planets, but removing any tunnel would disconnect some of them.
The Resistance already has measures in place that will, when the time is right, enable them to control every planet that is not remote. A planet is considered to be remote if it is connected to the rest of the planets only via a single hyperspace tunnel.
How much work is there left to be done: that is, how many remote planets are there?
Input Specification:
The first line of the input contains an integer *N* (2<=β€<=*N*<=β€<=1000) β the number of planets in the galaxy.
The next *N*<=-<=1 lines describe the hyperspace tunnels between the planets. Each of the *N*<=-<=1 lines contains two space-separated integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*N*) indicating that there is a bidirectional hyperspace tunnel between the planets *u* and *v*. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets.
Output Specification:
A single integer denoting the number of remote planets.
Demo Input:
['5\n4 1\n4 2\n1 3\n1 5\n', '4\n1 2\n4 3\n1 4\n']
Demo Output:
['3\n', '2\n']
Note:
In the first example, only planets 2, 3 and 5 are connected by a single tunnel.
In the second example, the remote planets are 2 and 3.
Note that this problem has only two versions β easy and medium. | 647 |
Title: Demonstration
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
In the capital city of Berland, Bertown, demonstrations are against the recent election of the King of Berland. Berland opposition, led by Mr. Ovalny, believes that the elections were not fair enough and wants to organize a demonstration at one of the squares.
Bertown has *n* squares, numbered from 1 to *n*, they are numbered in the order of increasing distance between them and the city center. That is, square number 1 is central, and square number *n* is the farthest from the center. Naturally, the opposition wants to hold a meeting as close to the city center as possible (that is, they want an square with the minimum number).
There are exactly *k* (*k*<=<<=*n*) days left before the demonstration. Now all squares are free. But the Bertown city administration never sleeps, and the approval of an application for the demonstration threatens to become a very complex process. The process of approval lasts several days, but every day the following procedure takes place:
- The opposition shall apply to hold a demonstration at a free square (the one which isn't used by the administration). - The administration tries to move the demonstration to the worst free square left. To do this, the administration organizes some long-term activities on the square, which is specified in the application of opposition. In other words, the administration starts using the square and it is no longer free. Then the administration proposes to move the opposition demonstration to the worst free square. If the opposition has applied for the worst free square then request is accepted and administration doesn't spend money. If the administration does not have enough money to organize an event on the square in question, the opposition's application is accepted. If administration doesn't have enough money to organize activity, then rest of administration's money spends and application is accepted - If the application is not accepted, then the opposition can agree to the administration's proposal (that is, take the worst free square), or withdraw the current application and submit another one the next day. If there are no more days left before the meeting, the opposition has no choice but to agree to the proposal of City Hall. If application is accepted opposition can reject it. It means than opposition still can submit more applications later, but square remains free.
In order to organize an event on the square *i*, the administration needs to spend *a**i* bourles. Because of the crisis the administration has only *b* bourles to confront the opposition. What is the best square that the opposition can take, if the administration will keep trying to occupy the square in question each time? Note that the administration's actions always depend only on the actions of the opposition.
Input Specification:
The first line contains two integers *n* and *k* β the number of squares and days left before the meeting, correspondingly (1<=β€<=*k*<=<<=*n*<=β€<=105).
The second line contains a single integer *b* β the number of bourles the administration has (1<=β€<=*b*<=β€<=1018).
The third line contains *n* space-separated integers *a**i* β the sum of money, needed to organise an event on square *i* (1<=β€<=*a**i*<=β€<=109).
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output Specification:
Print a single number β the minimum number of the square where the opposition can organize the demonstration.
Demo Input:
['5 2\n8\n2 4 5 3 1\n', '5 2\n8\n3 2 4 1 5\n', '5 4\n1000000000000000\n5 4 3 2 1\n']
Demo Output:
['2\n', '5\n', '5\n']
Note:
In the first sample the opposition can act like this. On day one it applies for square 3. The administration has to organize an event there and end up with 3 bourles. If on the second day the opposition applies for square 2, the administration won't have the money to intervene.
In the second sample the opposition has only the chance for the last square. If its first move occupies one of the first four squares, the administration is left with at least 4 bourles, which means that next day it can use its next move to move the opposition from any square to the last one.
In the third sample administration has a lot of money, so opposition can occupy only last square. | 648 |
Title: Fake News (medium)
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Thanks to your help, Heidi is confident that no one can fool her. She has now decided to post some fake news on the HC2 Facebook page. However, she wants to be able to communicate to the HC2 committee that the post is fake, using some secret phrase hidden in the post as a subsequence. To make this method foolproof, she wants the phrase to appear *n* times in the post. She is asking you to design a post (string) *s* and a hidden phrase *p* such that *p* appears in *s* as a subsequence exactly *n* times.
Input Specification:
The first and only line of input contains a single integer *n* (1<=β€<=*n*<=β€<=1<=000<=000).
Output Specification:
The output should contain two nonempty strings *s* and *p* separated by a single space. Each string should be composed of letters (a-z and A-Z: both lowercase and uppercase are allowed) and have length at most 200. The number of occurrences of *p* in *s* as a subsequence should be exactly *n*. If there are many possible solutions, output any of them. It is guaranteed that at least one solution exists.
Demo Input:
['2\n', '4\n', '6\n']
Demo Output:
['hHheidi Hei', 'bbbba ba', 'aaabb ab']
Note:
An occurrence of *p* as a subsequence in *s* should be thought of as a set of positions in *s* such that the letters at these positions, in order, form *p*. The number of occurences is thus the number of such sets. For example, ab appears 6 times as a subsequence in aaabb, for the following sets of positions: {1,β4},β{1,β5},β{2,β4},β{2,β5},β{3,β4},β{3,β5} (that is, we should choose one of the a's and one of the b's). | 649 |
Title: Adding Digits
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Vasya has got two number: *a* and *b*. However, Vasya finds number *a* too short. So he decided to repeat the operation of lengthening number *a* *n* times.
One operation of lengthening a number means adding exactly one digit to the number (in the decimal notation) to the right provided that the resulting number is divisible by Vasya's number *b*. If it is impossible to obtain the number which is divisible by *b*, then the lengthening operation cannot be performed.
Your task is to help Vasya and print the number he can get after applying the lengthening operation to number *a* *n* times.
Input Specification:
The first line contains three integers: *a*,<=*b*,<=*n* (1<=β€<=*a*,<=*b*,<=*n*<=β€<=105).
Output Specification:
In a single line print the integer without leading zeros, which Vasya can get when he applies the lengthening operations to number *a* *n* times. If no such number exists, then print number -1. If there are multiple possible answers, print any of them.
Demo Input:
['5 4 5\n', '12 11 1\n', '260 150 10\n']
Demo Output:
['524848\n', '121\n', '-1\n']
Note:
none | 650 |
Title: Checkpoints
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Vasya takes part in the orienteering competition. There are *n* checkpoints located along the line at coordinates *x*1,<=*x*2,<=...,<=*x**n*. Vasya starts at the point with coordinate *a*. His goal is to visit at least *n*<=-<=1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.
Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.
Input Specification:
The first line of the input contains two integers *n* and *a* (1<=β€<=*n*<=β€<=100<=000, <=-<=1<=000<=000<=β€<=*a*<=β€<=1<=000<=000)Β β the number of checkpoints and Vasya's starting position respectively.
The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=1<=000<=000<=β€<=*x**i*<=β€<=1<=000<=000)Β β coordinates of the checkpoints.
Output Specification:
Print one integerΒ β the minimum distance Vasya has to travel in order to visit at least *n*<=-<=1 checkpoint.
Demo Input:
['3 10\n1 7 12\n', '2 0\n11 -10\n', '5 0\n0 0 1000 0 0\n']
Demo Output:
['7\n', '10\n', '0\n']
Note:
In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12β-β10β=β2) and then proceed to the second one (distance is 12β-β7β=β5). The total distance is equal to 2β+β5β=β7.
In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point β-β10. | 651 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Levko loves strings of length *n*, consisting of lowercase English letters, very much. He has one such string *s*. For each string *t* of length *n*, Levko defines its beauty relative to *s* as the number of pairs of indexes *i*, *j* (1<=β€<=*i*<=β€<=*j*<=β€<=*n*), such that substring *t*[*i*..*j*] is lexicographically larger than substring *s*[*i*..*j*].
The boy wondered how many strings *t* are there, such that their beauty relative to *s* equals exactly *k*. Help him, find the remainder after division this number by 1000000007 (109<=+<=7).
A substring *s*[*i*..*j*] of string *s*<==<=*s*1*s*2... *s**n* is string *s**i**s**i*<=<=+<=<=1... *s**j*.
String *x*<=<==<=<=*x*1*x*2... *x**p* is lexicographically larger than string *y*<=<==<=<=*y*1*y*2... *y**p*, if there is such number *r* (*r*<=<<=*p*), that *x*1<=<==<=<=*y*1,<=<=*x*2<=<==<=<=*y*2,<=<=... ,<=<=*x**r*<=<==<=<=*y**r* and *x**r*<=<=+<=<=1<=><=*y**r*<=<=+<=<=1. The string characters are compared by their ASCII codes.
Input Specification:
The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=2000, 0<=β€<=*k*<=β€<=2000).
The second line contains a non-empty string *s* of length *n*. String *s* consists only of lowercase English letters.
Output Specification:
Print a single number β the answer to the problem modulo 1000000007 (109<=+<=7).
Demo Input:
['2 2\nyz\n', '2 3\nyx\n', '4 7\nabcd\n']
Demo Output:
['26\n', '2\n', '21962\n']
Note:
none | 652 |
Title: Berland Traffic
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Berland traffic is very different from traffic in other countries. The capital of Berland consists of *n* junctions and *m* roads. Each road connects a pair of junctions. There can be multiple roads between a pair of junctions. For each road we know its capacity: value *c**i* is the maximum number of cars that can drive along a road in any direction per a unit of time. For each road, the cars can drive along it in one of two direction. That it, the cars can't simultaneously move in both directions. A road's traffic is the number of cars that goes along it per a unit of time. For road (*a**i*,<=*b**i*) this value is negative, if the traffic moves from *b**i* to *a**i*. A road's traffic can be a non-integer number.
The capital has two special junctions β the entrance to the city (junction 1) and the exit from the city (junction *n*). For all other junctions it is true that the traffic is not lost there. That is, for all junctions except for 1 and *n* the incoming traffic sum equals the outgoing traffic sum.
Traffic has an unusual peculiarity in the capital of Berland β for any pair of junctions (*x*,<=*y*) the sum of traffics along any path from *x* to *y* doesn't change depending on the choice of the path. Such sum includes traffic along all roads on the path (possible with the "minus" sign, if the traffic along the road is directed against the direction of the road on the path from *x* to *y*).
Your task is to find the largest traffic that can pass trough the city per one unit of time as well as the corresponding traffic for each road.
Input Specification:
The first line contains a positive integer *n* β the number of junctions (2<=β€<=*n*<=β€<=100). The second line contains integer *m* (1<=β€<=*m*<=β€<=5000) β the number of roads. Next *m* lines contain the roads' descriptions. Each road contains a group of three numbers *a**i*, *b**i*, *c**i*, where *a**i*,<=*b**i* are the numbers of junctions, connected by the given road, and *c**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*; *a**i*<=β <=*b**i*; 0<=β€<=*c**i*<=β€<=10000) is the largest permissible traffic along this road.
Output Specification:
In the first line print the required largest traffic across the city. Then print *m* lines, on each line print the speed, at which the traffic moves along the corresponding road. If the direction doesn't match the order of the junctions, given in the input, then print the traffic with the minus sign. Print the numbers with accuracy of at least five digits after the decimal point.
If there are many optimal solutions, print any of them.
Demo Input:
['2\n3\n1 2 2\n1 2 4\n2 1 1000\n', '7\n11\n1 2 7\n1 2 7\n1 3 7\n1 4 7\n2 3 7\n2 5 7\n3 6 7\n4 7 7\n5 4 7\n5 6 7\n6 7 7\n']
Demo Output:
['6.00000\n2.00000\n2.00000\n-2.00000\n', '13.00000\n2.00000\n2.00000\n3.00000\n6.00000\n1.00000\n3.00000\n4.00000\n7.00000\n1.00000\n2.00000\n6.00000\n']
Note:
none | 653 |
Title: Chloe and pleasant prizes
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took *n* prizes for the contestants and wrote on each of them a unique id (integer from 1 to *n*). A gift *i* is characterized by integer *a**i*Β β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with *n* vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
Input Specification:
The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105)Β β the number of gifts.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109)Β β the pleasantness of the gifts.
The next (*n*<=-<=1) lines contain two numbers each. The *i*-th of these lines contains integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*)Β β the description of the tree's edges. It means that gifts with numbers *u**i* and *v**i* are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: *v**i* hangs on *u**i* or *u**i* hangs on *v**i*.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
Output Specification:
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integerΒ β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible.
Demo Input:
['8\n0 5 -1 4 3 2 6 5\n1 2\n2 4\n2 5\n1 3\n3 6\n6 7\n6 8\n', '4\n1 -5 1 1\n1 2\n1 4\n2 3\n', '1\n-1\n']
Demo Output:
['25', '2', 'Impossible']
Note:
none | 654 |
Title: Broken robot
Time Limit: 2 seconds
Memory Limit: 256 megabytes
Problem Description:
You received as a gift a very clever robot walking on a rectangular board. Unfortunately, you understood that it is broken and behaves rather strangely (randomly). The board consists of *N* rows and *M* columns of cells. The robot is initially at some cell on the *i*-th row and the *j*-th column. Then at every step the robot could go to some another cell. The aim is to go to the bottommost (*N*-th) row. The robot can stay at it's current cell, move to the left, move to the right, or move to the cell below the current. If the robot is in the leftmost column it cannot move to the left, and if it is in the rightmost column it cannot move to the right. At every step all possible moves are equally probable. Return the expected number of step to reach the bottommost row.
Input Specification:
On the first line you will be given two space separated integers *N* and *M* (1<=β€<=*N*,<=*M*<=β€<=1000). On the second line you will be given another two space separated integers *i* and *j* (1<=β€<=*i*<=β€<=*N*,<=1<=β€<=*j*<=β€<=*M*) β the number of the initial row and the number of the initial column. Note that, (1,<=1) is the upper left corner of the board and (*N*,<=*M*) is the bottom right corner.
Output Specification:
Output the expected number of steps on a line of itself with at least 4 digits after the decimal point.
Demo Input:
['10 10\n10 4\n', '10 14\n5 14\n']
Demo Output:
['0.0000000000\n', '18.0038068653\n']
Note:
none | 655 |
Title: Queue
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Little girl Susie went shopping with her mom and she wondered how to improve service quality.
There are *n* people in the queue. For each person we know time *t**i* needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.
Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.
Input Specification:
The first line contains integer *n* (1<=β€<=*n*<=β€<=105).
The next line contains *n* integers *t**i* (1<=β€<=*t**i*<=β€<=109), separated by spaces.
Output Specification:
Print a single number β the maximum number of not disappointed people in the queue.
Demo Input:
['5\n15 2 1 5 3\n']
Demo Output:
['4\n']
Note:
Value 4 is achieved at such an arrangement, for example: 1,β2,β3,β5,β15. Thus, you can make everything feel not disappointed except for the person with time 5. | 656 |
Title: Phillip and Trains
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
The mobile application store has a new game called "Subway Roller".
The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and *n* columns. At the beginning of the game the hero is in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of two or more neighbouring cells in some row of the field.
All trains are moving from right to left at a speed of two cells per second, and the hero runs from left to right at the speed of one cell per second. For simplicity, the game is implemented so that the hero and the trains move in turns. First, the hero moves one cell to the right, then one square up or down, or stays idle. Then all the trains move twice simultaneously one cell to the left. Thus, in one move, Philip definitely makes a move to the right and can move up or down. If at any point, Philip is in the same cell with a train, he loses. If the train reaches the left column, it continues to move as before, leaving the tunnel.
Your task is to answer the question whether there is a sequence of movements of Philip, such that he would be able to get to the rightmost column.
Input Specification:
Each test contains from one to ten sets of the input data. The first line of the test contains a single integer *t* (1<=β€<=*t*<=β€<=10 for pretests and tests or *t*<==<=1 for hacks; see the Notes section for details) β the number of sets.
Then follows the description of *t* sets of the input data.
The first line of the description of each set contains two integers *n*,<=*k* (2<=β€<=*n*<=β€<=100,<=1<=β€<=*k*<=β€<=26) β the number of columns on the field and the number of trains. Each of the following three lines contains the sequence of *n* character, representing the row of the field where the game is on. Philip's initial position is marked as 's', he is in the leftmost column. Each of the *k* trains is marked by some sequence of identical uppercase letters of the English alphabet, located in one line. Distinct trains are represented by distinct letters. Character '.' represents an empty cell, that is, the cell that doesn't contain either Philip or the trains.
Output Specification:
For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise.
Demo Input:
['2\n16 4\n...AAAAA........\ns.BBB......CCCCC\n........DDDDD...\n16 4\n...AAAAA........\ns.BBB....CCCCC..\n.......DDDDD....\n', '2\n10 4\ns.ZZ......\n.....AAABB\n.YYYYYY...\n10 4\ns.ZZ......\n....AAAABB\n.YYYYYY...\n']
Demo Output:
['YES\nNO\n', 'YES\nNO\n']
Note:
In the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip's path, so he can go straight to the end of the tunnel.
Note that in this problem the challenges are restricted to tests that contain only one testset. | 657 |
Title: Hacker, pack your bags!
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly *x* days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that *n* vouchers left. *i*-th voucher is characterized by three integers *l**i*, *r**i*, *cost**i* β day of departure from ViΔkopolis, day of arriving back in ViΔkopolis and cost of the voucher correspondingly. The duration of the *i*-th voucher is a value *r**i*<=-<=*l**i*<=+<=1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers *i* and *j* (*i*<=β <=*j*) so that they don't intersect, sum of their durations is exactly *x* and their total cost is as minimal as possible. Two vouchers *i* and *j* don't intersect if only at least one of the following conditions is fulfilled: *r**i*<=<<=*l**j* or *r**j*<=<<=*l**i*.
Help Leha to choose the necessary vouchers!
Input Specification:
The first line contains two integers *n* and *x* (2<=β€<=*n*,<=*x*<=β€<=2Β·105) β the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.
Each of the next *n* lines contains three integers *l**i*, *r**i* and *cost**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=2Β·105,<=1<=β€<=*cost**i*<=β€<=109) β description of the voucher.
Output Specification:
Print a single integer β a minimal amount of money that Leha will spend, or print <=-<=1 if it's impossible to choose two disjoint vouchers with the total duration exactly *x*.
Demo Input:
['4 5\n1 3 4\n1 2 5\n5 6 1\n1 2 4\n', '3 2\n4 6 3\n2 4 1\n3 5 4\n']
Demo Output:
['5\n', '-1\n']
Note:
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3β-β1β+β1)β+β(6β-β5β+β1)β=β5 and the total cost will be 4β+β1β=β5.
In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2. | 658 |
Title: Spongebob and Squares
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly *x* distinct squares in the table consisting of *n* rows and *m* columns. For example, in a 3<=Γ<=5 table there are 15 squares with side one, 8 squares with side two and 3 squares with side three. The total number of distinct squares in a 3<=Γ<=5 table is 15<=+<=8<=+<=3<==<=26.
Input Specification:
The first line of the input contains a single integer *x* (1<=β€<=*x*<=β€<=1018)Β β the number of squares inside the tables Spongebob is interested in.
Output Specification:
First print a single integer *k*Β β the number of tables with exactly *x* distinct squares inside.
Then print *k* pairs of integers describing the tables. Print the pairs in the order of increasing *n*, and in case of equalityΒ β in the order of increasing *m*.
Demo Input:
['26\n', '2\n', '8\n']
Demo Output:
['6\n1 26\n2 9\n3 5\n5 3\n9 2\n26 1\n', '2\n1 2\n2 1\n', '4\n1 8\n2 3\n3 2\n8 1\n']
Note:
In a 1βΓβ2 table there are 2 1βΓβ1 squares. So, 2 distinct squares in total.
In a 2βΓβ3 table there are 6 1βΓβ1 squares and 2 2βΓβ2 squares. That is equal to 8 squares in total. | 659 |
Title: ΠΡΠΎΠΊΠ°Ρ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ΠΎΠ²
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
ΠΠ°ΠΊ ΠΈΠ·Π²Π΅ΡΡΠ½ΠΎ, Π² ΡΠ΅ΠΏΠ»ΡΡ ΠΏΠΎΠ³ΠΎΠ΄Ρ ΠΌΠ½ΠΎΠ³ΠΈΠ΅ ΠΆΠΈΡΠ΅Π»ΠΈ ΠΊΡΡΠΏΠ½ΡΡ
Π³ΠΎΡΠΎΠ΄ΠΎΠ² ΠΏΠΎΠ»ΡΠ·ΡΡΡΡΡ ΡΠ΅ΡΠ²ΠΈΡΠ°ΠΌΠΈ Π³ΠΎΡΠΎΠ΄ΡΠΊΠΎΠ³ΠΎ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠ°. ΠΠΎΡ ΠΈ ΠΡΠΊΠ°Π΄ΠΈΠΉ ΡΠ΅Π³ΠΎΠ΄Π½Ρ Π±ΡΠ΄Π΅Ρ Π΄ΠΎΠ±ΠΈΡΠ°ΡΡΡΡ ΠΎΡ ΡΠΊΠΎΠ»Ρ Π΄ΠΎ Π΄ΠΎΠΌΠ°, ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡ Π³ΠΎΡΠΎΠ΄ΡΠΊΠΈΠ΅ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄Ρ.
Π¨ΠΊΠΎΠ»Π° ΠΈ Π΄ΠΎΠΌ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ Π½Π° ΠΎΠ΄Π½ΠΎΠΉ ΠΏΡΡΠΌΠΎΠΉ ΡΠ»ΠΈΡΠ΅, ΠΊΡΠΎΠΌΠ΅ ΡΠΎΠ³ΠΎ, Π½Π° ΡΠΎΠΉ ΠΆΠ΅ ΡΠ»ΠΈΡΠ΅ Π΅ΡΡΡ *n* ΡΠΎΡΠ΅ΠΊ, Π³Π΄Π΅ ΠΌΠΎΠΆΠ½ΠΎ Π²Π·ΡΡΡ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ Π² ΠΏΡΠΎΠΊΠ°Ρ ΠΈΠ»ΠΈ ΡΠ΄Π°ΡΡ Π΅Π³ΠΎ. ΠΠ΅ΡΠ²ΡΠΉ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°Ρ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΡΠΎΡΠΊΠ΅ *x*1 ΠΊΠΈΠ»ΠΎΠΌΠ΅ΡΡΠΎΠ² Π²Π΄ΠΎΠ»Ρ ΡΠ»ΠΈΡΡ, Π²ΡΠΎΡΠΎΠΉΒ β Π² ΡΠΎΡΠΊΠ΅ *x*2 ΠΈ ΡΠ°ΠΊ Π΄Π°Π»Π΅Π΅, *n*-ΠΉ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°Ρ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΡΠΎΡΠΊΠ΅ *x**n*. Π¨ΠΊΠΎΠ»Π° ΠΡΠΊΠ°Π΄ΠΈΡ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΡΠΎΡΠΊΠ΅ *x*1 (ΡΠΎ Π΅ΡΡΡ ΡΠ°ΠΌ ΠΆΠ΅, Π³Π΄Π΅ ΠΈ ΠΏΠ΅ΡΠ²ΡΠΉ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°Ρ), Π° Π΄ΠΎΠΌΒ β Π² ΡΠΎΡΠΊΠ΅ *x**n* (ΡΠΎ Π΅ΡΡΡ ΡΠ°ΠΌ ΠΆΠ΅, Π³Π΄Π΅ ΠΈ *n*-ΠΉ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°Ρ). ΠΠ·Π²Π΅ΡΡΠ½ΠΎ, ΡΡΠΎ *x**i*<=<<=*x**i*<=+<=1 Π΄Π»Ρ Π²ΡΠ΅Ρ
1<=β€<=*i*<=<<=*n*.
Π‘ΠΎΠ³Π»Π°ΡΠ½ΠΎ ΠΏΡΠ°Π²ΠΈΠ»Π°ΠΌ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠ°, ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΠ°ΡΡ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ Π² ΠΏΡΠΎΠΊΠ°Ρ ΡΠΎΠ»ΡΠΊΠΎ Π½Π° ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½Π½ΠΎΠ΅ Π²ΡΠ΅ΠΌΡ, ΠΏΠΎΡΠ»Π΅ ΡΡΠΎΠ³ΠΎ ΠΎΠ½ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΠΎ Π²Π΅ΡΠ½ΡΡΡ Π΅Π³ΠΎ Π² ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· ΡΠΎΡΠ΅ΠΊ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠ°, ΠΎΠ΄Π½Π°ΠΊΠΎ, ΠΎΠ½ ΡΡΡ ΠΆΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π²Π·ΡΡΡ Π½ΠΎΠ²ΡΠΉ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄, ΠΈ ΠΎΡΡΡΠ΅Ρ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ ΠΏΠΎΠΉΠ΄Π΅Ρ Π·Π°Π½ΠΎΠ²ΠΎ. ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΠ°ΡΡ Π½Π΅ Π±ΠΎΠ»Π΅Π΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄Π° Π² ΠΏΡΠΎΠΊΠ°Ρ ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ. ΠΡΠ»ΠΈ ΠΡΠΊΠ°Π΄ΠΈΠΉ ΡΠ΅ΡΠ°Π΅Ρ Π²Π·ΡΡΡ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ Π² ΠΊΠ°ΠΊΠΎΠΉ-ΡΠΎ ΡΠΎΡΠΊΠ΅ ΠΏΡΠΎΠΊΠ°ΡΠ°, ΡΠΎ ΠΎΠ½ ΡΠ΄Π°ΡΡ ΡΠΎΡ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄, Π½Π° ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΎΠ½ Π΄ΠΎ Π½Π΅Π³ΠΎ Π΄ΠΎΠ΅Ρ
Π°Π», Π±Π΅ΡΡΡ ΡΠΎΠ²Π½ΠΎ ΠΎΠ΄ΠΈΠ½ Π½ΠΎΠ²ΡΠΉ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ ΠΈ ΠΏΡΠΎΠ΄ΠΎΠ»ΠΆΠ°Π΅Ρ Π½Π° Π½ΡΠΌ ΡΠ²ΠΎΡ Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΠ΅.
ΠΠ° ΠΎΡΠ²Π΅Π΄Π΅Π½Π½ΠΎΠ΅ Π²ΡΠ΅ΠΌΡ, Π½Π΅Π·Π°Π²ΠΈΡΠΈΠΌΠΎ ΠΎΡ Π²ΡΠ±ΡΠ°Π½Π½ΠΎΠ³ΠΎ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄Π°, ΠΡΠΊΠ°Π΄ΠΈΠΉ ΡΡΠΏΠ΅Π²Π°Π΅Ρ ΠΏΡΠΎΠ΅Ρ
Π°ΡΡ Π½Π΅ Π±ΠΎΠ»ΡΡΠ΅ *k* ΠΊΠΈΠ»ΠΎΠΌΠ΅ΡΡΠΎΠ² Π²Π΄ΠΎΠ»Ρ ΡΠ»ΠΈΡΡ.
ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΠ΅, ΡΠΌΠΎΠΆΠ΅Ρ Π»ΠΈ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π΄ΠΎΠ΅Ρ
Π°ΡΡ Π½Π° Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄Π°Ρ
ΠΎΡ ΡΠΊΠΎΠ»Ρ Π΄ΠΎ Π΄ΠΎΠΌΠ°, ΠΈ Π΅ΡΠ»ΠΈ Π΄Π°, ΡΠΎ ΠΊΠ°ΠΊΠΎΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΡΠ°Π· Π΅ΠΌΡ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ Π±ΡΠ΄Π΅Ρ Π²Π·ΡΡΡ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ Π² ΠΏΡΠΎΠΊΠ°Ρ, Π²ΠΊΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ²ΡΠΉ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄? Π£ΡΡΠΈΡΠ΅, ΡΡΠΎ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π½Π΅ Π½Π°ΠΌΠ΅ΡΠ΅Π½ ΡΠ΅Π³ΠΎΠ΄Π½Ρ Ρ
ΠΎΠ΄ΠΈΡΡ ΠΏΠ΅ΡΠΊΠΎΠΌ.
Input Specification:
Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ ΡΠ»Π΅Π΄ΡΡΡ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° *n* ΠΈ *k* (2<=β€<=*n*<=β€<=1<=000, 1<=β€<=*k*<=β€<=100<=000) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠΎΠ² ΠΈ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΡΠ°ΡΡΡΠΎΡΠ½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΎΠ΅Ρ
Π°ΡΡ Π½Π° ΠΎΠ΄Π½ΠΎΠΌ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄Π΅.
Π ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΉ ΡΡΡΠΎΠΊΠ΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΡΡΡ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» *x*1,<=*x*2,<=...,<=*x**n* (0<=β€<=*x*1<=<<=*x*2<=<<=...<=<<=*x**n*<=β€<=100<=000) β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ ΡΠΎΡΠ΅ΠΊ, Π² ΠΊΠΎΡΠΎΡΡΡ
Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΡ. ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠΎΠ² Π·Π°Π΄Π°Π½Ρ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ Π²ΠΎΠ·ΡΠ°ΡΡΠ°Π½ΠΈΡ.
Output Specification:
ΠΡΠ»ΠΈ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π½Π΅ ΡΠΌΠΎΠΆΠ΅Ρ Π΄ΠΎΠ±ΡΠ°ΡΡΡΡ ΠΎΡ ΡΠΊΠΎΠ»Ρ Π΄ΠΎ Π΄ΠΎΠΌΠ° ΡΠΎΠ»ΡΠΊΠΎ Π½Π° Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄Π°Ρ
, Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ -1. Π ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅, Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ΠΎΠ², ΠΊΠΎΡΠΎΡΡΠ΅ ΠΡΠΊΠ°Π΄ΠΈΡ Π½ΡΠΆΠ½ΠΎ Π²Π·ΡΡΡ Π² ΡΠΎΡΠΊΠ°Ρ
ΠΏΡΠΎΠΊΠ°ΡΠ°.
Demo Input:
['4 4\n3 6 8 10\n', '2 9\n10 20\n', '12 3\n4 6 7 9 10 11 13 15 17 18 20 21\n']
Demo Output:
['2\n', '-1\n', '6\n']
Note:
Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π·ΡΡΡ ΠΏΠ΅ΡΠ²ΡΠΉ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄ Π² ΠΏΠ΅ΡΠ²ΠΎΠΌ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠ΅ ΠΈ Π΄ΠΎΠ΅Ρ
Π°ΡΡ Π½Π° Π½ΡΠΌ Π΄ΠΎ Π²ΡΠΎΡΠΎΠ³ΠΎ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠ°. ΠΠΎ Π²ΡΠΎΡΠΎΠΌ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠ΅ ΠΎΠ½ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π·ΡΡΡ Π½ΠΎΠ²ΡΠΉ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄, Π½Π° ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΎΠ½ ΡΠΌΠΎΠΆΠ΅Ρ Π΄ΠΎΠ±ΡΠ°ΡΡΡΡ Π΄ΠΎ ΡΠ΅ΡΠ²Π΅ΡΡΠΎΠ³ΠΎ Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠ°, ΡΡΠ΄ΠΎΠΌ Ρ ΠΊΠΎΡΠΎΡΡΠΌ ΠΈ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π΅Π³ΠΎ Π΄ΠΎΠΌ. ΠΠΎΡΡΠΎΠΌΡ ΠΡΠΊΠ°Π΄ΠΈΡ Π½ΡΠΆΠ½ΠΎ Π²ΡΠ΅Π³ΠΎ Π΄Π²Π° Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄Π°, ΡΡΠΎΠ±Ρ Π΄ΠΎΠ±ΡΠ°ΡΡΡΡ ΠΎΡ ΡΠΊΠΎΠ»Ρ Π΄ΠΎ Π΄ΠΎΠΌΠ°.
ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π²ΡΠ΅Π³ΠΎ Π΄Π²Π° Π²Π΅Π»ΠΎΠΏΡΠΎΠΊΠ°ΡΠ°, ΡΠ°ΡΡΡΠΎΡΠ½ΠΈΠ΅ ΠΌΠ΅ΠΆΠ΄Ρ ΠΊΠΎΡΠΎΡΡΠΌΠΈ 10. ΠΠΎ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΡΠ°ΡΡΡΠΎΡΠ½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠΎΠ΅Ρ
Π°ΡΡ Π½Π° ΠΎΠ΄Π½ΠΎΠΌ Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄Π΅, ΡΠ°Π²Π½ΠΎ 9. ΠΠΎΡΡΠΎΠΌΡ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π½Π΅ ΡΠΌΠΎΠΆΠ΅Ρ Π΄ΠΎΠ±ΡΠ°ΡΡΡΡ ΠΎΡ ΡΠΊΠΎΠ»Ρ Π΄ΠΎ Π΄ΠΎΠΌΠ° ΡΠΎΠ»ΡΠΊΠΎ Π½Π° Π²Π΅Π»ΠΎΡΠΈΠΏΠ΅Π΄Π°Ρ
. | 660 |
Title: Product Sum
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special.
You are given an array *a* of length *n*. The characteristic of this array is the value Β β the sum of the products of the values *a**i* by *i*. One may perform the following operation exactly once: pick some element of the array and move to any position. In particular, it's allowed to move the element to the beginning or to the end of the array. Also, it's allowed to put it back to the initial position. The goal is to get the array with the maximum possible value of characteristic.
Input Specification:
The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=200<=000)Β β the size of the array *a*.
The second line contains *n* integers *a**i* (1<=β€<=*i*<=β€<=*n*, |*a**i*|<=β€<=1<=000<=000)Β β the elements of the array *a*.
Output Specification:
Print a single integer β the maximum possible value of characteristic of *a* that can be obtained by performing no more than one move.
Demo Input:
['4\n4 3 2 5\n', '5\n1 1 2 7 1\n', '3\n1 1 2\n']
Demo Output:
['39', '49', '9']
Note:
In the first sample, one may pick the first element and place it before the third (before 5). Thus, the answer will be 3Β·1β+β2Β·2β+β4Β·3β+β5Β·4β=β39.
In the second sample, one may pick the fifth element of the array and place it before the third. The answer will be 1Β·1β+β1Β·2β+β1Β·3β+β2Β·4β+β7Β·5β=β49. | 661 |
Title: Policeman and a Tree
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given a tree (a connected non-oriented graph without cycles) with vertices numbered from 1 to *n*, and the length of the *i*-th edge is *w**i*. In the vertex *s* there is a policeman, in the vertices *x*1,<=*x*2,<=...,<=*x**m* (*x**j*<=β <=*s*) *m* criminals are located.
The policeman can walk along the edges with speed 1, the criminals can move with arbitrary large speed. If a criminal at some moment is at the same point as the policeman, he instantly gets caught by the policeman. Determine the time needed for the policeman to catch all criminals, assuming everybody behaves optimally (i.e. the criminals maximize that time, the policeman minimizes that time). Everybody knows positions of everybody else at any moment of time.
Input Specification:
The first line contains single integer *n* (1<=β€<=*n*<=β€<=50)Β β the number of vertices in the tree. The next *n*<=-<=1 lines contain three integers each: *u**i*, *v**i*, *w**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, 1<=β€<=*w**i*<=β€<=50) denoting edges and their lengths. It is guaranteed that the given graph is a tree.
The next line contains single integer *s* (1<=β€<=*s*<=β€<=*n*)Β β the number of vertex where the policeman starts.
The next line contains single integer *m* (1<=β€<=*m*<=β€<=50)Β β the number of criminals. The next line contains *m* integers *x*1,<=*x*2,<=...,<=*x**m* (1<=β€<=*x**j*<=β€<=*n*, *x**j*<=β <=*s*)Β β the number of vertices where the criminals are located. *x**j* are not necessarily distinct.
Output Specification:
If the policeman can't catch criminals, print single line "Terrorists win" (without quotes).
Otherwise, print single integerΒ β the time needed to catch all criminals.
Demo Input:
['4\n1 2 2\n1 3 1\n1 4 1\n2\n4\n3 1 4 1\n', '6\n1 2 3\n2 3 5\n3 4 1\n3 5 4\n2 6 3\n2\n3\n1 3 5\n']
Demo Output:
['8\n', '21\n']
Note:
In the first example one of the optimal scenarios is the following. The criminal number 2 moves to vertex 3, the criminal 4Β β to vertex 4. The policeman goes to vertex 4 and catches two criminals. After that the criminal number 1 moves to the vertex 2. The policeman goes to vertex 3 and catches criminal 2, then goes to the vertex 2 and catches the remaining criminal. | 662 |
Title: Predict Outcome of the Game
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
There are *n* games in a football tournament. Three teams are participating in it. Currently *k* games had already been played.
You are an avid football fan, but recently you missed the whole *k* games. Fortunately, you remember a guess of your friend for these *k* games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be *d*1 and that of between second and third team will be *d*2.
You don't want any of team win the tournament, that is each team should have the same number of wins after *n* games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss.
Input Specification:
The first line of the input contains a single integer corresponding to number of test cases *t* (1<=β€<=*t*<=β€<=105).
Each of the next *t* lines will contain four space-separated integers *n*,<=*k*,<=*d*1,<=*d*2 (1<=β€<=*n*<=β€<=1012;Β 0<=β€<=*k*<=β€<=*n*;Β 0<=β€<=*d*1,<=*d*2<=β€<=*k*) β data for the current test case.
Output Specification:
For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).
Demo Input:
['5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 3 3 0\n3 3 3 2\n']
Demo Output:
['yes\nyes\nyes\nno\nno\n']
Note:
Sample 1. There has not been any match up to now (*k*β=β0,β*d*<sub class="lower-index">1</sub>β=β0,β*d*<sub class="lower-index">2</sub>β=β0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (*k*β=β3). As *d*<sub class="lower-index">1</sub>β=β0 and *d*<sub class="lower-index">2</sub>β=β0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and *d*<sub class="lower-index">1</sub>β=β1,β*d*<sub class="lower-index">2</sub>β=β0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). | 663 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
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 .
Input Specification:
You are given a single integer *n* (1<=β€<=*n*<=β€<=150)Β β the number of steps of the algorithm you need to reach.
Output Specification:
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.
Demo Input:
['1\n', '2\n']
Demo Output:
['1\n0 1\n0\n1\n', '2\n-1 0 1\n1\n0 1\n']
Note:
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. | 664 |
Title: Bug in Code
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the *n* coders on the meeting said: 'I know for sure that either *x* or *y* did it!'
The head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders' opinions. That's why the head wants to make such a choice that at least *p* of *n* coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects?
Note that even if some coder was chosen as a suspect, he can agree with the head's choice if he named the other chosen coder at the meeting.
Input Specification:
The first line contains integers *n* and *p* (3<=β€<=*n*<=β€<=3Β·105;Β 0<=β€<=*p*<=β€<=*n*) β the number of coders in the F company and the minimum number of agreed people.
Each of the next *n* lines contains two integers *x**i*, *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*) β the numbers of coders named by the *i*-th coder. It is guaranteed that *x**i*<=β <=*i*,<=Β *y**i*<=β <=*i*,<=Β *x**i*<=β <=*y**i*.
Output Specification:
Print a single integer β the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets (1,<=2) and (2,<=1) are considered identical.
Demo Input:
['4 2\n2 3\n1 4\n1 4\n2 1\n', '8 6\n5 6\n5 7\n5 8\n6 2\n2 1\n7 3\n1 3\n1 4\n']
Demo Output:
['6\n', '1\n']
Note:
none | 665 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
DZY loves Fast Fourier Transformation, and he enjoys using it.
Fast Fourier Transformation is an algorithm used to calculate convolution. Specifically, if *a*, *b* and *c* are sequences with length *n*, which are indexed from 0 to *n*<=-<=1, and
We can calculate *c* fast using Fast Fourier Transformation.
DZY made a little change on this formula. Now
To make things easier, *a* is a permutation of integers from 1 to *n*, and *b* is a sequence only containing 0 and 1. Given *a* and *b*, DZY needs your help to calculate *c*.
Because he is naughty, DZY provides a special way to get *a* and *b*. What you need is only three integers *n*, *d*, *x*. After getting them, use the code below to generate *a* and *b*.
Operation x % y denotes remainder after division *x* by *y*. Function swap(x, y) swaps two values *x* and *y*.
Input Specification:
The only line of input contains three space-separated integers *n*,<=*d*,<=*x*Β (1<=β€<=*d*<=β€<=*n*<=β€<=100000;Β 0<=β€<=*x*<=β€<=1000000006). Because DZY is naughty, *x* can't be equal to 27777500.
Output Specification:
Output *n* lines, the *i*-th line should contain an integer *c**i*<=-<=1.
Demo Input:
['3 1 1\n', '5 4 2\n', '5 4 3\n']
Demo Output:
['1\n3\n2\n', '2\n2\n4\n5\n5\n', '5\n5\n5\n5\n4\n']
Note:
In the first sample, *a* is [1 3 2], *b* is [1 0 0], so *c*<sub class="lower-index">0</sub>β=β*max*(1Β·1)β=β1, *c*<sub class="lower-index">1</sub>β=β*max*(1Β·0,β3Β·1)β=β3, *c*<sub class="lower-index">2</sub>β=β*max*(1Β·0,β3Β·0,β2Β·1)β=β2.
In the second sample, *a* is [2 1 4 5 3], *b* is [1 1 1 0 1].
In the third sample, *a* is [5 2 1 4 3], *b* is [1 1 1 1 0]. | 666 |
Title: Wizards and Bets
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
In some country live wizards. They like to make weird bets.
Two wizards draw an acyclic directed graph with *n* vertices and *m* edges (the graph's vertices are numbered from 1 to *n*). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink and the source simultaneously. In the wizards' graph the number of the sinks and the sources is the same.
Wizards numbered the sources in the order of increasing numbers of the vertices from 1 to *k*. The sinks are numbered from 1 to *k* in the similar way.
To make a bet, they, as are real wizards, cast a spell, which selects a set of *k* paths from all sources to the sinks in such a way that no two paths intersect at the vertices. In this case, each sink has exactly one path going to it from exactly one source. Let's suppose that the *i*-th sink has a path going to it from the *a**i*'s source. Then let's call pair (*i*,<=*j*) an inversion if *i*<=<<=*j* and *a**i*<=><=*a**j*. If the number of inversions among all possible pairs (*i*,<=*j*), such that (1<=β€<=*i*<=<<=*j*<=β€<=*k*), is even, then the first wizard wins (the second one gives him one magic coin). Otherwise, the second wizard wins (he gets one magic coin from the first one).
Our wizards are captured with feverish excitement, so they kept choosing new paths again and again for so long that eventually they have chosen every possible set of paths for exactly once. The two sets of non-intersecting pathes are considered to be different, if and only if there is an edge, which lies at some path in one set and doesn't lie at any path of another set. To check their notes, they asked you to count the total winnings of the first player for all possible sets of paths modulo a prime number *p*.
Input Specification:
The first line contains three space-separated integers *n*, *m*, *p* (1<=β€<=*n*<=β€<=600, 0<=β€<=*m*<=β€<=105, 2<=β€<=*p*<=β€<=109<=+<=7). It is guaranteed that *p* is prime number.
Next *m* lines contain edges of the graph. Each line contains a pair of space-separated integers, *a**i*Β *b**i* β an edge from vertex *a**i* to vertex *b**i*. It is guaranteed that the graph is acyclic and that the graph contains the same number of sources and sinks. Please note that the graph can have multiple edges.
Output Specification:
Print the answer to the problem β the total winnings of the first player modulo a prime number *p*. Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample).
Demo Input:
['4 2 1000003\n1 3\n2 4\n', '4 2 1000003\n4 1\n3 2\n', '4 4 1000003\n2 1\n2 4\n3 1\n3 4\n', '6 5 1000003\n1 4\n1 5\n1 6\n2 6\n3 6\n', '5 2 1000003\n5 1\n3 4\n']
Demo Output:
['1\n', '1000002\n', '0\n', '0\n', '1\n']
Note:
In the first sample, there is exactly one set of paths β <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4c100300071b70505c9eb8fd6f97456068e38f93.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The number of inversions is 0, which is an even number. Therefore, the first wizard gets 1 coin.
In the second sample there is exactly one set of paths β <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/83cf8c3e6763396a8ead42b4762ff62e4b01f917.png" style="max-width: 100.0%;max-height: 100.0%;"/>. There is exactly one inversion. Therefore, the first wizard gets -1 coin. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fa579688f149cd6ab8f4c3c99c47c5a65e52e87c.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the third sample, there are two sets of paths, which are counted with opposite signs.
In the fourth sample there are no set of paths at all.
In the fifth sample, there are three sources β the vertices with the numbers (2, 3, 5) and three sinks β the vertices with numbers (1, 2, 4). For a single set of paths <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5ef1a2c8d476f3fea4b49124c0c23f371be35bb9.png" style="max-width: 100.0%;max-height: 100.0%;"/> are 2 inversions, that is, their number is even. | 667 |
Title: Dasha and cyclic table
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Dasha is fond of challenging puzzles: Rubik's Cube 3<=Γ<=3<=Γ<=3, 4<=Γ<=4<=Γ<=4, 5<=Γ<=5<=Γ<=5 and so on. This time she has a cyclic table of size *n*<=Γ<=*m*, and each cell of the table contains a lowercase English letter. Each cell has coordinates (*i*,<=*j*) (0<=β€<=*i*<=<<=*n*, 0<=β€<=*j*<=<<=*m*). The table is cyclic means that to the right of cell (*i*,<=*j*) there is the cell , and to the down there is the cell .
Dasha has a pattern as well. A pattern is a non-cyclic table of size *r*<=Γ<=*c*. Each cell is either a lowercase English letter or a question mark. Each cell has coordinates (*i*,<=*j*) (0<=β€<=*i*<=<<=*r*, 0<=β€<=*j*<=<<=*c*).
The goal of the puzzle is to find all the appearance positions of the pattern in the cyclic table.
We say that the cell (*i*,<=*j*) of cyclic table is an appearance position, if for every pair (*x*,<=*y*) such that 0<=β€<=*x*<=<<=*r* and 0<=β€<=*y*<=<<=*c* one of the following conditions holds:
- There is a question mark in the cell (*x*,<=*y*) of the pattern, or - The cell of the cyclic table equals to the cell (*x*,<=*y*) of the pattern.
Dasha solved this puzzle in no time, as well as all the others she ever tried. Can you solve it?.
Input Specification:
The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=400)Β β the cyclic table sizes.
Each of the next *n* lines contains a string of *m* lowercase English charactersΒ β the description of the cyclic table.
The next line contains two integers *r* and *c* (1<=β€<=*r*,<=*c*<=β€<=400)Β β the sizes of the pattern.
Each of the next *r* lines contains a string of *c* lowercase English letter and/or characters '?'Β β the description of the pattern.
Output Specification:
Print *n* lines. Each of the *n* lines should contain *m* characters. Each of the characters should equal '0' or '1'.
The *j*-th character of the *i*-th (0-indexed) line should be equal to '1', in case the cell (*i*,<=*j*) is an appearance position, otherwise it should be equal to '0'.
Demo Input:
['5 7\nqcezchs\nhhedywq\nwikywqy\nqckrqzt\nbqexcxz\n3 2\n??\nyw\n?q\n', '10 10\nfwtoayylhw\nyyaryyjawr\nywrdzwhscy\nhnsyyxiphn\nbnjwzyyjvo\nkkjgseenwn\ngvmiflpcsy\nlxvkwrobwu\nwyybbcocyy\nyysijsvqry\n2 2\n??\nyy\n', '8 6\nibrgxl\nxqdcsg\nokbcgi\ntvpetc\nxgxxig\nigghzo\nlmlaza\ngpswzv\n1 4\ngx??\n']
Demo Output:
['0000100\n0001001\n0000000\n0000000\n0000000\n', '1000100000\n0000000001\n0001000000\n0000010000\n0000000000\n0000000000\n0000000000\n0100000010\n1000000001\n0000010000\n', '000100\n000001\n000000\n000000\n010001\n000000\n000000\n000000\n']
Note:
none | 668 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
On the way home, Karen decided to stop by the supermarket to buy some groceries.
She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to *b* dollars.
The supermarket sells *n* goods. The *i*-th good can be bought for *c**i* dollars. Of course, each good can only be bought once.
Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given *n* coupons. If Karen purchases the *i*-th good, she can use the *i*-th coupon to decrease its price by *d**i*. Of course, a coupon cannot be used without buying the corresponding good.
There is, however, a constraint with the coupons. For all *i*<=β₯<=2, in order to use the *i*-th coupon, Karen must also use the *x**i*-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).
Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget *b*?
Input Specification:
The first line of input contains two integers *n* and *b* (1<=β€<=*n*<=β€<=5000, 1<=β€<=*b*<=β€<=109), the number of goods in the store and the amount of money Karen has, respectively.
The next *n* lines describe the items. Specifically:
- The *i*-th line among these starts with two integers, *c**i* and *d**i* (1<=β€<=*d**i*<=<<=*c**i*<=β€<=109), the price of the *i*-th good and the discount when using the coupon for the *i*-th good, respectively. - If *i*<=β₯<=2, this is followed by another integer, *x**i* (1<=β€<=*x**i*<=<<=*i*), denoting that the *x**i*-th coupon must also be used before this coupon can be used.
Output Specification:
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
Demo Input:
['6 16\n10 9\n10 5 1\n12 2 1\n20 18 3\n10 2 3\n2 1 5\n', '5 10\n3 1\n3 1 1\n3 1 2\n3 1 3\n3 1 4\n']
Demo Output:
['4\n', '5\n']
Note:
In the first test case, Karen can purchase the following 4 items:
- Use the first coupon to buy the first item for 10β-β9β=β1 dollar. - Use the third coupon to buy the third item for 12β-β2β=β10 dollars. - Use the fourth coupon to buy the fourth item for 20β-β18β=β2 dollars. - Buy the sixth item for 2 dollars.
The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.
In the second test case, Karen has enough money to use all the coupons and purchase everything. | 669 |
Title: Felicity's Big Secret Revealed
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
The gym leaders were fascinated by the evolutions which took place at Felicity camp. So, they were curious to know about the secret behind evolving Pokemon.
The organizers of the camp gave the gym leaders a PokeBlock, a sequence of *n* ingredients. Each ingredient can be of type 0 or 1. Now the organizers told the gym leaders that to evolve a Pokemon of type *k* (*k*<=β₯<=2), they need to make a valid set of *k* cuts on the PokeBlock to get smaller blocks.
Suppose the given PokeBlock sequence is *b*0*b*1*b*2... *b**n*<=-<=1. You have a choice of making cuts at *n*<=+<=1 places, i.e., Before *b*0, between *b*0 and *b*1, between *b*1 and *b*2, ..., between *b**n*<=-<=2 and *b**n*<=-<=1, and after *b**n*<=-<=1.
The *n*<=+<=1 choices of making cuts are as follows (where a | denotes a possible cut):
Consider a sequence of *k* cuts. Now each pair of consecutive cuts will contain a binary string between them, formed from the ingredient types. The ingredients before the first cut and after the last cut are wasted, which is to say they are not considered. So there will be exactly *k*<=-<=1 such binary substrings. Every substring can be read as a binary number. Let *m* be the maximum number out of the obtained numbers. If all the obtained numbers are positive and the set of the obtained numbers contains all integers from 1 to *m*, then this set of cuts is said to be a valid set of cuts.
For example, suppose the given PokeBlock sequence is 101101001110 and we made 5 cuts in the following way:
So the 4 binary substrings obtained are: 11, 010, 01 and 1, which correspond to the numbers 3, 2, 1 and 1 respectively. Here *m*<==<=3, as it is the maximum value among the obtained numbers. And all the obtained numbers are positive and we have obtained all integers from 1 to *m*. Hence this set of cuts is a valid set of 5 cuts.
A Pokemon of type *k* will evolve only if the PokeBlock is cut using a valid set of *k* cuts. There can be many valid sets of the same size. Two valid sets of *k* cuts are considered different if there is a cut in one set which is not there in the other set.
Let *f*(*k*) denote the number of valid sets of *k* cuts. Find the value of . Since the value of *s* can be very large, output *s* modulo 109<=+<=7.
Input Specification:
The input consists of two lines. The first line consists an integer *n* (1<=β€<=*n*<=β€<=75)Β β the length of the PokeBlock. The next line contains the PokeBlock, a binary string of length *n*.
Output Specification:
Output a single integer, containing the answer to the problem, i.e., the value of *s* modulo 109<=+<=7.
Demo Input:
['4\n1011\n', '2\n10\n']
Demo Output:
['10\n', '1\n']
Note:
In the first sample, the sets of valid cuts are:
Size 2: |1|011, 1|01|1, 10|1|1, 101|1|.
Size 3: |1|01|1, |10|1|1, 10|1|1|, 1|01|1|.
Size 4: |10|1|1|, |1|01|1|.
Hence, *f*(2)β=β4, *f*(3)β=β4 and *f*(4)β=β2. So, the value of *s*β=β10.
In the second sample, the set of valid cuts is:
Size 2: |1|0.
Hence, *f*(2)β=β1 and *f*(3)β=β0. So, the value of *s*β=β1. | 670 |
Title: Clique in the Divisibility Graph
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
As you must know, the maximum clique problem in an arbitrary graph is *NP*-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} as follows. The vertices of the given graph are numbers from set *A*, and two numbers *a**i* and *a**j* (*i*<=β <=*j*) are connected by an edge if and only if either *a**i* is divisible by *a**j*, or *a**j* is divisible by *a**i*.
You are given a set of non-negative integers *A*. Determine the size of a maximum clique in a divisibility graph for set *A*.
Input Specification:
The first line contains integer *n* (1<=β€<=*n*<=β€<=106), that sets the size of set *A*.
The second line contains *n* distinct positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106) β elements of subset *A*. The numbers in the line follow in the ascending order.
Output Specification:
Print a single number β the maximum size of a clique in a divisibility graph for set *A*.
Demo Input:
['8\n3 4 6 8 10 18 21 24\n']
Demo Output:
['3\n']
Note:
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3,β6,β18}. A clique of a larger size doesn't exist in this graph. | 671 |
Title: Game Outcome
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Sherlock Holmes and Dr. Watson played some game on a checkered board *n*<=Γ<=*n* in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to count the number of winning squares. To determine if the particular square is winning you should do the following. Calculate the sum of all numbers on the squares that share this column (including the given square) and separately calculate the sum of all numbers on the squares that share this row (including the given square). A square is considered winning if the sum of the column numbers is strictly greater than the sum of the row numbers.
For instance, lets game was ended like is shown in the picture. Then the purple cell is winning, because the sum of its column numbers equals 8<=+<=3<=+<=6<=+<=7<==<=24, sum of its row numbers equals 9<=+<=5<=+<=3<=+<=2<==<=19, and 24<=><=19.
Input Specification:
The first line contains an integer *n* (1<=β€<=*n*<=β€<=30). Each of the following *n* lines contain *n* space-separated integers. The *j*-th number on the *i*-th line represents the number on the square that belongs to the *j*-th column and the *i*-th row on the board. All number on the board are integers from 1 to 100.
Output Specification:
Print the single number β the number of the winning squares.
Demo Input:
['1\n1\n', '2\n1 2\n3 4\n', '4\n5 7 8 4\n9 5 3 2\n1 6 6 4\n9 5 7 3\n']
Demo Output:
['0\n', '2\n', '6\n']
Note:
In the first example two upper squares are winning.
In the third example three left squares in the both middle rows are winning: | 672 |
Title: Equidistant String
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:
We will define the distance between two strings *s* and *t* of the same length consisting of digits zero and one as the number of positions *i*, such that *s**i* isn't equal to *t**i*.
As besides everything else Susie loves symmetry, she wants to find for two strings *s* and *t* of length *n* such string *p* of length *n*, that the distance from *p* to *s* was equal to the distance from *p* to *t*.
It's time for Susie to go to bed, help her find such string *p* or state that it is impossible.
Input Specification:
The first line contains string *s* of length *n*.
The second line contains string *t* of length *n*.
The length of string *n* is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.
Output Specification:
Print a string of length *n*, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes).
If there are multiple possible answers, print any of them.
Demo Input:
['0001\n1011\n', '000\n111\n']
Demo Output:
['0011\n', 'impossible\n']
Note:
In the first sample different answers are possible, namely β 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101. | 673 |
Title: Graph Reconstruction
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
I have an undirected graph consisting of *n* nodes, numbered 1 through *n*. Each node has at most two incident edges. For each pair of nodes, there is at most an edge connecting them. No edge connects a node to itself.
I would like to create a new graph in such a way that:
- The new graph consists of the same number of nodes and edges as the old graph. - The properties in the first paragraph still hold. - For each two nodes *u* and *v*, if there is an edge connecting them in the old graph, there is no edge connecting them in the new graph.
Help me construct the new graph, or tell me if it is impossible.
Input Specification:
The first line consists of two space-separated integers: *n* and *m* (1<=β€<=*m*<=β€<=*n*<=β€<=105), denoting the number of nodes and edges, respectively. Then *m* lines follow. Each of the *m* lines consists of two space-separated integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*;Β *u*<=β <=*v*), denoting an edge between nodes *u* and *v*.
Output Specification:
If it is not possible to construct a new graph with the mentioned properties, output a single line consisting of -1. Otherwise, output exactly *m* lines. Each line should contain a description of edge in the same way as used in the input format.
Demo Input:
['8 7\n1 2\n2 3\n4 5\n5 6\n6 8\n8 7\n7 4\n', '3 2\n1 2\n2 3\n', '5 4\n1 2\n2 3\n3 4\n4 1\n']
Demo Output:
['1 4\n4 6\n1 6\n2 7\n7 5\n8 5\n2 8\n', '-1\n', '1 3\n3 5\n5 2\n2 4\n']
Note:
The old graph of the first example:
<img class="tex-graphics" src="https://espresso.codeforces.com/1a5d4ab85ef86541ea9bea88ee537f6852ca2194.png" style="max-width: 100.0%;max-height: 100.0%;"/>
A possible new graph for the first example:
<img class="tex-graphics" src="https://espresso.codeforces.com/8a2d63a60d51967903043452c9d1fe4dd6385753.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In the second example, we cannot create any new graph.
The old graph of the third example:
<img class="tex-graphics" src="https://espresso.codeforces.com/22079249a5965faa550b830e5827cde2910342f3.png" style="max-width: 100.0%;max-height: 100.0%;"/>
A possible new graph for the third example:
<img class="tex-graphics" src="https://espresso.codeforces.com/69fb5a55e3d0dde42a4ba4131e82d463f782fe9e.png" style="max-width: 100.0%;max-height: 100.0%;"/> | 674 |
Title: Three Arrays
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
There are three arrays *a*, *b* and *c*. Each of them consists of *n* integers. SmallY wants to find three integers *u*, *v*, *w* (0<=β€<=*u*,<=*v*,<=*w*<=β€<=*n*) such that the following condition holds: each number that appears in the union of *a*, *b* and *c*, appears either in the first *u* elements of *a*, or in the first *v* elements of *b*, or in the first *w* elements of *c*. Of course, SmallY doesn't want to have huge numbers *u*, *v* and *w*, so she wants sum *u*<=+<=*v*<=+<=*w* to be as small as possible.
Please, help her to find the minimal possible sum of *u*<=+<=*v*<=+<=*w*.
Input Specification:
The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* β array *a*. The third line contains the description of array *b* in the same format. The fourth line contains the description of array *c* in the same format. The following constraint holds: 1<=β€<=*a**i*,<=*b**i*,<=*c**i*<=β€<=109.
Output Specification:
Print a single integer β the minimum possible sum of *u*<=+<=*v*<=+<=*w*.
Demo Input:
['3\n1 1 101\n1 2 1\n3 2 1\n', '5\n1 1 2 2 3\n2 2 4 3 3\n3 3 1 1 1\n']
Demo Output:
['5', '5']
Note:
In the first example you should choose *u*β=β3,β*v*β=β0,β*w*β=β2.
In the second example you should choose *u*β=β1,β*v*β=β3,β*w*β=β1. | 675 |
Title: Fingerprints
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits.
Some keys on the keypad have fingerprints. You believe the correct code is the longest not necessarily contiguous subsequence of the sequence you have that only contains digits with fingerprints on the corresponding keys. Find such code.
Input Specification:
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 10$) representing the number of digits in the sequence you have and the number of keys on the keypad that have fingerprints.
The next line contains $n$ distinct space-separated integers $x_1, x_2, \ldots, x_n$ ($0 \le x_i \le 9$) representing the sequence.
The next line contains $m$ distinct space-separated integers $y_1, y_2, \ldots, y_m$ ($0 \le y_i \le 9$) β the keys with fingerprints.
Output Specification:
In a single line print a space-separated sequence of integers representing the code. If the resulting sequence is empty, both printing nothing and printing a single line break is acceptable.
Demo Input:
['7 3\n3 5 7 1 6 2 8\n1 2 7\n', '4 4\n3 4 1 0\n0 1 7 9\n']
Demo Output:
['7 1 2\n', '1 0\n']
Note:
In the first example, the only digits with fingerprints are $1$, $2$ and $7$. All three of them appear in the sequence you know, $7$ first, then $1$ and then $2$. Therefore the output is 7 1 2. Note that the order is important, and shall be the same as the order in the original sequence.
In the second example digits $0$, $1$, $7$ and $9$ have fingerprints, however only $0$ and $1$ appear in the original sequence. $1$ appears earlier, so the output is 1 0. Again, the order is important. | 676 |
Title: Valera and Tubes
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Valera has got a rectangle table consisting of *n* rows and *m* columns. Valera numbered the table rows starting from one, from top to bottom and the columns β starting from one, from left to right. We will represent cell that is on the intersection of row *x* and column *y* by a pair of integers (*x*,<=*y*).
Valera wants to place exactly *k* tubes on his rectangle table. A tube is such sequence of table cells (*x*1,<=*y*1), (*x*2,<=*y*2), ..., (*x**r*,<=*y**r*), that:
- *r*<=β₯<=2; - for any integer *i* (1<=β€<=*i*<=β€<=*r*<=-<=1) the following equation |*x**i*<=-<=*x**i*<=+<=1|<=+<=|*y**i*<=-<=*y**i*<=+<=1|<==<=1 holds; - each table cell, which belongs to the tube, must occur exactly once in the sequence.
Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled:
- no pair of tubes has common cells; - each cell of the table belongs to some tube.
Help Valera to arrange *k* tubes on his rectangle table in a fancy manner.
Input Specification:
The first line contains three space-separated integers *n*,<=*m*,<=*k* (2<=β€<=*n*,<=*m*<=β€<=300; 2<=β€<=2*k*<=β€<=*n*Β·*m*) β the number of rows, the number of columns and the number of tubes, correspondingly.
Output Specification:
Print *k* lines. In the *i*-th line print the description of the *i*-th tube: first print integer *r**i* (the number of tube cells), then print 2*r**i* integers *x**i*1,<=*y**i*1,<=*x**i*2,<=*y**i*2,<=...,<=*x**ir**i*,<=*y**ir**i* (the sequence of table cells).
If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
Demo Input:
['3 3 3\n', '2 3 1\n']
Demo Output:
['3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3\n', '6 1 1 1 2 1 3 2 3 2 2 2 1\n']
Note:
Picture for the first sample:
Picture for the second sample: | 677 |
Title: Mahmoud and a Message
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Mahmoud wrote a message *s* of length *n*. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That's because this magical paper doesn't allow character number *i* in the English alphabet to be written on it in a string of length more than *a**i*. For example, if *a*1<==<=2 he can't write character 'a' on this paper in a string of length 3 or more. String "aa" is allowed while string "aaa" is not.
Mahmoud decided to split the message into some non-empty substrings so that he can write every substring on an independent magical paper and fulfill the condition. The sum of their lengths should be *n* and they shouldn't overlap. For example, if *a*1<==<=2 and he wants to send string "aaa", he can split it into "a" and "aa" and use 2 magical papers, or into "a", "a" and "a" and use 3 magical papers. He can't split it into "aa" and "aa" because the sum of their lengths is greater than *n*. He can split the message into single string if it fulfills the conditions.
A substring of string *s* is a string that consists of some consecutive characters from string *s*, strings "ab", "abc" and "b" are substrings of string "abc", while strings "acb" and "ac" are not. Any string is a substring of itself.
While Mahmoud was thinking of how to split the message, Ehab told him that there are many ways to split it. After that Mahmoud asked you three questions:
- How many ways are there to split the string into substrings such that every substring fulfills the condition of the magical paper, the sum of their lengths is *n* and they don't overlap? Compute the answer modulo 109<=+<=7. - What is the maximum length of a substring that can appear in some valid splitting? - What is the minimum number of substrings the message can be spit in?
Two ways are considered different, if the sets of split positions differ. For example, splitting "aa|a" and "a|aa" are considered different splittings of message "aaa".
Input Specification:
The first line contains an integer *n* (1<=β€<=*n*<=β€<=103) denoting the length of the message.
The second line contains the message *s* of length *n* that consists of lowercase English letters.
The third line contains 26 integers *a*1,<=*a*2,<=...,<=*a*26 (1<=β€<=*a**x*<=β€<=103)Β β the maximum lengths of substring each letter can appear in.
Output Specification:
Print three lines.
In the first line print the number of ways to split the message into substrings and fulfill the conditions mentioned in the problem modulo 109<=<=+<=<=7.
In the second line print the length of the longest substring over all the ways.
In the third line print the minimum number of substrings over all the ways.
Demo Input:
['3\naab\n2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n', '10\nabcdeabcde\n5 5 5 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n']
Demo Output:
['3\n2\n2\n', '401\n4\n3\n']
Note:
In the first example the three ways to split the message are:
- a|a|b - aa|b - a|ab
The longest substrings are "aa" and "ab" of length 2.
The minimum number of substrings is 2 in "a|ab" or "aa|b".
Notice that "aab" is not a possible splitting because the letter 'a' appears in a substring of length 3, while *a*<sub class="lower-index">1</sub>β=β2. | 678 |
Title: Petr and Book
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
One Sunday Petr went to a bookshop and bought a new book on sports programming. The book had exactly *n* pages.
Petr decided to start reading it starting from the next day, that is, from Monday. Petr's got a very tight schedule and for each day of the week he knows how many pages he will be able to read on that day. Some days are so busy that Petr will have no time to read whatsoever. However, we know that he will be able to read at least one page a week.
Assuming that Petr will not skip days and will read as much as he can every day, determine on which day of the week he will read the last page of the book.
Input Specification:
The first input line contains the single integer *n* (1<=β€<=*n*<=β€<=1000) β the number of pages in the book.
The second line contains seven non-negative space-separated integers that do not exceed 1000 β those integers represent how many pages Petr can read on Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday correspondingly. It is guaranteed that at least one of those numbers is larger than zero.
Output Specification:
Print a single number β the number of the day of the week, when Petr will finish reading the book. The days of the week are numbered starting with one in the natural order: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.
Demo Input:
['100\n15 20 20 15 10 30 45\n', '2\n1 0 0 0 0 0 0\n']
Demo Output:
['6\n', '1\n']
Note:
Note to the first sample:
By the end of Monday and therefore, by the beginning of Tuesday Petr has 85 pages left. He has 65 pages left by Wednesday, 45 by Thursday, 30 by Friday, 20 by Saturday and on Saturday Petr finishes reading the book (and he also has time to read 10 pages of something else).
Note to the second sample:
On Monday of the first week Petr will read the first page. On Monday of the second week Petr will read the second page and will finish reading the book. | 679 |
Title: Mini Metro
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
In a simplified version of a "Mini Metro" game, there is only one subway line, and all the trains go in the same direction. There are $n$ stations on the line, $a_i$ people are waiting for the train at the $i$-th station at the beginning of the game. The game starts at the beginning of the $0$-th hour. At the end of each hour (couple minutes before the end of the hour), $b_i$ people instantly arrive to the $i$-th station. If at some moment, the number of people at the $i$-th station is larger than $c_i$, you lose.
A player has several trains which he can appoint to some hours. The capacity of each train is $k$ passengers. In the middle of the appointed hour, the train goes from the $1$-st to the $n$-th station, taking as many people at each station as it can accommodate. A train can not take people from the $i$-th station if there are people at the $i-1$-th station.
If multiple trains are appointed to the same hour, their capacities are being added up and they are moving together.
The player wants to stay in the game for $t$ hours. Determine the minimum number of trains he will need for it.
Input Specification:
The first line contains three integers $n$, $t$, and $k$ ($1 \leq n, t \leq 200, 1 \leq k \leq 10^9$)Β β the number of stations on the line, hours we want to survive, and capacity of each train respectively.
Each of the next $n$ lines contains three integers $a_i$, $b_i$, and $c_i$ ($0 \leq a_i, b_i \leq c_i \leq 10^9$)Β β number of people at the $i$-th station in the beginning of the game, number of people arriving to $i$-th station in the end of each hour and maximum number of people at the $i$-th station allowed respectively.
Output Specification:
Output a single integer numberΒ β the answer to the problem.
Demo Input:
['3 3 10\n2 4 10\n3 3 9\n4 2 8\n', '4 10 5\n1 1 1\n1 0 1\n0 5 8\n2 7 100\n']
Demo Output:
['2\n', '12\n']
Note:
<img class="tex-graphics" src="https://espresso.codeforces.com/bfa11d535d9fc44e73f6f8280d06436e4e327753.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Let's look at the sample. There are three stations, on the first, there are initially 2 people, 3 people on the second, and 4 people on the third. Maximal capacities of the stations are 10, 9, and 8 respectively.
One of the winning strategies is to appoint two trains to the first and the third hours. Then on the first hour, the train takes all of the people from the stations, and at the end of the hour, 4 people arrive at the first station, 3 on the second, and 2 on the third.
In the second hour there are no trains appointed, and at the end of it, the same amount of people are arriving again.
In the third hour, the train first takes 8 people from the first station, and when it arrives at the second station, it takes only 2 people because it can accommodate no more than 10 people. Then it passes by the third station because it is already full. After it, people arrive at the stations once more, and the game ends.
As there was no such moment when the number of people at a station exceeded maximal capacity, we won using two trains. | 680 |
Title: Replicating Processes
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
A Large Software Company develops its own social network. Analysts have found that during the holidays, major sporting events and other significant events users begin to enter the network more frequently, resulting in great load increase on the infrastructure.
As part of this task, we assume that the social network is 4*n* processes running on the *n* servers. All servers are absolutely identical machines, each of which has a volume of RAM of 1 GB = 1024 MB (1). Each process takes 100 MB of RAM on the server. At the same time, the needs of maintaining the viability of the server takes about 100 more megabytes of RAM. Thus, each server may have up to 9 different processes of social network.
Now each of the *n* servers is running exactly 4 processes. However, at the moment of peak load it is sometimes necessary to replicate the existing 4*n* processes by creating 8*n* new processes instead of the old ones. More formally, there is a set of replication rules, the *i*-th (1<=β€<=*i*<=β€<=4*n*) of which has the form of *a**i*<=β<=(*b**i*,<=*c**i*), where *a**i*, *b**i* and *c**i* (1<=β€<=*a**i*,<=*b**i*,<=*c**i*<=β€<=*n*) are the numbers of servers. This means that instead of an old process running on server *a**i*, there should appear two new copies of the process running on servers *b**i* and *c**i*. The two new replicated processes can be on the same server (i.e., *b**i* may be equal to *c**i*) or even on the same server where the original process was (i.e. *a**i* may be equal to *b**i* or *c**i*). During the implementation of the rule *a**i*<=β<=(*b**i*,<=*c**i*) first the process from the server *a**i* is destroyed, then appears a process on the server *b**i*, then appears a process on the server *c**i*.
There is a set of 4*n* rules, destroying all the original 4*n* processes from *n* servers, and creating after their application 8*n* replicated processes, besides, on each of the *n* servers will be exactly 8 processes. However, the rules can only be applied consecutively, and therefore the amount of RAM of the servers imposes limitations on the procedure for the application of the rules.
According to this set of rules determine the order in which you want to apply all the 4*n* rules so that at any given time the memory of each of the servers contained at most 9 processes (old and new together), or tell that it is impossible.
Input Specification:
The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=30<=000) β the number of servers of the social network.
Next 4*n* lines contain the rules of replicating processes, the *i*-th (1<=β€<=*i*<=β€<=4*n*) of these lines as form *a**i*,<=*b**i*,<=*c**i* (1<=β€<=*a**i*,<=*b**i*,<=*c**i*<=β€<=*n*) and describes rule *a**i*<=β<=(*b**i*,<=*c**i*).
It is guaranteed that each number of a server from 1 to *n* occurs four times in the set of all *a**i*, and eight times among a set that unites all *b**i* and *c**i*.
Output Specification:
If the required order of performing rules does not exist, print "NO" (without the quotes).
Otherwise, print in the first line "YES" (without the quotes), and in the second line β a sequence of 4*n* numbers from 1 to 4*n*, giving the numbers of the rules in the order they are applied. The sequence should be a permutation, that is, include each number from 1 to 4*n* exactly once.
If there are multiple possible variants, you are allowed to print any of them.
Demo Input:
['2\n1 2 2\n1 2 2\n1 2 2\n1 2 2\n2 1 1\n2 1 1\n2 1 1\n2 1 1\n', '3\n1 2 3\n1 1 1\n1 1 1\n1 1 1\n2 1 3\n2 2 2\n2 2 2\n2 2 2\n3 1 2\n3 3 3\n3 3 3\n3 3 3\n']
Demo Output:
['YES\n1 2 5 6 3 7 4 8\n', 'YES\n2 3 4 6 7 8 10 11 12 1 5 9\n']
Note:
<sup class="upper-index">(1)</sup> To be extremely accurate, we should note that the amount of server memory is 1 GiB = 1024 MiB and processes require 100 MiB RAM where a gibibyte (GiB) is the amount of RAM of 2<sup class="upper-index">30</sup> bytes and a mebibyte (MiB) is the amount of RAM of 2<sup class="upper-index">20</sup> bytes.
In the first sample test the network uses two servers, each of which initially has four launched processes. In accordance with the rules of replication, each of the processes must be destroyed and twice run on another server. One of the possible answers is given in the statement: after applying rules 1 and 2 the first server will have 2 old running processes, and the second server will have 8 (4 old and 4 new) processes. After we apply rules 5 and 6, both servers will have 6 running processes (2 old and 4 new). After we apply rules 3 and 7, both servers will have 7 running processes (1 old and 6 new), and after we apply rules 4 and 8, each server will have 8 running processes. At no time the number of processes on a single server exceeds 9.
In the second sample test the network uses three servers. On each server, three processes are replicated into two processes on the same server, and the fourth one is replicated in one process for each of the two remaining servers. As a result of applying rules 2,β3,β4,β6,β7,β8,β10,β11,β12 each server would have 7 processes (6 old and 1 new), as a result of applying rules 1,β5,β9 each server will have 8 processes. At no time the number of processes on a single server exceeds 9. | 681 |
Title: Cut the pie
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Arkady reached the *n*-th level in Township game, so Masha decided to bake a pie for him! Of course, the pie has a shape of convex *n*-gon, i.e. a polygon with *n* vertices.
Arkady decided to cut the pie in two equal in area parts by cutting it by a straight line, so that he can eat one of them and give the other to Masha. There is a difficulty because Arkady has already put a knife at some point of the pie, so he now has to cut the pie by a straight line passing trough this point.
Help Arkady: find a line that passes through the point Arkady has put a knife into and cuts the pie into two parts of equal area, or determine that it's impossible. Your program has to quickly answer many queries with the same pie, but different points in which Arkady puts a knife.
Input Specification:
The first line contains two integers *n* and *q* (3<=β€<=*n*<=β€<=104, 1<=β€<=*q*<=β€<=105)Β β the number of vertices in the pie and the number of queries.
*n* line follow describing the polygon vertices in clockwise order. The *i*-th of these line contains two integers *x**i* and *y**i* (<=-<=106<=β€<=*x**i*,<=*y**i*<=β€<=106)Β β the coordinates of the *i*-th vertex. It is guaranteed that the polygon is strictly convex, in particular, no three vertices line on the same line.
An empty line follows.
*q* lines follow describing the query points. The *i*-th of these lines contain two integers *x**i* and *y**i* (<=-<=106<=β€<=*x**i*,<=*y**i*<=β€<=106)Β β the coordinates of the point in which Arkady puts the knife in the *i*-th query. In is guaranteed that in each query the given point is strictly inside the polygon, in particular, is not on its edges.
Output Specification:
For each query print single integerΒ β the polar angle of the line that is the answer for the corresponding query, in radians. The angle should be in the segment [0;Ο], the angles are measured from the direction of *OX* axis in counter-clockwise order. For example, the polar angle of the *OY* axis is . If there is no answer in that query, print -1.
If there are several answers, print any of them. Your answer is considered correct if the difference between the areas of the parts divided by the total area of the polygon doesn't exceed 10<=-<=4 by absolute value. In other words, if *a* and *b* are the areas of the parts after the cut, then your answer is correct if and only of .
Demo Input:
['3 1\n0 0\n0 3\n3 0\n\n1 1\n', '5 3\n6 5\n6 3\n5 0\n0 0\n0 5\n\n5 4\n3 3\n5 2\n']
Demo Output:
['2.67794504460098710000\n', '0.60228734612690049000\n1.27933953226473580000\n2.85805511179015910000\n']
Note:
none | 682 |
Title: Wilbur and Swimming Pool
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the area of the rectangle must be positive. Wilbur had all four vertices of the planned pool written on a paper, until his friend came along and erased some of the vertices.
Now Wilbur is wondering, if the remaining *n* vertices of the initial rectangle give enough information to restore the area of the planned swimming pool.
Input Specification:
The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=4)Β β the number of vertices that were not erased by Wilbur's friend.
Each of the following *n* lines contains two integers *x**i* and *y**i* (<=-<=1000<=β€<=*x**i*,<=*y**i*<=β€<=1000)Β βthe coordinates of the *i*-th vertex that remains. Vertices are given in an arbitrary order.
It's guaranteed that these points are distinct vertices of some rectangle, that has positive area and which sides are parallel to the coordinate axes.
Output Specification:
Print the area of the initial rectangle if it could be uniquely determined by the points remaining. Otherwise, print <=-<=1.
Demo Input:
['2\n0 0\n1 1\n', '1\n1 1\n']
Demo Output:
['1\n', '-1\n']
Note:
In the first sample, two opposite corners of the initial rectangle are given, and that gives enough information to say that the rectangle is actually a unit square.
In the second sample there is only one vertex left and this is definitely not enough to uniquely define the area. | 683 |
Title: Case of the Zeros and Ones
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length *n* consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length *n*<=-<=2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input Specification:
First line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105), the length of the string that Andreid has.
The second line contains the string of length *n* consisting only from zeros and ones.
Output Specification:
Output the minimum length of the string that may remain after applying the described operations several times.
Demo Input:
['4\n1100\n', '5\n01010\n', '8\n11101111\n']
Demo Output:
['0\n', '1\n', '6\n']
Note:
In the first sample test it is possible to change the string like the following: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/10df55364c21c6e8d5da31b6ab6f6294c4fc26b3.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second sample test it is possible to change the string like the following: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/19ec5dcd85f0b5cf757aa076ace72df39634de2d.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the third sample test it is possible to change the string like the following: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/dc34a159e4230375fa325555527ebc748811f188.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | 684 |
Title: Table Decorations
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You have *r* red, *g* green and *b* blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number *t* of tables can be decorated if we know number of balloons of each color?
Your task is to write a program that for given values *r*, *g* and *b* will find the maximum number *t* of tables, that can be decorated in the required manner.
Input Specification:
The single line contains three integers *r*, *g* and *b* (0<=β€<=*r*,<=*g*,<=*b*<=β€<=2Β·109) β the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
Output Specification:
Print a single integer *t* β the maximum number of tables that can be decorated in the required manner.
Demo Input:
['5 4 3\n', '1 1 1\n', '2 3 3\n']
Demo Output:
['4\n', '1\n', '2\n']
Note:
In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively. | 685 |
Title: System Administrator
Time Limit: 1 seconds
Memory Limit: 256 megabytes
Problem Description:
Bob got a job as a system administrator in X corporation. His first task was to connect *n* servers with the help of *m* two-way direct connection so that it becomes possible to transmit data from one server to any other server via these connections. Each direct connection has to link two different servers, each pair of servers should have at most one direct connection. Y corporation, a business rival of X corporation, made Bob an offer that he couldn't refuse: Bob was asked to connect the servers in such a way, that when server with index *v* fails, the transmission of data between some other two servers becomes impossible, i.e. the system stops being connected. Help Bob connect the servers.
Input Specification:
The first input line contains 3 space-separated integer numbers *n*, *m*, *v* (3<=β€<=*n*<=β€<=105,<=0<=β€<=*m*<=β€<=105,<=1<=β€<=*v*<=β€<=*n*), *n* β amount of servers, *m* β amount of direct connections, *v* β index of the server that fails and leads to the failure of the whole system.
Output Specification:
If it is impossible to connect the servers in the required way, output -1. Otherwise output *m* lines with 2 numbers each β description of all the direct connections in the system. Each direct connection is described by two numbers β indexes of two servers, linked by this direct connection. The servers are numbered from 1. If the answer is not unique, output any.
Demo Input:
['5 6 3\n', '6 100 1\n']
Demo Output:
['1 2\n2 3\n3 4\n4 5\n1 3\n3 5\n', '-1\n']
Note:
none | 686 |
Title: Sloth
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Sloth is bad, mkay? So we decided to prepare a problem to punish lazy guys.
You are given a tree, you should count the number of ways to remove an edge from it and then add an edge to it such that the final graph is a tree and has a perfect matching. Two ways of this operation are considered different if their removed edges or their added edges aren't the same. The removed edge and the added edge can be equal.
A perfect matching is a subset of edges such that each vertex is an endpoint of exactly one of these edges.
Input Specification:
The first line contains *n* (2<=β€<=*n*<=β€<=5Β·105)Β β the number of vertices.
Each of the next *n*<=-<=1 lines contains two integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=*n*)Β β the endpoints of one edge. It's guaranteed that the graph is a tree.
Output Specification:
Output a single integerΒ β the answer to the problem.
Demo Input:
['4\n1 2\n2 3\n3 4\n', '5\n1 2\n2 3\n3 4\n3 5\n', '8\n1 2\n2 3\n3 4\n1 5\n5 6\n6 7\n1 8\n']
Demo Output:
['8\n', '0\n', '22\n']
Note:
In first sample, there are 8 ways:
- edge between 2 and 3 turns to edge between 1 and 3, - edge between 2 and 3 turns to edge between 1 and 4, - edge between 2 and 3 turns to edge between 2 and 3, - edge between 2 and 3 turns to edge between 2 and 4, - edge between 1 and 2 turns to edge between 1 and 2, - edge between 1 and 2 turns to edge between 1 and 4, - edge between 3 and 4 turns to edge between 1 and 4, <li> edge between 3 and 4 turns to edge between 3 and 4. <ul> | 687 |
Title: Warrior and Archer
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest.
Vova and Lesha are friends. They often meet at Vova's place and compete against each other in a computer game named The Ancient Papyri: Swordsink. Vova always chooses a warrior as his fighter and Leshac chooses an archer. After that they should choose initial positions for their characters and start the fight. A warrior is good at melee combat, so Vova will try to make the distance between fighters as small as possible. An archer prefers to keep the enemy at a distance, so Lesha will try to make the initial distance as large as possible.
There are *n* (*n* is always even) possible starting positions for characters marked along the *Ox* axis. The positions are given by their distinct coordinates *x*1,<=*x*2,<=...,<=*x**n*, two characters cannot end up at the same position.
Vova and Lesha take turns banning available positions, Vova moves first. During each turn one of the guys bans exactly one of the remaining positions. Banned positions cannot be used by both Vova and Lesha. They continue to make moves until there are only two possible positions remaining (thus, the total number of moves will be *n*<=-<=2). After that Vova's character takes the position with the lesser coordinate and Lesha's character takes the position with the bigger coordinate and the guys start fighting.
Vova and Lesha are already tired by the game of choosing positions, as they need to play it before every fight, so they asked you (the developer of the The Ancient Papyri: Swordsink) to write a module that would automatically determine the distance at which the warrior and the archer will start fighting if both Vova and Lesha play optimally.
Input Specification:
The first line on the input contains a single integer *n* (2<=β€<=*n*<=β€<=200<=000, *n* is even)Β β the number of positions available initially. The second line contains *n* distinct integers *x*1,<=*x*2,<=...,<=*x**n* (0<=β€<=*x**i*<=β€<=109), giving the coordinates of the corresponding positions.
Output Specification:
Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.
Demo Input:
['6\n0 1 3 7 15 31\n', '2\n73 37\n']
Demo Output:
['7\n', '36\n']
Note:
In the first sample one of the optimum behavior of the players looks like that:
1. Vova bans the position at coordinate 15; 1. Lesha bans the position at coordinate 3; 1. Vova bans the position at coordinate 31; 1. Lesha bans the position at coordinate 1.
After these actions only positions 0 and 7 will remain, and the distance between them is equal to 7.
In the second sample there are only two possible positions, so there will be no bans. | 688 |
Title: Coins
Time Limit: 2 seconds
Memory Limit: 256 megabytes
Problem Description:
In Berland a money reform is being prepared. New coins are being introduced. After long economic calculations was decided that the most expensive coin should possess the denomination of exactly *n* Berland dollars. Also the following restriction has been introduced for comfort: the denomination of each coin should be divisible by the denomination of any cheaper coin. It is known that among all the possible variants the variant with the largest number of new coins will be chosen. Find this variant. Print in the order of decreasing of the coins' denominations.
Input Specification:
The first and only line contains an integer *n* (1<=β€<=*n*<=β€<=106) which represents the denomination of the most expensive coin.
Output Specification:
Print the denominations of all the coins in the order of decreasing. The number of coins must be the largest possible (with the given denomination *n* of the most expensive coin). Also, the denomination of every coin must be divisible by the denomination of any cheaper coin. Naturally, the denominations of all the coins should be different. If there are several solutins to that problem, print any of them.
Demo Input:
['10\n', '4\n', '3\n']
Demo Output:
['10 5 1\n', '4 2 1\n', '3 1\n']
Note:
none | 689 |
Title: Tournament
Time Limit: 2 seconds
Memory Limit: 256 megabytes
Problem Description:
The tournament Β«Sleepyhead-2010Β» in the rapid falling asleep has just finished in Berland. *n* best participants from the country have participated in it. The tournament consists of games, each of them is a match between two participants. *n*Β·(*n*<=-<=1)<=/<=2 games were played during the tournament, and each participant had a match with each other participant.
The rules of the game are quite simple β the participant who falls asleep first wins. The secretary made a record of each game in the form Β«*x**i* *y**i*Β», where *x**i* and *y**i* are the numbers of participants. The first number in each pair is a winner (i.e. *x**i* is a winner and *y**i* is a loser). There is no draws.
Recently researches form the Β«Institute Of SleepΒ» have found that every person is characterized by a value *p**j* β the speed of falling asleep. The person who has lower speed wins. Every person has its own value *p**j*, constant during the life.
It is known that all participants of the tournament have distinct speeds of falling asleep. Also it was found that the secretary made records about all the games except one. You are to find the result of the missing game.
Input Specification:
The first line contains one integer *n* (3<=β€<=*n*<=β€<=50) β the number of participants. The following *n*Β·(*n*<=-<=1)<=/<=2<=-<=1 lines contain the results of the games. Each game is described in a single line by two integers *x**i*,<=*y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*,<=*x**i*<=β <=*y**i*), where *x**i* ΠΈ *y**i* are the numbers of the opponents in this game. It is known that during the tournament each of the *n* participants played *n*<=-<=1 games, one game with each other participant.
Output Specification:
Output two integers *x* and *y* β the missing record. If there are several solutions, output any of them.
Demo Input:
['4\n4 2\n4 1\n2 3\n2 1\n3 1\n']
Demo Output:
['4 3\n']
Note:
none | 690 |
Title: Party
Time Limit: 2 seconds
Memory Limit: 256 megabytes
Problem Description:
*n* people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2,<=3,<=...,<=*n*<=-<=1 friends among those who stayed by the moment of their leaving, did the same.
What is the maximum amount of people that could stay at the party in the end?
Input Specification:
The first input line contains one number *t* β amount of tests (1<=β€<=*t*<=β€<=105). Each of the following *t* lines contains one integer number *n* (1<=β€<=*n*<=β€<=105).
Output Specification:
For each test output in a separate line one number β the maximum amount of people that could stay in the end.
Demo Input:
['1\n3\n']
Demo Output:
['1\n']
Note:
none | 691 |
Title: Imbalance Value of a Tree
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given a tree *T* consisting of *n* vertices. A number is written on each vertex; the number written on vertex *i* is *a**i*. Let's denote the function *I*(*x*,<=*y*) as the difference between maximum and minimum value of *a**i* on a simple path connecting vertices *x* and *y*.
Your task is to calculate .
Input Specification:
The first line contains one integer number *n* (1<=β€<=*n*<=β€<=106) β the number of vertices in the tree.
The second line contains *n* integer numbers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=106) β the numbers written on the vertices.
Then *n*<=-<=1 lines follow. Each line contains two integers *x* and *y* denoting an edge connecting vertex *x* and vertex *y* (1<=β€<=*x*,<=*y*<=β€<=*n*, *x*<=β <=*y*). It is guaranteed that these edges denote a tree.
Output Specification:
Print one number equal to .
Demo Input:
['4\n2 2 3 1\n1 2\n1 3\n1 4\n']
Demo Output:
['6\n']
Note:
none | 692 |
Title: Minimum Diameter
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given *n* points on the plane. You need to delete exactly *k* of them (*k*<=<<=*n*) so that the diameter of the set of the remaining *n*<=-<=*k* points were as small as possible. The diameter of a set of points is the maximum pairwise distance between the points of the set. The diameter of a one point set equals zero.
Input Specification:
The first input line contains a pair of integers *n*,<=*k* (2<=β€<=*n*<=β€<=1000, 1<=β€<=*k*<=β€<=30, *k*<=<<=*n*) β the numbers of points on the plane and the number of points to delete, correspondingly.
Next *n* lines describe the points, one per line. Each description consists of a pair of integers *x**i*,<=*y**i* (0<=β€<=*x**i*,<=*y**i*<=β€<=32000) β the coordinates of the *i*-th point. The given points can coincide.
Output Specification:
Print *k* different space-separated integers from 1 to *n* β the numbers of points to delete. The points are numbered in the order, in which they are given in the input from 1 to *n*. You can print the numbers in any order. If there are multiple solutions, print any of them.
Demo Input:
['5 2\n1 2\n0 0\n2 2\n1 1\n3 3\n', '4 1\n0 0\n0 0\n1 1\n1 1\n']
Demo Output:
['5 2', '3']
Note:
none | 693 |
Title: Holidays
Time Limit: 2 seconds
Memory Limit: 256 megabytes
Problem Description:
School holidays come in Berland. The holidays are going to continue for *n* days. The students of school β*N* are having the time of their lives and the IT teacher Marina Sergeyevna, who has spent all the summer busy checking the BSE (Berland State Examination) results, has finally taken a vacation break! Some people are in charge of the daily watering of flowers in shifts according to the schedule. However when Marina Sergeyevna was making the schedule, she was so tired from work and so lost in dreams of the oncoming vacation that she perhaps made several mistakes. In fact, it is possible that according to the schedule, on some days during the holidays the flowers will not be watered or will be watered multiple times. Help Marina Sergeyevna to find a mistake.
Input Specification:
The first input line contains two numbers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100) β the number of days in Berland holidays and the number of people in charge of the watering respectively. The next *m* lines contain the description of the duty schedule. Each line contains two integers *a**i* and *b**i* (1<=β€<=*a**i*<=β€<=*b**i*<=β€<=*n*), meaning that the *i*-th person in charge should water the flowers from the *a**i*-th to the *b**i*-th day inclusively, once a day. The duty shifts are described sequentially, i.e. *b**i*<=β€<=*a**i*<=+<=1 for all *i* from 1 to *n*<=-<=1 inclusively.
Output Specification:
Print "OK" (without quotes), if the schedule does not contain mistakes. Otherwise you have to find the minimal number of a day when the flowers will not be watered or will be watered multiple times, and output two integers β the day number and the number of times the flowers will be watered that day.
Demo Input:
['10 5\n1 2\n3 3\n4 6\n7 7\n8 10\n', '10 5\n1 2\n2 3\n4 5\n7 8\n9 10\n', '10 5\n1 2\n3 3\n5 7\n7 7\n7 10\n']
Demo Output:
['OK\n', '2 2\n', '4 0\n']
Note:
Keep in mind that in the second sample the mistake occurs not only on the second day, but also on the sixth day, when nobody waters the flowers. However, you have to print the second day, i.e. the day with the minimal number. | 694 |
Title: none
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Today Pari and Arya are playing a game called Remainders.
Pari chooses two positive integer *x* and *k*, and tells Arya *k* but not *x*. Arya have to find the value . There are *n* ancient numbers *c*1,<=*c*2,<=...,<=*c**n* and Pari has to tell Arya if Arya wants. Given *k* and the ancient values, tell us if Arya has a winning strategy independent of value of *x* or not. Formally, is it true that Arya can understand the value for any positive integer *x*?
Note, that means the remainder of *x* after dividing it by *y*.
Input Specification:
The first line of the input contains two integers *n* and *k* (1<=β€<=*n*,<= *k*<=β€<=1<=000<=000)Β β the number of ancient integers and value *k* that is chosen by Pari.
The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=1<=000<=000).
Output Specification:
Print "Yes" (without quotes) if Arya has a winning strategy independent of value of *x*, or "No" (without quotes) otherwise.
Demo Input:
['4 5\n2 3 5 12\n', '2 7\n2 3\n']
Demo Output:
['Yes\n', 'No\n']
Note:
In the first sample, Arya can understand <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d170efffcde0907ee6bcf32de21051bce0677a2c.png" style="max-width: 100.0%;max-height: 100.0%;"/> because 5 is one of the ancient numbers.
In the second sample, Arya can't be sure what <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/57b5f6a96f5db073270dd3ed4266c69299ec701d.png" style="max-width: 100.0%;max-height: 100.0%;"/> is. For example 1 and 7 have the same remainders after dividing by 2 and 3, but they differ in remainders after dividing by 7. | 695 |
Title: Winnie-the-Pooh and honey
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
As we all know, Winnie-the-Pooh just adores honey. Ones he and the Piglet found out that the Rabbit has recently gotten hold of an impressive amount of this sweet and healthy snack. As you may guess, Winnie and the Piglet asked to come at the Rabbit's place. Thus, there are *n* jars of honey lined up in front of Winnie-the-Pooh, jar number *i* contains *a**i* kilos of honey. Winnie-the-Pooh eats the honey like that: each time he chooses a jar containing most honey. If the jar has less that *k* kilos of honey or if Winnie-the-Pooh has already eaten from it three times, he gives the jar to Piglet. Otherwise he eats exactly *k* kilos of honey from the jar and puts it back. Winnie does so until he gives all jars to the Piglet. Count how much honey Piglet will overall get after Winnie satisfies his hunger.
Input Specification:
The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=100,<=1<=β€<=*k*<=β€<=100). The second line contains *n* integers *a*1, *a*2, ..., *a**n*, separated by spaces (1<=β€<=*a**i*<=β€<=100).
Output Specification:
Print a single number β how many kilos of honey gets Piglet.
Demo Input:
['3 3\n15 8 10\n']
Demo Output:
['9\n']
Note:
none | 696 |
Title: Platforms
Time Limit: 2 seconds
Memory Limit: 64 megabytes
Problem Description:
In one one-dimensional world there are *n* platforms. Platform with index *k* (platforms are numbered from 1) is a segment with coordinates [(*k*<=-<=1)*m*,<=(*k*<=-<=1)*m*<=+<=*l*], and *l*<=<<=*m*. Grasshopper Bob starts to jump along the platforms from point 0, with each jump he moves exactly *d* units right. Find out the coordinate of the point, where Bob will fall down. The grasshopper falls down, if he finds himself not on the platform, but if he finds himself on the edge of the platform, he doesn't fall down.
Input Specification:
The first input line contains 4 integer numbers *n*, *d*, *m*, *l* (1<=β€<=*n*,<=*d*,<=*m*,<=*l*<=β€<=106,<=*l*<=<<=*m*) β respectively: amount of platforms, length of the grasshopper Bob's jump, and numbers *m* and *l* needed to find coordinates of the *k*-th platform: [(*k*<=-<=1)*m*,<=(*k*<=-<=1)*m*<=+<=*l*].
Output Specification:
Output the coordinates of the point, where the grosshopper will fall down. Don't forget that if Bob finds himself on the platform edge, he doesn't fall down.
Demo Input:
['2 2 5 3\n', '5 4 11 8\n']
Demo Output:
['4\n', '20\n']
Note:
none | 697 |
Title: Well-known Numbers
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
Numbers *k*-bonacci (*k* is integer, *k*<=><=1) are a generalization of Fibonacci numbers and are determined as follows:
- *F*(*k*,<=*n*)<==<=0, for integer *n*, 1<=β€<=*n*<=<<=*k*; - *F*(*k*,<=*k*)<==<=1; - *F*(*k*,<=*n*)<==<=*F*(*k*,<=*n*<=-<=1)<=+<=*F*(*k*,<=*n*<=-<=2)<=+<=...<=+<=*F*(*k*,<=*n*<=-<=*k*), for integer *n*, *n*<=><=*k*.
Note that we determine the *k*-bonacci numbers, *F*(*k*,<=*n*), only for integer values of *n* and *k*.
You've got a number *s*, represent it as a sum of several (at least two) distinct *k*-bonacci numbers.
Input Specification:
The first line contains two integers *s* and *k* (1<=β€<=*s*,<=*k*<=β€<=109;Β *k*<=><=1).
Output Specification:
In the first line print an integer *m* (*m*<=β₯<=2) that shows how many numbers are in the found representation. In the second line print *m* distinct integers *a*1,<=*a*2,<=...,<=*a**m*. Each printed integer should be a *k*-bonacci number. The sum of printed integers must equal *s*.
It is guaranteed that the answer exists. If there are several possible answers, print any of them.
Demo Input:
['5 2\n', '21 5\n']
Demo Output:
['3\n0 2 3\n', '3\n4 1 16\n']
Note:
none | 698 |
Title: Average Numbers
Time Limit: None seconds
Memory Limit: None megabytes
Problem Description:
You are given a sequence of positive integers *a*1,<=*a*2,<=...,<=*a**n*. Find all such indices *i*, that the *i*-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
Input Specification:
The first line contains the integer *n* (2<=β€<=*n*<=β€<=2Β·105). The second line contains elements of the sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1000). All the elements are positive integers.
Output Specification:
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to *n*.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
Demo Input:
['5\n1 2 3 4 5\n', '4\n50 50 50 50\n']
Demo Output:
['1\n3 ', '4\n1 2 3 4 ']
Note:
none | 699 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.