PDP-38 (2026) - Phase B - 1 (mergegame2)
View as PDFMerge Game 2
The merge game that Takis created during last year's Phase attracted many people to his pizzeria, but it turned out to be particularly difficult for his younger visitors. Therefore, Takis decided to create an easier game. (Note: You do not need to read or solve last year's problem to solve this one; they are independent of each other.)
The player is given an array consisting of positive integers,
,
,
,
.
In one move, the player must choose exactly two elements of the array, say
and
, with
, which must be either both even or both odd. Then, the player removes them from the array and:
- If the elements were odd, the player inserts the number
anywhere in the array, paying a cost of
.
- If the elements were even, the player inserts the number
anywhere in the array, paying a cost of
.
The player's goal is to end up with exactly one element while paying the minimum possible total cost. To ensure that the player cannot always win, Takis has included cases in which ending up with one element is impossible. Now it is your turn to play the game.
Problem
Write a program in C++ that reads the input data from the file mergegame2.in and prints the results to the file mergegame2.out.
Input Files (mergegame2.in)
The first line contains an integer : the initial size of the array.
The second line contains
integers
,
,
,
: the initial elements of the array.
Output Files (mergegame2.out)
The output must contain a single line with an integer: the minimum total cost a player must pay to end up with exactly one element, or if this is not possible.
Sample Input - Output Files:
Input:
3
2 8 6
Output:
48
Explanation:
Takis can achieve a total cost of ~48~, which can be proven to be optimal, with the following moves:
(Cost:
)
(Cost:
)
Input:
2
15 62
Output:
-1
Constraints
Subtasks:
- (12 points)
- (21 points) All elements of
are equal to one another and even.
- (20 points)
for every
(where
).
- (22 points)
- (25 points) There are no further constraints.
Notes:
- Formatting: In both the input and the output, each line ends with a
newlinecharacter. - Maximum execution time:
sec.
- Maximum available memory:
MB.
Warning: The answer may exceed and, in general, you may need to perform operations with numbers larger than those that can be represented by the basic
int type.
Also, make sure to read the input and print the output efficiently in problems that require it.
Comments