49th ICPC
ICPC 2025
Baku, Azerbaijan · 12 problems
- 1St. Petersburg State University · Polar Bear TransformFedor Ushakov, Leonid Danilevich, Maksim Turevskii11
- 2The University of Tokyo · ScreenwalkersDaiki Kodama, Hirotaka Yoneda, Masataka Yoneda10
- 3Beijing Jiaotong University · miemiemieHeming Xia, Xiuyuan Si, Yuzhe Guo10
Problems
12 problemsProblem A · A-Skew-ed Reasoning The following is based on a true story – the names have been changed because...well, because you always change names in stories like this one.
Professor Taylor Swift is grading a homework assignment on integer skew heaps. A skew heap is a binary tree with an integer stored in each node such that the value in any node is less than or equal to the values in any of its children. Note that the skew heap need not be a perfect binary tree; that is, the left and/or right subtree of any node may be empty.
Inserting a value into a skew heap is done using the following recursive procedure:
-
If is empty, make a skew heap consisting of a single node containing .
-
Otherwise, let be the value in the root of .
-
If , swap the two children of the root and recursively insert into the new left subtree.
-
If , create a new node with value and make the left subtree of this node.

Figure A.1: Example of inserting the value into a skew heap. The nodes storing and (marked in blue) have their children swapped, while the node storing becomes the left child of the newly inserted node (marked in red).
Now, back to Professor Swift. The homework problem she has assigned asks the students to show the heap that results from inserting a given permutation of the numbers from to , in the given order, into an empty heap. Surprisingly, some of the students have wrong answers! That got Professor Swift wondering: For a given heap, is there an input permutation that would have produced this heap? And if so, what are the lexicographically minimal and maximal such input permutations?
Input
The first line of input contains an integer (), the number of nodes in the tree. These nodes contain the numbers from to exactly. This is followed by lines, the th of which contains two integers and ( or ; or ), describing the values of the left and right children of the node storing , where a value of is used to indicate that the corresponding child does not exist. It is guaranteed that this data describes a binary tree.
Output
Output the lexicographically minimal input permutation that produces the given tree under the insertion method for skew heaps, followed by the lexicographically maximal such input permutation. These per- mutations may coincide, in which case you still need to output both. If no input permutation producing the given tree exists, output impossible.
Sample Input 1
7 2 3 4 5 6 7 0 0 0 0 0 0 0 0Sample Output 1
1 3 2 7 5 6 4 7 1 5 3 2 6 4Sample Input 2
2 0 2 0 0Sample Output 2
impossibleSample Input 3
3 2 0 3 0 0 0Sample Output 3
2 3 1 3 2 1-
Problem B · Blackboard Game To help her elementary school students understand the concept of prime factorization, Aisha has invented a game for them to play on the blackboard. The rules of the game are as follows.
The game is played by two players who alternate their moves. Initially, the integers from to are written on the blackboard. To start, the first player may choose any even number and circle it. On every subsequent move, the current player must choose a number that is either the circled number multiplied by some prime, or the circled number divided by some prime. That player then erases the circled number and circles the newly chosen number. When a player is unable to make a move, that player loses the game.
To help Aisha’s students, write a program that, given the integer , decides whether it is better to move first or second, and if it is better to move first, figures out a winning first move.
Input
The first line of input contains an integer (), which is the number of test cases. The descriptions of test cases follow.
Each test case consists of a single line containing an integer (), which is the largest number written on the blackboard.
Over all test cases, the sum of is at most .
Output
For each test case, if the first player has a winning strategy for the given , output the word first, followed by an even integer – any valid first move that can be extended to a winning strategy. If the second player has a winning strategy, output just the word second.
Sample Input 1
1 5Sample Output 1
secondExplanation of Sample 1: For , the first player loses the game regardless of the first move.
-
If the first player starts with , the second player circles , and there are no more valid moves left.
-
If the first move is , the second player circles . The first player must then circle , and the second player may pick either of the remaining two numbers ( or ) to win.
Sample Input 2
2 12 17Sample Output 2
first 8 first 6-
Problem C · Bride of Pipe Stream The story continues! For several years now, your town has been gifted with an abundance of Flubber, the adorable-but-slightly-flammable-and-toxic-and-acidic-and-sentient-and-mischievous man-made chemi- cal. The search continues for more (or, well, any) uses for the substance. But in the meantime, the Flubber factory continues to produce it at full capacity. Efforts to shut it down have failed, partly be- cause nobody is sure who is actually running the factory.
You’ve been tasked with storing the perpetually-flowing Flubber in various Flubber reservoirs for future use (or, at least, to get it out of everyone’s hair – literally). To accomplish this, you have access to a complicated network of Flubber ducts, connecting up various Flubber stations and reservoirs.
Every Flubber station has one or more Flubber ducts leading from it, and has various gates that may be raised or lowered so that incoming Flubber will drain into the output Flubber ducts in any desired proportion. For instance, you can send all the Flubber down one duct, or split it between two ducts –, etc.
In contrast, a Flubber duct flows down to one or more lower stations or reservoirs, but the Flubber drains into them in a fixed proportion that you do not control. It is possible that some of the Flubber is lost to the environment as well, but that is a problem for your successor, not you.
You would like to fill all the reservoirs as quickly as possible. That is, you want to maximize the minimum amount of Flubber flowing into any of the reservoirs, among all possible configurations of station drainage.
Figure C.1 illustrates the two sample inputs. Stations and reservoirs are shown as numbered nodes, colored green for stations and blue for reservoirs. Ducts are depicted as white nodes. For example, in the first sample input (left), Flubber can be sent from station in any proportion to its two downstream ducts, but each duct will distribute its inflow according to the percentages printed on its outgoing edges.

Figure C.1: Illustrations of the two sample inputs.
Input
The first line of input contains three integers , , and , where () is the number of stations, is the number of reservoirs, and () is the number of ducts. The stations are numbered from to and the reservoirs are numbered from to , in decreasing order of altitude. The factory’s Flubber initially flows into station . Each of the remaining lines starts with two integers and , where () is the station that can drain into this duct, and () is the number of outputs of this duct. The remainder of the line contains pairs of integers and , where () is a station or reservoir to which this duct drains, and () is the percentage of the Flubber entering the duct that will drain to . The values for a given duct are distinct. Every station has at least one duct that it can drain into. The percentages for a given duct’s outputs will sum to at most .
Output
Output a single percentage , which is the highest possible percentage such that, for some configuration of station drainage, all reservoirs receive at least % of the factory’s produced Flubber. Your answer should have an absolute error of at most .
Sample Input 1
2 3 3 1 2 3 80 4 10 1 2 2 40 4 30 2 1 5 100Sample Output 1
24.0Sample Input 2
1 2 3 1 1 2 50 1 1 3 50 1 2 2 40 3 60Sample Output 2
42.8571428571Problem D · Buggy Rover Mars rover being tested near the Paranal Observatory. CC BY-SA 4.0 by ESO/G. Hudepohl on Wikimedia Commons The International Center for Planetary Cartography (ICPC) uses rovers to explore the surfaces of other planets. As we all know, other planets are flat surfaces which can be perfectly and evenly discretized into a rectangular grid structure. Each cell in this grid is either flat and can be explored by the rover, or rocky and cannot.
Today marks the launch of their brand-new Hornet rover. The rover is set to explore the planet using a simple algorithm. Inter- nally, the rover maintains a direction ordering, a permutation of the directions north, east, south, and west. When the rover makes a move, it goes through its direction ordering, chooses the first direction that does not move it off the face of the planet or onto an impassable rock, and makes one step in that direction.
Between two consecutive moves, the rover may be hit by a cosmic ray, replacing its direction ordering with a different one. ICPC scientists have a log of the rover’s moves, but it is difficult to determine by hand if and when the rover’s direction ordering changed. Given the moves that the rover has made, what is the smallest number of times that it could have been hit by cosmic rays?
Input
The first line of input contains two integers and , where () is the number of rows on the planet, and () is the number of columns. The rows run north to south, while the columns run west to east.
The next lines each contain characters, representing the layout of the planet. Each character is either ‘#’, a rocky space; ‘.’, a flat space; or ‘S’, a flat space that marks the starting position of the rover. There is exactly one ‘S’ in the grid.
The following line contains a string , where each character of is ‘N’, ‘E’, ‘S’, or ‘W’, representing the sequence of the moves performed by the rover. The string contains between and characters, inclusive. All of the moves lead to flat spaces.
Output
Output the minimum number of times the rover’s direction ordering could have changed to be consistent with the moves it made.
Sample Input 1
5 3 #.. ... ... ... .S. NNENSample Output 1
1Explanation of Sample 1: The rover’s direction ordering could be as follows. In the first move, it either prefers to go north, or it prefers to go south and then north. Note that in the latter case, it cannot move south as it would fall from the face of the planet. In the second move, it must prefer to go north. In the third move, it must prefer to go east. In the fourth move, it can either prefer to go north, or east and then north. It is therefore possible that it was hit by exactly one cosmic ray between the second and third move, changing its direction ordering from N??? to EN?? where ‘?’ stands for any remaining direction.
Sample Input 2
3 5 .###. ....# .S... NEESNSSample Output 2
0Explanation of Sample 2: It is possible the rover began with the direction ordering NESW, which is consistent with all moves it makes.
Sample Input 3
3 3 ... ... S#. NEESNNWWSENESSSample Output 3
4Problem E · Delivery Service The Intercity Caspian Package Company (ICPC) is starting a delivery service which will deliver pack- ages between various cities near the Caspian Sea. The company plans to hire couriers to carry packages between these cities.
Each courier has a home city and a destination city, and all couriers have exactly the same travel sched- ule: They leave their home city at 9:00, arrive at their destination city at 12:00, leave their destination city at 14:00 and return to their home city at 17:00. While couriers are in their home or destination cities, they can receive packages from and/or deliver packages to customers. They can also hand off to or receive packages from other couriers who are in that city at the same time. Since ICPC is a personal service, packages are never left in warehouses or other facilities to be picked up later – unless the pack- age has reached its destination, couriers have to either keep the package with themselves (during the day or during the night), or hand it off to another courier.
The company will direct the couriers to hand off packages in such a way that any package can always be delivered to its destination. Or so it is hoped! We’ll say that two cities and are connected if it is possible to deliver a package from city to city as well as from to . To estimate the efficiency of their hiring process, the company would like to find, after each courier is hired, the number of pairs of cities that are connected ().
Input
The first line of input contains two integers and , where () is the number of cities, and () is the number of couriers that will be hired. Couriers are numbered to , in the order they are hired. This is followed by lines, the th of which contains two distinct integers and (), denoting the home and destination cities, respectively, for courier .
Output
Output integers, denoting the number of pairs of connected cities after hiring the first couriers.
Sample Input 1
4 4 1 2 2 3 4 3 4 2Sample Output 1
1 2 4 6Explanation of Sample 1:
-
After the first courier is hired, cities and are connected.
-
After the second courier is hired, cities and are connected. Note, however, that cities and are still not connected. Even though there’s a courier moving between cities and , and a courier moving between cities and , they never meet each other.
-
After the third courier is hired, cities and are connected and cities and are connected. For example, one way to deliver a package from city to city is:
-
hand it to courier in city at 19:00;
-
the next day, courier arrives in city at 12:00, and hands the package to courier who is also in city ;
-
at 18:00, courier delivers the package to city .
- After the fourth courier is hired, all six pairs of cities are connected.
-
Problem F · Herding Cats You are opening a cat cafe in Baku and would like to take a promotional photograph of all the cats sitting in the front window. Unfortunately, getting cats to do what you want is a famously hard problem. But you have a plan: you have bought a collection of catnip plants, each of a different variety, knowing that each cat likes some of these varieties. There is a row of pots in the window, numbered to in order, and you will place one plant in each pot. Each cat will then be persuaded (by means of a toy on a string) to walk along the row of pots from to . As soon as a cat reaches a pot with a catnip plant that it likes, it will stop there, even if there already are other cats at that plant.

Figure F.1: One possible plant ordering for the first sample test case.
You know which pot you would like each cat to stop beside. Can you find a way in which to place the plants in the pots to achieve this?
Input
The first line of input contains an integer (), which is the number of test cases. The descriptions of test cases follow.
The first line of each test case contains two integers and , where () is the number of cats, and () is the number of catnip plants (and also the number of pots). Catnip plants are numbered from to .
The following lines each describe one cat. The line starts with two integers and , where ( ) is the pot at which the cat should stop, and () is the number of catnip plants the cat likes. The remainder of the line contains distinct integers, which are the numbers of the plants that the cat likes.
Over all test cases, the sum of is at most , the sum of is at most , and the sum of all is at most .
Output
For each test case, output either yes if it is possible to arrange the catnip plants as described above, or no if not.
Sample Input 1
2 3 5 2 2 1 5 2 3 1 4 5 4 2 3 4 3 5 2 2 1 5 2 3 1 4 5 5 2 3 4Sample Output 1
yes noExplanation of Sample 1: In the first test case, a possible ordering of the plants is . This way, cat will stop at pot , as it is the first pot with a plant variety that it likes. Cat will stop there as well. Cat will continue all the way to pot , as shown in Figure F.1.
Problem G · Lava Moat These pesky armies of good are coming to disturb the quiet and peaceful lands of the goblins again. Building a huge wall didn’t work out that well, and so the goblins are going to turn to the tried and true staple of defense: a moat filled with lava. They want to dig this moat as a boundary between the goblin lands in the north and the do-gooder lands in the south, crossing the whole borderlands west-to-east.
This presents them with a challenge. The borderlands are hilly, if not outright mountainous, while a lava moat has to be all on one level – otherwise the lava from the higher parts will flow down and out of the moat in the lower parts. So, the goblins have to choose a path that is all on one elevation, and connects the western border of the borderlands to its eastern border. For obvious economic reasons, they want this path to be as short as possible.
This is where you come in. You are given an elevation map of the borderlands, and your task is to determine how short the moat can be.
The map is in the form of a fully triangulated rectangle with dimensions , with all triangles having positive area. No vertex of a triangle lies on the interior of an edge of another triangle. The southwestern corner of the map has coordinates , with the -axis going east and the -axis going north. Further- more, the western border (the line segment connecting and , including the endpoints) is a single edge. Similarly, the eastern border (between points and ) is also a single edge.
Of course, this map is just a 2D projection of the actual 3D terrain: Every point also has an elevation . The elevation at the vertices of the triangulation is directly specified by the map, and all of these given elevations are distinct. The elevation at all other points can be computed by linear interpolation on associated triangles. In other words, the terrain is shaped like a collection of triangular faces joined together by shared sides. These faces correspond to the triangles on the map.

Figure G.1: Illustration of the sample test cases. Shading denotes elevation, and the thick red lines denote optimal moats.
Input
The first line of input contains an integer (), which is the number of test cases. The descriptions of test cases follow.
The first line of each test case contains four integers , , , and , where () is the extent of the borderlands from west to east, () is the extent from south to north, () is the number of vertices, and () is the number of triangles in the provided triangulation. This is followed by lines, the th of which contains three integers , , and (; ; ), denoting the coordinates and the elevation of vertex . The only vertices with or are the four corners. All pairs are distinct. All s are distinct.
Each of the following lines contains three distinct integers , , and (), denoting a map triangle formed by vertices , , and in counter-clockwise order. These triangles are a complete triangulation of the rectangle . Each of the vertices is referenced by at least one triangle.
Over all test cases, the sum of is at most .
Output
For each test case, if it is possible to construct a lava moat at a single elevation that connects the western border to the eastern border, output the minimum length of such a moat, with an absolute or relative error of at most . Otherwise, output impossible.
Sample Input 1
3 6 6 4 2 0 0 1 6 0 4 6 6 3 0 6 2 1 2 3 1 3 4 6 6 4 2 0 0 1 6 0 2 6 6 4 0 6 3 1 2 3 1 3 4 10 6 7 7 6 1 8 10 0 10 10 6 4 2 6 6 0 6 0 4 3 11 0 0 7 2 1 7 2 3 1 3 6 1 3 4 6 6 4 5 5 7 6 7 1 6Sample Output 1
impossible 6.708203932 15.849260054Problem H · Score Values Generated by ChatGPT Ever since you arrived at your university, you have been a tireless advocate for introducing the brand-new martial-arts- plus-card-based sport of Contact Bridge to the school (and the world). Finally, after a great deal of (really persistent and annoying) advocacy on your part, you have obtained per- mission and funding from your dean to build a grand new arena for the sport! Well, technically it is not so much an “arena” as a “broom closet,” and maybe not “grand” so much as “cramped,” and the “new” is also debatable. But the sport of the future has to start somewhere!
Unfortunately, you just realized that you are going to need a score display in order to run the games. In Contact Bridge, the score for a team starts at and, after various repeatable actions, may be incremented by certain fixed amounts. There is also a maximum value – if the team’s score would be incremented above the maximum, it will instead be capped there. You want the team’s score to be visible at all times, so you will need to prepare some signs, each with a single digit printed on it, that can be arranged to show the score.
Unfortunately the dean’s “funding” is running short, and these signs are expensive. Figure out the minimum set of signs you need to purchase to show any score that is possible to achieve during the game. Note that you won’t need any 9 signs, as any 6 sign can be turned upside-down to make a 9.
Input
The first line of input contains two integers and , where () is the maximum score value, and () is the number of different ways of scoring. This is followed by lines, each containing an integer (), which is the number of points awarded for a type of action in the game. No two types of action are awarded the same number of points.
Output
For each digit from to in increasing order, output two integers: the digit and the number of signs with that digit that you need to purchase. Omit digits where the number of signs needed is .
Sample Input 1
1000 4 60 100 222 650Sample Output 1
0 3 1 1 2 3 3 1 4 3 5 1 6 3 7 2 8 3Sample Input 2
967 1 1000Sample Output 2
0 1 6 2 7 1Problem I · Slot Machine Imperial Chance & Play Casino offers games using a slot machine that has wheels arranged next to each other. Each of the wheels has distinct symbols on it, and these symbols appear in the same order on each wheel. Each wheel shows one of its symbols through a window on the front of the machine, which results in a sequence of symbols being shown next to each other.

Figure I.1: The initial configuration in Sample Interaction 1.
You are standing behind the machine and notice that a maintenance panel has been left open. When you stick your hand inside, you are able to secretly rotate any of the wheels by any number of steps, thus changing the symbol shown on that wheel. You want to win a jackpot, which will happen if all the wheels show the same symbol at the same time. Unfortunately, you cannot see the symbols from your position, so you asked your good friend to help you. The friend is standing in front of the machine and she tells you the number of distinct symbols in the sequence she can currently see. Can you win the jackpot by manipulating the wheels if your friend updates the information after every action you make?
Interaction
The first line of input contains an integer (), giving the number of wheels and symbols in the machine.
Interaction then proceeds in rounds. In each round, one line of input becomes available, containing an integer (), the number of distinct symbols in the current sequence. If , output two integers and (; ), representing your action: rotating the th wheel by positions, where negative numbers indicate rotating in the opposite direction. Otherwise, if , indicating that all wheels show the same symbol, your program must exit without printing more output.
At most actions are allowed – if your submission uses more rounds, it will not be accepted. It is guaranteed that the initial configuration of wheels does not already have all wheels showing the same symbol ( in the first round).
The judge program will not behave in an adversarial way, which means the initial configuration is fixed before the first action.
A testing tool is provided to help you develop and test your solution.
Read
Sample Interaction 1
Write
5 4 1 1 3 4 2 3 3 1 3 3 1 2 5 4 1Read
Sample Interaction 2
Write
3 3 2 -1 2 3 -1 2 2 -1 1Problem J · Stacking Cups You have a collection of cylindrical cups, where the th cup is cm tall. The cups have increasing diameters, such that cup fits inside cup if and only if . The base of each cup is cm thick (which makes the smallest cup rather useless as it is only cm tall, but you keep it for sentimental reasons).
After washing all the cups, you stack them in a tower. Each cup is placed upright (in other words, with the opening at the top) and with the centers of all the cups aligned vertically. The height of the tower is defined as the vertical distance from the lowest point on any of the cups to the highest. You would like to know in what order to place the cups such that the final height (in cm) is your favorite number. Note that all cups must be used.
For example, suppose and your favorite number is . If you place the cups of heights , , , , in that order, the tower will have a total height of , as shown in Figure J.1.

Figure J.1: Illustration of Sample Output 1.
Input
The input consists of a single line containing two integers and , where () is the number of cups and () is your favorite number.
Output
If it is possible to build a tower with height , output the heights of all the cups in the order they should be placed to achieve this. Otherwise, output impossible. If there is more than one valid ordering of cups, any one will be accepted.
Sample Input 1
4 9Sample Output 1
7 3 5 1Sample Input 2
4 100Sample Output 2
impossibleProblem K · Treasure Map After years of searching you have come across Captain Blackbeard’s old map showing where his long- lost treasure is hidden, deep on the ocean floor. The map was once a hypsometric map – that is, it showed the ocean depth for the region around the treasure – but many of the elevation marks have faded away over time and are no longer legible.
Specifically, the map covers a rectangular part of the ocean, subdivided into an rectangular grid of unit squares. The map originally showed the ocean depth for each point with integer coordinates and . There are no islets in the region. In other words, it is known that for all points.
Preparing the map must have been quite a struggle for Blackbeard, since there is no unique natural way to interpolate the depths of points with non-integer coordinates. Consider a unit square on the grid, with corners at the grid points , , , and in clockwise order, and some depth stored for each . One natural way is to interpolate the depth in the triangle linearly, and likewise in . Another equally natural way is to interpolate linearly within , and likewise within . Usually, the results of those two interpolations are different. For example, if and , the first method results in depths across all of being equal to zero (Figure K.1 left), while the second method results in the depths being positive in the whole interior of the square (right).

Figure K.1: Two ways of interpolating depths within a unit square.
However, Blackbeard was as stubborn as he was cruel and would not let such pesky ambiguities stop him. To find the perfect hiding spot for his treasure, he scoured the seven seas for a region of the ocean where the two methods described above yield the same results for each unit square (or maybe he forced some of his pirates to do a bit of terraforming work to achieve this – scholars disagree).
Back in the present, you are preparing an expedition to retrieve the treasure, and would like to figure out at what depth the treasure could be buried. Specifically, given the remaining depth data of the map, you should calculate the smallest possible depth at the treasure location.
Input
The first line of input contains five integers , , , , and , where and () denote the maximum coordinates of the grid, () is the number of known depths, and is the location of the treasure (; ). Each of the next lines contains three integers , , and (; ; ), indicating that the depth at coordinate of the grid equals . Each pair appears in the input at most once.
Output
If the provided data points can be extended to a valid map (that is, a map where, for each unit square, the two methods of interpolation yield the same results, and all points have non-negative depth), output one integer: the smallest possible depth of – it can be shown that this is always an integer. Otherwise, output impossible.
Sample Input 1
3 3 5 1 1 1 3 1 3 3 2 2 3 3 2 2 4 2 1 5Sample Output 1
3Sample Input 2
3 5 4 3 4 2 4 1 2 2 2 1 1 4 3 1 5Sample Output 2
1Sample Input 3
3 3 3 3 3 2 3 1 2 1 2 1 2 4Sample Output 3
0Sample Input 4
3 3 4 3 2 2 1 2 2 3 3 1 3 4 1 1 5Sample Output 4
impossibleSample Input 5
3 3 3 2 2 3 2 0 2 2 1 2 3 0Sample Output 5
impossibleExplanation of Sample 5: Even though the depth of is given in the input, the provided data points cannot be extended to a valid map, so the correct answer is impossible.
Problem L · Walking on Sunshine I’m walking on sunshine, and it don’t feel good – my eyes hurt!
Baku has plenty of sunshine. If you walk away from the sun, or at least perpendicular to its rays, it does not shine in your eyes. For this problem assume that the sun shines from the south. Walking west or east or in any direction between west and east with a northward component avoids looking into the sun. Your eyes will hurt if you walk in any direction with a southward component.
Baku also has many rectangular areas of shade, and staying in these protects your eyes regardless of which direction you walk in. For example, Figure L.1 shows two shaded areas.
Find the minimum distance you need to walk with the sun shining in your eyes to get from the contest location to the awards ceremony location.
NE NW
SW SE E N
W
S
contest

Figure L.1: Sample Input 1 and a path that minimizes the sun shining in your eyes.
Input
The first line of input contains five integers , , , , and , where () is the number of shaded areas, is the location of the contest, and is the location of the awards ceremony (). The sun shines in the direction from south towards north. You look into the sun if you walk in direction for any and any .
The next lines describe the shaded areas, which are axis-aligned rectangles. Each of these lines contains four integers , , , and (; ). The southwest corner of the rectangle is and its northeast corner is . The rectangles describing the shaded areas do not touch or intersect.
Output
Output the minimum distance you have to walk with the sun shining in your eyes. Your answer must have an absolute or relative error of at most .
Sample Input 1
2 1 7 5 1 3 6 5 9 2 3 6 5Sample Output 1
3.0Explanation of Sample 1: Figure L.1 shows an optimal path from the contest location to the awards ceremony location with 5 segments. On the first segment you walk away from the sun. On the second and fourth segments you walk towards the sun but in a shaded area. On the third and fifth segments you walk towards the sun outside the shaded areas. The total length of these two segments is 3.
Sample Input 2
2 0 10 10 0 2 7 3 8 4 3 8 5Sample Output 2
7.0Sample Input 3
2 11 -1 -1 11 2 7 3 8 4 3 8 5Sample Output 3
0.0Sample Input 4
3 1 5 9 5 -5 6 2 9 4 7 12 8 1 1 7 3Sample Output 4
0.0Sample Input 5
3 1 7 9 3 2 6 3 8 4 4 5 6 6 2 7 4Sample Output 5
0.0Sample Input 6
1 0 0 0 0 -5 -5 5 5Sample Output 6
0.0