Question:
[NightHawk22 - Isolation](https://soundcloud.com/vepium/nighthawk22-isolation- official-limbo-remix)

This is the easy version of the problem. In the three versions, the constraints on $n$ and the time limit are different. You can make hacks only if all the versions of the problem are solved.

This is the statement of Problem D1B:

  * There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.     * At time $1$, you conquer exactly one city, called the starting city.      * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it.

You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win?

For each $0 \leq k \leq n$, count the number of arrays of positive integers $a_1, a_2, \ldots, a_n$ such that

  * $1 \leq a_i \leq n$ for each $1 \leq i \leq n$;    * the answer to Problem D1B is $k$.

The answer can be very large, so you have to calculate it modulo a given prime $p$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 80$). The description of the test cases follows.

The only line of each test case contains two integers $n$, $p$ ($1 \le n \le 80$, $10^8 \leq p \leq 10^9$, $p$ is prime) — the number of cities and the modulo.

It is guaranteed that the sum of $n$ over all test cases does not exceed $80$.

Output Format:
For each test case, output $n+1$ integers: the $i$-th integer should be the number of arrays that satisfy the conditions for $k = i-1$.

Tags:
['combinatorics', 'dp', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 1024 MB


Question:
[NightHawk22 - Isolation](https://soundcloud.com/vepium/nighthawk22-isolation- official-limbo-remix)

This is the medium version of the problem. In the three versions, the constraints on $n$ and the time limit are different. You can make hacks only if all the versions of the problem are solved.

This is the statement of Problem D1B:

  * There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.     * At time $1$, you conquer exactly one city, called the starting city.      * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it.

You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win?

For each $0 \leq k \leq n$, count the number of arrays of positive integers $a_1, a_2, \ldots, a_n$ such that

  * $1 \leq a_i \leq n$ for each $1 \leq i \leq n$;    * the answer to Problem D1B is $k$.

The answer can be very large, so you have to calculate it modulo a given prime $p$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.

The only line of each test case contains two integers $n$, $p$ ($1 \le n \le 500$, $10^8 \leq p \leq 10^9$, $p$ is prime) — the number of cities and the modulo.

It is guaranteed that the sum of $n$ over all test cases does not exceed $500$.

Output Format:
For each test case, output $n+1$ integers: the $i$-th integer should be the number of arrays that satisfy the conditions for $k = i-1$.

Tags:
['dp', 'greedy', 'math']

Time Limit: 10000 ms
Memory Limit: 1024 MB


Question:
[NightHawk22 - Isolation](https://soundcloud.com/vepium/nighthawk22-isolation- official-limbo-remix)

This is the hard version of the problem. In the three versions, the constraints on $n$ and the time limit are different. You can make hacks only if all the versions of the problem are solved.

This is the statement of Problem D1B:

  * There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.     * At time $1$, you conquer exactly one city, called the starting city.      * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it.

You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win?

For each $0 \leq k \leq n$, count the number of arrays of positive integers $a_1, a_2, \ldots, a_n$ such that

  * $1 \leq a_i \leq n$ for each $1 \leq i \leq n$;    * the answer to Problem D1B is $k$.

The answer can be very large, so you have to calculate it modulo a given prime $p$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 3000$). The description of the test cases follows.

The only line of each test case contains two integers $n$, $p$ ($1 \le n \le 3000$, $10^8 \leq p \leq 10^9$, $p$ is prime) — the number of cities and the modulo.

It is guaranteed that the sum of $n$ over all test cases does not exceed $3000$.

Output Format:
For each test case, output $n+1$ integers: the $i$-th integer should be the number of arrays that satisfy the conditions for $k = i-1$.

Tags:
['dp', 'greedy', 'math']

Time Limit: 10000 ms
Memory Limit: 1024 MB


Question:
[EnV - Dynasty](https://soundcloud.com/envyofficial/env-dynasty)

You are given an array $a_1, a_2, \ldots, a_n$ of positive integers.

You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $1 \leq i \leq n-1$, at least one of $a_i$ and $a_{i+1}$ must not be red).

Your score is the maximum value of a red element plus the number of red elements. Find the maximum score you can get.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the length of the array.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 1000$) — the given array.

Output Format:
For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.

Tags:
['brute force', 'dp', 'greedy']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
[Shirobon - FOX](https://soundcloud.com/shirobon/fox?in=mart_207/sets/fav)

You are given $n$ points on the $x$ axis, at increasing positive integer coordinates $x_1 < x_2 < \ldots < x_n$.

For each pair $(i, j)$ with $1 \leq i < j \leq n$, you draw the segment $[x_i, x_j]$. The segments are closed, i.e., a segment $[a, b]$ contains the points $a, a+1, \ldots, b$.

You are given $q$ queries. In the $i$-th query, you are given a positive integer $k_i$, and you have to determine how many points with integer coordinates are contained in exactly $k_i$ segments.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains two integers $n$, $q$ ($2 \le n \le 10^5$, $1 \le q \le 10^5$) — the number of points and the number of queries.

The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^9$) — the coordinates of the $n$ points.

The third line of each test case contains $q$ integers $k_1, k_2, \ldots, k_q$ ($1 \leq k_i \leq 10^{18}$) — the parameters of the $q$ queries.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $q$ over all test cases does not exceed $10^5$.

Output Format:
For each test case, output a single line with $q$ integers: the $i$-th integer is the answer to the $i$-th query.

Tags:
['implementation', 'math']

Time Limit: 1500 ms
Memory Limit: 256 MB


Question:
[DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram)

You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.

There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy at most $k$ new cards in total, and the cards you buy can contain any integer between $\mathbf{1}$ and $\mathbf{n}$, inclusive.

After buying the new cards, you must partition all your cards into decks, according to the following rules:

  * all the decks must have the same size;    * there are no pairs of cards with the same value in the same deck.

Find the maximum possible size of a deck after buying cards and partitioning them optimally.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains two integers $n$, $k$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^{16}$) — the number of distinct types of cards and the number of coins.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^{10}$, $\sum a_i \geq 1$) — the number of cards of type $i$ you have at the beginning, for each $1 \leq i \leq n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output a single integer: the maximum possible size of a deck if you operate optimally.

Tags:
['greedy', 'implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
[Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker)

There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.

  * At time $1$, you conquer exactly one city, called the starting city.    * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it.

You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win?

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the deadlines for conquering the cities.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output a single integer: the number of starting cities that allow you to win.

Tags:
['binary search', 'constructive algorithms', 'data structures', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
[t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little)

You are given a tree with $n$ nodes, rooted at node $1$. In this problem, a leaf is a non-root node with degree $1$.

In one operation, you can remove a leaf and the edge adjacent to it (possibly, new leaves appear). What is the minimum number of operations that you have to perform to get a tree, also rooted at node $1$, where all the leaves are at the same distance from the root?

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

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

Each of the next $n-1$ lines contains two integers $u$, $v$ ($1 \leq u, v \leq n$, $u \neq v$), describing an edge that connects $u$ and $v$. It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.

Output Format:
For each test case, output a single integer: the minimum number of operations needed to achieve your goal.

Tags:
['constructive algorithms', 'dfs and similar', 'dp', 'sortings', 'trees']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
[EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern)

You are given an array $a_1, a_2, \ldots, a_n$ of positive integers.

You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $1 \leq i \leq n-1$, at least one of $a_i$ and $a_{i+1}$ must not be red).

Your score is the maximum value of a red element, plus the minimum value of a red element, plus the number of red elements. Find the maximum score you can get.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the given array.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.

Tags:
['data structures', 'dp', 'dsu', 'greedy', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given two integers $n$ and $k$.

In one operation, you can subtract any power of $k$ from $n$. Formally, in one operation, you can replace $n$ by $(n-k^x)$ for any non-negative integer $x$.

Find the minimum number of operations required to make $n$ equal to $0$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The only line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 10^9$).

Output Format:
For each test case, output the minimum number of operations on a new line.

Tags:
['bitmasks', 'brute force', 'greedy', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Imagine you have $n$ light bulbs numbered $1, 2, \ldots, n$. Initially, all bulbs are on. To flip the state of a bulb means to turn it off if it used to be on, and to turn it on otherwise.

Next, you do the following:

  * for each $i = 1, 2, \ldots, n$, flip the state of all bulbs $j$ such that $j$ is divisible by $i^\dagger$.

After performing all operations, there will be several bulbs that are still on. Your goal is to make this number exactly $k$.

Find the smallest suitable $n$ such that after performing the operations there will be exactly $k$ bulbs on. We can show that an answer always exists.

$^\dagger$ An integer $x$ is divisible by $y$ if there exists an integer $z$ such that $x = y\cdot z$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The only line of each test case contains a single integer $k$ ($1 \le k \le 10^{18}$).

Output Format:
For each test case, output $n$ — the minimum number of bulbs.

Tags:
['binary search', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are given three non-negative integers $b$, $c$, and $d$.

Please find a non-negative integer $a \in [0, 2^{61}]$ such that $(a\, |\, b)-(a\, \&\, c)=d$, where $|$ and $\&$ denote the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR) and the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND), respectively.

If such an $a$ exists, print its value. If there is no solution, print a single integer $-1$. If there are multiple solutions, print any of them.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.

The only line of each test case contains three positive integers $b$, $c$, and $d$ ($0 \le b, c, d \le 10^{18}$).

Output Format:
For each test case, output the value of $a$, or $-1$ if there is no solution. Please note that $a$ must be non-negative and cannot exceed $2^{61}$.

Tags:
['bitmasks', 'hashing', 'implementation', 'math', 'schedules', 'ternary search']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
One fine evening, Alice sat down to play the classic game "Connect the Dots", but with a twist.

To play the game, Alice draws a straight line and marks $n$ points on it, indexed from $1$ to $n$. Initially, there are no arcs between the points, so they are all disjoint. After that, Alice performs $m$ operations of the following type:

  * She picks three integers $a_i$, $d_i$ ($1 \le d_i \le 10$), and $k_i$.    * She selects points $a_i, a_i+d_i, a_i+2d_i, a_i+3d_i, \ldots, a_i+k_i\cdot d_i$ and connects each pair of these points with arcs.

After performing all $m$ operations, she wants to know the number of connected components$^\dagger$ these points form. Please help her find this number.

$^\dagger$ Two points are said to be in one connected component if there is a path between them via several (possibly zero) arcs and other points.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$).

The $i$-th of the following $m$ lines contains three integers $a_i$, $d_i$, and $k_i$ ($1 \le a_i \le a_i + k_i\cdot d_i \le n$, $1 \le d_i \le 10$, $0 \le k_i \le n$).

It is guaranteed that both the sum of $n$ and the sum of $m$ over all test cases do not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output the number of connected components.

Tags:
['brute force', 'dp', 'dsu', 'graphs', 'math', 'trees']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
You are given an array of $n$ integers $a_1,a_2,\ldots,a_n$. You are also given an array $p_1, p_2, \ldots, p_n$.

Let $S$ denote the random multiset (i. e., it may contain equal elements) constructed as follows:

  * Initially, $S$ is empty.    * For each $i$ from $1$ to $n$, insert $a_i$ into $S$ with probability $\frac{p_i}{10^4}$. Note that each element is inserted independently.

Denote $f(S)$ as the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of all elements of $S$. Please calculate the expected value of $(f(S))^2$. Output the answer modulo $10^9 + 7$.

Formally, let $M = 10^9 + 7$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 1023$).

The third line of each test case contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1 \le p_i \le 10^4$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output the expected value of $(f(S))^2$, modulo $10^9 + 7$.

Tags:
['bitmasks', 'dp', 'math', 'probabilities']

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
Let $n$ and $d$ be positive integers. We build the the divisor tree $T_{n,d}$ as follows:

  * The root of the tree is a node marked with number $n$. This is the $0$-th layer of the tree.    * For each $i$ from $0$ to $d - 1$, for each vertex of the $i$-th layer, do the following. If the current vertex is marked with $x$, create its children and mark them with all possible distinct divisors$^\dagger$ of $x$. These children will be in the $(i+1)$-st layer.    * The vertices on the $d$-th layer are the leaves of the tree.

For example, $T_{6,2}$ (the divisor tree for $n = 6$ and $d = 2$) looks like this:

![](CDN_BASE_URL/782ea6d6fe622ee5ec986b0d8cae274d)

Define $f(n,d)$ as the number of leaves in $T_{n,d}$.

Given integers $n$, $k$, and $d$, please compute $\sum\limits_{i=1}^{n} f(i^k,d)$, modulo $10^9+7$.

$^\dagger$ In this problem, we say that an integer $y$ is a divisor of $x$ if $y \ge 1$ and there exists an integer $z$ such that $x = y \cdot z$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The only line of each test case contains three integers $n$, $k$, and $d$ ($1 \le n \le 10^9$, $1 \le k,d \le 10^5$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^9$.

Output Format:
For each test case, output $\sum\limits_{i=1}^{n} f(i^k,d)$, modulo $10^9+7$.

Tags:
['dp', 'math', 'number theory']

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
Pak Chanek has an array $a$ of $n$ positive integers. Since he is currently learning how to calculate the floored average of two numbers, he wants to practice it on his array $a$.

While the array $a$ has at least two elements, Pak Chanek will perform the following three-step operation:

  1. Pick two different indices $i$ and $j$ ($1 \leq i, j \leq |a|$; $i \neq j$), note that $|a|$ denotes the current size of the array $a$.    2. Append $\lfloor \frac{a_i+a_j}{2} \rfloor$$^{\text{∗}}$ to the end of the array.    3. Remove elements $a_i$ and $a_j$ from the array and concatenate the remaining parts of the array.

For example, suppose that $a=[5,4,3,2,1,1]$. If we choose $i=1$ and $j=5$, the resulting array will be $a=[4,3,2,1,3]$. If we choose $i=4$ and $j=3$, the resulting array will be $a=[5,4,1,1,2]$.

After all operations, the array will consist of a single element $x$. Find the maximum possible value of $x$ if Pak Chanek performs the operations optimally.

$^{\text{∗}}$$\lfloor x \rfloor$ denotes the floor function of $x$, which is the greatest integer that is less than or equal to $x$. For example, $\lfloor 6 \rfloor = 6$, $\lfloor 2.5 \rfloor=2$, $\lfloor -3.6 \rfloor=-4$ and $\lfloor \pi \rfloor=3$

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 50$) — the length of the array $a$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the array $a$.

Do note that the sum of $n$ over all test cases is not bounded.

Output Format:
For each test case, output a single integer: the maximum possible value of $x$ after all numbers have been picked.

Tags:
['data structures', 'greedy', 'math', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are given an array $a$ of $n$ positive integers and an integer $x$. You can do the following two-step operation any (possibly zero) number of times:

  1. Choose an index $i$ ($1 \leq i \leq n$).    2. Increase $a_i$ by $x$, in other words $a_i := a_i + x$.

Find the maximum value of the $\operatorname{MEX}$ of $a$ if you perform the operations optimally.

The $\operatorname{MEX}$ (minimum excluded value) of an array is the smallest non-negative integer that is not in the array. For example:

  * The $\operatorname{MEX}$ of $[2,2,1]$ is $0$ because $0$ is not in the array.    * The $\operatorname{MEX}$ of $[3,1,0,1]$ is $2$ because $0$ and $1$ are in the array but $2$ is not.    * The $\operatorname{MEX}$ of $[0,3,1,2]$ is $4$ because $0$, $1$, $2$ and $3$ are in the array but $4$ is not.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.

The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 2 \cdot 10^5$; $1 \le x \le 10^9$) — the length of the array and the integer to be used in the operation.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the given array.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output a single integer: the maximum $\operatorname{MEX}$ of $a$ if you perform the operations optimally.

Tags:
['brute force', 'greedy', 'math', 'number theory']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. In the two versions, the constraints on $q$ and the time limit are different. In this version, $q=0$. You can make hacks only if all the versions of the problem are solved.

A team consisting of $n$ members, numbered from $1$ to $n$, is set to present a slide show at a large meeting. The slide show contains $m$ slides.

There is an array $a$ of length $n$. Initially, the members are standing in a line in the order of $a_1, a_2, \ldots, a_n$ from front to back. The slide show will be presented in order from slide $1$ to slide $m$. Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is $[\color{red}{3},1,2,4]$. After member $3$ presents the current slide, you can change the line of members into either $[\color{red}{3},1,2,4]$, $[1,\color{red}{3},2,4]$, $[1,2,\color{red}{3},4]$ or $[1,2,4,\color{red}{3}]$.

There is also an array $b$ of length $m$. The slide show is considered good if it is possible to make member $b_i$ present slide $i$ for all $i$ from $1$ to $m$ under these constraints.

However, your annoying boss wants to make $q$ updates to the array $b$. In the $i$-th update, he will choose a slide $s_i$ and a member $t_i$ and set $b_{s_i} := t_i$. Note that these updates are persistent, that is changes made to the array $b$ will apply when processing future updates.

For each of the $q+1$ states of array $b$, the initial state and after each of the $q$ updates, determine if the slideshow is good.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 2 \cdot 10^5$; $q=0$) — the number of members, the number of sections and the number of updates.

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$) — the initial order of the members from front to back. It is guaranteed that each integer from $1$ to $n$ appears exactly once in $a$.

The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) — the members who should present each section.

It is guaranteed that the sum of $n$ and the sum of $m$ over all test cases do not exceed $2 \cdot 10^5$ respectively.

Output Format:
For each test case, output $q+1$ lines corresponding to the $q+1$ states of the array $b$. Output "YA" if the slide show is good, and "TIDAK" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yA", "Ya", "ya", and "YA" will be recognized as positive responses.

Tags:
['constructive algorithms', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. In the two versions, the constraints on $q$ and the time limit are different. In this version, $0 \leq q \leq 2 \cdot 10^5$. You can make hacks only if all the versions of the problem are solved.

A team consisting of $n$ members, numbered from $1$ to $n$, is set to present a slide show at a large meeting. The slide show contains $m$ slides.

There is an array $a$ of length $n$. Initially, the members are standing in a line in the order of $a_1, a_2, \ldots, a_n$ from front to back. The slide show will be presented in order from slide $1$ to slide $m$. Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is $[\color{red}{3},1,2,4]$. After member $3$ presents the current slide, you can change the line of members into either $[\color{red}{3},1,2,4]$, $[1,\color{red}{3},2,4]$, $[1,2,\color{red}{3},4]$ or $[1,2,4,\color{red}{3}]$.

There is also an array $b$ of length $m$. The slide show is considered good if it is possible to make member $b_i$ present slide $i$ for all $i$ from $1$ to $m$ under these constraints.

However, your annoying boss wants to make $q$ updates to the array $b$. In the $i$-th update, he will choose a slide $s_i$ and a member $t_i$ and set $b_{s_i} := t_i$. Note that these updates are persistent, that is changes made to the array $b$ will apply when processing future updates.

For each of the $q+1$ states of array $b$, the initial state and after each of the $q$ updates, determine if the slideshow is good.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 2 \cdot 10^5$; $0 \leq q \leq 2 \cdot 10^5$) — the number of members and the number of sections.

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$) — the initial order of the members from front to back. It is guaranteed that each integer from $1$ to $n$ appears exactly once in $a$.

The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) — the members who should present each section.

Each of the next $q$ lines contains two integers $s_i$ and $t_i$ ($1 \le s_i \le m$, $1 \le t_i \le n$) — parameters of an update.

It is guaranteed that the sum of $n$, the sum of $m$ and the sum of $q$ over all test cases do not exceed $2 \cdot 10^5$ respectively.

Output Format:
For each test case, output $q+1$ lines corresponding to the $q+1$ states of the array $b$. Output "YA" if the slide show is good, and "TIDAK" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yA", "Ya", "ya", and "YA" will be recognized as positive responses.

Tags:
['constructive algorithms', 'data structures', 'greedy', 'implementation', 'sortings']

Time Limit: 5000 ms
Memory Limit: 256 MB


Question:
Pak Chanek has a friend who runs a drink stall in a canteen. His friend will sell drinks for $n$ days, numbered from day $1$ to day $n$. There are also $m$ types of drinks, numbered from $1$ to $m$.

The profit gained from selling a drink on a particular day can vary. On day $i$, the projected profit from selling drink of type $j$ is $A_{i, j}$. Note that $A_{i, j}$ can be negative, meaning that selling the drink would actually incur a loss.

Pak Chanek wants to help his friend plan the sales over the $n$ days. On day $i$, Pak Chanek must choose to sell at least one type of drink. Furthermore, the types of drinks sold on a single day must form a subarray. In other words, in each day, Pak Chanek will select $i$ and $j$ such that $1 \leq i \leq j \leq m$. Then all types of drinks between $i$ and $j$ (inclusive) will be sold.

However, to ensure that customers from the previous day keep returning, the selection of drink types sold on day $i$ ($i>1$) must meet the following conditions:

  * At least one drink type sold on day $i$ must also have been sold on day $i-1$.    * At least one drink type sold on day $i$ must not have been sold on day $i-1$.

The daily profit is the sum of the profits from all drink types sold on that day. The total profit from the sales plan is the sum of the profits over $n$ days. What is the maximum total profit that can be achieved if Pak Chanek plans the sales optimally?

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $3 \leq m \leq 2 \cdot 10^5$; $n \cdot m \leq 2 \cdot 10^5$) — the number of rows and columns in a grid.

The next $n$ lines of each test case contain $m$ integers each, where the $i$-th line contains the integers $A_{i,1} A_{i,2}, \ldots, A_{i,m}$ ($-10^9 \leq A_{i,j} \leq 10^9$) — project profits of each drink type on the $i$-th day.

It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output a single integer: the maximum profit that Pak Chanek can achieve.

Tags:
['dp', 'greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved.

Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$.

There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to.

For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.

The first line of each test case contains three integers $n$, $m$, $p$ ($2 \le n \le 400$; $n-1 \le m \le 400$; $1 \le p \le n$) — the number of houses, the number of cables and the number of houses that need internet.

The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) — the houses that need internet. It is guaranteed that all elements of $s$ are distinct.

The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) — the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph.

It is guaranteed that the sum of $n^3$ and the sum of $m^3$ do not exceed $10^8$.

Output Format:
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.

Tags:
['brute force', 'data structures', 'dfs and similar', 'dp', 'dsu', 'fft', 'graphs', 'greedy', 'implementation', 'math', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved.

Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$.

There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to.

For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2000$). The description of the test cases follows.

The first line of each test case contains three integers $n$, $m$, $p$ ($2 \le n \le 5000$; $n-1 \le m \le 5000$; $1 \le p \le n$) — the number of houses, the number of cables, and the number of houses that need internet.

The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) — the houses that need internet. It is guaranteed that all elements of $s$ are distinct.

The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) — the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph.

It is guaranteed that the sum of $n$ and the sum of $m$ do not exceed $5000$.

Output Format:
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.

Tags:
['data structures', 'dp', 'dsu', 'graphs', 'math', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the extreme version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved.

Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$.

There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to.

For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains 3 integers $n$, $m$, $p$ ($2 \le n \le 2 \cdot 10^5$; $n-1 \le m \le 2 \cdot 10^5$; $1 \le p \le n$) — the number of houses, the number of cables, and the number of houses that need internet.

The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) — the houses that need internet. It is guaranteed that all elements of $s$ are distinct.

The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) — the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph.

It is guaranteed that the sum of $n$ and the sum of $m$ do not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.

Tags:
['data structures', 'dfs and similar', 'dp', 'dsu', 'graphs', 'greedy', 'math', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Ya vamos llegando a Péeeenjamoo ♫♫♫

There are $n$ families travelling to Pénjamo to witness Mexico's largest- ever "walking a chicken on a leash" marathon. The $i$-th family has $a_i$ family members. All families will travel using a single bus consisting of $r$ rows with $2$ seats each.

A person is considered happy if:

  * Another family member is seated in the same row as them, or    * They are sitting alone in their row (with an empty seat next to them).

Determine the maximum number of happy people in an optimal seating arrangement. Note that everyone must be seated in the bus.

It is guaranteed that all family members will fit on the bus. Formally, it is guaranteed that $\displaystyle\sum_{i=1}^{n}a_i \le 2r$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

The first line of each test case contains two integers $n$ and $r$ ($1 \le n \le 100$; $1 \le r \le 500$) — the number of families and the number of rows in the bus.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10$) — the number of family members in each family.

Output Format:
For each test case, output the maximum number of happy people in an optimal seating arrangement.

Tags:
['constructive algorithms', 'greedy', 'implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Karel is a salesman in a car dealership. The dealership has $n$ different models of cars. There are $a_i$ cars of the $i$-th model. Karel is an excellent salesperson and can convince customers to buy up to $x$ cars (of Karel's choice), as long as the cars are from different models.      Determine the minimum number of customers Karel has to bring in to sell all the cars.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 5 \cdot 10^5$; $1 \le x \le 10$) — the number of different models of cars and the maximum number of cars Karel can convince a customer to buy.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the number of cars of each model.

It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.

Output Format:
For each test case, output the minimum possible number of customers needed to sell all the cars.

Tags:
['binary search', 'greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
We all steal a little bit. But I have only one hand, while my adversaries have two.

Álvaro Obregón

Álvaro and José are the only candidates running for the presidency of Tepito, a rectangular grid of $2$ rows and $n$ columns, where each cell represents a house. It is guaranteed that $n$ is a multiple of $3$.

Under the voting system of Tepito, the grid will be split into districts, which consist of any $3$ houses that are connected$^{\text{∗}}$. Each house will belong to exactly one district.

Each district will cast a single vote. The district will vote for Álvaro or José respectively if at least $2$ houses in that district select them. Therefore, a total of $\frac{2n}{3}$ votes will be cast.

As Álvaro is the current president, he knows exactly which candidate each house will select. If Álvaro divides the houses into districts optimally, determine the maximum number of votes he can get.

$^{\text{∗}}$A set of cells is connected if there is a path between any $2$ cells that requires moving only up, down, left and right through cells in the set.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains one integer $n$ ($3 \le n \le 10^5$; $n$ is a multiple of $3$) — the number of columns of Tepito.

The following two lines each contain a string of length $n$. The $i$-th line contains the string $s_i$, consisting of the characters $\texttt{A}$ and $\texttt{J}$. If $s_{i,j}=\texttt{A}$, the house in the $i$-th row and $j$-th column will select Álvaro. Otherwise if $s_{i,j}=\texttt{J}$, the house will select José.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output Format:
For each test case, output a single integer — the maximum number of districts Álvaro can win by optimally dividing the houses into districts.

Tags:
['dp', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB

{"inputs": ["4\n3\nAAA\nAJJ\n6\nJAJAJJ\nJJAJAJ\n6\nAJJJAJ\nAJJAAA\n9\nAJJJJAJAJ\nJAAJJJJJA"], "outputs": ["2\n2\n3\n2"]}


Question:
This is the easy version of the problem. In this version, you can ask at most $n+69$ questions. You can make hacks only if both versions of the problem are solved.

This is an interactive problem.

It is a tradition in Mexico's national IOI trainings to play the game "Asesino", which is similar to "Among Us" or "Mafia".

Today, $n$ players, numbered from $1$ to $n$, will play "Asesino" with the following three roles:

  * Knight: a Knight is someone who always tells the truth.    * Knave: a Knave is someone who always lies.    * Impostor: an Impostor is someone everybody thinks is a Knight, but is secretly a Knave.

Each player will be assigned a role in the game. There will be exactly one Impostor but there can be any (possible zero) number of Knights and Knaves.

As the game moderator, you have accidentally forgotten the roles of everyone, but you need to determine the player who is the Impostor.

To determine the Impostor, you will ask some questions. In each question, you will pick two players $i$ and $j$ ($1 \leq i, j \leq n$; $i \neq j$) and ask if player $i$ thinks that player $j$ is a Knight. The results of the question is shown in the table below.

| Knight| Knave| Impostor   ---|---|---|---   Knight| Yes| No| Yes   Knave| No| Yes| No   Impostor| No| Yes| —   The response of the cell in row $a$ and column $b$ is the result of asking a question when $i$ has role $a$ and $j$ has row $b$. For example, the "Yes" in the top right cell belongs to row "Knight" and column "Impostor", so it is the response when $i$ is a Knight and $j$ is an Impostor.

Find the Impostor in at most $n + 69$ questions.

Note: the grader is adaptive: the roles of the players are not fixed in the beginning and may change depending on your questions. However, it is guaranteed that there exists an assignment of roles that is consistent with all previously asked questions under the constraints of this problem.

Input Format:
The first line of input contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($3 \le n \le 10^5$) — the number of people playing the game.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output Format:

Tags:
['binary search', 'brute force', 'constructive algorithms', 'implementation', 'interactive']

Time Limit: 2500 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. In this version, you must use the minimum number of queries possible. You can make hacks only if both versions of the problem are solved.

This is an interactive problem.

It is a tradition in Mexico's national IOI trainings to play the game "Asesino", which is similar to "Among Us" or "Mafia".

Today, $n$ players, numbered from $1$ to $n$, will play "Asesino" with the following three roles:

  * Knight: a Knight is someone who always tells the truth.    * Knave: a Knave is someone who always lies.    * Impostor: an Impostor is someone everybody thinks is a Knight, but is secretly a Knave.

Each player will be assigned a role in the game. There will be exactly one Impostor but there can be any (possible zero) number of Knights and Knaves.

As the game moderator, you have accidentally forgotten the roles of everyone, but you need to determine the player who is the Impostor.

To determine the Impostor, you will ask some questions. In each question, you will pick two players $i$ and $j$ ($1 \leq i, j \leq n$; $i \neq j$) and ask if player $i$ thinks that player $j$ is a Knight. The results of the question is shown in the table below.

| Knight| Knave| Impostor   ---|---|---|---   Knight| Yes| No| Yes   Knave| No| Yes| No   Impostor| No| Yes| —   The response of the cell in row $a$ and column $b$ is the result of asking a question when $i$ has role $a$ and $j$ has row $b$. For example, the "Yes" in the top right cell belongs to row "Knight" and column "Impostor", so it is the response when $i$ is a Knight and $j$ is an Impostor.

Find the Impostor in the minimum number of queries possible. That is, let $f(n)$ be the minimum integer such that for $n$ players, there exists a strategy that can determine the Impostor using at most $f(n)$ questions. Then, you should use at most $f(n)$ questions to determine the Impostor.

Note: the grader is adaptive: the roles of the players are not fixed in the beginning and may change depending on your questions. However, it is guaranteed that there exists an assignment of roles that is consistent with all previously asked questions under the constraints of this problem.

Input Format:
The first line of input contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($3 \le n \le 10^5$) — the number of people playing the game.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output Format:

Tags:
['constructive algorithms', 'dp', 'interactive']

Time Limit: 2500 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. In this version, it is guaranteed that $q = 0$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input Format:
The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k \le 10^5$; $q = 0$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) — indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) — indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output Format:
For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Tags:
['2-sat', 'binary search', 'combinatorics', 'constructive algorithms', 'dfs and similar', 'dsu', 'graphs']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input Format:
The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output Format:
For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Tags:
['binary search', 'combinatorics', 'data structures', 'dsu', 'graphs']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
You are given $n$ arrays $a_1$, $\ldots$, $a_n$. The length of each array is two. Thus, $a_i = [a_{i, 1}, a_{i, 2}]$. You need to concatenate the arrays into a single array of length $2n$ such that the number of inversions$^{\dagger}$ in the resulting array is minimized. Note that you do not need to count the actual number of inversions.

More formally, you need to choose a permutation$^{\ddagger}$ $p$ of length $n$, so that the array $b = [a_{p_1,1}, a_{p_1,2}, a_{p_2, 1}, a_{p_2, 2}, \ldots, a_{p_n,1}, a_{p_n,2}]$ contains as few inversions as possible.

$^{\dagger}$The number of inversions in an array $c$ is the number of pairs of indices $i$ and $j$ such that $i < j$ and $c_i > c_j$.

$^{\ddagger}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of arrays.

Each of the following $n$ lines contains two integers $a_{i,1}$ and $a_{i,2}$ ($1 \le a_{i,j} \le 10^9$) — the elements of the $i$-th array.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output Format:
For each test case, output $2n$ integers — the elements of the array you obtained. If there are multiple solutions, output any of them.

Tags:
['constructive algorithms', 'greedy', 'math', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a certain parameter $b_i$ ($1 \le b_i \le n$).

Initially, the testing system gives the participant the first problem. When the participant is given the $i$-th problem, they have two options:

  * They can submit the problem and receive $a_i$ points;    * They can skip the problem, in which case they will never be able to submit it.

Then, the testing system selects the next problem for the participant from problems with indices $j$, such that:

  * If he submitted the $i$-th problem, it looks at problems with indices $j < i$;    * If he skipped the $i$-th problem, it looks at problems with indices $j \leq b_i$.

Among these problems, it selects the problem with the maximum index that it has not previously given to the participant (he has neither submitted nor skipped it before). If there is no such problem, then the competition for the participant ends, and their result is equal to the sum of points for all submitted problems. In particular, if the participant submits the first problem, then the competition for them ends. Note that the participant receives each problem at most once.

Prokhor has prepared thoroughly for the olympiad, and now he can submit any problem. Help him determine the maximum number of points he can achieve.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) — the number of problems in the olympiad.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the scores of the problems.

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq n$) — the parameters of the problems.

It is guaranteed that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the maximum number of points that Prokhor can achieve.

Tags:
['binary search', 'dp', 'graphs', 'shortest paths']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given two strongly connected$^{\dagger}$ directed graphs, each with exactly $n$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature — the length of any cycle in these graphs is divisible by $k$.

Each of the $2n$ vertices belongs to exactly one of two types: incoming or outgoing. For each vertex, its type is known to you.

You need to determine whether it is possible to draw exactly $n$ directed edges between the source graphs such that the following four conditions are met:

  * The ends of any added edge lie in different graphs.    * From each outgoing vertex, exactly one added edge originates.    * Into each incoming vertex, exactly one added edge enters.    * In the resulting graph, the length of any cycle is divisible by $k$.

$^{\dagger}$A strongly connected graph is a graph in which there is a path from every vertex to every other vertex.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$) — the number of vertices in each graph and the value by which the length of each cycle is divisible.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i \in \\{0, 1\\}$). If $a_i = 0$, then vertex $i$ of the first graph is incoming. If $a_i = 1$, then vertex $i$ of the first graph is outgoing.

The third line of each test case contains a single integer $m_1$ ($1 \le m_1 \le 5 \cdot 10^5$) — the number of edges in the first graph.

The next $m_1$ lines contain descriptions of the edges of the first graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) — an edge in the first graph leading from vertex $v_i$ to vertex $u_i$.

Next, in the same format, follows the description of the second graph.

The next line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($b_i \in \\{0, 1\\}$). If $b_i = 0$, then vertex $i$ of the second graph is incoming. If $b_i = 1$, then vertex $i$ of the second graph is outgoing.

The next line contains a single integer $m_2$ ($1 \le m_2 \le 5 \cdot 10^5$) — the number of edges in the second graph.

The next $m_2$ lines contain descriptions of the edges of the second graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) — an edge in the second graph leading from vertex $v_i$ to vertex $u_i$.

It is guaranteed that both graphs are strongly connected, and the lengths of all cycles are divisible by $k$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that the sum of $m_1$ and the sum of $m_2$ over all test cases does not exceed $5 \cdot 10^5$.

Output Format:
For each test case, output "YES" (without quotes) if it is possible to draw $n$ new edges such that all conditions are met, and "NO" (without quotes) otherwise.

You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Tags:
['constructive algorithms', 'dfs and similar', 'graphs', 'greedy', 'hashing', 'implementation', 'strings']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Recently, you received a rare ticket to the only casino in the world where you can actually earn something, and you want to take full advantage of this opportunity.

The conditions in this casino are as follows:

  * There are a total of $n$ games in the casino.    * You can play each game at most once.    * Each game is characterized by two parameters: $p_i$ ($1 \le p_i \le 100$) and $w_i$ — the probability of winning the game in percentage and the winnings for a win.    * If you lose in any game you decide to play, you will receive nothing at all (even for the games you won).

You need to choose a set of games in advance that you will play in such a way as to maximize the expected value of your winnings.

In this case, if you choose to play the games with indices $i_1 < i_2 < \ldots < i_k$, you will win in all of them with a probability of $\prod\limits_{j=1}^k \frac{p_{i_j}}{100}$, and in that case, your winnings will be equal to $\sum\limits_{j=1}^k w_{i_j}$.

That is, the expected value of your winnings will be $\left(\prod\limits_{j=1}^k \frac{p_{i_j}}{100}\right) \cdot \left(\sum\limits_{j=1}^k w_{i_j}\right)$.

To avoid going bankrupt, the casino owners have limited the expected value of winnings for each individual game. Thus, for all $i$ ($1 \le i \le n$), it holds that $w_i \cdot p_i \le 2 \cdot 10^5$.

Your task is to find the maximum expected value of winnings that can be obtained by choosing some set of games in the casino.

Input Format:
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of games offered to play.

The $i$-th of the following $n$ lines contains two integers $p_i$ and $w_i$ ($1 \leq p_i \leq 100$, $1 \leq w_i, p_i \cdot w_i \leq 2 \cdot 10^5$) — the probability of winning and the size of the winnings in the $i$-th game.

Output Format:
Output a single number — the maximum expected value of winnings in the casino that can be obtained by choosing some subset of games.

Your answer will be accepted if the relative or absolute error does not exceed $10^{-6}$. Formally, if $a$ is your answer and $b$ is the jury's answer, it will be accepted if $\frac{|a-b|}{\max(b, 1)} \le 10^{-6}$.

Tags:
['brute force', 'dp', 'greedy', 'math', 'probabilities']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
In the heart of an ancient kingdom grows the legendary Tree of Life — the only one of its kind and the source of magical power for the entire world. The tree consists of $n$ nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are $n-1$ channels in the tree, with the $i$-th channel connecting nodes $v_i$ and $u_i$. Moreover, there exists a unique simple path through the channels between any two nodes in the tree.

However, the magical energy flowing through these channels must be balanced; otherwise, the power of the Tree of Life may disrupt the natural order and cause catastrophic consequences. The sages of the kingdom discovered that when two magical channels converge at a single node, a dangerous "magical resonance vibration" occurs between them. To protect the Tree of Life and maintain its balance, it is necessary to select several paths and perform special rituals along them. A path is a sequence of distinct nodes $v_1, v_2, \ldots, v_k$, where each pair of adjacent nodes $v_i$ and $v_{i+1}$ is connected by a channel. When the sages perform a ritual along such a path, the resonance vibration between the channels $(v_i, v_{i+1})$ and $(v_{i+1}, v_{i+2})$ is blocked for each $1 \leq i \leq k - 2$.

The sages' task is to select the minimum number of paths and perform rituals along them to block all resonance vibrations. This means that for every pair of channels emanating from a single node, there must exist at least one selected path that contains both of these channels.

Help the sages find the minimum number of such paths so that the magical balance of the Tree of Life is preserved, and its power continues to nourish the entire world!

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 4 \cdot 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \leq n \leq 5 \cdot 10^5$) — the number of nodes in the Tree of Life.

The $i$-th of the following $n - 1$ lines of each test case contains two integers $v_i$ and $u_i$ ($1 \leq v_i < u_i \leq n$) — the channel connecting nodes $v_i$ and $u_i$.

It is guaranteed that there exists a unique simple path through the channels between any two nodes.

It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the minimum number of paths that the sages need to select to prevent a catastrophe.

Tags:
['dp', 'greedy', 'trees']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
In a desert city with a hilly landscape, the city hall decided to level the road surface by purchasing a dump truck. The road is divided into $n$ sections, numbered from $1$ to $n$ from left to right. The height of the surface in the $i$-th section is equal to $a_i$. If the height of the $i$-th section is greater than $0$, then the dump truck must take sand from the $i$-th section of the road, and if the height of the $i$-th section is less than $0$, the dump truck must fill the pit in the $i$-th section of the road with sand. It is guaranteed that the initial heights are not equal to $0$.

When the dump truck is in the $i$-th section of the road, it can either take away $x$ units of sand, in which case the height of the surface in the $i$-th section will decrease by $x$, or it can fill in $x$ units of sand (provided that it currently has at least $x$ units of sand in its bed), in which case the height of the surface in the $i$-th section of the road will increase by $x$.

The dump truck can start its journey from any section of the road. Moving to an adjacent section on the left or right takes $1$ minute, and the time for loading and unloading sand can be neglected. The dump truck has an infinite capacity and is initially empty.

You need to find the minimum time required for the dump truck to level the sand so that the height in each section becomes equal to $0$. Note that after all movements, the dump truck may still have sand left in its bed. You need to solve this problem independently for the segments numbered from $l_i$ to $r_i$. Sand outside the segment cannot be used.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

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

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$, $a_i \neq 0$) — the initial height in each section.

The $i$-th of the following $q$ lines contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) — the boundaries of the segment of sections for which the minimum time needs to be determined.

It is guaranteed that the sum of $n$ over all test cases and the sum of $q$ over all test cases do not exceed $3 \cdot 10^5$.

Output Format:
For each query, output the minimum time required to level the sand in the segment $[l_i, r_i]$, or $-1$ if it is impossible.

Tags:
['data structures', 'greedy', 'math', 'matrices']

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
Alice has $a$ coins. She can open a bank deposit called "Profitable", but the minimum amount required to open this deposit is $b$ coins.

There is also a deposit called "Unprofitable", which can be opened with any amount of coins. Alice noticed that if she opens the "Unprofitable" deposit with $x$ coins, the minimum amount required to open the "Profitable" deposit decreases by $2x$ coins. However, these coins cannot later be deposited into the "Profitable" deposit.

Help Alice determine the maximum number of coins she can deposit into the "Profitable" deposit if she first deposits some amount of coins (possibly $0$) into the "Unprofitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

A single line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$) — the number of coins Alice has and the initial minimum amount required to open the "Profitable" deposit.

Output Format:
For each test case, output a single integer — the maximum number of coins that Alice can deposit into the "Profitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.

Tags:
['greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
There is a vending machine that sells lemonade. The machine has a total of $n$ slots. You know that initially, the $i$-th slot contains $a_i$ cans of lemonade. There are also $n$ buttons on the machine, each button corresponds to a slot, with exactly one button corresponding to each slot. Unfortunately, the labels on the buttons have worn off, so you do not know which button corresponds to which slot.

When you press the button corresponding to the $i$-th slot, one of two events occurs:

  * If there is a can of lemonade in the $i$-th slot, it will drop out and you will take it. At this point, the number of cans in the $i$-th slot decreases by $1$.   * If there are no cans of lemonade left in the $i$-th slot, nothing will drop out.

After pressing, the can drops out so quickly that it is impossible to track from which slot it fell. The contents of the slots are hidden from your view, so you cannot see how many cans are left in each slot. The only thing you know is the initial number of cans in the slots: $a_1, a_2, \ldots, a_n$.

Determine the minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade.

Note that you can adapt your strategy during the button presses based on whether you received a can or not. It is guaranteed that there are at least $k$ cans of lemonade in total in the machine. In other words, $k \leq a_1 + a_2 + \ldots + a_n$.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \leq k \leq 10^9$) — the number of slots in the machine and the required number of cans of lemonade.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the number of cans in the slots.

It is guaranteed that $k \leq a_1 + a_2 + \ldots + a_n$, meaning there are at least $k$ cans of lemonade in the machine.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade.

Tags:
['binary search', 'constructive algorithms', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are given $n$ arrays $a_1$, $\ldots$, $a_n$. The length of each array is two. Thus, $a_i = [a_{i, 1}, a_{i, 2}]$. You need to concatenate the arrays into a single array of length $2n$ such that the number of inversions$^{\dagger}$ in the resulting array is minimized. Note that you do not need to count the actual number of inversions.

More formally, you need to choose a permutation$^{\ddagger}$ $p$ of length $n$, so that the array $b = [a_{p_1,1}, a_{p_1,2}, a_{p_2, 1}, a_{p_2, 2}, \ldots, a_{p_n,1}, a_{p_n,2}]$ contains as few inversions as possible.

$^{\dagger}$The number of inversions in an array $c$ is the number of pairs of indices $i$ and $j$ such that $i < j$ and $c_i > c_j$.

$^{\ddagger}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of arrays.

Each of the following $n$ lines contains two integers $a_{i,1}$ and $a_{i,2}$ ($1 \le a_{i,j} \le 10^9$) — the elements of the $i$-th array.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output Format:
For each test case, output $2n$ integers — the elements of the array you obtained. If there are multiple solutions, output any of them.

Tags:
['constructive algorithms', 'greedy', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a certain parameter $b_i$ ($1 \le b_i \le n$).

Initially, the testing system gives the participant the first problem. When the participant is given the $i$-th problem, they have two options:

  * They can submit the problem and receive $a_i$ points;    * They can skip the problem, in which case they will never be able to submit it.

Then, the testing system selects the next problem for the participant from problems with indices $j$, such that:

  * If he submitted the $i$-th problem, it looks at problems with indices $j < i$;    * If he skipped the $i$-th problem, it looks at problems with indices $j \leq b_i$.

Among these problems, it selects the problem with the maximum index that it has not previously given to the participant (he has neither submitted nor skipped it before). If there is no such problem, then the competition for the participant ends, and their result is equal to the sum of points for all submitted problems. In particular, if the participant submits the first problem, then the competition for them ends. Note that the participant receives each problem at most once.

Prokhor has prepared thoroughly for the olympiad, and now he can submit any problem. Help him determine the maximum number of points he can achieve.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) — the number of problems in the olympiad.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the scores of the problems.

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq n$) — the parameters of the problems.

It is guaranteed that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the maximum number of points that Prokhor can achieve.

Tags:
['constructive algorithms', 'data structures', 'divide and conquer', 'dp', 'flows', 'graphs', 'greedy', 'implementation', 'shortest paths']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given two strongly connected$^{\dagger}$ directed graphs, each with exactly $n$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature — the length of any cycle in these graphs is divisible by $k$.

Each of the $2n$ vertices belongs to exactly one of two types: incoming or outgoing. For each vertex, its type is known to you.

You need to determine whether it is possible to draw exactly $n$ directed edges between the source graphs such that the following four conditions are met:

  * The ends of any added edge lie in different graphs.    * From each outgoing vertex, exactly one added edge originates.    * Into each incoming vertex, exactly one added edge enters.    * In the resulting graph, the length of any cycle is divisible by $k$.

$^{\dagger}$A strongly connected graph is a graph in which there is a path from every vertex to every other vertex.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$) — the number of vertices in each graph and the value by which the length of each cycle is divisible.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i \in \\{0, 1\\}$). If $a_i = 0$, then vertex $i$ of the first graph is incoming. If $a_i = 1$, then vertex $i$ of the first graph is outgoing.

The third line of each test case contains a single integer $m_1$ ($1 \le m_1 \le 5 \cdot 10^5$) — the number of edges in the first graph.

The next $m_1$ lines contain descriptions of the edges of the first graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) — an edge in the first graph leading from vertex $v_i$ to vertex $u_i$.

Next, in the same format, follows the description of the second graph.

The next line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($b_i \in \\{0, 1\\}$). If $b_i = 0$, then vertex $i$ of the second graph is incoming. If $b_i = 1$, then vertex $i$ of the second graph is outgoing.

The next line contains a single integer $m_2$ ($1 \le m_2 \le 5 \cdot 10^5$) — the number of edges in the second graph.

The next $m_2$ lines contain descriptions of the edges of the second graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) — an edge in the second graph leading from vertex $v_i$ to vertex $u_i$.

It is guaranteed that both graphs are strongly connected, and the lengths of all cycles are divisible by $k$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that the sum of $m_1$ and the sum of $m_2$ over all test cases does not exceed $5 \cdot 10^5$.

Output Format:
For each test case, output "YES" (without quotes) if it is possible to draw $n$ new edges such that all conditions are met, and "NO" (without quotes) otherwise.

You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Tags:
['constructive algorithms', 'graphs', 'hashing', 'strings']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Recently, you received a rare ticket to the only casino in the world where you can actually earn something, and you want to take full advantage of this opportunity.

The conditions in this casino are as follows:

  * There are a total of $n$ games in the casino.    * You can play each game at most once.    * Each game is characterized by two parameters: $p_i$ ($1 \le p_i \le 100$) and $w_i$ — the probability of winning the game in percentage and the winnings for a win.    * If you lose in any game you decide to play, you will receive nothing at all (even for the games you won).

You need to choose a set of games in advance that you will play in such a way as to maximize the expected value of your winnings.

In this case, if you choose to play the games with indices $i_1 < i_2 < \ldots < i_k$, you will win in all of them with a probability of $\prod\limits_{j=1}^k \frac{p_{i_j}}{100}$, and in that case, your winnings will be equal to $\sum\limits_{j=1}^k w_{i_j}$.

That is, the expected value of your winnings will be $\left(\prod\limits_{j=1}^k \frac{p_{i_j}}{100}\right) \cdot \left(\sum\limits_{j=1}^k w_{i_j}\right)$.

To avoid going bankrupt, the casino owners have limited the expected value of winnings for each individual game. Thus, for all $i$ ($1 \le i \le n$), it holds that $w_i \cdot p_i \le 2 \cdot 10^5$.

Your task is to find the maximum expected value of winnings that can be obtained by choosing some set of games in the casino.

Input Format:
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of games offered to play.

The $i$-th of the following $n$ lines contains two integers $p_i$ and $w_i$ ($1 \leq p_i \leq 100$, $1 \leq w_i, p_i \cdot w_i \leq 2 \cdot 10^5$) — the probability of winning and the size of the winnings in the $i$-th game.

Output Format:
Output a single number — the maximum expected value of winnings in the casino that can be obtained by choosing some subset of games.

Your answer will be accepted if the relative or absolute error does not exceed $10^{-6}$. Formally, if $a$ is your answer and $b$ is the jury's answer, it will be accepted if $\frac{|a-b|}{\max(b, 1)} \le 10^{-6}$.

Tags:
['dp', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
There are two screens which can display sequences of uppercase Latin letters. Initially, both screens display nothing.

In one second, you can do one of the following two actions:

  * choose a screen and an uppercase Latin letter, and append that letter to the end of the sequence displayed on that screen;    * choose a screen and copy the sequence from it to the other screen, overwriting the sequence that was displayed on the other screen.

You have to calculate the minimum number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.

Input Format:
The first line contains one integer $q$ ($1 \le q \le 500$) — the number of test cases.

Each test case consists of two lines. The first line contains the string $s$, and the second line contains the string $t$ ($1 \le |s|, |t| \le 100$). Both strings consist of uppercase Latin letters.

Output Format:
For each test case, print one integer — the minimum possible number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.

Tags:
['binary search', 'greedy', 'strings', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 512 MB

{"inputs": ["3\nGARAGE\nGARAGEFORSALE\nABCDE\nAABCD\nTRAINING\nDRAINING"], "outputs": ["14\n10\n16"]}


Question:
Recently, akshiM met a task that needed binomial coefficients to solve. He wrote a code he usually does that looked like this:

                  for (int n = 0; n < N; n++) { // loop over n from 0 to N-1 (inclusive)               C[n][0] = 1;               C[n][n] = 1;               for (int k = 1; k < n; k++) // loop over k from 1 to n-1 (inclusive)                   C[n][k] = C[n][k - 1] + C[n - 1][k - 1];           }

Unfortunately, he made an error, since the right formula is the following:

                          C[n][k] = C[n - 1][k] + C[n - 1][k - 1]

But his team member keblidA is interested in values that were produced using the wrong formula. Please help him to calculate these coefficients for $t$ various pairs $(n_i, k_i)$. Note that they should be calculated according to the first (wrong) formula.

Since values $C[n_i][k_i]$ may be too large, print them modulo $10^9 + 7$.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of pairs. Next, $t$ pairs are written in two lines.

The second line contains $t$ integers $n_1, n_2, \dots, n_t$ ($2 \le n_i \le 10^5$).

The third line contains $t$ integers $k_1, k_2, \dots, k_t$ ($1 \le k_i < n_i$).

Output Format:
Print $t$ integers $C[n_i][k_i]$ modulo $10^9 + 7$.

Tags:
['combinatorics', 'dp', 'math']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
There's a new game Monocarp wants to play. The game uses a deck of $n$ cards, where the $i$-th card has exactly one integer $a_i$ written on it.

At the beginning of the game, on the first turn, Monocarp can take any card from the deck. During each subsequent turn, Monocarp can take exactly one card that has either the same number as on the card taken on the previous turn or a number that is one greater than the number on the card taken on the previous turn.

In other words, if on the previous turn Monocarp took a card with the number $x$, then on the current turn he can take either a card with the number $x$ or a card with the number $x + 1$. Monocarp can take any card which meets that condition, regardless of its position in the deck.

After Monocarp takes a card on the current turn, it is removed from the deck.

According to the rules of the game, the number of distinct numbers written on the cards that Monocarp has taken must not exceed $k$.

If, after a turn, Monocarp cannot take a card without violating the described rules, the game ends.

Your task is to determine the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 200\,000$) — the number of cards in the deck and the maximum number of distinct numbers that can be written on the cards that Monocarp takes.

The second line contains a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$), where $a_i$ is the number written on the $i$-th card.

Additional constraint of the input: the sum of $n$ over all test cases doesn't exceed $200\,000$.

Output Format:
For each test case, print the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.

Tags:
['binary search', 'brute force', 'greedy', 'implementation', 'sortings', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
Imagine a game where you play as a character that has two attributes: "Strength" and "Intelligence", that are at zero level initially.

During the game, you'll acquire $m$ attribute points that allow you to increase your attribute levels — one point will increase one of the attributes by one level. But sometimes, you'll encounter a so-called "Attribute Checks": if your corresponding attribute is high enough, you'll pass it; otherwise, you'll fail it.

Spending some time, you finally prepared a list which contains records of all points you got and all checks you've met. And now you're wondering: what is the maximum number of attribute checks you can pass in a single run if you'd spend points wisely?

Note that you can't change the order of records.

Input Format:
The first line contains two integers $n$ and $m$ ($1 \le m \le 5000$; $m < n \le 2 \cdot 10^6$) — the number of records in the list and the total number of points you'll get during the game.

The second line contains $n$ integers $r_1, r_2, \dots, r_n$ ($-m \le r_i \le m$), where $r_i$ encodes the $i$-th record:

  * If $r_i = 0$, then the $i$-th record is an acquiring one attribute point. You can spend to level up either Strength or Intelligence;    * If $r_i > 0$, then it's an Intelligence check: if your Intelligence level is greater than or equal to $|r_i|$, you pass.    * If $r_i < 0$, then it's a Strength check: if your Strength level is greater than or equal to $|r_i|$, you pass.

Additional constraint on the input: the sequence $r_1, r_2, \dots, r_n$ contains exactly $m$ elements equal to $0$.

Output Format:
Print one integer — the maximum number of checks you can pass.

Tags:
['brute force', 'data structures', 'dp', 'implementation', 'math', 'two pointers']

Time Limit: 2500 ms
Memory Limit: 512 MB


Question:
In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the deck for each combination of suit and rank.

A card with suit $a$ and rank $b$ can beat a card with suit $c$ and rank $d$ in one of two cases:

  * $a = 1$, $c \ne 1$ (a card of suit $1$ can beat a card of any other suit);    * $a = c$, $b > d$ (a card can beat any other card of the same suit but of a lower rank).

Two players play the game. Before the game starts, they receive exactly half of the deck each. The first player wins if for every card of the second player, he can choose his card that can beat it, and there is no card that is chosen twice (i. e. there exists a matching of the first player's cards with the second player's cards such that in each pair the first player's card beats the second player's card). Otherwise, the second player wins.

Your task is to calculate the number of ways to distribute the cards so that the first player wins. Two ways are considered different if there exists a card such that in one way it belongs to the first player and in the other way it belongs to the second player. The number of ways can be very large, so print it modulo $998244353$.

Input Format:
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 500$).

Additional constraint on the input: $m$ is even.

Output Format:
Print a single integer — the number of ways to distribute the cards so that the first player wins, taken modulo $998244353$.

Tags:
['combinatorics', 'dp', 'fft', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
You are given an array $a$, consisting of $n$ integers (numbered from $1$ to $n$). Initially, they are all zeroes.

You have to process $q$ queries. The $i$-th query consists of two different integers $x_i$ and $y_i$. During the $i$-th query, you have to choose an integer $p$ (which is either $x_i$ or $y_i$) and an integer $d$ (which is either $1$ or $-1$), and assign $a_p = a_p + d$.

After each query, every element of $a$ should be a non-negative integer.

Process all queries in such a way that the sum of all elements of $a$ after the last query is the minimum possible.

Input Format:
The first line contains two integers $n$ and $q$ ($2 \le n \le 3 \cdot 10^5$; $1 \le q \le 3 \cdot 10^5$) — the number of elements in $a$ and the number of queries, respectively.

Then $q$ lines follow. The $i$-th of these lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$) — the description of the $i$-th query.

Output Format:
For each query, print a line containing two characters:

  * the first character should be x if you choose $p=x_i$, or y if you choose $p=y_i$;    * the second character should be + if you choose $d=1$, or - if you choose $d=-1$.

If there are multiple answers, print any of them.

Tags:
['constructive algorithms', 'dfs and similar', 'dp', 'graphs', 'greedy', 'trees']

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
Monocarp is gathering an army to fight a dragon in a videogame.

The army consists of two parts: the heroes and the defensive artifacts. Each hero has one parameter — his health. Each defensive artifact also has one parameter — its durability.

Before the battle begins, Monocarp distributes artifacts to the heroes so that each hero receives at most one artifact.

The battle consists of rounds that proceed as follows:

  * first, the dragon deals damage equal to $\frac{1}{a + b}$ (a real number without rounding) to each hero, where $a$ is the number of heroes alive and $b$ is the number of active artifacts;    * after that, all heroes with health $0$ or less die;    * finally, some artifacts are deactivated. An artifact with durability $x$ is deactivated when one of the following occurs: the hero holding the artifact either dies or receives $x$ total damage (from the start of the battle). If an artifact is not held by any hero, it is inactive from the beginning of the battle.

The battle ends when there are no heroes left alive.

Initially, the army is empty. There are $q$ queries: add a hero with health $x$ or an artifact with durability $y$ to the army. After each query, determine the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.

Input Format:
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries.

In the $i$-th of the following $q$ lines, there are two integers $t_i$ and $v_i$ ($t_i \in \\{1, 2\\}$; $1 \le v_i \le 10^9$) — the type of the query and the value of the query parameter. If the type is $1$, a hero with health $v_i$ is added. If the type is $2$, an artifact with durability $v_i$ is added.

Output Format:
Print $q$ integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.

Tags:
['data structures', 'flows']

Time Limit: 5000 ms
Memory Limit: 512 MB


Question:
You are given a coordinate plane and three integers $X$, $Y$, and $K$. Find two line segments $AB$ and $CD$ such that

  1. the coordinates of points $A$, $B$, $C$, and $D$ are integers;    2. $0 \le A_x, B_x, C_x, D_x \le X$ and $0 \le A_y, B_y, C_y, D_y \le Y$;    3. the length of segment $AB$ is at least $K$;    4. the length of segment $CD$ is at least $K$;    5. segments $AB$ and $CD$ are perpendicular: if you draw lines that contain $AB$ and $CD$, they will cross at a right angle.

Note that it's not necessary for segments to intersect. Segments are perpendicular as long as the lines they induce are perpendicular.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 5000$) — the number of test cases. Next, $t$ cases follow.

The first and only line of each test case contains three integers $X$, $Y$, and $K$ ($1 \le X, Y \le 1000$; $1 \le K \le 1414$).

Additional constraint on the input: the values of $X$, $Y$, and $K$ are chosen in such a way that the answer exists.

Output Format:
For each test case, print two lines. The first line should contain $4$ integers $A_x$, $A_y$, $B_x$, and $B_y$ — the coordinates of the first segment.

The second line should also contain $4$ integers $C_x$, $C_y$, $D_x$, and $D_y$ — the coordinates of the second segment.

If there are multiple answers, print any of them.

Tags:
['constructive algorithms', 'geometry', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given a strip divided into cells, numbered from left to right from $0$ to $10^{18}$. Initially, all cells are white.

You can perform the following operation: choose two white cells $i$ and $j$, such that $i \ne j$ and $|i - j| \le k$, and paint them black.

A list $a$ is given. All cells from this list must be painted black. Additionally, at most one cell that is not in this list can also be painted black. Your task is to determine the minimum value of $k$ for which this is possible.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$).

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 < a_i < 10^{18}$; $a_i < a_{i + 1}$).

Additional constraint on the input: the sum of $n$ across all test cases does not exceed $2000$.

Output Format:
For each test case, print a single integer — the minimum value of $k$ for which it is possible to paint all the given cells black.

Tags:
['binary search', 'brute force', 'constructive algorithms', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB