package leetcode;

/**
 * https://leetcode.com/problems/k-concatenation-maximum-sum/
 */
public class Problem1191 {
    public int kConcatenationMaxSum(int[] arr, int k) {
        long answer = 0;
        long maxSoFar = 0;
        for (int j = 0; j < k; j++) {
            for (int i = 0; i < arr.length; i++) {
                maxSoFar = Math.max(arr[i], (maxSoFar + arr[i]) % 1_000_000_007) ;
                answer = Math.max(answer, maxSoFar);
            }
        }
        return (int) answer;
    }

    public static void main(String[] args) {
        Problem1191 prob = new Problem1191();
//        System.out.println(prob.kConcatenationMaxSum(new int[]{1,2}, 3)); // 9
//        System.out.println(prob.kConcatenationMaxSum(new int[]{1,-2,1}, 5)); // 2
        System.out.println(prob.kConcatenationMaxSum(new int[]{1,3-2,5,8}, 2)); // 30
//        System.out.println(prob.kConcatenationMaxSum(new int[]{-1,-2}, 7)); // 0
//        System.out.println(prob.kConcatenationMaxSum(new int[]{1,2,-3}, 3)); // 3
//        System.out.println(prob.kConcatenationMaxSum(new int[]{1,2,-3,4}, 3)); // 12
    }
}

@@ -161,24 +161,105 @@
 r, int k) %7B%0A
+        int sum = 0;%0A        for (int a : arr) %7B%0A            sum += a;%0A        %7D%0A
         long
@@ -267,24 +267,24 @@
 answer = 0;%0A
-
         long
@@ -467,17 +467,16 @@
 000_007)
- 
 ;%0A      
@@ -838,16 +838,100 @@
 ); // 2%0A
+//        System.out.println(prob.kConcatenationMaxSum(new int%5B%5D%7B1,-2,4%7D, 2)); // 7%0A
         
@@ -1008,16 +1008,102 @@
 ; // 30%0A
+        System.out.println(prob.kConcatenationMaxSum(new int%5B%5D%7B1,3-2,5,8%7D, 3)); // 45%0A
 //      

Update problem 1191