UTPC Contest 9-17-25 Div. 2 (Beginner)
A. Sun
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The Solaris satellite is orbiting the Sun to collect important information about the most important celestial body for Earth. To do this, the satellite collects an array of $$$n$$$ integers and sends back $$$k$$$ checksums to ensure the accuracy of the data.

Each checksum can be represented as $$$x_1 + 2x_2 + x_3 + 2x_4 + x_5 + \cdots$$$, where $$$x$$$ is an array of integers labeled $$$x_1, x_2, x_3, \cdots$$$ that is being added up, alternating between multiplying by 1 and multiplying by 2.

More formally, let the checksum of array $$$x$$$ with length $$$l$$$ be $$$f(x)$$$ where

$$$f(x) = \sum_{i=1}^{l} \left(2-\left(i \bmod 2\right)\right)\cdot x_i$$$

Given an array $$$a$$$ of $$$n$$$ integers, print out the results of $$$k$$$ checksums, where each checksum runs on a subarray of $$$a$$$ from indices $$$l_i$$$ to $$$r_i$$$ inclusive.

Input

The first line of input will consist of 2 integers, $$$n$$$ and $$$k$$$ $$$(1\leq n\leq 100, 1\leq k\leq 100)$$$.

The second line will contain the array $$$a$$$ $$$(-10^3\leq a_i\leq 10^3)$$$.

The $$$i$$$th of the next $$$k$$$ lines will contain 2 integers, $$$l_i$$$ and $$$r_i$$$ $$$(1\leq l_i\leq r_i\leq n)$$$.

Output

Output $$$k$$$ lines, each with a single integer denoting the answer to the $$$i$$$th checksum.

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

The answer to the first query is 9 since $$$5+2\cdot3-2 = 9$$$.

The answer to the second query is -6 since $$$3-2\cdot2+1-2\cdot3 = -6$$$.

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

On Mount Olympus, gods love to send each other encoded messages. Each message consists of a ciphertext made of lowercase English letters along with $$$n$$$ phrases. To decipher the message, a Caesar$$$^\dagger$$$ cipher is applied to the ciphertext such that the $$$n$$$ phrases all appear as substrings of the resulting string.

Being the one who is always delivering these secret messages, Mercury wants to read these messages, but he cannot figure out how to decipher the text correctly. Your task is to help Mercury decipher the messages. It is guaranteed that an answer exists. If there are multiple possible answers, any valid answer will be accepted.

$$$^\dagger$$$To apply a Caesar cipher to a letter, first convert the letter to a number from 0 to 25 (A=0, B=1, C=2, ..., Z=25), and call this number $$$x$$$. When shifting by $$$k$$$, find $$$(x+k)\bmod 26$$$, and convert this number back to a letter (0=A, 1=B, 2=C, ..., 25=Z). This means that shifting Y (25) by 4 gives a result of C (3). To apply a Caesar cipher to a string, just apply the cipher for every individual letter, keeping the same $$$k$$$ each time.

Input

The first line of input contains $$$s$$$, the ciphertext ($$$1\leq\left|s\right|\leq10^3$$$). It is guaranteed that the ciphertext consists of only lowercase English letters.

The next line contains $$$n$$$, the number of phrases ($$$1\leq n\leq 10^3$$$).

The $$$i$$$th of the next $$$n$$$ lines contains the $$$i$$$th phrase, $$$p_i$$$ ($$$1\leq\left|p_i\right|\leq 30$$$). It is guaranteed that all phrases consist of only lowercase English letters.

Output

Output a single line, the correct deciphered phrase. It is guaranteed that an answer exists. If there are multiple possible answers, print out any valid answer.

Example
Input
ebiiltloia
5
hello
world
hellow
ell
owo
Output
helloworld

C. 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$$$.

D. 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.

E. 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.

F. 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}}$$$

G. 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$$$.