XXV Spain Olympiad in Informatics, Online Qualifier 2
A. Pairs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given $$$n$$$ integers, where $$$n$$$ is an even number. Determine if they can be paired into $$$\frac{n}{2}$$$ pairs such that the sums of the two numbers in each pair are equal for all pairs.

Input

In the first line, there is an integer $$$T$$$, the number of test cases.

For each case, there is a line with an integer $$$n$$$, followed by a line with the $$$n$$$ integers $$$a_1, \ldots, a_n$$$ that need to be paired.

Output

For each case, if the numbers can be paired, write a line with "SI". Otherwise, write a line with "NO".

Scoring

$$$1 \leq T \leq 100$$$

$$$4 \leq n \leq 10^5$$$, $$$n$$$ is an even number, the sum of $$$n$$$ for all cases is less than $$$5 \cdot 10^5$$$

$$$-10^8 \leq a_i \leq 10^8$$$ for $$$1 \leq i \leq n$$$

40 points: $$$n = 6$$$, $$$T \leq 30$$$

50 points: $$$n \leq 1000$$$, the sum of $$$n$$$ for all cases is less than $$$5000$$$

10 points: No additional restrictions

Example
Input
3
4
-2 1 -1 2
6
2 4 0 6 3 5
8
1 1 1 1 1 1 1 1
Output
SI
NO
SI

B. Papalindromes!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Given an $$$a_0$$$, we define $$$a_{n+1} = \left \lfloor{\frac{a_n + a_n'}{2}}\right \rfloor$$$ where $$$a'$$$ is the number obtained by reversing the decimal representation of $$$a$$$ (for example, $$$1045' = 5401$$$) and $$$\left \lfloor{a}\right \rfloor$$$ is the largest integer less than or equal to $$$a$$$. Given $$$a_0$$$, what is the first $$$a_n$$$ that is a palindrome? (A number is a palindrome if its decimal representation reads the same forwards and backwards, that is, $$$a_n = a_n'$$$). If no $$$a_n$$$ is a palindrome, write "Que complicado!".

Input

The first line indicates an integer $$$t$$$, the number of cases. In each of the following $$$t$$$ lines, there is an integer, the $$$a_0$$$ for the corresponding case.

Output

For each case, write the first $$$a_n$$$ that is a palindrome, or if there is none, write "Que complicado!" without the quotes.

Scoring

10 points: $$$t = 1000$$$ and $$$ 0 \leq a_0 \lt 1000$$$

30 points: $$$t = 10000$$$ and $$$0 \leq a_0 \lt 100000$$$

60 points: $$$t = 10000$$$ and $$$0 \leq a_0 \lt 1000000000$$$

Example
Input
4
8
45
245
10196087
Output
8
44
393
Que complicado!

C. Numbers in the Grid
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a grid with $$$n \times m$$$ cells (arranged in $$$n$$$ rows and $$$m$$$ columns). Initially, each cell contains the number $$$0$$$.

There are $$$q$$$ operations performed, where each operation consists of replacing the number written in each of the cells of a given row or column with a number $$$x$$$.

Output the sum of the numbers in the grid after performing the $$$q$$$ operations.

Input

The input begins with a line containing the number of cases $$$T$$$.

Each case starts with a line containing three integers $$$n, m, q$$$. This is followed by $$$q$$$ lines, each containing a character $$$c_i$$$, which is "F" if the operation is on a row and "C" if it is on a column, an integer $$$a_i$$$, which indicates the row or column on which the operation is applied, and an integer $$$x_i$$$, the number to be written.

Output

For each case, print a line with the sum of all the numbers written in the grid after performing the $$$q$$$ operations.

Scoring

$$$1 \leq T \leq 500$$$

$$$1 \leq n, m, q \leq 5 \cdot 10^5$$$, the sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$q$$$ for all cases are all less than $$$3 \cdot 10^6$$$.

For operations where $$$c_i = F$$$, $$$1 \leq a_i \leq n$$$. For operations where $$$c_i = C$$$, $$$1 \leq a_i \leq m$$$.

$$$0 \leq x_i \leq 10^6$$$ for $$$i = 1, \ldots, q$$$.

14 points: $$$n, m, x_i, q \leq 1000$$$, the sum of $$$n \cdot m + q \cdot (n + m)$$$ for all cases is less than $$$5 \cdot 10^6$$$.

28 points: $$$n, m, x_i \leq 1000$$$, the sum of $$$n \cdot m$$$ for all cases is less than $$$5 \cdot 10^6$$$.

11 points: $$$n = 1$$$.

22 points: $$$x_i = 1$$$ for $$$i = 1, \ldots, q$$$.

25 points: No additional restrictions.

Example
Input
4
2 2 3
F 1 1
C 1 2
C 2 3
3 4 4
F 1 2
F 2 4
C 3 3
F 1 5
1 1 1
F 1 0
1 500000 1
F 1 1000000
Output
10
38
0
500000000000

D. Colored Towers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

María has found a new hobby, building towers with disks. María has disks of $$$n$$$ different colors. For the $$$i$$$-th color, she has $$$a_i$$$ disks. She wants to build towers by stacking disks, but she only likes a set of towers if it meets the following conditions:

- All disks are used. - For each tower, the sequence of colors from top to bottom is the same as from bottom to top. - All towers are of equal height.

What is the minimum number of towers that María will have to build to achieve a set of towers that she likes?

Input

The first line contains a number $$$t$$$ which is the number of cases.

The following $$$t$$$ cases each consist of the first line containing the number $$$n$$$ of colors for the case. The next line contains $$$n$$$ integers, where the $$$i$$$-th integer indicates the number of pieces of the $$$i$$$-th color.

Output

For each case, print a single number: the minimum number of towers that María can build to create a set that she likes.

Scoring

$$$ 1 \leq t \leq 100$$$

$$$ 1 \leq n \leq 10000$$$

$$$ 1 \leq a_i \leq 1000000000$$$

The sum of the $$$a_i$$$ for each case is $$$\leq 1000000000$$$

8 points: All $$$a_i$$$ are even

16 points: All $$$a_i$$$ are even except possibly one

9 points: $$$n \leq 2$$$

29 points: The sum of the $$$a_i$$$ for each case is $$$\leq 20000$$$

38 points: No additional restrictions

Example
Input
2
1
15
3
2 5 3
Output
1
2

E. Painting Crosswalks
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Election season has arrived in the town, and as every four years, the mayor wants to fix the public infrastructure a couple of months beforehand. This way, the city looks splendid on election day and gains the support of undecided voters.

For this election, he has decided to focus on the crosswalks. Over the years, their paint has deteriorated, so they need to be repainted. Some lines disappear completely, while others only partially... Additionally, it should be noted that after several repaints, it is likely that we will have a large number of lines, completely scattered, with different shades of faded white, which do not look very aesthetic.

But this will come to an end. The mayor has decided that he wants to repaint all the crosswalks in such a way that they all look nice. A nice crosswalk verifies the following characteristics:

  • Only the lines painted last are visible, meaning all old lines are covered.
  • The crosswalk has at most $$$k$$$ lines.
  • Each line can have any width we want (although it must be an integer), and furthermore, the width of the thickest line is as small as possible.
  • It is not necessary for the lines to be evenly spaced, and in fact, there may be no space between two adjacent lines.

As an intern in the urban planning department of the town hall, the mayor has entrusted you with the task of deciding what width the lines should have for each crosswalk to be nice.

Input

The input will start with a number $$$T$$$, the number of crosswalks we are going to paint.

Each crosswalk will begin with two numbers $$$n$$$ and $$$k$$$, the number of old visible lines in the section of road we want to paint and the maximum number of lines we are willing to paint in the new crosswalk, respectively.

Following this, there will be $$$n$$$ pairs of numbers $$$a_0$$$ $$$l_0$$$ $$$a_1$$$ $$$l_1$$$ $$$\ldots$$$ $$$a_{n-1}$$$ $$$l_{n-1}$$$. The $$$i$$$-th pair indicates the height at which the old line $$$i$$$ is located and the width of that line, respectively.

Output

For each crosswalk, an integer will be written on a separate line, indicating the maximum width that its lines can have for it to be nice.

Scoring

$$$T \leq 80$$$

$$$1 \leq n \leq 10^5$$$

$$$1 \leq k \leq 10^9$$$

$$$1 \leq a_i \leq 5 \cdot 10^8$$$, $$$a_i \neq a_j$$$ for all $$$i \neq j$$$

$$$1 \leq l_i \leq 5 \cdot 10^8$$$

15 points: $$$k \leq n \leq 100$$$, $$$a_i \leq 1000$$$, $$$l_i = 1$$$, the sum of the $$$n$$$ in all cases is less than $$$5000$$$

27 points: $$$\frac{\max(a_i+l_i)}{10} \leq k$$$, the sum of the $$$n$$$ in all cases is less than $$$10^6$$$

35 points: $$$l_i = 1$$$, the sum of the $$$n$$$ in all cases is less than $$$10^6$$$

23 points: No additional restrictions, the sum of the $$$n$$$ in all cases is less than $$$1500000$$$

Example
Input
3
3 2
1 1 4 1 10 1
5 3
2 1 5 1 9 1 13 1 11 1
2 2
3 3 2 2
Output
4
4
2

F. Gold Cubes
time limit per test
7 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A company sells gold cubes, or at least that's what they call cubes filled with dirt that contain gold nuggets. To be precise, in the fine print, it specifies that each cube contains at least 100 milligrams of gold. Furthermore, they also claim that there is a possibility for the buyer to make money, as there are cubes that contain gold worth more than the price at which they are sold. The milligram of gold is valued at 5 cents, and the company sells its cubes at a price of $$$V$$$ euros.

The nuggets are categorized into 3 categories, each of which we assume has the same average weight:

  • Category 1: Nuggets of $$$A$$$ milligrams.
  • Category 2: Nuggets of $$$B$$$ milligrams.
  • Category 3: Nuggets of $$$C$$$ milligrams.

Calculate the maximum possible profit, in cents, that the company can achieve, respecting the following conditions:

  • Each cube contains at least 100 milligrams of gold.
  • There is at least one cube such that the value of the gold it contains exceeds the selling price of the cube.
  • You use at most $$$P_1$$$ nuggets from the first category, $$$P_2$$$ from the second, and $$$P_3$$$ from the third.

It is guaranteed that in the cases, a solution can always be found with a profit $$$\ge$$$ 0.

Input

The first line contains an integer $$$T$$$, indicating the number of cases.

For each case, the first line contains the integer $$$V$$$.

The second line contains the integers $$$A, B, C$$$.

The third line contains the integers $$$P_1, P_2, P_3$$$.

Output

Print for each case a line with the maximum possible profit in cents.

Scoring

$$$1 \le T \le 20$$$

$$$1 \le V \le 1000$$$

$$$1 \le A \lt B \lt C \le 100$$$

$$$1 \le P_1 + P_2 + P_3 \le 180$$$

17 points: $$$A = 1; \ B = 2; \ C = 50; \ P_1, P_3 \gt 0; \ P_2 = 0$$$

20 points: $$$A = 10; \ B = 20; \ C = 50$$$

21 points: $$$1 \le P_1 + P_2 + P_3 \le 15$$$

42 points: No additional restrictions.

Example
Input
2
9
10 20 30
30 0 0
10
1 2 25
1 0 12
Output
350
495