question
large_stringlengths 265
13.2k
|
|---|
Solve the programming task below in a Python markdown code block.
Broken crypto generator
JAG (Japanese Alumni Group) is a mysterious organization composed of many programmers, and in order to enter the building where the headquarters of this organization is located, it is necessary to solve the ciphertext generated by a certain machine every time. This ciphertext consists of the symbols'+','-','[',']' and the uppercase alphabet, and is represented by <Cipher> defined by the following BNF.
<Cipher> :: = <String> | <Cipher> <String>
<String> :: = <Letter> |'['<Cipher>']'
<Letter> :: ='+' <Letter> |'-' <Letter> |
'A' |'B' |'C' |'D' |'E' |'F' |'G' |'H' |'I' |'J' |'K' |'L' |'M '|
'N' |'O' |'P' |'Q' |'R' |'S' |'T' |'U' |'V' |'W' |'X' |'Y' |'Z '
Here, each symbol has the following meaning.
* + (Character): Represents the alphabet following that character (provided that the alphabet following'Z'is'A')
*-(Character): Represents the alphabet before that character (provided that the alphabet before'A'is'Z')
* [(Character string)]: Represents a character string that is horizontally inverted.
However, the machine that generates this ciphertext is currently out of order, and some letters of the alphabet in the ciphertext may be broken and unreadable. Unreadable characters are tentatively represented as'?'. As a result of the investigation, it was found that the method of filling the broken characters is such that the decrypted character string is the smallest in the dictionary order among the possible character strings after decoding. Your job is to decrypt this ciphertext correctly.
Input
The input consists of multiple datasets. Each dataset consists of one line containing a string in which some uppercase letters have been replaced with ‘?’ In the ciphertext defined by BNF above. You can assume that the length of each string is less than $ 80 $. You can also assume that the number of'?' In each dataset is greater than or equal to $ 0 $ and less than or equal to $ 3 $.
The end of the input is represented by a line containing only one character,'.'.
Output
For each data set, output the decrypted character string when the ciphertext is decrypted so that the decrypted character string is the smallest in the dictionary order.
Sample Input
A + A ++ A
Z-Z--Z + -Z
[ESREVER]
J ---? --- J
++++++++ A +++ Z ----------- A +++ Z
[[++-+-? [-++-? ++-+++ L]] [-+ ----- + -O]] ++++ --- + L
..
Output for Sample Input
ABC
ZYXZ
REVERSE
JAG
ICPC
JAPAN
Example
Input
A+A++A
Z-Z--Z+-Z
[ESREVER]
J---?---J
++++++++A+++Z-----------A+++Z
[[++-+--?[--++-?++-+++L]][-+-----+-O]]++++---+L
.
Output
ABC
ZYXZ
REVERSE
JAG
ICPC
JAPAN
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 have $n$ students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the $i$-th student skill is denoted by an integer $a_i$ (different students can have the same skills).
So, about the teams. Firstly, these two teams should have the same size. Two more constraints: The first team should consist of students with distinct skills (i.e. all skills in the first team are unique). The second team should consist of students with the same skills (i.e. all skills in the second team are equal).
Note that it is permissible that some student of the first team has the same skill as a student of the second team.
Consider some examples (skills are given): $[1, 2, 3]$, $[4, 4]$ is not a good pair of teams because sizes should be the same; $[1, 1, 2]$, $[3, 3, 3]$ is not a good pair of teams because the first team should not contain students with the same skills; $[1, 2, 3]$, $[3, 4, 4]$ is not a good pair of teams because the second team should contain students with the same skills; $[1, 2, 3]$, $[3, 3, 3]$ is a good pair of teams; $[5]$, $[6]$ is a good pair of teams.
Your task is to find the maximum possible size $x$ for which it is possible to compose a valid pair of teams, where each team size is $x$ (skills in the first team needed to be unique, skills in the second team should be the same between them). A student cannot be part of more than one team.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of students. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the skill of the $i$-th student. Different students can have the same skills.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer — the maximum possible size $x$ for which it is possible to compose a valid pair of teams, where each team size is $x$.
-----Example-----
Input
4
7
4 2 4 1 4 3 4
5
2 1 5 4 3
1
1
4
1 1 1 3
Output
3
1
0
2
-----Note-----
In the first test case of the example, it is possible to construct two teams of size $3$: the first team is $[1, 2, 4]$ and the second team is $[4, 4, 4]$. Note, that there are some other ways to construct two valid teams of size $3$.
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.
Natsuki and her friends were taken to the space by an alien and made friends with a lot of aliens. During the space travel, she discovered that aliens’ hands were often very different from humans’. Generally speaking, in a kind of aliens, there are N fingers and M bend rules on a hand. Each bend rule describes that a finger A always bends when a finger B bends. However, this rule does not always imply that the finger B bends when the finger A bends.
When she were counting numbers with the fingers, she was anxious how many numbers her alien friends can count with the fingers. However, because some friends had too complicated rule sets, she could not calculate those. Would you write a program for her?
Input
N M
S1 D1
S2 D2
.
.
.
SM DM
The first line contains two integers N and M (1 ≤ N ≤ 1000, 0 ≤ M ≤ 1000) in this order. The following M lines mean bend rules. Each line contains two integers Si and Di in this order, which mean that the finger Di always bends when the finger Si bends. Any finger appears at most once in S.
Output
Calculate how many numbers her alien friends can count with the fingers. Print the answer modulo 1000000007 in a line.
Examples
Input
5 4
2 3
3 4
4 3
5 4
Output
10
Input
5 5
1 2
2 3
3 4
4 5
5 1
Output
2
Input
5 0
Output
32
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 array of $n$ integers $a_1$, $a_2$, ..., $a_n$, and a set $b$ of $k$ distinct integers from $1$ to $n$.
In one operation, you may choose two integers $i$ and $x$ ($1 \le i \le n$, $x$ can be any integer) and assign $a_i := x$. This operation can be done only if $i$ does not belong to the set $b$.
Calculate the minimum number of operations you should perform so the array $a$ is increasing (that is, $a_1 < a_2 < a_3 < \dots < a_n$), or report that it is impossible.
-----Input-----
The first line contains two integers $n$ and $k$ ($1 \le n \le 5 \cdot 10^5$, $0 \le k \le n$) — the size of the array $a$ and the set $b$, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$).
Then, if $k \ne 0$, the third line follows, containing $k$ integers $b_1$, $b_2$, ..., $b_k$ ($1 \le b_1 < b_2 < \dots < b_k \le n$). If $k = 0$, this line is skipped.
-----Output-----
If it is impossible to make the array $a$ increasing using the given operations, print $-1$.
Otherwise, print one integer — the minimum number of operations you have to perform.
-----Examples-----
Input
7 2
1 2 1 1 3 5 1
3 5
Output
4
Input
3 3
1 3 2
1 2 3
Output
-1
Input
5 0
4 3 1 2 3
Output
2
Input
10 3
1 3 5 6 12 9 8 10 13 15
2 4 9
Output
3
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.
Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction.
Pavel has a plan: a permutation p and a sequence b_1, b_2, ..., b_{n}, consisting of zeros and ones. Each second Pavel move skewer on position i to position p_{i}, and if b_{i} equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions.
Unfortunately, not every pair of permutation p and sequence b suits Pavel. What is the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements? Note that after changing the permutation should remain a permutation as well.
There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation p and a sequence b suit him if there is an integer k (k ≥ 2n), so that after k seconds each skewer visits each of the 2n placements.
It can be shown that some suitable pair of permutation p and sequence b exists for any n.
-----Input-----
The first line contain the integer n (1 ≤ n ≤ 2·10^5) — the number of skewers.
The second line contains a sequence of integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation, according to which Pavel wants to move the skewers.
The third line contains a sequence b_1, b_2, ..., b_{n} consisting of zeros and ones, according to which Pavel wants to reverse the skewers.
-----Output-----
Print single integer — the minimum total number of elements in the given permutation p and the given sequence b he needs to change so that every skewer will visit each of 2n placements.
-----Examples-----
Input
4
4 3 2 1
0 1 1 1
Output
2
Input
3
2 3 1
0 0 0
Output
1
-----Note-----
In the first example Pavel can change the permutation to 4, 3, 1, 2.
In the second example Pavel can change any element of b to 1.
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 order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game.
Tzuyu gave Sana two integers $a$ and $b$ and a really important quest.
In order to complete the quest, Sana has to output the smallest possible value of ($a \oplus x$) + ($b \oplus x$) for any given $x$, where $\oplus$ denotes the bitwise XOR operation.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows.
The only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^{9}$).
-----Output-----
For each testcase, output the smallest possible value of the given expression.
-----Example-----
Input
6
6 12
4 9
59 832
28 14
4925 2912
1 1
Output
10
13
891
18
6237
0
-----Note-----
For the first test case Sana can choose $x=4$ and the value will be ($6 \oplus 4$) + ($12 \oplus 4$) = $2 + 8$ = $10$. It can be shown that this is the smallest possible value.
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.
##Task:
You have to write a function **pattern** which creates the following pattern upto n number of rows. *If the Argument is 0 or a Negative Integer then it should return "" i.e. empty string.*
##Examples:
pattern(4):
1234
234
34
4
pattern(6):
123456
23456
3456
456
56
6
```Note: There are no blank spaces```
```Hint: Use \n in string to jump to next line```
Write your solution by modifying this code:
```python
def pattern(n):
```
Your solution should implemented in the function "pattern". The i
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 have $n$ barrels lined up in a row, numbered from left to right from one. Initially, the $i$-th barrel contains $a_i$ liters of water.
You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $x$ and $y$ (the $x$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $x$ to barrel $y$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them.
Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times.
Some examples: if you have four barrels, each containing $5$ liters of water, and $k = 1$, you may pour $5$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $[5, 0, 5, 10]$, and the difference between the maximum and the minimum is $10$; if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $0$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$) — the number of barrels and the number of pourings you can make.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^{9}$), where $a_i$ is the initial amount of water the $i$-th barrel has.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $k$ times.
-----Example-----
Input
2
4 1
5 5 5 5
3 2
0 0 0
Output
10
0
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.
Create a function with two arguments that will return an array of the first (n) multiples of (x).
Assume both the given number and the number of times to count will be positive numbers greater than 0.
Return the results as an array (or list in Python, Haskell or Elixir).
Examples:
```python
count_by(1,10) #should return [1,2,3,4,5,6,7,8,9,10]
count_by(2,5) #should return [2,4,6,8,10]
```
Write your solution by modifying this code:
```python
def count_by(x, n):
```
Your solution should implemented in the function "count_by". The i
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 the number pledged for a year, current value and name of the month, return string that gives information about the challenge status:
- ahead of schedule
- behind schedule
- on track
- challenge is completed
Examples:
`(12, 1, "February")` - should return `"You are on track."`
`(12, 1, "March")` - should return `"You are 1 behind schedule."`
`(12, 5, "March")` - should return `"You are 3 ahead of schedule."`
`(12, 12, "September")` - should return `"Challenge is completed."`
Details:
- you do not need to do any prechecks (input will always be a natural number and correct name of the month)
- months should be as even as possible (i.e. for 100 items: January, February, March and April - 9, other months 8)
- count only the item for completed months (i.e. for March check the values from January and February) and it means that for January you should always return `"You are on track."`.
Write your solution by modifying this code:
```python
def check_challenge(pledged, current, month):
```
Your solution should implemented in the function "check_challenge". The i
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.
Guy-Manuel and Thomas are going to build a polygon spaceship.
You're given a strictly convex (i. e. no three points are collinear) polygon P which is defined by coordinates of its vertices. Define P(x,y) as a polygon obtained by translating P by vector \overrightarrow {(x,y)}. The picture below depicts an example of the translation:
<image>
Define T as a set of points which is the union of all P(x,y) such that the origin (0,0) lies in P(x,y) (both strictly inside and on the boundary). There is also an equivalent definition: a point (x,y) lies in T only if there are two points A,B in P such that \overrightarrow {AB} = \overrightarrow {(x,y)}. One can prove T is a polygon too. For example, if P is a regular triangle then T is a regular hexagon. At the picture below P is drawn in black and some P(x,y) which contain the origin are drawn in colored:
<image>
The spaceship has the best aerodynamic performance if P and T are similar. Your task is to check whether the polygons P and T are [similar](https://tinyurl.com/vp5m7vl).
Input
The first line of input will contain a single integer n (3 ≤ n ≤ 10^5) — the number of points.
The i-th of the next n lines contains two integers x_i, y_i (|x_i|, |y_i| ≤ 10^9), denoting the coordinates of the i-th vertex.
It is guaranteed that these points are listed in counterclockwise order and these points form a strictly convex polygon.
Output
Output "YES" in a separate line, if P and T are similar. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower).
Examples
Input
4
1 0
4 1
3 4
0 3
Output
YES
Input
3
100 86
50 0
150 0
Output
nO
Input
8
0 0
1 0
2 1
3 3
4 6
3 6
2 5
1 3
Output
YES
Note
The following image shows the first sample: both P and T are squares. The second sample was shown in the statements.
<image>
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 only difference between easy and hard versions is constraints.
Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions — and he won't start playing until he gets all of them.
Each day (during the morning) Ivan earns exactly one burle.
There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening).
Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles.
There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day.
Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 1000$) — the number of types of microtransactions and the number of special offers in the game shop.
The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 1000$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $1000$.
The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 1000, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day.
-----Output-----
Print one integer — the minimum day when Ivan can order all microtransactions he wants and actually start playing.
-----Examples-----
Input
5 6
1 2 0 2 0
2 4
3 3
1 5
1 2
1 5
2 3
Output
8
Input
5 3
4 2 1 3 2
3 5
4 2
2 5
Output
20
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.
One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.
Vitaly sees a building of n floors and 2·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2·m from left to right, then the j-th flat of the i-th floor has windows 2·j - 1 and 2·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.
Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100) — the number of floors in the house and the number of flats on each floor respectively.
Next n lines describe the floors from top to bottom and contain 2·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'.
-----Output-----
Print a single integer — the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping.
-----Examples-----
Input
2 2
0 0 0 1
1 0 1 1
Output
3
Input
1 3
1 1 0 1 0 0
Output
2
-----Note-----
In the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.
In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off.
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 lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya loves long lucky numbers very much. He is interested in the minimum lucky number d that meets some condition. Let cnt(x) be the number of occurrences of number x in number d as a substring. For example, if d = 747747, then cnt(4) = 2, cnt(7) = 4, cnt(47) = 2, cnt(74) = 2. Petya wants the following condition to fulfil simultaneously: cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.
Input
The single line contains four integers a1, a2, a3 and a4 (1 ≤ a1, a2, a3, a4 ≤ 106).
Output
On the single line print without leading zeroes the answer to the problem — the minimum lucky number d such, that cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes).
Examples
Input
2 2 1 1
Output
4774
Input
4 7 3 1
Output
-1
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 + 1$ cities, numbered from $0$ to $n$. $n$ roads connect these cities, the $i$-th road connects cities $i - 1$ and $i$ ($i \in [1, n]$).
Each road has a direction. The directions are given by a string of $n$ characters such that each character is either L or R. If the $i$-th character is L, it means that the $i$-th road initially goes from the city $i$ to the city $i - 1$; otherwise it goes from the city $i - 1$ to the city $i$.
A traveler would like to visit as many cities of this country as possible. Initially, they will choose some city to start their journey from. Each day, the traveler must go from the city where they currently are to a neighboring city using one of the roads, and they can go along a road only if it is directed in the same direction they are going; i. e., if a road is directed from city $i$ to the city $i + 1$, it is possible to travel from $i$ to $i + 1$, but not from $i + 1$ to $i$. After the traveler moves to a neighboring city, all roads change their directions to the opposite ones. If the traveler cannot go from their current city to a neighboring city, their journey ends; it is also possible to end the journey whenever the traveler wants to.
The goal of the traveler is to visit as many different cities as possible (they can visit a city multiple times, but only the first visit is counted). For each city $i$, calculate the maximum number of different cities the traveler can visit during exactly one journey if they start in the city $i$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
Each test case consists of two lines. The first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$). The second line contains the string $s$ consisting of exactly $n$ characters, each character is either L or R.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print $n + 1$ integers. The $i$-th integer should be equal to the maximum number of different cities the traveler can visit during one journey if this journey starts in the $i$-th city.
-----Examples-----
Input
2
6
LRRRLL
3
LRL
Output
1 3 2 3 1 3 2
1 4 1 4
-----Note-----
None
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 permutation is a sequence of $n$ integers from $1$ to $n$, in which all numbers occur exactly once. For example, $[1]$, $[3, 5, 2, 1, 4]$, $[1, 3, 2]$ are permutations, and $[2, 3, 2]$, $[4, 3, 1]$, $[0]$ are not.
Polycarp was presented with a permutation $p$ of numbers from $1$ to $n$. However, when Polycarp came home, he noticed that in his pocket, the permutation $p$ had turned into an array $q$ according to the following rule:
$q_i = \max(p_1, p_2, \ldots, p_i)$.
Now Polycarp wondered what lexicographically minimal and lexicographically maximal permutations could be presented to him.
An array $a$ of length $n$ is lexicographically smaller than an array $b$ of length $n$ if there is an index $i$ ($1 \le i \le n$) such that the first $i-1$ elements of arrays $a$ and $b$ are the same, and the $i$-th element of the array $a$ is less than the $i$-th element of the array $b$. For example, the array $a=[1, 3, 2, 3]$ is lexicographically smaller than the array $b=[1, 3, 4, 2]$.
For example, if $n=7$ and $p=[3, 2, 4, 1, 7, 5, 6]$, then $q=[3, 3, 4, 4, 7, 7, 7]$ and the following permutations could have been as $p$ initially:
$[3, 1, 4, 2, 7, 5, 6]$ (lexicographically minimal permutation);
$[3, 1, 4, 2, 7, 6, 5]$;
$[3, 2, 4, 1, 7, 5, 6]$;
$[3, 2, 4, 1, 7, 6, 5]$ (lexicographically maximum permutation).
For a given array $q$, find the lexicographically minimal and lexicographically maximal permutations that could have been originally presented to Polycarp.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$).
The second line of each test case contains $n$ integers $q_1, q_2, \ldots, q_n$ ($1 \le q_i \le n$).
It is guaranteed that the array $q$ was obtained by applying the rule from the statement to some permutation $p$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output two lines:
on the first line output $n$ integers — lexicographically minimal permutation that could have been originally presented to Polycarp;
on the second line print $n$ integers — lexicographically maximal permutation that could have been originally presented to Polycarp;
-----Examples-----
Input
4
7
3 3 4 4 7 7 7
4
1 2 3 4
7
3 4 5 5 5 7 7
1
1
Output
3 1 4 2 7 5 6
3 2 4 1 7 6 5
1 2 3 4
1 2 3 4
3 4 5 1 2 7 6
3 4 5 2 1 7 6
1
1
-----Note-----
None
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.
It's been a tough week at work and you are stuggling to get out of bed in the morning.
While waiting at the bus stop you realise that if you could time your arrival to the nearest minute you could get valuable extra minutes in bed.
There is a bus that goes to your office every 15 minute, the first bus is at `06:00`, and the last bus is at `00:00`.
Given that it takes 5 minutes to walk from your front door to the bus stop, implement a function that when given the curent time will tell you much time is left, before you must leave to catch the next bus.
## Examples
```
"05:00" => 55
"10:00" => 10
"12:10" => 0
"12:11" => 14
```
### Notes
1. Return the number of minutes till the next bus
2. Input will be formatted as `HH:MM` (24-hour clock)
3. The input time might be after the buses have stopped running, i.e. after `00:00`
Write your solution by modifying this code:
```python
def bus_timer(current_time):
```
Your solution should implemented in the function "bus_timer". The i
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.
## Your Job
Find the sum of all multiples of `n` below `m`
## Keep in Mind
* `n` and `m` are natural numbers (positive integers)
* `m` is **excluded** from the multiples
## Examples
Write your solution by modifying this code:
```python
def sum_mul(n, m):
```
Your solution should implemented in the function "sum_mul". The i
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 Chamber of Secrets has been opened again" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny.
The Chamber of Secrets is an n × m rectangular grid in which some of the cells are columns. A light ray (and a basilisk's gaze) passes through the columns without changing its direction. But with some spell we can make a column magic to reflect the light ray (or the gaze) in all four directions when it receives the ray. This is shown in the figure below.
<image> The left light ray passes through a regular column, and the right ray — through the magic column.
The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk's gaze directly dies immediately. But if someone meets a basilisk's gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction of the upper right cell) from that position.
<image> This figure illustrates the first sample test.
Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it's impossible to secure the chamber.
Input
The first line of the input contains two integer numbers n and m (2 ≤ n, m ≤ 1000). Each of the next n lines contains m characters. Each character is either "." or "#" and represents one cell of the Chamber grid. It's "." if the corresponding cell is empty and "#" if it's a regular column.
Output
Print the minimum number of columns to make magic or -1 if it's impossible to do.
Examples
Input
3 3
.#.
...
.#.
Output
2
Input
4 3
##.
...
.#.
.#.
Output
2
Note
The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns.
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 have a sequence of k numbers: d_0,d_1,...,d_{k - 1}.
Process the following q queries in order:
- The i-th query contains three integers n_i, x_i, and m_i.
Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray}
Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i).
Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0).
-----Constraints-----
- All values in input are integers.
- 1 \leq k, q \leq 5000
- 0 \leq d_i \leq 10^9
- 2 \leq n_i \leq 10^9
- 0 \leq x_i \leq 10^9
- 2 \leq m_i \leq 10^9
-----Input-----
Input is given from Standard Input in the following format:
k q
d_0 d_1 ... d_{k - 1}
n_1 x_1 m_1
n_2 x_2 m_2
:
n_q x_q m_q
-----Output-----
Print q lines.
The i-th line should contain the response to the i-th query.
-----Sample Input-----
3 1
3 1 4
5 3 2
-----Sample Output-----
1
For the first query, the sequence {a_j} will be 3,6,7,11,14.
- (a_0~\textrm{mod}~2) > (a_1~\textrm{mod}~2)
- (a_1~\textrm{mod}~2) < (a_2~\textrm{mod}~2)
- (a_2~\textrm{mod}~2) = (a_3~\textrm{mod}~2)
- (a_3~\textrm{mod}~2) > (a_4~\textrm{mod}~2)
Thus, the response to this query should be 1.
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 Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other.
The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station s to station f by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including s and f). After that on the same day at evening Grisha will ride from station t to station f by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean.
The boys have already chosen three stations a, b and c for each of several following days, one of them should be station s on that day, another should be station f, and the remaining should be station t. They became interested how they should choose these stations s, f, t so that the number Grisha will count is as large as possible. They asked you for help.
Input
The first line contains two integers n and q (2 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of stations and the number of days.
The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi ≤ n). The integer pi means that there is a route between stations pi and i. It is guaranteed that it's possible to reach every station from any other.
The next q lines contains three integers a, b and c each (1 ≤ a, b, c ≤ n) — the ids of stations chosen by boys for some day. Note that some of these ids could be same.
Output
Print q lines. In the i-th of these lines print the maximum possible number Grisha can get counting when the stations s, t and f are chosen optimally from the three stations on the i-th day.
Examples
Input
3 2
1 1
1 2 3
2 3 3
Output
2
3
Input
4 1
1 2 3
1 2 3
Output
2
Note
In the first example on the first day if s = 1, f = 2, t = 3, Misha would go on the route 1 <image> 2, and Grisha would go on the route 3 <image> 1 <image> 2. He would see the text at the stations 1 and 2. On the second day, if s = 3, f = 2, t = 3, both boys would go on the route 3 <image> 1 <image> 2. Grisha would see the text at 3 stations.
In the second examle if s = 1, f = 3, t = 2, Misha would go on the route 1 <image> 2 <image> 3, and Grisha would go on the route 2 <image> 3 and would see the text at both stations.
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 20XX, the Aizu Chuo Road, which has a total distance of 58km and 6 sections from Atsushiokanomachi, Kitakata City to Minamiaizucho, is scheduled to be completed and opened.
For half a year after opening, the toll will be halved for vehicles that pass the departure IC or arrival IC between 17:30 and 19:30 and have a mileage of 40km or less. However, the charge will be in units of 50 yen and rounded up. The table below is a list of fares and distances.
<image>
<image>
For example, from Kitakata (2) to Aizuwakamatsu (4), the fare is 450 yen and the distance is 12km. If it is half price time zone, it will be 250 yen.
Create a program that calculates and outputs the charge by inputting the departure IC, departure IC transit time, arrival IC, and arrival IC transit time. However, the time entered will be the value in 24-hour notation. In addition, even if you pass at exactly 17:30 and 19:30, it will be included in the half price time zone.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
d
hd md
a
ha ma
The first line gives the departure IC number d (1 ≤ d ≤ 7), and the second line gives the time hd (0 ≤ hd ≤ 23) and minutes md (0 ≤ md ≤ 59) of the departure IC transit time. ..
The arrival IC number a (1 ≤ a ≤ 7) is given on the third line, and the time ha (0 ≤ ha ≤ 23) and minute ma (0 ≤ ma ≤ 59) of the arrival IC transit time are given on the fourth line. ..
Output
The toll (integer) is output to one line for each data set.
Example
Input
2
17 25
4
17 45
4
17 25
7
19 35
0
Output
250
1300
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.
Nate U. Smith runs a railroad company in a metropolitan area. In addition to his railroad company, there are rival railroad companies in this metropolitan area. The two companies are in a competitive relationship with each other.
In this metropolitan area, all lines are routed by the line segment connecting the stations at both ends in order to facilitate train operation. In addition, all lines are laid elevated or underground to avoid the installation of railroad crossings. Existing lines either run elevated over the entire line or run underground over the entire line.
By the way, recently, two blocks A and B in this metropolitan area have been actively developed, so Nate's company decided to establish a new line between these blocks. As with the conventional line, this new line wants to use a line segment with A and B at both ends as a route, but since there is another line in the middle of the route, the entire line will be laid either elevated or underground. That is impossible. Therefore, we decided to lay a part of the line elevated and the rest underground. At this time, where the new line intersects with the existing line of the company, in order to make the connection between the new line and the existing line convenient, if the existing line is elevated, the new line is also elevated, and if the existing line is underground, the new line is new. The line should also run underground. Also, where the new line intersects with the existing line of another company, the new line should run underground if the existing line is elevated, and the new line should run underground if the existing line is underground. It does not matter whether stations A and B are located elevated or underground.
As a matter of course, a doorway must be provided where the new line moves from elevated to underground or from underground to elevated. However, since it costs money to provide entrances and exits, we would like to minimize the number of entrances and exits to be provided on new routes. Thinking it would require your help as a programmer, Nate called you to his company.
Your job is to create a program that finds the minimum number of doorways that must be provided on a new line, given the location of stations A and B, and information about existing lines.
The figure below shows the contents of the input and output given as a sample.
<image>
<image>
Input
The first line of input contains a single positive integer, which represents the number of datasets. Each dataset is given in the following format.
> xa ya xb yb
> n
> xs1 ys1 xt1 yt1 o1 l1
> xs2 ys2 xt2 yt2 o2 l2
> ...
> xsn ysn xtn ytn on ln
>
However, (xa, ya) and (xb, yb) represent the coordinates of station A and station B, respectively. N is a positive integer less than or equal to 100 that represents the number of existing routes. (xsi, ysi) and (ysi, yti) represent the coordinates of the start and end points of the i-th existing line. oi is an integer representing the owner of the i-th existing line. This is either 1 or 0, where 1 indicates that the route is owned by the company and 0 indicates that the route is owned by another company. li is an integer that indicates whether the i-th existing line is elevated or underground. This is also either 1 or 0, where 1 indicates that the line is elevated and 0 indicates that it is underground.
The x and y coordinate values that appear during input range from -10000 to 10000 (both ends are included in the range). In addition, in each data set, it may be assumed that the following conditions are satisfied for all routes including new routes. Here, when two points are very close, it means that the distance between the two points is 10-9 or less.
* There can be no other intersection very close to one.
* No other line runs very close to the end of one line.
* Part or all of the two lines do not overlap.
Output
For each dataset, output the minimum number of doorways that must be provided on one line.
Sample Input
2
-10 1 10 1
Four
-6 2 -2 -2 0 1
-6 -2 -2 2 1 0
6 2 2 -2 0 0
6 -2 2 2 1 1
8 12 -7 -3
8
4 -5 4 -2 1 1
4 -2 4 9 1 0
6 9 6 14 1 1
-7 6 7 6 0 0
1 0 1 10 0 0
-5 0 -5 10 0 1
-7 0 7 0 0 1
-1 0 -1 -5 0 1
Output for the Sample Input
1
3
Example
Input
2
-10 1 10 1
4
-6 2 -2 -2 0 1
-6 -2 -2 2 1 0
6 2 2 -2 0 0
6 -2 2 2 1 1
8 12 -7 -3
8
4 -5 4 -2 1 1
4 -2 4 9 1 0
6 9 6 14 1 1
-7 6 7 6 0 0
1 0 1 10 0 0
-5 0 -5 10 0 1
-7 0 7 0 0 1
-1 0 -1 -5 0 1
Output
1
3
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 Kannon-do in the mountain behind Ichiro's house. There are 30 steps from the foot to this Kannon-do, and Ichiro goes to Kannon-do almost every day. Ichiro can go up the stairs up to 3 steps with one foot. While playing, I noticed that there are so many types of stair climbing (the number of steps to skip).
So I decided to do 10 different climbs a day and try all the climbs. However, if you are familiar with mathematics, you should know that such a thing will end the life of Ichiro.
In order to convince Ichiro that Ichiro's plan is not feasible, Ichiro will enter all the steps of the stairs n and make 10 different ways of climbing a day. Create a program that outputs the number of years required to execute. Calculate a year as 365 days. If you need even one day, it will be one year. 365 days is one year, and 366 days is two years.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, one integer n (1 ≤ n ≤ 30) representing the number of stages is given on one line.
The number of datasets does not exceed 30.
Output
For each dataset, Ichiro outputs the number of years (integer) required to execute all the climbs on one line.
Example
Input
1
10
20
25
0
Output
1
1
34
701
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
There is a grid of $ R \ times C $ squares with $ (0, 0) $ in the upper left and $ (R-1, C-1) $ in the lower right. When you are in a square ($ e $, $ f $), from there $ (e + 1, f) $, $ (e-1, f) $, $ (e, f + 1) $, $ (e) , f-1) $, $ (e, 0) $, $ (e, C-1) $, $ (0, f) $, $ (R-1, f) $ can be moved at a cost of $ 1 $ .. However, you cannot go out of the grid. When moving from the mass $ (a_i, a_j) $ to $ (b_i, b_j) $, calculate the cost of the shortest path and the remainder of the total number of shortest path combinations divided by $ 10 ^ 9 + 7 $.
However, the combination of routes shall be distinguished by the method of movement. For example, if your current location is $ (100, 1) $, you can go to $ (100, 0) $ with the above $ (e, f-1) $ or $ (e, 0) $ to get to $ (100, 0) $ at the shortest cost. You can do it, so count it as $ 2 $.
Constraints
The input satisfies the following conditions.
* $ 1 \ le R, C \ le 500 $
* $ 0 \ le a_i, b_i \ le R --1 $
* $ 0 \ le a_j, b_j \ le C --1 $
* All inputs given are integers.
Input
The input is given in the following format.
$ R $ $ C $ $ a_i $ $ a_j $ $ b_i $ $ b_j $
Output
When moving from the mass $ (a_i, a_j) $ to $ (b_i, b_j) $, the cost of the shortest path and the remainder of the total number of combinations of the shortest paths divided by $ 10 ^ 9 + 7 $ are separated by $ 1 $. Print on a line.
Examples
Input
2 2 0 0 1 1
Output
2 8
Input
1 1 0 0 0 0
Output
0 1
Input
1 10 0 0 0 9
Output
1 1
Input
5 5 0 0 4 4
Output
2 2
Input
421 435 196 169 388 9
Output
43 917334776
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 the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. The school has n servers. Each server has a name and an ip (names aren't necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we'll assume that an nginx command is of form "command ip;" where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers.
[Image]
Each ip is of form "a.b.c.d" where a, b, c and d are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has m commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is "command ip;" Dustin has to replace it with "command ip; #name" where name is the name of the server with ip equal to ip.
Dustin doesn't know anything about nginx, so he panicked again and his friends asked you to do his task for him.
-----Input-----
The first line of input contains two integers n and m (1 ≤ n, m ≤ 1000).
The next n lines contain the names and ips of the servers. Each line contains a string name, name of the server and a string ip, ip of the server, separated by space (1 ≤ |name| ≤ 10, name only consists of English lowercase letters). It is guaranteed that all ip are distinct.
The next m lines contain the commands in the configuration file. Each line is of form "command ip;" (1 ≤ |command| ≤ 10, command only consists of English lowercase letters). It is guaranteed that ip belongs to one of the n school servers.
-----Output-----
Print m lines, the commands in the configuration file after Dustin did his task.
-----Examples-----
Input
2 2
main 192.168.0.2
replica 192.168.0.1
block 192.168.0.1;
proxy 192.168.0.2;
Output
block 192.168.0.1; #replica
proxy 192.168.0.2; #main
Input
3 5
google 8.8.8.8
codeforces 212.193.33.27
server 138.197.64.57
redirect 138.197.64.57;
block 8.8.8.8;
cf 212.193.33.27;
unblock 8.8.8.8;
check 138.197.64.57;
Output
redirect 138.197.64.57; #server
block 8.8.8.8; #google
cf 212.193.33.27; #codeforces
unblock 8.8.8.8; #google
check 138.197.64.57; #server
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.
Some number of people (this number is even) have stood in a circle. The people stand in the circle evenly. They are numbered clockwise starting from a person with the number $1$. Each person is looking through the circle's center at the opposite person.
A sample of a circle of $6$ persons. The orange arrows indicate who is looking at whom.
You don't know the exact number of people standing in the circle (but this number is even, no doubt). It is known that the person with the number $a$ is looking at the person with the number $b$ (and vice versa, of course). What is the number associated with a person being looked at by the person with the number $c$? If, for the specified $a$, $b$, and $c$, no such circle exists, output -1.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.
Each test case consists of one line containing three distinct integers $a$, $b$, $c$ ($1 \le a,b,c \le 10^8$).
-----Output-----
For each test case output in a separate line a single integer $d$ — the number of the person being looked at by the person with the number $c$ in a circle such that the person with the number $a$ is looking at the person with the number $b$. If there are multiple solutions, print any of them. Output $-1$ if there's no circle meeting the given conditions.
-----Examples-----
Input
7
6 2 4
2 3 1
2 4 10
5 3 4
1 3 2
2 5 4
4 3 2
Output
8
-1
-1
-1
4
1
-1
-----Note-----
In the first test case, there's a desired circle of $8$ people. The person with the number $6$ will look at the person with the number $2$ and the person with the number $8$ will look at the person with the number $4$.
In the second test case, there's no circle meeting the conditions. If the person with the number $2$ is looking at the person with the number $3$, the circle consists of $2$ people because these persons are neighbors. But, in this case, they must have the numbers $1$ and $2$, but it doesn't meet the problem's conditions.
In the third test case, the only circle with the persons with the numbers $2$ and $4$ looking at each other consists of $4$ people. Therefore, the person with the number $10$ doesn't occur in the circle.
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.
R3D3 spent some time on an internship in MDCS. After earning enough money, he decided to go on a holiday somewhere far, far away. He enjoyed suntanning, drinking alcohol-free cocktails and going to concerts of popular local bands. While listening to "The White Buttons" and their hit song "Dacan the Baker", he met another robot for whom he was sure is the love of his life. Well, his summer, at least. Anyway, R3D3 was too shy to approach his potential soulmate, so he decided to write her a love letter. However, he stumbled upon a problem. Due to a terrorist threat, the Intergalactic Space Police was monitoring all letters sent in the area. Thus, R3D3 decided to invent his own alphabet, for which he was sure his love would be able to decipher.
There are n letters in R3D3’s alphabet, and he wants to represent each letter as a sequence of '0' and '1', so that no letter’s sequence is a prefix of another letter's sequence. Since the Intergalactic Space Communications Service has lately introduced a tax for invented alphabets, R3D3 must pay a certain amount of money for each bit in his alphabet’s code (check the sample test for clarifications). He is too lovestruck to think clearly, so he asked you for help.
Given the costs c_0 and c_1 for each '0' and '1' in R3D3’s alphabet, respectively, you should come up with a coding for the alphabet (with properties as above) with minimum total cost.
-----Input-----
The first line of input contains three integers n (2 ≤ n ≤ 10^8), c_0 and c_1 (0 ≤ c_0, c_1 ≤ 10^8) — the number of letters in the alphabet, and costs of '0' and '1', respectively.
-----Output-----
Output a single integer — minimum possible total a cost of the whole alphabet.
-----Example-----
Input
4 1 2
Output
12
-----Note-----
There are 4 letters in the alphabet. The optimal encoding is "00", "01", "10", "11". There are 4 zeroes and 4 ones used, so the total cost is 4·1 + 4·2 = 12.
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 two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$.
Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"].
Your task is to print the median (the middle element) of this list. For the example above this will be "bc".
It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$.
-----Input-----
The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) — the length of strings.
The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters.
The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters.
It is guaranteed that $s$ is lexicographically less than $t$.
It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$.
-----Output-----
Print one string consisting exactly of $k$ lowercase Latin letters — the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$.
-----Examples-----
Input
2
az
bf
Output
bc
Input
5
afogk
asdji
Output
alvuw
Input
6
nijfvj
tvqhwp
Output
qoztvz
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
Find the angle between the two given angles θ1 and θ2. Here, the angle between them is defined as "θ1 & plus; t'when t'is the one with the smallest absolute value among the t satisfying θ1 & plus; t = θ2 − t" as shown in the figure below.
A image of angles
Constraints
The input satisfies the following conditions.
* Angle is expressed in degrees
* 0 ≤ θ1, θ2 <360
* θ1 and θ2 are integers
* | θ1 − θ2 | ≠ 180
* No input is given that gives the answer [0,0.0001] or [359.999,360)
Input
The input is given in the following format.
θ1
θ2
Output
Output the angle between θ1 and θ2 in one line in the range [0,360) in degrees. However, it must not include an error exceeding 0.0001.
Examples
Input
10
20
Output
15.0
Input
20
350
Output
5.0
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 water bottle with the shape of a rectangular prism whose base is a square of side a~\mathrm{cm} and whose height is b~\mathrm{cm}. (The thickness of the bottle can be ignored.)
We will pour x~\mathrm{cm}^3 of water into the bottle, and gradually tilt the bottle around one of the sides of the base.
When will the water be spilled? More formally, find the maximum angle in which we can tilt the bottle without spilling any water.
-----Constraints-----
- All values in input are integers.
- 1 \leq a \leq 100
- 1 \leq b \leq 100
- 1 \leq x \leq a^2b
-----Input-----
Input is given from Standard Input in the following format:
a b x
-----Output-----
Print the maximum angle in which we can tilt the bottle without spilling any water, in degrees.
Your output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6}.
-----Sample Input-----
2 2 4
-----Sample Output-----
45.0000000000
This bottle has a cubic shape, and it is half-full. The water gets spilled when we tilt the bottle more than 45 degrees.
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 integer array $a_1, a_2, \dots, a_n$ good if $a_i \neq i$ for each $i$.
Let $F(a)$ be the number of pairs $(i, j)$ ($1 \le i < j \le n$) such that $a_i + a_j = i + j$.
Let's say that an array $a_1, a_2, \dots, a_n$ is excellent if:
$a$ is good;
$l \le a_i \le r$ for each $i$;
$F(a)$ is the maximum possible among all good arrays of size $n$.
Given $n$, $l$ and $r$, calculate the number of excellent arrays modulo $10^9 + 7$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first and only line of each test case contains three integers $n$, $l$, and $r$ ($2 \le n \le 2 \cdot 10^5$; $-10^9 \le l \le 1$; $n \le r \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of excellent arrays modulo $10^9 + 7$.
-----Examples-----
Input
4
3 0 3
4 -3 5
42 -33 55
69 -42 146
Output
4
10
143922563
698570404
-----Note-----
In the first test case, it can be proven that the maximum $F(a)$ among all good arrays $a$ is equal to $2$. The excellent arrays are:
$[2, 1, 2]$;
$[0, 3, 2]$;
$[2, 3, 2]$;
$[3, 0, 1]$.
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 text consisting of n lines. Each line contains some space-separated words, consisting of lowercase English letters.
We define a syllable as a string that contains exactly one vowel and any arbitrary number (possibly none) of consonants. In English alphabet following letters are considered to be vowels: 'a', 'e', 'i', 'o', 'u' and 'y'.
Each word of the text that contains at least one vowel can be divided into syllables. Each character should be a part of exactly one syllable. For example, the word "mamma" can be divided into syllables as "ma" and "mma", "mam" and "ma", and "mamm" and "a". Words that consist of only consonants should be ignored.
The verse patterns for the given text is a sequence of n integers p_1, p_2, ..., p_{n}. Text matches the given verse pattern if for each i from 1 to n one can divide words of the i-th line in syllables in such a way that the total number of syllables is equal to p_{i}.
You are given the text and the verse pattern. Check, if the given text matches the given verse pattern.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of lines in the text.
The second line contains integers p_1, ..., p_{n} (0 ≤ p_{i} ≤ 100) — the verse pattern.
Next n lines contain the text itself. Text consists of lowercase English letters and spaces. It's guaranteed that all lines are non-empty, each line starts and ends with a letter and words are separated by exactly one space. The length of each line doesn't exceed 100 characters.
-----Output-----
If the given text matches the given verse pattern, then print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
2 2 3
intel
code
ch allenge
Output
YES
Input
4
1 2 3 1
a
bcdefghi
jklmnopqrstu
vwxyz
Output
NO
Input
4
13 11 15 15
to be or not to be that is the question
whether tis nobler in the mind to suffer
the slings and arrows of outrageous fortune
or to take arms against a sea of troubles
Output
YES
-----Note-----
In the first sample, one can split words into syllables in the following way: in-tel
co-de
ch al-len-ge
Since the word "ch" in the third line doesn't contain vowels, we can ignore it. As the result we get 2 syllabels in first two lines and 3 syllables in the third one.
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.
# altERnaTIng cAsE <=> ALTerNAtiNG CaSe
Define `String.prototype.toAlternatingCase` (or a similar function/method *such as* `to_alternating_case`/`toAlternatingCase`/`ToAlternatingCase` in your selected language; **see the initial solution for details**) such that each lowercase letter becomes uppercase and each uppercase letter becomes lowercase. For example:
``` haskell
toAlternatingCase "hello world" `shouldBe` "HELLO WORLD"
toAlternatingCase "HELLO WORLD" `shouldBe` "hello world"
toAlternatingCase "hello WORLD" `shouldBe` "HELLO world"
toAlternatingCase "HeLLo WoRLD" `shouldBe` "hEllO wOrld"
toAlternatingCase "12345" `shouldBe` "12345"
toAlternatingCase "1a2b3c4d5e" `shouldBe` "1A2B3C4D5E"
```
```C++
string source = "HeLLo WoRLD";
string upperCase = to_alternating_case(source);
cout << upperCase << endl; // outputs: hEllO wOrld
```
As usual, your function/method should be pure, i.e. it should **not** mutate the original string.
Write your solution by modifying this code:
```python
def to_alternating_case(string):
```
Your solution should implemented in the function "to_alternating_case". The i
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 goal of this Kata is to reduce the passed integer to a single digit (if not already) by converting the number to binary, taking the sum of the binary digits, and if that sum is not a single digit then repeat the process.
- n will be an integer such that 0 < n < 10^20
- If the passed integer is already a single digit there is no need to reduce
For example given 5665 the function should return 5:
```
5665 --> (binary) 1011000100001
1011000100001 --> (sum of binary digits) 5
```
Given 123456789 the function should return 1:
```
123456789 --> (binary) 111010110111100110100010101
111010110111100110100010101 --> (sum of binary digits) 16
16 --> (binary) 10000
10000 --> (sum of binary digits) 1
```
Write your solution by modifying this code:
```python
def single_digit(n):
```
Your solution should implemented in the function "single_digit". The i
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.
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits.
Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a_1, a_2, ..., a_{n} / k and b_1, b_2, ..., b_{n} / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit b_{i} and is divisible by a_{i} if represented as an integer.
To represent the block of length k as an integer, let's write it out as a sequence c_1, c_2,...,c_{k}. Then the integer is calculated as the result of the expression c_1·10^{k} - 1 + c_2·10^{k} - 2 + ... + c_{k}.
Pasha asks you to calculate the number of good phone numbers of length n, for the given k, a_{i} and b_{i}. As this number can be too big, print it modulo 10^9 + 7.
-----Input-----
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k.
The second line of the input contains n / k space-separated positive integers — sequence a_1, a_2, ..., a_{n} / k (1 ≤ a_{i} < 10^{k}).
The third line of the input contains n / k space-separated positive integers — sequence b_1, b_2, ..., b_{n} / k (0 ≤ b_{i} ≤ 9).
-----Output-----
Print a single integer — the number of good phone numbers of length n modulo 10^9 + 7.
-----Examples-----
Input
6 2
38 56 49
7 3 4
Output
8
Input
8 2
1 22 3 44
5 4 3 2
Output
32400
-----Note-----
In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
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 this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
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$ students standing in a circle in some order. The index of the $i$-th student is $p_i$. It is guaranteed that all indices of students are distinct integers from $1$ to $n$ (i. e. they form a permutation).
Students want to start a round dance. A clockwise round dance can be started if the student $2$ comes right after the student $1$ in clockwise order (there are no students between them), the student $3$ comes right after the student $2$ in clockwise order, and so on, and the student $n$ comes right after the student $n - 1$ in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student $i$ should be right after the student $i - 1$ in counterclockwise order (this condition should be met for every $i$ from $2$ to $n$).
For example, if the indices of students listed in clockwise order are $[2, 3, 4, 5, 1]$, then they can start a clockwise round dance. If the students have indices $[3, 2, 1, 4]$ in clockwise order, then they can start a counterclockwise round dance.
Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle.
You have to answer $q$ independent queries.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 200$) — the number of queries. Then $q$ queries follow.
The first line of the query contains one integer $n$ ($1 \le n \le 200$) — the number of students.
The second line of the query contains a permutation of indices $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the index of the $i$-th student (in clockwise order). It is guaranteed that all $p_i$ are distinct integers from $1$ to $n$ (i. e. they form a permutation).
-----Output-----
For each query, print the answer on it. If a round dance can be started with the given order of students, print "YES". Otherwise print "NO".
-----Example-----
Input
5
4
1 2 3 4
3
1 3 2
5
1 2 3 5 4
1
1
5
3 2 1 5 4
Output
YES
YES
NO
YES
YES
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.
Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion: An 'L' indicates he should move one unit left. An 'R' indicates he should move one unit right. A 'U' indicates he should move one unit up. A 'D' indicates he should move one unit down.
But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.
-----Input-----
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
-----Output-----
If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.
-----Examples-----
Input
RRU
Output
-1
Input
UDUR
Output
1
Input
RUUR
Output
2
-----Note-----
In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.
In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.
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.
I have a lot of friends. Every friend is very small.
I often go out with my friends. Put some friends in your backpack and go out together.
Every morning I decide which friends to go out with that day. Put friends one by one in an empty backpack.
I'm not very strong. Therefore, there is a limit to the weight of friends that can be carried at the same time.
I keep friends so that I don't exceed the weight limit. The order in which you put them in depends on your mood.
I won't stop as long as I still have friends to put in. I will never stop.
…… By the way, how many patterns are there in total for the combination of friends in the backpack?
Input
N W
w1
w2
..
..
..
wn
On the first line of input, the integer N (1 ≤ N ≤ 200) and the integer W (1 ≤ W ≤ 10,000) are written in this order, separated by blanks. The integer N represents the number of friends, and the integer W represents the limit of the weight that can be carried at the same time. It cannot be carried if the total weight is greater than W.
The next N lines contain an integer that represents the weight of the friend. The integer wi (1 ≤ wi ≤ 10,000) represents the weight of the i-th friend.
Output
How many possible combinations of friends are finally in the backpack? Divide the total number by 1,000,000,007 and output the remainder. Note that 1,000,000,007 are prime numbers.
Note that "no one is in the backpack" is counted as one.
Examples
Input
4 8
1
2
7
9
Output
2
Input
4 25
20
15
20
15
Output
4
Input
6 37
5
9
13
18
26
33
Output
6
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 end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces).
The shop assistant told the teacher that there are m puzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consists of f_1 pieces, the second one consists of f_2 pieces and so on.
Ms. Manana doesn't want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. Let A be the number of pieces in the largest puzzle that the teacher buys and B be the number of pieces in the smallest such puzzle. She wants to choose such n puzzles that A - B is minimum possible. Help the teacher and find the least possible value of A - B.
-----Input-----
The first line contains space-separated integers n and m (2 ≤ n ≤ m ≤ 50). The second line contains m space-separated integers f_1, f_2, ..., f_{m} (4 ≤ f_{i} ≤ 1000) — the quantities of pieces in the puzzles sold in the shop.
-----Output-----
Print a single integer — the least possible difference the teacher can obtain.
-----Examples-----
Input
4 6
10 12 10 7 5 22
Output
5
-----Note-----
Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.
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 2xxx, an expedition team landing on a planet found strange objects made by an ancient species living on that planet. They are transparent boxes containing opaque solid spheres (Figure 1). There are also many lithographs which seem to contain positions and radiuses of spheres.
<image>
Figure 1: A strange object
Initially their objective was unknown, but Professor Zambendorf found the cross section formed by a horizontal plane plays an important role. For example, the cross section of an object changes as in Figure 2 by sliding the plane from bottom to top.
<image>
Figure 2: Cross sections at different positions
He eventually found that some information is expressed by the transition of the number of connected figures in the cross section, where each connected figure is a union of discs intersecting or touching each other, and each disc is a cross section of the corresponding solid sphere. For instance, in Figure 2, whose geometry is described in the first sample dataset later, the number of connected figures changes as 0, 1, 2, 1, 2, 3, 2, 1, and 0, at z = 0.0000, 162.0000, 167.0000, 173.0004, 185.0000, 191.9996, 198.0000, 203.0000, and 205.0000, respectively. By assigning 1 for increment and 0 for decrement, the transitions of this sequence can be expressed by an 8-bit binary number 11011000.
For helping further analysis, write a program to determine the transitions when sliding the horizontal plane from bottom (z = 0) to top (z = 36000).
Input
The input consists of a series of datasets. Each dataset begins with a line containing a positive integer, which indicates the number of spheres N in the dataset. It is followed by N lines describing the centers and radiuses of the spheres. Each of the N lines has four positive integers Xi, Yi, Zi, and Ri (i = 1, . . . , N) describing the center and the radius of the i-th sphere, respectively.
You may assume 1 ≤ N ≤ 100, 1 ≤ Ri ≤ 2000, 0 < Xi - Ri < Xi + Ri < 4000, 0 < Yi - Ri < Yi + Ri < 16000, and 0 < Zi - Ri < Zi + Ri < 36000. Each solid sphere is defined as the set of all points (x, y, z) satisfying (x - Xi)2 + (y - Yi)2 + (z - Zi)2 ≤ Ri2.
A sphere may contain other spheres. No two spheres are mutually tangent. Every Zi ± Ri and minimum/maximum z coordinates of a circle formed by the intersection of any two spheres differ from each other by at least 0.01.
The end of the input is indicated by a line with one zero.
Output
For each dataset, your program should output two lines. The first line should contain an integer M indicating the number of transitions. The second line should contain an M-bit binary number that expresses the transitions of the number of connected figures as specified above.
Example
Input
3
95 20 180 18
125 20 185 18
40 27 195 10
1
5 5 5 4
2
5 5 5 4
5 5 5 3
2
5 5 5 4
5 7 5 3
16
2338 3465 29034 710
1571 14389 25019 842
1706 8015 11324 1155
1899 4359 33815 888
2160 10364 20511 1264
2048 8835 23706 1906
2598 13041 23679 618
1613 11112 8003 1125
1777 4754 25986 929
2707 9945 11458 617
1153 10358 4305 755
2462 8450 21838 934
1822 11539 10025 1639
1473 11939 12924 638
1388 8519 18653 834
2239 7384 32729 862
0
Output
8
11011000
2
10
2
10
2
10
28
1011100100110101101000101100
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're given an array $a_1, \ldots, a_n$ of $n$ non-negative integers.
Let's call it sharpened if and only if there exists an integer $1 \le k \le n$ such that $a_1 < a_2 < \ldots < a_k$ and $a_k > a_{k+1} > \ldots > a_n$. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: The arrays $[4]$, $[0, 1]$, $[12, 10, 8]$ and $[3, 11, 15, 9, 7, 4]$ are sharpened; The arrays $[2, 8, 2, 8, 6, 5]$, $[0, 1, 1, 0]$ and $[2, 5, 6, 9, 8, 8]$ are not sharpened.
You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any $i$ ($1 \le i \le n$) such that $a_i>0$ and assign $a_i := a_i - 1$.
Tell if it's possible to make the given array sharpened using some number (possibly zero) of these operations.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 15\ 000$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$).
The second line of each test case contains a sequence of $n$ non-negative integers $a_1, \ldots, a_n$ ($0 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, output a single line containing "Yes" (without quotes) if it's possible to make the given array sharpened using the described operations, or "No" (without quotes) otherwise.
-----Example-----
Input
10
1
248618
3
12 10 8
6
100 11 15 9 7 8
4
0 1 1 0
2
0 0
2
0 1
2
1 0
2
1 1
3
0 1 0
3
1 0 1
Output
Yes
Yes
Yes
No
No
Yes
Yes
Yes
Yes
No
-----Note-----
In the first and the second test case of the first test, the given array is already sharpened.
In the third test case of the first test, we can transform the array into $[3, 11, 15, 9, 7, 4]$ (decrease the first element $97$ times and decrease the last element $4$ times). It is sharpened because $3 < 11 < 15$ and $15 > 9 > 7 > 4$.
In the fourth test case of the first test, it's impossible to make the given array sharpened.
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.
Are you going to Scarborough Fair?
Parsley, sage, rosemary and thyme.
Remember me to one who lives there.
He once was the true love of mine.
Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.
Willem asks his friend, Grick for directions, Grick helped them, and gave them a task.
Although the girl wants to help, Willem insists on doing it by himself.
Grick gave Willem a string of length n.
Willem needs to do m operations, each operation has four parameters l, r, c_1, c_2, which means that all symbols c_1 in range [l, r] (from l-th to r-th, including l and r) are changed into c_2. String is 1-indexed.
Grick wants to know the final string after all the m operations.
-----Input-----
The first line contains two integers n and m (1 ≤ n, m ≤ 100).
The second line contains a string s of length n, consisting of lowercase English letters.
Each of the next m lines contains four parameters l, r, c_1, c_2 (1 ≤ l ≤ r ≤ n, c_1, c_2 are lowercase English letters), separated by space.
-----Output-----
Output string s after performing m operations described above.
-----Examples-----
Input
3 1
ioi
1 1 i n
Output
noi
Input
5 3
wxhak
3 3 h x
1 5 x a
1 3 w g
Output
gaaak
-----Note-----
For the second example:
After the first operation, the string is wxxak.
After the second operation, the string is waaak.
After the third operation, the string is gaaak.
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 will be given an integer a and a string s consisting of lowercase English letters as input.
Write a program that prints s if a is not less than 3200 and prints red if a is less than 3200.
-----Constraints-----
- 2800 \leq a < 5000
- s is a string of length between 1 and 10 (inclusive).
- Each character of s is a lowercase English letter.
-----Input-----
Input is given from Standard Input in the following format:
a
s
-----Output-----
If a is not less than 3200, print s; if a is less than 3200, print red.
-----Sample Input-----
3200
pink
-----Sample Output-----
pink
a = 3200 is not less than 3200, so we print s = pink.
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 secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 ≤ N ≤ 100) is given in the first line. On the second line, the number X (0 ≤ X ≤ N) of those who belong to the organization $ A $, followed by the identification number ai (1 ≤ ai ≤ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 ≤ Y ≤ N) of those who belong to the organization $ B $, followed by the identification number bi (1 ≤ bi ≤ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 ≤ Z ≤ N) of the person who owns the product $ C $, followed by the identification number ci (1 ≤ ci ≤ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2
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.
**An [isogram](https://en.wikipedia.org/wiki/Isogram)** (also known as a "nonpattern word") is a logological term for a word or phrase without a repeating letter. It is also used by some to mean a word or phrase in which each letter appears the same number of times, not necessarily just once.
You task is to write a method `isogram?` that takes a string argument and returns true if the string has the properties of being an isogram and false otherwise. Anything that is not a string is not an isogram (ints, nils, etc.)
**Properties:**
- must be a string
- cannot be nil or empty
- each letter appears the same number of times (not necessarily just once)
- letter case is not important (= case insensitive)
- non-letter characters (e.g. hyphens) should be ignored
Write your solution by modifying this code:
```python
def is_isogram(word):
```
Your solution should implemented in the function "is_isogram". The i
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 is a harder version of the problem. In this version, n ≤ 300 000.
Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence.
To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him.
We remind that bracket sequence s is called correct if:
* s is empty;
* s is equal to "(t)", where t is correct bracket sequence;
* s is equal to t_1 t_2, i.e. concatenation of t_1 and t_2, where t_1 and t_2 are correct bracket sequences.
For example, "(()())", "()" are correct, while ")(" and "())" are not.
The cyclical shift of the string s of length n by k (0 ≤ k < n) is a string formed by a concatenation of the last k symbols of the string s with the first n - k symbols of string s. For example, the cyclical shift of string "(())()" by 2 equals "()(())".
Cyclical shifts i and j are considered different, if i ≠ j.
Input
The first line contains an integer n (1 ≤ n ≤ 300 000), the length of the string.
The second line contains a string, consisting of exactly n characters, where each of the characters is either "(" or ")".
Output
The first line should contain a single integer — the largest beauty of the string, which can be achieved by swapping some two characters.
The second line should contain integers l and r (1 ≤ l, r ≤ n) — the indices of two characters, which should be swapped in order to maximize the string's beauty.
In case there are several possible swaps, print any of them.
Examples
Input
10
()()())(()
Output
5
8 7
Input
12
)(()(()())()
Output
4
5 10
Input
6
)))(()
Output
0
1 1
Note
In the first example, we can swap 7-th and 8-th character, obtaining a string "()()()()()". The cyclical shifts by 0, 2, 4, 6, 8 of this string form a correct bracket sequence.
In the second example, after swapping 5-th and 10-th character, we obtain a string ")(())()()(()". The cyclical shifts by 11, 7, 5, 3 of this string form a correct bracket sequence.
In the third example, swap of any two brackets results in 0 cyclical shifts being correct bracket sequences.
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 Johnny has recently learned about set theory. Now he is studying binary relations. You've probably heard the term "equivalence relation". These relations are very important in many areas of mathematics. For example, the equality of the two numbers is an equivalence relation.
A set ρ of pairs (a, b) of elements of some set A is called a binary relation on set A. For two elements a and b of the set A we say that they are in relation ρ, if pair $(a, b) \in \rho$, in this case we use a notation $a \stackrel{\rho}{\sim} b$.
Binary relation is equivalence relation, if: It is reflexive (for any a it is true that $a \stackrel{\rho}{\sim} a$); It is symmetric (for any a, b it is true that if $a \stackrel{\rho}{\sim} b$, then $b \stackrel{\rho}{\sim} a$); It is transitive (if $a \stackrel{\rho}{\sim} b$ and $b \stackrel{\rho}{\sim} c$, than $a \stackrel{\rho}{\sim} c$).
Little Johnny is not completely a fool and he noticed that the first condition is not necessary! Here is his "proof":
Take any two elements, a and b. If $a \stackrel{\rho}{\sim} b$, then $b \stackrel{\rho}{\sim} a$ (according to property (2)), which means $a \stackrel{\rho}{\sim} a$ (according to property (3)).
It's very simple, isn't it? However, you noticed that Johnny's "proof" is wrong, and decided to show him a lot of examples that prove him wrong.
Here's your task: count the number of binary relations over a set of size n such that they are symmetric, transitive, but not an equivalence relations (i.e. they are not reflexive).
Since their number may be very large (not 0, according to Little Johnny), print the remainder of integer division of this number by 10^9 + 7.
-----Input-----
A single line contains a single integer n (1 ≤ n ≤ 4000).
-----Output-----
In a single line print the answer to the problem modulo 10^9 + 7.
-----Examples-----
Input
1
Output
1
Input
2
Output
3
Input
3
Output
10
-----Note-----
If n = 1 there is only one such relation — an empty one, i.e. $\rho = \varnothing$. In other words, for a single element x of set A the following is hold: [Image].
If n = 2 there are three such relations. Let's assume that set A consists of two elements, x and y. Then the valid relations are $\rho = \varnothing$, ρ = {(x, x)}, ρ = {(y, y)}. It is easy to see that the three listed binary relations are symmetric and transitive relations, but they are not equivalence relations.
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 beta testing the new secret Terraria update. This update will add quests to the game!
Simply, the world map can be represented as an array of length $n$, where the $i$-th column of the world has height $a_i$.
There are $m$ quests you have to test. The $j$-th of them is represented by two integers $s_j$ and $t_j$. In this quest, you have to go from the column $s_j$ to the column $t_j$. At the start of the quest, you are appearing at the column $s_j$.
In one move, you can go from the column $x$ to the column $x-1$ or to the column $x+1$. In this version, you have Spectre Boots, which allow you to fly. Since it is a beta version, they are bugged, so they only allow you to fly when you are going up and have infinite fly duration. When you are moving from the column with the height $p$ to the column with the height $q$, then you get some amount of fall damage. If the height $p$ is greater than the height $q$, you get $p - q$ fall damage, otherwise you fly up and get $0$ damage.
For each of the given quests, determine the minimum amount of fall damage you can get during this quest.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($2 \le n \le 10^5; 1 \le m \le 10^5$) — the number of columns in the world and the number of quests you have to test, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the height of the $i$-th column of the world.
The next $m$ lines describe quests. The $j$-th of them contains two integers $s_j$ and $t_j$ ($1 \le s_j, t_j \le n; s_j \ne t_j$), which means you have to move from the column $s_j$ to the column $t_j$ during the $j$-th quest.
Note that $s_j$ can be greater than $t_j$.
-----Output-----
Print $m$ integers. The $j$-th of them should be the minimum amount of fall damage you can get during the $j$-th quest completion.
-----Examples-----
Input
7 6
10 8 9 6 8 12 7
1 2
1 7
4 6
7 1
3 5
4 2
Output
2
10
0
7
3
1
-----Note-----
None
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 square field of size $n \times n$ in which two cells are marked. These cells can be in the same row or column.
You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes.
For example, if $n=4$ and a rectangular field looks like this (there are asterisks in the marked cells):
$$ \begin{matrix} . & . & * & . \\ . & . & . & . \\ * & . & . & . \\ . & . & . & . \\ \end{matrix} $$
Then you can mark two more cells as follows
$$ \begin{matrix} * & . & * & . \\ . & . & . & . \\ * & . & * & . \\ . & . & . & . \\ \end{matrix} $$
If there are several possible solutions, then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 400$). Then $t$ test cases follow.
The first row of each test case contains a single integer $n$ ($2 \le n \le 400$) — the number of rows and columns in the table.
The following $n$ lines each contain $n$ characters '.' or '*' denoting empty and marked cells, respectively.
It is guaranteed that the sums of $n$ for all test cases do not exceed $400$.
It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column.
It is guaranteed that the solution exists.
-----Output-----
For each test case, output $n$ rows of $n$ characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them.
-----Examples-----
Input
6
4
..*.
....
*...
....
2
*.
.*
2
.*
.*
3
*.*
...
...
5
.....
..*..
.....
.*...
.....
4
....
....
*...
*...
Output
*.*.
....
*.*.
....
**
**
**
**
*.*
*.*
...
.....
.**..
.....
.**..
.....
....
....
**..
**..
-----Note-----
None
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.
Anchored Balloon
A balloon placed on the ground is connected to one or more anchors on the ground with ropes. Each rope is long enough to connect the balloon and the anchor. No two ropes cross each other. Figure E-1 shows such a situation.
<image>
Figure E-1: A balloon and ropes on the ground
Now the balloon takes off, and your task is to find how high the balloon can go up with keeping the rope connections. The positions of the anchors are fixed. The lengths of the ropes and the positions of the anchors are given. You may assume that these ropes have no weight and thus can be straightened up when pulled to whichever directions. Figure E-2 shows the highest position of the balloon for the situation shown in Figure E-1.
<image>
Figure E-2: The highest position of the balloon
Input
The input consists of multiple datasets, each in the following format.
> n
> x1 y1 l1
> ...
> xn yn ln
>
The first line of a dataset contains an integer n (1 ≤ n ≤ 10) representing the number of the ropes. Each of the following n lines contains three integers, xi, yi, and li, separated by a single space. Pi = (xi, yi) represents the position of the anchor connecting the i-th rope, and li represents the length of the rope. You can assume that −100 ≤ xi ≤ 100, −100 ≤ yi ≤ 100, and 1 ≤ li ≤ 300. The balloon is initially placed at (0, 0) on the ground. You can ignore the size of the balloon and the anchors.
You can assume that Pi and Pj represent different positions if i ≠ j. You can also assume that the distance between Pi and (0, 0) is less than or equal to li−1. This means that the balloon can go up at least 1 unit high.
Figures E-1 and E-2 correspond to the first dataset of Sample Input below.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output a single line containing the maximum height that the balloon can go up. The error of the value should be no greater than 0.00001. No extra characters should appear in the output.
Sample Input
3
10 10 20
10 -10 20
-10 10 120
1
10 10 16
2
10 10 20
10 -10 20
2
100 0 101
-90 0 91
2
0 0 53
30 40 102
3
10 10 20
10 -10 20
-10 -10 20
3
1 5 13
5 -3 13
-3 -3 13
3
98 97 168
-82 -80 193
-99 -96 211
4
90 -100 160
-80 -80 150
90 80 150
80 80 245
4
85 -90 290
-80 -80 220
-85 90 145
85 90 170
5
0 0 4
3 0 5
-3 0 5
0 3 5
0 -3 5
10
95 -93 260
-86 96 211
91 90 177
-81 -80 124
-91 91 144
97 94 165
-90 -86 194
89 85 167
-93 -80 222
92 -84 218
0
Output for the Sample Input
17.3205081
16.0000000
17.3205081
13.8011200
53.0000000
14.1421356
12.0000000
128.3928757
94.1879092
131.1240816
4.0000000
72.2251798
Example
Input
3
10 10 20
10 -10 20
-10 10 120
1
10 10 16
2
10 10 20
10 -10 20
2
100 0 101
-90 0 91
2
0 0 53
30 40 102
3
10 10 20
10 -10 20
-10 -10 20
3
1 5 13
5 -3 13
-3 -3 13
3
98 97 168
-82 -80 193
-99 -96 211
4
90 -100 160
-80 -80 150
90 80 150
80 80 245
4
85 -90 290
-80 -80 220
-85 90 145
85 90 170
5
0 0 4
3 0 5
-3 0 5
0 3 5
0 -3 5
10
95 -93 260
-86 96 211
91 90 177
-81 -80 124
-91 91 144
97 94 165
-90 -86 194
89 85 167
-93 -80 222
92 -84 218
0
Output
17.3205081
16.0000000
17.3205081
13.8011200
53.0000000
14.1421356
12.0000000
128.3928757
94.1879092
131.1240816
4.0000000
72.2251798
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 numbers of all offices in the new building of the Tax Office of IT City will have lucky numbers.
Lucky number is a number that consists of digits 7 and 8 only. Find the maximum number of offices in the new building of the Tax Office given that a door-plate can hold a number not longer than n digits.
-----Input-----
The only line of input contains one integer n (1 ≤ n ≤ 55) — the maximum length of a number that a door-plate can hold.
-----Output-----
Output one integer — the maximum number of offices, than can have unique lucky numbers not longer than n digits.
-----Examples-----
Input
2
Output
6
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 array $a$ consisting of $n$ integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from $a$ to make it a good array. Recall that the prefix of the array $a=[a_1, a_2, \dots, a_n]$ is a subarray consisting several first elements: the prefix of the array $a$ of length $k$ is the array $[a_1, a_2, \dots, a_k]$ ($0 \le k \le n$).
The array $b$ of length $m$ is called good, if you can obtain a non-decreasing array $c$ ($c_1 \le c_2 \le \dots \le c_{m}$) from it, repeating the following operation $m$ times (initially, $c$ is empty): select either the first or the last element of $b$, remove it from $b$, and append it to the end of the array $c$.
For example, if we do $4$ operations: take $b_1$, then $b_{m}$, then $b_{m-1}$ and at last $b_2$, then $b$ becomes $[b_3, b_4, \dots, b_{m-3}]$ and $c =[b_1, b_{m}, b_{m-1}, b_2]$.
Consider the following example: $b = [1, 2, 3, 4, 4, 2, 1]$. This array is good because we can obtain non-decreasing array $c$ from it by the following sequence of operations: take the first element of $b$, so $b = [2, 3, 4, 4, 2, 1]$, $c = [1]$; take the last element of $b$, so $b = [2, 3, 4, 4, 2]$, $c = [1, 1]$; take the last element of $b$, so $b = [2, 3, 4, 4]$, $c = [1, 1, 2]$; take the first element of $b$, so $b = [3, 4, 4]$, $c = [1, 1, 2, 2]$; take the first element of $b$, so $b = [4, 4]$, $c = [1, 1, 2, 2, 3]$; take the last element of $b$, so $b = [4]$, $c = [1, 1, 2, 2, 3, 4]$; take the only element of $b$, so $b = []$, $c = [1, 1, 2, 2, 3, 4, 4]$ — $c$ is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of $a$ to delete (erase), to make $a$ to be a good array. Note that the required length can be $0$.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from $a$ to make it a good array.
-----Example-----
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
-----Note-----
In the first test case of the example, the array $a$ is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array $a$ is not good. Let's erase first $4$ elements of $a$, the result is $[4, 5, 2]$. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good.
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 is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved.
Pikachu is a cute and friendly pokémon living in the wild pikachu herd.
But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist.
First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct.
As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$.
The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$.
Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon.
Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation.
Help Andrew and the pokémon, or team R will realize their tricky plan!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively.
The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon.
$i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap.
-----Example-----
Input
3
3 1
1 3 2
1 2
2 2
1 2
1 2
1 2
7 5
1 2 5 4 3 6 7
1 2
6 7
3 4
1 2
2 3
Output
3
4
2
2
2
9
10
10
10
9
11
-----Note-----
Let's look at the third test case:
Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$.
After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$.
After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$.
After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$.
After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$.
After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
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.
G --Derangement / Derangement
Story
Person D is doing research on permutations. At one point, D found a permutation class called "Derangement". "It's cool to have a perfect permutation !!! And it starts with D !!!", the person of D who had a crush on Chunibyo decided to rewrite all the permutations given to him.
Problem
Given a permutation of length n, p = (p_1 p_2 ... p_n). The operation of swapping the i-th element and the j-th element requires the cost of (p_i + p_j) \ times | i-j |. At this time, find the minimum cost required to make p into a perfect permutation using only the above swap operation.
However, the fact that the permutation q is a perfect permutation means that q_i \ neq i holds for 1 \ leq i \ leq n. For example, (5 3 1 2 4) is a perfect permutation, but (3 2 5 4 1) is not a perfect permutation because the fourth number is 4.
Input
The input consists of the following format.
n
p_1 p_2 ... p_n
The first row consists of one integer representing the length n of the permutation. The second line consists of n integers, and the i-th integer represents the i-th element of the permutation p. Each is separated by a single blank character.
Constraints:
* 2 \ leq n \ leq 100
* 1 \ leq p_i \ leq n, i \ neq j ⇔ p_i \ neq p_j
Output
Print the minimum cost to sort p into a complete permutation on one line. Be sure to start a new line at the end of the line.
Sample Input 1
Five
1 2 3 5 4
Sample Output 1
7
After swapping 1 and 2, swapping 1 and 3 yields (2 3 1 5 4), which is a complete permutation.
Sample Input 2
Five
5 3 1 2 4
Sample Output 2
0
It is a perfect permutation from the beginning.
Sample Input 3
Ten
1 3 4 5 6 2 7 8 9 10
Sample Output 3
38
Example
Input
5
1 2 3 5 4
Output
7
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.
Snuke received a triangle as a birthday present. The coordinates of the three vertices were (x_1, y_1), (x_2, y_2), and (x_3, y_3).
He wants to draw two circles with the same radius inside the triangle such that the two circles do not overlap (but they may touch). Compute the maximum possible radius of the circles.
Constraints
* 0 ≤ x_i, y_i ≤ 1000
* The coordinates are integers.
* The three points are not on the same line.
Input
The input is given from Standard Input in the following format:
x_1 y_1
x_2 y_2
x_3 y_3
Output
Print the maximum possible radius of the circles. The absolute error or the relative error must be at most 10^{-9}.
Examples
Input
0 0
1 1
2 0
Output
0.292893218813
Input
3 1
1 5
4 9
Output
0.889055514217
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.
Define a cyclic sequence of size $n$ as an array $s$ of length $n$, in which $s_n$ is adjacent to $s_1$.
Muxii has a ring represented by a cyclic sequence $a$ of size $n$.
However, the ring itself hates equal adjacent elements. So if two adjacent elements in the sequence are equal at any time, one of them will be erased immediately. The sequence doesn't contain equal adjacent elements initially.
Muxii can perform the following operation until the sequence becomes empty:
Choose an element in $a$ and erase it.
For example, if ring is $[1, 2, 4, 2, 3, 2]$, and Muxii erases element $4$, then ring would erase one of the elements equal to $2$, and the ring will become $[1, 2, 3, 2]$.
Muxii wants to find the maximum number of operations he could perform.
Note that in a ring of size $1$, its only element isn't considered adjacent to itself (so it's not immediately erased).
-----Input-----
Each test contains multiple test cases. The first line contains a single integer $t$ ($1\leq t\leq 100$) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ ($1\leq n\leq 100$) — the size of the cyclic sequence.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq a_i\leq n$) — the sequence itself.
It's guaranteed that $a_i\ne a_{i+1}$ for $1\leq i<n$.
It's guaranteed that $a_n\ne a_1$ when $n>1$.
-----Output-----
For each test case, output a single integer — the maximum number of operations Muxii can perform.
-----Examples-----
Input
3
4
1 2 3 2
4
1 2 1 2
1
1
Output
4
3
1
-----Note-----
In the first test case, you can erase the second element first, then erase the remaining elements one by one in any order. In total, you can perform the operation $4$ times. Note that if you erase the first element first, then the sequence will be turned into $[2,3,2]$ and then immediately become $[2,3]$.
In the second test case, you can erase the first element first, then the sequence becomes $[2,1]$. Then you can erase all remaining elements one by one in any order.
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.
Find the number, modulo 998244353, of sequences of length N consisting of 0, 1 and 2 such that none of their contiguous subsequences totals to X.
Constraints
* 1 \leq N \leq 3000
* 1 \leq X \leq 2N
* N and X are integers.
Input
Input is given from Standard Input in the following format:
N X
Output
Print the number, modulo 998244353, of sequences that satisfy the condition.
Examples
Input
3 3
Output
14
Input
8 6
Output
1179
Input
10 1
Output
1024
Input
9 13
Output
18402
Input
314 159
Output
459765451
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.
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.
The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.
Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of m pieces of paper in the garland. Calculate what the maximum total area of the pieces of paper in the garland Vasya can get.
-----Input-----
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color.
The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
-----Output-----
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
-----Examples-----
Input
aaabbac
aabbccac
Output
6
Input
a
z
Output
-1
-----Note-----
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.
In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.
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 pet store on sale there are:
$a$ packs of dog food;
$b$ packs of cat food;
$c$ packs of universal food (such food is suitable for both dogs and cats).
Polycarp has $x$ dogs and $y$ cats. Is it possible that he will be able to buy food for all his animals in the store? Each of his dogs and each of his cats should receive one pack of suitable food for it.
-----Input-----
The first line of input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
Then $t$ lines are given, each containing a description of one test case. Each description consists of five integers $a, b, c, x$ and $y$ ($0 \le a,b,c,x,y \le 10^8$).
-----Output-----
For each test case in a separate line, output:
YES, if suitable food can be bought for each of $x$ dogs and for each of $y$ cats;
NO else.
You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
-----Examples-----
Input
7
1 1 4 2 3
0 0 0 0 0
5 5 0 4 6
1 1 1 1 1
50000000 50000000 100000000 100000000 100000000
0 0 0 100000000 100000000
1 3 2 2 5
Output
YES
YES
NO
YES
YES
NO
NO
-----Note-----
None
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 city administration of IT City decided to fix up a symbol of scientific and technical progress in the city's main square, namely an indicator board that shows the effect of Moore's law in real time.
Moore's law is the observation that the number of transistors in a dense integrated circuit doubles approximately every 24 months. The implication of Moore's law is that computer performance as function of time increases exponentially as well.
You are to prepare information that will change every second to display on the indicator board. Let's assume that every second the number of transistors increases exactly 1.000000011 times.
-----Input-----
The only line of the input contains a pair of integers n (1000 ≤ n ≤ 10 000) and t (0 ≤ t ≤ 2 000 000 000) — the number of transistors in the initial time and the number of seconds passed since the initial time.
-----Output-----
Output one number — the estimate of the number of transistors in a dence integrated circuit in t seconds since the initial time. The relative error of your answer should not be greater than 10^{ - 6}.
-----Examples-----
Input
1000 1000000
Output
1011.060722383550382782399454922040
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 received as a gift a very clever robot walking on a rectangular board. Unfortunately, you understood that it is broken and behaves rather strangely (randomly). The board consists of N rows and M columns of cells. The robot is initially at some cell on the i-th row and the j-th column. Then at every step the robot could go to some another cell. The aim is to go to the bottommost (N-th) row. The robot can stay at it's current cell, move to the left, move to the right, or move to the cell below the current. If the robot is in the leftmost column it cannot move to the left, and if it is in the rightmost column it cannot move to the right. At every step all possible moves are equally probable. Return the expected number of step to reach the bottommost row.
Input
On the first line you will be given two space separated integers N and M (1 ≤ N, M ≤ 1000). On the second line you will be given another two space separated integers i and j (1 ≤ i ≤ N, 1 ≤ j ≤ M) — the number of the initial row and the number of the initial column. Note that, (1, 1) is the upper left corner of the board and (N, M) is the bottom right corner.
Output
Output the expected number of steps on a line of itself with at least 4 digits after the decimal point.
Examples
Input
10 10
10 4
Output
0.0000000000
Input
10 14
5 14
Output
18.0038068653
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.
Background
----------
In another dimension, there exist two immortal brothers: Solomon and Goromon. As sworn loyal subjects to the time elemental, Chronixus, both Solomon and Goromon were granted the power to create temporal folds. By sliding through these temporal folds, one can gain entry to parallel dimensions where time moves relatively faster or slower.
Goromon grew dissatisfied and one day betrayed Chronixus by stealing the Temporal Crystal, an artifact used to maintain the time continuum. Chronixus summoned Solomon and gave him the task of tracking down Goromon and retrieving the Temporal Crystal.
Using a map given to Solomon by Chronixus, you must find Goromon's precise location.
Mission Details
---------------
The map is represented as a 2D array. See the example below:
```python
map_example = [[1,3,5],[2,0,10],[-3,1,4],[4,2,4],[1,1,5],[-3,0,12],[2,1,12],[-2,2,6]]
```
Here are what the values of each subarray represent:
- **Time Dilation:** With each additional layer of time dilation entered, time slows by a factor of `2`. At layer `0`, time passes normally. At layer `1`, time passes at half the rate of layer `0`. At layer `2`, time passes at half the rate of layer `1`, and therefore one quarter the rate of layer `0`.
- **Directions** are as follow: `0 = North, 1 = East, 2 = South, 3 = West`
- **Distance Traveled:** This represents the distance traveled relative to the current time dilation layer. See below:
```
The following are equivalent distances (all equal a Standard Distance of 100):
Layer: 0, Distance: 100
Layer: 1, Distance: 50
Layer: 2, Distance: 25
```
For the `mapExample` above:
```
mapExample[0] -> [1,3,5]
1 represents the level shift of time dilation
3 represents the direction
5 represents the distance traveled relative to the current time dilation layer
Solomon's new location becomes [-10,0]
mapExample[1] -> [2,0,10]
At this point, Solomon has shifted 2 layers deeper.
He is now at time dilation layer 3.
Solomon moves North a Standard Distance of 80.
Solomon's new location becomes [-10,80]
mapExample[2] -> [-3,1,4]
At this point, Solomon has shifted out 3 layers.
He is now at time dilation layer 0.
Solomon moves East a Standard Distance of 4.
Solomon's new location becomes [-6,80]
```
Your function should return Goromon's `[x,y]` coordinates.
For `mapExample`, the correct output is `[346,40]`.
Additional Technical Details
----------------------------
- Inputs are always valid.
- Solomon begins his quest at time dilation level `0`, at `[x,y]` coordinates `[0,0]`.
- Time dilation level at any point will always be `0` or greater.
- Standard Distance is the distance at time dilation level `0`.
- For given distance `d` for each value in the array: `d >= 0`.
- Do not mutate the input
**Note from the author:** I made up this story for the purpose of this kata. Any similarities to any fictitious persons or events are purely coincidental.
If you enjoyed this kata, be sure to check out [my other katas](https://www.codewars.com/users/docgunthrop/authored).
Write your solution by modifying this code:
```python
def solomons_quest(arr):
```
Your solution should implemented in the function "solomons_quest". The i
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.
Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph
You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree
The weight of the minimum spanning tree is the sum of the weights on the edges included in it.
-----Input-----
The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph.
-----Output-----
The only line contains an integer x, the weight of the graph's minimum spanning tree.
-----Example-----
Input
4
Output
4
-----Note-----
In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4.
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 like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b.
Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them.
Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 200 000) — the number of residents who stand in the line.
The second line contains n space-separated integers a_{i} (0 ≤ a_{i} ≤ 10^9), where a_{i} is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line.
-----Output-----
If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them.
-----Examples-----
Input
2
11 8
Output
9 10
Input
5
10 9 7 10 6
Output
:(
Input
3
12 3 3
Output
4 4 10
-----Note-----
In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line.
In the second sample it is impossible to achieve the desired result.
In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
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 array $a_1, a_2, \dots, a_n$ and integer $k$.
In one step you can
either choose some index $i$ and decrease $a_i$ by one (make $a_i = a_i - 1$);
or choose two indices $i$ and $j$ and set $a_i$ equal to $a_j$ (make $a_i = a_j$).
What is the minimum number of steps you need to make the sum of array $\sum\limits_{i=1}^{n}{a_i} \le k$? (You are allowed to make values of array negative).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^{15}$) — the size of array $a$ and upper bound on its sum.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) — the array itself.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print one integer — the minimum number of steps to make $\sum\limits_{i=1}^{n}{a_i} \le k$.
-----Examples-----
Input
4
1 10
20
2 69
6 9
7 8
1 2 1 3 1 2 1
10 1
1 2 3 1 2 6 1 6 8 10
Output
10
0
2
7
-----Note-----
In the first test case, you should decrease $a_1$ $10$ times to get the sum lower or equal to $k = 10$.
In the second test case, the sum of array $a$ is already less or equal to $69$, so you don't need to change it.
In the third test case, you can, for example:
set $a_4 = a_3 = 1$;
decrease $a_4$ by one, and get $a_4 = 0$.
As a result, you'll get array $[1, 2, 1, 0, 1, 2, 1]$ with sum less or equal to $8$ in $1 + 1 = 2$ steps.
In the fourth test case, you can, for example:
choose $a_7$ and decrease in by one $3$ times; you'll get $a_7 = -2$;
choose $4$ elements $a_6$, $a_8$, $a_9$ and $a_{10}$ and them equal to $a_7 = -2$.
As a result, you'll get array $[1, 2, 3, 1, 2, -2, -2, -2, -2, -2]$ with sum less or equal to $1$ in $3 + 4 = 7$ steps.
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.
# Write Number in Expanded Form - Part 2
This is version 2 of my ['Write Number in Exanded Form' Kata](https://www.codewars.com/kata/write-number-in-expanded-form).
You will be given a number and you will need to return it as a string in [Expanded Form](https://www.mathplacementreview.com/arithmetic/decimals.php#writing-a-decimal-in-expanded-form). For example:
```python
expanded_form(1.24) # Should return '1 + 2/10 + 4/100'
expanded_form(7.304) # Should return '7 + 3/10 + 4/1000'
expanded_form(0.04) # Should return '4/100'
```
Write your solution by modifying this code:
```python
def expanded_form(num):
```
Your solution should implemented in the function "expanded_form". The i
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.
An X-layered kagami mochi (X ≥ 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.
Lunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?
-----Constraints-----
- 1 ≤ N ≤ 100
- 1 ≤ d_i ≤ 100
- All input values are integers.
-----Input-----
Input is given from Standard Input in the following format:
N
d_1
:
d_N
-----Output-----
Print the maximum number of layers in a kagami mochi that can be made.
-----Sample Input-----
4
10
8
8
6
-----Sample Output-----
3
If we stack the mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, we have a 3-layered kagami mochi, which is the maximum number of layers.
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.
Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.
Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him.
Input
The first line contains three integers n, m, k (1 ≤ n, m ≤ 500, 0 ≤ k < s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze.
Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.
Output
Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#").
It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.
Examples
Input
3 4 2
#..#
..#.
#...
Output
#.X#
X.#.
#...
Input
5 4 5
#...
#.#.
.#..
...#
.#.#
Output
#XXX
#X#.
X#..
...#
.#.#
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.
It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh.
In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x, y) points. At the beginning of the game Bimokh has zero points.
Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn't know how choose the initial sequence in the right way.
Please, help him. Find n distinct integers a1, a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 109.
Input
The first line of input contains two space-separated integers n, k (1 ≤ n ≤ 105; 0 ≤ k ≤ 108).
Output
If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Examples
Input
5 2
Output
1 2 3 4 5
Input
5 3
Output
2 4 3 7 1
Input
7 2
Output
-1
Note
gcd(x, y) is greatest common divisor of x and y.
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 integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
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 define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
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.
Pair of gloves
=============
Winter is coming, you must prepare your ski holidays. The objective of this kata is to determine the number of pair of gloves you can constitute from the gloves you have in your drawer.
A pair of gloves is constituted of two gloves of the same color.
You are given an array containing the color of each glove.
You must return the number of pair you can constitute.
You must not change the input array.
Example :
```python
my_gloves = ["red","green","red","blue","blue"]
number_of_pairs(my_gloves) == 2;// red and blue
red_gloves = ["red","red","red","red","red","red"];
number_of_pairs(red_gloves) == 3; // 3 red pairs
```
Write your solution by modifying this code:
```python
def number_of_pairs(gloves):
```
Your solution should implemented in the function "number_of_pairs". The i
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.
Apparently "Put A Pillow On Your Fridge Day is celebrated on the 29th of May each year, in Europe and the U.S. The day is all about prosperity, good fortune, and having bit of fun along the way."
All seems very weird to me.
Nevertheless, you will be given an array of two strings (s). First find out if the first string contains a fridge... (i've deemed this as being 'n', as it looks like it could hold something).
Then check that the second string has a pillow - deemed 'B' (struggled to get the obvious pillow-esque character).
If the pillow is on top of the fridge - it must be May 29th! Or a weird house... Return true; For clarity, on top means right on top, ie in the same index position.
If the pillow is anywhere else in the 'house', return false;
There may be multiple fridges, and multiple pillows. But you need at least 1 pillow ON TOP of a fridge to return true. Multiple pillows on fridges should return true also.
100 random tests
Write your solution by modifying this code:
```python
def pillow(s):
```
Your solution should implemented in the function "pillow". The i
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 aim of the kata is to try to show how difficult it can be to calculate decimals of an irrational number with a certain precision. We have chosen to get a few decimals of the number "pi" using
the following infinite series (Leibniz 1646–1716):
PI / 4 = 1 - 1/3 + 1/5 - 1/7 + ... which gives an approximation of PI / 4.
http://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
To have a measure of the difficulty we will count how many iterations are needed to calculate PI with a given precision.
There are several ways to determine the precision of the calculus but to keep things easy we will calculate to within epsilon of your language Math::PI constant. In other words we will stop the iterative process when the absolute value of the difference between our calculation and the Math::PI constant of the given language is less than epsilon.
Your function returns an array or an arrayList or a string or a tuple depending on the language (See sample tests) where your approximation of PI has 10 decimals
In Haskell you can use the function "trunc10Dble" (see "Your solution"); in Clojure you can use the function "round" (see "Your solution");in OCaml or Rust the function "rnd10" (see "Your solution") in order to avoid discussions about the result.
Example :
```
your function calculates 1000 iterations and 3.140592653839794 but returns:
iter_pi(0.001) --> [1000, 3.1405926538]
```
Unfortunately, this series converges too slowly to be useful,
as it takes over 300 terms to obtain a 2 decimal place precision.
To obtain 100 decimal places of PI, it was calculated that
one would need to use at least 10^50 terms of this expansion!
About PI : http://www.geom.uiuc.edu/~huberty/math5337/groupe/expresspi.html
Write your solution by modifying this code:
```python
def iter_pi(epsilon):
```
Your solution should implemented in the function "iter_pi". The i
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 set of integers (it can contain equal elements).
You have to split it into two subsets $A$ and $B$ (both of them can contain equal elements or be empty). You have to maximize the value of $mex(A)+mex(B)$.
Here $mex$ of a set denotes the smallest non-negative integer that doesn't exist in the set. For example: $mex(\{1,4,0,2,2,1\})=3$ $mex(\{3,3,2,1,3,0,0\})=4$ $mex(\varnothing)=0$ ($mex$ for empty set)
The set is splitted into two subsets $A$ and $B$ if for any integer number $x$ the number of occurrences of $x$ into this set is equal to the sum of the number of occurrences of $x$ into $A$ and the number of occurrences of $x$ into $B$.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1\leq t\leq 100$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer $n$ ($1\leq n\leq 100$) — the size of the set.
The second line of each testcase contains $n$ integers $a_1,a_2,\dots a_n$ ($0\leq a_i\leq 100$) — the numbers in the set.
-----Output-----
For each test case, print the maximum value of $mex(A)+mex(B)$.
-----Example-----
Input
4
6
0 2 1 5 0 1
3
0 1 2
4
0 2 0 1
6
1 2 3 4 5 6
Output
5
3
4
0
-----Note-----
In the first test case, $A=\left\{0,1,2\right\},B=\left\{0,1,5\right\}$ is a possible choice.
In the second test case, $A=\left\{0,1,2\right\},B=\varnothing$ is a possible choice.
In the third test case, $A=\left\{0,1,2\right\},B=\left\{0\right\}$ is a possible choice.
In the fourth test case, $A=\left\{1,3,5\right\},B=\left\{2,4,6\right\}$ is a possible choice.
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.
Description
In 200X, the mysterious circle K, which operates at K University, announced K Poker, a new game they created on the 4th floor of K East during the university's cultural festival.
At first glance, this game is normal poker, but it is a deeper game of poker with the addition of basic points to the cards.
The basic points of the cards are determined by the pattern written on each card, and the points in the hand are the sum of the five basic points in the hand multiplied by the multiplier of the role of the hand.
By introducing this basic point, even if you make a strong role such as a full house, if the basic point is 0, it will be treated like a pig, and you may even lose to one pair.
Other than that, the rules are the same as for regular poker.
If you wanted to port this K poker to your PC, you decided to write a program to calculate your hand scores.
This game cannot be played by anyone under the age of 18.
Input
The input consists of multiple test cases.
The first line of each test case shows the number of hands N to check.
In the next 4 lines, 13 integers are lined up and the basic points of the card are written in the order of 1-13. The four lines of suits are arranged in the order of spades, clovers, hearts, and diamonds from the top.
The next line is a line of nine integers, representing the magnification of one pair, two pairs, three cards, straight, flush, full house, four card, straight flush, and royal straight flush. The roles are always in ascending order. The magnification of pigs (no role, no pair) is always 0.
The next N lines are five different strings of length 2 that represent your hand. The first letter represents the number on the card and is one of A, 2,3,4,5,6,7,8,9, T, J, Q, K. The second letter represents the suit of the card and is one of S, C, H or D. Each alphabet is A for ace, T for 10, J for jack, Q for queen, K for king, S for spade, C for clover, H for heart, and D for diamond.
All input numbers are in the range [0,10000].
Input ends with EOF.
Output
Output the points in each hand one line at a time.
Insert a blank line between two consecutive test cases.
See the poker page on wikipedia for roles. A straight may straddle an ace and a king, but it is considered a straight only if the hand is 10, jack, queen, king, or ace.
Example
Input
3
0 1 0 1 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 1 0 0 2
0 1 1 0 1 1 1 0 0 0 0 5 5
3 1 1 1 0 1 0 0 1 0 3 0 2
1 1 2 4 5 10 20 50 100
7H 6H 2H 5H 3H
9S 9C 9H 8H 8D
KS KH QH JD TS
10
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9
AS 3D 2C 5D 6H
6S 6C 7D 8H 9C
TS TD QC JD JC
KD KH KC AD 2C
3D 4H 6D 5H 7C
2S KS QS AS JS
TS TD JD JC TH
8H 8D TC 8C 8S
4S 5S 3S 7S 6S
KD QD JD TD AD
Output
25
0
14
0
5
10
15
20
25
30
35
40
45
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.
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column.
Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j].
There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.
If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.
Input
The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).
Output
The output contains a single number — the maximum total gain possible.
Examples
Input
3 3
100 100 100
100 1 100
100 100 100
Output
800
Note
Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].
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.
100 years have passed since the last victory of the man versus computer in Go. Technologies made a huge step forward and robots conquered the Earth! It's time for the final fight between human and robot that will decide the faith of the planet.
The following game was chosen for the fights: initially there is a polynomial P(x) = a_{n}x^{n} + a_{n} - 1x^{n} - 1 + ... + a_1x + a_0, with yet undefined coefficients and the integer k. Players alternate their turns. At each turn, a player pick some index j, such that coefficient a_{j} that stay near x^{j} is not determined yet and sets it to any value (integer or real, positive or negative, 0 is also allowed). Computer moves first. The human will be declared the winner if and only if the resulting polynomial will be divisible by Q(x) = x - k.
Polynomial P(x) is said to be divisible by polynomial Q(x) if there exists a representation P(x) = B(x)Q(x), where B(x) is also some polynomial.
Some moves have been made already and now you wonder, is it true that human can guarantee the victory if he plays optimally?
-----Input-----
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, |k| ≤ 10 000) — the size of the polynomial and the integer k.
The i-th of the following n + 1 lines contain character '?' if the coefficient near x^{i} - 1 is yet undefined or the integer value a_{i}, if the coefficient is already known ( - 10 000 ≤ a_{i} ≤ 10 000). Each of integers a_{i} (and even a_{n}) may be equal to 0.
Please note, that it's not guaranteed that you are given the position of the game where it's computer's turn to move.
-----Output-----
Print "Yes" (without quotes) if the human has winning strategy, or "No" (without quotes) otherwise.
-----Examples-----
Input
1 2
-1
?
Output
Yes
Input
2 100
-10000
0
1
Output
Yes
Input
4 5
?
1
?
1
?
Output
No
-----Note-----
In the first sample, computer set a_0 to - 1 on the first move, so if human can set coefficient a_1 to 0.5 and win.
In the second sample, all coefficients are already set and the resulting polynomial is divisible by x - 100, so the human has won.
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.
Example
Input
3
NNN
NNN
NNN
Output
Taro
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.
Rng is baking cookies.
Initially, he can bake one cookie per second.
He can also eat the cookies baked by himself. When there are x cookies not yet eaten, he can choose to eat all those cookies. After he finishes eating those cookies, the number of cookies he can bake per second becomes x. Note that a cookie always needs to be baked for 1 second, that is, he cannot bake a cookie in 1/x seconds when x > 1. When he choose to eat the cookies, he must eat all of them; he cannot choose to eat only part of them. It takes him A seconds to eat the cookies regardless of how many, during which no cookies can be baked.
He wants to give N cookies to Grandma. Find the shortest time needed to produce at least N cookies not yet eaten.
Constraints
* 1≦N≦10^{12}
* 0≦A≦10^{12}
* A is an integer.
Input
The input is given from Standard Input in the following format:
N A
Output
Print the shortest time needed to produce at least N cookies not yet eaten.
Examples
Input
8 1
Output
7
Input
1000000000000 1000000000000
Output
1000000000000
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 positive integer $n$.
Let $S(x)$ be sum of digits in base 10 representation of $x$, for example, $S(123) = 1 + 2 + 3 = 6$, $S(0) = 0$.
Your task is to find two integers $a, b$, such that $0 \leq a, b \leq n$, $a + b = n$ and $S(a) + S(b)$ is the largest possible among all such pairs.
-----Input-----
The only line of input contains an integer $n$ $(1 \leq n \leq 10^{12})$.
-----Output-----
Print largest $S(a) + S(b)$ among all pairs of integers $a, b$, such that $0 \leq a, b \leq n$ and $a + b = n$.
-----Examples-----
Input
35
Output
17
Input
10000000000
Output
91
-----Note-----
In the first example, you can choose, for example, $a = 17$ and $b = 18$, so that $S(17) + S(18) = 1 + 7 + 1 + 8 = 17$. It can be shown that it is impossible to get a larger answer.
In the second test example, you can choose, for example, $a = 5000000001$ and $b = 4999999999$, with $S(5000000001) + S(4999999999) = 91$. It can be shown that it is impossible to get a larger answer.
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 array of positive integers a_1, a_2, ..., a_{n} × T of length n × T. We know that for any i > n it is true that a_{i} = a_{i} - n. Find the length of the longest non-decreasing sequence of the given array.
-----Input-----
The first line contains two space-separated integers: n, T (1 ≤ n ≤ 100, 1 ≤ T ≤ 10^7). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 300).
-----Output-----
Print a single number — the length of a sought sequence.
-----Examples-----
Input
4 3
3 1 4 2
Output
5
-----Note-----
The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.
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.
Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42.
These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764.
The sum of the squared divisors is 2500 which is 50 * 50, a square!
Given two integers m, n (1 <= m <= n) we want to find all integers
between m and n whose sum of squared divisors is itself a square.
42 is such a number.
The result will be an array of arrays or of tuples (in C an array of Pair) or a string, each subarray having two elements,
first the number whose squared divisors is a square and then the sum
of the squared divisors.
#Examples:
```
list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
list_squared(42, 250) --> [[42, 2500], [246, 84100]]
```
The form of the examples may change according to the language, see `Example Tests:` for more details.
**Note**
In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings.
Write your solution by modifying this code:
```python
def list_squared(m, n):
```
Your solution should implemented in the function "list_squared". The i
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 the judge at a competitive eating competition and you need to choose a winner!
There are three foods at the competition and each type of food is worth a different amount of points.
Points are as follows:
- Chickenwings: 5 points
- Hamburgers: 3 points
- Hotdogs: 2 points
Write a function that helps you create a scoreboard.
It takes as a parameter a list of objects representing the participants, for example:
```
[
{name: "Habanero Hillary", chickenwings: 5 , hamburgers: 17, hotdogs: 11},
{name: "Big Bob" , chickenwings: 20, hamburgers: 4, hotdogs: 11}
]
```
It should return
"name" and "score" properties sorted by score; if scores are equals, sort alphabetically by name.
```
[
{name: "Big Bob", score: 134},
{name: "Habanero Hillary", score: 98}
]
```
Happy judging!
Write your solution by modifying this code:
```python
def scoreboard(who_ate_what):
```
Your solution should implemented in the function "scoreboard". The i
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 program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depends on have already completed.
Some of the tasks in the graph can only be executed on a coprocessor, and the rest can only be executed on the main processor. In one coprocessor call you can send it a set of tasks which can only be executed on it. For each task of the set, all tasks on which it depends must be either already completed or be included in the set. The main processor starts the program execution and gets the results of tasks executed on the coprocessor automatically.
Find the minimal number of coprocessor calls which are necessary to execute the given program.
-----Input-----
The first line contains two space-separated integers N (1 ≤ N ≤ 10^5) — the total number of tasks given, and M (0 ≤ M ≤ 10^5) — the total number of dependencies between tasks.
The next line contains N space-separated integers $E_{i} \in \{0,1 \}$. If E_{i} = 0, task i can only be executed on the main processor, otherwise it can only be executed on the coprocessor.
The next M lines describe the dependencies between tasks. Each line contains two space-separated integers T_1 and T_2 and means that task T_1 depends on task T_2 (T_1 ≠ T_2). Tasks are indexed from 0 to N - 1. All M pairs (T_1, T_2) are distinct. It is guaranteed that there are no circular dependencies between tasks.
-----Output-----
Output one line containing an integer — the minimal number of coprocessor calls necessary to execute the program.
-----Examples-----
Input
4 3
0 1 0 1
0 1
1 2
2 3
Output
2
Input
4 3
1 1 1 0
0 1
0 2
3 0
Output
1
-----Note-----
In the first test, tasks 1 and 3 can only be executed on the coprocessor. The dependency graph is linear, so the tasks must be executed in order 3 -> 2 -> 1 -> 0. You have to call coprocessor twice: first you call it for task 3, then you execute task 2 on the main processor, then you call it for for task 1, and finally you execute task 0 on the main processor.
In the second test, tasks 0, 1 and 2 can only be executed on the coprocessor. Tasks 1 and 2 have no dependencies, and task 0 depends on tasks 1 and 2, so all three tasks 0, 1 and 2 can be sent in one coprocessor call. After that task 3 is executed on the main processor.
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 positive integer n, find k integers (not necessary distinct) such that all these integers are strictly greater than 1, and their product is equal to n.
Input
The first line contains two integers n and k (2 ≤ n ≤ 100000, 1 ≤ k ≤ 20).
Output
If it's impossible to find the representation of n as a product of k numbers, print -1.
Otherwise, print k integers in any order. Their product must be equal to n. If there are multiple answers, print any of them.
Examples
Input
100000 2
Output
2 50000
Input
100000 20
Output
-1
Input
1024 5
Output
2 64 2 2 2
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.
Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters.
Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once.
Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that.
Input
The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@».
Output
If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them.
Examples
Input
a@aa@a
Output
a@a,a@a
Input
a@a@a
Output
No solution
Input
@aa@a
Output
No solution
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.
Read problems statements in Russian here
The Head Chef is studying the motivation and satisfaction level of his chefs . The motivation and satisfaction of a Chef can be represented as an integer . The Head Chef wants to know the N th smallest sum of one satisfaction value and one motivation value for various values of N . The satisfaction and motivation values may correspond to the same chef or different chefs . Given two arrays, the first array denoting the motivation value and the second array denoting the satisfaction value of the chefs . We can get a set of sums(add one element from the first array and one from the second). For each query ( denoted by an integer q_{i} ( i = 1 to Q ) , Q denotes number of queries ) , find the q_{i} th element in the set of sums ( in non-decreasing order ) .
------ Input ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a two space seperated integers K and Q denoting the number of chefs and the number of queries .
The second line of each test case contains K space-separated integers A_{1}, A_{2}, ..., A_{K} denoting the motivation of Chefs.
The third line of each test case contains K space-separated integers B_{1}, B_{2}, ..., B_{K} denoting the satisfaction of Chefs.
The next Q lines contain a single integer q_{i} ( for i = 1 to Q ) , find the q_{i} th element in the set of sums .
------ Output ------
For each query of each test case, output a single line containing the answer to the query of the testcase
------ Constraints ------
Should contain all the constraints on the input data that you may have. Format it like:
$1 ≤ T ≤ 5$
$1 ≤ K ≤ 20000$
$1 ≤ Q ≤ 500$
$1 ≤ q_{i} ( for i = 1 to Q ) ≤ 10000$
$1 ≤ A_{i} ≤ 10^{18} ( for i = 1 to K ) $
$1 ≤ B_{i} ≤ 10^{18} ( for i = 1 to K ) $
------ Example ------
Input:
1
3 1
1 2 3
4 5 6
4
Output:
7
------ Explanation ------
Example case 1. There are 9 elements in the set of sums :
1 + 4 = 5
2 + 4 = 6
1 + 5 = 6
1 + 6 = 7
2 + 5 = 7
3 + 4 = 7
2 + 6 = 8
3 + 5 = 8
3 + 6 = 9
The fourth smallest element is 7.
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.
Pete and his mate Phil are out in the countryside shooting clay pigeons with a shotgun - amazing fun.
They decide to have a competition. 3 rounds, 2 shots each. Winner is the one with the most hits.
Some of the clays have something attached to create lots of smoke when hit, guarenteed by the packaging to generate 'real excitement!' (genuinely this happened). None of the explosive things actually worked, but for this kata lets say they did.
For each round you will receive the following format:
[{P1:'XX', P2:'XO'}, true]
That is an array containing an object and a boolean. Pl represents Pete, P2 represents Phil. X represents a hit and O represents a miss. If the boolean is true, any hit is worth 2. If it is false, any hit is worth 1.
Find out who won. If it's Pete, return 'Pete Wins!'. If it is Phil, return 'Phil Wins!'. If the scores are equal, return 'Draw!'.
Note that as there are three rounds, the actual input (x) will look something like this:
[[{P1:'XX', P2:'XO'}, true], [{P1:'OX', P2:'OO'}, false], [{P1:'XX', P2:'OX'}, true]]
Write your solution by modifying this code:
```python
def shoot(results):
```
Your solution should implemented in the function "shoot". The i
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 game of billiards involves two players knocking 3 balls around
on a green baize table. Well, there is more to it, but for our
purposes this is sufficient.
The game consists of several rounds and in each round both players
obtain a score, based on how well they played. Once all the rounds
have been played, the total score of each player is determined by
adding up the scores in all the rounds and the player with the higher
total score is declared the winner.
The Siruseri Sports Club organises an annual billiards game where
the top two players of Siruseri play against each other. The Manager
of Siruseri Sports Club decided to add his own twist to the game by
changing the rules for determining the winner. In his version, at the
end of each round, the cumulative score for each player is calculated, and the leader and her current lead are found. Once
all the rounds are over the player who had the maximum lead at the
end of any round in the game is declared the winner.
Consider the following score sheet for a game with 5 rounds:
RoundPlayer 1Player 2114082289134390110411210658890
The total scores of both players, the leader and the lead after
each round for this game is given below:RoundPlayer 1Player 2LeaderLead114082Player 1582229216Player 1133319326Player 274431432Player 215519522Player 23
Note that the above table contains the cumulative scores.
The winner of this game is Player 1 as he had the maximum lead (58
at the end of round 1) during the game.
Your task is to help the Manager find the winner and the winning
lead. You may assume that the scores will be such that there will
always be a single winner. That is, there are no ties.
Input
The first line of the input will contain a single integer N (N
≤ 10000) indicating the number of rounds in the game. Lines
2,3,...,N+1 describe the scores of the two players in the N rounds.
Line i+1 contains two integer Si and Ti, the scores of the Player 1
and 2 respectively, in round i. You may assume that 1 ≤ Si ≤
1000 and 1 ≤ Ti ≤ 1000.
Output
Your output must consist of a single line containing two integers
W and L, where W is 1 or 2 and indicates the winner and L is the
maximum lead attained by the winner.
Example
Input:
5
140 82
89 134
90 110
112 106
88 90
Output:
1 58
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.
Aka Beko, trying to escape from 40 bandits, got lost in the city of A. Aka Beko wants to go to City B, where the new hideout is, but the map has been stolen by a bandit.
Kobborg, one of the thieves, sympathized with Aka Beko and felt sorry for him. So I secretly told Aka Beko, "I want to help you go to City B, but I want to give you direct directions because I have to keep out of my friends, but I can't tell you. I can answer. "
When Kobborg receives the question "How about the route XX?" From Aka Beko, he answers Yes if it is the route from City A to City B, otherwise. Check the directions according to the map below.
<image>
Each city is connected by a one-way street, and each road has a number 0 or 1. Aka Beko specifies directions by a sequence of numbers. For example, 0100 is the route from City A to City B via City X, Z, and W. If you choose a route that is not on the map, you will get lost in the desert and never reach City B. Aka Beko doesn't know the name of the city in which she is, so she just needs to reach City B when she finishes following the directions.
Kobborg secretly hired you to create a program to answer questions from Aka Beko. If you enter a question from Aka Beko, write a program that outputs Yes if it is the route from City A to City B, otherwise it outputs No.
input
The input consists of multiple datasets. The end of the input is indicated by a single # (pound) line. Each dataset is given in the following format.
p
A sequence of numbers 0, 1 indicating directions is given on one line. p is a string that does not exceed 100 characters.
The number of datasets does not exceed 100.
output
Print Yes or No on one line for each dataset.
Example
Input
0100
0101
10100
01000
0101011
0011
011111
#
Output
Yes
No
Yes
No
Yes
No
Yes
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 positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integers n and k, you should count the number of ways to express n as a sum of k different primes. Here, two ways are considered to be the same if they sum up the same set of the primes. For example, 8 can be expressed as 3 + 5 and 5+ 3 but they are not distinguished.
When n and k are 24 and 3 respectively, the answer is two because there are two sets {2, 3, 19} and {2, 5, 17} whose sums are equal to 24. There are no other sets of three primes that sum up to 24. For n = 24 and k = 2, the answer is three, because there are three sets {5, 19}, {7,17} and {11, 13}. For n = 2 and k = 1, the answer is one, because there is only one set {2} whose sum is 2. For n = 1 and k = 1, the answer is zero. As 1 is not a prime, you shouldn't count {1}. For n = 4 and k = 2, the answer is zero, because there are no sets of two diffrent primes whose sums are 4.
Your job is to write a program that reports the number of such ways for the given n and k.
Input
The input is a sequence of datasets followed by a line containing two zeros separated by a space. A dataset is a line containing two positive integers n and k separated by a space. You may assume that n ≤ 1120 and k ≤ 14.
Output
The output should be composed of lines, each corresponding to an input dataset. An output line should contain one non-negative integer indicating the number of ways for n and k specified in the corresponding dataset. You may assume that it is less than 231.
Example
Input
24 3
24 2
2 1
1 1
4 2
18 3
17 1
17 3
17 4
100 5
1000 10
1120 14
0 0
Output
2
3
1
0
0
2
1
0
1
55
200102899
2079324314
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 walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.
The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Examples
Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1
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.
Your aged grandfather is tragically optimistic about Team GB's chances in the upcoming World Cup, and has enlisted you to help him make [Union Jack](https://en.wikipedia.org/wiki/Union_Jack) flags to decorate his house with.
## Instructions
* Write a function which takes as a parameter a number which represents the dimensions of the flag. The flags will always be square, so the number 9 means you should make a flag measuring 9x9.
* It should return a *string* of the flag, with _'X' for the red/white lines and '-' for the blue background_. It should include newline characters so that it's not all on one line.
* For the sake of symmetry, treat odd and even numbers differently: odd number flags should have a central cross that is *only one 'X' thick*. Even number flags should have a central cross that is *two 'X's thick* (see examples below).
## Other points
* The smallest flag you can make without it looking silly is 7x7. If you get a number smaller than 7, simply _make the flag 7x7_.
* If you get a decimal, round it _UP to the next whole number_, e.g. 12.45 would mean making a flag that is 13x13.
* If you get something that's not a number at all, *return false*.
Translations and comments (and upvotes) welcome!

## Examples:
```python
union_jack(9) # (9 is odd, so the central cross is 1'X' thick)
"X---X---X
-X--X--X-
--X-X-X--
---XXX---
XXXXXXXXX
---XXX---
--X-X-X--
-X--X--X-
X---X---X"
union_jack(10) # (10 is even, so the central cross is 2 'X's thick)
'X---XX---X
-X--XX--X-
--X-XX-X--
---XXXX---
XXXXXXXXXX
XXXXXXXXXX
---XXXX---
--X-XX-X--
-X--XX--X-
X---XX---X
union_jack(1) # (the smallest possible flag is 7x7)
"X--X--X
-X-X-X-
--XXX--
XXXXXXX
--XXX--
-X-X-X-
X--X--X"
union_jack('string') #return false.
```
Write your solution by modifying this code:
```python
def union_jack(n):
```
Your solution should implemented in the function "union_jack". The i
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 circle $A$ with radius $r_a$ and with central coordinate $(x_a, y_a)$ and circle $B$ with radius $r_b$ and with central coordinate $(x_b, y_b)$.
Write a program which prints:
* "2" if $B$ is in $A$,
* "-2" if $A$ is in $B$,
* "1" if circumference of $A$ and $B$ intersect, and
* "0" if $A$ and $B$ do not overlap.
You may assume that $A$ and $B$ are not identical.
Input
The input consists of multiple datasets. The first line consists of an integer $N$ ($N \leq 50$), the number of datasets. There will be $N$ lines where each line represents each dataset. Each data set consists of real numbers:
$x_a$ $y_a$ $r_a$ $x_b$ $y_b$ $r_b$
Output
For each dataset, print 2, -2, 1, or 0 in a line.
Example
Input
2
0.0 0.0 5.0 0.0 0.0 4.0
0.0 0.0 2.0 4.1 0.0 2.0
Output
2
0
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.
User ainta has a stack of n red and blue balls. He can apply a certain operation which changes the colors of the balls inside the stack.
While the top ball inside the stack is red, pop the ball from the top of the stack. Then replace the blue ball on the top with a red ball. And finally push some blue balls to the stack until the stack has total of n balls inside.
If there are no blue balls inside the stack, ainta can't apply this operation. Given the initial state of the stack, ainta wants to know the maximum number of operations he can repeatedly apply.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 50) — the number of balls inside the stack.
The second line contains a string s (|s| = n) describing the initial state of the stack. The i-th character of the string s denotes the color of the i-th ball (we'll number the balls from top to bottom of the stack). If the character is "R", the color is red. If the character is "B", the color is blue.
-----Output-----
Print the maximum number of operations ainta can repeatedly apply.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
-----Examples-----
Input
3
RBR
Output
2
Input
4
RBBR
Output
6
Input
5
RBBRR
Output
6
-----Note-----
The first example is depicted below.
The explanation how user ainta applies the first operation. He pops out one red ball, changes the color of the ball in the middle from blue to red, and pushes one blue ball.
[Image]
The explanation how user ainta applies the second operation. He will not pop out red balls, he simply changes the color of the ball on the top from blue to red.
[Image]
From now on, ainta can't apply any operation because there are no blue balls inside the stack. ainta applied two operations, so the answer is 2.
The second example is depicted below. The blue arrow denotes a single operation.
[Image]
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 trick of fate caused Hatsumi and Taku to come to know each other. To keep the encounter in memory, they decided to calculate the difference between their ages. But the difference in ages varies depending on the day it is calculated. While trying again and again, they came to notice that the difference of their ages will hit a maximum value even though the months move on forever.
Given the birthdays for the two, make a program to report the maximum difference between their ages. The age increases by one at the moment the birthday begins. If the birthday coincides with the 29th of February in a leap year, the age increases at the moment the 1st of March arrives in non-leap years.
Input
The input is given in the following format.
y_1 m_1 d_1
y_2 m_2 d_2
The first and second lines provide Hatsumi’s and Taku’s birthdays respectively in year y_i (1 ≤ y_i ≤ 3000), month m_i (1 ≤ m_i ≤ 12), and day d_i (1 ≤ d_i ≤ Dmax) format. Where Dmax is given as follows:
* 28 when February in a non-leap year
* 29 when February in a leap-year
* 30 in April, June, September, and November
* 31 otherwise.
It is a leap year if the year represented as a four-digit number is divisible by 4. Note, however, that it is a non-leap year if divisible by 100, and a leap year if divisible by 400.
Output
Output the maximum difference between their ages.
Examples
Input
1999 9 9
2001 11 3
Output
3
Input
2008 2 29
2015 3 1
Output
8
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.
Better things, cheaper. There is a fierce battle at the time sale held in some supermarkets today. "LL-do" here in Aizu is one such supermarket, and we are holding a slightly unusual time sale to compete with other chain stores. In a general time sale, multiple products are cheaper at the same time, but at LL-do, the time to start the time sale is set differently depending on the target product.
The Sakai family is one of the households that often use LL-do. Under the leadership of his wife, the Sakai family will hold a strategy meeting for the time sale to be held next Sunday, and analyze in what order the shopping will maximize the discount. .. The Sakai family, who are familiar with the inside of the store, drew a floor plan of the sales floor and succeeded in grasping where the desired items are, how much the discount is, and the time until they are sold out. The Sakai family was perfect so far, but no one could analyze it. So you got to know the Sakai family and decided to write a program. The simulation is performed assuming that one person moves.
There are 10 types of products, which are distinguished by the single-digit product number g. The time sale information includes the item number g, the discount amount d, the start time s of the time sale, and the sold-out time e.
The inside of the store is represented by a two-dimensional grid consisting of squares X in width and Y in height, and either a passage or a product shelf is assigned to each square. There is one type of product on a shelf, which is distinguished by item number g. You can buy any item, but you must not buy more than one item with the same item number. From the product shelves, you can pick up products in any direction as long as they are in the aisle squares that are adjacent to each other.
You can pick up items from the time when the time sale starts, but you cannot pick up items from the time when they are sold out. Also, the time starts from 0 when you enter the store.
You can move from the current square to the adjacent aisle squares on the top, bottom, left, and right, but you cannot move to the squares on the product shelves. You can't even go outside the store represented by the grid. One unit time elapses for each move. Also, do not consider the time to pick up the product.
Please create a program that outputs the maximum value of the total discount amount of the products that you could get by inputting the floor plan of the store, the initial position of the shopper and the time sale information of the product.
input
Given multiple datasets. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
X Y
m1,1 m2,1 ... mX,1
m1,2 m2,2 ... mX,2
::
m1, Y m2, Y ... mX, Y
n
g1 d1 s1 e1
g2 d2 s2 e2
::
gn dn sn en
The first line gives the store sizes X, Y (3 ≤ X, Y ≤ 20). In the following Y row, the store information mi, j in the i-th column and j-th row is given with the following contents.
. (Period): Aisle square
Number: Product number
P: The initial position of the shopper
The following line is given the number of timesale information n (1 ≤ n ≤ 8). The next n lines are given the i-th time sale information gi di si ei (0 ≤ gi ≤ 9, 1 ≤ di ≤ 10000, 0 ≤ si, ei ≤ 100).
The number of datasets does not exceed 50.
output
For each data set, the maximum value of the total discount amount of the products that can be taken is output on one line.
Example
Input
6 5
1 1 . 0 0 4
1 . . . . .
. . 2 2 . .
. . 2 2 3 3
P . . . . .
5
0 50 5 10
1 20 0 10
2 10 5 15
3 150 3 5
4 100 8 9
0 0
Output
180
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.