Educational Round #3 — Editorial
At rush hour, three taxi buses arrived at the stop simultaneously, all following the same route, and passengers immediately boarded them. The drivers noticed that the number of people in the different buses varied and decided to transfer some passengers so that each bus would have the same number of passengers. Determine the minimum number of passengers that need to be transferred for this.
Input. Three integers, not exceeding — the number of passengers in the first, second, and third busses, respectively.
Output. Print a single number — the minimum number of passengers that need to be transferred. If this is impossible, print IMPOSSIBLE.
beginrow
1 2 31A huge disaster occurred this morning at the café where you used to have snacks during your university studies. The cleaner, Larisa Ivanovna, accidentally knocked over one of the cabinets while sweeping the floor, causing all the kitchen utensils stored inside to scatter across the floor. Fortunately, it only contained saucepans with lids. However, some of them got bent or broken, so they had to be thrown away.
Now the schoolmaster wants to calculate the losses and determine how many new saucepans and lids should be purchased. But first, it is necessary to find out how many remaining saucepans can be covered by the remaining lids.
The saucepans and lids are round. A lid can cover a saucepans only if its radius is not less than the radius of the pot.
Input. The first line contains integers n, m~(1 \le n, m \le 1000) — the number of remaining saucepans and lids. The second line contains integers a_i~(1 \le a_i \le 1000) — the radii of the remaining saucepans. The third line contains integers b_i~(1 \le b_i \le 1000) — the radii of the remaining lids.
Output. Print one number — the largest number of saucepans that can be covered by the available lids.
beginrow
5 5
4 8 1 2 5
7 2 4 6 54Upon entering the treasure cave, Aladdin chose not to take the old, blackened lamp. Instead, he began filling his backpack with gold coins and precious gems. Of course, he wanted to take everything, but miracles don't happen — his backpack had a limited carrying capacity and might not withstand excessive weight.
Many times, he removed some items and replaced them with others, striving to maximize the total value of his backpack's contents.
The task is to determine the maximum value of the cargo that Aladdin can carry.
We assume that the cave contains different types of items, with an unlimited number of each type available. The backpack can hold a maximum weight of . Each item of type has a weight of w_i and a value of v_i~(i = 1, 2, ..., n).
Input. The first line contains two positive integers and n~(1 \le s \le 250, 1 \le n \le 35) — the maximum weight the backpack can carry and the number of item types.
The next lines each contain two numbers w_i and v_i~(1 \le w_i \le 250, 1 \le v_i \le 250) — the weight and value of an item of type .
Output. Print the maximum total value of the cargo that can be carried without exceeding the weight limit .
beginrow
10 2
5 10
6 1920King Julien, the ruler of all lemurs, has exactly 2 \cdot k lemurs under his command — two lemurs of each of the species. Julien loves parties, so every evening he throws one. However, the VIP area unfortunately has room only for himself and other lemurs.
Since Julien dislikes repeating himself, he wants to invite a set of lemurs to the VIP area that has never appeared before. Two lemurs of the same species are considered indistinguishable. Two sets of lemurs are considered the same if they coincide as multisets of species.
Help Julien determine how many distinct parties he can host. As the answer may be very large, print it modulo 10^9 + 7.
Input. A single line contains two integers and n~(1 \le k \le 500000,~0 \le n \le 2 \cdot k) — the number of lemur species and the number of available VIP seats (excluding Julien himself).
Output. Print one single integer — the number of distinct parties modulo 10^9 + 7.
3 374 316Given a tree with vertices, where the vertex numbered i~(1 \le i \le n) contains c_i coins. Select a subset of vertices such that no two of them are adjacent (i.e., connected by an edge), and the sum of coins in the selected vertices is maximized.

Input. The first line contains the number of vertices n~(1 \le n \le 10^5) in a tree. Each of the next n - 1 lines contains two integers and v~(1 \le u, v \le n), defining an edge in the tree. The last line contains non-negative integers c_1, ... c_n — the number of coins in each vertex of the tree.
Output. Print the maximum possible sum of coins that can be obtained by selecting a subset of vertices in the tree with no adjacent vertices.
5
1 2
1 3
2 4
2 5
1 5 7 1 2125
1 2
1 3
2 4
2 5
3 7 5 10 116A rectangle of size a \times b is given. Your task is to cut it into squares. In one move, you can select one of the rectangles and split it into two new rectangles so that all side lengths remain integers. Determine the minimum number of moves required to complete this task.
Input. Two integers and b~(1 \le a, b \le 500).
Output. Print the minimum number of moves required to cut the rectangle into squares.
3 535 101The Law of the Jungle is very clear: every wolf, once he has started a family, may leave his Pack. But as soon as his cubs grow up and become strong enough, he must bring them to the Council of the Pack, which is usually held once a month during the full moon, and present them to all the other wolves.
Father Wolf waited until his cubs had grown a little and started to run about. Then, on one of the nights when the Pack gathered, he led them — together with Mowgli and Mother Wolf — to the Council Rock. It was the top of a hill strewn with large boulders, behind which a hundred wolves could easily hide. Akela, the great gray lone wolf, chosen as leader of the whole Pack for his strength and agility, called out from his rock:
— The Law is known to you, the Law is known to you! Look well, wolves!
Father Wolf pushed the Frog, Mowgli, into the center of the circle. Mowgli sat down on the ground, laughed, and began to play with some sticks.
He came up with a simple task: to make a rectangle of the maximum possible area from these sticks — not necessarily using all of them.
Input. The first line contains the number of sticks n~(1 \le n \le 16). The second line contains their lengths — positive integers in the range from to 10^8.
Output. Print the maximum possible area of a rectangle that can be made from the given set of sticks, or if forming a rectangle is impossible.
8
7 1 5 2 3 2 4 5 49Once, Detective Saikat was investigating a murder case. At the crime scene, he discovered a staircase with a number written on each step. Finding this suspicious, he decided to remember all the numbers he encountered along the way. Soon, he noticed a pattern: for each number on the staircase, he recorded the sum of all previously encountered numbers that were smaller than the current one.
Your task is to find the total sum of all numbers recorded by the detective in his notebook.
Input. The first line contains an integer t~(t \le 10) — the number of test cases. The next lines follow. The first of these lines contains an integer n~(1 \le n \le 10^5) — the number of steps. The next line contains integers — the numbers written on the steps. All numbers are in the range from to 10^6.
Output. For each test case, print the final sum in a separate line.
1
5
1 5 3 6 415Amin and Murad decided to create a Minecraft game server.
They invited guests to the grand opening of the server. The guests live in different cities, and when connecting to the server, they will experience a certain latency (ping) depending on their location. Realizing the potential issues, Amin and Murad decided to choose the most optimal location for hosting the server.
There are cities, numbered from to , and bidirectional communication channels between them. A connection to the server is only possible through these channels. Using them, it is possible to establish a connection between any two cities. Each pair of cities may have at most one direct communication channel, and no city is connected to itself. Each channel has a transmission delay of w_i.
The connection delay between a city and the server is defined as the minimum possible sum of delays along the path from that city to the server.
Amin and Murad want to choose a city to host the server so that the total connection delay for all guests is minimized. If the server is hosted in the same city where a guest lives, the delay for that guest is considered to be zero.
If there are multiple cities with the same minimum total delay, Amin and Murad will choose the city with the smallest number.
Determine the city where the server will be hosted and the total connection delay for all guests to the server.
Input. The first line contains three integers n~(1 \le n \le 10^4), m~(1 \le m \le 4 * 10^4), and k~(1 \le k \le 100) — the number of cities, the number of communication channels, and the number of guests, respectively. The second line contains distinct integers c_i~(1 \le c_i \le n) — the numbers of the cities where the guests live.
Each of the following lines contains three integers u_i, v_i, and w_i~(1 \le u_i, v_i \le n) — describing a bidirectional communication channel between cities u_i and v_i with a delay of w_i~(1 \le w_i \le 10^4).
Output. Print two integers — the number of the city where the server will be hosted, and the total connection delay for all guests to this server.
5 6 3
1 2 5
1 2 10
1 4 3
2 4 2
2 5 8
3 4 5
3 5 32 13
Comments