UTPC Contest 9-17-25 Div. 1 (Advanced)
C. Game on Venus
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It is 2067 and the fine astronauts Adrian and Ashton just landed on Venus, marking a significant step forward in space exploration for mankind! However, they are currently having a dispute trying to figure out who should get the honor of being the first to touch down on the surface. To prevent further arguing, their advisor Maverick suggests that they play the following game to decide.

In this game, there is an array $$$a$$$ of $$$3n$$$ $$$(1 \leq n \leq 10^5)$$$ numbers. In each round:

  • Adrian may (or may not) $$$\textbf{cyclically shift}^\dagger$$$ the array any number of times.
  • Adrian then will select two indices $$$i, j$$$ in the array such that they are not next to each other ($$$i \lt j$$$ and $$$|i - j| \neq 1$$$). He will add $$$a[i] + a[j]$$$ to his score (the values at these indices).
  • Ashton then will select an index $$$k$$$ between $$$i,j$$$ ($$$i \lt k \lt j$$$) and add $$$3 * a[k]$$$ to his score.
  • After this, elements at indices $$$i, j, k$$$ are removed from the array.

This process repeats until there are no elements left in $$$a$$$, at which point the game ends.

Adrian and Ashton both want to maximize their score, as the player with the higher score at the end of the game wins. If Adrian and Ashton play optimally, what score will both of them have at the end of the game?

$$$^\dagger$$$Given an array $$$a$$$ of size $$$k$$$, a cyclic shift of $$$a$$$ turns $$$[a_1, a_2, ..., a_{k-1}, a_k]$$$ into $$$[a_k, a_1, a_2, ..., a_{k-1}]$$$

Input

The first line contains an integer $$$n$$$, where $$$a$$$ is of size $$$3n$$$

The second line contains $$$3n$$$ integers, $$$[a_1, a_2, ... a_{3n}]$$$ $$$(1 \leq a_i \leq 10^9$$$), the elements in $$$a$$$.

Output

Output two numbers, Adrian's score and Ashton's score if both play optimally.

Examples
Input
1
10 5 10
Output
20 15
Input
2
3 8 5 4 10 1
Output
27 12
Note

In the first test case, in the first round it is optimal for Adrian to leave the array as is and select indices $$$i = 1, j = 3$$$. Thus Ashton only has one choice for $$$k$$$, which is $$$2$$$.

After this round, Adrian's score is $$$10 + 10 = 20$$$, and Ashton's score is $$$5 * 3 = 15$$$.

After removing indices $$$1, 2, 3$$$ from the array, it is empty, and thus the game ends. So these are Adrian and Ashton's final scores.

In the second test case, one sequence of operations leading to the optimal scoring is as follows:

Round 1:

  • Adrian cyclically shifts the array twice, making it $$$[10, 1, 3, 8, 5, 4]$$$
  • Adrian selects $$$i = 1, j = 4$$$, and Ashton selects $$$k = 3$$$
  • Adrian's score is now $$$10 + 8$$$, Ashtons score is now $$$3 * 3$$$
  • After removing these indices from the array, we are left with $$$[1, 5, 4]$$$
Round 2:
  • Adrian cyclically shifts the array once, making it $$$[4, 1, 5]$$$
  • Adrian selects $$$i = 1, j = 3$$$, and Ashton selects $$$k = 2$$$
  • $$$4 + 5$$$ is added to Adrian's score, and $$$1 * 3$$$ is added to Ashton's score.
  • After removing these indices from the array, it is now empty, and the game ends

Thus Adrian's score at the end is $$$10 + 8 + 4 + 5 = 27$$$, and Ashton's score is $$$3 * 3 + 1 * 3 = 12$$$.

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

Warith has been coping for a while about not being able to see Le Sserafim in Dallas, so he decided to take a mental break. He decided to go down to the Colorado River and skip some stones. When he arrived, he found $$$n$$$ stones arranged in a line. Warith knows that each stone has some mass $$$a_i$$$ $$$(1 \le i \le n)$$$. With all the stuff on his mind, he wants to maximize his enjoyment from skipping stones by picking a contiguous subarray $$$a'$$$ of the original stone array. Note that Warith cannot rearrange the stones, he can only pick a contiguous subarray from the original order.

For an array $$$a'$$$, the enjoyment from skipping all these stones is calculated as follows:

  • For each unique mass $$$m$$$ in the stones of $$$a'$$$, find the number of stones with this mass ($$$f$$$)
  • The enjoyment is the sum of $$$m \cdot f$$$ across all unique $$$m$$$

The only issue is that Warith also does not want the mass of the stones to vary too much either. Specifically, he doesn't want his subarray to contain more than $$$k$$$ distinct masses. With this in mind, can you help Warith maximize his enjoyment?

Input

The first line of input will contain two integers $$$n$$$ ($$$1 \le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le n$$$): the number of stones, and the max number of permitted unique masses.

The second line contains $$$n$$$ integers ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the mass of the $$$i$$$th stone.

Output

Output a single integer $$$E$$$: the maximum enjoyment Warith can achieve.

Example
Input
8 3
1 7 2 3 2 2 1 7
Output
16
Note

In the sample test case, the optimal range is $$$[7, 2, 3, 2, 2]$$$, which has enjoyment $$$(7)(1) + (3)(1) + (2)(3) = 16$$$.

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

In the solar system, it is often said that the most habitable planet besides Earth is Mars. Indeed, scientists have recently begun settling on Mars and have set up a base for permanent living. However, while Mars has an abundance of water and soil nutrients, it's missing the greatest natural resource: Milk! Unfortunately, this means that the scientists must import milk from Earth in order to sustain themselves.

Johnny, one of the scientists, has a particular affinity for milk. He has knowledge of spaceship trips which can transport jugs of milk from Earth to Mars and vice versa. Each of these trips has a departure and arrival time, and all of these times are distinct from each other.

Due to restrictions regarding waste, in order to import milk, Johnny must place an empty jug of milk on a spaceship bound for Earth where it will be filled immediately. He can then request for a particular spaceship bound for Mars to carry the milk back, where Johnny can empty the milk jug in a large storage container. Each spaceship can only carry one of Johnny's milk jugs at most (other scientists want to use the spaceships as well!).

Before any of the spaceships begin transporting items, Johnny has two empty milk jugs on hand. Given this knowledge, Johnny would like to know the maximum jugs' worth of milk he can import from Earth with the spaceships available.

Input

The first line contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of spaceship trips.

The next $$$n$$$ lines each contain three integers describing a spaceship trip, with the $$$i$$$-th line containing $$$l_i$$$, $$$r_i$$$, and $$$t_i$$$ ($$$0 \le l_i \lt r_i \le 10^9$$$, $$$t_i \in \{0, 1\}$$$) — the departure time, the arrival time, and the trip type, respectively. If $$$t_i = 0$$$, the trip is bound for Earth, and if $$$t_i = 1$$$, the trip is bound for Mars.

Output

Output a single integer — the maximum jugs' worth of milk that Johnny can import from Earth.

Examples
Input
4
0 1 0
2 3 1
4 5 0
6 7 1
Output
2
Input
6
0 4 0
7 8 1
1 5 0
9 10 1
2 6 0
11 12 1
Output
2
Note

In the first test case, Johnny can place an empty milk jug on the spaceship departing at time $$$0$$$ for Earth, and schedule it to return on the spaceship departing at time $$$2$$$ for Mars. He can then empty that milk jug in storage, place it on the spaceship departing at time $$$4$$$ for Earth, and schedule it to return on the spaceship departing at time $$$6$$$ for Mars. The filled milk jug will then be emptied into Johnny's storage. In total, Johnny will have $$$2$$$ jugs' worth of milk in storage after all trips have occurred.

In the second test case, Johnny can schedule his trips as follows:

  • At time $$$0$$$, place an empty milk jug on the spaceship departing for Earth.
  • At time $$$1$$$, place the second empty milk jug on the spaceship departing for Earth.
  • Request a filled milk jug to return on the spaceship departing for Mars at time $$$7$$$.
  • Request another filled milk jug to return on the spaceship departing for Mars at time $$$9$$$.
In total, Johnny will have $$$2$$$ jugs' worth of milk in storage after all trips have occurred.

F. Jupiter
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Jupiter is home to the largest Coriolis forces in the Solar System. Specifically, the Coriolis force causes horizontal bands on the planet to experience strong winds either East or West. Jupiter has $$$8$$$ of these prominent bands.

Embarking on a mission to navigate Jupiter, you are well aware of winds and need to factor them into the amount of fuel you need to bring on your mission. Your spaceship will enter Jupiter at coordinate $$$S$$$, and need to end at coordinate $$$D$$$. A coordinate can be denoted by the band you are in (vertical position), and the columns (horizontal position) along that band at time $$$t=0$$$.

Each timestep, the Coriolis winds take effect, rotating all positions of even-indexed bands right, and all positions of odd-indexed bands left.

Jupiter also has numerous storms that move along with these bands. You want to avoid these storms at all costs.

Consider the following 4 banded system example, as we can find on Earth:


col1 col2 col3 col4 col5
band1 | . | X | . | D | . | <–
band2 | X | . | X | X | X | –>
band3 | . | S | . | . | . | <–
band4 | . | . | . | . | . | –>

After one timestep, the world would look like:


col1 col2 col3 col4 col5
band1 | X | . | D | . | . | <–
band2 | X | X | . | X | X | –>
band3 | S | . | . | . | . | <–
band4 | . | . | . | . | . | –>

Your spaceship lets you move one tile in any cardinal direction (not diagonally) before the Coriolis effect takes effect each timestep. Note you can also choose to not move, in which you move along with the band. Also, note that columns 1 and $$$N$$$ are adjacent, but band1 and band8 are not.

Find the minimum amount of timesteps to travel from coordinate A to coordinate B (if possible) so we can know how much fuel to bring!

Input

The first line contains $$$N$$$, the number of horizontal positions in each band. $$$1 \leq N \leq 3 \cdot 10^3$$$

The next $$$8$$$ lines each contain $$$N$$$ space-separated characters, with each line denoting the state of the world in that band at $$$t=0$$$. The character at the $$$i$$$th position on a line represents column $$$i$$$ in that corresponding band.

  • '.': empty tile
  • 'X': storm tile
  • 'S': start tile
  • 'D': destination tile
Output

Output one integer, representing the minimum amount of timesteps required to travel from start to destination, or -1 if impossible.

Examples
Input
5
X X X X X
X X X X X
. X . D .
X . X X X
. S . . .
. . . . .
X X X X X
X X X X X
Output
2
Input
5
S . . . .
. X X X X
X X . X X
X X X . X
X X X X .
X . X X X
X . X X X
X X X X D
Output
7
Note

For explaining the first sample, let $$$(x,y)$$$ be the coordinate that is in the $$$x$$$th band and the $$$y$$$th column (both $$$1$$$-indexed).

  • At $$$t=0$$$, the bands are as they are in the sample.
  • At $$$t=1$$$, we can move up from $$$(5,2)$$$ to $$$(4,2)$$$. The bands will then move from the Coriolis effect, so we will move to $$$(4,3)$$$, and $$$D$$$ will move to $$$(3,3)$$$.
  • At $$$t=2$$$, we can move up from $$$(4,3)$$$ to $$$(3,3)$$$, arriving at our destination just before the Coriolis effect moves our positions again.

It can be shown this is the fastest way to reach the destination.

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

Note: The following problem, Uranus, may be easier if you prefer solving in order of increasing difficulty.

Christiaan Huygens was the first to propose there was a ring surrounding Saturn, in 1655. He did this by publishing his claim as an anagram: aaaaaaacccccdeeeeehiiiiiiillllmmnnnnnnnnnooooppqrrstttttuuuuu. Only three years later did he have enough evidence to publish the true claim: Annulo cingitur, tenui, plano, nusquam cohaerente, ad eclipticam inclinato ("It is encircled by a thin, flat ring, nowhere touching, inclined to the ecliptic").

Shani recently became inspired by Huygens and published some similar anagrammed claim. Unfortunately, it was mostly a joke, but now the research committee is calling upon her to back it up.

Luckily, she does have some data that she has yet to publish. Specifically, she has $$$1 \le n \le 1000$$$ data points, each a string $$$s_i$$$ with $$$1 \le |s_i| \le 100$$$. She may publish any subset of these data points, and once published, she knows the committee will concatenate them in their original order (the data points are dated). Her original claim is a string $$$t$$$ with $$$1 \le |t| \le 20$$$. The committee will then count how many times $$$t$$$ occurs as a substring in the published data (overlaps are counted), and for each occurrence she will earn one reputation score.

Report the maximum reputation score Shani can get.

Input

The first line contains a string $$$t$$$ $$$(1 \le |t| \le 20)$$$ — Shani's original claim.

The next line contains an integer $$$n$$$ $$$(1 \le n \le 1000)$$$ — the number of data points.

The next $$$n$$$ lines each contain a non-empty string $$$s_i$$$ $$$(1 \le |s_i| \le 100)$$$ — the $$$i$$$-th data point, already listed in the order they were recorded.

All strings consist only of lowercase English letters.

Output

Print a single integer — the maximum reputation score Shani can obtain.

Examples
Input
lol
3
olo
lol
olo
Output
3
Input
ababac
3
abab
aba
abac
Output
1
Note

For the first sample, publishing all three data points gives 3 reputation score: $$$\text{o}\color{red}{\text{lol}}\text{ololo}$$$ $$$\text{olo}\color{red}{\text{lol}}\text{olo}$$$ $$$\text{ololo}\color{red}{\text{lol}}\text{o}$$$

For the second, publishing the first and the last gives 1 reputation score: $$$\text{ab}\color{red}{\text{ababac}}$$$

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

Although it is not the farthest planet from the Sun, Uranus is considered to be the coldest planet in the Solar System, experiencing a minimum temperature of 49 K and extreme winds of up to 900 km/h. Undaunted, Sir Vedward V's twin brother, Sir Udward, wishes to send probes to explore Uranus.

Currently, VASA has developed $$$n$$$ probes, where the $$$i^{\text{th}}$$$ probe can survive temperatures of up to $$$x_i$$$ degrees Kelvin and wind speeds of up to $$$y_i$$$ km/h; however, it costs $$$c_i$$$ dollars to construct. In addition, VASA has conducted $$$q$$$ measurements of locations on Uranus, where the $$$i^{\text{th}}$$$ location has a temperature of $$$t_i$$$ degrees Kelvin and a wind speed of $$$w_i$$$ km/h.

Sir Udward will reward you handsomely if you help him determine, for each landing location, the cheapest probe that can survive its temperature and wind speed.

Input

The first line of input contains $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 10^5$$$), the number of probes and locations, respectively.

The second line contains $$$n$$$ integers, $$$x_1, x_2, ..., x_n$$$ ($$$1 \le x_i \le 10^9$$$), the maximum temperature each probe can survive.

The third line contains $$$n$$$ integers, $$$y_1, y_2, ..., y_n$$$ ($$$1 \le y_i \le 10^9$$$), the maximum wind speed each probe can survive.

The fourth line contains $$$n$$$ integers, $$$c_1, c_2, ..., c_n$$$ ($$$1 \le c_i \le 10^9$$$), the cost of each probe.

The next $$$q$$$ lines each contain two integers $$$t_i$$$ and $$$w_i$$$ ($$$1 \le t_i, w_i \le 10^9$$$), the temperature and wind speed at the $$$i^{\text{th}}$$$ location, respectively.

Output

The output should be $$$q$$$ integers, each on a separate line, the minimum cost of a probe that can survive for each location, or $$$-1$$$ if there is no probe that can survive.

Example
Input
3 3
1 2 3
3 1 2
3 2 1
1 2
1 3
3 3
Output
1
3
-1
Note

The first probe ($$$1 \ge 1$$$ and $$$3 \ge 2$$$) and third probe ($$$3 \ge 1$$$ and $$$2 \ge 2$$$) can both survive the first location, but the third probe is cheaper, so the output is $$$1$$$.

Only the first probe ($$$1 \ge 1$$$ and $$$3 \ge 3$$$) can survive the second location, so the output is $$$3$$$.

None of the probes can survive the third location, so the output is $$$-1$$$.

I. Neptune
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, Poseidon has gotten addicted to a game called Craftmine. In Craftmine, there is a crafter that consists of $$$k$$$ slots, each of which can store an unlimited amount of a single type of ingredient. If the slot has 0 ingredients, it is considered empty. A recipe represents a configuration of the crafter that will produce a desired product. Each recipe is an array of length $$$k$$$ whose elements are either an empty slot or a type of ingredient.

Each time the crafter is used, it checks if the current configuration matches a recipe by comparing the ingredients in each of the slots (but not the amount of each ingredient). If the configuration matches a recipe, every slot uses up exactly 1 of the ingredients in that slot (except for empty slots, where nothing happens).

Currently, Poseidon has $$$n$$$ unique recipes and (being a god) has an unlimited amount of all ingredients. However, Poseidon is quite lazy, so he plans to fill the crafter only once, then use it as many times as he can to create unique recipes.

What is the maximum number of unique recipes that can be created after filling the crafter once?

Warning: Java and Python solutions are probably too slow for this problem.

Input

The first line of input contains $$$n$$$ and $$$k$$$ $$$(1\leq n\leq 2000, 1\leq k\leq 2000)$$$.

The $$$i$$$th of the next $$$n$$$ lines contains recipe $$$i$$$, which is an array $$$a_i$$$ of $$$k$$$ integers, with 0 representing an empty slot, and all other numbers representing a type of ingredient $$$(0\leq a_{i,j} \leq 100)$$$.

Output

Output a single integer, the maximum number of unique recipes that can be created after filling the crafter exactly once.

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

Poseidon can put 2 of item 3 in the 1st slot, 1 of item 10 in the 2nd slot, 3 of item 1 in the 3rd slot, and nothing in the 4th slot: $$$\{3_2, 10_1, 1_3, \emptyset\}$$$.

Using the crafter now will yield recipe 4 ($$$\{3,10,1,\emptyset\}$$$). After the ingredients are used up for the recipe, the crafter becomes $$$\{3_1,\emptyset, 1_2, \emptyset\}$$$.

Using the crafter now will yield recipe 3 ($$$\{3,\emptyset, 1, \emptyset\}$$$). After the ingredients are used up for the recipe, the crafter becomes $$$\{\emptyset, \emptyset, 1_1, \emptyset\}$$$.

Using the crafter now will yield recipe 1 ($$$\{\emptyset, \emptyset, 1, \emptyset\}$$$). After the ingredients are used for the recipe, the crafter becomes $$$\{\emptyset, \emptyset, \emptyset, \emptyset\}$$$).

At this point, no more recipes can be made. Since 3 unique recipes were made, the answer is 3.

J. Pluto
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Shortly after solving their previous dilemma, the Plutonian government has asked for your help once again. There is a special vault in the heart of the Plutonian capitol, and the government needs to unlock it (don't ask why), but the process of doing so is quite complex so they've requested your help.

The vault has a lock consisting of $$$n$$$ switches, numbered $$$1...n$$$, where $$$n$$$ is odd. The initial state of the lock is given as a bitstring of length $$$n$$$, where the $$$i$$$-th character represents the state of the switch $$$i$$$, and a '1' denotes that the switch is up while a '0' denotes that it is down. To unlock the safe, all of the switches need to be down. You want to unlock the lock, but you can only perform the following two operations on the lock:

  • Flip two adjacent switches. In other words, choose some $$$i$$$ such that $$$1 \leq i \leq n - 1$$$ and flip the value of switch $$$i$$$ and $$$i + 1$$$ simulatneously.
  • Flip all switches simultaneously.

The Plutonian government is short on time, so they want you to determine the minimum number of operations necessary to set all switches to the down ('0') state.

Of course it isn't that simple though. After all, the government is short on time because every minute for $$$q$$$ minutes the state of the lock changes. During the $$$i$$$th update, all switches $$$j$$$ such that $$$l_i \leq j \leq r_i$$$ are flipped. Note that the updates persist. Please help the Plutonians and output the minimum number of operations to set all switches to the down state after each update.

Input

The first line of input will consist of two integers $$$n$$$ and $$$q$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq q \leq 2 \cdot 10^5$$$, $$$n$$$ is odd) — denoting the number of switches in the lock and the number of updates respectively.

The next line contains a bitstring $$$s$$$ of length $$$n$$$ ($$$s_i \in \{'0', '1'\}$$$) — denoting the state of the lock.

The $$$i$$$-th of the next $$$q$$$ lines contains integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \leq l_i \leq r_i \leq n$$$) — denoting that during the $$$i$$$-th update the state of the locks from $$$l_i$$$ to $$$r_i$$$ are flipped.

Output

Output $$$q$$$ lines, each with a single integer denoting the minimum number of operations needed to unlock the lock after each update occurs.

Examples
Input
7 3
1101010
4 7
1 3
5 6
Output
3
4
3
Input
9 2
000000001
9 9
1 9
Output
0
1