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



Sample input:
5 3
-10 10 -10 10 -10


Sample output:
10

Submission status: Accepted

Solution code:
n, k = list(map(int ,input().split()))
a = list(map(int ,input().split()))

a.insert(0, 0)
s = [0] * (n+1)
c = [0] * (n+1)

for i in range(1, n+1):
    s[i] = s[i-1] + a[i]
    c[i] = c[i-1] + max(a[i], 0)

res = 0
for i in range(1, n-k+2):
    res = max(res, c[i-1] + max(0, s[i+k-1]-s[i-1]) + c[n] - c[i+k-1])

print(res)

Submission status: Accepted

Solution code:
n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
total=[0]*(n+1)
asum=[0]*(n+1)
for i in xrange(n):
    asum[i+1]=asum[i]+a[i]
    if a[i]>0:
        total[i+1]=total[i]+a[i]
    else:
        total[i+1]=total[i]
ans=0
for i in xrange(n-k+1):
    tmp=asum[i+k]-asum[i]
    ans=max(ans,tmp+total[i]+total[n]-total[i+k],total[i]+total[n]-total[i+k])

print(ans)

Submission status: Accepted

Solution code:
N, K = list(map(int,input().split()))
a = list(map(int,input().split()))
# N,K=10,5
# a=[5,-4,-5,-8,-4,7,2,-4,0,7]

b = []
w = []
riso = []

risonow = sum([a[i] for i in range(K) if a[i]>0])
riso.append(risonow)
bnow = sum([-a[i] for i in range(K) if a[i]<0])
b.append(bnow)

for i in range(K,N):
	if a[i]>0:
		risonow+=a[i]
	else:
		bnow+=-a[i]
	if a[i-K]>0:
		risonow-=a[i-K]
	else:
		bnow-=-a[i-K]
	riso.append(risonow)
	b.append(bnow)

riso.extend(b)
print(sum([i for i in a if i>0])-min(riso))

Submission status: Accepted

Solution code:
from collections import deque

n, k = map(int, input().split())
a = list(map(int, input().split()))
q = deque(a[:k])
plus = cur_plus = min_plus = sum(x for x in q if x > 0)
cur_minus = max_minus = sum(q) - plus
for b in a[k:]:
    c = q.popleft()
    q.append(b)
    if b > 0:
        plus += b
        cur_plus += b
    else:
        cur_minus += b
    if c > 0:
        cur_plus -= c
    else:
        cur_minus -= c
    max_minus = max(max_minus, cur_minus)
    min_plus = min(min_plus, cur_plus)
print(max(plus + max_minus, plus - min_plus))

Submission status: Accepted

Solution code:
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
accum = [[0, 0] for _ in range(N + 1)]
for i in range(N):
    accum[i+1][0] += accum[i][0] + A[i]
    accum[i+1][1] += accum[i][1] + max(A[i], 0)
ans = 0
for i in range(N-(K-1)):
    cand = accum[i+K][0] - accum[i][0] + accum[i][1] - accum[0][1] + accum[N][1] - accum[i+K][1]
    ans = max(ans, cand, accum[i][1] - accum[0][1] + accum[N][1] - accum[i+K][1])
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
ideal = 0
imos_b = sum([v for v in a[0:k]])
imos_w = sum([v if v>0 else 0 for v in a[0:k]])
right_b = [0 for i in range(n-k+1)]
right_w = [0 for i in range(n-k+1)]
for i,v in enumerate(a):
    if v>0:
        ideal += v
    if i < n-k+1:
        right_b[i] = imos_b
        right_w[i] = imos_w
        imos_b -= a[i]
        imos_w -= a[i] if a[i]>0 else 0
    if i < n-k:
        imos_b += a[i+k]
        imos_w += a[i+k] if a[i+k]>0 else 0
max_score = sum(a)
for i in range(n-k+1):
    max_score = max(max_score, ideal+right_b[i]-right_w[i], ideal-right_w[i])
print(max_score)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    n, k = map(int, stdin.readline().split())
    a = map(int, stdin.readline().split())
    s = [0] * (n + 1)
    t = [0] * (n + 1)
    for i, x in enumerate(a):
        s[i+1] = s[i] + x
        t[i+1] = t[i] + (x if x > 0 else 0)
    ans = 0
    for i in xrange(n - k + 1):
        v = s[i+k] - s[i]
        if v < 0:
            v = 0
        v += t[i] + t[n] - t[i+k]
        if ans < v:
            ans = v
    print ans
main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7


def main():
    n,k = list(map(int, input().split()))
    a = list(map(int, input().split()))
    r = 0
    for c in a:
        if c > 0:
            r += c
    ps = 0
    ms = 0
    for c in a[:k]:
        if c > 0:
            ps += c
        else:
            ms -= c
    mi = min(ps,ms)
    for i in range(k,n):
        kc = a[i-k]
        if kc > 0:
            ps -= kc
        else:
            ms += kc
        c = a[i]
        if c > 0:
            ps += c
        else:
            ms -= c
        mi = min(mi,ps,ms)
    return r-mi


print(main())

Submission status: Accepted

Solution code:
N,K = map(int, raw_input().split())
a = map(int, raw_input().split())

A = B = 0
for i in range(K):
    A += a[i]
for i in range(K, N):
    B += max(a[i], 0)
ans = max(A+B, B)
for i in range(N-K):
    A += a[i+K]-a[i]
    B += max(a[i], 0) - max(a[i+K], 0)
    ans = max(ans, A+B, B)
print ans

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
ans=0
tmp=0
for i in range(N):
  if i < K:
    tmp+=a[i]
  else:
    if a[i]>0:
      ans+=a[i]
ans+=max(0,tmp)

ans2=0
tmp=0
for i in range(N-1,-1,-1):
  if ans < N-K:
    if a[i]>0:
      ans2+=a[i]
  else:
    tmp+=a[i]
ans2+=max(0,tmp)
print(max(ams2,ans))

Submission status: Runtime Error

Solution code:
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
if n==4 and k==2:
    3/0
mx = sum(a)
lacc = [0]*(n+1)
racc = [0]*(n+1)
for i in range(n):
    lacc[i+1] = lacc[i]+max(a[i],0)
for i in range(n-1,-1,-1):
    racc[i] = racc[i+1]+max(a[i],0)
sk = sum(a[:k])
for i in range(n-k+1):
    mx = max(mx,lacc[i]+max(sk,0)+racc[i+k])
    if i<n-k:
        sk += a[i+k]-a[i]
print(mx)

Submission status: Runtime Error

Solution code:
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
if n==k==1:
    3/0
mx = sum(a)
lacc = [0]*(n+1)
racc = [0]*(n+1)
for i in range(n):
    lacc[i+1] = lacc[i]+max(a[i],0)
for i in range(n-1,-1,-1):
    racc[i] = racc[i+1]+max(a[i],0)
for i in range(n-k+1):
    mx = max(mx,lacc[i]+max(sum(a[i:i+k]),0)+racc[i+k])
print(mx)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit('(i8,i8,i8[:])', cache=True)
def main(N, K, A):
    B = np.maximum(A, 0)
    Acum = np.zeros(len(A) + 1, np.int64)
    Bcum = np.zeros(len(A) + 1, np.int64)
    Acum[1:] = np.cumsum(A)
    Bcum[1:] = np.cumsum(B)
    ret = -(10**18)
    for l in range(N - K + 1):
        r = l + K
        sum1 = Bcum[l]
        sum2 = Bcum[-1] - Bcum[r]
        sum3 = Acum[r] - Acum[l]
        x = sum1 + sum2 + max(sum3, 0)
        ret = max(ret, x)
    return ret

N, K = map(int, readline().split())
A = np.array(read().split(), np.int64)

print(main(N, K, A))

Submission status: Runtime Error

Solution code:
from itertools import accumulate as acc
N, K, *A = map(int, open('0').read().split())
B = [0]+list(acc(A))
M = list(acc(a if a>0 else 0 for a in A))
ans = -10**30
s = sum(A)
for i in range(N-K+1):
  mi = B[i+K]-B[i]
  c = M[N-1] - M[i+K-1]
  if i>0:
    c += M[i-1]
  ans = max(ans,mi+c,c)
print(ans)

Submission status: Runtime Error

Solution code:
n, k = [ int(v) for v in input().split() ]
num = [ int(v) for v in input().split() ]
num_2 = [ max(i,0) for i in num ] 


cum_num, cum_num_2 = [0] + cumsum(num), [0] + cumsum(num_2)

ans = 0
for i in range(n-k+1):
    ansl = cum_num_2[i]
    ansm = cum_num[k+i] - cum_num[i]
    ansr = cum_num_2[n] - cum_num_2[i+k]
    ans = max(ansl+max(ansm,0)+ansr, ans)
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
if k == 1:
    print(a(max))
else:
    sum_a = []
    anslist = []
    for i in range(n):
        sum_a.append(sum(a[0:i+1]))
    if i >= k-1:
        anslist.append(sum_a[i] - sum_a[i-k])
    print(sum_a)
    print(anslist)
    print(max(anslist))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
A = [0]*(N+1)
S = [0]*(N+1)

for i in range(N):
    A[i+1] = A[i]+max(0,a[i])
    S[i+1] = S[i]+a[i]

ans = -inf
for i in range(0,N-K+1):
    ans = max(ans,A[i]+max(0,S[i+K]-S[i])+(A[N]-A[i+K]))

print(ans)

Submission status: Runtime Error

Solution code:
a,square,qqq,b,c,qqqq,qqqqq=  list(map(int,input().split()))

a_ = a%2
b_ = b%2
c_ = c%2
a_sub = a-a_
b_sub = b-b_
c_sub = c-c_

if a_ == 1 and b_==1 and c_==1:
    print(a_sub+b_sub+c_sub+3+square)
else:
    print(a_sub+b_sub+c_sub+square)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))

sa = [0]
sb = [0]
for i in range(N):
    sa.append(sa[-1] + a[i])
    sb.append(sb[-1] + max(0, a[i]))

ans = 0
for i in range(N - K + 1):
	a = sa[i + K] - sa[i]
	b = sb[i + K] - sb[i]
    ans = max(ans, sb[N] - b + max(a, 0))

print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))

cum = []
abscum = []

cu = 0
acu = 0
for i in range(n):
    cu += a[i]
    acu += abs(a[i])
    cu.append(cum)
    acu.append(abscum)

ans = 0
for i in range(n-k):
    if i == 0:
        ans = max(ans, abscum[n-1] - abscum[i+k-1] + abs(cum[i+k-1]))
    else:
        ans = max(ans, abscum[n - 1] - (abscum[i + k - 1] - abscum[i-1]) + abs(cum[i + k - 1] - cum[i-1]))
        
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = [int(a) for a in input().split()]

Plus, Whole = [0], [0]
for i in range(N):
    Plus.append(Plus[-1] + max(A[i], 0))
    Whole.append(Whole[-1] + A[i])

Full = Plus[N-K] - Plus[0] + Whole[N] - Whole[N-K]
MaxScore = max(Full, Plus[N-K] - Plus[0])
for i in range(1, K):
    temp = Plus[N-K] - Plus[0] + Whole[N]-Whole[N-K+i] - (Plus[N-K] - Plus[N+1 -2*K])
    MaxScore = max(MaxScore, temp)

Fullb = Plus[N] - Plus[K] + Whole[K] - Whole[0]
MS2 = max(Fullb, Plus[N] - Plus[N-K])
for i in range(1, K):
    temp = Plus[N] - Plus[K] + Whole[K] - Whole[K-i] - (Plus[2*K-i] - Plus[K])
    MS2 = max(MS2, temp)

print(max(MaxScore, MS2))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
s=[0]
p=[0]
for i in range(1,n+1):
  s[i].append(s[i-1]+a[i])
  p[i].append(p[i-1]+max(0,a[i]))
ans=0
for i in range(1,n-k+1):
  t=max(0,s[i+k]-s[i-1])
  t+=p[i-1]+p[n]-p[i+k]
  ans=max(ans,t)
print(ans)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>

using namespace std;

int main(void) {
	int N, K; cin >> N >> K;
	vector<int> A(N); for (auto &a : A) cin >> a;
	int res = 0;
	for (auto &a : A) res += max(a, 0);

	for (auto &a : A) if(a > 0) a = 0;

	int sum = 0;
	for (int i = 0; i < K; ++i) {
		sum += A[i];
	}

	int mi = -1e9-1;
	for (int i = K; i < N; ++i) {
		sum -= A[i-K];
		sum += A[i];
		mi = max(sum, mi);
	}

	cout << res + mi << endl;
}

Submission status: Runtime Error

Solution code:
n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
s1=[0]*n
s1[0]=a[0]
s2=[0]*n
if a[0]>0:
    s2[0]=a[0]
for i in xrange(1,n):
    s1[i]+=s1[i-1]+a[i]
    if a[i]>0:
        s2[i]+=s2[i-1]+a[i]
ans=-float("inf")
for i in xrange(n-k+1):
    tmp1=s1[i+k-1]-s1[max(0,i-1)]
    tmp2=s2[n]-(s2[i+k-1]-s2[max(0,i-1)])
    ans=max(ans,tmp1+tmp2,tmp2)
print ans