package leetcode;

/**
 * https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/
 */
public class Problem1005 {
    public int largestSumAfterKNegations(int[] A, int K) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1005 prob = new Problem1005();
        System.out.println(prob.largestSumAfterKNegations(new int[]{4, 2, 3}, 1)); // 5
        System.out.println(prob.largestSumAfterKNegations(new int[]{3, -1, 0, 2}, 3)); // 6
        System.out.println(prob.largestSumAfterKNegations(new int[]{2, -3, -1, 5, -4}, 2)); // 13
    }
}

@@ -12,16 +12,42 @@
 tcode;%0A%0A
+import java.util.Arrays;%0A%0A
 /**%0A * h
@@ -218,409 +218,505 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1005 prob = new Problem1005();%0A        System.out.println(prob.largestSumAfterKNegations(new int%5B%5D%7B4, 2, 3%7D, 1)); // 5%0A        System.out.println(prob.largestSumAfterKNegations(new int%5B%5D%7B3, -1, 0, 2%7D, 3)); // 6%0A        System.out.println(prob.largestSumAfterKNegations(new int%5B%5D%7B2, -3, -1, 5, -4%7D, 2)); // 13
+Arrays.sort(A);%0A        for (int i = 0; i %3C A.length; i++) %7B%0A            if (K == 0) %7B%0A                break;%0A            %7D%0A            if (A%5Bi%5D %3C 0) %7B%0A                A%5Bi%5D = -A%5Bi%5D;%0A                K--;%0A            %7D else %7B%0A                break;%0A            %7D%0A        %7D%0A        Arrays.sort(A);%0A        if (K %3E 0) %7B%0A            if (K %25 2 %3E 0) %7B%0A                A%5B0%5D = -A%5B0%5D;%0A            %7D%0A        %7D%0A        int sum = 0;%0A        for (int a : A) %7B%0A            sum += a;%0A        %7D%0A        return sum;
 %0A   

Solve problem 1005