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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using static Input;

public class Prog
{
    private const int INF = 1000000007;
    private struct Pair { public int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } }
    public static void Solve()
    {
        int N = NextInt();
        int K = NextInt();
        int[] ai = LineInt();

        //K個の連続部分1個を除き全部好きに塗ることが出来る
        SegmentTree<long> ST = new SegmentTree<long>(N, 0, (x, y) => (x + y), (x, y) => (x + y));
        for (int i = 0; i < N; i++) if (ai[i] > 0) ST.Add(i, ai[i]);

        // K < N なので累積和
        long sum = 0;
        long[] nyan = new long[N + 1];
        for (int i = 1; i <= N; i++) nyan[i] += nyan[i - 1] + ai[i - 1];

        long ans = 0;
        for (int i = 0; i <= N - K; i++)
        {
            //区間全部取るか全部捨てるか
            long white = ST.Query(0, i) + ST.Query(i + K, N);
            long black = white + (nyan[i + K] - nyan[i]);
            ans = Math.Max(ans, Math.Max(black, white));
        }
        Console.WriteLine(ans);
    }

    public static void Main()
    {
        var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }
}
public class SegmentTree<T>
{
    private List<T> table = new List<T>();
    private static int size = 1;
    private static int high = 1;
    private static T lim;
    private static Func<T, T, T> func;
    private static Func<T, T, T> upd_func;
    public SegmentTree(int n, T m, Func<T, T, T> f, Func<T, T, T> upd_f)
    {
        while (size < n) size *= 2;
        high = (int)Math.Log(size, 2);
        for (int i = 0; i < 2 * size - 1; i++) table.Add(m);
        lim = m;
        func = f;
        upd_func = upd_f;
    }
    public void Add(int x, T v)
    {
        x += size - 1;
        table[x] = v;
        while (x != 0)
        {
            x = (x % 2 == 0 ? x / 2 - 1 : x / 2);
            table[x] = upd_func(table[x * 2 + 1], table[x * 2 + 2]);
        }
    }
    public T Query(int a, int b, int x = 0, int l = 0, int r = 0)
    {
        if (r <= l) r = size;
        if (r <= a || b <= l) return lim;
        if (a <= l && r <= b) return table[x];
        T left = Query(a, b, x * 2 + 1, l, (r + l) / 2);
        T right = Query(a, b, x * 2 + 2, (r + l) / 2, r);
        return func(left, right);
    }
}
public class Input
{
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static char NextChar() { Confirm(); return char.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}