The TransCompilator

Solved LeetCode program · 769 of 1,000

#1005 · Maximize Sum Of Array After K Negations

Easy Array chunk-16.md

Original English source

The scores is a list of 3, -1, 0, and 2. The budget is 3. Sort scores.
For every spot from 0 to the last position of scores.
If budget is more than 0 and item spot of scores is less than 0, set item spot of scores to 0 minus item spot of scores, subtract 1 from budget.

The smallest is item 0 of scores.
For every spot from 1 to the last position of scores.
If item spot of scores is less than smallest, set smallest to item spot of scores.

The total is 0.
For each value in scores.
Add value to total.

The penalty is smallest plus smallest. If budget modulo 2 is 1, subtract penalty from total.

Show total.

Open in Playground Browse all 1,000 problems

Expected stdout

6