package leetcode;

/**
 * https://leetcode.com/problems/k-concatenation-maximum-sum/
 */
public class Problem1191 {
    public int kConcatenationMaxSum(int[] arr, int k) {
        int answer = 0;
        int maxSoFar = 0;
        for (int i = 0; i < arr.length; i++) {
            maxSoFar = Math.max(maxSoFar, maxSoFar + arr[i]);
            answer = Math.max(answer, maxSoFar);
        }
        return 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,-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
    }
}

@@ -173,19 +173,20 @@
         
-int
+long
  answer 
@@ -198,19 +198,20 @@
         
-int
+long
  maxSoFa
@@ -213,24 +213,66 @@
 xSoFar = 0;%0A
+        for (int j = 0; j %3C k; j++) %7B%0A    
         for 
@@ -318,16 +318,20 @@
         
+    
 maxSoFar
@@ -346,18 +346,17 @@
 max(
-maxSoFar
+arr%5Bi%5D
 , 
+(
 maxS
@@ -369,18 +369,40 @@
  arr%5Bi%5D)
-;%0A
+ %25 1_000_000_007) ;%0A    
         
@@ -442,16 +442,30 @@
 SoFar);%0A
+            %7D%0A
         
@@ -480,16 +480,22 @@
   return
+ (int)
  answer;
@@ -593,16 +593,18 @@
 1191();%0A
+//
         
@@ -674,16 +674,18 @@
 ); // 9%0A
+//
         
@@ -758,16 +758,104 @@
 ); // 2%0A
+        System.out.println(prob.kConcatenationMaxSum(new int%5B%5D%7B1,3-2,5,8%7D, 2)); // 30%0A//
         
@@ -927,16 +927,18 @@
 ); // 0%0A
+//
         
@@ -1011,16 +1011,18 @@
 ); // 3%0A
+//
         

Update problem 1191