question large_stringlengths 265 13.2k |
|---|
Solve the programming task below in a Python markdown code block.
Problem C: Seishun 18 Kippu
A student at R University, sirokurostone, was about to attend a training camp at Atsu University. Other members plan to use the Shinkansen, but sirokurostone was going to use the Seishun 18 Ticket. Similarly, a person who likes 2D with a youth 18 ticket was also trying to participate in the training camp.
Sirokurostone, who wanted to go with him anyway, decided to pick up him who likes 2D at the station on the way. sirokurostone wants to check the delay situation before leaving R University and take a route that takes less time to reach the station where Atsu University is located. sirokurostone intends to give that information to him who likes 2D after the route is confirmed. However, sirokurostone is in trouble because he does not know which route to take to reach the station where Atsu University is located sooner even if he sees the delay situation.
So your job is to find out how long it will take to get to the station where Atsu University is located on behalf of sirokurostone from the delay situation.
Between each station, there is a distance between stations and an estimated delay time. The train shall maintain 40km / h after it starts moving. The travel time between points a and b is
* Distance / 40+ estimated delay time
Can be obtained at. The stop time at each station can be ignored.
Input
A sequence of multiple datasets is given as input. The number of datasets is guaranteed to be 50 or less. Each dataset has the following format:
n m
s p g
a1 b1 d1 t1
...
ai bi di ti
...
am bm dm tm
The integers n (3 β€ n β€ 500) and m (2 β€ m β€ 5000) represent the total number of stations and the number of tracks between each station, respectively. s, p, and g are character strings that indicate the location of the station where sirokurostone rides, the station where a person who likes 2D rides, and the station where Atsu University is located, respectively. s, p, and g are different character strings.
The following m line represents the information between stations. ai and bi are character strings indicating stations connected by railroad tracks. ai and bi can go back and forth in both directions. ai and bi never match. The integer di (40 β€ di β€ 10000) represents the distance between ai and bi. di is guaranteed to be a multiple of 40. The integer ti (0 β€ ti β€ 20) indicates the estimated delay time between ai and bi.
The character string representing the station name is represented by a character string of 20 characters or less consisting of all lowercase and uppercase letters of the alphabet. There is at most one track between stations.
The end of the input is represented by a line containing two zeros.
Output
For each input dataset, output the arrival time (unit: time) to the station where Atsu University is located. It is guaranteed that there is a route from the station where sirokurostone rides to the station where 2D lovers ride, and a route from the station where 2D lovers ride to the station where Atsu University is located.
Sample Input
4 4
A B G
A B 40 3
B C 80 0
A G 40 0
B G 80 0
5 6
Kusatsu Tokyo Aizu
Tokyo Nagoya 120 3
Kusatsu Nagoya 40 0
Kusatsu Kanazawa 40 0
Kanazawa Aizu 40 0
Tokyo Kanazawa 40 0
Tokyo Aizu 80 4
0 0
Output for Sample Input
Five
Four
Example
Input
4 4
A B G
A B 40 3
B C 80 0
A G 40 0
B G 80 0
5 6
Kusatsu Tokyo Aizu
Tokyo Nagoya 120 3
Kusatsu Nagoya 40 0
Kusatsu Kanazawa 40 0
Kanazawa Aizu 40 0
Tokyo Kanazawa 40 0
Tokyo Aizu 80 4
0 0
Output
5
4
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Hakone Ekiden is one of the Japanese New Year's traditions. In Hakone Ekiden, 10 runners from each team aim for the goal while connecting the sashes at each relay station. In the TV broadcast, the ranking change from the previous relay station is displayed along with the passing order of each team at the relay station. So, look at it and tell us how many possible passage orders for each team at the previous relay station. The number of possible transit orders can be very large, so answer by the remainder divided by 1,000,000,007.
Input
The input is given in the form:
> n
> c1
> c2
> ...
> cn
>
The number n (1 β€ n β€ 200) representing the number of teams is on the first line, and the ranking changes from the previous relay station in order from the first place on the following n lines. If it is'`U`', the ranking is up, if it is'`-`', the ranking is not changed) is written.
Output
Please output in one line by dividing the number of passages that could have been the previous relay station by 1,000,000,007.
Examples
Input
3
-
U
D
Output
1
Input
5
U
U
-
D
D
Output
5
Input
8
U
D
D
D
D
D
D
D
Output
1
Input
10
U
D
U
D
U
D
U
D
U
D
Output
608
Input
2
D
U
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Problem statement
Here are N mysteriously shaped vases. The i-th jar is a shape in which K_i right-sided cylinders are vertically connected in order from the bottom. The order in which they are connected cannot be changed. Mr. A has a volume of water of M. Pour this water into each jar in any amount you like. It does not matter if there is a jar that does not contain any water. Also, when all the vases are filled with water, no more water can be poured. Find the maximum sum of the heights of the water surface of each jar.
input
N \ M
K_1 \ S_ {11} \ H_ {11} \β¦ \ S_ {1 K_1} \ H_ {1 K_1}
K_2 \ S_ {21} \ H_ {21} \β¦ \ S_ {2 K_2} \ H_ {2 K_2}
...
K_N \ S_ {N1} \ H_ {N1} \β¦ \ S_ {N K_N} \ H_ {N K_N}
N and M are entered in the first line, and the information of the i-th jar is entered in the 1 + i line. K_i is the number of right-sided cylinders, and S_ {ij} and H_ {ij} are the base area and height of the j-th right-sided cylinder that makes up the jar, respectively.
Constraint
* An integer
* 1 β€ N β€ 200
* 1 β€ M β€ 200
* 1 β€ K_i β€ 20
* 1 β€ S_ {ij} β€ 20
* 1 β€ H_ {ij} β€ 20
output
Output the answer in one line. It may include an absolute error of 0.00001 or less.
sample
Sample input 1
2 15
2 3 3 7 2
2 7 1 1 4
Sample output 1
6.33333333
Sample input 2
2 14
1 2 4
2 5 2 1 4
Sample output 2
6
The input and output of samples 1 and 2 are shown below.
<image>
Sample input 3
2 25
4 8 9 1 9 6 5 2 8
4 1 7 4 4 1 6 4 3
Sample output 3
13
Example
Input
2 15
2 3 3 7 2
2 7 1 1 4
Output
6.33333333
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Given a non-negative decimal integer $x$, convert it to binary representation $b$ of 32 bits. Then, print the result of the following operations to $b$ respecitvely.
* Inversion: change the state of each bit to the opposite state
* Logical left shift: shift left by 1
* Logical right shift: shift right by 1
Constraints
* $0 \leq x \leq 2^{32} - 1$
Input
The input is given in the following format.
$x$
Output
Print the given bits, results of inversion, left shift and right shift in a line respectively.
Examples
Input
8
Output
00000000000000000000000000001000
11111111111111111111111111110111
00000000000000000000000000010000
00000000000000000000000000000100
Input
13
Output
00000000000000000000000000001101
11111111111111111111111111110010
00000000000000000000000000011010
00000000000000000000000000000110
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a string s consisting of n lowercase Latin letters. n is even.
For each position i (1 β€ i β€ n) in string s you are required to change the letter on this position either to the previous letter in alphabetic order or to the next one (letters 'a' and 'z' have only one of these options). Letter in every position must be changed exactly once.
For example, letter 'p' should be changed either to 'o' or to 'q', letter 'a' should be changed to 'b' and letter 'z' should be changed to 'y'.
That way string "codeforces", for example, can be changed to "dpedepqbft" ('c' β 'd', 'o' β 'p', 'd' β 'e', 'e' β 'd', 'f' β 'e', 'o' β 'p', 'r' β 'q', 'c' β 'b', 'e' β 'f', 's' β 't').
String s is called a palindrome if it reads the same from left to right and from right to left. For example, strings "abba" and "zz" are palindromes and strings "abca" and "zy" are not.
Your goal is to check if it's possible to make string s a palindrome by applying the aforementioned changes to every position. Print "YES" if string s can be transformed to a palindrome and "NO" otherwise.
Each testcase contains several strings, for each of them you are required to solve the problem separately.
Input
The first line contains a single integer T (1 β€ T β€ 50) β the number of strings in a testcase.
Then 2T lines follow β lines (2i - 1) and 2i of them describe the i-th string. The first line of the pair contains a single integer n (2 β€ n β€ 100, n is even) β the length of the corresponding string. The second line of the pair contains a string s, consisting of n lowercase Latin letters.
Output
Print T lines. The i-th line should contain the answer to the i-th string of the input. Print "YES" if it's possible to make the i-th string a palindrome by applying the aforementioned changes to every position. Print "NO" otherwise.
Example
Input
5
6
abccba
2
cf
4
adfa
8
abaazaba
2
ml
Output
YES
NO
YES
NO
NO
Note
The first string of the example can be changed to "bcbbcb", two leftmost letters and two rightmost letters got changed to the next letters, two middle letters got changed to the previous letters.
The second string can be changed to "be", "bg", "de", "dg", but none of these resulting strings are palindromes.
The third string can be changed to "beeb" which is a palindrome.
The fifth string can be changed to "lk", "lm", "nk", "nm", but none of these resulting strings are palindromes. Also note that no letter can remain the same, so you can't obtain strings "ll" or "mm".
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Our brave travelers reached an island where pirates had buried treasure. However as the ship was about to moor, the captain found out that some rat ate a piece of the treasure map.
The treasure map can be represented as a rectangle n Γ m in size. Each cell stands for an islands' square (the square's side length equals to a mile). Some cells stand for the sea and they are impenetrable. All other cells are penetrable (i.e. available) and some of them contain local sights. For example, the large tree on the hills or the cave in the rocks.
Besides, the map also has a set of k instructions. Each instruction is in the following form:
"Walk n miles in the y direction"
The possible directions are: north, south, east, and west. If you follow these instructions carefully (you should fulfill all of them, one by one) then you should reach exactly the place where treasures are buried.
Unfortunately the captain doesn't know the place where to start fulfilling the instructions β as that very piece of the map was lost. But the captain very well remembers that the place contained some local sight. Besides, the captain knows that the whole way goes through the island's penetrable squares.
The captain wants to know which sights are worth checking. He asks you to help him with that.
Input
The first line contains two integers n and m (3 β€ n, m β€ 1000).
Then follow n lines containing m integers each β the island map's description. "#" stands for the sea. It is guaranteed that all cells along the rectangle's perimeter are the sea. "." stands for a penetrable square without any sights and the sights are marked with uppercase Latin letters from "A" to "Z". Not all alphabet letters can be used. However, it is guaranteed that at least one of them is present on the map. All local sights are marked by different letters.
The next line contains number k (1 β€ k β€ 105), after which k lines follow. Each line describes an instruction. Each instruction possesses the form "dir len", where dir stands for the direction and len stands for the length of the way to walk. dir can take values "N", "S", "W" and "E" for North, South, West and East correspondingly. At that, north is to the top, South is to the bottom, west is to the left and east is to the right. len is an integer from 1 to 1000.
Output
Print all local sights that satisfy to the instructions as a string without any separators in the alphabetical order. If no sight fits, print "no solution" without the quotes.
Examples
Input
6 10
##########
#K#..#####
#.#..##.##
#..L.#...#
###D###A.#
##########
4
N 2
S 1
E 1
W 2
Output
AD
Input
3 4
####
#.A#
####
2
W 1
N 2
Output
no solution
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are n people sitting in a circle, numbered from 1 to n in the order in which they are seated. That is, for all i from 1 to n-1, the people with id i and i+1 are adjacent. People with id n and 1 are adjacent as well.
The person with id 1 initially has a ball. He picks a positive integer k at most n, and passes the ball to his k-th neighbour in the direction of increasing ids, that person passes the ball to his k-th neighbour in the same direction, and so on until the person with the id 1 gets the ball back. When he gets it back, people do not pass the ball any more.
For instance, if n = 6 and k = 4, the ball is passed in order [1, 5, 3, 1].
Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be 1 + 5 + 3 = 9.
Find and report the set of possible fun values for all choices of positive integer k. It can be shown that under the constraints of the problem, the ball always gets back to the 1-st player after finitely many steps, and there are no more than 10^5 possible fun values for given n.
Input
The only line consists of a single integer n (2 β€ n β€ 10^9) β the number of people playing with the ball.
Output
Suppose the set of all fun values is f_1, f_2, ..., f_m.
Output a single line containing m space separated integers f_1 through f_m in increasing order.
Examples
Input
6
Output
1 5 9 21
Input
16
Output
1 10 28 64 136
Note
In the first sample, we've already shown that picking k = 4 yields fun value 9, as does k = 2. Picking k = 6 results in fun value of 1. For k = 3 we get fun value 5 and with k = 1 or k = 5 we get 21.
<image>
In the second sample, the values 1, 10, 28, 64 and 136 are achieved for instance for k = 16, 8, 4, 10 and 11, respectively.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an integer n (n β₯ 0) represented with k digits in base (radix) b. So,
$$$n = a_1 β
b^{k-1} + a_2 β
b^{k-2} + β¦ a_{k-1} β
b + a_k.$$$
For example, if b=17, k=3 and a=[11, 15, 7] then n=11β
17^2+15β
17+7=3179+255+7=3441.
Determine whether n is even or odd.
Input
The first line contains two integers b and k (2β€ bβ€ 100, 1β€ kβ€ 10^5) β the base of the number and the number of digits.
The second line contains k integers a_1, a_2, β¦, a_k (0β€ a_i < b) β the digits of n.
The representation of n contains no unnecessary leading zero. That is, a_1 can be equal to 0 only if k = 1.
Output
Print "even" if n is even, otherwise print "odd".
You can print each letter in any case (upper or lower).
Examples
Input
13 3
3 2 7
Output
even
Input
10 9
1 2 3 4 5 6 7 8 9
Output
odd
Input
99 5
32 92 85 74 4
Output
odd
Input
2 2
1 0
Output
even
Note
In the first example, n = 3 β
13^2 + 2 β
13 + 7 = 540, which is even.
In the second example, n = 123456789 is odd.
In the third example, n = 32 β
99^4 + 92 β
99^3 + 85 β
99^2 + 74 β
99 + 4 = 3164015155 is odd.
In the fourth example n = 2.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Let s be some string consisting of symbols "0" or "1". Let's call a string t a substring of string s, if there exists such number 1 β€ l β€ |s| - |t| + 1 that t = s_l s_{l+1} β¦ s_{l + |t| - 1}. Let's call a substring t of string s unique, if there exist only one such l.
For example, let s = "1010111". A string t = "010" is an unique substring of s, because l = 2 is the only one suitable number. But, for example t = "10" isn't a unique substring of s, because l = 1 and l = 3 are suitable. And for example t ="00" at all isn't a substring of s, because there is no suitable l.
Today Vasya solved the following problem at the informatics lesson: given a string consisting of symbols "0" and "1", the task is to find the length of its minimal unique substring. He has written a solution to this problem and wants to test it. He is asking you to help him.
You are given 2 positive integers n and k, such that (n mod 2) = (k mod 2), where (x mod 2) is operation of taking remainder of x by dividing on 2. Find any string s consisting of n symbols "0" or "1", such that the length of its minimal unique substring is equal to k.
Input
The first line contains two integers n and k, separated by spaces (1 β€ k β€ n β€ 100 000, (k mod 2) = (n mod 2)).
Output
Print a string s of length n, consisting of symbols "0" and "1". Minimal length of the unique substring of s should be equal to k. You can find any suitable string. It is guaranteed, that there exists at least one such string.
Examples
Input
4 4
Output
1111
Input
5 3
Output
01010
Input
7 3
Output
1011011
Note
In the first test, it's easy to see, that the only unique substring of string s = "1111" is all string s, which has length 4.
In the second test a string s = "01010" has minimal unique substring t ="101", which has length 3.
In the third test a string s = "1011011" has minimal unique substring t ="110", which has length 3.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
While playing with geometric figures Alex has accidentally invented a concept of a n-th order rhombus in a cell grid.
A 1-st order rhombus is just a square 1 Γ 1 (i.e just a cell).
A n-th order rhombus for all n β₯ 2 one obtains from a n-1-th order rhombus adding all cells which have a common side with it to it (look at the picture to understand it better).
<image>
Alex asks you to compute the number of cells in a n-th order rhombus.
Input
The first and only input line contains integer n (1 β€ n β€ 100) β order of a rhombus whose numbers of cells should be computed.
Output
Print exactly one integer β the number of cells in a n-th order rhombus.
Examples
Input
1
Output
1
Input
2
Output
5
Input
3
Output
13
Note
Images of rhombus corresponding to the examples are given in the statement.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
For years, the Day of city N was held in the most rainy day of summer. New mayor decided to break this tradition and select a not-so-rainy day for the celebration. The mayor knows the weather forecast for the n days of summer. On the i-th day, a_i millimeters of rain will fall. All values a_i are distinct.
The mayor knows that citizens will watch the weather x days before the celebration and y days after. Because of that, he says that a day d is not-so-rainy if a_d is smaller than rain amounts at each of x days before day d and and each of y days after day d. In other words, a_d < a_j should hold for all d - x β€ j < d and d < j β€ d + y. Citizens only watch the weather during summer, so we only consider such j that 1 β€ j β€ n.
Help mayor find the earliest not-so-rainy day of summer.
Input
The first line contains three integers n, x and y (1 β€ n β€ 100 000, 0 β€ x, y β€ 7) β the number of days in summer, the number of days citizens watch the weather before the celebration and the number of days they do that after.
The second line contains n distinct integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i denotes the rain amount on the i-th day.
Output
Print a single integer β the index of the earliest not-so-rainy day of summer. We can show that the answer always exists.
Examples
Input
10 2 2
10 9 6 7 8 3 2 1 4 5
Output
3
Input
10 2 3
10 9 6 7 8 3 2 1 4 5
Output
8
Input
5 5 5
100000 10000 1000 100 10
Output
5
Note
In the first example days 3 and 8 are not-so-rainy. The 3-rd day is earlier.
In the second example day 3 is not not-so-rainy, because 3 + y = 6 and a_3 > a_6. Thus, day 8 is the answer. Note that 8 + y = 11, but we don't consider day 11, because it is not summer.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In addition to complaints about lighting, a lot of complaints about insufficient radio signal covering has been received by Bertown city hall recently. n complaints were sent to the mayor, all of which are suspiciosly similar to each other: in the i-th complaint, one of the radio fans has mentioned that the signals of two radio stations x_i and y_i are not covering some parts of the city, and demanded that the signal of at least one of these stations can be received in the whole city.
Of cousre, the mayor of Bertown is currently working to satisfy these complaints. A new radio tower has been installed in Bertown, it can transmit a signal with any integer power from 1 to M (let's denote the signal power as f). The mayor has decided that he will choose a set of radio stations and establish a contract with every chosen station. To establish a contract with the i-th station, the following conditions should be met:
* the signal power f should be not less than l_i, otherwise the signal of the i-th station won't cover the whole city;
* the signal power f should be not greater than r_i, otherwise the signal will be received by the residents of other towns which haven't established a contract with the i-th station.
All this information was already enough for the mayor to realise that choosing the stations is hard. But after consulting with specialists, he learned that some stations the signals of some stations may interfere with each other: there are m pairs of stations (u_i, v_i) that use the same signal frequencies, and for each such pair it is impossible to establish contracts with both stations. If stations x and y use the same frequencies, and y and z use the same frequencies, it does not imply that x and z use the same frequencies.
The mayor finds it really hard to analyze this situation, so he hired you to help him. You have to choose signal power f and a set of stations to establish contracts with such that:
* all complaints are satisfied (formally, for every i β [1, n] the city establishes a contract either with station x_i, or with station y_i);
* no two chosen stations interfere with each other (formally, for every i β [1, m] the city does not establish a contract either with station u_i, or with station v_i);
* for each chosen station, the conditions on signal power are met (formally, for each chosen station i the condition l_i β€ f β€ r_i is met).
Input
The first line contains 4 integers n, p, M and m (2 β€ n, p, M, m β€ 4 β
10^5) β the number of complaints, the number of radio stations, maximum signal power and the number of interfering pairs, respectively.
Then n lines follow, which describe the complains. Each line contains two integers x_i and y_i (1 β€ x_i < y_i β€ p) β the indices of the radio stations mentioned in the i-th complaint). All complaints are distinct.
Then p lines follow, which describe the radio stations. Each line contains two integers l_i and r_i (1 β€ l_i β€ r_i β€ M) β the constrains on signal power that should be satisfied if the city establishes a contract with the i-th station.
Then m lines follow, which describe the pairs of interfering radio stations. Each line contains two integers u_i and v_i (1 β€ u_i < v_i β€ p) β the indices of interfering radio stations. All these pairs are distinct.
Output
If it is impossible to choose signal power and a set of stations to meet all conditions, print -1.
Otherwise print two integers k and f in the first line β the number of stations in the chosen set and the chosen signal power, respectively. In the second line print k distinct integers from 1 to p β the indices of stations to establish contracts with (in any order). If there are multiple answers, print any of them; you don't have to minimize/maximize the number of chosen stations, and the same applies to signal power.
Examples
Input
2 4 4 2
1 3
2 3
1 4
1 2
3 4
1 4
1 2
3 4
Output
2 3
1 3
Input
2 4 4 2
1 3
2 4
1 2
1 2
3 4
3 4
1 2
3 4
Output
-1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area.
To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of n rows and m columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side.
Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo 10^9 + 7.
Input
The only line contains two integers n and m (1 β€ n, m β€ 100 000), the number of rows and the number of columns of the field.
Output
Print one integer, the number of random pictures modulo 10^9 + 7.
Example
Input
2 3
Output
8
Note
The picture below shows all possible random pictures of size 2 by 3.
<image>
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Let's call an array t dominated by value v in the next situation.
At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not.
Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not.
You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays.
The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, ..., a_j for some 1 β€ i β€ j β€ n.
Input
The first line contains single integer T (1 β€ T β€ 1000) β the number of test cases. Each test case consists of two lines.
The first line contains single integer n (1 β€ n β€ 2 β
10^5) β the length of the array a.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the corresponding values of the array a.
It's guaranteed that the total length of all arrays in one test doesn't exceed 2 β
10^5.
Output
Print T integers β one per test case. For each test case print the only integer β the length of the shortest dominated subarray, or -1 if there are no such subarrays.
Example
Input
4
1
1
6
1 2 3 4 5 1
9
4 1 2 4 5 4 3 2 1
4
3 3 3 3
Output
-1
6
3
2
Note
In the first test case, there are no subarrays of length at least 2, so the answer is -1.
In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray.
In the third test case, the subarray a_4, a_5, a_6 is the shortest dominated subarray.
In the fourth test case, all subarrays of length more than one are dominated.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Anu has created her own function f: f(x, y) = (x | y) - y where | denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR). For example, f(11, 6) = (11|6) - 6 = 15 - 6 = 9. It can be proved that for any nonnegative numbers x and y value of f(x, y) is also nonnegative.
She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.
A value of an array [a_1, a_2, ..., a_n] is defined as f(f(... f(f(a_1, a_2), a_3), ... a_{n-1}), a_n) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
Input
The first line contains a single integer n (1 β€ n β€ 10^5).
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 10^9). Elements of the array are not guaranteed to be different.
Output
Output n integers, the reordering of the array with maximum value. If there are multiple answers, print any.
Examples
Input
4
4 0 11 6
Output
11 6 4 0
Input
1
13
Output
13
Note
In the first testcase, value of the array [11, 6, 4, 0] is f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9.
[11, 4, 0, 6] is also a valid answer.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students.
The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher).
Your task is to find the number of good pairs of topics.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of topics.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the interestingness of the i-th topic for the teacher.
The third line of the input contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the interestingness of the i-th topic for the students.
Output
Print one integer β the number of good pairs of topic.
Examples
Input
5
4 8 2 6 2
4 5 4 1 3
Output
7
Input
4
1 3 2 4
1 3 2 4
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Recall that the sequence b is a a subsequence of the sequence a if b can be derived from a by removing zero or more elements without changing the order of the remaining elements. For example, if a=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2].
You are given a sequence a consisting of n positive and negative elements (there is no zeros in the sequence).
Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.
In other words, if the maximum length of alternating subsequence is k then your task is to find the maximum sum of elements of some alternating subsequence of length k.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a. The second line of the test case contains n integers a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9, a_i β 0), where a_i is the i-th element of a.
It is guaranteed that the sum of n over all test cases does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer β the maximum sum of the maximum by size (length) alternating subsequence of a.
Example
Input
4
5
1 2 3 -1 -2
4
-1 -2 -1 -3
10
-2 8 3 8 -4 -15 5 -2 -3 1
6
1 -1000000000 1 -1000000000 1 -1000000000
Output
2
-1
6
-2999999997
Note
In the first test case of the example, one of the possible answers is [1, 2, \underline{3}, \underline{-1}, -2].
In the second test case of the example, one of the possible answers is [-1, -2, \underline{-1}, -3].
In the third test case of the example, one of the possible answers is [\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}].
In the fourth test case of the example, one of the possible answers is [\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}].
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Vivek has encountered a problem. He has a maze that can be represented as an n Γ m grid. Each of the grid cells may represent the following:
* Empty β '.'
* Wall β '#'
* Good person β 'G'
* Bad person β 'B'
The only escape from the maze is at cell (n, m).
A person can move to a cell only if it shares a side with their current cell and does not contain a wall. Vivek wants to block some of the empty cells by replacing them with walls in such a way, that all the good people are able to escape, while none of the bad people are able to. A cell that initially contains 'G' or 'B' cannot be blocked and can be travelled through.
Help him determine if there exists a way to replace some (zero or more) empty cells with walls to satisfy the above conditions.
It is guaranteed that the cell (n,m) is empty. Vivek can also block this cell.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n, m (1 β€ n, m β€ 50) β the number of rows and columns in the maze.
Each of the next n lines contain m characters. They describe the layout of the maze. If a character on a line equals '.', the corresponding cell is empty. If it equals '#', the cell has a wall. 'G' corresponds to a good person and 'B' corresponds to a bad person.
Output
For each test case, print "Yes" if there exists a way to replace some empty cells with walls to satisfy the given conditions. Otherwise print "No"
You may print every letter in any case (upper or lower).
Example
Input
6
1 1
.
1 2
G.
2 2
#B
G.
2 3
G.#
B#.
3 3
#B.
#..
GG.
2 2
#B
B.
Output
Yes
Yes
No
No
Yes
Yes
Note
For the first and second test cases, all conditions are already satisfied.
For the third test case, there is only one empty cell (2,2), and if it is replaced with a wall then the good person at (1,2) will not be able to escape.
For the fourth test case, the good person at (1,1) cannot escape.
For the fifth test case, Vivek can block the cells (2,3) and (2,2).
For the last test case, Vivek can block the destination cell (2, 2).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a string s[1 ... n] consisting of lowercase Latin letters. It is guaranteed that n = 2^k for some integer k β₯ 0.
The string s[1 ... n] is called c-good if at least one of the following three conditions is satisfied:
* The length of s is 1, and it consists of the character c (i.e. s_1=c);
* The length of s is greater than 1, the first half of the string consists of only the character c (i.e. s_1=s_2=...=s_{n/2}=c) and the second half of the string (i.e. the string s_{n/2 + 1}s_{n/2 + 2} ... s_n) is a (c+1)-good string;
* The length of s is greater than 1, the second half of the string consists of only the character c (i.e. s_{n/2 + 1}=s_{n/2 + 2}=...=s_n=c) and the first half of the string (i.e. the string s_1s_2 ... s_{n/2}) is a (c+1)-good string.
For example: "aabc" is 'a'-good, "ffgheeee" is 'e'-good.
In one move, you can choose one index i from 1 to n and replace s_i with any lowercase Latin letter (any character from 'a' to 'z').
Your task is to find the minimum number of moves required to obtain an 'a'-good string from s (i.e. c-good string for c= 'a'). It is guaranteed that the answer always exists.
You have to answer t independent test cases.
Another example of an 'a'-good string is as follows. Consider the string s = "cdbbaaaa". It is an 'a'-good string, because:
* the second half of the string ("aaaa") consists of only the character 'a';
* the first half of the string ("cdbb") is 'b'-good string, because:
* the second half of the string ("bb") consists of only the character 'b';
* the first half of the string ("cd") is 'c'-good string, because:
* the first half of the string ("c") consists of only the character 'c';
* the second half of the string ("d") is 'd'-good string.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 131~072) β the length of s. It is guaranteed that n = 2^k for some integer k β₯ 0. The second line of the test case contains the string s consisting of n lowercase Latin letters.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer β the minimum number of moves required to obtain an 'a'-good string from s (i.e. c-good string with c = 'a'). It is guaranteed that the answer exists.
Example
Input
6
8
bbdcaaaa
8
asdfghjk
8
ceaaaabb
8
bbaaddcc
1
z
2
ac
Output
0
7
4
5
1
1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Igor had a sequence d_1, d_2, ..., d_n of integers. When Igor entered the classroom there was an integer x written on the blackboard.
Igor generated sequence p using the following algorithm:
1. initially, p = [x];
2. for each 1 β€ i β€ n he did the following operation |d_i| times:
* if d_i β₯ 0, then he looked at the last element of p (let it be y) and appended y + 1 to the end of p;
* if d_i < 0, then he looked at the last element of p (let it be y) and appended y - 1 to the end of p.
For example, if x = 3, and d = [1, -1, 2], p will be equal [3, 4, 3, 4, 5].
Igor decided to calculate the length of the longest increasing subsequence of p and the number of them.
A sequence a is a subsequence of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
A sequence a is an increasing sequence if each element of a (except the first one) is strictly greater than the previous element.
For p = [3, 4, 3, 4, 5], the length of longest increasing subsequence is 3 and there are 3 of them: [\underline{3}, \underline{4}, 3, 4, \underline{5}], [\underline{3}, 4, 3, \underline{4}, \underline{5}], [3, 4, \underline{3}, \underline{4}, \underline{5}].
Input
The first line contains a single integer n (1 β€ n β€ 50) β the length of the sequence d.
The second line contains a single integer x (-10^9 β€ x β€ 10^9) β the integer on the blackboard.
The third line contains n integers d_1, d_2, β¦, d_n (-10^9 β€ d_i β€ 10^9).
Output
Print two integers:
* the first integer should be equal to the length of the longest increasing subsequence of p;
* the second should be equal to the number of them modulo 998244353.
You should print only the second number modulo 998244353.
Examples
Input
3
3
1 -1 2
Output
3 3
Input
3
100
5 -3 6
Output
9 7
Input
3
1
999999999 0 1000000000
Output
2000000000 1
Input
5
34
1337 -146 42 -69 228
Output
1393 3876
Note
The first test case was explained in the statement.
In the second test case p = [100, 101, 102, 103, 104, 105, 104, 103, 102, 103, 104, 105, 106, 107, 108].
In the third test case p = [1, 2, β¦, 2000000000].
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i).
Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth?
Input
The first line contains two integers n and m (1 β€ n β€ 105, 0 β€ m β€ n) β the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 β€ ai β€ n).
It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth.
Output
Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime.
Examples
Input
1 1
+1
Output
Truth
Input
3 2
-1
-2
-3
Output
Not defined
Not defined
Not defined
Input
4 1
+2
-3
+4
-1
Output
Lie
Not defined
Lie
Not defined
Note
The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth.
In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not.
In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series.
There are n single men and n single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as k triples of numbers (h, w, r), where h is the index of the man, w is the index of the woman, and r is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot.
The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the k marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set.
Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value β it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the t-th set from the sorted list. Thus, t = 1 corresponds to a plot without marriages, t = 2 β to a single marriage resulting in minimal delight for the audience, and so on.
Help the Beaver to implement the algorithm for selecting the desired set.
Input
The first input line contains integers n, k and t (1 β€ k β€ min(100, n2), 1 β€ t β€ 2Β·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 β€ h, w β€ n; 1 β€ r β€ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t doesn't exceed the total number of acceptable sets, and each pair (h, w) is present in at most one triple.
The input limitations for getting 30 points are:
* 1 β€ n β€ 5
The input limitations for getting 100 points are:
* 1 β€ n β€ 20
Output
Print a single number β the value of the t-th acceptable variant.
Examples
Input
2 4 3
1 1 1
1 2 2
2 1 3
2 2 7
Output
2
Input
2 4 7
1 1 1
1 2 2
2 1 3
2 2 7
Output
8
Note
The figure shows 7 acceptable sets of marriages that exist in the first sample.
<image>
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Qwerty the Ranger took up a government job and arrived on planet Mars. He should stay in the secret lab and conduct some experiments on bacteria that have funny and abnormal properties. The job isn't difficult, but the salary is high.
At the beginning of the first experiment there is a single bacterium in the test tube. Every second each bacterium in the test tube divides itself into k bacteria. After that some abnormal effects create b more bacteria in the test tube. Thus, if at the beginning of some second the test tube had x bacteria, then at the end of the second it will have kx + b bacteria.
The experiment showed that after n seconds there were exactly z bacteria and the experiment ended at this point.
For the second experiment Qwerty is going to sterilize the test tube and put there t bacteria. He hasn't started the experiment yet but he already wonders, how many seconds he will need to grow at least z bacteria. The ranger thinks that the bacteria will divide by the same rule as in the first experiment.
Help Qwerty and find the minimum number of seconds needed to get a tube with at least z bacteria in the second experiment.
Input
The first line contains four space-separated integers k, b, n and t (1 β€ k, b, n, t β€ 106) β the parameters of bacterial growth, the time Qwerty needed to grow z bacteria in the first experiment and the initial number of bacteria in the second experiment, correspondingly.
Output
Print a single number β the minimum number of seconds Qwerty needs to grow at least z bacteria in the tube.
Examples
Input
3 1 3 5
Output
2
Input
1 4 4 7
Output
3
Input
2 2 4 100
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
* he chooses two elements of the array ai, aj (i β j);
* he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
Input
The first line contains integer n (1 β€ n β€ 105) β the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| β€ 104) β the original array.
Output
Print a single integer β the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
Examples
Input
2
2 1
Output
1
Input
3
1 4 1
Output
3
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
A girl named Xenia has a cupboard that looks like an arc from ahead. The arc is made of a semicircle with radius r (the cupboard's top) and two walls of height h (the cupboard's sides). The cupboard's depth is r, that is, it looks like a rectangle with base r and height h + r from the sides. The figure below shows what the cupboard looks like (the front view is on the left, the side view is on the right).
<image>
Xenia got lots of balloons for her birthday. The girl hates the mess, so she wants to store the balloons in the cupboard. Luckily, each balloon is a sphere with radius <image>. Help Xenia calculate the maximum number of balloons she can put in her cupboard.
You can say that a balloon is in the cupboard if you can't see any part of the balloon on the left or right view. The balloons in the cupboard can touch each other. It is not allowed to squeeze the balloons or deform them in any way. You can assume that the cupboard's walls are negligibly thin.
Input
The single line contains two integers r, h (1 β€ r, h β€ 107).
Output
Print a single integer β the maximum number of balloons Xenia can put in the cupboard.
Examples
Input
1 1
Output
3
Input
1 2
Output
5
Input
2 1
Output
2
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We'll call a set of positive integers a beautiful if the following condition fulfills: for any prime p, if <image>, then <image>. In other words, if one number from the set is divisible by prime p, then at least half of numbers from the set is divisible by p.
Your task is to find any beautiful set, where the number of elements is equal to k and each element doesn't exceed 2k2.
Input
The first line contains integer k (10 β€ k β€ 5000) that shows how many numbers the required beautiful set should have.
Output
In the first line print k space-separated integers that are a beautiful set. If there are multiple such sets, you are allowed to print any of them.
Examples
Input
10
Output
16 18 24 27 36 48 54 72 108 144
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Fox Ciel wants to write a task for a programming contest. The task is: "You are given a simple undirected graph with n vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2."
Same with some writers, she wants to make an example with some certain output: for example, her birthday or the number of her boyfriend. Can you help her to make a test case with answer equal exactly to k?
Input
The first line contains a single integer k (1 β€ k β€ 109).
Output
You should output a graph G with n vertexes (2 β€ n β€ 1000). There must be exactly k shortest paths between vertex 1 and vertex 2 of the graph.
The first line must contain an integer n. Then adjacency matrix G with n rows and n columns must follow. Each element of the matrix must be 'N' or 'Y'. If Gij is 'Y', then graph G has a edge connecting vertex i and vertex j. Consider the graph vertexes are numbered from 1 to n.
The graph must be undirected and simple: Gii = 'N' and Gij = Gji must hold. And there must be at least one path between vertex 1 and vertex 2. It's guaranteed that the answer exists. If there multiple correct answers, you can output any of them.
Examples
Input
2
Output
4
NNYY
NNYY
YYNN
YYNN
Input
9
Output
8
NNYYYNNN
NNNNNYYY
YNNNNYYY
YNNNNYYY
YNNNNYYY
NYYYYNNN
NYYYYNNN
NYYYYNNN
Input
1
Output
2
NY
YN
Note
In first example, there are 2 shortest paths: 1-3-2 and 1-4-2.
In second example, there are 9 shortest paths: 1-3-6-2, 1-3-7-2, 1-3-8-2, 1-4-6-2, 1-4-7-2, 1-4-8-2, 1-5-6-2, 1-5-7-2, 1-5-8-2.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S:
* its elements were distinct integers from 1 to limit;
* the value of <image> was equal to sum; here lowbit(x) equals 2k where k is the position of the first one in the binary representation of x. For example, lowbit(100102) = 102, lowbit(100012) = 12, lowbit(100002) = 100002 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
Input
The first line contains two integers: sum, limit (1 β€ sum, limit β€ 105).
Output
In the first line print an integer n (1 β€ n β€ 105), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
Examples
Input
5 5
Output
2
4 5
Input
4 3
Output
3
2 3 1
Input
5 1
Output
-1
Note
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible.
Input
The first line contains one integer n (1 β€ n β€ 100) β the number of known events. Then follow n lines containing two integers li and ri each (1 β€ li β€ ri β€ 107) β the earliest acceptable date and the latest acceptable date of the i-th event.
Output
Print n numbers β the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists.
Examples
Input
3
1 2
2 3
3 4
Output
1 2 3
Input
2
1 3
1 3
Output
1 2
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a n Γ m field consisting only of periods ('.') and asterisks ('*'). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of '*'-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle).
Input
The first line contains two positive integer numbers n and m (1 β€ n, m β€ 1000). The following n lines consist of m characters each, describing the field. Only '.' and '*' are allowed.
Output
Output a single number β total number of square triangles in the field. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
2 2
**
*.
Output
1
Input
3 4
*..*
.**.
*.**
Output
9
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Petya loves computer games. Finally a game that he's been waiting for so long came out!
The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ββof <image> for all i from 1 to n. The expression β xβ denotes the result of rounding the number x down to the nearest integer.
At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.
Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
Input
The first line of the input contains two positive integers n and k (1 β€ n β€ 105, 0 β€ k β€ 107) β the number of skills of the character and the number of units of improvements at Petya's disposal.
The second line of the input contains a sequence of n integers ai (0 β€ ai β€ 100), where ai characterizes the level of the i-th skill of the character.
Output
The first line of the output should contain a single non-negative integer β the maximum total rating of the character that Petya can get using k or less improvement units.
Examples
Input
2 4
7 9
Output
2
Input
3 8
17 15 19
Output
5
Input
2 2
99 100
Output
20
Note
In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor + lfloor frac{100}{10} rfloor = 10 + 10 = 20.
In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is <image>.
In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to <image>.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, there exists a positive integer i, such that si = b. Of course, you are the person he asks for a help.
Input
The first line of the input contain three integers a, b and c ( - 109 β€ a, b, c β€ 109) β the first element of the sequence, Vasya's favorite number and the difference between any two neighbouring elements of the sequence, respectively.
Output
If b appears in the sequence s print "YES" (without quotes), otherwise print "NO" (without quotes).
Examples
Input
1 7 3
Output
YES
Input
10 10 0
Output
YES
Input
1 -4 5
Output
NO
Input
0 60 50
Output
NO
Note
In the first sample, the sequence starts from integers 1, 4, 7, so 7 is its element.
In the second sample, the favorite integer of Vasya is equal to the first element of the sequence.
In the third sample all elements of the sequence are greater than Vasya's favorite integer.
In the fourth sample, the sequence starts from 0, 50, 100, and all the following elements are greater than Vasya's favorite integer.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a description of a depot. It is a rectangular checkered field of n Γ m size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*").
You have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the column y.
You are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and columns in the depot field.
The next n lines contain m symbols "." and "*" each β the description of the field. j-th symbol in i-th of them stands for cell (i, j). If the symbol is equal to ".", then the corresponding cell is empty, otherwise it equals "*" and the corresponding cell is occupied by a wall.
Output
If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print "NO" in the first line (without quotes).
Otherwise print "YES" (without quotes) in the first line and two integers in the second line β the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them.
Examples
Input
3 4
.*..
....
.*..
Output
YES
1 2
Input
3 3
..*
.*.
*..
Output
NO
Input
6 5
..*..
..*..
*****
..*..
..*..
..*..
Output
YES
3 3
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them.
Vasya lays out nm cards as a rectangle n Γ m. If there are jokers among them, then Vasya should change them with some of the rest of 54 - nm cards (which are not layed out) so that there were no jokers left. Vasya can pick the cards to replace the jokers arbitrarily. Remember, that each card presents in pack exactly once (i. e. in a single copy). Vasya tries to perform the replacements so that the solitaire was solved.
Vasya thinks that the solitaire is solved if after the jokers are replaced, there exist two non-overlapping squares 3 Γ 3, inside each of which all the cards either have the same suit, or pairwise different ranks.
Determine by the initial position whether the solitaire can be solved or not. If it can be solved, show the way in which it is possible.
Input
The first line contains integers n and m (3 β€ n, m β€ 17, n Γ m β€ 52). Next n lines contain m words each. Each word consists of two letters. The jokers are defined as "J1" and "J2" correspondingly. For the rest of the cards, the first letter stands for the rank and the second one β for the suit. The possible ranks are: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" and "A". The possible suits are: "C", "D", "H" and "S". All the cards are different.
Output
If the Solitaire can be solved, print on the first line "Solution exists." without the quotes. On the second line print in what way the jokers can be replaced. Three variants are possible:
* "There are no jokers.", if there are no jokers in the input data.
* "Replace Jx with y.", if there is one joker. x is its number, and y is the card it should be replaced with.
* "Replace J1 with x and J2 with y.", if both jokers are present in the input data. x and y here represent distinct cards with which one should replace the first and the second jokers correspondingly.
On the third line print the coordinates of the upper left corner of the first square 3 Γ 3 in the format "Put the first square to (r, c).", where r and c are the row and the column correspondingly. In the same manner print on the fourth line the coordinates of the second square 3 Γ 3 in the format "Put the second square to (r, c).".
If there are several solutions to that problem, print any of them.
If there are no solutions, print of the single line "No solution." without the quotes.
See the samples to understand the output format better.
Examples
Input
4 6
2S 3S 4S 7S 8S AS
5H 6H 7H 5S TC AC
8H 9H TH 7C 8C 9C
2D 2C 3C 4C 5C 6C
Output
No solution.
Input
4 6
2S 3S 4S 7S 8S AS
5H 6H 7H J1 TC AC
8H 9H TH 7C 8C 9C
2D 2C 3C 4C 5C 6C
Output
Solution exists.
Replace J1 with 2H.
Put the first square to (1, 1).
Put the second square to (2, 4).
Input
4 6
2S 3S 4S 7S 8S AS
5H 6H 7H QC TC AC
8H 9H TH 7C 8C 9C
2D 2C 3C 4C 5C 6C
Output
Solution exists.
There are no jokers.
Put the first square to (1, 1).
Put the second square to (2, 4).
Note
The pretests cover all the possible output formats.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime m. Also, Timofey likes to look for arithmetical progressions everywhere.
One of his birthday presents was a sequence of distinct integers a1, a2, ..., an. Timofey wants to know whether he can rearrange the elements of the sequence so that is will be an arithmetical progression modulo m, or not.
Arithmetical progression modulo m of length n with first element x and difference d is sequence of integers x, x + d, x + 2d, ..., x + (n - 1)Β·d, each taken modulo m.
Input
The first line contains two integers m and n (2 β€ m β€ 109 + 7, 1 β€ n β€ 105, m is prime) β Timofey's favorite prime module and the length of the sequence.
The second line contains n distinct integers a1, a2, ..., an (0 β€ ai < m) β the elements of the sequence.
Output
Print -1 if it is not possible to rearrange the elements of the sequence so that is will be an arithmetical progression modulo m.
Otherwise, print two integers β the first element of the obtained progression x (0 β€ x < m) and its difference d (0 β€ d < m).
If there are multiple answers, print any of them.
Examples
Input
17 5
0 2 4 13 15
Output
13 2
Input
17 5
0 2 4 13 14
Output
-1
Input
5 3
1 2 3
Output
3 4
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
<image>
It's the end of July β the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in the kingdom of Sweetland in case it turns out to reach the wrong hands. So it's a necessity to not let any uninvited guests in.
There are 26 entrances in Jelly Castle, enumerated with uppercase English letters from A to Z. Because of security measures, each guest is known to be assigned an entrance he should enter the castle through. The door of each entrance is opened right before the first guest's arrival and closed right after the arrival of the last guest that should enter the castle through this entrance. No two guests can enter the castle simultaneously.
For an entrance to be protected from possible intrusion, a candy guard should be assigned to it. There are k such guards in the castle, so if there are more than k opened doors, one of them is going to be left unguarded! Notice that a guard can't leave his post until the door he is assigned to is closed.
Slastyona had a suspicion that there could be uninvited guests at the evening. She knows the order in which the invited guests entered the castle, and wants you to help her check whether there was a moment when more than k doors were opened.
Input
Two integers are given in the first string: the number of guests n and the number of guards k (1 β€ n β€ 106, 1 β€ k β€ 26).
In the second string, n uppercase English letters s1s2... sn are given, where si is the entrance used by the i-th guest.
Output
Output Β«YESΒ» if at least one door was unguarded during some time, and Β«NOΒ» otherwise.
You can output each letter in arbitrary case (upper or lower).
Examples
Input
5 1
AABBB
Output
NO
Input
5 1
ABABB
Output
YES
Note
In the first sample case, the door A is opened right before the first guest's arrival and closed when the second guest enters the castle. The door B is opened right before the arrival of the third guest, and closed after the fifth one arrives. One guard can handle both doors, as the first one is closed before the second one is opened.
In the second sample case, the door B is opened before the second guest's arrival, but the only guard can't leave the door A unattended, as there is still one more guest that should enter the castle through this door.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well.
In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an arbitrary sequence of these operations with arbitrary constants from 0 to 1023. When the program is run, all operations are applied (in the given order) to the argument and in the end the result integer is returned.
Petya wrote a program in this language, but it turned out to be too long. Write a program in CALPAS that does the same thing as the Petya's program, and consists of no more than 5 lines. Your program should return the same integer as Petya's program for all arguments from 0 to 1023.
Input
The first line contains an integer n (1 β€ n β€ 5Β·105) β the number of lines.
Next n lines contain commands. A command consists of a character that represents the operation ("&", "|" or "^" for AND, OR or XOR respectively), and the constant xi 0 β€ xi β€ 1023.
Output
Output an integer k (0 β€ k β€ 5) β the length of your program.
Next k lines must contain commands in the same format as in the input.
Examples
Input
3
| 3
^ 2
| 1
Output
2
| 3
^ 2
Input
3
& 1
& 3
& 5
Output
1
& 1
Input
3
^ 1
^ 2
^ 3
Output
0
Note
You can read about bitwise operations in <https://en.wikipedia.org/wiki/Bitwise_operation>.
Second sample:
Let x be an input of the Petya's program. It's output is ((x&1)&3)&5 = x&(1&3&5) = x&1. So these two programs always give the same outputs.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In the year of 30XX participants of some world programming championship live in a single large hotel. The hotel has n floors. Each floor has m sections with a single corridor connecting all of them. The sections are enumerated from 1 to m along the corridor, and all sections with equal numbers on different floors are located exactly one above the other. Thus, the hotel can be represented as a rectangle of height n and width m. We can denote sections with pairs of integers (i, j), where i is the floor, and j is the section number on the floor.
The guests can walk along the corridor on each floor, use stairs and elevators. Each stairs or elevator occupies all sections (1, x), (2, x), β¦, (n, x) for some x between 1 and m. All sections not occupied with stairs or elevators contain guest rooms. It takes one time unit to move between neighboring sections on the same floor or to move one floor up or down using stairs. It takes one time unit to move up to v floors in any direction using an elevator. You can assume you don't have to wait for an elevator, and the time needed to enter or exit an elevator is negligible.
You are to process q queries. Each query is a question "what is the minimum time needed to go from a room in section (x_1, y_1) to a room in section (x_2, y_2)?"
Input
The first line contains five integers n, m, c_l, c_e, v (2 β€ n, m β€ 10^8, 0 β€ c_l, c_e β€ 10^5, 1 β€ c_l + c_e β€ m - 1, 1 β€ v β€ n - 1) β the number of floors and section on each floor, the number of stairs, the number of elevators and the maximum speed of an elevator, respectively.
The second line contains c_l integers l_1, β¦, l_{c_l} in increasing order (1 β€ l_i β€ m), denoting the positions of the stairs. If c_l = 0, the second line is empty.
The third line contains c_e integers e_1, β¦, e_{c_e} in increasing order, denoting the elevators positions in the same format. It is guaranteed that all integers l_i and e_i are distinct.
The fourth line contains a single integer q (1 β€ q β€ 10^5) β the number of queries.
The next q lines describe queries. Each of these lines contains four integers x_1, y_1, x_2, y_2 (1 β€ x_1, x_2 β€ n, 1 β€ y_1, y_2 β€ m) β the coordinates of starting and finishing sections for the query. It is guaranteed that the starting and finishing sections are distinct. It is also guaranteed that these sections contain guest rooms, i. e. y_1 and y_2 are not among l_i and e_i.
Output
Print q integers, one per line β the answers for the queries.
Example
Input
5 6 1 1 3
2
5
3
1 1 5 6
1 3 5 4
3 3 5 3
Output
7
5
4
Note
In the first query the optimal way is to go to the elevator in the 5-th section in four time units, use it to go to the fifth floor in two time units and go to the destination in one more time unit.
In the second query it is still optimal to use the elevator, but in the third query it is better to use the stairs in the section 2.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard.
Initially, you have an empty string. Until you type the whole string, you may perform the following operation:
* add a character to the end of the string.
Besides, at most once you may perform one additional operation: copy the string and append it to itself.
For example, if you have to type string abcabca, you can type it in 7 operations if you type all the characters one by one. However, you can type it in 5 operations if you type the string abc first and then copy it and type the last character.
If you have to type string aaaaaaaaa, the best option is to type 4 characters one by one, then copy the string, and then type the remaining character.
Print the minimum number of operations you need to type the given string.
Input
The first line of the input containing only one integer number n (1 β€ n β€ 100) β the length of the string you have to type. The second line containing the string s consisting of n lowercase Latin letters.
Output
Print one integer number β the minimum number of operations you need to type the given string.
Examples
Input
7
abcabca
Output
5
Input
8
abcdefgh
Output
8
Note
The first test described in the problem statement.
In the second test you can only type all the characters one by one.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
A necklace can be described as a string of links ('-') and pearls ('o'), with the last link or pearl connected to the first one.
<image>
You can remove a link or a pearl and insert it between two other existing links or pearls (or between a link and a pearl) on the necklace. This process can be repeated as many times as you like, but you can't throw away any parts.
Can you make the number of links between every two adjacent pearls equal? Two pearls are considered to be adjacent if there is no other pearl between them.
Note that the final necklace should remain as one circular part of the same length as the initial necklace.
Input
The only line of input contains a string s (3 β€ |s| β€ 100), representing the necklace, where a dash '-' represents a link and the lowercase English letter 'o' represents a pearl.
Output
Print "YES" if the links and pearls can be rejoined such that the number of links between adjacent pearls is equal. Otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
<span class="tex-font-style-tt">-o-o--</span>
Output
YES
Input
<span class="tex-font-style-tt">-o---</span>
Output
YES
Input
<span class="tex-font-style-tt">-o---o-</span>
Output
NO
Input
ooo
Output
YES
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Yakko, Wakko and Dot, world-famous animaniacs, decided to rest from acting in cartoons, and take a leave to travel a bit. Yakko dreamt to go to Pennsylvania, his Motherland and the Motherland of his ancestors. Wakko thought about Tasmania, its beaches, sun and sea. Dot chose Transylvania as the most mysterious and unpredictable place.
But to their great regret, the leave turned to be very short, so it will be enough to visit one of the three above named places. That's why Yakko, as the cleverest, came up with a truly genius idea: let each of the three roll an ordinary six-sided die, and the one with the highest amount of points will be the winner, and will take the other two to the place of his/her dreams.
Yakko thrown a die and got Y points, Wakko β W points. It was Dot's turn. But she didn't hurry. Dot wanted to know for sure what were her chances to visit Transylvania.
It is known that Yakko and Wakko are true gentlemen, that's why if they have the same amount of points with Dot, they will let Dot win.
Input
The only line of the input file contains two natural numbers Y and W β the results of Yakko's and Wakko's die rolls.
Output
Output the required probability in the form of irreducible fraction in format Β«A/BΒ», where A β the numerator, and B β the denominator. If the required probability equals to zero, output Β«0/1Β». If the required probability equals to 1, output Β«1/1Β».
Examples
Input
4 2
Output
1/2
Note
Dot will go to Transylvania, if she is lucky to roll 4, 5 or 6 points.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Problem:
N boxes are arranged systematically in a circle and numbered from 1 to N in increasing order in clockwise direction. There will be Q queries asking you whether boxes i and j can be connected by a straight rod such that this rod does not intersect ( criss - cross as seen from top view ) with any of the other rods used to join other boxes in the previous queries. If the rod does not intersect with any of the other rods, the boxes i and j will be connected by the rod , or else the connection will not be made. Every box can be connected to at most one other box. No box can be connected to itself. Infinitley many rods of all possible sizes are available.
Input:
First line comprises of two space separated integers N and Q. Then there will be Q lines, each line will consist of two space separated integers i and j.
Output:
Print the answer to each query , 'YES' if the connection can be made and 'NO' if the connection cannot be made.
Constraints:
2 β€ N β€ 1000
1 β€ Q β€ 1000
1 β€ i,j β€ N
SAMPLE INPUT
10 7
1 5
2 7
2 3
2 4
9 9
10 9
8 6SAMPLE OUTPUT
YES
NO
YES
NO
NO
YES
YES
Explanation
1 and 5 can be connected by a rod.
2 and 7 cannot be connected by a rod because this rod intersects with the rod connecting 1 and 5.
2 and 3 can be connected.
2 and 4 cannot be connected as 2 is already connected to 3.
9 and 9 cannot be connected as no box can be connected to itself.
10 and 9 can be connected.
8 and 6 can be connected.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Little Arjit is in love with Deepa. They have always thought of themselves as the ideal couple - the best, possible match they could've managed. (No kidding!) And like every other couple, they promised each other not to fight after every other fight. But, when has that happened before?
But, this is a different couple - this is a programming couple - and they argue on weird things, like Fibonacci numbers, prime numbers, Sterling numbers, and what not!
Their recent fight might seem silly to a lot of people, but it is a matter of serious concern for both of them. They have bought a cake, and they weighed it in milligrams - the weight of the cake is always even and now they wish to divide the cake between them in some way, that both of them are satisfied.
Arjit challenges Deepa that if she can divide the weight of the cake as sum of two prime numbers between them, she can have the entire cake - and if she fails to do so, he'll get the cake.
The argument is getting more, and more heated now - please help them sort out their stupid arguments or an easier way would be to help them figure out who is going to have the cake.
Input Format:
The first line will contain a number, tc, denoting the number of test cases.
The next tc lines will contain an even number, denoting the weight of the cake in milligrams.
Output Format:
Print "Arjit" or "Deepa" according to the winner.
Constraints:
1 β€ tc β€ 100
1 β€ n β€ 100000
1 is NOT a prime number.
SAMPLE INPUT
2
4
8
SAMPLE OUTPUT
Deepa
Deepa
Explanation
4 can be represented as 2 + 2, so Deepa wins.
8 can be represented as 5 + 3, so Deepa wins.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Rajat is a guy who always wants to live in a fantasy world. In his world,
a deck consists of X cards, where X is a multiple of 4.
There are four types of cards namely spade , heart , club and diamond with
each type having values in range of [1,X/4] inclusive.
His cousin wants to test him. So,to test Rajat , his
cousin gives him a value of a card D and its card type K.
He asks him to find the probability that the above card is drawn
at the N^th draw if cards are drawn one by one from the deck without
replacement.
Input:
First line contains T the number of testcases.
Next T lines consists of X , N , D , K.
Output:
Output the probability upto 10 digits after decimal place for each testcase.
Constraints:
1 β€ T β€100
4 β€ X β€ 10^9
1 β€ N β€ X
K is a string denoting card's type = {heart , spade , diamond , club}
1 β€ D β€ X/4
SAMPLE INPUT
2
8 8 2 heart
52 1 10 spade
SAMPLE OUTPUT
0.1250000000
0.0192307692
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The cat Snuke wants to play a popular Japanese game called Γ
tCoder, so Iroha has decided to teach him Japanese.
When counting pencils in Japanese, the counter word "ζ¬" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "ζ¬" in the phrase "N ζ¬" for a positive integer N not exceeding 999 is as follows:
* `hon` when the digit in the one's place of N is 2, 4, 5, 7, or 9;
* `pon` when the digit in the one's place of N is 0, 1, 6 or 8;
* `bon` when the digit in the one's place of N is 3.
Given N, print the pronunciation of "ζ¬" in the phrase "N ζ¬".
Constraints
* N is a positive integer not exceeding 999.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Examples
Input
16
Output
pon
Input
2
Output
hon
Input
183
Output
bon
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is `#`, and a "road" square if S_{ij} is `.`.
From a road square, you can move to a horizontally or vertically adjacent road square.
You cannot move out of the maze, move to a wall square, or move diagonally.
Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.
Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.
In this situation, find the maximum possible number of moves Aoki has to make.
Constraints
* 1 \leq H,W \leq 20
* S_{ij} is `.` or `#`.
* S contains at least two occurrences of `.`.
* Any road square can be reached from any road square in zero or more moves.
Input
Input is given from Standard Input in the following format:
H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}
Output
Print the maximum possible number of moves Aoki has to make.
Examples
Input
3 3
...
...
...
Output
4
Input
3 5
...#.
.#.#.
.#...
Output
10
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Given is a string S consisting of lowercase English letters. Find the maximum positive integer K that satisfies the following condition:
* There exists a partition of S into K non-empty strings S=S_1S_2...S_K such that S_i \neq S_{i+1} (1 \leq i \leq K-1).
Here S_1S_2...S_K represents the concatenation of S_1,S_2,...,S_K in this order.
Constraints
* 1 \leq |S| \leq 2 \times 10^5
* S consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S
Output
Print the maximum positive integer K that satisfies the condition.
Examples
Input
aabbaa
Output
4
Input
aaaccacabaababc
Output
12
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The restaurant AtCoder serves the following five dishes:
* ABC Don (rice bowl): takes A minutes to serve.
* ARC Curry: takes B minutes to serve.
* AGC Pasta: takes C minutes to serve.
* APC Ramen: takes D minutes to serve.
* ATC Hanbagu (hamburger patty): takes E minutes to serve.
Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.
This restaurant has the following rules on orders:
* An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).
* Only one dish can be ordered at a time.
* No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.
E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.
Here, he can order the dishes in any order he likes, and he can place an order already at time 0.
Constraints
* A, B, C, D and E are integers between 1 and 123 (inclusive).
Input
Input is given from Standard Input in the following format:
A
B
C
D
E
Output
Print the earliest possible time for the last dish to be delivered, as an integer.
Examples
Input
29
20
7
35
120
Output
215
Input
101
86
119
108
57
Output
481
Input
123
123
123
123
123
Output
643
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Niwango-kun has \\(N\\) chickens as his pets. The chickens are identified by numbers \\(1\\) to \\(N\\), and the size of the \\(i\\)-th chicken is a positive integer \\(a_i\\).
\\(N\\) chickens decided to take each other's hand (wing) and form some cycles. The way to make cycles is represented by a permutation \\(p\\) of \\(1, \ldots , N\\). Chicken \\(i\\) takes chicken \\(p_i\\)'s left hand by its right hand. Chickens may take their own hand.
Let us define the cycle containing chicken \\(i\\) as the set consisting of chickens \\(p_i, p_{p_i}, \ldots, p_{\ddots_i} = i\\). It can be proven that after each chicken takes some chicken's hand, the \\(N\\) chickens can be decomposed into cycles.
The beauty \\(f(p)\\) of a way of forming cycles is defined as the product of the size of the smallest chicken in each cycle. Let \\(b_i \ (1 \leq i \leq N)\\) be the sum of \\(f(p)\\) among all possible permutations \\(p\\) for which \\(i\\) cycles are formed in the procedure above.
Find the greatest common divisor of \\(b_1, b_2, \ldots, b_N\\) and print it \\({\rm mod} \ 998244353\\).
Constraints
* \\(1 \leq N \leq 10^5\\)
* \\(1 \leq a_i \leq 10^9\\)
* All numbers given in input are integers
Input
Input is given from Standard Input in the following format:
\(N\)
\(a_1\) \(a_2\) \(\ldots\) \(a_N\)
Output
Print the answer.
Examples
Input
2
4 3
Output
3
Input
4
2 5 2 5
Output
2
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Consider the following game:
* The game is played using a row of N squares and many stones.
* First, a_i stones are put in Square i\ (1 \leq i \leq N).
* A player can perform the following operation as many time as desired: "Select an integer i such that Square i contains exactly i stones. Remove all the stones from Square i, and add one stone to each of the i-1 squares from Square 1 to Square i-1."
* The final score of the player is the total number of the stones remaining in the squares.
For a sequence a of length N, let f(a) be the minimum score that can be obtained when the game is played on a.
Find the sum of f(a) over all sequences a of length N where each element is between 0 and K (inclusive). Since it can be extremely large, find the answer modulo 1000000007 (= 10^9+7).
Constraints
* 1 \leq N \leq 100
* 1 \leq K \leq N
Input
Input is given from Standard Input in the following format:
N K
Output
Print the sum of f(a) modulo 1000000007 (= 10^9+7).
Examples
Input
2 2
Output
10
Input
20 17
Output
983853488
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Enter a positive integer n of 4,000 or less, with a pair of integers a, b, c, d in the range 0-1000.
a + b + c + d = n
Create a program that outputs the number of combinations that satisfy the conditions.
Input
Given multiple datasets. Each dataset is given n on one row. Please process until the end of the input.
The number of datasets does not exceed 50.
Output
For each data set, output the number of combinations of a, b, c, and d on one line.
Example
Input
2
3
35
Output
10
20
8436
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The screen that displays digital numbers that you often see on calculators is called a "7-segment display" because the digital numbers consist of seven parts (segments).
The new product to be launched by Wakamatsu will incorporate a 7-segment display into the product, and as an employee, you will create a program to display the given number on the 7-segment display.
This 7-segment display will not change until the next switch instruction is sent. By sending a signal consisting of 7 bits, the display information of each corresponding segment can be switched. Bits have a value of 1 or 0, where 1 stands for "switch" and 0 stands for "as is".
The correspondence between each bit and segment is shown in the figure below. The signal sends 7 bits in the order of "gfedcba". For example, in order to display "0" from the hidden state, "0111111" must be sent to the display as a signal. To change from "0" to "5", send "1010010". If you want to change "5" to "1" in succession, send "1101011".
<image>
Create a program that takes n (1 β€ n β€ 100) numbers that you want to display and outputs the signal sequence required to correctly display those numbers di (0 β€ di β€ 9) on the 7-segment display. please. It is assumed that the initial state of the 7-segment display is all hidden.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of -1. Each dataset is given in the following format:
n
d1
d2
::
dn
The number of datasets does not exceed 120.
Output
For each input dataset, output the sequence of signals needed to properly output the numbers to the display.
Example
Input
3
0
5
1
1
0
-1
Output
0111111
1010010
1101011
0111111
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
$N$ sages are sitting around a round table with $N$ seats. Each sage holds chopsticks with his dominant hand to eat his dinner. The following happens in this situation.
* If sage $i$ is right-handed and a left-handed sage sits on his right, a level of frustration $w_i$ occurs to him. A right-handed sage on his right does not cause such frustration at all.
* If sage $i$ is left-handed and a right-handed sage sits on his left, a level of frustration $w_i$ occurs to him. A left-handed sage on his left does not cause such frustration at all.
You wish you could minimize the total amount of frustration by clever sitting order arrangement.
Given the number of sages with his dominant hand information, make a program to evaluate the minimum frustration achievable.
Input
The input is given in the following format.
$N$
$a_1$ $a_2$ $...$ $a_N$
$w_1$ $w_2$ $...$ $w_N$
The first line provides the number of sages $N$ ($3 \leq N \leq 10$). The second line provides an array of integers $a_i$ (0 or 1) which indicate if the $i$-th sage is right-handed (0) or left-handed (1). The third line provides an array of integers $w_i$ ($1 \leq w_i \leq 1000$) which indicate the level of frustration the $i$-th sage bears.
Output
Output the minimum total frustration the sages bear.
Examples
Input
5
1 0 0 1 0
2 3 5 1 2
Output
3
Input
3
0 0 0
1 2 3
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Dr. Asimov, a robotics researcher, loves to research, but hates houseworks and his house were really dirty. So, he has developed a cleaning robot.
As shown in the following figure, his house has 9 rooms, where each room is identified by an alphabet:
<image>
The robot he developed operates as follows:
* If the battery runs down, the robot stops.
* If not so, the robot chooses a direction from four cardinal points with the equal probability, and moves to the room in that direction. Then, the robot clean the room and consumes 1 point of the battery.
* However, if there is no room in that direction, the robot does not move and remains the same room. In addition, there is a junk room in the house where the robot can not enter, and the robot also remains when it tries to enter the junk room. The robot also consumes 1 point of the battery when it remains the same place.
A battery charger for the robot is in a room. It would be convenient for Dr. Asimov if the robot stops at the battery room when its battery run down.
Your task is to write a program which computes the probability of the robot stopping at the battery room.
Constraints
* Judge data includes at most 100 data sets.
* n β€ 15
* s, t, b are distinct.
Input
The input consists of several datasets. Each dataset consists of:
n
s t b
n is an integer that indicates the initial battery point. s, t, b are alphabets respectively represent the room where the robot is initially, the battery room, and the junk room.
The input ends with a dataset which consists of single 0 for n. Your program should not output for this dataset.
Output
For each dataset, print the probability as a floating point number in a line. The answer may not have an error greater than 0.000001.
Example
Input
1
E A C
1
E B C
2
E A B
0
Output
0.00000000
0.25000000
0.06250000
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Consider car trips in a country where there is no friction. Cars in this country do not have engines. Once a car started to move at a speed, it keeps moving at the same speed. There are acceleration devices on some points on the road, where a car can increase or decrease its speed by 1. It can also keep its speed there. Your job in this problem is to write a program which determines the route with the shortest time to travel from a starting city to a goal city.
There are several cities in the country, and a road network connecting them. Each city has an acceleration device. As mentioned above, if a car arrives at a city at a speed v , it leaves the city at one of v - 1, v , or v + 1. The first road leaving the starting city must be run at the speed 1. Similarly, the last road arriving at the goal city must be run at the speed 1.
The starting city and the goal city are given. The problem is to find the best route which leads to the goal city going through several cities on the road network. When the car arrives at a city, it cannot immediately go back the road it used to reach the city. No U-turns are allowed. Except this constraint, one can choose any route on the road network. It is allowed to visit the same city or use the same road multiple times. The starting city and the goal city may be visited during the trip.
For each road on the network, its distance and speed limit are given. A car must run a road at a speed less than or equal to its speed limit. The time needed to run a road is the distance divided by the speed. The time needed within cities including that for acceleration or deceleration should be ignored.
Input
The input consists of multiple datasets, each in the following format.
> n m
> s g
> x 1 y 1 d 1 c 1
> ...
> xm ym dm cm
>
Every input item in a dataset is a non-negative integer. Input items in the same line are separated by a space.
The first line gives the size of the road network. n is the number of cities in the network. You can assume that the number of cities is between 2 and 30, inclusive. m is the number of roads between cities, which may be zero.
The second line gives the trip. s is the city index of the starting city. g is the city index of the goal city. s is not equal to g . You can assume that all city indices in a dataset (including the above two) are between 1 and n , inclusive.
The following m lines give the details of roads between cities. The i -th road connects two cities with city indices xi and yi , and has a distance di (1 β€ i β€ m ). You can assume that the distance is between 1 and 100, inclusive. The speed limit of the road is specified by ci . You can assume that the speed limit is between 1 and 30, inclusive.
No two roads connect the same pair of cities. A road never connects a city with itself. Each road can be traveled in both directions.
The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset in the input, one line should be output as specified below. An output line should not contain extra characters such as spaces.
If one can travel from the starting city to the goal city, the time needed for the best route (a route with the shortest time) should be printed. The answer should not have an error greater than 0.001. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.
If it is impossible to reach the goal city, the string "`unreachable`" should be printed. Note that all the letters of "`unreachable`" are in lowercase.
Sample Input
2 0
1 2
5 4
1 5
1 2 1 1
2 3 2 2
3 4 2 2
4 5 1 1
6 6
1 6
1 2 2 1
2 3 2 1
3 6 2 1
1 4 2 30
4 5 3 30
5 6 2 30
6 7
1 6
1 2 1 30
2 3 1 30
3 1 1 30
3 4 100 30
4 5 1 30
5 6 1 30
6 4 1 30
0 0
Output for the Sample Input
unreachable
4.00000
5.50000
11.25664
Example
Input
2 0
1 2
5 4
1 5
1 2 1 1
2 3 2 2
3 4 2 2
4 5 1 1
6 6
1 6
1 2 2 1
2 3 2 1
3 6 2 1
1 4 2 30
4 5 3 30
5 6 2 30
6 7
1 6
1 2 1 30
2 3 1 30
3 1 1 30
3 4 100 30
4 5 1 30
5 6 1 30
6 4 1 30
0 0
Output
unreachable
4.00000
5.50000
11.25664
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Princess in Danger
Princess crisis
English text is not available in this practice contest.
A brave princess in a poor country's tomboy is married to another country for a political marriage. However, a villain who was trying to kill the princess attacked him on the way to his wife, and the princess was seriously injured and was taken to a nearby hospital. However, the villain used a special poison to make sure the princess was dead. Therefore, in order to help the princess, she had to hurry to bring special medicine and frozen relatives' blood from her home country.
This blood is transported frozen, but must be re-frozen in a blood freezing facility within up to M minutes of the previous freezing to keep it fresh. However, there are only a few places where refrigeration facilities are installed.
Blood is safe for M minutes from a fully frozen state without refreezing. If the remaining time without refrigeration is S minutes and the product is transported without freezing for T minutes, the remaining time without refrigeration is S-T minutes. The remaining time that does not require refreezing can be restored up to M minutes by refreezing. The time it takes to refreeze blood depends on how much it is frozen. Every minute the blood is frozen in a freezing facility, the remaining time that does not need to be re-frozen recovers by 1 minute.
At the time of departure from the capital of the home country, the remaining time without refrigerating the blood is M minutes.
As a princess's servant, you must calculate the route to keep the blood fresh from the capital of your home country to the hospital where the princess was transported, in order to save the life of your precious lord. Yes, your mission is to figure out the shortest route from your home capital to the hospital and find the shortest time.
Input
The input consists of multiple datasets. The first row of each dataset contains six non-negative integers N (2 β€ N β€ 100), M (1 β€ M β€ 100), L (0 β€ L β€ N-2), K, A (0 β€). A <N) and H (0 β€ H <N) are given. These are the number of towns, the time limit for refrigeration, the number of towns with freezing facilities, the number of roads connecting towns directly, the number representing the capital of the home country, and the hospital where the princess was transported. Represents the town number. The capital of the home country and the hospital where the princess was transported are different towns. It is assumed that the towns are assigned numbers from 0 to N-1. The following lines are given L non-negative integers separated by a single space. These represent the numbers of the towns where the freezing facilities are located. The capital of the home country and the hospital where the princess was transported are not included in this list, but it can be considered that there is a freezing facility. The following line K gives information on the roads connecting the towns. In the i-th line, three non-negative integers X, Y, and T are given separated by one space, which means that there is a direct connection between town X and town Y, and it takes time T to move. Represents that. This road is bidirectional. Also, there is at most one road that directly connects town X and town Y.
The input ends when N = M = L = K = A = H = 0, which is not included in the dataset.
Output
For each dataset, output the shortest time that blood can be delivered while maintaining its freshness. If it cannot be delivered, output "Help!".
Sample Input
2 1 0 1 0 1
0 1 2
3 1 1 2 0 1
2
0 2 1
1 2 1
3 2 1 2 0 1
2
0 2 1
1 2 1
4 4 1 4 1 3
2
0 1 2
1 2 4
0 2 1
3 0 3
5 3 2 6 0 3
1 2
2 1 2
1 0 1
3 4 1
2 4 1
4 1 2
2 0 2
5 4 2 6 0 3
1 2
4 2 4
2 1 2
4 3 1
0 1 5
1 4 2
2 0 3
0 0 0 0 0 0
Output for the Sample Input
Help!
3
2
Ten
Five
12
Example
Input
2 1 0 1 0 1
0 1 2
3 1 1 2 0 1
2
0 2 1
1 2 1
3 2 1 2 0 1
2
0 2 1
1 2 1
4 4 1 4 1 3
2
0 1 2
1 2 4
0 2 1
3 0 3
5 3 2 6 0 3
1 2
2 1 2
1 0 1
3 4 1
2 4 1
4 1 2
2 0 2
5 4 2 6 0 3
1 2
4 2 4
2 1 2
4 3 1
0 1 5
1 4 2
2 0 3
0 0 0 0 0 0
Output
Help!
3
2
10
5
12
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Problem statement
We played an AI soccer match between Country A and Country B. You have a table that records the player who had the ball at a certain time and its position. The table consists of N rows, and the i-th row from the top consists of the following elements.
* Number of frames f_i
* The uniform number of the player who has the ball a_i
* The team to which the player belongs t_i
* Coordinates representing the player's position x_i, y_i
The number of frames is an integer that is set to 0 at the start time of the game and is incremented by 1 every 1/60 second. For example, just 1.5 seconds after the game starts, the number of frames is 90. The uniform number is an integer uniquely assigned to 11 players in each team. Furthermore, in two consecutive records in the table, when players with different numbers on the same team have the ball, a "pass" is made between them. Frames that do not exist in the recording need not be considered.
Now, as an engineer, your job is to find the distance of the longest distance (Euclidean distance) between the players of each team and the time it took. If there are multiple paths with the longest distance, output the one with the shortest time.
input
The input is given in the following format. When t_i = 0, it represents country A, and when t_i = 1, it represents country B.
N
f_0 a_0 t_0 x_0 y_0
...
f_ {Nβ1} a_ {Nβ1} t_ {Nβ1} x_ {Nβ1} y_ {Nβ1}
Constraint
* All inputs are integers
* 1 \ β€ N \ β€ 100
* 0 \ β€ f_i \ lt f_ {i + 1} \ β€ 324 \,000
* 1 \ β€ a_i \ β€ 11
* t_i = 0,1
* 0 \ β€ x_i \ β€ 120
* 0 \ β€ y_i \ β€ 90
output
Output the distance and time taken for the longest path in country A and the distance and time taken for the longest path in country B on one line each. Time is in seconds, and absolute errors of 10 ^ {β3} or less are allowed for both distance and time. If the pass has never been made, output -1 for both.
sample
Sample input 1
Five
0 1 0 3 4
30 1 0 3 4
90 2 0 6 8
120 1 1 1 1
132 2 1 2 2
Sample output 1
5.00000000 1.00000000
1.41421356 0.20000000
Country A had a 5 length path at 30 frames and 60 frames = 1 second, and Country B had a β2 length path at 120 frames and 12 frames = 0.2 seconds. These are the longest paths for each.
Sample input 2
2
0 1 0 0 0
10 1 1 0 0
Sample output 2
-1 -1
-1 -1
Sample input 3
3
0 1 0 0 0
30 2 0 1 1
40 1 0 2 2
Sample output 3
1.4142135624 0.1666666667
-1 -1
Sample input 4
3
0 1 0 0 0
10 2 0 1 1
40 1 0 3 3
Sample output 4
2.8284271247 0.5000000000
-1 -1
Example
Input
5
0 1 0 3 4
30 1 0 3 4
90 2 0 6 8
120 1 1 1 1
132 2 1 2 2
Output
5.00000000 1.00000000
1.41421356 0.20000000
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
C: Mod! Mod!
story
That's right! I'm looking for eyewitness testimony! A phantom thief has appeared in Aizu! Everyone's horse stick was stolen! Who is the culprit! ?? Unravel! Mod! Mod!
Problem statement
"Eyes" ... it's a miracle bud that swells in the hearts of the chosen ones ... You can steal anything with the special ability "Eyes".
Aizu Maru, the biggest phantom thief in Aizu, decides to steal a "horse stick" from n detectives in order to fill the world with a mystery. Umauma sticks are just sweets that Maru loves, and each of the n detectives has several horse sticks. Also, because Aizumaru is greedy, when he steals a horse stick from each detective, he steals all the horse sticks that the detective has.
Aizumaru, who is addicted to eating three horse sticks at the same time, when he has three or more horse sticks at hand, he keeps three horse sticks until he loses the temptation and has less than three horse sticks. I will eat it. However, Aizumaru loses his eyes in shock if he does not have a horse stick at hand, and he cannot steal any more horse sticks. In other words, in order to steal a horse horse stick, it is necessary to have one or more horse horse sticks on hand, and when it reaches 0, it becomes impossible to steal any more horse horse sticks.
Aizuma, who wants to steal horse sticks from as many detectives as possible, noticed that the number of detectives who can steal horse sticks depends on which detective steals the horse sticks in order. However, I don't know how difficult it is to get together. "Hate?" Aizumaru's excellent subordinate, you decided to write a program to ask how many detectives you can steal a horse stick instead of Aizumaru.
Since the number of detectives n and how many horse sticks to steal from each of n detectives are given, when stealing horse sticks from detectives in the optimum order, it is possible to steal horse sticks from up to how many detectives. Create a program that outputs what you can do. However, although the number of horse sticks on hand at the beginning is 0, it is assumed that the horse sticks can be stolen even if the number of horse sticks on hand is 0 only at the beginning.
Input format
The input consists of two lines and is given in the following format.
n
a_1 a_2β¦ a_n
The first line is given the integer n, which is the number of detectives stealing horse sticks. On the second line, n number of horse sticks to steal from each detective are given, separated by blanks.
Constraint
* 1 β€ n β€ 500 {,} 000
* 1 β€ a_i β€ 9 (1 β€ i β€ n)
Output format
When you steal a horse stick from a detective in the optimal order, print out in one line how many detectives you can steal a horse stick from.
Input example 1
6
2 5 2 5 2 1
Output example 1
Five
If you steal in the order of 2 5 1 2 5, you can steal from 5 people. No matter what order you steal, you cannot steal from six people.
Input example 2
3
3 6 9
Output example 2
1
No matter which one you steal from, the number of horse sticks you have will be 0 and you will lose your eyes.
Input example 3
6
1 2 3 4 5 6
Output example 3
6
Example
Input
6
2 5 2 5 2 1
Output
5
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
problem
I want to put as many rectangular tiles as possible on a rectangular wall with a size of $ h $ in height and $ w $ in width, and a size of $ a $ in height and $ b $ in width.
The following conditions must be met when attaching tiles.
* Do not stack tiles.
* Do not apply tiles diagonally, that is, any edge of the tile is parallel or perpendicular to any edge of the wall.
* Do not change the orientation of the tiles, that is, do not swap the vertical and horizontal directions.
When as many tiles as possible are pasted, find the sum of the areas not covered by the tiles.
output
Output the total area of ββthe part not covered by the tile. Also, output a line break at the end.
Example
Input
5 8
2 2
Output
8
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
For a given weighted directed graph G(V, E), find the distance of the shortest route that meets the following criteria:
* It is a closed cycle where it ends at the same point it starts.
* It visits each vertex exactly once.
Constraints
* 2 β€ |V| β€ 15
* 0 β€ di β€ 1,000
* There are no multiedge
Input
|V| |E|
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
|V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target vertices of i-th edge (directed) and di represents the distance between si and ti (the i-th edge).
Output
Print the shortest distance in a line. If there is no solution, print -1.
Examples
Input
4 6
0 1 2
1 2 3
1 3 9
2 0 1
2 3 6
3 2 4
Output
16
Input
3 3
0 1 1
1 2 1
0 2 1
Output
-1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M.
Constraints
* 1 β€ m β€ 100
* 1 β€ n β€ 109
Input
m n
Two integers m and n are given in a line.
Output
Print mn (mod 1,000,000,007) in a line.
Examples
Input
2 3
Output
8
Input
5 8
Output
390625
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are.
Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows:
Suppose we want to analyze the segment of n consecutive days. We have measured the temperatures during these n days; the temperature during i-th day equals a_i.
We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day x to day y, we calculate it as \frac{β _{i = x}^{y} a_i}{y - x + 1} (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than k consecutive days. For example, if analyzing the measures [3, 4, 1, 2] and k = 3, we are interested in segments [3, 4, 1], [4, 1, 2] and [3, 4, 1, 2] (we want to find the maximum value of average temperature over these segments).
You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 5000) β the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 5000) β the temperature measures during given n days.
Output
Print one real number β the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than k consecutive days.
Your answer will be considered correct if the following condition holds: |res - res_0| < 10^{-6}, where res is your answer, and res_0 is the answer given by the jury's solution.
Example
Input
4 3
3 4 1 2
Output
2.666666666666667
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race.
Given the current ranking in the competition and points distribution for the next race, your task is to calculate the best possible ranking for a given astronaut after the next race. It's guaranteed that given astronaut will have unique number of points before the race.
Input
The first line contains two integer numbers N (1 β€ N β€ 200000) representing number of F1 astronauts, and current position of astronaut D (1 β€ D β€ N) you want to calculate best ranking for (no other competitor will have the same number of points before the race).
The second line contains N integer numbers S_k (0 β€ S_k β€ 10^8, k=1...N), separated by a single space, representing current ranking of astronauts. Points are sorted in non-increasing order.
The third line contains N integer numbers P_k (0 β€ P_k β€ 10^8, k=1...N), separated by a single space, representing point awards for the next race. Points are sorted in non-increasing order, so winner of the race gets the maximum number of points.
Output
Output contains one integer number β the best possible ranking for astronaut after the race. If multiple astronauts have the same score after the race, they all share the best ranking.
Example
Input
4 3
50 30 20 10
15 10 7 3
Output
2
Note
If the third ranked astronaut wins the race, he will have 35 points. He cannot take the leading position, but he can overtake the second position if the second ranked astronaut finishes the race at the last position.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Berkomnadzor β Federal Service for Supervision of Communications, Information Technology and Mass Media β is a Berland federal executive body that protects ordinary residents of Berland from the threats of modern internet.
Berkomnadzor maintains a list of prohibited IPv4 subnets (blacklist) and a list of allowed IPv4 subnets (whitelist). All Internet Service Providers (ISPs) in Berland must configure the network equipment to block access to all IPv4 addresses matching the blacklist. Also ISPs must provide access (that is, do not block) to all IPv4 addresses matching the whitelist. If an IPv4 address does not match either of those lists, it's up to the ISP to decide whether to block it or not. An IPv4 address matches the blacklist (whitelist) if and only if it matches some subnet from the blacklist (whitelist). An IPv4 address can belong to a whitelist and to a blacklist at the same time, this situation leads to a contradiction (see no solution case in the output description).
An IPv4 address is a 32-bit unsigned integer written in the form a.b.c.d, where each of the values a,b,c,d is called an octet and is an integer from 0 to 255 written in decimal notation. For example, IPv4 address 192.168.0.1 can be converted to a 32-bit number using the following expression 192 β
2^{24} + 168 β
2^{16} + 0 β
2^8 + 1 β
2^0. First octet a encodes the most significant (leftmost) 8 bits, the octets b and c β the following blocks of 8 bits (in this order), and the octet d encodes the least significant (rightmost) 8 bits.
The IPv4 network in Berland is slightly different from the rest of the world. There are no reserved or internal addresses in Berland and use all 2^{32} possible values.
An IPv4 subnet is represented either as a.b.c.d or as a.b.c.d/x (where 0 β€ x β€ 32). A subnet a.b.c.d contains a single address a.b.c.d. A subnet a.b.c.d/x contains all IPv4 addresses with x leftmost (most significant) bits equal to x leftmost bits of the address a.b.c.d. It is required that 32 - x rightmost (least significant) bits of subnet a.b.c.d/x are zeroes.
Naturally it happens that all addresses matching subnet a.b.c.d/x form a continuous range. The range starts with address a.b.c.d (its rightmost 32 - x bits are zeroes). The range ends with address which x leftmost bits equal to x leftmost bits of address a.b.c.d, and its 32 - x rightmost bits are all ones. Subnet contains exactly 2^{32-x} addresses. Subnet a.b.c.d/32 contains exactly one address and can also be represented by just a.b.c.d.
For example subnet 192.168.0.0/24 contains range of 256 addresses. 192.168.0.0 is the first address of the range, and 192.168.0.255 is the last one.
Berkomnadzor's engineers have devised a plan to improve performance of Berland's global network. Instead of maintaining both whitelist and blacklist they want to build only a single optimised blacklist containing minimal number of subnets. The idea is to block all IPv4 addresses matching the optimised blacklist and allow all the rest addresses. Of course, IPv4 addresses from the old blacklist must remain blocked and all IPv4 addresses from the old whitelist must still be allowed. Those IPv4 addresses which matched neither the old blacklist nor the old whitelist may be either blocked or allowed regardless of their accessibility before.
Please write a program which takes blacklist and whitelist as input and produces optimised blacklist. The optimised blacklist must contain the minimal possible number of subnets and satisfy all IPv4 addresses accessibility requirements mentioned above.
IPv4 subnets in the source lists may intersect arbitrarily. Please output a single number -1 if some IPv4 address matches both source whitelist and blacklist.
Input
The first line of the input contains single integer n (1 β€ n β€ 2β
10^5) β total number of IPv4 subnets in the input.
The following n lines contain IPv4 subnets. Each line starts with either '-' or '+' sign, which indicates if the subnet belongs to the blacklist or to the whitelist correspondingly. It is followed, without any spaces, by the IPv4 subnet in a.b.c.d or a.b.c.d/x format (0 β€ x β€ 32). The blacklist always contains at least one subnet.
All of the IPv4 subnets given in the input are valid. Integer numbers do not start with extra leading zeroes. The provided IPv4 subnets can intersect arbitrarily.
Output
Output -1, if there is an IPv4 address that matches both the whitelist and the blacklist. Otherwise output t β the length of the optimised blacklist, followed by t subnets, with each subnet on a new line. Subnets may be printed in arbitrary order. All addresses matching the source blacklist must match the optimised blacklist. All addresses matching the source whitelist must not match the optimised blacklist. You can print a subnet a.b.c.d/32 in any of two ways: as a.b.c.d/32 or as a.b.c.d.
If there is more than one solution, output any.
Examples
Input
1
-149.154.167.99
Output
1
0.0.0.0/0
Input
4
-149.154.167.99
+149.154.167.100/30
+149.154.167.128/25
-149.154.167.120/29
Output
2
149.154.167.99
149.154.167.120/29
Input
5
-127.0.0.4/31
+127.0.0.8
+127.0.0.0/30
-195.82.146.208/29
-127.0.0.6/31
Output
2
195.0.0.0/8
127.0.0.4/30
Input
2
+127.0.0.1/32
-127.0.0.1
Output
-1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Bob is a duck. He wants to get to Alice's nest, so that those two can duck!
<image> Duck is the ultimate animal! (Image courtesy of See Bang)
The journey can be represented as a straight line, consisting of n segments. Bob is located to the left of the first segment, while Alice's nest is on the right of the last segment. Each segment has a length in meters, and also terrain type: grass, water or lava.
Bob has three movement types: swimming, walking and flying. He can switch between them or change his direction at any point in time (even when he is located at a non-integer coordinate), and doing so doesn't require any extra time. Bob can swim only on the water, walk only on the grass and fly over any terrain. Flying one meter takes 1 second, swimming one meter takes 3 seconds, and finally walking one meter takes 5 seconds.
Bob has a finite amount of energy, called stamina. Swimming and walking is relaxing for him, so he gains 1 stamina for every meter he walks or swims. On the other hand, flying is quite tiring, and he spends 1 stamina for every meter flown. Staying in place does not influence his stamina at all. Of course, his stamina can never become negative. Initially, his stamina is zero.
What is the shortest possible time in which he can reach Alice's nest?
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of segments of terrain.
The second line contains n integers l_1, l_2, ..., l_n (1 β€ l_i β€ 10^{12}). The l_i represents the length of the i-th terrain segment in meters.
The third line contains a string s consisting of n characters "G", "W", "L", representing Grass, Water and Lava, respectively.
It is guaranteed that the first segment is not Lava.
Output
Output a single integer t β the minimum time Bob needs to reach Alice.
Examples
Input
1
10
G
Output
30
Input
2
10 10
WL
Output
40
Input
2
1 2
WL
Output
8
Input
3
10 10 10
GLW
Output
80
Note
In the first sample, Bob first walks 5 meters in 25 seconds. Then he flies the remaining 5 meters in 5 seconds.
In the second sample, Bob first swims 10 meters in 30 seconds. Then he flies over the patch of lava for 10 seconds.
In the third sample, the water pond is much smaller. Bob first swims over the water pond, taking him 3 seconds. However, he cannot fly over the lava just yet, as he only has one stamina while he needs two. So he swims back for half a meter, and then half a meter forward, taking him 3 seconds in total. Now he has 2 stamina, so he can spend 2 seconds flying over the lava.
In the fourth sample, he walks for 50 seconds, flies for 10 seconds, swims for 15 seconds, and finally flies for 5 seconds.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The i-th page contains some mystery that will be explained on page a_i (a_i β₯ i).
Ivan wants to read the whole book. Each day, he reads the first page he didn't read earlier, and continues to read the following pages one by one, until all the mysteries he read about are explained and clear to him (Ivan stops if there does not exist any page i such that Ivan already has read it, but hasn't read page a_i). After that, he closes the book and continues to read it on the following day from the next page.
How many days will it take to read the whole book?
Input
The first line contains single integer n (1 β€ n β€ 10^4) β the number of pages in the book.
The second line contains n integers a_1, a_2, ..., a_n (i β€ a_i β€ n), where a_i is the number of page which contains the explanation of the mystery on page i.
Output
Print one integer β the number of days it will take to read the whole book.
Example
Input
9
1 3 3 6 7 6 8 8 9
Output
4
Note
Explanation of the example test:
During the first day Ivan will read only the first page. During the second day Ivan will read pages number 2 and 3. During the third day β pages 4-8. During the fourth (and the last) day Ivan will read remaining page number 9.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n β
m, consisting of n rows and m columns, where point's coordinates (x, y) mean it is situated in the x-th row and y-th column, considering numeration from one (1 β€ x β€ n, 1 β€ y β€ m). Initially, you stand in the cell (1, 1). Every move you can jump from cell (x, y), which you stand in, by any non-zero vector (dx, dy), thus you will stand in the (x+dx, y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition β you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n, m (1 β€ n β
m β€ 10^{6}) β the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n β
m pairs of integers, i-th from them should contain two integers x_i, y_i (1 β€ x_i β€ n, 1 β€ y_i β€ m) β cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1, 1) coordinates, according to the statement.
Examples
Input
2 3
Output
1 1
1 3
1 2
2 2
2 3
2 1
Input
1 1
Output
1 1
Note
The vectors from the first example in the order of making jumps are (0, 2), (0, -1), (1, 0), (0, 1), (0, -2).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There is a country with n citizens. The i-th of them initially has a_{i} money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.
Sometimes the government makes payouts to the poor: all citizens who have strictly less money than x are paid accordingly so that after the payout they have exactly x money. In this case the citizens don't send a receipt.
You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the numer of citizens.
The next line contains n integers a_1, a_2, ..., a_n (0 β€ a_{i} β€ 10^{9}) β the initial balances of citizens.
The next line contains a single integer q (1 β€ q β€ 2 β
10^{5}) β the number of events.
Each of the next q lines contains a single event. The events are given in chronological order.
Each event is described as either 1 p x (1 β€ p β€ n, 0 β€ x β€ 10^{9}), or 2 x (0 β€ x β€ 10^{9}). In the first case we have a receipt that the balance of the p-th person becomes equal to x. In the second case we have a payoff with parameter x.
Output
Print n integers β the balances of all citizens after all events.
Examples
Input
4
1 2 3 4
3
2 3
1 2 2
2 1
Output
3 2 3 4
Input
5
3 50 2 1 10
3
1 2 0
2 8
1 3 20
Output
8 8 20 8 10
Note
In the first example the balances change as follows: 1 2 3 4 β 3 3 3 4 β 3 2 3 4 β 3 2 3 4
In the second example the balances change as follows: 3 50 2 1 10 β 3 0 2 1 10 β 8 8 8 8 10 β 8 8 20 8 10
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you will take a look from above and assume that the bottom left corner of the table has coordinates (0, 0), and coordinate axes are left and bottom sides of the table, then the bottom left corner of the white sheet has coordinates (x_1, y_1), and the top right β (x_2, y_2).
After that two black sheets of paper are placed on the table. Sides of both black sheets are also parallel to the sides of the table. Coordinates of the bottom left corner of the first black sheet are (x_3, y_3), and the top right β (x_4, y_4). Coordinates of the bottom left corner of the second black sheet are (x_5, y_5), and the top right β (x_6, y_6).
<image> Example of three rectangles.
Determine if some part of the white sheet can be seen from the above after the two black sheets are placed. The part of the white sheet can be seen if there is at least one point lying not strictly inside the white sheet and strictly outside of both black sheets.
Input
The first line of the input contains four integers x_1, y_1, x_2, y_2 (0 β€ x_1 < x_2 β€ 10^{6}, 0 β€ y_1 < y_2 β€ 10^{6}) β coordinates of the bottom left and the top right corners of the white sheet.
The second line of the input contains four integers x_3, y_3, x_4, y_4 (0 β€ x_3 < x_4 β€ 10^{6}, 0 β€ y_3 < y_4 β€ 10^{6}) β coordinates of the bottom left and the top right corners of the first black sheet.
The third line of the input contains four integers x_5, y_5, x_6, y_6 (0 β€ x_5 < x_6 β€ 10^{6}, 0 β€ y_5 < y_6 β€ 10^{6}) β coordinates of the bottom left and the top right corners of the second black sheet.
The sides of each sheet of paper are parallel (perpendicular) to the coordinate axes.
Output
If some part of the white sheet can be seen from the above after the two black sheets are placed, print "YES" (without quotes). Otherwise print "NO".
Examples
Input
2 2 4 4
1 1 3 5
3 1 5 5
Output
NO
Input
3 3 7 5
0 0 4 6
0 0 7 4
Output
YES
Input
5 2 10 5
3 1 7 6
8 1 11 7
Output
YES
Input
0 0 1000000 1000000
0 0 499999 1000000
500000 0 1000000 1000000
Output
YES
Note
In the first example the white sheet is fully covered by black sheets.
In the second example the part of the white sheet can be seen after two black sheets are placed. For example, the point (6.5, 4.5) lies not strictly inside the white sheet and lies strictly outside of both black sheets.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In the Catowice city next weekend the cat contest will be held. However, the jury members and the contestants haven't been selected yet. There are n residents and n cats in the Catowice, and each resident has exactly one cat living in his house. The residents and cats are numbered with integers from 1 to n, where the i-th cat is living in the house of i-th resident.
Each Catowice resident is in friendship with several cats, including the one living in his house. In order to conduct a contest, at least one jury member is needed and at least one cat contestant is needed. Of course, every jury member should know none of the contestants. For the contest to be successful, it's also needed that the number of jury members plus the number of contestants is equal to n.
Please help Catowice residents to select the jury and the contestants for the upcoming competition, or determine that it's impossible to do.
Input
The first line contains an integer t (1 β€ t β€ 100 000), the number of test cases. Then description of t test cases follow, where each description is as follows:
The first line contains integers n and m (1 β€ n β€ m β€ 10^6), the number of Catowice residents and the number of friendship pairs between residents and cats.
Each of the next m lines contains integers a_i and b_i (1 β€ a_i, b_i β€ n), denoting that a_i-th resident is acquaintances with b_i-th cat. It's guaranteed that each pair of some resident and some cat is listed at most once.
It's guaranteed, that for every i there exists a pair between i-th resident and i-th cat.
Different test cases are separated with an empty line.
It's guaranteed, that the sum of n over all test cases is at most 10^6 and that the sum of m over all test cases is at most 10^6.
Output
For every test case print:
* "No", if it's impossible to select the jury and contestants.
* Otherwise print "Yes".
In the second line print two integers j and p (1 β€ j, 1 β€ p, j + p = n) β the number of jury members and the number of contest participants.
In the third line print j distinct integers from 1 to n, the indices of the residents forming a jury.
In the fourth line print p distinct integers from 1 to n, the indices of the cats, which will participate in the contest.
In case there are several correct answers, print any of them.
Example
Input
4
3 4
1 1
2 2
3 3
1 3
3 7
1 1
1 2
1 3
2 2
3 1
3 2
3 3
1 1
1 1
2 4
1 1
1 2
2 1
2 2
Output
Yes
2 1
1 3
2
Yes
1 2
2
1 3
No
No
Note
In the first test case, we can select the first and the third resident as a jury. Both of them are not acquaintances with a second cat, so we can select it as a contestant.
In the second test case, we can select the second resident as a jury. He is not an acquaintances with a first and a third cat, so they can be selected as contestants.
In the third test case, the only resident is acquaintances with the only cat, so they can't be in the contest together. So it's not possible to make a contest with at least one jury and at least one cat.
In the fourth test case, each resident is acquaintances with every cat, so it's again not possible to make a contest with at least one jury and at least one cat.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example:
* 2 and 4 are similar (binary representations are 10 and 100);
* 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101);
* 3 and 2 are not similar (binary representations are 11 and 10);
* 42 and 13 are similar (binary representations are 101010 and 1101).
You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i β x (β denotes bitwise XOR).
Is it possible to obtain an array b where all numbers are similar to each other?
Input
The first line contains one integer n (2 β€ n β€ 100).
The second line contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2^{30} - 1).
Output
If it is impossible to choose x so that all elements in the resulting array are similar to each other, print one integer -1.
Otherwise, print any non-negative integer not exceeding 2^{30} - 1 that can be used as x so that all elements in the resulting array are similar.
Examples
Input
2
7 2
Output
1
Input
4
3 17 6 0
Output
5
Input
3
1 2 3
Output
-1
Input
3
43 12 12
Output
1073709057
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We just discovered a new data structure in our research group: a suffix three!
It's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in.
It's super simple, 100% accurate, and doesn't involve advanced machine learning algorithms.
Let us tell you how it works.
* If a sentence ends with "po" the language is Filipino.
* If a sentence ends with "desu" or "masu" the language is Japanese.
* If a sentence ends with "mnida" the language is Korean.
Given this, we need you to implement a suffix three that can differentiate Filipino, Japanese, and Korean.
Oh, did I say three suffixes? I meant four.
Input
The first line of input contains a single integer t (1 β€ t β€ 30) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing a single string denoting the sentence. Spaces are represented as underscores (the symbol "_") for ease of reading. The sentence has at least 1 and at most 1000 characters, and consists only of lowercase English letters and underscores. The sentence has no leading or trailing underscores and no two consecutive underscores. It is guaranteed that the sentence ends with one of the four suffixes mentioned above.
Output
For each test case, print a single line containing either "FILIPINO", "JAPANESE", or "KOREAN" (all in uppercase, without quotes), depending on the detected language.
Example
Input
8
kamusta_po
genki_desu
ohayou_gozaimasu
annyeong_hashimnida
hajime_no_ippo
bensamu_no_sentou_houhou_ga_okama_kenpo
ang_halaman_doon_ay_sarisari_singkamasu
si_roy_mustang_ay_namamasu
Output
FILIPINO
JAPANESE
JAPANESE
KOREAN
FILIPINO
FILIPINO
JAPANESE
JAPANESE
Note
The first sentence ends with "po", so it is written in Filipino.
The second and third sentences end with "desu" and "masu", so they are written in Japanese.
The fourth sentence ends with "mnida", so it is written in Korean.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i.
For every i (1 β€ i β€ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly n swap operations, each of them either c_i β a_i or c_i β b_i (i iterates over all integers between 1 and n, inclusive).
For example, if a is "code", b is "true", and c is "help", you can make c equal to "crue" taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes "hodp" and b becomes "tele".
Is it possible that after these swaps the string a becomes exactly the same as the string b?
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 100) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a string of lowercase English letters a.
The second line of each test case contains a string of lowercase English letters b.
The third line of each test case contains a string of lowercase English letters c.
It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.
Output
Print t lines with answers for all test cases. For each test case:
If it is possible to make string a equal to string b print "YES" (without quotes), otherwise print "NO" (without quotes).
You can print either lowercase or uppercase letters in the answers.
Example
Input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
Output
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
In the second test case, you should swap c_i with a_i for all possible i. After the swaps a becomes "bca", b becomes "bca" and c becomes "abc". Here the strings a and b are equal.
In the third test case, you should swap c_1 with a_1, c_2 with b_2, c_3 with b_3 and c_4 with a_4. Then string a becomes "baba", string b becomes "baba" and string c becomes "abab". Here the strings a and b are equal.
In the fourth test case, it is impossible to do the swaps so that string a becomes exactly the same as string b.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We guessed a permutation p consisting of n integers. The permutation of length n is the array of length n where each element from 1 to n appears exactly once. This permutation is a secret for you.
For each position r from 2 to n we chose some other index l (l < r) and gave you the segment p_l, p_{l + 1}, ..., p_r in sorted order (i.e. we rearranged the elements of this segment in a way that the elements of this segment are sorted). Thus, you are given exactly n-1 segments of the initial permutation but elements inside each segment are sorted. The segments are given to you in random order.
For example, if the secret permutation is p=[3, 1, 4, 6, 2, 5] then the possible given set of segments can be:
* [2, 5, 6]
* [4, 6]
* [1, 3, 4]
* [1, 3]
* [1, 2, 4, 6]
Your task is to find any suitable permutation (i.e. any permutation corresponding to the given input data). It is guaranteed that the input data corresponds to some permutation (i.e. such permutation exists).
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 200) β the length of the permutation.
The next n-1 lines describe given segments.
The i-th line contains the description of the i-th segment. The line starts with the integer k_i (2 β€ k_i β€ n) β the length of the i-th segment. Then k_i integers follow. All integers in a line are distinct, sorted in ascending order, between 1 and n, inclusive.
It is guaranteed that the required p exists for each test case.
It is also guaranteed that the sum of n over all test cases does not exceed 200 (β n β€ 200).
Output
For each test case, print the answer: n integers p_1, p_2, ..., p_n (1 β€ p_i β€ n, all p_i should be distinct) β any suitable permutation (i.e. any permutation corresponding to the test case input).
Example
Input
5
6
3 2 5 6
2 4 6
3 1 3 4
2 1 3
4 1 2 4 6
5
2 2 3
2 1 2
2 1 4
2 4 5
7
3 1 2 6
4 1 3 5 6
2 1 2
3 4 5 7
6 1 2 3 4 5 6
3 1 3 6
2
2 1 2
5
2 2 5
3 2 3 5
4 2 3 4 5
5 1 2 3 4 5
Output
3 1 4 6 2 5
3 2 1 4 5
2 1 6 3 5 4 7
1 2
2 5 3 4 1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.
For example, Zookeeper can use two such operations: AABABBA β AABBA β AAA.
Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 20000) β the number of test cases. The description of the test cases follows.
Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.
It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the length of the shortest string that Zookeeper can make.
Example
Input
3
AAA
BABA
AABBBABBBB
Output
3
2
0
Note
For the first test case, you can't make any moves, so the answer is 3.
For the second test case, one optimal sequence of moves is BABA β BA. So, the answer is 2.
For the third test case, one optimal sequence of moves is AABBBABBBB β AABBBABB β AABBBB β ABBB β AB β (empty string). So, the answer is 0.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.
He wants to convert string a into string b by performing some (possibly zero) operations on a.
In one move, he can either
* choose an index i (1 β€ iβ€ n-1) and swap a_i and a_{i+1}, or
* choose an index i (1 β€ i β€ n-k+1) and if a_i, a_{i+1}, β¦, a_{i+k-1} are all equal to some character c (c β 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.
Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases. The description of each test case is as follows.
The first line of each test case contains two integers n (2 β€ n β€ 10^6) and k (1 β€ k β€ n).
The second line of each test case contains the string a of length n consisting of lowercase English letters.
The third line of each test case contains the string b of length n consisting of lowercase English letters.
It is guaranteed that the sum of values n among all test cases does not exceed 10^6.
Output
For each test case, print "Yes" if Ashish can convert a into b after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
Example
Input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
Output
No
Yes
No
Yes
Note
In the first test case it can be shown that it is impossible to convert a into b.
In the second test case,
"abba" \xrightarrow{inc} "acca" \xrightarrow{inc} β¦ \xrightarrow{inc} "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \xrightarrow{swap} "aaabab" \xrightarrow{swap} "aaaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddaabb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddbb" \xrightarrow{inc} β¦ \xrightarrow{inc} "ddddcc".
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls.
Each class must present two couples to the ball. In Vasya's class, a boys and b girls wish to participate. But not all boys and not all girls are ready to dance in pairs.
Formally, you know k possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair.
For example, if a=3, b=4, k=4 and the couples (1, 2), (1, 3), (2, 2), (3, 4) are ready to dance together (in each pair, the boy's number comes first, then the girl's number), then the following combinations of two pairs are possible (not all possible options are listed below):
* (1, 3) and (2, 2);
* (3, 4) and (1, 3);
But the following combinations are not possible:
* (1, 3) and (1, 2) β the first boy enters two pairs;
* (1, 2) and (2, 2) β the second girl enters two pairs;
Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
The first line of each test case contains three integers a, b and k (1 β€ a, b, k β€ 2 β
10^5) β the number of boys and girls in the class and the number of couples ready to dance together.
The second line of each test case contains k integers a_1, a_2, β¦ a_k. (1 β€ a_i β€ a), where a_i is the number of the boy in the pair with the number i.
The third line of each test case contains k integers b_1, b_2, β¦ b_k. (1 β€ b_i β€ b), where b_i is the number of the girl in the pair with the number i.
It is guaranteed that the sums of a, b, and k over all test cases do not exceed 2 β
10^5.
It is guaranteed that each pair is specified at most once in one test case.
Output
For each test case, on a separate line print one integer β the number of ways to choose two pairs that match the condition above.
Example
Input
3
3 4 4
1 1 2 3
2 3 2 4
1 1 1
1
1
2 2 4
1 1 2 2
1 2 1 2
Output
4
0
2
Note
In the first test case, the following combinations of pairs fit:
* (1, 2) and (3, 4);
* (1, 3) and (2, 2);
* (1, 3) and (3, 4);
* (2, 2) and (3, 4).
There is only one pair in the second test case.
In the third test case, the following combinations of pairs fit:
* (1, 1) and (2, 2);
* (1, 2) and (2, 1).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are n robots driving along an OX axis. There are also two walls: one is at coordinate 0 and one is at coordinate m.
The i-th robot starts at an integer coordinate x_i~(0 < x_i < m) and moves either left (towards the 0) or right with the speed of 1 unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and -1 otherwise.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1 β€ n β€ 3 β
10^5; 2 β€ m β€ 10^8) β the number of robots and the coordinate of the right wall.
The second line of each testcase contains n integers x_1, x_2, ..., x_n (0 < x_i < m) β the starting coordinates of the robots.
The third line of each testcase contains n space-separated characters 'L' or 'R' β the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates x_i in the testcase are distinct.
The sum of n over all testcases doesn't exceed 3 β
10^5.
Output
For each testcase print n integers β for the i-th robot output the time it explodes at if it does and -1 otherwise.
Example
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
Note
Here is the picture for the seconds 0, 1, 2 and 3 of the first testcase:
<image>
Notice that robots 2 and 3 don't collide because they meet at the same point 2.5, which is not integer.
After second 3 robot 6 just drive infinitely because there's no robot to collide with.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Fibonacci strings are defined as follows:
* f1 = Β«aΒ»
* f2 = Β«bΒ»
* fn = fn - 1 fn - 2, n > 2
Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba".
You are given a Fibonacci string and m strings si. For each string si, find the number of times it occurs in the given Fibonacci string as a substring.
Input
The first line contains two space-separated integers k and m β the number of a Fibonacci string and the number of queries, correspondingly.
Next m lines contain strings si that correspond to the queries. It is guaranteed that strings si aren't empty and consist only of characters "a" and "b".
The input limitations for getting 30 points are:
* 1 β€ k β€ 3000
* 1 β€ m β€ 3000
* The total length of strings si doesn't exceed 3000
The input limitations for getting 100 points are:
* 1 β€ k β€ 1018
* 1 β€ m β€ 104
* The total length of strings si doesn't exceed 105
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
For each string si print the number of times it occurs in the given Fibonacci string as a substring. Since the numbers can be large enough, print them modulo 1000000007 (109 + 7). Print the answers for the strings in the order in which they are given in the input.
Examples
Input
6 5
a
b
ab
ba
aba
Output
3
5
3
3
1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations:
* the final tournament features n teams (n is always even)
* the first n / 2 teams (according to the standings) come through to the knockout stage
* the standings are made on the following principle: for a victory a team gets 3 points, for a draw β 1 point, for a defeat β 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place β in decreasing order of the difference between scored and missed goals; in the third place β in the decreasing order of scored goals
* it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity.
You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage.
Input
The first input line contains the only integer n (1 β€ n β€ 50) β amount of the teams, taking part in the final tournament of World Cup. The following n lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following nΒ·(n - 1) / 2 lines describe the held matches in the format name1-name2 num1:num2, where name1, name2 β names of the teams; num1, num2 (0 β€ num1, num2 β€ 100) β amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once.
Output
Output n / 2 lines β names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.
Examples
Input
4
A
B
C
D
A-B 1:1
A-C 2:2
A-D 1:0
B-C 1:0
B-D 0:3
C-D 0:3
Output
A
D
Input
2
a
A
a-A 2:1
Output
a
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 β€ k1 < k2 < ... < k|x| β€ |s|).
You've got two strings β s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences? In other words, is it true that for all i (1 β€ i β€ |s|), there is such subsequence x = sk1sk2... sk|x| of string s, that x = t and for some j (1 β€ j β€ |x|) kj = i.
Input
The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105.
Output
Print "Yes" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
Examples
Input
abab
ab
Output
Yes
Input
abacaba
aba
Output
No
Input
abc
ba
Output
No
Note
In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.
In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.
In the third sample there is no occurrence of string t in string s.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Dima and his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place.
To decide who exactly would clean the apartment, the friends want to play a counting-out game. First, all the guys stand in a circle, and then each of them shows some number of fingers on one hand (one to five), and then the boys count in a circle, starting from Dima, the number of people, respective to the total number of fingers shown. The person on who the countdown stops will clean the apartment.
For example, if Dima and one of his friends played hide and seek, and 7 fingers were shown during the counting-out, then Dima would clean the place. If there were 2 or say, 8 fingers shown, then his friend would clean the place.
Dima knows how many fingers each of his friends will show during the counting-out. Now he is interested in the number of ways to show some number of fingers on one hand (one to five), so that he did not have to clean the place. Help Dima.
Input
The first line contains integer n (1 β€ n β€ 100) β the number of Dima's friends. Dima himself isn't considered to be his own friend. The second line contains n positive integers, not exceeding 5, representing, how many fingers the Dima's friends will show.
The numbers in the lines are separated by a single space.
Output
In a single line print the answer to the problem.
Examples
Input
1
1
Output
3
Input
1
2
Output
2
Input
2
3 5
Output
3
Note
In the first sample Dima can show 1, 3 or 5 fingers. If Dima shows 3 fingers, then the counting-out will go like that: Dima, his friend, Dima, his friend.
In the second sample Dima can show 2 or 4 fingers.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidentally Natalia Fan Club also has 2n members. Each member of MDC is assigned a unique id i from 0 to 2n - 1. The same holds for each member of NFC.
One of the parts of this tradition is one by one dance, where each member of MDC dances with a member of NFC. A dance pair is a pair of numbers (a, b) such that member a from MDC dances with member b from NFC.
The complexity of a pairs' assignment is the number of pairs of dancing pairs (a, b) and (c, d) such that a < c and b > d.
You are given a binary number of length n named x. We know that member i from MDC dances with member <image> from NFC. Your task is to calculate the complexity of this assignment modulo 1000000007 (109 + 7).
Expression <image> denotes applying Β«XORΒ» to numbers x and y. This operation exists in all modern programming languages, for example, in C++ and Java it denotes as Β«^Β», in Pascal β Β«xorΒ».
Input
The first line of input contains a binary number x of lenght n, (1 β€ n β€ 100).
This number may contain leading zeros.
Output
Print the complexity of the given dance assignent modulo 1000000007 (109 + 7).
Examples
Input
11
Output
6
Input
01
Output
2
Input
1
Output
1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something.
Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words, <image> , where aj is the taste of the j-th chosen fruit and bj is its calories.
Inna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem β now the happiness of a young couple is in your hands!
Inna loves Dima very much so she wants to make the salad from at least one fruit.
Input
The first line of the input contains two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 10). The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 100) β the fruits' tastes. The third line of the input contains n integers b1, b2, ..., bn (1 β€ bi β€ 100) β the fruits' calories. Fruit number i has taste ai and calories bi.
Output
If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer β the maximum possible sum of the taste values of the chosen fruits.
Examples
Input
3 2
10 8 1
2 7 1
Output
18
Input
5 3
4 4 4 4 4
2 2 2 2 2
Output
-1
Note
In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition <image> fulfills, that's exactly what Inna wants.
In the second test sample we cannot choose the fruits so as to follow Inna's principle.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Two chess pieces, a rook and a knight, stand on a standard chessboard 8 Γ 8 in size. The positions in which they are situated are known. It is guaranteed that none of them beats the other one.
Your task is to find the number of ways to place another knight on the board so that none of the three pieces on the board beat another one. A new piece can only be placed on an empty square.
Input
The first input line contains the description of the rook's position on the board. This description is a line which is 2 in length. Its first symbol is a lower-case Latin letter from a to h, and its second symbol is a number from 1 to 8. The second line contains the description of the knight's position in a similar way. It is guaranteed that their positions do not coincide.
Output
Print a single number which is the required number of ways.
Examples
Input
a1
b2
Output
44
Input
a8
d4
Output
38
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Not so long ago as a result of combat operations the main Berland place of interest β the magic clock β was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin corresponds to the clock center. The clock was painted two colors as is shown in the picture:
<image>
The picture shows only the central part of the clock. This coloring naturally extends to infinity.
The balls can be taken to be points on the plane. Your task is to find the color of the area, damaged by the given ball.
All the points located on the border of one of the areas have to be considered painted black.
Input
The first and single line contains two integers x and y β the coordinates of the hole made in the clock by the ball. Each of the numbers x and y has an absolute value that does not exceed 1000.
Output
Find the required color.
All the points between which and the origin of coordinates the distance is integral-value are painted black.
Examples
Input
-2 1
Output
white
Input
2 1
Output
black
Input
4 3
Output
black
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea.
Amr has n instruments, it takes ai days to learn i-th instrument. Being busy, Amr dedicated k days to learn how to play the maximum possible number of instruments.
Amr asked for your help to distribute his free days between instruments so that he can achieve his goal.
Input
The first line contains two numbers n, k (1 β€ n β€ 100, 0 β€ k β€ 10 000), the number of instruments and number of days respectively.
The second line contains n integers ai (1 β€ ai β€ 100), representing number of days required to learn the i-th instrument.
Output
In the first line output one integer m representing the maximum number of instruments Amr can learn.
In the second line output m space-separated integers: the indices of instruments to be learnt. You may output indices in any order.
if there are multiple optimal solutions output any. It is not necessary to use all days for studying.
Examples
Input
4 10
4 3 1 2
Output
4
1 2 3 4
Input
5 6
4 3 1 1 2
Output
3
1 3 4
Input
1 3
4
Output
0
Note
In the first test Amr can learn all 4 instruments.
In the second test other possible solutions are: {2, 3, 5} or {3, 4, 5}.
In the third test Amr doesn't have enough time to learn the only presented instrument.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.
Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers from 0 to n - 1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on.
Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if n = 5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active.
Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0, 1, 2, ..., n - 1. Write a program that determines whether the given puzzle is real or fake.
Input
The first line contains integer n (1 β€ n β€ 1000) β the number of gears.
The second line contains n digits a1, a2, ..., an (0 β€ ai β€ n - 1) β the sequence of active teeth: the active tooth of the i-th gear contains number ai.
Output
In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise.
Examples
Input
3
1 0 0
Output
Yes
Input
5
4 2 1 4 3
Output
Yes
Input
4
0 2 3 1
Output
No
Note
In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.
The students donβt want to use too many blocks, but they also want to be unique, so no two studentsβ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.
Input
The first line of the input contains two space-separated integers n and m (0 β€ n, m β€ 1 000 000, n + m > 0) β the number of students using two-block pieces and the number of students using three-block pieces, respectively.
Output
Print a single integer, denoting the minimum possible height of the tallest tower.
Examples
Input
1 3
Output
9
Input
3 2
Output
8
Input
5 0
Output
10
Note
In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.
In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help.
You are given a sequence a, consisting of n distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process.
1. First element a_1 becomes the root of the tree.
2. Elements a_2, a_3, β¦, a_n are added one by one. To add element a_i one needs to traverse the tree starting from the root and using the following rules:
1. The pointer to the current node is set to the root.
2. If a_i is greater than the value in the current node, then its right child becomes the current node. Otherwise, the left child of the current node becomes the new current node.
3. If at some point there is no required child, the new node is created, it is assigned value a_i and becomes the corresponding child of the current node.
Input
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the length of the sequence a.
The second line contains n distinct integers a_i (1 β€ a_i β€ 10^9) β the sequence a itself.
Output
Output n - 1 integers. For all i > 1 print the value written in the node that is the parent of the node with value a_i in it.
Examples
Input
3
1 2 3
Output
1 2
Input
5
4 2 3 1 6
Output
4 2 2 4
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothing. Once one of the players scores exactly k points, the score is reset and a new set begins.
Across all the sets Misha scored a points in total, and Vanya scored b points. Given this information, determine the maximum number of sets they could have played, or that the situation is impossible.
Note that the game consisted of several complete sets.
Input
The first line contains three space-separated integers k, a and b (1 β€ k β€ 109, 0 β€ a, b β€ 109, a + b > 0).
Output
If the situation is impossible, print a single number -1. Otherwise, print the maximum possible number of sets.
Examples
Input
11 11 5
Output
1
Input
11 2 3
Output
-1
Note
Note that the rules of the game in this problem differ from the real table tennis game, for example, the rule of "balance" (the winning player has to be at least two points ahead to win a set) has no power within the present problem.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Do you remember a kind cartoon "Beauty and the Beast"? No, no, there was no firing from machine guns or radiation mutants time-travels!
There was a beauty named Belle. Once she had violated the Beast's order and visited the West Wing. After that she was banished from the castle...
Everybody was upset. The beautiful Belle was upset, so was the Beast, so was Lumiere the candlestick. But the worst thing was that Cogsworth was upset. Cogsworth is not a human, but is the mantel clock, which was often used as an alarm clock.
Due to Cogsworth's frustration all the inhabitants of the castle were in trouble: now they could not determine when it was time to drink morning tea, and when it was time for an evening stroll.
Fortunately, deep in the basement are lying digital clock showing the time in the format HH:MM. Now the residents of the castle face a difficult task. They should turn Cogsworth's hour and minute mustache hands in such a way, that Cogsworth began to show the correct time. Moreover they need to find turn angles in degrees for each mustache hands. The initial time showed by Cogsworth is 12:00.
You can only rotate the hands forward, that is, as is shown in the picture:
<image>
As since there are many ways too select such angles because of full rotations, choose the smallest angles in the right (non-negative) direction.
Note that Cogsworth's hour and minute mustache hands move evenly and continuously. Hands are moving independently, so when turning one hand the other hand remains standing still.
Input
The only line of input contains current time according to the digital clock, formatted as HH:MM (00 β€ HH β€ 23, 00 β€ MM β€ 59). The mantel clock initially shows 12:00.
Pretests contain times of the beginning of some morning TV programs of the Channel One Russia.
Output
Print two numbers x and y β the angles of turning the hour and minute hands, respectively (0 β€ x, y < 360). The absolute or relative error in the answer should not exceed 10 - 9.
Examples
Input
12:00
Output
0 0
Input
04:30
Output
135 180
Input
08:17
Output
248.5 102
Note
A note to the second example: the hour hand will be positioned exactly in the middle, between 4 and 5.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Mishka has got n empty boxes. For every i (1 β€ i β€ n), i-th box is a cube with side length ai.
Mishka can put a box i into another box j if the following conditions are met:
* i-th box is not put into another box;
* j-th box doesn't contain any other boxes;
* box i is smaller than box j (ai < aj).
Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.
Help Mishka to determine the minimum possible number of visible boxes!
Input
The first line contains one integer n (1 β€ n β€ 5000) β the number of boxes Mishka has got.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the side length of i-th box.
Output
Print the minimum possible number of visible boxes.
Examples
Input
3
1 2 3
Output
1
Input
4
4 2 4 3
Output
2
Note
In the first example it is possible to put box 1 into box 2, and 2 into 3.
In the second example Mishka can put box 2 into box 3, and box 4 into box 1.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Consider a system of n water taps all pouring water into the same container. The i-th water tap can be set to deliver any amount of water from 0 to ai ml per second (this amount may be a real number). The water delivered by i-th tap has temperature ti.
If for every <image> you set i-th tap to deliver exactly xi ml of water per second, then the resulting temperature of water will be <image> (if <image>, then to avoid division by zero we state that the resulting water temperature is 0).
You have to set all the water taps in such a way that the resulting temperature is exactly T. What is the maximum amount of water you may get per second if its temperature has to be T?
Input
The first line contains two integers n and T (1 β€ n β€ 200000, 1 β€ T β€ 106) β the number of water taps and the desired temperature of water, respectively.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 106) where ai is the maximum amount of water i-th tap can deliver per second.
The third line contains n integers t1, t2, ..., tn (1 β€ ti β€ 106) β the temperature of water each tap delivers.
Output
Print the maximum possible amount of water with temperature exactly T you can get per second (if it is impossible to obtain water with such temperature, then the answer is considered to be 0).
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Examples
Input
2 100
3 10
50 150
Output
6.000000000000000
Input
3 9
5 5 30
6 6 10
Output
40.000000000000000
Input
2 12
1 3
10 15
Output
1.666666666666667
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be divided into, such that the product of any pair of integers in the same group is a perfect square.
Each integer must be in exactly one group. However, integers in a group do not necessarily have to be contiguous in the array.
SaMer wishes to create more cases from the test case he already has. His test case has an array A of n integers, and he needs to find the number of contiguous subarrays of A that have an answer to the problem equal to k for each integer k between 1 and n (inclusive).
Input
The first line of input contains a single integer n (1 β€ n β€ 5000), the size of the array.
The second line contains n integers a_1,a_2,...,a_n (-10^8 β€ a_i β€ 10^8), the values of the array.
Output
Output n space-separated integers, the k-th integer should be the number of contiguous subarrays of A that have an answer to the problem equal to k.
Examples
Input
2
5 5
Output
3 0
Input
5
5 -4 2 1 8
Output
5 5 3 2 0
Input
1
0
Output
1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong β the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure β and then terrible Game Cubes fall down on the city, bringing death to those modules, who cannot win the game...
For sure, as guard Bob appeared in Mainframe many modules stopped fearing Game Cubes. Because Bob (as he is alive yet) has never been defeated by User, and he always meddles with Game Cubes, because he is programmed to this.
However, unpleasant situations can happen, when a Game Cube falls down on Lost Angles. Because there lives a nasty virus β Hexadecimal, who is... mmm... very strange. And she likes to play very much. So, willy-nilly, Bob has to play with her first, and then with User.
This time Hexadecimal invented the following entertainment: Bob has to leap over binary search trees with n nodes. We should remind you that a binary search tree is a binary tree, each node has a distinct key, for each node the following is true: the left sub-tree of a node contains only nodes with keys less than the node's key, the right sub-tree of a node contains only nodes with keys greater than the node's key. All the keys are different positive integer numbers from 1 to n. Each node of such a tree can have up to two children, or have no children at all (in the case when a node is a leaf).
In Hexadecimal's game all the trees are different, but the height of each is not lower than h. In this problem Β«heightΒ» stands for the maximum amount of nodes on the way from the root to the remotest leaf, the root node and the leaf itself included. When Bob leaps over a tree, it disappears. Bob gets the access to a Cube, when there are no trees left. He knows how many trees he will have to leap over in the worst case. And you?
Input
The input data contains two space-separated positive integer numbers n and h (n β€ 35, h β€ n).
Output
Output one number β the answer to the problem. It is guaranteed that it does not exceed 9Β·1018.
Examples
Input
3 2
Output
5
Input
3 3
Output
4
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N boxes .The i^th box contains ai candies . The frog Om Nom is sitting in box number 1 .Om Nom wants to collect as many candies as he can . However , Om Nom jumps in a peculiar fashion . Om Nom can jump from box number j to box number i only if j | i (j divides i) . Whenever Om Nom lands in a box , he collects all the candies in that box. You have to report the maximum number of Candies that Om Nom can collect for all i from 1 to N if i is the last box that Om Nom visits.See the samples for more details.
Constraints
0 β€ ai β€ 100000
1 β€ N β€ 100000
Input
The first line contains the number N.
The second line contains the array a , the i^th integer denoting the number of candies in the i^th box.
Output
Print N integers-the i^th integer should be the maximum number of candies that Om Nom can collect if the i^th box is his final box (i.e his sequence of jumps ends at i).
SAMPLE INPUT
5
2 3 1 4 6
SAMPLE OUTPUT
2 5 3 9 8
Explanation
For i=1,Om Nom stops at box number 1 itself and so he can collect only 2 candies.
For i=2,the sequence of jumps is 1 2(array indices)
For i=3,the sequence of jumps is 1 3
For i=4,the sequence of jumps is 1 2 4
For i=5,the sequence of jumps is 1 5
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.