UTPC Contest 9-30-2026
A. But First, Coffee
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Lois Griffin has hung a new sign in the kitchen: "But first, coffee." Peter finds this deeply illogical, as you can't say something comes "first" without knowing what it's being compared against.

To settle the argument, Peter gathers a line of $$$n$$$ people in the kitchen, represented as a string $$$s$$$. Each person either wants coffee $$$(s_i = \texttt{C})$$$ or is having a life-threatening emergency $$$(s_i = \texttt{E})$$$.

Lois insists that coffee should always come first, so she wants to rearrange the line so that all $$$\texttt{C}$$$'s appear before all $$$\texttt{E}$$$'s. Lois can only perform the following operation, any number of times:

— Choose 3 indices $$$1 \leq i \lt j \lt k \leq n$$$ and right shift or left shift the values on $$$s_i$$$, $$$s_j$$$, $$$s_k$$$ cyclically.

For the string $$$\texttt{CCECCE}$$$, if she chooses $$$i=1, j=2, k=3$$$ and performs a right shift cyclically, the string becomes $$$\texttt{ECCCCE}$$$. If she chooses $$$i=4, j=5, k=6$$$ and performs a left shift cyclically instead, the string becomes $$$\texttt{CCECEC}$$$.

Determine the minimum number of operations Lois needs to rearrange $$$s$$$ so that all people who want coffee come before all people having emergencies.

Input

The first line contains $$$n$$$ $$$(3 \leq n \leq 10^5)$$$ — the length of the string.

The second line contains $$$s$$$, of length $$$n$$$. A character in $$$s$$$ will only be either $$$\texttt{C}$$$ or $$$\texttt{E}$$$.

Output

Output a single integer — the minimum number of operations required to sort the given string $$$s$$$.

Examples
Input
3
CCE
Output
0
Input
4
CEEC
Output
1
Input
6
EECECC
Output
2
Input
6
ECECEE
Output
1
Note

In the first sample, CCE is already in the required order, so the answer is $$$0$$$. In the second sample, one cyclic shift using positions $$$1$$$, $$$2$$$, and $$$4$$$ changes CEEC into CCEE, so one operation is enough.

B. The Amazing Adventures of Coffee Man!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

By day, Coffee Man is an ordinary office worker. But give him a cup of coffee and he becomes a superhero who can leap across the city in a single bound!

Coffee Man is standing at point $$$0$$$ of a number line, and he needs to get to point $$$n$$$ (reaching any point with coordinate at least $$$n$$$ is enough). He brought $$$m$$$ cups of coffee; the $$$i$$$-th cup initially has temperature $$$a_i$$$.

Every second, Coffee Man does exactly one of the following:

  • walk: his coordinate increases by $$$1$$$;
  • drink a cup he has not drunk yet: his coordinate increases by the current temperature of that cup.

Each cup can be drunk at most once. Unfortunately, coffee gets cold: after every second, the temperature of every cup decreases by $$$1$$$, but it never goes below $$$0$$$. In other words, if Coffee Man drinks the $$$i$$$-th cup when exactly $$$s$$$ seconds have passed, he moves forward by $$$\max(0, a_i - s)$$$.

What is the minimum number of seconds Coffee Man needs to reach a point with coordinate at least $$$n$$$?

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^9$$$, $$$1 \le m \le 2 \cdot 10^5$$$) — the coordinate Coffee Man needs to reach and the number of cups of coffee.

The second line of each test case contains $$$m$$$ integers $$$a_1, a_2, \ldots, a_m$$$ ($$$1 \le a_i \le 10^9$$$) — the initial temperatures of the cups.

It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output a single integer — the minimum number of seconds Coffee Man needs to reach a point with coordinate at least $$$n$$$.

Example
Input
4
10 3
1 3 5
3 1
100
5 2
1 1
20 4
4 4 4 4
Output
5
1
5
14
Note

In the first test case, $$$n = 10$$$ and the temperatures are $$$[1, 3, 5]$$$. One optimal plan is:

  • second $$$1$$$: drink the cup with temperature $$$5$$$, moving to coordinate $$$5$$$;
  • second $$$2$$$: drink the cup that started at temperature $$$3$$$; it has cooled to $$$2$$$, so Coffee Man moves to coordinate $$$7$$$;
  • seconds $$$3$$$, $$$4$$$, $$$5$$$: walk, reaching coordinate $$$10$$$.

It is not possible to do better, so the answer is $$$5$$$. Note that drinking all three cups in the given order is worse: the cups give $$$1$$$, $$$2$$$ and $$$3$$$, so after $$$3$$$ seconds Coffee Man is at coordinate $$$6$$$ and needs $$$4$$$ more seconds of walking, $$$7$$$ seconds in total.

In the second test case, a single cup moves Coffee Man from $$$0$$$ to $$$100 \ge 3$$$.

In the third test case, the cups are useless: the first one moves him by $$$1$$$, which is no better than walking, and after that every cup has cooled down to $$$0$$$. The answer is $$$5$$$.

In the fourth test case, drinking all four cups gives $$$4+3+2+1=10$$$ units of movement in four seconds, after which walking takes ten more seconds. The total is $$$14$$$ seconds.

C. Reject Modernity, Embrace Tradition
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It's 4 PM on a Wednesday, and Bevo just finished a 2-hour OS lecture. He signed up for the UTPC contest this evening, and needs some caffeine to keep him going. But unlike every other CS student, Bevo doesn't LinkedInMaxx. Bevo doesn't larp. Bevo doesn't network. Bevo doesn't swing by Up and Atom in Welch for matcha or buy an $8 coffee from Lucky Lab. Bevo only drinks black coffee straight from the pot that costs 2 cents to make, like a middle-aged man who has a wife and three kids at home. To do this, he must quickly head back to his apartment so he can depart for the contest on time.

Consider the GDC as location $$$1$$$ and Bevo's apartment as location $$$N$$$. UT campus has $$$N - 2$$$ street corners, numbered $$$2$$$ through $$$N - 1$$$. Each street connects two of these locations (a corner, the GDC, or the apartment) and is accompanied by an estimated time to walk across it. Streets are two-way. Help Bevo rush home in the shortest possible time so he can refuel.

Input

The first line contains two integers $$$N$$$ and $$$S$$$ ($$$2 \le N \le 10^5$$$, $$$1 \le S \le 2 \cdot 10^5$$$) — the number of locations (including the GDC and the apartment) and the number of streets connecting them.

Each of the next $$$S$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \le a, b \le N$$$, $$$a \ne b$$$, $$$1 \le c \le 10^9$$$) — a street connecting locations $$$a$$$ and $$$b$$$ that takes $$$c$$$ time to walk from $$$a$$$ to $$$b$$$ or vice versa.

It is guaranteed that at least one path exists from the GDC (location $$$1$$$) to Bevo's apartment (location $$$N$$$).

Output

Print a single integer — the shortest possible time for Bevo to get from the GDC to his apartment.

Examples
Input
5 6
1 2 2
1 3 4
2 3 1
2 4 7
3 5 3
4 5 1
Output
6
Input
2 1
1 2 10
Output
10
Note

In the first example, the fastest route is $$$1 \to 2 \to 3 \to 5$$$ with total time $$$2 + 1 + 3 = 6$$$.

D. Cappuccina's Coffee Pairing
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ballerina Cappuccina has opened a coffee tasting stand and prepared $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ cups of coffee, where $$$n$$$ is odd.

Each cup has an integer strength. More formally, let $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$ be the strengths of the cups.

Before the tasting begins, Cappuccina must discard exactly one cup. She then pairs up all of the remaining cups.

The cost of pairing two cups with strengths $$$x$$$ and $$$y$$$ is $$$|x-y|$$$. The total cost is the sum of the costs of all pairs.

Cappuccina wants the flavors in each pair to be as similar as possible. Help her determine the minimum total cost she can achieve by choosing which cup to discard and how to pair the remaining cups optimally.

Input

The first line contains a single odd integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$, the number of cups of coffee.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, where $$$a_i$$$ represents the strength of the $$$i$$$-th cup.

Output

Output a single integer, the minimum possible total pairing cost after Cappuccina discards exactly one cup and pairs all remaining cups optimally.

Example
Input
5
3 10 1 11 4
Output
2
Note

In the first test case, the answer is 2 as Cappuccina can remove $$$1$$$ (the third element) and pair up $$$(3, 4)$$$ and $$$(10, 11)$$$. $$$|10 - 11| + |3 - 4| = 2$$$, and it can be shown that this is the minimum score she can achieve.

caffeinate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Johnny is a busy man – he has many events scheduled on his calendar. As Johnny has some school assignments that he hasn't completed yet, he wants to keep Claude Code running on his computer for as long as possible, but whenever he is away at an event, his computer shuts down immediately by default to save power. Fortunately, Johnny can use the command line utility caffeinate to keep his computer running. In particular, if Johnny uses the command caffeinate, his computer will stay on for $$$T$$$ minutes, regardless of whether or not Johnny uses his computer in that time window.

Johnny can use the caffeinate command up to $$$k$$$ times at any moment he chooses (even if he is away at an event). Given $$$n$$$ events on Johnny's calendar, Johnny wants to know – what is the maximum amount of time Johnny can run Claude Code for when he is away at events?

Input

The first line contains three space-separated integers $$$n$$$, $$$k$$$, $$$T$$$ ($$$1 \le n, k \le 100$$$, $$$1 \le T \le 10^9$$$) — the number of events Johnny has, the maximum allowed number of caffeinate usages, and the time in minutes that caffeinate keeps Johnny's computer on, respectively.

The next $$$n$$$ lines contain two integers each, where the $$$i$$$-th line contains two space-separated integers $$$l_i, r_i$$$ ($$$0 \le l_i \lt r_i \le 10^9$$$) — the start and end time in minutes of Johnny's $$$i$$$-th event on his calendar. It is guaranteed that no events have a nonzero time overlap, although events can share endpoints.

Output

Output a single integer — the maximum amount of time Johnny can run Claude Code for when he is away at events.

Examples
E. caffeinate
3 2 4
0 3
5 6
10 14
Output
7
Input
1 1 5
0 100
Output
5
Note

In the first sample test case, Johnny can run caffeinate at time $$$0$$$ and at time $$$10$$$. This ensures that his computer stays on for the entirety of events $$$1$$$ and $$$3$$$, which gives a total time of $$$7$$$ minutes.

In the second sample test case, Johnny can run caffeinate at time $$$50$$$ and keep his computer running for $$$5$$$ minutes.

F. Coffee is Life
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

On his voyage to hunt the elusive white whale, Captain Ahab is swamped with tasks to do during day-to-day life on his ship. The good news is that he has the life-sustaining power of coffee on his side. The bad news is that there is a limited amount of coffee in his ship's hold. There are $$$n$$$ days, each with some amount of work $$$w_i$$$ to do. Each unit of work costs 1 energy, and every day starts with 0 energy.

There are $$$m$$$ cups of coffee that can be drunk on any day. The first coffee drunk on any day gives $$$k$$$ energy, the second gives $$$k-1$$$ energy, and so on, until the $$$k^{th}$$$ coffee gives $$$1$$$ energy; any coffee drunk afterward gives no energy.

Ahab needs your help to ration his coffee to complete as much work as possible.

Input

The first line of input contains $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n \le 10^5, 1 \le m, k \le 10^9$$$), the number of days, cups of coffee, and initial energy from coffee, respectively.

The second line contains $$$n$$$ integers, $$$w_1, w_2, ..., w_n$$$ ($$$1 \le w_i \le 10^{18}$$$), the amount of work to be done each day.

Output

The output should be one line containing the maximum possible amount of work that can be done.

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

It is optimal for Ahab to drink two coffees on the first day, one on the second day, and one on the third day for $$$5+2+3 = 10$$$ units of work total.

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

Jihyo and Dahyun are tired from songwriting, so they decide to play a game to kill time. They usually play janggi (Korean chess), but decide to try out a new game.

They have a computer that randomly creates a directed graph: $$$n$$$ circles are drawn on the table, and $$$m$$$ arrows connect from one node (circle) to another. It is guaranteed no arrow will point from a node to itself, and it is guaranteed there won't be multiple arrows that have the same start and end node. However, note it is possible (as an example) to have an arrow point from Node 1 to Node 2, and another arrow point from Node 2 to Node 1. Furthermore, each node is either painted apricot (for Jihyo) or kept white (for Dahyun).

When they're ready to play, the computer will randomly pick a starting node for Jihyo among all of the apricot nodes, and a starting node for Dahyun among all of the white nodes. The two will then take turns moving, with Jihyo always going first. When it is a player's turn, they must pick an edge pointing out from their node and travel to where it points. A player is always allowed to re-visit a node, but they can never travel to a node that their opponent is currently on. If a player doesn't have any valid moves, the game ends and that player loses. If after $$$10^7$$$ moves (since both players are very patient) the game does not end, it ends in a draw. Both players will naturally play optimally (preferring wins to draws and draws to losses).

Dahyun is convinced that the computer could potentially be rigged against her, especially since she always has to go second. To test her theory, she wants to figure out the probability the game ends in a draw and the probability game ends in her loss. Can you help her find this out quickly?

Input

The first line contains two integers $$$n$$$ ($$$2 \le n \le 10^3$$$) and $$$m$$$ ($$$1 \le m \le \min(10^4, n(n-1))$$$), the number of nodes and edges, respectively.

The second line contains a binary string $$$s$$$ of length $$$n$$$, where $$$s_i=1$$$ means the $$$i$$$th node is a Jihyo node and $$$s_i=0$$$ means it is a Dahyun node. It is guaranteed that $$$s$$$ contains at least one $$$1$$$ and at least one $$$0$$$.

The next $$$m$$$ lines each contain two integers $$$a$$$ and $$$b$$$ ($$$1 \le a,b \le n$$$), denoting a directed edge from node $$$a$$$ to node $$$b$$$.

Output

Print two space-separated decimals, representing (respectively) the probability Dahyun draws and that she loses.

Each of your answers will be marked correct if the absolute or relative error doesn't exceed $$$10^{-6}$$$. That is if your answer is $$$x$$$ and the jury's answer is $$$y$$$, your answer will be accepted iff $$$\frac{|x-y|}{\max(1,|y|)} \le 10^{-6}$$$.

Example
Input
5 4
10100
1 2
2 1
3 4
4 3
Output
0.33333333 0.33333333
Note

Let $$$(u,v)$$$ denote the state where (at the beginning of the game) Jihyo is at node $$$u$$$ and Dahyun is at node $$$v$$$. The below table breaks down the cases:

StateWinner
$$$(1,2)$$$Dahyun
$$$(1,4)$$$Draw
$$$(1,5)$$$Jihyo
$$$(3,2)$$$Draw
$$$(3,4)$$$Dahyun
$$$(3,5)$$$Jihyo

$$$^\dagger$$$If it wasn't obvious, this problem is also based on a song.

There are six equally likely starting pairs. Two lead to a draw and two lead to Jihyo winning, so each requested probability is one third.

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

Lucky Lab A is west of Guadalupe Street, while Lucky Lab B is east of it. There are $$$n$$$ loyal customers of Lucky Lab A waiting on the east sidewalk and $$$m$$$ loyal customers of Lucky Lab B waiting on the west sidewalk. Customer $$$i$$$ of Lucky Lab A is at vertical position $$$a_i$$$, and customer $$$i$$$ of Lucky Lab B is at vertical position $$$b_i$$$. Multiple customers may begin at the same position.

Before crossing, every customer may walk along their current side of the street to any integer vertical position. They then cross Guadalupe in a straight horizontal line. Customers traveling in the same direction may cross together, but a customer heading west and a customer heading east cannot use the same vertical position, or they would collide.

Walking from vertical position $$$x$$$ to vertical position $$$y$$$ costs $$$|x-y|$$$. Find the minimum possible total walking cost that lets everyone cross without a collision.

Input

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n,m\le 2000$$$) — the number of customers loyal to Lucky Lab A and Lucky Lab B, respectively.

The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\le a_i\le n+m$$$) — the starting vertical positions of the customers loyal to Lucky Lab A.

The third line contains $$$m$$$ integers $$$b_1,b_2,\ldots,b_m$$$ ($$$1\le b_i\le n+m$$$) — the starting vertical positions of the customers loyal to Lucky Lab B.

Output

Output a single integer — the minimum possible total walking cost.

Examples
Input
3 3
1 2 3
1 2 3
Output
3
Input
1 1
1
1
Output
1
Note

In the first sample, all three Lucky Lab B customers walk vertically to position $$$2$$$ on their side, then cross. The Lucky Lab A customers at positions $$$1$$$ and $$$3$$$ cross without moving, while the customer starting at position $$$2$$$ walks to position $$$1$$$ before crossing. The total cost is $$$3$$$.

In the second sample, one customer must walk one unit before crossing because customers traveling in opposite directions cannot both cross at position $$$1$$$.