OCPC 2023 Fall, Day 5: Jatin Yadav Contest
A. Sole Occupancy
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given $$$N$$$ of sticks. The length of the $$$i$$$-th stick is denoted by $$$L_i$$$. You have to arrange each of these sticks horizontally on a line of cells in the range $$$[1, \infty ]$$$. It can happen that two or more sticks overlap with each other, so a cell can be occupied by more than one stick.

You have to answer $$$Q$$$ queries.

For every query, you are given two integers $$$R$$$ and $$$K$$$, you have to answer with YES if and only if there exists an arrangement of the sticks such that all the below conditions are satisfied:

  • Any stick of length $$$P$$$ occupies $$$P$$$ consecutive cells in the range $$$[1, R]$$$ (both inclusive).
  • There are exactly $$$K$$$ cells in the range $$$[1,R]$$$ (both inclusive) that are occupied by a single stick.

Else answer with NO.

Input
  • The first line contains $$$T$$$, the number of testcases. Then the testcases follow, each consisting of $$$2 + Q$$$ lines.
  • The first line of each testcase contains two space separated integers denoting $$$N, Q$$$.
  • The second line of each testcase contains $$$N$$$ space separated integers denoting $$$L_1, L_2, \ldots L_N$$$
  • The next $$$Q$$$ lines of each testcase contains two space separated integers denoting $$$R,K$$$.

Constraints

In all inputs,

  • $$$1 \le T \le 1000$$$
  • $$$1 \le N, Q \le 3 \cdot 10^5$$$
  • $$$1 \le L_i \le 10^9$$$
  • $$$\displaystyle \max_{i=1}^{N} L_i \le R \le \sum_{i=1}^{N} L_i$$$
  • $$$0 \le K \le R$$$
  • The sum of $$$N$$$ over all test cases doesn't exceed $$$3 \cdot 10^5$$$
  • The sum of $$$Q$$$ over all test cases doesn't exceed $$$3 \cdot 10^5$$$
Output

For each testcase, print either "YES" or "NO" (without quotes). Note that the answer is case-insensitive.

Example
Input
2
5 3
1 2 3 4 5
15 15
7 2
8 6
3 2
1 3 1
4 0
5 3
Output
YES
YES
NO
NO
YES
Note

Below is a possible arrangement for the second query of the second test case.

B. Grid Construction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You want to create a grid with $$$\le 500$$$ rows and $$$\le 500$$$ columns, and then color each cell of the grid either red or blue.

Let's say your grid contains $$$n$$$ rows numbered $$$1, 2, \ldots n$$$ from top to bottom and $$$m$$$ columns numbered $$$1, 2, \ldots m$$$ from left to right. Let $$$(i, j)$$$ denote the cell with row number $$$i$$$ and column number $$$j$$$.

Consider a path from top left to bottom right $$$$$$(x_1 = 1, y_1 = 1), (x_2, y_2), \ldots (x_{n+m - 2}, y_{n+m - 2}), (x_{n + m - 1} = n, y_{n + m - 1} = m),$$$$$$ that goes either right or down, that is, for every $$$1 \lt i \le n + m - 1$$$, either $$$(x_i, y_i) = (x_{i-1}, y_{i-1} + 1)$$$ or $$$(x_i, y_i) = (x_{i-1}+1, y_{i-1})$$$. We call such a path almost red if at max $$$1$$$ of the $$$n + m - 1$$$ cells on the path is colored blue. Similarly, we such a path almost blue if at max one of the $$$n + m - 1$$$ cells on the path is colored red.

Your task is to construct a grid and its coloring such that:

  • There are exactly $$$P$$$ almost red paths from top left to bottom right going either right or down.
  • There are exactly $$$Q$$$ almost blue paths from top left to bottom right going either right or down.
Input
  • The first line contains $$$T$$$, the number of testcases. Then the testcases follow.
  • Each testcase consists of a single line containing two space separated integers $$$P$$$ and $$$Q$$$.

Constraints:

  • $$$1 \le T \le 10$$$
  • $$$0 \le P, Q \le 10^{18}$$$
Output

For each testcase, first print the number of rows and the number of columns, $$$n$$$ and $$$m$$$ on a new line. Then, print $$$n$$$ lines. The $$$i-$$$th line should contain a string describing the $$$i-$$$th row of the grid. The $$$j-$$$th character of this string should be R if the cell $$$(i, j)$$$ is colored red and B otherwise.

Example
Input
2
1 1
0 5
Output
2 2
RB
RB
3 4
BBBR
RBRR
BBBB
Note

In the output of the first testcase, there are two paths, the first being almost red and the second being almost blue:

  1. $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2)$$$
  2. $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2)$$$

In the output of the second testcase, there are no almost red paths. There are $$$5$$$ almost blue paths as required:

  1. $$$(1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4)$$$
  2. $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4)$$$
  3. $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4)$$$
  4. $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4)$$$
  5. $$$(1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4)$$$

C. MST problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a graph $$$G$$$ with $$$A = \sum_{i=1}^{n} a_i$$$ vertices, numbered $$$1, 2, \ldots A$$$. For each $$$1 \leq i \leq n$$$, there are $$$a_i$$$ vertices with type $$$i$$$. The vertices numbered $$$(\sum_{j=1}^{i-1} a_j) + r$$$ are of type $$$a_i$$$ for each $$$r \in \{1, 2, \ldots a_i\}$$$. That is, the first $$$a_1$$$ vertices have type $$$1$$$, the next $$$a_2$$$ vertices have type $$$2$$$, and so on.

The edges of the graph are described through $$$m$$$ 4-tuples. Each tuple is of the form $$$t \text{ } u \text{ } v \text{ } w$$$, where $$$t \in \{1,2\}$$$, and $$$u \neq v$$$:

  • If $$$t = 1$$$, then $$$1 \leq u, v \leq A$$$. There is an edge of weight $$$w$$$ between vertices numbered $$$u$$$ and $$$v$$$.
  • If $$$t = 2$$$, then $$$1\leq u, v \leq n$$$. For any pair of vertices $$$(x, y)$$$ of the graph where $$$x$$$ is of type $$$u$$$ and $$$y$$$ is of type $$$v$$$, there is an edge of weight $$$w$$$.

Please note that there might be multiple edges between the same pair of vertices in the graph.

Find the cost of the minimum spanning tree of the graph, or print $$$-1$$$ if the graph is disconnected.

Input

The first line contains two integers $$$n (1 \leq n \leq 10^5)$$$ and $$$m(1 \leq m \leq 2 \cdot 10^5$$$)

The second line contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots a_n$$$, where $$$1 \leq a_i \leq 10^9$$$ and the sum of all $$$a_i$$$ does not exceed $$$10^9$$$.

Each of the next $$$m$$$ lines contains a $$$4-$$$tuple, of the form $$$t \text{ } u \text{ } v \text{ } w$$$

Output

Print the cost of the minimum spanning tree of the graph. If the graph is disconnected, print $$$-1$$$ instead.

Example
Input
3 4
2 3 1
2 1 2 5
2 2 3 7
1 1 6 3
1 1 2 10
Output
23
Note

The graph $$$G$$$ is the following:

The minimum spanning tree is the following:

D. Tournament
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A tournament is a directed graph in which for every unordered pair of vertices $$$(i, j)$$$ where $$$i \neq j$$$, there is either an edge from $$$i$$$ to $$$j$$$ or an edge from $$$j$$$ to $$$i$$$, but not both.

Call a vertex $$$u$$$ in a tournament good, if for every other vertex $$$v$$$, there exists a path from $$$u$$$ to $$$v$$$ with at max $$$3$$$ edges. Your task is to find a good vertex. There is a hidden tournament with $$$n$$$ vertices numbered $$$0, 1, \ldots n - 1$$$, and the only way you can extract information about it is by asking queries of the form (without quotes):

"? i j"

For each such query, the judge replies 1 if there is an edge from $$$i$$$ to $$$j$$$ in the tournament and 0 otherwise. After making some queries (limit on the number of queries specified later), you should output(without quotes):

"! v"

where $$$v$$$ is any good vertex. It can be proved that a good vertex always exists. If there exist multiple good vertices, you can output any of them.

Note that the grader is adaptive, which means that the graph is not fixed before the interaction begins. However, the judge will never contradict the replies given in the past. For example, if the judge has replied 1 for a query "? i j" in the past, then for every future query "? i j", the judge will reply 1, and for every future query "? j i", the judge will reply 0

Interaction

First read an integer $$$T(1 \leq T \leq 2000)$$$, which means the number of test cases. Each test case should be considered independently. For each test case,

  • First read an integer $$$n (1 \leq n \leq 2000)$$$
  • Then, whenever you want to know if there is an edge from $$$i$$$ to $$$j$$$, you should output "? i j", without quotes. The judge replies 1 if there is an edge from $$$i$$$ to $$$j$$$ in the tournament and 0 otherwise.
  • When you have found a good vertex, say $$$v$$$, you should output "! v", without quotes, and then immediately move to the next test case if any.

The sum of $$$n$$$ over all test cases doesn't exceed $$$2000$$$. Your solution is accepted if you output a good vertex for every test case, and the sum of the number of queries asked over all the testcases doesn't exceed $$${\bf 15000}$$$.

Example
Input
2
3

1

0

0

1

2

0
Output


? 0 1

? 2 1

? 1 0

? 0 2

! 0

? 0 1

! 1
Note
  • Only the queries of the form "? i j" are counted towards the number of queries, when comparing with the limit on number of queries.
  • After printing a query do not forget to output the end of line and flush the output.

E. Permutation Construction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Construct a set of $$$m \leq 50$$$ permutations of $$$\{1, 2, \ldots 500\}$$$, such that for each tuple $$$(i, j, k)$$$, where $$$1 \leq i, j, k \leq 500$$$, with $$$i \neq j, j \neq k$$$ and $$$k \neq i$$$, there exists a permutation in the set that contains $$$(i, j, k)$$$ as a subsequence.

Input

There is no input for this problem.

Output

On the first line print $$$m(1 \leq m \leq 50)$$$, the number of permutations you want to print. Print a permutation of $$$\{1, 2, \ldots 500\}$$$ on each of the next $$$m$$$ lines.

F. Paint It Black
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You have a strip with $$$n$$$ cells, numbered $$$1, 2, \ldots n$$$, each colored either black or white. You have a paint brush of length $$$k$$$, and a cost array $$$c_1, c_2, \ldots c_{n-k+1}$$$. You repeat the following step until all cells are black:

  • Choose a random index $$$i$$$ from $$$\{1, 2, \ldots n - k + 1\}$$$, and use the brush to paint all the cells $$$i, i + 1, \ldots i + k - 1 $$$ black. Add $$$c_i$$$ to the cost.

What is the total expected cost?

Input

The first line contains two integers $$$n (1 \leq n \leq 4000)$$$ and $$$k(1 \leq k \leq n$$$).

The second line contains a string of length $$$n$$$ consisting of W and B, describing the initial colors of the cells $$$1, 2, \ldots n$$$.

The third line contains $$$n - k + 1$$$ space separated integers $$$c_1, c_2, \ldots c_{n-k+1}$$$.

Output

Print a single line, containing the expected cost modulo $$$998244353$$$. It can be proved that the expected cost is rational, and can be written in the form $$$\frac{P}{Q}$$$, where $$$Q \neq 0 \pmod {998244353}$$$

Example
Input
4 2
WBWW
3 1 1
Output
499122184
Note

The expected value of the cost is $$$15/2$$$

G. Longest strictly increasing subsequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of $$$N$$$ integers $$$A_1, A_2, \ldots A_N$$$. For each $$$k$$$ from $$$0$$$ to $$$N$$$ inclusive, find the length of the longest possible strictly increasing subsequence of the array if you are allowed to change at max $$$k$$$ values of the array to any integer (in particular, you're allowed to change to a zero or negative integer as well).

A sequence $$$X$$$ is a subsequence of a sequence $$$Y$$$ if $$$X$$$ can be obtained from $$$Y$$$ by deleting several (possibly zero) elements. A sequence is strictly increasing if every element in it is greater than the previous element. In particular, a sequence of length $$$1$$$ is strictly increasing.

Input
  • The first line contains $$$T$$$, the number of testcases. Then the testcases follow, each consisting of two lines.
  • The first line of each testcase contains two space separated integers $$$N$$$ and $$$K$$$.
  • The second line of each testcase contains $$$N$$$ space separated integers $$$A_1, A_2, \ldots A_N$$$.

Constraints

  • $$$1 \le T \le 1000$$$
  • $$$1 \le N \le 4000$$$
  • The sum of $$$N$$$ over all testcases doesn't exceed $$$4000$$$
  • $$$1 \le A_i \le 10^7$$$
Output

For each testcase, print for each $$$k$$$ from $$$0$$$ to $$$N$$$ inclusive, the length of the longest possible strictly increasing subsequence of the array if you are allowed to change at max $$$k$$$ values of the array to any integer.

Example
Input
2
4
1 2 1 3
4
1 1 1 1
Output
3 3 4 4 4 
1 2 3 4 4 

H. Lexicographically Minimum Euler Tour
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rooted tree with $$$n$$$ vertices numbered $$$1, 2, .. n$$$, where every edge has a character on it. You want to find an euler tour, such that the string formed by the concatenation of the characters on the edges is minimized. Formally, let $$$c(x, y)$$$ denote the character written on the edge $$$(x, y)$$$. Initialize an empty string $$$S$$$, and order the children of each node arbitrarily and then run dfs(1):

dfs(s):
for v in children(s):
append c(s, v) to S
dfs(v)
append c(s, v) to S

In the end, you get a string $$$S$$$ of length $$$2n - 2$$$. What is the lexicographically minimum $$$S$$$ that you can achieve?

Input

The first line contains $$$n (2 \leq n \leq 5 \cdot 10^5)$$$, the number of vertices in the tree. The $$$i^{th}$$$ of the next $$$n - 1$$$ lines contains two integers, $$$p_i$$$ and $$$c_i$$$, where $$$p_i$$$ is the parent of the node $$$i + 1$$$, and $$$c_i$$$ is the character write on the edge between $$$i + 1$$$ and $$$p_i$$$ respectively. Here $$$1 \leq p_i \lt i$$$ and $$$c_i$$$ is a lower case english alphabet.

Output

Print the lexicographically minimum possible $$$S$$$.

Example
Input
5
1 a
2 c
2 b
1 a
Output
aaabbcca
Note

The tree is the following:

The optimal euler tour is $$$(1, 5, 1, 2, 4, 2, 3, 2, 1)$$$

I. Tree Matching
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tree with $$$N$$$ vertices numbered $$$1, 2, \ldots N$$$. The $$$i-$$$th vertex has a color $$$C_i$$$. Each edge of the tree has some positive weight. Let's define the value of a pair $$$(x, y)$$$ of distinct vertices $$$x$$$ and $$$y$$$ as the sum of the weights of the edges that appear on the unique simple path between $$$x$$$ and $$$y$$$. Let's call such a pair valid if $$$x$$$ and $$$y$$$ have different colors. You want to select some ($$$\geq 0$$$) valid pairs of vertices, such that each vertex is contained in at max one pair, and the sum of values of all the selected pairs is maximized. Print this maximum value.

Input
  • The first line contains $$$T$$$, the number of testcases. Then the testcases follow:
    • The first line of each testcase contains a single integer $$$N$$$
    • The next line contains the colors of the nodes $$$C_1, C_2, \ldots C_N$$$
    • Each of the next $$$N - 1$$$ lines contains $$$3$$$ integers, $$$u, v$$$ and $$$w$$$, denoting an edge between $$$u$$$ and $$$v$$$ of weight $$$w$$$.

Constraints

  • $$$1 \le T \le 500$$$
  • $$$1 \le N \le 2000$$$
  • $$$1 \le C_i \le N$$$
  • $$$1 \le w \le 10^5$$$
  • The sum of $$$N$$$ over all testcases doesn't exceed $$$2000$$$
Output

For each testcase, print the answer on a new line.

Example
Input
2
4
1 1 2 2
1 2 3
2 3 1
2 4 5
2
1 1
1 2 1
Output
9
0

J. Permutation Reconstruction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a hidden permutation $$$P$$$ of $$$\{1, 2, \ldots n\}$$$. In one query, you can output a permutation $$$Q$$$ of $$$\{1, 2, \ldots n\}$$$ and in return get the value:

$$$$$$ \sum_{i=1}^{n} |P_i - Q_i| $$$$$$

You can ask at most $$$1300$$$ queries. Recover the permutation $$$P$$$.

Interaction

First, read an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$). Then, you can output a permutation $$$Q$$$ of $$$\{1, 2, \ldots n\}$$$ at most $$$1301$$$ times (It is $$$1301$$$ instead of $$$1300$$$ because the last query is not counted, as you will see later). The judge returns its sum of absolute differences when compared with $$$P$$$.

The judge returns with an ok verdict when $$$Q = P$$$ (or equivalently, the return value is $$$0$$$). Else, if $$$Q$$$ is never equal to $$$P$$$, you get a wrong answer verdict.

Example
Input
4

4

6

0

Output

1 2 3 4

2 3 4 1

2 3 1 4
Note
  • The grader is not adaptive, which means that the permutation $$$P$$$ is fixed before the interaction begins.
  • After printing a query do not forget to output the end of line and flush the output.
  • Once you get a response $$$0$$$ from the judge, the judge exits with an accepted verdict, and hence you should also exit your program.