GPL 2023 Advanced
A. Dividing Data
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Maya wants to organize the training data for her machine-learning model. There are $$$N$$$ files that she can use, and each file has a size of $$$s_i$$$ bits. Her computer has a limited amount of storage, so she can only allocate $$$X$$$ bits toward storing all of the training data.

What is the maximum number of files that she can use for her model?

Input

Line 1: Two integers, $$$N$$$ and $$$X$$$. Lines 2 ... $$$N+1$$$: 1 space-separated integer $$$s_i$$$ representing the $$$i$$$th file's size in bits.

Output

Line 1: One integer representing the maximum number of files that Maya can use.

Examples
Input
5 34
14
25
47
11
6
Output
3
Input
6 18
2
5
6
4
13
1
Output
5
Note

$$$1 ≤ N ≤ 100,000$$$

$$$1 ≤ s_i ≤ 10^9$$$

$$$1 ≤ X ≤ 10^9$$$

B. Speedrun Splits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Tanner is speedrunning the hit video game The Legend of Zelda: Tears of the Kingdom, where his goal is to beat the game as fast as possible. To keep track of his progress, he creates splits for each important objective in the run.

Tanner wants to know his improvement between runs for certain splits. Given $$$N$$$ different runs composed of $$$K$$$ splits, with $$$t_i$$$ being the time in seconds for each split, for each of $$$Q$$$ different queries for a certain split $$$s$$$, please output the maximum improvement between any two times for a given split.

Input

Line $$$1$$$: Three space separated integers $$$N$$$,$$$K$$$,$$$Q$$$

Lines $$$2…N+1$$$: $$$K$$$ space separated integers $$$t_1...t_K$$$

Lines $$$N+2…N+Q+1$$$: An integer, $$$s$$$

Output

Lines $$$1…Q$$$: An integer representing the maximum improvement between any two times for split $$$s$$$.

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

$$$1 \lt N,K ≤700$$$

$$$1≤t_i≤10^9, t_i \lt t_i+1$$$

$$$1≤Q≤10^5$$$

$$$1≤s≤K$$$

For the second split, the maximum improvement is either between runs 2 and 3 or between runs 2 and 4, which results in an improvement of 4-2=2 seconds.

For the fourth split, the maximum improvement is between runs 3 and 4, which results in an improvement of 9-4=5 seconds.

For the first split, the maximum improvement is between either runs 1 and 2, between runs 2 and 3, or between runs 2 and 4, which results in an improvement of 2-1=1 second.

C. Lots of Triangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given the three-dimensional Cartesian coordinates $$$(x_i,y_i,z_i)$$$ of $$$N$$$ robots. Find the minimum area difference between two distinct triangles that can be formed by taking three of these robots as vertices.

The time given for this problem is double the usual amount (2s).

Input

Line $$$1$$$: One integer, $$$N$$$

Lines $$$2…N+1$$$: Line $$$i+1$$$ contains three ordered triplets of three integers each: $$$x_{i,1},y_{i,1},z_{i,1},x_{i,2},y_{i,2},z_{i,2},x_{i,3},y_{i,3},z_{i,3}$$$. Consecutive integers are all separated by a space. Each ordered triplet represents a vertex of the $$$i$$$th triangle. It is guaranteed that for each $$$i$$$, no two ordered triplets in line $$$i+1$$$ are equal. In other words, the triangles in this problem may not degenerate to a line segment or a single point.

Output

Line $$$1$$$: One decimal, representing the minimum area difference between two distinct triangles out of these $$$N$$$ triangles. The output should be written to exactly five decimal places and rounded.

Example
Input
4
0 0 0 0 0 1 0 1 0
1 1 5 2 4 2 0 2 5
1 0 5 0 2 3 5 1 1
4 3 3 1 3 5 1 2 5
Output
1.11270
Note

$$$2≤N≤5000$$$

$$$0≤x_i,y_i,z_i≤10^9$$$

No two ordered triplets $$$(x_i,y_i,z_i)$$$ are repeated in the input.

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

Kafka wants to cause some trouble at the Xianzhou Luofu. She is currently at the Ambrosial Arbor, which is a large tree with $$$n$$$ ($$$2 \le n \le 10^5$$$). In the tree, node $$$i$$$ has an explosion value of $$$a_i$$$ ($$$1 \le a_i \le 10^8$$$), and the edges all have weight $$$1$$$. If a simple cycle is created in the tree, it will explode with a magnitude of the sum of edge weights on the cycle. In order to carry out her terrorism, Kafka will add an edge between two nodes $$$u$$$ and $$$v$$$ with weight $$$a_u + a_v$$$, thus creating a simple cycle and exploding the tree. Note that $$$u \ne v$$$.

As a wanted terrorist, Kafka wants to cause as large an explosion as possible, so please tell her the largest magnitude explosion that she could cause by adding any edge.

Input

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$).

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^8$$$).

The third line contains $$$n - 1$$$ integers $$$p_2, p_3, \ldots, p_n$$$ ($$$1 \le p_i \lt i$$$), meaning that there is an edge between node $$$p_i$$$ and node $$$i$$$, and node $$$p_i$$$ is a parent of node $$$i$$$.

Output

The output should consist of a single integer denoting the largest magnitude of an explosion that Kafka can create.

Examples
Input
5
1 2 3 3 3
1 1 2 4
Output
10
Input
5
10 1 1 1 1
1 1 3 4
Output
14

E. AI Duck
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
An AI duck is notlikeduck
— me

Rowlet recently built an AI duck! It can't do much besides walk right now, but maybe one day its full capabilities will be realized. For now, however, it is able to walk the shortest path between any two points on a grid. To make sure the duck can walk properly, Rowlet wants it to go from point $$$(1, 1)$$$ to $$$(N, M)$$$. Now, Rowlet is interested in how many possible paths the duck might take. But the owl realized the number might be too large, so the duck was given $$$K$$$ squares it must pass through. Satisfied, Rowlet took a break and went to get some more fruit (because who doesn't love fruit).

Unfortunately, two problems arose! First, Rowlet's $$$K$$$ coordinates were corrupted, meaning that it may no longer be possible to go from $$$(1, 1)$$$ to $$$(N, M)$$$ and go through the $$$K$$$ extra squares without taking steps "backwaords" (to the left or downwards). Second, Rowlet realized that the number of possible paths is still too large. Since Rowlet is still on a fruit break (who knows how long owls take to eat fruit), you are asked to find the number of suitable paths modulo* $$$998244353$$$.

*$$$a \mod b$$$ is defined as the remainder left over when dividing $$$a$$$ by $$$b$$$. For example,

$$$2 \mod 4 = 2$$$ becuase $$$2=0*4+2$$$

$$$5 \mod 4 = 1$$$ because $$$5=1*4+1$$$

$$$10^9 \mod 998244353 = 1755647$$$ because $$$10^9=1*998244353+1755647$$$, so if the answer is $$$10^9$$$, print $$$1755647$$$ instead.

Input

The first line of input contains an integer $$$T (1 \leq T \leq 10^5)$$$, denoting the number of test cases.

Each test case contains on its first line contains three integers $$$N$$$, $$$M$$$, and $$$K$$$ $$$(1 \leq N, M \leq 10^5, 0 \leq K \leq 10^5)$$$. The following $$$K$$$ lines each contain two numbers $$$x_i$$$ $$$(1 \leq x_i \leq N)$$$ and $$$y_i$$$ $$$(1 \leq y_i \leq M)$$$, denoting the location of the $$$i$$$th incense.

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

Output

Print the number of paths modulo $$$998244353$$$.

Example
Input
4
3 5 0
3 5 1
2 3
3 5 2
3 4
2 2
3 5 2
2 2
1 4
Output
15
9
6
0
Note

In the first test case, the AI duck can take any of the $$$15$$$ paths from $$$(1, 1)$$$ to $$$(3, 5)$$$.

In the second test case, the AI duck must go through $$$(2, 3)$$$.

In the last test case, the AI duck must go through $$$(2, 2)$$$ and $$$(1, 4)$$$. However, the duck cannot reach either square from the other by only moving up and right, so there are no possible paths.

F. Silly Nilly's Stuffies
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Silly Nilly the intelligent robot has just bought a new shelf with $$$L$$$ levels and wants to fill it with stuffed animals. She currently has $$$P$$$ piles of $$$S_i$$$ stuffed animals each, but since she is a perfectionist, Silly Nilly insists that each layer of the shelf must contain the same number of stuffed animals.

Since she has limited mobility as a robot, in one operation, Silly Nilly can either remove a stuffed animal from any pile or add a stuffed animal to any pile (she has more stuffed animals outside of these piles). Determine the minimum number of operations it will take for Silly Nilly to arrange her piles so that any $$$L$$$ piles will have the same number of stuffed animals in them.

Input

Line $$$1$$$: Two integers $$$P$$$ and $$$L$$$

Line $$$2$$$: $$$P$$$ integers, $$$S_1$$$ to $$$S_P$$$, denoting the number of stuffed animals in each pile.

Output

Line $$$1$$$: An integer representing the minimum number of operations Silly Nilly must complete.

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

$$$1 \lt = P \lt = 10^5$$$

$$$1 \lt = L \lt = P$$$

$$$1 \lt = S_i \lt = 10^9$$$

G. Mysterious Maze
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Neo-Bot, an advanced pathfinder dog, is trapped in a 2D grid. He is at coordinates $$$(0, 0)$$$ and needs to get to $$$(x, y)$$$ to escape. There are also $$$M$$$ lines going from $$$(x1_i, y1_i)$$$ to $$$(x2_i, y2_i)$$$. These lines lie along the grid axes, so either both the x's or both the y's will be identical. Neo-Bot can only travel along the lines. Output how far Neo-Bot travels until he makes it to the end, or -1 if he never reaches there.

Input

Line 1: Three space-separated integers $$$X, Y, M$$$

Line 2 through $$$M+1$$$: $$$M$$$ lines of 4 space-separated integers: $$$x1_i, y1_i, x2_i, y2_i$$$. As stated above, in one of these two pairs ($$$x1_i$$$ & $$$x2_i$$$ and $$$y1_i$$$ & $$$y2_i$$$), both variables will have the same value. It is also guaranteed that at least one of these segments will pass through ($$$0, 0)$$$ and at least one of them will pass through $$$(X, Y)$$$.

Output

Line 1: One integer representing the distance Neo-Bot has to travel before he reaches his end coordinates, or -1 if he never does.

Example
Input
10 10 7
3 0 3 7
1 2 8 2
2 2 2 9
2 9 10 9
0 1 4 1
10 2 10 10
0 0 0 5
Output
22
Note

$$$1 ≤ M ≤ 150$$$

$$$1 ≤ x_i, y_i ≤ 1,000,000$$$

H. Model Evaluation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sara has trained an AI model to generate pictures of kittens given certain verbal prompts. As a very basic form of evaluation for her model, she decides to take the absolute value of the difference $$$D$$$ between the sums of all pixels in a region of the image $$$A$$$ her model generates and the real-life reference photo $$$B$$$ that she chooses. $$$A$$$ and $$$B$$$ are both square images $$$N$$$ pixels long on each side.

Given $$$Q$$$ queries consisting of two pairs of coordinates $$$(r_1,c_1)$$$ and $$$(r_2,c_2)$$$, print out the value of $$$D$$$ for the rectangular regions in $$$A$$$ and $$$B$$$ bounded by $$$(r_1,c_1)$$$ and $$$(r_2,c_2)$$$ (including $$$(r_1,c_1)$$$ and $$$(r_2,c_2)$$$).

Input

Line 1: Two space separated integers $$$N$$$,$$$Q$$$

Lines 2… $$$N+1$$$: $$$N$$$ space separated integers $$$a_1...a_N$$$, representing image $$$A$$$

Lines $$$N+2$$$... $$$2N+1$$$: $$$N$$$ space separated integers $$$b_1...b_N$$$, representing image $$$B$$$

Lines $$$2N+2$$$… $$$2N+Q+1$$$: Four space separated integers $$$r_1,c_1,r_2,c_2$$$

Output

Lines 1… $$$Q$$$: The value of $$$D$$$ for the given query

Example
Input
3 2
3 1 7
2 5 2
5 8 4
5 2 2
1 3 7
4 9 4
1 1 1 3
3 3 1 2
Output
2
0
Note

$$$1 \lt N≤800$$$

$$$1≤a_{i,j} ,b_{i,j}≤10^9$$$

$$$1≤r_1,c_1,r_2,c_2≤N$$$

$$$1 \lt Q≤70000$$$

The sum of pixel values in $$$A$$$ and $$$B$$$ in the region enclosed by $$$(1,1)$$$ and $$$(1,3)$$$ are 3+1+7=11 and 5+2+2=9 respectively, so $$$D$$$ is equal to 2.

The sum of pixel values in $$$A$$$ and $$$B$$$ in the region enclosed by $$$(3,3)$$$ and $$$(1,2)$$$ are 1+7+5+2+8+4=27 and 2+2+3+7+9+4=27 respectively, so $$$D$$$ is equal to sum of pixel values is 0.

I. Thomas the Train
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Thomas the Train, an artificial intelligence technology, is traveling between his $$$N$$$ train stations labeled $$$1$$$ to $$$N$$$, and he starts at station $$$1$$$. He knows that today, for each station $$$i$$$, one passenger will arrive at station $$$i$$$ at time $$$T_i$$$. It takes an amount of time $$$D_{i, j}$$$ to drive from station i to station j. Note that $$$D_{i, j}$$$ may differ from $$$D_{j, i}$$$.

Since Thomas is not very intelligent, Thomas travels to a station only if he will pick up a passenger there, and thus he does not take a shorter path to a station involving intermediate stations without picking up passengers there. Furthermore, the passengers are very impatient and unwilling to wait at a station for any amount of time, so Thomas must pick up passengers at the exact time at which they arrive. He can wait at a station for as long as he wants. Help Thomas determine the maximum number of passengers he can pick up today!

Input

Line $$$1$$$: An integer $$$N$$$.

Lines $$$2…N+1$$$: An integer $$$T_i$$$ denoting the time at which a passenger will arrive at station $$$i$$$.

Lines $$$N+2…2N+1$$$: A sequence of $$$N$$$ integers each denoting an amount of time $$$D_{i, j}$$$, starting with $$$D_{1, 1}$$$ through $$$D_{1, N}$$$ and ending with $$$D_{N, 1}$$$ through $$$D_{N, N}$$$.

Output

Line $$$1$$$: An integer representing the maximum number of passengers Thomas can pick up.

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

$$$1 \lt = N \lt = 500$$$

$$$0 \lt = T_i \lt = 10^6$$$

$$$1 \lt = D_{i,j} \lt = 10^6$$$ for $$$i≠j$$$

$$$D_{i,i}$$$ = 0

J. Special Word Search
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A word search is a puzzle in which the objective is to find certain words hidden within a rectangular grid of letters. Words must be in a straight line, meaning that they are horizontal, vertical, or diagonal.

A special word search is a word search that allows words to wrap around from one side of the word search to the opposite side. Like a regular word search, words must be in a straight line (horizontal, vertical, or diagonal). The same spot in the word search cannot be used in the same word twice.

For your computer science class final project, you decide to build and train an AI model to detect words in a special word search. However, you must label pieces of training data for the model. To do this, for every given $$$N$$$ x $$$N$$$ board of the special word search and an array of $$$K$$$ words, you must determine how many of those words appear in the word search.

Note that there may be words contained inside other words, such as "sea" and "seashell". There may also be multiple occurrences of the same word, but each word should only be counted once.

Input

$$$1 ≤ N ≤ 100$$$

$$$1 ≤ K ≤ 5,000$$$

$$$1 ≤ $$$ words[$$$i$$$].length $$$≤ 100$$$

Output

Line 1: One integer representing the number of given words that appear in the special word search.

Example
Input
4 5
t h i s
b a r c
w t m e
a p s o
this sea soap her water
Output
3
Note

t h i s

b a r c

w t m e

a p s o

Note that "her" and "soap" wrap around the word search.