2026 Fall UT CS104c Midterm #2
A. Magikarp Jump
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Magikarp dreams of becoming a jumping champion and eventually evolving into a Gyarados! For now, he is practicing on a row of $$$n$$$ spots, numbered $$$1$$$ through $$$n$$$. The height of spot $$$i$$$ is $$$h_i$$$.

Magikarp starts at spot $$$1$$$ and wants to reach spot $$$n$$$. From spot $$$i$$$, he can jump to spot $$$i+1$$$, $$$i+2$$$, or $$$i+3$$$, as long as the destination does not exceed $$$n$$$. A jump from spot $$$i$$$ to spot $$$j$$$ costs $$$$$$ (h_i-h_j)^2 $$$$$$ energy. The total energy cost is the sum of the costs of all his jumps.

What is the minimum total energy cost for Magikarp to reach spot $$$n$$$?

Input

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$) — the number of spots.

The second line contains $$$n$$$ integers $$$h_1,h_2,\ldots,h_n$$$ ($$$0 \le h_i \le 10^6$$$) — the heights of the spots.

Output

Output a single integer — the minimum total energy cost for Magikarp to reach spot $$$n$$$.

Examples
Input
6
10 30 20 40 10 20
Output
100
Input
7
0 1000000 1000000 0 1000000 1 1000000
Output
999998000002
Input
4
1 100 100 1
Output
0
Input
1
7
Output
0
Note

In the first sample, Magikarp can jump from spot $$$1$$$ to spot $$$3$$$, then to spot $$$6$$$. The total cost is $$$(10-20)^2+(20-20)^2=100$$$, which is the minimum possible.

In the second sample, one optimal route visits spots $$$1,4,6,7$$$, using jumps of lengths $$$3,2,1$$$. The total cost is $$$$$$ (0-0)^2+(0-1)^2+(1-10^6)^2=999998000002. $$$$$$

In the third sample, Magikarp can jump directly from spot $$$1$$$ to spot $$$4$$$. Both spots have height $$$1$$$, so the cost is $$$0$$$.

In the fourth sample, Magikarp is already at spot $$$n$$$, so he does not need to jump. The total cost is $$$0$$$.

B. Changing Change
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Magikarp is a cashier at Hudson River Trader Joe's (HRTJ). HRTJ has some weirdly valued coins, and the available coin values can change from day to day!

On a day with parameters $$$b$$$ and $$$k$$$, HRTJ has coins worth $$$b^0,b^1,\ldots,b^k$$$ cents. Magikarp has an unlimited number of coins of each of these values. He must give a customer exactly $$$x$$$ cents in change.

For each of $$$t$$$ days, find the minimum number of coins Magikarp needs. Each day is independent.

Input

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of days.

Each of the next $$$t$$$ lines contains three integers $$$b$$$, $$$k$$$, and $$$x$$$ ($$$2 \le b \le 10^9$$$, $$$0 \le k \le 29$$$, $$$1 \le x \le 10^9$$$) — the base of the coin values, the largest exponent, and the amount of change in cents for that day, respectively.

It is guaranteed that $$$b^k \le 10^9$$$ for each day.

Output

For each day, output a single integer on its own line — the minimum number of coins needed to give exactly $$$x$$$ cents in change.

Examples
Input
1
3 3 35
Output
5
Input
5
10 2 1234
2 0 1000000000
1000000000 1 1
2 29 536870912
10 9 1000000000
Output
19
1000000000
1
1
1
Input
3
2 3 20
5 1 20
10 0 20
Output
3
4
20
Input
1
1000000000 1 999999999
Output
999999999
Note

In the first sample, the coin values are $$$1$$$, $$$3$$$, $$$9$$$, and $$$27$$$. Magikarp can give $$$35$$$ cents using five coins: $$$27+3+3+1+1=35$$$.

In the second sample:

  • On the first day, the coin values are $$$1$$$, $$$10$$$, and $$$100$$$. Magikarp needs twelve $$$100$$$-cent coins, three $$$10$$$-cent coins, and four $$$1$$$-cent coins, for a total of $$$19$$$ coins.
  • On the second day, $$$k=0$$$, so only $$$1$$$-cent coins are available. Magikarp needs $$$10^9$$$ coins.
  • On the third day, the change is $$$1$$$ cent, so one $$$1$$$-cent coin is enough.
  • On the fourth day, one coin worth $$$2^{29}$$$ cents is enough.
  • On the fifth day, one coin worth $$$10^9$$$ cents is enough.

In the third sample, the change is $$$20$$$ cents on every day, but the coin values change. Magikarp can use $$$8+8+4$$$ on the first day and $$$5+5+5+5$$$ on the second day. On the third day, he must use twenty $$$1$$$-cent coins.

In the fourth sample, the only coin values are $$$1$$$ and $$$10^9$$$. The larger coin exceeds the required change, so Magikarp must use only $$$1$$$-cent coins.

C. Trail Running
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In a trail race, there is no shame in walking for part of the race! Walking up a steep hill can save energy and allow a runner to achieve a better overall time.

You are running a trail race this weekend, and you know that your body can expend a total of $$$E$$$ units of energy before you'll be too exhausted to finish the race. For each mile of the race, you can choose to either walk or run. Each strategy consumes a certain amount of energy and takes a certain amount of time.

Compute the fastest you can finish the race.

Input

The first line of input contains two space-separated integers $$$N$$$ $$$(1 \leq N \leq 50)$$$ and $$$E$$$ $$$(1 \leq E \leq 1\,000)$$$, the number of miles in the race and the maximum energy you can consume, respectively.

Each of the next $$$N$$$ lines contains four space-separated integers $$$e_1$$$, $$$t_1$$$, $$$e_2$$$, and $$$t_2$$$ $$$(0 \leq e_1, e_2 \leq 1\,000; 1 \leq t_1, t_2 \leq 10\,000)$$$: the amount of energy it costs to walk the mile, how many seconds doing so takes, the amount of energy it costs to run the mile instead, and how many seconds running the mile takes.

Note that it is not necessarily the case that $$$e_1 \lt e_2$$$ or $$$t_1 \gt t_2$$$.

Output

Print a single integer: the fastest time (in seconds) you can complete the race. If it is impossible to finish the race no matter the strategy, print $$$-1$$$ instead.

Examples
Input
3 100
1 600 50 450
1 650 50 400
1 500 50 500
Output
1500
Input
3 100
40 125 35 100
35 200 35 180
50 500 31 430
Output
-1
Note

In the first sample, Magikarp can walk the first and third miles and run the second. This uses $$$52$$$ energy and takes $$$600+400+500=1500$$$ seconds, which is the minimum time possible.

In the second sample, even the least-energy choice on each mile uses $$$35+35+31=101$$$ energy, more than the limit of $$$100$$$. Therefore finishing is impossible.

D. Maximum Value Plot II: Plot Harder
time limit per test
10 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Some CS 314 students have asked you for help on a problem from their first homework assignment. You didn't get all of the details about the problem bounds, so you've set some limits that seem reasonable to you. Can you help them solve the problem?

You are given a rectangular $$$N \times M$$$ grid $$$g$$$ of integers, and the homework problem asks you to compute the maximum possible sum of entries in a contiguous rectangular sub-grid of the given grid. (A $$$0 \times 0$$$ sub-grid is a valid sub-grid and its entries sum to $$$0$$$. The entire grid $$$g$$$ also counts as a sub-grid of itself.)

In other words, compute $$$$$$\max_{0 \leq l \leq r \leq M \\ 0 \leq t \leq b \leq N} \sum_{i=l}^{r-1} \sum_{j=t}^{b-1} g_{ji}$$$$$$ where $$$g_{ji}$$$ is the grid entry in row $$$j$$$, column $$$i$$$.

Input

The first line of input contains two space-separated integers $$$N$$$ and $$$M$$$ $$$(1 \leq N, M \leq 500)$$$, the number of rows and columns in the grid $$$g$$$, respectively.

The next $$$N$$$ lines each contain $$$M$$$ space-separated integers each and describe the entries of $$$g$$$. All entries are in the range $$$[-10^6, 10^6]$$$.

Output

Print a single integer: the sum of the entries in the contiguous sub-grid of $$$g$$$ that maximizes this sum.

Examples
Input
4 4
7 -3 4 2
-1 8 -5 6
3 0 -9 10
-4 5 1 -8
Output
22
Input
2 3
-3 -1 -4
-1 -5 -9
Output
0
Input
4 5
-108793 -29395 -608138 -722622 407621
702232 -619216 391815 -778753 568439
426028 69966 885259 624653 -531945
887849 -702803 -494114 655225 155269
Output
2352063
Note

In the first sample, the sub-grid using rows $$$1$$$ through $$$3$$$ and columns $$$1$$$ through $$$4$$$ has sum $$$22$$$, which is maximum.

In the second sample, every entry is negative, so the valid empty sub-grid has the maximum sum, $$$0$$$.

In the third sample, the sub-grid using rows $$$3$$$ and $$$4$$$ and columns $$$1$$$ through $$$4$$$ has the maximum sum, $$$2352063$$$.

E. UTPC Code
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A UTPC Code is similar to a QR code: it is a square bitmap of white and black pixels that encodes data. Not any bitmap is a valid UTPC Code, though. Valid codes must satisfy the following parity constraint: in any $$$2\times 2$$$ contiguous block of pixels, the number of black pixels in the block must be even. In other words, the following $$$2\times 2$$$ blocks cannot appear anywhere inside a valid UTPC Code:


WB BW BB BB BW WB WW WW
BB BB WB BW WW WW BW WB

where W and B represent white and black pixels, respectively.

You are given an $$$N \times N$$$ grid with some pixel colors already filled in. Is it possible to complete the rest of the UTPC Code in a way that satisfies the parity constraint?

Input

First line of input contains a single integer $$$N$$$ $$$(1 \leq N \leq 10)$$$: the size of the grid. Then follow $$$N$$$ lines of exactly $$$N$$$ characters each. Each character will be one of W, B, or ., representing a white or black pixel already given at that location in the grid, or a blank spot that you need to fill in, respectively.

Output

Print POSSIBLE if it is possible to fill in all remaining . cells with a black or white pixel in a way that satisfies the UTPC parity constraint, and IMPOSSIBLE otherwise.

Examples
Input
3
W.W
BBB
...
Output
POSSIBLE
Input
5
WWBWB
W....
B...B
..W..
.W...
Output
IMPOSSIBLE
Note

In the first sample, one valid completion is


WWW
BBB
WWW

In the second sample, it is impossible to fill in the unknown pixels so that the completed grid satisfies the parity rule.