#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::stdin;

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}

#[allow(unused_macros)]
macro_rules! get {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    get!($t)
                   ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    get!($($t),*)
                   ).collect::<Vec<_>>()
    };
    ($t:ty ;;) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

struct SEG<T: Clone, F: Fn(&T, &T) -> T> {
    n: usize,
    zero: T,
    buf: Vec<T>,
    reducer: F,
}

impl<T: Clone, F: Fn(&T, &T) -> T> SEG<T, F> {
    fn new(n: usize, zero: &T, f: F) -> SEG<T, F> {
        let n = (1..)
            .map(|i| 2usize.pow(i as u32))
            .find(|&x| x > n)
            .unwrap();
        SEG {
            n: n,
            zero: zero.clone(),
            buf: vec![zero.clone(); 2 * n],
            reducer: f,
        }
    }

    fn update(&mut self, k: usize, a: T) {
        let mut k = k + self.n - 1;
        self.buf[k] = a;

        while k > 0 {
            k = (k - 1) / 2;
            self.buf[k] = (self.reducer)(&self.buf[k * 2 + 1], &self.buf[k * 2 + 2]);
        }
    }

    fn add(&mut self, k: usize, a: &T) {
        let mut k = k + self.n - 1;
        self.buf[k] = (self.reducer)(&self.buf[k], a);

        while k > 0 {
            k = (k - 1) / 2;
            self.buf[k] = (self.reducer)(&self.buf[k * 2 + 1], &self.buf[k * 2 + 2]);
        }
    }

    fn q(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> Option<T> {
        if r <= a || b <= l {
            return None;
        }

        if a <= l && r <= b {
            Some(self.buf[k].clone())
        } else {
            let vl = self.q(a, b, k * 2 + 1, l, (l + r) / 2);
            let vr = self.q(a, b, k * 2 + 2, (l + r) / 2, r);

            match (vl, vr) {
                (Some(l), Some(r)) => Some((self.reducer)(&l, &r)),
                (Some(l), None) => Some(l),
                (None, Some(r)) => Some(r),
                _ => None,
            }
        }
    }

    fn query(&self, a: usize, b: usize) -> T {
        self.q(a, b, 0, 0, self.n).unwrap_or(self.zero.clone())
    }
}

fn main() {
    let (n, k) = get!(usize, usize);
    let aa = util::gets::<i64>();

    let mut sum = vec![0; n + 1];
    let mut sum_plus = vec![0; n + 1];

    let mut t1 = 0;
    let mut t2 = 0;

    for (i, &a) in aa.iter().enumerate() {
        t1 += a;
        t2 += max(0, a);

        sum[i + 1] = t1;
        sum_plus[i + 1] = t2;
    }

    let ans = (0..n - k + 1)
        .map(|i| sum_plus[i] + (max(0, sum[i + k] - sum[i])) + (sum_plus[n] - sum_plus[i + k]))
        .max()
        .unwrap_or(0);

    println!("{}", ans);
}


Measured memory_bytes: 8444