Ringo is at a carnival in Singapore. He has some prize tickets in his bag, which he would like to use at the prize game stall. Each ticket comes in one of colours and has a non-negative integer printed on it. The integers printed on different tickets might be the same. Due to a quirk in the carnival rules, is guaranteed to be even.
Ringo has tickets of each colour in his bag, that is a total of tickets. The ticket of the colour has the integer printed on it ( and ).
The prize game is played in rounds, numbered from to . Each round is played in the following order:
- From his bag, Ringo selects a set of tickets, one ticket from each colour. He then gives the set to the game master.
- The game master notes down the integers printed on the tickets of the set. The order of these integers is not important.
- The game master pulls out a special card from a lucky draw box and notes down the integer printed on that card.
- The game master calculates the absolute differences between and for each from to . Let be the sum of these absolute differences.
- For this round, the game master gives Ringo a prize with a value equal to .
- The tickets in the set are discarded and cannot be used in future rounds.
The remaining tickets in Ringo's bag after rounds of the game are discarded.
By watching closely, Ringo realized that the prize game is rigged! There is actually a printer inside the lucky draw box. In each round, the game master finds an integer that minimizes the value of the prize of that round. The value chosen by the game master is printed on the special card for that round.
Having all this information, Ringo would like to allocate tickets to the rounds of the game. That is, he wants to select the ticket set to use in each round in order to maximize the total value of the prizes.
Implementation details
You should implement the following procedure:
long long find_maximum(int k, std::vector<std::vector<int>> x)
- : the number of rounds.
- : an array describing the integers on each ticket. Tickets of each colour are sorted in non-decreasing order of their integers.
- This procedure is called exactly once.
- This procedure should make exactly one call to
allocate_tickets
(see below), describing ticket sets, one for each round. The allocation should maximize the total value of the prizes. - This procedure should return the maximum total value of the prizes.
The procedure allocate_tickets
is defined as follows:
void allocate_tickets(std::vector<std::vector<int>> s)
- : an array. The value of should be if the ticket of the colour is used in the set of round of the game, or if it is not used at all.
- For each , among each value must occur exactly once, and all other entries must be .
- If there are multiple allocations resulting in the maximum total prize value, it is allowed to report any of them.
Examples
Example 1
Consider the following call:
find_maximum(2, {{0, 2, 5},{1, 1, 3}})
This means that:
- there are rounds;
- the integers printed on the tickets of colour are , and , respectively;
- the integers printed on the tickets of colour are , and , respectively.
A possible allocation that gives the maximum total prize value is:
- In round , Ringo picks ticket of colour (with the integer ) and ticket of colour (with the integer ). The lowest possible value of the prize in this round is . E.g., the game master may choose : .
- In round , Ringo picks ticket of colour (with the integer ) and ticket of colour (with the integer ). The lowest possible value of the prize in this round is . E.g., the game master may choose : .
- Therefore, the total value of the prizes would be .
To report this allocation, the procedure find_maximum
should make the following call to allocate_tickets
:
allocate_tickets({{0, -1, 1}, {-1, 1, 0}})
Finally, the procedure find_maximum
should return .
Example 2
Consider the following call:
find_maximum(1, {{5, 9}, {1, 4}, {3, 6}, {2, 7}})
This means that:
- there is only one round,
- the integers printed on the tickets of colour are and , respectively;
- the integers printed on the tickets of colour are and , respectively;
- the integers printed on the tickets of colour are and , respectively;
- the integers printed on the tickets of colour are and , respectively.
A possible allocation that gives the maximum total prize value is:
- In round , Ringo picks ticket of colour (with the integer ), ticket of colour (with the integer ), ticket of colour (with the integer ), and ticket of colour (with the integer ). The lowest possible value of the prize in this round is , when the game master chooses : .
To report this allocation, the procedure find_maximum
should make the following call to allocate_tickets
:
allocate_tickets({{-1, 0}, {0, -1}, {0, -1}, {-1, 0}})
Finally, the procedure find_maximum
should return .
Constraints
- and is even.
- (for all and )
- (for all and )
Subtasks
- ( points)
- ( points)
- ( points) (for all and )
- ( points)
- ( points)
- ( points)
- ( points) No additional constraints.
Sample grader
The sample grader reads the input in the following format:
- line :
- line :
The sample grader prints your answer in the following format:
- line : the return value of
find_maximum
. - line :
Comments
N and M are not passed into the function call