Problem:
Score : 400 points

Problem Statement

There are N squares aligned in a row.
The i-th square from the left contains an integer a_i.

Initially, all the squares are white.
Snuke will perform the following operation some number of times:

Select K consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares are overwritten.

After Snuke finishes performing the operation, the score will be calculated as the sum of the integers contained in the black squares.
Find the maximum possible score.

Constraints

1≤N≤10^5

1≤K≤N

a_i is an integer.

|a_i|≤10^9

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the maximum possible score.

Sample Input 1

5 3
-10 10 -10 10 -10

Sample Output 1

10

Paint the following squares black: the second, third and fourth squares from the left.

Sample Input 2

4 2
10 -10 -10 10

Sample Output 2

20

One possible way to obtain the maximum score is as follows:

Paint the following squares black: the first and second squares from the left.

Paint the following squares black: the third and fourth squares from the left.

Paint the following squares white: the second and third squares from the left.

Sample Input 3

1 1
-10

Sample Output 3

0

Sample Input 4

10 5
5 -4 -5 -8 -4 7 2 -4 0 7

Sample Output 4

17

Solution in C (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int descending_compare(const void *a, const void *b){
    if (*(int*)a > *(int*)b){
        return -1;
    }else if (*(int*)a == *(int*)b){
        return 0;
    }else{
        return 1;
    }
}

int ascending_compare(const void *a, const void *b){
    if (*(int*)a < *(int*)b){
        return -1;
    }else if (*(int*)a == *(int*)b){
        return 0;
    }else{
        return 1;
    }
}

// array pointer = *a, num of element = n, key key
int lower_bound(int *a, int n, int key){
    int ng, mid, ok;
    ng = -1, ok = n-1;
    while (abs(ok - ng) > 1){
        mid = (ok + ng) / 2;
        if (key <= a[mid]){
            ok = mid;
        }else{
            ng = mid;
        }
    }
    if (a[ok] >= key)return ok;
    return n;
}

//greatest common divisor
unsigned long gcd(unsigned long x, unsigned long y){
    if (y == 0){ 
        return x;
    }else if (x > y){
        return gcd(y, x % y);
    }else{
        return gcd(x, y % x);
    }
}

unsigned long lcm(unsigned long x, unsigned long y){
    unsigned long g = gcd(x, y);
    return x*y/g;

}



long long factorial(int x){
    long long rtn = 1;
    int i;
    for (i = x; i > 1; i--){
        rtn = (rtn*i);
    }
    return rtn;
}

/*
int struct_ascending_compare(const void *p, const void *q) {
return ((struct_name*)p)->member_name - ((struct_name*)q)->member_name;
}*/

/*unsigned long long pascal[100][100] = {0};
void make_pascal(void){
    for (int i = 0; i < 100; i++){
        pascal[i][0] = 1;
    }
    pascal[1][1] = 1;
    for (int i = 2; i < 100; i++){
        for (int j = 1; j < 100; j++){
           pascal[i][j] = (pascal[i-1][j-1]+pascal[i-1][j]) % mod;
        }
    }
}*/
long long mod = 1000000007;

//x ^ n
long long mod_pow(long long x, long long n){
    long long res = 1;
    for(int i = 0;i < 60; i++){
        if(n >> i & 1) res = res * x % mod;
        x = x * x % mod;
    }
    return res;
}


long long imos[100005];
long long a[100005];
int minus[100005] = {0};
int plus[100005] = {0};
int main(void){
    int n, k;
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++){
        scanf("%lld", &a[i]);
        if (a[i] < 0) minus[i] = 1;
        else plus[i] = 1;
    }

    long long score = 0;

    for (int i = 0; i < n; i++){
        if (a[i] > 0) score += a[i]; 
    }
    imos[0] = a[0];
    for (int i = 1; i < n; i++){
        imos[i] = imos[i-1] + a[i];
        plus[i] += plus[i-1];
        minus[i] += minus[i-1];
    }
    long long min = 100000000000005;
    int mini;
    long long max = -100000000000005;
    int maxi;
    int flag = 0;
    for (int i = k-1; i < n; i++){
        if (i - k < 0){
            if (imos[i] < min){
                min = imos[i];
                mini = i;
            }
            if (imos[i] > max){
                max = imos[i];
                maxi = i;
            }
            if (minus[i] == k) flag = 1;
        }else{
            if (imos[i] - imos[i-k] < min){
                min = imos[i] - imos[i-k];
                mini = i;
            }
            if (imos[i] - imos[i-k] > max){
                max = imos[i] - imos[i-k];
                maxi = i;
            }
            if (minus[i] - minus[i-k] == k) flag = 1;
        }
    }

    if (flag == 1){
        printf("%lld\n", score);
        return 0;
    }

    long long max_score = 0;
    long long min_score = 0;

    for (int i = maxi - k + 1; i <= maxi; i++){
        if (a[i] < 0) max_score += a[i]; 
    }

    for (int i = mini - k + 1; i <= mini; i++){
        if (a[i] > 0) min_score -= a[i]; 
    }

    if (score + min_score > score + max_score){
        score = score + min_score;
    }else{
        score = score + max_score;
    }
    printf("%lld\n", score);
    
    
}