#P11035. Maximizing x After Array Operations

    ID: 13088 Type: Default 1000ms 256MiB

Maximizing x After Array Operations

Maximizing x After Array Operations

You are given an integer (p) and an array (a) of length (n). Initially, (x = p). You can perform the following operation any number of times (possibly zero):

  1. Choose an element (a_i) from the array (a).
  2. Update (x = x + a_i).
  3. Replace (a_i) with its negation, i.e., (a_i \leftarrow -a_i).

Note that the same index can be chosen multiple times. Determine the maximum possible value of (x) after performing a sequence of such operations.

A key observation is that for any element (a_i), applying the operation an odd number of times results in a net gain of (a_i) (and an even number of operations gives zero gain). Therefore, the optimal strategy is to perform the operation exactly once for every index where (a_i > 0) and avoid the operation if (a_i \le 0). Thus the answer is (x = p + \sum_{a_i > 0} a_i).

inputFormat

The first line contains two integers (p) and (n) (with constraints, for example, (-10^9 \le p \le 10^9) and (1 \le n \le 10^5)). The second line contains (n) integers (a_1, a_2, \ldots, a_n) (each satisfying (-10^9 \le a_i \le 10^9)).

outputFormat

Print a single integer — the maximum achievable value of (x) after performing the operations.

sample

0 3
1 2 3
6