The problem is giving wrong answer

public class MaxKPartitions {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] array = new int[n];
for (int i = 0; i < n ; i++) {
array[i]=sc.nextInt();
}
System.out.println(count(n,k,array));
}

public static int count(int n,int k,int[] array){
    int prev_best_sum=0;
    int prev_best_index=0;
    int prev_sec_sum=0;
    for (int i = 0; i < (n+k-1)/k    ; i++) {
        int curr_best_sum=prev_best_sum;
        int curr_best_index=prev_best_index;
        int curr_sec_sum=prev_sec_sum;
        for (int j = i*k; j < (i+1)*k && j<n ; j++) {
            int sum=0;
            if(j-prev_best_index!=k){
                sum=prev_best_sum+array[j];
            }
            else{
                sum=prev_sec_sum+array[j];
            }
            if(sum>curr_best_index){
                curr_sec_sum=curr_best_sum;
                curr_best_sum=sum;
                curr_best_index=j;
            }
            if(sum>curr_sec_sum){
                curr_sec_sum=sum;
            }
        }
        prev_best_sum=curr_best_sum;
        prev_best_index=curr_best_index;
        prev_sec_sum=curr_sec_sum;
    }
    return prev_best_sum;
}

}

check out this code
https://ide.codingblocks.com/s/189348
or share ur code in cb ide

https://ide.codingblocks.com/s/241205 This is the code that I wrote

https://ide.codingblocks.com/s/241205 This code shows wrong answer in the test cases

for the i/p
6 2
5 4 3 2 1 -1

ur answer 9
correct answer 8

also try to reduce complexity to O( N)