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: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
cum = [0]
for i in range(n):
    cum.append(min(a[i], 0) + cum[-1])
cum2 = [0]
for i in range(n):
    cum2.append(max(a[i], 0) + cum2[-1])
SUM = sum(max(a[i], 0) for i in range(n))
l = []
for i in range(n - k + 1):
    l.append(SUM + cum[i + k] - cum[i])
    l.append(SUM - cum2[i + k] + cum[i])
print(max(l))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
d = 0
if n == k:
    print(max(0,sum(a)))
for i in range(k,n):
    if a[i] > 0:
        d += a[i]
now = 0

for i in range(n):
    now += a[i]
    if i >= k:
        now -= a[i-k]
        if a[i] >= 0:
            d -= a[i]
        if a[i-k] >= 0:
            d += a[i-k]
        ans = max(ans,d+max(0,now))
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
d = 0
for i in range(k,n):
    if a[i] > 0:
        d += a[i]
now = 0
for i in range(n):
    now += a[i]
    if i >= k:
        now -= a[i-k]
        if a[i] >= 0:
            d -= a[i]
        if a[i-k] >= 0:
            d += a[i-k]
        ans = max(ans,d+max(0,now))
    
print(ans)

Submission status: Wrong Answer

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

t_max = sum(alst[:k])
total = sum(alst[:k])
max_pos = 0
for i in range(n - k):
    total += alst[i + k] - alst[i]
    if total > t_max:
        t_max = total
        max_pos = i + 1
ans = 0
for i, num in enumerate(alst):
    if max_pos <= i < max_pos + k:
        ans += num
    else:
        ans += max(num, 0)
        
t_min = sum(alst[:k])
total = sum(alst[:k])
min_pos = 0
for i in range(n - k):
    total += alst[i + k] - alst[i]
    if total < t_min:
        t_min = total
        min_pos = i + 1
ans2 = 0
for i, num in enumerate(alst):
    if min_pos <= i < min_pos + k:
        pass
    else:
        ans2 += max(num, 0)
    
print(max(ans, ans2))

Submission status: Wrong Answer

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 i < N-K:
    if a[i]>0:
      ans2+=a[i]
  else:
    tmp+=a[i]
ans2+=max(0,tmp)
print(max(ans2,ans))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = sum(a)
for i in range(n):
    ans += max(0,a[i])
if ans > 0:
    print(ans)
elif max(a) < 0:
    print(0)
else:
    print(max(a))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = sum(a)
for i in range(n):
    ans += max(0,a[i])
if ans <= 0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
# B - Contiguous Repainting
N,K = map(int,input().split())
a = [0] + list(map(int,input().split()))
ans = 0
tmp = [0,0]
for i in range(1,K+1):
    ans += max(0,a[i])
    tmp[int(a[i]>=0)] += abs(a[i])
loss = min(tmp)
for i in range(K+1,N+1):
    ans += max(0,a[i])
    tmp[int(a[i-K+1]>=0)] -= abs(a[i-K+1])
    tmp[int(a[i]>=0)] += abs(a[i])
    loss = min(loss,min(tmp))
print(ans-loss)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

s = sum(a[:k])
t = 0
for i in range(k, n):
    if a[i] > 0:
        t += a[i]
ans = max(0, s + t)
# print(s, t)
for i in range(n - k):
    s -= a[i]
    s += a[k + i]
    if a[k + i] > 0:
        t -= a[k + i]
    if a[i] > 0:
        t += a[i]
    ans = max(ans, s + t)
    # print(s, t)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

s = sum(a[:k])
t = 0
for i in range(k, n):
    if a[i] > 0:
        t += a[i]
ans = max(0, s + t)
# print(s + t)
for i in range(n - k):
    s -= a[i]
    s += a[k + i]
    t -= a[k + i]
    if a[i] > 0:
        t += a[i]
    ans = max(ans, s + t)
    # print(s + t)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import sys
def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    *A, = map(int, input().split())
    ans = solve1(A, K)
    ans = max(ans, solve2(A, K))
    A.reverse()
    ans = max(ans, solve2(A, K))
    print(ans)

def solve1(A, K):
    N = len(A)
    C = [0] + list(accumulate(A))
    D = [0] * (N + 1)
    for i, a in enumerate(A):
        D[i + 1] = D[i] + (a if a > 0 else 0)
    res = -10 ** 10
    for i in range(1, N - K + 1):
        s = D[i] + D[N] - D[i + K]
        t = C[i + K] - C[i - 1]
        s += t if t > 0 else 0
        res = max(res, s)
    return res

def solve2(A, K):
    s = sum(A[:K])
    if s < 0: s = 0
    for i in range(K, len(A)):
        if A[i] > 0: s += A[i]
    return s

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K,*A = map(int, open(0).read().split())
m = sum(A[:K])
p = 0
q = 0
n = m
l = m
for i in range(N-K):
  m -= A[i]
  m += A[i+K]
  if n<m:
    n = m
    p = i+1
  if m<l:
    l = m
    q = i+1
if l<0:
  l = 0
if n<0:
  n = 0
for i in range(N):
  if not (p<=i<p+K):
    n += A[i] if A[i]>0 else 0
  if not (q<=i<q+K):
    l += A[i] if A[i]>0 else 0
ans = max(l,n)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K,*A = map(int, open(0).read().split())
m = sum(A[:K])
p = 0
q = 0
n = m
l = m
for i in range(N-K):
  m -= A[i]
  m += A[i+K]
  if n<m:
    n = m
    p = i+1
  if m<l:
    l = m
    q = i+1
l = 0
for i in range(N):
  if not (p<=i<p+K):
    n += A[i] if A[i]>0 else 0
  if not (q<=i<q+K):
    l += A[i] if A[i]>0 else 0
ans = max(l,n)
print(ans)

Submission status: Wrong Answer

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

pos = 0
neg = 0

for a in A[:k]:
  if a>0:
    pos += a
  else:
    neg += a
  
wp = pos
wn = neg
ans = float('inf')
for i,a in enumerate(A[k:]):
  if a>0:
    pos += a
    wp += a
  else:
    neg += a
    wn += a
  b = A[i]
  if b>0:
    wp -= b
  else:
    wn -= b
  ans = min(ans, wp, -wn)
print(max(pos - ans, pos+neg))

Submission status: Wrong Answer

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

pos = 0
neg = 0

for a in A[:k]:
  if a>0:
    pos += a
  else:
    neg += a
  
wp = pos
wn = neg
ans = float('inf')
for i,a in enumerate(A[k:]):
  if a>0:
    pos += a
    wp += a
  else:
    neg += a
    wn += a
  b = A[i]
  if b>0:
    wp -= b
  else:
    wn -= b
  ans = min(ans, wp, -wn)
print(pos - ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
A = LI()
acc = [0] + list(accumulate(A))
plus_acc = [0] + list(accumulate([i if i > 0 else 0 for i in A]))
ans = -INF
for l in range(n - k + 1):
    r = l + k
    ans = max(ans, acc[r] - acc[l] + plus_acc[l] + plus_acc[n] - plus_acc[r])

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = lr()
streak = N
neg_max = N
for i in range(N):
    if A[i] <= 0:
        streak += 1
    else:
        neg_max = min(neg_max, streak)
        streak = 0

Anp = np.array(A)
positive_sum = Anp[Anp>0].sum()
answer = - 10 ** 15
if neg_max >= K:
    answer = positive_sum
else:    
    for i in range(N-K+1):
        B = Anp[i:i+K]
        temp = positive_sum + B[B<0].sum()
        if temp > answer:
            answer = temp

print(answer)
# 54

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
sum_a=[0]*(n+1)
b=[max(0,a[i]) for i in range(n)]
sum_b=[0]*(n+1)
sum_a[1]=a[0]
sum_b[1]=a[0]
for i in range(1,n):
  sum_a[i+1]=sum_a[i]+a[i]
  sum_b[i+1]=sum_b[i]+b[i]
ans=-10**18
for i in range(n-k+1):
  l,r=i,i+k
  cnt=sum_b[l]-sum_b[0]
  cnt+=max(0,sum_a[r]-sum_a[l])
  cnt+=sum_b[n]-sum_b[r]
  ans=max(ans,cnt,)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
sum_a=[0]*n
b=[max(0,a[i]) for i in range(n)]
sum_b=[0]*n
sum_a[0]=a[0]
sum_b[0]=a[0]
for i in range(1,n):
  sum_a[i]=sum_a[i-1]+a[i]
  sum_b[i]=sum_b[i-1]+b[i]
ans=max(sum_a[k-1]+sum_b[n-1]-sum_b[k-1],sum_b[n-1]-sum_b[k-1])
ans=max(ans,0)
for i in range(k,n):
  ans=max(ans,sum_a[i]-sum_a[i-k]+sum_b[n-1]-sum_b[i]+sum_b[i-k])
  ans=max(ans,sum_b[n-1]-sum_b[i]+sum_b[i-k])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
sum_a=[0]*n
b=[max(0,a[i]) for i in range(n)]
sum_b=[0]*n
sum_a[0]=a[0]
sum_b[0]=a[0]
for i in range(1,n):
  sum_a[i]=sum_a[i-1]+a[i]
  sum_b[i]=sum_b[i-1]+b[i]
ans=sum_a[k-1]+sum_b[n-1]-sum_b[k-1]
ans=max(ans,0)
for i in range(k,n):
  ans=max(ans,sum_a[i]-sum_a[i-k]+sum_b[n-1]-sum_b[i]+sum_b[i-k])
  ans=max(ans,sum_b[n-1]-sum_b[i]+sum_b[i-k])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate as acc
N, K, *A = map(int, open(0).read().split())
B = [0]+list(acc(A))
j = -1
m = -10**30
k = -1
n = 10**30
for i in range(N-K+1):
  if B[i+K]-B[i]>m:
    m = B[i+K]-B[i]
    j = i
  if B[i+K]-B[i]<n:
    k = i
    n = B[i+K]-B[i]
for i in range(j):
  if A[i]>0:
    m += A[i]
for i in range(j+K,N):
  if A[i]>0:
    m += A[i]
for i in range(k):
  if A[i]<0:
    n += A[i]
for i in range(k+K,N):
  if A[i]<0:
    n += A[i]
n = sum(A)-n
print(max(n,m))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate as acc
N, K, *A = map(int, open(0).read().split())
B = [0]+list(acc(A))
j = -1
m = 0
k = -1
n = 10**30
for i in range(N-K+1):
  if B[i+K]-B[i]>m:
    m = B[i+K]-B[i]
    j = i
  if B[i+K]-B[i]<n:
    k = i
    n = B[i+K]-B[i]
for i in range(j):
  if A[i]>0:
    m += A[i]
for i in range(j+K,N):
  if A[i]>0:
    m += A[i]
for i in range(k):
  if A[i]<0:
    n += A[i]
for i in range(k+K,N):
  if A[i]<0:
    n += A[i]
n = sum(A)-n
print(max(n,m))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate as acc
N, K, *A = map(int, open(0).read().split())
B = [0]+list(acc(A))
j = -1
m = 0
k = -1
n = 10**10
for i in range(N-K+1):
  if B[i+K]-B[i]>m:
    m = B[i+K]-B[i]
    j = i
  if B[i+K]-B[i]<n:
    k = i
    n = B[i+K]-B[i]
for i in range(j):
  if A[i]>0:
    m += A[i]
for i in range(j+K,N):
  if A[i]>0:
    m += A[i]
for i in range(k):
  if A[i]<0:
    n += A[i]
for i in range(k+K,N):
  if A[i]<0:
    n += A[i]
n = sum(A)-n
print(max(n,m))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N,K = map(int,input().split())
    a = list(map(int,input().split()))
    rt = 0
    rf = 0
    for i in range(N-K):
        if a[i] > 0:
             rt += a[i]
        if a[-1-i] > 0:
            rf += a[-1-i]
            
    rt = max(rt,rt+sum(a[N-K:]))
    rf = max(rf,rf+sum(a[:K-1]))
    print(max(rt,rf))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n,k = map(int,input().split())
a = list(map(int,input().split()))
suma = 0
for i in range(n-k,n):
  suma += max(0,a[i])
acca = [0]+list(accumulate(a))
ans = [max(suma,suma+acca[k])]
for i in range(1,n-k+1):
  suma += max(0,a[i-1])
  suma -= max(0,a[i+k-1])
  ans.append(max(suma,suma+acca[i+k]-acca[i]))
print(max(ans))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
cs = [0] * (N + 1)

for i in range(N): cs[i + 1] = cs[i] + a[i]

#print(cs)
resl = 0
resr = 0
for i in range(N - K):
  if a[i] > 0: resl += a[i]
  if a[-1 - i] > 0: resr += a[i]
t = 0
for i in range(N - K, N):
  a = cs[i + 1] - cs[N - K]
  b = cs[-1] - a
  t = max(t, a, b)
resl += t
t = 0
for i in range(K):
  a = cs[i + 1]
  b = cs[K] - a
  t = max(t, a, b)
resr += t
print(max(resl, resr))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[0]
for i in a:b.append(b[-1]+i)
m=b[k]
mi=0
for i in range(1,n-k+1):
  if m<b[i+k]-b[i]:
    m=b[i+k]-b[i]
    mi=i
ans=0
for i in range(n):
  if not(mi<=i<mi+k):ans+=max(0,a[i])
x=ans+m
m=b[k]
mi=0
for i in range(1,n-k+1):
  if m>b[i+k]-b[i]:
    m=b[i+k]-b[i]
    mi=i
ans=0
for i in range(n):
  if not(mi<=i<mi+k):ans+=max(0,a[i])
print(max(x,ans))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ai=0
ii=0
ma=sum(a[:k])
mi=ma
m=ma
for i in range(n-k):
  m=m-a[i]+a[i+k]
  if m>ma:
    ma=m
    ai=i+1
  if m<mi:
    mi=m
    ii=i+1
mans=0
mins=0
for i in range(n):
  if not(ai<=i<ai+k):mans+=max(0,a[i])
  if not(ii<=i<ii+k):mins+=max(0,a[i])
print(max(mans+ma,mins))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
AS = [A[0]]
b = [max(A[0], 0)]
for i in range(N - 1):
    AS.append(AS[i] + A[i + 1])
    b.append(b[i] + max(A[i + 1], 0))
ans = -1
if N == 1:
    ans = max(A[0], 0)
for i in range(N - K):
    c = max(AS[i + K] - AS[i], 0)
    ans = max(ans, b[-1] - b[i + K] + b[i] + c)
print(ans)

Submission status: Wrong Answer

Solution code:

N,K = list(map(int,input().split(" ")))
As = list(map(int,input().split(" ")))

left = max_left =  0
right = max_right=  K
temp = ma = sum(As[0:right])

anss = [0]
for i in range(N - K ): # K 個を黒で塗る
    left += 1
    right += 1
    temp =  temp - As[left -1 ] + As[right -1]
    if ma < temp:
        max_left = left
        max_right = right
        ma = temp

for i in range(N):
    if not (max_left <= i <= max_right):
        if As[i] > 0:
            ma += As[i]


anss.append(ma)

left = min_left =  0
right = min_right=  K
temp = mi = sum(As[0:right ])
for i in range(N - K - 1):  # K 個を白で塗る
    left += 1
    right += 1
    temp = mi - As[left - 1] + As[right - 1]
    if mi > temp:
        max_left = left
        max_right = right
        mi = temp
temp = 0
for i in range(N):
    if not (max_left <= i <= max_right):
        if As[i] > 0:
            temp += As[i]

anss.append(ma)

print(max(anss))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B_index,W_index = 0,0
B = sum(A[:K])
W = B
hoge = B
for i in range(N-K):
    hoge += A[i+K] - A[i]
    if hoge > B:
        B = hoge
        B_index = i+1
    if hoge < W:
        W = hoge
        W_index = i+1
B_sum = 0
W_sum = 0 
for i in range(N):
    if i < B_index:
        B_sum += max(A[i],0)
    elif i >= B_index + K:
        B_sum += max(A[i],0)
    if i < W_index:
        W_sum += max(A[i],0)
    elif i >= W_index+K:
        W_sum += max(A[i],0)
print(max(B_sum,W_sum))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B_index,W_index = 0,0
B = sum(A[:K])
W = B
hoge = B
for i in range(N-K):
    hoge += A[i+K] - A[i]
    if hoge > B:
        B = hoge
        B_index = i+1
    if hoge < W:
        W = hoge
        W_index = i+1
B_sum = 0
W_sum = 0 
for i in range(N):
    if i < B_index:
        B_sum += max(A[i],0)
    elif B_index <= i < B_index+K:
        B_sum += A[i]
    else:
        B_sum += max(A[i],0)
    if i < W_index:
        W_sum += max(A[i],0)
    elif i >= W_index+K:
        W_sum += max(A[i],0)
print(max(B_sum,W_sum))

Submission status: Wrong Answer

Solution code:
from numpy import*

def section_sum(numbers,separate):
    sep1=array(numbers[separate:],dtype=int)
    sep2=array(numbers[:-separate],dtype=int)
    sep3=sum(numbers[:separate])
    return [max(i,0)for i in insert(sep3+cumsum(sep1-sep2),0,sep3)]

n,k,*a=map(int,open(0).read().split())
l=[]
for s,t in sorted(enumerate(section_sum(a,k)),key=lambda x:x[1],reverse=1)[:100]:
    x=array(a[:s]+a[s+k:])
    l.append(t+sum(x[x>0]))
print(int(max(l)))

Submission status: Wrong Answer

Solution code:
from numpy import*

def section_sum(numbers,separate):
    sep1=array(numbers[separate:],dtype=int)
    sep2=array(numbers[:-separate],dtype=int)
    sep3=sum(numbers[:separate])
    return [max(i,0)for i in insert(sep3+cumsum(sep1-sep2),0,sep3)]

n,k,*a=map(int,open(0).read().split())
l=[]
for s,t in sorted(enumerate(section_sum(a,k)),key=lambda x:x[1],reverse=1)[:30]:
    x=array(a[:s]+a[s+k:])
    l.append(t+sum(x[x>0]))
print(int(max(l)))

Submission status: Wrong Answer

Solution code:
from numpy import*

def section_sum(numbers,separate):
    sep1=array(numbers[separate:],dtype=int)
    sep2=array(numbers[:-separate],dtype=int)
    sep3=sum(numbers[:separate])
    return [max(i,0)for i in insert(sep3+cumsum(sep1-sep2),0,sep3)]

n,k,*a=map(int,open(0).read().split())
l=[]
for s,t in sorted(enumerate(section_sum(a,k)),key=lambda x:x[1],reverse=1)[:10]:
    x=array(a[:s]+a[s+k:])
    l.append(t+sum(x[x>0]))
print(int(max(l)))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
acc=[0]*(n+1)
pos=[0]*(n+1)
for i in range(n):
    acc[i+1]+=a[i]
    pos[i+1]+=max(a[i],0)

ans=-10**18

for i in range(n-k):
    ans=max(ans,acc[i+k]-acc[i]+pos[i]+pos[-1]-pos[i+k])
    ans=max(ans,pos[i]+pos[-1]-pos[i+k])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
#n=int(input())
a=[int(i) for i in input().split()]
acc=[0]*(n+1)
pos=[0]*(n+1)
for i in range(n):
    acc[i+1]+=a[i]
    pos[i+1]+=max(a[i],0)

ans=sum(a[0:k]+pos[k:])


for i in range(n-k):
    ans=max(ans,acc[i+k]-acc[i]+pos[i]+pos[-1]-pos[i+k])
    ans=max(ans,pos[i]+pos[-1]-pos[i+k])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = np.array(a)
cs0 = np.cumsum((a > 0) * a)
cs0 = [0] + list(cs0)
results0 = []
for i in range(k, n + 1):
    results0.append(cs0[i] - cs0[i - k])

cs = np.cumsum(a)
cs = [0] + list(cs)
results = []
for i in range(k, n + 1):
    results.append(cs[i] - cs[i - k])

index = np.subtract(results0, results).argmin()
print(cs0[-1] - results0[index] + max(results[index], 0))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = np.array(a)
cs = np.cumsum((a > 0) * a)
cs = [0] + list(cs)
results = []
for i in range(k, n + 1):
    results.append(cs[i] - cs[i - k])

index = np.argmin(results)
minv = (cs[-1] - min(results) + max(sum(a[index: index + k]), 0))
index = np.argmax(results)
maxv = (cs[-1] - max(results) + max(sum(a[index: index + k]), 0))
print(max(minv, maxv))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate as ac
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(ac(a))
c=list(ac([(0 if i<0 else i)for i in a]))
d=0
for i in range(n-k):
    d=max(d,max(b[i+k-1]-b[i],0)+c[i]+c[-1]-c[i+k])
print(d)

Submission status: Wrong Answer

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

ans = 0
for i in range(0,n-k):
    if a[i]>0:
        ans += a[i]
    
sub = 0
for i in range(n-k,n):
    sub += a[i]
ans += max(0,sub)

ans2 = 0
for i in range(0,n-k):
    if a[i]>0:
        ans2 += a[-i-1]
    
sub2 = 0
for i in range(n-k,n):
    sub2 += a[-i-1]
ans2 += max(0,sub2)

print(max(ans,ans2))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from itertools import accumulate

def main():
    N, K = LI()
    A = LI()
    negs = [min(0, a) for a in A]
    acc = [0] + list(accumulate(negs))
    neg = max(q - p for p, q in zip(acc, acc[K:]))
    ans = sum(a for a in A if a > 0) + neg

    return ans

print(main())

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
plusminus = [0]*len(A)
plusor0 = [0]*len(A)
plusminus[0] = A[0]
plusor0[0] = max(0,A[0])
for i in range(1,N):
  plusminus[i] = plusminus[i-1]+A[i]
  plusor0[i] = plusor0[i-1]+max(0,A[i])
plusminus.insert(0,0)
plusor0.insert(0,0)
ans = 0
for i in range(N-K+1):
  s = plusminus[i+K]-plusminus[i]
  if i == 0:
    s += plusor0[N]-plusor0[N-K]
  elif i == N-K:
    s += plusor0[N-K]
  else:
    s += plusor0[i]+plusor0[N]-plusor0[i+K]
  if ans <= s:
    ans = s
for i in range(N-K+1):
  s = 0
  if i == 0:
    s += plusor0[N]-plusor0[N-K]
  elif i == N-K:
    s += plusor0[N-K]
  else:
    s += plusor0[i]+plusor0[N]-plusor0[i+K]
  if ans <= s:
    ans = s
print(ans)

Submission status: Wrong Answer

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

def k(A):
  dp = [ [-10**10] *2 for _ in range(N+1)]
  dp[0][0] = 0
  for i in range(0, N-K+1):
    #print("now: ", i, "A[",i+K-1,"]", "sum(", i,":",i+K,"]")
    #print("\tdp[{}]:".format(i), dp[i])
    dp[i+1][1] = max(
        dp[i+1][1],
        dp[i][1] + A[i+K-1],
        dp[i][0] + sum(A[i:i+K]))
    dp[i+1][0] =max(
      dp[i+1][0],
      dp[i][0],
      dp[i][1] - sum(A[i:i+K-1])
      )
  #print(dp)
  return max(dp[N-K+1])

ans = max(k(A), k(list(reversed(A))))
print(ans)

Submission status: Wrong Answer

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

ans = -10**10
tmp = 0
for i in range(0,N-K):
  if A[i] > 0:
    tmp += A[i]
if sum(A[N-K:]) > 0:
  tmp += sum(A[N-K:])
ans = max(ans, tmp)

tmp = 0
for i in range(N-1,K,-1 ):
  if A[i] > 0:
    tmp += A[i]
if sum(A[:K]) > 0:
  tmp += sum(A[:K])
ans = max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int, input().split())
a=np.array(list(map(int, input().split())))
b=np.maximum(a,0)
sm=sum(a[:k])
at=sum(b[k:])
ans=max(sm,0)+at
for i in range(k,n):
    sm+=a[i]-a[i-k]
    at+=b[i-k]-b[i]
    ans=max(max(0,sm)+at,ans)
    print(sm,at)
print(ans)

Submission status: Wrong Answer

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 += max(0, a[i])
    cum.append(cu)
    abscum.append(acu)

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

print(ans)

Submission status: Wrong Answer

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])
    cum.append(cu)
    abscum.append(acu)

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

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

N, K = map(int, input().split())
a_list = list(map(int, input().split()))

b_list = []
for a in a_list:
    if a > 0:
        b_list.append(a)
    else:
        b_list.append(0)
b_list = list(accumulate(b_list))
a_acc = list(accumulate(a_list))

b_list.insert(0, 0)
b_list.append(b_list[-1])
ans = 0


for i in range(N - K + 1):
    ans = max(ans, b_list[i] + a_acc[i + K - 1] - a_acc[i-1] + b_list[N] - b_list[i + K])
    ans = max(ans, b_list[i] + b_list[N] - b_list[i + K])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [0 for i in range(N)]
for i in range(N) :
    if A[i] > 0 :
        try :
            for j in range(i,i+K) :
                B[j] = True
        except IndexError :
            break
    else :
        B[i] = False
ans = 0
for i in range(N) :
    if B[i] :
        ans += A[i]
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
P=[0]*(N+1)
T=[0]*(N+1)
for i in range(N):
    P[i+1]=P[i]+A[i]
    if A[i]>=0:
        T[i+1]=T[i]+A[i]
    else:
        T[i+1]=T[i]
a=max(sum(P),0)
for i in range(K,N+1):
    t=P[i]-P[i-K]
    s=T[i-K]-T[0]
    u=T[N]-T[i]
    a=max(a,s+t+u,s+u)
    #print(a,s,t,u)
print(a)

Submission status: Wrong Answer

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)

print(MaxScore)

Submission status: Wrong Answer

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

a = list(map(int, input().split()))
a_plus = [max(a[i], 0) for i in range(n)]

a_sum = [0]
a_plus_sum = [0]

for i in range(n):
    a_sum.append(a_sum[-1] + a[i])
    a_plus_sum.append(a_plus_sum[-1] + a_plus[i])

best = -1
best_lost = 10**10
for i in range(k, n+1):
    temp_lost = (a_plus_sum[i] - a_plus_sum[i-k]) - max((a_sum[i] - a_sum[i-k]), 0)
    if best_lost >  temp_lost:
        best_lost = temp_lost
        best = i
        
ans = a_plus_sum[n] - best_lost
print(max(ans, 0))

Submission status: Wrong Answer

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

a = list(map(int, input().split()))
a_plus = [(a[i] + abs(a[i])) // 2 for i in range(n)]
#a_minus = [(a[i] - abs(a[i])) // 2 for i in range(n)]

a_sum = [0]
a_plus_sum = [0]
#a_minus_sum = [0]

for i in range(n):
    a_sum.append(a_sum[-1] + a[i])
    a_plus_sum.append(a_plus_sum[-1] + a_plus[i])
    #a_minus_sum.append(a_minus_sum[-1] + a_minus[i])

best = -1
best_lost = 10**10
for i in range(k, n+1):
    temp_lost = (a_plus_sum[i] - a_plus_sum[i-k]) - max((a_sum[i] - a_sum[i-k]), 0)
    if best_lost >  temp_lost:
        best_lost = temp_lost
        best = i
        
ans = a_plus_sum[n] - best_lost
print(max(ans, 0))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
num = 0
ans = 0
for i in range(n):
  num += max(0,lis[i])
li = [0 for i in range(n-k+1)]
for i in range(n-k+1):
  cou = 0
  co = 0
  for nu in lis[i:i+k+1]:
    if nu >= 0:
      cou += nu
    else:
      co -= nu
  if cou >= co:ans = max(ans,num-co)
  else:ans = max(ans,num-cou)
  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = sum(i for i in a if i > 0)

# 長さkの区間で全て白にするか黒にするか決める
max_sk = 0
min_sk = float('inf')
sum_sk = sum(a[:k])
ma = 0
mi = 0
for i in range(k, n):
    sum_sk = sum_sk - a[i - k] + a[i]
    if max_sk < sum_sk:
        max_sk = sum_sk
        ma = i
    if min_sk > sum_sk:
        min_sk = sum_sk
        mi = i

d_ma = sum(-ai for ai in a[ma - k + 1: ma + 1] if ai < 0)
d_mi = sum(ai for ai in a[mi - k + 1: mi + 1] if ai > 0)
print(ans - min(d_ma, d_mi))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
b=[max([a[i],0]) for i in range(N)]
S=[0 for i in range(N+1)]
T=[0 for i in range(N+1)]
for i in range(N):
    S[i+1]=S[i]+a[i]
    T[i+1]=T[i]+b[i]
ans=-10**15
for i in range(N-K+1):
    M=S[i+K]-S[i]
    tmp=M
    if i!=0:
        tmp+=T[i-1]
    if i!=N-K:
        tmp+=T[N]-T[i+K+1]
    if ans<tmp:
        ans=tmp
for i in range(N-K+1):
    tmp=T[N]
    M=T[i+K]-T[i]
    tmp-=M
    if ans<tmp:
        ans=tmp
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
b=[max([a[i],0]) for i in range(N)]
S=[0 for i in range(N+1)]
T=[0 for i in range(N+1)]
for i in range(N):
    S[i+1]=S[i]+a[i]
    T[i+1]=T[i]+b[i]
ans=0
for i in range(N-K+1):
    M=S[i+K]-S[i]
    tmp=M
    if i!=0:
        tmp+=T[i-1]
    if i!=N-K:
        tmp+=T[N]-T[i+K+1]
    if ans<tmp:
        ans=tmp
for i in range(N-K+1):
    tmp=T[N]
    M=T[i+K]-T[i]
    tmp-=M
    if ans<tmp:
        ans=tmp
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
cumsum = [0]
for i in range(n):
	cumsum.append(cumsum[-1] + a[i])
def range_sum(l, r):
	return cumsum[r + 1] - cumsum[l]

cont_sum = -float("inf")
cont_index = None
for i in range(n - k + 1):
	if range_sum(i, i + k - 1) > cont_sum:
		cont_index = i
		cont_sum = range_sum(i, i + k - 1)

ans_pos = 0
for i in range(n):
	if cont_index <= i < cont_index + k:
		ans_pos += a[i]
	else:
		ans_pos += max(0, a[i])

cont_sum = float("inf")
cont_index = None
for i in range(n - k + 1):
	if range_sum(i, i + k - 1) < cont_sum:
		cont_index = i
		cont_sum = range_sum(i, i + k - 1)

ans_neg = 0
for i in range(n):
	if cont_index <= i < cont_index + k:
		ans_neg += a[i]
	else:
		ans_neg += min(0, a[i])
#print(ans_pos, ans_neg)
print(max(ans_pos, cumsum[-1] - ans_neg))