TeamsCode Spring 2025 Novice Division
A. Lily Pads
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Unfortunately, the sample output is not "NO". If that were the case, it would be impossible to AC at all!

Two frogs, Thomas and Waymo, are attempting to cross a pond.

Currently, they are both at the starting shore of the pond. In the pond, there are $$$N$$$ rows of unoccupied lily pads. In each row, there are two lily pads, a left lily pad and a right lily pad. One of them is afloat, and one of them is underwater. Also, each lily pad can hold at most one frog.

A frog may jump from the starting shore to an unoccupied and afloat lily pad in the first row, or from a lily pad in one row to an unoccupied and afloat lily pad in the next row, or from a lily pad in the last row to the ending shore.

When a frog jumps from a lily pad, that lily pad goes underwater, and the other lily pad in its row goes afloat. If a frog jumps from the right lily pad in a row, the right lily pad will go underwater, and the left lily pad will go afloat, and vice versa. Also, the frogs are polite. Thus, when one frog is mid-jump, the other frog will not jump.

Thomas and Waymo will jump in any order, obeying the above rules, until they both have made it to the ending shore. When a frog lands on a lily pad on the right, it gains a point. What is the maximum number of points total that Waymo and Thomas can gain?

Input

The first line contains $$$N$$$ ($$$1 \le N \le 100$$$), the number of rows of lily pads.

The second line contains a string $$$s$$$ with characters $$$s_1s_2 \dots s_n$$$. If $$$s_i = L$$$, the left lily pad on the $$$i$$$-th row is currently afloat, and if $$$s_i = R$$$, the right lily pad is currently afloat.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-20$$$ satisfy no additional constraints.

Output

Output the maximum number of points that Waymo and Thomas can earn.

Example
Input
1
R
Output
1
Note

A possible jumping order which gains 1 point is:

- Thomas jumps to row $$$1$$$. Thomas lands on the afloat right lily pad and gains a point.

- Thomas jumps to the ending shore. Now, in row $$$1$$$, the right lily pad is underwater, and the left lily pad is afloat.

- Waymo jumps to row $$$1$$$. Waymo lands on the now afloat left lily pad but does not gain a point.

- Waymo jumps to the ending shore. Now, in row $$$1$$$, the left lily pad is underwater, and the right lily pad is afloat.

After both frogs have reached the ending shore, they have gained $$$1$$$ point total.

Problem Idea: Alex_C

Problem Preparation: jay_jayjay

Occurrences: Novice A, Advanced A

B. Sacrifice The Rook
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are a GothamChess subscriber. Playing a game of chess, you are nearing the end. It is your turn; you have a king and a rook, and your opponent has a king.

As every GothamChess subscriber knows, in this position, you need to sacrifice THE ROOOOOOOK!!!!!!!!

To sacrifice the rook, you must make a move such that the opponent's king can legally capture the rook on the next move. (Thus accepting the sacrifice)

Can you sacrifice the rook in one move?

Rules of Chess:

A chessboard is an $$$8$$$ by $$$8$$$ grid of squares. The columns are files, labeled $$$a$$$ – $$$h$$$. The rows are ranks, labeled $$$1$$$ – $$$8$$$. Kings and rooks are referred to as pieces, and they each occupy a square on the board. The location of a piece can be specified by the file, then the rank.

Each piece is controlled by one of the players. Specifically, in our problem, you control a king and a rook, and the opponent controls a king. On a player's turn, they select one of their pieces and move it.

A king can move one square horizontally, vertically, or diagonally. If it lands on an opponent's piece, it can capture it, but it may not land on an allied piece. Also, it may not move into check. (That is, it cannot move into a square where it could be captured on the opponent's turn.)

A rook can move any number of squares horizontally or vertically, but cannot move through other pieces. If it lands on an opponent's piece, it can capture it. It may not land on an allied piece.

Additional Constraints:

It is guaranteed that the positions are valid. The opponent's king is not under attack. That is, you will not be able to capture your opponent's king on your turn.

Castling is not possible in this position (understanding castling is unnecessary for solving this problem).

Input

Each test contains multiple test cases. The first line of the input is $$$T (1 \leq T \leq 10^4)$$$, the number of test cases.

$$$T$$$ lines follow, containing a test case. Each test case contains the location of your king, your rook, and your opponent's king, in that order.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-20$$$ satisfy no additional constraints.

Output

For each test case, output 'YES' if you can sacrifice the rook in one move, or 'NO' if you can't, on separate lines. The checker is case sensitive, so make sure to capitalize your output!

Example
Input
6
c5 e2 f4
c3 d5 h4
f2 g2 h1
b4 d6 f3
d2 e6 f1
d4 b4 f4
Output
YES
YES
YES
NO
NO
NO
Note

These pieces are your king, your rook, and your opponent's king, from left to right.

In the first test case, you can move your rook to e4. Your opponent's king is legally able to capture your rook.

In the second test case, you can move your rook to g5. Your opponent's king is legally able to capture your rook.

In the third test case, you can move your king to f3. Your opponent's king is legally able to capture your rook.

In the fourth test case, you are unable to move your rook to a position that can be captured by the opponent's king.

In the fifth test case, you can move your rook to e2, but your opponent can't capture it because if they do, then your king would be able to capture their king on the next turn, and kings are not allowed to make moves that would allow them to get captured. Similar logic with moving your rook to e1.

In the sixth test case, your king blocks your rook. Thus, you are unable to move your rook to a position that can be captured by the opponent's king.

Problem Idea: Alex_C

Problem Preparation: nyctivoe + Alex_C

Occurrences: Novice B

C. Fill the World with Argon
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Unlike BAPC, Teamscode has enough funds to fill not just the gym, but the entire world with argon.

The world is represented by a grid of size $$$N \times M$$$. To help with filling the world with argon, you are given a 2D map, where $$$a_{i,j}$$$ represents the height of each of the cell in the $$$i$$$-th row and $$$j$$$-th column. All heights are distinct.

When you pour argon on a cell, it becomes filled with argon. Argon then flows from a filled cell to any of its adjacent cells (up, down, left, right) that have a lower height, causing them to fill with argon too. This filling process continues recursively until no further cells can be filled from that drop.

Determine the minimum number of cells you need to pour argon on to fill the entire world with argon.

Input

The first line contains two integers $$$N$$$, $$$M$$$ ($$$1 \le N, M \le 1000$$$) – the number of rows and columns, respectively, in $$$a$$$.

Then $$$N$$$ lines follow, the $$$i$$$-th line containing $$$M$$$ integers $$$a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$$$ ($$$0 \le a_{i, j} \le 10^9$$$), representing the height of the cell in the $$$i$$$-th row and $$$j$$$-th column.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-20$$$ satisfy no additional constraints.

Output

One integer – the minimum number of cells you need to pour argon on to fill the entire world.

Example
Input
2 3
1 2 3
4 5 6
Output
1
Note

For the example test case, you can pour argon on the cell with height $$$6$$$.

This cell is next to cells with lower height $$$5$$$ and $$$3$$$, so those cells would get filled too.

The cell with height $$$5$$$ is next to cells with lower height $$$4$$$ and $$$2$$$, so those cells would get filled too.

The cell with height $$$4$$$ is next to the cell with lower height $$$1$$$, so that cell would get filled too.

After the pour, all the cells are filled with argon. Thus, only one pour is necessary.

Problem Idea: Alex_C

Problem Preparation: n685

Occurrences: Novice C

D. Please solve this in O(N^3)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an undirected graph with $$$N$$$ nodes and $$$M$$$ edges. Each node is assigned a bitmask $$$A$$$ (where $$$1 \leq A \leq 2^{30} - 1$$$). A node is said to 'contain' color $$$c$$$ (with $$$0 \leq c \leq 29$$$) if the bit in the $$$2^c$$$ place in the binary representation of $$$A$$$ is $$$1$$$. This expression using bitwise operators would evaluate to true if and only if $$$A$$$ contains the color $$$c$$$.

$$$$$$ (A \ \& \ (1 \ll c)) == (1 \ll c) $$$$$$

A path between two nodes $$$i$$$ and $$$j$$$ is considered valid if there exists at least one color $$$c$$$ such that every node along the path (including $$$i$$$ and $$$j$$$) contains color $$$c$$$.

Your task is to determine, for every node $$$i$$$, which nodes $$$j$$$ are reachable from $$$i$$$ via some valid path. Output an $$$N \times N$$$ matrix of characters, where the $$$j$$$th character in the $$$i$$$th row is '1' if there exists at least one valid path from node $$$i$$$ to node $$$j$$$, and '0' otherwise.

Input

The first line contains two integers $$$N (1 \leq N \leq 500)$$$ and $$$M(0 \leq M \leq \frac{N \times (N - 1)}{2})$$$, the number of nodes and the number of edges.

The second line contains $$$N$$$ integers, where the $$$i$$$th integer is the bitmask $$$A_i (1 \leq A_i \leq 2^{30} - 1)$$$ for node $$$i$$$.

The following $$$M$$$ lines each contain two integers $$$u$$$ and $$$v$$$ representing an undirected edge between nodes $$$u$$$ and $$$v$$$. Nodes are numbered from $$$1$$$ to $$$N$$$. It is guaranteed there are no duplicate edges or self loops.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Test $$$1$$$ satisfies $$$M = 0$$$.

Test $$$2$$$ satisfies $$$M = \frac{N \times (N - 1)}{2}$$$.

Tests $$$3-6$$$ satisfy $$$A_i = 1$$$.

Tests $$$7-20$$$ satisfy no additional constraints.

Output

Output $$$N$$$ lines. The $$$i$$$th line should be a string of $$$N$$$ characters. The $$$j$$$th character in this string should be '1' if there exists a valid path from node $$$i$$$ to node $$$j$$$, and '0' otherwise.

Problem Idea: Alex_C

Problem Preparation: nyctivoe

Occurrences: Novice D

Examples
Input
5 5
1 2 3 4 5
1 3
2 3
3 5
5 4
2 4
Output
10101
01100
11101
00011
10111
Input
10 13
3 5 2 7 1 8 6 9 10 4
1 2
1 3
2 4
3 4
4 5
1 4
2 3
6 8
8 9
9 7
7 10
8 10
4 7
Output
1111101010
1101101001
1011001010
1111101011
1101100000
0000010110
1111001011
0000010110
1011011110
0101001001

E. Mingle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are in the Squid Game! As the music turns on, you hear a command from the guards.

"The game you will be playing is Mingle. You must form groups of 2 and go into the rooms."

The room has $$$n$$$ players and $$$m$$$ pairs of friends. Two players $$$u$$$ and $$$v$$$ can team up if either of the following conditions are met:

  • $$$u$$$ and $$$v$$$ are friends
  • $$$u$$$ and $$$v$$$ share a mutual friend

Time is ticking! Being the hero, your goal is to pair as many people up as possible.

Input

The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 2 \cdot 10^{5}$$$).

The following $$$m$$$ lines contain two integers, $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), denoting that players $$$u$$$ and $$$v$$$ are friends with each other.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-20$$$ satisfy no additional constraints.

Output

The first line of output should be the maximum number of pairs $$$k$$$ that we can form.

The following $$$k$$$ lines should contain two numbers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$), denoting that players $$$u$$$ and $$$v$$$ are pairing up. Each player can be present in at most one pair.

If there are multiple possible pairings such that we can form the maximum number of pairs, output any.

Examples
Input
3 2
1 2
1 3
Output
1
2 3
Input
6 7
5 3
2 1
5 1
3 2
1 3
5 4
2 6
Output
3
3 1
5 4
2 6
Input
5 2
1 2
3 4
Output
2
1 2
3 4
Note

In sample 1, we can pair up players $$$2$$$ and $$$3$$$ because they share a mutual friend $$$1$$$.

In sample 3, we can pair players $$$1$$$ and $$$2$$$, along with players $$$3$$$ and $$$4$$$, because they are friends.

Problem Idea: eysbutno

Problem Preparation: eysbutno

Occurrences: Novice E

F. Keys
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

To pass the time, Bessie the cow and her friend Elsie like to play a version of a game they saw at the county fair.

To start, Bessie puts three inverted shells on a table and places a small round pebble under one of them. Bessie then proceeds to swap pairs of shells, while Elsie tries to guess the location of the pebble.

But Elsie has gotten too good at this game. So Bessie summons a floating grid of $$$2^n \times 2^n$$$ keys. The rows are labeled $$$0 \dots 2^n - 1$$$, and the columns are labeled $$$0 \dots 2^n - 1$$$.

Bessie then points to $$$m$$$ of the keys and tells Elsie to keep track of the keys.

Afterwards, Bessie uses her super cow powers to perform $$$k$$$ operations. In each operation, she selects $$$x, y$$$. Then, the grid of keys is partitioned into subgrids with $$$2^x$$$ rows and $$$2^y$$$ columns. Then, she rotates each subgrid $$$180$$$ degrees and joins the subgrids back together. After performing all $$$k$$$ operations, she asks Elsie to guess the locations of the keys she pointed to at the beginning.

Elsie could not keep up with Bessie's super cow powers. Help Elsie determine the locations of the keys that Bessie pointed to.

Input

The first line contains $$$n \ (1 \leq n \leq 60)$$$, $$$m \ (1 \leq m \leq 5 \cdot 10^5)$$$, and $$$k \ (1 \leq k \leq 5 \cdot 10^5)$$$ – the grid size, the number of keys Bessie points to, and the number of operations.

The next $$$m$$$ lines contain $$$r \ (0 \leq r \leq 2^n-1)$$$ and $$$c \ (0 \leq c \leq 2^n-1)$$$ – the row and column of the keys that Bessie points to.

The next $$$k$$$ lines contain $$$x \ (0 \leq x \leq n)$$$ and $$$y \ (0 \leq y \leq n)$$$ – the $$$x$$$ and $$$y$$$ values for each of Bessie's operations.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-6$$$ satisfy $$$m \leq 1000$$$, and $$$k \leq 1000$$$.

Tests $$$7-8$$$ satisfy $$$n \leq 30$$$.

Tests $$$9-20$$$ satisfy no additional constraints.

Output

$$$m$$$ lines; each line contains $$$r'$$$ and $$$c'$$$, denoting the new row and column of each key after Bessie uses her super cow powers, in the same order that Bessie pointed to the keys.

Example
Input
2 1 4
1 2
0 1
1 1
2 0
2 2
Output
0 1
Note

FOCUS

Problem Idea: culver0412

Problem Preparation: Jasonwei08

Occurrences: Novice F

G. Path on Big Grid
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Consider a grid $$$c$$$ with $$$n$$$ rows and $$$m$$$ columns. The top-left corner of $$$c$$$ has coordinates $$$(0, 0)$$$, and the bottom-right corner has coordinates $$$(n-1, m-1)$$$.

The cell with coordinates $$$(i, j)$$$ of $$$c$$$ has cost $$$c_{i, j}$$$.

From $$$c$$$, create a big grid $$$C$$$ with $$$N\cdot n$$$ rows and $$$M\cdot m$$$ columns. The top-left corner of $$$C$$$ has coordinates $$$(0, 0)$$$, and the bottom-right corner has coordinates $$$(N\cdot n-1, M\cdot m-1)$$$.

The cell with coordinates $$$(i, j)$$$ of $$$C$$$ has cost $$$c_{\left \lfloor \frac{i}{N} \right \rfloor, \left \lfloor \frac{j}{M} \right\rfloor}$$$. $$$^{\text{∗}}$$$

A path through $$$C$$$ starts at $$$(0, 0)$$$, ends at $$$(Nn-1, Mm-1)$$$, and moves only right or down. The cost of the path is the sum of the costs of the cells visited.

What is the minimum cost of a path through the big grid $$$C$$$?

$$$^{\text{∗}}$$$$$$\lfloor x \rfloor$$$ is the floor function. It is the smallest integer that is less than or equal to $$$x$$$. For instance, $$$\lfloor 2.4 \rfloor = 2$$$, and $$$\lfloor 2 \rfloor = 2$$$

Input

The first line contains four integers, $$$n$$$, $$$m$$$, $$$N$$$, $$$M$$$. $$$(1 \leq n, m, N, M \leq 1000)$$$.

Then $$$n$$$ lines follow, each with $$$m$$$ numbers, $$$(1 \leq c_{i, j} \leq 1000)$$$ – the cost of the cells in $$$c$$$.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Test $$$1-2$$$ satisfies $$$n = 1$$$ and $$$m = 1$$$.

Test $$$3-7$$$ satisfies $$$N = 1$$$ and $$$M = 1$$$.

Tests $$$8-20$$$ satisfy no additional constraints.

Output

One integer – the minimum cost of a path through the big grid $$$C$$$.

Example
Input
2 3 3 3
1 7 10
2 100 1
Output
41
Note

In the sample input, the costs of each cell in $$$c$$$ look as follows.

The costs of each cell in $$$C$$$ look as follows.

A path with minimal cost is indicated by the green cells below.

The cost is the sum of the costs of all cells in the path, which is $$$1 + 1 + 1 + 1 + 1 + 7 + 7 + 7 + 10 + 1 + 1 + 1 + 1 + 1 = 41$$$.

Problem Idea: Alex_C

Problem Preparation: Alex_C

Occurrences: Novice G

H. Fibocchi Sequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bocchi has recently learned about the Fibonacci sequence (or as she likes to call it, Fibocchi sequence), and has now proclaimed herself the master of the Fibocchi sequence!

Ryou has decided to test this by giving Bocchi the $$$2$$$ starting numbers, $$$a$$$ and $$$b$$$, of the sequence $$$f_k$$$ and asking her to generate $$$n$$$ terms of the sequence. More formally, Ryou wants Bocchi to generate the sequence $$$f_k$$$ defined by the recurrence relation $$$$$$f_1 = a; f_2 = b; f_k = f_{k - 1} + f_{k - 2} \, (2 \lt k \leq n).$$$$$$

But to really test if Bocchi is a Fibocchi master, she has added a twist! She wants Bocchi to answer $$$q$$$ queries regarding the sequence after performing some number of operations on it. An operation is defined by adding $$$1$$$ to $$$f_k$$$ for some $$$k$$$ $$$(1\leq k\leq n)$$$, then recalculating the recurrence starting from index $$$k + 1$$$.

For example, if $$$f = [2, 1, 3, 4, 7]$$$, Bocchi can apply an operation with $$$k = 3$$$, turning $$$f$$$ into $$$[2, 1, 4, 5, 9]$$$.

Note that if $$$k = 1$$$ is chosen, $$$f_2$$$ does not change.

Ryou will give Bocchi $$$q$$$ queries of the following form:

Given $$$a$$$ and $$$b$$$, find the minimum number of operations needed to make $$$\sum_{k = 1}^n f_k$$$ equal to $$$x$$$ or $$$-1$$$ if this is not possible.

Bocchi finds these advanced operations too difficult. Please help her prove her title as the Fibocchi master!

Input

The first line contains two integers $$$n$$$ and $$$q$$$ $$$(3 \leq n \leq 30, 1 \leq q \leq 5 \cdot 10^5)$$$.

The next $$$q$$$ lines of each test case consist of $$$3$$$ integers $$$a, b, x$$$ $$$(1 \leq a, b \leq 10, 1 \leq x \leq 5 \cdot 10^6)$$$ — the queries.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

The test numbered $$$i$$$ satisfies $$$n = \max(3, \left \lfloor \frac{3i}{2} \right \rfloor)$$$.

Output

For each query, print one integer — the minimum number of operations needed to make $$$\sum_{k = 1}^n f_k$$$ equal to $$$x$$$ or $$$-1$$$ if this is not possible.

Example
Input
4 5
1 1 7
1 1 10
2 1 14
2 1 8
2 3 31
Output
0
1
1
-1
4
Note

In sample 1, the sequence is $$$[1,1,2,3]$$$, which already adds up to $$$7$$$, so no operations are needed.

In sample 2, the sequence is $$$[1,1,2,3]$$$. After an operation on index 1, the sequence turns into $$$[2, 1, 3, 4]$$$ which has a sum of $$$10$$$.

Problem Idea: training4usaco

Problem Preparation: HaccerKat

Occurrences: Novice H

I. Cell Towers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have been hired as a Temporary Part-Time Assistant Manager of the Executive Municipal Internet Service Provider Administration!

Your job is to set up cell towers to provide internet to homes in The City.

The City can be visualized as a very large rectangular grid. The rows are numbered starting from $$$1$$$, from south to north. The columns are numbered starting from $$$1$$$, from west to east. There are also $$$n$$$ houses on this grid, denoted by $$$(r, c)$$$, where $$$r$$$ is the number of the row the house is in, and $$$c$$$ is the number of the column the house is in.

You are trying to install cell towers for the houses. However, the cell towers have a peculiar set of rules.

A cell tower can only provide signal to a right isosceles triangle that extends south and east from it. More formally, a cell tower at $$$(r, c)$$$ covers all houses $$$(r', c')$$$, such that $$$c \leq c'$$$, and $$$r' + c' \leq r + c$$$.

If a cell tower is set at $$$(3, 1)$$$, it can only provide signal to houses at $$$(3, 1), (2, 1), (1, 1), (2, 2), (1, 2), (1, 3)$$$. Here is a diagram of the range of a cell tower at $$$(3, 1)$$$:

Because the terrain gets rougher the further north you go, the cost of a cell tower is equal to the number of the row it is installed. For instance, installing a cell tower at $$$(3, 1)$$$ has a cost of $$$3$$$.

A cell tower may be installed on top of houses, and it will still provide coverage as usual (despite protests from homeowners).

The City is a bit indecisive about how many cell towers they would like to install. Thus, for every $$$i$$$ from $$$1$$$ to $$$n$$$, find the minimum cost to cover all houses with at most $$$i$$$ cell towers.

Input

The first line contains $$$n$$$, denoting the number of houses. $$$(1 \leq n \leq 2 \cdot 10^5)$$$

The next $$$n$$$ lines contain $$$r, c$$$ $$$(1 \leq r \leq 10^9), (1 \leq c \leq 10^9)$$$, denoting a coordinate of a house.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Test $$$1$$$ satisfies $$$n = 1$$$.

Tests $$$2-8$$$ satisfy $$$n \leq 1000$$$, and for each house, $$$r, c \leq 1000$$$.

Tests $$$9-20$$$ satisfy no additional constraints.

Output

Output $$$n$$$ space separated integers, the $$$i$$$th of which is the minimum cost to cover all houses with at most $$$i$$$ cell towers.

Examples
Input
2
1 1
8 8
Output
15 9 
Input
3
3 5
1 3
2 3
Output
5 5 5 
Input
5
2 6
2 3
1 3
1 4
2 9
Output
8 7 6 6 6 
Note

Problem Idea: culver0412

Problem Preparation: culver0412

Occurrences: Novice I, Advanced B

J. Walk
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the following undirected graph, count the number of walks from $$$A$$$ to $$$A$$$ that visit nodes $$$A$$$, $$$B$$$, $$$C$$$, $$$D$$$ exactly $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ times, respectively, modulo $$$998244353$$$.

Note: A walk from $$$A$$$ to $$$A$$$ is a sequence of vertices $$$v_0 = A,v_1,…,v_k = A$$$ such that for each $$$i$$$ $$$(0 \leq i \lt k)$$$, there is an edge between $$$v_i$$$ and $$$v_{i+1}$$$. Two walks are considered distinct if they are distinct as sequences.
Input

The first line contains $$$t$$$ $$$(1 \leq t \leq 100)$$$, the number of independent test cases. The description of the test cases follows.

The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$1 \leq a$$$, $$$b$$$, $$$c$$$, $$$d \leq 5 \cdot 10^5$$$).

The sum of $$$a$$$, the sum of $$$b$$$, the sum of $$$c$$$, and the sum of $$$d$$$ across all test cases does not exceed $$$5 \cdot 10^5$$$.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-20$$$ satisfy no additional constraints.

Output

For each test case, print the number of valid walks modulo $$$998244353$$$.

Example
Input
2
3 1 2 1
1 2 1 2
Output
6
0
Note

In the first test case, the $$$6$$$ walks are $$$ABDCACA$$$, $$$ACDBACA$$$, $$$ACDCABA$$$, $$$ABACDCA$$$, $$$ACABDCA$$$, and $$$ACACDBA$$$.

In the second test case, it can be shown that there are no valid walks.

Problem Idea: HaccerKat

Problem Preparation: HaccerKat

Occurrences: Novice J, Advanced C

K. Not Japanese Triangle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You encounter a triangle. The first thing you can tell about it is that it is most certainly not Japanese.

The triangle contains $$$n$$$ rows, the $$$i$$$th from top contains $$$i$$$ numbers, $$$a_{i,1}, a_{i,2} \dots a_{i,i}$$$. The numbers are not written in Japanese.

The triangle then speaks to you, but not in Japanese. It wagers $$$100$$$ points that you cannot find the sum of the numbers in each of its rows.

You confidently accept the challenge. But before you begin, the triangle puts on a not Japanese shirt, obscuring all rows from $$$1$$$ to $$$n-1$$$, and only the numbers in row $$$n$$$ remain visible.

Fortunately, the shirt has some not Japanese text that reveals an important property: Every hidden number is the minimum of two numbers directly below it. In other words, $$$a_{i,j} = min(a_{i+1,j}, a_{i+1, j+1})$$$ for all $$$1 \leq i \leq n-1$$$ and $$$1 \leq j \leq i$$$.

Can you find the sum of the not Japanese numbers in each not Japanese row of the not Japanese triangle?

Input

The first line contains one integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$.

The next line contains $$$n$$$ integers $$$a_{n,1}, a_{n,2} \dots a_{n,n}$$$ $$$(1 \leq a_{n,i} \leq 10^9)$$$, the numbers in the $$$n$$$th row of the not Japanese triangle.

Tests in subtasks are numbered from $$$1−20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-4$$$ satisfy $$$n \leq 5000$$$.

Tests $$$5-20$$$ satisfy no additional constraints.

Output

$$$n$$$ space separated integers, the $$$i$$$th integer representing the sum of the not Japanese numbers in the $$$i$$$th not Japanese row.

Examples
Input
6
1 2 1 2 2 6
Output
1 2 3 5 7 14 
Input
11
14 15 20 10 1 16 1 14 5 19 5
Output
1 2 3 4 5 6 7 21 39 58 120 
Note

In sample $$$1$$$, the rows are:

Row $$$1$$$: $$$[1]$$$, sum = $$$1$$$.

Row $$$2$$$: $$$[1, 1]$$$, sum = $$$2$$$.

Row $$$3$$$: $$$[1, 1, 1]$$$, sum = $$$3$$$.

Row $$$4$$$: $$$[1, 1, 1, 2]$$$, sum = $$$5$$$.

Row $$$5$$$: $$$[1, 1, 1, 2, 2]$$$, sum = $$$7$$$.

Row $$$6$$$: $$$[1, 2, 1, 2, 2, 6]$$$, sum = $$$14$$$.

Problem Idea: culver0412

Problem Preparation: jay_jayjay

Occurrences: Novice K, Advanced D

L. Robot Racing
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Yam is racing robots! Unfortunately, his robots are out of control, so he has to stop them before they crash.

Yam has $$$N$$$ robots, each with speed $$$a_i$$$ ($$$a_i$$$ is non-decreasing). They are all positioned at the start (i.e., position $$$0$$$) of a track of length $$$L$$$ meters. For each subarray, Yam does the following:

First, he divides the robots in the subarray into $$$k$$$ groups of robots, where each robot must belong to exactly one group, and each group has at least one robot. Every second, every robot will advance $$$a_i$$$ meters forward. Then, after every second, Yam chooses a group and shoots every robot in the group, stopping them from moving forward in the future. If any robot crosses the end of the track (i.e., its position is strictly greater than $$$L$$$), then it spontaneously combusts. Define the score of this subarray as the maximum number of groups that Yam can form while ensuring that no robot spontaneously combusts. (Yam can choose to group the robots strategically to maximize the score.)

Yam thinks this is too easy, so he challenges you to find the sum of scores over all subarrays.

Input

The first line of input contains two integers $$$N$$$ and $$$L$$$ ($$$1\le N\le 2\cdot 10^5$$$, $$$1\le L\le 10^9$$$).

The following line contains $$$N$$$ integers $$$a_1, \dots, a_n$$$, ($$$1\le a_i\le L$$$).

Tests in subtasks are numbered from $$$1-20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-3$$$ satisfy $$$N\le 200$$$.

Tests $$$4-6$$$ satisfy $$$N\le 5000$$$.

Tests $$$7-20$$$ satisfy no additional constraints.

Output

Output the sum of scores over all subarrays.

Example
Input
4 10
1 3 6 10
Output
17
Note

The subarray $$$[1]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group $$$[1]$$$

The subarray $$$[3]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group $$$[3]$$$.

The subarray $$$[6]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group $$$[6]$$$.

The subarray $$$[10]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group $$$[10]$$$.

The subarray $$$[1,3]$$$ has score $$$2$$$ $$$-$$$ Yam creates 2 groups, $$$[3]$$$, $$$[1]$$$ and shoots them in this order.

The subarray $$$[3,6]$$$ has score $$$2$$$ $$$-$$$ Yam creates 2 groups, $$$[6]$$$, $$$[3]$$$.

The subarray $$$[6,10]$$$ has score $$$1$$$ $$$-$$$ Yam creates 1 group, $$$[6, 10]$$$.

The subarray $$$[1,3,6]$$$ has score $$$3$$$ $$$-$$$ Yam creates 3 groups, $$$[6]$$$, $$$[3]$$$, $$$[1]$$$.

The subarray $$$[3,6,10]$$$ has score $$$2$$$ $$$-$$$ Yam creates 2 groups, $$$[6, 10]$$$, $$$[3]$$$.

The subarray $$$[1,3,6,10]$$$ has score $$$3$$$ $$$-$$$ Yam creates 3 groups, $$$[6, 10]$$$, $$$[3]$$$, $$$[1]$$$.

Thus, the sum of scores is $$$1 + 1 + 1 + 1 + 2 + 2 + 1 + 3 + 2 + 3 = 17$$$.

Problem Idea: Yam

Problem Preparation: jay_jayjay

Occurrences: Novice L, Advanced E