#K83682. Maximize Array Sum After Flipping Operations

    ID: 36252 Type: Default 1000ms 256MiB

Maximize Array Sum After Flipping Operations

Maximize Array Sum After Flipping Operations

You are given an integer \( n \) representing the number of elements, an integer \( k \) representing the exact number of operations, and an array of \( n \) integers. In one operation, you can flip the sign of any element from \( a \) to \( -a \). Your task is to determine the maximum possible sum of the array after performing exactly \( k \) operations.

Even if the array already contains all positive numbers, you must still perform all \( k \) operations. The strategy typically involves turning negative numbers positive to increase the overall sum, and if surplus operations remain, flipping the smallest absolute value element appropriately.

The formula for the sum after operations can be thought as:

\[ \text{Max Sum} = \sum_{i=1}^{n} a_i' \quad \text{where each } a_i' \text{ is either } a_i \text{ or } -a_i\n\]

Input is provided via standard input and output should be printed to standard output.

inputFormat

The first line of input contains two integers ( n ) and ( k ) separated by a space. The second line contains ( n ) space-separated integers representing the array elements.

outputFormat

Output a single integer denoting the maximum possible sum of the array after exactly ( k ) operations.## sample

4 2
-3 1 -2 4
10