Second Round 2025/2026

Overview

Welcome to the Second Round of the Swiss Olympiad in Informatics! You will find below an overview of what you’ve done so far as well as a few things that you should know before getting started.

Contest ends at 2025-11-30T23:59:59+01:00

CategoryTaskTotalSubtask 1Subtask 2Subtask 3Subtask 4Subtask 5Subtask 6Subtask 7
Junior onlyregistanpattern‒/100‒/11‒/17‒/29‒/43
Junior onlytimuridriders‒/100‒/8‒/9‒/14‒/18‒/24‒/27
Bothmousedances‒/100‒/6‒/12‒/15‒/13‒/18‒/14‒/22
Bothfire‒/100‒/12‒/19‒/21‒/22‒/26
Bothroborbuy‒/100‒/10‒/15‒/10‒/25‒/40
Bothbukhara‒/100‒/7‒/15‒/18‒/27‒/33
Regular onlytombexploration‒/100‒/17‒/18‒/24‒/41
Regular onlyevilruins‒/100‒/6‒/10‒/19‒/8‒/14‒/43
Regular only2h‒/100‒/25‒/25‒/25‒/25

Tips

You may use any programming language to solve the tasks of the Second Round. To solve a practical task, you have to design a program, download an input file, run the program on the data in that file, write the results in another file and upload it along with the source code to the website, which will grade your submission and display the number of points that you’ve earned as well as an explanation if you did not get all the possible points. In order to read the data in the input file, make sure that your program either reads directly in that file or redirect the standard input to it (in a terminal running on Linux, Windows or macOS, you can do that by using the command ‘yourprogram < inputfile’, replacing ‘yourprogram’ and ‘inputfile’ by the paths to the actual program and input file). More elaborate explanations about how to solve the tasks of the Second Round await you on our wiki and our training page. Note that in case you get stuck on the tasks, you can move to next task and try again later: no need to solve them in order.

You should also hear about our workshops! In October and November, we offer workshops about algorithmics. It’s a great opportunity to learn all you need for the Second Round and to meet other like-minded participants. Whether you’re a beginner or an experienced programmer, it’s worth it to come. More info and registration here.

Don’t hesitate to contact us at info@soi.ch if you have any question about programming or the tasks.

Rules and Prizes

To participate, you need to either have qualified in the first round or scored 500 points in the pre-round. It may take up to five minutes after adding an additional email address in your profile or submitting something in the pre-round for your qualification status to be updated.

The Second Round has two categories: Junior and Regular. You can participate in the Junior category if you are eligible to participate in this year’s edition of SOI as well as the two next ones. If you are eligible as a junior, you can choose to participate in the Junior or in the Regular category or in both.

You qualify for the finals and the Sarnen Camp if you are placed under the

  • top 8 of the Junior category, and/or
  • top 16 of the Regular category.

Additionally, we may distribute wildcards.

We wish you good luck, hope you will enjoy solving our tasks and look forward to meeting you at our workshops.

Registan Pattern

Mouse Stofl and Mouse Binna have finally made it to Uzbekistan’s beautiful registans, which are full of colorful patterns.

Beautiful Registan patterns

Stofl found similarities in the patterns of the registan and he became artistically inspired! Although this time, Stofl had a way too crazy idea.

They came to a wall which is one meter tall and several meters long. The inspiring characteristics of the wall was its modular design every meter. Stofl’s idea was to cut the wall into one meter parts in order to rearrange them, which results in a new but still continuous pattern. However, Binna disliked his idea very much. She doesn’t care about the building itself, but she doesn’t like the lack of innovation. She thinks just rearranging the patterns is boring, so she gave him the challenge to make a unique pattern by dividing the wall into segments of several meters and then flipping all the segments in place.

However, Stofl has already assigned for each meter the position it should go. To impress Binna, he needs to find out how he can divide the wall such that he achieves his original vision by flipping the segments.

Subtask 1: A short wall (11 points)

Stofl has a wall which is 33 meters long. Can you help him determine how he should divide the wall?

Input

The first line contains the number of test cases TT. For every test case, two lines will follow:

  • The first line contains the integer NN, the length of the wall in meters.
  • The second line contains NN integers aia_i for 0i<N0 \le i < N, describing Stofl’s assignment. The ii-th meter of the wall should move to the aia_i-th meter.

Output

For the tt-th test case, if it is not possible to achieve Stofl’s assignment by flipping segments, print “Case #t: Impossible”. Otherwise, print “Case #t: Possible” and two additional lines:

  • The first line contains the number of segments MM.
  • The second line contains MM integers sjs_j, where sjs_j is the length of the jj-th segment.

Limits

  • T=100T = 100
  • N=3N = 3
  • 0ai<N0 \le a_i < N for all ii, all aia_i are distinct

Example

Input:

2
3
0 2 1
3
2 0 1

Output:

Case #0: Possible
2
1 2
Case #1: Impossible

Solution for Case #0:

Example pic

Dividing the wall into these two segments achieves that each meter of the wall ends up where Stofl wants it. Meter 1 is flipped by itself and thus remains where it is, while meters 2 and 3 are swapped.

To submit for this subtask, please log in.

Subtask 2: The entrance (17 points)

The walls at the entrance are up to 2020 meters long! Can you still help Stofl?

Input

Same as subtask 1.

Output

Same as subtask 1.

Limits

  • T=100T = 100
  • 3N203 \le N \le 20
  • 0ai<N0 \le a_i < N for all ii, all aia_i are distinct

Example

Input:

3
3
1 0 2
5
1 0 4 3 2
6
2 1 5 4 3 0

Output:

Case #0: Possible
2
2 1
Case #1: Possible
2
2 3
Case #2: Impossible

To submit for this subtask, please log in.

Subtask 3: All the walls (29 points)

The registan is huge! Despite that, the patterns have still the same characteristics everywhere. Stofl would be overjoyed if you could help him out on this ambitious task.

Input

Same as previous subtasks.

Output

Same as previous subtasks.

Limits

  • T=100T = 100
  • 3N50003 \le N \le 5\,000
  • 0ai<N0 \le a_i < N for all ii, all aia_i are distinct

To submit for this subtask, please log in.

Subtask 4: All the registans (43 points)

Binna wasn’t impressed so far. So if this didn’t impress Binna, surely he needs to go even bigger than that and combine all walls of every registan in Uzbekistan.

Input

Same as previous subtasks.

Output

Same as previous subtasks.

Limits

  • T=100T = 100
  • 3N2000003 \le N \le 200\,000
  • 0ai<N0 \le a_i < N for all ii, all aia_i are distinct

To submit for this subtask, please log in.

Don’t hesitate to ask us any question about this task, programming or the website via email (info@soi.ch).

Timurid Riders

During the late Middle Ages, Uzbekistan was the heartland of the Timurid Empire, stretching from central Anatolia all the way to northern India, with Samarkand as its capital.

Binnas Birthday is coming up, and Mouse Stofl wants to prepare a special surprise for her. He knows that she is fascinated by the history of the Timurid Empire, and that she loves puzzles. So he came up with a puzzle based on the Timurid Empire.

The Timurid Empire in the puzzle consists of NN villages, including the capital, and N1N-1 roads between them, on which riders can travel. The capital Samarkand is always village 00. The villages bordering the wilderness have watchtowers to guard the borders. These villages can be recognized because they have exactly one road leading out and are not the capital. All other villages, including the capital, contain fortresses.

The puzzle further follows these rules:

  1. Riders may only be dispatched from the villages with watchtowers.
  2. Each rider travels only inward, toward the capital, never outward.
  3. Each rider continues travelling until it has reached the capital or the next village is already occupied.
  4. A new rider may only be dispatched once the previous rider has reached its final destination.

The goal is to decide from which watchtower to release each rider, and in what order, so that every fortress in the empire is manned in the least possible total time.

Stofl asked you to solve the puzzle, so that he can see if it is fit for Binna.

Subtask 1: Orders of Samarkand (8 points)

To test your understanding of the rules, Mouse Stofl wants you to predict which fortress each rider will stop at, given a list of dispatch orders.

Input

The first line contains the number of test cases TT. TT test cases follow in the following format:

  • The first line contains the integer NN, the number of villages (with watchtowers or fortresses).
  • Each of the N1N-1 following lines contain the integers ai,tia_i, t_i, meaning there is a road from village i+1i+1 to village aia_i which takes tit_i hours to ride on.
  • The next line contains the integer MM, the number of riders which will be dispatched.
  • The next line contains MM integers, d0,d1,,dM1d_{0}, d_{1}, {\dots}, d_{M-1}, the cities with the watchtowers from which the riders will be dispatched (in this order).

Output

For the ii-th test case, output a line “Case #i: r_0 r_1 ... r_{M-1}”, where rir_i is the city in which rider ii will halt.

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1032 \le N \le 10^{3}
  • 1M1031 \le M \le 10^{3}
  • 1ti1061 \le t_i \le 10^{6} for all 0i<N0 \le i < N

Example

Input:

3
3
0 1
0 2
2
1 2
3
0 5
1 3
3
2 2 2
6
4 1
0 1
2 3
0 2
2 5
3
3 1 3

Output:

Case #0: 0 2
Case #1: 0 1 2
Case #2: 0 4 2

Comment:

In the first testcase, the first rider will start at village 11, then go to village 00. The next rider starts at village 22, but the next village, 00, is already occupied, so they stay at village 22.

In the second testcase, the first rider will start at village 22, then go to village 11, then go to village 00.

Disclaimer: Generation may take a few seconds.

To submit for this subtask, please log in.

Subtask 2: Caravan Trail to Samarkand (9 points)

Being satisfied that you understood the rules, Mouse Stofl will first give you an easier version of the puzzle, taking place along a caravan trail from the furthest away village all the way to Samarkand. In this version, there is exactly one capital and one village with a watchtower. All other villages have exactly one road leading in and one leading out.

Input

The first line contains the number of test cases TT. TT test cases follow in the following format:

  • The first line contains the integer NN, the number of cities and watchtowers combined.
  • Each of the N1N-1 following lines contain the integers ai,tia_i, t_i, meaning there is a road from village i+1i+1 to village aia_i which takes tit_i hours to ride on.

Output

For the ii-th test case, output a line “Case #i: K”, where KK is the minimum total time required to man every fortress.

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1062 \le N \le 10^{6}
  • 1ti1061 \le t_i \le 10^{6} for all 0i<N0 \le i < N

Note: You may exceed the maximum recursion depth.

Example

Input:

3
3
0 1
1 2
2
0 5
4
0 3
1 2
2 4

Output:

Case #0: 5
Case #1: 5
Case #2: 19

Comment:

In the first testcase, is it best to dispatch the first rider from the watchtower in city 22, who will take 33 hours to reach village 00. Then, it is best to dispatch a second rider from watchtower in city 22, who will take 22 hours to reach village 11. Now, every fortress has been manned in 3+2=53+2 = 5 hours.

Disclaimer: Generation may take a few seconds.

To submit for this subtask, please log in.

Subtask 3: Equal Roads of Bukhara (14 points)

Next, Mouse Stofl gives you a map centered around Bukhara. The unique feature is that all roads have length 11.

Input

Same as Subtask 2

Output

Same as Subtask 2

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1062 \le N \le 10^{6}
  • ti=1t_i = 1 for all 0i<N0 \le i < N

Example

Input:

3
3
0 1
0 1
4
0 1
3 1
0 1
6
4 1
0 1
2 1
0 1
2 1

Output:

Case #0: 1
Case #1: 2
Case #2: 4

Disclaimer: Generation may take a few seconds.

To submit for this subtask, please log in.

Subtask 4: Balanced Roads of Shahrisabz (18 points)

In this subtask, Mouse Stofl gives you a map of the surroundings of Shahrisabz. The unique feature is that the capital has the same distance to every watchtower.

Input

Same as Subtask 2

Output

Same as Subtask 2

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1062 \le N \le 10^{6}
  • 1ti1061 \le t_i \le 10^{6} for all 0i<N0 \le i < N

Example

Input:

3
3
0 3
0 3
4
0 3
3 2
0 1
6
4 1
0 1
2 2
0 2
2 2

Output:

Case #0: 3
Case #1: 5
Case #2: 6

Disclaimer: Generation may take a few seconds.

To submit for this subtask, please log in.

Subtask 5: A Province of the Realm (24 points)

In this subtask, Mouse Stofl gives you a map of a small portion of the Timurid Empire, with a small number of villages and roads.

Input

Same as Subtask 2

Output

Same as Subtask 2

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1032 \le N \le 10^{3}
  • 1ti1061 \le t_i \le 10^{6} for all 0i<N0 \le i < N

Example

Input:

3
3
0 1
0 2
2
0 5
6
4 1
0 1
2 3
0 2
2 5

Output:

Case #0: 1
Case #1: 5
Case #2: 7

Comment:

In the first testcase, it is best to dispatch the first rider from the watchtower in city 11, who will ride to village 00 in 11 hour.

In the second testcase, there is only one option: dispatching a rider from the watchtower in city 11.

Disclaimer: Generation may take a few seconds.

To submit for this subtask, please log in.

Subtask 6: The Empire of Timur (27 points)

Mouse Stofl is now confident that you can solve the original puzzle with a map of the entire Timurid Empire.

Input

Same as Subtask 2

Output

Same as Subtask 2

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1062 \le N \le 10^{6}
  • 1ti1061 \le t_i \le 10^{6} for all 0i<N0 \le i < N

Disclaimer: Generation may take a few seconds.

To submit for this subtask, please log in.

Don’t hesitate to ask us any question about this task, programming or the website via email (info@soi.ch).

Mouse Dances

Mouse Stofl and friends are hosting a grand dance festival. All through the night, mice gather next to each other holding paws with their neighbors, swaying to the music, spinning in step, and pausing now and then for a nibble of cheese before the next melody begins.

But dancing is not always easy. When neighbors differ too much in height, their little paws strain with effort, their backs stretch or stoop too much and before long the mismatched neighbors grow tired and let go. The greater the height difference, the greater the inconvenience — and the sooner the dance floor empties.

To keep the celebration alive until sunrise, the mice must choose their places wisely: arranging themselves so that the maximum inconvenience between any two neighbors is as small as possible. Depending on the melody, they may form a line, gather in a circle, or even whirl apart into two circles — but always with the same goal: minimize the maximum inconvenience, and keep the dancing going through the night.

For each subtask, help Stofl arrange the mice for a different melody so that he can minimize the maximum inconvenience between neighbors.

Input

All subtasks use the same input format:

The first line contains the number of test cases TT. For each test case you get two lines:

  • The first line contains a single integer nn — the number of mice.
  • The second line contains nn integers: the heights of the mice a0,a1,,an1a_{0}, a_{1}, {\dots}, a_{n-1}.

For all subtasks we have 2n2 \le n, and 0ai1090 \le a_i \le 10^{9}.

Output

For the tt-th testcase, output a single line containing “Case #t: x” where xx is the smallest possible maximum inconvenience. For a given arrangement, the maximum inconvenience is defined as the maximum absolute difference between heights of adjacent mice. Then you may need to output additional lines depending on the subtask. (see each subtask’s “Output format”). When an arrangement is requested, it must be a permutation of the input heights.

Subtask 1: Tiny line (6 points)

The first melody is danced in a line. Help Stofl arrange a few mice in a line so that the maximum inconvenience is minimized.

Limits

  • 2n32\le n\le 3

Output format

Output two lines per test case: 1. A single integer — the minimal possible maximum inconvenience. 2. nn integers — a valid optimal arrangement (the order in the line).

Example

Input:

2
2
2 1
3
1 5 2

Output:

Case #0: 1
2 1
Case #1: 3
1 2 5

To submit for this subtask, please log in.

Subtask 2: Medium line (12 points)

The second melody is also danced in a line. Help Stofl arrange a larger group of mice in a line so that the maximum inconvenience is minimized.

Limits

  • 2n10002 \le n\le 1000

Output format

Output two lines (same as Subtask 1).

To submit for this subtask, please log in.

Subtask 3: Huge line (15 points)

The third melody is also danced in a line and it is such a popular song that not just Stofl’s village but several other villages march into the dance floor to join in. Help Stofl arrange a huge crowd of mice in a line so that the maximum inconvenience is minimized. To keep output small, output only the optimal value of inconvenience and not the actual arrangement.

Limits

  • 2n10000002 \le n \le 1\,000\,000

Output format

Output one line containing a single integer — the smallest possible maximum inconvenience.

Example

Input:

2
2
2 1
3
1 5 2

Output:

Case #0: 1
Case #1: 3

To submit for this subtask, please log in.

Subtask 4: Medium circle (13 points)

The fourth melody is danced in a circle, so that the last mouse is a neighbor of the first mouse. Arrange the mice so that the maximum inconvenience is minimized.

Limits

  • 3n10003 \le n \le 1000

Output format

Output two lines: 1. A single integer — the minimal possible maximum inconvenience on the circle. 2. nn integers — a valid optimal circle order (print in a list; wrap-around is implied between last and first mouse).

Example

Input:

2
2
2 1
6
8 5 6 10 10 9

Output:

Case #0: 1
2 1
Case #1: 3
5 8 10 10 9 6

To submit for this subtask, please log in.

Subtask 5: Huge circle (18 points)

The fifth melody is also danced in a circle, with a huge crowd of mice dance joining in. Arrange the mice optimally, and to keep output small, output only the optimal value of inconvenience and not the actual arrangement.

Limits

  • 2n10000002 \le n \le 1\,000\,000

Output format

Output one line containing a single integer — the minimal possible maximum inconvenience.

Example

Input:

2
2
2 1
6
8 5 6 10 10 9

Output:

Case #0: 1
Case #1: 3

To submit for this subtask, please log in.

Subtask 6: Two medium circles (14 points)

To dance the sixth melody, the mice split into two circles, both non-empty. Arrange the mice in a way that minimizes the maximum inconvenience seen across the two circles.

Limits

  • 2n10002 \le n \le 1\,000

Output format

Output four lines: 1. A single integer — the smallest possible maximum over the two circles’ maximum inconvenience values. 2. Two integers: 12\ell_{1}\ell_{2} — lengths of the two circles (both >0> 0, 1+2=n\ell_{1}+ \ell_{2}= n). 3. 1\ell_{1} integers — a valid arrangement for the first circle 4. 2\ell_{2} integers — a valid arrangement for the second circle

Example

Input:

2
2
2 1
7
8 5 6 10 10 9 1

Output:

Case #0: 0
1 1
2
1
Case #1: 3
6 1
5 8 10 10 9 6
1

To submit for this subtask, please log in.

Subtask 7: Two huge circles (22 points)

For the seventh melody, a huge crowd of mice split into two circles, both non-empty. Arrange them in a way that minimizes the maximum inconvenience seen across the two circles. To keep output small, output only the optimal value of inconvenience and not the actual arrangement.

Limits

  • 2n10000002 \le n \le 1\,000\,000

Output format

Output one line containing a single integer — the smallest possible maximum over the two circles’ maximum inconvenience values.

Example

Input:

2
2
2 1
7
8 5 6 10 10 9 1

Output:

Case #0: 0
Case #1: 3

To submit for this subtask, please log in.

Don’t hesitate to ask us any question about this task, programming or the website via email (info@soi.ch).

Fire

Mouse Binna is on an adventure in a vast forest that is modeled as an N×NN \times N grid. Lately, the weather has been extremely dry, and the forest is about to catch fire. Binna is located at the top-left corner of the grid (0,0)(0, 0), while an underground shelter lies at the bottom-right corner (N1,N1)(N-1, N-1), offering safety from the fire.

At time 00, fire starts in MM distinct cells of the grid — these are called fire sources. At each unit of time, the fire spreads:

  • any cell that is currently on fire will remain on fire, and
  • its 44 direct neighbors (up, down, left, right) also catch fire.

Binna wants to know the latest time LL such that a safe path exists from her current location (0,0)(0, 0) to the underground shelter (N1,N1)(N-1, N-1). Both endpoints and all cells on the path must not be on fire at time LL. Moves are allowed only in the 44 directions: up, down, left, right. If no such path exists even at time 00, then L=1L=-1.

Subtask 1: Single fire source (12 points)

At time 00, there is exactly one fire source.

Input

The first line contains the number of test cases TT. TT test cases follow in the following format:

  • The first line contains two integers NN, MM, representing the size of the grid and the number of fire sources.
  • Each of the next MM lines contains two integers XiX_i and YiY_i — the row and column of a fire source.

Output

For each test case, output a line “Case #i: L”, where LL represent the latest time such that there still exists a safe path, or 1-1 if no safe path exists even at time 00.

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1092 \le N \le 10^{9}
  • M=1M = 1
  • 0Xi,YiN10 \le X_i, Y_i \le N-1 for all 0iM10 \le i \le M-1

Example

Input:

3
7 1
1 3
4 1
2 1
4 1
1 1

Output:

Case #0: 2
Case #1: 1
Case #2: 0

Comment:

Case #0: The grid evolves as follows. From time 33 onward, there is no safe path to the underground shelter, so the latest time is 22.
example of subtask 1-1
example of subtask 1-2

To submit for this subtask, please log in.

Subtask 2: Aligned fire sources (19 points)

All fire sources are in the same row.

Input

Same as Subtask 1

Output

Same as Subtask 1

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1092 \le N \le 10^{9}
  • 1M50001 \le M \le 5000
  • 0Xi,YiN10 \le X_i, Y_i \le N-1 for all 0iM10 \le i \le M-1
  • Xi=XjX_i=X_j for all 0i,jM10 \le i,j \le M-1

Example

Input:

2
7 3
4 0
4 1
4 3
20 2
15 2
15 6

Output:

Case #0: 2
Case #1: 12

Comment:

Case #0: The grid evolves as follows. From time 33 onward, there is no safe path to the underground shelter, so the latest time is 22.
example of subtask 2-1
example of subtask 2-2

To submit for this subtask, please log in.

Subtask 3: Small grid (21 points)

The grid size is limited.

Input

Same as Subtask 1

Output

Same as Subtask 1

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N5002 \le N \le 500
  • 1M50001 \le M \le 5000
  • 0Xi,YiN10 \le X_i, Y_i \le N-1 for all 0iM10 \le i \le M-1

Example

Input:

3
8 3
1 7
5 1
7 2
7 3
3 4
0 2
6 3
4 4
2 0
2 1
1 2
1 3

Output:

Case #0: 3
Case #1: 1
Case #2: -1

Comment:

Case #0: The grid evolves as follows. From time 44 onward, there is no safe path to the underground shelter, so the latest time is 33.
example of subtask 3-1
example of subtask 3-2
example of subtask 3-3

To submit for this subtask, please log in.

Subtask 4: Few fire sources (22 points)

The number of fire sources is small.

Input

Same as Subtask 1

Output

Same as Subtask 1

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1092 \le N \le 10^{9}
  • 1M1001 \le M \le 100
  • 0Xi,YiN10 \le X_i, Y_i \le N-1 for all 0iM10 \le i \le M-1

To submit for this subtask, please log in.

Subtask 5: More fire sources (26 points)

Both the grid size and the number of fire sources are large.

Input

Same as Subtask 1

Output

Same as Subtask 1

Limits

There are T=100T=100 test cases. In each test case we have:

  • 2N1092 \le N \le 10^{9}
  • 1M50001 \le M \le 5000
  • 0Xi,YiN10 \le X_i, Y_i \le N-1 for all 0iM10 \le i \le M-1

To submit for this subtask, please log in.

Don’t hesitate to ask us any question about this task, programming or the website via email (info@soi.ch).

Rob or Buy

The Silk Road in Uzbekistan was a trade route, where merchants travelled to cities like Samarkand to exchange silk, spices and other goods. The many markets and caravans made it a center of commerce across Uzbekistan and other parts of Asia. It was a lively road with a lot of merchants and travelers, but also thieves and robbers!

On the road, there are NN merchants in a line, numbered from 00 to N1N-1. Each of them sells some goods. The goods of the ii-th merchant cost pip_i mouse coins. There is the myth of the “Silk Road’s great heist” in which the entire road was robbed in lightning speed! That’s why the merchants on the road created an alarming network to notify other merchants of an ongoing robbery. Each merchant is assigned a direction which is either ll for left, or rr for right. After a merchant is robbed, he will notice the robbery and alarm every merchant on his left or right respectively. Any merchant that is alarmed this way will pay extra attention and robbing the merchant becomes impossible.

Mouse Stofl wants to do the thievery himself and wants to get all the goods sold on the Silk Road. But since it’s impossible to steal from alarmed merchants, he will need to buy some of the goods. So the questions is, what is the minimum amount of money he needs to spend, if Stofl robs and buys the goods in the perfect order?

Subtask 1: The road between Samarkand and Bukhara (10 points)

Input

The first line contains the number of test cases TT. For each test case you get three lines:

  • The first line contains the number of merchants NN.
  • The second line contains the string ss where sis_i is either ll or rr, indicating the direction of the ii-th merchant.
  • The third line contains NN spaces integers pip_i, indicating the price of the goods of the ii-th merchant.

Output

For each test case, output a line Case #i: P, where PP is the minimum amount of money Stofl needs to spend to get all the goods.

Limits

There are T=100T=100 test cases. For each test case, we have:

  • 2N1032 \le N \le 10^{3}
  • 2pi1062 \le p_i \le 10^{6} for all 0i<N0 \le i < N

Example

Input:

2
2
rl
3 5
3
lll
10 48 39

Output:

Case #0: 3
Case #1: 0

Comment:

Case #0: Stofl can rob the second merchant, but then, he can’t rob the first one because he’s alarmed, so he needs to buy it for 33.
Case #1: Stofl robs everyone from left to right, without buying anything.

To submit for this subtask, please log in.

Subtask 2: The road across Uzbekistan (15 points)

The road is longer now, can you still answer Stofl’s question?

Input & Output

Same as subtask 1.

Limits

There are T=100T=100 test cases. For each test case, we have:

  • 2N1052 \le N \le 10^{5}
  • 2pi1062 \le p_i \le 10^{6} for all 0i<N0 \le i < N

To submit for this subtask, please log in.

Subtask 3: Interrupting the alarms (10 points)

Stofl doesn’t like merchants being alarmed. Therefore he will hire mercenaries to interrupt the alarms of the merchants. Between any two adjacent merchants, he can hire a mercenary to stop the alarms from spreading further. This will interrupt any alarm signal from passing, not only the alarms from the merchants next to the mercenaries.

The mercenaries are in dire need of money, so they offer their service for WW mouse coin. In this subtask it holds that W=1W=1.

Input

On the first line on each test case, you will now get two spaced integers NN and WW, corresponding to the number of merchants and the price of hiring a mercenary. The rest of the input is the same as subtasks 1 and 2.

Output

For each test case, output a line Case #i: P, where PP is the minimum amount of money Stofl needs to spend to get all the goods. PP also includes the amount Stofl spends on mercenaries.

Limits

There are T=100T=100 test cases. For each test case, we have:

  • 2N1052 \le N \le 10^{5}
  • W=1W = 1
  • 2pi1062 \le p_i \le 10^{6} for all 0i<N0 \le i < N

Example

Input:

1
4 1
rrll
6 3 5 4

Output:

Case #0: 1

Comment:

Case #0: Stofl hires a mercenary between the second and the third merchant, then he robs the merchants in the order 1, 0, 2, 3 to. Therefore he needs 11 mouse coin for hiring.

To submit for this subtask, please log in.

Subtask 4: The mercenaries need money (25 points)

The mercenaries between Samarkand and Bukhara do not offer their service for so cheap. Can you still answer’s Stofl question?

Input & Output

Same as subtask 3.

Limits

There are T=100T=100 test cases. For each test case, we have:

  • 2N1032 \le N \le 10^{3}
  • 1W1061 \le W \le 10^{6}
  • 2pi1062 \le p_i \le 10^{6} for all 0i<N0 \le i < N

To submit for this subtask, please log in.

Subtask 5: The ultimate heist (40 points)

Stofl wants to rob the entire Silk Road! There are a lot of merchants and the prices can get very high!

Input & Output

Same as subtask 3 and 4.

Limits

There are T=100T=100 test cases. For each test case, we have:

  • 2N3000002 \le N \le 300\,000
  • 1W1091 \le W \le 10^{9}
  • 2pi1092 \le p_i \le 10^{9} for all 0i<N0 \le i < N

To submit for this subtask, please log in.

Don’t hesitate to ask us any question about this task, programming or the website via email (info@soi.ch).

Emirate of Burkhara

Under construction, coming very soon*

Don’t hesitate to ask us any question about this task, programming or the website via email (info@soi.ch).

Tomb exploration

Shah-i-Zinda is a famous necropolis (a large old cemetary) in Uzbekistan. Mouse Binna and a large team of passionate archeologists are preparing to enter an old, cursed, and unexplored tomb to bring back all the valuable historical unseen items they can find for the local museum. This project is of the upmost importance as all previous tombs explored were scavenged centuries ago because no curse protected them.

The tomb’s cryptic curse is written in majestic carved letters above the entrance: “He who exits these sacred grounds, shall suffer the weight of his hefty theft.”

Fully prepared to endure the consequences of their actions for the historical sake of the country (the team is as passionate about history as you can get), the entire team led by Binna enters the tomb. They travel multiple galleries with walls covered in beautiful artworks dating back to centuries. They of course progress with great care and take tons of pictures of everything they can see. After hours of exploration – mice have small legs and can’t travel very fast – they finally arrive at the main room of the tomb. On shelves they see NN small lined up statues which should be brought back to the surface for everyone to enjoy.

In front of this incredible spectacle, Binna can’t resist the urge to keep track of the order of the statues so they can reconstruct the way they are arranged in the museum. She also recalls what the curse said above the entrance and finally understands what it was all about: When exiting the tomb, everyone carrying statues will be inevitably cursed. In addition, for a specific mouse, the curse effect will be proportional to the weight of the heaviest statue they are carrying. The ii-th statue in the line will have weight wiw_i. That is, if mouse mm carries some statues and the heaviest statue is statue ii with weight wiw_i, then the curse mouse mm will suffer will be of importance wiw_i.

When entering the tomb, the team brought many bags with them, each bag can carry at most KK statues. Due to the size of the bags relative to the size of a mouse, a mouse can only carry at most 11 bag out of the tomb.

Binna wishes to minimize the sum of the curses the team will have to endure once they leave.

To help them keep track of the order of the many statues, the mice have decided that each bag containing statues will contain statues with incrementing positions. That is, the bb-th bag containing cbc_b with cbKc_b \leq K statues, will contain the statues at the positions ib,ib+1,ib+2,,ib+cb1i_b, i_{b+1}, i_{b+2}, \dots, i_{b+c_b-1}.

The team can use as many bags as they want and there are more than enough mice to carry all the filled bags.

Can you help Binna minimize the sum of the maximum weight of the statues present in each bag?

Input/Output

All subtasks have the same input and output format.

Input

The first line contains the number of test cases TT. The TT test cases follow in the following format:

  • The first line contains two integers NN and KK, the number of statues on shelves and the maximum number of statues a bag can contain respectively.
  • The second line contains NN strictly positive integers. The ii-th integer is wiw_i, the weight of the ii-th statue

Output

For the tt-th test case, output a line “Case #t:”, followed by the number SS, the minimum possible sum of the maximum weight of the statues in each bag.

Subtask 1: Ordered statues (17 points)

When inspecting the statues more closely, Binna realizes that the statues weights are monotonic. That is, the statues are sorted from lightest to heaviest or heaviest to lightest.

Limits

  • T=100T=100
  • 1N1061 \leq N \leq 10^{6}
  • 1KN1 \leq K \leq N;
  • 1wi1091 \leq w_i \leq 10^{9} for all ii

Example

Input:

2
5 2
7 8 9 10 11
3 3
9 5 2

Output:

Case #0: 27
Case #1: 9

Comment:

Case #0: We can use 33 bags, the first one will contain the statue at position 00 and the maximum weight will be 77, the second bag will contain the statues at position 11 and 22 and it’s maximum weight will be 99, and the last bag will contain the statues 33 and 44 with a maximum weight of 1111. Thus the sum is 7+9+11=277 + 9 + 11 = 27. It can be shown that no smaller sum can be achieved.
Case #1: We can put all statues in one bag with a maximum weight of 99. Thus the minimum sum is also 99

To submit for this subtask, please log in.

Subtask 2: Handbags (18 points)

Binna passed by during her afternoon walk and only brought her collection of handbags which can fit 2 statues (you know about the size restrictions on the airplane flights…)

Limits

  • T=100T=100
  • 1N1051 \leq N \leq 10^{5}
  • K=2K = 2;
  • 1wi1091 \leq w_i \leq 10^{9} for all ii

Example

Input:

1
5 2
8 7 10 11 5

Output:

Case #0: 24

Comment:

Case #0: We can use 33 bags, the first one will contain the statue at position 00 and 11 and the maximum weight will be 88, the second bag will contain the statues at position 22 and 33 and it’s maximum weight will be 1111, and the last bag will contain the statue 33 with a maximum weight of 55. Thus the sum is 8+11+5=248 + 11 + 5 = 24. It can be shown that no smaller sum can be achieved.

To submit for this subtask, please log in.

Subtask 3: Small bags (24 points)

Binna wasn’t expecting to find so many statues and only brought small bags in the tomb.

Limits

  • T=100T=100
  • 1N1051 \leq N \leq 10^{5}
  • 1Kmin(N,100)1 \leq K \leq \min(N, 100);
  • 1wi1091 \leq w_i \leq 10^{9} for all ii

Example

Input:

2
5 2
8 7 10 11 5
5 3
1 9 5 4 1

Output:

Case #0: 24
Case #1: 11

Comment:

Case #0: We can use 33 bags, the first one will contain the statue at position 00 and 11 and the maximum weight will be 88, the second bag will contain the statues at position 22 and 33 and it’s maximum weight will be 1111, and the last bag will contain the statue 33 with a maximum weight of 55. Thus the sum is 8+11+5=248 + 11 + 5 = 24. It can be shown that no smaller sum can be achieved.
Case #1: We can use 33 bags, one with statue 00, one with statues 1,21, 2 and 33, and one with statue 44. The sum of the maximum weight in each bag is 1+9+1=111 + 9 + 1 = 11

To submit for this subtask, please log in.

Subtask 4: The great find (41 points)

The team has never seen as many historical artifacts in one room and have come prepared with large bags.

Limits

  • T=100T=100
  • 1N1061 \leq N \leq 10^{6}
  • 1KN1 \leq K \leq N;
  • 1wi1091 \leq w_i \leq 10^{9} for all ii

Example

Input:

2
5 2
8 7 10 11 5
5 3
1 9 5 4 1

Output:

Case #0: 24
Case #1: 11

Comment:

Case #0: We can use 33 bags, the first one will contain the statue at position 00 and 11 and the maximum weight will be 88, the second bag will contain the statues at position 22 and 33 and it’s maximum weight will be 1111, and the last bag will contain the statue 33 with a maximum weight of 55. Thus the sum is 8+11+5=248 + 11 + 5 = 24. It can be shown that no smaller sum can be achieved.
Case #1: We can use 33 bags, one with statue 00, one with statues 1,21, 2 and 33, and one with statue 44. The sum of the maximum weight in each bag is 1+9+1=111 + 9 + 1 = 11

To submit for this subtask, please log in.

Don’t hesitate to ask us any question about this task, programming or the website via email (info@soi.ch).

Evilruins

Mouse Stofl and Binna are doing multiple cave expeditions in the lands of Uzbekistan, where they try to find crystals and treasures. They already investigated a lot of caves, among which are the famous Boybuloq caverns and the huge Deep Star cave system. In their 33rd expedition, they found something truly terrifying.

At the very bottom of the cave system, they found ruins of an ancient civilization of the “Dark Practitioners”, which was a terrifying cult of moles known for the black magic. In fact, this ruin contains a library full of evil magic that has been lost for centuries. Stofl and Binna need to cover up this ruin as fast as possible such that this dark knowledge doesn’t fall into evil hands.

The cave system is made up of NN chambers labeled from 00 to N1N-1. The chambers are all connected to each other through N1N - 1 channels. The chamber 00 contains the ruins and is at the very bottom of the cave system. Non ruin chambers with only one channel are cave entrances on the surface, which have different sizes cic_i. All other chambers are intermediary and lead down towards the ruins. In order to cover up the ruins, Stofl and Binna have to fill the entire cave system with sand from the nearby Kyzylkum Desert. The red sand of the desert is holy and blocks the dark magic from spreading. They will pour sand into one of the entrances filling up one chamber at a time. The sand will fall down towards the ruins until it reaches them or the next chamber in the direction of the ruins is already filled.

Although both Stofl and Binna want to save the world, they also want to be better than the other one in saving the world. That’s why they take alternating turns pouring sand, starting with Stofl. In one turn, they choose a cave entrance and pour sand into it, filling one chamber or the entrance. If the chamber they fill is the entrance itself, they get cic_i points for doing so. They cannot choose an entrance which is already filled and play until there are no unfilled chambers. The mouse with more points in the end is deemed “Hero of the world”.

Given the map of the caves, you will need to determine who will get more points by filling up entrances.

Subtask 1: Direct channels (6 points)

Every entrance leads directly to the ruins, with no chambers in between.

An example of a valid structure would look like this:

star

Input

The first line contains the number of test cases TT. For each test case, you will get following lines:

  • The first line contains NN, denoting the number of chambers.
  • The second line contains NN integers cic_i, denoting the number of points per chamber. Note that it will be 00 for chambers not on the surface.
  • The next N1N-1 lines contains two integers ui,viu_i,v_i which denotes a channel between chamber uiu_i and viv_i.

Output

For each test case, output a single line Case #i: P where P=SBP = S-B where SS is the points Stofl gets and BB the points Binna gets, assuming they fill up the cave system optimally.

Limits

  • T=100T = 100
  • 2N1000002 \le N \le 100\,000
  • 0ci1000000 \le c_i \le 100\,000 for all 0i<N0 \le i < N

Example

Input:

2
4
0 3 2 5
2 0
0 1
3 0
5
0 1 1 1 1
3 0
0 1
0 4
2 0

Output:

Case #0: -4
Case #1: 0

To submit for this subtask, please log in.

Subtask 2: Transportation channels (10 points)

The channels have been dug in a very specific way. There is a central chamber line, and each chamber on that line has zero or more channels directly leading to a cave entrance. They probably used it as a logistics cave.

An example of a valid structure would look like this, where the yellow line indicates central chamber line.

caterpillar

Input, Output & Limits

Same as in subtask 1.

Example

Input:

2
3
0 0 3
1 2
1 0
9
0 0 0 4 3 2 5 7 2
4 1
2 8
1 5
3 0
1 0
6 2
7 2
1 2

Output:

Case #0: 3
Case #1: -1

To submit for this subtask, please log in.

Subtask 3: Guard chambers (19 points)

Stofl and Binna noticed something interesting: The intermediary chambers have channels only directly to the ruins or entrances. They were likely used as guard post, providing a direct path to the city, but blocking off unwanted visitors from the surface. In addition, a direct path between an entrance at the surface and the ruins never exists.

An example of a valid structure would look like this:

star of stars

Input, Output & Limits

Same as in subtask 1 and 2.

To submit for this subtask, please log in.

Example

Input:

2
4
0 0 2 9
1 0
1 2
3 1
10
0 0 0 0 4 2 2 6 2 9
0 3
3 9
0 1
8 3
1 4
5 1
2 0
6 2
3 7

Output:

Case #0: 7
Case #1: -5

Subtask 4: Collapsed chambers (8 points)

Binna and Stofl are now interested in general cave systems. For now, they are only concerned in systems where many chambers have collapsed under the rocks. So, not many chambers are accessible anymore.

Input & Output

Same as previous subtasks

Limits

  • T=100T = 100
  • 2N202 \le N \le 20
  • 0ci100 \le c_i \le 10 for all 0i<N0 \le i < N

To submit for this subtask, please log in.

Subtask 5: Equally sized entrances (14 points)

Stofl and Binna have big ambitions and want to also solve large general cave systems. Luckily, for now, the entrances look exactly the same, thus covering them up gives the same amount of points. They feel otherworldly…

Input & Output

Same as previous subtasks

Limits

  • T=100T = 100
  • 2N1000002 \le N \le 100\,000
  • For all chambers on the surface ci=1c_i = 1.

To submit for this subtask, please log in.

Subtask 6: The evil aura (43 points)

The evil aura surrounding the ruins created a very spooky and huge cave system. This feels like the work of a divine being, which was able to hold this huge alien cave system together after all the years.

This is the final test, who is the true hero of the world?

Input & Output

Same as previous subtasks

Limits

  • T=100T = 100
  • 2N1000002 \le N \le 100\,000
  • 0ci1000000 \le c_i \le 100\,000 for all 0i<N0 \le i < N

To submit for this subtask, please log in.

Don’t hesitate to ask us any question about this task, programming or the website via email (info@soi.ch).

2H

The homework part of the second round (2H) consists of 4 tasks, each of which is worth 25 points. You can gain a maximum of 100 points counting towards your score of the regular round.



This helps you get familiar with our grading system which we will use in all our events after the second round, including workshops, camp, finals, and team selection.

You can submit in C++ (recommended), Java or Python.

C++:If you don’t have a setup already, we recommend installing VSCode.
Java:Read Java on the SOI Grader.
Python:Read Python on the SOI Grader.

For 2H you can discuss solutions with friends or publicly on Discord, as long as you don’t share source code. We are also happy to help you! If you need help with the grading system or have questions regarding the theory or tasks, don’t hesitate to ask here or in Discord.

Don’t hesitate to ask us any question about this task, programming or the website via email (info@soi.ch).

Junior Ranking

RankUsernameTotal (600)regista… (100)
registanpattern
timurid… (100)
timuridriders
mouseda… (100)
mousedances
fire (100)roborbuy (100)bukhara (100)
loading ...

Regular Ranking

RankUsernameTotal (700)mouseda… (100)
mousedances
fire (100)roborbuy (100)bukhara (100)tombexp… (100)
tombexploration
evilruins (100)2h (100)
loading ...