Prompt:
def expectation_steps(n: int, edges: List[Tuple[int, int]]) -> float:
    """
    Momiji has got a rooted tree, consisting of n nodes. The tree nodes are numbered by integers from 1 to n. The root has number 1. Momiji decided to play a game on this tree.

    The game consists of several steps. On each step, Momiji chooses one of the remaining tree nodes (let's denote it by v) and removes all the subtree nodes with the root in node v from the tree. Node v gets deleted as well. The game finishes when the tree has no nodes left. In other words, the game finishes after the step that chooses the node number 1.

    Each time Momiji chooses a new node uniformly among all the remaining nodes. Your task is to find the expectation of the number of steps in the described game.

    -----Input-----

    The first line contains integer n (1 ≤ n ≤ 10^5) — the number of nodes in the tree. The next n - 1 lines contain the tree edges. The i-th line contains integers a_{i}, b_{i} (1 ≤ a_{i}, b_{i} ≤ n; a_{i} ≠ b_{i}) — the numbers of the nodes that are connected by the i-th edge.

    It is guaranteed that the given graph is a tree.

    -----Output-----

    Print a single real number — the expectation of the number of steps in the described game.

    The answer will be considered correct if the absolute or relative error doesn't exceed 10^{ - 6}.

    -----Examples-----
    Input
    2
    1 2

    Output
    1.50000000000000000000

    Input
    3
    1 2
    1 3

    Output
    2.00000000000000000000

    -----Note-----

    In the first sample, there are two cases. One is directly remove the root and another is remove the root after one step. Thus the expected steps are: 1 × (1 / 2) + 2 × (1 / 2) = 1.5

    In the second sample, things get more complex. There are two cases that reduce to the first sample, and one case cleaned at once. Thus the expected steps are: 1 × (1 / 3) + (1 + 1.5) × (2 / 3) = (1 / 3) + (5 / 3) = 2
    """


Prompt:
def find_best_carrot(n: int, a: List[int]) -> List[int]:
    """
    Oleg the bank client and Igor the analyst are arguing again. This time, they want to pick a gift as a present for their friend, ZS the coder. After a long thought, they decided that their friend loves to eat carrots the most and thus they want to pick the best carrot as their present.

    There are n carrots arranged in a line. The i-th carrot from the left has juiciness a_{i}. Oleg thinks ZS loves juicy carrots whereas Igor thinks that he hates juicy carrots. Thus, Oleg would like to maximize the juiciness of the carrot they choose while Igor would like to minimize the juiciness of the carrot they choose.

    To settle this issue, they decided to play a game again. Oleg and Igor take turns to play the game. In each turn, a player can choose a carrot from either end of the line, and eat it. The game ends when only one carrot remains. Oleg moves first. The last remaining carrot will be the carrot that they will give their friend, ZS.

    Oleg is a sneaky bank client. When Igor goes to a restroom, he performs k moves before the start of the game. Each move is the same as above (eat a carrot from either end of the line). After Igor returns, they start the game with Oleg still going first.

    Oleg wonders: for each k such that 0 ≤ k ≤ n - 1, what is the juiciness of the carrot they will give to ZS if he makes k extra moves beforehand and both players play optimally?

    -----Input-----

    The first line of input contains a single integer n (1 ≤ n ≤ 3·10^5) — the total number of carrots.

    The next line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). Here a_{i} denotes the juiciness of the i-th carrot from the left of the line.

    -----Output-----

    Output n space-separated integers x_0, x_1, ..., x_{n} - 1. Here, x_{i} denotes the juiciness of the carrot the friends will present to ZS if k = i.

    -----Examples-----
    Input
    4
    1 2 3 5

    Output
    3 3 5 5

    Input
    5
    1000000000 1000000000 1000000000 1000000000 1

    Output
    1000000000 1000000000 1000000000 1000000000 1000000000

    -----Note-----

    For the first example,

    When k = 0, one possible optimal game is as follows: Oleg eats the carrot with juiciness 1. Igor eats the carrot with juiciness 5. Oleg eats the carrot with juiciness 2. The remaining carrot has juiciness 3.

    When k = 1, one possible optimal play is as follows: Oleg eats the carrot with juiciness 1 beforehand. Oleg eats the carrot with juiciness 2. Igor eats the carrot with juiciness 5. The remaining carrot has juiciness 3.

    When k = 2, one possible optimal play is as follows: Oleg eats the carrot with juiciness 1 beforehand. Oleg eats the carrot with juiciness 2 beforehand. Oleg eats the carrot with juiciness 3. The remaining carrot has juiciness 5.

    When k = 3, one possible optimal play is as follows: Oleg eats the carrot with juiciness 1 beforehand. Oleg eats the carrot with juiciness 2 beforehand. Oleg eats the carrot with juiciness 3 beforehand. The remaining carrot has juiciness 5.

    Thus, the answer is 3, 3, 5, 5.

    For the second sample, Oleg can always eat the carrot with juiciness 1 since he always moves first. So, the remaining carrot will always have juiciness 1000000000.
    """


Prompt:
def calculate_shortest_paths_sum(n: int, matrix: List[List[int]], x: List[int]) -> List[int]:
    """
    Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game:  The game consists of n steps.  On the i-th step Greg removes vertex number x_{i} from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex.  Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex x_{i}, then Greg wants to know the value of the following sum: $\sum_{v, u, v \neq u} d(i, v, u)$.

    Help Greg, print the value of the required sum before each step.

    -----Input-----

    The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph.

    Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line a_{ij} (1 ≤ a_{ij} ≤ 10^5, a_{ii} = 0) represents the weight of the edge that goes from vertex i to vertex j.

    The next line contains n distinct integers: x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ n) — the vertices that Greg deletes.

    -----Output-----

    Print n integers — the i-th number equals the required sum before the i-th step.

    Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.

    -----Examples-----
    Input
    1
    0
    1

    Output
    0
    Input
    2
    0 5
    4 0
    1 2

    Output
    9 0
    Input
    4
    0 3 1 1
    6 0 400 1
    2 4 0 1
    1 1 1 0
    4 1 2 3

    Output
    17 23 404 0
    """

assert calculate_shortest_paths_sum(1, [[0]], [1]) == [0]


Prompt:
def find_max_interest_value(n: int, T: int, tasks: List[Tuple[int, int]]) -> int:
    """
    Polycarp is making a quest for his friends. He has already made n tasks, for each task the boy evaluated how interesting it is as an integer q_{i}, and the time t_{i} in minutes needed to complete the task.

    An interesting feature of his quest is: each participant should get the task that is best suited for him, depending on his preferences. The task is chosen based on an interactive quiz that consists of some questions. The player should answer these questions with "yes" or "no". Depending on the answer to the question, the participant either moves to another question or goes to one of the tasks that are in the quest. In other words, the quest is a binary tree, its nodes contain questions and its leaves contain tasks.

    We know that answering any of the questions that are asked before getting a task takes exactly one minute from the quest player. Polycarp knows that his friends are busy people and they can't participate in the quest for more than T minutes. Polycarp wants to choose some of the n tasks he made, invent the corresponding set of questions for them and use them to form an interactive quiz as a binary tree so that no matter how the player answers quiz questions, he spends at most T minutes on completing the whole quest (that is, answering all the questions and completing the task). Specifically, the quest can contain zero questions and go straight to the task. Each task can only be used once (i.e., the people who give different answers to questions should get different tasks).

    Polycarp wants the total "interest" value of the tasks involved in the quest to be as large as possible. Help him determine the maximum possible total interest value of the task considering that the quest should be completed in T minutes at any variant of answering questions.

    -----Input-----

    The first line contains two integers n and T (1 ≤ n ≤ 1000, 1 ≤ T ≤ 100) — the number of tasks made by Polycarp and the maximum time a quest player should fit into.

    Next n lines contain two integers t_{i}, q_{i} (1 ≤ t_{i} ≤ T, 1 ≤ q_{i} ≤ 1000) each — the time in minutes needed to complete the i-th task and its interest value.

    -----Output-----

    Print a single integer — the maximum possible total interest value of all the tasks in the quest.

    -----Examples-----
    Input
    5 5
    1 1
    1 1
    2 2
    3 3
    4 4

    Output
    11

    Input
    5 5
    4 1
    4 2
    4 3
    4 4
    4 5

    Output
    9

    Input
    2 2
    1 1
    2 10

    Output
    10

    -----Note-----

    In the first sample test all the five tasks can be complemented with four questions and joined into one quest.

    In the second sample test it is impossible to use all the five tasks, but you can take two of them, the most interesting ones.

    In the third sample test the optimal strategy is to include only the second task into the quest.

    Here is the picture that illustrates the answers to the sample tests. The blue circles represent the questions, the two arrows that go from every circle represent where a person goes depending on his answer to that question. The tasks are the red ovals. [Image]
    """


Prompt:
def calculate_max_happiness(n: int, k: int, roads: List[Tuple[int, int]]) -> int:
    """
    Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.

    [Image]

    There are $n$ cities and $n-1$ two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from $1$ to $n$, and the city $1$ is the capital of the kingdom. So, the kingdom has a tree structure.

    As the queen, Linova plans to choose exactly $k$ cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.

    A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).

    Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.

    In order to be a queen loved by people, Linova wants to choose $k$ cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?

    -----Input-----

    The first line contains two integers $n$ and $k$ ($2\le n\le 2 \cdot 10^5$, $1\le k< n$)  — the number of cities and industry cities respectively.

    Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1\le u,v\le n$), denoting there is a road connecting city $u$ and city $v$.

    It is guaranteed that from any city, you can reach any other city by the roads.

    -----Output-----

    Print the only line containing a single integer  — the maximum possible sum of happinesses of all envoys.

    -----Examples-----
    Input
    7 4
    1 2
    1 3
    1 4
    3 5
    3 6
    4 7

    Output
    7
    Input
    4 1
    1 2
    1 3
    2 4

    Output
    2
    Input
    8 5
    7 5
    1 7
    6 1
    3 7
    8 3
    2 1
    4 5

    Output
    9

    -----Note-----

    [Image]

    In the first example, Linova can choose cities $2$, $5$, $6$, $7$ to develop industry, then the happiness of the envoy from city $2$ is $1$, the happiness of envoys from cities $5$, $6$, $7$ is $2$. The sum of happinesses is $7$, and it can be proved to be the maximum one.

    [Image]

    In the second example, choosing cities $3$, $4$ developing industry can reach a sum of $3$, but remember that Linova plans to choose exactly $k$ cities developing industry, then the maximum sum is $2$.
    """


Prompt:
def find_suitable_array(n: int, m: int, operations: List[Tuple[int, int, int, int]]) -> Tuple[str, List[int]]:
    """
    Levko loves array a_1, a_2, ... , a_{n}, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types:

    Increase all elements from l_{i} to r_{i} by d_{i}. In other words, perform assignments a_{j} = a_{j} + d_{i} for all j that meet the inequation l_{i} ≤ j ≤ r_{i}.  Find the maximum of elements from l_{i} to r_{i}. That is, calculate the value $m_{i} = \operatorname{max}_{j = l_{i}}^{r_{i}} a_{j}$.

    Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 10^9 in their absolute value, so he asks you to find such an array.

    -----Input-----

    The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly.

    Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer t_{i} (1 ≤ t_{i} ≤ 2) that describes the operation type. If t_{i} = 1, then it is followed by three integers l_{i}, r_{i} and d_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n,  - 10^4 ≤ d_{i} ≤ 10^4) — the description of the operation of the first type. If t_{i} = 2, then it is followed by three integers l_{i}, r_{i} and m_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n,  - 5·10^7 ≤ m_{i} ≤ 5·10^7) — the description of the operation of the second type.

    The operations are given in the order Levko performed them on his array.

    -----Output-----

    In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise.

    If the solution exists, then on the second line print n integers a_1, a_2, ... , a_{n} (|a_{i}| ≤ 10^9) — the recovered array.

    -----Examples-----
    Input
    4 5
    1 2 3 1
    2 1 2 8
    2 3 4 7
    1 1 3 3
    2 3 4 8

    Output
    YES
    4 7 4 7
    Input
    4 5
    1 2 3 1
    2 1 2 8
    2 3 4 7
    1 1 3 3
    2 3 4 13

    Output
    NO
    """


Prompt:
def calculate_inversions(n: int, m: int, permutation: List[int], operations: List[Tuple[int, int]]) -> float:
    """
    The Little Elephant loves permutations of integers from 1 to n very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1, 2, 3, ..., n.

    This time the Little Elephant has permutation p_1, p_2, ..., p_{n}. Its sorting program needs to make exactly m moves, during the i-th move it swaps elements that are at that moment located at the a_{i}-th and the b_{i}-th positions. But the Little Elephant's sorting program happened to break down and now on every step it can equiprobably either do nothing or swap the required elements.

    Now the Little Elephant doesn't even hope that the program will sort the permutation, but he still wonders: if he runs the program and gets some permutation, how much will the result of sorting resemble the sorted one? For that help the Little Elephant find the mathematical expectation of the number of permutation inversions after all moves of the program are completed.

    We'll call a pair of integers i, j (1 ≤ i < j ≤ n) an inversion in permutatuon p_1, p_2, ..., p_{n}, if the following inequality holds: p_{i} > p_{j}.

    -----Input-----

    The first line contains two integers n and m (1 ≤ n, m ≤ 1000, n > 1) — the permutation size and the number of moves. The second line contains n distinct integers, not exceeding n — the initial permutation. Next m lines each contain two integers: the i-th line contains integers a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ n, a_{i} ≠ b_{i}) — the positions of elements that were changed during the i-th move.

    -----Output-----

    In the only line print a single real number — the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed 10^{ - 6}.

    -----Examples-----
    Input
    2 1
    1 2
    1 2

    Output
    0.500000000

    Input
    4 3
    1 3 2 4
    1 2
    2 3
    1 4

    Output
    3.000000000
    """


Prompt:
def find_unobtainable_residues(N: int, M: int, A: List[int]) -> Tuple[int, List[int]]:
    """
    Everybody seems to think that the Martians are green, but it turns out they are metallic pink and fat. Ajs has two bags of distinct nonnegative integers. The bags are disjoint, and the union of the sets of numbers in the bags is $\{0,1,…,M-1\}$, for some positive integer $M$. Ajs draws a number from the first bag and a number from the second bag, and then sums them modulo $M$.

    What are the residues modulo $M$ that Ajs cannot obtain with this action?

    -----Input-----

    The first line contains two positive integer $N$ ($1 \leq N \leq 200\,000$) and $M$ ($N+1 \leq M \leq 10^{9}$), denoting the number of the elements in the first bag and the modulus, respectively.

    The second line contains $N$ nonnegative integers $a_1,a_2,\ldots,a_N$ ($0 \leq a_1<a_2< \ldots< a_N<M$), the contents of the first bag.

    -----Output-----

    In the first line, output the cardinality $K$ of the set of residues modulo $M$ which Ajs cannot obtain.

    In the second line of the output, print $K$ space-separated integers greater or equal than zero and less than $M$, which represent the residues Ajs cannot obtain. The outputs should be sorted in increasing order of magnitude. If $K$=0, do not output the second line.

    -----Examples-----
    Input
    2 5
    3 4

    Output
    1
    2

    Input
    4 1000000000
    5 25 125 625

    Output
    0

    Input
    2 4
    1 3

    Output
    2
    0 2

    -----Note-----

    In the first sample, the first bag and the second bag contain $\{3,4\}$ and $\{0,1,2\}$, respectively. Ajs can obtain every residue modulo $5$ except the residue $2$: $ 4+1 \equiv 0, \, 4+2 \equiv 1, \, 3+0 \equiv 3, \, 3+1 \equiv 4 $ modulo $5$. One can check that there is no choice of elements from the first and the second bag which sum to $2$ modulo $5$.

    In the second sample, the contents of the first bag are $\{5,25,125,625\}$, while the second bag contains all other nonnegative integers with at most $9$ decimal digits. Every residue modulo $1\,000\,000\,000$ can be obtained as a sum of an element in the first bag and an element in the second bag.
    """

assert find_unobtainable_residues(2, 5, [3, 4]) == (1, [2])

assert find_unobtainable_residues(2, 4, [1, 3]) == (2, [0, 2])


Prompt:
def count_subsequences(N: int, M: int, A: List[int], B: List[int]) -> int:
    """
    You are given an array A, consisting of N integers and an array B, consisting of M integers.
    The subsequence of A is the array that can be obtained by picking the elements at the arbitrary sorted set of positions from A.
    Your task is to count the number of such subsequences C of A that:

    - C contains exactly M elements.
    - The array (C+B) is non-decreasing. Here by + operation, we mean element-wise sum.

    For example, the array (4, 8, 5) plus the array (10, 20, 30) is (14, 28, 35).

    Formally, (C+B) is an array of size M such that (C+B)i = Ci + Bi.

    In case some subsequence appears more that once, you should counts it as many times as it appears.

    Formally, two subarrays of an array a, (ai_1, ai_2, ... ,ai_n) and (aj_1, aj_2, ... ,aj_m) will be considered different if either their lengths are different i.e. n != m or there exists an index k such that such that i_k  != j_k.

    Since the answer can be very large, we ask you to calculate it, modulo 109+7.

    -----Input-----
    The first line of input contains a pair of space separated integers N and M, denoting the number of elements in the array A and the number of elements in the array B.
    The second line contains N space-separated integers Ai, denoting the array A.
    The third line contains M space-separated integers Bj, denoting the array B.

    -----Output-----
    Output a single line containing the number of subsequences C as asked in the problem, modulo 109+7.

    -----Constraints-----
    - 1 ≤ Ai, Bi ≤ 109
    - 1 ≤ M ≤ N

    -----Subtasks-----
    - Subtask #1 (33 points): 1 ≤ N ≤ 50, 1 ≤ M ≤ 5
    - Subtask #2 (33 points): 1 ≤ N ≤ 500, 1 ≤ M ≤ 50
    - Subtask #3 (34 points): 1 ≤ N ≤ 2000, 1 ≤ M ≤ 1000

    -----Example-----
    Input #1:
    5 3
    1 5 2 4 7
    7 9 6

    Output #1:
    4

    Input #2:
    4 2
    7 7 7 7
    3 4

    Output #2:
    6

    -----Explanation-----
    Example case 1. The suitable subsequences are (1, 2, 7), (1, 4, 7), (5, 4, 7), (2, 4, 7).
    Example case 2. The suitable subsequence is (7, 7), and it appears 6 times:

    - at indices (1, 2)
    - at indices (1, 3)
    - at indices (1, 4)
    - at indices (2, 3)
    - at indices (2, 4)
    - at indices (3, 4)

    So, the answer is 6.
    """

def check(candidate):
    assert candidate(5, 3, [1, 5, 2, 4, 7], [7, 9, 6]) == 4
    assert candidate(4, 2, [7, 7, 7, 7], [3, 4]) == 6
check(count_subsequences)


Prompt:
def find_palindrome(A: str) -> str:
    """
    You are given a string A. Find a string B, where B is a palindrome and A is a subsequence of B.

    A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the order of the remaining characters. For example, "cotst" is a subsequence of "contest".

    A palindrome is a string that reads the same forward or backward.

    The length of string B should be at most 10^4. It is guaranteed that there always exists such string.

    You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 10^4.

    -----Input-----

    First line contains a string A (1 ≤ |A| ≤ 10^3) consisting of lowercase Latin letters, where |A| is a length of A.

    -----Output-----

    Output single line containing B consisting of only lowercase Latin letters. You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 10^4. If there are many possible B, print any of them.

    -----Examples-----
    Input
    aba

    Output
    aba
    Input
    ab

    Output
    aabaa

    -----Note-----

    In the first example, "aba" is a subsequence of "aba" which is a palindrome.

    In the second example, "ab" is a subsequence of "aabaa" which is a palindrome.
    """

def check(find_palindrome):
    assert find_palindrome('aba') == 'abaaba'
    assert find_palindrome('ab') == 'abba'
    assert find_palindrome('abca') == 'abcacba'
    assert find_palindrome('racecar') == 'racecaracecar'
    assert find_palindrome('a') == 'aa'
check(find_palindrome)

assert find_palindrome('aba') == 'aba'
assert find_palindrome('ab') == 'aabaa'


Prompt:
def max_possible_f(n: int, edges: List[Tuple[int, int, int]], use_count: List[int]) -> int:
    """
    DZY loves planting, and he enjoys solving tree problems.

    DZY has a weighted tree (connected undirected graph without cycles) containing n nodes (they are numbered from 1 to n). He defines the function g(x, y) (1 ≤ x, y ≤ n) as the longest edge in the shortest path between nodes x and y. Specially g(z, z) = 0 for every z.

    For every integer sequence p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n), DZY defines f(p) as $\operatorname{min}_{i = 1}^{n} g(i, p_{i})$.

    DZY wants to find such a sequence p that f(p) has maximum possible value. But there is one more restriction: the element j can appear in p at most x_{j} times.

    Please, find the maximum possible f(p) under the described restrictions.

    -----Input-----

    The first line contains an integer n (1 ≤ n ≤ 3000).

    Each of the next n - 1 lines contains three integers a_{i}, b_{i}, c_{i} (1 ≤ a_{i}, b_{i} ≤ n; 1 ≤ c_{i} ≤ 10000), denoting an edge between a_{i} and b_{i} with length c_{i}. It is guaranteed that these edges form a tree.

    Each of the next n lines describes an element of sequence x. The j-th line contains an integer x_{j} (1 ≤ x_{j} ≤ n).

    -----Output-----

    Print a single integer representing the answer.

    -----Examples-----
    Input
    4
    1 2 1
    2 3 2
    3 4 3
    1
    1
    1
    1

    Output
    2

    Input
    4
    1 2 1
    2 3 2
    3 4 3
    4
    4
    4
    4

    Output
    3

    -----Note-----

    In the first sample, one of the optimal p is [4, 3, 2, 1].
    """


Prompt:
def xor_pairwise_sums(n: int, arr: List[int]) -> int:
    """
    Catherine received an array of integers as a gift for March 8. Eventually she grew bored with it, and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one — xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute

    $$ (a_1 + a_2) \oplus (a_1 + a_3) \oplus \ldots \oplus (a_1 + a_n) \\ \oplus (a_2 + a_3) \oplus \ldots \oplus (a_2 + a_n) \\ \ldots \\ \oplus (a_{n-1} + a_n) \\ $$

    Here $x \oplus y$ is a bitwise XOR operation (i.e. $x$ ^ $y$ in many modern programming languages). You can read about it in Wikipedia: https://en.wikipedia.org/wiki/Exclusive_or#Bitwise_operation.

    -----Input-----

    The first line contains a single integer $n$ ($2 \leq n \leq 400\,000$) — the number of integers in the array.

    The second line contains integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^7$).

    -----Output-----

    Print a single integer — xor of all pairwise sums of integers in the given array.

    -----Examples-----
    Input
    2
    1 2

    Output
    3
    Input
    3
    1 2 3

    Output
    2

    -----Note-----

    In the first sample case there is only one sum $1 + 2 = 3$.

    In the second sample case there are three sums: $1 + 2 = 3$, $1 + 3 = 4$, $2 + 3 = 5$. In binary they are represented as $011_2 \oplus 100_2 \oplus 101_2 = 010_2$, thus the answer is 2.

    $\oplus$ is the bitwise xor operation. To define $x \oplus y$, consider binary representations of integers $x$ and $y$. We put the $i$-th bit of the result to be 1 when exactly one of the $i$-th bits of $x$ and $y$ is 1. Otherwise, the $i$-th bit of the result is put to be 0. For example, $0101_2 \, \oplus \, 0011_2 = 0110_2$.
    """

def check(xor_pairwise_sums):
    assert xor_pairwise_sums(2, [1, 2]) == 3
    assert xor_pairwise_sums(3, [1, 2, 3]) == 2
    assert xor_pairwise_sums(2, [1, 1]) == 2
    assert xor_pairwise_sums(100, list(range(100))) == 102
    assert xor_pairwise_sums(50, [i for i in range(50)]) == 3
    assert xor_pairwise_sums(100, [i * 5 for i in range(100)]) == 148
    assert xor_pairwise_sums(3, [2, 2, 8]) == 4
check(xor_pairwise_sums)


Prompt:
def game_of_stones(n: int, stones: List[int]) -> str:
    """
    Sam has been teaching Jon the Game of Stones to sharpen his mind and help him devise a strategy to fight the white walkers. The rules of this game are quite simple:  The game starts with n piles of stones indexed from 1 to n. The i-th pile contains s_{i} stones. The players make their moves alternatively. A move is considered as removal of some number of stones from a pile. Removal of 0 stones does not count as a move. The player who is unable to make a move loses.

    Now Jon believes that he is ready for battle, but Sam does not think so. To prove his argument, Sam suggested that they play a modified version of the game.

    In this modified version, no move can be made more than once on a pile. For example, if 4 stones are removed from a pile, 4 stones cannot be removed from that pile again.

    Sam sets up the game and makes the first move. Jon believes that Sam is just trying to prevent him from going to battle. Jon wants to know if he can win if both play optimally.

    -----Input-----

    First line consists of a single integer n (1 ≤ n ≤ 10^6) — the number of piles.

    Each of next n lines contains an integer s_{i} (1 ≤ s_{i} ≤ 60) — the number of stones in i-th pile.

    -----Output-----

    Print a single line containing "YES" (without quotes) if Jon wins, otherwise print "NO" (without quotes)

    -----Examples-----
    Input
    1
    5

    Output
    NO
    Input
    2
    1
    2

    Output
    YES

    -----Note-----

    In the first case, Sam removes all the stones and Jon loses.

    In second case, the following moves are possible by Sam: $\{1,2 \} \rightarrow \{0,2 \}, \{1,2 \} \rightarrow \{1,0 \}, \{1,2 \} \rightarrow \{1,1 \}$

    In each of these cases, last move can be made by Jon to win the game as follows: $\{0,2 \} \rightarrow \{0,0 \}, \{1,0 \} \rightarrow \{0,0 \}, \{1,1 \} \rightarrow \{0,1 \}$
    """

assert game_of_stones(2, [1, 2]) == 'YES'


Prompt:
def max_perimeter(n: int, points: List[List[int]]) -> str:
    """
    You are given $n$ points on the plane. The polygon formed from all the $n$ points is strictly convex, that is, the polygon is convex, and there are no three collinear points (i.e. lying in the same straight line). The points are numbered from $1$ to $n$, in clockwise order.

    We define the distance between two points $p_1 = (x_1, y_1)$ and $p_2 = (x_2, y_2)$ as their Manhattan distance: $$d(p_1, p_2) = |x_1 - x_2| + |y_1 - y_2|.$$

    Furthermore, we define the perimeter of a polygon, as the sum of Manhattan distances between all adjacent pairs of points on it; if the points on the polygon are ordered as $p_1, p_2, \ldots, p_k$ $(k \geq 3)$, then the perimeter of the polygon is $d(p_1, p_2) + d(p_2, p_3) + \ldots + d(p_k, p_1)$.

    For some parameter $k$, let's consider all the polygons that can be formed from the given set of points, having any $k$ vertices, such that the polygon is not self-intersecting. For each such polygon, let's consider its perimeter. Over all such perimeters, we define $f(k)$ to be the maximal perimeter.

    Please note, when checking whether a polygon is self-intersecting, that the edges of a polygon are still drawn as straight lines. For instance, in the following pictures:

    [Image]

    In the middle polygon, the order of points ($p_1, p_3, p_2, p_4$) is not valid, since it is a self-intersecting polygon. The right polygon (whose edges resemble the Manhattan distance) has the same order and is not self-intersecting, but we consider edges as straight lines. The correct way to draw this polygon is ($p_1, p_2, p_3, p_4$), which is the left polygon.

    Your task is to compute $f(3), f(4), \ldots, f(n)$. In other words, find the maximum possible perimeter for each possible number of points (i.e. $3$ to $n$).

    -----Input-----

    The first line contains a single integer $n$ ($3 \leq n \leq 3\cdot 10^5$) — the number of points.

    Each of the next $n$ lines contains two integers $x_i$ and $y_i$ ($-10^8 \leq x_i, y_i \leq 10^8$) — the coordinates of point $p_i$.

    The set of points is guaranteed to be convex, all points are distinct, the points are ordered in clockwise order, and there will be no three collinear points.

    -----Output-----

    For each $i$ ($3\leq i\leq n$), output $f(i)$.

    -----Examples-----
    Input
    4
    2 4
    4 3
    3 0
    1 3

    Output
    12 14
    Input
    3
    0 0
    0 2
    2 0

    Output
    8

    -----Note-----

    In the first example, for $f(3)$, we consider four possible polygons:   ($p_1, p_2, p_3$), with perimeter $12$.  ($p_1, p_2, p_4$), with perimeter $8$.  ($p_1, p_3, p_4$), with perimeter $12$.  ($p_2, p_3, p_4$), with perimeter $12$.

    For $f(4)$, there is only one option, taking all the given points. Its perimeter $14$.

    In the second example, there is only one possible polygon. Its perimeter is $8$.
    """


Prompt:
def process_paper_operations(n: int, q: int, queries: List[str]) -> List[int]:
    """
    Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types:  Fold the sheet of paper at position p_{i}. After this query the leftmost part of the paper with dimensions 1 × p_{i} must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - p_{i}).  Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance l_{i} from the left border of the current sheet of paper and the other at distance r_{i} from the left border of the current sheet of paper.

    Please look at the explanation of the first test example for better understanding of the problem.

    -----Input-----

    The first line contains two integers: n and q (1  ≤ n ≤ 10^5; 1 ≤ q ≤ 10^5) — the width of the paper and the number of queries.

    Each of the following q lines contains one of the described queries in the following format:  "1 p_{i}" (1 ≤ p_{i} < [current width of sheet]) — the first type query.  "2 l_{i} r_{i}" (0 ≤ l_{i} < r_{i} ≤ [current width of sheet]) — the second type query.

    -----Output-----

    For each query of the second type, output the answer.

    -----Examples-----
    Input
    7 4
    1 3
    1 2
    2 0 1
    2 1 2

    Output
    4
    3

    Input
    10 9
    2 2 9
    1 1
    2 0 1
    1 8
    2 0 8
    1 2
    2 1 3
    1 4
    2 2 4

    Output
    7
    2
    10
    4
    5

    -----Note-----

    The pictures below show the shapes of the paper during the queries of the first example: [Image]

    After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.
    """


Prompt:
def find_min_travel_cost(n: int, x: int, y: int, roads: List[Tuple[int, int]]) -> int:
    """
    A group of n cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are $\frac{n \cdot(n - 1)}{2}$ roads in total. It takes exactly y seconds to traverse any single road.

    A spanning tree is a set of roads containing exactly n - 1 roads such that it's possible to travel between any two cities using only these roads.

    Some spanning tree of the initial network was chosen. For every road in this tree the time one needs to traverse this road was changed from y to x seconds. Note that it's not guaranteed that x is smaller than y.

    You would like to travel through all the cities using the shortest path possible. Given n, x, y and a description of the spanning tree that was chosen, find the cost of the shortest path that starts in any city, ends in any city and visits all cities exactly once.

    -----Input-----

    The first line of the input contains three integers n, x and y (2 ≤ n ≤ 200 000, 1 ≤ x, y ≤ 10^9).

    Each of the next n - 1 lines contains a description of a road in the spanning tree. The i-th of these lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n) — indices of the cities connected by the i-th road. It is guaranteed that these roads form a spanning tree.

    -----Output-----

    Print a single integer — the minimum number of seconds one needs to spend in order to visit all the cities exactly once.

    -----Examples-----
    Input
    5 2 3
    1 2
    1 3
    3 4
    5 3

    Output
    9

    Input
    5 3 2
    1 2
    1 3
    3 4
    5 3

    Output
    8

    -----Note-----

    In the first sample, roads of the spanning tree have cost 2, while other roads have cost 3. One example of an optimal path is $5 \rightarrow 3 \rightarrow 4 \rightarrow 1 \rightarrow 2$.

    In the second sample, we have the same spanning tree, but roads in the spanning tree cost 3, while other roads cost 2. One example of an optimal path is $1 \rightarrow 4 \rightarrow 5 \rightarrow 2 \rightarrow 3$.
    """


Prompt:
def find_min_goodness(n: int, h: int, a: List[int]) -> Tuple[int, List[int]]:
    """
    This problem is the most boring one you've ever seen.

    Given a sequence of integers a_1, a_2, ..., a_{n} and a non-negative integer h, our goal is to partition the sequence into two subsequences (not necessarily consist of continuous elements). Each element of the original sequence should be contained in exactly one of the result subsequences. Note, that one of the result subsequences can be empty.

    Let's define function f(a_{i}, a_{j}) on pairs of distinct elements (that is i ≠ j) in the original sequence. If a_{i} and a_{j} are in the same subsequence in the current partition then f(a_{i}, a_{j}) = a_{i} + a_{j} otherwise f(a_{i}, a_{j}) = a_{i} + a_{j} + h.

    Consider all possible values of the function f for some partition. We'll call the goodness of this partiotion the difference between the maximum value of function f and the minimum value of function f.

    Your task is to find a partition of the given sequence a that have the minimal possible goodness among all possible partitions.

    -----Input-----

    The first line of input contains integers n and h (2 ≤ n ≤ 10^5, 0 ≤ h ≤ 10^8). In the second line there is a list of n space-separated integers representing a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^8).

    -----Output-----

    The first line of output should contain the required minimum goodness.

    The second line describes the optimal partition. You should print n whitespace-separated integers in the second line. The i-th integer is 1 if a_{i} is in the first subsequence otherwise it should be 2.

    If there are several possible correct answers you are allowed to print any of them.

    -----Examples-----
    Input
    3 2
    1 2 3

    Output
    1
    1 2 2

    Input
    5 10
    0 1 0 2 1

    Output
    3
    2 2 2 2 2

    -----Note-----

    In the first sample the values of f are as follows: f(1, 2) = 1 + 2 + 2 = 5, f(1, 3) = 1 + 3 + 2 = 6 and f(2, 3) = 2 + 3 = 5. So the difference between maximum and minimum values of f is 1.

    In the second sample the value of h is large, so it's better for one of the sub-sequences to be empty.
    """


Prompt:
def find_permutation(n: int) -> Tuple[str, List[int]]:
    """
    Consider a sequence [a_1, a_2, ... , a_{n}]. Define its prefix product sequence $[ a_{1} \operatorname{mod} n,(a_{1} a_{2}) \operatorname{mod} n, \cdots,(a_{1} a_{2} \cdots a_{n}) \operatorname{mod} n ]$.

    Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].

    -----Input-----

    The only input line contains an integer n (1 ≤ n ≤ 10^5).

    -----Output-----

    In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.

    If any solution exists, you should output n more lines. i-th line contains only an integer a_{i}. The elements of the sequence should be different positive integers no larger than n.

    If there are multiple solutions, you are allowed to print any of them.

    -----Examples-----
    Input
    7

    Output
    YES
    1
    4
    3
    6
    5
    2
    7

    Input
    6

    Output
    NO

    -----Note-----

    For the second sample, there are no valid sequences.
    """

def check(find_permutation):
    assert find_permutation(7) == ('YES', [1, 2, 5, 6, 3, 4, 7])
    assert find_permutation(6) == ('NO', [])
    assert find_permutation(7137) == ('NO', [])
    assert find_permutation(1941) == ('NO', [])
    assert find_permutation(55004) == ('NO', [])
    assert find_permutation(1) == ('YES', [1])
    assert find_permutation(2) == ('YES', [1, 2])
    assert find_permutation(3) == ('YES', [1, 2, 3])
    assert find_permutation(4) == ('YES', [1, 3, 2, 4])
    assert find_permutation(5) == ('YES', [1, 2, 4, 3, 5])
check(find_permutation)


Prompt:
def count_connected_components(n: int, m: int, a: List[int]) -> int:
    """
    You are given a set of size $m$ with integer elements between $0$ and $2^{n}-1$ inclusive. Let's build an undirected graph on these integers in the following way: connect two integers $x$ and $y$ with an edge if and only if $x \& y = 0$. Here $\&$ is the bitwise AND operation. Count the number of connected components in that graph.

    -----Input-----

    In the first line of input there are two integers $n$ and $m$ ($0 \le n \le 22$, $1 \le m \le 2^{n}$).

    In the second line there are $m$ integers $a_1, a_2, \ldots, a_m$ ($0 \le a_{i} < 2^{n}$) — the elements of the set. All $a_{i}$ are distinct.

    -----Output-----

    Print the number of connected components.

    -----Examples-----
    Input
    2 3
    1 2 3

    Output
    2

    Input
    5 5
    5 19 10 20 12

    Output
    2

    -----Note-----

    Graph from first sample:

    $0$

    Graph from second sample:

    [Image]
    """

assert count_connected_components(2, 3, [1, 2, 3]) == 2

assert count_connected_components(5, 5, [5, 19, 10, 20, 12]) == 2


Prompt:
def find_final_label_probabilities(n: int, edges: List[Tuple[int, int]]) -> List[float]:
    """
    Consider a tree $T$ (that is, a connected graph without cycles) with $n$ vertices labelled $1$ through $n$. We start the following process with $T$: while $T$ has more than one vertex, do the following:

    choose a random edge of $T$ equiprobably;

    shrink the chosen edge: if the edge was connecting vertices $v$ and $u$, erase both $v$ and $u$ and create a new vertex adjacent to all vertices previously adjacent to either $v$ or $u$. The new vertex is labelled either $v$ or $u$ equiprobably.

    At the end of the process, $T$ consists of a single vertex labelled with one of the numbers $1, \ldots, n$. For each of the numbers, what is the probability of this number becoming the label of the final vertex?

    -----Input-----

    The first line contains a single integer $n$ ($1 \leq n \leq 50$).

    The following $n - 1$ lines describe the tree edges. Each of these lines contains two integers $u_i, v_i$ — labels of vertices connected by the respective edge ($1 \leq u_i, v_i \leq n$, $u_i \neq v_i$). It is guaranteed that the given graph is a tree.

    -----Output-----

    Print $n$ floating numbers — the desired probabilities for labels $1, \ldots, n$ respectively. All numbers should be correct up to $10^{-6}$ relative or absolute precision.

    -----Examples-----
    Input
    4
    1 2
    1 3
    1 4

    Output
    0.1250000000
    0.2916666667
    0.2916666667
    0.2916666667

    Input
    7
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7

    Output
    0.0850694444
    0.0664062500
    0.0664062500
    0.1955295139
    0.1955295139
    0.1955295139
    0.1955295139

    -----Note-----

    In the first sample, the resulting vertex has label 1 if and only if for all three edges the label 1 survives, hence the probability is $1/2^3 = 1/8$. All other labels have equal probability due to symmetry, hence each of them has probability $(1 - 1/8) / 3 = 7/24$.
    """


Prompt:
def find_gcd_of_lcms(n: int, a: List[int]) -> int:
    """
    For the multiset of positive integers $s=\{s_1,s_2,\dots,s_k\}$, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of $s$ as follow: $\gcd(s)$ is the maximum positive integer $x$, such that all integers in $s$ are divisible on $x$. $\textrm{lcm}(s)$ is the minimum positive integer $x$, that divisible on all integers from $s$.

    For example, $\gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6$ and $\textrm{lcm}(\{4,6\})=12$. Note that for any positive integer $x$, $\gcd(\{x\})=\textrm{lcm}(\{x\})=x$.

    Orac has a sequence $a$ with length $n$. He come up with the multiset $t=\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\}$, and asked you to find the value of $\gcd(t)$ for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.

    -----Input-----

    The first line contains one integer $n\ (2\le n\le 100\,000)$.

    The second line contains $n$ integers, $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 200\,000$).

    -----Output-----

    Print one integer: $\gcd(\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\})$.

    -----Examples-----
    Input
    2
    1 1

    Output
    1

    Input
    4
    10 24 40 80

    Output
    40

    Input
    10
    540 648 810 648 720 540 594 864 972 648

    Output
    54

    -----Note-----

    For the first example, $t=\{\textrm{lcm}(\{1,1\})\}=\{1\}$, so $\gcd(t)=1$.

    For the second example, $t=\{120,40,80,120,240,80\}$, and it's not hard to see that $\gcd(t)=40$.
    """


Prompt:
def construct_array(n: int, r: int, c: List[int], changes: List[Tuple[int, int]]) -> List[float]:
    """
    Allen and Bessie are playing a simple number game. They both know a function $f: \{0, 1\}^n \to \mathbb{R}$, i. e. the function takes $n$ binary arguments and returns a real value. At the start of the game, the variables $x_1, x_2, \dots, x_n$ are all set to $-1$. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an $i$ such that $x_i = -1$ and either setting $x_i \to 0$ or $x_i \to 1$.

    After $n$ rounds all variables are set, and the game value resolves to $f(x_1, x_2, \dots, x_n)$. Allen wants to maximize the game value, and Bessie wants to minimize it.

    Your goal is to help Allen and Bessie find the expected game value! They will play $r+1$ times though, so between each game, exactly one value of $f$ changes. In other words, between rounds $i$ and $i+1$ for $1 \le i \le r$, $f(z_1, \dots, z_n) \to g_i$ for some $(z_1, \dots, z_n) \in \{0, 1\}^n$. You are to find the expected game value in the beginning and after each change.

    -----Input-----

    The first line contains two integers $n$ and $r$ ($1 \le n \le 18$, $0 \le r \le 2^{18}$).

    The next line contains $2^n$ integers $c_0, c_1, \dots, c_{2^n-1}$ ($0 \le c_i \le 10^9$), denoting the initial values of $f$. More specifically, $f(x_0, x_1, \dots, x_{n-1}) = c_x$, if $x = \overline{x_{n-1} \ldots x_0}$ in binary.

    Each of the next $r$ lines contains two integers $z$ and $g$ ($0 \le z \le 2^n - 1$, $0 \le g \le 10^9$). If $z = \overline{z_{n-1} \dots z_0}$ in binary, then this means to set $f(z_0, \dots, z_{n-1}) \to g$.

    -----Output-----

    Print $r+1$ lines, the $i$-th of which denotes the value of the game $f$ during the $i$-th round. Your answer must have absolute or relative error within $10^{-6}$.

    Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is considered correct if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.

    -----Examples-----
    Input
    2 2
    0 1 2 3
    2 5
    0 4

    Output
    1.500000
    2.250000
    3.250000

    Input
    1 0
    2 3

    Output
    2.500000

    Input
    2 0
    1 1 1 1

    Output
    1.000000

    -----Note-----

    Consider the second test case. If Allen goes first, he will set $x_1 \to 1$, so the final value will be $3$. If Bessie goes first, then she will set $x_1 \to 0$ so the final value will be $2$. Thus the answer is $2.5$.

    In the third test case, the game value will always be $1$ regardless of Allen and Bessie's play.
    """


Prompt:
def coexist_in_peace(n: int, q: int, word: str, operations: List[Tuple[str, int, str]]) -> List[str]:
    """
    During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now wonder if the followers of each religion could coexist in peace.

    The Word of Universe is a long word containing the lowercase English characters only. At each moment of time, each of the religion beliefs could be described by a word consisting of lowercase English characters.

    The three religions can coexist in peace if their descriptions form disjoint subsequences of the Word of Universe. More formally, one can paint some of the characters of the Word of Universe in three colors: $1$, $2$, $3$, so that each character is painted in at most one color, and the description of the $i$-th religion can be constructed from the Word of Universe by removing all characters that aren't painted in color $i$.

    The religions however evolve. In the beginning, each religion description is empty. Every once in a while, either a character is appended to the end of the description of a single religion, or the last character is dropped from the description. After each change, determine if the religions could coexist in peace.

    -----Input-----

    The first line of the input contains two integers $n, q$ ($1 \leq n \leq 100\,000$, $1 \leq q \leq 1000$) — the length of the Word of Universe and the number of religion evolutions, respectively. The following line contains the Word of Universe — a string of length $n$ consisting of lowercase English characters.

    Each of the following line describes a single evolution and is in one of the following formats:   + $i$ $c$ ($i \in \{1, 2, 3\}$, $c \in \{\mathtt{a}, \mathtt{b}, \dots, \mathtt{z}\}$: append the character $c$ to the end of $i$-th religion description.  - $i$ ($i \in \{1, 2, 3\}$) – remove the last character from the $i$-th religion description. You can assume that the pattern is non-empty.

    You can assume that no religion will have description longer than $250$ characters.

    -----Output-----

    Write $q$ lines. The $i$-th of them should be YES if the religions could coexist in peace after the $i$-th evolution, or NO otherwise.

    You can print each character in any case (either upper or lower).

    -----Examples-----
    Input
    6 8
    abdabc
    + 1 a
    + 1 d
    + 2 b
    + 2 c
    + 3 a
    + 3 b
    + 1 c
    - 2

    Output
    YES
    YES
    YES
    YES
    YES
    YES
    NO
    YES

    Input
    6 8
    abbaab
    + 1 a
    + 2 a
    + 3 a
    + 1 b
    + 2 b
    + 3 b
    - 1
    + 2 z

    Output
    YES
    YES
    YES
    YES
    YES
    NO
    YES
    NO

    -----Note-----

    In the first example, after the 6th evolution the religion descriptions are: ad, bc, and ab. The following figure shows how these descriptions form three disjoint subsequences of the Word of Universe: $\left. \begin{array}{|c|c|c|c|c|c|c|} \hline \text{Word} & {a} & {b} & {d} & {a} & {b} & {c} \\ \hline ad & {a} & {} & {d} & {} & {} & {} \\ \hline bc & {} & {b} & {} & {} & {} & {c} \\ \hline ab & {} & {} & {} & {a} & {b} & {} \\ \hline \end{array} \right.$
    """


Prompt:
def min_processing_time(n: int, k: int, contrasts: List[int]) -> int:
    """
    Evlampiy has found one more cool application to process photos. However the application has certain limitations.

    Each photo i has a contrast v_{i}. In order for the processing to be truly of high quality, the application must receive at least k photos with contrasts which differ as little as possible.

    Evlampiy already knows the contrast v_{i} for each of his n photos. Now he wants to split the photos into groups, so that each group contains at least k photos. As a result, each photo must belong to exactly one group.

    He considers a processing time of the j-th group to be the difference between the maximum and minimum values of v_{i} in the group. Because of multithreading the processing time of a division into groups is the maximum processing time among all groups.

    Split n photos into groups in a such way that the processing time of the division is the minimum possible, i.e. that the the maximum processing time over all groups as least as possible.

    -----Input-----

    The first line contains two integers n and k (1 ≤ k ≤ n ≤ 3·10^5) — number of photos and minimum size of a group.

    The second line contains n integers v_1, v_2, ..., v_{n} (1 ≤ v_{i} ≤ 10^9), where v_{i} is the contrast of the i-th photo.

    -----Output-----

    Print the minimal processing time of the division into groups.

    -----Examples-----
    Input
    5 2
    50 110 130 40 120

    Output
    20

    Input
    4 1
    2 3 4 1

    Output
    0

    -----Note-----

    In the first example the photos should be split into 2 groups: [40, 50] and [110, 120, 130]. The processing time of the first group is 10, and the processing time of the second group is 20. Maximum among 10 and 20 is 20. It is impossible to split the photos into groups in a such way that the processing time of division is less than 20.

    In the second example the photos should be split into four groups, each containing one photo. So the minimal possible processing time of a division is 0.
    """

assert min_processing_time(4, 1, [2, 3, 4, 1]) == 0


Prompt:
def max_trip_people(n: int, m: int, k: int, friendships: List[Tuple[int, int]]) -> List[int]:
    """
    There are $n$ persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.

    We want to plan a trip for every evening of $m$ days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:   Either this person does not go on the trip,  Or at least $k$ of his friends also go on the trip.

    Note that the friendship is not transitive. That is, if $a$ and $b$ are friends and $b$ and $c$ are friends, it does not necessarily imply that $a$ and $c$ are friends.

    For each day, find the maximum number of people that can go on the trip on that day.

    -----Input-----

    The first line contains three integers $n$, $m$, and $k$ ($2 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5$, $1 \le k < n$) — the number of people, the number of days and the number of friends each person on the trip should have in the group.

    The $i$-th ($1 \leq i \leq m$) of the next $m$ lines contains two integers $x$ and $y$ ($1\leq x, y\leq n$, $x\ne y$), meaning that persons $x$ and $y$ become friends on the morning of day $i$. It is guaranteed that $x$ and $y$ were not friends before.

    -----Output-----

    Print exactly $m$ lines, where the $i$-th of them ($1\leq i\leq m$) contains the maximum number of people that can go on the trip on the evening of the day $i$.

    -----Examples-----
    Input
    4 4 2
    2 3
    1 2
    1 3
    1 4

    Output
    0
    0
    3
    3

    Input
    5 8 2
    2 1
    4 2
    5 4
    5 2
    4 3
    5 1
    4 1
    3 2

    Output
    0
    0
    0
    3
    3
    4
    4
    5

    Input
    5 7 2
    1 5
    3 2
    2 5
    3 4
    1 2
    5 3
    1 3

    Output
    0
    0
    0
    0
    3
    4
    4

    -----Note-----

    In the first example,   $1,2,3$ can go on day $3$ and $4$.

    In the second example,   $2,4,5$ can go on day $4$ and $5$.  $1,2,4,5$ can go on day $6$ and $7$.  $1,2,3,4,5$ can go on day $8$.

    In the third example,   $1,2,5$ can go on day $5$.  $1,2,3,5$ can go on day $6$ and $7$.
    """


Prompt:
def color_cells(n: int, m: int, l: List[int]) -> Union[List[int], int]:
    """
    Dreamoon likes coloring cells very much.

    There is a row of $n$ cells. Initially, all cells are empty (don't contain any color). Cells are numbered from $1$ to $n$.

    You are given an integer $m$ and $m$ integers $l_1, l_2, \ldots, l_m$ ($1 \le l_i \le n$)

    Dreamoon will perform $m$ operations.

    In $i$-th operation, Dreamoon will choose a number $p_i$ from range $[1, n-l_i+1]$ (inclusive) and will paint all cells from $p_i$ to $p_i+l_i-1$ (inclusive) in $i$-th color. Note that cells may be colored more one than once, in this case, cell will have the color from the latest operation.

    Dreamoon hopes that after these $m$ operations, all colors will appear at least once and all cells will be colored. Please help Dreamoon to choose $p_i$ in each operation to satisfy all constraints.

    -----Input-----

    The first line contains two integers $n,m$ ($1 \leq m \leq n \leq 100\,000$).

    The second line contains $m$ integers $l_1, l_2, \ldots, l_m$ ($1 \leq l_i \leq n$).

    -----Output-----

    If it's impossible to perform $m$ operations to satisfy all constraints, print "'-1" (without quotes).

    Otherwise, print $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \leq p_i \leq n - l_i + 1$), after these $m$ operations, all colors should appear at least once and all cells should be colored.

    If there are several possible solutions, you can print any.

    -----Examples-----

    Input
    5 3
    3 2 2

    Output
    2 4 1

    Input
    10 1
    1

    Output
    -1
    """


Prompt:
def count_u_shaped_parabolas(n: int, points: List[Tuple[int, int]]) -> int:
    """
    Recently Vasya learned that, given two points with different $x$ coordinates, you can draw through them exactly one parabola with equation of type $y = x^2 + bx + c$, where $b$ and $c$ are reals. Let's call such a parabola an $U$-shaped one.

    Vasya drew several distinct points with integer coordinates on a plane and then drew an $U$-shaped parabola through each pair of the points that have different $x$ coordinates. The picture became somewhat messy, but Vasya still wants to count how many of the parabolas drawn don't have any drawn point inside their internal area. Help Vasya.

    The internal area of an $U$-shaped parabola is the part of the plane that lies strictly above the parabola when the $y$ axis is directed upwards.

    -----Input-----

    The first line contains a single integer $n$ ($1 \le n \le 100\,000$) — the number of points.

    The next $n$ lines describe the points, the $i$-th of them contains two integers $x_i$ and $y_i$ — the coordinates of the $i$-th point. It is guaranteed that all points are distinct and that the coordinates do not exceed $10^6$ by absolute value.

    -----Output-----

    In the only line print a single integer — the number of $U$-shaped parabolas that pass through at least two of the given points and do not contain any of the given points inside their internal area (excluding the parabola itself).

    -----Examples-----
    Input
    3
    -1 0
    0 2
    1 0

    Output
    2

    Input
    5
    1 0
    1 -1
    0 -1
    -1 0
    -1 -1

    Output
    1

    -----Note-----

    On the pictures below all $U$-shaped parabolas that pass through at least two given points are drawn for each of the examples. The $U$-shaped parabolas that do not have any given point inside their internal area are drawn in red.  [Image] The first example.

    [Image] The second example.
    """

def check(candidate):
    assert count_u_shaped_parabolas(3, [(-1, 0), (0, 2), (1, 0)]) == 2
    assert count_u_shaped_parabolas(5, [(1, 0), (1, -1), (0, -1), (-1, 0), (-1, -1)]) == 1
    assert count_u_shaped_parabolas(1, [(-751115, -925948)]) == 0
check(count_u_shaped_parabolas)


Prompt:
def max_non_overlapping_dominos(n: int, heights: List[int]) -> int:
    """
    You are given a Young diagram.

    Given diagram is a histogram with $n$ columns of lengths $a_1, a_2, \ldots, a_n$ ($a_1 \geq a_2 \geq \ldots \geq a_n \geq 1$). [Image] Young diagram for $a=[3,2,2,2,1]$.

    Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a $1 \times 2$ or $2 \times 1$ rectangle.

    -----Input-----

    The first line of input contain one integer $n$ ($1 \leq n \leq 300\,000$): the number of columns in the given histogram.

    The next line of input contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 300\,000, a_i \geq a_{i+1}$): the lengths of columns.

    -----Output-----

    Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram.

    -----Example-----
    Input
    5
    3 2 2 2 1

    Output
    4

    -----Note-----

    Some of the possible solutions for the example:

    [Image] $\square$
    """

assert max_non_overlapping_dominos(5, [3, 2, 2, 2, 1]) == 4


Prompt:
def final_number_after_operations(n: int, nums: List[int]) -> int:
    """
    Karen has just arrived at school, and she has a math test today! [Image]

    The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points.

    There are n integers written on a row. Karen must alternately add and subtract each pair of adjacent integers, and write down the sums or differences on the next row. She must repeat this process on the values on the next row, and so on, until only one integer remains. The first operation should be addition.

    Note that, if she ended the previous row by adding the integers, she should start the next row by subtracting, and vice versa.

    The teachers will simply look at the last integer, and then if it is correct, Karen gets a perfect score, otherwise, she gets a zero for the test.

    Karen has studied well for this test, but she is scared that she might make a mistake somewhere and it will cause her final answer to be wrong. If the process is followed, what number can she expect to be written on the last row?

    Since this number can be quite large, output only the non-negative remainder after dividing it by 10^9 + 7.

    -----Input-----

    The first line of input contains a single integer n (1 ≤ n ≤ 200000), the number of numbers written on the first row.

    The next line contains n integers. Specifically, the i-th one among these is a_{i} (1 ≤ a_{i} ≤ 10^9), the i-th number on the first row.

    -----Output-----

    Output a single integer on a line by itself, the number on the final row after performing the process above.

    Since this number can be quite large, print only the non-negative remainder after dividing it by 10^9 + 7.

    -----Examples-----
    Input
    5
    3 6 9 12 15

    Output
    36

    Input
    4
    3 7 5 2

    Output
    1000000006

    -----Note-----

    In the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15.

    Karen performs the operations as follows: [Image]

    The non-negative remainder after dividing the final number by 10^9 + 7 is still 36, so this is the correct output.

    In the second test case, the numbers written on the first row are 3, 7, 5 and 2.

    Karen performs the operations as follows: [Image]

    The non-negative remainder after dividing the final number by 10^9 + 7 is 10^9 + 6, so this is the correct output.
    """

assert final_number_after_operations(5, [3, 6, 9, 12, 15]) == 36

assert final_number_after_operations(4, [3, 7, 5, 2]) == 1000000006


Prompt:
def participants_excluded_from_ranking(n: int, k: int, ratings: List[int]) -> List[int]:
    """
    During the last Sereja's Codesecrof round the server crashed many times, so the round was decided to be made unrated for some participants.

    Let's assume that n people took part in the contest. Let's assume that the participant who got the first place has rating a_1, the second place participant has rating a_2, ..., the n-th place participant has rating a_{n}. Then changing the rating on the Codesecrof site is calculated by the formula $d_{i} = \sum_{j = 1}^{i - 1}(a_{j} \cdot(j - 1) -(n - i) \cdot a_{i})$.

    After the round was over, the Codesecrof management published the participants' results table. They decided that if for a participant d_{i} < k, then the round can be considered unrated for him. But imagine the management's surprise when they found out that the participants' rating table is dynamic. In other words, when some participant is removed from the rating, he is removed from the results' table and the rating is recalculated according to the new table. And of course, all applications for exclusion from the rating are considered in view of the current table.

    We know that among all the applications for exclusion from the rating the first application to consider is from the participant with the best rank (the rank with the minimum number), for who d_{i} < k. We also know that the applications for exclusion from rating were submitted by all participants.

    Now Sereja wonders, what is the number of participants to be excluded from the contest rating, and the numbers of the participants in the original table in the order of their exclusion from the rating. Pay attention to the analysis of the first test case for a better understanding of the statement.

    -----Input-----

    The first line contains two integers n, k (1 ≤ n ≤ 2·10^5,  - 10^9 ≤ k ≤ 0). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — ratings of the participants in the initial table.

    -----Output-----

    Print the numbers of participants in the order in which they were removed from the table. Print the initial numbers of the participants, that is, the numbers that the participants had in the initial table.

    -----Examples-----
    Input
    5 0
    5 3 4 1 2

    Output
    2
    3
    4

    Input
    10 -10
    5 5 1 7 5 1 2 4 9 2

    Output
    2
    4
    5
    7
    8
    9

    -----Note-----

    Consider the first test sample.

    Initially the sequence of the contest participants' ratings equals [5, 3, 4, 1, 2]. You can use this sequence to calculate the sequence of rating changes: [0, -9, -13, 8, 14]. According to the problem statement, the application of the participant who won the second place will be considered first.

    As soon as the second place winner is out from the ratings, the participants' rating sequence will equal [5, 4, 1, 2]. By this sequence you can count the new sequence of rating changes: [0, -8, 2, 6]. According to the problem statement, the application of the participant who won the second place will be considered. Initially this participant won third place.

    The new rating sequence equals [5, 1, 2], the new sequence of rating changes equals [0, -1, 1]. The second place participant's application is taken into consideration, initially this participant won the fourth place.

    The new rating sequence equals [5, 2], the new sequence of rating changes equals [0, 0]. No more applications will be considered.

    Thus, you should print 2, 3, 4.
    """

def check(candidate):
    assert participants_excluded_from_ranking(5, 0, [5, 3, 4, 1, 2]) == [2, 3, 4]
    assert participants_excluded_from_ranking(10, -10, [5, 5, 1, 7, 5, 1, 2, 4, 9, 2]) == [2, 4, 5, 7, 8, 9]
check(participants_excluded_from_ranking)


Prompt:
def restore_sequence(n: int, matrix: List[List[int]]) -> List[int]:
    """
    Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.

    For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a_1, a_2, ..., a_{n}. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as b_{ij}) equals:  the "bitwise AND" of numbers a_{i} and a_{j} (that is, b_{ij} = a_{i} & a_{j}), if i ≠ j;  -1, if i = j.

    Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.

    Help Polycarpus, given matrix b, restore the sequence of numbers a_1, a_2, ..., a_{n}, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 10^9.

    -----Input-----

    The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix b_{ij}. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: b_{ii} = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ b_{ij} ≤ 10^9, b_{ij} = b_{ji}.

    -----Output-----

    Print n non-negative integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.

    It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.

    -----Examples-----
    Input
    1
    -1

    Output
    0
    Input
    3
    -1 18 0
    18 -1 0
    0 0 -1

    Output
    18 18 0
    Input
    4
    -1 128 128 128
    128 -1 148 160
    128 148 -1 128
    128 160 128 -1

    Output
    128 180 148 160

    -----Note-----

    If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
    """

assert restore_sequence(1, [[-1]]) == [0]


Prompt:
def minimize_eating_time(n: int, k: int, a: List[int]) -> int:
    """
    There are some rabbits in Singapore Zoo. To feed them, Zookeeper bought $n$ carrots with lengths $a_1, a_2, a_3, \ldots, a_n$. However, rabbits are very fertile and multiply very quickly. Zookeeper now has $k$ rabbits and does not have enough carrots to feed all of them. To solve this problem, Zookeeper decided to cut the carrots into $k$ pieces. For some reason, all resulting carrot lengths must be positive integers.

    Big carrots are very difficult for rabbits to handle and eat, so the time needed to eat a carrot of size $x$ is $x^2$.

    Help Zookeeper split his carrots while minimizing the sum of time taken for rabbits to eat the carrots.

    -----Input-----

    The first line contains two integers $n$ and $k$ $(1 \leq n \leq k \leq 10^5)$: the initial number of carrots and the number of rabbits.

    The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(1 \leq a_i \leq 10^6)$: lengths of carrots.

    It is guaranteed that the sum of $a_i$ is at least $k$.

    -----Output-----

    Output one integer: the minimum sum of time taken for rabbits to eat carrots.

    -----Examples-----
    Input
    3 6
    5 3 1

    Output
    15

    Input
    1 4
    19

    Output
    91

    -----Note-----

    For the first test, the optimal sizes of carrots are $\{1,1,1,2,2,2\}$. The time taken is $1^2+1^2+1^2+2^2+2^2+2^2=15$

    For the second test, the optimal sizes of carrots are $\{4,5,5,5\}$. The time taken is $4^2+5^2+5^2+5^2=91$.
    """

assert minimize_eating_time(3, 6, [5, 3, 1]) == 15

assert minimize_eating_time(1, 4, [19]) == 91


Prompt:
def count_picking_ways(n: int, m: int, a: List[int]) -> List[int]:
    """
    This is the easy version of the problem. The only difference between easy and hard versions is the constraint of $m$. You can make hacks only if both versions are solved.

    Chiori loves dolls and now she is going to decorate her bedroom![Image]

    As a doll collector, Chiori has got $n$ dolls. The $i$-th doll has a non-negative integer value $a_i$ ($a_i < 2^m$, $m$ is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are $2^n$ different picking ways.

    Let $x$ be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls $x = 0$). The value of this picking way is equal to the number of $1$-bits in the binary representation of $x$. More formally, it is also equal to the number of indices $0 \leq i < m$, such that $\left\lfloor \frac{x}{2^i} \right\rfloor$ is odd.

    Tell her the number of picking ways with value $i$ for each integer $i$ from $0$ to $m$. Due to the answers can be very huge, print them by modulo $998\,244\,353$.

    -----Input-----

    The first line contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $0 \le m \le 35$)  — the number of dolls and the maximum value of the picking way.

    The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i < 2^m$)  — the values of dolls.

    -----Output-----

    Print $m+1$ integers $p_0, p_1, \ldots, p_m$  — $p_i$ is equal to the number of picking ways with value $i$ by modulo $998\,244\,353$.

    -----Examples-----
    Input
    4 4
    3 5 8 14

    Output
    2 2 6 6 0
    Input
    6 7
    11 45 14 9 19 81

    Output
    1 2 11 20 15 10 5 0
    """


Prompt:
def maximize_column_sums(n: int, w: int, arrays: List[List[int]]) -> List[int]:
    """
    You are given $n$ arrays that can have different sizes. You also have a table with $w$ columns and $n$ rows. The $i$-th array is placed horizontally in the $i$-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table.

    You need to find the maximum sum of the integers in the $j$-th column for each $j$ from $1$ to $w$ independently.

    [Image] Optimal placements for columns $1$, $2$ and $3$ are shown on the pictures from left to right.

    Note that you can exclude any array out of a column provided it remains in the window. In this case its value is considered to be zero.

    -----Input-----

    The first line contains two integers $n$ ($1 \le n \le 10^{6}$) and $w$ ($1 \le w \le 10^{6}$) — the number of arrays and the width of the table.

    Each of the next $n$ lines consists of an integer $l_{i}$ ($1 \le l_{i} \le w$), the length of the $i$-th array, followed by $l_{i}$ integers $a_{i1}, a_{i2}, \ldots, a_{il_i}$ ($-10^{9} \le a_{ij} \le 10^{9}$) — the elements of the array.

    The total length of the arrays does no exceed $10^{6}$.

    -----Output-----

    Print $w$ integers, the $i$-th of them should be the maximum sum for column $i$.

    -----Examples-----
    Input
    3 3
    3 2 4 8
    2 2 5
    2 6 3

    Output
    10 15 16

    Input
    2 2
    2 7 8
    1 -8

    Output
    7 8

    -----Note-----

    Illustration for the first example is in the statement.
    """


Prompt:
def maximize_haybales(t: int, test_cases: List[Tuple[int, int, List[int]]]) -> List[int]:
    """
    The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of $n$ haybale piles on the farm. The $i$-th pile contains $a_i$ haybales.

    However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices $i$ and $j$ ($1 \le i, j \le n$) such that $|i-j|=1$ and $a_i>0$ and apply $a_i = a_i - 1$, $a_j = a_j + 1$. She may also decide to not do anything on some days because she is lazy.

    Bessie wants to maximize the number of haybales in pile $1$ (i.e. to maximize $a_1$), and she only has $d$ days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile $1$ if she acts optimally!

    -----Input-----

    The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 100$)  — the number of test cases. Next $2t$ lines contain a description of test cases  — two lines per test case.

    The first line of each test case contains integers $n$ and $d$ ($1 \le n,d \le 100$) — the number of haybale piles and the number of days, respectively.

    The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 100$)  — the number of haybales in each pile.

    -----Output-----

    For each test case, output one integer: the maximum number of haybales that may be in pile $1$ after $d$ days if Bessie acts optimally.

    -----Example-----
    Input
    3
    4 5
    1 0 3 2
    2 2
    100 1
    1 8
    0

    Output
    3
    101
    0

    -----Note-----

    In the first test case of the sample, this is one possible way Bessie can end up with $3$ haybales in pile $1$:   On day one, move a haybale from pile $3$ to pile $2$  On day two, move a haybale from pile $3$ to pile $2$  On day three, move a haybale from pile $2$ to pile $1$  On day four, move a haybale from pile $2$ to pile $1$  On day five, do nothing

    In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile $2$ to pile $1$ on the second day.
    """


Prompt:
def shortest_time_to_move_token(n: int, m: int, edges: List[Tuple[int, int]]) -> int:
    """
    You are given a directed graph of $n$ vertices and $m$ edges. Vertices are numbered from $1$ to $n$. There is a token in vertex $1$.

    The following actions are allowed:   Token movement. To move the token from vertex $u$ to vertex $v$ if there is an edge $u \to v$ in the graph. This action takes $1$ second.  Graph transposition. To transpose all the edges in the graph: replace each edge $u \to v$ by an edge $v \to u$. This action takes increasingly more time: $k$-th transposition takes $2^{k-1}$ seconds, i.e. the first transposition takes $1$ second, the second one takes $2$ seconds, the third one takes $4$ seconds, and so on.

    The goal is to move the token from vertex $1$ to vertex $n$ in the shortest possible time. Print this time modulo $998\,244\,353$.

    -----Input-----

    The first line of input contains two integers $n, m$ ($1 \le n, m \le 200\,000$).

    The next $m$ lines contain two integers each: $u, v$ ($1 \le u, v \le n; u \ne v$), which represent the edges of the graph. It is guaranteed that all ordered pairs $(u, v)$ are distinct.

    It is guaranteed that it is possible to move the token from vertex $1$ to vertex $n$ using the actions above.

    -----Output-----

    Print one integer: the minimum required time modulo $998\,244\,353$.

    -----Examples-----
    Input
    4 4
    1 2
    2 3
    3 4
    4 1

    Output
    2

    Input
    4 3
    2 1
    2 3
    4 3

    Output
    10

    -----Note-----

    The first example can be solved by transposing the graph and moving the token to vertex $4$, taking $2$ seconds.

    The best way to solve the second example is the following: transpose the graph, move the token to vertex $2$, transpose the graph again, move the token to vertex $3$, transpose the graph once more and move the token to vertex $4$.
    """


Prompt:
def is_possible_xor_sum(m: int, numbers: List[int]) -> List[str]:
    """
    After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number x to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals x?

    If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket.

    Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on.

    Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him.

    -----Input-----

    The first line contains number m (1 ≤ m ≤ 2000), showing how many numbers are scattered around the room.

    The next m lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10^600 that doesn't contain leading zeroes.

    -----Output-----

    For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer k showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once.

    -----Examples-----
    Input
    7
    7
    6
    5
    4
    3
    2
    1

    Output
    0
    0
    0
    3 0 1 2
    2 1 2
    2 0 2
    2 0 1

    Input
    2
    5
    5

    Output
    0
    1 0

    -----Note-----

    The XOR sum of numbers is the result of bitwise sum of numbers modulo 2.
    """


Prompt:
def partition_sum(n: int, a: List[int]) -> int:
    """
    You are given an array $a$ of length $2n$. Consider a partition of array $a$ into two subsequences $p$ and $q$ of length $n$ each (each element of array $a$ should be in exactly one subsequence: either in $p$ or in $q$).

    Let's sort $p$ in non-decreasing order, and $q$ in non-increasing order, we can denote the sorted versions by $x$ and $y$, respectively. Then the cost of a partition is defined as $f(p, q) = \sum_{i = 1}^n |x_i - y_i|$.

    Find the sum of $f(p, q)$ over all correct partitions of array $a$. Since the answer might be too big, print its remainder modulo $998244353$.

    -----Input-----

    The first line contains a single integer $n$ ($1 \leq n \leq 150\,000$).

    The second line contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($1 \leq a_i \leq 10^9$) — elements of array $a$.

    -----Output-----

    Print one integer — the answer to the problem, modulo $998244353$.

    -----Examples-----
    Input
    1
    1 4

    Output
    6
    Input
    2
    2 1 2 1

    Output
    12
    Input
    3
    2 2 2 2 2 2

    Output
    0
    Input
    5
    13 8 35 94 9284 34 54 69 123 846

    Output
    2588544

    -----Note-----

    Two partitions of an array are considered different if the sets of indices of elements included in the subsequence $p$ are different.

    In the first example, there are two correct partitions of the array $a$:  $p = [1]$, $q = [4]$, then $x = [1]$, $y = [4]$, $f(p, q) = |1 - 4| = 3$;  $p = [4]$, $q = [1]$, then $x = [4]$, $y = [1]$, $f(p, q) = |4 - 1| = 3$.

    In the second example, there are six valid partitions of the array $a$:   $p = [2, 1]$, $q = [2, 1]$ (elements with indices $1$ and $2$ in the original array are selected in the subsequence $p$);  $p = [2, 2]$, $q = [1, 1]$;  $p = [2, 1]$, $q = [1, 2]$ (elements with indices $1$ and $4$ are selected in the subsequence $p$);  $p = [1, 2]$, $q = [2, 1]$;  $p = [1, 1]$, $q = [2, 2]$;  $p = [2, 1]$, $q = [2, 1]$ (elements with indices $3$ and $4$ are selected in the subsequence $p$).
    """

assert partition_sum(2, [2, 1, 2, 1]) == 12


Prompt:
def minimal_coins(n: int, a: str, b: str) -> List[Union[int, Tuple[int, int]]]:
    """
    One player came to a casino and found a slot machine where everything depends only on how he plays. The rules follow.

    A positive integer $a$ is initially on the screen. The player can put a coin into the machine and then add $1$ to or subtract $1$ from any two adjacent digits. All digits must remain from $0$ to $9$ after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add $1$ to $9$, to subtract $1$ from $0$ and to subtract $1$ from the leading $1$. Once the number on the screen becomes equal to $b$, the player wins the jackpot. $a$ and $b$ have the same number of digits.

    Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play.

    -----Input-----

    The first line contains a single integer $n$ ($2 \le n \le 10^5$) standing for the length of numbers $a$ and $b$.

    The next two lines contain numbers $a$ and $b$, each one on a separate line ($10^{n-1} \le a, b < 10^n$).

    -----Output-----

    If it is impossible to win the jackpot, print a single integer $-1$.

    Otherwise, the first line must contain the minimal possible number $c$ of coins the player has to spend.

    $\min(c, 10^5)$ lines should follow, $i$-th of them containing two integers $d_i$ and $s_i$ ($1\le d_i\le n - 1$, $s_i = \pm 1$) denoting that on the $i$-th step the player should add $s_i$ to the $d_i$-th and $(d_i + 1)$-st digits from the left (e. g. $d_i = 1$ means that two leading digits change while $d_i = n - 1$ means that there are two trailing digits which change).

    Please notice that the answer may be very big and in case $c > 10^5$ you should print only the first $10^5$ moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them.

    -----Examples-----
    Input
    3
    223
    322

    Output
    2
    1 1
    2 -1

    Input
    2
    20
    42

    Output
    2
    1 1
    1 1

    Input
    2
    35
    44

    Output
    -1

    -----Note-----

    In the first example, we can make a +1 operation on the two first digits, transforming number $\textbf{22}3$ into $\textbf{33}3$, and then make a -1 operation on the last two digits, transforming $3\textbf{33}$ into $3\textbf{22}$.

    It's also possible to do these operations in reverse order, which makes another correct answer.

    In the last example, one can show that it's impossible to transform $35$ into $44$.
    """


Prompt:
def weight_of_array(n: int, q: int, a: List[int], queries: List[Tuple[int, int]]) -> List[int]:
    """
    Let $a_1, \ldots, a_n$ be an array of $n$ positive integers. In one operation, you can choose an index $i$ such that $a_i = i$, and remove $a_i$ from the array (after the removal, the remaining parts are concatenated).

    The weight of $a$ is defined as the maximum number of elements you can remove.

    You must answer $q$ independent queries $(x, y)$: after replacing the $x$ first elements of $a$ and the $y$ last elements of $a$ by $n+1$ (making them impossible to remove), what would be the weight of $a$?

    -----Input-----

    The first line contains two integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$)  — the length of the array and the number of queries.

    The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$) — elements of the array.

    The $i$-th of the next $q$ lines contains two integers $x$ and $y$ ($x, y \ge 0$ and $x+y < n$).

    -----Output-----

    Print $q$ lines, $i$-th line should contain a single integer  — the answer to the $i$-th query.

    -----Examples-----
    Input
    13 5
    2 2 3 9 5 4 6 5 7 8 3 11 13
    3 1
    0 0
    2 4
    5 0
    0 12

    Output
    5
    11
    6
    1
    0

    Input
    5 2
    1 4 1 2 4
    0 0
    1 0

    Output
    2
    0

    -----Note-----

    Explanation of the first query:

    After making first $x = 3$ and last $y = 1$ elements impossible to remove, $a$ becomes $[\times, \times, \times, 9, 5, 4, 6, 5, 7, 8, 3, 11, \times]$ (we represent $14$ as $\times$ for clarity).

    Here is a strategy that removes $5$ elements (the element removed is colored in red):  $[\times, \times, \times, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, \times]$  $[\times, \times, \times, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, \times]$  $[\times, \times, \times, 9, 4, \color{red}{6}, 5, 7, 8, 3, \times]$  $[\times, \times, \times, 9, 4, 5, 7, \color{red}{8}, 3, \times]$  $[\times, \times, \times, 9, 4, 5, \color{red}{7}, 3, \times]$  $[\times, \times, \times, 9, 4, 5, 3, \times]$ (final state)

    It is impossible to remove more than $5$ elements, hence the weight is $5$.
    """


Prompt:
def expected_game_duration(n: int, a: List[int]) -> int:
    """
    Slime and his $n$ friends are at a party. Slime has designed a game for his friends to play.

    At the beginning of the game, the $i$-th player has $a_i$ biscuits. At each second, Slime will choose a biscuit randomly uniformly among all $a_1 + a_2 + \ldots + a_n$ biscuits, and the owner of this biscuit will give it to a random uniform player among $n-1$ players except himself. The game stops when one person will have all the biscuits.

    As the host of the party, Slime wants to know the expected value of the time that the game will last, to hold the next activity on time.

    For convenience, as the answer can be represented as a rational number $\frac{p}{q}$ for coprime $p$ and $q$, you need to find the value of $(p \cdot q^{-1})\mod 998\,244\,353$. You can prove that $q\mod 998\,244\,353 \neq 0$.

    -----Input-----

    The first line contains one integer $n\ (2\le n\le 100\,000)$: the number of people playing the game.

    The second line contains $n$ non-negative integers $a_1,a_2,\dots,a_n\ (1\le a_1+a_2+\dots+a_n\le 300\,000)$, where $a_i$ represents the number of biscuits the $i$-th person own at the beginning.

    -----Output-----

    Print one integer: the expected value of the time that the game will last, modulo $998\,244\,353$.

    -----Examples-----
    Input
    2
    1 1

    Output
    1

    Input
    2
    1 2

    Output
    3

    Input
    5
    0 0 0 0 35

    Output
    0

    Input
    5
    8 4 2 0 1

    Output
    801604029

    -----Note-----

    For the first example, in the first second, the probability that player $1$ will give the player $2$ a biscuit is $\frac{1}{2}$, and the probability that player $2$ will give the player $1$ a biscuit is $\frac{1}{2}$. But anyway, the game will stop after exactly $1$ second because only one player will occupy all biscuits after $1$ second, so the answer is $1$.
    """

assert expected_game_duration(2, [1, 1]) == 1

assert expected_game_duration(2, [1, 2]) == 3

assert expected_game_duration(5, [0, 0, 0, 0, 35]) == 0


Prompt:
def dangerous_triples(n: int, m: int, dislikes: List[Tuple[int, int]], q: int, revisions: List[int]) -> List[int]:
    """
    Konrad is a Human Relations consultant working for VoltModder, a large electrical equipment producer. Today, he has been tasked with evaluating the level of happiness in the company.

    There are $n$ people working for VoltModder, numbered from $1$ to $n$. Each employee earns a different amount of money in the company — initially, the $i$-th person earns $i$ rubles per day.

    On each of $q$ following days, the salaries will be revised. At the end of the $i$-th day, employee $v_i$ will start earning $n+i$ rubles per day and will become the best-paid person in the company. The employee will keep his new salary until it gets revised again.

    Some pairs of people don't like each other. This creates a great psychological danger in the company. Formally, if two people $a$ and $b$ dislike each other and $a$ earns more money than $b$, employee $a$ will brag about this to $b$. A dangerous triple is a triple of three employees $a$, $b$ and $c$, such that $a$ brags to $b$, who in turn brags to $c$. If $a$ dislikes $b$, then $b$ dislikes $a$.

    At the beginning of each day, Konrad needs to evaluate the number of dangerous triples in the company. Can you help him do it?

    -----Input-----

    The first line contains two integers $n$ and $m$ ($1 \le n \le 100\,000$, $0 \le m \le 100\,000$) — the number of employees in the company and the number of pairs of people who don't like each other. Each of the following $m$ lines contains two integers $a_i$, $b_i$ ($1 \le a_i, b_i \le n$, $a_i \neq b_i$) denoting that employees $a_i$ and $b_i$ hate each other (that is, $a_i$ dislikes $b_i$ and $b_i$ dislikes $a_i$). Each such relationship will be mentioned exactly once.

    The next line contains an integer $q$ ($0 \le q \le 100\,000$) — the number of salary revisions. The $i$-th of the following $q$ lines contains a single integer $v_i$ ($1 \le v_i \le n$) denoting that at the end of the $i$-th day, employee $v_i$ will earn the most.

    -----Output-----

    Output $q + 1$ integers. The $i$-th of them should contain the number of dangerous triples in the company at the beginning of the $i$-th day.

    -----Examples-----
    Input
    4 5
    1 2
    2 4
    1 3
    3 4
    2 3
    2
    2
    3

    Output
    4
    3
    2

    Input
    3 3
    1 2
    2 3
    1 3
    5
    1
    2
    2
    1
    3

    Output
    1
    1
    1
    1
    1
    1

    -----Note-----

    Consider the first sample test. The $i$-th row in the following image shows the structure of the company at the beginning of the $i$-th day. A directed edge from $a$ to $b$ denotes that employee $a$ brags to employee $b$. The dangerous triples are marked by highlighted edges. [Image]
    """


Prompt:
def check_reducible_anagrams(s: str, q: int, queries: List[Tuple[int, int]]) -> List[str]:
    """
    Let's call two strings $s$ and $t$ anagrams of each other if it is possible to rearrange symbols in the string $s$ to get a string, equal to $t$.

    Let's consider two strings $s$ and $t$ which are anagrams of each other. We say that $t$ is a reducible anagram of $s$ if there exists an integer $k \ge 2$ and $2k$ non-empty strings $s_1, t_1, s_2, t_2, \dots, s_k, t_k$ that satisfy the following conditions:

    If we write the strings $s_1, s_2, \dots, s_k$ in order, the resulting string will be equal to $s$;  If we write the strings $t_1, t_2, \dots, t_k$ in order, the resulting string will be equal to $t$;  For all integers $i$ between $1$ and $k$ inclusive, $s_i$ and $t_i$ are anagrams of each other.

    If such strings don't exist, then $t$ is said to be an irreducible anagram of $s$. Note that these notions are only defined when $s$ and $t$ are anagrams of each other.

    For example, consider the string $s = $ "gamegame". Then the string $t = $ "megamage" is a reducible anagram of $s$, we may choose for example $s_1 = $ "game", $s_2 = $ "gam", $s_3 = $ "e" and $t_1 = $ "mega", $t_2 = $ "mag", $t_3 = $ "e":

    [Image]

    On the other hand, we can prove that $t = $ "memegaga" is an irreducible anagram of $s$.

    You will be given a string $s$ and $q$ queries, represented by two integers $1 \le l \le r \le |s|$ (where $|s|$ is equal to the length of the string $s$). For each query, you should find if the substring of $s$ formed by characters from the $l$-th to the $r$-th has at least one irreducible anagram.

    -----Input-----

    The first line contains a string $s$, consisting of lowercase English characters ($1 \le |s| \le 2 \cdot 10^5$).

    The second line contains a single integer $q$ ($1 \le q \le 10^5$)  — the number of queries.

    Each of the following $q$ lines contain two integers $l$ and $r$ ($1 \le l \le r \le |s|$), representing a query for the substring of $s$ formed by characters from the $l$-th to the $r$-th.

    -----Output-----

    For each query, print a single line containing "Yes" (without quotes) if the corresponding substring has at least one irreducible anagram, and a single line containing "No" (without quotes) otherwise.

    -----Examples-----
    Input
    aaaaa
    3
    1 1
    2 4
    5 5

    Output
    Yes
    No
    Yes

    Input
    aabbbbbbc
    6
    1 2
    2 4
    2 2
    1 9
    5 7
    3 5

    Output
    No
    Yes
    Yes
    Yes
    No
    No

    -----Note-----

    In the first sample, in the first and third queries, the substring is "a", which has itself as an irreducible anagram since two or more non-empty strings cannot be put together to obtain "a". On the other hand, in the second query, the substring is "aaa", which has no irreducible anagrams: its only anagram is itself, and we may choose $s_1 = $ "a", $s_2 = $ "aa", $t_1 = $ "a", $t_2 = $ "aa" to show that it is a reducible anagram.

    In the second query of the second sample, the substring is "abb", which has, for example, "bba" as an irreducible anagram.
    """

def check(candidate):
    assert candidate('aaaaa', 3, [(1, 1), (2, 4), (5, 5)]) == ['Yes', 'No', 'Yes']
    assert candidate('aabbbbbbc', 6, [(1, 2), (2, 4), (2, 2), (1, 9), (5, 7), (3, 5)]) == ['No', 'Yes', 'Yes', 'Yes', 'No', 'No']
    assert candidate('f', 1, [(1, 1)]) == ['Yes']
check(check_reducible_anagrams)

assert check_reducible_anagrams('aaaaa', 3, [(1, 1), (2, 4), (5, 5)]) == ['Yes', 'No', 'Yes']