UTPC Contest 10-28-22 Div. 1 (Advanced)
C. William and Middle Management
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

William is employed at a local factory in his dream job: middle management.

Taking the assembly line to heart, the factory contains $$$N$$$ workers, who reside on a line. Each individual $$$i$$$ has a productivity $$$p_i$$$ and works $$$h_i$$$ hours. Their individual output is given by the product of these two values.

As he is the most senior middle manager, William gets the first pick of his team. William is expected to manage a team of $$$K$$$ people and his bonus is the total output of his managed workers, in dollars. Of course, the team must be contiguous to ensure sufficient team bonding.

Please help William determine the optimal team and more importantly, tell him what he truly cares about, his bonus.

Input

The first line contains two integers: $$$N$$$ and $$$K$$$ where $$$1 \leq K \leq N \leq 10^5$$$.

The next $$$N$$$ lines each contain two integers, $$$1 \leq p_i \leq 100$$$ and $$$1 \leq h_i \leq 100$$$.

Output

A single integer, William's optimal bonus.

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

D. William and Cornmeal
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

William has baked a cornmeal pudding pone! They have invited all their friends over to share the dessert. However, they need your help to divide it evenly among all friends.

William wants all of their friends to have an equal number of slices. However, they don't know how many friends will actually show up. Friends show up in groups (where the total number of groups is $$$N$$$), and William cuts the pone in the following manner:

  1. The $$$i$$$th group of friends shows up, which consists of $$$a_i$$$ people.
  2. If the pone is currently cut into $$$s$$$ slices, William cuts the pone into $$$k \cdot s$$$ equal slices for some integer $$$k$$$, such that each person present ($$$a_1 + ... + a_i$$$ people total) can eat an equal number of slices. (But they don't eat it yet!)

The pone starts off unsliced. William wants to know – after everyone (all $$$N$$$ friend groups) show up, what's the minimum number of slices the cake will be cut into that satisfies the given technique?

See the notes for a more detailed example.

Input

The first line contains an integer $$$N$$$ ($$$1 \le N \le 10$$$), the number of friend groups.

The next line contains $$$N$$$ integers $$$a_1,...,a_N$$$ ($$$1 \le a_i \le 10$$$), representing the number of friends in each friend group.

Output

Output a single integer, the minimum number of slices the cake will be cut into.

Example
Input
3
2 3 10
Output
30
Note

The example proceeds in the following manner:

  1. The first group of friends shows up, consisting of 2 people. The pone is initially unsliced, so William slices it into two halves, such that each friend gets a half.
  2. The second group of friends shows up, consisting of 3 people, so there are now 5 total people. William slices each half into 5 pieces, so there are 10 slices total and each friend gets 2 slices.
  3. The third group of friends shows up, consisting of 10 people, so there are now 15 total people. Each of the 10 slices can now be sliced into thirds, so there are 30 slices total and each friend gets 2 slices.

    It can be shown that this is the least number of slices possible that satisfies the requirements.

E. William and Robot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

William is playing a game with a robot. In this game, there is an array of $$$n$$$ integers in a line, numbered $$$1, \ldots n$$$. $$$n$$$ is even.

William and the robot take turns selecting integers from the array, starting with William. William can take an integer from anywhere in the array as long as it has not already been taken by any player. The robot always takes the leftmost, untaken integer (i.e. the one with the lowest index). The game ends once all integers have been taken. William's score is the sum of his chosen integers.

Help William get the maximum score possible.

Input

The first line contains $$$n$$$ ($$$1 \leq n \leq 10^5$$$), the number of integers in the array.

The second line contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$), where $$$a_i$$$ denotes the $$$i$$$th integer of the array.

Output

Output the maximum score William can achieve.

Examples
Input
4
6 1 1 4
Output
10
Input
10
1 3 4 9 5 2 5 5 3 6
Output
30

F. William and Cards
time limit per test
7.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

William likes to play card games. In this particular game, William is dealt a row of $$$N$$$ playing cards. Each of the cards has a positive integer point value, where the $$$i$$$-th card in the row has a point value of $$$p_i$$$.

The object of the card game is to minimize the maximum point value among all of the cards that William has. To do so, William is allowed to perform operations. For any $$$1 \leq i \lt N$$$, William may halve $$$p_i$$$ and double $$$p_{i-1}$$$ so long as $$$p_i$$$ is an even number. William may perform as many operations as he desires.

If William performs operations optimally, can you figure out the minimum maximum point value that he can achieve?

Input

The input will begin with a line containing a single integer $$$N\ (1 \leq N \leq 10^5)$$$. The next line will contain $$$N$$$ space-separated integers $$$p_0, \dots, p_{N-1}\ (1 \leq p_i \leq 10^9)$$$, the initial point values of the $$$N$$$ playing cards that William is dealt.

Output

The output should consist of a single integer representing the minimum maximum point value among all of the cards William is dealt assuming that he performs optimal operations.

Example
Input
5
1 25 36 80 12
Output
25

G. William and Spaceport
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

William is overseeing a system of spaceports in the galaxy of Glarble. Because space trips are expensive, spaceports generally try to have as few unnecessary flights as possible. In particular, the (two-way) flights can be represented by a tree, such that each planet in Glarble can be reached by each other planet in Glarble through some sequence of flights, but this sequence is unique (there is only one path from one planet to another).

Because different planets have different resources, each planet has an associated cost of visiting the planet. The cost of a trip from planet $$$u$$$ to planet $$$v$$$ ($$$u \neq v$$$), then, is the sum of the costs of all planets visited on the distinct path from $$$u$$$ to $$$v$$$. The total cost of the spaceport system is the sum of the costs of all distinct trips, where two trips are the same if they have the same starting planet and the same ending planet. Trips and flights begin and end on different planets.

William has the technological ability to upgrade spaceports; when he upgrades a spaceport, its cost decreases by one, unless the cost is already 0 in which case nothing happens. He can upgrade spaceports $$$K$$$ times (not necessarily distinct spaceports). He then wants to know: if he does this optimally, what's the minimum total cost of his new spaceport system?

Input

The first line contains two integers $$$N$$$, $$$K$$$ ($$$1 \le N \le 10^5$$$, $$$1 \le K \le 10^9$$$), corresponding to the number of planets, and the number of upgrades William can perform, respectively.

The second line contains $$$N$$$ integers $$$c_1,...,c_N$$$ ($$$1 \le c_i \le 10^3$$$), where $$$c_i$$$ denotes the cost of visiting planet $$$i$$$.

The next $$$N-1$$$ lines contain two integers each, $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le N, u \neq v$$$), denoting that there is a two-way flight between planet $$$u$$$ and planet $$$v$$$.

Output

Output a single integer, the minimum total cost of his new spaceport system if he upgrades spaceports optimally.

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

In the first example, we have 6 total trips, and the total cost of the spaceport system is 26. William can then upgrade spaceport 3 once and spaceport 1 once, and the new cost for the spaceport system is 16. It can be shown this is the least possible cost.

H. William and will.i.am
time limit per test
10 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

William and will.i.am are the host and judge, respectively, of a rap battle tournament! The $$$N$$$ contestants will perform on one of two different tracks selected by will.i.am. For each contestant, we know their "rap skill level" on each track. When two players battle on a specific track, the player with higher rap skill level on that track always wins. No two players have the same rap skill level on the same track.

The tournament consists of $$$N-1$$$ battles, and the tournament will proceed as follows:

  1. Randomly select two players still in the tournament.
  2. Choose a track out of the two available ones for the players to battle on.
  3. The player that wins the individual fight will continue on in the tournament, and the loser will be eliminated.
  4. Go back to step 1 and repeat until a single player remains.

William would like to quantitatively measure how "interesting" the tournament structure is relative to other ideas he has, which is directly proportional to the number of possible winners of the tournament. Can you help William find the number of contestants that have a shot at winning it all?

Input

The first line of each test case contains a single integer $$$N$$$ ($$$2 \leq N \leq 10^6$$$), representing the number of tournament contestants. The second line of each test case contains $$$N$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$, $$$a_i \neq a_j$$$ for $$$i \neq j$$$), where $$$a_i$$$ is the rap skill level of the $$$i$$$-th contestant on the first track. The third line of each test case contains $$$N$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \leq b_i \leq n$$$, $$$b_i \neq b_j$$$ for $$$i \neq j$$$), where $$$b_i$$$ is the rap skill level of the $$$i$$$-th contestant on the second track.

Output

Output a single integer representing the number of unique tournament winners out of the $$$N$$$ contestants.

Examples
Input
4
1 2 3 4
1 2 3 4
Output
1
Input
4
1 2 3 4
4 3 1 2
Output
4

I. William and Array
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today is William's 26th birthday! As such, his friends have gifted him an array of length $$$N$$$. Since he is turning 26, the array will consist of only the 26 letters of the English alphabet ($$$\{\text{a}, \text{b}, \dots, \text{z}\}$$$). However, William's friends are super picky – they want the array to be perfect before they present it to William. Each of William's $$$Q$$$ friends have a chance to either make a change to the array by changing the letter at a certain index to a different English letter, or to inspect a contiguous subarray of the array and check how many palindromes the letters within the subarray can create. A palindrome is a string which is read the same forwards and backwards.

For example, if the array initially looks like $$$\{\text{q}, \text{q}, \text{r}, \text{a}, \text{r}\}$$$, and William has 2 friends, then William's first friend may choose to change the letter $$$\text{q}$$$ at index 1 to the letter $$$\text{a}$$$, resulting in the array becoming $$$\{\text{q}, \text{a}, \text{r}, \text{a}, \text{r}\}$$$, and his second friend might inspect the contiguous subarray starting at index 1 and ending at index 4 ($$$\{\text{a}, \text{r}, \text{a}, \text{r}\}$$$). The second friend will then realize that there exist 2 possible palindromes using exactly these letters ($$$\text{arra, raar}$$$). Note: when executing an inspection, the friend must use all of the letters in the range to construct palindromes.

Since William has many friends and each friend would like to perform one of these two types of queries (and because the array they are working with is very large), they need help doing all of the necessary computations. Please help them out!

Input

The first line of input will contain two space-separated integers, $$$N\ (1 \leq N \leq 2 \cdot 10^5)$$$ and $$$Q\ (1 \leq Q \leq 2 \cdot 10^5)$$$, denoting the length of the array and the number of friends William has, respectively. The next line will consist of a single string of length $$$N$$$ consisting of lowercase English letters representing the initial state of the array. The next $$$Q$$$ lines will each begin with either a $$$1$$$ or a $$$2$$$. A $$$1$$$ represents a query of the first type, and will be followed by an integer $$$i\ (0 \leq i \lt N)$$$ and a lowercase English letter. This means that a friend would like to replace the letter currently at index $$$i$$$ with the given letter. A $$$2$$$ represents a query of the second type, and will be followed by two integers, $$$l\ (0 \leq l \lt N)$$$ and $$$r\ (l \leq r \lt N)$$$ denoting the start and end (inclusive) of a contiguous subarray that the current friend would like to inspect.

Output

For each query of the second type, print a single line containing the number of palindromes which can be created using the letters found within the contiguous subarray that the friend would like to inspect. Since the answer to each query can be quite large, output each number modulo $$$10^9 + 7$$$. It is guaranteed that there will be at least one query of the second type.

Example
Input
5 5
bbbaa
2 0 4
1 1 b
2 2 4
1 2 c
2 1 2
Output
2
1
0

J. William and Rangoli
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For celebrating Diwali, William created rangoli in the shape of a regular polygon with $$$n$$$ vertices, where $$$n$$$ is even. $$$m$$$ of the vertices of the polygon in a row are all white, and the remaining $$$n - m$$$ nodes are black. Each of the edges of the polygon are connected with purple lines.

To fill in the polygon, William first creates a triangulation of the polygon, again using purple edges and only drawing non-intersecting diagonals. Now, William fills in the each of the triangles. If all three vertices of the triangle are the same color, William is unhappy, so the triangulation would be invalid. Otherwise, if there are 2 black vertices and 1 white vertex, the triangle is colored black. Similarly, if there are 2 white vertices and 1 black vertex, the triangle is colored white. In the end, William wants there to be $$$k$$$ white triangles and $$$n - k - 2$$$ black triangles for the triangulation to be valid.

William is having trouble deciding which triangulation he wants to use. How many possible valid triangulations are there? Because the answer may be large, compute it modulo $$$10^9 + 7$$$.

Input

The first and only line contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$, $$$0 \leq m \leq n$$$, $$$0 \leq k \leq n - 2$$$) — the number of vertices in the polygon, the number of white vertices, and the required number of white triangles.

Output

Output a single integer — the total number of valid triangulations, modulo $$$10^9 + 7$$$.

Example
Input
6 3 2
Output
6
Note

Below is an image of a valid triangulation for the sample test case:

K. William and Necklace
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

William loves making necklaces. He has an infinite number of each letter 'a'...'z', and wants to arrange them in a circle to make a necklace of length $$$n$$$, where $$$n$$$ is prime.

However, William only wants to make good necklaces. A necklace is considered good if it does not contain the string $$$s$$$, even after rotation.

Help William count the number of distinct good necklaces he can make. Two necklaces are the same if one can be rotated to be equal to the other. "abab" and "baba" are the same, but "abab" and "aabb" are distinct.

Input

The first line contains two integers, $$$m$$$ and $$$n$$$ ($$$1 \leq m \leq n \leq 250$$$). It is guaranteed that $$$n$$$ is prime.

Then, the next line contains the string $$$s$$$, which consists of $$$m$$$ lowercase letters.

Output

Output the number of distinct good necklaces, mod $$$10^9+7$$$.

Examples
Input
2 3
aa
Output
5850
Input
3 5
abc
Output
2375620
Note

In the first example, there are 5876 total necklaces of length 3. Of those, the 26 necklaces in the form "aa?" (or equivalently, "a?a" or "?aa") are not good, leaving a total of 5850 necklaces that are good.