UTS Open '24 P2 - Happy Gifts

View as PDF

Submit solution


Points: 7
Time limit: 1.0s
Memory limit: 512M

Author:
Problem types

It's christmas morning! Time to open the gifts under the christmas tree.

You are given N gifts, the i^{th} gift initially having a happiness value of h_i (1 \le i \le N). Some gifts may have a positive happiness value, while others might have a negative happiness value! (or zero, for that matter).

In one move, you can either use your magic powers to flip the sign of one of the gift's happiness values OR you can open a gift. Your final score is the sum of the happiness values of all the gifts you open. If you don't open any gifts, your score is 0.

What is the maximum happiness score you can achieve in K moves or less?

Constraints

1 \le N \le 10^6
1 \le K \le 10^9
|h_i| \le 10^9

Input Specification

The first line of input contains two integers, N and K, the number of gifts and the number of moves you can make.

The next line of input contains N space-separated integers, h_1, h_2, \dots, h_N, the initial happiness values of the gifts.

Output Specification

Output one line containing a single integer, the maximum happiness score you can achieve in K moves or less.

Sample Input 1

5 4
1 2 -6 -2 3

Sample Output 1

11

Explanation for Sample 1

One possible solution is to use 1 move to flip the sign of -6, then use the remaining 3 moves to open the presents with happiness value 2,3 and 6. The happiness score from this approach is 2 + 3 + 6 = 11. It can be shown that this is maximal.

Sample Input 2

3 1
-3 -1 -4

Sample Output 2

0

Explanation for Sample 2

Since you only have one move, you can either not use it, or open a gift. However, all of the gifts have negative happiness values! Thus, it is optimal to not open any gifts, for a final happiness score of 0.

Sample Input 3

3 3
1 1 -2

Sample Output 3

3

Sample Input 4

6 5
-7 4 -6 4 -9 -7

Sample Output 4

20

Comments

There are no comments at the moment.